diff --git a/.gitee/ISSUE_TEMPLATE.zh-CN.md b/.gitee/ISSUE_TEMPLATE.zh-CN.md deleted file mode 100755 index f09d98dde9597de75ffcdb237c2b580b8fffa3f9..0000000000000000000000000000000000000000 --- a/.gitee/ISSUE_TEMPLATE.zh-CN.md +++ /dev/null @@ -1,13 +0,0 @@ -### 该问题是怎么引起的? - - - -### 重现步骤 - - - -### 报错信息 - - - - diff --git a/.gitee/PULL_REQUEST_TEMPLATE.zh-CN.md b/.gitee/PULL_REQUEST_TEMPLATE.zh-CN.md deleted file mode 100755 index 33948fdcb51264545ce5ae797f5310a1c06f871d..0000000000000000000000000000000000000000 --- a/.gitee/PULL_REQUEST_TEMPLATE.zh-CN.md +++ /dev/null @@ -1,15 +0,0 @@ -### 相关的Issue - - -### 原因(目的、解决的问题等) - - -### 描述(做了什么,变更了什么) - - -### 测试用例(新增、改动、可能影响的功能) - - - - - diff --git a/.gitignore b/.gitignore old mode 100755 new mode 100644 index cf00b53065085c1ce9fa1f91f6c0a2ace9803e7f..77dbb6a9907fa0834c076ac8e7b081d22840a628 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ /out -/fs/proc /test /include .config* @@ -22,4 +21,3 @@ platform/include/menuconfig.h platform/board.ld apps/init/init apps/shell/shell -lib/libc/musl diff --git a/BUILD.gn b/BUILD.gn index a7e30ac37004b50e47234bcd37a57aba6947243d..ad48b1216bade8fa5935fb363c0606b81d9edee5 100755 --- a/BUILD.gn +++ b/BUILD.gn @@ -1,68 +1,82 @@ -# Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. -# Copyright (c) 2020, 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("//build/lite/config/subsystem/lite_subsystem.gni") - -declare_args() { - enable_ohos_kernel_liteos_a_ext_build = true - LOSCFG_TEST_APPS = false -} - -lite_subsystem("kernel") { - subsystem_components = [] - - if (enable_ohos_kernel_liteos_a_ext_build == false) { - subsystem_components += [ - "//kernel/liteos_a/kernel", - "//kernel/liteos_a/net", - "//kernel/liteos_a/lib", - "//kernel/liteos_a/compat", - "//kernel/liteos_a/fs", - "//kernel/liteos_a/arch:platform_cpu", - ] - if (LOSCFG_SHELL) { - subsystem_components += [ "//kernel/liteos_a/shell" ] - } - } else { - deps = [":make"] - } -} - -action("make") { - script = "//build/lite/build_ext_components.py" - outputs = [ "$target_gen_dir/liteos_a_build_log.txt" ] - exec_path = rebase_path(rebase_path(".", root_build_dir)) - outdir = rebase_path(get_path_info(".", "out_dir")) - command = "make clean OUTDIR=$outdir && make rootfs VERSION=\"${ohos_version}\" -j 16 OUTDIR=$outdir" - args = [ - "--path=$exec_path", - "--prebuilts=sh build.sh ${board_name} ${ohos_build_compiler} ${ohos_build_type}", - "--command=${command}" - ] -} +# 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("//build/lite/config/component/lite_component.gni") +import("//build/lite/config/subsystem/lite_subsystem.gni") + +generate_notice_file("kernel_notice_file") { + module_name = "kernel" + module_source_dir_list = [ + "//third_party/FreeBSD", + "//third_party/musl", + "//third_party/zlib", + "//third_party/FatFs", + "//third_party/Linux_Kernel", + "//third_party/lwip", + "//third_party/NuttX", + "//third_party/mtd-utils", + ] +} + +declare_args() { + enable_ohos_kernel_liteos_a_ext_build = true + LOSCFG_TEST_APPS = false + tee_enable = "" +} + +lite_subsystem("kernel") { + subsystem_components = [] + + if (enable_ohos_kernel_liteos_a_ext_build == false) { + subsystem_components += [ + "//kernel/liteos_a/kernel", + "//kernel/liteos_a/net", + "//kernel/liteos_a/lib", + "//kernel/liteos_a/compat", + "//kernel/liteos_a/fs", + "//kernel/liteos_a/arch:platform_cpu", + ] + if (LOSCFG_SHELL) { + subsystem_components += [ "//kernel/liteos_a/shell" ] + } + } else { + deps = [ ":make" ] + } +} + +build_ext_component("make") { + exec_path = rebase_path(".", root_build_dir) + tee_enable = "false" + if (board_name == "hi3516dv300" && enable_tee_ree) { + tee_enable = "tee" + } + prebuilts = "sh build.sh ${board_name} ${ohos_build_compiler} ${root_build_dir} ${ohos_build_type} ${tee_enable} \"${device_company}\" \"${product_path}\"" + outdir = rebase_path(get_path_info(".", "out_dir")) + command = "make clean OUTDIR=$outdir && make rootfs VERSION=\"${ohos_version}\" -j 16 OUTDIR=$outdir" +} diff --git a/Kconfig b/Kconfig index 522800bd2e11be2a7ba428734b7e985dac8c886a..77380a272b92b7590b88f3e7cfb5aaa49fd84cc3 100755 --- a/Kconfig +++ b/Kconfig @@ -1,5 +1,5 @@ -# Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. -# Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. +# 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: @@ -42,11 +42,11 @@ choice config COMPILER_HIMIX_32 bool "arm-linux-ohoseabi" - depends on PLATFORM_HI3518EV300 || PLATFORM_HI3516DV300 + depends on PLATFORM_HI3518EV300 || PLATFORM_HI3516DV300 || PLATFORM_QEMU_ARM_VIRT_CA7 config COMPILER_CLANG_LLVM bool "clang-llvm" - depends on PLATFORM_HI3518EV300 || PLATFORM_HI3516DV300 + depends on PLATFORM_HI3518EV300 || PLATFORM_HI3516DV300 || PLATFORM_QEMU_ARM_VIRT_CA7 endchoice endmenu @@ -61,6 +61,15 @@ source "../../kernel/liteos_a/arch/Kconfig" ######################### config options of rootfs ##################### source "../../kernel/liteos_a/kernel/common/Kconfig" +######################### config options of patchfs ##################### +source "../../kernel/liteos_a/kernel/common/patchfs/Kconfig" + +config QUICK_START + bool "Enable QUICK_START" + default n + depends on DRIVERS && FS_VFS + help + Answer Y to enable LiteOS support quick start. endmenu ######################### config options of kernel ##################### @@ -84,6 +93,12 @@ source "../../kernel/liteos_a/fs/ramfs/Kconfig" source "../../kernel/liteos_a/fs/nfs/Kconfig" source "../../kernel/liteos_a/fs/proc/Kconfig" source "../../kernel/liteos_a/fs/jffs2/Kconfig" +source "../../kernel/liteos_a/fs/zpfs/Kconfig" +config ENABLE_READ_BUFFER + bool "Enable read buffer Option" + default n + help + Answer Y to add enable read buffer Option. endmenu ######################## config options of net ############################ @@ -197,6 +212,13 @@ config EXC_INTERACTION Answer Y to enable exception interaction for LiteOS, when the system enter exception, user can also interact with system by shell command like readreg, writereg,task,hwi. +config SCHED_DEBUG + bool "Enable sched debug Feature" + default n + depends on DEBUG_VERSION + help + If you wish to build LiteOS with support for sched debug. + config USER_INIT_DEBUG bool "Enable user init Debug" default n @@ -221,30 +243,6 @@ config MEM_DEBUG help Answer Y to enable LiteOS support mem debug. -config MEM_MUL_MODULE - bool "Enable Memory module statistics" - default n - depends on MEM_DEBUG - help - Answer Y to enable LiteOS support memory statistics depend on different module -config MEM_MUL_POOL - bool "Enable Memory multi-pool control" - default y - depends on DEBUG_VERSION && MEM_DEBUG - help - Answer Y to enable LiteOS support memory multi-pool control -config MEM_HEAD_BACKUP - bool "Enable Node Head Backup" - default 0 - depends on DEBUG_VERSION && MEM_DEBUG - help - Answer Y to enable node head backup -config MEM_RECORDINFO - bool "Enable Mem Record" - default n - depends on DEBUG_VERSION && MEM_DEBUG - help - Answer Y to enable mem record config MEM_LEAKCHECK bool "Enable Function call stack of Mem operation recorded" default n @@ -255,14 +253,10 @@ config BASE_MEM_NODE_INTEGRITY_CHECK bool "Enable integrity check or not " default n depends on DEBUG_VERSION && MEM_DEBUG -config BASE_MEM_NODE_SIZE_CHECK - bool "Enable size check or not" +config MEM_WATERLINE + bool "Enable memory pool waterline or not" default n depends on DEBUG_VERSION && MEM_DEBUG -config SLAB_INFO_STATISTIC - bool "Enable SLAB Statistic" - default n - depends on DEBUG_VERSION && KERNEL_MEM_SLAB && MEM_DEBUG config VM_OVERLAP_CHECK bool "Enable VM overlap check or not" @@ -274,23 +268,10 @@ config VM_OVERLAP_CHECK config NULL_ADDRESS_PROTECT bool "Enable NULL Address protect" default n - depends on (PLATFORM_HI3518EV200 || PLATFORM_HI3516CV300 || PLATFORM_HI3518EV300 || PLATFORM_HI3516DV300) && DEBUG_VERSION + depends on (PLATFORM_HI3518EV200 || PLATFORM_HI3516CV300 || PLATFORM_HI3518EV300 || PLATFORM_HI3516DV300 || PLATFORM_QEMU_ARM_VIRT_CA7) && DEBUG_VERSION help Answer Y to set mem address 0~1M prohibit to access, read or write will trigger exception. -choice - prompt "Enable Uart" - default DRIVERS_HDF_PLATFORM_UART - help - Enable simple uart (without vfs) only for litekernel. - Enable general uart (with vfs) for full code. -source "../../vendor/hisi/hi35xx/platform/uart/Kconfig" -config PLATFORM_UART_WITHOUT_VFS - bool "Simple Uart" -config PLATFORM_NO_UART - bool "NO Uart" - -endchoice endmenu ######################## config options os drivers ######################## @@ -301,42 +282,13 @@ config DRIVERS help Answer Y to enable LiteOS support driver. -source "../../vendor/hisi/hi35xx/platform/hiedmac/Kconfig" source "../../kernel/liteos_a/bsd/dev/usb/Kconfig" -source "../../drivers/hdf/lite/Kconfig" - -config DRIVERS_NETDEV - bool "Enable Net Device" - default y - depends on DRIVERS && NET_LWIP_SACK - help - Answer Y to enable LiteOS support net device. - -choice - depends on DRIVERS_NETDEV - prompt "MAC" - default n - help - Enable higmac for hi3516a hi3519 hi3559a_cortex-a53_aarch64. - Enable hieth-sf for hi3516cv300 hi3516ev200 and hi3518ev200. -source "../../vendor/hisi/hi35xx/platform/hieth-sf/Kconfig" -endchoice - -source "../../drivers/liteos/mem/Kconfig" -source "../../vendor/hisi/hi35xx/platform/mmc/Kconfig" - - -config DRIVERS_MTD - bool "Enable MTD" - default y - depends on DRIVERS && FS_VFS - help - Answer Y to enable LiteOS support jffs2 multipartion. +source "../../drivers/adapter/khdf/liteos/Kconfig" -source "../../vendor/hisi/hi35xx/platform/mtd/spi_nor/Kconfig" -source "../../drivers/liteos/random/Kconfig" +source "drivers/char/mem/Kconfig" +source "drivers/char/random/Kconfig" source "../../drivers/liteos/tzdriver/Kconfig" -source "../../drivers/liteos/video/Kconfig" +source "drivers/char/video/Kconfig" source "../../drivers/liteos/hievent/Kconfig" endmenu diff --git a/LICENSE b/LICENSE index 61fa69e3906c43870779e2c98903e447c8d86f73..208b4cd3556ada5a8fa98bcf0fade1795fa982c6 100644 --- a/LICENSE +++ b/LICENSE @@ -1,5 +1,5 @@ -Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. -Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. +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: @@ -25,4 +25,4 @@ 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. \ No newline at end of file +ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/Makefile b/Makefile index 673c0988207dcafe01280a0ca7e87bf50c8b5cc0..27dda5e627e6ae98464894eacaf9e1ccd2cf86ae 100755 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ -# Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. -# Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. +# 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: @@ -38,8 +38,6 @@ LITEOSTHIRDPARTY := $(LITEOSTOPDIR)/../../third_party export LITEOSTOPDIR export LITEOSTHIRDPARTY --include $(LITEOSTOPDIR)/tools/build/config.mk - RM = -rm -rf MAKE = make __LIBS = libs @@ -53,12 +51,34 @@ LITEOS_MENUCONFIG_H = $(LITEOSTOPDIR)/include/generated/autoconf.h LITEOS_PLATFORM_BASE = $(LITEOSTOPDIR)/platform LITEOS_PLATFORM_MENUCONFIG_H = $(LITEOS_PLATFORM_BASE)/include/menuconfig.h -ifeq ($(LOSCFG_PLATFORM_HI3518EV300), y) +export CONFIG_=LOSCFG_ +MENUCONFIG_PATH = $(LITEOSTOPDIR)/tools/menuconfig +KCONFIG_FILE_PATH = $(LITEOSTOPDIR)/Kconfig + +ifeq ($(OS), Linux) +MENUCONFIG_MCONF := $(MENUCONFIG_PATH)/mconf +MENUCONFIG_CONF := $(MENUCONFIG_PATH)/conf +else +MENUCONFIG_MCONF := $(MENUCONFIG_PATH)/kconfig-mconf.exe +MENUCONFIG_CONF := $(MENUCONFIG_PATH)/kconfig-conf.exe +endif + +$(shell env CONFIG_=$(CONFIG_) $(MENUCONFIG_CONF) -s --olddefconfig $(KCONFIG_FILE_PATH)) + +-include $(LITEOSTOPDIR)/tools/build/config.mk + +ifeq ($(LOSCFG_STORAGE_SPINOR), y) FSTYPE = jffs2 endif -ifeq ($(LOSCFG_PLATFORM_HI3516DV300), y) +ifeq ($(LOSCFG_STORAGE_EMMC), y) FSTYPE = vfat endif +ifeq ($(LOSCFG_STORAGE_SPINAND), y) +FSTYPE = yaffs2 +endif +ifeq ($(LOSCFG_PLATFORM_QEMU_ARM_VIRT_CA7), y) +FSTYPE = jffs2 +endif ROOTFS_DIR = $(OUT)/rootfs ROOTFS_ZIP = $(OUT)/rootfs.zip VERSION = @@ -103,8 +123,8 @@ $(__LIBS): $(OUT) $(CXX_INCLUDE) $(OUT): $(LITEOS_MENUCONFIG_H) $(HIDE)mkdir -p $(OUT)/lib - $(HIDE)$(CC) -I$(LITEOS_PLATFORM_BASE)/include -I$(LITEOSTOPDIR)/../../vendor/hisi/hi35xx/$(LITEOS_PLATFORM)/config/board \ - -E $(LITEOS_PLATFORM_BASE)/board.ld.S \ + $(HIDE)$(CC) -I$(LITEOSTOPDIR)/kernel/base/include -I$(LITEOSTOPDIR)/../../$(LOSCFG_BOARD_CONFIG_PATH) \ + -I$(LITEOS_PLATFORM_BASE)/include -E $(LITEOS_PLATFORM_BASE)/board.ld.S \ -o $(LITEOS_PLATFORM_BASE)/board.ld -P $(BUILD): @@ -117,15 +137,12 @@ $(LITEOS_LIBS_TARGET): $(__LIBS) $(HIDE)echo "=============== make lib done ===============" ##### make menuconfig ##### -export CONFIG_=LOSCFG_ -MENUCONFIG_PATH = $(LITEOSTOPDIR)/tools/menuconfig -KCONFIG_FILE_PATH = $(LITEOSTOPDIR)/Kconfig - -menuconfig:$(MENUCONFIG_PATH)/mconf +menuconfig:$(MENUCONFIG_MCONF) $< $(KCONFIG_FILE_PATH) -genconfig:$(MENUCONFIG_PATH)/conf +genconfig:$(MENUCONFIG_CONF) $(HIDE)mkdir -p include/config include/generated + $< --olddefconfig $(KCONFIG_FILE_PATH) $< --silentoldconfig $(KCONFIG_FILE_PATH) -mv -f $(LITEOS_MENUCONFIG_H) $(LITEOS_PLATFORM_MENUCONFIG_H) ##### menuconfig end ####### @@ -165,15 +182,16 @@ endif $(ROOTFSDIR): prepare $(APPS) $(HIDE)$(MAKE) clean -C apps - $(HIDE)$(shell $(LITEOSTOPDIR)/tools/scripts/make_rootfs/rootfsdir.sh $(OUT)/bin $(OUT)/musl $(ROOTFS_DIR)) + $(HIDE)$(LITEOSTOPDIR)/tools/scripts/make_rootfs/rootfsdir.sh $(OUT)/bin $(OUT)/musl $(ROOTFS_DIR) ifneq ($(VERSION),) - $(HIDE)$(shell $(LITEOSTOPDIR)/tools/scripts/make_rootfs/releaseinfo.sh "$(VERSION)" $(ROOTFS_DIR)) + $(HIDE)$(LITEOSTOPDIR)/tools/scripts/make_rootfs/releaseinfo.sh "$(VERSION)" $(ROOTFS_DIR) endif $(ROOTFS): $(ROOTFSDIR) - $(HIDE)$(shell $(LITEOSTOPDIR)/tools/scripts/make_rootfs/rootfsimg.sh $(ROOTFS_DIR) $(FSTYPE)) + $(HIDE)$(LITEOSTOPDIR)/tools/scripts/make_rootfs/rootfsimg.sh $(ROOTFS_DIR) $(FSTYPE) $(HIDE)cd $(ROOTFS_DIR)/.. && zip -r $(ROOTFS_ZIP) $(ROOTFS) ifneq ($(OUT), $(LITEOS_TARGET_DIR)) + rm -rf $(LITEOS_TARGET_DIR)rootfs $(HIDE)mv $(ROOTFS_DIR) $(LITEOS_TARGET_DIR)rootfs endif @@ -182,7 +200,7 @@ clean: do $(MAKE) -C $$dir clean|| exit 1; \ done $(HIDE)$(MAKE) -C apps clean - $(HIDE)$(RM) $(__OBJS) $(LITEOS_TARGET) $(OUT) $(BUILD) $(LITEOS_MENUCONFIG_H) *.bak *~ + $(HIDE)$(RM) $(__OBJS) $(LITEOS_TARGET) $(BUILD) $(LITEOS_MENUCONFIG_H) *.bak *~ $(HIDE)$(RM) $(LITEOS_PLATFORM_MENUCONFIG_H) $(HIDE)$(RM) include $(HIDE)$(MAKE) cleanrootfs @@ -199,4 +217,14 @@ cleanrootfs: $(HIDE)$(RM) $(OUT)/rootfs.zip $(HIDE)$(RM) $(OUT)/rootfs.img -.PHONY: all lib clean cleanall $(LITEOS_TARGET) debug release help +update_all_config: + $(HIDE)shopt -s globstar && for f in tools/build/config/**/*.config ; \ + do \ + echo updating $$f; \ + test -f $$f && cp $$f .config && $(MENUCONFIG_CONF) -s --olddefconfig $(KCONFIG_FILE_PATH) && $(MENUCONFIG_CONF) --savedefconfig $$f $(KCONFIG_FILE_PATH); \ + done + +%.config: + $(HIDE)test -f tools/build/config/$@ && cp tools/build/config/$@ .config && $(MENUCONFIG_MCONF) $(KCONFIG_FILE_PATH) && $(MENUCONFIG_CONF) --savedefconfig tools/build/config/$@ $(KCONFIG_FILE_PATH) + +.PHONY: all lib clean cleanall $(LITEOS_TARGET) debug release help update_all_config diff --git a/README.md b/README.md new file mode 100644 index 0000000000000000000000000000000000000000..7997be41716882bbd690bb0de8f7bc51f41735f5 --- /dev/null +++ b/README.md @@ -0,0 +1,96 @@ +# LiteOS Cortex-A + +- [Introduction](#section11660541593) +- [Directory Structure](#section161941989596) +- [Constraints](#section119744591305) +- [Usage](#section741617511812) + - [Preparations](#section1579912573329) + - [Source Code Acquisition](#section11443189655) + - [Compilation and Building](#section2081013992812) + +- [Repositories Involved](#section1371113476307) + +## Introduction + +The OpenHarmony LiteOS Cortex-A is a new-generation kernel developed based on the Huawei LiteOS kernel. Huawei LiteOS is a lightweight operating system \(OS\) built for the Internet of Things \(IoT\) field. With the rapid development of the IoT industry, OpenHarmony LiteOS Cortex-A brings small-sized, low-power, and high-performance experience and builds a unified and open ecosystem for developers. In addition, it provides rich kernel mechanisms, more comprehensive Portable Operating System Interface \(POSIX\), and a unified driver framework, Hardware Driver Foundation \(HDF\), which offers unified access for device developers and friendly development experience for application developers. [Figure1](#fig27311582210) shows the architecture of the OpenHarmony LiteOS Cortex-A kernel. + +**Figure 1** Architecture of the OpenHarmony LiteOS Cortex-A kernel +![](figures/architecture-of-the-openharmony-liteos-cortex-a-kernel.png "architecture-of-the-openharmony-liteos-cortex-a-kernel") + +## Directory Structure + +``` +/kernel/liteos_a +├── apps # User-space init and shell application programs +├── arch # System architecture, such as ARM +│ └── arm # Code for ARM architecture +├── bsd # Code of the driver and adaptation layer module related to the FreeBSD, such as the USB module +├── compat # Kernel API compatibility +│ └── posix # POSIX APIs +├── drivers # Kernel driver +│ └── char # Character device +│ ├── mem # Driver for accessing physical input/output (I/O) devices +│ ├── quickstart # APIs for quick start of the system +│ ├── random # Driver for random number generators +│ └── video # Framework of the framebuffer driver +├── fs # File system module, which mainly derives from the NuttX open-source project +│ ├── fat # FAT file system +│ ├── jffs2 # JFFS2 file system +│ ├── include # Header files exposed externally +│ ├── nfs # NFS file system +│ ├── proc # proc file system +│ ├── ramfs # RAMFS file system +│ └── vfs # VFS layer +├── kernel # Kernel modules including the process, memory, and IPC modules +│ ├── base # Basic kernel modules including the scheduling and memory modules +│ ├── common # Common components used by the kernel +│ ├── extended # Extended kernel modules including the dynamic loading, vDSO, and LiteIPC modules +│ ├── include # Header files exposed externally +│ └── user # Init process loading +├── lib # Kernel library +├── net # Network module, which mainly derives from the lwIP open-source project +├── platform # Code for supporting different systems on a chip (SOCs), such as Hi3516D V300 +│ ├── hw # Logic code related to clocks and interupts +│ ├── include # Header files exposed externally +│ └── uart # Logic code related to the serial port +├── platform # Code for supporting different systems on a chip (SOCs), such as Hi3516D V300 +├── security # Code related to security features, including process permission management and virtual ID mapping management +├── syscall # System calling +└── tools # Building tools as well as related configuration and code +``` + +## Constraints + +- Programming languages: C and C++ +- Applicable development boards: Hi3518E V300 and Hi3516D V300 +- Hi3518E V300 uses the JFFS2 file system by default, and Hi3516D V300 uses the FAT file system by default. + +## Usage + +OpenHarmony LiteOS Cortex-A supports the [Hi3518E V300](https://gitee.com/openharmony/docs/blob/master/docs-en/quick-start/introduction-to-the-hi3518-development-board.md) and [Hi3516D V300](https://gitee.com/openharmony/docs/blob/master/docs-en/quick-start/introduction-to-the-hi3516-development-board.md). You can develop and run your applications based on both development boards. + +### Preparations + +You need to set up the compilation environment on Linux. + +- For Hi3518E V300, see [Setting Up the Hi3518 Development Environment](https://gitee.com/openharmony/docs/blob/master/docs-en/quick-start/setting-up-the-hi3518-development-environment.md). +- For Hi3516D V300, see [Setting Up the Hi3516 Development Environment](https://gitee.com/openharmony/docs/blob/master/docs-en/quick-start/setting-up-the-hi3516-development-environment.md). + +### Source Code Acquisition + +Download and decompress a set of source code on a Linux server to acquire the [source code](https://repo.huaweicloud.com/harmonyos/os/1.0/code-1.0.tar.gz). For more acquisition methods, see [Source Code Acquisition](https://gitee.com/openharmony/docs/blob/master/docs-en/get-code/source-code-acquisition.md). + +### Compilation and Building + +For details about how to develop the first application, see: + +- [Developing the First Example Program Running on Hi3518](https://gitee.com/openharmony/docs/blob/master/docs-en/quick-start/developing-the-first-example-program-running-on-hi3518.md) + +- [Developing the First Example Program Running on Hi3516](https://gitee.com/openharmony/docs/blob/master/docs-en/quick-start/developing-the-first-example-program-running-on-hi3516.md) + +## Repositories Involved + +[drivers\_liteos](https://gitee.com/openharmony/drivers_liteos) + +**[kernel\_liteos\_a](https://gitee.com/openharmony/kernel_liteos_a)** + diff --git a/README_zh.md b/README_zh.md new file mode 100644 index 0000000000000000000000000000000000000000..61bcf07307d427506fa6c622e9413b2ffd740020 --- /dev/null +++ b/README_zh.md @@ -0,0 +1,96 @@ +# LiteOS-A内核 + +- [简介](#section11660541593) +- [目录](#section161941989596) +- [约束](#section119744591305) +- [使用说明](#section741617511812) + - [准备](#section1579912573329) + - [获取源码](#section11443189655) + - [编译构建](#section2081013992812) + +- [相关仓](#section1371113476307) + +## 简介 + +OpenHarmony LiteOS-A内核是基于Huawei LiteOS内核演进发展的新一代内核,Huawei LiteOS是面向IoT领域构建的轻量级物联网操作系统。在IoT产业高速发展的潮流中,OpenHarmony LiteOS-A内核能够带给用户小体积、低功耗、高性能的体验以及统一开放的生态系统能力,新增了丰富的内核机制、更加全面的POSIX标准接口以及统一驱动框架**HDF**(OpenHarmony Driver Foundation)等,为设备厂商提供了更统一的接入方式,为OpenHarmony的应用开发者提供了更友好的开发体验。图1为OpenHarmony LiteOS-A内核架构图: + +**图 1** OpenHarmony LiteOS-A内核架构图 +![](figures/OpenHarmony-LiteOS-A内核架构图.png "OpenHarmony-LiteOS-A内核架构图") + +## 目录 + +``` +/kernel/liteos_a +├── apps # 用户态的init和shell应用程序 +├── arch # 体系架构的目录,如arm等 +│ └── arm # arm架构代码 +├── bsd # freebsd相关的驱动和适配层模块代码引入,例如USB等 +├── compat # 内核接口兼容性目录 +│ └── posix # posix相关接口 +├── drivers # 内核驱动 +│ └── char # 字符设备 +│ ├── mem # 访问物理IO设备驱动 +│ ├── quickstart # 系统快速启动接口目录 +│ ├── random # 随机数设备驱动 +│ └── video # framebuffer驱动框架 +├── fs # 文件系统模块,主要来源于NuttX开源项目 +│ ├── fat # fat文件系统 +│ ├── jffs2 # jffs2文件系统 +│ ├── include # 对外暴露头文件存放目录 +│ ├── nfs # nfs文件系统 +│ ├── proc # proc文件系统 +│ ├── ramfs # ramfs文件系统 +│ └── vfs # vfs层 +├── kernel # 进程、内存、IPC等模块 +│ ├── base # 基础内核,包括调度、内存等模块 +│ ├── common # 内核通用组件 +│ ├── extended # 扩展内核,包括动态加载、vdso、liteipc等模块 +│ ├── include # 对外暴露头文件存放目录 +│ └── user # 加载init进程 +├── lib # 内核的lib库 +├── net # 网络模块,主要来源于lwip开源项目 +├── platform # 支持不同的芯片平台代码,如Hi3516DV300等 +│ ├── hw # 时钟与中断相关逻辑代码 +│ ├── include # 对外暴露头文件存放目录 +│ └── uart # 串口相关逻辑代码 +├── platform # 支持不同的芯片平台代码,如Hi3516DV300等 +├── security # 安全特性相关的代码,包括进程权限管理和虚拟id映射管理 +├── syscall # 系统调用 +└── tools # 构建工具及相关配置和代码 +``` + +## 约束 + +- 开发语言:C/C++; +- 适用于Hi3518EV300、Hi3516DV300单板; +- Hi3518EV300默认使用jffs2文件系统,Hi3516DV300默认使用FAT文件系统。 + +## 使用说明 + +OpenHarmony LiteOS-A内核支持Hi3518EV300([介绍](https://gitee.com/openharmony/docs/blob/master/quick-start/Hi3518%E5%BC%80%E5%8F%91%E6%9D%BF%E4%BB%8B%E7%BB%8D.md))、Hi3516DV300([介绍](https://gitee.com/openharmony/docs/blob/master/quick-start/Hi3516%E5%BC%80%E5%8F%91%E6%9D%BF%E4%BB%8B%E7%BB%8D.md))单板,开发者可基于两种单板开发运行自己的应用程序。 + +### 准备 + +开发者需要在Linux上搭建编译环境: + +- Hi3518EV300单板:参考[环境搭建](https://gitee.com/openharmony/docs/blob/master/quick-start/Hi3518%E6%90%AD%E5%BB%BA%E7%8E%AF%E5%A2%83.md); +- Hi3516DV300单板:参考[环境搭建](https://gitee.com/openharmony/docs/blob/master/quick-start/Hi3516%E6%90%AD%E5%BB%BA%E7%8E%AF%E5%A2%83.md)。 + +### 获取源码 + +在Linux服务器上下载并解压一套源代码,获取源码([下载链接](https://repo.huaweicloud.com/harmonyos/os/1.0/code-1.0.tar.gz))。更多源码获取方式,参考[源码获取](https://gitee.com/openharmony/docs/blob/master/get-code/%E6%BA%90%E7%A0%81%E8%8E%B7%E5%8F%96.md)。 + +### 编译构建 + +开发者开发第一个应用程序可参考: + +- [helloworld for Hi3518EV300](https://gitee.com/openharmony/docs/blob/master/quick-start/%E5%BC%80%E5%8F%91Hi3518%E7%AC%AC%E4%B8%80%E4%B8%AA%E7%A4%BA%E4%BE%8B%E7%A8%8B%E5%BA%8F.md); + +- [helloworld for Hi3516DV300](https://gitee.com/openharmony/docs/blob/master/quick-start/%E5%BC%80%E5%8F%91Hi3516%E7%AC%AC%E4%B8%80%E4%B8%AA%E5%BA%94%E7%94%A8%E7%A8%8B%E5%BA%8F%E7%A4%BA%E4%BE%8B.md)。 + +## 相关仓 + +[drivers\_liteos](https://gitee.com/openharmony/drivers_liteos) + +**[kernel\_liteos\_a](https://gitee.com/openharmony/kernel_liteos_a)** + diff --git a/apps/Makefile b/apps/Makefile index e5857242c98fd1487eb44350bc36c1ad427094f0..631b955cf9701268b5048c7dc123629296263c63 100755 --- a/apps/Makefile +++ b/apps/Makefile @@ -1,5 +1,5 @@ -# Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. -# Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. +# 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: @@ -38,7 +38,7 @@ all: $(APPS) # Make $(APPS): ifneq ($(APP_SUBDIRS), ) - $(HIDE) for dir in $(APP_SUBDIRS); do $(MAKE) -C $$dir ; done + $(HIDE) for dir in $(APP_SUBDIRS); do $(MAKE) -C $$dir || exit 1; done endif clean: diff --git a/apps/config.mk b/apps/config.mk index 8e4c95f9508efe3bcf374ae634377380c9c86a60..5f6efe06d4d1f2c572a05a9c4fb578f781c107b0 100755 --- a/apps/config.mk +++ b/apps/config.mk @@ -1,5 +1,5 @@ -# Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. -# Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. +# 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: diff --git a/apps/init/Makefile b/apps/init/Makefile index 82017e7e6cad05beb8bd124c21c795757f33adeb..96eba811382793af81db1ebad894e6b0877440e1 100755 --- a/apps/init/Makefile +++ b/apps/init/Makefile @@ -1,5 +1,5 @@ -# Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. -# Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. +# 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: @@ -43,6 +43,12 @@ ifeq ($(LOSCFG_COMPILER_CLANG_LLVM), y) LOCAL_FLAGS += -Wno-shift-op-parentheses -Wno-bitwise-op-parentheses -Wnonnull $(LLVM_SYSROOT) LDCFLAGS += $(LLVM_EXTRA_LD_OPTS) $(LLVM_SYSROOT) endif + +ifeq ($(LOSCFG_QUICK_START), y) +LDCFLAGS += --static +CFLAGS += -DLOSCFG_QUICK_START +endif + INITNAME := init all: $(INITNAME) diff --git a/apps/init/src/init.c b/apps/init/src/init.c index 882219df6f2f58210942b61e5cc4a55d94426804..7cab38a83105451745031a4b2e9446aea53b0c24 100755 --- a/apps/init/src/init.c +++ b/apps/init/src/init.c @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: @@ -35,11 +35,41 @@ #include #include +#ifdef LOSCFG_QUICK_START +#include +#include +#include +#include +#include + +#define QUICKSTART_IOC_MAGIC 'T' +#define QUICKSTART_INITSTEP2 _IO(QUICKSTART_IOC_MAGIC, 0) +#define WAIT_FOR_SAMPLE 300000 // wait 300ms for sample +#endif int main(int argc, char * const *argv) { int ret; const char *shellPath = "/bin/shell"; +#ifdef LOSCFG_QUICK_START + const char *samplePath = "/dev/shm/sample_quickstart"; + + ret = fork(); + if (ret < 0) { + printf("Failed to fork for sample_quickstart\n"); + } else if (ret == 0) { + (void)execve(samplePath, NULL, NULL); + exit(0); + } + + usleep(WAIT_FOR_SAMPLE); + + int fd = open("/dev/quickstart", O_RDONLY); + if (fd != -1) { + ioctl(fd, QUICKSTART_INITSTEP2); + close(fd); + } +#endif ret = fork(); if (ret < 0) { printf("Failed to fork for shell\n"); diff --git a/apps/module.mk b/apps/module.mk index 1cd7ae1164c004b2db132b9777eab6631d6feca7..520f339354222e8a2d52632fb78b249aa9e0ee40 100755 --- a/apps/module.mk +++ b/apps/module.mk @@ -1,5 +1,5 @@ -# Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. -# Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. +# 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: @@ -38,3 +38,7 @@ endif ifeq ($(LOSCFG_USER_INIT_DEBUG), y) APP_SUBDIRS += init endif + +ifeq ($(LOSCFG_NET_LWIP_SACK_TFTP), y) +APP_SUBDIRS += tftp +endif diff --git a/apps/shell/Makefile b/apps/shell/Makefile index 857cb1367216a2927e0a44b62692d2ce2686c0df..23024cac865c6f7d453e7169569c006879fa3ff9 100755 --- a/apps/shell/Makefile +++ b/apps/shell/Makefile @@ -1,5 +1,5 @@ -# Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. -# Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. +# 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: diff --git a/apps/shell/builtin/cd.c b/apps/shell/builtin/cd.c index 56d6897e3df557dec7456374a0db53acd864b046..6fdd41d36c99730306c6b965bd4b2f8d8c04203e 100755 --- a/apps/shell/builtin/cd.c +++ b/apps/shell/builtin/cd.c @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/apps/shell/include/shcmd.h b/apps/shell/include/shcmd.h index d664a4bd8ff4d3060aec0799257a2779b0bc59b2..f5485a2a9278c3624ca2c64f01eb30d66b1130fd 100755 --- a/apps/shell/include/shcmd.h +++ b/apps/shell/include/shcmd.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/apps/shell/include/shcmdparse.h b/apps/shell/include/shcmdparse.h old mode 100755 new mode 100644 index e3206220497e6c6fb41fe1be81847f1b878cd8ba..a6ec619732885be889efd3fbddad34933dbcd30d --- a/apps/shell/include/shcmdparse.h +++ b/apps/shell/include/shcmdparse.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/apps/shell/include/shell.h b/apps/shell/include/shell.h index 48fa3093b772b2c16da350dec6f7d008909447af..d23b9e1b056584ad54e29e5cb7de257e8c7ba5de 100755 --- a/apps/shell/include/shell.h +++ b/apps/shell/include/shell.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/apps/shell/include/shell_list.h b/apps/shell/include/shell_list.h old mode 100755 new mode 100644 index 91b5bbd33f2119d11fb5210129c8a0d8709b8b24..020c1711d8063a129970fd34b69fa287e2567276 --- a/apps/shell/include/shell_list.h +++ b/apps/shell/include/shell_list.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/apps/shell/include/shell_pri.h b/apps/shell/include/shell_pri.h old mode 100755 new mode 100644 index b8e75c1c641856c55b7d9699b812ff9e4b0bc481..8e5ab6f6458a49b5c1f1b909385c3a47fa98aac4 --- a/apps/shell/include/shell_pri.h +++ b/apps/shell/include/shell_pri.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/apps/shell/include/sherr.h b/apps/shell/include/sherr.h old mode 100755 new mode 100644 index 6122fd2c4efb3f4ab575b22cf3768878601b8e74..a560a740721eacc0e928eca02c55056ab903e5af --- a/apps/shell/include/sherr.h +++ b/apps/shell/include/sherr.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/apps/shell/include/shmsg.h b/apps/shell/include/shmsg.h index fb683559799550cf088b331a18b0c316d93fc04b..ee90ec6c858f7f2995351cfa4b8d0dd1b5023b41 100755 --- a/apps/shell/include/shmsg.h +++ b/apps/shell/include/shmsg.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/apps/shell/include/show.h b/apps/shell/include/show.h index 95c56b125e0967065c07fb865bf553719bbce44f..2bb9486576fafd7ddfa35c212d379799a0ddedca 100755 --- a/apps/shell/include/show.h +++ b/apps/shell/include/show.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/apps/shell/src/main.c b/apps/shell/src/main.c index 54c0bf9fdc10cfae4582608e1ce9b23adbd176b7..ef57ca00b1fef7321dc3a278c055f725a79aee72 100755 --- a/apps/shell/src/main.c +++ b/apps/shell/src/main.c @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: @@ -98,6 +98,8 @@ int main() int ret = SH_NOK; ShellCB *shellCB = NULL; + setbuf(stdout, NULL); + shellCB = (ShellCB *)malloc(sizeof(ShellCB)); if (shellCB == NULL) { goto ERR_OUT1; diff --git a/apps/shell/src/shcmd.c b/apps/shell/src/shcmd.c index 06845ac8e3c409d9a2b9decaf083594a4a6918b0..a4930a902a9abac2277a21a67b8d920e426b2f68 100755 --- a/apps/shell/src/shcmd.c +++ b/apps/shell/src/shcmd.c @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: @@ -406,7 +406,7 @@ unsigned int OsCmdKeyShift(const char *cmdKey, char *cmdOut, unsigned int size) } output = (char *)malloc(len + 1); if (output == NULL) { - printf("malloc failure in %s[%d]", __FUNCTION__, __LINE__); + printf("malloc failure in %s[%d]\n", __FUNCTION__, __LINE__); return (unsigned int)SH_ERROR; } diff --git a/apps/shell/src/shcmdparse.c b/apps/shell/src/shcmdparse.c index 1a9bc75e1b96e435dcd6d70976fb240bbb9ff3eb..978917475d4c7e992d5fe78f6951030adbc46b98 100755 --- a/apps/shell/src/shcmdparse.c +++ b/apps/shell/src/shcmdparse.c @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/apps/shell/src/shmsg.c b/apps/shell/src/shmsg.c index a10b4c8058024a3b8e2d543696ab260892ca97bb..d8c17c09a7f08847f63e3746f69f863598226f67 100755 --- a/apps/shell/src/shmsg.c +++ b/apps/shell/src/shmsg.c @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: @@ -61,9 +61,19 @@ char *GetCmdline(ShellCB *shellCB) } cmdNode = SH_LIST_ENTRY(cmdkey->list.pstNext, CmdKeyLink, list); + if (cmdNode == NULL) { + (void)pthread_mutex_unlock(&shellCB->keyMutex); + return NULL; + } + SH_ListDelete(&(cmdNode->list)); (void)pthread_mutex_unlock(&shellCB->keyMutex); + if (strlen(cmdNode->cmdString) == 0) { + free(cmdNode); + return NULL; + } + return cmdNode->cmdString; } @@ -73,7 +83,8 @@ static void ShellSaveHistoryCmd(char *string, ShellCB *shellCB) CmdKeyLink *cmdkey = SH_LIST_ENTRY(string, CmdKeyLink, cmdString); CmdKeyLink *cmdNxt = NULL; - if ((string == NULL) || (*string == '\n') || (strlen(string) == 0)) { + if (*string == '\n') { + free(cmdkey); return; } @@ -87,7 +98,7 @@ static void ShellSaveHistoryCmd(char *string, ShellCB *shellCB) } } - if (cmdHistory->count == CMD_HISTORY_LEN) { + if (cmdHistory->count >= CMD_HISTORY_LEN) { cmdNxt = SH_LIST_ENTRY(cmdHistory->list.pstNext, CmdKeyLink, list); SH_ListDelete(&(cmdNxt->list)); SH_ListTailInsert(&(cmdHistory->list), &(cmdkey->list)); diff --git a/apps/tftp/Makefile b/apps/tftp/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..37cf594c1123840299d5eabc0bdb9f121c02236b --- /dev/null +++ b/apps/tftp/Makefile @@ -0,0 +1,77 @@ +# 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. + +ROOT_DIR := $(dir $(shell pwd))/tftp/ + +ifeq ($(APPSTOPDIR), ) +APPSTOPDIR := $(shell pwd)/../ +LITEOSTOPDIR = $(APPSTOPDIR)/../ +endif +include $(ROOT_DIR)/../config.mk +APPS_OUT := $(OUT)/bin + +SECUREC_DIR := $(LITEOSTHIRDPARTY)/bounds_checking_function + +LOCAL_SRCS = $(wildcard src/*.c) +LOCAL_SRCS += $(wildcard $(SECUREC_DIR)/src/strncpy_s.c) +LOCAL_SRCS += $(wildcard $(SECUREC_DIR)/src/memcpy_s.c) +LOCAL_SRCS += $(wildcard $(SECUREC_DIR)/src/memset_s.c) +LOCAL_SRCS += $(wildcard $(SECUREC_DIR)/src/strncat_s.c) +LOCAL_OBJ := $(LOCAL_SRCS:.c=.o) + +LOCAL_INCLUDE := \ + -I $(ROOT_DIR)/include/ \ + -I $(SECUREC_DIR)/include/\ + +LOCAL_FLAGS += $(CFLAGS) + +LDPATH := -L$(ROOT_DIR)/ +ifeq ($(LOSCFG_COMPILER_CLANG_LLVM), y) +LOCAL_FLAGS += -Wno-shift-op-parentheses -Wno-bitwise-op-parentheses $(LLVM_SYSROOT) +LDCFLAGS += $(LLVM_EXTRA_LD_OPTS) $(LLVM_SYSROOT) +endif + +TARGETNAME := tftp + +all: $(TARGETNAME) + +$(LOCAL_OBJ): %.o : %.c + $(HIDE) $(CC) $(LOCAL_FLAGS) -fPIE $(LOCAL_INCLUDE) -c $< -o $@ + +$(TARGETNAME): $(LOCAL_OBJ) + $(HIDE) $(CC) -pie -s $(LDPATH) $(BASE_OPTS) -o $(TARGETNAME) $^ $(LDCFLAGS) + $(HIDE) mkdir -p $(APPS_OUT) + $(HIDE) $(MV) $(TARGETNAME) $(APPS_OUT) + $(HIDE) $(RM) $(LOCAL_OBJ) + +clean: + $(HIDE) $(RM) $(LOCAL_OBJ) + $(HIDE) $(RM) $(TARGETNAME) + +.PHONY: all $(TARGETNAME) clean diff --git a/apps/tftp/include/tftpc.h b/apps/tftp/include/tftpc.h new file mode 100644 index 0000000000000000000000000000000000000000..3d0bf568ced68334ff1fd33f8f6d55bf5bbc7074 --- /dev/null +++ b/apps/tftp/include/tftpc.h @@ -0,0 +1,309 @@ +/* + * 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. + */ + +#ifndef TFTPC_H +#define TFTPC_H + +#include "types_adapt.h" + +#if LWIP_TFTP /* don't build if not configured for use in lwipopts.h */ + +#if defined (__cplusplus) && __cplusplus +extern "C" { +#endif + +#define TFTP_NULL_UINT32 ((u32_t)0xffffffffUL) + +#define TFTP_NULL_INT32 -1 + +/** @cond liteos +* @defgroup TFTP_Interfaces +* @ingroup Enums +* * This section contains the TFTP enums. +*/ +/** +* +* This Enum is used to specify the transfer mode of the file to be handled by TFTP client. +*/ +typedef enum tagTFTPC_TransferMode { + TRANSFER_MODE_ASCII = 0, /**< Indicates that the mode of transfer is ASCII. */ + TRANSFER_MODE_BINARY, /**< Indicates that the mode of transfer is Binary */ + TRANSFER_MODE_BUTT /**< Indicates invalid transfer mode.*/ +} TFTPC_TRANSFER_MODE_E; + +/** +* This Enum is used to specify the transfer mode to be handled by TFTP client +* This Enum indicates the TFTP client transfer mode of the file + +*/ +typedef enum tagTFTPC_ErrCode { + TFTPC_SOCKET_FAILURE = 1, /**< Error while creating UDP socket. */ + TFTPC_BIND_FAILURE = 2, /**< Error while binding to the UDP socket. */ + TFTPC_SELECT_ERROR = 3, /**< Error returned by select() system call. */ + TFTPC_RECVFROM_ERROR = 4, /**< Error while receiving data from the peer. */ + TFTPC_SENDTO_ERROR = 5, /**< Error while sending data to the peer. */ + TFTPC_FILE_NOT_FOUND = 6, /**< Requested file is not found. */ + + /**< This is the error sent by the server when host name cannot be resolved. */ + TFTPC_CANNOT_RESOLVE_HOSTNAME = 7, + TFTPC_INVALID_PARAVALUE = 8, /**< Input parameters passed to TFTP interfaces are invalid. */ + + /**< Error detected in TFTP packet or the error received from the TFTP server. */ + TFTPC_PROTO_ERROR = 9, + /**< Error during packet synchronization while sending or unexpected packet is received. */ + TFTPC_SYNC_FAILURE = 10, + /**< File size limit crossed, Max block can be 0xFFFF, each block containing 512 bytes. */ + TFTPC_FILE_TOO_BIG = 11, + TFTPC_SRC_FILENAME_LENGTH_ERROR = 12, /**< File name length greater than 256. */ + TFTPC_IP_NOT_WITHIN_RANGE = 13, /**< Host name IP is not valid. */ + TFTPC_ACCESS_ERROR = 14, /**< TFTP server returned file access error. */ + + /**< TFTP server returned error signifying that the DISK is full to write. */ + TFTPC_DISK_FULL = 15, + TFTPC_FILE_EXISTS = 16, /**< TFTP server returned error signifying that the file exists. */ + + /**< tftp_put_file_by_filename returned error signifying that the source file name do not exist. */ + TFTPC_FILE_NOT_EXIST = 17, + TFTPC_MEMALLOC_ERROR = 18, /**< Memory allocation failed in TFTP client. */ + TFTPC_FILEOPEN_ERROR = 19, /**< File open failed. */ + TFTPC_FILEREAD_ERROR = 20, /**< File read error. */ + TFTPC_FILECREATE_ERROR = 21, /**< File create error. */ + TFTPC_FILEWRITE_ERROR = 22, /**< File write error. */ + TFTPC_TIMEOUT_ERROR = 23, /**< Max time expired while waiting for file to be received. */ + + /**< Error when the received packet is less than 4 bytes (error length) or greater than 512 bytes. */ + TFTPC_PKT_SIZE_ERROR = 24, + TFTPC_ERROR_NOT_DEFINED = 25, /**< Returned by TFTP server for protocol user error. */ + TFTPC_DEST_PATH_LENGTH_ERROR = 26, /**< If the destination file path length is greater than 256. */ + TFTPC_UNKNOWN_TRANSFER_ID = 27, /**< Returned by TFTP server for undefined transfer ID. */ + + /**< IOCTL function failed at TFTP client while setting the socket to non-block. */ + TFTPC_IOCTLSOCKET_FAILURE = 28, + TFTPC_MEMCPY_FAILURE = 29 /**< TFTP memcpy failure. */ +} TFTPC_ERR_CODE_E; + +typedef enum tagTFTPC_OpCode { + TFTPC_OP_RRQ = 1, /* read request */ + TFTPC_OP_WRQ, /* write request */ + TFTPC_OP_DATA, /* data packet */ + TFTPC_OP_ACK, /* acknowledgement */ + TFTPC_OP_ERROR, /* error code */ + TFTPC_OP_OPT /* option code */ +} TFTPC_OPCODE_E; + +typedef enum tagTFTPC_PROTOCOL_ErrCode { + TFTPC_PROTOCOL_USER_DEFINED = 0, + TFTPC_PROTOCOL_FILE_NOT_FOUND, + TFTPC_PROTOCOL_ACCESS_ERROR, + TFTPC_PROTOCOL_DISK_FULL, + TFTPC_PROTOCOL_PROTO_ERROR, + TFTPC_PROTOCOL_UNKNOWN_TRANSFER_ID, + TFTPC_PROTOCOL_FILE_EXISTS, + TFTPC_PROTOCOL_CANNOT_RESOLVE_HOSTNAME +} TFTPC_PROT_ERRCODE_E; + + +#ifndef TFTPC_MAX_SEND_REQ_ATTEMPTS +#define TFTPC_MAX_SEND_REQ_ATTEMPTS 5 /* tftp max attempts */ +#endif + +#ifndef TFTPC_TIMEOUT_PERIOD +#define TFTPC_TIMEOUT_PERIOD 5 /* tftp timeout period,unit :s */ +#endif + +#define TFTPC_SERVER_PORT 69 /* tftp server well known port no. */ + +/* MAX file size in TFTP is 32 MB. + Reason for keeping 75 here , is ((75*512=38400bytes)/1024) = 37MB. So the recv/Send Loop can + receive the complete MAX message from the network +*/ +#define TFTPC_MAX_WAIT_IN_LOOP 75 + +#define TFTP_BLKSIZE 512 /* data block size (IEN-133) */ +#define TFTP_HDRSIZE 4 /* TFTP header size */ +#define TFTP_PKTSIZE (TFTP_BLKSIZE + TFTP_HDRSIZE) /* Packet size */ +#define TFTP_MAX_MODE_SIZE 9 /* max size of mode string */ +#define TFTP_MAXERRSTRSIZE 100 /* max size of error message string */ +#define TFTP_MAX_PATH_LENGTH 256 /* Max path or filename length */ +#define TFTP_MAX_BLK_NUM (0xFFFFL) /* MAximum block number */ + +/* IP address not including reserved IPs(0 and 127) and multicast addresses(Class D) */ +#define TFTPC_IP_ADDR_MIN 0x01000000 +#define TFTPC_IP_ADDR_EX_RESV 0x7effffff +#define TFTPC_IP_ADDR_CLASS_B 0x80000000 +#define TFTPC_IP_ADDR_EX_CLASS_DE 0xdfffffff + +#define TFTPC_FOUR 4 /* minimum packet size */ + +/****************************************************************************/ +/* Structure definitions */ +/****************************************************************************/ +/* Tftp data packet */ +typedef struct tagTFTPC_DATA { + u16_t usBlknum; /* block number */ + u8_t ucDataBuf[TFTP_BLKSIZE]; /* Actual data */ +} TFTPC_DATA_S; + + +/* TFTP error packet */ +typedef struct tagTFTPC_ERROR { + u16_t usErrNum; /* error number */ + u8_t ucErrMesg[TFTP_MAXERRSTRSIZE]; /* error message */ +} TFTPC_ERROR_S; + + +/* TFTP packet format */ +typedef struct tagTFTPC_PACKET { + u16_t usOpcode; /* Opcode value */ + union { + /* it contains mode and filename */ + s8_t ucName_Mode[TFTP_MAX_PATH_LENGTH + TFTP_MAX_MODE_SIZE]; + u16_t usBlknum; /* Block Number */ + TFTPC_DATA_S stTFTP_Data; /* Data Packet */ + TFTPC_ERROR_S stTFTP_Err; /* Error Packet */ + } u; +} TFTPC_PACKET_S; + + +/** @defgroup TFTP_Interfaces +* This section contains the TFTP Interfaces +*/ +/* +Func Name: lwip_tftp_get_file_by_filename +*/ +/** +* @ingroup TFTP_Interfaces +* @brief +* This API gets the source file from the server. It then stores the received file in the destination path +* on the client system. +* +* @param[in] ulHostAddr IP address of Host. This is the TFTP server IP. [NA] +* @param[in] usTftpServPort TFTP server port. If the value is passed as 0 then the default TFTP +* PORT 69 is used. [NA] +* @param[in] ucTftpTransMode File transfer mode, either TRANSFER_MODE_BINARY or TRANSFER_MODE_ASCII. [NA] +* @param[in] szSrcFileName Source file in the tftp server. [NA] +* @param[in] szDestDirPath Destination file path in the in the client. [NA] +* @param[out] [N/A] +* +* @return +* ERR_OK: On success \n +* TFTPC_ERR_CODE_E: On failure +* +* @note +* \n +* The behavior of this API is such that if the destination file already exists, it will be overwritten. +*/ +u32_t lwip_tftp_get_file_by_filename(u32_t ulHostAddr, + u16_t usTftpServPort, + u8_t ucTftpTransMode, + s8_t *szSrcFileName, + s8_t *szDestDirPath); + + +/* @defgroup TFTP_Interfaces +* This section contains the TFTP Interfaces +*/ +/* +Func Name: lwip_tftp_put_file_by_filename +*/ +/** +* @ingroup TFTP_Interfaces + +* @brief +* This API reads the contents of the source file on the client system and sends it to the server and +* server then receives the data and stores it in the specified destination path. +* +* @param[in] ulHostAddr Indicates the IP address of Host. This is the TFTP server IP. +* @param[in] usTftpServPort Indicates the TFTP server port. If the value is passed as 0 then the default TFTP +* PORT 69 is used. +* @param[in] ucTftpTransMode Indicates the file transfer mode, either TRANSFER_MODE_BINARY or TRANSFER_MODE_ASCII. +* @param[in] szSrcFileName Indicates the source file in the client. +* @param[in] szDestDirPath Indicates the destination file path on the tftp server. +* +* @return +* ERR_OK: On success \n +* TFTPC_ERR_CODE_E: On failure +* +*/ +u32_t lwip_tftp_put_file_by_filename(u32_t ulHostAddr, + u16_t usTftpServPort, + u8_t cTftpTransMode, + s8_t *szSrcFileName, + s8_t *szDestDirPath); + +#ifdef TFTP_TO_RAWMEM +/* @defgroup TFTP_Interfaces +* This section contains the TFTP Interfaces +*/ +/* +Func Name: lwip_tftp_get_file_by_filename_to_rawmem +*/ +/** +* @ingroup TFTP_Interfaces + +* @brief +* This API gets the source file from the server. It then stores the received file in the target memory +* on the client system. +* +* @param[in] ulHostAddr Indicates the IP address of the Host. This is the TFTP server IP. +* @param[in] usTftpServPort Indicates the TFTP server port. If the value is passed as 0 then the default TFTP +* PORT 69 is used. +* @param[in] ucTftpTransMode Indicates the File transfer mode, either TRANSFER_MODE_BINARY or TRANSFER_MODE_ASCII. +* @param[in] szSrcFileName Indicates the Source file in the TFTP server. +* @param[in] szDestMemAddr Indicates the target memory address in the client. +* @param[in/out] ulFileLength Indicates the target memory address can cache the size of the content, + and The real size of the Source file. +* +* @return +* ERR_OK: On success \n +* TFTPC_ERR_CODE_E: On failure +* @note + +* 1.You must define TFTP_TO_RAWMEM when using this API. \n +* 2.The behavior of this API is such that if the destination file already exists, it will be overwritten. +* @endcond +*/ + +u32_t lwip_tftp_get_file_by_filename_to_rawmem(u32_t ulHostAddr, + u16_t usTftpServPort, + u8_t ucTftpTransMode, + s8_t *szSrcFileName, + s8_t *szDestMemAddr, + u32_t *ulFileLength); +#endif + +#if defined (__cplusplus) && __cplusplus +} +#endif + +#endif /* LWIP_TFTP */ + +#endif /* TFTPC_H */ diff --git a/apps/tftp/include/types_adapt.h b/apps/tftp/include/types_adapt.h new file mode 100644 index 0000000000000000000000000000000000000000..5634495f6046970cb535b99de3f0906753dd6a2d --- /dev/null +++ b/apps/tftp/include/types_adapt.h @@ -0,0 +1,88 @@ +/* + * 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. + */ + +#ifndef TYPES_ADAPT_H +#define TYPES_ADAPT_H + +#include +#include +#include + +#define LWIP_TFTP 1 +#define LOSCFG_NET_LWIP_SACK_TFTP 1 +#define LOSCFG_SHELL_CMD_DEBUG 1 + +#define u8_t uint8_t +#define s8_t int8_t +#define u16_t uint16_t +#define s16_t int16_t +#define u32_t uint32_t +#define s32_t int32_t + +#define X8_F "02" PRIx8 +#define U16_F PRIu16 +#define S16_F PRId16 +#define X16_F PRIx16 +#define U32_F PRIu32 +#define S32_F PRId32 +#define X32_F PRIx32 +#define SZT_F PRIuPTR + +#define PRINTK(fmt, ...) printf(fmt, ##__VA_ARGS__) +#define LWIP_ASSERT(msg, expr) assert(expr) +#define LWIP_DEBUGF(module, msg) PRINTK msg + +#define LOS_OK 0 +#define LOS_NOK 1 +#define ERR_OK 0 +#define EOK 0 + +#define mem_malloc malloc +#define mem_free free +#define lwip_socket socket +#define lwip_ioctl ioctl +#define lwip_close close +#define lwip_bind bind +#define lwip_sendto sendto +#define lwip_recvfrom recvfrom + +#define IPADDR_NONE INADDR_NONE +#define DEFFILEMODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) + +#define SHELLCMD_ENTRY(l, cmdType, cmdKey, paraNum, cmdHook) \ +int main(int argc, const char **argv) \ +{ \ + return (int)cmdHook(argc, argv); \ +} + +typedef u32_t (*CmdCallBackFunc)(u32_t argc, const char **argv); + +#endif /* TYPES_ADAPT_H */ diff --git a/apps/tftp/src/main.c b/apps/tftp/src/main.c new file mode 100644 index 0000000000000000000000000000000000000000..4a476c746395d8ff7ca51c4fb78c6db280799090 --- /dev/null +++ b/apps/tftp/src/main.c @@ -0,0 +1,160 @@ +/* + * 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 +#include +#include +#include +#include +#include +#include +#include +#include "tftpc.h" + +#ifdef LOSCFG_NET_LWIP_SACK_TFTP +static int tcpip_init_finish = 1; +static char *TftpError[] = { + "TFTP transfer finish\n", + "Error while creating UDP socket\n", + "Error while binding to the UDP socket\n", + "Error returned by select() system call\n", + "Error while receiving data from the peer\n", + "Error while sending data to the peer\n", + "Requested file is not found\n", + "This is the error sent by the server when hostname cannot be resolved\n", + "Input paramters passed to TFTP interfaces are invalid\n", + "Error detected in TFTP packet or the error received from the TFTP server\n", + "Error during packet synhronization while sending or unexpected packet is received\n", + "File size limit crossed, Max block can be 0xFFFF, each block containing 512 bytes\n", + "File name lenght greater than 256\n", + "Hostname IP is not valid\n", + "TFTP server returned file access error\n", + "TFTP server returned error signifying that the DISK is full to write\n", + "TFTP server returned error signifying that the file exist\n", + "The source file name do not exisits\n", + "Memory allocaion failed in TFTP client\n", + "File open failed\n", + "File read error\n", + "File create error\n", + "File write error\n", + "Max time expired while waiting for file to be recived\n", + "Error when the received packet is less than 4bytes(error lenght) or greater than 512bytes\n", + "Returned by TFTP server for protocol user error\n", + "The destination file path length greater than 256\n", + "Returned by TFTP server for undefined transfer ID\n", + "IOCTL fucntion failed at TFTP client while setting the socket to non-block\n", +}; + +#ifndef ARRAY_SIZE +#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0])) +#endif + +u32_t osShellTftp(int argc, const char **argv) +{ + u32_t ulRemoteAddr = IPADDR_NONE; + const u16_t usTftpServPort = 69; + u8_t ucTftpGet = 0; + s8_t *szLocalFileName = NULL; + s8_t *szRemoteFileName = NULL; + u32_t ret; + + int i = 1; + if (argc < 1 || argv == NULL) { + goto usage; + } + + if (!tcpip_init_finish) { + PRINTK("%s: tcpip_init have not been called\n", __FUNCTION__); + return LOS_NOK; + } + + while (i < argc) { + if (strcmp(argv[i], "-p") == 0) { + ucTftpGet = 0; + i++; + continue; + } + + if (strcmp(argv[i], "-g") == 0) { + ucTftpGet = 1; + i++; + continue; + } + + if (strcmp(argv[i], "-l") == 0 && ((i + 1) < argc)) { + szLocalFileName = (s8_t *)argv[i + 1]; + i += 2; + continue; + } + + if (strcmp(argv[i], "-r") == 0 && ((i + 1) < argc)) { + szRemoteFileName = (s8_t *)argv[i + 1]; + i += 2; + continue; + } + + if ((i + 1) == argc) { + ulRemoteAddr = inet_addr(argv[i]); + break; + } + + goto usage; + } + + if (ulRemoteAddr == IPADDR_NONE || szLocalFileName == NULL || szRemoteFileName == NULL) { + goto usage; + } + + if (ucTftpGet) { + ret = lwip_tftp_get_file_by_filename(ntohl(ulRemoteAddr), usTftpServPort, + TRANSFER_MODE_BINARY, szRemoteFileName, szLocalFileName); + } else { + ret = lwip_tftp_put_file_by_filename(ntohl(ulRemoteAddr), usTftpServPort, + TRANSFER_MODE_BINARY, szLocalFileName, szRemoteFileName); + } + + LWIP_ASSERT("TFTP UNKNOW ERROR!", ret < ARRAY_SIZE(TftpError)); + PRINTK("%s", TftpError[ret]); + if (ret) { + return LOS_NOK; + } else { + return LOS_OK; + } +usage: + PRINTK("usage:\nTransfer a file from/to tftp server\n"); + PRINTK("tftp <-g/-p> -l FullPathLocalFile -r RemoteFile Host\n"); + return LOS_NOK; +} + +#ifdef LOSCFG_SHELL_CMD_DEBUG +SHELLCMD_ENTRY(tftp_shellcmd, CMD_TYPE_EX, "tftp", XARGS, (CmdCallBackFunc)(uintptr_t)osShellTftp); +#endif /* LOSCFG_SHELL_CMD_DEBUG */ +#endif /* LOSCFG_NET_LWIP_SACK_TFTP */ diff --git a/apps/tftp/src/tftpc.c b/apps/tftp/src/tftpc.c new file mode 100644 index 0000000000000000000000000000000000000000..b78bd45fb7c1ef3d16a8efe89b850feece419f16 --- /dev/null +++ b/apps/tftp/src/tftpc.c @@ -0,0 +1,1720 @@ +/* + * 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. + */ + +/* TFTP Client utility */ + +#include "tftpc.h" + +#if LWIP_TFTP /* don't build if not configured for use in lwipopts.h */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* Function Declarations */ +#ifdef LOSCFG_NET_LWIP_SACK_TFTP +static u32_t lwip_tftp_create_bind_socket(s32_t *piSocketID); + +static s32_t lwip_tftp_make_tftp_packet(u16_t usOpcode, s8_t *szFileName, + u32_t ulMode, TFTPC_PACKET_S *pstPacket); + +static u32_t lwip_tftp_recv_from_server(s32_t iSockNum, + u32_t *pulSize, + TFTPC_PACKET_S *pstRecvBuf, + u32_t *pulIgnorePkt, + struct sockaddr_in *pstServerAddr, + TFTPC_PACKET_S *pstSendBuf); + +static u32_t lwip_tftp_send_to_server(s32_t iSockNum, u32_t ulSize, + TFTPC_PACKET_S *pstSendBuf, + struct sockaddr_in *pstServerAddr); + +static u32_t lwip_tftp_validate_data_pkt(s32_t iSockNum, + u32_t *pulSize, + TFTPC_PACKET_S *pstRecvBuf, + u16_t usCurrBlk, u32_t *pulResendPkt, + struct sockaddr_in *pstServerAddr); + +static u32_t lwip_tftp_inner_put_file(s32_t iSockNum, TFTPC_PACKET_S *pstSendBuf, + u32_t ulSendSize, u16_t usCurrBlk, + struct sockaddr_in *pstServerAddr); + +static void lwip_tftp_send_error(s32_t iSockNum, u32_t ulError, const char *szErrMsg, + struct sockaddr_in *pstServerAddr, TFTPC_PACKET_S *pstSendBuf); + + +/* Create and bind a UDP socket. */ +u32_t lwip_tftp_create_bind_socket(s32_t *piSocketID) +{ + int retval; + struct sockaddr_in stClientAddr; + u32_t ulTempClientIp; + u32_t set_non_block_socket = 1; + + /* create a socket */ + *piSocketID = lwip_socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); + if (*piSocketID == -1) { + LWIP_DEBUGF(TFTP_DEBUG, ("lwip_tftp_create_bind_socket : lwip_socket create socket failed\n")); + return TFTPC_SOCKET_FAILURE; + } + + /* Make the socket as NON-BLOCKING. */ + retval = lwip_ioctl(*piSocketID, (long)FIONBIO, &set_non_block_socket); + if (retval != 0) { + (void)lwip_close(*piSocketID); + *piSocketID = TFTP_NULL_INT32; + return TFTPC_IOCTLSOCKET_FAILURE; + } + + ulTempClientIp = INADDR_ANY; + + /* specify a local address for this socket */ + (void)memset_s(&stClientAddr, sizeof(stClientAddr), 0, sizeof(stClientAddr)); + stClientAddr.sin_family = AF_INET; + stClientAddr.sin_port = 0; + stClientAddr.sin_addr.s_addr = htonl(ulTempClientIp); + + retval = lwip_bind(*piSocketID, (struct sockaddr *)&stClientAddr, sizeof(stClientAddr)); + if (retval != 0) { + (void)lwip_close(*piSocketID); + *piSocketID = TFTP_NULL_INT32; + + return TFTPC_BIND_FAILURE; + } + + return ERR_OK; +} + + +/* Function to create TFTP packet. + usOpcode - indiacting the nature of the operation + pFileName -filename on which the operation needs to done + ulMode -mode in which the operation needs to done + pstPacket - packet generated + Returns packet address on success +*/ +static s32_t lwip_tftp_make_tftp_packet(u16_t usOpcode, s8_t *szFileName, u32_t ulMode, TFTPC_PACKET_S *pstPacket) +{ + s8_t *pcCp = NULL; + + pstPacket->usOpcode = htons(usOpcode); + pcCp = pstPacket->u.ucName_Mode; + + /* Request packet format is: + | Opcode | Filename | 0 | Mode | 0 | + */ + (void)strncpy_s((char *)pcCp, TFTP_MAX_PATH_LENGTH, (char *)szFileName, (TFTP_MAX_PATH_LENGTH - 1)); + pcCp[(TFTP_MAX_PATH_LENGTH - 1)] = '\0'; + + pcCp += (strlen((char *)szFileName) + 1); + if (ulMode == TRANSFER_MODE_BINARY) { + (void)strncpy_s((char *)pcCp, TFTP_MAX_MODE_SIZE, "octet", (TFTP_MAX_MODE_SIZE - 1)); + pcCp[(TFTP_MAX_MODE_SIZE - 1)] = '\0'; + } else if (ulMode == TRANSFER_MODE_ASCII) { + (void)strncpy_s((char *)pcCp, TFTP_MAX_MODE_SIZE, "netascii", (TFTP_MAX_MODE_SIZE - 1)); + pcCp[(TFTP_MAX_MODE_SIZE - 1)] = '\0'; + } + + pcCp += (strlen((char *)pcCp) + 1); + + return (pcCp - (s8_t *)pstPacket); +} + +/* Function to recv a packet from server + iSockNum - Socket Number + pstServerAddr - Server address + pulIgnorePkt - Ignore packet flag + pstRecvBuf - received packet + pulSize - Size of the packet +*/ +u32_t lwip_tftp_recv_from_server(s32_t iSockNum, u32_t *pulSize, TFTPC_PACKET_S *pstRecvBuf, u32_t *pulIgnorePkt, + struct sockaddr_in *pstServerAddr, TFTPC_PACKET_S *pstSendBuf) +{ + u32_t ulError; + socklen_t slFromAddrLen; + struct sockaddr_in stFromAddr; + fd_set stReadfds; + struct timeval stTimeout; + u16_t usOpcode; /* Opcode value */ + s32_t iRet; + + slFromAddrLen = sizeof(stFromAddr); + stTimeout.tv_sec = TFTPC_TIMEOUT_PERIOD; + stTimeout.tv_usec = 0; + + /* wait for DATA packet */ + FD_ZERO(&stReadfds); + FD_SET(iSockNum, &stReadfds); + + iRet = select((s32_t)(iSockNum + 1), &stReadfds, 0, 0, &stTimeout); + if (iRet == -1) { + return TFTPC_SELECT_ERROR; + } else if (iRet == 0) { + return TFTPC_TIMEOUT_ERROR; /* Select timeout occured */ + } + + if (!FD_ISSET(iSockNum, &stReadfds)) { + return TFTPC_TIMEOUT_ERROR; /* FD not set*/ + } + + /* receive a packet from server */ + iRet = lwip_recvfrom(iSockNum, (s8_t *)pstRecvBuf, TFTP_PKTSIZE, 0, + (struct sockaddr *)&stFromAddr, &slFromAddrLen); + if (iRet <= 0) { + return TFTPC_RECVFROM_ERROR; + } + + /* If received packet size < minimum packet size */ + if (iRet < TFTPC_FOUR) { + /* Send Error packet to server */ + lwip_tftp_send_error(iSockNum, + TFTPC_PROTOCOL_PROTO_ERROR, + "Packet size < min size", + pstServerAddr, pstSendBuf); + + return TFTPC_PKT_SIZE_ERROR; + } + + /* convert network opcode to host format after receive. */ + usOpcode = ntohs(pstRecvBuf->usOpcode); + /* if this packet is ERROR packet */ + if (usOpcode == TFTPC_OP_ERROR) { + ulError = ntohs (pstRecvBuf->u.stTFTP_Err.usErrNum); + + /*If the error is according to RFC,then convert to lwip error codes. + Constant values are used in the cases as these error codes are as per + the RFC, these are constant values returned by many standard TFTP + serevrs.*/ + switch (ulError) { + case TFTPC_PROTOCOL_USER_DEFINED: + ulError = TFTPC_ERROR_NOT_DEFINED; + break; + case TFTPC_PROTOCOL_FILE_NOT_FOUND: + ulError = TFTPC_FILE_NOT_FOUND; + break; + case TFTPC_PROTOCOL_ACCESS_ERROR: + ulError = TFTPC_ACCESS_ERROR; + break; + case TFTPC_PROTOCOL_DISK_FULL: + ulError = TFTPC_DISK_FULL; + break; + case TFTPC_PROTOCOL_PROTO_ERROR: + ulError = TFTPC_PROTO_ERROR; + break; + case TFTPC_PROTOCOL_UNKNOWN_TRANSFER_ID: + ulError = TFTPC_UNKNOWN_TRANSFER_ID; + break; + case TFTPC_PROTOCOL_FILE_EXISTS: + ulError = TFTPC_FILE_EXISTS; + break; + case TFTPC_PROTOCOL_CANNOT_RESOLVE_HOSTNAME: + ulError = TFTPC_CANNOT_RESOLVE_HOSTNAME; + break; + default: + ulError = TFTPC_ERROR_NOT_DEFINED; + break; + } + + /* If length of error msg > 100 chars */ + pstRecvBuf->u.stTFTP_Err.ucErrMesg[TFTP_MAXERRSTRSIZE - 1] = '\0'; + + LWIP_DEBUGF(TFTP_DEBUG, ("lwip_tftp_recv_from_server : ERROR pkt received: %s\n", + pstRecvBuf->u.stTFTP_Err.ucErrMesg)); + + /* Now we get error block, so return. */ + return ulError; + } + + /* Store the size of received block */ + *pulSize = (u32_t)iRet; + + /* If received packet is first block of data(for get operation) or if + received packet is acknowledgement for write request (put operation) + store the received port number */ + if (((usOpcode == TFTPC_OP_DATA) && + (ntohs(pstRecvBuf->u.stTFTP_Data.usBlknum) == 1)) || + ((usOpcode == TFTPC_OP_ACK) && + (ntohs(pstRecvBuf->u.usBlknum) == 0))) { + /* If received packet from correct server */ + if (stFromAddr.sin_addr.s_addr == pstServerAddr->sin_addr.s_addr) { + /* set the server port to received port */ + pstServerAddr->sin_port = stFromAddr.sin_port; + } else { + /* Received packet form wrong server. */ + LWIP_DEBUGF(TFTP_DEBUG, + ("lwip_tftp_recv_from_server : Received 1st packet from wrong Server or unknown server\n")); + + /* Set ignore packet flag */ + *pulIgnorePkt = 1; + } + } else { + /* If not first packet, check if the received packet is from correct + server and from correct port */ + if ((stFromAddr.sin_addr.s_addr != pstServerAddr->sin_addr.s_addr) || + (pstServerAddr->sin_port != stFromAddr.sin_port)) { + /* Received packet form wrong server or wrong port.Ignore packet. */ + LWIP_DEBUGF(TFTP_DEBUG, + ("lwip_tftp_recv_from_server : Received a packet from wrong Server or unknown server\n")); + + /* Set ignore packet flag */ + *pulIgnorePkt = 1; + } + } + + return ERR_OK; +} + +/* Function to send a packet to server + iSockNum: Socket Number + ulSize: Size of the packet + pstSendBuf: Packet to send + pstServerAddr: Server address +*/ +u32_t lwip_tftp_send_to_server(s32_t iSockNum, + u32_t ulSize, + TFTPC_PACKET_S *pstSendBuf, + struct sockaddr_in *pstServerAddr) +{ + s32_t iRet; + + /* Send packet to server */ + iRet = lwip_sendto(iSockNum, (s8_t *)pstSendBuf, + (size_t)ulSize, 0, + (struct sockaddr *)pstServerAddr, + sizeof(struct sockaddr_in)); + /* Size of data sent not equal to size of packet */ + if ((iRet == TFTP_NULL_INT32) || ((u32_t)iRet != ulSize)) { + return TFTPC_SENDTO_ERROR; + } + + return ERR_OK; +} + +/* lwip_tftp_validate_data_pkt +* Get the data block from the received packet +* @param Input iSockNum Socket Number +* pulSize: Size of received packet, + pstRecvBuf - received packet + usCurrBlk - Current block number + * @param Output pulResendPkt - Resend packet flag + * @return VOS_OK on success.else error code*/ + +u32_t lwip_tftp_validate_data_pkt(s32_t iSockNum, + u32_t *pulSize, + TFTPC_PACKET_S *pstRecvBuf, + u16_t usCurrBlk, + u32_t *pulResendPkt, + struct sockaddr_in *pstServerAddr) +{ + fd_set stReadfds; + struct timeval stTimeout; + struct sockaddr_in stFromAddr; + socklen_t ulFromAddrLen; + s32_t iRecvLen = (s32_t)*pulSize; + s32_t iError; + u16_t usBlknum; + u32_t ulLoopCnt = 0; + + ulFromAddrLen = sizeof(stFromAddr); + + /* Initialize from address to the server address at first */ + if (memcpy_s((void *)&stFromAddr, sizeof(struct sockaddr_in), (void *)pstServerAddr, sizeof(stFromAddr)) != 0) { + LWIP_DEBUGF(TFTP_DEBUG, ("lwip_tftp_validate_data_pkt : memcpy_s error\n")); + return TFTPC_MEMCPY_FAILURE; + } + + /* Get Block Number */ + usBlknum = ntohs(pstRecvBuf->u.stTFTP_Data.usBlknum); + /* Now data blocks are not in sync. */ + if (usBlknum != usCurrBlk) { + /* Set timeout value */ + stTimeout.tv_sec = 1; + stTimeout.tv_usec = 0; + + /* Reset any stored packets. */ + FD_ZERO(&stReadfds); + FD_SET(iSockNum, &stReadfds); + + iError = select((s32_t)(iSockNum + 1), + &stReadfds, 0, 0, &stTimeout); + + /* Loop to get the last data packet from the receive buffer */ + while ((iError != TFTP_NULL_INT32) && (iError != 0)) { + ulLoopCnt++; + + /* MAX file size in TFTP is 32 MB. + Reason for keeping 75 here , is ((75*512=38400bytes)/1024) = 37MB. So the recv/Send + Loop can receive the complete MAX message from the network. + */ + if (ulLoopCnt > TFTPC_MAX_WAIT_IN_LOOP) { + LWIP_DEBUGF(TFTP_DEBUG, ("lwip_tftp_validate_data_pkt : unexpected packets are received repeatedly\n")); + *pulSize = TFTP_NULL_UINT32; + return TFTPC_PKT_SIZE_ERROR; + } + + FD_ZERO(&stReadfds); + FD_SET(iSockNum, &stReadfds); + + iRecvLen = lwip_recvfrom(iSockNum, + (s8_t *)pstRecvBuf, + TFTP_PKTSIZE, 0, + (struct sockaddr *)&stFromAddr, + &ulFromAddrLen); + if (iRecvLen == -1) { + *pulSize = TFTP_NULL_UINT32; + + /* return from the function, recvfrom operation failed */ + return TFTPC_RECVFROM_ERROR; + } + + stTimeout.tv_sec = 1; + stTimeout.tv_usec = 0; + iError = select((s32_t)(iSockNum + 1), &stReadfds, 0, 0, &stTimeout); + } + + /* If received packet size < minimum packet size */ + if (iRecvLen < TFTPC_FOUR) { + return TFTPC_PKT_SIZE_ERROR; + } + + /* Check if the received packet is from correct server and from + correct port + */ + if ((stFromAddr.sin_addr.s_addr != pstServerAddr->sin_addr.s_addr) || + (pstServerAddr->sin_port != stFromAddr.sin_port)) { + /* resend ack packet */ + *pulResendPkt = 1; + LWIP_DEBUGF(TFTP_DEBUG, ("lwip_tftp_validate_data_pkt : Received pkt from unknown server\n")); + + return ERR_OK; + } + + /* if this packet is not DATA packet */ + if (TFTPC_OP_DATA != ntohs(pstRecvBuf->usOpcode)) { + LWIP_DEBUGF(TFTP_DEBUG, ("lwip_tftp_validate_data_pkt : Received pkt not a DATA pkt\n")); + + /* return from the function, incorrect packet received, + expected packet is data packet */ + return TFTPC_PROTO_ERROR; + } + + usBlknum = ntohs(pstRecvBuf->u.stTFTP_Data.usBlknum); + /* if we now have the earlier data packet, then the host probably + never got our acknowledge packet, now we will send it again. */ + if (usBlknum == (usCurrBlk - 1)) { + /* resend ack packet */ + *pulResendPkt = 1; + LWIP_DEBUGF(TFTP_DEBUG, ("lwip_tftp_validate_data_pkt : Received previous DATA pkt\n")); + + return ERR_OK; + } + + /* If the block of data received is not current block or also + previous block, then it is abnormal case. */ + if (usBlknum != usCurrBlk) { + LWIP_DEBUGF(TFTP_DEBUG, + ("lwip_tftp_validate_data_pkt : Received DATA pkt no. %"S32_F" instead of pkt no.%"S32_F"\n", + usBlknum, usCurrBlk)); + + return TFTPC_SYNC_FAILURE; + } + } + + *pulSize = (u32_t)iRecvLen; + return ERR_OK; +} + +/* Send an error packet to the server + iSockNum : Socket Number + ulError: Error code + szErrMsg - Error message + pstServerAddr - Server address +*/ +static void lwip_tftp_send_error(s32_t iSockNum, u32_t ulError, const char *szErrMsg, + struct sockaddr_in *pstServerAddr, TFTPC_PACKET_S *pstSendBuf) +{ + u16_t usOpCode = TFTPC_OP_ERROR; + + if (memset_s((void *)pstSendBuf, sizeof(TFTPC_PACKET_S), 0, sizeof(TFTPC_PACKET_S)) != 0) { + LWIP_DEBUGF(TFTP_DEBUG, ("lwip_tftp_send_error : memset_s error\n")); + return; + } + + /* Set up the send buffer */ + pstSendBuf->usOpcode = htons(usOpCode); + pstSendBuf->u.stTFTP_Err.usErrNum = htons((u16_t)ulError); + + if (strncpy_s((char *)(pstSendBuf->u.stTFTP_Err.ucErrMesg), TFTP_MAXERRSTRSIZE, + (char *)szErrMsg, (TFTP_MAXERRSTRSIZE - 1)) != EOK) { + LWIP_DEBUGF(TFTP_DEBUG, ("lwip_tftp_send_error : strncpy_s error\n")); + return; + } + pstSendBuf->u.stTFTP_Err.ucErrMesg[(TFTP_MAXERRSTRSIZE - 1)] = '\0'; + + /* Send to server */ + if (lwip_tftp_send_to_server(iSockNum, + sizeof(TFTPC_PACKET_S), + pstSendBuf, + pstServerAddr) != ERR_OK) { + LWIP_DEBUGF(TFTP_DEBUG, ("lwip_tftp_send_to_server error.")); + return; + } +} + +/* INTEFACE to get a file using filename + ulHostAddr - IP address of Host + szSrcFileName - Source file + szDestDirPath - Destination file path +*/ +u32_t lwip_tftp_get_file_by_filename(u32_t ulHostAddr, + u16_t usTftpServPort, + u8_t ucTftpTransMode, + s8_t *szSrcFileName, + s8_t *szDestDirPath) +{ + s32_t iSockNum = TFTP_NULL_INT32; + u32_t ulSrcStrLen; + u32_t ulDestStrLen; + u32_t ulSize; + u32_t ulRecvSize = TFTP_NULL_UINT32; + s32_t iErrCode; + u32_t ulErrCode; + u16_t usReadReq; + u16_t usTempServPort; + s8_t *pszTempDestName = NULL; + s8_t *szTempSrcName = NULL; + u32_t ulCurrBlk = 1; + u32_t ulResendPkt = 0; /*Resend the previous packet*/ + u32_t ulIgnorePkt = 0; /*Ignore received packet*/ + u32_t ulTotalTime = 0; + u32_t isLocalFileOpened = false; + + TFTPC_PACKET_S *pstSendBuf = NULL; + TFTPC_PACKET_S *pstRecvBuf = NULL; + struct sockaddr_in stServerAddr; + struct stat sb; + u32_t IsDirExist = 0; + s32_t fp = -1; + + /*Validate the parameters*/ + if ((szSrcFileName == NULL) || (szDestDirPath == NULL)) { + return TFTPC_INVALID_PARAVALUE; + } + + if ((ucTftpTransMode != TRANSFER_MODE_BINARY) && (ucTftpTransMode != TRANSFER_MODE_ASCII)) { + return TFTPC_INVALID_PARAVALUE; + } + + /*check IP address not within ( 1.0.0.0 - 126.255.255.255 ) + and ( 128.0.0.0 - 223.255.255.255 ) range.*/ + if (!(((ulHostAddr >= TFTPC_IP_ADDR_MIN) && + (ulHostAddr <= TFTPC_IP_ADDR_EX_RESV)) || + ((ulHostAddr >= TFTPC_IP_ADDR_CLASS_B) && + (ulHostAddr <= TFTPC_IP_ADDR_EX_CLASS_DE)))) { + return TFTPC_IP_NOT_WITHIN_RANGE; + } + + /*Check validity of source filename*/ + ulSrcStrLen = strlen((char *)szSrcFileName); + if ((ulSrcStrLen == 0) || (ulSrcStrLen >= TFTP_MAX_PATH_LENGTH)) { + return TFTPC_SRC_FILENAME_LENGTH_ERROR; + } + + /*Check validity of destination path*/ + ulDestStrLen = strlen((char *)szDestDirPath); + if ((ulDestStrLen >= TFTP_MAX_PATH_LENGTH) || (ulDestStrLen == 0)) { + return TFTPC_DEST_PATH_LENGTH_ERROR; + } + + pstSendBuf = (TFTPC_PACKET_S *)mem_malloc(sizeof(TFTPC_PACKET_S)); + if (pstSendBuf == NULL) { + return TFTPC_MEMALLOC_ERROR; + } + + pstRecvBuf = (TFTPC_PACKET_S *)mem_malloc(sizeof(TFTPC_PACKET_S)); + if (pstRecvBuf == NULL) { + mem_free(pstSendBuf); + return TFTPC_MEMALLOC_ERROR; + } + + pszTempDestName = (s8_t *)mem_malloc(TFTP_MAX_PATH_LENGTH); + if (pszTempDestName == NULL) { + mem_free(pstSendBuf); + mem_free(pstRecvBuf); + return TFTPC_MEMALLOC_ERROR; + } + + /* First time initialize the buffers */ + (void)memset_s((void *)pstSendBuf, sizeof(TFTPC_PACKET_S), 0, sizeof(TFTPC_PACKET_S)); + (void)memset_s((void *)pstRecvBuf, sizeof(TFTPC_PACKET_S), 0, sizeof(TFTPC_PACKET_S)); + + /*If given src filename is a relative path extract + the file name from the path*/ + if ((0 != strchr((char *)szSrcFileName, '/')) || (0 != strchr((char *)szSrcFileName, '\\'))) { + /*Move to the end of the src file path*/ + szTempSrcName = szSrcFileName + (ulSrcStrLen - 1); + + while (((*(szTempSrcName - 1) != '/') && + (*(szTempSrcName - 1) != '\\')) && + (szTempSrcName != szSrcFileName)) { + szTempSrcName--; + } + + /*Get length of the extracted src filename*/ + ulSrcStrLen = strlen((char *)szTempSrcName); + } else { + /*If not a relative src path use the given src filename*/ + szTempSrcName = szSrcFileName; + } + + (void)memset_s(pszTempDestName, TFTP_MAX_PATH_LENGTH, 0, TFTP_MAX_PATH_LENGTH); + if (strncpy_s((char *)pszTempDestName, TFTP_MAX_PATH_LENGTH, (char *)szDestDirPath, TFTP_MAX_PATH_LENGTH - 1) != + 0) { + ulErrCode = TFTPC_MEMCPY_FAILURE; + goto err_handler; + } + pszTempDestName[TFTP_MAX_PATH_LENGTH - 1] = '\0'; + + if (stat((char *)pszTempDestName, &sb) == 0 && S_ISDIR(sb.st_mode)) { + IsDirExist = 1; + } + + if (IsDirExist == 1) { + /*The filename is not present concat source filename and try*/ + if ((ulDestStrLen + ulSrcStrLen) >= TFTP_MAX_PATH_LENGTH) { + /*If concatenating src filename exceeds 256 bytes*/ + ulErrCode = TFTPC_DEST_PATH_LENGTH_ERROR; + goto err_handler; + } + + /*Check if / present at end of string*/ + if ((pszTempDestName[ulDestStrLen - 1] != '/') && + (pszTempDestName[ulDestStrLen - 1] != '\\')) { + if ((ulDestStrLen + ulSrcStrLen + 1) >= TFTP_MAX_PATH_LENGTH) { + /*If concatenating src filename exceeds 256 bytes*/ + ulErrCode = TFTPC_DEST_PATH_LENGTH_ERROR; + goto err_handler; + } + + /*If not present concat / to the path*/ + if (strncat_s((char *)pszTempDestName, (TFTP_MAX_PATH_LENGTH - strlen((char *)pszTempDestName)), + "/", TFTP_MAX_PATH_LENGTH - strlen((char *)pszTempDestName) - 1) != 0) { + ulErrCode = TFTPC_ERROR_NOT_DEFINED; + goto err_handler; + } + } + + /*Concatenate src filename to destination path*/ + if (strncat_s((char *)pszTempDestName, (TFTP_MAX_PATH_LENGTH - strlen((char *)pszTempDestName)), + (char *)szTempSrcName, TFTP_MAX_PATH_LENGTH - strlen((char *)pszTempDestName) - 1) != 0) { + ulErrCode = TFTPC_ERROR_NOT_DEFINED; + goto err_handler; + } + } + + ulErrCode = lwip_tftp_create_bind_socket(&iSockNum); + if (ulErrCode != ERR_OK) { + goto err_handler; + } + + if (usTftpServPort == 0) { + usTftpServPort = TFTPC_SERVER_PORT; + } + + usTempServPort = usTftpServPort; + + /* set server IP address */ + (void)memset_s(&stServerAddr, sizeof(stServerAddr), 0, sizeof(stServerAddr)); + stServerAddr.sin_family = AF_INET; + stServerAddr.sin_port = htons(usTempServPort); + stServerAddr.sin_addr.s_addr = htonl(ulHostAddr); + + /* Make a request packet - TFTPC_OP_RRQ */ + ulSize = (u32_t)lwip_tftp_make_tftp_packet(TFTPC_OP_RRQ, szSrcFileName, + (u32_t)ucTftpTransMode, + pstSendBuf); + ulErrCode = lwip_tftp_send_to_server(iSockNum, ulSize, + pstSendBuf, &stServerAddr); + if (ulErrCode != ERR_OK) { + /* send to server failed */ + (void)lwip_close(iSockNum); + goto err_handler; + } + + for (;;) { + if (ulIgnorePkt > 0) { + ulIgnorePkt = 0; + } + + ulErrCode = lwip_tftp_recv_from_server(iSockNum, &ulRecvSize, pstRecvBuf, + &ulIgnorePkt, &stServerAddr, pstSendBuf); + /* If select timeout occured */ + if (ulErrCode == TFTPC_TIMEOUT_ERROR) { + ulTotalTime++; + if (ulTotalTime < TFTPC_MAX_SEND_REQ_ATTEMPTS) { + /* Max attempts not reached. Resend packet */ + ulErrCode = lwip_tftp_send_to_server(iSockNum, ulSize, + pstSendBuf, &stServerAddr); + if (ulErrCode != ERR_OK) { + (void)lwip_close(iSockNum); + if (isLocalFileOpened == true) { + close(fp); + } + goto err_handler; + } + + continue; + } else { + /* return from the function, max attempts limit reached */ + (void)lwip_close(iSockNum); + if (isLocalFileOpened == true) { + close(fp); + } + ulErrCode = TFTPC_TIMEOUT_ERROR; + goto err_handler; + } + } else if (ulErrCode != ERR_OK) { + (void)lwip_close(iSockNum); + if (isLocalFileOpened == true) { + close(fp); + } + goto err_handler; + } + + /* Now we have receive block from different server. */ + if (ulIgnorePkt > 0) { + /*Continue without processing this block. */ + continue; + } + + /* if this packet is unkonwn or incorrect packet */ + if (ntohs (pstRecvBuf->usOpcode) != TFTPC_OP_DATA) { + /* Send error packet to server */ + lwip_tftp_send_error(iSockNum, + TFTPC_PROTOCOL_PROTO_ERROR, + "Protocol error.", + &stServerAddr, pstSendBuf); + + (void)lwip_close(iSockNum); + if (isLocalFileOpened == true) { + close(fp); + } + + LWIP_DEBUGF(TFTP_DEBUG, ("lwip_tftp_get_file_by_filename : Received pkt not DATA pkt\n")); + + ulErrCode = TFTPC_PROTO_ERROR; + goto err_handler; + } + + /* Now the number of tries will be reset. */ + ulTotalTime = 0; + + /* Validate received DATA packet. */ + ulErrCode = lwip_tftp_validate_data_pkt(iSockNum, &ulRecvSize, + pstRecvBuf, (u16_t)ulCurrBlk, + &ulResendPkt, + &stServerAddr); + if (ulErrCode != ERR_OK) { + /* Send Error packet to server */ + if (ulErrCode != TFTPC_RECVFROM_ERROR) { + lwip_tftp_send_error(iSockNum, + TFTPC_PROTOCOL_PROTO_ERROR, + "Received unexpected packet", + &stServerAddr, pstSendBuf); + } + + (void)lwip_close(iSockNum); + if (isLocalFileOpened == true) { + close(fp); + } + + goto err_handler; + } + + /* Received previous data block again. Resend last packet */ + if (ulResendPkt > 0) { + /* Now set ulResendPkt to 0 to send the last packet. */ + ulResendPkt = 0; + ulErrCode = lwip_tftp_send_to_server(iSockNum, ulSize, + pstSendBuf, &stServerAddr); + if (ulErrCode != ERR_OK) { + (void)lwip_close(iSockNum); + if (isLocalFileOpened == true) { + close(fp); + } + + goto err_handler; + } + + /* Continue in loop to send last packet again. */ + continue; + } + + /* Get the size of the data block received */ + ulRecvSize -= TFTP_HDRSIZE; + + /* Check if the size of the received data block > max size */ + if (ulRecvSize > TFTP_BLKSIZE) { + /* Send Error packet to server */ + lwip_tftp_send_error(iSockNum, + TFTPC_PROTOCOL_PROTO_ERROR, + "Packet size > max size", + &stServerAddr, pstSendBuf); + + (void)lwip_close(iSockNum); + if (isLocalFileOpened == true) { + close(fp); + } + + LWIP_DEBUGF(TFTP_DEBUG, ("lwip_tftp_get_file_by_filename : Packet size > max size\n")); + + ulErrCode = TFTPC_PKT_SIZE_ERROR; + goto err_handler; + } + + usReadReq = (u16_t)TFTPC_OP_ACK; + pstSendBuf->usOpcode = htons(usReadReq); + pstSendBuf->u.usBlknum = htons((u16_t)ulCurrBlk); + ulSize = TFTP_HDRSIZE; + + if (isLocalFileOpened == false) { + fp = open((char *)pszTempDestName, (O_WRONLY | O_CREAT | O_TRUNC), DEFFILEMODE); + if (fp == TFTP_NULL_INT32) { + ulErrCode = TFTPC_FILECREATE_ERROR; + (void)lwip_close(iSockNum); + goto err_handler; + } + isLocalFileOpened = true; + } + + if (ulRecvSize != TFTP_BLKSIZE) { + (void)lwip_tftp_send_to_server(iSockNum, ulSize, pstSendBuf, &stServerAddr); + + /* If the received packet has only header and do not have payload, the return failure */ + if (ulRecvSize != 0) { + /* Write the last packet to the file */ + iErrCode = write(fp, (void *)pstRecvBuf->u.stTFTP_Data.ucDataBuf, (size_t)ulRecvSize); + if (ulRecvSize != (u32_t)iErrCode) { + /* Write to file failed. */ + lwip_tftp_send_error(iSockNum, + TFTPC_PROTOCOL_USER_DEFINED, + "Write to file failed", + &stServerAddr, pstSendBuf); + + (void)lwip_close(iSockNum); + close(fp); + + /* return from the function, file write failed */ + ulErrCode = TFTPC_FILEWRITE_ERROR; + goto err_handler; + } + } + + /* Now free allocated resourdes and return, + data block receiving is already completed */ + (void)lwip_close(iSockNum); + close(fp); + ulErrCode = ERR_OK; + goto err_handler; + } + + ulErrCode = lwip_tftp_send_to_server(iSockNum, ulSize, + pstSendBuf, &stServerAddr); + if (ulErrCode != ERR_OK) { + (void)lwip_close(iSockNum); + close(fp); + goto err_handler; + } + + iErrCode = write(fp, (void *)pstRecvBuf->u.stTFTP_Data.ucDataBuf, (size_t)ulRecvSize); + if (ulRecvSize != (u32_t)iErrCode) { + /* Write to file failed. */ + lwip_tftp_send_error(iSockNum, + TFTPC_PROTOCOL_USER_DEFINED, + "Write to file failed", + &stServerAddr, pstSendBuf); + + (void)lwip_close(iSockNum); + close(fp); + + /* return from the function, file write failed */ + ulErrCode = TFTPC_FILEWRITE_ERROR; + goto err_handler; + } + + /* form the ACK packet for the DATA packet received */ + /* Go to the next packet no. */ + ulCurrBlk++; + + /* if the file is too big, exit */ + if (ulCurrBlk > TFTP_MAX_BLK_NUM) { + /* Send error packet to server */ + lwip_tftp_send_error(iSockNum, + TFTPC_PROTOCOL_USER_DEFINED, + "File is too big.", + &stServerAddr, pstSendBuf); + + (void)lwip_close(iSockNum); + close(fp); + LWIP_DEBUGF(TFTP_DEBUG, ("lwip_tftp_get_file_by_filename : Data block number exceeded max value\n")); + + ulErrCode = TFTPC_FILE_TOO_BIG; + goto err_handler; + } + } + +err_handler: + mem_free(pstSendBuf); + mem_free(pstRecvBuf); + mem_free(pszTempDestName); + return ulErrCode; +} + + +/* INTERFACE Function to put a file using filename + ulHostAddr: IP address of Host + szSrcFileName: Source file + szDestDirPath: Destination file path +*/ +u32_t lwip_tftp_put_file_by_filename(u32_t ulHostAddr, u16_t usTftpServPort, u8_t ucTftpTransMode, + s8_t *szSrcFileName, s8_t *szDestDirPath) +{ + u32_t ulSrcStrLen; + u32_t ulDestStrLen; + s32_t iSockNum = TFTP_NULL_INT32; + s32_t iErrCode; + u32_t ulErrCode; + u16_t usTempServPort; + TFTPC_PACKET_S *pstSendBuf = NULL; + u16_t usReadReq; + u32_t ulSize; + s8_t *pucBuffer = 0; + s8_t *szTempDestName = NULL; + + /*Initialize the block number*/ + u16_t usCurrBlk = 0; + struct sockaddr_in stServerAddr; + struct stat buffer; + s32_t fp = -1; + + /* Validate parameters */ + if ((szSrcFileName == NULL) || (szDestDirPath == NULL)) { + return TFTPC_INVALID_PARAVALUE; + } + + if ((ucTftpTransMode != TRANSFER_MODE_BINARY) && (ucTftpTransMode != TRANSFER_MODE_ASCII)) { + return TFTPC_INVALID_PARAVALUE; + } + + /*check IP address not within ( 1.0.0.0 - 126.255.255.255 ) + and ( 128.0.0.0 - 223.255.255.255 ) range.*/ + if (!(((ulHostAddr >= TFTPC_IP_ADDR_MIN) && + (ulHostAddr <= TFTPC_IP_ADDR_EX_RESV)) || + ((ulHostAddr >= TFTPC_IP_ADDR_CLASS_B) && + (ulHostAddr <= TFTPC_IP_ADDR_EX_CLASS_DE)))) { + return TFTPC_IP_NOT_WITHIN_RANGE; + } + + /* If Src filename is empty or exceeded max length */ + ulSrcStrLen = strlen((char *)szSrcFileName); + if ((ulSrcStrLen == 0) || (ulSrcStrLen >= TFTP_MAX_PATH_LENGTH)) { + return TFTPC_SRC_FILENAME_LENGTH_ERROR; + } + + /* Check if source file exists */ + if (stat((char *)szSrcFileName, &buffer) != 0) { + return TFTPC_FILE_NOT_EXIST; + } + + /* Check if the file is too big */ + if (buffer.st_size >= (off_t)(TFTP_MAX_BLK_NUM * TFTP_BLKSIZE)) { + return TFTPC_FILE_TOO_BIG; + } + + /* Check validity of destination path */ + ulDestStrLen = strlen((char *)szDestDirPath); + /* If dest path length exceeded max value */ + if (ulDestStrLen >= TFTP_MAX_PATH_LENGTH) { + return TFTPC_DEST_PATH_LENGTH_ERROR; + } + + pstSendBuf = (TFTPC_PACKET_S *)mem_malloc(sizeof(TFTPC_PACKET_S)); + if (pstSendBuf == NULL) { + return TFTPC_MEMALLOC_ERROR; + } + + /* First time intialize the buffer */ + (void)memset_s((void *)pstSendBuf, sizeof(TFTPC_PACKET_S), 0, sizeof(TFTPC_PACKET_S)); + + /* The destination path can only be one of the following: + 1. Only filename + 2. Relative path WITH filename + 3. Empty string + */ + if (ulDestStrLen != 0) { + /* If not empty string use the Destination path name */ + szTempDestName = szDestDirPath; + } else { + /* If destination directory is empty string use source filename + If given src filename is a relative path extract the file name + from the path */ + if ((strchr((char *)szSrcFileName, '/') != 0) || + (strchr((char *)szSrcFileName, '\\') != 0)) { + /* Move to the end of the src file path */ + szTempDestName = szSrcFileName + (ulSrcStrLen - 1); + + while (((*(szTempDestName - 1) != '/') && (*(szTempDestName - 1) != '\\')) && + (szTempDestName != szSrcFileName)) { + szTempDestName--; + } + } else { + /* If not a relative src path use the given src filename */ + szTempDestName = szSrcFileName; + } + } + + /* Create a socket and bind it to an available port number */ + ulErrCode = lwip_tftp_create_bind_socket(&iSockNum); + if (ulErrCode != EOK) { + /* Create and Bind socket failed */ + goto err_handler; + } + + if (usTftpServPort == 0) { + usTftpServPort = TFTPC_SERVER_PORT; + } + + usTempServPort = usTftpServPort; + + /* set server internet address */ + (void)memset_s(&stServerAddr, sizeof(stServerAddr), 0, sizeof(stServerAddr)); + stServerAddr.sin_family = AF_INET; + stServerAddr.sin_port = htons(usTempServPort); + stServerAddr.sin_addr.s_addr = htonl(ulHostAddr); + + /* Make request packet - TFTPC_OP_WRQ */ + ulSize = (u32_t)lwip_tftp_make_tftp_packet(TFTPC_OP_WRQ, + szTempDestName, + ucTftpTransMode, + pstSendBuf); + + ulErrCode = lwip_tftp_send_to_server(iSockNum, ulSize, + pstSendBuf, &stServerAddr); + if (ulErrCode != ERR_OK) { + /* Send to server error */ + (void)lwip_close(iSockNum); + + goto err_handler; + } + + /* Send the request packet */ + ulErrCode = lwip_tftp_inner_put_file(iSockNum, pstSendBuf, ulSize, + usCurrBlk, &stServerAddr); + if (ulErrCode != ERR_OK) { + /* Send request packet failed */ + (void)lwip_close(iSockNum); + + LWIP_DEBUGF(TFTP_DEBUG, ("lwip_tftp_put_file_by_filename : Failed to send request packet\n")); + + goto err_handler; + } + + /* Create buffer block size */ + pucBuffer = mem_malloc(TFTP_BLKSIZE); + if (pucBuffer == NULL) { + /* Memory allocation failed */ + lwip_tftp_send_error(iSockNum, + TFTPC_PROTOCOL_USER_DEFINED, + "Memory allocation failed.", + &stServerAddr, pstSendBuf); + + (void)lwip_close(iSockNum); + ulErrCode = TFTPC_MEMALLOC_ERROR; + goto err_handler; + } + + (void)memset_s((void *)pucBuffer, TFTP_BLKSIZE, 0, TFTP_BLKSIZE); + + fp = open((char *)szSrcFileName, O_RDONLY); + if (TFTP_NULL_INT32 == fp) { + /* If file could not be opened send error to server */ + lwip_tftp_send_error(iSockNum, + TFTPC_PROTOCOL_USER_DEFINED, + "File open error.", + &stServerAddr, pstSendBuf); + + (void)lwip_close(iSockNum); + mem_free(pucBuffer); + + ulErrCode = TFTPC_FILEOPEN_ERROR; + goto err_handler; + } + + iErrCode = read(fp, pucBuffer, TFTP_BLKSIZE); + if (iErrCode < 0) { + /* If failed to read from file */ + lwip_tftp_send_error(iSockNum, + TFTPC_PROTOCOL_USER_DEFINED, + "File read error.", + &stServerAddr, pstSendBuf); + + (void)lwip_close(iSockNum); + close(fp); + mem_free(pucBuffer); + + ulErrCode = TFTPC_FILEREAD_ERROR; + goto err_handler; + } + + /* Read from source file and send to server */ + /* To send empty packet to server when file is a 0 byte file */ + do { + if (((u32_t)usCurrBlk + 1) > TFTP_MAX_BLK_NUM) { + lwip_tftp_send_error(iSockNum, + TFTPC_PROTOCOL_USER_DEFINED, + "File is too big.", + &stServerAddr, pstSendBuf); + + (void)lwip_close(iSockNum); + close(fp); + mem_free(pucBuffer); + LWIP_DEBUGF(TFTP_DEBUG, ("lwip_tftp_put_file_by_filename : Data block number exceeded max value\n")); + + ulErrCode = TFTPC_FILE_TOO_BIG; + goto err_handler; + } + + /* Increment block number */ + usCurrBlk++; + + ulSize = (u32_t)iErrCode + TFTP_HDRSIZE; + + /* Form the DATA packet */ + usReadReq = (u16_t)TFTPC_OP_DATA; + pstSendBuf->usOpcode = htons(usReadReq); + pstSendBuf->u.stTFTP_Data.usBlknum = htons(usCurrBlk); + if (memcpy_s((void *)pstSendBuf->u.stTFTP_Data.ucDataBuf, TFTP_BLKSIZE, + (void *)pucBuffer, (u32_t)iErrCode) != EOK) { + (void)lwip_close(iSockNum); + close(fp); + mem_free(pucBuffer); + goto err_handler; + } + + ulErrCode = lwip_tftp_send_to_server(iSockNum, ulSize, + pstSendBuf, &stServerAddr); + if ((ulErrCode != ERR_OK) || (memset_s((void *)pucBuffer, TFTP_BLKSIZE, 0, TFTP_BLKSIZE) != 0)) { + (void)lwip_close(iSockNum); + close(fp); + mem_free(pucBuffer); + goto err_handler; + } + + /* Read a block from the file to buffer */ + iErrCode = read(fp, pucBuffer, TFTP_BLKSIZE); + if (iErrCode < 0) { + /*If failed to read from file*/ + lwip_tftp_send_error(iSockNum, TFTPC_PROTOCOL_USER_DEFINED, "File read error.", + &stServerAddr, pstSendBuf); + + (void)lwip_close(iSockNum); + close(fp); + mem_free(pucBuffer); + ulErrCode = TFTPC_FILEREAD_ERROR; + goto err_handler; + } + + /* Send the request packet */ + ulErrCode = lwip_tftp_inner_put_file(iSockNum, pstSendBuf, ulSize, + usCurrBlk, &stServerAddr); + if (ulErrCode != ERR_OK) { + /* Sending buffer contents failed */ + (void)lwip_close(iSockNum); + close(fp); + mem_free(pucBuffer); + LWIP_DEBUGF(TFTP_DEBUG, ("lwip_tftp_put_file_by_filename : Sending file to server failed\n")); + goto err_handler; + } + } while (ulSize == (TFTP_BLKSIZE + TFTP_HDRSIZE)); + + /* Transfer of data is finished */ + (void)lwip_close(iSockNum); + close(fp); + mem_free(pucBuffer); + + ulErrCode = ERR_OK; +err_handler: + mem_free(pstSendBuf); + return ulErrCode; +} + +/* Put file function + iSockNum: Socket ID + pstSendBuf: Packet to send to server + ulSendSize: Packet length + usCurrBlk: Current block number + pstServerAddr: Server address +*/ +u32_t lwip_tftp_inner_put_file(s32_t iSockNum, + TFTPC_PACKET_S *pstSendBuf, + u32_t ulSendSize, + u16_t usCurrBlk, + struct sockaddr_in *pstServerAddr) +{ + u32_t ulPktSize; + u32_t ulError; + s32_t iError; + int iRecvLen = 0; + socklen_t iFromAddrLen; + u32_t ulTotalTime = 0; + fd_set stReadfds; + struct sockaddr_in stFromAddr; + struct timeval stTimeout; + TFTPC_PACKET_S *pstRecvBuf = NULL; + u32_t ulIgnorePkt = 0; + u16_t usBlknum; + u32_t ulLoopCnt = 0; + + iFromAddrLen = sizeof(stFromAddr); + + pstRecvBuf = (TFTPC_PACKET_S *)mem_malloc(sizeof(TFTPC_PACKET_S)); + if (pstRecvBuf == NULL) { + return TFTPC_MEMALLOC_ERROR; + } + + /* First time intialize the buffer */ + (void)memset_s((void *)pstRecvBuf, sizeof(TFTPC_PACKET_S), 0, sizeof(TFTPC_PACKET_S)); + + /* Initialize from address to the server address at first */ + if (memcpy_s((void *)&stFromAddr, sizeof(struct sockaddr_in), + (void *)pstServerAddr, sizeof(stFromAddr)) != EOK) { + ulError = TFTPC_MEMCPY_FAILURE; + goto err_handler; + } + + for (;;) { + ulError = lwip_tftp_recv_from_server(iSockNum, &ulPktSize, + pstRecvBuf, &ulIgnorePkt, + pstServerAddr, pstSendBuf); + /* If select timeout occured */ + if (ulError == TFTPC_TIMEOUT_ERROR) { + ulTotalTime++; + if (ulTotalTime < TFTPC_MAX_SEND_REQ_ATTEMPTS) { + /*Max attempts not reached. Resend packet*/ + ulError = lwip_tftp_send_to_server(iSockNum, ulSendSize, + pstSendBuf, pstServerAddr); + if (ulError != ERR_OK) { + goto err_handler; + } + + continue; + } else { + /* return from the function, max attempts limit reached */ + ulError = TFTPC_TIMEOUT_ERROR; + goto err_handler; + } + } else if (ulError != ERR_OK) { + /* return from the function, RecvFromServer failed */ + goto err_handler; + } + + /* If Received packet from another server */ + if (ulIgnorePkt > 0) { + /* The packet that is received is to be ignored. + So continue without processing it. */ + ulIgnorePkt = 0; + continue; + } + + /* if this packet is unknown or incorrect packet */ + if (TFTPC_OP_ACK != ntohs(pstRecvBuf->usOpcode)) { + lwip_tftp_send_error(iSockNum, + TFTPC_PROTOCOL_PROTO_ERROR, + "Protocol error.", + pstServerAddr, pstSendBuf); + + LWIP_DEBUGF(TFTP_DEBUG, ("lwip_tftp_inner_put_file : Received pkt not Ack pkt\n")); + + ulError = TFTPC_PROTO_ERROR; + goto err_handler; + } + + ulTotalTime = 0; + + /* if the packet is acknowledge packet */ + usBlknum = ntohs(pstRecvBuf->u.usBlknum); + iRecvLen = (int)ulPktSize; + + /* If not correct block no. */ + if (usBlknum != usCurrBlk) { + /* we are not in sync now */ + /* reset any collected packets. */ + stTimeout.tv_sec = 1; + stTimeout.tv_usec = 0; + + FD_ZERO(&stReadfds); + FD_SET(iSockNum, &stReadfds); + + /* + Need to take care of timeout scenario in Select call. + Since the socket used is blocking, if select timeout occurs, + the following recvfrom will block indefinitely. + */ + iError = select((s32_t)(iSockNum + 1), &stReadfds, 0, 0, &stTimeout); + + /* Loop to get the last data packet from the receive buffer */ + while ((iError != -1) && (iError != 0)) { + ulLoopCnt++; + + /* MAX file size in TFTP is 32 MB. + Reason for keeping 75 here , is ((75*512=38400bytes)/1024) = 37MB. So the recv/Snd + Loop can receive the complete MAX message from the network. + */ + if (ulLoopCnt > TFTPC_MAX_WAIT_IN_LOOP) { + LWIP_DEBUGF(TFTP_DEBUG, + ("lwip_tftp_inner_put_file : unexpected packets are received repeatedly\n")); + ulError = TFTPC_PKT_SIZE_ERROR; + goto err_handler; + } + + FD_ZERO(&stReadfds); + FD_SET(iSockNum, &stReadfds); + iRecvLen = lwip_recvfrom(iSockNum, + (s8_t *)pstRecvBuf, + TFTP_PKTSIZE, 0, + (struct sockaddr *)&stFromAddr, + &iFromAddrLen); + if (TFTP_NULL_INT32 == iRecvLen) { + ulError = TFTPC_RECVFROM_ERROR; + goto err_handler; + } + + stTimeout.tv_sec = 1; + stTimeout.tv_usec = 0; + iError = select((s32_t)(iSockNum + 1), + &stReadfds, 0, 0, &stTimeout); + } + + /* If a new packet is not received then donot change the byte order + * as it has already been done + */ + /* If received packet size < minimum packet size */ + if (iRecvLen < TFTPC_FOUR) { + lwip_tftp_send_error(iSockNum, + TFTPC_PROTOCOL_PROTO_ERROR, + "Packet size < min packet size", + pstServerAddr, pstSendBuf); + + LWIP_DEBUGF(TFTP_DEBUG, ("lwip_tftp_inner_put_file : Received pkt not Ack pkt\n")); + + ulError = TFTPC_PKT_SIZE_ERROR; + goto err_handler; + } + + /* Check if the received packet is from correct server and from + correct port + */ + if ((stFromAddr.sin_addr.s_addr != pstServerAddr->sin_addr.s_addr) || + (pstServerAddr->sin_port != stFromAddr.sin_port)) { + /* This ACK packet is invalid. Just ignore it. */ + LWIP_DEBUGF(TFTP_DEBUG, ("lwip_tftp_inner_put_file : Received pkt from unknown server\n")); + continue; + } + + /* if this packet is not ACK packet */ + if (TFTPC_OP_ACK != ntohs(pstRecvBuf->usOpcode)) { + lwip_tftp_send_error(iSockNum, + TFTPC_PROTOCOL_PROTO_ERROR, + "Protocol error.", + pstServerAddr, pstSendBuf); + + LWIP_DEBUGF(TFTP_DEBUG, ("lwip_tftp_inner_put_file : Received pkt not Ack pkt\n")); + + ulError = TFTPC_PROTO_ERROR; + goto err_handler; + } + + usBlknum = ntohs(pstRecvBuf->u.usBlknum); + /* + * In this case we have received a duplicate ACK for data block. + * (ACK for this data block was aready received earlier) + * In this case we have usRecvBlkNum == (usNextBlkNum - 1). + * This could mean that: + * (i) last data packet that was sent was not received at server side + * (ii) Acknowledgement of peer side is delayed. + * + * In this case, this duplicate ACK will be ignored and return to the + * state machine to initiate a receive of this data packet. + */ + if ((usCurrBlk - 1) == usBlknum) { + /* This ACK packet is invalid. Just ignore it. */ + continue; + } + + /* Now check the block number with current block. + * If it is not the previous block and the current block, + * then it is an unexpected packet. + */ + if (usBlknum != usCurrBlk) { + lwip_tftp_send_error(iSockNum, + TFTPC_PROTOCOL_PROTO_ERROR, + "Received unexpected packet", + pstServerAddr, pstSendBuf); + + LWIP_DEBUGF(TFTP_DEBUG, + ("lwip_tftp_inner_put_file : Received DATA pkt no. %"S32_F"instead of pkt no. %"S32_F"\n", + usBlknum, usCurrBlk)); + + ulError = TFTPC_SYNC_FAILURE; + goto err_handler; + } + } + + ulError = ERR_OK; + goto err_handler; + } + +err_handler: + mem_free(pstRecvBuf); + return ulError; +} + +#ifdef TFTP_TO_RAWMEM +/* INTEFACE to get a file using filename + ulHostAddr - IP address of Host + szSrcFileName - Source file + szDestMemAddr - The target memory address in the client + + Example : + ulHostAddr = ntohl(inet_addr ("192.168.1.3")); + lwip_tftp_get_file_by_filename_to_rawmem(ulHostAddr, "/ramfs/vs_server.bin", memaddr, &filelen); +*/ +u32_t lwip_tftp_get_file_by_filename_to_rawmem(u32_t ulHostAddr, + u16_t usTftpServPort, + u8_t ucTftpTransMode, + s8_t *szSrcFileName, + s8_t *szDestMemAddr, + u32_t *ulFileLength) +{ + s32_t iSockNum = TFTP_NULL_INT32; + u32_t ulSrcStrLen; + u32_t lDestStrLen; + u32_t ulSize; + u32_t ulRecvSize = TFTP_NULL_UINT32; + s32_t iErrCode; + u32_t ulErrCode; + u16_t usReadReq; + u16_t usTempServPort; + u32_t ulCurrBlk = 1; + u32_t ulResendPkt = 0; /* Resend the previous packet */ + u32_t ulIgnorePkt = 0; /* Ignore received packet */ + u32_t ulTotalTime = 0; + + TFTPC_PACKET_S *pstSendBuf = NULL; + TFTPC_PACKET_S *pstRecvBuf = NULL; + struct sockaddr_in stServerAddr; + u32_t ulMemOffset = 0; + + /* Validate the parameters */ + if ((szSrcFileName == NULL) || (szDestMemAddr == NULL) || (*ulFileLength == 0)) { + return TFTPC_INVALID_PARAVALUE; + } + + if ((ucTftpTransMode != TRANSFER_MODE_BINARY) && (ucTftpTransMode != TRANSFER_MODE_ASCII)) { + return TFTPC_INVALID_PARAVALUE; + } + + /* check IP address not within ( 1.0.0.0 - 126.255.255.255 ) + and ( 128.0.0.0 - 223.255.255.255 ) range. */ + if (!(((ulHostAddr >= TFTPC_IP_ADDR_MIN) && + (ulHostAddr <= TFTPC_IP_ADDR_EX_RESV)) || + ((ulHostAddr >= TFTPC_IP_ADDR_CLASS_B) && + (ulHostAddr <= TFTPC_IP_ADDR_EX_CLASS_DE)))) { + return TFTPC_IP_NOT_WITHIN_RANGE; + } + + /*Check validity of source filename*/ + ulSrcStrLen = strlen(szSrcFileName); + if ((ulSrcStrLen == 0) || (ulSrcStrLen >= TFTP_MAX_PATH_LENGTH)) { + return TFTPC_SRC_FILENAME_LENGTH_ERROR; + } + + pstSendBuf = (TFTPC_PACKET_S *)mem_malloc(sizeof(TFTPC_PACKET_S)); + if (pstSendBuf == NULL) { + return TFTPC_MEMALLOC_ERROR; + } + + pstRecvBuf = (TFTPC_PACKET_S *)mem_malloc(sizeof(TFTPC_PACKET_S)); + if (pstRecvBuf == NULL) { + mem_free(pstSendBuf); + return TFTPC_MEMALLOC_ERROR; + } + + /* First time initialize the buffers */ + (void)memset_s((void *)pstSendBuf, sizeof(TFTPC_PACKET_S), 0, sizeof(TFTPC_PACKET_S)); + (void)memset_s((void *)pstRecvBuf, sizeof(TFTPC_PACKET_S), 0, sizeof(TFTPC_PACKET_S)); + + ulErrCode = lwip_tftp_create_bind_socket(&iSockNum); + if (ulErrCode != EOK) { + goto err_handler; + } + + if (usTftpServPort == 0) { + usTftpServPort = TFTPC_SERVER_PORT; + } + + usTempServPort = usTftpServPort; + + /* set server IP address */ + (void)memset_s(&stServerAddr, sizeof(stServerAddr), 0, sizeof(stServerAddr)); + stServerAddr.sin_family = AF_INET; + stServerAddr.sin_port = htons(usTempServPort); + stServerAddr.sin_addr.s_addr = htonl(ulHostAddr); + + /* Make a request packet - TFTPC_OP_RRQ */ + ulSize = (u32_t)lwip_tftp_make_tftp_packet(TFTPC_OP_RRQ, szSrcFileName, (u32_t)ucTftpTransMode, pstSendBuf); + ulErrCode = lwip_tftp_send_to_server(iSockNum, ulSize, pstSendBuf, &stServerAddr); + if (ulErrCode != ERR_OK) { + /* send to server failed */ + (void)lwip_close(iSockNum); + goto err_handler; + } + + for (;;) { + if (ulIgnorePkt > 0) { + ulIgnorePkt = 0; + } + + ulErrCode = lwip_tftp_recv_from_server(iSockNum, &ulRecvSize, pstRecvBuf, &ulIgnorePkt, + &stServerAddr, pstSendBuf); + /* If select timeout occured */ + if (ulErrCode == TFTPC_TIMEOUT_ERROR) { + ulTotalTime++; + if (ulTotalTime < TFTPC_MAX_SEND_REQ_ATTEMPTS) { + /* Max attempts not reached. Resend packet */ + ulErrCode = lwip_tftp_send_to_server(iSockNum, ulSize, + pstSendBuf, &stServerAddr); + if (ulErrCode != ERR_OK) { + (void)lwip_close(iSockNum); + goto err_handler; + } + + continue; + } else { + /* return from the function, max attempts limit reached */ + (void)lwip_close(iSockNum); + ulErrCode = TFTPC_TIMEOUT_ERROR; + goto err_handler; + } + } else if (ulErrCode != ERR_OK) { + (void)lwip_close(iSockNum); + goto err_handler; + } + + /* Now we have receive block from different server. */ + if (ulIgnorePkt > 0) { + /*Continue without processing this block. */ + continue; + } + + /* if this packet is unkonwn or incorrect packet */ + if (ntohs (pstRecvBuf->usOpcode) != TFTPC_OP_DATA) { + /* Send error packet to server */ + lwip_tftp_send_error(iSockNum, + TFTPC_PROTOCOL_PROTO_ERROR, + "Protocol error.", + &stServerAddr, pstSendBuf); + + (void)lwip_close(iSockNum); + + LWIP_DEBUGF(TFTP_DEBUG, ("lwip_tftp_get_file_by_filename : Received pkt not DATA pkt\n")); + + ulErrCode = TFTPC_PROTO_ERROR; + goto err_handler; + } + + /* Now the number of tries will be reset. */ + ulTotalTime = 0; + + /* Validate received DATA packet. */ + ulErrCode = lwip_tftp_validate_data_pkt(iSockNum, &ulRecvSize, + pstRecvBuf, (u16_t)ulCurrBlk, + &ulResendPkt, + &stServerAddr); + if (ulErrCode != ERR_OK) { + /* Send Error packet to server */ + if (ulErrCode != TFTPC_RECVFROM_ERROR) { + lwip_tftp_send_error(iSockNum, + TFTPC_PROTOCOL_PROTO_ERROR, + "Received unexpected packet", + &stServerAddr, pstSendBuf); + } + + (void)lwip_close(iSockNum); + + goto err_handler; + } + + /* Received previous data block again. Resend last packet */ + if (ulResendPkt > 0) { + /* Now set ulResendPkt to 0 to send the last packet. */ + ulResendPkt = 0; + ulErrCode = lwip_tftp_send_to_server(iSockNum, ulSize, + pstSendBuf, &stServerAddr); + if (ulErrCode != ERR_OK) { + (void)lwip_close(iSockNum); + + goto err_handler; + } + + /* Continue in loop to send last packet again. */ + continue; + } + + /* Get the size of the data block received */ + ulRecvSize -= TFTP_HDRSIZE; + + /* Check if the size of the received data block > max size */ + if (ulRecvSize > TFTP_BLKSIZE) { + /* Send Error packet to server */ + lwip_tftp_send_error(iSockNum, + TFTPC_PROTOCOL_PROTO_ERROR, + "Packet size > max size", + &stServerAddr, pstSendBuf); + + (void)lwip_close(iSockNum); + + LWIP_DEBUGF(TFTP_DEBUG, ("lwip_tftp_get_file_by_filename : Packet size > max size\n")); + + ulErrCode = TFTPC_PKT_SIZE_ERROR; + goto err_handler; + } + + usReadReq = (u16_t)TFTPC_OP_ACK; + pstSendBuf->usOpcode = htons(usReadReq); + pstSendBuf->u.usBlknum = htons((u16_t)ulCurrBlk); + ulSize = TFTP_HDRSIZE; + + if (ulRecvSize != TFTP_BLKSIZE) { + (void)lwip_tftp_send_to_server(iSockNum, ulSize, + pstSendBuf, &stServerAddr); + + /* If the received packet has only header and do not have payload, the return failure */ + if (ulRecvSize != 0) { + /* memcopy filed */ + if (*ulFileLength < (ulMemOffset + ulRecvSize)) { + ulErrCode = TFTPC_MEMCPY_FAILURE; + (void)lwip_close(iSockNum); + *ulFileLength = ulMemOffset; + goto err_handler; + } + /* copy the last packet to the memory */ + if (memcpy_s(szDestMemAddr + ulMemOffset, TFTP_MAX_BLK_NUM * TFTP_BLKSIZE, + (void *)pstRecvBuf->u.stTFTP_Data.ucDataBuf, (size_t)ulRecvSize) != EOK) { + ulErrCode = TFTPC_MEMCPY_FAILURE; + (void)lwip_close(iSockNum); + *ulFileLength = ulMemOffset; + goto err_handler; + } + ulMemOffset += ulRecvSize; + } + + /* Now free allocated resourdes and return, + data block receiving is already completed */ + (void)lwip_close(iSockNum); + ulErrCode = ERR_OK; + *ulFileLength = ulMemOffset; + goto err_handler; + } + + ulErrCode = lwip_tftp_send_to_server(iSockNum, ulSize, + pstSendBuf, &stServerAddr); + if (ulErrCode != ERR_OK) { + (void)lwip_close(iSockNum); + goto err_handler; + } + + /* memcopy filed */ + if (*ulFileLength < ulRecvSize * ulCurrBlk) { + ulErrCode = TFTPC_MEMCPY_FAILURE; + (void)lwip_close(iSockNum); + *ulFileLength = ulMemOffset; + goto err_handler; + } + if (memcpy_s(szDestMemAddr + ulMemOffset, TFTP_MAX_BLK_NUM * TFTP_BLKSIZE, + (void *)pstRecvBuf->u.stTFTP_Data.ucDataBuf, (size_t)ulRecvSize) != EOK) { + ulErrCode = TFTPC_MEMCPY_FAILURE; + (void)lwip_close(iSockNum); + *ulFileLength = ulMemOffset; + goto err_handler; + } + + ulMemOffset += ulRecvSize; + /* form the ACK packet for the DATA packet received */ + /* Go to the next packet no. */ + ulCurrBlk++; + /* if the file is too big, exit */ + if (ulCurrBlk > TFTP_MAX_BLK_NUM) { + /* Send error packet to server */ + lwip_tftp_send_error(iSockNum, + TFTPC_PROTOCOL_USER_DEFINED, + "File is too big.", + &stServerAddr, pstSendBuf); + + (void)lwip_close(iSockNum); + + LWIP_DEBUGF(TFTP_DEBUG, ("lwip_tftp_get_file_by_filename : Data block number exceeded max value\n")); + + ulErrCode = TFTPC_FILE_TOO_BIG; + goto err_handler; + } + } + +err_handler: + mem_free(pstSendBuf); + mem_free(pstRecvBuf); + return ulErrCode; +} +#endif + +#endif /* LOSCFG_NET_LWIP_SACK_TFTP */ +#endif /* LWIP_TFTP */ diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig old mode 100755 new mode 100644 diff --git a/arch/arm/arm.mk b/arch/arm/arm.mk old mode 100755 new mode 100644 index 923518c91e643b282871a9dde97bd1f5c66e255e..6e765e9e973eb09ea9c64407a0050d1a79a43083 --- a/arch/arm/arm.mk +++ b/arch/arm/arm.mk @@ -1,5 +1,5 @@ -# Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. -# Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. +# 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: diff --git a/arch/arm/arm/Makefile b/arch/arm/arm/Makefile index 24d47056b11cad0e784abdec6555fa99029695ad..d105536e72600dacc78a6e741e48f75cd5867d50 100755 --- a/arch/arm/arm/Makefile +++ b/arch/arm/arm/Makefile @@ -1,5 +1,5 @@ -# Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. -# Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. +# 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: @@ -35,6 +35,7 @@ LOCAL_SRCS := $(wildcard src/*.c) $(wildcard src/*.S) LOCAL_INCLUDE := \ -I $(LITEOSTOPDIR)/kernel/base/include \ -I $(LITEOSTOPDIR)/kernel/extended/include \ + -I $(LITEOSTOPDIR)/kernel/extended/dynload/include \ -I $(LITEOSTOPDIR)/arch/arm/arm/include \ ifeq ($(LITEOS_ARM_ARCH), -march=armv7-a) diff --git a/arch/arm/arm/include/arch_config.h b/arch/arm/arm/include/arch_config.h index 09de16a48e0ff13eca9a39c2a318c82a66e051bc..53f09d0245303b432b7cc98842dd25da92b9cbdd 100755 --- a/arch/arm/arm/include/arch_config.h +++ b/arch/arm/arm/include/arch_config.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/arch/arm/arm/include/arm.h b/arch/arm/arm/include/arm.h index bde3879349678e556111eba4b187ca43ebff9b11..c8a10adef555a4f71ce3cc3e6b48cc7f2578164f 100755 --- a/arch/arm/arm/include/arm.h +++ b/arch/arm/arm/include/arm.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/arch/arm/arm/include/arm_user_copy.h b/arch/arm/arm/include/arm_user_copy.h index ac82b958744d3e9473d23804f7f69621349f6978..b86e37c47229c00f11db66ee19149cf92e3b03c6 100755 --- a/arch/arm/arm/include/arm_user_copy.h +++ b/arch/arm/arm/include/arm_user_copy.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/arch/arm/arm/include/arm_user_get.h b/arch/arm/arm/include/arm_user_get.h index 53532107dd3f6a9a9942ac95a03d67096a570d06..43843e02804943f887ad441dca0a06cf5685d6c7 100755 --- a/arch/arm/arm/include/arm_user_get.h +++ b/arch/arm/arm/include/arm_user_get.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/arch/arm/arm/include/arm_user_put.h b/arch/arm/arm/include/arm_user_put.h index cacae1b26b4551516a194f23d5d81f29830827a8..c5c475d35980fefa33b3fc1cee07ee2938bc9d4f 100755 --- a/arch/arm/arm/include/arm_user_put.h +++ b/arch/arm/arm/include/arm_user_put.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/arch/arm/arm/include/los_arch_mmu.h b/arch/arm/arm/include/los_arch_mmu.h old mode 100755 new mode 100644 index c501b219702ddaafffba9ca5dd94a140b7a86ed2..93b2fd34495fbd9fbe00134f3467aa07d5ce65d0 --- a/arch/arm/arm/include/los_arch_mmu.h +++ b/arch/arm/arm/include/los_arch_mmu.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/arch/arm/arm/include/los_asid.h b/arch/arm/arm/include/los_asid.h old mode 100755 new mode 100644 index eb343611a9dab70ec4f3a65f116dfc63d670259e..613982db08c127ff03a4af9dd293ef618a485466 --- a/arch/arm/arm/include/los_asid.h +++ b/arch/arm/arm/include/los_asid.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/arch/arm/arm/include/los_atomic.h b/arch/arm/arm/include/los_atomic.h old mode 100755 new mode 100644 index d007b66e7a60d3e37f031dab423121c486bae811..702ea78ef5165db43dee44b947ad4132c3f4f3d1 --- a/arch/arm/arm/include/los_atomic.h +++ b/arch/arm/arm/include/los_atomic.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/arch/arm/arm/include/los_hw_cpu.h b/arch/arm/arm/include/los_hw_cpu.h index 5585eebc7fe2118c7db0eb2a28cffd07b61f0f2c..45ecb23ee70463bda078db92f4a3b258a9bfc87b 100755 --- a/arch/arm/arm/include/los_hw_cpu.h +++ b/arch/arm/arm/include/los_hw_cpu.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/arch/arm/arm/include/los_mmu_descriptor_v6.h b/arch/arm/arm/include/los_mmu_descriptor_v6.h index 1eb04229ea3bf412033e8acf19e741979920ab9b..6f93de03a91948aee39afc414b94b0d99450d7d2 100755 --- a/arch/arm/arm/include/los_mmu_descriptor_v6.h +++ b/arch/arm/arm/include/los_mmu_descriptor_v6.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: @@ -104,6 +104,8 @@ extern "C" { ((x) << MMU_DESCRIPTOR_L1_TEX_SHIFT) /* type extension */ #define MMU_DESCRIPTOR_L1_TYPE_STRONGLY_ORDERED \ (MMU_DESCRIPTOR_L1_TEX(MMU_DESCRIPTOR_TEX_0) | MMU_DESCRIPTOR_NON_CACHEABLE) +#define MMU_DESCRIPTOR_L1_TYPE_NORMAL_NOCACHE \ + (MMU_DESCRIPTOR_L1_TEX(MMU_DESCRIPTOR_TEX_1) | MMU_DESCRIPTOR_NON_CACHEABLE) #define MMU_DESCRIPTOR_L1_TYPE_DEVICE_SHARED \ (MMU_DESCRIPTOR_L1_TEX(MMU_DESCRIPTOR_TEX_0) | MMU_DESCRIPTOR_WRITE_BACK_ALLOCATE) #define MMU_DESCRIPTOR_L1_TYPE_DEVICE_NON_SHARED \ @@ -141,6 +143,8 @@ extern "C" { ((x) << MMU_DESCRIPTOR_L2_TEX_SHIFT) /* type extension */ #define MMU_DESCRIPTOR_L2_TYPE_STRONGLY_ORDERED \ (MMU_DESCRIPTOR_L2_TEX(MMU_DESCRIPTOR_TEX_0) | MMU_DESCRIPTOR_NON_CACHEABLE) +#define MMU_DESCRIPTOR_L2_TYPE_NORMAL_NOCACHE \ + (MMU_DESCRIPTOR_L2_TEX(MMU_DESCRIPTOR_TEX_1) | MMU_DESCRIPTOR_NON_CACHEABLE) #define MMU_DESCRIPTOR_L2_TYPE_DEVICE_SHARED \ (MMU_DESCRIPTOR_L2_TEX(MMU_DESCRIPTOR_TEX_0) | MMU_DESCRIPTOR_WRITE_BACK_ALLOCATE) #define MMU_DESCRIPTOR_L2_TYPE_DEVICE_NON_SHARED \ @@ -158,7 +162,7 @@ extern "C" { #define MMU_DESCRIPTOR_L2_AP01_0 (MMU_DESCRIPTOR_L2_AP01(0)) #define MMU_DESCRIPTOR_L2_AP01_1 (MMU_DESCRIPTOR_L2_AP01(1)) #define MMU_DESCRIPTOR_L2_AP01_3 (MMU_DESCRIPTOR_L2_AP01(3)) -#define MMU_DESCRIPTOR_L2_AP_P_NA_U_NA (MMUDESCRIPTOR_L2_AP2_0 | MMU_DESCRIPTOR_L2_AP01_0) +#define MMU_DESCRIPTOR_L2_AP_P_NA_U_NA (MMU_DESCRIPTOR_L2_AP2_0 | MMU_DESCRIPTOR_L2_AP01_0) #define MMU_DESCRIPTOR_L2_AP_P_RW_U_RW (MMU_DESCRIPTOR_L2_AP2_0 | MMU_DESCRIPTOR_L2_AP01_3) #define MMU_DESCRIPTOR_L2_AP_P_RW_U_NA (MMU_DESCRIPTOR_L2_AP2_0 | MMU_DESCRIPTOR_L2_AP01_1) #define MMU_DESCRIPTOR_L2_AP_P_RO_U_RO (MMU_DESCRIPTOR_L2_AP2_1 | MMU_DESCRIPTOR_L2_AP01_3) @@ -208,6 +212,12 @@ extern "C" { MMU_DESCRIPTOR_L1_SMALL_DOMAIN_CLIENT | \ MMU_DESCRIPTOR_L1_AP_P_RW_U_NA) +#define MMU_INITIAL_MAP_NORMAL_NOCACHE \ + (MMU_DESCRIPTOR_L1_TYPE_SECTION | \ + MMU_DESCRIPTOR_L1_TYPE_NORMAL_NOCACHE | \ + MMU_DESCRIPTOR_L1_SMALL_DOMAIN_CLIENT | \ + MMU_DESCRIPTOR_L1_AP_P_RW_U_NA) + #define MMU_INITIAL_MAP_DEVICE \ (MMU_DESCRIPTOR_L1_TYPE_SECTION | \ MMU_DESCRIPTOR_L1_TYPE_DEVICE_SHARED | \ diff --git a/arch/arm/arm/include/los_pte_ops.h b/arch/arm/arm/include/los_pte_ops.h old mode 100755 new mode 100644 index 5d6c2b74cd87119472f96717e8eb3029f110f083..6d1bee534dacc27ce7e5e9b28320ee02f703100f --- a/arch/arm/arm/include/los_pte_ops.h +++ b/arch/arm/arm/include/los_pte_ops.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/arch/arm/arm/include/los_tlb_v6.h b/arch/arm/arm/include/los_tlb_v6.h old mode 100755 new mode 100644 index 5ccacaf58f1a09a27a316772fe3134efadc33dce..7257babeb20977286716ea201583fcb81a049912 --- a/arch/arm/arm/include/los_tlb_v6.h +++ b/arch/arm/arm/include/los_tlb_v6.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/arch/arm/arm/src/armv7a/cache.S b/arch/arm/arm/src/armv7a/cache.S old mode 100755 new mode 100644 index cdcbee06603bbeed76053828ccaef3cd9ef5e445..0970d4538e66f39281d0d52d2c37bada7c966ca1 --- a/arch/arm/arm/src/armv7a/cache.S +++ b/arch/arm/arm/src/armv7a/cache.S @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/arch/arm/arm/src/hw_user_get.S b/arch/arm/arm/src/hw_user_get.S index 820d48ecefc224ea2b440dc9f46cca1210c061e2..32711f605be53f3e8c49dc1062bf725ea389b012 100755 --- a/arch/arm/arm/src/hw_user_get.S +++ b/arch/arm/arm/src/hw_user_get.S @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/arch/arm/arm/src/hw_user_put.S b/arch/arm/arm/src/hw_user_put.S index 941efe7ed4d0f2b6f04dca9aa6388ad0a30ca75b..d79e509c0c78544b0d4f4525e957c840cfb4c11c 100755 --- a/arch/arm/arm/src/hw_user_put.S +++ b/arch/arm/arm/src/hw_user_put.S @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/arch/arm/arm/src/include/asm.h b/arch/arm/arm/src/include/asm.h index 796e14c0559eefe6cacdb3477a1138e514d817fb..96ea07addb849edcc4b33c5c4e73a2b78819a5dc 100755 --- a/arch/arm/arm/src/include/asm.h +++ b/arch/arm/arm/src/include/asm.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/arch/arm/arm/src/include/los_exc_pri.h b/arch/arm/arm/src/include/los_exc_pri.h old mode 100755 new mode 100644 index 4031217b4c725583fd6ea4df40cf1faf87ee3944..e1e9c4873a37e5fab943fb77ed694fe41642e7b2 --- a/arch/arm/arm/src/include/los_exc_pri.h +++ b/arch/arm/arm/src/include/los_exc_pri.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: @@ -52,7 +52,8 @@ STATIC INLINE VOID OsSetCurrCpuSp(UINTPTR regSp) extern UINT32 OsGetSystemStatus(VOID); extern VOID BackTraceSub(UINTPTR regFP); extern VOID OsExcInit(VOID); -extern VOID OsKillUserProcess(UINTPTR sp); +extern BOOL OsSystemExcIsReset(VOID); + #ifdef __cplusplus #if __cplusplus } diff --git a/arch/arm/arm/src/include/los_hw_pri.h b/arch/arm/arm/src/include/los_hw_pri.h index 5d470b348abc3f0c6a94978d13b43cbcc2e505cd..052da990a59ae488b59246c6592c4a51e6c39eeb 100755 --- a/arch/arm/arm/src/include/los_hw_pri.h +++ b/arch/arm/arm/src/include/los_hw_pri.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/arch/arm/arm/src/include/los_hwi_pri.h b/arch/arm/arm/src/include/los_hwi_pri.h old mode 100755 new mode 100644 index ea1726b629e363ac2eaf7091e73399684d80726b..ad8e589c9c1210c8c1d0f699e7449b84e1f20ab7 --- a/arch/arm/arm/src/include/los_hwi_pri.h +++ b/arch/arm/arm/src/include/los_hwi_pri.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/arch/arm/arm/src/jmp.S b/arch/arm/arm/src/jmp.S old mode 100755 new mode 100644 index 209b4e8a9e7bea72270f4c56b4b24a1f3001f8c6..14dd05a39256be9e5693b7cb6a1942f7acd40f48 --- a/arch/arm/arm/src/jmp.S +++ b/arch/arm/arm/src/jmp.S @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/arch/arm/arm/src/los_arch_mmu.c b/arch/arm/arm/src/los_arch_mmu.c index 7d7cf935c162f659acc4593966b383da29d05bee..3e046193ddd4fc6a493bf48102f7cc8969ad0b8b 100755 --- a/arch/arm/arm/src/los_arch_mmu.c +++ b/arch/arm/arm/src/los_arch_mmu.c @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: @@ -91,11 +91,6 @@ STATIC INT32 OsMapParamCheck(UINT32 flags, VADDR_T vaddr, PADDR_T paddr) } #endif - if (!(flags & VM_MAP_REGION_FLAG_PERM_READ)) { - VM_ERR("miss read flag"); - return LOS_ERRNO_VM_INVALID_ARGS; - } - /* paddr and vaddr must be aligned */ if (!MMU_DESCRIPTOR_IS_L2_SIZE_ALIGNED(vaddr) || !MMU_DESCRIPTOR_IS_L2_SIZE_ALIGNED(paddr)) { return LOS_ERRNO_VM_INVALID_ARGS; @@ -114,6 +109,9 @@ STATIC VOID OsCvtPte2AttsToFlags(PTE_T l1Entry, PTE_T l2Entry, UINT32 *flags) switch (l2Entry & MMU_DESCRIPTOR_L2_TEX_TYPE_MASK) { case MMU_DESCRIPTOR_L2_TYPE_STRONGLY_ORDERED: + *flags |= VM_MAP_REGION_FLAG_STRONGLY_ORDERED; + break; + case MMU_DESCRIPTOR_L2_TYPE_NORMAL_NOCACHE: *flags |= VM_MAP_REGION_FLAG_UNCACHED; break; case MMU_DESCRIPTOR_L2_TYPE_DEVICE_SHARED: @@ -208,10 +206,10 @@ STATIC VOID OsTryUnmapL1PTE(const LosArchMmu *archMmu, vaddr_t vaddr, UINT32 sca } } -/* convert user level mmu flags to L1 descriptors flags */ -STATIC UINT32 OsCvtSecFlagsToAttrs(UINT32 flags) +STATIC UINT32 OsCvtSecCacheFlagsToMMUFlags(UINT32 flags) { - UINT32 mmuFlags = MMU_DESCRIPTOR_L1_SMALL_DOMAIN_CLIENT; + UINT32 mmuFlags = 0; + switch (flags & VM_MAP_REGION_FLAG_CACHE_MASK) { case VM_MAP_REGION_FLAG_CACHED: mmuFlags |= MMU_DESCRIPTOR_L1_TYPE_NORMAL_WRITE_BACK_ALLOCATE; @@ -219,33 +217,63 @@ STATIC UINT32 OsCvtSecFlagsToAttrs(UINT32 flags) mmuFlags |= MMU_DESCRIPTOR_L1_SECTION_SHAREABLE; #endif break; - case VM_MAP_REGION_FLAG_WRITE_COMBINING: - case VM_MAP_REGION_FLAG_UNCACHED: + case VM_MAP_REGION_FLAG_STRONGLY_ORDERED: mmuFlags |= MMU_DESCRIPTOR_L1_TYPE_STRONGLY_ORDERED; break; + case VM_MAP_REGION_FLAG_UNCACHED: + mmuFlags |= MMU_DESCRIPTOR_L1_TYPE_NORMAL_NOCACHE; + break; case VM_MAP_REGION_FLAG_UNCACHED_DEVICE: mmuFlags |= MMU_DESCRIPTOR_L1_TYPE_DEVICE_SHARED; break; default: return LOS_ERRNO_VM_INVALID_ARGS; } + return mmuFlags; +} + +STATIC UINT32 OsCvtSecAccessFlagsToMMUFlags(UINT32 flags) +{ + UINT32 mmuFlags = 0; - switch (flags & (VM_MAP_REGION_FLAG_PERM_USER | VM_MAP_REGION_FLAG_PERM_WRITE)) { + switch (flags & (VM_MAP_REGION_FLAG_PERM_USER | VM_MAP_REGION_FLAG_PERM_READ | VM_MAP_REGION_FLAG_PERM_WRITE)) { case 0: + mmuFlags |= MMU_DESCRIPTOR_L1_AP_P_NA_U_NA; + break; + case VM_MAP_REGION_FLAG_PERM_READ: + case VM_MAP_REGION_FLAG_PERM_USER: mmuFlags |= MMU_DESCRIPTOR_L1_AP_P_RO_U_NA; break; + case VM_MAP_REGION_FLAG_PERM_USER | VM_MAP_REGION_FLAG_PERM_READ: + mmuFlags |= MMU_DESCRIPTOR_L1_AP_P_RO_U_RO; + break; case VM_MAP_REGION_FLAG_PERM_WRITE: + case VM_MAP_REGION_FLAG_PERM_READ | VM_MAP_REGION_FLAG_PERM_WRITE: mmuFlags |= MMU_DESCRIPTOR_L1_AP_P_RW_U_NA; break; - case VM_MAP_REGION_FLAG_PERM_USER: - mmuFlags |= MMU_DESCRIPTOR_L1_AP_P_RO_U_RO; - break; case VM_MAP_REGION_FLAG_PERM_USER | VM_MAP_REGION_FLAG_PERM_WRITE: + case VM_MAP_REGION_FLAG_PERM_USER | VM_MAP_REGION_FLAG_PERM_READ | VM_MAP_REGION_FLAG_PERM_WRITE: mmuFlags |= MMU_DESCRIPTOR_L1_AP_P_RW_U_RW; break; default: break; } + return mmuFlags; +} + +/* convert user level mmu flags to L1 descriptors flags */ +STATIC UINT32 OsCvtSecFlagsToAttrs(UINT32 flags) +{ + UINT32 mmuFlags; + + mmuFlags = OsCvtSecCacheFlagsToMMUFlags(flags); + if (mmuFlags == LOS_ERRNO_VM_INVALID_ARGS) { + return mmuFlags; + } + + mmuFlags |= MMU_DESCRIPTOR_L1_SMALL_DOMAIN_CLIENT; + + mmuFlags |= OsCvtSecAccessFlagsToMMUFlags(flags); if (!(flags & VM_MAP_REGION_FLAG_PERM_EXECUTE)) { mmuFlags |= MMU_DESCRIPTOR_L1_SECTION_XN; @@ -271,6 +299,9 @@ STATIC VOID OsCvtSecAttsToFlags(PTE_T l1Entry, UINT32 *flags) switch (l1Entry & MMU_DESCRIPTOR_L1_TEX_TYPE_MASK) { case MMU_DESCRIPTOR_L1_TYPE_STRONGLY_ORDERED: + *flags |= VM_MAP_REGION_FLAG_STRONGLY_ORDERED; + break; + case MMU_DESCRIPTOR_L1_TYPE_NORMAL_NOCACHE: *flags |= VM_MAP_REGION_FLAG_UNCACHED; break; case MMU_DESCRIPTOR_L1_TYPE_DEVICE_SHARED: @@ -494,8 +525,7 @@ STATIC VOID OsMapL1PTE(LosArchMmu *archMmu, PTE_T *pte1Ptr, vaddr_t vaddr, UINT3 OsSavePte1(OsGetPte1Ptr(archMmu->virtTtb, vaddr), *pte1Ptr); } -/* convert user level mmu flags to L2 descriptors flags */ -STATIC UINT32 OsCvtPte2FlagsToAttrs(uint32_t flags) +STATIC UINT32 OsCvtPte2CacheFlagsToMMUFlags(UINT32 flags) { UINT32 mmuFlags = 0; @@ -506,33 +536,61 @@ STATIC UINT32 OsCvtPte2FlagsToAttrs(uint32_t flags) #endif mmuFlags |= MMU_DESCRIPTOR_L2_TYPE_NORMAL_WRITE_BACK_ALLOCATE; break; - case VM_MAP_REGION_FLAG_WRITE_COMBINING: - case VM_MAP_REGION_FLAG_UNCACHED: + case VM_MAP_REGION_FLAG_STRONGLY_ORDERED: mmuFlags |= MMU_DESCRIPTOR_L2_TYPE_STRONGLY_ORDERED; break; + case VM_MAP_REGION_FLAG_UNCACHED: + mmuFlags |= MMU_DESCRIPTOR_L2_TYPE_NORMAL_NOCACHE; + break; case VM_MAP_REGION_FLAG_UNCACHED_DEVICE: mmuFlags |= MMU_DESCRIPTOR_L2_TYPE_DEVICE_SHARED; break; default: return LOS_ERRNO_VM_INVALID_ARGS; } + return mmuFlags; +} + +STATIC UINT32 OsCvtPte2AccessFlagsToMMUFlags(UINT32 flags) +{ + UINT32 mmuFlags = 0; - switch (flags & (VM_MAP_REGION_FLAG_PERM_USER | VM_MAP_REGION_FLAG_PERM_WRITE)) { + switch (flags & (VM_MAP_REGION_FLAG_PERM_USER | VM_MAP_REGION_FLAG_PERM_READ | VM_MAP_REGION_FLAG_PERM_WRITE)) { case 0: + mmuFlags |= MMU_DESCRIPTOR_L1_AP_P_NA_U_NA; + break; + case VM_MAP_REGION_FLAG_PERM_READ: + case VM_MAP_REGION_FLAG_PERM_USER: mmuFlags |= MMU_DESCRIPTOR_L2_AP_P_RO_U_NA; break; + case VM_MAP_REGION_FLAG_PERM_USER | VM_MAP_REGION_FLAG_PERM_READ: + mmuFlags |= MMU_DESCRIPTOR_L2_AP_P_RO_U_RO; + break; case VM_MAP_REGION_FLAG_PERM_WRITE: + case VM_MAP_REGION_FLAG_PERM_READ | VM_MAP_REGION_FLAG_PERM_WRITE: mmuFlags |= MMU_DESCRIPTOR_L2_AP_P_RW_U_NA; break; - case VM_MAP_REGION_FLAG_PERM_USER: - mmuFlags |= MMU_DESCRIPTOR_L2_AP_P_RO_U_RO; - break; case VM_MAP_REGION_FLAG_PERM_USER | VM_MAP_REGION_FLAG_PERM_WRITE: + case VM_MAP_REGION_FLAG_PERM_USER | VM_MAP_REGION_FLAG_PERM_READ | VM_MAP_REGION_FLAG_PERM_WRITE: mmuFlags |= MMU_DESCRIPTOR_L2_AP_P_RW_U_RW; break; default: break; } + return mmuFlags; +} + +/* convert user level mmu flags to L2 descriptors flags */ +STATIC UINT32 OsCvtPte2FlagsToAttrs(UINT32 flags) +{ + UINT32 mmuFlags; + + mmuFlags = OsCvtPte2CacheFlagsToMMUFlags(flags); + if (mmuFlags == LOS_ERRNO_VM_INVALID_ARGS) { + return mmuFlags; + } + + mmuFlags |= OsCvtPte2AccessFlagsToMMUFlags(flags); if (!(flags & VM_MAP_REGION_FLAG_PERM_EXECUTE)) { mmuFlags |= MMU_DESCRIPTOR_L2_TYPE_SMALL_PAGE_XN; @@ -624,8 +682,7 @@ STATUS_T LOS_ArchMmuChangeProt(LosArchMmu *archMmu, VADDR_T vaddr, size_t count, status = LOS_ArchMmuUnmap(archMmu, vaddr, 1); if (status < 0) { - VM_ERR("invalid args:aspace %p, vaddr %p, count %d", - __FUNCTION__, __LINE__, archMmu, vaddr, count); + VM_ERR("invalid args:aspace %p, vaddr %p, count %d", archMmu, vaddr, count); return LOS_NOK; } @@ -832,7 +889,7 @@ STATIC VOID OsSetKSectionAttr(VOID) kmallocLength >> MMU_DESCRIPTOR_L2_SMALL_SHIFT, VM_MAP_REGION_FLAG_PERM_READ | VM_MAP_REGION_FLAG_PERM_WRITE); if (status != (kmallocLength >> MMU_DESCRIPTOR_L2_SMALL_SHIFT)) { - VM_ERR("unmap failed, status: %d", status); + VM_ERR("mmap failed, status: %d", status); return; } LOS_VmSpaceReserve(kSpace, kmallocLength, bssEndBoundary); diff --git a/arch/arm/arm/src/los_asid.c b/arch/arm/arm/src/los_asid.c old mode 100755 new mode 100644 index 72b4d7097f1f9359a3a3cc43c22d700bc56b052b..b92dae2708b07ac486d72dbd163c1e81015bc851 --- a/arch/arm/arm/src/los_asid.c +++ b/arch/arm/arm/src/los_asid.c @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: @@ -34,8 +34,8 @@ * @ingroup kernel */ -#include "los_typedef.h" #include "los_asid.h" +#include "los_bitmap.h" #include "los_spinlock.h" #include "los_mmu_descriptor_v6.h" diff --git a/arch/arm/arm/src/los_dispatch.S b/arch/arm/arm/src/los_dispatch.S index add17d0ffcda8efe93dfc7769e8effd700cc103b..1f9c24697564c51472c7ba8e09a671918b8a262e 100755 --- a/arch/arm/arm/src/los_dispatch.S +++ b/arch/arm/arm/src/los_dispatch.S @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: @@ -32,20 +32,11 @@ #include "asm.h" #include "arch_config.h" -#ifdef LOSCFG_KERNEL_SMP_LOCKDEP - .extern OsLockDepCheckOut -#endif - .extern g_taskSpin - .extern g_percpu - .global OsStartToRun + .extern OsSchedToUserReleaseLock .global OsTaskSchedule + .global OsTaskContextLoad .global OsIrqHandler - .global ArchSpinUnlock - .global OsSchedToUserSpinUnlock - .equ OS_TASK_STATUS_RUNNING, 0x0010U - .equ OS_PERCPU_STRUCT_SIZE, 0x28U - .equ OS_PERCPU_TASK_LOCK_OFFSET, 0x14U .fpu vfpv4 /* macros to align and unalign the stack on 8 byte boundary for ABI compliance */ @@ -88,26 +79,6 @@ #endif .endm -/* R0: new task */ -OsStartToRun: - - MSR CPSR_c, #(CPSR_INT_DISABLE | CPSR_SVC_MODE) - - LDRH R1, [R0, #4] - ORR R1, #OS_TASK_STATUS_RUNNING - STRH R1, [R0, #4] - - /* R0 is new task, save it on tpidrprw */ - MCR p15, 0, R0, c13, c0, 4 - ISB - - VPUSH {S0} /* fpu */ - VPOP {S0} - VPUSH {D0} - VPOP {D0} - - B OsTaskContextLoad - /* * R0: new task * R1: run task @@ -152,24 +123,11 @@ OsTaskContextLoad: BNE OsKernelTaskLoad #ifdef LOSCFG_KERNEL_SMP -#ifdef LOSCFG_KERNEL_SMP_LOCKDEP /* 8 bytes stack align */ SUB SP, SP, #4 - LDR R0, =g_taskSpin - BL OsLockDepCheckOut + BL OsSchedToUserReleaseLock ADD SP, SP, #4 #endif - /* R0 = &g_taskSpin.rawLock */ - LDR R0, =g_taskSpin - BL ArchSpinUnlock - LDR R2, =g_percpu - MRC P15, 0, R3, C0, C0, 5 - UXTB R3, R3 - MOV R1, #OS_PERCPU_STRUCT_SIZE - MLA R3, R1, R3, R2 - MOV R2, #0 - STR R2, [R3, #OS_PERCPU_TASK_LOCK_OFFSET] -#endif MVN R3, #CPSR_INT_DISABLE AND R4, R4, R3 @@ -245,9 +203,7 @@ OsIrqFromKernel: /* process pending signals */ BL OsTaskProcSignal - /* check if needs to schedule */ - CMP R0, #0 - BLNE OsSchedPreempt + BL OsSchedIrqEndCheckNeedSched MOV R0,SP MOV R1,R7 diff --git a/arch/arm/arm/src/los_exc.c b/arch/arm/arm/src/los_exc.c index 989cf2e32498c569524c2f4f08fa9e9c3d2664a2..de57d93cfc9d6a79c523b016c9879c6b13a3ef27 100755 --- a/arch/arm/arm/src/los_exc.c +++ b/arch/arm/arm/src/los_exc.c @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: @@ -55,6 +55,7 @@ #include "los_vm_phys.h" #include "los_vm_fault.h" #include "los_vm_common.h" +#include "los_load_elf.h" #include "arm.h" #include "los_bitmap.h" #include "los_process_pri.h" @@ -255,6 +256,39 @@ STATIC const CHAR *g_excTypeString[] = { "irq" }; +STATIC VADDR_T OsGetTextRegionBase(LosVmMapRegion *region, LosProcessCB *runProcess) +{ + struct file *curFilep = NULL; + struct file *lastFilep = NULL; + LosVmMapRegion *curRegion = NULL; + LosVmMapRegion *lastRegion = NULL; + + if ((region == NULL) || (runProcess == NULL)) { + return 0; + } + + if (!LOS_IsRegionFileValid(region)) { + return region->range.base; + } + + lastRegion = region; + do { + curRegion = lastRegion; + lastRegion = LOS_RegionFind(runProcess->vmSpace, curRegion->range.base - 1); + if ((lastRegion == NULL) || !LOS_IsRegionFileValid(lastRegion)) { + goto DONE; + } + curFilep = curRegion->unTypeData.rf.file; + lastFilep = lastRegion->unTypeData.rf.file; + } while (!strcmp(curFilep->f_path, lastFilep->f_path)); + +DONE: + if (curRegion->range.base == EXEC_MMAP_BASE) { + return 0; + } + return curRegion->range.base; +} + STATIC VOID OsExcSysInfo(UINT32 excType, const ExcContext *excBufAddr) { LosTaskCB *runTask = OsCurrTaskGet(); @@ -289,7 +323,7 @@ STATIC VOID OsExcSysInfo(UINT32 excType, const ExcContext *excBufAddr) region = LOS_RegionFind(runProcess->vmSpace, (VADDR_T)excBufAddr->PC); if (region != NULL) { PrintExcInfo("in %s ---> 0x%x", OsGetRegionNameOrFilePath(region), - (VADDR_T)excBufAddr->PC - region->range.base); + (VADDR_T)excBufAddr->PC - OsGetTextRegionBase(region, runProcess)); } } @@ -297,7 +331,7 @@ STATIC VOID OsExcSysInfo(UINT32 excType, const ExcContext *excBufAddr) region = LOS_RegionFind(runProcess->vmSpace, (VADDR_T)excBufAddr->ULR); if (region != NULL) { PrintExcInfo("in %s ---> 0x%x", OsGetRegionNameOrFilePath(region), - (VADDR_T)excBufAddr->ULR - region->range.base); + (VADDR_T)excBufAddr->ULR - OsGetTextRegionBase(region, runProcess)); } PrintExcInfo("\nusp = 0x%x", excBufAddr->USP); } else { @@ -307,7 +341,7 @@ STATIC VOID OsExcSysInfo(UINT32 excType, const ExcContext *excBufAddr) excBufAddr->SP); } - PrintExcInfo("fp = 0x%x\n", excBufAddr->R11); + PrintExcInfo("\nfp = 0x%x\n", excBufAddr->R11); } STATIC VOID OsExcRegsInfo(const ExcContext *excBufAddr) @@ -388,7 +422,7 @@ STATIC VOID OsDumpExcVaddrRegion(LosVmSpace *space, LosVmMapRegion *region) mmuFlag = FALSE; } PrintExcInfo(" 0x%08x 0x%08x 0x%08x\n", - startVaddr, LOS_PaddrToKVaddr(startPaddr), pageCount << PAGE_SHIFT); + startVaddr, LOS_PaddrToKVaddr(startPaddr), (UINT32)pageCount << PAGE_SHIFT); pageCount = 0; startPaddr = 0; } @@ -510,6 +544,7 @@ STATIC VOID OsUserExcHandle(ExcContext *excBufAddr) OsProcessExitCodeCoreDumpSet(runProcess); #endif OsProcessExitCodeSignalSet(runProcess, SIGUSR2); + /* kill user exc process */ LOS_Exit(OS_PRO_EXIT_OK); @@ -750,9 +785,9 @@ VOID OsBackTrace(VOID) { UINT32 regFP = Get_Fp(); LosTaskCB *runTask = OsCurrTaskGet(); - PRINTK("OsBackTrace fp = 0x%x\n", regFP); - PRINTK("runTask->taskName = %s\n", runTask->taskName); - PRINTK("runTask->taskID = %u\n", runTask->taskID); + PrintExcInfo("OsBackTrace fp = 0x%x\n", regFP); + PrintExcInfo("runTask->taskName = %s\n", runTask->taskName); + PrintExcInfo("runTask->taskID = %u\n", runTask->taskID); BackTrace(regFP); } @@ -971,6 +1006,35 @@ LITE_OS_SEC_TEXT VOID STATIC OsExcPriorDisposal(ExcContext *excBufAddr) #endif } } + +#if (LOSCFG_KERNEL_SMP == YES) +#ifdef LOSCFG_FS_VFS + /* Wait for the end of the Console task to avoid multicore printing code */ + OsWaitConsoleSendTaskPend(OsCurrTaskGet()->taskID); +#endif +#endif +} + +LITE_OS_SEC_TEXT_INIT STATIC VOID OsPrintExcHead(UINT32 far) +{ +#ifdef LOSCFG_DEBUG_VERSION + LosVmSpace *space = NULL; + VADDR_T vaddr; +#endif + + /* You are not allowed to add any other print information before this exception information */ + if (g_excFromUserMode[ArchCurrCpuid()] == TRUE) { +#ifdef LOSCFG_DEBUG_VERSION + vaddr = ROUNDDOWN(far, PAGE_SIZE); + space = LOS_SpaceGet(vaddr); + if (space != NULL) { + LOS_DumpMemRegion(vaddr); + } +#endif + PrintExcInfo("##################excFrom: User!####################\n"); + } else { + PrintExcInfo("##################excFrom: kernel!###################\n"); + } } /* @@ -987,19 +1051,7 @@ LITE_OS_SEC_TEXT_INIT VOID OsExcHandleEntry(UINT32 excType, ExcContext *excBufAd OsExcPriorDisposal(excBufAddr); -#if (LOSCFG_KERNEL_SMP == YES) -#ifdef LOSCFG_FS_VFS - /* Wait for the end of the Console task to avoid multicore printing code */ - OsWaitConsoleSendTaskPend(OsCurrTaskGet()->taskID); -#endif -#endif - - /* You are not allowed to add any other print information before this exception information */ - if (g_excFromUserMode[ArchCurrCpuid()] == TRUE) { - PrintExcInfo("##################excFrom: User!####################\n"); - } else { - PrintExcInfo("##################excFrom: kernel###################!\n"); - } + OsPrintExcHead(far); #if (LOSCFG_KERNEL_SMP == YES) OsAllCpuStatusOutput(); @@ -1036,6 +1088,15 @@ LITE_OS_SEC_TEXT_INIT VOID OsExcHandleEntry(UINT32 excType, ExcContext *excBufAd #ifdef LOSCFG_EXC_INTERACTION OsExcInteractionTaskKeep(); #endif + +#ifdef LOSCFG_SHELL_CMD_DEBUG + SystemRebootFunc rebootHook = OsGetRebootHook(); + if ((OsSystemExcIsReset() == TRUE) && (rebootHook != NULL)) { + LOS_Mdelay(3000); /* 3000: System dead, delay 3 seconds after system restart */ + rebootHook(); + } +#endif + while (1) {} } @@ -1058,6 +1119,46 @@ VOID __stack_chk_fail(VOID) __builtin_return_address(0)); } +VOID LOS_RecordLR(UINTPTR *LR, UINT32 LRSize, UINT32 recordCount, UINT32 jumpCount) +{ + UINT32 count = 0; + UINT32 index = 0; + UINT32 stackStart, stackEnd; + LosTaskCB *taskCB = NULL; + UINTPTR framePtr, tmpFramePtr, linkReg; + + if (LR == NULL) { + return; + } + /* if LR array is not enough,just record LRSize. */ + if (LRSize < recordCount) { + recordCount = LRSize; + } + + taskCB = OsCurrTaskGet(); + stackStart = taskCB->topOfStack; + stackEnd = stackStart + taskCB->stackSize; + + framePtr = Get_Fp(); + while ((framePtr > stackStart) && (framePtr < stackEnd) && IS_ALIGNED(framePtr, sizeof(CHAR *))) { + tmpFramePtr = framePtr; + linkReg = *(UINTPTR *)framePtr; + if (index >= jumpCount) { + LR[count++] = linkReg; + if (count == recordCount) { + break; + } + } + index++; + framePtr = *(UINTPTR *)(tmpFramePtr - sizeof(UINTPTR)); + } + + /* if linkReg is not enough,clean up the last of the effective LR as the end. */ + if (count < recordCount) { + LR[count] = 0; + } +} + #ifdef __cplusplus #if __cplusplus } diff --git a/arch/arm/arm/src/los_hw.c b/arch/arm/arm/src/los_hw.c index dbc24d2cbf52b87eefbc083a3969a982f963397b..800bf1b99b06430b5df4ee680a2dfa894d0e1e39 100755 --- a/arch/arm/arm/src/los_hw.c +++ b/arch/arm/arm/src/los_hw.c @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/arch/arm/arm/src/los_hw_exc.S b/arch/arm/arm/src/los_hw_exc.S index 25d8639c3b8768fde0031cb1b8baf6c9af5d2c51..5dd00c78c1fbfe6453f0395fca6f9411e56526d4 100755 --- a/arch/arm/arm/src/los_hw_exc.S +++ b/arch/arm/arm/src/los_hw_exc.S @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: @@ -57,7 +57,11 @@ .global _osExceptPrefetchAbortHdl .global _osExceptSwiHdl .global _osExceptUndefInstrHdl +#if defined(LOSCFG_CC_STACKPROTECTOR_ALL) || \ + defined(LOSCFG_CC_STACKPROTECTOR_STRONG) || \ + defined(LOSCFG_CC_STACKPROTECTOR) .global __stack_chk_guard_setup +#endif .fpu vfpv4 @@ -147,15 +151,22 @@ .endm #endif +#if defined(LOSCFG_CC_STACKPROTECTOR_ALL) || \ + defined(LOSCFG_CC_STACKPROTECTOR_STRONG) || \ + defined(LOSCFG_CC_STACKPROTECTOR) @ Description: Stack-Protector Init __stack_chk_guard_setup: PUSH {FP, LR} +#if defined(LOSCFG_PLATFORM_HI3516DV300) || defined(LOSCFG_PLATFORM_HI3518EV300) + /* Randomize __stack_chk_quard using platform-specific function. */ BL OsRandomStackGuard LDR R1, =__stack_chk_guard MOV R3, R0 ORR R2, R3, #0X80000000 STR R2, [R1] +#endif POP {FP, PC} +#endif @ Description: Undefined instruction exception handler _osExceptUndefInstrHdl: diff --git a/arch/arm/arm/src/los_hw_runstop.S b/arch/arm/arm/src/los_hw_runstop.S old mode 100755 new mode 100644 index 032e9af12e99c72e098f33c30f2758ef0f8da845..b8f8cf7455463bd23a8e11b4d5a25f2c6bd694c5 --- a/arch/arm/arm/src/los_hw_runstop.S +++ b/arch/arm/arm/src/los_hw_runstop.S @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/arch/arm/arm/src/los_hw_tick.c b/arch/arm/arm/src/los_hw_tick.c old mode 100755 new mode 100644 index b52f346c29d0a67105eb3f643beff1b71f0484d9..b53bdfa265023fef35844de9e021bf65e000bbf4 --- a/arch/arm/arm/src/los_hw_tick.c +++ b/arch/arm/arm/src/los_hw_tick.c @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/arch/arm/arm/src/los_hwi.c b/arch/arm/arm/src/los_hwi.c old mode 100755 new mode 100644 index 733fc0103402b531c9e3ca5cf83e15d05b68d7fe..5bac58950c85137f560ca9e019956c78d70d4b65 --- a/arch/arm/arm/src/los_hwi.c +++ b/arch/arm/arm/src/los_hwi.c @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: @@ -31,11 +31,11 @@ #include "los_hwi.h" #include "los_memory.h" -#include "los_tickless_pri.h" #include "los_spinlock.h" #ifdef LOSCFG_KERNEL_CPUP #include "los_cpup_pri.h" #endif +#include "los_sched_pri.h" #ifdef __cplusplus #if __cplusplus @@ -68,6 +68,11 @@ CHAR *OsGetHwiFormName(UINT32 index) return g_hwiFormName[index]; } +UINT32 LOS_GetSystemHwiMaximum(VOID) +{ + return OS_HWI_MAX_NUM; +} + typedef VOID (*HWI_PROC_FUNC0)(VOID); typedef VOID (*HWI_PROC_FUNC2)(INT32, VOID *); VOID OsInterrupt(UINT32 intNum) @@ -75,16 +80,16 @@ VOID OsInterrupt(UINT32 intNum) HwiHandleForm *hwiForm = NULL; UINT32 *intCnt = NULL; + /* Must keep the operation at the beginning of the interface */ intCnt = &g_intCount[ArchCurrCpuid()]; *intCnt = *intCnt + 1; + OsSchedIrqStartTime(); + #ifdef LOSCFG_CPUP_INCLUDE_IRQ OsCpupIrqStart(); #endif -#ifdef LOSCFG_KERNEL_TICKLESS - OsTicklessUpdate(intNum); -#endif hwiForm = (&g_hwiForm[intNum]); #ifndef LOSCFG_NO_SHARED_IRQ while (hwiForm->pstNext != NULL) { @@ -107,10 +112,13 @@ VOID OsInterrupt(UINT32 intNum) #endif ++g_hwiFormCnt[intNum]; - *intCnt = *intCnt - 1; #ifdef LOSCFG_CPUP_INCLUDE_IRQ OsCpupIrqEnd(intNum); #endif + OsSchedIrqUpdateUsedTime(); + + /* Must keep the operation at the end of the interface */ + *intCnt = *intCnt - 1; } STATIC HWI_ARG_T OsHwiCpIrqParam(const HwiIrqParam *irqParam) diff --git a/arch/arm/arm/src/startup/reset_vector_mp.S b/arch/arm/arm/src/startup/reset_vector_mp.S index dc08b6dc55f3008e43723a48944771d579322ad1..cec87786964d26a9641127c128fc4568df01c83f 100755 --- a/arch/arm/arm/src/startup/reset_vector_mp.S +++ b/arch/arm/arm/src/startup/reset_vector_mp.S @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: @@ -112,6 +112,20 @@ __exception_handlers: /* Startup code which will get the machine into supervisor mode */ .global reset_vector .type reset_vector,function + +#ifdef LOSCFG_QUICK_START +__quickstart_args_start: + .fill 512,1,0 +__quickstart_args_end: + +.global OsGetArgsAddr +.type OsGetArgsAddr,function + +OsGetArgsAddr: + ldr r0, =__quickstart_args_start + bx lr +#endif + reset_vector: /* clear register TPIDRPRW */ mov r0, #0 @@ -122,6 +136,20 @@ reset_vector: bic r0, #(1<<2 | 1<<0) mcr p15, 0, r0, c1, c0, 0 + /* enable fpu+neon */ +#ifndef LOSCFG_TEE_ENABLE + MRC p15, 0, r0, c1, c1, 2 + ORR r0, r0, #0xC00 + BIC r0, r0, #0xC000 + MCR p15, 0, r0, c1, c1, 2 + + LDR r0, =(0xF << 20) + MCR p15, 0, r0, c1, c0, 2 + ISB +#endif + MOV r3, #0x40000000 + VMSR FPEXC, r3 + /* r11: delta of physical address and virtual address */ adr r11, pa_va_offset ldr r0, [r11] @@ -156,10 +184,13 @@ reloc_img_to_bottom_loop: reloc_img_to_bottom_done: ldr r4, =g_firstPageTable /* r4: physical address of translation table and clear it */ add r4, r4, r11 - bl page_table_clear + mov r0, r4 + mov r1, #0 + mov r2, #MMU_DESCRIPTOR_L1_SMALL_ENTRY_NUMBERS + bl memset PAGE_TABLE_SET SYS_MEM_BASE, KERNEL_VMM_BASE, KERNEL_VMM_SIZE, MMU_DESCRIPTOR_KERNEL_L1_PTE_FLAGS - PAGE_TABLE_SET SYS_MEM_BASE, UNCACHED_VMM_BASE, UNCACHED_VMM_SIZE, MMU_INITIAL_MAP_STRONGLY_ORDERED + PAGE_TABLE_SET SYS_MEM_BASE, UNCACHED_VMM_BASE, UNCACHED_VMM_SIZE, MMU_INITIAL_MAP_NORMAL_NOCACHE PAGE_TABLE_SET PERIPH_PMM_BASE, PERIPH_DEVICE_BASE, PERIPH_DEVICE_SIZE, MMU_INITIAL_MAP_DEVICE PAGE_TABLE_SET PERIPH_PMM_BASE, PERIPH_CACHED_BASE, PERIPH_CACHED_SIZE, MMU_DESCRIPTOR_KERNEL_L1_PTE_FLAGS PAGE_TABLE_SET PERIPH_PMM_BASE, PERIPH_UNCACHED_BASE, PERIPH_UNCACHED_SIZE, MMU_INITIAL_MAP_STRONGLY_ORDERED @@ -169,7 +200,6 @@ reloc_img_to_bottom_done: add r4, r4, r11 ldr r4, [r4] add r4, r4, r11 /* r4: jump pagetable paddr */ - bl page_table_clear /* build 1M section mapping, in order to jump va during turing on mmu:pa == pa, va == pa */ mov r6, pc @@ -233,20 +263,6 @@ warm_reset: sub r0, r0, r2 mov sp, r0 - /* enable fpu+neon */ -#ifndef LOSCFG_TEE_ENABLE - MRC p15, 0, r0, c1, c1, 2 - ORR r0, r0, #0xC00 - BIC r0, r0, #0xC000 - MCR p15, 0, r0, c1, c1, 2 - - LDR r0, =(0xF << 20) - MCR p15, 0, r0, c1, c0, 2 -#endif - - MOV r3, #0x40000000 - VMSR FPEXC, r3 - LDR r0, =__exception_handlers MCR p15, 0, r0, c12, c0, 0 @@ -254,14 +270,11 @@ warm_reset: bne cpu_start clear_bss: - ldr r1, =__bss_start + ldr r0, =__bss_start ldr r2, =__bss_end - mov r0, #0 - -bss_loop: - cmp r1, r2 - strlo r0, [r1], #4 - blo bss_loop + mov r1, #0 + sub r2, r2, r0 + bl memset #if defined(LOSCFG_CC_STACKPROTECTOR_ALL) || \ defined(LOSCFG_CC_STACKPROTECTOR_STRONG) || \ @@ -371,20 +384,6 @@ sp_set: mov sp, r2 bx lr /* set sp */ -/* - * r4: page table base address - * r5 and r6 will be used as variable - */ -page_table_clear: - mov r5, #0 - mov r6, #0 -0: - str r5, [r4, r6, lsl #2] - add r6, #1 - cmp r6, #0x1000 /* r6 < 4096 */ - blt 0b - bx lr - /* * r4: page table base address * r6: physical address diff --git a/arch/arm/arm/src/startup/reset_vector_up.S b/arch/arm/arm/src/startup/reset_vector_up.S index d7de47773eed9519c6fca4563f2a50bcbf9b33e1..63ac57641ba8c2205b91bdab6a535c966ecc9b86 100755 --- a/arch/arm/arm/src/startup/reset_vector_up.S +++ b/arch/arm/arm/src/startup/reset_vector_up.S @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: @@ -53,7 +53,6 @@ .extern __bss_start .extern __bss_end .extern hal_clock_initialize_start - .extern los_bss_init .extern _osExceptFiqHdl .extern _osExceptAddrAbortHdl .extern _osExceptDataAbortHdl @@ -119,6 +118,20 @@ reset_vector: bic r0, #(1<<2 | 1<<0) mcr p15, 0, r0, c1, c0, 0 + /* enable fpu+neon */ +#ifndef LOSCFG_TEE_ENABLE + MRC p15, 0, r0, c1, c1, 2 + ORR r0, r0, #0xC00 + BIC r0, r0, #0xC000 + MCR p15, 0, r0, c1, c1, 2 + + LDR r0, =(0xF << 20) + MCR p15, 0, r0, c1, c0, 2 + ISB +#endif + MOV r3, #0x40000000 + VMSR FPEXC, r3 + /* r11: delta of physical address and virtual address */ adr r11, pa_va_offset ldr r0, [r11] @@ -148,10 +161,14 @@ reloc_img_to_bottom_loop: reloc_img_to_bottom_done: ldr r4, =g_firstPageTable /* r4: physical address of translation table and clear it */ add r4, r4, r11 - bl page_table_clear + mov r0, r4 + mov r1, #0 + mov r2, #MMU_DESCRIPTOR_L1_SMALL_ENTRY_NUMBERS + bl memset + PAGE_TABLE_SET SYS_MEM_BASE, KERNEL_VMM_BASE, KERNEL_VMM_SIZE, MMU_DESCRIPTOR_KERNEL_L1_PTE_FLAGS - PAGE_TABLE_SET SYS_MEM_BASE, UNCACHED_VMM_BASE, UNCACHED_VMM_SIZE, MMU_INITIAL_MAP_STRONGLY_ORDERED + PAGE_TABLE_SET SYS_MEM_BASE, UNCACHED_VMM_BASE, UNCACHED_VMM_SIZE, MMU_INITIAL_MAP_NORMAL_NOCACHE PAGE_TABLE_SET PERIPH_PMM_BASE, PERIPH_DEVICE_BASE, PERIPH_DEVICE_SIZE, MMU_INITIAL_MAP_DEVICE PAGE_TABLE_SET PERIPH_PMM_BASE, PERIPH_CACHED_BASE, PERIPH_CACHED_SIZE, MMU_DESCRIPTOR_KERNEL_L1_PTE_FLAGS PAGE_TABLE_SET PERIPH_PMM_BASE, PERIPH_UNCACHED_BASE, PERIPH_UNCACHED_SIZE, MMU_INITIAL_MAP_STRONGLY_ORDERED @@ -161,7 +178,6 @@ reloc_img_to_bottom_done: add r4, r4, r11 ldr r4, [r4] add r4, r4, r11 /* r4: jump pagetable paddr */ - bl page_table_clear /* build 1M section mapping, in order to jump va during turing on mmu:pa == pa, va == pa */ mov r6, pc @@ -246,14 +262,11 @@ warm_reset: bne cpu_start clear_bss: - ldr r1, =__bss_start + ldr r0, =__bss_start ldr r2, =__bss_end - mov r0, #0 - -bss_loop: - cmp r1, r2 - strlo r0, [r1], #4 - blo bss_loop + mov r1, #0 + sub r2, r2, r0 + bl memset #if defined(LOSCFG_CC_STACKPROTECTOR_ALL) || \ defined(LOSCFG_CC_STACKPROTECTOR_STRONG) || \ @@ -339,20 +352,6 @@ sp_set: mov sp, r2 bx lr /* set sp */ -/* - * r4: page table base address - * r5 and r6 will be used as variable - */ -page_table_clear: - mov r5, #0 - mov r6, #0 -0: - str r5, [r4, r6, lsl #2] - add r6, #1 - cmp r6, #0x1000 /* r6 < 4096 */ - blt 0b - bx lr - /* * r4: page table base address * r6: physical address diff --git a/arch/arm/arm/src/strncpy_from_user.c b/arch/arm/arm/src/strncpy_from_user.c index 0e307f64480f898c28013eb05f41b67ff1d08173..352b9310220038eb17433de9113aecba2b607942 100755 --- a/arch/arm/arm/src/strncpy_from_user.c +++ b/arch/arm/arm/src/strncpy_from_user.c @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/arch/arm/arm/src/strnlen_user.c b/arch/arm/arm/src/strnlen_user.c index b34d090b5176e52679977bbcca53edf70a742f22..4da0bf4906704a2f32b3251e0ed2c17b51d32414 100755 --- a/arch/arm/arm/src/strnlen_user.c +++ b/arch/arm/arm/src/strnlen_user.c @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/arch/arm/arm/src/user_copy.c b/arch/arm/arm/src/user_copy.c index 13e23baadd2b39734da7f03114d2c1b31124ac5e..f1016f00807f302e38bf28493d62c91d9e4e8994 100755 --- a/arch/arm/arm/src/user_copy.c +++ b/arch/arm/arm/src/user_copy.c @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/arch/arm/include/in_cksum.h b/arch/arm/include/in_cksum.h index 8920b1df150dcbac070021c3b101ac3bef804f57..e86c496654aef768b2ac1bb746f279703523a5ed 100755 --- a/arch/arm/include/in_cksum.h +++ b/arch/arm/include/in_cksum.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/arch/arm/include/los_exc.h b/arch/arm/include/los_exc.h index 62cbe6ff70482f22c9a078eef1d7bddda0bb6a18..807bcb2fad1db91571b8a0951118c9cd9e910440 100755 --- a/arch/arm/include/los_exc.h +++ b/arch/arm/include/los_exc.h @@ -1,233 +1,251 @@ -/* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, 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_exc Exception handling - * @ingroup kernel - */ -#ifndef _LOS_EXC_H -#define _LOS_EXC_H - -#include "los_typedef.h" -#include "arch_config.h" - -#ifdef __cplusplus -#if __cplusplus -extern "C" { -#endif /* __cplusplus */ -#endif /* __cplusplus */ - -/** - * @ingroup los_exc - * Register information structure - * - * Description: register information stored when an exception occurs on an LPC2458 platform. - * - * Note: The following register names without uw are the register names used in the chip manual. - */ -#ifdef LOSCFG_ARCH_ARM_AARCH64 -#define EXC_GEN_REGS_NUM 30 -typedef struct { - UINT64 X[EXC_GEN_REGS_NUM]; /**< Register X0-X29 */ - UINT64 LR; /**< Program returning address. X30 */ - UINT64 SP; - UINT64 regELR; - UINT64 SPSR; -} ExcContext; -#else -typedef struct { - UINT32 USP; /**< User mode stack pointer */ - UINT32 ULR; /**< User mode program returning address */ - UINT32 regCPSR; /**< Current program status register (CPSR) */ - UINT32 R0; /**< Register R0 */ - UINT32 R1; /**< Register R1 */ - UINT32 R2; /**< Register R2 */ - UINT32 R3; /**< Register R3 */ - UINT32 R4; /**< Register R4 */ - UINT32 R5; /**< Register R5 */ - UINT32 R6; /**< Register R6 */ - UINT32 R7; /**< Register R7 */ - UINT32 R8; /**< Register R8 */ - UINT32 R9; /**< Register R9 */ - UINT32 R10; /**< Register R10 */ - UINT32 R11; /**< Register R11 */ - UINT32 R12; /**< Register R12 */ - UINT32 SP; /**< Stack pointer */ - UINT32 LR; /**< Program returning address. */ - UINT32 PC; /**< PC pointer of the exceptional function */ -} ExcContext; -#endif - -/** - * @ingroup los_exc - * Exception information structure - * - * Description: exception information stored when an exception occurs on an LPC2458 platform. - * - */ -typedef struct { - UINT16 phase; /**< Phase in which an exception occurs */ - UINT16 type; /**< Exception type */ - UINT16 nestCnt; /**< Count of nested exception */ - UINT16 reserved; /**< Reserved for alignment */ - ExcContext *context; /**< Hardware context when an exception occurs */ -} ExcInfo; - -/** - * @ingroup los_exc - * @brief Kernel FP Register address obtain function. - * - * @par Description: - * The API is used to obtain the FP Register address. - * @attention None. - * - * @param None. - * - * @retval #UINTPTR The FP Register address. - * - * @par Dependency: - * los_exc.h: the header file that contains the API declaration. - * @see None. - */ -STATIC INLINE UINTPTR Get_Fp(VOID) -{ - UINTPTR regFp; - -#ifdef LOSCFG_ARCH_ARM_AARCH64 - __asm__ __volatile__("mov %0, X29" : "=r"(regFp)); -#else - __asm__ __volatile__("mov %0, fp" : "=r"(regFp)); -#endif - - return regFp; -} - -/** - * @ingroup los_exc - * @brief Define an exception handling function hook. - * - * @par Description: - * This API is used to define the exception handling function hook based on the type of - * the exception handling function and record exceptions. - * @attention None. - * - * @param None. - * - * @retval None. - * - * @par Dependency: - * los_exc.h: the header file that contains the API declaration. - * @see None. - */ -typedef VOID (*EXC_PROC_FUNC)(UINT32, ExcContext *, UINT32, UINT32); - -/** - * @ingroup los_exc - * @brief Register an exception handling hook. - * - * @par Description: - * This API is used to register an exception handling hook. - * @attention If the hook is registered for multiple times, the hook registered at the last time is effective. - * @attention The hook can be registered as NULL, indicating that the hook registration is canceled. - * @param excHook [IN] Type #EXC_PROC_FUNC: hook function. - * - * @retval #LOS_OK The exception handling hook is successfully registered. - * - * @par Dependency: - * los_exc.h: the header file that contains the API declaration. - * @see None. - */ -extern UINT32 LOS_ExcRegHook(EXC_PROC_FUNC excHook); - -/** - * @ingroup los_exc - * @brief Kernel panic function. - * - * @par Description: - * Stack function that prints kernel panics. - * @attention After this function is called and stack information is printed, the system will fail to respond. - * @attention The input parameter can be NULL. - * @param fmt [IN] Type #CHAR* : variadic argument. - * - * @retval #None. - * - * @par Dependency: - * los_exc.h: the header file that contains the API declaration. - * @see None. - */ -VOID LOS_Panic(const CHAR *fmt, ...); - -/** - * @ingroup los_exc - * @brief Kernel backtrace function. - * - * @par Description: - * Backtrace function that prints task call stack information traced from the running task. - * @attention None. - * - * @param None. - * - * @retval #None. - * - * @par Dependency: - * los_exc.h: the header file that contains the API declaration. - * @see None. - */ -extern VOID OsBackTrace(VOID); - -/** - * @ingroup los_exc - * @brief Kernel task backtrace function. - * - * @par Description: - * Backtrace function that prints task call stack information traced from the input task. - * @attention - *
    - *
  • The input taskID should be valid.
  • - *
- * - * @param taskID [IN] Type #UINT32 Task ID. - * - * @retval #None. - * - * @par Dependency: - * los_exc.h: the header file that contains the API declaration. - * @see None. - */ -extern VOID OsTaskBackTrace(UINT32 taskID); - -#ifdef __cplusplus -#if __cplusplus -} -#endif /* __cplusplus */ -#endif /* __cplusplus */ - -#endif /* _LOS_EXC_H */ +/* + * 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_exc Exception handling + * @ingroup kernel + */ +#ifndef _LOS_EXC_H +#define _LOS_EXC_H + +#include "los_typedef.h" +#include "arch_config.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +/** + * @ingroup los_exc + * Register information structure + * + * Description: register information stored when an exception occurs on an LPC2458 platform. + * + * Note: The following register names without uw are the register names used in the chip manual. + */ +#ifdef LOSCFG_ARCH_ARM_AARCH64 +#define EXC_GEN_REGS_NUM 30 +typedef struct { + UINT64 X[EXC_GEN_REGS_NUM]; /**< Register X0-X29 */ + UINT64 LR; /**< Program returning address. X30 */ + UINT64 SP; + UINT64 regELR; + UINT64 SPSR; +} ExcContext; +#else +typedef struct { + UINT32 USP; /**< User mode stack pointer */ + UINT32 ULR; /**< User mode program returning address */ + UINT32 regCPSR; /**< Current program status register (CPSR) */ + UINT32 R0; /**< Register R0 */ + UINT32 R1; /**< Register R1 */ + UINT32 R2; /**< Register R2 */ + UINT32 R3; /**< Register R3 */ + UINT32 R4; /**< Register R4 */ + UINT32 R5; /**< Register R5 */ + UINT32 R6; /**< Register R6 */ + UINT32 R7; /**< Register R7 */ + UINT32 R8; /**< Register R8 */ + UINT32 R9; /**< Register R9 */ + UINT32 R10; /**< Register R10 */ + UINT32 R11; /**< Register R11 */ + UINT32 R12; /**< Register R12 */ + UINT32 SP; /**< Stack pointer */ + UINT32 LR; /**< Program returning address. */ + UINT32 PC; /**< PC pointer of the exceptional function */ +} ExcContext; +#endif + +/** + * @ingroup los_exc + * Exception information structure + * + * Description: exception information stored when an exception occurs on an LPC2458 platform. + * + */ +typedef struct { + UINT16 phase; /**< Phase in which an exception occurs */ + UINT16 type; /**< Exception type */ + UINT16 nestCnt; /**< Count of nested exception */ + UINT16 reserved; /**< Reserved for alignment */ + ExcContext *context; /**< Hardware context when an exception occurs */ +} ExcInfo; + +/** + * @ingroup los_exc + * @brief Kernel FP Register address obtain function. + * + * @par Description: + * The API is used to obtain the FP Register address. + * @attention None. + * + * @param None. + * + * @retval #UINTPTR The FP Register address. + * + * @par Dependency: + * los_exc.h: the header file that contains the API declaration. + * @see None. + */ +STATIC INLINE UINTPTR Get_Fp(VOID) +{ + UINTPTR regFp; + +#ifdef LOSCFG_ARCH_ARM_AARCH64 + __asm__ __volatile__("mov %0, X29" : "=r"(regFp)); +#else + __asm__ __volatile__("mov %0, fp" : "=r"(regFp)); +#endif + + return regFp; +} + +/** + * @ingroup los_exc + * @brief Define an exception handling function hook. + * + * @par Description: + * This API is used to define the exception handling function hook based on the type of + * the exception handling function and record exceptions. + * @attention None. + * + * @param None. + * + * @retval None. + * + * @par Dependency: + * los_exc.h: the header file that contains the API declaration. + * @see None. + */ +typedef VOID (*EXC_PROC_FUNC)(UINT32, ExcContext *, UINT32, UINT32); + +/** + * @ingroup los_exc + * @brief Register an exception handling hook. + * + * @par Description: + * This API is used to register an exception handling hook. + * @attention If the hook is registered for multiple times, the hook registered at the last time is effective. + * @attention The hook can be registered as NULL, indicating that the hook registration is canceled. + * @param excHook [IN] Type #EXC_PROC_FUNC: hook function. + * + * @retval #LOS_OK The exception handling hook is successfully registered. + * + * @par Dependency: + * los_exc.h: the header file that contains the API declaration. + * @see None. + */ +extern UINT32 LOS_ExcRegHook(EXC_PROC_FUNC excHook); + +/** + * @ingroup los_exc + * @brief Kernel panic function. + * + * @par Description: + * Stack function that prints kernel panics. + * @attention After this function is called and stack information is printed, the system will fail to respond. + * @attention The input parameter can be NULL. + * @param fmt [IN] Type #CHAR* : variadic argument. + * + * @retval #None. + * + * @par Dependency: + * los_exc.h: the header file that contains the API declaration. + * @see None. + */ +VOID LOS_Panic(const CHAR *fmt, ...); + +/** + * @ingroup los_exc + * @brief record LR function. + * + * @par Description: + * @attention + * @param LR [IN] Type #UINTPTR * LR buffer. + * @param recordCount [IN] Type UINT32 record LR lay number. + * @param jumpCount [IN] Type UINT32 ignore LR lay number. + * + * @retval #None. + * + * @par Dependency: + * los_exc.h: the header file that contains the API declaration. + * @see None. + */ +VOID LOS_RecordLR(UINTPTR *LR, UINT32 LRSize, UINT32 recordCount, UINT32 jumpCount); + +/** + * @ingroup los_exc + * @brief Kernel backtrace function. + * + * @par Description: + * Backtrace function that prints task call stack information traced from the running task. + * @attention None. + * + * @param None. + * + * @retval #None. + * + * @par Dependency: + * los_exc.h: the header file that contains the API declaration. + * @see None. + */ +extern VOID OsBackTrace(VOID); + +/** + * @ingroup los_exc + * @brief Kernel task backtrace function. + * + * @par Description: + * Backtrace function that prints task call stack information traced from the input task. + * @attention + *
    + *
  • The input taskID should be valid.
  • + *
+ * + * @param taskID [IN] Type #UINT32 Task ID. + * + * @retval #None. + * + * @par Dependency: + * los_exc.h: the header file that contains the API declaration. + * @see None. + */ +extern VOID OsTaskBackTrace(UINT32 taskID); + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +#endif /* _LOS_EXC_H */ diff --git a/arch/arm/include/los_hw.h b/arch/arm/include/los_hw.h index c9d5825c38d09f8d04a506559d1432d79197aa98..ad30d5a1836c9c8003e306fe2011cd7ce4c84eab 100755 --- a/arch/arm/include/los_hw.h +++ b/arch/arm/include/los_hw.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/arch/arm/include/los_hw_arch.h b/arch/arm/include/los_hw_arch.h old mode 100755 new mode 100644 index f7bd812ad8a80ad680aa4f3f33d55e4c7d7bc020..3539d928fad82eaefa502f487c687f32eadcdda5 --- a/arch/arm/include/los_hw_arch.h +++ b/arch/arm/include/los_hw_arch.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/arch/arm/include/los_hw_tick_pri.h b/arch/arm/include/los_hw_tick_pri.h old mode 100755 new mode 100644 index 98a7296b3b52dc688b089e22bc71ebb06a41f272..1b8931762e938ce12175b1da81cc15371d6361a5 --- a/arch/arm/include/los_hw_tick_pri.h +++ b/arch/arm/include/los_hw_tick_pri.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/arch/arm/include/los_hwi.h b/arch/arm/include/los_hwi.h index b8bcadb970dadde2d568bf6131d3f2b5f7e8cf8e..cee783dcccde3626b032c214bae96c5e9e40528c 100755 --- a/arch/arm/include/los_hwi.h +++ b/arch/arm/include/los_hwi.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: @@ -339,6 +339,23 @@ STATIC INLINE VOID LOS_IntRestore(UINT32 intSave) ArchIntRestore(intSave); } +/** + * @ingroup los_hwi + * @brief Gets the maximum number of interrupts supported by the system. + * + * @par Description: + *
    + *
  • This API is used to gets the maximum number of interrupts supported by the system.
  • + *
+ * + * @param None. + * + * @retval None. + * @par Dependency: + *
  • los_hwi.h: the header file that contains the API declaration.
+ */ +extern UINT32 LOS_GetSystemHwiMaximum(VOID); + /** * @ingroup los_hwi * @brief Create a hardware interrupt. diff --git a/arch/arm/include/los_strncpy_from_user.h b/arch/arm/include/los_strncpy_from_user.h index 764be8a22b27fa7b13157d1b121ee3592e885006..58d68d9be6a1e9e702ccc90ecc9f57edb3bde69a 100755 --- a/arch/arm/include/los_strncpy_from_user.h +++ b/arch/arm/include/los_strncpy_from_user.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/arch/arm/include/los_strnlen_user.h b/arch/arm/include/los_strnlen_user.h index 5c90d5906e244d2d13c36d9296af8b930ef29370..29c3e67980322dabe24ff0741d279b2c9311626f 100755 --- a/arch/arm/include/los_strnlen_user.h +++ b/arch/arm/include/los_strnlen_user.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/arch/arm/include/los_sys_stack_pri.h b/arch/arm/include/los_sys_stack_pri.h old mode 100755 new mode 100644 index 0dbc46d10879a121060981026ca0144832f76344..e45e4a66f50ec1ec2777663b0ff64daca6a1b580 --- a/arch/arm/include/los_sys_stack_pri.h +++ b/arch/arm/include/los_sys_stack_pri.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/arch/arm/include/los_user_get.h b/arch/arm/include/los_user_get.h index 883d299ea564d42ee0b3205c2eb59ac8b36ab969..2ce55a0106f28866dbcd502bc3e1c895716bd658 100755 --- a/arch/arm/include/los_user_get.h +++ b/arch/arm/include/los_user_get.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/arch/arm/include/los_user_put.h b/arch/arm/include/los_user_put.h index bec15dae7c490c3e9be4f5af5551597e9af25a0d..c0251623a4db5cdb17f40b934205a6dc04c03b2c 100755 --- a/arch/arm/include/los_user_put.h +++ b/arch/arm/include/los_user_put.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/arch/arm/include/user_copy.h b/arch/arm/include/user_copy.h index 955634ec5e9b80c70e4e441b612f0de72a81d423..293d4e34d80af75d58a674e143edfd3012933d75 100755 --- a/arch/arm/include/user_copy.h +++ b/arch/arm/include/user_copy.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/arch/cpu.mk b/arch/cpu.mk old mode 100755 new mode 100644 index c1af11ac57b6eec47e6e01500fb8a1fcf76cf387..856099ef7462fd3f3aa6e37f6cac0266b5121ca8 --- a/arch/cpu.mk +++ b/arch/cpu.mk @@ -1,5 +1,5 @@ -# Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. -# Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. +# 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: diff --git a/bsd/Makefile b/bsd/Makefile old mode 100755 new mode 100644 index 9540092d36a62b13d7540a54f44bf0a624692800..a5c0d1ffe3edc391e03bfd89252f77ded4b6c323 --- a/bsd/Makefile +++ b/bsd/Makefile @@ -1,5 +1,5 @@ -# Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. -# Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. +# 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: diff --git a/bsd/compat/linuxkpi/Makefile b/bsd/compat/linuxkpi/Makefile index 5244c46976b5a48ebec3cd1be89205deb7d1aa3c..1b270ae19c4a62c192b1d01833f4063f9b3f9ada 100755 --- a/bsd/compat/linuxkpi/Makefile +++ b/bsd/compat/linuxkpi/Makefile @@ -1,5 +1,5 @@ -# Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. -# Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. +# 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: @@ -33,6 +33,10 @@ MODULE_NAME := $(notdir $(shell pwd)) LOCAL_SRCS := $(wildcard src/*.c) +ifneq ($(LOSCFG_HRTIMER_ENABLE), y) +LOCAL_SRCS := $(filter-out src/linux_hrtimer.c, $(LOCAL_SRCS)) +endif + LOCAL_INCLUDE := \ -I $(LITEOSTOPDIR)/kernel/base/include \ -I $(LITEOSTOPDIR)/bsd/compat/linuxkpi/include diff --git a/bsd/dev/usb/Makefile b/bsd/dev/usb/Makefile index 5eaa4120d32bac7eb84cae7e622c98d54ca733ff..a0fb6e8837bebed2867c8adeb42fec65dc6c68f5 100755 --- a/bsd/dev/usb/Makefile +++ b/bsd/dev/usb/Makefile @@ -1,5 +1,5 @@ -# Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. -# Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. +# 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: @@ -28,6 +28,7 @@ # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. include $(LITEOSTOPDIR)/config.mk +include $(LITEOSTOPDIR)/../../drivers/adapter/khdf/liteos/lite.mk MODULE_NAME := usb_base @@ -120,12 +121,20 @@ ifeq ($(LOSCFG_DRIVERS_USB_MASS_STORAGE), y) LOCAL_SRCS += $(STORAGE_SRC)/umass.c endif -ifeq ($(LOSCFG_DRIVERS_USB_HID_CLASS), y) +ifeq ($(LOSCFG_DRIVERS_USB_HID_CLASS)_$(LOSCFG_DRIVERS_HDF_INPUT), y_y) +LOCAL_FLAGS += -I$(LITEOSTOPDIR)/../../drivers/framework/model/input/driver \ + -I$(LITEOSTOPDIR)/../../drivers/framework/include/core \ + -I$(LITEOSTOPDIR)/../../drivers/framework/core/common/include/host \ + -I$(LITEOSTOPDIR)/../../drivers/framework/utils \ + -I$(LITEOSTOPDIR)/../../drivers/framework/osal \ + -I$(LITEOSTOPDIR)/../../drivers/framework/ability/sbuf/include \ + -I$(LITEOSTOPDIR)/../../drivers/framework/include/osal \ + -I$(LITEOSTOPDIR)/../../third_party/FreeBSD/sys/dev/evdev + LOCAL_SRCS += $(INPUT_SRC)/uhid.c \ $(CORE_SRC)/usb_hid.c endif LOCAL_FLAGS += $(LITEOS_GCOV_OPTS) -include $(MODULE) - +include $(HDF_DRIVER) diff --git a/build.sh b/build.sh index bcdf9009c6a35c8b1d9478186827acf5eefb5cd9..6ad37db5d67823d6908ef8f90c564bb5325044b4 100755 --- a/build.sh +++ b/build.sh @@ -1,7 +1,7 @@ #!/bin/bash # -# Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. -# Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. +# 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: @@ -30,20 +30,26 @@ # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. set -e -echo "sh param:$1,$2,$3" -source="tools/build/config/$1_release.config" +echo "sh param:$1,$2,$3,$4,$5,$6,$7" destination=".config" +tee="" +outdir="../..$3/test_info/gen/kernel/test" +if [ "$5" = "tee" ]; then + tee="_tee" +fi +productName="$(basename $7)" +source="tools/build/config/${productName}_release.config" if [ "$2" = "clang" ]; then - if [ "$3" = "debug" ]; then - source="tools/build/config/debug/$1_$2.config" + if [ "$4" = "debug" ]; then + source="tools/build/config/debug/${productName}_$2$tee.config" else - source="tools/build/config/$1_$2_release.config" + source="tools/build/config/${productName}_$2_release$tee.config" fi elif [ "$2" = "gcc" ]; then - if [ "$3" = "debug" ]; then - source="tools/build/config/$1_debug_shell.config" + if [ "$4" = "debug" ]; then + source="tools/build/config/${productName}_debug_shell$tee.config" else - source="tools/build/config/$1_release.config" + source="tools/build/config/${productName}_release$tee.config" fi fi if [ -d "./out" ]; then @@ -53,3 +59,7 @@ if [ -f "$destination" ]; then rm -rf $destination fi cp $source $destination + +mkdir -p $outdir +cp kernel_test.sources $outdir + diff --git a/compat/posix/Makefile b/compat/posix/Makefile old mode 100755 new mode 100644 index 02c6cc9f84a573eea26e964f403d6dd65458149b..c9cd39b06a8344b4d37337898cd1e113750b2c52 --- a/compat/posix/Makefile +++ b/compat/posix/Makefile @@ -1,5 +1,5 @@ -# Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. -# Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. +# 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: diff --git a/compat/posix/include/mqueue.h b/compat/posix/include/mqueue.h index c32b158d10dd96336e66d1ce2baeb1039ee3a67f..eb58bd0669684d477979b833300c8a8f48569fbc 100755 --- a/compat/posix/include/mqueue.h +++ b/compat/posix/include/mqueue.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: @@ -43,6 +43,9 @@ #include "limits.h" #include "los_typedef.h" #include "time.h" +#include +#include +#include #include "los_queue_pri.h" @@ -71,11 +74,23 @@ extern "C" { /* not suppurt prio */ #define MQ_PRIO_MAX 1 +typedef union send_receive_t { + unsigned oth : 3; + unsigned grp : 6; + unsigned usr : 9; + short data; +} mode_s; + /* TYPE DEFINITIONS */ struct mqarray { UINT32 mq_id : 31; UINT32 unlinkflag : 1; char *mq_name; + UINT32 unlink_ref; + mode_s mode_data; /* mode data of mqueue */ + uid_t euid; /* euid of mqueue */ + gid_t egid; /* egid of mqueue */ + fd_set mq_fdset; /* mqueue sysFd bit map */ LosQueueCB *mqcb; struct mqpersonal *mq_personal; }; @@ -84,7 +99,9 @@ struct mqpersonal { struct mqarray *mq_posixdes; struct mqpersonal *mq_next; int mq_flags; + int mq_mode; /* Mode of mqueue */ UINT32 mq_status; + UINT32 mq_refcount; }; /** @@ -400,6 +417,8 @@ extern int mq_timedsend(mqd_t personal, const char *msg, size_t msgLen, extern ssize_t mq_timedreceive(mqd_t personal, char *msg, size_t msgLen, unsigned int *msgPrio, const struct timespec *absTimeout); +extern void mqueue_refer(int sysFd); + #ifdef __cplusplus #if __cplusplus } diff --git a/compat/posix/include/time_posix.h b/compat/posix/include/time_posix.h index 0665d21b64097537c97e364b9ad467db2b1792cf..6f8f537d131f9f9aad55d45f7430635f74da1505 100755 --- a/compat/posix/include/time_posix.h +++ b/compat/posix/include/time_posix.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/compat/posix/src/map_error.c b/compat/posix/src/map_error.c old mode 100755 new mode 100644 index d2cdf013a465c0d719d4a401ebc7254254ac3851..31435f7d8ff1ee75a88d17e76f775689d1decb12 --- a/compat/posix/src/map_error.c +++ b/compat/posix/src/map_error.c @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/compat/posix/src/map_error.h b/compat/posix/src/map_error.h old mode 100755 new mode 100644 index 4731a32cdf5e1f460e6f96bd73c771db648df425..935b2d5e8124885dc61ed015d5631405f4e78def --- a/compat/posix/src/map_error.h +++ b/compat/posix/src/map_error.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/compat/posix/src/misc.c b/compat/posix/src/misc.c index 98152931202be05e95c2d00721f1735a65eabda0..3e623674e5699e037f7e6f805a4e4e291ae8f1e2 100755 --- a/compat/posix/src/misc.c +++ b/compat/posix/src/misc.c @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/compat/posix/src/mqueue.c b/compat/posix/src/mqueue.c index 0aa2705db3d1b69905cbd16e67d8a4cd0c0a7186..588ae11293b57bcf62ca3c11d0be2b4b38fd84bc 100755 --- a/compat/posix/src/mqueue.c +++ b/compat/posix/src/mqueue.c @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: @@ -36,6 +36,8 @@ #include "time_posix.h" #include "los_memory.h" #include "los_vm_map.h" +#include "los_process_pri.h" +#include "fs_file.h" #include "user_copy.h" #ifdef __cplusplus @@ -46,9 +48,14 @@ extern "C" { #define FNONBLOCK O_NONBLOCK +#ifndef MAX_MQ_FD +#define MAX_MQ_FD CONFIG_NQUEUE_DESCRIPTORS +#endif + /* GLOBALS */ STATIC struct mqarray g_queueTable[LOSCFG_BASE_IPC_QUEUE_LIMIT]; STATIC pthread_mutex_t g_mqueueMutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; +STATIC struct mqpersonal *g_mqPrivBuf[MAX_MQ_FD]; /* LOCAL FUNCTIONS */ STATIC INLINE INT32 MqNameCheck(const CHAR *mqName) @@ -100,7 +107,6 @@ STATIC INLINE struct mqarray *GetMqueueCBByName(const CHAR *name) return &(g_queueTable[index]); } } - return NULL; } @@ -114,6 +120,10 @@ STATIC INT32 DoMqueueDelete(struct mqarray *mqueueCB) } mqueueCB->mqcb = NULL; + /* When mqueue-list head node needed free ,reset the mode_data */ + mqueueCB->mode_data.data = 0; + mqueueCB->euid = -1; + mqueueCB->egid = -1; ret = LOS_QueueDelete(mqueueCB->mq_id); switch (ret) { @@ -152,7 +162,7 @@ STATIC int SaveMqueueName(const CHAR *mqName, struct mqarray *mqueueCB) return LOS_OK; } -STATIC struct mqpersonal *DoMqueueCreate(const struct mq_attr *attr, const CHAR *mqName, INT32 openFlag) +STATIC struct mqpersonal *DoMqueueCreate(const struct mq_attr *attr, const CHAR *mqName, INT32 openFlag, UINT32 mode) { struct mqarray *mqueueCB = NULL; UINT32 mqueueID; @@ -191,10 +201,13 @@ STATIC struct mqpersonal *DoMqueueCreate(const struct mq_attr *attr, const CHAR } mqueueCB->unlinkflag = FALSE; + mqueueCB->unlink_ref = 0; mqueueCB->mq_personal->mq_status = MQ_USE_MAGIC; mqueueCB->mq_personal->mq_next = NULL; mqueueCB->mq_personal->mq_posixdes = mqueueCB; mqueueCB->mq_personal->mq_flags = (INT32)((UINT32)openFlag | ((UINT32)attr->mq_flags & (UINT32)FNONBLOCK)); + mqueueCB->mq_personal->mq_mode = mode; + mqueueCB->mq_personal->mq_refcount = 0; return mqueueCB->mq_personal; ERROUT: @@ -228,6 +241,7 @@ STATIC struct mqpersonal *DoMqueueOpen(struct mqarray *mqueueCB, INT32 openFlag) privateMqPersonal->mq_posixdes = mqueueCB; privateMqPersonal->mq_flags = openFlag; privateMqPersonal->mq_status = MQ_USE_MAGIC; + privateMqPersonal->mq_refcount = 0; return privateMqPersonal; @@ -235,14 +249,226 @@ ERROUT: return (struct mqpersonal *)-1; } +/* Translate a sysFd into privateMqPersonal */ +STATIC struct mqpersonal *MqGetPrivDataBuff(mqd_t personal) +{ + INT32 sysFd = (INT32)personal; + INT32 id = sysFd - MQUEUE_FD_OFFSET; + + /* Filter illegal id */ + if ((id < 0) || (id >= MAX_MQ_FD)) { + errno = EBADF; + return NULL; + } + return g_mqPrivBuf[id]; +} + +/** + * Alloc sysFd, storage mq private data, set using bit. + * + * @param maxfdp: Maximum allowed application of mqueue sysFd. + * @param fdset: Mqueue sysFd bit map. + * @param privateMqPersonal: Private data. + * @return the index of the new fd; -1 on error + */ +STATIC INT32 MqAllocSysFd(int maxfdp, struct mqpersonal *privateMqPersonal) +{ + INT32 i; + struct mqarray *mqueueCB = privateMqPersonal->mq_posixdes; + fd_set *fdset = &mqueueCB->mq_fdset; + for (i = 0; i < maxfdp; i++) { + /* sysFd: used bit setting, and get the index of swtmrID buffer */ + if (!(fdset && FD_ISSET(i + MQUEUE_FD_OFFSET, fdset))) { + FD_SET(i + MQUEUE_FD_OFFSET, fdset); + if (!g_mqPrivBuf[i]) { + g_mqPrivBuf[i] = mqueueCB->mq_personal; + return i + MQUEUE_FD_OFFSET; + } + } + } + /* there are no more mq sysFd to use, free the personal */ + LOS_MemFree(OS_SYS_MEM_ADDR, privateMqPersonal); + privateMqPersonal = NULL; + mqueueCB->mq_personal = NULL; + return -1; +} + +STATIC VOID MqFreeSysFd(struct mqarray *mqueueCB, mqd_t personal) +{ + INT32 sysFd = (INT32)personal; + fd_set *fdset = &mqueueCB->mq_fdset; + if (fdset && FD_ISSET(sysFd, fdset)) { + FD_CLR(sysFd, fdset); + g_mqPrivBuf[sysFd - MQUEUE_FD_OFFSET] = NULL; + } +} + +/* Mqueue fd reference count */ +void mqueue_refer(int sysFd) +{ + struct mqarray *mqueueCB = NULL; + struct mqpersonal *privateMqPersonal = NULL; + + (VOID)pthread_mutex_lock(&g_mqueueMutex); + /* Get the personal sysFd and reset personal fd -1 */ + privateMqPersonal = MqGetPrivDataBuff((mqd_t)sysFd); + if (privateMqPersonal == NULL) { + goto OUT_UNLOCK; + } + mqueueCB = privateMqPersonal->mq_posixdes; + if (mqueueCB == NULL) { + goto OUT_UNLOCK; + } + privateMqPersonal->mq_refcount++; + mqueueCB->unlink_ref++; +OUT_UNLOCK: + (VOID)pthread_mutex_unlock(&g_mqueueMutex); +} + +STATIC INT32 MqTryClose(struct mqpersonal *privateMqPersonal) +{ + struct mqarray *mqueueCB = NULL; + mqueueCB = privateMqPersonal->mq_posixdes; + if (mqueueCB == NULL) { + errno = ENFILE; + return false; + } + + if (privateMqPersonal->mq_refcount == 0) { + return TRUE; + } + privateMqPersonal->mq_refcount--; + return FALSE; +} + +STATIC INT32 MqTryUnlink(struct mqarray *mqueueCB) +{ + if (mqueueCB->unlink_ref == 0) { + return TRUE; + } + mqueueCB->unlink_ref--; + return FALSE; +} + +/* Set the mode data bit,for consumer's mode comparing. */ +STATIC INT32 MqueueModeAnalysisSet(struct mqpersonal *privateMqPersonal) +{ + UINT32 mode; + UINT32 intSave; + User *user = NULL; + struct mqarray *mqueueCB = NULL; + + if ((INT32)(UINTPTR)privateMqPersonal < 0) { + return -1; + } + /* Get mqueueCB of first time creating mqueue */ + mqueueCB = privateMqPersonal->mq_posixdes; + if (mqueueCB == NULL) { + errno = ENFILE; + return -1; + } + + mode = mqueueCB->mq_personal->mq_mode; + /* Set mqueue gid uid */ + SCHEDULER_LOCK(intSave); + user = OsCurrUserGet(); + mqueueCB->euid = user->effUserID; + mqueueCB->egid = user->effGid; + SCHEDULER_UNLOCK(intSave); + + /* Set mode data bit */ + if (mode & S_IRUSR) { + mqueueCB->mode_data.usr |= S_IRUSR; + } + if (mode & S_IWUSR) { + mqueueCB->mode_data.usr |= S_IWUSR; + } + if (mode & S_IRGRP) { + mqueueCB->mode_data.grp |= S_IRGRP; + } + if (mode & S_IWGRP) { + mqueueCB->mode_data.grp |= S_IWGRP; + } + if (mode & S_IROTH) { + mqueueCB->mode_data.oth |= S_IROTH; + } + if (mode & S_IWOTH) { + mqueueCB->mode_data.oth |= S_IWOTH; + } + return 0; +} + +STATIC INT32 GetPermissionOfVisitor(struct mqarray *mqueueCB) +{ + uid_t euid; + gid_t egid; + UINT32 intSave; + User *user = NULL; + + if (mqueueCB == NULL) { + errno = ENOENT; + return -EPERM; + } + + /* Get the visitor process euid and egid */ + SCHEDULER_LOCK(intSave); + user = OsCurrUserGet(); + euid = user->effUserID; + egid = user->effGid; + SCHEDULER_UNLOCK(intSave); + + /* root */ + if (euid == 0) { + return ENOERR; + } + if (euid == mqueueCB->euid) { /* usr */ + if (!((mqueueCB->mode_data.usr & S_IRUSR) || (mqueueCB->mode_data.usr & S_IWUSR))) { + errno = EACCES; + goto ERR_OUT; + } + } else if (egid == mqueueCB->egid) { /* grp */ + if (!((mqueueCB->mode_data.grp & S_IRGRP) || (mqueueCB->mode_data.grp & S_IWGRP))) { + errno = EACCES; + goto ERR_OUT; + } + } else { /* oth */ + if (!((mqueueCB->mode_data.oth & S_IROTH) || (mqueueCB->mode_data.oth & S_IWOTH))) { + errno = EACCES; + goto ERR_OUT; + } + } + return ENOERR; + +ERR_OUT: + return -EPERM; +} + +STATIC INT32 GetMqueueAttr(struct mq_attr *defaultAttr, struct mq_attr *attr) +{ + if (attr != NULL) { + if (LOS_ArchCopyFromUser(defaultAttr, attr, sizeof(struct mq_attr))) { + errno = EFAULT; + return -1; + } + if ((defaultAttr->mq_maxmsg < 0) || (defaultAttr->mq_maxmsg > (long int)USHRT_MAX) || + (defaultAttr->mq_msgsize < 0) || (defaultAttr->mq_msgsize > (long int)(USHRT_MAX - sizeof(UINT32)))) { + errno = EINVAL; + return -1; + } + } + return 0; +} + mqd_t mq_open(const char *mqName, int openFlag, ...) { struct mqarray *mqueueCB = NULL; struct mqpersonal *privateMqPersonal = (struct mqpersonal *)-1; struct mq_attr *attr = NULL; struct mq_attr defaultAttr = { 0, MQ_MAX_MSG_NUM, MQ_MAX_MSG_LEN, 0 }; - int retVal; va_list ap; + int sysFd; + mqd_t mqFd = -1; + unsigned int mode = 0; if (MqNameCheck(mqName) == -1) { return (mqd_t)-1; @@ -259,56 +485,62 @@ mqd_t mq_open(const char *mqName, int openFlag, ...) privateMqPersonal = DoMqueueOpen(mqueueCB, openFlag); } else { va_start(ap, openFlag); - (VOID)va_arg(ap, int); + mode = va_arg(ap, unsigned int); attr = va_arg(ap, struct mq_attr *); va_end(ap); - if (attr != NULL) { - retVal = LOS_ArchCopyFromUser(&defaultAttr, attr, sizeof(struct mq_attr)); - if (retVal != 0) { - errno = EFAULT; - goto OUT; - } - if ((defaultAttr.mq_maxmsg < 0) || (defaultAttr.mq_maxmsg > (long int)USHRT_MAX) || - (defaultAttr.mq_msgsize < 0) || (defaultAttr.mq_msgsize > (long int)(USHRT_MAX - sizeof(UINT32)))) { - errno = EINVAL; - goto OUT; - } + if (GetMqueueAttr(&defaultAttr, attr)) { + goto OUT; } - privateMqPersonal = DoMqueueCreate(&defaultAttr, mqName, openFlag); + privateMqPersonal = DoMqueueCreate(&defaultAttr, mqName, openFlag, mode); + } + /* Set mode data bit ,just for the first node */ + if (MqueueModeAnalysisSet(privateMqPersonal)) { + goto OUT; } } else { - if (mqueueCB == NULL) { - errno = ENOENT; + if (GetPermissionOfVisitor(mqueueCB)) { goto OUT; } privateMqPersonal = DoMqueueOpen(mqueueCB, openFlag); } - OUT: + if ((INT32)(UINTPTR)privateMqPersonal > 0) { + /* alloc sysFd */ + sysFd = MqAllocSysFd(MAX_MQ_FD, privateMqPersonal); + if (sysFd == -1) { + errno = ENFILE; + } + mqFd = (mqd_t)sysFd; + } (VOID)pthread_mutex_unlock(&g_mqueueMutex); - return (mqd_t)privateMqPersonal; + return mqFd; } int mq_close(mqd_t personal) { - INT32 ret = 0; + INT32 ret = -1; struct mqarray *mqueueCB = NULL; struct mqpersonal *privateMqPersonal = NULL; struct mqpersonal *tmp = NULL; - if (!LOS_IsKernelAddressRange(personal, sizeof(struct mqpersonal))) { - errno = EBADF; - return -1; + (VOID)pthread_mutex_lock(&g_mqueueMutex); + + /* Get the personal sysFd and reset personal fd -1 */ + privateMqPersonal = MqGetPrivDataBuff(personal); + if (privateMqPersonal == NULL) { + goto OUT_UNLOCK; } - (VOID)pthread_mutex_lock(&g_mqueueMutex); - privateMqPersonal = (struct mqpersonal *)personal; if (privateMqPersonal->mq_status != MQ_USE_MAGIC) { errno = EBADF; goto OUT_UNLOCK; } - + /* there have other thread used the fd */ + if (!MqTryClose(privateMqPersonal)) { + ret = 0; + goto OUT_UNLOCK; + } mqueueCB = privateMqPersonal->mq_posixdes; if (mqueueCB->mq_personal == NULL) { errno = EBADF; @@ -332,6 +564,7 @@ int mq_close(mqd_t personal) } /* flag no use */ privateMqPersonal->mq_status = 0; + MqFreeSysFd(mqueueCB, personal); /* free the personal */ ret = LOS_MemFree(OS_SYS_MEM_ADDR, privateMqPersonal); @@ -354,8 +587,8 @@ int OsMqGetAttr(mqd_t personal, struct mq_attr *mqAttr) struct mqarray *mqueueCB = NULL; struct mqpersonal *privateMqPersonal = NULL; - if (!LOS_IsKernelAddressRange(personal, sizeof(struct mqpersonal))) { - errno = EBADF; + privateMqPersonal = MqGetPrivDataBuff(personal); + if (privateMqPersonal == NULL) { return -1; } @@ -365,7 +598,6 @@ int OsMqGetAttr(mqd_t personal, struct mq_attr *mqAttr) } (VOID)pthread_mutex_lock(&g_mqueueMutex); - privateMqPersonal = (struct mqpersonal *)personal; if (privateMqPersonal->mq_status != MQ_USE_MAGIC) { errno = EBADF; (VOID)pthread_mutex_unlock(&g_mqueueMutex); @@ -385,8 +617,8 @@ int OsMqSetAttr(mqd_t personal, const struct mq_attr *mqSetAttr, struct mq_attr { struct mqpersonal *privateMqPersonal = NULL; - if (!LOS_IsKernelAddressRange(personal, sizeof(struct mqpersonal))) { - errno = EBADF; + privateMqPersonal = MqGetPrivDataBuff(personal); + if (privateMqPersonal == NULL) { return -1; } @@ -396,7 +628,6 @@ int OsMqSetAttr(mqd_t personal, const struct mq_attr *mqSetAttr, struct mq_attr } (VOID)pthread_mutex_lock(&g_mqueueMutex); - privateMqPersonal = (struct mqpersonal *)personal; if (privateMqPersonal->mq_status != MQ_USE_MAGIC) { errno = EBADF; (VOID)pthread_mutex_unlock(&g_mqueueMutex); @@ -404,7 +635,7 @@ int OsMqSetAttr(mqd_t personal, const struct mq_attr *mqSetAttr, struct mq_attr } if (mqOldAttr != NULL) { - (VOID)OsMqGetAttr((mqd_t)privateMqPersonal, mqOldAttr); + (VOID)OsMqGetAttr(personal, mqOldAttr); } privateMqPersonal->mq_flags = (INT32)((UINT32)privateMqPersonal->mq_flags & (UINT32)(~FNONBLOCK)); /* clear */ @@ -438,10 +669,13 @@ int mq_unlink(const char *mqName) errno = ENOENT; goto ERROUT_UNLOCK; } - + if (!MqTryUnlink(mqueueCB)) { + (VOID)pthread_mutex_unlock(&g_mqueueMutex); + return 0; + } if (mqueueCB->mq_personal != NULL) { mqueueCB->unlinkflag = TRUE; - } else { + } else if (mqueueCB->unlink_ref == 0) { ret = DoMqueueDelete(mqueueCB); } @@ -476,8 +710,7 @@ STATIC INT32 ConvertTimeout(long flags, const struct timespec *absTimeout, UINT6 STATIC INLINE BOOL MqParamCheck(mqd_t personal, const char *msg, size_t msgLen) { - if (!LOS_IsKernelAddressRange(personal, sizeof(struct mqpersonal))) { - errno = EBADF; + if (personal < 0) { return FALSE; } @@ -507,11 +740,14 @@ int mq_timedsend(mqd_t personal, const char *msg, size_t msgLen, unsigned int ms struct mqpersonal *privateMqPersonal = NULL; OS_MQ_GOTO_ERROUT_IF(!MqParamCheck(personal, msg, msgLen), errno); - OS_MQ_GOTO_ERROUT_IF(msgPrio > (MQ_PRIO_MAX - 1), EINVAL); (VOID)pthread_mutex_lock(&g_mqueueMutex); - privateMqPersonal = (struct mqpersonal *)personal; + privateMqPersonal = MqGetPrivDataBuff(personal); + if (privateMqPersonal == NULL) { + goto ERROUT_UNLOCK; + } + OS_MQ_GOTO_ERROUT_UNLOCK_IF(privateMqPersonal->mq_status != MQ_USE_MAGIC, EBADF); mqueueCB = privateMqPersonal->mq_posixdes; @@ -554,7 +790,10 @@ ssize_t mq_timedreceive(mqd_t personal, char *msg, size_t msgLen, unsigned int * } (VOID)pthread_mutex_lock(&g_mqueueMutex); - privateMqPersonal = (struct mqpersonal *)personal; + privateMqPersonal = MqGetPrivDataBuff(personal); + if (privateMqPersonal == NULL) { + goto ERROUT_UNLOCK; + } if (privateMqPersonal->mq_status != MQ_USE_MAGIC) { errno = EBADF; goto ERROUT_UNLOCK; diff --git a/compat/posix/src/posix_memalign.c b/compat/posix/src/posix_memalign.c old mode 100755 new mode 100644 index 40af37dd67af6fd85f07835033450b8269af2aba..59e85702f9c8663653852c63762cc2e0fba56c07 --- a/compat/posix/src/posix_memalign.c +++ b/compat/posix/src/posix_memalign.c @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/compat/posix/src/pprivate.h b/compat/posix/src/pprivate.h old mode 100755 new mode 100644 index e484c81ad140f908052f32883f9cd9c51860ec1f..85f358ef800288e1e71dca73cb24a55eef4bd538 --- a/compat/posix/src/pprivate.h +++ b/compat/posix/src/pprivate.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/compat/posix/src/pthread.c b/compat/posix/src/pthread.c index 372fbe56165fe38ffe05a1edee2f55924daa1d5e..4fca2c13dd579fe78c77220ea6e5d82a86ff6e78 100755 --- a/compat/posix/src/pthread.c +++ b/compat/posix/src/pthread.c @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: @@ -36,6 +36,7 @@ #include "stdio.h" #include "map_error.h" #include "los_process_pri.h" +#include "los_sched_pri.h" #ifdef __cplusplus #if __cplusplus @@ -203,10 +204,9 @@ STATIC UINT32 InitPthreadData(pthread_t threadID, pthread_attr_t *userAttr, return LOS_NOK; } userAttr->stacksize = taskCB->stackSize; - err = memcpy_s(taskCB->taskName, OS_TCB_NAME_LEN, created->name, strlen(created->name)); - if (err != EOK) { + err = OsSetTaskName(taskCB, created->name, FALSE); + if (err != LOS_OK) { PRINT_ERR("%s: %d, err: %d\n", __FUNCTION__, __LINE__, err); - taskCB->taskName[0] = '\0'; return LOS_NOK; } #if (LOSCFG_KERNEL_SMP == YES) @@ -224,7 +224,7 @@ int pthread_create(pthread_t *thread, const pthread_attr_t *attr, { pthread_attr_t userAttr; UINT32 ret; - CHAR name[PTHREAD_DATA_NAME_MAX]; + CHAR name[PTHREAD_DATA_NAME_MAX] = {0}; STATIC UINT16 pthreadNumber = 1; TSK_INIT_PARAM_S taskInitParam = {0}; UINT32 taskHandle; @@ -236,7 +236,6 @@ int pthread_create(pthread_t *thread, const pthread_attr_t *attr, SetPthreadAttr(self, attr, &userAttr); - (VOID)memset_s(name, sizeof(name), 0, sizeof(name)); (VOID)snprintf_s(name, sizeof(name), sizeof(name) - 1, "pth%02d", pthreadNumber); pthreadNumber++; diff --git a/compat/posix/src/pthread_attr.c b/compat/posix/src/pthread_attr.c old mode 100755 new mode 100644 index ec3764be65933f56deefcfb232ec5b3be741c122..736c898a5924dd706c076a303e3ad2e990ff1725 --- a/compat/posix/src/pthread_attr.c +++ b/compat/posix/src/pthread_attr.c @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/compat/posix/src/pthread_cond.c b/compat/posix/src/pthread_cond.c index 0f824902b3d98ba3de81a864a72d7cea8021af17..1b9e320341164f69c200ec3d081dff80ac9c79e9 100755 --- a/compat/posix/src/pthread_cond.c +++ b/compat/posix/src/pthread_cond.c @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/compat/posix/src/pthread_mutex.c b/compat/posix/src/pthread_mutex.c index d12a3828aa74c0b321eeb97a598b183104ab6408..b4ab40150612a42c8326f69c63b056a0d7bc13f5 100755 --- a/compat/posix/src/pthread_mutex.c +++ b/compat/posix/src/pthread_mutex.c @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/compat/posix/src/sched.c b/compat/posix/src/sched.c old mode 100755 new mode 100644 index d48413034b4bb1d9a2532490c5b9bdde262ca8f1..feb713d8f127f0443c3824e3a33233d61ee31b61 --- a/compat/posix/src/sched.c +++ b/compat/posix/src/sched.c @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/compat/posix/src/semaphore.c b/compat/posix/src/semaphore.c index 425259ba952c82965241b927fb6e054697345f3a..63039039c871ee59944a42bd20da7a110b0fb3b2 100755 --- a/compat/posix/src/semaphore.c +++ b/compat/posix/src/semaphore.c @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/compat/posix/src/socket.c b/compat/posix/src/socket.c old mode 100755 new mode 100644 index f29443dc168896a15e07eea178e558688d62b932..23d00cf701a89ef52582364b100b448fad6c73df --- a/compat/posix/src/socket.c +++ b/compat/posix/src/socket.c @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/compat/posix/src/time.c b/compat/posix/src/time.c index d3a42f4eb7f912e73b5b65f0e4bcac30c47a7e06..462ea7c0d8c354b9f5f1028292bc5446435e6ad5 100755 --- a/compat/posix/src/time.c +++ b/compat/posix/src/time.c @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: @@ -30,7 +30,6 @@ */ #include "time.h" -#include "asm/hal_platform_ints.h" #include "stdint.h" #include "stdio.h" #include "sys/times.h" @@ -744,7 +743,7 @@ int timer_settime(timer_t timerID, int flags, swtmr->ucMode = interval ? LOS_SWTMR_MODE_OPP : LOS_SWTMR_MODE_NO_SELFDELETE; swtmr->uwExpiry = expiry + !!expiry; // PS: skip the first tick because it is NOT a full tick. swtmr->uwInterval = interval; - swtmr->ucOverrun = 0; + swtmr->uwOverrun = 0; LOS_SpinUnlockRestore(&g_swtmrSpin, intSave); if ((value->it_value.tv_sec == 0) && (value->it_value.tv_nsec == 0)) { @@ -813,20 +812,15 @@ int timer_getoverrun(timer_t timerID) return -1; } - overRun = (INT32)(swtmr->ucOverrun); + overRun = (INT32)(swtmr->uwOverrun); return (overRun > DELAYTIMER_MAX) ? DELAYTIMER_MAX : overRun; } -STATIC INT32 DoNanoSleep(UINT64 nseconds) +STATIC INT32 DoNanoSleep(UINT64 useconds) { - UINT64 tick; UINT32 ret; - const UINT32 nsPerTick = OS_SYS_NS_PER_SECOND / LOSCFG_BASE_CORE_TICK_PER_SECOND; - tick = (nseconds + nsPerTick - 1) / nsPerTick; // Round up for ticks - - /* PS: skip the first tick because it is NOT a full tick. */ - ret = LOS_TaskDelay((tick >= UINT32_MAX) ? UINT32_MAX : (tick ? ((UINT32)tick + 1) : 0)); + ret = LOS_TaskDelay(OsUS2Tick(useconds)); if (ret == LOS_OK || ret == LOS_ERRNO_TSK_YIELD_NOT_ENOUGH_TASK) { return 0; } @@ -835,12 +829,12 @@ STATIC INT32 DoNanoSleep(UINT64 nseconds) int usleep(unsigned useconds) { - return DoNanoSleep((UINT64)useconds * OS_SYS_NS_PER_US); + return DoNanoSleep((UINT64)useconds); } int nanosleep(const struct timespec *rqtp, struct timespec *rmtp) { - UINT64 nseconds; + UINT64 useconds; INT32 ret = -1; (VOID)rmtp; @@ -851,14 +845,14 @@ int nanosleep(const struct timespec *rqtp, struct timespec *rmtp) return ret; } - nseconds = (UINT64)rqtp->tv_sec * OS_SYS_NS_PER_SECOND + rqtp->tv_nsec; + useconds = (UINT64)rqtp->tv_sec * OS_SYS_US_PER_SECOND + rqtp->tv_nsec / OS_SYS_NS_PER_US; - return DoNanoSleep(nseconds); + return DoNanoSleep(useconds); } unsigned int sleep(unsigned int seconds) { - return DoNanoSleep((UINT64)seconds * OS_SYS_NS_PER_SECOND); + return DoNanoSleep((UINT64)seconds * OS_SYS_US_PER_SECOND); } double difftime(time_t time2, time_t time1) @@ -879,15 +873,10 @@ clock_t clock(VOID) clock_t times(struct tms *buf) { - clock_t clockTick; + clock_t clockTick = -1; - clockTick = LOS_TickCountGet(); - if (buf != NULL) { - buf->tms_cstime = clockTick; - buf->tms_cutime = clockTick; - buf->tms_stime = clockTick; - buf->tms_utime = clockTick; - } + (void)buf; + set_errno(ENOSYS); return clockTick; } @@ -914,6 +903,12 @@ int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue } } LOS_TaskUnlock(); + + if (!ValidTimeval(&value->it_value) || !ValidTimeval(&value->it_interval)) { + set_errno(EINVAL); + return -1; + } + TIMEVAL_TO_TIMESPEC(&value->it_value, &spec.it_value); TIMEVAL_TO_TIMESPEC(&value->it_interval, &spec.it_interval); diff --git a/config.mk b/config.mk index 04f95d535855202b78ff4d1d525d9c182e657177..50fd88773a8fb7708f37f42a1fd763f977102e18 100755 --- a/config.mk +++ b/config.mk @@ -1,5 +1,5 @@ -# Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. -# Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. +# 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: diff --git a/drivers/char/mem/Kconfig b/drivers/char/mem/Kconfig new file mode 100644 index 0000000000000000000000000000000000000000..7cfed49266d613441198dea7dfb6d9a1d087033d --- /dev/null +++ b/drivers/char/mem/Kconfig @@ -0,0 +1,6 @@ +config DRIVERS_MEM + bool "Enable MEM" + default y + depends on DRIVERS && FS_VFS + help + Answer Y to enable LiteOS support mem. \ No newline at end of file diff --git a/drivers/char/mem/Makefile b/drivers/char/mem/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..4e35bd1bdebf88811cf46eb8f46c35775c811aaf --- /dev/null +++ b/drivers/char/mem/Makefile @@ -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. + +include $(LITEOSTOPDIR)/config.mk + +MODULE_NAME := $(notdir $(shell pwd)) + +LOCAL_SRCS := $(wildcard src/*.c) + +include $(MODULE) diff --git a/net/lwip-2.1/porting/src/fixme.c b/drivers/char/mem/include/los_dev_mem.h old mode 100755 new mode 100644 similarity index 73% rename from net/lwip-2.1/porting/src/fixme.c rename to drivers/char/mem/include/los_dev_mem.h index e7b1268faa2af677c0c2172e7b6d3eacee66e036..cb5989e8ae145cb2eb51c526c644c6db7a3fcfb3 --- a/net/lwip-2.1/porting/src/fixme.c +++ b/drivers/char/mem/include/los_dev_mem.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: @@ -29,18 +29,23 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include -#include +#ifndef __LOS_DEV_MEM_H__ +#define __LOS_DEV_MEM_H__ -err_t dhcp_is_bound(struct netif *netif) -{ - return (netif_dhcp_data(netif) ? (netif_dhcp_data(netif)->state == DHCP_STATE_BOUND) ? ERR_OK : ERR_INPROGRESS : ERR_ARG); -} +#include "los_typedef.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ -#if (LWIP_CHKSUM_ALGORITHM == 4) /* version #4, asm based */ -#include "in_cksum.h" -u16_t lwip_standard_chksum(const void *dataptr, int len) -{ - return ~(u16_t)(in_cksum(dataptr, len)); +extern int DevMemRegister(void); + +#ifdef __cplusplus +#if __cplusplus } +#endif /* __cplusplus */ +#endif /* __cplusplus */ + #endif diff --git a/kernel/base/mem/common/memrecord/los_interto64radix.c b/drivers/char/mem/src/mem.c old mode 100755 new mode 100644 similarity index 51% rename from kernel/base/mem/common/memrecord/los_interto64radix.c rename to drivers/char/mem/src/mem.c index ecf91dac07092faa0e2e708312a4650268991167..df82cd07895854449b14ce793aa2e57e7d04b6fe --- a/kernel/base/mem/common/memrecord/los_interto64radix.c +++ b/drivers/char/mem/src/mem.c @@ -1,87 +1,108 @@ -/* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, 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_typedef.h" -#include "los_printf.h" - -#ifdef __cplusplus -#if __cplusplus -extern "C" { -#endif /* __cplusplus */ -#endif /* __cplusplus */ - -/* array used for 64 binary conversion, include 64 characters */ -const CHAR g_base64Array[] = { - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', - 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', - 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', - 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', - 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', - 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', - 'Y', 'Z', '~', '!' -}; - -#define LOGARITHM 6 /* the logarithm of 64 to base 2 */ -#define BASE64_MARK ((1U << LOGARITHM) - 1) - -VOID OsDecTo64F(UINT32 num, CHAR *base64, INT32 base64Len) -{ - INT32 len = base64Len - 1; - UINT32 tempNum = num; - if (base64 == NULL) { - PRINT_ERR("%s:%d input null buf\n", __FUNCTION__, __LINE__); - return; - } - - if (base64Len <= 0) { - PRINT_ERR("%s:%d input illegal Len\n", __FUNCTION__, __LINE__); - return; - } - - while (num) { - if (len < 0) { - PRINT_ERR("Len[%d] is too short, input num: %u\n", base64Len, tempNum); - break; - } - base64[len--] = g_base64Array[num & BASE64_MARK]; - num >>= LOGARITHM; - } - for (; len >= 0; len--) { - base64[len] = '0'; - } - base64[base64Len] = '\0'; -} - -#ifdef __cplusplus -#if __cplusplus -} -#endif /* __cplusplus */ -#endif /* __cplusplus */ +/* + * 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 "fcntl.h" +#include "linux/kernel.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +static int MemOpen(struct file *filep) +{ + return 0; +} + +static int MemClose(struct file *filep) +{ + return 0; +} + +static ssize_t MemRead(FAR struct file *filep, FAR char *buffer, size_t buflen) +{ + return 0; +} + +static ssize_t MemWrite(FAR struct file *filep, FAR const char *buffer, size_t buflen) +{ + return 0; +} + +static ssize_t MemMap(FAR struct file *filep, FAR LosVmMapRegion *region) +{ + size_t size = region->range.size; + PADDR_T paddr = region->pgOff << PAGE_SHIFT; + VADDR_T vaddr = region->range.base; + LosVmSpace *space = LOS_SpaceGet(vaddr); + + if ((paddr >= SYS_MEM_BASE) && (paddr < SYS_MEM_END)) { + return -EINVAL; + } + + /* Peripheral register memory adds strongly ordered attributes */ + region->regionFlags |= VM_MAP_REGION_FLAG_STRONGLY_ORDERED; + + if (space == NULL) { + return -EAGAIN; + } + if (LOS_ArchMmuMap(&space->archMmu, vaddr, paddr, size >> PAGE_SHIFT, region->regionFlags) <= 0) { + return -EAGAIN; + } + + return 0; +} + +static const struct file_operations_vfs g_memDevOps = { + MemOpen, /* open */ + MemClose, /* close */ + MemRead, /* read */ + MemWrite, /* write */ + NULL, /* seek */ + NULL, /* ioctl */ + MemMap, /* mmap */ +#ifndef CONFIG_DISABLE_POLL + NULL, /* poll */ +#endif + NULL, /* unlink */ +}; + +int DevMemRegister(void) +{ + return register_driver("/dev/mem", &g_memDevOps, 0666, 0); /* 0666: file mode */ +} + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ +#endif /* __cplusplus */ diff --git a/drivers/char/quickstart/Makefile b/drivers/char/quickstart/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..4e35bd1bdebf88811cf46eb8f46c35775c811aaf --- /dev/null +++ b/drivers/char/quickstart/Makefile @@ -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. + +include $(LITEOSTOPDIR)/config.mk + +MODULE_NAME := $(notdir $(shell pwd)) + +LOCAL_SRCS := $(wildcard src/*.c) + +include $(MODULE) diff --git a/kernel/base/include/los_timeslice_pri.h b/drivers/char/quickstart/include/los_quick_start.h old mode 100755 new mode 100644 similarity index 66% rename from kernel/base/include/los_timeslice_pri.h rename to drivers/char/quickstart/include/los_quick_start.h index 99f159ca0d4a4c854b6ff33a5f81471d1bab128d..c9b970826c1064ef1bbb4aae0a26845431a83b84 --- a/kernel/base/include/los_timeslice_pri.h +++ b/drivers/char/quickstart/include/los_quick_start.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: @@ -29,44 +29,18 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -/** - * @defgroup los_timeslice Timeslice - * @ingroup kernel - */ - -#ifndef _LOS_TIMESLICE_PRI_H -#define _LOS_TIMESLICE_PRI_H +#ifndef __LOS_QUICK_START_H__ +#define __LOS_QUICK_START_H__ #include "los_typedef.h" -#ifdef __cplusplus +fdef __cplusplus #if __cplusplus extern "C" { #endif /* __cplusplus */ #endif /* __cplusplus */ -/** - * @ingroup los_timeslice - * @brief Check time slices. - * - * @par Description: - *
    - *
  • This API is used to check time slices. If the number of Ticks equals to the time for task switch, - * tasks are switched. Otherwise, the Tick counting continues.
  • - *
- * @attention - *
    - *
  • None.
  • - *
- * - * @param None. - * - * @retval None. - * @par Dependency: - *
  • los_timeslice_pri.h: the header file that contains the API declaration.
- * @see None. - */ -extern VOID OsTimesliceCheck(VOID); +extern int DevQuickStartRegister(void); #ifdef __cplusplus #if __cplusplus @@ -74,4 +48,4 @@ extern VOID OsTimesliceCheck(VOID); #endif /* __cplusplus */ #endif /* __cplusplus */ -#endif /* _LOS_TIMESLICE_PRI_H */ +#endif diff --git a/kernel/extended/include/los_tickless_pri.h b/drivers/char/quickstart/include/los_quick_start_pri.h old mode 100755 new mode 100644 similarity index 72% rename from kernel/extended/include/los_tickless_pri.h rename to drivers/char/quickstart/include/los_quick_start_pri.h index 7116aa8eec03bec16c399c606616d2f4453830b3..012b548e4db602e8331ebb2b4260e740c7812d53 --- a/kernel/extended/include/los_tickless_pri.h +++ b/drivers/char/quickstart/include/los_quick_start_pri.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: @@ -29,10 +29,10 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef _LOS_TICKLESS_PRI_H -#define _LOS_TICKLESS_PRI_H +#ifndef __LOS_QUICK_START_PRI_H__ +#define __LOS_QUICK_START_PRI_H__ -#include "los_tickless.h" +#include "los_typedef.h" #ifdef __cplusplus #if __cplusplus @@ -40,15 +40,8 @@ extern "C" { #endif /* __cplusplus */ #endif /* __cplusplus */ -extern VOID OsTickTimerReload(UINT32 period); -extern VOID OsSysTimeUpdate(UINT32 sleepTicks); -extern VOID OsTicklessStart(VOID); -extern BOOL OsTickIrqFlagGet(VOID); -extern VOID OsTickIrqFlagSet(BOOL tickIrqFlag); -extern BOOL OsTicklessFlagGet(VOID); -extern UINT32 OsTicklessSleepTickGet(VOID); -extern VOID OsTicklessSleepTickSet(UINT32 sleeptick); -extern VOID OsTicklessUpdate(UINT32 irqNum); +unsigned int OsSystemInitStep2(void); +extern VOID SystemInit2(VOID); #ifdef __cplusplus #if __cplusplus @@ -56,4 +49,4 @@ extern VOID OsTicklessUpdate(UINT32 irqNum); #endif /* __cplusplus */ #endif /* __cplusplus */ -#endif /* _LOS_TICKLESS_PRI_H */ +#endif /* __LOS_QUICK_START_PRI_H__ */ \ No newline at end of file diff --git a/drivers/char/quickstart/src/qucikstart.c b/drivers/char/quickstart/src/qucikstart.c new file mode 100644 index 0000000000000000000000000000000000000000..8b4e8a57a6a0e90f7eabe6fe9577dd9c9a90bcbc --- /dev/null +++ b/drivers/char/quickstart/src/qucikstart.c @@ -0,0 +1,117 @@ +/* + * 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_quick_start_pri.h" +#include "bits/ioctl.h" +#include "fcntl.h" +#include "linux/kernel.h" + +#define QUICKSTART_IOC_MAGIC 'T' +#define QUICKSTART_INITSTEP2 _IO(QUICKSTART_IOC_MAGIC, 0) +#define QUICKSTART_UNREGISTER _IO(QUICKSTART_IOC_MAGIC, 1) +#define QUICKSTART_NODE "/dev/quickstart" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +static int QuickstartOpen(struct file *filep) +{ + return 0; +} + +static int QuickstartClose(struct file *filep) +{ + return 0; +} + +static void SystemInitStep2(void) +{ + static int once = 0; + /* Only one call is allowed */ + if (once != 0) { + return; + } + once = 1; + + unsigned int ret = OsSystemInitStep2(); + if (ret != LOS_OK) { + PRINT_ERR("systemInitStep2 failed\n"); + } +} + +static int QuickstartDevUnregister(void) +{ + return unregister_driver(QUICKSTART_NODE); +} + +static ssize_t QuickstartIoctl(struct file *filep, int cmd, unsigned long arg) +{ + switch (cmd) { + case QUICKSTART_INITSTEP2: + SystemInitStep2(); + break; + case QUICKSTART_UNREGISTER: + QuickstartDevUnregister(); + break; + + default: + break; + } + return 0; +} + +static const struct file_operations_vfs g_quickstartDevOps = { + QuickstartOpen, /* open */ + QuickstartClose, /* close */ + NULL, /* read */ + NULL, /* write */ + NULL, /* seek */ + QuickstartIoctl, /* ioctl */ + NULL, /* mmap */ +#ifndef CONFIG_DISABLE_POLL + NULL, /* poll */ +#endif + NULL, /* unlink */ +}; + +int DevQuickStartRegister(void) +{ + return register_driver(QUICKSTART_NODE, &g_quickstartDevOps, 0666, 0); /* 0666: file mode */ +} + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ +#endif /* __cplusplus */ diff --git a/drivers/char/random/Kconfig b/drivers/char/random/Kconfig new file mode 100644 index 0000000000000000000000000000000000000000..5574bef6b45f431f57b7f587880cfb4565aa7cf9 --- /dev/null +++ b/drivers/char/random/Kconfig @@ -0,0 +1,12 @@ +config DRIVERS_RANDOM + bool "Enable random" + default y + depends on DRIVERS && FS_VFS && COMPAT_BSD + help + Answer Y to support random. +config HW_RANDOM_ENABLE + depends on DRIVERS_RANDOM && (PLATFORM_HI3518EV300 || PLATFORM_HI3516CV300 || PLATFORM_HI3516DV300 || PLATFORM_HI3556V200) + bool "Select hw random" + default y + help + Answer Y to select hw random. diff --git a/drivers/char/random/Makefile b/drivers/char/random/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..edd03c4a4df96215cfd33a35a405ef53d9e57f5f --- /dev/null +++ b/drivers/char/random/Makefile @@ -0,0 +1,45 @@ +# 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 $(LITEOSTOPDIR)/config.mk + +MODULE_NAME := $(notdir $(shell pwd)) + +LOCAL_SRCS := $(wildcard src/random.c) + +ifdef LOSCFG_HW_RANDOM_ENABLE +LOCAL_SRCS += $(wildcard src/random_hw.c) +endif + +LOCAL_INCLUDE := -I $(LITEOSTOPDIR)/drivers/char/random/include \ + -I $(LITEOSTOPDIR)/../../$(LOSCFG_BOARD_CONFIG_PATH)/include + +LOCAL_FLAGS := $(LOCAL_INCLUDE) + +include $(MODULE) diff --git a/kernel/base/include/los_memrecord_pri.h b/drivers/char/random/include/los_random.h old mode 100755 new mode 100644 similarity index 63% rename from kernel/base/include/los_memrecord_pri.h rename to drivers/char/random/include/los_random.h index daa102e25426f6028aa6537714483140031dd268..298449ea78b86b07f4aeb046e65ac12569734036 --- a/kernel/base/include/los_memrecord_pri.h +++ b/drivers/char/random/include/los_random.h @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: @@ -29,10 +29,11 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef _LOS_MEMRECORD_PRI_H -#define _LOS_MEMRECORD_PRI_H +#ifndef __LOS_RANDOM_H__ +#define __LOS_RANDOM_H__ -#include "los_memory.h" +#include "los_typedef.h" +#include "sys/ioctl.h" #ifdef __cplusplus #if __cplusplus @@ -40,40 +41,21 @@ extern "C" { #endif /* __cplusplus */ #endif /* __cplusplus */ -#define RECORD_LEN 4000 +#define RAMDOM_IOC_MAGIC 'r' +#define RANDOM_SET_MAX _IO(RAMDOM_IOC_MAGIC, 1) -typedef struct { - UINT32 addrID; - UINT32 taskID; - UINT32 reqSizeID; - UINT32 allocatedSizeID; - UINT32 actType; - UINT64 sysTick; - UINT32 linkRegID; -} MemRecordInfo; - -#define MEM_RECORDSHOW_TIMEOUT 6000 - -extern VOID OsMemRecordShowSet(UINT32 value); -extern VOID OsMemRecordMalloc(const VOID *ptr, UINT32 size); -extern VOID OsMemRecordFree(const VOID *ptr, UINT32 size); -extern VOID OsMemRecordShowTask(VOID); -extern VOID OsDecTo64F(UINT32 num, CHAR *base64, INT32 base64Len); +int DevRandomRegister(void); +int DevUrandomRegister(void); +typedef struct { + int (*support)(void); /* Whether hard random numbers are supported */ + void (*init)(void); /* Initializing the hard random number generator */ + void (*deinit)(void); /* Deinitializing the hard random number generator */ + int (*read)(char *buffer, size_t buflen); /* Read hard random number */ + int (*ioctl)(int cmd, unsigned long arg); /* Control hard random number generator */ +} RandomOperations; -#ifndef LOSCFG_MEM_RECORDINFO -INLINE VOID OsMemRecordFree(const VOID *ptr, UINT32 size) -{ - return; -} -#endif - -#ifndef LOSCFG_MEM_RECORDINFO -INLINE VOID OsMemRecordMalloc(const VOID *ptr, UINT32 size) -{ - return; -} -#endif +void RandomOperationsInit(const RandomOperations *r); #ifdef __cplusplus #if __cplusplus @@ -81,4 +63,4 @@ INLINE VOID OsMemRecordMalloc(const VOID *ptr, UINT32 size) #endif /* __cplusplus */ #endif /* __cplusplus */ -#endif /* _LOS_MEMRECORD_PRI_H */ +#endif diff --git a/drivers/char/random/src/random.c b/drivers/char/random/src/random.c new file mode 100644 index 0000000000000000000000000000000000000000..9ef114d6cf8c59e709445d9175a21e2b2140c33e --- /dev/null +++ b/drivers/char/random/src/random.c @@ -0,0 +1,128 @@ +/* + * 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_random.h" +#include "fcntl.h" +#include "linux/kernel.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +static unsigned long g_randomMax = 0x7FFFFFFF; + +static long DoRand(unsigned long *value) +{ + long quotient, remainder, t; + + quotient = *value / 127773L; + remainder = *value % 127773L; + t = 16807L * remainder - 2836L * quotient; + if (t <= 0) { + t += 0x7fffffff; + } + return ((*value = t) % (g_randomMax + 1)); +} + +static unsigned long g_seed = 1; + +int RanOpen(struct file *filep) +{ + g_seed = (unsigned long)(hi_sched_clock() & 0xffffffff); + return 0; +} + +static int RanClose(struct file *filep) +{ + return 0; +} + +int RanIoctl(struct file *filep, int cmd, unsigned long arg) +{ + PRINT_ERR("random ioctl is not supported\n"); + return -ENOTSUP; +} + +ssize_t RanRead(struct file *filep, char *buffer, size_t buflen) +{ + ssize_t len = buflen; + char *buf = buffer; + unsigned int temp; + int ret; + + if (len % sizeof(unsigned int)) { + PRINT_ERR("random size not aligned by 4 bytes\n"); + return -EINVAL; + } + while (len > 0) { + temp = DoRand(&g_seed); + ret = LOS_CopyFromKernel((void *)buf, sizeof(unsigned int), (void *)&temp, sizeof(unsigned int)); + if (ret) { + break; + } + len -= sizeof(unsigned int); + buf += sizeof(unsigned int); + } + return (buflen - len); /* return a successful len */ +} + +static ssize_t RanMap(FAR struct file* filep, FAR LosVmMapRegion *region) +{ + PRINTK("%s %d, mmap is not support\n", __FUNCTION__, __LINE__); + return 0; +} + +static const struct file_operations_vfs g_ranDevOps = { + RanOpen, /* open */ + RanClose, /* close */ + RanRead, /* read */ + NULL, /* write */ + NULL, /* seek */ + RanIoctl, /* ioctl */ + RanMap, /* mmap */ +#ifndef CONFIG_DISABLE_POLL + NULL, /* poll */ +#endif + NULL, /* unlink */ +}; + +int DevRandomRegister(void) +{ + return register_driver("/dev/random", &g_ranDevOps, 0666, 0); /* 0666: file mode */ +} + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ +#endif /* __cplusplus */ diff --git a/drivers/char/random/src/random_hw.c b/drivers/char/random/src/random_hw.c new file mode 100644 index 0000000000000000000000000000000000000000..fe412ee6fa414b2444037f61aaf3bca72caf73e7 --- /dev/null +++ b/drivers/char/random/src/random_hw.c @@ -0,0 +1,127 @@ +/* + * 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_random.h" +#include "fcntl.h" +#include "hisoc/random.h" +#include "linux/kernel.h" + +static RandomOperations g_randomOp; +void RandomOperationsInit(const RandomOperations *r) +{ + if (r != NULL) { + (void)memcpy_s(&g_randomOp, sizeof(RandomOperations), r, sizeof(RandomOperations)); + } else { + PRINT_ERR("param is invalid\n", __FUNCTION__, __LINE__); + } + return; +} +static int RandomHwOpen(struct file *filep) +{ + if (g_randomOp.init != NULL) { + g_randomOp.init(); + return ENOERR; + } + return -1; +} + +static int RandomHwClose(struct file *filep) +{ + if (g_randomOp.deinit != NULL) { + g_randomOp.deinit(); + return ENOERR; + } + return -1; +} + +static int RandomHwIoctl(struct file *filep, int cmd, unsigned long arg) +{ + int ret; + + switch (cmd) { + default: + PRINT_ERR("!!!bad command!!!\n"); + return -EINVAL; + } + return ret; +} + +static ssize_t RandomHwRead(struct file *filep, char *buffer, size_t buflen) +{ + int ret; + + if (g_randomOp.read != NULL) { + ret = g_randomOp.read(buffer, buflen); + if (ret == ENOERR) { + ret = buflen; + } + } else { + ret = -1; + } + return ret; +} + +static ssize_t RandomMap(FAR struct file *filep, FAR LosVmMapRegion *region) +{ + PRINTK("%s %d, mmap is not support\n", __FUNCTION__, __LINE__); + return 0; +} + +static const struct file_operations_vfs g_randomHwDevOps = { + RandomHwOpen, /* open */ + RandomHwClose, /* close */ + RandomHwRead, /* read */ + NULL, /* write */ + NULL, /* seek */ + RandomHwIoctl, /* ioctl */ + RandomMap, /* mmap */ +#ifndef CONFIG_DISABLE_POLL + NULL, /* poll */ +#endif + NULL, /* unlink */ +}; + +int DevUrandomRegister(void) +{ + if (g_randomOp.support != NULL) { + int ret = g_randomOp.support(); + if (ret) { + return register_driver("/dev/urandom", &g_randomHwDevOps, 0666, 0); /* 0666: file mode */ + } + } + return -EPERM; +} + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ +#endif /* __cplusplus */ diff --git a/drivers/char/video/Kconfig b/drivers/char/video/Kconfig new file mode 100644 index 0000000000000000000000000000000000000000..3fad196adbb2919427acd9c07af545b610849e38 --- /dev/null +++ b/drivers/char/video/Kconfig @@ -0,0 +1,6 @@ +config DRIVERS_VIDEO + bool "Enable Video" + default y + depends on DRIVERS + help + Answer Y to enable LiteOS support video driver. \ No newline at end of file diff --git a/kernel/extended/tickless/Makefile b/drivers/char/video/Makefile old mode 100755 new mode 100644 similarity index 83% rename from kernel/extended/tickless/Makefile rename to drivers/char/video/Makefile index 813c1296f5b32558e5d81c4d631adcc697593b8c..790d2e6247b5d8ff9eecbc89af0ac88f66fbf0c3 --- a/kernel/extended/tickless/Makefile +++ b/drivers/char/video/Makefile @@ -1,5 +1,5 @@ -# Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. -# Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. +# 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: @@ -31,13 +31,10 @@ include $(LITEOSTOPDIR)/config.mk MODULE_NAME := $(notdir $(shell pwd)) -LOCAL_SRCS := $(wildcard *.c) +LOCAL_SRCS := $(LITEOSTHIRDPARTY)/NuttX/drivers/video/fb.c -LOCAL_INCLUDE := \ - -I $(LITEOSTOPDIR)/kernel/base/include -I $(LITEOSTOPDIR)/kernel/extended/include \ - -I $(LITEOSTOPDIR)/kernel/base/core +LOCAL_INCLUDE := -I $(LITEOSTHIRDPARTY)/NuttX/include/nuttx/video LOCAL_FLAGS := $(LOCAL_INCLUDE) include $(MODULE) - diff --git "a/figures/OpenHarmony-LiteOS-A\345\206\205\346\240\270\346\236\266\346\236\204\345\233\276.png" "b/figures/OpenHarmony-LiteOS-A\345\206\205\346\240\270\346\236\266\346\236\204\345\233\276.png" new file mode 100644 index 0000000000000000000000000000000000000000..b2ff552d12a62fb207505772fc4c8cad29262022 Binary files /dev/null and "b/figures/OpenHarmony-LiteOS-A\345\206\205\346\240\270\346\236\266\346\236\204\345\233\276.png" differ diff --git a/figures/architecture-of-the-openharmony-liteos-cortex-a-kernel.png b/figures/architecture-of-the-openharmony-liteos-cortex-a-kernel.png new file mode 100644 index 0000000000000000000000000000000000000000..27b8a0e059ea26e2eb4e53c27afd76f722761550 Binary files /dev/null and b/figures/architecture-of-the-openharmony-liteos-cortex-a-kernel.png differ diff --git a/fs/fat/Makefile b/fs/fat/Makefile index ad65ab6ba5931e1cab60a579ac80663be2c625f8..ce37249085bdc7e56722284eb15f75f2ecfda7ab 100755 --- a/fs/fat/Makefile +++ b/fs/fat/Makefile @@ -1,5 +1,5 @@ -# Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. -# Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. +# 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: @@ -35,8 +35,9 @@ LOCAL_SRCS := $(wildcard os_adapt/*.c) LOCAL_SRCS += $(wildcard $(LITEOSTHIRDPARTY)/FatFs/source/*.c) LOCAL_INCLUDE := \ - -I $(LITEOSTHIRDPARTY)/FatFs/source \ - -I $(LITEOSTOPDIR)/fs/fat/virpart/include + -I $(LITEOSTHIRDPARTY)/FatFs/source \ + -I $(LITEOSTOPDIR)/fs/fat/os_adapt \ + -I $(LITEOSTOPDIR)/fs/fat/virpart/include LOCAL_FLAGS := $(LOCAL_INCLUDE) $(LITEOS_GCOV_OPTS) diff --git a/fs/fat/os_adapt/dirop_fat.c b/fs/fat/os_adapt/dirop_fat.c new file mode 100755 index 0000000000000000000000000000000000000000..0515cf15324dd4b32307c8731ff9c3c73772a130 --- /dev/null +++ b/fs/fat/os_adapt/dirop_fat.c @@ -0,0 +1,274 @@ +/* + * 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 "dirop_fat.h" +#include "fatfs.h" +#include "errno.h" +#include "fs/fs.h" +#include "inode/inode.h" +#include "integer.h" +#include "string.h" + +#ifdef LOSCFG_FS_FAT + +#ifdef __cplusplus +#if __cplusplus +extern "C"{ +#endif +#endif /* __cplusplus */ + +extern const struct mountpt_operations fat_operations; +#define SECTOR_SIZE 512 +#define FIRST_MALLOC_SIZE 10 + +static INT vfat_check_path(const char *path) +{ + struct inode *inode_ptr = NULL; + char *fullpath = NULL; + INT ret = vfs_normalize_path((const char *)NULL, path, &fullpath); + struct inode_search_s desc; + + if (ret < ENOERR) { + ret = -ret; + set_errno(ret); + return FAT_ERROR; + } + + SETUP_SEARCH(&desc, fullpath, false); + ret = inode_find(&desc); + if (ret < 0) { + PRINT_ERR("ERROR: Failed to find %s\n", fullpath); + ret = -ret; + set_errno(ret); + return FAT_ERROR; + } + inode_ptr = desc.node; + + free(fullpath); + + if (inode_ptr && (inode_ptr->u.i_mops == &fat_operations)) { + inode_release(inode_ptr); + return ENOERR; + } + + return FAT_ERROR; +} + +static DIR_FAT *initdir_fat(DIR *dp) +{ + DIR_FAT *dir_fat = NULL; + + if (dp != NULL) { + dir_fat = (DIR_FAT *)malloc(sizeof(DIR_FAT) + PATH_MAX); + if (dir_fat != NULL) { + (void)memset_s(dir_fat, sizeof(DIR_FAT) + PATH_MAX, 0, sizeof(DIR_FAT) + PATH_MAX); + + dir_fat->stDirStream.dd_nextloc = 0; + dir_fat->stDirStream.dd_size = 0; + dir_fat->stBuf.d_count = SECTOR_SIZE; + dir_fat->stBuf.d_usecount = 0; + + (void)pthread_mutex_init(&(dir_fat->stDirStream.dd_lock), (const pthread_mutexattr_t *)NULL); + dir_fat->stDirStream.dp = dp; + + return dir_fat; + } + + (void)closedir(dp); + } + + return NULL; +} + +DIR_FAT *opendir_fat(const char *name) +{ + INT ret; + DIR *dp = NULL; + + ret = vfat_check_path(name); + if (ret) { + return NULL; + } + + dp = opendir(name); + + return initdir_fat(dp); +} + +int closedir_fat(DIR_FAT *dir_fat) +{ + INT ret; + + if (dir_fat == NULL) { + return FAT_ERROR; + } + + ret = closedir(dir_fat->stDirStream.dp); + if (ret == ENOERR) { + (void)pthread_mutex_destroy(&(dir_fat->stDirStream.dd_lock)); + free(dir_fat); + } + + return ret; +} + +extern int fatfs_readdir_all(DIR_FAT *dir_fat); + +struct fat_direntall *readdir_fat(DIR_FAT *dir_fat) +{ + INT ret; + struct fat_direntall *de = (struct fat_direntall *)NULL; + + if (dir_fat == NULL) { + return NULL; + } + + if (pthread_mutex_lock(&(dir_fat->stDirStream.dd_lock)) != ENOERR) { + return NULL; + } + + ret = fatfs_readdir_all(dir_fat); + if (!ret) { + de = &(dir_fat->stBuf.direntall); + } + + if (pthread_mutex_unlock(&(dir_fat->stDirStream.dd_lock)) != ENOERR) + PRINT_ERR("readdir_fat mutex unlock error \n"); + return de; +} + +static struct fat_direntall **scandir_fat_remalloc_names(struct fat_direntall **names, + UINT *names_size, UINT pos, bool *failed) +{ + struct fat_direntall **new_direntall = NULL; + INT32 ret; + + if (pos == *names_size) { + + if (*names_size == 0) { + *names_size = FIRST_MALLOC_SIZE; + } else { + *names_size <<= 1; + } + + new_direntall = (struct fat_direntall **)malloc(*names_size * sizeof(struct fat_direntall *)); + if (new_direntall == NULL) { + *failed = 1; + return names; + } + + if (names != NULL) { + ret = memcpy_s(new_direntall, (*names_size) * sizeof(struct fat_direntall *), + names, pos * sizeof(struct fat_direntall *)); + if (ret != EOK){ + *failed = 1; + free(new_direntall); + return names; + } + free(names); + } + return new_direntall; + } + return names; +} + +int scandir_fat(const char *dir, struct fat_direntall ***namelist, + int (*selector) (const struct fat_direntall *), + int (*compar) (const struct fat_direntall **, const struct fat_direntall **)) +{ + DIR_FAT *dp = opendir_fat(dir); + struct fat_direntall *current = NULL; + struct fat_direntall **names = NULL; + struct fat_direntall *vnew = NULL; + UINT names_size = 0; + UINT pos = 0; + UINT dsize; + bool failed = 0; + INT use_it; + + if (dp == NULL) { + return FAT_ERROR; + } + + current = readdir_fat(dp); + while (current != NULL) { + use_it = (selector == NULL); + if (!use_it) { + use_it = (*selector) (current); + } + if (use_it) { + names = scandir_fat_remalloc_names(names, &names_size, pos, &failed); + if (failed == 1) { + break; + } + + dsize = current->d_reclen; + vnew = (struct fat_direntall *)malloc(dsize); + if (vnew == NULL) { + failed = 1; + break; + } + + (void)memcpy_s(vnew, dsize, current, dsize); + names[pos++] = vnew; + } + current = readdir_fat(dp); + } + + if (failed == 1) { + (void)closedir_fat(dp); + while (pos > 0) { + free(names[--pos]); + } + + if (names != NULL) { + free(names); + } + + return FAT_ERROR; + } + + (void)closedir_fat(dp); + + /* Sort the list if we have a comparison function to sort with. */ + if (compar != NULL && names != NULL) { + qsort((void *)names, pos, sizeof (struct fat_direntall *), (int (*)(const void *, const void *))*compar); + } + *namelist = names; + return pos; +} + +#ifdef __cplusplus +#if __cplusplus +} +#endif +#endif /* __cplusplus */ +#endif /* CONFIG_FS_FAT */ diff --git a/fs/fat/os_adapt/dirop_fat.h b/fs/fat/os_adapt/dirop_fat.h new file mode 100755 index 0000000000000000000000000000000000000000..2f9b9fa2f77af4430f7bfa32c8a53150024999f5 --- /dev/null +++ b/fs/fat/os_adapt/dirop_fat.h @@ -0,0 +1,195 @@ +/* + * 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 fat Fat + * @ingroup filesystem + */ + +#ifndef _DIROP_FAT_H +#define _DIROP_FAT_H + +#include "pthread.h" +#include "dirent.h" +#ifdef LOSCFG_FS_FAT + +#ifdef __cplusplus +#if __cplusplus +extern "C"{ +#endif +#endif /* __cplusplus */ + +#define TIME_LENGTH 8 + +struct fat_direntall { + unsigned long d_ino; + unsigned long d_off; + unsigned char d_type; + unsigned int d_size; + char d_createtime[TIME_LENGTH]; + unsigned short d_reclen; + char d_name[1]; +}; + +struct fat_direntall_buf { + int d_count; + int d_usecount; + struct fat_direntall direntall; +}; + +struct dirstream_fat { + DIR *dp; + + /* offset of the next dir entry in buffer */ + unsigned int dd_nextloc; + + /* bytes of valid entries in buffer */ + unsigned int dd_size; + + pthread_mutex_t dd_lock; +}; + +typedef struct fat_dir{ + struct dirstream_fat stDirStream; + struct fat_direntall_buf stBuf; +} DIR_FAT; + +/** + * @ingroup fat + * @brief open a directory + * + * @par Description: + * This API is used to open a directory stream corresponding to the directory name, and + * returns a pointer to the directory stream. + * + * @attention + *
    + *
  • The parameter name should be a valid string.
  • + *
+ * + * @param name [IN] the directory to open. + * + * @retval #NULL Open directory unsuccessfully and set errno. + * @retval #DIR_FAT* A pointer to the directory stream. + * @par Dependency: + *
  • dirop_fat.h: the header file that contains the API declaration.
+ * @see closedir_fat + */ +DIR_FAT *opendir_fat(const char *name); + +/** + * @ingroup fat + * @brief close a directory + * + * @par Description: + * This API is used to close the directory stream associated with dirp. + * + * @attention + *
    + *
  • The parameter dir_fat should be a valid pointer which opendir_fat returns.
  • + *
+ * + * @param dir_fat [IN] Directory object structure pointer which opendir_fat returns. + * + * @retval #FAT_ERROR The directory dirp close unsuccessfully and set errno. + * @retval #OK The directory dirp close successfully. + * @par Dependency: + *
  • dirop_fat.h: the header file that contains the API declaration.
+ * @see opendir_fat + */ +int closedir_fat(DIR_FAT *dir_fat); + +/** + * @ingroup fat + * @brief read a directory + * + * @par Description: + * This API is used to get a pointer to a dirent structure + * representing the next directory entry in the directory stream pointed + * to by dirp. + * + * @attention + *
    + *
  • The parameter dir_fat should be a valid pointer which opendir_fat returns.
  • + *
+ * + * @param dir_fat [IN] An instance of type DIR created by a previous call to opendir_fat(). + * + * @retval #NULL Reaching the end of the directory stream or if an error occurred and set errno. + * @retval #fat_direntall* A pointer to a dirent structure. + * @par Dependency: + *
  • dirop_fat.h: the header file that contains the API declaration.
+ * @see opendir_fat + */ +struct fat_direntall *readdir_fat(DIR_FAT *dir_fat); + +/** +* @ingroup fat +* @brief scan a directory for matching entries. +* +* @par Description: +* The scandir_fat() function scans the directory dirp, calling selector() in +* each directory entry. Entries for which selector() returns nonzero are +* stored in strings allocated via malloc, sorted using qsort with +* the comparison function compar(), and collected in array namelist +* which is allocated via malloc. If filter is NULL, all entries are +* selected, compare with scandir(), scandir_fat() can get the create-time of +* file. +* +* @attention +*
    +*
  • +*
+* +* @param dir [IN] Type #const char* a pointer to directory information. +* @param namelist [OUT] Type #const struct fat_direntall*** a pointer to collected directory entries. +* @param selector [IN] Type #int(*selector)(const struct fat_direntall*) a filter type function. +* @param compar [IN] Type #int(*compar)(const struct fat_direntall**,const struct dirent**) a compar type function. +* +* @retval #int The number of directory entries selected. +* @retval #<0 An error occurred. +* @par Dependency: +*
  • dirop_fat.h: the header file that contains the API declaration.
+* @see readdir_fat +*/ +int scandir_fat(const char *dir, struct fat_direntall ***namelist, + int (*selector) (const struct fat_direntall *), + int (*compar) (const struct fat_direntall **, const struct fat_direntall **)); + +#ifdef __cplusplus +#if __cplusplus +} +#endif +#endif /* __cplusplus */ + +#endif /* LOSCFG_FS_FAT */ + +#endif /* _DIROP_FAT_H */ diff --git a/fs/fat/os_adapt/fat_shellcmd.c b/fs/fat/os_adapt/fat_shellcmd.c old mode 100755 new mode 100644 index 2720a2ea2a91b8a3323b4b6afbbd8db09fae1e4f..0648d29c7e96103a84c1258de30fc4b9493a6bd3 --- a/fs/fat/os_adapt/fat_shellcmd.c +++ b/fs/fat/os_adapt/fat_shellcmd.c @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: diff --git a/fs/fat/os_adapt/fatfs.c b/fs/fat/os_adapt/fatfs.c index 1bf4173ab93478a1d2ccf9017f69698317b62acf..e282910950334ab8c06ef81e7e42938e64d56b75 100755 --- a/fs/fat/os_adapt/fatfs.c +++ b/fs/fat/os_adapt/fatfs.c @@ -1,6 +1,6 @@ /* - * Copyright (c) 2013-2019, Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020, Huawei Device Co., Ltd. All rights reserved. + * 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: @@ -348,8 +348,6 @@ int fatfs_bind(struct inode *blkdriver, const void *data, void **handle, const c los_part *part = NULL; char drive[DRIVER_NAME_LEN]; struct FatfsMountOpts opts = {0}; - struct inode_search_s desc; - FAR struct inode *inode; FatParseOptions((char *)data, &opts); @@ -424,6 +422,8 @@ int fatfs_bind(struct inode *blkdriver, const void *data, void **handle, const c free(dir); #ifdef LOSCFG_FS_FAT_VIRTUAL_PARTITION /* Parent Partition Mount Success, then process virtual partition mount operation */ + struct inode_search_s desc; + FAR struct inode *inode; if (result == FR_OK) { if (fat->fs_type != FS_FAT32) return VIRERR_NOTFIT; @@ -472,8 +472,6 @@ int fatfs_unbind(void *handle, struct inode **blkdriver) INT result; los_part *part = NULL; char drive[DRIVER_NAME_LEN]; - struct inode_search_s desc; - FAR struct inode *inode; fat = (FATFS *)handle; if (fat == NULL) @@ -487,12 +485,14 @@ int fatfs_unbind(void *handle, struct inode **blkdriver) #ifdef LOSCFG_FS_FAT_VIRTUAL_PARTITION /* Release virtual partition objects before unbind real partition */ + struct inode_search_s desc; + FAR struct inode *inode; SETUP_SEARCH(&desc, g_fatVirPart.virtualinfo.devpartpath, false); FatfsVirtLock(); result = FatFsUnbindVirPart(fat); - FatfsVirtLock(); + FatfsVirtUnlock(); if (result == FR_OK) { - part = los_part_find(DiskDev[index]); + part = get_part((INT)fat->pdrv); (void)inode_find(&desc); inode = desc.node; if (part == NULL || part->dev == inode) { @@ -515,7 +515,7 @@ int fatfs_unbind(void *handle, struct inode **blkdriver) VolToPart[index].ps = 0; VolToPart[index].pc = 0; - part = los_part_find(DiskDev[index]); + part = get_part((INT)fat->pdrv); if (part != NULL && part->part_name) { free(part->part_name); part->part_name = NULL; @@ -543,17 +543,17 @@ static BYTE FatFsGetMode(int oflags, int *accMode) if ((UINT)oflags & O_WRONLY) { fmode |= FA_WRITE; - *accMode |= WRITE_OP; + *accMode = (unsigned int)(*accMode) | WRITE_OP; } if (((UINT)oflags & O_ACCMODE) & O_RDWR) { fmode |= FA_WRITE; - *accMode |= WRITE_OP; + *accMode = (unsigned int)(*accMode) | WRITE_OP; } /* Creates a new file if the file is not existing, otherwise, just open it. */ if ((UINT)oflags & O_CREAT) { fmode |= FA_OPEN_ALWAYS; - *accMode |= WRITE_OP; + *accMode = (unsigned int)(*accMode) | WRITE_OP; /* Creates a new file. If the file already exists, the function shall fail. */ if ((UINT)oflags & O_EXCL) fmode |= FA_CREATE_NEW; @@ -561,7 +561,7 @@ static BYTE FatFsGetMode(int oflags, int *accMode) /* Creates a new file. If the file already exists, its length shall be truncated to 0. */ if ((UINT)oflags & O_TRUNC) { fmode |= FA_CREATE_ALWAYS; - *accMode |= WRITE_OP; + *accMode = (unsigned int)(*accMode) | WRITE_OP; } return fmode; @@ -598,31 +598,31 @@ static unsigned int FatFsCheckPermission(struct inode *mountpt, bool dirFlag, in fileMode &= (READ_OP | WRITE_OP | EXEC_OP); - if ((accMode & fileMode) == accMode) { + if (((unsigned int)accMode & fileMode) == (unsigned int)accMode) { return FAT_PERM; } fileMode = 0; if (S_ISDIR(mountpt->i_mode)) { - if ((accMode & EXEC_OP) && (IsCapPermit(CAP_DAC_READ_SEARCH))) { + if (((unsigned int)accMode & EXEC_OP) && (IsCapPermit(CAP_DAC_READ_SEARCH))) { fileMode |= EXEC_OP; } } else { - if ((accMode & EXEC_OP) && (IsCapPermit(CAP_DAC_EXECUTE)) + if (((unsigned int)accMode & EXEC_OP) && (IsCapPermit(CAP_DAC_EXECUTE)) && (mountpt->i_mode & (S_IXUSR | S_IXGRP | S_IXOTH))) { fileMode |= EXEC_OP; } } - if ((accMode & WRITE_OP) && IsCapPermit(CAP_DAC_WRITE)) { + if (((unsigned int)accMode & WRITE_OP) && IsCapPermit(CAP_DAC_WRITE)) { fileMode |= WRITE_OP; } - if ((accMode & READ_OP) && IsCapPermit(CAP_DAC_READ_SEARCH)) { + if (((unsigned int)accMode & READ_OP) && IsCapPermit(CAP_DAC_READ_SEARCH)) { fileMode |= READ_OP; } - if ((accMode & fileMode) == accMode) { + if (((unsigned int)accMode & fileMode) == (unsigned int)accMode) { return FAT_PERM; } @@ -1190,7 +1190,7 @@ int fatfs_stat(struct inode *mountpt, const char *path, struct stat *buf) get_stmtime(buf, &finfo); } - if (result == FR_INVALID_NAME) { /* root directoy */ + if ((result == FR_INVALID_NAME) && (strlen(path) == 0)) { /* root directory */ buf->st_dev = 0; buf->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH; @@ -1256,7 +1256,6 @@ int fatfs_statfs(struct inode *mountpt, struct statfs *buf) return fatfs_2_vfs(result); } - (void)memset_s((void *)buf, sizeof(struct statfs), 0, sizeof(struct statfs)); buf->f_type = MSDOS_SUPER_MAGIC; buf->f_bfree = freClust; buf->f_bavail = freClust; @@ -1274,7 +1273,7 @@ int fatfs_statfs(struct inode *mountpt, struct statfs *buf) /* Maximum length of filenames: 8 is the basename length, 1 is the dot, 3 is the extension length */ buf->f_namelen = (8 + 1 + 3); #endif - + buf->f_flags = mountpt->mountflags; return 0; #else return -ENOSYS; @@ -1331,9 +1330,10 @@ int fatfs_utime(struct inode *mountpt, const char *pathname, const struct tm * s */ /* fdate: bit[15:9] Year since 1980(0..127), bit[8:5] Month(0..11), bit[4:0] Day(1..31) */ - finfo.fdate = (WORD)(((set_tm->tm_year - 80) << 9) | (set_tm->tm_mon + 1) << 5 | set_tm->tm_mday); + finfo.fdate = (WORD)(((UINT)(set_tm->tm_year - 80) << 9) | ((UINT)(set_tm->tm_mon + 1) << 5) | + (UINT)set_tm->tm_mday); /* ftime: bit[15:11] Hour(0..23), bit[10:5] Minute(0..59), bit[4:0] Second/2(0..29) */ - finfo.ftime = (WORD)(set_tm->tm_hour << 11 | set_tm->tm_min << 5 | set_tm->tm_sec >> 1); + finfo.ftime = (WORD)(((UINT)set_tm->tm_hour << 11) | ((UINT)set_tm->tm_min << 5) | ((UINT)set_tm->tm_sec >> 1)); result = f_utime(filePath, &finfo); status = fatfs_2_vfs(result); free(filePath); @@ -1421,6 +1421,7 @@ int fatfs_closedir(struct inode *mountpt, struct fs_dirent_s *dir) int fatfs_readdir(struct inode *mountpt, struct fs_dirent_s *dir) { + int i = 0; #if FF_FS_MINIMIZE <= 1 DIR *fdir = NULL; FILINFO fno; @@ -1429,37 +1430,41 @@ int fatfs_readdir(struct inode *mountpt, struct fs_dirent_s *dir) FAT_CHECK(dir); fdir = (DIR *)(dir->u.fs_dir); - d = &dir->fd_dir; - (void)memset_s(&fno, sizeof(FILINFO), 0, sizeof(FILINFO)); - result = f_readdir(fdir, &fno); - if (result != FR_OK) { - return fatfs_2_vfs(result); - } + while (i < dir->read_cnt) { + d = &(dir->fd_dir[i]); - /* - * 0x00:Reached end of directory. - * 0xFF:The directory is empty. - */ - if (fno.fname[0] == 0 || fno.fname[0] == (TCHAR)0xFF) { - return -ENOENT; - } + (void)memset_s(&fno, sizeof(FILINFO), 0, sizeof(FILINFO)); + result = f_readdir(fdir, &fno); + if (result != FR_OK) { + break; + } - if (fno.fattrib & AM_DIR) { - d->d_type = DT_DIR; - } else { - d->d_type = DT_REG; - } + /* + * 0x00:Reached end of directory. + * 0xFF:The directory is empty. + */ + if (fno.fname[0] == 0 || fno.fname[0] == (TCHAR)0xFF) { + break; + } + + if (fno.fattrib & AM_DIR) { + d->d_type = DT_DIR; + } else { + d->d_type = DT_REG; + } - d->d_reclen = (WORD)sizeof(struct dirent); + if (strncpy_s(d->d_name, sizeof(d->d_name), fno.fname, strlen(fno.fname)) != EOK) { + return -ENAMETOOLONG; + } - if (strncpy_s(d->d_name, sizeof(d->d_name), fno.fname, strlen(fno.fname)) != EOK) { - return -ENAMETOOLONG; + dir->fd_position++; + dir->fd_dir[i].d_off = dir->fd_position; + dir->fd_dir[i].d_reclen = (uint16_t)sizeof(struct dirent); + i++; } - d->d_off = dir->fd_position; - - return ENOERR; + return i; #else return -ENOSYS; #endif diff --git a/fs/fat/os_adapt/fatfs.h b/fs/fat/os_adapt/fatfs.h new file mode 100755 index 0000000000000000000000000000000000000000..cae3ec676a9f95108d27b33a9e73618b554cd3c2 --- /dev/null +++ b/fs/fat/os_adapt/fatfs.h @@ -0,0 +1,100 @@ +/* + * 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. + */ + +#ifndef _FATFS_H +#define _FATFS_H + +#include "fs/fs.h" +#include "disk.h" +#include "unistd.h" +#include "string.h" +#include "stdio.h" +#include "stdlib.h" +#include "time.h" +#include "sys/stat.h" +#include "sys/statfs.h" + +#include "inode/inode.h" +#include "fs/dirent_fs.h" +#include "fcntl.h" + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +#define MAX_LFNAME_LENTH 256 +#define LABEL_LEN 12 +#define FAT32_MAXSZIE 0x100000000 +#define FAT_ERROR (-1) +extern char FatLabel[LABEL_LEN]; + +#define VOLUME_CHAR_LENGTH 4 +#define FAT_CHECK(ptr) \ + do { \ + if ((ptr) == NULL) \ + return -EINVAL; \ + } while (0) + +int fatfs_bind (struct inode *blkdriver, const void *data, void **handle, const char *realpath); +int fatfs_unbind (void *handle, struct inode **blkdriver); +int fatfs_mkfs (const char *dev, int sectors, int option); +int fatfs_statfs (struct inode *mountpt, struct statfs *buf); +int fatfs_open (struct file *filep, const char *relpath, int oflags, mode_t mode); +int fatfs_close (struct file *filep); +int fatfs_ioctl (FAR struct file *filep, int cmd, unsigned long arg); +ssize_t fatfs_read (struct file *filep, char *buffer, size_t buflen); +ssize_t fatfs_write (struct file *filep, const char *buffer, size_t buflen); +int fatfs_sync (struct file *filep); +int fatfs_virstatfs_internel (struct inode *mountpt, const char *relpath, struct statfs *buf); +int fatfs_dup (FAR const struct file *oldp, FAR struct file *newp); +off_t fatfs_seek (struct file *filep, off_t offset, int whence); +int fatfs_unlink (struct inode *mountpt, const char *relpath); +int fatfs_rename (struct inode *mountpt, const char *oldpath, const char *newpath); +int fatfs_stat (struct inode *mountpt, const char *path, struct stat *st); +int fatfs_opendir (struct inode *mountpt, const char *relpath, struct fs_dirent_s *dir); +int fatfs_closedir (struct inode *mountpt, struct fs_dirent_s *dir); +int fatfs_readdir (struct inode *mountpt, struct fs_dirent_s *dir); +int fatfs_rewinddir (struct inode *mountpt, struct fs_dirent_s *dir); +int fatfs_mkdir (struct inode *mountpt, const char *relpath, mode_t mode); +int fatfs_rmdir (struct inode *mountpt, const char *relpath); +int fatfs_utime (struct inode *mountpt, const char *pathname, const struct tm *times); +int fatfs_getlabel (void *handle, char *label); +int fatfs_2_vfs (int result); + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +#endif diff --git a/fs/fat/os_adapt/format.c b/fs/fat/os_adapt/format.c new file mode 100755 index 0000000000000000000000000000000000000000..6ffbfb6ac27d78072841e2543477cab40620a0c1 --- /dev/null +++ b/fs/fat/os_adapt/format.c @@ -0,0 +1,99 @@ +/* + * 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 "errno.h" +#include "fatfs.h" +#include "unistd.h" +#include "stdio.h" +#include "string.h" +#include "errcode_fat.h" +#include "integer.h" +#ifdef LOSCFG_FS_FAT + +#define DEV_NAME_SIZE 4 +int format(const char *dev, int sectors, int option) +{ + INT err; + if (dev == NULL) { + set_errno(EINVAL); + return -1; + } + + if (strncmp(dev, "/dev", DEV_NAME_SIZE) != 0) { + PRINTK("Usage :\n"); + PRINTK(" format