From b361e0b1a38f95fee6ab657dc24f5337f4c02226 Mon Sep 17 00:00:00 2001 From: Christophe Priouzeau Date: Mon, 6 Jun 2022 15:14:36 +0200 Subject: [PATCH] OPTEE: 3.16.0-stm32mp1-r1 Signed-off-by: Christophe Priouzeau Change-Id: Idd8b84221d5f0621b31dbd8523e0b8e28368dc89 --- .../optee/optee-os-stm32mp-archiver.inc | 32 +- .../optee/optee-os-stm32mp-common.inc | 10 +- ...p_3.12.0.bb => optee-os-stm32mp_3.16.0.bb} | 17 +- .../optee-os/0001-3.12.0-stm32mp-r1.patch | 21255 --- .../optee-os/0001-3.16.0-stm32mp-r1.patch | 127744 +++++++++++++++ .../0002-3.12.0-stm32mp-r1.1-rc1.patch | 61 - .../optee-os/0003-3.12.0-stm32mp-r2.patch | 2251 - .../optee/optee-os/README.HOW_TO.txt | 78 +- recipes-security/optee/optee-os/fonts.tar.gz | Bin 0 -> 398382 bytes 9 files changed, 127818 insertions(+), 23630 deletions(-) rename recipes-security/optee/{optee-os-stm32mp_3.12.0.bb => optee-os-stm32mp_3.16.0.bb} (79%) delete mode 100644 recipes-security/optee/optee-os/0001-3.12.0-stm32mp-r1.patch create mode 100644 recipes-security/optee/optee-os/0001-3.16.0-stm32mp-r1.patch delete mode 100644 recipes-security/optee/optee-os/0002-3.12.0-stm32mp-r1.1-rc1.patch delete mode 100644 recipes-security/optee/optee-os/0003-3.12.0-stm32mp-r2.patch create mode 100644 recipes-security/optee/optee-os/fonts.tar.gz diff --git a/recipes-security/optee/optee-os-stm32mp-archiver.inc b/recipes-security/optee/optee-os-stm32mp-archiver.inc index d3cb532..a92e506 100644 --- a/recipes-security/optee/optee-os-stm32mp-archiver.inc +++ b/recipes-security/optee/optee-os-stm32mp-archiver.inc @@ -21,31 +21,16 @@ DEPLOYDIR ?= \$(SRC_PATH)/../deploy # Set default optee-os config CFG_EMBED_DTB_SOURCE_FILE ?= ${OPTEE_CONF} -# Configure default optee-os features -ENABLE_FIP ?= ${@bb.utils.contains('MACHINE_FEATURES','fip','1','',d)} - # Remove default variables LDFLAGS = CFLAGS = CPPFLAGS = # Define default make options EXTRA_OEMAKE = $(echo "${EXTRA_OEMAKE}" | sed "s|LIBGCC_LOCATE_CFLAGS=[^ ]* |LIBGCC_LOCATE_CFLAGS=\$(KCFLAGS) |") -ifeq (\$(ENABLE_FIP), 0) -EXTRA_OEMAKE += CFG_STM32MP15x_STM32IMAGE=y -endif - -# Configure default optee-os features -ifeq (\$(ENABLE_FIP), 1) -OPTEE_SUFFIX ?= bin -else -OPTEE_SUFFIX ?= stm32 -endif # Set dependencies list for building all DEPS = optee -ifeq (\$(ENABLE_FIP), 1) DEPS += fip -endif help: @echo @@ -58,10 +43,7 @@ help: @echo " DEPLOYDIR = \$(DEPLOYDIR)" @echo @echo "FIP configuration:" - @echo " ENABLE_FIP = \$(ENABLE_FIP) ('1' to generate fip binary)" -ifeq (\$(ENABLE_FIP),1) @echo " Do not forget to set FIP deploydir folders (such as FIP_DEPLOYDIR_ROOT) to provide path to needed binaries" -endif @echo @echo "Available targets:" @echo " all : build OPTEE-OS binaries for defined config(s)" @@ -77,22 +59,22 @@ optee: for dt in \$(CFG_EMBED_DTB_SOURCE_FILE) ; do \\ \$(MAKE) \$(EXTRA_OEMAKE) -C \$(SRC_PATH) PREFIX=\$(SDKTARGETSYSROOT) O=\$(BLD_PATH)/\$\$dt CFG_EMBED_DTB_SOURCE_FILE=\$\$dt.dts ; \\ # Copy binary files with explicit name \\ - cp \$(BLD_PATH)/\$\$dt/core/${OPTEE_HEADER}.\$(OPTEE_SUFFIX) \$(DEPLOYDIR)/${OPTEE_HEADER}-\$\$dt.\$(OPTEE_SUFFIX) ; \\ - cp \$(BLD_PATH)/\$\$dt/core/${OPTEE_PAGER}.\$(OPTEE_SUFFIX) \$(DEPLOYDIR)/${OPTEE_PAGER}-\$\$dt.\$(OPTEE_SUFFIX) ; \\ - cp \$(BLD_PATH)/\$\$dt/core/${OPTEE_PAGEABLE}.\$(OPTEE_SUFFIX) \$(DEPLOYDIR)/${OPTEE_PAGEABLE}-\$\$dt.\$(OPTEE_SUFFIX) ; \\ + cp \$(BLD_PATH)/\$\$dt/core/${OPTEE_HEADER}.${OPTEE_SUFFIX} \$(DEPLOYDIR)/${OPTEE_HEADER}-\$\$dt.${OPTEE_SUFFIX} ; \\ + cp \$(BLD_PATH)/\$\$dt/core/${OPTEE_PAGER}.${OPTEE_SUFFIX} \$(DEPLOYDIR)/${OPTEE_PAGER}-\$\$dt.${OPTEE_SUFFIX} ; \\ + cp \$(BLD_PATH)/\$\$dt/core/${OPTEE_PAGEABLE}.${OPTEE_SUFFIX} \$(DEPLOYDIR)/${OPTEE_PAGEABLE}-\$\$dt.${OPTEE_SUFFIX} ; \\ cp \$(BLD_PATH)/\$\$dt/core/${OPTEE_ELF}.${OPTEE_ELF_SUFFIX} \$(DEPLOYDIR)/debug/${OPTEE_ELF}-\$\$dt.${OPTEE_ELF_SUFFIX} ; \\ done ; \\ else \\ \$(MAKE) \$(EXTRA_OEMAKE) -C \$(SRC_PATH) PREFIX=\$(SDKTARGETSYSROOT) O=\$(BLD_PATH)/ ; \\ # Copy binary files with explicit name \\ - cp \$(BLD_PATH)/core/${OPTEE_HEADER}.\$(OPTEE_SUFFIX) \$(DEPLOYDIR)/ ; \\ - cp \$(BLD_PATH)/core/${OPTEE_PAGER}.\$(OPTEE_SUFFIX) \$(DEPLOYDIR)/ ; \\ - cp \$(BLD_PATH)/core/${OPTEE_PAGEABLE}.\$(OPTEE_SUFFIX) \$(DEPLOYDIR)/ ; \\ + cp \$(BLD_PATH)/core/${OPTEE_HEADER}.${OPTEE_SUFFIX} \$(DEPLOYDIR)/ ; \\ + cp \$(BLD_PATH)/core/${OPTEE_PAGER}.${OPTEE_SUFFIX} \$(DEPLOYDIR)/ ; \\ + cp \$(BLD_PATH)/core/${OPTEE_PAGEABLE}.${OPTEE_SUFFIX} \$(DEPLOYDIR)/ ; \\ cp \$(BLD_PATH)/core/${OPTEE_ELF}.${OPTEE_ELF_SUFFIX} \$(DEPLOYDIR)/debug/ ; \\ fi fip: optee - FIP_DEPLOYDIR_OPTEE=\$(DEPLOYDIR) FIP_DEVICETREE="\$(CFG_EMBED_DTB_SOURCE_FILE)" fiptool-stm32mp + FIP_DEPLOYDIR_OPTEE=\$(DEPLOYDIR) FIP_DEVICETREE="\$(CFG_EMBED_DTB_SOURCE_FILE)" FIP_CONFIG="optee" FIP_BL32_CONF="optee" fiptool-stm32mp clean: @echo "Removing \$(BLD_PATH) ..." diff --git a/recipes-security/optee/optee-os-stm32mp-common.inc b/recipes-security/optee/optee-os-stm32mp-common.inc index b108612..4bbd632 100644 --- a/recipes-security/optee/optee-os-stm32mp-common.inc +++ b/recipes-security/optee/optee-os-stm32mp-common.inc @@ -13,6 +13,7 @@ DEPENDS += "dtc-native" DEPENDS += "python3-pycryptodomex-native" DEPENDS += "python3-pyelftools-native" DEPENDS += "libgcc python3-cryptography-native" +DEPENDS += "python3-pillow-native" inherit deploy python3native @@ -22,17 +23,18 @@ OPTEEOUTPUTMACHINE ?= "${MACHINE}" # Default log level ST_OPTEE_DEBUG_LOG_LEVEL ??= "2" +# default core debug +ST_OPTEE_CORE_DEBUG ??="y" + EXTRA_OEMAKE = "PLATFORM=${OPTEEMACHINE}" EXTRA_OEMAKE += "CROSS_COMPILE_core=${HOST_PREFIX}" EXTRA_OEMAKE += "CROSS_COMPILE_ta_arm64=${HOST_PREFIX}" EXTRA_OEMAKE += "${@bb.utils.contains('TUNE_FEATURES', 'aarch64', 'CFG_ARM64_core=y ta-targets=ta_arm64', 'CFG_ARM32_core=y CROSS_COMPILE_ta_arm32=${HOST_PREFIX}', d)}" EXTRA_OEMAKE += "NOWERROR=1" EXTRA_OEMAKE += "LDFLAGS=" -EXTRA_OEMAKE += "LIBGCC_LOCATE_CFLAGS=--sysroot=${STAGING_DIR_TARGET}" -EXTRA_OEMAKE += "${@bb.utils.contains('MACHINE_FEATURES', 'fip', '', 'CFG_STM32MP15x_STM32IMAGE=y', d)}" # debug and trace -EXTRA_OEMAKE += "${@bb.utils.contains('ST_OPTEE_DEBUG_TRACE', '1', 'CFG_TEE_CORE_LOG_LEVEL=${ST_OPTEE_DEBUG_LOG_LEVEL} CFG_TEE_CORE_DEBUG=n', '', d)}" +EXTRA_OEMAKE += "${@bb.utils.contains('ST_OPTEE_DEBUG_TRACE', '1', 'CFG_TEE_CORE_LOG_LEVEL=${ST_OPTEE_DEBUG_LOG_LEVEL} CFG_TEE_CORE_DEBUG=${ST_OPTEE_CORE_DEBUG}', '', d)}" OPTEE_ARCH:armv7a = "arm32" OPTEE_ARCH:armv7ve = "arm32" @@ -77,7 +79,7 @@ do_install() { OPTEE_HEADER = "tee-header_v2" OPTEE_PAGEABLE = "tee-pageable_v2" OPTEE_PAGER = "tee-pager_v2" -OPTEE_SUFFIX = "${@bb.utils.contains('MACHINE_FEATURES','fip','bin','stm32',d)}" +OPTEE_SUFFIX = "bin" # Output the ELF generated ELF_DEBUG_ENABLE ?= "" OPTEE_ELF = "tee" diff --git a/recipes-security/optee/optee-os-stm32mp_3.12.0.bb b/recipes-security/optee/optee-os-stm32mp_3.16.0.bb similarity index 79% rename from recipes-security/optee/optee-os-stm32mp_3.12.0.bb rename to recipes-security/optee/optee-os-stm32mp_3.16.0.bb index a3ce23c..53cbbdb 100644 --- a/recipes-security/optee/optee-os-stm32mp_3.12.0.bb +++ b/recipes-security/optee/optee-os-stm32mp_3.16.0.bb @@ -2,18 +2,19 @@ SUMMARY = "OPTEE TA development kit for stm32mp" LICENSE = "BSD-2-Clause & BSD-3-Clause" LIC_FILES_CHKSUM = "file://LICENSE;md5=c1f21c4f72f372ef38a5a4aee55ec173" -SRC_URI = "git://github.com/OP-TEE/optee_os.git;protocol=https;name=os" -SRCREV = "3d47a131bca1d9ed511bfd516aa5e70269e12c1d" +SRC_URI = "git://github.com/OP-TEE/optee_os.git;protocol=https;branch=master;name=os" +SRCREV = "d0b742d1564834dac903f906168d7357063d5459" SRC_URI += " \ - file://0001-3.12.0-stm32mp-r1.patch \ - file://0002-3.12.0-stm32mp-r1.1-rc1.patch \ - file://0003-3.12.0-stm32mp-r2.patch \ + file://fonts.tar.gz;subdir=git;name=fonts \ + file://0001-3.16.0-stm32mp-r1.patch \ " -OPTEE_VERSION = "3.12.0" -OPTEE_SUBVERSION = "stm32mp" -OPTEE_RELEASE = "r2" +SRC_URI[fonts.sha256sum] = "4941e8bb6d8ac377838e27b214bf43008c496a24a8f897e0b06433988cbd53b2" + +OPTEE_VERSION = "3.16.0" +OPTEE_SUBVERSION = "stm32mp1" +OPTEE_RELEASE = "r1" PV = "${OPTEE_VERSION}-${OPTEE_SUBVERSION}-${OPTEE_RELEASE}" diff --git a/recipes-security/optee/optee-os/0001-3.12.0-stm32mp-r1.patch b/recipes-security/optee/optee-os/0001-3.12.0-stm32mp-r1.patch deleted file mode 100644 index e7a14f0..0000000 --- a/recipes-security/optee/optee-os/0001-3.12.0-stm32mp-r1.patch +++ /dev/null @@ -1,21255 +0,0 @@ -From c85389468d5ee9f06de67f10783bc015117caf67 Mon Sep 17 00:00:00 2001 -From: Romuald JEANNE -Date: Mon, 15 Mar 2021 17:27:07 +0100 -Subject: [PATCH] 3.12.0-stm32mp-r1 - -Signed-off-by: Romuald JEANNE ---- - CONTRIBUTING.md | 30 + - core/arch/arm/dts/stm32mp15-pinctrl.dtsi | 957 ++---------- - core/arch/arm/dts/stm32mp151.dtsi | 1368 +++-------------- - core/arch/arm/dts/stm32mp153.dtsi | 31 +- - core/arch/arm/dts/stm32mp157.dtsi | 24 - - core/arch/arm/dts/stm32mp157a-dk1.dts | 29 +- - core/arch/arm/dts/stm32mp157a-ed1.dts | 38 + - core/arch/arm/dts/stm32mp157a-ev1.dts | 24 + - core/arch/arm/dts/stm32mp157c-dk2.dts | 95 +- - core/arch/arm/dts/stm32mp157c-ed1.dts | 377 +---- - core/arch/arm/dts/stm32mp157c-ev1.dts | 349 +---- - core/arch/arm/dts/stm32mp157d-dk1.dts | 45 + - core/arch/arm/dts/stm32mp157d-ed1.dts | 39 + - core/arch/arm/dts/stm32mp157d-ev1.dts | 23 + - core/arch/arm/dts/stm32mp157f-dk2.dts | 51 + - core/arch/arm/dts/stm32mp157f-ed1.dts | 43 + - core/arch/arm/dts/stm32mp157f-ev1.dts | 23 + - core/arch/arm/dts/stm32mp15xa.dtsi | 13 + - core/arch/arm/dts/stm32mp15xc.dtsi | 3 + - core/arch/arm/dts/stm32mp15xd.dtsi | 19 + - core/arch/arm/dts/stm32mp15xf.dtsi | 21 + - core/arch/arm/dts/stm32mp15xx-dkx.dtsi | 708 ++++----- - core/arch/arm/dts/stm32mp15xx-edx.dtsi | 517 +++++++ - core/arch/arm/dts/stm32mp15xx-evx.dtsi | 73 + - core/arch/arm/dts/stm32mp15xxaa-pinctrl.dtsi | 1 + - core/arch/arm/dts/stm32mp15xxac-pinctrl.dtsi | 1 + - core/arch/arm/include/arm32.h | 11 +- - core/arch/arm/include/kernel/tlb_helpers.h | 9 +- - core/arch/arm/include/mm/core_mmu.h | 15 + - core/arch/arm/include/mm/pgt_cache.h | 5 +- - core/arch/arm/include/sm/pm.h | 4 + - core/arch/arm/mm/core_mmu.c | 35 +- - core/arch/arm/mm/mobj.c | 5 +- - core/arch/arm/mm/pgt_cache.c | 56 +- - core/arch/arm/plat-stm32mp1/boot_api.h | 2 + - core/arch/arm/plat-stm32mp1/conf.mk | 74 +- - .../plat-stm32mp1/drivers/stm32mp1_calib.c | 507 ++++++ - .../arm/plat-stm32mp1/drivers/stm32mp1_ddrc.c | 469 ++++++ - .../arm/plat-stm32mp1/drivers/stm32mp1_ddrc.h | 219 +++ - .../arm/plat-stm32mp1/drivers/stm32mp1_pmic.c | 50 +- - .../arm/plat-stm32mp1/drivers/stm32mp1_pmic.h | 8 +- - .../arm/plat-stm32mp1/drivers/stm32mp1_pwr.h | 33 +- - .../plat-stm32mp1/drivers/stm32mp1_syscfg.c | 14 +- - core/arch/arm/plat-stm32mp1/drivers/sub.mk | 3 +- - core/arch/arm/plat-stm32mp1/link.mk | 2 + - core/arch/arm/plat-stm32mp1/main.c | 354 ++++- - .../nsec-service/low_power_svc.c | 152 ++ - .../nsec-service/low_power_svc.h | 55 + - .../arm/plat-stm32mp1/nsec-service/pwr_svc.c | 89 ++ - .../arm/plat-stm32mp1/nsec-service/pwr_svc.h | 22 + - .../arm/plat-stm32mp1/nsec-service/rcc_svc.c | 139 ++ - .../arm/plat-stm32mp1/nsec-service/rcc_svc.h | 30 + - .../plat-stm32mp1/nsec-service/stm32mp1_smc.h | 196 ++- - .../nsec-service/stm32mp1_svc_setup.c | 79 +- - .../arm/plat-stm32mp1/nsec-service/sub.mk | 3 + - core/arch/arm/plat-stm32mp1/plat_tzc400.c | 52 +- - core/arch/arm/plat-stm32mp1/platform_config.h | 139 +- - core/arch/arm/plat-stm32mp1/pm/context.c | 522 +++++++ - core/arch/arm/plat-stm32mp1/pm/context.h | 102 ++ - .../plat-stm32mp1/pm/context_asm_defines.c | 28 + - core/arch/arm/plat-stm32mp1/pm/low_power.c | 626 ++++++++ - core/arch/arm/plat-stm32mp1/pm/pm_helpers.S | 729 +++++++++ - core/arch/arm/plat-stm32mp1/pm/power.h | 27 + - core/arch/arm/plat-stm32mp1/pm/power_config.c | 252 +++ - core/arch/arm/plat-stm32mp1/pm/psci.c | 234 ++- - core/arch/arm/plat-stm32mp1/pm/sub.mk | 6 + - core/arch/arm/plat-stm32mp1/remoteproc_pta.c | 528 +++++++ - core/arch/arm/plat-stm32mp1/reset.S | 28 +- - core/arch/arm/plat-stm32mp1/rproc_pub_key.h | 16 + - core/arch/arm/plat-stm32mp1/scmi_server.c | 96 +- - .../arch/arm/plat-stm32mp1/shared_resources.c | 156 +- - core/arch/arm/plat-stm32mp1/stm32_util.h | 77 +- - core/arch/arm/plat-stm32mp1/stm32mp_pm.h | 21 + - core/arch/arm/plat-stm32mp1/sub.mk | 9 + - core/arch/arm/sm/pm_a32.S | 83 +- - core/arch/arm/tee/entry_std.c | 2 +- - .../clk/clk-stm32mp15.c} | 1345 +++++++++++++++- - core/drivers/clk/clk.c | 58 + - core/drivers/clk/sub.mk | 2 + - core/drivers/gic.c | 225 ++- - core/drivers/scmi-msg/clock.c | 4 +- - core/drivers/stm32_bsec.c | 111 +- - core/drivers/stm32_etzpc.c | 47 + - core/drivers/stm32_gpio.c | 23 +- - core/drivers/stm32_i2c.c | 37 +- - core/drivers/stm32_iwdg.c | 314 ++++ - core/drivers/stm32_rng.c | 7 +- - core/drivers/stm32_rtc.c | 446 ++++++ - core/drivers/stm32_tim.c | 298 ++++ - core/drivers/sub.mk | 4 + - core/include/drivers/clk.h | 35 + - core/include/drivers/gic.h | 24 + - core/include/drivers/stm32_bsec.h | 16 + - core/include/drivers/stm32_iwdg.h | 17 + - core/include/drivers/stm32_rtc.h | 60 + - core/include/drivers/stm32_tim.h | 25 + - .../drivers/stm32mp1_rcc.h | 38 +- - .../dt-bindings/clock/stm32mp1-clksrc.h | 284 ++++ - .../dt-bindings/power/stm32mp1-power.h | 19 + - .../dt-bindings/soc/st,stm32-etzpc.h} | 35 +- - core/include/kernel/interrupt.h | 15 + - core/include/kernel/panic.h | 6 + - core/kernel/interrupt.c | 10 + - core/kernel/panic.c | 23 +- - core/mm/vm.c | 42 +- - core/sub.mk | 7 +- - keys/default_rproc.pem | 27 + - lib/libutee/include/remoteproc_pta.h | 157 ++ - mk/config.mk | 4 + - mk/gcc.mk | 6 +- - scripts/sign_rproc_fw.py | 416 +++++ - ta/remoteproc/Makefile | 18 + - ta/remoteproc/elf_parser.c | 186 +++ - ta/remoteproc/include/elf32.h | 243 +++ - ta/remoteproc/include/elf_common.h | 1014 ++++++++++++ - ta/remoteproc/include/elf_parser.h | 59 + - ta/remoteproc/include/ta_remoteproc.h | 65 + - .../include/user_ta_header_defines.h | 29 + - ta/remoteproc/remoteproc_core.c | 781 ++++++++++ - ta/remoteproc/sub.mk | 3 + - ta/remoteproc/user_ta.mk | 1 + - 121 files changed, 14243 insertions(+), 3618 deletions(-) - create mode 100644 CONTRIBUTING.md - create mode 100644 core/arch/arm/dts/stm32mp157a-ed1.dts - create mode 100644 core/arch/arm/dts/stm32mp157a-ev1.dts - create mode 100644 core/arch/arm/dts/stm32mp157d-dk1.dts - create mode 100644 core/arch/arm/dts/stm32mp157d-ed1.dts - create mode 100644 core/arch/arm/dts/stm32mp157d-ev1.dts - create mode 100644 core/arch/arm/dts/stm32mp157f-dk2.dts - create mode 100644 core/arch/arm/dts/stm32mp157f-ed1.dts - create mode 100644 core/arch/arm/dts/stm32mp157f-ev1.dts - create mode 100644 core/arch/arm/dts/stm32mp15xa.dtsi - create mode 100644 core/arch/arm/dts/stm32mp15xd.dtsi - create mode 100644 core/arch/arm/dts/stm32mp15xf.dtsi - create mode 100644 core/arch/arm/dts/stm32mp15xx-edx.dtsi - create mode 100644 core/arch/arm/dts/stm32mp15xx-evx.dtsi - create mode 100644 core/arch/arm/plat-stm32mp1/drivers/stm32mp1_calib.c - create mode 100644 core/arch/arm/plat-stm32mp1/drivers/stm32mp1_ddrc.c - create mode 100644 core/arch/arm/plat-stm32mp1/drivers/stm32mp1_ddrc.h - create mode 100644 core/arch/arm/plat-stm32mp1/nsec-service/low_power_svc.c - create mode 100644 core/arch/arm/plat-stm32mp1/nsec-service/low_power_svc.h - create mode 100644 core/arch/arm/plat-stm32mp1/nsec-service/pwr_svc.c - create mode 100644 core/arch/arm/plat-stm32mp1/nsec-service/pwr_svc.h - create mode 100644 core/arch/arm/plat-stm32mp1/nsec-service/rcc_svc.c - create mode 100644 core/arch/arm/plat-stm32mp1/nsec-service/rcc_svc.h - create mode 100644 core/arch/arm/plat-stm32mp1/pm/context.c - create mode 100644 core/arch/arm/plat-stm32mp1/pm/context.h - create mode 100644 core/arch/arm/plat-stm32mp1/pm/context_asm_defines.c - create mode 100644 core/arch/arm/plat-stm32mp1/pm/low_power.c - create mode 100644 core/arch/arm/plat-stm32mp1/pm/pm_helpers.S - create mode 100644 core/arch/arm/plat-stm32mp1/pm/power.h - create mode 100644 core/arch/arm/plat-stm32mp1/pm/power_config.c - create mode 100644 core/arch/arm/plat-stm32mp1/remoteproc_pta.c - create mode 100644 core/arch/arm/plat-stm32mp1/rproc_pub_key.h - create mode 100644 core/arch/arm/plat-stm32mp1/stm32mp_pm.h - rename core/{arch/arm/plat-stm32mp1/drivers/stm32mp1_clk.c => drivers/clk/clk-stm32mp15.c} (50%) - create mode 100644 core/drivers/clk/clk.c - create mode 100644 core/drivers/clk/sub.mk - create mode 100644 core/drivers/stm32_iwdg.c - create mode 100644 core/drivers/stm32_rtc.c - create mode 100644 core/drivers/stm32_tim.c - create mode 100644 core/include/drivers/clk.h - create mode 100644 core/include/drivers/stm32_iwdg.h - create mode 100644 core/include/drivers/stm32_rtc.h - create mode 100644 core/include/drivers/stm32_tim.h - rename core/{arch/arm/plat-stm32mp1 => include}/drivers/stm32mp1_rcc.h (95%) - create mode 100644 core/include/dt-bindings/clock/stm32mp1-clksrc.h - create mode 100644 core/include/dt-bindings/power/stm32mp1-power.h - rename core/{arch/arm/plat-stm32mp1/drivers/stm32mp1_etzpc.h => include/dt-bindings/soc/st,stm32-etzpc.h} (83%) - create mode 100644 keys/default_rproc.pem - create mode 100644 lib/libutee/include/remoteproc_pta.h - create mode 100755 scripts/sign_rproc_fw.py - create mode 100644 ta/remoteproc/Makefile - create mode 100644 ta/remoteproc/elf_parser.c - create mode 100644 ta/remoteproc/include/elf32.h - create mode 100644 ta/remoteproc/include/elf_common.h - create mode 100644 ta/remoteproc/include/elf_parser.h - create mode 100644 ta/remoteproc/include/ta_remoteproc.h - create mode 100644 ta/remoteproc/include/user_ta_header_defines.h - create mode 100644 ta/remoteproc/remoteproc_core.c - create mode 100644 ta/remoteproc/sub.mk - create mode 100644 ta/remoteproc/user_ta.mk - -diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md -new file mode 100644 -index 000000000..3d1bacd78 ---- /dev/null -+++ b/CONTRIBUTING.md -@@ -0,0 +1,30 @@ -+# Contributing guide -+ -+This document serves as a checklist before contributing to this repository. It includes links to read up on if topics are unclear to you. -+ -+This guide mainly focuses on the proper use of Git. -+ -+## 1. Issues -+ -+STM32MPU projects do not activate "Github issues" feature for the time being. If you need to report an issue or question about this project deliverables, you can report them using [ ST Support Center ](https://my.st.com/ols#/ols/newrequest) or [ ST Community MPU Forum ](https://community.st.com/s/topic/0TO0X0000003u2AWAQ/stm32-mpus). -+ -+## 2. Pull Requests -+ -+STMicrolectronics is happy to receive contributions from the community, based on an initial Contributor License Agreement (CLA) procedure. -+ -+* If you are an individual writing original source code and you are sure **you own the intellectual property**, then you need to sign an Individual CLA (https://cla.st.com). -+* If you work for a company that wants also to allow you to contribute with your work, your company needs to provide a Corporate CLA (https://cla.st.com) mentioning your GitHub account name. -+* If you are not sure that a CLA (Individual or Corporate) has been signed for your GitHub account you can check here (https://cla.st.com). -+ -+Please note that: -+* The Corporate CLA will always take precedence over the Individual CLA. -+* One CLA submission is sufficient, for any project proposed by STMicroelectronics. -+ -+__How to proceed__ -+ -+* We recommend to fork the project in your GitHub account to further develop your contribution. Please use the latest commit version. -+* Please, submit one Pull Request for one new feature or proposal. This will ease the analysis and final merge if accepted. -+ -+__Note__ -+ -+Merge will not be done directly in GitHub but it will need first to follow internal integration process before public deliver in a standard release. The Pull request will stay open until it is merged and delivered. -diff --git a/core/arch/arm/dts/stm32mp15-pinctrl.dtsi b/core/arch/arm/dts/stm32mp15-pinctrl.dtsi -index 0237d4dda..16c40cbff 100644 ---- a/core/arch/arm/dts/stm32mp15-pinctrl.dtsi -+++ b/core/arch/arm/dts/stm32mp15-pinctrl.dtsi -@@ -6,162 +6,6 @@ - #include - - &pinctrl { -- adc1_in6_pins_a: adc1-in6 { -- pins { -- pinmux = ; -- }; -- }; -- -- adc12_ain_pins_a: adc12-ain-0 { -- pins { -- pinmux = , /* ADC1 in13 */ -- , /* ADC1 in6 */ -- , /* ADC2 in2 */ -- ; /* ADC2 in6 */ -- }; -- }; -- -- adc12_usb_cc_pins_a: adc12-usb-cc-pins-0 { -- pins { -- pinmux = , /* ADC12 in18 */ -- ; /* ADC12 in19 */ -- }; -- }; -- -- cec_pins_a: cec-0 { -- pins { -- pinmux = ; -- bias-disable; -- drive-open-drain; -- slew-rate = <0>; -- }; -- }; -- -- cec_pins_sleep_a: cec-sleep-0 { -- pins { -- pinmux = ; /* HDMI_CEC */ -- }; -- }; -- -- cec_pins_b: cec-1 { -- pins { -- pinmux = ; -- bias-disable; -- drive-open-drain; -- slew-rate = <0>; -- }; -- }; -- -- cec_pins_sleep_b: cec-sleep-1 { -- pins { -- pinmux = ; /* HDMI_CEC */ -- }; -- }; -- -- dac_ch1_pins_a: dac-ch1 { -- pins { -- pinmux = ; -- }; -- }; -- -- dac_ch2_pins_a: dac-ch2 { -- pins { -- pinmux = ; -- }; -- }; -- -- dcmi_pins_a: dcmi-0 { -- pins { -- pinmux = ,/* DCMI_HSYNC */ -- ,/* DCMI_VSYNC */ -- ,/* DCMI_PIXCLK */ -- ,/* DCMI_D0 */ -- ,/* DCMI_D1 */ -- ,/* DCMI_D2 */ -- ,/* DCMI_D3 */ -- ,/* DCMI_D4 */ -- ,/* DCMI_D5 */ -- ,/* DCMI_D6 */ -- ,/* DCMI_D7 */ -- ,/* DCMI_D8 */ -- ,/* DCMI_D9 */ -- ,/* DCMI_D10 */ -- ;/* DCMI_D11 */ -- bias-disable; -- }; -- }; -- -- dcmi_sleep_pins_a: dcmi-sleep-0 { -- pins { -- pinmux = ,/* DCMI_HSYNC */ -- ,/* DCMI_VSYNC */ -- ,/* DCMI_PIXCLK */ -- ,/* DCMI_D0 */ -- ,/* DCMI_D1 */ -- ,/* DCMI_D2 */ -- ,/* DCMI_D3 */ -- ,/* DCMI_D4 */ -- ,/* DCMI_D5 */ -- ,/* DCMI_D6 */ -- ,/* DCMI_D7 */ -- ,/* DCMI_D8 */ -- ,/* DCMI_D9 */ -- ,/* DCMI_D10 */ -- ;/* DCMI_D11 */ -- }; -- }; -- -- ethernet0_rgmii_pins_a: rgmii-0 { -- pins1 { -- pinmux = , /* ETH_RGMII_CLK125 */ -- , /* ETH_RGMII_GTX_CLK */ -- , /* ETH_RGMII_TXD0 */ -- , /* ETH_RGMII_TXD1 */ -- , /* ETH_RGMII_TXD2 */ -- , /* ETH_RGMII_TXD3 */ -- , /* ETH_RGMII_TX_CTL */ -- ; /* ETH_MDC */ -- bias-disable; -- drive-push-pull; -- slew-rate = <2>; -- }; -- pins2 { -- pinmux = ; /* ETH_MDIO */ -- bias-disable; -- drive-push-pull; -- slew-rate = <0>; -- }; -- pins3 { -- pinmux = , /* ETH_RGMII_RXD0 */ -- , /* ETH_RGMII_RXD1 */ -- , /* ETH_RGMII_RXD2 */ -- , /* ETH_RGMII_RXD3 */ -- , /* ETH_RGMII_RX_CLK */ -- ; /* ETH_RGMII_RX_CTL */ -- bias-disable; -- }; -- }; -- -- ethernet0_rgmii_pins_sleep_a: rgmii-sleep-0 { -- pins1 { -- pinmux = , /* ETH_RGMII_CLK125 */ -- , /* ETH_RGMII_GTX_CLK */ -- , /* ETH_RGMII_TXD0 */ -- , /* ETH_RGMII_TXD1 */ -- , /* ETH_RGMII_TXD2 */ -- , /* ETH_RGMII_TXD3 */ -- , /* ETH_RGMII_TX_CTL */ -- , /* ETH_MDIO */ -- , /* ETH_MDC */ -- , /* ETH_RGMII_RXD0 */ -- , /* ETH_RGMII_RXD1 */ -- , /* ETH_RGMII_RXD2 */ -- , /* ETH_RGMII_RXD3 */ -- , /* ETH_RGMII_RX_CLK */ -- ; /* ETH_RGMII_RX_CTL */ -- }; -- }; -- - fmc_pins_a: fmc-0 { - pins1 { - pinmux = , /* FMC_NOE */ -@@ -187,412 +31,6 @@ - }; - }; - -- fmc_sleep_pins_a: fmc-sleep-0 { -- pins { -- pinmux = , /* FMC_NOE */ -- , /* FMC_NWE */ -- , /* FMC_A16_FMC_CLE */ -- , /* FMC_A17_FMC_ALE */ -- , /* FMC_D0 */ -- , /* FMC_D1 */ -- , /* FMC_D2 */ -- , /* FMC_D3 */ -- , /* FMC_D4 */ -- , /* FMC_D5 */ -- , /* FMC_D6 */ -- , /* FMC_D7 */ -- , /* FMC_NWAIT */ -- ; /* FMC_NE2_FMC_NCE */ -- }; -- }; -- -- i2c1_pins_a: i2c1-0 { -- pins { -- pinmux = , /* I2C1_SCL */ -- ; /* I2C1_SDA */ -- bias-disable; -- drive-open-drain; -- slew-rate = <0>; -- }; -- }; -- -- i2c1_pins_sleep_a: i2c1-1 { -- pins { -- pinmux = , /* I2C1_SCL */ -- ; /* I2C1_SDA */ -- }; -- }; -- -- i2c1_pins_b: i2c1-2 { -- pins { -- pinmux = , /* I2C1_SCL */ -- ; /* I2C1_SDA */ -- bias-disable; -- drive-open-drain; -- slew-rate = <0>; -- }; -- }; -- -- i2c1_pins_sleep_b: i2c1-3 { -- pins { -- pinmux = , /* I2C1_SCL */ -- ; /* I2C1_SDA */ -- }; -- }; -- -- i2c2_pins_a: i2c2-0 { -- pins { -- pinmux = , /* I2C2_SCL */ -- ; /* I2C2_SDA */ -- bias-disable; -- drive-open-drain; -- slew-rate = <0>; -- }; -- }; -- -- i2c2_pins_sleep_a: i2c2-1 { -- pins { -- pinmux = , /* I2C2_SCL */ -- ; /* I2C2_SDA */ -- }; -- }; -- -- i2c2_pins_b1: i2c2-2 { -- pins { -- pinmux = ; /* I2C2_SDA */ -- bias-disable; -- drive-open-drain; -- slew-rate = <0>; -- }; -- }; -- -- i2c2_pins_sleep_b1: i2c2-3 { -- pins { -- pinmux = ; /* I2C2_SDA */ -- }; -- }; -- -- i2c5_pins_a: i2c5-0 { -- pins { -- pinmux = , /* I2C5_SCL */ -- ; /* I2C5_SDA */ -- bias-disable; -- drive-open-drain; -- slew-rate = <0>; -- }; -- }; -- -- i2c5_pins_sleep_a: i2c5-1 { -- pins { -- pinmux = , /* I2C5_SCL */ -- ; /* I2C5_SDA */ -- -- }; -- }; -- -- i2s2_pins_a: i2s2-0 { -- pins { -- pinmux = , /* I2S2_SDO */ -- , /* I2S2_WS */ -- ; /* I2S2_CK */ -- slew-rate = <1>; -- drive-push-pull; -- bias-disable; -- }; -- }; -- -- i2s2_pins_sleep_a: i2s2-1 { -- pins { -- pinmux = , /* I2S2_SDO */ -- , /* I2S2_WS */ -- ; /* I2S2_CK */ -- }; -- }; -- -- ltdc_pins_a: ltdc-a-0 { -- pins { -- pinmux = , /* LCD_CLK */ -- , /* LCD_HSYNC */ -- , /* LCD_VSYNC */ -- , /* LCD_DE */ -- , /* LCD_R0 */ -- , /* LCD_R1 */ -- , /* LCD_R2 */ -- , /* LCD_R3 */ -- , /* LCD_R4 */ -- , /* LCD_R5 */ -- , /* LCD_R6 */ -- , /* LCD_R7 */ -- , /* LCD_G0 */ -- , /* LCD_G1 */ -- , /* LCD_G2 */ -- , /* LCD_G3 */ -- , /* LCD_G4 */ -- , /* LCD_G5 */ -- , /* LCD_G6 */ -- , /* LCD_G7 */ -- , /* LCD_B0 */ -- , /* LCD_B1 */ -- , /* LCD_B2 */ -- , /* LCD_B3 */ -- , /* LCD_B4 */ -- , /* LCD_B5 */ -- , /* LCD_B6 */ -- ; /* LCD_B7 */ -- bias-disable; -- drive-push-pull; -- slew-rate = <1>; -- }; -- }; -- -- ltdc_pins_sleep_a: ltdc-a-1 { -- pins { -- pinmux = , /* LCD_CLK */ -- , /* LCD_HSYNC */ -- , /* LCD_VSYNC */ -- , /* LCD_DE */ -- , /* LCD_R0 */ -- , /* LCD_R1 */ -- , /* LCD_R2 */ -- , /* LCD_R3 */ -- , /* LCD_R4 */ -- , /* LCD_R5 */ -- , /* LCD_R6 */ -- , /* LCD_R7 */ -- , /* LCD_G0 */ -- , /* LCD_G1 */ -- , /* LCD_G2 */ -- , /* LCD_G3 */ -- , /* LCD_G4 */ -- , /* LCD_G5 */ -- , /* LCD_G6 */ -- , /* LCD_G7 */ -- , /* LCD_B0 */ -- , /* LCD_B1 */ -- , /* LCD_B2 */ -- , /* LCD_B3 */ -- , /* LCD_B4 */ -- , /* LCD_B5 */ -- , /* LCD_B6 */ -- ; /* LCD_B7 */ -- }; -- }; -- -- ltdc_pins_b: ltdc-b-0 { -- pins { -- pinmux = , /* LCD_CLK */ -- , /* LCD_HSYNC */ -- , /* LCD_VSYNC */ -- , /* LCD_DE */ -- , /* LCD_R0 */ -- , /* LCD_R1 */ -- , /* LCD_R2 */ -- , /* LCD_R3 */ -- , /* LCD_R4 */ -- , /* LCD_R5 */ -- , /* LCD_R6 */ -- , /* LCD_R7 */ -- , /* LCD_G0 */ -- , /* LCD_G1 */ -- , /* LCD_G2 */ -- , /* LCD_G3 */ -- , /* LCD_G4 */ -- , /* LCD_G5 */ -- , /* LCD_G6 */ -- , /* LCD_G7 */ -- , /* LCD_B0 */ -- , /* LCD_B1 */ -- , /* LCD_B2 */ -- , /* LCD_B3 */ -- , /* LCD_B4 */ -- , /* LCD_B5 */ -- , /* LCD_B6 */ -- ; /* LCD_B7 */ -- bias-disable; -- drive-push-pull; -- slew-rate = <1>; -- }; -- }; -- -- ltdc_pins_sleep_b: ltdc-b-1 { -- pins { -- pinmux = , /* LCD_CLK */ -- , /* LCD_HSYNC */ -- , /* LCD_VSYNC */ -- , /* LCD_DE */ -- , /* LCD_R0 */ -- , /* LCD_R1 */ -- , /* LCD_R2 */ -- , /* LCD_R3 */ -- , /* LCD_R4 */ -- , /* LCD_R5 */ -- , /* LCD_R6 */ -- , /* LCD_R7 */ -- , /* LCD_G0 */ -- , /* LCD_G1 */ -- , /* LCD_G2 */ -- , /* LCD_G3 */ -- , /* LCD_G4 */ -- , /* LCD_G5 */ -- , /* LCD_G6 */ -- , /* LCD_G7 */ -- , /* LCD_B0 */ -- , /* LCD_B1 */ -- , /* LCD_B2 */ -- , /* LCD_B3 */ -- , /* LCD_B4 */ -- , /* LCD_B5 */ -- , /* LCD_B6 */ -- ; /* LCD_B7 */ -- }; -- }; -- -- m_can1_pins_a: m-can1-0 { -- pins1 { -- pinmux = ; /* CAN1_TX */ -- slew-rate = <1>; -- drive-push-pull; -- bias-disable; -- }; -- pins2 { -- pinmux = ; /* CAN1_RX */ -- bias-disable; -- }; -- }; -- -- m_can1_sleep_pins_a: m_can1-sleep-0 { -- pins { -- pinmux = , /* CAN1_TX */ -- ; /* CAN1_RX */ -- }; -- }; -- -- pwm1_pins_a: pwm1-0 { -- pins { -- pinmux = , /* TIM1_CH1 */ -- , /* TIM1_CH2 */ -- ; /* TIM1_CH4 */ -- bias-pull-down; -- drive-push-pull; -- slew-rate = <0>; -- }; -- }; -- -- pwm1_sleep_pins_a: pwm1-sleep-0 { -- pins { -- pinmux = , /* TIM1_CH1 */ -- , /* TIM1_CH2 */ -- ; /* TIM1_CH4 */ -- }; -- }; -- -- pwm2_pins_a: pwm2-0 { -- pins { -- pinmux = ; /* TIM2_CH4 */ -- bias-pull-down; -- drive-push-pull; -- slew-rate = <0>; -- }; -- }; -- -- pwm2_sleep_pins_a: pwm2-sleep-0 { -- pins { -- pinmux = ; /* TIM2_CH4 */ -- }; -- }; -- -- pwm3_pins_a: pwm3-0 { -- pins { -- pinmux = ; /* TIM3_CH2 */ -- bias-pull-down; -- drive-push-pull; -- slew-rate = <0>; -- }; -- }; -- -- pwm3_sleep_pins_a: pwm3-sleep-0 { -- pins { -- pinmux = ; /* TIM3_CH2 */ -- }; -- }; -- -- pwm4_pins_a: pwm4-0 { -- pins { -- pinmux = , /* TIM4_CH3 */ -- ; /* TIM4_CH4 */ -- bias-pull-down; -- drive-push-pull; -- slew-rate = <0>; -- }; -- }; -- -- pwm4_sleep_pins_a: pwm4-sleep-0 { -- pins { -- pinmux = , /* TIM4_CH3 */ -- ; /* TIM4_CH4 */ -- }; -- }; -- -- pwm4_pins_b: pwm4-1 { -- pins { -- pinmux = ; /* TIM4_CH2 */ -- bias-pull-down; -- drive-push-pull; -- slew-rate = <0>; -- }; -- }; -- -- pwm4_sleep_pins_b: pwm4-sleep-1 { -- pins { -- pinmux = ; /* TIM4_CH2 */ -- }; -- }; -- -- pwm5_pins_a: pwm5-0 { -- pins { -- pinmux = ; /* TIM5_CH2 */ -- bias-pull-down; -- drive-push-pull; -- slew-rate = <0>; -- }; -- }; -- -- pwm5_sleep_pins_a: pwm5-sleep-0 { -- pins { -- pinmux = ; /* TIM5_CH2 */ -- }; -- }; -- -- pwm8_pins_a: pwm8-0 { -- pins { -- pinmux = ; /* TIM8_CH4 */ -- bias-pull-down; -- drive-push-pull; -- slew-rate = <0>; -- }; -- }; -- -- pwm8_sleep_pins_a: pwm8-sleep-0 { -- pins { -- pinmux = ; /* TIM8_CH4 */ -- }; -- }; -- -- pwm12_pins_a: pwm12-0 { -- pins { -- pinmux = ; /* TIM12_CH1 */ -- bias-pull-down; -- drive-push-pull; -- slew-rate = <0>; -- }; -- }; -- -- pwm12_sleep_pins_a: pwm12-sleep-0 { -- pins { -- pinmux = ; /* TIM12_CH1 */ -- }; -- }; -- - qspi_clk_pins_a: qspi-clk-0 { - pins { - pinmux = ; /* QSPI_CLK */ -@@ -602,12 +40,6 @@ - }; - }; - -- qspi_clk_sleep_pins_a: qspi-clk-sleep-0 { -- pins { -- pinmux = ; /* QSPI_CLK */ -- }; -- }; -- - qspi_bk1_pins_a: qspi-bk1-0 { - pins1 { - pinmux = , /* QSPI_BK1_IO0 */ -@@ -626,16 +58,6 @@ - }; - }; - -- qspi_bk1_sleep_pins_a: qspi-bk1-sleep-0 { -- pins { -- pinmux = , /* QSPI_BK1_IO0 */ -- , /* QSPI_BK1_IO1 */ -- , /* QSPI_BK1_IO2 */ -- , /* QSPI_BK1_IO3 */ -- ; /* QSPI_BK1_NCS */ -- }; -- }; -- - qspi_bk2_pins_a: qspi-bk2-0 { - pins1 { - pinmux = , /* QSPI_BK2_IO0 */ -@@ -654,86 +76,9 @@ - }; - }; - -- qspi_bk2_sleep_pins_a: qspi-bk2-sleep-0 { -+ rtc_out2_rmp_pins_a: rtc-out2-rmp-pins-0 { - pins { -- pinmux = , /* QSPI_BK2_IO0 */ -- , /* QSPI_BK2_IO1 */ -- , /* QSPI_BK2_IO2 */ -- , /* QSPI_BK2_IO3 */ -- ; /* QSPI_BK2_NCS */ -- }; -- }; -- -- sai2a_pins_a: sai2a-0 { -- pins { -- pinmux = , /* SAI2_SCK_A */ -- , /* SAI2_SD_A */ -- , /* SAI2_FS_A */ -- ; /* SAI2_MCLK_A */ -- slew-rate = <0>; -- drive-push-pull; -- bias-disable; -- }; -- }; -- -- sai2a_sleep_pins_a: sai2a-1 { -- pins { -- pinmux = , /* SAI2_SCK_A */ -- , /* SAI2_SD_A */ -- , /* SAI2_FS_A */ -- ; /* SAI2_MCLK_A */ -- }; -- }; -- -- sai2b_pins_a: sai2b-0 { -- pins1 { -- pinmux = , /* SAI2_SCK_B */ -- , /* SAI2_FS_B */ -- ; /* SAI2_MCLK_B */ -- slew-rate = <0>; -- drive-push-pull; -- bias-disable; -- }; -- pins2 { -- pinmux = ; /* SAI2_SD_B */ -- bias-disable; -- }; -- }; -- -- sai2b_sleep_pins_a: sai2b-1 { -- pins { -- pinmux = , /* SAI2_SD_B */ -- , /* SAI2_SCK_B */ -- , /* SAI2_FS_B */ -- ; /* SAI2_MCLK_B */ -- }; -- }; -- -- sai2b_pins_b: sai2b-2 { -- pins { -- pinmux = ; /* SAI2_SD_B */ -- bias-disable; -- }; -- }; -- -- sai2b_sleep_pins_b: sai2b-3 { -- pins { -- pinmux = ; /* SAI2_SD_B */ -- }; -- }; -- -- sai4a_pins_a: sai4a-0 { -- pins { -- pinmux = ; /* SAI4_SD_A */ -- slew-rate = <0>; -- drive-push-pull; -- bias-disable; -- }; -- }; -- -- sai4a_sleep_pins_a: sai4a-1 { -- pins { -- pinmux = ; /* SAI4_SD_A */ -+ pinmux = ; /* RTC_OUT2_RMP */ - }; - }; - -@@ -756,41 +101,6 @@ - }; - }; - -- sdmmc1_b4_od_pins_a: sdmmc1-b4-od-0 { -- pins1 { -- pinmux = , /* SDMMC1_D0 */ -- , /* SDMMC1_D1 */ -- , /* SDMMC1_D2 */ -- ; /* SDMMC1_D3 */ -- slew-rate = <1>; -- drive-push-pull; -- bias-disable; -- }; -- pins2 { -- pinmux = ; /* SDMMC1_CK */ -- slew-rate = <2>; -- drive-push-pull; -- bias-disable; -- }; -- pins3 { -- pinmux = ; /* SDMMC1_CMD */ -- slew-rate = <1>; -- drive-open-drain; -- bias-disable; -- }; -- }; -- -- sdmmc1_b4_sleep_pins_a: sdmmc1-b4-sleep-0 { -- pins { -- pinmux = , /* SDMMC1_D0 */ -- , /* SDMMC1_D1 */ -- , /* SDMMC1_D2 */ -- , /* SDMMC1_D3 */ -- , /* SDMMC1_CK */ -- ; /* SDMMC1_CMD */ -- }; -- }; -- - sdmmc1_dir_pins_a: sdmmc1-dir-0 { - pins1 { - pinmux = , /* SDMMC1_D0DIR */ -@@ -806,15 +116,6 @@ - }; - }; - -- sdmmc1_dir_sleep_pins_a: sdmmc1-dir-sleep-0 { -- pins { -- pinmux = , /* SDMMC1_D0DIR */ -- , /* SDMMC1_D123DIR */ -- , /* SDMMC1_CDIR */ -- ; /* SDMMC1_CKIN */ -- }; -- }; -- - sdmmc2_b4_pins_a: sdmmc2-b4-0 { - pins1 { - pinmux = , /* SDMMC2_D0 */ -@@ -834,230 +135,220 @@ - }; - }; - -- sdmmc2_b4_od_pins_a: sdmmc2-b4-od-0 { -+ sdmmc2_b4_pins_b: sdmmc2-b4-1 { - pins1 { - pinmux = , /* SDMMC2_D0 */ - , /* SDMMC2_D1 */ - , /* SDMMC2_D2 */ -- ; /* SDMMC2_D3 */ -+ , /* SDMMC2_D3 */ -+ ; /* SDMMC2_CMD */ - slew-rate = <1>; - drive-push-pull; -- bias-pull-up; -+ bias-disable; - }; - pins2 { - pinmux = ; /* SDMMC2_CK */ - slew-rate = <2>; - drive-push-pull; -- bias-pull-up; -- }; -- pins3 { -- pinmux = ; /* SDMMC2_CMD */ -- slew-rate = <1>; -- drive-open-drain; -- bias-pull-up; -+ bias-disable; - }; - }; - -- sdmmc2_b4_sleep_pins_a: sdmmc2-b4-sleep-0 { -+ sdmmc2_d47_pins_a: sdmmc2-d47-0 { - pins { -- pinmux = , /* SDMMC2_D0 */ -- , /* SDMMC2_D1 */ -- , /* SDMMC2_D2 */ -- , /* SDMMC2_D3 */ -- , /* SDMMC2_CK */ -- ; /* SDMMC2_CMD */ -+ pinmux = , /* SDMMC2_D4 */ -+ , /* SDMMC2_D5 */ -+ , /* SDMMC2_D6 */ -+ ; /* SDMMC2_D7 */ -+ slew-rate = <1>; -+ drive-push-pull; -+ bias-pull-up; - }; - }; - -- sdmmc2_b4_pins_b: sdmmc2-b4-1 { -+ uart4_pins_a: uart4-0 { - pins1 { -- pinmux = , /* SDMMC2_D0 */ -- , /* SDMMC2_D1 */ -- , /* SDMMC2_D2 */ -- , /* SDMMC2_D3 */ -- ; /* SDMMC2_CMD */ -- slew-rate = <1>; -- drive-push-pull; -+ pinmux = ; /* UART4_TX */ - bias-disable; -+ drive-push-pull; -+ slew-rate = <0>; - }; - pins2 { -- pinmux = ; /* SDMMC2_CK */ -- slew-rate = <2>; -- drive-push-pull; -+ pinmux = ; /* UART4_RX */ - bias-disable; - }; - }; - -- sdmmc2_b4_od_pins_b: sdmmc2-b4-od-1 { -+ uart4_pins_b: uart4-1 { - pins1 { -- pinmux = , /* SDMMC2_D0 */ -- , /* SDMMC2_D1 */ -- , /* SDMMC2_D2 */ -- ; /* SDMMC2_D3 */ -- slew-rate = <1>; -- drive-push-pull; -+ pinmux = ; /* UART4_TX */ - bias-disable; -- }; -- pins2 { -- pinmux = ; /* SDMMC2_CK */ -- slew-rate = <2>; - drive-push-pull; -- bias-disable; -+ slew-rate = <0>; - }; -- pins3 { -- pinmux = ; /* SDMMC2_CMD */ -- slew-rate = <1>; -- drive-open-drain; -+ pins2 { -+ pinmux = ; /* UART4_RX */ - bias-disable; - }; - }; - -- sdmmc2_d47_pins_a: sdmmc2-d47-0 { -- pins { -- pinmux = , /* SDMMC2_D4 */ -- , /* SDMMC2_D5 */ -- , /* SDMMC2_D6 */ -- ; /* SDMMC2_D7 */ -- slew-rate = <1>; -+ uart7_pins_a: uart7-0 { -+ pins1 { -+ pinmux = ; /* UART7_TX */ -+ bias-disable; - drive-push-pull; -- bias-pull-up; -+ slew-rate = <0>; -+ }; -+ pins2 { -+ pinmux = , /* UART7_RX */ -+ , /* UART7_CTS */ -+ ; /* UART7_RTS */ -+ bias-disable; - }; - }; - -- sdmmc2_d47_sleep_pins_a: sdmmc2-d47-sleep-0 { -- pins { -- pinmux = , /* SDMMC2_D4 */ -- , /* SDMMC2_D5 */ -- , /* SDMMC2_D6 */ -- ; /* SDMMC2_D7 */ -+ uart7_pins_b: uart7-1 { -+ pins1 { -+ pinmux = ; /* UART7_TX */ -+ bias-disable; -+ drive-push-pull; -+ slew-rate = <0>; -+ }; -+ pins2 { -+ pinmux = ; /* UART7_RX */ -+ bias-disable; - }; - }; - -- sdmmc3_b4_pins_a: sdmmc3-b4-0 { -+ uart7_pins_c: uart7-2 { - pins1 { -- pinmux = , /* SDMMC3_D0 */ -- , /* SDMMC3_D1 */ -- , /* SDMMC3_D2 */ -- , /* SDMMC3_D3 */ -- ; /* SDMMC3_CMD */ -- slew-rate = <1>; -+ pinmux = ; /* UART7_TX */ -+ bias-disable; - drive-push-pull; -- bias-pull-up; -+ slew-rate = <0>; - }; - pins2 { -- pinmux = ; /* SDMMC3_CK */ -- slew-rate = <2>; -- drive-push-pull; -+ pinmux = ; /* UART7_RX */ - bias-pull-up; - }; - }; - -- sdmmc3_b4_od_pins_a: sdmmc3-b4-od-0 { -+ uart8_pins_a: uart8-0 { - pins1 { -- pinmux = , /* SDMMC3_D0 */ -- , /* SDMMC3_D1 */ -- , /* SDMMC3_D2 */ -- ; /* SDMMC3_D3 */ -- slew-rate = <1>; -+ pinmux = ; /* UART8_TX */ -+ bias-disable; - drive-push-pull; -- bias-pull-up; -+ slew-rate = <0>; - }; - pins2 { -- pinmux = ; /* SDMMC3_CK */ -- slew-rate = <2>; -- drive-push-pull; -- bias-pull-up; -- }; -- pins3 { -- pinmux = ; /* SDMMC2_CMD */ -- slew-rate = <1>; -- drive-open-drain; -- bias-pull-up; -+ pinmux = ; /* UART8_RX */ -+ bias-disable; - }; - }; - -- sdmmc3_b4_sleep_pins_a: sdmmc3-b4-sleep-0 { -- pins { -- pinmux = , /* SDMMC3_D0 */ -- , /* SDMMC3_D1 */ -- , /* SDMMC3_D2 */ -- , /* SDMMC3_D3 */ -- , /* SDMMC3_CK */ -- ; /* SDMMC3_CMD */ -+ usart2_pins_a: usart2-0 { -+ pins1 { -+ pinmux = , /* USART2_TX */ -+ ; /* USART2_RTS */ -+ bias-disable; -+ drive-push-pull; -+ slew-rate = <0>; -+ }; -+ pins2 { -+ pinmux = , /* USART2_RX */ -+ ; /* USART2_CTS_NSS */ -+ bias-disable; - }; - }; - -- spdifrx_pins_a: spdifrx-0 { -- pins { -- pinmux = ; /* SPDIF_IN1 */ -+ usart2_pins_b: usart2-1 { -+ pins1 { -+ pinmux = , /* USART2_TX */ -+ ; /* USART2_RTS */ -+ bias-disable; -+ drive-push-pull; -+ slew-rate = <0>; -+ }; -+ pins2 { -+ pinmux = , /* USART2_RX */ -+ ; /* USART2_CTS_NSS */ - bias-disable; - }; - }; - -- spdifrx_sleep_pins_a: spdifrx-1 { -- pins { -- pinmux = ; /* SPDIF_IN1 */ -+ usart2_pins_c: usart2-2 { -+ pins1 { -+ pinmux = , /* USART2_TX */ -+ ; /* USART2_RTS */ -+ bias-disable; -+ drive-push-pull; -+ slew-rate = <3>; -+ }; -+ pins2 { -+ pinmux = , /* USART2_RX */ -+ ; /* USART2_CTS_NSS */ -+ bias-disable; - }; - }; - -- uart4_pins_a: uart4-0 { -+ usart3_pins_a: usart3-0 { - pins1 { -- pinmux = ; /* UART4_TX */ -+ pinmux = ; /* USART3_TX */ - bias-disable; - drive-push-pull; - slew-rate = <0>; - }; - pins2 { -- pinmux = ; /* UART4_RX */ -+ pinmux = ; /* USART3_RX */ - bias-disable; - }; - }; - -- uart4_pins_b: uart4-1 { -+ usart3_pins_b: usart3-1 { - pins1 { -- pinmux = ; /* UART4_TX */ -+ pinmux = , /* USART3_TX */ -+ ; /* USART3_RTS */ - bias-disable; - drive-push-pull; - slew-rate = <0>; - }; - pins2 { -- pinmux = ; /* UART4_RX */ -+ pinmux = , /* USART3_RX */ -+ ; /* USART3_CTS_NSS */ - bias-disable; - }; - }; - -- uart7_pins_a: uart7-0 { -+ usart3_pins_c: usart3-2 { - pins1 { -- pinmux = ; /* UART4_TX */ -+ pinmux = , /* USART3_TX */ -+ ; /* USART3_RTS */ - bias-disable; - drive-push-pull; - slew-rate = <0>; - }; - pins2 { -- pinmux = , /* UART4_RX */ -- , /* UART4_CTS */ -- ; /* UART4_RTS */ -- bias-disable; -+ pinmux = , /* USART3_RX */ -+ ; /* USART3_CTS_NSS */ -+ bias-pull-up; - }; - }; --}; - --&pinctrl_z { -- i2c2_pins_b2: i2c2-0 { -+ usbotg_hs_pins_a: usbotg-hs-0 { - pins { -- pinmux = ; /* I2C2_SCL */ -- bias-disable; -- drive-open-drain; -- slew-rate = <0>; -+ pinmux = ; /* OTG_ID */ - }; - }; - -- i2c2_pins_sleep_b2: i2c2-1 { -+ usbotg_fs_dp_dm_pins_a: usbotg-fs-dp-dm-0 { - pins { -- pinmux = ; /* I2C2_SCL */ -+ pinmux = , /* OTG_FS_DM */ -+ ; /* OTG_FS_DP */ - }; - }; -+}; - -+&pinctrl_z { - i2c4_pins_a: i2c4-0 { - pins { - pinmux = , /* I2C4_SCL */ -@@ -1067,26 +358,4 @@ - slew-rate = <0>; - }; - }; -- -- i2c4_pins_sleep_a: i2c4-1 { -- pins { -- pinmux = , /* I2C4_SCL */ -- ; /* I2C4_SDA */ -- }; -- }; -- -- spi1_pins_a: spi1-0 { -- pins1 { -- pinmux = , /* SPI1_SCK */ -- ; /* SPI1_MOSI */ -- bias-disable; -- drive-push-pull; -- slew-rate = <1>; -- }; -- -- pins2 { -- pinmux = ; /* SPI1_MISO */ -- bias-disable; -- }; -- }; - }; -diff --git a/core/arch/arm/dts/stm32mp151.dtsi b/core/arch/arm/dts/stm32mp151.dtsi -index d8ac70292..714d94710 100644 ---- a/core/arch/arm/dts/stm32mp151.dtsi -+++ b/core/arch/arm/dts/stm32mp151.dtsi -@@ -19,9 +19,41 @@ - compatible = "arm,cortex-a7"; - device_type = "cpu"; - reg = <0>; -+ clocks = <&rcc CK_MPU>; -+ clock-names = "cpu"; -+ operating-points-v2 = <&cpu0_opp_table>; -+ nvmem-cells = <&part_number_otp>; -+ nvmem-cell-names = "part_number"; - }; - }; - -+ cpu0_opp_table: cpu0-opp-table { -+ compatible = "operating-points-v2"; -+ opp-shared; -+ }; -+ -+ nvmem_layout: nvmem_layout@0 { -+ compatible = "st,stm32-nvmem-layout"; -+ -+ nvmem-cells = <&cfg0_otp>, -+ <&part_number_otp>, -+ <&monotonic_otp>, -+ <&nand_otp>, -+ <&uid_otp>, -+ <&package_otp>, -+ <&hw2_otp>, -+ <&pkh_otp>; -+ -+ nvmem-cell-names = "cfg0_otp", -+ "part_number_otp", -+ "monotonic_otp", -+ "nand_otp", -+ "uid_otp", -+ "package_otp", -+ "hw2_otp", -+ "pkh_otp"; -+ }; -+ - psci { - compatible = "arm,psci-1.0"; - method = "smc"; -@@ -35,15 +67,6 @@ - <0xa0022000 0x2000>; - }; - -- timer { -- compatible = "arm,armv7-timer"; -- interrupts = , -- , -- , -- ; -- interrupt-parent = <&intc>; -- }; -- - clocks { - clk_hse: clk-hse { - #clock-cells = <0>; -@@ -76,37 +99,6 @@ - }; - }; - -- thermal-zones { -- cpu_thermal: cpu-thermal { -- polling-delay-passive = <0>; -- polling-delay = <0>; -- thermal-sensors = <&dts>; -- -- trips { -- cpu_alert1: cpu-alert1 { -- temperature = <85000>; -- hysteresis = <0>; -- type = "passive"; -- }; -- -- cpu-crit { -- temperature = <120000>; -- hysteresis = <0>; -- type = "critical"; -- }; -- }; -- -- cooling-maps { -- }; -- }; -- }; -- -- booster: regulator-booster { -- compatible = "st,stm32mp1-booster"; -- st,syscfg = <&syscfg>; -- status = "disabled"; -- }; -- - soc { - compatible = "simple-bus"; - #address-cells = <1>; -@@ -114,175 +106,6 @@ - interrupt-parent = <&intc>; - ranges; - -- timers2: timer@40000000 { -- #address-cells = <1>; -- #size-cells = <0>; -- compatible = "st,stm32-timers"; -- reg = <0x40000000 0x400>; -- clocks = <&rcc TIM2_K>; -- clock-names = "int"; -- dmas = <&dmamux1 18 0x400 0x1>, -- <&dmamux1 19 0x400 0x1>, -- <&dmamux1 20 0x400 0x1>, -- <&dmamux1 21 0x400 0x1>, -- <&dmamux1 22 0x400 0x1>; -- dma-names = "ch1", "ch2", "ch3", "ch4", "up"; -- status = "disabled"; -- -- pwm { -- compatible = "st,stm32-pwm"; -- #pwm-cells = <3>; -- status = "disabled"; -- }; -- -- timer@1 { -- compatible = "st,stm32h7-timer-trigger"; -- reg = <1>; -- status = "disabled"; -- }; -- -- counter { -- compatible = "st,stm32-timer-counter"; -- status = "disabled"; -- }; -- }; -- -- timers3: timer@40001000 { -- #address-cells = <1>; -- #size-cells = <0>; -- compatible = "st,stm32-timers"; -- reg = <0x40001000 0x400>; -- clocks = <&rcc TIM3_K>; -- clock-names = "int"; -- dmas = <&dmamux1 23 0x400 0x1>, -- <&dmamux1 24 0x400 0x1>, -- <&dmamux1 25 0x400 0x1>, -- <&dmamux1 26 0x400 0x1>, -- <&dmamux1 27 0x400 0x1>, -- <&dmamux1 28 0x400 0x1>; -- dma-names = "ch1", "ch2", "ch3", "ch4", "up", "trig"; -- status = "disabled"; -- -- pwm { -- compatible = "st,stm32-pwm"; -- #pwm-cells = <3>; -- status = "disabled"; -- }; -- -- timer@2 { -- compatible = "st,stm32h7-timer-trigger"; -- reg = <2>; -- status = "disabled"; -- }; -- -- counter { -- compatible = "st,stm32-timer-counter"; -- status = "disabled"; -- }; -- }; -- -- timers4: timer@40002000 { -- #address-cells = <1>; -- #size-cells = <0>; -- compatible = "st,stm32-timers"; -- reg = <0x40002000 0x400>; -- clocks = <&rcc TIM4_K>; -- clock-names = "int"; -- dmas = <&dmamux1 29 0x400 0x1>, -- <&dmamux1 30 0x400 0x1>, -- <&dmamux1 31 0x400 0x1>, -- <&dmamux1 32 0x400 0x1>; -- dma-names = "ch1", "ch2", "ch3", "ch4"; -- status = "disabled"; -- -- pwm { -- compatible = "st,stm32-pwm"; -- #pwm-cells = <3>; -- status = "disabled"; -- }; -- -- timer@3 { -- compatible = "st,stm32h7-timer-trigger"; -- reg = <3>; -- status = "disabled"; -- }; -- -- counter { -- compatible = "st,stm32-timer-counter"; -- status = "disabled"; -- }; -- }; -- -- timers5: timer@40003000 { -- #address-cells = <1>; -- #size-cells = <0>; -- compatible = "st,stm32-timers"; -- reg = <0x40003000 0x400>; -- clocks = <&rcc TIM5_K>; -- clock-names = "int"; -- dmas = <&dmamux1 55 0x400 0x1>, -- <&dmamux1 56 0x400 0x1>, -- <&dmamux1 57 0x400 0x1>, -- <&dmamux1 58 0x400 0x1>, -- <&dmamux1 59 0x400 0x1>, -- <&dmamux1 60 0x400 0x1>; -- dma-names = "ch1", "ch2", "ch3", "ch4", "up", "trig"; -- status = "disabled"; -- -- pwm { -- compatible = "st,stm32-pwm"; -- #pwm-cells = <3>; -- status = "disabled"; -- }; -- -- timer@4 { -- compatible = "st,stm32h7-timer-trigger"; -- reg = <4>; -- status = "disabled"; -- }; -- -- counter { -- compatible = "st,stm32-timer-counter"; -- status = "disabled"; -- }; -- }; -- -- timers6: timer@40004000 { -- #address-cells = <1>; -- #size-cells = <0>; -- compatible = "st,stm32-timers"; -- reg = <0x40004000 0x400>; -- clocks = <&rcc TIM6_K>; -- clock-names = "int"; -- dmas = <&dmamux1 69 0x400 0x1>; -- dma-names = "up"; -- status = "disabled"; -- -- timer@5 { -- compatible = "st,stm32h7-timer-trigger"; -- reg = <5>; -- status = "disabled"; -- }; -- }; -- -- timers7: timer@40005000 { -- #address-cells = <1>; -- #size-cells = <0>; -- compatible = "st,stm32-timers"; -- reg = <0x40005000 0x400>; -- clocks = <&rcc TIM7_K>; -- clock-names = "int"; -- dmas = <&dmamux1 70 0x400 0x1>; -- dma-names = "up"; -- status = "disabled"; -- -- timer@6 { -- compatible = "st,stm32h7-timer-trigger"; -- reg = <6>; -- status = "disabled"; -- }; -- }; -- - timers12: timer@40006000 { - #address-cells = <1>; - #size-cells = <0>; -@@ -291,152 +114,7 @@ - clocks = <&rcc TIM12_K>; - clock-names = "int"; - status = "disabled"; -- -- pwm { -- compatible = "st,stm32-pwm"; -- #pwm-cells = <3>; -- status = "disabled"; -- }; -- -- timer@11 { -- compatible = "st,stm32h7-timer-trigger"; -- reg = <11>; -- status = "disabled"; -- }; -- }; -- -- timers13: timer@40007000 { -- #address-cells = <1>; -- #size-cells = <0>; -- compatible = "st,stm32-timers"; -- reg = <0x40007000 0x400>; -- clocks = <&rcc TIM13_K>; -- clock-names = "int"; -- status = "disabled"; -- -- pwm { -- compatible = "st,stm32-pwm"; -- #pwm-cells = <3>; -- status = "disabled"; -- }; -- -- timer@12 { -- compatible = "st,stm32h7-timer-trigger"; -- reg = <12>; -- status = "disabled"; -- }; -- }; -- -- timers14: timer@40008000 { -- #address-cells = <1>; -- #size-cells = <0>; -- compatible = "st,stm32-timers"; -- reg = <0x40008000 0x400>; -- clocks = <&rcc TIM14_K>; -- clock-names = "int"; -- status = "disabled"; -- -- pwm { -- compatible = "st,stm32-pwm"; -- #pwm-cells = <3>; -- status = "disabled"; -- }; -- -- timer@13 { -- compatible = "st,stm32h7-timer-trigger"; -- reg = <13>; -- status = "disabled"; -- }; -- }; -- -- lptimer1: timer@40009000 { -- #address-cells = <1>; -- #size-cells = <0>; -- compatible = "st,stm32-lptimer"; -- reg = <0x40009000 0x400>; -- clocks = <&rcc LPTIM1_K>; -- clock-names = "mux"; -- status = "disabled"; -- -- pwm { -- compatible = "st,stm32-pwm-lp"; -- #pwm-cells = <3>; -- status = "disabled"; -- }; -- -- trigger@0 { -- compatible = "st,stm32-lptimer-trigger"; -- reg = <0>; -- status = "disabled"; -- }; -- -- counter { -- compatible = "st,stm32-lptimer-counter"; -- status = "disabled"; -- }; -- }; -- -- spi2: spi@4000b000 { -- #address-cells = <1>; -- #size-cells = <0>; -- compatible = "st,stm32h7-spi"; -- reg = <0x4000b000 0x400>; -- interrupts = ; -- clocks = <&rcc SPI2_K>; -- resets = <&rcc SPI2_R>; -- dmas = <&dmamux1 39 0x400 0x05>, -- <&dmamux1 40 0x400 0x05>; -- dma-names = "rx", "tx"; -- status = "disabled"; -- }; -- -- i2s2: audio-controller@4000b000 { -- compatible = "st,stm32h7-i2s"; -- #sound-dai-cells = <0>; -- reg = <0x4000b000 0x400>; -- interrupts = ; -- dmas = <&dmamux1 39 0x400 0x01>, -- <&dmamux1 40 0x400 0x01>; -- dma-names = "rx", "tx"; -- status = "disabled"; -- }; -- -- spi3: spi@4000c000 { -- #address-cells = <1>; -- #size-cells = <0>; -- compatible = "st,stm32h7-spi"; -- reg = <0x4000c000 0x400>; -- interrupts = ; -- clocks = <&rcc SPI3_K>; -- resets = <&rcc SPI3_R>; -- dmas = <&dmamux1 61 0x400 0x05>, -- <&dmamux1 62 0x400 0x05>; -- dma-names = "rx", "tx"; -- status = "disabled"; -- }; -- -- i2s3: audio-controller@4000c000 { -- compatible = "st,stm32h7-i2s"; -- #sound-dai-cells = <0>; -- reg = <0x4000c000 0x400>; -- interrupts = ; -- dmas = <&dmamux1 61 0x400 0x01>, -- <&dmamux1 62 0x400 0x01>; -- dma-names = "rx", "tx"; -- status = "disabled"; -- }; -- -- spdifrx: audio-controller@4000d000 { -- compatible = "st,stm32h7-spdifrx"; -- #sound-dai-cells = <0>; -- reg = <0x4000d000 0x400>; -- clocks = <&rcc SPDIF_K>; -- clock-names = "kclk"; -- interrupts = ; -- dmas = <&dmamux1 93 0x400 0x01>, -- <&dmamux1 94 0x400 0x01>; -- dma-names = "rx", "rx-ctrl"; -- status = "disabled"; -+ secure-status = "disabled"; - }; - - usart2: serial@4000e000 { -@@ -444,6 +122,7 @@ - reg = <0x4000e000 0x400>; - interrupts = ; - clocks = <&rcc USART2_K>; -+ resets = <&rcc USART2_R>; - status = "disabled"; - }; - -@@ -452,14 +131,17 @@ - reg = <0x4000f000 0x400>; - interrupts = ; - clocks = <&rcc USART3_K>; -+ resets = <&rcc USART3_R>; - status = "disabled"; - }; - - uart4: serial@40010000 { - compatible = "st,stm32h7-uart"; - reg = <0x40010000 0x400>; -- interrupts = ; -+ interrupts-extended = <&exti 30 IRQ_TYPE_LEVEL_HIGH>; - clocks = <&rcc UART4_K>; -+ resets = <&rcc UART4_R>; -+ wakeup-source; - status = "disabled"; - }; - -@@ -468,99 +150,16 @@ - reg = <0x40011000 0x400>; - interrupts = ; - clocks = <&rcc UART5_K>; -+ resets = <&rcc UART5_R>; - status = "disabled"; - }; - -- i2c1: i2c@40012000 { -- compatible = "st,stm32f7-i2c"; -- reg = <0x40012000 0x400>; -- interrupt-names = "event", "error"; -- interrupts = , -- ; -- clocks = <&rcc I2C1_K>; -- resets = <&rcc I2C1_R>; -- #address-cells = <1>; -- #size-cells = <0>; -- status = "disabled"; -- }; -- -- i2c2: i2c@40013000 { -- compatible = "st,stm32f7-i2c"; -- reg = <0x40013000 0x400>; -- interrupt-names = "event", "error"; -- interrupts = , -- ; -- clocks = <&rcc I2C2_K>; -- resets = <&rcc I2C2_R>; -- #address-cells = <1>; -- #size-cells = <0>; -- status = "disabled"; -- }; -- -- i2c3: i2c@40014000 { -- compatible = "st,stm32f7-i2c"; -- reg = <0x40014000 0x400>; -- interrupt-names = "event", "error"; -- interrupts = , -- ; -- clocks = <&rcc I2C3_K>; -- resets = <&rcc I2C3_R>; -- #address-cells = <1>; -- #size-cells = <0>; -- status = "disabled"; -- }; -- -- i2c5: i2c@40015000 { -- compatible = "st,stm32f7-i2c"; -- reg = <0x40015000 0x400>; -- interrupt-names = "event", "error"; -- interrupts = , -- ; -- clocks = <&rcc I2C5_K>; -- resets = <&rcc I2C5_R>; -- #address-cells = <1>; -- #size-cells = <0>; -- status = "disabled"; -- }; -- -- cec: cec@40016000 { -- compatible = "st,stm32-cec"; -- reg = <0x40016000 0x400>; -- interrupts = ; -- clocks = <&rcc CEC_K>, <&clk_lse>; -- clock-names = "cec", "hdmi-cec"; -- status = "disabled"; -- }; -- -- dac: dac@40017000 { -- compatible = "st,stm32h7-dac-core"; -- reg = <0x40017000 0x400>; -- clocks = <&rcc DAC12>; -- clock-names = "pclk"; -- #address-cells = <1>; -- #size-cells = <0>; -- status = "disabled"; -- -- dac1: dac@1 { -- compatible = "st,stm32-dac"; -- #io-channels-cells = <1>; -- reg = <1>; -- status = "disabled"; -- }; -- -- dac2: dac@2 { -- compatible = "st,stm32-dac"; -- #io-channels-cells = <1>; -- reg = <2>; -- status = "disabled"; -- }; -- }; -- - uart7: serial@40018000 { - compatible = "st,stm32h7-uart"; - reg = <0x40018000 0x400>; - interrupts = ; - clocks = <&rcc UART7_K>; -+ resets = <&rcc UART7_R>; - status = "disabled"; - }; - -@@ -569,125 +168,16 @@ - reg = <0x40019000 0x400>; - interrupts = ; - clocks = <&rcc UART8_K>; -+ resets = <&rcc UART8_R>; - status = "disabled"; - }; - -- timers1: timer@44000000 { -- #address-cells = <1>; -- #size-cells = <0>; -- compatible = "st,stm32-timers"; -- reg = <0x44000000 0x400>; -- clocks = <&rcc TIM1_K>; -- clock-names = "int"; -- dmas = <&dmamux1 11 0x400 0x1>, -- <&dmamux1 12 0x400 0x1>, -- <&dmamux1 13 0x400 0x1>, -- <&dmamux1 14 0x400 0x1>, -- <&dmamux1 15 0x400 0x1>, -- <&dmamux1 16 0x400 0x1>, -- <&dmamux1 17 0x400 0x1>; -- dma-names = "ch1", "ch2", "ch3", "ch4", -- "up", "trig", "com"; -- status = "disabled"; -- -- pwm { -- compatible = "st,stm32-pwm"; -- #pwm-cells = <3>; -- status = "disabled"; -- }; -- -- timer@0 { -- compatible = "st,stm32h7-timer-trigger"; -- reg = <0>; -- status = "disabled"; -- }; -- -- counter { -- compatible = "st,stm32-timer-counter"; -- status = "disabled"; -- }; -- }; -- -- timers8: timer@44001000 { -- #address-cells = <1>; -- #size-cells = <0>; -- compatible = "st,stm32-timers"; -- reg = <0x44001000 0x400>; -- clocks = <&rcc TIM8_K>; -- clock-names = "int"; -- dmas = <&dmamux1 47 0x400 0x1>, -- <&dmamux1 48 0x400 0x1>, -- <&dmamux1 49 0x400 0x1>, -- <&dmamux1 50 0x400 0x1>, -- <&dmamux1 51 0x400 0x1>, -- <&dmamux1 52 0x400 0x1>, -- <&dmamux1 53 0x400 0x1>; -- dma-names = "ch1", "ch2", "ch3", "ch4", -- "up", "trig", "com"; -- status = "disabled"; -- -- pwm { -- compatible = "st,stm32-pwm"; -- #pwm-cells = <3>; -- status = "disabled"; -- }; -- -- timer@7 { -- compatible = "st,stm32h7-timer-trigger"; -- reg = <7>; -- status = "disabled"; -- }; -- -- counter { -- compatible = "st,stm32-timer-counter"; -- status = "disabled"; -- }; -- }; -- - usart6: serial@44003000 { - compatible = "st,stm32h7-uart"; - reg = <0x44003000 0x400>; - interrupts = ; - clocks = <&rcc USART6_K>; -- status = "disabled"; -- }; -- -- spi1: spi@44004000 { -- #address-cells = <1>; -- #size-cells = <0>; -- compatible = "st,stm32h7-spi"; -- reg = <0x44004000 0x400>; -- interrupts = ; -- clocks = <&rcc SPI1_K>; -- resets = <&rcc SPI1_R>; -- dmas = <&dmamux1 37 0x400 0x05>, -- <&dmamux1 38 0x400 0x05>; -- dma-names = "rx", "tx"; -- status = "disabled"; -- }; -- -- i2s1: audio-controller@44004000 { -- compatible = "st,stm32h7-i2s"; -- #sound-dai-cells = <0>; -- reg = <0x44004000 0x400>; -- interrupts = ; -- dmas = <&dmamux1 37 0x400 0x01>, -- <&dmamux1 38 0x400 0x01>; -- dma-names = "rx", "tx"; -- status = "disabled"; -- }; -- -- spi4: spi@44005000 { -- #address-cells = <1>; -- #size-cells = <0>; -- compatible = "st,stm32h7-spi"; -- reg = <0x44005000 0x400>; -- interrupts = ; -- clocks = <&rcc SPI4_K>; -- resets = <&rcc SPI4_R>; -- dmas = <&dmamux1 83 0x400 0x05>, -- <&dmamux1 84 0x400 0x05>; -- dma-names = "rx", "tx"; -+ resets = <&rcc USART6_R>; - status = "disabled"; - }; - -@@ -698,401 +188,42 @@ - reg = <0x44006000 0x400>; - clocks = <&rcc TIM15_K>; - clock-names = "int"; -- dmas = <&dmamux1 105 0x400 0x1>, -- <&dmamux1 106 0x400 0x1>, -- <&dmamux1 107 0x400 0x1>, -- <&dmamux1 108 0x400 0x1>; -- dma-names = "ch1", "up", "trig", "com"; -- status = "disabled"; -- -- pwm { -- compatible = "st,stm32-pwm"; -- #pwm-cells = <3>; -- status = "disabled"; -- }; -- -- timer@14 { -- compatible = "st,stm32h7-timer-trigger"; -- reg = <14>; -- status = "disabled"; -- }; -- }; -- -- timers16: timer@44007000 { -- #address-cells = <1>; -- #size-cells = <0>; -- compatible = "st,stm32-timers"; -- reg = <0x44007000 0x400>; -- clocks = <&rcc TIM16_K>; -- clock-names = "int"; -- dmas = <&dmamux1 109 0x400 0x1>, -- <&dmamux1 110 0x400 0x1>; -- dma-names = "ch1", "up"; -- status = "disabled"; -- -- pwm { -- compatible = "st,stm32-pwm"; -- #pwm-cells = <3>; -- status = "disabled"; -- }; -- timer@15 { -- compatible = "st,stm32h7-timer-trigger"; -- reg = <15>; -- status = "disabled"; -- }; -- }; -- -- timers17: timer@44008000 { -- #address-cells = <1>; -- #size-cells = <0>; -- compatible = "st,stm32-timers"; -- reg = <0x44008000 0x400>; -- clocks = <&rcc TIM17_K>; -- clock-names = "int"; -- dmas = <&dmamux1 111 0x400 0x1>, -- <&dmamux1 112 0x400 0x1>; -- dma-names = "ch1", "up"; -- status = "disabled"; -- -- pwm { -- compatible = "st,stm32-pwm"; -- #pwm-cells = <3>; -- status = "disabled"; -- }; -- -- timer@16 { -- compatible = "st,stm32h7-timer-trigger"; -- reg = <16>; -- status = "disabled"; -- }; -- }; -- -- spi5: spi@44009000 { -- #address-cells = <1>; -- #size-cells = <0>; -- compatible = "st,stm32h7-spi"; -- reg = <0x44009000 0x400>; -- interrupts = ; -- clocks = <&rcc SPI5_K>; -- resets = <&rcc SPI5_R>; -- dmas = <&dmamux1 85 0x400 0x05>, -- <&dmamux1 86 0x400 0x05>; -- dma-names = "rx", "tx"; -- status = "disabled"; -- }; -- -- sai1: sai@4400a000 { -- compatible = "st,stm32h7-sai"; -- #address-cells = <1>; -- #size-cells = <1>; -- ranges = <0 0x4400a000 0x400>; -- reg = <0x4400a000 0x4>, <0x4400a3f0 0x10>; -- interrupts = ; -- resets = <&rcc SAI1_R>; -- status = "disabled"; -- -- sai1a: audio-controller@4400a004 { -- #sound-dai-cells = <0>; -- -- compatible = "st,stm32-sai-sub-a"; -- reg = <0x4 0x1c>; -- clocks = <&rcc SAI1_K>; -- clock-names = "sai_ck"; -- dmas = <&dmamux1 87 0x400 0x01>; -- status = "disabled"; -- }; -- -- sai1b: audio-controller@4400a024 { -- #sound-dai-cells = <0>; -- compatible = "st,stm32-sai-sub-b"; -- reg = <0x24 0x1c>; -- clocks = <&rcc SAI1_K>; -- clock-names = "sai_ck"; -- dmas = <&dmamux1 88 0x400 0x01>; -- status = "disabled"; -- }; -- }; -- -- sai2: sai@4400b000 { -- compatible = "st,stm32h7-sai"; -- #address-cells = <1>; -- #size-cells = <1>; -- ranges = <0 0x4400b000 0x400>; -- reg = <0x4400b000 0x4>, <0x4400b3f0 0x10>; -- interrupts = ; -- resets = <&rcc SAI2_R>; -- status = "disabled"; -- -- sai2a: audio-controller@4400b004 { -- #sound-dai-cells = <0>; -- compatible = "st,stm32-sai-sub-a"; -- reg = <0x4 0x1c>; -- clocks = <&rcc SAI2_K>; -- clock-names = "sai_ck"; -- dmas = <&dmamux1 89 0x400 0x01>; -- status = "disabled"; -- }; -- -- sai2b: audio-controller@4400b024 { -- #sound-dai-cells = <0>; -- compatible = "st,stm32-sai-sub-b"; -- reg = <0x24 0x1c>; -- clocks = <&rcc SAI2_K>; -- clock-names = "sai_ck"; -- dmas = <&dmamux1 90 0x400 0x01>; -- status = "disabled"; -- }; -- }; -- -- sai3: sai@4400c000 { -- compatible = "st,stm32h7-sai"; -- #address-cells = <1>; -- #size-cells = <1>; -- ranges = <0 0x4400c000 0x400>; -- reg = <0x4400c000 0x4>, <0x4400c3f0 0x10>; -- interrupts = ; -- resets = <&rcc SAI3_R>; -- status = "disabled"; -- -- sai3a: audio-controller@4400c004 { -- #sound-dai-cells = <0>; -- compatible = "st,stm32-sai-sub-a"; -- reg = <0x04 0x1c>; -- clocks = <&rcc SAI3_K>; -- clock-names = "sai_ck"; -- dmas = <&dmamux1 113 0x400 0x01>; -- status = "disabled"; -- }; -- -- sai3b: audio-controller@4400c024 { -- #sound-dai-cells = <0>; -- compatible = "st,stm32-sai-sub-b"; -- reg = <0x24 0x1c>; -- clocks = <&rcc SAI3_K>; -- clock-names = "sai_ck"; -- dmas = <&dmamux1 114 0x400 0x01>; -- status = "disabled"; -- }; -- }; -- -- dfsdm: dfsdm@4400d000 { -- compatible = "st,stm32mp1-dfsdm"; -- reg = <0x4400d000 0x800>; -- clocks = <&rcc DFSDM_K>; -- clock-names = "dfsdm"; -- #address-cells = <1>; -- #size-cells = <0>; -- status = "disabled"; -- -- dfsdm0: filter@0 { -- compatible = "st,stm32-dfsdm-adc"; -- #io-channel-cells = <1>; -- reg = <0>; -- interrupts = ; -- dmas = <&dmamux1 101 0x400 0x01>; -- dma-names = "rx"; -- status = "disabled"; -- }; -- -- dfsdm1: filter@1 { -- compatible = "st,stm32-dfsdm-adc"; -- #io-channel-cells = <1>; -- reg = <1>; -- interrupts = ; -- dmas = <&dmamux1 102 0x400 0x01>; -- dma-names = "rx"; -- status = "disabled"; -- }; -- -- dfsdm2: filter@2 { -- compatible = "st,stm32-dfsdm-adc"; -- #io-channel-cells = <1>; -- reg = <2>; -- interrupts = ; -- dmas = <&dmamux1 103 0x400 0x01>; -- dma-names = "rx"; -- status = "disabled"; -- }; -- -- dfsdm3: filter@3 { -- compatible = "st,stm32-dfsdm-adc"; -- #io-channel-cells = <1>; -- reg = <3>; -- interrupts = ; -- dmas = <&dmamux1 104 0x400 0x01>; -- dma-names = "rx"; -- status = "disabled"; -- }; -- -- dfsdm4: filter@4 { -- compatible = "st,stm32-dfsdm-adc"; -- #io-channel-cells = <1>; -- reg = <4>; -- interrupts = ; -- dmas = <&dmamux1 91 0x400 0x01>; -- dma-names = "rx"; -- status = "disabled"; -- }; -- -- dfsdm5: filter@5 { -- compatible = "st,stm32-dfsdm-adc"; -- #io-channel-cells = <1>; -- reg = <5>; -- interrupts = ; -- dmas = <&dmamux1 92 0x400 0x01>; -- dma-names = "rx"; -- status = "disabled"; -- }; -- }; -- -- dma1: dma-controller@48000000 { -- compatible = "st,stm32-dma"; -- reg = <0x48000000 0x400>; -- interrupts = , -- , -- , -- , -- , -- , -- , -- ; -- clocks = <&rcc DMA1>; -- #dma-cells = <4>; -- st,mem2mem; -- dma-requests = <8>; -- }; -- -- dma2: dma-controller@48001000 { -- compatible = "st,stm32-dma"; -- reg = <0x48001000 0x400>; -- interrupts = , -- , -- , -- , -- , -- , -- , -- ; -- clocks = <&rcc DMA2>; -- #dma-cells = <4>; -- st,mem2mem; -- dma-requests = <8>; -- }; -- -- dmamux1: dma-router@48002000 { -- compatible = "st,stm32h7-dmamux"; -- reg = <0x48002000 0x1c>; -- #dma-cells = <3>; -- dma-requests = <128>; -- dma-masters = <&dma1 &dma2>; -- dma-channels = <16>; -- clocks = <&rcc DMAMUX>; -- }; -- -- adc: adc@48003000 { -- compatible = "st,stm32mp1-adc-core"; -- reg = <0x48003000 0x400>; -- interrupts = , -- ; -- clocks = <&rcc ADC12>, <&rcc ADC12_K>; -- clock-names = "bus", "adc"; -- interrupt-controller; -- st,syscfg = <&syscfg>; -- #interrupt-cells = <1>; -- #address-cells = <1>; -- #size-cells = <0>; -- status = "disabled"; -- -- adc1: adc@0 { -- compatible = "st,stm32mp1-adc"; -- #io-channel-cells = <1>; -- reg = <0x0>; -- interrupt-parent = <&adc>; -- interrupts = <0>; -- dmas = <&dmamux1 9 0x400 0x01>; -- dma-names = "rx"; -- status = "disabled"; -- }; -- -- adc2: adc@100 { -- compatible = "st,stm32mp1-adc"; -- #io-channel-cells = <1>; -- reg = <0x100>; -- interrupt-parent = <&adc>; -- interrupts = <1>; -- dmas = <&dmamux1 10 0x400 0x01>; -- dma-names = "rx"; -- status = "disabled"; -- }; -- }; -- -- sdmmc3: sdmmc@48004000 { -- compatible = "arm,pl18x", "arm,primecell"; -- arm,primecell-periphid = <0x10153180>; -- reg = <0x48004000 0x400>; -- interrupts = ; -- interrupt-names = "cmd_irq"; -- clocks = <&rcc SDMMC3_K>; -- clock-names = "apb_pclk"; -- resets = <&rcc SDMMC3_R>; -- cap-sd-highspeed; -- cap-mmc-highspeed; -- max-frequency = <120000000>; - status = "disabled"; -+ secure-status = "disabled"; - }; - - usbotg_hs: usb-otg@49000000 { -- compatible = "snps,dwc2"; -+ compatible = "st,stm32mp15-hsotg", "snps,dwc2"; - reg = <0x49000000 0x10000>; - clocks = <&rcc USBO_K>; - clock-names = "otg"; - resets = <&rcc USBO_R>; - reset-names = "dwc2"; - interrupts = ; -- g-rx-fifo-size = <256>; -+ g-rx-fifo-size = <512>; - g-np-tx-fifo-size = <32>; -- g-tx-fifo-size = <128 128 64 64 64 64 32 32>; -+ g-tx-fifo-size = <256 16 16 16 16 16 16 16>; - dr_mode = "otg"; -- status = "disabled"; -- }; -- -- ipcc: mailbox@4c001000 { -- compatible = "st,stm32mp1-ipcc"; -- #mbox-cells = <1>; -- reg = <0x4c001000 0x400>; -- st,proc-id = <0>; -- interrupts-extended = -- <&intc GIC_SPI 100 IRQ_TYPE_LEVEL_HIGH>, -- <&intc GIC_SPI 101 IRQ_TYPE_LEVEL_HIGH>, -- <&exti 61 1>; -- interrupt-names = "rx", "tx", "wakeup"; -- clocks = <&rcc IPCC>; -- wakeup-source; -- status = "disabled"; -- }; -- -- dcmi: dcmi@4c006000 { -- compatible = "st,stm32-dcmi"; -- reg = <0x4c006000 0x400>; -- interrupts = ; -- resets = <&rcc CAMITF_R>; -- clocks = <&rcc DCMI>; -- clock-names = "mclk"; -- dmas = <&dmamux1 75 0x400 0x0d>; -- dma-names = "tx"; -+ usb33d-supply = <&usb33>; - status = "disabled"; - }; - - rcc: rcc@50000000 { -- compatible = "st,stm32mp1-rcc", "syscon"; -+ compatible = "st,stm32mp1-rcc-secure", "st,stm32mp1-rcc", "syscon"; - reg = <0x50000000 0x1000>; -+ #address-cells = <1>; -+ #size-cells = <0>; - #clock-cells = <1>; - #reset-cells = <1>; -+ interrupts = ; -+ secure-interrupts = ; -+ secure-interrupt-names = "wakeup"; - }; - - pwr_regulators: pwr@50001000 { - compatible = "st,stm32mp1,pwr-reg"; - reg = <0x50001000 0x10>; -+ st,tzcr = <&rcc 0x0 0x1>; - - reg11: reg11 { - regulator-name = "reg11"; -@@ -1113,11 +244,35 @@ - }; - }; - -+ pwr_mcu: pwr_mcu@50001014 { -+ compatible = "st,stm32mp151-pwr-mcu", "syscon"; -+ reg = <0x50001014 0x4>; -+ }; -+ -+ pwr_irq: pwr@50001020 { -+ compatible = "st,stm32mp1-pwr"; -+ reg = <0x50001020 0x100>; -+ interrupts = ; -+ interrupt-controller; -+ #interrupt-cells = <3>; -+ }; -+ - exti: interrupt-controller@5000d000 { - compatible = "st,stm32mp1-exti", "syscon"; - interrupt-controller; - #interrupt-cells = <2>; - reg = <0x5000d000 0x400>; -+ -+ /* exti_pwr is an extra interrupt controller used for -+ * EXTI 55 to 60. It's mapped on pwr interrupt -+ * controller. -+ */ -+ exti_pwr: exti-pwr { -+ interrupt-controller; -+ #interrupt-cells = <2>; -+ interrupt-parent = <&pwr_irq>; -+ st,irq-number = <6>; -+ }; - }; - - syscfg: syscon@50020000 { -@@ -1126,143 +281,14 @@ - clocks = <&rcc SYSCFG>; - }; - -- lptimer2: timer@50021000 { -- #address-cells = <1>; -- #size-cells = <0>; -- compatible = "st,stm32-lptimer"; -- reg = <0x50021000 0x400>; -- clocks = <&rcc LPTIM2_K>; -- clock-names = "mux"; -- status = "disabled"; -- -- pwm { -- compatible = "st,stm32-pwm-lp"; -- #pwm-cells = <3>; -- status = "disabled"; -- }; -- -- trigger@1 { -- compatible = "st,stm32-lptimer-trigger"; -- reg = <1>; -- status = "disabled"; -- }; -- -- counter { -- compatible = "st,stm32-lptimer-counter"; -- status = "disabled"; -- }; -- }; -- -- lptimer3: timer@50022000 { -- #address-cells = <1>; -- #size-cells = <0>; -- compatible = "st,stm32-lptimer"; -- reg = <0x50022000 0x400>; -- clocks = <&rcc LPTIM3_K>; -- clock-names = "mux"; -- status = "disabled"; -- -- pwm { -- compatible = "st,stm32-pwm-lp"; -- #pwm-cells = <3>; -- status = "disabled"; -- }; -- -- trigger@2 { -- compatible = "st,stm32-lptimer-trigger"; -- reg = <2>; -- status = "disabled"; -- }; -- }; -- -- lptimer4: timer@50023000 { -- compatible = "st,stm32-lptimer"; -- reg = <0x50023000 0x400>; -- clocks = <&rcc LPTIM4_K>; -- clock-names = "mux"; -- status = "disabled"; -- -- pwm { -- compatible = "st,stm32-pwm-lp"; -- #pwm-cells = <3>; -- status = "disabled"; -- }; -- }; -- -- lptimer5: timer@50024000 { -- compatible = "st,stm32-lptimer"; -- reg = <0x50024000 0x400>; -- clocks = <&rcc LPTIM5_K>; -- clock-names = "mux"; -- status = "disabled"; -- -- pwm { -- compatible = "st,stm32-pwm-lp"; -- #pwm-cells = <3>; -- status = "disabled"; -- }; -- }; -- -- vrefbuf: vrefbuf@50025000 { -- compatible = "st,stm32-vrefbuf"; -- reg = <0x50025000 0x8>; -- regulator-min-microvolt = <1500000>; -- regulator-max-microvolt = <2500000>; -- clocks = <&rcc VREF>; -- status = "disabled"; -- }; -- -- sai4: sai@50027000 { -- compatible = "st,stm32h7-sai"; -- #address-cells = <1>; -- #size-cells = <1>; -- ranges = <0 0x50027000 0x400>; -- reg = <0x50027000 0x4>, <0x500273f0 0x10>; -- interrupts = ; -- resets = <&rcc SAI4_R>; -- status = "disabled"; -- -- sai4a: audio-controller@50027004 { -- #sound-dai-cells = <0>; -- compatible = "st,stm32-sai-sub-a"; -- reg = <0x04 0x1c>; -- clocks = <&rcc SAI4_K>; -- clock-names = "sai_ck"; -- dmas = <&dmamux1 99 0x400 0x01>; -- status = "disabled"; -- }; -- -- sai4b: audio-controller@50027024 { -- #sound-dai-cells = <0>; -- compatible = "st,stm32-sai-sub-b"; -- reg = <0x24 0x1c>; -- clocks = <&rcc SAI4_K>; -- clock-names = "sai_ck"; -- dmas = <&dmamux1 100 0x400 0x01>; -- status = "disabled"; -- }; -- }; -- -- dts: thermal@50028000 { -- compatible = "st,stm32-thermal"; -- reg = <0x50028000 0x100>; -- interrupts = ; -- clocks = <&rcc TMPSENS>; -- clock-names = "pclk"; -- #thermal-sensor-cells = <0>; -- status = "disabled"; -- }; -- - hash1: hash@54002000 { - compatible = "st,stm32f756-hash"; - reg = <0x54002000 0x400>; - interrupts = ; - clocks = <&rcc HASH1>; - resets = <&rcc HASH1_R>; -- dmas = <&mdma1 31 0x10 0x1000A02 0x0 0x0>; -- dma-names = "in"; -- dma-maxburst = <2>; - status = "disabled"; -+ secure-status = "disabled"; - }; - - rng1: rng@54003000 { -@@ -1271,35 +297,37 @@ - clocks = <&rcc RNG1_K>; - resets = <&rcc RNG1_R>; - status = "disabled"; -+ secure-status = "disabled"; - }; - -- mdma1: dma-controller@58000000 { -- compatible = "st,stm32h7-mdma"; -- reg = <0x58000000 0x1000>; -- interrupts = ; -- clocks = <&rcc MDMA>; -- #dma-cells = <5>; -- dma-channels = <32>; -- dma-requests = <48>; -- }; -- -- fmc: nand-controller@58002000 { -- compatible = "st,stm32mp15-fmc2"; -- reg = <0x58002000 0x1000>, -- <0x80000000 0x1000>, -- <0x88010000 0x1000>, -- <0x88020000 0x1000>, -- <0x81000000 0x1000>, -- <0x89010000 0x1000>, -- <0x89020000 0x1000>; -- interrupts = ; -- dmas = <&mdma1 20 0x10 0x12000a02 0x0 0x0>, -- <&mdma1 20 0x10 0x12000a08 0x0 0x0>, -- <&mdma1 21 0x10 0x12000a0a 0x0 0x0>; -- dma-names = "tx", "rx", "ecc"; -+ fmc: memory-controller@58002000 { -+ #address-cells = <2>; -+ #size-cells = <1>; -+ compatible = "st,stm32mp1-fmc2-ebi"; -+ reg = <0x58002000 0x1000>; - clocks = <&rcc FMC_K>; - resets = <&rcc FMC_R>; - status = "disabled"; -+ -+ ranges = <0 0 0x60000000 0x04000000>, /* EBI CS 1 */ -+ <1 0 0x64000000 0x04000000>, /* EBI CS 2 */ -+ <2 0 0x68000000 0x04000000>, /* EBI CS 3 */ -+ <3 0 0x6c000000 0x04000000>, /* EBI CS 4 */ -+ <4 0 0x80000000 0x10000000>; /* NAND */ -+ -+ nand-controller@4,0 { -+ #address-cells = <1>; -+ #size-cells = <0>; -+ compatible = "st,stm32mp1-fmc2-nfc"; -+ reg = <4 0x00000000 0x1000>, -+ <4 0x08010000 0x1000>, -+ <4 0x08020000 0x1000>, -+ <4 0x01000000 0x1000>, -+ <4 0x09010000 0x1000>, -+ <4 0x09020000 0x1000>; -+ interrupts = ; -+ status = "disabled"; -+ }; - }; - - qspi: spi@58003000 { -@@ -1307,18 +335,17 @@ - reg = <0x58003000 0x1000>, <0x70000000 0x10000000>; - reg-names = "qspi", "qspi_mm"; - interrupts = ; -- dmas = <&mdma1 22 0x10 0x100002 0x0 0x0>, -- <&mdma1 22 0x10 0x100008 0x0 0x0>; -- dma-names = "tx", "rx"; - clocks = <&rcc QSPI_K>; - resets = <&rcc QSPI_R>; -+ #address-cells = <1>; -+ #size-cells = <0>; - status = "disabled"; - }; - - sdmmc1: sdmmc@58005000 { -- compatible = "arm,pl18x", "arm,primecell"; -- arm,primecell-periphid = <0x10153180>; -- reg = <0x58005000 0x1000>; -+ compatible = "st,stm32-sdmmc2", "arm,pl18x", "arm,primecell"; -+ arm,primecell-periphid = <0x00253180>; -+ reg = <0x58005000 0x1000>, <0x58006000 0x1000>; - interrupts = ; - interrupt-names = "cmd_irq"; - clocks = <&rcc SDMMC1_K>; -@@ -1331,9 +358,9 @@ - }; - - sdmmc2: sdmmc@58007000 { -- compatible = "arm,pl18x", "arm,primecell"; -- arm,primecell-periphid = <0x10153180>; -- reg = <0x58007000 0x1000>; -+ compatible = "st,stm32-sdmmc2", "arm,pl18x", "arm,primecell"; -+ arm,primecell-periphid = <0x00253180>; -+ reg = <0x58007000 0x1000>, <0x58008000 0x1000>; - interrupts = ; - interrupt-names = "cmd_irq"; - clocks = <&rcc SDMMC2_K>; -@@ -1345,87 +372,26 @@ - status = "disabled"; - }; - -- crc1: crc@58009000 { -- compatible = "st,stm32f7-crc"; -- reg = <0x58009000 0x400>; -- clocks = <&rcc CRC1>; -- status = "disabled"; -- }; -- -- stmmac_axi_config_0: stmmac-axi-config { -- snps,wr_osr_lmt = <0x7>; -- snps,rd_osr_lmt = <0x7>; -- snps,blen = <0 0 0 0 16 8 4>; -- }; -- -- ethernet0: ethernet@5800a000 { -- compatible = "st,stm32mp1-dwmac", "snps,dwmac-4.20a"; -- reg = <0x5800a000 0x2000>; -- reg-names = "stmmaceth"; -- interrupts-extended = <&intc GIC_SPI 61 IRQ_TYPE_LEVEL_HIGH>; -- interrupt-names = "macirq"; -- clock-names = "stmmaceth", -- "mac-clk-tx", -- "mac-clk-rx", -- "ethstp"; -- clocks = <&rcc ETHMAC>, -- <&rcc ETHTX>, -- <&rcc ETHRX>, -- <&rcc ETHSTP>; -- st,syscon = <&syscfg 0x4>; -- snps,mixed-burst; -- snps,pbl = <2>; -- snps,en-tx-lpi-clockgating; -- snps,axi-config = <&stmmac_axi_config_0>; -- snps,tso; -- status = "disabled"; -- }; -- -- usbh_ohci: usbh-ohci@5800c000 { -- compatible = "generic-ohci"; -- reg = <0x5800c000 0x1000>; -- clocks = <&rcc USBH>; -- resets = <&rcc USBH_R>; -- interrupts = ; -- status = "disabled"; -- }; -- -- usbh_ehci: usbh-ehci@5800d000 { -- compatible = "generic-ehci"; -- reg = <0x5800d000 0x1000>; -- clocks = <&rcc USBH>; -- resets = <&rcc USBH_R>; -- interrupts = ; -- companion = <&usbh_ohci>; -- status = "disabled"; -- }; -- -- ltdc: display-controller@5a001000 { -- compatible = "st,stm32-ltdc"; -- reg = <0x5a001000 0x400>; -- interrupts = , -- ; -- clocks = <&rcc LTDC_PX>; -- clock-names = "lcd"; -- resets = <&rcc LTDC_R>; -- status = "disabled"; -- }; -- - iwdg2: watchdog@5a002000 { - compatible = "st,stm32mp1-iwdg"; - reg = <0x5a002000 0x400>; -+ secure-interrupts = ; - clocks = <&rcc IWDG2>, <&rcc CK_LSI>; - clock-names = "pclk", "lsi"; - status = "disabled"; -+ secure-status = "disabled"; - }; - - usbphyc: usbphyc@5a006000 { - #address-cells = <1>; - #size-cells = <0>; -+ #clock-cells = <0>; - compatible = "st,stm32mp1-usbphyc"; - reg = <0x5a006000 0x1000>; - clocks = <&rcc USBPHY_K>; - resets = <&rcc USBPHY_R>; -+ vdda1v1-supply = <®11>; -+ vdda1v8-supply = <®18>; - status = "disabled"; - - usbphyc_port0: usb-phy@0 { -@@ -1446,6 +412,7 @@ - clocks = <&rcc USART1_K>; - resets = <&rcc USART1_R>; - status = "disabled"; -+ secure-status = "disabled"; - }; - - spi6: spi@5c001000 { -@@ -1456,22 +423,34 @@ - interrupts = ; - clocks = <&rcc SPI6_K>; - resets = <&rcc SPI6_R>; -- dmas = <&mdma1 34 0x0 0x40008 0x0 0x0>, -- <&mdma1 35 0x0 0x40002 0x0 0x0>; -- dma-names = "rx", "tx"; - status = "disabled"; -+ secure-status = "disabled"; - }; - - i2c4: i2c@5c002000 { -- compatible = "st,stm32f7-i2c"; -+ compatible = "st,stm32mp15-i2c"; - reg = <0x5c002000 0x400>; - interrupt-names = "event", "error"; -- interrupts = , -- ; -+ interrupts-extended = <&exti 24 IRQ_TYPE_LEVEL_HIGH>, -+ <&intc GIC_SPI 96 IRQ_TYPE_LEVEL_HIGH>; - clocks = <&rcc I2C4_K>; - resets = <&rcc I2C4_R>; - #address-cells = <1>; - #size-cells = <0>; -+ st,syscfg-fmp = <&syscfg 0x4 0x8>; -+ wakeup-source; -+ status = "disabled"; -+ secure-status = "disabled"; -+ }; -+ -+ iwdg1: watchdog@5c003000 { -+ compatible = "st,stm32mp1-iwdg"; -+ reg = <0x5C003000 0x400>; -+ interrupts = ; -+ clocks = <&rcc IWDG1>, <&rcc CK_LSI>; -+ clock-names = "pclk", "lsi"; -+ status = "disabled"; -+ secure-status = "disabled"; - }; - - rtc: rtc@5c004000 { -@@ -1479,8 +458,9 @@ - reg = <0x5c004000 0x400>; - clocks = <&rcc RTCAPB>, <&rcc RTC>; - clock-names = "pclk", "rtc_ck"; -- interrupts = ; -+ interrupts-extended = <&exti 19 IRQ_TYPE_LEVEL_HIGH>; - status = "disabled"; -+ secure-status = "disabled"; - }; - - bsec: efuse@5c005000 { -@@ -1488,13 +468,38 @@ - reg = <0x5c005000 0x400>; - #address-cells = <1>; - #size-cells = <1>; -+ -+ cfg0_otp: cfg0_otp@0 { -+ reg = <0x0 0x1>; -+ }; -+ part_number_otp: part_number_otp@4 { -+ reg = <0x4 0x1>; -+ }; -+ monotonic_otp: monotonic_otp@10 { -+ reg = <0x10 0x4>; -+ }; -+ nand_otp: nand_otp@24 { -+ reg = <0x24 0x4>; -+ }; -+ uid_otp: uid_otp@34 { -+ reg = <0x34 0xc>; -+ }; -+ package_otp: package_otp@40 { -+ reg = <0x40 0x4>; -+ }; -+ hw2_otp: hw2_otp@48 { -+ reg = <0x48 0x4>; -+ }; - ts_cal1: calib@5c { - reg = <0x5c 0x2>; - }; - ts_cal2: calib@5e { - reg = <0x5e 0x2>; - }; -- mac_addr: mac_addr@e4 { -+ pkh_otp: pkh_otp@60 { -+ reg = <0x60 0x20>; -+ }; -+ ethernet_mac_address: mac@e4 { - reg = <0xe4 0x8>; - st,non-secure-otp; - }; -@@ -1508,17 +513,32 @@ - secure-status = "okay"; - }; - -+ stgen: stgen@5c008000 { -+ compatible = "st,stm32-stgen"; -+ reg = <0x5C008000 0x1000>; -+ }; -+ - i2c6: i2c@5c009000 { -- compatible = "st,stm32f7-i2c"; -+ compatible = "st,stm32mp15-i2c"; - reg = <0x5c009000 0x400>; - interrupt-names = "event", "error"; -- interrupts = , -- ; -+ interrupts-extended = <&exti 54 IRQ_TYPE_LEVEL_HIGH>, -+ <&intc GIC_SPI 136 IRQ_TYPE_LEVEL_HIGH>; - clocks = <&rcc I2C6_K>; - resets = <&rcc I2C6_R>; - #address-cells = <1>; - #size-cells = <0>; -+ st,syscfg-fmp = <&syscfg 0x4 0x20>; -+ wakeup-source; - status = "disabled"; -+ secure-status = "disabled"; -+ }; -+ -+ tamp: tamp@5c00a000 { -+ compatible = "st,stm32-tamp", "simple-bus", "syscon", "simple-mfd"; -+ reg = <0x5c00a000 0x400>; -+ secure-interrupts = ; -+ clocks = <&rcc RTCAPB>; - }; - - /* -@@ -1675,28 +695,8 @@ - st,bank-name = "GPIOZ"; - st,bank-ioport = <11>; - status = "disabled"; -+ secure-status = "disabled"; - }; - }; - }; -- -- mlahb: ahb { -- compatible = "st,mlahb", "simple-bus"; -- #address-cells = <1>; -- #size-cells = <1>; -- ranges; -- dma-ranges = <0x00000000 0x38000000 0x10000>, -- <0x10000000 0x10000000 0x60000>, -- <0x30000000 0x30000000 0x60000>; -- -- m4_rproc: m4@10000000 { -- compatible = "st,stm32mp1-m4"; -- reg = <0x10000000 0x40000>, -- <0x30000000 0x40000>, -- <0x38000000 0x10000>; -- resets = <&rcc MCU_R>; -- st,syscfg-holdboot = <&rcc 0x10C 0x1>; -- st,syscfg-tz = <&rcc 0x000 0x1>; -- status = "disabled"; -- }; -- }; - }; -diff --git a/core/arch/arm/dts/stm32mp153.dtsi b/core/arch/arm/dts/stm32mp153.dtsi -index 2d759fc60..617380a52 100644 ---- a/core/arch/arm/dts/stm32mp153.dtsi -+++ b/core/arch/arm/dts/stm32mp153.dtsi -@@ -12,34 +12,9 @@ - compatible = "arm,cortex-a7"; - device_type = "cpu"; - reg = <1>; -- }; -- }; -- -- soc { -- m_can1: can@4400e000 { -- compatible = "bosch,m_can"; -- reg = <0x4400e000 0x400>, <0x44011000 0x1400>; -- reg-names = "m_can", "message_ram"; -- interrupts = , -- ; -- interrupt-names = "int0", "int1"; -- clocks = <&rcc CK_HSE>, <&rcc FDCAN_K>; -- clock-names = "hclk", "cclk"; -- bosch,mram-cfg = <0x0 0 0 32 0 0 2 2>; -- status = "disabled"; -- }; -- -- m_can2: can@4400f000 { -- compatible = "bosch,m_can"; -- reg = <0x4400f000 0x400>, <0x44011000 0x2800>; -- reg-names = "m_can", "message_ram"; -- interrupts = , -- ; -- interrupt-names = "int0", "int1"; -- clocks = <&rcc CK_HSE>, <&rcc FDCAN_K>; -- clock-names = "hclk", "cclk"; -- bosch,mram-cfg = <0x1400 0 0 32 0 0 2 2>; -- status = "disabled"; -+ clocks = <&rcc CK_MPU>; -+ clock-names = "cpu"; -+ operating-points-v2 = <&cpu0_opp_table>; - }; - }; - }; -diff --git a/core/arch/arm/dts/stm32mp157.dtsi b/core/arch/arm/dts/stm32mp157.dtsi -index 3f0a4a91c..c83402907 100644 ---- a/core/arch/arm/dts/stm32mp157.dtsi -+++ b/core/arch/arm/dts/stm32mp157.dtsi -@@ -5,27 +5,3 @@ - */ - - #include "stm32mp153.dtsi" -- --/ { -- soc { -- gpu: gpu@59000000 { -- compatible = "vivante,gc"; -- reg = <0x59000000 0x800>; -- interrupts = ; -- clocks = <&rcc GPU>, <&rcc GPU_K>; -- clock-names = "bus" ,"core"; -- resets = <&rcc GPU_R>; -- status = "disabled"; -- }; -- -- dsi: dsi@5a000000 { -- compatible = "st,stm32-dsi"; -- reg = <0x5a000000 0x800>; -- clocks = <&rcc DSI_K>, <&clk_hse>, <&rcc DSI_PX>; -- clock-names = "pclk", "ref", "px_clk"; -- resets = <&rcc DSI_R>; -- reset-names = "apb"; -- status = "disabled"; -- }; -- }; --}; -diff --git a/core/arch/arm/dts/stm32mp157a-dk1.dts b/core/arch/arm/dts/stm32mp157a-dk1.dts -index bc9ee69e6..5d5c0a5f7 100644 ---- a/core/arch/arm/dts/stm32mp157a-dk1.dts -+++ b/core/arch/arm/dts/stm32mp157a-dk1.dts -@@ -7,17 +7,20 @@ - /dts-v1/; - - #include "stm32mp157.dtsi" -+#include "stm32mp15xa.dtsi" - #include "stm32mp15-pinctrl.dtsi" - #include "stm32mp15xxac-pinctrl.dtsi" - #include "stm32mp15xx-dkx.dtsi" -+#include - - / { - model = "STMicroelectronics STM32MP157A-DK1 Discovery Board"; - compatible = "st,stm32mp157a-dk1", "st,stm32mp157"; - - aliases { -- ethernet0 = ðernet0; - serial0 = &uart4; -+ serial1 = &usart3; -+ serial2 = &uart7; - }; - - chosen { -@@ -25,14 +28,18 @@ - }; - }; - --&rcc { -- status = "okay"; -- secure-status = "disable"; --}; -- --&bsec { -- board_id: board_id@ec { -- reg = <0xec 0x4>; -- st,non-secure-otp; -- }; -+&etzpc { -+ st,decprot = < -+ DECPROT(STM32MP1_ETZPC_USART1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_SPI6_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_I2C4_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_I2C6_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_RNG1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_HASH1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_DDRCTRL_ID, DECPROT_NS_R_S_W, DECPROT_LOCK) -+ DECPROT(STM32MP1_ETZPC_DDRPHYC_ID, DECPROT_NS_R_S_W, DECPROT_LOCK) -+ DECPROT(STM32MP1_ETZPC_STGENC_ID, DECPROT_S_RW, DECPROT_LOCK) -+ DECPROT(STM32MP1_ETZPC_BKPSRAM_ID, DECPROT_S_RW, DECPROT_LOCK) -+ DECPROT(STM32MP1_ETZPC_IWDG1_ID, DECPROT_S_RW, DECPROT_LOCK) -+ >; - }; -diff --git a/core/arch/arm/dts/stm32mp157a-ed1.dts b/core/arch/arm/dts/stm32mp157a-ed1.dts -new file mode 100644 -index 000000000..1527b642a ---- /dev/null -+++ b/core/arch/arm/dts/stm32mp157a-ed1.dts -@@ -0,0 +1,38 @@ -+// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) -+/* -+ * Copyright (C) STMicroelectronics 2019 - All Rights Reserved -+ * Author: Alexandre Torgue for STMicroelectronics. -+ */ -+/dts-v1/; -+ -+#include "stm32mp157.dtsi" -+#include "stm32mp15xa.dtsi" -+#include "stm32mp15-pinctrl.dtsi" -+#include "stm32mp15xxaa-pinctrl.dtsi" -+#include "stm32mp15xx-edx.dtsi" -+#include -+ -+/ { -+ model = "STMicroelectronics STM32MP157A eval daughter"; -+ compatible = "st,stm32mp157a-ed1", "st,stm32mp157"; -+ -+ chosen { -+ stdout-path = "serial0:115200n8"; -+ }; -+}; -+ -+&etzpc { -+ st,decprot = < -+ DECPROT(STM32MP1_ETZPC_USART1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_SPI6_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_I2C4_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_I2C6_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_RNG1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_HASH1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_DDRCTRL_ID, DECPROT_NS_R_S_W, DECPROT_LOCK) -+ DECPROT(STM32MP1_ETZPC_DDRPHYC_ID, DECPROT_NS_R_S_W, DECPROT_LOCK) -+ DECPROT(STM32MP1_ETZPC_STGENC_ID, DECPROT_S_RW, DECPROT_LOCK) -+ DECPROT(STM32MP1_ETZPC_BKPSRAM_ID, DECPROT_S_RW, DECPROT_LOCK) -+ DECPROT(STM32MP1_ETZPC_IWDG1_ID, DECPROT_S_RW, DECPROT_LOCK) -+ >; -+}; -diff --git a/core/arch/arm/dts/stm32mp157a-ev1.dts b/core/arch/arm/dts/stm32mp157a-ev1.dts -new file mode 100644 -index 000000000..3cb35698a ---- /dev/null -+++ b/core/arch/arm/dts/stm32mp157a-ev1.dts -@@ -0,0 +1,24 @@ -+// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) -+/* -+ * Copyright (C) STMicroelectronics 2019 - All Rights Reserved -+ * Author: Alexandre Torgue for STMicroelectronics. -+ */ -+/dts-v1/; -+ -+#include "stm32mp157a-ed1.dts" -+#include "stm32mp15xx-evx.dtsi" -+ -+/ { -+ model = "STMicroelectronics STM32MP157A eval daughter on eval mother"; -+ compatible = "st,stm32mp157a-ev1", "st,stm32mp157a-ed1", "st,stm32mp157"; -+ -+ chosen { -+ stdout-path = "serial0:115200n8"; -+ }; -+ -+ aliases { -+ serial0 = &uart4; -+ serial1 = &usart3; -+ }; -+}; -+ -diff --git a/core/arch/arm/dts/stm32mp157c-dk2.dts b/core/arch/arm/dts/stm32mp157c-dk2.dts -index c5b226de6..ff5c4509f 100644 ---- a/core/arch/arm/dts/stm32mp157c-dk2.dts -+++ b/core/arch/arm/dts/stm32mp157c-dk2.dts -@@ -11,14 +11,17 @@ - #include "stm32mp15-pinctrl.dtsi" - #include "stm32mp15xxac-pinctrl.dtsi" - #include "stm32mp15xx-dkx.dtsi" -+#include - - / { - model = "STMicroelectronics STM32MP157C-DK2 Discovery Board"; - compatible = "st,stm32mp157c-dk2", "st,stm32mp157"; - - aliases { -- ethernet0 = ðernet0; - serial0 = &uart4; -+ serial1 = &usart3; -+ serial2 = &uart7; -+ serial3 = &usart2; - }; - - chosen { -@@ -26,81 +29,23 @@ - }; - }; - --&dsi { -- #address-cells = <1>; -- #size-cells = <0>; -+&cryp1 { - status = "okay"; -- phy-dsi-supply = <®18>; -- -- ports { -- #address-cells = <1>; -- #size-cells = <0>; -- -- port@0 { -- reg = <0>; -- dsi_in: endpoint { -- remote-endpoint = <<dc_ep1_out>; -- }; -- }; -- -- port@1 { -- reg = <1>; -- dsi_out: endpoint { -- remote-endpoint = <&panel_in>; -- }; -- }; -- }; -- -- panel@0 { -- compatible = "orisetech,otm8009a"; -- reg = <0>; -- reset-gpios = <&gpioe 4 GPIO_ACTIVE_LOW>; -- power-supply = <&v3v3>; -- status = "okay"; -- -- port { -- panel_in: endpoint { -- remote-endpoint = <&dsi_out>; -- }; -- }; -- }; --}; -- --&i2c1 { -- touchscreen@38 { -- compatible = "focaltech,ft6236"; -- reg = <0x38>; -- interrupts = <2 2>; -- interrupt-parent = <&gpiof>; -- interrupt-controller; -- touchscreen-size-x = <480>; -- touchscreen-size-y = <800>; -- status = "okay"; -- }; - }; - --<dc { -- status = "okay"; -- -- port { -- #address-cells = <1>; -- #size-cells = <0>; -- -- ltdc_ep1_out: endpoint@1 { -- reg = <1>; -- remote-endpoint = <&dsi_in>; -- }; -- }; --}; -- --&rcc { -- status = "okay"; -- secure-status = "disable"; --}; -- --&bsec { -- board_id: board_id@ec { -- reg = <0xec 0x4>; -- st,non-secure-otp; -- }; -+&etzpc { -+ st,decprot = < -+ DECPROT(STM32MP1_ETZPC_USART1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_SPI6_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_I2C4_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_I2C6_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_RNG1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_HASH1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_CRYP1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_DDRCTRL_ID, DECPROT_NS_R_S_W, DECPROT_LOCK) -+ DECPROT(STM32MP1_ETZPC_DDRPHYC_ID, DECPROT_NS_R_S_W, DECPROT_LOCK) -+ DECPROT(STM32MP1_ETZPC_STGENC_ID, DECPROT_S_RW, DECPROT_LOCK) -+ DECPROT(STM32MP1_ETZPC_BKPSRAM_ID, DECPROT_S_RW, DECPROT_LOCK) -+ DECPROT(STM32MP1_ETZPC_IWDG1_ID, DECPROT_S_RW, DECPROT_LOCK) -+ >; - }; -diff --git a/core/arch/arm/dts/stm32mp157c-ed1.dts b/core/arch/arm/dts/stm32mp157c-ed1.dts -index ea301c550..8d8014751 100644 ---- a/core/arch/arm/dts/stm32mp157c-ed1.dts -+++ b/core/arch/arm/dts/stm32mp157c-ed1.dts -@@ -1,7 +1,7 @@ - // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) - /* -- * Copyright (C) STMicroelectronics 2017 - All Rights Reserved -- * Author: Ludovic Barre for STMicroelectronics. -+ * Copyright (C) STMicroelectronics 2019 - All Rights Reserved -+ * Author: Alexandre Torgue for STMicroelectronics. - */ - /dts-v1/; - -@@ -9,8 +9,8 @@ - #include "stm32mp15xc.dtsi" - #include "stm32mp15-pinctrl.dtsi" - #include "stm32mp15xxaa-pinctrl.dtsi" --#include --#include -+#include "stm32mp15xx-edx.dtsi" -+#include - - / { - model = "STMicroelectronics STM32MP157C eval daughter"; -@@ -19,362 +19,25 @@ - chosen { - stdout-path = "serial0:115200n8"; - }; -- -- memory@c0000000 { -- device_type = "memory"; -- reg = <0xC0000000 0x40000000>; -- }; -- -- reserved-memory { -- #address-cells = <1>; -- #size-cells = <1>; -- ranges; -- -- mcuram2: mcuram2@10000000 { -- compatible = "shared-dma-pool"; -- reg = <0x10000000 0x40000>; -- no-map; -- }; -- -- vdev0vring0: vdev0vring0@10040000 { -- compatible = "shared-dma-pool"; -- reg = <0x10040000 0x1000>; -- no-map; -- }; -- -- vdev0vring1: vdev0vring1@10041000 { -- compatible = "shared-dma-pool"; -- reg = <0x10041000 0x1000>; -- no-map; -- }; -- -- vdev0buffer: vdev0buffer@10042000 { -- compatible = "shared-dma-pool"; -- reg = <0x10042000 0x4000>; -- no-map; -- }; -- -- mcuram: mcuram@30000000 { -- compatible = "shared-dma-pool"; -- reg = <0x30000000 0x40000>; -- no-map; -- }; -- -- retram: retram@38000000 { -- compatible = "shared-dma-pool"; -- reg = <0x38000000 0x10000>; -- no-map; -- }; -- -- gpu_reserved: gpu@e8000000 { -- reg = <0xe8000000 0x8000000>; -- no-map; -- }; -- }; -- -- aliases { -- serial0 = &uart4; -- }; -- -- sd_switch: regulator-sd_switch { -- compatible = "regulator-gpio"; -- regulator-name = "sd_switch"; -- regulator-min-microvolt = <1800000>; -- regulator-max-microvolt = <2900000>; -- regulator-type = "voltage"; -- regulator-always-on; -- -- gpios = <&gpiof 14 GPIO_ACTIVE_HIGH>; -- gpios-states = <0>; -- states = <1800000 0x1>, -- <2900000 0x0>; -- }; --}; -- --&adc { -- /* ANA0, ANA1 are dedicated pins and don't need pinctrl: only in6. */ -- pinctrl-0 = <&adc1_in6_pins_a>; -- pinctrl-names = "default"; -- vdd-supply = <&vdd>; -- vdda-supply = <&vdda>; -- vref-supply = <&vdda>; -- status = "disabled"; -- adc1: adc@0 { -- st,adc-channels = <0 1 6>; -- /* 16.5 ck_cycles sampling time */ -- st,min-sample-time-nsecs = <400>; -- status = "okay"; -- }; --}; -- --&dac { -- pinctrl-names = "default"; -- pinctrl-0 = <&dac_ch1_pins_a &dac_ch2_pins_a>; -- vref-supply = <&vdda>; -- status = "disabled"; -- dac1: dac@1 { -- status = "okay"; -- }; -- dac2: dac@2 { -- status = "okay"; -- }; --}; -- --&dts { -- status = "okay"; --}; -- --&gpu { -- contiguous-area = <&gpu_reserved>; -- status = "okay"; --}; -- --&i2c4 { -- pinctrl-names = "default"; -- pinctrl-0 = <&i2c4_pins_a>; -- i2c-scl-rising-time-ns = <185>; -- i2c-scl-falling-time-ns = <20>; -- status = "okay"; -- /* spare dmas for other usage */ -- /delete-property/dmas; -- /delete-property/dma-names; -- -- pmic: stpmic@33 { -- compatible = "st,stpmic1"; -- reg = <0x33>; -- interrupts-extended = <&gpioa 0 IRQ_TYPE_EDGE_FALLING>; -- interrupt-controller; -- #interrupt-cells = <2>; -- status = "okay"; -- -- regulators { -- compatible = "st,stpmic1-regulators"; -- ldo1-supply = <&v3v3>; -- ldo2-supply = <&v3v3>; -- ldo3-supply = <&vdd_ddr>; -- ldo5-supply = <&v3v3>; -- ldo6-supply = <&v3v3>; -- pwr_sw1-supply = <&bst_out>; -- pwr_sw2-supply = <&bst_out>; -- -- vddcore: buck1 { -- regulator-name = "vddcore"; -- regulator-min-microvolt = <1200000>; -- regulator-max-microvolt = <1350000>; -- regulator-always-on; -- regulator-initial-mode = <0>; -- regulator-over-current-protection; -- }; -- -- vdd_ddr: buck2 { -- regulator-name = "vdd_ddr"; -- regulator-min-microvolt = <1350000>; -- regulator-max-microvolt = <1350000>; -- regulator-always-on; -- regulator-initial-mode = <0>; -- regulator-over-current-protection; -- }; -- -- vdd: buck3 { -- regulator-name = "vdd"; -- regulator-min-microvolt = <3300000>; -- regulator-max-microvolt = <3300000>; -- regulator-always-on; -- st,mask-reset; -- regulator-initial-mode = <0>; -- regulator-over-current-protection; -- }; -- -- v3v3: buck4 { -- regulator-name = "v3v3"; -- regulator-min-microvolt = <3300000>; -- regulator-max-microvolt = <3300000>; -- regulator-always-on; -- regulator-over-current-protection; -- regulator-initial-mode = <0>; -- }; -- -- vdda: ldo1 { -- regulator-name = "vdda"; -- regulator-min-microvolt = <2900000>; -- regulator-max-microvolt = <2900000>; -- interrupts = ; -- }; -- -- v2v8: ldo2 { -- regulator-name = "v2v8"; -- regulator-min-microvolt = <2800000>; -- regulator-max-microvolt = <2800000>; -- interrupts = ; -- }; -- -- vtt_ddr: ldo3 { -- regulator-name = "vtt_ddr"; -- regulator-min-microvolt = <500000>; -- regulator-max-microvolt = <750000>; -- regulator-always-on; -- regulator-over-current-protection; -- }; -- -- vdd_usb: ldo4 { -- regulator-name = "vdd_usb"; -- regulator-min-microvolt = <3300000>; -- regulator-max-microvolt = <3300000>; -- interrupts = ; -- }; -- -- vdd_sd: ldo5 { -- regulator-name = "vdd_sd"; -- regulator-min-microvolt = <2900000>; -- regulator-max-microvolt = <2900000>; -- interrupts = ; -- regulator-boot-on; -- }; -- -- v1v8: ldo6 { -- regulator-name = "v1v8"; -- regulator-min-microvolt = <1800000>; -- regulator-max-microvolt = <1800000>; -- interrupts = ; -- }; -- -- vref_ddr: vref_ddr { -- regulator-name = "vref_ddr"; -- regulator-always-on; -- regulator-over-current-protection; -- }; -- -- bst_out: boost { -- regulator-name = "bst_out"; -- interrupts = ; -- }; -- -- vbus_otg: pwr_sw1 { -- regulator-name = "vbus_otg"; -- interrupts = ; -- }; -- -- vbus_sw: pwr_sw2 { -- regulator-name = "vbus_sw"; -- interrupts = ; -- regulator-active-discharge = <1>; -- }; -- }; -- -- onkey { -- compatible = "st,stpmic1-onkey"; -- interrupts = , ; -- interrupt-names = "onkey-falling", "onkey-rising"; -- power-off-time-sec = <10>; -- status = "okay"; -- }; -- -- watchdog { -- compatible = "st,stpmic1-wdt"; -- status = "disabled"; -- }; -- }; --}; -- --&ipcc { -- status = "okay"; --}; -- --&iwdg2 { -- timeout-sec = <32>; -- status = "okay"; --}; -- --&m4_rproc { -- memory-region = <&retram>, <&mcuram>, <&mcuram2>, <&vdev0vring0>, -- <&vdev0vring1>, <&vdev0buffer>; -- mboxes = <&ipcc 0>, <&ipcc 1>, <&ipcc 2>; -- mbox-names = "vq0", "vq1", "shutdown"; -- interrupt-parent = <&exti>; -- interrupts = <68 1>; -- status = "okay"; --}; -- --&pwr_regulators { -- vdd-supply = <&vdd>; -- vdd_3v3_usbfs-supply = <&vdd_usb>; --}; -- --&rng1 { -- status = "okay"; - }; - --&rtc { -+&cryp1 { - status = "okay"; - }; - --&sdmmc1 { -- pinctrl-names = "default", "opendrain", "sleep"; -- pinctrl-0 = <&sdmmc1_b4_pins_a &sdmmc1_dir_pins_a>; -- pinctrl-1 = <&sdmmc1_b4_od_pins_a &sdmmc1_dir_pins_a>; -- pinctrl-2 = <&sdmmc1_b4_sleep_pins_a &sdmmc1_dir_sleep_pins_a>; -- broken-cd; -- st,sig-dir; -- st,neg-edge; -- st,use-ckin; -- bus-width = <4>; -- vmmc-supply = <&vdd_sd>; -- vqmmc-supply = <&sd_switch>; -- status = "okay"; --}; -- --&sdmmc2 { -- pinctrl-names = "default", "opendrain", "sleep"; -- pinctrl-0 = <&sdmmc2_b4_pins_a &sdmmc2_d47_pins_a>; -- pinctrl-1 = <&sdmmc2_b4_od_pins_a &sdmmc2_d47_pins_a>; -- pinctrl-2 = <&sdmmc2_b4_sleep_pins_a &sdmmc2_d47_sleep_pins_a>; -- non-removable; -- no-sd; -- no-sdio; -- st,neg-edge; -- bus-width = <8>; -- vmmc-supply = <&v3v3>; -- vqmmc-supply = <&v3v3>; -- mmc-ddr-3_3v; -- status = "okay"; --}; -- --&timers6 { -- status = "okay"; -- /* spare dmas for other usage */ -- /delete-property/dmas; -- /delete-property/dma-names; -- timer@5 { -- status = "okay"; -- }; --}; -- --&uart4 { -- pinctrl-names = "default"; -- pinctrl-0 = <&uart4_pins_a>; -- status = "okay"; --}; -- --&usbphyc_port0 { -- phy-supply = <&vdd_usb>; -- vdda1v1-supply = <®11>; -- vdda1v8-supply = <®18>; --}; -- --&usbphyc_port1 { -- phy-supply = <&vdd_usb>; -- vdda1v1-supply = <®11>; -- vdda1v8-supply = <®18>; --}; -- --&rcc { -- status = "okay"; -- secure-status = "disable"; --}; -- --&bsec { -- board_id: board_id@ec { -- reg = <0xec 0x4>; -- st,non-secure-otp; -- }; -+&etzpc { -+ st,decprot = < -+ DECPROT(STM32MP1_ETZPC_USART1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_SPI6_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_I2C4_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_I2C6_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_RNG1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_HASH1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_CRYP1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_DDRCTRL_ID, DECPROT_NS_R_S_W, DECPROT_LOCK) -+ DECPROT(STM32MP1_ETZPC_DDRPHYC_ID, DECPROT_NS_R_S_W, DECPROT_LOCK) -+ DECPROT(STM32MP1_ETZPC_STGENC_ID, DECPROT_S_RW, DECPROT_LOCK) -+ DECPROT(STM32MP1_ETZPC_BKPSRAM_ID, DECPROT_S_RW, DECPROT_LOCK) -+ DECPROT(STM32MP1_ETZPC_IWDG1_ID, DECPROT_S_RW, DECPROT_LOCK) -+ >; - }; -diff --git a/core/arch/arm/dts/stm32mp157c-ev1.dts b/core/arch/arm/dts/stm32mp157c-ev1.dts -index fd8c958b0..d12d30093 100644 ---- a/core/arch/arm/dts/stm32mp157c-ev1.dts -+++ b/core/arch/arm/dts/stm32mp157c-ev1.dts -@@ -1,13 +1,12 @@ - // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) - /* -- * Copyright (C) STMicroelectronics 2017 - All Rights Reserved -- * Author: Ludovic Barre for STMicroelectronics. -+ * Copyright (C) STMicroelectronics 2019 - All Rights Reserved -+ * Author: Alexandre Torgue for STMicroelectronics. - */ - /dts-v1/; - - #include "stm32mp157c-ed1.dts" --#include --/* #include Remove due to BSD license issue */ -+#include "stm32mp15xx-evx.dtsi" - - / { - model = "STMicroelectronics STM32MP157C eval daughter on eval mother"; -@@ -19,346 +18,6 @@ - - aliases { - serial0 = &uart4; -- ethernet0 = ðernet0; -+ serial1 = &usart3; - }; -- -- clocks { -- clk_ext_camera: clk-ext-camera { -- #clock-cells = <0>; -- compatible = "fixed-clock"; -- clock-frequency = <24000000>; -- }; -- }; -- -- joystick { -- compatible = "gpio-keys"; -- pinctrl-0 = <&joystick_pins>; -- pinctrl-names = "default"; -- button-0 { -- label = "JoySel"; -- /* linux,code = ; BSD license issue */ -- interrupt-parent = <&stmfx_pinctrl>; -- interrupts = <0 IRQ_TYPE_EDGE_RISING>; -- }; -- button-1 { -- label = "JoyDown"; -- /* linux,code = ; BSD license issue */ -- interrupt-parent = <&stmfx_pinctrl>; -- interrupts = <1 IRQ_TYPE_EDGE_RISING>; -- }; -- button-2 { -- label = "JoyLeft"; -- /* linux,code = ; BSD license issue */ -- interrupt-parent = <&stmfx_pinctrl>; -- interrupts = <2 IRQ_TYPE_EDGE_RISING>; -- }; -- button-3 { -- label = "JoyRight"; -- /* linux,code = ; BSD license issue */ -- interrupt-parent = <&stmfx_pinctrl>; -- interrupts = <3 IRQ_TYPE_EDGE_RISING>; -- }; -- button-4 { -- label = "JoyUp"; -- /* linux,code = ; BSD license issue */ -- interrupt-parent = <&stmfx_pinctrl>; -- interrupts = <4 IRQ_TYPE_EDGE_RISING>; -- }; -- }; -- -- panel_backlight: panel-backlight { -- compatible = "gpio-backlight"; -- gpios = <&gpiod 13 GPIO_ACTIVE_LOW>; -- default-on; -- status = "okay"; -- }; --}; -- --&cec { -- pinctrl-names = "default"; -- pinctrl-0 = <&cec_pins_a>; -- status = "okay"; --}; -- --&dcmi { -- status = "okay"; -- pinctrl-names = "default", "sleep"; -- pinctrl-0 = <&dcmi_pins_a>; -- pinctrl-1 = <&dcmi_sleep_pins_a>; -- -- port { -- dcmi_0: endpoint { -- remote-endpoint = <&ov5640_0>; -- bus-width = <8>; -- hsync-active = <0>; -- vsync-active = <0>; -- pclk-sample = <1>; -- }; -- }; --}; -- --&dsi { -- #address-cells = <1>; -- #size-cells = <0>; -- phy-dsi-supply = <®18>; -- status = "okay"; -- -- ports { -- #address-cells = <1>; -- #size-cells = <0>; -- -- port@0 { -- reg = <0>; -- dsi_in: endpoint { -- remote-endpoint = <<dc_ep0_out>; -- }; -- }; -- -- port@1 { -- reg = <1>; -- dsi_out: endpoint { -- remote-endpoint = <&dsi_panel_in>; -- }; -- }; -- }; -- -- panel-dsi@0 { -- compatible = "raydium,rm68200"; -- reg = <0>; -- reset-gpios = <&gpiof 15 GPIO_ACTIVE_LOW>; -- backlight = <&panel_backlight>; -- power-supply = <&v3v3>; -- status = "okay"; -- -- port { -- dsi_panel_in: endpoint { -- remote-endpoint = <&dsi_out>; -- }; -- }; -- }; --}; -- --ðernet0 { -- status = "okay"; -- pinctrl-0 = <ðernet0_rgmii_pins_a>; -- pinctrl-1 = <ðernet0_rgmii_pins_sleep_a>; -- pinctrl-names = "default", "sleep"; -- phy-mode = "rgmii-id"; -- max-speed = <1000>; -- phy-handle = <&phy0>; -- -- mdio0 { -- #address-cells = <1>; -- #size-cells = <0>; -- compatible = "snps,dwmac-mdio"; -- phy0: ethernet-phy@0 { -- reg = <0>; -- }; -- }; --}; -- --&fmc { -- pinctrl-names = "default", "sleep"; -- pinctrl-0 = <&fmc_pins_a>; -- pinctrl-1 = <&fmc_sleep_pins_a>; -- status = "okay"; -- #address-cells = <1>; -- #size-cells = <0>; -- -- nand@0 { -- reg = <0>; -- nand-on-flash-bbt; -- #address-cells = <1>; -- #size-cells = <1>; -- }; --}; -- --&i2c2 { -- pinctrl-names = "default"; -- pinctrl-0 = <&i2c2_pins_a>; -- i2c-scl-rising-time-ns = <185>; -- i2c-scl-falling-time-ns = <20>; -- status = "okay"; -- -- ov5640: camera@3c { -- compatible = "ovti,ov5640"; -- reg = <0x3c>; -- clocks = <&clk_ext_camera>; -- clock-names = "xclk"; -- DOVDD-supply = <&v2v8>; -- powerdown-gpios = <&stmfx_pinctrl 18 (GPIO_ACTIVE_HIGH | GPIO_PUSH_PULL)>; -- reset-gpios = <&stmfx_pinctrl 19 (GPIO_ACTIVE_LOW | GPIO_PUSH_PULL)>; -- rotation = <180>; -- status = "okay"; -- -- port { -- ov5640_0: endpoint { -- remote-endpoint = <&dcmi_0>; -- bus-width = <8>; -- data-shift = <2>; /* lines 9:2 are used */ -- hsync-active = <0>; -- vsync-active = <0>; -- pclk-sample = <1>; -- }; -- }; -- }; -- -- stmfx: stmfx@42 { -- compatible = "st,stmfx-0300"; -- reg = <0x42>; -- interrupts = <8 IRQ_TYPE_EDGE_RISING>; -- interrupt-parent = <&gpioi>; -- vdd-supply = <&v3v3>; -- -- stmfx_pinctrl: stmfx-pin-controller { -- compatible = "st,stmfx-0300-pinctrl"; -- gpio-controller; -- #gpio-cells = <2>; -- interrupt-controller; -- #interrupt-cells = <2>; -- gpio-ranges = <&stmfx_pinctrl 0 0 24>; -- -- joystick_pins: joystick { -- pins = "gpio0", "gpio1", "gpio2", "gpio3", "gpio4"; -- bias-pull-down; -- }; -- }; -- }; --}; -- --&i2c5 { -- pinctrl-names = "default"; -- pinctrl-0 = <&i2c5_pins_a>; -- i2c-scl-rising-time-ns = <185>; -- i2c-scl-falling-time-ns = <20>; -- status = "okay"; --}; -- --<dc { -- status = "okay"; -- -- port { -- #address-cells = <1>; -- #size-cells = <0>; -- -- ltdc_ep0_out: endpoint@0 { -- reg = <0>; -- remote-endpoint = <&dsi_in>; -- }; -- }; --}; -- --&m_can1 { -- pinctrl-names = "default", "sleep"; -- pinctrl-0 = <&m_can1_pins_a>; -- pinctrl-1 = <&m_can1_sleep_pins_a>; -- status = "okay"; --}; -- --&qspi { -- pinctrl-names = "default", "sleep"; -- pinctrl-0 = <&qspi_clk_pins_a &qspi_bk1_pins_a &qspi_bk2_pins_a>; -- pinctrl-1 = <&qspi_clk_sleep_pins_a &qspi_bk1_sleep_pins_a &qspi_bk2_sleep_pins_a>; -- reg = <0x58003000 0x1000>, <0x70000000 0x4000000>; -- #address-cells = <1>; -- #size-cells = <0>; -- status = "okay"; -- -- flash0: mx66l51235l@0 { -- compatible = "jedec,spi-nor"; -- reg = <0>; -- spi-rx-bus-width = <4>; -- spi-max-frequency = <108000000>; -- #address-cells = <1>; -- #size-cells = <1>; -- }; -- -- flash1: mx66l51235l@1 { -- compatible = "jedec,spi-nor"; -- reg = <1>; -- spi-rx-bus-width = <4>; -- spi-max-frequency = <108000000>; -- #address-cells = <1>; -- #size-cells = <1>; -- }; --}; -- --&sdmmc3 { -- pinctrl-names = "default", "opendrain", "sleep"; -- pinctrl-0 = <&sdmmc3_b4_pins_a>; -- pinctrl-1 = <&sdmmc3_b4_od_pins_a>; -- pinctrl-2 = <&sdmmc3_b4_sleep_pins_a>; -- broken-cd; -- st,neg-edge; -- bus-width = <4>; -- vmmc-supply = <&v3v3>; -- status = "disabled"; --}; -- --&spi1 { -- pinctrl-names = "default"; -- pinctrl-0 = <&spi1_pins_a>; -- status = "disabled"; --}; -- --&timers2 { -- /* spare dmas for other usage (un-delete to enable pwm capture) */ -- /delete-property/dmas; -- /delete-property/dma-names; -- status = "disabled"; -- pwm { -- pinctrl-0 = <&pwm2_pins_a>; -- pinctrl-1 = <&pwm2_sleep_pins_a>; -- pinctrl-names = "default", "sleep"; -- status = "okay"; -- }; -- timer@1 { -- status = "okay"; -- }; --}; -- --&timers8 { -- /delete-property/dmas; -- /delete-property/dma-names; -- status = "disabled"; -- pwm { -- pinctrl-0 = <&pwm8_pins_a>; -- pinctrl-1 = <&pwm8_sleep_pins_a>; -- pinctrl-names = "default", "sleep"; -- status = "okay"; -- }; -- timer@7 { -- status = "okay"; -- }; --}; -- --&timers12 { -- /delete-property/dmas; -- /delete-property/dma-names; -- status = "disabled"; -- pwm { -- pinctrl-0 = <&pwm12_pins_a>; -- pinctrl-1 = <&pwm12_sleep_pins_a>; -- pinctrl-names = "default", "sleep"; -- status = "okay"; -- }; -- timer@11 { -- status = "okay"; -- }; --}; -- --&usbh_ehci { -- phys = <&usbphyc_port0>; -- status = "okay"; --}; -- --&usbotg_hs { -- dr_mode = "peripheral"; -- phys = <&usbphyc_port1 0>; -- phy-names = "usb2-phy"; -- status = "okay"; --}; -- --&usbphyc { -- status = "okay"; - }; -diff --git a/core/arch/arm/dts/stm32mp157d-dk1.dts b/core/arch/arm/dts/stm32mp157d-dk1.dts -new file mode 100644 -index 000000000..79297b831 ---- /dev/null -+++ b/core/arch/arm/dts/stm32mp157d-dk1.dts -@@ -0,0 +1,45 @@ -+// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) -+/* -+ * Copyright (C) STMicroelectronics 2019 - All Rights Reserved -+ * Author: Alexandre Torgue for STMicroelectronics. -+ */ -+ -+/dts-v1/; -+ -+#include "stm32mp157.dtsi" -+#include "stm32mp15xd.dtsi" -+#include "stm32mp15-pinctrl.dtsi" -+#include "stm32mp15xxac-pinctrl.dtsi" -+#include "stm32mp15xx-dkx.dtsi" -+#include -+ -+/ { -+ model = "STMicroelectronics STM32MP157D-DK1 Discovery Board"; -+ compatible = "st,stm32mp157d-dk1", "st,stm32mp157"; -+ -+ aliases { -+ serial0 = &uart4; -+ serial1 = &usart3; -+ serial2 = &uart7; -+ }; -+ -+ chosen { -+ stdout-path = "serial0:115200n8"; -+ }; -+}; -+ -+&etzpc { -+ st,decprot = < -+ DECPROT(STM32MP1_ETZPC_USART1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_SPI6_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_I2C4_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_I2C6_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_RNG1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_HASH1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_DDRCTRL_ID, DECPROT_NS_R_S_W, DECPROT_LOCK) -+ DECPROT(STM32MP1_ETZPC_DDRPHYC_ID, DECPROT_NS_R_S_W, DECPROT_LOCK) -+ DECPROT(STM32MP1_ETZPC_STGENC_ID, DECPROT_S_RW, DECPROT_LOCK) -+ DECPROT(STM32MP1_ETZPC_BKPSRAM_ID, DECPROT_S_RW, DECPROT_LOCK) -+ DECPROT(STM32MP1_ETZPC_IWDG1_ID, DECPROT_S_RW, DECPROT_LOCK) -+ >; -+}; -diff --git a/core/arch/arm/dts/stm32mp157d-ed1.dts b/core/arch/arm/dts/stm32mp157d-ed1.dts -new file mode 100644 -index 000000000..2c67ec0ac ---- /dev/null -+++ b/core/arch/arm/dts/stm32mp157d-ed1.dts -@@ -0,0 +1,39 @@ -+// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) -+/* -+ * Copyright (C) STMicroelectronics 2019 - All Rights Reserved -+ * Author: Alexandre Torgue for STMicroelectronics. -+ */ -+/dts-v1/; -+ -+#include "stm32mp157.dtsi" -+#include "stm32mp15xd.dtsi" -+#include "stm32mp15-pinctrl.dtsi" -+#include "stm32mp15xxaa-pinctrl.dtsi" -+#include "stm32mp15xx-edx.dtsi" -+#include -+ -+/ { -+ model = "STMicroelectronics STM32MP157D eval daughter"; -+ compatible = "st,stm32mp157d-ed1", "st,stm32mp157"; -+ -+ chosen { -+ stdout-path = "serial0:115200n8"; -+ }; -+}; -+ -+ -+&etzpc { -+ st,decprot = < -+ DECPROT(STM32MP1_ETZPC_USART1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_SPI6_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_I2C4_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_I2C6_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_RNG1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_HASH1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_DDRCTRL_ID, DECPROT_NS_R_S_W, DECPROT_LOCK) -+ DECPROT(STM32MP1_ETZPC_DDRPHYC_ID, DECPROT_NS_R_S_W, DECPROT_LOCK) -+ DECPROT(STM32MP1_ETZPC_STGENC_ID, DECPROT_S_RW, DECPROT_LOCK) -+ DECPROT(STM32MP1_ETZPC_BKPSRAM_ID, DECPROT_S_RW, DECPROT_LOCK) -+ DECPROT(STM32MP1_ETZPC_IWDG1_ID, DECPROT_S_RW, DECPROT_LOCK) -+ >; -+}; -diff --git a/core/arch/arm/dts/stm32mp157d-ev1.dts b/core/arch/arm/dts/stm32mp157d-ev1.dts -new file mode 100644 -index 000000000..4a40f5fe5 ---- /dev/null -+++ b/core/arch/arm/dts/stm32mp157d-ev1.dts -@@ -0,0 +1,23 @@ -+// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) -+/* -+ * Copyright (C) STMicroelectronics 2019 - All Rights Reserved -+ * Author: Alexandre Torgue for STMicroelectronics. -+ */ -+/dts-v1/; -+ -+#include "stm32mp157d-ed1.dts" -+#include "stm32mp15xx-evx.dtsi" -+ -+/ { -+ model = "STMicroelectronics STM32MP157D eval daughter on eval mother"; -+ compatible = "st,stm32mp157d-ev1", "st,stm32mp157d-ed1", "st,stm32mp157"; -+ -+ chosen { -+ stdout-path = "serial0:115200n8"; -+ }; -+ -+ aliases { -+ serial0 = &uart4; -+ serial1 = &usart3; -+ }; -+}; -diff --git a/core/arch/arm/dts/stm32mp157f-dk2.dts b/core/arch/arm/dts/stm32mp157f-dk2.dts -new file mode 100644 -index 000000000..680ca0f6e ---- /dev/null -+++ b/core/arch/arm/dts/stm32mp157f-dk2.dts -@@ -0,0 +1,51 @@ -+// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) -+/* -+ * Copyright (C) STMicroelectronics 2019 - All Rights Reserved -+ * Author: Alexandre Torgue for STMicroelectronics. -+ */ -+ -+/dts-v1/; -+ -+#include "stm32mp157.dtsi" -+#include "stm32mp15xf.dtsi" -+#include "stm32mp15-pinctrl.dtsi" -+#include "stm32mp15xxac-pinctrl.dtsi" -+#include "stm32mp15xx-dkx.dtsi" -+#include -+ -+/ { -+ model = "STMicroelectronics STM32MP157F-DK2 Discovery Board"; -+ compatible = "st,stm32mp157f-dk2", "st,stm32mp157"; -+ -+ aliases { -+ serial0 = &uart4; -+ serial1 = &usart3; -+ serial2 = &uart7; -+ serial3 = &usart2; -+ }; -+ -+ chosen { -+ stdout-path = "serial0:115200n8"; -+ }; -+}; -+ -+&cryp1 { -+ status = "okay"; -+}; -+ -+&etzpc { -+ st,decprot = < -+ DECPROT(STM32MP1_ETZPC_USART1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_SPI6_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_I2C4_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_I2C6_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_RNG1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_HASH1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_CRYP1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_DDRCTRL_ID, DECPROT_NS_R_S_W, DECPROT_LOCK) -+ DECPROT(STM32MP1_ETZPC_DDRPHYC_ID, DECPROT_NS_R_S_W, DECPROT_LOCK) -+ DECPROT(STM32MP1_ETZPC_STGENC_ID, DECPROT_S_RW, DECPROT_LOCK) -+ DECPROT(STM32MP1_ETZPC_BKPSRAM_ID, DECPROT_S_RW, DECPROT_LOCK) -+ DECPROT(STM32MP1_ETZPC_IWDG1_ID, DECPROT_S_RW, DECPROT_LOCK) -+ >; -+}; -diff --git a/core/arch/arm/dts/stm32mp157f-ed1.dts b/core/arch/arm/dts/stm32mp157f-ed1.dts -new file mode 100644 -index 000000000..1aa26cdbb ---- /dev/null -+++ b/core/arch/arm/dts/stm32mp157f-ed1.dts -@@ -0,0 +1,43 @@ -+// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) -+/* -+ * Copyright (C) STMicroelectronics 2019 - All Rights Reserved -+ * Author: Alexandre Torgue for STMicroelectronics. -+ */ -+/dts-v1/; -+ -+#include "stm32mp157.dtsi" -+#include "stm32mp15xf.dtsi" -+#include "stm32mp15-pinctrl.dtsi" -+#include "stm32mp15xxaa-pinctrl.dtsi" -+#include "stm32mp15xx-edx.dtsi" -+#include -+ -+/ { -+ model = "STMicroelectronics STM32MP157F eval daughter"; -+ compatible = "st,stm32mp157f-ed1", "st,stm32mp157"; -+ -+ chosen { -+ stdout-path = "serial0:115200n8"; -+ }; -+}; -+ -+&cryp1 { -+ status = "okay"; -+}; -+ -+&etzpc { -+ st,decprot = < -+ DECPROT(STM32MP1_ETZPC_USART1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_SPI6_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_I2C4_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_I2C6_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_RNG1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_HASH1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_CRYP1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) -+ DECPROT(STM32MP1_ETZPC_DDRCTRL_ID, DECPROT_NS_R_S_W, DECPROT_LOCK) -+ DECPROT(STM32MP1_ETZPC_DDRPHYC_ID, DECPROT_NS_R_S_W, DECPROT_LOCK) -+ DECPROT(STM32MP1_ETZPC_STGENC_ID, DECPROT_S_RW, DECPROT_LOCK) -+ DECPROT(STM32MP1_ETZPC_BKPSRAM_ID, DECPROT_S_RW, DECPROT_LOCK) -+ DECPROT(STM32MP1_ETZPC_IWDG1_ID, DECPROT_S_RW, DECPROT_LOCK) -+ >; -+}; -diff --git a/core/arch/arm/dts/stm32mp157f-ev1.dts b/core/arch/arm/dts/stm32mp157f-ev1.dts -new file mode 100644 -index 000000000..caf5dfe11 ---- /dev/null -+++ b/core/arch/arm/dts/stm32mp157f-ev1.dts -@@ -0,0 +1,23 @@ -+// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) -+/* -+ * Copyright (C) STMicroelectronics 2019 - All Rights Reserved -+ * Author: Alexandre Torgue for STMicroelectronics. -+ */ -+/dts-v1/; -+ -+#include "stm32mp157f-ed1.dts" -+#include "stm32mp15xx-evx.dtsi" -+ -+/ { -+ model = "STMicroelectronics STM32MP157F eval daughter on eval mother"; -+ compatible = "st,stm32mp157f-ev1", "st,stm32mp157f-ed1", "st,stm32mp157"; -+ -+ chosen { -+ stdout-path = "serial0:115200n8"; -+ }; -+ -+ aliases { -+ serial0 = &uart4; -+ serial1 = &usart3; -+ }; -+}; -diff --git a/core/arch/arm/dts/stm32mp15xa.dtsi b/core/arch/arm/dts/stm32mp15xa.dtsi -new file mode 100644 -index 000000000..5ed7e594f ---- /dev/null -+++ b/core/arch/arm/dts/stm32mp15xa.dtsi -@@ -0,0 +1,13 @@ -+// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) -+/* -+ * Copyright (C) STMicroelectronics 2019 - All Rights Reserved -+ * Author: Alexandre Torgue for STMicroelectronics. -+ */ -+ -+&cpu0_opp_table { -+ opp-650000000 { -+ opp-hz = /bits/ 64 <650000000>; -+ opp-microvolt = <1200000>; -+ opp-supported-hw = <0x1>; -+ }; -+}; -diff --git a/core/arch/arm/dts/stm32mp15xc.dtsi b/core/arch/arm/dts/stm32mp15xc.dtsi -index b06a55a2f..68d822d8c 100644 ---- a/core/arch/arm/dts/stm32mp15xc.dtsi -+++ b/core/arch/arm/dts/stm32mp15xc.dtsi -@@ -4,6 +4,8 @@ - * Author: Alexandre Torgue for STMicroelectronics. - */ - -+#include "stm32mp15xa.dtsi" -+ - / { - soc { - cryp1: cryp@54001000 { -@@ -13,6 +15,7 @@ - clocks = <&rcc CRYP1>; - resets = <&rcc CRYP1_R>; - status = "disabled"; -+ secure-status = "disabled"; - }; - }; - }; -diff --git a/core/arch/arm/dts/stm32mp15xd.dtsi b/core/arch/arm/dts/stm32mp15xd.dtsi -new file mode 100644 -index 000000000..18b05ee38 ---- /dev/null -+++ b/core/arch/arm/dts/stm32mp15xd.dtsi -@@ -0,0 +1,19 @@ -+// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) -+/* -+ * Copyright (C) STMicroelectronics 2019 - All Rights Reserved -+ * Author: Alexandre Torgue for STMicroelectronics. -+ */ -+ -+&cpu0_opp_table { -+ opp-800000000 { -+ opp-hz = /bits/ 64 <800000000>; -+ opp-microvolt = <1350000>; -+ opp-supported-hw = <0x2>; -+ }; -+ opp-400000000 { -+ opp-hz = /bits/ 64 <400000000>; -+ opp-microvolt = <1200000>; -+ opp-supported-hw = <0x2>; -+ opp-suspend; -+ }; -+}; -diff --git a/core/arch/arm/dts/stm32mp15xf.dtsi b/core/arch/arm/dts/stm32mp15xf.dtsi -new file mode 100644 -index 000000000..526a1627c ---- /dev/null -+++ b/core/arch/arm/dts/stm32mp15xf.dtsi -@@ -0,0 +1,21 @@ -+// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) -+/* -+ * Copyright (C) STMicroelectronics 2019 - All Rights Reserved -+ * Author: Alexandre Torgue for STMicroelectronics. -+ */ -+ -+#include "stm32mp15xd.dtsi" -+ -+/ { -+ soc { -+ cryp1: cryp@54001000 { -+ compatible = "st,stm32mp1-cryp"; -+ reg = <0x54001000 0x400>; -+ interrupts = ; -+ clocks = <&rcc CRYP1>; -+ resets = <&rcc CRYP1_R>; -+ status = "disabled"; -+ secure-status = "disabled"; -+ }; -+ }; -+}; -diff --git a/core/arch/arm/dts/stm32mp15xx-dkx.dtsi b/core/arch/arm/dts/stm32mp15xx-dkx.dtsi -index f6672e87a..236cff932 100644 ---- a/core/arch/arm/dts/stm32mp15xx-dkx.dtsi -+++ b/core/arch/arm/dts/stm32mp15xx-dkx.dtsi -@@ -4,8 +4,8 @@ - * Author: Alexandre Torgue for STMicroelectronics. - */ - --#include --#include -+#include -+#include - - / { - memory@c0000000 { -@@ -13,206 +13,36 @@ - reg = <0xc0000000 0x20000000>; - }; - -- reserved-memory { -- #address-cells = <1>; -- #size-cells = <1>; -- ranges; -- -- mcuram2: mcuram2@10000000 { -- compatible = "shared-dma-pool"; -- reg = <0x10000000 0x40000>; -- no-map; -- }; -- -- vdev0vring0: vdev0vring0@10040000 { -- compatible = "shared-dma-pool"; -- reg = <0x10040000 0x1000>; -- no-map; -- }; -- -- vdev0vring1: vdev0vring1@10041000 { -- compatible = "shared-dma-pool"; -- reg = <0x10041000 0x1000>; -- no-map; -- }; -- -- vdev0buffer: vdev0buffer@10042000 { -- compatible = "shared-dma-pool"; -- reg = <0x10042000 0x4000>; -- no-map; -- }; -- -- mcuram: mcuram@30000000 { -- compatible = "shared-dma-pool"; -- reg = <0x30000000 0x40000>; -- no-map; -- }; -- -- retram: retram@38000000 { -- compatible = "shared-dma-pool"; -- reg = <0x38000000 0x10000>; -- no-map; -- }; -- -- gpu_reserved: gpu@d4000000 { -- reg = <0xd4000000 0x4000000>; -- no-map; -- }; -- }; -- -- led { -- compatible = "gpio-leds"; -- blue { -- label = "heartbeat"; -- gpios = <&gpiod 11 GPIO_ACTIVE_HIGH>; -- linux,default-trigger = "heartbeat"; -- default-state = "off"; -- }; -- }; -- -- sound { -- compatible = "audio-graph-card"; -- label = "STM32MP1-DK"; -- routing = -- "Playback" , "MCLK", -- "Capture" , "MCLK", -- "MICL" , "Mic Bias"; -- dais = <&sai2a_port &sai2b_port &i2s2_port>; -- status = "okay"; -+ vin: vin { -+ compatible = "regulator-fixed"; -+ regulator-name = "vin"; -+ regulator-min-microvolt = <5000000>; -+ regulator-max-microvolt = <5000000>; -+ regulator-always-on; - }; - }; - --&adc { -- pinctrl-names = "default"; -- pinctrl-0 = <&adc12_ain_pins_a>, <&adc12_usb_cc_pins_a>; -- vdd-supply = <&vdd>; -- vdda-supply = <&vdd>; -- vref-supply = <&vrefbuf>; -- status = "disabled"; -- adc1: adc@0 { -- /* -- * Type-C USB_PWR_CC1 & USB_PWR_CC2 on in18 & in19. -- * Use at least 5 * RC time, e.g. 5 * (Rp + Rd) * C: -- * 5 * (56 + 47kOhms) * 5pF => 2.5us. -- * Use arbitrary margin here (e.g. 5us). -- */ -- st,min-sample-time-nsecs = <5000>; -- /* AIN connector, USB Type-C CC1 & CC2 */ -- st,adc-channels = <0 1 6 13 18 19>; -- status = "okay"; -- }; -- adc2: adc@100 { -- /* AIN connector, USB Type-C CC1 & CC2 */ -- st,adc-channels = <0 1 2 6 18 19>; -- st,min-sample-time-nsecs = <5000>; -- status = "okay"; -+&bsec { -+ board_id: board_id@ec { -+ reg = <0xec 0x4>; -+ st,non-secure-otp; - }; - }; - --&cec { -- pinctrl-names = "default", "sleep"; -- pinctrl-0 = <&cec_pins_b>; -- pinctrl-1 = <&cec_pins_sleep_b>; -- status = "okay"; -+&clk_hse { -+ st,digbypass; - }; - --ðernet0 { -- status = "okay"; -- pinctrl-0 = <ðernet0_rgmii_pins_a>; -- pinctrl-1 = <ðernet0_rgmii_pins_sleep_a>; -- pinctrl-names = "default", "sleep"; -- phy-mode = "rgmii-id"; -- max-speed = <1000>; -- phy-handle = <&phy0>; -- -- mdio0 { -- #address-cells = <1>; -- #size-cells = <0>; -- compatible = "snps,dwmac-mdio"; -- phy0: ethernet-phy@0 { -- reg = <0>; -- }; -- }; -+&cpu0{ -+ cpu-supply = <&vddcore>; - }; - --&gpu { -- contiguous-area = <&gpu_reserved>; -- status = "okay"; -+&cpu1{ -+ cpu-supply = <&vddcore>; - }; - --&i2c1 { -- pinctrl-names = "default", "sleep"; -- pinctrl-0 = <&i2c1_pins_a>; -- pinctrl-1 = <&i2c1_pins_sleep_a>; -- i2c-scl-rising-time-ns = <100>; -- i2c-scl-falling-time-ns = <7>; -+&hash1 { - status = "okay"; -- /delete-property/dmas; -- /delete-property/dma-names; -- -- hdmi-transmitter@39 { -- compatible = "sil,sii9022"; -- reg = <0x39>; -- iovcc-supply = <&v3v3_hdmi>; -- cvcc12-supply = <&v1v2_hdmi>; -- reset-gpios = <&gpioa 10 GPIO_ACTIVE_LOW>; -- interrupts = <1 IRQ_TYPE_EDGE_FALLING>; -- interrupt-parent = <&gpiog>; -- #sound-dai-cells = <0>; -- status = "okay"; -- -- ports { -- #address-cells = <1>; -- #size-cells = <0>; -- -- port@0 { -- reg = <0>; -- sii9022_in: endpoint { -- remote-endpoint = <<dc_ep0_out>; -- }; -- }; -- -- port@3 { -- reg = <3>; -- sii9022_tx_endpoint: endpoint { -- remote-endpoint = <&i2s2_endpoint>; -- }; -- }; -- }; -- }; -- -- cs42l51: cs42l51@4a { -- compatible = "cirrus,cs42l51"; -- reg = <0x4a>; -- #sound-dai-cells = <0>; -- VL-supply = <&v3v3>; -- VD-supply = <&v1v8_audio>; -- VA-supply = <&v1v8_audio>; -- VAHP-supply = <&v1v8_audio>; -- reset-gpios = <&gpiog 9 GPIO_ACTIVE_LOW>; -- clocks = <&sai2a>; -- clock-names = "MCLK"; -- status = "okay"; -- -- cs42l51_port: port { -- #address-cells = <1>; -- #size-cells = <0>; -- -- cs42l51_tx_endpoint: endpoint@0 { -- reg = <0>; -- remote-endpoint = <&sai2a_endpoint>; -- frame-master; -- bitclock-master; -- }; -- -- cs42l51_rx_endpoint: endpoint@1 { -- reg = <1>; -- remote-endpoint = <&sai2b_endpoint>; -- frame-master; -- bitclock-master; -- }; -- }; -- }; - }; - - &i2c4 { -@@ -220,24 +50,33 @@ - pinctrl-0 = <&i2c4_pins_a>; - i2c-scl-rising-time-ns = <185>; - i2c-scl-falling-time-ns = <20>; -+ clock-frequency = <400000>; - status = "okay"; -- /* spare dmas for other usage */ -- /delete-property/dmas; -- /delete-property/dma-names; -+ secure-status = "okay"; - - pmic: stpmic@33 { - compatible = "st,stpmic1"; - reg = <0x33>; -- interrupts-extended = <&gpioa 0 IRQ_TYPE_EDGE_FALLING>; -+ interrupts-extended = <&exti_pwr 55 IRQ_TYPE_EDGE_FALLING>; - interrupt-controller; - #interrupt-cells = <2>; - status = "okay"; -+ secure-status = "okay"; - - regulators { - compatible = "st,stpmic1-regulators"; -+ buck1-supply = <&vin>; -+ buck2-supply = <&vin>; -+ buck3-supply = <&vin>; -+ buck4-supply = <&vin>; - ldo1-supply = <&v3v3>; -+ ldo2-supply = <&vin>; - ldo3-supply = <&vdd_ddr>; -+ ldo4-supply = <&vin>; -+ ldo5-supply = <&vin>; - ldo6-supply = <&v3v3>; -+ vref_ddr-supply = <&vin>; -+ boost-supply = <&vin>; - pwr_sw1-supply = <&bst_out>; - pwr_sw2-supply = <&bst_out>; - -@@ -283,7 +122,6 @@ - regulator-min-microvolt = <1800000>; - regulator-max-microvolt = <1800000>; - regulator-always-on; -- interrupts = ; - }; - - v3v3_hdmi: ldo2 { -@@ -291,7 +129,6 @@ - regulator-min-microvolt = <3300000>; - regulator-max-microvolt = <3300000>; - regulator-always-on; -- interrupts = ; - }; - - vtt_ddr: ldo3 { -@@ -304,16 +141,12 @@ - - vdd_usb: ldo4 { - regulator-name = "vdd_usb"; -- regulator-min-microvolt = <3300000>; -- regulator-max-microvolt = <3300000>; -- interrupts = ; - }; - - vdda: ldo5 { - regulator-name = "vdda"; - regulator-min-microvolt = <2900000>; - regulator-max-microvolt = <2900000>; -- interrupts = ; - regulator-boot-on; - }; - -@@ -322,7 +155,6 @@ - regulator-min-microvolt = <1200000>; - regulator-max-microvolt = <1200000>; - regulator-always-on; -- interrupts = ; - }; - - vref_ddr: vref_ddr { -@@ -331,295 +163,339 @@ - regulator-over-current-protection; - }; - -- bst_out: boost { -+ bst_out: boost { - regulator-name = "bst_out"; -- interrupts = ; -- }; -+ }; - - vbus_otg: pwr_sw1 { - regulator-name = "vbus_otg"; -- interrupts = ; -- }; -+ }; - -- vbus_sw: pwr_sw2 { -+ vbus_sw: pwr_sw2 { - regulator-name = "vbus_sw"; -- interrupts = ; - regulator-active-discharge = <1>; -- }; -- }; -- -- onkey { -- compatible = "st,stpmic1-onkey"; -- interrupts = , ; -- interrupt-names = "onkey-falling", "onkey-rising"; -- power-off-time-sec = <10>; -- status = "okay"; -- }; -- -- watchdog { -- compatible = "st,stpmic1-wdt"; -- status = "disabled"; -+ }; - }; - }; - }; - --&i2s2 { -- clocks = <&rcc SPI2>, <&rcc SPI2_K>, <&rcc PLL3_Q>, <&rcc PLL3_R>; -- clock-names = "pclk", "i2sclk", "x8k", "x11k"; -- pinctrl-names = "default", "sleep"; -- pinctrl-0 = <&i2s2_pins_a>; -- pinctrl-1 = <&i2s2_pins_sleep_a>; -+&iwdg2 { -+ timeout-sec = <32>; -+ secure-timeout-sec = <5>; - status = "okay"; -- -- i2s2_port: port { -- i2s2_endpoint: endpoint { -- remote-endpoint = <&sii9022_tx_endpoint>; -- format = "i2s"; -- mclk-fs = <256>; -- }; -- }; -+ secure-status = "okay"; - }; - --&ipcc { -- status = "okay"; -+&nvmem_layout { -+ nvmem-cells = <&cfg0_otp>, -+ <&part_number_otp>, -+ <&monotonic_otp>, -+ <&nand_otp>, -+ <&uid_otp>, -+ <&package_otp>, -+ <&hw2_otp>, -+ <&pkh_otp>, -+ <&board_id>; -+ -+ nvmem-cell-names = "cfg0_otp", -+ "part_number_otp", -+ "monotonic_otp", -+ "nand_otp", -+ "uid_otp", -+ "package_otp", -+ "hw2_otp", -+ "pkh_otp", -+ "board_id"; - }; - --&iwdg2 { -- timeout-sec = <32>; -- status = "okay"; -+&pwr_regulators { -+ system_suspend_supported_soc_modes = < -+ STM32_PM_CSLEEP_RUN -+ STM32_PM_CSTOP_ALLOW_LP_STOP -+ STM32_PM_CSTOP_ALLOW_STANDBY_DDR_SR -+ >; -+ system_off_soc_mode = ; -+ vdd-supply = <&vdd>; -+ vdd_3v3_usbfs-supply = <&vdd_usb>; - }; - --<dc { -- pinctrl-names = "default", "sleep"; -- pinctrl-0 = <<dc_pins_a>; -- pinctrl-1 = <<dc_pins_sleep_a>; -- status = "okay"; -+&rcc { -+ st,hsi-cal; -+ st,csi-cal; -+ st,cal-sec = <60>; -+ st,clksrc = < -+ CLK_MPU_PLL1P -+ CLK_AXI_PLL2P -+ CLK_MCU_PLL3P -+ CLK_PLL12_HSE -+ CLK_PLL3_HSE -+ CLK_PLL4_HSE -+ CLK_RTC_LSE -+ CLK_MCO1_DISABLED -+ CLK_MCO2_DISABLED -+ >; -+ -+ st,clkdiv = < -+ 1 /*MPU*/ -+ 0 /*AXI*/ -+ 0 /*MCU*/ -+ 1 /*APB1*/ -+ 1 /*APB2*/ -+ 1 /*APB3*/ -+ 1 /*APB4*/ -+ 2 /*APB5*/ -+ 23 /*RTC*/ -+ 0 /*MCO1*/ -+ 0 /*MCO2*/ -+ >; -+ -+ st,pkcs = < -+ CLK_CKPER_HSE -+ CLK_FMC_ACLK -+ CLK_QSPI_ACLK -+ CLK_ETH_DISABLED -+ CLK_SDMMC12_PLL4P -+ CLK_DSI_DSIPLL -+ CLK_STGEN_HSE -+ CLK_USBPHY_HSE -+ CLK_SPI2S1_PLL3Q -+ CLK_SPI2S23_PLL3Q -+ CLK_SPI45_HSI -+ CLK_SPI6_HSI -+ CLK_I2C46_HSI -+ CLK_SDMMC3_PLL4P -+ CLK_USBO_USBPHY -+ CLK_ADC_CKPER -+ CLK_CEC_LSE -+ CLK_I2C12_HSI -+ CLK_I2C35_HSI -+ CLK_UART1_HSI -+ CLK_UART24_HSI -+ CLK_UART35_HSI -+ CLK_UART6_HSI -+ CLK_UART78_HSI -+ CLK_SPDIF_PLL4P -+ CLK_FDCAN_PLL4R -+ CLK_SAI1_PLL3Q -+ CLK_SAI2_PLL3Q -+ CLK_SAI3_PLL3Q -+ CLK_SAI4_PLL3Q -+ CLK_RNG1_LSI -+ CLK_RNG2_LSI -+ CLK_LPTIM1_PCLK1 -+ CLK_LPTIM23_PCLK3 -+ CLK_LPTIM45_LSE -+ >; -+ -+ /* VCO = 1066.0 MHz => P = 266 (AXI), Q = 533 (GPU), R = 533 (DDR) */ -+ pll2: st,pll@1 { -+ compatible = "st,stm32mp1-pll"; -+ reg = <1>; -+ cfg = <2 65 1 0 0 PQR(1,1,1)>; -+ frac = <0x1400>; -+ }; - -- port { -- #address-cells = <1>; -- #size-cells = <0>; -+ /* VCO = 417.8 MHz => P = 209, Q = 24, R = 11 */ -+ pll3: st,pll@2 { -+ compatible = "st,stm32mp1-pll"; -+ reg = <2>; -+ cfg = <1 33 1 16 36 PQR(1,1,1)>; -+ frac = <0x1a04>; -+ }; - -- ltdc_ep0_out: endpoint@0 { -- reg = <0>; -- remote-endpoint = <&sii9022_in>; -- }; -+ /* VCO = 594.0 MHz => P = 99, Q = 74, R = 74 */ -+ pll4: st,pll@3 { -+ compatible = "st,stm32mp1-pll"; -+ reg = <3>; -+ cfg = <3 98 5 7 7 PQR(1,1,1)>; - }; - }; - --&m4_rproc { -- memory-region = <&retram>, <&mcuram>, <&mcuram2>, <&vdev0vring0>, -- <&vdev0vring1>, <&vdev0buffer>; -- mboxes = <&ipcc 0>, <&ipcc 1>, <&ipcc 2>; -- mbox-names = "vq0", "vq1", "shutdown"; -- interrupt-parent = <&exti>; -- interrupts = <68 1>; -+&rng1 { - status = "okay"; -+ secure-status = "okay"; - }; - --&pwr_regulators { -- vdd-supply = <&vdd>; -- vdd_3v3_usbfs-supply = <&vdd_usb>; -+&rtc { -+ status = "okay"; -+ secure-status = "okay"; - }; - --&rng1 { -+&sdmmc1 { -+ pinctrl-names = "default"; -+ pinctrl-0 = <&sdmmc1_b4_pins_a>; -+ disable-wp; -+ st,neg-edge; -+ bus-width = <4>; -+ vmmc-supply = <&v3v3>; - status = "okay"; - }; - --&rtc { -- status = "okay"; -+&timers15 { -+ secure-status = "okay"; -+ st,hsi-cal-input = <7>; -+ st,csi-cal-input = <8>; - }; - --&sai2 { -- clocks = <&rcc SAI2>, <&rcc PLL3_Q>, <&rcc PLL3_R>; -- clock-names = "pclk", "x8k", "x11k"; -- pinctrl-names = "default", "sleep"; -- pinctrl-0 = <&sai2a_pins_a>, <&sai2b_pins_b>; -- pinctrl-1 = <&sai2a_sleep_pins_a>, <&sai2b_sleep_pins_b>; -+&uart4 { -+ pinctrl-names = "default"; -+ pinctrl-0 = <&uart4_pins_a>; - status = "okay"; -+}; - -- sai2a: audio-controller@4400b004 { -- #clock-cells = <0>; -- dma-names = "tx"; -- clocks = <&rcc SAI2_K>; -- clock-names = "sai_ck"; -- status = "okay"; -- -- sai2a_port: port { -- sai2a_endpoint: endpoint { -- remote-endpoint = <&cs42l51_tx_endpoint>; -- format = "i2s"; -- mclk-fs = <256>; -- dai-tdm-slot-num = <2>; -- dai-tdm-slot-width = <32>; -- }; -- }; -- }; -+&uart7 { -+ pinctrl-names = "default"; -+ pinctrl-0 = <&uart7_pins_c>; -+ status = "disabled"; -+}; - -- sai2b: audio-controller@4400b024 { -- dma-names = "rx"; -- st,sync = <&sai2a 2>; -- clocks = <&rcc SAI2_K>, <&sai2a>; -- clock-names = "sai_ck", "MCLK"; -- status = "okay"; -+&usart3 { -+ pinctrl-names = "default"; -+ pinctrl-0 = <&usart3_pins_c>; -+ uart-has-rtscts; -+ status = "disabled"; -+}; - -- sai2b_port: port { -- sai2b_endpoint: endpoint { -- remote-endpoint = <&cs42l51_rx_endpoint>; -- format = "i2s"; -- mclk-fs = <256>; -- dai-tdm-slot-num = <2>; -- dai-tdm-slot-width = <32>; -- }; -- }; -- }; -+&usbotg_hs { -+ phys = <&usbphyc_port1 0>; -+ phy-names = "usb2-phy"; -+ usb-role-switch; -+ status = "okay"; - }; - --&sdmmc1 { -- pinctrl-names = "default", "opendrain", "sleep"; -- pinctrl-0 = <&sdmmc1_b4_pins_a>; -- pinctrl-1 = <&sdmmc1_b4_od_pins_a>; -- pinctrl-2 = <&sdmmc1_b4_sleep_pins_a>; -- broken-cd; -- st,neg-edge; -- bus-width = <4>; -- vmmc-supply = <&v3v3>; -+&usbphyc { - status = "okay"; - }; - --&sdmmc3 { -- pinctrl-names = "default", "opendrain", "sleep"; -- pinctrl-0 = <&sdmmc3_b4_pins_a>; -- pinctrl-1 = <&sdmmc3_b4_od_pins_a>; -- pinctrl-2 = <&sdmmc3_b4_sleep_pins_a>; -- broken-cd; -- st,neg-edge; -- bus-width = <4>; -- vmmc-supply = <&v3v3>; -- status = "disabled"; -+&usbphyc_port0 { -+ phy-supply = <&vdd_usb>; - }; - --&timers1 { -- /* spare dmas for other usage */ -- /delete-property/dmas; -- /delete-property/dma-names; -- status = "disabled"; -- pwm { -- pinctrl-0 = <&pwm1_pins_a>; -- pinctrl-1 = <&pwm1_sleep_pins_a>; -- pinctrl-names = "default", "sleep"; -- status = "okay"; -- }; -- timer@0 { -- status = "okay"; -- }; -+&usbphyc_port1 { -+ phy-supply = <&vdd_usb>; - }; - --&timers3 { -- /delete-property/dmas; -- /delete-property/dma-names; -- status = "disabled"; -- pwm { -- pinctrl-0 = <&pwm3_pins_a>; -- pinctrl-1 = <&pwm3_sleep_pins_a>; -- pinctrl-names = "default", "sleep"; -- status = "okay"; -+/* Low-power states of regulators */ -+&v1v2_hdmi { -+ standby-ddr-sr { -+ regulator-off-in-suspend; - }; -- timer@2 { -- status = "okay"; -+ standby-ddr-off { -+ regulator-off-in-suspend; - }; - }; - --&timers4 { -- /delete-property/dmas; -- /delete-property/dma-names; -- status = "disabled"; -- pwm { -- pinctrl-0 = <&pwm4_pins_a &pwm4_pins_b>; -- pinctrl-1 = <&pwm4_sleep_pins_a &pwm4_sleep_pins_b>; -- pinctrl-names = "default", "sleep"; -- status = "okay"; -+&v1v8_audio { -+ standby-ddr-sr { -+ regulator-off-in-suspend; - }; -- timer@3 { -- status = "okay"; -+ standby-ddr-off { -+ regulator-off-in-suspend; - }; - }; - --&timers5 { -- /delete-property/dmas; -- /delete-property/dma-names; -- status = "disabled"; -- pwm { -- pinctrl-0 = <&pwm5_pins_a>; -- pinctrl-1 = <&pwm5_sleep_pins_a>; -- pinctrl-names = "default", "sleep"; -- status = "okay"; -+&v3v3 { -+ lp-stop { -+ regulator-suspend-microvolt = <3300000>; -+ regulator-on-in-suspend; - }; -- timer@4 { -- status = "okay"; -+ standby-ddr-sr { -+ regulator-off-in-suspend; - }; --}; -- --&timers6 { -- /delete-property/dmas; -- /delete-property/dma-names; -- status = "disabled"; -- timer@5 { -- status = "okay"; -+ standby-ddr-off { -+ regulator-off-in-suspend; - }; - }; - --&timers12 { -- /delete-property/dmas; -- /delete-property/dma-names; -- status = "disabled"; -- pwm { -- pinctrl-0 = <&pwm12_pins_a>; -- pinctrl-1 = <&pwm12_sleep_pins_a>; -- pinctrl-names = "default", "sleep"; -- status = "okay"; -+&v3v3_hdmi { -+ standby-ddr-sr { -+ regulator-off-in-suspend; - }; -- timer@11 { -- status = "okay"; -+ standby-ddr-off { -+ regulator-off-in-suspend; - }; - }; - --&uart4 { -- pinctrl-names = "default"; -- pinctrl-0 = <&uart4_pins_a>; -- status = "okay"; -+&vdd { -+ lp-stop { -+ regulator-suspend-microvolt = <3300000>; -+ regulator-on-in-suspend; -+ }; -+ standby-ddr-sr { -+ regulator-suspend-microvolt = <3300000>; -+ regulator-on-in-suspend; -+ }; -+ standby-ddr-off { -+ regulator-suspend-microvolt = <3300000>; -+ regulator-on-in-suspend; -+ }; - }; - --&usbh_ehci { -- phys = <&usbphyc_port0>; -- status = "okay"; -+&vdda { -+ standby-ddr-sr { -+ regulator-off-in-suspend; -+ }; -+ standby-ddr-off { -+ regulator-off-in-suspend; -+ }; - }; - --&usbotg_hs { -- dr_mode = "peripheral"; -- phys = <&usbphyc_port1 0>; -- phy-names = "usb2-phy"; -- status = "okay"; -+&vddcore { -+ lp-stop { -+ regulator-on-in-suspend; -+ regulator-suspend-microvolt = <1200000>; -+ }; -+ standby-ddr-sr { -+ regulator-off-in-suspend; -+ }; -+ standby-ddr-off { -+ regulator-off-in-suspend; -+ }; - }; - --&usbphyc { -- status = "okay"; -+&vdd_ddr { -+ lp-stop { -+ regulator-suspend-microvolt = <1350000>; -+ regulator-on-in-suspend; -+ }; -+ standby-ddr-sr { -+ regulator-suspend-microvolt = <1350000>; -+ regulator-on-in-suspend; -+ }; -+ standby-ddr-off { -+ regulator-off-in-suspend; -+ }; - }; - --&usbphyc_port0 { -- phy-supply = <&vdd_usb>; -- vdda1v1-supply = <®11>; -- vdda1v8-supply = <®18>; -+&vdd_usb { -+ standby-ddr-sr { -+ regulator-off-in-suspend; -+ }; -+ standby-ddr-off { -+ regulator-off-in-suspend; -+ }; - }; - --&usbphyc_port1 { -- phy-supply = <&vdd_usb>; -- vdda1v1-supply = <®11>; -- vdda1v8-supply = <®18>; -+&vref_ddr { -+ lp-stop { -+ regulator-on-in-suspend; -+ }; -+ standby-ddr-sr { -+ regulator-on-in-suspend; -+ }; -+ standby-ddr-off { -+ regulator-off-in-suspend; -+ }; - }; - --&vrefbuf { -- regulator-min-microvolt = <2500000>; -- regulator-max-microvolt = <2500000>; -- vdda-supply = <&vdd>; -- status = "okay"; -+&vtt_ddr { -+ lp-stop { -+ regulator-off-in-suspend; -+ }; -+ standby-ddr-sr { -+ regulator-off-in-suspend; -+ }; -+ standby-ddr-off { -+ regulator-off-in-suspend; -+ }; - }; -diff --git a/core/arch/arm/dts/stm32mp15xx-edx.dtsi b/core/arch/arm/dts/stm32mp15xx-edx.dtsi -new file mode 100644 -index 000000000..ef51b00af ---- /dev/null -+++ b/core/arch/arm/dts/stm32mp15xx-edx.dtsi -@@ -0,0 +1,517 @@ -+// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) -+/* -+ * Copyright (C) STMicroelectronics 2017 - All Rights Reserved -+ * Author: Ludovic Barre for STMicroelectronics. -+ */ -+ -+#include -+#include -+ -+/ { -+ memory@c0000000 { -+ device_type = "memory"; -+ reg = <0xC0000000 0x40000000>; -+ }; -+ -+ aliases { -+ serial0 = &uart4; -+ }; -+ -+ vin: vin { -+ compatible = "regulator-fixed"; -+ regulator-name = "vin"; -+ regulator-min-microvolt = <5000000>; -+ regulator-max-microvolt = <5000000>; -+ regulator-always-on; -+ }; -+}; -+ -+&bsec { -+ board_id: board_id@ec { -+ reg = <0xec 0x4>; -+ st,non-secure-otp; -+ }; -+}; -+ -+&clk_hse { -+ st,digbypass; -+}; -+ -+&cpu0{ -+ cpu-supply = <&vddcore>; -+}; -+ -+&cpu1{ -+ cpu-supply = <&vddcore>; -+}; -+ -+&hash1 { -+ status = "okay"; -+}; -+ -+&i2c4 { -+ pinctrl-names = "default"; -+ pinctrl-0 = <&i2c4_pins_a>; -+ i2c-scl-rising-time-ns = <185>; -+ i2c-scl-falling-time-ns = <20>; -+ clock-frequency = <400000>; -+ status = "okay"; -+ secure-status = "okay"; -+ -+ pmic: stpmic@33 { -+ compatible = "st,stpmic1"; -+ reg = <0x33>; -+ interrupts-extended = <&exti_pwr 55 IRQ_TYPE_EDGE_FALLING>; -+ interrupt-controller; -+ #interrupt-cells = <2>; -+ status = "okay"; -+ secure-status = "okay"; -+ -+ regulators { -+ compatible = "st,stpmic1-regulators"; -+ buck1-supply = <&vin>; -+ buck2-supply = <&vin>; -+ buck3-supply = <&vin>; -+ buck4-supply = <&vin>; -+ ldo1-supply = <&v3v3>; -+ ldo2-supply = <&v3v3>; -+ ldo3-supply = <&vdd_ddr>; -+ ldo4-supply = <&vin>; -+ ldo5-supply = <&v3v3>; -+ ldo6-supply = <&v3v3>; -+ vref_ddr-supply = <&vin>; -+ boost-supply = <&vin>; -+ pwr_sw1-supply = <&bst_out>; -+ pwr_sw2-supply = <&bst_out>; -+ -+ vddcore: buck1 { -+ regulator-name = "vddcore"; -+ regulator-min-microvolt = <1200000>; -+ regulator-max-microvolt = <1350000>; -+ regulator-always-on; -+ regulator-initial-mode = <0>; -+ regulator-over-current-protection; -+ }; -+ -+ vdd_ddr: buck2 { -+ regulator-name = "vdd_ddr"; -+ regulator-min-microvolt = <1350000>; -+ regulator-max-microvolt = <1350000>; -+ regulator-always-on; -+ regulator-initial-mode = <0>; -+ regulator-over-current-protection; -+ }; -+ -+ vdd: buck3 { -+ regulator-name = "vdd"; -+ regulator-min-microvolt = <3300000>; -+ regulator-max-microvolt = <3300000>; -+ regulator-always-on; -+ st,mask-reset; -+ regulator-initial-mode = <0>; -+ regulator-over-current-protection; -+ }; -+ -+ v3v3: buck4 { -+ regulator-name = "v3v3"; -+ regulator-min-microvolt = <3300000>; -+ regulator-max-microvolt = <3300000>; -+ regulator-always-on; -+ regulator-over-current-protection; -+ regulator-initial-mode = <0>; -+ }; -+ -+ vdda: ldo1 { -+ regulator-name = "vdda"; -+ regulator-min-microvolt = <2900000>; -+ regulator-max-microvolt = <2900000>; -+ }; -+ -+ v2v8: ldo2 { -+ regulator-name = "v2v8"; -+ regulator-min-microvolt = <2800000>; -+ regulator-max-microvolt = <2800000>; -+ }; -+ -+ vtt_ddr: ldo3 { -+ regulator-name = "vtt_ddr"; -+ regulator-min-microvolt = <500000>; -+ regulator-max-microvolt = <750000>; -+ regulator-always-on; -+ regulator-over-current-protection; -+ }; -+ -+ vdd_usb: ldo4 { -+ regulator-name = "vdd_usb"; -+ }; -+ -+ vdd_sd: ldo5 { -+ regulator-name = "vdd_sd"; -+ regulator-min-microvolt = <2900000>; -+ regulator-max-microvolt = <2900000>; -+ regulator-boot-on; -+ }; -+ -+ v1v8: ldo6 { -+ regulator-name = "v1v8"; -+ regulator-min-microvolt = <1800000>; -+ regulator-max-microvolt = <1800000>; -+ }; -+ -+ vref_ddr: vref_ddr { -+ regulator-name = "vref_ddr"; -+ regulator-always-on; -+ regulator-over-current-protection; -+ }; -+ -+ bst_out: boost { -+ regulator-name = "bst_out"; -+ }; -+ -+ vbus_otg: pwr_sw1 { -+ regulator-name = "vbus_otg"; -+ }; -+ -+ vbus_sw: pwr_sw2 { -+ regulator-name = "vbus_sw"; -+ regulator-active-discharge = <1>; -+ }; -+ }; -+ }; -+}; -+ -+&iwdg2 { -+ timeout-sec = <32>; -+ secure-timeout-sec = <5>; -+ status = "okay"; -+ secure-status = "okay"; -+}; -+ -+&nvmem_layout { -+ nvmem-cells = <&cfg0_otp>, -+ <&part_number_otp>, -+ <&monotonic_otp>, -+ <&nand_otp>, -+ <&uid_otp>, -+ <&package_otp>, -+ <&hw2_otp>, -+ <&pkh_otp>, -+ <&board_id>; -+ -+ nvmem-cell-names = "cfg0_otp", -+ "part_number_otp", -+ "monotonic_otp", -+ "nand_otp", -+ "uid_otp", -+ "package_otp", -+ "hw2_otp", -+ "pkh_otp", -+ "board_id"; -+}; -+ -+&pwr_regulators { -+ system_suspend_supported_soc_modes = < -+ STM32_PM_CSLEEP_RUN -+ STM32_PM_CSTOP_ALLOW_LP_STOP -+ STM32_PM_CSTOP_ALLOW_LPLV_STOP -+ STM32_PM_CSTOP_ALLOW_STANDBY_DDR_SR -+ >; -+ system_off_soc_mode = ; -+ vdd-supply = <&vdd>; -+ vdd_3v3_usbfs-supply = <&vdd_usb>; -+}; -+ -+&rcc { -+ st,hsi-cal; -+ st,csi-cal; -+ st,cal-sec = <60>; -+ st,clksrc = < -+ CLK_MPU_PLL1P -+ CLK_AXI_PLL2P -+ CLK_MCU_PLL3P -+ CLK_PLL12_HSE -+ CLK_PLL3_HSE -+ CLK_PLL4_HSE -+ CLK_RTC_LSE -+ CLK_MCO1_DISABLED -+ CLK_MCO2_DISABLED -+ >; -+ -+ st,clkdiv = < -+ 1 /*MPU*/ -+ 0 /*AXI*/ -+ 0 /*MCU*/ -+ 1 /*APB1*/ -+ 1 /*APB2*/ -+ 1 /*APB3*/ -+ 1 /*APB4*/ -+ 2 /*APB5*/ -+ 23 /*RTC*/ -+ 0 /*MCO1*/ -+ 0 /*MCO2*/ -+ >; -+ -+ st,pkcs = < -+ CLK_CKPER_HSE -+ CLK_FMC_ACLK -+ CLK_QSPI_ACLK -+ CLK_ETH_DISABLED -+ CLK_SDMMC12_PLL4P -+ CLK_DSI_DSIPLL -+ CLK_STGEN_HSE -+ CLK_USBPHY_HSE -+ CLK_SPI2S1_PLL3Q -+ CLK_SPI2S23_PLL3Q -+ CLK_SPI45_HSI -+ CLK_SPI6_HSI -+ CLK_I2C46_HSI -+ CLK_SDMMC3_PLL4P -+ CLK_USBO_USBPHY -+ CLK_ADC_CKPER -+ CLK_CEC_LSE -+ CLK_I2C12_HSI -+ CLK_I2C35_HSI -+ CLK_UART1_HSI -+ CLK_UART24_HSI -+ CLK_UART35_HSI -+ CLK_UART6_HSI -+ CLK_UART78_HSI -+ CLK_SPDIF_PLL4P -+ CLK_FDCAN_PLL4R -+ CLK_SAI1_PLL3Q -+ CLK_SAI2_PLL3Q -+ CLK_SAI3_PLL3Q -+ CLK_SAI4_PLL3Q -+ CLK_RNG1_LSI -+ CLK_RNG2_LSI -+ CLK_LPTIM1_PCLK1 -+ CLK_LPTIM23_PCLK3 -+ CLK_LPTIM45_LSE -+ >; -+ -+ /* VCO = 1066.0 MHz => P = 266 (AXI), Q = 533 (GPU), R = 533 (DDR) */ -+ pll2: st,pll@1 { -+ compatible = "st,stm32mp1-pll"; -+ reg = <1>; -+ cfg = <2 65 1 0 0 PQR(1,1,1)>; -+ frac = <0x1400>; -+ }; -+ -+ /* VCO = 417.8 MHz => P = 209, Q = 24, R = 11 */ -+ pll3: st,pll@2 { -+ compatible = "st,stm32mp1-pll"; -+ reg = <2>; -+ cfg = <1 33 1 16 36 PQR(1,1,1)>; -+ frac = <0x1a04>; -+ }; -+ -+ /* VCO = 594.0 MHz => P = 99, Q = 74, R = 74 */ -+ pll4: st,pll@3 { -+ compatible = "st,stm32mp1-pll"; -+ reg = <3>; -+ cfg = <3 98 5 7 7 PQR(1,1,1)>; -+ }; -+}; -+ -+&rng1 { -+ status = "okay"; -+ secure-status = "okay"; -+}; -+ -+&rtc { -+ status = "okay"; -+ secure-status = "okay"; -+}; -+ -+&sdmmc1 { -+ pinctrl-names = "default"; -+ pinctrl-0 = <&sdmmc1_b4_pins_a &sdmmc1_dir_pins_a>; -+ disable-wp; -+ st,sig-dir; -+ st,neg-edge; -+ st,use-ckin; -+ bus-width = <4>; -+ vmmc-supply = <&vdd_sd>; -+ sd-uhs-sdr12; -+ sd-uhs-sdr25; -+ sd-uhs-sdr50; -+ sd-uhs-ddr50; -+ status = "okay"; -+}; -+ -+&sdmmc2 { -+ pinctrl-names = "default"; -+ pinctrl-0 = <&sdmmc2_b4_pins_a &sdmmc2_d47_pins_a>; -+ non-removable; -+ no-sd; -+ no-sdio; -+ st,neg-edge; -+ bus-width = <8>; -+ vmmc-supply = <&v3v3>; -+ vqmmc-supply = <&vdd>; -+ mmc-ddr-3_3v; -+ status = "okay"; -+}; -+ -+&timers15 { -+ secure-status = "okay"; -+ st,hsi-cal-input = <7>; -+ st,csi-cal-input = <8>; -+}; -+ -+&uart4 { -+ pinctrl-names = "default"; -+ pinctrl-0 = <&uart4_pins_a>; -+ status = "okay"; -+}; -+ -+&usbotg_hs { -+ vbus-supply = <&vbus_otg>; -+}; -+ -+&usbphyc_port0 { -+ phy-supply = <&vdd_usb>; -+}; -+ -+&usbphyc_port1 { -+ phy-supply = <&vdd_usb>; -+}; -+ -+/* Low-power states of regulators */ -+&v1v8 { -+ standby-ddr-sr { -+ regulator-off-in-suspend; -+ }; -+ standby-ddr-off { -+ regulator-off-in-suspend; -+ }; -+}; -+ -+&v2v8 { -+ standby-ddr-sr { -+ regulator-off-in-suspend; -+ }; -+ standby-ddr-off { -+ regulator-off-in-suspend; -+ }; -+}; -+ -+&v3v3 { -+ standby-ddr-sr { -+ regulator-off-in-suspend; -+ }; -+ standby-ddr-off { -+ regulator-off-in-suspend; -+ }; -+}; -+ -+&vdd { -+ lp-stop { -+ regulator-suspend-microvolt = <3300000>; -+ regulator-on-in-suspend; -+ }; -+ lplv-stop { -+ regulator-suspend-microvolt = <3300000>; -+ regulator-on-in-suspend; -+ }; -+ standby-ddr-sr { -+ regulator-suspend-microvolt = <3300000>; -+ regulator-on-in-suspend; -+ }; -+ standby-ddr-off { -+ regulator-suspend-microvolt = <3300000>; -+ regulator-on-in-suspend; -+ }; -+}; -+ -+&vdda { -+ standby-ddr-sr { -+ regulator-off-in-suspend; -+ }; -+ standby-ddr-off { -+ regulator-off-in-suspend; -+ }; -+}; -+ -+&vddcore { -+ lp-stop { -+ regulator-on-in-suspend; -+ regulator-suspend-microvolt = <1200000>; -+ }; -+ lplv-stop { -+ regulator-on-in-suspend; -+ regulator-suspend-microvolt = <900000>; -+ }; -+ standby-ddr-sr { -+ regulator-off-in-suspend; -+ }; -+ standby-ddr-off { -+ regulator-off-in-suspend; -+ }; -+}; -+ -+&vdd_ddr { -+ lp-stop { -+ regulator-suspend-microvolt = <1350000>; -+ regulator-on-in-suspend; -+ }; -+ lplv-stop { -+ regulator-suspend-microvolt = <1350000>; -+ regulator-on-in-suspend; -+ }; -+ standby-ddr-sr { -+ regulator-suspend-microvolt = <1350000>; -+ regulator-on-in-suspend; -+ }; -+ standby-ddr-off { -+ regulator-off-in-suspend; -+ }; -+}; -+ -+&vdd_sd { -+ standby-ddr-sr { -+ regulator-off-in-suspend; -+ }; -+ standby-ddr-off { -+ regulator-off-in-suspend; -+ }; -+}; -+ -+&vdd_usb { -+ standby-ddr-sr { -+ regulator-off-in-suspend; -+ }; -+ standby-ddr-off { -+ regulator-off-in-suspend; -+ }; -+}; -+ -+&vref_ddr { -+ lp-stop { -+ regulator-on-in-suspend; -+ }; -+ lplv-stop { -+ regulator-on-in-suspend; -+ }; -+ standby-ddr-sr { -+ regulator-on-in-suspend; -+ }; -+ standby-ddr-off { -+ regulator-off-in-suspend; -+ }; -+}; -+ -+&vtt_ddr { -+ lp-stop { -+ regulator-off-in-suspend; -+ }; -+ lplv-stop { -+ regulator-off-in-suspend; -+ }; -+ standby-ddr-sr { -+ regulator-off-in-suspend; -+ }; -+ standby-ddr-off { -+ regulator-off-in-suspend; -+ }; -+}; -diff --git a/core/arch/arm/dts/stm32mp15xx-evx.dtsi b/core/arch/arm/dts/stm32mp15xx-evx.dtsi -new file mode 100644 -index 000000000..ce1982d45 ---- /dev/null -+++ b/core/arch/arm/dts/stm32mp15xx-evx.dtsi -@@ -0,0 +1,73 @@ -+// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) -+/* -+ * Copyright (C) STMicroelectronics 2017 - All Rights Reserved -+ * Author: Ludovic Barre for STMicroelectronics. -+ */ -+ -+&fmc { -+ pinctrl-names = "default"; -+ pinctrl-0 = <&fmc_pins_a>; -+ status = "okay"; -+ -+ nand-controller@4,0 { -+ status = "okay"; -+ -+ nand@0 { -+ reg = <0>; -+ nand-on-flash-bbt; -+ #address-cells = <1>; -+ #size-cells = <1>; -+ }; -+ }; -+}; -+ -+&i2c4 { -+ pmic: stpmic@33 { -+ regulators { -+ v1v8: ldo6 { -+ regulator-enable-ramp-delay = <300000>; -+ }; -+ }; -+ }; -+}; -+ -+&qspi { -+ pinctrl-names = "default"; -+ pinctrl-0 = <&qspi_clk_pins_a &qspi_bk1_pins_a &qspi_bk2_pins_a>; -+ reg = <0x58003000 0x1000>, <0x70000000 0x4000000>; -+ #address-cells = <1>; -+ #size-cells = <0>; -+ status = "okay"; -+ -+ flash0: mx66l51235l@0 { -+ compatible = "jedec,spi-nor"; -+ reg = <0>; -+ spi-rx-bus-width = <4>; -+ spi-max-frequency = <108000000>; -+ #address-cells = <1>; -+ #size-cells = <1>; -+ }; -+}; -+ -+&timers12 { -+ status = "disabled"; -+}; -+ -+&usart3 { -+ pinctrl-names = "default"; -+ pinctrl-0 = <&usart3_pins_b>; -+ uart-has-rtscts; -+ status = "disabled"; -+}; -+ -+&usbotg_hs { -+ pinctrl-0 = <&usbotg_hs_pins_a>; -+ pinctrl-names = "default"; -+ phys = <&usbphyc_port1 0>; -+ phy-names = "usb2-phy"; -+ status = "okay"; -+}; -+ -+&usbphyc { -+ status = "okay"; -+}; -diff --git a/core/arch/arm/dts/stm32mp15xxaa-pinctrl.dtsi b/core/arch/arm/dts/stm32mp15xxaa-pinctrl.dtsi -index 04f7a43ad..4f4130eff 100644 ---- a/core/arch/arm/dts/stm32mp15xxaa-pinctrl.dtsi -+++ b/core/arch/arm/dts/stm32mp15xxaa-pinctrl.dtsi -@@ -79,6 +79,7 @@ - - gpioz: gpio@54004000 { - status = "okay"; -+ secure-status = "okay"; - ngpios = <8>; - gpio-ranges = <&pinctrl_z 0 400 8>; - }; -diff --git a/core/arch/arm/dts/stm32mp15xxac-pinctrl.dtsi b/core/arch/arm/dts/stm32mp15xxac-pinctrl.dtsi -index 7eaa245f4..866c050da 100644 ---- a/core/arch/arm/dts/stm32mp15xxac-pinctrl.dtsi -+++ b/core/arch/arm/dts/stm32mp15xxac-pinctrl.dtsi -@@ -67,6 +67,7 @@ - - gpioz: gpio@54004000 { - status = "okay"; -+ secure-status = "okay"; - ngpios = <8>; - gpio-ranges = <&pinctrl_z 0 400 8>; - }; -diff --git a/core/arch/arm/include/arm32.h b/core/arch/arm/include/arm32.h -index f28b73fda..2f67cc762 100644 ---- a/core/arch/arm/include/arm32.h -+++ b/core/arch/arm/include/arm32.h -@@ -1,6 +1,6 @@ - /* SPDX-License-Identifier: BSD-2-Clause */ - /* -- * Copyright (c) 2016, Linaro Limited -+ * Copyright (c) 2016-2019, Linaro Limited - * Copyright (c) 2014, STMicroelectronics International N.V. - */ - -@@ -163,6 +163,15 @@ - #define IDPFR1_GENTIMER_SHIFT 16 - #define IDPFR1_GENTIMER_MASK (0xF << IDPFR1_GENTIMER_SHIFT) - -+/* Generic timer registers and fields */ -+#define CNTCR_OFFSET 0x000 -+#define CNTSR_OFFSET 0x004 -+#define CNTCVL_OFFSET 0x008 -+#define CNTCVU_OFFSET 0x00C -+#define CNTFID_OFFSET 0x020 -+ -+#define CNTCR_EN BIT(0) -+ - #ifndef __ASSEMBLER__ - #include - #ifdef CFG_ARM_GICV3 -diff --git a/core/arch/arm/include/kernel/tlb_helpers.h b/core/arch/arm/include/kernel/tlb_helpers.h -index d33dd5c01..2dbcbcf08 100644 ---- a/core/arch/arm/include/kernel/tlb_helpers.h -+++ b/core/arch/arm/include/kernel/tlb_helpers.h -@@ -25,11 +25,10 @@ static inline void tlbi_mva_allasid_nosync(vaddr_t va) - #endif - } - --static inline void tlbi_mva_asid(vaddr_t va, uint32_t asid) -+static inline void tlbi_mva_asid_nosync(vaddr_t va, uint32_t asid) - { - uint32_t a = asid & TLBI_ASID_MASK; - -- dsb_ishst(); - #ifdef ARM64 - tlbi_vale1is((va >> TLBI_MVA_SHIFT) | SHIFT_U64(a, TLBI_ASID_SHIFT)); - tlbi_vale1is((va >> TLBI_MVA_SHIFT) | -@@ -38,6 +37,12 @@ static inline void tlbi_mva_asid(vaddr_t va, uint32_t asid) - write_tlbimvais((va & ~(BIT32(TLBI_MVA_SHIFT) - 1)) | a); - write_tlbimvais((va & ~(BIT32(TLBI_MVA_SHIFT) - 1)) | a | 1); - #endif -+} -+ -+static inline void tlbi_mva_asid(vaddr_t va, uint32_t asid) -+{ -+ dsb_ishst(); -+ tlbi_mva_asid_nosync(va, asid); - dsb_ish(); - isb(); - } -diff --git a/core/arch/arm/include/mm/core_mmu.h b/core/arch/arm/include/mm/core_mmu.h -index 33ee2ebd7..bf8e68607 100644 ---- a/core/arch/arm/include/mm/core_mmu.h -+++ b/core/arch/arm/include/mm/core_mmu.h -@@ -97,6 +97,7 @@ - * MEM_AREA_NSEC_SHM: NonSecure shared RAM between NSec and TEE. - * MEM_AREA_RAM_NSEC: NonSecure RAM storing data - * MEM_AREA_RAM_SEC: Secure RAM storing some secrets -+ * MEM_AREA_ROM_SEC: Secure read only memory storing some secrets - * MEM_AREA_IO_NSEC: NonSecure HW mapped registers - * MEM_AREA_IO_SEC: Secure HW mapped registers - * MEM_AREA_EXT_DT: Memory loads external device tree -@@ -121,6 +122,7 @@ enum teecore_memtypes { - MEM_AREA_NSEC_SHM, - MEM_AREA_RAM_NSEC, - MEM_AREA_RAM_SEC, -+ MEM_AREA_ROM_SEC, - MEM_AREA_IO_NSEC, - MEM_AREA_IO_SEC, - MEM_AREA_EXT_DT, -@@ -150,6 +152,7 @@ static inline const char *teecore_memtype_name(enum teecore_memtypes type) - [MEM_AREA_NSEC_SHM] = "NSEC_SHM", - [MEM_AREA_RAM_NSEC] = "RAM_NSEC", - [MEM_AREA_RAM_SEC] = "RAM_SEC", -+ [MEM_AREA_ROM_SEC] = "ROM_SEC", - [MEM_AREA_IO_NSEC] = "IO_NSEC", - [MEM_AREA_IO_SEC] = "IO_SEC", - [MEM_AREA_EXT_DT] = "EXT_DT", -@@ -629,6 +632,18 @@ enum teecore_tlb_op { - /* TLB invalidation for a range of virtual address */ - void tlbi_mva_range(vaddr_t va, size_t size, size_t granule); - -+/* -+ * tlbi_mva_range_asid() - Invalidate TLB for virtual address range for -+ * a specific ASID -+ * @va: start virtual address, must be a multiple of @granule -+ * @len: length in bytes of range, must be a multiple of @granule -+ * @granule: granularity of mapping, supported values are -+ * CORE_MMU_PGDIR_SIZE or SMALL_PAGE_SIZE. This value must -+ * match the actual mappings. -+ * @asid: Address space identifier -+ */ -+void tlbi_mva_range_asid(vaddr_t va, size_t len, size_t granule, uint32_t asid); -+ - /* deprecated: please call straight tlbi_all() and friends */ - int core_tlb_maintenance(int op, unsigned long a) __deprecated; - -diff --git a/core/arch/arm/include/mm/pgt_cache.h b/core/arch/arm/include/mm/pgt_cache.h -index d19414e48..433dc3920 100644 ---- a/core/arch/arm/include/mm/pgt_cache.h -+++ b/core/arch/arm/include/mm/pgt_cache.h -@@ -23,8 +23,8 @@ struct ts_ctx; - - struct pgt { - void *tbl; --#if defined(CFG_PAGED_USER_TA) - vaddr_t vabase; -+#if defined(CFG_PAGED_USER_TA) - struct ts_ctx *ctx; - size_t num_used_entries; - #endif -@@ -61,6 +61,8 @@ void pgt_alloc(struct pgt_cache *pgt_cache, struct ts_ctx *owning_ctx, - vaddr_t begin, vaddr_t last); - void pgt_free(struct pgt_cache *pgt_cache, bool save_ctx); - -+void pgt_clear_ctx_range(struct pgt_cache *pgt_cache, struct ts_ctx *ctx, -+ vaddr_t begin, vaddr_t end); - #ifdef CFG_PAGED_USER_TA - void pgt_flush_ctx_range(struct pgt_cache *pgt_cache, struct ts_ctx *ctx, - vaddr_t begin, vaddr_t last); -@@ -73,6 +75,7 @@ static inline void pgt_flush_ctx_range(struct pgt_cache *pgt_cache __unused, - } - #endif - -+ - void pgt_init(void); - - #if defined(CFG_PAGED_USER_TA) -diff --git a/core/arch/arm/include/sm/pm.h b/core/arch/arm/include/sm/pm.h -index 939f966e8..90f031a2b 100644 ---- a/core/arch/arm/include/sm/pm.h -+++ b/core/arch/arm/include/sm/pm.h -@@ -34,7 +34,11 @@ - struct sm_pm_ctx { - uint32_t sp; - paddr_t cpu_resume_addr; -+#ifdef CFG_WITH_LPAE -+ uint32_t suspend_regs[18]; -+#else - uint32_t suspend_regs[16]; -+#endif - }; - - /* suspend/resume core functions */ -diff --git a/core/arch/arm/mm/core_mmu.c b/core/arch/arm/mm/core_mmu.c -index 4f27b2499..489dae220 100644 ---- a/core/arch/arm/mm/core_mmu.c -+++ b/core/arch/arm/mm/core_mmu.c -@@ -388,6 +388,7 @@ void core_mmu_set_discovered_nsec_ddr(struct core_mmu_phys_mem *start, - carve_out_phys_mem(&m, &num_elems, map->pa, map->size); - break; - case MEM_AREA_EXT_DT: -+ case MEM_AREA_RAM_NSEC: - case MEM_AREA_RES_VASPACE: - case MEM_AREA_SHM_VASPACE: - case MEM_AREA_TA_VASPACE: -@@ -490,7 +491,6 @@ static bool pbuf_is_sdp_mem(paddr_t pbuf __unused, size_t len __unused) - - /* Check special memories comply with registered memories */ - static void verify_special_mem_areas(struct tee_mmap_region *mem_map, -- size_t len, - const struct core_mmu_phys_mem *start, - const struct core_mmu_phys_mem *end, - const char *area_name __maybe_unused) -@@ -498,7 +498,6 @@ static void verify_special_mem_areas(struct tee_mmap_region *mem_map, - const struct core_mmu_phys_mem *mem; - const struct core_mmu_phys_mem *mem2; - struct tee_mmap_region *mmap; -- size_t n; - - if (start == end) { - DMSG("No %s memory area defined", area_name); -@@ -524,9 +523,18 @@ static void verify_special_mem_areas(struct tee_mmap_region *mem_map, - /* - * Check memories do not intersect any mapped memory. - * This is called before reserved VA space is loaded in mem_map. -+ * -+ * Exceptions are the memory areas that maps with the same attributes -+ * as for example MEM_AREA_RAM_NSEC and MEM_AREA_NSEC_SHM -+ * which may overlap since they are used for the same purpose -+ * except that MEM_AREA_NSEC_SHM is always mapped and -+ * MEM_AREA_RAM_NSEC only uses a dynamic mapping. - */ - for (mem = start; mem < end; mem++) { -- for (mmap = mem_map, n = 0; n < len; mmap++, n++) { -+ for (mmap = mem_map; mmap->type != MEM_AREA_END; mmap++) { -+ if (core_mmu_type_to_attr(mem->type) == -+ core_mmu_type_to_attr(mmap->type)) -+ continue; - if (core_is_buffer_intersect(mem->addr, mem->size, - mmap->pa, mmap->size)) { - MSG_MEM_INSTERSECT(mem->addr, mem->size, -@@ -650,6 +658,8 @@ uint32_t core_mmu_type_to_attr(enum teecore_memtypes t) - case MEM_AREA_RAM_SEC: - case MEM_AREA_SEC_RAM_OVERALL: - return attr | TEE_MATTR_SECURE | TEE_MATTR_PRW | cached; -+ case MEM_AREA_ROM_SEC: -+ return attr | TEE_MATTR_SECURE | TEE_MATTR_PR | cached; - case MEM_AREA_RES_VASPACE: - case MEM_AREA_SHM_VASPACE: - return 0; -@@ -841,8 +851,7 @@ static size_t collect_mem_ranges(struct tee_mmap_region *memory_map, - } - - if (IS_ENABLED(CFG_SECURE_DATA_PATH)) -- verify_special_mem_areas(memory_map, num_elems, -- phys_sdp_mem_begin, -+ verify_special_mem_areas(memory_map, phys_sdp_mem_begin, - phys_sdp_mem_end, "SDP"); - - add_va_space(memory_map, num_elems, MEM_AREA_RES_VASPACE, -@@ -1145,6 +1154,7 @@ static void check_mem_map(struct tee_mmap_region *map) - case MEM_AREA_IO_NSEC: - case MEM_AREA_EXT_DT: - case MEM_AREA_RAM_SEC: -+ case MEM_AREA_ROM_SEC: - case MEM_AREA_RAM_NSEC: - case MEM_AREA_RES_VASPACE: - case MEM_AREA_SHM_VASPACE: -@@ -1395,6 +1405,21 @@ void tlbi_mva_range(vaddr_t va, size_t size, size_t granule) - isb(); - } - -+void tlbi_mva_range_asid(vaddr_t va, size_t len, size_t granule, uint32_t asid) -+{ -+ assert(granule == CORE_MMU_PGDIR_SIZE || granule == SMALL_PAGE_SIZE); -+ assert(!(va & (granule - 1)) && !(len & (granule - 1))); -+ -+ dsb_ishst(); -+ while (len) { -+ tlbi_mva_asid_nosync(va, asid); -+ len -= granule; -+ va += granule; -+ } -+ dsb_ish(); -+ isb(); -+} -+ - TEE_Result cache_op_inner(enum cache_op op, void *va, size_t len) - { - switch (op) { -diff --git a/core/arch/arm/mm/mobj.c b/core/arch/arm/mm/mobj.c -index 7a129cf5a..b14e93939 100644 ---- a/core/arch/arm/mm/mobj.c -+++ b/core/arch/arm/mm/mobj.c -@@ -49,6 +49,7 @@ static void *mobj_phys_get_va(struct mobj *mobj, size_t offset) - - return (void *)(moph->va + offset); - } -+DECLARE_KEEP_PAGER(mobj_phys_get_va); - - static TEE_Result mobj_phys_get_pa(struct mobj *mobj, size_t offs, - size_t granule, paddr_t *pa) -@@ -228,7 +229,7 @@ static void *mobj_mm_get_va(struct mobj *mobj, size_t offs) - return mobj_get_va(to_mobj_mm(mobj)->parent_mobj, - mobj_mm_offs(mobj, offs)); - } -- -+DECLARE_KEEP_PAGER(mobj_mm_get_va); - - static TEE_Result mobj_mm_get_pa(struct mobj *mobj, size_t offs, - size_t granule, paddr_t *pa) -@@ -657,4 +658,4 @@ static TEE_Result mobj_init(void) - return TEE_SUCCESS; - } - --driver_init_late(mobj_init); -+service_init(mobj_init); -diff --git a/core/arch/arm/mm/pgt_cache.c b/core/arch/arm/mm/pgt_cache.c -index 3e854934d..988c1a351 100644 ---- a/core/arch/arm/mm/pgt_cache.c -+++ b/core/arch/arm/mm/pgt_cache.c -@@ -413,13 +413,65 @@ static void pgt_free_unlocked(struct pgt_cache *pgt_cache, - } - } - --static struct pgt *pop_from_some_list(vaddr_t vabase __unused, -+static struct pgt *pop_from_some_list(vaddr_t vabase, - struct ts_ctx *ctx __unused) - { -- return pop_from_free_list(); -+ struct pgt *p = pop_from_free_list(); -+ -+ if (p) -+ p->vabase = vabase; -+ -+ return p; - } - #endif /*!CFG_PAGED_USER_TA*/ - -+static void clear_ctx_range_from_list(struct pgt_cache *pgt_cache, -+ void *ctx __maybe_unused, -+ vaddr_t begin, vaddr_t end) -+{ -+ struct pgt *p = NULL; -+#ifdef CFG_WITH_LPAE -+ uint64_t *tbl = NULL; -+#else -+ uint32_t *tbl = NULL; -+#endif -+ unsigned int idx = 0; -+ unsigned int n = 0; -+ -+ SLIST_FOREACH(p, pgt_cache, link) { -+ vaddr_t b = MAX(p->vabase, begin); -+ vaddr_t e = MIN(p->vabase + CORE_MMU_PGDIR_SIZE, end); -+ -+#ifdef CFG_PAGED_USER_TA -+ if (p->ctx != ctx) -+ continue; -+#endif -+ if (b >= e) -+ continue; -+ -+ tbl = p->tbl; -+ idx = (b - p->vabase) / SMALL_PAGE_SIZE; -+ n = (e - b) / SMALL_PAGE_SIZE; -+ memset(tbl + idx, 0, n * sizeof(*tbl)); -+ } -+} -+ -+void pgt_clear_ctx_range(struct pgt_cache *pgt_cache, struct ts_ctx *ctx, -+ vaddr_t begin, vaddr_t end) -+{ -+ mutex_lock(&pgt_mu); -+ -+ if (pgt_cache) -+ clear_ctx_range_from_list(pgt_cache, ctx, begin, end); -+#ifdef CFG_PAGED_USER_TA -+ clear_ctx_range_from_list(&pgt_cache_list, ctx, begin, end); -+#endif -+ -+ mutex_unlock(&pgt_mu); -+} -+ -+ -+ - static bool pgt_alloc_unlocked(struct pgt_cache *pgt_cache, struct ts_ctx *ctx, - vaddr_t begin, vaddr_t last) - { -diff --git a/core/arch/arm/plat-stm32mp1/boot_api.h b/core/arch/arm/plat-stm32mp1/boot_api.h -index 62e38b588..a7daffd44 100644 ---- a/core/arch/arm/plat-stm32mp1/boot_api.h -+++ b/core/arch/arm/plat-stm32mp1/boot_api.h -@@ -14,7 +14,9 @@ - #define BCKR_CORE1_MAGIC_NUMBER 4 - - /* Value for BCKR_CORE1_MAGIC_NUMBER entry */ -+#define BOOT_API_A7_CORE0_MAGIC_NUMBER 0xca7face0 - #define BOOT_API_A7_CORE1_MAGIC_NUMBER 0xca7face1 -+#define BOOT_API_A7_RESET_MAGIC_NUMBER 0xca7dead0 - - /* Backup register #5: physical address of core1 entry at boot up */ - #define BCKR_CORE1_BRANCH_ADDRESS 5 -diff --git a/core/arch/arm/plat-stm32mp1/conf.mk b/core/arch/arm/plat-stm32mp1/conf.mk -index baf3fe49a..b1d41c92a 100644 ---- a/core/arch/arm/plat-stm32mp1/conf.mk -+++ b/core/arch/arm/plat-stm32mp1/conf.mk -@@ -1,22 +1,41 @@ - # 1GB and 512MB DDR targets do not locate secure DDR at the same place. - flavor_dts_file-157A_DK1 = stm32mp157a-dk1.dts -+flavor_dts_file-157A_ED1 = stm32mp157a-ed1.dts -+flavor_dts_file-157A_EV1 = stm32mp157a-ev1.dts - flavor_dts_file-157C_DK2 = stm32mp157c-dk2.dts - flavor_dts_file-157C_ED1 = stm32mp157c-ed1.dts - flavor_dts_file-157C_EV1 = stm32mp157c-ev1.dts -+flavor_dts_file-157D_DK1 = stm32mp157d-dk1.dts -+flavor_dts_file-157D_ED1 = stm32mp157d-ed1.dts -+flavor_dts_file-157D_EV1 = stm32mp157d-ev1.dts -+flavor_dts_file-157F_DK2 = stm32mp157f-dk2.dts -+flavor_dts_file-157F_ED1 = stm32mp157f-ed1.dts -+flavor_dts_file-157F_EV1 = stm32mp157f-ev1.dts - --flavorlist-cryp-512M = $(flavor_dts_file-157C_DK2) -+flavorlist-cryp-512M = $(flavor_dts_file-157C_DK2) \ -+ $(flavor_dts_file-157F_DK2) - --flavorlist-no_cryp-512M = $(flavor_dts_file-157A_DK1) -+flavorlist-no_cryp-512M = $(flavor_dts_file-157A_DK1) \ -+ $(flavor_dts_file-157D_DK1) - - flavorlist-cryp-1G = $(flavor_dts_file-157C_ED1) \ -- $(flavor_dts_file-157C_EV1) -+ $(flavor_dts_file-157C_EV1) \ -+ $(flavor_dts_file-157F_ED1) \ -+ $(flavor_dts_file-157F_EV1) - --flavorlist-no_cryp = $(flavorlist-no_cryp-512M) -+flavorlist-no_cryp-1G = $(flavor_dts_file-157A_ED1) \ -+ $(flavor_dts_file-157A_EV1) \ -+ $(flavor_dts_file-157D_ED1) \ -+ $(flavor_dts_file-157D_EV1) -+ -+flavorlist-no_cryp = $(flavorlist-no_cryp-512M) \ -+ $(flavorlist-no_cryp-1G) - - flavorlist-512M = $(flavorlist-cryp-512M) \ - $(flavorlist-no_cryp-512M) - --flavorlist-1G = $(flavorlist-cryp-1G) -+flavorlist-1G = $(flavorlist-cryp-1G) \ -+ $(flavorlist-no_cryp-1G) - - ifneq ($(PLATFORM_FLAVOR),) - ifeq ($(flavor_dts_file-$(PLATFORM_FLAVOR)),) -@@ -31,9 +50,14 @@ endif - - include core/arch/arm/cpu/cortex-a7.mk - -+$(call force,CFG_CLK_DRIVER,y) -+$(call force,CFG_ARM_GIC_PM,y) - $(call force,CFG_BOOT_SECONDARY_REQUEST,y) - $(call force,CFG_GIC,y) - $(call force,CFG_INIT_CNTVOFF,y) -+$(call force,CFG_PM,y) -+$(call force,CFG_PM_ARM32,y) -+$(call force,CFG_PM_STUBS,y) - $(call force,CFG_PSCI_ARM32,y) - $(call force,CFG_SCMI_MSG_DRIVERS,y) - $(call force,CFG_SCMI_MSG_CLOCK,y) -@@ -52,6 +76,8 @@ CFG_SHMEM_START ?= 0xdfe00000 - CFG_DRAM_SIZE ?= 0x20000000 - endif - -+CFG_DTB_MAX_SIZE ?= 0x20000 -+ - CFG_TZSRAM_START ?= 0x2ffc0000 - CFG_TZSRAM_SIZE ?= 0x0003f000 - CFG_STM32MP1_SCMI_SHM_BASE ?= 0x2ffff000 -@@ -65,23 +91,45 @@ CFG_DRAM_SIZE ?= 0x40000000 - CFG_TEE_CORE_NB_CORE ?= 2 - CFG_WITH_PAGER ?= y - CFG_WITH_LPAE ?= y --CFG_MMAP_REGIONS ?= 23 -+CFG_MMAP_REGIONS ?= 30 -+CFG_CORE_HEAP_SIZE ?= 49152 -+CFG_DTB_MAX_SIZE = 0x20000 -+ -+# Disable early TA compression to limit HEAP size -+CFG_EARLY_TA_COMPRESS ?= n -+ -+# Embed public part of this key in OP-TEE OS -+CFG_RPROC_SIGN_KEY ?= keys/default_rproc.pem - - ifeq ($(CFG_EMBED_DTB_SOURCE_FILE),) - # Some drivers mandate DT support -+$(call force,CFG_STM32_CLKCALIB,n) - $(call force,CFG_STM32_I2C,n) -+$(call force,CFG_STM32_IWDG,n) -+$(call force,CFG_STM32_TIM,n) - $(call force,CFG_STPMIC1,n) - endif - -+CFG_RPROC_PTA ?= n - CFG_STM32_BSEC ?= y -+CFG_STM32_CLKCALIB ?= y -+CFG_STM32_CRYP ?= y - CFG_STM32_ETZPC ?= y - CFG_STM32_GPIO ?= y - CFG_STM32_I2C ?= y -+CFG_STM32_IWDG ?= y - CFG_STM32_RNG ?= y -+CFG_STM32_RTC ?= y -+CFG_STM32_TIM ?= y - CFG_STM32_UART ?= y -+CFG_STM32MP15_CLK ?= y - CFG_STPMIC1 ?= y - CFG_TZC400 ?= y - -+ifeq ($(CFG_STM32_CLKCALIB),y) -+$(call force,CFG_STM32_TIM,y) -+endif -+ - ifeq ($(CFG_STPMIC1),y) - $(call force,CFG_STM32_I2C,y) - $(call force,CFG_STM32_GPIO,y) -@@ -92,12 +140,23 @@ CFG_STM32MP_PANIC_ON_TZC_PERM_VIOLATION ?= y - - # SiP/OEM service for non-secure world - CFG_STM32_BSEC_SIP ?= y -+CFG_STM32_CLKCALIB_SIP ?= y -+CFG_STM32_LOWPOWER_SIP ?= $(CFG_PM) -+CFG_STM32_PWR_SIP ?= y -+CFG_STM32_RCC_SIP ?= y -+ -+# Default use stm32mp1 PM mailbox context version 2 -+# Use CFG_STM32MP15_PM_CONTEX_VERSION=1 to force version 0 when dealing with -+# a TF-A firmware that supports version 1 of the context mailbox. -+CFG_STM32MP15_PM_CONTEX_VERSION ?= 2 - - # Default enable some test facitilites - CFG_TEE_CORE_EMBED_INTERNAL_TESTS ?= y - CFG_WITH_STATS ?= y -+CFG_WERROR ?= y - - # Default disable some support for pager memory size constraint -+CFG_TEE_CORE_LOG_LEVEL ?= 2 - CFG_TEE_CORE_DEBUG ?= n - CFG_UNWIND ?= n - CFG_LOCKDEP ?= n -@@ -108,3 +167,6 @@ CFG_WITH_NSEC_GPIOS ?= y - CFG_WITH_NSEC_UARTS ?= y - # UART instance used for early console (0 disables early console) - CFG_STM32_EARLY_CONSOLE_UART ?= 4 -+ -+# Generate the STM32 files -+CFG_STM32MP15x_STM32IMAGE ?= n -diff --git a/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_calib.c b/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_calib.c -new file mode 100644 -index 000000000..d52f638eb ---- /dev/null -+++ b/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_calib.c -@@ -0,0 +1,507 @@ -+// SPDX-License-Identifier: BSD-3-Clause -+/* -+ * Copyright (c) 2018-2020, STMicroelectronics -+ */ -+ -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+ -+#define CALIBRATION_TIMEOUT_US 10000 -+ -+/* List of forbiden values for HSI and CSI */ -+static const uint16_t fbv_hsi[] = { -+ 512, 480, 448, 416, 384, 352, 320, 288, -+ 256, 224, 192, 160, 128, 96, 64, 32, 0, -+}; -+static const uint16_t fbv_csi[] = { -+ 256, 240, 224, 208, 192, 176, 160, 144, -+ 128, 112, 96, 80, 64, 48, 32, 16, 0, -+}; -+ -+struct stm32mp1_trim_boundary_t { -+ unsigned int max; /* Max boundary trim value around forbidden value */ -+ unsigned int min; /* Min boundary trim value around forbidden value */ -+}; -+ -+struct stm32mp1_clk_cal { -+ const uint16_t *fbv; -+ unsigned int cal_ref; -+ int trim_max; -+ int trim_min; -+ unsigned int boundary_max; -+ unsigned long ref_freq; -+ unsigned int freq_margin; -+ unsigned long (*get_freq)(void); -+ void (*set_trim)(unsigned int cal); -+ unsigned int (*get_trim)(void); -+ struct stm32mp1_trim_boundary_t boundary[16]; -+}; -+ -+static void hsi_set_trim(unsigned int cal); -+static unsigned int hsi_get_trimed_cal(void); -+static void csi_set_trim(unsigned int cal); -+static unsigned int csi_get_trimed_cal(void); -+ -+static struct stm32mp1_clk_cal *hsi_calib; -+static struct stm32mp1_clk_cal *csi_calib; -+ -+static const struct stm32mp1_clk_cal hsi_calib_config = { -+ .fbv = fbv_hsi, -+ .trim_max = 63, -+ .trim_min = -64, -+ .ref_freq = 0, -+ .freq_margin = 5, -+ .set_trim = hsi_set_trim, -+ .get_trim = hsi_get_trimed_cal, -+}; -+ -+static const struct stm32mp1_clk_cal csi_calib_config = { -+ .fbv = fbv_csi, -+ .trim_max = 15, -+ .trim_min = -16, -+ .ref_freq = 0, -+ .freq_margin = 8, -+ .set_trim = csi_set_trim, -+ .get_trim = csi_get_trimed_cal, -+}; -+ -+static int get_signed_value(uint8_t val) -+{ -+ return (int8_t)(val << 1) / 2; -+} -+ -+static void hsi_set_trim(unsigned int cal) -+{ -+ int clk_trim = (int)cal - (int)hsi_calib->cal_ref; -+ uint32_t trim = ((uint32_t)clk_trim << RCC_HSICFGR_HSITRIM_SHIFT) & -+ RCC_HSICFGR_HSITRIM_MASK; -+ -+ io_clrsetbits32(stm32_rcc_base() + RCC_HSICFGR, -+ RCC_HSICFGR_HSITRIM_MASK, trim); -+} -+DECLARE_KEEP_PAGER(hsi_set_trim); -+ -+static unsigned int hsi_get_trimed_cal(void) -+{ -+ uint32_t utrim = (io_read32(stm32_rcc_base() + RCC_HSICFGR) & -+ RCC_HSICFGR_HSITRIM_MASK) >> -+ RCC_HSICFGR_HSITRIM_SHIFT; -+ int trim = get_signed_value((uint8_t)utrim); -+ -+ if (trim + (int)hsi_calib->cal_ref < 0) -+ return 0; -+ -+ return hsi_calib->cal_ref + trim; -+} -+DECLARE_KEEP_PAGER(hsi_get_trimed_cal); -+ -+static void csi_set_trim(unsigned int cal) -+{ -+ int clk_trim = (int)cal - (int)csi_calib->cal_ref + -+ csi_calib->trim_max + 1; -+ uint32_t trim = ((uint32_t)clk_trim << RCC_CSICFGR_CSITRIM_SHIFT) & -+ RCC_CSICFGR_CSITRIM_MASK; -+ -+ io_clrsetbits32(stm32_rcc_base() + RCC_CSICFGR, -+ RCC_CSICFGR_CSITRIM_MASK, trim); -+} -+DECLARE_KEEP_PAGER(csi_set_trim); -+ -+static unsigned int csi_get_trimed_cal(void) -+{ -+ uint32_t trim = (io_read32(stm32_rcc_base() + RCC_CSICFGR) & -+ RCC_CSICFGR_CSITRIM_MASK) >> -+ RCC_CSICFGR_CSITRIM_SHIFT; -+ -+ return (int)trim - csi_calib->trim_max + (int)csi_calib->cal_ref - 1; -+} -+DECLARE_KEEP_PAGER(csi_get_trimed_cal); -+ -+static unsigned int trim_increase(struct stm32mp1_clk_cal *clk_cal, -+ unsigned int cal) -+{ -+ struct stm32mp1_trim_boundary_t *boundary = NULL; -+ unsigned int new_cal = 0; -+ int i = 0; -+ -+ /* By default: last calibration value */ -+ new_cal = cal; -+ -+ /* Start from Lowest cal value */ -+ for (i = (int)clk_cal->boundary_max - 1; i >= 0; i--) { -+ boundary = &clk_cal->boundary[i]; -+ -+ if (cal < boundary->min) { -+ new_cal = boundary->min; -+ break; -+ } -+ -+ if ((cal >= boundary->min) && (cal < boundary->max)) { -+ new_cal = cal + 1; -+ break; -+ } -+ } -+ -+ return new_cal; -+} -+ -+static unsigned int trim_decrease(struct stm32mp1_clk_cal *clk_cal, -+ unsigned int cal) -+{ -+ struct stm32mp1_trim_boundary_t *boundary = NULL; -+ unsigned int new_cal = 0; -+ unsigned int i = 0; -+ -+ /* By default: last calibration value */ -+ new_cal = cal; -+ -+ /* Start from Highest cal value */ -+ for (i = 0; i < clk_cal->boundary_max; i++) { -+ boundary = &clk_cal->boundary[i]; -+ -+ if (cal > boundary->max) { -+ new_cal = boundary->max; -+ break; -+ } -+ -+ if ((cal > boundary->min) && (cal <= boundary->max)) { -+ new_cal = cal - 1; -+ break; -+ } -+ } -+ -+ return new_cal; -+} -+ -+static void rcc_calibration(struct stm32mp1_clk_cal *clk_cal) -+{ -+ unsigned long margin = (clk_cal->ref_freq * -+ clk_cal->freq_margin) / 1000; -+ unsigned long min = clk_cal->ref_freq - margin; -+ unsigned long max = clk_cal->ref_freq + margin; -+ unsigned long freq = clk_cal->get_freq(); -+ int trim = 0; -+ int new_trim = 0; -+ unsigned long conv = 0; -+ unsigned long min_conv = ULONG_MAX; -+ uint64_t timeout_ref = 0; -+ -+ if ((freq >= min) && (freq <= max)) -+ return; -+ -+ trim = clk_cal->get_trim(); -+ timeout_ref = timeout_init_us(CALIBRATION_TIMEOUT_US); -+ do { -+ if (freq < clk_cal->ref_freq) -+ new_trim = trim_increase(clk_cal, trim); -+ else -+ new_trim = trim_decrease(clk_cal, trim); -+ -+ clk_cal->set_trim(new_trim); -+ freq = clk_cal->get_freq(); -+ if (freq == 0) { -+ /* Calibration will be stopped */ -+ clk_cal->ref_freq = 0U; -+ return; -+ } -+ conv = (clk_cal->ref_freq < freq) ? -+ freq - clk_cal->ref_freq : clk_cal->ref_freq - freq; -+ if (conv < min_conv) { -+ min_conv = conv; -+ trim = new_trim; -+ } -+ -+ if (timeout_elapsed(timeout_ref)) -+ break; -+ -+ } while (conv == min_conv); -+ -+ clk_cal->set_trim(trim); -+ freq = clk_cal->get_freq(); -+ if ((freq < min) || (freq > max)) { -+ EMSG("%s Calibration : Freq %lu , trim %i\n", -+ (clk_cal->set_trim == hsi_set_trim) ? "HSI" : "CSI", -+ freq, trim); -+ } -+} -+ -+static void save_trim(struct stm32mp1_clk_cal *clk_cal, -+ unsigned int i, unsigned int max, unsigned int min) -+{ -+ clk_cal->boundary[i].max = max; -+ clk_cal->boundary[i].min = min; -+} -+ -+static int trim_find_prev_boundary(struct stm32mp1_clk_cal *clk_cal, -+ unsigned int x1) -+{ -+ unsigned int x = x1; -+ unsigned long freq = 0; -+ -+ clk_cal->set_trim(x1 + 1); -+ freq = clk_cal->get_freq(); -+ -+ while (x >= (clk_cal->cal_ref + clk_cal->trim_min)) { -+ x--; -+ clk_cal->set_trim(x); -+ -+ if (clk_cal->get_freq() <= freq) -+ break; -+ }; -+ -+ return x; -+} -+ -+static void trim_table_init(struct stm32mp1_clk_cal *clk_cal) -+{ -+ const uint16_t *trim_fbv = clk_cal->fbv; -+ unsigned int min = 0; -+ unsigned int max = 0; -+ int boundary = 0; -+ int i = 0; -+ -+ max = clk_cal->cal_ref + clk_cal->trim_max; -+ min = clk_cal->cal_ref + clk_cal->trim_min; -+ -+ while (trim_fbv[i]) { -+ unsigned int x = 0; -+ unsigned int x1 = trim_fbv[i]; -+ unsigned int x2 = trim_fbv[i + 1]; -+ -+ if ((max <= x2) || (min >= x1)) { -+ i++; -+ if (boundary != 0) -+ goto out; -+ -+ continue; -+ } -+ -+ /* Take forbiden value + 1 */ -+ x2 = x2 + 1; -+ if (x2 < min) -+ x2 = min; -+ -+ if (boundary == 0) { -+ /* Save first boundary */ -+ save_trim(clk_cal, boundary, max, x2); -+ boundary++; -+ i++; -+ continue; -+ } -+ -+ x = trim_find_prev_boundary(clk_cal, x1); -+ /* Save boundary values */ -+ save_trim(clk_cal, boundary, x - 1, x2); -+ boundary++; -+ i++; -+ }; -+out: -+ clk_cal->boundary_max = boundary; -+} -+ -+/* Timer countdown/delay argument for the target calibration periodicity */ -+static uint32_t timer_val; -+ -+#define CNTP_CTL_ENABLE BIT(0) -+#define CNTP_CTL_IMASK BIT(1) -+#define CNTP_CTL_ISTATUS BIT(2) -+ -+static void arm_timer(void) -+{ -+ if (!timer_val) -+ return; -+ -+ write_cntp_ctl(read_cntp_ctl() & ~(CNTP_CTL_ENABLE | CNTP_CTL_IMASK)); -+ write_cntp_tval(timer_val); -+ write_cntp_ctl(read_cntp_ctl() | CNTP_CTL_ENABLE); -+} -+ -+static void arm_timer_with_period(uint32_t period_sec) -+{ -+ timer_val = period_sec * read_cntfrq(); -+ -+ if (timer_val > INT32_MAX) -+ timer_val = INT32_MAX; -+ -+ DMSG("Calibration timeout set to %"PRIu32, timer_val / read_cntfrq()); -+ -+ arm_timer(); -+} -+ -+static void calib_period(void) -+{ -+ (void)stm32mp_start_clock_calib(CK_HSI); -+ (void)stm32mp_start_clock_calib(CK_CSI); -+ -+ arm_timer(); -+} -+ -+static enum itr_return arm_cntp_it_handler(struct itr_handler *handler __unused) -+{ -+ if (timer_val) -+ calib_period(); -+ -+ return ITRR_HANDLED; -+} -+static struct itr_handler arm_cntp_handler = { -+ .it = GIC_SPI_SEC_PHY_TIMER, -+ .handler = arm_cntp_it_handler, -+}; -+DECLARE_KEEP_PAGER(arm_cntp_handler); -+ -+static TEE_Result timer_pm(enum pm_op op, uint32_t pm_hint __unused, -+ const struct pm_callback_handle *handle __unused) -+{ -+ if (op == PM_OP_RESUME && timer_val) -+ calib_period(); -+ -+ return TEE_SUCCESS; -+} -+DECLARE_KEEP_PAGER(timer_pm); -+ -+static TEE_Result init_arm_cntp_timer(void) -+{ -+ itr_add(&arm_cntp_handler); -+ itr_enable(arm_cntp_handler.it); -+ -+ register_pm_driver_cb(timer_pm, NULL); -+ -+ return TEE_SUCCESS; -+} -+driver_init(init_arm_cntp_timer); -+ -+static void init_periodic_calibration(void *fdt, int node) -+{ -+ uint32_t period = 0; -+ int lenp = 0; -+ const fdt32_t *cuint = fdt_getprop(fdt, node, "st,cal-sec", &lenp); -+ -+ if (cuint) -+ period = fdt32_to_cpu(*cuint); -+ -+ DMSG("Calib period %us", period); -+ arm_timer_with_period(period); -+} -+ -+int stm32mp_start_clock_calib(unsigned int clock_id) -+{ -+ struct stm32mp1_clk_cal *clk_calib = NULL; -+ -+ switch (clock_id) { -+ case CK_HSI: -+ clk_calib = hsi_calib; -+ break; -+ case CK_CSI: -+ clk_calib = csi_calib; -+ break; -+ default: -+ DMSG("Cannot calibrate clock %u", clock_id); -+ return 1; -+ } -+ -+ if (clk_calib->ref_freq == 0U) -+ return 1; -+ -+ DMSG("%s", clock_id == CK_HSI ? "HSI" : "CSI"); -+ rcc_calibration(clk_calib); -+ -+ return 0; -+} -+ -+static int init_hsi_calibration(void *fdt, int node) -+{ -+ if (!fdt_getprop(fdt, node, "st,hsi-cal", NULL)) -+ return 0; -+ -+ hsi_calib = calloc(1, sizeof(*hsi_calib)); -+ assert(hsi_calib); -+ memcpy(hsi_calib, &hsi_calib_config, sizeof(*hsi_calib)); -+ -+ stm32_tim_freq_func(&hsi_calib->get_freq, HSI_CAL); -+ if (hsi_calib->get_freq == NULL) { -+ free(hsi_calib); -+ return -1; -+ } -+ -+ hsi_calib->ref_freq = clk_get_rate(CK_HSI); -+ -+ hsi_calib->cal_ref = (io_read32(stm32_rcc_base() + RCC_HSICFGR) & -+ RCC_HSICFGR_HSICAL_MASK) >> -+ RCC_HSICFGR_HSICAL_SHIFT; -+ -+ trim_table_init(hsi_calib); -+ hsi_calib->set_trim(hsi_calib->cal_ref); -+ stm32mp_start_clock_calib(CK_HSI); -+ return 1; -+} -+ -+static int init_csi_calibration(void *fdt, int node) -+{ -+ if (!fdt_getprop(fdt, node, "st,csi-cal", NULL)) -+ return 0; -+ -+ csi_calib = calloc(1, sizeof(*csi_calib)); -+ assert(csi_calib); -+ memcpy(csi_calib, &csi_calib_config, sizeof(*csi_calib)); -+ -+ stm32_tim_freq_func(&csi_calib->get_freq, CSI_CAL); -+ if (csi_calib->get_freq == NULL) { -+ free(csi_calib); -+ return -1; -+ } -+ -+ csi_calib->ref_freq = clk_get_rate(CK_CSI); -+ -+ csi_calib->cal_ref = (io_read32(stm32_rcc_base() + RCC_CSICFGR) & -+ RCC_CSICFGR_CSICAL_MASK) >> -+ RCC_CSICFGR_CSICAL_SHIFT; -+ trim_table_init(csi_calib); -+ csi_calib->set_trim(csi_calib->cal_ref); -+ stm32mp_start_clock_calib(CK_CSI); -+ return 1; -+} -+ -+static TEE_Result init_stm32mp1_calib(void) -+{ -+ void *fdt = NULL; -+ int rcc_node = 0; -+ int res_csi = 0; -+ int res_hsi = 0; -+ -+ fdt = get_embedded_dt(); -+ if (!fdt) -+ panic(); -+ -+ rcc_node = fdt_node_offset_by_compatible(fdt, -1, DT_RCC_CLK_COMPAT); -+ if (rcc_node < 0) -+ panic(); -+ -+ res_hsi = init_hsi_calibration(fdt, rcc_node); -+ if (res_hsi < 0) -+ panic("HSI calibration init failed"); -+ res_csi = init_csi_calibration(fdt, rcc_node); -+ if (res_csi < 0) -+ panic("CSI calibration init failed"); -+ if (res_csi || res_hsi) -+ init_periodic_calibration(fdt, rcc_node); -+ -+ return TEE_SUCCESS; -+} -+driver_init(init_stm32mp1_calib); -diff --git a/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_ddrc.c b/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_ddrc.c -new file mode 100644 -index 000000000..1c2d9b2c8 ---- /dev/null -+++ b/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_ddrc.c -@@ -0,0 +1,469 @@ -+// SPDX-License-Identifier: BSD-3-Clause -+/* -+ * Copyright (c) 2017-2020, STMicroelectronics - All Rights Reserved -+ * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved. -+ */ -+ -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+ -+#define TIMEOUT_500US 500 -+ -+static enum stm32mp1_ddr_sr_mode saved_ddr_sr_mode; -+ -+static vaddr_t get_ddrctrl_base(void) -+{ -+ static struct io_pa_va base __nex_data = { .pa = DDRCTRL_BASE }; -+ -+ return io_pa_or_va(&base); -+} -+ -+static vaddr_t get_ddrphy_base(void) -+{ -+ static struct io_pa_va base __nex_data = { .pa = DDRPHYC_BASE }; -+ -+ return io_pa_or_va(&base); -+} -+ -+static void ddr_disable_clock(void) -+{ -+ vaddr_t rcc_base = stm32_rcc_base(); -+ -+ /* Disable all clocks */ -+ io_clrbits32(rcc_base + RCC_DDRITFCR, -+ RCC_DDRITFCR_DDRC1EN | -+ RCC_DDRITFCR_DDRC2EN | -+ RCC_DDRITFCR_DDRPHYCAPBEN | -+ RCC_DDRITFCR_DDRCAPBEN); -+} -+ -+static void ddr_enable_clock(void) -+{ -+ vaddr_t rcc_base = stm32_rcc_base(); -+ -+ /* Enable all clocks */ -+ io_setbits32(rcc_base + RCC_DDRITFCR, -+ RCC_DDRITFCR_DDRC1EN | -+ RCC_DDRITFCR_DDRC2EN | -+ RCC_DDRITFCR_DDRPHYCEN | -+ RCC_DDRITFCR_DDRPHYCAPBEN | -+ RCC_DDRITFCR_DDRCAPBEN); -+} -+ -+static void do_sw_handshake(void) -+{ -+ vaddr_t ddrctrl_base = get_ddrctrl_base(); -+ -+ io_clrbits32(ddrctrl_base + DDRCTRL_SWCTL, DDRCTRL_SWCTL_SW_DONE); -+} -+ -+static void do_sw_ack(void) -+{ -+ uint64_t timeout_ref = 0; -+ vaddr_t ddrctrl_base = get_ddrctrl_base(); -+ -+ io_setbits32(ddrctrl_base + DDRCTRL_SWCTL, DDRCTRL_SWCTL_SW_DONE); -+ -+ timeout_ref = timeout_init_us(TIMEOUT_500US); -+ while (!timeout_elapsed(timeout_ref)) -+ if (io_read32(ddrctrl_base + DDRCTRL_SWSTAT) & -+ DDRCTRL_SWSTAT_SW_DONE_ACK) -+ return; -+ -+ panic(); -+} -+ -+static int ddr_sw_self_refresh_in(void) -+{ -+ uint64_t timeout_ref = 0; -+ uint32_t operating_mode = 0; -+ uint32_t selref_type = 0; -+ uint8_t op_mode_changed = 0; -+ vaddr_t pwr_base = stm32_pwr_base(); -+ vaddr_t rcc_base = stm32_rcc_base(); -+ vaddr_t ddrctrl_base = get_ddrctrl_base(); -+ vaddr_t ddrphy_base = get_ddrphy_base(); -+ -+ io_clrbits32(rcc_base + RCC_DDRITFCR, RCC_DDRITFCR_AXIDCGEN); -+ -+ /* Blocks AXI ports from taking anymore transactions */ -+ io_clrbits32(ddrctrl_base + DDRCTRL_PCTRL_0, DDRCTRL_PCTRL_N_PORT_EN); -+ io_clrbits32(ddrctrl_base + DDRCTRL_PCTRL_1, DDRCTRL_PCTRL_N_PORT_EN); -+ -+ /* -+ * Waits unit all AXI ports are idle -+ * Poll PSTAT.rd_port_busy_n = 0 -+ * Poll PSTAT.wr_port_busy_n = 0 -+ */ -+ timeout_ref = timeout_init_us(TIMEOUT_500US); -+ while (io_read32(ddrctrl_base + DDRCTRL_PSTAT)) -+ if (timeout_elapsed(timeout_ref)) -+ goto pstat_failed; -+ -+ /* SW Self-Refresh entry */ -+ io_setbits32(ddrctrl_base + DDRCTRL_PWRCTL, DDRCTRL_PWRCTL_SELFREF_SW); -+ -+ /* -+ * Wait operating mode change in self-refresh mode -+ * with STAT.operating_mode[1:0]==11. -+ * Ensure transition to self-refresh was due to software -+ * by checking also that STAT.selfref_type[1:0]=2. -+ */ -+ timeout_ref = timeout_init_us(TIMEOUT_500US); -+ while (!timeout_elapsed(timeout_ref)) { -+ uint32_t stat = io_read32(ddrctrl_base + DDRCTRL_STAT); -+ -+ operating_mode = stat & DDRCTRL_STAT_OPERATING_MODE_MASK; -+ selref_type = stat & DDRCTRL_STAT_SELFREF_TYPE_MASK; -+ -+ if ((operating_mode == DDRCTRL_STAT_OPERATING_MODE_SR) && -+ (selref_type == DDRCTRL_STAT_SELFREF_TYPE_SR)) { -+ op_mode_changed = 1; -+ break; -+ } -+ } -+ -+ if (op_mode_changed == 0U) -+ goto selfref_sw_failed; -+ -+ /* IOs powering down (PUBL registers) */ -+ io_setbits32(ddrphy_base + DDRPHYC_ACIOCR, DDRPHYC_ACIOCR_ACPDD); -+ io_setbits32(ddrphy_base + DDRPHYC_ACIOCR, DDRPHYC_ACIOCR_ACPDR); -+ io_clrsetbits32(ddrphy_base + DDRPHYC_ACIOCR, -+ DDRPHYC_ACIOCR_CKPDD_MASK, DDRPHYC_ACIOCR_CKPDD_0); -+ io_clrsetbits32(ddrphy_base + DDRPHYC_ACIOCR, -+ DDRPHYC_ACIOCR_CKPDR_MASK, DDRPHYC_ACIOCR_CKPDR_0); -+ io_clrsetbits32(ddrphy_base + DDRPHYC_ACIOCR, -+ DDRPHYC_ACIOCR_CSPDD_MASK, DDRPHYC_ACIOCR_CSPDD_0); -+ io_clrbits32(ddrphy_base + DDRPHYC_ACIOCR, DDRPHYC_ACIOCR_ACOE); -+ io_setbits32(ddrphy_base + DDRPHYC_DXCCR, DDRPHYC_DXCCR_DXPDD); -+ io_setbits32(ddrphy_base + DDRPHYC_DXCCR, DDRPHYC_DXCCR_DXPDR); -+ io_clrsetbits32(ddrphy_base + DDRPHYC_DSGCR, -+ DDRPHYC_DSGCR_ODTPDD_MASK, DDRPHYC_DSGCR_ODTPDD_0); -+ io_setbits32(ddrphy_base + DDRPHYC_DSGCR, DDRPHYC_DSGCR_NL2PD); -+ io_clrsetbits32(ddrphy_base + DDRPHYC_DSGCR, -+ DDRPHYC_DSGCR_CKEPDD_MASK, DDRPHYC_DSGCR_CKEPDD_0); -+ -+ /* Disable PZQ cell (PUBL register) */ -+ io_setbits32(ddrphy_base + DDRPHYC_ZQ0CR0, DDRPHYC_ZQ0CRN_ZQPD); -+ -+ /* Activate sw retention in PWRCTRL */ -+ io_setbits32(pwr_base + PWR_CR3_OFF, PWR_CR3_DDRRETEN); -+ -+ /* Switch controller clocks (uMCTL2/PUBL) to DLL ref clock */ -+ io_setbits32(rcc_base + RCC_DDRITFCR, RCC_DDRITFCR_GSKPCTRL); -+ -+ /* Disable all DLLs: GLITCH window */ -+ io_setbits32(ddrphy_base + DDRPHYC_ACDLLCR, DDRPHYC_ACDLLCR_DLLDIS); -+ io_setbits32(ddrphy_base + DDRPHYC_DX0DLLCR, DDRPHYC_DXNDLLCR_DLLDIS); -+ io_setbits32(ddrphy_base + DDRPHYC_DX1DLLCR, DDRPHYC_DXNDLLCR_DLLDIS); -+ io_setbits32(ddrphy_base + DDRPHYC_DX2DLLCR, DDRPHYC_DXNDLLCR_DLLDIS); -+ io_setbits32(ddrphy_base + DDRPHYC_DX3DLLCR, DDRPHYC_DXNDLLCR_DLLDIS); -+ -+ /* Switch controller clocks (uMCTL2/PUBL) to DLL output clock */ -+ io_clrbits32(rcc_base + RCC_DDRITFCR, RCC_DDRITFCR_GSKPCTRL); -+ -+ /* Disable all clocks */ -+ ddr_disable_clock(); -+ -+ return 0; -+ -+selfref_sw_failed: -+ /* This bit should be cleared to restore DDR in its previous state */ -+ io_clrbits32(ddrctrl_base + DDRCTRL_PWRCTL, DDRCTRL_PWRCTL_SELFREF_SW); -+ -+pstat_failed: -+ io_setbits32(ddrctrl_base + DDRCTRL_PCTRL_0, DDRCTRL_PCTRL_N_PORT_EN); -+ io_setbits32(ddrctrl_base + DDRCTRL_PCTRL_1, DDRCTRL_PCTRL_N_PORT_EN); -+ -+ return -1; -+} -+ -+static int ddr_sw_self_refresh_exit(void) -+{ -+ uint64_t timeout_ref = 0; -+ vaddr_t rcc_base = stm32_rcc_base(); -+ vaddr_t pwr_base = stm32_pwr_base(); -+ vaddr_t ddrctrl_base = get_ddrctrl_base(); -+ vaddr_t ddrphy_base = get_ddrphy_base(); -+ -+ /* Enable all clocks */ -+ ddr_enable_clock(); -+ -+ do_sw_handshake(); -+ -+ /* Mask dfi_init_complete_en */ -+ io_clrbits32(ddrctrl_base + DDRCTRL_DFIMISC, -+ DDRCTRL_DFIMISC_DFI_INIT_COMPLETE_EN); -+ -+ do_sw_ack(); -+ -+ /* Switch controller clocks (uMCTL2/PUBL) to DLL ref clock */ -+ io_setbits32(rcc_base + RCC_DDRITFCR, RCC_DDRITFCR_GSKPCTRL); -+ -+ /* Enable all DLLs: GLITCH window */ -+ io_clrbits32(ddrphy_base + DDRPHYC_ACDLLCR, DDRPHYC_ACDLLCR_DLLDIS); -+ io_clrbits32(ddrphy_base + DDRPHYC_DX0DLLCR, DDRPHYC_DXNDLLCR_DLLDIS); -+ io_clrbits32(ddrphy_base + DDRPHYC_DX1DLLCR, DDRPHYC_DXNDLLCR_DLLDIS); -+ io_clrbits32(ddrphy_base + DDRPHYC_DX2DLLCR, DDRPHYC_DXNDLLCR_DLLDIS); -+ io_clrbits32(ddrphy_base + DDRPHYC_DX3DLLCR, DDRPHYC_DXNDLLCR_DLLDIS); -+ -+ /* Additional delay to avoid early DLL clock switch */ -+ udelay(50); -+ -+ /* Switch controller clocks (uMCTL2/PUBL) to DLL ref clock */ -+ io_clrbits32(rcc_base + RCC_DDRITFCR, RCC_DDRITFCR_GSKPCTRL); -+ io_clrbits32(ddrphy_base + DDRPHYC_ACDLLCR, DDRPHYC_ACDLLCR_DLLSRST); -+ udelay(10); -+ io_setbits32(ddrphy_base + DDRPHYC_ACDLLCR, DDRPHYC_ACDLLCR_DLLSRST); -+ -+ /* PHY partial init: (DLL lock and ITM reset) */ -+ io_write32(ddrphy_base + DDRPHYC_PIR, -+ DDRPHYC_PIR_DLLSRST | DDRPHYC_PIR_DLLLOCK | -+ DDRPHYC_PIR_ITMSRST | DDRPHYC_PIR_INIT); -+ -+ /* Need to wait at least 10 clock cycles before accessing PGSR */ -+ udelay(10); -+ -+ timeout_ref = timeout_init_us(TIMEOUT_500US); -+ while (!(io_read32(ddrphy_base + DDRPHYC_PGSR) & DDRPHYC_PGSR_IDONE)) -+ if (timeout_elapsed(timeout_ref)) -+ return -1; -+ -+ do_sw_handshake(); -+ -+ /* Unmask dfi_init_complete_en to uMCTL2 */ -+ io_setbits32(ddrctrl_base + DDRCTRL_DFIMISC, -+ DDRCTRL_DFIMISC_DFI_INIT_COMPLETE_EN); -+ -+ do_sw_ack(); -+ -+ /* Deactivate sw retention in PWR */ -+ io_clrbits32(pwr_base + PWR_CR3_OFF, PWR_CR3_DDRRETEN); -+ -+ /* Enable PZQ cell (PUBL register) */ -+ io_clrbits32(ddrphy_base + DDRPHYC_ZQ0CR0, DDRPHYC_ZQ0CRN_ZQPD); -+ -+ /* Enable pad drivers */ -+ io_clrbits32(ddrphy_base + DDRPHYC_ACIOCR, DDRPHYC_ACIOCR_ACPDD); -+ io_setbits32(ddrphy_base + DDRPHYC_ACIOCR, DDRPHYC_ACIOCR_ACOE); -+ io_clrbits32(ddrphy_base + DDRPHYC_ACIOCR, DDRPHYC_ACIOCR_CKPDD_MASK); -+ io_clrbits32(ddrphy_base + DDRPHYC_ACIOCR, DDRPHYC_ACIOCR_CSPDD_MASK); -+ io_clrbits32(ddrphy_base + DDRPHYC_DXCCR, DDRPHYC_DXCCR_DXPDD); -+ io_clrbits32(ddrphy_base + DDRPHYC_DXCCR, DDRPHYC_DXCCR_DXPDR); -+ io_clrbits32(ddrphy_base + DDRPHYC_DSGCR, DDRPHYC_DSGCR_ODTPDD_MASK); -+ io_clrbits32(ddrphy_base + DDRPHYC_DSGCR, DDRPHYC_DSGCR_NL2PD); -+ io_clrbits32(ddrphy_base + DDRPHYC_DSGCR, DDRPHYC_DSGCR_CKEPDD_MASK); -+ -+ /* Remove selfrefresh */ -+ io_clrbits32(ddrctrl_base + DDRCTRL_PWRCTL, DDRCTRL_PWRCTL_SELFREF_SW); -+ -+ /* Wait operating_mode == normal */ -+ timeout_ref = timeout_init_us(TIMEOUT_500US); -+ while (1) { -+ if ((io_read32(ddrctrl_base + DDRCTRL_STAT) & -+ DDRCTRL_STAT_OPERATING_MODE_MASK) == -+ DDRCTRL_STAT_OPERATING_MODE_NORMAL) -+ break; -+ -+ if (timeout_elapsed(timeout_ref)) -+ return -1; -+ } -+ -+ /* AXI ports are no longer blocked from taking transactions */ -+ io_setbits32(ddrctrl_base + DDRCTRL_PCTRL_0, DDRCTRL_PCTRL_N_PORT_EN); -+ io_setbits32(ddrctrl_base + DDRCTRL_PCTRL_1, DDRCTRL_PCTRL_N_PORT_EN); -+ -+ io_setbits32(rcc_base + RCC_DDRITFCR, RCC_DDRITFCR_AXIDCGEN); -+ -+ return 0; -+} -+ -+uint32_t get_ddrphy_calibration(void) -+{ -+ vaddr_t ddrphy_base = get_ddrphy_base(); -+ uint32_t zcal = io_read32(ddrphy_base + DDRPHYC_ZQ0CR0); -+ -+ return (zcal & DDRPHYC_ZQ0CRN_ZDATA_MASK) >> DDRPHYC_ZQ0CRN_ZDATA_SHIFT; -+} -+ -+int ddr_standby_sr_entry(void) -+{ -+ vaddr_t pwr_base = stm32_pwr_base(); -+ -+ if (ddr_sw_self_refresh_in()) -+ return -1; -+ -+ /* Enable I/O retention mode in standby */ -+ io_setbits32(pwr_base + PWR_CR3_OFF, PWR_CR3_DDRSREN); -+ -+ return 0; -+} -+ -+int ddr_standby_sr_exit(void) -+{ -+ return ddr_sw_self_refresh_exit(); -+} -+ -+static void ddr_sr_mode_ssr(void) -+{ -+ vaddr_t rcc_ddritfcr = stm32_rcc_base() + RCC_DDRITFCR; -+ vaddr_t ddrctrl_base = get_ddrctrl_base(); -+ -+ io_setbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRC1LPEN); -+ io_setbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRC2LPEN); -+ io_setbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRC1EN); -+ io_setbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRC2EN); -+ io_setbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRCAPBLPEN); -+ io_setbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRPHYCAPBLPEN); -+ io_setbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRCAPBEN); -+ io_setbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRPHYCAPBEN); -+ io_setbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRPHYCEN); -+ io_clrbits32(rcc_ddritfcr, RCC_DDRITFCR_AXIDCGEN); -+ io_clrbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRCKMOD_MASK); -+ -+ /* Disable HW LP interface of uMCTL2 */ -+ io_clrbits32(ddrctrl_base + DDRCTRL_HWLPCTL, DDRCTRL_HWLPCTL_HW_LP_EN); -+ -+ /* Configure Automatic LP modes of uMCTL2 */ -+ io_clrsetbits32(ddrctrl_base + DDRCTRL_PWRTMG, -+ DDRCTRL_PWRTMG_SELFREF_TO_X32_MASK, -+ DDRCTRL_PWRTMG_SELFREF_TO_X32_0); -+ -+ /* -+ * Disable Clock disable with LP modes -+ * (used in RUN mode for LPDDR2 with specific timing). -+ */ -+ io_clrbits32(ddrctrl_base + DDRCTRL_PWRCTL, -+ DDRCTRL_PWRCTL_EN_DFI_DRAM_CLK_DISABLE); -+ -+ /* Disable automatic Self-Refresh mode */ -+ io_clrbits32(ddrctrl_base + DDRCTRL_PWRCTL, -+ DDRCTRL_PWRCTL_SELFREF_EN); -+} -+ -+static void ddr_sr_mode_asr(void) -+{ -+ vaddr_t rcc_ddritfcr = stm32_rcc_base() + RCC_DDRITFCR; -+ vaddr_t ddrctrl_base = get_ddrctrl_base(); -+ -+ io_setbits32(rcc_ddritfcr, RCC_DDRITFCR_AXIDCGEN); -+ io_setbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRC1LPEN); -+ io_setbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRC2LPEN); -+ io_setbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRPHYCLPEN); -+ io_clrsetbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRCKMOD_MASK, -+ RCC_DDRITFCR_DDRCKMOD_ASR1); -+ -+ /* Enable HW LP interface of uMCTL2 */ -+ io_setbits32(ddrctrl_base + DDRCTRL_HWLPCTL, DDRCTRL_HWLPCTL_HW_LP_EN); -+ -+ /* Configure Automatic LP modes of uMCTL2 */ -+ io_clrsetbits32(ddrctrl_base + DDRCTRL_PWRTMG, -+ DDRCTRL_PWRTMG_SELFREF_TO_X32_MASK, -+ DDRCTRL_PWRTMG_SELFREF_TO_X32_0); -+ -+ /* -+ * Enable Clock disable with LP modes -+ * (used in RUN mode for LPDDR2 with specific timing). -+ */ -+ io_setbits32(ddrctrl_base + DDRCTRL_PWRCTL, -+ DDRCTRL_PWRCTL_EN_DFI_DRAM_CLK_DISABLE); -+ -+ /* Enable automatic Self-Refresh for ASR mode */ -+ io_setbits32(ddrctrl_base + DDRCTRL_PWRCTL, -+ DDRCTRL_PWRCTL_SELFREF_EN); -+} -+ -+static void ddr_sr_mode_hsr(void) -+{ -+ vaddr_t rcc_ddritfcr = stm32_rcc_base() + RCC_DDRITFCR; -+ vaddr_t ddrctrl_base = get_ddrctrl_base(); -+ -+ io_setbits32(rcc_ddritfcr, RCC_DDRITFCR_AXIDCGEN); -+ io_clrbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRC1LPEN); -+ io_clrbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRC2LPEN); -+ io_setbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRPHYCLPEN); -+ io_clrsetbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRCKMOD_MASK, -+ RCC_DDRITFCR_DDRCKMOD_HSR1); -+ -+ /* Enable HW LP interface of uMCTL2 */ -+ io_setbits32(ddrctrl_base + DDRCTRL_HWLPCTL, DDRCTRL_HWLPCTL_HW_LP_EN); -+ -+ /* Configure Automatic LP modes of uMCTL2 */ -+ io_clrsetbits32(ddrctrl_base + DDRCTRL_PWRTMG, -+ DDRCTRL_PWRTMG_SELFREF_TO_X32_MASK, -+ DDRCTRL_PWRTMG_SELFREF_TO_X32_0); -+ -+ /* -+ * Enable Clock disable with LP modes -+ * (used in RUN mode for LPDDR2 with specific timing). -+ */ -+ io_setbits32(ddrctrl_base + DDRCTRL_PWRCTL, -+ DDRCTRL_PWRCTL_EN_DFI_DRAM_CLK_DISABLE); -+} -+ -+static enum stm32mp1_ddr_sr_mode ddr_read_sr_mode(void) -+{ -+ uint32_t pwrctl = io_read32(get_ddrctrl_base() + DDRCTRL_PWRCTL); -+ uint32_t mask = DDRCTRL_PWRCTL_EN_DFI_DRAM_CLK_DISABLE | -+ DDRCTRL_PWRCTL_SELFREF_EN; -+ -+ switch (pwrctl & mask) { -+ case 0U: -+ return DDR_SSR_MODE; -+ -+ case DDRCTRL_PWRCTL_EN_DFI_DRAM_CLK_DISABLE: -+ return DDR_HSR_MODE; -+ -+ case DDRCTRL_PWRCTL_EN_DFI_DRAM_CLK_DISABLE | DDRCTRL_PWRCTL_SELFREF_EN: -+ return DDR_ASR_MODE; -+ -+ default: -+ return DDR_SR_MODE_INVALID; -+ } -+} -+ -+static void ddr_set_sr_mode(enum stm32mp1_ddr_sr_mode mode) -+{ -+ switch (mode) { -+ case DDR_SSR_MODE: -+ ddr_sr_mode_ssr(); -+ break; -+ -+ case DDR_HSR_MODE: -+ ddr_sr_mode_hsr(); -+ break; -+ -+ case DDR_ASR_MODE: -+ ddr_sr_mode_asr(); -+ break; -+ -+ default: -+ EMSG("Unknown Self Refresh mode\n"); -+ panic(); -+ } -+} -+ -+void ddr_save_sr_mode(enum stm32mp1_ddr_sr_mode mode) -+{ -+ /* Save current mode before setting new one */ -+ saved_ddr_sr_mode = ddr_read_sr_mode(); -+ ddr_set_sr_mode(mode); -+} -+ -+void ddr_restore_sr_mode(void) -+{ -+ ddr_set_sr_mode(saved_ddr_sr_mode); -+} -diff --git a/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_ddrc.h b/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_ddrc.h -new file mode 100644 -index 000000000..e2d809bcd ---- /dev/null -+++ b/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_ddrc.h -@@ -0,0 +1,219 @@ -+/* SPDX-License-Identifier: BSD-3-Clause */ -+/* -+ * Copyright (c) 2017-2020, STMicroelectronics - All Rights Reserved -+ */ -+ -+#ifndef __STM32MP1_DDRC_H__ -+#define __STM32MP1_DDRC_H__ -+ -+#include -+ -+/* DDR Controller */ -+/* DDR Controller registers offsets */ -+#define DDRCTRL_MSTR 0x000 -+#define DDRCTRL_STAT 0x004 -+#define DDRCTRL_MRCTRL0 0x010 -+#define DDRCTRL_MRSTAT 0x018 -+#define DDRCTRL_PWRCTL 0x030 -+#define DDRCTRL_PWRTMG 0x034 -+#define DDRCTRL_HWLPCTL 0x038 -+#define DDRCTRL_RFSHCTL3 0x060 -+#define DDRCTRL_RFSHTMG 0x064 -+#define DDRCTRL_INIT0 0x0D0 -+#define DDRCTRL_DFIMISC 0x1B0 -+#define DDRCTRL_DBG1 0x304 -+#define DDRCTRL_DBGCAM 0x308 -+#define DDRCTRL_DBGCMD 0x30C -+#define DDRCTRL_DBGSTAT 0x310 -+#define DDRCTRL_SWCTL 0x320 -+#define DDRCTRL_SWSTAT 0x324 -+#define DDRCTRL_PSTAT 0x3FC -+#define DDRCTRL_PCTRL_0 0x490 -+#define DDRCTRL_PCTRL_1 0x540 -+ -+/* DDR Controller Register fields */ -+#define DDRCTRL_MSTR_DDR3 BIT(0) -+#define DDRCTRL_MSTR_LPDDR2 BIT(2) -+#define DDRCTRL_MSTR_LPDDR3 BIT(3) -+#define DDRCTRL_MSTR_DATA_BUS_WIDTH_MASK GENMASK_32(13, 12) -+#define DDRCTRL_MSTR_DATA_BUS_WIDTH_FULL 0 -+#define DDRCTRL_MSTR_DATA_BUS_WIDTH_HALF BIT(12) -+#define DDRCTRL_MSTR_DATA_BUS_WIDTH_QUARTER BIT(13) -+#define DDRCTRL_MSTR_DLL_OFF_MODE BIT(15) -+ -+#define DDRCTRL_STAT_OPERATING_MODE_MASK GENMASK_32(2, 0) -+#define DDRCTRL_STAT_OPERATING_MODE_NORMAL BIT(0) -+#define DDRCTRL_STAT_OPERATING_MODE_SR (BIT(0) | BIT(1)) -+#define DDRCTRL_STAT_SELFREF_TYPE_MASK GENMASK_32(5, 4) -+#define DDRCTRL_STAT_SELFREF_TYPE_ASR (BIT(4) | BIT(5)) -+#define DDRCTRL_STAT_SELFREF_TYPE_SR BIT(5) -+ -+#define DDRCTRL_MRCTRL0_MR_TYPE_WRITE 0 -+/* only one rank supported */ -+#define DDRCTRL_MRCTRL0_MR_RANK_SHIFT 4 -+#define DDRCTRL_MRCTRL0_MR_RANK_ALL \ -+ BIT(DDRCTRL_MRCTRL0_MR_RANK_SHIFT) -+#define DDRCTRL_MRCTRL0_MR_ADDR_SHIFT 12 -+#define DDRCTRL_MRCTRL0_MR_ADDR_MASK GENMASK_32(15, 12) -+#define DDRCTRL_MRCTRL0_MR_WR BIT(31) -+ -+#define DDRCTRL_MRSTAT_MR_WR_BUSY BIT(0) -+ -+#define DDRCTRL_PWRCTL_SELFREF_EN BIT(0) -+#define DDRCTRL_PWRCTL_POWERDOWN_EN BIT(1) -+#define DDRCTRL_PWRCTL_EN_DFI_DRAM_CLK_DISABLE BIT(3) -+#define DDRCTRL_PWRCTL_SELFREF_SW BIT(5) -+ -+#define DDRCTRL_PWRTMG_SELFREF_TO_X32_MASK GENMASK_32(19, 12) -+#define DDRCTRL_PWRTMG_SELFREF_TO_X32_0 BIT(16) -+ -+#define DDRCTRL_RFSHCTL3_DIS_AUTO_REFRESH BIT(0) -+ -+#define DDRCTRL_HWLPCTL_HW_LP_EN BIT(0) -+ -+#define DDRCTRL_RFSHTMG_T_RFC_NOM_X1_X32_MASK GENMASK_32(27, 16) -+#define DDRCTRL_RFSHTMG_T_RFC_NOM_X1_X32_SHIFT 16 -+ -+#define DDRCTRL_INIT0_SKIP_DRAM_INIT_MASK GENMASK_32(31, 30) -+#define DDRCTRL_INIT0_SKIP_DRAM_INIT_NORMAL BIT(30) -+ -+#define DDRCTRL_DFIMISC_DFI_INIT_COMPLETE_EN BIT(0) -+ -+#define DDRCTRL_DBG1_DIS_HIF BIT(1) -+ -+#define DDRCTRL_DBGCAM_WR_DATA_PIPELINE_EMPTY BIT(29) -+#define DDRCTRL_DBGCAM_RD_DATA_PIPELINE_EMPTY BIT(28) -+#define DDRCTRL_DBGCAM_DBG_WR_Q_EMPTY BIT(26) -+#define DDRCTRL_DBGCAM_DBG_LPR_Q_DEPTH GENMASK_32(12, 8) -+#define DDRCTRL_DBGCAM_DBG_HPR_Q_DEPTH GENMASK_32(4, 0) -+ -+#define DDRCTRL_DBGCAM_DATA_PIPELINE_EMPTY \ -+ (DDRCTRL_DBGCAM_WR_DATA_PIPELINE_EMPTY | \ -+ DDRCTRL_DBGCAM_RD_DATA_PIPELINE_EMPTY) -+ -+#define DDRCTRL_DBGCAM_DBG_Q_DEPTH \ -+ (DDRCTRL_DBGCAM_DBG_WR_Q_EMPTY | \ -+ DDRCTRL_DBGCAM_DBG_LPR_Q_DEPTH | \ -+ DDRCTRL_DBGCAM_DBG_HPR_Q_DEPTH) -+ -+#define DDRCTRL_DBGCMD_RANK0_REFRESH BIT(0) -+ -+#define DDRCTRL_DBGSTAT_RANK0_REFRESH_BUSY BIT(0) -+ -+#define DDRCTRL_SWCTL_SW_DONE BIT(0) -+ -+#define DDRCTRL_SWSTAT_SW_DONE_ACK BIT(0) -+ -+#define DDRCTRL_PCTRL_N_PORT_EN BIT(0) -+ -+/* DDR PHY registers offsets */ -+#define DDRPHYC_PIR 0x004 -+#define DDRPHYC_PGCR 0x008 -+#define DDRPHYC_PGSR 0x00C -+#define DDRPHYC_DLLGCR 0x010 -+#define DDRPHYC_ACDLLCR 0x014 -+#define DDRPHYC_PTR0 0x018 -+#define DDRPHYC_ACIOCR 0x024 -+#define DDRPHYC_DXCCR 0x028 -+#define DDRPHYC_DSGCR 0x02C -+#define DDRPHYC_ZQ0CR0 0x180 -+#define DDRPHYC_DX0GCR 0x1C0 -+#define DDRPHYC_DX0DLLCR 0x1CC -+#define DDRPHYC_DX1GCR 0x200 -+#define DDRPHYC_DX1DLLCR 0x20C -+#define DDRPHYC_DX2GCR 0x240 -+#define DDRPHYC_DX2DLLCR 0x24C -+#define DDRPHYC_DX3GCR 0x280 -+#define DDRPHYC_DX3DLLCR 0x28C -+ -+/* DDR PHY Register fields */ -+#define DDRPHYC_PIR_INIT BIT(0) -+#define DDRPHYC_PIR_DLLSRST BIT(1) -+#define DDRPHYC_PIR_DLLLOCK BIT(2) -+#define DDRPHYC_PIR_ZCAL BIT(3) -+#define DDRPHYC_PIR_ITMSRST BIT(4) -+#define DDRPHYC_PIR_DRAMRST BIT(5) -+#define DDRPHYC_PIR_DRAMINIT BIT(6) -+#define DDRPHYC_PIR_QSTRN BIT(7) -+#define DDRPHYC_PIR_ICPC BIT(16) -+#define DDRPHYC_PIR_ZCALBYP BIT(30) -+#define DDRPHYC_PIR_INITSTEPS_MASK GENMASK(31, 7) -+ -+#define DDRPHYC_PGCR_DFTCMP BIT(2) -+#define DDRPHYC_PGCR_PDDISDX BIT(24) -+#define DDRPHYC_PGCR_RFSHDT_MASK GENMASK_32(28, 25) -+ -+#define DDRPHYC_PGSR_IDONE BIT(0) -+#define DDRPHYC_PGSR_DTERR BIT(5) -+#define DDRPHYC_PGSR_DTIERR BIT(6) -+#define DDRPHYC_PGSR_DFTERR BIT(7) -+#define DDRPHYC_PGSR_RVERR BIT(8) -+#define DDRPHYC_PGSR_RVEIRR BIT(9) -+ -+#define DDRPHYC_DLLGCR_BPS200 BIT(23) -+ -+#define DDRPHYC_ACDLLCR_DLLSRST BIT(30) -+#define DDRPHYC_ACDLLCR_DLLDIS BIT(31) -+ -+#define DDRPHYC_PTR0_TDLLSRST_OFFSET 0 -+#define DDRPHYC_PTR0_TDLLSRST_MASK GENMASK_32(5, 0) -+#define DDRPHYC_PTR0_TDLLLOCK_OFFSET 6 -+#define DDRPHYC_PTR0_TDLLLOCK_MASK GENMASK_32(17, 6) -+#define DDRPHYC_PTR0_TITMSRST_OFFSET 18 -+#define DDRPHYC_PTR0_TITMSRST_MASK GENMASK_32(21, 18) -+ -+#define DDRPHYC_ACIOCR_ACOE BIT(1) -+#define DDRPHYC_ACIOCR_ACPDD BIT(3) -+#define DDRPHYC_ACIOCR_ACPDR BIT(4) -+#define DDRPHYC_ACIOCR_CKPDD_MASK GENMASK_32(10, 8) -+#define DDRPHYC_ACIOCR_CKPDD_0 BIT(8) -+#define DDRPHYC_ACIOCR_CKPDR_MASK GENMASK_32(13, 11) -+#define DDRPHYC_ACIOCR_CKPDR_0 BIT(11) -+#define DDRPHYC_ACIOCR_CSPDD_MASK GENMASK_32(21, 18) -+#define DDRPHYC_ACIOCR_CSPDD_0 BIT(18) -+#define DDRPHYC_ACIOCR_RSTPDD BIT(27) -+#define DDRPHYC_ACIOCR_RSTPDR BIT(28) -+ -+#define DDRPHYC_DXCCR_DXPDD BIT(2) -+#define DDRPHYC_DXCCR_DXPDR BIT(3) -+ -+#define DDRPHYC_DSGCR_CKEPDD_MASK GENMASK_32(19, 16) -+#define DDRPHYC_DSGCR_CKEPDD_0 BIT(16) -+#define DDRPHYC_DSGCR_ODTPDD_MASK GENMASK_32(23, 20) -+#define DDRPHYC_DSGCR_ODTPDD_0 BIT(20) -+#define DDRPHYC_DSGCR_NL2PD BIT(24) -+ -+#define DDRPHYC_ZQ0CRN_ZDATA_MASK GENMASK_32(27, 0) -+#define DDRPHYC_ZQ0CRN_ZDATA_SHIFT 0 -+#define DDRPHYC_ZQ0CRN_ZDEN BIT(28) -+#define DDRPHYC_ZQ0CRN_ZQPD BIT(31) -+ -+#define DDRPHYC_DXNGCR_DXEN BIT(0) -+ -+#define DDRPHYC_DXNDLLCR_DLLSRST BIT(30) -+#define DDRPHYC_DXNDLLCR_DLLDIS BIT(31) -+#define DDRPHYC_DXNDLLCR_SDPHASE_MASK GENMASK(17, 14) -+#define DDRPHYC_DXNDLLCR_SDPHASE_SHIFT 14 -+ -+/* DDR Self Refresh (SR) modes */ -+enum stm32mp1_ddr_sr_mode { -+ DDR_SR_MODE_INVALID = 0, -+ DDR_SSR_MODE, -+ DDR_HSR_MODE, -+ DDR_ASR_MODE, -+}; -+ -+void ddr_save_sr_mode(enum stm32mp1_ddr_sr_mode mode); -+void ddr_restore_sr_mode(void); -+ -+/* Return 32bit calibration value used for DDRPHY */ -+uint32_t get_ddrphy_calibration(void); -+ -+/* -+ * Entry/exit DDR selfrefresh mode -+ * Return 0 on success and a non-null value on error -+ */ -+int ddr_standby_sr_entry(void); -+int ddr_standby_sr_exit(void); -+ -+#endif /*__STM32MP1_DDRC_H__*/ -diff --git a/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_pmic.c b/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_pmic.c -index 8d18ff94b..a7d755a0c 100644 ---- a/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_pmic.c -+++ b/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_pmic.c -@@ -1,6 +1,6 @@ - // SPDX-License-Identifier: BSD-3-Clause - /* -- * Copyright (c) 2017-2020, STMicroelectronics -+ * Copyright (c) 2017-2021, STMicroelectronics - */ - - #include -@@ -36,6 +36,8 @@ static uint32_t pmic_i2c_addr; - - /* CPU voltage supplier if found */ - static char cpu_supply_name[PMIC_REGU_SUPPLY_NAME_LEN]; -+/* USB voltage supplier if found */ -+static char *usb_supply_name; - - bool stm32mp_with_pmic(void) - { -@@ -372,6 +374,49 @@ const char *stm32mp_pmic_get_cpu_supply_name(void) - return cpu_supply_name; - } - -+/* Return a libfdt compliant status value */ -+static int save_usb_supply_name(void) -+{ -+ void *fdt = NULL; -+ int node = 0; -+ int subnode = 0; -+ const fdt32_t *cuint = NULL; -+ const char *name = NULL; -+ -+ fdt = get_embedded_dt(); -+ if (!fdt) -+ panic(); -+ -+ node = fdt_node_offset_by_compatible(fdt, -1, "st,stm32mp1-usbphyc"); -+ if (node < 0) -+ return -FDT_ERR_NOTFOUND; -+ -+ fdt_for_each_subnode(subnode, fdt, node) { -+ cuint = fdt_getprop(fdt, subnode, "phy-supply", NULL); -+ if (cuint) -+ break; -+ } -+ if (!cuint) -+ return -FDT_ERR_NOTFOUND; -+ -+ node = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(*cuint)); -+ if (node < 0) -+ return -FDT_ERR_NOTFOUND; -+ -+ name = fdt_get_name(fdt, node, NULL); -+ assert(name); -+ usb_supply_name = strdup(name); -+ if (!usb_supply_name) -+ panic(); -+ -+ return 0; -+} -+ -+const char *stm32mp_pmic_get_usb_supply_name(void) -+{ -+ return usb_supply_name; -+} -+ - /* Preallocate not that much regu references */ - static char *nsec_access_regu_name[PMIC_REGU_COUNT]; - -@@ -452,6 +497,9 @@ static void parse_regulator_fdt_nodes(void) - - if (save_cpu_supply_name()) - DMSG("No CPU supply provided"); -+ -+ if (save_usb_supply_name()) -+ DMSG("No USB supply provided"); - } - - /* -diff --git a/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_pmic.h b/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_pmic.h -index 4625af125..f91654733 100644 ---- a/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_pmic.h -+++ b/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_pmic.h -@@ -1,6 +1,6 @@ - /* SPDX-License-Identifier: BSD-3-Clause */ - /* -- * Copyright (c) 2017-2020, STMicroelectronics -+ * Copyright (c) 2017-2021, STMicroelectronics - */ - - #ifndef __STM32MP1_PMIC_H__ -@@ -15,6 +15,7 @@ void stm32mp_get_pmic(void); - void stm32mp_put_pmic(void); - int stm32mp_dt_pmic_status(void); - const char *stm32mp_pmic_get_cpu_supply_name(void); -+const char *stm32mp_pmic_get_usb_supply_name(void); - #else - static inline void stm32mp_pmic_apply_boot_on_config(void) - { -@@ -43,6 +44,11 @@ static inline const char *stm32mp_pmic_get_cpu_supply_name(void) - { - return NULL; - } -+ -+static inline const char *stm32mp_pmic_get_usb_supply_name(void) -+{ -+ return NULL; -+} - #endif - - #endif /*__STM32MP1_PMIC_H__*/ -diff --git a/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_pwr.h b/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_pwr.h -index 9cedd9321..5783a5ca9 100644 ---- a/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_pwr.h -+++ b/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_pwr.h -@@ -13,10 +13,41 @@ - #define PWR_CR2_OFF 0x08 - #define PWR_CR3_OFF 0x0c - #define PWR_MPUCR_OFF 0x10 -+#define PWR_MCUCR_OFF 0x14 - #define PWR_WKUPCR_OFF 0x20 - #define PWR_MPUWKUPENR_OFF 0x28 - --#define PWR_OFFSET_MASK 0x3fUL -+#define PWR_OFFSET_MASK GENMASK_32(5, 0) -+ -+#define PWR_CR1_LPDS BIT(0) -+#define PWR_CR1_LPCFG BIT(1) -+#define PWR_CR1_LVDS BIT(2) -+#define PWR_CR1_DBP BIT(8) -+ -+#define PWR_CR2_BREN BIT(0) -+#define PWR_CR2_RREN BIT(1) -+#define PWR_CR2_BRRDY BIT(16) -+#define PWR_CR2_RRRDY BIT(17) -+ -+#define PWR_CR3_VBE BIT(8) -+#define PWR_CR3_VBRS BIT(9) -+#define PWR_CR3_DDRSREN BIT(10) -+#define PWR_CR3_DDRSRDIS BIT(11) -+#define PWR_CR3_DDRRETEN BIT(12) -+#define PWR_CR3_USB33DEN BIT(24) -+#define PWR_CR3_REG18EN BIT(28) -+#define PWR_CR3_REG11EN BIT(30) -+ -+#define PWR_MPUCR_PDDS BIT(0) -+#define PWR_MPUCR_CSTDBYDIS BIT(3) -+#define PWR_MPUCR_CSSF BIT(9) -+ -+#define PWR_MCUCR_PDDS BIT(0) -+ -+#define PWR_WKUPCR_MASK (GENMASK_32(27, 16) | \ -+ GENMASK_32(13, 8) | GENMASK_32(5, 0)) -+ -+#define PWR_MPUWKUPENR_MASK GENMASK_32(5, 0) - - enum pwr_regulator { - PWR_REG11 = 0, -diff --git a/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_syscfg.c b/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_syscfg.c -index 37d2cb0dc..345a19807 100644 ---- a/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_syscfg.c -+++ b/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_syscfg.c -@@ -5,6 +5,7 @@ - - #include - #include -+#include - #include - #include - #include -@@ -17,6 +18,7 @@ - */ - #define SYSCFG_CMPCR 0x20U - #define SYSCFG_CMPENSETR 0x24U -+#define SYSCFG_CMPENCLRR 0x28U - - /* - * SYSCFG_CMPCR Register -@@ -47,8 +49,8 @@ void stm32mp_syscfg_enable_io_compensation(void) - vaddr_t syscfg_base = get_syscfg_base(); - uint64_t timeout_ref = 0; - -- stm32_clock_enable(CK_CSI); -- stm32_clock_enable(SYSCFG); -+ clk_enable(CK_CSI); -+ clk_enable(SYSCFG); - - io_setbits32(syscfg_base + SYSCFG_CMPENSETR, SYSCFG_CMPENSETR_MPU_EN); - -@@ -71,6 +73,9 @@ void stm32mp_syscfg_disable_io_compensation(void) - vaddr_t syscfg_base = get_syscfg_base(); - uint32_t value = 0; - -+ /* No refcount balance needed on non-secure SYSCFG clock */ -+ clk_enable(SYSCFG); -+ - value = io_read32(syscfg_base + SYSCFG_CMPCR) >> - SYSCFG_CMPCR_ANSRC_SHIFT; - -@@ -84,10 +89,9 @@ void stm32mp_syscfg_disable_io_compensation(void) - - DMSG("SYSCFG.cmpcr = %#"PRIx32, io_read32(syscfg_base + SYSCFG_CMPCR)); - -- io_clrbits32(syscfg_base + SYSCFG_CMPENSETR, SYSCFG_CMPENSETR_MPU_EN); -+ io_setbits32(syscfg_base + SYSCFG_CMPENCLRR, SYSCFG_CMPENSETR_MPU_EN); - -- stm32_clock_disable(SYSCFG); -- stm32_clock_disable(CK_CSI); -+ clk_disable(CK_CSI); - } - - static TEE_Result stm32mp1_iocomp(void) -diff --git a/core/arch/arm/plat-stm32mp1/drivers/sub.mk b/core/arch/arm/plat-stm32mp1/drivers/sub.mk -index 42b6893f0..0b814a410 100644 ---- a/core/arch/arm/plat-stm32mp1/drivers/sub.mk -+++ b/core/arch/arm/plat-stm32mp1/drivers/sub.mk -@@ -1,4 +1,5 @@ --srcs-y += stm32mp1_clk.c -+srcs-$(CFG_STM32_CLKCALIB) += stm32mp1_calib.c -+srcs-y += stm32mp1_ddrc.c - srcs-$(CFG_STPMIC1) += stm32mp1_pmic.c - srcs-y += stm32mp1_pwr.c - srcs-y += stm32mp1_rcc.c -diff --git a/core/arch/arm/plat-stm32mp1/link.mk b/core/arch/arm/plat-stm32mp1/link.mk -index 01a9b8ed5..fa1465a45 100644 ---- a/core/arch/arm/plat-stm32mp1/link.mk -+++ b/core/arch/arm/plat-stm32mp1/link.mk -@@ -1,5 +1,6 @@ - include core/arch/arm/kernel/link.mk - -+ifeq ($(CFG_STM32MP15x_STM32IMAGE),y) - # Create stm32 formatted images from the native binary images - - define stm32image_cmd -@@ -22,3 +23,4 @@ all: $(link-out-dir)/tee-pageable_v2.stm32 - cleanfiles += $(link-out-dir)/tee-pageable_v2.stm32 - $(link-out-dir)/tee-pageable_v2.stm32: $(link-out-dir)/tee-pageable_v2.bin - $(stm32image_cmd) --source $< --dest $@ --bintype 0x22 -+endif -diff --git a/core/arch/arm/plat-stm32mp1/main.c b/core/arch/arm/plat-stm32mp1/main.c -index 9dca331c7..bf2e394fb 100644 ---- a/core/arch/arm/plat-stm32mp1/main.c -+++ b/core/arch/arm/plat-stm32mp1/main.c -@@ -1,23 +1,30 @@ - // SPDX-License-Identifier: BSD-2-Clause - /* -- * Copyright (c) 2017-2018, STMicroelectronics - * Copyright (c) 2016-2018, Linaro Limited -+ * Copyright (c) 2017-2021, STMicroelectronics - */ - - #include - #include - #include -+#include - #include - #include --#include -+#include - #include -+#include -+#include -+#include - #include -+#include -+#include - #include - #include - #include - #include - #include - #include -+#include - #include - #include - #include -@@ -29,7 +36,10 @@ register_phys_mem_pgdir(MEM_AREA_IO_NSEC, GPIOS_NSEC_BASE, GPIOS_NSEC_SIZE); - #endif - register_phys_mem_pgdir(MEM_AREA_IO_NSEC, I2C4_BASE, SMALL_PAGE_SIZE); - register_phys_mem_pgdir(MEM_AREA_IO_NSEC, I2C6_BASE, SMALL_PAGE_SIZE); -+register_phys_mem_pgdir(MEM_AREA_IO_NSEC, IWDG1_BASE, SMALL_PAGE_SIZE); -+register_phys_mem_pgdir(MEM_AREA_IO_NSEC, IWDG2_BASE, SMALL_PAGE_SIZE); - register_phys_mem_pgdir(MEM_AREA_IO_NSEC, RNG1_BASE, SMALL_PAGE_SIZE); -+register_phys_mem_pgdir(MEM_AREA_IO_NSEC, RTC_BASE, SMALL_PAGE_SIZE); - #ifdef CFG_WITH_NSEC_UARTS - register_phys_mem_pgdir(MEM_AREA_IO_NSEC, USART1_BASE, SMALL_PAGE_SIZE); - register_phys_mem_pgdir(MEM_AREA_IO_NSEC, USART2_BASE, SMALL_PAGE_SIZE); -@@ -41,17 +51,38 @@ register_phys_mem_pgdir(MEM_AREA_IO_NSEC, UART7_BASE, SMALL_PAGE_SIZE); - register_phys_mem_pgdir(MEM_AREA_IO_NSEC, UART8_BASE, SMALL_PAGE_SIZE); - #endif - -+register_phys_mem_pgdir(MEM_AREA_IO_SEC, BKPSRAM_BASE, SMALL_PAGE_SIZE); - register_phys_mem_pgdir(MEM_AREA_IO_SEC, BSEC_BASE, SMALL_PAGE_SIZE); -+register_phys_mem_pgdir(MEM_AREA_IO_SEC, DBGMCU_BASE, SMALL_PAGE_SIZE); -+register_phys_mem_pgdir(MEM_AREA_IO_SEC, DDRCTRL_BASE, SMALL_PAGE_SIZE); -+register_phys_mem_pgdir(MEM_AREA_IO_SEC, DDRPHYC_BASE, SMALL_PAGE_SIZE); - register_phys_mem_pgdir(MEM_AREA_IO_SEC, ETZPC_BASE, SMALL_PAGE_SIZE); - register_phys_mem_pgdir(MEM_AREA_IO_SEC, GIC_BASE, GIC_SIZE); - register_phys_mem_pgdir(MEM_AREA_IO_SEC, GPIOZ_BASE, SMALL_PAGE_SIZE); - register_phys_mem_pgdir(MEM_AREA_IO_SEC, I2C4_BASE, SMALL_PAGE_SIZE); - register_phys_mem_pgdir(MEM_AREA_IO_SEC, I2C6_BASE, SMALL_PAGE_SIZE); -+register_phys_mem_pgdir(MEM_AREA_IO_SEC, IWDG1_BASE, SMALL_PAGE_SIZE); - register_phys_mem_pgdir(MEM_AREA_IO_SEC, PWR_BASE, SMALL_PAGE_SIZE); - register_phys_mem_pgdir(MEM_AREA_IO_SEC, RCC_BASE, SMALL_PAGE_SIZE); - register_phys_mem_pgdir(MEM_AREA_IO_SEC, RNG1_BASE, SMALL_PAGE_SIZE); -+register_phys_mem_pgdir(MEM_AREA_IO_SEC, RTC_BASE, SMALL_PAGE_SIZE); -+register_phys_mem_pgdir(MEM_AREA_IO_SEC, STGEN_BASE, SMALL_PAGE_SIZE); - register_phys_mem_pgdir(MEM_AREA_IO_SEC, SYSCFG_BASE, SMALL_PAGE_SIZE); - register_phys_mem_pgdir(MEM_AREA_IO_SEC, TAMP_BASE, SMALL_PAGE_SIZE); -+register_phys_mem_pgdir(MEM_AREA_IO_SEC, TIM1_BASE, SMALL_PAGE_SIZE); -+register_phys_mem_pgdir(MEM_AREA_IO_SEC, TIM2_BASE, SMALL_PAGE_SIZE); -+register_phys_mem_pgdir(MEM_AREA_IO_SEC, TIM3_BASE, SMALL_PAGE_SIZE); -+register_phys_mem_pgdir(MEM_AREA_IO_SEC, TIM4_BASE, SMALL_PAGE_SIZE); -+register_phys_mem_pgdir(MEM_AREA_IO_SEC, TIM5_BASE, SMALL_PAGE_SIZE); -+register_phys_mem_pgdir(MEM_AREA_IO_SEC, TIM6_BASE, SMALL_PAGE_SIZE); -+register_phys_mem_pgdir(MEM_AREA_IO_SEC, TIM7_BASE, SMALL_PAGE_SIZE); -+register_phys_mem_pgdir(MEM_AREA_IO_SEC, TIM8_BASE, SMALL_PAGE_SIZE); -+register_phys_mem_pgdir(MEM_AREA_IO_SEC, TIM12_BASE, SMALL_PAGE_SIZE); -+register_phys_mem_pgdir(MEM_AREA_IO_SEC, TIM13_BASE, SMALL_PAGE_SIZE); -+register_phys_mem_pgdir(MEM_AREA_IO_SEC, TIM14_BASE, SMALL_PAGE_SIZE); -+register_phys_mem_pgdir(MEM_AREA_IO_SEC, TIM15_BASE, SMALL_PAGE_SIZE); -+register_phys_mem_pgdir(MEM_AREA_IO_SEC, TIM16_BASE, SMALL_PAGE_SIZE); -+register_phys_mem_pgdir(MEM_AREA_IO_SEC, TIM17_BASE, SMALL_PAGE_SIZE); - register_phys_mem_pgdir(MEM_AREA_IO_SEC, TZC_BASE, SMALL_PAGE_SIZE); - register_phys_mem_pgdir(MEM_AREA_IO_SEC, USART1_BASE, SMALL_PAGE_SIZE); - -@@ -66,6 +97,20 @@ register_dynamic_shm(DDR_BASE, CFG_TZDRAM_START - DDR_BASE); - register_dynamic_shm(TZDRAM_END, DRAM_END - TZDRAM_END); - #endif - -+/* Map non-secure DDR bottom for the low power sequence */ -+register_phys_mem(MEM_AREA_RAM_NSEC, DDR_BASE, SMALL_PAGE_SIZE); -+ -+#ifdef CFG_RPROC_PTA -+/* Map MCU RETRAM as read write for Cortex-M4 firmware management */ -+register_phys_mem(MEM_AREA_IO_SEC, RETRAM_BASE, RETRAM_SIZE); -+ -+/* Map MCU SRAM as read write for Cortex-M4 firmware management */ -+register_phys_mem(MEM_AREA_IO_SEC, MCUSRAM_BASE, MCUSRAM_SIZE); -+#endif -+ -+/* Map TEE physical RAM as read-only for content storage when suspending */ -+register_phys_mem(MEM_AREA_ROM_SEC, TEE_RAM_START, TEE_RAM_PH_SIZE); -+ - #define _ID2STR(id) (#id) - #define ID2STR(id) _ID2STR(id) - -@@ -168,8 +213,106 @@ static TEE_Result init_console_from_dt(void) - - /* Probe console from DT once clock inits (service init level) are completed */ - service_init_late(init_console_from_dt); -+ -+static TEE_Result initialize_pll1_settings(void) -+{ -+ uint32_t cpu_voltage = 0U; -+ int ret = 0; -+ -+ if (stm32mp1_clk_pll1_settings_are_valid()) -+ return TEE_SUCCESS; -+ -+ if (stm32mp_dt_pmic_status() > 0) { -+ stm32mp_get_pmic(); -+ -+ ret = stpmic1_regulator_voltage_get( -+ stm32mp_pmic_get_cpu_supply_name()); -+ if (ret < 0) -+ return ret; -+ -+ cpu_voltage = (uint32_t)ret; -+ -+ stm32mp_put_pmic(); -+ } -+ -+ if (stm32mp1_clk_compute_all_pll1_settings(cpu_voltage)) -+ panic(); -+ -+ return TEE_SUCCESS; -+} -+ -+/* Compute PLL1 settings once PMIC init is completed */ -+driver_init_late(initialize_pll1_settings); -+ -+static TEE_Result disable_usb_phy_regulator(void) -+{ -+ if (stm32mp_dt_pmic_status() > 0) { -+ const char *name = stm32mp_pmic_get_usb_supply_name(); -+ -+ if (!name) -+ return TEE_SUCCESS; -+ -+ stm32mp_get_pmic(); -+ if (stpmic1_regulator_disable(name)) -+ panic(); -+ -+ stm32mp_put_pmic(); -+ } -+ -+ return TEE_SUCCESS; -+} -+ -+/* Disable USB regulator to avoid initialization issues */ -+boot_final(disable_usb_phy_regulator); - #endif - -+static uintptr_t stm32_dbgmcu_base(void) -+{ -+ static void *va; -+ -+ if (!cpu_mmu_enabled()) -+ return DBGMCU_BASE; -+ -+ if (!va) -+ va = phys_to_virt(DBGMCU_BASE, MEM_AREA_IO_SEC); -+ -+ return (uintptr_t)va; -+} -+ -+/* SoC versioning util, returns default ID if can't access DBGMCU */ -+TEE_Result stm32mp1_dbgmcu_get_chip_version(uint32_t *chip_version) -+{ -+ uint32_t id = STM32MP1_CHIP_DEFAULT_VERSION; -+ -+ if (!chip_version) -+ return TEE_ERROR_BAD_PARAMETERS; -+ -+ if (stm32_bsec_read_debug_conf() & BSEC_DBGSWGEN) -+ id = (io_read32(stm32_dbgmcu_base() + DBGMCU_IDC) & -+ DBGMCU_IDC_REV_ID_MASK) >> DBGMCU_IDC_REV_ID_SHIFT; -+ -+ *chip_version = id; -+ -+ return TEE_SUCCESS; -+} -+ -+/* SoC device ID util, returns default ID if can't access DBGMCU */ -+TEE_Result stm32mp1_dbgmcu_get_chip_dev_id(uint32_t *chip_dev_id) -+{ -+ uint32_t id = STM32MP1_CHIP_ID; -+ -+ if (!chip_dev_id) -+ return TEE_ERROR_BAD_PARAMETERS; -+ -+ if (stm32_bsec_read_debug_conf() & BSEC_DBGSWGEN) -+ id = io_read32(stm32_dbgmcu_base() + DBGMCU_IDC) & -+ DBGMCU_IDC_DEV_ID_MASK; -+ -+ *chip_dev_id = id; -+ -+ return TEE_SUCCESS; -+} -+ - /* - * GIC init, used also for primary/secondary boot core wake completion - */ -@@ -212,7 +355,11 @@ static TEE_Result init_stm32mp1_drivers(void) - (SYSRAM_SEC_SIZE >= CFG_TZSRAM_SIZE))); - - etzpc_configure_tzma(1, SYSRAM_SEC_SIZE >> SMALL_PAGE_SHIFT); -+#ifdef STM32MP1_USE_MPU0_RESET -+ /* BootROM needs unlocked for independent reset */ -+#else - etzpc_lock_tzma(1); -+#endif - - return TEE_SUCCESS; - } -@@ -241,12 +388,20 @@ void stm32mp_get_bsec_static_cfg(struct stm32_bsec_static_cfg *cfg) - - bool stm32mp_is_closed_device(void) - { -- uint32_t otp = 0; -+ uint32_t otp_id = 0; -+ size_t bit_len = 0; -+ uint32_t otp_value = 0; - TEE_Result result = TEE_ERROR_GENERIC; - -+ if (stm32_bsec_find_otp_in_nvmem_layout(CFG0_OTP, &otp_id, &bit_len)) -+ panic(); -+ -+ if (bit_len != 8) -+ panic(); -+ - /* Non closed_device platform expects fuse well programmed to 0 */ -- result = stm32_bsec_shadow_read_otp(&otp, DATA0_OTP); -- if (!result && !(otp & BIT(DATA0_OTP_SECURED_POS))) -+ result = stm32_bsec_shadow_read_otp(&otp_value, otp_id); -+ if (!result && !(otp_value & BIT(CFG0_OTP_SECURED_POS))) - return false; - - return true; -@@ -290,6 +445,20 @@ vaddr_t stm32mp_bkpreg(unsigned int idx) - return bkpreg_base() + (idx * sizeof(uint32_t)); - } - -+vaddr_t stm32mp_bkpsram_base(void) -+{ -+ struct io_pa_va base = { .pa = BKPSRAM_BASE }; -+ -+ return io_pa_or_va(&base); -+} -+ -+vaddr_t stm32mp_stgen_base(void) -+{ -+ struct io_pa_va base = { .pa = STGEN_BASE }; -+ -+ return io_pa_or_va(&base); -+} -+ - vaddr_t stm32_get_gpio_bank_base(unsigned int bank) - { - static struct io_pa_va gpios_nsec_base = { .pa = GPIOS_NSEC_BASE }; -@@ -322,3 +491,178 @@ unsigned int stm32_get_gpio_bank_clock(unsigned int bank) - assert(bank <= GPIO_BANK_K); - return GPIOA + bank; - } -+ -+static int get_part_number(uint32_t *part_nb) -+{ -+ uint32_t part_number = 0; -+ uint32_t dev_id = 0; -+ uint32_t otp = 0; -+ size_t bit_len = 0; -+ -+ assert(part_nb); -+ -+ if (stm32mp1_dbgmcu_get_chip_dev_id(&dev_id)) -+ return -1; -+ -+ if (stm32_bsec_find_otp_in_nvmem_layout(PART_NUMBER_OTP, -+ &otp, &bit_len)) -+ return -1; -+ -+ if (bit_len != 8) -+ panic(); -+ -+ if (stm32_bsec_read_otp(&part_number, otp)) -+ return -1; -+ -+ part_number = (part_number & PART_NUMBER_OTP_PART_MASK) >> -+ PART_NUMBER_OTP_PART_SHIFT; -+ -+ *part_nb = part_number | (dev_id << 16); -+ -+ return 0; -+} -+ -+bool stm32mp_supports_cpu_opp(uint32_t opp_id) -+{ -+ uint32_t part_number = 0; -+ uint32_t id = 0; -+ -+ if (get_part_number(&part_number)) { -+ DMSG("Cannot get part number\n"); -+ panic(); -+ } -+ -+ switch (opp_id) { -+ case PLAT_OPP_ID1: -+ case PLAT_OPP_ID2: -+ id = opp_id; -+ break; -+ default: -+ return false; -+ } -+ -+ switch (part_number) { -+ case STM32MP157F_PART_NB: -+ case STM32MP157D_PART_NB: -+ case STM32MP153F_PART_NB: -+ case STM32MP153D_PART_NB: -+ case STM32MP151F_PART_NB: -+ case STM32MP151D_PART_NB: -+ return true; -+ default: -+ return id == PLAT_OPP_ID1; -+ } -+} -+ -+enum etzpc_decprot_attributes stm32mp_etzpc_binding2decprot(uint32_t mode) -+{ -+ switch (mode) { -+ case DECPROT_S_RW: -+ return ETZPC_DECPROT_S_RW; -+ case DECPROT_NS_R_S_W: -+ return ETZPC_DECPROT_NS_R_S_W; -+ case DECPROT_MCU_ISOLATION: -+ return ETZPC_DECPROT_MCU_ISOLATION; -+ case DECPROT_NS_RW: -+ return ETZPC_DECPROT_NS_RW; -+ default: -+ panic(); -+ } -+} -+ -+unsigned int stm32mp_iwdg_irq2instance(size_t irq) -+{ -+ int id = irq - STM32MP1_IRQ_IWDG1; -+ -+ assert(id >= IWDG1_INST && id <= IWDG2_INST); -+ return (unsigned int)id; -+} -+ -+size_t stm32mp_iwdg_instance2irq(unsigned int instance) -+{ -+ return instance + STM32MP1_IRQ_IWDG1; -+} -+ -+unsigned int stm32mp_iwdg_iomem2instance(vaddr_t pbase) -+{ -+ switch (pbase) { -+ case IWDG1_BASE: -+ return IWDG1_INST; -+ case IWDG2_BASE: -+ return IWDG2_INST; -+ default: -+ panic(); -+ } -+} -+ -+unsigned long stm32_get_iwdg_otp_config(vaddr_t pbase) -+{ -+ unsigned int idx = 0; -+ unsigned long iwdg_cfg = 0; -+ uint32_t otp_id = 0; -+ size_t bit_len = 0; -+ uint32_t otp_value = 0; -+ -+ idx = stm32mp_iwdg_iomem2instance(pbase); -+ -+ if (stm32_bsec_find_otp_in_nvmem_layout(HW2_OTP, &otp_id, &bit_len)) -+ panic(); -+ -+ if (bit_len != 32) -+ panic(); -+ -+ if (stm32_bsec_read_otp(&otp_value, otp_id)) -+ panic(); -+ -+ if (otp_value & BIT(idx + HW2_OTP_IWDG_HW_ENABLE_SHIFT)) -+ iwdg_cfg |= IWDG_HW_ENABLED; -+ -+ if (otp_value & BIT(idx + HW2_OTP_IWDG_FZ_STOP_SHIFT)) -+ iwdg_cfg |= IWDG_DISABLE_ON_STOP; -+ -+ if (otp_value & BIT(idx + HW2_OTP_IWDG_FZ_STANDBY_SHIFT)) -+ iwdg_cfg |= IWDG_DISABLE_ON_STANDBY; -+ -+ return iwdg_cfg; -+} -+ -+#ifdef CFG_STM32_RTC -+/* -+ * Return true if RTC needs to be read twice not once for a reliable value. -+ * -+ * This function determines the number of needed RTC calendar read operations -+ * to get consistent values: RTC may need to be read twice depending on clock -+ * frequencies. -+ * If APB1 frequency is less than 7 times the RTC one, the software has to -+ * read the calendar time and date register twice. -+ */ -+bool stm32_rtc_get_read_twice(void) -+{ -+ unsigned long apb1_freq = 0; -+ unsigned long rtc_freq = 0; -+ uint32_t apb1_div = 0; -+ vaddr_t rcc_base = stm32_rcc_base(); -+ -+ switch ((io_read32(rcc_base + RCC_BDCR) & -+ RCC_BDCR_RTCSRC_MASK) >> RCC_BDCR_RTCSRC_SHIFT) { -+ case 1: -+ rtc_freq = clk_get_rate(CK_LSE); -+ break; -+ case 2: -+ rtc_freq = clk_get_rate(CK_LSI); -+ break; -+ case 3: -+ rtc_freq = clk_get_rate(CK_HSE); -+ rtc_freq /= (io_read32(rcc_base + RCC_RTCDIVR) & -+ RCC_DIVR_DIV_MASK) + 1U; -+ break; -+ default: -+ panic(); -+ } -+ -+ apb1_div = io_read32(rcc_base + RCC_APB1DIVR) & RCC_APBXDIV_MASK; -+ apb1_freq = clk_get_rate(CK_MCU) >> apb1_div; -+ -+ return apb1_freq < (rtc_freq * 7U); -+} -+#endif -diff --git a/core/arch/arm/plat-stm32mp1/nsec-service/low_power_svc.c b/core/arch/arm/plat-stm32mp1/nsec-service/low_power_svc.c -new file mode 100644 -index 000000000..486977baa ---- /dev/null -+++ b/core/arch/arm/plat-stm32mp1/nsec-service/low_power_svc.c -@@ -0,0 +1,152 @@ -+// SPDX-License-Identifier: BSD-3-Clause -+/* -+ * Copyright (c) 2017-2018, STMicroelectronics - All Rights Reserved -+ * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved. -+ */ -+ -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+ -+#include "stm32mp1_smc.h" -+#include "low_power_svc.h" -+#include "../pm/power.h" -+#include "../pm/context.h" -+ -+#undef DDR_SR_TEST -+ -+#ifdef DDR_SR_TEST -+uint32_t sr_mode_scv_handler(uint32_t __maybe_unused x1, uint32_t x2) -+{ -+ unsigned int mode = x2; -+ -+ DMSG("DDR selfrefresh mode 0x%" PRIx32 ", 0x%" PRIx32, mode, x1); -+ -+ switch (mode) { -+ case STM32_SIP_SVC_SR_MODE_SSR: -+ ddr_sr_mode_ssr(); -+ break; -+ case STM32_SIP_SVC_SR_MODE_ASR: -+ ddr_sr_mode_asr(); -+ break; -+ case STM32_SIP_SVC_SR_MODE_HSR: -+ ddr_sr_mode_hsr(); -+ break; -+ default: -+ return STM32_SIP_SVC_INVALID_PARAMS; -+ } -+ -+ return STM32_SIP_SVC_OK; -+} -+#else -+uint32_t sr_mode_scv_handler(uint32_t __unused x1, uint32_t __unused x2) -+{ -+ return STM32_SIP_SVC_FAILED; -+} -+#endif -+ -+uint32_t cstop_scv_handler(struct sm_ctx __unused *ctx, uint32_t __unused x1, -+ uint32_t __unused x2, uint32_t __unused x3) -+{ -+ DMSG("core %u", get_core_pos()); -+ -+ stm32mp1_set_lp_deepest_soc_mode(PSCI_MODE_SYSTEM_SUSPEND, -+ STM32_PM_CSTOP_ALLOW_LPLV_STOP); -+ -+ return (psci_system_suspend(ctx->nsec.mon_lr, 0, &ctx->nsec) == 0) ? -+ STM32_SIP_SVC_OK : STM32_SIP_SVC_FAILED; -+} -+ -+uint32_t standby_scv_handler(struct sm_ctx *ctx, uint32_t __unused x1, -+ uint32_t __unused x2, uint32_t x3) -+{ -+ uint32_t nsec_resume_ep = x3; -+ -+ DMSG("core %u", get_core_pos()); -+ -+ if (nsec_resume_ep == 0U) { -+ shutdown_scv_handler(); -+ panic(); -+ } -+ -+ stm32mp1_set_lp_deepest_soc_mode(PSCI_MODE_SYSTEM_SUSPEND, -+ STM32_PM_CSTOP_ALLOW_STANDBY_DDR_SR); -+ -+ return (psci_system_suspend(nsec_resume_ep, 0, &ctx->nsec) == 0) ? -+ STM32_SIP_SVC_OK : STM32_SIP_SVC_FAILED; -+} -+ -+uint32_t shutdown_scv_handler(void) -+{ -+ DMSG("core %u", get_core_pos()); -+ -+ if (!stm32mp_with_pmic()) { -+ return STM32_SIP_SVC_FAILED; -+ } -+ -+ psci_system_off(); -+ panic(); -+} -+ -+uint32_t pm_domain_scv_handler(uint32_t id, uint32_t enable) -+{ -+ unsigned int pd = id; -+ -+ DMSG("%sable PD %u", enable != 0 ? "En" : "Dis", pd); -+ -+ switch (pd) { -+ case STM32MP1_PD_VSW: -+ case STM32MP1_PD_CORE_RET: -+ case STM32MP1_PD_CORE: -+ break; -+ default: -+ return STM32_SIP_SVC_INVALID_PARAMS; -+ } -+ -+ stm32mp1_set_pm_domain_state(pd, enable); -+ -+ return STM32_SIP_SVC_OK; -+} -+ -+#ifdef CFG_TEE_CORE_DEBUG -+uint32_t pm_set_lp_state_scv_handler(uint32_t request, uint32_t state) -+{ -+ uint32_t power_mode; -+ -+ switch (request) { -+ case STM32_OEM_SVC_LP_FORCE_SUSPEND_PARAMS: -+ DMSG("Set suspend mode to %u", state); -+ power_mode = PSCI_MODE_SYSTEM_SUSPEND; -+ break; -+ case STM32_OEM_SVC_LP_FORCE_OFF_PARAMS: -+ DMSG("Set off mode to %u", state); -+ power_mode = PSCI_MODE_SYSTEM_OFF; -+ break; -+ default: -+ return STM32_OEM_SVC_INVALID_PARAMS; -+ } -+ -+ if (stm32mp1_set_lp_deepest_soc_mode(power_mode, state) < 0) { -+ return STM32_OEM_SVC_FAILED; -+ } -+ -+ return STM32_OEM_SVC_OK; -+} -+#else -+uint32_t pm_set_lp_state_scv_handler(uint32_t __unused mode, -+ uint32_t __unused state) -+{ -+ return STM32_SIP_SVC_FAILED; -+} -+#endif -diff --git a/core/arch/arm/plat-stm32mp1/nsec-service/low_power_svc.h b/core/arch/arm/plat-stm32mp1/nsec-service/low_power_svc.h -new file mode 100644 -index 000000000..3d7d247d9 ---- /dev/null -+++ b/core/arch/arm/plat-stm32mp1/nsec-service/low_power_svc.h -@@ -0,0 +1,55 @@ -+/* SPDX-License-Identifier: BSD-3-Clause */ -+/* -+ * Copyright (c) 2018, STMicroelectronics -+ */ -+ -+#ifndef LOW_POWER_SVC_H -+#define LOW_POWER_SVC_H -+ -+#include -+#include -+ -+#ifdef CFG_STM32_LOWPOWER_SIP -+uint32_t sr_mode_scv_handler(uint32_t x1, uint32_t x2); -+uint32_t cstop_scv_handler(struct sm_ctx *nsec, -+ uint32_t x1, uint32_t x2, uint32_t x3); -+uint32_t standby_scv_handler(struct sm_ctx *nsec, -+ uint32_t x1, uint32_t x2, uint32_t x3); -+uint32_t shutdown_scv_handler(void); -+uint32_t pm_domain_scv_handler(uint32_t x1, uint32_t x2); -+ -+uint32_t pm_set_lp_state_scv_handler(uint32_t x1, uint32_t x2); -+#else -+uint32_t sr_mode_scv_handler(uint32_t x1 __unused, uint32_t x2 __unused) -+{ -+ return STM32_SIP_SVC_FAILED; -+} -+ -+uint32_t cstop_scv_handler(struct sm_ctx *nsec __unused, uint32_t x1 __unused, -+ uint32_t x2 __unused, uint32_t x3 __unused) -+{ -+ return STM32_SIP_SVC_FAILED; -+} -+ -+uint32_t standby_scv_handler(struct sm_ctx *nsec __unused, uint32_t x1 __unused, -+ uint32_t x2 __unused, uint32_t x3 __unused) -+{ -+ return STM32_SIP_SVC_FAILED; -+} -+ -+uint32_t shutdown_scv_handler(void) -+{ -+ return STM32_SIP_SVC_FAILED; -+} -+ -+uint32_t pm_domain_scv_handler(uint32_t x1 __unused, uint32_t x2 __unused) -+{ -+ return STM32_SIP_SVC_FAILED; -+} -+ -+uint32_t pm_set_lp_state_scv_handler(uint32_t x1 __unused, uint32_t x2 __unused) -+{ -+ return STM32_SIP_SVC_FAILED; -+} -+#endif /* CFG_STM32_LOWPOWER_SIP */ -+#endif /* LOW_POWER_SVC_H */ -diff --git a/core/arch/arm/plat-stm32mp1/nsec-service/pwr_svc.c b/core/arch/arm/plat-stm32mp1/nsec-service/pwr_svc.c -new file mode 100644 -index 000000000..6c78d8203 ---- /dev/null -+++ b/core/arch/arm/plat-stm32mp1/nsec-service/pwr_svc.c -@@ -0,0 +1,89 @@ -+// SPDX-License-Identifier: BSD-3-Clause -+/* -+ * Copyright (c) 2017-2018, STMicroelectronics -+ */ -+ -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+ -+#include "pwr_svc.h" -+#include "stm32mp1_smc.h" -+ -+struct pwr_reg_prop { -+ uint32_t offset; -+ uint32_t mask; -+}; -+ -+#define PWR_ALLOWED_MASK(_off, _mask) { .offset = (_off), .mask = (_mask), } -+ -+static const struct pwr_reg_prop allowed_regs[] = { -+ PWR_ALLOWED_MASK(PWR_CR3_OFF, PWR_CR3_VBE | PWR_CR3_VBRS | -+ PWR_CR3_USB33DEN | -+ PWR_CR3_REG18EN | PWR_CR3_REG11EN), -+ PWR_ALLOWED_MASK(PWR_WKUPCR_OFF, PWR_WKUPCR_MASK), -+ PWR_ALLOWED_MASK(PWR_MPUWKUPENR_OFF, PWR_MPUWKUPENR_MASK), -+}; -+ -+uint32_t pwr_scv_handler(uint32_t x1, uint32_t x2, uint32_t x3) -+{ -+ uint32_t req = x1; -+ uint32_t offset = x2; -+ uint32_t value = x3; -+ vaddr_t va = 0; -+ uint32_t allowed = 0; -+ unsigned int i = 0; -+ -+ /* -+ * Argument x2 can be either the register physical address of the -+ * register offset toward PWR_BASE. -+ */ -+ if ((offset & ~PWR_OFFSET_MASK) != 0) { -+ if ((offset & ~PWR_OFFSET_MASK) != PWR_BASE) -+ return STM32_SIP_SVC_INVALID_PARAMS; -+ -+ offset &= PWR_OFFSET_MASK; -+ } -+ -+ DMSG("PWR service: %s 0x%" PRIx32 " at offset 0x%" PRIx32, -+ req == STM32_SIP_SVC_REG_WRITE ? "write" : -+ req == STM32_SIP_SVC_REG_SET ? "set" : "clear", -+ value, offset); -+ -+ for (i = 0; i < ARRAY_SIZE(allowed_regs); i++) { -+ if (offset != allowed_regs[i].offset) -+ continue; -+ -+ va = stm32_pwr_base() + offset; -+ allowed = allowed_regs[i].mask; -+ value &= allowed; -+ -+ switch (req) { -+ case STM32_SIP_SVC_REG_WRITE: -+ io_mask32_stm32shregs(va, value, allowed); -+ FMSG("wrt off %" PRIx32 "=%" PRIx32 " => %" PRIx32, -+ offset, value, io_read32(va)); -+ return STM32_SIP_SVC_OK; -+ case STM32_SIP_SVC_REG_SET: -+ io_setbits32_stm32shregs(va, value); -+ FMSG("set off %" PRIx32 "=%" PRIx32 " => %" PRIx32, -+ offset, value, io_read32(va)); -+ return STM32_SIP_SVC_OK; -+ case STM32_SIP_SVC_REG_CLEAR: -+ io_clrbits32_stm32shregs(va, value); -+ FMSG("clr off %" PRIx32 "=%" PRIx32 " => %" PRIx32, -+ offset, value, io_read32(va)); -+ return STM32_SIP_SVC_OK; -+ default: -+ return STM32_SIP_SVC_INVALID_PARAMS; -+ } -+ } -+ -+ return STM32_SIP_SVC_INVALID_PARAMS; -+} -diff --git a/core/arch/arm/plat-stm32mp1/nsec-service/pwr_svc.h b/core/arch/arm/plat-stm32mp1/nsec-service/pwr_svc.h -new file mode 100644 -index 000000000..429d62071 ---- /dev/null -+++ b/core/arch/arm/plat-stm32mp1/nsec-service/pwr_svc.h -@@ -0,0 +1,22 @@ -+/* SPDX-License-Identifier: BSD-3-Clause */ -+/* -+ * Copyright (c) 2017-2018, STMicroelectronics -+ */ -+ -+#ifndef __PWR_SVC_H__ -+#define __PWR_SVC_H__ -+ -+#include -+ -+#ifdef CFG_STM32_PWR_SIP -+uint32_t pwr_scv_handler(uint32_t x1, uint32_t x2, uint32_t x3); -+#else -+static inline uint32_t pwr_scv_handler(uint32_t x1 __unused, -+ uint32_t x2 __unused, -+ uint32_t x3 __unused) -+{ -+ return STM32_SIP_SVC_FAILED; -+} -+#endif -+ -+#endif /* __PWR_SVC_H__*/ -diff --git a/core/arch/arm/plat-stm32mp1/nsec-service/rcc_svc.c b/core/arch/arm/plat-stm32mp1/nsec-service/rcc_svc.c -new file mode 100644 -index 000000000..b4307b065 ---- /dev/null -+++ b/core/arch/arm/plat-stm32mp1/nsec-service/rcc_svc.c -@@ -0,0 +1,139 @@ -+// SPDX-License-Identifier: BSD-3-Clause -+/* -+ * Copyright (c) 2017-2019, STMicroelectronics -+ */ -+ -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+ -+#include "rcc_svc.h" -+#include "stm32mp1_smc.h" -+ -+static bool offset_is_clear_register(uint32_t __maybe_unused offset) -+{ -+ /* All allowed registers are non set/clear registers */ -+ return false; -+} -+ -+static void access_allowed_mask(uint32_t request, uint32_t offset, -+ uint32_t value, uint32_t allowed_mask) -+{ -+ vaddr_t va = stm32_rcc_base() + offset; -+ -+ if (!allowed_mask) -+ return; -+ -+ switch (request) { -+ case STM32_SIP_SVC_REG_WRITE: -+ if (offset_is_clear_register(offset)) { -+ /* CLR registers show SET state, not CLR state */ -+ io_write32(va, value & allowed_mask); -+ } else { -+ io_mask32_stm32shregs(va, value, allowed_mask); -+ } -+ FMSG("wrt 0x%" PRIx32 "=0x%" PRIx32 " => 0x%" PRIx32, -+ offset, value, io_read32(va)); -+ break; -+ -+ case STM32_SIP_SVC_REG_SET: -+ if (offset_is_clear_register(offset)) { -+ /* CLR registers show SET state, not CLR state */ -+ io_write32(va, value & allowed_mask); -+ } else { -+ io_setbits32_stm32shregs(va, value & allowed_mask); -+ } -+ FMSG("set 0x%" PRIx32 "=0x%" PRIx32 " => 0x%" PRIx32, -+ offset, value, io_read32(va)); -+ break; -+ -+ case STM32_SIP_SVC_REG_CLEAR: -+ /* Nothing to do on CLR registers */ -+ if (!offset_is_clear_register(offset)) -+ io_clrbits32_stm32shregs(va, value & allowed_mask); -+ FMSG("clear 0x%" PRIx32 "=0x%" PRIx32 " => 0x%" PRIx32, -+ offset, value, io_read32(va)); -+ break; -+ -+ default: -+ break; -+ } -+} -+ -+static uint32_t raw_allowed_access_request(uint32_t request, -+ uint32_t offset, uint32_t value) -+{ -+ uint32_t allowed_mask = 0; -+ -+ switch (offset) { -+ case RCC_MP_CIER: -+ case RCC_MP_CIFR: -+ allowed_mask = RCC_MP_CIFR_WKUPF; -+ break; -+ case RCC_MP_GCR: -+ allowed_mask = RCC_MP_GCR_BOOT_MCU; -+ break; -+ default: -+ return STM32_SIP_SVC_INVALID_PARAMS; -+ } -+ -+ access_allowed_mask(request, offset, value, allowed_mask); -+ -+ return STM32_SIP_SVC_OK; -+} -+ -+uint32_t rcc_scv_handler(uint32_t x1, uint32_t x2, uint32_t x3) -+{ -+ uint32_t request = x1; -+ uint32_t offset = x2; -+ uint32_t value = x3; -+ -+ /* -+ * Argument x2 can be either the register physical address of the -+ * register offset toward RCC_BASE. -+ */ -+ if (offset & ~RCC_OFFSET_MASK) { -+ if ((offset & ~RCC_OFFSET_MASK) != RCC_BASE) -+ return STM32_SIP_SVC_INVALID_PARAMS; -+ -+ offset &= RCC_OFFSET_MASK; -+ } -+ -+ DMSG_RAW("RCC service: %s 0x%" PRIx32 " at offset 0x%" PRIx32, -+ request == STM32_SIP_SVC_REG_WRITE ? "write" : -+ request == STM32_SIP_SVC_REG_SET ? "set" : "clear", -+ value, offset); -+ -+ return raw_allowed_access_request(request, offset, value); -+} -+ -+uint32_t rcc_opp_scv_handler(uint32_t x1, uint32_t x2, uint32_t *res) -+{ -+ uint32_t cmd = x1; -+ uint32_t opp = x2 / 1000U; /* KHz */ -+ -+ switch (cmd) { -+ case STM32_SIP_SVC_RCC_OPP_SET: -+ if (stm32mp1_set_opp_khz(opp)) -+ return STM32_SIP_SVC_FAILED; -+ break; -+ -+ case STM32_SIP_SVC_RCC_OPP_ROUND: -+ if(stm32mp1_round_opp_khz(&opp)) -+ return STM32_SIP_SVC_FAILED; -+ -+ if (MUL_OVERFLOW(opp, 1000, res)) -+ return STM32_SIP_SVC_FAILED; -+ break; -+ -+ default: -+ return STM32_SIP_SVC_INVALID_PARAMS; -+ } -+ -+ return STM32_SIP_SVC_OK; -+} -diff --git a/core/arch/arm/plat-stm32mp1/nsec-service/rcc_svc.h b/core/arch/arm/plat-stm32mp1/nsec-service/rcc_svc.h -new file mode 100644 -index 000000000..873458f49 ---- /dev/null -+++ b/core/arch/arm/plat-stm32mp1/nsec-service/rcc_svc.h -@@ -0,0 +1,30 @@ -+/* SPDX-License-Identifier: BSD-2-Clause */ -+/* -+ * Copyright (c) 2017-2019, STMicroelectronics -+ */ -+ -+#ifndef RCC_SVC_H -+#define RCC_SVC_H -+ -+#include -+ -+#ifdef CFG_STM32_RCC_SIP -+uint32_t rcc_scv_handler(uint32_t x1, uint32_t x2, uint32_t x3); -+uint32_t rcc_opp_scv_handler(uint32_t x1, uint32_t x2, uint32_t *res); -+#else -+static inline uint32_t rcc_scv_handler(uint32_t x1 __unused, -+ uint32_t x2 __unused, -+ uint32_t x3 __unused) -+{ -+ return STM32_SIP_SVC_FAILED; -+} -+static inline uint32_t rcc_opp_scv_handler(uint32_t x1 __unused, -+ uint32_t x2 __unused, -+ uint32_t *res __unused) -+ -+{ -+ return STM32_SIP_SVC_FAILED; -+} -+#endif -+ -+#endif /*RCC_SVC_H*/ -diff --git a/core/arch/arm/plat-stm32mp1/nsec-service/stm32mp1_smc.h b/core/arch/arm/plat-stm32mp1/nsec-service/stm32mp1_smc.h -index 3fbde31fe..a1f0e5859 100644 ---- a/core/arch/arm/plat-stm32mp1/nsec-service/stm32mp1_smc.h -+++ b/core/arch/arm/plat-stm32mp1/nsec-service/stm32mp1_smc.h -@@ -14,7 +14,7 @@ - #define STM32_SIP_SVC_VERSION_MAJOR 0x0 - #define STM32_SIP_SVC_VERSION_MINOR 0x1 - --#define STM32_SIP_SVC_FUNCTION_COUNT 0x3 -+#define STM32_SIP_SVC_FUNCTION_COUNT 12 - - /* STM32 SIP service generic return codes */ - #define STM32_SIP_SVC_OK 0x0 -@@ -71,6 +71,51 @@ - */ - #define STM32_SIP_SVC_FUNC_VERSION 0xff03 - -+/* -+ * SIP function STM32_SIP_SVC_FUNC_RCC -+ * -+ * Argument a0: (input) SMCC ID -+ * (output) status return code -+ * Argument a1: (input) Service ID (STM32_SIP_SVC_REG_xxx) -+ * Argument a2: (input) register offset or physical address -+ * (output) register read value, if applicable -+ * Argument a3: (input) register target value if applicable -+ */ -+#define STM32_SIP_SVC_FUNC_RCC 0x1000 -+ -+/* Service ID for STM32_SIP_FUNC_RCC */ -+#define STM32_SIP_SVC_REG_READ 0x0 -+#define STM32_SIP_SVC_REG_WRITE 0x1 -+#define STM32_SIP_SVC_REG_SET 0x2 -+#define STM32_SIP_SVC_REG_CLEAR 0x3 -+ -+/* -+ * SIP functions STM32_SIP_SVC_FUNC_CAL -+ * -+ * Argument a0: (input) SMCC ID -+ * (output) status return code -+ * Argument a1: (input) Clock ID (from DT clock bindings) -+ */ -+#define STM32_SIP_SVC_FUNC_CAL 0x1002 -+ -+/* -+ * SIP functions STM32_SIP_FUNC_PWR -+ * -+ * Argument a0: (input) SMCC ID -+ * (output) status return code -+ * Argument a1: (input) Service ID (STM32_SIP_REG_xxx) -+ * Argument a2: (input) register offset or physical address -+ * (output) register read value, if applicable -+ * Argument a3: (input) register target value if applicable -+ */ -+#define STM32_SIP_SVC_FUNC_PWR 0x1001 -+ -+/* Service ID for STM32_SIP_SVC_FUNC_RCC/_PWR */ -+#define STM32_SIP_SVC_REG_READ 0x0 -+#define STM32_SIP_SVC_REG_WRITE 0x1 -+#define STM32_SIP_SVC_REG_SET 0x2 -+#define STM32_SIP_SVC_REG_CLEAR 0x3 -+ - /* - * SIP functions STM32_SIP_SVC_FUNC_BSEC - * -@@ -92,6 +137,91 @@ - /* reserved for STM32_SIP_SVC_SMC_WRITE_ALL 0x6 */ - #define STM32_SIP_SVC_BSEC_WRLOCK_OTP 0x7 - -+/* -+ * SIP functions STM32_SIP_FUNC_SR_MODE -+ * -+ * Argument a0: (input) SMCC ID -+ * (output) status return code -+ * Argument a1: (unused) -+ * Argument a2: (input) Target selfrefresh mode -+ */ -+#define STM32_SIP_FUNC_SR_MODE 0x1004 -+ -+/* DDR Self-Refresh modes */ -+#define STM32_SIP_SR_MODE_SSR 0x0 -+#define STM32_SIP_SR_MODE_ASR 0x1 -+#define STM32_SIP_SR_MODE_HSR 0x2 -+ -+/* -+ * SIP functions STM32_SIP_FUNC_CSTOP -+ * -+ * Argument a0: (input) SMCC ID -+ * (output) status return code -+ * Argument a1: (unused) -+ * Argument a2: (unused) -+ * Argument a3: (input) Target SoC mode -+ */ -+#define STM32_SIP_FUNC_CSTOP 0x1005 -+ -+/* Valid SoC modes used for CSTOP, */ -+#define STM32_SIP_CSLEEP_RUN 0x0 -+#define STM32_SIP_CSTOP_ALLOW_STOP 0x1 -+#define STM32_SIP_CSTOP_ALLOW_LP_STOP 0x2 -+#define STM32_SIP_CSTOP_ALLOW_LPLV_STOP 0x3 -+#define STM32_SIP_CSTOP_ALLOW_STANDBY 0x4 -+#define STM32_SIP_CSTOP_ALLOW_STANDBY_DDR_OFF 0x5 -+#define STM32_SIP_CSTOP_SHUTDOWN 0x6 -+ -+/* -+ * SIP functions STM32_SIP_FUNC_STANDBY -+ * -+ * Argument a0: (input) SMCC ID -+ * (output) status return code -+ * Argument a1: (unused) -+ * Argument a2: (unused) -+ * Argument a3: (input) non null only for DDR off standby -+ */ -+#define STM32_SIP_FUNC_STANDBY 0x1006 -+ -+/* -+ * SIP function STM32_SIP_FUNC_SHUTDOWN -+ * -+ * Argument a0: (input) SMCC ID -+ * (output) status return code -+ */ -+#define STM32_SIP_FUNC_SHUTDOWN 0x1007 -+ -+/* -+ * SIP function STM32_SIP_FUNC_PD_DOMAIN -+ * -+ * Argument a0: (input) SMCC ID -+ * (output) status return code -+ * Argument a2: (index) ID of target power domain to be enabled/disabled -+ * Argument a3: (input) 0 to disable, 1 to eanble target domain -+ */ -+#define STM32_SIP_FUNC_PD_DOMAIN 0x1008 -+ -+/* Valid IDs for power domain for function STM32_SIP_FUNC_PD_DOMAIN */ -+#define STM32_SIP_PD_VSW 0x0 -+#define STM32_SIP_PD_CORE_RET 0x1 -+#define STM32_SIP_PD_CORE 0x2 -+#define STM32_SIP_PD_MAX_PM_DOMAIN 0x3 -+ -+/* -+ * SIP function STM32_SIP_FUNC_RCC_OPP. -+ * -+ * Argument a0: (input) SMCC ID. -+ * (output) Status return code. -+ * Argument a1: (input) Service ID (STM32_SIP_RCC_OPP_xxx). -+ * (output) Rounded frequency, if applicable. -+ * Argument a2: (input) Requested frequency. -+ */ -+#define STM32_SIP_SVC_FUNC_RCC_OPP 0x1009 -+ -+/* Service ID for STM32_SIP_FUNC_RCC_OPP */ -+#define STM32_SIP_SVC_RCC_OPP_SET 0x0 -+#define STM32_SIP_SVC_RCC_OPP_ROUND 0x1 -+ - /* - * SIP function STM32_SIP_SVC_FUNC_SCMI_AGENT0 - * SIP function STM32_SIP_SVC_FUNC_SCMI_AGENT1 -@@ -103,4 +233,68 @@ - #define STM32_SIP_SVC_FUNC_SCMI_AGENT0 0x2000 - #define STM32_SIP_SVC_FUNC_SCMI_AGENT1 0x2001 - -+/* -+ * OEM Functions -+ */ -+#define STM32_OEM_SVC_VERSION_MAJOR 0x0 -+#define STM32_OEM_SVC_VERSION_MINOR 0x1 -+ -+#define STM32_OEM_SVC_FUNCTION_COUNT 1 -+ -+/* Use the same UID as for SiP service */ -+#define STM32_OEM_SVC_UID_0 STM32_SIP_SVC_UID_0 -+#define STM32_OEM_SVC_UID_1 STM32_SIP_SVC_UID_1 -+#define STM32_OEM_SVC_UID_2 STM32_SIP_SVC_UID_2 -+#define STM32_OEM_SVC_UID_3 STM32_SIP_SVC_UID_3 -+ -+ -+/* OEM service generic return codes */ -+#define STM32_OEM_SVC_OK 0x0 -+#define STM32_OEM_SVC_NOT_SUPPORTED 0xffffffffU -+#define STM32_OEM_SVC_FAILED 0xfffffffeU -+#define STM32_OEM_SVC_INVALID_PARAMS 0xfffffffdU -+ -+/* -+ * OEM function STM32_OEM_FUNC_CALL_COUNT -+ * -+ * Argument a0: (input) SMCC ID -+ * (output) Count of defined function IDs -+ */ -+#define STM32_OEM_SVC_FUNC_CALL_COUNT 0xff00 -+ -+/* -+ * OEM function STM32_OEM_SVC_FUNC_UID -+ * -+ * Argument a0: (input) SMCC ID -+ * (output) Lowest 32bit of the stm32mp1 OEM service UUID -+ * Argument a1: (output) Next 32bit of the stm32mp1 OEM service UUID -+ * Argument a2: (output) Next 32bit of the stm32mp1 OEM service UUID -+ * Argument a3: (output) Last 32bit of the stm32mp1 OEM service UUID -+ */ -+#define STM32_OEM_SVC_FUNC_UID 0xff01 -+ -+/* -+ * OEM function STM32_OEM_FUNC_VERSION -+ * -+ * Argument a0: (input) SMCC ID -+ * (output) STM32 OEM service major -+ * Argument a1: (output) STM32 OEM service minor -+ */ -+#define STM32_OEM_SVC_FUNC_VERSION 0xff03 -+ -+/* -+ * OEM function STM32_OEM_SVC_FUNC_LP_FORCE_PARAMS -+ * -+ * Argument a0: (input) SMCC ID -+ * (output) status return code -+ * Argument a2: (input) ID of the mode: suspend or shutdown (off) -+ * Argument a3: (input) ID of the power state to be reached for the mode -+ * Refer to stm32mp1 power bindings. -+ */ -+#define STM32_OEM_SVC_FUNC_LP_FORCE_PARAMS 0x0f800 -+ -+/* Valid IDs for power mode in STM32_OEM_SVC_FUNC_LP_FORCE_PARAMS */ -+#define STM32_OEM_SVC_LP_FORCE_SUSPEND_PARAMS 0 -+#define STM32_OEM_SVC_LP_FORCE_OFF_PARAMS 1 -+ - #endif /* __STM32MP1_SMC_H__*/ -diff --git a/core/arch/arm/plat-stm32mp1/nsec-service/stm32mp1_svc_setup.c b/core/arch/arm/plat-stm32mp1/nsec-service/stm32mp1_svc_setup.c -index 49d23ff02..f5f131df6 100644 ---- a/core/arch/arm/plat-stm32mp1/nsec-service/stm32mp1_svc_setup.c -+++ b/core/arch/arm/plat-stm32mp1/nsec-service/stm32mp1_svc_setup.c -@@ -7,11 +7,32 @@ - #include - #include - #include -+#include - - #include "bsec_svc.h" -+#include "low_power_svc.h" -+#include "pwr_svc.h" -+#include "rcc_svc.h" - #include "stm32mp1_smc.h" - --static enum sm_handler_ret sip_service(struct sm_ctx *ctx __unused, -+#ifdef CFG_STM32_CLKCALIB_SIP -+static uint32_t calib_scv_handler(uint32_t x1) -+{ -+ unsigned long clock_id = x1; -+ -+ if (stm32mp_start_clock_calib(clock_id)) -+ return STM32_SIP_SVC_FAILED; -+ -+ return STM32_SIP_SVC_OK; -+} -+#else -+static uint32_t calib_scv_handler(uint32_t __unused x1) -+{ -+ return STM32_SIP_SVC_FAILED; -+} -+#endif -+ -+static enum sm_handler_ret sip_service(struct sm_ctx *ctx, - struct thread_smc_args *args) - { - switch (OPTEE_SMC_FUNC_NUM(args->a0)) { -@@ -39,6 +60,60 @@ static enum sm_handler_ret sip_service(struct sm_ctx *ctx __unused, - case STM32_SIP_SVC_FUNC_BSEC: - bsec_main(args); - break; -+ case STM32_SIP_SVC_FUNC_RCC: -+ args->a0 = rcc_scv_handler(args->a1, args->a2, args->a3); -+ break; -+ case STM32_SIP_SVC_FUNC_RCC_OPP: -+ args->a0 = rcc_opp_scv_handler(args->a1, args->a2, &args->a1); -+ break; -+ case STM32_SIP_SVC_FUNC_CAL: -+ args->a0 = calib_scv_handler(args->a1); -+ break; -+ case STM32_SIP_SVC_FUNC_PWR: -+ args->a0 = pwr_scv_handler(args->a1, args->a2, args->a3); -+ break; -+ case STM32_SIP_FUNC_SR_MODE: -+ args->a0 = sr_mode_scv_handler(args->a1, args->a2); -+ break; -+ case STM32_SIP_FUNC_CSTOP: -+ args->a0 = cstop_scv_handler(ctx, args->a1, args->a2, args->a3); -+ break; -+ case STM32_SIP_FUNC_STANDBY: -+ args->a0 = standby_scv_handler(ctx, args->a1, args->a2, args->a3); -+ break; -+ case STM32_SIP_FUNC_SHUTDOWN: -+ args->a0 = shutdown_scv_handler(); -+ break; -+ case STM32_SIP_FUNC_PD_DOMAIN: -+ args->a0 = pm_domain_scv_handler(args->a1, args->a2); -+ break; -+ default: -+ return SM_HANDLER_PENDING_SMC; -+ } -+ -+ return SM_HANDLER_SMC_HANDLED; -+} -+ -+static enum sm_handler_ret oem_service(struct sm_ctx *ctx __unused, -+ struct thread_smc_args *args) -+{ -+ switch (OPTEE_SMC_FUNC_NUM(args->a0)) { -+ case STM32_OEM_SVC_FUNC_CALL_COUNT: -+ args->a0 = STM32_OEM_SVC_FUNCTION_COUNT; -+ break; -+ case STM32_OEM_SVC_FUNC_VERSION: -+ args->a0 = STM32_OEM_SVC_VERSION_MAJOR; -+ args->a1 = STM32_OEM_SVC_VERSION_MINOR; -+ break; -+ case STM32_OEM_SVC_FUNC_UID: -+ args->a0 = STM32_OEM_SVC_UID_0; -+ args->a1 = STM32_OEM_SVC_UID_1; -+ args->a2 = STM32_OEM_SVC_UID_2; -+ args->a3 = STM32_OEM_SVC_UID_3; -+ break; -+ case STM32_OEM_SVC_FUNC_LP_FORCE_PARAMS: -+ args->a0 = pm_set_lp_state_scv_handler(args->a1, args->a2); -+ break; - default: - return SM_HANDLER_PENDING_SMC; - } -@@ -56,6 +131,8 @@ enum sm_handler_ret sm_platform_handler(struct sm_ctx *ctx) - switch (OPTEE_SMC_OWNER_NUM(args->a0)) { - case OPTEE_SMC_OWNER_SIP: - return sip_service(ctx, args); -+ case OPTEE_SMC_OWNER_OEM: -+ return oem_service(ctx, args); - default: - return SM_HANDLER_PENDING_SMC; - } -diff --git a/core/arch/arm/plat-stm32mp1/nsec-service/sub.mk b/core/arch/arm/plat-stm32mp1/nsec-service/sub.mk -index 6e1abcb4a..acccda16a 100644 ---- a/core/arch/arm/plat-stm32mp1/nsec-service/sub.mk -+++ b/core/arch/arm/plat-stm32mp1/nsec-service/sub.mk -@@ -2,3 +2,6 @@ global-incdirs-y += . - - srcs-y += stm32mp1_svc_setup.c - srcs-$(CFG_STM32_BSEC_SIP) += bsec_svc.c -+srcs-$(CFG_STM32_LOWPOWER_SIP) += low_power_svc.c -+srcs-$(CFG_STM32_PWR_SIP) += pwr_svc.c -+srcs-$(CFG_STM32_RCC_SIP) += rcc_svc.c -diff --git a/core/arch/arm/plat-stm32mp1/plat_tzc400.c b/core/arch/arm/plat-stm32mp1/plat_tzc400.c -index bce923711..23c7170e5 100644 ---- a/core/arch/arm/plat-stm32mp1/plat_tzc400.c -+++ b/core/arch/arm/plat-stm32mp1/plat_tzc400.c -@@ -33,39 +33,45 @@ static struct itr_handler tzc_itr_handler = { - }; - DECLARE_KEEP_PAGER(tzc_itr_handler); - --static bool tzc_region_is_non_secure(unsigned int i, vaddr_t base, size_t size) -+static bool tzc_find_region(vaddr_t base, size_t size, struct tzc_region_config *region_cfg) -+{ -+ uint8_t i = 1; -+ -+ while (true) { -+ if (tzc_get_region_config(i++, region_cfg) != TEE_SUCCESS) -+ return false; -+ -+ if (region_cfg->base <= base && region_cfg->top >= (base + size - 1)) -+ return true; -+ } -+} -+ -+static bool tzc_region_is_non_secure(vaddr_t base, size_t size) - { - struct tzc_region_config region_cfg = { }; - uint32_t ns_cpu_mask = TZC_REGION_ACCESS_RDWR(STM32MP1_TZC_A7_ID); - uint32_t filters_mask = GENMASK_32(1, 0); - -- if (tzc_get_region_config(i, ®ion_cfg)) -- panic(); -- -- return region_cfg.base == base && region_cfg.top == (base + size - 1) && -- region_cfg.sec_attr == TZC_REGION_S_NONE && -- (region_cfg.ns_device_access & ns_cpu_mask) == ns_cpu_mask && -- region_cfg.filters == filters_mask; -+ return tzc_find_region(base, size, ®ion_cfg) && -+ region_cfg.sec_attr == TZC_REGION_S_NONE && -+ (region_cfg.ns_device_access & ns_cpu_mask) == ns_cpu_mask && -+ region_cfg.filters == filters_mask; - } - --static bool tzc_region_is_secure(unsigned int i, vaddr_t base, size_t size) -+static bool tzc_region_is_secure(vaddr_t base, size_t size) - { - struct tzc_region_config region_cfg = { }; - uint32_t filters_mask = GENMASK_32(1, 0); - -- if (tzc_get_region_config(i, ®ion_cfg)) -- panic(); -- -- return region_cfg.base == base && region_cfg.top == (base + size - 1) && -- region_cfg.sec_attr == TZC_REGION_S_RDWR && -- region_cfg.ns_device_access == 0 && -- region_cfg.filters == filters_mask; -+ return tzc_find_region(base, size, ®ion_cfg) && -+ region_cfg.sec_attr == TZC_REGION_S_RDWR && -+ region_cfg.ns_device_access == 0 && -+ region_cfg.filters == filters_mask; - } - - static TEE_Result init_stm32mp1_tzc(void) - { - void *base = phys_to_virt(TZC_BASE, MEM_AREA_IO_SEC); -- unsigned int region_index = 1; - const uint64_t dram_start = DDR_BASE; - const uint64_t dram_end = dram_start + CFG_DRAM_SIZE; - const uint64_t tzdram_start = CFG_TZDRAM_START; -@@ -83,21 +89,15 @@ static TEE_Result init_stm32mp1_tzc(void) - * expectations. - */ - if (dram_start < tzdram_start) { -- if (!tzc_region_is_non_secure(region_index, dram_start, -- tzdram_start - dram_start)) -+ if (!tzc_region_is_non_secure(dram_start, tzdram_start - dram_start)) - panic("Unexpected TZC area on non-secure region"); -- -- region_index++; - } - -- if (!tzc_region_is_secure(region_index, tzdram_start, tzdram_size)) -+ if (!tzc_region_is_secure(tzdram_start, tzdram_size)) - panic("Unexpected TZC configuration on secure region"); - - if (tzdram_end < dram_end) { -- region_index++; -- -- if (!tzc_region_is_non_secure(region_index, tzdram_end, -- dram_end - tzdram_end)) -+ if (!tzc_region_is_non_secure(tzdram_end, dram_end - tzdram_end)) - panic("Unexpected TZC area on non-secure region"); - } - -diff --git a/core/arch/arm/plat-stm32mp1/platform_config.h b/core/arch/arm/plat-stm32mp1/platform_config.h -index cf68f4510..0ea5efc32 100644 ---- a/core/arch/arm/plat-stm32mp1/platform_config.h -+++ b/core/arch/arm/plat-stm32mp1/platform_config.h -@@ -8,14 +8,47 @@ - - #include - -+/* Enable/disable use of the core0 reset control from RCC */ -+#undef STM32MP1_USE_MPU0_RESET -+ - /* Make stacks aligned to data cache line length */ - #define STACK_ALIGNMENT 32 - -+#if defined(CFG_WITH_PAGER) -+#if defined(CFG_WITH_LPAE) -+/* -+ * Optimize unpaged memory size: -+ * - one table for the level2 table for overall vmem range -+ * - two tables for TEE RAM fine grain mapping [2ffc.0000 301f.ffff] -+ * - one table for a 2MByte dynamic shared virtual memory (SHM_VASPACE) -+ */ -+#define MAX_XLAT_TABLES 4 -+#else -+/* -+ * Optimize unpaged memory size: -+ * - two tables for TEE RAM mapping [2ffc.0000 300f.ffff] -+ * - one table for secure internal RAMs (PM: ROMed core TEE RAM) -+ * - one table for non-secure internal RAMs (PM: DDR first page) -+ * - two tables for a 2MByte dynamiq shared virtual memory (SHM_VASPACE) -+ */ -+#define MAX_XLAT_TABLES 6 -+#endif /*CFG_WITH_LPAE*/ -+#else -+/* Be generous with this setup that has plenty of secure RAM */ -+#define MAX_XLAT_TABLES 10 -+#endif /*CFG_WITH_PAGER*/ -+ - /* SoC interface registers base address */ -+#define MCUSRAM_BASE 0x30000000ul -+#define RETRAM_BASE 0x38000000ul -+#define BKPSRAM_BASE 0x54000000 - #define BSEC_BASE 0x5c005000 - #define ETZPC_BASE 0x5c007000 - #define CRYP1_BASE 0x54001000 -+#define DBGMCU_BASE 0x50081000 - #define DDR_BASE 0xc0000000ul -+#define DDRCTRL_BASE 0x5a003000 -+#define DDRPHYC_BASE 0x5a004000 - #define GIC_BASE 0xa0021000ul - #define GPIOA_BASE 0x50002000 - #define GPIOB_BASE 0x50003000 -@@ -39,9 +72,24 @@ - #define RNG1_BASE 0x54003000 - #define RTC_BASE 0x5c004000 - #define SPI6_BASE 0x5c001000 -+#define STGEN_BASE 0x5c008000 - #define SYSCFG_BASE 0x50020000 - #define SYSRAM_BASE 0x2ffc0000 - #define TAMP_BASE 0x5c00a000 -+#define TIM1_BASE 0x44000000 -+#define TIM2_BASE 0x40000000 -+#define TIM3_BASE 0x40001000 -+#define TIM4_BASE 0x40002000 -+#define TIM5_BASE 0x40003000 -+#define TIM6_BASE 0x40004000 -+#define TIM7_BASE 0x40005000 -+#define TIM8_BASE 0x44001000 -+#define TIM12_BASE 0x40006000 -+#define TIM13_BASE 0x40007000 -+#define TIM14_BASE 0x40008000 -+#define TIM15_BASE 0x44006000 -+#define TIM16_BASE 0x44007000 -+#define TIM17_BASE 0x44008000 - #define TZC_BASE 0x5c006000 - #define UART1_BASE 0x5c000000 - #define UART2_BASE 0x4000e000 -@@ -52,6 +100,9 @@ - #define UART7_BASE 0x40018000 - #define UART8_BASE 0x40019000 - -+/* DDR expected size if not found in device tree */ -+#define STM32MP1_DDR_SIZE_DFLT (1 * 1024 * 1024 * 1024) -+ - /* Console configuration */ - #define STM32MP1_DEBUG_USART_BASE UART4_BASE - #define GIC_SPI_UART4 84 -@@ -65,16 +116,21 @@ - - #define OTP_MAX_SIZE (STM32MP1_OTP_MAX_ID + 1U) - --#define DATA0_OTP 0 --#define PART_NUMBER_OTP 1 --#define MONOTONIC_OTP 4 --#define NAND_OTP 9 --#define UID0_OTP 13 --#define UID1_OTP 14 --#define UID2_OTP 15 --#define HW2_OTP 18 -+#define CFG0_OTP "cfg0_otp" -+#define CFG0_OTP_SECURED_POS 6 -+ -+#define HW2_OTP "hw2_otp" -+#define HW2_OTP_IWDG_HW_ENABLE_SHIFT 3 -+#define HW2_OTP_IWDG_FZ_STOP_SHIFT 5 -+#define HW2_OTP_IWDG_FZ_STANDBY_SHIFT 7 - --#define DATA0_OTP_SECURED_POS 6 -+#define PART_NUMBER_OTP "part_number_otp" -+#define PART_NUMBER_OTP_PART_MASK GENMASK_32(7, 0) -+#define PART_NUMBER_OTP_PART_SHIFT 0 -+ -+#define HW2_OTP_IWDG_HW_ENABLE_SHIFT 3 -+#define HW2_OTP_IWDG_FZ_STOP_SHIFT 5 -+#define HW2_OTP_IWDG_FZ_STANDBY_SHIFT 7 - - /* GIC resources */ - #define GIC_SIZE 0x2000 -@@ -84,11 +140,14 @@ - #define GIC_NON_SEC_SGI_0 0 - #define GIC_SEC_SGI_0 8 - #define GIC_SEC_SGI_1 9 -+#define GIC_SPI_SEC_PHY_TIMER 29 - - #define TARGET_CPU0_GIC_MASK BIT(0) - #define TARGET_CPU1_GIC_MASK BIT(1) - #define TARGET_CPUS_GIC_MASK GENMASK_32(CFG_TEE_CORE_NB_CORE - 1, 0) - -+#define STM32MP_GIC_PRIORITY_CSTOP 0xc0 -+ - /* - * GPIO banks: 11 non secure banks (A to K) and 1 secure bank (Z) - * Bank register's base address is computed from the bank ID listed here. -@@ -116,6 +175,16 @@ - #define GPIO_BANK_K 10U - #define GPIO_BANK_Z 25U - -+/* IWDG resources */ -+#define IWDG1_INST 0 -+#define IWDG2_INST 1 -+ -+#define STM32MP1_IRQ_IWDG1 182U -+#define STM32MP1_IRQ_IWDG2 183U -+ -+/* RCC platform resources */ -+#define RCC_WAKEUP_IT 177 -+ - /* TAMP resources */ - #define TAMP_BKP_REGISTER_OFF 0x100 - -@@ -140,6 +209,24 @@ - #define USART3_BASE UART3_BASE - #define USART6_BASE UART6_BASE - -+/* DBGMCU resources */ -+#define DBGMCU_IDC 0x0 -+#define DBGMCU_IDC_DEV_ID_MASK GENMASK_32(11, 0) -+#define DBGMCU_IDC_REV_ID_MASK GENMASK_32(31, 16) -+#define DBGMCU_IDC_REV_ID_SHIFT 16 -+ -+/* BKPSRAM layout */ -+#define BKPSRAM_SIZE 0x1000 -+#define BKPSRAM_PM_OFFSET 0x000 -+#define BKPSRAM_PM_SIZE (BKPSRAM_PM_MAILBOX_SIZE + \ -+ BKPSRAM_PM_CONTEXT_SIZE) -+ -+#define BKPSRAM_PM_MAILBOX_OFFSET BKPSRAM_PM_OFFSET -+#define BKPSRAM_PM_MAILBOX_SIZE 0x100 -+#define BKPSRAM_PM_CONTEXT_OFFSET (BKPSRAM_PM_MAILBOX_OFFSET + \ -+ BKPSRAM_PM_MAILBOX_SIZE) -+#define BKPSRAM_PM_CONTEXT_SIZE 0xF00 -+ - /* SYSRAM layout */ - #define SYSRAM_SIZE 0x40000 - #define SYSRAM_NS_SIZE (SYSRAM_SIZE - SYSRAM_SEC_SIZE) -@@ -153,4 +240,38 @@ - #define SYSRAM_SEC_SIZE SYSRAM_SIZE - #endif - -+/* RETRAM layout */ -+#define RETRAM_SIZE 0x10000 -+ -+/* MCUSRAM layout */ -+#define MCUSRAM_SIZE 0x60000 -+ -+/* SoC part numbers and revisions */ -+#define STM32MP1_CHIP_ID 0x500 -+ -+#define STM32MP157C_PART_NB 0x05000000 -+#define STM32MP157A_PART_NB 0x05000001 -+#define STM32MP153C_PART_NB 0x05000024 -+#define STM32MP153A_PART_NB 0x05000025 -+#define STM32MP151C_PART_NB 0x0500002E -+#define STM32MP151A_PART_NB 0x0500002F -+#define STM32MP157F_PART_NB 0x05000080 -+#define STM32MP157D_PART_NB 0x05000081 -+#define STM32MP153F_PART_NB 0x050000A4 -+#define STM32MP153D_PART_NB 0x050000A5 -+#define STM32MP151F_PART_NB 0x050000AE -+#define STM32MP151D_PART_NB 0x050000AF -+ -+#define STM32MP1_CHIP_DEFAULT_VERSION 0 -+ -+#define STM32MP1_REV_A 0x00001000 -+#define STM32MP1_REV_B 0x00002000 -+#define STM32MP1_REV_Z 0x00002001 -+ -+/* OPP */ -+#define PLAT_OPP_ID1 1U -+#define PLAT_OPP_ID2 2U -+#define PLAT_MAX_OPP_NB 2U -+#define PLAT_MAX_PLLCFG_NB 6U -+ - #endif /*PLATFORM_CONFIG_H*/ -diff --git a/core/arch/arm/plat-stm32mp1/pm/context.c b/core/arch/arm/plat-stm32mp1/pm/context.c -new file mode 100644 -index 000000000..8dddb0c02 ---- /dev/null -+++ b/core/arch/arm/plat-stm32mp1/pm/context.c -@@ -0,0 +1,522 @@ -+// SPDX-License-Identifier: BSD-3-Clause -+/* -+ * Copyright (c) 2018-2020, STMicroelectronics - All Rights Reserved -+ * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved. -+ */ -+ -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+ -+#include "context.h" -+#include "power.h" -+ -+#define TRAINING_AREA_SIZE 64 -+ -+/* -+ * STANDBY_CONTEXT_MAGIC0: -+ * Context provides magic, resume entry, zq0cr0 zdata and DDR training buffer. -+ * -+ * STANDBY_CONTEXT_MAGIC1: -+ * Context provides magic, resume entry, zq0cr0 zdata, DDR training buffer -+ * and PLL1 dual OPP settings structure (86 bytes). -+ */ -+#define STANDBY_CONTEXT_MAGIC0 (0x0001 << 16) -+#define STANDBY_CONTEXT_MAGIC1 (0x0002 << 16) -+ -+#if CFG_STM32MP15_PM_CONTEX_VERSION == 1 -+#define STANDBY_CONTEXT_MAGIC (STANDBY_CONTEXT_MAGIC0 | TRAINING_AREA_SIZE) -+#elif CFG_STM32MP15_PM_CONTEX_VERSION == 2 -+#define STANDBY_CONTEXT_MAGIC (STANDBY_CONTEXT_MAGIC1 | TRAINING_AREA_SIZE) -+#else -+#error Invalid value for CFG_STM32MP15_PM_CONTEX_VERSION -+#endif -+ -+#if (PLAT_MAX_OPP_NB != 2) || (PLAT_MAX_PLLCFG_NB != 6) -+#error STANDBY_CONTEXT_MAGIC1 does not support expected PLL1 settings -+#endif -+ -+/* pll_settings structure size definitions (reference to clock driver) */ -+#define PLL1_SETTINGS_SIZE (((PLAT_MAX_OPP_NB * \ -+ (PLAT_MAX_PLLCFG_NB + 3)) + 1) * \ -+ sizeof(uint32_t)) -+ -+/* -+ * Context saved in TEE RAM during lower power sequence. -+ * Can be allocated if to big for static allocation. -+ * -+ * @stgen_cnt_h: Upper 32bit of the STGEN counter -+ * @stgen_cnt_l: Lower 32bit of the STGEN counter -+ * @rtc: RTC time read at suspend -+ */ -+struct pm_context { -+ uint32_t stgen_cnt_h; -+ uint32_t stgen_cnt_l; -+ struct stm32_rtc_calendar rtc; -+}; -+ -+static struct pm_context plat_ctx; -+ -+/* -+ * BKPSRAM contains a mailbox used with early boot stages for resume sequence. -+ * The mailbox content data that must be restored before OP-TEE is resumed. -+ * -+ * @magic: magic value read by early boot stage for consistency -+ * @zq0cr0_zdata: DDRPHY configuration to be restored. -+ * @ddr_training_backup: DDR area saved at suspend and backed up at resume -+ */ -+struct pm_mailbox { -+ uint32_t magic; -+ uint32_t core0_resume_ep; -+ uint32_t zq0cr0_zdata; -+ uint8_t ddr_training_backup[TRAINING_AREA_SIZE]; -+#if CFG_STM32MP15_PM_CONTEX_VERSION >= 2 -+ uint8_t pll1_settings[PLL1_SETTINGS_SIZE]; -+#endif -+}; -+ -+/* -+ * BKPSRAM contains OP-TEE resume instruction sequence which restores -+ * TEE RAM content. The BKPSRAM contains restoration materials -+ * (key, tag) and the resume entry point in restored TEE RAM. -+ */ -+static struct retram_resume_ctx *get_retram_resume_ctx(void) -+{ -+ vaddr_t bkpsram_base = stm32mp_bkpsram_base(); -+ vaddr_t context_base = bkpsram_base + BKPSRAM_PM_CONTEXT_OFFSET; -+ -+ return (struct retram_resume_ctx *)context_base; -+} -+ -+static struct pm_mailbox *get_pm_mailbox(void) -+{ -+ vaddr_t bkpsram_base = stm32mp_bkpsram_base(); -+ vaddr_t mailbox_base = bkpsram_base + BKPSRAM_PM_MAILBOX_OFFSET; -+ -+ return (struct pm_mailbox *)mailbox_base; -+} -+ -+#if TRACE_LEVEL >= TRACE_DEBUG -+static void __maybe_unused dump_context(void) -+{ -+ struct pm_mailbox *mailbox = get_pm_mailbox(); -+ struct retram_resume_ctx *ctx = get_retram_resume_ctx(); -+ -+ clk_enable(RTCAPB); -+ -+ DMSG("Backup registers: address 0x%" PRIx32 ", magic 0x%" PRIx32, -+ *(uint32_t *)stm32mp_bkpreg(BCKR_CORE1_BRANCH_ADDRESS), -+ *(uint32_t *)stm32mp_bkpreg(BCKR_CORE1_MAGIC_NUMBER)); -+ -+ clk_disable(RTCAPB); -+ -+ clk_enable(BKPSRAM); -+ -+ DMSG("BKPSRAM mailbox: 0x%" PRIx32 ", zd 0x%" PRIx32 ", ep 0x%" PRIx32, -+ mailbox->magic, mailbox->zq0cr0_zdata, -+ mailbox->core0_resume_ep); -+ -+ DMSG("BKPSRAM context: teeram backup @%" PRIx32 ", resume @0x%" PRIx32, -+ ctx->teeram_bkp_pa, ctx->resume_pa); -+ -+ clk_disable(BKPSRAM); -+} -+#else -+static void __maybe_unused dump_context(void) -+{ -+} -+#endif -+ -+/* -+ * Save and restore functions -+ */ -+static void save_time(void) -+{ -+ vaddr_t stgen = stm32mp_stgen_base(); -+ -+ plat_ctx.stgen_cnt_h = io_read32(stgen + CNTCVU_OFFSET); -+ plat_ctx.stgen_cnt_l = io_read32(stgen + CNTCVL_OFFSET); -+ if (plat_ctx.stgen_cnt_l < 10) -+ plat_ctx.stgen_cnt_h = io_read32(stgen + CNTCVU_OFFSET); -+ -+ clk_enable(RTC); -+ stm32_rtc_get_calendar(&plat_ctx.rtc); -+} -+ -+#if TRACE_LEVEL >= TRACE_DEBUG -+static void print_ccm_decryption_duration(void) -+{ -+ vaddr_t stgen = stm32mp_stgen_base(); -+ struct retram_resume_ctx *ctx = get_retram_resume_ctx(); -+ -+ clk_enable(BKPSRAM); -+ -+ DMSG("CCM decryption duration %llums", -+ ((unsigned long long)ctx->stgen_cnt * 1000) / -+ io_read32(stgen + CNTFID_OFFSET)); -+ -+ clk_enable(BKPSRAM); -+} -+#else -+static void print_ccm_decryption_duration(void) -+{ -+} -+#endif -+ -+static void restore_time(void) -+{ -+ struct stm32_rtc_calendar current_calendar = { }; -+ unsigned long long stdby_time_in_ms = 0; -+ unsigned long long cnt = 0; -+ vaddr_t stgen = stm32mp_stgen_base(); -+ struct retram_resume_ctx __maybe_unused *ctx = get_retram_resume_ctx(); -+ -+ stm32_rtc_get_calendar(¤t_calendar); -+ stdby_time_in_ms = stm32_rtc_diff_calendar(¤t_calendar, -+ &plat_ctx.rtc); -+ -+ cnt = ((uint64_t)plat_ctx.stgen_cnt_h << 32) | plat_ctx.stgen_cnt_l; -+ cnt += (stdby_time_in_ms * io_read32(stgen + CNTFID_OFFSET)) / 1000U; -+ -+ io_clrbits32(stgen + CNTCR_OFFSET, CNTCR_EN); -+ io_write32(stgen + CNTCVL_OFFSET, (uint32_t)cnt); -+ io_write32(stgen + CNTCVU_OFFSET, (uint32_t)(cnt >> 32)); -+ io_setbits32(stgen + CNTCR_OFFSET, CNTCR_EN); -+ -+ /* Balance clock enable(RTC) at save_time() */ -+ clk_disable(RTC); -+ -+ print_ccm_decryption_duration(); -+} -+ -+static bool __maybe_unused pm_cb_is_valid(void (*cb)(enum pm_op op, void *hdl), -+ void *hdl) -+{ -+ void *cb_voidp = (void *)(vaddr_t)cb; -+ paddr_t cb_phy = virt_to_phys(cb_voidp); -+ paddr_t hdl_phy = virt_to_phys(hdl); -+ bool valid = false; -+ -+ valid = (phys_to_virt(cb_phy, MEM_AREA_TEE_RAM_RX) == cb_voidp) && -+ ((phys_to_virt(hdl_phy, MEM_AREA_TEE_RAM_RX) == hdl) || -+ (phys_to_virt(hdl_phy, MEM_AREA_TEE_RAM_RO) == hdl) || -+ (phys_to_virt(hdl_phy, MEM_AREA_TEE_RAM_RW) == hdl)); -+ -+ if (!valid) -+ EMSG("pm_cb mandates unpaged arguments %p %p", cb_voidp, hdl); -+ -+ return valid; -+} -+ -+uintptr_t stm32mp_pm_retram_resume_ep(void) -+{ -+ struct retram_resume_ctx *ctx = get_retram_resume_ctx(); -+ -+ return (uintptr_t)&ctx->resume_sequence; -+} -+ -+/* Clear the content of the PM mailbox */ -+void stm32mp_pm_wipe_context(void) -+{ -+ struct retram_resume_ctx *ctx = get_retram_resume_ctx(); -+ struct pm_mailbox *mailbox = get_pm_mailbox(); -+ -+ clk_enable(BKPSRAM); -+ -+ memset(ctx, 0xa5, sizeof(*ctx)); -+ memset(mailbox, 0xa5, sizeof(*mailbox)); -+ -+ clk_disable(BKPSRAM); -+} -+ -+static struct mobj *teeram_bkp_mobj; -+ -+static void init_retram_resume_resources(void) -+{ -+ struct retram_resume_ctx *ctx = get_retram_resume_ctx(); -+ size_t __maybe_unused csize = 0; -+ paddr_t __maybe_unused pa = 0; -+ -+ COMPILE_TIME_ASSERT(sizeof(struct pm_mailbox) < -+ BKPSRAM_PM_MAILBOX_SIZE); -+ COMPILE_TIME_ASSERT(sizeof(struct retram_resume_ctx) < -+ BKPSRAM_PM_CONTEXT_SIZE); -+ csize = (vaddr_t)stm32mp_bkpsram_image_end - -+ (vaddr_t)stm32mp_bkpsram_resume; -+ assert((sizeof(*ctx) + csize) < BKPSRAM_PM_CONTEXT_SIZE); -+ -+ teeram_bkp_mobj = mobj_mm_alloc(mobj_sec_ddr, TEE_RAM_PH_SIZE, -+ &tee_mm_sec_ddr); -+ if (!teeram_bkp_mobj) -+ panic(); -+ -+ assert((mobj_get_va(teeram_bkp_mobj, 0) != NULL) && -+ (mobj_get_pa(teeram_bkp_mobj, 0, 0, &pa) == 0)); -+ -+ clk_enable(BKPSRAM); -+ memset(ctx, 0, sizeof(*ctx)); -+ clk_disable(BKPSRAM); -+} -+ -+/* -+ * When returning from STANDBY, the 64 first bytes of DDR will be overwritten -+ * during DDR DQS training. This area must then be saved before going to -+ * standby in the PM mailbox with the earlier boot stages. -+ */ -+static void save_ddr_training_area(void) -+{ -+ struct pm_mailbox *mailbox = get_pm_mailbox(); -+ size_t size = sizeof(mailbox->ddr_training_backup); -+ paddr_t pa = DDR_BASE; -+ void *va = phys_to_virt(pa, MEM_AREA_RAM_NSEC); -+ -+ memcpy(&mailbox->ddr_training_backup[0], va, size); -+ -+} -+ -+/* -+ * When returning from STANDBY, warm boot boot stage needs to access to PLL1 -+ * settings. This avoids to re-compute them and optimizes performances. This -+ * structure must then be saved before going to STANDBY in the PM mailbox -+ * shared with the warm boot boot stage. -+ */ -+#if CFG_STM32MP15_PM_CONTEX_VERSION >= 2 -+static void save_pll1_settings(void) -+{ -+ struct pm_mailbox *mailbox = get_pm_mailbox(); -+ size_t size = sizeof(mailbox->pll1_settings); -+ uint8_t *data = &mailbox->pll1_settings[0]; -+ -+ stm32mp1_clk_lp_save_opp_pll1_settings(data, size); -+} -+#endif -+ -+static void load_earlyboot_pm_mailbox(void) -+{ -+ struct pm_mailbox *mailbox = get_pm_mailbox(); -+ -+ COMPILE_TIME_ASSERT(sizeof(struct pm_mailbox) < -+ BKPSRAM_PM_MAILBOX_SIZE); -+ -+ assert(clk_is_enabled(BKPSRAM)); -+ -+ memset(mailbox, 0, sizeof(*mailbox)); -+ -+ mailbox->zq0cr0_zdata = get_ddrphy_calibration(); -+ -+ save_ddr_training_area(); -+ -+#if CFG_STM32MP15_PM_CONTEX_VERSION >= 2 -+ save_pll1_settings(); -+#endif -+} -+ -+#if defined(CFG_STM32_RNG) && defined(CFG_STM32_CRYP) -+/* -+ * CRYP relies on standard format for CCM IV/B0/CRT0 data. Our sequence uses -+ * no AAD, 4 bytes to encode the payload byte size and a 11 byte nonce. -+ */ -+#define PM_CCM_Q 4 -+#define PM_CCM_Q_FLAGS (PM_CCM_Q - 1) -+#define PM_CCM_TAG_LEN 16 -+#define PM_CCM_TAG_FLAGS (((PM_CCM_TAG_LEN - 2) / 2) << 3) -+ -+static void save_teeram_in_ddr(void) -+{ -+ struct retram_resume_ctx *ctx = get_retram_resume_ctx(); -+ size_t __maybe_unused size = (vaddr_t)stm32mp_bkpsram_image_end - -+ (vaddr_t)stm32mp_bkpsram_resume; -+ paddr_t pa = 0; -+ struct ccm_unpg_ctx *ccm = &ctx->ccm_ctx; -+ void *teeram = phys_to_virt(TEE_RAM_START, MEM_AREA_ROM_SEC); -+ void *teeram_bkp = mobj_get_va(teeram_bkp_mobj, 0); -+ -+ COMPILE_TIME_ASSERT(PM_CTX_CCM_KEY_SIZE == sizeof(ccm->key)); -+ COMPILE_TIME_ASSERT(PM_CTX_CCM_CTR1_SIZE == sizeof(ccm->ctr1)); -+ COMPILE_TIME_ASSERT(PM_CTX_CCM_B0_SIZE == sizeof(ccm->b0)); -+ COMPILE_TIME_ASSERT(PM_CTX_CCM_CTR0_SIZE == sizeof(ccm->ctr0)); -+ COMPILE_TIME_ASSERT(PM_CTX_CCM_TAG_SIZE == sizeof(ccm->tag)); -+ -+ assert(clk_is_enabled(BKPSRAM) && -+ clk_is_enabled(CRYP1)); -+ -+ memcpy(ctx->resume_sequence, -+ (void *)(vaddr_t)stm32mp_bkpsram_resume, size); -+ -+ memset(ctx, 0, sizeof(*ctx)); -+ ctx->resume_pa = virt_to_phys((void *)(vaddr_t)stm32mp_sysram_resume); -+ if (mobj_get_pa(teeram_bkp_mobj, 0, 0, &pa)) -+ panic(); -+ -+ ctx->teeram_bkp_pa = (uint32_t)pa; -+ ctx->cryp1_base = (uint32_t)phys_to_virt(CRYP1_BASE, MEM_AREA_IO_SEC); -+ ctx->rcc_base = (uint32_t)phys_to_virt(RCC_BASE, MEM_AREA_IO_SEC); -+ ctx->stgen_base = (uint32_t)phys_to_virt(STGEN_BASE, MEM_AREA_IO_SEC); -+ -+ if (stm32_rng_read((uint8_t *)ccm->key, sizeof(ccm->key))) -+ panic(); -+ -+ assert(((PM_CCM_TAG_FLAGS & ~0x38U) | (PM_CCM_Q_FLAGS & ~0x07U)) == 0); -+ COMPILE_TIME_ASSERT(PM_CCM_Q <= 4); -+ COMPILE_TIME_ASSERT(TEE_RAM_PH_SIZE > UINT16_MAX); -+ COMPILE_TIME_ASSERT(TEE_RAM_PH_SIZE < UINT32_MAX); -+ -+ if (stm32_rng_read((uint8_t *)ccm->ctr1, sizeof(ccm->ctr1))) -+ panic(); -+ -+ ccm->ctr1[0] &= GENMASK_32(24, 0); -+ memcpy(ccm->b0, ccm->ctr1, sizeof(ccm->b0)); -+ memcpy(ccm->ctr0, ccm->ctr1, sizeof(ccm->ctr0)); -+ -+ ccm->ctr0[0] |= PM_CCM_Q_FLAGS << 24; -+ ccm->ctr0[3] = 0; -+ ccm->ctr1[0] |= PM_CCM_Q_FLAGS << 24; -+ ccm->ctr1[3] = 1; -+ ccm->b0[0] |= (PM_CCM_Q_FLAGS | PM_CCM_TAG_FLAGS) << 24; -+ ccm->b0[3] = TEE_RAM_PH_SIZE; -+ -+ stm32mp_ccm_encrypt_teeram(ctx, teeram_bkp, teeram, TEE_RAM_PH_SIZE); -+ dcache_clean_range(teeram_bkp, TEE_RAM_PH_SIZE); -+ -+ memcpy(ctx->ccm_ref_tag, ccm->tag, sizeof(ctx->ccm_ref_tag)); -+ -+ DMSG("CCM encryption duration %llums", -+ ((unsigned long long)ctx->stgen_cnt * 1000) / -+ io_read32(ctx->stgen_base + CNTFID_OFFSET)); -+ ctx->stgen_cnt = 0; -+} -+#else -+static void save_teeram_in_ddr(void) -+{ -+ panic("Mandates RNG and CRYP support"); -+} -+#endif /*CFG_STM32_RNG*/ -+ -+/* Finalize the PM mailbox now that everything is loaded */ -+static void enable_pm_mailbox(unsigned int suspend) -+{ -+ struct pm_mailbox *mailbox = get_pm_mailbox(); -+ uint32_t magic = 0; -+ uint32_t hint = 0; -+ -+ assert(clk_is_enabled(BKPSRAM) && -+ clk_is_enabled(RTCAPB)); -+ -+ if (suspend) { -+ magic = BOOT_API_A7_CORE0_MAGIC_NUMBER; -+ mailbox->magic = STANDBY_CONTEXT_MAGIC; -+ -+ hint = virt_to_phys(&get_retram_resume_ctx()->resume_sequence); -+ } else { -+ mailbox->magic = 0; -+ } -+ -+ io_write32(stm32mp_bkpreg(BCKR_CORE1_MAGIC_NUMBER), magic); -+ io_write32(stm32mp_bkpreg(BCKR_CORE1_BRANCH_ADDRESS), hint); -+ -+ mailbox->core0_resume_ep = hint; -+} -+ -+static void gate_pm_context_clocks(bool enable) -+{ -+ static bool clocks_enabled; -+ -+ if (enable) { -+ assert(!clocks_enabled); -+ clk_enable(BKPSRAM); -+ clk_enable(RTCAPB); -+ clk_enable(CRYP1); -+ clocks_enabled = true; -+ return; -+ } -+ -+ /* Suspended TEE RAM state left the clocks enabled */ -+ if (clocks_enabled) { -+ clk_disable(BKPSRAM); -+ clk_disable(RTCAPB); -+ clk_disable(CRYP1); -+ clocks_enabled = false; -+ } -+} -+ -+/* -+ * Context (TEE RAM content + peripherals) must be restored -+ * only if system may reach STANDBY state. -+ */ -+TEE_Result stm32mp_pm_save_context(unsigned int soc_mode) -+{ -+ TEE_Result res = TEE_ERROR_GENERIC; -+ -+ save_time(); -+ -+ if (!need_to_backup_cpu_context(soc_mode)) { -+ if (need_to_backup_stop_context(soc_mode)) -+ stm32mp1_clk_save_context_for_stop(); -+ -+ return TEE_SUCCESS; -+ } -+ -+ gate_pm_context_clocks(true); -+ load_earlyboot_pm_mailbox(); -+ res = pm_change_state(PM_OP_SUSPEND, 0); -+ if (res) -+ return res; -+ -+ save_teeram_in_ddr(); -+ enable_pm_mailbox(1); -+ -+ return TEE_SUCCESS; -+} -+ -+void stm32mp_pm_restore_context(unsigned int soc_mode) -+{ -+ if (need_to_backup_cpu_context(soc_mode)) { -+ if (pm_change_state(PM_OP_RESUME, 0)) -+ panic(); -+ -+ gate_pm_context_clocks(false); -+ } else if (need_to_backup_stop_context(soc_mode)) { -+ stm32mp1_clk_restore_context_for_stop(); -+ } -+ -+ restore_time(); -+} -+ -+void stm32mp_pm_shutdown_context(void) -+{ -+ gate_pm_context_clocks(true); -+ load_earlyboot_pm_mailbox(); -+ enable_pm_mailbox(0); -+ gate_pm_context_clocks(false); -+} -+ -+static TEE_Result init_pm_support(void) -+{ -+ init_retram_resume_resources(); -+ -+ stm32mp_pm_wipe_context(); -+ -+ return TEE_SUCCESS; -+} -+driver_init(init_pm_support); -diff --git a/core/arch/arm/plat-stm32mp1/pm/context.h b/core/arch/arm/plat-stm32mp1/pm/context.h -new file mode 100644 -index 000000000..a806d685b ---- /dev/null -+++ b/core/arch/arm/plat-stm32mp1/pm/context.h -@@ -0,0 +1,102 @@ -+/* SPDX-License-Identifier: BSD-3-Clause */ -+/* -+ * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved. -+ */ -+ -+#ifndef __STM32MP_PM_CONTEXT_H__ -+#define __STM32MP_PM_CONTEXT_H__ -+ -+#ifndef __ASSEMBLER__ -+#include -+#include -+#include -+#include -+#include -+#include -+#endif -+ -+#define PM_CTX_CCM_KEY_SIZE 32 -+#define PM_CTX_CCM_CTR1_SIZE 16 -+#define PM_CTX_CCM_B0_SIZE 16 -+#define PM_CTX_CCM_CTR0_SIZE 16 -+#define PM_CTX_CCM_TAG_SIZE 16 -+ -+#ifndef __ASSEMBLER__ -+/* -+ * All materials for the CCM sequence using CRYP support are preloaded -+ * in this specific structure. Note that the sequence does not use AAD. -+ * -+ * @key: AES key material buffer -+ * @ctr1: Preformatted 128bit CTR1 block -+ * @ctr1: Preformatted 128bit B0 block -+ * @ctr1: Preformatted 128bit CTR0 block -+ * @tag: Buffer where the generated CCM tag is stored -+ */ -+struct ccm_unpg_ctx { -+ uint32_t key[PM_CTX_CCM_KEY_SIZE / sizeof(uint32_t)]; -+ uint32_t ctr1[PM_CTX_CCM_CTR1_SIZE / sizeof(uint32_t)]; -+ uint32_t b0[PM_CTX_CCM_B0_SIZE / sizeof(uint32_t)]; -+ uint32_t ctr0[PM_CTX_CCM_CTR0_SIZE / sizeof(uint32_t)]; -+ uint32_t tag[PM_CTX_CCM_TAG_SIZE / sizeof(uint32_t)]; -+}; -+ -+/* -+ * This structure is used by pm_helpers.S at early resume from retention RAM. -+ * It is defined here and used by context_asm_defines.c to generate offset -+ * macros for the assembly implementation in pm_helpers.S. -+ * -+ * To lower the memory footprint of suspend sequence, The same function is -+ * used for encryption (executed from TEE RAM with MMU enabled) and for -+ * decryption (executed from BKPSRAM with MMU disabled). Therefore some -+ * required addresses are provided by the caller through this structure -+ * especially some SoC interface registers that are likely to have different -+ * physical and virtual addresses. -+ * -+ * @resume_pa: OP-TEE resume physical entry in TEE RAM (once restored) -+ * @teeram_bkp_pa: Physical base address in TEE RAM backup in DDR -+ * @cryp1_base: Base address of the CRYP1 registers (physical or virtual) -+ * @rcc_base: Base address of the RCC registers (physical or virtual) -+ * @stgen_base: Base address of the STGEN registers (physical or virtual) -+ * @stgen_cnt: STGEN cycle counter backup cell and measure of cycles spent -+ * @ccm_ref_tag: 128bit arrays storing tag generated during encryption -+ * @ccm_ctx: Structure storing CCM configuration and generated tag -+ * @resume_sequence: Code/data array for the BKPSRAM resume sequence -+ */ -+struct retram_resume_ctx { -+ uint32_t resume_pa; -+ uint32_t teeram_bkp_pa; -+ uint32_t cryp1_base; -+ uint32_t rcc_base; -+ uint32_t stgen_base; -+ uint32_t stgen_cnt; -+ uint8_t ccm_ref_tag[PM_CTX_CCM_TAG_SIZE]; -+ struct ccm_unpg_ctx ccm_ctx; -+ /* Last start the resume routine ARM (32bit) instructions sequence */ -+ uint32_t resume_sequence[]; -+}; -+ -+extern const uint8_t stm32mp_bkpsram_image_end[]; -+void stm32mp_bkpsram_resume(void); -+void stm32mp_sysram_resume(void); -+ -+void stm32mp_cpu_reset_state(void); -+ -+TEE_Result stm32mp_pm_save_context(unsigned int soc_mode); -+void stm32mp_pm_restore_context(unsigned int soc_mode); -+void stm32mp_pm_shutdown_context(void); -+void stm32mp_pm_wipe_context(void); -+ -+int stm32mp1_set_pm_domain_state(enum stm32mp1_pm_domain domain, bool status); -+ -+uint32_t stm32mp1_get_lp_soc_mode(uint32_t psci_mode); -+int stm32mp1_set_lp_deepest_soc_mode(uint32_t psci_mode, uint32_t soc_mode); -+ -+uintptr_t stm32mp_pm_retram_resume_ep(void); -+ -+int stm32mp_ccm_encrypt_teeram(struct retram_resume_ctx *ctx, -+ void *dst, void *src, size_t size); -+int stm32mp_ccm_decrypt_teeram(struct retram_resume_ctx *ctx, -+ void *dst, void *src, size_t size); -+#endif /*__ASSEMBLER__*/ -+ -+#endif /*__STM32MP_PM_CONTEXT_H__*/ -diff --git a/core/arch/arm/plat-stm32mp1/pm/context_asm_defines.c b/core/arch/arm/plat-stm32mp1/pm/context_asm_defines.c -new file mode 100644 -index 000000000..34c797f52 ---- /dev/null -+++ b/core/arch/arm/plat-stm32mp1/pm/context_asm_defines.c -@@ -0,0 +1,28 @@ -+// SPDX-License-Identifier: BSD-3-Clause -+/* -+ * Copyright (c) 2018, STMicroelectronics -+ * Copyright (c) 2018, Linaro Limited -+ */ -+ -+#include -+ -+#include "context.h" -+ -+#define OFFSET_OF_CTX_STRUCT(_f) offsetof(struct retram_resume_ctx, _f) -+#define OFFSET_OF_CMM_CTX_STRUCT(_f) (OFFSET_OF_CTX_STRUCT(ccm_ctx) + \ -+ offsetof(struct ccm_unpg_ctx, _f)) -+DEFINES -+{ -+ DEFINE(PM_CTX_RESUME_PA, OFFSET_OF_CTX_STRUCT(resume_pa)); -+ DEFINE(PM_CTX_TEERAM_BKP_PA, OFFSET_OF_CTX_STRUCT(teeram_bkp_pa)); -+ DEFINE(PM_CTX_CRYP1_BASE, OFFSET_OF_CTX_STRUCT(cryp1_base)); -+ DEFINE(PM_CTX_RCC_BASE, OFFSET_OF_CTX_STRUCT(rcc_base)); -+ DEFINE(PM_CTX_STGEN_BASE, OFFSET_OF_CTX_STRUCT(stgen_base)); -+ DEFINE(PM_CTX_STGEN_CNT, OFFSET_OF_CTX_STRUCT(stgen_cnt)); -+ DEFINE(PM_CTX_CCM_KEY, OFFSET_OF_CMM_CTX_STRUCT(key)); -+ DEFINE(PM_CTX_CCM_CTR1, OFFSET_OF_CMM_CTX_STRUCT(ctr1)); -+ DEFINE(PM_CTX_CCM_B0, OFFSET_OF_CMM_CTX_STRUCT(b0)); -+ DEFINE(PM_CTX_CCM_CTR0, OFFSET_OF_CMM_CTX_STRUCT(ctr0)); -+ DEFINE(PM_CTX_CCM_TAG, OFFSET_OF_CMM_CTX_STRUCT(tag)); -+ DEFINE(PM_CTX_CCM_REF_TAG, OFFSET_OF_CTX_STRUCT(ccm_ref_tag)); -+} -diff --git a/core/arch/arm/plat-stm32mp1/pm/low_power.c b/core/arch/arm/plat-stm32mp1/pm/low_power.c -new file mode 100644 -index 000000000..f06d8273d ---- /dev/null -+++ b/core/arch/arm/plat-stm32mp1/pm/low_power.c -@@ -0,0 +1,626 @@ -+// SPDX-License-Identifier: BSD-3-Clause -+/* -+ * Copyright (c) 2017-2020, STMicroelectronics - All Rights Reserved -+ */ -+ -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+ -+#include "context.h" -+#include "power.h" -+ -+#define TIMEOUT_US_1MS 1000 -+ -+#define PWRLP_TEMPO_5_HSI 5 -+ -+static uint8_t gicd_rcc_wakeup; -+static uint8_t gicc_pmr; -+ -+struct pwr_lp_config { -+ uint32_t pwr_cr1; -+ uint32_t pwr_mpucr; -+ const char *regul_suspend_node_name; -+}; -+ -+#define PWR_CR1_MASK (PWR_CR1_LPDS | PWR_CR1_LPCFG | PWR_CR1_LVDS) -+#define PWR_MPUCR_MASK (PWR_MPUCR_CSTDBYDIS | PWR_MPUCR_CSSF | PWR_MPUCR_PDDS) -+ -+static const struct pwr_lp_config config_pwr[STM32_PM_MAX_SOC_MODE] = { -+ [STM32_PM_CSLEEP_RUN] = { -+ .pwr_cr1 = 0U, -+ .pwr_mpucr = 0U, -+ .regul_suspend_node_name = NULL, -+ }, -+ [STM32_PM_CSTOP_ALLOW_STOP] = { -+ .pwr_cr1 = 0U, -+ .pwr_mpucr = PWR_MPUCR_CSTDBYDIS | PWR_MPUCR_CSSF, -+ .regul_suspend_node_name = NULL, -+ }, -+ [STM32_PM_CSTOP_ALLOW_LP_STOP] = { -+ .pwr_cr1 = PWR_CR1_LPDS, -+ .pwr_mpucr = PWR_MPUCR_CSTDBYDIS | PWR_MPUCR_CSSF, -+ .regul_suspend_node_name = "lp-stop", -+ }, -+ [STM32_PM_CSTOP_ALLOW_LPLV_STOP] = { -+ .pwr_cr1 = PWR_CR1_LVDS | PWR_CR1_LPDS | PWR_CR1_LPCFG, -+ .pwr_mpucr = PWR_MPUCR_CSTDBYDIS | PWR_MPUCR_CSSF, -+ .regul_suspend_node_name = "lplv-stop", -+ }, -+ [STM32_PM_CSTOP_ALLOW_STANDBY_DDR_SR] = { -+ .pwr_cr1 = 0U, -+ .pwr_mpucr = PWR_MPUCR_CSTDBYDIS | PWR_MPUCR_CSSF | -+ PWR_MPUCR_PDDS, -+ .regul_suspend_node_name = "standby-ddr-sr", -+ }, -+ [STM32_PM_CSTOP_ALLOW_STANDBY_DDR_OFF] = { -+ .pwr_cr1 = 0U, -+ .pwr_mpucr = PWR_MPUCR_CSTDBYDIS | PWR_MPUCR_CSSF | -+ PWR_MPUCR_PDDS, -+ .regul_suspend_node_name = "standby-ddr-off", -+ }, -+ [STM32_PM_SHUTDOWN] = { -+ .pwr_cr1 = 0U, -+ .pwr_mpucr = 0U, -+ .regul_suspend_node_name = "standby-ddr-off", -+ }, -+}; -+ -+static void set_rcc_it_priority(uint8_t *it_prio, uint8_t *pmr) -+{ -+ *it_prio = itr_set_ipriority(RCC_WAKEUP_IT, GIC_HIGHEST_SEC_PRIORITY); -+ *pmr = itr_set_pmr(STM32MP_GIC_PRIORITY_CSTOP); -+} -+ -+static void restore_rcc_it_priority(uint8_t it_prio, uint8_t pmr) -+{ -+ (void)itr_set_ipriority(RCC_WAKEUP_IT, it_prio); -+ (void)itr_set_pmr(pmr); -+} -+ -+static void stm32_apply_pmic_suspend_config(uint32_t mode) -+{ -+ const char *name = config_pwr[mode].regul_suspend_node_name; -+ -+ assert(mode < ARRAY_SIZE(config_pwr)); -+ -+ if (stm32mp_with_pmic() && name) { -+ stm32mp_get_pmic(); -+ stm32mp_pmic_apply_lp_config(name); -+ stm32mp_pmic_apply_boot_on_config(); -+ stm32mp_put_pmic(); -+ } -+} -+ -+#define CONSOLE_FLUSH_DELAY_MS 10 -+ -+#if TRACE_LEVEL >= TRACE_DEBUG -+static void wait_console_flushed(void) -+{ -+ console_flush(); -+ mdelay(CONSOLE_FLUSH_DELAY_MS); -+} -+#else -+static void wait_console_flushed(void) -+{ -+} -+#endif -+ -+static void cpu_wfi(void) -+{ -+ dsb(); -+ isb(); -+ wfi(); -+} -+ -+void stm32_pm_cpu_wfi(void) -+{ -+ wait_console_flushed(); -+ cpu_wfi(); -+} -+ -+/* If IWDG is not supported, provide a stubbed weak watchdog kicker */ -+void __weak stm32_iwdg_refresh(uint32_t __unused instance) -+{ -+} -+ -+#define ARM_CNTXCTL_IMASK BIT(1) -+ -+static void stm32mp_mask_timer(void) -+{ -+ /* Mask timer interrupts */ -+ write_cntp_ctl(read_cntp_ctl() | ARM_CNTXCTL_IMASK); -+ write_cntv_ctl(read_cntv_ctl() | ARM_CNTXCTL_IMASK); -+} -+ -+/* -+ * stm32_enter_cstop - Prepare CSTOP mode -+ * -+ * @mode - Target low power mode -+ */ -+void stm32_enter_cstop(uint32_t mode) -+{ -+ uint32_t pwr_cr1 = config_pwr[mode].pwr_cr1; -+ vaddr_t pwr_base = stm32_pwr_base(); -+ vaddr_t rcc_base = stm32_rcc_base(); -+ -+ stm32mp_syscfg_disable_io_compensation(); -+ -+ /* Save Self-Refresh (SR) mode and switch to Software SR mode */ -+ ddr_save_sr_mode(DDR_SSR_MODE); -+ -+ stm32_apply_pmic_suspend_config(mode); -+ -+ if (stm32mp_with_pmic() && (mode == STM32_PM_CSTOP_ALLOW_LP_STOP)) -+ pwr_cr1 |= PWR_CR1_LPCFG; -+ -+ /* Workaround for non secure cache issue: this should not be needed */ -+ dcache_op_all(DCACHE_OP_CLEAN_INV); -+ -+ /* Clear RCC interrupt before enabling it */ -+ io_setbits32(rcc_base + RCC_MP_CIFR, RCC_MP_CIFR_WKUPF); -+ -+ /* Enable RCC Wake-up */ -+ io_setbits32(rcc_base + RCC_MP_CIER, RCC_MP_CIFR_WKUPF); -+ -+ /* Configure low power mode */ -+ io_clrsetbits32(pwr_base + PWR_MPUCR_OFF, PWR_MPUCR_MASK, -+ config_pwr[mode].pwr_mpucr); -+ io_clrsetbits32(pwr_base + PWR_CR1_OFF, PWR_CR1_MASK, pwr_cr1); -+ -+ /* Clear RCC pending interrupt flags */ -+ io_write32(rcc_base + RCC_MP_CIFR, RCC_MP_CIFR_MASK); -+ -+ /* Request CSTOP mode to RCC */ -+ io_setbits32(rcc_base + RCC_MP_SREQSETR, -+ RCC_MP_SREQSETR_STPREQ_P0 | RCC_MP_SREQSETR_STPREQ_P1); -+ -+ stm32_iwdg_refresh(IWDG2_INST); -+ -+ set_rcc_it_priority(&gicd_rcc_wakeup, &gicc_pmr); -+ -+ if (ddr_standby_sr_entry() != 0) -+ panic(); -+ -+ if (mode == STM32_PM_CSTOP_ALLOW_STANDBY_DDR_SR) { -+ /* Keep retention and backup RAM content in standby */ -+ io_setbits32(pwr_base + PWR_CR2_OFF, PWR_CR2_BREN | -+ PWR_CR2_RREN); -+ -+ while ((io_read32(pwr_base + PWR_CR2_OFF) & -+ (PWR_CR2_BRRDY | PWR_CR2_RRRDY)) == 0U) -+ ; -+ } -+} -+ -+/* -+ * stm32_exit_cstop - Exit from CSTOP mode -+ */ -+void stm32_exit_cstop(void) -+{ -+ vaddr_t rcc_base = stm32_rcc_base(); -+ -+ if (ddr_standby_sr_exit()) -+ panic(); -+ -+ /* Restore Self-Refresh mode saved in stm32_enter_cstop() */ -+ ddr_restore_sr_mode(); -+ -+ restore_rcc_it_priority(gicd_rcc_wakeup, gicc_pmr); -+ -+ /* Disable STOP request */ -+ io_setbits32(rcc_base + RCC_MP_SREQCLRR, -+ RCC_MP_SREQSETR_STPREQ_P0 | RCC_MP_SREQSETR_STPREQ_P1); -+ -+ /* Disable RCC Wake-up */ -+ io_clrbits32(rcc_base + RCC_MP_CIER, RCC_MP_CIFR_WKUPF); -+ -+ dsb(); -+ isb(); -+ -+ /* Disable retention and backup RAM content after stop */ -+ io_clrbits32(stm32_pwr_base() + PWR_CR2_OFF, PWR_CR2_BREN | PWR_CR2_RREN); -+ -+ stm32mp_syscfg_enable_io_compensation(); -+} -+ -+/* -+ * GIC support required in low power sequences and reset sequences -+ */ -+#define GICC_IAR 0x00C -+#define GICC_IT_ID_MASK 0x3ff -+#define GICC_EOIR 0x010 -+#define GICC_HPPIR 0x018 -+#define GICC_AHPPIR 0x028 -+#define GIC_PENDING_G1_INTID 1022U -+#define GIC_SPURIOUS_INTERRUPT 1023U -+#define GIC_NUM_INTS_PER_REG 32 -+#define GIC_MAX_SPI_ID 1020 -+#define GICD_ICENABLER(n) (0x180 + (n) * 4) -+ -+static void clear_pending_interrupts(void) -+{ -+ uint32_t id = 0; -+ vaddr_t gicc_base = get_gicc_base(); -+ vaddr_t gicd_base = get_gicd_base(); -+ -+ do { -+ id = io_read32(gicc_base + GICC_HPPIR) & GICC_IT_ID_MASK; -+ -+ /* -+ * Find out which interrupt it is under the -+ * assumption that the GICC_CTLR.AckCtl bit is 0. -+ */ -+ if (id == GIC_PENDING_G1_INTID) -+ id = io_read32(gicc_base + GICC_AHPPIR) & GICC_IT_ID_MASK; -+ -+ if (id < GIC_MAX_SPI_ID) { -+ size_t idx = id / GIC_NUM_INTS_PER_REG; -+ uint32_t mask = 1 << (id % GIC_NUM_INTS_PER_REG); -+ -+ io_write32(gicc_base + GICC_EOIR, id); -+ -+ io_write32(gicd_base + GICD_ICENABLER(idx), mask); -+ -+ dsb_ishst(); -+ } -+ } while (id < GIC_MAX_SPI_ID); -+} -+ -+void stm32mp_gic_set_end_of_interrupt(uint32_t it) -+{ -+ vaddr_t gicc_base = get_gicc_base(); -+ -+ io_write32(gicc_base + GICC_EOIR, it); -+} -+ -+static void __noreturn wait_cpu_reset(void) -+{ -+#ifdef STM32MP1_USE_MPU0_RESET -+ dcache_op_all(DCACHE_OP_CLEAN_INV); -+ write_sctlr(read_sctlr() & ~SCTLR_C); -+ dcache_op_all(DCACHE_OP_CLEAN_INV); -+ __asm__("clrex"); -+ -+ dsb(); -+ isb(); -+#else -+ psci_armv7_cpu_off(); -+#endif -+ -+ for ( ; ; ) { -+ clear_pending_interrupts(); -+ wfi(); -+ } -+} -+ -+#ifdef STM32MP1_USE_MPU0_RESET -+/* -+ * tzc_source_ip contains the TZC transaction source IPs that need to be reset -+ * before a C-A7 subsystem is reset (i.e. independent reset): -+ * - C-A7 subsystem is reset separately later in the sequence, -+ * - C-M4 subsystem is not concerned here, -+ * - DAP is excluded for debug purpose, -+ * - IPs are stored with their ETZPC IDs (STM32MP1_ETZPC_MAX_ID if not -+ * applicable) because some of them need to be reset only if they are not -+ * configured in MCU isolation mode inside ETZPC device tree. -+ */ -+struct tzc_source_ip { -+ uint16_t reset_id; -+ uint16_t clock_id; -+ uint32_t decprot_id; -+}; -+ -+#define _TZC_FIXED(res, clk) \ -+ { \ -+ .reset_id = (res), \ -+ .clock_id = (clk), \ -+ .decprot_id = STM32MP1_ETZPC_MAX_ID, \ -+ } -+ -+#define _TZC_COND(res, clk, decprot) \ -+ { \ -+ .reset_id = (res), \ -+ .clock_id = (clk), \ -+ .decprot_id = (decprot), \ -+ } -+ -+static const struct tzc_source_ip __maybe_unused tzc_source_ip[] = { -+ _TZC_FIXED(LTDC_R, LTDC_PX), -+ _TZC_FIXED(GPU_R, GPU), -+ _TZC_FIXED(USBH_R, USBH), -+ _TZC_FIXED(SDMMC1_R, SDMMC1_K), -+ _TZC_FIXED(SDMMC2_R, SDMMC2_K), -+ _TZC_FIXED(MDMA_R, MDMA), -+ _TZC_COND(USBO_R, USBO_K, STM32MP1_ETZPC_OTG_ID), -+ _TZC_COND(SDMMC3_R, SDMMC3_K, STM32MP1_ETZPC_SDMMC3_ID), -+ _TZC_COND(ETHMAC_R, ETHMAC, STM32MP1_ETZPC_ETH_ID), -+ _TZC_COND(DMA1_R, DMA1, STM32MP1_ETZPC_DMA1_ID), -+ _TZC_COND(DMA2_R, DMA2, STM32MP1_ETZPC_DMA2_ID), -+}; -+ -+static void reset_peripherals(void) -+{ -+ vaddr_t rcc_base = stm32_rcc_base(); -+ size_t __maybe_unused id = 0; -+ -+ for (id = 0U; id < ARRAY_SIZE(tzc_source_ip); id++) { -+ const struct tzc_source_ip *tzc = &tzc_source_ip[id]; -+ -+ if (!stm32_clock_is_enabled(tzc->clock_id) || -+ ((tzc->decprot_id != STM32MP1_ETZPC_MAX_ID) && -+ (etzpc_get_decprot(tzc->decprot_id) == -+ ETZPC_DECPROT_MCU_ISOLATION))) -+ continue; -+ -+ if (tzc->reset_id != GPU_R) { -+ stm32_reset_assert(tzc->reset_id, TIMEOUT_US_1MS); -+ stm32_reset_deassert(tzc->reset_id, TIMEOUT_US_1MS); -+ } else { -+ /* GPU reset automatically cleared by hardware */ -+ io_setbits32(rcc_base + RCC_AHB6RSTSETR, -+ RCC_AHB6RSTSETR_GPURST); -+ } -+ } -+} -+#endif /* STM32MP1_USE_MPU0_RESET */ -+ -+static void __noreturn reset_cores(void) -+{ -+ vaddr_t rcc_base = stm32_rcc_base(); -+ uint32_t reset_mask = RCC_MP_GRSTCSETR_MPUP0RST | -+ RCC_MP_GRSTCSETR_MPUP1RST; -+ uint32_t target_mask = 0; -+ -+ /* Mask timer interrupts */ -+ stm32mp_mask_timer(); -+ -+#ifdef STM32MP1_USE_MPU0_RESET -+ reset_peripherals(); -+#endif -+ -+ if (get_core_pos() == 0) -+ target_mask = TARGET_CPU1_GIC_MASK; -+ else -+ target_mask = TARGET_CPU0_GIC_MASK; -+ -+ itr_raise_sgi(GIC_SEC_SGI_1, target_mask); -+ -+ clear_pending_interrupts(); -+ -+ io_write32(rcc_base + RCC_MP_GRSTCSETR, reset_mask); -+ -+ wait_cpu_reset(); -+} -+ -+/* -+ * stm32_pm_cpus_reset - Reset only cpus -+ */ -+void __noreturn stm32_cores_reset(void) -+{ -+ reset_cores(); -+} -+DECLARE_KEEP_PAGER(stm32_cores_reset); -+ -+static __maybe_unused void reset_other_core(void) -+{ -+ vaddr_t rcc_base = stm32_rcc_base(); -+ uint32_t reset_mask = 0; -+ uint32_t target_mask = 0; -+ -+ if (get_core_pos() == 0) { -+ reset_mask = RCC_MP_GRSTCSETR_MPUP1RST; -+ target_mask = TARGET_CPU1_GIC_MASK; -+ } else { -+ reset_mask = RCC_MP_GRSTCSETR_MPUP0RST; -+ target_mask = TARGET_CPU0_GIC_MASK; -+ } -+ -+ itr_raise_sgi(GIC_SEC_SGI_1, target_mask); -+ -+ io_write32(rcc_base + RCC_MP_GRSTCSETR, reset_mask); -+} -+ -+/* -+ * stm32_enter_cstop_shutdown - Shutdown CPUs to target low power mode -+ * @mode - Target low power mode -+ */ -+void __noreturn stm32_enter_cstop_shutdown(uint32_t mode) -+{ -+ switch (mode) { -+ case STM32_PM_SHUTDOWN: -+ if (stm32mp_with_pmic()) { -+ wait_console_flushed(); -+ stm32mp_get_pmic(); -+ stpmic1_switch_off(); -+ udelay(100); -+ } -+ break; -+ case STM32_PM_CSTOP_ALLOW_STANDBY_DDR_SR: -+ case STM32_PM_CSTOP_ALLOW_STANDBY_DDR_OFF: -+#ifdef STM32MP1_USE_MPU0_RESET -+ stm32mp_pm_shutdown_context(); -+ reset_other_core(); -+ stm32_enter_cstop(mode); -+ dsb(); -+ isb(); -+ for ( ; ; ) -+ wfi(); -+#else -+ if (stm32mp_with_pmic()) { -+ wait_console_flushed(); -+ stm32mp_get_pmic(); -+ stpmic1_switch_off(); -+ udelay(100); -+ } -+#endif -+ break; -+ default: -+ break; -+ } -+ -+ panic(); -+} -+ -+/* -+ * stm32_enter_cstop_reset - Reset CPUs to target low power mode -+ * @mode - Target low power mode -+ */ -+void __noreturn stm32_enter_cstop_reset(uint32_t mode) -+{ -+ vaddr_t rcc = stm32_rcc_base(); -+ -+ switch (mode) { -+ case STM32_PM_SHUTDOWN: -+ io_write32(rcc + RCC_MP_GRSTCSETR, RCC_MP_GRSTCSETR_MPSYSRST); -+ udelay(100); -+ break; -+ default: -+ IMSG("Forced system reset"); -+ wait_console_flushed(); -+ io_write32(rcc + RCC_MP_GRSTCSETR, RCC_MP_GRSTCSETR_MPSYSRST); -+ udelay(100); -+ break; -+ } -+ -+ panic(); -+} -+ -+/* -+ * stm32_enter_csleep - enter CSLEEP state while WFI and exit in CRUN -+ * -+ * Configure PWR for CSLEEP state. CPU shall execute a WFI and return -+ * once a interrupt is pending. -+ */ -+void stm32_enter_csleep(void) -+{ -+ vaddr_t pwr_base = stm32_pwr_base(); -+ -+ io_clrsetbits32(pwr_base + PWR_MPUCR_OFF, PWR_MPUCR_MASK, -+ config_pwr[STM32_PM_CSLEEP_RUN].pwr_mpucr); -+ io_clrsetbits32(pwr_base + PWR_CR1_OFF, PWR_CR1_MASK, -+ config_pwr[STM32_PM_CSLEEP_RUN].pwr_cr1); -+ -+ stm32_pm_cpu_wfi(); -+} -+ -+/* RCC Wakeup interrupt is used to wake from suspeneded mode */ -+static enum itr_return rcc_wakeup_it_handler(struct itr_handler *hdl __unused) -+{ -+ /* This interrupt is not expected to be handled */ -+ panic("RCC wakeup interrupt"); -+ return ITRR_HANDLED; -+} -+ -+static struct itr_handler rcc_wakeup_handler = { -+ .it = RCC_WAKEUP_IT, -+ .handler = rcc_wakeup_it_handler, -+}; -+DECLARE_KEEP_PAGER(rcc_wakeup_handler); -+ -+/* SGI9 (secure SGI 1) informs targeted CPU it shall reset */ -+static enum itr_return sgi9_it_handler(struct itr_handler *handler) -+{ -+ stm32mp_mask_timer(); -+ -+ stm32mp_gic_set_end_of_interrupt(handler->it); -+ -+ clear_pending_interrupts(); -+ -+ wait_cpu_reset(); -+ -+ panic("Core reset"); -+ -+ return ITRR_HANDLED; -+} -+ -+static struct itr_handler sgi9_reset_handler = { -+ .it = GIC_SEC_SGI_1, -+ .handler = sgi9_it_handler, -+}; -+DECLARE_KEEP_PAGER(sgi9_reset_handler); -+ -+static TEE_Result init_low_power(void) -+{ -+ vaddr_t pwr_base = stm32_pwr_base(); -+ vaddr_t rcc_base = stm32_rcc_base(); -+ -+ itr_add(&rcc_wakeup_handler); -+ itr_enable(rcc_wakeup_handler.it); -+ -+ itr_add(&sgi9_reset_handler); -+ itr_enable(sgi9_reset_handler.it); -+ -+ /* Enable retention for BKPSRAM and BKPREG */ -+ io_mask32(pwr_base + PWR_CR2_OFF, -+ PWR_CR2_BREN | PWR_CR2_RREN, PWR_CR2_BREN | PWR_CR2_RREN); -+ -+ /* -+ * Configure Standby mode available for MCU by default -+ * and allow to switch in standby SoC in all case -+ */ -+ io_setbits32(pwr_base + PWR_MCUCR_OFF, PWR_MCUCR_PDDS); -+ -+ /* Disable STOP request */ -+ io_setbits32(rcc_base + RCC_MP_SREQCLRR, -+ RCC_MP_SREQSETR_STPREQ_P0 | RCC_MP_SREQSETR_STPREQ_P1); -+ -+ /* Wait 5 HSI periods before re-enabling PLLs after STOP modes */ -+ io_clrsetbits32(rcc_base + RCC_PWRLPDLYCR, RCC_PWRLPDLYCR_PWRLP_DLY_MASK, -+ PWRLP_TEMPO_5_HSI); -+ -+ return TEE_SUCCESS; -+} -+service_init(init_low_power); -+ -+/* -+ * CPU low power sequences -+ */ -+void __noreturn stm32_pm_cpu_power_down_wfi(void) -+{ -+ vaddr_t rcc_base = stm32_rcc_base(); -+ -+ if (get_core_pos() == 0) { -+ void (*reset_ep)(void) = stm32mp_sysram_resume; -+ -+ stm32_pm_cpu_wfi(); -+ -+ /* STANDBY not reached: resume from retained SYSRAM */ -+ stm32_exit_cstop(); -+ stm32mp_cpu_reset_state(); -+ reset_ep(); -+ panic(); -+ } -+ -+ dcache_op_level1(DCACHE_OP_CLEAN); -+ io_write32(rcc_base + RCC_MP_GRSTCSETR, RCC_MP_GRSTCSETR_MPUP1RST); -+ cpu_wfi(); -+ panic(); -+} -diff --git a/core/arch/arm/plat-stm32mp1/pm/pm_helpers.S b/core/arch/arm/plat-stm32mp1/pm/pm_helpers.S -new file mode 100644 -index 000000000..63142a08e ---- /dev/null -+++ b/core/arch/arm/plat-stm32mp1/pm/pm_helpers.S -@@ -0,0 +1,729 @@ -+/* SPDX-License-Identifier: BSD-2-Clause */ -+/* -+ * Copyright (c) 2018, STMicroelectronics -+ * Copyright (c) 2017 NXP -+ * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved. -+ */ -+ -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+ -+#include "context.h" -+ -+/* -+ * Right bit shift distance to reach timeout from a 1s STGEN freq count -+ * Value N relates to 1000ms / 2^N, i.e 7 relates to 7.8125ms=~8ms -+ */ -+#define CCM_TIMEOUT_128MS 2 -+#define CCM_TIMEOUT_8MS 7 -+#define CCM_TIMEOUT_1MS 10 -+#define CCM_TIMEOUT_16US 16 -+#define CCM_TIMEOUT CCM_TIMEOUT_8MS -+ -+/* -+ * CRYP interface register used for AES CCM -+ */ -+#define CRYP_CR 0x000 -+#define CRYP_SR 0x004 -+#define CRYP_DIN 0x008 -+#define CRYP_DOUT 0x00c -+#define CRYP_KEYR_BASE 0x020 -+#define CRYP_IVR_BASE 0x040 -+ -+#define CRYP_CR_ALGODIR_DECRYPT BIT(2) -+#define CRYP_CR_ALGOMODE_MASK (BIT(19) | GENMASK_32(5, 3)) -+#define CRYP_CR_ALGOMODE(m) (((m & BIT(3)) << 16) | (m & 0x7) << 3) -+#define ALGOMODE_AES_CCM 0x9 -+#define CRYP_CR_DATATYPE_SHIFT 6 -+#define CRYP_CR_DATATYPE_8BIT (2 << CRYP_CR_DATATYPE_SHIFT) -+#define CRYP_CR_KEYSIZE_SHIFT 8 -+#define CRYP_CR_KEYSIZE_256BIT (2U << CRYP_CR_KEYSIZE_SHIFT) -+#define CRYP_CR_CRYPEN BIT(15) -+#define CRYP_CR_FFLUSH BIT(14) -+#define CRYP_CR_GCM_CCMPH_SHIFT 16 -+#define CRYP_CR_PHASE_MASK (0x3 << CRYP_CR_GCM_CCMPH_SHIFT) -+#define CRYP_CR_INIT_PHASE (0 << CRYP_CR_GCM_CCMPH_SHIFT) -+#define CRYP_CR_HEADER_PHASE (1 << CRYP_CR_GCM_CCMPH_SHIFT) -+#define CRYP_CR_PAYLOAD_PHASE (2 << CRYP_CR_GCM_CCMPH_SHIFT) -+#define CRYP_CR_FINAL_PHASE (3 << CRYP_CR_GCM_CCMPH_SHIFT) -+ -+#define CRYP_SR_BUSY BIT(4) -+#define CRYP_SR_OFFU BIT(3) -+#define CRYP_SR_OFNE BIT(2) -+#define CRYP_SR_IFNF BIT(1) -+#define CRYP_SR_IFEM BIT(0) -+ -+/* -+ * Enable TRACE_SYSRAM_RESTORE to get some UART console traces -+ * at resume time. -+ */ -+#if defined(TRACE_SYSRAM_RESTORE) -+ -+#define UART_BASE UART4_BASE -+#define UART_ISR_OFF 0x1c -+#define UART_TDR_OFF 0x28 -+#define USART_ISR_TXE_TXFNF (1<< 7) -+ -+ .macro PRINT_CHAR _reg0, _reg1, _char -+ /* Trace only at resume when MMU is OFF */ -+ read_sctlr \_reg0 -+ ands \_reg0, #SCTLR_M -+ 101: -+ bne 102f -+ mov_imm \_reg0, UART4_BASE -+ ldr \_reg1, [\_reg0, #UART_ISR_OFF] -+ ands \_reg1, #USART_ISR_TXE_TXFNF -+ beq 101b -+ mov_imm \_reg1, (\_char) -+ str \_reg1, [\_reg0, #UART_TDR_OFF] -+ 102: -+ .endm -+#else -+ .macro PRINT_CHAR _reg0, _reg1, _char -+ .endm -+#endif -+ -+ /* Bound of the binary image loaded in retained memory */ -+ .global stm32mp_bkpsram_image_end -+ -+/* -+ * stm32mp_bkpsram_resume - Restore TEE RAM from backup memory and resume into -+ * -+ * This function executes at early resume from suspend state. It is the -+ * entrypoint of the OP-TEE provided to early boot stage when SoC wakes. -+ * This code is located in a retained memory, MMU disabled. This function -+ * shall restore TEE RAM content for OP-TEE to resume execution. Once -+ * TEE RAM is restored, this function branches to the resident resume entry -+ * point in TEE_RAM. This function and its resources shall execute in place. -+ */ -+FUNC stm32mp_bkpsram_resume , : -+UNWIND( .cantunwind) -+ -+ PRINT_CHAR r0, r1, '0' -+ -+ /* -+ * Almost all sequences here expect PM context structure base address -+ * from CPU register r11. -+ */ -+ mov_imm r11, (BKPSRAM_BASE + BKPSRAM_PM_CONTEXT_OFFSET) -+ -+ /* stm32mp_ccm_teeram needs some HW interface base addresss */ -+ mov_imm r0, CRYP1_BASE -+ str r0, [r11, #PM_CTX_CRYP1_BASE] -+ mov_imm r0, RCC_BASE -+ str r0, [r11, #PM_CTX_RCC_BASE] -+ mov_imm r0, STGEN_BASE -+ str r0, [r11, #PM_CTX_STGEN_BASE] -+ -+ bl _clear_early_mailbox -+ bl _prepare_time -+ -+ mov_imm r0, TEE_RAM_START -+ ldr r1, [r11, #PM_CTX_TEERAM_BKP_PA] -+ mov_imm r2, TEE_RAM_PH_SIZE -+ mov_imm r3, 1 -+ bl stm32mp_ccm_teeram -+ cmp r0, #0 -+ bne _failed -+ -+ PRINT_CHAR r0, r1, 'T' -+ PRINT_CHAR r0, r1, 'a' -+ PRINT_CHAR r0, r1, 'g' -+ PRINT_CHAR r0, r1, '\n' -+ -+ /* Compare the generated and reference tags */ -+ add r8, r11, #PM_CTX_CCM_TAG -+ add r9, r11, #PM_CTX_CCM_REF_TAG -+ ldm r8, {r2-r5} -+ ldm r9, {r6-r9} -+ mov r0, #0 -+ cmp r2, r6 -+ addeq r0, #1 -+ cmp r3, r7 -+ addeq r0, #1 -+ cmp r4, r8 -+ addeq r0, #1 -+ cmp r5, r9 -+ addeq r0, #1 -+ cmp r0, #4 -+ bne _failed -+ bl _save_resume_time -+ -+ PRINT_CHAR r1, r2, 'O' -+ PRINT_CHAR r1, r2, 'k' -+ PRINT_CHAR r1, r2, '\n' -+ -+ /* Resume into the restored TEE RAM */ -+ ldr r1, [r11, #PM_CTX_RESUME_PA] -+ bx r1 -+ -+_failed: -+ PRINT_CHAR r0, r12, 'F' -+ PRINT_CHAR r0, r12, '\n' -+ -+ /* Clear context including key and reference tag */ -+ mov r0, #0xa5 -+ mov_imm r12, BKPSRAM_PM_CONTEXT_SIZE -+ add r12, r11, r12 -+1: str r0, [r11], #4 -+ cmp r11, r12 -+ blt 1b -+ b . -+ -+ /* -+ * _clear_early_mailbox - Wipe mailbox in case of reset -+ * -+ * Sratches r0-r4. -+ * All other CPU registers are preserved. -+ */ -+_clear_early_mailbox: -+ /* Clear the backup registers (first enable RTCAPB clock) */ -+ mov_imm r0, (RCC_BASE + RCC_MP_APB5ENSETR) -+ mov_imm r2, RCC_MP_APB5ENSETR_RTCAPBEN -+ ldr r1, [r0] -+ ands r1, r1, r2 -+ moveq r1, r2 -+ movne r1, #0 -+ str r2, [r0] -+ mov_imm r2, (TAMP_BASE + TAMP_BKP_REGISTER_OFF) -+ mov_imm r3, (BCKR_CORE1_MAGIC_NUMBER * 4) -+ mov_imm r4, BOOT_API_A7_RESET_MAGIC_NUMBER -+ str r4, [r2, r3] -+ mov_imm r3, (BCKR_CORE1_BRANCH_ADDRESS * 4) -+ mov r4, #0 -+ str r4, [r2, r3] -+ /* Restore RTCAPB clock initial state */ -+ str r1, [r0, #RCC_MP_ENCLRR_OFFSET] -+ bx lr -+ -+ /* -+ * prepare_time - save/reset cycle counter to prevent later overflow -+ * -+ * Save current 32bit lower counter and reset to 0 so that later -+ * timeout test do not need to care about overflow. -+ * -+ * Expects r11 is context base and lr is return address. -+ * Scrathes r0-r2. -+ * All other CPU registers are preserved. -+ */ -+_prepare_time: -+ ldr r2, [r11, #PM_CTX_STGEN_BASE] -+ /* Disable STGEN counter */ -+ ldr r1, [r2, #CNTCR_OFFSET] -+ bic r1, r1, #CNTCR_EN -+ str r1, [r2, #CNTCR_OFFSET] -+1: ldr r1, [r2, #CNTSR_OFFSET] -+ tst r1, #CNTCR_EN -+ bne 1b -+ /* Save and reset STGEN counter */ -+ ldr r0, [r2, #CNTCVL_OFFSET] -+ str r0, [r11, #PM_CTX_STGEN_CNT] -+ mov r0, #0 -+ str r0, [r2, #CNTCVL_OFFSET] -+ ldr r0, [r2, #CNTCVU_OFFSET] -+ str r0, [r2, #CNTCVU_OFFSET] -+ /* Enable STGEN counter */ -+ ldr r1, [r2, #CNTCR_OFFSET] -+ orr r1, r1, #CNTCR_EN -+ str r1, [r2, #CNTCR_OFFSET] -+ bx lr -+ -+ /* -+ * save_resume_time - save time spent and restore STGEN cycle counter -+ * -+ * Restore STGEN counter to initial value incremented by the current -+ * count. Note 32bit upper may need to be incremented. -+ * -+ * Expects r11 is context base and lr is return address. -+ * Scrathes r0-r3. -+ * All other CPU registers are preserved. -+ */ -+_save_resume_time: -+ /* Compute update STGEN counter 32bit LSB value */ -+ ldr r2, [r11, #PM_CTX_STGEN_BASE] -+ ldr r0, [r11, #PM_CTX_STGEN_CNT] -+ ldr r3, [r2, #CNTCVL_OFFSET] -+ str r3, [r11, #PM_CTX_STGEN_CNT] -+ adds r0, r0, r3 -+ /* Disable STGEN */ -+ ldr r1, [r2, #CNTCR_OFFSET] -+ bic r1, r1, #CNTCR_EN -+ str r1, [r2, #CNTCR_OFFSET] -+1: ldr r1, [r2, #CNTSR_OFFSET] -+ tst r1, #CNTCR_EN -+ bne 1b -+ /* Update counter (increment 32bit MSB if requried) */ -+ str r0, [r2, #CNTCVL_OFFSET] -+ ldr r0, [r2, #CNTCVU_OFFSET] -+ addcs r0, r0, #1 -+ str r0, [r2, #CNTCVU_OFFSET] /* Write CNTCVU value ... */ -+ ldr r0, [r2, #CNTCVU_OFFSET] /* ... and wait it is set */ -+ /* Enable STGEN */ -+ ldr r0, [r2, #CNTCR_OFFSET] -+ orr r0, r0, #CNTCR_EN -+ str r0, [r2, #CNTCR_OFFSET] -+ bx lr -+ -+ /* -+ * _setup_cryp1 - Enable CRYP1 hardware: reset & clock -+ * _reset_cryp1 - Reset CRYP1 hardware -+ * -+ * Function call before and after CCM sequence. Note that the CRYP1 -+ * clock remain enabled. It is disabled later by the resume sequence. -+ * -+ * Expects r11 is context base and lr is return address. -+ * Scratches r0-r3. -+ */ -+_setup_cryp1: -+ ldr r1, [r11, #PM_CTX_RCC_BASE] -+ mov_imm r0, RCC_MP_AHB5ENSETR_CRYP1EN -+ str r0, [r1, #RCC_MP_AHB5ENSETR] -+ /* Intentionnally fall through reset_cryp1 */ -+_reset_cryp1: -+ ldr r3, [r11, #PM_CTX_RCC_BASE] -+ mov_imm r0, RCC_AHB5RSTSETR_CRYP1RST -+ str r0, [r3, #RCC_AHB5RSTSETR] -+1: ldr r1, [r3, #RCC_AHB5RSTSETR] -+ ands r1, r1, r0 -+ beq 1b -+ mov_imm r0, RCC_AHB5RSTSETR_CRYP1RST -+ str r0, [r3, #RCC_AHB5RSTCLRR] -+1: ldr r1, [r3, #RCC_AHB5RSTSETR] -+ ands r1, r1, r0 -+ bne 1b -+ bx lr -+ -+ /* -+ * _ccm_arm_8ms_timeout - Init 8ms threshold for _ccm_failed_on_timeout -+ * _ccm_fail_on_timeout - Check STGEN counter against timeout threshold -+ * -+ * These function are used by the macro wait_flag_timeout_8ms. The -+ * former loads the timeout in CPU register r0 while the later get the -+ * timeout counter threshold from CPU register r0. -+ * -+ * Expect r11 is context base and lr is return address. -+ * Scratch r0-r1. -+ * All other CPU registers are preserved. -+ */ -+_ccm_arm_8ms_timeout: -+ ldr r1, [r11, #PM_CTX_STGEN_BASE] -+ ldr r0, [r1, #CNTFID_OFFSET] -+ lsrs r0, r0, #CCM_TIMEOUT -+ moveq r0, #1 -+ ldr r1, [r1, #CNTCVL_OFFSET] -+ adds r0, r0, r1 -+ bcs _ccm_failed_on_timeout -+ bx lr -+ -+_ccm_fail_on_timeout: -+ -+ ldr r1, [r11, #PM_CTX_STGEN_BASE] -+ ldr r1, [r1, #CNTCVL_OFFSET] -+ cmp r1, r0 -+ bge _ccm_failed_on_timeout -+ bx lr -+ -+_ccm_failed_on_timeout: -+ PRINT_CHAR r0, r1, 'T' -+ PRINT_CHAR r0, r1, 'o' -+ PRINT_CHAR r0, r1, '\n' -+ b _ccm_failed -+ -+ /* -+ * Macro WAIT_FLAG_TIMEOUT compares timeout threshold (r0) with -+ * current time and branches the CCM failure entry on timeout. -+ * It is assumed the 32bit timestamps cannot overflow. -+ */ -+ .macro WAIT_FLAG_TIMEOUT register_offset, bit_mask, awaited_mask -+ bl _ccm_arm_8ms_timeout -+ 1: -+ bl _ccm_fail_on_timeout -+ ldr r1, [r10, #(\register_offset)] -+ and r1, r1, #(\bit_mask) -+ cmp r1, #(\awaited_mask) -+ bne 1b -+ .endm -+ -+/* -+ * stm32mp_ccm_teeram - Size optimzed unpaged CCM encryption/decryption -+ * -+ * This sequence encrypts or decrypts a input block using AES CCM with a -+ * 256bit key and no AAD and generates the CCM tag. The key, CTR0, CTR1 -+ * and B0 block are read from PM context structure. The generated tag is -+ * stored in the PM context structure. -+ * -+ * This function is executed from TEE RAM during suspend sequence to generate -+ * the encrypted data and the tag. This function is also executed from BKPSRAM -+ * called with MMU disabled. Therefore this sequence shall be comply with -+ * position independent code constraints. -+ * -+ * Expects at entry: -+ * lr = caller return address -+ * r11 = retram_resume_ctx structure base address -+ * r0 = Destination buffer for the output data (ciphertext or plaintext) -+ * r1 = Source buffer for the input data (plaintext or ciphertext) -+ * r2 = Input (and output) data size in bytes -+ * r3 = 1 if decrypting, 0 if encrypting -+ */ -+stm32mp_ccm_teeram: -+ /* -+ * Use of the CPU registers in the whole stm32mp_ccm_teeram sequence -+ * -+ * sp: preserved, not used -+ * lr: scratch register used to call subroutines. -+ * r12: saves the caller link register for final return -+ * r11: context from BKPSRAM -+ * r10: CRYP1 base address -+ * r9: destination buffer -+ * r8: source buffer to cipher -+ * r7: data byte counter -+ * r0-r6 are scratch registers -+ */ -+ mov r12, lr -+ ldr r10, [r11, #PM_CTX_CRYP1_BASE] -+ mov r9, r0 -+ mov r8, r1 -+ mov r7, r2 -+ mov r6, r3 -+ -+ PRINT_CHAR r0, r1, '1' -+ -+ bl _setup_cryp1 -+ -+ PRINT_CHAR r0, r1, '2' -+ -+ mov_imm r0, (CRYP_CR_ALGOMODE(ALGOMODE_AES_CCM) | \ -+ CRYP_CR_DATATYPE_8BIT | CRYP_CR_FFLUSH | \ -+ CRYP_CR_KEYSIZE_256BIT) -+ cmp r6, #0 -+ orrne r0, r0, #CRYP_CR_ALGODIR_DECRYPT -+ str r0, [r10, #CRYP_CR] -+ -+ PRINT_CHAR r0, r1, '3' -+ -+ /* Check data alignment (addresses and size) */ -+ ands r0, r7, #0x0F -+ bne _ccm_failed -+ ands r0, r8, #0x03 -+ bne _ccm_failed -+ ands r0, r9, #0x03 -+ bne _ccm_failed -+ -+ PRINT_CHAR r0, r1, '4' -+ -+ ldr r0, [r11, #PM_CTX_CCM_KEY] -+ str r0, [r10, #CRYP_KEYR_BASE] -+ ldr r0, [r11, #(PM_CTX_CCM_KEY + 4)] -+ str r0, [r10, #(CRYP_KEYR_BASE + 4)] -+ ldr r0, [r11, #(PM_CTX_CCM_KEY + 8)] -+ str r0, [r10, #(CRYP_KEYR_BASE + 8)] -+ ldr r0, [r11, #(PM_CTX_CCM_KEY + 12)] -+ str r0, [r10, #(CRYP_KEYR_BASE + 12)] -+ ldr r0, [r11, #(PM_CTX_CCM_KEY + 16)] -+ str r0, [r10, #(CRYP_KEYR_BASE + 16)] -+ ldr r0, [r11, #(PM_CTX_CCM_KEY + 20)] -+ str r0, [r10, #(CRYP_KEYR_BASE + 20)] -+ ldr r0, [r11, #(PM_CTX_CCM_KEY + 24)] -+ str r0, [r10, #(CRYP_KEYR_BASE + 24)] -+ ldr r0, [r11, #(PM_CTX_CCM_KEY + 28)] -+ str r0, [r10, #(CRYP_KEYR_BASE + 28)] -+ -+ ldr r0, [r11, #PM_CTX_CCM_CTR1] -+ str r0, [r10, #CRYP_IVR_BASE] -+ ldr r0, [r11, #(PM_CTX_CCM_CTR1 + 4)] -+ str r0, [r10, #(CRYP_IVR_BASE + 4)] -+ ldr r0, [r11, #(PM_CTX_CCM_CTR1 + 8)] -+ str r0, [r10, #(CRYP_IVR_BASE + 8)] -+ ldr r0, [r11, #(PM_CTX_CCM_CTR1 + 12)] -+ str r0, [r10, #(CRYP_IVR_BASE + 12)] -+ -+ /* Setup CRYP for the CCM Init Phase */ -+ ldr r0, [r10, #CRYP_CR] -+ orr r0, r0, #(CRYP_CR_CRYPEN | CRYP_CR_INIT_PHASE) -+ str r0, [r10, #CRYP_CR] -+ ldr r0, [r10, #CRYP_CR] -+ -+ ldr r0, [r11, #PM_CTX_CCM_B0] -+ str r0, [r10, #CRYP_DIN] -+ ldr r0, [r11, #(PM_CTX_CCM_B0 + 4)] -+ str r0, [r10, #CRYP_DIN] -+ ldr r0, [r11, #(PM_CTX_CCM_B0 + 8)] -+ str r0, [r10, #CRYP_DIN] -+ ldr r0, [r11, #(PM_CTX_CCM_B0 + 12)] -+ str r0, [r10, #CRYP_DIN] -+ -+ PRINT_CHAR r0, r1, '5' -+ -+ WAIT_FLAG_TIMEOUT CRYP_CR, CRYP_CR_CRYPEN, 0 -+ -+ /* Setup CRYP for the CCM Payload phase */ -+ ldr r0, [r10, #CRYP_CR] -+ bic r0, r0, #CRYP_CR_PHASE_MASK -+ orr r0, r0, #CRYP_CR_PAYLOAD_PHASE -+ orr r0, r0, #CRYP_CR_CRYPEN -+ str r0, [r10, #CRYP_CR] -+ ldr r0, [r10, #CRYP_CR] -+ -+ PRINT_CHAR r0, r1, '\n' -+ -+_next_block: -+ PRINT_CHAR r0, r1, 'b' -+ -+ WAIT_FLAG_TIMEOUT CRYP_SR, CRYP_SR_IFEM, CRYP_SR_IFEM -+ -+ /* Feed input data, r8 stores the current source buffer */ -+ ldr r0, [r8], #4 -+ str r0, [r10, #CRYP_DIN] -+ ldr r0, [r8], #4 -+ str r0, [r10, #CRYP_DIN] -+ ldr r0, [r8], #4 -+ str r0, [r10, #CRYP_DIN] -+ ldr r0, [r8], #4 -+ str r0, [r10, #CRYP_DIN] -+ -+ WAIT_FLAG_TIMEOUT CRYP_SR, CRYP_SR_OFNE, CRYP_SR_OFNE -+ -+ /* Store output data, r9 stores the current source buffer */ -+ ldr r0, [r10, #CRYP_DOUT] -+ str r0, [r9], #4 -+ ldr r0, [r10, #CRYP_DOUT] -+ str r0, [r9], #4 -+ ldr r0, [r10, #CRYP_DOUT] -+ str r0, [r9], #4 -+ /* Before last 32bit word, the output FIFO shall not be empty */ -+ ldr r0, [r10, #CRYP_SR] -+ ands r0, r0, #CRYP_SR_OFNE -+ beq _ccm_failed -+ /* After last 32bit word for this 128block, FIFO shall be empty */ -+ ldr r0, [r10, #CRYP_DOUT] -+ str r0, [r9], #4 -+ ldr r0, [r10, #CRYP_SR] -+ ands r0, r0, #CRYP_SR_OFNE -+ bne _ccm_failed -+ -+ /* Another round if remaining data */ -+ subs r7, r7, #16 -+ bne _next_block; -+ -+ PRINT_CHAR r0, r1, '\n' -+ PRINT_CHAR r0, r1, '6' -+ -+ WAIT_FLAG_TIMEOUT CRYP_SR, CRYP_SR_BUSY, 0 -+ -+ /* -+ * Data processing completed, now remains the tag generation. -+ * Here expect SR[IFNF]=SR[OFNE]=1 and all others bits are 0. -+ */ -+ ldr r0, [r10, #CRYP_SR] -+ cmp r0, #(CRYP_SR_IFEM | CRYP_SR_IFNF) -+ bne _ccm_failed -+ -+ PRINT_CHAR r0, r1, '7' -+ -+ /* Setup CRYP1 for the CCM Final Phase */ -+ ldr r0, [r10, #CRYP_CR] -+ bic r0, r0, #CRYP_CR_CRYPEN -+ str r0, [r10, #CRYP_CR] -+ ldr r0, [r10, #CRYP_CR] -+ bic r0, r0, #CRYP_CR_PHASE_MASK -+ bic r0, r0, #CRYP_CR_ALGODIR_DECRYPT -+ orr r0, r0, #CRYP_CR_FINAL_PHASE -+ orr r0, r0, #CRYP_CR_CRYPEN -+ str r0, [r10, #CRYP_CR] -+ ldr r0, [r10, #CRYP_CR] -+ -+ /* Load CTR0 to generate the tag */ -+ ldr r0, [r11, #PM_CTX_CCM_CTR0] -+ str r0, [r10, #CRYP_DIN] -+ ldr r0, [r11, #(PM_CTX_CCM_CTR0 + 4)] -+ str r0, [r10, #CRYP_DIN] -+ ldr r0, [r11, #(PM_CTX_CCM_CTR0 + 8)] -+ str r0, [r10, #CRYP_DIN] -+ ldr r0, [r11, #(PM_CTX_CCM_CTR0 + 12)] -+ str r0, [r10, #CRYP_DIN] -+ -+ PRINT_CHAR r0, r1, '8' -+ -+ WAIT_FLAG_TIMEOUT CRYP_SR, CRYP_SR_OFNE, CRYP_SR_OFNE -+ -+ /* Store generated tag in the PM_CTX structure */ -+ ldr r0, [r10, #CRYP_DOUT] -+ str r0, [r11, #PM_CTX_CCM_TAG] -+ ldr r0, [r10, #CRYP_DOUT] -+ str r0, [r11, #(PM_CTX_CCM_TAG + 4)] -+ ldr r0, [r10, #CRYP_DOUT] -+ str r0, [r11, #(PM_CTX_CCM_TAG + 8)] -+ /* Before last 32bit word, the output FIFO shall not be empty */ -+ ldr r0, [r10, #CRYP_SR] -+ ands r0, r0, #CRYP_SR_OFNE -+ beq _ccm_failed -+ /* After last 32bit word for this 128block, FIFO shall be empty */ -+ ldr r0, [r10, #CRYP_DOUT] -+ str r0, [r11, #(PM_CTX_CCM_TAG + 12)] -+ ldr r0, [r10, #CRYP_SR] -+ ands r0, r0, #CRYP_SR_OFNE -+ bne _ccm_failed -+ -+ PRINT_CHAR r0, r1, '9' -+ -+ /* Successful return */ -+ bl _reset_cryp1 -+ mov r0, #0 -+ bx r12 -+ -+_ccm_failed: -+ -+ PRINT_CHAR r0, r1, 'K' -+ PRINT_CHAR r0, r1, 'O' -+ -+ bl _reset_cryp1 -+ mov r0, #1 -+ bx r12 -+ -+/* End address of the PIC resume sequence copy in retained RAM */ -+stm32mp_bkpsram_image_end: -+ nop -+ -+END_FUNC stm32mp_bkpsram_resume -+ -+/* -+ * int stm32mp_ccm_encrypt_teeram(ctx, dst, src, len) -+ */ -+FUNC stm32mp_ccm_encrypt_teeram , : -+ push {r4-r12, lr} -+UNWIND( .save {r4-r12, lr}) -+ mov r11, r0 -+ mov r0, r1 -+ mov r1, r2 -+ mov r2, r3 -+ mov r3, #0 -+ push {r0-r3} -+ bl _prepare_time -+ pop {r0-r3} -+ bl stm32mp_ccm_teeram -+ bl _save_resume_time -+ pop {r4-r12, pc} -+END_FUNC stm32mp_ccm_encrypt_teeram -+ -+/* -+ * int stm32mp_ccm_decrypt_teeram(ctx, cryp_base, dst, src) -+ */ -+FUNC stm32mp_ccm_decrypt_teeram , : -+ push {r4-r12, lr} -+UNWIND( .save {r4-r12, lr}) -+ mov r11, r0 -+ mov r0, r1 -+ mov r1, r2 -+ mov r2, r3 -+ mov r3, #1 -+ push {r0-r3} -+ bl _prepare_time -+ pop {r0-r3} -+ bl stm32mp_ccm_teeram -+ bl _save_resume_time -+ pop {r4-r12, pc} -+END_FUNC stm32mp_ccm_decrypt_teeram -+ -+/* -+ * stm32mp_sysram_resume - Resume OP-TEE execution -+ * -+ * This function is the entry point of OP-TEE core resume sequence in the TEE -+ * RAM. When TEE RAM is lost during a power cycle, stm32mp_bkpsram_resume() is -+ * called to restore TEE RAM content and branch to this stm32mp_sysram_resume() -+ * routine. -+ * -+ * This function calls the OP-TEE core generic PM resume API -+ * sm_pm_cpu_resume(). -+ */ -+FUNC stm32mp_sysram_resume, : -+UNWIND( .cantunwind) -+ /* Invalidate the data cache */ -+ -+ read_clidr r2 -+ ubfx r3, r2, #CLIDR_LOC_SHIFT, #CLIDR_FIELD_WIDTH -+ lsl r3, r3, #CSSELR_LEVEL_SHIFT -+ mov r1, #0 -+ -+loop1: -+ add r10, r1, r1, LSR #1 // Work out 3x current cache level -+ mov r12, r2, LSR r10 // extract cache type bits from clidr -+ and r12, r12, #7 // mask the bits for current cache only -+ cmp r12, #2 // see what cache we have at this level -+ blo level_done // no cache or only instruction cache at this level -+ -+ write_csselr r1 // select current cache level in csselr -+ isb // isb to sych the new cssr&csidr -+ read_ccsidr r12 // read the new ccsidr -+ and r10, r12, #7 // extract the length of the cache lines -+ add r10, r10, #4 // add 4 (r10 = line length offset) -+ ubfx r4, r12, #3, #10 // r4 = maximum way number (right aligned) -+ clz r5, r4 // r5 = the bit position of the way size increment -+ mov r9, r4 // r9 working copy of the aligned max way number -+ -+loop2: -+ ubfx r7, r12, #13, #15 // r7 = max set number (right aligned) -+ -+loop3: -+ orr r0, r1, r9, LSL r5 // factor in the way number and cache level into r0 -+ orr r0, r0, r7, LSL r10 // factor in the set number -+ -+ write_dcisw r0 -+ -+ subs r7, r7, #1 // decrement the set number -+ bhs loop3 -+ subs r9, r9, #1 // decrement the way number -+ bhs loop2 -+level_done: -+ add r1, r1, #2 // increment the cache number -+ cmp r3, r1 -+ dsb sy // ensure completion of previous cache maintenance instruction -+ bhi loop1 -+ -+ mov r6, #0 -+ write_csselr r6 //select cache level 0 in csselr -+ dsb sy -+ isb -+ -+ /* Resume sequence executes in Monitor mode */ -+ cps #CPSR_MODE_MON -+ -+ blx plat_cpu_reset_early -+ b sm_pm_cpu_resume -+END_FUNC stm32mp_sysram_resume -+ -+/* -+ * stm32mp_cpu_reset_state - set CPU in a reset like state -+ * -+ * Disable CPU env (interrupts, cache, SMP, MMU) and return. -+ * Preserve the execution mode in CPSR. -+ */ -+FUNC stm32mp_cpu_reset_state, : -+ push {r12, lr} -+UNWIND( .save {r12, lr}) -+ -+ cpsid aif -+ -+ bl psci_armv7_cpu_off -+ -+ write_bpiall -+ dsb -+ isb -+ read_sctlr r0 -+ bic r0, r0, #SCTLR_M -+ bic r0, r0, #SCTLR_I -+ write_sctlr r0 -+ dsb sy -+ isb -+ -+ pop {r12, pc} -+END_FUNC stm32mp_cpu_reset_state -diff --git a/core/arch/arm/plat-stm32mp1/pm/power.h b/core/arch/arm/plat-stm32mp1/pm/power.h -new file mode 100644 -index 000000000..4a5578c63 ---- /dev/null -+++ b/core/arch/arm/plat-stm32mp1/pm/power.h -@@ -0,0 +1,27 @@ -+/* SPDX-License-Identifier: BSD-3-Clause */ -+/* -+ * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved. -+ */ -+ -+#ifndef __STM32MP_PM_POWER_H__ -+#define __STM32MP_PM_POWER_H__ -+ -+#include -+#include -+#include -+ -+bool need_to_backup_cpu_context(unsigned int soc_mode); -+bool need_to_backup_stop_context(unsigned int soc_mode); -+ -+void stm32_enter_csleep(void); -+ -+void stm32_enter_cstop(uint32_t mode); -+void stm32_exit_cstop(void); -+ -+void stm32_enter_cstop_shutdown(uint32_t mode) __noreturn; -+void stm32_enter_cstop_reset(uint32_t mode) __noreturn; -+ -+void stm32_pm_cpu_power_down_wfi(void) __noreturn; -+void stm32_pm_cpu_wfi(void); -+ -+#endif /*__STM32MP_PM_POWER_H__*/ -diff --git a/core/arch/arm/plat-stm32mp1/pm/power_config.c b/core/arch/arm/plat-stm32mp1/pm/power_config.c -new file mode 100644 -index 000000000..61e4f6800 ---- /dev/null -+++ b/core/arch/arm/plat-stm32mp1/pm/power_config.c -@@ -0,0 +1,252 @@ -+// SPDX-License-Identifier: BSD-3-Clause -+/* -+ * Copyright (c) 2017-2018, STMicroelectronics - All Rights Reserved -+ */ -+ -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+ -+#include "context.h" -+#include "power.h" -+ -+#define DT_PWR_COMPAT "st,stm32mp1,pwr-reg" -+#define SYSTEM_SUSPEND_SUPPORTED_MODES "system_suspend_supported_soc_modes" -+#define SYSTEM_OFF_MODE "system_off_soc_mode" -+ -+static uint32_t deepest_suspend_mode; -+static uint32_t system_off_mode; -+static uint8_t stm32mp1_supported_soc_modes[STM32_PM_MAX_SOC_MODE]; -+ -+/* Boot with all domains ON */ -+static bool stm32mp1_pm_dom[STM32MP1_PD_MAX_PM_DOMAIN] = { -+ [STM32MP1_PD_VSW] = false, -+ [STM32MP1_PD_CORE_RET] = false, -+ [STM32MP1_PD_CORE] = false -+}; -+ -+bool need_to_backup_cpu_context(unsigned int soc_mode) -+{ -+ switch (soc_mode) { -+ case STM32_PM_CSTOP_ALLOW_STANDBY_DDR_SR: -+ return true; -+ case STM32_PM_CSLEEP_RUN: -+ case STM32_PM_CSTOP_ALLOW_STOP: -+ case STM32_PM_CSTOP_ALLOW_LP_STOP: -+ case STM32_PM_CSTOP_ALLOW_LPLV_STOP: -+ case STM32_PM_CSTOP_ALLOW_STANDBY_DDR_OFF: -+ case STM32_PM_SHUTDOWN: -+ return false; -+ default: -+ EMSG("Invalid mode 0x%x", soc_mode); -+ panic(); -+ } -+} -+ -+bool need_to_backup_stop_context(unsigned int soc_mode) -+{ -+ switch (soc_mode) { -+ case STM32_PM_CSTOP_ALLOW_STOP: -+ case STM32_PM_CSTOP_ALLOW_LP_STOP: -+ case STM32_PM_CSTOP_ALLOW_LPLV_STOP: -+ return true; -+ default: -+ return false; -+ } -+} -+ -+static bool get_pm_domain_state(uint8_t mode) -+{ -+ bool res = true; -+ enum stm32mp1_pm_domain id = STM32MP1_PD_MAX_PM_DOMAIN; -+ -+ while (res && (id > mode)) { -+ id--; -+ res &= stm32mp1_pm_dom[id]; -+ } -+ -+ return res; -+} -+ -+int stm32mp1_set_pm_domain_state(enum stm32mp1_pm_domain domain, bool status) -+{ -+ if (domain >= STM32MP1_PD_MAX_PM_DOMAIN) -+ return -1; -+ -+ stm32mp1_pm_dom[domain] = status; -+ -+ return 0; -+} -+ -+#ifdef CFG_DT -+static int fdt_read_uint32_array(void *fdt, int node, const char *prop_name, -+ uint32_t *array, uint32_t count) -+{ -+ const fdt32_t *cuint = NULL; -+ int len = 0; -+ uint32_t i = 0; -+ -+ cuint = fdt_getprop(fdt, node, prop_name, &len); -+ if (!cuint) -+ return -FDT_ERR_NOTFOUND; -+ -+ if ((uint32_t)len != (count * sizeof(uint32_t))) -+ return -FDT_ERR_BADLAYOUT; -+ -+ for (i = 0; i < ((uint32_t)len / sizeof(uint32_t)); i++) { -+ *array = fdt32_to_cpu(*cuint); -+ array++; -+ cuint++; -+ } -+ -+ return 0; -+} -+ -+static void save_supported_mode(void *fdt, int pwr_node) -+{ -+ int len = 0; -+ uint32_t count = 0; -+ unsigned int i = 0; -+ uint32_t supported[ARRAY_SIZE(stm32mp1_supported_soc_modes)] = { }; -+ const void *prop = 0; -+ -+ prop = fdt_getprop(fdt, pwr_node, SYSTEM_SUSPEND_SUPPORTED_MODES, &len); -+ if (!prop) -+ panic(); -+ -+ count = (uint32_t)len / sizeof(uint32_t); -+ if (count > STM32_PM_MAX_SOC_MODE) -+ panic(); -+ -+ if (fdt_read_uint32_array(fdt, pwr_node, SYSTEM_SUSPEND_SUPPORTED_MODES, -+ &supported[0], count) < 0) -+ panic("PWR DT"); -+ -+ for (i = 0; i < count; i++) { -+ if (supported[i] >= STM32_PM_MAX_SOC_MODE) -+ panic("Invalid mode"); -+ -+ stm32mp1_supported_soc_modes[supported[i]] = true; -+ } -+} -+#endif -+ -+static bool is_supported_mode(uint32_t soc_mode) -+{ -+ assert(soc_mode < ARRAY_SIZE(stm32mp1_supported_soc_modes)); -+ return stm32mp1_supported_soc_modes[soc_mode] == 1; -+} -+ -+uint32_t stm32mp1_get_lp_soc_mode(uint32_t psci_mode) -+{ -+ uint32_t mode = 0; -+ -+ if (psci_mode == PSCI_MODE_SYSTEM_OFF) -+ return system_off_mode; -+ -+ mode = deepest_suspend_mode; -+ -+ if ((mode == STM32_PM_CSTOP_ALLOW_STANDBY_DDR_SR) && -+ (!get_pm_domain_state(STM32MP1_PD_CORE_RET) || -+ !is_supported_mode(mode))) -+ mode = STM32_PM_CSTOP_ALLOW_LPLV_STOP; -+ -+ if ((mode == STM32_PM_CSTOP_ALLOW_LPLV_STOP) && -+ (!get_pm_domain_state(STM32MP1_PD_CORE) || -+ !is_supported_mode(mode))) -+ mode = STM32_PM_CSTOP_ALLOW_LP_STOP; -+ -+ if ((mode == STM32_PM_CSTOP_ALLOW_LP_STOP) && -+ !is_supported_mode(mode)) -+ mode = STM32_PM_CSTOP_ALLOW_STOP; -+ -+ if ((mode == STM32_PM_CSTOP_ALLOW_STOP) && -+ !is_supported_mode(mode)) -+ mode = STM32_PM_CSLEEP_RUN; -+ -+ return mode; -+} -+ -+int stm32mp1_set_lp_deepest_soc_mode(uint32_t psci_mode, uint32_t soc_mode) -+{ -+ if (soc_mode >= STM32_PM_MAX_SOC_MODE) -+ return -1; -+ -+ if (psci_mode == PSCI_MODE_SYSTEM_SUSPEND) { -+ deepest_suspend_mode = soc_mode; -+ -+ if (!IS_ENABLED(CFG_STM32_CRYP) && -+ deepest_suspend_mode == STM32_PM_CSTOP_ALLOW_STANDBY_DDR_SR) -+ deepest_suspend_mode = STM32_PM_CSTOP_ALLOW_LPLV_STOP; -+ } -+ -+ if (psci_mode == PSCI_MODE_SYSTEM_OFF) -+ system_off_mode = soc_mode; -+ -+ return 0; -+} -+ -+#ifdef CFG_DT -+static int dt_get_pwr_node(void *fdt) -+{ -+ return fdt_node_offset_by_compatible(fdt, -1, DT_PWR_COMPAT); -+} -+ -+static TEE_Result stm32mp1_init_lp_states(void) -+{ -+ void *fdt = NULL; -+ int pwr_node = -1; -+ const fdt32_t *cuint = NULL; -+ -+ fdt = get_embedded_dt(); -+ if (fdt) -+ pwr_node = dt_get_pwr_node(fdt); -+ -+ if (pwr_node >= 0) -+ cuint = fdt_getprop(fdt, pwr_node, SYSTEM_OFF_MODE, NULL); -+ -+ if (!fdt || (pwr_node < 0) || !cuint) { -+ IMSG("No power configuration found in DT"); -+ return TEE_SUCCESS; -+ } -+ -+ system_off_mode = fdt32_to_cpu(*cuint); -+ -+ /* Initialize suspend support to the deepest possible mode */ -+ if (IS_ENABLED(CFG_STM32_CRYP)) -+ deepest_suspend_mode = STM32_PM_CSTOP_ALLOW_STANDBY_DDR_SR; -+ else -+ deepest_suspend_mode = STM32_PM_CSTOP_ALLOW_LPLV_STOP; -+ -+ save_supported_mode(fdt, pwr_node); -+ -+ DMSG("Power configuration: shutdown to %u, suspend to %u", -+ stm32mp1_get_lp_soc_mode(PSCI_MODE_SYSTEM_OFF), -+ stm32mp1_get_lp_soc_mode(PSCI_MODE_SYSTEM_SUSPEND)); -+ -+ return TEE_SUCCESS; -+} -+#else -+static TEE_Result stm32mp1_init_lp_states(void) -+{ -+ if (IS_ENABLED(CFG_STM32_CRYP)) -+ deepest_suspend_mode = STM32_PM_CSTOP_ALLOW_STANDBY_DDR_SR; -+ else -+ deepest_suspend_mode = STM32_PM_CSTOP_ALLOW_LPLV_STOP; -+ -+ system_off_mode = STM32_PM_SHUTDOWN; -+ -+ DMSG("Power configuration: shutdown to %u, suspend to %u", -+ stm32mp1_get_lp_soc_mode(PSCI_MODE_SYSTEM_OFF), -+ stm32mp1_get_lp_soc_mode(PSCI_MODE_SYSTEM_SUSPEND)); -+ -+ return TEE_SUCCESS; -+} -+#endif -+service_init(stm32mp1_init_lp_states); -diff --git a/core/arch/arm/plat-stm32mp1/pm/psci.c b/core/arch/arm/plat-stm32mp1/pm/psci.c -index 9c472c796..65f583d2e 100644 ---- a/core/arch/arm/plat-stm32mp1/pm/psci.c -+++ b/core/arch/arm/plat-stm32mp1/pm/psci.c -@@ -1,16 +1,23 @@ - // SPDX-License-Identifier: BSD-2-Clause - /* -- * Copyright (c) 2017-2018, STMicroelectronics -+ * Copyright (c) 2017-2020, STMicroelectronics - */ - - #include - #include -+#include - #include -+#include -+#include -+#include - #include - #include - #include - #include -+#include -+#include - #include -+#include - #include - #include - #include -@@ -20,10 +27,17 @@ - #include - #include - #include -+#include - #include - #include - #include -+#include -+#include - #include -+#include -+ -+#include "context.h" -+#include "power.h" - - #define CONSOLE_FLUSH_DELAY_MS 10 - -@@ -57,10 +71,9 @@ int psci_affinity_info(uint32_t affinity, uint32_t lowest_affinity_level) - - DMSG("core %zu, state %u", pos, core_state[pos]); - -- if ((pos >= CFG_TEE_CORE_NB_CORE) || -- (lowest_affinity_level > PSCI_AFFINITY_LEVEL_ON)) { -+ if (pos >= CFG_TEE_CORE_NB_CORE || -+ lowest_affinity_level > PSCI_AFFINITY_LEVEL_ON) - return PSCI_RET_INVALID_PARAMETERS; -- } - - switch (core_state[pos]) { - case CORE_OFF: -@@ -82,38 +95,33 @@ int psci_affinity_info(uint32_t affinity, uint32_t lowest_affinity_level) - */ - void stm32mp_register_online_cpu(void) - { -- assert(core_state[0] == CORE_OFF); -+ assert(core_state[0] == CORE_OFF || core_state[0] == CORE_RET); - core_state[0] = CORE_ON; - } - #else --static void __noreturn stm32_pm_cpu_power_down_wfi(void) --{ -- dcache_op_level1(DCACHE_OP_CLEAN); -- -- io_write32(stm32_rcc_base() + RCC_MP_GRSTCSETR, -- RCC_MP_GRSTCSETR_MPUP1RST); -- -- dsb(); -- isb(); -- wfi(); -- panic(); --} -- - void stm32mp_register_online_cpu(void) - { - size_t pos = get_core_pos(); - uint32_t exceptions = lock_state_access(); - - if (pos == 0) { -- assert(core_state[pos] == CORE_OFF); -+ assert((core_state[pos] == CORE_OFF) || -+ (core_state[pos] == CORE_RET)); - } else { - if (core_state[pos] != CORE_AWAKE) { - core_state[pos] = CORE_OFF; - unlock_state_access(exceptions); -- stm32_pm_cpu_power_down_wfi(); -+ if (IS_ENABLED(CFG_PM)) -+ stm32_pm_cpu_power_down_wfi(); - panic(); - } -- stm32_clock_disable(RTCAPB); -+ -+ /* Clear hold in pen flag */ -+ io_write32(stm32mp_bkpreg(BCKR_CORE1_MAGIC_NUMBER), -+ BOOT_API_A7_RESET_MAGIC_NUMBER); -+ -+ /* Balance BKPREG clock gating */ -+ clk_disable(RTCAPB); - } - - core_state[pos] = CORE_ON; -@@ -134,11 +142,16 @@ static void release_secondary_early_hpen(size_t __unused pos) - raise_sgi0_as_secure(); - udelay(20); - -+ clk_enable(RTCAPB); -+ - io_write32(stm32mp_bkpreg(BCKR_CORE1_BRANCH_ADDRESS), - TEE_LOAD_ADDR); - io_write32(stm32mp_bkpreg(BCKR_CORE1_MAGIC_NUMBER), - BOOT_API_A7_CORE1_MAGIC_NUMBER); - -+ dsb_ishst(); -+ clk_disable(RTCAPB); -+ - dsb_ishst(); - itr_raise_sgi(GIC_SEC_SGI_0, TARGET_CPU1_GIC_MASK); - } -@@ -207,44 +220,188 @@ int psci_cpu_off(void) - unlock_state_access(exceptions); - - /* Enable BKPREG access for the disabled CPU */ -- stm32_clock_enable(RTCAPB); -+ clk_enable(RTCAPB); - - thread_mask_exceptions(THREAD_EXCP_ALL); -- stm32_pm_cpu_power_down_wfi(); -+ if (IS_ENABLED(CFG_PM)) -+ stm32_pm_cpu_power_down_wfi(); - panic(); - } - #endif - --/* Override default psci_system_off() with platform specific sequence */ --void __noreturn psci_system_off(void) -+#ifdef CFG_PM -+static int enter_cstop_suspend(unsigned int soc_mode) - { -- DMSG("core %u", get_core_pos()); -+ int rc = 1; -+ -+ if (read_isr()) -+ return rc; -+ -+ stm32_enter_cstop(soc_mode); -+ -+ if (need_to_backup_cpu_context(soc_mode)) { -+ stm32_pm_cpu_power_down_wfi(); -+ } else { -+ stm32_pm_cpu_wfi(); -+ rc = 0; -+ } -+ -+ stm32_exit_cstop(); -+ -+ return rc; -+} -+ -+static int plat_suspend(uint32_t arg) -+{ -+ unsigned int soc_mode = arg; -+ size_t pos = get_core_pos(); -+ int rc = 1; -+ -+ if (read_isr()) -+ return rc; -+ -+ /* No need to lock state access as CPU is alone when here */ -+ assert(core_state[pos] == CORE_ON); -+ core_state[pos] = CORE_RET; -+ -+ if (stm32mp_pm_save_context(soc_mode) == TEE_SUCCESS) -+ rc = enter_cstop_suspend(soc_mode); -+ -+ stm32mp_pm_restore_context(soc_mode); -+ stm32mp_pm_wipe_context(); -+ -+ assert(core_state[pos] == CORE_RET); -+ core_state[pos] = CORE_ON; -+ -+ return rc; -+} -+ -+static void plat_resume(uint32_t arg) -+{ -+ unsigned int soc_mode = arg; -+ -+ stm32mp_register_online_cpu(); -+ -+ assert(core_state[get_core_pos()] == CORE_ON); -+ -+ stm32mp_pm_restore_context(soc_mode); -+} - -- if (TRACE_LEVEL >= TRACE_DEBUG) { -- console_flush(); -- mdelay(CONSOLE_FLUSH_DELAY_MS); -+static bool plat_can_suspend(void) -+{ -+ size_t pos = get_core_pos(); -+ size_t n = 0; -+ uint32_t exceptions = 0; -+ bool rc = true; -+ -+ if (!IS_ENABLED(CFG_STM32_RNG)) -+ return false; -+ -+ if (CFG_TEE_CORE_NB_CORE == 1) -+ return true; -+ -+ exceptions = lock_state_access(); -+ -+ for (n = 0; n < ARRAY_SIZE(core_state); n++) { -+ if (n == pos) -+ continue; -+ -+ if (core_state[n] == CORE_AWAKE) { -+ /* State core as lost and proceed suspend */ -+ core_state[n] = CORE_OFF; -+ } -+ -+ if (core_state[n] != CORE_OFF) -+ rc = false; -+ } -+ -+ unlock_state_access(exceptions); -+ -+ return rc; -+} -+ -+/* Override default psci_system_suspend() with platform specific sequence */ -+int psci_system_suspend(uintptr_t entry, uint32_t context_id __unused, -+ struct sm_nsec_ctx *nsec) -+{ -+ int ret = PSCI_RET_INVALID_PARAMETERS; -+ uint32_t soc_mode = 0; -+ int __maybe_unused pos = get_core_pos(); -+ -+ DMSG("core %u", pos); -+ -+ if (!plat_can_suspend()) -+ return PSCI_RET_DENIED; -+ -+ soc_mode = stm32mp1_get_lp_soc_mode(PSCI_MODE_SYSTEM_SUSPEND); -+ -+ switch (soc_mode) { -+ case STM32_PM_CSLEEP_RUN: -+ stm32_enter_csleep(); -+ nsec->mon_lr = (uint32_t)entry; -+ return PSCI_RET_SUCCESS; -+ case STM32_PM_SHUTDOWN: -+ stm32_enter_cstop_shutdown(soc_mode); -+ panic(); -+ default: -+ /* Others are suspended mode: at least some context to backup */ -+ break; - } - -- if (stm32mp_with_pmic()) { -- stm32mp_get_pmic(); -- stpmic1_switch_off(); -- udelay(100); -+ assert(cpu_mmu_enabled() && core_state[pos] == CORE_ON); -+ -+ if (need_to_backup_cpu_context(soc_mode)) { -+ if (!IS_ENABLED(CFG_STM32_CRYP)) -+ return PSCI_RET_DENIED; -+ -+ sm_save_unbanked_regs(&nsec->ub_regs); -+ /* -+ * sm_pm_cpu_suspend(arg, func) saves the CPU core context in TEE RAM -+ * then calls func(arg) to run the platform lower power sequence. -+ * -+ * If platform fails to suspend, sm_pm_cpu_suspend() returns with a -+ * non null return code. When sm_pm_cpu_suspend() returns 0 platform -+ * context must be restored. -+ */ -+ ret = sm_pm_cpu_suspend((uint32_t)soc_mode, plat_suspend); -+ if (ret == 0) { -+ plat_resume((uint32_t)soc_mode); -+ sm_restore_unbanked_regs(&nsec->ub_regs); -+ } -+ } else { -+ ret = plat_suspend((uint32_t)soc_mode); - } - -- panic(); -+ if (ret == 0) { -+ nsec->mon_lr = (uint32_t)entry; -+ IMSG("Resumed"); -+ return PSCI_RET_SUCCESS; -+ } -+ -+ return PSCI_RET_INTERNAL_FAILURE; -+} -+ -+/* Override default psci_system_off() with platform specific sequence */ -+void __noreturn psci_system_off(void) -+{ -+ uint32_t soc_mode = stm32mp1_get_lp_soc_mode(PSCI_MODE_SYSTEM_OFF); -+ -+ DMSG("core %u", get_core_pos()); -+ -+ stm32_enter_cstop_shutdown(soc_mode); - } - - /* Override default psci_system_reset() with platform specific sequence */ - void __noreturn psci_system_reset(void) - { -- vaddr_t rcc_base = stm32_rcc_base(); -+ uint32_t soc_mode = stm32mp1_get_lp_soc_mode(PSCI_MODE_SYSTEM_OFF); - - DMSG("core %u", get_core_pos()); - -- io_write32(rcc_base + RCC_MP_GRSTCSETR, RCC_MP_GRSTCSETR_MPSYSRST); -- udelay(100); -- panic(); -+ stm32_enter_cstop_reset(soc_mode); - } -+#endif /*CFG_PM*/ -+ - - /* Override default psci_cpu_on() with platform supported features */ - int psci_features(uint32_t psci_fid) -@@ -261,6 +418,7 @@ int psci_features(uint32_t psci_fid) - return PSCI_RET_SUCCESS; - return PSCI_RET_NOT_SUPPORTED; - case PSCI_SYSTEM_OFF: -+ case PSCI_SYSTEM_SUSPEND: - if (stm32mp_with_pmic()) - return PSCI_RET_SUCCESS; - return PSCI_RET_NOT_SUPPORTED; -diff --git a/core/arch/arm/plat-stm32mp1/pm/sub.mk b/core/arch/arm/plat-stm32mp1/pm/sub.mk -index c8ae8f915..d652958e3 100644 ---- a/core/arch/arm/plat-stm32mp1/pm/sub.mk -+++ b/core/arch/arm/plat-stm32mp1/pm/sub.mk -@@ -1 +1,7 @@ -+asm-defines-y += context_asm_defines.c -+ -+srcs-$(CFG_PM) += context.c -+srcs-$(CFG_PM) += low_power.c -+srcs-$(CFG_PM) += pm_helpers.S -+srcs-$(CFG_PM) += power_config.c - srcs-$(CFG_PSCI_ARM32) += psci.c -diff --git a/core/arch/arm/plat-stm32mp1/remoteproc_pta.c b/core/arch/arm/plat-stm32mp1/remoteproc_pta.c -new file mode 100644 -index 000000000..27b0dfa07 ---- /dev/null -+++ b/core/arch/arm/plat-stm32mp1/remoteproc_pta.c -@@ -0,0 +1,528 @@ -+ // SPDX-License-Identifier: BSD-2-Clause -+ /* -+ * Copyright (C) 2020, STMicroelectronics - All Rights Reserved -+ */ -+ -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+ -+#define PTA_NAME "remoteproc.pta" -+ -+#define STM32_M4_FW_ID 0 -+ -+/* Firmware states */ -+enum rproc_load_state { -+ REMOTEPROC_OFF = 0, -+ REMOTEPROC_ON, -+}; -+ -+/* -+ * struct rproc_ta_etzpc_rams - memory protection strategy table -+ * @pa - Memory physical base address from current CPU space -+ * @size - Memory region byte size -+ * @etzpc_id - associated ETZPC identifier. -+ * @attr - memory access permission according to @etzpc_decprot_attributes -+ */ -+struct rproc_ta_etzpc_rams { -+ paddr_t pa; -+ size_t size; -+ uint32_t etzpc_id; -+ enum etzpc_decprot_attributes attr; -+}; -+ -+/* -+ * struct rproc_ta_memory_region - Represent a remote processor memory mapping -+ * @pa - Memory physical base address from current CPU space -+ * @da - Memory physical base address from remote processor space -+ * @size - Memory region byte size -+ */ -+struct rproc_ta_memory_region { -+ paddr_t pa; -+ paddr_t da; -+ size_t size; -+}; -+ -+static const struct rproc_ta_etzpc_rams rproc_ta_mp1_m4_rams[] = { -+ /* MCU SRAM 1*/ -+ { -+ .pa = MCUSRAM_BASE, -+ .size = 0x20000, -+ .etzpc_id = STM32MP1_ETZPC_SRAM1_ID, -+ .attr = ETZPC_DECPROT_MCU_ISOLATION, -+ }, -+ /* MCU SRAM 2*/ -+ { -+ .pa = MCUSRAM_BASE + 0x20000, -+ .size = 0x20000, -+ .etzpc_id = STM32MP1_ETZPC_SRAM2_ID, -+ .attr = ETZPC_DECPROT_MCU_ISOLATION, -+ }, -+ -+ /* MCU SRAM 3*/ -+ { -+ /* Used as shared memory between the NS and the coprocessor */ -+ .pa = MCUSRAM_BASE + 0x40000, -+ .size = 0x10000, -+ .etzpc_id = STM32MP1_ETZPC_SRAM3_ID, -+ .attr = ETZPC_DECPROT_NS_RW, -+ }, -+ /* MCU SRAM 4*/ -+ /* Not used reserved by NS for MDMA */ -+ { -+ .pa = MCUSRAM_BASE + 0x50000, -+ .size = 0x10000, -+ .etzpc_id = STM32MP1_ETZPC_SRAM4_ID, -+ .attr = ETZPC_DECPROT_NS_RW, -+ }, -+ -+ /* MCU RETRAM */ -+ { -+ .pa = RETRAM_BASE, -+ .size = RETRAM_SIZE, -+ .etzpc_id = STM32MP1_ETZPC_RETRAM_ID, -+ .attr = ETZPC_DECPROT_MCU_ISOLATION, -+ }, -+}; -+ -+static const struct rproc_ta_memory_region rproc_ta_mp1_m4_mems[] = { -+ /* MCU SRAM */ -+ { .pa = MCUSRAM_BASE, .da = 0x10000000, .size = MCUSRAM_SIZE }, -+ /* Alias of the MCU SRAM */ -+ { .pa = MCUSRAM_BASE, .da = 0x30000000, .size = MCUSRAM_SIZE }, -+ /* RETRAM */ -+ { .pa = RETRAM_BASE, .da = 0x00000000, .size = RETRAM_SIZE }, -+}; -+ -+static enum rproc_load_state rproc_ta_state; -+ -+static TEE_Result rproc_pta_capabilities(uint32_t pt, -+ TEE_Param params[TEE_NUM_PARAMS]) -+{ -+ const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, -+ TEE_PARAM_TYPE_VALUE_OUTPUT, -+ TEE_PARAM_TYPE_VALUE_OUTPUT, -+ TEE_PARAM_TYPE_NONE); -+ -+ if (pt != exp_pt) -+ return TEE_ERROR_BAD_PARAMETERS; -+ -+ /* Support only ELF format */ -+ params[1].value.a = PTA_REMOTEPROC_ELF_FMT; -+ -+ /* -+ * Due to stm32mp1 pager, secure memory is too expensive. Support hash -+ * protected image only, so that firmware image can be loaded from -+ * non-secure memory. -+ */ -+ params[2].value.a = PTA_REMOTEPROC_FW_WITH_HASH_TABLE; -+ -+ return TEE_SUCCESS; -+} -+ -+static TEE_Result da_to_pa(paddr_t da, size_t size, paddr_t *pa) -+{ -+ const struct rproc_ta_memory_region *mems = rproc_ta_mp1_m4_mems; -+ size_t i = 0; -+ -+ DMSG("da addr: %#"PRIxPA" size: %zu", da, size); -+ -+ for (i = 0; i < ARRAY_SIZE(rproc_ta_mp1_m4_mems); i++) { -+ if (da >= mems[i].da && -+ (da + size) <= (mems[i].da + mems[i].size)) { -+ *pa = da - mems[i].da + mems[i].pa; -+ DMSG("da %#"PRIxPA" to pa %#"PRIxPA, da, *pa); -+ -+ return TEE_SUCCESS; -+ } -+ } -+ -+ return TEE_ERROR_ACCESS_DENIED; -+} -+ -+static TEE_Result rproc_pta_load_segment(uint32_t pt, -+ TEE_Param params[TEE_NUM_PARAMS]) -+{ -+ const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, -+ TEE_PARAM_TYPE_MEMREF_INPUT, -+ TEE_PARAM_TYPE_VALUE_INPUT, -+ TEE_PARAM_TYPE_MEMREF_INPUT); -+ TEE_Result res = TEE_ERROR_GENERIC; -+ paddr_t pa = 0; -+ uint8_t *dst = 0; -+ uint8_t *src = params[1].memref.buffer; -+ size_t size = params[1].memref.size; -+ uint8_t *hash = params[3].memref.buffer; -+ paddr_t da = (paddr_t)reg_pair_to_64(params[2].value.b, -+ params[2].value.a); -+ -+ if (pt != exp_pt) -+ return TEE_ERROR_BAD_PARAMETERS; -+ -+ if (!hash || params[3].memref.size != TEE_SHA256_HASH_SIZE) -+ return TEE_ERROR_BAD_PARAMETERS; -+ -+ /* Only STM32_M4_FW_ID supported */ -+ if (params[0].value.a != STM32_M4_FW_ID) { -+ EMSG("Unsupported firmware ID %#"PRIx32, params[0].value.a); -+ return TEE_ERROR_NOT_SUPPORTED; -+ } -+ -+ if (rproc_ta_state != REMOTEPROC_OFF) -+ return TEE_ERROR_BAD_STATE; -+ -+ /* Get the physical address in A7 mapping */ -+ res = da_to_pa(da, size, &pa); -+ if (res) -+ return res; -+ -+ /* Get the associated va */ -+ dst = (void *)core_mmu_get_va(pa, MEM_AREA_IO_SEC); -+ -+ /* Copy the segment to the remote processor memory*/ -+ memcpy(dst, src, size); -+ -+ /* Verify that loaded segment is valid */ -+ res = hash_sha256_check(hash, dst, size); -+ if (res) -+ memset(dst, 0, size); -+ -+ return res; -+} -+ -+static TEE_Result rproc_pta_set_memory(uint32_t pt, -+ TEE_Param params[TEE_NUM_PARAMS]) -+{ -+ const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, -+ TEE_PARAM_TYPE_VALUE_INPUT, -+ TEE_PARAM_TYPE_VALUE_INPUT, -+ TEE_PARAM_TYPE_VALUE_INPUT); -+ TEE_Result res = TEE_ERROR_GENERIC; -+ paddr_t pa = 0; -+ vaddr_t dst = 0; -+ paddr_t da = params[1].value.a; -+ size_t size = params[2].value.a; -+ char value = (char)params[3].value.a; -+ -+ if (pt != exp_pt) -+ return TEE_ERROR_BAD_PARAMETERS; -+ -+ /* Only STM32_M4_FW_ID supported */ -+ if (params[0].value.a != STM32_M4_FW_ID) { -+ EMSG("Unsupported firmware ID %#"PRIx32, params[0].value.a); -+ return TEE_ERROR_NOT_SUPPORTED; -+ } -+ -+ if (rproc_ta_state != REMOTEPROC_OFF) -+ return TEE_ERROR_BAD_STATE; -+ -+ /* Get the physical address in CPU mapping */ -+ res = da_to_pa(da, size, &pa); -+ if (res) -+ return res; -+ -+ dst = core_mmu_get_va(pa, MEM_AREA_IO_SEC); -+ -+ memset((void *)dst, value, size); -+ -+ return TEE_SUCCESS; -+} -+ -+static TEE_Result rproc_pta_da_to_pa(uint32_t pt, -+ TEE_Param params[TEE_NUM_PARAMS]) -+{ -+ const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, -+ TEE_PARAM_TYPE_VALUE_INPUT, -+ TEE_PARAM_TYPE_VALUE_INPUT, -+ TEE_PARAM_TYPE_VALUE_OUTPUT); -+ TEE_Result res = TEE_ERROR_GENERIC; -+ paddr_t da = params[1].value.a; -+ size_t size = params[2].value.a; -+ paddr_t pa = 0; -+ -+ DMSG("Conversion for address %#"PRIxPA" size %zu", da, size); -+ -+ if (pt != exp_pt) -+ return TEE_ERROR_BAD_PARAMETERS; -+ -+ /* Only STM32_M4_FW_ID supported */ -+ if (params[0].value.a != STM32_M4_FW_ID) { -+ EMSG("Unsupported firmware ID %#"PRIx32, params[0].value.a); -+ return TEE_ERROR_NOT_SUPPORTED; -+ } -+ -+ /* Target address is expected 32bit, ensure 32bit MSB are zero */ -+ if (params[1].value.b || params[2].value.b) -+ return TEE_ERROR_BAD_PARAMETERS; -+ -+ res = da_to_pa(da, size, &pa); -+ if (res) -+ return res; -+ -+ reg_pair_from_64((uint64_t)pa, ¶ms[3].value.b, ¶ms[3].value.a); -+ -+ return TEE_SUCCESS; -+} -+ -+static void rproc_pta_mem_protect(bool secure_access) -+{ -+ unsigned int i = 0; -+ const struct rproc_ta_etzpc_rams *ram = NULL; -+ -+ /* -+ * MCU RAM banks access permissions for MCU memories depending on -+ * rproc_ta_mp1_m4_rams[]. -+ * If memory bank is declared as MCU isolated: -+ * if secure_access then set to secure world read/write permission -+ * else set to MCU isolated -+ * else apply memory permission as defined in rproc_ta_mp1_m4_rams[]. -+ */ -+ for (i = 0; i < ARRAY_SIZE(rproc_ta_mp1_m4_rams); i++) { -+ enum etzpc_decprot_attributes attr = ETZPC_DECPROT_MAX; -+ -+ ram = &rproc_ta_mp1_m4_rams[i]; -+ attr = ram->attr; -+ -+ if (secure_access && attr == ETZPC_DECPROT_MCU_ISOLATION) -+ attr = ETZPC_DECPROT_S_RW; -+ -+ etzpc_configure_decprot(ram->etzpc_id, attr); -+ } -+} -+ -+static TEE_Result rproc_pta_start(uint32_t pt, -+ TEE_Param params[TEE_NUM_PARAMS]) -+{ -+ const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, -+ TEE_PARAM_TYPE_NONE, -+ TEE_PARAM_TYPE_NONE, -+ TEE_PARAM_TYPE_NONE); -+ vaddr_t rcc_base = stm32_rcc_base(); -+ -+ if (pt != exp_pt) -+ return TEE_ERROR_BAD_PARAMETERS; -+ -+ /* Only STM32_M4_FW_ID supported */ -+ if (params[0].value.a != STM32_M4_FW_ID) { -+ EMSG("Unsupported firmware ID %#"PRIx32, params[0].value.a); -+ return TEE_ERROR_NOT_SUPPORTED; -+ } -+ -+ if (rproc_ta_state != REMOTEPROC_OFF) -+ return TEE_ERROR_BAD_STATE; -+ -+ clk_enable(CK_MCU); -+ -+ /* Configure the Cortex-M4 RAMs as expected to run the firmware */ -+ rproc_pta_mem_protect(false); -+ -+ /* -+ * The firmware is started by deasserting the hold boot and -+ * asserting back to avoid auto restart on a crash. -+ * No need to release the MCU reset as it is automatically released by -+ * the hardware. -+ */ -+ io_setbits32(rcc_base + RCC_MP_GCR, RCC_MP_GCR_BOOT_MCU); -+ io_clrbits32(rcc_base + RCC_MP_GCR, RCC_MP_GCR_BOOT_MCU); -+ -+ rproc_ta_state = REMOTEPROC_ON; -+ -+ return TEE_SUCCESS; -+} -+ -+static TEE_Result rproc_pta_stop(uint32_t pt, -+ TEE_Param params[TEE_NUM_PARAMS]) -+{ -+ const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, -+ TEE_PARAM_TYPE_NONE, -+ TEE_PARAM_TYPE_NONE, -+ TEE_PARAM_TYPE_NONE); -+ const struct rproc_ta_etzpc_rams *ram = NULL; -+ vaddr_t rcc_base = stm32_rcc_base(); -+ unsigned int i = 0; -+ -+ if (pt != exp_pt) -+ return TEE_ERROR_BAD_PARAMETERS; -+ -+ /* Only STM32_M4_FW_ID supported */ -+ if (params[0].value.a != STM32_M4_FW_ID) { -+ EMSG("Unsupported firmware ID %#"PRIx32, params[0].value.a); -+ return TEE_ERROR_NOT_SUPPORTED; -+ } -+ -+ if (rproc_ta_state != REMOTEPROC_ON) -+ return TEE_ERROR_BAD_STATE; -+ -+ /* The firmware is stopped (reset with holdboot is active) */ -+ io_clrbits32(rcc_base + RCC_MP_GCR, RCC_MP_GCR_BOOT_MCU); -+ -+ stm32_reset_set(MCU_R); -+ -+ clk_disable(CK_MCU); -+ -+ /* -+ * Cortex-M4 memories are cleaned and access rights restored for the -+ * secure context. -+ */ -+ rproc_pta_mem_protect(true); -+ for (i = 0; i < ARRAY_SIZE(rproc_ta_mp1_m4_rams); i++) { -+ ram = &rproc_ta_mp1_m4_rams[i]; -+ if (ram->attr == ETZPC_DECPROT_MCU_ISOLATION) { -+ memset((void *)core_mmu_get_va(ram->pa, -+ MEM_AREA_IO_SEC), -+ 0, ram->size); -+ } -+ } -+ rproc_ta_state = REMOTEPROC_OFF; -+ -+ return TEE_SUCCESS; -+} -+ -+static TEE_Result rproc_pta_verify_rsa_signature(TEE_Param *hash, -+ TEE_Param *sig, uint32_t algo) -+{ -+ struct rsa_public_key key = { }; -+ TEE_Result res = TEE_ERROR_GENERIC; -+ uint32_t e = TEE_U32_TO_BIG_ENDIAN(rproc_pub_key_exponent); -+ size_t hash_size = (size_t)hash->memref.size; -+ size_t sig_size = (size_t)sig->memref.size; -+ -+ res = crypto_acipher_alloc_rsa_public_key(&key, sig_size); -+ if (res) -+ return TEE_ERROR_SECURITY; -+ -+ res = crypto_bignum_bin2bn((uint8_t *)&e, sizeof(e), key.e); -+ if (res) -+ goto out; -+ -+ res = crypto_bignum_bin2bn(rproc_pub_key_modulus, -+ rproc_pub_key_modulus_size, key.n); -+ if (res) -+ goto out; -+ -+ res = crypto_acipher_rsassa_verify(algo, &key, hash_size, -+ hash->memref.buffer, hash_size, -+ sig->memref.buffer, sig_size); -+ -+out: -+ crypto_acipher_free_rsa_public_key(&key); -+ -+ return res; -+} -+ -+static TEE_Result rproc_pta_verify_digest(uint32_t pt, -+ TEE_Param params[TEE_NUM_PARAMS]) -+{ -+ struct rproc_pta_key_info *keyinfo = NULL; -+ const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, -+ TEE_PARAM_TYPE_MEMREF_INPUT, -+ TEE_PARAM_TYPE_MEMREF_INPUT, -+ TEE_PARAM_TYPE_MEMREF_INPUT); -+ -+ if (pt != exp_pt) -+ return TEE_ERROR_BAD_PARAMETERS; -+ -+ /* Only STM32_M4_FW_ID supported */ -+ if (params[0].value.a != STM32_M4_FW_ID) { -+ EMSG("Unsupported firmware ID %#"PRIx32, params[0].value.a); -+ return TEE_ERROR_NOT_SUPPORTED; -+ } -+ -+ if (rproc_ta_state != REMOTEPROC_OFF) -+ return TEE_ERROR_BAD_STATE; -+ -+ keyinfo = params[1].memref.buffer; -+ -+ if (!keyinfo || -+ RPROC_PTA_GET_KEYINFO_SIZE(keyinfo) != params[1].memref.size) -+ return TEE_ERROR_BAD_PARAMETERS; -+ -+ if (keyinfo->algo != TEE_ALG_RSASSA_PKCS1_V1_5_SHA256) -+ return TEE_ERROR_NOT_SUPPORTED; -+ -+ return rproc_pta_verify_rsa_signature(¶ms[2], ¶ms[3], -+ keyinfo->algo); -+} -+ -+static TEE_Result rproc_pta_invoke_command(void *pSessionContext __unused, -+ uint32_t cmd_id, -+ uint32_t param_types, -+ TEE_Param params[TEE_NUM_PARAMS]) -+{ -+ switch (cmd_id) { -+ case PTA_REMOTEPROC_HW_CAPABILITIES: -+ return rproc_pta_capabilities(param_types, params); -+ case PTA_REMOTEPROC_LOAD_SEGMENT_SHA256: -+ return rproc_pta_load_segment(param_types, params); -+ case PTA_REMOTEPROC_SET_MEMORY: -+ return rproc_pta_set_memory(param_types, params); -+ case PTA_REMOTEPROC_FIRMWARE_START: -+ return rproc_pta_start(param_types, params); -+ case PTA_REMOTEPROC_FIRMWARE_STOP: -+ return rproc_pta_stop(param_types, params); -+ case PTA_REMOTEPROC_FIRMWARE_DA_TO_PA: -+ return rproc_pta_da_to_pa(param_types, params); -+ case PTA_REMOTEPROC_VERIFY_DIGEST: -+ return rproc_pta_verify_digest(param_types, params); -+ default: -+ break; -+ } -+ -+ return TEE_ERROR_NOT_IMPLEMENTED; -+} -+ -+/* -+ * Trusted Application entry points -+ */ -+static TEE_Result -+ rproc_pta_open_session(uint32_t param_types __unused, -+ TEE_Param params[TEE_NUM_PARAMS] __unused, -+ void **sess_ctx __unused) -+{ -+ struct tee_ta_session *s = tee_ta_get_calling_session(); -+ -+ /* Check that we're called from a user TA */ -+ if (!s) -+ return TEE_ERROR_ACCESS_DENIED; -+ -+ if (!is_user_ta_ctx(s->ctx)) -+ return TEE_ERROR_ACCESS_DENIED; -+ -+ return TEE_SUCCESS; -+} -+ -+static TEE_Result rproc_pta_init(void) -+{ -+ vaddr_t rcc_base = stm32_rcc_base(); -+ -+ /* Configure the Cortex-M4 rams access right for secure context only */ -+ rproc_pta_mem_protect(true); -+ -+ /* Initialise the context */ -+ rproc_ta_state = REMOTEPROC_OFF; -+ -+ /* Ensure that the MCU is HOLD */ -+ io_clrbits32(rcc_base + RCC_MP_GCR, RCC_MP_GCR_BOOT_MCU); -+ stm32_reset_set(MCU_R); -+ -+ return TEE_SUCCESS; -+} -+service_init_late(rproc_pta_init); -+ -+pseudo_ta_register(.uuid = PTA_REMOTEPROC_UUID, .name = PTA_NAME, -+ .flags = PTA_DEFAULT_FLAGS, -+ .invoke_command_entry_point = rproc_pta_invoke_command, -+ .open_session_entry_point = rproc_pta_open_session); -diff --git a/core/arch/arm/plat-stm32mp1/reset.S b/core/arch/arm/plat-stm32mp1/reset.S -index 8a1ab6494..0a11d915a 100644 ---- a/core/arch/arm/plat-stm32mp1/reset.S -+++ b/core/arch/arm/plat-stm32mp1/reset.S -@@ -3,7 +3,7 @@ - * Copyright (c) 2018, STMicroelectronics - */ - --#include -+#include - #include - #include - -@@ -21,7 +21,33 @@ FUNC plat_cpu_reset_early , : - mov_imm r1, STM32MP1_NSACR_PRESERVE_MASK - and r0, r0, r1 - write_nsacr r0 -+ isb -+ -+ read_actlr r0 -+ orr r0, r0, #ACTLR_SMP -+ write_actlr r0 -+ -+ /* -+ * Always reset CNTVOFF for the dear non secure world. -+ * This operation requires being in Monitor mode and -+ * non secure state. -+ */ -+ mrs r1, cpsr -+ cps #CPSR_MODE_MON -+ isb -+ -+ read_scr r2 -+ orr r0, r2, #SCR_NS -+ write_scr r0 -+ isb -+ -+ mov r0, #0 -+ write_cntvoff r0, r0 - -+ write_scr r2 - isb -+ msr cpsr, r1 -+ isb -+ - bx lr - END_FUNC plat_cpu_reset_early -diff --git a/core/arch/arm/plat-stm32mp1/rproc_pub_key.h b/core/arch/arm/plat-stm32mp1/rproc_pub_key.h -new file mode 100644 -index 000000000..9b1db4271 ---- /dev/null -+++ b/core/arch/arm/plat-stm32mp1/rproc_pub_key.h -@@ -0,0 +1,16 @@ -+/* SPDX-License-Identifier: BSD-2-Clause */ -+/* -+ * Copyright (C) 2020, STMicroelectronics - All Rights Reserved -+ */ -+ -+#ifndef RPROC_PUB_KEY_H -+#define RPROC_PUB_KEY_H -+ -+#include -+ -+extern const uint32_t rproc_pub_key_exponent; -+extern const uint8_t rproc_pub_key_modulus[]; -+extern const size_t rproc_pub_key_modulus_size; -+ -+#endif /*RPROC_PUB_KEY_H*/ -+ -diff --git a/core/arch/arm/plat-stm32mp1/scmi_server.c b/core/arch/arm/plat-stm32mp1/scmi_server.c -index dce8c169e..169e69ee7 100644 ---- a/core/arch/arm/plat-stm32mp1/scmi_server.c -+++ b/core/arch/arm/plat-stm32mp1/scmi_server.c -@@ -1,9 +1,10 @@ - // SPDX-License-Identifier: BSD-2-Clause - /* -- * Copyright (c) 2019, STMicroelectronics -+ * Copyright (c) 2019-2020, STMicroelectronics - */ - #include - #include -+#include - #include - #include - #include -@@ -13,6 +14,7 @@ - #include - #include - #include -+#include - #include - #include - #include -@@ -312,9 +314,22 @@ const char *plat_scmi_clock_get_name(unsigned int agent_id, - return clock->name; - } - --int32_t plat_scmi_clock_rates_array(unsigned int agent_id, unsigned int scmi_id, -- size_t start_index, unsigned long *array, -- size_t *nb_elts) -+int32_t plat_scmi_clock_rates_array(unsigned int agent_id __unused, -+ unsigned int scmi_id __unused, -+ size_t start_index __unused, -+ unsigned long *array __unused, -+ size_t *nb_elts __unused) -+{ -+ /* -+ * Explicitly do not expose clock rates by array since not -+ * fully supported by Linux kernel as of v5.4.24. -+ */ -+ return SCMI_NOT_SUPPORTED; -+} -+ -+int32_t plat_scmi_clock_rates_by_step(unsigned int agent_id, -+ unsigned int scmi_id, -+ unsigned long *array) - { - struct stm32_scmi_clk *clock = find_clock(agent_id, scmi_id); - -@@ -324,16 +339,48 @@ int32_t plat_scmi_clock_rates_array(unsigned int agent_id, unsigned int scmi_id, - if (!stm32mp_nsec_can_access_clock(clock->clock_id)) - return SCMI_DENIED; - -- /* Exposed clocks are currently fixed rate clocks */ -- if (start_index) -- return SCMI_INVALID_PARAMETERS; -+ switch (scmi_id) { -+ case CK_SCMI0_MPU: -+ /* -+ * Pretend we support all rates for MPU clock, -+ * CLOCK_RATE_SET will reject unsupported rates. -+ */ -+ array[0] = 0U; -+ array[1] = UINT32_MAX; -+ array[2] = 1U; -+ break; -+ default: -+ array[0] = clk_get_rate(clock->clock_id); -+ array[1] = array[0]; -+ array[2] = 0U; -+ break; -+ } - -- if (!array) -- *nb_elts = 1; -- else if (*nb_elts == 1) -- *array = stm32_clock_get_rate(clock->clock_id); -- else -- return SCMI_GENERIC_ERROR; -+ return SCMI_SUCCESS; -+} -+ -+int32_t plat_scmi_clock_set_rate(unsigned int agent_id, unsigned int scmi_id, -+ unsigned long rate) -+{ -+ struct stm32_scmi_clk *clock = find_clock(agent_id, scmi_id); -+ int ret = SCMI_DENIED; -+ -+ if (!clock) -+ return SCMI_NOT_FOUND; -+ if (!stm32mp_nsec_can_access_clock(clock->clock_id)) -+ return SCMI_DENIED; -+ -+ switch (scmi_id) { -+ case CK_SCMI0_MPU: -+ ret = stm32mp1_set_opp_khz(rate / 1000); -+ if (ret) -+ return SCMI_INVALID_PARAMETERS; -+ break; -+ default: -+ if (rate != clk_get_rate(clock->clock_id)) -+ return SCMI_INVALID_PARAMETERS; -+ break; -+ } - - return SCMI_SUCCESS; - } -@@ -346,7 +393,7 @@ unsigned long plat_scmi_clock_get_rate(unsigned int agent_id, - if (!clock || !stm32mp_nsec_can_access_clock(clock->clock_id)) - return 0; - -- return stm32_clock_get_rate(clock->clock_id); -+ return clk_get_rate(clock->clock_id); - } - - int32_t plat_scmi_clock_get_state(unsigned int agent_id, unsigned int scmi_id) -@@ -373,13 +420,13 @@ int32_t plat_scmi_clock_set_state(unsigned int agent_id, unsigned int scmi_id, - if (enable_not_disable) { - if (!clock->enabled) { - DMSG("SCMI clock %u enable", scmi_id); -- stm32_clock_enable(clock->clock_id); -+ clk_enable(clock->clock_id); - clock->enabled = true; - } - } else { - if (clock->enabled) { - DMSG("SCMI clock %u disable", scmi_id); -- stm32_clock_disable(clock->clock_id); -+ clk_disable(clock->clock_id); - clock->enabled = false; - } - } -@@ -483,6 +530,19 @@ int32_t plat_scmi_rd_set_state(unsigned int agent_id, unsigned int scmi_id, - return SCMI_SUCCESS; - } - -+static TEE_Result stm32_scmi_pm(enum pm_op op, unsigned int pm_hint __unused, -+ const struct pm_callback_handle *hdl __unused) -+{ -+ size_t i = 0; -+ -+ if (op == PM_OP_RESUME) -+ for (i = 0; i < ARRAY_SIZE(scmi_channel); i++) -+ scmi_smt_init_agent_channel(&scmi_channel[i]); -+ -+ return TEE_SUCCESS; -+} -+DECLARE_KEEP_PAGER(stm32_scmi_pm); -+ - /* - * Platform SCMI voltage domains - */ -@@ -810,6 +870,8 @@ static TEE_Result stm32mp1_init_scmi_server(void) - size_t i = 0; - size_t j = 0; - -+ register_pm_driver_cb(stm32_scmi_pm, NULL); -+ - for (i = 0; i < ARRAY_SIZE(scmi_channel); i++) { - struct scmi_msg_channel *chan = &scmi_channel[i]; - -@@ -834,7 +896,7 @@ static TEE_Result stm32mp1_init_scmi_server(void) - /* Sync SCMI clocks with their targeted initial state */ - if (clk->enabled && - stm32mp_nsec_can_access_clock(clk->clock_id)) -- stm32_clock_enable(clk->clock_id); -+ clk_enable(clk->clock_id); - } - - for (j = 0; j < res->rd_count; j++) { -diff --git a/core/arch/arm/plat-stm32mp1/shared_resources.c b/core/arch/arm/plat-stm32mp1/shared_resources.c -index 3d1609353..7ad3d7b9c 100644 ---- a/core/arch/arm/plat-stm32mp1/shared_resources.c -+++ b/core/arch/arm/plat-stm32mp1/shared_resources.c -@@ -1,14 +1,14 @@ - // SPDX-License-Identifier: BSD-3-Clause - /* -- * Copyright (c) 2017-2019, STMicroelectronics -+ * Copyright (c) 2017-2020, STMicroelectronics - */ - - #include - #include --#include - #include - #include - #include -+#include - #include - #include - #include -@@ -147,6 +147,9 @@ static const char __maybe_unused *shres2str_id_tbl[STM32MP1_SHRES_COUNT] = { - - static __maybe_unused const char *shres2str_id(enum stm32mp_shres id) - { -+ if (id >= ARRAY_SIZE(shres2str_id_tbl)) -+ panic("Invalid shared resource ID"); -+ - return shres2str_id_tbl[id]; - } - -@@ -407,6 +410,91 @@ void stm32mp_register_non_secure_gpio(unsigned int bank, unsigned int pin) - } - } - -+#ifdef CFG_STM32_ETZPC -+struct shres2decprot { -+ enum stm32mp_shres shres_id; -+ unsigned int decprot_id; -+}; -+ -+#define SHRES2DECPROT(shres, decprot) { \ -+ .shres_id = shres, \ -+ .decprot_id = decprot, \ -+ } -+ -+static const struct shres2decprot shres2decprot_tbl[] = { -+ SHRES2DECPROT(STM32MP1_SHRES_IWDG1, STM32MP1_ETZPC_IWDG1_ID), -+ SHRES2DECPROT(STM32MP1_SHRES_USART1, STM32MP1_ETZPC_USART1_ID), -+ SHRES2DECPROT(STM32MP1_SHRES_SPI6, STM32MP1_ETZPC_SPI6_ID), -+ SHRES2DECPROT(STM32MP1_SHRES_I2C4, STM32MP1_ETZPC_I2C4_ID), -+ SHRES2DECPROT(STM32MP1_SHRES_RNG1, STM32MP1_ETZPC_RNG1_ID), -+ SHRES2DECPROT(STM32MP1_SHRES_HASH1, STM32MP1_ETZPC_HASH1_ID), -+ SHRES2DECPROT(STM32MP1_SHRES_CRYP1, STM32MP1_ETZPC_CRYP1_ID), -+ SHRES2DECPROT(STM32MP1_SHRES_I2C6, STM32MP1_ETZPC_I2C6_ID), -+}; -+ -+static enum stm32mp_shres decprot2shres(unsigned int decprot_id) -+{ -+ size_t i = 0; -+ -+ for (i = 0; i < ARRAY_SIZE(shres2decprot_tbl); i++) -+ if (shres2decprot_tbl[i].decprot_id == decprot_id) -+ return shres2decprot_tbl[i].shres_id; -+ -+ DMSG("No shared resource for DECPROT ID %u", decprot_id); -+ return STM32MP1_SHRES_COUNT; -+} -+ -+void stm32mp_register_etzpc_decprot(unsigned int id, -+ enum etzpc_decprot_attributes attr) -+{ -+ enum shres_state state = SHRES_SECURE; -+ bool write_secure = false; -+ unsigned int id_shres = STM32MP1_SHRES_COUNT; -+ -+ switch (attr) { -+ case ETZPC_DECPROT_S_RW: -+ state = SHRES_SECURE; -+ break; -+ case ETZPC_DECPROT_NS_R_S_W: -+ case ETZPC_DECPROT_MCU_ISOLATION: -+ case ETZPC_DECPROT_NS_RW: -+ state = SHRES_NON_SECURE; -+ break; -+ default: -+ panic(); -+ } -+ -+ write_secure = attr == ETZPC_DECPROT_NS_R_S_W || -+ attr == ETZPC_DECPROT_S_RW; -+ -+ switch (id) { -+ case STM32MP1_ETZPC_STGENC_ID: -+ case STM32MP1_ETZPC_BKPSRAM_ID: -+ if (state != SHRES_SECURE) -+ panic("ETZPC: STGEN & BKSRAM should be secure"); -+ break; -+ case STM32MP1_ETZPC_DDRCTRL_ID: -+ case STM32MP1_ETZPC_DDRPHYC_ID: -+ /* We assume these must always be write-only to secure world */ -+ if (!write_secure) -+ panic("ETZPC: DDRCTRL & DDRPHY should be write secure"); -+ break; -+ default: -+ id_shres = decprot2shres(id); -+ if (id_shres >= STM32MP1_SHRES_COUNT) { -+ if (write_secure) { -+ DMSG("ETZPC: cannot secure DECPROT %s", -+ shres2str_id(id_shres)); -+ panic(); -+ } -+ } else { -+ register_periph(id_shres, state); -+ } -+ break; -+ } -+} -+#endif /*CFG_STM32_ETZPC*/ -+ - static void lock_registering(void) - { - registering_locked = true; -@@ -614,13 +702,39 @@ static void config_lock_decprot(uint32_t decprot_id, - etzpc_lock_decprot(decprot_id); - } - -+static bool decprot_is_write_secure(uint32_t decprot_id) -+{ -+ enum etzpc_decprot_attributes attr = etzpc_get_decprot(decprot_id); -+ -+ return attr == ETZPC_DECPROT_S_RW || -+ attr == ETZPC_DECPROT_NS_R_S_W; -+} -+ -+static bool decprot_is_secure(uint32_t decprot_id) -+{ -+ enum etzpc_decprot_attributes attr = etzpc_get_decprot(decprot_id); -+ -+ return attr == ETZPC_DECPROT_S_RW; -+} -+ - static void set_etzpc_secure_configuration(void) - { - /* Some peripherals shall be secure */ -- config_lock_decprot(STM32MP1_ETZPC_STGENC_ID, ETZPC_DECPROT_S_RW); -- config_lock_decprot(STM32MP1_ETZPC_BKPSRAM_ID, ETZPC_DECPROT_S_RW); -- config_lock_decprot(STM32MP1_ETZPC_DDRCTRL_ID, ETZPC_DECPROT_S_RW); -- config_lock_decprot(STM32MP1_ETZPC_DDRPHYC_ID, ETZPC_DECPROT_S_RW); -+ if (!decprot_is_secure(STM32MP1_ETZPC_STGENC_ID)) -+ config_lock_decprot(STM32MP1_ETZPC_STGENC_ID, -+ ETZPC_DECPROT_S_RW); -+ -+ if (!decprot_is_secure(STM32MP1_ETZPC_BKPSRAM_ID)) -+ config_lock_decprot(STM32MP1_ETZPC_BKPSRAM_ID, -+ ETZPC_DECPROT_S_RW); -+ -+ if (!decprot_is_write_secure(STM32MP1_ETZPC_DDRCTRL_ID)) -+ config_lock_decprot(STM32MP1_ETZPC_DDRCTRL_ID, -+ ETZPC_DECPROT_NS_R_S_W); -+ -+ if (!decprot_is_write_secure(STM32MP1_ETZPC_DDRPHYC_ID)) -+ config_lock_decprot(STM32MP1_ETZPC_DDRPHYC_ID, -+ ETZPC_DECPROT_NS_R_S_W); - - /* Configure ETZPC with peripheral registering */ - config_lock_decprot(STM32MP1_ETZPC_IWDG1_ID, -@@ -651,27 +765,35 @@ static void check_rcc_secure_configuration(void) - { - bool secure = stm32_rcc_is_secure(); - bool mckprot = stm32_rcc_is_mckprot(); -+ bool need_secure = false; -+ bool need_mckprot = false; - enum stm32mp_shres id = STM32MP1_SHRES_COUNT; -- bool have_error = false; - -- if (stm32mp_is_closed_device() && !secure) -- panic(); -+ if (stm32mp_is_closed_device() && !secure) { -+ DMSG("stm32mp1: closed device mandates OP-TEE to secure RCC"); -+ need_secure = true; -+ } - - for (id = 0; id < STM32MP1_SHRES_COUNT; id++) { - if (shres_state[id] != SHRES_SECURE) - continue; - -- if ((mckprot_resource(id) && !mckprot) || !secure) { -- EMSG("RCC %s MCKPROT %s and %s (%u) secure", -- secure ? "secure" : "non-secure", -- mckprot ? "set" : "not set", -- shres2str_id(id), id); -- have_error = true; -- } -+ need_secure = true; -+ if (mckprot_resource(id)) -+ need_mckprot = true; -+ -+ if ((mckprot_resource(id) && !mckprot) || !secure) -+ EMSG("Error: RCC TZEN=%u MCKPROT=%u while %s is secure", -+ secure, mckprot, shres2str_id(id)); - } - -- if (have_error) -+ DMSG("RCC/PWR secure hardening: TZEN %sable, MCKPROT %sable", -+ secure ? "en" : "dis", mckprot ? "en" : "dis"); -+ -+ if (need_secure && !secure) - panic(); -+ -+ stm32mp1_clk_mcuss_protect(need_mckprot); - } - - static void set_gpio_secure_configuration(void) -diff --git a/core/arch/arm/plat-stm32mp1/stm32_util.h b/core/arch/arm/plat-stm32mp1/stm32_util.h -index 6e4ac7499..efb364998 100644 ---- a/core/arch/arm/plat-stm32mp1/stm32_util.h -+++ b/core/arch/arm/plat-stm32mp1/stm32_util.h -@@ -1,6 +1,6 @@ - /* SPDX-License-Identifier: BSD-3-Clause */ - /* -- * Copyright (c) 2018-2019, STMicroelectronics -+ * Copyright (c) 2018-2020, STMicroelectronics - */ - - #ifndef __STM32_UTIL_H__ -@@ -9,11 +9,23 @@ - #include - #include - #include -+#include - #include - #include - -+/* SoC versioning and device ID */ -+TEE_Result stm32mp1_dbgmcu_get_chip_version(uint32_t *chip_version); -+TEE_Result stm32mp1_dbgmcu_get_chip_dev_id(uint32_t *chip_dev_id); -+ -+/* OPP service */ -+bool stm32mp_supports_cpu_opp(uint32_t opp_id); -+ - /* Backup registers and RAM utils */ - vaddr_t stm32mp_bkpreg(unsigned int idx); -+vaddr_t stm32mp_bkpsram_base(void); -+ -+/* Platform util for the STGEN driver */ -+vaddr_t stm32mp_stgen_base(void); - - /* - * SYSCFG IO compensation. -@@ -25,6 +37,7 @@ void stm32mp_syscfg_disable_io_compensation(void); - /* Platform util for the GIC */ - vaddr_t get_gicc_base(void); - vaddr_t get_gicd_base(void); -+void stm32mp_gic_set_end_of_interrupt(uint32_t it); - - /* - * Platform util functions for the GPIO driver -@@ -43,6 +56,18 @@ vaddr_t stm32_get_gpio_bank_base(unsigned int bank); - unsigned int stm32_get_gpio_bank_offset(unsigned int bank); - unsigned int stm32_get_gpio_bank_clock(unsigned int bank); - -+/* -+ * Platform util for the IWDG driver -+ */ -+ -+/* Get IWDG_* enable flags mask from watchdog configuration read from fuses */ -+unsigned long stm32_get_iwdg_otp_config(vaddr_t pbase); -+ -+/* Conversion between IWDG instance IDs and hardware resources */ -+size_t stm32mp_iwdg_instance2irq(unsigned int instance); -+unsigned int stm32mp_iwdg_irq2instance(size_t irq); -+unsigned int stm32mp_iwdg_iomem2instance(vaddr_t pbase); -+ - /* Platform util for PMIC support */ - bool stm32mp_with_pmic(void); - -@@ -62,15 +87,6 @@ static inline void stm32mp_register_online_cpu(void) - uint32_t may_spin_lock(unsigned int *lock); - void may_spin_unlock(unsigned int *lock, uint32_t exceptions); - --/* -- * Util for clock gating and to get clock rate for stm32 and platform drivers -- * @id: Target clock ID, ID used in clock DT bindings -- */ --void stm32_clock_enable(unsigned long id); --void stm32_clock_disable(unsigned long id); --unsigned long stm32_clock_get_rate(unsigned long id); --bool stm32_clock_is_enabled(unsigned long id); -- - /* Return true if @clock_id is shared by secure and non-secure worlds */ - bool stm32mp_nsec_can_access_clock(unsigned long clock_id); - -@@ -84,6 +100,22 @@ static inline bool stm32mp_nsec_can_access_pmic_regu(const char *name __unused) - } - #endif - -+/* PM sequences specific to SoC STOP mode support */ -+void stm32mp1_clk_save_context_for_stop(void); -+void stm32mp1_clk_restore_context_for_stop(void); -+ -+/* Protect the MCU clock subsytem */ -+void stm32mp1_clk_mcuss_protect(bool enable); -+ -+/* -+ * Util for PLL1 settings management based on DT OPP table content. -+ */ -+int stm32mp1_clk_compute_all_pll1_settings(uint32_t buck1_voltage); -+void stm32mp1_clk_lp_save_opp_pll1_settings(uint8_t *data, size_t size); -+bool stm32mp1_clk_pll1_settings_are_valid(void); -+int stm32mp1_set_opp_khz(uint32_t freq_khz); -+int stm32mp1_round_opp_khz(uint32_t *freq_khz); -+ - /* - * Util for reset signal assertion/desassertion for stm32 and platform drivers - * @id: Target peripheral ID, ID used in reset DT bindings -@@ -123,6 +155,27 @@ struct stm32_bsec_static_cfg { - - void stm32mp_get_bsec_static_cfg(struct stm32_bsec_static_cfg *cfg); - -+/* Reset function for early watchdog management */ -+void stm32mp_platform_reset(int cpu); -+ -+/* Clock calibration. Returns 0 on success */ -+#ifdef CFG_STM32_CLKCALIB -+int stm32mp_start_clock_calib(unsigned int clock_id); -+#else -+static inline int stm32mp_start_clock_calib(unsigned int clock_id __unused) -+{ -+ return 1; -+} -+#endif -+ -+/* Platform util for the RTC driver */ -+bool stm32_rtc_get_read_twice(void); -+ -+/* Platform util for the ETZPC driver */ -+ -+/* Convert a ETZPC mode from DT binding to ETZPC DECPROT configuration */ -+enum etzpc_decprot_attributes stm32mp_etzpc_binding2decprot(uint32_t mode); -+ - /* - * Return true if platform is in closed_device mode - */ -@@ -294,4 +347,8 @@ bool stm32mp_gpio_bank_is_non_secure(unsigned int bank); - /* Register parent clocks of @clock (ID used in clock DT bindings) as secure */ - void stm32mp_register_clock_parents_secure(unsigned long clock_id); - -+/* Register a resource identified by a ETZPC DECPROT identifier */ -+void stm32mp_register_etzpc_decprot(unsigned int id, -+ enum etzpc_decprot_attributes attr); -+ - #endif /*__STM32_UTIL_H__*/ -diff --git a/core/arch/arm/plat-stm32mp1/stm32mp_pm.h b/core/arch/arm/plat-stm32mp1/stm32mp_pm.h -new file mode 100644 -index 000000000..4265db9dd ---- /dev/null -+++ b/core/arch/arm/plat-stm32mp1/stm32mp_pm.h -@@ -0,0 +1,21 @@ -+/* SPDX-License-Identifier: BSD-2-Clause */ -+/* -+ * Copyright (c) 2017-2018, STMicroelectronics -+ */ -+#ifndef __STM32MP_PM_H__ -+#define __STM32MP_PM_H__ -+ -+#define PSCI_MODE_SYSTEM_SUSPEND 0 -+#define PSCI_MODE_SYSTEM_OFF 1 -+ -+enum stm32mp1_pm_domain { -+ STM32MP1_PD_VSW, -+ STM32MP1_PD_CORE_RET, -+ STM32MP1_PD_CORE, -+ STM32MP1_PD_MAX_PM_DOMAIN -+}; -+ -+void __noreturn stm32_cores_reset(void); -+ -+#endif /*__STM32MP_PM_H__*/ -+ -diff --git a/core/arch/arm/plat-stm32mp1/sub.mk b/core/arch/arm/plat-stm32mp1/sub.mk -index 69df1ced5..ae2041741 100644 ---- a/core/arch/arm/plat-stm32mp1/sub.mk -+++ b/core/arch/arm/plat-stm32mp1/sub.mk -@@ -6,6 +6,15 @@ srcs-$(CFG_STM32_RNG) += rng_seed.c - srcs-y += scmi_server.c - srcs-y += shared_resources.c - srcs-$(CFG_TZC400) += plat_tzc400.c -+srcs-$(CFG_RPROC_PTA) += remoteproc_pta.c -+ -+ifeq ($(CFG_RPROC_PTA),y) -+gensrcs-y += rproc_pub_key -+produce-rproc_pub_key = rproc_pub_key.c -+depends-rproc_pub_key = $(CFG_RPROC_SIGN_KEY) scripts/pem_to_pub_c.py -+recipe-rproc_pub_key = $(PYTHON3) scripts/pem_to_pub_c.py --prefix rproc_pub_key \ -+ --key $(CFG_RPROC_SIGN_KEY) --out $(sub-dir-out)/rproc_pub_key.c -+endif - - subdirs-y += drivers - subdirs-y += nsec-service -diff --git a/core/arch/arm/sm/pm_a32.S b/core/arch/arm/sm/pm_a32.S -index a927b1e77..3e0080af4 100644 ---- a/core/arch/arm/sm/pm_a32.S -+++ b/core/arch/arm/sm/pm_a32.S -@@ -65,25 +65,30 @@ a9_suspend: - a7_suspend: - read_fcseidr r4 - read_tpidruro r5 -- stmia r0!, {r4 - r5} -- read_dacr r4 -+ read_dacr r6 -+ stmia r0!, {r4 - r6} - #ifdef CFG_WITH_LPAE --#error "Not supported" -+ read_ttbr0_64bit r4, r5 -+ read_ttbr1_64bit r6, r7 -+ read_mair0 r8 -+ read_mair1 r9 -+ stmia r0!, {r4 - r9} - #else -- read_ttbr0 r5 -- read_ttbr1 r6 -- read_ttbcr r7 -+ read_ttbr0 r4 -+ read_ttbr1 r5 -+ read_prrr r6 -+ read_nmrr r7 -+ stmia r0!, {r4 - r7} - #endif -- read_sctlr r8 -- read_actlr r9 -- read_cpacr r10 -- read_mvbar r11 -- stmia r0!, {r4 - r11} -- read_prrr r4 -- read_nmrr r5 -- read_vbar r6 -- read_nsacr r7 -- stmia r0, {r4 - r7} -+ read_ttbcr r4 -+ read_actlr r5 -+ read_cpacr r6 -+ read_mvbar r7 -+ read_vbar r8 -+ read_nsacr r9 -+ read_sctlr r10 -+ stmia r0, {r4 - r10} -+ - pop {r4 - r11} - bx lr - END_FUNC sm_pm_cpu_do_suspend -@@ -146,50 +151,50 @@ UNWIND( .cantunwind) - cmp r5, r4 - beq a7_resume - -- /* -- * A9 needs PCR/DIAG -- */ -+ /* A9 needs PCR/DIAG */ - ldmia r0!, {r4 - r5} - write_pcr r4 - write_diag r5 -- - a7_resume: -- /* v7 resume */ -+ /* Armv7 generic resume */ - mov ip, #0 - /* Invalidate icache to PoU */ - write_iciallu - /* set reserved context */ - write_contextidr ip -- ldmia r0!, {r4 - r5} -+ ldmia r0!, {r4 - r6} - write_fcseidr r4 - write_tpidruro r5 -- ldmia r0!, {r4 - r11} - /* Invalidate entire TLB */ - write_tlbiall -- write_dacr r4 -+ write_dacr r6 - #ifdef CFG_WITH_LPAE --#error "Not supported -" -+ ldmia r0!, {r4 - r9} -+ write_ttbr0_64bit r4, r5 -+ write_ttbr1_64bit r6, r7 -+ write_mair0 r8 -+ write_mair1 r9 - #else -- write_ttbr0 r5 -- write_ttbr1 r6 -- write_ttbcr r7 -+ ldmia r0!, {r4 - r7} -+ write_ttbr0 r4 -+ write_ttbr1 r5 -+ write_prrr r6 -+ write_nmrr r7 - #endif -- -- ldmia r0, {r4 - r7} -- write_prrr r4 -- write_nmrr r5 -- write_vbar r6 -- write_nsacr r7 -- -- write_actlr r9 -- write_cpacr r10 -- write_mvbar r11 -+ ldmia r0!, {r4 - r10} -+ write_ttbcr r4 -+ write_actlr r5 -+ write_cpacr r6 -+ write_mvbar r7 -+ write_vbar r8 -+ write_nsacr r9 - write_bpiall - isb - dsb - /* MMU will be enabled here */ -- write_sctlr r8 -+ write_sctlr r10 - isb -+ - mov r0, #0 - b suspend_return - END_FUNC sm_pm_cpu_do_resume -diff --git a/core/arch/arm/tee/entry_std.c b/core/arch/arm/tee/entry_std.c -index 424c4c677..92db391d4 100644 ---- a/core/arch/arm/tee/entry_std.c -+++ b/core/arch/arm/tee/entry_std.c -@@ -588,4 +588,4 @@ static TEE_Result default_mobj_init(void) - return TEE_SUCCESS; - } - --driver_init_late(default_mobj_init); -+service_init(default_mobj_init); -diff --git a/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_clk.c b/core/drivers/clk/clk-stm32mp15.c -similarity index 50% -rename from core/arch/arm/plat-stm32mp1/drivers/stm32mp1_clk.c -rename to core/drivers/clk/clk-stm32mp15.c -index c8dbbb7ae..794fd5e75 100644 ---- a/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_clk.c -+++ b/core/drivers/clk/clk-stm32mp15.c -@@ -1,25 +1,47 @@ - // SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0+) - /* -- * Copyright (C) 2018-2019, STMicroelectronics -+ * Copyright (C) 2018-2020, STMicroelectronics - */ - - #include -+#include - #include - #include -+#include - #include - #include - #include -+#include - #include - #include - #include -+#include - #include - #include - #include - #include - #include -+#include - #include - #include - -+#define DT_OPP_COMPAT "operating-points-v2" -+ -+/* PLL settings computation related definitions */ -+#define POST_DIVM_MIN 8000000 -+#define POST_DIVM_MAX 16000000 -+#define DIVM_MIN 0 -+#define DIVM_MAX 63 -+#define DIVN_MIN 24 -+#define DIVN_MAX 99 -+#define DIVP_MIN 0 -+#define DIVP_MAX 127 -+#define FRAC_MAX 8192 -+#define VCO_MIN 800000000 -+#define VCO_MAX 1600000000 -+ -+#define PLL1_SETTINGS_VALID_ID 0x504C4C31 /* "PLL1" */ -+ - /* Identifiers for root oscillators */ - enum stm32mp_osc_id { - _HSI = 0, -@@ -85,11 +107,14 @@ enum stm32mp1_parent_sel { - _UART24_SEL, - _UART35_SEL, - _UART78_SEL, -+ _SDMMC12_SEL, -+ _SDMMC3_SEL, - _AXISS_SEL, - _MCUSS_SEL, - _USBPHY_SEL, - _USBO_SEL, - _RTC_SEL, -+ _MPU_SEL, - _PARENT_SEL_NB, - _UNKNOWN_SEL = 0xff, - }; -@@ -160,6 +185,16 @@ enum stm32mp1_div_id { - _DIV_NB, - }; - -+enum stm32mp1_pllcfg { -+ PLLCFG_M, -+ PLLCFG_N, -+ PLLCFG_P, -+ PLLCFG_Q, -+ PLLCFG_R, -+ PLLCFG_O, -+ PLLCFG_NB -+}; -+ - enum stm32mp1_plltype { - PLL_800, - PLL_1600, -@@ -212,6 +247,21 @@ struct stm32mp1_clk_pll { - enum stm32mp_osc_id refclk[REFCLK_SIZE]; - }; - -+struct stm32mp1_pll { -+ uint8_t refclk_min; -+ uint8_t refclk_max; -+ uint8_t divn_max; -+}; -+ -+/* Compact structure of 32bit cells, copied raw when suspending */ -+struct stm32mp1_pll_settings { -+ uint32_t valid_id; -+ uint32_t freq[PLAT_MAX_OPP_NB]; -+ uint32_t volt[PLAT_MAX_OPP_NB]; -+ uint32_t cfg[PLAT_MAX_OPP_NB][PLAT_MAX_PLLCFG_NB]; -+ uint32_t frac[PLAT_MAX_OPP_NB]; -+}; -+ - #define N_S 0 /* Non-secure can access RCC interface */ - #define SEC 1 /* RCC[TZEN] protects RCC interface */ - -@@ -380,6 +430,18 @@ static const struct stm32mp1_clk_gate stm32mp1_clk_gate[] = { - _CLK_SC_FIXED(N_S, RCC_MP_APB3ENSETR, 11, SYSCFG, _UNKNOWN_ID), - _CLK_SC_SELEC(N_S, RCC_MP_APB4ENSETR, 8, DDRPERFM, _UNKNOWN_SEL), - _CLK_SC_SELEC(N_S, RCC_MP_APB4ENSETR, 15, IWDG2, _UNKNOWN_SEL), -+#ifdef STM32MP1_USE_MPU0_RESET -+ _CLK_SC_SELEC(N_S, RCC_MP_APB4ENSETR, 0, LTDC_PX, _UNKNOWN_SEL), -+ _CLK_SC_SELEC(N_S, RCC_MP_AHB2ENSETR, 0, DMA1, _UNKNOWN_SEL), -+ _CLK_SC_SELEC(N_S, RCC_MP_AHB2ENSETR, 1, DMA2, _UNKNOWN_SEL), -+ _CLK_SC_SELEC(N_S, RCC_MP_AHB2ENSETR, 8, USBO_K, _USBO_SEL), -+ _CLK_SC_SELEC(N_S, RCC_MP_AHB2ENSETR, 16, SDMMC3_K, _SDMMC3_SEL), -+ _CLK_SC_SELEC(N_S, RCC_MP_AHB6ENSETR, 5, GPU, _UNKNOWN_SEL), -+ _CLK_SC_FIXED(N_S, RCC_MP_AHB6ENSETR, 10, ETHMAC, _ACLK), -+ _CLK_SC_SELEC(N_S, RCC_MP_AHB6ENSETR, 16, SDMMC1_K, _SDMMC12_SEL), -+ _CLK_SC_SELEC(N_S, RCC_MP_AHB6ENSETR, 17, SDMMC2_K, _SDMMC12_SEL), -+ _CLK_SC_SELEC(N_S, RCC_MP_AHB6ENSETR, 24, USBH, _UNKNOWN_SEL), -+#endif - - _CLK_SELEC(N_S, RCC_DBGCFGR, 8, CK_DBG, _UNKNOWN_SEL), - }; -@@ -406,6 +468,10 @@ static const uint8_t rng1_parents[] = { - _CSI, _PLL4_R, _LSE, _LSI - }; - -+static const uint8_t mpu_parents[] = { -+ _HSI, _HSE, _PLL1_P, _PLL1_P /* specific div */ -+}; -+ - /* Parents for (some) non-secure clocks */ - #ifdef CFG_WITH_NSEC_UARTS - static const uint8_t uart6_parents[] = { -@@ -429,6 +495,24 @@ static const uint8_t rtc_parents[] = { - _UNKNOWN_ID, _LSE, _LSI, _HSE - }; - -+#ifdef STM32MP1_USE_MPU0_RESET -+static const uint8_t usbphy_parents[] = { -+ _HSE_KER, _PLL4_R, _HSE_KER_DIV2 -+}; -+ -+static const uint8_t usbo_parents[] = { -+ _PLL4_R, _USB_PHY_48 -+}; -+ -+static const uint8_t sdmmc12_parents[] = { -+ _HCLK6, _PLL3_R, _PLL4_P, _HSI_KER -+}; -+ -+static const uint8_t sdmmc3_parents[] = { -+ _HCLK2, _PLL3_R, _PLL4_P, _HSI_KER -+}; -+#endif -+ - static const struct stm32mp1_clk_sel stm32mp1_clk_sel[_PARENT_SEL_NB] = { - /* Secure aware clocks */ - _CLK_PARENT(_STGEN_SEL, RCC_STGENCKSELR, 0, 0x3, stgen_parents), -@@ -437,6 +521,9 @@ static const struct stm32mp1_clk_sel stm32mp1_clk_sel[_PARENT_SEL_NB] = { - _CLK_PARENT(_USART1_SEL, RCC_UART1CKSELR, 0, 0x7, usart1_parents), - _CLK_PARENT(_RNG1_SEL, RCC_RNG1CKSELR, 0, 0x3, rng1_parents), - _CLK_PARENT(_RTC_SEL, RCC_BDCR, 0, 0x3, rtc_parents), -+ _CLK_PARENT(_MPU_SEL, RCC_MPCKSELR, 0, 0x3, mpu_parents), -+ _CLK_PARENT(_AXISS_SEL, RCC_ASSCKSELR, 0, 0x3, axiss_parents), -+ _CLK_PARENT(_MCUSS_SEL, RCC_MSSCKSELR, 0, 0x3, mcuss_parents), - /* Always non-secure clocks (maybe used in some way in secure world) */ - #ifdef CFG_WITH_NSEC_UARTS - _CLK_PARENT(_UART6_SEL, RCC_UART6CKSELR, 0, 0x7, uart6_parents), -@@ -444,8 +531,26 @@ static const struct stm32mp1_clk_sel stm32mp1_clk_sel[_PARENT_SEL_NB] = { - _CLK_PARENT(_UART35_SEL, RCC_UART35CKSELR, 0, 0x7, uart234578_parents), - _CLK_PARENT(_UART78_SEL, RCC_UART78CKSELR, 0, 0x7, uart234578_parents), - #endif -- _CLK_PARENT(_AXISS_SEL, RCC_ASSCKSELR, 0, 0x3, axiss_parents), -- _CLK_PARENT(_MCUSS_SEL, RCC_MSSCKSELR, 0, 0x3, mcuss_parents), -+#ifdef STM32MP1_USE_MPU0_RESET -+ _CLK_PARENT(_SDMMC12_SEL, RCC_SDMMC12CKSELR, 0, 0x7, sdmmc12_parents), -+ _CLK_PARENT(_SDMMC3_SEL, RCC_SDMMC3CKSELR, 0, 0x7, sdmmc3_parents), -+ _CLK_PARENT(_USBPHY_SEL, RCC_USBCKSELR, 0, 0x3, usbphy_parents), -+ _CLK_PARENT(_USBO_SEL, RCC_USBCKSELR, 4, 0x1, usbo_parents), -+#endif -+}; -+ -+/* Define characteristics of PLL according type */ -+static const struct stm32mp1_pll stm32mp1_pll[PLL_TYPE_NB] = { -+ [PLL_800] = { -+ .refclk_min = 4, -+ .refclk_max = 16, -+ .divn_max = 99, -+ }, -+ [PLL_1600] = { -+ .refclk_min = 8, -+ .refclk_max = 16, -+ .divn_max = 199, -+ }, - }; - - /* PLLNCFGR2 register divider by output */ -@@ -549,6 +654,10 @@ static unsigned long osc_frequency(enum stm32mp_osc_id idx) - static unsigned int gate_refcounts[NB_GATES]; - static unsigned int refcount_lock; - -+/* Storage of the precomputed SoC settings for PLL1 various OPPs */ -+static struct stm32mp1_pll_settings pll1_settings; -+static uint32_t current_opp_khz; -+ - static const struct stm32mp1_clk_gate *gate_ref(unsigned int idx) - { - return &stm32mp1_clk_gate[idx]; -@@ -591,7 +700,7 @@ static enum stm32mp1_parent_id stm32mp1_clk_get_fixed_parent(int i) - return (enum stm32mp1_parent_id)gate_ref(i)->fixed; - } - --static int stm32mp1_clk_get_parent(unsigned long id) -+static int __clk_get_parent(unsigned long id) - { - const struct stm32mp1_clk_sel *sel = NULL; - enum stm32mp1_parent_id parent_id = 0; -@@ -701,7 +810,153 @@ static unsigned long stm32mp1_read_pll_freq(enum stm32mp1_pll_id pll_id, - return dfout; - } - --static unsigned long get_clock_rate(int p) -+static void pll_start(enum stm32mp1_pll_id pll_id) -+{ -+ const struct stm32mp1_clk_pll *pll = pll_ref(pll_id); -+ uint32_t pllxcr = stm32_rcc_base() + pll->pllxcr; -+ -+ if (io_read32(pllxcr) & RCC_PLLNCR_PLLON) -+ return; -+ -+ io_clrsetbits32(pllxcr, RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN | -+ RCC_PLLNCR_DIVREN, RCC_PLLNCR_PLLON); -+} -+ -+#define PLLRDY_TIMEOUT_US (200 * 1000) -+ -+static int pll_output(enum stm32mp1_pll_id pll_id, uint32_t output) -+{ -+ const struct stm32mp1_clk_pll *pll = pll_ref(pll_id); -+ uint32_t pllxcr = stm32_rcc_base() + pll->pllxcr; -+ uint64_t start = 0; -+ -+ start = timeout_init_us(PLLRDY_TIMEOUT_US); -+ /* Wait PLL lock */ -+ while (!(io_read32(pllxcr) & RCC_PLLNCR_PLLRDY)) -+ if (timeout_elapsed(start)) { -+ EMSG("PLL%d start failed @ 0x%"PRIx32": 0x%"PRIx32, -+ pll_id, pllxcr, io_read32(pllxcr)); -+ return -1; -+ } -+ -+ /* Start the requested output */ -+ io_setbits32(pllxcr, output << RCC_PLLNCR_DIVEN_SHIFT); -+ -+ return 0; -+} -+ -+static int pll_stop(enum stm32mp1_pll_id pll_id) -+{ -+ const struct stm32mp1_clk_pll *pll = pll_ref(pll_id); -+ uint32_t pllxcr = stm32_rcc_base() + pll->pllxcr; -+ uint64_t start = 0; -+ -+ /* Stop all output */ -+ io_clrbits32(pllxcr, RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN | -+ RCC_PLLNCR_DIVREN); -+ -+ /* Stop PLL */ -+ io_clrbits32(pllxcr, RCC_PLLNCR_PLLON); -+ -+ start = timeout_init_us(PLLRDY_TIMEOUT_US); -+ /* Wait PLL stopped */ -+ while (!(io_read32(pllxcr) & RCC_PLLNCR_PLLRDY)) -+ if (timeout_elapsed(start)) { -+ EMSG("PLL%d stop failed @ 0x%"PRIx32": 0x%"PRIx32, -+ pll_id, pllxcr, io_read32(pllxcr)); -+ -+ return -1; -+ } -+ -+ return 0; -+} -+ -+static uint32_t pll_compute_pllxcfgr2(uint32_t *pllcfg) -+{ -+ uint32_t value = 0; -+ -+ value = (pllcfg[PLLCFG_P] << RCC_PLLNCFGR2_DIVP_SHIFT) & -+ RCC_PLLNCFGR2_DIVP_MASK; -+ value |= (pllcfg[PLLCFG_Q] << RCC_PLLNCFGR2_DIVQ_SHIFT) & -+ RCC_PLLNCFGR2_DIVQ_MASK; -+ value |= (pllcfg[PLLCFG_R] << RCC_PLLNCFGR2_DIVR_SHIFT) & -+ RCC_PLLNCFGR2_DIVR_MASK; -+ -+ return value; -+} -+ -+static void pll_config_output(enum stm32mp1_pll_id pll_id, uint32_t *pllcfg) -+{ -+ const struct stm32mp1_clk_pll *pll = pll_ref(pll_id); -+ uintptr_t rcc_base = stm32_rcc_base(); -+ uint32_t value = 0; -+ -+ value = pll_compute_pllxcfgr2(pllcfg); -+ -+ io_write32(rcc_base + pll->pllxcfgr2, value); -+} -+ -+static int pll_compute_pllxcfgr1(const struct stm32mp1_clk_pll *pll, -+ uint32_t *pllcfg, uint32_t *cfgr1) -+{ -+ uint32_t rcc_base = stm32_rcc_base(); -+ enum stm32mp1_plltype type = pll->plltype; -+ unsigned long refclk = 0; -+ uint32_t ifrge = 0; -+ uint32_t src = 0; -+ -+ src = io_read32(rcc_base + pll->rckxselr) & -+ RCC_SELR_REFCLK_SRC_MASK; -+ -+ refclk = osc_frequency(pll->refclk[src]) / -+ (pllcfg[PLLCFG_M] + 1U); -+ -+ if ((refclk < (stm32mp1_pll[type].refclk_min * 1000000U)) || -+ (refclk > (stm32mp1_pll[type].refclk_max * 1000000U))) -+ return -1; -+ -+ if ((type == PLL_800) && (refclk >= 8000000U)) -+ ifrge = 1U; -+ -+ *cfgr1 = (pllcfg[PLLCFG_N] << RCC_PLLNCFGR1_DIVN_SHIFT) & -+ RCC_PLLNCFGR1_DIVN_MASK; -+ *cfgr1 |= (pllcfg[PLLCFG_M] << RCC_PLLNCFGR1_DIVM_SHIFT) & -+ RCC_PLLNCFGR1_DIVM_MASK; -+ *cfgr1 |= (ifrge << RCC_PLLNCFGR1_IFRGE_SHIFT) & -+ RCC_PLLNCFGR1_IFRGE_MASK; -+ -+ return 0; -+} -+ -+static int pll_config(enum stm32mp1_pll_id pll_id, uint32_t *pllcfg, -+ uint32_t fracv) -+{ -+ const struct stm32mp1_clk_pll *pll = pll_ref(pll_id); -+ uint32_t rcc_base = stm32_rcc_base(); -+ uint32_t value = 0; -+ int ret = 0; -+ -+ ret = pll_compute_pllxcfgr1(pll, pllcfg, &value); -+ if (ret) -+ return ret; -+ -+ io_write32(rcc_base + pll->pllxcfgr1, value); -+ -+ /* Fractional configuration */ -+ io_write32(rcc_base + pll->pllxfracr, value); -+ -+ /* Frac must be enabled only once its configuration is loaded */ -+ value = fracv << RCC_PLLNFRACR_FRACV_SHIFT; -+ io_write32(rcc_base + pll->pllxfracr, value); -+ value = io_read32(rcc_base + pll->pllxfracr); -+ io_write32(rcc_base + pll->pllxfracr, value | RCC_PLLNFRACR_FRACLE); -+ -+ pll_config_output(pll_id, pllcfg); -+ -+ return 0; -+} -+ -+static unsigned long __clk_get_parent_rate(enum stm32mp1_parent_id p) - { - uint32_t reg = 0; - unsigned long clock = 0; -@@ -964,7 +1219,7 @@ static bool clock_is_always_on(unsigned long id) - } - } - --bool stm32_clock_is_enabled(unsigned long id) -+static bool clk_stm32_is_enabled(unsigned long id) - { - int i = 0; - -@@ -978,13 +1233,13 @@ bool stm32_clock_is_enabled(unsigned long id) - return __clk_is_enabled(gate_ref(i)); - } - --void stm32_clock_enable(unsigned long id) -+static TEE_Result clk_stm32_enable(unsigned long id) - { - int i = 0; - uint32_t exceptions = 0; - - if (clock_is_always_on(id)) -- return; -+ return TEE_SUCCESS; - - i = stm32mp1_clk_get_gated_id(id); - if (i < 0) { -@@ -995,7 +1250,7 @@ void stm32_clock_enable(unsigned long id) - if (gate_is_non_secure(gate_ref(i))) { - /* Enable non-secure clock w/o any refcounting */ - __clk_enable(gate_ref(i)); -- return; -+ return TEE_SUCCESS; - } - - exceptions = may_spin_lock(&refcount_lock); -@@ -1006,9 +1261,11 @@ void stm32_clock_enable(unsigned long id) - gate_refcounts[i]++; - - may_spin_unlock(&refcount_lock, exceptions); -+ -+ return TEE_SUCCESS; - } - --void stm32_clock_disable(unsigned long id) -+static void clk_stm32_disable(unsigned long id) - { - int i = 0; - uint32_t exceptions = 0; -@@ -1067,16 +1324,16 @@ static long get_timer_rate(long parent_rate, unsigned int apb_bus) - return parent_rate * (timgxpre + 1) * 2; - } - --unsigned long stm32_clock_get_rate(unsigned long id) -+static unsigned long clk_stm32_get_rate(unsigned long id) - { -- int p = 0; -+ enum stm32mp1_parent_id p = _UNKNOWN_ID; - unsigned long rate = 0; - -- p = stm32mp1_clk_get_parent(id); -+ p = __clk_get_parent(id); - if (p < 0) - return 0; - -- rate = get_clock_rate(p); -+ rate = __clk_get_parent_rate(p); - - if ((id >= TIM2_K) && (id <= TIM14_K)) - rate = get_timer_rate(rate, 1); -@@ -1210,7 +1467,7 @@ static void secure_parent_clocks(unsigned long parent_id) - - void stm32mp_register_clock_parents_secure(unsigned long clock_id) - { -- int parent_id = stm32mp1_clk_get_parent(clock_id); -+ enum stm32mp1_parent_id parent_id = __clk_get_parent(clock_id); - - if (parent_id < 0) { - DMSG("No parent for clock %lu", clock_id); -@@ -1220,6 +1477,14 @@ void stm32mp_register_clock_parents_secure(unsigned long clock_id) - secure_parent_clocks(parent_id); - } - -+static const struct clk_ops stm32mp_clk_ops = { -+ .enable = clk_stm32_enable, -+ .disable = clk_stm32_disable, -+ .is_enabled = clk_stm32_is_enabled, -+ .get_rate = clk_stm32_get_rate, -+}; -+DECLARE_KEEP_PAGER(stm32mp_clk_ops); -+ - #ifdef CFG_EMBED_DTB - static const char *stm32mp_osc_node_label[NB_OSC] = { - [_LSI] = "clk-lsi", -@@ -1291,15 +1556,15 @@ static void enable_static_secure_clocks(void) - }; - - for (idx = 0; idx < ARRAY_SIZE(secure_enable); idx++) { -- stm32_clock_enable(secure_enable[idx]); -+ clk_stm32_enable(secure_enable[idx]); - stm32mp_register_clock_parents_secure(secure_enable[idx]); - } - - if (CFG_TEE_CORE_NB_CORE > 1) -- stm32_clock_enable(RTCAPB); -+ clk_stm32_enable(RTCAPB); - } - --static TEE_Result stm32mp1_clk_early_init(void) -+static void stm32mp1_clk_early_init(void) - { - void *fdt = NULL; - int node = 0; -@@ -1308,18 +1573,21 @@ static TEE_Result stm32mp1_clk_early_init(void) - int ignored = 0; - - fdt = get_embedded_dt(); -- node = fdt_node_offset_by_compatible(fdt, -1, DT_RCC_CLK_COMPAT); -- -- if (node < 0 || _fdt_reg_base_address(fdt, node) != RCC_BASE) -- panic(); -- -- if (_fdt_get_status(fdt, node) & DT_STATUS_OK_SEC) { -- io_setbits32(stm32_rcc_base() + RCC_TZCR, RCC_TZCR_TZEN); -+ node = fdt_node_offset_by_compatible(fdt, -1, DT_RCC_SEC_CLK_COMPAT); -+ -+ if (node < 0 || _fdt_reg_base_address(fdt, node) != RCC_BASE) { -+ /* Check non secure compatible */ -+ node = fdt_node_offset_by_compatible(fdt, -1, -+ DT_RCC_CLK_COMPAT); -+ if (node < 0 || _fdt_reg_base_address(fdt, node) != RCC_BASE) { -+ panic(); -+ } else { -+ io_clrbits32(stm32_rcc_base() + RCC_TZCR, -+ RCC_TZCR_TZEN); -+ IMSG("RCC is non secure"); -+ } - } else { -- if (io_read32(stm32_rcc_base() + RCC_TZCR) & RCC_TZCR_TZEN) -- panic("Refuse to release RCC[TZEN]"); -- -- IMSG("RCC is non-secure"); -+ io_setbits32(stm32_rcc_base() + RCC_TZCR, RCC_TZCR_TZEN); - } - - get_osc_freq_from_dt(fdt); -@@ -1360,11 +1628,1024 @@ static TEE_Result stm32mp1_clk_early_init(void) - - if (ignored != 0) - IMSG("DT clock tree configurations were ignored"); -+} - -- enable_static_secure_clocks(); -+/* -+ * Gets OPP parameters (frequency in KHz and voltage in mV) from an OPP table -+ * subnode. Platform HW support capabilities are also checked. -+ */ -+static int get_opp_freqvolt_from_dt_subnode(void *fdt, int subnode, -+ uint32_t *freq_khz, -+ uint32_t *voltage_mv) -+{ -+ const fdt64_t *cuint64 = NULL; -+ const fdt32_t *cuint32 = NULL; -+ uint64_t read_freq_64 = 0; -+ uint32_t read_voltage_32 = 0; -+ -+ assert(freq_khz); -+ assert(voltage_mv); -+ -+ cuint32 = fdt_getprop(fdt, subnode, "opp-supported-hw", NULL); -+ if (cuint32) -+ if (!stm32mp_supports_cpu_opp(fdt32_to_cpu(*cuint32))) { -+ DMSG("Invalid opp-supported-hw 0x%"PRIx32, -+ fdt32_to_cpu(*cuint32)); -+ return -FDT_ERR_BADVALUE; -+ } - -- return TEE_SUCCESS; -+ cuint64 = fdt_getprop(fdt, subnode, "opp-hz", NULL); -+ if (!cuint64) { -+ DMSG("Missing opp-hz"); -+ return -FDT_ERR_NOTFOUND; -+ } -+ -+ /* Frequency value expressed in KHz must fit on 32 bits */ -+ read_freq_64 = fdt64_to_cpu(*cuint64) / 1000ULL; -+ if (read_freq_64 > (uint64_t)UINT32_MAX) { -+ DMSG("Invalid opp-hz %"PRIu64, read_freq_64); -+ return -FDT_ERR_BADVALUE; -+ } -+ -+ cuint32 = fdt_getprop(fdt, subnode, "opp-microvolt", NULL); -+ if (!cuint32) { -+ DMSG("Missing opp-microvolt"); -+ return -FDT_ERR_NOTFOUND; -+ } -+ -+ /* Millivolt value must fit on 16 bits */ -+ read_voltage_32 = fdt32_to_cpu(*cuint32) / 1000U; -+ if (read_voltage_32 > UINT16_MAX) { -+ DMSG("Invalid opp-microvolt %"PRIu32, read_voltage_32); -+ return -FDT_ERR_BADVALUE; -+ } -+ -+ *freq_khz = (uint32_t)read_freq_64; -+ -+ *voltage_mv = read_voltage_32; -+ -+ return 0; -+} -+ -+/* -+ * Parses OPP table in DT and finds all parameters supported by the HW -+ * platform. If found, the corresponding frequency and voltage values are -+ * respectively stored in @pll1_settings structure. -+ * Note that @*count has to be set by caller to the effective size allocated -+ * for both tables. Its value is then replaced by the number of filled elements. -+ */ -+static int get_all_opp_freqvolt_from_dt(uint32_t *count) -+{ -+ void *fdt = NULL; -+ int node = 0; -+ int subnode = 0; -+ uint32_t idx = 0; -+ -+ assert(count); -+ -+ fdt = get_embedded_dt(); -+ node = fdt_node_offset_by_compatible(fdt, -1, DT_OPP_COMPAT); -+ if (node < 0) -+ return node; -+ -+ fdt_for_each_subnode(subnode, fdt, node) { -+ uint32_t read_freq = 0; -+ uint32_t read_voltage = 0; -+ -+ if (get_opp_freqvolt_from_dt_subnode(fdt, subnode, &read_freq, -+ &read_voltage)) -+ continue; -+ -+ if (idx >= *count) -+ return -FDT_ERR_NOSPACE; -+ -+ pll1_settings.freq[idx] = read_freq; -+ pll1_settings.volt[idx] = read_voltage; -+ idx++; -+ } -+ -+ if (!idx) -+ return -FDT_ERR_NOTFOUND; -+ -+ *count = idx; -+ -+ return 0; -+} -+ -+static int clk_compute_pll1_settings(unsigned long input_freq, int idx) -+{ -+ unsigned long post_divm = 0; -+ unsigned long long output_freq = pll1_settings.freq[idx] * 1000U; -+ unsigned long long freq = 0; -+ unsigned long long vco = 0; -+ int divm = 0; -+ int divn = 0; -+ int divp = 0; -+ int frac = 0; -+ int i = 0; -+ unsigned int diff = 0; -+ unsigned int best_diff = UINT_MAX; -+ -+ /* Following parameters have always the same value */ -+ pll1_settings.cfg[idx][PLLCFG_Q] = 0; -+ pll1_settings.cfg[idx][PLLCFG_R] = 0; -+ pll1_settings.cfg[idx][PLLCFG_O] = PQR(1, 0, 0); -+ -+ for (divm = DIVM_MAX; divm >= DIVM_MIN; divm--) { -+ post_divm = input_freq / (unsigned long)(divm + 1); -+ -+ if ((post_divm < POST_DIVM_MIN) || -+ (post_divm > POST_DIVM_MAX)) -+ continue; -+ -+ for (divp = DIVP_MIN; divp <= DIVP_MAX; divp++) { -+ -+ freq = output_freq * (divm + 1) * (divp + 1); -+ -+ divn = (int)((freq / input_freq) - 1); -+ if ((divn < DIVN_MIN) || (divn > DIVN_MAX)) -+ continue; -+ -+ frac = (int)(((freq * FRAC_MAX) / input_freq) - -+ ((divn + 1) * FRAC_MAX)); -+ -+ /* 2 loops to refine the fractional part */ -+ for (i = 2; i != 0; i--) { -+ if (frac > FRAC_MAX) -+ break; -+ -+ vco = (post_divm * (divn + 1)) + -+ ((post_divm * (unsigned long long)frac) / -+ FRAC_MAX); -+ -+ if ((vco < (VCO_MIN / 2)) || -+ (vco > (VCO_MAX / 2))) { -+ frac++; -+ continue; -+ } -+ -+ freq = vco / (divp + 1); -+ if (output_freq < freq) -+ diff = (unsigned int)(freq - -+ output_freq); -+ else -+ diff = (unsigned int)(output_freq - -+ freq); -+ -+ if (diff < best_diff) { -+ pll1_settings.cfg[idx][PLLCFG_M] = divm; -+ pll1_settings.cfg[idx][PLLCFG_N] = divn; -+ pll1_settings.cfg[idx][PLLCFG_P] = divp; -+ pll1_settings.frac[idx] = frac; -+ -+ if (!diff) -+ return 0; -+ -+ best_diff = diff; -+ } -+ -+ frac++; -+ } -+ } -+ } -+ -+ if (best_diff == UINT_MAX) { -+ pll1_settings.cfg[idx][PLLCFG_O] = 0; -+ return -1; -+ } -+ -+ return 0; -+} -+ -+static int clk_get_pll1_settings(uint32_t clksrc, int index) -+{ -+ unsigned long input_freq = 0; -+ unsigned int i = 0; -+ -+ for (i = 0; i < PLAT_MAX_OPP_NB; i++) -+ if (pll1_settings.freq[i] == pll1_settings.freq[index]) -+ break; -+ -+ if (((i == PLAT_MAX_OPP_NB) && -+ !stm32mp1_clk_pll1_settings_are_valid()) || -+ ((i < PLAT_MAX_OPP_NB) && !pll1_settings.cfg[i][PLLCFG_O])) { -+ /* -+ * Either PLL1 settings structure is completely empty, -+ * or these settings are not yet computed: do it. -+ */ -+ switch (clksrc) { -+ case CLK_PLL12_HSI: -+ input_freq = clk_stm32_get_rate(CK_HSI); -+ break; -+ case CLK_PLL12_HSE: -+ input_freq = clk_stm32_get_rate(CK_HSE); -+ break; -+ default: -+ panic(); -+ } -+ -+ return clk_compute_pll1_settings(input_freq, index); -+ } -+ -+ if (i < PLAT_MAX_OPP_NB) { -+ if (pll1_settings.cfg[i][PLLCFG_O]) -+ return 0; -+ -+ /* -+ * Index is in range and PLL1 settings are computed: -+ * use content to answer to the request. -+ */ -+ memcpy(&pll1_settings.cfg[index][0], &pll1_settings.cfg[i][0], -+ sizeof(uint32_t) * PLAT_MAX_PLLCFG_NB); -+ pll1_settings.frac[index] = pll1_settings.frac[i]; -+ -+ return 0; -+ } -+ -+ return -1; -+} -+ -+static int clk_save_current_pll1_settings(uint32_t buck1_voltage) -+{ -+ const struct stm32mp1_clk_pll *pll = pll_ref(_PLL1); -+ uint32_t rcc_base = stm32_rcc_base(); -+ uint32_t freq = 0; -+ unsigned int i = 0; -+ -+ freq = UDIV_ROUND_NEAREST(clk_stm32_get_rate(CK_MPU), 1000L); -+ -+ for (i = 0; i < PLAT_MAX_OPP_NB; i++) -+ if (pll1_settings.freq[i] == freq) -+ break; -+ -+ if ((i == PLAT_MAX_OPP_NB) || -+ ((pll1_settings.volt[i] != buck1_voltage) && buck1_voltage)) -+ return -1; -+ -+ pll1_settings.cfg[i][PLLCFG_M] = (io_read32(rcc_base + pll->pllxcfgr1) & -+ RCC_PLLNCFGR1_DIVM_MASK) >> -+ RCC_PLLNCFGR1_DIVM_SHIFT; -+ -+ pll1_settings.cfg[i][PLLCFG_N] = (io_read32(rcc_base + pll->pllxcfgr1) & -+ RCC_PLLNCFGR1_DIVN_MASK) >> -+ RCC_PLLNCFGR1_DIVN_SHIFT; -+ -+ pll1_settings.cfg[i][PLLCFG_P] = (io_read32(rcc_base + pll->pllxcfgr2) & -+ RCC_PLLNCFGR2_DIVP_MASK) >> -+ RCC_PLLNCFGR2_DIVP_SHIFT; -+ -+ pll1_settings.cfg[i][PLLCFG_Q] = (io_read32(rcc_base + pll->pllxcfgr2) & -+ RCC_PLLNCFGR2_DIVQ_MASK) >> -+ RCC_PLLNCFGR2_DIVQ_SHIFT; -+ -+ pll1_settings.cfg[i][PLLCFG_R] = (io_read32(rcc_base + pll->pllxcfgr2) & -+ RCC_PLLNCFGR2_DIVR_MASK) >> -+ RCC_PLLNCFGR2_DIVR_SHIFT; -+ -+ pll1_settings.cfg[i][PLLCFG_O] = io_read32(rcc_base + pll->pllxcr) >> -+ RCC_PLLNCR_DIVEN_SHIFT; -+ -+ pll1_settings.frac[i] = (io_read32(rcc_base + pll->pllxfracr) & -+ RCC_PLLNFRACR_FRACV_MASK) >> -+ RCC_PLLNFRACR_FRACV_SHIFT; -+ -+ return i; -+} -+ -+static uint32_t stm32mp1_clk_get_pll1_current_clksrc(void) -+{ -+ uint32_t value = 0; -+ const struct stm32mp1_clk_pll *pll = pll_ref(_PLL1); -+ uint32_t rcc_base = stm32_rcc_base(); -+ -+ value = io_read32(rcc_base + pll->rckxselr); -+ -+ switch (value & RCC_SELR_REFCLK_SRC_MASK) { -+ case 0: -+ return CLK_PLL12_HSI; -+ case 1: -+ return CLK_PLL12_HSE; -+ default: -+ panic(); -+ } -+} -+ -+int stm32mp1_clk_compute_all_pll1_settings(uint32_t buck1_voltage) -+{ -+ unsigned int i = 0; -+ int ret = 0; -+ int index = 0; -+ uint32_t count = PLAT_MAX_OPP_NB; -+ uint32_t clksrc = 0; -+ -+ ret = get_all_opp_freqvolt_from_dt(&count); -+ switch (ret) { -+ case 0: -+ break; -+ case -FDT_ERR_NOTFOUND: -+ DMSG("Cannot find all OPP info in DT: use default settings."); -+ return 0; -+ default: -+ EMSG("Inconsistent OPP settings found in DT, ignored."); -+ return 0; -+ } -+ -+ index = clk_save_current_pll1_settings(buck1_voltage); -+ -+ clksrc = stm32mp1_clk_get_pll1_current_clksrc(); -+ -+ for (i = 0; i < count; i++) { -+ if (index >= 0 && i == (unsigned int)index) -+ continue; -+ -+ ret = clk_get_pll1_settings(clksrc, i); -+ if (ret != 0) -+ return ret; -+ } -+ -+ pll1_settings.valid_id = PLL1_SETTINGS_VALID_ID; -+ -+ return 0; -+} -+ -+void stm32mp1_clk_lp_save_opp_pll1_settings(uint8_t *data, size_t size) -+{ -+ if ((size != sizeof(pll1_settings)) || -+ !stm32mp1_clk_pll1_settings_are_valid()) -+ panic(); -+ -+ memcpy(data, &pll1_settings, size); -+} -+ -+bool stm32mp1_clk_pll1_settings_are_valid(void) -+{ -+ return pll1_settings.valid_id == PLL1_SETTINGS_VALID_ID; -+} -+#else -+static void stm32mp1_clk_early_init(void) -+{ -+ vaddr_t rcc_base = stm32_rcc_base(); -+ -+ /* Expect booting from a secure setup */ -+ if ((io_read32(rcc_base + RCC_TZCR) & RCC_TZCR_TZEN) == 0) -+ panic("RCC TZC[TZEN]"); -+} -+ -+int stm32mp1_clk_compute_all_pll1_settings(uint32_t buck1_voltage __unused) -+{ -+ return 0; -+} -+ -+void stm32mp1_clk_lp_save_opp_pll1_settings(uint8_t *data __unused, -+ size_t size __unused) -+{ -+} -+ -+bool stm32mp1_clk_pll1_settings_are_valid(void) -+{ -+ return false; - } - --service_init(stm32mp1_clk_early_init); -+static void enable_static_secure_clocks(void) -+{ -+} - #endif /*CFG_EMBED_DTB*/ -+ -+/* Start MPU OPP */ -+#define CLKSRC_TIMEOUT_US (200 * 1000) -+#define CLKDIV_TIMEOUT_US (200 * 1000) -+#define CLK_MPU_PLL1P 0x00000202 -+#define CLK_MPU_PLL1P_DIV 0x00000203 -+ -+static int stm32mp1_set_clksrc(unsigned int clksrc) -+{ -+ uintptr_t address = stm32_rcc_base() + (clksrc >> 4); -+ uint64_t timeout_ref = 0; -+ -+ io_clrsetbits32(address, RCC_SELR_SRC_MASK, clksrc & RCC_SELR_SRC_MASK); -+ -+ timeout_ref = timeout_init_us(CLKSRC_TIMEOUT_US); -+ while ((io_read32(address) & RCC_SELR_SRCRDY) == 0U) { -+ if (timeout_elapsed(timeout_ref)) { -+ EMSG("CLKSRC %u start failed @ 0x%"PRIxPTR": 0x%"PRIx32, -+ clksrc, address, io_read32(address)); -+ return -1; -+ } -+ } -+ -+ return 0; -+} -+ -+static int stm32mp1_set_clkdiv(unsigned int clkdiv, uintptr_t address) -+{ -+ uint64_t timeout_ref = 0; -+ -+ io_clrsetbits32(address, RCC_DIVR_DIV_MASK, clkdiv & RCC_DIVR_DIV_MASK); -+ -+ timeout_ref = timeout_init_us(CLKDIV_TIMEOUT_US); -+ while ((io_read32(address) & RCC_DIVR_DIVRDY) == 0U) { -+ if (timeout_elapsed(timeout_ref)) { -+ EMSG("CLKDIV 0x%x start failed @ 0x%"PRIxPTR": 0x%"PRIx32, -+ clkdiv, address, io_read32(address)); -+ return -1; -+ } -+ } -+ -+ return 0; -+} -+ -+/* -+ * Check if PLL1 can be configured on the fly. -+ * @result (-1) => config on the fly is not possible. -+ * (0) => config on the fly is possible. -+ * (+1) => same parameters as those in place, no need to reconfig. -+ * Return value is 0 if no error. -+ */ -+static int is_pll_config_on_the_fly(enum stm32mp1_pll_id pll_id, -+ uint32_t *pllcfg, uint32_t fracv, -+ int *result) -+{ -+ const struct stm32mp1_clk_pll *pll = pll_ref(pll_id); -+ uintptr_t rcc_base = stm32_rcc_base(); -+ uint32_t fracr = 0; -+ uint32_t value = 0; -+ int ret = 0; -+ -+ ret = pll_compute_pllxcfgr1(pll, pllcfg, &value); -+ if (ret) -+ return ret; -+ -+ if (io_read32(rcc_base + pll->pllxcfgr1) != value) { -+ /* Different DIVN/DIVM, can't config on the fly */ -+ *result = -1; -+ return 0; -+ } -+ -+ *result = true; -+ -+ fracr = fracv << RCC_PLLNFRACR_FRACV_SHIFT; -+ fracr |= RCC_PLLNFRACR_FRACLE; -+ value = pll_compute_pllxcfgr2(pllcfg); -+ -+ if ((io_read32(rcc_base + pll->pllxfracr) == fracr) && -+ (io_read32(rcc_base + pll->pllxcfgr2) == value)) -+ /* Same parameters, no need to config */ -+ *result = 1; -+ else -+ *result = 0; -+ -+ return 0; -+} -+ -+static int stm32mp1_get_mpu_div(uint32_t freq_khz) -+{ -+ unsigned long freq_pll1_p; -+ unsigned long div; -+ -+ freq_pll1_p = __clk_get_parent_rate(_PLL1_P) / 1000UL; -+ if ((freq_pll1_p % freq_khz) != 0U) -+ return -1; -+ -+ div = freq_pll1_p / freq_khz; -+ -+ switch (div) { -+ case 1UL: -+ case 2UL: -+ case 4UL: -+ case 8UL: -+ case 16UL: -+ return __builtin_ffs(div) - 1; -+ default: -+ return -1; -+ } -+} -+ -+/* Configure PLL1 from input frequency OPP parameters */ -+static int pll1_config_from_opp_khz(uint32_t freq_khz) -+{ -+ unsigned int idx = 0; -+ int ret = 0; -+ int div = 0; -+ int config_on_the_fly = -1; -+ -+ for (idx = 0; idx < PLAT_MAX_OPP_NB; idx++) -+ if (pll1_settings.freq[idx] == freq_khz) -+ break; -+ -+ if (idx == PLAT_MAX_OPP_NB) -+ return -1; -+ -+ div = stm32mp1_get_mpu_div(freq_khz); -+ switch (div) { -+ case -1: -+ break; -+ case 0: -+ return stm32mp1_set_clksrc(CLK_MPU_PLL1P); -+ default: -+ ret = stm32mp1_set_clkdiv(div, stm32_rcc_base() + -+ RCC_MPCKDIVR); -+ if (ret == 0) -+ ret = stm32mp1_set_clksrc(CLK_MPU_PLL1P_DIV); -+ -+ return ret; -+ } -+ -+ ret = is_pll_config_on_the_fly(_PLL1, &pll1_settings.cfg[idx][0], -+ pll1_settings.frac[idx], -+ &config_on_the_fly); -+ if (ret) -+ return ret; -+ -+ if (config_on_the_fly == 1) -+ return 0; -+ -+ if (config_on_the_fly == -1) { -+ /* Switch to HSI and stop PLL1 before reconfiguration */ -+ ret = stm32mp1_set_clksrc(CLK_MPU_HSI); -+ if (ret) -+ return ret; -+ -+ ret = pll_stop(_PLL1); -+ if (ret) -+ return ret; -+ } -+ -+ ret = pll_config(_PLL1, &pll1_settings.cfg[idx][0], -+ pll1_settings.frac[idx]); -+ if (ret) -+ return ret; -+ -+ if (config_on_the_fly == -1) { -+ /* Start PLL1 and switch back to after reconfiguration */ -+ pll_start(_PLL1); -+ -+ ret = pll_output(_PLL1, pll1_settings.cfg[idx][PLLCFG_O]); -+ if (ret) -+ return ret; -+ -+ ret = stm32mp1_set_clksrc(CLK_MPU_PLL1P); -+ if (ret) -+ return ret; -+ } -+ -+ return 0; -+} -+ -+static void save_current_opp(void) -+{ -+ unsigned long freq_khz = UDIV_ROUND_NEAREST(clk_stm32_get_rate(CK_MPU), -+ 1000UL); -+ if (freq_khz > (unsigned long)UINT32_MAX) -+ panic(); -+ -+ current_opp_khz = (uint32_t)freq_khz; -+} -+ -+int stm32mp1_set_opp_khz(uint32_t freq_khz) -+{ -+ uint32_t mpu_src = 0; -+ -+ if (freq_khz == current_opp_khz) -+ return 0; -+ -+ if (!stm32mp1_clk_pll1_settings_are_valid()) { -+ /* -+ * No OPP table in DT or an error occurred during PLL1 -+ * settings computation, system can only work on current -+ * operating point so return error. -+ */ -+ return -1; -+ } -+ -+ /* Check that PLL1 is MPU clock source */ -+ mpu_src = io_read32(stm32_rcc_base() + RCC_MPCKSELR) & -+ RCC_SELR_SRC_MASK; -+ if ((mpu_src != RCC_MPCKSELR_PLL) && -+ (mpu_src != RCC_MPCKSELR_PLL_MPUDIV)) -+ return -1; -+ -+ if (pll1_config_from_opp_khz(freq_khz)) { -+ /* Restore original value */ -+ if (pll1_config_from_opp_khz(current_opp_khz)) { -+ EMSG("No CPU operating point can be set"); -+ panic(); -+ } -+ -+ return -1; -+ } -+ -+ current_opp_khz = freq_khz; -+ -+ return 0; -+} -+ -+int stm32mp1_round_opp_khz(uint32_t *freq_khz) -+{ -+ unsigned int i = 0; -+ uint32_t round_opp = 0; -+ -+ if (!stm32mp1_clk_pll1_settings_are_valid()) { -+ /* -+ * No OPP table in DT, or an error occurred during PLL1 -+ * settings computation, system can only work on current -+ * operating point, so return current CPU frequency. -+ */ -+ *freq_khz = current_opp_khz; -+ -+ return 0; -+ } -+ -+ for (i = 0; i < PLAT_MAX_OPP_NB; i++) -+ if ((pll1_settings.freq[i] <= *freq_khz) && -+ (pll1_settings.freq[i] > round_opp)) -+ round_opp = pll1_settings.freq[i]; -+ -+ *freq_khz = round_opp; -+ -+ return 0; -+} -+/* End PMU OPP */ -+ -+#ifdef CFG_PM -+struct soc_stop_context { -+ uint32_t pll3cr; -+ uint32_t pll4cr; -+ uint32_t mssckselr; -+ uint32_t mcudivr; -+}; -+ -+static struct soc_stop_context soc_stop_ctx; -+ -+static void save_pll34_state(void) -+{ -+ uintptr_t rcc_base = stm32_rcc_base(); -+ struct soc_stop_context *ctx = &soc_stop_ctx; -+ -+ ctx->pll3cr = io_read32(rcc_base + RCC_PLL3CR); -+ ctx->pll4cr = io_read32(rcc_base + RCC_PLL4CR); -+} -+ -+static void save_mcu_subsys_clocks(void) -+{ -+ uintptr_t rcc_base = stm32_rcc_base(); -+ struct soc_stop_context *ctx = &soc_stop_ctx; -+ -+ ctx->mssckselr = io_read32(rcc_base + RCC_MSSCKSELR); -+ ctx->mcudivr = io_read32(rcc_base + RCC_MCUDIVR) & -+ RCC_MCUDIV_MASK; -+} -+ -+static void restore_pll34_state(void) -+{ -+ struct soc_stop_context *ctx = &soc_stop_ctx; -+ -+ /* Let PLL4 start while we're starting and waiting for PLL3 */ -+ if (ctx->pll4cr & RCC_PLLNCR_PLLON) -+ pll_start(_PLL4); -+ -+ if (ctx->pll3cr & RCC_PLLNCR_PLLON) { -+ pll_start(_PLL3); -+ if (pll_output(_PLL3, ctx->pll3cr >> RCC_PLLNCR_DIVEN_SHIFT)) { -+ EMSG("Failed to restore PLL3"); -+ panic(); -+ } -+ } -+ -+ if (ctx->pll4cr & RCC_PLLNCR_PLLON) { -+ if (pll_output(_PLL4, ctx->pll4cr >> RCC_PLLNCR_DIVEN_SHIFT)) { -+ EMSG("Failed to restore PLL4"); -+ panic(); -+ } -+ } -+} -+ -+static void restore_mcu_subsys_clocks(void) -+{ -+ uintptr_t rcc_base = stm32_rcc_base(); -+ struct soc_stop_context *ctx = &soc_stop_ctx; -+ -+ io_write32(rcc_base + RCC_MSSCKSELR, ctx->mssckselr); -+ -+ if (stm32mp1_set_clkdiv(ctx->mcudivr, rcc_base + RCC_MCUDIVR)) { -+ EMSG("Failed to restore MCUDIVR"); -+ panic(); -+ } -+} -+ -+/* -+ * Sequence to save/restore the non-secure configuration. -+ * Restoring clocks and muxes need IPs to run on kernel clock -+ * hence on configuration is restored at resume, kernel clock -+ * should be disable: this mandates secure access. -+ * -+ * backup_mux*_cfg for the clock muxes. -+ * backup_clock_sc_cfg for the set/clear clock gating registers -+ * backup_clock_cfg for the regular full write registers -+ */ -+ -+struct backup_mux_cfg { -+ uint16_t offset; -+ uint8_t value; -+ uint8_t bit_len; -+}; -+ -+#define MUXCFG(_offset, _bit_len) \ -+ { .offset = (_offset), .bit_len = (_bit_len) } -+ -+struct backup_mux_cfg backup_mux0_cfg[] = { -+ MUXCFG(RCC_SDMMC12CKSELR, 3), -+ MUXCFG(RCC_SPI2S23CKSELR, 3), -+ MUXCFG(RCC_SPI45CKSELR, 3), -+ MUXCFG(RCC_I2C12CKSELR, 3), -+ MUXCFG(RCC_I2C35CKSELR, 3), -+ MUXCFG(RCC_LPTIM23CKSELR, 3), -+ MUXCFG(RCC_LPTIM45CKSELR, 3), -+ MUXCFG(RCC_UART24CKSELR, 3), -+ MUXCFG(RCC_UART35CKSELR, 3), -+ MUXCFG(RCC_UART78CKSELR, 3), -+ MUXCFG(RCC_SAI1CKSELR, 3), -+ MUXCFG(RCC_ETHCKSELR, 2), -+ MUXCFG(RCC_I2C46CKSELR, 3), -+ MUXCFG(RCC_RNG2CKSELR, 2), -+ MUXCFG(RCC_SDMMC3CKSELR, 3), -+ MUXCFG(RCC_FMCCKSELR, 2), -+ MUXCFG(RCC_QSPICKSELR, 2), -+ MUXCFG(RCC_USBCKSELR, 2), -+ MUXCFG(RCC_SPDIFCKSELR, 2), -+ MUXCFG(RCC_SPI2S1CKSELR, 3), -+ MUXCFG(RCC_CECCKSELR, 2), -+ MUXCFG(RCC_LPTIM1CKSELR, 3), -+ MUXCFG(RCC_UART6CKSELR, 3), -+ MUXCFG(RCC_FDCANCKSELR, 2), -+ MUXCFG(RCC_SAI2CKSELR, 3), -+ MUXCFG(RCC_SAI3CKSELR, 3), -+ MUXCFG(RCC_SAI4CKSELR, 3), -+ MUXCFG(RCC_ADCCKSELR, 2), -+ MUXCFG(RCC_DSICKSELR, 1), -+ MUXCFG(RCC_CPERCKSELR, 2), -+ MUXCFG(RCC_RNG1CKSELR, 2), -+ MUXCFG(RCC_STGENCKSELR, 2), -+ MUXCFG(RCC_UART1CKSELR, 3), -+ MUXCFG(RCC_SPI6CKSELR, 3), -+}; -+ -+struct backup_mux_cfg backup_mux4_cfg[] = { -+ MUXCFG(RCC_USBCKSELR, 1), -+}; -+ -+static void backup_mux_cfg(void) -+{ -+ struct backup_mux_cfg *cfg = backup_mux0_cfg; -+ size_t count = ARRAY_SIZE(backup_mux0_cfg); -+ size_t i = 0; -+ uintptr_t base = stm32_rcc_base(); -+ -+ for (i = 0; i < count; i++) -+ cfg[i].value = io_read32(base + cfg[i].offset) & -+ GENMASK_32(cfg[i].bit_len - 1, 0); -+ -+ cfg = backup_mux4_cfg; -+ count = ARRAY_SIZE(backup_mux4_cfg); -+ -+ for (i = 0; i < count; i++) -+ cfg[i].value = io_read32(base + cfg[i].offset) & -+ GENMASK_32(4 + cfg[i].bit_len - 1, 4); -+} -+ -+static void restore_mux_cfg(void) -+{ -+ struct backup_mux_cfg *cfg = backup_mux0_cfg; -+ size_t count = ARRAY_SIZE(backup_mux0_cfg); -+ size_t i = 0; -+ uintptr_t base = stm32_rcc_base(); -+ -+ for (i = 0; i < count; i++) -+ io_clrsetbits32(base + cfg[i].offset, -+ GENMASK_32(cfg[i].bit_len - 1, 0), -+ cfg[i].value); -+ -+ cfg = backup_mux4_cfg; -+ count = ARRAY_SIZE(backup_mux4_cfg); -+ -+ for (i = 0; i < count; i++) -+ io_clrsetbits32(base + cfg[i].offset, -+ GENMASK_32(4 + cfg[i].bit_len - 1, 4), -+ cfg[i].value); -+} -+ -+/* Structure is used for set/clear registers and for regular registers */ -+struct backup_clock_cfg { -+ uint32_t offset; -+ uint32_t value; -+}; -+ -+static struct backup_clock_cfg backup_clock_sc_cfg[] = { -+ { .offset = RCC_MP_APB1ENSETR }, -+ { .offset = RCC_MP_APB2ENSETR }, -+ { .offset = RCC_MP_APB3ENSETR }, -+ { .offset = RCC_MP_APB4ENSETR }, -+ { .offset = RCC_MP_APB5ENSETR }, -+ { .offset = RCC_MP_AHB2ENSETR }, -+ { .offset = RCC_MP_AHB3ENSETR }, -+ { .offset = RCC_MP_AHB4ENSETR }, -+ { .offset = RCC_MP_AHB5ENSETR }, -+ { .offset = RCC_MP_AHB6ENSETR }, -+ { .offset = RCC_MP_MLAHBENSETR }, -+}; -+ -+static struct backup_clock_cfg backup_clock_cfg[] = { -+ { .offset = RCC_TZCR}, -+ { .offset = RCC_MCO1CFGR }, -+ { .offset = RCC_MCO2CFGR }, -+ { .offset = RCC_PLL3CR }, -+ { .offset = RCC_PLL4CR }, -+ { .offset = RCC_PLL4CFGR2 }, -+ { .offset = RCC_MCUDIVR }, -+ { .offset = RCC_MSSCKSELR }, -+}; -+ -+static void backup_sc_cfg(void) -+{ -+ struct backup_clock_cfg *cfg = backup_clock_sc_cfg; -+ size_t count = ARRAY_SIZE(backup_clock_sc_cfg); -+ size_t i = 0; -+ uintptr_t base = stm32_rcc_base(); -+ -+ for (i = 0; i < count; i++) -+ cfg[i].value = io_read32(base + cfg[i].offset); -+} -+ -+static void restore_sc_cfg(void) -+{ -+ struct backup_clock_cfg *cfg = backup_clock_sc_cfg; -+ size_t count = ARRAY_SIZE(backup_clock_sc_cfg); -+ size_t i = 0; -+ uintptr_t base = stm32_rcc_base(); -+ -+ for (i = 0; i < count; i++) { -+ io_write32(base + cfg[i].offset, cfg[i].value); -+ io_write32(base + cfg[i].offset + RCC_MP_ENCLRR_OFFSET, -+ ~cfg[i].value); -+ } -+} -+ -+static void backup_regular_cfg(void) -+{ -+ struct backup_clock_cfg *cfg = backup_clock_cfg; -+ size_t count = ARRAY_SIZE(backup_clock_cfg); -+ size_t i = 0; -+ uintptr_t base = stm32_rcc_base(); -+ -+ for (i = 0; i < count; i++) -+ cfg[i].value = io_read32(base + cfg[i].offset); -+} -+ -+static void restore_regular_cfg(void) -+{ -+ struct backup_clock_cfg *cfg = backup_clock_cfg; -+ size_t count = ARRAY_SIZE(backup_clock_cfg); -+ size_t i = 0; -+ uintptr_t base = stm32_rcc_base(); -+ -+ for (i = 0; i < count; i++) -+ io_write32(base + cfg[i].offset, cfg[i].value); -+} -+ -+static void disable_kernel_clocks(void) -+{ -+ const uint32_t ker_mask = RCC_OCENR_HSIKERON | -+ RCC_OCENR_CSIKERON | -+ RCC_OCENR_HSEKERON; -+ -+ /* Disable all ck_xxx_ker clocks */ -+ io_write32(stm32_rcc_base() + RCC_OCENCLRR, ker_mask); -+} -+ -+static void enable_kernel_clocks(void) -+{ -+ uintptr_t rcc_base = stm32_rcc_base(); -+ uint32_t reg = 0; -+ const uint32_t ker_mask = RCC_OCENR_HSIKERON | -+ RCC_OCENR_CSIKERON | -+ RCC_OCENR_HSEKERON; -+ -+ /* Enable ck_xxx_ker clocks if ck_xxx was on */ -+ reg = io_read32(rcc_base + RCC_OCENSETR) << 1; -+ io_write32(rcc_base + RCC_OCENSETR, reg & ker_mask); -+} -+ -+static void clear_rcc_reset_status(void) -+{ -+ /* Clear reset status fields */ -+ io_write32(stm32_rcc_base() + RCC_MP_RSTSCLRR, 0); -+} -+ -+void stm32mp1_clk_save_context_for_stop(void) -+{ -+ enable_kernel_clocks(); -+ save_mcu_subsys_clocks(); -+ save_pll34_state(); -+} -+ -+void stm32mp1_clk_restore_context_for_stop(void) -+{ -+ restore_pll34_state(); -+ /* Restore MCU clock source after PLL3 is ready */ -+ restore_mcu_subsys_clocks(); -+ disable_kernel_clocks(); -+} -+ -+void stm32mp1_clk_mcuss_protect(bool enable) -+{ -+ uintptr_t rcc_base = stm32_rcc_base(); -+ -+ if (enable) -+ io_setbits32(rcc_base + RCC_TZCR, RCC_TZCR_MCKPROT); -+ else -+ io_clrbits32(rcc_base + RCC_TZCR, RCC_TZCR_MCKPROT); -+} -+ -+static void stm32_clock_suspend(void) -+{ -+ backup_regular_cfg(); -+ backup_sc_cfg(); -+ backup_mux_cfg(); -+ save_pll34_state(); -+ -+ enable_kernel_clocks(); -+ clear_rcc_reset_status(); -+} -+ -+static void stm32_clock_resume(void) -+{ -+ unsigned int idx = 0; -+ -+ restore_pll34_state(); -+ restore_mux_cfg(); -+ restore_sc_cfg(); -+ restore_regular_cfg(); -+ -+ /* Sync secure and shared clocks physical state on functional state */ -+ for (idx = 0; idx < NB_GATES; idx++) { -+ struct stm32mp1_clk_gate const *gate = gate_ref(idx); -+ -+ if (gate_is_non_secure(gate)) -+ continue; -+ -+ if (gate_refcounts[idx]) { -+ DMSG("Force clock %d enable", gate->clock_id); -+ __clk_enable(gate); -+ } else { -+ DMSG("Force clock %d disable", gate->clock_id); -+ __clk_disable(gate); -+ } -+ } -+ -+ disable_kernel_clocks(); -+} -+ -+static TEE_Result stm32_clock_pm(enum pm_op op, unsigned int pm_hint __unused, -+ const struct pm_callback_handle *hdl __unused) -+{ -+ if (op == PM_OP_SUSPEND) -+ stm32_clock_suspend(); -+ else -+ stm32_clock_resume(); -+ -+ return TEE_SUCCESS; -+} -+DECLARE_KEEP_PAGER(stm32_clock_pm); -+#else -+static TEE_Result stm32_clock_pm(enum pm_op op __unused, -+ unsigned int pm_hint __unused, -+ const struct pm_callback_handle *hdl __unused) -+{ -+ return TEE_ERROR_SECURITY; -+} -+#endif /*CFG_PM*/ -+ -+static void init_non_secure_rcc(void) -+{ -+ uintptr_t rcc_base = stm32_rcc_base(); -+ -+ /* Clear all interrupt flags and core stop requests */ -+ io_write32(rcc_base + RCC_MP_CIFR, 0x110F1F); -+ io_write32(rcc_base + RCC_MP_SREQCLRR, 0x3); -+} -+ -+static TEE_Result stm32_clk_probe(void) -+{ -+ assert(PLLCFG_NB == PLAT_MAX_PLLCFG_NB); -+ -+ stm32mp1_clk_early_init(); -+ enable_static_secure_clocks(); -+ save_current_opp(); -+ init_non_secure_rcc(); -+ register_pm_core_service_cb(stm32_clock_pm, NULL); -+ -+ clk_provider_register(&stm32mp_clk_ops); -+ -+ return TEE_SUCCESS; -+} -+/* Setup clock support before driver initialization */ -+service_init(stm32_clk_probe); -diff --git a/core/drivers/clk/clk.c b/core/drivers/clk/clk.c -new file mode 100644 -index 000000000..f6e5d8ecf ---- /dev/null -+++ b/core/drivers/clk/clk.c -@@ -0,0 +1,58 @@ -+// SPDX-License-Identifier: BSD-2-Clause -+/* -+ * Copyright (c) 2020, STMicroelectronics - All Rights Reserved -+ * Author(s): Ludovic Barre, for STMicroelectronics. -+ */ -+ -+#include -+#include -+#include -+ -+static const struct clk_ops *ops; -+ -+TEE_Result clk_enable(unsigned long id) -+{ -+ assert(ops && ops->enable); -+ -+ return ops->enable(id); -+} -+ -+void clk_disable(unsigned long id) -+{ -+ assert(ops && ops->disable); -+ -+ ops->disable(id); -+} -+ -+unsigned long clk_get_rate(unsigned long id) -+{ -+ assert(ops && ops->get_rate); -+ -+ return ops->get_rate(id); -+} -+ -+unsigned long clk_get_parent(unsigned long id) -+{ -+ assert(ops); -+ -+ if (ops->get_parent) -+ return ops->get_parent(id); -+ -+ return CLK_UNKNOWN_ID; -+} -+ -+bool clk_is_enabled(unsigned long id) -+{ -+ assert(ops && ops->is_enabled); -+ -+ return ops->is_enabled(id); -+} -+ -+void clk_provider_register(const struct clk_ops *ops_ptr) -+{ -+ assert(!ops && ops_ptr && ops_ptr->enable && -+ ops_ptr->disable && ops_ptr->get_rate && -+ ops_ptr->is_enabled); -+ -+ ops = ops_ptr; -+} -diff --git a/core/drivers/clk/sub.mk b/core/drivers/clk/sub.mk -new file mode 100644 -index 000000000..4caed574b ---- /dev/null -+++ b/core/drivers/clk/sub.mk -@@ -0,0 +1,2 @@ -+srcs-$(CFG_CLK_DRIVER) += clk.c -+srcs-$(CFG_STM32MP15_CLK) += clk-stm32mp15.c -diff --git a/core/drivers/gic.c b/core/drivers/gic.c -index 0bad97486..3f1a606ed 100644 ---- a/core/drivers/gic.c -+++ b/core/drivers/gic.c -@@ -7,12 +7,15 @@ - #include - #include - #include -+#include - #include - #include - #include -+#include -+#include - #include --#include - #include -+#include - - /* Offsets from gic.gicc_base */ - #define GICC_CTLR (0x000) -@@ -33,9 +36,11 @@ - #define GICD_ICENABLER(n) (0x180 + (n) * 4) - #define GICD_ISPENDR(n) (0x200 + (n) * 4) - #define GICD_ICPENDR(n) (0x280 + (n) * 4) -+#define GICD_ISACTIVER(n) (0x300 + (n) * 4) - #define GICD_IPRIORITYR(n) (0x400 + (n) * 4) - #define GICD_ITARGETSR(n) (0x800 + (n) * 4) - #define GICD_IGROUPMODR(n) (0xd00 + (n) * 4) -+#define GICD_ICFGR(n) (0xc00 + (n) * 4) - #define GICD_SGIR (0xF00) - - #define GICD_CTLR_ENABLEGRP0 (1 << 0) -@@ -76,6 +81,25 @@ static void gic_op_raise_sgi(struct itr_chip *chip, size_t it, - static void gic_op_set_affinity(struct itr_chip *chip, size_t it, - uint8_t cpu_mask); - -+#if defined(CFG_ARM_GIC_PM) -+static void gic_pm_add_it(struct gic_data *gd, unsigned int it); -+static void gic_pm_register(struct gic_data *gd); -+#else -+static void gic_pm_add_it(struct gic_data *gd __unused, -+ unsigned int it __unused) -+{ -+} -+static void gic_pm_register(struct gic_data *gd __unused) -+{ -+} -+#endif -+ -+#if !defined(CFG_ARM_GICV3) -+static uint8_t gic_op_set_pmr(struct itr_chip *chip, uint8_t mask); -+static uint8_t gic_op_set_ipriority(struct itr_chip *chip, size_t it, -+ uint8_t mask); -+#endif -+ - static const struct itr_ops gic_ops = { - .add = gic_op_add, - .enable = gic_op_enable, -@@ -83,6 +107,10 @@ static const struct itr_ops gic_ops = { - .raise_pi = gic_op_raise_pi, - .raise_sgi = gic_op_raise_sgi, - .set_affinity = gic_op_set_affinity, -+#if !defined(CFG_ARM_GICV3) -+ .set_pmr = gic_op_set_pmr, -+ .set_ipriority = gic_op_set_ipriority, -+#endif - }; - DECLARE_KEEP_PAGER(gic_ops); - -@@ -148,10 +176,10 @@ void gic_cpu_init(struct gic_data *gd) - * allow the Non-secure world to adjust the priority mask itself - */ - #if defined(CFG_ARM_GICV3) -- write_icc_pmr(0x80); -+ write_icc_pmr(GIC_HIGHEST_NS_PRIORITY); - write_icc_igrpen1(1); - #else -- io_write32(gd->gicc_base + GICC_PMR, 0x80); -+ io_write32(gd->gicc_base + GICC_PMR, GIC_HIGHEST_NS_PRIORITY); - - /* Enable GIC */ - io_write32(gd->gicc_base + GICC_CTLR, -@@ -160,13 +188,10 @@ void gic_cpu_init(struct gic_data *gd) - #endif - } - --void gic_init(struct gic_data *gd, vaddr_t gicc_base __maybe_unused, -- vaddr_t gicd_base) -+void gic_init_setup(struct gic_data *gd) - { - size_t n; - -- gic_init_base_addr(gd, gicc_base, gicd_base); -- - for (n = 0; n <= gd->max_it / NUM_INTS_PER_REG; n++) { - /* Disable interrupts */ - io_write32(gd->gicd_base + GICD_ICENABLER(n), 0xffffffff); -@@ -191,11 +216,11 @@ void gic_init(struct gic_data *gd, vaddr_t gicc_base __maybe_unused, - * allow the Non-secure world to adjust the priority mask itself - */ - #if defined(CFG_ARM_GICV3) -- write_icc_pmr(0x80); -+ write_icc_pmr(GIC_HIGHEST_NS_PRIORITY); - write_icc_igrpen1(1); - io_setbits32(gd->gicd_base + GICD_CTLR, GICD_CTLR_ENABLEGRP1S); - #else -- io_write32(gd->gicc_base + GICC_PMR, 0x80); -+ io_write32(gd->gicc_base + GICC_PMR, GIC_HIGHEST_NS_PRIORITY); - - /* Enable GIC */ - io_write32(gd->gicc_base + GICC_CTLR, GICC_CTLR_FIQEN | -@@ -212,6 +237,14 @@ void gic_init_base_addr(struct gic_data *gd, vaddr_t gicc_base __maybe_unused, - gd->gicd_base = gicd_base; - gd->max_it = probe_max_it(gicc_base, gicd_base); - gd->chip.ops = &gic_ops; -+ -+ gic_pm_register(gd); -+} -+ -+void gic_init(struct gic_data *gd, vaddr_t gicc_base, vaddr_t gicd_base) -+{ -+ gic_init_base_addr(gd, gicc_base, gicd_base); -+ gic_init_setup(gd); - } - - static void gic_it_add(struct gic_data *gd, size_t it) -@@ -236,7 +269,8 @@ static void gic_it_set_cpu_mask(struct gic_data *gd, size_t it, - { - size_t idx __maybe_unused = it / NUM_INTS_PER_REG; - uint32_t mask __maybe_unused = 1 << (it % NUM_INTS_PER_REG); -- uint32_t target, target_shift; -+ uint32_t target = 0; -+ uint32_t target_shift = 0; - vaddr_t itargetsr = gd->gicd_base + - GICD_ITARGETSR(it / NUM_TARGETS_PER_REG); - -@@ -258,15 +292,39 @@ static void gic_it_set_prio(struct gic_data *gd, size_t it, uint8_t prio) - size_t idx __maybe_unused = it / NUM_INTS_PER_REG; - uint32_t mask __maybe_unused = 1 << (it % NUM_INTS_PER_REG); - -- /* Assigned to group0 */ -- assert(!(io_read32(gd->gicd_base + GICD_IGROUPR(idx)) & mask)); -- - /* Set prio it to selected CPUs */ - DMSG("prio: writing 0x%x to 0x%" PRIxVA, - prio, gd->gicd_base + GICD_IPRIORITYR(0) + it); - io_write8(gd->gicd_base + GICD_IPRIORITYR(0) + it, prio); - } - -+static uint8_t gic_op_set_pmr(struct itr_chip *chip, uint8_t mask) -+{ -+ struct gic_data *gd = container_of(chip, struct gic_data, chip); -+ uint32_t pmr = io_read32(gd->gicc_base + GICC_PMR); -+ -+ /* -+ * Order memory updates w.r.t. PMR write, and ensure they're visible -+ * before potential out of band interrupt trigger because of PMR update. -+ */ -+ dsb_ishst(); -+ io_write32(gd->gicc_base + GICC_PMR, mask); -+ dsb_ishst(); -+ -+ return (uint8_t)pmr; -+} -+ -+static uint8_t gic_op_set_ipriority(struct itr_chip *chip, size_t it, -+ uint8_t mask) -+{ -+ struct gic_data *gd = container_of(chip, struct gic_data, chip); -+ uint8_t prio = io_read8(gd->gicd_base + GICD_IPRIORITYR(0) + it); -+ -+ gic_it_set_prio(gd, it, mask); -+ -+ return prio; -+} -+ - static void gic_it_enable(struct gic_data *gd, size_t it) - { - size_t idx = it / NUM_INTS_PER_REG; -@@ -391,8 +449,8 @@ void gic_dump_state(struct gic_data *gd) - - void gic_it_handle(struct gic_data *gd) - { -- uint32_t iar; -- uint32_t id; -+ uint32_t iar = 0; -+ uint32_t id = 0; - - iar = gic_read_iar(gd); - id = iar & GICC_IAR_IT_ID_MASK; -@@ -417,6 +475,8 @@ static void gic_op_add(struct itr_chip *chip, size_t it, - /* Set the CPU mask to deliver interrupts to any online core */ - gic_it_set_cpu_mask(gd, it, 0xff); - gic_it_set_prio(gd, it, 0x1); -+ -+ gic_pm_add_it(gd, it); - } - - static void gic_op_enable(struct itr_chip *chip, size_t it) -@@ -462,6 +522,7 @@ static void gic_op_raise_sgi(struct itr_chip *chip, size_t it, - else - gic_it_raise_sgi(gd, it, cpu_mask, 0); - } -+ - static void gic_op_set_affinity(struct itr_chip *chip, size_t it, - uint8_t cpu_mask) - { -@@ -472,3 +533,137 @@ static void gic_op_set_affinity(struct itr_chip *chip, size_t it, - - gic_it_set_cpu_mask(gd, it, cpu_mask); - } -+ -+#if defined(CFG_ARM_GIC_PM) -+/* -+ * Save/restore interrupts registered from the gic_op_add_it() handler -+ */ -+#define IT_PM_GPOUP1_BIT BIT(0) -+#define IT_PM_ENABLE_BIT BIT(1) -+#define IT_PM_PENDING_BIT BIT(2) -+#define IT_PM_ACTIVE_BIT BIT(3) -+#define IT_PM_CONFIG_MASK GENMASK_32(1, 0) -+ -+/* -+ * @it - interrupt ID/number -+ * @flags - bitflag IT_PM_*_BIT -+ * @iprio - 8bit prio from IPRIORITYR -+ * @itarget - 8bit target from ITARGETR -+ * @icfg - 2bit configuration from ICFGR and IT_PM_CONFIG_MASK -+ */ -+struct gic_it_pm { -+ uint16_t it; -+ uint8_t flags; -+ uint8_t iprio; -+ uint8_t itarget; -+ uint8_t icfg; -+}; -+ -+static void gic_pm_add_it(struct gic_data *gd, unsigned int it) -+{ -+ struct gic_pm *pm = &gd->pm; -+ -+ pm->count++; -+ pm->pm_cfg = realloc(pm->pm_cfg, pm->count * sizeof(*pm->pm_cfg)); -+ if (!pm->pm_cfg) -+ panic(); -+ -+ pm->pm_cfg[pm->count - 1] = (struct gic_it_pm){ .it = it }; -+} -+ -+static void gic_save_it(struct gic_data *gd, struct gic_it_pm *pm) -+{ -+ unsigned int it = pm->it; -+ size_t idx = 0; -+ uint32_t bit_mask = BIT(it % NUM_INTS_PER_REG); -+ uint32_t shift2 = it % (NUM_INTS_PER_REG / 2); -+ uint32_t shift8 = it % (NUM_INTS_PER_REG / 8); -+ uint32_t data32 = 0; -+ -+ pm->flags = 0; -+ idx = it / NUM_INTS_PER_REG; -+ -+ if (io_read32(gd->gicd_base + GICD_IGROUPR(idx)) & bit_mask) -+ pm->flags |= IT_PM_GPOUP1_BIT; -+ if (io_read32(gd->gicd_base + GICD_ISENABLER(idx)) & bit_mask) -+ pm->flags |= IT_PM_ENABLE_BIT; -+ if (io_read32(gd->gicd_base + GICD_ISPENDR(idx)) & bit_mask) -+ pm->flags |= IT_PM_PENDING_BIT; -+ if (io_read32(gd->gicd_base + GICD_ISACTIVER(idx)) & bit_mask) -+ pm->flags |= IT_PM_ACTIVE_BIT; -+ -+ idx = (8 * it) / NUM_INTS_PER_REG; -+ -+ data32 = io_read32(gd->gicd_base + GICD_IPRIORITYR(idx)) >> shift8; -+ pm->iprio = (uint8_t)data32; -+ -+ data32 = io_read32(gd->gicd_base + GICD_ITARGETSR(idx)) >> shift8; -+ pm->itarget = (uint8_t)data32; -+ -+ /* Note: ICFGR is RAO for SPIs and PPIs */ -+ idx = (2 * it) / NUM_INTS_PER_REG; -+ data32 = io_read32(gd->gicd_base + GICD_ICFGR(idx)) >> shift2; -+ pm->icfg = (uint8_t)data32 & IT_PM_CONFIG_MASK; -+} -+ -+static void gic_restore_it(struct gic_data *gd, struct gic_it_pm *pm) -+{ -+ unsigned int it = pm->it; -+ size_t idx = it / NUM_INTS_PER_REG; -+ uint32_t mask = BIT(it % NUM_INTS_PER_REG); -+ uint32_t shift2 = it % (NUM_INTS_PER_REG / 2); -+ uint32_t shift8 = it % (NUM_INTS_PER_REG / 8); -+ -+ io_mask32(gd->gicd_base + GICD_IGROUPR(idx), -+ (pm->flags & IT_PM_GPOUP1_BIT) ? mask : 0, mask); -+ -+ io_mask32(gd->gicd_base + GICD_ISENABLER(idx), -+ (pm->flags & IT_PM_ENABLE_BIT) ? mask : 0, mask); -+ -+ io_mask32(gd->gicd_base + GICD_ISPENDR(idx), -+ (pm->flags & IT_PM_PENDING_BIT) ? mask : 0, mask); -+ -+ io_mask32(gd->gicd_base + GICD_ISACTIVER(idx), -+ (pm->flags & IT_PM_ACTIVE_BIT) ? mask : 0, mask); -+ -+ idx = (8 * it) / NUM_INTS_PER_REG; -+ -+ io_mask32(gd->gicd_base + GICD_IPRIORITYR(idx), -+ (uint32_t)pm->iprio << shift8, UINT8_MAX << shift8); -+ -+ io_mask32(gd->gicd_base + GICD_ITARGETSR(idx), -+ (uint32_t)pm->itarget << shift8, UINT8_MAX << shift8); -+ -+ /* Note: ICFGR is WI for SPIs and PPIs */ -+ idx = (2 * it) / NUM_INTS_PER_REG; -+ io_mask32(gd->gicd_base + GICD_ICFGR(idx), -+ (uint32_t)pm->icfg << shift2, IT_PM_CONFIG_MASK << shift2); -+} -+ -+static TEE_Result gic_pm(enum pm_op op, unsigned int pm_hint __unused, -+ const struct pm_callback_handle *handle) -+{ -+ void (*sequence)(struct gic_data *gd, struct gic_it_pm *pm) = NULL; -+ struct gic_it_pm *cfg = NULL; -+ unsigned int n = 0; -+ struct gic_data *gd = (struct gic_data *)PM_CALLBACK_GET_HANDLE(handle); -+ struct gic_pm *pm = &gd->pm; -+ -+ if (op == PM_OP_SUSPEND) { -+ sequence = gic_save_it; -+ } else { -+ gic_init_setup(gd); -+ sequence = gic_restore_it; -+ } -+ for (n = 0, cfg = pm->pm_cfg; n < pm->count; n++, cfg++) -+ sequence(gd, cfg); -+ -+ return TEE_SUCCESS; -+} -+DECLARE_KEEP_PAGER(gic_pm); -+ -+static void gic_pm_register(struct gic_data *gd) -+{ -+ register_pm_core_service_cb(gic_pm, gd); -+} -+#endif /*CFG_ARM_GIC_PM*/ -diff --git a/core/drivers/scmi-msg/clock.c b/core/drivers/scmi-msg/clock.c -index 6fe62477e..0361f8828 100644 ---- a/core/drivers/scmi-msg/clock.c -+++ b/core/drivers/scmi-msg/clock.c -@@ -1,7 +1,7 @@ - // SPDX-License-Identifier: BSD-3-Clause - /* -- * Copyright (c) 2015-2019, Arm Limited and Contributors. All rights reserved. - * Copyright (c) 2019, Linaro Limited -+ * Copyright (c) 2015-2020, Arm Limited and Contributors. All rights reserved. - */ - #include - #include -@@ -357,7 +357,7 @@ static const scmi_msg_handler_t scmi_clock_handler_table[] = { - [SCMI_CLOCK_CONFIG_SET] = scmi_clock_config_set, - }; - --static bool message_id_is_supported(size_t message_id) -+static bool message_id_is_supported(unsigned int message_id) - { - return message_id < ARRAY_SIZE(scmi_clock_handler_table) && - scmi_clock_handler_table[message_id]; -diff --git a/core/drivers/stm32_bsec.c b/core/drivers/stm32_bsec.c -index 2b1584ff5..7d389a061 100644 ---- a/core/drivers/stm32_bsec.c -+++ b/core/drivers/stm32_bsec.c -@@ -91,10 +91,6 @@ - #define BSEC_MODE_BIST2_LOCK_MASK BIT(7) - - /* BSEC_DEBUG */ --#define BSEC_HDPEN BIT(4) --#define BSEC_SPIDEN BIT(5) --#define BSEC_SPINDEN BIT(6) --#define BSEC_DBGSWGEN BIT(10) - #define BSEC_DEN_ALL_MSK GENMASK_32(10, 0) - - /* -@@ -546,6 +542,40 @@ bool stm32_bsec_nsec_can_access_otp(uint32_t otp_id) - nsec_access_granted(otp_id - otp_upper_base()); - } - -+struct nvmem_layout { -+ char *name; -+ uint32_t otp_id; -+ size_t bit_len; -+}; -+ -+static struct nvmem_layout *nvmem_layout; -+static size_t nvmem_layout_count; -+ -+TEE_Result stm32_bsec_find_otp_in_nvmem_layout(const char *name, -+ uint32_t *otp_id, -+ size_t *otp_bit_len) -+{ -+ size_t i = 0; -+ -+ if (!name) -+ return TEE_ERROR_BAD_PARAMETERS; -+ -+ for (i = 0; i < nvmem_layout_count; i++) { -+ if (!nvmem_layout[i].name || strcmp(name, nvmem_layout[i].name)) -+ continue; -+ -+ if (otp_id) -+ *otp_id = nvmem_layout[i].otp_id; -+ -+ if (otp_bit_len) -+ *otp_bit_len = nvmem_layout[i].bit_len; -+ -+ return TEE_SUCCESS; -+ } -+ -+ return TEE_ERROR_ITEM_NOT_FOUND; -+} -+ - #ifdef CFG_DT - static void enable_nsec_access(unsigned int otp_id) - { -@@ -624,6 +654,77 @@ static void bsec_dt_otp_nsec_access(void *fdt, int bsec_node) - } - } - -+static void save_dt_nvmem_layout(void *fdt) -+{ -+ const fdt32_t *cells = NULL; -+ int i = 0; -+ int cell_nb = 0; -+ int nvmem_node = 0; -+ -+ nvmem_node = fdt_node_offset_by_compatible(fdt, -1, -+ "st,stm32-nvmem-layout"); -+ if (nvmem_node < 0) -+ return; -+ -+ cells = fdt_getprop(fdt, nvmem_node, "nvmem-cells", &cell_nb); -+ if (!cells) -+ cell_nb = 0; -+ -+ cell_nb /= sizeof(uint32_t); -+ -+ i = fdt_stringlist_count(fdt, nvmem_node, "nvmem-cell-names"); -+ if (i < 0) -+ i = 0; -+ -+ if (cell_nb != i) -+ panic("Inconsistent NVMEM layout"); -+ -+ nvmem_layout = calloc(cell_nb, sizeof(*nvmem_layout)); -+ if (!nvmem_layout) -+ panic(); -+ -+ nvmem_layout_count = (size_t)cell_nb; -+ -+ for (i = 0; i < cell_nb; i++) { -+ const fdt32_t *cuint = NULL; -+ const char *string = NULL; -+ int len = 0; -+ int node = 0; -+ struct nvmem_layout *layout_cell = &nvmem_layout[i]; -+ -+ node = fdt_node_offset_by_phandle(fdt, -+ fdt32_to_cpu(*(cells + i))); -+ if (node < 0) { -+ IMSG("Malformed nvmem_layout node: ignored"); -+ continue; -+ } -+ -+ cuint = fdt_getprop(fdt, node, "reg", &len); -+ if (!cuint || (len != (2 * (int)sizeof(uint32_t)))) { -+ IMSG("Malformed nvmem_layout node: ignored"); -+ continue; -+ } -+ -+ if (fdt32_to_cpu(*cuint) % sizeof(uint32_t)) { -+ IMSG("Misaligned nvmem_layout element: ignored"); -+ continue; -+ } -+ layout_cell->otp_id = fdt32_to_cpu(*cuint) / sizeof(uint32_t); -+ layout_cell->bit_len = fdt32_to_cpu(*(cuint + 1)) * CHAR_BIT; -+ -+ string = fdt_stringlist_get(fdt, nvmem_node, "nvmem-cell-names", -+ i, &len); -+ if (!string || !len) -+ continue; -+ -+ layout_cell->name = calloc(1, len + 1); -+ if (!layout_cell->name) -+ panic(); -+ -+ memcpy(layout_cell->name, string, len); -+ } -+} -+ - static void initialize_bsec_from_dt(void) - { - void *fdt = NULL; -@@ -642,6 +743,8 @@ static void initialize_bsec_from_dt(void) - panic(); - - bsec_dt_otp_nsec_access(fdt, node); -+ -+ save_dt_nvmem_layout(fdt); - } - #else - static void initialize_bsec_from_dt(void) -diff --git a/core/drivers/stm32_etzpc.c b/core/drivers/stm32_etzpc.c -index 3482f3431..5573503a1 100644 ---- a/core/drivers/stm32_etzpc.c -+++ b/core/drivers/stm32_etzpc.c -@@ -26,10 +26,16 @@ - #include - #include - #include -+#include - #include - - /* Devicetree compatibility */ - #define ETZPC_COMPAT "st,stm32-etzpc" -+#define ETZPC_LOCK_MASK BIT(0) -+#define ETZPC_MODE_SHIFT 8 -+#define ETZPC_MODE_MASK GENMASK_32(1, 0) -+#define ETZPC_ID_SHIFT 16 -+#define ETZPC_ID_MASK GENMASK_32(7, 0) - - /* ID Registers */ - #define ETZPC_TZMA0_SIZE 0x000U -@@ -316,6 +322,45 @@ void stm32_etzpc_init(paddr_t base) - } - - #ifdef CFG_DT -+struct dt_id_attr { -+ /* The effective size of the array is meaningless here */ -+ fdt32_t id_attr[1]; -+}; -+ -+static void etzpc_dt_conf_decprot(void *fdt, int node) -+{ -+ const struct dt_id_attr *conf_list = NULL; -+ size_t i = 0; -+ int len = 0; -+ -+ conf_list = (const struct dt_id_attr *)fdt_getprop(fdt, node, -+ "st,decprot", &len); -+ if (conf_list == NULL) { -+ DMSG("No ETZPC DECPROT configuration in DT"); -+ return; -+ } -+ -+ for (i = 0; i < len / sizeof(uint32_t); i++) { -+ uint32_t value = fdt32_to_cpu(conf_list->id_attr[i]); -+ uint32_t id = (value >> ETZPC_ID_SHIFT) & ETZPC_ID_MASK; -+ uint32_t mode = (value >> ETZPC_MODE_SHIFT) & ETZPC_MODE_MASK; -+ bool lock = value & ETZPC_LOCK_MASK; -+ enum etzpc_decprot_attributes attr = ETZPC_DECPROT_MAX; -+ -+ if (!valid_decprot_id(id)) { -+ DMSG("Invalid DECPROT %"PRIu32, id); -+ panic(); -+ } -+ -+ attr = stm32mp_etzpc_binding2decprot(mode); -+ stm32mp_register_etzpc_decprot(id, attr); -+ etzpc_configure_decprot(id, attr); -+ -+ if (lock) -+ etzpc_lock_decprot(id); -+ } -+} -+ - static TEE_Result init_etzpc_from_dt(void) - { - void *fdt = get_embedded_dt(); -@@ -339,6 +384,8 @@ static TEE_Result init_etzpc_from_dt(void) - - init_device_from_hw_config(&etzpc_dev, pbase); - -+ etzpc_dt_conf_decprot(fdt, node); -+ - return TEE_SUCCESS; - } - -diff --git a/core/drivers/stm32_gpio.c b/core/drivers/stm32_gpio.c -index 8f7927d90..83c7b8b4e 100644 ---- a/core/drivers/stm32_gpio.c -+++ b/core/drivers/stm32_gpio.c -@@ -1,12 +1,13 @@ - // SPDX-License-Identifier: BSD-3-Clause - /* -- * Copyright (c) 2017-2019, STMicroelectronics -+ * Copyright (c) 2017-2020, STMicroelectronics - * - * STM32 GPIO driver is used as pin controller for stm32mp SoCs. - * The driver API is defined in header file stm32_gpio.h. - */ - - #include -+#include - #include - #include - #include -@@ -54,7 +55,7 @@ static void get_gpio_cfg(uint32_t bank, uint32_t pin, struct gpio_cfg *cfg) - vaddr_t base = stm32_get_gpio_bank_base(bank); - unsigned int clock = stm32_get_gpio_bank_clock(bank); - -- stm32_clock_enable(clock); -+ clk_enable(clock); - - /* - * Save GPIO configuration bits spread over the few bank registers. -@@ -84,7 +85,7 @@ static void get_gpio_cfg(uint32_t bank, uint32_t pin, struct gpio_cfg *cfg) - ((pin - GPIO_ALT_LOWER_LIMIT) << 2)) & - GPIO_ALTERNATE_MASK; - -- stm32_clock_disable(clock); -+ clk_disable(clock); - } - - /* Apply GPIO (@bank/@pin) configuration described by @cfg */ -@@ -94,7 +95,7 @@ static void set_gpio_cfg(uint32_t bank, uint32_t pin, struct gpio_cfg *cfg) - unsigned int clock = stm32_get_gpio_bank_clock(bank); - uint32_t exceptions = cpu_spin_lock_xsave(&gpio_lock); - -- stm32_clock_enable(clock); -+ clk_enable(clock); - - /* Load GPIO MODE value, 2bit value shifted by twice the pin number */ - io_clrsetbits32(base + GPIO_MODER_OFFSET, -@@ -129,7 +130,7 @@ static void set_gpio_cfg(uint32_t bank, uint32_t pin, struct gpio_cfg *cfg) - /* Load GPIO Output direction confuguration, 1bit */ - io_clrsetbits32(base + GPIO_ODR_OFFSET, BIT(pin), cfg->od << pin); - -- stm32_clock_disable(clock); -+ clk_disable(clock); - cpu_spin_unlock_xrestore(&gpio_lock, exceptions); - } - -@@ -395,12 +396,12 @@ int stm32_gpio_get_input_level(unsigned int bank, unsigned int pin) - - assert(valid_gpio_config(bank, pin, true)); - -- stm32_clock_enable(clock); -+ clk_enable(clock); - - if (io_read32(base + GPIO_IDR_OFFSET) == BIT(pin)) - rc = 1; - -- stm32_clock_disable(clock); -+ clk_disable(clock); - - return rc; - } -@@ -412,14 +413,14 @@ void stm32_gpio_set_output_level(unsigned int bank, unsigned int pin, int level) - - assert(valid_gpio_config(bank, pin, false)); - -- stm32_clock_enable(clock); -+ clk_enable(clock); - - if (level) - io_write32(base + GPIO_BSRR_OFFSET, BIT(pin)); - else - io_write32(base + GPIO_BSRR_OFFSET, BIT(pin + 16)); - -- stm32_clock_disable(clock); -+ clk_disable(clock); - } - - void stm32_gpio_set_secure_cfg(unsigned int bank, unsigned int pin, bool secure) -@@ -428,13 +429,13 @@ void stm32_gpio_set_secure_cfg(unsigned int bank, unsigned int pin, bool secure) - unsigned int clock = stm32_get_gpio_bank_clock(bank); - uint32_t exceptions = cpu_spin_lock_xsave(&gpio_lock); - -- stm32_clock_enable(clock); -+ clk_enable(clock); - - if (secure) - io_setbits32(base + GPIO_SECR_OFFSET, BIT(pin)); - else - io_clrbits32(base + GPIO_SECR_OFFSET, BIT(pin)); - -- stm32_clock_disable(clock); -+ clk_disable(clock); - cpu_spin_unlock_xrestore(&gpio_lock, exceptions); - } -diff --git a/core/drivers/stm32_i2c.c b/core/drivers/stm32_i2c.c -index 790a5cab6..2807cc42a 100644 ---- a/core/drivers/stm32_i2c.c -+++ b/core/drivers/stm32_i2c.c -@@ -1,6 +1,6 @@ - // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) - /* -- * Copyright (c) 2017-2019, STMicroelectronics -+ * Copyright (c) 2017-2020, STMicroelectronics - * - * The driver API is defined in header file stm32_i2c.h. - * -@@ -10,6 +10,7 @@ - */ - - #include -+#include - #include - #include - #include -@@ -313,7 +314,7 @@ static void save_cfg(struct i2c_handle_s *hi2c, struct i2c_cfg *cfg) - { - vaddr_t base = get_base(hi2c); - -- stm32_clock_enable(hi2c->clock); -+ clk_enable(hi2c->clock); - - cfg->cr1 = io_read32(base + I2C_CR1); - cfg->cr2 = io_read32(base + I2C_CR2); -@@ -321,14 +322,14 @@ static void save_cfg(struct i2c_handle_s *hi2c, struct i2c_cfg *cfg) - cfg->oar2 = io_read32(base + I2C_OAR2); - cfg->timingr = io_read32(base + I2C_TIMINGR); - -- stm32_clock_disable(hi2c->clock); -+ clk_disable(hi2c->clock); - } - - static void restore_cfg(struct i2c_handle_s *hi2c, struct i2c_cfg *cfg) - { - vaddr_t base = get_base(hi2c); - -- stm32_clock_enable(hi2c->clock); -+ clk_enable(hi2c->clock); - - io_clrbits32(base + I2C_CR1, I2C_CR1_PE); - io_write32(base + I2C_TIMINGR, cfg->timingr & TIMINGR_CLEAR_MASK); -@@ -338,7 +339,7 @@ static void restore_cfg(struct i2c_handle_s *hi2c, struct i2c_cfg *cfg) - io_write32(base + I2C_CR1, cfg->cr1 & ~I2C_CR1_PE); - io_setbits32(base + I2C_CR1, cfg->cr1 & I2C_CR1_PE); - -- stm32_clock_disable(hi2c->clock); -+ clk_disable(hi2c->clock); - } - - static void __maybe_unused dump_cfg(struct i2c_cfg *cfg __maybe_unused) -@@ -354,7 +355,7 @@ static void __maybe_unused dump_i2c(struct i2c_handle_s *hi2c) - { - vaddr_t __maybe_unused base = get_base(hi2c); - -- stm32_clock_enable(hi2c->clock); -+ clk_enable(hi2c->clock); - - DMSG("CR1: %#"PRIx32, io_read32(base + I2C_CR1)); - DMSG("CR2: %#"PRIx32, io_read32(base + I2C_CR2)); -@@ -362,7 +363,7 @@ static void __maybe_unused dump_i2c(struct i2c_handle_s *hi2c) - DMSG("OAR2: %#"PRIx32, io_read32(base + I2C_OAR2)); - DMSG("TIM: %#"PRIx32, io_read32(base + I2C_TIMINGR)); - -- stm32_clock_disable(hi2c->clock); -+ clk_disable(hi2c->clock); - } - - /* -@@ -597,7 +598,7 @@ static int i2c_setup_timing(struct i2c_handle_s *hi2c, - - assert(i2c_specs_is_consistent()); - -- clock_src = stm32_clock_get_rate(hi2c->clock); -+ clock_src = clk_get_rate(hi2c->clock); - if (!clock_src) { - DMSG("Null I2C clock rate"); - return -1; -@@ -759,7 +760,7 @@ int stm32_i2c_init(struct i2c_handle_s *hi2c, - if (rc) - return rc; - -- stm32_clock_enable(hi2c->clock); -+ clk_enable(hi2c->clock); - base = get_base(hi2c); - hi2c->i2c_state = I2C_STATE_BUSY; - -@@ -819,7 +820,7 @@ int stm32_i2c_init(struct i2c_handle_s *hi2c, - if (rc) - DMSG("I2C analog filter error %d", rc); - -- stm32_clock_disable(hi2c->clock); -+ clk_disable(hi2c->clock); - - return rc; - } -@@ -1074,7 +1075,7 @@ static int i2c_write(struct i2c_handle_s *hi2c, struct i2c_request *request, - if (!p_data || !size) - return -1; - -- stm32_clock_enable(hi2c->clock); -+ clk_enable(hi2c->clock); - - timeout_ref = timeout_init_us(I2C_TIMEOUT_BUSY_MS * 1000); - if (wait_isr_event(hi2c, I2C_ISR_BUSY, 0, timeout_ref)) -@@ -1161,7 +1162,7 @@ static int i2c_write(struct i2c_handle_s *hi2c, struct i2c_request *request, - rc = 0; - - bail: -- stm32_clock_disable(hi2c->clock); -+ clk_disable(hi2c->clock); - - return rc; - } -@@ -1207,7 +1208,7 @@ int stm32_i2c_read_write_membyte(struct i2c_handle_s *hi2c, uint16_t dev_addr, - if (hi2c->i2c_state != I2C_STATE_READY || !p_data) - return -1; - -- stm32_clock_enable(hi2c->clock); -+ clk_enable(hi2c->clock); - - timeout_ref = timeout_init_us(I2C_TIMEOUT_BUSY_US); - if (wait_isr_event(hi2c, I2C_ISR_BUSY, 0, timeout_ref)) -@@ -1264,7 +1265,7 @@ int stm32_i2c_read_write_membyte(struct i2c_handle_s *hi2c, uint16_t dev_addr, - rc = 0; - - bail: -- stm32_clock_disable(hi2c->clock); -+ clk_disable(hi2c->clock); - - return rc; - } -@@ -1297,7 +1298,7 @@ static int i2c_read(struct i2c_handle_s *hi2c, struct i2c_request *request, - if (!p_data || !size) - return -1; - -- stm32_clock_enable(hi2c->clock); -+ clk_enable(hi2c->clock); - - timeout_ref = timeout_init_us(I2C_TIMEOUT_BUSY_MS * 1000); - if (wait_isr_event(hi2c, I2C_ISR_BUSY, 0, timeout_ref)) -@@ -1374,7 +1375,7 @@ static int i2c_read(struct i2c_handle_s *hi2c, struct i2c_request *request, - rc = 0; - - bail: -- stm32_clock_disable(hi2c->clock); -+ clk_disable(hi2c->clock); - - return rc; - } -@@ -1417,7 +1418,7 @@ bool stm32_i2c_is_device_ready(struct i2c_handle_s *hi2c, uint32_t dev_addr, - if (hi2c->i2c_state != I2C_STATE_READY) - return rc; - -- stm32_clock_enable(hi2c->clock); -+ clk_enable(hi2c->clock); - - if (io_read32(base + I2C_ISR) & I2C_ISR_BUSY) - goto bail; -@@ -1489,7 +1490,7 @@ bool stm32_i2c_is_device_ready(struct i2c_handle_s *hi2c, uint32_t dev_addr, - notif_i2c_timeout(hi2c); - - bail: -- stm32_clock_disable(hi2c->clock); -+ clk_disable(hi2c->clock); - - return rc; - } -diff --git a/core/drivers/stm32_iwdg.c b/core/drivers/stm32_iwdg.c -new file mode 100644 -index 000000000..335d1a2cb ---- /dev/null -+++ b/core/drivers/stm32_iwdg.c -@@ -0,0 +1,314 @@ -+// SPDX-License-Identifier: BSD-3-Clause -+/* -+ * Copyright (c) 2017-2020, STMicroelectronics - All Rights Reserved -+ */ -+ -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+ -+/* IWDG Compatibility */ -+#define IWDG_COMPAT "st,stm32mp1-iwdg" -+#define IWDG_TIMEOUT_US 100000U -+ -+/* IWDG registers offsets */ -+#define IWDG_KR_OFFSET 0x00U -+#define IWDG_PR_OFFSET 0x04U -+#define IWDG_RLR_OFFSET 0x08U -+#define IWDG_SR_OFFSET 0x0CU -+#define IWDG_EWCR_OFFSET 0x14U -+ -+/* Registers values */ -+#define IWDG_KR_ACCESS_KEY 0x5555 -+#define IWDG_KR_RELOAD_KEY 0xAAAA -+#define IWDG_KR_START_KEY 0xCCCC -+ -+#define IWDG_PR_DIV_4 0x00 -+#define IWDG_PR_DIV_256 0x06 -+ -+#define IWDG_RLR_MAX_VAL 0xFFF -+ -+#define IWDG_SR_EWU BIT(3) -+ -+#define IWDG_EWCR_EWIE BIT(15) -+#define IWDG_EWCR_EWIC BIT(14) -+#define IWDG_EWCR_EWIT_MASK GENMASK_32(11, 0) -+ -+struct stm32_iwdg_instance { -+ struct io_pa_va base; -+ unsigned long clock; -+ uint8_t instance; -+ uint8_t flags; -+}; -+ -+static struct stm32_iwdg_instance *stm32_iwdg; -+static size_t stm32_iwdg_count; -+ -+static vaddr_t get_base(struct stm32_iwdg_instance *iwdg) -+{ -+ return io_pa_or_va(&iwdg->base); -+} -+ -+static int stm32_iwdg_get_dt_node(void *fdt, struct dt_node_info *info, -+ int offset) -+{ -+ int node = fdt_node_offset_by_compatible(fdt, offset, IWDG_COMPAT); -+ -+ if (node < 0) { -+ if (offset == -1) -+ DMSG("No IDWG found"); -+ -+ return -FDT_ERR_NOTFOUND; -+ } -+ -+ _fdt_fill_device_info(fdt, info, node); -+ -+ return node; -+} -+ -+static struct stm32_iwdg_instance *get_iwdg(unsigned int instance) -+{ -+ size_t i = 0; -+ -+ assert(instance <= UINT8_MAX); -+ for (i = 0; i < stm32_iwdg_count; i++) -+ if (stm32_iwdg[i].instance == instance) -+ return &stm32_iwdg[i]; -+ -+ return NULL; -+} -+ -+static enum itr_return stm32_iwdg_it_handler(struct itr_handler *handler) -+{ -+ unsigned int __maybe_unused cpu = get_core_pos(); -+ unsigned int instance = stm32mp_iwdg_irq2instance(handler->it); -+ struct stm32_iwdg_instance *iwdg = get_iwdg(instance); -+ vaddr_t iwdg_base = get_base(iwdg); -+ -+ DMSG("CPU %u IT Watchdog %d\n", cpu, instance + 1); -+ -+ stm32_iwdg_refresh(instance); -+ -+ clk_enable(iwdg->clock); -+ -+ io_setbits32(iwdg_base + IWDG_EWCR_OFFSET, IWDG_EWCR_EWIC); -+ -+ clk_disable(iwdg->clock); -+ -+#ifdef CFG_PM -+ /* -+ * Ack interrupt as we do not return from next call. -+ * And interrupt is no more considered as pending here. -+ */ -+ stm32mp_gic_set_end_of_interrupt(handler->it); -+ -+ stm32_cores_reset(); -+#else -+ panic("Watchdog"); -+#endif -+ -+ return ITRR_HANDLED; -+} -+DECLARE_KEEP_PAGER(stm32_iwdg_it_handler); -+ -+static int stm32_iwdg_get_secure_timeout(void *fdt, int node) -+{ -+ const fdt32_t *cuint = NULL; -+ -+ cuint = fdt_getprop(fdt, node, "secure-timeout-sec", NULL); -+ if (!cuint) -+ return -1; -+ -+ return (int)fdt32_to_cpu(*cuint); -+} -+ -+static int fdt_get_clock_id_by_name(void *fdt, int node, const char *name) -+{ -+ const fdt32_t *cuint = NULL; -+ int index = 0; -+ int len = 0; -+ -+ index = fdt_stringlist_search(fdt, node, "clock-names", name); -+ if (index < 0) -+ return index; -+ -+ cuint = fdt_getprop(fdt, node, "clocks", &len); -+ if (!cuint) -+ return -FDT_ERR_NOTFOUND; -+ -+ if ((index * (int)sizeof(uint32_t)) > len) -+ return -FDT_ERR_BADVALUE; -+ -+ cuint += (index << 1) + 1; -+ return (int)fdt32_to_cpu(*cuint); -+} -+ -+static TEE_Result stm32_iwdg_conf_etimeout(void *fdt, int node, -+ struct stm32_iwdg_instance *iwdg) -+{ -+ int id_lsi = 0; -+ int dt_secure_timeout = stm32_iwdg_get_secure_timeout(fdt, node); -+ uint32_t reload = 0; -+ uint32_t status = 0; -+ uint64_t timeout_ref = 0; -+ unsigned long long reload_ll = 0; -+ vaddr_t iwdg_base = get_base(iwdg); -+ struct itr_handler *itr = NULL; -+ -+ if (dt_secure_timeout < 0) -+ return TEE_SUCCESS; -+ -+ if (dt_secure_timeout == 0) -+ return TEE_ERROR_GENERIC; -+ -+ id_lsi = fdt_get_clock_id_by_name(fdt, node, "lsi"); -+ if (id_lsi < 0) -+ return TEE_ERROR_GENERIC; -+ -+ /* Prescaler fix to 256 */ -+ reload_ll = (unsigned long long)dt_secure_timeout * -+ clk_get_rate(id_lsi); -+ reload = ((uint32_t)(reload_ll >> 8) - 1) & IWDG_EWCR_EWIT_MASK; -+ -+ clk_enable(iwdg->clock); -+ -+ io_write32(iwdg_base + IWDG_KR_OFFSET, IWDG_KR_START_KEY); -+ io_write32(iwdg_base + IWDG_KR_OFFSET, IWDG_KR_ACCESS_KEY); -+ io_write32(iwdg_base + IWDG_PR_OFFSET, IWDG_PR_DIV_256); -+ io_write32(iwdg_base + IWDG_EWCR_OFFSET, reload | IWDG_EWCR_EWIE); -+ -+ timeout_ref = timeout_init_us(IWDG_TIMEOUT_US); -+ while (!timeout_elapsed(timeout_ref)) -+ if (!(io_read32(iwdg_base + IWDG_SR_OFFSET) & IWDG_SR_EWU)) -+ break; -+ -+ status = io_read32(iwdg_base + IWDG_SR_OFFSET) & IWDG_SR_EWU; -+ clk_disable(iwdg->clock); -+ if (status) -+ return TEE_ERROR_GENERIC; -+ -+ itr = calloc(1, sizeof(*itr)); -+ if (!itr) -+ panic("out of memory"); -+ -+ itr->it = stm32mp_iwdg_instance2irq(iwdg->instance); -+ itr->handler = stm32_iwdg_it_handler; -+ itr_add(itr); -+ itr_enable(itr->it); -+ -+ return TEE_SUCCESS; -+} -+ -+void stm32_iwdg_refresh(unsigned int instance) -+{ -+ struct stm32_iwdg_instance *iwdg = get_iwdg(instance); -+ -+ if (!iwdg) -+ return; -+ -+ clk_enable(iwdg->clock); -+ -+ io_write32(get_base(iwdg) + IWDG_KR_OFFSET, IWDG_KR_RELOAD_KEY); -+ -+ clk_disable(iwdg->clock); -+} -+ -+static TEE_Result iwdg_init(void) -+{ -+ int node = -1; -+ TEE_Result res = TEE_ERROR_GENERIC; -+ struct dt_node_info dt_info = { }; -+ void *fdt = NULL; -+ size_t count = 0; -+ -+ fdt = get_embedded_dt(); -+ if (!fdt) -+ panic(); -+ -+ assert(!stm32_iwdg && !stm32_iwdg_count); -+ -+ for (node = stm32_iwdg_get_dt_node(fdt, &dt_info, node); -+ node != -FDT_ERR_NOTFOUND; -+ node = stm32_iwdg_get_dt_node(fdt, &dt_info, node)) { -+ struct stm32_iwdg_instance iwdg = { }; -+ enum teecore_memtypes memtype = MEM_AREA_MAXTYPE; -+ uint32_t hw_init = 0; -+ -+ iwdg.base.pa = dt_info.reg; -+ iwdg.clock = (unsigned long)dt_info.clock; -+ iwdg.instance = stm32mp_iwdg_iomem2instance(iwdg.base.pa); -+ -+ memtype = ((dt_info.status & DT_STATUS_OK_NSEC) != 0) ? -+ MEM_AREA_IO_NSEC : MEM_AREA_IO_SEC; -+ iwdg.base.va = (vaddr_t)phys_to_virt(iwdg.base.pa, memtype); -+ -+ /* DT can specify low power cases */ -+ if (!fdt_getprop(fdt, node, "stm32,enable-on-stop", NULL)) -+ iwdg.flags |= IWDG_DISABLE_ON_STOP; -+ -+ if (!fdt_getprop(fdt, node, "stm32,enable-on-standby", NULL)) -+ iwdg.flags |= IWDG_DISABLE_ON_STANDBY; -+ -+ hw_init = stm32_get_iwdg_otp_config(iwdg.base.pa); -+ -+ if (hw_init & IWDG_HW_ENABLED) { -+ if (dt_info.status == DT_STATUS_DISABLED) -+ panic("IWDG HW enabled"); -+ -+ iwdg.flags |= IWDG_HW_ENABLED; -+ } -+ -+ if (hw_init & IWDG_DISABLE_ON_STOP) -+ iwdg.flags |= IWDG_DISABLE_ON_STOP; -+ -+ if (hw_init & IWDG_DISABLE_ON_STANDBY) -+ iwdg.flags |= IWDG_DISABLE_ON_STANDBY; -+ -+ if (dt_info.status == DT_STATUS_DISABLED) -+ continue; -+ -+ DMSG("IWDG%u found, %ssecure", iwdg.instance + 1, -+ (dt_info.status & DT_STATUS_OK_NSEC) ? "non-" : ""); -+ -+ if (dt_info.status & DT_STATUS_OK_NSEC) -+ stm32mp_register_non_secure_periph_iomem(iwdg.base.pa); -+ else -+ stm32mp_register_secure_periph_iomem(iwdg.base.pa); -+ -+ res = stm32_iwdg_conf_etimeout(fdt, node, &iwdg); -+ if (res) { -+ EMSG("IWDG%x early timeout config failed (%d)\n", -+ iwdg.instance + 1, res); -+ panic(); -+ } -+ -+ stm32_iwdg = realloc(stm32_iwdg, (count + 1) * sizeof(iwdg)); -+ if (!stm32_iwdg) -+ panic("out of memory"); -+ -+ memcpy(&stm32_iwdg[count], &iwdg, sizeof(iwdg)); -+ count++; -+ } -+ -+ stm32_iwdg_count = count; -+ -+ DMSG("%u IWDG instance%s found", count, count > 1 ? "s" : ""); -+ -+ return TEE_SUCCESS; -+} -+driver_init(iwdg_init); -diff --git a/core/drivers/stm32_rng.c b/core/drivers/stm32_rng.c -index d55f28f19..e95bde8ad 100644 ---- a/core/drivers/stm32_rng.c -+++ b/core/drivers/stm32_rng.c -@@ -1,9 +1,10 @@ - // SPDX-License-Identifier: BSD-3-Clause - /* -- * Copyright (c) 2018-2019, STMicroelectronics -+ * Copyright (c) 2018-2020, STMicroelectronics - */ - - #include -+#include - #include - #include - #include -@@ -129,7 +130,7 @@ static void gate_rng(bool enable, struct stm32_rng_instance *dev) - if (enable) { - /* incr_refcnt return non zero if resource shall be enabled */ - if (incr_refcnt(&dev->refcount)) { -- stm32_clock_enable(dev->clock); -+ clk_enable(dev->clock); - io_write32(rng_cr, 0); - io_write32(rng_cr, RNG_CR_RNGEN | RNG_CR_CED); - } -@@ -137,7 +138,7 @@ static void gate_rng(bool enable, struct stm32_rng_instance *dev) - /* decr_refcnt return non zero if resource shall be disabled */ - if (decr_refcnt(&dev->refcount)) { - io_write32(rng_cr, 0); -- stm32_clock_disable(dev->clock); -+ clk_disable(dev->clock); - } - } - -diff --git a/core/drivers/stm32_rtc.c b/core/drivers/stm32_rtc.c -new file mode 100644 -index 000000000..79ab627e6 ---- /dev/null -+++ b/core/drivers/stm32_rtc.c -@@ -0,0 +1,446 @@ -+// SPDX-License-Identifier: BSD-3-Clause -+/* -+ * Copyright (c) 2018-2020, STMicroelectronics - All Rights Reserved -+ * -+ */ -+ -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+ -+#define RTC_COMPAT "st,stm32mp1-rtc" -+ -+#define RTC_TR 0x00 -+#define RTC_DR 0x04 -+#define RTC_SSR 0x08 -+#define RTC_ICSR 0x0C -+#define RTC_PRER 0x10 -+#define RTC_WUTR 0x14 -+#define RTC_CR 0x18 -+#define RTC_SMCR 0x20 -+#define RTC_WPR 0x24 -+#define RTC_CALR 0x28 -+#define RTC_SHIFTR 0x2C -+#define RTC_TSTR 0x30 -+#define RTC_TSDR 0x34 -+#define RTC_TSSSR 0x38 -+#define RTC_ALRMAR 0x40 -+#define RTC_ALRMASSR 0x44 -+#define RTC_ALRMBR 0x48 -+#define RTC_ALRMBSSR 0x4C -+#define RTC_SR 0x50 -+#define RTC_SCR 0x5C -+#define RTC_OR 0x60 -+ -+#define RTC_TR_SU_MASK GENMASK_32(3, 0) -+#define RTC_TR_ST_MASK GENMASK_32(6, 4) -+#define RTC_TR_ST_SHIFT 4 -+#define RTC_TR_MNU_MASK GENMASK_32(11, 8) -+#define RTC_TR_MNU_SHIFT 8 -+#define RTC_TR_MNT_MASK GENMASK_32(14, 12) -+#define RTC_TR_MNT_SHIFT 12 -+#define RTC_TR_HU_MASK GENMASK_32(19, 16) -+#define RTC_TR_HU_SHIFT 16 -+#define RTC_TR_HT_MASK GENMASK_32(21, 20) -+#define RTC_TR_HT_SHIFT 20 -+#define RTC_TR_PM BIT(22) -+ -+#define RTC_DR_DU_MASK GENMASK_32(3, 0) -+#define RTC_DR_DT_MASK GENMASK_32(5, 4) -+#define RTC_DR_DT_SHIFT 4 -+#define RTC_DR_MU_MASK GENMASK_32(11, 8) -+#define RTC_DR_MU_SHIFT 8 -+#define RTC_DR_MT BIT(12) -+#define RTC_DR_MT_SHIFT 12 -+#define RTC_DR_WDU_MASK GENMASK_32(15, 13) -+#define RTC_DR_WDU_SHIFT 13 -+#define RTC_DR_YU_MASK GENMASK_32(19, 16) -+#define RTC_DR_YU_SHIFT 16 -+#define RTC_DR_YT_MASK GENMASK_32(23, 20) -+#define RTC_DR_YT_SHIFT 20 -+ -+#define RTC_SSR_SS_MASK GENMASK_32(15, 0) -+ -+#define RTC_ICSR_RSF BIT(5) -+ -+#define RTC_PRER_PREDIV_S_MASK GENMASK_32(15, 0) -+ -+#define RTC_CR_BYPSHAD BIT(5) -+#define RTC_CR_BYPSHAD_SHIFT 5 -+#define RTC_CR_TAMPTS BIT(25) -+ -+#define RTC_SMCR_TS_DPROT BIT(3) -+#define RTC_SR_TSF BIT(3) -+#define RTC_SCR_CTSF BIT(3) -+#define RTC_SR_TSOVF BIT(4) -+#define RTC_SCR_CTSOVF BIT(4) -+ -+#define RTC_TSDR_MU_MASK GENMASK_32(11, 8) -+#define RTC_TSDR_MU_SHIFT 8 -+#define RTC_TSDR_DT_MASK GENMASK_32(5, 4) -+#define RTC_TSDR_DT_SHIFT 4 -+#define RTC_TSDR_DU_MASK GENMASK_32(3, 0) -+#define RTC_TSDR_DU_SHIFT 0 -+ -+#define RTC_WPR_KEY1 0xCA -+#define RTC_WPR_KEY2 0x53 -+#define RTC_WPR_KEY_LOCK 0xFF -+ -+#define RTC_FLAGS_READ_TWICE BIT(0) -+#define RTC_FLAGS_SECURE BIT(1) -+ -+struct rtc_device { -+ struct io_pa_va base; -+ uint16_t clock; -+ uint8_t flags; -+}; -+ -+/* Expect a single RTC instance */ -+struct rtc_device rtc_dev; -+ -+static vaddr_t get_base(void) -+{ -+ assert(rtc_dev.base.pa); -+ return io_pa_or_va(&rtc_dev.base); -+} -+ -+static void stm32_rtc_write_unprotect(void) -+{ -+ vaddr_t rtc_base = get_base(); -+ -+ io_write32(rtc_base + RTC_WPR, RTC_WPR_KEY1); -+ io_write32(rtc_base + RTC_WPR, RTC_WPR_KEY2); -+} -+ -+static void stm32_rtc_write_protect(void) -+{ -+ vaddr_t rtc_base = get_base(); -+ -+ io_write32(rtc_base + RTC_WPR, RTC_WPR_KEY_LOCK); -+} -+ -+static bool stm32_rtc_get_bypshad(void) -+{ -+ return io_read32(get_base() + RTC_CR) & RTC_CR_BYPSHAD; -+} -+ -+/* Get calendar data from RTC devicecalendar valueregister values */ -+static void stm32_rtc_read_calendar(struct stm32_rtc_calendar *calendar) -+{ -+ vaddr_t rtc_base = get_base(); -+ bool bypshad = stm32_rtc_get_bypshad(); -+ -+ if (!bypshad) { -+ /* Shadow RTC registers */ -+ io_clrbits32(rtc_base + RTC_ICSR, RTC_ICSR_RSF); -+ while (!(io_read32(rtc_base + RTC_ICSR) & RTC_ICSR_RSF)) -+ ; -+ } -+ -+ calendar->ssr = io_read32(rtc_base + RTC_SSR); -+ calendar->tr = io_read32(rtc_base + RTC_TR); -+ calendar->dr = io_read32(rtc_base + RTC_DR); -+} -+ -+/* Fill the RTC timestamp structure from a given RTC time-in-day value */ -+static void stm32_rtc_get_time(struct stm32_rtc_calendar *cal, -+ struct stm32_rtc_time *tm) -+{ -+ tm->hour = (((cal->tr & RTC_TR_HT_MASK) >> RTC_TR_HT_SHIFT) * 10) + -+ ((cal->tr & RTC_TR_HU_MASK) >> RTC_TR_HU_SHIFT); -+ -+ if (cal->tr & RTC_TR_PM) -+ tm->hour += 12; -+ -+ tm->min = (((cal->tr & RTC_TR_MNT_MASK) >> RTC_TR_MNT_SHIFT) * 10) + -+ ((cal->tr & RTC_TR_MNU_MASK) >> RTC_TR_MNU_SHIFT); -+ tm->sec = (((cal->tr & RTC_TR_ST_MASK) >> RTC_TR_ST_SHIFT) * 10) + -+ (cal->tr & RTC_TR_SU_MASK); -+} -+ -+/* Fill the RTC timestamp structure from a given RTC date value */ -+static void stm32_rtc_get_date(struct stm32_rtc_calendar *cal, -+ struct stm32_rtc_time *tm) -+{ -+ tm->wday = (((cal->dr & RTC_DR_WDU_MASK) >> RTC_DR_WDU_SHIFT)); -+ -+ tm->day = (((cal->dr & RTC_DR_DT_MASK) >> RTC_DR_DT_SHIFT) * 10) + -+ (cal->dr & RTC_DR_DU_MASK); -+ -+ tm->month = (((cal->dr & RTC_DR_MT) >> RTC_DR_MT_SHIFT) * 10) + -+ ((cal->dr & RTC_DR_MU_MASK) >> RTC_DR_MU_SHIFT); -+ -+ tm->year = (((cal->dr & RTC_DR_YT_MASK) >> RTC_DR_YT_SHIFT) * 10) + -+ ((cal->dr & RTC_DR_YU_MASK) >> RTC_DR_YU_SHIFT) + 2000; -+} -+ -+/* Update time value with RTC timestamp */ -+static void stm32_rtc_read_timestamp(struct stm32_rtc_time *time) -+{ -+ struct stm32_rtc_calendar cal_tamp = { }; -+ vaddr_t rtc_base = get_base(); -+ -+ cal_tamp.tr = io_read32(rtc_base + RTC_TSTR); -+ cal_tamp.dr = io_read32(rtc_base + RTC_TSDR); -+ stm32_rtc_get_time(&cal_tamp, time); -+ stm32_rtc_get_date(&cal_tamp, time); -+} -+ -+void stm32_rtc_get_calendar(struct stm32_rtc_calendar *calendar) -+{ -+ clk_enable(rtc_dev.clock); -+ -+ stm32_rtc_read_calendar(calendar); -+ -+ /* RTC may need to be read twice, depending of clocks configuration */ -+ if (rtc_dev.flags & RTC_FLAGS_READ_TWICE) { -+ uint32_t tr_save = calendar->tr; -+ -+ stm32_rtc_read_calendar(calendar); -+ -+ if (calendar->tr != tr_save) -+ stm32_rtc_read_calendar(calendar); -+ } -+ -+ clk_disable(rtc_dev.clock); -+} -+ -+/* Return difference in milliseconds on second fraction */ -+static uint32_t stm32_rtc_get_second_fraction(struct stm32_rtc_calendar *cal) -+{ -+ uint32_t prediv_s = io_read32(get_base() + RTC_PRER) & -+ RTC_PRER_PREDIV_S_MASK; -+ uint32_t ss = cal->ssr & RTC_SSR_SS_MASK; -+ -+ return ((prediv_s - ss) * 1000) / (prediv_s + 1); -+} -+ -+/* Return absolute difference in milliseconds on second fraction */ -+static signed long long stm32_rtc_diff_frac(struct stm32_rtc_calendar *cur, -+ struct stm32_rtc_calendar *ref) -+{ -+ return (signed long long)stm32_rtc_get_second_fraction(cur) - -+ (signed long long)stm32_rtc_get_second_fraction(ref); -+} -+ -+/* Return absolute difference in milliseconds on seconds-in-day fraction */ -+static signed long long stm32_rtc_diff_time(struct stm32_rtc_time *current, -+ struct stm32_rtc_time *ref) -+{ -+ signed long long curr_s = 0; -+ signed long long ref_s = 0; -+ -+ curr_s = (signed long long)current->sec + -+ (((signed long long)current->min + -+ (((signed long long)current->hour * 60))) * 60); -+ -+ ref_s = (signed long long)ref->sec + -+ (((signed long long)ref->min + -+ (((signed long long)ref->hour * 60))) * 60); -+ -+ return (curr_s - ref_s) * 1000U; -+} -+ -+static bool stm32_is_a_leap_year(uint32_t year) -+{ -+ return ((year % 4) == 0) && -+ (((year % 100) != 0) || ((year % 400) == 0)); -+} -+ -+/* Return absolute difference in milliseconds on day-in-year fraction */ -+static signed long long stm32_rtc_diff_date(struct stm32_rtc_time *current, -+ struct stm32_rtc_time *ref) -+{ -+ uint32_t diff_in_days = 0; -+ uint32_t m = 0; -+ const uint8_t month_len[NB_MONTHS] = { -+ 31, 28, 31, 30, 31, 30, -+ 31, 31, 30, 31, 30, 31 -+ }; -+ -+ /* Get the number of non-entire month days */ -+ if (current->day >= ref->day) -+ diff_in_days += current->day - ref->day; -+ else -+ diff_in_days += month_len[ref->month - 1] - -+ ref->day + current->day; -+ -+ /* Get the number of entire months, and compute the related days */ -+ if (current->month > (ref->month + 1)) -+ for (m = ref->month + 1; m < current->month && m < 12; m++) -+ diff_in_days += month_len[m - 1]; -+ -+ if (current->month < (ref->month - 1)) { -+ for (m = 1; m < current->month && m < 12; m++) -+ diff_in_days += month_len[m - 1]; -+ -+ for (m = ref->month + 1; m < 12; m++) -+ diff_in_days += month_len[m - 1]; -+ } -+ -+ /* Get complete years */ -+ if (current->year > (ref->year + 1)) -+ diff_in_days += (current->year - ref->year - 1) * 365; -+ -+ /* Particular cases: leap years (one day more) */ -+ if (diff_in_days > 0) { -+ if (current->year == ref->year) { -+ if (stm32_is_a_leap_year(current->year) && -+ ref->month <= 2 && -+ current->month >= 3 && current->day <= 28) -+ diff_in_days++; -+ } else { -+ uint32_t y = 0; -+ -+ /* Ref year is leap */ -+ if (stm32_is_a_leap_year(ref->year) && -+ ref->month <= 2 && ref->day <= 28) -+ diff_in_days++; -+ -+ /* Current year is leap */ -+ if (stm32_is_a_leap_year(current->year) && -+ current->month >= 3) -+ diff_in_days++; -+ -+ /* Interleaved years are leap */ -+ for (y = ref->year + 1; y < current->year; y++) -+ if (stm32_is_a_leap_year(y)) -+ diff_in_days++; -+ } -+ } -+ -+ return (24 * 60 * 60 * 1000) * (signed long long)diff_in_days; -+} -+ -+unsigned long long stm32_rtc_diff_calendar(struct stm32_rtc_calendar *cur, -+ struct stm32_rtc_calendar *ref) -+{ -+ signed long long diff_in_ms = 0; -+ struct stm32_rtc_time curr_t = { }; -+ struct stm32_rtc_time ref_t = { }; -+ -+ stm32_rtc_get_date(cur, &curr_t); -+ stm32_rtc_get_date(ref, &ref_t); -+ stm32_rtc_get_time(cur, &curr_t); -+ stm32_rtc_get_time(ref, &ref_t); -+ -+ diff_in_ms += stm32_rtc_diff_frac(cur, ref); -+ diff_in_ms += stm32_rtc_diff_time(&curr_t, &ref_t); -+ diff_in_ms += stm32_rtc_diff_date(&curr_t, &ref_t); -+ -+ return (unsigned long long)diff_in_ms; -+} -+ -+void stm32_rtc_get_timestamp(struct stm32_rtc_time *tamp_ts) -+{ -+ vaddr_t rtc_base = get_base(); -+ -+ clk_enable(rtc_dev.clock); -+ -+ if (io_read32(rtc_base + RTC_SR) & RTC_SR_TSF) { -+ /* Timestamp for tamper event */ -+ stm32_rtc_read_timestamp(tamp_ts); -+ io_setbits32(rtc_base + RTC_SCR, RTC_SCR_CTSF); -+ -+ /* Overflow detection */ -+ if (io_read32(rtc_base + RTC_SR) & RTC_SR_TSOVF) -+ io_setbits32(rtc_base + RTC_SCR, RTC_SCR_CTSOVF); -+ } -+ -+ clk_disable(rtc_dev.clock); -+} -+ -+void stm32_rtc_set_tamper_timestamp(void) -+{ -+ vaddr_t rtc_base = get_base(); -+ -+ clk_enable(rtc_dev.clock); -+ -+ stm32_rtc_write_unprotect(); -+ -+ /* Enable tamper timestamper */ -+ io_setbits32(rtc_base + RTC_CR, RTC_CR_TAMPTS); -+ -+ /* Secure Timestamp bit */ -+ io_clrbits32(rtc_base + RTC_SMCR, RTC_SMCR_TS_DPROT); -+ -+ stm32_rtc_write_protect(); -+ -+ clk_disable(rtc_dev.clock); -+} -+ -+bool stm32_rtc_is_timestamp_enable(void) -+{ -+ bool ret = false; -+ -+ clk_enable(rtc_dev.clock); -+ -+ ret = io_read32(get_base() + RTC_CR) & RTC_CR_TAMPTS; -+ -+ clk_disable(rtc_dev.clock); -+ -+ return ret; -+} -+ -+#ifdef CFG_DT -+static unsigned int get_second_clock(void *fdt, int offs) -+{ -+ const fdt32_t *cuint = NULL; -+ int len = 0; -+ -+ cuint = fdt_getprop(fdt, offs, "clocks", &len); -+ if (len == 3) { -+ EMSG("RTC expects 2 clocks, %d found", len); -+ return DT_INFO_INVALID_CLOCK; -+ } -+ -+ return fdt32_to_cpu(*(cuint + 3)); -+} -+ -+static TEE_Result stm32_rtc_init(void) -+{ -+ int node = 0; -+ struct dt_node_info dt_info = { }; -+ void *fdt = get_embedded_dt(); -+ -+ if (!fdt) -+ panic(); -+ -+ node = fdt_node_offset_by_compatible(fdt, -1, RTC_COMPAT); -+ if (node < 0) -+ return TEE_SUCCESS; -+ -+ _fdt_fill_device_info(fdt, &dt_info, node); -+ -+ rtc_dev.base.pa = dt_info.reg; -+ -+ if (dt_info.status == DT_STATUS_OK_SEC) { -+ rtc_dev.flags |= RTC_FLAGS_SECURE; -+ stm32mp_register_secure_periph_iomem(rtc_dev.base.pa); -+ rtc_dev.base.va = (vaddr_t)phys_to_virt(rtc_dev.base.pa, -+ MEM_AREA_IO_SEC); -+ /* Unbalanced clock enable: keep RTC running */ -+ clk_enable(get_second_clock(fdt, node)); -+ } else { -+ stm32mp_register_non_secure_periph_iomem(rtc_dev.base.pa); -+ rtc_dev.base.va = (vaddr_t)phys_to_virt(rtc_dev.base.pa, -+ MEM_AREA_IO_NSEC); -+ } -+ -+ rtc_dev.clock = (unsigned long)dt_info.clock; -+ -+ if (stm32_rtc_get_read_twice()) -+ rtc_dev.flags |= RTC_FLAGS_READ_TWICE; -+ -+ return 0; -+} -+driver_init(stm32_rtc_init); -+#endif -diff --git a/core/drivers/stm32_tim.c b/core/drivers/stm32_tim.c -new file mode 100644 -index 000000000..635e13824 ---- /dev/null -+++ b/core/drivers/stm32_tim.c -@@ -0,0 +1,298 @@ -+// SPDX-License-Identifier: BSD-3-Clause -+/* -+ * Copyright (c) 2018-2020, STMicroelectronics -+ */ -+ -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+ -+#define TIM_CR1 0x00 /* Control Register 1 */ -+#define TIM_CR2 0x04 /* Control Register 2 */ -+#define TIM_SMCR 0x08 /* Slave mode control reg */ -+#define TIM_DIER 0x0C /* DMA/interrupt register */ -+#define TIM_SR 0x10 /* Status register */ -+#define TIM_EGR 0x14 /* Event Generation Reg */ -+#define TIM_CCMR1 0x18 /* Capt/Comp 1 Mode Reg */ -+#define TIM_CCMR2 0x1C /* Capt/Comp 2 Mode Reg */ -+#define TIM_CCER 0x20 /* Capt/Comp Enable Reg */ -+#define TIM_CNT 0x24 /* Counter */ -+#define TIM_PSC 0x28 /* Prescaler */ -+#define TIM_ARR 0x2C /* Auto-Reload Register */ -+#define TIM_CCR1 0x34 /* Capt/Comp Register 1 */ -+#define TIM_CCR2 0x38 /* Capt/Comp Register 2 */ -+#define TIM_CCR3 0x3C /* Capt/Comp Register 3 */ -+#define TIM_CCR4 0x40 /* Capt/Comp Register 4 */ -+#define TIM_BDTR 0x44 /* Break and Dead-Time Reg */ -+#define TIM_DCR 0x48 /* DMA control register */ -+#define TIM_DMAR 0x4C /* DMA transfer register */ -+#define TIM_AF1 0x60 /* Alt Function Reg 1 */ -+#define TIM_AF2 0x64 /* Alt Function Reg 2 */ -+#define TIM_TISEL 0x68 /* Input Selection */ -+ -+#define TIM_CR1_CEN BIT(0) -+#define TIM_SMCR_SMS GENMASK_32(2, 0) /* Slave mode selection */ -+#define TIM_SMCR_TS GENMASK_32(6, 4) /* Trigger selection */ -+#define TIM_CCMR_CC1S_TI1 BIT(0) /* IC1/IC3 selects TI1/TI3 */ -+#define TIM_CCMR_CC1S_TI2 BIT(1) /* IC1/IC3 selects TI2/TI4 */ -+#define TIM_CCMR_CC2S_TI2 BIT(8) /* IC2/IC4 selects TI2/TI4 */ -+#define TIM_CCMR_CC2S_TI1 BIT(9) /* IC2/IC4 selects TI1/TI3 */ -+#define TIM_CCER_CC1E BIT(0) /* Capt/Comp 1 out Ena */ -+#define TIM_CCER_CC1P BIT(1) /* Capt/Comp 1 Polarity */ -+#define TIM_CCER_CC1NP BIT(3) /* Capt/Comp 1N Polarity */ -+#define TIM_SR_UIF BIT(0) /* UIF interrupt flag */ -+#define TIM_SR_CC1IF BIT(1) /* CC1 interrupt flag */ -+#define TIM_TISEL_TI1SEL_MASK GENMASK_32(3, 0) -+#define TIM_SMCR_SMS_RESET BIT(2) -+#define TIM_SMCR_TS_SHIFT 4 -+#define TIM_SMCR_TS_TI1FP1 0x5 -+ -+#define TIM_COMPAT "st,stm32-timers" -+#define TIM_TIMEOUT_US 100000 -+#define TIM_TIMEOUT_STEP_US 10 -+#define TIM_PRESCAL_HSI 10 -+#define TIM_PRESCAL_CSI 7 -+#define TIM_MIN_FREQ_CALIB 50000000U -+#define TIM_THRESHOLD 1U -+ -+struct stm32_tim_instance { -+ struct io_pa_va base; -+ unsigned long clk; -+ unsigned long freq; -+ uint8_t cal_input; -+}; -+ -+static vaddr_t timer_base(struct stm32_tim_instance *timer) -+{ -+ return io_pa_or_va(&timer->base); -+} -+ -+/* Currently support HSI and CSI calibratrion */ -+#define TIM_MAX_INSTANCE 2 -+static struct stm32_tim_instance stm32_tim[TIM_MAX_INSTANCE]; -+ -+static int timer_get_dt_node(void *fdt, struct dt_node_info *info, int offset) -+{ -+ int node = fdt_node_offset_by_compatible(fdt, offset, TIM_COMPAT); -+ -+ if (node < 0) -+ return -FDT_ERR_NOTFOUND; -+ -+ _fdt_fill_device_info(fdt, info, node); -+ -+ return node; -+} -+ -+static int timer_config(struct stm32_tim_instance *timer) -+{ -+ vaddr_t base = timer_base(timer); -+ -+ clk_enable(timer->clk); -+ -+ timer->freq = clk_get_rate(timer->clk); -+ if (timer->freq < TIM_MIN_FREQ_CALIB) { -+ EMSG("Calibration: timer not accurate enough"); -+ clk_disable(timer->clk); -+ return -1; -+ } -+ -+ if ((io_read32(base + TIM_TISEL) & TIM_TISEL_TI1SEL_MASK) != -+ timer->cal_input) { -+ io_clrsetbits32(base + TIM_CCMR1, -+ TIM_CCMR_CC1S_TI1 | TIM_CCMR_CC1S_TI2, -+ TIM_CCMR_CC1S_TI1); -+ -+ io_clrbits32(base + TIM_CCER, -+ TIM_CCER_CC1P | TIM_CCER_CC1NP); -+ -+ io_clrsetbits32(base + TIM_SMCR, -+ TIM_SMCR_TS | TIM_SMCR_SMS, -+ (TIM_SMCR_TS_TI1FP1 << TIM_SMCR_TS_SHIFT) | -+ TIM_SMCR_SMS_RESET); -+ -+ io_write32(base + TIM_TISEL, timer->cal_input); -+ io_setbits32(base + TIM_CR1, TIM_CR1_CEN); -+ io_setbits32(base + TIM_CCER, TIM_CCER_CC1E); -+ } -+ -+ clk_disable(timer->clk); -+ -+ return 0; -+} -+ -+static uint32_t timer_start_capture(struct stm32_tim_instance *timer) -+{ -+ uint64_t timeout_ref = 0; -+ uint64_t timeout_conv = 0; -+ uint32_t counter = 0; -+ uint32_t old_counter = 0; -+ vaddr_t base = timer_base(timer); -+ -+ if (timer_config(timer)) -+ return 0; -+ -+ clk_enable(timer->clk); -+ -+ io_write32(base + TIM_SR, 0); -+ -+ timeout_ref = timeout_init_us(TIM_TIMEOUT_US / TIM_TIMEOUT_STEP_US); -+ -+ while (!timeout_elapsed(timeout_ref)) -+ if (io_read32(base + TIM_SR) & TIM_SR_UIF) -+ break; -+ if (!(io_read32(base + TIM_SR) & TIM_SR_UIF)) -+ goto bail; -+ -+ io_write32(base + TIM_SR, 0); -+ -+ timeout_conv = timeout_init_us(TIM_TIMEOUT_US); -+ -+ do { -+ if (timeout_elapsed(timeout_conv)) { -+ counter = 0; -+ goto bail; -+ } -+ -+ timeout_ref = timeout_init_us(TIM_TIMEOUT_US / -+ TIM_TIMEOUT_STEP_US); -+ while (!timeout_elapsed(timeout_ref)) -+ if (io_read32(base + TIM_SR) & TIM_SR_CC1IF) -+ break; -+ -+ if (!(io_read32(base + TIM_SR) & TIM_SR_CC1IF)) { -+ counter = 0; -+ goto bail; -+ } -+ -+ old_counter = counter; -+ counter = io_read32(base + TIM_CCR1); -+ } while (MAX(counter, old_counter) - MIN(counter, old_counter) > -+ TIM_THRESHOLD); -+ -+bail: -+ clk_disable(timer->clk); -+ -+ return counter; -+} -+ -+unsigned long stm32_tim_hsi_freq(void) -+{ -+ struct stm32_tim_instance *timer = &stm32_tim[HSI_CAL]; -+ uint32_t counter = 0; -+ -+ if (timer->base.pa) -+ counter = timer_start_capture(timer); -+ -+ if (!counter) -+ return 0; -+ -+ return (timer->freq / counter) << TIM_PRESCAL_HSI; -+} -+DECLARE_KEEP_PAGER(stm32_tim_hsi_freq); -+ -+unsigned long stm32_tim_csi_freq(void) -+{ -+ struct stm32_tim_instance *timer = &stm32_tim[CSI_CAL]; -+ uint32_t counter = 0; -+ -+ if (timer->base.pa) -+ counter = timer_start_capture(timer); -+ -+ if (!counter) -+ return 0; -+ -+ return (timer->freq / counter) << TIM_PRESCAL_CSI; -+} -+DECLARE_KEEP_PAGER(stm32_tim_csi_freq); -+ -+static void _init_stm32_tim(void) -+{ -+ void *fdt = get_embedded_dt(); -+ struct dt_node_info dt_timer = { }; -+ int node = -1; -+ static bool inited; -+ -+ if (inited) -+ return; -+ inited = true; -+ -+ if (!fdt) -+ panic(); -+ -+ for (node = timer_get_dt_node(fdt, &dt_timer, node); -+ node != -FDT_ERR_NOTFOUND; -+ node = timer_get_dt_node(fdt, &dt_timer, node)) { -+ struct stm32_tim_instance *timer = NULL; -+ const uint32_t *cuint = NULL; -+ -+ if (!(dt_timer.status & DT_STATUS_OK_SEC)) -+ continue; -+ -+ cuint = fdt_getprop(fdt, node, "st,hsi-cal-input", NULL); -+ if (cuint) { -+ timer = &stm32_tim[HSI_CAL]; -+ timer->base.pa = dt_timer.reg; -+ timer->clk = dt_timer.clock; -+ timer->freq = clk_get_rate(timer->clk); -+ timer->cal_input = fdt32_to_cpu(*cuint); -+ if (timer_config(timer)) { -+ timer->base.pa = 0; -+ continue; -+ } -+ } -+ -+ cuint = fdt_getprop(fdt, node, "st,csi-cal-input", NULL); -+ if (cuint) { -+ timer = &stm32_tim[CSI_CAL]; -+ timer->base.pa = dt_timer.reg; -+ timer->clk = dt_timer.clock; -+ timer->freq = clk_get_rate(timer->clk); -+ timer->cal_input = fdt32_to_cpu(*cuint); -+ if (timer_config(timer)) { -+ timer->base.pa = 0; -+ continue; -+ } -+ } -+ } -+} -+ -+void stm32_tim_freq_func(unsigned long (**timer_freq_cb)(void), -+ enum stm32_tim_cal type) -+{ -+ _init_stm32_tim(); -+ -+ *timer_freq_cb = NULL; -+ -+ switch (type) { -+ case HSI_CAL: -+ if (stm32_tim[HSI_CAL].base.pa) -+ *timer_freq_cb = stm32_tim_hsi_freq; -+ break; -+ -+ case CSI_CAL: -+ if (stm32_tim[CSI_CAL].base.pa) -+ *timer_freq_cb = stm32_tim_csi_freq; -+ break; -+ default: -+ panic(); -+ } -+} -+ -+static TEE_Result init_stm32_tim(void) -+{ -+ _init_stm32_tim(); -+ -+ return TEE_SUCCESS; -+} -+driver_init(init_stm32_tim); -diff --git a/core/drivers/sub.mk b/core/drivers/sub.mk -index 25d69eea8..0d3b81e21 100644 ---- a/core/drivers/sub.mk -+++ b/core/drivers/sub.mk -@@ -26,13 +26,17 @@ srcs-$(CFG_STM32_BSEC) += stm32_bsec.c - srcs-$(CFG_STM32_ETZPC) += stm32_etzpc.c - srcs-$(CFG_STM32_GPIO) += stm32_gpio.c - srcs-$(CFG_STM32_I2C) += stm32_i2c.c -+srcs-$(CFG_STM32_IWDG) += stm32_iwdg.c - srcs-$(CFG_STM32_RNG) += stm32_rng.c -+srcs-$(CFG_STM32_RTC) += stm32_rtc.c -+srcs-$(CFG_STM32_TIM) += stm32_tim.c - srcs-$(CFG_STM32_UART) += stm32_uart.c - srcs-$(CFG_STPMIC1) += stpmic1.c - srcs-$(CFG_BCM_HWRNG) += bcm_hwrng.c - srcs-$(CFG_BCM_SOTP) += bcm_sotp.c - srcs-$(CFG_BCM_GPIO) += bcm_gpio.c - -+subdirs-y += clk - subdirs-y += crypto - subdirs-$(CFG_BNXT_FW) += bnxt - subdirs-$(CFG_SCMI_MSG_DRIVERS) += scmi-msg -diff --git a/core/include/drivers/clk.h b/core/include/drivers/clk.h -new file mode 100644 -index 000000000..8c0c44b34 ---- /dev/null -+++ b/core/include/drivers/clk.h -@@ -0,0 +1,35 @@ -+/* SPDX-License-Identifier: BSD-2-Clause */ -+/* -+ * Copyright (c) 2020, STMicroelectronics - All Rights Reserved -+ */ -+ -+#ifndef CLK_H -+#define CLK_H -+ -+#include -+#include -+ -+#define CLK_UNKNOWN_ID ULONG_MAX -+ -+/* -+ * Minimal generic clock framework where platform is expected implement a -+ * single clock provider and each individual clock identified with a unique -+ * unsigned long identifier. -+ */ -+struct clk_ops { -+ TEE_Result (*enable)(unsigned long id); -+ void (*disable)(unsigned long id); -+ unsigned long (*get_rate)(unsigned long id); -+ unsigned long (*get_parent)(unsigned long id); -+ bool (*is_enabled)(unsigned long id); -+}; -+ -+TEE_Result clk_enable(unsigned long id); -+void clk_disable(unsigned long id); -+unsigned long clk_get_rate(unsigned long id); -+bool clk_is_enabled(unsigned long id); -+unsigned long clk_get_parent(unsigned long id); -+ -+void clk_provider_register(const struct clk_ops *ops); -+ -+#endif /* CLK_H */ -diff --git a/core/include/drivers/gic.h b/core/include/drivers/gic.h -index f9bb28ec1..c8da6d849 100644 ---- a/core/include/drivers/gic.h -+++ b/core/include/drivers/gic.h -@@ -9,17 +9,38 @@ - #include - #include - -+/* Constants to categorize priorities */ -+#define GIC_HIGHEST_SEC_PRIORITY 0x0U -+#define GIC_LOWEST_SEC_PRIORITY 0x7fU -+#define GIC_HIGHEST_NS_PRIORITY 0x80U -+#define GIC_LOWEST_NS_PRIORITY 0xfeU -+/* 0xff would disable all interrupts */ -+ - #define GIC_DIST_REG_SIZE 0x10000 - #define GIC_CPU_REG_SIZE 0x10000 - #define GIC_SGI(x) (x) - #define GIC_PPI(x) ((x) + 16) - #define GIC_SPI(x) ((x) + 32) - -+/* -+ * Save and restore some interrupts configuration during low power sequences. -+ * This is used on platforms using OP-TEE secure monitor. -+ */ -+struct gic_it_pm; -+ -+struct gic_pm { -+ struct gic_it_pm *pm_cfg; -+ size_t count; -+}; -+ - struct gic_data { - vaddr_t gicc_base; - vaddr_t gicd_base; - size_t max_it; - struct itr_chip chip; -+#if defined(CFG_ARM_GIC_PM) -+ struct gic_pm pm; -+#endif - }; - - /* -@@ -31,6 +52,9 @@ void gic_init(struct gic_data *gd, vaddr_t gicc_base, vaddr_t gicd_base); - /* initial base address only */ - void gic_init_base_addr(struct gic_data *gd, vaddr_t gicc_base, - vaddr_t gicd_base); -+/* Setup GIC default configuration */ -+void gic_init_setup(struct gic_data *gd); -+ - /* initial cpu if only, mainly use for secondary cpu setup cpu interface */ - void gic_cpu_init(struct gic_data *gd); - -diff --git a/core/include/drivers/stm32_bsec.h b/core/include/drivers/stm32_bsec.h -index b6afbf8c4..9fe85bc98 100644 ---- a/core/include/drivers/stm32_bsec.h -+++ b/core/include/drivers/stm32_bsec.h -@@ -10,6 +10,12 @@ - #include - #include - -+/* BSEC_DEBUG */ -+#define BSEC_HDPEN BIT(4) -+#define BSEC_SPIDEN BIT(5) -+#define BSEC_SPINDEN BIT(6) -+#define BSEC_DBGSWGEN BIT(10) -+ - /* - * Load OTP from SAFMEM and provide its value - * @value: Output read value -@@ -147,4 +153,14 @@ TEE_Result stm32_bsec_otp_lock(uint32_t service); - */ - bool stm32_bsec_nsec_can_access_otp(uint32_t otp_id); - -+/* -+ * Find and get OTP location from its name. -+ * @name: sub-node name to look up. -+ * @otp_id: pointer to read OTP number or NULL. -+ * @otp_bit_len: pointer to read OTP length in bits or NULL. -+ * Return a TEE_Result compliant status -+ */ -+TEE_Result stm32_bsec_find_otp_in_nvmem_layout(const char *name, -+ uint32_t *otp_id, -+ size_t *otp_bit_len); - #endif /*__STM32_BSEC_H*/ -diff --git a/core/include/drivers/stm32_iwdg.h b/core/include/drivers/stm32_iwdg.h -new file mode 100644 -index 000000000..f06325812 ---- /dev/null -+++ b/core/include/drivers/stm32_iwdg.h -@@ -0,0 +1,17 @@ -+/* SPDX-License-Identifier: BSD-3-Clause */ -+/* -+ * Copyright (c) 2018-2019, STMicroelectronics - All Rights Reserved -+ */ -+ -+#ifndef __STM32_IWDG_H__ -+#define __STM32_IWDG_H__ -+ -+#include -+ -+#define IWDG_HW_ENABLED BIT(0) -+#define IWDG_DISABLE_ON_STOP BIT(1) -+#define IWDG_DISABLE_ON_STANDBY BIT(2) -+ -+void stm32_iwdg_refresh(unsigned int instance); -+ -+#endif /*__STM32_IWDG_H__*/ -diff --git a/core/include/drivers/stm32_rtc.h b/core/include/drivers/stm32_rtc.h -new file mode 100644 -index 000000000..b0f4ef858 ---- /dev/null -+++ b/core/include/drivers/stm32_rtc.h -@@ -0,0 +1,60 @@ -+/* SPDX-License-Identifier: BSD-3-Clause */ -+/* -+ * Copyright (c) 2017-2018, STMicroelectronics - All Rights Reserved -+ * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved. -+ */ -+ -+#ifndef __PLAT_RTC_H__ -+#define __PLAT_RTC_H__ -+ -+#include -+ -+struct stm32_rtc_calendar { -+ uint32_t ssr; -+ uint32_t tr; -+ uint32_t dr; -+}; -+ -+enum months { -+ JANUARY = 1, -+ FEBRUARY, -+ MARCH, -+ APRIL, -+ MAY, -+ JUNE, -+ JULY, -+ AUGUST, -+ SEPTEMBER, -+ OCTOBER, -+ NOVEMBER, -+ DECEMBER, -+ NB_MONTHS = 12 -+}; -+ -+struct stm32_rtc_time { -+ uint32_t hour; -+ uint32_t min; -+ uint32_t sec; -+ uint32_t wday; -+ uint32_t day; -+ enum months month; -+ uint32_t year; -+}; -+ -+/* Get calendar formated time from RTC device */ -+void stm32_rtc_get_calendar(struct stm32_rtc_calendar *calendar); -+ -+/* Return time diff in milliseconds between current and reference time */ -+unsigned long long stm32_rtc_diff_calendar(struct stm32_rtc_calendar *cur, -+ struct stm32_rtc_calendar *ref); -+ -+/* Enable tamper and secure timestamp access in RTC */ -+void stm32_rtc_set_tamper_timestamp(void); -+ -+/* Retrun true if and only if RTC timestamp is enable */ -+bool stm32_rtc_is_timestamp_enable(void); -+ -+/* Get RTC timestamp for current time */ -+void stm32_rtc_get_timestamp(struct stm32_rtc_time *tamp_ts); -+ -+#endif /* __PLAT_RTC_H__ */ -diff --git a/core/include/drivers/stm32_tim.h b/core/include/drivers/stm32_tim.h -new file mode 100644 -index 000000000..2373561a3 ---- /dev/null -+++ b/core/include/drivers/stm32_tim.h -@@ -0,0 +1,25 @@ -+/* SPDX-License-Identifier: BSD-3-Clause */ -+/* -+ * Copyright (c) 2018-2019, STMicroelectronics -+ */ -+ -+#ifndef STM32_TIM_H -+#define STM32_TIM_H -+ -+enum stm32_tim_cal { -+ HSI_CAL = 0, -+ CSI_CAL -+}; -+ -+unsigned long stm32_tim_hsi_freq(void); -+unsigned long stm32_tim_csi_freq(void); -+ -+/* -+ * Get the timer frequence callback function for a target clock calibration -+ * @timer_freq_cb - Output callback function -+ * @type - Target clock calibration ID -+ */ -+void stm32_tim_freq_func(unsigned long (**timer_freq_cb)(void), -+ enum stm32_tim_cal type); -+ -+#endif /* STM32_TIM_H */ -diff --git a/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_rcc.h b/core/include/drivers/stm32mp1_rcc.h -similarity index 95% -rename from core/arch/arm/plat-stm32mp1/drivers/stm32mp1_rcc.h -rename to core/include/drivers/stm32mp1_rcc.h -index 4dedbad27..6c92257b6 100644 ---- a/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_rcc.h -+++ b/core/include/drivers/stm32mp1_rcc.h -@@ -1,13 +1,11 @@ - /* SPDX-License-Identifier: BSD-3-Clause */ - /* -- * Copyright (c) 2017-2018, STMicroelectronics -+ * Copyright (c) 2017-2020, STMicroelectronics - */ - - #ifndef __STM32MP1_RCC_H__ - #define __STM32MP1_RCC_H__ - --#include --#include - #include - - #define RCC_TZCR 0x00 -@@ -392,7 +390,8 @@ - #define RCC_HSICFGR_HSITRIM_SHIFT 8 - #define RCC_HSICFGR_HSITRIM_MASK GENMASK_32(14, 8) - #define RCC_HSICFGR_HSICAL_SHIFT 16 --#define RCC_HSICFGR_HSICAL_MASK GENMASK_32(27, 16) -+#define RCC_HSICFGR_HSICAL_MASK GENMASK_32(24, 16) -+#define RCC_HSICFGR_HSICAL_TEMP_MASK GENMASK_32(27, 25) - - /* Fields of RCC_CSICFGR register */ - #define RCC_CSICFGR_CSITRIM_SHIFT 8 -@@ -455,6 +454,9 @@ - #define RCC_MP_SREQCLRR_STPREQ_P0 BIT(0) - #define RCC_MP_SREQCLRR_STPREQ_P1 BIT(1) - -+/* Values of RCC_PWRLPDLYCR register */ -+#define RCC_PWRLPDLYCR_PWRLP_DLY_MASK GENMASK_32(21, 0) -+ - /* Global Control Register */ - #define RCC_MP_GCR_BOOT_MCU BIT(0) - -@@ -511,6 +513,9 @@ - #define RCC_AHB5RSTSETR_RNG1RST BIT(6) - #define RCC_AHB5RSTSETR_AXIMCRST BIT(16) - -+/* RCC_MP_AHB6RST(SET|CLR)R bit fields */ -+#define RCC_AHB6RSTSETR_GPURST BIT(5) -+ - /* RCC_MP_AHB5EN(SET|CLR)R bit fields */ - #define RCC_MP_AHB5ENSETR_GPIOZEN_POS 0 - #define RCC_MP_AHB5ENSETR_CRYP1EN_POS 4 -@@ -534,26 +539,43 @@ - #define RCC_MP_AHB5LPENSETR_BKPSRAMLPEN BIT(8) - - /* RCC_MP_TZAHB6EN(SET|CLR)R bit fields */ --#define RCC_MP_TZAHB6ENSETR_MDMA_POS 0 --#define RCC_MP_TZAHB6ENSETR_MDMA BIT(RCC_MP_TZAHB6ENSETR_MDMA_POS) -+#define RCC_MP_TZAHB6ENSETR_MDMA_POS 0 -+#define RCC_MP_TZAHB6ENSETR_MDMA \ -+ BIT(RCC_MP_TZAHB6ENSETR_MDMA_POS) - - /* RCC_MP_IWDGFZ(SET|CLR)R bit fields */ - #define RCC_MP_IWDGFZSETR_IWDG1 BIT(0) - #define RCC_MP_IWDGFZSETR_IWDG2 BIT(1) - - #define DT_RCC_CLK_COMPAT "st,stm32mp1-rcc" -+#define DT_RCC_SEC_CLK_COMPAT "st,stm32mp1-rcc-secure" - - #ifndef __ASSEMBLER__ -+#include -+#include -+#include -+ - vaddr_t stm32_rcc_base(void); - - static inline bool stm32_rcc_is_secure(void) - { -- return io_read32(stm32_rcc_base() + RCC_TZCR) & RCC_TZCR_TZEN; -+ static int state = -1; -+ -+ if (state < 0) -+ state = io_read32(stm32_rcc_base() + RCC_TZCR) & RCC_TZCR_TZEN; -+ -+ return state; - } - - static inline bool stm32_rcc_is_mckprot(void) - { -- return io_read32(stm32_rcc_base() + RCC_TZCR) & RCC_TZCR_MCKPROT; -+ const uint32_t mask = RCC_TZCR_TZEN | RCC_TZCR_MCKPROT; -+ static int state = -1; -+ -+ if (state < 0) -+ state = (io_read32(stm32_rcc_base() + RCC_TZCR) & mask) == mask; -+ -+ return state; - } - #endif /*__ASSEMBLER__*/ - -diff --git a/core/include/dt-bindings/clock/stm32mp1-clksrc.h b/core/include/dt-bindings/clock/stm32mp1-clksrc.h -new file mode 100644 -index 000000000..de7d1604c ---- /dev/null -+++ b/core/include/dt-bindings/clock/stm32mp1-clksrc.h -@@ -0,0 +1,284 @@ -+/* -+ * Copyright (C) 2017, STMicroelectronics - All Rights Reserved -+ * -+ * SPDX-License-Identifier: GPL-2.0+ BSD-3-Clause -+ */ -+ -+#ifndef _DT_BINDINGS_CLOCK_STM32MP1_CLKSRC_H_ -+#define _DT_BINDINGS_CLOCK_STM32MP1_CLKSRC_H_ -+ -+/* PLL output is enable when x=1, with x=p,q or r */ -+#define PQR(p, q, r) (((p) & 1) | (((q) & 1) << 1) | (((r) & 1) << 2)) -+ -+/* st,clksrc: mandatory clock source */ -+ -+#define CLK_MPU_HSI 0x00000200 -+#define CLK_MPU_HSE 0x00000201 -+#define CLK_MPU_PLL1P 0x00000202 -+#define CLK_MPU_PLL1P_DIV 0x00000203 -+ -+#define CLK_AXI_HSI 0x00000240 -+#define CLK_AXI_HSE 0x00000241 -+#define CLK_AXI_PLL2P 0x00000242 -+ -+#define CLK_MCU_HSI 0x00000480 -+#define CLK_MCU_HSE 0x00000481 -+#define CLK_MCU_CSI 0x00000482 -+#define CLK_MCU_PLL3P 0x00000483 -+ -+#define CLK_PLL12_HSI 0x00000280 -+#define CLK_PLL12_HSE 0x00000281 -+ -+#define CLK_PLL3_HSI 0x00008200 -+#define CLK_PLL3_HSE 0x00008201 -+#define CLK_PLL3_CSI 0x00008202 -+ -+#define CLK_PLL4_HSI 0x00008240 -+#define CLK_PLL4_HSE 0x00008241 -+#define CLK_PLL4_CSI 0x00008242 -+#define CLK_PLL4_I2SCKIN 0x00008243 -+ -+#define CLK_RTC_DISABLED 0x00001400 -+#define CLK_RTC_LSE 0x00001401 -+#define CLK_RTC_LSI 0x00001402 -+#define CLK_RTC_HSE 0x00001403 -+ -+#define CLK_MCO1_HSI 0x00008000 -+#define CLK_MCO1_HSE 0x00008001 -+#define CLK_MCO1_CSI 0x00008002 -+#define CLK_MCO1_LSI 0x00008003 -+#define CLK_MCO1_LSE 0x00008004 -+#define CLK_MCO1_DISABLED 0x0000800F -+ -+#define CLK_MCO2_MPU 0x00008040 -+#define CLK_MCO2_AXI 0x00008041 -+#define CLK_MCO2_MCU 0x00008042 -+#define CLK_MCO2_PLL4P 0x00008043 -+#define CLK_MCO2_HSE 0x00008044 -+#define CLK_MCO2_HSI 0x00008045 -+#define CLK_MCO2_DISABLED 0x0000804F -+ -+/* st,pkcs: peripheral kernel clock source */ -+ -+#define CLK_I2C12_PCLK1 0x00008C00 -+#define CLK_I2C12_PLL4R 0x00008C01 -+#define CLK_I2C12_HSI 0x00008C02 -+#define CLK_I2C12_CSI 0x00008C03 -+#define CLK_I2C12_DISABLED 0x00008C07 -+ -+#define CLK_I2C35_PCLK1 0x00008C40 -+#define CLK_I2C35_PLL4R 0x00008C41 -+#define CLK_I2C35_HSI 0x00008C42 -+#define CLK_I2C35_CSI 0x00008C43 -+#define CLK_I2C35_DISABLED 0x00008C47 -+ -+#define CLK_I2C46_PCLK5 0x00000C00 -+#define CLK_I2C46_PLL3Q 0x00000C01 -+#define CLK_I2C46_HSI 0x00000C02 -+#define CLK_I2C46_CSI 0x00000C03 -+#define CLK_I2C46_DISABLED 0x00000C07 -+ -+#define CLK_SAI1_PLL4Q 0x00008C80 -+#define CLK_SAI1_PLL3Q 0x00008C81 -+#define CLK_SAI1_I2SCKIN 0x00008C82 -+#define CLK_SAI1_CKPER 0x00008C83 -+#define CLK_SAI1_PLL3R 0x00008C84 -+#define CLK_SAI1_DISABLED 0x00008C87 -+ -+#define CLK_SAI2_PLL4Q 0x00008CC0 -+#define CLK_SAI2_PLL3Q 0x00008CC1 -+#define CLK_SAI2_I2SCKIN 0x00008CC2 -+#define CLK_SAI2_CKPER 0x00008CC3 -+#define CLK_SAI2_SPDIF 0x00008CC4 -+#define CLK_SAI2_PLL3R 0x00008CC5 -+#define CLK_SAI2_DISABLED 0x00008CC7 -+ -+#define CLK_SAI3_PLL4Q 0x00008D00 -+#define CLK_SAI3_PLL3Q 0x00008D01 -+#define CLK_SAI3_I2SCKIN 0x00008D02 -+#define CLK_SAI3_CKPER 0x00008D03 -+#define CLK_SAI3_PLL3R 0x00008D04 -+#define CLK_SAI3_DISABLED 0x00008D07 -+ -+#define CLK_SAI4_PLL4Q 0x00008D40 -+#define CLK_SAI4_PLL3Q 0x00008D41 -+#define CLK_SAI4_I2SCKIN 0x00008D42 -+#define CLK_SAI4_CKPER 0x00008D43 -+#define CLK_SAI4_PLL3R 0x00008D44 -+#define CLK_SAI4_DISABLED 0x00008D47 -+ -+#define CLK_SPI2S1_PLL4P 0x00008D80 -+#define CLK_SPI2S1_PLL3Q 0x00008D81 -+#define CLK_SPI2S1_I2SCKIN 0x00008D82 -+#define CLK_SPI2S1_CKPER 0x00008D83 -+#define CLK_SPI2S1_PLL3R 0x00008D84 -+#define CLK_SPI2S1_DISABLED 0x00008D87 -+ -+#define CLK_SPI2S23_PLL4P 0x00008DC0 -+#define CLK_SPI2S23_PLL3Q 0x00008DC1 -+#define CLK_SPI2S23_I2SCKIN 0x00008DC2 -+#define CLK_SPI2S23_CKPER 0x00008DC3 -+#define CLK_SPI2S23_PLL3R 0x00008DC4 -+#define CLK_SPI2S23_DISABLED 0x00008DC7 -+ -+#define CLK_SPI45_PCLK2 0x00008E00 -+#define CLK_SPI45_PLL4Q 0x00008E01 -+#define CLK_SPI45_HSI 0x00008E02 -+#define CLK_SPI45_CSI 0x00008E03 -+#define CLK_SPI45_HSE 0x00008E04 -+#define CLK_SPI45_DISABLED 0x00008E07 -+ -+#define CLK_SPI6_PCLK5 0x00000C40 -+#define CLK_SPI6_PLL4Q 0x00000C41 -+#define CLK_SPI6_HSI 0x00000C42 -+#define CLK_SPI6_CSI 0x00000C43 -+#define CLK_SPI6_HSE 0x00000C44 -+#define CLK_SPI6_PLL3Q 0x00000C45 -+#define CLK_SPI6_DISABLED 0x00000C47 -+ -+#define CLK_UART6_PCLK2 0x00008E40 -+#define CLK_UART6_PLL4Q 0x00008E41 -+#define CLK_UART6_HSI 0x00008E42 -+#define CLK_UART6_CSI 0x00008E43 -+#define CLK_UART6_HSE 0x00008E44 -+#define CLK_UART6_DISABLED 0x00008E47 -+ -+#define CLK_UART24_PCLK1 0x00008E80 -+#define CLK_UART24_PLL4Q 0x00008E81 -+#define CLK_UART24_HSI 0x00008E82 -+#define CLK_UART24_CSI 0x00008E83 -+#define CLK_UART24_HSE 0x00008E84 -+#define CLK_UART24_DISABLED 0x00008E87 -+ -+#define CLK_UART35_PCLK1 0x00008EC0 -+#define CLK_UART35_PLL4Q 0x00008EC1 -+#define CLK_UART35_HSI 0x00008EC2 -+#define CLK_UART35_CSI 0x00008EC3 -+#define CLK_UART35_HSE 0x00008EC4 -+#define CLK_UART35_DISABLED 0x00008EC7 -+ -+#define CLK_UART78_PCLK1 0x00008F00 -+#define CLK_UART78_PLL4Q 0x00008F01 -+#define CLK_UART78_HSI 0x00008F02 -+#define CLK_UART78_CSI 0x00008F03 -+#define CLK_UART78_HSE 0x00008F04 -+#define CLK_UART78_DISABLED 0x00008F07 -+ -+#define CLK_UART1_PCLK5 0x00000C80 -+#define CLK_UART1_PLL3Q 0x00000C81 -+#define CLK_UART1_HSI 0x00000C82 -+#define CLK_UART1_CSI 0x00000C83 -+#define CLK_UART1_PLL4Q 0x00000C84 -+#define CLK_UART1_HSE 0x00000C85 -+#define CLK_UART1_DISABLED 0x00000C87 -+ -+#define CLK_SDMMC12_HCLK6 0x00008F40 -+#define CLK_SDMMC12_PLL3R 0x00008F41 -+#define CLK_SDMMC12_PLL4P 0x00008F42 -+#define CLK_SDMMC12_HSI 0x00008F43 -+#define CLK_SDMMC12_DISABLED 0x00008F47 -+ -+#define CLK_SDMMC3_HCLK2 0x00008F80 -+#define CLK_SDMMC3_PLL3R 0x00008F81 -+#define CLK_SDMMC3_PLL4P 0x00008F82 -+#define CLK_SDMMC3_HSI 0x00008F83 -+#define CLK_SDMMC3_DISABLED 0x00008F87 -+ -+#define CLK_ETH_PLL4P 0x00008FC0 -+#define CLK_ETH_PLL3Q 0x00008FC1 -+#define CLK_ETH_DISABLED 0x00008FC3 -+ -+#define CLK_QSPI_ACLK 0x00009000 -+#define CLK_QSPI_PLL3R 0x00009001 -+#define CLK_QSPI_PLL4P 0x00009002 -+#define CLK_QSPI_CKPER 0x00009003 -+ -+#define CLK_FMC_ACLK 0x00009040 -+#define CLK_FMC_PLL3R 0x00009041 -+#define CLK_FMC_PLL4P 0x00009042 -+#define CLK_FMC_CKPER 0x00009043 -+ -+#define CLK_FDCAN_HSE 0x000090C0 -+#define CLK_FDCAN_PLL3Q 0x000090C1 -+#define CLK_FDCAN_PLL4Q 0x000090C2 -+#define CLK_FDCAN_PLL4R 0x000090C3 -+ -+#define CLK_SPDIF_PLL4P 0x00009140 -+#define CLK_SPDIF_PLL3Q 0x00009141 -+#define CLK_SPDIF_HSI 0x00009142 -+#define CLK_SPDIF_DISABLED 0x00009143 -+ -+#define CLK_CEC_LSE 0x00009180 -+#define CLK_CEC_LSI 0x00009181 -+#define CLK_CEC_CSI_DIV122 0x00009182 -+#define CLK_CEC_DISABLED 0x00009183 -+ -+#define CLK_USBPHY_HSE 0x000091C0 -+#define CLK_USBPHY_PLL4R 0x000091C1 -+#define CLK_USBPHY_HSE_DIV2 0x000091C2 -+#define CLK_USBPHY_DISABLED 0x000091C3 -+ -+#define CLK_USBO_PLL4R 0x800091C0 -+#define CLK_USBO_USBPHY 0x800091C1 -+ -+#define CLK_RNG1_CSI 0x00000CC0 -+#define CLK_RNG1_PLL4R 0x00000CC1 -+#define CLK_RNG1_LSE 0x00000CC2 -+#define CLK_RNG1_LSI 0x00000CC3 -+ -+#define CLK_RNG2_CSI 0x00009200 -+#define CLK_RNG2_PLL4R 0x00009201 -+#define CLK_RNG2_LSE 0x00009202 -+#define CLK_RNG2_LSI 0x00009203 -+ -+#define CLK_CKPER_HSI 0x00000D00 -+#define CLK_CKPER_CSI 0x00000D01 -+#define CLK_CKPER_HSE 0x00000D02 -+#define CLK_CKPER_DISABLED 0x00000D03 -+ -+#define CLK_STGEN_HSI 0x00000D40 -+#define CLK_STGEN_HSE 0x00000D41 -+#define CLK_STGEN_DISABLED 0x00000D43 -+ -+#define CLK_DSI_DSIPLL 0x00009240 -+#define CLK_DSI_PLL4P 0x00009241 -+ -+#define CLK_ADC_PLL4R 0x00009280 -+#define CLK_ADC_CKPER 0x00009281 -+#define CLK_ADC_PLL3Q 0x00009282 -+#define CLK_ADC_DISABLED 0x00009283 -+ -+#define CLK_LPTIM45_PCLK3 0x000092C0 -+#define CLK_LPTIM45_PLL4P 0x000092C1 -+#define CLK_LPTIM45_PLL3Q 0x000092C2 -+#define CLK_LPTIM45_LSE 0x000092C3 -+#define CLK_LPTIM45_LSI 0x000092C4 -+#define CLK_LPTIM45_CKPER 0x000092C5 -+#define CLK_LPTIM45_DISABLED 0x000092C7 -+ -+#define CLK_LPTIM23_PCLK3 0x00009300 -+#define CLK_LPTIM23_PLL4Q 0x00009301 -+#define CLK_LPTIM23_CKPER 0x00009302 -+#define CLK_LPTIM23_LSE 0x00009303 -+#define CLK_LPTIM23_LSI 0x00009304 -+#define CLK_LPTIM23_DISABLED 0x00009307 -+ -+#define CLK_LPTIM1_PCLK1 0x00009340 -+#define CLK_LPTIM1_PLL4P 0x00009341 -+#define CLK_LPTIM1_PLL3Q 0x00009342 -+#define CLK_LPTIM1_LSE 0x00009343 -+#define CLK_LPTIM1_LSI 0x00009344 -+#define CLK_LPTIM1_CKPER 0x00009345 -+#define CLK_LPTIM1_DISABLED 0x00009347 -+ -+/* define for st,pll /csg */ -+#define SSCG_MODE_CENTER_SPREAD 0 -+#define SSCG_MODE_DOWN_SPREAD 1 -+ -+/* define for st,drive */ -+#define LSEDRV_LOWEST 0 -+#define LSEDRV_MEDIUM_LOW 1 -+#define LSEDRV_MEDIUM_HIGH 2 -+#define LSEDRV_HIGHEST 3 -+ -+#endif -diff --git a/core/include/dt-bindings/power/stm32mp1-power.h b/core/include/dt-bindings/power/stm32mp1-power.h -new file mode 100644 -index 000000000..bfb7f785e ---- /dev/null -+++ b/core/include/dt-bindings/power/stm32mp1-power.h -@@ -0,0 +1,19 @@ -+/* SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause */ -+/* -+ * Copyright (C) STMicroelectronics 2018 - All Rights Reserved -+ * Author: Yann Gautier for STMicroelectronics. -+ */ -+ -+#ifndef DT_BINDINGS_STM32MP1_POWER_H -+#define DT_BINDINGS_STM32MP1_POWER_H -+ -+#define STM32_PM_CSLEEP_RUN 0 -+#define STM32_PM_CSTOP_ALLOW_STOP 1 -+#define STM32_PM_CSTOP_ALLOW_LP_STOP 2 -+#define STM32_PM_CSTOP_ALLOW_LPLV_STOP 3 -+#define STM32_PM_CSTOP_ALLOW_STANDBY_DDR_SR 4 -+#define STM32_PM_CSTOP_ALLOW_STANDBY_DDR_OFF 5 -+#define STM32_PM_SHUTDOWN 6 -+#define STM32_PM_MAX_SOC_MODE 7 -+ -+#endif /* DT_BINDINGS_STM32MP1_POWER_H */ -diff --git a/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_etzpc.h b/core/include/dt-bindings/soc/st,stm32-etzpc.h -similarity index 83% -rename from core/arch/arm/plat-stm32mp1/drivers/stm32mp1_etzpc.h -rename to core/include/dt-bindings/soc/st,stm32-etzpc.h -index 338a39283..6678b8e66 100644 ---- a/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_etzpc.h -+++ b/core/include/dt-bindings/soc/st,stm32-etzpc.h -@@ -1,26 +1,35 @@ --/* SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause */ - /* -- * Copyright (C) 2018-2019, STMicroelectronics -+ * Copyright (C) 2017, STMicroelectronics - All Rights Reserved -+ * -+ * SPDX-License-Identifier: GPL-2.0+ BSD-3-Clause - */ - --#ifndef __STM32MP1_ETZPC_H --#define __STM32MP1_ETZPC_H -+#ifndef _DT_BINDINGS_STM32_ETZPC_H -+#define _DT_BINDINGS_STM32_ETZPC_H - --/* Define DECPROT IDs for stm32mp1 familly */ -+/* define DECPROT modes */ -+#define DECPROT_S_RW 0x0 -+#define DECPROT_NS_R_S_W 0x1 -+#define DECPROT_MCU_ISOLATION 0x2 -+#define DECPROT_NS_RW 0x3 -+ -+/* define DECPROT lock */ -+#define DECPROT_UNLOCK 0x0 -+#define DECPROT_LOCK 0x1 -+ -+/* define ETZPC ID */ - #define STM32MP1_ETZPC_STGENC_ID 0 - #define STM32MP1_ETZPC_BKPSRAM_ID 1 - #define STM32MP1_ETZPC_IWDG1_ID 2 - #define STM32MP1_ETZPC_USART1_ID 3 - #define STM32MP1_ETZPC_SPI6_ID 4 - #define STM32MP1_ETZPC_I2C4_ID 5 --#define STM32MP1_ETZPC_GPIOZ_ID 6 - #define STM32MP1_ETZPC_RNG1_ID 7 - #define STM32MP1_ETZPC_HASH1_ID 8 - #define STM32MP1_ETZPC_CRYP1_ID 9 - #define STM32MP1_ETZPC_DDRCTRL_ID 10 - #define STM32MP1_ETZPC_DDRPHYC_ID 11 - #define STM32MP1_ETZPC_I2C6_ID 12 --/* 13-15 Reserved */ - #define STM32MP1_ETZPC_TIM2_ID 16 - #define STM32MP1_ETZPC_TIM3_ID 17 - #define STM32MP1_ETZPC_TIM4_ID 18 -@@ -47,12 +56,9 @@ - #define STM32MP1_ETZPC_DAC_ID 39 - #define STM32MP1_ETZPC_UART7_ID 40 - #define STM32MP1_ETZPC_UART8_ID 41 --/* 42-43 Reserved */ - #define STM32MP1_ETZPC_MDIOS_ID 44 --/* 45-47 Reserved */ - #define STM32MP1_ETZPC_TIM1_ID 48 - #define STM32MP1_ETZPC_TIM8_ID 49 --/* 50 Reserved */ - #define STM32MP1_ETZPC_USART6_ID 51 - #define STM32MP1_ETZPC_SPI1_ID 52 - #define STM32MP1_ETZPC_SPI4_ID 53 -@@ -65,7 +71,6 @@ - #define STM32MP1_ETZPC_SAI3_ID 60 - #define STM32MP1_ETZPC_DFSDM_ID 61 - #define STM32MP1_ETZPC_TT_FDCAN_ID 62 --/* 63 Reserved */ - #define STM32MP1_ETZPC_LPTIM2_ID 64 - #define STM32MP1_ETZPC_LPTIM3_ID 65 - #define STM32MP1_ETZPC_LPTIM4_ID 66 -@@ -78,7 +83,6 @@ - #define STM32MP1_ETZPC_HASH2_ID 73 - #define STM32MP1_ETZPC_RNG2_ID 74 - #define STM32MP1_ETZPC_CRYP2_ID 75 --/* 76-79 Reserved */ - #define STM32MP1_ETZPC_SRAM1_ID 80 - #define STM32MP1_ETZPC_SRAM2_ID 81 - #define STM32MP1_ETZPC_SRAM3_ID 82 -@@ -94,7 +98,10 @@ - #define STM32MP1_ETZPC_QSPI_ID 92 - #define STM32MP1_ETZPC_DLYBQ_ID 93 - #define STM32MP1_ETZPC_ETH_ID 94 --/* 95 Reserved */ -+ - #define STM32MP1_ETZPC_MAX_ID 96 - --#endif /*__STM32MP1_ETZPC_H*/ -+#define DECPROT(id, mode, lock) (((id) << 16) | ((mode) << 8) | (lock)) -+ -+#endif /* _DT_BINDINGS_STM32_ETZPC_H */ -+ -diff --git a/core/include/kernel/interrupt.h b/core/include/kernel/interrupt.h -index 849e987f9..796eded29 100644 ---- a/core/include/kernel/interrupt.h -+++ b/core/include/kernel/interrupt.h -@@ -25,6 +25,11 @@ struct itr_ops { - uint8_t cpu_mask); - void (*set_affinity)(struct itr_chip *chip, size_t it, - uint8_t cpu_mask); -+#if !defined(CFG_ARM_GICV3) -+ uint8_t (*set_pmr)(struct itr_chip *chip, uint8_t mask); -+ uint8_t (*set_ipriority)(struct itr_chip *chip, size_t it, -+ uint8_t mask); -+#endif - }; - - enum itr_return { -@@ -66,4 +71,14 @@ void itr_set_affinity(size_t it, uint8_t cpu_mask); - */ - void itr_core_handler(void); - -+/* -+ * Set the Priority Mask Regarding and return its previous value -+ */ -+uint8_t itr_set_pmr(uint8_t mask); -+ -+/* -+ * Set the targe tinterrupt priority mask and return its previous value -+ */ -+uint8_t itr_set_ipriority(size_t it, uint8_t mask); -+ - #endif /*__KERNEL_INTERRUPT_H*/ -diff --git a/core/include/kernel/panic.h b/core/include/kernel/panic.h -index b9a56dc2c..0f0d0794e 100644 ---- a/core/include/kernel/panic.h -+++ b/core/include/kernel/panic.h -@@ -8,6 +8,12 @@ - - #include - -+/* -+ * Platform can define a panic sequence to trap cpu/reset core or system -+ * after eventual debug trace. -+ */ -+void plat_panic(void); -+ - /* debug disabled => __FILE__, ... and panic message are not built. */ - #if defined(CFG_TEE_CORE_DEBUG) - #define __panic(str) __do_panic(__FILE__, __LINE__, __func__, str) -diff --git a/core/kernel/interrupt.c b/core/kernel/interrupt.c -index 961ce6be3..376012556 100644 ---- a/core/kernel/interrupt.c -+++ b/core/kernel/interrupt.c -@@ -88,3 +88,13 @@ void __weak __noreturn itr_core_handler(void) - { - panic("Secure interrupt handler not defined"); - } -+ -+uint8_t itr_set_pmr(uint8_t mask) -+{ -+ return itr_chip->ops->set_pmr(itr_chip, mask); -+} -+ -+uint8_t itr_set_ipriority(size_t it, uint8_t mask) -+{ -+ return itr_chip->ops->set_ipriority(itr_chip, it, mask); -+} -diff --git a/core/kernel/panic.c b/core/kernel/panic.c -index f30c6ffe7..023f4be5d 100644 ---- a/core/kernel/panic.c -+++ b/core/kernel/panic.c -@@ -7,8 +7,21 @@ - #include - #include - #include -+#include - #include - -+static void __noreturn stall_cpu(void) -+{ -+ while (true) -+ ; -+} -+ -+void __weak __noreturn plat_panic(void) -+{ -+ /* abort current execution */ -+ stall_cpu(); -+} -+ - void __do_panic(const char *file __maybe_unused, - const int line __maybe_unused, - const char *func __maybe_unused, -@@ -17,8 +30,6 @@ void __do_panic(const char *file __maybe_unused, - /* disable prehemption */ - (void)thread_mask_exceptions(THREAD_EXCP_ALL); - -- /* TODO: notify other cores */ -- - /* trace: Panic ['panic-string-message' ]at FILE:LINE []" */ - if (!file && !func && !msg) - EMSG_RAW("Panic"); -@@ -29,7 +40,9 @@ void __do_panic(const char *file __maybe_unused, - func ? "<" : "", func ? func : "", func ? ">" : ""); - - print_kernel_stack(); -- /* abort current execution */ -- while (1) -- ; -+ -+ plat_panic(); -+ -+ EMSG("platform failed to abord execution"); -+ stall_cpu(); - } -diff --git a/core/mm/vm.c b/core/mm/vm.c -index 27ffa18af..95080a700 100644 ---- a/core/mm/vm.c -+++ b/core/mm/vm.c -@@ -162,14 +162,26 @@ static TEE_Result alloc_pgt(struct user_mode_ctx *uctx) - return TEE_SUCCESS; - } - --static void maybe_free_pgt(struct user_mode_ctx *uctx, struct vm_region *r) -+static void rem_um_region(struct user_mode_ctx *uctx, struct vm_region *r) - { -- struct thread_specific_data *tsd = NULL; -+ struct thread_specific_data *tsd = thread_get_tsd(); - struct pgt_cache *pgt_cache = NULL; - vaddr_t begin = ROUNDDOWN(r->va, CORE_MMU_PGDIR_SIZE); - vaddr_t last = ROUNDUP(r->va + r->size, CORE_MMU_PGDIR_SIZE); - struct vm_region *r2 = NULL; - -+ if (uctx->ts_ctx == tsd->ctx) -+ pgt_cache = &tsd->pgt_cache; -+ -+ if (mobj_is_paged(r->mobj)) { -+ tee_pager_rem_um_region(uctx, r->va, r->size); -+ } else { -+ pgt_clear_ctx_range(pgt_cache, uctx->ts_ctx, r->va, -+ r->va + r->size); -+ tlbi_mva_range_asid(r->va, r->size, SMALL_PAGE_SIZE, -+ uctx->vm_info.asid); -+ } -+ - r2 = TAILQ_NEXT(r, link); - if (r2) - last = MIN(last, ROUNDDOWN(r2->va, CORE_MMU_PGDIR_SIZE)); -@@ -183,10 +195,6 @@ static void maybe_free_pgt(struct user_mode_ctx *uctx, struct vm_region *r) - if (begin >= last) - return; - -- tsd = thread_get_tsd(); -- if (uctx->ts_ctx == tsd->ctx) -- pgt_cache = &tsd->pgt_cache; -- - pgt_flush_ctx_range(pgt_cache, uctx->ts_ctx, r->va, r->va + r->size); - } - -@@ -539,9 +547,7 @@ TEE_Result vm_remap(struct user_mode_ctx *uctx, vaddr_t *new_va, vaddr_t old_va, - if (r->va + r->size > old_va + len) - break; - r_next = TAILQ_NEXT(r, link); -- if (fobj) -- tee_pager_rem_um_region(uctx, r->va, r->size); -- maybe_free_pgt(uctx, r); -+ rem_um_region(uctx, r); - TAILQ_REMOVE(&uctx->vm_info.regions, r, link); - TAILQ_INSERT_TAIL(®s, r, link); - } -@@ -774,21 +780,13 @@ TEE_Result vm_unmap(struct user_mode_ctx *uctx, vaddr_t va, size_t len) - while (true) { - r_next = TAILQ_NEXT(r, link); - unmap_end_va = r->va + r->size; -- if (mobj_is_paged(r->mobj)) -- tee_pager_rem_um_region(uctx, r->va, r->size); -- maybe_free_pgt(uctx, r); -+ rem_um_region(uctx, r); - umap_remove_region(&uctx->vm_info, r); - if (!r_next || unmap_end_va == end_va) - break; - r = r_next; - } - -- /* -- * Synchronize change to translation tables. Even though the pager -- * case unmaps immediately we may still free a translation table. -- */ -- vm_set_ctx(uctx->ts_ctx); -- - return TEE_SUCCESS; - } - -@@ -843,9 +841,7 @@ void vm_clean_param(struct user_mode_ctx *uctx) - - TAILQ_FOREACH_SAFE(r, &uctx->vm_info.regions, link, next_r) { - if (r->flags & VM_FLAG_EPHEMERAL) { -- if (mobj_is_paged(r->mobj)) -- tee_pager_rem_um_region(uctx, r->va, r->size); -- maybe_free_pgt(uctx, r); -+ rem_um_region(uctx, r); - umap_remove_region(&uctx->vm_info, r); - } - } -@@ -1055,9 +1051,7 @@ void vm_rem_rwmem(struct user_mode_ctx *uctx, struct mobj *mobj, vaddr_t va) - - TAILQ_FOREACH(r, &uctx->vm_info.regions, link) { - if (r->mobj == mobj && r->va == va) { -- if (mobj_is_paged(r->mobj)) -- tee_pager_rem_um_region(uctx, r->va, r->size); -- maybe_free_pgt(uctx, r); -+ rem_um_region(uctx, r); - umap_remove_region(&uctx->vm_info, r); - return; - } -diff --git a/core/sub.mk b/core/sub.mk -index 5a17cd923..faaf21c74 100644 ---- a/core/sub.mk -+++ b/core/sub.mk -@@ -20,13 +20,16 @@ recipe-ldelf = $(PYTHON3) scripts/gen_ldelf_hex.py --input $(out-dir)/ldelf/ldel - endif - - ifeq ($(CFG_WITH_USER_TA)-$(CFG_EARLY_TA),y-y) -+ifeq ($(CFG_EARLY_TA_COMPRESS),y) -+early-ta-compress = --compress -+endif - define process_early_ta - early-ta-$1-uuid := $(firstword $(subst ., ,$(notdir $1))) - gensrcs-y += early-ta-$1 - produce-early-ta-$1 = early_ta_$$(early-ta-$1-uuid).c - depends-early-ta-$1 = $1 scripts/ts_bin_to_c.py --recipe-early-ta-$1 = $(PYTHON3) scripts/ts_bin_to_c.py --compress --ta $1 \ -- --out $(sub-dir-out)/early_ta_$$(early-ta-$1-uuid).c -+recipe-early-ta-$1 = $(PYTHON3) scripts/ts_bin_to_c.py $(early-ta-compress) \ -+ --ta $1 --out $(sub-dir-out)/early_ta_$$(early-ta-$1-uuid).c - endef - $(foreach f, $(EARLY_TA_PATHS), $(eval $(call process_early_ta,$(f)))) - $(foreach f, $(CFG_IN_TREE_EARLY_TAS), $(eval $(call \ -diff --git a/keys/default_rproc.pem b/keys/default_rproc.pem -new file mode 100644 -index 000000000..d54476f35 ---- /dev/null -+++ b/keys/default_rproc.pem -@@ -0,0 +1,27 @@ -+-----BEGIN RSA PRIVATE KEY----- -+MIIEpAIBAAKCAQEA1TD3AMwyJhzsliOhEStPrCrg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l/DbBCSLVun8fHNW -+E6QW4sEUld7eMg12H7h+xc0u+ya1TlJvXbOXFaKZnYFvmKtmjf5WhwMDWTvvaJiN -+za9jf5kommXtIJEhc0quc5TxpubYcpfadipM/L9mX7UzCrN90Hueeq81LwuIT8gb -+wEaxNrD3GJeQRAXoFpxwk57hAoGAaeqLfwyKDq4WJG120VtnY4xUomVJOVnOow2l -+YX+4kG45wvzTOoSrPbzb/G/QgqOdsPMt1VzdcO5VByN0XY1XKcyztlhRg3+raRWg -+vxDbR+K2Ysj+YvqHB1N/KzDOjtOHxWpOvpXWLBwHkpPTXoZos5wIEvyOcqIfM5rM -+oWvPcpECgYBCtncnPQQJfYcFebw3+rzm9OodF/s6G9afOrzqgEk/0Z6mi20x1c23 -+dzcZUpLl9p7fCFg0iz9NwLUYR9tZ/4zy+J4ZET7PduxoLG3m1TVzxSJM+dU+GtQi -+fBcdQuC6od9K9MJD2egwmes/I+aWhrIAatrG2iMtrOTG5N0mUMUc+w== -+-----END RSA PRIVATE KEY----- -diff --git a/lib/libutee/include/remoteproc_pta.h b/lib/libutee/include/remoteproc_pta.h -new file mode 100644 -index 000000000..cede192eb ---- /dev/null -+++ b/lib/libutee/include/remoteproc_pta.h -@@ -0,0 +1,157 @@ -+/* SPDX-License-Identifier: BSD-2-Clause */ -+/* -+ * Copyright (C) 2020, STMicroelectronics - All Rights Reserved -+ */ -+ -+#ifndef __REMOTEPROC_PTA_H -+#define __REMOTEPROC_PTA_H -+ -+#include -+ -+/* -+ * Interface to the pseudo TA which provides platform implementation -+ * of the remote processor management -+ */ -+ -+#define PTA_REMOTEPROC_UUID { 0x54af4a68, 0x19be, 0x40d7, \ -+ { 0xbb, 0xe6, 0x89, 0x50, 0x35, 0x0a, 0x87, 0x44 } } -+ -+/* Firmware format */ -+#define PTA_REMOTEPROC_PROPRIETARY_FMT BIT32(0) -+#define PTA_REMOTEPROC_ELF_FMT BIT32(1) -+ -+/* Firmware image protection */ -+/* The platorm supports copy of the input firmware image in secure memory */ -+#define PTA_REMOTEPROC_FW_SECURE_COPY BIT32(0) -+/* The platorm supports load of segment with hash protection */ -+#define PTA_REMOTEPROC_FW_WITH_HASH_TABLE BIT32(1) -+/* The platorm is able to change access to secure the firmware input image */ -+#define PTA_REMOTEPROC_FW_MEMORY_PROTECTION BIT32(2) -+ -+/** -+ * struct rproc_pta_key_info - public key information -+ * @algo: Algorithm, defined by public key algorithms TEE_ALG_* -+ * from TEE Internal API specification -+ * @info_size: Byte size of the @info -+ * @info: Append key information data -+ */ -+struct rproc_pta_key_info { -+ uint32_t algo; -+ uint32_t info_size; -+ char info[]; -+}; -+ -+static inline size_t -+ rproc_pta_get_keyinfo_size(struct rproc_pta_key_info *keyinf) -+{ -+ size_t s = 0; -+ -+ if (!keyinf || ADD_OVERFLOW(sizeof(*keyinf), keyinf->info_size, &s)) -+ return 0; -+ -+ return s; -+} -+ -+#define RPROC_PTA_GET_KEYINFO_SIZE(x) rproc_pta_get_keyinfo_size((x)) -+ -+/* -+ * Platform capabilities. -+ * -+ * Get Platform firmware loader service capabilities. -+ * -+ * [in] params[0].value.a: Unique 32bit firmware identifier -+ * [out] params[1].value.a: Firmware format (PTA_REMOTEPROC_*_FMT) -+ * [out] params[2].value.a: Image protection method (PTA_REMOTEPROC_FW_*) -+ */ -+#define PTA_REMOTEPROC_HW_CAPABILITIES 1 -+ -+/* -+ * Firmware loading. -+ * -+ * Optional service to implement only in case of proprietary format. -+ * -+ * [in] params[0].value.a: Unique 32bit firmware identifier -+ * [in] params[1].memref: Loadable firmware image -+ */ -+#define PTA_REMOTEPROC_FIRMWARE_LOAD 2 -+ -+/* -+ * Load a segment with a SHA256 hash. -+ * -+ * This command is used when the platform secure memory is too expensive to -+ * save the whole firmware image in secure memory. Upon segment load, a -+ * successful completion ensures the loaded image complies with the provided -+ * hash. -+ * -+ * [in] params[0].value.a: Unique 32bit firmware identifier -+ * [in] params[1].memref: Section data to load -+ * [in] params[2].value.a: 32bit LSB load device segment address -+ * [in] params[2].value.b: 32bit MSB load device segment address -+ * [in] params[3].memref: Expected hash (SHA256) of the payload -+ */ -+#define PTA_REMOTEPROC_LOAD_SEGMENT_SHA256 3 -+ -+/* -+ * Memory set. -+ * -+ * Fill a remote device memory with requested value. this is use for instance -+ * to clear a memory on the remote firmware load. -+ * -+ * [in] params[0].value.a: Unique 32bit firmware identifier -+ * [in] params[1].value.a: 32bit LSB device memory address -+ * [in] params[1].value.b: 32bit MSB device memory address -+ * [in] params[2].value.a: 32bit LSB device memory size -+ * [in] params[2].value.b: 32bit MSB device memory size -+ * [in] params[3].value.a: Byte value to be set -+ */ -+#define PTA_REMOTEPROC_SET_MEMORY 4 -+ -+/* -+ * Firmware start. -+ * -+ * Start up a successfully remote processor firmware. -+ * -+ * [in] params[0].value.a: Unique 32bit firmware identifier -+ */ -+#define PTA_REMOTEPROC_FIRMWARE_START 5 -+ -+/* -+ * Firmware stop. -+ * -+ * Stop of the remote processor firmware and release/clean resources. -+ * After the command successful completion, remote processor firmware must be -+ * reloaded prior being started again. -+ * -+ * [in] params[0].value.a: Unique 32bit firmware identifier -+ */ -+#define PTA_REMOTEPROC_FIRMWARE_STOP 6 -+ -+/* -+ * Firmware device to physical address conversion. -+ * -+ * Return the physical address corresponding to an address got from the -+ * firmware address layout. -+ * -+ * [in] params[0].value.a: Unique 32bit firmware identifier -+ * [in] params[1].value.a: 32bit LSB Device memory address -+ * [in] params[1].value.b: 32bit MSB Device memory address -+ * [in] params[2].value.a: 32bit LSB Device memory size -+ * [in] params[2].value.b: 32bit MSB Device memory size -+ * [out] params[3].value.a: 32bit LSB converted physical address -+ * [out] params[3].value.b: 32bit MSB converted physical address -+ */ -+#define PTA_REMOTEPROC_FIRMWARE_DA_TO_PA 7 -+ -+/* -+ * Verify the firmware digest against a signature -+ * -+ * Return TEE_SUCCESS if the signature is verified, else an error -+ * -+ * [in] params[0].value.a: Unique 32bit firmware identifier -+ * [in] params[1].memref: Key information (refer to @rproc_pta_key_info) -+ * [in] params[2].memref: Digest of the firmware authenticated data -+ * [in] params[3].memref: Signature of the firmware authenticated data -+ */ -+#define PTA_REMOTEPROC_VERIFY_DIGEST 8 -+ -+#endif /* __REMOTEPROC_PTA_H */ -diff --git a/mk/config.mk b/mk/config.mk -index 72f13fba4..a14c4f832 100644 ---- a/mk/config.mk -+++ b/mk/config.mk -@@ -306,6 +306,9 @@ $(eval $(call cfg-depends-all,CFG_REE_FS_TA_BUFFERED,CFG_REE_FS_TA)) - # in-tree TAs. CFG_IN_TREE_EARLY_TAS is formatted as: - # / - # for instance avb/023f8f1a-292a-432b-8fc4-de8471358067 -+# -+# By default the early TAs are compressed in the TEE binary, it is possible to -+# not compress them with CFG_EARLY_TA_COMPRESS=n - ifneq ($(EARLY_TA_PATHS)$(CFG_IN_TREE_EARLY_TAS),) - $(call force,CFG_EARLY_TA,y) - $(call force,CFG_EMBEDDED_TS,y) -@@ -323,6 +326,7 @@ endif - ifeq ($(CFG_EMBEDDED_TS),y) - $(call force,CFG_ZLIB,y) - endif -+CFG_EARLY_TA_COMPRESS ?= y - - # Enable paging, requires SRAM, can't be enabled by default - CFG_WITH_PAGER ?= n -diff --git a/mk/gcc.mk b/mk/gcc.mk -index adc77a24f..81bfa78ad 100644 ---- a/mk/gcc.mk -+++ b/mk/gcc.mk -@@ -13,11 +13,11 @@ nostdinc$(sm) := -nostdinc -isystem $(shell $(CC$(sm)) \ - -print-file-name=include 2> /dev/null) - - # Get location of libgcc from gcc --libgcc$(sm) := $(shell $(CC$(sm)) $(CFLAGS$(arch-bits-$(sm))) \ -+libgcc$(sm) := $(shell $(CC$(sm)) $(LIBGCC_LOCATE_CFLAGS) $(CFLAGS$(arch-bits-$(sm))) \ - -print-libgcc-file-name 2> /dev/null) --libstdc++$(sm) := $(shell $(CXX$(sm)) $(CXXFLAGS$(arch-bits-$(sm))) $(comp-cxxflags$(sm)) \ -+libstdc++$(sm) := $(shell $(CXX$(sm)) $(LIBGCC_LOCATE_CFLAGS) $(CXXFLAGS$(arch-bits-$(sm))) $(comp-cxxflags$(sm)) \ - -print-file-name=libstdc++.a 2> /dev/null) --libgcc_eh$(sm) := $(shell $(CXX$(sm)) $(CXXFLAGS$(arch-bits-$(sm))) $(comp-cxxflags$(sm)) \ -+libgcc_eh$(sm) := $(shell $(CXX$(sm)) $(LIBGCC_LOCATE_CFLAGS) $(CXXFLAGS$(arch-bits-$(sm))) $(comp-cxxflags$(sm)) \ - -print-file-name=libgcc_eh.a 2> /dev/null) - - # Define these to something to discover accidental use -diff --git a/scripts/sign_rproc_fw.py b/scripts/sign_rproc_fw.py -new file mode 100755 -index 000000000..4248ed280 ---- /dev/null -+++ b/scripts/sign_rproc_fw.py -@@ -0,0 +1,416 @@ -+#!/usr/bin/env python3 -+# SPDX-License-Identifier: BSD-2-Clause -+# -+# Copyright (C) 2020, STMicroelectronics - All Rights Reserved -+# -+ -+try: -+ from elftools.elf.elffile import ELFFile -+ from elftools.elf.sections import SymbolTableSection -+ from elftools.elf.enums import ENUM_P_TYPE_BASE -+ from elftools.elf.enums import * -+except ImportError: -+ print(""" -+*** -+ERROR: pyelftools python module is not installed or version < 0.25. -+*** -+""") -+ raise -+ -+from Cryptodome.Hash import SHA256 -+from Cryptodome.Signature import pkcs1_15 -+from Cryptodome.PublicKey import RSA -+from Cryptodome.Signature import DSS -+from Cryptodome.PublicKey import ECC -+import os -+import sys -+import struct -+import logging -+import binascii -+ -+ -+logging.basicConfig(stream=sys.stderr, level=logging.INFO) -+ -+ENUM_HASH_TYPE = dict( -+ SHA256=1, -+) -+ -+ENUM_SIGNATURE_TYPE = dict( -+ RSA=1, -+ ECC=2, -+) -+ -+ENUM_BINARY_TYPE = dict( -+ ELF=1, -+) -+ -+ -+def dump_buffer(buf, step=16, name="", logger=logging.info, indent=""): -+ logger("%s%s:" % (indent, name)) -+ for i in range(0, len(buf), step): -+ logger("%s " % (indent) + " ". -+ join(["%02X" % c for c in buf[i:i+step]])) -+ logger("\n") -+ -+ -+class RSA_Signature(object): -+ -+ def __init__(self, key): -+ self._hasher = SHA256.new() -+ self.signer = pkcs1_15.new(key) -+ -+ def hash_compute(self, segment): -+ self._hasher.update(segment) -+ -+ def sign(self): -+ return self.signer.sign(self._hasher) -+ -+ -+class ECC_Signature(object): -+ -+ def __init__(self, key): -+ self._hasher = SHA256.new() -+ self.signer = DSS.new(key, 'fips-186-3') -+ -+ def hash_compute(self, segment): -+ self._hasher.update(segment) -+ -+ def sign(self): -+ return self.signer.sign(self._hasher) -+ -+ -+Signature = { -+ 1: RSA_Signature, -+ 2: ECC_Signature, -+} -+ -+ -+class SegmentHashStruct: -+ pass -+ -+ -+class SegmentHash(object): -+ ''' -+ Hash table based on Elf program segments -+ ''' -+ def __init__(self, img): -+ self._num_segments = img.num_segments() -+ self._pack_fmt = '<%dL' % 8 -+ self.img = img -+ self.hashProgTable = bytes() -+ self._offset = 0 -+ -+ def get_table(self): -+ ''' -+ Create a segment hash table containing for each segment: -+ - the segments header -+ - a hash of the segment -+ ''' -+ h = SHA256.new() -+ seg = SegmentHashStruct() -+ self.size = (h.digest_size + 32) * self._num_segments -+ logging.debug("hash section size %d" % self.size) -+ del h -+ self.buf = bytearray(self.size) -+ self._bufview_ = memoryview(self.buf) -+ -+ for i in range(self._num_segments): -+ h = SHA256.new() -+ segment = self.img.get_segment(i) -+ seg.header = self.img.get_segment(i).header -+ logging.debug("compute hash for segment offset %s" % seg.header) -+ h.update(segment.data()) -+ seg.hash = h.digest() -+ logging.debug("hash computed: %s" % seg.hash) -+ del h -+ struct.pack_into('.sig') -+ -+ parsed = parser.parse_args() -+ -+ # Set defaults for optional arguments. -+ -+ if parsed.outf is None: -+ parsed.outf = str(parsed.inf)+'.sig' -+ -+ return parsed -+ -+ -+def rsa_key(keyf): -+ return RSA.importKey(open(keyf).read()) -+ -+ -+def ecc_key(keyf): -+ return ECC.import_key(open(keyf).read()) -+ -+ -+key_type = { -+ 1: rsa_key, -+ 2: ecc_key, -+} -+ -+ -+def rsa_sig_size(key): -+ return key.size_in_bytes() -+ -+ -+def ecc_sig_size(key): -+ # to be improve... -+ # DSA size is N/4 so 64 for DSA (L,N) = (2048, 256) -+ return 64 -+ -+ -+sig_size_type = { -+ 1: rsa_sig_size, -+ 2: ecc_sig_size, -+} -+ -+ -+def main(): -+ from Cryptodome.Signature import pss -+ from Cryptodome.Hash import SHA256 -+ from Cryptodome.PublicKey import RSA -+ import base64 -+ import logging -+ import struct -+ -+ logging.basicConfig() -+ logger = logging.getLogger(os.path.basename(__file__)) -+ -+ args = get_args(logger) -+ -+ # Initialise the header */ -+ s_header = ImageHeader() -+ -+ get_key = key_type.get(ENUM_SIGNATURE_TYPE[args.key_type], -+ lambda: "Invalid sign type") -+ key = get_key(args.keyf) -+ -+ if not key.has_private(): -+ logger.error('Provided key cannot be used for signing, ' + -+ 'please use offline-signing mode.') -+ sys.exit(1) -+ -+ # Firmware image -+ input_file = open(args.inf, 'rb') -+ img = ELFFile(input_file) -+ -+ # need to reopen the file to get the raw data -+ with open(args.inf, 'rb') as f: -+ bin_img = f.read() -+ img_size = len(bin_img) -+ logging.debug("image size %d" % img_size) -+ s_header.img_length = img_size -+ s_header.img_type = ENUM_BINARY_TYPE['ELF'] -+ -+ # Hash table chunk -+ h = SHA256.new() -+ -+ # Compute the hash table -+ hash_table = SegmentHash(img) -+ hash = hash_table.get_table() -+ -+ s_header.hash_offset = s_header.size -+ s_header.hash_length = hash_table.size -+ s_header.hash_type = ENUM_HASH_TYPE['SHA256'] -+ # Get padding to align on 64 bytes -+ hash_align = s_header.hash_length % 8 -+ -+ # Key information chunk -+ if args.key_infof: -+ with open(args.key_infof, 'rb') as f: -+ key_info = f.read() -+ s_header.key_length = sys.getsizeof(key_info) -+ s_header.key_offset = s_header.hash_offset + s_header.hash_length + \ -+ hash_align -+ # Get padding to align on 64 bytes -+ key_info_align = s_header.key_length % 8 -+ else: -+ key_info_align = 0 -+ -+ # Signature chunk -+ s_header.sign_type = ENUM_SIGNATURE_TYPE[args.key_type] -+ -+ sign_size = sig_size_type.get(ENUM_SIGNATURE_TYPE[args.key_type], -+ lambda: "Invalid sign type")(key) -+ s_header.sign_length = sign_size -+ -+ if args.key_infof: -+ s_header.sign_offset = s_header.key_offset + s_header.key_length + \ -+ key_info_align -+ else: -+ s_header.sign_offset = s_header.hash_offset + s_header.hash_length + \ -+ hash_align -+ -+ s_header.img_offset = s_header.sign_offset + sign_size -+ -+ s_header.length = s_header.size + s_header.hash_length + hash_align + \ -+ s_header.key_length + key_info_align + s_header.sign_length -+ -+ header = s_header.get_packed() -+ -+ # Generate signature -+ signer = Signature.get(ENUM_SIGNATURE_TYPE[args.key_type])(key) -+ -+ signer.hash_compute(header) -+ signer.hash_compute(bytes(hash)) -+ if args.key_infof: -+ signer.hash_compute(key_info) -+ -+ signature = signer.sign() -+ if len(signature) != sign_size: -+ raise Exception(("Actual signature length is not equal to ", -+ "the computed one: {} != {}". -+ format(len(signature), sign_size))) -+ -+ s_header.dump() -+ -+ with open(args.outf, 'wb') as f: -+ f.write(header) -+ f.write(hash) -+ if hash_align: -+ f.write(bytearray(hash_align)) -+ if args.key_infof: -+ if key_info_align: -+ f.write(key_info) -+ f.write(bytearray(key_info_align)) -+ f.write(signature) -+ f.write(bytearray(sign_size - s_header.sign_length)) -+ f.write(bin_img) -+ -+ -+if __name__ == "__main__": -+ main() -diff --git a/ta/remoteproc/Makefile b/ta/remoteproc/Makefile -new file mode 100644 -index 000000000..0d1a5010c ---- /dev/null -+++ b/ta/remoteproc/Makefile -@@ -0,0 +1,18 @@ -+# The UUID for the Trusted Application -+BINARY=80a4c275-0a47-4905-8285-1486a9771a08 -+ -+ifdef TA_CROSS_COMPILE -+CROSS_COMPILE ?= $(TA_CROSS_COMPILE) -+endif -+export CROSS_COMPILE -+ -+CFG_TEE_TA_LOG_LEVEL ?= 2 -+CPPFLAGS += -DCFG_TEE_TA_LOG_LEVEL=$(CFG_TEE_TA_LOG_LEVEL) -+ -+-include $(TA_DEV_KIT_DIR)/mk/ta_dev_kit.mk -+ -+ifeq ($(wildcard $(TA_DEV_KIT_DIR)/mk/ta_dev_kit.mk), ) -+clean: -+ @echo 'Note: $$(TA_DEV_KIT_DIR)/mk/ta_dev_kit.mk not found, cannot clean TA' -+ @echo 'Note: TA_DEV_KIT_DIR=$(TA_DEV_KIT_DIR)' -+endif -diff --git a/ta/remoteproc/elf_parser.c b/ta/remoteproc/elf_parser.c -new file mode 100644 -index 000000000..da3e6f4ae ---- /dev/null -+++ b/ta/remoteproc/elf_parser.c -@@ -0,0 +1,186 @@ -+ // SPDX-License-Identifier: BSD-2-Clause -+ /* -+ * Copyright (C) 2020, STMicroelectronics - All Rights Reserved -+ */ -+ -+#include -+#include -+#include -+#include -+#include -+#include -+ -+static bool va_in_fwm_image_range(void *va, uint8_t *fw, size_t fw_size) -+{ -+ uint8_t *vaddr = va; -+ -+ assert(fw + fw_size >= fw); -+ return vaddr >= fw && vaddr < fw + fw_size; -+} -+ -+/* -+ * e32_parse_ehdr() - Check and parse the ELF header -+ * -+ * fw: Firmware ELF file image -+ * size: Byte size of firmware ELF file image -+ */ -+TEE_Result e32_parse_ehdr(uint8_t *fw, size_t size) -+{ -+ Elf32_Ehdr *ehdr = (Elf32_Ehdr *)(void *)fw; -+ -+ if (!fw || !ALIGNMENT_IS_OK(fw, uint32_t)) { -+ EMSG("Invalid fw address %p", fw); -+ return TEE_ERROR_BAD_PARAMETERS; -+ } -+ -+ if (size < sizeof(Elf32_Ehdr) || -+ size < (ehdr->e_shoff + sizeof(Elf32_Shdr))) -+ return TEE_ERROR_CORRUPT_OBJECT; -+ -+ if (!IS_ELF(*ehdr) || -+ ehdr->e_ident[EI_VERSION] != EV_CURRENT || -+ ehdr->e_ident[EI_CLASS] != ELFCLASS32 || -+ ehdr->e_phentsize != sizeof(Elf32_Phdr) || -+ ehdr->e_shentsize != sizeof(Elf32_Shdr)) { -+ EMSG("Invalid header"); -+ -+ return TEE_ERROR_BAD_FORMAT; -+ } -+ -+ if (ehdr->e_phnum == 0) { -+ EMSG("No loadable segment found"); -+ return TEE_ERROR_BAD_FORMAT; -+ } -+ -+ return TEE_SUCCESS; -+} -+ -+/* -+ * e32_parser_load_elf_image - simple ELF loader -+ * fw: Firmware ELF file image -+ * fw_size: Firmware ELF file image byte size -+ * load_seg: Callback for loading a firmware image segment into device memory -+ * priv_data: Private data passed to @load_seg callback. -+ */ -+TEE_Result e32_parser_load_elf_image(uint8_t *fw, size_t fw_size, -+ TEE_Result (*load_seg)(uint8_t *src, -+ uint32_t size, -+ uint32_t da, -+ uint32_t mem_size, -+ void *priv), -+ void *priv_data) -+{ -+ Elf32_Ehdr *ehdr = (Elf32_Ehdr *)(void *)fw; -+ Elf32_Phdr *phdr = (void *)((int8_t *)ehdr + ehdr->e_phoff); -+ TEE_Result res = TEE_SUCCESS; -+ unsigned int i = 0; -+ -+ if (!load_seg) -+ return TEE_ERROR_BAD_PARAMETERS; -+ -+ if (!ALIGNMENT_IS_OK(phdr, uint32_t) || -+ !va_in_fwm_image_range(phdr, fw, fw_size)) -+ return TEE_ERROR_CORRUPT_OBJECT; -+ -+ for (i = 0; i < ehdr->e_phnum; i++, phdr++) { -+ uint32_t dst = phdr->p_paddr; -+ uint8_t *src = NULL; -+ -+ if (!va_in_fwm_image_range((void *)((vaddr_t)(phdr + 1) - 1), -+ fw, fw_size)) -+ return TEE_ERROR_CORRUPT_OBJECT; -+ -+ if (phdr->p_type != PT_LOAD) -+ continue; -+ -+ src = (uint8_t *)fw + phdr->p_offset; -+ -+ if (!va_in_fwm_image_range(src, fw, fw_size) || -+ !va_in_fwm_image_range(src + phdr->p_filesz, fw, fw_size)) -+ return TEE_ERROR_CORRUPT_OBJECT; -+ -+ res = load_seg(src, phdr->p_filesz, dst, phdr->p_memsz, -+ priv_data); -+ if (res) -+ return res; -+ } -+ -+ return TEE_SUCCESS; -+} -+ -+/* Helper to find resource table in an ELF image */ -+int e32_parser_find_rsc_table(uint8_t *fw, size_t fw_size, -+ Elf32_Addr *rsc_addr, Elf32_Word *rsc_size) -+{ -+ Elf32_Shdr *shdr = NULL; -+ int i = 0; -+ char *name_table = NULL; -+ struct resource_table *table = NULL; -+ Elf32_Ehdr *ehdr = (Elf32_Ehdr *)(void *)fw; -+ uint8_t *elf_data = fw; -+ -+ shdr = (void *)(fw + ehdr->e_shoff); -+ if (!ALIGNMENT_IS_OK(shdr, uint32_t) || -+ !va_in_fwm_image_range(shdr, fw, fw_size)) -+ return TEE_ERROR_CORRUPT_OBJECT; -+ -+ name_table = (char *)elf_data + shdr[ehdr->e_shstrndx].sh_offset; -+ if (!va_in_fwm_image_range(name_table, fw, fw_size)) -+ return TEE_ERROR_CORRUPT_OBJECT; -+ -+ for (i = 0; i < ehdr->e_shnum; i++, shdr++) { -+ size_t size = shdr->sh_size; -+ size_t offset = shdr->sh_offset; -+ -+ if (!va_in_fwm_image_range(shdr, fw, fw_size)) -+ return TEE_ERROR_CORRUPT_OBJECT; -+ -+ if (strcmp(name_table + shdr->sh_name, ".resource_table")) -+ continue; -+ -+ if (!shdr->sh_size) { -+ IMSG("Ignore empty resource table section"); -+ return TEE_ERROR_NO_DATA; -+ } -+ -+ if (offset + size > fw_size || offset + size < size) { -+ EMSG("Resource table truncated"); -+ return TEE_ERROR_BAD_FORMAT; -+ } -+ -+ if (sizeof(struct resource_table) > size) { -+ EMSG("No header found in resource table"); -+ return TEE_ERROR_BAD_FORMAT; -+ } -+ -+ table = (struct resource_table *)(void *)(elf_data + offset); -+ if (!ALIGNMENT_IS_OK(table, uint32_t)) -+ return TEE_ERROR_CORRUPT_OBJECT; -+ -+ if (table->ver != 1) { -+ EMSG("Unsupported fw version %"PRId32, table->ver); -+ return TEE_ERROR_BAD_FORMAT; -+ } -+ -+ if (table->reserved[0] || table->reserved[1]) { -+ EMSG("Non zero reserved bytes"); -+ return TEE_ERROR_BAD_FORMAT; -+ } -+ -+ if (table->num * sizeof(*table->offset) + -+ sizeof(struct resource_table) > size) { -+ EMSG("Resource table incomplete"); -+ return TEE_ERROR_BAD_FORMAT; -+ } -+ -+ DMSG("Resource table address %#"PRIx32", size %"PRIu32, -+ shdr->sh_addr, shdr->sh_size); -+ -+ *rsc_addr = shdr->sh_addr; -+ *rsc_size = shdr->sh_size; -+ -+ return TEE_SUCCESS; -+ } -+ -+ return TEE_ERROR_NO_DATA; -+} -diff --git a/ta/remoteproc/include/elf32.h b/ta/remoteproc/include/elf32.h -new file mode 100644 -index 000000000..2806c615d ---- /dev/null -+++ b/ta/remoteproc/include/elf32.h -@@ -0,0 +1,243 @@ -+/* SPDX-License-Identifier: BSD-2-Clause */ -+/*- -+ * Copyright (c) 1996-1998 John D. Polstra. -+ * 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. -+ * -+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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. -+ * -+ * $FreeBSD$ -+ */ -+ -+#ifndef _SYS_ELF32_H_ -+#define _SYS_ELF32_H_ 1 -+ -+#include -+#include -+ -+/* -+ * ELF definitions common to all 32-bit architectures. -+ */ -+ -+typedef uint32_t Elf32_Addr; -+typedef uint16_t Elf32_Half; -+typedef uint32_t Elf32_Off; -+typedef int32_t Elf32_Sword; -+typedef uint32_t Elf32_Word; -+typedef uint64_t Elf32_Lword; -+ -+typedef Elf32_Word Elf32_Hashelt; -+ -+/* Non-standard class-dependent datatype used for abstraction. */ -+typedef Elf32_Word Elf32_Size; -+typedef Elf32_Sword Elf32_Ssize; -+ -+/* -+ * ELF header. -+ */ -+ -+typedef struct { -+ unsigned char e_ident[EI_NIDENT]; /* File identification. */ -+ Elf32_Half e_type; /* File type. */ -+ Elf32_Half e_machine; /* Machine architecture. */ -+ Elf32_Word e_version; /* ELF format version. */ -+ Elf32_Addr e_entry; /* Entry point. */ -+ Elf32_Off e_phoff; /* Program header file offset. */ -+ Elf32_Off e_shoff; /* Section header file offset. */ -+ Elf32_Word e_flags; /* Architecture-specific flags. */ -+ Elf32_Half e_ehsize; /* Size of ELF header in bytes. */ -+ Elf32_Half e_phentsize; /* Size of program header entry. */ -+ Elf32_Half e_phnum; /* Number of program header entries. */ -+ Elf32_Half e_shentsize; /* Size of section header entry. */ -+ Elf32_Half e_shnum; /* Number of section header entries. */ -+ Elf32_Half e_shstrndx; /* Section name strings section. */ -+} Elf32_Ehdr; -+ -+/* -+ * Section header. -+ */ -+ -+typedef struct { -+ Elf32_Word sh_name; /* Section name (index into the -+ section header string table). */ -+ Elf32_Word sh_type; /* Section type. */ -+ Elf32_Word sh_flags; /* Section flags. */ -+ Elf32_Addr sh_addr; /* Address in memory image. */ -+ Elf32_Off sh_offset; /* Offset in file. */ -+ Elf32_Word sh_size; /* Size in bytes. */ -+ Elf32_Word sh_link; /* Index of a related section. */ -+ Elf32_Word sh_info; /* Depends on section type. */ -+ Elf32_Word sh_addralign; /* Alignment in bytes. */ -+ Elf32_Word sh_entsize; /* Size of each entry in section. */ -+} Elf32_Shdr; -+ -+/* -+ * Program header. -+ */ -+ -+typedef struct { -+ Elf32_Word p_type; /* Entry type. */ -+ Elf32_Off p_offset; /* File offset of contents. */ -+ Elf32_Addr p_vaddr; /* Virtual address in memory image. */ -+ Elf32_Addr p_paddr; /* Physical address (not used). */ -+ Elf32_Word p_filesz; /* Size of contents in file. */ -+ Elf32_Word p_memsz; /* Size of contents in memory. */ -+ Elf32_Word p_flags; /* Access permission flags. */ -+ Elf32_Word p_align; /* Alignment in memory and file. */ -+} Elf32_Phdr; -+ -+/* -+ * Dynamic structure. The ".dynamic" section contains an array of them. -+ */ -+ -+typedef struct { -+ Elf32_Sword d_tag; /* Entry type. */ -+ union { -+ Elf32_Word d_val; /* Integer value. */ -+ Elf32_Addr d_ptr; /* Address value. */ -+ } d_un; -+} Elf32_Dyn; -+ -+/* -+ * Relocation entries. -+ */ -+ -+/* Relocations that don't need an addend field. */ -+typedef struct { -+ Elf32_Addr r_offset; /* Location to be relocated. */ -+ Elf32_Word r_info; /* Relocation type and symbol index. */ -+} Elf32_Rel; -+ -+/* Relocations that need an addend field. */ -+typedef struct { -+ Elf32_Addr r_offset; /* Location to be relocated. */ -+ Elf32_Word r_info; /* Relocation type and symbol index. */ -+ Elf32_Sword r_addend; /* Addend. */ -+} Elf32_Rela; -+ -+/* Macros for accessing the fields of r_info. */ -+#define ELF32_R_SYM(info) ((info) >> 8) -+#define ELF32_R_TYPE(info) ((unsigned char)(info)) -+ -+/* Macro for constructing r_info from field values. */ -+#define ELF32_R_INFO(sym, type) (((sym) << 8) + (unsigned char)(type)) -+ -+/* -+ * Note entry header -+ */ -+typedef Elf_Note Elf32_Nhdr; -+ -+/* -+ * Move entry -+ */ -+typedef struct { -+ Elf32_Lword m_value; /* symbol value */ -+ Elf32_Word m_info; /* size + index */ -+ Elf32_Word m_poffset; /* symbol offset */ -+ Elf32_Half m_repeat; /* repeat count */ -+ Elf32_Half m_stride; /* stride info */ -+} Elf32_Move; -+ -+/* -+ * The macros compose and decompose values for Move.r_info -+ * -+ * sym = ELF32_M_SYM(M.m_info) -+ * size = ELF32_M_SIZE(M.m_info) -+ * M.m_info = ELF32_M_INFO(sym, size) -+ */ -+#define ELF32_M_SYM(info) ((info)>>8) -+#define ELF32_M_SIZE(info) ((unsigned char)(info)) -+#define ELF32_M_INFO(sym, size) (((sym)<<8)+(unsigned char)(size)) -+ -+/* -+ * Hardware/Software capabilities entry -+ */ -+typedef struct { -+ Elf32_Word c_tag; /* how to interpret value */ -+ union { -+ Elf32_Word c_val; -+ Elf32_Addr c_ptr; -+ } c_un; -+} Elf32_Cap; -+ -+/* -+ * Symbol table entries. -+ */ -+ -+typedef struct { -+ Elf32_Word st_name; /* String table index of name. */ -+ Elf32_Addr st_value; /* Symbol value. */ -+ Elf32_Word st_size; /* Size of associated object. */ -+ unsigned char st_info; /* Type and binding information. */ -+ unsigned char st_other; /* Reserved (not used). */ -+ Elf32_Half st_shndx; /* Section index of symbol. */ -+} Elf32_Sym; -+ -+/* Macros for accessing the fields of st_info. */ -+#define ELF32_ST_BIND(info) ((info) >> 4) -+#define ELF32_ST_TYPE(info) ((info) & 0xf) -+ -+/* Macro for constructing st_info from field values. */ -+#define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf)) -+ -+/* Macro for accessing the fields of st_other. */ -+#define ELF32_ST_VISIBILITY(oth) ((oth) & 0x3) -+ -+/* Structures used by Sun & GNU symbol versioning. */ -+typedef struct { -+ Elf32_Half vd_version; -+ Elf32_Half vd_flags; -+ Elf32_Half vd_ndx; -+ Elf32_Half vd_cnt; -+ Elf32_Word vd_hash; -+ Elf32_Word vd_aux; -+ Elf32_Word vd_next; -+} Elf32_Verdef; -+ -+typedef struct { -+ Elf32_Word vda_name; -+ Elf32_Word vda_next; -+} Elf32_Verdaux; -+ -+typedef struct { -+ Elf32_Half vn_version; -+ Elf32_Half vn_cnt; -+ Elf32_Word vn_file; -+ Elf32_Word vn_aux; -+ Elf32_Word vn_next; -+} Elf32_Verneed; -+ -+typedef struct { -+ Elf32_Word vna_hash; -+ Elf32_Half vna_flags; -+ Elf32_Half vna_other; -+ Elf32_Word vna_name; -+ Elf32_Word vna_next; -+} Elf32_Vernaux; -+ -+typedef Elf32_Half Elf32_Versym; -+ -+typedef struct { -+ Elf32_Half si_boundto; /* direct bindings - symbol bound to */ -+ Elf32_Half si_flags; /* per symbol flags */ -+} Elf32_Syminfo; -+ -+#endif /* !_SYS_ELF32_H_ */ -diff --git a/ta/remoteproc/include/elf_common.h b/ta/remoteproc/include/elf_common.h -new file mode 100644 -index 000000000..a1da0ef80 ---- /dev/null -+++ b/ta/remoteproc/include/elf_common.h -@@ -0,0 +1,1014 @@ -+/* SPDX-License-Identifier: BSD-2-Clause */ -+/*- -+ * Copyright (c) 2000, 2001, 2008, 2011, David E. O'Brien -+ * Copyright (c) 1998 John D. Polstra. -+ * 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. -+ * -+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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. -+ * -+ * $FreeBSD$ -+ */ -+ -+#ifndef _SYS_ELF_COMMON_H_ -+#define _SYS_ELF_COMMON_H_ 1 -+ -+#include -+ -+/* -+ * ELF definitions that are independent of architecture or word size. -+ */ -+ -+#ifndef __ASSEMBLER__ -+/* -+ * Note header. The ".note" section contains an array of notes. Each -+ * begins with this header, aligned to a word boundary. Immediately -+ * following the note header is n_namesz bytes of name, padded to the -+ * next word boundary. Then comes n_descsz bytes of descriptor, again -+ * padded to a word boundary. The values of n_namesz and n_descsz do -+ * not include the padding. -+ */ -+ -+typedef struct { -+ uint32_t n_namesz; /* Length of name. */ -+ uint32_t n_descsz; /* Length of descriptor. */ -+ uint32_t n_type; /* Type of this note. */ -+} Elf_Note; -+ -+/* -+ * The header for GNU-style hash sections. -+ */ -+ -+typedef struct { -+ uint32_t gh_nbuckets; /* Number of hash buckets. */ -+ uint32_t gh_symndx; /* First visible symbol in .dynsym. */ -+ uint32_t gh_maskwords; /* #maskwords used in bloom filter. */ -+ uint32_t gh_shift2; /* Bloom filter shift count. */ -+} Elf_GNU_Hash_Header; -+#endif /*__ASSEMBLER__*/ -+ -+/* Indexes into the e_ident array. Keep synced with -+ http://www.sco.com/developers/gabi/latest/ch4.eheader.html */ -+#define EI_MAG0 0 /* Magic number, byte 0. */ -+#define EI_MAG1 1 /* Magic number, byte 1. */ -+#define EI_MAG2 2 /* Magic number, byte 2. */ -+#define EI_MAG3 3 /* Magic number, byte 3. */ -+#define EI_CLASS 4 /* Class of machine. */ -+#define EI_DATA 5 /* Data format. */ -+#define EI_VERSION 6 /* ELF format version. */ -+#define EI_OSABI 7 /* Operating system / ABI identification */ -+#define EI_ABIVERSION 8 /* ABI version */ -+#define OLD_EI_BRAND 8 /* Start of architecture identification. */ -+#define EI_PAD 9 /* Start of padding (per SVR4 ABI). */ -+#define EI_NIDENT 16 /* Size of e_ident array. */ -+ -+/* Values for the magic number bytes. */ -+#define ELFMAG0 0x7f -+#define ELFMAG1 'E' -+#define ELFMAG2 'L' -+#define ELFMAG3 'F' -+#define ELFMAG "\177ELF" /* magic string */ -+#define SELFMAG 4 /* magic string size */ -+ -+/* Values for e_ident[EI_VERSION] and e_version. */ -+#define EV_NONE 0 -+#define EV_CURRENT 1 -+ -+/* Values for e_ident[EI_CLASS]. */ -+#define ELFCLASSNONE 0 /* Unknown class. */ -+#define ELFCLASS32 1 /* 32-bit architecture. */ -+#define ELFCLASS64 2 /* 64-bit architecture. */ -+ -+/* Values for e_ident[EI_DATA]. */ -+#define ELFDATANONE 0 /* Unknown data format. */ -+#define ELFDATA2LSB 1 /* 2's complement little-endian. */ -+#define ELFDATA2MSB 2 /* 2's complement big-endian. */ -+ -+/* Values for e_ident[EI_OSABI]. */ -+#define ELFOSABI_NONE 0 /* UNIX System V ABI */ -+#define ELFOSABI_HPUX 1 /* HP-UX operating system */ -+#define ELFOSABI_NETBSD 2 /* NetBSD */ -+#define ELFOSABI_LINUX 3 /* GNU/Linux */ -+#define ELFOSABI_HURD 4 /* GNU/Hurd */ -+#define ELFOSABI_86OPEN 5 /* 86Open common IA32 ABI */ -+#define ELFOSABI_SOLARIS 6 /* Solaris */ -+#define ELFOSABI_AIX 7 /* AIX */ -+#define ELFOSABI_IRIX 8 /* IRIX */ -+#define ELFOSABI_FREEBSD 9 /* FreeBSD */ -+#define ELFOSABI_TRU64 10 /* TRU64 UNIX */ -+#define ELFOSABI_MODESTO 11 /* Novell Modesto */ -+#define ELFOSABI_OPENBSD 12 /* OpenBSD */ -+#define ELFOSABI_OPENVMS 13 /* Open VMS */ -+#define ELFOSABI_NSK 14 /* HP Non-Stop Kernel */ -+#define ELFOSABI_AROS 15 /* Amiga Research OS */ -+#define ELFOSABI_ARM 97 /* ARM */ -+#define ELFOSABI_STANDALONE 255 /* Standalone (embedded) application */ -+ -+#define ELFOSABI_SYSV ELFOSABI_NONE /* symbol used in old spec */ -+#define ELFOSABI_MONTEREY ELFOSABI_AIX /* Monterey */ -+ -+/* e_ident */ -+#define IS_ELF(ehdr) ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \ -+ (ehdr).e_ident[EI_MAG1] == ELFMAG1 && \ -+ (ehdr).e_ident[EI_MAG2] == ELFMAG2 && \ -+ (ehdr).e_ident[EI_MAG3] == ELFMAG3) -+ -+/* Values for e_type. */ -+#define ET_NONE 0 /* Unknown type. */ -+#define ET_REL 1 /* Relocatable. */ -+#define ET_EXEC 2 /* Executable. */ -+#define ET_DYN 3 /* Shared object. */ -+#define ET_CORE 4 /* Core file. */ -+#define ET_LOOS 0xfe00 /* First operating system specific. */ -+#define ET_HIOS 0xfeff /* Last operating system-specific. */ -+#define ET_LOPROC 0xff00 /* First processor-specific. */ -+#define ET_HIPROC 0xffff /* Last processor-specific. */ -+ -+/* Values for e_machine. */ -+#define EM_NONE 0 /* Unknown machine. */ -+#define EM_M32 1 /* AT&T WE32100. */ -+#define EM_SPARC 2 /* Sun SPARC. */ -+#define EM_386 3 /* Intel i386. */ -+#define EM_68K 4 /* Motorola 68000. */ -+#define EM_88K 5 /* Motorola 88000. */ -+#define EM_860 7 /* Intel i860. */ -+#define EM_MIPS 8 /* MIPS R3000 Big-Endian only. */ -+#define EM_S370 9 /* IBM System/370. */ -+#define EM_MIPS_RS3_LE 10 /* MIPS R3000 Little-Endian. */ -+#define EM_PARISC 15 /* HP PA-RISC. */ -+#define EM_VPP500 17 /* Fujitsu VPP500. */ -+#define EM_SPARC32PLUS 18 /* SPARC v8plus. */ -+#define EM_960 19 /* Intel 80960. */ -+#define EM_PPC 20 /* PowerPC 32-bit. */ -+#define EM_PPC64 21 /* PowerPC 64-bit. */ -+#define EM_S390 22 /* IBM System/390. */ -+#define EM_V800 36 /* NEC V800. */ -+#define EM_FR20 37 /* Fujitsu FR20. */ -+#define EM_RH32 38 /* TRW RH-32. */ -+#define EM_RCE 39 /* Motorola RCE. */ -+#define EM_ARM 40 /* ARM. */ -+#define EM_SH 42 /* Hitachi SH. */ -+#define EM_SPARCV9 43 /* SPARC v9 64-bit. */ -+#define EM_TRICORE 44 /* Siemens TriCore embedded processor. */ -+#define EM_ARC 45 /* Argonaut RISC Core. */ -+#define EM_H8_300 46 /* Hitachi H8/300. */ -+#define EM_H8_300H 47 /* Hitachi H8/300H. */ -+#define EM_H8S 48 /* Hitachi H8S. */ -+#define EM_H8_500 49 /* Hitachi H8/500. */ -+#define EM_IA_64 50 /* Intel IA-64 Processor. */ -+#define EM_MIPS_X 51 /* Stanford MIPS-X. */ -+#define EM_COLDFIRE 52 /* Motorola ColdFire. */ -+#define EM_68HC12 53 /* Motorola M68HC12. */ -+#define EM_MMA 54 /* Fujitsu MMA. */ -+#define EM_PCP 55 /* Siemens PCP. */ -+#define EM_NCPU 56 /* Sony nCPU. */ -+#define EM_NDR1 57 /* Denso NDR1 microprocessor. */ -+#define EM_STARCORE 58 /* Motorola Star*Core processor. */ -+#define EM_ME16 59 /* Toyota ME16 processor. */ -+#define EM_ST100 60 /* STMicroelectronics ST100 processor. */ -+#define EM_TINYJ 61 /* Advanced Logic Corp. TinyJ processor. */ -+#define EM_X86_64 62 /* Advanced Micro Devices x86-64 */ -+#define EM_AMD64 EM_X86_64 /* Advanced Micro Devices x86-64 (compat) */ -+#define EM_PDSP 63 /* Sony DSP Processor. */ -+#define EM_FX66 66 /* Siemens FX66 microcontroller. */ -+#define EM_ST9PLUS 67 /* STMicroelectronics ST9+ 8/16 -+ microcontroller. */ -+#define EM_ST7 68 /* STmicroelectronics ST7 8-bit -+ microcontroller. */ -+#define EM_68HC16 69 /* Motorola MC68HC16 microcontroller. */ -+#define EM_68HC11 70 /* Motorola MC68HC11 microcontroller. */ -+#define EM_68HC08 71 /* Motorola MC68HC08 microcontroller. */ -+#define EM_68HC05 72 /* Motorola MC68HC05 microcontroller. */ -+#define EM_SVX 73 /* Silicon Graphics SVx. */ -+#define EM_ST19 74 /* STMicroelectronics ST19 8-bit mc. */ -+#define EM_VAX 75 /* Digital VAX. */ -+#define EM_CRIS 76 /* Axis Communications 32-bit embedded -+ processor. */ -+#define EM_JAVELIN 77 /* Infineon Technologies 32-bit embedded -+ processor. */ -+#define EM_FIREPATH 78 /* Element 14 64-bit DSP Processor. */ -+#define EM_ZSP 79 /* LSI Logic 16-bit DSP Processor. */ -+#define EM_MMIX 80 /* Donald Knuth's educational 64-bit proc. */ -+#define EM_HUANY 81 /* Harvard University machine-independent -+ object files. */ -+#define EM_PRISM 82 /* SiTera Prism. */ -+#define EM_AVR 83 /* Atmel AVR 8-bit microcontroller. */ -+#define EM_FR30 84 /* Fujitsu FR30. */ -+#define EM_D10V 85 /* Mitsubishi D10V. */ -+#define EM_D30V 86 /* Mitsubishi D30V. */ -+#define EM_V850 87 /* NEC v850. */ -+#define EM_M32R 88 /* Mitsubishi M32R. */ -+#define EM_MN10300 89 /* Matsushita MN10300. */ -+#define EM_MN10200 90 /* Matsushita MN10200. */ -+#define EM_PJ 91 /* picoJava. */ -+#define EM_OPENRISC 92 /* OpenRISC 32-bit embedded processor. */ -+#define EM_ARC_A5 93 /* ARC Cores Tangent-A5. */ -+#define EM_XTENSA 94 /* Tensilica Xtensa Architecture. */ -+#define EM_VIDEOCORE 95 /* Alphamosaic VideoCore processor. */ -+#define EM_TMM_GPP 96 /* Thompson Multimedia General Purpose -+ Processor. */ -+#define EM_NS32K 97 /* National Semiconductor 32000 series. */ -+#define EM_TPC 98 /* Tenor Network TPC processor. */ -+#define EM_SNP1K 99 /* Trebia SNP 1000 processor. */ -+#define EM_ST200 100 /* STMicroelectronics ST200 microcontroller. */ -+#define EM_IP2K 101 /* Ubicom IP2xxx microcontroller family. */ -+#define EM_MAX 102 /* MAX Processor. */ -+#define EM_CR 103 /* National Semiconductor CompactRISC -+ microprocessor. */ -+#define EM_F2MC16 104 /* Fujitsu F2MC16. */ -+#define EM_MSP430 105 /* Texas Instruments embedded microcontroller -+ msp430. */ -+#define EM_BLACKFIN 106 /* Analog Devices Blackfin (DSP) processor. */ -+#define EM_SE_C33 107 /* S1C33 Family of Seiko Epson processors. */ -+#define EM_SEP 108 /* Sharp embedded microprocessor. */ -+#define EM_ARCA 109 /* Arca RISC Microprocessor. */ -+#define EM_UNICORE 110 /* Microprocessor series from PKU-Unity Ltd. -+ and MPRC of Peking University */ -+#define EM_AARCH64 183 /* AArch64 (64-bit ARM) */ -+ -+/* Non-standard or deprecated. */ -+#define EM_486 6 /* Intel i486. */ -+#define EM_MIPS_RS4_BE 10 /* MIPS R4000 Big-Endian */ -+#define EM_ALPHA_STD 41 /* Digital Alpha (standard value). */ -+#define EM_ALPHA 0x9026 /* Alpha (written in the absence of an ABI) */ -+ -+/* e_flags for EM_ARM */ -+#define EF_ARM_ABI_VERSION 0x05000000 /* ABI version 5 */ -+#define EF_ARM_ABIMASK 0xFF000000 -+#define EF_ARM_BE8 0x00800000 -+#define EF_ARM_ABI_FLOAT_HARD 0x00000400 /* ABI version 5 and later */ -+#define EF_ARM_ABI_FLOAT_SOFT 0x00000200 /* ABI version 5 and later */ -+ -+/* Special section indexes. */ -+#define SHN_UNDEF 0 /* Undefined, missing, irrelevant. */ -+#define SHN_LORESERVE 0xff00 /* First of reserved range. */ -+#define SHN_LOPROC 0xff00 /* First processor-specific. */ -+#define SHN_HIPROC 0xff1f /* Last processor-specific. */ -+#define SHN_LOOS 0xff20 /* First operating system-specific. */ -+#define SHN_HIOS 0xff3f /* Last operating system-specific. */ -+#define SHN_ABS 0xfff1 /* Absolute values. */ -+#define SHN_COMMON 0xfff2 /* Common data. */ -+#define SHN_XINDEX 0xffff /* Escape -- index stored elsewhere. */ -+#define SHN_HIRESERVE 0xffff /* Last of reserved range. */ -+ -+/* sh_type */ -+#define SHT_NULL 0 /* inactive */ -+#define SHT_PROGBITS 1 /* program defined information */ -+#define SHT_SYMTAB 2 /* symbol table section */ -+#define SHT_STRTAB 3 /* string table section */ -+#define SHT_RELA 4 /* relocation section with addends */ -+#define SHT_HASH 5 /* symbol hash table section */ -+#define SHT_DYNAMIC 6 /* dynamic section */ -+#define SHT_NOTE 7 /* note section */ -+#define SHT_NOBITS 8 /* no space section */ -+#define SHT_REL 9 /* relocation section - no addends */ -+#define SHT_SHLIB 10 /* reserved - purpose unknown */ -+#define SHT_DYNSYM 11 /* dynamic symbol table section */ -+#define SHT_INIT_ARRAY 14 /* Initialization function pointers. */ -+#define SHT_FINI_ARRAY 15 /* Termination function pointers. */ -+#define SHT_PREINIT_ARRAY 16 /* Pre-initialization function ptrs. */ -+#define SHT_GROUP 17 /* Section group. */ -+#define SHT_SYMTAB_SHNDX 18 /* Section indexes (see SHN_XINDEX). */ -+#define SHT_LOOS 0x60000000 /* First of OS specific semantics */ -+#define SHT_LOSUNW 0x6ffffff4 -+#define SHT_SUNW_dof 0x6ffffff4 -+#define SHT_SUNW_cap 0x6ffffff5 -+#define SHT_SUNW_SIGNATURE 0x6ffffff6 -+#define SHT_GNU_HASH 0x6ffffff6 -+#define SHT_GNU_LIBLIST 0x6ffffff7 -+#define SHT_SUNW_ANNOTATE 0x6ffffff7 -+#define SHT_SUNW_DEBUGSTR 0x6ffffff8 -+#define SHT_SUNW_DEBUG 0x6ffffff9 -+#define SHT_SUNW_move 0x6ffffffa -+#define SHT_SUNW_COMDAT 0x6ffffffb -+#define SHT_SUNW_syminfo 0x6ffffffc -+#define SHT_SUNW_verdef 0x6ffffffd -+#define SHT_GNU_verdef 0x6ffffffd /* Symbol versions provided */ -+#define SHT_SUNW_verneed 0x6ffffffe -+#define SHT_GNU_verneed 0x6ffffffe /* Symbol versions required */ -+#define SHT_SUNW_versym 0x6fffffff -+#define SHT_GNU_versym 0x6fffffff /* Symbol version table */ -+#define SHT_HISUNW 0x6fffffff -+#define SHT_HIOS 0x6fffffff /* Last of OS specific semantics */ -+#define SHT_LOPROC 0x70000000 /* reserved range for processor */ -+#define SHT_AMD64_UNWIND 0x70000001 /* unwind information */ -+#define SHT_ARM_EXIDX 0x70000001 /* Exception index table. */ -+#define SHT_ARM_PREEMPTMAP 0x70000002 /* BPABI DLL dynamic linking -+ pre-emption map. */ -+#define SHT_ARM_ATTRIBUTES 0x70000003 /* Object file compatibility -+ attributes. */ -+#define SHT_ARM_DEBUGOVERLAY 0x70000004 /* See DBGOVL for details. */ -+#define SHT_ARM_OVERLAYSECTION 0x70000005 /* See DBGOVL for details. */ -+#define SHT_MIPS_REGINFO 0x70000006 -+#define SHT_MIPS_OPTIONS 0x7000000d -+#define SHT_MIPS_DWARF 0x7000001e /* MIPS gcc uses MIPS_DWARF */ -+#define SHT_HIPROC 0x7fffffff /* specific section header types */ -+#define SHT_LOUSER 0x80000000 /* reserved range for application */ -+#define SHT_HIUSER 0xffffffff /* specific indexes */ -+ -+/* Flags for sh_flags. */ -+#define SHF_WRITE 0x1 /* Section contains writable data. */ -+#define SHF_ALLOC 0x2 /* Section occupies memory. */ -+#define SHF_EXECINSTR 0x4 /* Section contains instructions. */ -+#define SHF_MERGE 0x10 /* Section may be merged. */ -+#define SHF_STRINGS 0x20 /* Section contains strings. */ -+#define SHF_INFO_LINK 0x40 /* sh_info holds section index. */ -+#define SHF_LINK_ORDER 0x80 /* Special ordering requirements. */ -+#define SHF_OS_NONCONFORMING 0x100 /* OS-specific processing required. */ -+#define SHF_GROUP 0x200 /* Member of section group. */ -+#define SHF_TLS 0x400 /* Section contains TLS data. */ -+#define SHF_MASKOS 0x0ff00000 /* OS-specific semantics. */ -+#define SHF_MASKPROC 0xf0000000 /* Processor-specific semantics. */ -+ -+/* Values for p_type. */ -+#define PT_NULL 0 /* Unused entry. */ -+#define PT_LOAD 1 /* Loadable segment. */ -+#define PT_DYNAMIC 2 /* Dynamic linking information segment. */ -+#define PT_INTERP 3 /* Pathname of interpreter. */ -+#define PT_NOTE 4 /* Auxiliary information. */ -+#define PT_SHLIB 5 /* Reserved (not used). */ -+#define PT_PHDR 6 /* Location of program header itself. */ -+#define PT_TLS 7 /* Thread local storage segment */ -+#define PT_LOOS 0x60000000 /* First OS-specific. */ -+#define PT_SUNW_UNWIND 0x6464e550 /* amd64 UNWIND program header */ -+#define PT_GNU_EH_FRAME 0x6474e550 -+#define PT_GNU_STACK 0x6474e551 -+#define PT_GNU_RELRO 0x6474e552 -+#define PT_LOSUNW 0x6ffffffa -+#define PT_SUNWBSS 0x6ffffffa /* Sun Specific segment */ -+#define PT_SUNWSTACK 0x6ffffffb /* describes the stack segment */ -+#define PT_SUNWDTRACE 0x6ffffffc /* private */ -+#define PT_SUNWCAP 0x6ffffffd /* hard/soft capabilities segment */ -+#define PT_HISUNW 0x6fffffff -+#define PT_HIOS 0x6fffffff /* Last OS-specific. */ -+#define PT_LOPROC 0x70000000 /* First processor-specific type. */ -+#define PT_ARM_EXIDX 0x70000001 /* .ARM.exidx segment */ -+#define PT_HIPROC 0x7fffffff /* Last processor-specific type. */ -+ -+/* Values for p_flags. */ -+#define PF_X 0x1 /* Executable. */ -+#define PF_W 0x2 /* Writable. */ -+#define PF_R 0x4 /* Readable. */ -+#define PF_MASKOS 0x0ff00000 /* Operating system-specific. */ -+#define PF_MASKPROC 0xf0000000 /* Processor-specific. */ -+ -+/* Extended program header index. */ -+#define PN_XNUM 0xffff -+ -+/* Values for d_tag. */ -+#define DT_NULL 0 /* Terminating entry. */ -+#define DT_NEEDED 1 /* String table offset of a needed shared -+ library. */ -+#define DT_PLTRELSZ 2 /* Total size in bytes of PLT relocations. */ -+#define DT_PLTGOT 3 /* Processor-dependent address. */ -+#define DT_HASH 4 /* Address of symbol hash table. */ -+#define DT_STRTAB 5 /* Address of string table. */ -+#define DT_SYMTAB 6 /* Address of symbol table. */ -+#define DT_RELA 7 /* Address of ElfNN_Rela relocations. */ -+#define DT_RELASZ 8 /* Total size of ElfNN_Rela relocations. */ -+#define DT_RELAENT 9 /* Size of each ElfNN_Rela relocation entry. */ -+#define DT_STRSZ 10 /* Size of string table. */ -+#define DT_SYMENT 11 /* Size of each symbol table entry. */ -+#define DT_INIT 12 /* Address of initialization function. */ -+#define DT_FINI 13 /* Address of finalization function. */ -+#define DT_SONAME 14 /* String table offset of shared object -+ name. */ -+#define DT_RPATH 15 /* String table offset of library path. [sup] */ -+#define DT_SYMBOLIC 16 /* Indicates "symbolic" linking. [sup] */ -+#define DT_REL 17 /* Address of ElfNN_Rel relocations. */ -+#define DT_RELSZ 18 /* Total size of ElfNN_Rel relocations. */ -+#define DT_RELENT 19 /* Size of each ElfNN_Rel relocation. */ -+#define DT_PLTREL 20 /* Type of relocation used for PLT. */ -+#define DT_DEBUG 21 /* Reserved (not used). */ -+#define DT_TEXTREL 22 /* Indicates there may be relocations in -+ non-writable segments. [sup] */ -+#define DT_JMPREL 23 /* Address of PLT relocations. */ -+#define DT_BIND_NOW 24 /* [sup] */ -+#define DT_INIT_ARRAY 25 /* Address of the array of pointers to -+ initialization functions */ -+#define DT_FINI_ARRAY 26 /* Address of the array of pointers to -+ termination functions */ -+#define DT_INIT_ARRAYSZ 27 /* Size in bytes of the array of -+ initialization functions. */ -+#define DT_FINI_ARRAYSZ 28 /* Size in bytes of the array of -+ termination functions. */ -+#define DT_RUNPATH 29 /* String table offset of a null-terminated -+ library search path string. */ -+#define DT_FLAGS 30 /* Object specific flag values. */ -+#define DT_ENCODING 32 /* Values greater than or equal to DT_ENCODING -+ and less than DT_LOOS follow the rules for -+ the interpretation of the d_un union -+ as follows: even == 'd_ptr', odd == 'd_val' -+ or none */ -+#define DT_PREINIT_ARRAY 32 /* Address of the array of pointers to -+ pre-initialization functions. */ -+#define DT_PREINIT_ARRAYSZ 33 /* Size in bytes of the array of -+ pre-initialization functions. */ -+#define DT_MAXPOSTAGS 34 /* number of positive tags */ -+#define DT_LOOS 0x6000000d /* First OS-specific */ -+#define DT_SUNW_AUXILIARY 0x6000000d /* symbol auxiliary name */ -+#define DT_SUNW_RTLDINF 0x6000000e /* ld.so.1 info (private) */ -+#define DT_SUNW_FILTER 0x6000000f /* symbol filter name */ -+#define DT_SUNW_CAP 0x60000010 /* hardware/software */ -+#define DT_HIOS 0x6ffff000 /* Last OS-specific */ -+ -+/* -+ * DT_* entries which fall between DT_VALRNGHI & DT_VALRNGLO use the -+ * Dyn.d_un.d_val field of the Elf*_Dyn structure. -+ */ -+#define DT_VALRNGLO 0x6ffffd00 -+#define DT_CHECKSUM 0x6ffffdf8 /* elf checksum */ -+#define DT_PLTPADSZ 0x6ffffdf9 /* pltpadding size */ -+#define DT_MOVEENT 0x6ffffdfa /* move table entry size */ -+#define DT_MOVESZ 0x6ffffdfb /* move table size */ -+#define DT_FEATURE_1 0x6ffffdfc /* feature holder */ -+#define DT_POSFLAG_1 0x6ffffdfd /* flags for DT_* entries, effecting */ -+ /* the following DT_* entry. */ -+ /* See DF_P1_* definitions */ -+#define DT_SYMINSZ 0x6ffffdfe /* syminfo table size (in bytes) */ -+#define DT_SYMINENT 0x6ffffdff /* syminfo entry size (in bytes) */ -+#define DT_VALRNGHI 0x6ffffdff -+ -+/* -+ * DT_* entries which fall between DT_ADDRRNGHI & DT_ADDRRNGLO use the -+ * Dyn.d_un.d_ptr field of the Elf*_Dyn structure. -+ * -+ * If any adjustment is made to the ELF object after it has been -+ * built, these entries will need to be adjusted. -+ */ -+#define DT_ADDRRNGLO 0x6ffffe00 -+#define DT_GNU_HASH 0x6ffffef5 /* GNU-style hash table */ -+#define DT_CONFIG 0x6ffffefa /* configuration information */ -+#define DT_DEPAUDIT 0x6ffffefb /* dependency auditing */ -+#define DT_AUDIT 0x6ffffefc /* object auditing */ -+#define DT_PLTPAD 0x6ffffefd /* pltpadding (sparcv9) */ -+#define DT_MOVETAB 0x6ffffefe /* move table */ -+#define DT_SYMINFO 0x6ffffeff /* syminfo table */ -+#define DT_ADDRRNGHI 0x6ffffeff -+ -+#define DT_VERSYM 0x6ffffff0 /* Address of versym section. */ -+#define DT_RELACOUNT 0x6ffffff9 /* number of RELATIVE relocations */ -+#define DT_RELCOUNT 0x6ffffffa /* number of RELATIVE relocations */ -+#define DT_FLAGS_1 0x6ffffffb /* state flags - see DF_1_* defs */ -+#define DT_VERDEF 0x6ffffffc /* Address of verdef section. */ -+#define DT_VERDEFNUM 0x6ffffffd /* Number of elems in verdef section */ -+#define DT_VERNEED 0x6ffffffe /* Address of verneed section. */ -+#define DT_VERNEEDNUM 0x6fffffff /* Number of elems in verneed section */ -+ -+#define DT_LOPROC 0x70000000 /* First processor-specific type. */ -+#define DT_DEPRECATED_SPARC_REGISTER 0x7000001 -+#define DT_AUXILIARY 0x7ffffffd /* shared library auxiliary name */ -+#define DT_USED 0x7ffffffe /* ignored - same as needed */ -+#define DT_FILTER 0x7fffffff /* shared library filter name */ -+#define DT_HIPROC 0x7fffffff /* Last processor-specific type. */ -+ -+/* Values for DT_FLAGS */ -+#define DF_ORIGIN 0x0001 /* Indicates that the object being loaded may -+ make reference to the $ORIGIN substitution -+ string */ -+#define DF_SYMBOLIC 0x0002 /* Indicates "symbolic" linking. */ -+#define DF_TEXTREL 0x0004 /* Indicates there may be relocations in -+ non-writable segments. */ -+#define DF_BIND_NOW 0x0008 /* Indicates that the dynamic linker should -+ process all relocations for the object -+ containing this entry before transferring -+ control to the program. */ -+#define DF_STATIC_TLS 0x0010 /* Indicates that the shared object or -+ executable contains code using a static -+ thread-local storage scheme. */ -+ -+/* Values for DT_FLAGS_1 */ -+#define DF_1_BIND_NOW 0x00000001 /* Same as DF_BIND_NOW */ -+#define DF_1_GLOBAL 0x00000002 /* Set the RTLD_GLOBAL for object */ -+#define DF_1_NODELETE 0x00000008 /* Set the RTLD_NODELETE for object */ -+#define DF_1_LOADFLTR 0x00000010 /* Immediate loading of filtees */ -+#define DF_1_NOOPEN 0x00000040 /* Do not allow loading on dlopen() */ -+#define DF_1_ORIGIN 0x00000080 /* Process $ORIGIN */ -+#define DF_1_INTERPOSE 0x00000400 /* Interpose all objects but main */ -+#define DF_1_NODEFLIB 0x00000800 /* Do not search default paths */ -+ -+/* Values for n_type. Used in core files. */ -+#define NT_PRSTATUS 1 /* Process status. */ -+#define NT_FPREGSET 2 /* Floating point registers. */ -+#define NT_PRPSINFO 3 /* Process state info. */ -+#define NT_THRMISC 7 /* Thread miscellaneous info. */ -+#define NT_PROCSTAT_PROC 8 /* Procstat proc data. */ -+#define NT_PROCSTAT_FILES 9 /* Procstat files data. */ -+#define NT_PROCSTAT_VMMAP 10 /* Procstat vmmap data. */ -+#define NT_PROCSTAT_GROUPS 11 /* Procstat groups data. */ -+#define NT_PROCSTAT_UMASK 12 /* Procstat umask data. */ -+#define NT_PROCSTAT_RLIMIT 13 /* Procstat rlimit data. */ -+#define NT_PROCSTAT_OSREL 14 /* Procstat osreldate data. */ -+#define NT_PROCSTAT_PSSTRINGS 15 /* Procstat ps_strings data. */ -+#define NT_PROCSTAT_AUXV 16 /* Procstat auxv data. */ -+ -+/* Symbol Binding - ELFNN_ST_BIND - st_info */ -+#define STB_LOCAL 0 /* Local symbol */ -+#define STB_GLOBAL 1 /* Global symbol */ -+#define STB_WEAK 2 /* like global - lower precedence */ -+#define STB_LOOS 10 /* Reserved range for operating system */ -+#define STB_HIOS 12 /* specific semantics. */ -+#define STB_LOPROC 13 /* reserved range for processor */ -+#define STB_HIPROC 15 /* specific semantics. */ -+ -+/* Symbol type - ELFNN_ST_TYPE - st_info */ -+#define STT_NOTYPE 0 /* Unspecified type. */ -+#define STT_OBJECT 1 /* Data object. */ -+#define STT_FUNC 2 /* Function. */ -+#define STT_SECTION 3 /* Section. */ -+#define STT_FILE 4 /* Source file. */ -+#define STT_COMMON 5 /* Uninitialized common block. */ -+#define STT_TLS 6 /* TLS object. */ -+#define STT_NUM 7 -+#define STT_LOOS 10 /* Reserved range for operating system */ -+#define STT_GNU_IFUNC 10 -+#define STT_HIOS 12 /* specific semantics. */ -+#define STT_LOPROC 13 /* reserved range for processor */ -+#define STT_HIPROC 15 /* specific semantics. */ -+ -+/* Symbol visibility - ELFNN_ST_VISIBILITY - st_other */ -+#define STV_DEFAULT 0x0 /* Default visibility (see binding). */ -+#define STV_INTERNAL 0x1 /* Special meaning in relocatable objects. */ -+#define STV_HIDDEN 0x2 /* Not visible. */ -+#define STV_PROTECTED 0x3 /* Visible but not preemptible. */ -+#define STV_EXPORTED 0x4 -+#define STV_SINGLETON 0x5 -+#define STV_ELIMINATE 0x6 -+ -+/* Special symbol table indexes. */ -+#define STN_UNDEF 0 /* Undefined symbol index. */ -+ -+/* Symbol versioning flags. */ -+#define VER_DEF_CURRENT 1 -+#define VER_DEF_IDX(x) VER_NDX(x) -+ -+#define VER_FLG_BASE 0x01 -+#define VER_FLG_WEAK 0x02 -+ -+#define VER_NEED_CURRENT 1 -+#define VER_NEED_WEAK (1u << 15) -+#define VER_NEED_HIDDEN VER_NDX_HIDDEN -+#define VER_NEED_IDX(x) VER_NDX(x) -+ -+#define VER_NDX_LOCAL 0 -+#define VER_NDX_GLOBAL 1 -+#define VER_NDX_GIVEN 2 -+ -+#define VER_NDX_HIDDEN (1u << 15) -+#define VER_NDX(x) ((x) & ~(1u << 15)) -+ -+#define CA_SUNW_NULL 0 -+#define CA_SUNW_HW_1 1 /* first hardware capabilities entry */ -+#define CA_SUNW_SF_1 2 /* first software capabilities entry */ -+ -+/* -+ * Syminfo flag values -+ */ -+#define SYMINFO_FLG_DIRECT 0x0001 /* symbol ref has direct association */ -+ /* to object containing defn. */ -+#define SYMINFO_FLG_PASSTHRU 0x0002 /* ignored - see SYMINFO_FLG_FILTER */ -+#define SYMINFO_FLG_COPY 0x0004 /* symbol is a copy-reloc */ -+#define SYMINFO_FLG_LAZYLOAD 0x0008 /* object containing defn should be */ -+ /* lazily-loaded */ -+#define SYMINFO_FLG_DIRECTBIND 0x0010 /* ref should be bound directly to */ -+ /* object containing defn. */ -+#define SYMINFO_FLG_NOEXTDIRECT 0x0020 /* don't let an external reference */ -+ /* directly bind to this symbol */ -+#define SYMINFO_FLG_FILTER 0x0002 /* symbol ref is associated to a */ -+#define SYMINFO_FLG_AUXILIARY 0x0040 /* standard or auxiliary filter */ -+ -+/* -+ * Syminfo.si_boundto values. -+ */ -+#define SYMINFO_BT_SELF 0xffff /* symbol bound to self */ -+#define SYMINFO_BT_PARENT 0xfffe /* symbol bound to parent */ -+#define SYMINFO_BT_NONE 0xfffd /* no special symbol binding */ -+#define SYMINFO_BT_EXTERN 0xfffc /* symbol defined as external */ -+#define SYMINFO_BT_LOWRESERVE 0xff00 /* beginning of reserved entries */ -+ -+/* -+ * Syminfo version values. -+ */ -+#define SYMINFO_NONE 0 /* Syminfo version */ -+#define SYMINFO_CURRENT 1 -+#define SYMINFO_NUM 2 -+ -+/* -+ * Relocation types. -+ * -+ * All machine architectures are defined here to allow tools on one to -+ * handle others. -+ */ -+ -+#define R_386_NONE 0 /* No relocation. */ -+#define R_386_32 1 /* Add symbol value. */ -+#define R_386_PC32 2 /* Add PC-relative symbol value. */ -+#define R_386_GOT32 3 /* Add PC-relative GOT offset. */ -+#define R_386_PLT32 4 /* Add PC-relative PLT offset. */ -+#define R_386_COPY 5 /* Copy data from shared object. */ -+#define R_386_GLOB_DAT 6 /* Set GOT entry to data address. */ -+#define R_386_JMP_SLOT 7 /* Set GOT entry to code address. */ -+#define R_386_RELATIVE 8 /* Add load address of shared object. */ -+#define R_386_GOTOFF 9 /* Add GOT-relative symbol address. */ -+#define R_386_GOTPC 10 /* Add PC-relative GOT table address. */ -+#define R_386_TLS_TPOFF 14 /* Negative offset in static TLS block */ -+#define R_386_TLS_IE 15 /* Absolute address of GOT for -ve static TLS */ -+#define R_386_TLS_GOTIE 16 /* GOT entry for negative static TLS block */ -+#define R_386_TLS_LE 17 /* Negative offset relative to static TLS */ -+#define R_386_TLS_GD 18 /* 32 bit offset to GOT (index,off) pair */ -+#define R_386_TLS_LDM 19 /* 32 bit offset to GOT (index,zero) pair */ -+#define R_386_TLS_GD_32 24 /* 32 bit offset to GOT (index,off) pair */ -+#define R_386_TLS_GD_PUSH 25 /* pushl instruction for Sun ABI GD sequence */ -+#define R_386_TLS_GD_CALL 26 /* call instruction for Sun ABI GD sequence */ -+#define R_386_TLS_GD_POP 27 /* popl instruction for Sun ABI GD sequence */ -+#define R_386_TLS_LDM_32 28 /* 32 bit offset to GOT (index,zero) pair */ -+#define R_386_TLS_LDM_PUSH 29 /* pushl instruction for Sun ABI LD sequence */ -+#define R_386_TLS_LDM_CALL 30 /* call instruction for Sun ABI LD sequence */ -+#define R_386_TLS_LDM_POP 31 /* popl instruction for Sun ABI LD sequence */ -+#define R_386_TLS_LDO_32 32 /* 32 bit offset from start of TLS block */ -+#define R_386_TLS_IE_32 33 /* 32 bit offset to GOT static TLS offset entry */ -+#define R_386_TLS_LE_32 34 /* 32 bit offset within static TLS block */ -+#define R_386_TLS_DTPMOD32 35 /* GOT entry containing TLS index */ -+#define R_386_TLS_DTPOFF32 36 /* GOT entry containing TLS offset */ -+#define R_386_TLS_TPOFF32 37 /* GOT entry of -ve static TLS offset */ -+#define R_386_IRELATIVE 42 /* PLT entry resolved indirectly at runtime */ -+ -+#define R_AARCH64_ABS64 257 -+#define R_AARCH64_GLOB_DAT 1025 /* Set GOT entry to data address. */ -+#define R_AARCH64_JUMP_SLOT 1026 /* Set GOT entry to code address. */ -+#define R_AARCH64_RELATIVE 1027 -+ -+#define R_ARM_NONE 0 /* No relocation. */ -+#define R_ARM_PC24 1 -+#define R_ARM_ABS32 2 -+#define R_ARM_REL32 3 -+#define R_ARM_PC13 4 -+#define R_ARM_ABS16 5 -+#define R_ARM_ABS12 6 -+#define R_ARM_THM_ABS5 7 -+#define R_ARM_ABS8 8 -+#define R_ARM_SBREL32 9 -+#define R_ARM_THM_PC22 10 -+#define R_ARM_THM_PC8 11 -+#define R_ARM_AMP_VCALL9 12 -+#define R_ARM_SWI24 13 -+#define R_ARM_THM_SWI8 14 -+#define R_ARM_XPC25 15 -+#define R_ARM_THM_XPC22 16 -+/* TLS relocations */ -+#define R_ARM_TLS_DTPMOD32 17 /* ID of module containing symbol */ -+#define R_ARM_TLS_DTPOFF32 18 /* Offset in TLS block */ -+#define R_ARM_TLS_TPOFF32 19 /* Offset in static TLS block */ -+#define R_ARM_COPY 20 /* Copy data from shared object. */ -+#define R_ARM_GLOB_DAT 21 /* Set GOT entry to data address. */ -+#define R_ARM_JUMP_SLOT 22 /* Set GOT entry to code address. */ -+#define R_ARM_RELATIVE 23 /* Add load address of shared object. */ -+#define R_ARM_GOTOFF 24 /* Add GOT-relative symbol address. */ -+#define R_ARM_GOTPC 25 /* Add PC-relative GOT table address. */ -+#define R_ARM_GOT32 26 /* Add PC-relative GOT offset. */ -+#define R_ARM_PLT32 27 /* Add PC-relative PLT offset. */ -+#define R_ARM_GNU_VTENTRY 100 -+#define R_ARM_GNU_VTINHERIT 101 -+#define R_ARM_RSBREL32 250 -+#define R_ARM_THM_RPC22 251 -+#define R_ARM_RREL32 252 -+#define R_ARM_RABS32 253 -+#define R_ARM_RPC24 254 -+#define R_ARM_RBASE 255 -+ -+/* Name Value Field Calculation */ -+#define R_IA_64_NONE 0 /* None */ -+#define R_IA_64_IMM14 0x21 /* immediate14 S + A */ -+#define R_IA_64_IMM22 0x22 /* immediate22 S + A */ -+#define R_IA_64_IMM64 0x23 /* immediate64 S + A */ -+#define R_IA_64_DIR32MSB 0x24 /* word32 MSB S + A */ -+#define R_IA_64_DIR32LSB 0x25 /* word32 LSB S + A */ -+#define R_IA_64_DIR64MSB 0x26 /* word64 MSB S + A */ -+#define R_IA_64_DIR64LSB 0x27 /* word64 LSB S + A */ -+#define R_IA_64_GPREL22 0x2a /* immediate22 @gprel(S + A) */ -+#define R_IA_64_GPREL64I 0x2b /* immediate64 @gprel(S + A) */ -+#define R_IA_64_GPREL32MSB 0x2c /* word32 MSB @gprel(S + A) */ -+#define R_IA_64_GPREL32LSB 0x2d /* word32 LSB @gprel(S + A) */ -+#define R_IA_64_GPREL64MSB 0x2e /* word64 MSB @gprel(S + A) */ -+#define R_IA_64_GPREL64LSB 0x2f /* word64 LSB @gprel(S + A) */ -+#define R_IA_64_LTOFF22 0x32 /* immediate22 @ltoff(S + A) */ -+#define R_IA_64_LTOFF64I 0x33 /* immediate64 @ltoff(S + A) */ -+#define R_IA_64_PLTOFF22 0x3a /* immediate22 @pltoff(S + A) */ -+#define R_IA_64_PLTOFF64I 0x3b /* immediate64 @pltoff(S + A) */ -+#define R_IA_64_PLTOFF64MSB 0x3e /* word64 MSB @pltoff(S + A) */ -+#define R_IA_64_PLTOFF64LSB 0x3f /* word64 LSB @pltoff(S + A) */ -+#define R_IA_64_FPTR64I 0x43 /* immediate64 @fptr(S + A) */ -+#define R_IA_64_FPTR32MSB 0x44 /* word32 MSB @fptr(S + A) */ -+#define R_IA_64_FPTR32LSB 0x45 /* word32 LSB @fptr(S + A) */ -+#define R_IA_64_FPTR64MSB 0x46 /* word64 MSB @fptr(S + A) */ -+#define R_IA_64_FPTR64LSB 0x47 /* word64 LSB @fptr(S + A) */ -+#define R_IA_64_PCREL60B 0x48 /* immediate60 form1 S + A - P */ -+#define R_IA_64_PCREL21B 0x49 /* immediate21 form1 S + A - P */ -+#define R_IA_64_PCREL21M 0x4a /* immediate21 form2 S + A - P */ -+#define R_IA_64_PCREL21F 0x4b /* immediate21 form3 S + A - P */ -+#define R_IA_64_PCREL32MSB 0x4c /* word32 MSB S + A - P */ -+#define R_IA_64_PCREL32LSB 0x4d /* word32 LSB S + A - P */ -+#define R_IA_64_PCREL64MSB 0x4e /* word64 MSB S + A - P */ -+#define R_IA_64_PCREL64LSB 0x4f /* word64 LSB S + A - P */ -+#define R_IA_64_LTOFF_FPTR22 0x52 /* immediate22 @ltoff(@fptr(S + A)) */ -+#define R_IA_64_LTOFF_FPTR64I 0x53 /* immediate64 @ltoff(@fptr(S + A)) */ -+#define R_IA_64_LTOFF_FPTR32MSB 0x54 /* word32 MSB @ltoff(@fptr(S + A)) */ -+#define R_IA_64_LTOFF_FPTR32LSB 0x55 /* word32 LSB @ltoff(@fptr(S + A)) */ -+#define R_IA_64_LTOFF_FPTR64MSB 0x56 /* word64 MSB @ltoff(@fptr(S + A)) */ -+#define R_IA_64_LTOFF_FPTR64LSB 0x57 /* word64 LSB @ltoff(@fptr(S + A)) */ -+#define R_IA_64_SEGREL32MSB 0x5c /* word32 MSB @segrel(S + A) */ -+#define R_IA_64_SEGREL32LSB 0x5d /* word32 LSB @segrel(S + A) */ -+#define R_IA_64_SEGREL64MSB 0x5e /* word64 MSB @segrel(S + A) */ -+#define R_IA_64_SEGREL64LSB 0x5f /* word64 LSB @segrel(S + A) */ -+#define R_IA_64_SECREL32MSB 0x64 /* word32 MSB @secrel(S + A) */ -+#define R_IA_64_SECREL32LSB 0x65 /* word32 LSB @secrel(S + A) */ -+#define R_IA_64_SECREL64MSB 0x66 /* word64 MSB @secrel(S + A) */ -+#define R_IA_64_SECREL64LSB 0x67 /* word64 LSB @secrel(S + A) */ -+#define R_IA_64_REL32MSB 0x6c /* word32 MSB BD + A */ -+#define R_IA_64_REL32LSB 0x6d /* word32 LSB BD + A */ -+#define R_IA_64_REL64MSB 0x6e /* word64 MSB BD + A */ -+#define R_IA_64_REL64LSB 0x6f /* word64 LSB BD + A */ -+#define R_IA_64_LTV32MSB 0x74 /* word32 MSB S + A */ -+#define R_IA_64_LTV32LSB 0x75 /* word32 LSB S + A */ -+#define R_IA_64_LTV64MSB 0x76 /* word64 MSB S + A */ -+#define R_IA_64_LTV64LSB 0x77 /* word64 LSB S + A */ -+#define R_IA_64_PCREL21BI 0x79 /* immediate21 form1 S + A - P */ -+#define R_IA_64_PCREL22 0x7a /* immediate22 S + A - P */ -+#define R_IA_64_PCREL64I 0x7b /* immediate64 S + A - P */ -+#define R_IA_64_IPLTMSB 0x80 /* function descriptor MSB special */ -+#define R_IA_64_IPLTLSB 0x81 /* function descriptor LSB speciaal */ -+#define R_IA_64_SUB 0x85 /* immediate64 A - S */ -+#define R_IA_64_LTOFF22X 0x86 /* immediate22 special */ -+#define R_IA_64_LDXMOV 0x87 /* immediate22 special */ -+#define R_IA_64_TPREL14 0x91 /* imm14 @tprel(S + A) */ -+#define R_IA_64_TPREL22 0x92 /* imm22 @tprel(S + A) */ -+#define R_IA_64_TPREL64I 0x93 /* imm64 @tprel(S + A) */ -+#define R_IA_64_TPREL64MSB 0x96 /* word64 MSB @tprel(S + A) */ -+#define R_IA_64_TPREL64LSB 0x97 /* word64 LSB @tprel(S + A) */ -+#define R_IA_64_LTOFF_TPREL22 0x9a /* imm22 @ltoff(@tprel(S+A)) */ -+#define R_IA_64_DTPMOD64MSB 0xa6 /* word64 MSB @dtpmod(S + A) */ -+#define R_IA_64_DTPMOD64LSB 0xa7 /* word64 LSB @dtpmod(S + A) */ -+#define R_IA_64_LTOFF_DTPMOD22 0xaa /* imm22 @ltoff(@dtpmod(S+A)) */ -+#define R_IA_64_DTPREL14 0xb1 /* imm14 @dtprel(S + A) */ -+#define R_IA_64_DTPREL22 0xb2 /* imm22 @dtprel(S + A) */ -+#define R_IA_64_DTPREL64I 0xb3 /* imm64 @dtprel(S + A) */ -+#define R_IA_64_DTPREL32MSB 0xb4 /* word32 MSB @dtprel(S + A) */ -+#define R_IA_64_DTPREL32LSB 0xb5 /* word32 LSB @dtprel(S + A) */ -+#define R_IA_64_DTPREL64MSB 0xb6 /* word64 MSB @dtprel(S + A) */ -+#define R_IA_64_DTPREL64LSB 0xb7 /* word64 LSB @dtprel(S + A) */ -+#define R_IA_64_LTOFF_DTPREL22 0xba /* imm22 @ltoff(@dtprel(S+A)) */ -+ -+#define R_MIPS_NONE 0 /* No reloc */ -+#define R_MIPS_16 1 /* Direct 16 bit */ -+#define R_MIPS_32 2 /* Direct 32 bit */ -+#define R_MIPS_REL32 3 /* PC relative 32 bit */ -+#define R_MIPS_26 4 /* Direct 26 bit shifted */ -+#define R_MIPS_HI16 5 /* High 16 bit */ -+#define R_MIPS_LO16 6 /* Low 16 bit */ -+#define R_MIPS_GPREL16 7 /* GP relative 16 bit */ -+#define R_MIPS_LITERAL 8 /* 16 bit literal entry */ -+#define R_MIPS_GOT16 9 /* 16 bit GOT entry */ -+#define R_MIPS_PC16 10 /* PC relative 16 bit */ -+#define R_MIPS_CALL16 11 /* 16 bit GOT entry for function */ -+#define R_MIPS_GPREL32 12 /* GP relative 32 bit */ -+#define R_MIPS_64 18 /* Direct 64 bit */ -+#define R_MIPS_GOTHI16 21 /* GOT HI 16 bit */ -+#define R_MIPS_GOTLO16 22 /* GOT LO 16 bit */ -+#define R_MIPS_CALLHI16 30 /* upper 16 bit GOT entry for function */ -+#define R_MIPS_CALLLO16 31 /* lower 16 bit GOT entry for function */ -+ -+#define R_PPC_NONE 0 /* No relocation. */ -+#define R_PPC_ADDR32 1 -+#define R_PPC_ADDR24 2 -+#define R_PPC_ADDR16 3 -+#define R_PPC_ADDR16_LO 4 -+#define R_PPC_ADDR16_HI 5 -+#define R_PPC_ADDR16_HA 6 -+#define R_PPC_ADDR14 7 -+#define R_PPC_ADDR14_BRTAKEN 8 -+#define R_PPC_ADDR14_BRNTAKEN 9 -+#define R_PPC_REL24 10 -+#define R_PPC_REL14 11 -+#define R_PPC_REL14_BRTAKEN 12 -+#define R_PPC_REL14_BRNTAKEN 13 -+#define R_PPC_GOT16 14 -+#define R_PPC_GOT16_LO 15 -+#define R_PPC_GOT16_HI 16 -+#define R_PPC_GOT16_HA 17 -+#define R_PPC_PLTREL24 18 -+#define R_PPC_COPY 19 -+#define R_PPC_GLOB_DAT 20 -+#define R_PPC_JMP_SLOT 21 -+#define R_PPC_RELATIVE 22 -+#define R_PPC_LOCAL24PC 23 -+#define R_PPC_UADDR32 24 -+#define R_PPC_UADDR16 25 -+#define R_PPC_REL32 26 -+#define R_PPC_PLT32 27 -+#define R_PPC_PLTREL32 28 -+#define R_PPC_PLT16_LO 29 -+#define R_PPC_PLT16_HI 30 -+#define R_PPC_PLT16_HA 31 -+#define R_PPC_SDAREL16 32 -+#define R_PPC_SECTOFF 33 -+#define R_PPC_SECTOFF_LO 34 -+#define R_PPC_SECTOFF_HI 35 -+#define R_PPC_SECTOFF_HA 36 -+ -+/* -+ * 64-bit relocations -+ */ -+#define R_PPC64_ADDR64 38 -+#define R_PPC64_ADDR16_HIGHER 39 -+#define R_PPC64_ADDR16_HIGHERA 40 -+#define R_PPC64_ADDR16_HIGHEST 41 -+#define R_PPC64_ADDR16_HIGHESTA 42 -+#define R_PPC64_UADDR64 43 -+#define R_PPC64_REL64 44 -+#define R_PPC64_PLT64 45 -+#define R_PPC64_PLTREL64 46 -+#define R_PPC64_TOC16 47 -+#define R_PPC64_TOC16_LO 48 -+#define R_PPC64_TOC16_HI 49 -+#define R_PPC64_TOC16_HA 50 -+#define R_PPC64_TOC 51 -+#define R_PPC64_DTPMOD64 68 -+#define R_PPC64_TPREL64 73 -+#define R_PPC64_DTPREL64 78 -+ -+/* -+ * TLS relocations -+ */ -+#define R_PPC_TLS 67 -+#define R_PPC_DTPMOD32 68 -+#define R_PPC_TPREL16 69 -+#define R_PPC_TPREL16_LO 70 -+#define R_PPC_TPREL16_HI 71 -+#define R_PPC_TPREL16_HA 72 -+#define R_PPC_TPREL32 73 -+#define R_PPC_DTPREL16 74 -+#define R_PPC_DTPREL16_LO 75 -+#define R_PPC_DTPREL16_HI 76 -+#define R_PPC_DTPREL16_HA 77 -+#define R_PPC_DTPREL32 78 -+#define R_PPC_GOT_TLSGD16 79 -+#define R_PPC_GOT_TLSGD16_LO 80 -+#define R_PPC_GOT_TLSGD16_HI 81 -+#define R_PPC_GOT_TLSGD16_HA 82 -+#define R_PPC_GOT_TLSLD16 83 -+#define R_PPC_GOT_TLSLD16_LO 84 -+#define R_PPC_GOT_TLSLD16_HI 85 -+#define R_PPC_GOT_TLSLD16_HA 86 -+#define R_PPC_GOT_TPREL16 87 -+#define R_PPC_GOT_TPREL16_LO 88 -+#define R_PPC_GOT_TPREL16_HI 89 -+#define R_PPC_GOT_TPREL16_HA 90 -+ -+/* -+ * The remaining relocs are from the Embedded ELF ABI, and are not in the -+ * SVR4 ELF ABI. -+ */ -+ -+#define R_PPC_EMB_NADDR32 101 -+#define R_PPC_EMB_NADDR16 102 -+#define R_PPC_EMB_NADDR16_LO 103 -+#define R_PPC_EMB_NADDR16_HI 104 -+#define R_PPC_EMB_NADDR16_HA 105 -+#define R_PPC_EMB_SDAI16 106 -+#define R_PPC_EMB_SDA2I16 107 -+#define R_PPC_EMB_SDA2REL 108 -+#define R_PPC_EMB_SDA21 109 -+#define R_PPC_EMB_MRKREF 110 -+#define R_PPC_EMB_RELSEC16 111 -+#define R_PPC_EMB_RELST_LO 112 -+#define R_PPC_EMB_RELST_HI 113 -+#define R_PPC_EMB_RELST_HA 114 -+#define R_PPC_EMB_BIT_FLD 115 -+#define R_PPC_EMB_RELSDA 116 -+ -+#define R_SPARC_NONE 0 -+#define R_SPARC_8 1 -+#define R_SPARC_16 2 -+#define R_SPARC_32 3 -+#define R_SPARC_DISP8 4 -+#define R_SPARC_DISP16 5 -+#define R_SPARC_DISP32 6 -+#define R_SPARC_WDISP30 7 -+#define R_SPARC_WDISP22 8 -+#define R_SPARC_HI22 9 -+#define R_SPARC_22 10 -+#define R_SPARC_13 11 -+#define R_SPARC_LO10 12 -+#define R_SPARC_GOT10 13 -+#define R_SPARC_GOT13 14 -+#define R_SPARC_GOT22 15 -+#define R_SPARC_PC10 16 -+#define R_SPARC_PC22 17 -+#define R_SPARC_WPLT30 18 -+#define R_SPARC_COPY 19 -+#define R_SPARC_GLOB_DAT 20 -+#define R_SPARC_JMP_SLOT 21 -+#define R_SPARC_RELATIVE 22 -+#define R_SPARC_UA32 23 -+#define R_SPARC_PLT32 24 -+#define R_SPARC_HIPLT22 25 -+#define R_SPARC_LOPLT10 26 -+#define R_SPARC_PCPLT32 27 -+#define R_SPARC_PCPLT22 28 -+#define R_SPARC_PCPLT10 29 -+#define R_SPARC_10 30 -+#define R_SPARC_11 31 -+#define R_SPARC_64 32 -+#define R_SPARC_OLO10 33 -+#define R_SPARC_HH22 34 -+#define R_SPARC_HM10 35 -+#define R_SPARC_LM22 36 -+#define R_SPARC_PC_HH22 37 -+#define R_SPARC_PC_HM10 38 -+#define R_SPARC_PC_LM22 39 -+#define R_SPARC_WDISP16 40 -+#define R_SPARC_WDISP19 41 -+#define R_SPARC_GLOB_JMP 42 -+#define R_SPARC_7 43 -+#define R_SPARC_5 44 -+#define R_SPARC_6 45 -+#define R_SPARC_DISP64 46 -+#define R_SPARC_PLT64 47 -+#define R_SPARC_HIX22 48 -+#define R_SPARC_LOX10 49 -+#define R_SPARC_H44 50 -+#define R_SPARC_M44 51 -+#define R_SPARC_L44 52 -+#define R_SPARC_REGISTER 53 -+#define R_SPARC_UA64 54 -+#define R_SPARC_UA16 55 -+#define R_SPARC_TLS_GD_HI22 56 -+#define R_SPARC_TLS_GD_LO10 57 -+#define R_SPARC_TLS_GD_ADD 58 -+#define R_SPARC_TLS_GD_CALL 59 -+#define R_SPARC_TLS_LDM_HI22 60 -+#define R_SPARC_TLS_LDM_LO10 61 -+#define R_SPARC_TLS_LDM_ADD 62 -+#define R_SPARC_TLS_LDM_CALL 63 -+#define R_SPARC_TLS_LDO_HIX22 64 -+#define R_SPARC_TLS_LDO_LOX10 65 -+#define R_SPARC_TLS_LDO_ADD 66 -+#define R_SPARC_TLS_IE_HI22 67 -+#define R_SPARC_TLS_IE_LO10 68 -+#define R_SPARC_TLS_IE_LD 69 -+#define R_SPARC_TLS_IE_LDX 70 -+#define R_SPARC_TLS_IE_ADD 71 -+#define R_SPARC_TLS_LE_HIX22 72 -+#define R_SPARC_TLS_LE_LOX10 73 -+#define R_SPARC_TLS_DTPMOD32 74 -+#define R_SPARC_TLS_DTPMOD64 75 -+#define R_SPARC_TLS_DTPOFF32 76 -+#define R_SPARC_TLS_DTPOFF64 77 -+#define R_SPARC_TLS_TPOFF32 78 -+#define R_SPARC_TLS_TPOFF64 79 -+ -+#define R_X86_64_NONE 0 /* No relocation. */ -+#define R_X86_64_64 1 /* Add 64 bit symbol value. */ -+#define R_X86_64_PC32 2 /* PC-relative 32 bit signed sym value. */ -+#define R_X86_64_GOT32 3 /* PC-relative 32 bit GOT offset. */ -+#define R_X86_64_PLT32 4 /* PC-relative 32 bit PLT offset. */ -+#define R_X86_64_COPY 5 /* Copy data from shared object. */ -+#define R_X86_64_GLOB_DAT 6 /* Set GOT entry to data address. */ -+#define R_X86_64_JMP_SLOT 7 /* Set GOT entry to code address. */ -+#define R_X86_64_RELATIVE 8 /* Add load address of shared object. */ -+#define R_X86_64_GOTPCREL 9 /* Add 32 bit signed pcrel offset to GOT. */ -+#define R_X86_64_32 10 /* Add 32 bit zero extended symbol value */ -+#define R_X86_64_32S 11 /* Add 32 bit sign extended symbol value */ -+#define R_X86_64_16 12 /* Add 16 bit zero extended symbol value */ -+#define R_X86_64_PC16 13 /* Add 16 bit signed extended pc relative symbol value */ -+#define R_X86_64_8 14 /* Add 8 bit zero extended symbol value */ -+#define R_X86_64_PC8 15 /* Add 8 bit signed extended pc relative symbol value */ -+#define R_X86_64_DTPMOD64 16 /* ID of module containing symbol */ -+#define R_X86_64_DTPOFF64 17 /* Offset in TLS block */ -+#define R_X86_64_TPOFF64 18 /* Offset in static TLS block */ -+#define R_X86_64_TLSGD 19 /* PC relative offset to GD GOT entry */ -+#define R_X86_64_TLSLD 20 /* PC relative offset to LD GOT entry */ -+#define R_X86_64_DTPOFF32 21 /* Offset in TLS block */ -+#define R_X86_64_GOTTPOFF 22 /* PC relative offset to IE GOT entry */ -+#define R_X86_64_TPOFF32 23 /* Offset in static TLS block */ -+#define R_X86_64_IRELATIVE 37 -+ -+#endif /* !_SYS_ELF_COMMON_H_ */ -diff --git a/ta/remoteproc/include/elf_parser.h b/ta/remoteproc/include/elf_parser.h -new file mode 100644 -index 000000000..5950db971 ---- /dev/null -+++ b/ta/remoteproc/include/elf_parser.h -@@ -0,0 +1,59 @@ -+/* SPDX-License-Identifier: BSD-2-Clause */ -+/* -+ * Copyright (C) 2020, STMicroelectronics - All Rights Reserved -+ */ -+ -+#ifndef ELF_PARSER -+#define ELF_PARSER -+ -+#include -+#include -+#include -+ -+/** -+ * struct resource_table - firmware resource table header -+ * @ver: version number -+ * @num: number of resource entries -+ * @reserved: reserved (must be zero) -+ * @offset: array of offsets pointing at the various resource entries -+ * -+ * A resource table is essentially a list of system resources required -+ * by the remote processor. It may also include configuration entries. -+ * If needed, the remote processor firmware should contain this table -+ * as a dedicated ".resource_table" ELF section. -+ * -+ * This structure shall be consistent with the Linux kernel structure -+ * definition from include/linux/remoteproc.h. -+ */ -+struct resource_table { -+ uint32_t ver; -+ uint32_t num; -+ uint32_t reserved[2]; -+ uint32_t offset[]; -+} __packed; -+ -+struct fw_elf32 { -+ uintptr_t e_entry; -+ uintptr_t e_phoff; -+ uintptr_t e_shoff; -+ uint32_t e_phnum; -+ uint32_t e_shnum; -+ uint32_t e_phentsize; -+ uint32_t e_shentsize; -+ -+ Elf32_Phdr *phdr; -+ Elf32_Shdr *shdr; -+}; -+ -+TEE_Result e32_parse_ehdr(uint8_t *fw, size_t size); -+TEE_Result e32_parser_load_elf_image(uint8_t *fw, size_t fw_size, -+ TEE_Result (*load_seg)(uint8_t *src, -+ uint32_t size, -+ uint32_t da, -+ uint32_t mem_size, -+ void *priv), -+ void *priv_data); -+int e32_parser_find_rsc_table(uint8_t *fw, size_t fw_size, Elf32_Addr *rsc_addr, -+ Elf32_Word *rsc_size); -+ -+#endif /*ELF_PARSER*/ -diff --git a/ta/remoteproc/include/ta_remoteproc.h b/ta/remoteproc/include/ta_remoteproc.h -new file mode 100644 -index 000000000..a51ba5f70 ---- /dev/null -+++ b/ta/remoteproc/include/ta_remoteproc.h -@@ -0,0 +1,65 @@ -+/* SPDX-License-Identifier: BSD-2-Clause */ -+/* -+ * Copyright (C) 2020, STMicroelectronics - All Rights Reserved -+ */ -+ -+#ifndef TA_RPROC_FW_H -+#define TA_RPROC_FW_H -+ -+/* -+ * This UUID is generated with uuidgen -+ * the ITU-T UUID generator at http://www.itu.int/ITU-T/asn1/uuid.html -+ */ -+#define TA_REMOTEPROC_UUID \ -+ { 0x80a4c275, 0x0a47, 0x4905, \ -+ { 0x82, 0x85, 0x14, 0x86, 0xa9, 0x77, 0x1a, 0x08} } -+ -+/* The function IDs implemented in this TA */ -+ -+/* -+ * Authentication of the firmware and load in the remote processor memory. -+ * -+ * [in] params[0].value.a: unique 32bit identifier of the firmware -+ * [in] params[1].memref: buffer containing the image of the firmware -+ */ -+#define TA_RPROC_FW_CMD_LOAD_FW 1 -+ -+/* -+ * Start the remote processor. -+ * -+ * [in] params[0].value.a: unique 32bit identifier of the firmware -+ */ -+#define TA_RPROC_FW_CMD_START_FW 2 -+ -+/* -+ * Stop the remote processor. -+ * -+ * [in] params[0].value.a: unique 32bit identifier of the firmware -+ */ -+#define TA_RPROC_FW_CMD_STOP_FW 3 -+ -+/* -+ * Return the physical address of the resource table, or 0 if not found -+ * No check is done to verify that the address returned is accessible by the -+ * non secure world. If the resource table is loaded in a protected memory, -+ * then accesses from non-secure world will likely fail. -+ * -+ * [in] params[0].value.a: unique 32bit identifier of the firmware -+ * [out] params[1].value.a: 32bit LSB resource table memory address -+ * [out] params[1].value.b: 32bit MSB resource table memory address -+ * [out] params[2].value.a: 32bit LSB resource table memory size -+ * [out] params[2].value.b: 32bit MSB resource table memory size -+ */ -+#define TA_RPROC_FW_CMD_GET_RSC_TABLE 4 -+ -+/* -+ * Get remote processor firmware core dump. If found, return either -+ * TEE_SUCCESS on successful completion or TEE_ERROR_SHORT_BUFFER if output -+ * buffer is too short to store the core dump. -+ * -+ * [in] params[0].value.a: unique 32bit identifier of the firmware -+ * [out] params[1].memref: Core dump, if found -+ */ -+#define TA_RPROC_FW_CMD_GET_COREDUMP 5 -+ -+#endif /*TA_RPROC_FW_H*/ -diff --git a/ta/remoteproc/include/user_ta_header_defines.h b/ta/remoteproc/include/user_ta_header_defines.h -new file mode 100644 -index 000000000..c2ef1b21b ---- /dev/null -+++ b/ta/remoteproc/include/user_ta_header_defines.h -@@ -0,0 +1,29 @@ -+/* SPDX-License-Identifier: BSD-2-Clause */ -+/* -+ * Copyright (C) 2020, STMicroelectronics - All Rights Reserved -+ */ -+ -+#ifndef USER_TA_HEADER_DEFINES_H -+#define USER_TA_HEADER_DEFINES_H -+ -+#include -+ -+#define TA_UUID TA_REMOTEPROC_UUID -+ -+#define TA_FLAGS (TA_FLAG_DEVICE_ENUM | \ -+ TA_FLAG_SINGLE_INSTANCE | \ -+ TA_FLAG_INSTANCE_KEEP_ALIVE) -+ -+/* Provisioned stack size */ -+#define TA_STACK_SIZE (4 * 1024) -+ -+/* Provisioned heap size for TEE_Malloc() and friends */ -+#define TA_DATA_SIZE (4 * 1024) -+ -+/* The gpd.ta.version property */ -+#define TA_VERSION "1.0" -+ -+/* The gpd.ta.description property */ -+#define TA_DESCRIPTION "remote processor firmware management" -+ -+#endif /* USER_TA_HEADER_DEFINES_H */ -diff --git a/ta/remoteproc/remoteproc_core.c b/ta/remoteproc/remoteproc_core.c -new file mode 100644 -index 000000000..454edf2fb ---- /dev/null -+++ b/ta/remoteproc/remoteproc_core.c -@@ -0,0 +1,781 @@ -+ // SPDX-License-Identifier: BSD-2-Clause -+ /* -+ * Copyright (C) 2020-2021, STMicroelectronics - All Rights Reserved -+ */ -+ -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+ -+/* Firmware state */ -+enum remoteproc_state { -+ REMOTEPROC_OFF, -+ REMOTEPROC_LOADED, -+ REMOTEPROC_STARTED, -+}; -+ -+#define RPROC_HDR_MAGIC 0x3543A468 /*random value */ -+#define HEADER_VERSION 1 -+ -+/* Supported signature algorithm */ -+enum remoteproc_sign_type { -+ RPROC_RSASSA_PKCS1_v1_5_SHA256 = 1, -+ RPROC_ECDSA_SHA256 = 2, -+}; -+ -+/* -+ * struct remoteproc_segment - program header with hash structure -+ * @phdr: program header -+ * @hash: hash associated to the program segment. -+ */ -+struct remoteproc_segment { -+ Elf32_Phdr phdr; -+ unsigned char hash[TEE_SHA256_HASH_SIZE]; -+}; -+ -+/* -+ * struct remoteproc_fw_hdr - firmware header -+ * @magic: Magic number, must be equal to RPROC_HDR_MAGIC -+ * @version: Version of the header (must be 1) -+ * @hdr_length: Total header byte length including chunks -+ * @sign_length: Signature chunk byte length -+ * @sign_offset: Signature chunk byte offset from header start -+ * @sign_type: Signature type -+ * @phhdr_length: Program header with hashes byte size, possibly 0 -+ * @phhdr_offset: Program header with hashes byte offset, 0 if not used -+ * @phhdr_type: Program header with hash type or 0 if not used -+ * @key_length: Authentication key info byte length, possibly 0 -+ * @key_offset: Authentication key info byte offset, 0 if not used -+ * @img_length: Firmware image chunk byte length -+ * @img_offset: Firmware image chunk byte offset -+ * @img_type: Firmware image type -+ */ -+struct remoteproc_fw_hdr { -+ uint32_t magic; -+ uint32_t version; -+ uint32_t hdr_length; -+ uint32_t sign_length; -+ uint32_t sign_offset; -+ uint32_t sign_type; -+ uint32_t phhdr_length; -+ uint32_t phhdr_offset; -+ uint32_t phhdr_type; -+ uint32_t key_length; -+ uint32_t key_offset; -+ uint32_t img_length; -+ uint32_t img_offset; -+ uint32_t img_type; -+}; -+ -+/* -+ * struct remoteproc_sig_algo - signature algorithm information -+ * @sign_type: Header signature type -+ * @id: Signature algorigthm identifier TEE_ALG_* -+ * @hash_len: Signature hash length -+ */ -+struct remoteproc_sig_algo { -+ enum remoteproc_sign_type sign_type; -+ uint32_t id; -+ size_t hash_len; -+}; -+ -+/* -+ * struct remoteproc_context - firmware context -+ * @fw_id: Unique Id of the firmware -+ * @hdr: Location of a secure copy of the firmware header -+ * @fw_img: Firmware image -+ * @fw_img_size: Byte size of the firmware image -+ * @rsc_pa: Physical address of the firmware resource table -+ * @rsc_size: Byte size of the firmware resource table -+ * @state: Remote-processor state -+ * @hw_fmt: Image format capabilities of the remoteproc PTA -+ * @hw_img_prot: Image protection capabilities of the remoteproc PTA -+ * @link: Linked list element -+ */ -+struct remoteproc_context { -+ uint32_t fw_id; -+ struct remoteproc_fw_hdr *hdr; -+ uint8_t *fw_img; -+ size_t fw_img_size; -+ paddr_t rsc_pa; -+ uint32_t rsc_size; -+ enum remoteproc_state state; -+ uint32_t hw_fmt; -+ uint32_t hw_img_prot; -+ TAILQ_ENTRY(remoteproc_context) link; -+}; -+ -+TAILQ_HEAD(remoteproc_firmware_head, remoteproc_context); -+ -+static struct remoteproc_firmware_head firmware_head = -+ TAILQ_HEAD_INITIALIZER(firmware_head); -+ -+static const struct remoteproc_sig_algo rproc_ta_sign_algo[] = { -+ { -+ .sign_type = RPROC_RSASSA_PKCS1_v1_5_SHA256, -+ .id = TEE_ALG_RSASSA_PKCS1_V1_5_SHA256, -+ .hash_len = TEE_SHA256_HASH_SIZE, -+ }, -+ { -+ .sign_type = RPROC_ECDSA_SHA256, -+ .id = TEE_ALG_ECDSA_P256, -+ .hash_len = TEE_SHA256_HASH_SIZE, -+ }, -+}; -+ -+static size_t session_refcount; -+static TEE_TASessionHandle pta_session; -+ -+static void remoteproc_header_dump(struct remoteproc_fw_hdr __maybe_unused *hdr) -+{ -+ DMSG("magic :\t%#"PRIx32, hdr->magic); -+ DMSG("version :\t%#"PRIx32, hdr->version); -+ DMSG("hdr_length :\t%#"PRIx32, hdr->hdr_length); -+ DMSG("sign_length :\t%#"PRIx32, hdr->sign_length); -+ DMSG("sign_offset :\t%#"PRIx32, hdr->sign_offset); -+ DMSG("sign_type :\t%#"PRIx32, hdr->sign_type); -+ DMSG("phhdr_length :\t%#"PRIx32, hdr->phhdr_length); -+ DMSG("phhdr_offset :\t%#"PRIx32, hdr->phhdr_offset); -+ DMSG("phhdr_type :\t%#"PRIx32, hdr->phhdr_type); -+ DMSG("key_length :\t%#"PRIx32, hdr->key_length); -+ DMSG("key_offset :\t%#"PRIx32, hdr->key_offset); -+ DMSG("img_length :\t%#"PRIx32, hdr->img_length); -+ DMSG("img_offset :\t%#"PRIx32, hdr->img_offset); -+ DMSG("img_type :\t%#"PRIx32, hdr->img_type); -+} -+ -+static struct remoteproc_context *remoteproc_find_firmware(uint32_t fw_id) -+{ -+ struct remoteproc_context *ctx = NULL; -+ -+ TAILQ_FOREACH(ctx, &firmware_head, link) { -+ if (ctx->fw_id == fw_id) -+ return ctx; -+ } -+ -+ return NULL; -+} -+ -+static struct remoteproc_context *remoteproc_add_firmware(uint32_t fw_id) -+{ -+ struct remoteproc_context *ctx = NULL; -+ -+ ctx = TEE_Malloc(sizeof(*ctx), TEE_MALLOC_FILL_ZERO); -+ if (!ctx) -+ return NULL; -+ -+ ctx->fw_id = fw_id; -+ -+ TAILQ_INSERT_TAIL(&firmware_head, ctx, link); -+ -+ return ctx; -+} -+ -+static const struct remoteproc_sig_algo *remoteproc_get_algo(uint32_t sign_type) -+{ -+ unsigned int i = 0; -+ -+ for (i = 0; i < ARRAY_SIZE(rproc_ta_sign_algo); i++) -+ if (sign_type == rproc_ta_sign_algo[i].sign_type) -+ return &rproc_ta_sign_algo[i]; -+ -+ return NULL; -+} -+ -+static TEE_Result remoteproc_pta_verify(struct remoteproc_context *ctx, -+ const struct remoteproc_sig_algo *algo, -+ char *hash, uint32_t hash_len) -+{ -+ TEE_Result res = TEE_ERROR_GENERIC; -+ struct remoteproc_fw_hdr *hdr = ctx->hdr; -+ struct rproc_pta_key_info *keyinfo = NULL; -+ uint32_t param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, -+ TEE_PARAM_TYPE_MEMREF_INPUT, -+ TEE_PARAM_TYPE_MEMREF_INPUT, -+ TEE_PARAM_TYPE_MEMREF_INPUT); -+ TEE_Param params[TEE_NUM_PARAMS] = { }; -+ -+ keyinfo = TEE_Malloc(sizeof(*keyinfo) + hdr->key_length, 0); -+ if (!keyinfo) -+ return TEE_ERROR_OUT_OF_MEMORY; -+ -+ keyinfo->algo = algo->id; -+ keyinfo->info_size = hdr->key_length; -+ memcpy(keyinfo->info, (uint8_t *)hdr + hdr->key_offset, -+ hdr->key_length); -+ -+ params[0].value.a = ctx->fw_id; -+ params[1].memref.buffer = keyinfo; -+ params[1].memref.size = RPROC_PTA_GET_KEYINFO_SIZE(keyinfo); -+ params[2].memref.buffer = hash; -+ params[2].memref.size = hash_len; -+ params[3].memref.buffer = (uint8_t *)hdr + hdr->sign_offset; -+ params[3].memref.size = hdr->sign_length; -+ -+ res = TEE_InvokeTACommand(pta_session, TEE_TIMEOUT_INFINITE, -+ PTA_REMOTEPROC_VERIFY_DIGEST, -+ param_types, params, NULL); -+ if (res != TEE_SUCCESS) -+ EMSG("Failed to verify signature, res = %#"PRIx32, res); -+ -+ TEE_Free(keyinfo); -+ -+ return res; -+} -+ -+static TEE_Result remoteproc_save_fw_header(struct remoteproc_context *ctx, -+ void *fw_orig, -+ uint32_t fw_orig_size) -+{ -+ struct remoteproc_fw_hdr *hdr = fw_orig; -+ -+ remoteproc_header_dump(hdr); -+ -+ if (fw_orig_size <= sizeof(*hdr) || fw_orig_size <= hdr->hdr_length) -+ return TEE_ERROR_CORRUPT_OBJECT; -+ -+ ctx->hdr = TEE_Malloc(hdr->hdr_length, TEE_MALLOC_FILL_ZERO); -+ if (!ctx->hdr) -+ return TEE_ERROR_OUT_OF_MEMORY; -+ -+ memcpy(ctx->hdr, fw_orig, hdr->hdr_length); -+ -+ return TEE_SUCCESS; -+} -+ -+static TEE_Result remoteproc_verify_signature(struct remoteproc_context *ctx) -+{ -+ TEE_OperationHandle op = TEE_HANDLE_NULL; -+ struct remoteproc_fw_hdr *hdr = ctx->hdr; -+ const struct remoteproc_sig_algo *algo = NULL; -+ TEE_Result res = TEE_ERROR_GENERIC; -+ char *hash = NULL; -+ uint32_t hash_len = 0; -+ -+ algo = remoteproc_get_algo(hdr->sign_type); -+ if (!algo) { -+ EMSG("Unsupported signature type %d", hdr->sign_type); -+ return TEE_ERROR_NOT_SUPPORTED; -+ } -+ -+ /* Compute the header hash */ -+ hash_len = algo->hash_len; -+ hash = TEE_Malloc(hash_len, 0); -+ if (!hash) -+ return TEE_ERROR_OUT_OF_MEMORY; -+ -+ res = TEE_AllocateOperation(&op, TEE_ALG_SHA256, TEE_MODE_DIGEST, 0); -+ if (res != TEE_SUCCESS) -+ goto free_hash; -+ -+ res = TEE_DigestDoFinal(op, hdr, hdr->sign_offset, hash, &hash_len); -+ if (res != TEE_SUCCESS) -+ goto out; -+ -+ /* -+ * TODO: -+ * Provide alternative to verify the signature in the TA. This could -+ * be done for instance by getting the key object from secure storage. -+ */ -+ -+ /* By default ask the pta to verify the signature. */ -+ res = remoteproc_pta_verify(ctx, algo, hash, hash_len); -+ -+out: -+ TEE_FreeOperation(op); -+free_hash: -+ TEE_Free(hash); -+ -+ return res; -+} -+ -+static TEE_Result remoteproc_verify_header(struct remoteproc_context *ctx) -+{ -+ struct remoteproc_fw_hdr *hdr = ctx->hdr; -+ uint32_t hdr_size = 0; -+ uint32_t chunk_size = 0; -+ uint32_t alignment = 0; -+ -+ if (hdr->magic != RPROC_HDR_MAGIC) -+ return TEE_ERROR_CORRUPT_OBJECT; -+ -+ if (hdr->version != HEADER_VERSION) -+ return TEE_ERROR_CORRUPT_OBJECT; -+ -+ /* -+ * The offsets are aligned to 64 bits format. The hdr_length takes into -+ * account these alignments while the length of each chunks are the -+ * effective length,excluding the alignment padding bytes. -+ */ -+ alignment = hdr->sign_length % sizeof(uint64_t) + -+ hdr->phhdr_length % sizeof(uint64_t) + -+ hdr->key_length % sizeof(uint64_t); -+ -+ if (ADD_OVERFLOW(sizeof(*hdr), hdr->sign_length, &hdr_size) || -+ ADD_OVERFLOW(hdr_size, hdr->phhdr_length, &hdr_size) || -+ ADD_OVERFLOW(hdr_size, hdr->key_length, &hdr_size) || -+ ADD_OVERFLOW(hdr_size, alignment, &hdr_size) || -+ hdr->hdr_length != hdr_size) -+ return TEE_ERROR_CORRUPT_OBJECT; -+ -+ if (ADD_OVERFLOW(hdr->sign_offset, hdr->sign_length, &chunk_size) || -+ chunk_size > hdr_size || -+ ADD_OVERFLOW(hdr->key_offset, hdr->key_length, &chunk_size) || -+ chunk_size > hdr_size || -+ ADD_OVERFLOW(hdr->phhdr_offset, hdr->phhdr_length, &chunk_size) || -+ chunk_size > hdr_size) -+ return TEE_ERROR_CORRUPT_OBJECT; -+ -+ if (hdr->phhdr_length % sizeof(struct remoteproc_segment)) -+ return TEE_ERROR_CORRUPT_OBJECT; -+ -+ return remoteproc_verify_signature(ctx); -+} -+ -+static TEE_Result get_rproc_pta_capabilities(struct remoteproc_context *ctx) -+{ -+ uint32_t param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, -+ TEE_PARAM_TYPE_VALUE_OUTPUT, -+ TEE_PARAM_TYPE_VALUE_OUTPUT, -+ TEE_PARAM_TYPE_NONE); -+ TEE_Param params[TEE_NUM_PARAMS] = { }; -+ TEE_Result res = TEE_ERROR_GENERIC; -+ -+ params[0].value.a = ctx->fw_id; -+ -+ res = TEE_InvokeTACommand(pta_session, TEE_TIMEOUT_INFINITE, -+ PTA_REMOTEPROC_HW_CAPABILITIES, -+ param_types, params, NULL); -+ if (res) -+ return res; -+ -+ ctx->hw_fmt = params[1].value.a; -+ ctx->hw_img_prot = params[2].value.a; -+ -+ return TEE_SUCCESS; -+} -+ -+static TEE_Result remoteproc_verify_firmware(struct remoteproc_context *ctx, -+ uint8_t *fw_orig, -+ uint32_t fw_orig_size) -+{ -+ struct remoteproc_fw_hdr *hdr = NULL; -+ TEE_Result res = TEE_ERROR_GENERIC; -+ -+ res = get_rproc_pta_capabilities(ctx); -+ if (res) -+ return res; -+ -+ /* Secure the firmware image depending on strategy */ -+ if (!(ctx->hw_img_prot & PTA_REMOTEPROC_FW_WITH_HASH_TABLE) || -+ ctx->hw_fmt != PTA_REMOTEPROC_ELF_FMT) { -+ /* -+ * Only hash table for ELF format support implemented -+ * in a first step. -+ */ -+ return TEE_ERROR_NOT_IMPLEMENTED; -+ } -+ -+ res = remoteproc_save_fw_header(ctx, fw_orig, fw_orig_size); -+ if (res) -+ return res; -+ -+ res = remoteproc_verify_header(ctx); -+ if (res) -+ goto free_hdr; -+ -+ /* Store location of the loadable binary in non-secure memory */ -+ hdr = ctx->hdr; -+ ctx->fw_img_size = hdr->img_length; -+ ctx->fw_img = fw_orig + hdr->img_offset; -+ -+ DMSG("Firmware image addr: %p size: %zu", ctx->fw_img, -+ ctx->fw_img_size); -+ -+free_hdr: -+ TEE_Free(ctx->hdr); -+ -+ return res; -+} -+ -+static paddr_t remoteproc_da_to_pa(uint32_t da, uint32_t size, void *priv) -+{ -+ struct remoteproc_context *ctx = priv; -+ uint32_t param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, -+ TEE_PARAM_TYPE_VALUE_INPUT, -+ TEE_PARAM_TYPE_VALUE_INPUT, -+ TEE_PARAM_TYPE_VALUE_OUTPUT); -+ TEE_Param params[TEE_NUM_PARAMS] = { }; -+ TEE_Result res = TEE_ERROR_GENERIC; -+ -+ params[0].value.a = ctx->fw_id; -+ params[1].value.a = da; -+ params[2].value.a = size; -+ -+ res = TEE_InvokeTACommand(pta_session, TEE_TIMEOUT_INFINITE, -+ PTA_REMOTEPROC_FIRMWARE_DA_TO_PA, -+ param_types, params, NULL); -+ if (res != TEE_SUCCESS) { -+ EMSG("Failed to translate device address %#"PRIx32, da); -+ return 0; -+ } -+ -+ return (paddr_t)reg_pair_to_64(params[3].value.b, params[3].value.a); -+} -+ -+static TEE_Result remoteproc_parse_rsc_table(struct remoteproc_context *ctx) -+{ -+ uint32_t da = 0; -+ TEE_Result res = TEE_ERROR_GENERIC; -+ -+ res = e32_parser_find_rsc_table(ctx->fw_img, ctx->fw_img_size, -+ &da, &ctx->rsc_size); -+ if (res == TEE_ERROR_NO_DATA) { -+ /* Firmware without resource table */ -+ ctx->rsc_size = 0; -+ ctx->rsc_pa = 0; -+ return TEE_SUCCESS; -+ } -+ if (res) -+ return res; -+ -+ if (da) { -+ DMSG("Resource table device address %#"PRIx32" size %zu", -+ da, ctx->rsc_size); -+ -+ ctx->rsc_pa = remoteproc_da_to_pa(da, ctx->rsc_size, ctx); -+ if (!ctx->rsc_pa) -+ return TEE_ERROR_ACCESS_DENIED; -+ } -+ -+ return TEE_SUCCESS; -+} -+ -+static TEE_Result get_segment_hash(struct remoteproc_context *ctx, uint8_t *src, -+ uint32_t size, uint32_t da, -+ uint32_t mem_size, unsigned char **hash) -+{ -+ struct remoteproc_fw_hdr *hdr = ctx->hdr; -+ struct remoteproc_segment *peh = NULL; -+ unsigned int i = 0; -+ unsigned int nb_entry = hdr->phhdr_length / sizeof(*peh); -+ -+ peh = (void *)((uint8_t *)hdr + hdr->phhdr_offset); -+ -+ for (i = 0; i < nb_entry; peh++, i++) { -+ if (peh->phdr.p_paddr != da) -+ continue; -+ -+ /* -+ * Segment is read from a non secure memory. Crosscheck it using -+ * the hash table to verify that the segment has not been -+ * corrupted. -+ */ -+ if (peh->phdr.p_type != PT_LOAD) -+ return TEE_ERROR_CORRUPT_OBJECT; -+ -+ if (peh->phdr.p_filesz != size || peh->phdr.p_memsz != mem_size) -+ return TEE_ERROR_CORRUPT_OBJECT; -+ -+ if ((Elf32_Off)(src - ctx->fw_img) != peh->phdr.p_offset) -+ return TEE_ERROR_CORRUPT_OBJECT; -+ -+ *hash = peh->hash; -+ -+ return TEE_SUCCESS; -+ } -+ -+ return TEE_ERROR_NO_DATA; -+} -+ -+static TEE_Result remoteproc_load_segment(uint8_t *src, uint32_t size, -+ uint32_t da, uint32_t mem_size, -+ void *priv) -+{ -+ struct remoteproc_context *ctx = priv; -+ uint32_t param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, -+ TEE_PARAM_TYPE_MEMREF_INPUT, -+ TEE_PARAM_TYPE_VALUE_INPUT, -+ TEE_PARAM_TYPE_MEMREF_INPUT); -+ TEE_Param params[TEE_NUM_PARAMS] = { }; -+ TEE_Result res = TEE_ERROR_GENERIC; -+ unsigned char *hash = NULL; -+ -+ /* -+ * Invoke platform remoteproc PTA to load the segment in remote -+ * processor memory which is not mapped in the TA space. -+ */ -+ -+ DMSG("Load segment %#"PRIx32" size %"PRIu32" (%"PRIu32")", da, size, -+ mem_size); -+ -+ res = get_segment_hash(ctx, src, size, da, mem_size, &hash); -+ if (res) -+ return res; -+ -+ params[0].value.a = ctx->fw_id; -+ params[1].memref.buffer = src; -+ params[1].memref.size = size; -+ params[2].value.a = da; -+ params[3].memref.buffer = hash; -+ params[3].memref.size = TEE_SHA256_HASH_SIZE; -+ -+ res = TEE_InvokeTACommand(pta_session, TEE_TIMEOUT_INFINITE, -+ PTA_REMOTEPROC_LOAD_SEGMENT_SHA256, -+ param_types, params, NULL); -+ if (res != TEE_SUCCESS) { -+ EMSG("Fails to load segment, res = 0x%x", res); -+ return res; -+ } -+ -+ /* Fill the rest of the memory with 0 */ -+ if (size < mem_size) { -+ param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, -+ TEE_PARAM_TYPE_VALUE_INPUT, -+ TEE_PARAM_TYPE_VALUE_INPUT, -+ TEE_PARAM_TYPE_VALUE_INPUT); -+ params[1].value.a = da + size; -+ params[2].value.a = mem_size - size; -+ params[3].value.a = 0; -+ -+ res = TEE_InvokeTACommand(pta_session, TEE_TIMEOUT_INFINITE, -+ PTA_REMOTEPROC_SET_MEMORY, -+ param_types, params, NULL); -+ if (res != TEE_SUCCESS) -+ EMSG("Fails to clear segment, res = 0x%x", res); -+ } -+ -+ return res; -+} -+ -+static TEE_Result remoteproc_load_elf(struct remoteproc_context *ctx) -+{ -+ TEE_Result res = TEE_ERROR_GENERIC; -+ -+ res = e32_parse_ehdr(ctx->fw_img, ctx->fw_img_size); -+ if (res) { -+ EMSG("Failed to parse firmware, res = %#"PRIx32, res); -+ return res; -+ } -+ -+ return e32_parser_load_elf_image(ctx->fw_img, ctx->fw_img_size, -+ remoteproc_load_segment, ctx); -+} -+ -+static TEE_Result remoteproc_load_fw(uint32_t pt, -+ TEE_Param params[TEE_NUM_PARAMS]) -+{ -+ const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, -+ TEE_PARAM_TYPE_MEMREF_INPUT, -+ TEE_PARAM_TYPE_NONE, -+ TEE_PARAM_TYPE_NONE); -+ struct remoteproc_context *ctx = NULL; -+ uint32_t fw_id = params[0].value.a; -+ TEE_Result res = TEE_ERROR_GENERIC; -+ -+ if (pt != exp_pt) -+ return TEE_ERROR_BAD_PARAMETERS; -+ -+ ctx = remoteproc_find_firmware(fw_id); -+ if (!ctx) -+ ctx = remoteproc_add_firmware(fw_id); -+ if (!ctx) -+ return TEE_ERROR_OUT_OF_MEMORY; -+ -+ if (ctx->state != REMOTEPROC_OFF) -+ return TEE_ERROR_BAD_STATE; -+ -+ if (!params[1].memref.buffer || !params[1].memref.size) -+ return TEE_ERROR_BAD_PARAMETERS; -+ -+ DMSG("Got base addr: %p size %zu", params[1].memref.buffer, -+ params[1].memref.size); -+ -+ res = remoteproc_verify_firmware(ctx, params[1].memref.buffer, -+ params[1].memref.size); -+ if (res) { -+ EMSG("Can't Authenticate the firmware, res = %#"PRIx32, res); -+ goto out; -+ } -+ -+ res = remoteproc_load_elf(ctx); -+ if (res) -+ goto out; -+ -+ /* Take opportunity to get the resource table address */ -+ res = remoteproc_parse_rsc_table(ctx); -+ if (res == TEE_SUCCESS) -+ ctx->state = REMOTEPROC_LOADED; -+ -+out: -+ /* Clear reference to firmware image from shared memory */ -+ ctx->fw_img = NULL; -+ ctx->fw_img_size = 0; -+ -+ return res; -+} -+ -+static TEE_Result remoteproc_start_fw(uint32_t pt, -+ TEE_Param params[TEE_NUM_PARAMS]) -+{ -+ const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, -+ TEE_PARAM_TYPE_NONE, -+ TEE_PARAM_TYPE_NONE, -+ TEE_PARAM_TYPE_NONE); -+ struct remoteproc_context *ctx = NULL; -+ TEE_Result res = TEE_ERROR_GENERIC; -+ -+ if (pt != exp_pt) -+ return TEE_ERROR_BAD_PARAMETERS; -+ -+ ctx = remoteproc_find_firmware(params[0].value.a); -+ if (!ctx) -+ return TEE_ERROR_BAD_PARAMETERS; -+ -+ switch (ctx->state) { -+ case REMOTEPROC_OFF: -+ res = TEE_ERROR_BAD_STATE; -+ break; -+ case REMOTEPROC_STARTED: -+ res = TEE_SUCCESS; -+ break; -+ case REMOTEPROC_LOADED: -+ res = TEE_InvokeTACommand(pta_session, TEE_TIMEOUT_INFINITE, -+ PTA_REMOTEPROC_FIRMWARE_START, -+ pt, params, NULL); -+ if (res == TEE_SUCCESS) -+ ctx->state = REMOTEPROC_STARTED; -+ break; -+ default: -+ res = TEE_ERROR_BAD_STATE; -+ } -+ -+ return res; -+} -+ -+static TEE_Result remoteproc_stop_fw(uint32_t pt, -+ TEE_Param params[TEE_NUM_PARAMS]) -+{ -+ const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, -+ TEE_PARAM_TYPE_NONE, -+ TEE_PARAM_TYPE_NONE, -+ TEE_PARAM_TYPE_NONE); -+ struct remoteproc_context *ctx = NULL; -+ TEE_Result res = TEE_ERROR_GENERIC; -+ -+ if (pt != exp_pt) -+ return TEE_ERROR_BAD_PARAMETERS; -+ -+ ctx = remoteproc_find_firmware(params[0].value.a); -+ if (!ctx) -+ return TEE_ERROR_BAD_PARAMETERS; -+ -+ switch (ctx->state) { -+ case REMOTEPROC_LOADED: -+ res = TEE_ERROR_BAD_STATE; -+ break; -+ case REMOTEPROC_OFF: -+ res = TEE_SUCCESS; -+ break; -+ case REMOTEPROC_STARTED: -+ res = TEE_InvokeTACommand(pta_session, TEE_TIMEOUT_INFINITE, -+ PTA_REMOTEPROC_FIRMWARE_STOP, -+ pt, params, NULL); -+ if (res == TEE_SUCCESS) -+ ctx->state = REMOTEPROC_OFF; -+ break; -+ default: -+ res = TEE_ERROR_BAD_STATE; -+ } -+ -+ return res; -+} -+ -+static TEE_Result remoteproc_get_rsc_table(uint32_t pt, -+ TEE_Param params[TEE_NUM_PARAMS]) -+{ -+ const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, -+ TEE_PARAM_TYPE_VALUE_OUTPUT, -+ TEE_PARAM_TYPE_VALUE_OUTPUT, -+ TEE_PARAM_TYPE_NONE); -+ struct remoteproc_context *ctx = NULL; -+ -+ if (pt != exp_pt) -+ return TEE_ERROR_BAD_PARAMETERS; -+ -+ ctx = remoteproc_find_firmware(params[0].value.a); -+ if (!ctx) -+ return TEE_ERROR_BAD_PARAMETERS; -+ -+ if (ctx->state == REMOTEPROC_OFF) -+ return TEE_ERROR_BAD_STATE; -+ -+ reg_pair_from_64((uint64_t)ctx->rsc_pa, -+ ¶ms[1].value.b, ¶ms[1].value.a); -+ reg_pair_from_64((uint64_t)ctx->rsc_size, -+ ¶ms[2].value.b, ¶ms[2].value.a); -+ -+ return TEE_SUCCESS; -+} -+ -+TEE_Result TA_CreateEntryPoint(void) -+{ -+ return TEE_SUCCESS; -+} -+ -+void TA_DestroyEntryPoint(void) -+{ -+} -+ -+TEE_Result TA_OpenSessionEntryPoint(uint32_t pt __unused, -+ TEE_Param params[TEE_NUM_PARAMS] __unused, -+ void **sess __unused) -+{ -+ static const TEE_UUID uuid = PTA_REMOTEPROC_UUID; -+ TEE_Result res = TEE_ERROR_GENERIC; -+ -+ if (!session_refcount) { -+ res = TEE_OpenTASession(&uuid, TEE_TIMEOUT_INFINITE, 0, NULL, -+ &pta_session, NULL); -+ if (res) -+ return res; -+ } -+ -+ session_refcount++; -+ -+ return TEE_SUCCESS; -+} -+ -+void TA_CloseSessionEntryPoint(void *sess __unused) -+{ -+ session_refcount--; -+ -+ if (!session_refcount) -+ TEE_CloseTASession(pta_session); -+} -+ -+TEE_Result TA_InvokeCommandEntryPoint(void *sess __unused, uint32_t cmd_id, -+ uint32_t pt, -+ TEE_Param params[TEE_NUM_PARAMS]) -+{ -+ switch (cmd_id) { -+ case TA_RPROC_FW_CMD_LOAD_FW: -+ return remoteproc_load_fw(pt, params); -+ case TA_RPROC_FW_CMD_START_FW: -+ return remoteproc_start_fw(pt, params); -+ case TA_RPROC_FW_CMD_STOP_FW: -+ return remoteproc_stop_fw(pt, params); -+ case TA_RPROC_FW_CMD_GET_RSC_TABLE: -+ return remoteproc_get_rsc_table(pt, params); -+ case TA_RPROC_FW_CMD_GET_COREDUMP: -+ return TEE_ERROR_NOT_IMPLEMENTED; -+ default: -+ return TEE_ERROR_BAD_PARAMETERS; -+ } -+} -diff --git a/ta/remoteproc/sub.mk b/ta/remoteproc/sub.mk -new file mode 100644 -index 000000000..caca5901a ---- /dev/null -+++ b/ta/remoteproc/sub.mk -@@ -0,0 +1,3 @@ -+global-incdirs-y += include -+srcs-y += remoteproc_core.c -+srcs-y += elf_parser.c -diff --git a/ta/remoteproc/user_ta.mk b/ta/remoteproc/user_ta.mk -new file mode 100644 -index 000000000..6db23f705 ---- /dev/null -+++ b/ta/remoteproc/user_ta.mk -@@ -0,0 +1 @@ -+user-ta-uuid := 80a4c275-0a47-4905-8285-1486a9771a08 -\ No newline at end of file --- -2.17.1 - diff --git a/recipes-security/optee/optee-os/0001-3.16.0-stm32mp-r1.patch b/recipes-security/optee/optee-os/0001-3.16.0-stm32mp-r1.patch new file mode 100644 index 0000000..324c4fd --- /dev/null +++ b/recipes-security/optee/optee-os/0001-3.16.0-stm32mp-r1.patch @@ -0,0 +1,127744 @@ +From 87a308570c2dfe94356df7a8cb7e252cd5b7c321 Mon Sep 17 00:00:00 2001 +From: Christophe Priouzeau +Date: Mon, 30 May 2022 10:59:52 +0200 +Subject: [PATCH] 3.16.0-stm32mp-r1 + +--- + CONTRIBUTING.md | 30 + + SECURITY.md | 8 + + core/arch/arm/dts/stm32mp13-pinctrl.dtsi | 161 + + core/arch/arm/dts/stm32mp131.dtsi | 644 ++ + core/arch/arm/dts/stm32mp133.dtsi | 7 + + core/arch/arm/dts/stm32mp135.dtsi | 24 + + core/arch/arm/dts/stm32mp135d-dk.dts | 630 ++ + core/arch/arm/dts/stm32mp135f-dk.dts | 642 ++ + core/arch/arm/dts/stm32mp13xa.dtsi | 14 + + core/arch/arm/dts/stm32mp13xc.dtsi | 34 + + core/arch/arm/dts/stm32mp13xd.dtsi | 20 + + core/arch/arm/dts/stm32mp13xf.dtsi | 34 + + core/arch/arm/dts/stm32mp15-pinctrl.dtsi | 1022 +-- + core/arch/arm/dts/stm32mp151.dtsi | 1603 +---- + core/arch/arm/dts/stm32mp153.dtsi | 28 - + core/arch/arm/dts/stm32mp157.dtsi | 24 - + core/arch/arm/dts/stm32mp157a-dk1.dts | 28 +- + core/arch/arm/dts/stm32mp157a-ed1.dts | 43 + + core/arch/arm/dts/stm32mp157a-ev1.dts | 24 + + core/arch/arm/dts/stm32mp157c-dk2.dts | 98 +- + core/arch/arm/dts/stm32mp157c-ed1.dts | 378 +- + core/arch/arm/dts/stm32mp157c-ev1.dts | 348 +- + core/arch/arm/dts/stm32mp157d-dk1.dts | 49 + + core/arch/arm/dts/stm32mp157d-ed1.dts | 43 + + core/arch/arm/dts/stm32mp157d-ev1.dts | 24 + + core/arch/arm/dts/stm32mp157f-dk2.dts | 51 + + core/arch/arm/dts/stm32mp157f-ed1.dts | 44 + + core/arch/arm/dts/stm32mp157f-ev1.dts | 24 + + core/arch/arm/dts/stm32mp15xa.dtsi | 14 + + core/arch/arm/dts/stm32mp15xc.dtsi | 20 +- + core/arch/arm/dts/stm32mp15xd.dtsi | 20 + + core/arch/arm/dts/stm32mp15xf.dtsi | 18 + + core/arch/arm/dts/stm32mp15xx-dkx.dtsi | 662 +- + core/arch/arm/dts/stm32mp15xx-edx.dtsi | 448 ++ + core/arch/arm/dts/stm32mp15xx-evx.dtsi | 23 + + core/arch/arm/include/arm32.h | 9 + + core/arch/arm/include/kernel/thread.h | 4 +- + core/arch/arm/include/mm/core_mmu.h | 3 + + core/arch/arm/include/sm/optee_smc.h | 3 + + core/arch/arm/include/sm/pm.h | 4 + + core/arch/arm/kernel/boot.c | 13 +- + core/arch/arm/kernel/trace_ext.c | 53 +- + core/arch/arm/mm/core_mmu.c | 39 +- + core/arch/arm/plat-stm32mp1/boot_api.h | 2 + + core/arch/arm/plat-stm32mp1/bsec_pta.c | 317 + + core/arch/arm/plat-stm32mp1/conf.mk | 251 +- + core/arch/arm/plat-stm32mp1/cpu_opp.c | 354 + + core/arch/arm/plat-stm32mp1/display.c | 164 + + .../plat-stm32mp1/drivers/stm32mp1_calib.c | 615 ++ + .../arm/plat-stm32mp1/drivers/stm32mp1_ddrc.c | 655 ++ + .../arm/plat-stm32mp1/drivers/stm32mp1_ddrc.h | 32 + + .../arm/plat-stm32mp1/drivers/stm32mp1_pmic.c | 771 ++- + .../arm/plat-stm32mp1/drivers/stm32mp1_pmic.h | 24 +- + .../arm/plat-stm32mp1/drivers/stm32mp1_pwr.c | 195 +- + .../arm/plat-stm32mp1/drivers/stm32mp1_pwr.h | 65 +- + .../plat-stm32mp1/drivers/stm32mp1_pwr_irq.c | 396 ++ + .../arm/plat-stm32mp1/drivers/stm32mp1_rcc.c | 18 + + .../plat-stm32mp1/drivers/stm32mp1_syscfg.c | 324 +- + core/arch/arm/plat-stm32mp1/drivers/sub.mk | 5 +- + core/arch/arm/plat-stm32mp1/link.mk | 2 + + core/arch/arm/plat-stm32mp1/main.c | 578 +- + .../arm/plat-stm32mp1/nsec-service/bsec_svc.c | 67 - + .../arm/plat-stm32mp1/nsec-service/bsec_svc.h | 21 - + .../nsec-service/low_power_svc.c | 44 + + .../nsec-service/low_power_svc.h | 23 + + .../arm/plat-stm32mp1/nsec-service/pwr_svc.c | 89 + + .../arm/plat-stm32mp1/nsec-service/pwr_svc.h | 22 + + .../plat-stm32mp1/nsec-service/stm32mp1_smc.h | 101 +- + .../nsec-service/stm32mp1_svc_setup.c | 38 +- + .../arm/plat-stm32mp1/nsec-service/sub.mk | 3 +- + core/arch/arm/plat-stm32mp1/plat_tzc400.c | 536 +- + core/arch/arm/plat-stm32mp1/platform_config.h | 298 +- + core/arch/arm/plat-stm32mp1/pm/context.c | 617 ++ + core/arch/arm/plat-stm32mp1/pm/context.h | 105 + + .../plat-stm32mp1/pm/context_asm_defines.c | 28 + + core/arch/arm/plat-stm32mp1/pm/low_power.c | 559 ++ + core/arch/arm/plat-stm32mp1/pm/pm_helpers.S | 729 ++ + core/arch/arm/plat-stm32mp1/pm/power.h | 27 + + core/arch/arm/plat-stm32mp1/pm/power_config.c | 261 + + core/arch/arm/plat-stm32mp1/pm/psci.c | 268 +- + core/arch/arm/plat-stm32mp1/pm/sub.mk | 6 + + core/arch/arm/plat-stm32mp1/remoteproc_pta.c | 540 ++ + core/arch/arm/plat-stm32mp1/reset.S | 28 +- + core/arch/arm/plat-stm32mp1/rng_seed.c | 35 +- + core/arch/arm/plat-stm32mp1/rproc_pub_key.h | 16 + + core/arch/arm/plat-stm32mp1/scmi_server.c | 722 +- + .../arch/arm/plat-stm32mp1/shared_resources.c | 356 +- + core/arch/arm/plat-stm32mp1/stm32_util.h | 145 +- + core/arch/arm/plat-stm32mp1/stm32mp_pm.h | 24 + + core/arch/arm/plat-stm32mp1/sub.mk | 14 +- + core/arch/arm/sm/pm_a32.S | 83 +- + core/arch/arm/tee/entry_fast.c | 3 + + core/arch/arm/tee/sub.mk | 1 + + core/arch/arm/tee/tui.c | 157 + + core/core.mk | 19 +- + core/drivers/clk/clk-stm32-core.c | 733 ++ + core/drivers/clk/clk-stm32-core.h | 272 + + core/drivers/clk/clk-stm32mp13.c | 3437 ++++++++++ + core/drivers/clk/clk-stm32mp15.c | 1421 +++- + core/drivers/clk/clk.c | 246 +- + core/drivers/clk/sub.mk | 4 +- + core/drivers/counter/counter.c | 233 + + core/drivers/counter/stm32_lptimer.c | 592 ++ + core/drivers/counter/stm32_tim.c | 425 ++ + core/drivers/counter/sub.mk | 3 + + .../crypto_api/include/drvcrypt_acipher.h | 2 +- + core/drivers/crypto/stm32/cipher.c | 216 +- + core/drivers/crypto/stm32/common.h | 10 +- + core/drivers/crypto/stm32/crypto.mk | 12 +- + core/drivers/crypto/stm32/ecc.c | 502 ++ + core/drivers/crypto/stm32/hash.c | 192 + + core/drivers/crypto/stm32/hmac.c | 223 + + core/drivers/crypto/stm32/stm32_cryp.c | 88 +- + core/drivers/crypto/stm32/stm32_cryp.h | 6 +- + core/drivers/crypto/stm32/stm32_hash.c | 918 +++ + core/drivers/crypto/stm32/stm32_hash.h | 78 + + core/drivers/crypto/stm32/stm32_pka.c | 1628 +++++ + core/drivers/crypto/stm32/stm32_pka.h | 80 + + core/drivers/crypto/stm32/stm32_saes.c | 1221 ++++ + core/drivers/crypto/stm32/stm32_saes.h | 68 + + core/drivers/crypto/stm32/sub.mk | 6 + + core/drivers/firewall/stm32_etzpc.c | 815 +++ + core/drivers/firewall/stm32_firewall.c | 217 + + core/drivers/firewall/sub.mk | 2 + + core/drivers/frame_buffer.c | 36 + + core/drivers/gic.c | 235 +- + core/drivers/regulator/core.c | 1019 +++ + core/drivers/regulator/regulator_fixed.c | 86 + + core/drivers/regulator/stm32_regulator_gpio.c | 169 + + core/drivers/regulator/stm32_vrefbuf.c | 295 + + core/drivers/regulator/sub.mk | 4 + + core/drivers/scmi-msg/base.c | 3 +- + core/drivers/scmi-msg/clock.c | 73 +- + core/drivers/scmi-msg/clock.h | 12 + + core/drivers/scmi-msg/entry.c | 4 + + core/drivers/scmi-msg/perf_domain.c | 385 ++ + core/drivers/scmi-msg/perf_domain.h | 139 + + core/drivers/scmi-msg/regulator_consumer.c | 354 + + core/drivers/scmi-msg/sub.mk | 2 + + core/drivers/stm32_bsec.c | 409 +- + core/drivers/stm32_etzpc.c | 346 - + core/drivers/stm32_exti.c | 380 + + core/drivers/stm32_gpio.c | 918 ++- + core/drivers/stm32_i2c.c | 180 +- + core/drivers/stm32_iwdg.c | 297 + + core/drivers/stm32_ltdc.c | 502 ++ + core/drivers/stm32_rng.c | 517 +- + core/drivers/stm32_rtc.c | 502 ++ + core/drivers/stm32_tamp.c | 1441 ++++ + core/drivers/stm32_uart.c | 31 +- + core/drivers/stpmic1.c | 300 +- + core/drivers/sub.mk | 10 +- + core/include/display.h | 52 + + core/include/drivers/clk.h | 60 + + core/include/drivers/clk_dt.h | 1 + + core/include/drivers/counter.h | 140 + + core/include/drivers/frame_buffer.h | 29 + + core/include/drivers/gic.h | 24 + + core/include/drivers/regulator.h | 135 + + core/include/drivers/scmi-msg.h | 99 +- + .../include/drivers/scmi_regulator_consumer.h | 12 + + core/include/drivers/stm32_bsec.h | 57 +- + core/include/drivers/stm32_etzpc.h | 72 +- + core/include/drivers/stm32_exti.h | 73 + + core/include/drivers/stm32_firewall.h | 212 + + core/include/drivers/stm32_gpio.h | 179 +- + core/include/drivers/stm32_i2c.h | 88 +- + core/include/drivers/stm32_iwdg.h | 33 + + core/include/drivers/stm32_lptimer.h | 48 + + core/include/drivers/stm32_rng.h | 32 +- + core/include/drivers/stm32_rtc.h | 60 + + core/include/drivers/stm32_tamp.h | 207 + + core/include/drivers/stm32_tim.h | 19 + + core/include/drivers/stm32_uart.h | 3 +- + core/include/drivers/stm32mp13_rcc.h | 1879 +++++ + core/include/drivers/stm32mp1_rcc.h | 12 +- + core/include/drivers/stm32mp_dt_bindings.h | 26 + + core/include/drivers/stpmic1.h | 74 +- + .../include/dt-bindings/clock/stm32mp1-clks.h | 46 +- + .../dt-bindings/clock/stm32mp1-clksrc.h | 284 + + .../dt-bindings/clock/stm32mp13-clks.h | 232 + + .../dt-bindings/clock/stm32mp13-clksrc.h | 396 ++ + core/include/dt-bindings/gpio/gpio.h | 6 + + core/include/dt-bindings/gpio/stm32mp_gpio.h | 15 + + .../dt-bindings/power/stm32mp1-power.h | 20 + + .../regulator/st,stm32mp15-regulator.h | 6 +- + .../dt-bindings/reset/stm32mp1-resets.h | 24 +- + .../dt-bindings/reset/stm32mp13-resets.h | 100 + + core/include/dt-bindings/rtc/rtc-stm32.h | 13 + + .../dt-bindings/soc/stm32mp-tzc400-macro.h | 29 + + .../include/dt-bindings/soc/stm32mp13-etzpc.h | 68 + + .../dt-bindings/soc/stm32mp13-tzc400.h | 33 + + .../dt-bindings/soc/stm32mp15-etzpc.h} | 34 +- + .../dt-bindings/soc/stm32mp15-tzc400.h | 35 + + core/include/io.h | 10 + + core/include/kernel/dt.h | 63 + + core/include/kernel/dt_driver.h | 18 +- + core/include/kernel/interrupt.h | 17 + + core/include/kernel/panic.h | 6 + + core/include/optee_rpc_cmd.h | 12 + + core/include/tee/tui.h | 51 + + core/kernel/dt.c | 49 + + core/kernel/dt_driver.c | 67 +- + core/kernel/interrupt.c | 10 + + core/kernel/notif.c | 1 - + core/kernel/panic.c | 15 +- + core/kernel/tee_ta_manager.c | 4 +- + core/mm/mobj.c | 5 +- + core/pta/scmi.c | 126 +- + core/pta/sub.mk | 1 + + core/pta/tui.c | 213 + + core/tee/entry_std.c | 2 +- + core/tee/sub.mk | 1 + + core/tee/tee_svc.c | 15 + + core/tee/tui.c | 177 + + .../devicetree/bindings/arm/cpus.yaml | 544 ++ + .../devicetree/bindings/arm/psci.yaml | 268 + + .../bindings/arm/stm32/st,stm32-syscon.yaml | 60 + + .../devicetree/bindings/arm/stm32/stm32.yaml | 108 + + .../bindings/clock/clock-bindings.txt | 186 + + .../bindings/clock/fixed-clock.yaml | 87 + + .../bindings/clock/st,stm32-calibration.yaml | 61 + + .../bindings/clock/st,stm32mp13-rcc.yaml | 487 ++ + .../devicetree/bindings/counter/counter.yaml | 36 + + .../bindings/crypto/st,stm32-cryp.yaml | 53 + + .../bindings/crypto/st,stm32-hash.yaml | 80 + + .../bindings/crypto/st,stm32-pka.yaml | 47 + + .../bindings/crypto/st,stm32-saes.yaml | 47 + + .../bindings/display/st,stm32-ltdc.yaml | 78 + + .../hwmon/st,stm32-hse-monitoring.yaml | 38 + + .../bindings/hwmon/st,stm32-tamp.yaml | 91 + + .../devicetree/bindings/i2c/st,stm32-i2c.yaml | 150 + + .../interrupt-controller/arm,gic.yaml | 242 + + .../interrupt-controller/st,stm32-exti.yaml | 120 + + .../bindings/mfd/st,stm32-lptimer.yaml | 147 + + .../bindings/mfd/st,stm32-timers.yaml | 159 + + .../devicetree/bindings/mfd/st,stpmic1.yaml | 377 + + .../devicetree/bindings/nvmem/nvmem.yaml | 103 + + .../bindings/nvmem/st,stm32-romem.yaml | 86 + + .../devicetree/bindings/opp/opp-v2-base.yaml | 214 + + .../devicetree/bindings/opp/opp-v2.yaml | 475 ++ + .../bindings/pinctrl/st,stm32-pinctrl.yaml | 305 + + .../bindings/power/power-domain.yaml | 133 + + .../bindings/regulator/fixed-regulator.yaml | 139 + + .../bindings/regulator/regulator.yaml | 288 + + .../bindings/regulator/st,stm32-vrefbuf.yaml | 56 + + .../regulator/st,stm32mp1-pwr-reg.yaml | 104 + + .../reserved-memory/reserved-memory.txt | 171 + + .../devicetree/bindings/reset/reset.txt | 75 + + .../devicetree/bindings/rng/st,stm32-rng.yaml | 54 + + .../devicetree/bindings/rtc/st,stm32-rtc.yaml | 180 + + .../devicetree/bindings/serial/rs485.yaml | 65 + + .../devicetree/bindings/serial/serial.yaml | 153 + + .../bindings/serial/st,stm32-uart.yaml | 138 + + .../bindings/soc/stm32/st,stm32-etzpc.yaml | 76 + + .../bindings/soc/stm32/st,stm32mp1-tzc.yaml | 88 + + .../bindings/watchdog/st,stm32-iwdg.yaml | 67 + + .../bindings/watchdog/watchdog.yaml | 27 + + keys/default_rproc.pem | 27 + + lib/libpng/CHANGES | 6109 +++++++++++++++++ + lib/libpng/INSTALL | 465 ++ + lib/libpng/LICENSE | 134 + + lib/libpng/README | 183 + + lib/libpng/TODO | 23 + + lib/libpng/include/png.h | 3247 +++++++++ + lib/libpng/include/pngconf.h | 623 ++ + lib/libpng/include/pnglibconf.h | 218 + + lib/libpng/libpng-manual.txt | 5409 +++++++++++++++ + lib/libpng/libpng.3 | 6052 ++++++++++++++++ + lib/libpng/libpngpf.3 | 24 + + lib/libpng/png.5 | 84 + + lib/libpng/png.c | 4607 +++++++++++++ + lib/libpng/pngdebug.h | 153 + + lib/libpng/pngerror.c | 963 +++ + lib/libpng/pngget.c | 1249 ++++ + lib/libpng/pnginfo.h | 267 + + lib/libpng/pngmem.c | 284 + + lib/libpng/pngpread.c | 1096 +++ + lib/libpng/pngpriv.h | 2152 ++++++ + lib/libpng/pngread.c | 4225 ++++++++++++ + lib/libpng/pngrio.c | 120 + + lib/libpng/pngrtran.c | 5044 ++++++++++++++ + lib/libpng/pngrutil.c | 4681 +++++++++++++ + lib/libpng/pngset.c | 1802 +++++ + lib/libpng/pngstruct.h | 489 ++ + lib/libpng/pngtrans.c | 864 +++ + lib/libpng/pngwio.c | 168 + + lib/libpng/pngwrite.c | 2395 +++++++ + lib/libpng/pngwtran.c | 575 ++ + lib/libpng/pngwutil.c | 2781 ++++++++ + lib/libpng/sub.mk | 22 + + lib/libutee/include/pta_bsec.h | 70 + + lib/libutee/include/pta_scmi_client.h | 69 + + lib/libutee/include/pta_tui.h | 56 + + lib/libutee/include/remoteproc_pta.h | 157 + + lib/libutee/include/stm32_pta_calib.h | 15 + + lib/libutee/include/tee_tui_api.h | 24 + + lib/libutee/include/tee_tui_api_types.h | 119 + + lib/libutee/sub.mk | 5 + + lib/libutee/tee_api_property.c | 11 + + lib/libutee/tui/font.c | 240 + + lib/libutee/tui/font.h | 40 + + lib/libutee/tui/image.c | 109 + + lib/libutee/tui/image.h | 49 + + lib/libutee/tui/image_png.c | 141 + + lib/libutee/tui/sub.mk | 38 + + lib/libutee/tui/tee_tui.c | 554 ++ + lib/libutee/tui/tui_entry.c | 502 ++ + lib/libutee/tui/tui_keyboard.c | 517 ++ + lib/libutee/tui/tui_private.h | 107 + + lib/libutee/tui/tui_pta.c | 137 + + lib/libutee/tui/tui_widget.c | 199 + + lib/libutee/tui/utf8.c | 64 + + lib/libutee/tui/utf8.h | 15 + + {core/lib/zlib => lib/libzlib}/adler32.c | 0 + lib/libzlib/crc32.c | 443 ++ + lib/libzlib/crc32.h | 443 ++ + lib/libzlib/deflate.c | 2165 ++++++ + lib/libzlib/deflate.h | 350 + + {core/lib/zlib => lib/libzlib}/gzguts.h | 0 + {core/lib/zlib => lib/libzlib}/inffast.c | 0 + {core/lib/zlib => lib/libzlib}/inffast.h | 0 + {core/lib/zlib => lib/libzlib}/inffixed.h | 0 + {core/lib/zlib => lib/libzlib}/inflate.c | 0 + {core/lib/zlib => lib/libzlib}/inflate.h | 0 + {core/lib/zlib => lib/libzlib}/inftrees.c | 0 + {core/lib/zlib => lib/libzlib}/inftrees.h | 0 + {core/lib/zlib => lib/libzlib}/sub.mk | 3 + + lib/libzlib/trees.c | 1204 ++++ + lib/libzlib/trees.h | 129 + + {core/lib/zlib => lib/libzlib}/zconf.h | 0 + {core/lib/zlib => lib/libzlib}/zlib.h | 0 + {core/lib/zlib => lib/libzlib}/zutil.c | 0 + {core/lib/zlib => lib/libzlib}/zutil.h | 0 + mk/config.mk | 23 +- + mk/gcc.mk | 6 +- + scripts/checkpatch_inc.sh | 4 +- + scripts/render_font.py | 135 + + scripts/sign_rproc_fw.py | 416 ++ + ta/mk/build-user-ta.mk | 3 + + ta/mk/ta_dev_kit.mk | 7 + + ta/remoteproc/Makefile | 18 + + ta/remoteproc/elf_parser.c | 186 + + ta/remoteproc/include/elf32.h | 243 + + ta/remoteproc/include/elf_common.h | 1014 +++ + ta/remoteproc/include/elf_parser.h | 59 + + ta/remoteproc/include/ta_remoteproc.h | 65 + + .../include/user_ta_header_defines.h | 29 + + ta/remoteproc/remoteproc_core.c | 781 +++ + ta/remoteproc/sub.mk | 3 + + ta/remoteproc/user_ta.mk | 1 + + ta/stm32mp_nvmem/Makefile | 18 + + ta/stm32mp_nvmem/include/ta_stm32mp_nvmem.h | 37 + + ta/stm32mp_nvmem/sub.mk | 3 + + ta/stm32mp_nvmem/ta_stm32mp_nvmem.c | 251 + + ta/stm32mp_nvmem/user_ta.mk | 1 + + ta/stm32mp_nvmem/user_ta_header_defines.h | 19 + + ta/ta.mk | 10 + + 358 files changed, 113944 insertions(+), 6655 deletions(-) + create mode 100644 CONTRIBUTING.md + create mode 100644 SECURITY.md + create mode 100644 core/arch/arm/dts/stm32mp13-pinctrl.dtsi + create mode 100644 core/arch/arm/dts/stm32mp131.dtsi + create mode 100644 core/arch/arm/dts/stm32mp133.dtsi + create mode 100644 core/arch/arm/dts/stm32mp135.dtsi + create mode 100644 core/arch/arm/dts/stm32mp135d-dk.dts + create mode 100644 core/arch/arm/dts/stm32mp135f-dk.dts + create mode 100644 core/arch/arm/dts/stm32mp13xa.dtsi + create mode 100644 core/arch/arm/dts/stm32mp13xc.dtsi + create mode 100644 core/arch/arm/dts/stm32mp13xd.dtsi + create mode 100644 core/arch/arm/dts/stm32mp13xf.dtsi + create mode 100644 core/arch/arm/dts/stm32mp157a-ed1.dts + create mode 100644 core/arch/arm/dts/stm32mp157a-ev1.dts + create mode 100644 core/arch/arm/dts/stm32mp157d-dk1.dts + create mode 100644 core/arch/arm/dts/stm32mp157d-ed1.dts + create mode 100644 core/arch/arm/dts/stm32mp157d-ev1.dts + create mode 100644 core/arch/arm/dts/stm32mp157f-dk2.dts + create mode 100644 core/arch/arm/dts/stm32mp157f-ed1.dts + create mode 100644 core/arch/arm/dts/stm32mp157f-ev1.dts + create mode 100644 core/arch/arm/dts/stm32mp15xa.dtsi + create mode 100644 core/arch/arm/dts/stm32mp15xd.dtsi + create mode 100644 core/arch/arm/dts/stm32mp15xf.dtsi + create mode 100644 core/arch/arm/dts/stm32mp15xx-edx.dtsi + create mode 100644 core/arch/arm/dts/stm32mp15xx-evx.dtsi + create mode 100644 core/arch/arm/plat-stm32mp1/bsec_pta.c + create mode 100644 core/arch/arm/plat-stm32mp1/cpu_opp.c + create mode 100644 core/arch/arm/plat-stm32mp1/display.c + create mode 100644 core/arch/arm/plat-stm32mp1/drivers/stm32mp1_calib.c + create mode 100644 core/arch/arm/plat-stm32mp1/drivers/stm32mp1_ddrc.c + create mode 100644 core/arch/arm/plat-stm32mp1/drivers/stm32mp1_ddrc.h + create mode 100644 core/arch/arm/plat-stm32mp1/drivers/stm32mp1_pwr_irq.c + delete mode 100644 core/arch/arm/plat-stm32mp1/nsec-service/bsec_svc.c + delete mode 100644 core/arch/arm/plat-stm32mp1/nsec-service/bsec_svc.h + create mode 100644 core/arch/arm/plat-stm32mp1/nsec-service/low_power_svc.c + create mode 100644 core/arch/arm/plat-stm32mp1/nsec-service/low_power_svc.h + create mode 100644 core/arch/arm/plat-stm32mp1/nsec-service/pwr_svc.c + create mode 100644 core/arch/arm/plat-stm32mp1/nsec-service/pwr_svc.h + create mode 100644 core/arch/arm/plat-stm32mp1/pm/context.c + create mode 100644 core/arch/arm/plat-stm32mp1/pm/context.h + create mode 100644 core/arch/arm/plat-stm32mp1/pm/context_asm_defines.c + create mode 100644 core/arch/arm/plat-stm32mp1/pm/low_power.c + create mode 100644 core/arch/arm/plat-stm32mp1/pm/pm_helpers.S + create mode 100644 core/arch/arm/plat-stm32mp1/pm/power.h + create mode 100644 core/arch/arm/plat-stm32mp1/pm/power_config.c + create mode 100644 core/arch/arm/plat-stm32mp1/remoteproc_pta.c + create mode 100644 core/arch/arm/plat-stm32mp1/rproc_pub_key.h + create mode 100644 core/arch/arm/plat-stm32mp1/stm32mp_pm.h + create mode 100644 core/arch/arm/tee/tui.c + create mode 100644 core/drivers/clk/clk-stm32-core.c + create mode 100644 core/drivers/clk/clk-stm32-core.h + create mode 100644 core/drivers/clk/clk-stm32mp13.c + create mode 100644 core/drivers/counter/counter.c + create mode 100644 core/drivers/counter/stm32_lptimer.c + create mode 100644 core/drivers/counter/stm32_tim.c + create mode 100644 core/drivers/counter/sub.mk + create mode 100644 core/drivers/crypto/stm32/ecc.c + create mode 100644 core/drivers/crypto/stm32/hash.c + create mode 100644 core/drivers/crypto/stm32/hmac.c + create mode 100644 core/drivers/crypto/stm32/stm32_hash.c + create mode 100644 core/drivers/crypto/stm32/stm32_hash.h + create mode 100644 core/drivers/crypto/stm32/stm32_pka.c + create mode 100644 core/drivers/crypto/stm32/stm32_pka.h + create mode 100644 core/drivers/crypto/stm32/stm32_saes.c + create mode 100644 core/drivers/crypto/stm32/stm32_saes.h + create mode 100644 core/drivers/firewall/stm32_etzpc.c + create mode 100644 core/drivers/firewall/stm32_firewall.c + create mode 100644 core/drivers/firewall/sub.mk + create mode 100644 core/drivers/frame_buffer.c + create mode 100644 core/drivers/regulator/core.c + create mode 100644 core/drivers/regulator/regulator_fixed.c + create mode 100644 core/drivers/regulator/stm32_regulator_gpio.c + create mode 100644 core/drivers/regulator/stm32_vrefbuf.c + create mode 100644 core/drivers/regulator/sub.mk + create mode 100644 core/drivers/scmi-msg/perf_domain.c + create mode 100644 core/drivers/scmi-msg/perf_domain.h + create mode 100644 core/drivers/scmi-msg/regulator_consumer.c + delete mode 100644 core/drivers/stm32_etzpc.c + create mode 100644 core/drivers/stm32_exti.c + create mode 100644 core/drivers/stm32_iwdg.c + create mode 100644 core/drivers/stm32_ltdc.c + create mode 100644 core/drivers/stm32_rtc.c + create mode 100644 core/drivers/stm32_tamp.c + create mode 100644 core/include/display.h + create mode 100644 core/include/drivers/counter.h + create mode 100644 core/include/drivers/frame_buffer.h + create mode 100644 core/include/drivers/regulator.h + create mode 100644 core/include/drivers/scmi_regulator_consumer.h + create mode 100644 core/include/drivers/stm32_exti.h + create mode 100644 core/include/drivers/stm32_firewall.h + create mode 100644 core/include/drivers/stm32_iwdg.h + create mode 100644 core/include/drivers/stm32_lptimer.h + create mode 100644 core/include/drivers/stm32_rtc.h + create mode 100644 core/include/drivers/stm32_tamp.h + create mode 100644 core/include/drivers/stm32_tim.h + create mode 100644 core/include/drivers/stm32mp13_rcc.h + create mode 100644 core/include/drivers/stm32mp_dt_bindings.h + create mode 100644 core/include/dt-bindings/clock/stm32mp1-clksrc.h + create mode 100644 core/include/dt-bindings/clock/stm32mp13-clks.h + create mode 100644 core/include/dt-bindings/clock/stm32mp13-clksrc.h + create mode 100644 core/include/dt-bindings/gpio/stm32mp_gpio.h + create mode 100644 core/include/dt-bindings/power/stm32mp1-power.h + create mode 100644 core/include/dt-bindings/reset/stm32mp13-resets.h + create mode 100644 core/include/dt-bindings/rtc/rtc-stm32.h + create mode 100644 core/include/dt-bindings/soc/stm32mp-tzc400-macro.h + create mode 100644 core/include/dt-bindings/soc/stm32mp13-etzpc.h + create mode 100644 core/include/dt-bindings/soc/stm32mp13-tzc400.h + rename core/{arch/arm/plat-stm32mp1/drivers/stm32mp1_etzpc.h => include/dt-bindings/soc/stm32mp15-etzpc.h} (83%) + create mode 100644 core/include/dt-bindings/soc/stm32mp15-tzc400.h + create mode 100644 core/include/tee/tui.h + create mode 100644 core/pta/tui.c + create mode 100644 core/tee/tui.c + create mode 100644 documentation/devicetree/bindings/arm/cpus.yaml + create mode 100644 documentation/devicetree/bindings/arm/psci.yaml + create mode 100644 documentation/devicetree/bindings/arm/stm32/st,stm32-syscon.yaml + create mode 100644 documentation/devicetree/bindings/arm/stm32/stm32.yaml + create mode 100644 documentation/devicetree/bindings/clock/clock-bindings.txt + create mode 100644 documentation/devicetree/bindings/clock/fixed-clock.yaml + create mode 100644 documentation/devicetree/bindings/clock/st,stm32-calibration.yaml + create mode 100644 documentation/devicetree/bindings/clock/st,stm32mp13-rcc.yaml + create mode 100644 documentation/devicetree/bindings/counter/counter.yaml + create mode 100644 documentation/devicetree/bindings/crypto/st,stm32-cryp.yaml + create mode 100644 documentation/devicetree/bindings/crypto/st,stm32-hash.yaml + create mode 100644 documentation/devicetree/bindings/crypto/st,stm32-pka.yaml + create mode 100644 documentation/devicetree/bindings/crypto/st,stm32-saes.yaml + create mode 100644 documentation/devicetree/bindings/display/st,stm32-ltdc.yaml + create mode 100644 documentation/devicetree/bindings/hwmon/st,stm32-hse-monitoring.yaml + create mode 100644 documentation/devicetree/bindings/hwmon/st,stm32-tamp.yaml + create mode 100644 documentation/devicetree/bindings/i2c/st,stm32-i2c.yaml + create mode 100644 documentation/devicetree/bindings/interrupt-controller/arm,gic.yaml + create mode 100644 documentation/devicetree/bindings/interrupt-controller/st,stm32-exti.yaml + create mode 100644 documentation/devicetree/bindings/mfd/st,stm32-lptimer.yaml + create mode 100644 documentation/devicetree/bindings/mfd/st,stm32-timers.yaml + create mode 100644 documentation/devicetree/bindings/mfd/st,stpmic1.yaml + create mode 100644 documentation/devicetree/bindings/nvmem/nvmem.yaml + create mode 100644 documentation/devicetree/bindings/nvmem/st,stm32-romem.yaml + create mode 100644 documentation/devicetree/bindings/opp/opp-v2-base.yaml + create mode 100644 documentation/devicetree/bindings/opp/opp-v2.yaml + create mode 100644 documentation/devicetree/bindings/pinctrl/st,stm32-pinctrl.yaml + create mode 100644 documentation/devicetree/bindings/power/power-domain.yaml + create mode 100644 documentation/devicetree/bindings/regulator/fixed-regulator.yaml + create mode 100644 documentation/devicetree/bindings/regulator/regulator.yaml + create mode 100644 documentation/devicetree/bindings/regulator/st,stm32-vrefbuf.yaml + create mode 100644 documentation/devicetree/bindings/regulator/st,stm32mp1-pwr-reg.yaml + create mode 100644 documentation/devicetree/bindings/reserved-memory/reserved-memory.txt + create mode 100644 documentation/devicetree/bindings/reset/reset.txt + create mode 100644 documentation/devicetree/bindings/rng/st,stm32-rng.yaml + create mode 100644 documentation/devicetree/bindings/rtc/st,stm32-rtc.yaml + create mode 100644 documentation/devicetree/bindings/serial/rs485.yaml + create mode 100644 documentation/devicetree/bindings/serial/serial.yaml + create mode 100644 documentation/devicetree/bindings/serial/st,stm32-uart.yaml + create mode 100644 documentation/devicetree/bindings/soc/stm32/st,stm32-etzpc.yaml + create mode 100644 documentation/devicetree/bindings/soc/stm32/st,stm32mp1-tzc.yaml + create mode 100644 documentation/devicetree/bindings/watchdog/st,stm32-iwdg.yaml + create mode 100644 documentation/devicetree/bindings/watchdog/watchdog.yaml + create mode 100644 keys/default_rproc.pem + create mode 100644 lib/libpng/CHANGES + create mode 100644 lib/libpng/INSTALL + create mode 100644 lib/libpng/LICENSE + create mode 100644 lib/libpng/README + create mode 100644 lib/libpng/TODO + create mode 100644 lib/libpng/include/png.h + create mode 100644 lib/libpng/include/pngconf.h + create mode 100644 lib/libpng/include/pnglibconf.h + create mode 100644 lib/libpng/libpng-manual.txt + create mode 100644 lib/libpng/libpng.3 + create mode 100644 lib/libpng/libpngpf.3 + create mode 100644 lib/libpng/png.5 + create mode 100644 lib/libpng/png.c + create mode 100644 lib/libpng/pngdebug.h + create mode 100644 lib/libpng/pngerror.c + create mode 100644 lib/libpng/pngget.c + create mode 100644 lib/libpng/pnginfo.h + create mode 100644 lib/libpng/pngmem.c + create mode 100644 lib/libpng/pngpread.c + create mode 100644 lib/libpng/pngpriv.h + create mode 100644 lib/libpng/pngread.c + create mode 100644 lib/libpng/pngrio.c + create mode 100644 lib/libpng/pngrtran.c + create mode 100644 lib/libpng/pngrutil.c + create mode 100644 lib/libpng/pngset.c + create mode 100644 lib/libpng/pngstruct.h + create mode 100644 lib/libpng/pngtrans.c + create mode 100644 lib/libpng/pngwio.c + create mode 100644 lib/libpng/pngwrite.c + create mode 100644 lib/libpng/pngwtran.c + create mode 100644 lib/libpng/pngwutil.c + create mode 100644 lib/libpng/sub.mk + create mode 100644 lib/libutee/include/pta_bsec.h + create mode 100644 lib/libutee/include/pta_tui.h + create mode 100644 lib/libutee/include/remoteproc_pta.h + create mode 100644 lib/libutee/include/stm32_pta_calib.h + create mode 100644 lib/libutee/include/tee_tui_api.h + create mode 100644 lib/libutee/include/tee_tui_api_types.h + create mode 100644 lib/libutee/tui/font.c + create mode 100644 lib/libutee/tui/font.h + create mode 100644 lib/libutee/tui/image.c + create mode 100644 lib/libutee/tui/image.h + create mode 100644 lib/libutee/tui/image_png.c + create mode 100644 lib/libutee/tui/sub.mk + create mode 100644 lib/libutee/tui/tee_tui.c + create mode 100644 lib/libutee/tui/tui_entry.c + create mode 100644 lib/libutee/tui/tui_keyboard.c + create mode 100644 lib/libutee/tui/tui_private.h + create mode 100644 lib/libutee/tui/tui_pta.c + create mode 100644 lib/libutee/tui/tui_widget.c + create mode 100644 lib/libutee/tui/utf8.c + create mode 100644 lib/libutee/tui/utf8.h + rename {core/lib/zlib => lib/libzlib}/adler32.c (100%) + create mode 100644 lib/libzlib/crc32.c + create mode 100644 lib/libzlib/crc32.h + create mode 100644 lib/libzlib/deflate.c + create mode 100644 lib/libzlib/deflate.h + rename {core/lib/zlib => lib/libzlib}/gzguts.h (100%) + rename {core/lib/zlib => lib/libzlib}/inffast.c (100%) + rename {core/lib/zlib => lib/libzlib}/inffast.h (100%) + rename {core/lib/zlib => lib/libzlib}/inffixed.h (100%) + rename {core/lib/zlib => lib/libzlib}/inflate.c (100%) + rename {core/lib/zlib => lib/libzlib}/inflate.h (100%) + rename {core/lib/zlib => lib/libzlib}/inftrees.c (100%) + rename {core/lib/zlib => lib/libzlib}/inftrees.h (100%) + rename {core/lib/zlib => lib/libzlib}/sub.mk (78%) + create mode 100644 lib/libzlib/trees.c + create mode 100644 lib/libzlib/trees.h + rename {core/lib/zlib => lib/libzlib}/zconf.h (100%) + rename {core/lib/zlib => lib/libzlib}/zlib.h (100%) + rename {core/lib/zlib => lib/libzlib}/zutil.c (100%) + rename {core/lib/zlib => lib/libzlib}/zutil.h (100%) + create mode 100755 scripts/render_font.py + create mode 100755 scripts/sign_rproc_fw.py + create mode 100644 ta/remoteproc/Makefile + create mode 100644 ta/remoteproc/elf_parser.c + create mode 100644 ta/remoteproc/include/elf32.h + create mode 100644 ta/remoteproc/include/elf_common.h + create mode 100644 ta/remoteproc/include/elf_parser.h + create mode 100644 ta/remoteproc/include/ta_remoteproc.h + create mode 100644 ta/remoteproc/include/user_ta_header_defines.h + create mode 100644 ta/remoteproc/remoteproc_core.c + create mode 100644 ta/remoteproc/sub.mk + create mode 100644 ta/remoteproc/user_ta.mk + create mode 100644 ta/stm32mp_nvmem/Makefile + create mode 100644 ta/stm32mp_nvmem/include/ta_stm32mp_nvmem.h + create mode 100644 ta/stm32mp_nvmem/sub.mk + create mode 100644 ta/stm32mp_nvmem/ta_stm32mp_nvmem.c + create mode 100644 ta/stm32mp_nvmem/user_ta.mk + create mode 100644 ta/stm32mp_nvmem/user_ta_header_defines.h + +diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md +new file mode 100644 +index 000000000..3d1bacd78 +--- /dev/null ++++ b/CONTRIBUTING.md +@@ -0,0 +1,30 @@ ++# Contributing guide ++ ++This document serves as a checklist before contributing to this repository. It includes links to read up on if topics are unclear to you. ++ ++This guide mainly focuses on the proper use of Git. ++ ++## 1. Issues ++ ++STM32MPU projects do not activate "Github issues" feature for the time being. If you need to report an issue or question about this project deliverables, you can report them using [ ST Support Center ](https://my.st.com/ols#/ols/newrequest) or [ ST Community MPU Forum ](https://community.st.com/s/topic/0TO0X0000003u2AWAQ/stm32-mpus). ++ ++## 2. Pull Requests ++ ++STMicrolectronics is happy to receive contributions from the community, based on an initial Contributor License Agreement (CLA) procedure. ++ ++* If you are an individual writing original source code and you are sure **you own the intellectual property**, then you need to sign an Individual CLA (https://cla.st.com). ++* If you work for a company that wants also to allow you to contribute with your work, your company needs to provide a Corporate CLA (https://cla.st.com) mentioning your GitHub account name. ++* If you are not sure that a CLA (Individual or Corporate) has been signed for your GitHub account you can check here (https://cla.st.com). ++ ++Please note that: ++* The Corporate CLA will always take precedence over the Individual CLA. ++* One CLA submission is sufficient, for any project proposed by STMicroelectronics. ++ ++__How to proceed__ ++ ++* We recommend to fork the project in your GitHub account to further develop your contribution. Please use the latest commit version. ++* Please, submit one Pull Request for one new feature or proposal. This will ease the analysis and final merge if accepted. ++ ++__Note__ ++ ++Merge will not be done directly in GitHub but it will need first to follow internal integration process before public deliver in a standard release. The Pull request will stay open until it is merged and delivered. +diff --git a/SECURITY.md b/SECURITY.md +new file mode 100644 +index 000000000..4b3e4e6ba +--- /dev/null ++++ b/SECURITY.md +@@ -0,0 +1,8 @@ ++# Report potential product security vulnerabilities ++ST places a high priority on security, and our Product Security Incident Response Team (PSIRT) is committed to rapidly addressing potential security vulnerabilities affecting our products. PSIRT's long history and vast experience in security allows ST to perform clear analyses and provide appropriate guidance on mitigations and solutions when applicable. ++If you wish to report potential security vulnerabilities regarding our products, **please do not report them through public GitHub issues.** Instead, we encourage you to report them to our ST PSIRT following the process described at: **https://www.st.com/content/st_com/en/security/report-vulnerabilities.html** ++ ++### IMPORTANT - READ CAREFULLY: ++STMicroelectronics International N.V., on behalf of itself, its affiliates and subsidiaries, (collectively “ST”) takes all potential security vulnerability reports or other related communications (“Report(s)”) seriously. In order to review Your Report (the terms “You” and “Yours” include your employer, and all affiliates, subsidiaries and related persons or entities) and take actions as deemed appropriate, ST requires that we have the rights and Your permission to do so. ++As such, by submitting Your Report to ST, You agree that You have the right to do so, and You grant to ST the rights to use the Report for purposes related to security vulnerability analysis, testing, correction, patching, reporting and any other related purpose or function. ++By submitting Your Report, You agree that ST’s [Privacy Policy](https://www.st.com/content/st_com/en/common/privacy-portal.html) applies to all related communications. +diff --git a/core/arch/arm/dts/stm32mp13-pinctrl.dtsi b/core/arch/arm/dts/stm32mp13-pinctrl.dtsi +new file mode 100644 +index 000000000..304d663d0 +--- /dev/null ++++ b/core/arch/arm/dts/stm32mp13-pinctrl.dtsi +@@ -0,0 +1,161 @@ ++// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) ++/* ++ * Copyright (C) STMicroelectronics 2019 - All Rights Reserved ++ * Author: Alexandre Torgue ++ */ ++#include ++ ++&pinctrl { ++ i2c4_pins_a: i2c4-0 { ++ pins { ++ pinmux = , /* I2C4_SCL */ ++ ; /* I2C4_SDA */ ++ bias-disable; ++ drive-open-drain; ++ slew-rate = <0>; ++ }; ++ }; ++ ++ rcc_mco_pins_a: rcc-pins-0 { ++ pins { ++ pinmux = ; /* RCC_MCO_1 */ ++ bias-disable; ++ drive-push-pull; ++ slew-rate = <1>; ++ }; ++ }; ++ ++ ltdc_pins_a: ltdc-0 { ++ pins { ++ pinmux = , /* LCD_CLK */ ++ , /* LCD_HSYNC */ ++ , /* LCD_VSYNC */ ++ , /* LCD_DE */ ++ , /* LCD_R2 */ ++ , /* LCD_R3 */ ++ , /* LCD_R4 */ ++ , /* LCD_R5 */ ++ , /* LCD_R6 */ ++ , /* LCD_R7 */ ++ , /* LCD_G2 */ ++ , /* LCD_G3 */ ++ , /* LCD_G4 */ ++ , /* LCD_G5 */ ++ , /* LCD_G6 */ ++ , /* LCD_G7 */ ++ , /* LCD_B2 */ ++ , /* LCD_B3 */ ++ , /* LCD_B4 */ ++ , /* LCD_B5 */ ++ , /* LCD_B6 */ ++ ; /* LCD_B7 */ ++ bias-disable; ++ drive-push-pull; ++ slew-rate = <0>; ++ }; ++ }; ++ ++ tamp0_in1_pin_a: tamp0_in1_pin_0 { ++ pins { ++ pinmux = ; ++ st,tamp_id = <1>; ++ }; ++ }; ++ ++ tamp0_in2_pin_a: tamp0_in2_pin_0 { ++ pins { ++ pinmux = ; ++ st,tamp_id = <2>; ++ }; ++ }; ++ ++ tamp0_in3_pin_a: tamp0_in3_pin_0 { ++ pins { ++ pinmux = ; ++ st,tamp_id = <3>; ++ }; ++ }; ++ ++ tamp0_in5_pin_a: tamp0_in5_pin_0 { ++ pins { ++ pinmux = ; ++ st,tamp_id = <5>; ++ }; ++ }; ++ ++ tamp0_in8_pin_a: tamp0_in8_pin_0 { ++ pins { ++ pinmux = ; ++ st,tamp_id = <8>; ++ }; ++ }; ++ ++ tamp0_out1_pin_a: tamp0_out1_pin_0 { ++ pins { ++ pinmux = ; ++ st,tamp_id = <1>; ++ }; ++ }; ++ ++ uart4_pins_a: uart4-0 { ++ pins1 { ++ pinmux = ; /* UART4_TX */ ++ bias-disable; ++ drive-push-pull; ++ slew-rate = <0>; ++ }; ++ pins2 { ++ pinmux = ; /* UART4_RX */ ++ bias-disable; ++ }; ++ }; ++ ++ usart1_pins_a: usart1-0 { ++ pins1 { ++ pinmux = , /* USART1_TX */ ++ ; /* USART1_RTS */ ++ bias-disable; ++ drive-push-pull; ++ slew-rate = <0>; ++ }; ++ pins2 { ++ pinmux = , /* USART1_RX */ ++ ; /* USART1_CTS_NSS */ ++ bias-pull-up; ++ }; ++ }; ++ ++ uart8_pins_a: uart8-0 { ++ pins1 { ++ pinmux = ; /* UART8_TX */ ++ bias-disable; ++ drive-push-pull; ++ slew-rate = <0>; ++ }; ++ pins2 { ++ pinmux = ; /* UART8_RX */ ++ bias-pull-up; ++ }; ++ }; ++ ++ wakeup_pins: wakeup_pins { ++ wakeup_pin_1: pins1 { ++ pinmux = ; ++ }; ++ wakeup_pin_2: pins2 { ++ pinmux = ; ++ }; ++ wakeup_pin_3: pins3 { ++ pinmux = ; ++ }; ++ wakeup_pin_4: pins4 { ++ pinmux = ; ++ }; ++ wakeup_pin_5: pins5 { ++ pinmux = ; ++ }; ++ wakeup_pin_6: pins6 { ++ pinmux = ; ++ }; ++ }; ++}; +diff --git a/core/arch/arm/dts/stm32mp131.dtsi b/core/arch/arm/dts/stm32mp131.dtsi +new file mode 100644 +index 000000000..80e854fdb +--- /dev/null ++++ b/core/arch/arm/dts/stm32mp131.dtsi +@@ -0,0 +1,644 @@ ++// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) ++/* ++ * Copyright (C) STMicroelectronics 2021 - All Rights Reserved ++ * Author: Alexandre Torgue for STMicroelectronics. ++ */ ++ ++#include ++#include ++#include ++ ++/ { ++ #address-cells = <1>; ++ #size-cells = <1>; ++ ++ cpus { ++ #address-cells = <1>; ++ #size-cells = <0>; ++ ++ cpu0: cpu@0 { ++ compatible = "arm,cortex-a7"; ++ device_type = "cpu"; ++ reg = <0>; ++ clocks = <&rcc CK_MPU>; ++ clock-names = "cpu"; ++ operating-points-v2 = <&cpu0_opp_table>; ++ nvmem-cells = <&part_number_otp>; ++ nvmem-cell-names = "part_number"; ++ }; ++ }; ++ ++ cpu0_opp_table: cpu0-opp-table { ++ compatible = "operating-points-v2"; ++ }; ++ ++ intc: interrupt-controller@a0021000 { ++ compatible = "arm,cortex-a7-gic"; ++ #interrupt-cells = <3>; ++ interrupt-controller; ++ reg = <0xa0021000 0x1000>, ++ <0xa0022000 0x2000>; ++ }; ++ ++ psci { ++ compatible = "arm,psci-1.0"; ++ method = "smc"; ++ }; ++ ++ clocks { ++ clk_hse: clk-hse { ++ #clock-cells = <0>; ++ compatible = "fixed-clock"; ++ clock-frequency = <24000000>; ++ }; ++ ++ clk_hsi: clk-hsi { ++ #clock-cells = <0>; ++ compatible = "fixed-clock"; ++ clock-frequency = <64000000>; ++ }; ++ ++ clk_lse: clk-lse { ++ #clock-cells = <0>; ++ compatible = "fixed-clock"; ++ clock-frequency = <32768>; ++ }; ++ ++ clk_lsi: clk-lsi { ++ #clock-cells = <0>; ++ compatible = "fixed-clock"; ++ clock-frequency = <32000>; ++ }; ++ ++ clk_csi: clk-csi { ++ #clock-cells = <0>; ++ compatible = "fixed-clock"; ++ clock-frequency = <4000000>; ++ }; ++ ++ clk_i2sin: clk-i2sin { ++ #clock-cells = <0>; ++ compatible = "fixed-clock"; ++ clock-frequency = <19000000>; ++ }; ++ }; ++ ++ scmi_regu: scmi-regu { ++ compatible = "st,scmi-regulator-consumer"; ++ scmi-channel-id = <0>; ++ ++ scmi_reg11: voltd-reg11 { ++ voltd-name = "reg11"; ++ }; ++ ++ scmi_reg18: voltd-reg18 { ++ voltd-name = "reg18"; ++ }; ++ ++ scmi_usb33: voltd-usb33 { ++ voltd-name = "usb33"; ++ }; ++ }; ++ ++ hse_monitor: hse-monitor { ++ compatible = "st,freq-monitor"; ++ counter = <&lptimer3 1 1 0 0>; ++ status = "disabled"; ++ }; ++ ++ osc_calibration: osc-calibration { ++ compatible = "st,osc-calibration"; ++ ++ csi_calibration: csi-calibration { ++ compatible = "st,csi-cal"; ++ counter = <&timers12 0 2>; ++ status = "disabled"; ++ }; ++ ++ hsi_calibration: hsi-calibration { ++ compatible = "st,hsi-cal"; ++ counter = <&timers12 0 1>; ++ status = "disabled"; ++ }; ++ }; ++ ++ soc { ++ compatible = "simple-bus"; ++ #address-cells = <1>; ++ #size-cells = <1>; ++ interrupt-parent = <&intc>; ++ ranges; ++ ++ usart3: serial@4000f000 { ++ compatible = "st,stm32h7-uart"; ++ reg = <0x4000f000 0x400>; ++ interrupts = ; ++ clocks = <&rcc USART3_K>; ++ resets = <&rcc USART3_R>; ++ status = "disabled"; ++ }; ++ ++ uart4: serial@40010000 { ++ compatible = "st,stm32h7-uart"; ++ reg = <0x40010000 0x400>; ++ interrupts = ; ++ clocks = <&rcc UART4_K>; ++ resets = <&rcc UART4_R>; ++ status = "disabled"; ++ }; ++ ++ uart5: serial@40011000 { ++ compatible = "st,stm32h7-uart"; ++ reg = <0x40011000 0x400>; ++ interrupts = ; ++ clocks = <&rcc UART5_K>; ++ resets = <&rcc UART5_R>; ++ status = "disabled"; ++ }; ++ ++ uart7: serial@40018000 { ++ compatible = "st,stm32h7-uart"; ++ reg = <0x40018000 0x400>; ++ interrupts = ; ++ clocks = <&rcc UART7_K>; ++ resets = <&rcc UART7_R>; ++ status = "disabled"; ++ }; ++ ++ uart8: serial@40019000 { ++ compatible = "st,stm32h7-uart"; ++ reg = <0x40019000 0x400>; ++ interrupts = ; ++ clocks = <&rcc UART8_K>; ++ resets = <&rcc UART8_R>; ++ status = "disabled"; ++ }; ++ ++ usart6: serial@44003000 { ++ compatible = "st,stm32h7-uart"; ++ reg = <0x44003000 0x400>; ++ interrupts = ; ++ clocks = <&rcc USART6_K>; ++ resets = <&rcc USART6_R>; ++ status = "disabled"; ++ }; ++ ++ rcc: rcc@50000000 { ++ compatible = "st,stm32mp13-rcc", "syscon"; ++ reg = <0x50000000 0x1000>; ++ #address-cells = <1>; ++ #size-cells = <0>; ++ #clock-cells = <1>; ++ #reset-cells = <1>; ++ clocks = <&clk_hse>, <&clk_hsi>, <&clk_lse>, <&clk_lsi>, <&clk_csi>, <&clk_i2sin>; ++ clock-names = "clk-hse", "clk-hsi", "clk-lse", "clk-lsi", "clk-csi", "clk-i2sin"; ++ interrupts = ; ++ secure-interrupts = ; ++ secure-interrupt-names = "wakeup"; ++ }; ++ ++ pwr_regulators: pwr@50001000 { ++ compatible = "st,stm32mp1,pwr-reg"; ++ reg = <0x50001000 0x10>; ++ ++ reg11: reg11 { ++ regulator-name = "reg11"; ++ regulator-min-microvolt = <1100000>; ++ regulator-max-microvolt = <1100000>; ++ }; ++ ++ reg18: reg18 { ++ regulator-name = "reg18"; ++ regulator-min-microvolt = <1800000>; ++ regulator-max-microvolt = <1800000>; ++ }; ++ ++ usb33: usb33 { ++ regulator-name = "usb33"; ++ regulator-min-microvolt = <3300000>; ++ regulator-max-microvolt = <3300000>; ++ }; ++ }; ++ ++ pwr_irq: pwr@50001010 { ++ compatible = "st,stm32mp1,pwr-irq"; ++ status = "disabled"; ++ }; ++ ++ exti: interrupt-controller@5000d000 { ++ compatible = "st,stm32mp13-exti", "syscon"; ++ interrupt-controller; ++ #interrupt-cells = <2>; ++ reg = <0x5000d000 0x400>; ++ }; ++ ++ ++ syscfg: syscon@50020000 { ++ compatible = "st,stm32mp157-syscfg", "syscon"; ++ reg = <0x50020000 0x400>; ++ clocks = <&rcc SYSCFG>; ++ }; ++ ++ iwdg2: watchdog@5a002000 { ++ compatible = "st,stm32mp1-iwdg"; ++ reg = <0x5a002000 0x400>; ++ interrupts = ; ++ clocks = <&rcc IWDG2>, <&rcc CK_LSI>; ++ clock-names = "pclk", "lsi"; ++ status = "disabled"; ++ }; ++ ++ rtc: rtc@5c004000 { ++ compatible = "st,stm32mp13-rtc"; ++ reg = <0x5c004000 0x400>; ++ clocks = <&rcc RTCAPB>, <&rcc RTC>; ++ clock-names = "pclk", "rtc_ck"; ++ interrupts-extended = <&exti 19 IRQ_TYPE_LEVEL_HIGH>; ++ status = "disabled"; ++ }; ++ ++ bsec: efuse@5c005000 { ++ compatible = "st,stm32mp13-bsec"; ++ reg = <0x5c005000 0x400>; ++ clocks = <&rcc BSEC>; ++ #address-cells = <1>; ++ #size-cells = <1>; ++ ++ cfg0_otp: cfg0_otp@0 { ++ reg = <0x0 0x2>; ++ }; ++ part_number_otp: part_number_otp@4 { ++ reg = <0x4 0x2>; ++ }; ++ monotonic_otp: monotonic_otp@10 { ++ reg = <0x10 0x4>; ++ }; ++ nand_otp: cfg9_otp@24 { ++ reg = <0x24 0x4>; ++ }; ++ uid_otp: uid_otp@34 { ++ reg = <0x34 0xc>; ++ }; ++ hw2_otp: hw2_otp@48 { ++ reg = <0x48 0x4>; ++ }; ++ ts_cal1: calib@5c { ++ reg = <0x5c 0x2>; ++ }; ++ ts_cal2: calib@5e { ++ reg = <0x5e 0x2>; ++ }; ++ pkh_otp: pkh_otp@60 { ++ reg = <0x60 0x20>; ++ }; ++ ethernet_mac1_address: mac1@e4 { ++ reg = <0xe4 0xc>; ++ st,non-secure-otp; ++ }; ++ /* ++ * Not defined in secure context, alignment issue ++ * Adding mac2 size in mac1 reg ++ * ++ ethernet_mac2_address: mac2@ea { ++ reg = <0xea 0x8>; ++ }; ++ */ ++ oem_enc_key: oem_enc_key@170 { ++ reg = <0x170 0x10>; ++ }; ++ }; ++ ++ tzc400: tzc@5c006000 { ++ compatible = "st,stm32mp1-tzc"; ++ reg = <0x5c006000 0x1000>; ++ interrupts = ; ++ clocks = <&rcc TZC>; ++ }; ++ ++ tamp: tamp@5c00a000 { ++ compatible = "st,stm32mp13-tamp"; ++ reg = <0x5c00a000 0x400>; ++ interrupts = ; ++ clocks = <&rcc RTCAPB>; ++ }; ++ ++ pinctrl: pin-controller@50002000 { ++ #address-cells = <1>; ++ #size-cells = <1>; ++ compatible = "st,stm32mp135-pinctrl"; ++ ranges = <0 0x50002000 0x8400>; ++ interrupt-parent = <&exti>; ++ st,syscfg = <&exti 0x60 0xff>; ++ pins-are-numbered; ++ ++ gpioa: gpio@50002000 { ++ gpio-controller; ++ #gpio-cells = <2>; ++ interrupt-controller; ++ #interrupt-cells = <2>; ++ reg = <0x0 0x400>; ++ clocks = <&rcc GPIOA>; ++ st,bank-name = "GPIOA"; ++ ngpios = <16>; ++ gpio-ranges = <&pinctrl 0 0 16>; ++ }; ++ ++ gpiob: gpio@50003000 { ++ gpio-controller; ++ #gpio-cells = <2>; ++ interrupt-controller; ++ #interrupt-cells = <2>; ++ reg = <0x1000 0x400>; ++ clocks = <&rcc GPIOB>; ++ st,bank-name = "GPIOB"; ++ ngpios = <16>; ++ gpio-ranges = <&pinctrl 0 16 16>; ++ }; ++ ++ gpioc: gpio@50004000 { ++ gpio-controller; ++ #gpio-cells = <2>; ++ interrupt-controller; ++ #interrupt-cells = <2>; ++ reg = <0x2000 0x400>; ++ clocks = <&rcc GPIOC>; ++ st,bank-name = "GPIOC"; ++ ngpios = <16>; ++ gpio-ranges = <&pinctrl 0 32 16>; ++ }; ++ ++ gpiod: gpio@50005000 { ++ gpio-controller; ++ #gpio-cells = <2>; ++ interrupt-controller; ++ #interrupt-cells = <2>; ++ reg = <0x3000 0x400>; ++ clocks = <&rcc GPIOD>; ++ st,bank-name = "GPIOD"; ++ ngpios = <16>; ++ gpio-ranges = <&pinctrl 0 48 16>; ++ }; ++ ++ gpioe: gpio@50006000 { ++ gpio-controller; ++ #gpio-cells = <2>; ++ interrupt-controller; ++ #interrupt-cells = <2>; ++ reg = <0x4000 0x400>; ++ clocks = <&rcc GPIOE>; ++ st,bank-name = "GPIOE"; ++ ngpios = <16>; ++ gpio-ranges = <&pinctrl 0 64 16>; ++ }; ++ ++ gpiof: gpio@50007000 { ++ gpio-controller; ++ #gpio-cells = <2>; ++ interrupt-controller; ++ #interrupt-cells = <2>; ++ reg = <0x5000 0x400>; ++ clocks = <&rcc GPIOF>; ++ st,bank-name = "GPIOF"; ++ ngpios = <16>; ++ gpio-ranges = <&pinctrl 0 80 16>; ++ }; ++ ++ gpiog: gpio@50008000 { ++ gpio-controller; ++ #gpio-cells = <2>; ++ interrupt-controller; ++ #interrupt-cells = <2>; ++ reg = <0x6000 0x400>; ++ clocks = <&rcc GPIOG>; ++ st,bank-name = "GPIOG"; ++ ngpios = <16>; ++ gpio-ranges = <&pinctrl 0 96 16>; ++ }; ++ ++ gpioh: gpio@50009000 { ++ gpio-controller; ++ #gpio-cells = <2>; ++ interrupt-controller; ++ #interrupt-cells = <2>; ++ reg = <0x7000 0x400>; ++ clocks = <&rcc GPIOH>; ++ st,bank-name = "GPIOH"; ++ ngpios = <15>; ++ gpio-ranges = <&pinctrl 0 112 15>; ++ }; ++ ++ gpioi: gpio@5000a000 { ++ gpio-controller; ++ #gpio-cells = <2>; ++ interrupt-controller; ++ #interrupt-cells = <2>; ++ reg = <0x8000 0x400>; ++ clocks = <&rcc GPIOI>; ++ st,bank-name = "GPIOI"; ++ ngpios = <8>; ++ gpio-ranges = <&pinctrl 0 128 8>; ++ }; ++ }; ++ ++ etzpc: etzpc@5c007000 { ++ compatible = "st,stm32-etzpc", "firewall-bus"; ++ reg = <0x5C007000 0x400>; ++ clocks = <&rcc TZPC>; ++ #address-cells = <1>; ++ #size-cells = <1>; ++ ++ usart1: serial@4c000000 { ++ compatible = "st,stm32h7-uart"; ++ reg = <0x4c000000 0x400>; ++ interrupts = ; ++ clocks = <&rcc USART1_K>; ++ resets = <&rcc USART1_R>; ++ status = "disabled"; ++ }; ++ ++ usart2: serial@4c001000 { ++ compatible = "st,stm32h7-uart"; ++ reg = <0x4c001000 0x400>; ++ interrupts = ; ++ clocks = <&rcc USART2_K>; ++ resets = <&rcc USART2_R>; ++ status = "disabled"; ++ }; ++ ++ i2c3: i2c@4c004000 { ++ compatible = "st,stm32mp13-i2c"; ++ reg = <0x4c004000 0x400>; ++ clocks = <&rcc I2C3_K>; ++ resets = <&rcc I2C3_R>; ++ #address-cells = <1>; ++ #size-cells = <0>; ++ st,syscfg-fmp = <&syscfg 0x4 0x4>; ++ i2c-analog-filter; ++ status = "disabled"; ++ }; ++ ++ i2c4: i2c@4c005000 { ++ compatible = "st,stm32mp13-i2c"; ++ reg = <0x4c005000 0x400>; ++ clocks = <&rcc I2C4_K>; ++ resets = <&rcc I2C4_R>; ++ #address-cells = <1>; ++ #size-cells = <0>; ++ st,syscfg-fmp = <&syscfg 0x4 0x8>; ++ i2c-analog-filter; ++ status = "disabled"; ++ }; ++ ++ i2c5: i2c@4c006000 { ++ compatible = "st,stm32mp13-i2c"; ++ reg = <0x4c006000 0x400>; ++ clocks = <&rcc I2C5_K>; ++ resets = <&rcc I2C5_R>; ++ #address-cells = <1>; ++ #size-cells = <0>; ++ st,syscfg-fmp = <&syscfg 0x4 0x10>; ++ i2c-analog-filter; ++ status = "disabled"; ++ }; ++ ++ timers12: timer@4c007000 { ++ #address-cells = <1>; ++ #size-cells = <0>; ++ compatible = "st,stm32-timers"; ++ reg = <0x4c007000 0x400>; ++ interrupts = ; ++ clocks = <&rcc TIM12_K>; ++ clock-names = "int"; ++ ++ counter { ++ compatible = "st,stm32-timer-counter"; ++ status = "disabled"; ++ }; ++ }; ++ ++ timers13: timer@4c008000 { ++ #address-cells = <1>; ++ #size-cells = <0>; ++ compatible = "st,stm32-timers"; ++ reg = <0x4c008000 0x400>; ++ clocks = <&rcc TIM13_K>; ++ clock-names = "int"; ++ status = "disabled"; ++ }; ++ ++ timers14: timer@4c009000 { ++ #address-cells = <1>; ++ #size-cells = <0>; ++ compatible = "st,stm32-timers"; ++ reg = <0x4c009000 0x400>; ++ clocks = <&rcc TIM14_K>; ++ clock-names = "int"; ++ status = "disabled"; ++ }; ++ ++ timers15: timer@4c00a000 { ++ #address-cells = <1>; ++ #size-cells = <0>; ++ compatible = "st,stm32-timers"; ++ reg = <0x4c00a000 0x400>; ++ interrupts = ; ++ clocks = <&rcc TIM15_K>; ++ clock-names = "int"; ++ status = "disabled"; ++ ++ counter { ++ compatible = "st,stm32-timer-counter"; ++ status = "disabled"; ++ }; ++ }; ++ ++ timers16: timer@4c00b000 { ++ #address-cells = <1>; ++ #size-cells = <0>; ++ compatible = "st,stm32-timers"; ++ reg = <0x4c00b000 0x400>; ++ clocks = <&rcc TIM16_K>; ++ clock-names = "int"; ++ status = "disabled"; ++ ++ }; ++ ++ timers17: timer@4c00c000 { ++ #address-cells = <1>; ++ #size-cells = <0>; ++ compatible = "st,stm32-timers"; ++ reg = <0x4c00c000 0x400>; ++ clocks = <&rcc TIM17_K>; ++ clock-names = "int"; ++ status = "disabled"; ++ }; ++ ++ lptimer2: timer@50021000 { ++ #address-cells = <1>; ++ #size-cells = <0>; ++ compatible = "st,stm32-lptimer"; ++ reg = <0x50021000 0x400>; ++ interrupts = ; ++ clocks = <&rcc LPTIM2_K>; ++ clock-names = "mux"; ++ status = "disabled"; ++ }; ++ ++ lptimer3: timer@50022000 { ++ #address-cells = <1>; ++ #size-cells = <0>; ++ compatible = "st,stm32-lptimer"; ++ reg = <0x50022000 0x400>; ++ interrupts = ; ++ clocks = <&rcc LPTIM3_K>; ++ clock-names = "mux"; ++ status = "disabled"; ++ ++ counter { ++ compatible = "st,stm32-lptimer-counter"; ++ status = "disabled"; ++ }; ++ }; ++ ++ vrefbuf: vrefbuf@50025000 { ++ compatible = "st,stm32mp13-vrefbuf"; ++ reg = <0x50025000 0x8>; ++ regulator-name = "vrefbuf"; ++ regulator-min-microvolt = <1650000>; ++ regulator-max-microvolt = <2500000>; ++ clocks = <&rcc VREF>; ++ status = "disabled"; ++ }; ++ ++ hash: hash@54003000 { ++ compatible = "st,stm32mp13-hash"; ++ reg = <0x54003000 0x400>; ++ clocks = <&rcc HASH1>; ++ resets = <&rcc HASH1_R>; ++ status = "disabled"; ++ }; ++ ++ rng: rng@54004000 { ++ compatible = "st,stm32mp13-rng"; ++ reg = <0x54004000 0x400>; ++ clocks = <&rcc RNG1_K>; ++ resets = <&rcc RNG1_R>; ++ status = "disabled"; ++ }; ++ ++ iwdg1: watchdog@5c003000 { ++ compatible = "st,stm32mp1-iwdg"; ++ reg = <0x5C003000 0x400>; ++ interrupts = ; ++ clocks = <&rcc IWDG1>, <&rcc CK_LSI>; ++ clock-names = "pclk", "lsi"; ++ status = "disabled"; ++ }; ++ ++ stgen: stgen@5c008000 { ++ compatible = "st,stm32-stgen"; ++ reg = <0x5C008000 0x1000>; ++ }; ++ }; ++ }; ++}; +diff --git a/core/arch/arm/dts/stm32mp133.dtsi b/core/arch/arm/dts/stm32mp133.dtsi +new file mode 100644 +index 000000000..58eb120af +--- /dev/null ++++ b/core/arch/arm/dts/stm32mp133.dtsi +@@ -0,0 +1,7 @@ ++// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) ++/* ++ * Copyright (C) STMicroelectronics 2021 - All Rights Reserved ++ * Author: Alexandre Torgue for STMicroelectronics. ++ */ ++ ++#include "stm32mp131.dtsi" +diff --git a/core/arch/arm/dts/stm32mp135.dtsi b/core/arch/arm/dts/stm32mp135.dtsi +new file mode 100644 +index 000000000..3cde21895 +--- /dev/null ++++ b/core/arch/arm/dts/stm32mp135.dtsi +@@ -0,0 +1,24 @@ ++// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) ++/* ++ * Copyright (C) STMicroelectronics 2021 - All Rights Reserved ++ * Author: Alexandre Torgue for STMicroelectronics. ++ */ ++ ++#include "stm32mp133.dtsi" ++ ++/ { ++ soc { ++ etzpc: etzpc@5c007000 { ++ ltdc: display-controller@5a001000 { ++ compatible = "st,stm32-ltdc"; ++ reg = <0x5a001000 0x400>; ++ interrupts = , ++ ; ++ clocks = <&rcc LTDC_PX>; ++ clock-names = "lcd"; ++ resets = <&rcc LTDC_R>; ++ status = "disabled"; ++ }; ++ }; ++ }; ++}; +diff --git a/core/arch/arm/dts/stm32mp135d-dk.dts b/core/arch/arm/dts/stm32mp135d-dk.dts +new file mode 100644 +index 000000000..a4ea42488 +--- /dev/null ++++ b/core/arch/arm/dts/stm32mp135d-dk.dts +@@ -0,0 +1,630 @@ ++// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) ++/* ++ * Copyright (C) STMicroelectronics 2022 - All Rights Reserved ++ * Author: Alexandre Torgue for STMicroelectronics. ++ */ ++ ++/dts-v1/; ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include "stm32mp135.dtsi" ++#include "stm32mp13xd.dtsi" ++#include "stm32mp13-pinctrl.dtsi" ++ ++/ { ++ model = "STMicroelectronics STM32MP135D-DK Discovery Board"; ++ compatible = "st,stm32mp135d-dk", "st,stm32mp135"; ++ ++ aliases { ++ serial0 = &uart4; ++ serial1 = &usart1; ++ }; ++ ++ chosen { ++ stdout-path = "serial0:115200n8"; ++ }; ++ ++ memory@c0000000 { ++ device_type = "memory"; ++ reg = <0xc0000000 0x20000000>; ++ }; ++ ++ reserved-memory { ++ #address-cells = <1>; ++ #size-cells = <1>; ++ ranges; ++ ++ optee_framebuffer: optee-framebuffer@dd000000 { ++ /* Secure framebuffer memory */ ++ reg = <0xdd000000 0x1000000>; ++ st,protreg = ; ++ }; ++ }; ++ ++ vin: vin { ++ compatible = "regulator-fixed"; ++ regulator-name = "vin"; ++ regulator-min-microvolt = <5000000>; ++ regulator-max-microvolt = <5000000>; ++ regulator-always-on; ++ }; ++ ++ v3v3_ao: v3v3_ao { ++ compatible = "regulator-fixed"; ++ regulator-name = "v3v3_ao"; ++ regulator-min-microvolt = <3300000>; ++ regulator-max-microvolt = <3300000>; ++ regulator-always-on; ++ }; ++}; ++ ++&bsec { ++ board_id: board_id@f0 { ++ reg = <0xf0 0x4>; ++ st,non-secure-otp; ++ }; ++}; ++ ++&cpu0 { ++ cpu-supply = <&vddcpu>; ++}; ++ ++&etzpc { ++ st,decprot = < ++ DECPROT(STM32MP1_ETZPC_ADC1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_ADC2_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_BKPSRAM_ID, DECPROT_S_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_DCMIPP_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_DDRCTRLPHY_ID, DECPROT_NS_R_S_W, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_ETH1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_ETH2_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_FMC_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_HASH_ID, DECPROT_S_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_I2C3_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_I2C4_ID, DECPROT_S_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_I2C5_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_IWDG1_ID, DECPROT_S_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_LPTIM2_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_LPTIM3_ID, DECPROT_S_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_LTDC_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_MCE_ID, DECPROT_S_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_OTG_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_QSPI_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_RNG_ID, DECPROT_S_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_SDMMC1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_SDMMC2_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_SPI4_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_SPI5_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_SRAM1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_SRAM2_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_SRAM3_ID, DECPROT_S_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_STGENC_ID, DECPROT_S_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_TIM12_ID, DECPROT_S_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_TIM13_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_TIM14_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_TIM15_ID, DECPROT_S_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_TIM16_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_TIM17_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_USART1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_USART2_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_USBPHYCTRL_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_VREFBUF_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ >; ++}; ++ ++&gpiob { ++ st,protreg = < (TZPROT(9)) >; ++}; ++ ++&gpiod { ++ st,protreg = < (TZPROT(7)) >; ++}; ++ ++&gpioe { ++ st,protreg = < (TZPROT(15)) >; ++}; ++ ++&gpiof { ++ st,protreg = < (TZPROT(8)) >; ++}; ++ ++&hash { ++ status = "okay"; ++}; ++ ++&hse_monitor { ++ status = "okay"; ++}; ++ ++&i2c4 { ++ pinctrl-names = "default"; ++ pinctrl-0 = <&i2c4_pins_a>; ++ i2c-scl-rising-time-ns = <185>; ++ i2c-scl-falling-time-ns = <20>; ++ clock-frequency = <400000>; ++ status = "okay"; ++ ++ pmic: stpmic@33 { ++ compatible = "st,stpmic1"; ++ reg = <0x33>; ++ status = "okay"; ++ st,wakeup-pin-number = <1>; ++ ++ regulators { ++ compatible = "st,stpmic1-regulators"; ++ buck1-supply = <&vin>; ++ buck2-supply = <&vin>; ++ buck3-supply = <&vin>; ++ buck4-supply = <&vin>; ++ ldo1-supply = <&vin>; ++ ldo4-supply = <&vin>; ++ ldo5-supply = <&vin>; ++ ldo6-supply = <&vin>; ++ vref_ddr-supply = <&vin>; ++ pwr_sw1-supply = <&bst_out>; ++ pwr_sw2-supply = <&v3v3_ao>; ++ ++ vddcpu: buck1 { ++ regulator-name = "vddcpu"; ++ regulator-min-microvolt = <1250000>; ++ regulator-max-microvolt = <1350000>; ++ regulator-always-on; ++ regulator-over-current-protection; ++ ++ lp-stop { ++ regulator-suspend-microvolt = <1250000>; ++ }; ++ lplv-stop { ++ regulator-suspend-microvolt = <900000>; ++ }; ++ lplv-stop2 { ++ regulator-off-in-suspend; ++ }; ++ standby-ddr-sr { ++ regulator-off-in-suspend; ++ }; ++ standby-ddr-off { ++ regulator-off-in-suspend; ++ }; ++ }; ++ ++ vdd_ddr: buck2 { ++ regulator-name = "vdd_ddr"; ++ regulator-min-microvolt = <1350000>; ++ regulator-max-microvolt = <1350000>; ++ regulator-always-on; ++ regulator-over-current-protection; ++ ++ standby-ddr-off { ++ regulator-off-in-suspend; ++ }; ++ }; ++ ++ vdd: buck3 { ++ regulator-name = "vdd"; ++ regulator-min-microvolt = <3300000>; ++ regulator-max-microvolt = <3300000>; ++ regulator-always-on; ++ st,mask-reset; ++ regulator-over-current-protection; ++ }; ++ ++ vddcore: buck4 { ++ regulator-name = "vddcore"; ++ regulator-min-microvolt = <1250000>; ++ regulator-max-microvolt = <1250000>; ++ regulator-always-on; ++ regulator-over-current-protection; ++ ++ lplv-stop { ++ regulator-suspend-microvolt = <900000>; ++ }; ++ lplv-stop2 { ++ regulator-suspend-microvolt = <900000>; ++ }; ++ standby-ddr-sr { ++ regulator-off-in-suspend; ++ }; ++ standby-ddr-off { ++ regulator-off-in-suspend; ++ }; ++ }; ++ ++ vdd_adc: ldo1 { ++ regulator-name = "vdd_adc"; ++ regulator-min-microvolt = <3300000>; ++ regulator-max-microvolt = <3300000>; ++ ++ standby-ddr-sr { ++ regulator-off-in-suspend; ++ }; ++ standby-ddr-off { ++ regulator-off-in-suspend; ++ }; ++ }; ++ ++ unused1: ldo2 { ++ regulator-name = "ldo2"; ++ }; ++ ++ unused2: ldo3 { ++ regulator-name = "ldo3"; ++ }; ++ ++ vdd_usb: ldo4 { ++ regulator-name = "vdd_usb"; ++ regulator-min-microvolt = <3300000>; ++ regulator-max-microvolt = <3300000>; ++ ++ standby-ddr-sr { ++ regulator-off-in-suspend; ++ }; ++ standby-ddr-off { ++ regulator-off-in-suspend; ++ }; ++ }; ++ ++ vdd_sd: ldo5 { ++ regulator-name = "vdd_sd"; ++ regulator-min-microvolt = <3300000>; ++ regulator-max-microvolt = <3300000>; ++ regulator-boot-on; ++ ++ standby-ddr-sr { ++ regulator-off-in-suspend; ++ }; ++ standby-ddr-off { ++ regulator-off-in-suspend; ++ }; ++ }; ++ ++ v1v8_periph: ldo6 { ++ regulator-name = "v1v8_periph"; ++ regulator-min-microvolt = <1800000>; ++ regulator-max-microvolt = <1800000>; ++ ++ standby-ddr-sr { ++ regulator-off-in-suspend; ++ }; ++ standby-ddr-off { ++ regulator-off-in-suspend; ++ }; ++ }; ++ ++ vref_ddr: vref_ddr { ++ regulator-name = "vref_ddr"; ++ regulator-always-on; ++ ++ standby-ddr-sr { ++ regulator-off-in-suspend; ++ }; ++ standby-ddr-off { ++ regulator-off-in-suspend; ++ }; ++ }; ++ ++ bst_out: boost { ++ regulator-name = "bst_out"; ++ }; ++ ++ v3v3_sw: pwr_sw2 { ++ regulator-name = "v3v3_sw"; ++ regulator-active-discharge = <1>; ++ regulator-min-microvolt = <3300000>; ++ regulator-max-microvolt = <3300000>; ++ }; ++ }; ++ }; ++}; ++ ++&iwdg2 { ++ timeout-sec = <32>; ++ secure-timeout-sec = <5>; ++ status = "okay"; ++}; ++ ++&lptimer3 { ++ status = "okay"; ++ ++ counter { ++ status = "okay"; ++ }; ++}; ++ ++<dc { ++ pinctrl-names = "default"; ++ pinctrl-0 = <<dc_pins_a>; ++ status = "okay"; ++}; ++ ++&oem_enc_key { ++ st,non-secure-otp-provisioning; ++}; ++ ++&osc_calibration { ++ csi-calibration { ++ status = "okay"; ++ }; ++ ++ hsi-calibration { ++ status = "okay"; ++ }; ++}; ++ ++&pwr_irq { ++ pinctrl-names = "default"; ++ pinctrl-0 = <&wakeup_pins>; ++ status = "okay"; ++}; ++ ++&pwr_regulators { ++ system_suspend_supported_soc_modes = < ++ STM32_PM_CSLEEP_RUN ++ STM32_PM_CSTOP_ALLOW_LP_STOP ++ STM32_PM_CSTOP_ALLOW_LPLV_STOP ++ STM32_PM_CSTOP_ALLOW_LPLV_STOP2 ++ STM32_PM_CSTOP_ALLOW_STANDBY_DDR_SR ++ >; ++ ++ system_off_soc_mode = ; ++ vdd-supply = <&vdd>; ++ vdd_3v3_usbfs-supply = <&vdd_usb>; ++}; ++ ++&rcc { ++ compatible = "st,stm32mp13-rcc", "syscon", "st,stm32mp13-rcc-mco"; ++ pinctrl-0 = <&rcc_mco_pins_a>; ++ pinctrl-names = "default"; ++ ++ st,clksrc = < ++ CLK_MPU_PLL1P ++ CLK_AXI_PLL2P ++ CLK_MLAHBS_PLL3 ++ CLK_RTC_LSE ++ CLK_MCO1_HSE ++ CLK_MCO2_DISABLED ++ CLK_CKPER_HSE ++ CLK_ETH1_PLL4P ++ CLK_ETH2_PLL4P ++ CLK_SDMMC1_PLL4P ++ CLK_SDMMC2_PLL4P ++ CLK_STGEN_HSE ++ CLK_USBPHY_HSE ++ CLK_I2C4_HSI ++ CLK_USBO_USBPHY ++ CLK_ADC2_CKPER ++ CLK_I2C12_HSI ++ CLK_UART1_HSI ++ CLK_UART2_HSI ++ CLK_UART35_HSI ++ CLK_UART4_HSI ++ CLK_UART6_HSI ++ CLK_UART78_HSI ++ CLK_DCMIPP_PLL2Q ++ CLK_LPTIM3_PCLK3 ++ CLK_RNG1_PLL4R ++ >; ++ ++ st,clkdiv = < ++ DIV(DIV_MPU, 1) ++ DIV(DIV_AXI, 0) ++ DIV(DIV_MLAHB, 0) ++ DIV(DIV_APB1, 1) ++ DIV(DIV_APB2, 1) ++ DIV(DIV_APB3, 1) ++ DIV(DIV_APB4, 1) ++ DIV(DIV_APB5, 2) ++ DIV(DIV_APB6, 1) ++ DIV(DIV_RTC, 0) ++ DIV(DIV_MCO1, 0) ++ DIV(DIV_MCO2, 0) ++ >; ++ ++ st,pll_vco { ++ pll1_vco_2000Mhz: pll1-vco-2000Mhz { ++ src = < CLK_PLL12_HSE >; ++ divmn = < 1 82 >; ++ frac = < 0xAAA >; ++ }; ++ ++ pll1_vco_1300Mhz: pll1-vco-1300Mhz { ++ src = < CLK_PLL12_HSE >; ++ divmn = < 2 80 >; ++ frac = < 0x800 >; ++ }; ++ ++ pll2_vco_1066Mhz: pll2-vco-1066Mhz { ++ src = < CLK_PLL12_HSE >; ++ divmn = < 2 65 >; ++ frac = < 0x1400 >; ++ }; ++ ++ pll3_vco_417_8Mhz: pll3-vco-417_8Mhz { ++ src = < CLK_PLL3_HSE >; ++ divmn = < 1 33 >; ++ frac = < 0x1a04 >; ++ }; ++ ++ pll4_vco_600Mhz: pll4-vco-600Mhz { ++ src = < CLK_PLL4_HSE >; ++ divmn = < 1 49 >; ++ }; ++ }; ++ ++ /* VCO = 1300.0 MHz => P = 650 (CPU) */ ++ pll1:st,pll@0 { ++ compatible = "st,stm32mp1-pll"; ++ reg = <0>; ++ ++ st,pll = < &pll1_cfg1 >; ++ ++ pll1_cfg1: pll1_cfg1 { ++ st,pll_vco = < &pll1_vco_1300Mhz >; ++ st,pll_div_pqr = < 0 1 1 >; ++ }; ++ ++ pll1_cfg2: pll1_cfg2 { ++ st,pll_vco = < &pll1_vco_2000Mhz >; ++ st,pll_div_pqr = < 0 1 1 >; ++ }; ++ }; ++ ++ /* VCO = 1066.0 MHz => P = 266 (AXI), Q = 266, R = 533 (DDR) */ ++ pll2:st,pll@1 { ++ compatible = "st,stm32mp1-pll"; ++ reg = <1>; ++ ++ st,pll = < &pll2_cfg1 >; ++ ++ pll2_cfg1: pll2_cfg1 { ++ st,pll_vco = < &pll2_vco_1066Mhz >; ++ st,pll_div_pqr = < 1 1 0 >; ++ }; ++ }; ++ ++ /* VCO = 417.8 MHz => P = 209, Q = 24, R = 11 */ ++ pll3:st,pll@2 { ++ compatible = "st,stm32mp1-pll"; ++ reg = <2>; ++ ++ st,pll = < &pll3_cfg1 >; ++ ++ pll3_cfg1: pll3_cfg1 { ++ st,pll_vco = < &pll3_vco_417_8Mhz >; ++ st,pll_div_pqr = < 1 16 36 >; ++ }; ++ }; ++ ++ /* VCO = 600.0 MHz => P = 50, Q = 10, R = 50 */ ++ pll4:st,pll@3 { ++ compatible = "st,stm32mp1-pll"; ++ reg = <3>; ++ st,pll = < &pll4_cfg1 >; ++ ++ pll4_cfg1: pll4_cfg1 { ++ st,pll_vco = < &pll4_vco_600Mhz >; ++ st,pll_div_pqr = < 11 59 11 >; ++ }; ++ }; ++ ++ st,clk_opp { ++ /* CK_MPU clock config for MP13 */ ++ st,ck_mpu { ++ ++ cfg_1 { ++ hz = < 1000000000 >; ++ st,clksrc = < CLK_MPU_PLL1P >; ++ st,pll = < &pll1_cfg2 >; ++ }; ++ ++ cfg_2 { ++ hz = < 650000000 >; ++ st,clksrc = < CLK_MPU_PLL1P >; ++ st,pll = < &pll1_cfg1 >; ++ }; ++ }; ++ }; ++}; ++ ++&rng { ++ status = "okay"; ++ clock-error-detect; ++}; ++ ++&rtc { ++ status = "okay"; ++}; ++ ++&scmi_regu { ++ scmi_vddcpu: voltd-vddcpu { ++ voltd-name = "vddcpu"; ++ regulator-name = "vddcpu"; ++ }; ++ scmi_vdd: voltd-vdd { ++ voltd-name = "vdd"; ++ regulator-name = "vdd"; ++ }; ++ scmi_vddcore: voltd-vddcore { ++ voltd-name = "vddcore"; ++ regulator-name = "vddcore"; ++ }; ++ scmi_vdd_adc: voltd-vdd_adc { ++ voltd-name = "vdd_adc"; ++ regulator-name = "vdd_adc"; ++ }; ++ scmi_vdd_usb: voltd-vdd_usb { ++ voltd-name = "vdd_usb"; ++ regulator-name = "vdd_usb"; ++ }; ++ scmi_vdd_sd: voltd-vdd_sd { ++ voltd-name = "vdd_sd"; ++ regulator-name = "vdd_sd"; ++ }; ++ scmi_v1v8_periph: voltd-v1v8_periph { ++ voltd-name = "v1v8_periph"; ++ regulator-name = "v1v8_periph"; ++ }; ++ scmi_v3v3_sw: voltd-v3v3_sw { ++ voltd-name = "v3v3_sw"; ++ regulator-name = "v3v3_sw"; ++ }; ++}; ++ ++&tamp { ++ status = "okay"; ++ st,tamp_passive_nb_sample = <4>; ++ st,tamp_passive_sample_clk_div = <16384>; ++ ++ tamp_passive@2 { ++ pinctrl-0 = <&tamp0_in2_pin_a>; ++ status = "okay"; ++ }; ++ ++ /* Connect pin8 and pin22 from CN3 */ ++ tamp_active@1 { ++ pinctrl-0 = <&tamp0_in3_pin_a &tamp0_out1_pin_a>; ++ status = "disabled"; ++ }; ++}; ++ ++&timers12 { ++ status = "okay"; ++ ++ counter { ++ status = "okay"; ++ }; ++}; ++ ++&tzc400 { ++ memory-region = <&optee_framebuffer>; ++}; ++ ++&uart4 { ++ pinctrl-names = "default"; ++ pinctrl-0 = <&uart4_pins_a>; ++ status = "okay"; ++}; ++ ++&usart1 { ++ pinctrl-names = "default"; ++ pinctrl-0 = <&usart1_pins_a>; ++ uart-has-rtscts; ++ status = "disabled"; ++}; ++ ++&uart8 { ++ pinctrl-names = "default"; ++ pinctrl-0 = <&uart8_pins_a>; ++ status = "disabled"; ++}; ++ ++&wakeup_pin_1 { ++ bias-pull-up; ++}; +diff --git a/core/arch/arm/dts/stm32mp135f-dk.dts b/core/arch/arm/dts/stm32mp135f-dk.dts +new file mode 100644 +index 000000000..d40194d85 +--- /dev/null ++++ b/core/arch/arm/dts/stm32mp135f-dk.dts +@@ -0,0 +1,642 @@ ++// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) ++/* ++ * Copyright (C) STMicroelectronics 2021 - All Rights Reserved ++ * Author: Alexandre Torgue for STMicroelectronics. ++ */ ++ ++/dts-v1/; ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include "stm32mp135.dtsi" ++#include "stm32mp13xf.dtsi" ++#include "stm32mp13-pinctrl.dtsi" ++ ++/ { ++ model = "STMicroelectronics STM32MP135F-DK Discovery Board"; ++ compatible = "st,stm32mp135f-dk", "st,stm32mp135"; ++ ++ aliases { ++ serial0 = &uart4; ++ serial1 = &usart1; ++ }; ++ ++ chosen { ++ stdout-path = "serial0:115200n8"; ++ }; ++ ++ memory@c0000000 { ++ device_type = "memory"; ++ reg = <0xc0000000 0x20000000>; ++ }; ++ ++ reserved-memory { ++ #address-cells = <1>; ++ #size-cells = <1>; ++ ranges; ++ ++ optee_framebuffer: optee-framebuffer@dd000000 { ++ /* Secure framebuffer memory */ ++ reg = <0xdd000000 0x1000000>; ++ st,protreg = ; ++ }; ++ }; ++ ++ vin: vin { ++ compatible = "regulator-fixed"; ++ regulator-name = "vin"; ++ regulator-min-microvolt = <5000000>; ++ regulator-max-microvolt = <5000000>; ++ regulator-always-on; ++ }; ++ ++ v3v3_ao: v3v3_ao { ++ compatible = "regulator-fixed"; ++ regulator-name = "v3v3_ao"; ++ regulator-min-microvolt = <3300000>; ++ regulator-max-microvolt = <3300000>; ++ regulator-always-on; ++ }; ++}; ++ ++&bsec { ++ board_id: board_id@f0 { ++ reg = <0xf0 0x4>; ++ st,non-secure-otp; ++ }; ++}; ++ ++&cpu0 { ++ cpu-supply = <&vddcpu>; ++}; ++ ++&etzpc { ++ st,decprot = < ++ DECPROT(STM32MP1_ETZPC_ADC1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_ADC2_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_BKPSRAM_ID, DECPROT_S_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_CRYP_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_DCMIPP_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_DDRCTRLPHY_ID, DECPROT_NS_R_S_W, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_ETH1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_ETH2_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_FMC_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_HASH_ID, DECPROT_S_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_I2C3_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_I2C4_ID, DECPROT_S_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_I2C5_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_IWDG1_ID, DECPROT_S_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_LPTIM2_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_LPTIM3_ID, DECPROT_S_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_LTDC_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_MCE_ID, DECPROT_S_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_OTG_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_PKA_ID, DECPROT_S_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_QSPI_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_RNG_ID, DECPROT_S_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_SAES_ID, DECPROT_S_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_SDMMC1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_SDMMC2_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_SPI4_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_SPI5_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_SRAM1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_SRAM2_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_SRAM3_ID, DECPROT_S_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_STGENC_ID, DECPROT_S_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_TIM12_ID, DECPROT_S_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_TIM13_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_TIM14_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_TIM15_ID, DECPROT_S_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_TIM16_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_TIM17_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_USART1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_USART2_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_USBPHYCTRL_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_VREFBUF_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ >; ++}; ++ ++&gpiob { ++ st,protreg = < (TZPROT(9)) >; ++}; ++ ++&gpiod { ++ st,protreg = < (TZPROT(7)) >; ++}; ++ ++&gpioe { ++ st,protreg = < (TZPROT(15)) >; ++}; ++ ++&gpiof { ++ st,protreg = < (TZPROT(8)) >; ++}; ++ ++&hash { ++ status = "okay"; ++}; ++ ++&hse_monitor { ++ status = "okay"; ++}; ++ ++&i2c4 { ++ pinctrl-names = "default"; ++ pinctrl-0 = <&i2c4_pins_a>; ++ i2c-scl-rising-time-ns = <185>; ++ i2c-scl-falling-time-ns = <20>; ++ clock-frequency = <400000>; ++ status = "okay"; ++ ++ pmic: stpmic@33 { ++ compatible = "st,stpmic1"; ++ reg = <0x33>; ++ status = "okay"; ++ st,wakeup-pin-number = <1>; ++ ++ regulators { ++ compatible = "st,stpmic1-regulators"; ++ buck1-supply = <&vin>; ++ buck2-supply = <&vin>; ++ buck3-supply = <&vin>; ++ buck4-supply = <&vin>; ++ ldo1-supply = <&vin>; ++ ldo4-supply = <&vin>; ++ ldo5-supply = <&vin>; ++ ldo6-supply = <&vin>; ++ vref_ddr-supply = <&vin>; ++ pwr_sw1-supply = <&bst_out>; ++ pwr_sw2-supply = <&v3v3_ao>; ++ ++ vddcpu: buck1 { ++ regulator-name = "vddcpu"; ++ regulator-min-microvolt = <1250000>; ++ regulator-max-microvolt = <1350000>; ++ regulator-always-on; ++ regulator-over-current-protection; ++ ++ lp-stop { ++ regulator-suspend-microvolt = <1250000>; ++ }; ++ lplv-stop { ++ regulator-suspend-microvolt = <900000>; ++ }; ++ lplv-stop2 { ++ regulator-off-in-suspend; ++ }; ++ standby-ddr-sr { ++ regulator-off-in-suspend; ++ }; ++ standby-ddr-off { ++ regulator-off-in-suspend; ++ }; ++ }; ++ ++ vdd_ddr: buck2 { ++ regulator-name = "vdd_ddr"; ++ regulator-min-microvolt = <1350000>; ++ regulator-max-microvolt = <1350000>; ++ regulator-always-on; ++ regulator-over-current-protection; ++ ++ standby-ddr-off { ++ regulator-off-in-suspend; ++ }; ++ }; ++ ++ vdd: buck3 { ++ regulator-name = "vdd"; ++ regulator-min-microvolt = <3300000>; ++ regulator-max-microvolt = <3300000>; ++ regulator-always-on; ++ st,mask-reset; ++ regulator-over-current-protection; ++ }; ++ ++ vddcore: buck4 { ++ regulator-name = "vddcore"; ++ regulator-min-microvolt = <1250000>; ++ regulator-max-microvolt = <1250000>; ++ regulator-always-on; ++ regulator-over-current-protection; ++ ++ lplv-stop { ++ regulator-suspend-microvolt = <900000>; ++ }; ++ lplv-stop2 { ++ regulator-suspend-microvolt = <900000>; ++ }; ++ standby-ddr-sr { ++ regulator-off-in-suspend; ++ }; ++ standby-ddr-off { ++ regulator-off-in-suspend; ++ }; ++ }; ++ ++ vdd_adc: ldo1 { ++ regulator-name = "vdd_adc"; ++ regulator-min-microvolt = <3300000>; ++ regulator-max-microvolt = <3300000>; ++ ++ standby-ddr-sr { ++ regulator-off-in-suspend; ++ }; ++ standby-ddr-off { ++ regulator-off-in-suspend; ++ }; ++ }; ++ ++ unused1: ldo2 { ++ regulator-name = "ldo2"; ++ }; ++ ++ unused2: ldo3 { ++ regulator-name = "ldo3"; ++ }; ++ ++ vdd_usb: ldo4 { ++ regulator-name = "vdd_usb"; ++ regulator-min-microvolt = <3300000>; ++ regulator-max-microvolt = <3300000>; ++ ++ standby-ddr-sr { ++ regulator-off-in-suspend; ++ }; ++ standby-ddr-off { ++ regulator-off-in-suspend; ++ }; ++ }; ++ ++ vdd_sd: ldo5 { ++ regulator-name = "vdd_sd"; ++ regulator-min-microvolt = <3300000>; ++ regulator-max-microvolt = <3300000>; ++ regulator-boot-on; ++ ++ standby-ddr-sr { ++ regulator-off-in-suspend; ++ }; ++ standby-ddr-off { ++ regulator-off-in-suspend; ++ }; ++ }; ++ ++ v1v8_periph: ldo6 { ++ regulator-name = "v1v8_periph"; ++ regulator-min-microvolt = <1800000>; ++ regulator-max-microvolt = <1800000>; ++ ++ standby-ddr-sr { ++ regulator-off-in-suspend; ++ }; ++ standby-ddr-off { ++ regulator-off-in-suspend; ++ }; ++ }; ++ ++ vref_ddr: vref_ddr { ++ regulator-name = "vref_ddr"; ++ regulator-always-on; ++ ++ standby-ddr-sr { ++ regulator-off-in-suspend; ++ }; ++ standby-ddr-off { ++ regulator-off-in-suspend; ++ }; ++ }; ++ ++ bst_out: boost { ++ regulator-name = "bst_out"; ++ }; ++ ++ v3v3_sw: pwr_sw2 { ++ regulator-name = "v3v3_sw"; ++ regulator-active-discharge = <1>; ++ regulator-min-microvolt = <3300000>; ++ regulator-max-microvolt = <3300000>; ++ }; ++ }; ++ }; ++}; ++ ++&iwdg2 { ++ timeout-sec = <32>; ++ secure-timeout-sec = <5>; ++ status = "okay"; ++}; ++ ++&lptimer3 { ++ status = "okay"; ++ ++ counter { ++ status = "okay"; ++ }; ++}; ++ ++<dc { ++ pinctrl-names = "default"; ++ pinctrl-0 = <<dc_pins_a>; ++ status = "okay"; ++}; ++ ++&oem_enc_key { ++ st,non-secure-otp-provisioning; ++}; ++ ++&osc_calibration { ++ csi-calibration { ++ status = "okay"; ++ }; ++ ++ hsi-calibration { ++ status = "okay"; ++ }; ++}; ++ ++&pka { ++ status = "okay"; ++}; ++ ++&pwr_irq { ++ pinctrl-names = "default"; ++ pinctrl-0 = <&wakeup_pins>; ++ status = "okay"; ++}; ++ ++&pwr_regulators { ++ system_suspend_supported_soc_modes = < ++ STM32_PM_CSLEEP_RUN ++ STM32_PM_CSTOP_ALLOW_LP_STOP ++ STM32_PM_CSTOP_ALLOW_LPLV_STOP ++ STM32_PM_CSTOP_ALLOW_LPLV_STOP2 ++ STM32_PM_CSTOP_ALLOW_STANDBY_DDR_SR ++ >; ++ ++ system_off_soc_mode = ; ++ vdd-supply = <&vdd>; ++ vdd_3v3_usbfs-supply = <&vdd_usb>; ++}; ++ ++&rcc { ++ compatible = "st,stm32mp13-rcc", "syscon", "st,stm32mp13-rcc-mco"; ++ pinctrl-0 = <&rcc_mco_pins_a>; ++ pinctrl-names = "default"; ++ ++ st,clksrc = < ++ CLK_MPU_PLL1P ++ CLK_AXI_PLL2P ++ CLK_MLAHBS_PLL3 ++ CLK_RTC_LSE ++ CLK_MCO1_HSE ++ CLK_MCO2_DISABLED ++ CLK_CKPER_HSE ++ CLK_ETH1_PLL4P ++ CLK_ETH2_PLL4P ++ CLK_SDMMC1_PLL4P ++ CLK_SDMMC2_PLL4P ++ CLK_STGEN_HSE ++ CLK_USBPHY_HSE ++ CLK_I2C4_HSI ++ CLK_USBO_USBPHY ++ CLK_ADC2_CKPER ++ CLK_I2C12_HSI ++ CLK_UART1_HSI ++ CLK_UART2_HSI ++ CLK_UART35_HSI ++ CLK_UART4_HSI ++ CLK_UART6_HSI ++ CLK_UART78_HSI ++ CLK_SAES_AXI ++ CLK_DCMIPP_PLL2Q ++ CLK_LPTIM3_PCLK3 ++ CLK_RNG1_PLL4R ++ >; ++ ++ st,clkdiv = < ++ DIV(DIV_MPU, 1) ++ DIV(DIV_AXI, 0) ++ DIV(DIV_MLAHB, 0) ++ DIV(DIV_APB1, 1) ++ DIV(DIV_APB2, 1) ++ DIV(DIV_APB3, 1) ++ DIV(DIV_APB4, 1) ++ DIV(DIV_APB5, 2) ++ DIV(DIV_APB6, 1) ++ DIV(DIV_RTC, 0) ++ DIV(DIV_MCO1, 0) ++ DIV(DIV_MCO2, 0) ++ >; ++ ++ st,pll_vco { ++ pll1_vco_2000Mhz: pll1-vco-2000Mhz { ++ src = < CLK_PLL12_HSE >; ++ divmn = < 1 82 >; ++ frac = < 0xAAA >; ++ }; ++ ++ pll1_vco_1300Mhz: pll1-vco-1300Mhz { ++ src = < CLK_PLL12_HSE >; ++ divmn = < 2 80 >; ++ frac = < 0x800 >; ++ }; ++ ++ pll2_vco_1066Mhz: pll2-vco-1066Mhz { ++ src = < CLK_PLL12_HSE >; ++ divmn = < 2 65 >; ++ frac = < 0x1400 >; ++ }; ++ ++ pll3_vco_417_8Mhz: pll3-vco-417_8Mhz { ++ src = < CLK_PLL3_HSE >; ++ divmn = < 1 33 >; ++ frac = < 0x1a04 >; ++ }; ++ ++ pll4_vco_600Mhz: pll4-vco-600Mhz { ++ src = < CLK_PLL4_HSE >; ++ divmn = < 1 49 >; ++ }; ++ }; ++ ++ /* VCO = 1300.0 MHz => P = 650 (CPU) */ ++ pll1:st,pll@0 { ++ compatible = "st,stm32mp1-pll"; ++ reg = <0>; ++ ++ st,pll = < &pll1_cfg1 >; ++ ++ pll1_cfg1: pll1_cfg1 { ++ st,pll_vco = < &pll1_vco_1300Mhz >; ++ st,pll_div_pqr = < 0 1 1 >; ++ }; ++ ++ pll1_cfg2: pll1_cfg2 { ++ st,pll_vco = < &pll1_vco_2000Mhz >; ++ st,pll_div_pqr = < 0 1 1 >; ++ }; ++ }; ++ ++ /* VCO = 1066.0 MHz => P = 266 (AXI), Q = 266, R = 533 (DDR) */ ++ pll2:st,pll@1 { ++ compatible = "st,stm32mp1-pll"; ++ reg = <1>; ++ ++ st,pll = < &pll2_cfg1 >; ++ ++ pll2_cfg1: pll2_cfg1 { ++ st,pll_vco = < &pll2_vco_1066Mhz >; ++ st,pll_div_pqr = < 1 1 0 >; ++ }; ++ }; ++ ++ /* VCO = 417.8 MHz => P = 209, Q = 24, R = 11 */ ++ pll3:st,pll@2 { ++ compatible = "st,stm32mp1-pll"; ++ reg = <2>; ++ ++ st,pll = < &pll3_cfg1 >; ++ ++ pll3_cfg1: pll3_cfg1 { ++ st,pll_vco = < &pll3_vco_417_8Mhz >; ++ st,pll_div_pqr = < 1 16 36 >; ++ }; ++ }; ++ ++ /* VCO = 600.0 MHz => P = 50, Q = 10, R = 50 */ ++ pll4:st,pll@3 { ++ compatible = "st,stm32mp1-pll"; ++ reg = <3>; ++ st,pll = < &pll4_cfg1 >; ++ ++ pll4_cfg1: pll4_cfg1 { ++ st,pll_vco = < &pll4_vco_600Mhz >; ++ st,pll_div_pqr = < 11 59 11 >; ++ }; ++ }; ++ ++ st,clk_opp { ++ /* CK_MPU clock config for MP13 */ ++ st,ck_mpu { ++ ++ cfg_1 { ++ hz = < 1000000000 >; ++ st,clksrc = < CLK_MPU_PLL1P >; ++ st,pll = < &pll1_cfg2 >; ++ }; ++ ++ cfg_2 { ++ hz = < 650000000 >; ++ st,clksrc = < CLK_MPU_PLL1P >; ++ st,pll = < &pll1_cfg1 >; ++ }; ++ }; ++ }; ++}; ++ ++&rng { ++ status = "okay"; ++ clock-error-detect; ++}; ++ ++&rtc { ++ status = "okay"; ++}; ++ ++&saes { ++ status = "okay"; ++}; ++ ++&scmi_regu { ++ scmi_vddcpu: voltd-vddcpu { ++ voltd-name = "vddcpu"; ++ regulator-name = "vddcpu"; ++ }; ++ scmi_vdd: voltd-vdd { ++ voltd-name = "vdd"; ++ regulator-name = "vdd"; ++ }; ++ scmi_vddcore: voltd-vddcore { ++ voltd-name = "vddcore"; ++ regulator-name = "vddcore"; ++ }; ++ scmi_vdd_adc: voltd-vdd_adc { ++ voltd-name = "vdd_adc"; ++ regulator-name = "vdd_adc"; ++ }; ++ scmi_vdd_usb: voltd-vdd_usb { ++ voltd-name = "vdd_usb"; ++ regulator-name = "vdd_usb"; ++ }; ++ scmi_vdd_sd: voltd-vdd_sd { ++ voltd-name = "vdd_sd"; ++ regulator-name = "vdd_sd"; ++ }; ++ scmi_v1v8_periph: voltd-v1v8_periph { ++ voltd-name = "v1v8_periph"; ++ regulator-name = "v1v8_periph"; ++ }; ++ scmi_v3v3_sw: voltd-v3v3_sw { ++ voltd-name = "v3v3_sw"; ++ regulator-name = "v3v3_sw"; ++ }; ++}; ++ ++&tamp { ++ status = "okay"; ++ st,tamp_passive_nb_sample = <4>; ++ st,tamp_passive_sample_clk_div = <16384>; ++ ++ tamp_passive@2 { ++ pinctrl-0 = <&tamp0_in2_pin_a>; ++ status = "okay"; ++ }; ++ ++ /* Connect pin8 and pin22 from CN3 */ ++ tamp_active@1 { ++ pinctrl-0 = <&tamp0_in3_pin_a &tamp0_out1_pin_a>; ++ status = "disabled"; ++ }; ++}; ++ ++&timers12 { ++ status = "okay"; ++ ++ counter { ++ status = "okay"; ++ }; ++}; ++ ++&tzc400 { ++ memory-region = <&optee_framebuffer>; ++}; ++ ++&uart4 { ++ pinctrl-names = "default"; ++ pinctrl-0 = <&uart4_pins_a>; ++ status = "okay"; ++}; ++ ++&usart1 { ++ pinctrl-names = "default"; ++ pinctrl-0 = <&usart1_pins_a>; ++ uart-has-rtscts; ++ status = "disabled"; ++}; ++ ++&uart8 { ++ pinctrl-names = "default"; ++ pinctrl-0 = <&uart8_pins_a>; ++ status = "disabled"; ++}; ++ ++&wakeup_pin_1 { ++ bias-pull-up; ++}; +diff --git a/core/arch/arm/dts/stm32mp13xa.dtsi b/core/arch/arm/dts/stm32mp13xa.dtsi +new file mode 100644 +index 000000000..85502c0b6 +--- /dev/null ++++ b/core/arch/arm/dts/stm32mp13xa.dtsi +@@ -0,0 +1,14 @@ ++// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) ++/* ++ * Copyright (C) STMicroelectronics 2019 - All Rights Reserved ++ * Author: Alexandre Torgue for STMicroelectronics. ++ */ ++ ++&cpu0_opp_table { ++ opp-650000000 { ++ opp-hz = /bits/ 64 <650000000>; ++ opp-microvolt = <1250000>; ++ opp-supported-hw = <0x1>; ++ st,opp-default; ++ }; ++}; +diff --git a/core/arch/arm/dts/stm32mp13xc.dtsi b/core/arch/arm/dts/stm32mp13xc.dtsi +new file mode 100644 +index 000000000..f7c01c1a2 +--- /dev/null ++++ b/core/arch/arm/dts/stm32mp13xc.dtsi +@@ -0,0 +1,34 @@ ++// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) ++/* ++ * Copyright (C) STMicroelectronics 2021 - All Rights Reserved ++ * Author: Alexandre Torgue for STMicroelectronics. ++ */ ++ ++#include "stm32mp13xa.dtsi" ++ ++&etzpc { ++ cryp: crypto@54002000 { ++ compatible = "st,stm32mp1-cryp"; ++ reg = <0x54002000 0x400>; ++ interrupts = ; ++ clocks = <&rcc CRYP1>; ++ resets = <&rcc CRYP1_R>; ++ status = "disabled"; ++ }; ++ ++ saes: saes@54005000 { ++ compatible = "st,stm32mp13-saes"; ++ reg = <0x54005000 0x400>; ++ clocks = <&rcc SAES_K>; ++ resets = <&rcc SAES_R>; ++ status = "disabled"; ++ }; ++ ++ pka: pka@54006000 { ++ compatible = "st,stm32mp13-pka64"; ++ reg = <0x54006000 0x2000>; ++ clocks = <&rcc PKA>; ++ resets = <&rcc PKA_R>; ++ status = "disabled"; ++ }; ++}; +diff --git a/core/arch/arm/dts/stm32mp13xd.dtsi b/core/arch/arm/dts/stm32mp13xd.dtsi +new file mode 100644 +index 000000000..fe2b45b21 +--- /dev/null ++++ b/core/arch/arm/dts/stm32mp13xd.dtsi +@@ -0,0 +1,20 @@ ++// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) ++/* ++ * Copyright (C) STMicroelectronics 2021 - All Rights Reserved ++ * Author: Alexandre Torgue for STMicroelectronics. ++ */ ++ ++&cpu0_opp_table { ++ opp-1000000000 { ++ opp-hz = /bits/ 64 <1000000000>; ++ opp-microvolt = <1350000>; ++ opp-supported-hw = <0x2>; ++ st,opp-default; ++ }; ++ ++ opp-650000000 { ++ opp-hz = /bits/ 64 <650000000>; ++ opp-microvolt = <1250000>; ++ opp-supported-hw = <0x2>; ++ }; ++}; +diff --git a/core/arch/arm/dts/stm32mp13xf.dtsi b/core/arch/arm/dts/stm32mp13xf.dtsi +new file mode 100644 +index 000000000..2f0a56479 +--- /dev/null ++++ b/core/arch/arm/dts/stm32mp13xf.dtsi +@@ -0,0 +1,34 @@ ++// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) ++/* ++ * Copyright (C) STMicroelectronics 2021 - All Rights Reserved ++ * Author: Alexandre Torgue for STMicroelectronics. ++ */ ++ ++#include "stm32mp13xd.dtsi" ++ ++&etzpc { ++ cryp: crypto@54002000 { ++ compatible = "st,stm32mp1-cryp"; ++ reg = <0x54002000 0x400>; ++ interrupts = ; ++ clocks = <&rcc CRYP1>; ++ resets = <&rcc CRYP1_R>; ++ status = "disabled"; ++ }; ++ ++ saes: saes@54005000 { ++ compatible = "st,stm32mp13-saes"; ++ reg = <0x54005000 0x400>; ++ clocks = <&rcc SAES_K>; ++ resets = <&rcc SAES_R>; ++ status = "disabled"; ++ }; ++ ++ pka: pka@54006000 { ++ compatible = "st,stm32mp13-pka64"; ++ reg = <0x54006000 0x2000>; ++ clocks = <&rcc PKA>; ++ resets = <&rcc PKA_R>; ++ status = "disabled"; ++ }; ++}; +diff --git a/core/arch/arm/dts/stm32mp15-pinctrl.dtsi b/core/arch/arm/dts/stm32mp15-pinctrl.dtsi +index 0237d4dda..90b055ced 100644 +--- a/core/arch/arm/dts/stm32mp15-pinctrl.dtsi ++++ b/core/arch/arm/dts/stm32mp15-pinctrl.dtsi +@@ -6,1058 +6,176 @@ + #include + + &pinctrl { +- adc1_in6_pins_a: adc1-in6 { +- pins { +- pinmux = ; +- }; +- }; +- +- adc12_ain_pins_a: adc12-ain-0 { +- pins { +- pinmux = , /* ADC1 in13 */ +- , /* ADC1 in6 */ +- , /* ADC2 in2 */ +- ; /* ADC2 in6 */ +- }; +- }; +- +- adc12_usb_cc_pins_a: adc12-usb-cc-pins-0 { +- pins { +- pinmux = , /* ADC12 in18 */ +- ; /* ADC12 in19 */ +- }; +- }; +- +- cec_pins_a: cec-0 { +- pins { +- pinmux = ; +- bias-disable; +- drive-open-drain; +- slew-rate = <0>; +- }; +- }; +- +- cec_pins_sleep_a: cec-sleep-0 { +- pins { +- pinmux = ; /* HDMI_CEC */ +- }; +- }; +- +- cec_pins_b: cec-1 { +- pins { +- pinmux = ; +- bias-disable; +- drive-open-drain; +- slew-rate = <0>; +- }; +- }; +- +- cec_pins_sleep_b: cec-sleep-1 { +- pins { +- pinmux = ; /* HDMI_CEC */ +- }; +- }; +- +- dac_ch1_pins_a: dac-ch1 { +- pins { +- pinmux = ; +- }; +- }; +- +- dac_ch2_pins_a: dac-ch2 { +- pins { +- pinmux = ; +- }; +- }; +- +- dcmi_pins_a: dcmi-0 { +- pins { +- pinmux = ,/* DCMI_HSYNC */ +- ,/* DCMI_VSYNC */ +- ,/* DCMI_PIXCLK */ +- ,/* DCMI_D0 */ +- ,/* DCMI_D1 */ +- ,/* DCMI_D2 */ +- ,/* DCMI_D3 */ +- ,/* DCMI_D4 */ +- ,/* DCMI_D5 */ +- ,/* DCMI_D6 */ +- ,/* DCMI_D7 */ +- ,/* DCMI_D8 */ +- ,/* DCMI_D9 */ +- ,/* DCMI_D10 */ +- ;/* DCMI_D11 */ +- bias-disable; +- }; +- }; +- +- dcmi_sleep_pins_a: dcmi-sleep-0 { +- pins { +- pinmux = ,/* DCMI_HSYNC */ +- ,/* DCMI_VSYNC */ +- ,/* DCMI_PIXCLK */ +- ,/* DCMI_D0 */ +- ,/* DCMI_D1 */ +- ,/* DCMI_D2 */ +- ,/* DCMI_D3 */ +- ,/* DCMI_D4 */ +- ,/* DCMI_D5 */ +- ,/* DCMI_D6 */ +- ,/* DCMI_D7 */ +- ,/* DCMI_D8 */ +- ,/* DCMI_D9 */ +- ,/* DCMI_D10 */ +- ;/* DCMI_D11 */ +- }; +- }; +- +- ethernet0_rgmii_pins_a: rgmii-0 { ++ uart4_pins_a: uart4-0 { + pins1 { +- pinmux = , /* ETH_RGMII_CLK125 */ +- , /* ETH_RGMII_GTX_CLK */ +- , /* ETH_RGMII_TXD0 */ +- , /* ETH_RGMII_TXD1 */ +- , /* ETH_RGMII_TXD2 */ +- , /* ETH_RGMII_TXD3 */ +- , /* ETH_RGMII_TX_CTL */ +- ; /* ETH_MDC */ +- bias-disable; +- drive-push-pull; +- slew-rate = <2>; +- }; +- pins2 { +- pinmux = ; /* ETH_MDIO */ ++ pinmux = ; /* UART4_TX */ + bias-disable; + drive-push-pull; + slew-rate = <0>; + }; +- pins3 { +- pinmux = , /* ETH_RGMII_RXD0 */ +- , /* ETH_RGMII_RXD1 */ +- , /* ETH_RGMII_RXD2 */ +- , /* ETH_RGMII_RXD3 */ +- , /* ETH_RGMII_RX_CLK */ +- ; /* ETH_RGMII_RX_CTL */ ++ pins2 { ++ pinmux = ; /* UART4_RX */ + bias-disable; + }; + }; + +- ethernet0_rgmii_pins_sleep_a: rgmii-sleep-0 { +- pins1 { +- pinmux = , /* ETH_RGMII_CLK125 */ +- , /* ETH_RGMII_GTX_CLK */ +- , /* ETH_RGMII_TXD0 */ +- , /* ETH_RGMII_TXD1 */ +- , /* ETH_RGMII_TXD2 */ +- , /* ETH_RGMII_TXD3 */ +- , /* ETH_RGMII_TX_CTL */ +- , /* ETH_MDIO */ +- , /* ETH_MDC */ +- , /* ETH_RGMII_RXD0 */ +- , /* ETH_RGMII_RXD1 */ +- , /* ETH_RGMII_RXD2 */ +- , /* ETH_RGMII_RXD3 */ +- , /* ETH_RGMII_RX_CLK */ +- ; /* ETH_RGMII_RX_CTL */ +- }; +- }; +- +- fmc_pins_a: fmc-0 { ++ uart4_pins_b: uart4-1 { + pins1 { +- pinmux = , /* FMC_NOE */ +- , /* FMC_NWE */ +- , /* FMC_A16_FMC_CLE */ +- , /* FMC_A17_FMC_ALE */ +- , /* FMC_D0 */ +- , /* FMC_D1 */ +- , /* FMC_D2 */ +- , /* FMC_D3 */ +- , /* FMC_D4 */ +- , /* FMC_D5 */ +- , /* FMC_D6 */ +- , /* FMC_D7 */ +- ; /* FMC_NE2_FMC_NCE */ ++ pinmux = ; /* UART4_TX */ + bias-disable; + drive-push-pull; +- slew-rate = <1>; +- }; +- pins2 { +- pinmux = ; /* FMC_NWAIT */ +- bias-pull-up; +- }; +- }; +- +- fmc_sleep_pins_a: fmc-sleep-0 { +- pins { +- pinmux = , /* FMC_NOE */ +- , /* FMC_NWE */ +- , /* FMC_A16_FMC_CLE */ +- , /* FMC_A17_FMC_ALE */ +- , /* FMC_D0 */ +- , /* FMC_D1 */ +- , /* FMC_D2 */ +- , /* FMC_D3 */ +- , /* FMC_D4 */ +- , /* FMC_D5 */ +- , /* FMC_D6 */ +- , /* FMC_D7 */ +- , /* FMC_NWAIT */ +- ; /* FMC_NE2_FMC_NCE */ +- }; +- }; +- +- i2c1_pins_a: i2c1-0 { +- pins { +- pinmux = , /* I2C1_SCL */ +- ; /* I2C1_SDA */ +- bias-disable; +- drive-open-drain; +- slew-rate = <0>; +- }; +- }; +- +- i2c1_pins_sleep_a: i2c1-1 { +- pins { +- pinmux = , /* I2C1_SCL */ +- ; /* I2C1_SDA */ +- }; +- }; +- +- i2c1_pins_b: i2c1-2 { +- pins { +- pinmux = , /* I2C1_SCL */ +- ; /* I2C1_SDA */ +- bias-disable; +- drive-open-drain; +- slew-rate = <0>; +- }; +- }; +- +- i2c1_pins_sleep_b: i2c1-3 { +- pins { +- pinmux = , /* I2C1_SCL */ +- ; /* I2C1_SDA */ +- }; +- }; +- +- i2c2_pins_a: i2c2-0 { +- pins { +- pinmux = , /* I2C2_SCL */ +- ; /* I2C2_SDA */ +- bias-disable; +- drive-open-drain; +- slew-rate = <0>; +- }; +- }; +- +- i2c2_pins_sleep_a: i2c2-1 { +- pins { +- pinmux = , /* I2C2_SCL */ +- ; /* I2C2_SDA */ +- }; +- }; +- +- i2c2_pins_b1: i2c2-2 { +- pins { +- pinmux = ; /* I2C2_SDA */ +- bias-disable; +- drive-open-drain; + slew-rate = <0>; + }; +- }; +- +- i2c2_pins_sleep_b1: i2c2-3 { +- pins { +- pinmux = ; /* I2C2_SDA */ +- }; +- }; +- +- i2c5_pins_a: i2c5-0 { +- pins { +- pinmux = , /* I2C5_SCL */ +- ; /* I2C5_SDA */ +- bias-disable; +- drive-open-drain; +- slew-rate = <0>; +- }; +- }; +- +- i2c5_pins_sleep_a: i2c5-1 { +- pins { +- pinmux = , /* I2C5_SCL */ +- ; /* I2C5_SDA */ +- +- }; +- }; +- +- i2s2_pins_a: i2s2-0 { +- pins { +- pinmux = , /* I2S2_SDO */ +- , /* I2S2_WS */ +- ; /* I2S2_CK */ +- slew-rate = <1>; +- drive-push-pull; ++ pins2 { ++ pinmux = ; /* UART4_RX */ + bias-disable; + }; + }; + +- i2s2_pins_sleep_a: i2s2-1 { +- pins { +- pinmux = , /* I2S2_SDO */ +- , /* I2S2_WS */ +- ; /* I2S2_CK */ +- }; +- }; +- +- ltdc_pins_a: ltdc-a-0 { +- pins { +- pinmux = , /* LCD_CLK */ +- , /* LCD_HSYNC */ +- , /* LCD_VSYNC */ +- , /* LCD_DE */ +- , /* LCD_R0 */ +- , /* LCD_R1 */ +- , /* LCD_R2 */ +- , /* LCD_R3 */ +- , /* LCD_R4 */ +- , /* LCD_R5 */ +- , /* LCD_R6 */ +- , /* LCD_R7 */ +- , /* LCD_G0 */ +- , /* LCD_G1 */ +- , /* LCD_G2 */ +- , /* LCD_G3 */ +- , /* LCD_G4 */ +- , /* LCD_G5 */ +- , /* LCD_G6 */ +- , /* LCD_G7 */ +- , /* LCD_B0 */ +- , /* LCD_B1 */ +- , /* LCD_B2 */ +- , /* LCD_B3 */ +- , /* LCD_B4 */ +- , /* LCD_B5 */ +- , /* LCD_B6 */ +- ; /* LCD_B7 */ ++ uart7_pins_a: uart7-0 { ++ pins1 { ++ pinmux = ; /* UART7_TX */ + bias-disable; + drive-push-pull; +- slew-rate = <1>; +- }; +- }; +- +- ltdc_pins_sleep_a: ltdc-a-1 { +- pins { +- pinmux = , /* LCD_CLK */ +- , /* LCD_HSYNC */ +- , /* LCD_VSYNC */ +- , /* LCD_DE */ +- , /* LCD_R0 */ +- , /* LCD_R1 */ +- , /* LCD_R2 */ +- , /* LCD_R3 */ +- , /* LCD_R4 */ +- , /* LCD_R5 */ +- , /* LCD_R6 */ +- , /* LCD_R7 */ +- , /* LCD_G0 */ +- , /* LCD_G1 */ +- , /* LCD_G2 */ +- , /* LCD_G3 */ +- , /* LCD_G4 */ +- , /* LCD_G5 */ +- , /* LCD_G6 */ +- , /* LCD_G7 */ +- , /* LCD_B0 */ +- , /* LCD_B1 */ +- , /* LCD_B2 */ +- , /* LCD_B3 */ +- , /* LCD_B4 */ +- , /* LCD_B5 */ +- , /* LCD_B6 */ +- ; /* LCD_B7 */ ++ slew-rate = <0>; + }; +- }; +- +- ltdc_pins_b: ltdc-b-0 { +- pins { +- pinmux = , /* LCD_CLK */ +- , /* LCD_HSYNC */ +- , /* LCD_VSYNC */ +- , /* LCD_DE */ +- , /* LCD_R0 */ +- , /* LCD_R1 */ +- , /* LCD_R2 */ +- , /* LCD_R3 */ +- , /* LCD_R4 */ +- , /* LCD_R5 */ +- , /* LCD_R6 */ +- , /* LCD_R7 */ +- , /* LCD_G0 */ +- , /* LCD_G1 */ +- , /* LCD_G2 */ +- , /* LCD_G3 */ +- , /* LCD_G4 */ +- , /* LCD_G5 */ +- , /* LCD_G6 */ +- , /* LCD_G7 */ +- , /* LCD_B0 */ +- , /* LCD_B1 */ +- , /* LCD_B2 */ +- , /* LCD_B3 */ +- , /* LCD_B4 */ +- , /* LCD_B5 */ +- , /* LCD_B6 */ +- ; /* LCD_B7 */ ++ pins2 { ++ pinmux = , /* UART7_RX */ ++ , /* UART7_CTS */ ++ ; /* UART7_RTS */ + bias-disable; +- drive-push-pull; +- slew-rate = <1>; + }; + }; + +- ltdc_pins_sleep_b: ltdc-b-1 { +- pins { +- pinmux = , /* LCD_CLK */ +- , /* LCD_HSYNC */ +- , /* LCD_VSYNC */ +- , /* LCD_DE */ +- , /* LCD_R0 */ +- , /* LCD_R1 */ +- , /* LCD_R2 */ +- , /* LCD_R3 */ +- , /* LCD_R4 */ +- , /* LCD_R5 */ +- , /* LCD_R6 */ +- , /* LCD_R7 */ +- , /* LCD_G0 */ +- , /* LCD_G1 */ +- , /* LCD_G2 */ +- , /* LCD_G3 */ +- , /* LCD_G4 */ +- , /* LCD_G5 */ +- , /* LCD_G6 */ +- , /* LCD_G7 */ +- , /* LCD_B0 */ +- , /* LCD_B1 */ +- , /* LCD_B2 */ +- , /* LCD_B3 */ +- , /* LCD_B4 */ +- , /* LCD_B5 */ +- , /* LCD_B6 */ +- ; /* LCD_B7 */ +- }; +- }; +- +- m_can1_pins_a: m-can1-0 { ++ uart7_pins_b: uart7-1 { + pins1 { +- pinmux = ; /* CAN1_TX */ +- slew-rate = <1>; +- drive-push-pull; ++ pinmux = ; /* UART7_TX */ + bias-disable; +- }; +- pins2 { +- pinmux = ; /* CAN1_RX */ +- bias-disable; +- }; +- }; +- +- m_can1_sleep_pins_a: m_can1-sleep-0 { +- pins { +- pinmux = , /* CAN1_TX */ +- ; /* CAN1_RX */ +- }; +- }; +- +- pwm1_pins_a: pwm1-0 { +- pins { +- pinmux = , /* TIM1_CH1 */ +- , /* TIM1_CH2 */ +- ; /* TIM1_CH4 */ +- bias-pull-down; +- drive-push-pull; +- slew-rate = <0>; +- }; +- }; +- +- pwm1_sleep_pins_a: pwm1-sleep-0 { +- pins { +- pinmux = , /* TIM1_CH1 */ +- , /* TIM1_CH2 */ +- ; /* TIM1_CH4 */ +- }; +- }; +- +- pwm2_pins_a: pwm2-0 { +- pins { +- pinmux = ; /* TIM2_CH4 */ +- bias-pull-down; +- drive-push-pull; +- slew-rate = <0>; +- }; +- }; +- +- pwm2_sleep_pins_a: pwm2-sleep-0 { +- pins { +- pinmux = ; /* TIM2_CH4 */ +- }; +- }; +- +- pwm3_pins_a: pwm3-0 { +- pins { +- pinmux = ; /* TIM3_CH2 */ +- bias-pull-down; +- drive-push-pull; +- slew-rate = <0>; +- }; +- }; +- +- pwm3_sleep_pins_a: pwm3-sleep-0 { +- pins { +- pinmux = ; /* TIM3_CH2 */ +- }; +- }; +- +- pwm4_pins_a: pwm4-0 { +- pins { +- pinmux = , /* TIM4_CH3 */ +- ; /* TIM4_CH4 */ +- bias-pull-down; +- drive-push-pull; +- slew-rate = <0>; +- }; +- }; +- +- pwm4_sleep_pins_a: pwm4-sleep-0 { +- pins { +- pinmux = , /* TIM4_CH3 */ +- ; /* TIM4_CH4 */ +- }; +- }; +- +- pwm4_pins_b: pwm4-1 { +- pins { +- pinmux = ; /* TIM4_CH2 */ +- bias-pull-down; +- drive-push-pull; +- slew-rate = <0>; +- }; +- }; +- +- pwm4_sleep_pins_b: pwm4-sleep-1 { +- pins { +- pinmux = ; /* TIM4_CH2 */ +- }; +- }; +- +- pwm5_pins_a: pwm5-0 { +- pins { +- pinmux = ; /* TIM5_CH2 */ +- bias-pull-down; + drive-push-pull; + slew-rate = <0>; + }; +- }; +- +- pwm5_sleep_pins_a: pwm5-sleep-0 { +- pins { +- pinmux = ; /* TIM5_CH2 */ +- }; +- }; +- +- pwm8_pins_a: pwm8-0 { +- pins { +- pinmux = ; /* TIM8_CH4 */ +- bias-pull-down; +- drive-push-pull; +- slew-rate = <0>; +- }; +- }; +- +- pwm8_sleep_pins_a: pwm8-sleep-0 { +- pins { +- pinmux = ; /* TIM8_CH4 */ +- }; +- }; +- +- pwm12_pins_a: pwm12-0 { +- pins { +- pinmux = ; /* TIM12_CH1 */ +- bias-pull-down; +- drive-push-pull; +- slew-rate = <0>; +- }; +- }; +- +- pwm12_sleep_pins_a: pwm12-sleep-0 { +- pins { +- pinmux = ; /* TIM12_CH1 */ +- }; +- }; +- +- qspi_clk_pins_a: qspi-clk-0 { +- pins { +- pinmux = ; /* QSPI_CLK */ ++ pins2 { ++ pinmux = ; /* UART7_RX */ + bias-disable; +- drive-push-pull; +- slew-rate = <3>; +- }; +- }; +- +- qspi_clk_sleep_pins_a: qspi-clk-sleep-0 { +- pins { +- pinmux = ; /* QSPI_CLK */ + }; + }; + +- qspi_bk1_pins_a: qspi-bk1-0 { ++ uart7_pins_c: uart7-2 { + pins1 { +- pinmux = , /* QSPI_BK1_IO0 */ +- , /* QSPI_BK1_IO1 */ +- , /* QSPI_BK1_IO2 */ +- ; /* QSPI_BK1_IO3 */ ++ pinmux = ; /* UART7_TX */ + bias-disable; + drive-push-pull; +- slew-rate = <1>; ++ slew-rate = <0>; + }; + pins2 { +- pinmux = ; /* QSPI_BK1_NCS */ ++ pinmux = ; /* UART7_RX */ + bias-pull-up; +- drive-push-pull; +- slew-rate = <1>; +- }; +- }; +- +- qspi_bk1_sleep_pins_a: qspi-bk1-sleep-0 { +- pins { +- pinmux = , /* QSPI_BK1_IO0 */ +- , /* QSPI_BK1_IO1 */ +- , /* QSPI_BK1_IO2 */ +- , /* QSPI_BK1_IO3 */ +- ; /* QSPI_BK1_NCS */ + }; + }; + +- qspi_bk2_pins_a: qspi-bk2-0 { ++ uart8_pins_a: uart8-0 { + pins1 { +- pinmux = , /* QSPI_BK2_IO0 */ +- , /* QSPI_BK2_IO1 */ +- , /* QSPI_BK2_IO2 */ +- ; /* QSPI_BK2_IO3 */ ++ pinmux = ; /* UART8_TX */ + bias-disable; + drive-push-pull; +- slew-rate = <1>; +- }; +- pins2 { +- pinmux = ; /* QSPI_BK2_NCS */ +- bias-pull-up; +- drive-push-pull; +- slew-rate = <1>; +- }; +- }; +- +- qspi_bk2_sleep_pins_a: qspi-bk2-sleep-0 { +- pins { +- pinmux = , /* QSPI_BK2_IO0 */ +- , /* QSPI_BK2_IO1 */ +- , /* QSPI_BK2_IO2 */ +- , /* QSPI_BK2_IO3 */ +- ; /* QSPI_BK2_NCS */ +- }; +- }; +- +- sai2a_pins_a: sai2a-0 { +- pins { +- pinmux = , /* SAI2_SCK_A */ +- , /* SAI2_SD_A */ +- , /* SAI2_FS_A */ +- ; /* SAI2_MCLK_A */ +- slew-rate = <0>; +- drive-push-pull; +- bias-disable; +- }; +- }; +- +- sai2a_sleep_pins_a: sai2a-1 { +- pins { +- pinmux = , /* SAI2_SCK_A */ +- , /* SAI2_SD_A */ +- , /* SAI2_FS_A */ +- ; /* SAI2_MCLK_A */ +- }; +- }; +- +- sai2b_pins_a: sai2b-0 { +- pins1 { +- pinmux = , /* SAI2_SCK_B */ +- , /* SAI2_FS_B */ +- ; /* SAI2_MCLK_B */ + slew-rate = <0>; +- drive-push-pull; +- bias-disable; + }; + pins2 { +- pinmux = ; /* SAI2_SD_B */ +- bias-disable; +- }; +- }; +- +- sai2b_sleep_pins_a: sai2b-1 { +- pins { +- pinmux = , /* SAI2_SD_B */ +- , /* SAI2_SCK_B */ +- , /* SAI2_FS_B */ +- ; /* SAI2_MCLK_B */ +- }; +- }; +- +- sai2b_pins_b: sai2b-2 { +- pins { +- pinmux = ; /* SAI2_SD_B */ +- bias-disable; +- }; +- }; +- +- sai2b_sleep_pins_b: sai2b-3 { +- pins { +- pinmux = ; /* SAI2_SD_B */ +- }; +- }; +- +- sai4a_pins_a: sai4a-0 { +- pins { +- pinmux = ; /* SAI4_SD_A */ +- slew-rate = <0>; +- drive-push-pull; ++ pinmux = ; /* UART8_RX */ + bias-disable; + }; + }; + +- sai4a_sleep_pins_a: sai4a-1 { +- pins { +- pinmux = ; /* SAI4_SD_A */ +- }; +- }; +- +- sdmmc1_b4_pins_a: sdmmc1-b4-0 { ++ usart2_pins_a: usart2-0 { + pins1 { +- pinmux = , /* SDMMC1_D0 */ +- , /* SDMMC1_D1 */ +- , /* SDMMC1_D2 */ +- , /* SDMMC1_D3 */ +- ; /* SDMMC1_CMD */ +- slew-rate = <1>; +- drive-push-pull; +- bias-disable; +- }; +- pins2 { +- pinmux = ; /* SDMMC1_CK */ +- slew-rate = <2>; +- drive-push-pull; ++ pinmux = , /* USART2_TX */ ++ ; /* USART2_RTS */ + bias-disable; +- }; +- }; +- +- sdmmc1_b4_od_pins_a: sdmmc1-b4-od-0 { +- pins1 { +- pinmux = , /* SDMMC1_D0 */ +- , /* SDMMC1_D1 */ +- , /* SDMMC1_D2 */ +- ; /* SDMMC1_D3 */ +- slew-rate = <1>; + drive-push-pull; +- bias-disable; ++ slew-rate = <0>; + }; + pins2 { +- pinmux = ; /* SDMMC1_CK */ +- slew-rate = <2>; +- drive-push-pull; ++ pinmux = , /* USART2_RX */ ++ ; /* USART2_CTS_NSS */ + bias-disable; + }; +- pins3 { +- pinmux = ; /* SDMMC1_CMD */ +- slew-rate = <1>; +- drive-open-drain; +- bias-disable; +- }; +- }; +- +- sdmmc1_b4_sleep_pins_a: sdmmc1-b4-sleep-0 { +- pins { +- pinmux = , /* SDMMC1_D0 */ +- , /* SDMMC1_D1 */ +- , /* SDMMC1_D2 */ +- , /* SDMMC1_D3 */ +- , /* SDMMC1_CK */ +- ; /* SDMMC1_CMD */ +- }; +- }; +- +- sdmmc1_dir_pins_a: sdmmc1-dir-0 { +- pins1 { +- pinmux = , /* SDMMC1_D0DIR */ +- , /* SDMMC1_D123DIR */ +- ; /* SDMMC1_CDIR */ +- slew-rate = <1>; +- drive-push-pull; +- bias-pull-up; +- }; +- pins2{ +- pinmux = ; /* SDMMC1_CKIN */ +- bias-pull-up; +- }; +- }; +- +- sdmmc1_dir_sleep_pins_a: sdmmc1-dir-sleep-0 { +- pins { +- pinmux = , /* SDMMC1_D0DIR */ +- , /* SDMMC1_D123DIR */ +- , /* SDMMC1_CDIR */ +- ; /* SDMMC1_CKIN */ +- }; +- }; +- +- sdmmc2_b4_pins_a: sdmmc2-b4-0 { +- pins1 { +- pinmux = , /* SDMMC2_D0 */ +- , /* SDMMC2_D1 */ +- , /* SDMMC2_D2 */ +- , /* SDMMC2_D3 */ +- ; /* SDMMC2_CMD */ +- slew-rate = <1>; +- drive-push-pull; +- bias-pull-up; +- }; +- pins2 { +- pinmux = ; /* SDMMC2_CK */ +- slew-rate = <2>; +- drive-push-pull; +- bias-pull-up; +- }; +- }; +- +- sdmmc2_b4_od_pins_a: sdmmc2-b4-od-0 { +- pins1 { +- pinmux = , /* SDMMC2_D0 */ +- , /* SDMMC2_D1 */ +- , /* SDMMC2_D2 */ +- ; /* SDMMC2_D3 */ +- slew-rate = <1>; +- drive-push-pull; +- bias-pull-up; +- }; +- pins2 { +- pinmux = ; /* SDMMC2_CK */ +- slew-rate = <2>; +- drive-push-pull; +- bias-pull-up; +- }; +- pins3 { +- pinmux = ; /* SDMMC2_CMD */ +- slew-rate = <1>; +- drive-open-drain; +- bias-pull-up; +- }; + }; + +- sdmmc2_b4_sleep_pins_a: sdmmc2-b4-sleep-0 { +- pins { +- pinmux = , /* SDMMC2_D0 */ +- , /* SDMMC2_D1 */ +- , /* SDMMC2_D2 */ +- , /* SDMMC2_D3 */ +- , /* SDMMC2_CK */ +- ; /* SDMMC2_CMD */ +- }; +- }; +- +- sdmmc2_b4_pins_b: sdmmc2-b4-1 { ++ usart2_pins_b: usart2-1 { + pins1 { +- pinmux = , /* SDMMC2_D0 */ +- , /* SDMMC2_D1 */ +- , /* SDMMC2_D2 */ +- , /* SDMMC2_D3 */ +- ; /* SDMMC2_CMD */ +- slew-rate = <1>; +- drive-push-pull; +- bias-disable; +- }; +- pins2 { +- pinmux = ; /* SDMMC2_CK */ +- slew-rate = <2>; +- drive-push-pull; ++ pinmux = , /* USART2_TX */ ++ ; /* USART2_RTS */ + bias-disable; +- }; +- }; +- +- sdmmc2_b4_od_pins_b: sdmmc2-b4-od-1 { +- pins1 { +- pinmux = , /* SDMMC2_D0 */ +- , /* SDMMC2_D1 */ +- , /* SDMMC2_D2 */ +- ; /* SDMMC2_D3 */ +- slew-rate = <1>; + drive-push-pull; +- bias-disable; ++ slew-rate = <0>; + }; + pins2 { +- pinmux = ; /* SDMMC2_CK */ +- slew-rate = <2>; +- drive-push-pull; ++ pinmux = , /* USART2_RX */ ++ ; /* USART2_CTS_NSS */ + bias-disable; + }; +- pins3 { +- pinmux = ; /* SDMMC2_CMD */ +- slew-rate = <1>; +- drive-open-drain; +- bias-disable; +- }; +- }; +- +- sdmmc2_d47_pins_a: sdmmc2-d47-0 { +- pins { +- pinmux = , /* SDMMC2_D4 */ +- , /* SDMMC2_D5 */ +- , /* SDMMC2_D6 */ +- ; /* SDMMC2_D7 */ +- slew-rate = <1>; +- drive-push-pull; +- bias-pull-up; +- }; +- }; +- +- sdmmc2_d47_sleep_pins_a: sdmmc2-d47-sleep-0 { +- pins { +- pinmux = , /* SDMMC2_D4 */ +- , /* SDMMC2_D5 */ +- , /* SDMMC2_D6 */ +- ; /* SDMMC2_D7 */ +- }; +- }; +- +- sdmmc3_b4_pins_a: sdmmc3-b4-0 { +- pins1 { +- pinmux = , /* SDMMC3_D0 */ +- , /* SDMMC3_D1 */ +- , /* SDMMC3_D2 */ +- , /* SDMMC3_D3 */ +- ; /* SDMMC3_CMD */ +- slew-rate = <1>; +- drive-push-pull; +- bias-pull-up; +- }; +- pins2 { +- pinmux = ; /* SDMMC3_CK */ +- slew-rate = <2>; +- drive-push-pull; +- bias-pull-up; +- }; + }; + +- sdmmc3_b4_od_pins_a: sdmmc3-b4-od-0 { ++ usart2_pins_c: usart2-2 { + pins1 { +- pinmux = , /* SDMMC3_D0 */ +- , /* SDMMC3_D1 */ +- , /* SDMMC3_D2 */ +- ; /* SDMMC3_D3 */ +- slew-rate = <1>; ++ pinmux = , /* USART2_TX */ ++ ; /* USART2_RTS */ ++ bias-disable; + drive-push-pull; +- bias-pull-up; ++ slew-rate = <0>; + }; + pins2 { +- pinmux = ; /* SDMMC3_CK */ +- slew-rate = <2>; +- drive-push-pull; +- bias-pull-up; +- }; +- pins3 { +- pinmux = ; /* SDMMC2_CMD */ +- slew-rate = <1>; +- drive-open-drain; +- bias-pull-up; +- }; +- }; +- +- sdmmc3_b4_sleep_pins_a: sdmmc3-b4-sleep-0 { +- pins { +- pinmux = , /* SDMMC3_D0 */ +- , /* SDMMC3_D1 */ +- , /* SDMMC3_D2 */ +- , /* SDMMC3_D3 */ +- , /* SDMMC3_CK */ +- ; /* SDMMC3_CMD */ +- }; +- }; +- +- spdifrx_pins_a: spdifrx-0 { +- pins { +- pinmux = ; /* SPDIF_IN1 */ ++ pinmux = , /* USART2_RX */ ++ ; /* USART2_CTS_NSS */ + bias-disable; + }; + }; + +- spdifrx_sleep_pins_a: spdifrx-1 { +- pins { +- pinmux = ; /* SPDIF_IN1 */ +- }; +- }; +- +- uart4_pins_a: uart4-0 { ++ usart3_pins_a: usart3-0 { + pins1 { +- pinmux = ; /* UART4_TX */ ++ pinmux = ; /* USART3_TX */ + bias-disable; + drive-push-pull; + slew-rate = <0>; + }; + pins2 { +- pinmux = ; /* UART4_RX */ ++ pinmux = ; /* USART3_RX */ + bias-disable; + }; + }; + +- uart4_pins_b: uart4-1 { ++ usart3_pins_b: usart3-1 { + pins1 { +- pinmux = ; /* UART4_TX */ ++ pinmux = , /* USART3_TX */ ++ ; /* USART3_RTS */ + bias-disable; + drive-push-pull; + slew-rate = <0>; + }; + pins2 { +- pinmux = ; /* UART4_RX */ ++ pinmux = , /* USART3_RX */ ++ ; /* USART3_CTS_NSS */ + bias-disable; + }; + }; + +- uart7_pins_a: uart7-0 { ++ usart3_pins_c: usart3-2 { + pins1 { +- pinmux = ; /* UART4_TX */ ++ pinmux = , /* USART3_TX */ ++ ; /* USART3_RTS */ + bias-disable; + drive-push-pull; + slew-rate = <0>; + }; + pins2 { +- pinmux = , /* UART4_RX */ +- , /* UART4_CTS */ +- ; /* UART4_RTS */ ++ pinmux = , /* USART3_RX */ ++ ; /* USART3_CTS_NSS */ + bias-disable; + }; + }; + }; + + &pinctrl_z { +- i2c2_pins_b2: i2c2-0 { +- pins { +- pinmux = ; /* I2C2_SCL */ +- bias-disable; +- drive-open-drain; +- slew-rate = <0>; +- }; +- }; +- +- i2c2_pins_sleep_b2: i2c2-1 { +- pins { +- pinmux = ; /* I2C2_SCL */ +- }; +- }; +- + i2c4_pins_a: i2c4-0 { + pins { + pinmux = , /* I2C4_SCL */ +@@ -1067,26 +185,4 @@ + slew-rate = <0>; + }; + }; +- +- i2c4_pins_sleep_a: i2c4-1 { +- pins { +- pinmux = , /* I2C4_SCL */ +- ; /* I2C4_SDA */ +- }; +- }; +- +- spi1_pins_a: spi1-0 { +- pins1 { +- pinmux = , /* SPI1_SCK */ +- ; /* SPI1_MOSI */ +- bias-disable; +- drive-push-pull; +- slew-rate = <1>; +- }; +- +- pins2 { +- pinmux = ; /* SPI1_MISO */ +- bias-disable; +- }; +- }; + }; +diff --git a/core/arch/arm/dts/stm32mp151.dtsi b/core/arch/arm/dts/stm32mp151.dtsi +index d8ac70292..469ae226c 100644 +--- a/core/arch/arm/dts/stm32mp151.dtsi ++++ b/core/arch/arm/dts/stm32mp151.dtsi +@@ -5,6 +5,7 @@ + */ + #include + #include ++#include + #include + + / { +@@ -19,9 +20,19 @@ + compatible = "arm,cortex-a7"; + device_type = "cpu"; + reg = <0>; ++ clocks = <&rcc CK_MPU>; ++ clock-names = "cpu"; ++ operating-points-v2 = <&cpu0_opp_table>; ++ nvmem-cells = <&part_number_otp>; ++ nvmem-cell-names = "part_number"; + }; + }; + ++ cpu0_opp_table: cpu0-opp-table { ++ compatible = "operating-points-v2"; ++ opp-shared; ++ }; ++ + psci { + compatible = "arm,psci-1.0"; + method = "smc"; +@@ -35,15 +46,6 @@ + <0xa0022000 0x2000>; + }; + +- timer { +- compatible = "arm,armv7-timer"; +- interrupts = , +- , +- , +- ; +- interrupt-parent = <&intc>; +- }; +- + clocks { + clk_hse: clk-hse { + #clock-cells = <0>; +@@ -69,1030 +71,154 @@ + clock-frequency = <32000>; + }; + +- clk_csi: clk-csi { +- #clock-cells = <0>; +- compatible = "fixed-clock"; +- clock-frequency = <4000000>; +- }; +- }; +- +- thermal-zones { +- cpu_thermal: cpu-thermal { +- polling-delay-passive = <0>; +- polling-delay = <0>; +- thermal-sensors = <&dts>; +- +- trips { +- cpu_alert1: cpu-alert1 { +- temperature = <85000>; +- hysteresis = <0>; +- type = "passive"; +- }; +- +- cpu-crit { +- temperature = <120000>; +- hysteresis = <0>; +- type = "critical"; +- }; +- }; +- +- cooling-maps { +- }; +- }; +- }; +- +- booster: regulator-booster { +- compatible = "st,stm32mp1-booster"; +- st,syscfg = <&syscfg>; +- status = "disabled"; +- }; +- +- soc { +- compatible = "simple-bus"; +- #address-cells = <1>; +- #size-cells = <1>; +- interrupt-parent = <&intc>; +- ranges; +- +- timers2: timer@40000000 { +- #address-cells = <1>; +- #size-cells = <0>; +- compatible = "st,stm32-timers"; +- reg = <0x40000000 0x400>; +- clocks = <&rcc TIM2_K>; +- clock-names = "int"; +- dmas = <&dmamux1 18 0x400 0x1>, +- <&dmamux1 19 0x400 0x1>, +- <&dmamux1 20 0x400 0x1>, +- <&dmamux1 21 0x400 0x1>, +- <&dmamux1 22 0x400 0x1>; +- dma-names = "ch1", "ch2", "ch3", "ch4", "up"; +- status = "disabled"; +- +- pwm { +- compatible = "st,stm32-pwm"; +- #pwm-cells = <3>; +- status = "disabled"; +- }; +- +- timer@1 { +- compatible = "st,stm32h7-timer-trigger"; +- reg = <1>; +- status = "disabled"; +- }; +- +- counter { +- compatible = "st,stm32-timer-counter"; +- status = "disabled"; +- }; +- }; +- +- timers3: timer@40001000 { +- #address-cells = <1>; +- #size-cells = <0>; +- compatible = "st,stm32-timers"; +- reg = <0x40001000 0x400>; +- clocks = <&rcc TIM3_K>; +- clock-names = "int"; +- dmas = <&dmamux1 23 0x400 0x1>, +- <&dmamux1 24 0x400 0x1>, +- <&dmamux1 25 0x400 0x1>, +- <&dmamux1 26 0x400 0x1>, +- <&dmamux1 27 0x400 0x1>, +- <&dmamux1 28 0x400 0x1>; +- dma-names = "ch1", "ch2", "ch3", "ch4", "up", "trig"; +- status = "disabled"; +- +- pwm { +- compatible = "st,stm32-pwm"; +- #pwm-cells = <3>; +- status = "disabled"; +- }; +- +- timer@2 { +- compatible = "st,stm32h7-timer-trigger"; +- reg = <2>; +- status = "disabled"; +- }; +- +- counter { +- compatible = "st,stm32-timer-counter"; +- status = "disabled"; +- }; +- }; +- +- timers4: timer@40002000 { +- #address-cells = <1>; +- #size-cells = <0>; +- compatible = "st,stm32-timers"; +- reg = <0x40002000 0x400>; +- clocks = <&rcc TIM4_K>; +- clock-names = "int"; +- dmas = <&dmamux1 29 0x400 0x1>, +- <&dmamux1 30 0x400 0x1>, +- <&dmamux1 31 0x400 0x1>, +- <&dmamux1 32 0x400 0x1>; +- dma-names = "ch1", "ch2", "ch3", "ch4"; +- status = "disabled"; +- +- pwm { +- compatible = "st,stm32-pwm"; +- #pwm-cells = <3>; +- status = "disabled"; +- }; +- +- timer@3 { +- compatible = "st,stm32h7-timer-trigger"; +- reg = <3>; +- status = "disabled"; +- }; +- +- counter { +- compatible = "st,stm32-timer-counter"; +- status = "disabled"; +- }; +- }; +- +- timers5: timer@40003000 { +- #address-cells = <1>; +- #size-cells = <0>; +- compatible = "st,stm32-timers"; +- reg = <0x40003000 0x400>; +- clocks = <&rcc TIM5_K>; +- clock-names = "int"; +- dmas = <&dmamux1 55 0x400 0x1>, +- <&dmamux1 56 0x400 0x1>, +- <&dmamux1 57 0x400 0x1>, +- <&dmamux1 58 0x400 0x1>, +- <&dmamux1 59 0x400 0x1>, +- <&dmamux1 60 0x400 0x1>; +- dma-names = "ch1", "ch2", "ch3", "ch4", "up", "trig"; +- status = "disabled"; +- +- pwm { +- compatible = "st,stm32-pwm"; +- #pwm-cells = <3>; +- status = "disabled"; +- }; +- +- timer@4 { +- compatible = "st,stm32h7-timer-trigger"; +- reg = <4>; +- status = "disabled"; +- }; +- +- counter { +- compatible = "st,stm32-timer-counter"; +- status = "disabled"; +- }; +- }; +- +- timers6: timer@40004000 { +- #address-cells = <1>; +- #size-cells = <0>; +- compatible = "st,stm32-timers"; +- reg = <0x40004000 0x400>; +- clocks = <&rcc TIM6_K>; +- clock-names = "int"; +- dmas = <&dmamux1 69 0x400 0x1>; +- dma-names = "up"; +- status = "disabled"; +- +- timer@5 { +- compatible = "st,stm32h7-timer-trigger"; +- reg = <5>; +- status = "disabled"; +- }; +- }; +- +- timers7: timer@40005000 { +- #address-cells = <1>; +- #size-cells = <0>; +- compatible = "st,stm32-timers"; +- reg = <0x40005000 0x400>; +- clocks = <&rcc TIM7_K>; +- clock-names = "int"; +- dmas = <&dmamux1 70 0x400 0x1>; +- dma-names = "up"; +- status = "disabled"; +- +- timer@6 { +- compatible = "st,stm32h7-timer-trigger"; +- reg = <6>; +- status = "disabled"; +- }; +- }; +- +- timers12: timer@40006000 { +- #address-cells = <1>; +- #size-cells = <0>; +- compatible = "st,stm32-timers"; +- reg = <0x40006000 0x400>; +- clocks = <&rcc TIM12_K>; +- clock-names = "int"; +- status = "disabled"; +- +- pwm { +- compatible = "st,stm32-pwm"; +- #pwm-cells = <3>; +- status = "disabled"; +- }; +- +- timer@11 { +- compatible = "st,stm32h7-timer-trigger"; +- reg = <11>; +- status = "disabled"; +- }; +- }; +- +- timers13: timer@40007000 { +- #address-cells = <1>; +- #size-cells = <0>; +- compatible = "st,stm32-timers"; +- reg = <0x40007000 0x400>; +- clocks = <&rcc TIM13_K>; +- clock-names = "int"; +- status = "disabled"; +- +- pwm { +- compatible = "st,stm32-pwm"; +- #pwm-cells = <3>; +- status = "disabled"; +- }; +- +- timer@12 { +- compatible = "st,stm32h7-timer-trigger"; +- reg = <12>; +- status = "disabled"; +- }; +- }; +- +- timers14: timer@40008000 { +- #address-cells = <1>; +- #size-cells = <0>; +- compatible = "st,stm32-timers"; +- reg = <0x40008000 0x400>; +- clocks = <&rcc TIM14_K>; +- clock-names = "int"; +- status = "disabled"; +- +- pwm { +- compatible = "st,stm32-pwm"; +- #pwm-cells = <3>; +- status = "disabled"; +- }; +- +- timer@13 { +- compatible = "st,stm32h7-timer-trigger"; +- reg = <13>; +- status = "disabled"; +- }; +- }; +- +- lptimer1: timer@40009000 { +- #address-cells = <1>; +- #size-cells = <0>; +- compatible = "st,stm32-lptimer"; +- reg = <0x40009000 0x400>; +- clocks = <&rcc LPTIM1_K>; +- clock-names = "mux"; +- status = "disabled"; +- +- pwm { +- compatible = "st,stm32-pwm-lp"; +- #pwm-cells = <3>; +- status = "disabled"; +- }; +- +- trigger@0 { +- compatible = "st,stm32-lptimer-trigger"; +- reg = <0>; +- status = "disabled"; +- }; +- +- counter { +- compatible = "st,stm32-lptimer-counter"; +- status = "disabled"; +- }; +- }; +- +- spi2: spi@4000b000 { +- #address-cells = <1>; +- #size-cells = <0>; +- compatible = "st,stm32h7-spi"; +- reg = <0x4000b000 0x400>; +- interrupts = ; +- clocks = <&rcc SPI2_K>; +- resets = <&rcc SPI2_R>; +- dmas = <&dmamux1 39 0x400 0x05>, +- <&dmamux1 40 0x400 0x05>; +- dma-names = "rx", "tx"; +- status = "disabled"; +- }; +- +- i2s2: audio-controller@4000b000 { +- compatible = "st,stm32h7-i2s"; +- #sound-dai-cells = <0>; +- reg = <0x4000b000 0x400>; +- interrupts = ; +- dmas = <&dmamux1 39 0x400 0x01>, +- <&dmamux1 40 0x400 0x01>; +- dma-names = "rx", "tx"; +- status = "disabled"; +- }; +- +- spi3: spi@4000c000 { +- #address-cells = <1>; +- #size-cells = <0>; +- compatible = "st,stm32h7-spi"; +- reg = <0x4000c000 0x400>; +- interrupts = ; +- clocks = <&rcc SPI3_K>; +- resets = <&rcc SPI3_R>; +- dmas = <&dmamux1 61 0x400 0x05>, +- <&dmamux1 62 0x400 0x05>; +- dma-names = "rx", "tx"; +- status = "disabled"; +- }; +- +- i2s3: audio-controller@4000c000 { +- compatible = "st,stm32h7-i2s"; +- #sound-dai-cells = <0>; +- reg = <0x4000c000 0x400>; +- interrupts = ; +- dmas = <&dmamux1 61 0x400 0x01>, +- <&dmamux1 62 0x400 0x01>; +- dma-names = "rx", "tx"; +- status = "disabled"; +- }; +- +- spdifrx: audio-controller@4000d000 { +- compatible = "st,stm32h7-spdifrx"; +- #sound-dai-cells = <0>; +- reg = <0x4000d000 0x400>; +- clocks = <&rcc SPDIF_K>; +- clock-names = "kclk"; +- interrupts = ; +- dmas = <&dmamux1 93 0x400 0x01>, +- <&dmamux1 94 0x400 0x01>; +- dma-names = "rx", "rx-ctrl"; +- status = "disabled"; +- }; +- +- usart2: serial@4000e000 { +- compatible = "st,stm32h7-uart"; +- reg = <0x4000e000 0x400>; +- interrupts = ; +- clocks = <&rcc USART2_K>; +- status = "disabled"; +- }; +- +- usart3: serial@4000f000 { +- compatible = "st,stm32h7-uart"; +- reg = <0x4000f000 0x400>; +- interrupts = ; +- clocks = <&rcc USART3_K>; +- status = "disabled"; +- }; +- +- uart4: serial@40010000 { +- compatible = "st,stm32h7-uart"; +- reg = <0x40010000 0x400>; +- interrupts = ; +- clocks = <&rcc UART4_K>; +- status = "disabled"; +- }; +- +- uart5: serial@40011000 { +- compatible = "st,stm32h7-uart"; +- reg = <0x40011000 0x400>; +- interrupts = ; +- clocks = <&rcc UART5_K>; +- status = "disabled"; +- }; +- +- i2c1: i2c@40012000 { +- compatible = "st,stm32f7-i2c"; +- reg = <0x40012000 0x400>; +- interrupt-names = "event", "error"; +- interrupts = , +- ; +- clocks = <&rcc I2C1_K>; +- resets = <&rcc I2C1_R>; +- #address-cells = <1>; +- #size-cells = <0>; +- status = "disabled"; +- }; +- +- i2c2: i2c@40013000 { +- compatible = "st,stm32f7-i2c"; +- reg = <0x40013000 0x400>; +- interrupt-names = "event", "error"; +- interrupts = , +- ; +- clocks = <&rcc I2C2_K>; +- resets = <&rcc I2C2_R>; +- #address-cells = <1>; +- #size-cells = <0>; +- status = "disabled"; +- }; +- +- i2c3: i2c@40014000 { +- compatible = "st,stm32f7-i2c"; +- reg = <0x40014000 0x400>; +- interrupt-names = "event", "error"; +- interrupts = , +- ; +- clocks = <&rcc I2C3_K>; +- resets = <&rcc I2C3_R>; +- #address-cells = <1>; +- #size-cells = <0>; +- status = "disabled"; +- }; +- +- i2c5: i2c@40015000 { +- compatible = "st,stm32f7-i2c"; +- reg = <0x40015000 0x400>; +- interrupt-names = "event", "error"; +- interrupts = , +- ; +- clocks = <&rcc I2C5_K>; +- resets = <&rcc I2C5_R>; +- #address-cells = <1>; +- #size-cells = <0>; +- status = "disabled"; +- }; +- +- cec: cec@40016000 { +- compatible = "st,stm32-cec"; +- reg = <0x40016000 0x400>; +- interrupts = ; +- clocks = <&rcc CEC_K>, <&clk_lse>; +- clock-names = "cec", "hdmi-cec"; +- status = "disabled"; +- }; +- +- dac: dac@40017000 { +- compatible = "st,stm32h7-dac-core"; +- reg = <0x40017000 0x400>; +- clocks = <&rcc DAC12>; +- clock-names = "pclk"; +- #address-cells = <1>; +- #size-cells = <0>; +- status = "disabled"; +- +- dac1: dac@1 { +- compatible = "st,stm32-dac"; +- #io-channels-cells = <1>; +- reg = <1>; +- status = "disabled"; +- }; +- +- dac2: dac@2 { +- compatible = "st,stm32-dac"; +- #io-channels-cells = <1>; +- reg = <2>; +- status = "disabled"; +- }; +- }; +- +- uart7: serial@40018000 { +- compatible = "st,stm32h7-uart"; +- reg = <0x40018000 0x400>; +- interrupts = ; +- clocks = <&rcc UART7_K>; +- status = "disabled"; +- }; +- +- uart8: serial@40019000 { +- compatible = "st,stm32h7-uart"; +- reg = <0x40019000 0x400>; +- interrupts = ; +- clocks = <&rcc UART8_K>; +- status = "disabled"; +- }; +- +- timers1: timer@44000000 { +- #address-cells = <1>; +- #size-cells = <0>; +- compatible = "st,stm32-timers"; +- reg = <0x44000000 0x400>; +- clocks = <&rcc TIM1_K>; +- clock-names = "int"; +- dmas = <&dmamux1 11 0x400 0x1>, +- <&dmamux1 12 0x400 0x1>, +- <&dmamux1 13 0x400 0x1>, +- <&dmamux1 14 0x400 0x1>, +- <&dmamux1 15 0x400 0x1>, +- <&dmamux1 16 0x400 0x1>, +- <&dmamux1 17 0x400 0x1>; +- dma-names = "ch1", "ch2", "ch3", "ch4", +- "up", "trig", "com"; +- status = "disabled"; +- +- pwm { +- compatible = "st,stm32-pwm"; +- #pwm-cells = <3>; +- status = "disabled"; +- }; +- +- timer@0 { +- compatible = "st,stm32h7-timer-trigger"; +- reg = <0>; +- status = "disabled"; +- }; +- +- counter { +- compatible = "st,stm32-timer-counter"; +- status = "disabled"; +- }; +- }; +- +- timers8: timer@44001000 { +- #address-cells = <1>; +- #size-cells = <0>; +- compatible = "st,stm32-timers"; +- reg = <0x44001000 0x400>; +- clocks = <&rcc TIM8_K>; +- clock-names = "int"; +- dmas = <&dmamux1 47 0x400 0x1>, +- <&dmamux1 48 0x400 0x1>, +- <&dmamux1 49 0x400 0x1>, +- <&dmamux1 50 0x400 0x1>, +- <&dmamux1 51 0x400 0x1>, +- <&dmamux1 52 0x400 0x1>, +- <&dmamux1 53 0x400 0x1>; +- dma-names = "ch1", "ch2", "ch3", "ch4", +- "up", "trig", "com"; +- status = "disabled"; +- +- pwm { +- compatible = "st,stm32-pwm"; +- #pwm-cells = <3>; +- status = "disabled"; +- }; +- +- timer@7 { +- compatible = "st,stm32h7-timer-trigger"; +- reg = <7>; +- status = "disabled"; +- }; +- +- counter { +- compatible = "st,stm32-timer-counter"; +- status = "disabled"; +- }; +- }; +- +- usart6: serial@44003000 { +- compatible = "st,stm32h7-uart"; +- reg = <0x44003000 0x400>; +- interrupts = ; +- clocks = <&rcc USART6_K>; +- status = "disabled"; +- }; +- +- spi1: spi@44004000 { +- #address-cells = <1>; +- #size-cells = <0>; +- compatible = "st,stm32h7-spi"; +- reg = <0x44004000 0x400>; +- interrupts = ; +- clocks = <&rcc SPI1_K>; +- resets = <&rcc SPI1_R>; +- dmas = <&dmamux1 37 0x400 0x05>, +- <&dmamux1 38 0x400 0x05>; +- dma-names = "rx", "tx"; +- status = "disabled"; +- }; +- +- i2s1: audio-controller@44004000 { +- compatible = "st,stm32h7-i2s"; +- #sound-dai-cells = <0>; +- reg = <0x44004000 0x400>; +- interrupts = ; +- dmas = <&dmamux1 37 0x400 0x01>, +- <&dmamux1 38 0x400 0x01>; +- dma-names = "rx", "tx"; +- status = "disabled"; +- }; +- +- spi4: spi@44005000 { +- #address-cells = <1>; +- #size-cells = <0>; +- compatible = "st,stm32h7-spi"; +- reg = <0x44005000 0x400>; +- interrupts = ; +- clocks = <&rcc SPI4_K>; +- resets = <&rcc SPI4_R>; +- dmas = <&dmamux1 83 0x400 0x05>, +- <&dmamux1 84 0x400 0x05>; +- dma-names = "rx", "tx"; +- status = "disabled"; +- }; +- +- timers15: timer@44006000 { +- #address-cells = <1>; +- #size-cells = <0>; +- compatible = "st,stm32-timers"; +- reg = <0x44006000 0x400>; +- clocks = <&rcc TIM15_K>; +- clock-names = "int"; +- dmas = <&dmamux1 105 0x400 0x1>, +- <&dmamux1 106 0x400 0x1>, +- <&dmamux1 107 0x400 0x1>, +- <&dmamux1 108 0x400 0x1>; +- dma-names = "ch1", "up", "trig", "com"; +- status = "disabled"; +- +- pwm { +- compatible = "st,stm32-pwm"; +- #pwm-cells = <3>; +- status = "disabled"; +- }; +- +- timer@14 { +- compatible = "st,stm32h7-timer-trigger"; +- reg = <14>; +- status = "disabled"; +- }; +- }; +- +- timers16: timer@44007000 { +- #address-cells = <1>; +- #size-cells = <0>; +- compatible = "st,stm32-timers"; +- reg = <0x44007000 0x400>; +- clocks = <&rcc TIM16_K>; +- clock-names = "int"; +- dmas = <&dmamux1 109 0x400 0x1>, +- <&dmamux1 110 0x400 0x1>; +- dma-names = "ch1", "up"; +- status = "disabled"; +- +- pwm { +- compatible = "st,stm32-pwm"; +- #pwm-cells = <3>; +- status = "disabled"; +- }; +- timer@15 { +- compatible = "st,stm32h7-timer-trigger"; +- reg = <15>; +- status = "disabled"; +- }; +- }; +- +- timers17: timer@44008000 { +- #address-cells = <1>; +- #size-cells = <0>; +- compatible = "st,stm32-timers"; +- reg = <0x44008000 0x400>; +- clocks = <&rcc TIM17_K>; +- clock-names = "int"; +- dmas = <&dmamux1 111 0x400 0x1>, +- <&dmamux1 112 0x400 0x1>; +- dma-names = "ch1", "up"; +- status = "disabled"; +- +- pwm { +- compatible = "st,stm32-pwm"; +- #pwm-cells = <3>; +- status = "disabled"; +- }; +- +- timer@16 { +- compatible = "st,stm32h7-timer-trigger"; +- reg = <16>; +- status = "disabled"; +- }; +- }; +- +- spi5: spi@44009000 { +- #address-cells = <1>; +- #size-cells = <0>; +- compatible = "st,stm32h7-spi"; +- reg = <0x44009000 0x400>; +- interrupts = ; +- clocks = <&rcc SPI5_K>; +- resets = <&rcc SPI5_R>; +- dmas = <&dmamux1 85 0x400 0x05>, +- <&dmamux1 86 0x400 0x05>; +- dma-names = "rx", "tx"; +- status = "disabled"; +- }; +- +- sai1: sai@4400a000 { +- compatible = "st,stm32h7-sai"; +- #address-cells = <1>; +- #size-cells = <1>; +- ranges = <0 0x4400a000 0x400>; +- reg = <0x4400a000 0x4>, <0x4400a3f0 0x10>; +- interrupts = ; +- resets = <&rcc SAI1_R>; +- status = "disabled"; +- +- sai1a: audio-controller@4400a004 { +- #sound-dai-cells = <0>; +- +- compatible = "st,stm32-sai-sub-a"; +- reg = <0x4 0x1c>; +- clocks = <&rcc SAI1_K>; +- clock-names = "sai_ck"; +- dmas = <&dmamux1 87 0x400 0x01>; +- status = "disabled"; +- }; +- +- sai1b: audio-controller@4400a024 { +- #sound-dai-cells = <0>; +- compatible = "st,stm32-sai-sub-b"; +- reg = <0x24 0x1c>; +- clocks = <&rcc SAI1_K>; +- clock-names = "sai_ck"; +- dmas = <&dmamux1 88 0x400 0x01>; +- status = "disabled"; +- }; +- }; +- +- sai2: sai@4400b000 { +- compatible = "st,stm32h7-sai"; +- #address-cells = <1>; +- #size-cells = <1>; +- ranges = <0 0x4400b000 0x400>; +- reg = <0x4400b000 0x4>, <0x4400b3f0 0x10>; +- interrupts = ; +- resets = <&rcc SAI2_R>; +- status = "disabled"; +- +- sai2a: audio-controller@4400b004 { +- #sound-dai-cells = <0>; +- compatible = "st,stm32-sai-sub-a"; +- reg = <0x4 0x1c>; +- clocks = <&rcc SAI2_K>; +- clock-names = "sai_ck"; +- dmas = <&dmamux1 89 0x400 0x01>; +- status = "disabled"; +- }; +- +- sai2b: audio-controller@4400b024 { +- #sound-dai-cells = <0>; +- compatible = "st,stm32-sai-sub-b"; +- reg = <0x24 0x1c>; +- clocks = <&rcc SAI2_K>; +- clock-names = "sai_ck"; +- dmas = <&dmamux1 90 0x400 0x01>; +- status = "disabled"; +- }; +- }; +- +- sai3: sai@4400c000 { +- compatible = "st,stm32h7-sai"; +- #address-cells = <1>; +- #size-cells = <1>; +- ranges = <0 0x4400c000 0x400>; +- reg = <0x4400c000 0x4>, <0x4400c3f0 0x10>; +- interrupts = ; +- resets = <&rcc SAI3_R>; +- status = "disabled"; +- +- sai3a: audio-controller@4400c004 { +- #sound-dai-cells = <0>; +- compatible = "st,stm32-sai-sub-a"; +- reg = <0x04 0x1c>; +- clocks = <&rcc SAI3_K>; +- clock-names = "sai_ck"; +- dmas = <&dmamux1 113 0x400 0x01>; +- status = "disabled"; +- }; +- +- sai3b: audio-controller@4400c024 { +- #sound-dai-cells = <0>; +- compatible = "st,stm32-sai-sub-b"; +- reg = <0x24 0x1c>; +- clocks = <&rcc SAI3_K>; +- clock-names = "sai_ck"; +- dmas = <&dmamux1 114 0x400 0x01>; +- status = "disabled"; +- }; ++ clk_csi: clk-csi { ++ #clock-cells = <0>; ++ compatible = "fixed-clock"; ++ clock-frequency = <4000000>; + }; ++ }; + +- dfsdm: dfsdm@4400d000 { +- compatible = "st,stm32mp1-dfsdm"; +- reg = <0x4400d000 0x800>; +- clocks = <&rcc DFSDM_K>; +- clock-names = "dfsdm"; +- #address-cells = <1>; +- #size-cells = <0>; +- status = "disabled"; ++ scmi_regu: scmi-regu { ++ compatible = "st,scmi-regulator-consumer"; ++ scmi-channel-id = <0>; + +- dfsdm0: filter@0 { +- compatible = "st,stm32-dfsdm-adc"; +- #io-channel-cells = <1>; +- reg = <0>; +- interrupts = ; +- dmas = <&dmamux1 101 0x400 0x01>; +- dma-names = "rx"; +- status = "disabled"; +- }; ++ scmi_reg11: voltd-reg11 { ++ voltd-name = "reg11"; ++ }; + +- dfsdm1: filter@1 { +- compatible = "st,stm32-dfsdm-adc"; +- #io-channel-cells = <1>; +- reg = <1>; +- interrupts = ; +- dmas = <&dmamux1 102 0x400 0x01>; +- dma-names = "rx"; +- status = "disabled"; +- }; ++ scmi_reg18: voltd-reg18 { ++ voltd-name = "reg18"; ++ }; + +- dfsdm2: filter@2 { +- compatible = "st,stm32-dfsdm-adc"; +- #io-channel-cells = <1>; +- reg = <2>; +- interrupts = ; +- dmas = <&dmamux1 103 0x400 0x01>; +- dma-names = "rx"; +- status = "disabled"; +- }; ++ scmi_usb33: voltd-usb33 { ++ voltd-name = "usb33"; ++ }; ++ }; + +- dfsdm3: filter@3 { +- compatible = "st,stm32-dfsdm-adc"; +- #io-channel-cells = <1>; +- reg = <3>; +- interrupts = ; +- dmas = <&dmamux1 104 0x400 0x01>; +- dma-names = "rx"; +- status = "disabled"; +- }; ++ osc_calibration: osc-calibration { ++ compatible = "st,osc-calibration"; + +- dfsdm4: filter@4 { +- compatible = "st,stm32-dfsdm-adc"; +- #io-channel-cells = <1>; +- reg = <4>; +- interrupts = ; +- dmas = <&dmamux1 91 0x400 0x01>; +- dma-names = "rx"; +- status = "disabled"; +- }; ++ csi_calibration: csi-calibration { ++ compatible = "st,csi-cal"; ++ counter = <&timers15 0 8>; ++ status = "disabled"; ++ }; + +- dfsdm5: filter@5 { +- compatible = "st,stm32-dfsdm-adc"; +- #io-channel-cells = <1>; +- reg = <5>; +- interrupts = ; +- dmas = <&dmamux1 92 0x400 0x01>; +- dma-names = "rx"; +- status = "disabled"; +- }; ++ hsi_calibration: hsi-calibration { ++ compatible = "st,hsi-cal"; ++ counter = <&timers15 0 7>; ++ status = "disabled"; + }; ++ }; ++ ++ soc { ++ compatible = "simple-bus"; ++ #address-cells = <1>; ++ #size-cells = <1>; ++ interrupt-parent = <&intc>; ++ ranges; + +- dma1: dma-controller@48000000 { +- compatible = "st,stm32-dma"; +- reg = <0x48000000 0x400>; +- interrupts = , +- , +- , +- , +- , +- , +- , +- ; +- clocks = <&rcc DMA1>; +- #dma-cells = <4>; +- st,mem2mem; +- dma-requests = <8>; ++ usart2: serial@4000e000 { ++ compatible = "st,stm32h7-uart"; ++ reg = <0x4000e000 0x400>; ++ interrupts = ; ++ clocks = <&rcc USART2_K>; ++ resets = <&rcc USART2_R>; ++ status = "disabled"; + }; + +- dma2: dma-controller@48001000 { +- compatible = "st,stm32-dma"; +- reg = <0x48001000 0x400>; +- interrupts = , +- , +- , +- , +- , +- , +- , +- ; +- clocks = <&rcc DMA2>; +- #dma-cells = <4>; +- st,mem2mem; +- dma-requests = <8>; ++ usart3: serial@4000f000 { ++ compatible = "st,stm32h7-uart"; ++ reg = <0x4000f000 0x400>; ++ interrupts = ; ++ clocks = <&rcc USART3_K>; ++ resets = <&rcc USART3_R>; ++ status = "disabled"; + }; + +- dmamux1: dma-router@48002000 { +- compatible = "st,stm32h7-dmamux"; +- reg = <0x48002000 0x1c>; +- #dma-cells = <3>; +- dma-requests = <128>; +- dma-masters = <&dma1 &dma2>; +- dma-channels = <16>; +- clocks = <&rcc DMAMUX>; ++ uart4: serial@40010000 { ++ compatible = "st,stm32h7-uart"; ++ reg = <0x40010000 0x400>; ++ interrupts = ; ++ clocks = <&rcc UART4_K>; ++ resets = <&rcc UART4_R>; ++ wakeup-source; ++ status = "disabled"; + }; + +- adc: adc@48003000 { +- compatible = "st,stm32mp1-adc-core"; +- reg = <0x48003000 0x400>; +- interrupts = , +- ; +- clocks = <&rcc ADC12>, <&rcc ADC12_K>; +- clock-names = "bus", "adc"; +- interrupt-controller; +- st,syscfg = <&syscfg>; +- #interrupt-cells = <1>; ++ timers12: timer@40006000 { + #address-cells = <1>; + #size-cells = <0>; ++ compatible = "st,stm32-timers"; ++ reg = <0x40006000 0x400>; ++ clocks = <&rcc TIM12_K>; ++ clock-names = "int"; + status = "disabled"; + +- adc1: adc@0 { +- compatible = "st,stm32mp1-adc"; +- #io-channel-cells = <1>; +- reg = <0x0>; +- interrupt-parent = <&adc>; +- interrupts = <0>; +- dmas = <&dmamux1 9 0x400 0x01>; +- dma-names = "rx"; +- status = "disabled"; +- }; +- +- adc2: adc@100 { +- compatible = "st,stm32mp1-adc"; +- #io-channel-cells = <1>; +- reg = <0x100>; +- interrupt-parent = <&adc>; +- interrupts = <1>; +- dmas = <&dmamux1 10 0x400 0x01>; +- dma-names = "rx"; ++ counter { ++ compatible = "st,stm32-timer-counter"; + status = "disabled"; + }; + }; + +- sdmmc3: sdmmc@48004000 { +- compatible = "arm,pl18x", "arm,primecell"; +- arm,primecell-periphid = <0x10153180>; +- reg = <0x48004000 0x400>; +- interrupts = ; +- interrupt-names = "cmd_irq"; +- clocks = <&rcc SDMMC3_K>; +- clock-names = "apb_pclk"; +- resets = <&rcc SDMMC3_R>; +- cap-sd-highspeed; +- cap-mmc-highspeed; +- max-frequency = <120000000>; ++ uart5: serial@40011000 { ++ compatible = "st,stm32h7-uart"; ++ reg = <0x40011000 0x400>; ++ interrupts = ; ++ clocks = <&rcc UART5_K>; ++ resets = <&rcc UART5_R>; + status = "disabled"; + }; + +- usbotg_hs: usb-otg@49000000 { +- compatible = "snps,dwc2"; +- reg = <0x49000000 0x10000>; +- clocks = <&rcc USBO_K>; +- clock-names = "otg"; +- resets = <&rcc USBO_R>; +- reset-names = "dwc2"; +- interrupts = ; +- g-rx-fifo-size = <256>; +- g-np-tx-fifo-size = <32>; +- g-tx-fifo-size = <128 128 64 64 64 64 32 32>; +- dr_mode = "otg"; ++ uart7: serial@40018000 { ++ compatible = "st,stm32h7-uart"; ++ reg = <0x40018000 0x400>; ++ interrupts = ; ++ clocks = <&rcc UART7_K>; ++ resets = <&rcc UART7_R>; + status = "disabled"; + }; + +- ipcc: mailbox@4c001000 { +- compatible = "st,stm32mp1-ipcc"; +- #mbox-cells = <1>; +- reg = <0x4c001000 0x400>; +- st,proc-id = <0>; +- interrupts-extended = +- <&intc GIC_SPI 100 IRQ_TYPE_LEVEL_HIGH>, +- <&intc GIC_SPI 101 IRQ_TYPE_LEVEL_HIGH>, +- <&exti 61 1>; +- interrupt-names = "rx", "tx", "wakeup"; +- clocks = <&rcc IPCC>; +- wakeup-source; ++ uart8: serial@40019000 { ++ compatible = "st,stm32h7-uart"; ++ reg = <0x40019000 0x400>; ++ interrupts = ; ++ clocks = <&rcc UART8_K>; ++ resets = <&rcc UART8_R>; + status = "disabled"; + }; + +- dcmi: dcmi@4c006000 { +- compatible = "st,stm32-dcmi"; +- reg = <0x4c006000 0x400>; +- interrupts = ; +- resets = <&rcc CAMITF_R>; +- clocks = <&rcc DCMI>; +- clock-names = "mclk"; +- dmas = <&dmamux1 75 0x400 0x0d>; +- dma-names = "tx"; ++ timers15: timer@44006000 { ++ #address-cells = <1>; ++ #size-cells = <0>; ++ compatible = "st,stm32-timers"; ++ reg = <0x44006000 0x400>; ++ clocks = <&rcc TIM15_K>; ++ clock-names = "int"; + status = "disabled"; ++ ++ counter { ++ compatible = "st,stm32-timer-counter"; ++ status = "disabled"; ++ }; + }; + + rcc: rcc@50000000 { +- compatible = "st,stm32mp1-rcc", "syscon"; ++ compatible = "st,stm32mp1-rcc-secure", "st,stm32mp1-rcc", "syscon"; + reg = <0x50000000 0x1000>; ++ #address-cells = <1>; ++ #size-cells = <0>; + #clock-cells = <1>; + #reset-cells = <1>; ++ interrupts = ; ++ secure-interrupts = ; ++ secure-interrupt-names = "wakeup"; + }; + + pwr_regulators: pwr@50001000 { + compatible = "st,stm32mp1,pwr-reg"; + reg = <0x50001000 0x10>; ++ st,tzcr = <&rcc 0x0 0x1>; + + reg11: reg11 { + regulator-name = "reg11"; +@@ -1113,425 +239,85 @@ + }; + }; + +- exti: interrupt-controller@5000d000 { +- compatible = "st,stm32mp1-exti", "syscon"; +- interrupt-controller; +- #interrupt-cells = <2>; +- reg = <0x5000d000 0x400>; +- }; +- +- syscfg: syscon@50020000 { +- compatible = "st,stm32mp157-syscfg", "syscon"; +- reg = <0x50020000 0x400>; +- clocks = <&rcc SYSCFG>; +- }; +- +- lptimer2: timer@50021000 { +- #address-cells = <1>; +- #size-cells = <0>; +- compatible = "st,stm32-lptimer"; +- reg = <0x50021000 0x400>; +- clocks = <&rcc LPTIM2_K>; +- clock-names = "mux"; +- status = "disabled"; +- +- pwm { +- compatible = "st,stm32-pwm-lp"; +- #pwm-cells = <3>; +- status = "disabled"; +- }; +- +- trigger@1 { +- compatible = "st,stm32-lptimer-trigger"; +- reg = <1>; +- status = "disabled"; +- }; +- +- counter { +- compatible = "st,stm32-lptimer-counter"; +- status = "disabled"; +- }; +- }; +- +- lptimer3: timer@50022000 { +- #address-cells = <1>; +- #size-cells = <0>; +- compatible = "st,stm32-lptimer"; +- reg = <0x50022000 0x400>; +- clocks = <&rcc LPTIM3_K>; +- clock-names = "mux"; +- status = "disabled"; +- +- pwm { +- compatible = "st,stm32-pwm-lp"; +- #pwm-cells = <3>; +- status = "disabled"; +- }; +- +- trigger@2 { +- compatible = "st,stm32-lptimer-trigger"; +- reg = <2>; +- status = "disabled"; +- }; +- }; +- +- lptimer4: timer@50023000 { +- compatible = "st,stm32-lptimer"; +- reg = <0x50023000 0x400>; +- clocks = <&rcc LPTIM4_K>; +- clock-names = "mux"; +- status = "disabled"; +- +- pwm { +- compatible = "st,stm32-pwm-lp"; +- #pwm-cells = <3>; +- status = "disabled"; +- }; +- }; +- +- lptimer5: timer@50024000 { +- compatible = "st,stm32-lptimer"; +- reg = <0x50024000 0x400>; +- clocks = <&rcc LPTIM5_K>; +- clock-names = "mux"; +- status = "disabled"; +- +- pwm { +- compatible = "st,stm32-pwm-lp"; +- #pwm-cells = <3>; +- status = "disabled"; +- }; +- }; +- +- vrefbuf: vrefbuf@50025000 { +- compatible = "st,stm32-vrefbuf"; +- reg = <0x50025000 0x8>; +- regulator-min-microvolt = <1500000>; +- regulator-max-microvolt = <2500000>; +- clocks = <&rcc VREF>; +- status = "disabled"; +- }; +- +- sai4: sai@50027000 { +- compatible = "st,stm32h7-sai"; +- #address-cells = <1>; +- #size-cells = <1>; +- ranges = <0 0x50027000 0x400>; +- reg = <0x50027000 0x4>, <0x500273f0 0x10>; +- interrupts = ; +- resets = <&rcc SAI4_R>; +- status = "disabled"; +- +- sai4a: audio-controller@50027004 { +- #sound-dai-cells = <0>; +- compatible = "st,stm32-sai-sub-a"; +- reg = <0x04 0x1c>; +- clocks = <&rcc SAI4_K>; +- clock-names = "sai_ck"; +- dmas = <&dmamux1 99 0x400 0x01>; +- status = "disabled"; +- }; +- +- sai4b: audio-controller@50027024 { +- #sound-dai-cells = <0>; +- compatible = "st,stm32-sai-sub-b"; +- reg = <0x24 0x1c>; +- clocks = <&rcc SAI4_K>; +- clock-names = "sai_ck"; +- dmas = <&dmamux1 100 0x400 0x01>; +- status = "disabled"; +- }; +- }; +- +- dts: thermal@50028000 { +- compatible = "st,stm32-thermal"; +- reg = <0x50028000 0x100>; +- interrupts = ; +- clocks = <&rcc TMPSENS>; +- clock-names = "pclk"; +- #thermal-sensor-cells = <0>; +- status = "disabled"; +- }; +- +- hash1: hash@54002000 { +- compatible = "st,stm32f756-hash"; +- reg = <0x54002000 0x400>; +- interrupts = ; +- clocks = <&rcc HASH1>; +- resets = <&rcc HASH1_R>; +- dmas = <&mdma1 31 0x10 0x1000A02 0x0 0x0>; +- dma-names = "in"; +- dma-maxburst = <2>; +- status = "disabled"; +- }; +- +- rng1: rng@54003000 { +- compatible = "st,stm32-rng"; +- reg = <0x54003000 0x400>; +- clocks = <&rcc RNG1_K>; +- resets = <&rcc RNG1_R>; +- status = "disabled"; +- }; +- +- mdma1: dma-controller@58000000 { +- compatible = "st,stm32h7-mdma"; +- reg = <0x58000000 0x1000>; +- interrupts = ; +- clocks = <&rcc MDMA>; +- #dma-cells = <5>; +- dma-channels = <32>; +- dma-requests = <48>; +- }; +- +- fmc: nand-controller@58002000 { +- compatible = "st,stm32mp15-fmc2"; +- reg = <0x58002000 0x1000>, +- <0x80000000 0x1000>, +- <0x88010000 0x1000>, +- <0x88020000 0x1000>, +- <0x81000000 0x1000>, +- <0x89010000 0x1000>, +- <0x89020000 0x1000>; +- interrupts = ; +- dmas = <&mdma1 20 0x10 0x12000a02 0x0 0x0>, +- <&mdma1 20 0x10 0x12000a08 0x0 0x0>, +- <&mdma1 21 0x10 0x12000a0a 0x0 0x0>; +- dma-names = "tx", "rx", "ecc"; +- clocks = <&rcc FMC_K>; +- resets = <&rcc FMC_R>; +- status = "disabled"; +- }; +- +- qspi: spi@58003000 { +- compatible = "st,stm32f469-qspi"; +- reg = <0x58003000 0x1000>, <0x70000000 0x10000000>; +- reg-names = "qspi", "qspi_mm"; +- interrupts = ; +- dmas = <&mdma1 22 0x10 0x100002 0x0 0x0>, +- <&mdma1 22 0x10 0x100008 0x0 0x0>; +- dma-names = "tx", "rx"; +- clocks = <&rcc QSPI_K>; +- resets = <&rcc QSPI_R>; +- status = "disabled"; +- }; +- +- sdmmc1: sdmmc@58005000 { +- compatible = "arm,pl18x", "arm,primecell"; +- arm,primecell-periphid = <0x10153180>; +- reg = <0x58005000 0x1000>; +- interrupts = ; +- interrupt-names = "cmd_irq"; +- clocks = <&rcc SDMMC1_K>; +- clock-names = "apb_pclk"; +- resets = <&rcc SDMMC1_R>; +- cap-sd-highspeed; +- cap-mmc-highspeed; +- max-frequency = <120000000>; +- status = "disabled"; +- }; +- +- sdmmc2: sdmmc@58007000 { +- compatible = "arm,pl18x", "arm,primecell"; +- arm,primecell-periphid = <0x10153180>; +- reg = <0x58007000 0x1000>; +- interrupts = ; +- interrupt-names = "cmd_irq"; +- clocks = <&rcc SDMMC2_K>; +- clock-names = "apb_pclk"; +- resets = <&rcc SDMMC2_R>; +- cap-sd-highspeed; +- cap-mmc-highspeed; +- max-frequency = <120000000>; +- status = "disabled"; +- }; +- +- crc1: crc@58009000 { +- compatible = "st,stm32f7-crc"; +- reg = <0x58009000 0x400>; +- clocks = <&rcc CRC1>; +- status = "disabled"; +- }; +- +- stmmac_axi_config_0: stmmac-axi-config { +- snps,wr_osr_lmt = <0x7>; +- snps,rd_osr_lmt = <0x7>; +- snps,blen = <0 0 0 0 16 8 4>; +- }; +- +- ethernet0: ethernet@5800a000 { +- compatible = "st,stm32mp1-dwmac", "snps,dwmac-4.20a"; +- reg = <0x5800a000 0x2000>; +- reg-names = "stmmaceth"; +- interrupts-extended = <&intc GIC_SPI 61 IRQ_TYPE_LEVEL_HIGH>; +- interrupt-names = "macirq"; +- clock-names = "stmmaceth", +- "mac-clk-tx", +- "mac-clk-rx", +- "ethstp"; +- clocks = <&rcc ETHMAC>, +- <&rcc ETHTX>, +- <&rcc ETHRX>, +- <&rcc ETHSTP>; +- st,syscon = <&syscfg 0x4>; +- snps,mixed-burst; +- snps,pbl = <2>; +- snps,en-tx-lpi-clockgating; +- snps,axi-config = <&stmmac_axi_config_0>; +- snps,tso; +- status = "disabled"; +- }; +- +- usbh_ohci: usbh-ohci@5800c000 { +- compatible = "generic-ohci"; +- reg = <0x5800c000 0x1000>; +- clocks = <&rcc USBH>; +- resets = <&rcc USBH_R>; +- interrupts = ; +- status = "disabled"; +- }; +- +- usbh_ehci: usbh-ehci@5800d000 { +- compatible = "generic-ehci"; +- reg = <0x5800d000 0x1000>; +- clocks = <&rcc USBH>; +- resets = <&rcc USBH_R>; +- interrupts = ; +- companion = <&usbh_ohci>; +- status = "disabled"; +- }; +- +- ltdc: display-controller@5a001000 { +- compatible = "st,stm32-ltdc"; +- reg = <0x5a001000 0x400>; +- interrupts = , +- ; +- clocks = <&rcc LTDC_PX>; +- clock-names = "lcd"; +- resets = <&rcc LTDC_R>; +- status = "disabled"; +- }; +- + iwdg2: watchdog@5a002000 { + compatible = "st,stm32mp1-iwdg"; + reg = <0x5a002000 0x400>; ++ interrupts = ; + clocks = <&rcc IWDG2>, <&rcc CK_LSI>; + clock-names = "pclk", "lsi"; + status = "disabled"; + }; + +- usbphyc: usbphyc@5a006000 { +- #address-cells = <1>; +- #size-cells = <0>; +- compatible = "st,stm32mp1-usbphyc"; +- reg = <0x5a006000 0x1000>; +- clocks = <&rcc USBPHY_K>; +- resets = <&rcc USBPHY_R>; +- status = "disabled"; +- +- usbphyc_port0: usb-phy@0 { +- #phy-cells = <0>; +- reg = <0>; +- }; +- +- usbphyc_port1: usb-phy@1 { +- #phy-cells = <1>; +- reg = <1>; +- }; +- }; +- +- usart1: serial@5c000000 { +- compatible = "st,stm32h7-uart"; +- reg = <0x5c000000 0x400>; +- interrupts = ; +- clocks = <&rcc USART1_K>; +- resets = <&rcc USART1_R>; +- status = "disabled"; +- }; +- +- spi6: spi@5c001000 { +- #address-cells = <1>; +- #size-cells = <0>; +- compatible = "st,stm32h7-spi"; +- reg = <0x5c001000 0x400>; +- interrupts = ; +- clocks = <&rcc SPI6_K>; +- resets = <&rcc SPI6_R>; +- dmas = <&mdma1 34 0x0 0x40008 0x0 0x0>, +- <&mdma1 35 0x0 0x40002 0x0 0x0>; +- dma-names = "rx", "tx"; +- status = "disabled"; +- }; +- +- i2c4: i2c@5c002000 { +- compatible = "st,stm32f7-i2c"; +- reg = <0x5c002000 0x400>; +- interrupt-names = "event", "error"; +- interrupts = , +- ; +- clocks = <&rcc I2C4_K>; +- resets = <&rcc I2C4_R>; +- #address-cells = <1>; +- #size-cells = <0>; +- }; +- + rtc: rtc@5c004000 { + compatible = "st,stm32mp1-rtc"; + reg = <0x5c004000 0x400>; + clocks = <&rcc RTCAPB>, <&rcc RTC>; + clock-names = "pclk", "rtc_ck"; +- interrupts = ; + status = "disabled"; + }; + + bsec: efuse@5c005000 { + compatible = "st,stm32mp15-bsec"; + reg = <0x5c005000 0x400>; ++ clocks = <&rcc BSEC>; + #address-cells = <1>; + #size-cells = <1>; ++ ++ cfg0_otp: cfg0_otp@0 { ++ reg = <0x0 0x1>; ++ }; ++ part_number_otp: part_number_otp@4 { ++ reg = <0x4 0x1>; ++ }; ++ monotonic_otp: monotonic_otp@10 { ++ reg = <0x10 0x4>; ++ }; ++ nand_otp: nand_otp@24 { ++ reg = <0x24 0x4>; ++ }; ++ uid_otp: uid_otp@34 { ++ reg = <0x34 0xc>; ++ }; ++ package_otp: package_otp@40 { ++ reg = <0x40 0x4>; ++ }; ++ hw2_otp: hw2_otp@48 { ++ reg = <0x48 0x4>; ++ }; + ts_cal1: calib@5c { + reg = <0x5c 0x2>; + }; + ts_cal2: calib@5e { + reg = <0x5e 0x2>; + }; +- mac_addr: mac_addr@e4 { ++ pkh_otp: pkh_otp@60 { ++ reg = <0x60 0x20>; ++ }; ++ ethernet_mac_address: mac@e4 { + reg = <0xe4 0x8>; + st,non-secure-otp; + }; + }; + +- etzpc: etzpc@5c007000 { +- compatible = "st,stm32-etzpc"; +- reg = <0x5C007000 0x400>; +- clocks = <&rcc TZPC>; +- status = "disabled"; +- secure-status = "okay"; ++ tamp: tamp@5c00a000 { ++ compatible = "st,stm32-tamp", "syscon", "simple-mfd"; ++ reg = <0x5c00a000 0x400>; ++ interrupts = ; ++ clocks = <&rcc RTCAPB>; + }; + +- i2c6: i2c@5c009000 { +- compatible = "st,stm32f7-i2c"; +- reg = <0x5c009000 0x400>; +- interrupt-names = "event", "error"; +- interrupts = , +- ; +- clocks = <&rcc I2C6_K>; +- resets = <&rcc I2C6_R>; +- #address-cells = <1>; +- #size-cells = <0>; +- status = "disabled"; ++ tzc400: tzc@5c006000 { ++ compatible = "st,stm32mp1-tzc"; ++ reg = <0x5c006000 0x1000>; ++ interrupts = ; ++ clocks = <&rcc TZC1>, <&rcc TZC2>; + }; + +- /* +- * Break node order to solve dependency probe issue between +- * pinctrl and exti. +- */ + pinctrl: pin-controller@50002000 { + #address-cells = <1>; + #size-cells = <1>; + compatible = "st,stm32mp157-pinctrl"; + ranges = <0 0x50002000 0xa400>; +- interrupt-parent = <&exti>; +- st,syscfg = <&exti 0x60 0xff>; + pins-are-numbered; + + gpioa: gpio@50002000 { +@@ -1662,8 +448,6 @@ + compatible = "st,stm32mp157-z-pinctrl"; + ranges = <0 0x54004000 0x400>; + pins-are-numbered; +- interrupt-parent = <&exti>; +- st,syscfg = <&exti 0x60 0xff>; + + gpioz: gpio@54004000 { + gpio-controller; +@@ -1677,26 +461,73 @@ + status = "disabled"; + }; + }; +- }; + +- mlahb: ahb { +- compatible = "st,mlahb", "simple-bus"; +- #address-cells = <1>; +- #size-cells = <1>; +- ranges; +- dma-ranges = <0x00000000 0x38000000 0x10000>, +- <0x10000000 0x10000000 0x60000>, +- <0x30000000 0x30000000 0x60000>; ++ etzpc: etzpc@5c007000 { ++ compatible = "st,stm32-etzpc", "firewall-bus"; ++ reg = <0x5C007000 0x400>; ++ clocks = <&rcc TZPC>; ++ #address-cells = <1>; ++ #size-cells = <1>; + +- m4_rproc: m4@10000000 { +- compatible = "st,stm32mp1-m4"; +- reg = <0x10000000 0x40000>, +- <0x30000000 0x40000>, +- <0x38000000 0x10000>; +- resets = <&rcc MCU_R>; +- st,syscfg-holdboot = <&rcc 0x10C 0x1>; +- st,syscfg-tz = <&rcc 0x000 0x1>; +- status = "disabled"; ++ hash1: hash@54002000 { ++ compatible = "st,stm32f756-hash"; ++ reg = <0x54002000 0x400>; ++ interrupts = ; ++ clocks = <&rcc HASH1>; ++ resets = <&rcc HASH1_R>; ++ status = "disabled"; ++ }; ++ ++ rng1: rng@54003000 { ++ compatible = "st,stm32-rng"; ++ reg = <0x54003000 0x400>; ++ clocks = <&rcc RNG1_K>; ++ resets = <&rcc RNG1_R>; ++ status = "disabled"; ++ }; ++ ++ usart1: serial@5c000000 { ++ compatible = "st,stm32h7-uart"; ++ reg = <0x5c000000 0x400>; ++ interrupts = ; ++ clocks = <&rcc USART1_K>; ++ resets = <&rcc USART1_R>; ++ status = "disabled"; ++ }; ++ ++ i2c4: i2c@5c002000 { ++ compatible = "st,stm32mp15-i2c"; ++ reg = <0x5c002000 0x400>; ++ clocks = <&rcc I2C4_K>; ++ resets = <&rcc I2C4_R>; ++ #address-cells = <1>; ++ #size-cells = <0>; ++ status = "disabled"; ++ }; ++ ++ i2c6: i2c@5c009000 { ++ compatible = "st,stm32mp15-i2c"; ++ reg = <0x5c009000 0x400>; ++ clocks = <&rcc I2C6_K>; ++ resets = <&rcc I2C6_R>; ++ #address-cells = <1>; ++ #size-cells = <0>; ++ status = "disabled"; ++ }; ++ ++ iwdg1: watchdog@5c003000 { ++ compatible = "st,stm32mp1-iwdg"; ++ reg = <0x5C003000 0x400>; ++ interrupts = ; ++ clocks = <&rcc IWDG1>, <&rcc CK_LSI>; ++ clock-names = "pclk", "lsi"; ++ status = "disabled"; ++ }; ++ ++ stgen: stgen@5c008000 { ++ compatible = "st,stm32-stgen"; ++ reg = <0x5C008000 0x1000>; ++ }; + }; + }; + }; +diff --git a/core/arch/arm/dts/stm32mp153.dtsi b/core/arch/arm/dts/stm32mp153.dtsi +index 2d759fc60..81e1aefe9 100644 +--- a/core/arch/arm/dts/stm32mp153.dtsi ++++ b/core/arch/arm/dts/stm32mp153.dtsi +@@ -14,32 +14,4 @@ + reg = <1>; + }; + }; +- +- soc { +- m_can1: can@4400e000 { +- compatible = "bosch,m_can"; +- reg = <0x4400e000 0x400>, <0x44011000 0x1400>; +- reg-names = "m_can", "message_ram"; +- interrupts = , +- ; +- interrupt-names = "int0", "int1"; +- clocks = <&rcc CK_HSE>, <&rcc FDCAN_K>; +- clock-names = "hclk", "cclk"; +- bosch,mram-cfg = <0x0 0 0 32 0 0 2 2>; +- status = "disabled"; +- }; +- +- m_can2: can@4400f000 { +- compatible = "bosch,m_can"; +- reg = <0x4400f000 0x400>, <0x44011000 0x2800>; +- reg-names = "m_can", "message_ram"; +- interrupts = , +- ; +- interrupt-names = "int0", "int1"; +- clocks = <&rcc CK_HSE>, <&rcc FDCAN_K>; +- clock-names = "hclk", "cclk"; +- bosch,mram-cfg = <0x1400 0 0 32 0 0 2 2>; +- status = "disabled"; +- }; +- }; + }; +diff --git a/core/arch/arm/dts/stm32mp157.dtsi b/core/arch/arm/dts/stm32mp157.dtsi +index 3f0a4a91c..c83402907 100644 +--- a/core/arch/arm/dts/stm32mp157.dtsi ++++ b/core/arch/arm/dts/stm32mp157.dtsi +@@ -5,27 +5,3 @@ + */ + + #include "stm32mp153.dtsi" +- +-/ { +- soc { +- gpu: gpu@59000000 { +- compatible = "vivante,gc"; +- reg = <0x59000000 0x800>; +- interrupts = ; +- clocks = <&rcc GPU>, <&rcc GPU_K>; +- clock-names = "bus" ,"core"; +- resets = <&rcc GPU_R>; +- status = "disabled"; +- }; +- +- dsi: dsi@5a000000 { +- compatible = "st,stm32-dsi"; +- reg = <0x5a000000 0x800>; +- clocks = <&rcc DSI_K>, <&clk_hse>, <&rcc DSI_PX>; +- clock-names = "pclk", "ref", "px_clk"; +- resets = <&rcc DSI_R>; +- reset-names = "apb"; +- status = "disabled"; +- }; +- }; +-}; +diff --git a/core/arch/arm/dts/stm32mp157a-dk1.dts b/core/arch/arm/dts/stm32mp157a-dk1.dts +index 69f15d735..acc1d7041 100644 +--- a/core/arch/arm/dts/stm32mp157a-dk1.dts ++++ b/core/arch/arm/dts/stm32mp157a-dk1.dts +@@ -7,17 +7,20 @@ + /dts-v1/; + + #include "stm32mp157.dtsi" ++#include "stm32mp15xa.dtsi" + #include "stm32mp15-pinctrl.dtsi" + #include "stm32mp15xxac-pinctrl.dtsi" + #include "stm32mp15xx-dkx.dtsi" ++#include + + / { + model = "STMicroelectronics STM32MP157A-DK1 Discovery Board"; + compatible = "st,stm32mp157a-dk1", "st,stm32mp157"; + + aliases { +- ethernet0 = ðernet0; + serial0 = &uart4; ++ serial1 = &usart3; ++ serial2 = &uart7; + }; + + chosen { +@@ -25,13 +28,22 @@ + }; + }; + +-&rcc { +- status = "okay"; ++&cpu1{ ++ cpu-supply = <&vddcore>; + }; + +-&bsec { +- board_id: board_id@ec { +- reg = <0xec 0x4>; +- st,non-secure-otp; +- }; ++&etzpc { ++ st,decprot = < ++ DECPROT(STM32MP1_ETZPC_USART1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_SPI6_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_I2C4_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_I2C6_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_RNG1_ID, DECPROT_S_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_HASH1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_DDRCTRL_ID, DECPROT_NS_R_S_W, DECPROT_LOCK) ++ DECPROT(STM32MP1_ETZPC_DDRPHYC_ID, DECPROT_NS_R_S_W, DECPROT_LOCK) ++ DECPROT(STM32MP1_ETZPC_STGENC_ID, DECPROT_S_RW, DECPROT_LOCK) ++ DECPROT(STM32MP1_ETZPC_BKPSRAM_ID, DECPROT_S_RW, DECPROT_LOCK) ++ DECPROT(STM32MP1_ETZPC_IWDG1_ID, DECPROT_S_RW, DECPROT_LOCK) ++ >; + }; +diff --git a/core/arch/arm/dts/stm32mp157a-ed1.dts b/core/arch/arm/dts/stm32mp157a-ed1.dts +new file mode 100644 +index 000000000..de8e2c57e +--- /dev/null ++++ b/core/arch/arm/dts/stm32mp157a-ed1.dts +@@ -0,0 +1,43 @@ ++// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) ++/* ++ * Copyright (C) STMicroelectronics 2019 - All Rights Reserved ++ * Author: Alexandre Torgue for STMicroelectronics. ++ */ ++ ++/dts-v1/; ++ ++#include "stm32mp157.dtsi" ++#include "stm32mp15xa.dtsi" ++#include "stm32mp15-pinctrl.dtsi" ++#include "stm32mp15xxaa-pinctrl.dtsi" ++#include "stm32mp15xx-edx.dtsi" ++#include ++ ++/ { ++ model = "STMicroelectronics STM32MP157A eval daughter"; ++ compatible = "st,stm32mp157a-ed1", "st,stm32mp157"; ++ ++ chosen { ++ stdout-path = "serial0:115200n8"; ++ }; ++}; ++ ++&cpu1{ ++ cpu-supply = <&vddcore>; ++}; ++ ++&etzpc { ++ st,decprot = < ++ DECPROT(STM32MP1_ETZPC_USART1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_SPI6_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_I2C4_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_I2C6_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_RNG1_ID, DECPROT_S_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_HASH1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_DDRCTRL_ID, DECPROT_NS_R_S_W, DECPROT_LOCK) ++ DECPROT(STM32MP1_ETZPC_DDRPHYC_ID, DECPROT_NS_R_S_W, DECPROT_LOCK) ++ DECPROT(STM32MP1_ETZPC_STGENC_ID, DECPROT_S_RW, DECPROT_LOCK) ++ DECPROT(STM32MP1_ETZPC_BKPSRAM_ID, DECPROT_S_RW, DECPROT_LOCK) ++ DECPROT(STM32MP1_ETZPC_IWDG1_ID, DECPROT_S_RW, DECPROT_LOCK) ++ >; ++}; +diff --git a/core/arch/arm/dts/stm32mp157a-ev1.dts b/core/arch/arm/dts/stm32mp157a-ev1.dts +new file mode 100644 +index 000000000..76179ccde +--- /dev/null ++++ b/core/arch/arm/dts/stm32mp157a-ev1.dts +@@ -0,0 +1,24 @@ ++// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) ++/* ++ * Copyright (C) STMicroelectronics 2019 - All Rights Reserved ++ * Author: Alexandre Torgue for STMicroelectronics. ++ */ ++ ++/dts-v1/; ++ ++#include "stm32mp157a-ed1.dts" ++#include "stm32mp15xx-evx.dtsi" ++ ++/ { ++ model = "STMicroelectronics STM32MP157A eval daughter on eval mother"; ++ compatible = "st,stm32mp157a-ev1", "st,stm32mp157a-ed1", "st,stm32mp157"; ++ ++ aliases { ++ serial0 = &uart4; ++ }; ++ ++ chosen { ++ stdout-path = "serial0:115200n8"; ++ }; ++}; ++ +diff --git a/core/arch/arm/dts/stm32mp157c-dk2.dts b/core/arch/arm/dts/stm32mp157c-dk2.dts +index 21edea021..e72ab7297 100644 +--- a/core/arch/arm/dts/stm32mp157c-dk2.dts ++++ b/core/arch/arm/dts/stm32mp157c-dk2.dts +@@ -11,14 +11,17 @@ + #include "stm32mp15-pinctrl.dtsi" + #include "stm32mp15xxac-pinctrl.dtsi" + #include "stm32mp15xx-dkx.dtsi" ++#include + + / { + model = "STMicroelectronics STM32MP157C-DK2 Discovery Board"; + compatible = "st,stm32mp157c-dk2", "st,stm32mp157"; + + aliases { +- ethernet0 = ðernet0; + serial0 = &uart4; ++ serial1 = &usart3; ++ serial2 = &uart7; ++ serial3 = &usart2; + }; + + chosen { +@@ -26,80 +29,29 @@ + }; + }; + +-&dsi { +- #address-cells = <1>; +- #size-cells = <0>; +- status = "okay"; +- phy-dsi-supply = <®18>; +- +- ports { +- #address-cells = <1>; +- #size-cells = <0>; +- +- port@0 { +- reg = <0>; +- dsi_in: endpoint { +- remote-endpoint = <<dc_ep1_out>; +- }; +- }; +- +- port@1 { +- reg = <1>; +- dsi_out: endpoint { +- remote-endpoint = <&panel_in>; +- }; +- }; +- }; +- +- panel@0 { +- compatible = "orisetech,otm8009a"; +- reg = <0>; +- reset-gpios = <&gpioe 4 GPIO_ACTIVE_LOW>; +- power-supply = <&v3v3>; +- status = "okay"; +- +- port { +- panel_in: endpoint { +- remote-endpoint = <&dsi_out>; +- }; +- }; +- }; +-}; +- +-&i2c1 { +- touchscreen@38 { +- compatible = "focaltech,ft6236"; +- reg = <0x38>; +- interrupts = <2 2>; +- interrupt-parent = <&gpiof>; +- interrupt-controller; +- touchscreen-size-x = <480>; +- touchscreen-size-y = <800>; +- status = "okay"; +- }; +-}; +- +-<dc { +- status = "okay"; +- +- port { +- #address-cells = <1>; +- #size-cells = <0>; +- +- ltdc_ep1_out: endpoint@1 { +- reg = <1>; +- remote-endpoint = <&dsi_in>; +- }; +- }; ++&cpu1{ ++ cpu-supply = <&vddcore>; + }; + +-&rcc { +- status = "okay"; ++&usart2 { ++ pinctrl-names = "default"; ++ pinctrl-0 = <&usart2_pins_c>; ++ status = "disabled"; + }; + +-&bsec { +- board_id: board_id@ec { +- reg = <0xec 0x4>; +- st,non-secure-otp; +- }; ++&etzpc { ++ st,decprot = < ++ DECPROT(STM32MP1_ETZPC_USART1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_SPI6_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_I2C4_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_I2C6_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_RNG1_ID, DECPROT_S_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_HASH1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_CRYP1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_DDRCTRL_ID, DECPROT_NS_R_S_W, DECPROT_LOCK) ++ DECPROT(STM32MP1_ETZPC_DDRPHYC_ID, DECPROT_NS_R_S_W, DECPROT_LOCK) ++ DECPROT(STM32MP1_ETZPC_STGENC_ID, DECPROT_S_RW, DECPROT_LOCK) ++ DECPROT(STM32MP1_ETZPC_BKPSRAM_ID, DECPROT_S_RW, DECPROT_LOCK) ++ DECPROT(STM32MP1_ETZPC_IWDG1_ID, DECPROT_S_RW, DECPROT_LOCK) ++ >; + }; +diff --git a/core/arch/arm/dts/stm32mp157c-ed1.dts b/core/arch/arm/dts/stm32mp157c-ed1.dts +index 0c4cd1be7..8a6869aff 100644 +--- a/core/arch/arm/dts/stm32mp157c-ed1.dts ++++ b/core/arch/arm/dts/stm32mp157c-ed1.dts +@@ -1,7 +1,7 @@ + // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) + /* +- * Copyright (C) STMicroelectronics 2017 - All Rights Reserved +- * Author: Ludovic Barre for STMicroelectronics. ++ * Copyright (C) STMicroelectronics 2019 - All Rights Reserved ++ * Author: Alexandre Torgue for STMicroelectronics. + */ + /dts-v1/; + +@@ -9,8 +9,8 @@ + #include "stm32mp15xc.dtsi" + #include "stm32mp15-pinctrl.dtsi" + #include "stm32mp15xxaa-pinctrl.dtsi" +-#include +-#include ++#include "stm32mp15xx-edx.dtsi" ++#include + + / { + model = "STMicroelectronics STM32MP157C eval daughter"; +@@ -19,361 +19,25 @@ + chosen { + stdout-path = "serial0:115200n8"; + }; +- +- memory@c0000000 { +- device_type = "memory"; +- reg = <0xC0000000 0x40000000>; +- }; +- +- reserved-memory { +- #address-cells = <1>; +- #size-cells = <1>; +- ranges; +- +- mcuram2: mcuram2@10000000 { +- compatible = "shared-dma-pool"; +- reg = <0x10000000 0x40000>; +- no-map; +- }; +- +- vdev0vring0: vdev0vring0@10040000 { +- compatible = "shared-dma-pool"; +- reg = <0x10040000 0x1000>; +- no-map; +- }; +- +- vdev0vring1: vdev0vring1@10041000 { +- compatible = "shared-dma-pool"; +- reg = <0x10041000 0x1000>; +- no-map; +- }; +- +- vdev0buffer: vdev0buffer@10042000 { +- compatible = "shared-dma-pool"; +- reg = <0x10042000 0x4000>; +- no-map; +- }; +- +- mcuram: mcuram@30000000 { +- compatible = "shared-dma-pool"; +- reg = <0x30000000 0x40000>; +- no-map; +- }; +- +- retram: retram@38000000 { +- compatible = "shared-dma-pool"; +- reg = <0x38000000 0x10000>; +- no-map; +- }; +- +- gpu_reserved: gpu@e8000000 { +- reg = <0xe8000000 0x8000000>; +- no-map; +- }; +- }; +- +- aliases { +- serial0 = &uart4; +- }; +- +- sd_switch: regulator-sd_switch { +- compatible = "regulator-gpio"; +- regulator-name = "sd_switch"; +- regulator-min-microvolt = <1800000>; +- regulator-max-microvolt = <2900000>; +- regulator-type = "voltage"; +- regulator-always-on; +- +- gpios = <&gpiof 14 GPIO_ACTIVE_HIGH>; +- gpios-states = <0>; +- states = <1800000 0x1>, +- <2900000 0x0>; +- }; +-}; +- +-&adc { +- /* ANA0, ANA1 are dedicated pins and don't need pinctrl: only in6. */ +- pinctrl-0 = <&adc1_in6_pins_a>; +- pinctrl-names = "default"; +- vdd-supply = <&vdd>; +- vdda-supply = <&vdda>; +- vref-supply = <&vdda>; +- status = "disabled"; +- adc1: adc@0 { +- st,adc-channels = <0 1 6>; +- /* 16.5 ck_cycles sampling time */ +- st,min-sample-time-nsecs = <400>; +- status = "okay"; +- }; +-}; +- +-&dac { +- pinctrl-names = "default"; +- pinctrl-0 = <&dac_ch1_pins_a &dac_ch2_pins_a>; +- vref-supply = <&vdda>; +- status = "disabled"; +- dac1: dac@1 { +- status = "okay"; +- }; +- dac2: dac@2 { +- status = "okay"; +- }; +-}; +- +-&dts { +- status = "okay"; +-}; +- +-&gpu { +- contiguous-area = <&gpu_reserved>; +- status = "okay"; +-}; +- +-&i2c4 { +- pinctrl-names = "default"; +- pinctrl-0 = <&i2c4_pins_a>; +- i2c-scl-rising-time-ns = <185>; +- i2c-scl-falling-time-ns = <20>; +- status = "okay"; +- /* spare dmas for other usage */ +- /delete-property/dmas; +- /delete-property/dma-names; +- +- pmic: stpmic@33 { +- compatible = "st,stpmic1"; +- reg = <0x33>; +- interrupts-extended = <&gpioa 0 IRQ_TYPE_EDGE_FALLING>; +- interrupt-controller; +- #interrupt-cells = <2>; +- status = "okay"; +- +- regulators { +- compatible = "st,stpmic1-regulators"; +- ldo1-supply = <&v3v3>; +- ldo2-supply = <&v3v3>; +- ldo3-supply = <&vdd_ddr>; +- ldo5-supply = <&v3v3>; +- ldo6-supply = <&v3v3>; +- pwr_sw1-supply = <&bst_out>; +- pwr_sw2-supply = <&bst_out>; +- +- vddcore: buck1 { +- regulator-name = "vddcore"; +- regulator-min-microvolt = <1200000>; +- regulator-max-microvolt = <1350000>; +- regulator-always-on; +- regulator-initial-mode = <0>; +- regulator-over-current-protection; +- }; +- +- vdd_ddr: buck2 { +- regulator-name = "vdd_ddr"; +- regulator-min-microvolt = <1350000>; +- regulator-max-microvolt = <1350000>; +- regulator-always-on; +- regulator-initial-mode = <0>; +- regulator-over-current-protection; +- }; +- +- vdd: buck3 { +- regulator-name = "vdd"; +- regulator-min-microvolt = <3300000>; +- regulator-max-microvolt = <3300000>; +- regulator-always-on; +- st,mask-reset; +- regulator-initial-mode = <0>; +- regulator-over-current-protection; +- }; +- +- v3v3: buck4 { +- regulator-name = "v3v3"; +- regulator-min-microvolt = <3300000>; +- regulator-max-microvolt = <3300000>; +- regulator-always-on; +- regulator-over-current-protection; +- regulator-initial-mode = <0>; +- }; +- +- vdda: ldo1 { +- regulator-name = "vdda"; +- regulator-min-microvolt = <2900000>; +- regulator-max-microvolt = <2900000>; +- interrupts = ; +- }; +- +- v2v8: ldo2 { +- regulator-name = "v2v8"; +- regulator-min-microvolt = <2800000>; +- regulator-max-microvolt = <2800000>; +- interrupts = ; +- }; +- +- vtt_ddr: ldo3 { +- regulator-name = "vtt_ddr"; +- regulator-min-microvolt = <500000>; +- regulator-max-microvolt = <750000>; +- regulator-always-on; +- regulator-over-current-protection; +- }; +- +- vdd_usb: ldo4 { +- regulator-name = "vdd_usb"; +- regulator-min-microvolt = <3300000>; +- regulator-max-microvolt = <3300000>; +- interrupts = ; +- }; +- +- vdd_sd: ldo5 { +- regulator-name = "vdd_sd"; +- regulator-min-microvolt = <2900000>; +- regulator-max-microvolt = <2900000>; +- interrupts = ; +- regulator-boot-on; +- }; +- +- v1v8: ldo6 { +- regulator-name = "v1v8"; +- regulator-min-microvolt = <1800000>; +- regulator-max-microvolt = <1800000>; +- interrupts = ; +- }; +- +- vref_ddr: vref_ddr { +- regulator-name = "vref_ddr"; +- regulator-always-on; +- regulator-over-current-protection; +- }; +- +- bst_out: boost { +- regulator-name = "bst_out"; +- interrupts = ; +- }; +- +- vbus_otg: pwr_sw1 { +- regulator-name = "vbus_otg"; +- interrupts = ; +- }; +- +- vbus_sw: pwr_sw2 { +- regulator-name = "vbus_sw"; +- interrupts = ; +- regulator-active-discharge = <1>; +- }; +- }; +- +- onkey { +- compatible = "st,stpmic1-onkey"; +- interrupts = , ; +- interrupt-names = "onkey-falling", "onkey-rising"; +- power-off-time-sec = <10>; +- status = "okay"; +- }; +- +- watchdog { +- compatible = "st,stpmic1-wdt"; +- status = "disabled"; +- }; +- }; +-}; +- +-&ipcc { +- status = "okay"; +-}; +- +-&iwdg2 { +- timeout-sec = <32>; +- status = "okay"; +-}; +- +-&m4_rproc { +- memory-region = <&retram>, <&mcuram>, <&mcuram2>, <&vdev0vring0>, +- <&vdev0vring1>, <&vdev0buffer>; +- mboxes = <&ipcc 0>, <&ipcc 1>, <&ipcc 2>; +- mbox-names = "vq0", "vq1", "shutdown"; +- interrupt-parent = <&exti>; +- interrupts = <68 1>; +- status = "okay"; +-}; +- +-&pwr_regulators { +- vdd-supply = <&vdd>; +- vdd_3v3_usbfs-supply = <&vdd_usb>; + }; + +-&rng1 { +- status = "okay"; ++&cpu1{ ++ cpu-supply = <&vddcore>; + }; + +-&rtc { +- status = "okay"; +-}; +- +-&sdmmc1 { +- pinctrl-names = "default", "opendrain", "sleep"; +- pinctrl-0 = <&sdmmc1_b4_pins_a &sdmmc1_dir_pins_a>; +- pinctrl-1 = <&sdmmc1_b4_od_pins_a &sdmmc1_dir_pins_a>; +- pinctrl-2 = <&sdmmc1_b4_sleep_pins_a &sdmmc1_dir_sleep_pins_a>; +- broken-cd; +- st,sig-dir; +- st,neg-edge; +- st,use-ckin; +- bus-width = <4>; +- vmmc-supply = <&vdd_sd>; +- vqmmc-supply = <&sd_switch>; +- status = "okay"; +-}; +- +-&sdmmc2 { +- pinctrl-names = "default", "opendrain", "sleep"; +- pinctrl-0 = <&sdmmc2_b4_pins_a &sdmmc2_d47_pins_a>; +- pinctrl-1 = <&sdmmc2_b4_od_pins_a &sdmmc2_d47_pins_a>; +- pinctrl-2 = <&sdmmc2_b4_sleep_pins_a &sdmmc2_d47_sleep_pins_a>; +- non-removable; +- no-sd; +- no-sdio; +- st,neg-edge; +- bus-width = <8>; +- vmmc-supply = <&v3v3>; +- vqmmc-supply = <&v3v3>; +- mmc-ddr-3_3v; +- status = "okay"; +-}; +- +-&timers6 { +- status = "okay"; +- /* spare dmas for other usage */ +- /delete-property/dmas; +- /delete-property/dma-names; +- timer@5 { +- status = "okay"; +- }; +-}; +- +-&uart4 { +- pinctrl-names = "default"; +- pinctrl-0 = <&uart4_pins_a>; +- status = "okay"; +-}; +- +-&usbphyc_port0 { +- phy-supply = <&vdd_usb>; +- vdda1v1-supply = <®11>; +- vdda1v8-supply = <®18>; +-}; +- +-&usbphyc_port1 { +- phy-supply = <&vdd_usb>; +- vdda1v1-supply = <®11>; +- vdda1v8-supply = <®18>; +-}; +- +-&rcc { +- status = "okay"; +-}; +- +-&bsec { +- board_id: board_id@ec { +- reg = <0xec 0x4>; +- st,non-secure-otp; +- }; ++&etzpc { ++ st,decprot = < ++ DECPROT(STM32MP1_ETZPC_USART1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_SPI6_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_I2C4_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_I2C6_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_RNG1_ID, DECPROT_S_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_HASH1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_CRYP1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_DDRCTRL_ID, DECPROT_NS_R_S_W, DECPROT_LOCK) ++ DECPROT(STM32MP1_ETZPC_DDRPHYC_ID, DECPROT_NS_R_S_W, DECPROT_LOCK) ++ DECPROT(STM32MP1_ETZPC_STGENC_ID, DECPROT_S_RW, DECPROT_LOCK) ++ DECPROT(STM32MP1_ETZPC_BKPSRAM_ID, DECPROT_S_RW, DECPROT_LOCK) ++ DECPROT(STM32MP1_ETZPC_IWDG1_ID, DECPROT_S_RW, DECPROT_LOCK) ++ >; + }; +diff --git a/core/arch/arm/dts/stm32mp157c-ev1.dts b/core/arch/arm/dts/stm32mp157c-ev1.dts +index fd8c958b0..8fec47b7f 100644 +--- a/core/arch/arm/dts/stm32mp157c-ev1.dts ++++ b/core/arch/arm/dts/stm32mp157c-ev1.dts +@@ -1,13 +1,12 @@ + // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) + /* +- * Copyright (C) STMicroelectronics 2017 - All Rights Reserved +- * Author: Ludovic Barre for STMicroelectronics. ++ * Copyright (C) STMicroelectronics 2019 - All Rights Reserved ++ * Author: Alexandre Torgue for STMicroelectronics. + */ + /dts-v1/; + + #include "stm32mp157c-ed1.dts" +-#include +-/* #include Remove due to BSD license issue */ ++#include "stm32mp15xx-evx.dtsi" + + / { + model = "STMicroelectronics STM32MP157C eval daughter on eval mother"; +@@ -19,346 +18,5 @@ + + aliases { + serial0 = &uart4; +- ethernet0 = ðernet0; + }; +- +- clocks { +- clk_ext_camera: clk-ext-camera { +- #clock-cells = <0>; +- compatible = "fixed-clock"; +- clock-frequency = <24000000>; +- }; +- }; +- +- joystick { +- compatible = "gpio-keys"; +- pinctrl-0 = <&joystick_pins>; +- pinctrl-names = "default"; +- button-0 { +- label = "JoySel"; +- /* linux,code = ; BSD license issue */ +- interrupt-parent = <&stmfx_pinctrl>; +- interrupts = <0 IRQ_TYPE_EDGE_RISING>; +- }; +- button-1 { +- label = "JoyDown"; +- /* linux,code = ; BSD license issue */ +- interrupt-parent = <&stmfx_pinctrl>; +- interrupts = <1 IRQ_TYPE_EDGE_RISING>; +- }; +- button-2 { +- label = "JoyLeft"; +- /* linux,code = ; BSD license issue */ +- interrupt-parent = <&stmfx_pinctrl>; +- interrupts = <2 IRQ_TYPE_EDGE_RISING>; +- }; +- button-3 { +- label = "JoyRight"; +- /* linux,code = ; BSD license issue */ +- interrupt-parent = <&stmfx_pinctrl>; +- interrupts = <3 IRQ_TYPE_EDGE_RISING>; +- }; +- button-4 { +- label = "JoyUp"; +- /* linux,code = ; BSD license issue */ +- interrupt-parent = <&stmfx_pinctrl>; +- interrupts = <4 IRQ_TYPE_EDGE_RISING>; +- }; +- }; +- +- panel_backlight: panel-backlight { +- compatible = "gpio-backlight"; +- gpios = <&gpiod 13 GPIO_ACTIVE_LOW>; +- default-on; +- status = "okay"; +- }; +-}; +- +-&cec { +- pinctrl-names = "default"; +- pinctrl-0 = <&cec_pins_a>; +- status = "okay"; +-}; +- +-&dcmi { +- status = "okay"; +- pinctrl-names = "default", "sleep"; +- pinctrl-0 = <&dcmi_pins_a>; +- pinctrl-1 = <&dcmi_sleep_pins_a>; +- +- port { +- dcmi_0: endpoint { +- remote-endpoint = <&ov5640_0>; +- bus-width = <8>; +- hsync-active = <0>; +- vsync-active = <0>; +- pclk-sample = <1>; +- }; +- }; +-}; +- +-&dsi { +- #address-cells = <1>; +- #size-cells = <0>; +- phy-dsi-supply = <®18>; +- status = "okay"; +- +- ports { +- #address-cells = <1>; +- #size-cells = <0>; +- +- port@0 { +- reg = <0>; +- dsi_in: endpoint { +- remote-endpoint = <<dc_ep0_out>; +- }; +- }; +- +- port@1 { +- reg = <1>; +- dsi_out: endpoint { +- remote-endpoint = <&dsi_panel_in>; +- }; +- }; +- }; +- +- panel-dsi@0 { +- compatible = "raydium,rm68200"; +- reg = <0>; +- reset-gpios = <&gpiof 15 GPIO_ACTIVE_LOW>; +- backlight = <&panel_backlight>; +- power-supply = <&v3v3>; +- status = "okay"; +- +- port { +- dsi_panel_in: endpoint { +- remote-endpoint = <&dsi_out>; +- }; +- }; +- }; +-}; +- +-ðernet0 { +- status = "okay"; +- pinctrl-0 = <ðernet0_rgmii_pins_a>; +- pinctrl-1 = <ðernet0_rgmii_pins_sleep_a>; +- pinctrl-names = "default", "sleep"; +- phy-mode = "rgmii-id"; +- max-speed = <1000>; +- phy-handle = <&phy0>; +- +- mdio0 { +- #address-cells = <1>; +- #size-cells = <0>; +- compatible = "snps,dwmac-mdio"; +- phy0: ethernet-phy@0 { +- reg = <0>; +- }; +- }; +-}; +- +-&fmc { +- pinctrl-names = "default", "sleep"; +- pinctrl-0 = <&fmc_pins_a>; +- pinctrl-1 = <&fmc_sleep_pins_a>; +- status = "okay"; +- #address-cells = <1>; +- #size-cells = <0>; +- +- nand@0 { +- reg = <0>; +- nand-on-flash-bbt; +- #address-cells = <1>; +- #size-cells = <1>; +- }; +-}; +- +-&i2c2 { +- pinctrl-names = "default"; +- pinctrl-0 = <&i2c2_pins_a>; +- i2c-scl-rising-time-ns = <185>; +- i2c-scl-falling-time-ns = <20>; +- status = "okay"; +- +- ov5640: camera@3c { +- compatible = "ovti,ov5640"; +- reg = <0x3c>; +- clocks = <&clk_ext_camera>; +- clock-names = "xclk"; +- DOVDD-supply = <&v2v8>; +- powerdown-gpios = <&stmfx_pinctrl 18 (GPIO_ACTIVE_HIGH | GPIO_PUSH_PULL)>; +- reset-gpios = <&stmfx_pinctrl 19 (GPIO_ACTIVE_LOW | GPIO_PUSH_PULL)>; +- rotation = <180>; +- status = "okay"; +- +- port { +- ov5640_0: endpoint { +- remote-endpoint = <&dcmi_0>; +- bus-width = <8>; +- data-shift = <2>; /* lines 9:2 are used */ +- hsync-active = <0>; +- vsync-active = <0>; +- pclk-sample = <1>; +- }; +- }; +- }; +- +- stmfx: stmfx@42 { +- compatible = "st,stmfx-0300"; +- reg = <0x42>; +- interrupts = <8 IRQ_TYPE_EDGE_RISING>; +- interrupt-parent = <&gpioi>; +- vdd-supply = <&v3v3>; +- +- stmfx_pinctrl: stmfx-pin-controller { +- compatible = "st,stmfx-0300-pinctrl"; +- gpio-controller; +- #gpio-cells = <2>; +- interrupt-controller; +- #interrupt-cells = <2>; +- gpio-ranges = <&stmfx_pinctrl 0 0 24>; +- +- joystick_pins: joystick { +- pins = "gpio0", "gpio1", "gpio2", "gpio3", "gpio4"; +- bias-pull-down; +- }; +- }; +- }; +-}; +- +-&i2c5 { +- pinctrl-names = "default"; +- pinctrl-0 = <&i2c5_pins_a>; +- i2c-scl-rising-time-ns = <185>; +- i2c-scl-falling-time-ns = <20>; +- status = "okay"; +-}; +- +-<dc { +- status = "okay"; +- +- port { +- #address-cells = <1>; +- #size-cells = <0>; +- +- ltdc_ep0_out: endpoint@0 { +- reg = <0>; +- remote-endpoint = <&dsi_in>; +- }; +- }; +-}; +- +-&m_can1 { +- pinctrl-names = "default", "sleep"; +- pinctrl-0 = <&m_can1_pins_a>; +- pinctrl-1 = <&m_can1_sleep_pins_a>; +- status = "okay"; +-}; +- +-&qspi { +- pinctrl-names = "default", "sleep"; +- pinctrl-0 = <&qspi_clk_pins_a &qspi_bk1_pins_a &qspi_bk2_pins_a>; +- pinctrl-1 = <&qspi_clk_sleep_pins_a &qspi_bk1_sleep_pins_a &qspi_bk2_sleep_pins_a>; +- reg = <0x58003000 0x1000>, <0x70000000 0x4000000>; +- #address-cells = <1>; +- #size-cells = <0>; +- status = "okay"; +- +- flash0: mx66l51235l@0 { +- compatible = "jedec,spi-nor"; +- reg = <0>; +- spi-rx-bus-width = <4>; +- spi-max-frequency = <108000000>; +- #address-cells = <1>; +- #size-cells = <1>; +- }; +- +- flash1: mx66l51235l@1 { +- compatible = "jedec,spi-nor"; +- reg = <1>; +- spi-rx-bus-width = <4>; +- spi-max-frequency = <108000000>; +- #address-cells = <1>; +- #size-cells = <1>; +- }; +-}; +- +-&sdmmc3 { +- pinctrl-names = "default", "opendrain", "sleep"; +- pinctrl-0 = <&sdmmc3_b4_pins_a>; +- pinctrl-1 = <&sdmmc3_b4_od_pins_a>; +- pinctrl-2 = <&sdmmc3_b4_sleep_pins_a>; +- broken-cd; +- st,neg-edge; +- bus-width = <4>; +- vmmc-supply = <&v3v3>; +- status = "disabled"; +-}; +- +-&spi1 { +- pinctrl-names = "default"; +- pinctrl-0 = <&spi1_pins_a>; +- status = "disabled"; +-}; +- +-&timers2 { +- /* spare dmas for other usage (un-delete to enable pwm capture) */ +- /delete-property/dmas; +- /delete-property/dma-names; +- status = "disabled"; +- pwm { +- pinctrl-0 = <&pwm2_pins_a>; +- pinctrl-1 = <&pwm2_sleep_pins_a>; +- pinctrl-names = "default", "sleep"; +- status = "okay"; +- }; +- timer@1 { +- status = "okay"; +- }; +-}; +- +-&timers8 { +- /delete-property/dmas; +- /delete-property/dma-names; +- status = "disabled"; +- pwm { +- pinctrl-0 = <&pwm8_pins_a>; +- pinctrl-1 = <&pwm8_sleep_pins_a>; +- pinctrl-names = "default", "sleep"; +- status = "okay"; +- }; +- timer@7 { +- status = "okay"; +- }; +-}; +- +-&timers12 { +- /delete-property/dmas; +- /delete-property/dma-names; +- status = "disabled"; +- pwm { +- pinctrl-0 = <&pwm12_pins_a>; +- pinctrl-1 = <&pwm12_sleep_pins_a>; +- pinctrl-names = "default", "sleep"; +- status = "okay"; +- }; +- timer@11 { +- status = "okay"; +- }; +-}; +- +-&usbh_ehci { +- phys = <&usbphyc_port0>; +- status = "okay"; +-}; +- +-&usbotg_hs { +- dr_mode = "peripheral"; +- phys = <&usbphyc_port1 0>; +- phy-names = "usb2-phy"; +- status = "okay"; +-}; +- +-&usbphyc { +- status = "okay"; + }; +diff --git a/core/arch/arm/dts/stm32mp157d-dk1.dts b/core/arch/arm/dts/stm32mp157d-dk1.dts +new file mode 100644 +index 000000000..59bda553f +--- /dev/null ++++ b/core/arch/arm/dts/stm32mp157d-dk1.dts +@@ -0,0 +1,49 @@ ++// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) ++/* ++ * Copyright (C) STMicroelectronics 2019 - All Rights Reserved ++ * Author: Alexandre Torgue for STMicroelectronics. ++ */ ++ ++/dts-v1/; ++ ++#include "stm32mp157.dtsi" ++#include "stm32mp15xd.dtsi" ++#include "stm32mp15-pinctrl.dtsi" ++#include "stm32mp15xxac-pinctrl.dtsi" ++#include "stm32mp15xx-dkx.dtsi" ++#include ++ ++/ { ++ model = "STMicroelectronics STM32MP157D-DK1 Discovery Board"; ++ compatible = "st,stm32mp157d-dk1", "st,stm32mp157"; ++ ++ aliases { ++ serial0 = &uart4; ++ serial1 = &usart3; ++ serial2 = &uart7; ++ }; ++ ++ chosen { ++ stdout-path = "serial0:115200n8"; ++ }; ++}; ++ ++&cpu1{ ++ cpu-supply = <&vddcore>; ++}; ++ ++&etzpc { ++ st,decprot = < ++ DECPROT(STM32MP1_ETZPC_USART1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_SPI6_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_I2C4_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_I2C6_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_RNG1_ID, DECPROT_S_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_HASH1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_DDRCTRL_ID, DECPROT_NS_R_S_W, DECPROT_LOCK) ++ DECPROT(STM32MP1_ETZPC_DDRPHYC_ID, DECPROT_NS_R_S_W, DECPROT_LOCK) ++ DECPROT(STM32MP1_ETZPC_STGENC_ID, DECPROT_S_RW, DECPROT_LOCK) ++ DECPROT(STM32MP1_ETZPC_BKPSRAM_ID, DECPROT_S_RW, DECPROT_LOCK) ++ DECPROT(STM32MP1_ETZPC_IWDG1_ID, DECPROT_S_RW, DECPROT_LOCK) ++ >; ++}; +diff --git a/core/arch/arm/dts/stm32mp157d-ed1.dts b/core/arch/arm/dts/stm32mp157d-ed1.dts +new file mode 100644 +index 000000000..b5668569a +--- /dev/null ++++ b/core/arch/arm/dts/stm32mp157d-ed1.dts +@@ -0,0 +1,43 @@ ++// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) ++/* ++ * Copyright (C) STMicroelectronics 2019 - All Rights Reserved ++ * Author: Alexandre Torgue for STMicroelectronics. ++ */ ++ ++/dts-v1/; ++ ++#include "stm32mp157.dtsi" ++#include "stm32mp15xd.dtsi" ++#include "stm32mp15-pinctrl.dtsi" ++#include "stm32mp15xxaa-pinctrl.dtsi" ++#include "stm32mp15xx-edx.dtsi" ++#include ++ ++/ { ++ model = "STMicroelectronics STM32MP157D eval daughter"; ++ compatible = "st,stm32mp157d-ed1", "st,stm32mp157"; ++ ++ chosen { ++ stdout-path = "serial0:115200n8"; ++ }; ++}; ++ ++&cpu1{ ++ cpu-supply = <&vddcore>; ++}; ++ ++&etzpc { ++ st,decprot = < ++ DECPROT(STM32MP1_ETZPC_USART1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_SPI6_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_I2C4_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_I2C6_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_RNG1_ID, DECPROT_S_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_HASH1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_DDRCTRL_ID, DECPROT_NS_R_S_W, DECPROT_LOCK) ++ DECPROT(STM32MP1_ETZPC_DDRPHYC_ID, DECPROT_NS_R_S_W, DECPROT_LOCK) ++ DECPROT(STM32MP1_ETZPC_STGENC_ID, DECPROT_S_RW, DECPROT_LOCK) ++ DECPROT(STM32MP1_ETZPC_BKPSRAM_ID, DECPROT_S_RW, DECPROT_LOCK) ++ DECPROT(STM32MP1_ETZPC_IWDG1_ID, DECPROT_S_RW, DECPROT_LOCK) ++ >; ++}; +diff --git a/core/arch/arm/dts/stm32mp157d-ev1.dts b/core/arch/arm/dts/stm32mp157d-ev1.dts +new file mode 100644 +index 000000000..5e1b64c47 +--- /dev/null ++++ b/core/arch/arm/dts/stm32mp157d-ev1.dts +@@ -0,0 +1,24 @@ ++// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) ++/* ++ * Copyright (C) STMicroelectronics 2019 - All Rights Reserved ++ * Author: Alexandre Torgue for STMicroelectronics. ++ */ ++ ++/dts-v1/; ++ ++#include "stm32mp157d-ed1.dts" ++#include "stm32mp15xx-evx.dtsi" ++ ++/ { ++ model = "STMicroelectronics STM32MP157D eval daughter on eval mother"; ++ compatible = "st,stm32mp157d-ev1", "st,stm32mp157d-ed1", "st,stm32mp157"; ++ ++ chosen { ++ stdout-path = "serial0:115200n8"; ++ }; ++ ++ aliases { ++ serial0 = &uart4; ++ serial1 = &usart3; ++ }; ++}; +diff --git a/core/arch/arm/dts/stm32mp157f-dk2.dts b/core/arch/arm/dts/stm32mp157f-dk2.dts +new file mode 100644 +index 000000000..3a14cd98b +--- /dev/null ++++ b/core/arch/arm/dts/stm32mp157f-dk2.dts +@@ -0,0 +1,51 @@ ++// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) ++/* ++ * Copyright (C) STMicroelectronics 2019 - All Rights Reserved ++ * Author: Alexandre Torgue for STMicroelectronics. ++ */ ++ ++/dts-v1/; ++ ++#include "stm32mp157.dtsi" ++#include "stm32mp15xf.dtsi" ++#include "stm32mp15-pinctrl.dtsi" ++#include "stm32mp15xxac-pinctrl.dtsi" ++#include "stm32mp15xx-dkx.dtsi" ++#include ++ ++/ { ++ model = "STMicroelectronics STM32MP157F-DK2 Discovery Board"; ++ compatible = "st,stm32mp157f-dk2", "st,stm32mp157"; ++ ++ aliases { ++ serial0 = &uart4; ++ serial1 = &usart3; ++ serial2 = &uart7; ++ serial3 = &usart2; ++ }; ++ ++ chosen { ++ stdout-path = "serial0:115200n8"; ++ }; ++}; ++ ++&cpu1{ ++ cpu-supply = <&vddcore>; ++}; ++ ++&etzpc { ++ st,decprot = < ++ DECPROT(STM32MP1_ETZPC_USART1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_SPI6_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_I2C4_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_I2C6_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_RNG1_ID, DECPROT_S_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_HASH1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_CRYP1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_DDRCTRL_ID, DECPROT_NS_R_S_W, DECPROT_LOCK) ++ DECPROT(STM32MP1_ETZPC_DDRPHYC_ID, DECPROT_NS_R_S_W, DECPROT_LOCK) ++ DECPROT(STM32MP1_ETZPC_STGENC_ID, DECPROT_S_RW, DECPROT_LOCK) ++ DECPROT(STM32MP1_ETZPC_BKPSRAM_ID, DECPROT_S_RW, DECPROT_LOCK) ++ DECPROT(STM32MP1_ETZPC_IWDG1_ID, DECPROT_S_RW, DECPROT_LOCK) ++ >; ++}; +diff --git a/core/arch/arm/dts/stm32mp157f-ed1.dts b/core/arch/arm/dts/stm32mp157f-ed1.dts +new file mode 100644 +index 000000000..539e373bc +--- /dev/null ++++ b/core/arch/arm/dts/stm32mp157f-ed1.dts +@@ -0,0 +1,44 @@ ++// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) ++/* ++ * Copyright (C) STMicroelectronics 2019 - All Rights Reserved ++ * Author: Alexandre Torgue for STMicroelectronics. ++ */ ++ ++/dts-v1/; ++ ++#include "stm32mp157.dtsi" ++#include "stm32mp15xf.dtsi" ++#include "stm32mp15-pinctrl.dtsi" ++#include "stm32mp15xxaa-pinctrl.dtsi" ++#include "stm32mp15xx-edx.dtsi" ++#include ++ ++/ { ++ model = "STMicroelectronics STM32MP157F eval daughter"; ++ compatible = "st,stm32mp157f-ed1", "st,stm32mp157"; ++ ++ chosen { ++ stdout-path = "serial0:115200n8"; ++ }; ++}; ++ ++&cpu1{ ++ cpu-supply = <&vddcore>; ++}; ++ ++&etzpc { ++ st,decprot = < ++ DECPROT(STM32MP1_ETZPC_USART1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_SPI6_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_I2C4_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_I2C6_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_RNG1_ID, DECPROT_S_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_HASH1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_CRYP1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_DDRCTRL_ID, DECPROT_NS_R_S_W, DECPROT_LOCK) ++ DECPROT(STM32MP1_ETZPC_DDRPHYC_ID, DECPROT_NS_R_S_W, DECPROT_LOCK) ++ DECPROT(STM32MP1_ETZPC_STGENC_ID, DECPROT_S_RW, DECPROT_LOCK) ++ DECPROT(STM32MP1_ETZPC_BKPSRAM_ID, DECPROT_S_RW, DECPROT_LOCK) ++ DECPROT(STM32MP1_ETZPC_IWDG1_ID, DECPROT_S_RW, DECPROT_LOCK) ++ >; ++}; +diff --git a/core/arch/arm/dts/stm32mp157f-ev1.dts b/core/arch/arm/dts/stm32mp157f-ev1.dts +new file mode 100644 +index 000000000..19284bbbf +--- /dev/null ++++ b/core/arch/arm/dts/stm32mp157f-ev1.dts +@@ -0,0 +1,24 @@ ++// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) ++/* ++ * Copyright (C) STMicroelectronics 2019 - All Rights Reserved ++ * Author: Alexandre Torgue for STMicroelectronics. ++ */ ++ ++/dts-v1/; ++ ++#include "stm32mp157f-ed1.dts" ++#include "stm32mp15xx-evx.dtsi" ++ ++/ { ++ model = "STMicroelectronics STM32MP157F eval daughter on eval mother"; ++ compatible = "st,stm32mp157f-ev1", "st,stm32mp157f-ed1", "st,stm32mp157"; ++ ++ chosen { ++ stdout-path = "serial0:115200n8"; ++ }; ++ ++ aliases { ++ serial0 = &uart4; ++ serial1 = &usart3; ++ }; ++}; +diff --git a/core/arch/arm/dts/stm32mp15xa.dtsi b/core/arch/arm/dts/stm32mp15xa.dtsi +new file mode 100644 +index 000000000..3df72ec64 +--- /dev/null ++++ b/core/arch/arm/dts/stm32mp15xa.dtsi +@@ -0,0 +1,14 @@ ++// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) ++/* ++ * Copyright (C) STMicroelectronics 2019 - All Rights Reserved ++ * Author: Alexandre Torgue for STMicroelectronics. ++ */ ++ ++&cpu0_opp_table { ++ opp-650000000 { ++ opp-hz = /bits/ 64 <650000000>; ++ opp-microvolt = <1200000>; ++ opp-supported-hw = <0x1>; ++ st,opp-default; ++ }; ++}; +diff --git a/core/arch/arm/dts/stm32mp15xc.dtsi b/core/arch/arm/dts/stm32mp15xc.dtsi +index b06a55a2f..8fb63a638 100644 +--- a/core/arch/arm/dts/stm32mp15xc.dtsi ++++ b/core/arch/arm/dts/stm32mp15xc.dtsi +@@ -4,15 +4,15 @@ + * Author: Alexandre Torgue for STMicroelectronics. + */ + +-/ { +- soc { +- cryp1: cryp@54001000 { +- compatible = "st,stm32mp1-cryp"; +- reg = <0x54001000 0x400>; +- interrupts = ; +- clocks = <&rcc CRYP1>; +- resets = <&rcc CRYP1_R>; +- status = "disabled"; +- }; ++#include "stm32mp15xa.dtsi" ++ ++&etzpc { ++ cryp1: cryp@54001000 { ++ compatible = "st,stm32mp1-cryp"; ++ reg = <0x54001000 0x400>; ++ interrupts = ; ++ clocks = <&rcc CRYP1>; ++ resets = <&rcc CRYP1_R>; ++ status = "disabled"; + }; + }; +diff --git a/core/arch/arm/dts/stm32mp15xd.dtsi b/core/arch/arm/dts/stm32mp15xd.dtsi +new file mode 100644 +index 000000000..bf4ee9572 +--- /dev/null ++++ b/core/arch/arm/dts/stm32mp15xd.dtsi +@@ -0,0 +1,20 @@ ++// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) ++/* ++ * Copyright (C) STMicroelectronics 2019 - All Rights Reserved ++ * Author: Alexandre Torgue for STMicroelectronics. ++ */ ++ ++&cpu0_opp_table { ++ opp-800000000 { ++ opp-hz = /bits/ 64 <800000000>; ++ opp-microvolt = <1350000>; ++ opp-supported-hw = <0x2>; ++ st,opp-default; ++ }; ++ opp-400000000 { ++ opp-hz = /bits/ 64 <400000000>; ++ opp-microvolt = <1200000>; ++ opp-supported-hw = <0x2>; ++ opp-suspend; ++ }; ++}; +diff --git a/core/arch/arm/dts/stm32mp15xf.dtsi b/core/arch/arm/dts/stm32mp15xf.dtsi +new file mode 100644 +index 000000000..f27a0e2d6 +--- /dev/null ++++ b/core/arch/arm/dts/stm32mp15xf.dtsi +@@ -0,0 +1,18 @@ ++// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) ++/* ++ * Copyright (C) STMicroelectronics 2019 - All Rights Reserved ++ * Author: Alexandre Torgue for STMicroelectronics. ++ */ ++ ++#include "stm32mp15xd.dtsi" ++ ++&etzpc { ++ cryp1: cryp@54001000 { ++ compatible = "st,stm32mp1-cryp"; ++ reg = <0x54001000 0x400>; ++ interrupts = ; ++ clocks = <&rcc CRYP1>; ++ resets = <&rcc CRYP1_R>; ++ status = "disabled"; ++ }; ++}; +diff --git a/core/arch/arm/dts/stm32mp15xx-dkx.dtsi b/core/arch/arm/dts/stm32mp15xx-dkx.dtsi +index f6672e87a..a4d6d1603 100644 +--- a/core/arch/arm/dts/stm32mp15xx-dkx.dtsi ++++ b/core/arch/arm/dts/stm32mp15xx-dkx.dtsi +@@ -4,8 +4,8 @@ + * Author: Alexandre Torgue for STMicroelectronics. + */ + +-#include +-#include ++#include ++#include + + / { + memory@c0000000 { +@@ -13,231 +13,60 @@ + reg = <0xc0000000 0x20000000>; + }; + +- reserved-memory { +- #address-cells = <1>; +- #size-cells = <1>; +- ranges; +- +- mcuram2: mcuram2@10000000 { +- compatible = "shared-dma-pool"; +- reg = <0x10000000 0x40000>; +- no-map; +- }; +- +- vdev0vring0: vdev0vring0@10040000 { +- compatible = "shared-dma-pool"; +- reg = <0x10040000 0x1000>; +- no-map; +- }; +- +- vdev0vring1: vdev0vring1@10041000 { +- compatible = "shared-dma-pool"; +- reg = <0x10041000 0x1000>; +- no-map; +- }; +- +- vdev0buffer: vdev0buffer@10042000 { +- compatible = "shared-dma-pool"; +- reg = <0x10042000 0x4000>; +- no-map; +- }; +- +- mcuram: mcuram@30000000 { +- compatible = "shared-dma-pool"; +- reg = <0x30000000 0x40000>; +- no-map; +- }; +- +- retram: retram@38000000 { +- compatible = "shared-dma-pool"; +- reg = <0x38000000 0x10000>; +- no-map; +- }; +- +- gpu_reserved: gpu@d4000000 { +- reg = <0xd4000000 0x4000000>; +- no-map; +- }; +- }; +- +- led { +- compatible = "gpio-leds"; +- blue { +- label = "heartbeat"; +- gpios = <&gpiod 11 GPIO_ACTIVE_HIGH>; +- linux,default-trigger = "heartbeat"; +- default-state = "off"; +- }; +- }; +- +- sound { +- compatible = "audio-graph-card"; +- label = "STM32MP1-DK"; +- routing = +- "Playback" , "MCLK", +- "Capture" , "MCLK", +- "MICL" , "Mic Bias"; +- dais = <&sai2a_port &sai2b_port &i2s2_port>; +- status = "okay"; +- }; +-}; +- +-&adc { +- pinctrl-names = "default"; +- pinctrl-0 = <&adc12_ain_pins_a>, <&adc12_usb_cc_pins_a>; +- vdd-supply = <&vdd>; +- vdda-supply = <&vdd>; +- vref-supply = <&vrefbuf>; +- status = "disabled"; +- adc1: adc@0 { +- /* +- * Type-C USB_PWR_CC1 & USB_PWR_CC2 on in18 & in19. +- * Use at least 5 * RC time, e.g. 5 * (Rp + Rd) * C: +- * 5 * (56 + 47kOhms) * 5pF => 2.5us. +- * Use arbitrary margin here (e.g. 5us). +- */ +- st,min-sample-time-nsecs = <5000>; +- /* AIN connector, USB Type-C CC1 & CC2 */ +- st,adc-channels = <0 1 6 13 18 19>; +- status = "okay"; +- }; +- adc2: adc@100 { +- /* AIN connector, USB Type-C CC1 & CC2 */ +- st,adc-channels = <0 1 2 6 18 19>; +- st,min-sample-time-nsecs = <5000>; +- status = "okay"; ++ vin: vin { ++ compatible = "regulator-fixed"; ++ regulator-name = "vin"; ++ regulator-min-microvolt = <5000000>; ++ regulator-max-microvolt = <5000000>; ++ regulator-always-on; + }; + }; + +-&cec { +- pinctrl-names = "default", "sleep"; +- pinctrl-0 = <&cec_pins_b>; +- pinctrl-1 = <&cec_pins_sleep_b>; +- status = "okay"; +-}; +- +-ðernet0 { +- status = "okay"; +- pinctrl-0 = <ðernet0_rgmii_pins_a>; +- pinctrl-1 = <ðernet0_rgmii_pins_sleep_a>; +- pinctrl-names = "default", "sleep"; +- phy-mode = "rgmii-id"; +- max-speed = <1000>; +- phy-handle = <&phy0>; +- +- mdio0 { +- #address-cells = <1>; +- #size-cells = <0>; +- compatible = "snps,dwmac-mdio"; +- phy0: ethernet-phy@0 { +- reg = <0>; +- }; ++&bsec { ++ board_id: board_id@ec { ++ reg = <0xec 0x4>; ++ st,non-secure-otp; + }; + }; + +-&gpu { +- contiguous-area = <&gpu_reserved>; +- status = "okay"; ++&clk_hse { ++ st,digbypass; + }; + +-&i2c1 { +- pinctrl-names = "default", "sleep"; +- pinctrl-0 = <&i2c1_pins_a>; +- pinctrl-1 = <&i2c1_pins_sleep_a>; +- i2c-scl-rising-time-ns = <100>; +- i2c-scl-falling-time-ns = <7>; +- status = "okay"; +- /delete-property/dmas; +- /delete-property/dma-names; +- +- hdmi-transmitter@39 { +- compatible = "sil,sii9022"; +- reg = <0x39>; +- iovcc-supply = <&v3v3_hdmi>; +- cvcc12-supply = <&v1v2_hdmi>; +- reset-gpios = <&gpioa 10 GPIO_ACTIVE_LOW>; +- interrupts = <1 IRQ_TYPE_EDGE_FALLING>; +- interrupt-parent = <&gpiog>; +- #sound-dai-cells = <0>; +- status = "okay"; +- +- ports { +- #address-cells = <1>; +- #size-cells = <0>; +- +- port@0 { +- reg = <0>; +- sii9022_in: endpoint { +- remote-endpoint = <<dc_ep0_out>; +- }; +- }; +- +- port@3 { +- reg = <3>; +- sii9022_tx_endpoint: endpoint { +- remote-endpoint = <&i2s2_endpoint>; +- }; +- }; +- }; +- }; +- +- cs42l51: cs42l51@4a { +- compatible = "cirrus,cs42l51"; +- reg = <0x4a>; +- #sound-dai-cells = <0>; +- VL-supply = <&v3v3>; +- VD-supply = <&v1v8_audio>; +- VA-supply = <&v1v8_audio>; +- VAHP-supply = <&v1v8_audio>; +- reset-gpios = <&gpiog 9 GPIO_ACTIVE_LOW>; +- clocks = <&sai2a>; +- clock-names = "MCLK"; +- status = "okay"; +- +- cs42l51_port: port { +- #address-cells = <1>; +- #size-cells = <0>; +- +- cs42l51_tx_endpoint: endpoint@0 { +- reg = <0>; +- remote-endpoint = <&sai2a_endpoint>; +- frame-master; +- bitclock-master; +- }; +- +- cs42l51_rx_endpoint: endpoint@1 { +- reg = <1>; +- remote-endpoint = <&sai2b_endpoint>; +- frame-master; +- bitclock-master; +- }; +- }; +- }; ++&cpu0{ ++ cpu-supply = <&vddcore>; + }; + + &i2c4 { ++ compatible = "st,stm32mp15-i2c-non-secure"; + pinctrl-names = "default"; + pinctrl-0 = <&i2c4_pins_a>; + i2c-scl-rising-time-ns = <185>; + i2c-scl-falling-time-ns = <20>; ++ clock-frequency = <400000>; + status = "okay"; +- /* spare dmas for other usage */ +- /delete-property/dmas; +- /delete-property/dma-names; + + pmic: stpmic@33 { + compatible = "st,stpmic1"; + reg = <0x33>; +- interrupts-extended = <&gpioa 0 IRQ_TYPE_EDGE_FALLING>; + interrupt-controller; + #interrupt-cells = <2>; + status = "okay"; + + regulators { + compatible = "st,stpmic1-regulators"; ++ buck1-supply = <&vin>; ++ buck2-supply = <&vin>; ++ buck3-supply = <&vin>; ++ buck4-supply = <&vin>; + ldo1-supply = <&v3v3>; ++ ldo2-supply = <&vin>; + ldo3-supply = <&vdd_ddr>; ++ ldo4-supply = <&vin>; ++ ldo5-supply = <&vin>; + ldo6-supply = <&v3v3>; ++ vref_ddr-supply = <&vin>; ++ boost-supply = <&vin>; + pwr_sw1-supply = <&bst_out>; + pwr_sw2-supply = <&bst_out>; + +@@ -283,7 +112,6 @@ + regulator-min-microvolt = <1800000>; + regulator-max-microvolt = <1800000>; + regulator-always-on; +- interrupts = ; + }; + + v3v3_hdmi: ldo2 { +@@ -291,29 +119,23 @@ + regulator-min-microvolt = <3300000>; + regulator-max-microvolt = <3300000>; + regulator-always-on; +- interrupts = ; + }; + + vtt_ddr: ldo3 { + regulator-name = "vtt_ddr"; +- regulator-min-microvolt = <500000>; +- regulator-max-microvolt = <750000>; + regulator-always-on; + regulator-over-current-protection; ++ st,regulator-sink-source; + }; + + vdd_usb: ldo4 { + regulator-name = "vdd_usb"; +- regulator-min-microvolt = <3300000>; +- regulator-max-microvolt = <3300000>; +- interrupts = ; + }; + + vdda: ldo5 { + regulator-name = "vdda"; + regulator-min-microvolt = <2900000>; + regulator-max-microvolt = <2900000>; +- interrupts = ; + regulator-boot-on; + }; + +@@ -322,7 +144,6 @@ + regulator-min-microvolt = <1200000>; + regulator-max-microvolt = <1200000>; + regulator-always-on; +- interrupts = ; + }; + + vref_ddr: vref_ddr { +@@ -331,96 +152,138 @@ + regulator-over-current-protection; + }; + +- bst_out: boost { ++ bst_out: boost { + regulator-name = "bst_out"; +- interrupts = ; +- }; ++ }; + + vbus_otg: pwr_sw1 { + regulator-name = "vbus_otg"; +- interrupts = ; +- }; ++ }; + +- vbus_sw: pwr_sw2 { ++ vbus_sw: pwr_sw2 { + regulator-name = "vbus_sw"; +- interrupts = ; + regulator-active-discharge = <1>; +- }; +- }; +- +- onkey { +- compatible = "st,stpmic1-onkey"; +- interrupts = , ; +- interrupt-names = "onkey-falling", "onkey-rising"; +- power-off-time-sec = <10>; +- status = "okay"; +- }; +- +- watchdog { +- compatible = "st,stpmic1-wdt"; +- status = "disabled"; +- }; +- }; +-}; +- +-&i2s2 { +- clocks = <&rcc SPI2>, <&rcc SPI2_K>, <&rcc PLL3_Q>, <&rcc PLL3_R>; +- clock-names = "pclk", "i2sclk", "x8k", "x11k"; +- pinctrl-names = "default", "sleep"; +- pinctrl-0 = <&i2s2_pins_a>; +- pinctrl-1 = <&i2s2_pins_sleep_a>; +- status = "okay"; +- +- i2s2_port: port { +- i2s2_endpoint: endpoint { +- remote-endpoint = <&sii9022_tx_endpoint>; +- format = "i2s"; +- mclk-fs = <256>; ++ }; + }; + }; + }; + +-&ipcc { +- status = "okay"; +-}; +- + &iwdg2 { + timeout-sec = <32>; ++ secure-timeout-sec = <15>; + status = "okay"; + }; + +-<dc { +- pinctrl-names = "default", "sleep"; +- pinctrl-0 = <<dc_pins_a>; +- pinctrl-1 = <<dc_pins_sleep_a>; +- status = "okay"; +- +- port { +- #address-cells = <1>; +- #size-cells = <0>; +- +- ltdc_ep0_out: endpoint@0 { +- reg = <0>; +- remote-endpoint = <&sii9022_in>; +- }; ++&osc_calibration { ++ csi-calibration { ++ status = "okay"; + }; +-}; + +-&m4_rproc { +- memory-region = <&retram>, <&mcuram>, <&mcuram2>, <&vdev0vring0>, +- <&vdev0vring1>, <&vdev0buffer>; +- mboxes = <&ipcc 0>, <&ipcc 1>, <&ipcc 2>; +- mbox-names = "vq0", "vq1", "shutdown"; +- interrupt-parent = <&exti>; +- interrupts = <68 1>; +- status = "okay"; ++ hsi-calibration { ++ status = "okay"; ++ }; + }; + + &pwr_regulators { ++ system_suspend_supported_soc_modes = < ++ STM32_PM_CSLEEP_RUN ++ STM32_PM_CSTOP_ALLOW_LP_STOP ++ STM32_PM_CSTOP_ALLOW_STANDBY_DDR_SR ++ >; ++ system_off_soc_mode = ; + vdd-supply = <&vdd>; + vdd_3v3_usbfs-supply = <&vdd_usb>; + }; + ++&rcc { ++ st,clksrc = < ++ CLK_MPU_PLL1P ++ CLK_AXI_PLL2P ++ CLK_MCU_PLL3P ++ CLK_PLL12_HSE ++ CLK_PLL3_HSE ++ CLK_PLL4_HSE ++ CLK_RTC_LSE ++ CLK_MCO1_DISABLED ++ CLK_MCO2_DISABLED ++ >; ++ ++ st,clkdiv = < ++ 1 /*MPU*/ ++ 0 /*AXI*/ ++ 0 /*MCU*/ ++ 1 /*APB1*/ ++ 1 /*APB2*/ ++ 1 /*APB3*/ ++ 1 /*APB4*/ ++ 2 /*APB5*/ ++ 23 /*RTC*/ ++ 0 /*MCO1*/ ++ 0 /*MCO2*/ ++ >; ++ ++ st,pkcs = < ++ CLK_CKPER_HSE ++ CLK_FMC_ACLK ++ CLK_QSPI_ACLK ++ CLK_ETH_PLL4P ++ CLK_SDMMC12_PLL4P ++ CLK_DSI_DSIPLL ++ CLK_STGEN_HSE ++ CLK_USBPHY_HSE ++ CLK_SPI2S1_PLL3Q ++ CLK_SPI2S23_PLL3Q ++ CLK_SPI45_HSI ++ CLK_SPI6_HSI ++ CLK_I2C46_HSI ++ CLK_SDMMC3_PLL4P ++ CLK_USBO_USBPHY ++ CLK_ADC_CKPER ++ CLK_CEC_LSE ++ CLK_I2C12_HSI ++ CLK_I2C35_HSI ++ CLK_UART1_HSI ++ CLK_UART24_HSI ++ CLK_UART35_HSI ++ CLK_UART6_HSI ++ CLK_UART78_HSI ++ CLK_SPDIF_PLL4P ++ CLK_FDCAN_PLL4R ++ CLK_SAI1_PLL3Q ++ CLK_SAI2_PLL3Q ++ CLK_SAI3_PLL3Q ++ CLK_SAI4_PLL3Q ++ CLK_RNG1_CSI ++ CLK_RNG2_LSI ++ CLK_LPTIM1_PCLK1 ++ CLK_LPTIM23_PCLK3 ++ CLK_LPTIM45_LSE ++ >; ++ ++ /* VCO = 1066.0 MHz => P = 266 (AXI), Q = 533 (GPU), R = 533 (DDR) */ ++ pll2: st,pll@1 { ++ compatible = "st,stm32mp1-pll"; ++ reg = <1>; ++ cfg = <2 65 1 0 0 PQR(1,1,1)>; ++ frac = <0x1400>; ++ }; ++ ++ /* VCO = 417.8 MHz => P = 209, Q = 24, R = 11 */ ++ pll3: st,pll@2 { ++ compatible = "st,stm32mp1-pll"; ++ reg = <2>; ++ cfg = <1 33 1 16 36 PQR(1,1,1)>; ++ frac = <0x1a04>; ++ }; ++ ++ /* VCO = 594.0 MHz => P = 99, Q = 74, R = 74 */ ++ pll4: st,pll@3 { ++ compatible = "st,stm32mp1-pll"; ++ reg = <3>; ++ cfg = <3 98 5 7 7 PQR(1,1,1)>; ++ }; ++}; ++ + &rng1 { + status = "okay"; + }; +@@ -429,197 +292,154 @@ + status = "okay"; + }; + +-&sai2 { +- clocks = <&rcc SAI2>, <&rcc PLL3_Q>, <&rcc PLL3_R>; +- clock-names = "pclk", "x8k", "x11k"; +- pinctrl-names = "default", "sleep"; +- pinctrl-0 = <&sai2a_pins_a>, <&sai2b_pins_b>; +- pinctrl-1 = <&sai2a_sleep_pins_a>, <&sai2b_sleep_pins_b>; ++&timers15 { + status = "okay"; + +- sai2a: audio-controller@4400b004 { +- #clock-cells = <0>; +- dma-names = "tx"; +- clocks = <&rcc SAI2_K>; +- clock-names = "sai_ck"; ++ counter { + status = "okay"; +- +- sai2a_port: port { +- sai2a_endpoint: endpoint { +- remote-endpoint = <&cs42l51_tx_endpoint>; +- format = "i2s"; +- mclk-fs = <256>; +- dai-tdm-slot-num = <2>; +- dai-tdm-slot-width = <32>; +- }; +- }; +- }; +- +- sai2b: audio-controller@4400b024 { +- dma-names = "rx"; +- st,sync = <&sai2a 2>; +- clocks = <&rcc SAI2_K>, <&sai2a>; +- clock-names = "sai_ck", "MCLK"; +- status = "okay"; +- +- sai2b_port: port { +- sai2b_endpoint: endpoint { +- remote-endpoint = <&cs42l51_rx_endpoint>; +- format = "i2s"; +- mclk-fs = <256>; +- dai-tdm-slot-num = <2>; +- dai-tdm-slot-width = <32>; +- }; +- }; + }; + }; + +-&sdmmc1 { +- pinctrl-names = "default", "opendrain", "sleep"; +- pinctrl-0 = <&sdmmc1_b4_pins_a>; +- pinctrl-1 = <&sdmmc1_b4_od_pins_a>; +- pinctrl-2 = <&sdmmc1_b4_sleep_pins_a>; +- broken-cd; +- st,neg-edge; +- bus-width = <4>; +- vmmc-supply = <&v3v3>; ++&uart4 { ++ pinctrl-names = "default"; ++ pinctrl-0 = <&uart4_pins_a>; + status = "okay"; + }; + +-&sdmmc3 { +- pinctrl-names = "default", "opendrain", "sleep"; +- pinctrl-0 = <&sdmmc3_b4_pins_a>; +- pinctrl-1 = <&sdmmc3_b4_od_pins_a>; +- pinctrl-2 = <&sdmmc3_b4_sleep_pins_a>; +- broken-cd; +- st,neg-edge; +- bus-width = <4>; +- vmmc-supply = <&v3v3>; ++&uart7 { ++ pinctrl-names = "default"; ++ pinctrl-0 = <&uart7_pins_c>; + status = "disabled"; + }; + +-&timers1 { +- /* spare dmas for other usage */ +- /delete-property/dmas; +- /delete-property/dma-names; ++&usart3 { ++ pinctrl-names = "default"; ++ pinctrl-0 = <&usart3_pins_c>; ++ uart-has-rtscts; + status = "disabled"; +- pwm { +- pinctrl-0 = <&pwm1_pins_a>; +- pinctrl-1 = <&pwm1_sleep_pins_a>; +- pinctrl-names = "default", "sleep"; +- status = "okay"; +- }; +- timer@0 { +- status = "okay"; +- }; + }; + +-&timers3 { +- /delete-property/dmas; +- /delete-property/dma-names; +- status = "disabled"; +- pwm { +- pinctrl-0 = <&pwm3_pins_a>; +- pinctrl-1 = <&pwm3_sleep_pins_a>; +- pinctrl-names = "default", "sleep"; +- status = "okay"; ++/* Low-power states of regulators */ ++&v1v2_hdmi { ++ standby-ddr-sr { ++ regulator-off-in-suspend; + }; +- timer@2 { +- status = "okay"; ++ standby-ddr-off { ++ regulator-off-in-suspend; + }; + }; + +-&timers4 { +- /delete-property/dmas; +- /delete-property/dma-names; +- status = "disabled"; +- pwm { +- pinctrl-0 = <&pwm4_pins_a &pwm4_pins_b>; +- pinctrl-1 = <&pwm4_sleep_pins_a &pwm4_sleep_pins_b>; +- pinctrl-names = "default", "sleep"; +- status = "okay"; ++&v1v8_audio { ++ standby-ddr-sr { ++ regulator-off-in-suspend; + }; +- timer@3 { +- status = "okay"; ++ standby-ddr-off { ++ regulator-off-in-suspend; + }; + }; + +-&timers5 { +- /delete-property/dmas; +- /delete-property/dma-names; +- status = "disabled"; +- pwm { +- pinctrl-0 = <&pwm5_pins_a>; +- pinctrl-1 = <&pwm5_sleep_pins_a>; +- pinctrl-names = "default", "sleep"; +- status = "okay"; ++&v3v3 { ++ lp-stop { ++ regulator-suspend-microvolt = <3300000>; ++ regulator-on-in-suspend; + }; +- timer@4 { +- status = "okay"; ++ standby-ddr-sr { ++ regulator-off-in-suspend; + }; +-}; +- +-&timers6 { +- /delete-property/dmas; +- /delete-property/dma-names; +- status = "disabled"; +- timer@5 { +- status = "okay"; ++ standby-ddr-off { ++ regulator-off-in-suspend; + }; + }; + +-&timers12 { +- /delete-property/dmas; +- /delete-property/dma-names; +- status = "disabled"; +- pwm { +- pinctrl-0 = <&pwm12_pins_a>; +- pinctrl-1 = <&pwm12_sleep_pins_a>; +- pinctrl-names = "default", "sleep"; +- status = "okay"; ++&v3v3_hdmi { ++ standby-ddr-sr { ++ regulator-off-in-suspend; + }; +- timer@11 { +- status = "okay"; ++ standby-ddr-off { ++ regulator-off-in-suspend; + }; + }; + +-&uart4 { +- pinctrl-names = "default"; +- pinctrl-0 = <&uart4_pins_a>; +- status = "okay"; ++&vdd { ++ lp-stop { ++ regulator-suspend-microvolt = <3300000>; ++ regulator-on-in-suspend; ++ }; ++ standby-ddr-sr { ++ regulator-suspend-microvolt = <3300000>; ++ regulator-on-in-suspend; ++ }; ++ standby-ddr-off { ++ regulator-suspend-microvolt = <3300000>; ++ regulator-on-in-suspend; ++ }; + }; + +-&usbh_ehci { +- phys = <&usbphyc_port0>; +- status = "okay"; ++&vdda { ++ standby-ddr-sr { ++ regulator-off-in-suspend; ++ }; ++ standby-ddr-off { ++ regulator-off-in-suspend; ++ }; + }; + +-&usbotg_hs { +- dr_mode = "peripheral"; +- phys = <&usbphyc_port1 0>; +- phy-names = "usb2-phy"; +- status = "okay"; ++&vddcore { ++ lp-stop { ++ regulator-on-in-suspend; ++ regulator-suspend-microvolt = <1200000>; ++ }; ++ standby-ddr-sr { ++ regulator-off-in-suspend; ++ }; ++ standby-ddr-off { ++ regulator-off-in-suspend; ++ }; + }; + +-&usbphyc { +- status = "okay"; ++&vdd_ddr { ++ lp-stop { ++ regulator-suspend-microvolt = <1350000>; ++ regulator-on-in-suspend; ++ }; ++ standby-ddr-sr { ++ regulator-suspend-microvolt = <1350000>; ++ regulator-on-in-suspend; ++ }; ++ standby-ddr-off { ++ regulator-off-in-suspend; ++ }; + }; + +-&usbphyc_port0 { +- phy-supply = <&vdd_usb>; +- vdda1v1-supply = <®11>; +- vdda1v8-supply = <®18>; ++&vdd_usb { ++ standby-ddr-sr { ++ regulator-off-in-suspend; ++ }; ++ standby-ddr-off { ++ regulator-off-in-suspend; ++ }; + }; + +-&usbphyc_port1 { +- phy-supply = <&vdd_usb>; +- vdda1v1-supply = <®11>; +- vdda1v8-supply = <®18>; ++&vref_ddr { ++ lp-stop { ++ regulator-on-in-suspend; ++ }; ++ standby-ddr-sr { ++ regulator-on-in-suspend; ++ }; ++ standby-ddr-off { ++ regulator-off-in-suspend; ++ }; + }; + +-&vrefbuf { +- regulator-min-microvolt = <2500000>; +- regulator-max-microvolt = <2500000>; +- vdda-supply = <&vdd>; +- status = "okay"; ++&vtt_ddr { ++ lp-stop { ++ regulator-off-in-suspend; ++ }; ++ standby-ddr-sr { ++ regulator-off-in-suspend; ++ }; ++ standby-ddr-off { ++ regulator-off-in-suspend; ++ }; + }; +diff --git a/core/arch/arm/dts/stm32mp15xx-edx.dtsi b/core/arch/arm/dts/stm32mp15xx-edx.dtsi +new file mode 100644 +index 000000000..749851d5f +--- /dev/null ++++ b/core/arch/arm/dts/stm32mp15xx-edx.dtsi +@@ -0,0 +1,448 @@ ++// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) ++/* ++ * Copyright (C) STMicroelectronics 2017 - All Rights Reserved ++ * Author: Ludovic Barre for STMicroelectronics. ++ */ ++ ++#include ++#include ++ ++/ { ++ memory@c0000000 { ++ device_type = "memory"; ++ reg = <0xC0000000 0x40000000>; ++ }; ++ ++ aliases { ++ serial0 = &uart4; ++ }; ++ ++ vin: vin { ++ compatible = "regulator-fixed"; ++ regulator-name = "vin"; ++ regulator-min-microvolt = <5000000>; ++ regulator-max-microvolt = <5000000>; ++ regulator-always-on; ++ }; ++}; ++ ++&bsec { ++ board_id: board_id@ec { ++ reg = <0xec 0x4>; ++ st,non-secure-otp; ++ }; ++}; ++ ++&clk_hse { ++ st,digbypass; ++}; ++ ++&cpu0{ ++ cpu-supply = <&vddcore>; ++}; ++ ++&i2c4 { ++ compatible = "st,stm32mp15-i2c-non-secure"; ++ pinctrl-names = "default"; ++ pinctrl-0 = <&i2c4_pins_a>; ++ i2c-scl-rising-time-ns = <185>; ++ i2c-scl-falling-time-ns = <20>; ++ clock-frequency = <400000>; ++ status = "okay"; ++ ++ pmic: stpmic@33 { ++ compatible = "st,stpmic1"; ++ reg = <0x33>; ++ interrupt-controller; ++ #interrupt-cells = <2>; ++ status = "okay"; ++ ++ regulators { ++ compatible = "st,stpmic1-regulators"; ++ buck1-supply = <&vin>; ++ buck2-supply = <&vin>; ++ buck3-supply = <&vin>; ++ buck4-supply = <&vin>; ++ ldo1-supply = <&v3v3>; ++ ldo2-supply = <&v3v3>; ++ ldo3-supply = <&vdd_ddr>; ++ ldo4-supply = <&vin>; ++ ldo5-supply = <&v3v3>; ++ ldo6-supply = <&v3v3>; ++ vref_ddr-supply = <&vin>; ++ boost-supply = <&vin>; ++ pwr_sw1-supply = <&bst_out>; ++ pwr_sw2-supply = <&bst_out>; ++ ++ vddcore: buck1 { ++ regulator-name = "vddcore"; ++ regulator-min-microvolt = <1200000>; ++ regulator-max-microvolt = <1350000>; ++ regulator-always-on; ++ regulator-initial-mode = <0>; ++ regulator-over-current-protection; ++ }; ++ ++ vdd_ddr: buck2 { ++ regulator-name = "vdd_ddr"; ++ regulator-min-microvolt = <1350000>; ++ regulator-max-microvolt = <1350000>; ++ regulator-always-on; ++ regulator-initial-mode = <0>; ++ regulator-over-current-protection; ++ }; ++ ++ vdd: buck3 { ++ regulator-name = "vdd"; ++ regulator-min-microvolt = <3300000>; ++ regulator-max-microvolt = <3300000>; ++ regulator-always-on; ++ st,mask-reset; ++ regulator-initial-mode = <0>; ++ regulator-over-current-protection; ++ }; ++ ++ v3v3: buck4 { ++ regulator-name = "v3v3"; ++ regulator-min-microvolt = <3300000>; ++ regulator-max-microvolt = <3300000>; ++ regulator-always-on; ++ regulator-over-current-protection; ++ regulator-initial-mode = <0>; ++ }; ++ ++ vdda: ldo1 { ++ regulator-name = "vdda"; ++ regulator-min-microvolt = <2900000>; ++ regulator-max-microvolt = <2900000>; ++ }; ++ ++ v2v8: ldo2 { ++ regulator-name = "v2v8"; ++ regulator-min-microvolt = <2800000>; ++ regulator-max-microvolt = <2800000>; ++ }; ++ ++ vtt_ddr: ldo3 { ++ regulator-name = "vtt_ddr"; ++ regulator-always-on; ++ regulator-over-current-protection; ++ st,regulator-sink-source; ++ }; ++ ++ vdd_usb: ldo4 { ++ regulator-name = "vdd_usb"; ++ }; ++ ++ vdd_sd: ldo5 { ++ regulator-name = "vdd_sd"; ++ regulator-min-microvolt = <2900000>; ++ regulator-max-microvolt = <2900000>; ++ regulator-boot-on; ++ }; ++ ++ v1v8: ldo6 { ++ regulator-name = "v1v8"; ++ regulator-min-microvolt = <1800000>; ++ regulator-max-microvolt = <1800000>; ++ }; ++ ++ vref_ddr: vref_ddr { ++ regulator-name = "vref_ddr"; ++ regulator-always-on; ++ regulator-over-current-protection; ++ }; ++ ++ bst_out: boost { ++ regulator-name = "bst_out"; ++ }; ++ ++ vbus_otg: pwr_sw1 { ++ regulator-name = "vbus_otg"; ++ }; ++ ++ vbus_sw: pwr_sw2 { ++ regulator-name = "vbus_sw"; ++ regulator-active-discharge = <1>; ++ }; ++ }; ++ }; ++}; ++ ++&iwdg2 { ++ timeout-sec = <32>; ++ secure-timeout-sec = <5>; ++ status = "okay"; ++}; ++ ++&osc_calibration { ++ csi-calibration { ++ status = "okay"; ++ }; ++ ++ hsi-calibration { ++ status = "okay"; ++ }; ++}; ++ ++&pwr_regulators { ++ system_suspend_supported_soc_modes = < ++ STM32_PM_CSLEEP_RUN ++ STM32_PM_CSTOP_ALLOW_LP_STOP ++ STM32_PM_CSTOP_ALLOW_LPLV_STOP ++ STM32_PM_CSTOP_ALLOW_STANDBY_DDR_SR ++ >; ++ system_off_soc_mode = ; ++ vdd-supply = <&vdd>; ++ vdd_3v3_usbfs-supply = <&vdd_usb>; ++}; ++ ++&rcc { ++ st,clksrc = < ++ CLK_MPU_PLL1P ++ CLK_AXI_PLL2P ++ CLK_MCU_PLL3P ++ CLK_PLL12_HSE ++ CLK_PLL3_HSE ++ CLK_PLL4_HSE ++ CLK_RTC_LSE ++ CLK_MCO1_DISABLED ++ CLK_MCO2_DISABLED ++ >; ++ ++ st,clkdiv = < ++ 1 /*MPU*/ ++ 0 /*AXI*/ ++ 0 /*MCU*/ ++ 1 /*APB1*/ ++ 1 /*APB2*/ ++ 1 /*APB3*/ ++ 1 /*APB4*/ ++ 2 /*APB5*/ ++ 23 /*RTC*/ ++ 0 /*MCO1*/ ++ 0 /*MCO2*/ ++ >; ++ ++ st,pkcs = < ++ CLK_CKPER_HSE ++ CLK_FMC_ACLK ++ CLK_QSPI_ACLK ++ CLK_ETH_PLL4P ++ CLK_SDMMC12_PLL4P ++ CLK_DSI_DSIPLL ++ CLK_STGEN_HSE ++ CLK_USBPHY_HSE ++ CLK_SPI2S1_PLL3Q ++ CLK_SPI2S23_PLL3Q ++ CLK_SPI45_HSI ++ CLK_SPI6_HSI ++ CLK_I2C46_HSI ++ CLK_SDMMC3_PLL4P ++ CLK_USBO_USBPHY ++ CLK_ADC_CKPER ++ CLK_CEC_LSE ++ CLK_I2C12_HSI ++ CLK_I2C35_HSI ++ CLK_UART1_HSI ++ CLK_UART24_HSI ++ CLK_UART35_HSI ++ CLK_UART6_HSI ++ CLK_UART78_HSI ++ CLK_SPDIF_PLL4P ++ CLK_FDCAN_PLL4R ++ CLK_SAI1_PLL3Q ++ CLK_SAI2_PLL3Q ++ CLK_SAI3_PLL3Q ++ CLK_SAI4_PLL3Q ++ CLK_RNG1_CSI ++ CLK_RNG2_LSI ++ CLK_LPTIM1_PCLK1 ++ CLK_LPTIM23_PCLK3 ++ CLK_LPTIM45_LSE ++ >; ++ ++ /* VCO = 1066.0 MHz => P = 266 (AXI), Q = 533 (GPU), R = 533 (DDR) */ ++ pll2: st,pll@1 { ++ compatible = "st,stm32mp1-pll"; ++ reg = <1>; ++ cfg = <2 65 1 0 0 PQR(1,1,1)>; ++ frac = <0x1400>; ++ }; ++ ++ /* VCO = 417.8 MHz => P = 209, Q = 24, R = 11 */ ++ pll3: st,pll@2 { ++ compatible = "st,stm32mp1-pll"; ++ reg = <2>; ++ cfg = <1 33 1 16 36 PQR(1,1,1)>; ++ frac = <0x1a04>; ++ }; ++ ++ /* VCO = 594.0 MHz => P = 99, Q = 74, R = 74 */ ++ pll4: st,pll@3 { ++ compatible = "st,stm32mp1-pll"; ++ reg = <3>; ++ cfg = <3 98 5 7 7 PQR(1,1,1)>; ++ }; ++}; ++ ++&rng1 { ++ status = "okay"; ++}; ++ ++&rtc { ++ status = "okay"; ++}; ++ ++&timers15 { ++ status = "okay"; ++ ++ counter { ++ status = "okay"; ++ }; ++}; ++ ++&uart4 { ++ pinctrl-names = "default"; ++ pinctrl-0 = <&uart4_pins_a>; ++ status = "okay"; ++}; ++ ++/* Low-power states of regulators */ ++&v1v8 { ++ standby-ddr-sr { ++ regulator-off-in-suspend; ++ }; ++ standby-ddr-off { ++ regulator-off-in-suspend; ++ }; ++}; ++ ++&v2v8 { ++ standby-ddr-sr { ++ regulator-off-in-suspend; ++ }; ++ standby-ddr-off { ++ regulator-off-in-suspend; ++ }; ++}; ++ ++&v3v3 { ++ standby-ddr-sr { ++ regulator-off-in-suspend; ++ }; ++ standby-ddr-off { ++ regulator-off-in-suspend; ++ }; ++}; ++ ++&vdd { ++ lp-stop { ++ regulator-suspend-microvolt = <3300000>; ++ regulator-on-in-suspend; ++ }; ++ lplv-stop { ++ regulator-suspend-microvolt = <3300000>; ++ regulator-on-in-suspend; ++ }; ++ standby-ddr-sr { ++ regulator-suspend-microvolt = <3300000>; ++ regulator-on-in-suspend; ++ }; ++ standby-ddr-off { ++ regulator-suspend-microvolt = <3300000>; ++ regulator-on-in-suspend; ++ }; ++}; ++ ++&vdda { ++ standby-ddr-sr { ++ regulator-off-in-suspend; ++ }; ++ standby-ddr-off { ++ regulator-off-in-suspend; ++ }; ++}; ++ ++&vddcore { ++ lp-stop { ++ regulator-on-in-suspend; ++ regulator-suspend-microvolt = <1200000>; ++ }; ++ lplv-stop { ++ regulator-on-in-suspend; ++ regulator-suspend-microvolt = <900000>; ++ }; ++ standby-ddr-sr { ++ regulator-off-in-suspend; ++ }; ++ standby-ddr-off { ++ regulator-off-in-suspend; ++ }; ++}; ++ ++&vdd_ddr { ++ lp-stop { ++ regulator-suspend-microvolt = <1350000>; ++ regulator-on-in-suspend; ++ }; ++ lplv-stop { ++ regulator-suspend-microvolt = <1350000>; ++ regulator-on-in-suspend; ++ }; ++ standby-ddr-sr { ++ regulator-suspend-microvolt = <1350000>; ++ regulator-on-in-suspend; ++ }; ++ standby-ddr-off { ++ regulator-off-in-suspend; ++ }; ++}; ++ ++&vdd_sd { ++ standby-ddr-sr { ++ regulator-off-in-suspend; ++ }; ++ standby-ddr-off { ++ regulator-off-in-suspend; ++ }; ++}; ++ ++&vdd_usb { ++ standby-ddr-sr { ++ regulator-off-in-suspend; ++ }; ++ standby-ddr-off { ++ regulator-off-in-suspend; ++ }; ++}; ++ ++&vref_ddr { ++ lp-stop { ++ regulator-on-in-suspend; ++ }; ++ lplv-stop { ++ regulator-on-in-suspend; ++ }; ++ standby-ddr-sr { ++ regulator-on-in-suspend; ++ }; ++ standby-ddr-off { ++ regulator-off-in-suspend; ++ }; ++}; ++ ++&vtt_ddr { ++ lp-stop { ++ regulator-off-in-suspend; ++ }; ++ lplv-stop { ++ regulator-off-in-suspend; ++ }; ++ standby-ddr-sr { ++ regulator-off-in-suspend; ++ }; ++ standby-ddr-off { ++ regulator-off-in-suspend; ++ }; ++}; +diff --git a/core/arch/arm/dts/stm32mp15xx-evx.dtsi b/core/arch/arm/dts/stm32mp15xx-evx.dtsi +new file mode 100644 +index 000000000..804fb2631 +--- /dev/null ++++ b/core/arch/arm/dts/stm32mp15xx-evx.dtsi +@@ -0,0 +1,23 @@ ++// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) ++/* ++ * Copyright (C) STMicroelectronics 2017 - All Rights Reserved ++ * Author: Ludovic Barre for STMicroelectronics. ++ */ ++ ++ ++&i2c4 { ++ pmic: stpmic@33 { ++ regulators { ++ v1v8: ldo6 { ++ regulator-enable-ramp-delay = <300000>; ++ }; ++ }; ++ }; ++}; ++ ++&usart3 { ++ pinctrl-names = "default"; ++ pinctrl-0 = <&usart3_pins_b>; ++ uart-has-rtscts; ++ status = "disabled"; ++}; +diff --git a/core/arch/arm/include/arm32.h b/core/arch/arm/include/arm32.h +index 9ed132fb0..6ea076edf 100644 +--- a/core/arch/arm/include/arm32.h ++++ b/core/arch/arm/include/arm32.h +@@ -163,6 +163,15 @@ + #define IDPFR1_GENTIMER_SHIFT U(16) + #define IDPFR1_GENTIMER_MASK SHIFT_U32(0xF, IDPFR1_GENTIMER_SHIFT) + ++/* Generic timer registers and fields */ ++#define CNTCR_OFFSET 0x000 ++#define CNTSR_OFFSET 0x004 ++#define CNTCVL_OFFSET 0x008 ++#define CNTCVU_OFFSET 0x00C ++#define CNTFID_OFFSET 0x020 ++ ++#define CNTCR_EN BIT(0) ++ + #ifndef __ASSEMBLER__ + #include + #ifdef CFG_ARM_GICV3 +diff --git a/core/arch/arm/include/kernel/thread.h b/core/arch/arm/include/kernel/thread.h +index d2b0d006c..a7cbb87e2 100644 +--- a/core/arch/arm/include/kernel/thread.h ++++ b/core/arch/arm/include/kernel/thread.h +@@ -2,7 +2,7 @@ + /* + * Copyright (c) 2014, STMicroelectronics International N.V. + * Copyright (c) 2016-2017, Linaro Limited +- * Copyright (c) 2020-2021, Arm Limited ++ * Copyright (c) 2020, Arm Limited + */ + + #ifndef KERNEL_THREAD_H +@@ -20,7 +20,7 @@ + #define THREAD_ID_0 0 + #define THREAD_ID_INVALID -1 + +-#define THREAD_RPC_MAX_NUM_PARAMS U(4) ++#define THREAD_RPC_MAX_NUM_PARAMS U(6) + + #ifndef __ASSEMBLER__ + +diff --git a/core/arch/arm/include/mm/core_mmu.h b/core/arch/arm/include/mm/core_mmu.h +index c4c34b955..3521f0fa8 100644 +--- a/core/arch/arm/include/mm/core_mmu.h ++++ b/core/arch/arm/include/mm/core_mmu.h +@@ -116,6 +116,7 @@ + * MEM_AREA_NSEC_SHM: NonSecure shared RAM between NSec and TEE. + * MEM_AREA_RAM_NSEC: NonSecure RAM storing data + * MEM_AREA_RAM_SEC: Secure RAM storing some secrets ++ * MEM_AREA_ROM_SEC: Secure read only memory storing some secrets + * MEM_AREA_IO_NSEC: NonSecure HW mapped registers + * MEM_AREA_IO_SEC: Secure HW mapped registers + * MEM_AREA_EXT_DT: Memory loads external device tree +@@ -143,6 +144,7 @@ enum teecore_memtypes { + MEM_AREA_NSEC_SHM, + MEM_AREA_RAM_NSEC, + MEM_AREA_RAM_SEC, ++ MEM_AREA_ROM_SEC, + MEM_AREA_IO_NSEC, + MEM_AREA_IO_SEC, + MEM_AREA_EXT_DT, +@@ -175,6 +177,7 @@ static inline const char *teecore_memtype_name(enum teecore_memtypes type) + [MEM_AREA_NSEC_SHM] = "NSEC_SHM", + [MEM_AREA_RAM_NSEC] = "RAM_NSEC", + [MEM_AREA_RAM_SEC] = "RAM_SEC", ++ [MEM_AREA_ROM_SEC] = "ROM_SEC", + [MEM_AREA_IO_NSEC] = "IO_NSEC", + [MEM_AREA_IO_SEC] = "IO_SEC", + [MEM_AREA_EXT_DT] = "EXT_DT", +diff --git a/core/arch/arm/include/sm/optee_smc.h b/core/arch/arm/include/sm/optee_smc.h +index bf64713a8..c2f4d0556 100644 +--- a/core/arch/arm/include/sm/optee_smc.h ++++ b/core/arch/arm/include/sm/optee_smc.h +@@ -6,6 +6,7 @@ + #define OPTEE_SMC_H + + #include ++#include + + /* + * This file is exported by OP-TEE and is in kept in sync between secure +@@ -290,6 +291,8 @@ + #define OPTEE_SMC_SEC_CAP_MEMREF_NULL BIT(4) + /* Secure world supports asynchronous notification of normal world */ + #define OPTEE_SMC_SEC_CAP_ASYNC_NOTIF BIT(5) ++/* Secure world is built with OCALL support */ ++#define OPTEE_SMC_SEC_CAP_OCALL BIT(31) + + #define OPTEE_SMC_FUNCID_EXCHANGE_CAPABILITIES U(9) + #define OPTEE_SMC_EXCHANGE_CAPABILITIES \ +diff --git a/core/arch/arm/include/sm/pm.h b/core/arch/arm/include/sm/pm.h +index 939f966e8..90f031a2b 100644 +--- a/core/arch/arm/include/sm/pm.h ++++ b/core/arch/arm/include/sm/pm.h +@@ -34,7 +34,11 @@ + struct sm_pm_ctx { + uint32_t sp; + paddr_t cpu_resume_addr; ++#ifdef CFG_WITH_LPAE ++ uint32_t suspend_regs[18]; ++#else + uint32_t suspend_regs[16]; ++#endif + }; + + /* suspend/resume core functions */ +diff --git a/core/arch/arm/kernel/boot.c b/core/arch/arm/kernel/boot.c +index 136a7edec..7d42c5375 100644 +--- a/core/arch/arm/kernel/boot.c ++++ b/core/arch/arm/kernel/boot.c +@@ -708,7 +708,7 @@ static int add_optee_dt_node(struct dt_descriptor *dt) + + if (fdt_path_offset(dt->blob, "/firmware/optee") >= 0) { + DMSG("OP-TEE Device Tree node already exists!"); +- return 0; ++ return 1; + } + + offs = fdt_path_offset(dt->blob, "/firmware"); +@@ -759,6 +759,11 @@ static int add_optee_dt_node(struct dt_descriptor *dt) + if (ret < 0) + return -1; + } ++ ++ ret = fdt_setprop(dt->blob, offs, "u-boot,dm-pre-reloc", ++ NULL, 0); ++ if (ret < 0) ++ return -1; + return 0; + } + +@@ -1116,12 +1121,16 @@ static int mark_tzdram_as_reserved(struct dt_descriptor *dt) + static void update_external_dt(void) + { + struct dt_descriptor *dt = &external_dt; ++ int node_already_exists = 0; + + if (!dt->blob) + return; + +- if (!IS_ENABLED(CFG_CORE_FFA) && add_optee_dt_node(dt)) ++ node_already_exists = add_optee_dt_node(dt); ++ if (!IS_ENABLED(CFG_CORE_FFA) && node_already_exists < 0) + panic("Failed to add OP-TEE Device Tree node"); ++ else if (node_already_exists == 1) ++ return; + + if (config_psci(dt)) + panic("Failed to config PSCI"); +diff --git a/core/arch/arm/kernel/trace_ext.c b/core/arch/arm/kernel/trace_ext.c +index 6ed3bf94c..ffa63251c 100644 +--- a/core/arch/arm/kernel/trace_ext.c ++++ b/core/arch/arm/kernel/trace_ext.c +@@ -2,13 +2,15 @@ + /* + * Copyright (c) 2014, Linaro Limited + */ +-#include +-#include + #include + #include + #include + #include + #include ++#include ++#include ++#include ++#include + + const char trace_ext_prefix[] = "TC"; + int trace_level __nex_data = TRACE_LEVEL; +@@ -61,3 +63,50 @@ int trace_ext_get_core_id(void) + else + return -1; + } ++ ++int printf(const char *fmt, ...) ++{ ++ char to_format[MAX_PRINT_SIZE]; ++ va_list ap; ++ int s; ++ ++ if (trace_get_level() < TRACE_PRINTF_LEVEL) ++ return 0; ++ ++ va_start(ap, fmt); ++ s = vsnprintk(to_format, sizeof(to_format), fmt, ap); ++ va_end(ap); ++ ++ if (s < 0) ++ return s; ++ ++ trace_ext_puts(to_format); ++ ++ return s; ++} ++ ++ ++int putchar(int c) ++{ ++ char str[2] = { (char)c, '\0' }; ++ ++ if (trace_get_level() >= TRACE_PRINTF_LEVEL) ++ trace_ext_puts(str); ++ ++ /* ++ * From the putchar() man page: ++ * "fputc(), putc() and putchar() return the character written as an ++ * unsigned char cast to an int or EOF on error." ++ */ ++ return (int)(unsigned char)c; ++} ++ ++int puts(const char *str) ++{ ++ if (trace_get_level() >= TRACE_PRINTF_LEVEL) { ++ trace_ext_puts(str); ++ trace_ext_puts("\n"); ++ } ++ ++ return 1; ++} +diff --git a/core/arch/arm/mm/core_mmu.c b/core/arch/arm/mm/core_mmu.c +index 81a26f288..c62c8ebda 100644 +--- a/core/arch/arm/mm/core_mmu.c ++++ b/core/arch/arm/mm/core_mmu.c +@@ -8,6 +8,7 @@ + #include + #include + #include ++#include + #include + #include + #include +@@ -352,6 +353,20 @@ static void check_phys_mem_is_outside(struct core_mmu_phys_mem *start, + } + } + ++#ifdef CFG_WITH_TUI ++static void carve_out_tui_framebuffer(struct core_mmu_phys_mem **mem, ++ size_t *nelems) ++{ ++ paddr_t fb_pa = 0; ++ size_t fb_sz = 0; ++ ++ if (display_get_fb_addr_from_dtb(&fb_pa, &fb_sz) != TEE_SUCCESS) ++ panic(); ++ ++ carve_out_phys_mem(mem, nelems, fb_pa, fb_sz); ++} ++#endif ++ + static const struct core_mmu_phys_mem *discovered_nsec_ddr_start __nex_bss; + static size_t discovered_nsec_ddr_nelems __nex_bss; + +@@ -395,13 +410,16 @@ void core_mmu_set_discovered_nsec_ddr(struct core_mmu_phys_mem *start, + + carve_out_phys_mem(&m, &num_elems, TEE_RAM_START, TEE_RAM_PH_SIZE); + carve_out_phys_mem(&m, &num_elems, TA_RAM_START, TA_RAM_SIZE); +- ++#ifdef CFG_WITH_TUI ++ carve_out_tui_framebuffer(&m, &num_elems); ++#endif + for (map = static_memory_map; !core_mmap_is_end_of_table(map); map++) { + switch (map->type) { + case MEM_AREA_NSEC_SHM: + carve_out_phys_mem(&m, &num_elems, map->pa, map->size); + break; + case MEM_AREA_EXT_DT: ++ case MEM_AREA_RAM_NSEC: + case MEM_AREA_RES_VASPACE: + case MEM_AREA_SHM_VASPACE: + case MEM_AREA_TS_VASPACE: +@@ -504,7 +522,6 @@ static bool pbuf_is_sdp_mem(paddr_t pbuf __unused, size_t len __unused) + + /* Check special memories comply with registered memories */ + static void verify_special_mem_areas(struct tee_mmap_region *mem_map, +- size_t len, + const struct core_mmu_phys_mem *start, + const struct core_mmu_phys_mem *end, + const char *area_name __maybe_unused) +@@ -512,7 +529,6 @@ static void verify_special_mem_areas(struct tee_mmap_region *mem_map, + const struct core_mmu_phys_mem *mem; + const struct core_mmu_phys_mem *mem2; + struct tee_mmap_region *mmap; +- size_t n; + + if (start == end) { + DMSG("No %s memory area defined", area_name); +@@ -538,9 +554,18 @@ static void verify_special_mem_areas(struct tee_mmap_region *mem_map, + /* + * Check memories do not intersect any mapped memory. + * This is called before reserved VA space is loaded in mem_map. ++ * ++ * Exceptions are the memory areas that maps with the same attributes ++ * as for example MEM_AREA_RAM_NSEC and MEM_AREA_NSEC_SHM ++ * which may overlap since they are used for the same purpose ++ * except that MEM_AREA_NSEC_SHM is always mapped and ++ * MEM_AREA_RAM_NSEC only uses a dynamic mapping. + */ + for (mem = start; mem < end; mem++) { +- for (mmap = mem_map, n = 0; n < len; mmap++, n++) { ++ for (mmap = mem_map; mmap->type != MEM_AREA_END; mmap++) { ++ if (core_mmu_type_to_attr(mem->type) == ++ core_mmu_type_to_attr(mmap->type)) ++ continue; + if (core_is_buffer_intersect(mem->addr, mem->size, + mmap->pa, mmap->size)) { + MSG_MEM_INSTERSECT(mem->addr, mem->size, +@@ -667,6 +692,8 @@ uint32_t core_mmu_type_to_attr(enum teecore_memtypes t) + case MEM_AREA_RAM_SEC: + case MEM_AREA_SEC_RAM_OVERALL: + return attr | TEE_MATTR_SECURE | TEE_MATTR_PRW | cached; ++ case MEM_AREA_ROM_SEC: ++ return attr | TEE_MATTR_SECURE | TEE_MATTR_PR | cached; + case MEM_AREA_RES_VASPACE: + case MEM_AREA_SHM_VASPACE: + return 0; +@@ -861,8 +888,7 @@ static size_t collect_mem_ranges(struct tee_mmap_region *memory_map, + } + + if (IS_ENABLED(CFG_SECURE_DATA_PATH)) +- verify_special_mem_areas(memory_map, num_elems, +- phys_sdp_mem_begin, ++ verify_special_mem_areas(memory_map, phys_sdp_mem_begin, + phys_sdp_mem_end, "SDP"); + + add_va_space(memory_map, num_elems, MEM_AREA_RES_VASPACE, +@@ -1187,6 +1213,7 @@ static void check_mem_map(struct tee_mmap_region *map) + case MEM_AREA_IO_NSEC: + case MEM_AREA_EXT_DT: + case MEM_AREA_RAM_SEC: ++ case MEM_AREA_ROM_SEC: + case MEM_AREA_RAM_NSEC: + case MEM_AREA_RES_VASPACE: + case MEM_AREA_SHM_VASPACE: +diff --git a/core/arch/arm/plat-stm32mp1/boot_api.h b/core/arch/arm/plat-stm32mp1/boot_api.h +index 62e38b588..a7daffd44 100644 +--- a/core/arch/arm/plat-stm32mp1/boot_api.h ++++ b/core/arch/arm/plat-stm32mp1/boot_api.h +@@ -14,7 +14,9 @@ + #define BCKR_CORE1_MAGIC_NUMBER 4 + + /* Value for BCKR_CORE1_MAGIC_NUMBER entry */ ++#define BOOT_API_A7_CORE0_MAGIC_NUMBER 0xca7face0 + #define BOOT_API_A7_CORE1_MAGIC_NUMBER 0xca7face1 ++#define BOOT_API_A7_RESET_MAGIC_NUMBER 0xca7dead0 + + /* Backup register #5: physical address of core1 entry at boot up */ + #define BCKR_CORE1_BRANCH_ADDRESS 5 +diff --git a/core/arch/arm/plat-stm32mp1/bsec_pta.c b/core/arch/arm/plat-stm32mp1/bsec_pta.c +new file mode 100644 +index 000000000..262bca91c +--- /dev/null ++++ b/core/arch/arm/plat-stm32mp1/bsec_pta.c +@@ -0,0 +1,317 @@ ++// SPDX-License-Identifier: BSD-2-Clause ++/* ++ * Copyright (C) 2021, STMicroelectronics - All Rights Reserved ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#define TA_STM32MP_NVMEM_UUID { 0x1a8342cc, 0x81a5, 0x4512, \ ++ { 0x99, 0xfe, 0x9e, 0x2b, 0x3e, 0x37, 0xd6, 0x26 } } ++ ++#define PTA_NAME "bsec.pta" ++ ++static TEE_Result bsec_check_access(uint32_t otp_id) ++{ ++ struct ts_session *ts = ts_get_current_session(); ++ struct tee_ta_session *ta_session = to_ta_session(ts); ++ ++ /* REE kernel is allowed to access non secure OTP */ ++ if (ta_session->clnt_id.login == TEE_LOGIN_REE_KERNEL) { ++ if (!stm32_bsec_nsec_can_access_otp(otp_id)) ++ return TEE_ERROR_ACCESS_DENIED; ++ ++ return TEE_SUCCESS; ++ } ++ ++ if (stm32_bsec_can_access_otp(otp_id)) ++ return TEE_ERROR_ACCESS_DENIED; ++ ++ /* All TA have access to fuses */ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result bsec_read_mem(uint32_t pt, TEE_Param params[TEE_NUM_PARAMS]) ++{ ++ const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_MEMREF_OUTPUT, ++ TEE_PARAM_TYPE_NONE, ++ TEE_PARAM_TYPE_NONE); ++ ++ uint32_t *buf = (uint32_t *)params[1].memref.buffer; ++ uint32_t otp_start = 0; ++ size_t otp_length = 0; ++ uint32_t otp_id = 0; ++ TEE_Result res = TEE_ERROR_GENERIC; ++ size_t size = params[1].memref.size; ++ bool locked = false; ++ ++ if (pt != exp_pt || !buf || !size) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ /* check 32bits alignment */ ++ if (params[0].value.a % BSEC_BYTES_PER_WORD || ++ size % BSEC_BYTES_PER_WORD) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ otp_start = params[0].value.a / BSEC_BYTES_PER_WORD; ++ otp_length = size / BSEC_BYTES_PER_WORD; ++ ++ for (otp_id = otp_start; otp_id < otp_start + otp_length; ++ otp_id++, buf++) { ++ res = bsec_check_access(otp_id); ++ switch (params[0].value.b) { ++ case SHADOW_ACCESS: ++ if (res) { ++ /* Force 0 when access is not allowed */ ++ *buf = 0x0; ++ continue; ++ } ++ /* Read shadow register */ ++ res = stm32_bsec_read_otp(buf, otp_id); ++ FMSG("Read shadow %i val: %x", otp_id, *buf); ++ break; ++ ++ case FUSE_ACCESS: ++ /* check access */ ++ if (res) ++ goto out; ++ /* Read fuse value */ ++ res = stm32_bsec_shadow_read_otp(buf, otp_id); ++ FMSG("Read fuse %i val: %x", otp_id, *buf); ++ break; ++ ++ case LOCK_ACCESS: ++ if (res) { ++ /* Force error when access is not allowed */ ++ *buf = LOCK_ERROR; ++ continue; ++ } ++ *buf = 0; ++ /* Read lock value */ ++ res = stm32_bsec_read_permanent_lock(otp_id, &locked); ++ if (res) ++ goto out; ++ ++ if (locked) ++ *buf |= LOCK_PERM; ++ ++ res = stm32_bsec_read_sr_lock(otp_id, &locked); ++ if (res) ++ goto out; ++ ++ if (locked) ++ *buf |= LOCK_SHADOW_R; ++ ++ res = stm32_bsec_read_sw_lock(otp_id, &locked); ++ if (res) ++ goto out; ++ ++ if (locked) ++ *buf |= LOCK_SHADOW_W; ++ ++ res = stm32_bsec_read_sp_lock(otp_id, &locked); ++ if (res) ++ goto out; ++ ++ if (locked) ++ *buf |= LOCK_SHADOW_P; ++ ++ FMSG("Read lock %i val: %x", otp_id, *buf); ++ break; ++ ++ default: ++ FMSG("%i invalid operation: %d", otp_id, ++ params[0].value.b); ++ res = TEE_ERROR_BAD_PARAMETERS; ++ } ++ ++ if (res) ++ goto out; ++ } ++ ++ FMSG("Buffer orig %p, size %i\n", buf, size); ++ ++ res = TEE_SUCCESS; ++out: ++ return res; ++} ++ ++static TEE_Result bsec_write_mem(uint32_t pt, TEE_Param params[TEE_NUM_PARAMS]) ++{ ++ const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_MEMREF_INPUT, ++ TEE_PARAM_TYPE_NONE, ++ TEE_PARAM_TYPE_NONE); ++ ++ uint32_t *buf = (uint32_t *)params[1].memref.buffer; ++ uint32_t otp_start = 0; ++ size_t otp_length = 0; ++ uint32_t otp_id = 0; ++ TEE_Result res = TEE_ERROR_GENERIC; ++ size_t size = params[1].memref.size; ++ ++ if (pt != exp_pt || !buf || !size) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ /* check 32bits alignment */ ++ if (params[0].value.a % BSEC_BYTES_PER_WORD || ++ size % BSEC_BYTES_PER_WORD) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ otp_start = params[0].value.a / BSEC_BYTES_PER_WORD; ++ otp_length = params[1].memref.size / BSEC_BYTES_PER_WORD; ++ ++ /* Initial check to ensure that all BSEC words are available */ ++ for (otp_id = otp_start; otp_id < otp_start + otp_length; otp_id++) { ++ res = bsec_check_access(otp_id); ++ if (res) ++ return res; ++ } ++ ++ for (otp_id = otp_start; otp_id < otp_start + otp_length; ++ otp_id++, buf++) { ++ switch (params[0].value.b) { ++ case SHADOW_ACCESS: ++ /* Write shadow register */ ++ FMSG("Write shadow %"PRIx32" : %"PRIx32, ++ otp_id, *buf); ++ res = stm32_bsec_write_otp(*buf, otp_id); ++ break; ++ ++ case FUSE_ACCESS: ++ /* Write fuse value */ ++ FMSG("Write fuse %"PRIx32" : %08"PRIx32, ++ otp_id, *buf); ++ res = stm32_bsec_program_otp(*buf, otp_id); ++ break; ++ ++ case LOCK_ACCESS: ++ if (*buf & LOCK_PERM) { ++ FMSG("Perm lock access"); ++ res = stm32_bsec_permanent_lock_otp(otp_id); ++ if (res) ++ break; ++ } ++ ++ if (*buf & LOCK_SHADOW_R) { ++ FMSG("Shadow read lock detected"); ++ res = stm32_bsec_set_sr_lock(otp_id); ++ if (res) ++ break; ++ } ++ ++ if (*buf & LOCK_SHADOW_W) { ++ FMSG("Shadow write lock detected"); ++ res = stm32_bsec_set_sw_lock(otp_id); ++ if (res) ++ break; ++ } ++ ++ if (*buf & LOCK_SHADOW_P) { ++ FMSG("Shadow programming lock detected"); ++ res = stm32_bsec_set_sp_lock(otp_id); ++ } ++ ++ break; ++ ++ default: ++ FMSG("OTP %"PRIx32" invalid operation: %"PRIx32, ++ otp_id, params[0].value.b); ++ res = TEE_ERROR_BAD_PARAMETERS; ++ } ++ ++ if (res) ++ return res; ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result bsec_pta_state(uint32_t pt, TEE_Param params[TEE_NUM_PARAMS]) ++{ ++ const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_OUTPUT, ++ TEE_PARAM_TYPE_NONE, ++ TEE_PARAM_TYPE_NONE, ++ TEE_PARAM_TYPE_NONE); ++ TEE_Result res = TEE_ERROR_GENERIC; ++ uint32_t state = BSEC_STATE_INVALID; ++ ++ if (pt != exp_pt) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ res = stm32_bsec_get_state(&state); ++ if (res) ++ return res; ++ ++ params[0].value.a = state; ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result bsec_pta_invoke_command(void *pSessionContext __unused, ++ uint32_t cmd_id, ++ uint32_t param_types, ++ TEE_Param params[TEE_NUM_PARAMS]) ++{ ++ FMSG(PTA_NAME" command %#"PRIx32" ptypes %#"PRIx32, ++ cmd_id, param_types); ++ ++ switch (cmd_id) { ++ case PTA_BSEC_READ_MEM: ++ return bsec_read_mem(param_types, params); ++ case PTA_BSEC_WRITE_MEM: ++ return bsec_write_mem(param_types, params); ++ case PTA_BSEC_GET_STATE: ++ return bsec_pta_state(param_types, params); ++ default: ++ break; ++ } ++ ++ return TEE_ERROR_NOT_IMPLEMENTED; ++} ++ ++static TEE_Result pta_bsec_open_session(uint32_t ptypes __unused, ++ TEE_Param par[TEE_NUM_PARAMS] __unused, ++ void **session __unused) ++{ ++ uint32_t login = to_ta_session(ts_get_current_session())->clnt_id.login; ++ struct ts_session *caller_ts = ts_get_calling_session(); ++ static const TEE_UUID ta_uuid = TA_STM32MP_NVMEM_UUID; ++ uint32_t state = BSEC_STATE_INVALID; ++ TEE_Result res = TEE_ERROR_GENERIC; ++ ++ if (!IS_ENABLED(CFG_STM32_BSEC)) ++ return TEE_ERROR_NOT_SUPPORTED; ++ ++ if (login == TEE_LOGIN_TRUSTED_APP && ++ !memcmp((char *)&caller_ts->ctx->uuid, (char *)&ta_uuid, ++ sizeof(TEE_UUID))) { ++ assert(is_user_ta_ctx(caller_ts->ctx)); ++ ++ res = stm32_bsec_get_state(&state); ++ if (res || state != BSEC_STATE_SEC_OPEN) ++ return TEE_ERROR_ACCESS_DENIED; ++ ++ return TEE_SUCCESS; ++ } ++ ++ if (login == TEE_LOGIN_REE_KERNEL) ++ return TEE_SUCCESS; ++ ++ return TEE_ERROR_ACCESS_DENIED; ++} ++ ++pseudo_ta_register(.uuid = PTA_BSEC_UUID, .name = PTA_NAME, ++ .flags = PTA_DEFAULT_FLAGS | TA_FLAG_CONCURRENT | ++ TA_FLAG_DEVICE_ENUM, ++ . open_session_entry_point = pta_bsec_open_session, ++ .invoke_command_entry_point = bsec_pta_invoke_command); +diff --git a/core/arch/arm/plat-stm32mp1/conf.mk b/core/arch/arm/plat-stm32mp1/conf.mk +index e8bc398e0..23e82adb4 100644 +--- a/core/arch/arm/plat-stm32mp1/conf.mk ++++ b/core/arch/arm/plat-stm32mp1/conf.mk +@@ -1,22 +1,50 @@ + # 1GB and 512MB DDR targets do not locate secure DDR at the same place. + flavor_dts_file-157A_DK1 = stm32mp157a-dk1.dts ++flavor_dts_file-157A_ED1 = stm32mp157a-ed1.dts ++flavor_dts_file-157A_EV1 = stm32mp157a-ev1.dts + flavor_dts_file-157C_DK2 = stm32mp157c-dk2.dts + flavor_dts_file-157C_ED1 = stm32mp157c-ed1.dts + flavor_dts_file-157C_EV1 = stm32mp157c-ev1.dts ++flavor_dts_file-157D_DK1 = stm32mp157d-dk1.dts ++flavor_dts_file-157D_ED1 = stm32mp157d-ed1.dts ++flavor_dts_file-157D_EV1 = stm32mp157d-ev1.dts ++flavor_dts_file-157F_DK2 = stm32mp157f-dk2.dts ++flavor_dts_file-157F_ED1 = stm32mp157f-ed1.dts ++flavor_dts_file-157F_EV1 = stm32mp157f-ev1.dts ++flavor_dts_file-135D_DK = stm32mp135d-dk.dts ++flavor_dts_file-135F_DK = stm32mp135f-dk.dts + +-flavorlist-cryp-512M = $(flavor_dts_file-157C_DK2) ++flavorlist-512M = $(flavor_dts_file-157A_DK1) \ ++ $(flavor_dts_file-157C_DK2) \ ++ $(flavor_dts_file-157D_DK1) \ ++ $(flavor_dts_file-157F_DK2) \ ++ $(flavor_dts_file-135D_DK) \ ++ $(flavor_dts_file-135F_DK) + +-flavorlist-no_cryp-512M = $(flavor_dts_file-157A_DK1) ++flavorlist-1G = $(flavor_dts_file-157A_ED1) \ ++ $(flavor_dts_file-157A_EV1) \ ++ $(flavor_dts_file-157D_ED1) \ ++ $(flavor_dts_file-157D_EV1) \ ++ $(flavor_dts_file-157C_ED1) \ ++ $(flavor_dts_file-157C_EV1) \ ++ $(flavor_dts_file-157F_ED1) \ ++ $(flavor_dts_file-157F_EV1) + +-flavorlist-cryp-1G = $(flavor_dts_file-157C_ED1) \ +- $(flavor_dts_file-157C_EV1) ++flavorlist-MP15 = $(flavor_dts_file-157A_DK1) \ ++ $(flavor_dts_file-157C_DK2) \ ++ $(flavor_dts_file-157D_DK1) \ ++ $(flavor_dts_file-157F_DK2) \ ++ $(flavor_dts_file-157A_ED1) \ ++ $(flavor_dts_file-157A_EV1) \ ++ $(flavor_dts_file-157D_ED1) \ ++ $(flavor_dts_file-157D_EV1) \ ++ $(flavor_dts_file-157C_ED1) \ ++ $(flavor_dts_file-157C_EV1) \ ++ $(flavor_dts_file-157F_ED1) \ ++ $(flavor_dts_file-157F_EV1) + +-flavorlist-no_cryp = $(flavorlist-no_cryp-512M) +- +-flavorlist-512M = $(flavorlist-cryp-512M) \ +- $(flavorlist-no_cryp-512M) +- +-flavorlist-1G = $(flavorlist-cryp-1G) ++flavorlist-MP13 = $(flavor_dts_file-135D_DK) \ ++ $(flavor_dts_file-135F_DK) + + ifneq ($(PLATFORM_FLAVOR),) + ifeq ($(flavor_dts_file-$(PLATFORM_FLAVOR)),) +@@ -25,51 +53,132 @@ endif + CFG_EMBED_DTB_SOURCE_FILE ?= $(flavor_dts_file-$(PLATFORM_FLAVOR)) + endif + +-ifneq ($(filter $(CFG_EMBED_DTB_SOURCE_FILE),$(flavorlist-no_cryp)),) +-$(call force,CFG_STM32_CRYP,n) ++ifneq ($(filter $(CFG_EMBED_DTB_SOURCE_FILE),$(flavorlist-MP13)),) ++$(call force,CFG_STM32MP13,y) ++endif ++ ++ifneq ($(filter $(CFG_EMBED_DTB_SOURCE_FILE),$(flavorlist-MP15)),) ++$(call force,CFG_STM32MP15,y) ++endif ++ ++ifeq ($(filter $(CFG_STM32MP15) $(CFG_STM32MP13),y),) ++$(error STM32 Platform must be defined) + endif + + include core/arch/arm/cpu/cortex-a7.mk + +-$(call force,CFG_BOOT_SECONDARY_REQUEST,y) +-$(call force,CFG_DRIVERS_CLK,y) +-$(call force,CFG_DRIVERS_CLK_FIXED,n) ++$(call force,CFG_ARM_GIC_PM,y) + $(call force,CFG_GIC,y) + $(call force,CFG_INIT_CNTVOFF,y) ++$(call force,CFG_PM,y) ++$(call force,CFG_PM_ARM32,y) ++$(call force,CFG_PM_STUBS,y) + $(call force,CFG_PSCI_ARM32,y) +-$(call force,CFG_SECONDARY_INIT_CNTFRQ,y) + $(call force,CFG_SECURE_TIME_SOURCE_CNTPCT,y) + $(call force,CFG_SM_PLATFORM_HANDLER,y) +-$(call force,CFG_WITH_SOFTWARE_PRNG,y) ++ ++ifeq ($(CFG_STM32MP13),y) ++$(call force,CFG_CORE_ASYNC_NOTIF,y) ++$(call force,CFG_CORE_ASYNC_NOTIF_GIC_INTID,31) ++$(call force,CFG_BOOT_SECONDARY_REQUEST,n) ++$(call force,CFG_DRIVERS_CLK,y) ++$(call force,CFG_DRIVERS_CLK_FIXED,y) ++$(call force,CFG_RPROC_PTA,n) ++$(call force,CFG_REGULATOR_DRIVERS,y) ++$(call force,CFG_SECONDARY_INIT_CNTFRQ,n) ++$(call force,CFG_STM32_CRYP,n) ++$(call force,CFG_STM32_EXTI,y) ++$(call force,CFG_STM32_GPIO,y) ++$(call force,CFG_STM32_HSE_MONITORING,y) ++$(call force,CFG_STM32MP_CLK_CORE,y) ++$(call force,CFG_STM32MP1_SCMI_SIP,n) ++$(call force,CFG_STM32MP13_CLK,y) ++$(call force,CFG_STM32MP15,n) ++$(call force,CFG_TEE_CORE_NB_CORE,1) ++$(call force,CFG_TZSRAM_START,0x2ffe0000) ++$(call force,CFG_TZSRAM_SIZE,0x0001f000) ++$(call force,CFG_WITH_NSEC_GPIOS,n) ++CFG_NUM_THREADS ?= 5 ++CFG_WITH_PAGER ?= n ++CFG_WITH_TUI ?= y ++else # Assume CFG_STM32MP15 ++$(call force,CFG_BOOT_SECONDARY_REQUEST,y) ++$(call force,CFG_DDR_LOWPOWER,y) ++$(call force,CFG_DRIVERS_CLK,y) ++$(call force,CFG_DRIVERS_CLK_FIXED,n) ++$(call force,CFG_REGULATOR_DRIVERS,y) ++$(call force,CFG_SCMI_MSG_PERF_DOMAIN,n) ++$(call force,CFG_SECONDARY_INIT_CNTFRQ,y) ++$(call force,CFG_STM32_PKA,n) ++$(call force,CFG_STM32_SAES,n) ++$(call force,CFG_STM32_VREFBUF,n) ++$(call force,CFG_STM32MP13,n) ++$(call force,CFG_STM32MP15,y) ++$(call force,CFG_STM32MP13_CLK,n) ++$(call force,CFG_STM32MP15_CLK,y) ++$(call force,CFG_WITH_NSEC_GPIOS,y) ++CFG_NUM_THREADS ?= 3 ++CFG_STM32MP1_CPU_OPP ?= y ++CFG_TEE_CORE_NB_CORE ?= 2 ++CFG_WITH_PAGER ?= y ++endif # CFG_STM32MPx ++ ++# Trusted User Interface ++ifeq ($(CFG_WITH_TUI),y) ++$(call force,CFG_DISPLAY,y,Mandated by CFG_WITH_TUI) ++$(call force,CFG_FRAME_BUFFER,y,Mandated by CFG_WITH_TUI) ++$(call force,CFG_STM32_LTDC,y,Mandated by CFG_WITH_TUI) ++# Provision virtual space to fit 10MByte plus the TUI frame buffer ++CFG_TUI_FRAME_BUFFER_SIZE_MAX ?= 0x01000000 ++CFG_RESERVED_VASPACE_SIZE ?= (10 * 1024 * 1024 + $(CFG_TUI_FRAME_BUFFER_SIZE_MAX)) ++endif + + ifneq ($(filter $(CFG_EMBED_DTB_SOURCE_FILE),$(flavorlist-512M)),) +-CFG_TZDRAM_START ?= 0xde000000 +-CFG_SHMEM_START ?= 0xdfe00000 + CFG_DRAM_SIZE ?= 0x20000000 + endif + ++CFG_DRAM_BASE ?= 0xc0000000 + CFG_TZSRAM_START ?= 0x2ffc0000 + CFG_TZSRAM_SIZE ?= 0x0003f000 + CFG_STM32MP1_SCMI_SHM_BASE ?= 0x2ffff000 + CFG_STM32MP1_SCMI_SHM_SIZE ?= 0x00001000 +-CFG_TZDRAM_START ?= 0xfe000000 + CFG_TZDRAM_SIZE ?= 0x01e00000 +-CFG_SHMEM_START ?= 0xffe00000 + CFG_SHMEM_SIZE ?= 0x00200000 + CFG_DRAM_SIZE ?= 0x40000000 ++CFG_TZDRAM_START ?= ($(CFG_DRAM_BASE) + $(CFG_DRAM_SIZE) - $(CFG_TZDRAM_SIZE)) ++CFG_SHMEM_START ?= ($(CFG_TZDRAM_START) - $(CFG_SHMEM_SIZE)) + +-CFG_TEE_CORE_NB_CORE ?= 2 +-CFG_WITH_PAGER ?= y + CFG_WITH_LPAE ?= y +-CFG_MMAP_REGIONS ?= 23 ++CFG_MMAP_REGIONS ?= 30 + CFG_DTB_MAX_SIZE ?= (256 * 1024) + ++# Default disable RPC command shared memory allocation caching due to ++# side effect on TEE session release by the Linux tee & optee drivers. ++CFG_PREALLOC_RPC_CACHE ?= n ++ ++# Disable early TA compression to limit HEAP size ++CFG_EARLY_TA_COMPRESS ?= n ++ ++# Embed public part of this key in OP-TEE OS ++CFG_RPROC_SIGN_KEY ?= keys/default_rproc.pem ++ + ifeq ($(CFG_EMBED_DTB_SOURCE_FILE),) + # Some drivers mandate DT support + $(call force,CFG_DRIVERS_CLK_DT,n) ++$(call force,CFG_REGULATOR_FIXED,n) + $(call force,CFG_STM32_CRYP,n) + $(call force,CFG_STM32_GPIO,n) ++$(call force,CFG_STM32_HASH,n) + $(call force,CFG_STM32_I2C,n) ++$(call force,CFG_STM32_IWDG,n) ++$(call force,CFG_STM32_LPTIMER,n) ++$(call force,CFG_STM32_PKA,n) ++$(call force,CFG_STM32_REGULATOR_GPIO,n) ++$(call force,CFG_STM32_RTC,n) ++$(call force,CFG_STM32_SAES,n) ++$(call force,CFG_STM32_TAMP,n) ++$(call force,CFG_STM32_TIM,n) ++$(call force,CFG_STM32_VREFBUF,y) + $(call force,CFG_STPMIC1,n) + $(call force,CFG_STM32MP1_SCMI_SIP,n) + $(call force,CFG_SCMI_PTA,n) +@@ -77,24 +186,76 @@ else + $(call force,CFG_DRIVERS_CLK_DT,y) + endif + ++# Enable Early TA NVMEM for provisioning management ++CFG_TA_STM32MP_NVMEM ?= y ++ifeq ($(CFG_TA_STM32MP_NVMEM),y) ++$(call force,CFG_BSEC_PTA,y,Mandated by CFG_TA_STM32MP_NVMEM) ++CFG_IN_TREE_EARLY_TAS += stm32mp_nvmem/1a8342cc-81a5-4512-99fe-9e2b3e37d626 ++endif ++ ++# Enable BSEC Pseudo TA for fuses access management ++CFG_BSEC_PTA ?= y ++ifeq ($(CFG_BSEC_PTA),y) ++$(call force,CFG_STM32_BSEC,y,Mandated by CFG_BSEC_PTA) ++endif ++ ++# Remoteproc early TA for coprocessor firmware management ++CFG_RPROC_PTA ?= n ++ifeq ($(CFG_RPROC_PTA),y) ++CFG_IN_TREE_EARLY_TAS += remoteproc/80a4c275-0a47-4905-8285-1486a9771a08 ++endif ++ ++CFG_REGULATOR_FIXED ?= y + CFG_STM32_BSEC ?= y ++CFG_STM32_CLKCALIB ?=y + CFG_STM32_CRYP ?= y + CFG_STM32_ETZPC ?= y + CFG_STM32_GPIO ?= y ++CFG_STM32_HASH ?= y + CFG_STM32_I2C ?= y ++CFG_STM32_IWDG ?= y ++CFG_STM32_LPTIMER ?= y ++CFG_STM32_PKA ?= y ++CFG_STM32_REGULATOR_GPIO ?= y + CFG_STM32_RNG ?= y ++CFG_STM32_RTC ?= y ++CFG_STM32_SAES ?= y ++CFG_STM32_TAMP ?= y ++CFG_STM32_TIM ?= y + CFG_STM32_UART ?= y +-$(call force,CFG_STM32MP15_CLK,y) ++CFG_STM32_VREFBUF ?= y ++CFG_STM32MP1_CPU_OPP ?= y + CFG_STPMIC1 ?= y ++CFG_SYSCFG ?= y + CFG_TZC400 ?= y + ++CFG_WITH_SOFTWARE_PRNG ?= n ++ifeq ($(CFG_WITH_SOFTWARE_PRNG),y) ++$(call force,CFG_STM32_RNG,y,Mandated by CFG_WITH_SOFTWARE_PRNG) ++endif ++ ++ifeq ($(CFG_STM32_ETZPC),y) ++$(call force,CFG_STM32_FIREWALL,y) ++endif ++ + ifeq ($(CFG_STPMIC1),y) + $(call force,CFG_STM32_I2C,y) + $(call force,CFG_STM32_GPIO,y) + endif + ++ifeq ($(CFG_STM32_HSE_MONITORING),y) ++$(call force,CFG_STM32_LPTIMER,y) ++endif ++ ++ifeq ($(call cfg-one-enabled, CFG_STM32_LPTIMER CFG_STM32_TIM),y) ++$(call force,CFG_COUNTER_DRIVER,y) ++endif ++ + # if any crypto driver is enabled, enable the crypto-framework layer +-ifeq ($(call cfg-one-enabled, CFG_STM32_CRYP),y) ++ifeq ($(call cfg-one-enabled, CFG_STM32_CRYP \ ++ CFG_STM32_SAES \ ++ CFG_STM32_PKA \ ++ CFG_STM32_HASH),y) + $(call force,CFG_STM32_CRYPTO_DRIVER,y) + endif + +@@ -102,8 +263,9 @@ endif + CFG_STM32MP_PANIC_ON_TZC_PERM_VIOLATION ?= y + + # SiP/OEM service for non-secure world +-CFG_STM32_BSEC_SIP ?= y +-CFG_STM32MP1_SCMI_SIP ?= y ++CFG_STM32_LOWPOWER_SIP ?= $(CFG_PM) ++CFG_STM32_PWR_SIP ?= y ++CFG_STM32MP1_SCMI_SIP ?= n + ifeq ($(CFG_STM32MP1_SCMI_SIP),y) + $(call force,CFG_SCMI_MSG_DRIVERS,y,Mandated by CFG_STM32MP1_SCMI_SIP) + $(call force,CFG_SCMI_MSG_SMT_FASTCALL_ENTRY,y,Mandated by CFG_STM32MP1_SCMI_SIP) +@@ -114,6 +276,15 @@ CFG_SCMI_PTA ?= y + ifeq ($(CFG_SCMI_PTA),y) + $(call force,CFG_SCMI_MSG_DRIVERS,y,Mandated by CFG_SCMI_PTA) + $(call force,CFG_SCMI_MSG_SMT_THREAD_ENTRY,y,Mandated by CFG_SCMI_PTA) ++CFG_CORE_OCALL ?= y ++endif ++ ++# Default enable HWRNG PTA support ++CFG_HWRNG_PTA ?= y ++ifeq ($(CFG_HWRNG_PTA),y) ++$(call force,CFG_STM32_RNG,y,Mandated by CFG_HWRNG_PTA) ++$(call force,CFG_WITH_SOFTWARE_PRNG,n,Mandated by CFG_HWRNG_PTA) ++CFG_HWRNG_QUALITY ?= 1024 + endif + + CFG_SCMI_MSG_DRIVERS ?= n +@@ -121,24 +292,42 @@ ifeq ($(CFG_SCMI_MSG_DRIVERS),y) + $(call force,CFG_SCMI_MSG_CLOCK,y) + $(call force,CFG_SCMI_MSG_RESET_DOMAIN,y) + $(call force,CFG_SCMI_MSG_SMT,y) ++$(call force,CFG_SCMI_MSG_REGULATOR_CONSUMER,y) + $(call force,CFG_SCMI_MSG_VOLTAGE_DOMAIN,y) ++CFG_SCMI_MSG_PERF_DOMAIN ?= y + endif + ++# Default use stm32mp1 PM mailbox context version 3 ++# Use CFG_STM32MP1_PM_CONTEXT_VERSION=1 to force version 0 when dealing with ++# a TF-A firmware that supports version 1 of the context mailbox. ++CFG_STM32MP1_PM_CONTEXT_VERSION ?= 3 ++ + # Default enable some test facitilites + CFG_ENABLE_EMBEDDED_TESTS ?= y + CFG_WITH_STATS ?= y ++CFG_WERROR ?= y ++ ++# Enable to allow debug ++ifeq ($(CFG_TEE_CORE_DEBUG),y) ++CFG_STM32_BSEC_WRITE ?= y ++endif + + # Default disable some support for pager memory size constraint + ifeq ($(CFG_WITH_PAGER),y) + CFG_TEE_CORE_DEBUG ?= n + CFG_UNWIND ?= n + CFG_LOCKDEP ?= n +-CFG_CORE_ASLR ?= n + CFG_TA_BGET_TEST ?= n ++CFG_CORE_HEAP_SIZE ?= 49152 + endif + +-# Non-secure UART and GPIO/pinctrl for the output console +-CFG_WITH_NSEC_GPIOS ?= y ++# Default disable ASLR ++CFG_CORE_ASLR ?= n ++ ++# Non-secure UART for the output console + CFG_WITH_NSEC_UARTS ?= y + # UART instance used for early console (0 disables early console) + CFG_STM32_EARLY_CONSOLE_UART ?= 4 ++ ++# Generate the STM32 files ++CFG_STM32MP15x_STM32IMAGE ?= n +diff --git a/core/arch/arm/plat-stm32mp1/cpu_opp.c b/core/arch/arm/plat-stm32mp1/cpu_opp.c +new file mode 100644 +index 000000000..e38cd2caf +--- /dev/null ++++ b/core/arch/arm/plat-stm32mp1/cpu_opp.c +@@ -0,0 +1,354 @@ ++// SPDX-License-Identifier: BSD-2-Clause ++/* ++ * Copyright (c) 2021, STMicroelectronics ++ */ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++struct cpu_dvfs { ++ unsigned int freq_khz; ++ unsigned int volt_mv; ++}; ++ ++struct cpu_opp { ++ unsigned int current_opp; ++ unsigned int opp_count; ++ struct clk *clock; ++ struct rdev *rdev; ++ struct cpu_dvfs *dvfs; ++}; ++ ++struct cpu_opp cpu_opp; ++ ++/* Mutex for protecting CPU OPP changes */ ++static struct mutex cpu_opp_mu = MUTEX_INITIALIZER; ++ ++#define MPU_RAM_LOW_SPEED_THRESHOLD 1250 ++ ++size_t stm32mp1_cpu_opp_count(void) ++{ ++ return cpu_opp.opp_count; ++} ++ ++unsigned int stm32mp1_cpu_opp_level(size_t opp_index) ++{ ++ assert(opp_index < cpu_opp.opp_count); ++ ++ return cpu_opp.dvfs[opp_index].freq_khz; ++} ++ ++static TEE_Result _set_opp_clk_rate(unsigned int opp) ++{ ++#ifdef CFG_STM32MP15 ++ return stm32mp1_set_opp_khz(cpu_opp.dvfs[opp].freq_khz); ++#else ++ return clk_set_rate(cpu_opp.clock, cpu_opp.dvfs[opp].freq_khz * 1000UL); ++#endif ++} ++ ++static TEE_Result opp_set_voltage(struct rdev *rdev, uint16_t volt_mv) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ uint16_t mv = 0; ++ ++ res = regulator_set_voltage(rdev, volt_mv); ++ if (!res) ++ return TEE_SUCCESS; ++ ++ if (res != TEE_ERROR_NOT_IMPLEMENTED) { ++ EMSG("Failed to set voltage to %umV: %#"PRIx32, volt_mv, res); ++ return res; ++ } ++ ++ /* Fixed regulator management */ ++ if (regulator_get_voltage(rdev, &mv)) ++ return TEE_ERROR_GENERIC; ++ ++ if (mv != volt_mv) ++ return TEE_ERROR_GENERIC; ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result set_clock_then_voltage(unsigned int opp) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ ++ if (_set_opp_clk_rate(opp)) { ++ EMSG("Failed to set clock to %ukHz", ++ cpu_opp.dvfs[opp].freq_khz); ++ return TEE_ERROR_GENERIC; ++ } ++ ++#ifdef CFG_STM32MP13 ++ if (cpu_opp.dvfs[opp].volt_mv <= MPU_RAM_LOW_SPEED_THRESHOLD) ++ io_setbits32(stm32_pwr_base(), PWR_CR1_MPU_RAM_LOW_SPEED); ++#endif ++ ++ res = opp_set_voltage(cpu_opp.rdev, cpu_opp.dvfs[opp].volt_mv); ++ if (res) { ++ unsigned int current_opp = cpu_opp.current_opp; ++ ++ if (current_opp == cpu_opp.opp_count) ++ panic(); ++ ++ if (_set_opp_clk_rate(current_opp)) ++ EMSG("Failed to restore clock"); ++ ++ return res; ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result set_voltage_then_clock(unsigned int opp) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ ++ res = opp_set_voltage(cpu_opp.rdev, cpu_opp.dvfs[opp].volt_mv); ++ if (res) ++ return res; ++ ++#ifdef CFG_STM32MP13 ++ if (cpu_opp.dvfs[opp].volt_mv > MPU_RAM_LOW_SPEED_THRESHOLD) ++ io_clrbits32(stm32_pwr_base(), PWR_CR1_MPU_RAM_LOW_SPEED); ++#endif ++ ++ if (_set_opp_clk_rate(opp)) { ++ unsigned int current_opp = cpu_opp.current_opp; ++ unsigned int previous_volt = 0U; ++ ++ EMSG("Failed to set clock"); ++ ++ if (current_opp == cpu_opp.opp_count) ++ panic(); ++ ++ previous_volt = cpu_opp.dvfs[current_opp].volt_mv; ++ ++ opp_set_voltage(cpu_opp.rdev, previous_volt); ++ ++ return TEE_ERROR_GENERIC; ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++TEE_Result stm32mp1_cpu_opp_set_level(unsigned int level) ++{ ++ unsigned int current_level = 0; ++ TEE_Result res = TEE_ERROR_GENERIC; ++ unsigned int opp = 0; ++ ++ mutex_lock(&cpu_opp_mu); ++ ++ /* Perf level relates straight to CPU frequency in kHz */ ++ current_level = cpu_opp.dvfs[cpu_opp.current_opp].freq_khz; ++ ++ if (level == current_level) { ++ mutex_unlock(&cpu_opp_mu); ++ return TEE_SUCCESS; ++ } ++ ++ for (opp = 0; opp < cpu_opp.opp_count; opp++) ++ if (level == cpu_opp.dvfs[opp].freq_khz) ++ break; ++ ++ if (opp == cpu_opp.opp_count) { ++ mutex_unlock(&cpu_opp_mu); ++ return TEE_ERROR_BAD_PARAMETERS; ++ } ++ ++ if (level < current_level) ++ res = set_clock_then_voltage(opp); ++ else ++ res = set_voltage_then_clock(opp); ++ ++ if (!res) ++ cpu_opp.current_opp = opp; ++ ++ mutex_unlock(&cpu_opp_mu); ++ ++ return res; ++} ++ ++TEE_Result stm32mp1_cpu_opp_read_level(unsigned int *level) ++{ ++ if (cpu_opp.current_opp >= cpu_opp.opp_count) ++ return TEE_ERROR_BAD_STATE; ++ ++ *level = cpu_opp.dvfs[cpu_opp.current_opp].freq_khz; ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result stm32mp1_cpu_opp_is_supported(const void *fdt, int subnode) ++{ ++ const fdt32_t *cuint32 = NULL; ++ uint32_t opp = 0; ++ ++ cuint32 = fdt_getprop(fdt, subnode, "opp-supported-hw", NULL); ++ ++ if (!cuint32) { ++ DMSG("Can't find property opp-supported-hw"); ++ return TEE_ERROR_GENERIC; ++ } ++ ++ opp = fdt32_to_cpu(*cuint32); ++ if (!stm32mp_supports_cpu_opp(opp)) { ++ EMSG("Invalid opp-supported-hw %#"PRIx32, opp); ++ return TEE_ERROR_GENERIC; ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result stm32mp1_cpu_opp_get_dt_subnode(const void *fdt, int node) ++{ ++ const fdt64_t *cuint64 = NULL; ++ const fdt32_t *cuint32 = NULL; ++ uint64_t freq_khz = 0; ++ uint32_t volt_mv = 0; ++ unsigned long clk_cpu = 0; ++ unsigned int i = 0; ++ int subnode = -1; ++ TEE_Result res = TEE_ERROR_GENERIC; ++ ++ fdt_for_each_subnode(subnode, fdt, node) ++ if (!stm32mp1_cpu_opp_is_supported(fdt, subnode)) ++ cpu_opp.opp_count++; ++ ++ cpu_opp.dvfs = calloc(1, cpu_opp.opp_count * sizeof(*cpu_opp.dvfs)); ++ if (!cpu_opp.dvfs) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ cpu_opp.current_opp = cpu_opp.opp_count; ++ ++ fdt_for_each_subnode(subnode, fdt, node) { ++ if (stm32mp1_cpu_opp_is_supported(fdt, subnode)) ++ continue; ++ ++ cuint64 = fdt_getprop(fdt, subnode, "opp-hz", NULL); ++ if (!cuint64) { ++ DMSG("Missing opp-hz"); ++ return TEE_ERROR_GENERIC; ++ } ++ ++ freq_khz = fdt64_to_cpu(*cuint64) / 1000ULL; ++ if (freq_khz > (uint64_t)UINT32_MAX) { ++ DMSG("Invalid opp-hz %"PRIu64, freq_khz); ++ return TEE_ERROR_GENERIC; ++ } ++ ++ cuint32 = fdt_getprop(fdt, subnode, "opp-microvolt", NULL); ++ if (!cuint32) { ++ DMSG("Missing opp-microvolt"); ++ return TEE_ERROR_GENERIC; ++ } ++ ++ volt_mv = fdt32_to_cpu(*cuint32) / 1000U; ++ ++ cpu_opp.dvfs[i].freq_khz = freq_khz; ++ cpu_opp.dvfs[i].volt_mv = volt_mv; ++ ++ DMSG("Found OPP %u (%"PRIu64"kHz/%"PRIu32"mV) from DT", ++ i, freq_khz, volt_mv); ++ ++ if (fdt_getprop(fdt, subnode, "st,opp-default", NULL)) { ++ assert(cpu_opp.current_opp == cpu_opp.opp_count); ++ clk_cpu = clk_get_rate(cpu_opp.clock); ++ assert(clk_cpu); ++ if (freq_khz * 1000U > clk_cpu) ++ res = set_voltage_then_clock(i); ++ else ++ res = set_clock_then_voltage(i); ++ ++ if (res) ++ return res; ++ ++ cpu_opp.current_opp = i; ++ } ++ ++ i++; ++ } ++ ++ if (cpu_opp.current_opp == cpu_opp.opp_count) ++ return TEE_ERROR_GENERIC; ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result get_cpu_parent(const void *fdt) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ const struct fdt_property *prop = NULL; ++ int node = fdt_node_offset_by_compatible(fdt, -1, "arm,cortex-a7"); ++ ++ if (node < 0) { ++ EMSG("Compatible \"arm,cortex-a7\" not found"); ++ panic(); ++ } ++ ++ res = clk_dt_get_by_index(fdt, node, 0, &cpu_opp.clock); ++ if (res) ++ return res; ++ ++ prop = fdt_get_property(fdt, node, "operating-points-v2", NULL); ++ if (!prop) { ++ EMSG("OPP table not defined by CPU"); ++ return TEE_ERROR_GENERIC; ++ } ++ ++ cpu_opp.rdev = regulator_get_by_supply_name(fdt, node, "cpu"); ++ if (!cpu_opp.rdev) ++ return TEE_ERROR_DEFER_DRIVER_INIT; ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result ++stm32mp1_cpu_opp_init(const void *fdt, int node, ++ const void *compat_data __unused) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ uint16_t cpu_voltage __maybe_unused = 0; ++ ++ res = get_cpu_parent(fdt); ++ if (res) ++ return res; ++ ++#ifdef CFG_STM32MP15 ++ res = regulator_get_voltage(cpu_opp.rdev, &cpu_voltage); ++ if (res) ++ return res; ++ ++ if (stm32mp1_clk_compute_all_pll1_settings(cpu_voltage)) ++ panic(); ++#endif ++ ++ res = stm32mp1_cpu_opp_get_dt_subnode(fdt, node); ++ ++ return res; ++} ++ ++static const struct dt_device_match stm32mp1_cpu_opp_match_table[] = { ++ { .compatible = "operating-points-v2" }, ++ { } ++}; ++ ++DEFINE_DT_DRIVER(stm32mp1_opp_dt_driver) = { ++ .name = "stm32mp1-cpu-opp", ++ .match_table = stm32mp1_cpu_opp_match_table, ++ .probe = &stm32mp1_cpu_opp_init, ++}; +diff --git a/core/arch/arm/plat-stm32mp1/display.c b/core/arch/arm/plat-stm32mp1/display.c +new file mode 100644 +index 000000000..94e20d7d9 +--- /dev/null ++++ b/core/arch/arm/plat-stm32mp1/display.c +@@ -0,0 +1,164 @@ ++// SPDX-License-Identifier: BSD-2-Clause ++/* ++ * Copyright (c) 2020-2021, STMicroelectronics ++ * Copyright (c) 2016-2021, Linaro Limited ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++static SLIST_HEAD(, disp_dev_list) display_device_list = ++ SLIST_HEAD_INITIALIZER(disp_dev_list); ++ ++/* ++ * Trusted UI frame buffer description ++ */ ++static struct frame_buffer tui_frame_buffer = { ++ .width = 480, ++ .height = 272, ++ .width_dpi = 160, ++ .height_dpi = 160, ++ .bpp = FB_24BPP, // never used actually. ++}; ++ ++struct frame_buffer *display_get_frame_buffer(void) ++{ ++ return &tui_frame_buffer; ++} ++ ++TEE_Result display_get_fb_addr_from_dtb(paddr_t *pa, size_t *size) ++{ ++ static paddr_t fb_pa; ++ static ssize_t fb_sz; ++ ++ /* Note physical address 0x0 is reserved in OP-TEE core */ ++ if (!fb_pa) { ++ const void *fdt = get_embedded_dt(); ++ int node = fdt_path_offset(fdt, "/reserved-memory"); ++ ++ node = fdt_subnode_offset(fdt, node, "optee-framebuffer"); ++ if (node < 0) { ++ EMSG("Can't find optee-framebuffer"); ++ return TEE_ERROR_ITEM_NOT_FOUND; ++ } ++ ++ fb_pa = _fdt_reg_base_address(fdt, node); ++ fb_sz = _fdt_reg_size(fdt, node); ++ ++ DMSG("TUI frame buffer phys range: [%#"PRIxPA" %#"PRIxPA"]", ++ fb_pa, fb_pa + fb_sz - 1); ++ } ++ ++ if (fb_pa == DT_INFO_INVALID_REG || fb_sz < 0) { ++ DMSG("Can't retrieve TUI frame buffer location"); ++ return TEE_ERROR_GENERIC; ++ } ++ ++ if (pa) ++ *pa = fb_pa; ++ if (size) ++ *size = (size_t)fb_sz; ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result get_fb_from_dtb(struct frame_buffer *fb) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ paddr_t fb_pa = 0; ++ size_t fb_sz = 0; ++ ++ res = display_get_fb_addr_from_dtb(&fb_pa, &fb_sz); ++ if (res) ++ return res; ++ ++ if (!core_mmu_add_mapping(MEM_AREA_RAM_SEC, fb_pa, fb_sz)) { ++ DMSG("Can't map TUI frame buffer"); ++ return TEE_ERROR_GENERIC; ++ } ++ ++ fb->base = phys_to_virt(fb_pa, MEM_AREA_RAM_SEC, fb_sz); ++ assert(fb->base); ++ ++ DMSG("TUI frame buffer virt range: [%p %#"PRIxPTR"]", ++ fb->base, (uintptr_t)fb->base + fb_sz - 1); ++ ++ return TEE_SUCCESS; ++} ++ ++TEE_Result display_init(void) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ uint32_t width = 0, height = 0; ++ struct disp_dev_list *ddev = NULL; ++ ++ if (get_fb_from_dtb(&tui_frame_buffer)) ++ panic(); ++ ++ SLIST_FOREACH(ddev, &display_device_list, link) { ++ if (ddev->device_init) { ++ res = ddev->device_init(ddev->device); ++ if (res != TEE_SUCCESS) ++ return res; ++ } ++ } ++ ++ SLIST_FOREACH(ddev, &display_device_list, link) { ++ if (ddev->device_get_display_size) { ++ res = ddev->device_get_display_size(ddev->device, ++ &width, ++ &height); ++ if (res != TEE_SUCCESS) ++ return res; ++ } ++ } ++ ++ DMSG("TUI frame buffer size: %"PRIu32" x %"PRIu32, width, height); ++ tui_frame_buffer.width = width; ++ tui_frame_buffer.height = height; ++ ++ SLIST_FOREACH(ddev, &display_device_list, link) { ++ if (ddev->device_activate) { ++ res = ddev->device_activate(ddev->device, ++ display_get_frame_buffer(), ++ 0, 0); ++ if (res != TEE_SUCCESS) ++ return res; ++ } ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++void display_final(void) ++{ ++ struct disp_dev_list *ddev = NULL; ++ TEE_Result res = TEE_ERROR_GENERIC; ++ ++ SLIST_FOREACH(ddev, &display_device_list, link) { ++ if (ddev->device_final) { ++ res = ddev->device_final(ddev->device); ++ if (res != TEE_SUCCESS) ++ return; ++ } ++ } ++} ++ ++void display_register_device(struct disp_dev_list *ddev) ++{ ++ SLIST_INSERT_HEAD(&display_device_list, ddev, link); ++} +diff --git a/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_calib.c b/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_calib.c +new file mode 100644 +index 000000000..9b38d62a8 +--- /dev/null ++++ b/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_calib.c +@@ -0,0 +1,615 @@ ++// SPDX-License-Identifier: BSD-3-Clause ++/* ++ * Copyright (c) 2018-2021, STMicroelectronics ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#ifdef CFG_STM32MP13 ++#include ++#endif ++#ifdef CFG_STM32MP15 ++#include ++#endif ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#define OSC_CALIBRATION_COMPAT "st,osc-calibration" ++#define CSI_CALIBRATION_COMPAT "st,csi-cal" ++#define HSI_CALIBRATION_COMPAT "st,hsi-cal" ++#define CALIBRATION_TIMEOUT_US 100000 ++#define PRESCAL_HSI 10 ++#define PRESCAL_CSI 7 ++ ++#define RCC_HSICFGR_HSICAL_MASK_LIMITED GENMASK_32(24, 16) ++ ++enum osc_calibration { ++ HSI_CAL = 0, ++ CSI_CAL, ++ MAX_CAL, ++}; ++ ++/* List of forbidden values for HSI and CSI */ ++static const uint16_t fbv_hsi[] = { ++ 512, 480, 448, 416, 384, 352, 320, 288, ++ 256, 224, 192, 160, 128, 96, 64, 32, 0, ++}; ++ ++static const uint16_t fbv_csi[] = { ++ 256, 240, 224, 208, 192, 176, 160, 144, ++ 128, 112, 96, 80, 64, 48, 32, 16, 0, ++}; ++ ++struct stm32mp1_trim_boundary_t { ++ /* Max boundary trim value around forbidden value */ ++ unsigned int max; ++ /* Min boundary trim value around forbidden value */ ++ unsigned int min; ++}; ++ ++struct counter_calib { ++ struct counter_device *counter; ++ struct clk *clock; ++ void *config; ++}; ++ ++struct stm32mp1_clk_cal { ++ const char *compatible; ++ const char *name; ++ const uint16_t *fbv; ++ unsigned int cal_ref; ++ int trim_max; ++ int trim_min; ++ unsigned int boundary_max; ++ unsigned long ref_freq; ++ unsigned int freq_margin; ++ bool is_used; ++ bool force_init; ++ unsigned int prescaler; ++ struct counter_calib cnt_cal; ++ int (*init_cal)(struct stm32mp1_clk_cal *cal); ++ void (*set_trim)(unsigned int cal, unsigned int cal_ref); ++ unsigned int (*get_cal)(void); ++ struct stm32mp1_trim_boundary_t boundary[16]; ++}; ++ ++struct stm32mp1_osc_cal { ++ uint32_t period_ms; ++ struct stm32mp1_clk_cal cal[MAX_CAL]; ++}; ++ ++static void stm32mp1_clk_hsi_set_trim(unsigned int cal, unsigned int cal_ref); ++static unsigned int stm32mp1_clk_hsi_get_cal(void); ++static void stm32mp1_clk_csi_set_trim(unsigned int cal, unsigned int cal_ref); ++static unsigned int stm32mp1_clk_csi_get_cal(void); ++ ++static int init_hsi_calibration(struct stm32mp1_clk_cal *hsi_cal); ++static int init_csi_calibration(struct stm32mp1_clk_cal *csi_cal); ++ ++static struct stm32mp1_clk_cal hsi_calib_config = { ++ .compatible = HSI_CALIBRATION_COMPAT, ++ .fbv = fbv_hsi, ++ .trim_max = 63, ++ .trim_min = -64, ++ .ref_freq = 0, ++ .freq_margin = 5, ++ .prescaler = PRESCAL_HSI, ++ .init_cal = init_hsi_calibration, ++ .set_trim = stm32mp1_clk_hsi_set_trim, ++ .get_cal = stm32mp1_clk_hsi_get_cal, ++}; ++ ++static struct stm32mp1_clk_cal csi_calib_config = { ++ .compatible = CSI_CALIBRATION_COMPAT, ++ .fbv = fbv_csi, ++ .trim_max = 15, ++ .trim_min = -16, ++ .ref_freq = 0, ++ .freq_margin = 8, ++ .prescaler = PRESCAL_CSI, ++ .init_cal = init_csi_calibration, ++ .set_trim = stm32mp1_clk_csi_set_trim, ++ .get_cal = stm32mp1_clk_csi_get_cal, ++}; ++ ++static struct stm32mp1_osc_cal *osc_cal_device; ++ ++static unsigned int stm32mp1_clk_hsi_get_cal(void) ++{ ++ return (io_read32(stm32_rcc_base() + RCC_HSICFGR) & ++ RCC_HSICFGR_HSICAL_MASK_LIMITED) >> RCC_HSICFGR_HSICAL_SHIFT; ++} ++DECLARE_KEEP_PAGER(stm32mp1_clk_hsi_get_cal); ++ ++static void stm32mp1_clk_hsi_set_trim(unsigned int cal, unsigned int cal_ref) ++{ ++ int clk_trim = 0; ++ uint32_t trim = 0; ++ ++ clk_trim = cal - cal_ref; ++ trim = ((uint32_t)clk_trim << RCC_HSICFGR_HSITRIM_SHIFT) & ++ RCC_HSICFGR_HSITRIM_MASK; ++ ++ io_clrsetbits32(stm32_rcc_base() + RCC_HSICFGR, ++ RCC_HSICFGR_HSITRIM_MASK, trim); ++} ++DECLARE_KEEP_PAGER(stm32mp1_clk_hsi_set_trim); ++ ++static unsigned int stm32mp1_clk_csi_get_cal(void) ++{ ++ return (io_read32(stm32_rcc_base() + RCC_CSICFGR) & ++ RCC_CSICFGR_CSICAL_MASK) >> RCC_CSICFGR_CSICAL_SHIFT; ++} ++DECLARE_KEEP_PAGER(stm32mp1_clk_csi_get_cal); ++ ++static void stm32mp1_clk_csi_set_trim(unsigned int cal, unsigned int cal_ref) ++{ ++ uint32_t trim_max = RCC_CSICFGR_CSITRIM_MASK >> ++ (RCC_CSICFGR_CSITRIM_SHIFT + 1); ++ uint32_t clk_trim = trim_max + cal - cal_ref + 1; ++ uint32_t trim = (clk_trim << RCC_CSICFGR_CSITRIM_SHIFT) & ++ RCC_CSICFGR_CSITRIM_MASK; ++ ++ io_clrsetbits32(stm32_rcc_base() + RCC_CSICFGR, ++ RCC_CSICFGR_CSITRIM_MASK, trim); ++} ++DECLARE_KEEP_PAGER(stm32mp1_clk_csi_set_trim); ++ ++static unsigned long get_freq(struct stm32mp1_clk_cal *clk_cal) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ unsigned int ticks = 0; ++ unsigned long freq = 0; ++ struct counter_calib *cnt = &clk_cal->cnt_cal; ++ ++ res = counter_get_value(cnt->counter, &ticks); ++ if (res && !ticks) ++ panic(); ++ ++ /* ++ * Calibration uses timer in input mode. ++ * It uses the default timer clock to increase ++ * timer counter ++ * Warning: If a prescaler is set in timer, rate ++ * must be adapted accordingly. ++ */ ++ freq = (clk_get_rate(cnt->clock) / ticks) << clk_cal->prescaler; ++ ++ FMSG("%s Freq is %lu", clk_cal->name, freq); ++ ++ if (!freq) ++ panic("Freq calculation error"); ++ ++ return freq; ++} ++ ++static unsigned int trim_increase(struct stm32mp1_clk_cal *clk_cal, ++ unsigned int cal) ++{ ++ struct stm32mp1_trim_boundary_t *boundary = NULL; ++ unsigned int new_cal = 0; ++ int i = 0; ++ ++ /* By default: last calibration value */ ++ new_cal = cal; ++ ++ /* Start from Lowest cal value */ ++ for (i = (int)clk_cal->boundary_max - 1; i >= 0; i--) { ++ boundary = &clk_cal->boundary[i]; ++ ++ if (cal < boundary->min) { ++ new_cal = boundary->min; ++ break; ++ } ++ ++ if (cal >= boundary->min && cal < boundary->max) { ++ new_cal = cal + 1; ++ break; ++ } ++ } ++ ++ return new_cal; ++} ++ ++static unsigned int trim_decrease(struct stm32mp1_clk_cal *clk_cal, ++ unsigned int cal) ++{ ++ struct stm32mp1_trim_boundary_t *boundary = NULL; ++ unsigned int new_cal = 0; ++ unsigned int i = 0; ++ ++ /* By default: last calibration value */ ++ new_cal = cal; ++ ++ /* Start from Highest cal value */ ++ for (i = 0; i < clk_cal->boundary_max; i++) { ++ boundary = &clk_cal->boundary[i]; ++ ++ if (cal > boundary->max) { ++ new_cal = boundary->max; ++ break; ++ } ++ ++ if (cal > boundary->min && cal <= boundary->max) { ++ new_cal = cal - 1; ++ break; ++ } ++ } ++ ++ return new_cal; ++} ++ ++static void osc_calibration(struct stm32mp1_clk_cal *clk_cal) ++{ ++ unsigned long margin = (clk_cal->ref_freq * ++ clk_cal->freq_margin) / 1000; ++ unsigned long min = clk_cal->ref_freq - margin; ++ unsigned long max = clk_cal->ref_freq + margin; ++ unsigned long freq = get_freq(clk_cal); ++ int trim = 0; ++ int new_trim = 0; ++ unsigned long conv = 0; ++ unsigned long min_conv = ULONG_MAX; ++ uint64_t timeout_ref = 0; ++ ++ if (freq >= min && freq <= max && clk_cal->force_init) ++ return; ++ ++ clk_cal->force_init = true; ++ trim = clk_cal->get_cal(); ++ timeout_ref = timeout_init_us(CALIBRATION_TIMEOUT_US); ++ do { ++ if (freq < clk_cal->ref_freq) ++ new_trim = trim_increase(clk_cal, trim); ++ else ++ new_trim = trim_decrease(clk_cal, trim); ++ ++ clk_cal->set_trim(new_trim, clk_cal->cal_ref); ++ freq = get_freq(clk_cal); ++ if (freq == 0) { ++ /* Calibration will be stopped */ ++ clk_cal->ref_freq = 0U; ++ return; ++ } ++ conv = (clk_cal->ref_freq < freq) ? ++ freq - clk_cal->ref_freq : clk_cal->ref_freq - freq; ++ if (conv < min_conv) { ++ min_conv = conv; ++ trim = new_trim; ++ } ++ ++ if (timeout_elapsed(timeout_ref)) ++ break; ++ ++ } while (conv == min_conv); ++ ++ DMSG("Calibration : Freq %lu , trim %i\n", freq, trim); ++ ++ clk_cal->set_trim(trim, clk_cal->cal_ref); ++ freq = get_freq(clk_cal); ++ if (freq < min || freq > max) { ++ EMSG("Calibration failed: Freq %lu , trim %i\n", freq, trim); ++ panic(); ++ } ++} ++ ++static void save_trim(struct stm32mp1_clk_cal *clk_cal, ++ unsigned int i, unsigned int max, unsigned int min) ++{ ++ clk_cal->boundary[i].max = max; ++ clk_cal->boundary[i].min = min; ++} ++ ++static int trim_find_prev_boundary(struct stm32mp1_clk_cal *clk_cal, ++ unsigned int x1) ++{ ++ unsigned int x = x1; ++ unsigned long freq = 0; ++ ++ clk_cal->set_trim(x1 + 1, clk_cal->cal_ref); ++ freq = get_freq(clk_cal); ++ ++ while (x >= (clk_cal->cal_ref + clk_cal->trim_min)) { ++ x--; ++ clk_cal->set_trim(x, clk_cal->cal_ref); ++ ++ if (get_freq(clk_cal) <= freq) ++ break; ++ }; ++ ++ return x; ++} ++ ++static void trim_table_init(struct stm32mp1_clk_cal *clk_cal) ++{ ++ const uint16_t *trim_fbv = clk_cal->fbv; ++ unsigned int min = 0; ++ unsigned int max = 0; ++ int boundary = 0; ++ int i = 0; ++ ++ max = clk_cal->cal_ref + clk_cal->trim_max; ++ min = clk_cal->cal_ref + clk_cal->trim_min; ++ ++ while (trim_fbv[i]) { ++ unsigned int x = 0; ++ unsigned int x1 = trim_fbv[i]; ++ unsigned int x2 = trim_fbv[i + 1]; ++ ++ if (max <= x2 || min >= x1) { ++ i++; ++ if (boundary != 0) ++ goto out; ++ ++ continue; ++ } ++ ++ /* Take forbiden value + 1 */ ++ x2 = x2 + 1; ++ if (x2 < min) ++ x2 = min; ++ ++ if (boundary == 0) { ++ /* Save first boundary */ ++ save_trim(clk_cal, boundary, max, x2); ++ boundary++; ++ i++; ++ continue; ++ } ++ ++ x = trim_find_prev_boundary(clk_cal, x1); ++ /* Save boundary values */ ++ save_trim(clk_cal, boundary, x - 1, x2); ++ boundary++; ++ i++; ++ }; ++out: ++ clk_cal->boundary_max = boundary; ++} ++ ++static void stm32mp_start_clock_calib(struct stm32mp1_osc_cal *calib) ++{ ++ unsigned int i = 0; ++ ++ for (i = 0; i < ARRAY_SIZE(osc_cal_device->cal); i++) { ++ if (calib->cal[i].is_used) { ++ TEE_Result res = TEE_ERROR_GENERIC; ++ struct counter_calib *cnt_cal = &calib->cal[i].cnt_cal; ++ ++ /* Start associated counter */ ++ res = counter_start(cnt_cal->counter, ++ cnt_cal->config); ++ if (res) ++ panic(); ++ ++ osc_calibration(&calib->cal[i]); ++ ++ res = counter_stop(cnt_cal->counter); ++ if (res) ++ panic(); ++ } else { ++ if (HSI_CAL == i) ++ DMSG("HSI clock calibration not supported"); ++ else ++ DMSG("CSI clock calibration not supported"); ++ } ++ } ++} ++ ++static void stm32mp_alarm_calib(unsigned int ticks __maybe_unused, void *priv) ++{ ++ stm32mp_start_clock_calib((struct stm32mp1_osc_cal *)priv); ++} ++DECLARE_KEEP_PAGER(stm32mp_alarm_calib); ++ ++static void set_cal_counter(struct counter_calib *cnt_cal, ++ const void *fdt, int node) ++{ ++ int phandle = 0; ++ int counter_node = 0; ++ const int *cuint = NULL; ++ struct clk *clock = NULL; ++ TEE_Result res = TEE_SUCCESS; ++ ++ cnt_cal->counter = fdt_counter_get(fdt, node, &cnt_cal->config); ++ if (!cnt_cal->counter || !cnt_cal->config) ++ panic(); ++ ++ cuint = fdt_getprop(fdt, node, "counter", NULL); ++ if (!cuint) ++ panic(); ++ ++ phandle = fdt32_to_cpu(*cuint); ++ counter_node = fdt_node_offset_by_phandle(fdt, phandle); ++ if (!counter_node) ++ panic(); ++ ++ res = clk_dt_get_by_index(fdt, counter_node, 0, &clock); ++ if (res) ++ panic(); ++ ++ cnt_cal->clock = clock; ++ ++ FMSG("Counter freq is %li", clk_get_rate(cnt_cal->clock)); ++} ++ ++static int init_hsi_calibration(struct stm32mp1_clk_cal *hsi_cal) ++{ ++ struct clk *hsi_clk = NULL; ++ ++ hsi_clk = stm32mp_rcc_clock_id_to_clk(CK_HSI); ++ ++ hsi_cal->ref_freq = clk_get_rate(hsi_clk); ++ hsi_cal->cal_ref = stm32mp1_clk_hsi_get_cal(); ++ trim_table_init(hsi_cal); ++ hsi_cal->set_trim(hsi_cal->cal_ref, hsi_cal->cal_ref); ++ ++ return 0; ++} ++ ++static int init_csi_calibration(struct stm32mp1_clk_cal *csi_cal) ++{ ++ struct clk *csi_clk = NULL; ++ ++ csi_clk = stm32mp_rcc_clock_id_to_clk(CK_CSI); ++ ++ csi_cal->ref_freq = clk_get_rate(csi_clk); ++ csi_cal->cal_ref = stm32mp1_clk_csi_get_cal(); ++ trim_table_init(csi_cal); ++ csi_cal->set_trim(csi_cal->cal_ref, csi_cal->cal_ref); ++ ++ return 0; ++} ++ ++static TEE_Result ++stm32mp1_calib_pm(enum pm_op op, unsigned int pm_hint __unused, ++ const struct pm_callback_handle *hdl __unused) ++{ ++ unsigned int i = 0; ++ ++ if (op == PM_OP_RESUME) ++ return TEE_SUCCESS; ++ ++ /* SUSPEND */ ++ for (i = 0; i < ARRAY_SIZE(osc_cal_device->cal); i++) { ++ struct stm32mp1_clk_cal *cal = &osc_cal_device->cal[i]; ++ ++ if (cal->is_used) ++ cal->force_init = false; ++ } ++ ++ return TEE_SUCCESS; ++} ++DECLARE_KEEP_PAGER(stm32mp1_calib_pm); ++ ++static TEE_Result init_stm32mp1_calib(void) ++{ ++ void *fdt = NULL; ++ TEE_Result res = TEE_ERROR_GENERIC; ++ int node = 0; ++ int subnode = 0; ++ unsigned int i = 0; ++ ++ fdt = get_embedded_dt(); ++ if (!fdt) ++ panic(); ++ ++ node = fdt_node_offset_by_compatible(fdt, -1, OSC_CALIBRATION_COMPAT); ++ if (node < 0) ++ panic(); ++ ++ osc_cal_device = calloc(1, sizeof(*osc_cal_device)); ++ if (!osc_cal_device) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ osc_cal_device->cal[HSI_CAL] = hsi_calib_config; ++ osc_cal_device->cal[CSI_CAL] = csi_calib_config; ++ ++ /* Parse subnode to find the calibration */ ++ fdt_for_each_subnode(subnode, fdt, node) { ++ const char *compat = NULL; ++ int status = _fdt_get_status(fdt, subnode); ++ ++ if (status == DT_STATUS_DISABLED) ++ continue; ++ ++ compat = fdt_stringlist_get(fdt, subnode, ++ "compatible", 0, NULL); ++ if (!compat) ++ continue; ++ ++ for (i = 0; i < ARRAY_SIZE(osc_cal_device->cal); i++) { ++ struct stm32mp1_clk_cal *cal = &osc_cal_device->cal[i]; ++ struct counter_calib *cnt_cal = &cal->cnt_cal; ++ ++ if (strcmp(cal->compatible, compat) != 0) ++ continue; ++ ++ DMSG("Calibration %s enabled", ++ fdt_get_name(fdt, subnode, NULL)); ++ cal->is_used = true; ++ cal->name = fdt_get_name(fdt, subnode, NULL); ++ ++ /* Start counter */ ++ set_cal_counter(cnt_cal, fdt, subnode); ++ ++ res = counter_start(cnt_cal->counter, ++ cnt_cal->config); ++ if (res) ++ panic(); ++ ++ res = cal->init_cal(cal); ++ if (res) { ++ EMSG("Calibration %s failed", cal->name); ++ panic(); ++ } ++ ++ res = counter_stop(cnt_cal->counter); ++ if (res) ++ panic(); ++ } ++ } ++ ++ stm32mp_start_clock_calib(osc_cal_device); ++ ++ register_pm_driver_cb(stm32mp1_calib_pm, NULL, "service-calib"); ++ ++ return TEE_SUCCESS; ++} ++ ++driver_init_late(init_stm32mp1_calib); ++ ++typedef TEE_Result (*ta_func)(struct ts_session *s, uint32_t param_types, ++ TEE_Param params[TEE_NUM_PARAMS]); ++ ++static TEE_Result request_calibration(struct ts_session *s __unused, ++ uint32_t param_types __unused, ++ TEE_Param params[TEE_NUM_PARAMS] __unused) ++{ ++ assert(osc_cal_device); ++ ++ stm32mp_start_clock_calib(osc_cal_device); ++ ++ return TEE_SUCCESS; ++} ++ ++static const ta_func ta_funcs[] = { ++ [PTA_CALIB_REQUEST] = request_calibration, ++}; ++ ++static TEE_Result calib_invoke_command(void *sess_ctx __unused, uint32_t cmd_id, ++ uint32_t param_types, ++ TEE_Param params[TEE_NUM_PARAMS]) ++{ ++ struct ts_session *s = ts_get_calling_session(); ++ ++ DMSG("Invoking calibration command"); ++ ++ if (cmd_id < ARRAY_SIZE(ta_funcs) && ta_funcs[cmd_id]) ++ return ta_funcs[cmd_id](s, param_types, params); ++ ++ return TEE_ERROR_NOT_IMPLEMENTED; ++} ++ ++pseudo_ta_register(.uuid = PTA_CALIB_UUID, .name = "calib", ++ .flags = PTA_DEFAULT_FLAGS, ++ .invoke_command_entry_point = calib_invoke_command); +diff --git a/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_ddrc.c b/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_ddrc.c +new file mode 100644 +index 000000000..a4dddf006 +--- /dev/null ++++ b/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_ddrc.c +@@ -0,0 +1,655 @@ ++// SPDX-License-Identifier: BSD-3-Clause ++/* ++ * Copyright (c) 2017-2021, STMicroelectronics - All Rights Reserved ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++/* DDR Controller */ ++/* DDR Controller registers offsets */ ++#define DDRCTRL_MSTR 0x000 ++#define DDRCTRL_STAT 0x004 ++#define DDRCTRL_MRCTRL0 0x010 ++#define DDRCTRL_MRSTAT 0x018 ++#define DDRCTRL_PWRCTL 0x030 ++#define DDRCTRL_PWRTMG 0x034 ++#define DDRCTRL_HWLPCTL 0x038 ++#define DDRCTRL_RFSHCTL3 0x060 ++#define DDRCTRL_RFSHTMG 0x064 ++#define DDRCTRL_INIT0 0x0D0 ++#define DDRCTRL_DFIMISC 0x1B0 ++#define DDRCTRL_DBG1 0x304 ++#define DDRCTRL_DBGCAM 0x308 ++#define DDRCTRL_DBGCMD 0x30C ++#define DDRCTRL_DBGSTAT 0x310 ++#define DDRCTRL_SWCTL 0x320 ++#define DDRCTRL_SWSTAT 0x324 ++#define DDRCTRL_PSTAT 0x3FC ++#define DDRCTRL_PCTRL_0 0x490 ++#define DDRCTRL_PCTRL_1 0x540 ++ ++/* DDR Controller Register fields */ ++#define DDRCTRL_MSTR_DDR3 BIT(0) ++#define DDRCTRL_MSTR_LPDDR2 BIT(2) ++#define DDRCTRL_MSTR_LPDDR3 BIT(3) ++#define DDRCTRL_MSTR_DATA_BUS_WIDTH_MASK GENMASK_32(13, 12) ++#define DDRCTRL_MSTR_DATA_BUS_WIDTH_FULL 0 ++#define DDRCTRL_MSTR_DATA_BUS_WIDTH_HALF BIT(12) ++#define DDRCTRL_MSTR_DATA_BUS_WIDTH_QUARTER BIT(13) ++#define DDRCTRL_MSTR_DLL_OFF_MODE BIT(15) ++ ++#define DDRCTRL_STAT_OPERATING_MODE_MASK GENMASK_32(2, 0) ++#define DDRCTRL_STAT_OPERATING_MODE_NORMAL BIT(0) ++#define DDRCTRL_STAT_OPERATING_MODE_SR (BIT(0) | BIT(1)) ++#define DDRCTRL_STAT_SELFREF_TYPE_MASK GENMASK_32(5, 4) ++#define DDRCTRL_STAT_SELFREF_TYPE_ASR (BIT(4) | BIT(5)) ++#define DDRCTRL_STAT_SELFREF_TYPE_SR BIT(5) ++ ++#define DDRCTRL_MRCTRL0_MR_TYPE_WRITE 0 ++/* only one rank supported */ ++#define DDRCTRL_MRCTRL0_MR_RANK_SHIFT 4 ++#define DDRCTRL_MRCTRL0_MR_RANK_ALL \ ++ BIT(DDRCTRL_MRCTRL0_MR_RANK_SHIFT) ++#define DDRCTRL_MRCTRL0_MR_ADDR_SHIFT 12 ++#define DDRCTRL_MRCTRL0_MR_ADDR_MASK GENMASK_32(15, 12) ++#define DDRCTRL_MRCTRL0_MR_WR BIT(31) ++ ++#define DDRCTRL_MRSTAT_MR_WR_BUSY BIT(0) ++ ++#define DDRCTRL_PWRCTL_SELFREF_EN BIT(0) ++#define DDRCTRL_PWRCTL_POWERDOWN_EN BIT(1) ++#define DDRCTRL_PWRCTL_EN_DFI_DRAM_CLK_DISABLE BIT(3) ++#define DDRCTRL_PWRCTL_SELFREF_SW BIT(5) ++ ++#define DDRCTRL_PWRTMG_SELFREF_TO_X32_MASK GENMASK_32(19, 12) ++#define DDRCTRL_PWRTMG_SELFREF_TO_X32_0 BIT(16) ++ ++#define DDRCTRL_RFSHCTL3_DIS_AUTO_REFRESH BIT(0) ++ ++#define DDRCTRL_HWLPCTL_HW_LP_EN BIT(0) ++ ++#define DDRCTRL_RFSHTMG_T_RFC_NOM_X1_X32_MASK GENMASK_32(27, 16) ++#define DDRCTRL_RFSHTMG_T_RFC_NOM_X1_X32_SHIFT 16 ++ ++#define DDRCTRL_INIT0_SKIP_DRAM_INIT_MASK GENMASK_32(31, 30) ++#define DDRCTRL_INIT0_SKIP_DRAM_INIT_NORMAL BIT(30) ++ ++#define DDRCTRL_DFIMISC_DFI_INIT_COMPLETE_EN BIT(0) ++ ++#define DDRCTRL_DBG1_DIS_HIF BIT(1) ++ ++#define DDRCTRL_DBGCAM_WR_DATA_PIPELINE_EMPTY BIT(29) ++#define DDRCTRL_DBGCAM_RD_DATA_PIPELINE_EMPTY BIT(28) ++#define DDRCTRL_DBGCAM_DBG_WR_Q_EMPTY BIT(26) ++#define DDRCTRL_DBGCAM_DBG_LPR_Q_DEPTH GENMASK_32(12, 8) ++#define DDRCTRL_DBGCAM_DBG_HPR_Q_DEPTH GENMASK_32(4, 0) ++ ++#define DDRCTRL_DBGCAM_DATA_PIPELINE_EMPTY \ ++ (DDRCTRL_DBGCAM_WR_DATA_PIPELINE_EMPTY | \ ++ DDRCTRL_DBGCAM_RD_DATA_PIPELINE_EMPTY) ++ ++#define DDRCTRL_DBGCAM_DBG_Q_DEPTH \ ++ (DDRCTRL_DBGCAM_DBG_WR_Q_EMPTY | \ ++ DDRCTRL_DBGCAM_DBG_LPR_Q_DEPTH | \ ++ DDRCTRL_DBGCAM_DBG_HPR_Q_DEPTH) ++ ++#define DDRCTRL_DBGCMD_RANK0_REFRESH BIT(0) ++ ++#define DDRCTRL_DBGSTAT_RANK0_REFRESH_BUSY BIT(0) ++ ++#define DDRCTRL_SWCTL_SW_DONE BIT(0) ++ ++#define DDRCTRL_SWSTAT_SW_DONE_ACK BIT(0) ++ ++#define DDRCTRL_PCTRL_N_PORT_EN BIT(0) ++ ++/* DDR PHY registers offsets */ ++#define DDRPHYC_PIR 0x004 ++#define DDRPHYC_PGCR 0x008 ++#define DDRPHYC_PGSR 0x00C ++#define DDRPHYC_DLLGCR 0x010 ++#define DDRPHYC_ACDLLCR 0x014 ++#define DDRPHYC_PTR0 0x018 ++#define DDRPHYC_ACIOCR 0x024 ++#define DDRPHYC_DXCCR 0x028 ++#define DDRPHYC_DSGCR 0x02C ++#define DDRPHYC_ZQ0CR0 0x180 ++#define DDRPHYC_DX0GCR 0x1C0 ++#define DDRPHYC_DX0DLLCR 0x1CC ++#define DDRPHYC_DX1GCR 0x200 ++#define DDRPHYC_DX1DLLCR 0x20C ++#define DDRPHYC_DX2GCR 0x240 ++#define DDRPHYC_DX2DLLCR 0x24C ++#define DDRPHYC_DX3GCR 0x280 ++#define DDRPHYC_DX3DLLCR 0x28C ++ ++/* DDR PHY Register fields */ ++#define DDRPHYC_PIR_INIT BIT(0) ++#define DDRPHYC_PIR_DLLSRST BIT(1) ++#define DDRPHYC_PIR_DLLLOCK BIT(2) ++#define DDRPHYC_PIR_ZCAL BIT(3) ++#define DDRPHYC_PIR_ITMSRST BIT(4) ++#define DDRPHYC_PIR_DRAMRST BIT(5) ++#define DDRPHYC_PIR_DRAMINIT BIT(6) ++#define DDRPHYC_PIR_QSTRN BIT(7) ++#define DDRPHYC_PIR_ICPC BIT(16) ++#define DDRPHYC_PIR_ZCALBYP BIT(30) ++#define DDRPHYC_PIR_INITSTEPS_MASK GENMASK(31, 7) ++ ++#define DDRPHYC_PGCR_DFTCMP BIT(2) ++#define DDRPHYC_PGCR_PDDISDX BIT(24) ++#define DDRPHYC_PGCR_RFSHDT_MASK GENMASK_32(28, 25) ++ ++#define DDRPHYC_PGSR_IDONE BIT(0) ++#define DDRPHYC_PGSR_DTERR BIT(5) ++#define DDRPHYC_PGSR_DTIERR BIT(6) ++#define DDRPHYC_PGSR_DFTERR BIT(7) ++#define DDRPHYC_PGSR_RVERR BIT(8) ++#define DDRPHYC_PGSR_RVEIRR BIT(9) ++ ++#define DDRPHYC_DLLGCR_BPS200 BIT(23) ++ ++#define DDRPHYC_ACDLLCR_DLLSRST BIT(30) ++#define DDRPHYC_ACDLLCR_DLLDIS BIT(31) ++ ++#define DDRPHYC_PTR0_TDLLSRST_OFFSET 0 ++#define DDRPHYC_PTR0_TDLLSRST_MASK GENMASK_32(5, 0) ++#define DDRPHYC_PTR0_TDLLLOCK_OFFSET 6 ++#define DDRPHYC_PTR0_TDLLLOCK_MASK GENMASK_32(17, 6) ++#define DDRPHYC_PTR0_TITMSRST_OFFSET 18 ++#define DDRPHYC_PTR0_TITMSRST_MASK GENMASK_32(21, 18) ++ ++#define DDRPHYC_ACIOCR_ACOE BIT(1) ++#define DDRPHYC_ACIOCR_ACPDD BIT(3) ++#define DDRPHYC_ACIOCR_ACPDR BIT(4) ++#define DDRPHYC_ACIOCR_CKPDD_MASK GENMASK_32(10, 8) ++#define DDRPHYC_ACIOCR_CKPDD_0 BIT(8) ++#define DDRPHYC_ACIOCR_CKPDR_MASK GENMASK_32(13, 11) ++#define DDRPHYC_ACIOCR_CKPDR_0 BIT(11) ++#define DDRPHYC_ACIOCR_CSPDD_MASK GENMASK_32(21, 18) ++#define DDRPHYC_ACIOCR_CSPDD_0 BIT(18) ++#define DDRPHYC_ACIOCR_RSTPDD BIT(27) ++#define DDRPHYC_ACIOCR_RSTPDR BIT(28) ++ ++#define DDRPHYC_DXCCR_DXPDD BIT(2) ++#define DDRPHYC_DXCCR_DXPDR BIT(3) ++ ++#define DDRPHYC_DSGCR_CKEPDD_MASK GENMASK_32(19, 16) ++#define DDRPHYC_DSGCR_CKEPDD_0 BIT(16) ++#define DDRPHYC_DSGCR_ODTPDD_MASK GENMASK_32(23, 20) ++#define DDRPHYC_DSGCR_ODTPDD_0 BIT(20) ++#define DDRPHYC_DSGCR_NL2PD BIT(24) ++ ++#define DDRPHYC_ZQ0CRN_ZDATA_MASK GENMASK_32(27, 0) ++#define DDRPHYC_ZQ0CRN_ZDATA_SHIFT 0 ++#define DDRPHYC_ZQ0CRN_ZDEN BIT(28) ++#define DDRPHYC_ZQ0CRN_ZQPD BIT(31) ++ ++#define DDRPHYC_DXNGCR_DXEN BIT(0) ++ ++#define DDRPHYC_DXNDLLCR_DLLSRST BIT(30) ++#define DDRPHYC_DXNDLLCR_DLLDIS BIT(31) ++#define DDRPHYC_DXNDLLCR_SDPHASE_MASK GENMASK(17, 14) ++#define DDRPHYC_DXNDLLCR_SDPHASE_SHIFT 14 ++ ++#define TIMEOUT_500US 500 ++ ++static enum stm32mp1_ddr_sr_mode saved_ddr_sr_mode; ++ ++static vaddr_t get_ddrctrl_base(void) ++{ ++ static struct io_pa_va base __nex_data = { .pa = DDRCTRL_BASE }; ++ ++ return io_pa_or_va(&base, 1); ++} ++ ++static vaddr_t get_ddrphy_base(void) ++{ ++ static struct io_pa_va base __nex_data = { .pa = DDRPHYC_BASE }; ++ ++ return io_pa_or_va(&base, 1); ++} ++ ++static void ddr_disable_clock(void) ++{ ++ vaddr_t rcc_base = stm32_rcc_base(); ++ ++ /* Disable all clocks */ ++ io_clrbits32(rcc_base + RCC_DDRITFCR, ++ RCC_DDRITFCR_DDRC1EN | ++ RCC_DDRITFCR_DDRC2EN | ++ RCC_DDRITFCR_DDRPHYCAPBEN | ++ RCC_DDRITFCR_DDRCAPBEN); ++} ++ ++static void ddr_enable_clock(void) ++{ ++ vaddr_t rcc_base = stm32_rcc_base(); ++ ++ /* Enable all clocks */ ++ io_setbits32(rcc_base + RCC_DDRITFCR, ++ RCC_DDRITFCR_DDRC1EN | ++ RCC_DDRITFCR_DDRC2EN | ++ RCC_DDRITFCR_DDRPHYCEN | ++ RCC_DDRITFCR_DDRPHYCAPBEN | ++ RCC_DDRITFCR_DDRCAPBEN); ++} ++ ++static void do_sw_handshake(void) ++{ ++ vaddr_t ddrctrl_base = get_ddrctrl_base(); ++ ++ io_clrbits32(ddrctrl_base + DDRCTRL_SWCTL, DDRCTRL_SWCTL_SW_DONE); ++} ++ ++static void do_sw_ack(void) ++{ ++ uint64_t timeout_ref = 0; ++ vaddr_t ddrctrl_base = get_ddrctrl_base(); ++ ++ io_setbits32(ddrctrl_base + DDRCTRL_SWCTL, DDRCTRL_SWCTL_SW_DONE); ++ ++ timeout_ref = timeout_init_us(TIMEOUT_500US); ++ while (!timeout_elapsed(timeout_ref)) ++ if (io_read32(ddrctrl_base + DDRCTRL_SWSTAT) & ++ DDRCTRL_SWSTAT_SW_DONE_ACK) ++ return; ++ ++ panic(); ++} ++ ++static int ddr_sw_self_refresh_in(void) ++{ ++ uint64_t timeout_ref = 0; ++ uint32_t operating_mode = 0; ++ uint32_t selref_type = 0; ++ uint8_t op_mode_changed = 0; ++ vaddr_t pwr_base = stm32_pwr_base(); ++ vaddr_t rcc_base = stm32_rcc_base(); ++ vaddr_t ddrctrl_base = get_ddrctrl_base(); ++ vaddr_t ddrphy_base = get_ddrphy_base(); ++ ++ io_clrbits32(rcc_base + RCC_DDRITFCR, RCC_DDRITFCR_AXIDCGEN); ++ ++ /* Blocks AXI ports from taking anymore transactions */ ++ io_clrbits32(ddrctrl_base + DDRCTRL_PCTRL_0, DDRCTRL_PCTRL_N_PORT_EN); ++ io_clrbits32(ddrctrl_base + DDRCTRL_PCTRL_1, DDRCTRL_PCTRL_N_PORT_EN); ++ ++ /* ++ * Waits unit all AXI ports are idle ++ * Poll PSTAT.rd_port_busy_n = 0 ++ * Poll PSTAT.wr_port_busy_n = 0 ++ */ ++ timeout_ref = timeout_init_us(TIMEOUT_500US); ++ while (io_read32(ddrctrl_base + DDRCTRL_PSTAT)) ++ if (timeout_elapsed(timeout_ref)) ++ goto pstat_failed; ++ ++ /* SW Self-Refresh entry */ ++ io_setbits32(ddrctrl_base + DDRCTRL_PWRCTL, DDRCTRL_PWRCTL_SELFREF_SW); ++ ++ /* ++ * Wait operating mode change in self-refresh mode ++ * with STAT.operating_mode[1:0]==11. ++ * Ensure transition to self-refresh was due to software ++ * by checking also that STAT.selfref_type[1:0]=2. ++ */ ++ timeout_ref = timeout_init_us(TIMEOUT_500US); ++ while (!timeout_elapsed(timeout_ref)) { ++ uint32_t stat = io_read32(ddrctrl_base + DDRCTRL_STAT); ++ ++ operating_mode = stat & DDRCTRL_STAT_OPERATING_MODE_MASK; ++ selref_type = stat & DDRCTRL_STAT_SELFREF_TYPE_MASK; ++ ++ if (operating_mode == DDRCTRL_STAT_OPERATING_MODE_SR && ++ selref_type == DDRCTRL_STAT_SELFREF_TYPE_SR) { ++ op_mode_changed = 1; ++ break; ++ } ++ } ++ ++ if (op_mode_changed == 0U) ++ goto selfref_sw_failed; ++ ++ /* IOs powering down (PUBL registers) */ ++ io_setbits32(ddrphy_base + DDRPHYC_ACIOCR, DDRPHYC_ACIOCR_ACPDD); ++ io_setbits32(ddrphy_base + DDRPHYC_ACIOCR, DDRPHYC_ACIOCR_ACPDR); ++ io_clrsetbits32(ddrphy_base + DDRPHYC_ACIOCR, ++ DDRPHYC_ACIOCR_CKPDD_MASK, DDRPHYC_ACIOCR_CKPDD_0); ++ io_clrsetbits32(ddrphy_base + DDRPHYC_ACIOCR, ++ DDRPHYC_ACIOCR_CKPDR_MASK, DDRPHYC_ACIOCR_CKPDR_0); ++ io_clrsetbits32(ddrphy_base + DDRPHYC_ACIOCR, ++ DDRPHYC_ACIOCR_CSPDD_MASK, DDRPHYC_ACIOCR_CSPDD_0); ++ io_clrbits32(ddrphy_base + DDRPHYC_ACIOCR, DDRPHYC_ACIOCR_ACOE); ++ io_setbits32(ddrphy_base + DDRPHYC_DXCCR, DDRPHYC_DXCCR_DXPDD); ++ io_setbits32(ddrphy_base + DDRPHYC_DXCCR, DDRPHYC_DXCCR_DXPDR); ++ io_clrsetbits32(ddrphy_base + DDRPHYC_DSGCR, ++ DDRPHYC_DSGCR_ODTPDD_MASK, DDRPHYC_DSGCR_ODTPDD_0); ++ io_setbits32(ddrphy_base + DDRPHYC_DSGCR, DDRPHYC_DSGCR_NL2PD); ++ io_clrsetbits32(ddrphy_base + DDRPHYC_DSGCR, ++ DDRPHYC_DSGCR_CKEPDD_MASK, DDRPHYC_DSGCR_CKEPDD_0); ++ ++ /* Disable PZQ cell (PUBL register) */ ++ io_setbits32(ddrphy_base + DDRPHYC_ZQ0CR0, DDRPHYC_ZQ0CRN_ZQPD); ++ ++ /* Activate sw retention in PWRCTRL */ ++ io_setbits32(pwr_base + PWR_CR3_OFF, PWR_CR3_DDRRETEN); ++ ++ /* Switch controller clocks (uMCTL2/PUBL) to DLL ref clock */ ++ io_setbits32(rcc_base + RCC_DDRITFCR, RCC_DDRITFCR_GSKPCTRL); ++ ++ /* Disable all DLLs: GLITCH window */ ++ io_setbits32(ddrphy_base + DDRPHYC_ACDLLCR, DDRPHYC_ACDLLCR_DLLDIS); ++ io_setbits32(ddrphy_base + DDRPHYC_DX0DLLCR, DDRPHYC_DXNDLLCR_DLLDIS); ++ io_setbits32(ddrphy_base + DDRPHYC_DX1DLLCR, DDRPHYC_DXNDLLCR_DLLDIS); ++ io_setbits32(ddrphy_base + DDRPHYC_DX2DLLCR, DDRPHYC_DXNDLLCR_DLLDIS); ++ io_setbits32(ddrphy_base + DDRPHYC_DX3DLLCR, DDRPHYC_DXNDLLCR_DLLDIS); ++ ++ /* Switch controller clocks (uMCTL2/PUBL) to DLL output clock */ ++ io_clrbits32(rcc_base + RCC_DDRITFCR, RCC_DDRITFCR_GSKPCTRL); ++ ++ /* Disable all clocks */ ++ ddr_disable_clock(); ++ ++ return 0; ++ ++selfref_sw_failed: ++ /* This bit should be cleared to restore DDR in its previous state */ ++ io_clrbits32(ddrctrl_base + DDRCTRL_PWRCTL, DDRCTRL_PWRCTL_SELFREF_SW); ++ ++pstat_failed: ++ io_setbits32(ddrctrl_base + DDRCTRL_PCTRL_0, DDRCTRL_PCTRL_N_PORT_EN); ++ io_setbits32(ddrctrl_base + DDRCTRL_PCTRL_1, DDRCTRL_PCTRL_N_PORT_EN); ++ ++ return -1; ++} ++ ++static int ddr_sw_self_refresh_exit(void) ++{ ++ uint64_t timeout_ref = 0; ++ vaddr_t rcc_base = stm32_rcc_base(); ++ vaddr_t pwr_base = stm32_pwr_base(); ++ vaddr_t ddrctrl_base = get_ddrctrl_base(); ++ vaddr_t ddrphy_base = get_ddrphy_base(); ++ ++ /* Enable all clocks */ ++ ddr_enable_clock(); ++ ++ do_sw_handshake(); ++ ++ /* Mask dfi_init_complete_en */ ++ io_clrbits32(ddrctrl_base + DDRCTRL_DFIMISC, ++ DDRCTRL_DFIMISC_DFI_INIT_COMPLETE_EN); ++ ++ do_sw_ack(); ++ ++ /* Switch controller clocks (uMCTL2/PUBL) to DLL ref clock */ ++ io_setbits32(rcc_base + RCC_DDRITFCR, RCC_DDRITFCR_GSKPCTRL); ++ ++ /* Enable all DLLs: GLITCH window */ ++ io_clrbits32(ddrphy_base + DDRPHYC_ACDLLCR, DDRPHYC_ACDLLCR_DLLDIS); ++ io_clrbits32(ddrphy_base + DDRPHYC_DX0DLLCR, DDRPHYC_DXNDLLCR_DLLDIS); ++ io_clrbits32(ddrphy_base + DDRPHYC_DX1DLLCR, DDRPHYC_DXNDLLCR_DLLDIS); ++ io_clrbits32(ddrphy_base + DDRPHYC_DX2DLLCR, DDRPHYC_DXNDLLCR_DLLDIS); ++ io_clrbits32(ddrphy_base + DDRPHYC_DX3DLLCR, DDRPHYC_DXNDLLCR_DLLDIS); ++ ++ /* Additional delay to avoid early DLL clock switch */ ++ udelay(50); ++ ++ /* Switch controller clocks (uMCTL2/PUBL) to DLL ref clock */ ++ io_clrbits32(rcc_base + RCC_DDRITFCR, RCC_DDRITFCR_GSKPCTRL); ++ io_clrbits32(ddrphy_base + DDRPHYC_ACDLLCR, DDRPHYC_ACDLLCR_DLLSRST); ++ udelay(10); ++ io_setbits32(ddrphy_base + DDRPHYC_ACDLLCR, DDRPHYC_ACDLLCR_DLLSRST); ++ ++ /* PHY partial init: (DLL lock and ITM reset) */ ++ io_write32(ddrphy_base + DDRPHYC_PIR, ++ DDRPHYC_PIR_DLLSRST | DDRPHYC_PIR_DLLLOCK | ++ DDRPHYC_PIR_ITMSRST | DDRPHYC_PIR_INIT); ++ ++ /* Need to wait at least 10 clock cycles before accessing PGSR */ ++ udelay(10); ++ ++ timeout_ref = timeout_init_us(TIMEOUT_500US); ++ while (!(io_read32(ddrphy_base + DDRPHYC_PGSR) & DDRPHYC_PGSR_IDONE)) ++ if (timeout_elapsed(timeout_ref)) ++ return -1; ++ ++ do_sw_handshake(); ++ ++ /* Unmask dfi_init_complete_en to uMCTL2 */ ++ io_setbits32(ddrctrl_base + DDRCTRL_DFIMISC, ++ DDRCTRL_DFIMISC_DFI_INIT_COMPLETE_EN); ++ ++ do_sw_ack(); ++ ++ /* Deactivate sw retention in PWR */ ++ io_clrbits32(pwr_base + PWR_CR3_OFF, PWR_CR3_DDRRETEN); ++ ++ /* Enable PZQ cell (PUBL register) */ ++ io_clrbits32(ddrphy_base + DDRPHYC_ZQ0CR0, DDRPHYC_ZQ0CRN_ZQPD); ++ ++ /* Enable pad drivers */ ++ io_clrbits32(ddrphy_base + DDRPHYC_ACIOCR, DDRPHYC_ACIOCR_ACPDD); ++ io_setbits32(ddrphy_base + DDRPHYC_ACIOCR, DDRPHYC_ACIOCR_ACOE); ++ io_clrbits32(ddrphy_base + DDRPHYC_ACIOCR, DDRPHYC_ACIOCR_CKPDD_MASK); ++ io_clrbits32(ddrphy_base + DDRPHYC_ACIOCR, DDRPHYC_ACIOCR_CSPDD_MASK); ++ io_clrbits32(ddrphy_base + DDRPHYC_DXCCR, DDRPHYC_DXCCR_DXPDD); ++ io_clrbits32(ddrphy_base + DDRPHYC_DXCCR, DDRPHYC_DXCCR_DXPDR); ++ io_clrbits32(ddrphy_base + DDRPHYC_DSGCR, DDRPHYC_DSGCR_ODTPDD_MASK); ++ io_clrbits32(ddrphy_base + DDRPHYC_DSGCR, DDRPHYC_DSGCR_NL2PD); ++ io_clrbits32(ddrphy_base + DDRPHYC_DSGCR, DDRPHYC_DSGCR_CKEPDD_MASK); ++ ++ /* Remove selfrefresh */ ++ io_clrbits32(ddrctrl_base + DDRCTRL_PWRCTL, DDRCTRL_PWRCTL_SELFREF_SW); ++ ++ /* Wait operating_mode == normal */ ++ timeout_ref = timeout_init_us(TIMEOUT_500US); ++ while (1) { ++ if ((io_read32(ddrctrl_base + DDRCTRL_STAT) & ++ DDRCTRL_STAT_OPERATING_MODE_MASK) == ++ DDRCTRL_STAT_OPERATING_MODE_NORMAL) ++ break; ++ ++ if (timeout_elapsed(timeout_ref)) ++ return -1; ++ } ++ ++ /* AXI ports are no longer blocked from taking transactions */ ++ io_setbits32(ddrctrl_base + DDRCTRL_PCTRL_0, DDRCTRL_PCTRL_N_PORT_EN); ++ io_setbits32(ddrctrl_base + DDRCTRL_PCTRL_1, DDRCTRL_PCTRL_N_PORT_EN); ++ ++ io_setbits32(rcc_base + RCC_DDRITFCR, RCC_DDRITFCR_AXIDCGEN); ++ ++ return 0; ++} ++ ++uint32_t get_ddrphy_calibration(void) ++{ ++ vaddr_t ddrphy_base = get_ddrphy_base(); ++ uint32_t zcal = io_read32(ddrphy_base + DDRPHYC_ZQ0CR0); ++ ++ return (zcal & DDRPHYC_ZQ0CRN_ZDATA_MASK) >> DDRPHYC_ZQ0CRN_ZDATA_SHIFT; ++} ++ ++int ddr_standby_sr_entry(void) ++{ ++ vaddr_t pwr_base = stm32_pwr_base(); ++ ++ if (ddr_sw_self_refresh_in()) ++ return -1; ++ ++ /* Enable I/O retention mode in standby */ ++ io_setbits32(pwr_base + PWR_CR3_OFF, PWR_CR3_DDRSREN); ++ ++ return 0; ++} ++ ++int ddr_standby_sr_exit(void) ++{ ++ return ddr_sw_self_refresh_exit(); ++} ++ ++static void ddr_sr_mode_ssr(void) ++{ ++ vaddr_t rcc_ddritfcr = stm32_rcc_base() + RCC_DDRITFCR; ++ vaddr_t ddrctrl_base = get_ddrctrl_base(); ++ ++ io_setbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRC1LPEN); ++ io_setbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRC2LPEN); ++ io_setbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRC1EN); ++ io_setbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRC2EN); ++ io_setbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRCAPBLPEN); ++ io_setbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRPHYCAPBLPEN); ++ io_setbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRCAPBEN); ++ io_setbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRPHYCAPBEN); ++ io_setbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRPHYCEN); ++ io_clrbits32(rcc_ddritfcr, RCC_DDRITFCR_AXIDCGEN); ++ io_clrbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRCKMOD_MASK); ++ ++ /* Disable HW LP interface of uMCTL2 */ ++ io_clrbits32(ddrctrl_base + DDRCTRL_HWLPCTL, DDRCTRL_HWLPCTL_HW_LP_EN); ++ ++ /* Configure Automatic LP modes of uMCTL2 */ ++ io_clrsetbits32(ddrctrl_base + DDRCTRL_PWRTMG, ++ DDRCTRL_PWRTMG_SELFREF_TO_X32_MASK, ++ DDRCTRL_PWRTMG_SELFREF_TO_X32_0); ++ ++ /* ++ * Disable Clock disable with LP modes ++ * (used in RUN mode for LPDDR2 with specific timing). ++ */ ++ io_clrbits32(ddrctrl_base + DDRCTRL_PWRCTL, ++ DDRCTRL_PWRCTL_EN_DFI_DRAM_CLK_DISABLE); ++ ++ /* Disable automatic Self-Refresh mode */ ++ io_clrbits32(ddrctrl_base + DDRCTRL_PWRCTL, ++ DDRCTRL_PWRCTL_SELFREF_EN); ++} ++ ++static void ddr_sr_mode_asr(void) ++{ ++ vaddr_t rcc_ddritfcr = stm32_rcc_base() + RCC_DDRITFCR; ++ vaddr_t ddrctrl_base = get_ddrctrl_base(); ++ ++ io_setbits32(rcc_ddritfcr, RCC_DDRITFCR_AXIDCGEN); ++ io_setbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRC1LPEN); ++ io_setbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRC2LPEN); ++ io_setbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRPHYCLPEN); ++ io_clrsetbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRCKMOD_MASK, ++ RCC_DDRITFCR_DDRCKMOD_ASR1); ++ ++ /* Enable HW LP interface of uMCTL2 */ ++ io_setbits32(ddrctrl_base + DDRCTRL_HWLPCTL, DDRCTRL_HWLPCTL_HW_LP_EN); ++ ++ /* Configure Automatic LP modes of uMCTL2 */ ++ io_clrsetbits32(ddrctrl_base + DDRCTRL_PWRTMG, ++ DDRCTRL_PWRTMG_SELFREF_TO_X32_MASK, ++ DDRCTRL_PWRTMG_SELFREF_TO_X32_0); ++ ++ /* ++ * Enable Clock disable with LP modes ++ * (used in RUN mode for LPDDR2 with specific timing). ++ */ ++ io_setbits32(ddrctrl_base + DDRCTRL_PWRCTL, ++ DDRCTRL_PWRCTL_EN_DFI_DRAM_CLK_DISABLE); ++ ++ /* Enable automatic Self-Refresh for ASR mode */ ++ io_setbits32(ddrctrl_base + DDRCTRL_PWRCTL, ++ DDRCTRL_PWRCTL_SELFREF_EN); ++} ++ ++static void ddr_sr_mode_hsr(void) ++{ ++ vaddr_t rcc_ddritfcr = stm32_rcc_base() + RCC_DDRITFCR; ++ vaddr_t ddrctrl_base = get_ddrctrl_base(); ++ ++ io_setbits32(rcc_ddritfcr, RCC_DDRITFCR_AXIDCGEN); ++ io_clrbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRC1LPEN); ++ io_clrbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRC2LPEN); ++ io_setbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRPHYCLPEN); ++ io_clrsetbits32(rcc_ddritfcr, RCC_DDRITFCR_DDRCKMOD_MASK, ++ RCC_DDRITFCR_DDRCKMOD_HSR1); ++ ++ /* Enable HW LP interface of uMCTL2 */ ++ io_setbits32(ddrctrl_base + DDRCTRL_HWLPCTL, DDRCTRL_HWLPCTL_HW_LP_EN); ++ ++ /* Configure Automatic LP modes of uMCTL2 */ ++ io_clrsetbits32(ddrctrl_base + DDRCTRL_PWRTMG, ++ DDRCTRL_PWRTMG_SELFREF_TO_X32_MASK, ++ DDRCTRL_PWRTMG_SELFREF_TO_X32_0); ++ ++ /* ++ * Enable Clock disable with LP modes ++ * (used in RUN mode for LPDDR2 with specific timing). ++ */ ++ io_setbits32(ddrctrl_base + DDRCTRL_PWRCTL, ++ DDRCTRL_PWRCTL_EN_DFI_DRAM_CLK_DISABLE); ++} ++ ++static enum stm32mp1_ddr_sr_mode ddr_read_sr_mode(void) ++{ ++ uint32_t pwrctl = io_read32(get_ddrctrl_base() + DDRCTRL_PWRCTL); ++ uint32_t mask = DDRCTRL_PWRCTL_EN_DFI_DRAM_CLK_DISABLE | ++ DDRCTRL_PWRCTL_SELFREF_EN; ++ ++ switch (pwrctl & mask) { ++ case 0U: ++ return DDR_SSR_MODE; ++ ++ case DDRCTRL_PWRCTL_EN_DFI_DRAM_CLK_DISABLE: ++ return DDR_HSR_MODE; ++ ++ case DDRCTRL_PWRCTL_EN_DFI_DRAM_CLK_DISABLE | DDRCTRL_PWRCTL_SELFREF_EN: ++ return DDR_ASR_MODE; ++ ++ default: ++ return DDR_SR_MODE_INVALID; ++ } ++} ++ ++static void ddr_set_sr_mode(enum stm32mp1_ddr_sr_mode mode) ++{ ++ switch (mode) { ++ case DDR_SSR_MODE: ++ ddr_sr_mode_ssr(); ++ break; ++ ++ case DDR_HSR_MODE: ++ ddr_sr_mode_hsr(); ++ break; ++ ++ case DDR_ASR_MODE: ++ ddr_sr_mode_asr(); ++ break; ++ ++ default: ++ EMSG("Unknown Self Refresh mode\n"); ++ panic(); ++ } ++} ++ ++void ddr_save_sr_mode(enum stm32mp1_ddr_sr_mode mode) ++{ ++ /* Save current mode before setting new one */ ++ saved_ddr_sr_mode = ddr_read_sr_mode(); ++ ddr_set_sr_mode(mode); ++} ++ ++void ddr_restore_sr_mode(void) ++{ ++ ddr_set_sr_mode(saved_ddr_sr_mode); ++} +diff --git a/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_ddrc.h b/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_ddrc.h +new file mode 100644 +index 000000000..729250a52 +--- /dev/null ++++ b/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_ddrc.h +@@ -0,0 +1,32 @@ ++/* SPDX-License-Identifier: BSD-3-Clause */ ++/* ++ * Copyright (c) 2017-2021, STMicroelectronics - All Rights Reserved ++ */ ++ ++#ifndef __STM32MP1_DDRC_H__ ++#define __STM32MP1_DDRC_H__ ++ ++#include ++ ++/* DDR Self Refresh (SR) modes */ ++enum stm32mp1_ddr_sr_mode { ++ DDR_SR_MODE_INVALID = 0, ++ DDR_SSR_MODE, ++ DDR_HSR_MODE, ++ DDR_ASR_MODE, ++}; ++ ++void ddr_save_sr_mode(enum stm32mp1_ddr_sr_mode mode); ++void ddr_restore_sr_mode(void); ++ ++/* Return 32bit calibration value used for DDRPHY */ ++uint32_t get_ddrphy_calibration(void); ++ ++/* ++ * Entry/exit DDR selfrefresh mode ++ * Return 0 on success and a non-null value on error ++ */ ++int ddr_standby_sr_entry(void); ++int ddr_standby_sr_exit(void); ++ ++#endif /*__STM32MP1_DDRC_H__*/ +diff --git a/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_pmic.c b/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_pmic.c +index f121c65d6..1f7d61f22 100644 +--- a/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_pmic.c ++++ b/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_pmic.c +@@ -3,16 +3,23 @@ + * Copyright (c) 2017-2021, STMicroelectronics + */ + ++#include ++#include ++#include ++#include ++#include + #include + #include ++#include + #include + #include + #include ++#include + #include + #include +-#include + #include + #include ++#include + #include + #include + #include +@@ -31,18 +38,18 @@ + #define PMIC_REGU_COUNT 14 + + /* Expect a single PMIC instance */ +-static struct i2c_handle_s i2c_handle; ++static struct i2c_handle_s *i2c_pmic_handle; + static uint32_t pmic_i2c_addr; + + /* CPU voltage supplier if found */ + static char cpu_supply_name[PMIC_REGU_SUPPLY_NAME_LEN]; + +-bool stm32mp_with_pmic(void) +-{ +- return i2c_handle.dt_status & DT_STATUS_OK_SEC; +-} ++/* Mutex for protecting PMIC accesses */ ++static struct mutex pmic_mu = MUTEX_INITIALIZER; + +-static int dt_get_pmic_node(void *fdt) ++static TEE_Result register_pmic_regulator(const char *regu_name, int node); ++ ++static int dt_get_pmic_node(const void *fdt) + { + static int node = -FDT_ERR_BADOFFSET; + +@@ -52,286 +59,18 @@ static int dt_get_pmic_node(void *fdt) + return node; + } + +-static int dt_pmic_status(void) +-{ +- void *fdt = get_embedded_dt(); +- +- if (fdt) { +- int node = dt_get_pmic_node(fdt); +- +- if (node > 0) +- return _fdt_get_status(fdt, node); +- } +- +- return -1; +-} +- +-int stm32mp_dt_pmic_status(void) +-{ +- return dt_pmic_status(); +-} +- +-static bool dt_pmic_is_secure(void) +-{ +- int status = dt_pmic_status(); +- +- return status == DT_STATUS_OK_SEC && +- i2c_handle.dt_status == DT_STATUS_OK_SEC; +-} +- +-/* +- * struct regu_bo_config - Boot on configuration for a regulator +- * @flags: Operations expected when entering a low power sequence +- * @cfg: Boot-on configuration to apply during low power sequences +- */ +-struct regu_bo_config { +- uint8_t flags; +- struct stpmic1_bo_cfg cfg; +-}; +- +-#define REGU_BO_FLAG_ENABLE_REGU BIT(0) +-#define REGU_BO_FLAG_SET_VOLTAGE BIT(1) +-#define REGU_BO_FLAG_PULL_DOWN BIT(2) +-#define REGU_BO_FLAG_MASK_RESET BIT(3) +- +-static struct regu_bo_config *regu_bo_config; +-static size_t regu_bo_count; +- +-/* boot-on mandatory? if so: caller panic() on error status */ +-static void dt_get_regu_boot_on_config(void *fdt, const char *regu_name, +- int regu_node) +-{ +- const fdt32_t *cuint = NULL; +- struct regu_bo_config regu_cfg = { }; +- uint16_t mv = 0; +- +- if ((!fdt_getprop(fdt, regu_node, "regulator-boot-on", NULL)) && +- (!fdt_getprop(fdt, regu_node, "regulator-always-on", NULL))) +- return; +- +- regu_cfg.flags |= REGU_BO_FLAG_ENABLE_REGU; +- if (stpmic1_bo_enable_cfg(regu_name, ®u_cfg.cfg)) { +- EMSG("PMIC regulator %s not supported", regu_name); +- panic(); +- } +- +- if (fdt_getprop(fdt, regu_node, "regulator-pull-down", NULL)) { +- if (stpmic1_bo_pull_down_cfg(regu_name, ®u_cfg.cfg)) { +- DMSG("No pull down mode for regu %s", regu_name); +- panic(); +- } +- regu_cfg.flags |= REGU_BO_FLAG_PULL_DOWN; +- } +- +- if (fdt_getprop(fdt, regu_node, "st,mask-reset", NULL)) { +- if (stpmic1_bo_mask_reset_cfg(regu_name, ®u_cfg.cfg)) { +- DMSG("No reset mode for regu %s", regu_name); +- panic(); +- } +- regu_cfg.flags |= REGU_BO_FLAG_MASK_RESET; +- } +- +- cuint = fdt_getprop(fdt, regu_node, +- "regulator-min-microvolt", NULL); +- if (cuint) { +- /* DT uses microvolts and driver awaits millivolts */ +- mv = fdt32_to_cpu(*cuint) / 1000; +- +- if (stpmic1_bo_voltage_cfg(regu_name, mv, ®u_cfg.cfg)) +- DMSG("Ignore regulator-min-microvolt for %s", +- regu_name); +- else +- regu_cfg.flags |= REGU_BO_FLAG_SET_VOLTAGE; +- } +- +- /* Save config in the Boot On configuration list */ +- regu_bo_count++; +- regu_bo_config = realloc(regu_bo_config, +- regu_bo_count * sizeof(regu_cfg)); +- if (!regu_bo_config) +- panic(); +- +- regu_bo_config[regu_bo_count - 1] = regu_cfg; +-} +- +-void stm32mp_pmic_apply_boot_on_config(void) +-{ +- size_t i = 0; +- +- for (i = 0; i < regu_bo_count; i++) { +- struct regu_bo_config *regu_cfg = ®u_bo_config[i]; +- +- if (regu_cfg->flags & REGU_BO_FLAG_SET_VOLTAGE) +- if (stpmic1_bo_voltage_unpg(®u_cfg->cfg)) +- panic(); +- +- if (regu_cfg->flags & REGU_BO_FLAG_ENABLE_REGU) +- if (stpmic1_bo_enable_unpg(®u_cfg->cfg)) +- panic(); +- +- if (regu_cfg->flags & REGU_BO_FLAG_PULL_DOWN) +- if (stpmic1_bo_pull_down_unpg(®u_cfg->cfg)) +- panic(); +- +- if (regu_cfg->flags & REGU_BO_FLAG_MASK_RESET) +- if (stpmic1_bo_mask_reset_unpg(®u_cfg->cfg)) +- panic(); +- } +-} +- +-/* +- * @flags: Operations expected when entering a low power sequence +- * @voltage: Target voltage to apply during low power sequences +- */ +-struct regu_lp_config { +- uint8_t flags; +- struct stpmic1_lp_cfg cfg; +-}; +- +-#define REGU_LP_FLAG_LOAD_PWRCTRL BIT(0) +-#define REGU_LP_FLAG_ON_IN_SUSPEND BIT(1) +-#define REGU_LP_FLAG_OFF_IN_SUSPEND BIT(2) +-#define REGU_LP_FLAG_SET_VOLTAGE BIT(3) +-#define REGU_LP_FLAG_MODE_STANDBY BIT(4) +- +-/* +- * struct regu_lp_state - Low power configuration for regulators +- * @name: low power state identifier string name +- * @cfg_count: number of regulator configuration instance in @cfg +- * @cfg: regulator configurations for low power state @name +- */ +-struct regu_lp_state { +- const char *name; +- size_t cfg_count; +- struct regu_lp_config *cfg; +-}; +- +-enum regu_lp_state_id { +- REGU_LP_STATE_DISK = 0, +- REGU_LP_STATE_STANDBY, +- REGU_LP_STATE_MEM, +- REGU_LP_STATE_MEM_LOWVOLTAGE, +- REGU_LP_STATE_COUNT +-}; +- +-static struct regu_lp_state regu_lp_state[REGU_LP_STATE_COUNT] = { +- [REGU_LP_STATE_DISK] = { .name = "standby-ddr-off", }, +- [REGU_LP_STATE_STANDBY] = { .name = "standby-ddr-sr", }, +- [REGU_LP_STATE_MEM] = { .name = "lp-stop", }, +- [REGU_LP_STATE_MEM_LOWVOLTAGE] = { .name = "lplv-stop", }, +-}; ++static int pmic_status = -1; + +-static unsigned int regu_lp_state2idx(const char *name) ++static bool pmic_is_secure(void) + { +- unsigned int i = 0; +- +- for (i = 0; i < ARRAY_SIZE(regu_lp_state); i++) +- if (!strcmp(name, regu_lp_state[i].name)) +- return i; ++ assert(pmic_status != -1); + +- panic(); ++ return pmic_status == DT_STATUS_OK_SEC; + } + +-static void dt_get_regu_low_power_config(void *fdt, const char *regu_name, +- int regu_node, const char *lp_state) ++bool stm32mp_with_pmic(void) + { +- unsigned int state_idx = regu_lp_state2idx(lp_state); +- struct regu_lp_state *state = regu_lp_state + state_idx; +- const fdt32_t *cuint = NULL; +- int regu_state_node = 0; +- struct regu_lp_config *regu_cfg = NULL; +- +- state->cfg_count++; +- state->cfg = realloc(state->cfg, +- state->cfg_count * sizeof(*state->cfg)); +- if (!state->cfg) +- panic(); +- +- regu_cfg = &state->cfg[state->cfg_count - 1]; +- +- memset(regu_cfg, 0, sizeof(*regu_cfg)); +- +- if (stpmic1_regu_has_lp_cfg(regu_name)) { +- if (stpmic1_lp_cfg(regu_name, ®u_cfg->cfg)) { +- DMSG("Cannot setup low power for regu %s", regu_name); +- panic(); +- } +- /* +- * Always copy active configuration (Control register) +- * to PWRCTRL Control register, even if regu_state_node +- * does not exist. +- */ +- regu_cfg->flags |= REGU_LP_FLAG_LOAD_PWRCTRL; +- } +- +- /* Parse regulator stte node if any */ +- regu_state_node = fdt_subnode_offset(fdt, regu_node, lp_state); +- if (regu_state_node <= 0) +- return; +- +- if (fdt_getprop(fdt, regu_state_node, +- "regulator-on-in-suspend", NULL)) +- regu_cfg->flags |= REGU_LP_FLAG_ON_IN_SUSPEND; +- +- if (fdt_getprop(fdt, regu_state_node, +- "regulator-off-in-suspend", NULL)) +- regu_cfg->flags |= REGU_LP_FLAG_OFF_IN_SUSPEND; +- +- cuint = fdt_getprop(fdt, regu_state_node, +- "regulator-suspend-microvolt", NULL); +- if (cuint) { +- uint32_t mv = fdt32_to_cpu(*cuint) / 1000U; +- +- if (stpmic1_lp_voltage_cfg(regu_name, mv, ®u_cfg->cfg)) { +- DMSG("Cannot set voltage for %s", regu_name); +- panic(); +- } +- regu_cfg->flags |= REGU_LP_FLAG_SET_VOLTAGE; +- } +- +- cuint = fdt_getprop(fdt, regu_state_node, +- "regulator-mode", NULL); +- if (cuint && fdt32_to_cpu(*cuint) == MODE_STANDBY) +- regu_cfg->flags |= REGU_LP_FLAG_MODE_STANDBY; +-} +- +-/* +- * int stm32mp_pmic_set_lp_config(char *lp_state) +- * +- * Load the low power configuration stored in regu_lp_state[]. +- */ +-void stm32mp_pmic_apply_lp_config(const char *lp_state) +-{ +- unsigned int state_idx = regu_lp_state2idx(lp_state); +- struct regu_lp_state *state = ®u_lp_state[state_idx]; +- size_t i = 0; +- +- if (stpmic1_powerctrl_on()) +- panic(); +- +- for (i = 0; i < state->cfg_count; i++) { +- struct stpmic1_lp_cfg *cfg = &state->cfg[i].cfg; +- +- if ((state->cfg[i].flags & REGU_LP_FLAG_LOAD_PWRCTRL) && +- stpmic1_lp_load_unpg(cfg)) +- panic(); +- +- if ((state->cfg[i].flags & REGU_LP_FLAG_ON_IN_SUSPEND) && +- stpmic1_lp_on_off_unpg(cfg, 1)) +- panic(); +- +- if ((state->cfg[i].flags & REGU_LP_FLAG_OFF_IN_SUSPEND) && +- stpmic1_lp_on_off_unpg(cfg, 0)) +- panic(); +- +- if ((state->cfg[i].flags & REGU_LP_FLAG_SET_VOLTAGE) && +- stpmic1_lp_voltage_unpg(cfg)) +- panic(); +- +- if ((state->cfg[i].flags & REGU_LP_FLAG_MODE_STANDBY) && +- stpmic1_lp_mode_unpg(cfg, 1)) +- panic(); +- } ++ return pmic_status != -1; + } + + /* Return a libfdt compliant status value */ +@@ -413,9 +152,6 @@ static void parse_regulator_fdt_nodes(void) + int regu_node = 0; + void *fdt = NULL; + +- /* Expected called once */ +- assert(!regu_bo_config && !regu_bo_count); +- + fdt = get_embedded_dt(); + if (!fdt) + panic(); +@@ -431,8 +167,9 @@ static void parse_regulator_fdt_nodes(void) + fdt_for_each_subnode(regu_node, fdt, regulators_node) { + int status = _fdt_get_status(fdt, regu_node); + const char *regu_name = NULL; +- size_t n = 0; ++ size_t __maybe_unused n = 0; + ++ assert(status >= 0); + if (status == DT_STATUS_DISABLED) + continue; + +@@ -443,11 +180,7 @@ static void parse_regulator_fdt_nodes(void) + if (status & DT_STATUS_OK_NSEC) + register_nsec_regu(regu_name); + +- dt_get_regu_boot_on_config(fdt, regu_name, regu_node); +- +- for (n = 0; n < ARRAY_SIZE(regu_lp_state); n++) +- dt_get_regu_low_power_config(fdt, regu_name, regu_node, +- regu_lp_state[n].name); ++ register_pmic_regulator(regu_name, regu_node); + } + + if (save_cpu_supply_name()) +@@ -455,105 +188,260 @@ static void parse_regulator_fdt_nodes(void) + } + + /* +- * Get PMIC and its I2C bus configuration from the device tree. +- * Return 0 on success, 1 if no PMIC node found and a negative value otherwise ++ * PMIC and resource initialization + */ +-static int dt_pmic_i2c_config(struct dt_node_info *i2c_info, +- struct stm32_pinctrl **pinctrl, +- size_t *pinctrl_count, +- struct stm32_i2c_init_s *init) +-{ +- int pmic_node = 0; +- int i2c_node = 0; +- void *fdt = NULL; +- const fdt32_t *cuint = NULL; + +- fdt = get_embedded_dt(); +- if (!fdt) +- return -FDT_ERR_NOTFOUND; ++/* Return true if PMIC is available, false if not found, panics on errors */ ++static void initialize_pmic_i2c(const void *fdt, int pmic_node) ++{ + +- pmic_node = dt_get_pmic_node(fdt); +- if (pmic_node < 0) +- return 1; ++ const fdt32_t *cuint = NULL; + + cuint = fdt_getprop(fdt, pmic_node, "reg", NULL); +- if (!cuint) +- return -FDT_ERR_NOTFOUND; ++ if (!cuint) { ++ EMSG("PMIC configuration failed on reg property"); ++ panic(); ++ } + + pmic_i2c_addr = fdt32_to_cpu(*cuint) << 1; +- if (pmic_i2c_addr > UINT16_MAX) +- return -FDT_ERR_BADVALUE; +- +- i2c_node = fdt_parent_offset(fdt, pmic_node); +- if (i2c_node < 0) +- return -FDT_ERR_NOTFOUND; ++ if (pmic_i2c_addr > UINT16_MAX) { ++ EMSG("PMIC configuration failed on i2c address translation"); ++ panic(); ++ } + +- _fdt_fill_device_info(fdt, i2c_info, i2c_node); +- if (!i2c_info->reg) +- return -FDT_ERR_NOTFOUND; ++ stm32mp_get_pmic(); + +- if (stm32_i2c_get_setup_from_fdt(fdt, i2c_node, init, +- pinctrl, pinctrl_count)) ++ if (!stm32_i2c_is_device_ready(i2c_pmic_handle, pmic_i2c_addr, ++ PMIC_I2C_TRIALS, ++ PMIC_I2C_TIMEOUT_BUSY_MS)) + panic(); + +- return 0; ++ stpmic1_bind_i2c(i2c_pmic_handle, pmic_i2c_addr); ++ ++ stm32mp_put_pmic(); + } + +-/* +- * PMIC and resource initialization +- */ ++#ifdef CFG_REGULATOR_DRIVERS ++static TEE_Result pmic_set_state(const struct regul_desc *desc, bool enable) ++{ ++ int ret = 0; ++ FMSG("%s: set state to %u", desc->node_name, enable); + +-/* Return true if PMIC is available, false if not found, panics on errors */ +-static bool initialize_pmic_i2c(void) ++ if (enable) ++ ret = stpmic1_regulator_enable(desc->node_name); ++ else ++ ret = stpmic1_regulator_disable(desc->node_name); ++ ++ if (ret) ++ return TEE_ERROR_GENERIC; ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result pmic_get_state(const struct regul_desc *desc, bool *enabled) ++{ ++ FMSG("%s: get state\n", desc->node_name); ++ ++ *enabled = stpmic1_is_regulator_enabled(desc->node_name); ++ ++ return TEE_SUCCESS; ++ ++} ++ ++static TEE_Result pmic_get_voltage(const struct regul_desc *desc, uint16_t *mv) + { + int ret = 0; +- struct dt_node_info i2c_info = { }; +- struct i2c_handle_s *i2c = &i2c_handle; +- struct stm32_pinctrl *pinctrl = NULL; +- size_t pin_count = 0; +- struct stm32_i2c_init_s i2c_init = { }; +- +- ret = dt_pmic_i2c_config(&i2c_info, &pinctrl, &pin_count, &i2c_init); +- if (ret < 0) { +- EMSG("I2C configuration failed %d", ret); ++ ++ FMSG("%s: get volt", desc->node_name); ++ ++ ret = stpmic1_regulator_voltage_get(desc->node_name); ++ if (ret < 0) ++ return TEE_ERROR_GENERIC; ++ ++ *mv = ret; ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result pmic_set_voltage(const struct regul_desc *desc, uint16_t mv) ++{ ++ FMSG("%s: set volt", desc->node_name); ++ ++ if (stpmic1_regulator_voltage_set(desc->node_name, mv)) ++ return TEE_ERROR_GENERIC; ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result pmic_list_voltages(const struct regul_desc *desc, ++ uint16_t **levels, size_t *count) ++{ ++ FMSG("%s: list volt", desc->node_name); ++ ++ if (stpmic1_regulator_levels_mv(desc->node_name, ++ (const uint16_t **)levels, count)) ++ return TEE_ERROR_GENERIC; ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result pmic_set_flag(const struct regul_desc *desc, uint16_t flag) ++{ ++ int ret = 0; ++ ++ FMSG("%s: set_flag 0x%x\n", desc->node_name, flag); ++ ++ switch (flag) { ++ case REGUL_OCP: ++ ret = stpmic1_regulator_icc_set(desc->node_name); ++ break; ++ case REGUL_ACTIVE_DISCHARGE: ++ ret = stpmic1_active_discharge_mode_set(desc->node_name); ++ break; ++ case REGUL_PULL_DOWN: ++ ret = stpmic1_regulator_pull_down_set(desc->node_name); ++ break; ++ case REGUL_MASK_RESET: ++ ret = stpmic1_regulator_mask_reset_set(desc->node_name); ++ break; ++ case REGUL_SINK_SOURCE: ++ ret = stpmic1_regulator_sink_mode_set(desc->node_name); ++ break; ++ case REGUL_ENABLE_BYPASS: ++ ret = stpmic1_regulator_bypass_mode_set(desc->node_name); ++ break; ++ default: ++ DMSG("Invalid flag %#"PRIx16, flag); + panic(); + } ++ + if (ret) +- return false; +- +- /* Initialize PMIC I2C */ +- i2c->base.pa = i2c_info.reg; +- i2c->base.va = (vaddr_t)phys_to_virt(i2c->base.pa, MEM_AREA_IO_SEC, 1); +- assert(i2c->base.va); +- i2c->dt_status = i2c_info.status; +- i2c->clock = i2c_init.clock; +- i2c->i2c_state = I2C_STATE_RESET; +- i2c_init.own_address1 = pmic_i2c_addr; +- i2c_init.analog_filter = true; +- i2c_init.digital_filter_coef = 0; +- +- i2c->pinctrl = pinctrl; +- i2c->pinctrl_count = pin_count; ++ return TEE_ERROR_GENERIC; ++ ++ return TEE_SUCCESS; ++} ++ ++static void driver_lock(const struct regul_desc *desc __unused) ++{ ++ if (thread_get_id_may_fail() != THREAD_ID_INVALID) ++ mutex_lock(&pmic_mu); + + stm32mp_get_pmic(); ++} + +- ret = stm32_i2c_init(i2c, &i2c_init); +- if (ret) { +- EMSG("I2C init 0x%" PRIxPA ": %d", i2c_info.reg, ret); +- panic(); ++static void driver_unlock(const struct regul_desc *desc __unused) ++{ ++ if (thread_get_id_may_fail() != THREAD_ID_INVALID) ++ mutex_unlock(&pmic_mu); ++ ++ stm32mp_put_pmic(); ++} ++ ++static TEE_Result driver_suspend(const struct regul_desc *desc, uint8_t state, ++ uint16_t mv) ++{ ++ int ret = 0; ++ ++ FMSG("%s: suspend state:%u %u mV", desc->node_name, state, mv); ++ ++ if (!stpmic1_regu_has_lp_cfg(desc->node_name)) ++ return TEE_SUCCESS; ++ ++ ret = stpmic1_lp_copy_reg(desc->node_name); ++ if (ret) ++ return TEE_ERROR_GENERIC; ++ ++ if ((state & LP_STATE_OFF) != 0U) { ++ ret = stpmic1_lp_reg_on_off(desc->node_name, 0); ++ if (ret) ++ return TEE_ERROR_GENERIC; + } + +- if (!stm32_i2c_is_device_ready(i2c, pmic_i2c_addr, +- PMIC_I2C_TRIALS, +- PMIC_I2C_TIMEOUT_BUSY_MS)) +- panic(); ++ if ((state & LP_STATE_ON) != 0U) { ++ ret = stpmic1_lp_reg_on_off(desc->node_name, 1); ++ if (ret) ++ return TEE_ERROR_GENERIC; ++ } + +- stpmic1_bind_i2c(i2c, pmic_i2c_addr); ++ if ((state & LP_STATE_SET_VOLT) != 0U) { ++ ret = stpmic1_lp_set_voltage(desc->node_name, mv); ++ if (ret) ++ return TEE_ERROR_GENERIC; ++ } + +- stm32mp_put_pmic(); ++ return 0; ++} + +- return true; ++struct regul_ops pmic_ops = { ++ .set_state = pmic_set_state, ++ .get_state = pmic_get_state, ++ .set_voltage = pmic_set_voltage, ++ .get_voltage = pmic_get_voltage, ++ .list_voltages = pmic_list_voltages, ++ .set_flag = pmic_set_flag, ++ .lock = driver_lock, ++ .unlock = driver_unlock, ++ .suspend = driver_suspend, ++}; ++ ++#define DEFINE_REGU(name) { \ ++ .node_name = name, \ ++ .ops = &pmic_ops, \ ++ .ramp_delay_uv_per_us = 2200, \ ++ .enable_ramp_delay_us = 1000, \ ++} ++ ++static const struct regul_ops pmic_sw_ops = { ++ .set_state = pmic_set_state, ++ .get_state = pmic_get_state, ++ .set_flag = pmic_set_flag, ++ .lock = driver_lock, ++ .unlock = driver_unlock, ++ .suspend = driver_suspend, ++}; ++ ++#define DEFINE_SWITCH(name) { \ ++ .node_name = name, \ ++ .ops = &pmic_sw_ops, \ ++ .enable_ramp_delay_us = 1000, \ ++} ++ ++static const struct regul_desc pmic_reguls[] = { ++ DEFINE_REGU("buck1"), ++ DEFINE_REGU("buck2"), ++ DEFINE_REGU("buck3"), ++ DEFINE_REGU("buck4"), ++ DEFINE_REGU("ldo1"), ++ DEFINE_REGU("ldo2"), ++ DEFINE_REGU("ldo3"), ++ DEFINE_REGU("ldo4"), ++ DEFINE_REGU("ldo5"), ++ DEFINE_REGU("ldo6"), ++ DEFINE_REGU("vref_ddr"), ++ DEFINE_REGU("boost"), ++ DEFINE_SWITCH("pwr_sw1"), ++ DEFINE_SWITCH("pwr_sw2"), ++}; ++DECLARE_KEEP_PAGER(pmic_reguls); ++ ++static TEE_Result register_pmic_regulator(const char *regu_name, int node) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ size_t i = 0; ++ ++ for (i = 0; i < ARRAY_SIZE(pmic_reguls); i++) ++ if (!strcmp(pmic_reguls[i].node_name, regu_name)) ++ break; ++ ++ assert(i < ARRAY_SIZE(pmic_reguls)); ++ ++ res = regulator_register(pmic_reguls + i, node); ++ if (res) ++ EMSG("Failed to register %s", regu_name); ++ ++ return res; + } ++#endif /* CFG_REGULATOR_DRIVERS */ + + /* + * Automated suspend/resume at system suspend/resume is expected +@@ -564,9 +452,9 @@ static TEE_Result pmic_pm(enum pm_op op, uint32_t pm_hint __unused, + const struct pm_callback_handle *pm_handle __unused) + { + if (op == PM_OP_SUSPEND) +- stm32_i2c_suspend(&i2c_handle); ++ stm32_i2c_suspend(i2c_pmic_handle); + else +- stm32_i2c_resume(&i2c_handle); ++ stm32_i2c_resume(i2c_pmic_handle); + + return TEE_SUCCESS; + } +@@ -575,50 +463,104 @@ DECLARE_KEEP_PAGER(pmic_pm); + /* stm32mp_get/put_pmic allows secure atomic sequences to use non secure PMIC */ + void stm32mp_get_pmic(void) + { +- stm32_i2c_resume(&i2c_handle); ++ if (!pmic_is_secure()) ++ stm32_i2c_resume(i2c_pmic_handle); + } + + void stm32mp_put_pmic(void) + { +- stm32_i2c_suspend(&i2c_handle); ++ if (!pmic_is_secure()) ++ stm32_i2c_suspend(i2c_pmic_handle); ++} ++ ++/* stm32mp_pm_get/put_pmic enforces get/put PMIC accesses */ ++void stm32mp_pm_get_pmic(void) ++{ ++ stm32_i2c_resume(i2c_pmic_handle); ++} ++ ++void stm32mp_pm_put_pmic(void) ++{ ++ stm32_i2c_suspend(i2c_pmic_handle); + } + + static void register_non_secure_pmic(void) + { +- size_t n = 0; ++ unsigned int __maybe_unused clock_id = 0; + + /* Allow this function to be called when STPMIC1 not used */ +- if (!i2c_handle.base.pa) ++ if (!i2c_pmic_handle->base.pa) + return; + +- for (n = 0; n < i2c_handle.pinctrl_count; n++) +- stm32mp_register_non_secure_gpio(i2c_handle.pinctrl[n].bank, +- i2c_handle.pinctrl[n].pin); ++ if (stm32_pinctrl_set_secure_cfg(i2c_pmic_handle->pinctrl_list, false)) ++ panic(); + +- stm32mp_register_non_secure_periph_iomem(i2c_handle.base.pa); ++ if (IS_ENABLED(CFG_STM32MP15)) { ++ stm32mp_register_non_secure_periph_iomem(i2c_pmic_handle->base.pa); ++ /* ++ * Non secure PMIC can be used by secure world during power state ++ * transition when non-secure world is suspended. Therefore secure ++ * the I2C clock parents, if not specifically the I2C clock itself. ++ */ ++ clock_id = stm32mp_rcc_clk_to_clock_id(i2c_pmic_handle->clock); ++ stm32mp_register_clock_parents_secure(clock_id); ++ } + } + +-static void register_secure_pmic(void) ++static enum itr_return stpmic1_irq_handler(struct itr_handler *handler __unused) + { +- size_t n = 0; ++ uint8_t read_val = 0U; ++ unsigned int i = 0U; ++ ++ FMSG("Stpmic1 irq handler"); ++ ++ stm32mp_get_pmic(); ++ ++ for (i = 0U; i < 4U; i++) { ++ if (stpmic1_register_read(ITLATCH1_REG + i, &read_val)) ++ panic(); ++ ++ if (read_val) { ++ FMSG("Stpmic1 irq pending %u: %#"PRIx8, i, read_val); ++ ++ if (stpmic1_register_write(ITCLEARLATCH1_REG + i, ++ read_val)) ++ panic(); ++ } ++ } ++ ++ stm32mp_put_pmic(); ++ ++ return ITRR_HANDLED; ++} + +- for (n = 0; n < i2c_handle.pinctrl_count; n++) +- stm32mp_register_secure_gpio(i2c_handle.pinctrl[n].bank, +- i2c_handle.pinctrl[n].pin); ++static TEE_Result init_pmic_secure_state(void) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ const struct stm32_firewall_cfg sec_cfg[] = { ++ { FWLL_SEC_RW | FWLL_MASTER(0) }, ++ { }, /* Null terminated */ ++ }; ++ ++ res = stm32_firewall_check_access(i2c_pmic_handle->base.pa, ++ 0, sec_cfg); ++ ++ if (!res) ++ pmic_status = DT_STATUS_OK_SEC; ++ else ++ pmic_status = DT_STATUS_OK_NSEC; + +- stm32mp_register_secure_periph_iomem(i2c_handle.base.pa); +- register_pm_driver_cb(pmic_pm, NULL, "stm32mp1-pmic"); ++ return res; + } + +-static TEE_Result initialize_pmic(void) ++static void initialize_pmic(const void *fdt, int pmic_node) + { + unsigned long pmic_version = 0; + +- if (!initialize_pmic_i2c()) { +- DMSG("No PMIC"); ++ if (init_pmic_secure_state()) + register_non_secure_pmic(); +- return TEE_SUCCESS; +- } ++ ++ initialize_pmic_i2c(fdt, pmic_node); + + stm32mp_get_pmic(); + +@@ -628,15 +570,66 @@ static TEE_Result initialize_pmic(void) + DMSG("PMIC version = 0x%02lx", pmic_version); + stpmic1_dump_regulators(); + +- if (dt_pmic_is_secure()) +- register_secure_pmic(); +- else +- register_non_secure_pmic(); ++ if (stpmic1_powerctrl_on()) ++ panic("Failed to enable PMIC pwr control"); ++ ++ register_pm_core_service_cb(pmic_pm, NULL, "stm32mp1-pmic"); + + parse_regulator_fdt_nodes(); + + stm32mp_put_pmic(); ++} + +- return TEE_SUCCESS; ++static TEE_Result stm32_pmic_probe(const void *fdt, int node, ++ const void *compat_data __unused) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ const fdt32_t *cuint = NULL; ++ struct itr_handler *hdl = NULL; ++ size_t it = 0; ++ ++ res = i2c_dt_get_by_subnode(fdt, node, &i2c_pmic_handle); ++ if (res) ++ return res; ++ ++ initialize_pmic(fdt, node); ++ ++ if (IS_ENABLED(CFG_STM32MP13)) { ++ cuint = fdt_getprop(fdt, node, "st,wakeup-pin-number", NULL); ++ if (!cuint) { ++ IMSG("Missing wake-up pin description"); ++ return TEE_SUCCESS; ++ } ++ ++ it = fdt32_to_cpu(*cuint) - 1U; ++ ++ res = stm32mp1_pwr_itr_alloc_add(it, stpmic1_irq_handler, ++ PWR_WKUP_FLAG_FALLING, NULL, ++ &hdl); ++ if (res) ++ panic("pmic: Couldn't allocate itr"); ++ ++ stm32mp1_pwr_itr_enable(hdl->it); ++ ++ /* Enable ponkey irq */ ++ stm32mp_get_pmic(); ++ if (stpmic1_register_write(ITCLEARMASK1_REG, ++ BIT(IT_PONKEY_F) | BIT(IT_PONKEY_R))) ++ panic(); ++ ++ stm32mp_put_pmic(); ++ } ++ ++ return res; + } +-driver_init(initialize_pmic); ++ ++static const struct dt_device_match stm32_pmic_match_table[] = { ++ { .compatible = "st,stpmic1" }, ++ { } ++}; ++ ++DEFINE_DT_DRIVER(stm32_pmic_dt_driver) = { ++ .name = "stm32_pmic", ++ .match_table = stm32_pmic_match_table, ++ .probe = stm32_pmic_probe, ++}; +diff --git a/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_pmic.h b/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_pmic.h +index 4625af125..499b56ab0 100644 +--- a/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_pmic.h ++++ b/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_pmic.h +@@ -8,13 +8,9 @@ + + #include + +-#ifdef CFG_STPMIC1 ++#if defined(CFG_STPMIC1) && defined(CFG_STM32MP15) + void stm32mp_pmic_apply_boot_on_config(void); + void stm32mp_pmic_apply_lp_config(const char *lp_state); +-void stm32mp_get_pmic(void); +-void stm32mp_put_pmic(void); +-int stm32mp_dt_pmic_status(void); +-const char *stm32mp_pmic_get_cpu_supply_name(void); + #else + static inline void stm32mp_pmic_apply_boot_on_config(void) + { +@@ -23,7 +19,16 @@ static inline void stm32mp_pmic_apply_boot_on_config(void) + static inline void stm32mp_pmic_apply_lp_config(const char *lp_state __unused) + { + } ++#endif + ++#if defined(CFG_STPMIC1) ++void stm32mp_get_pmic(void); ++void stm32mp_put_pmic(void); ++void stm32mp_pm_get_pmic(void); ++void stm32mp_pm_put_pmic(void); ++int stm32mp_dt_pmic_status(void); ++const char *stm32mp_pmic_get_cpu_supply_name(void); ++#else + static inline void stm32mp_get_pmic(void) + { + panic(); +@@ -34,9 +39,14 @@ static inline void stm32mp_put_pmic(void) + panic(); + } + +-static inline int stm32mp_dt_pmic_status(void) ++static inline void stm32mp_pm_get_pmic(void) + { +- return -1; ++ panic(); ++} ++ ++static inline void stm32mp_pm_put_pmic(void) ++{ ++ panic(); + } + + static inline const char *stm32mp_pmic_get_cpu_supply_name(void) +diff --git a/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_pwr.c b/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_pwr.c +index 3ec0d42c3..dcaf93d19 100644 +--- a/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_pwr.c ++++ b/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_pwr.c +@@ -1,15 +1,36 @@ + // SPDX-License-Identifier: BSD-3-Clause + /* +- * Copyright (c) 2018-2019, STMicroelectronics ++ * Copyright (c) 2018-2021, STMicroelectronics + */ + ++#include + #include ++#include ++#include + #include ++#include ++#include + #include ++#include + #include ++#include ++#include + #include + #include + ++#define TIMEOUT_US_10MS 10U * 1000U ++ ++#define PWR_CR3_VBE BIT(8) ++#define PWR_CR3_VBRS BIT(9) ++ ++/* STM32MP13x VDDSD1/2 controls */ ++#define PWR_CR3_VDDSD1_EN BIT(13) ++#define PWR_CR3_VDDSD1_RDY BIT(14) ++#define PWR_CR3_VDDSD2_EN BIT(15) ++#define PWR_CR3_VDDSD2_RDY BIT(16) ++#define PWR_CR3_VDDSD1_VALID BIT(22) ++#define PWR_CR3_VDDSD2_VALID BIT(23) ++ + #define PWR_CR3_USB33_EN BIT(24) + #define PWR_CR3_USB33_RDY BIT(26) + #define PWR_CR3_REG18_EN BIT(28) +@@ -17,8 +38,11 @@ + #define PWR_CR3_REG11_EN BIT(30) + #define PWR_CR3_REG11_RDY BIT(31) + ++/* Mutex for protecting PMIC accesses */ ++static struct mutex pwr_regul_mu = MUTEX_INITIALIZER; ++ + struct pwr_regu_desc { +- unsigned int level_mv; ++ uint16_t level_mv; + uint32_t cr3_enable_mask; + uint32_t cr3_ready_mask; + }; +@@ -55,28 +79,39 @@ unsigned int stm32mp1_pwr_regulator_mv(enum pwr_regulator id) + return pwr_regulators[id].level_mv; + } + +-void stm32mp1_pwr_regulator_set_state(enum pwr_regulator id, bool enable) ++static TEE_Result pwr_set_state(const struct regul_desc *desc, bool enable) + { +- uintptr_t cr3 = stm32_pwr_base() + PWR_CR3_OFF; +- uint32_t enable_mask = pwr_regulators[id].cr3_enable_mask; +- +- assert(id < PWR_REGU_COUNT); ++ struct pwr_regu_desc *p = (struct pwr_regu_desc *)desc->driver_data; ++ uintptr_t pwr_cr3 = stm32_pwr_base() + PWR_CR3_OFF; ++ uint32_t enable_mask = p->cr3_enable_mask; + + if (enable) { +- uint64_t to = timeout_init_us(10 * 1000); +- uint32_t ready_mask = pwr_regulators[id].cr3_ready_mask; ++ uint64_t to = timeout_init_us(TIMEOUT_US_10MS); ++ uint32_t ready_mask = p->cr3_ready_mask; + +- io_setbits32(cr3, enable_mask); ++ io_setbits32(pwr_cr3, enable_mask); + + while (!timeout_elapsed(to)) +- if (io_read32(cr3) & ready_mask) ++ if (io_read32(pwr_cr3) & ready_mask) + break; + +- if (!(io_read32(cr3) & ready_mask)) +- panic(); ++ if (!(io_read32(pwr_cr3) & ready_mask)) ++ return TEE_ERROR_GENERIC; + } else { +- io_clrbits32(cr3, enable_mask); ++ io_clrbits32(pwr_cr3, enable_mask); + } ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result pwr_get_state(const struct regul_desc *desc, bool *enabled) ++{ ++ struct pwr_regu_desc *p = (struct pwr_regu_desc *)desc->driver_data; ++ uintptr_t pwr_cr3 = stm32_pwr_base() + PWR_CR3_OFF; ++ ++ *enabled = io_read32(pwr_cr3) & p->cr3_enable_mask; ++ ++ return TEE_SUCCESS; + } + + bool stm32mp1_pwr_regulator_is_enabled(enum pwr_regulator id) +@@ -86,3 +121,135 @@ bool stm32mp1_pwr_regulator_is_enabled(enum pwr_regulator id) + return io_read32(stm32_pwr_base() + PWR_CR3_OFF) & + pwr_regulators[id].cr3_enable_mask; + } ++ ++static TEE_Result pwr_get_voltage(const struct regul_desc *desc, uint16_t *mv) ++{ ++ struct pwr_regu_desc *p = (struct pwr_regu_desc *)desc->driver_data; ++ ++ *mv = p->level_mv; ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result pwr_list_voltages(const struct regul_desc *desc, ++ uint16_t **levels, size_t *count) ++{ ++ struct pwr_regu_desc *p = (struct pwr_regu_desc *)desc->driver_data; ++ ++ *count = 1; ++ *levels = &p->level_mv; ++ ++ return TEE_SUCCESS; ++} ++ ++static void pwr_regul_lock(const struct regul_desc *desc __unused) ++{ ++ if (thread_get_id_may_fail() != THREAD_ID_INVALID) ++ mutex_lock(&pwr_regul_mu); ++} ++ ++static void pwr_regul_unlock(const struct regul_desc *desc __unused) ++{ ++ if (thread_get_id_may_fail() != THREAD_ID_INVALID) ++ mutex_unlock(&pwr_regul_mu); ++} ++ ++struct regul_ops pwr_ops = { ++ .set_state = pwr_set_state, ++ .get_state = pwr_get_state, ++ .get_voltage = pwr_get_voltage, ++ .list_voltages = pwr_list_voltages, ++ .lock = pwr_regul_lock, ++ .unlock = pwr_regul_unlock, ++}; ++ ++#define DEFINE_REG(id, name, supply) { \ ++ .node_name = name, \ ++ .ops = &pwr_ops, \ ++ .driver_data = &pwr_regulators[id], \ ++ .supply_name = supply, \ ++} ++ ++static struct regul_desc stm32mp1_pwr_regs[] = { ++ [PWR_REG11] = DEFINE_REG(PWR_REG11, "reg11", "vdd"), ++ [PWR_REG18] = DEFINE_REG(PWR_REG18, "reg18", "vdd"), ++ [PWR_USB33] = DEFINE_REG(PWR_USB33, "usb33", "vdd_3v3_usbfs"), ++}; ++DECLARE_KEEP_PAGER(stm32mp1_pwr_regs); ++ ++#ifdef CFG_EMBED_DTB ++static void enable_sd_io(uint32_t enable_mask, uint32_t ready_mask, ++ uint32_t valid_mask) ++{ ++ uintptr_t cr3 = stm32_pwr_base() + PWR_CR3_OFF; ++ uint64_t to = timeout_init_us(TIMEOUT_US_10MS); ++ ++ io_setbits32(cr3, enable_mask); ++ ++ while (!timeout_elapsed(to)) ++ if (io_read32(cr3) & ready_mask) ++ break; ++ ++ if (!(io_read32(cr3) & ready_mask)) ++ DMSG("timeout during enable sd io for %#"PRIx32, enable_mask); ++ ++ /* Disable voltage detector and keep the IOs powered */ ++ io_setbits32(cr3, valid_mask); ++ io_clrbits32(cr3, enable_mask); ++} ++ ++static TEE_Result stm32mp1_pwr_regu_probe(const void *fdt, int node, ++ const void *compat_data __unused) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ int subnode = 0; ++ ++ fdt_for_each_subnode(subnode, fdt, node) { ++ const char *reg_name = fdt_get_name(fdt, subnode, NULL); ++ const struct regul_desc *desc = NULL; ++ unsigned int i = 0; ++ ++ for (i = 0; i < ARRAY_SIZE(stm32mp1_pwr_regs); i++) { ++ desc = &stm32mp1_pwr_regs[i]; ++ if (!strcmp(stm32mp1_pwr_regs[i].node_name, reg_name)) ++ break; ++ } ++ assert(i != ARRAY_SIZE(stm32mp1_pwr_regs)); ++ ++ res = regulator_register(desc, subnode); ++ if (res) { ++ EMSG("Can't register %s: %#"PRIx32, reg_name, res); ++ panic(); ++ } ++ } ++ ++ if (IS_ENABLED(CFG_STM32MP13)) { ++ enable_sd_io(PWR_CR3_VDDSD1_EN, PWR_CR3_VDDSD1_RDY, ++ PWR_CR3_VDDSD1_VALID); ++ enable_sd_io(PWR_CR3_VDDSD2_EN, PWR_CR3_VDDSD2_RDY, ++ PWR_CR3_VDDSD2_VALID); ++ } ++ ++ if (fdt_getprop(fdt, node, "st,enable-vbat-charge", NULL)) { ++ uintptr_t cr3 = stm32_pwr_base() + PWR_CR3_OFF; ++ ++ io_setbits32(cr3, PWR_CR3_VBE); ++ ++ if (fdt_getprop(fdt, node, "st,vbat-charge-1K5", NULL)) ++ io_setbits32(cr3, PWR_CR3_VBRS); ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++static const struct dt_device_match pwr_regu_match_table[] = { ++ { .compatible = "st,stm32mp1,pwr-reg"}, ++ { } ++}; ++ ++DEFINE_DT_DRIVER(stm32mp1_pwr_regu_dt_driver) = { ++ .name = "stm32mp1-pwr-regu", ++ .match_table = pwr_regu_match_table, ++ .probe = stm32mp1_pwr_regu_probe, ++}; ++#endif /* CFG_EMBED_DTB */ +diff --git a/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_pwr.h b/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_pwr.h +index 9cedd9321..cba6f1cc9 100644 +--- a/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_pwr.h ++++ b/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_pwr.h +@@ -1,11 +1,13 @@ + /* SPDX-License-Identifier: BSD-3-Clause */ + /* +- * Copyright (c) 2018-2019, STMicroelectronics ++ * Copyright (c) 2018-2021, STMicroelectronics + */ + + #ifndef __STM32MP1_PWR_H + #define __STM32MP1_PWR_H + ++#include ++#include + #include + #include + +@@ -13,10 +15,46 @@ + #define PWR_CR2_OFF 0x08 + #define PWR_CR3_OFF 0x0c + #define PWR_MPUCR_OFF 0x10 ++#define PWR_MCUCR_OFF 0x14 + #define PWR_WKUPCR_OFF 0x20 + #define PWR_MPUWKUPENR_OFF 0x28 + +-#define PWR_OFFSET_MASK 0x3fUL ++#define PWR_OFFSET_MASK GENMASK_32(5, 0) ++ ++#define PWR_CR1_LPDS BIT(0) ++#define PWR_CR1_LPCFG BIT(1) ++#define PWR_CR1_LVDS BIT(2) ++#define PWR_CR1_STOP2 BIT(3) ++#define PWR_CR1_DBP BIT(8) ++#define PWR_CR1_MPU_RAM_LOW_SPEED BIT(9) ++ ++#define PWR_CR2_BREN BIT(0) ++#define PWR_CR2_RREN BIT(1) ++#define PWR_CR2_BRRDY BIT(16) ++#define PWR_CR2_RRRDY BIT(17) ++ ++#define PWR_CR3_VBE BIT(8) ++#define PWR_CR3_VBRS BIT(9) ++#define PWR_CR3_DDRSREN BIT(10) ++#define PWR_CR3_DDRSRDIS BIT(11) ++#define PWR_CR3_DDRRETEN BIT(12) ++#define PWR_CR3_USB33DEN BIT(24) ++#define PWR_CR3_REG18EN BIT(28) ++#define PWR_CR3_REG11EN BIT(30) ++ ++#define PWR_CR3_POPL_SHIFT 17 ++#define PWR_CR3_POPL_MASK GENMASK_32(21, 17) ++ ++#define PWR_MPUCR_PDDS BIT(0) ++#define PWR_MPUCR_CSTDBYDIS BIT(3) ++#define PWR_MPUCR_CSSF BIT(9) ++ ++#define PWR_MCUCR_PDDS BIT(0) ++ ++#define PWR_WKUPCR_MASK (GENMASK_32(27, 16) | \ ++ GENMASK_32(13, 8) | GENMASK_32(5, 0)) ++ ++#define PWR_MPUWKUPENR_MASK GENMASK_32(5, 0) + + enum pwr_regulator { + PWR_REG11 = 0, +@@ -31,4 +69,27 @@ unsigned int stm32mp1_pwr_regulator_mv(enum pwr_regulator id); + void stm32mp1_pwr_regulator_set_state(enum pwr_regulator id, bool enable); + bool stm32mp1_pwr_regulator_is_enabled(enum pwr_regulator id); + ++/* wakeup-pins irq chip */ ++enum pwr_wkup_pins { ++ PWR_WKUP_PIN1 = 0, ++ PWR_WKUP_PIN2, ++ PWR_WKUP_PIN3, ++ PWR_WKUP_PIN4, ++ PWR_WKUP_PIN5, ++ PWR_WKUP_PIN6, ++ PWR_NB_WAKEUPPINS ++}; ++ ++enum pwr_wkup_flags { ++ PWR_WKUP_FLAG_RISING = 0, ++ PWR_WKUP_FLAG_FALLING, ++}; ++ ++TEE_Result ++stm32mp1_pwr_itr_alloc_add(size_t it, itr_handler_t handler, uint32_t flags, ++ void *data, struct itr_handler **phdl); ++ ++void stm32mp1_pwr_itr_enable(size_t it); ++void stm32mp1_pwr_itr_disable(size_t it); ++ + #endif /*__STM32MP1_PWR_H*/ +diff --git a/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_pwr_irq.c b/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_pwr_irq.c +new file mode 100644 +index 000000000..92e1f0122 +--- /dev/null ++++ b/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_pwr_irq.c +@@ -0,0 +1,396 @@ ++// SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0+) ++/* ++ * Copyright (c) 2020-2021, STMicroelectronics ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#define VERBOSE_PWR FMSG ++ ++/* PWR Registers */ ++#define WKUPCR 0x20 ++#define WKUPFR 0x24 ++#define MPUWKUPENR 0x28 ++ ++/* WKUPCR bits definition */ ++#define WKUP_EDGE_SHIFT 8 ++#define WKUP_PULL_SHIFT 16 ++#define WKUP_PULL_MASK GENMASK_32(1, 0) ++ ++enum wkup_pull_setting { ++ WKUP_NO_PULL = 0, ++ WKUP_PULL_UP, ++ WKUP_PULL_DOWN, ++ WKUP_PULL_RESERVED ++}; ++ ++/* EXTI line number for PWR Wakeup pin 1 */ ++#define PWR_EXTI_WKUP1 55 ++ ++struct stm32_pwr_data { ++ vaddr_t base; ++ struct stm32_pinctrl_list *pinctrl_list; ++ struct itr_handler *hdl[PWR_NB_WAKEUPPINS]; ++ struct itr_handler *gic_hdl; ++}; ++ ++static struct stm32_pwr_data *pwr_data; ++ ++static enum itr_return pwr_it_threaded_handler(void) ++{ ++ struct stm32_pwr_data *priv = pwr_data; ++ uint32_t wkupfr = 0; ++ uint32_t wkupenr = 0; ++ uint32_t i = 0; ++ ++ VERBOSE_PWR(""); ++ ++ wkupfr = io_read32(priv->base + WKUPFR); ++ wkupenr = io_read32(priv->base + MPUWKUPENR); ++ ++ for (i = 0; i < PWR_NB_WAKEUPPINS; i++) { ++ if ((wkupfr & BIT(i)) && (wkupenr & BIT(i))) { ++ VERBOSE_PWR("handle wkup irq:%d\n", i); ++ ++ if (priv->hdl[i]) { ++ struct itr_handler *h = priv->hdl[i]; ++ ++ if (h->handler(h) != ITRR_HANDLED) { ++ EMSG("Disabling unhandled interrupt %u", ++ i); ++ stm32mp1_pwr_itr_disable(i); ++ } ++ } ++ ++ /* Ack IRQ */ ++ io_setbits32(priv->base + WKUPCR, BIT(i)); ++ } ++ } ++ ++ itr_enable(priv->gic_hdl->it); ++ ++ return ITRR_HANDLED; ++} ++ ++static void yielding_stm32_pwr_notif(struct notif_driver *ndrv __unused, ++ enum notif_event ev) ++{ ++ switch (ev) { ++ case NOTIF_EVENT_DO_BOTTOM_HALF: ++ VERBOSE_PWR("PWR Bottom half"); ++ pwr_it_threaded_handler(); ++ break; ++ case NOTIF_EVENT_STOPPED: ++ VERBOSE_PWR("PWR notif stopped"); ++ break; ++ default: ++ EMSG("Unknown event %d", (int)ev); ++ panic(); ++ } ++} ++ ++struct notif_driver stm32_pwr_notif = { ++ .yielding_cb = yielding_stm32_pwr_notif, ++}; ++ ++static enum itr_return pwr_it_handler(struct itr_handler *handler) ++{ ++ struct stm32_pwr_data *priv = (struct stm32_pwr_data *)handler->data; ++ ++ VERBOSE_PWR(""); ++ ++ itr_disable(priv->gic_hdl->it); ++ ++ if (notif_async_is_started()) ++ notif_send_async(NOTIF_VALUE_DO_BOTTOM_HALF); ++ else ++ return pwr_it_threaded_handler(); ++ ++ return ITRR_HANDLED; ++} ++ ++static TEE_Result ++stm32_pwr_irq_set_pull_config(size_t it, enum wkup_pull_setting config) ++{ ++ struct stm32_pwr_data *priv = pwr_data; ++ ++ VERBOSE_PWR("irq:%zu pull config:0%#"PRIx32, it, config); ++ ++ if (config >= WKUP_PULL_RESERVED) { ++ EMSG("bad irq pull config"); ++ return TEE_ERROR_GENERIC; ++ } ++ ++ io_mask32(priv->base + WKUPCR, ++ (config & WKUP_PULL_MASK) << (WKUP_PULL_SHIFT + it * 2), ++ (WKUP_PULL_MASK) << (WKUP_PULL_SHIFT + it * 2)); ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result ++stm32_pwr_irq_set_trig(size_t it, enum pwr_wkup_flags trig) ++{ ++ struct stm32_pwr_data *priv = pwr_data; ++ uint32_t wkupcr = 0; ++ int en = 0; ++ ++ VERBOSE_PWR("irq:%zu trig:%#"PRIx32, it, trig); ++ ++ en = io_read32(priv->base + MPUWKUPENR) & BIT(it); ++ /* ++ * Reference manual request to disable the wakeup pin while ++ * changing the edge detection setting ++ */ ++ if (en) ++ stm32mp1_pwr_itr_disable(it); ++ ++ wkupcr = io_read32(priv->base + WKUPCR); ++ switch (trig) { ++ case PWR_WKUP_FLAG_FALLING: ++ wkupcr |= BIT(WKUP_EDGE_SHIFT + it); ++ break; ++ case PWR_WKUP_FLAG_RISING: ++ wkupcr &= ~BIT(WKUP_EDGE_SHIFT + it); ++ break; ++ default: ++ panic("Bad edge configuration"); ++ } ++ ++ io_write32(priv->base + WKUPCR, wkupcr); ++ ++ if (en) ++ stm32mp1_pwr_itr_enable(it); ++ ++ return TEE_SUCCESS; ++} ++ ++void stm32mp1_pwr_itr_enable(size_t it) ++{ ++ struct stm32_pwr_data *priv = pwr_data; ++ ++ VERBOSE_PWR("Pwr irq enable"); ++ ++ if (IS_ENABLED(CFG_STM32_EXTI)) ++ stm32_exti_enable_wake(PWR_EXTI_WKUP1 + it); ++ ++ io_setbits32(priv->base + MPUWKUPENR, BIT(it)); ++} ++ ++void stm32mp1_pwr_itr_disable(size_t it) ++{ ++ struct stm32_pwr_data *priv = pwr_data; ++ ++ VERBOSE_PWR("Pwr irq disable"); ++ ++ io_clrbits32(priv->base + MPUWKUPENR, BIT(it)); ++ ++ if (IS_ENABLED(CFG_STM32_EXTI)) ++ stm32_exti_disable_wake(PWR_EXTI_WKUP1 + it); ++} ++ ++static TEE_Result stm32mp1_pwr_irt_add(struct itr_handler *hdl) ++{ ++ struct stm32_pwr_data *priv = pwr_data; ++ int it = hdl->it; ++ struct stm32_pinctrl_list pinctrl_list = { }; ++ struct stm32_pinctrl *pinctrl = NULL; ++ unsigned int i = 0; ++ ++ VERBOSE_PWR("Pwr IRQ add"); ++ ++ if (!priv) { ++ DMSG("Pwr IRQs not yet initialized"); ++ return TEE_ERROR_DEFER_DRIVER_INIT; ++ } ++ ++ assert(it >= PWR_WKUP_PIN1 && it < PWR_NB_WAKEUPPINS); ++ /* check IRQ not already in use */ ++ assert(!priv->hdl[it]); ++ ++ priv->hdl[it] = hdl; ++ ++ STAILQ_FOREACH(pinctrl, priv->pinctrl_list, link) { ++ if ((unsigned int)it == i) ++ break; ++ ++ i++; ++ } ++ assert(pinctrl); ++ ++ STAILQ_INIT(&pinctrl_list); ++ STAILQ_INSERT_HEAD(&pinctrl_list, pinctrl, link); ++ ++ stm32mp1_pwr_itr_disable(it); ++ ++ stm32_pinctrl_load_config(&pinctrl_list); ++ ++ VERBOSE_PWR("Wake-up pin on bank=%"PRIu8" pin=%"PRIu8, ++ pinctrl->bank, pinctrl->pin); ++ ++ /* use the same pull up configuration than for the gpio */ ++ switch (pinctrl->config.pupd) { ++ case GPIO_PUPD_NO_PULL: ++ break; ++ case GPIO_PUPD_PULL_UP: ++ stm32_pwr_irq_set_pull_config(it, WKUP_PULL_UP); ++ break; ++ case GPIO_PUPD_PULL_DOWN: ++ stm32_pwr_irq_set_pull_config(it, WKUP_PULL_DOWN); ++ break; ++ default: ++ panic(); ++ } ++ ++ stm32_pwr_irq_set_trig(it, hdl->flags); ++ ++ return TEE_SUCCESS; ++} ++ ++TEE_Result ++stm32mp1_pwr_itr_alloc_add(size_t it, itr_handler_t handler, uint32_t flags, ++ void *data, struct itr_handler **phdl) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ struct itr_handler *hdl = NULL; ++ ++ assert(!(flags & ITRF_SHARED)); ++ ++ hdl = calloc(1, sizeof(*hdl)); ++ if (!hdl) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ hdl->it = it; ++ hdl->handler = handler; ++ hdl->flags = flags; ++ hdl->data = data; ++ ++ res = stm32mp1_pwr_irt_add(hdl); ++ if (res) { ++ free(hdl); ++ return res; ++ } ++ ++ *phdl = hdl; ++ ++ return res; ++} ++ ++static TEE_Result ++stm32mp1_pwr_irq_probe(const void *fdt, int node, ++ const void *compat_data __unused) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ struct stm32_pwr_data *priv = NULL; ++ struct stm32_pinctrl *pinctrl = NULL; ++ size_t count = 0; ++ ++ VERBOSE_PWR("Init PWR IRQ"); ++ ++ pwr_data = calloc(1, sizeof(*pwr_data)); ++ if (!pwr_data) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ priv = pwr_data; ++ priv->base = stm32_pwr_base(); ++ ++ res = stm32_pinctrl_dt_get_by_index(fdt, node, 0, &priv->pinctrl_list); ++ if (res) ++ goto err; ++ ++ STAILQ_FOREACH(pinctrl, priv->pinctrl_list, link) ++ count++; ++ ++ if (count != PWR_NB_WAKEUPPINS) { ++ res = TEE_ERROR_BAD_PARAMETERS; ++ EMSG("Missing pinctrl description"); ++ goto err; ++ } ++ ++ priv->gic_hdl = itr_alloc_add(GIC_MPU_WAKEUP_PIN, pwr_it_handler, 0, ++ priv); ++ if (!priv->gic_hdl) ++ panic("Could not get wake-up pin IRQ"); ++ ++ itr_enable(priv->gic_hdl->it); ++ ++ if (IS_ENABLED(CFG_CORE_ASYNC_NOTIF)) ++ notif_register_driver(&stm32_pwr_notif); ++ ++ VERBOSE_PWR("Init pwr irq done"); ++ ++ return TEE_SUCCESS; ++err: ++ free(pwr_data); ++ ++ return res; ++} ++ ++static const struct dt_device_match pwr_irq_match_table[] = { ++ { .compatible = "st,stm32mp1,pwr-irq" }, ++ { } ++}; ++ ++DEFINE_DT_DRIVER(stm32mp1_pwr_irq_dt_driver) = { ++ .name = "stm32mp1-pwr-irq", ++ .match_table = pwr_irq_match_table, ++ .probe = &stm32mp1_pwr_irq_probe, ++}; ++ ++static enum itr_return pwr_it_user_handler(struct itr_handler *handler __unused) ++{ ++ VERBOSE_PWR("pwr irq tester handler"); ++ ++ return ITRR_HANDLED; ++} ++ ++static TEE_Result ++stm32mp1_pwr_irq_user_dt_probe(const void *fdt, int node, ++ const void *compat_data __unused) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ struct itr_handler *hdl = NULL; ++ const fdt32_t *cuint = NULL; ++ size_t it = 0; ++ ++ VERBOSE_PWR("Init pwr irq user"); ++ ++ cuint = fdt_getprop(fdt, node, "st,wakeup-pin-number", NULL); ++ if (!cuint) ++ panic("Missing wake-up pin number"); ++ ++ it = fdt32_to_cpu(*cuint) - 1U; ++ ++ res = stm32mp1_pwr_itr_alloc_add(it, pwr_it_user_handler, ++ PWR_WKUP_FLAG_FALLING, NULL, &hdl); ++ if (res != TEE_SUCCESS) ++ return res; ++ ++ stm32mp1_pwr_itr_enable(hdl->it); ++ ++ return TEE_SUCCESS; ++} ++ ++static const struct dt_device_match pwr_irq_test_match_table[] = { ++ { .compatible = "st,stm32mp1,pwr-irq-user" }, ++ { } ++}; ++ ++DEFINE_DT_DRIVER(stm32mp1_pwr_irq_dt_tester) = { ++ .name = "stm32mp1-pwr-irq-user", ++ .match_table = pwr_irq_test_match_table, ++ .probe = &stm32mp1_pwr_irq_user_dt_probe, ++}; +diff --git a/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_rcc.c b/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_rcc.c +index 7a5f492db..f48b9ac22 100644 +--- a/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_rcc.c ++++ b/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_rcc.c +@@ -3,7 +3,11 @@ + * Copyright (c) 2018-2019, STMicroelectronics + */ + ++#ifdef CFG_STM32MP13 ++#include ++#else /* assume CFG_STM32MP15 */ + #include ++#endif + #include + #include + #include +@@ -57,7 +61,12 @@ TEE_Result stm32_reset_assert(unsigned int id, unsigned int to_us) + + TEE_Result stm32_reset_deassert(unsigned int id, unsigned int to_us) + { ++#ifdef CFG_STM32MP13 ++ size_t offset = reset_id2reg_offset(id) + RCC_RSTCLRR_OFFSET; ++#endif ++#ifdef CFG_STM32MP15 + size_t offset = reset_id2reg_offset(id) + RCC_MP_RSTCLRR_OFFSET; ++#endif + uint32_t bitmsk = BIT(reset_id2reg_bit_pos(id)); + vaddr_t rcc_base = stm32_rcc_base(); + +@@ -77,6 +86,7 @@ TEE_Result stm32_reset_deassert(unsigned int id, unsigned int to_us) + return TEE_SUCCESS; + } + ++#ifdef CFG_STM32MP15 + void stm32_reset_assert_deassert_mcu(bool assert_not_deassert) + { + vaddr_t rcc_base = stm32_rcc_base(); +@@ -91,3 +101,11 @@ void stm32_reset_assert_deassert_mcu(bool assert_not_deassert) + else + io_setbits32(rcc_base + RCC_MP_GCR, RCC_MP_GCR_BOOT_MCU); + } ++#endif ++ ++void stm32_reset_system(void) ++{ ++ vaddr_t rcc = stm32_rcc_base(); ++ ++ io_write32(rcc + RCC_MP_GRSTCSETR, RCC_MP_GRSTCSETR_MPSYSRST); ++} +diff --git a/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_syscfg.c b/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_syscfg.c +index 9b923d4bb..b795fbade 100644 +--- a/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_syscfg.c ++++ b/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_syscfg.c +@@ -1,11 +1,17 @@ + // SPDX-License-Identifier: BSD-2-Clause + /* +- * Copyright (c) 2019-2020, STMicroelectronics ++ * Copyright (c) 2019-2021, STMicroelectronics + */ + +-#include ++#include ++#include ++#include ++#include ++#include ++#include + #include + #include ++#include + #include + #include + #include +@@ -15,8 +21,41 @@ + /* + * SYSCFG register offsets (base relative) + */ +-#define SYSCFG_CMPCR 0x20U +-#define SYSCFG_CMPENSETR 0x24U ++#define SYSCFG_SRAM3ERASER U(0x10) ++#define SYSCFG_SRAM3KR U(0x14) ++#define SYSCFG_IOCTRLSETR U(0x18) ++#define SYSCFG_CMPCR U(0x20) ++#define SYSCFG_CMPENSETR U(0x24) ++#define SYSCFG_CMPENCLRR U(0x28) ++#define SYSCFG_CMPSD1CR U(0x30) ++#define SYSCFG_CMPSD1ENSETR U(0x34) ++#define SYSCFG_CMPSD1ENCLRR U(0x38) ++#define SYSCFG_CMPSD2CR U(0x40) ++#define SYSCFG_CMPSD2ENSETR U(0x44) ++#define SYSCFG_CMPSD2ENCLRR U(0x48) ++#define SYSCFG_HSLVEN0R U(0x50) ++#define SYSCFG_IDC U(0x380) ++#define SYSCFG_IOSIZE U(0x400) ++ ++/* ++ * SYSCFG_SRAM3ERASE Register ++ */ ++#define SYSCFG_SRAM3KR_KEY1 U(0xCA) ++#define SYSCFG_SRAM3KR_KEY2 U(0x53) ++ ++#define SYSCFG_SRAM3ERASER_SRAM3EO BIT(1) ++#define SYSCFG_SRAM3ERASER_SRAM3ER BIT(0) ++ ++#define SYSCFG_SRAM3ERASE_TIMEOUT_US U(1000) ++ ++/* ++ * SYSCFG_IOCTRLSETR Register ++ */ ++#define SYSCFG_IOCTRLSETR_HSLVEN_TRACE BIT(0) ++#define SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI BIT(1) ++#define SYSCFG_IOCTRLSETR_HSLVEN_ETH BIT(2) ++#define SYSCFG_IOCTRLSETR_HSLVEN_SDMMC BIT(3) ++#define SYSCFG_IOCTRLSETR_HSLVEN_SPI BIT(4) + + /* + * SYSCFG_CMPCR Register +@@ -24,76 +63,297 @@ + #define SYSCFG_CMPCR_SW_CTRL BIT(1) + #define SYSCFG_CMPCR_READY BIT(8) + #define SYSCFG_CMPCR_RANSRC GENMASK_32(19, 16) +-#define SYSCFG_CMPCR_RANSRC_SHIFT 16 ++#define SYSCFG_CMPCR_RANSRC_SHIFT U(16) + #define SYSCFG_CMPCR_RAPSRC GENMASK_32(23, 20) +-#define SYSCFG_CMPCR_ANSRC_SHIFT 24 ++#define SYSCFG_CMPCR_ANSRC_SHIFT U(24) + +-#define SYSCFG_CMPCR_READY_TIMEOUT_US 1000U ++#define SYSCFG_CMPCR_READY_TIMEOUT_US U(1000) ++ ++#define CMPENSETR_OFFSET U(0x4) ++#define CMPENCLRR_OFFSET U(0x8) + + /* + * SYSCFG_CMPENSETR Register + */ + #define SYSCFG_CMPENSETR_MPU_EN BIT(0) + ++/* ++ * SYSCFG_IDC Register ++ */ ++#define SYSCFG_IDC_DEV_ID_MASK GENMASK_32(11, 0) ++#define SYSCFG_IDC_REV_ID_MASK GENMASK_32(31, 16) ++#define SYSCFG_IDC_REV_ID_SHIFT U(16) ++ ++/* ++ * HSLV definitions ++ */ ++#define SYSCFG_HSLV_IDX_TPIU U(0) ++#define SYSCFG_HSLV_IDX_QSPI U(1) ++#define SYSCFG_HSLV_IDX_ETH1 U(2) ++#define SYSCFG_HSLV_IDX_ETH2 U(3) ++#define SYSCFG_HSLV_IDX_SDMMC1 U(4) ++#define SYSCFG_HSLV_IDX_SDMMC2 U(5) ++#define SYSCFG_HSLV_IDX_SPI1 U(6) ++#define SYSCFG_HSLV_IDX_SPI2 U(7) ++#define SYSCFG_HSLV_IDX_SPI3 U(8) ++#define SYSCFG_HSLV_IDX_SPI4 U(9) ++#define SYSCFG_HSLV_IDX_SPI5 U(10) ++#define SYSCFG_HSLV_IDX_LTDC U(11) ++#define SYSCFG_HSLV_NB_IDX U(12) ++ ++#define SYSCFG_HSLV_KEY U(0x1018) ++ ++static bool vdd_low_voltage; ++ + static vaddr_t get_syscfg_base(void) + { +- struct io_pa_va base = { .pa = SYSCFG_BASE }; ++ static struct io_pa_va base = { .pa = SYSCFG_BASE }; + +- return io_pa_or_va(&base, 1); ++ return io_pa_or_va(&base, SYSCFG_IOSIZE); + } + +-void stm32mp_syscfg_enable_io_compensation(void) ++uint32_t stm32mp_syscfg_get_chip_dev_id(void) ++{ ++ if (IS_ENABLED(CFG_STM32MP13)) ++ return io_read32(get_syscfg_base() + SYSCFG_IDC) & ++ SYSCFG_IDC_DEV_ID_MASK; ++ ++ return 0; ++} ++ ++static void enable_io_compensation(int cmpcr_offset) + { +- vaddr_t syscfg_base = get_syscfg_base(); ++ vaddr_t cmpcr_base = get_syscfg_base() + cmpcr_offset; ++ struct clk *csi_clk = stm32mp_rcc_clock_id_to_clk(CK_CSI); ++ struct clk *syscfg_clk = stm32mp_rcc_clock_id_to_clk(SYSCFG); + uint64_t timeout_ref = 0; + +- stm32_clock_enable(CK_CSI); +- stm32_clock_enable(SYSCFG); ++ clk_enable(csi_clk); ++ clk_enable(syscfg_clk); ++ ++ if (io_read32(cmpcr_base) & SYSCFG_CMPCR_READY) ++ return; + +- io_setbits32(syscfg_base + SYSCFG_CMPENSETR, SYSCFG_CMPENSETR_MPU_EN); ++ io_setbits32(cmpcr_base + CMPENSETR_OFFSET, SYSCFG_CMPENSETR_MPU_EN); + + timeout_ref = timeout_init_us(SYSCFG_CMPCR_READY_TIMEOUT_US); + +- while (!(io_read32(syscfg_base + SYSCFG_CMPCR) & SYSCFG_CMPCR_READY)) ++ while (!(io_read32(cmpcr_base) & SYSCFG_CMPCR_READY)) + if (timeout_elapsed(timeout_ref)) { + EMSG("IO compensation cell not ready"); + /* Allow an almost silent failure here */ + break; + } + +- io_clrbits32(syscfg_base + SYSCFG_CMPCR, SYSCFG_CMPCR_SW_CTRL); ++ io_clrbits32(cmpcr_base, SYSCFG_CMPCR_SW_CTRL); + +- DMSG("SYSCFG.cmpcr = %#"PRIx32, io_read32(syscfg_base + SYSCFG_CMPCR)); ++ DMSG("SYSCFG.cmpcr = %#"PRIx32, io_read32(cmpcr_base)); + } + +-void stm32mp_syscfg_disable_io_compensation(void) ++static void disable_io_compensation(int cmpcr_offset) ++{ ++ vaddr_t cmpcr_base = get_syscfg_base() + cmpcr_offset; ++ struct clk *csi_clk = stm32mp_rcc_clock_id_to_clk(CK_CSI); ++ struct clk *syscfg_clk = stm32mp_rcc_clock_id_to_clk(SYSCFG); ++ uint32_t value_cmpcr = 0; ++ uint32_t value_cmpcr2 = 0; ++ ++ assert(csi_clk && syscfg_clk); ++ ++ /* No refcount balance needed on non-secure SYSCFG clock */ ++ clk_enable(syscfg_clk); ++ ++ value_cmpcr = io_read32(cmpcr_base); ++ value_cmpcr2 = io_read32(cmpcr_base + CMPENSETR_OFFSET); ++ if (!(value_cmpcr & SYSCFG_CMPCR_READY && ++ value_cmpcr2 & SYSCFG_CMPENSETR_MPU_EN)) ++ return; ++ ++ value_cmpcr = io_read32(cmpcr_base) >> SYSCFG_CMPCR_ANSRC_SHIFT; ++ ++ io_clrbits32(cmpcr_base, SYSCFG_CMPCR_RANSRC | SYSCFG_CMPCR_RAPSRC); ++ ++ value_cmpcr <<= SYSCFG_CMPCR_RANSRC_SHIFT; ++ value_cmpcr |= io_read32(cmpcr_base); ++ ++ io_write32(cmpcr_base, value_cmpcr | SYSCFG_CMPCR_SW_CTRL); ++ ++ DMSG("SYSCFG.cmpcr = %#"PRIx32, io_read32(cmpcr_base)); ++ ++ io_setbits32(cmpcr_base + CMPENCLRR_OFFSET, SYSCFG_CMPENSETR_MPU_EN); ++ ++ clk_disable(syscfg_clk); ++ clk_disable(csi_clk); ++} ++ ++TEE_Result stm32mp_syscfg_erase_sram3(void) + { +- vaddr_t syscfg_base = get_syscfg_base(); +- uint32_t value = 0; ++ vaddr_t base = get_syscfg_base(); ++ uint64_t timeout_ref = 0; + +- value = io_read32(syscfg_base + SYSCFG_CMPCR) >> +- SYSCFG_CMPCR_ANSRC_SHIFT; ++ if (!IS_ENABLED(CFG_STM32MP13)) ++ return TEE_ERROR_NOT_SUPPORTED; + +- io_clrbits32(syscfg_base + SYSCFG_CMPCR, +- SYSCFG_CMPCR_RANSRC | SYSCFG_CMPCR_RAPSRC); ++ /* Unlock SYSCFG_SRAM3ERASER_SRAM3ER */ ++ io_write32(base + SYSCFG_SRAM3KR, SYSCFG_SRAM3KR_KEY1); ++ io_write32(base + SYSCFG_SRAM3KR, SYSCFG_SRAM3KR_KEY2); + +- value = io_read32(syscfg_base + SYSCFG_CMPCR) | +- (value << SYSCFG_CMPCR_RANSRC_SHIFT); ++ /* Request SRAM3 erase */ ++ io_setbits32(base + SYSCFG_SRAM3ERASER, SYSCFG_SRAM3ERASER_SRAM3ER); + +- io_write32(syscfg_base + SYSCFG_CMPCR, value | SYSCFG_CMPCR_SW_CTRL); ++ /* Lock SYSCFG_SRAM3ERASER_SRAM3ER */ ++ io_write32(base + SYSCFG_SRAM3KR, 0); + +- DMSG("SYSCFG.cmpcr = %#"PRIx32, io_read32(syscfg_base + SYSCFG_CMPCR)); ++ /* Wait end of SRAM3 erase */ ++ timeout_ref = timeout_init_us(SYSCFG_SRAM3ERASE_TIMEOUT_US); ++ while (io_read32(base + SYSCFG_SRAM3ERASER) & ++ SYSCFG_SRAM3ERASER_SRAM3EO) { ++ if (timeout_elapsed(timeout_ref)) ++ break; ++ } + +- io_clrbits32(syscfg_base + SYSCFG_CMPENSETR, SYSCFG_CMPENSETR_MPU_EN); ++ /* Timeout may append due to a schedule after the while(test) */ ++ if (io_read32(base + SYSCFG_SRAM3ERASER) & SYSCFG_SRAM3ERASER_SRAM3EO) ++ return TEE_ERROR_BUSY; + +- stm32_clock_disable(SYSCFG); +- stm32_clock_disable(CK_CSI); ++ return TEE_SUCCESS; ++} ++ ++void stm32mp_syscfg_enable_io_compensation(void) ++{ ++ enable_io_compensation(SYSCFG_CMPCR); ++ ++ if (IS_ENABLED(CFG_STM32MP13)) { ++ enable_io_compensation(SYSCFG_CMPSD1CR); ++ enable_io_compensation(SYSCFG_CMPSD2CR); ++ } ++} ++ ++void stm32mp_syscfg_disable_io_compensation(void) ++{ ++ disable_io_compensation(SYSCFG_CMPCR); ++ ++ if (IS_ENABLED(CFG_STM32MP13)) { ++ disable_io_compensation(SYSCFG_CMPSD1CR); ++ disable_io_compensation(SYSCFG_CMPSD2CR); ++ } + } + + static TEE_Result stm32mp1_iocomp(void) + { +- stm32mp_syscfg_enable_io_compensation(); ++ stm32mp_syscfg_enable_io_comp(); + + return TEE_SUCCESS; + } + driver_init(stm32mp1_iocomp); ++ ++static void enable_hslv_by_index(uint32_t index) ++{ ++ assert(index < SYSCFG_HSLV_NB_IDX); ++ ++ switch (index) { ++ case SYSCFG_HSLV_IDX_SDMMC1: ++ case SYSCFG_HSLV_IDX_SDMMC2: ++ DMSG("Not managing SDMMC HSLV"); ++ break; ++ default: ++ io_write32(get_syscfg_base() + SYSCFG_HSLVEN0R + ++ index * sizeof(uint32_t), SYSCFG_HSLV_KEY); ++ break; ++ } ++} ++ ++static void enable_high_speed_mode_low_voltage(void) ++{ ++ if (IS_ENABLED(CFG_STM32MP13_CFG)) { ++ unsigned int idx = 0; ++ ++ for (idx = 0; idx < SYSCFG_HSLV_NB_IDX; idx++) ++ enable_hslv_by_index(idx); ++ } else { ++ io_write32(get_syscfg_base() + SYSCFG_IOCTRLSETR, ++ SYSCFG_IOCTRLSETR_HSLVEN_TRACE | ++ SYSCFG_IOCTRLSETR_HSLVEN_QUADSPI | ++ SYSCFG_IOCTRLSETR_HSLVEN_ETH | ++ SYSCFG_IOCTRLSETR_HSLVEN_SDMMC | ++ SYSCFG_IOCTRLSETR_HSLVEN_SPI); ++ } ++} ++ ++static TEE_Result syscfg_hslv_pm(enum pm_op op, uint32_t pm_hint __unused, ++ const struct pm_callback_handle ++ *pm_handle __unused) ++{ ++ if (op == PM_OP_RESUME && vdd_low_voltage) ++ enable_high_speed_mode_low_voltage(); ++ ++ return TEE_SUCCESS; ++} ++DECLARE_KEEP_PAGER(syscfg_hslv_pm); ++ ++static TEE_Result stm32mp_syscfg_set_hslv(void) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ uint32_t otp_value = 0; ++ uint32_t otp_id = 0; ++ uint16_t vdd_voltage = 0; ++ bool product_below_2v5 = false; ++ struct rdev *regu = NULL; ++ ++ /* ++ * High Speed Low Voltage Pad mode Enable for SPI, SDMMC, ETH, QSPI ++ * and TRACE. Needed above ~50MHz and conditioned by AFMUX selection. ++ * It could be disabled for low frequencies or if AFMUX is selected ++ * but the function is not used, typically for TRACE. ++ * If high speed low voltage pad mode is enabled, platform will ++ * over consume. ++ * ++ * WARNING: ++ * Enabling High Speed mode while VDD > 2.7V ++ * with the OTP product_below_2v5 (OTP 18, BIT 13) ++ * erroneously set to 1 can damage the SoC! ++ * => TF-A enables the low power mode only if VDD < 2.7V (in DT) ++ * but this value needs to be consistent with board design. ++ */ ++ res = stm32_bsec_find_otp_in_nvmem_layout("hw2_otp", &otp_id, NULL); ++ if (res) ++ panic(); ++ ++ res = stm32_bsec_read_otp(&otp_value, otp_id); ++ if (res) ++ panic(); ++ ++ product_below_2v5 = ((otp_value & HW2_OTP_PRODUCT_BELOW_2V5) != 0); ++ ++ /* Get VDD supply */ ++ regu = regulator_get_by_regulator_name("vdd"); ++ if (!regu) ++ panic("VDD regulator not found"); ++ ++ res = regulator_get_voltage(regu, &vdd_voltage); ++ if (res || !vdd_voltage) ++ panic("VDD get voltage error"); ++ ++ /* Check if VDD is Low Voltage */ ++ if (vdd_voltage < 2700U) { ++ vdd_low_voltage = true; ++ enable_high_speed_mode_low_voltage(); ++ ++ if (!product_below_2v5) ++ IMSG("Product_below_2v5=0: HSLVEN protected by HW\n"); ++ ++ } else { ++ if (product_below_2v5) { ++ EMSG("Product_below_2v5=1:"); ++ EMSG("\tHSLVEN update is destructive,"); ++ EMSG("\tno update as VDD > 2.7V"); ++ panic(); ++ } ++ } ++ ++ register_pm_driver_cb(syscfg_hslv_pm, NULL, "stm32mp1-hslv-driver"); ++ ++ return TEE_SUCCESS; ++} ++ ++driver_init_late(stm32mp_syscfg_set_hslv); +diff --git a/core/arch/arm/plat-stm32mp1/drivers/sub.mk b/core/arch/arm/plat-stm32mp1/drivers/sub.mk +index a97bba4f0..990ab5d77 100644 +--- a/core/arch/arm/plat-stm32mp1/drivers/sub.mk ++++ b/core/arch/arm/plat-stm32mp1/drivers/sub.mk +@@ -1,4 +1,7 @@ ++srcs-$(CFG_STM32_CLKCALIB) += stm32mp1_calib.c ++srcs-$(CFG_DDR_LOWPOWER) += stm32mp1_ddrc.c + srcs-$(CFG_STPMIC1) += stm32mp1_pmic.c + srcs-y += stm32mp1_pwr.c ++srcs-y += stm32mp1_pwr_irq.c + srcs-y += stm32mp1_rcc.c +-srcs-y += stm32mp1_syscfg.c ++srcs-$(CFG_SYSCFG) += stm32mp1_syscfg.c +diff --git a/core/arch/arm/plat-stm32mp1/link.mk b/core/arch/arm/plat-stm32mp1/link.mk +index 01a9b8ed5..fa1465a45 100644 +--- a/core/arch/arm/plat-stm32mp1/link.mk ++++ b/core/arch/arm/plat-stm32mp1/link.mk +@@ -1,5 +1,6 @@ + include core/arch/arm/kernel/link.mk + ++ifeq ($(CFG_STM32MP15x_STM32IMAGE),y) + # Create stm32 formatted images from the native binary images + + define stm32image_cmd +@@ -22,3 +23,4 @@ all: $(link-out-dir)/tee-pageable_v2.stm32 + cleanfiles += $(link-out-dir)/tee-pageable_v2.stm32 + $(link-out-dir)/tee-pageable_v2.stm32: $(link-out-dir)/tee-pageable_v2.bin + $(stm32image_cmd) --source $< --dest $@ --bintype 0x22 ++endif +diff --git a/core/arch/arm/plat-stm32mp1/main.c b/core/arch/arm/plat-stm32mp1/main.c +index e6d97041a..eecd4fe0b 100644 +--- a/core/arch/arm/plat-stm32mp1/main.c ++++ b/core/arch/arm/plat-stm32mp1/main.c +@@ -7,17 +7,28 @@ + #include + #include + #include ++#include + #include ++#include + #include +-#include ++#include ++#include ++#include ++#include + #include +-#include ++#include ++#include ++#include ++#include ++#include + #include + #include + #include + #include + #include + #include ++#include ++#include + #include + #include + #include +@@ -32,14 +43,34 @@ register_phys_mem_pgdir(MEM_AREA_IO_NSEC, APB5_BASE, APB5_SIZE); + register_phys_mem_pgdir(MEM_AREA_IO_NSEC, AHB4_BASE, AHB4_SIZE); + register_phys_mem_pgdir(MEM_AREA_IO_NSEC, AHB5_BASE, AHB5_SIZE); + +-register_phys_mem_pgdir(MEM_AREA_IO_SEC, APB3_BASE, APB4_SIZE); ++register_phys_mem_pgdir(MEM_AREA_IO_SEC, APB3_BASE, APB3_SIZE); ++register_phys_mem_pgdir(MEM_AREA_IO_SEC, APB4_BASE, APB4_SIZE); + register_phys_mem_pgdir(MEM_AREA_IO_SEC, APB5_BASE, APB5_SIZE); ++#ifdef CFG_STM32MP13 ++register_phys_mem_pgdir(MEM_AREA_IO_SEC, APB6_BASE, APB6_SIZE); ++#endif + register_phys_mem_pgdir(MEM_AREA_IO_SEC, AHB4_BASE, AHB4_SIZE); + register_phys_mem_pgdir(MEM_AREA_IO_SEC, AHB5_BASE, AHB5_SIZE); + register_phys_mem_pgdir(MEM_AREA_IO_SEC, GIC_BASE, GIC_SIZE); + + register_ddr(DDR_BASE, CFG_DRAM_SIZE); + ++#ifdef CFG_RPROC_PTA ++/* Map MCU RETRAM as read write for Cortex-M4 firmware management */ ++register_phys_mem(MEM_AREA_IO_SEC, RETRAM_BASE, RETRAM_SIZE); ++ ++/* Map MCU SRAM as read write for Cortex-M4 firmware management */ ++register_phys_mem(MEM_AREA_IO_SEC, MCUSRAM_BASE, MCUSRAM_SIZE); ++#endif ++ ++/* Map non-secure DDR bottom for the low power sequence */ ++register_phys_mem(MEM_AREA_RAM_NSEC, DDR_BASE, SMALL_PAGE_SIZE); ++ ++#ifdef CFG_STM32MP15 ++/* Map TEE physical RAM as read-only for content storage when suspending */ ++register_phys_mem(MEM_AREA_ROM_SEC, TEE_RAM_START, TEE_RAM_PH_SIZE); ++#endif /* CFG_STM32MP15 */ ++ + #define _ID2STR(id) (#id) + #define ID2STR(id) _ID2STR(id) + +@@ -144,6 +175,53 @@ static TEE_Result init_console_from_dt(void) + service_init_late(init_console_from_dt); + #endif + ++static uintptr_t stm32_dbgmcu_base(void) ++{ ++ static void *va; ++ ++ if (!cpu_mmu_enabled()) ++ return DBGMCU_BASE; ++ ++ if (!va) ++ va = phys_to_virt(DBGMCU_BASE, MEM_AREA_IO_SEC, 1); ++ ++ return (uintptr_t)va; ++} ++ ++/* SoC versioning util, returns default ID if can't access DBGMCU */ ++TEE_Result stm32mp1_dbgmcu_get_chip_version(uint32_t *chip_version) ++{ ++ uint32_t id = STM32MP1_CHIP_DEFAULT_VERSION; ++ ++ if (!chip_version) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ if (stm32_bsec_read_debug_conf() & BSEC_DBGSWGEN) ++ id = (io_read32(stm32_dbgmcu_base() + DBGMCU_IDC) & ++ DBGMCU_IDC_REV_ID_MASK) >> DBGMCU_IDC_REV_ID_SHIFT; ++ ++ *chip_version = id; ++ ++ return TEE_SUCCESS; ++} ++ ++/* SoC device ID util, returns default ID if can't access DBGMCU */ ++TEE_Result stm32mp1_dbgmcu_get_chip_dev_id(uint32_t *chip_dev_id) ++{ ++ uint32_t id = STM32MP1_CHIP_ID; ++ ++ if (!chip_dev_id) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ if (stm32_bsec_read_debug_conf() & BSEC_DBGSWGEN) ++ id = io_read32(stm32_dbgmcu_base() + DBGMCU_IDC) & ++ DBGMCU_IDC_DEV_ID_MASK; ++ ++ *chip_dev_id = id; ++ ++ return TEE_SUCCESS; ++} ++ + /* + * GIC init, used also for primary/secondary boot core wake completion + */ +@@ -173,24 +251,27 @@ void main_secondary_init_gic(void) + + static TEE_Result init_stm32mp1_drivers(void) + { ++ TEE_Result res = TEE_ERROR_GENERIC; ++ const struct stm32_firewall_cfg sec_cfg[] = { ++ { FWLL_SEC_RW | FWLL_MASTER(0) }, ++ { }, /* Null terminated */ ++ }; ++ + /* Without secure DTB support, some drivers must be inited */ + if (!IS_ENABLED(CFG_EMBED_DTB)) + stm32_etzpc_init(ETZPC_BASE); + +- /* Secure internal memories for the platform, once ETZPC is ready */ +- etzpc_configure_tzma(0, ETZPC_TZMA_ALL_SECURE); +- etzpc_lock_tzma(0); +- + COMPILE_TIME_ASSERT(((SYSRAM_BASE + SYSRAM_SIZE) <= CFG_TZSRAM_START) || + ((SYSRAM_BASE <= CFG_TZSRAM_START) && + (SYSRAM_SEC_SIZE >= CFG_TZSRAM_SIZE))); + +- etzpc_configure_tzma(1, SYSRAM_SEC_SIZE >> SMALL_PAGE_SHIFT); +- etzpc_lock_tzma(1); ++ res = stm32_firewall_set_config(SYSRAM_BASE, SYSRAM_SEC_SIZE, sec_cfg); ++ if (res) ++ panic("Unable to secure SYSRAM"); + + return TEE_SUCCESS; + } +-service_init_late(init_stm32mp1_drivers); ++driver_init_late(init_stm32mp1_drivers); + + vaddr_t get_gicc_base(void) + { +@@ -213,19 +294,6 @@ void stm32mp_get_bsec_static_cfg(struct stm32_bsec_static_cfg *cfg) + cfg->max_id = STM32MP1_OTP_MAX_ID; + } + +-bool stm32mp_is_closed_device(void) +-{ +- uint32_t otp = 0; +- TEE_Result result = TEE_ERROR_GENERIC; +- +- /* Non closed_device platform expects fuse well programmed to 0 */ +- result = stm32_bsec_shadow_read_otp(&otp, DATA0_OTP); +- if (!result && !(otp & BIT(DATA0_OTP_SECURED_POS))) +- return false; +- +- return true; +-} +- + bool __weak stm32mp_with_pmic(void) + { + return false; +@@ -264,50 +332,458 @@ vaddr_t stm32mp_bkpreg(unsigned int idx) + return bkpreg_base() + (idx * sizeof(uint32_t)); + } + +-#ifdef CFG_STM32_GPIO +-vaddr_t stm32_get_gpio_bank_base(unsigned int bank) ++vaddr_t stm32mp_bkpsram_base(void) + { +- static struct io_pa_va gpios_nsec_base = { .pa = GPIOS_NSEC_BASE }; +- static struct io_pa_va gpioz_base = { .pa = GPIOZ_BASE }; ++ struct io_pa_va base = { .pa = BKPSRAM_BASE }; + +- /* Get secure mapping address for GPIOZ */ +- if (bank == GPIO_BANK_Z) +- return io_pa_or_va_secure(&gpioz_base, GPIO_BANK_OFFSET); ++ return io_pa_or_va(&base, BKPSRAM_SIZE); ++} + +- COMPILE_TIME_ASSERT(GPIO_BANK_A == 0); +- assert(bank <= GPIO_BANK_K); ++vaddr_t stm32mp_stgen_base(void) ++{ ++ struct io_pa_va base = { .pa = STGEN_BASE }; ++ ++ return io_pa_or_va(&base, 1); ++} + +- return io_pa_or_va_nsec(&gpios_nsec_base, +- (bank + 1) * GPIO_BANK_OFFSET) + +- (bank * GPIO_BANK_OFFSET); ++static int get_chip_dev_id(uint32_t *dev_id) ++{ ++#ifdef CFG_STM32MP13 ++ *dev_id = stm32mp_syscfg_get_chip_dev_id(); ++#else /* assume CFG_STM32MP15 */ ++ if (stm32mp1_dbgmcu_get_chip_dev_id(dev_id) != TEE_SUCCESS) ++ return -1; ++#endif ++ return 0; + } + +-unsigned int stm32_get_gpio_bank_offset(unsigned int bank) ++static int get_part_number(uint32_t *part_nb) + { +- if (bank == GPIO_BANK_Z) ++ static uint32_t part_number; ++ uint32_t dev_id = 0; ++ uint32_t otp = 0; ++ size_t bit_len = 0; ++ ++ assert(part_nb); ++ ++ if (part_number) { ++ *part_nb = part_number; + return 0; ++ } ++ ++ if (get_chip_dev_id(&dev_id) < 0) ++ return -1; ++ ++ if (stm32_bsec_find_otp_in_nvmem_layout(PART_NUMBER_OTP, ++ &otp, &bit_len)) ++ return -1; ++ ++ if (bit_len != PART_NUMBER_OTP_BIT_LENGTH) ++ panic(); ++ ++ if (stm32_bsec_read_otp(&part_number, otp)) ++ return -1; ++ ++ part_number = (part_number & PART_NUMBER_OTP_PART_MASK) >> ++ PART_NUMBER_OTP_PART_SHIFT; + +- assert(bank <= GPIO_BANK_K); +- return bank * GPIO_BANK_OFFSET; ++ part_number = part_number | (dev_id << 16); ++ ++ *part_nb = part_number; ++ ++ return 0; + } + +-unsigned int stm32_get_gpio_bank_clock(unsigned int bank) ++bool stm32mp_supports_cpu_opp(uint32_t opp_id) + { +- if (bank == GPIO_BANK_Z) +- return GPIOZ; ++ uint32_t part_number = 0; ++ uint32_t id = 0; ++ ++ if (get_part_number(&part_number)) { ++ DMSG("Cannot get part number\n"); ++ panic(); ++ } + +- assert(bank <= GPIO_BANK_K); +- return GPIOA + bank; ++ switch (opp_id) { ++ case PLAT_OPP_ID1: ++ case PLAT_OPP_ID2: ++ id = opp_id; ++ break; ++ default: ++ return false; ++ } ++ ++ switch (part_number) { ++#ifdef CFG_STM32MP13 ++ case STM32MP135F_PART_NB: ++ case STM32MP135D_PART_NB: ++ case STM32MP133F_PART_NB: ++ case STM32MP133D_PART_NB: ++ case STM32MP131F_PART_NB: ++ case STM32MP131D_PART_NB: ++#endif ++#ifdef CFG_STM32MP15 ++ case STM32MP157F_PART_NB: ++ case STM32MP157D_PART_NB: ++ case STM32MP153F_PART_NB: ++ case STM32MP153D_PART_NB: ++ case STM32MP151F_PART_NB: ++ case STM32MP151D_PART_NB: ++#endif ++ return true; ++ default: ++ return id == PLAT_OPP_ID1; ++ } + } + +-#ifdef CFG_DRIVERS_CLK +-struct clk *stm32_get_gpio_bank_clk(unsigned int bank) ++bool stm32mp_supports_hw_cryp(void) + { +- if (bank == GPIO_BANK_Z) +- return stm32mp_rcc_clock_id_to_clk(GPIOZ); ++ uint32_t part_number = 0; + +- assert(bank <= GPIO_BANK_K); +- return stm32mp_rcc_clock_id_to_clk(GPIOA + bank); ++ if (!IS_ENABLED(CFG_STM32_CRYP)) ++ return false; ++ ++ if (get_part_number(&part_number)) { ++ DMSG("Cannot get part number\n"); ++ panic(); ++ } ++ ++ switch (part_number) { ++#ifdef CFG_STM32MP13 ++ case STM32MP135F_PART_NB: ++ case STM32MP135C_PART_NB: ++ case STM32MP133F_PART_NB: ++ case STM32MP133C_PART_NB: ++ case STM32MP131F_PART_NB: ++ case STM32MP131C_PART_NB: ++ return true; ++#endif ++#ifdef CFG_STM32MP15 ++ case STM32MP157F_PART_NB: ++ case STM32MP157C_PART_NB: ++ case STM32MP153F_PART_NB: ++ case STM32MP153C_PART_NB: ++ case STM32MP151F_PART_NB: ++ case STM32MP151C_PART_NB: ++ return true; ++#endif ++ default: ++ return false; ++ } + } ++ ++bool stm32mp_supports_second_core(void) ++{ ++ uint32_t __maybe_unused part_number = 0; ++ ++ if (IS_ENABLED(CFG_STM32MP13)) ++ return false; ++ ++ if (CFG_TEE_CORE_NB_CORE == 1) ++ return false; ++ ++ if (get_part_number(&part_number)) { ++ DMSG("Cannot get part number\n"); ++ panic(); ++ } ++ ++ switch (part_number) { ++#ifdef CFG_STM32MP15 ++ case STM32MP151F_PART_NB: ++ case STM32MP151D_PART_NB: ++ case STM32MP151C_PART_NB: ++ case STM32MP151A_PART_NB: ++ return false; + #endif +-#endif /* CFG_STM32_GPIO */ ++ default: ++ return true; ++ } ++} ++ ++static unsigned int stm32mp_iwdg_iomem2instance(paddr_t pbase) ++{ ++ DMSG("Base %lx", pbase); ++ ++ switch (pbase) { ++ case IWDG1_BASE: ++ return IWDG1_INST; ++ case IWDG2_BASE: ++ return IWDG2_INST; ++ default: ++ panic(); ++ } ++} ++ ++unsigned long stm32_get_iwdg_otp_config(vaddr_t pbase) ++{ ++ unsigned int idx = 0; ++ unsigned long iwdg_cfg = 0; ++ uint32_t otp_id = 0; ++ size_t bit_len = 0; ++ uint32_t otp_value = 0; ++ ++ idx = stm32mp_iwdg_iomem2instance(pbase); ++ ++ if (stm32_bsec_find_otp_in_nvmem_layout(HW2_OTP, &otp_id, &bit_len)) ++ panic(); ++ ++ if (bit_len != 32) ++ panic(); ++ ++ if (stm32_bsec_read_otp(&otp_value, otp_id)) ++ panic(); ++ ++ if (otp_value & BIT(idx + HW2_OTP_IWDG_HW_ENABLE_SHIFT)) ++ iwdg_cfg |= IWDG_HW_ENABLED; ++ ++ if (otp_value & BIT(idx + HW2_OTP_IWDG_FZ_STOP_SHIFT)) ++ iwdg_cfg |= IWDG_DISABLE_ON_STOP; ++ ++ if (otp_value & BIT(idx + HW2_OTP_IWDG_FZ_STANDBY_SHIFT)) ++ iwdg_cfg |= IWDG_DISABLE_ON_STANDBY; ++ ++ return iwdg_cfg; ++} ++ ++#ifdef CFG_TEE_CORE_DEBUG ++static const char *const dump_table[] = { ++ "usr_sp", ++ "usr_lr", ++ "irq_spsr", ++ "irq_sp", ++ "irq_lr", ++ "fiq_spsr", ++ "fiq_sp", ++ "fiq_lr", ++ "svc_spsr", ++ "svc_sp", ++ "svc_lr", ++ "abt_spsr", ++ "abt_sp", ++ "abt_lr", ++ "und_spsr", ++ "und_sp", ++ "und_lr", ++#ifdef CFG_SM_NO_CYCLE_COUNTING ++ "pmcr" ++#endif ++}; ++ ++void stm32mp_dump_core_registers(bool force_display) ++{ ++ static bool display; ++ size_t i = 0U; ++ uint32_t __maybe_unused *reg = NULL; ++ struct sm_nsec_ctx *sm_nsec_ctx = sm_get_nsec_ctx(); ++ ++ if (force_display) ++ display = true; ++ ++ if (!display) ++ return; ++ ++ MSG("CPU : %i\n", get_core_pos()); ++ ++ reg = (uint32_t *)&sm_nsec_ctx->ub_regs.usr_sp; ++ for (i = 0U; i < ARRAY_SIZE(dump_table); i++) ++ MSG("%10s : 0x%08x\n", dump_table[i], reg[i]); ++} ++DECLARE_KEEP_PAGER(stm32mp_dump_core_registers); ++#endif ++ ++static TEE_Result init_debug(void) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ uint32_t conf = stm32_bsec_read_debug_conf(); ++ struct clk *dbg_clk = stm32mp_rcc_clock_id_to_clk(CK_DBG); ++ uint32_t state = 0; ++ ++ res = stm32_bsec_get_state(&state); ++ if (res) ++ return res; ++ ++ if (state != BSEC_STATE_SEC_CLOSED && conf) { ++#ifdef CFG_TEE_CORE_DEBUG ++ if (IS_ENABLED(CFG_WARN_INSECURE)) ++ IMSG("WARNING: All debug access are allowed"); ++ ++ res = stm32_bsec_write_debug_conf(conf | BSEC_DEBUG_ALL); ++#else ++ res = stm32_bsec_write_debug_conf(conf | BSEC_DBGSWGEN); ++#endif ++ ++ /* Enable DBG as used to access coprocessor debug registers */ ++ clk_enable(dbg_clk); ++ } ++ ++ return res; ++} ++early_init_late(init_debug); ++ ++#ifdef CFG_STM32_TAMP ++ ++#ifdef CFG_STM32MP13 ++static const char * const itamper_name[] = { ++ [INT_TAMP1] = "Backup domain voltage threshold monitoring", ++ [INT_TAMP2] = "Temperature monitoring", ++ [INT_TAMP3] = "LSE monitoring", ++ [INT_TAMP4] = "HSE monitoring", ++ [INT_TAMP7] = "ADC2 analog watchdog monitoring 1", ++ [INT_TAMP12] = "ADC2 analog watchdog monitoring 2", ++ [INT_TAMP13] = "ADC2 analog watchdog monitoring 3", ++}; ++#endif ++ ++#ifdef CFG_STM32MP15 ++static const char * const itamper_name[] = { ++ [INT_TAMP1] = "RTC power domain", ++ [INT_TAMP2] = "Temperature monitoring", ++ [INT_TAMP3] = "LSE monitoring", ++ [INT_TAMP4] = "HSE monitoring", ++}; ++#endif ++DECLARE_KEEP_PAGER(itamper_name); ++ ++static uint32_t stm32mp1_itamper_action(int id __maybe_unused) ++{ ++ const char __maybe_unused *tamp_name = NULL; ++ ++ if (id >= 0 && ((size_t)id < ARRAY_SIZE(itamper_name))) ++ tamp_name = itamper_name[id]; ++ ++ MSG("Internal tamper %u (%s) occurs", id - INT_TAMP1 + 1, tamp_name); ++ ++ return TAMP_CB_ACK_AND_RESET; ++} ++DECLARE_KEEP_PAGER(stm32mp1_itamper_action); ++ ++static uint32_t __unused stm32mp1_etamper_action(int id __maybe_unused) ++{ ++ MSG("External tamper %u occurs", id - EXT_TAMP1 + 1); ++ ++ return TAMP_CB_ACK_AND_RESET; ++} ++DECLARE_KEEP_PAGER(stm32mp1_etamper_action); ++ ++static TEE_Result stm32_configure_tamp(void) ++{ ++ TEE_Result res __maybe_unused = TEE_SUCCESS; ++ struct stm32_bkpregs_conf bkpregs_conf = { ++ .nb_zone1_regs = 10, /* 10 registers in zone 1 */ ++ .nb_zone2_regs = 5 /* 5 registers in zone 2 */ ++ /* Zone3 all remaining */ ++ }; ++ ++ /* Enable BKP Register protection */ ++ if (stm32_tamp_set_secure_bkpregs(&bkpregs_conf)) ++ panic(); ++ ++ /* ++ * The stm32_tamp_activate_tamp() for INT_TAMPx returns an error code ++ * only if INT_TAMPx doesn't exist on the SoC or if 'mode' is ++ * incompatible. Here 'mode' is the simplest one, and we only activate ++ * existing INT_TAMPx. ++ */ ++ stm32_tamp_activate_tamp(INT_TAMP1, TAMP_ERASE, ++ stm32mp1_itamper_action); ++ stm32_tamp_activate_tamp(INT_TAMP2, TAMP_ERASE, ++ stm32mp1_itamper_action); ++ stm32_tamp_activate_tamp(INT_TAMP3, TAMP_ERASE, ++ stm32mp1_itamper_action); ++ stm32_tamp_activate_tamp(INT_TAMP4, TAMP_ERASE, ++ stm32mp1_itamper_action); ++#ifdef CFG_STM32MP13 ++ stm32_tamp_activate_tamp(INT_TAMP7, TAMP_ERASE, ++ stm32mp1_itamper_action); ++ stm32_tamp_activate_tamp(INT_TAMP12, TAMP_ERASE, ++ stm32mp1_itamper_action); ++ stm32_tamp_activate_tamp(INT_TAMP13, TAMP_ERASE, ++ stm32mp1_itamper_action); ++#endif ++ ++#ifdef CFG_STM32MP13 ++ /* ++ * EXT_TAMPx needs to exist but also to be activated in DT. Here, we ++ * check if the EXT_TAMP2 is defined in DT. ++ */ ++ res = stm32_tamp_activate_tamp(EXT_TAMP2, TAMP_ERASE, ++ stm32mp1_etamper_action); ++ if (res == TEE_ERROR_BAD_PARAMETERS) ++ DMSG("no EXT_TAMP2 on this platform"); ++ else if (res == TEE_ERROR_ITEM_NOT_FOUND) ++ DMSG("EXT_TAMP2 in pin was not found in device tree"); ++#endif ++ ++ if (stm32_tamp_set_config()) ++ panic(); ++ ++ /* Enable timestamp for tamper */ ++ stm32_rtc_set_tamper_timestamp(); ++ ++ return TEE_SUCCESS; ++} ++ ++driver_init_late(stm32_configure_tamp); ++#endif ++ ++#ifdef CFG_STM32_HSE_MONITORING ++/* pourcent rate of hse alarm */ ++#define HSE_ALARM_PERCENT 110 ++#define FREQ_MONITOR_COMPAT "st,freq-monitor" ++ ++static void stm32_hse_over_frequency(uint32_t ticks __unused, ++ void *user_data __unused) ++{ ++ EMSG("HSE over frequency: nb ticks:%"PRIu32, ticks); ++} ++DECLARE_KEEP_PAGER(stm32_hse_over_frequency); ++ ++static TEE_Result stm32_hse_monitoring(void) ++{ ++ struct clk *hse_clk = stm32mp_rcc_clock_id_to_clk(CK_HSE); ++ struct clk *hsi_clk = stm32mp_rcc_clock_id_to_clk(CK_HSI); ++ unsigned long hse = 0; ++ unsigned long hsi_cal = 0; ++ struct counter_device *counter; ++ uint32_t ticks = 0; ++ void *fdt = NULL; ++ void *config = NULL; ++ int node = 0; ++ ++ DMSG("HSE monitoring"); ++ ++ fdt = get_embedded_dt(); ++ node = fdt_node_offset_by_compatible(fdt, 0, FREQ_MONITOR_COMPAT); ++ if (node < 0) ++ panic(); ++ ++ if (_fdt_get_status(fdt, node) == DT_STATUS_DISABLED) ++ return TEE_SUCCESS; ++ ++ hse = clk_get_rate(hse_clk); ++ hsi_cal = clk_get_rate(hsi_clk); ++ /* ++ * hsi_cal is based on hsi & DIVISOR ++ * DIVISOR is fixed, (stm32mp13:1024) ++ */ ++ hsi_cal /= 1024; ++ ++ ticks = (hse / 100) * HSE_ALARM_PERCENT; ++ ticks /= hsi_cal; ++ ++ DMSG("HSE:%luHz HSI cal:%luHz alarm:%"PRIu32, hse, hsi_cal, ticks); ++ ++ counter = fdt_counter_get(fdt, node, &config); ++ assert(counter && config); ++ ++ counter->alarm.callback = stm32_hse_over_frequency; ++ counter->alarm.ticks = ticks; ++ ++ counter_start(counter, config); ++ counter_set_alarm(counter); ++ ++ return TEE_SUCCESS; ++} ++ ++driver_init_late(stm32_hse_monitoring); ++#endif /* CFG_STM32_HSE_MONITORING */ +diff --git a/core/arch/arm/plat-stm32mp1/nsec-service/bsec_svc.c b/core/arch/arm/plat-stm32mp1/nsec-service/bsec_svc.c +deleted file mode 100644 +index 9f426de40..000000000 +--- a/core/arch/arm/plat-stm32mp1/nsec-service/bsec_svc.c ++++ /dev/null +@@ -1,67 +0,0 @@ +-// SPDX-License-Identifier: BSD-3-Clause +-/* +- * Copyright (c) 2016-2020, STMicroelectronics +- */ +- +-#include +-#include +-#include +-#include +- +-#include "bsec_svc.h" +-#include "stm32mp1_smc.h" +- +-void bsec_main(struct thread_smc_args *args) +-{ +- TEE_Result result = TEE_ERROR_GENERIC; +- uint32_t cmd = args->a1; +- uint32_t otp_id = args->a2; +- uint32_t in_value = args->a3; +- uint32_t *out_value = &args->a1; +- uint32_t tmp = 0; +- +- if (!stm32_bsec_nsec_can_access_otp(otp_id)) { +- args->a0 = STM32_SIP_SVC_INVALID_PARAMS; +- return; +- } +- +- switch (cmd) { +- case STM32_SIP_SVC_BSEC_READ_SHADOW: +- FMSG("read shadow @%#"PRIx32, otp_id); +- result = stm32_bsec_read_otp(out_value, otp_id); +- break; +- case STM32_SIP_SVC_BSEC_PROG_OTP: +- FMSG("program @%#"PRIx32, otp_id); +- result = stm32_bsec_program_otp(in_value, otp_id); +- break; +- case STM32_SIP_SVC_BSEC_WRITE_SHADOW: +- FMSG("write shadow @%#"PRIx32, otp_id); +- result = stm32_bsec_write_otp(in_value, otp_id); +- break; +- case STM32_SIP_SVC_BSEC_READ_OTP: +- FMSG("read @%#"PRIx32, otp_id); +- result = stm32_bsec_read_otp(&tmp, otp_id); +- if (!result) +- result = stm32_bsec_shadow_register(otp_id); +- if (!result) +- result = stm32_bsec_read_otp(out_value, otp_id); +- if (!result) +- result = stm32_bsec_write_otp(tmp, otp_id); +- break; +- case STM32_SIP_SVC_BSEC_WRLOCK_OTP: +- FMSG("permanent write lock @%#"PRIx32, otp_id); +- result = stm32_bsec_permanent_lock_otp(otp_id); +- break; +- default: +- DMSG("Invalid command %#"PRIx32, cmd); +- result = TEE_ERROR_BAD_PARAMETERS; +- break; +- } +- +- if (!result) +- args->a0 = STM32_SIP_SVC_OK; +- else if (result == TEE_ERROR_BAD_PARAMETERS) +- args->a0 = STM32_SIP_SVC_INVALID_PARAMS; +- else +- args->a0 = STM32_SIP_SVC_FAILED; +-} +diff --git a/core/arch/arm/plat-stm32mp1/nsec-service/bsec_svc.h b/core/arch/arm/plat-stm32mp1/nsec-service/bsec_svc.h +deleted file mode 100644 +index 5e57f5012..000000000 +--- a/core/arch/arm/plat-stm32mp1/nsec-service/bsec_svc.h ++++ /dev/null +@@ -1,21 +0,0 @@ +-/* SPDX-License-Identifier: BSD-3-Clause */ +-/* +- * Copyright (c) 2016-2020, STMicroelectronics +- */ +- +-#ifndef __STM32MP1_BSEC_SVC_H__ +-#define __STM32MP1_BSEC_SVC_H__ +- +-#include +-#include +-#include +- +-#ifdef CFG_STM32_BSEC_SIP +-void bsec_main(struct thread_smc_args *args); +-#else +-static inline void bsec_main(struct thread_smc_args *args) +-{ +- args->a0 = STM32_SIP_SVC_UNKNOWN_FUNCTION; +-} +-#endif +-#endif /*__STM32MP1_BSEC_SVC_H__*/ +diff --git a/core/arch/arm/plat-stm32mp1/nsec-service/low_power_svc.c b/core/arch/arm/plat-stm32mp1/nsec-service/low_power_svc.c +new file mode 100644 +index 000000000..d4d1eb03a +--- /dev/null ++++ b/core/arch/arm/plat-stm32mp1/nsec-service/low_power_svc.c +@@ -0,0 +1,44 @@ ++// SPDX-License-Identifier: BSD-3-Clause ++/* ++ * Copyright (c) 2017-2018, STMicroelectronics - All Rights Reserved ++ * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved. ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#include "stm32mp1_smc.h" ++#include "low_power_svc.h" ++#include "../pm/power.h" ++#include "../pm/context.h" ++ ++#ifndef CFG_PM ++uint32_t pm_domain_scv_handler(uint32_t id __unused, uint32_t enable __unused) ++{ ++ return STM32_SIP_SVC_FAILED; ++} ++#else ++uint32_t pm_domain_scv_handler(uint32_t id, uint32_t enable) ++{ ++ unsigned int pd = id; ++ ++ DMSG("%sable PD %u", enable != 0 ? "En" : "Dis", pd); ++ ++ switch (pd) { ++ case STM32MP1_PD_VSW: ++ case STM32MP1_PD_CORE_RET: ++ case STM32MP1_PD_CORE: ++ break; ++ default: ++ return STM32_SIP_SVC_INVALID_PARAMS; ++ } ++ ++ stm32mp1_set_pm_domain_state(pd, enable); ++ ++ return STM32_SIP_SVC_OK; ++} ++#endif /*CFG_PM*/ +diff --git a/core/arch/arm/plat-stm32mp1/nsec-service/low_power_svc.h b/core/arch/arm/plat-stm32mp1/nsec-service/low_power_svc.h +new file mode 100644 +index 000000000..520a26846 +--- /dev/null ++++ b/core/arch/arm/plat-stm32mp1/nsec-service/low_power_svc.h +@@ -0,0 +1,23 @@ ++/* SPDX-License-Identifier: BSD-3-Clause */ ++/* ++ * Copyright (c) 2018-2021, STMicroelectronics ++ */ ++ ++#ifndef LOW_POWER_SVC_H ++#define LOW_POWER_SVC_H ++ ++#include ++#include ++ ++#include "stm32mp1_smc.h" ++ ++#ifdef CFG_STM32_LOWPOWER_SIP ++uint32_t pm_domain_scv_handler(uint32_t x1, uint32_t x2); ++#else ++static inline uint32_t pm_domain_scv_handler(uint32_t x1 __unused, ++ uint32_t x2 __unused) ++{ ++ return STM32_SIP_SVC_FAILED; ++} ++#endif /* CFG_STM32_LOWPOWER_SIP */ ++#endif /* LOW_POWER_SVC_H */ +diff --git a/core/arch/arm/plat-stm32mp1/nsec-service/pwr_svc.c b/core/arch/arm/plat-stm32mp1/nsec-service/pwr_svc.c +new file mode 100644 +index 000000000..6c78d8203 +--- /dev/null ++++ b/core/arch/arm/plat-stm32mp1/nsec-service/pwr_svc.c +@@ -0,0 +1,89 @@ ++// SPDX-License-Identifier: BSD-3-Clause ++/* ++ * Copyright (c) 2017-2018, STMicroelectronics ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#include "pwr_svc.h" ++#include "stm32mp1_smc.h" ++ ++struct pwr_reg_prop { ++ uint32_t offset; ++ uint32_t mask; ++}; ++ ++#define PWR_ALLOWED_MASK(_off, _mask) { .offset = (_off), .mask = (_mask), } ++ ++static const struct pwr_reg_prop allowed_regs[] = { ++ PWR_ALLOWED_MASK(PWR_CR3_OFF, PWR_CR3_VBE | PWR_CR3_VBRS | ++ PWR_CR3_USB33DEN | ++ PWR_CR3_REG18EN | PWR_CR3_REG11EN), ++ PWR_ALLOWED_MASK(PWR_WKUPCR_OFF, PWR_WKUPCR_MASK), ++ PWR_ALLOWED_MASK(PWR_MPUWKUPENR_OFF, PWR_MPUWKUPENR_MASK), ++}; ++ ++uint32_t pwr_scv_handler(uint32_t x1, uint32_t x2, uint32_t x3) ++{ ++ uint32_t req = x1; ++ uint32_t offset = x2; ++ uint32_t value = x3; ++ vaddr_t va = 0; ++ uint32_t allowed = 0; ++ unsigned int i = 0; ++ ++ /* ++ * Argument x2 can be either the register physical address of the ++ * register offset toward PWR_BASE. ++ */ ++ if ((offset & ~PWR_OFFSET_MASK) != 0) { ++ if ((offset & ~PWR_OFFSET_MASK) != PWR_BASE) ++ return STM32_SIP_SVC_INVALID_PARAMS; ++ ++ offset &= PWR_OFFSET_MASK; ++ } ++ ++ DMSG("PWR service: %s 0x%" PRIx32 " at offset 0x%" PRIx32, ++ req == STM32_SIP_SVC_REG_WRITE ? "write" : ++ req == STM32_SIP_SVC_REG_SET ? "set" : "clear", ++ value, offset); ++ ++ for (i = 0; i < ARRAY_SIZE(allowed_regs); i++) { ++ if (offset != allowed_regs[i].offset) ++ continue; ++ ++ va = stm32_pwr_base() + offset; ++ allowed = allowed_regs[i].mask; ++ value &= allowed; ++ ++ switch (req) { ++ case STM32_SIP_SVC_REG_WRITE: ++ io_mask32_stm32shregs(va, value, allowed); ++ FMSG("wrt off %" PRIx32 "=%" PRIx32 " => %" PRIx32, ++ offset, value, io_read32(va)); ++ return STM32_SIP_SVC_OK; ++ case STM32_SIP_SVC_REG_SET: ++ io_setbits32_stm32shregs(va, value); ++ FMSG("set off %" PRIx32 "=%" PRIx32 " => %" PRIx32, ++ offset, value, io_read32(va)); ++ return STM32_SIP_SVC_OK; ++ case STM32_SIP_SVC_REG_CLEAR: ++ io_clrbits32_stm32shregs(va, value); ++ FMSG("clr off %" PRIx32 "=%" PRIx32 " => %" PRIx32, ++ offset, value, io_read32(va)); ++ return STM32_SIP_SVC_OK; ++ default: ++ return STM32_SIP_SVC_INVALID_PARAMS; ++ } ++ } ++ ++ return STM32_SIP_SVC_INVALID_PARAMS; ++} +diff --git a/core/arch/arm/plat-stm32mp1/nsec-service/pwr_svc.h b/core/arch/arm/plat-stm32mp1/nsec-service/pwr_svc.h +new file mode 100644 +index 000000000..429d62071 +--- /dev/null ++++ b/core/arch/arm/plat-stm32mp1/nsec-service/pwr_svc.h +@@ -0,0 +1,22 @@ ++/* SPDX-License-Identifier: BSD-3-Clause */ ++/* ++ * Copyright (c) 2017-2018, STMicroelectronics ++ */ ++ ++#ifndef __PWR_SVC_H__ ++#define __PWR_SVC_H__ ++ ++#include ++ ++#ifdef CFG_STM32_PWR_SIP ++uint32_t pwr_scv_handler(uint32_t x1, uint32_t x2, uint32_t x3); ++#else ++static inline uint32_t pwr_scv_handler(uint32_t x1 __unused, ++ uint32_t x2 __unused, ++ uint32_t x3 __unused) ++{ ++ return STM32_SIP_SVC_FAILED; ++} ++#endif ++ ++#endif /* __PWR_SVC_H__*/ +diff --git a/core/arch/arm/plat-stm32mp1/nsec-service/stm32mp1_smc.h b/core/arch/arm/plat-stm32mp1/nsec-service/stm32mp1_smc.h +index 3fbde31fe..af236176a 100644 +--- a/core/arch/arm/plat-stm32mp1/nsec-service/stm32mp1_smc.h ++++ b/core/arch/arm/plat-stm32mp1/nsec-service/stm32mp1_smc.h +@@ -1,7 +1,7 @@ + /* SPDX-License-Identifier: BSD-3-Clause */ + /* +- * Copyright (c) 2016-2020, STMicroelectronics +- * Copyright (c) 2018, Linaro Limited ++ * Copyright (c) 2016-2021, STMicroelectronics ++ * Copyright (c) 2018-2021, Linaro Limited + */ + #ifndef __STM32MP1_SMC_H__ + #define __STM32MP1_SMC_H__ +@@ -14,7 +14,7 @@ + #define STM32_SIP_SVC_VERSION_MAJOR 0x0 + #define STM32_SIP_SVC_VERSION_MINOR 0x1 + +-#define STM32_SIP_SVC_FUNCTION_COUNT 0x3 ++#define STM32_SIP_SVC_FUNCTION_COUNT 0x5 + + /* STM32 SIP service generic return codes */ + #define STM32_SIP_SVC_OK 0x0 +@@ -72,25 +72,44 @@ + #define STM32_SIP_SVC_FUNC_VERSION 0xff03 + + /* +- * SIP functions STM32_SIP_SVC_FUNC_BSEC ++ * SIP function STM32_SIP_SVC_FUNC_PWR + * +- * Argument a0: (input) SMCCC function ID ++ * Argument a0: (input) SMCC ID + * (output) status return code +- * Argument a1: (input) Service ID (STM32_SIP_BSEC_xxx) +- * Argument a2: (input) OTP index +- * (output) OTP read value, if applicable +- * Argument a3: (input) OTP value if applicable ++ * Argument a1: (input) Service ID (STM32_SIP_REG_xxx) ++ * Argument a2: (input) register offset or physical address ++ * (output) register read value, if applicable ++ * Argument a3: (input) register target value if applicable ++ */ ++#define STM32_SIP_SVC_FUNC_PWR 0x1001 ++ ++/* Service ID for STM32_SIP_SVC_FUNC_RCC/_PWR */ ++#define STM32_SIP_SVC_REG_READ 0x0 ++#define STM32_SIP_SVC_REG_WRITE 0x1 ++#define STM32_SIP_SVC_REG_SET 0x2 ++#define STM32_SIP_SVC_REG_CLEAR 0x3 ++ ++/* ++ * SIP functions STM32_SIP_SVC_FUNC_BSEC ++ * Deprecated + */ + #define STM32_SIP_SVC_FUNC_BSEC 0x1003 + +-/* Service ID for function ID STM32_SIP_FUNC_BSEC */ +-#define STM32_SIP_SVC_BSEC_READ_SHADOW 0x1 +-#define STM32_SIP_SVC_BSEC_PROG_OTP 0x2 +-#define STM32_SIP_SVC_BSEC_WRITE_SHADOW 0x3 +-#define STM32_SIP_SVC_BSEC_READ_OTP 0x4 +-/* reserved for STM32_SIP_SVC_SMC_READ_ALL 0x5 */ +-/* reserved for STM32_SIP_SVC_SMC_WRITE_ALL 0x6 */ +-#define STM32_SIP_SVC_BSEC_WRLOCK_OTP 0x7 ++/* ++ * SIP function STM32_SIP_FUNC_PD_DOMAIN ++ * ++ * Argument a0: (input) SMCC ID ++ * (output) status return code ++ * Argument a2: (index) ID of target power domain to be enabled/disabled ++ * Argument a3: (input) 0 to disable, 1 to eanble target domain ++ */ ++#define STM32_SIP_FUNC_PD_DOMAIN 0x1008 ++ ++/* Valid IDs for power domain for function STM32_SIP_FUNC_PD_DOMAIN */ ++#define STM32_SIP_PD_VSW 0x0 ++#define STM32_SIP_PD_CORE_RET 0x1 ++#define STM32_SIP_PD_CORE 0x2 ++#define STM32_SIP_PD_MAX_PM_DOMAIN 0x3 + + /* + * SIP function STM32_SIP_SVC_FUNC_SCMI_AGENT0 +@@ -103,4 +122,52 @@ + #define STM32_SIP_SVC_FUNC_SCMI_AGENT0 0x2000 + #define STM32_SIP_SVC_FUNC_SCMI_AGENT1 0x2001 + ++/* ++ * OEM Functions ++ */ ++#define STM32_OEM_SVC_VERSION_MAJOR 0x0 ++#define STM32_OEM_SVC_VERSION_MINOR 0x1 ++ ++#define STM32_OEM_SVC_FUNCTION_COUNT 1 ++ ++/* Use the same UID as for SiP service */ ++#define STM32_OEM_SVC_UID_0 STM32_SIP_SVC_UID_0 ++#define STM32_OEM_SVC_UID_1 STM32_SIP_SVC_UID_1 ++#define STM32_OEM_SVC_UID_2 STM32_SIP_SVC_UID_2 ++#define STM32_OEM_SVC_UID_3 STM32_SIP_SVC_UID_3 ++ ++/* OEM service generic return codes */ ++#define STM32_OEM_SVC_OK 0x0 ++#define STM32_OEM_SVC_NOT_SUPPORTED 0xffffffffU ++#define STM32_OEM_SVC_FAILED 0xfffffffeU ++#define STM32_OEM_SVC_INVALID_PARAMS 0xfffffffdU ++ ++/* ++ * OEM function STM32_OEM_FUNC_CALL_COUNT ++ * ++ * Argument a0: (input) SMCC ID ++ * (output) Count of defined function IDs ++ */ ++#define STM32_OEM_SVC_FUNC_CALL_COUNT 0xff00 ++ ++/* ++ * OEM function STM32_OEM_SVC_FUNC_UID ++ * ++ * Argument a0: (input) SMCC ID ++ * (output) Lowest 32bit of the stm32mp1 OEM service UUID ++ * Argument a1: (output) Next 32bit of the stm32mp1 OEM service UUID ++ * Argument a2: (output) Next 32bit of the stm32mp1 OEM service UUID ++ * Argument a3: (output) Last 32bit of the stm32mp1 OEM service UUID ++ */ ++#define STM32_OEM_SVC_FUNC_UID 0xff01 ++ ++/* ++ * OEM function STM32_OEM_FUNC_VERSION ++ * ++ * Argument a0: (input) SMCC ID ++ * (output) STM32 OEM service major ++ * Argument a1: (output) STM32 OEM service minor ++ */ ++#define STM32_OEM_SVC_FUNC_VERSION 0xff03 ++ + #endif /* __STM32MP1_SMC_H__*/ +diff --git a/core/arch/arm/plat-stm32mp1/nsec-service/stm32mp1_svc_setup.c b/core/arch/arm/plat-stm32mp1/nsec-service/stm32mp1_svc_setup.c +index 70d66674c..d18d9e40a 100644 +--- a/core/arch/arm/plat-stm32mp1/nsec-service/stm32mp1_svc_setup.c ++++ b/core/arch/arm/plat-stm32mp1/nsec-service/stm32mp1_svc_setup.c +@@ -1,6 +1,6 @@ + // SPDX-License-Identifier: BSD-3-Clause + /* +- * Copyright (c) 2017-2020, STMicroelectronics ++ * Copyright (c) 2017-2021, STMicroelectronics + */ + + #include +@@ -10,7 +10,8 @@ + #include + #include + +-#include "bsec_svc.h" ++#include "low_power_svc.h" ++#include "pwr_svc.h" + #include "stm32mp1_smc.h" + + static enum sm_handler_ret sip_service(struct sm_ctx *ctx __unused, +@@ -46,8 +47,35 @@ static enum sm_handler_ret sip_service(struct sm_ctx *ctx __unused, + args->a0 = ARM_SMCCC_RET_NOT_SUPPORTED; + } + break; +- case STM32_SIP_SVC_FUNC_BSEC: +- bsec_main(args); ++ case STM32_SIP_SVC_FUNC_PWR: ++ args->a0 = pwr_scv_handler(args->a1, args->a2, args->a3); ++ break; ++ case STM32_SIP_FUNC_PD_DOMAIN: ++ args->a0 = pm_domain_scv_handler(args->a1, args->a2); ++ break; ++ default: ++ return SM_HANDLER_PENDING_SMC; ++ } ++ ++ return SM_HANDLER_SMC_HANDLED; ++} ++ ++static enum sm_handler_ret oem_service(struct sm_ctx *ctx __unused, ++ struct thread_smc_args *args) ++{ ++ switch (OPTEE_SMC_FUNC_NUM(args->a0)) { ++ case STM32_OEM_SVC_FUNC_CALL_COUNT: ++ args->a0 = STM32_OEM_SVC_FUNCTION_COUNT; ++ break; ++ case STM32_OEM_SVC_FUNC_VERSION: ++ args->a0 = STM32_OEM_SVC_VERSION_MAJOR; ++ args->a1 = STM32_OEM_SVC_VERSION_MINOR; ++ break; ++ case STM32_OEM_SVC_FUNC_UID: ++ args->a0 = STM32_OEM_SVC_UID_0; ++ args->a1 = STM32_OEM_SVC_UID_1; ++ args->a2 = STM32_OEM_SVC_UID_2; ++ args->a3 = STM32_OEM_SVC_UID_3; + break; + default: + return SM_HANDLER_PENDING_SMC; +@@ -66,6 +94,8 @@ enum sm_handler_ret sm_platform_handler(struct sm_ctx *ctx) + switch (OPTEE_SMC_OWNER_NUM(args->a0)) { + case OPTEE_SMC_OWNER_SIP: + return sip_service(ctx, args); ++ case OPTEE_SMC_OWNER_OEM: ++ return oem_service(ctx, args); + default: + return SM_HANDLER_PENDING_SMC; + } +diff --git a/core/arch/arm/plat-stm32mp1/nsec-service/sub.mk b/core/arch/arm/plat-stm32mp1/nsec-service/sub.mk +index 6e1abcb4a..64c075d6f 100644 +--- a/core/arch/arm/plat-stm32mp1/nsec-service/sub.mk ++++ b/core/arch/arm/plat-stm32mp1/nsec-service/sub.mk +@@ -1,4 +1,5 @@ + global-incdirs-y += . + + srcs-y += stm32mp1_svc_setup.c +-srcs-$(CFG_STM32_BSEC_SIP) += bsec_svc.c ++srcs-$(CFG_STM32_LOWPOWER_SIP) += low_power_svc.c ++srcs-$(CFG_STM32_PWR_SIP) += pwr_svc.c +diff --git a/core/arch/arm/plat-stm32mp1/plat_tzc400.c b/core/arch/arm/plat-stm32mp1/plat_tzc400.c +index 6d467b3ee..964c26915 100644 +--- a/core/arch/arm/plat-stm32mp1/plat_tzc400.c ++++ b/core/arch/arm/plat-stm32mp1/plat_tzc400.c +@@ -1,19 +1,72 @@ + // SPDX-License-Identifier: BSD-2-Clause + /* +- * Copyright (c) 2019-2020, STMicroelectronics ++ * Copyright (c) 2019-2021, STMicroelectronics + */ + + #include + #include ++#include ++#include ++#include + #include + #include ++#include ++#include ++#include + #include + #include ++#include ++#include ++#include ++#include + #include + #include ++#include + #include + #include + ++struct stm32mp_tzc_region { ++ uint32_t cfg; ++ uint32_t addr; ++ uint32_t len; ++}; ++ ++struct stm32mp_tzc_platdata { ++ const char *name; ++ uintptr_t base; ++ struct clk *clk[2]; ++ int irq; ++ uint32_t mem_base; ++ uint32_t mem_size; ++ struct firewall_compat *tzc_compat; ++ struct stm32mp_tzc_region *regions; ++}; ++ ++struct stm32mp_tzc_driver_data { ++ uint32_t nb_filters; ++ uint32_t nb_regions; ++}; ++ ++struct tzc_device { ++ struct stm32mp_tzc_platdata pdata; ++ struct stm32mp_tzc_driver_data *ddata; ++ struct itr_handler *itr; ++ struct tzc_region_config *reg; ++ uint32_t nb_reg_used; ++ unsigned int lock; ++}; ++ ++#define IS_PAGE_ALIGNED(addr) (((addr) & SMALL_PAGE_MASK) == 0) ++#define filter_mask(_width) GENMASK_32(((_width) - 1U), 0U) ++ ++struct tzc_region_non_sec { ++ struct tzc_region_config region; ++ SLIST_ENTRY(tzc_region_non_sec) link; ++}; ++ ++static SLIST_HEAD(nsec_list_head, tzc_region_non_sec) nsec_region_list = ++ SLIST_HEAD_INITIALIZER(nsec_list_head); ++ + static enum itr_return tzc_it_handler(struct itr_handler *handler __unused) + { + EMSG("TZC permission failure"); +@@ -27,84 +80,455 @@ static enum itr_return tzc_it_handler(struct itr_handler *handler __unused) + return ITRR_HANDLED; + } + +-static struct itr_handler tzc_itr_handler = { +- .it = STM32MP1_IRQ_TZC, +- .handler = tzc_it_handler, +-}; +-DECLARE_KEEP_PAGER(tzc_itr_handler); ++static TEE_Result tzc_region_check_overlap(struct tzc_device *tzc_dev, ++ const struct tzc_region_config *reg) ++{ ++ unsigned int i = 0; ++ ++ /* Check if base address already defined in another region */ ++ for (i = 0; i < tzc_dev->nb_reg_used; i++) ++ if (reg->base <= tzc_dev->reg[i].top && ++ reg->top >= tzc_dev->reg[i].base) ++ return TEE_ERROR_ACCESS_CONFLICT; ++ ++ return TEE_SUCCESS; ++} ++ ++static void tzc_set_driverdata(struct tzc_device *tzc_dev) ++{ ++ struct stm32mp_tzc_driver_data *ddata = tzc_dev->ddata; ++ uintptr_t base = tzc_dev->pdata.base; ++ uint32_t regval = 0; ++ ++ clk_enable(tzc_dev->pdata.clk[0]); ++ ++ regval = io_read32(base + BUILD_CONFIG_OFF); ++ ddata->nb_filters = ((regval >> BUILD_CONFIG_NF_SHIFT) & ++ BUILD_CONFIG_NF_MASK) + 1; ++ ddata->nb_regions = ((regval >> BUILD_CONFIG_NR_SHIFT) & ++ BUILD_CONFIG_NR_MASK); ++ ++ clk_disable(tzc_dev->pdata.clk[0]); ++ ++ DMSG("TZC400 Filters %i Regions %i\n", ddata->nb_filters, ++ ddata->nb_regions); ++} ++ ++static struct tzc_device *tzc_alloc(void) ++{ ++ struct tzc_device *tzc_dev = NULL; ++ struct stm32mp_tzc_driver_data *ddata = NULL; ++ ++ tzc_dev = calloc(1, sizeof(*tzc_dev)); ++ ddata = calloc(1, sizeof(*ddata)); ++ ++ if (tzc_dev && ddata) { ++ tzc_dev->ddata = ddata; ++ return tzc_dev; ++ } ++ ++ free(ddata); ++ free(tzc_dev); ++ ++ return NULL; ++} + +-static bool tzc_region_is_non_secure(unsigned int i, vaddr_t base, size_t size) ++static void tzc_free(struct tzc_device *tzc_dev) + { +- struct tzc_region_config region_cfg = { }; +- uint32_t ns_cpu_mask = TZC_REGION_ACCESS_RDWR(STM32MP1_TZC_A7_ID); +- uint32_t filters_mask = GENMASK_32(1, 0); ++ if (tzc_dev) { ++ free(tzc_dev->ddata); ++ free(tzc_dev); ++ } ++} ++ ++static void stm32mp_tzc_region0(bool enable) ++{ ++ struct tzc_region_config region_cfg_0 = { ++ .base = 0, ++ .top = UINT_MAX, ++ .sec_attr = TZC_REGION_S_NONE, ++ .ns_device_access = 0, ++ }; ++ ++ if (enable) ++ region_cfg_0.sec_attr = TZC_REGION_S_RDWR; ++ ++ tzc_configure_region(0, ®ion_cfg_0); ++} ++ ++static void stm32mp_tzc_reset_region(struct tzc_device *tzc_dev) ++{ ++ unsigned int i = 0; ++ const struct tzc_region_config cfg = { .top = 0x00000FFF }; ++ ++ /* Clean old configuration */ ++ for (i = 0; i < tzc_dev->ddata->nb_regions; i++) ++ tzc_configure_region(i + 1, &cfg); ++} ++ ++static TEE_Result stm32mp_tzc_region_append(struct tzc_device *tzc_dev, ++ const struct tzc_region_config ++ *region_cfg) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ unsigned int index = 0; ++ uint32_t exceptions = 0; ++ ++ exceptions = may_spin_lock(&tzc_dev->lock); ++ index = tzc_dev->nb_reg_used; ++ ++ if (index >= tzc_dev->ddata->nb_regions || ++ region_cfg->base < tzc_dev->pdata.mem_base || ++ region_cfg->top > tzc_dev->pdata.mem_base + ++ (tzc_dev->pdata.mem_size - 1)) { ++ res = TEE_ERROR_BAD_PARAMETERS; ++ goto end; ++ } ++ ++ res = tzc_region_check_overlap(tzc_dev, region_cfg); ++ if (res) ++ goto end; + +- if (tzc_get_region_config(i, ®ion_cfg)) ++ memcpy(&tzc_dev->reg[index], region_cfg, ++ sizeof(struct tzc_region_config)); ++ ++ tzc_configure_region(index + 1, region_cfg); ++ ++ tzc_dev->nb_reg_used++; ++ ++end: ++ may_spin_unlock(&tzc_dev->lock, exceptions); ++ ++ return res; ++} ++ ++static TEE_Result ++exclude_region_from_nsec(const struct tzc_region_config *reg_exclude) ++{ ++ struct tzc_region_non_sec *reg = NULL; ++ bool found = false; ++ ++ SLIST_FOREACH(reg, &nsec_region_list, link) { ++ found = core_is_buffer_inside(reg_exclude->base, ++ reg_exclude->top - ++ reg_exclude->base + 1, ++ reg->region.base, ++ reg->region.top - ++ reg->region.base + 1); ++ if (found) ++ break; ++ } ++ ++ if (!found) + panic(); + +- return region_cfg.base == base && region_cfg.top == (base + size - 1) && +- region_cfg.sec_attr == TZC_REGION_S_NONE && +- (region_cfg.ns_device_access & ns_cpu_mask) == ns_cpu_mask && +- region_cfg.filters == filters_mask; ++ if (reg_exclude->base == reg->region.base && ++ reg_exclude->top == reg->region.top) { ++ /* Remove this entry */ ++ SLIST_REMOVE(&nsec_region_list, reg, tzc_region_non_sec, link); ++ } else if (reg_exclude->base == reg->region.base) { ++ reg->region.base = reg_exclude->top + 1; ++ } else if (reg_exclude->top == reg->region.top) { ++ reg->region.top = reg_exclude->base - 1; ++ } else { ++ struct tzc_region_non_sec *new_nsec = ++ calloc(1, sizeof(*new_nsec)); ++ ++ if (!new_nsec || !reg) ++ panic(); ++ ++ memcpy((void *)&new_nsec->region, (void *)®->region, ++ sizeof(struct tzc_region_config)); ++ reg->region.top = reg_exclude->base - 1; ++ new_nsec->region.base = reg_exclude->top + 1; ++ SLIST_INSERT_AFTER(reg, new_nsec, link); ++ } ++ ++ return TEE_SUCCESS; + } + +-static bool tzc_region_is_secure(unsigned int i, vaddr_t base, size_t size) ++static void stm32mp_tzc_cfg_boot_region(struct tzc_device *tzc_dev) + { +- struct tzc_region_config region_cfg = { }; +- uint32_t filters_mask = GENMASK_32(1, 0); ++ unsigned int idx = 0; ++ static struct tzc_region_config boot_region[] = { ++ { ++ .base = CFG_TZDRAM_START, ++ .top = CFG_TZDRAM_START + CFG_TZDRAM_SIZE - 1, ++ .sec_attr = TZC_REGION_S_RDWR, ++ .ns_device_access = 0, ++ }, ++#if CFG_CORE_RESERVED_SHM ++ { ++ .base = CFG_SHMEM_START, ++ .top = CFG_SHMEM_START + CFG_SHMEM_SIZE - 1, ++ .sec_attr = TZC_REGION_S_NONE, ++ .ns_device_access = ++ TZC_REGION_ACCESS_RDWR(STM32MP1_TZC_A7_ID), ++ } ++#endif ++ }; ++ ++ COMPILE_TIME_ASSERT(IS_PAGE_ALIGNED(CFG_TZDRAM_START)); ++ COMPILE_TIME_ASSERT(IS_PAGE_ALIGNED(CFG_TZDRAM_SIZE)); ++ COMPILE_TIME_ASSERT(IS_PAGE_ALIGNED(CFG_SHMEM_START)); ++ COMPILE_TIME_ASSERT(IS_PAGE_ALIGNED(CFG_SHMEM_SIZE)); + +- if (tzc_get_region_config(i, ®ion_cfg)) ++ stm32mp_tzc_region0(true); ++ ++ stm32mp_tzc_reset_region(tzc_dev); ++ ++ for (idx = 0; idx < ARRAY_SIZE(boot_region); idx++) { ++ TEE_Result res = TEE_ERROR_GENERIC; ++ ++ boot_region[idx].filters = ++ filter_mask(tzc_dev->ddata->nb_filters); ++ ++ res = stm32mp_tzc_region_append(tzc_dev, &boot_region[idx]); ++ if (res) ++ panic("Enable to configure core regions"); ++ ++ res = exclude_region_from_nsec(&boot_region[idx]); ++ } ++ ++ /* Remove region0 access */ ++ stm32mp_tzc_region0(false); ++} ++ ++static TEE_Result fdt_stm32mp_tzc_add_regions(struct tzc_device *tzc_dev, ++ const void *fdt, int node) ++{ ++ const fdt32_t *conf_list = NULL; ++ TEE_Result res = TEE_SUCCESS; ++ unsigned int nregions = 0; ++ unsigned int i = 0; ++ int len = 0; ++ ++ conf_list = fdt_getprop(fdt, node, "memory-region", &len); ++ if (!conf_list) ++ return TEE_SUCCESS; ++ ++ nregions = len / sizeof(uint32_t); ++ if (nregions > tzc_dev->ddata->nb_regions) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ for (i = 0; i < nregions; i++) { ++ struct tzc_region_config region_cfg = { }; ++ int pnode = 0; ++ const fdt32_t *prop = NULL; ++ uint32_t phandle = fdt32_to_cpu(*(conf_list + i)); ++ ++ pnode = fdt_node_offset_by_phandle(fdt, phandle); ++ if (pnode < 0) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ prop = fdt_getprop(fdt, pnode, "reg", NULL); ++ if (!prop) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ if (!IS_PAGE_ALIGNED(fdt32_to_cpu(prop[0])) || ++ !IS_PAGE_ALIGNED(fdt32_to_cpu(prop[1]))) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ region_cfg.base = fdt32_to_cpu(prop[0]); ++ region_cfg.top = region_cfg.base + (fdt32_to_cpu(prop[1]) - 1); ++ region_cfg.filters = filter_mask(tzc_dev->ddata->nb_filters); ++ ++ prop = fdt_getprop(fdt, pnode, "st,protreg", &len); ++ if (!prop || len != (2 * sizeof(uint32_t))) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ region_cfg.sec_attr = fdt32_to_cpu(prop[0]); ++ region_cfg.ns_device_access = fdt32_to_cpu(prop[1]); ++ ++ DMSG("0x%#08"PRIxVA" - 0x%#08"PRIxVA" : Sec access %i NS access %#"PRIx32, ++ region_cfg.base, region_cfg.top, region_cfg.sec_attr, ++ region_cfg.ns_device_access); ++ ++ res = stm32mp_tzc_region_append(tzc_dev, ®ion_cfg); ++ if (res) ++ panic("Error adding region"); ++ ++ res = exclude_region_from_nsec(®ion_cfg); ++ if (res) ++ panic("Not able to exclude region"); ++ } ++ ++ return 0; ++} ++ ++static TEE_Result stm32mp_tzc_parse_fdt(struct tzc_device *tzc_dev, ++ const void *fdt, int node) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ struct dt_node_info dt_info = { }; ++ struct io_pa_va base = { }; ++ const fdt32_t *cuint = NULL; ++ int offs = 0; ++ ++ _fdt_fill_device_info(fdt, &dt_info, node); ++ if (dt_info.reg == DT_INFO_INVALID_REG || ++ dt_info.reg_size == DT_INFO_INVALID_REG_SIZE || ++ dt_info.clock == DT_INFO_INVALID_CLOCK || ++ dt_info.interrupt == DT_INFO_INVALID_INTERRUPT) ++ panic("Missing properties in TZC DT node"); ++ ++ base.pa = dt_info.reg; ++ tzc_dev->pdata.name = fdt_get_name(fdt, node, NULL); ++ tzc_dev->pdata.base = io_pa_or_va_secure(&base, dt_info.reg_size); ++ tzc_dev->pdata.irq = dt_info.interrupt; ++ ++ res = clk_dt_get_by_index(fdt, node, 0, tzc_dev->pdata.clk); ++ if (res) ++ return res; ++ ++ res = clk_dt_get_by_index(fdt, node, 1, tzc_dev->pdata.clk + 1); ++ if (res || !tzc_dev->pdata.clk[1]) ++ DMSG("No secondary clock for %s", ++ fdt_get_name(fdt, node, NULL)); ++ ++ /* Use memory node instead of that new one */ ++ offs = fdt_node_offset_by_prop_value(fdt, offs, "device_type", ++ "memory", sizeof("memory")); ++ if (offs < 0) ++ panic("No memory reference for TZC DT node"); ++ ++ cuint = fdt_getprop(fdt, offs, "reg", NULL); ++ if (!cuint) + panic(); + +- return region_cfg.base == base && region_cfg.top == (base + size - 1) && +- region_cfg.sec_attr == TZC_REGION_S_RDWR && +- region_cfg.ns_device_access == 0 && +- region_cfg.filters == filters_mask; ++ tzc_dev->pdata.mem_base = fdt32_to_cpu(*cuint); ++ tzc_dev->pdata.mem_size = fdt32_to_cpu(*(cuint + 1)); ++ ++ return TEE_SUCCESS; + } + +-static TEE_Result init_stm32mp1_tzc(void) ++static TEE_Result stm32mp1_tzc_pm(enum pm_op op, ++ unsigned int pm_hint __unused, ++ const struct pm_callback_handle *hdl) + { +- void *base = phys_to_virt(TZC_BASE, MEM_AREA_IO_SEC, 1); +- unsigned int region_index = 1; +- const uint64_t dram_start = DDR_BASE; +- const uint64_t dram_end = dram_start + CFG_DRAM_SIZE; +- const uint64_t tzdram_start = CFG_TZDRAM_START; +- const uint64_t tzdram_size = CFG_TZDRAM_SIZE; +- const uint64_t tzdram_end = tzdram_start + tzdram_size; ++ unsigned int i = 0; ++ struct tzc_device *tzc_dev = ++ (struct tzc_device *)PM_CALLBACK_GET_HANDLE(hdl); + +- assert(base); ++ if (op == PM_OP_RESUME) { ++ stm32mp_tzc_region0(true); + +- tzc_init((vaddr_t)base); +- tzc_dump_state(); ++ stm32mp_tzc_reset_region(tzc_dev); ++ ++ for (i = 0; i < tzc_dev->nb_reg_used; i++) ++ tzc_configure_region(i + 1, &tzc_dev->reg[i]); ++ } ++ ++ return TEE_SUCCESS; ++} ++DECLARE_KEEP_PAGER(stm32mp1_tzc_pm); + +- /* +- * Early boot stage is in charge of configuring memory regions +- * OP-TEE hence here only check this complies with static Core +- * expectations. +- */ +- if (dram_start < tzdram_start) { +- if (!tzc_region_is_non_secure(region_index, dram_start, +- tzdram_start - dram_start)) +- panic("Unexpected TZC area on non-secure region"); +- +- region_index++; ++static TEE_Result stm32mp1_tzc_probe(const void *fdt, int node, ++ const void *compt_data __unused) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ struct tzc_device *tzc_dev = NULL; ++ struct tzc_region_non_sec *nsec_region = NULL; ++ struct tzc_region_non_sec *region_safe = NULL; ++ ++ assert(fdt && node >= 0); ++ ++ tzc_dev = tzc_alloc(); ++ if (!tzc_dev) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ res = stm32mp_tzc_parse_fdt(tzc_dev, fdt, node); ++ if (res) ++ goto err; ++ ++ if (tzc_dev->ddata) { ++ tzc_set_driverdata(tzc_dev); ++ tzc_dev->reg = calloc(tzc_dev->ddata->nb_regions, ++ sizeof(*tzc_dev->reg)); ++ if (!tzc_dev->reg) { ++ res = TEE_ERROR_OUT_OF_MEMORY; ++ goto err; ++ } + } + +- if (!tzc_region_is_secure(region_index, tzdram_start, tzdram_size)) +- panic("Unexpected TZC configuration on secure region"); ++ clk_enable(tzc_dev->pdata.clk[0]); ++ if (tzc_dev->pdata.clk[1]) ++ clk_enable(tzc_dev->pdata.clk[1]); + +- if (tzdram_end < dram_end) { +- region_index++; ++ tzc_init((vaddr_t)tzc_dev->pdata.base); + +- if (!tzc_region_is_non_secure(region_index, tzdram_end, +- dram_end - tzdram_end)) +- panic("Unexpected TZC area on non-secure region"); ++ nsec_region = calloc(1, sizeof(*nsec_region)); ++ if (!nsec_region) { ++ res = TEE_ERROR_OUT_OF_MEMORY; ++ goto err; + } + +- itr_add(&tzc_itr_handler); +- itr_enable(tzc_itr_handler.it); +- tzc_set_action(TZC_ACTION_INT); ++ nsec_region->region.base = tzc_dev->pdata.mem_base; ++ nsec_region->region.top = tzc_dev->pdata.mem_base + ++ tzc_dev->pdata.mem_size - 1; ++ nsec_region->region.sec_attr = TZC_REGION_S_NONE; ++ nsec_region->region.ns_device_access = TZC_REGION_NSEC_ALL_ACCESS_RDWR; ++ nsec_region->region.filters = filter_mask(tzc_dev->ddata->nb_filters); ++ ++ SLIST_INSERT_HEAD(&nsec_region_list, nsec_region, link); ++ ++ stm32mp_tzc_cfg_boot_region(tzc_dev); ++ ++ res = fdt_stm32mp_tzc_add_regions(tzc_dev, fdt, node); ++ if (res) ++ goto err; ++ ++ SLIST_FOREACH_SAFE(nsec_region, &nsec_region_list, link, region_safe) { ++ DMSG("0x%#08"PRIxVA" - 0x%#08"PRIxVA" : Sec access %i NS access %#"PRIx32, ++ nsec_region->region.base, nsec_region->region.top, ++ nsec_region->region.sec_attr, ++ nsec_region->region.ns_device_access); ++ ++ res = stm32mp_tzc_region_append(tzc_dev, &nsec_region->region); ++ if (res) ++ panic("Error adding region"); ++ ++ SLIST_REMOVE(&nsec_region_list, nsec_region, ++ tzc_region_non_sec, link); ++ free(nsec_region); ++ }; ++ ++ tzc_dump_state(); ++ ++ tzc_dev->itr = itr_alloc_add(tzc_dev->pdata.irq, tzc_it_handler, ++ ITRF_TRIGGER_LEVEL, tzc_dev); ++ if (!tzc_dev->itr) ++ panic(); ++ ++ itr_enable(tzc_dev->pdata.irq); ++ tzc_set_action(TZC_ACTION_ERR); ++ ++ register_pm_core_service_cb(stm32mp1_tzc_pm, tzc_dev, ++ "stm32mp1-tzc400"); + + return TEE_SUCCESS; ++ ++err: ++ if (res) ++ tzc_free(tzc_dev); ++ ++ SLIST_FOREACH_SAFE(nsec_region, &nsec_region_list, link, region_safe) { ++ SLIST_REMOVE(&nsec_region_list, nsec_region, ++ tzc_region_non_sec, link); ++ free(nsec_region); ++ }; ++ ++ return res; + } +-driver_init(init_stm32mp1_tzc); ++ ++static const struct dt_device_match tzc_secu_match_table[] = { ++ { .compatible = "st,stm32mp1-tzc" }, ++ { } ++}; ++ ++DEFINE_DT_DRIVER(tzc_stm32mp1_dt_driver) = { ++ .name = "stm32mp1-tzc400", ++ .type = DT_DRIVER_NOTYPE, ++ .match_table = tzc_secu_match_table, ++ .probe = stm32mp1_tzc_probe, ++}; +diff --git a/core/arch/arm/plat-stm32mp1/platform_config.h b/core/arch/arm/plat-stm32mp1/platform_config.h +index b45d2240d..cffd07122 100644 +--- a/core/arch/arm/plat-stm32mp1/platform_config.h ++++ b/core/arch/arm/plat-stm32mp1/platform_config.h +@@ -1,6 +1,6 @@ + /* SPDX-License-Identifier: BSD-2-Clause */ + /* +- * Copyright (c) 2017-2018, STMicroelectronics ++ * Copyright (c) 2017-2022, STMicroelectronics + */ + + #ifndef PLATFORM_CONFIG_H +@@ -11,6 +11,30 @@ + /* Make stacks aligned to data cache line length */ + #define STACK_ALIGNMENT 32 + ++#if defined(CFG_WITH_PAGER) ++#if defined(CFG_WITH_LPAE) ++/* ++ * Optimize unpaged memory size: ++ * - one table for the level2 table for overall vmem range ++ * - two tables for TEE RAM fine grain mapping [2ffc.0000 301f.ffff] ++ * - one table for a 2MByte dynamic shared virtual memory (SHM_VASPACE) ++ */ ++#define MAX_XLAT_TABLES 4 ++#else ++/* ++ * Optimize unpaged memory size: ++ * - two tables for TEE RAM mapping [2ffc.0000 300f.ffff] ++ * - one table for secure internal RAMs (PM: ROMed core TEE RAM) ++ * - one table for non-secure internal RAMs (PM: DDR first page) ++ * - two tables for a 2MByte dynamiq shared virtual memory (SHM_VASPACE) ++ */ ++#define MAX_XLAT_TABLES 6 ++#endif /*CFG_WITH_LPAE*/ ++#else ++/* Be generous with this setup that has plenty of secure RAM */ ++#define MAX_XLAT_TABLES 10 ++#endif /*CFG_WITH_PAGER*/ ++ + /* SoC interface registers base address ranges */ + #define APB1_BASE 0x40000000 + #define APB1_SIZE 0x0001d000 +@@ -22,17 +46,33 @@ + #define APB4_SIZE 0x00008000 + #define APB5_BASE 0x5c000000 + #define APB5_SIZE 0x0000b000 ++#ifdef CFG_STM32MP13 ++#define APB6_BASE 0x4c000000 ++#define APB6_SIZE 0x0000d000 ++#endif + + #define AHB4_BASE 0x50000000 + #define AHB4_SIZE 0x00020000 ++#ifdef CFG_STM32MP13 ++#define AHB5_BASE 0x54000000 ++#define AHB5_SIZE 0x00008000 ++#else + #define AHB5_BASE 0x54000000 + #define AHB5_SIZE 0x00005000 ++#endif ++ + + /* SoC interface registers base address */ ++#define MCUSRAM_BASE 0x30000000ul ++#define RETRAM_BASE 0x38000000ul ++#define BKPSRAM_BASE 0x54000000 + #define BSEC_BASE 0x5c005000 + #define ETZPC_BASE 0x5c007000 + #define CRYP1_BASE 0x54001000 ++#define DBGMCU_BASE 0x50081000 + #define DDR_BASE 0xc0000000ul ++#define DDRCTRL_BASE 0x5a003000 ++#define DDRPHYC_BASE 0x5a004000 + #define GIC_BASE 0xa0021000ul + #define GPIOA_BASE 0x50002000 + #define GPIOB_BASE 0x50003000 +@@ -43,25 +83,76 @@ + #define GPIOG_BASE 0x50008000 + #define GPIOH_BASE 0x50009000 + #define GPIOI_BASE 0x5000a000 ++ ++#ifdef CFG_STM32MP13 ++#define HASH1_BASE 0x54003000 ++#define RNG1_BASE 0x54004000 ++#define I2C3_BASE 0x4C004000 ++#define I2C4_BASE 0x4C005000 ++#define I2C5_BASE 0x4C006000 ++#endif ++ ++#ifdef CFG_STM32MP15 + #define GPIOJ_BASE 0x5000b000 + #define GPIOK_BASE 0x5000c000 + #define GPIOZ_BASE 0x54004000 + #define HASH1_BASE 0x54002000 ++#define RNG1_BASE 0x54003000 + #define I2C4_BASE 0x5c002000 + #define I2C6_BASE 0x5c009000 ++#endif ++ + #define IWDG1_BASE 0x5c003000 + #define IWDG2_BASE 0x5a002000 + #define PWR_BASE 0x50001000 + #define RCC_BASE 0x50000000 +-#define RNG1_BASE 0x54003000 + #define RTC_BASE 0x5c004000 ++#ifdef CFG_STM32MP15 + #define SPI6_BASE 0x5c001000 ++#endif ++#define STGEN_BASE 0x5c008000 + #define SYSCFG_BASE 0x50020000 ++#ifdef CFG_STM32MP13 ++#define SYSRAM_BASE 0x2ffe0000 ++#endif ++#ifdef CFG_STM32MP15 + #define SYSRAM_BASE 0x2ffc0000 ++#endif + #define TAMP_BASE 0x5c00a000 ++#define TIM1_BASE 0x44000000 ++#define TIM2_BASE 0x40000000 ++#define TIM3_BASE 0x40001000 ++#define TIM4_BASE 0x40002000 ++#define TIM5_BASE 0x40003000 ++#define TIM6_BASE 0x40004000 ++#define TIM7_BASE 0x40005000 ++#define TIM8_BASE 0x44001000 ++#ifdef CFG_STM32MP13 ++#define LPTIM2_BASE 0x50021000 ++#define LPTIM3_BASE 0x50022000 ++#define TIM12_BASE 0x4C007000 ++#define TIM13_BASE 0x4C008000 ++#define TIM14_BASE 0x4C009000 ++#define TIM15_BASE 0x4C00A000 ++#define TIM16_BASE 0x4C00B000 ++#define TIM17_BASE 0x4C00C000 ++#endif ++#ifdef CFG_STM32MP15 ++#define TIM12_BASE 0x40006000 ++#define TIM13_BASE 0x40007000 ++#define TIM14_BASE 0x40008000 ++#define TIM15_BASE 0x44006000 ++#define TIM16_BASE 0x44007000 ++#define TIM17_BASE 0x44008000 ++#endif + #define TZC_BASE 0x5c006000 + #define UART1_BASE 0x5c000000 ++#ifdef CFG_STM32MP13 ++#define UART2_BASE 0x4c001000 ++#endif ++#ifdef CFG_STM32MP15 + #define UART2_BASE 0x4000e000 ++#endif + #define UART3_BASE 0x4000f000 + #define UART4_BASE 0x40010000 + #define UART5_BASE 0x40011000 +@@ -69,6 +160,19 @@ + #define UART7_BASE 0x40018000 + #define UART8_BASE 0x40019000 + ++#define VREFBUF_BASE 0x50025000 ++#define LTDC_BASE 0x5A001000 ++#define DCMIPP_BASE 0x5A000000 ++#define ADC1_BASE 0x48003000 ++#define ADC2_BASE 0x48004000 ++#define OTG_BASE 0x49000000 ++#define TSC_BASE 0x5000B000 ++#define MDMA_BASE 0x58000000 ++#define ETH1_BASE 0x5800A000 ++#define ETH2_BASE 0x5800E000 ++#define FMC_BASE 0x58002000 ++#define QSPI_BASE 0x58003000 ++ + /* Console configuration */ + #define STM32MP1_DEBUG_USART_BASE UART4_BASE + #define GIC_SPI_UART4 84 +@@ -77,21 +181,50 @@ + #define CONSOLE_UART_SIZE 1024 + + /* BSEC OTP resources */ +-#define STM32MP1_OTP_MAX_ID 0x5FU +-#define STM32MP1_UPPER_OTP_START 0x20U ++#define STM32MP1_OTP_MAX_ID U(0x5F) ++#define STM32MP1_UPPER_OTP_START U(0x20) + +-#define OTP_MAX_SIZE (STM32MP1_OTP_MAX_ID + 1U) ++#define OTP_MAX_SIZE (STM32MP1_OTP_MAX_ID + U(1)) ++ ++#define CFG0_OTP "cfg0_otp" ++#ifdef CFG_STM32MP13 ++#define CFG0_OTP_BIT_LENGTH U(16) ++#define CFG0_OTP_MODE_MASK GENMASK_32(9, 0) ++#define CFG0_OTP_MODE_SHIFT U(0) ++#define CFG0_OPEN_DEVICE U(0x17) ++#define CFG0_CLOSED_DEVICE U(0x3F) ++#define CFG0_CLOSED_DEVICE_NO_BOUNDARY_SCAN U(0x17F) ++#define CFG0_CLOSED_DEVICE_NO_JTAG U(0x3FF) ++#define CFG0_CLOSED_MASK CFG0_CLOSED_DEVICE ++#endif ++#ifdef CFG_STM32MP15 ++#define CFG0_OTP_BIT_LENGTH U(8) ++#define CFG0_OTP_SECURED_POS U(6) ++#define CFG0_CLOSED_MASK BIT(CFG0_OTP_SECURED_POS) ++#endif + +-#define DATA0_OTP 0 +-#define PART_NUMBER_OTP 1 +-#define MONOTONIC_OTP 4 +-#define NAND_OTP 9 +-#define UID0_OTP 13 +-#define UID1_OTP 14 +-#define UID2_OTP 15 +-#define HW2_OTP 18 ++#define HW2_OTP "hw2_otp" ++#define HW2_OTP_IWDG_HW_ENABLE_SHIFT U(3) ++#define HW2_OTP_IWDG_FZ_STOP_SHIFT U(5) ++#define HW2_OTP_IWDG_FZ_STANDBY_SHIFT U(7) ++#define HW2_OTP_PRODUCT_BELOW_2V5 BIT(13) + +-#define DATA0_OTP_SECURED_POS 6 ++#define PART_NUMBER_OTP "part_number_otp" ++#define PART_NUMBER_OTP_PART_SHIFT U(0) ++ ++#ifdef CFG_STM32MP13 ++#define PART_NUMBER_OTP_BIT_LENGTH U(16) ++#define PART_NUMBER_OTP_PART_MASK GENMASK_32(14, 0) ++#endif ++ ++#ifdef CFG_STM32MP15 ++#define PART_NUMBER_OTP_BIT_LENGTH U(8) ++#define PART_NUMBER_OTP_PART_MASK GENMASK_32(7, 0) ++#endif ++ ++#define HW2_OTP_IWDG_HW_ENABLE_SHIFT U(3) ++#define HW2_OTP_IWDG_FZ_STOP_SHIFT U(5) ++#define HW2_OTP_IWDG_FZ_STANDBY_SHIFT U(7) + + /* GIC resources */ + #define GIC_SIZE 0x2000 +@@ -102,54 +235,29 @@ + #define GIC_SEC_SGI_0 8 + #define GIC_SEC_SGI_1 9 + ++#define GIC_MPU_WAKEUP_PIN 157 ++ + #define TARGET_CPU0_GIC_MASK BIT(0) + #define TARGET_CPU1_GIC_MASK BIT(1) + #define TARGET_CPUS_GIC_MASK GENMASK_32(CFG_TEE_CORE_NB_CORE - 1, 0) + +-/* +- * GPIO banks: 11 non secure banks (A to K) and 1 secure bank (Z) +- * Bank register's base address is computed from the bank ID listed here. +- */ +-#define GPIOS_NSEC_COUNT 11 +-#define GPIOS_NSEC_BASE GPIOA_BASE +-#define GPIOS_NSEC_SIZE (GPIOS_NSEC_COUNT * SMALL_PAGE_SIZE) +- +-#define STM32MP1_GPIOZ_MAX_COUNT 1 +-#define STM32MP1_GPIOZ_PIN_MAX_COUNT 8 +- +-#define GPIO_BANK_OFFSET 0x1000U +- +-/* Bank IDs used in GPIO driver API */ +-#define GPIO_BANK_A 0U +-#define GPIO_BANK_B 1U +-#define GPIO_BANK_C 2U +-#define GPIO_BANK_D 3U +-#define GPIO_BANK_E 4U +-#define GPIO_BANK_F 5U +-#define GPIO_BANK_G 6U +-#define GPIO_BANK_H 7U +-#define GPIO_BANK_I 8U +-#define GPIO_BANK_J 9U +-#define GPIO_BANK_K 10U +-#define GPIO_BANK_Z 25U ++#define STM32MP_GIC_PRIORITY_CSTOP U(0xC0) ++ ++/* IWDG resources */ ++#define IWDG1_INST U(0) ++#define IWDG2_INST U(1) ++ ++/* RCC platform resources */ ++#ifdef CFG_STM32MP13 ++#define RCC_WAKEUP_IT U(154) ++#endif ++ ++#ifdef CFG_STM32MP15 ++#define RCC_WAKEUP_IT U(177) ++#endif + + /* TAMP resources */ +-#define TAMP_BKP_REGISTER_OFF 0x100 +- +-/* TZC resources */ +-#define STM32MP1_IRQ_TZC 36 +- +-#define STM32MP1_TZC_A7_ID 0 +-#define STM32MP1_TZC_M4_ID 1 +-#define STM32MP1_TZC_LCD_ID 3 +-#define STM32MP1_TZC_GPU_ID 4 +-#define STM32MP1_TZC_MDMA_ID 5 +-#define STM32MP1_TZC_DMA_ID 6 +-#define STM32MP1_TZC_USB_HOST_ID 7 +-#define STM32MP1_TZC_USB_OTG_ID 8 +-#define STM32MP1_TZC_SDMMC_ID 9 +-#define STM32MP1_TZC_ETH_ID 10 +-#define STM32MP1_TZC_DAP_ID 15 ++#define TAMP_BKP_REGISTER_OFF U(0x100) + + /* USART/UART resources */ + #define USART1_BASE UART1_BASE +@@ -157,6 +265,29 @@ + #define USART3_BASE UART3_BASE + #define USART6_BASE UART6_BASE + ++/* DBGMCU resources */ ++#define DBGMCU_IDC U(0x0) ++#define DBGMCU_IDC_DEV_ID_MASK GENMASK_32(11, 0) ++#define DBGMCU_IDC_REV_ID_MASK GENMASK_32(31, 16) ++#define DBGMCU_IDC_REV_ID_SHIFT U(16) ++ ++/* BKPSRAM layout */ ++#ifdef CFG_STM32MP13 ++#define BKPSRAM_SIZE U(0x2000) ++#else ++#define BKPSRAM_SIZE U(0x1000) ++#endif ++ ++#define BKPSRAM_PM_OFFSET U(0x0000) ++#define BKPSRAM_PM_SIZE (BKPSRAM_PM_MAILBOX_SIZE + \ ++ BKPSRAM_PM_CONTEXT_SIZE) ++ ++#define BKPSRAM_PM_MAILBOX_OFFSET BKPSRAM_PM_OFFSET ++#define BKPSRAM_PM_MAILBOX_SIZE U(0x100) ++#define BKPSRAM_PM_CONTEXT_OFFSET (BKPSRAM_PM_MAILBOX_OFFSET + \ ++ BKPSRAM_PM_MAILBOX_SIZE) ++#define BKPSRAM_PM_CONTEXT_SIZE U(0xF00) ++ + /* SYSRAM layout */ + #define SYSRAM_SIZE 0x40000 + #define SYSRAM_NS_SIZE (SYSRAM_SIZE - SYSRAM_SEC_SIZE) +@@ -170,4 +301,59 @@ + #define SYSRAM_SEC_SIZE SYSRAM_SIZE + #endif + ++/* RETRAM layout */ ++#define RETRAM_SIZE U(0x10000) ++ ++/* MCUSRAM layout */ ++#define MCUSRAM_SIZE U(0x60000) ++ ++/* SoC part numbers and revisions */ ++#define STM32MP135C_PART_NB U(0x05010000) ++#define STM32MP135A_PART_NB U(0x05010001) ++#define STM32MP133C_PART_NB U(0x050100C0) ++#define STM32MP133A_PART_NB U(0x050100C1) ++#define STM32MP131C_PART_NB U(0x050106C8) ++#define STM32MP131A_PART_NB U(0x050106C9) ++#define STM32MP135F_PART_NB U(0x05010800) ++#define STM32MP135D_PART_NB U(0x05010801) ++#define STM32MP133F_PART_NB U(0x050108C0) ++#define STM32MP133D_PART_NB U(0x050108C1) ++#define STM32MP131F_PART_NB U(0x05010EC8) ++#define STM32MP131D_PART_NB U(0x05010EC9) ++ ++#define STM32MP157C_PART_NB U(0x05000000) ++#define STM32MP157A_PART_NB U(0x05000001) ++#define STM32MP153C_PART_NB U(0x05000024) ++#define STM32MP153A_PART_NB U(0x05000025) ++#define STM32MP151C_PART_NB U(0x0500002E) ++#define STM32MP151A_PART_NB U(0x0500002F) ++#define STM32MP157F_PART_NB U(0x05000080) ++#define STM32MP157D_PART_NB U(0x05000081) ++#define STM32MP153F_PART_NB U(0x050000A4) ++#define STM32MP153D_PART_NB U(0x050000A5) ++#define STM32MP151F_PART_NB U(0x050000AE) ++#define STM32MP151D_PART_NB U(0x050000AF) ++ ++#ifdef CFG_STM32MP13 ++#define STM32MP1_CHIP_ID U(0x501) ++#define STM32MP1_REV_A U(0x00001000) ++#define STM32MP1_REV_Y U(0x00001003) ++#define STM32MP1_REV_Z U(0x00001001) ++#endif ++ ++#ifdef CFG_STM32MP15 ++#define STM32MP1_CHIP_ID U(0x500) ++#define STM32MP1_REV_A U(0x00001000) ++#define STM32MP1_REV_B U(0x00002000) ++#define STM32MP1_REV_Z U(0x00002001) ++#endif ++ ++#define STM32MP1_CHIP_DEFAULT_VERSION U(0) ++ ++/* OPP */ ++#define PLAT_OPP_ID1 1U ++#define PLAT_OPP_ID2 2U ++#define PLAT_MAX_OPP_NB 2U ++#define PLAT_MAX_PLLCFG_NB 6U ++ + #endif /*PLATFORM_CONFIG_H*/ +diff --git a/core/arch/arm/plat-stm32mp1/pm/context.c b/core/arch/arm/plat-stm32mp1/pm/context.c +new file mode 100644 +index 000000000..c9d26ee37 +--- /dev/null ++++ b/core/arch/arm/plat-stm32mp1/pm/context.c +@@ -0,0 +1,617 @@ ++// SPDX-License-Identifier: BSD-3-Clause ++/* ++ * Copyright (c) 2018-2021, STMicroelectronics - All Rights Reserved ++ * Copyright (c) 2017-2021, ARM Limited and Contributors. All rights reserved. ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#include "context.h" ++#include "power.h" ++ ++#define TRAINING_AREA_SIZE 64 ++ ++/* ++ * STANDBY_CONTEXT_MAGIC0: ++ * Context provides magic, resume entry, zq0cr0 zdata and DDR training buffer. ++ * ++ * STANDBY_CONTEXT_MAGIC1: ++ * Context provides MAGIC0 content and PLL1 dual OPP settings structure ++ * (86 bytes). ++ * ++ * STANDBY_CONTEXT_MAGIC2: ++ * Context provides MAGIC1 content, low power entry point, BL2 code start, end ++ * and BL2_END (102 bytes). And, only for STM32MP13, add MCE master key ++ * (16 bytes). ++ */ ++#define STANDBY_CONTEXT_MAGIC0 (0x0001 << 16) ++#define STANDBY_CONTEXT_MAGIC1 (0x0002 << 16) ++#define STANDBY_CONTEXT_MAGIC2 (0x0003 << 16) ++ ++#if CFG_STM32MP1_PM_CONTEXT_VERSION == 1 ++#define STANDBY_CONTEXT_MAGIC (STANDBY_CONTEXT_MAGIC0 | TRAINING_AREA_SIZE) ++#elif CFG_STM32MP1_PM_CONTEXT_VERSION == 2 ++#define STANDBY_CONTEXT_MAGIC (STANDBY_CONTEXT_MAGIC1 | TRAINING_AREA_SIZE) ++#elif CFG_STM32MP1_PM_CONTEXT_VERSION == 3 ++#define STANDBY_CONTEXT_MAGIC (STANDBY_CONTEXT_MAGIC2 | TRAINING_AREA_SIZE) ++#else ++#error Invalid value for CFG_STM32MP1_PM_CONTEXT_VERSION ++#endif ++ ++#if CFG_STM32MP1_PM_CONTEXT_VERSION >= 2 ++#if (PLAT_MAX_OPP_NB != 2) || (PLAT_MAX_PLLCFG_NB != 6) ++#error STANDBY_CONTEXT_MAGIC does not support expected PLL1 settings ++#endif ++ ++/* pll_settings structure size definitions (reference to clock driver) */ ++#define PLL1_SETTINGS_SIZE (((PLAT_MAX_OPP_NB * \ ++ (PLAT_MAX_PLLCFG_NB + 3)) + 1) * \ ++ sizeof(uint32_t)) ++#endif ++ ++#if CFG_STM32MP1_PM_CONTEXT_VERSION >= 3 && defined(CFG_STM32MP13) ++#define MCE_KEY_SIZE_IN_BYTES 16 ++#endif ++ ++/* ++ * Context saved in TEE RAM during lower power sequence. ++ * Can be allocated if to big for static allocation. ++ * ++ * @stgen_cnt_h: Upper 32bit of the STGEN counter ++ * @stgen_cnt_l: Lower 32bit of the STGEN counter ++ * @rtc: RTC time read at suspend ++ */ ++struct pm_context { ++ uint32_t stgen_cnt_h; ++ uint32_t stgen_cnt_l; ++ struct stm32_rtc_calendar rtc; ++}; ++ ++static struct pm_context plat_ctx; ++ ++/* ++ * Clocks used during PM sequences. Stores clock references ++ * saved at boot time for clk.h API functions. ++ */ ++struct pm_clocks { ++ struct clk *rtcapb; ++ struct clk *bkpsram; ++ struct clk *cryp1; ++ struct clk *rtc; ++}; ++ ++static struct pm_clocks pm_clocks; ++ ++/* ++ * BKPSRAM contains a mailbox used with early boot stages for resume sequence. ++ * The mailbox content data that must be restored before OP-TEE is resumed. ++ * ++ * @magic: magic value read by early boot stage for consistency ++ * @zq0cr0_zdata: DDRPHY configuration to be restored. ++ * @ddr_training_backup: DDR area saved at suspend and backed up at resume ++ */ ++struct pm_mailbox { ++ uint32_t magic; ++ uint32_t core0_resume_ep; ++ uint32_t zq0cr0_zdata; ++ uint8_t ddr_training_backup[TRAINING_AREA_SIZE]; ++#if CFG_STM32MP1_PM_CONTEXT_VERSION >= 2 ++ uint8_t pll1_settings[PLL1_SETTINGS_SIZE]; ++#endif ++#if CFG_STM32MP1_PM_CONTEXT_VERSION >= 3 ++ uint32_t low_power_ep; ++ uint32_t bl2_code_base; ++ uint32_t bl2_code_end; ++ uint32_t bl2_end; ++#ifdef CFG_STM32MP13 ++ uint8_t mce_mkey[MCE_KEY_SIZE_IN_BYTES]; ++#endif ++#endif ++}; ++ ++/* ++ * BKPSRAM contains OP-TEE resume instruction sequence which restores ++ * TEE RAM content. The BKPSRAM contains restoration materials ++ * (key, tag) and the resume entry point in restored TEE RAM. ++ */ ++static struct retram_resume_ctx *get_retram_resume_ctx(void) ++{ ++ vaddr_t bkpsram_base = stm32mp_bkpsram_base(); ++ vaddr_t context_base = bkpsram_base + BKPSRAM_PM_CONTEXT_OFFSET; ++ ++ return (struct retram_resume_ctx *)context_base; ++} ++ ++static struct pm_mailbox *get_pm_mailbox(void) ++{ ++ vaddr_t bkpsram_base = stm32mp_bkpsram_base(); ++ vaddr_t mailbox_base = bkpsram_base + BKPSRAM_PM_MAILBOX_OFFSET; ++ ++ return (struct pm_mailbox *)mailbox_base; ++} ++ ++#if TRACE_LEVEL >= TRACE_DEBUG ++static void __maybe_unused dump_context(void) ++{ ++ struct pm_mailbox *mailbox = get_pm_mailbox(); ++ struct retram_resume_ctx *ctx = get_retram_resume_ctx(); ++ ++ clk_enable(pm_clocks.rtcapb); ++ ++ DMSG("Backup registers: address 0x%" PRIx32 ", magic 0x%" PRIx32, ++ *(uint32_t *)stm32mp_bkpreg(BCKR_CORE1_BRANCH_ADDRESS), ++ *(uint32_t *)stm32mp_bkpreg(BCKR_CORE1_MAGIC_NUMBER)); ++ ++ clk_disable(pm_clocks.rtcapb); ++ ++ clk_enable(pm_clocks.bkpsram); ++ ++ DMSG("BKPSRAM mailbox: 0x%" PRIx32 ", zd 0x%" PRIx32 ", ep 0x%" PRIx32, ++ mailbox->magic, mailbox->zq0cr0_zdata, ++ mailbox->core0_resume_ep); ++ ++ DMSG("BKPSRAM context: teeram backup @%" PRIx32 ", resume @0x%" PRIx32, ++ ctx->teeram_bkp_pa, ctx->resume_pa); ++ ++ clk_disable(pm_clocks.bkpsram); ++} ++#else ++static void __maybe_unused dump_context(void) ++{ ++} ++#endif ++ ++/* ++ * Save and restore functions ++ */ ++static void save_time(void) ++{ ++ vaddr_t stgen = stm32mp_stgen_base(); ++ ++ plat_ctx.stgen_cnt_h = io_read32(stgen + CNTCVU_OFFSET); ++ plat_ctx.stgen_cnt_l = io_read32(stgen + CNTCVL_OFFSET); ++ if (plat_ctx.stgen_cnt_l < 10) ++ plat_ctx.stgen_cnt_h = io_read32(stgen + CNTCVU_OFFSET); ++ ++ clk_enable(pm_clocks.rtc); ++ ++ stm32_rtc_get_calendar(&plat_ctx.rtc); ++} ++ ++#if TRACE_LEVEL >= TRACE_DEBUG ++static void __maybe_unused print_ccm_decryption_duration(void) ++{ ++ vaddr_t stgen = stm32mp_stgen_base(); ++ struct retram_resume_ctx *ctx = get_retram_resume_ctx(); ++ ++ clk_enable(pm_clocks.bkpsram); ++ ++ DMSG("CCM decryption duration %llums", ++ ((unsigned long long)ctx->stgen_cnt * 1000) / ++ io_read32(stgen + CNTFID_OFFSET)); ++ ++ clk_enable(pm_clocks.bkpsram); ++} ++#else ++static void __maybe_unused print_ccm_decryption_duration(void) ++{ ++} ++#endif ++ ++static void restore_time(void) ++{ ++ struct stm32_rtc_calendar current_calendar = { }; ++ unsigned long long stdby_time_in_ms = 0; ++ unsigned long long cnt = 0; ++ vaddr_t stgen = stm32mp_stgen_base(); ++ struct retram_resume_ctx __maybe_unused *ctx = get_retram_resume_ctx(); ++ ++ stm32_rtc_get_calendar(¤t_calendar); ++ stdby_time_in_ms = stm32_rtc_diff_calendar(¤t_calendar, ++ &plat_ctx.rtc); ++ ++ cnt = ((uint64_t)plat_ctx.stgen_cnt_h << 32) | plat_ctx.stgen_cnt_l; ++ cnt += (stdby_time_in_ms * io_read32(stgen + CNTFID_OFFSET)) / 1000U; ++ ++ io_clrbits32(stgen + CNTCR_OFFSET, CNTCR_EN); ++ io_write32(stgen + CNTCVL_OFFSET, (uint32_t)cnt); ++ io_write32(stgen + CNTCVU_OFFSET, (uint32_t)(cnt >> 32)); ++ io_setbits32(stgen + CNTCR_OFFSET, CNTCR_EN); ++ ++ /* Balance clock enable(RTC) at save_time() */ ++ clk_disable(pm_clocks.rtc); ++ ++#ifndef CFG_STM32MP13 ++ print_ccm_decryption_duration(); ++#endif ++} ++ ++static bool __maybe_unused pm_cb_is_valid(void (*cb)(enum pm_op op, void *hdl), ++ void *hdl) ++{ ++ void *cb_voidp = (void *)(vaddr_t)cb; ++ paddr_t cb_phy = virt_to_phys(cb_voidp); ++ paddr_t hdl_phy = virt_to_phys(hdl); ++ bool valid = false; ++ ++ valid = (phys_to_virt(cb_phy, MEM_AREA_TEE_RAM_RX, 1) == cb_voidp) && ++ ((phys_to_virt(hdl_phy, MEM_AREA_TEE_RAM_RX, 1) == hdl) || ++ (phys_to_virt(hdl_phy, MEM_AREA_TEE_RAM_RO, 1) == hdl) || ++ (phys_to_virt(hdl_phy, MEM_AREA_TEE_RAM_RW, 1) == hdl)); ++ ++ if (!valid) ++ EMSG("pm_cb mandates unpaged arguments %p %p", cb_voidp, hdl); ++ ++ return valid; ++} ++ ++uintptr_t stm32mp_pm_retram_resume_ep(void) ++{ ++ struct retram_resume_ctx *ctx = get_retram_resume_ctx(); ++ ++ return (uintptr_t)&ctx->resume_sequence; ++} ++ ++/* Clear the content of the PM mailbox */ ++void stm32mp_pm_wipe_context(void) ++{ ++ struct retram_resume_ctx *ctx = get_retram_resume_ctx(); ++ struct pm_mailbox __maybe_unused *mailbox = get_pm_mailbox(); ++ ++ clk_enable(pm_clocks.bkpsram); ++ ++ memset(ctx, 0xa5, sizeof(*ctx)); ++#ifndef CFG_STM32MP13 ++ memset(mailbox, 0xa5, sizeof(*mailbox)); ++#endif ++ ++ clk_disable(pm_clocks.bkpsram); ++} ++ ++static struct mobj *teeram_bkp_mobj; ++ ++static void init_retram_resume_resources(void) ++{ ++ struct retram_resume_ctx *ctx = get_retram_resume_ctx(); ++ size_t __maybe_unused csize = 0; ++ paddr_t __maybe_unused pa = 0; ++ ++ COMPILE_TIME_ASSERT(sizeof(struct pm_mailbox) < ++ BKPSRAM_PM_MAILBOX_SIZE); ++ COMPILE_TIME_ASSERT(sizeof(struct retram_resume_ctx) < ++ BKPSRAM_PM_CONTEXT_SIZE); ++ csize = (vaddr_t)stm32mp_bkpsram_image_end - ++ (vaddr_t)stm32mp_bkpsram_resume; ++ assert((sizeof(*ctx) + csize) < BKPSRAM_PM_CONTEXT_SIZE); ++ ++ teeram_bkp_mobj = mobj_mm_alloc(mobj_sec_ddr, TEE_RAM_PH_SIZE, ++ &tee_mm_sec_ddr); ++ if (!teeram_bkp_mobj) ++ panic(); ++ ++ assert((mobj_get_va(teeram_bkp_mobj, 0, TEE_RAM_PH_SIZE) != NULL) && ++ (mobj_get_pa(teeram_bkp_mobj, 0, 0, &pa) == 0)); ++ ++ clk_enable(pm_clocks.bkpsram); ++ memset(ctx, 0, sizeof(*ctx)); ++ clk_disable(pm_clocks.bkpsram); ++} ++ ++/* ++ * When returning from STANDBY, the 64 first bytes of DDR will be overwritten ++ * during DDR DQS training. This area must then be saved before going to ++ * standby in the PM mailbox with the earlier boot stages. ++ */ ++__maybe_unused static void save_ddr_training_area(void) ++{ ++ struct pm_mailbox *mailbox = get_pm_mailbox(); ++ size_t size = sizeof(mailbox->ddr_training_backup); ++ paddr_t pa = DDR_BASE; ++ void *va = phys_to_virt(pa, MEM_AREA_RAM_NSEC, SMALL_PAGE_SIZE); ++ ++ memcpy(&mailbox->ddr_training_backup[0], va, size); ++ ++} ++ ++/* ++ * When returning from STANDBY, warm boot boot stage needs to access to PLL1 ++ * settings. This avoids to re-compute them and optimizes performances. This ++ * structure must then be saved before going to STANDBY in the PM mailbox ++ * shared with the warm boot boot stage. ++ */ ++#if CFG_STM32MP1_PM_CONTEXT_VERSION >= 2 ++__maybe_unused static void save_pll1_settings(void) ++{ ++ struct pm_mailbox *mailbox = get_pm_mailbox(); ++ size_t size = sizeof(mailbox->pll1_settings); ++ uint8_t *data = &mailbox->pll1_settings[0]; ++ ++ stm32mp1_clk_lp_save_opp_pll1_settings(data, size); ++} ++#endif ++ ++static void load_earlyboot_pm_mailbox(void) ++{ ++ struct pm_mailbox *mailbox __maybe_unused = get_pm_mailbox(); ++ ++ COMPILE_TIME_ASSERT(sizeof(struct pm_mailbox) < ++ BKPSRAM_PM_MAILBOX_SIZE); ++ ++ assert(clk_is_enabled(pm_clocks.bkpsram)); ++ ++#ifdef CFG_STM32MP15 ++ memset(mailbox, 0, sizeof(*mailbox)); ++ ++ mailbox->zq0cr0_zdata = get_ddrphy_calibration(); ++ ++#if CFG_STM32MP1_PM_CONTEXT_VERSION >= 2 ++ save_pll1_settings(); ++#endif ++#endif /*CFG_STM32MP15*/ ++ ++ save_ddr_training_area(); ++} ++ ++#if defined(CFG_STM32_CRYP) && defined(CFG_STM32MP15) ++/* ++ * CRYP relies on standard format for CCM IV/B0/CRT0 data. Our sequence uses ++ * no AAD, 4 bytes to encode the payload byte size and a 11 byte nonce. ++ */ ++#define PM_CCM_Q 4 ++#define PM_CCM_Q_FLAGS (PM_CCM_Q - 1) ++#define PM_CCM_TAG_LEN 16 ++#define PM_CCM_TAG_FLAGS (((PM_CCM_TAG_LEN - 2) / 2) << 3) ++ ++static void save_teeram_in_ddr(void) ++{ ++ struct retram_resume_ctx *ctx = get_retram_resume_ctx(); ++ size_t __maybe_unused size = (vaddr_t)stm32mp_bkpsram_image_end - ++ (vaddr_t)stm32mp_bkpsram_resume; ++ paddr_t pa = 0; ++ struct ccm_unpg_ctx *ccm = &ctx->ccm_ctx; ++ void *teeram = phys_to_virt(TEE_RAM_START, MEM_AREA_ROM_SEC, TEE_RAM_PH_SIZE); ++ void *teeram_bkp = mobj_get_va(teeram_bkp_mobj, 0, TEE_RAM_PH_SIZE); ++ ++ COMPILE_TIME_ASSERT(PM_CTX_CCM_KEY_SIZE == sizeof(ccm->key)); ++ COMPILE_TIME_ASSERT(PM_CTX_CCM_CTR1_SIZE == sizeof(ccm->ctr1)); ++ COMPILE_TIME_ASSERT(PM_CTX_CCM_B0_SIZE == sizeof(ccm->b0)); ++ COMPILE_TIME_ASSERT(PM_CTX_CCM_CTR0_SIZE == sizeof(ccm->ctr0)); ++ COMPILE_TIME_ASSERT(PM_CTX_CCM_TAG_SIZE == sizeof(ccm->tag)); ++ ++ if (!stm32mp_supports_hw_cryp()) ++ panic(); ++ ++ assert(clk_is_enabled(pm_clocks.bkpsram) && ++ clk_is_enabled(pm_clocks.cryp1)); ++ ++ memcpy(ctx->resume_sequence, ++ (void *)(vaddr_t)stm32mp_bkpsram_resume, size); ++ ++ memset(ctx, 0, sizeof(*ctx)); ++ ctx->resume_pa = virt_to_phys((void *)(vaddr_t)stm32mp_sysram_resume); ++ if (mobj_get_pa(teeram_bkp_mobj, 0, 0, &pa)) ++ panic(); ++ ++ ctx->teeram_bkp_pa = (uint32_t)pa; ++ ctx->cryp1_base = (uint32_t)phys_to_virt(CRYP1_BASE, MEM_AREA_IO_SEC, 1); ++ ctx->rcc_base = (uint32_t)phys_to_virt(RCC_BASE, MEM_AREA_IO_SEC, 1); ++ ctx->stgen_base = (uint32_t)phys_to_virt(STGEN_BASE, MEM_AREA_IO_SEC, 1); ++ ++ if (crypto_rng_read((uint8_t *)ccm->key, sizeof(ccm->key))) ++ panic(); ++ ++ assert(((PM_CCM_TAG_FLAGS & ~0x38U) | (PM_CCM_Q_FLAGS & ~0x07U)) == 0); ++ COMPILE_TIME_ASSERT(PM_CCM_Q <= 4); ++ COMPILE_TIME_ASSERT(TEE_RAM_PH_SIZE > UINT16_MAX); ++ COMPILE_TIME_ASSERT(TEE_RAM_PH_SIZE < UINT32_MAX); ++ ++ if (crypto_rng_read((uint8_t *)ccm->ctr1, sizeof(ccm->ctr1))) ++ panic(); ++ ++ ccm->ctr1[0] &= GENMASK_32(24, 0); ++ memcpy(ccm->b0, ccm->ctr1, sizeof(ccm->b0)); ++ memcpy(ccm->ctr0, ccm->ctr1, sizeof(ccm->ctr0)); ++ ++ ccm->ctr0[0] |= PM_CCM_Q_FLAGS << 24; ++ ccm->ctr0[3] = 0; ++ ccm->ctr1[0] |= PM_CCM_Q_FLAGS << 24; ++ ccm->ctr1[3] = 1; ++ ccm->b0[0] |= (PM_CCM_Q_FLAGS | PM_CCM_TAG_FLAGS) << 24; ++ ccm->b0[3] = TEE_RAM_PH_SIZE; ++ ++ stm32mp_ccm_encrypt_teeram(ctx, teeram_bkp, teeram, TEE_RAM_PH_SIZE); ++ dcache_clean_range(teeram_bkp, TEE_RAM_PH_SIZE); ++ ++ memcpy(ctx->ccm_ref_tag, ccm->tag, sizeof(ctx->ccm_ref_tag)); ++ ++ DMSG("CCM encryption duration %llums", ++ ((unsigned long long)ctx->stgen_cnt * 1000) / ++ io_read32(ctx->stgen_base + CNTFID_OFFSET)); ++ ctx->stgen_cnt = 0; ++} ++#else ++static void __maybe_unused save_teeram_in_ddr(void) ++{ ++ panic("Mandates CRYP support"); ++} ++#endif /* CFG_STM32_CRYP && CFG_STM32MP15 */ ++ ++/* Finalize the PM mailbox now that everything is loaded */ ++static void enable_pm_mailbox(unsigned int suspend) ++{ ++ struct pm_mailbox *mailbox = get_pm_mailbox(); ++ uint32_t magic = 0; ++ uint32_t hint = 0; ++ ++ assert(clk_is_enabled(pm_clocks.bkpsram) && ++ clk_is_enabled(pm_clocks.rtcapb)); ++ ++ if (suspend) { ++ magic = BOOT_API_A7_CORE0_MAGIC_NUMBER; ++ mailbox->magic = STANDBY_CONTEXT_MAGIC; ++ ++#ifdef CFG_STM32MP13 ++ hint = virt_to_phys(stm32mp_sysram_resume); ++#else ++ hint = virt_to_phys(&get_retram_resume_ctx()->resume_sequence); ++#endif ++ } else { ++ mailbox->magic = 0; ++ } ++ ++ io_write32(stm32mp_bkpreg(BCKR_CORE1_MAGIC_NUMBER), magic); ++ io_write32(stm32mp_bkpreg(BCKR_CORE1_BRANCH_ADDRESS), hint); ++ ++ mailbox->core0_resume_ep = hint; ++} ++ ++static void gate_pm_context_clocks(bool enable) ++{ ++ static bool clocks_enabled; ++ ++ if (enable) { ++ assert(!clocks_enabled); ++ clk_enable(pm_clocks.bkpsram); ++ clk_enable(pm_clocks.rtcapb); ++ clk_enable(pm_clocks.cryp1); ++ clocks_enabled = true; ++ return; ++ } ++ ++ /* Suspended TEE RAM state left the clocks enabled */ ++ if (clocks_enabled) { ++ clk_disable(pm_clocks.bkpsram); ++ clk_disable(pm_clocks.rtcapb); ++ clk_disable(pm_clocks.cryp1); ++ clocks_enabled = false; ++ } ++} ++ ++/* ++ * Context (TEE RAM content + peripherals) must be restored ++ * only if system may reach STANDBY state. ++ */ ++TEE_Result stm32mp_pm_save_context(unsigned int soc_mode) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ ++ save_time(); ++ ++ res = pm_change_state(PM_OP_SUSPEND, soc_mode); ++ if (res) ++ return res; ++ ++ if (!need_to_backup_cpu_context(soc_mode)) { ++#ifndef CFG_STM32MP13 ++ if (need_to_backup_stop_context(soc_mode)) ++ stm32mp1_clk_save_context_for_stop(); ++#endif ++ ++ return TEE_SUCCESS; ++ } ++ ++ gate_pm_context_clocks(true); ++ load_earlyboot_pm_mailbox(); ++ ++#ifndef CFG_STM32MP13 ++ save_teeram_in_ddr(); ++#endif ++ enable_pm_mailbox(1); ++ ++ return TEE_SUCCESS; ++} ++ ++void stm32mp_pm_restore_context(unsigned int soc_mode) ++{ ++ if (need_to_backup_cpu_context(soc_mode)) ++ gate_pm_context_clocks(false); ++#ifndef CFG_STM32MP13 ++ else if (need_to_backup_stop_context(soc_mode)) ++ stm32mp1_clk_restore_context_for_stop(); ++#endif ++ ++ if (pm_change_state(PM_OP_RESUME, 0)) ++ panic(); ++ ++ restore_time(); ++} ++ ++void stm32mp_pm_shutdown_context(void) ++{ ++ gate_pm_context_clocks(true); ++ load_earlyboot_pm_mailbox(); ++ enable_pm_mailbox(0); ++ gate_pm_context_clocks(false); ++} ++ ++TEE_Result stm32mp_pm_call_bl2_lp_entry(unsigned int soc_mode) ++{ ++ struct pm_mailbox *mailbox; ++ void (*stm32_pwr_down_wfi)(bool is_cstop, unsigned int soc_mode); ++ ++ clk_enable(pm_clocks.bkpsram); ++ ++ mailbox = get_pm_mailbox(); ++ ++ FMSG("BL2 low_power_ep %x", mailbox->low_power_ep); ++ ++ stm32_pwr_down_wfi = (void *)mailbox->low_power_ep; ++ ++ dcache_op_all(DCACHE_OP_CLEAN_INV); ++ ++ dsb(); ++ ++ /* Disable MMU before calling low_power section */ ++ write_sctlr(read_sctlr() & ~SCTLR_M); ++ ++ (*stm32_pwr_down_wfi)(true, soc_mode); ++ ++ /* Enable MMU */ ++ write_sctlr(read_sctlr() | SCTLR_M); ++ ++ clk_disable(pm_clocks.bkpsram); ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result init_pm_support(void) ++{ ++ pm_clocks.rtcapb = stm32mp_rcc_clock_id_to_clk(RTCAPB); ++ pm_clocks.bkpsram = stm32mp_rcc_clock_id_to_clk(BKPSRAM); ++ pm_clocks.cryp1 = stm32mp_rcc_clock_id_to_clk(CRYP1); ++ pm_clocks.rtc = stm32mp_rcc_clock_id_to_clk(RTC); ++ ++ if (!pm_clocks.rtcapb || !pm_clocks.bkpsram || ++ !pm_clocks.cryp1 || !pm_clocks.rtc) ++ panic(); ++ ++ init_retram_resume_resources(); ++ ++ stm32mp_pm_wipe_context(); ++ ++ return TEE_SUCCESS; ++} ++driver_init(init_pm_support); +diff --git a/core/arch/arm/plat-stm32mp1/pm/context.h b/core/arch/arm/plat-stm32mp1/pm/context.h +new file mode 100644 +index 000000000..2ac9ba62f +--- /dev/null ++++ b/core/arch/arm/plat-stm32mp1/pm/context.h +@@ -0,0 +1,105 @@ ++/* SPDX-License-Identifier: BSD-3-Clause */ ++/* ++ * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved. ++ */ ++ ++#ifndef __STM32MP_PM_CONTEXT_H__ ++#define __STM32MP_PM_CONTEXT_H__ ++ ++#ifndef __ASSEMBLER__ ++#include ++#include ++#include ++#include ++#include ++#include ++#endif ++ ++#define PM_CTX_CCM_KEY_SIZE 32 ++#define PM_CTX_CCM_CTR1_SIZE 16 ++#define PM_CTX_CCM_B0_SIZE 16 ++#define PM_CTX_CCM_CTR0_SIZE 16 ++#define PM_CTX_CCM_TAG_SIZE 16 ++ ++#ifndef __ASSEMBLER__ ++/* ++ * All materials for the CCM sequence using CRYP support are preloaded ++ * in this specific structure. Note that the sequence does not use AAD. ++ * ++ * @key: AES key material buffer ++ * @ctr1: Preformatted 128bit CTR1 block ++ * @ctr1: Preformatted 128bit B0 block ++ * @ctr1: Preformatted 128bit CTR0 block ++ * @tag: Buffer where the generated CCM tag is stored ++ */ ++struct ccm_unpg_ctx { ++ uint32_t key[PM_CTX_CCM_KEY_SIZE / sizeof(uint32_t)]; ++ uint32_t ctr1[PM_CTX_CCM_CTR1_SIZE / sizeof(uint32_t)]; ++ uint32_t b0[PM_CTX_CCM_B0_SIZE / sizeof(uint32_t)]; ++ uint32_t ctr0[PM_CTX_CCM_CTR0_SIZE / sizeof(uint32_t)]; ++ uint32_t tag[PM_CTX_CCM_TAG_SIZE / sizeof(uint32_t)]; ++}; ++ ++/* ++ * This structure is used by pm_helpers.S at early resume from retention RAM. ++ * It is defined here and used by context_asm_defines.c to generate offset ++ * macros for the assembly implementation in pm_helpers.S. ++ * ++ * To lower the memory footprint of suspend sequence, The same function is ++ * used for encryption (executed from TEE RAM with MMU enabled) and for ++ * decryption (executed from BKPSRAM with MMU disabled). Therefore some ++ * required addresses are provided by the caller through this structure ++ * especially some SoC interface registers that are likely to have different ++ * physical and virtual addresses. ++ * ++ * @resume_pa: OP-TEE resume physical entry in TEE RAM (once restored) ++ * @teeram_bkp_pa: Physical base address in TEE RAM backup in DDR ++ * @cryp1_base: Base address of the CRYP1 registers (physical or virtual) ++ * @rcc_base: Base address of the RCC registers (physical or virtual) ++ * @stgen_base: Base address of the STGEN registers (physical or virtual) ++ * @stgen_cnt: STGEN cycle counter backup cell and measure of cycles spent ++ * @ccm_ref_tag: 128bit arrays storing tag generated during encryption ++ * @ccm_ctx: Structure storing CCM configuration and generated tag ++ * @resume_sequence: Code/data array for the BKPSRAM resume sequence ++ */ ++struct retram_resume_ctx { ++ uint32_t resume_pa; ++ uint32_t teeram_bkp_pa; ++ uint32_t cryp1_base; ++ uint32_t rcc_base; ++ uint32_t stgen_base; ++ uint32_t stgen_cnt; ++ uint8_t ccm_ref_tag[PM_CTX_CCM_TAG_SIZE]; ++ struct ccm_unpg_ctx ccm_ctx; ++ /* Last start the resume routine ARM (32bit) instructions sequence */ ++ uint32_t resume_sequence[]; ++}; ++ ++extern const uint8_t stm32mp_bkpsram_image_end[]; ++void stm32mp_bkpsram_resume(void); ++void stm32mp_sysram_resume(void); ++ ++void stm32mp_cpu_reset_state(void); ++ ++TEE_Result stm32mp_pm_save_context(unsigned int soc_mode); ++void stm32mp_pm_restore_context(unsigned int soc_mode); ++void stm32mp_pm_shutdown_context(void); ++void stm32mp_pm_wipe_context(void); ++ ++TEE_Result stm32mp_pm_call_bl2_lp_entry(unsigned int soc_mode); ++ ++int stm32mp1_set_pm_domain_state(enum stm32mp1_pm_domain domain, bool status); ++ ++uint32_t stm32mp1_get_lp_soc_mode(uint32_t psci_mode); ++int stm32mp1_set_lp_deepest_soc_mode(uint32_t psci_mode, uint32_t soc_mode); ++ ++uintptr_t stm32mp_pm_retram_resume_ep(void); ++bool stm32mp1_is_retram_during_standby(void); ++ ++int stm32mp_ccm_encrypt_teeram(struct retram_resume_ctx *ctx, ++ void *dst, void *src, size_t size); ++int stm32mp_ccm_decrypt_teeram(struct retram_resume_ctx *ctx, ++ void *dst, void *src, size_t size); ++#endif /*__ASSEMBLER__*/ ++ ++#endif /*__STM32MP_PM_CONTEXT_H__*/ +diff --git a/core/arch/arm/plat-stm32mp1/pm/context_asm_defines.c b/core/arch/arm/plat-stm32mp1/pm/context_asm_defines.c +new file mode 100644 +index 000000000..34c797f52 +--- /dev/null ++++ b/core/arch/arm/plat-stm32mp1/pm/context_asm_defines.c +@@ -0,0 +1,28 @@ ++// SPDX-License-Identifier: BSD-3-Clause ++/* ++ * Copyright (c) 2018, STMicroelectronics ++ * Copyright (c) 2018, Linaro Limited ++ */ ++ ++#include ++ ++#include "context.h" ++ ++#define OFFSET_OF_CTX_STRUCT(_f) offsetof(struct retram_resume_ctx, _f) ++#define OFFSET_OF_CMM_CTX_STRUCT(_f) (OFFSET_OF_CTX_STRUCT(ccm_ctx) + \ ++ offsetof(struct ccm_unpg_ctx, _f)) ++DEFINES ++{ ++ DEFINE(PM_CTX_RESUME_PA, OFFSET_OF_CTX_STRUCT(resume_pa)); ++ DEFINE(PM_CTX_TEERAM_BKP_PA, OFFSET_OF_CTX_STRUCT(teeram_bkp_pa)); ++ DEFINE(PM_CTX_CRYP1_BASE, OFFSET_OF_CTX_STRUCT(cryp1_base)); ++ DEFINE(PM_CTX_RCC_BASE, OFFSET_OF_CTX_STRUCT(rcc_base)); ++ DEFINE(PM_CTX_STGEN_BASE, OFFSET_OF_CTX_STRUCT(stgen_base)); ++ DEFINE(PM_CTX_STGEN_CNT, OFFSET_OF_CTX_STRUCT(stgen_cnt)); ++ DEFINE(PM_CTX_CCM_KEY, OFFSET_OF_CMM_CTX_STRUCT(key)); ++ DEFINE(PM_CTX_CCM_CTR1, OFFSET_OF_CMM_CTX_STRUCT(ctr1)); ++ DEFINE(PM_CTX_CCM_B0, OFFSET_OF_CMM_CTX_STRUCT(b0)); ++ DEFINE(PM_CTX_CCM_CTR0, OFFSET_OF_CMM_CTX_STRUCT(ctr0)); ++ DEFINE(PM_CTX_CCM_TAG, OFFSET_OF_CMM_CTX_STRUCT(tag)); ++ DEFINE(PM_CTX_CCM_REF_TAG, OFFSET_OF_CTX_STRUCT(ccm_ref_tag)); ++} +diff --git a/core/arch/arm/plat-stm32mp1/pm/low_power.c b/core/arch/arm/plat-stm32mp1/pm/low_power.c +new file mode 100644 +index 000000000..cbc5fa151 +--- /dev/null ++++ b/core/arch/arm/plat-stm32mp1/pm/low_power.c +@@ -0,0 +1,559 @@ ++// SPDX-License-Identifier: BSD-3-Clause ++/* ++ * Copyright (c) 2017-2022, STMicroelectronics - All Rights Reserved ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#ifdef CFG_STM32MP13 ++#include ++#else ++#include ++#endif ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#include "context.h" ++#include "power.h" ++ ++#define TIMEOUT_US_1MS 1000 ++ ++#define PWRLP_TEMPO_5_HSI 5 ++ ++static uint8_t gicd_rcc_wakeup; ++static uint8_t gicc_pmr; ++ ++struct pwr_lp_config { ++ uint32_t pwr_cr1; ++ uint32_t pwr_mpucr; ++ const char *regul_suspend_node_name; ++}; ++ ++#ifdef CFG_STM32MP13 ++#define PWR_CR1_MASK (PWR_CR1_LPDS | PWR_CR1_LPCFG | PWR_CR1_LVDS |\ ++ PWR_CR1_STOP2) ++#define PWR_MPUCR_MASK (PWR_MPUCR_CSSF | PWR_MPUCR_PDDS) ++ ++static const struct pwr_lp_config config_pwr[STM32_PM_MAX_SOC_MODE] = { ++ [STM32_PM_CSLEEP_RUN] = { ++ .pwr_cr1 = 0U, ++ .pwr_mpucr = 0U, ++ .regul_suspend_node_name = NULL, ++ }, ++ [STM32_PM_CSTOP_ALLOW_STOP] = { ++ .pwr_cr1 = 0U, ++ .pwr_mpucr = PWR_MPUCR_CSSF, ++ .regul_suspend_node_name = NULL, ++ }, ++ [STM32_PM_CSTOP_ALLOW_LP_STOP] = { ++ .pwr_cr1 = PWR_CR1_LPDS, ++ .pwr_mpucr = PWR_MPUCR_CSSF, ++ .regul_suspend_node_name = "lp-stop", ++ }, ++ [STM32_PM_CSTOP_ALLOW_LPLV_STOP] = { ++ .pwr_cr1 = PWR_CR1_LVDS | PWR_CR1_LPDS, ++ .pwr_mpucr = PWR_MPUCR_CSSF, ++ .regul_suspend_node_name = "lplv-stop", ++ }, ++ [STM32_PM_CSTOP_ALLOW_LPLV_STOP2] = { ++ .pwr_cr1 = PWR_CR1_LVDS | PWR_CR1_LPDS | PWR_CR1_STOP2, ++ .pwr_mpucr = PWR_MPUCR_CSSF, ++ .regul_suspend_node_name = "lplv-stop2", ++ }, ++ [STM32_PM_CSTOP_ALLOW_STANDBY_DDR_SR] = { ++ .pwr_cr1 = 0U, ++ .pwr_mpucr = PWR_MPUCR_CSSF | ++ PWR_MPUCR_PDDS, ++ .regul_suspend_node_name = "standby-ddr-sr", ++ }, ++ [STM32_PM_CSTOP_ALLOW_STANDBY_DDR_OFF] = { ++ .pwr_cr1 = 0U, ++ .pwr_mpucr = PWR_MPUCR_CSSF | ++ PWR_MPUCR_PDDS, ++ .regul_suspend_node_name = "standby-ddr-off", ++ }, ++ [STM32_PM_SHUTDOWN] = { ++ .pwr_cr1 = 0U, ++ .pwr_mpucr = 0U, ++ .regul_suspend_node_name = "standby-ddr-off", ++ }, ++}; ++#else ++#define PWR_CR1_MASK (PWR_CR1_LPDS | PWR_CR1_LPCFG | PWR_CR1_LVDS) ++#define PWR_MPUCR_MASK (PWR_MPUCR_CSTDBYDIS | PWR_MPUCR_CSSF | PWR_MPUCR_PDDS) ++ ++static const struct pwr_lp_config config_pwr[STM32_PM_MAX_SOC_MODE] = { ++ [STM32_PM_CSLEEP_RUN] = { ++ .pwr_cr1 = 0U, ++ .pwr_mpucr = 0U, ++ .regul_suspend_node_name = NULL, ++ }, ++ [STM32_PM_CSTOP_ALLOW_STOP] = { ++ .pwr_cr1 = 0U, ++ .pwr_mpucr = PWR_MPUCR_CSTDBYDIS | PWR_MPUCR_CSSF, ++ .regul_suspend_node_name = NULL, ++ }, ++ [STM32_PM_CSTOP_ALLOW_LP_STOP] = { ++ .pwr_cr1 = PWR_CR1_LPDS, ++ .pwr_mpucr = PWR_MPUCR_CSTDBYDIS | PWR_MPUCR_CSSF, ++ .regul_suspend_node_name = "lp-stop", ++ }, ++ [STM32_PM_CSTOP_ALLOW_LPLV_STOP] = { ++ .pwr_cr1 = PWR_CR1_LVDS | PWR_CR1_LPDS, ++ .pwr_mpucr = PWR_MPUCR_CSTDBYDIS | PWR_MPUCR_CSSF, ++ .regul_suspend_node_name = "lplv-stop", ++ }, ++ [STM32_PM_CSTOP_ALLOW_STANDBY_DDR_SR] = { ++ .pwr_cr1 = 0U, ++ .pwr_mpucr = PWR_MPUCR_CSTDBYDIS | PWR_MPUCR_CSSF | ++ PWR_MPUCR_PDDS, ++ .regul_suspend_node_name = "standby-ddr-sr", ++ }, ++ [STM32_PM_CSTOP_ALLOW_STANDBY_DDR_OFF] = { ++ .pwr_cr1 = 0U, ++ .pwr_mpucr = PWR_MPUCR_CSTDBYDIS | PWR_MPUCR_CSSF | ++ PWR_MPUCR_PDDS, ++ .regul_suspend_node_name = "standby-ddr-off", ++ }, ++ [STM32_PM_SHUTDOWN] = { ++ .pwr_cr1 = 0U, ++ .pwr_mpucr = 0U, ++ .regul_suspend_node_name = "standby-ddr-off", ++ }, ++}; ++#endif ++ ++static void set_rcc_it_priority(uint8_t *it_prio, uint8_t *pmr) ++{ ++ *it_prio = itr_set_ipriority(RCC_WAKEUP_IT, GIC_HIGHEST_SEC_PRIORITY); ++ *pmr = itr_set_pmr(STM32MP_GIC_PRIORITY_CSTOP); ++} ++ ++static void restore_rcc_it_priority(uint8_t it_prio, uint8_t pmr) ++{ ++ (void)itr_set_ipriority(RCC_WAKEUP_IT, it_prio); ++ (void)itr_set_pmr(pmr); ++} ++ ++const char *stm32mp_pm_hint2mode_name(uint32_t pm_hint) ++{ ++ assert(pm_hint < ARRAY_SIZE(config_pwr)); ++ ++ return config_pwr[pm_hint].regul_suspend_node_name; ++} ++ ++const char *plat_get_lp_mode_name(int mode) ++{ ++ return stm32mp_pm_hint2mode_name(mode); ++} ++ ++#define CONSOLE_FLUSH_DELAY_MS 10 ++ ++#if TRACE_LEVEL >= TRACE_DEBUG ++static void wait_console_flushed(void) ++{ ++ console_flush(); ++ mdelay(CONSOLE_FLUSH_DELAY_MS); ++} ++#else ++static void wait_console_flushed(void) ++{ ++} ++#endif ++ ++static void cpu_wfi(void) ++{ ++ dsb(); ++ isb(); ++ wfi(); ++} ++ ++void stm32_pm_cpu_wfi(void) ++{ ++ wait_console_flushed(); ++ cpu_wfi(); ++} ++ ++/* If IWDG is not supported, provide a stubbed weak watchdog kicker */ ++void __weak stm32_iwdg_refresh(void) ++{ ++} ++ ++#define ARM_CNTXCTL_IMASK BIT(1) ++ ++static void stm32mp_mask_timer(void) ++{ ++ /* Mask timer interrupts */ ++ write_cntp_ctl(read_cntp_ctl() | ARM_CNTXCTL_IMASK); ++ write_cntv_ctl(read_cntv_ctl() | ARM_CNTXCTL_IMASK); ++} ++ ++/* ++ * stm32_enter_cstop - Prepare CSTOP mode ++ * ++ * @mode - Target low power mode ++ */ ++void stm32_enter_cstop(uint32_t mode) ++{ ++ uint32_t pwr_cr1 = config_pwr[mode].pwr_cr1; ++ vaddr_t pwr_base = stm32_pwr_base(); ++ vaddr_t rcc_base = stm32_rcc_base(); ++ ++#ifdef CFG_STM32MP15 ++ if (mode == STM32_PM_CSTOP_ALLOW_LPLV_STOP2) ++ panic("LPLV-Stop2 mode not supported"); ++ ++ /* Save Self-Refresh (SR) mode and switch to Software SR mode */ ++ ddr_save_sr_mode(DDR_SSR_MODE); ++#endif ++ ++ if (stm32mp_with_pmic() && (mode == STM32_PM_CSTOP_ALLOW_LP_STOP || ++ mode == STM32_PM_CSTOP_ALLOW_LPLV_STOP || ++ mode == STM32_PM_CSTOP_ALLOW_LPLV_STOP2)) ++ pwr_cr1 |= PWR_CR1_LPCFG; ++ ++ if (mode == STM32_PM_CSTOP_ALLOW_LPLV_STOP2) { ++ struct clk *hsi_clk = stm32mp_rcc_clock_id_to_clk(CK_HSI); ++ unsigned long freq = clk_get_rate(hsi_clk); ++ /* Wait for 2ms before re-enabling PLLs after LPLV-Stop2 mode */ ++ uint32_t dly = 2U * freq / 1000U; ++ ++ io_clrsetbits32(rcc_base + RCC_PWRLPDLYCR, ++ RCC_PWRLPDLYCR_PWRLP_DLY_MASK, ++ dly); ++ } else { ++ /* Wait 6 HSI periods after other Stop modes */ ++ io_clrsetbits32(rcc_base + RCC_PWRLPDLYCR, ++ RCC_PWRLPDLYCR_PWRLP_DLY_MASK, ++ PWRLP_TEMPO_5_HSI); ++ } ++ ++ /* Workaround for non secure cache issue: this should not be needed */ ++ dcache_op_all(DCACHE_OP_CLEAN_INV); ++ ++ /* Clear RCC interrupt before enabling it */ ++ io_setbits32(rcc_base + RCC_MP_CIFR, RCC_MP_CIFR_WKUPF); ++ ++ /* Enable RCC Wake-up */ ++ io_setbits32(rcc_base + RCC_MP_CIER, RCC_MP_CIFR_WKUPF); ++ ++ /* Configure low power mode */ ++ io_clrsetbits32(pwr_base + PWR_MPUCR_OFF, PWR_MPUCR_MASK, ++ config_pwr[mode].pwr_mpucr); ++ io_clrsetbits32(pwr_base + PWR_CR1_OFF, PWR_CR1_MASK, pwr_cr1); ++ ++ /* Clear RCC pending interrupt flags */ ++ io_write32(rcc_base + RCC_MP_CIFR, RCC_MP_CIFR_MASK); ++ ++#ifdef CFG_STM32MP13 ++ /* Request CSTOP mode to RCC */ ++ io_setbits32(rcc_base + RCC_MP_SREQSETR, RCC_MP_SREQSETR_STPREQ_P0); ++#else ++ io_setbits32(rcc_base + RCC_MP_SREQSETR, ++ RCC_MP_SREQSETR_STPREQ_P0 | RCC_MP_SREQSETR_STPREQ_P1); ++ ++ stm32_iwdg_refresh(); ++#endif ++ ++ set_rcc_it_priority(&gicd_rcc_wakeup, &gicc_pmr); ++ ++#ifndef CFG_STM32MP13 ++ if (ddr_standby_sr_entry() != 0) ++ panic(); ++#endif ++ ++ if (mode == STM32_PM_CSTOP_ALLOW_STANDBY_DDR_SR) { ++ /* set POPL to 20ms */ ++ io_clrsetbits32(pwr_base + PWR_CR3_OFF, PWR_CR3_POPL_MASK, ++ 20U << PWR_CR3_POPL_SHIFT); ++ ++ /* Keep backup RAM content in standby */ ++ io_setbits32(pwr_base + PWR_CR2_OFF, PWR_CR2_BREN); ++ ++ // TODO add a timeout? ++ while (!(io_read32(pwr_base + PWR_CR2_OFF) & PWR_CR2_BRRDY)) ++ ; ++ ++#ifndef CFG_STM32MP13 ++ if (stm32mp1_is_retram_during_standby()) { ++ /* Keep retention in standby */ ++ io_setbits32(pwr_base + PWR_CR2_OFF, PWR_CR2_RREN); ++ ++ while ((io_read32(pwr_base + PWR_CR2_OFF) & ++ (PWR_CR2_RRRDY)) == 0U) ++ ; ++ } ++#endif ++ } ++} ++ ++/* ++ * stm32_exit_cstop - Exit from CSTOP mode ++ */ ++void stm32_exit_cstop(void) ++{ ++ vaddr_t rcc_base = stm32_rcc_base(); ++ ++#ifdef CFG_STM32MP15 ++ if (ddr_standby_sr_exit()) ++ panic(); ++ ++ /* Restore Self-Refresh mode saved in stm32_enter_cstop() */ ++ ddr_restore_sr_mode(); ++#endif ++ ++ restore_rcc_it_priority(gicd_rcc_wakeup, gicc_pmr); ++ ++#ifdef CFG_STM32MP13 ++ /* Disable STOP request */ ++ io_setbits32(rcc_base + RCC_MP_SREQCLRR, RCC_MP_SREQSETR_STPREQ_P0); ++#else ++ /* Disable STOP request */ ++ io_setbits32(rcc_base + RCC_MP_SREQCLRR, ++ RCC_MP_SREQSETR_STPREQ_P0 | RCC_MP_SREQSETR_STPREQ_P1); ++#endif ++ ++ /* Disable RCC Wake-up */ ++ io_clrbits32(rcc_base + RCC_MP_CIER, RCC_MP_CIFR_WKUPF); ++ ++ dsb(); ++ isb(); ++ ++ /* Disable retention and backup RAM content after stop */ ++ io_clrbits32(stm32_pwr_base() + PWR_CR2_OFF, PWR_CR2_BREN); ++ ++#ifndef CFG_STM32MP13 ++ /* Disable retention and backup RAM content after stop */ ++ io_clrbits32(stm32_pwr_base() + PWR_CR2_OFF, PWR_CR2_RREN); ++#endif ++} ++ ++/* ++ * GIC support required in low power sequences and reset sequences ++ */ ++#define GICC_EOIR 0x010 ++ ++void stm32mp_gic_set_end_of_interrupt(uint32_t it) ++{ ++ vaddr_t gicc_base = get_gicc_base(); ++ ++ io_write32(gicc_base + GICC_EOIR, it); ++} ++ ++/* ++ * stm32_enter_cstop_shutdown - Shutdown CPUs to target low power mode ++ * @mode - Target low power mode ++ */ ++void __noreturn stm32_enter_cstop_shutdown(uint32_t mode) ++{ ++#ifdef CFG_STM32MP15 ++ if (stm32mp_supports_second_core() && (get_core_pos() == 0)) { ++ /* Prepare CPU reset */ ++ io_setbits32(stm32_rcc_base() + RCC_MP_GRSTCSETR, ++ RCC_MP_GRSTCSETR_MPUP1RST); ++ ++ itr_raise_sgi(GIC_SEC_SGI_1, TARGET_CPU1_GIC_MASK); ++ } ++#endif ++ ++ switch (mode) { ++ case STM32_PM_SHUTDOWN: ++ if (stm32mp_with_pmic()) { ++ wait_console_flushed(); ++ stm32mp_pm_get_pmic(); ++ stpmic1_switch_off(); ++ udelay(100); ++ } ++ break; ++ case STM32_PM_CSTOP_ALLOW_STANDBY_DDR_OFF: ++ stm32mp_pm_shutdown_context(); ++ stm32_enter_cstop(mode); ++ dsb(); ++ isb(); ++ for ( ; ; ) ++ wfi(); ++ break; ++ default: ++ break; ++ } ++ ++ panic(); ++} ++ ++/* ++ * stm32_enter_cstop_reset - Reset CPUs to target low power mode ++ * @mode - Target low power mode ++ */ ++void __noreturn stm32_enter_cstop_reset(uint32_t mode) ++{ ++ switch (mode) { ++ case STM32_PM_SHUTDOWN: ++ stm32_reset_system(); ++ udelay(100); ++ break; ++ default: ++ IMSG("Forced system reset"); ++ wait_console_flushed(); ++ stm32_reset_system(); ++ udelay(100); ++ break; ++ } ++ ++ panic(); ++} ++ ++/* ++ * stm32_enter_csleep - enter CSLEEP state while WFI and exit in CRUN ++ * ++ * Configure PWR for CSLEEP state. CPU shall execute a WFI and return ++ * once a interrupt is pending. ++ */ ++void stm32_enter_csleep(void) ++{ ++ vaddr_t pwr_base = stm32_pwr_base(); ++ ++ io_clrsetbits32(pwr_base + PWR_MPUCR_OFF, PWR_MPUCR_MASK, ++ config_pwr[STM32_PM_CSLEEP_RUN].pwr_mpucr); ++ io_clrsetbits32(pwr_base + PWR_CR1_OFF, PWR_CR1_MASK, ++ config_pwr[STM32_PM_CSLEEP_RUN].pwr_cr1); ++ ++ stm32_pm_cpu_wfi(); ++} ++ ++/* RCC Wakeup interrupt is used to wake from suspeneded mode */ ++static enum itr_return rcc_wakeup_it_handler(struct itr_handler *hdl __unused) ++{ ++ /* This interrupt is not expected to be handled */ ++ panic("RCC wakeup interrupt"); ++ return ITRR_HANDLED; ++} ++ ++static struct itr_handler rcc_wakeup_handler = { ++ .it = RCC_WAKEUP_IT, ++ .handler = rcc_wakeup_it_handler, ++}; ++DECLARE_KEEP_PAGER(rcc_wakeup_handler); ++ ++/* SGI9 (secure SGI 1) informs targeted CPU it shall reset */ ++static enum itr_return sgi9_it_handler(struct itr_handler *hdl __unused) ++{ ++ DMSG("Halting CPU %u", get_core_pos()); ++ ++ stm32mp_mask_timer(); ++ ++ stm32mp_dump_core_registers(false); ++ ++ while (true) ++ cpu_idle(); ++ ++ return ITRR_HANDLED; ++} ++ ++static struct itr_handler sgi9_reset_handler = { ++ .it = GIC_SEC_SGI_1, ++ .handler = sgi9_it_handler, ++}; ++DECLARE_KEEP_PAGER(sgi9_reset_handler); ++ ++void __noreturn plat_panic(void) ++{ ++ stm32mp_mask_timer(); ++ ++ if (stm32mp_supports_second_core()) { ++ uint32_t target_mask = 0; ++ ++ if (get_core_pos() == 0) ++ target_mask = TARGET_CPU1_GIC_MASK; ++ else ++ target_mask = TARGET_CPU0_GIC_MASK; ++ ++ itr_raise_sgi(GIC_SEC_SGI_1, target_mask); ++ } ++ ++ while (true) ++ cpu_idle(); ++} ++ ++static TEE_Result init_low_power(void) ++{ ++ vaddr_t pwr_base __maybe_unused = stm32_pwr_base(); ++ vaddr_t rcc_base = stm32_rcc_base(); ++ ++ itr_add(&rcc_wakeup_handler); ++ itr_enable(rcc_wakeup_handler.it); ++ ++ itr_add(&sgi9_reset_handler); ++ itr_enable(sgi9_reset_handler.it); ++ ++#ifdef CFG_STM32MP13 ++ /* Disable STOP request */ ++ io_setbits32(rcc_base + RCC_MP_SREQCLRR, RCC_MP_SREQSETR_STPREQ_P0); ++#else ++ /* ++ * Configure Standby mode available for MCU by default ++ * and allow to switch in standby SoC in all case ++ */ ++ io_setbits32(pwr_base + PWR_MCUCR_OFF, PWR_MCUCR_PDDS); ++ ++ /* Disable STOP request */ ++ io_setbits32(rcc_base + RCC_MP_SREQCLRR, ++ RCC_MP_SREQSETR_STPREQ_P0 | RCC_MP_SREQSETR_STPREQ_P1); ++#endif ++ ++ return TEE_SUCCESS; ++} ++driver_init_late(init_low_power); ++ ++/* ++ * CPU low power sequences ++ */ ++void __noreturn stm32_pm_cpu_power_down_wfi(void) ++{ ++ vaddr_t __maybe_unused rcc_base = stm32_rcc_base(); ++ ++ if (get_core_pos() == 0) { ++ void (*reset_ep)(void) = stm32mp_sysram_resume; ++ ++ stm32_pm_cpu_wfi(); ++ ++ /* STANDBY not reached: resume from retained SYSRAM */ ++ stm32_exit_cstop(); ++ stm32mp_cpu_reset_state(); ++ reset_ep(); ++ panic(); ++ } ++ ++ dcache_op_level1(DCACHE_OP_CLEAN); ++#ifdef CFG_STM32MP13 ++ panic(); ++#else ++ io_write32(rcc_base + RCC_MP_GRSTCSETR, RCC_MP_GRSTCSETR_MPUP1RST); ++#endif ++ cpu_wfi(); ++ panic(); ++} +diff --git a/core/arch/arm/plat-stm32mp1/pm/pm_helpers.S b/core/arch/arm/plat-stm32mp1/pm/pm_helpers.S +new file mode 100644 +index 000000000..bec67e2b2 +--- /dev/null ++++ b/core/arch/arm/plat-stm32mp1/pm/pm_helpers.S +@@ -0,0 +1,729 @@ ++/* SPDX-License-Identifier: BSD-2-Clause */ ++/* ++ * Copyright (c) 2018-2021, STMicroelectronics ++ * Copyright (c) 2017-2021 NXP ++ * Copyright (c) 2016-2021, ARM Limited and Contributors. All rights reserved. ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#include "context.h" ++ ++/* ++ * Right bit shift distance to reach timeout from a 1s STGEN freq count ++ * Value N relates to 1000ms / 2^N, i.e 7 relates to 7.8125ms=~8ms ++ */ ++#define CCM_TIMEOUT_128MS 2 ++#define CCM_TIMEOUT_8MS 7 ++#define CCM_TIMEOUT_1MS 10 ++#define CCM_TIMEOUT_16US 16 ++#define CCM_TIMEOUT CCM_TIMEOUT_8MS ++ ++/* ++ * CRYP interface register used for AES CCM ++ */ ++#define CRYP_CR 0x000 ++#define CRYP_SR 0x004 ++#define CRYP_DIN 0x008 ++#define CRYP_DOUT 0x00c ++#define CRYP_KEYR_BASE 0x020 ++#define CRYP_IVR_BASE 0x040 ++ ++#define CRYP_CR_ALGODIR_DECRYPT BIT(2) ++#define CRYP_CR_ALGOMODE_MASK (BIT(19) | GENMASK_32(5, 3)) ++#define CRYP_CR_ALGOMODE(m) (((m & BIT(3)) << 16) | (m & 0x7) << 3) ++#define ALGOMODE_AES_CCM 0x9 ++#define CRYP_CR_DATATYPE_SHIFT 6 ++#define CRYP_CR_DATATYPE_8BIT (2 << CRYP_CR_DATATYPE_SHIFT) ++#define CRYP_CR_KEYSIZE_SHIFT 8 ++#define CRYP_CR_KEYSIZE_256BIT (2U << CRYP_CR_KEYSIZE_SHIFT) ++#define CRYP_CR_CRYPEN BIT(15) ++#define CRYP_CR_FFLUSH BIT(14) ++#define CRYP_CR_GCM_CCMPH_SHIFT 16 ++#define CRYP_CR_PHASE_MASK (0x3 << CRYP_CR_GCM_CCMPH_SHIFT) ++#define CRYP_CR_INIT_PHASE (0 << CRYP_CR_GCM_CCMPH_SHIFT) ++#define CRYP_CR_HEADER_PHASE (1 << CRYP_CR_GCM_CCMPH_SHIFT) ++#define CRYP_CR_PAYLOAD_PHASE (2 << CRYP_CR_GCM_CCMPH_SHIFT) ++#define CRYP_CR_FINAL_PHASE (3 << CRYP_CR_GCM_CCMPH_SHIFT) ++ ++#define CRYP_SR_BUSY BIT(4) ++#define CRYP_SR_OFFU BIT(3) ++#define CRYP_SR_OFNE BIT(2) ++#define CRYP_SR_IFNF BIT(1) ++#define CRYP_SR_IFEM BIT(0) ++ ++/* ++ * Enable TRACE_SYSRAM_RESTORE to get some UART console traces ++ * at resume time. ++ */ ++#if defined(TRACE_SYSRAM_RESTORE) ++ ++#define UART_BASE UART4_BASE ++#define UART_ISR_OFF 0x1c ++#define UART_TDR_OFF 0x28 ++#define USART_ISR_TXE_TXFNF (1<< 7) ++ ++ .macro PRINT_CHAR _reg0, _reg1, _char ++ /* Trace only at resume when MMU is OFF */ ++ read_sctlr \_reg0 ++ ands \_reg0, #SCTLR_M ++ 101: ++ bne 102f ++ mov_imm \_reg0, UART4_BASE ++ ldr \_reg1, [\_reg0, #UART_ISR_OFF] ++ ands \_reg1, #USART_ISR_TXE_TXFNF ++ beq 101b ++ mov_imm \_reg1, (\_char) ++ str \_reg1, [\_reg0, #UART_TDR_OFF] ++ 102: ++ .endm ++#else ++ .macro PRINT_CHAR _reg0, _reg1, _char ++ .endm ++#endif ++ ++ /* Bound of the binary image loaded in retained memory */ ++ .global stm32mp_bkpsram_image_end ++ ++/* ++ * stm32mp_bkpsram_resume - Restore TEE RAM from backup memory and resume into ++ * ++ * This function executes at early resume from suspend state. It is the ++ * entrypoint of the OP-TEE provided to early boot stage when SoC wakes. ++ * This code is located in a retained memory, MMU disabled. This function ++ * shall restore TEE RAM content for OP-TEE to resume execution. Once ++ * TEE RAM is restored, this function branches to the resident resume entry ++ * point in TEE_RAM. This function and its resources shall execute in place. ++ */ ++FUNC stm32mp_bkpsram_resume , : ++UNWIND( .cantunwind) ++ ++ PRINT_CHAR r0, r1, '0' ++ ++ /* ++ * Almost all sequences here expect PM context structure base address ++ * from CPU register r11. ++ */ ++ mov_imm r11, (BKPSRAM_BASE + BKPSRAM_PM_CONTEXT_OFFSET) ++ ++ /* stm32mp_ccm_teeram needs some HW interface base addresss */ ++ mov_imm r0, CRYP1_BASE ++ str r0, [r11, #PM_CTX_CRYP1_BASE] ++ mov_imm r0, RCC_BASE ++ str r0, [r11, #PM_CTX_RCC_BASE] ++ mov_imm r0, STGEN_BASE ++ str r0, [r11, #PM_CTX_STGEN_BASE] ++ ++ bl _clear_early_mailbox ++ bl _prepare_time ++ ++ mov_imm r0, TEE_RAM_START ++ ldr r1, [r11, #PM_CTX_TEERAM_BKP_PA] ++ mov_imm r2, TEE_RAM_PH_SIZE ++ mov_imm r3, 1 ++ bl stm32mp_ccm_teeram ++ cmp r0, #0 ++ bne _failed ++ ++ PRINT_CHAR r0, r1, 'T' ++ PRINT_CHAR r0, r1, 'a' ++ PRINT_CHAR r0, r1, 'g' ++ PRINT_CHAR r0, r1, '\n' ++ ++ /* Compare the generated and reference tags */ ++ add r8, r11, #PM_CTX_CCM_TAG ++ add r9, r11, #PM_CTX_CCM_REF_TAG ++ ldm r8, {r2-r5} ++ ldm r9, {r6-r9} ++ mov r0, #0 ++ cmp r2, r6 ++ addeq r0, #1 ++ cmp r3, r7 ++ addeq r0, #1 ++ cmp r4, r8 ++ addeq r0, #1 ++ cmp r5, r9 ++ addeq r0, #1 ++ cmp r0, #4 ++ bne _failed ++ bl _save_resume_time ++ ++ PRINT_CHAR r1, r2, 'O' ++ PRINT_CHAR r1, r2, 'k' ++ PRINT_CHAR r1, r2, '\n' ++ ++ /* Resume into the restored TEE RAM */ ++ ldr r1, [r11, #PM_CTX_RESUME_PA] ++ bx r1 ++ ++_failed: ++ PRINT_CHAR r0, r12, 'F' ++ PRINT_CHAR r0, r12, '\n' ++ ++ /* Clear context including key and reference tag */ ++ mov r0, #0xa5 ++ mov_imm r12, BKPSRAM_PM_CONTEXT_SIZE ++ add r12, r11, r12 ++1: str r0, [r11], #4 ++ cmp r11, r12 ++ blt 1b ++ b . ++ ++ /* ++ * _clear_early_mailbox - Wipe mailbox in case of reset ++ * ++ * Sratches r0-r4. ++ * All other CPU registers are preserved. ++ */ ++_clear_early_mailbox: ++ /* Clear the backup registers (first enable RTCAPB clock) */ ++ mov_imm r0, (RCC_BASE + RCC_MP_APB5ENSETR) ++ mov_imm r2, RCC_MP_APB5ENSETR_RTCAPBEN ++ ldr r1, [r0] ++ ands r1, r1, r2 ++ moveq r1, r2 ++ movne r1, #0 ++ str r2, [r0] ++ mov_imm r2, (TAMP_BASE + TAMP_BKP_REGISTER_OFF) ++ mov_imm r3, (BCKR_CORE1_MAGIC_NUMBER * 4) ++ mov_imm r4, BOOT_API_A7_RESET_MAGIC_NUMBER ++ str r4, [r2, r3] ++ mov_imm r3, (BCKR_CORE1_BRANCH_ADDRESS * 4) ++ mov r4, #0 ++ str r4, [r2, r3] ++ /* Restore RTCAPB clock initial state */ ++ str r1, [r0, #RCC_MP_ENCLRR_OFFSET] ++ bx lr ++ ++ /* ++ * prepare_time - save/reset cycle counter to prevent later overflow ++ * ++ * Save current 32bit lower counter and reset to 0 so that later ++ * timeout test do not need to care about overflow. ++ * ++ * Expects r11 is context base and lr is return address. ++ * Scrathes r0-r2. ++ * All other CPU registers are preserved. ++ */ ++_prepare_time: ++ ldr r2, [r11, #PM_CTX_STGEN_BASE] ++ /* Disable STGEN counter */ ++ ldr r1, [r2, #CNTCR_OFFSET] ++ bic r1, r1, #CNTCR_EN ++ str r1, [r2, #CNTCR_OFFSET] ++1: ldr r1, [r2, #CNTSR_OFFSET] ++ tst r1, #CNTCR_EN ++ bne 1b ++ /* Save and reset STGEN counter */ ++ ldr r0, [r2, #CNTCVL_OFFSET] ++ str r0, [r11, #PM_CTX_STGEN_CNT] ++ mov r0, #0 ++ str r0, [r2, #CNTCVL_OFFSET] ++ ldr r0, [r2, #CNTCVU_OFFSET] ++ str r0, [r2, #CNTCVU_OFFSET] ++ /* Enable STGEN counter */ ++ ldr r1, [r2, #CNTCR_OFFSET] ++ orr r1, r1, #CNTCR_EN ++ str r1, [r2, #CNTCR_OFFSET] ++ bx lr ++ ++ /* ++ * save_resume_time - save time spent and restore STGEN cycle counter ++ * ++ * Restore STGEN counter to initial value incremented by the current ++ * count. Note 32bit upper may need to be incremented. ++ * ++ * Expects r11 is context base and lr is return address. ++ * Scrathes r0-r3. ++ * All other CPU registers are preserved. ++ */ ++_save_resume_time: ++ /* Compute update STGEN counter 32bit LSB value */ ++ ldr r2, [r11, #PM_CTX_STGEN_BASE] ++ ldr r0, [r11, #PM_CTX_STGEN_CNT] ++ ldr r3, [r2, #CNTCVL_OFFSET] ++ str r3, [r11, #PM_CTX_STGEN_CNT] ++ adds r0, r0, r3 ++ /* Disable STGEN */ ++ ldr r1, [r2, #CNTCR_OFFSET] ++ bic r1, r1, #CNTCR_EN ++ str r1, [r2, #CNTCR_OFFSET] ++1: ldr r1, [r2, #CNTSR_OFFSET] ++ tst r1, #CNTCR_EN ++ bne 1b ++ /* Update counter (increment 32bit MSB if requried) */ ++ str r0, [r2, #CNTCVL_OFFSET] ++ ldr r0, [r2, #CNTCVU_OFFSET] ++ addcs r0, r0, #1 ++ str r0, [r2, #CNTCVU_OFFSET] /* Write CNTCVU value ... */ ++ ldr r0, [r2, #CNTCVU_OFFSET] /* ... and wait it is set */ ++ /* Enable STGEN */ ++ ldr r0, [r2, #CNTCR_OFFSET] ++ orr r0, r0, #CNTCR_EN ++ str r0, [r2, #CNTCR_OFFSET] ++ bx lr ++ ++ /* ++ * _setup_cryp1 - Enable CRYP1 hardware: reset & clock ++ * _reset_cryp1 - Reset CRYP1 hardware ++ * ++ * Function call before and after CCM sequence. Note that the CRYP1 ++ * clock remain enabled. It is disabled later by the resume sequence. ++ * ++ * Expects r11 is context base and lr is return address. ++ * Scratches r0-r3. ++ */ ++_setup_cryp1: ++ ldr r1, [r11, #PM_CTX_RCC_BASE] ++ mov_imm r0, RCC_MP_AHB5ENSETR_CRYP1EN ++ str r0, [r1, #RCC_MP_AHB5ENSETR] ++ /* Intentionnally fall through reset_cryp1 */ ++_reset_cryp1: ++ ldr r3, [r11, #PM_CTX_RCC_BASE] ++ mov_imm r0, RCC_AHB5RSTSETR_CRYP1RST ++ str r0, [r3, #RCC_AHB5RSTSETR] ++1: ldr r1, [r3, #RCC_AHB5RSTSETR] ++ ands r1, r1, r0 ++ beq 1b ++ mov_imm r0, RCC_AHB5RSTSETR_CRYP1RST ++ str r0, [r3, #RCC_AHB5RSTCLRR] ++1: ldr r1, [r3, #RCC_AHB5RSTSETR] ++ ands r1, r1, r0 ++ bne 1b ++ bx lr ++ ++ /* ++ * _ccm_arm_8ms_timeout - Init 8ms threshold for _ccm_failed_on_timeout ++ * _ccm_fail_on_timeout - Check STGEN counter against timeout threshold ++ * ++ * These function are used by the macro wait_flag_timeout_8ms. The ++ * former loads the timeout in CPU register r0 while the later get the ++ * timeout counter threshold from CPU register r0. ++ * ++ * Expect r11 is context base and lr is return address. ++ * Scratch r0-r1. ++ * All other CPU registers are preserved. ++ */ ++_ccm_arm_8ms_timeout: ++ ldr r1, [r11, #PM_CTX_STGEN_BASE] ++ ldr r0, [r1, #CNTFID_OFFSET] ++ lsrs r0, r0, #CCM_TIMEOUT ++ moveq r0, #1 ++ ldr r1, [r1, #CNTCVL_OFFSET] ++ adds r0, r0, r1 ++ bcs _ccm_failed_on_timeout ++ bx lr ++ ++_ccm_fail_on_timeout: ++ ++ ldr r1, [r11, #PM_CTX_STGEN_BASE] ++ ldr r1, [r1, #CNTCVL_OFFSET] ++ cmp r1, r0 ++ bge _ccm_failed_on_timeout ++ bx lr ++ ++_ccm_failed_on_timeout: ++ PRINT_CHAR r0, r1, 'T' ++ PRINT_CHAR r0, r1, 'o' ++ PRINT_CHAR r0, r1, '\n' ++ b _ccm_failed ++ ++ /* ++ * Macro WAIT_FLAG_TIMEOUT compares timeout threshold (r0) with ++ * current time and branches the CCM failure entry on timeout. ++ * It is assumed the 32bit timestamps cannot overflow. ++ */ ++ .macro WAIT_FLAG_TIMEOUT register_offset, bit_mask, awaited_mask ++ bl _ccm_arm_8ms_timeout ++ 1: ++ bl _ccm_fail_on_timeout ++ ldr r1, [r10, #(\register_offset)] ++ and r1, r1, #(\bit_mask) ++ cmp r1, #(\awaited_mask) ++ bne 1b ++ .endm ++ ++/* ++ * stm32mp_ccm_teeram - Size optimzed unpaged CCM encryption/decryption ++ * ++ * This sequence encrypts or decrypts a input block using AES CCM with a ++ * 256bit key and no AAD and generates the CCM tag. The key, CTR0, CTR1 ++ * and B0 block are read from PM context structure. The generated tag is ++ * stored in the PM context structure. ++ * ++ * This function is executed from TEE RAM during suspend sequence to generate ++ * the encrypted data and the tag. This function is also executed from BKPSRAM ++ * called with MMU disabled. Therefore this sequence shall be comply with ++ * position independent code constraints. ++ * ++ * Expects at entry: ++ * lr = caller return address ++ * r11 = retram_resume_ctx structure base address ++ * r0 = Destination buffer for the output data (ciphertext or plaintext) ++ * r1 = Source buffer for the input data (plaintext or ciphertext) ++ * r2 = Input (and output) data size in bytes ++ * r3 = 1 if decrypting, 0 if encrypting ++ */ ++stm32mp_ccm_teeram: ++ /* ++ * Use of the CPU registers in the whole stm32mp_ccm_teeram sequence ++ * ++ * sp: preserved, not used ++ * lr: scratch register used to call subroutines. ++ * r12: saves the caller link register for final return ++ * r11: context from BKPSRAM ++ * r10: CRYP1 base address ++ * r9: destination buffer ++ * r8: source buffer to cipher ++ * r7: data byte counter ++ * r0-r6 are scratch registers ++ */ ++ mov r12, lr ++ ldr r10, [r11, #PM_CTX_CRYP1_BASE] ++ mov r9, r0 ++ mov r8, r1 ++ mov r7, r2 ++ mov r6, r3 ++ ++ PRINT_CHAR r0, r1, '1' ++ ++ bl _setup_cryp1 ++ ++ PRINT_CHAR r0, r1, '2' ++ ++ mov_imm r0, (CRYP_CR_ALGOMODE(ALGOMODE_AES_CCM) | \ ++ CRYP_CR_DATATYPE_8BIT | CRYP_CR_FFLUSH | \ ++ CRYP_CR_KEYSIZE_256BIT) ++ cmp r6, #0 ++ orrne r0, r0, #CRYP_CR_ALGODIR_DECRYPT ++ str r0, [r10, #CRYP_CR] ++ ++ PRINT_CHAR r0, r1, '3' ++ ++ /* Check data alignment (addresses and size) */ ++ ands r0, r7, #0x0F ++ bne _ccm_failed ++ ands r0, r8, #0x03 ++ bne _ccm_failed ++ ands r0, r9, #0x03 ++ bne _ccm_failed ++ ++ PRINT_CHAR r0, r1, '4' ++ ++ ldr r0, [r11, #PM_CTX_CCM_KEY] ++ str r0, [r10, #CRYP_KEYR_BASE] ++ ldr r0, [r11, #(PM_CTX_CCM_KEY + 4)] ++ str r0, [r10, #(CRYP_KEYR_BASE + 4)] ++ ldr r0, [r11, #(PM_CTX_CCM_KEY + 8)] ++ str r0, [r10, #(CRYP_KEYR_BASE + 8)] ++ ldr r0, [r11, #(PM_CTX_CCM_KEY + 12)] ++ str r0, [r10, #(CRYP_KEYR_BASE + 12)] ++ ldr r0, [r11, #(PM_CTX_CCM_KEY + 16)] ++ str r0, [r10, #(CRYP_KEYR_BASE + 16)] ++ ldr r0, [r11, #(PM_CTX_CCM_KEY + 20)] ++ str r0, [r10, #(CRYP_KEYR_BASE + 20)] ++ ldr r0, [r11, #(PM_CTX_CCM_KEY + 24)] ++ str r0, [r10, #(CRYP_KEYR_BASE + 24)] ++ ldr r0, [r11, #(PM_CTX_CCM_KEY + 28)] ++ str r0, [r10, #(CRYP_KEYR_BASE + 28)] ++ ++ ldr r0, [r11, #PM_CTX_CCM_CTR1] ++ str r0, [r10, #CRYP_IVR_BASE] ++ ldr r0, [r11, #(PM_CTX_CCM_CTR1 + 4)] ++ str r0, [r10, #(CRYP_IVR_BASE + 4)] ++ ldr r0, [r11, #(PM_CTX_CCM_CTR1 + 8)] ++ str r0, [r10, #(CRYP_IVR_BASE + 8)] ++ ldr r0, [r11, #(PM_CTX_CCM_CTR1 + 12)] ++ str r0, [r10, #(CRYP_IVR_BASE + 12)] ++ ++ /* Setup CRYP for the CCM Init Phase */ ++ ldr r0, [r10, #CRYP_CR] ++ orr r0, r0, #(CRYP_CR_CRYPEN | CRYP_CR_INIT_PHASE) ++ str r0, [r10, #CRYP_CR] ++ ldr r0, [r10, #CRYP_CR] ++ ++ ldr r0, [r11, #PM_CTX_CCM_B0] ++ str r0, [r10, #CRYP_DIN] ++ ldr r0, [r11, #(PM_CTX_CCM_B0 + 4)] ++ str r0, [r10, #CRYP_DIN] ++ ldr r0, [r11, #(PM_CTX_CCM_B0 + 8)] ++ str r0, [r10, #CRYP_DIN] ++ ldr r0, [r11, #(PM_CTX_CCM_B0 + 12)] ++ str r0, [r10, #CRYP_DIN] ++ ++ PRINT_CHAR r0, r1, '5' ++ ++ WAIT_FLAG_TIMEOUT CRYP_CR, CRYP_CR_CRYPEN, 0 ++ ++ /* Setup CRYP for the CCM Payload phase */ ++ ldr r0, [r10, #CRYP_CR] ++ bic r0, r0, #CRYP_CR_PHASE_MASK ++ orr r0, r0, #CRYP_CR_PAYLOAD_PHASE ++ orr r0, r0, #CRYP_CR_CRYPEN ++ str r0, [r10, #CRYP_CR] ++ ldr r0, [r10, #CRYP_CR] ++ ++ PRINT_CHAR r0, r1, '\n' ++ ++_next_block: ++ PRINT_CHAR r0, r1, 'b' ++ ++ WAIT_FLAG_TIMEOUT CRYP_SR, CRYP_SR_IFEM, CRYP_SR_IFEM ++ ++ /* Feed input data, r8 stores the current source buffer */ ++ ldr r0, [r8], #4 ++ str r0, [r10, #CRYP_DIN] ++ ldr r0, [r8], #4 ++ str r0, [r10, #CRYP_DIN] ++ ldr r0, [r8], #4 ++ str r0, [r10, #CRYP_DIN] ++ ldr r0, [r8], #4 ++ str r0, [r10, #CRYP_DIN] ++ ++ WAIT_FLAG_TIMEOUT CRYP_SR, CRYP_SR_OFNE, CRYP_SR_OFNE ++ ++ /* Store output data, r9 stores the current source buffer */ ++ ldr r0, [r10, #CRYP_DOUT] ++ str r0, [r9], #4 ++ ldr r0, [r10, #CRYP_DOUT] ++ str r0, [r9], #4 ++ ldr r0, [r10, #CRYP_DOUT] ++ str r0, [r9], #4 ++ /* Before last 32bit word, the output FIFO shall not be empty */ ++ ldr r0, [r10, #CRYP_SR] ++ ands r0, r0, #CRYP_SR_OFNE ++ beq _ccm_failed ++ /* After last 32bit word for this 128block, FIFO shall be empty */ ++ ldr r0, [r10, #CRYP_DOUT] ++ str r0, [r9], #4 ++ ldr r0, [r10, #CRYP_SR] ++ ands r0, r0, #CRYP_SR_OFNE ++ bne _ccm_failed ++ ++ /* Another round if remaining data */ ++ subs r7, r7, #16 ++ bne _next_block; ++ ++ PRINT_CHAR r0, r1, '\n' ++ PRINT_CHAR r0, r1, '6' ++ ++ WAIT_FLAG_TIMEOUT CRYP_SR, CRYP_SR_BUSY, 0 ++ ++ /* ++ * Data processing completed, now remains the tag generation. ++ * Here expect SR[IFNF]=SR[OFNE]=1 and all others bits are 0. ++ */ ++ ldr r0, [r10, #CRYP_SR] ++ cmp r0, #(CRYP_SR_IFEM | CRYP_SR_IFNF) ++ bne _ccm_failed ++ ++ PRINT_CHAR r0, r1, '7' ++ ++ /* Setup CRYP1 for the CCM Final Phase */ ++ ldr r0, [r10, #CRYP_CR] ++ bic r0, r0, #CRYP_CR_CRYPEN ++ str r0, [r10, #CRYP_CR] ++ ldr r0, [r10, #CRYP_CR] ++ bic r0, r0, #CRYP_CR_PHASE_MASK ++ bic r0, r0, #CRYP_CR_ALGODIR_DECRYPT ++ orr r0, r0, #CRYP_CR_FINAL_PHASE ++ orr r0, r0, #CRYP_CR_CRYPEN ++ str r0, [r10, #CRYP_CR] ++ ldr r0, [r10, #CRYP_CR] ++ ++ /* Load CTR0 to generate the tag */ ++ ldr r0, [r11, #PM_CTX_CCM_CTR0] ++ str r0, [r10, #CRYP_DIN] ++ ldr r0, [r11, #(PM_CTX_CCM_CTR0 + 4)] ++ str r0, [r10, #CRYP_DIN] ++ ldr r0, [r11, #(PM_CTX_CCM_CTR0 + 8)] ++ str r0, [r10, #CRYP_DIN] ++ ldr r0, [r11, #(PM_CTX_CCM_CTR0 + 12)] ++ str r0, [r10, #CRYP_DIN] ++ ++ PRINT_CHAR r0, r1, '8' ++ ++ WAIT_FLAG_TIMEOUT CRYP_SR, CRYP_SR_OFNE, CRYP_SR_OFNE ++ ++ /* Store generated tag in the PM_CTX structure */ ++ ldr r0, [r10, #CRYP_DOUT] ++ str r0, [r11, #PM_CTX_CCM_TAG] ++ ldr r0, [r10, #CRYP_DOUT] ++ str r0, [r11, #(PM_CTX_CCM_TAG + 4)] ++ ldr r0, [r10, #CRYP_DOUT] ++ str r0, [r11, #(PM_CTX_CCM_TAG + 8)] ++ /* Before last 32bit word, the output FIFO shall not be empty */ ++ ldr r0, [r10, #CRYP_SR] ++ ands r0, r0, #CRYP_SR_OFNE ++ beq _ccm_failed ++ /* After last 32bit word for this 128block, FIFO shall be empty */ ++ ldr r0, [r10, #CRYP_DOUT] ++ str r0, [r11, #(PM_CTX_CCM_TAG + 12)] ++ ldr r0, [r10, #CRYP_SR] ++ ands r0, r0, #CRYP_SR_OFNE ++ bne _ccm_failed ++ ++ PRINT_CHAR r0, r1, '9' ++ ++ /* Successful return */ ++ bl _reset_cryp1 ++ mov r0, #0 ++ bx r12 ++ ++_ccm_failed: ++ ++ PRINT_CHAR r0, r1, 'K' ++ PRINT_CHAR r0, r1, 'O' ++ ++ bl _reset_cryp1 ++ mov r0, #1 ++ bx r12 ++ ++/* End address of the PIC resume sequence copy in retained RAM */ ++stm32mp_bkpsram_image_end: ++ nop ++ ++END_FUNC stm32mp_bkpsram_resume ++ ++/* ++ * int stm32mp_ccm_encrypt_teeram(ctx, dst, src, len) ++ */ ++FUNC stm32mp_ccm_encrypt_teeram , : ++ push {r4-r12, lr} ++UNWIND( .save {r4-r12, lr}) ++ mov r11, r0 ++ mov r0, r1 ++ mov r1, r2 ++ mov r2, r3 ++ mov r3, #0 ++ push {r0-r3} ++ bl _prepare_time ++ pop {r0-r3} ++ bl stm32mp_ccm_teeram ++ bl _save_resume_time ++ pop {r4-r12, pc} ++END_FUNC stm32mp_ccm_encrypt_teeram ++ ++/* ++ * int stm32mp_ccm_decrypt_teeram(ctx, cryp_base, dst, src) ++ */ ++FUNC stm32mp_ccm_decrypt_teeram , : ++ push {r4-r12, lr} ++UNWIND( .save {r4-r12, lr}) ++ mov r11, r0 ++ mov r0, r1 ++ mov r1, r2 ++ mov r2, r3 ++ mov r3, #1 ++ push {r0-r3} ++ bl _prepare_time ++ pop {r0-r3} ++ bl stm32mp_ccm_teeram ++ bl _save_resume_time ++ pop {r4-r12, pc} ++END_FUNC stm32mp_ccm_decrypt_teeram ++ ++/* ++ * stm32mp_sysram_resume - Resume OP-TEE execution ++ * ++ * This function is the entry point of OP-TEE core resume sequence in the TEE ++ * RAM. When TEE RAM is lost during a power cycle, stm32mp_bkpsram_resume() is ++ * called to restore TEE RAM content and branch to this stm32mp_sysram_resume() ++ * routine. ++ * ++ * This function calls the OP-TEE core generic PM resume API ++ * sm_pm_cpu_resume(). ++ */ ++FUNC stm32mp_sysram_resume, : ++UNWIND( .cantunwind) ++ /* Invalidate the data cache */ ++ ++ read_clidr r2 ++ ubfx r3, r2, #CLIDR_LOC_SHIFT, #CLIDR_FIELD_WIDTH ++ lsl r3, r3, #CSSELR_LEVEL_SHIFT ++ mov r1, #0 ++ ++loop1: ++ add r10, r1, r1, LSR #1 // Work out 3x current cache level ++ mov r12, r2, LSR r10 // extract cache type bits from clidr ++ and r12, r12, #7 // mask the bits for current cache only ++ cmp r12, #2 // see what cache we have at this level ++ blo level_done // no cache or only instruction cache at this level ++ ++ write_csselr r1 // select current cache level in csselr ++ isb // isb to sych the new cssr&csidr ++ read_ccsidr r12 // read the new ccsidr ++ and r10, r12, #7 // extract the length of the cache lines ++ add r10, r10, #4 // add 4 (r10 = line length offset) ++ ubfx r4, r12, #3, #10 // r4 = maximum way number (right aligned) ++ clz r5, r4 // r5 = the bit position of the way size increment ++ mov r9, r4 // r9 working copy of the aligned max way number ++ ++loop2: ++ ubfx r7, r12, #13, #15 // r7 = max set number (right aligned) ++ ++loop3: ++ orr r0, r1, r9, LSL r5 // factor in the way number and cache level into r0 ++ orr r0, r0, r7, LSL r10 // factor in the set number ++ ++ write_dcisw r0 ++ ++ subs r7, r7, #1 // decrement the set number ++ bhs loop3 ++ subs r9, r9, #1 // decrement the way number ++ bhs loop2 ++level_done: ++ add r1, r1, #2 // increment the cache number ++ cmp r3, r1 ++ dsb sy // ensure completion of previous cache maintenance instruction ++ bhi loop1 ++ ++ mov r6, #0 ++ write_csselr r6 //select cache level 0 in csselr ++ dsb sy ++ isb ++ ++ /* Resume sequence executes in Monitor mode */ ++ cps #CPSR_MODE_MON ++ ++ blx plat_cpu_reset_early ++ b sm_pm_cpu_resume ++END_FUNC stm32mp_sysram_resume ++ ++/* ++ * stm32mp_cpu_reset_state - set CPU in a reset like state ++ * ++ * Disable CPU env (interrupts, cache, SMP, MMU) and return. ++ * Preserve the execution mode in CPSR. ++ */ ++FUNC stm32mp_cpu_reset_state, : ++ push {r12, lr} ++UNWIND( .save {r12, lr}) ++ ++ cpsid aif ++ ++ bl psci_armv7_cpu_off ++ ++ write_bpiall ++ dsb ++ isb ++ read_sctlr r0 ++ bic r0, r0, #SCTLR_M ++ bic r0, r0, #SCTLR_I ++ write_sctlr r0 ++ dsb sy ++ isb ++ ++ pop {r12, pc} ++END_FUNC stm32mp_cpu_reset_state +diff --git a/core/arch/arm/plat-stm32mp1/pm/power.h b/core/arch/arm/plat-stm32mp1/pm/power.h +new file mode 100644 +index 000000000..4a5578c63 +--- /dev/null ++++ b/core/arch/arm/plat-stm32mp1/pm/power.h +@@ -0,0 +1,27 @@ ++/* SPDX-License-Identifier: BSD-3-Clause */ ++/* ++ * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved. ++ */ ++ ++#ifndef __STM32MP_PM_POWER_H__ ++#define __STM32MP_PM_POWER_H__ ++ ++#include ++#include ++#include ++ ++bool need_to_backup_cpu_context(unsigned int soc_mode); ++bool need_to_backup_stop_context(unsigned int soc_mode); ++ ++void stm32_enter_csleep(void); ++ ++void stm32_enter_cstop(uint32_t mode); ++void stm32_exit_cstop(void); ++ ++void stm32_enter_cstop_shutdown(uint32_t mode) __noreturn; ++void stm32_enter_cstop_reset(uint32_t mode) __noreturn; ++ ++void stm32_pm_cpu_power_down_wfi(void) __noreturn; ++void stm32_pm_cpu_wfi(void); ++ ++#endif /*__STM32MP_PM_POWER_H__*/ +diff --git a/core/arch/arm/plat-stm32mp1/pm/power_config.c b/core/arch/arm/plat-stm32mp1/pm/power_config.c +new file mode 100644 +index 000000000..aa32c53c3 +--- /dev/null ++++ b/core/arch/arm/plat-stm32mp1/pm/power_config.c +@@ -0,0 +1,261 @@ ++// SPDX-License-Identifier: BSD-3-Clause ++/* ++ * Copyright (c) 2017-2021, STMicroelectronics - All Rights Reserved ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#include "context.h" ++#include "power.h" ++ ++#define DT_PWR_COMPAT "st,stm32mp1,pwr-reg" ++#define SYSTEM_SUSPEND_SUPPORTED_MODES "system_suspend_supported_soc_modes" ++#define SYSTEM_OFF_MODE "system_off_soc_mode" ++#define RETRAM_ENABLED "st,retram-enabled-in-standby-ddr-sr" ++ ++static uint32_t deepest_suspend_mode; ++static uint32_t system_off_mode; ++static bool retram_enabled; ++static uint8_t stm32mp1_supported_soc_modes[STM32_PM_MAX_SOC_MODE]; ++ ++/* Boot with all domains ON */ ++static bool stm32mp1_pm_dom[STM32MP1_PD_MAX_PM_DOMAIN] = { ++ [STM32MP1_PD_VSW] = false, ++ [STM32MP1_PD_CORE_RET] = false, ++ [STM32MP1_PD_CORE] = false ++}; ++ ++int plat_get_lp_mode_count(void) ++{ ++ return STM32_PM_MAX_SOC_MODE; ++} ++ ++bool stm32mp1_is_retram_during_standby(void) ++{ ++ return retram_enabled; ++} ++ ++bool need_to_backup_cpu_context(unsigned int soc_mode) ++{ ++ switch (soc_mode) { ++ case STM32_PM_CSTOP_ALLOW_LPLV_STOP2: ++ case STM32_PM_CSTOP_ALLOW_STANDBY_DDR_SR: ++ return true; ++ case STM32_PM_CSLEEP_RUN: ++ case STM32_PM_CSTOP_ALLOW_STOP: ++ case STM32_PM_CSTOP_ALLOW_LP_STOP: ++ case STM32_PM_CSTOP_ALLOW_LPLV_STOP: ++ case STM32_PM_CSTOP_ALLOW_STANDBY_DDR_OFF: ++ case STM32_PM_SHUTDOWN: ++ return false; ++ default: ++ EMSG("Invalid mode 0x%x", soc_mode); ++ panic(); ++ } ++} ++ ++bool need_to_backup_stop_context(unsigned int soc_mode) ++{ ++ switch (soc_mode) { ++ case STM32_PM_CSTOP_ALLOW_STOP: ++ case STM32_PM_CSTOP_ALLOW_LP_STOP: ++ case STM32_PM_CSTOP_ALLOW_LPLV_STOP: ++ return true; ++ default: ++ return false; ++ } ++} ++ ++static bool get_pm_domain_state(uint8_t mode) ++{ ++ bool res = true; ++ enum stm32mp1_pm_domain id = STM32MP1_PD_MAX_PM_DOMAIN; ++ ++ while (res && (id > mode)) { ++ id--; ++ res &= stm32mp1_pm_dom[id]; ++ } ++ ++ return res; ++} ++ ++int stm32mp1_set_pm_domain_state(enum stm32mp1_pm_domain domain, bool status) ++{ ++ if (domain >= STM32MP1_PD_MAX_PM_DOMAIN) ++ return -1; ++ ++ stm32mp1_pm_dom[domain] = status; ++ ++ return 0; ++} ++ ++#ifdef CFG_EMBED_DTB ++static void save_supported_mode(void *fdt, int pwr_node) ++{ ++ int len = 0; ++ uint32_t count = 0; ++ unsigned int i = 0; ++ uint32_t supported[ARRAY_SIZE(stm32mp1_supported_soc_modes)] = { }; ++ const void *prop = 0; ++ ++ prop = fdt_getprop(fdt, pwr_node, SYSTEM_SUSPEND_SUPPORTED_MODES, &len); ++ if (!prop) ++ panic(); ++ ++ count = (uint32_t)len / sizeof(uint32_t); ++ if (count > STM32_PM_MAX_SOC_MODE) ++ panic(); ++ ++ if (_fdt_read_uint32_array(fdt, pwr_node, ++ SYSTEM_SUSPEND_SUPPORTED_MODES, ++ &supported[0], count) < 0) ++ panic("PWR DT"); ++ ++ for (i = 0; i < count; i++) { ++ if (supported[i] >= STM32_PM_MAX_SOC_MODE) ++ panic("Invalid mode"); ++ ++ stm32mp1_supported_soc_modes[supported[i]] = true; ++ } ++} ++#endif ++ ++static bool is_supported_mode(uint32_t soc_mode) ++{ ++ assert(soc_mode < ARRAY_SIZE(stm32mp1_supported_soc_modes)); ++ return stm32mp1_supported_soc_modes[soc_mode] == 1; ++} ++ ++uint32_t stm32mp1_get_lp_soc_mode(uint32_t psci_mode) ++{ ++ uint32_t mode = 0; ++ ++ if (psci_mode == PSCI_MODE_SYSTEM_OFF) ++ return system_off_mode; ++ ++ mode = deepest_suspend_mode; ++ ++ if ((mode == STM32_PM_CSTOP_ALLOW_STANDBY_DDR_SR) && ++ (!get_pm_domain_state(STM32MP1_PD_CORE_RET) || ++ !is_supported_mode(mode))) ++ mode = STM32_PM_CSTOP_ALLOW_LPLV_STOP2; ++ ++ if ((mode == STM32_PM_CSTOP_ALLOW_LPLV_STOP2) && ++ (!get_pm_domain_state(STM32MP1_PD_CORE) || ++ !is_supported_mode(mode))) ++ mode = STM32_PM_CSTOP_ALLOW_LPLV_STOP; ++ ++ if ((mode == STM32_PM_CSTOP_ALLOW_LPLV_STOP) && ++ (!get_pm_domain_state(STM32MP1_PD_CORE) || ++ !is_supported_mode(mode))) ++ mode = STM32_PM_CSTOP_ALLOW_LP_STOP; ++ ++ if ((mode == STM32_PM_CSTOP_ALLOW_LP_STOP) && ++ !is_supported_mode(mode)) ++ mode = STM32_PM_CSTOP_ALLOW_STOP; ++ ++ if ((mode == STM32_PM_CSTOP_ALLOW_STOP) && ++ !is_supported_mode(mode)) ++ mode = STM32_PM_CSLEEP_RUN; ++ ++ return mode; ++} ++ ++int stm32mp1_set_lp_deepest_soc_mode(uint32_t psci_mode, uint32_t soc_mode) ++{ ++ if (soc_mode >= STM32_PM_MAX_SOC_MODE) ++ return -1; ++ ++ if (psci_mode == PSCI_MODE_SYSTEM_SUSPEND) { ++ deepest_suspend_mode = soc_mode; ++ ++#ifdef CFG_STM32MP15 ++ if (!stm32mp_supports_hw_cryp() && ++ deepest_suspend_mode == STM32_PM_CSTOP_ALLOW_STANDBY_DDR_SR) ++ deepest_suspend_mode = STM32_PM_CSTOP_ALLOW_LPLV_STOP; ++#endif ++ } ++ ++ if (psci_mode == PSCI_MODE_SYSTEM_OFF) ++ system_off_mode = soc_mode; ++ ++ return 0; ++} ++ ++#ifdef CFG_EMBED_DTB ++static int dt_get_pwr_node(void *fdt) ++{ ++ return fdt_node_offset_by_compatible(fdt, -1, DT_PWR_COMPAT); ++} ++ ++static TEE_Result stm32mp1_init_lp_states(void) ++{ ++ void *fdt = NULL; ++ int pwr_node = -1; ++ const fdt32_t *cuint = NULL; ++ ++ fdt = get_embedded_dt(); ++ if (fdt) ++ pwr_node = dt_get_pwr_node(fdt); ++ ++ if (pwr_node >= 0) { ++ if (fdt_getprop(fdt, pwr_node, RETRAM_ENABLED, NULL)) ++ retram_enabled = true; ++ ++ cuint = fdt_getprop(fdt, pwr_node, SYSTEM_OFF_MODE, NULL); ++ } ++ ++ if (!fdt || (pwr_node < 0) || !cuint) { ++ IMSG("No power configuration found in DT"); ++ return TEE_SUCCESS; ++ } ++ ++ system_off_mode = fdt32_to_cpu(*cuint); ++ ++ /* Initialize suspend support to the deepest possible mode */ ++ deepest_suspend_mode = STM32_PM_CSTOP_ALLOW_STANDBY_DDR_SR; ++ ++#ifdef CFG_STM32MP15 ++ if (!stm32mp_supports_hw_cryp()) ++ deepest_suspend_mode = STM32_PM_CSTOP_ALLOW_LPLV_STOP; ++#endif ++ ++ save_supported_mode(fdt, pwr_node); ++ ++ DMSG("Power configuration: shutdown to %u, suspend to %u", ++ stm32mp1_get_lp_soc_mode(PSCI_MODE_SYSTEM_OFF), ++ stm32mp1_get_lp_soc_mode(PSCI_MODE_SYSTEM_SUSPEND)); ++ ++ return TEE_SUCCESS; ++} ++#else ++static TEE_Result stm32mp1_init_lp_states(void) ++{ ++ deepest_suspend_mode = STM32_PM_CSTOP_ALLOW_STANDBY_DDR_SR; ++ ++#ifdef CFG_STM32MP15 ++ if (!stm32mp_supports_hw_cryp()) ++ deepest_suspend_mode = STM32_PM_CSTOP_ALLOW_LPLV_STOP; ++#endif ++ ++ system_off_mode = STM32_PM_SHUTDOWN; ++ ++ DMSG("Power configuration: shutdown to %u, suspend to %u", ++ stm32mp1_get_lp_soc_mode(PSCI_MODE_SYSTEM_OFF), ++ stm32mp1_get_lp_soc_mode(PSCI_MODE_SYSTEM_SUSPEND)); ++ ++ return TEE_SUCCESS; ++} ++#endif ++driver_init_late(stm32mp1_init_lp_states); +diff --git a/core/arch/arm/plat-stm32mp1/pm/psci.c b/core/arch/arm/plat-stm32mp1/pm/psci.c +index 9c472c796..11bcef6a3 100644 +--- a/core/arch/arm/plat-stm32mp1/pm/psci.c ++++ b/core/arch/arm/plat-stm32mp1/pm/psci.c +@@ -1,29 +1,40 @@ + // SPDX-License-Identifier: BSD-2-Clause + /* +- * Copyright (c) 2017-2018, STMicroelectronics ++ * Copyright (c) 2017-2021, STMicroelectronics + */ + + #include + #include ++#include + #include ++#include ++#include + #include +-#include + #include +-#include ++#include + #include ++#include + #include + #include + #include + #include + #include + #include ++#include + #include + #include + #include ++#include + #include + #include + #include ++#include ++#include + #include ++#include ++ ++#include "context.h" ++#include "power.h" + + #define CONSOLE_FLUSH_DELAY_MS 10 + +@@ -57,10 +68,9 @@ int psci_affinity_info(uint32_t affinity, uint32_t lowest_affinity_level) + + DMSG("core %zu, state %u", pos, core_state[pos]); + +- if ((pos >= CFG_TEE_CORE_NB_CORE) || +- (lowest_affinity_level > PSCI_AFFINITY_LEVEL_ON)) { ++ if (pos >= CFG_TEE_CORE_NB_CORE || ++ lowest_affinity_level > PSCI_AFFINITY_LEVEL_ON) + return PSCI_RET_INVALID_PARAMETERS; +- } + + switch (core_state[pos]) { + case CORE_OFF: +@@ -82,38 +92,40 @@ int psci_affinity_info(uint32_t affinity, uint32_t lowest_affinity_level) + */ + void stm32mp_register_online_cpu(void) + { +- assert(core_state[0] == CORE_OFF); ++ assert(core_state[0] == CORE_OFF || core_state[0] == CORE_RET); + core_state[0] = CORE_ON; + } + #else +-static void __noreturn stm32_pm_cpu_power_down_wfi(void) +-{ +- dcache_op_level1(DCACHE_OP_CLEAN); +- +- io_write32(stm32_rcc_base() + RCC_MP_GRSTCSETR, +- RCC_MP_GRSTCSETR_MPUP1RST); +- +- dsb(); +- isb(); +- wfi(); +- panic(); +-} +- + void stm32mp_register_online_cpu(void) + { + size_t pos = get_core_pos(); + uint32_t exceptions = lock_state_access(); ++ struct clk *clk_rtc = stm32mp_rcc_clock_id_to_clk(RTCAPB); ++ ++ assert(clk_rtc); + + if (pos == 0) { +- assert(core_state[pos] == CORE_OFF); ++ assert((core_state[pos] == CORE_OFF) || ++ (core_state[pos] == CORE_RET)); + } else { ++ /* Check if second core available */ ++ if (!stm32mp_supports_second_core()) ++ return; ++ + if (core_state[pos] != CORE_AWAKE) { + core_state[pos] = CORE_OFF; + unlock_state_access(exceptions); +- stm32_pm_cpu_power_down_wfi(); ++ if (IS_ENABLED(CFG_PM)) ++ stm32_pm_cpu_power_down_wfi(); + panic(); + } +- stm32_clock_disable(RTCAPB); ++ ++ /* Clear hold in pen flag */ ++ io_write32(stm32mp_bkpreg(BCKR_CORE1_MAGIC_NUMBER), ++ BOOT_API_A7_RESET_MAGIC_NUMBER); ++ ++ /* Balance BKPREG clock gating */ ++ clk_disable(clk_rtc); + } + + core_state[pos] = CORE_ON; +@@ -130,15 +142,22 @@ static void raise_sgi0_as_secure(void) + + static void release_secondary_early_hpen(size_t __unused pos) + { ++ struct clk *clk_rtc = stm32mp_rcc_clock_id_to_clk(RTCAPB); ++ + /* Need to send SIG#0 over Group0 after individual core 1 reset */ + raise_sgi0_as_secure(); + udelay(20); + ++ clk_enable(clk_rtc); ++ + io_write32(stm32mp_bkpreg(BCKR_CORE1_BRANCH_ADDRESS), + TEE_LOAD_ADDR); + io_write32(stm32mp_bkpreg(BCKR_CORE1_MAGIC_NUMBER), + BOOT_API_A7_CORE1_MAGIC_NUMBER); + ++ dsb_ishst(); ++ clk_disable(clk_rtc); ++ + dsb_ishst(); + itr_raise_sgi(GIC_SEC_SGI_0, TARGET_CPU1_GIC_MASK); + } +@@ -191,6 +210,9 @@ int psci_cpu_off(void) + { + unsigned int pos = get_core_pos(); + uint32_t exceptions = 0; ++ struct clk *clk_rtc = stm32mp_rcc_clock_id_to_clk(RTCAPB); ++ ++ assert(clk_rtc); + + if (pos == 0) { + EMSG("PSCI_CPU_OFF not supported for core #0"); +@@ -207,42 +229,213 @@ int psci_cpu_off(void) + unlock_state_access(exceptions); + + /* Enable BKPREG access for the disabled CPU */ +- stm32_clock_enable(RTCAPB); ++ clk_enable(clk_rtc); + + thread_mask_exceptions(THREAD_EXCP_ALL); +- stm32_pm_cpu_power_down_wfi(); ++ if (IS_ENABLED(CFG_PM)) ++ stm32_pm_cpu_power_down_wfi(); ++ + panic(); + } + #endif + ++#ifdef CFG_PM ++static int enter_cstop_suspend(unsigned int soc_mode) ++{ ++ int rc = 1; ++ ++ if (read_isr()) ++ return rc; ++ ++ stm32_enter_cstop(soc_mode); ++ ++#ifdef CFG_STM32MP13 ++ stm32mp_pm_call_bl2_lp_entry(soc_mode); ++ rc = 0; ++#else ++ if (need_to_backup_cpu_context(soc_mode)) { ++ stm32_pm_cpu_power_down_wfi(); ++ } else { ++ stm32_pm_cpu_wfi(); ++ rc = 0; ++ } ++#endif ++ ++ stm32_exit_cstop(); ++ ++ return rc; ++} ++ ++static int plat_suspend(uint32_t arg) ++{ ++ unsigned int soc_mode = arg; ++ size_t pos = get_core_pos(); ++ int rc = 1; ++ ++ if (read_isr()) ++ return rc; ++ ++ /* No need to lock state access as CPU is alone when here */ ++ assert(core_state[pos] == CORE_ON); ++ core_state[pos] = CORE_RET; ++ ++ if (stm32mp_pm_save_context(soc_mode) == TEE_SUCCESS) ++ rc = enter_cstop_suspend(soc_mode); ++ ++ stm32mp_pm_restore_context(soc_mode); ++ stm32mp_pm_wipe_context(); ++ ++ assert(core_state[pos] == CORE_RET); ++ core_state[pos] = CORE_ON; ++ ++ return rc; ++} ++ ++static void plat_resume(uint32_t arg) ++{ ++ unsigned int soc_mode = arg; ++ ++ stm32mp_register_online_cpu(); ++ ++ assert(core_state[get_core_pos()] == CORE_ON); ++ ++ stm32mp_pm_restore_context(soc_mode); ++} ++ ++static bool plat_can_suspend(void) ++{ ++ size_t pos = get_core_pos(); ++ size_t n = 0; ++ uint32_t exceptions = 0; ++ bool rc = true; ++ ++#ifndef CFG_STM32MP13 ++ if (!IS_ENABLED(CFG_STM32_RNG)) ++ return false; ++#endif ++ ++ if (CFG_TEE_CORE_NB_CORE == 1) ++ return true; ++ ++ exceptions = lock_state_access(); ++ ++ for (n = 0; n < ARRAY_SIZE(core_state); n++) { ++ if (n == pos) ++ continue; ++ ++ if (core_state[n] == CORE_AWAKE) { ++ /* State core as lost and proceed suspend */ ++ core_state[n] = CORE_OFF; ++ } ++ ++ if (core_state[n] != CORE_OFF) ++ rc = false; ++ } ++ ++ unlock_state_access(exceptions); ++ ++ return rc; ++} ++ ++/* Override default psci_system_suspend() with platform specific sequence */ ++int psci_system_suspend(uintptr_t entry, uint32_t context_id __unused, ++ struct sm_nsec_ctx *nsec) ++{ ++ int ret = PSCI_RET_INVALID_PARAMETERS; ++ uint32_t soc_mode = 0; ++ int __maybe_unused pos = get_core_pos(); ++ ++ DMSG("core %u", pos); ++ ++ if (!plat_can_suspend()) ++ return PSCI_RET_DENIED; ++ ++ soc_mode = stm32mp1_get_lp_soc_mode(PSCI_MODE_SYSTEM_SUSPEND); ++ ++ DMSG("Soc mode requested is %i", soc_mode); ++ ++ if (soc_mode == STM32_PM_CSLEEP_RUN) { ++ stm32_enter_csleep(); ++ nsec->mon_lr = (uint32_t)entry; ++ return PSCI_RET_SUCCESS; ++ } ++ ++ assert(cpu_mmu_enabled() && core_state[pos] == CORE_ON); ++ ++ if (need_to_backup_cpu_context(soc_mode)) { ++#ifdef CFG_STM32MP15 ++ if (!stm32mp_supports_hw_cryp()) ++ return PSCI_RET_DENIED; ++#endif ++ ++ sm_save_unbanked_regs(&nsec->ub_regs); ++ /* ++ * sm_pm_cpu_suspend(arg, func) saves the CPU core context in TEE RAM ++ * then calls func(arg) to run the platform lower power sequence. ++ * ++ * If platform fails to suspend, sm_pm_cpu_suspend() returns with a ++ * non null return code. When sm_pm_cpu_suspend() returns 0 platform ++ * context must be restored. ++ */ ++ ret = sm_pm_cpu_suspend((uint32_t)soc_mode, plat_suspend); ++ if (ret == 0) { ++#ifndef CFG_STM32MP15 ++ stm32_exit_cstop(); ++#endif ++ plat_resume((uint32_t)soc_mode); ++ sm_restore_unbanked_regs(&nsec->ub_regs); ++ } ++ } else { ++ ret = plat_suspend((uint32_t)soc_mode); ++ } ++ ++ if (ret == 0) { ++ nsec->mon_lr = (uint32_t)entry; ++ IMSG("Resumed"); ++ return PSCI_RET_SUCCESS; ++ } ++ ++ return PSCI_RET_INTERNAL_FAILURE; ++} ++ + /* Override default psci_system_off() with platform specific sequence */ + void __noreturn psci_system_off(void) + { ++ uint32_t soc_mode = stm32mp1_get_lp_soc_mode(PSCI_MODE_SYSTEM_OFF); ++ + DMSG("core %u", get_core_pos()); + +- if (TRACE_LEVEL >= TRACE_DEBUG) { +- console_flush(); +- mdelay(CONSOLE_FLUSH_DELAY_MS); +- } ++ if (pm_change_state(PM_OP_SUSPEND, soc_mode)) ++ panic(); + ++ stm32_enter_cstop_shutdown(soc_mode); ++} ++#else /* CFG_PM */ ++/* Override default psci_system_off() with platform specific sequence */ ++void __noreturn psci_system_off(void) ++{ + if (stm32mp_with_pmic()) { +- stm32mp_get_pmic(); ++ console_flush(); ++ mdelay(10); ++ stm32mp_pm_get_pmic(); + stpmic1_switch_off(); + udelay(100); + } + + panic(); + } ++#endif /* CFG_PM */ + + /* Override default psci_system_reset() with platform specific sequence */ + void __noreturn psci_system_reset(void) + { +- vaddr_t rcc_base = stm32_rcc_base(); ++ IMSG("Forced system reset"); + +- DMSG("core %u", get_core_pos()); +- +- io_write32(rcc_base + RCC_MP_GRSTCSETR, RCC_MP_GRSTCSETR_MPSYSRST); ++ console_flush(); ++ mdelay(10); ++ stm32_reset_system(); + udelay(100); ++ + panic(); + } + +@@ -257,13 +450,12 @@ int psci_features(uint32_t psci_fid) + return PSCI_RET_SUCCESS; + case PSCI_CPU_ON: + case PSCI_CPU_OFF: +- if (CFG_TEE_CORE_NB_CORE > 1) ++ if (CFG_TEE_CORE_NB_CORE > 1 && stm32mp_supports_second_core()) + return PSCI_RET_SUCCESS; + return PSCI_RET_NOT_SUPPORTED; + case PSCI_SYSTEM_OFF: +- if (stm32mp_with_pmic()) +- return PSCI_RET_SUCCESS; +- return PSCI_RET_NOT_SUPPORTED; ++ case PSCI_SYSTEM_SUSPEND: ++ return PSCI_RET_SUCCESS; + default: + return PSCI_RET_NOT_SUPPORTED; + } +diff --git a/core/arch/arm/plat-stm32mp1/pm/sub.mk b/core/arch/arm/plat-stm32mp1/pm/sub.mk +index c8ae8f915..d652958e3 100644 +--- a/core/arch/arm/plat-stm32mp1/pm/sub.mk ++++ b/core/arch/arm/plat-stm32mp1/pm/sub.mk +@@ -1 +1,7 @@ ++asm-defines-y += context_asm_defines.c ++ ++srcs-$(CFG_PM) += context.c ++srcs-$(CFG_PM) += low_power.c ++srcs-$(CFG_PM) += pm_helpers.S ++srcs-$(CFG_PM) += power_config.c + srcs-$(CFG_PSCI_ARM32) += psci.c +diff --git a/core/arch/arm/plat-stm32mp1/remoteproc_pta.c b/core/arch/arm/plat-stm32mp1/remoteproc_pta.c +new file mode 100644 +index 000000000..4c787d1a6 +--- /dev/null ++++ b/core/arch/arm/plat-stm32mp1/remoteproc_pta.c +@@ -0,0 +1,540 @@ ++ // SPDX-License-Identifier: BSD-2-Clause ++ /* ++ * Copyright (C) 2020-2021, STMicroelectronics - All Rights Reserved ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#define PTA_NAME "remoteproc.pta" ++ ++#define STM32_M4_FW_ID 0 ++ ++/* Firmware states */ ++enum rproc_load_state { ++ REMOTEPROC_OFF = 0, ++ REMOTEPROC_ON, ++}; ++ ++/* ++ * struct rproc_ta_etzpc_rams - memory protection strategy table ++ * @pa - Memory physical base address from current CPU space ++ * @size - Memory region byte size ++ * @firewall_cfg - Firewall configuration. ++ * @attr - memory access permission according to @etzpc_decprot_attributes ++ */ ++struct rproc_ta_etzpc_rams { ++ paddr_t pa; ++ size_t size; ++ struct stm32_firewall_cfg cfg[2]; ++}; ++ ++/* ++ * struct rproc_ta_memory_region - Represent a remote processor memory mapping ++ * @pa - Memory physical base address from current CPU space ++ * @da - Memory physical base address from remote processor space ++ * @size - Memory region byte size ++ */ ++struct rproc_ta_memory_region { ++ paddr_t pa; ++ paddr_t da; ++ size_t size; ++}; ++ ++static const struct rproc_ta_etzpc_rams rproc_ta_mp1_m4_rams[] = { ++ /* MCU SRAM 1*/ ++ { ++ .pa = MCUSRAM_BASE, ++ .size = 0x20000, ++ .cfg = { ++ { FWLL_NSEC_RW | FWLL_MASTER(1) }, ++ { }, /* Null terminated */ ++ }, ++ }, ++ /* MCU SRAM 2*/ ++ { ++ .pa = MCUSRAM_BASE + 0x20000, ++ .size = 0x20000, ++ .cfg = { ++ { FWLL_NSEC_RW | FWLL_MASTER(1) }, ++ { }, /* Null terminated */ ++ }, ++ }, ++ ++ /* MCU SRAM 3*/ ++ { ++ /* Used as shared memory between the NS and the coprocessor */ ++ .pa = MCUSRAM_BASE + 0x40000, ++ .size = 0x10000, ++ .cfg = { ++ { FWLL_NSEC_RW | FWLL_MASTER(0) }, ++ { }, /* Null terminated */ ++ }, ++ }, ++ /* MCU SRAM 4*/ ++ /* Not used reserved by NS for MDMA */ ++ { ++ .pa = MCUSRAM_BASE + 0x50000, ++ .size = 0x10000, ++ .cfg = { ++ { FWLL_NSEC_RW | FWLL_MASTER(0) }, ++ { }, /* Null terminated */ ++ }, ++ }, ++ ++ /* MCU RETRAM */ ++ { ++ .pa = RETRAM_BASE, ++ .size = RETRAM_SIZE, ++ .cfg = { ++ { FWLL_NSEC_RW | FWLL_MASTER(1) }, ++ { }, /* Null terminated */ ++ }, ++ }, ++}; ++ ++static const struct rproc_ta_memory_region rproc_ta_mp1_m4_mems[] = { ++ /* MCU SRAM */ ++ { .pa = MCUSRAM_BASE, .da = 0x10000000, .size = MCUSRAM_SIZE }, ++ /* Alias of the MCU SRAM */ ++ { .pa = MCUSRAM_BASE, .da = 0x30000000, .size = MCUSRAM_SIZE }, ++ /* RETRAM */ ++ { .pa = RETRAM_BASE, .da = 0x00000000, .size = RETRAM_SIZE }, ++}; ++ ++static enum rproc_load_state rproc_ta_state; ++ ++static TEE_Result rproc_pta_capabilities(uint32_t pt, ++ TEE_Param params[TEE_NUM_PARAMS]) ++{ ++ const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_VALUE_OUTPUT, ++ TEE_PARAM_TYPE_VALUE_OUTPUT, ++ TEE_PARAM_TYPE_NONE); ++ ++ if (pt != exp_pt) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ /* Support only ELF format */ ++ params[1].value.a = PTA_REMOTEPROC_ELF_FMT; ++ ++ /* ++ * Due to stm32mp1 pager, secure memory is too expensive. Support hash ++ * protected image only, so that firmware image can be loaded from ++ * non-secure memory. ++ */ ++ params[2].value.a = PTA_REMOTEPROC_FW_WITH_HASH_TABLE; ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result da_to_pa(paddr_t da, size_t size, paddr_t *pa) ++{ ++ const struct rproc_ta_memory_region *mems = rproc_ta_mp1_m4_mems; ++ size_t i = 0; ++ ++ DMSG("da addr: %#"PRIxPA" size: %zu", da, size); ++ ++ for (i = 0; i < ARRAY_SIZE(rproc_ta_mp1_m4_mems); i++) { ++ if (da >= mems[i].da && ++ (da + size) <= (mems[i].da + mems[i].size)) { ++ *pa = da - mems[i].da + mems[i].pa; ++ DMSG("da %#"PRIxPA" to pa %#"PRIxPA, da, *pa); ++ ++ return TEE_SUCCESS; ++ } ++ } ++ ++ return TEE_ERROR_ACCESS_DENIED; ++} ++ ++static TEE_Result rproc_pta_load_segment(uint32_t pt, ++ TEE_Param params[TEE_NUM_PARAMS]) ++{ ++ const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_MEMREF_INPUT, ++ TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_MEMREF_INPUT); ++ TEE_Result res = TEE_ERROR_GENERIC; ++ paddr_t pa = 0; ++ uint8_t *dst = 0; ++ uint8_t *src = params[1].memref.buffer; ++ size_t size = params[1].memref.size; ++ uint8_t *hash = params[3].memref.buffer; ++ paddr_t da = (paddr_t)reg_pair_to_64(params[2].value.b, ++ params[2].value.a); ++ ++ if (pt != exp_pt) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ if (!hash || params[3].memref.size != TEE_SHA256_HASH_SIZE) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ /* Only STM32_M4_FW_ID supported */ ++ if (params[0].value.a != STM32_M4_FW_ID) { ++ EMSG("Unsupported firmware ID %#"PRIx32, params[0].value.a); ++ return TEE_ERROR_NOT_SUPPORTED; ++ } ++ ++ if (rproc_ta_state != REMOTEPROC_OFF) ++ return TEE_ERROR_BAD_STATE; ++ ++ /* Get the physical address in A7 mapping */ ++ res = da_to_pa(da, size, &pa); ++ if (res) ++ return res; ++ ++ /* Get the associated va */ ++ dst = (void *)core_mmu_get_va(pa, MEM_AREA_IO_SEC, 1); ++ ++ /* Copy the segment to the remote processor memory*/ ++ memcpy(dst, src, size); ++ ++ /* Verify that loaded segment is valid */ ++ res = hash_sha256_check(hash, dst, size); ++ if (res) ++ memset(dst, 0, size); ++ ++ return res; ++} ++ ++static TEE_Result rproc_pta_set_memory(uint32_t pt, ++ TEE_Param params[TEE_NUM_PARAMS]) ++{ ++ const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_VALUE_INPUT); ++ TEE_Result res = TEE_ERROR_GENERIC; ++ paddr_t pa = 0; ++ vaddr_t dst = 0; ++ paddr_t da = params[1].value.a; ++ size_t size = params[2].value.a; ++ char value = (char)params[3].value.a; ++ ++ if (pt != exp_pt) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ /* Only STM32_M4_FW_ID supported */ ++ if (params[0].value.a != STM32_M4_FW_ID) { ++ EMSG("Unsupported firmware ID %#"PRIx32, params[0].value.a); ++ return TEE_ERROR_NOT_SUPPORTED; ++ } ++ ++ if (rproc_ta_state != REMOTEPROC_OFF) ++ return TEE_ERROR_BAD_STATE; ++ ++ /* Get the physical address in CPU mapping */ ++ res = da_to_pa(da, size, &pa); ++ if (res) ++ return res; ++ ++ dst = core_mmu_get_va(pa, MEM_AREA_IO_SEC, 1); ++ ++ memset((void *)dst, value, size); ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result rproc_pta_da_to_pa(uint32_t pt, ++ TEE_Param params[TEE_NUM_PARAMS]) ++{ ++ const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_VALUE_OUTPUT); ++ TEE_Result res = TEE_ERROR_GENERIC; ++ paddr_t da = params[1].value.a; ++ size_t size = params[2].value.a; ++ paddr_t pa = 0; ++ ++ DMSG("Conversion for address %#"PRIxPA" size %zu", da, size); ++ ++ if (pt != exp_pt) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ /* Only STM32_M4_FW_ID supported */ ++ if (params[0].value.a != STM32_M4_FW_ID) { ++ EMSG("Unsupported firmware ID %#"PRIx32, params[0].value.a); ++ return TEE_ERROR_NOT_SUPPORTED; ++ } ++ ++ /* Target address is expected 32bit, ensure 32bit MSB are zero */ ++ if (params[1].value.b || params[2].value.b) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ res = da_to_pa(da, size, &pa); ++ if (res) ++ return res; ++ ++ reg_pair_from_64((uint64_t)pa, ¶ms[3].value.b, ¶ms[3].value.a); ++ ++ return TEE_SUCCESS; ++} ++ ++static void rproc_pta_mem_protect(bool secure_access) ++{ ++ unsigned int i = 0; ++ const struct stm32_firewall_cfg *sec = NULL; ++ const struct rproc_ta_etzpc_rams *ram = NULL; ++ const struct stm32_firewall_cfg sec_cfg[] = { ++ { FWLL_SEC_RW | FWLL_MASTER(0) }, ++ { }, /* Null terminated */ ++ }; ++ ++ /* ++ * MCU RAM banks access permissions for MCU memories depending on ++ * rproc_ta_mp1_m4_rams[]. ++ * If memory bank is declared as MCU isolated: ++ * if secure_access then set to secure world read/write permission ++ * else set to MCU isolated ++ * else apply memory permission as defined in rproc_ta_mp1_m4_rams[]. ++ */ ++ for (i = 0; i < ARRAY_SIZE(rproc_ta_mp1_m4_rams); i++) { ++ ram = &rproc_ta_mp1_m4_rams[i]; ++ sec = ram->cfg; ++ ++ if (secure_access && ++ (stm32_firewall_check_access(ram->pa, ram->size, sec_cfg) != ++ TEE_SUCCESS)) ++ sec = sec_cfg; ++ ++ stm32_firewall_set_config(ram->pa, ram->size, sec); ++ } ++} ++ ++static TEE_Result rproc_pta_start(uint32_t pt, ++ TEE_Param params[TEE_NUM_PARAMS]) ++{ ++ const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_NONE, ++ TEE_PARAM_TYPE_NONE, ++ TEE_PARAM_TYPE_NONE); ++ vaddr_t rcc_base = stm32_rcc_base(); ++ struct clk *mcu_clk = stm32mp_rcc_clock_id_to_clk(CK_MCU); ++ ++ if (pt != exp_pt) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ /* Only STM32_M4_FW_ID supported */ ++ if (params[0].value.a != STM32_M4_FW_ID) { ++ EMSG("Unsupported firmware ID %#"PRIx32, params[0].value.a); ++ return TEE_ERROR_NOT_SUPPORTED; ++ } ++ ++ if (rproc_ta_state != REMOTEPROC_OFF) ++ return TEE_ERROR_BAD_STATE; ++ ++ clk_enable(mcu_clk); ++ ++ /* Configure the Cortex-M4 RAMs as expected to run the firmware */ ++ rproc_pta_mem_protect(false); ++ ++ /* ++ * The firmware is started by deasserting the hold boot and ++ * asserting back to avoid auto restart on a crash. ++ * No need to release the MCU reset as it is automatically released by ++ * the hardware. ++ */ ++ io_setbits32(rcc_base + RCC_MP_GCR, RCC_MP_GCR_BOOT_MCU); ++ io_clrbits32(rcc_base + RCC_MP_GCR, RCC_MP_GCR_BOOT_MCU); ++ ++ rproc_ta_state = REMOTEPROC_ON; ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result rproc_pta_stop(uint32_t pt, ++ TEE_Param params[TEE_NUM_PARAMS]) ++{ ++ const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_NONE, ++ TEE_PARAM_TYPE_NONE, ++ TEE_PARAM_TYPE_NONE); ++ struct clk *mcu_clk = stm32mp_rcc_clock_id_to_clk(CK_MCU); ++ const struct rproc_ta_etzpc_rams *ram = NULL; ++ vaddr_t rcc_base = stm32_rcc_base(); ++ unsigned int i = 0; ++ ++ if (pt != exp_pt) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ /* Only STM32_M4_FW_ID supported */ ++ if (params[0].value.a != STM32_M4_FW_ID) { ++ EMSG("Unsupported firmware ID %#"PRIx32, params[0].value.a); ++ return TEE_ERROR_NOT_SUPPORTED; ++ } ++ ++ if (rproc_ta_state != REMOTEPROC_ON) ++ return TEE_ERROR_BAD_STATE; ++ ++ /* The firmware is stopped (reset with holdboot is active) */ ++ io_clrbits32(rcc_base + RCC_MP_GCR, RCC_MP_GCR_BOOT_MCU); ++ ++ stm32_reset_set(MCU_R); ++ ++ clk_disable(mcu_clk); ++ ++ /* ++ * Cortex-M4 memories are cleaned and access rights restored for the ++ * secure context. ++ */ ++ rproc_pta_mem_protect(true); ++ for (i = 0; i < ARRAY_SIZE(rproc_ta_mp1_m4_rams); i++) { ++ ram = &rproc_ta_mp1_m4_rams[i]; ++ if (stm32_firewall_check_access(ram->pa, ram->size, ram->cfg) == ++ TEE_SUCCESS) { ++ memset((void *)core_mmu_get_va(ram->pa, ++ MEM_AREA_IO_SEC, 1), ++ 0, ram->size); ++ } ++ } ++ rproc_ta_state = REMOTEPROC_OFF; ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result rproc_pta_verify_rsa_signature(TEE_Param *hash, ++ TEE_Param *sig, uint32_t algo) ++{ ++ struct rsa_public_key key = { }; ++ TEE_Result res = TEE_ERROR_GENERIC; ++ uint32_t e = TEE_U32_TO_BIG_ENDIAN(rproc_pub_key_exponent); ++ size_t hash_size = (size_t)hash->memref.size; ++ size_t sig_size = (size_t)sig->memref.size; ++ ++ res = crypto_acipher_alloc_rsa_public_key(&key, sig_size); ++ if (res) ++ return TEE_ERROR_SECURITY; ++ ++ res = crypto_bignum_bin2bn((uint8_t *)&e, sizeof(e), key.e); ++ if (res) ++ goto out; ++ ++ res = crypto_bignum_bin2bn(rproc_pub_key_modulus, ++ rproc_pub_key_modulus_size, key.n); ++ if (res) ++ goto out; ++ ++ res = crypto_acipher_rsassa_verify(algo, &key, hash_size, ++ hash->memref.buffer, hash_size, ++ sig->memref.buffer, sig_size); ++ ++out: ++ crypto_acipher_free_rsa_public_key(&key); ++ ++ return res; ++} ++ ++static TEE_Result rproc_pta_verify_digest(uint32_t pt, ++ TEE_Param params[TEE_NUM_PARAMS]) ++{ ++ struct rproc_pta_key_info *keyinfo = NULL; ++ const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_MEMREF_INPUT, ++ TEE_PARAM_TYPE_MEMREF_INPUT, ++ TEE_PARAM_TYPE_MEMREF_INPUT); ++ ++ if (pt != exp_pt) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ /* Only STM32_M4_FW_ID supported */ ++ if (params[0].value.a != STM32_M4_FW_ID) { ++ EMSG("Unsupported firmware ID %#"PRIx32, params[0].value.a); ++ return TEE_ERROR_NOT_SUPPORTED; ++ } ++ ++ if (rproc_ta_state != REMOTEPROC_OFF) ++ return TEE_ERROR_BAD_STATE; ++ ++ keyinfo = params[1].memref.buffer; ++ ++ if (!keyinfo || ++ RPROC_PTA_GET_KEYINFO_SIZE(keyinfo) != params[1].memref.size) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ if (keyinfo->algo != TEE_ALG_RSASSA_PKCS1_V1_5_SHA256) ++ return TEE_ERROR_NOT_SUPPORTED; ++ ++ return rproc_pta_verify_rsa_signature(¶ms[2], ¶ms[3], ++ keyinfo->algo); ++} ++ ++static TEE_Result rproc_pta_invoke_command(void *pSessionContext __unused, ++ uint32_t cmd_id, ++ uint32_t param_types, ++ TEE_Param params[TEE_NUM_PARAMS]) ++{ ++ switch (cmd_id) { ++ case PTA_REMOTEPROC_HW_CAPABILITIES: ++ return rproc_pta_capabilities(param_types, params); ++ case PTA_REMOTEPROC_LOAD_SEGMENT_SHA256: ++ return rproc_pta_load_segment(param_types, params); ++ case PTA_REMOTEPROC_SET_MEMORY: ++ return rproc_pta_set_memory(param_types, params); ++ case PTA_REMOTEPROC_FIRMWARE_START: ++ return rproc_pta_start(param_types, params); ++ case PTA_REMOTEPROC_FIRMWARE_STOP: ++ return rproc_pta_stop(param_types, params); ++ case PTA_REMOTEPROC_FIRMWARE_DA_TO_PA: ++ return rproc_pta_da_to_pa(param_types, params); ++ case PTA_REMOTEPROC_VERIFY_DIGEST: ++ return rproc_pta_verify_digest(param_types, params); ++ default: ++ break; ++ } ++ ++ return TEE_ERROR_NOT_IMPLEMENTED; ++} ++ ++/* ++ * Trusted Application entry points ++ */ ++static TEE_Result ++ rproc_pta_open_session(uint32_t param_types __unused, ++ TEE_Param params[TEE_NUM_PARAMS] __unused, ++ void **sess_ctx __unused) ++{ ++ struct ts_session *s = ts_get_calling_session(); ++ ++ /* TODO: check that we're called the remove proc TA (check UUID) */ ++ if (!s || !is_user_ta_ctx(s->ctx)) ++ return TEE_ERROR_ACCESS_DENIED; ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result rproc_pta_init(void) ++{ ++ vaddr_t rcc_base = stm32_rcc_base(); ++ ++ /* Configure the Cortex-M4 rams access right for secure context only */ ++ rproc_pta_mem_protect(true); ++ ++ /* Initialise the context */ ++ rproc_ta_state = REMOTEPROC_OFF; ++ ++ /* Ensure that the MCU is HOLD */ ++ io_clrbits32(rcc_base + RCC_MP_GCR, RCC_MP_GCR_BOOT_MCU); ++ stm32_reset_set(MCU_R); ++ ++ return TEE_SUCCESS; ++} ++service_init_late(rproc_pta_init); ++ ++pseudo_ta_register(.uuid = PTA_REMOTEPROC_UUID, .name = PTA_NAME, ++ .flags = PTA_DEFAULT_FLAGS, ++ .invoke_command_entry_point = rproc_pta_invoke_command, ++ .open_session_entry_point = rproc_pta_open_session); +diff --git a/core/arch/arm/plat-stm32mp1/reset.S b/core/arch/arm/plat-stm32mp1/reset.S +index 8a1ab6494..0a11d915a 100644 +--- a/core/arch/arm/plat-stm32mp1/reset.S ++++ b/core/arch/arm/plat-stm32mp1/reset.S +@@ -3,7 +3,7 @@ + * Copyright (c) 2018, STMicroelectronics + */ + +-#include ++#include + #include + #include + +@@ -21,7 +21,33 @@ FUNC plat_cpu_reset_early , : + mov_imm r1, STM32MP1_NSACR_PRESERVE_MASK + and r0, r0, r1 + write_nsacr r0 ++ isb ++ ++ read_actlr r0 ++ orr r0, r0, #ACTLR_SMP ++ write_actlr r0 ++ ++ /* ++ * Always reset CNTVOFF for the dear non secure world. ++ * This operation requires being in Monitor mode and ++ * non secure state. ++ */ ++ mrs r1, cpsr ++ cps #CPSR_MODE_MON ++ isb ++ ++ read_scr r2 ++ orr r0, r2, #SCR_NS ++ write_scr r0 ++ isb ++ ++ mov r0, #0 ++ write_cntvoff r0, r0 + ++ write_scr r2 + isb ++ msr cpsr, r1 ++ isb ++ + bx lr + END_FUNC plat_cpu_reset_early +diff --git a/core/arch/arm/plat-stm32mp1/rng_seed.c b/core/arch/arm/plat-stm32mp1/rng_seed.c +index c61f3be3a..76cce402c 100644 +--- a/core/arch/arm/plat-stm32mp1/rng_seed.c ++++ b/core/arch/arm/plat-stm32mp1/rng_seed.c +@@ -1,50 +1,21 @@ + // SPDX-License-Identifier: BSD-2-Clause + /* +- * Copyright (c) 2020, Linaro Limited ++ * Copyright (c) 2020-2021, Linaro Limited + */ + +-#include + #include +-#include +-#include +-#include + #include +-#include +-#include +-#include + #include + #include + +-#define RNG1_RESET_TIMEOUT_US 1000 + #define PRNG_SEED_SIZE 16 + + /* Override weak plat_rng_init with platform handler to seed PRNG */ + void plat_rng_init(void) + { +- vaddr_t rng = (vaddr_t)phys_to_virt(RNG1_BASE, MEM_AREA_IO_SEC, 1); +- vaddr_t rcc = stm32_rcc_base(); +- uint64_t timeout_ref = timeout_init_us(RNG1_RESET_TIMEOUT_US); +- uint8_t seed[PRNG_SEED_SIZE] = { }; +- size_t size = 0; ++ uint8_t seed[PRNG_SEED_SIZE] = { 0 }; + +- assert(cpu_mmu_enabled()); +- +- /* Setup RNG1 without clock/reset driver support, not yet initialized */ +- io_setbits32(rcc + RCC_MP_AHB5ENSETR, RCC_MP_AHB5ENSETR_RNG1EN); +- io_setbits32(rcc + RCC_MP_AHB5LPENCLRR, RCC_MP_AHB5LPENSETR_RNG1LPEN); +- io_setbits32(rcc + RCC_AHB5RSTSETR, RCC_AHB5RSTSETR_RNG1RST); +- while (!(io_read32(rcc + RCC_AHB5RSTSETR) & RCC_AHB5RSTSETR_RNG1RST)) +- if (timeout_elapsed(timeout_ref)) +- panic(); +- io_setbits32(rcc + RCC_AHB5RSTCLRR, RCC_AHB5RSTSETR_RNG1RST); +- while (io_read32(rcc + RCC_AHB5RSTSETR) & RCC_AHB5RSTSETR_RNG1RST) +- if (timeout_elapsed(timeout_ref)) +- panic(); +- +- size = sizeof(seed); +- if (stm32_rng_read_raw(rng, seed, &size)) +- panic(); +- if (size != sizeof(seed)) ++ if (stm32_rng_read(seed, sizeof(seed))) + panic(); + + if (crypto_rng_init(seed, sizeof(seed))) +diff --git a/core/arch/arm/plat-stm32mp1/rproc_pub_key.h b/core/arch/arm/plat-stm32mp1/rproc_pub_key.h +new file mode 100644 +index 000000000..9b1db4271 +--- /dev/null ++++ b/core/arch/arm/plat-stm32mp1/rproc_pub_key.h +@@ -0,0 +1,16 @@ ++/* SPDX-License-Identifier: BSD-2-Clause */ ++/* ++ * Copyright (C) 2020, STMicroelectronics - All Rights Reserved ++ */ ++ ++#ifndef RPROC_PUB_KEY_H ++#define RPROC_PUB_KEY_H ++ ++#include ++ ++extern const uint32_t rproc_pub_key_exponent; ++extern const uint8_t rproc_pub_key_modulus[]; ++extern const size_t rproc_pub_key_modulus_size; ++ ++#endif /*RPROC_PUB_KEY_H*/ ++ +diff --git a/core/arch/arm/plat-stm32mp1/scmi_server.c b/core/arch/arm/plat-stm32mp1/scmi_server.c +index 3543e8a69..4865fc40e 100644 +--- a/core/arch/arm/plat-stm32mp1/scmi_server.c ++++ b/core/arch/arm/plat-stm32mp1/scmi_server.c +@@ -1,20 +1,17 @@ + // SPDX-License-Identifier: BSD-2-Clause + /* +- * Copyright (c) 2019, STMicroelectronics ++ * Copyright (c) 2019-2021, STMicroelectronics + */ + #include + #include ++#include + #include + #include + #include + #include + #include +-#include +-#include +-#include +-#include +-#include +-#include ++#include ++#include + #include + #include + #include +@@ -48,37 +45,28 @@ struct stm32_scmi_clk { + /* + * struct stm32_scmi_rd - Data for the exposed reset controller + * @reset_id: Reset identifier in RCC reset driver ++ * @base: Physical controller address + * @name: Reset string ID exposed to channel + */ + struct stm32_scmi_rd { + unsigned long reset_id; ++ paddr_t base; + const char *name; + }; + +-enum voltd_device { +- VOLTD_PWR, +- VOLTD_PMIC, +-}; +- + /* +- * struct stm32_scmi_voltd - Data for the exposed voltage domains +- * @name: Power regulator string ID exposed to channel +- * @priv_id: Internal string ID for the regulator +- * @priv_dev: Internal ID for the device implementing the regulator ++ * struct stm32_scmi_perfd - Data for the exposed performance domains ++ * @name: Performance domain string ID (aka name) exposed to channel + */ +-struct stm32_scmi_voltd { ++struct stm32_scmi_perfd { + const char *name; +- const char *priv_id; +- enum voltd_device priv_dev; +- + }; + + /* Locate all non-secure SMT message buffers in last page of SYSRAM */ + #define SMT_BUFFER_BASE CFG_STM32MP1_SCMI_SHM_BASE + #define SMT_BUFFER0_BASE SMT_BUFFER_BASE +-#define SMT_BUFFER1_BASE (SMT_BUFFER_BASE + 0x200) + +-#if (SMT_BUFFER1_BASE + SMT_BUF_SLOT_SIZE > \ ++#if (SMT_BUFFER0_BASE + SMT_BUF_SLOT_SIZE > \ + CFG_STM32MP1_SCMI_SHM_BASE + CFG_STM32MP1_SCMI_SHM_SIZE) + #error "SCMI shared memory mismatch" + #endif +@@ -93,82 +81,82 @@ register_phys_mem(MEM_AREA_IO_NSEC, CFG_STM32MP1_SCMI_SHM_BASE, + .enabled = _init_enabled, \ + } + +-static struct stm32_scmi_clk stm32_scmi0_clock[] = { +- CLOCK_CELL(CK_SCMI0_HSE, CK_HSE, "ck_hse", true), +- CLOCK_CELL(CK_SCMI0_HSI, CK_HSI, "ck_hsi", true), +- CLOCK_CELL(CK_SCMI0_CSI, CK_CSI, "ck_csi", true), +- CLOCK_CELL(CK_SCMI0_LSE, CK_LSE, "ck_lse", true), +- CLOCK_CELL(CK_SCMI0_LSI, CK_LSI, "ck_lsi", true), +- CLOCK_CELL(CK_SCMI0_PLL2_Q, PLL2_Q, "pll2_q", true), +- CLOCK_CELL(CK_SCMI0_PLL2_R, PLL2_R, "pll2_r", true), +- CLOCK_CELL(CK_SCMI0_MPU, CK_MPU, "ck_mpu", true), +- CLOCK_CELL(CK_SCMI0_AXI, CK_AXI, "ck_axi", true), +- CLOCK_CELL(CK_SCMI0_BSEC, BSEC, "bsec", true), +- CLOCK_CELL(CK_SCMI0_CRYP1, CRYP1, "cryp1", false), +- CLOCK_CELL(CK_SCMI0_GPIOZ, GPIOZ, "gpioz", false), +- CLOCK_CELL(CK_SCMI0_HASH1, HASH1, "hash1", false), +- CLOCK_CELL(CK_SCMI0_I2C4, I2C4_K, "i2c4_k", false), +- CLOCK_CELL(CK_SCMI0_I2C6, I2C6_K, "i2c6_k", false), +- CLOCK_CELL(CK_SCMI0_IWDG1, IWDG1, "iwdg1", false), +- CLOCK_CELL(CK_SCMI0_RNG1, RNG1_K, "rng1_k", true), +- CLOCK_CELL(CK_SCMI0_RTC, RTC, "ck_rtc", true), +- CLOCK_CELL(CK_SCMI0_RTCAPB, RTCAPB, "rtcapb", true), +- CLOCK_CELL(CK_SCMI0_SPI6, SPI6_K, "spi6_k", false), +- CLOCK_CELL(CK_SCMI0_USART1, USART1_K, "usart1_k", false), +-}; +- +-static struct stm32_scmi_clk stm32_scmi1_clock[] = { +- CLOCK_CELL(CK_SCMI1_PLL3_Q, PLL3_Q, "pll3_q", true), +- CLOCK_CELL(CK_SCMI1_PLL3_R, PLL3_R, "pll3_r", true), +- CLOCK_CELL(CK_SCMI1_MCU, CK_MCU, "ck_mcu", false), +-}; +- +-#define RESET_CELL(_scmi_id, _id, _name) \ ++#define RESET_CELL(_scmi_id, _id, _base, _name) \ + [_scmi_id] = { \ + .reset_id = _id, \ ++ .base = _base, \ + .name = _name, \ + } + +-static struct stm32_scmi_rd stm32_scmi0_reset_domain[] = { +- RESET_CELL(RST_SCMI0_SPI6, SPI6_R, "spi6"), +- RESET_CELL(RST_SCMI0_I2C4, I2C4_R, "i2c4"), +- RESET_CELL(RST_SCMI0_I2C6, I2C6_R, "i2c6"), +- RESET_CELL(RST_SCMI0_USART1, USART1_R, "usart1"), +- RESET_CELL(RST_SCMI0_STGEN, STGEN_R, "stgen"), +- RESET_CELL(RST_SCMI0_GPIOZ, GPIOZ_R, "gpioz"), +- RESET_CELL(RST_SCMI0_CRYP1, CRYP1_R, "cryp1"), +- RESET_CELL(RST_SCMI0_HASH1, HASH1_R, "hash1"), +- RESET_CELL(RST_SCMI0_RNG1, RNG1_R, "rng1"), +- RESET_CELL(RST_SCMI0_MDMA, MDMA_R, "mdma"), +- RESET_CELL(RST_SCMI0_MCU, MCU_R, "mcu"), +- RESET_CELL(RST_SCMI0_MCU_HOLD_BOOT, MCU_HOLD_BOOT_R, "mcu_hold_boot"), +-}; +- +-#define VOLTD_CELL(_scmi_id, _dev_id, _priv_id, _name) \ ++#define PERFD_CELL(_scmi_id, _name) \ + [_scmi_id] = { \ +- .priv_id = (_priv_id), \ +- .priv_dev = (_dev_id), \ + .name = (_name), \ + } + +-#define PWR_REG11_NAME_ID "0" +-#define PWR_REG18_NAME_ID "1" +-#define PWR_USB33_NAME_ID "2" +- +-struct stm32_scmi_voltd scmi0_voltage_domain[] = { +- VOLTD_CELL(VOLTD_SCMI0_REG11, VOLTD_PWR, PWR_REG11_NAME_ID, "reg11"), +- VOLTD_CELL(VOLTD_SCMI0_REG18, VOLTD_PWR, PWR_REG18_NAME_ID, "reg18"), +- VOLTD_CELL(VOLTD_SCMI0_USB33, VOLTD_PWR, PWR_USB33_NAME_ID, "usb33"), +-}; +- + struct channel_resources { + struct scmi_msg_channel *channel; + struct stm32_scmi_clk *clock; + size_t clock_count; + struct stm32_scmi_rd *rd; + size_t rd_count; +- struct stm32_scmi_voltd *voltd; +- size_t voltd_count; ++ struct stm32_scmi_perfd *perfd; ++ size_t perfd_count; ++}; ++ ++#ifdef CFG_STM32MP13 ++static struct stm32_scmi_clk stm32_scmi_clock[] = { ++ CLOCK_CELL(CK_SCMI_HSE, CK_HSE, "ck_hse", true), ++ CLOCK_CELL(CK_SCMI_HSI, CK_HSI, "ck_hsi", true), ++ CLOCK_CELL(CK_SCMI_CSI, CK_CSI, "ck_csi", true), ++ CLOCK_CELL(CK_SCMI_LSE, CK_LSE, "ck_lse", true), ++ CLOCK_CELL(CK_SCMI_LSI, CK_LSI, "ck_lsi", true), ++ CLOCK_CELL(CK_SCMI_HSE_DIV2, CK_HSE_DIV2, "clk-hse-div2", true), ++ CLOCK_CELL(CK_SCMI_PLL2_Q, PLL2_Q, "pll2_q", true), ++ CLOCK_CELL(CK_SCMI_PLL2_R, PLL2_R, "pll2_r", true), ++ CLOCK_CELL(CK_SCMI_PLL3_P, PLL3_P, "pll3_p", true), ++ CLOCK_CELL(CK_SCMI_PLL3_Q, PLL3_Q, "pll3_q", true), ++ CLOCK_CELL(CK_SCMI_PLL3_R, PLL3_R, "pll3_r", true), ++ CLOCK_CELL(CK_SCMI_PLL4_P, PLL4_P, "pll4_p", true), ++ CLOCK_CELL(CK_SCMI_PLL4_Q, PLL4_Q, "pll4_q", true), ++ CLOCK_CELL(CK_SCMI_PLL4_R, PLL4_R, "pll4_r", true), ++ CLOCK_CELL(CK_SCMI_MPU, CK_MPU, "ck_mpu", true), ++ CLOCK_CELL(CK_SCMI_AXI, CK_AXI, "ck_axi", true), ++ CLOCK_CELL(CK_SCMI_MLAHB, CK_MLAHB, "ck_mlahb", true), ++ CLOCK_CELL(CK_SCMI_CKPER, CK_PER, "ck_per", true), ++ CLOCK_CELL(CK_SCMI_PCLK1, PCLK1, "pclk1", true), ++ CLOCK_CELL(CK_SCMI_PCLK2, PCLK2, "pclk2", true), ++ CLOCK_CELL(CK_SCMI_PCLK3, PCLK3, "pclk3", true), ++ CLOCK_CELL(CK_SCMI_PCLK4, PCLK4, "pclk4", true), ++ CLOCK_CELL(CK_SCMI_PCLK5, PCLK5, "pclk5", true), ++ CLOCK_CELL(CK_SCMI_PCLK6, PCLK6, "pclk6", true), ++ CLOCK_CELL(CK_SCMI_CKTIMG1, CK_TIMG1, "timg1_ck", true), ++ CLOCK_CELL(CK_SCMI_CKTIMG2, CK_TIMG2, "timg2_ck", true), ++ CLOCK_CELL(CK_SCMI_CKTIMG3, CK_TIMG3, "timg3_ck", true), ++ CLOCK_CELL(CK_SCMI_RTC, RTC, "ck_rtc", true), ++ CLOCK_CELL(CK_SCMI_RTCAPB, RTCAPB, "rtcapb", true), ++ CLOCK_CELL(CK_SCMI_BSEC, BSEC, "bsec", true), ++}; ++ ++static struct stm32_scmi_rd stm32_scmi_reset_domain[] = { ++ RESET_CELL(RST_SCMI_LTDC, LTDC_R, LTDC_BASE, "ltdc"), ++ RESET_CELL(RST_SCMI_MDMA, MDMA_R, MDMA_BASE, "mdma"), ++}; ++ ++struct stm32_scmi_perfd scmi_performance_domain[] = { ++ PERFD_CELL(0 /* PERFD_SCMI_CPU_OPP */, "cpu-opp"), ++}; ++ ++/* Currently supporting Clocks and Reset Domains */ ++static const uint8_t plat_protocol_list[] = { ++ SCMI_PROTOCOL_ID_CLOCK, ++ SCMI_PROTOCOL_ID_RESET_DOMAIN, ++#ifdef CFG_SCMI_MSG_REGULATOR_CONSUMER ++ SCMI_PROTOCOL_ID_VOLTAGE_DOMAIN, ++#endif ++#ifdef CFG_SCMI_MSG_PERF_DOMAIN ++ SCMI_PROTOCOL_ID_PERF, ++#endif ++ 0 /* Null termination */ + }; + + static const struct channel_resources scmi_channel[] = { +@@ -177,22 +165,81 @@ static const struct channel_resources scmi_channel[] = { + .shm_addr = { .pa = SMT_BUFFER0_BASE }, + .shm_size = SMT_BUF_SLOT_SIZE, + }, +- .clock = stm32_scmi0_clock, +- .clock_count = ARRAY_SIZE(stm32_scmi0_clock), +- .rd = stm32_scmi0_reset_domain, +- .rd_count = ARRAY_SIZE(stm32_scmi0_reset_domain), +- .voltd = scmi0_voltage_domain, +- .voltd_count = ARRAY_SIZE(scmi0_voltage_domain), ++ .clock = stm32_scmi_clock, ++ .clock_count = ARRAY_SIZE(stm32_scmi_clock), ++ .rd = stm32_scmi_reset_domain, ++ .rd_count = ARRAY_SIZE(stm32_scmi_reset_domain), ++ .perfd = scmi_performance_domain, ++ .perfd_count = ARRAY_SIZE(scmi_performance_domain), + }, +- [1] = { ++}; ++ ++#endif /* CFG_STM32MP13 */ ++ ++#ifdef CFG_STM32MP15 ++static struct stm32_scmi_clk stm32_scmi_clock[] = { ++ CLOCK_CELL(CK_SCMI_HSE, CK_HSE, "ck_hse", true), ++ CLOCK_CELL(CK_SCMI_HSI, CK_HSI, "ck_hsi", true), ++ CLOCK_CELL(CK_SCMI_CSI, CK_CSI, "ck_csi", true), ++ CLOCK_CELL(CK_SCMI_LSE, CK_LSE, "ck_lse", true), ++ CLOCK_CELL(CK_SCMI_LSI, CK_LSI, "ck_lsi", true), ++ CLOCK_CELL(CK_SCMI_PLL2_Q, PLL2_Q, "pll2_q", true), ++ CLOCK_CELL(CK_SCMI_PLL2_R, PLL2_R, "pll2_r", true), ++ CLOCK_CELL(CK_SCMI_MPU, CK_MPU, "ck_mpu", true), ++ CLOCK_CELL(CK_SCMI_AXI, CK_AXI, "ck_axi", true), ++ CLOCK_CELL(CK_SCMI_BSEC, BSEC, "bsec", true), ++ CLOCK_CELL(CK_SCMI_CRYP1, CRYP1, "cryp1", false), ++ CLOCK_CELL(CK_SCMI_GPIOZ, GPIOZ, "gpioz", false), ++ CLOCK_CELL(CK_SCMI_HASH1, HASH1, "hash1", false), ++ CLOCK_CELL(CK_SCMI_I2C4, I2C4_K, "i2c4_k", false), ++ CLOCK_CELL(CK_SCMI_I2C6, I2C6_K, "i2c6_k", false), ++ CLOCK_CELL(CK_SCMI_IWDG1, IWDG1, "iwdg1", false), ++ CLOCK_CELL(CK_SCMI_RNG1, RNG1_K, "rng1_k", true), ++ CLOCK_CELL(CK_SCMI_RTC, RTC, "ck_rtc", true), ++ CLOCK_CELL(CK_SCMI_RTCAPB, RTCAPB, "rtcapb", true), ++ CLOCK_CELL(CK_SCMI_SPI6, SPI6_K, "spi6_k", false), ++ CLOCK_CELL(CK_SCMI_USART1, USART1_K, "usart1_k", false), ++}; ++ ++static struct stm32_scmi_rd stm32_scmi_reset_domain[] = { ++ RESET_CELL(RST_SCMI_SPI6, SPI6_R, SPI6_BASE, "spi6"), ++ RESET_CELL(RST_SCMI_I2C4, I2C4_R, I2C4_BASE, "i2c4"), ++ RESET_CELL(RST_SCMI_I2C6, I2C6_R, I2C6_BASE, "i2c6"), ++ RESET_CELL(RST_SCMI_USART1, USART1_R, USART1_BASE, "usart1"), ++ RESET_CELL(RST_SCMI_STGEN, STGEN_R, STGEN_BASE, "stgen"), ++ RESET_CELL(RST_SCMI_GPIOZ, GPIOZ_R, GPIOZ_BASE, "gpioz"), ++ RESET_CELL(RST_SCMI_CRYP1, CRYP1_R, CRYP1_BASE, "cryp1"), ++ RESET_CELL(RST_SCMI_HASH1, HASH1_R, HASH1_BASE, "hash1"), ++ RESET_CELL(RST_SCMI_RNG1, RNG1_R, RNG1_BASE, "rng1"), ++ RESET_CELL(RST_SCMI_MDMA, MDMA_R, MDMA_BASE, "mdma"), ++ RESET_CELL(RST_SCMI_MCU, MCU_R, 0, "mcu"), ++ RESET_CELL(RST_SCMI_MCU_HOLD_BOOT, MCU_HOLD_BOOT_R, 0, ++ "mcu_hold_boot"), ++}; ++ ++/* Currently supporting Clocks and Reset Domains */ ++static const uint8_t plat_protocol_list[] = { ++ SCMI_PROTOCOL_ID_CLOCK, ++ SCMI_PROTOCOL_ID_RESET_DOMAIN, ++#ifdef CFG_SCMI_MSG_REGULATOR_CONSUMER ++ SCMI_PROTOCOL_ID_VOLTAGE_DOMAIN, ++#endif ++ 0 /* Null termination */ ++}; ++ ++static const struct channel_resources scmi_channel[] = { ++ [0] = { + .channel = &(struct scmi_msg_channel){ +- .shm_addr = { .pa = SMT_BUFFER1_BASE }, ++ .shm_addr = { .pa = SMT_BUFFER0_BASE }, + .shm_size = SMT_BUF_SLOT_SIZE, + }, +- .clock = stm32_scmi1_clock, +- .clock_count = ARRAY_SIZE(stm32_scmi1_clock), ++ .clock = stm32_scmi_clock, ++ .clock_count = ARRAY_SIZE(stm32_scmi_clock), ++ .rd = stm32_scmi_reset_domain, ++ .rd_count = ARRAY_SIZE(stm32_scmi_reset_domain), + }, + }; ++#endif /* CFG_STM32MP15 */ + + static const struct channel_resources *find_resource(unsigned int channel_id) + { +@@ -231,7 +278,14 @@ static size_t __maybe_unused plat_scmi_protocol_count_paranoid(void) + count++; + + for (n = 0; n < channel_count; n++) +- if (scmi_channel[n].voltd_count) ++ if (IS_ENABLED(CFG_SCMI_MSG_REGULATOR_CONSUMER) && ++ plat_scmi_voltd_count(n)) ++ break; ++ if (n < channel_count) ++ count++; ++ ++ for (n = 0; n < channel_count; n++) ++ if (scmi_channel[n].perfd_count) + break; + if (n < channel_count) + count++; +@@ -252,14 +306,6 @@ const char *plat_scmi_sub_vendor_name(void) + return sub_vendor; + } + +-/* Currently supporting Clocks and Reset Domains */ +-static const uint8_t plat_protocol_list[] = { +- SCMI_PROTOCOL_ID_CLOCK, +- SCMI_PROTOCOL_ID_RESET_DOMAIN, +- SCMI_PROTOCOL_ID_VOLTAGE_DOMAIN, +- 0 /* Null termination */ +-}; +- + size_t plat_scmi_protocol_count(void) + { + const size_t count = ARRAY_SIZE(plat_protocol_list) - 1; +@@ -284,13 +330,9 @@ static struct stm32_scmi_clk *find_clock(unsigned int channel_id, + unsigned int scmi_id) + { + const struct channel_resources *resource = find_resource(channel_id); +- size_t n = 0; + +- if (resource) { +- for (n = 0; n < resource->clock_count; n++) +- if (n == scmi_id) +- return &resource->clock[n]; +- } ++ if (resource && scmi_id < resource->clock_count) ++ return &resource->clock[scmi_id]; + + return NULL; + } +@@ -316,9 +358,22 @@ const char *plat_scmi_clock_get_name(unsigned int channel_id, + return clock->name; + } + +-int32_t plat_scmi_clock_rates_array(unsigned int channel_id, +- unsigned int scmi_id, size_t start_index, +- unsigned long *array, size_t *nb_elts) ++int32_t plat_scmi_clock_rates_array(unsigned int channel_id __unused, ++ unsigned int scmi_id __unused, ++ size_t start_index __unused, ++ unsigned long *array __unused, ++ size_t *nb_elts __unused) ++{ ++ /* ++ * Explicitly do not expose clock rates by array since not ++ * fully supported by Linux kernel as of v5.4.24. ++ */ ++ return SCMI_NOT_SUPPORTED; ++} ++ ++int32_t plat_scmi_clock_rates_by_step(unsigned int channel_id, ++ unsigned int scmi_id, ++ unsigned long *array) + { + struct stm32_scmi_clk *clock = find_clock(channel_id, scmi_id); + +@@ -328,16 +383,62 @@ int32_t plat_scmi_clock_rates_array(unsigned int channel_id, + if (!stm32mp_nsec_can_access_clock(clock->clock_id)) + return SCMI_DENIED; + +- /* Exposed clocks are currently fixed rate clocks */ +- if (start_index) +- return SCMI_INVALID_PARAMETERS; ++ switch (scmi_id) { ++ case CK_SCMI_MPU: ++ /* ++ * Pretend we support all rates for MPU clock, ++ * CLOCK_RATE_SET will reject unsupported rates. ++ */ ++ array[0] = 0U; ++ array[1] = UINT32_MAX; ++ array[2] = 1U; ++ break; ++#ifdef CFG_STM32MP13 ++ case CK_SCMI_PLL4_Q: ++ array[0] = 0U; ++ array[1] = UINT32_MAX; ++ array[2] = 0U; ++ break; ++#endif ++ default: ++ array[0] = clk_get_rate(clock->clk); ++ array[1] = array[0]; ++ array[2] = 0U; ++ break; ++ } ++ ++ return SCMI_SUCCESS; ++} + +- if (!array) +- *nb_elts = 1; +- else if (*nb_elts == 1) +- *array = clk_get_rate(clock->clk); +- else +- return SCMI_GENERIC_ERROR; ++int32_t plat_scmi_clock_set_rate(unsigned int channel_id, unsigned int scmi_id, ++ unsigned long rate) ++{ ++ struct stm32_scmi_clk *clock = find_clock(channel_id, scmi_id); ++ ++ if (!clock) ++ return SCMI_NOT_FOUND; ++ ++ if (!stm32mp_nsec_can_access_clock(clock->clock_id)) ++ return SCMI_DENIED; ++ ++ switch (scmi_id) { ++#ifdef CFG_STM32MP15 ++ case CK_SCMI_MPU: ++ if (stm32mp1_set_opp_khz(rate / 1000)) ++ return SCMI_INVALID_PARAMETERS; ++ break; ++#endif ++#ifdef CFG_STM32MP13 ++ case CK_SCMI_PLL4_Q: ++ if (clk_set_rate(clock->clk, rate)) ++ return SCMI_INVALID_PARAMETERS; ++ break; ++#endif ++ default: ++ if (rate != clk_get_rate(clock->clk)) ++ return SCMI_INVALID_PARAMETERS; ++ break; ++ } + + return SCMI_SUCCESS; + } +@@ -353,6 +454,17 @@ unsigned long plat_scmi_clock_get_rate(unsigned int channel_id, + return clk_get_rate(clock->clk); + } + ++unsigned long plat_scmi_clock_round_rate(unsigned int channel_id, ++ unsigned int scmi_id, unsigned long rate) ++{ ++ struct stm32_scmi_clk *clock = find_clock(channel_id, scmi_id); ++ ++ if (!clock || !stm32mp_nsec_can_access_clock(clock->clock_id)) ++ return 0; ++ ++ return clk_round_rate(stm32mp_rcc_clock_id_to_clk(clock->clock_id), rate); ++} ++ + int32_t plat_scmi_clock_get_state(unsigned int channel_id, unsigned int scmi_id) + { + struct stm32_scmi_clk *clock = find_clock(channel_id, scmi_id); +@@ -376,13 +488,13 @@ int32_t plat_scmi_clock_set_state(unsigned int channel_id, unsigned int scmi_id, + + if (enable_not_disable) { + if (!clock->enabled) { +- DMSG("SCMI clock %u enable", scmi_id); ++ FMSG("SCMI clock %u enable", scmi_id); + clk_enable(clock->clk); + clock->enabled = true; + } + } else { + if (clock->enabled) { +- DMSG("SCMI clock %u disable", scmi_id); ++ FMSG("SCMI clock %u disable", scmi_id); + clk_disable(clock->clk); + clock->enabled = false; + } +@@ -391,6 +503,25 @@ int32_t plat_scmi_clock_set_state(unsigned int channel_id, unsigned int scmi_id, + return SCMI_SUCCESS; + } + ++int32_t plat_scmi_clock_get_duty_cycle(unsigned int channel_id, unsigned int scmi_id, ++ struct clk_duty *duty) ++{ ++ struct stm32_scmi_clk *clock = find_clock(channel_id, scmi_id); ++ struct clk *clk_ref = NULL; ++ int ret = 0; ++ ++ if (!clock || !stm32mp_nsec_can_access_clock(clock->clock_id)) ++ return SCMI_DENIED; ++ ++ clk_ref = stm32mp_rcc_clock_id_to_clk(clock->clock_id); ++ ++ ret = clk_get_duty_cyle(clk_ref, duty); ++ if (ret) ++ return SCMI_NOT_SUPPORTED; ++ ++ return SCMI_SUCCESS; ++} ++ + /* + * Platform SCMI reset domains + */ +@@ -398,13 +529,9 @@ static struct stm32_scmi_rd *find_rd(unsigned int channel_id, + unsigned int scmi_id) + { + const struct channel_resources *resource = find_resource(channel_id); +- size_t n = 0; + +- if (resource) { +- for (n = 0; n < resource->rd_count; n++) +- if (n == scmi_id) +- return &resource->rd[n]; +- } ++ if (resource && scmi_id < resource->rd_count) ++ return &resource->rd[scmi_id]; + + return NULL; + } +@@ -433,21 +560,27 @@ int32_t plat_scmi_rd_autonomous(unsigned int channel_id, unsigned int scmi_id, + uint32_t state) + { + const struct stm32_scmi_rd *rd = find_rd(channel_id, scmi_id); ++ const struct stm32_firewall_cfg nsec_cfg[] = { ++ { FWLL_NSEC_RW | FWLL_MASTER(0) }, ++ { }, /* Null terminated */ ++ }; + + if (!rd) + return SCMI_NOT_FOUND; + +- if (!stm32mp_nsec_can_access_reset(rd->reset_id)) ++ if (rd->base && stm32_firewall_check_access(rd->base, 0, nsec_cfg)) + return SCMI_DENIED; + ++#ifdef CFG_STM32MP15 + if (rd->reset_id == MCU_HOLD_BOOT_R) + return SCMI_NOT_SUPPORTED; ++#endif + + /* Supports only reset with context loss */ + if (state) + return SCMI_NOT_SUPPORTED; + +- DMSG("SCMI reset %u cycle", scmi_id); ++ FMSG("SCMI reset %u cycle", scmi_id); + + if (stm32_reset_assert(rd->reset_id, TIMEOUT_US_1MS)) + return SCMI_HARDWARE_ERROR; +@@ -462,155 +595,97 @@ int32_t plat_scmi_rd_set_state(unsigned int channel_id, unsigned int scmi_id, + bool assert_not_deassert) + { + const struct stm32_scmi_rd *rd = find_rd(channel_id, scmi_id); ++ const struct stm32_firewall_cfg nsec_cfg[] = { ++ { FWLL_NSEC_RW | FWLL_MASTER(0) }, ++ { }, /* Null terminated */ ++ }; + + if (!rd) + return SCMI_NOT_FOUND; + +- if (!stm32mp_nsec_can_access_reset(rd->reset_id)) ++ if (rd->base && stm32_firewall_check_access(rd->base, 0, nsec_cfg)) + return SCMI_DENIED; + ++#ifdef CFG_STM32MP15 + if (rd->reset_id == MCU_HOLD_BOOT_R) { +- DMSG("SCMI MCU hold boot %s", ++ FMSG("SCMI MCU hold boot %s", + assert_not_deassert ? "set" : "release"); + stm32_reset_assert_deassert_mcu(assert_not_deassert); + return SCMI_SUCCESS; + } ++#endif + + if (assert_not_deassert) { +- DMSG("SCMI reset %u set", scmi_id); ++ FMSG("SCMI reset %u set", scmi_id); + stm32_reset_set(rd->reset_id); + } else { +- DMSG("SCMI reset %u release", scmi_id); ++ FMSG("SCMI reset %u release", scmi_id); + stm32_reset_release(rd->reset_id); + } + + return SCMI_SUCCESS; + } + ++#ifdef CFG_SCMI_MSG_PERF_DOMAIN + /* +- * Platform SCMI voltage domains ++ * Platform SCMI performance domains ++ * ++ * Note: currently exposes a single performance domain that is the CPU DVFS ++ * operating point. Non-secure is allowed to access this performance domain. + */ +-static struct stm32_scmi_voltd *find_voltd(unsigned int channel_id, +- unsigned int scmi_id) ++static struct stm32_scmi_perfd *find_perfd(unsigned int channel_id, ++ unsigned int domain_id) + { + const struct channel_resources *resource = find_resource(channel_id); + size_t n = 0; + + if (resource) { +- for (n = 0; n < resource->voltd_count; n++) +- if (n == scmi_id) +- return &resource->voltd[n]; ++ for (n = 0; n < resource->perfd_count; n++) ++ if (n == domain_id) ++ return &resource->perfd[n]; + } + + return NULL; + } + +-size_t plat_scmi_voltd_count(unsigned int channel_id) ++size_t plat_scmi_perf_count(unsigned int channel_id) + { + const struct channel_resources *resource = find_resource(channel_id); + + if (!resource) + return 0; + +- return resource->voltd_count; ++ return resource->perfd_count; + } + +-const char *plat_scmi_voltd_get_name(unsigned int channel_id, +- unsigned int scmi_id) ++const char *plat_scmi_perf_domain_name(unsigned int channel_id, ++ unsigned int domain_id) + { +- struct stm32_scmi_voltd *voltd = find_voltd(channel_id, scmi_id); ++ struct stm32_scmi_perfd *perfd = find_perfd(channel_id, domain_id); + +- /* Currently non-secure is allowed to access all PWR regulators */ +- if (!voltd) ++ if (!perfd) + return NULL; + +- return voltd->name; +-} +- +-static enum pwr_regulator pwr_scmi_to_regu_id(struct stm32_scmi_voltd *voltd) +-{ +- if (!strcmp(voltd->priv_id, PWR_REG11_NAME_ID)) +- return PWR_REG11; +- if (!strcmp(voltd->priv_id, PWR_REG18_NAME_ID)) +- return PWR_REG18; +- if (!strcmp(voltd->priv_id, PWR_USB33_NAME_ID)) +- return PWR_USB33; +- +- panic(); +-} +- +-static long pwr_get_level(struct stm32_scmi_voltd *voltd) +-{ +- enum pwr_regulator regu_id = pwr_scmi_to_regu_id(voltd); +- +- return (long)stm32mp1_pwr_regulator_mv(regu_id) * 1000; +-} +- +-static int32_t pwr_set_level(struct stm32_scmi_voltd *voltd, long level_uv) +-{ +- if (level_uv != pwr_get_level(voltd)) +- return SCMI_INVALID_PARAMETERS; +- +- return SCMI_SUCCESS; +-} +- +-static int32_t pwr_describe_levels(struct stm32_scmi_voltd *voltd, +- size_t start_index, long *microvolt, +- size_t *nb_elts) +-{ +- if (start_index) +- return SCMI_INVALID_PARAMETERS; +- +- if (!microvolt) { +- *nb_elts = 1; +- return SCMI_SUCCESS; +- } +- +- if (*nb_elts < 1) +- return SCMI_GENERIC_ERROR; +- +- *nb_elts = 1; +- *microvolt = pwr_get_level(voltd); +- +- return SCMI_SUCCESS; +-} +- +-static uint32_t pwr_get_state(struct stm32_scmi_voltd *voltd) +-{ +- enum pwr_regulator regu_id = pwr_scmi_to_regu_id(voltd); +- +- if (stm32mp1_pwr_regulator_is_enabled(regu_id)) +- return SCMI_VOLTAGE_DOMAIN_CONFIG_ARCH_ON; +- +- return SCMI_VOLTAGE_DOMAIN_CONFIG_ARCH_OFF; +-} +- +-static void pwr_set_state(struct stm32_scmi_voltd *voltd, bool enable) +-{ +- enum pwr_regulator regu_id = pwr_scmi_to_regu_id(voltd); +- +- DMSG("%sable PWR %s (was %s)", enable ? "En" : "Dis", voltd->name, +- stm32mp1_pwr_regulator_is_enabled(regu_id) ? "on" : "off"); +- +- stm32mp1_pwr_regulator_set_state(regu_id, enable); ++ return perfd->name; + } + +-static int32_t pmic_describe_levels(struct stm32_scmi_voltd *voltd, +- size_t start_index, long *microvolt, +- size_t *nb_elts) ++int32_t plat_scmi_perf_levels_array(unsigned int channel_id, ++ unsigned int domain_id, size_t start_index, ++ unsigned int *levels, size_t *nb_elts) + { +- const uint16_t *levels = NULL; ++ struct stm32_scmi_perfd *perfd = find_perfd(channel_id, domain_id); + size_t full_count = 0; + size_t out_count = 0; +- size_t i = 0; ++ size_t n = 0; + +- if (!stm32mp_nsec_can_access_pmic_regu(voltd->priv_id)) +- return SCMI_DENIED; ++ if (!perfd) ++ return SCMI_NOT_FOUND; + +- stpmic1_regulator_levels_mv(voltd->priv_id, &levels, &full_count); ++ full_count = stm32mp1_cpu_opp_count(); + +- if (!microvolt) { ++ if (!levels) { + *nb_elts = full_count - start_index; ++ + return SCMI_SUCCESS; + } + +@@ -622,189 +697,67 @@ static int32_t pmic_describe_levels(struct stm32_scmi_voltd *voltd, + FMSG("%zu levels: start %zu requested %zu output %zu", + full_count, start_index, *nb_elts, out_count); + +- for (i = 0; i < out_count; i++) +- microvolt[i] = levels[start_index + i] * 1000; ++ for (n = 0; n < out_count; n++) ++ levels[n] = stm32mp1_cpu_opp_level(n); + + *nb_elts = out_count; + + return SCMI_SUCCESS; + } + +-static long pmic_get_level(struct stm32_scmi_voltd *voltd) +-{ +- unsigned long level_mv = 0; +- +- if (!stm32mp_nsec_can_access_pmic_regu(voltd->priv_id)) +- return 0; +- +- stm32mp_get_pmic(); +- level_mv = stpmic1_regulator_voltage_get(voltd->priv_id); +- stm32mp_put_pmic(); +- +- return (long)level_mv * 1000; +-} +- +-static int32_t pmic_set_level(struct stm32_scmi_voltd *voltd, long level_uv) +-{ +- int rc = 0; +- unsigned int level_mv = 0; +- +- if (!stm32mp_nsec_can_access_pmic_regu(voltd->priv_id)) +- return SCMI_DENIED; +- +- if (level_uv < 0 || level_uv > (UINT16_MAX * 1000)) +- return SCMI_INVALID_PARAMETERS; +- +- level_mv = (unsigned int)level_uv / 1000; +- +- DMSG("Set STPMIC1 regulator %s level to %dmV", voltd->name, level_mv); +- +- stm32mp_get_pmic(); +- rc = stpmic1_regulator_voltage_set(voltd->priv_id, level_mv); +- stm32mp_put_pmic(); +- +- return rc ? SCMI_GENERIC_ERROR : SCMI_SUCCESS; +-} +- +-static uint32_t pmic_get_state(struct stm32_scmi_voltd *voltd) +-{ +- bool enabled = false; +- +- if (!stm32mp_nsec_can_access_pmic_regu(voltd->priv_id)) +- return SCMI_VOLTAGE_DOMAIN_CONFIG_ARCH_OFF; +- +- stm32mp_get_pmic(); +- enabled = stpmic1_is_regulator_enabled(voltd->priv_id); +- stm32mp_put_pmic(); +- +- if (enabled) +- return SCMI_VOLTAGE_DOMAIN_CONFIG_ARCH_ON; +- +- return SCMI_VOLTAGE_DOMAIN_CONFIG_ARCH_OFF; +-} +- +-static int32_t pmic_set_state(struct stm32_scmi_voltd *voltd, bool enable) +-{ +- int rc = 0; +- +- if (!stm32mp_nsec_can_access_pmic_regu(voltd->priv_id)) +- return SCMI_DENIED; +- +- stm32mp_get_pmic(); +- +- DMSG("%sable STPMIC1 %s (was %s)", enable ? "En" : "Dis", voltd->name, +- stpmic1_is_regulator_enabled(voltd->priv_id) ? "on" : "off"); +- +- if (enable) +- rc = stpmic1_regulator_enable(voltd->priv_id); +- else +- rc = stpmic1_regulator_disable(voltd->priv_id); +- +- stm32mp_put_pmic(); +- +- return rc ? SCMI_GENERIC_ERROR : SCMI_SUCCESS; +-} +- +-int32_t plat_scmi_voltd_levels_array(unsigned int channel_id, +- unsigned int scmi_id, size_t start_index, +- long *levels, size_t *nb_elts) +- ++int32_t plat_scmi_perf_level_get(unsigned int channel_id, ++ unsigned int domain_id, unsigned int *level) + { +- struct stm32_scmi_voltd *voltd = find_voltd(channel_id, scmi_id); ++ struct stm32_scmi_perfd *perfd = find_perfd(channel_id, domain_id); ++ unsigned int current_level = 0; + +- if (!voltd) ++ if (!perfd) + return SCMI_NOT_FOUND; + +- switch (voltd->priv_dev) { +- case VOLTD_PWR: +- return pwr_describe_levels(voltd, start_index, levels, nb_elts); +- case VOLTD_PMIC: +- return pmic_describe_levels(voltd, start_index, levels, +- nb_elts); +- default: ++ if (stm32mp1_cpu_opp_read_level(¤t_level)) + return SCMI_GENERIC_ERROR; +- } +-} + +-long plat_scmi_voltd_get_level(unsigned int channel_id, unsigned int scmi_id) +-{ +- struct stm32_scmi_voltd *voltd = find_voltd(channel_id, scmi_id); +- +- if (!voltd) +- return 0; ++ *level = current_level; + +- switch (voltd->priv_dev) { +- case VOLTD_PWR: +- return pwr_get_level(voltd); +- case VOLTD_PMIC: +- return pmic_get_level(voltd); +- default: +- panic(); +- } ++ return SCMI_SUCCESS; + } + +-int32_t plat_scmi_voltd_set_level(unsigned int channel_id, unsigned int scmi_id, +- long level) ++int32_t plat_scmi_perf_level_set(unsigned int channel_id, ++ unsigned int domain_id, unsigned int level) + { +- struct stm32_scmi_voltd *voltd = find_voltd(channel_id, scmi_id); ++ struct stm32_scmi_perfd *perfd = find_perfd(channel_id, domain_id); + +- if (!voltd) ++ if (!perfd) + return SCMI_NOT_FOUND; + +- switch (voltd->priv_dev) { +- case VOLTD_PWR: +- return pwr_set_level(voltd, level); +- case VOLTD_PMIC: +- return pmic_set_level(voltd, level); ++ switch (stm32mp1_cpu_opp_set_level(level)) { ++ case TEE_SUCCESS: ++ return SCMI_SUCCESS; ++ case TEE_ERROR_BAD_PARAMETERS: ++ return SCMI_OUT_OF_RANGE; + default: + return SCMI_GENERIC_ERROR; + } + } ++#endif + +-int32_t plat_scmi_voltd_get_config(unsigned int channel_id, +- unsigned int scmi_id, uint32_t *config) ++static TEE_Result stm32_scmi_pm(enum pm_op op, unsigned int pm_hint __unused, ++ const struct pm_callback_handle *hdl __unused) + { +- struct stm32_scmi_voltd *voltd = find_voltd(channel_id, scmi_id); +- +- if (!voltd) +- return SCMI_NOT_FOUND; +- +- switch (voltd->priv_dev) { +- case VOLTD_PWR: +- *config = pwr_get_state(voltd); +- break; +- case VOLTD_PMIC: +- *config = pmic_get_state(voltd); +- break; +- default: +- return SCMI_GENERIC_ERROR; +- } +- +- return SCMI_SUCCESS; +-} ++ size_t i = 0; + +-int32_t plat_scmi_voltd_set_config(unsigned int channel_id, +- unsigned int scmi_id, uint32_t config) +-{ +- struct stm32_scmi_voltd *voltd = find_voltd(channel_id, scmi_id); +- int32_t rc = SCMI_SUCCESS; ++ if (op == PM_OP_RESUME) ++ for (i = 0; i < ARRAY_SIZE(scmi_channel); i++) { ++ struct scmi_msg_channel *chan = plat_scmi_get_channel(i); + +- if (!voltd) +- return SCMI_NOT_FOUND; ++ assert(chan && chan->shm_addr.va); + +- switch (voltd->priv_dev) { +- case VOLTD_PWR: +- pwr_set_state(voltd, config); +- break; +- case VOLTD_PMIC: +- rc = pmic_set_state(voltd, config); +- break; +- default: +- return SCMI_GENERIC_ERROR; +- } ++ scmi_smt_init_agent_channel(chan); ++ } + +- return rc; ++ return TEE_SUCCESS; + } ++DECLARE_KEEP_PAGER(stm32_scmi_pm); + + /* + * Initialize platform SCMI resources +@@ -814,9 +767,12 @@ static TEE_Result stm32mp1_init_scmi_server(void) + size_t i = 0; + size_t j = 0; + ++ register_pm_driver_cb(stm32_scmi_pm, NULL, "scmi-server-driver"); ++ + for (i = 0; i < ARRAY_SIZE(scmi_channel); i++) { + const struct channel_resources *res = scmi_channel + i; + struct scmi_msg_channel *chan = res->channel; ++ size_t voltd_count = 0; + + /* Enforce non-secure shm mapped as device memory */ + chan->shm_addr.va = (vaddr_t)phys_to_virt(chan->shm_addr.pa, +@@ -849,11 +805,13 @@ static TEE_Result stm32mp1_init_scmi_server(void) + panic("SCMI reset domain name invalid"); + } + +- for (j = 0; j < res->voltd_count; j++) { +- struct stm32_scmi_voltd *voltd = &res->voltd[j]; ++ if (IS_ENABLED(CFG_SCMI_MSG_REGULATOR_CONSUMER)) ++ voltd_count = plat_scmi_voltd_count(i); ++ ++ for (j = 0; j < voltd_count; j++) { ++ const char *name = plat_scmi_voltd_get_name(i, j); + +- if (!voltd->name || +- strlen(voltd->name) >= SCMI_VOLTD_NAME_SIZE) ++ if (!name || strlen(name) >= SCMI_VOLTD_NAME_SIZE) + panic("SCMI voltage domain name invalid"); + } + } +diff --git a/core/arch/arm/plat-stm32mp1/shared_resources.c b/core/arch/arm/plat-stm32mp1/shared_resources.c +index e282c3f68..ce0b1c04f 100644 +--- a/core/arch/arm/plat-stm32mp1/shared_resources.c ++++ b/core/arch/arm/plat-stm32mp1/shared_resources.c +@@ -1,15 +1,17 @@ + // SPDX-License-Identifier: BSD-3-Clause + /* +- * Copyright (c) 2017-2021, STMicroelectronics ++ * Copyright (c) 2017-2022, STMicroelectronics + */ + + #include + #include + #include +-#include ++#include ++#ifdef CFG_STM32MP13 ++#include ++#else /* assume CFG_STM32MP15 */ + #include +-#include +-#include ++#endif + #include + #include + #include +@@ -28,7 +30,7 @@ + * Once one starts to get the resource registering state, one cannot register + * new resources. This ensures resource state cannot change. + */ +-static bool registering_locked; ++static bool registering_locked __unused; + + /* + * Shared register access: upon shared resource lock +@@ -85,7 +87,8 @@ enum shres_state { + }; + + /* Use a byte array to store each resource state */ +-static uint8_t shres_state[STM32MP1_SHRES_COUNT] = { ++static uint8_t shres_state[STM32MP1_SHRES_COUNT] __maybe_unused = { ++#ifdef CFG_STM32MP15 + #if !defined(CFG_STM32_IWDG) + [STM32MP1_SHRES_IWDG1] = SHRES_NON_SECURE, + #endif +@@ -99,16 +102,6 @@ static uint8_t shres_state[STM32MP1_SHRES_COUNT] = { + [STM32MP1_SHRES_I2C4] = SHRES_NON_SECURE, + [STM32MP1_SHRES_I2C6] = SHRES_NON_SECURE, + #endif +-#if !defined(CFG_STM32_GPIO) +- [STM32MP1_SHRES_GPIOZ(0)] = SHRES_NON_SECURE, +- [STM32MP1_SHRES_GPIOZ(1)] = SHRES_NON_SECURE, +- [STM32MP1_SHRES_GPIOZ(2)] = SHRES_NON_SECURE, +- [STM32MP1_SHRES_GPIOZ(3)] = SHRES_NON_SECURE, +- [STM32MP1_SHRES_GPIOZ(4)] = SHRES_NON_SECURE, +- [STM32MP1_SHRES_GPIOZ(5)] = SHRES_NON_SECURE, +- [STM32MP1_SHRES_GPIOZ(6)] = SHRES_NON_SECURE, +- [STM32MP1_SHRES_GPIOZ(7)] = SHRES_NON_SECURE, +-#endif + #if !defined(CFG_STM32_RNG) + [STM32MP1_SHRES_RNG1] = SHRES_NON_SECURE, + #endif +@@ -121,17 +114,11 @@ static uint8_t shres_state[STM32MP1_SHRES_COUNT] = { + #if !defined(CFG_STM32_RTC) + [STM32MP1_SHRES_RTC] = SHRES_NON_SECURE, + #endif ++#endif + }; + + static const char __maybe_unused *shres2str_id_tbl[STM32MP1_SHRES_COUNT] = { +- [STM32MP1_SHRES_GPIOZ(0)] = "GPIOZ0", +- [STM32MP1_SHRES_GPIOZ(1)] = "GPIOZ1", +- [STM32MP1_SHRES_GPIOZ(2)] = "GPIOZ2", +- [STM32MP1_SHRES_GPIOZ(3)] = "GPIOZ3", +- [STM32MP1_SHRES_GPIOZ(4)] = "GPIOZ4", +- [STM32MP1_SHRES_GPIOZ(5)] = "GPIOZ5", +- [STM32MP1_SHRES_GPIOZ(6)] = "GPIOZ6", +- [STM32MP1_SHRES_GPIOZ(7)] = "GPIOZ7", ++#ifdef CFG_STM32MP15 + [STM32MP1_SHRES_IWDG1] = "IWDG1", + [STM32MP1_SHRES_USART1] = "USART1", + [STM32MP1_SHRES_SPI6] = "SPI6", +@@ -143,6 +130,7 @@ static const char __maybe_unused *shres2str_id_tbl[STM32MP1_SHRES_COUNT] = { + [STM32MP1_SHRES_RTC] = "RTC", + [STM32MP1_SHRES_MCU] = "MCU", + [STM32MP1_SHRES_PLL3] = "PLL3", ++#endif + [STM32MP1_SHRES_MDMA] = "MDMA", + }; + +@@ -162,41 +150,7 @@ static __maybe_unused const char *shres2str_state(enum shres_state id) + return shres2str_state_tbl[id]; + } + +-/* GPIOZ bank pin count depends on SoC variants */ +-#ifdef CFG_EMBED_DTB +-/* A light count routine for unpaged context to not depend on DTB support */ +-static int gpioz_nbpin = -1; +- +-static unsigned int get_gpioz_nbpin(void) +-{ +- if (gpioz_nbpin < 0) +- panic(); +- +- return gpioz_nbpin; +-} +- +-static TEE_Result set_gpioz_nbpin_from_dt(void) +-{ +- void *fdt = get_embedded_dt(); +- int node = fdt_path_offset(fdt, "/soc/pin-controller-z"); +- int count = stm32_get_gpio_count(fdt, node, GPIO_BANK_Z); +- +- if (count < 0 || count > STM32MP1_GPIOZ_PIN_MAX_COUNT) +- panic(); +- +- gpioz_nbpin = count; +- +- return TEE_SUCCESS; +-} +-/* Get GPIOZ pin count before drivers initialization, hence service_init() */ +-service_init(set_gpioz_nbpin_from_dt); +-#else +-static unsigned int get_gpioz_nbpin(void) +-{ +- return STM32MP1_GPIOZ_PIN_MAX_COUNT; +-} +-#endif +- ++#ifdef CFG_STM32MP15 + static void register_periph(enum stm32mp_shres id, enum shres_state state) + { + assert(id < STM32MP1_SHRES_COUNT && +@@ -218,40 +172,11 @@ static void register_periph(enum stm32mp_shres id, enum shres_state state) + DMSG("Register %s as %s", + shres2str_id(id), shres2str_state(state)); + +- switch (id) { +- case STM32MP1_SHRES_GPIOZ(0): +- case STM32MP1_SHRES_GPIOZ(1): +- case STM32MP1_SHRES_GPIOZ(2): +- case STM32MP1_SHRES_GPIOZ(3): +- case STM32MP1_SHRES_GPIOZ(4): +- case STM32MP1_SHRES_GPIOZ(5): +- case STM32MP1_SHRES_GPIOZ(6): +- case STM32MP1_SHRES_GPIOZ(7): +- if ((id - STM32MP1_SHRES_GPIOZ(0)) >= get_gpioz_nbpin()) { +- EMSG("Invalid GPIO %u >= %u", +- id - STM32MP1_SHRES_GPIOZ(0), get_gpioz_nbpin()); +- panic(); +- } +- break; +- default: +- break; +- } +- + shres_state[id] = state; + + /* Explore clock tree to lock secure clock dependencies */ + if (state == SHRES_SECURE) { + switch (id) { +- case STM32MP1_SHRES_GPIOZ(0): +- case STM32MP1_SHRES_GPIOZ(1): +- case STM32MP1_SHRES_GPIOZ(2): +- case STM32MP1_SHRES_GPIOZ(3): +- case STM32MP1_SHRES_GPIOZ(4): +- case STM32MP1_SHRES_GPIOZ(5): +- case STM32MP1_SHRES_GPIOZ(6): +- case STM32MP1_SHRES_GPIOZ(7): +- stm32mp_register_clock_parents_secure(GPIOZ); +- break; + case STM32MP1_SHRES_IWDG1: + stm32mp_register_clock_parents_secure(IWDG1); + break; +@@ -331,21 +256,6 @@ static void register_periph_iomem(vaddr_t base, enum shres_state state) + id = STM32MP1_SHRES_HASH1; + break; + +- /* Always non-secure resource cases */ +-#ifdef CFG_WITH_NSEC_GPIOS +- case GPIOA_BASE: +- case GPIOB_BASE: +- case GPIOC_BASE: +- case GPIOD_BASE: +- case GPIOE_BASE: +- case GPIOF_BASE: +- case GPIOG_BASE: +- case GPIOH_BASE: +- case GPIOI_BASE: +- case GPIOJ_BASE: +- case GPIOK_BASE: +- /* Fall through */ +-#endif + #ifdef CFG_WITH_NSEC_UARTS + case USART2_BASE: + case USART3_BASE: +@@ -382,32 +292,6 @@ void stm32mp_register_non_secure_periph_iomem(vaddr_t base) + register_periph_iomem(base, SHRES_NON_SECURE); + } + +-/* Register GPIO resource */ +-void stm32mp_register_secure_gpio(unsigned int bank, unsigned int pin) +-{ +- switch (bank) { +- case GPIO_BANK_Z: +- assert(pin < get_gpioz_nbpin()); +- register_periph(STM32MP1_SHRES_GPIOZ(pin), SHRES_SECURE); +- break; +- default: +- EMSG("GPIO bank %u cannot be secured", bank); +- panic(); +- } +-} +- +-void stm32mp_register_non_secure_gpio(unsigned int bank, unsigned int pin) +-{ +- switch (bank) { +- case GPIO_BANK_Z: +- assert(pin < get_gpioz_nbpin()); +- register_periph(STM32MP1_SHRES_GPIOZ(pin), SHRES_NON_SECURE); +- break; +- default: +- break; +- } +-} +- + static void lock_registering(void) + { + registering_locked = true; +@@ -419,57 +303,20 @@ bool stm32mp_periph_is_secure(enum stm32mp_shres id) + + return shres_state[id] == SHRES_SECURE; + } +- +-bool stm32mp_gpio_bank_is_shared(unsigned int bank) ++#else ++void stm32mp_register_secure_periph_iomem(vaddr_t base __maybe_unused) + { +- unsigned int not_secure = 0; +- unsigned int pin = 0; +- +- lock_registering(); +- +- if (bank != GPIO_BANK_Z) +- return false; +- +- for (pin = 0; pin < get_gpioz_nbpin(); pin++) +- if (!stm32mp_periph_is_secure(STM32MP1_SHRES_GPIOZ(pin))) +- not_secure++; +- +- return not_secure > 0 && not_secure < get_gpioz_nbpin(); + } + +-bool stm32mp_gpio_bank_is_non_secure(unsigned int bank) ++void stm32mp_register_non_secure_periph_iomem(vaddr_t base __maybe_unused) + { +- unsigned int not_secure = 0; +- unsigned int pin = 0; +- +- lock_registering(); +- +- if (bank != GPIO_BANK_Z) +- return true; +- +- for (pin = 0; pin < get_gpioz_nbpin(); pin++) +- if (!stm32mp_periph_is_secure(STM32MP1_SHRES_GPIOZ(pin))) +- not_secure++; +- +- return not_secure > 0 && not_secure == get_gpioz_nbpin(); + } + +-bool stm32mp_gpio_bank_is_secure(unsigned int bank) ++bool stm32mp_periph_is_secure(enum stm32mp_shres id __maybe_unused) + { +- unsigned int secure = 0; +- unsigned int pin = 0; +- +- lock_registering(); +- +- if (bank != GPIO_BANK_Z) +- return false; +- +- for (pin = 0; pin < get_gpioz_nbpin(); pin++) +- if (stm32mp_periph_is_secure(STM32MP1_SHRES_GPIOZ(pin))) +- secure++; +- +- return secure > 0 && secure == get_gpioz_nbpin(); ++ return true; + } ++#endif /* CFG_STM32MP15 */ + + bool stm32mp_nsec_can_access_clock(unsigned long clock_id) + { +@@ -496,13 +343,46 @@ bool stm32mp_nsec_can_access_clock(unsigned long clock_id) + return true; + + switch (clock_id) { +- case RTCAPB: +- case CK_MPU: +- case CK_AXI: + case BSEC: ++ case CK_AXI: ++ case CK_CSI: ++ case CK_HSE: ++ case CK_HSE_DIV2: ++ case CK_HSI: ++ case CK_LSE: ++ case CK_LSI: ++ case CK_MPU: ++ case PLL1_P: ++ case PLL1_Q: ++ case PLL1_R: ++ case PLL2_P: ++ case PLL2_Q: ++ case PLL2_R: ++ case PLL3_P: ++ case PLL3_Q: ++ case PLL3_R: ++ case RTCAPB: ++#ifdef CFG_STM32MP13 ++ case CK_MLAHB: ++ case CK_PER: ++ case PLL4_P: ++ case PLL4_Q: ++ case PLL4_R: ++ case PCLK1: ++ case PCLK2: ++ case PCLK3: ++ case PCLK4: ++ case PCLK5: ++ case PCLK6: ++ case CK_TIMG1: ++ case CK_TIMG2: ++ case CK_TIMG3: ++ case RTC: ++#endif + return true; ++#ifdef CFG_STM32MP15 + case GPIOZ: +- return !stm32mp_gpio_bank_is_secure(GPIO_BANK_Z); ++ return true; + case SPI6_K: + shres_id = STM32MP1_SHRES_SPI6; + break; +@@ -533,6 +413,7 @@ bool stm32mp_nsec_can_access_clock(unsigned long clock_id) + case CK_MCU: + shres_id = STM32MP1_SHRES_MCU; + break; ++#endif + default: + return false; + } +@@ -545,8 +426,7 @@ bool stm32mp_nsec_can_access_reset(unsigned int reset_id) + enum stm32mp_shres shres_id = STM32MP1_SHRES_COUNT; + + switch (reset_id) { +- case GPIOZ_R: +- return stm32mp_gpio_bank_is_non_secure(GPIO_BANK_Z); ++#ifdef CFG_STM32MP15 + case SPI6_R: + shres_id = STM32MP1_SHRES_SPI6; + break; +@@ -568,13 +448,14 @@ bool stm32mp_nsec_can_access_reset(unsigned int reset_id) + case RNG1_R: + shres_id = STM32MP1_SHRES_RNG1; + break; +- case MDMA_R: +- shres_id = STM32MP1_SHRES_MDMA; +- break; + case MCU_R: + case MCU_HOLD_BOOT_R: + shres_id = STM32MP1_SHRES_MCU; + break; ++#endif ++ case MDMA_R: ++ shres_id = STM32MP1_SHRES_MDMA; ++ break; + default: + return false; + } +@@ -582,6 +463,7 @@ bool stm32mp_nsec_can_access_reset(unsigned int reset_id) + return !stm32mp_periph_is_secure(shres_id); + } + ++#ifdef CFG_STM32MP15 + static bool mckprot_resource(enum stm32mp_shres id) + { + switch (id) { +@@ -593,109 +475,42 @@ static bool mckprot_resource(enum stm32mp_shres id) + } + } + +-#ifdef CFG_STM32_ETZPC +-static enum etzpc_decprot_attributes shres2decprot_attr(enum stm32mp_shres id) +-{ +- if (!stm32mp_periph_is_secure(id)) +- return ETZPC_DECPROT_NS_RW; +- +- if (mckprot_resource(id)) +- return ETZPC_DECPROT_MCU_ISOLATION; +- +- return ETZPC_DECPROT_S_RW; +-} +- +-/* Configure ETZPC cell and lock it when resource is secure */ +-static void config_lock_decprot(uint32_t decprot_id, +- enum etzpc_decprot_attributes decprot_attr) +-{ +- etzpc_configure_decprot(decprot_id, decprot_attr); +- +- if (decprot_attr == ETZPC_DECPROT_S_RW) +- etzpc_lock_decprot(decprot_id); +-} +- +-static void set_etzpc_secure_configuration(void) +-{ +- /* Some peripherals shall be secure */ +- config_lock_decprot(STM32MP1_ETZPC_STGENC_ID, ETZPC_DECPROT_S_RW); +- config_lock_decprot(STM32MP1_ETZPC_BKPSRAM_ID, ETZPC_DECPROT_S_RW); +- config_lock_decprot(STM32MP1_ETZPC_DDRCTRL_ID, ETZPC_DECPROT_NS_R_S_W); +- config_lock_decprot(STM32MP1_ETZPC_DDRPHYC_ID, ETZPC_DECPROT_NS_R_S_W); +- +- /* Configure ETZPC with peripheral registering */ +- config_lock_decprot(STM32MP1_ETZPC_IWDG1_ID, +- shres2decprot_attr(STM32MP1_SHRES_IWDG1)); +- config_lock_decprot(STM32MP1_ETZPC_USART1_ID, +- shres2decprot_attr(STM32MP1_SHRES_USART1)); +- config_lock_decprot(STM32MP1_ETZPC_SPI6_ID, +- shres2decprot_attr(STM32MP1_SHRES_SPI6)); +- config_lock_decprot(STM32MP1_ETZPC_I2C4_ID, +- shres2decprot_attr(STM32MP1_SHRES_I2C4)); +- config_lock_decprot(STM32MP1_ETZPC_RNG1_ID, +- shres2decprot_attr(STM32MP1_SHRES_RNG1)); +- config_lock_decprot(STM32MP1_ETZPC_HASH1_ID, +- shres2decprot_attr(STM32MP1_SHRES_HASH1)); +- config_lock_decprot(STM32MP1_ETZPC_CRYP1_ID, +- shres2decprot_attr(STM32MP1_SHRES_CRYP1)); +- config_lock_decprot(STM32MP1_ETZPC_I2C6_ID, +- shres2decprot_attr(STM32MP1_SHRES_I2C6)); +-} +-#else +-static void set_etzpc_secure_configuration(void) +-{ +- /* Nothing to do */ +-} +-#endif +- + static void check_rcc_secure_configuration(void) + { + bool secure = stm32_rcc_is_secure(); + bool mckprot = stm32_rcc_is_mckprot(); ++ bool need_secure = false; ++ bool need_mckprot = false; + enum stm32mp_shres id = STM32MP1_SHRES_COUNT; +- bool have_error = false; ++ uint32_t state = 0; + +- if (stm32mp_is_closed_device() && !secure) ++ if (stm32_bsec_get_state(&state)) + panic(); + ++ if (state == BSEC_STATE_SEC_CLOSED && !secure) ++ panic("Closed device mandates secure RCC"); ++ + for (id = 0; id < STM32MP1_SHRES_COUNT; id++) { + if (shres_state[id] != SHRES_SECURE) + continue; + +- if ((mckprot_resource(id) && !mckprot) || !secure) { +- EMSG("RCC %s MCKPROT %s and %s (%u) secure", +- secure ? "secure" : "non-secure", +- mckprot ? "set" : "not set", +- shres2str_id(id), id); +- have_error = true; +- } +- } +- +- if (have_error) +- panic(); +-} +- +-static void set_gpio_secure_configuration(void) +-{ +- unsigned int pin = 0; +- +- for (pin = 0; pin < get_gpioz_nbpin(); pin++) { +- enum stm32mp_shres shres = STM32MP1_SHRES_GPIOZ(pin); +- bool secure = stm32mp_periph_is_secure(shres); ++ need_secure = true; ++ if (mckprot_resource(id)) ++ need_mckprot = true; + +- stm32_gpio_set_secure_cfg(GPIO_BANK_Z, pin, secure); ++ if ((mckprot_resource(id) && !mckprot) || !secure) ++ EMSG("Error: RCC TZEN=%u MCKPROT=%u while %s is secure", ++ secure, mckprot, shres2str_id(id)); + } +-} + +-static TEE_Result gpioz_pm(enum pm_op op, uint32_t pm_hint __unused, +- const struct pm_callback_handle *hdl __unused) +-{ +- if (op == PM_OP_RESUME) +- set_gpio_secure_configuration(); ++ DMSG("RCC/PWR secure hardening: TZEN %sable, MCKPROT %sable", ++ secure ? "en" : "dis", mckprot ? "en" : "dis"); + +- return TEE_SUCCESS; ++ if (need_secure && !secure) ++ panic(); ++ ++ stm32mp1_clk_mcuss_protect(need_mckprot); + } +-DECLARE_KEEP_PAGER(gpioz_pm); + + static TEE_Result stm32mp1_init_final_shres(void) + { +@@ -710,15 +525,10 @@ static TEE_Result stm32mp1_init_final_shres(void) + shres2str_id(id), id, shres2str_state(*state)); + } + +- set_etzpc_secure_configuration(); +- if (IS_ENABLED(CFG_STM32_GPIO)) { +- set_gpio_secure_configuration(); +- register_pm_driver_cb(gpioz_pm, NULL, +- "stm32mp1-shared-resources"); +- } + check_rcc_secure_configuration(); + + return TEE_SUCCESS; + } + /* Finalize shres after drivers initialization, hence driver_init_late() */ + driver_init_late(stm32mp1_init_final_shres); ++#endif +diff --git a/core/arch/arm/plat-stm32mp1/stm32_util.h b/core/arch/arm/plat-stm32mp1/stm32_util.h +index 2105d6a0b..1c33fd37c 100644 +--- a/core/arch/arm/plat-stm32mp1/stm32_util.h ++++ b/core/arch/arm/plat-stm32mp1/stm32_util.h +@@ -1,6 +1,6 @@ + /* SPDX-License-Identifier: BSD-3-Clause */ + /* +- * Copyright (c) 2018-2019, STMicroelectronics ++ * Copyright (c) 2018-2021, STMicroelectronics + */ + + #ifndef __STM32_UTIL_H__ +@@ -9,12 +9,35 @@ + #include + #include + #include ++#include + #include ++#include + #include ++#include + #include + ++/* SoC versioning and device ID */ ++TEE_Result stm32mp1_dbgmcu_get_chip_version(uint32_t *chip_version); ++TEE_Result stm32mp1_dbgmcu_get_chip_dev_id(uint32_t *chip_dev_id); ++ ++/* OPP service */ ++bool stm32mp_supports_cpu_opp(uint32_t opp_id); ++ ++/* Crypto HW support */ ++bool stm32mp_supports_hw_cryp(void); ++ ++/* Second core support */ ++bool stm32mp_supports_second_core(void); ++ + /* Backup registers and RAM utils */ + vaddr_t stm32mp_bkpreg(unsigned int idx); ++vaddr_t stm32mp_bkpsram_base(void); ++ ++/* Platform util for the STGEN driver */ ++vaddr_t stm32mp_stgen_base(void); ++ ++/* Platform util for the GIC */ ++void stm32mp_gic_set_end_of_interrupt(uint32_t it); + + /* + * SYSCFG IO compensation. +@@ -23,27 +46,40 @@ vaddr_t stm32mp_bkpreg(unsigned int idx); + void stm32mp_syscfg_enable_io_compensation(void); + void stm32mp_syscfg_disable_io_compensation(void); + ++static inline void stm32mp_syscfg_enable_io_comp(void) ++{ ++ stm32mp_syscfg_enable_io_compensation(); ++} ++ ++static inline void stm32mp_syscfg_disable_io_comp(void) ++{ ++ stm32mp_syscfg_disable_io_compensation(); ++} ++ ++/* Get device ID from SYSCFG registers */ ++uint32_t stm32mp_syscfg_get_chip_dev_id(void); ++ ++/* Erase ESRAM3 */ ++TEE_Result stm32mp_syscfg_erase_sram3(void); ++ + /* Platform util for the GIC */ + vaddr_t get_gicc_base(void); + vaddr_t get_gicd_base(void); + + /* +- * Platform util functions for the GPIO driver +- * @bank: Target GPIO bank ID as per DT bindings +- * +- * Platform shall implement these functions to provide to stm32_gpio +- * driver the resource reference for a target GPIO bank. That are +- * memory mapped interface base address, interface offset (see below) +- * and clock identifier. +- * +- * stm32_get_gpio_bank_offset() returns a bank offset that is used to +- * check DT configuration matches platform implementation of the banks +- * description. ++ * Platform util for the IWDG driver + */ +-vaddr_t stm32_get_gpio_bank_base(unsigned int bank); +-unsigned int stm32_get_gpio_bank_offset(unsigned int bank); +-unsigned int stm32_get_gpio_bank_clock(unsigned int bank); +-struct clk *stm32_get_gpio_bank_clk(unsigned int bank); ++ ++/* Get IWDG_* enable flags mask from watchdog configuration read from fuses */ ++unsigned long stm32_get_iwdg_otp_config(vaddr_t pbase); ++ ++#ifdef CFG_TEE_CORE_DEBUG ++void stm32mp_dump_core_registers(bool force_display); ++#else ++static inline void stm32mp_dump_core_registers(bool force_display __unused) ++{ ++} ++#endif + + /* Platform util for PMIC support */ + bool stm32mp_with_pmic(void); +@@ -64,17 +100,11 @@ static inline void stm32mp_register_online_cpu(void) + uint32_t may_spin_lock(unsigned int *lock); + void may_spin_unlock(unsigned int *lock, uint32_t exceptions); + +-/* +- * Util for clock gating and to get clock rate for stm32 and platform drivers +- * @id: Target clock ID, ID used in clock DT bindings +- */ +-void stm32_clock_enable(unsigned long id); +-void stm32_clock_disable(unsigned long id); +-unsigned long stm32_clock_get_rate(unsigned long id); +-bool stm32_clock_is_enabled(unsigned long id); +- ++#ifdef CFG_DRIVERS_CLK + /* Helper from platform RCC clock driver */ + struct clk *stm32mp_rcc_clock_id_to_clk(unsigned long clock_id); ++unsigned int stm32mp_rcc_clk_to_clock_id(struct clk *clk); ++#endif + + /* Return true if @clock_id is shared by secure and non-secure worlds */ + bool stm32mp_nsec_can_access_clock(unsigned long clock_id); +@@ -91,6 +121,23 @@ static inline bool stm32mp_nsec_can_access_pmic_regu(const char *name __unused) + } + #endif + ++/* PM sequences specific to SoC STOP mode support */ ++void stm32mp1_clk_save_context_for_stop(void); ++void stm32mp1_clk_restore_context_for_stop(void); ++ ++/* Protect the MCU clock subsytem */ ++void stm32mp1_clk_mcuss_protect(bool enable); ++ ++/* ++ * Util for PLL1 settings management based on DT OPP table content. ++ */ ++int stm32mp1_clk_compute_all_pll1_settings(uint32_t buck1_voltage); ++void stm32mp1_clk_lp_save_opp_pll1_settings(uint8_t *data, size_t size); ++bool stm32mp1_clk_pll1_settings_are_valid(void); ++TEE_Result stm32mp1_set_opp_khz(uint32_t freq_khz); ++int stm32mp1_round_opp_khz(uint32_t *freq_khz); ++TEE_Result initialize_pll1_settings(void); ++ + /* + * Util for reset signal assertion/desassertion for stm32 and platform drivers + * @id: Target peripheral ID, ID used in reset DT bindings +@@ -112,6 +159,8 @@ static inline void stm32_reset_release(unsigned int id) + (void)stm32_reset_deassert(id, 0); + } + ++void stm32_reset_system(void); ++ + /* Return true if and only if @reset_id relates to a non-secure peripheral */ + bool stm32mp_nsec_can_access_reset(unsigned int reset_id); + +@@ -227,17 +276,7 @@ static inline int decr_refcnt(unsigned int *refcnt) + * resource as secure, non-secure or shared and to get the resource + * assignation state. + */ +-#define STM32MP1_SHRES_GPIOZ(i) (STM32MP1_SHRES_GPIOZ_0 + i) +- + enum stm32mp_shres { +- STM32MP1_SHRES_GPIOZ_0 = 0, +- STM32MP1_SHRES_GPIOZ_1, +- STM32MP1_SHRES_GPIOZ_2, +- STM32MP1_SHRES_GPIOZ_3, +- STM32MP1_SHRES_GPIOZ_4, +- STM32MP1_SHRES_GPIOZ_5, +- STM32MP1_SHRES_GPIOZ_6, +- STM32MP1_SHRES_GPIOZ_7, + STM32MP1_SHRES_IWDG1, + STM32MP1_SHRES_USART1, + STM32MP1_SHRES_SPI6, +@@ -272,33 +311,25 @@ void stm32mp_register_secure_periph_iomem(vaddr_t base); + */ + void stm32mp_register_non_secure_periph_iomem(vaddr_t base); + +-/* +- * Register GPIO resource as a secure peripheral +- * @bank: Bank of the target GPIO +- * @pin: Bit position of the target GPIO in the bank +- */ +-void stm32mp_register_secure_gpio(unsigned int bank, unsigned int pin); +- +-/* +- * Register GPIO resource as a non-secure peripheral +- * @bank: Bank of the target GPIO +- * @pin: Bit position of the target GPIO in the bank +- */ +-void stm32mp_register_non_secure_gpio(unsigned int bank, unsigned int pin); +- + /* Return true if and only if resource @id is registered as secure */ + bool stm32mp_periph_is_secure(enum stm32mp_shres id); + +-/* Return true if and only if GPIO bank @bank is registered as secure */ +-bool stm32mp_gpio_bank_is_secure(unsigned int bank); ++/* Register parent clocks of @clock (ID used in clock DT bindings) as secure */ ++void stm32mp_register_clock_parents_secure(unsigned long clock_id); + +-/* Return true if and only if GPIO bank @bank is registered as shared */ +-bool stm32mp_gpio_bank_is_shared(unsigned int bank); ++/* ++ * CPU operating point ++ */ + +-/* Return true if and only if GPIO bank @bank is registered as non-secure */ +-bool stm32mp_gpio_bank_is_non_secure(unsigned int bank); ++/* Return the number of CPU operating points */ ++size_t stm32mp1_cpu_opp_count(void); + +-/* Register parent clocks of @clock (ID used in clock DT bindings) as secure */ +-void stm32mp_register_clock_parents_secure(unsigned long clock_id); ++/* Get level value identifying CPU operating point @opp_index */ ++unsigned int stm32mp1_cpu_opp_level(size_t opp_index); ++ ++/* Request to switch to CPU operating point related to @level */ ++TEE_Result stm32mp1_cpu_opp_set_level(unsigned int level); + ++/* Get level related to current CPU operating point */ ++TEE_Result stm32mp1_cpu_opp_read_level(unsigned int *level); + #endif /*__STM32_UTIL_H__*/ +diff --git a/core/arch/arm/plat-stm32mp1/stm32mp_pm.h b/core/arch/arm/plat-stm32mp1/stm32mp_pm.h +new file mode 100644 +index 000000000..291ad5d1a +--- /dev/null ++++ b/core/arch/arm/plat-stm32mp1/stm32mp_pm.h +@@ -0,0 +1,24 @@ ++/* SPDX-License-Identifier: BSD-2-Clause */ ++/* ++ * Copyright (c) 2017-2018, STMicroelectronics ++ */ ++#ifndef __STM32MP_PM_H__ ++#define __STM32MP_PM_H__ ++ ++#define PSCI_MODE_SYSTEM_SUSPEND 0 ++#define PSCI_MODE_SYSTEM_OFF 1 ++ ++enum stm32mp1_pm_domain { ++ STM32MP1_PD_VSW, ++ STM32MP1_PD_CORE_RET, ++ STM32MP1_PD_CORE, ++ STM32MP1_PD_MAX_PM_DOMAIN ++}; ++ ++void __noreturn stm32_cores_reset(void); ++ ++/* Convert low power mode carried by pm_hint into pm mode name */ ++const char *stm32mp_pm_hint2mode_name(uint32_t pm_hint); ++ ++#endif /*__STM32MP_PM_H__*/ ++ +diff --git a/core/arch/arm/plat-stm32mp1/sub.mk b/core/arch/arm/plat-stm32mp1/sub.mk +index 39d7ca509..a3dd4575c 100644 +--- a/core/arch/arm/plat-stm32mp1/sub.mk ++++ b/core/arch/arm/plat-stm32mp1/sub.mk +@@ -2,11 +2,23 @@ global-incdirs-y += . + + srcs-y += main.c + srcs-y += reset.S +-srcs-$(CFG_STM32_RNG) += rng_seed.c ++srcs-$(CFG_WITH_SOFTWARE_PRNG) += rng_seed.c ++srcs-$(CFG_BSEC_PTA) += bsec_pta.c ++srcs-$(CFG_DISPLAY) += display.c + srcs-$(CFG_SCMI_MSG_DRIVERS) += scmi_server.c + srcs-y += shared_resources.c + srcs-$(CFG_TZC400) += plat_tzc400.c + srcs-$(CFG_WITH_PAGER) += link_dummies_paged.c ++srcs-$(CFG_RPROC_PTA) += remoteproc_pta.c ++srcs-$(CFG_STM32MP1_CPU_OPP) += cpu_opp.c ++ ++ifeq ($(CFG_RPROC_PTA),y) ++gensrcs-y += rproc_pub_key ++produce-rproc_pub_key = rproc_pub_key.c ++depends-rproc_pub_key = $(CFG_RPROC_SIGN_KEY) scripts/pem_to_pub_c.py ++recipe-rproc_pub_key = $(PYTHON3) scripts/pem_to_pub_c.py --prefix rproc_pub_key \ ++ --key $(CFG_RPROC_SIGN_KEY) --out $(sub-dir-out)/rproc_pub_key.c ++endif + + subdirs-y += drivers + subdirs-y += nsec-service +diff --git a/core/arch/arm/sm/pm_a32.S b/core/arch/arm/sm/pm_a32.S +index a927b1e77..3e0080af4 100644 +--- a/core/arch/arm/sm/pm_a32.S ++++ b/core/arch/arm/sm/pm_a32.S +@@ -65,25 +65,30 @@ a9_suspend: + a7_suspend: + read_fcseidr r4 + read_tpidruro r5 +- stmia r0!, {r4 - r5} +- read_dacr r4 ++ read_dacr r6 ++ stmia r0!, {r4 - r6} + #ifdef CFG_WITH_LPAE +-#error "Not supported" ++ read_ttbr0_64bit r4, r5 ++ read_ttbr1_64bit r6, r7 ++ read_mair0 r8 ++ read_mair1 r9 ++ stmia r0!, {r4 - r9} + #else +- read_ttbr0 r5 +- read_ttbr1 r6 +- read_ttbcr r7 ++ read_ttbr0 r4 ++ read_ttbr1 r5 ++ read_prrr r6 ++ read_nmrr r7 ++ stmia r0!, {r4 - r7} + #endif +- read_sctlr r8 +- read_actlr r9 +- read_cpacr r10 +- read_mvbar r11 +- stmia r0!, {r4 - r11} +- read_prrr r4 +- read_nmrr r5 +- read_vbar r6 +- read_nsacr r7 +- stmia r0, {r4 - r7} ++ read_ttbcr r4 ++ read_actlr r5 ++ read_cpacr r6 ++ read_mvbar r7 ++ read_vbar r8 ++ read_nsacr r9 ++ read_sctlr r10 ++ stmia r0, {r4 - r10} ++ + pop {r4 - r11} + bx lr + END_FUNC sm_pm_cpu_do_suspend +@@ -146,50 +151,50 @@ UNWIND( .cantunwind) + cmp r5, r4 + beq a7_resume + +- /* +- * A9 needs PCR/DIAG +- */ ++ /* A9 needs PCR/DIAG */ + ldmia r0!, {r4 - r5} + write_pcr r4 + write_diag r5 +- + a7_resume: +- /* v7 resume */ ++ /* Armv7 generic resume */ + mov ip, #0 + /* Invalidate icache to PoU */ + write_iciallu + /* set reserved context */ + write_contextidr ip +- ldmia r0!, {r4 - r5} ++ ldmia r0!, {r4 - r6} + write_fcseidr r4 + write_tpidruro r5 +- ldmia r0!, {r4 - r11} + /* Invalidate entire TLB */ + write_tlbiall +- write_dacr r4 ++ write_dacr r6 + #ifdef CFG_WITH_LPAE +-#error "Not supported -" ++ ldmia r0!, {r4 - r9} ++ write_ttbr0_64bit r4, r5 ++ write_ttbr1_64bit r6, r7 ++ write_mair0 r8 ++ write_mair1 r9 + #else +- write_ttbr0 r5 +- write_ttbr1 r6 +- write_ttbcr r7 ++ ldmia r0!, {r4 - r7} ++ write_ttbr0 r4 ++ write_ttbr1 r5 ++ write_prrr r6 ++ write_nmrr r7 + #endif +- +- ldmia r0, {r4 - r7} +- write_prrr r4 +- write_nmrr r5 +- write_vbar r6 +- write_nsacr r7 +- +- write_actlr r9 +- write_cpacr r10 +- write_mvbar r11 ++ ldmia r0!, {r4 - r10} ++ write_ttbcr r4 ++ write_actlr r5 ++ write_cpacr r6 ++ write_mvbar r7 ++ write_vbar r8 ++ write_nsacr r9 + write_bpiall + isb + dsb + /* MMU will be enabled here */ +- write_sctlr r8 ++ write_sctlr r10 + isb ++ + mov r0, #0 + b suspend_return + END_FUNC sm_pm_cpu_do_resume +diff --git a/core/arch/arm/tee/entry_fast.c b/core/arch/arm/tee/entry_fast.c +index 08955f9ba..9457ac154 100644 +--- a/core/arch/arm/tee/entry_fast.c ++++ b/core/arch/arm/tee/entry_fast.c +@@ -100,6 +100,9 @@ static void tee_entry_exchange_capabilities(struct thread_smc_args *args) + DMSG("Asynchronous notifications are %sabled", + IS_ENABLED(CFG_CORE_ASYNC_NOTIF) ? "en" : "dis"); + ++ if (IS_ENABLED(CFG_CORE_OCALL)) ++ args->a1 |= OPTEE_SMC_SEC_CAP_OCALL; ++ + #if defined(CFG_CORE_DYN_SHM) + dyn_shm_en = core_mmu_nsec_ddr_is_defined(); + if (dyn_shm_en) +diff --git a/core/arch/arm/tee/sub.mk b/core/arch/arm/tee/sub.mk +index f0aeb7d5a..1211313ed 100644 +--- a/core/arch/arm/tee/sub.mk ++++ b/core/arch/arm/tee/sub.mk +@@ -9,3 +9,4 @@ srcs-y += entry_fast.c + cppflags-entry_fast.c-y += -DTEE_IMPL_GIT_SHA1=$(TEE_IMPL_GIT_SHA1) + endif + srcs-y += cache.c ++srcs-$(CFG_WITH_TUI) += tui.c +diff --git a/core/arch/arm/tee/tui.c b/core/arch/arm/tee/tui.c +new file mode 100644 +index 000000000..fc2262db8 +--- /dev/null ++++ b/core/arch/arm/tee/tui.c +@@ -0,0 +1,157 @@ ++// SPDX-License-Identifier: BSD-2-Clause ++/* ++ * Copyright (c) 2016-2021, Linaro Limited ++ */ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#define TUI_INPUT_BUFFER_SIZE 16 ++ ++/** ++ * struct tui_input - Trusted UI input state ++ * ++ * @lock - Lock changes ++ * @queue - Array of possible input events location in TUI ++ * @write_pos ++ * @read_pos ++ */ ++struct tui_input { ++ unsigned int lock; ++ struct { ++ int16_t key; ++ uint16_t x; ++ uint16_t y; ++ } queue[TUI_INPUT_BUFFER_SIZE]; ++ size_t write_pos; ++ size_t read_pos; ++}; ++ ++struct tui_input tui_input = { ++ .lock = SPINLOCK_UNLOCK, ++ .read_pos = TUI_INPUT_BUFFER_SIZE, ++}; ++ ++TEE_Result tui_input_enable(void) ++{ ++ uint32_t exceptions = cpu_spin_lock_xsave(&tui_input.lock); ++ ++ tui_input.read_pos = tui_input.write_pos; ++ ++ cpu_spin_unlock_xrestore(&tui_input.lock, exceptions); ++ ++ return TEE_SUCCESS; ++} ++ ++void tui_input_disable(void) ++{ ++ uint32_t exceptions = cpu_spin_lock_xsave(&tui_input.lock); ++ ++ tui_input.read_pos = TUI_INPUT_BUFFER_SIZE; ++ ++ cpu_spin_unlock_xrestore(&tui_input.lock, exceptions); ++} ++ ++static size_t next_pos(size_t pos) ++{ ++ return (pos + 1) % TUI_INPUT_BUFFER_SIZE; ++} ++ ++TEE_Result tui_input_get(struct ts_session *s, size_t timeout, ++ bool *is_timedout, bool *is_key, ++ uint32_t *key, size_t *xpos, size_t *ypos) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ uint32_t exceptions = 0; ++ TEE_Time cur_time = { }; ++ TEE_Time base_time = { }; ++ TEE_Time timeout_time = { ++ .seconds = timeout / TEE_TIME_MILLIS_BASE, ++ .millis = timeout % TEE_TIME_MILLIS_BASE, ++ }; ++ ++ res = tee_time_get_sys_time(&base_time); ++ if (res) ++ return res; ++ cur_time = base_time; ++ TEE_TIME_ADD(timeout_time, base_time, timeout_time); ++ ++ exceptions = cpu_spin_lock_xsave(&tui_input.lock); ++ ++ if (tui_input.read_pos >= TUI_INPUT_BUFFER_SIZE) { ++ /* Input cancelled or not enabled */ ++ res = TEE_ERROR_EXTERNAL_CANCEL; ++ goto out; ++ } ++ ++ while (tui_input.read_pos == tui_input.write_pos && ++ TEE_TIME_LT(cur_time, timeout_time)) { ++ cpu_spin_unlock_xrestore(&tui_input.lock, exceptions); ++ ++ /* ++ * Will take eventual interrupt(s) here. ++ */ ++ ++ res = tee_time_get_sys_time(&cur_time); ++ if (res) ++ return res; ++ ++ res = tui_get_time_to_session_timeout(s, &cur_time, NULL); ++ if (res) ++ return res; ++ ++ if (tee_ta_session_is_cancelled(to_ta_session(s), &cur_time)) ++ return TEE_ERROR_CANCEL; ++ ++ exceptions = cpu_spin_lock_xsave(&tui_input.lock); ++ } ++ ++ if (tui_input.read_pos >= TUI_INPUT_BUFFER_SIZE) { ++ /* Input cancelled or not enabled */ ++ res = TEE_ERROR_EXTERNAL_CANCEL; ++ goto out; ++ } ++ ++ if (res == TEE_SUCCESS) { ++ if (TEE_TIME_LT(cur_time, timeout_time)) { ++ *is_timedout = false; ++ *is_key = !!tui_input.queue[tui_input.read_pos].key; ++ *key = tui_input.queue[tui_input.read_pos].key; ++ *xpos = tui_input.queue[tui_input.read_pos].x; ++ *ypos = tui_input.queue[tui_input.read_pos].y; ++ tui_input.read_pos = next_pos(tui_input.read_pos); ++ } else { ++ *is_timedout = true; ++ *is_key = false; ++ *key = 0; ++ *xpos = 0; ++ *ypos = 0; ++ } ++ } ++ ++out: ++ cpu_spin_unlock_xrestore(&tui_input.lock, exceptions); ++ ++ return res; ++} ++ ++void tui_async_input(uint16_t key, uint16_t x, uint16_t y) ++{ ++ uint32_t exceptions = cpu_spin_lock_xsave(&tui_input.lock); ++ ++ if (tui_input.read_pos < TUI_INPUT_BUFFER_SIZE && ++ next_pos(tui_input.write_pos) != tui_input.read_pos) { ++ tui_input.queue[tui_input.write_pos].key = key; ++ tui_input.queue[tui_input.write_pos].x = x; ++ tui_input.queue[tui_input.write_pos].y = y; ++ tui_input.write_pos = next_pos(tui_input.write_pos); ++ } ++ ++ cpu_spin_unlock_xrestore(&tui_input.lock, exceptions); ++} +diff --git a/core/core.mk b/core/core.mk +index c4c070ee2..068eb87a2 100644 +--- a/core/core.mk ++++ b/core/core.mk +@@ -17,6 +17,23 @@ PLATFORM_FLAVOR_$(PLATFORM_FLAVOR) := y + $(eval $(call cfg-depends-all,CFG_PAGED_USER_TA,CFG_WITH_PAGER CFG_WITH_USER_TA)) + include core/crypto.mk + ++ifeq ($(CFG_WITH_TUI),y) ++ifneq ($(CFG_FRAME_BUFFER),y) ++(warning: disabling CFG_WITH_TUI due to missing CFG_FRAME_BUFFER) ++CFG_WITH_TUI := ++ifeq ($(CFG_WITH_TUI),y) ++$(error error: can't disable CFG_WITH_TUI) ++endif ++endif ++ifneq ($(CFG_DISPLAY),y) ++$(warning warning: disabling CFG_WITH_TUI due to missing CFG_DISPLAY) ++CFG_WITH_TUI := ++ifeq ($(CFG_WITH_TUI),y) ++$(error error: can't disable CFG_WITH_TUI) ++endif ++endif ++endif ++ + cppflags$(sm) += -D__KERNEL__ + + cppflags$(sm) += -Icore/include +@@ -138,7 +155,7 @@ include mk/lib.mk + + ifeq ($(CFG_ZLIB),y) + libname = zlib +-libdir = core/lib/zlib ++libdir = lib/libzlib + include mk/lib.mk + endif + +diff --git a/core/drivers/clk/clk-stm32-core.c b/core/drivers/clk/clk-stm32-core.c +new file mode 100644 +index 000000000..cee09799c +--- /dev/null ++++ b/core/drivers/clk/clk-stm32-core.c +@@ -0,0 +1,733 @@ ++// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) ++/* ++ * Copyright (C) 2018-2021, STMicroelectronics - All Rights Reserved ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#include "clk-stm32-core.h" ++ ++#define RCC_MP_ENCLRR_OFFSET 0x4 ++ ++static struct clk_stm32_priv *stm32_clock_data; ++ ++inline struct clk_stm32_priv *clk_stm32_get_priv(void) ++{ ++ return stm32_clock_data; ++} ++ ++uintptr_t clk_stm32_get_rcc_base(void) ++{ ++ struct clk_stm32_priv *priv = clk_stm32_get_priv(); ++ ++ return priv->base; ++} ++ ++#define TIMEOUT_US_200MS U(200000) ++#define TIMEOUT_US_1S U(1000000) ++ ++/* STM32 MUX API */ ++int clk_stm32_get_parent_mux(uint32_t mux_id) ++{ ++ struct clk_stm32_priv *priv = clk_stm32_get_priv(); ++ const struct mux_cfg *mux = &priv->muxes[mux_id]; ++ uint32_t mask = MASK_WIDTH_SHIFT(mux->width, mux->shift); ++ ++ return (io_read32(priv->base + mux->offset) & mask) >> mux->shift; ++} ++ ++int clk_stm32_set_parent_mux(uint16_t mux_id, uint8_t sel) ++{ ++ struct clk_stm32_priv *priv = clk_stm32_get_priv(); ++ const struct mux_cfg *mux = &priv->muxes[mux_id]; ++ uint32_t mask = MASK_WIDTH_SHIFT(mux->width, mux->shift); ++ uintptr_t address = priv->base + mux->offset; ++ ++ io_clrsetbits32(address, mask, (sel << mux->shift) & mask); ++ ++ if (mux->ready == MUX_NO_RDY) ++ return 0; ++ ++ return clk_stm32_wait_ready_gate((uint16_t) mux->ready, true); ++} ++ ++/* STM32 GATE API */ ++void clk_stm32_endisable_gate(uint16_t gate_id, bool enable) ++{ ++ struct clk_stm32_priv *priv = clk_stm32_get_priv(); ++ const struct gate_cfg *gate = &priv->gates[gate_id]; ++ uintptr_t addr = priv->base + gate->offset; ++ ++ if (enable) { ++ if (gate->set_clr != 0U) ++ io_write32(addr, BIT(gate->bit_idx)); ++ else ++ io_setbits32_stm32shregs(addr, BIT(gate->bit_idx)); ++ } else { ++ if (gate->set_clr != 0U) ++ io_write32(addr + RCC_MP_ENCLRR_OFFSET, BIT(gate->bit_idx)); ++ else ++ io_clrbits32_stm32shregs(addr, BIT(gate->bit_idx)); ++ } ++} ++ ++void clk_stm32_disable_gate(uint16_t gate_id) ++{ ++ clk_stm32_endisable_gate(gate_id, false); ++} ++ ++int clk_stm32_enable_gate(uint16_t gate_id) ++{ ++ clk_stm32_endisable_gate(gate_id, true); ++ ++ return 0; ++} ++ ++bool clk_stm32_is_enabled_gate(uint16_t gate_id) ++{ ++ struct clk_stm32_priv *priv = clk_stm32_get_priv(); ++ const struct gate_cfg *gate = &priv->gates[gate_id]; ++ uintptr_t addr = priv->base + gate->offset; ++ ++ return (io_read32(addr) & BIT(gate->bit_idx)) != 0U; ++} ++ ++int clk_stm32_wait_ready_gate(uint16_t gate_id, bool ready_on) ++{ ++ struct clk_stm32_priv *priv = clk_stm32_get_priv(); ++ const struct gate_cfg *gate = &priv->gates[gate_id]; ++ uintptr_t address = priv->base + gate->offset; ++ uint32_t mask_rdy = BIT(gate->bit_idx); ++ uint64_t timeout = timeout_init_us(TIMEOUT_US_1S); ++ uint32_t mask = 0U; ++ ++ if (ready_on) ++ mask = BIT(gate->bit_idx); ++ ++ while ((io_read32(address) & mask_rdy) != mask) ++ if (timeout_elapsed(timeout)) ++ break; ++ ++ if ((io_read32(address) & mask_rdy) != mask) ++ return -1; ++ ++ return 0; ++} ++ ++/* STM32 GATE READY CLOCK OPS */ ++int clk_stm32_gate_ready_endisable(uint16_t gate_id, bool enable, bool wait_rdy) ++{ ++ clk_stm32_endisable_gate(gate_id, enable); ++ ++ if (wait_rdy == true) ++ return clk_stm32_wait_ready_gate(gate_id + 1, enable); ++ ++ return 0; ++} ++ ++int clk_stm32_gate_rdy_enable(uint16_t gate_id) ++{ ++ return clk_stm32_gate_ready_endisable(gate_id, true, true); ++} ++ ++int clk_stm32_gate_rdy_disable(uint16_t gate_id) ++{ ++ return clk_stm32_gate_ready_endisable(gate_id, false, true); ++} ++ ++/* STM32 DIV API */ ++#define clk_div_mask(_width) GENMASK_32(((_width) - 1U), 0U) ++ ++static unsigned int _get_table_div(const struct div_table_cfg *table, ++ unsigned int val) ++{ ++ const struct div_table_cfg *clkt; ++ ++ for (clkt = table; clkt->div; clkt++) ++ if (clkt->val == val) ++ return clkt->div; ++ ++ return 0; ++} ++ ++ ++static unsigned int _get_table_val(const struct div_table_cfg *table, ++ unsigned int div) ++{ ++ const struct div_table_cfg *clkt; ++ ++ for (clkt = table; clkt->div; clkt++) ++ if (clkt->div == div) ++ return clkt->val; ++ return 0; ++} ++ ++static unsigned int _get_div(const struct div_table_cfg *table, ++ unsigned int val, unsigned long flags, ++ uint8_t width) ++{ ++ if ((flags & CLK_DIVIDER_ONE_BASED) != 0UL) ++ return val; ++ ++ if ((flags & CLK_DIVIDER_POWER_OF_TWO) != 0UL) ++ return BIT(val); ++ ++ if ((flags & CLK_DIVIDER_MAX_AT_ZERO) != 0UL) ++ return (val != 0U) ? val : BIT(width); ++ ++ if (table != NULL) ++ return _get_table_div(table, val); ++ ++ return val + 1U; ++} ++ ++static unsigned int _get_val(const struct div_table_cfg *table, ++ unsigned int div, unsigned long flags, ++ uint8_t width) ++{ ++ if ((flags & CLK_DIVIDER_ONE_BASED) != 0UL) ++ return div; ++ ++ if ((flags & CLK_DIVIDER_POWER_OF_TWO) != 0UL) ++ return __builtin_ffs(div) - 1; ++ ++ if ((flags & CLK_DIVIDER_MAX_AT_ZERO) != 0UL) ++ return (div != 0U) ? div : BIT(width); ++ ++ if (table != NULL) ++ return _get_table_val(table, div); ++ ++ return div - 1U; ++} ++ ++static bool is_power_of_2(unsigned long n) ++{ ++ return (n != 0 && ((n & (n - 1)) == 0)); ++} ++ ++static bool _is_valid_table_div(const struct div_table_cfg *table, ++ unsigned int div) ++{ ++ const struct div_table_cfg *clkt; ++ ++ for (clkt = table; clkt->div; clkt++) ++ if (clkt->div == div) ++ return true; ++ ++ return false; ++} ++ ++static bool _is_valid_div(const struct div_table_cfg *table, ++ unsigned int div, unsigned long flags) ++{ ++ if (flags & CLK_DIVIDER_POWER_OF_TWO) ++ return is_power_of_2(div); ++ ++ if (table) ++ return _is_valid_table_div(table, div); ++ ++ return true; ++} ++ ++static int divider_get_val(unsigned long rate, unsigned long parent_rate, ++ const struct div_table_cfg *table, uint8_t width, ++ unsigned long flags) ++{ ++ unsigned int div = 0U; ++ unsigned int value = 0U; ++ ++ div = UDIV_ROUND_NEAREST((uint64_t)parent_rate, rate); ++ ++ if (!_is_valid_div(table, div, flags)) ++ return -1; ++ ++ value = _get_val(table, div, flags, width); ++ ++ return MIN(value, clk_div_mask(width)); ++} ++ ++uint32_t clk_stm32_div_get_value(int div_id) ++{ ++ struct clk_stm32_priv *priv = clk_stm32_get_priv(); ++ const struct div_cfg *divider = &priv->div[div_id]; ++ uint32_t val = 0; ++ ++ val = io_read32(priv->base + divider->offset) >> divider->shift; ++ val &= clk_div_mask(divider->width); ++ ++ return val; ++} ++ ++int clk_stm32_set_div_value(uint32_t div_id, uint32_t value) ++{ ++ struct clk_stm32_priv *priv = clk_stm32_get_priv(); ++ const struct div_cfg *divider; ++ uintptr_t address; ++ uint32_t mask; ++ ++ if (div_id >= priv->nb_div) ++ panic(); ++ ++ divider = &priv->div[div_id]; ++ address = priv->base + divider->offset; ++ ++ mask = MASK_WIDTH_SHIFT(divider->width, divider->shift); ++ io_clrsetbits32(address, mask, (value << divider->shift) & mask); ++ ++ if (divider->ready == DIV_NO_RDY) ++ return 0; ++ ++ return clk_stm32_wait_ready_gate((uint16_t) divider->ready, true); ++} ++ ++unsigned long clk_stm32_get_rate_divider(int div_id, unsigned long prate) ++{ ++ struct clk_stm32_priv *priv = clk_stm32_get_priv(); ++ const struct div_cfg *divider = &priv->div[div_id]; ++ uint32_t val = clk_stm32_div_get_value(div_id); ++ unsigned int div = 0U; ++ ++ div = _get_div(divider->table, val, divider->flags, divider->width); ++ if (div == 0U) ++ return prate; ++ ++ return ROUNDUP_DIV((uint64_t)prate, div); ++} ++ ++int clk_stm32_set_rate_divider(int div_id, unsigned long rate, ++ unsigned long prate) ++{ ++ struct clk_stm32_priv *priv = clk_stm32_get_priv(); ++ const struct div_cfg *divider = &priv->div[div_id]; ++ int value = 0; ++ ++ value = divider_get_val(rate, prate, divider->table, ++ divider->width, divider->flags); ++ ++ if (value < 0) ++ return value; ++ ++ return clk_stm32_set_div_value(div_id, value); ++} ++ ++/* STM32 MUX CLOCK OPS */ ++size_t clk_stm32_mux_get_parent(struct clk *clk) ++{ ++ struct clk_stm32_mux_cfg *cfg = clk->priv; ++ ++ return clk_stm32_get_parent_mux(cfg->mux_id); ++} ++ ++TEE_Result clk_stm32_mux_set_parent(struct clk *clk, size_t pidx) ++{ ++ struct clk_stm32_mux_cfg *cfg = clk->priv; ++ ++ return clk_stm32_set_parent_mux(cfg->mux_id, pidx); ++} ++ ++const struct clk_ops clk_stm32_mux_ops = { ++ .get_parent = clk_stm32_mux_get_parent, ++ .set_parent = clk_stm32_mux_set_parent, ++}; ++ ++/* STM32 GATE CLOCK OPS */ ++static TEE_Result clk_stm32_gate_enable(struct clk *clk) ++{ ++ struct clk_stm32_gate_cfg *cfg = clk->priv; ++ ++ return clk_stm32_enable_gate(cfg->gate_id); ++} ++ ++static void clk_stm32_gate_disable(struct clk *clk) ++{ ++ struct clk_stm32_gate_cfg *cfg = clk->priv; ++ ++ clk_stm32_disable_gate(cfg->gate_id); ++} ++ ++static bool clk_stm32_gate_is_enabled(struct clk *clk) ++{ ++ struct clk_stm32_gate_cfg *cfg = clk->priv; ++ ++ return clk_stm32_is_enabled_gate(cfg->gate_id); ++} ++ ++const struct clk_ops clk_stm32_gate_ops = { ++ .enable = clk_stm32_gate_enable, ++ .disable = clk_stm32_gate_disable, ++ .is_enabled = clk_stm32_gate_is_enabled, ++}; ++ ++static TEE_Result clk_stm32_gate_ready_enable(struct clk *clk) ++{ ++ struct clk_stm32_gate_cfg *cfg = clk->priv; ++ ++ if (clk_stm32_gate_rdy_enable(cfg->gate_id) != 0U) { ++ EMSG("%s timeout\n", clk_get_name(clk)); ++ panic(); ++ } ++ ++ return 0; ++} ++ ++static void clk_stm32_gate_ready_disable(struct clk *clk) ++{ ++ struct clk_stm32_gate_cfg *cfg = clk->priv; ++ ++ if (clk_stm32_gate_rdy_disable(cfg->gate_id) != 0U) ++ panic(); ++} ++ ++const struct clk_ops clk_stm32_gate_ready_ops = { ++ .enable = clk_stm32_gate_ready_enable, ++ .disable = clk_stm32_gate_ready_disable, ++ .is_enabled = clk_stm32_gate_is_enabled, ++}; ++ ++/* STM32 DIV CLOCK OPS */ ++unsigned long clk_stm32_divider_get_rate(struct clk *clk, ++ unsigned long parent_rate) ++{ ++ struct clk_stm32_div_cfg *cfg = clk->priv; ++ ++ return clk_stm32_get_rate_divider(cfg->div_id, parent_rate); ++} ++ ++TEE_Result clk_stm32_divider_set_rate(struct clk *clk, ++ unsigned long rate, ++ unsigned long parent_rate) ++{ ++ struct clk_stm32_div_cfg *cfg = clk->priv; ++ ++ return clk_stm32_set_rate_divider(cfg->div_id, rate, parent_rate); ++} ++ ++const struct clk_ops clk_stm32_divider_ops = { ++ .get_rate = clk_stm32_divider_get_rate, ++ .set_rate = clk_stm32_divider_set_rate, ++}; ++ ++/* STM32 COMPOSITE CLOCK OPS */ ++size_t clk_stm32_composite_get_parent(struct clk *clk) ++{ ++ struct clk_stm32_composite_cfg *cfg = clk->priv; ++ ++ if (cfg->mux_id == NO_MUX) { ++ /* TODO: it could be a normal case */ ++ return 0; ++ } ++ ++ return clk_stm32_get_parent_mux(cfg->mux_id); ++} ++ ++TEE_Result clk_stm32_composite_set_parent(struct clk *clk, size_t pidx) ++{ ++ struct clk_stm32_composite_cfg *cfg = clk->priv; ++ ++ if (cfg->mux_id == NO_MUX) { ++ panic(); ++ } ++ ++ return clk_stm32_set_parent_mux(cfg->mux_id, pidx); ++} ++ ++unsigned long clk_stm32_composite_get_rate(struct clk *clk, ++ unsigned long parent_rate) ++{ ++ struct clk_stm32_composite_cfg *cfg = clk->priv; ++ ++ if (cfg->div_id == NO_DIV) ++ return parent_rate; ++ ++ return clk_stm32_get_rate_divider(cfg->div_id, parent_rate); ++} ++ ++TEE_Result clk_stm32_composite_set_rate(struct clk *clk, unsigned long rate, ++ unsigned long parent_rate) ++{ ++ struct clk_stm32_composite_cfg *cfg = clk->priv; ++ ++ if (cfg->div_id == NO_DIV) ++ return 0; ++ ++ return clk_stm32_set_rate_divider(cfg->div_id, rate, parent_rate); ++} ++ ++TEE_Result clk_stm32_composite_gate_enable(struct clk *clk) ++{ ++ struct clk_stm32_composite_cfg *cfg = clk->priv; ++ ++ return clk_stm32_enable_gate(cfg->gate_id); ++} ++ ++void clk_stm32_composite_gate_disable(struct clk *clk) ++{ ++ struct clk_stm32_composite_cfg *cfg = clk->priv; ++ ++ clk_stm32_disable_gate(cfg->gate_id); ++} ++ ++bool clk_stm32_composite_gate_is_enabled(struct clk *clk) ++{ ++ struct clk_stm32_composite_cfg *cfg = clk->priv; ++ ++ return clk_stm32_is_enabled_gate(cfg->gate_id); ++} ++ ++const struct clk_ops clk_stm32_composite_ops = { ++ .get_parent = clk_stm32_composite_get_parent, ++ .set_parent = clk_stm32_composite_set_parent, ++ .get_rate = clk_stm32_composite_get_rate, ++ .set_rate = clk_stm32_composite_set_rate, ++ .enable = clk_stm32_composite_gate_enable, ++ .disable = clk_stm32_composite_gate_disable, ++ .is_enabled = clk_stm32_composite_gate_is_enabled, ++}; ++ ++TEE_Result clk_stm32_set_parent_by_index(struct clk *clk, size_t pidx) ++{ ++ struct clk *parent = clk_get_parent_by_index(clk, pidx); ++ TEE_Result res = TEE_ERROR_GENERIC; ++ ++ if (parent) ++ res = clk_set_parent(clk, parent); ++ ++ return res; ++} ++ ++static bool clk_stm32_get_ignore_unused_property(void) ++{ ++ int node; ++ const char *prop; ++ const void *fdt; ++ ++ fdt = get_embedded_dt(); ++ if (!fdt) ++ panic(); ++ ++ node = fdt_path_offset(fdt, "/chosen"); ++ if (node < 0) ++ return false; ++ ++ prop = fdt_getprop(fdt, node, "bootargs", NULL); ++ if (prop == NULL) ++ return false; ++ ++ return strcmp(prop, "clk_ignore_unused") == 0; ++} ++ ++int clk_stm32_parse_fdt_by_name(const void *fdt, int node, const char *name, ++ uint32_t *tab, uint32_t *nb) ++{ ++ const fdt32_t *cell; ++ int len = 0; ++ uint32_t i; ++ ++ cell = fdt_getprop(fdt, node, name, &len); ++ if (cell != NULL) { ++ for (i = 0; i < ((uint32_t)len / sizeof(uint32_t)); i++) { ++ uint32_t val = fdt32_to_cpu(cell[i]); ++ ++ tab[i] = val; ++ } ++ } ++ ++ *nb = (uint32_t)len / sizeof(uint32_t); ++ ++ return 0; ++} ++int clk_stm32_init(struct clk_stm32_priv *priv, uintptr_t base) ++{ ++ stm32_clock_data = priv; ++ ++ priv->base = base; ++ ++ priv->clk_ignore_unused = clk_stm32_get_ignore_unused_property(); ++ ++ return 0; ++} ++ ++unsigned long fixed_factor_get_rate(struct clk *clk, unsigned long parent_rate) ++{ ++ struct fixed_factor_cfg *d = clk->priv; ++ ++ unsigned long long rate = (unsigned long long)parent_rate * d->mult; ++ ++ if (d->div == 0U) ++ panic("error division by zero\n"); ++ ++ return (unsigned long)(rate / d->div); ++}; ++ ++const struct clk_ops clk_fixed_factor_ops = { ++ .get_rate = fixed_factor_get_rate, ++}; ++ ++static unsigned long clk_fixed_get_rate(struct clk *clk, ++ unsigned long parent_rate __unused) ++{ ++ struct clk_fixed_rate_cfg *cfg = clk->priv; ++ ++ return cfg->rate; ++} ++ ++const struct clk_ops clk_fixed_clk_ops = { ++ .get_rate = clk_fixed_get_rate, ++}; ++ ++struct clk *stm32mp_rcc_clock_id_to_clk(unsigned long clock_id) ++{ ++ struct clk_stm32_priv *priv = clk_stm32_get_priv(); ++ ++ if (clock_id > priv->nb_clk_refs) ++ return NULL; ++ ++ return priv->clk_refs[clock_id]; ++} ++ ++static struct clk *stm32mp_clk_dt_get_clk(struct dt_driver_phandle_args *pargs, ++ void *data __unused, TEE_Result *res) ++{ ++ unsigned long clock_id = pargs->args[0]; ++ struct clk *clk = NULL; ++ ++ *res = TEE_ERROR_BAD_PARAMETERS; ++ ++ if (pargs->args_count != 1) ++ return NULL; ++ ++ clk = stm32mp_rcc_clock_id_to_clk(clock_id); ++ if (!clk) ++ return NULL; ++ ++ *res = TEE_SUCCESS; ++ return clk; ++} ++ ++static void clk_stm32_register_clocks(struct clk_stm32_priv *priv) ++{ ++ size_t i = 0; ++ ++ for(i = 0; i < priv->nb_clk_refs; i++) { ++ struct clk *clk = priv->clk_refs[i]; ++ ++ if (clk == NULL) ++ continue; ++ ++ refcount_set(&clk->enabled_count, 0); ++ ++ if (clk_register(clk)) ++ panic(); ++ } ++ ++ /* Critical clocks management */ ++ for(i = 0; i < priv->nb_clk_refs; i++) { ++ struct clk *clk = priv->clk_refs[i]; ++ ++ if (clk == NULL) ++ continue; ++ ++ if (priv->is_critical && priv->is_critical(clk)) ++ clk_enable(clk); ++ } ++} ++ ++#ifdef CFG_STM32_CLK_DEBUG ++static void clk_stm32_display_clock_ignore_unused(void) ++{ ++ struct clk_stm32_priv *priv = clk_stm32_get_priv(); ++ size_t i; ++ ++ printf("\nCLOCK CLK_IGNORE_UNUSED:\n"); ++ ++ printf("\tSTATUS = %s\n", ++ priv->clk_ignore_unused ? "ENABLED" : "DISABLED"); ++ ++ printf("CLOCK WITH CLK_IGNORE_UNUSED FLAGS:\n"); ++ for(i = 0; i < priv->nb_clk_refs; i++) { ++ struct clk *clk = priv->clk_refs[i]; ++ ++ if (priv->is_ignore_unused && priv->is_ignore_unused(clk)) ++ printf("\t%s\n", clk_get_name(clk)); ++ } ++ ++ printf("CLOCK DISABLED IF CLK_IGNORE_UNUSED IS DISABLED:\n"); ++ for(i = 0; i < priv->nb_clk_refs; i++) { ++ struct clk *clk = priv->clk_refs[i]; ++ ++ if (clk == NULL) ++ continue; ++ ++ /* if counter > 0 */ ++ if (clk_is_enabled(clk)) ++ continue; ++ ++ if (priv->is_ignore_unused && priv->is_ignore_unused(clk)) ++ continue; ++ ++ if (clk->ops->is_enabled && clk->ops->is_enabled(clk) && ++ clk->ops->disable) { ++ printf("\t%s, EN = %d COUNTER = %d\n", clk_get_name(clk), ++ clk->ops->is_enabled(clk), ++ clk->enabled_count.val); ++ } ++ } ++ printf("\n"); ++} ++#endif ++ ++void clk_stm32_clock_ignore_unused(void) ++{ ++ struct clk_stm32_priv *priv = clk_stm32_get_priv(); ++ size_t i; ++ ++#ifdef CFG_STM32_CLK_DEBUG ++ clk_stm32_display_clock_ignore_unused(); ++#endif ++ ++ if (priv->clk_ignore_unused) ++ return; ++ ++ for(i = 0; i < priv->nb_clk_refs; i++) { ++ struct clk *clk = priv->clk_refs[i]; ++ ++ if (clk == NULL) ++ continue; ++ ++ /* if counter > 0 */ ++ if (clk_is_enabled(clk)) ++ continue; ++ ++ if (priv->is_ignore_unused && priv->is_ignore_unused(clk)) ++ continue; ++ ++ if (clk->ops->is_enabled && clk->ops->is_enabled(clk) && ++ clk->ops->disable) { ++ DMSG("%s: disabling %s ...\n", __func__, ++ clk_get_name(clk)); ++ clk->ops->disable(clk); ++ } ++ } ++} ++ ++void stm32mp_clk_provider_probe_final(const void *fdt, int node, ++ struct clk_stm32_priv *priv) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ ++ clk_stm32_register_clocks(priv); ++ ++ res = clk_dt_register_clk_provider(fdt, node, stm32mp_clk_dt_get_clk, ++ priv); ++ if (res) ++ panic("Couldn't register clock provider"); ++} +diff --git a/core/drivers/clk/clk-stm32-core.h b/core/drivers/clk/clk-stm32-core.h +new file mode 100644 +index 000000000..c05beebdc +--- /dev/null ++++ b/core/drivers/clk/clk-stm32-core.h +@@ -0,0 +1,272 @@ ++/* SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) */ ++/* ++ * Copyright (C) 2018-2022, STMicroelectronics - All Rights Reserved ++ */ ++ ++#ifndef CLK_STM32_CORE_H ++#define CLK_STM32_CORE_H ++ ++#include ++ ++struct mux_cfg { ++ uint16_t offset; ++ uint8_t shift; ++ uint8_t width; ++ uint8_t ready; ++}; ++ ++struct gate_cfg { ++ uint16_t offset; ++ uint8_t bit_idx; ++ uint8_t set_clr; ++}; ++ ++struct div_table_cfg { ++ unsigned int val; ++ unsigned int div; ++}; ++ ++struct div_cfg { ++ uint16_t offset; ++ uint8_t shift; ++ uint8_t width; ++ uint8_t flags; ++ uint8_t ready; ++ const struct div_table_cfg *table; ++}; ++ ++struct clk_stm32_priv { ++ uintptr_t base; ++ size_t nb_clk_refs; ++ struct clk **clk_refs; ++ const struct mux_cfg *muxes; ++ const uint32_t nb_muxes; ++ const struct gate_cfg *gates; ++ const uint32_t nb_gates; ++ const struct div_cfg *div; ++ const uint32_t nb_div; ++ bool clk_ignore_unused; ++ bool (*is_ignore_unused)(struct clk *clk); ++ bool (*is_critical)(struct clk *clk); ++ void *pdata; ++}; ++ ++struct clk_fixed_rate_cfg { ++ unsigned long rate; ++}; ++ ++struct fixed_factor_cfg { ++ unsigned int mult; ++ unsigned int div; ++}; ++ ++struct clk_gate_cfg { ++ uint32_t offset; ++ uint8_t bit_idx; ++}; ++ ++struct clk_stm32_mux_cfg { ++ int mux_id; ++}; ++ ++struct clk_stm32_gate_cfg { ++ int gate_id; ++}; ++ ++struct clk_stm32_div_cfg { ++ int div_id; ++}; ++ ++struct clk_stm32_composite_cfg { ++ int gate_id; ++ int div_id; ++ int mux_id; ++}; ++ ++struct clk_stm32_timer_cfg { ++ uint32_t apbdiv; ++ uint32_t timpre; ++}; ++ ++struct clk_stm32_gate_ready_cfg { ++ int gate_id; ++ int gate_rdy_id; ++}; ++ ++/* Define for divider clocks */ ++#define CLK_DIVIDER_ONE_BASED BIT(0) ++#define CLK_DIVIDER_POWER_OF_TWO BIT(1) ++#define CLK_DIVIDER_ALLOW_ZERO BIT(2) ++#define CLK_DIVIDER_HIWORD_MASK BIT(3) ++#define CLK_DIVIDER_ROUND_CLOSEST BIT(4) ++#define CLK_DIVIDER_READ_ONLY BIT(5) ++#define CLK_DIVIDER_MAX_AT_ZERO BIT(6) ++#define CLK_DIVIDER_BIG_ENDIAN BIT(7) ++ ++#define DIV_NO_RDY UINT8_MAX ++#define MUX_NO_RDY UINT8_MAX ++ ++#define MASK_WIDTH_SHIFT(_width, _shift) \ ++ GENMASK_32(((_width) + (_shift) - 1U), _shift) ++ ++/* Define for composite clocks */ ++#define NO_MUX INT32_MAX ++#define NO_DIV INT32_MAX ++#define NO_GATE INT32_MAX ++ ++int clk_stm32_enable_gate(uint16_t gate_id); ++void clk_stm32_disable_gate(uint16_t gate_id); ++void clk_stm32_endisable_gate(uint16_t gate_id, bool enable); ++bool clk_stm32_is_enabled_gate(uint16_t gate_id); ++int clk_stm32_gate_ready_endisable(uint16_t gate_id, bool enable, ++ bool wait_rdy); ++int clk_stm32_wait_ready_gate(uint16_t gate_id, bool ready_on); ++int clk_stm32_gate_rdy_enable(uint16_t gate_id); ++int clk_stm32_gate_rdy_disable(uint16_t gate_id); ++int clk_stm32_get_parent_mux(uint32_t mux_id); ++int clk_stm32_set_parent_mux(uint16_t pid, uint8_t sel); ++int clk_stm32_set_rate_divider(int div_id, unsigned long rate, ++ unsigned long prate); ++ ++ ++uint32_t clk_stm32_div_get_value(int div_id); ++int clk_stm32_set_div_value(uint32_t div_id, uint32_t value); ++unsigned long clk_stm32_get_rate_divider(int div_id, unsigned long prate); ++ ++int clk_stm32_parse_fdt_by_name(const void *fdt, int node, const char *name, ++ uint32_t *tab, uint32_t *nb); ++ ++ ++size_t clk_stm32_mux_get_parent(struct clk *clk); ++TEE_Result clk_stm32_mux_set_parent(struct clk *clk, size_t pidx); ++ ++unsigned long clk_stm32_divider_get_rate(struct clk *clk, ++ unsigned long parent_rate); ++ ++TEE_Result clk_stm32_divider_set_rate(struct clk *clk, ++ unsigned long rate, ++ unsigned long parent_rate); ++ ++size_t clk_stm32_composite_get_parent(struct clk *clk); ++TEE_Result clk_stm32_composite_set_parent(struct clk *clk, size_t pidx); ++unsigned long clk_stm32_composite_get_rate(struct clk *clk, ++ unsigned long parent_rate); ++TEE_Result clk_stm32_composite_set_rate(struct clk *clk, unsigned long rate, ++ unsigned long parent_rate); ++TEE_Result clk_stm32_composite_gate_enable(struct clk *clk); ++void clk_stm32_composite_gate_disable(struct clk *clk); ++bool clk_stm32_composite_gate_is_enabled(struct clk *clk); ++ ++TEE_Result clk_stm32_set_parent_by_index(struct clk *clk, size_t pidx); ++ ++unsigned long fixed_factor_get_rate(struct clk *clk, unsigned long parent_rate); ++ ++ ++extern const struct clk_ops clk_fixed_factor_ops; ++extern const struct clk_ops clk_fixed_clk_ops; ++extern const struct clk_ops clk_stm32_gate_ops; ++extern const struct clk_ops clk_stm32_gate_ready_ops; ++extern const struct clk_ops clk_stm32_divider_ops; ++extern const struct clk_ops clk_stm32_mux_ops; ++extern const struct clk_ops clk_stm32_composite_ops; ++ ++#define PARENT(x...) { x } ++ ++#define STM32_FIXED_RATE(_name, _rate)\ ++ struct clk _name = {\ ++ .ops = &clk_fixed_clk_ops,\ ++ .priv = &(struct clk_fixed_rate_cfg) {\ ++ .rate = (_rate),\ ++ },\ ++ .name = #_name,\ ++ .flags = 0,\ ++ .num_parents = 0,\ ++} ++ ++#define STM32_FIXED_FACTOR(_name, _parent, _flags, _mult_, _div)\ ++ struct clk _name = {\ ++ .ops = &clk_fixed_factor_ops,\ ++ .priv = &(struct fixed_factor_cfg) {\ ++ .mult = _mult_,\ ++ .div = _div,\ ++ },\ ++ .name = #_name,\ ++ .flags = (_flags),\ ++ .num_parents = 1,\ ++ .parents = { _parent },\ ++} ++ ++#define STM32_GATE(_name, _parent, _flags, _gate_id)\ ++struct clk _name = {\ ++ .ops = &clk_stm32_gate_ops,\ ++ .priv = &(struct clk_stm32_gate_cfg) {\ ++ .gate_id = _gate_id,\ ++ },\ ++ .name = #_name,\ ++ .flags = (_flags),\ ++ .num_parents = 1,\ ++ .parents = { _parent },\ ++} ++ ++#define STM32_DIVIDER(_name, _parent, _flags, _div_id)\ ++struct clk _name = {\ ++ .ops = &clk_stm32_divider_ops,\ ++ .priv = &(struct clk_stm32_div_cfg) {\ ++ .div_id = (_div_id),\ ++ },\ ++ .name = #_name,\ ++ .flags = (_flags),\ ++ .num_parents = 1,\ ++ .parents = { _parent },\ ++} ++ ++#define STM32_MUX(_name, _nb_parents, _parents, _flags, _mux_id)\ ++struct clk _name = {\ ++ .ops = &clk_stm32_mux_ops,\ ++ .priv = &(struct clk_stm32_mux_cfg) {\ ++ .mux_id = (_mux_id),\ ++ },\ ++ .name = #_name,\ ++ .flags = (_flags),\ ++ .num_parents = (_nb_parents),\ ++ .parents = _parents ,\ ++} ++ ++#define STM32_GATE_READY(_name, _parent, _flags, _gate_id)\ ++struct clk _name = {\ ++ .ops = &clk_stm32_gate_ready_ops,\ ++ .priv = &(struct clk_stm32_gate_cfg) {\ ++ .gate_id = _gate_id,\ ++ },\ ++ .name = #_name,\ ++ .flags = (_flags),\ ++ .num_parents = 1,\ ++ .parents = { _parent },\ ++} ++ ++#define STM32_COMPOSITE(_name, _nb_parents, _parents, _flags,\ ++ _gate_id, _div_id, _mux_id)\ ++ struct clk _name = {\ ++ .ops = &clk_stm32_composite_ops,\ ++ .priv = &(struct clk_stm32_composite_cfg) {\ ++ .gate_id = (_gate_id),\ ++ .div_id = (_div_id),\ ++ .mux_id = (_mux_id),\ ++ },\ ++ .name = #_name,\ ++ .flags = (_flags),\ ++ .num_parents = (_nb_parents),\ ++ .parents = _parents ,\ ++} ++ ++struct clk_stm32_priv *clk_stm32_get_priv(void); ++uintptr_t clk_stm32_get_rcc_base(void); ++ ++int clk_stm32_init(struct clk_stm32_priv *priv, uintptr_t base); ++ ++void stm32mp_clk_provider_probe_final(const void *fdt, int node, ++ struct clk_stm32_priv *priv); ++ ++void clk_stm32_clock_ignore_unused(void); ++ ++#endif /* CLK_STM32_CORE_H */ +diff --git a/core/drivers/clk/clk-stm32mp13.c b/core/drivers/clk/clk-stm32mp13.c +new file mode 100644 +index 000000000..9fcee96fd +--- /dev/null ++++ b/core/drivers/clk/clk-stm32mp13.c +@@ -0,0 +1,3437 @@ ++// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) ++/* ++ * Copyright (C) 2018-2021, STMicroelectronics - All Rights Reserved ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#include "clk-stm32-core.h" ++ ++#define MAX_HSI_HZ 64000000 ++#define USB_PHY_48_MHZ 48000000 ++ ++#define TIMEOUT_US_200MS U(200000) ++#define HSIDIV_TIMEOUT TIMEOUT_US_200MS ++ ++struct stm32_osci_dt_cfg { ++ unsigned long freq; ++ bool bypass; ++ bool digbyp; ++ bool css; ++ uint32_t drive; ++}; ++ ++enum pll_mn { ++ PLL_CFG_M, ++ PLL_CFG_N, ++ PLL_DIV_MN_NB ++}; ++ ++enum pll_pqr { ++ PLL_CFG_P, ++ PLL_CFG_Q, ++ PLL_CFG_R, ++ PLL_DIV_PQR_NB ++}; ++ ++enum pll_csg { ++ PLL_CSG_MOD_PER, ++ PLL_CSG_INC_STEP, ++ PLL_CSG_SSCG_MODE, ++ PLL_CSG_NB ++}; ++ ++struct stm32_pll_vco { ++ uint32_t status; ++ uint32_t src; ++ uint32_t div_mn[PLL_DIV_MN_NB]; ++ uint32_t frac; ++ bool csg_enabled; ++ uint32_t csg[PLL_CSG_NB]; ++}; ++ ++struct stm32_pll_output { ++ uint32_t output[PLL_DIV_PQR_NB]; ++}; ++ ++struct stm32_pll_dt_cfg { ++ struct stm32_pll_vco vco; ++ struct stm32_pll_output output; ++}; ++ ++struct stm32_clk_opp_cfg { ++ uint32_t frq; ++ uint32_t src; ++ uint32_t div; ++ struct stm32_pll_dt_cfg pll_cfg; ++}; ++ ++#define MAX_OPP 5 ++ ++struct stm32_clk_opp_dt_cfg { ++ struct stm32_clk_opp_cfg mpu_opp[MAX_OPP]; ++ struct stm32_clk_opp_cfg axi_opp[MAX_OPP]; ++ struct stm32_clk_opp_cfg mlahbs_opp[MAX_OPP]; ++}; ++ ++struct stm32_clk_platdata { ++ uintptr_t rcc_base; ++ uint32_t nosci; ++ struct stm32_osci_dt_cfg *osci; ++ uint32_t npll; ++ struct stm32_pll_dt_cfg *pll; ++ struct stm32_clk_opp_dt_cfg *opp; ++ uint32_t nclksrc; ++ uint32_t *clksrc; ++ uint32_t nclkdiv; ++ uint32_t *clkdiv; ++ struct stm32_pinctrl_list *pinctrl_cfg; ++}; ++ ++/* ++ * GATE CONFIG ++ */ ++ ++/* WARNING GATE_XXX_RDY MUST FOLOW GATE_XXX */ ++enum enum_gate_cfg { ++ GATE_LSE, ++ GATE_LSE_RDY, ++ GATE_LSI, ++ GATE_LSI_RDY, ++ GATE_HSI, ++ GATE_HSI_RDY, ++ GATE_CSI, ++ GATE_CSI_RDY, ++ GATE_HSE, ++ GATE_HSE_RDY, ++ GATE_PLL1, ++ GATE_PLL1_RDY, ++ GATE_PLL2, ++ GATE_PLL2_RDY, ++ GATE_PLL3, ++ GATE_PLL3_RDY, ++ GATE_PLL4, ++ GATE_PLL4_RDY, ++ GATE_HSIDIVRDY, ++ GATE_MPUSRCRDY, ++ GATE_AXISSRCRDY, ++ GATE_MCUSSRCRDY, ++ GATE_PLL12SRCRDY, ++ GATE_PLL3SRCRDY, ++ GATE_PLL4SRCRDY, ++ GATE_MPUDIVRDY, ++ GATE_AXIDIVRDY, ++ GATE_MLAHBDIVRDY, ++ GATE_APB1DIVRDY, ++ GATE_APB2DIVRDY, ++ GATE_APB3DIVRDY, ++ GATE_APB4DIVRDY, ++ GATE_APB5DIVRDY, ++ GATE_APB6DIVRDY, ++ GATE_RTCCK, ++ GATE_MCO1, ++ GATE_MCO2, ++ GATE_DBGCK, ++ GATE_TRACECK, ++ GATE_PLL1_DIVP, ++ GATE_PLL1_DIVQ, ++ GATE_PLL1_DIVR, ++ GATE_PLL2_DIVP, ++ GATE_PLL2_DIVQ, ++ GATE_PLL2_DIVR, ++ GATE_PLL3_DIVP, ++ GATE_PLL3_DIVQ, ++ GATE_PLL3_DIVR, ++ GATE_PLL4_DIVP, ++ GATE_PLL4_DIVQ, ++ GATE_PLL4_DIVR, ++ GATE_DDRC1, ++ GATE_DDRC1LP, ++ GATE_DDRPHYC, ++ GATE_DDRPHYCLP, ++ GATE_DDRCAPB, ++ GATE_DDRCAPBLP, ++ GATE_AXIDCG, ++ GATE_DDRPHYCAPB, ++ GATE_DDRPHYCAPBLP, ++ GATE_TIM2, ++ GATE_TIM3, ++ GATE_TIM4, ++ GATE_TIM5, ++ GATE_TIM6, ++ GATE_TIM7, ++ GATE_LPTIM1, ++ GATE_SPI2, ++ GATE_SPI3, ++ GATE_USART3, ++ GATE_UART4, ++ GATE_UART5, ++ GATE_UART7, ++ GATE_UART8, ++ GATE_I2C1, ++ GATE_I2C2, ++ GATE_SPDIF, ++ GATE_TIM1, ++ GATE_TIM8, ++ GATE_SPI1, ++ GATE_USART6, ++ GATE_SAI1, ++ GATE_SAI2, ++ GATE_DFSDM, ++ GATE_ADFSDM, ++ GATE_FDCAN, ++ GATE_LPTIM2, ++ GATE_LPTIM3, ++ GATE_LPTIM4, ++ GATE_LPTIM5, ++ GATE_VREF, ++ GATE_DTS, ++ GATE_PMBCTRL, ++ GATE_HDP, ++ GATE_SYSCFG, ++ GATE_DCMIPP, ++ GATE_DDRPERFM, ++ GATE_IWDG2APB, ++ GATE_USBPHY, ++ GATE_STGENRO, ++ GATE_LTDC, ++ GATE_RTCAPB, ++ GATE_TZC, ++ GATE_ETZPC, ++ GATE_IWDG1APB, ++ GATE_BSEC, ++ GATE_STGENC, ++ GATE_USART1, ++ GATE_USART2, ++ GATE_SPI4, ++ GATE_SPI5, ++ GATE_I2C3, ++ GATE_I2C4, ++ GATE_I2C5, ++ GATE_TIM12, ++ GATE_TIM13, ++ GATE_TIM14, ++ GATE_TIM15, ++ GATE_TIM16, ++ GATE_TIM17, ++ GATE_DMA1, ++ GATE_DMA2, ++ GATE_DMAMUX1, ++ GATE_DMA3, ++ GATE_DMAMUX2, ++ GATE_ADC1, ++ GATE_ADC2, ++ GATE_USBO, ++ GATE_TSC, ++ GATE_GPIOA, ++ GATE_GPIOB, ++ GATE_GPIOC, ++ GATE_GPIOD, ++ GATE_GPIOE, ++ GATE_GPIOF, ++ GATE_GPIOG, ++ GATE_GPIOH, ++ GATE_GPIOI, ++ GATE_PKA, ++ GATE_SAES, ++ GATE_CRYP1, ++ GATE_HASH1, ++ GATE_RNG1, ++ GATE_BKPSRAM, ++ GATE_AXIMC, ++ GATE_MCE, ++ GATE_ETH1CK, ++ GATE_ETH1TX, ++ GATE_ETH1RX, ++ GATE_ETH1MAC, ++ GATE_FMC, ++ GATE_QSPI, ++ GATE_SDMMC1, ++ GATE_SDMMC2, ++ GATE_CRC1, ++ GATE_USBH, ++ GATE_ETH2CK, ++ GATE_ETH2TX, ++ GATE_ETH2RX, ++ GATE_ETH2MAC, ++ GATE_MDMA, ++ GATE_NB ++}; ++ ++#define GATE_CFG(_id, _offset, _bit_idx, _offset_clr)\ ++ [(_id)] = {\ ++ .offset = (_offset),\ ++ .bit_idx = (_bit_idx),\ ++ .set_clr = (_offset_clr),\ ++ } ++ ++static const struct gate_cfg gates_mp13[GATE_NB] = { ++ GATE_CFG(GATE_LSE, RCC_BDCR, 0, 0), ++ GATE_CFG(GATE_LSE_RDY, RCC_BDCR, 2, 0), ++ GATE_CFG(GATE_RTCCK, RCC_BDCR, 20, 0), ++ GATE_CFG(GATE_LSI, RCC_RDLSICR, 0, 0), ++ GATE_CFG(GATE_LSI_RDY, RCC_RDLSICR, 1, 0), ++ GATE_CFG(GATE_HSI, RCC_OCENSETR, 0, 1), ++ GATE_CFG(GATE_HSI_RDY, RCC_OCRDYR, 0, 0), ++ GATE_CFG(GATE_CSI, RCC_OCENSETR, 4, 1), ++ GATE_CFG(GATE_CSI_RDY, RCC_OCRDYR, 4, 0), ++ GATE_CFG(GATE_HSE, RCC_OCENSETR, 8, 1), ++ GATE_CFG(GATE_HSE_RDY, RCC_OCRDYR, 8, 0), ++ GATE_CFG(GATE_HSIDIVRDY, RCC_OCRDYR, 2, 0), ++ GATE_CFG(GATE_MPUSRCRDY, RCC_MPCKSELR, 31, 0), ++ GATE_CFG(GATE_AXISSRCRDY, RCC_ASSCKSELR, 31, 0), ++ GATE_CFG(GATE_MCUSSRCRDY, RCC_MSSCKSELR, 31, 0), ++ GATE_CFG(GATE_PLL12SRCRDY, RCC_RCK12SELR, 31, 0), ++ GATE_CFG(GATE_PLL3SRCRDY, RCC_RCK3SELR, 31, 0), ++ GATE_CFG(GATE_PLL4SRCRDY, RCC_RCK4SELR, 31, 0), ++ GATE_CFG(GATE_MPUDIVRDY, RCC_MPCKDIVR, 31, 0), ++ GATE_CFG(GATE_AXIDIVRDY, RCC_AXIDIVR, 31, 0), ++ GATE_CFG(GATE_MLAHBDIVRDY, RCC_MLAHBDIVR, 31, 0), ++ GATE_CFG(GATE_APB1DIVRDY, RCC_APB1DIVR, 31, 0), ++ GATE_CFG(GATE_APB2DIVRDY, RCC_APB2DIVR, 31, 0), ++ GATE_CFG(GATE_APB3DIVRDY, RCC_APB3DIVR, 31, 0), ++ GATE_CFG(GATE_APB4DIVRDY, RCC_APB4DIVR, 31, 0), ++ GATE_CFG(GATE_APB5DIVRDY, RCC_APB5DIVR, 31, 0), ++ GATE_CFG(GATE_APB6DIVRDY, RCC_APB6DIVR, 31, 0), ++ GATE_CFG(GATE_MCO1, RCC_MCO1CFGR, 12, 0), ++ GATE_CFG(GATE_MCO2, RCC_MCO2CFGR, 12, 0), ++ GATE_CFG(GATE_DBGCK, RCC_DBGCFGR, 8, 0), ++ GATE_CFG(GATE_TRACECK, RCC_DBGCFGR, 9, 0), ++ GATE_CFG(GATE_PLL1, RCC_PLL1CR, 0, 0), ++ GATE_CFG(GATE_PLL1_RDY, RCC_PLL1CR, 1, 0), ++ GATE_CFG(GATE_PLL1_DIVP, RCC_PLL1CR, 4, 0), ++ GATE_CFG(GATE_PLL1_DIVQ, RCC_PLL1CR, 5, 0), ++ GATE_CFG(GATE_PLL1_DIVR, RCC_PLL1CR, 6, 0), ++ GATE_CFG(GATE_PLL2, RCC_PLL2CR, 0, 0), ++ GATE_CFG(GATE_PLL2_RDY, RCC_PLL2CR, 1, 0), ++ GATE_CFG(GATE_PLL2_DIVP, RCC_PLL2CR, 4, 0), ++ GATE_CFG(GATE_PLL2_DIVQ, RCC_PLL2CR, 5, 0), ++ GATE_CFG(GATE_PLL2_DIVR, RCC_PLL2CR, 6, 0), ++ GATE_CFG(GATE_PLL3, RCC_PLL3CR, 0, 0), ++ GATE_CFG(GATE_PLL3_RDY, RCC_PLL3CR, 1, 0), ++ GATE_CFG(GATE_PLL3_DIVP, RCC_PLL3CR, 4, 0), ++ GATE_CFG(GATE_PLL3_DIVQ, RCC_PLL3CR, 5, 0), ++ GATE_CFG(GATE_PLL3_DIVR, RCC_PLL3CR, 6, 0), ++ GATE_CFG(GATE_PLL4, RCC_PLL4CR, 0, 0), ++ GATE_CFG(GATE_PLL4_RDY, RCC_PLL4CR, 1, 0), ++ GATE_CFG(GATE_PLL4_DIVP, RCC_PLL4CR, 4, 0), ++ GATE_CFG(GATE_PLL4_DIVQ, RCC_PLL4CR, 5, 0), ++ GATE_CFG(GATE_PLL4_DIVR, RCC_PLL4CR, 6, 0), ++ GATE_CFG(GATE_DDRC1, RCC_DDRITFCR, 0, 0), ++ GATE_CFG(GATE_DDRC1LP, RCC_DDRITFCR, 1, 0), ++ GATE_CFG(GATE_DDRPHYC, RCC_DDRITFCR, 4, 0), ++ GATE_CFG(GATE_DDRPHYCLP, RCC_DDRITFCR, 5, 0), ++ GATE_CFG(GATE_DDRCAPB, RCC_DDRITFCR, 6, 0), ++ GATE_CFG(GATE_DDRCAPBLP, RCC_DDRITFCR, 7, 0), ++ GATE_CFG(GATE_AXIDCG, RCC_DDRITFCR, 8, 0), ++ GATE_CFG(GATE_DDRPHYCAPB, RCC_DDRITFCR, 9, 0), ++ GATE_CFG(GATE_DDRPHYCAPBLP, RCC_DDRITFCR, 10, 0), ++ GATE_CFG(GATE_TIM2, RCC_MP_APB1ENSETR, 0, 1), ++ GATE_CFG(GATE_TIM3, RCC_MP_APB1ENSETR, 1, 1), ++ GATE_CFG(GATE_TIM4, RCC_MP_APB1ENSETR, 2, 1), ++ GATE_CFG(GATE_TIM5, RCC_MP_APB1ENSETR, 3, 1), ++ GATE_CFG(GATE_TIM6, RCC_MP_APB1ENSETR, 4, 1), ++ GATE_CFG(GATE_TIM7, RCC_MP_APB1ENSETR, 5, 1), ++ GATE_CFG(GATE_LPTIM1, RCC_MP_APB1ENSETR, 9, 1), ++ GATE_CFG(GATE_SPI2, RCC_MP_APB1ENSETR, 11, 1), ++ GATE_CFG(GATE_SPI3, RCC_MP_APB1ENSETR, 12, 1), ++ GATE_CFG(GATE_USART3, RCC_MP_APB1ENSETR, 15, 1), ++ GATE_CFG(GATE_UART4, RCC_MP_APB1ENSETR, 16, 1), ++ GATE_CFG(GATE_UART5, RCC_MP_APB1ENSETR, 17, 1), ++ GATE_CFG(GATE_UART7, RCC_MP_APB1ENSETR, 18, 1), ++ GATE_CFG(GATE_UART8, RCC_MP_APB1ENSETR, 19, 1), ++ GATE_CFG(GATE_I2C1, RCC_MP_APB1ENSETR, 21, 1), ++ GATE_CFG(GATE_I2C2, RCC_MP_APB1ENSETR, 22, 1), ++ GATE_CFG(GATE_SPDIF, RCC_MP_APB1ENSETR, 26, 1), ++ GATE_CFG(GATE_TIM1, RCC_MP_APB2ENSETR, 0, 1), ++ GATE_CFG(GATE_TIM8, RCC_MP_APB2ENSETR, 1, 1), ++ GATE_CFG(GATE_SPI1, RCC_MP_APB2ENSETR, 8, 1), ++ GATE_CFG(GATE_USART6, RCC_MP_APB2ENSETR, 13, 1), ++ GATE_CFG(GATE_SAI1, RCC_MP_APB2ENSETR, 16, 1), ++ GATE_CFG(GATE_SAI2, RCC_MP_APB2ENSETR, 17, 1), ++ GATE_CFG(GATE_DFSDM, RCC_MP_APB2ENSETR, 20, 1), ++ GATE_CFG(GATE_ADFSDM, RCC_MP_APB2ENSETR, 21, 1), ++ GATE_CFG(GATE_FDCAN, RCC_MP_APB2ENSETR, 24, 1), ++ GATE_CFG(GATE_LPTIM2, RCC_MP_APB3ENSETR, 0, 1), ++ GATE_CFG(GATE_LPTIM3, RCC_MP_APB3ENSETR, 1, 1), ++ GATE_CFG(GATE_LPTIM4, RCC_MP_APB3ENSETR, 2, 1), ++ GATE_CFG(GATE_LPTIM5, RCC_MP_APB3ENSETR, 3, 1), ++ GATE_CFG(GATE_VREF, RCC_MP_APB3ENSETR, 13, 1), ++ GATE_CFG(GATE_DTS, RCC_MP_APB3ENSETR, 16, 1), ++ GATE_CFG(GATE_PMBCTRL, RCC_MP_APB3ENSETR, 17, 1), ++ GATE_CFG(GATE_HDP, RCC_MP_APB3ENSETR, 20, 1), ++ GATE_CFG(GATE_SYSCFG, RCC_MP_S_APB3ENSETR, 0, 1), ++ GATE_CFG(GATE_DCMIPP, RCC_MP_APB4ENSETR, 1, 1), ++ GATE_CFG(GATE_DDRPERFM, RCC_MP_APB4ENSETR, 8, 1), ++ GATE_CFG(GATE_IWDG2APB, RCC_MP_APB4ENSETR, 15, 1), ++ GATE_CFG(GATE_USBPHY, RCC_MP_APB4ENSETR, 16, 1), ++ GATE_CFG(GATE_STGENRO, RCC_MP_APB4ENSETR, 20, 1), ++ GATE_CFG(GATE_LTDC, RCC_MP_S_APB4ENSETR, 0, 1), ++ GATE_CFG(GATE_RTCAPB, RCC_MP_APB5ENSETR, 8, 1), ++ GATE_CFG(GATE_TZC, RCC_MP_APB5ENSETR, 11, 1), ++ GATE_CFG(GATE_ETZPC, RCC_MP_APB5ENSETR, 13, 1), ++ GATE_CFG(GATE_IWDG1APB, RCC_MP_APB5ENSETR, 15, 1), ++ GATE_CFG(GATE_BSEC, RCC_MP_APB5ENSETR, 16, 1), ++ GATE_CFG(GATE_STGENC, RCC_MP_APB5ENSETR, 20, 1), ++ GATE_CFG(GATE_USART1, RCC_MP_APB6ENSETR, 0, 1), ++ GATE_CFG(GATE_USART2, RCC_MP_APB6ENSETR, 1, 1), ++ GATE_CFG(GATE_SPI4, RCC_MP_APB6ENSETR, 2, 1), ++ GATE_CFG(GATE_SPI5, RCC_MP_APB6ENSETR, 3, 1), ++ GATE_CFG(GATE_I2C3, RCC_MP_APB6ENSETR, 4, 1), ++ GATE_CFG(GATE_I2C4, RCC_MP_APB6ENSETR, 5, 1), ++ GATE_CFG(GATE_I2C5, RCC_MP_APB6ENSETR, 6, 1), ++ GATE_CFG(GATE_TIM12, RCC_MP_APB6ENSETR, 7, 1), ++ GATE_CFG(GATE_TIM13, RCC_MP_APB6ENSETR, 8, 1), ++ GATE_CFG(GATE_TIM14, RCC_MP_APB6ENSETR, 9, 1), ++ GATE_CFG(GATE_TIM15, RCC_MP_APB6ENSETR, 10, 1), ++ GATE_CFG(GATE_TIM16, RCC_MP_APB6ENSETR, 11, 1), ++ GATE_CFG(GATE_TIM17, RCC_MP_APB6ENSETR, 12, 1), ++ GATE_CFG(GATE_DMA1, RCC_MP_AHB2ENSETR, 0, 1), ++ GATE_CFG(GATE_DMA2, RCC_MP_AHB2ENSETR, 1, 1), ++ GATE_CFG(GATE_DMAMUX1, RCC_MP_AHB2ENSETR, 2, 1), ++ GATE_CFG(GATE_DMA3, RCC_MP_AHB2ENSETR, 3, 1), ++ GATE_CFG(GATE_DMAMUX2, RCC_MP_AHB2ENSETR, 4, 1), ++ GATE_CFG(GATE_ADC1, RCC_MP_AHB2ENSETR, 5, 1), ++ GATE_CFG(GATE_ADC2, RCC_MP_AHB2ENSETR, 6, 1), ++ GATE_CFG(GATE_USBO, RCC_MP_AHB2ENSETR, 8, 1), ++ GATE_CFG(GATE_TSC, RCC_MP_AHB4ENSETR, 15, 1), ++ GATE_CFG(GATE_GPIOA, RCC_MP_S_AHB4ENSETR, 0, 1), ++ GATE_CFG(GATE_GPIOB, RCC_MP_S_AHB4ENSETR, 1, 1), ++ GATE_CFG(GATE_GPIOC, RCC_MP_S_AHB4ENSETR, 2, 1), ++ GATE_CFG(GATE_GPIOD, RCC_MP_S_AHB4ENSETR, 3, 1), ++ GATE_CFG(GATE_GPIOE, RCC_MP_S_AHB4ENSETR, 4, 1), ++ GATE_CFG(GATE_GPIOF, RCC_MP_S_AHB4ENSETR, 5, 1), ++ GATE_CFG(GATE_GPIOG, RCC_MP_S_AHB4ENSETR, 6, 1), ++ GATE_CFG(GATE_GPIOH, RCC_MP_S_AHB4ENSETR, 7, 1), ++ GATE_CFG(GATE_GPIOI, RCC_MP_S_AHB4ENSETR, 8, 1), ++ GATE_CFG(GATE_PKA, RCC_MP_AHB5ENSETR, 2, 1), ++ GATE_CFG(GATE_SAES, RCC_MP_AHB5ENSETR, 3, 1), ++ GATE_CFG(GATE_CRYP1, RCC_MP_AHB5ENSETR, 4, 1), ++ GATE_CFG(GATE_HASH1, RCC_MP_AHB5ENSETR, 5, 1), ++ GATE_CFG(GATE_RNG1, RCC_MP_AHB5ENSETR, 6, 1), ++ GATE_CFG(GATE_BKPSRAM, RCC_MP_AHB5ENSETR, 8, 1), ++ GATE_CFG(GATE_AXIMC, RCC_MP_AHB5ENSETR, 16, 1), ++ GATE_CFG(GATE_MCE, RCC_MP_AHB6ENSETR, 1, 1), ++ GATE_CFG(GATE_ETH1CK, RCC_MP_AHB6ENSETR, 7, 1), ++ GATE_CFG(GATE_ETH1TX, RCC_MP_AHB6ENSETR, 8, 1), ++ GATE_CFG(GATE_ETH1RX, RCC_MP_AHB6ENSETR, 9, 1), ++ GATE_CFG(GATE_ETH1MAC, RCC_MP_AHB6ENSETR, 10, 1), ++ GATE_CFG(GATE_FMC, RCC_MP_AHB6ENSETR, 12, 1), ++ GATE_CFG(GATE_QSPI, RCC_MP_AHB6ENSETR, 14, 1), ++ GATE_CFG(GATE_SDMMC1, RCC_MP_AHB6ENSETR, 16, 1), ++ GATE_CFG(GATE_SDMMC2, RCC_MP_AHB6ENSETR, 17, 1), ++ GATE_CFG(GATE_CRC1, RCC_MP_AHB6ENSETR, 20, 1), ++ GATE_CFG(GATE_USBH, RCC_MP_AHB6ENSETR, 24, 1), ++ GATE_CFG(GATE_ETH2CK, RCC_MP_AHB6ENSETR, 27, 1), ++ GATE_CFG(GATE_ETH2TX, RCC_MP_AHB6ENSETR, 28, 1), ++ GATE_CFG(GATE_ETH2RX, RCC_MP_AHB6ENSETR, 29, 1), ++ GATE_CFG(GATE_ETH2MAC, RCC_MP_AHB6ENSETR, 30, 1), ++ GATE_CFG(GATE_MDMA, RCC_MP_S_AHB6ENSETR, 0, 1), ++}; ++ ++/* ++ * MUX CONFIG ++ */ ++#define MUXRDY_CFG(_id, _offset, _shift, _witdh, _rdy)\ ++ [(_id)] = {\ ++ .offset = (_offset),\ ++ .shift = (_shift),\ ++ .width = (_witdh),\ ++ .ready = _rdy,\ ++ } ++ ++#define MUX_CFG(_id, _offset, _shift, _witdh)\ ++ MUXRDY_CFG(_id, _offset, _shift, _witdh, MUX_NO_RDY) ++ ++static const struct mux_cfg parent_mp13[MUX_NB] = { ++ MUXRDY_CFG(MUX_MPU, RCC_MPCKSELR, 0, 2, GATE_MPUSRCRDY), ++ MUXRDY_CFG(MUX_AXI, RCC_ASSCKSELR, 0, 3, GATE_AXISSRCRDY), ++ MUXRDY_CFG(MUX_MLAHB, RCC_MSSCKSELR, 0, 2, GATE_MCUSSRCRDY), ++ MUXRDY_CFG(MUX_PLL12, RCC_RCK12SELR, 0, 2, GATE_PLL12SRCRDY), ++ MUXRDY_CFG(MUX_PLL3, RCC_RCK3SELR, 0, 2, GATE_PLL3SRCRDY), ++ MUXRDY_CFG(MUX_PLL4, RCC_RCK4SELR, 0, 2, GATE_PLL4SRCRDY), ++ MUX_CFG(MUX_ADC1, RCC_ADC12CKSELR, 0, 2), ++ MUX_CFG(MUX_ADC2, RCC_ADC12CKSELR, 2, 2), ++ MUX_CFG(MUX_CKPER, RCC_CPERCKSELR, 0, 2), ++ MUX_CFG(MUX_DCMIPP, RCC_DCMIPPCKSELR, 0, 2), ++ MUX_CFG(MUX_ETH1, RCC_ETH12CKSELR, 0, 2), ++ MUX_CFG(MUX_ETH2, RCC_ETH12CKSELR, 8, 2), ++ MUX_CFG(MUX_FDCAN, RCC_FDCANCKSELR, 0, 2), ++ MUX_CFG(MUX_FMC, RCC_FMCCKSELR, 0, 2), ++ MUX_CFG(MUX_I2C12, RCC_I2C12CKSELR, 0, 3), ++ MUX_CFG(MUX_I2C3, RCC_I2C345CKSELR, 0, 3), ++ MUX_CFG(MUX_I2C4, RCC_I2C345CKSELR, 3, 3), ++ MUX_CFG(MUX_I2C5, RCC_I2C345CKSELR, 6, 3), ++ MUX_CFG(MUX_LPTIM1, RCC_LPTIM1CKSELR, 0, 3), ++ MUX_CFG(MUX_LPTIM2, RCC_LPTIM23CKSELR, 0, 3), ++ MUX_CFG(MUX_LPTIM3, RCC_LPTIM23CKSELR, 3, 3), ++ MUX_CFG(MUX_LPTIM45, RCC_LPTIM45CKSELR, 0, 3), ++ MUX_CFG(MUX_MCO1, RCC_MCO1CFGR, 0, 3), ++ MUX_CFG(MUX_MCO2, RCC_MCO2CFGR, 0, 3), ++ MUX_CFG(MUX_QSPI, RCC_QSPICKSELR, 0, 2), ++ MUX_CFG(MUX_RNG1, RCC_RNG1CKSELR, 0, 2), ++ MUX_CFG(MUX_RTC, RCC_BDCR, 16, 2), ++ MUX_CFG(MUX_SAES, RCC_SAESCKSELR, 0, 2), ++ MUX_CFG(MUX_SAI1, RCC_SAI1CKSELR, 0, 3), ++ MUX_CFG(MUX_SAI2, RCC_SAI2CKSELR, 0, 3), ++ MUX_CFG(MUX_SDMMC1, RCC_SDMMC12CKSELR, 0, 3), ++ MUX_CFG(MUX_SDMMC2, RCC_SDMMC12CKSELR, 3, 3), ++ MUX_CFG(MUX_SPDIF, RCC_SPDIFCKSELR, 0, 2), ++ MUX_CFG(MUX_SPI1, RCC_SPI2S1CKSELR, 0, 3), ++ MUX_CFG(MUX_SPI23, RCC_SPI2S23CKSELR, 0, 3), ++ MUX_CFG(MUX_SPI4, RCC_SPI45CKSELR, 0, 3), ++ MUX_CFG(MUX_SPI5, RCC_SPI45CKSELR, 3, 3), ++ MUX_CFG(MUX_STGEN, RCC_STGENCKSELR, 0, 2), ++ MUX_CFG(MUX_UART1, RCC_UART12CKSELR, 0, 3), ++ MUX_CFG(MUX_UART2, RCC_UART12CKSELR, 3, 3), ++ MUX_CFG(MUX_UART35, RCC_UART35CKSELR, 0, 3), ++ MUX_CFG(MUX_UART4, RCC_UART4CKSELR, 0, 3), ++ MUX_CFG(MUX_UART6, RCC_UART6CKSELR, 0, 3), ++ MUX_CFG(MUX_UART78, RCC_UART78CKSELR, 0, 3), ++ MUX_CFG(MUX_USBO, RCC_USBCKSELR, 4, 1), ++ MUX_CFG(MUX_USBPHY, RCC_USBCKSELR, 0, 2), ++}; ++ ++/* ++ * DIV CONFIG ++ */ ++static const struct div_table_cfg axi_div_table[] = { ++ { 0, 1 }, { 1, 2 }, { 2, 3 }, { 3, 4 }, ++ { 4, 4 }, { 5, 4 }, { 6, 4 }, { 7, 4 }, ++ { 0 }, ++}; ++ ++static const struct div_table_cfg mlahb_div_table[] = { ++ { 0, 1 }, { 1, 2 }, { 2, 4 }, { 3, 8 }, ++ { 4, 16 }, { 5, 32 }, { 6, 64 }, { 7, 128 }, ++ { 8, 256 }, { 9, 512 }, { 10, 512}, { 11, 512 }, ++ { 12, 512 }, { 13, 512 }, { 14, 512}, { 15, 512 }, ++ { 0 }, ++}; ++ ++static const struct div_table_cfg apb_div_table[] = { ++ { 0, 1 }, { 1, 2 }, { 2, 4 }, { 3, 8 }, ++ { 4, 16 }, { 5, 16 }, { 6, 16 }, { 7, 16 }, ++ { 0 }, ++}; ++ ++#define DIVRDY_CFG(_id, _offset, _shift, _width, _flags, _table, _ready)\ ++ [(_id)] = {\ ++ .offset = (_offset),\ ++ .shift = (_shift),\ ++ .width = (_width),\ ++ .flags = (_flags),\ ++ .table = (_table),\ ++ .ready = (_ready),\ ++ } ++ ++#define DIV_CFG(_id, _offset, _shift, _width, _flags, _table)\ ++ DIVRDY_CFG(_id, _offset, _shift, _width, _flags, _table, DIV_NO_RDY) ++ ++static const struct div_cfg dividers_mp13[] = { ++ DIVRDY_CFG(DIV_MPU, RCC_MPCKDIVR, 0, 4, 0, NULL, ++ GATE_MPUDIVRDY), ++ DIVRDY_CFG(DIV_AXI, RCC_AXIDIVR, 0, 3, 0, axi_div_table, ++ GATE_AXIDIVRDY), ++ DIVRDY_CFG(DIV_MLAHB, RCC_MLAHBDIVR, 0, 4, 0, mlahb_div_table, ++ GATE_MLAHBDIVRDY), ++ DIVRDY_CFG(DIV_APB1, RCC_APB1DIVR, 0, 3, 0, apb_div_table, ++ GATE_APB1DIVRDY), ++ DIVRDY_CFG(DIV_APB2, RCC_APB2DIVR, 0, 3, 0, apb_div_table, ++ GATE_APB2DIVRDY), ++ DIVRDY_CFG(DIV_APB3, RCC_APB3DIVR, 0, 3, 0, apb_div_table, ++ GATE_APB3DIVRDY), ++ DIVRDY_CFG(DIV_APB4, RCC_APB4DIVR, 0, 3, 0, apb_div_table, ++ GATE_APB4DIVRDY), ++ DIVRDY_CFG(DIV_APB5, RCC_APB5DIVR, 0, 3, 0, apb_div_table, ++ GATE_APB5DIVRDY), ++ DIVRDY_CFG(DIV_APB6, RCC_APB6DIVR, 0, 3, 0, apb_div_table, ++ GATE_APB6DIVRDY), ++ DIVRDY_CFG(DIV_HSI, RCC_HSICFGR, 0, 2, CLK_DIVIDER_POWER_OF_TWO, NULL, ++ GATE_HSIDIVRDY), ++ DIV_CFG(DIV_PLL1DIVP, RCC_PLL1CFGR2, 0, 7, 0, NULL), ++ DIV_CFG(DIV_PLL2DIVP, RCC_PLL2CFGR2, 0, 7, 0, NULL), ++ DIV_CFG(DIV_PLL2DIVQ, RCC_PLL2CFGR2, 8, 7, 0, NULL), ++ DIV_CFG(DIV_PLL2DIVR, RCC_PLL2CFGR2, 16, 7, 0, NULL), ++ DIV_CFG(DIV_PLL3DIVP, RCC_PLL3CFGR2, 0, 7, 0, NULL), ++ DIV_CFG(DIV_PLL3DIVQ, RCC_PLL3CFGR2, 8, 7, 0, NULL), ++ DIV_CFG(DIV_PLL3DIVR, RCC_PLL3CFGR2, 16, 7, 0, NULL), ++ DIV_CFG(DIV_PLL4DIVP, RCC_PLL4CFGR2, 0, 7, 0, NULL), ++ DIV_CFG(DIV_PLL4DIVQ, RCC_PLL4CFGR2, 8, 7, 0, NULL), ++ DIV_CFG(DIV_PLL4DIVR, RCC_PLL4CFGR2, 16, 7, 0, NULL), ++ DIV_CFG(DIV_RTC, RCC_RTCDIVR, 0, 6, 0, NULL), ++ DIV_CFG(DIV_MCO1, RCC_MCO1CFGR, 4, 4, 0, NULL), ++ DIV_CFG(DIV_MCO2, RCC_MCO2CFGR, 4, 4, 0, NULL), ++ DIV_CFG(DIV_TRACE, RCC_DBGCFGR, 0, 3, CLK_DIVIDER_POWER_OF_TWO, NULL), ++ DIV_CFG(DIV_ETH1PTP, RCC_ETH12CKSELR, 4, 4, 0, NULL), ++ DIV_CFG(DIV_ETH2PTP, RCC_ETH12CKSELR, 12, 4, 0, NULL), ++}; ++ ++enum stm32_osc { ++ OSC_HSI, ++ OSC_HSE, ++ OSC_CSI, ++ OSC_LSI, ++ OSC_LSE, ++ NB_OSCILLATOR ++}; ++ ++struct stm32_osc_cfg { ++ int osc_id; ++}; ++ ++struct clk_stm32_bypass { ++ uint16_t offset; ++ uint8_t bit_byp; ++ uint8_t bit_digbyp; ++}; ++ ++struct clk_stm32_css { ++ uint16_t offset; ++ uint8_t bit_css; ++}; ++ ++struct clk_stm32_drive { ++ uint16_t offset; ++ uint8_t drv_shift; ++ uint8_t drv_width; ++ uint8_t drv_default; ++}; ++ ++struct clk_oscillator_data { ++ const char *name; ++ unsigned long frequency; ++ uint16_t gate_id; ++ struct clk_stm32_bypass *bypass; ++ struct clk_stm32_css *css; ++ struct clk_stm32_drive *drive; ++}; ++ ++#define BYPASS(_offset, _bit_byp, _bit_digbyp) &(struct clk_stm32_bypass){\ ++ .offset = (_offset),\ ++ .bit_byp = (_bit_byp),\ ++ .bit_digbyp = (_bit_digbyp),\ ++} ++ ++#define CSS(_offset, _bit_css) &(struct clk_stm32_css){\ ++ .offset = (_offset),\ ++ .bit_css = (_bit_css),\ ++} ++ ++#define DRIVE(_offset, _shift, _width, _default) &(struct clk_stm32_drive){\ ++ .offset = (_offset),\ ++ .drv_shift = (_shift),\ ++ .drv_width = (_width),\ ++ .drv_default = (_default),\ ++} ++ ++#define OSCILLATOR(idx_osc, _name, _gate_id, _bypass, _css, _drive) \ ++ [(idx_osc)] = (struct clk_oscillator_data){\ ++ .name = (_name),\ ++ .gate_id = (_gate_id),\ ++ .bypass = (_bypass),\ ++ .css = (_css),\ ++ .drive = (_drive),\ ++ } ++ ++static struct clk_oscillator_data stm32mp13_osc_data[NB_OSCILLATOR] = { ++ OSCILLATOR(OSC_HSI, "clk-hsi", GATE_HSI, ++ NULL, NULL, NULL), ++ ++ OSCILLATOR(OSC_LSI, "clk-lsi", GATE_LSI, ++ NULL, NULL, NULL), ++ ++ OSCILLATOR(OSC_CSI, "clk-csi", GATE_CSI, ++ NULL, NULL, NULL), ++ ++ OSCILLATOR(OSC_LSE, "clk-lse", GATE_LSE, ++ BYPASS(RCC_BDCR, 1, 3), ++ CSS(RCC_BDCR, 8), ++ DRIVE(RCC_BDCR, 4, 2, 2)), ++ ++ OSCILLATOR(OSC_HSE, "clk-hse", GATE_HSE, ++ BYPASS(RCC_OCENSETR, 10, 7), ++ CSS(RCC_OCENSETR, 11), ++ NULL), ++}; ++ ++static inline struct clk_oscillator_data *clk_oscillator_get_data(int osc_id) ++{ ++ return &stm32mp13_osc_data[osc_id]; ++} ++ ++static unsigned long clk_stm32_get_rate_oscillateur(int osc_id) ++{ ++ struct clk_stm32_priv *priv = clk_stm32_get_priv(); ++ struct stm32_clk_platdata *pdata = priv->pdata; ++ struct stm32_osci_dt_cfg *osci = &pdata->osci[osc_id]; ++ ++ return osci->freq; ++} ++ ++static void clk_oscillator_set_bypass(struct clk_stm32_priv *priv, ++ struct clk_oscillator_data *osc_data, ++ bool digbyp, bool bypass) ++{ ++ struct clk_stm32_bypass *bypass_data = osc_data->bypass; ++ uintptr_t address; ++ ++ if (bypass_data == NULL) ++ return; ++ ++ address = priv->base + bypass_data->offset; ++ ++ if (digbyp) ++ io_setbits32(address, BIT(bypass_data->bit_digbyp)); ++ ++ if (bypass || digbyp) ++ io_setbits32(address, BIT(bypass_data->bit_byp)); ++} ++ ++static void clk_oscillator_set_css(struct clk_stm32_priv *priv, ++ struct clk_oscillator_data *osc_data, ++ bool css) ++{ ++ struct clk_stm32_css *css_data = osc_data->css; ++ uintptr_t address; ++ ++ if (css_data == NULL) ++ return; ++ ++ address = priv->base + css_data->offset; ++ ++ if (css) ++ io_setbits32(address, BIT(css_data->bit_css)); ++} ++ ++static void clk_oscillator_set_drive(struct clk_stm32_priv *priv, ++ struct clk_oscillator_data *osc_data, ++ uint8_t lsedrv) ++{ ++ struct clk_stm32_drive *drive_data = osc_data->drive; ++ uintptr_t address; ++ uint32_t mask; ++ uint32_t value; ++ ++ if (drive_data == NULL) ++ return; ++ ++ address = priv->base + drive_data->offset; ++ ++ mask = (BIT(drive_data->drv_width) - 1U) << drive_data->drv_shift; ++ ++ /* ++ * Warning: not recommended to switch directly from "high drive" ++ * to "medium low drive", and vice-versa. ++ */ ++ value = (io_read32(address) & mask) >> drive_data->drv_shift; ++ ++ while (value != lsedrv) { ++ if (value > lsedrv) ++ value--; ++ else ++ value++; ++ ++ io_clrsetbits32(address, mask, value << drive_data->drv_shift); ++ } ++} ++ ++static void stm32_enable_oscillator_hse(struct clk_stm32_priv *priv, ++ struct stm32_clk_platdata *pdata) ++{ ++ struct clk_oscillator_data *osc_data = clk_oscillator_get_data(OSC_HSE); ++ struct stm32_osci_dt_cfg *osci = &pdata->osci[OSC_HSE]; ++ ++ if (osci->freq == 0U) ++ return; ++ ++ clk_oscillator_set_bypass(priv, osc_data, osci->digbyp, osci->bypass); ++ ++ /* Enable clock and wait ready bit */ ++ clk_stm32_gate_ready_endisable(osc_data->gate_id, true, true); ++ ++ clk_oscillator_set_css(priv, osc_data, osci->css); ++} ++ ++static void stm32_enable_oscillator_lse(struct clk_stm32_priv *priv, ++ struct stm32_clk_platdata *pdata) ++{ ++ struct clk_oscillator_data *osc_data = clk_oscillator_get_data(OSC_LSE); ++ struct stm32_osci_dt_cfg *osci = &pdata->osci[OSC_LSE]; ++ ++ if (osci->freq == 0U) ++ return; ++ ++ if (clk_stm32_is_enabled_gate(osc_data->gate_id)) ++ return; ++ ++ clk_oscillator_set_bypass(priv, osc_data, osci->digbyp, osci->bypass); ++ ++ clk_oscillator_set_drive(priv, osc_data, osci->drive); ++ ++ /* Enable lse clock, but don't wait ready bit */ ++ clk_stm32_gate_ready_endisable(osc_data->gate_id, true, false); ++} ++ ++static void stm32_enable_oscillator_lsi(__maybe_unused struct clk_stm32_priv *priv, ++ struct stm32_clk_platdata *pdata) ++{ ++ struct clk_oscillator_data *osc_data = clk_oscillator_get_data(OSC_LSI); ++ struct stm32_osci_dt_cfg *osci = &pdata->osci[OSC_LSI]; ++ ++ if (osci->freq == 0U) ++ return; ++ ++ /* Enable clock and wait ready bit */ ++ clk_stm32_gate_ready_endisable(osc_data->gate_id, true, true); ++} ++ ++static void stm32_enable_oscillator_csi(__maybe_unused struct clk_stm32_priv *priv, ++ struct stm32_clk_platdata *pdata) ++{ ++ struct clk_oscillator_data *osc_data = clk_oscillator_get_data(OSC_CSI); ++ struct stm32_osci_dt_cfg *osci = &pdata->osci[OSC_CSI]; ++ ++ if (osci->freq == 0U) ++ return; ++ ++ /* Enable clock and wait ready bit */ ++ clk_stm32_gate_ready_endisable(osc_data->gate_id, true, true); ++} ++ ++static int stm32_clk_oscillators_lse_set_css(struct clk_stm32_priv *priv, ++ struct stm32_clk_platdata *pdata) ++ ++{ ++ struct clk_oscillator_data *osc_data = clk_oscillator_get_data(OSC_LSE); ++ struct stm32_osci_dt_cfg *osci = &pdata->osci[OSC_LSE]; ++ ++ clk_oscillator_set_css(priv, osc_data, osci->css); ++ ++ return 0; ++} ++ ++static int stm32_clk_oscillators_wait_lse_ready(__maybe_unused struct clk_stm32_priv *priv, ++ struct stm32_clk_platdata *pdata) ++{ ++ struct clk_oscillator_data *osc_data = clk_oscillator_get_data(OSC_LSE); ++ struct stm32_osci_dt_cfg *osci = &pdata->osci[OSC_LSE]; ++ int ret = 0; ++ ++ if (osci->freq != 0U) ++ ret = clk_stm32_wait_ready_gate(osc_data->gate_id, true); ++ ++ return ret; ++} ++ ++static void stm32_clk_oscillators_enable(struct clk_stm32_priv *priv, ++ struct stm32_clk_platdata *pdata) ++{ ++ stm32_enable_oscillator_hse(priv, pdata); ++ stm32_enable_oscillator_lse(priv, pdata); ++ stm32_enable_oscillator_lsi(priv, pdata); ++ stm32_enable_oscillator_csi(priv, pdata); ++} ++ ++enum stm32_pll_id { ++ PLL1_ID, ++ PLL2_ID, ++ PLL3_ID, ++ PLL4_ID, ++ PLL_NB ++}; ++ ++enum stm32mp1_plltype { ++ PLL_800, ++ PLL_1600, ++ PLL_2000, ++ PLL_TYPE_NB ++}; ++ ++#define RCC_OFFSET_PLLXCR 0 ++#define RCC_OFFSET_PLLXCFGR1 4 ++#define RCC_OFFSET_PLLXCFGR2 8 ++#define RCC_OFFSET_PLLXFRACR 12 ++#define RCC_OFFSET_PLLXCSGR 16 ++ ++struct stm32_clk_pll { ++ enum stm32mp1_plltype plltype; ++ uint16_t gate_id; ++ uint16_t mux_id; ++ uint16_t reg_pllxcr; ++}; ++ ++struct stm32mp1_pll { ++ uint8_t refclk_min; ++ uint8_t refclk_max; ++}; ++ ++/* Define characteristic of PLL according type */ ++static const struct stm32mp1_pll stm32mp1_pll[PLL_TYPE_NB] = { ++ [PLL_800] = { ++ .refclk_min = 4, ++ .refclk_max = 16, ++ }, ++ [PLL_1600] = { ++ .refclk_min = 8, ++ .refclk_max = 16, ++ }, ++ [PLL_2000] = { ++ .refclk_min = 8, ++ .refclk_max = 16, ++ }, ++}; ++ ++#define CLK_PLL_CFG(_idx, _type, _gate_id, _mux_id, _reg)\ ++ [(_idx)] = {\ ++ .gate_id = (_gate_id),\ ++ .mux_id = (_mux_id),\ ++ .plltype = (_type),\ ++ .reg_pllxcr = (_reg),\ ++ } ++ ++static const struct stm32_clk_pll stm32_mp13_clk_pll[PLL_NB] = { ++ CLK_PLL_CFG(PLL1_ID, PLL_2000, GATE_PLL1, MUX_PLL12, RCC_PLL1CR), ++ CLK_PLL_CFG(PLL2_ID, PLL_1600, GATE_PLL2, MUX_PLL12, RCC_PLL2CR), ++ CLK_PLL_CFG(PLL3_ID, PLL_800, GATE_PLL3, MUX_PLL3, RCC_PLL3CR), ++ CLK_PLL_CFG(PLL4_ID, PLL_800, GATE_PLL4, MUX_PLL4, RCC_PLL4CR), ++}; ++ ++static const struct stm32_clk_pll *clk_stm32_pll_data(unsigned int idx) ++{ ++ return &stm32_mp13_clk_pll[idx]; ++} ++ ++/* Clock TREE configuration */ ++ ++static unsigned int stm32_clk_configure_clk_get_binding_id(uint32_t data) ++{ ++ return (data & CLK_ID_MASK) >> CLK_ID_SHIFT; ++} ++ ++static int stm32_clk_configure_clk(__maybe_unused struct clk_stm32_priv *priv, ++ uint32_t data) ++{ ++ int sel = (data & CLK_SEL_MASK) >> CLK_SEL_SHIFT; ++ int enable = (data & CLK_ON_MASK) >> CLK_ON_SHIFT; ++ struct clk *clk = NULL; ++ int clk_id = 0; ++ int ret = 0; ++ ++ clk_id = stm32_clk_configure_clk_get_binding_id(data); ++ if (clk_id < 0) ++ return clk_id; ++ ++ clk = stm32mp_rcc_clock_id_to_clk(clk_id); ++ if (clk == NULL) ++ panic(); ++ ++ ret = clk_stm32_set_parent_by_index(clk, sel); ++ if (ret != 0) ++ return ret; ++ ++ if (clk->ops) { ++ if (enable) ++ clk->ops->enable(clk); ++ else ++ clk->ops->disable(clk); ++ } ++ ++ return 0; ++} ++ ++static int stm32_clk_configure_mux(__maybe_unused struct clk_stm32_priv *priv, uint32_t data) ++{ ++ int mux = (data & MUX_ID_MASK) >> MUX_ID_SHIFT; ++ int sel = (data & MUX_SEL_MASK) >> MUX_SEL_SHIFT; ++ ++ if (mux == MUX_RTC) { ++ if (sel == 0) ++ return 0; ++ ++ if (clk_stm32_is_enabled_gate(GATE_RTCCK)) ++ return 0; ++ ++ return clk_stm32_set_parent_mux(mux, sel); ++ } ++ ++ return clk_stm32_set_parent_mux(mux, sel); ++} ++ ++static int stm32_clk_configure_div(__maybe_unused struct clk_stm32_priv *priv, ++ uint32_t data) ++{ ++ int div_id, div_n; ++ ++ div_id = (data & DIV_ID_MASK) >> DIV_ID_SHIFT; ++ div_n = (data & DIV_DIVN_MASK) >> DIV_DIVN_SHIFT; ++ ++ return clk_stm32_set_div_value(div_id, div_n); ++} ++ ++static int stm32_clk_dividers_configure(struct clk_stm32_priv *priv) ++{ ++ struct stm32_clk_platdata *pdata = priv->pdata; ++ uint32_t i = 0; ++ int ret = 0; ++ ++ for (i = 0; i < pdata->nclkdiv; i++) { ++ ret = stm32_clk_configure_div(priv, pdata->clkdiv[i]); ++ if (ret != 0) ++ return ret; ++ } ++ ++ return 0; ++} ++ ++static int stm32_clk_source_configure(struct clk_stm32_priv *priv) ++{ ++ struct stm32_clk_platdata *pdata = priv->pdata; ++ bool ckper_disabled = false; ++ int ret = 0; ++ size_t i = 0; ++ ++ for (i = 0; i < pdata->nclksrc; i++) { ++ uint32_t val = pdata->clksrc[i]; ++ uint32_t cmd = 0; ++ uint32_t cmd_data = 0; ++ ++ if (val == (uint32_t)CLK_CKPER_DISABLED) { ++ ckper_disabled = true; ++ continue; ++ } ++ ++ cmd = (val & CMD_MASK) >> CMD_SHIFT; ++ cmd_data = val & ~CMD_MASK; ++ ++ switch (cmd) { ++ case CMD_MUX: ++ ret = stm32_clk_configure_mux(priv, cmd_data); ++ break; ++ ++ case CMD_CLK: ++ ret = stm32_clk_configure_clk(priv, cmd_data); ++ break; ++ default: ++ ret = -1; ++ break; ++ } ++ ++ if (ret != 0) { ++ return ret; ++ } ++ } ++ ++ /* ++ * CKPER is source for some peripheral clocks ++ * (FMC-NAND / QPSI-NOR) and switching source is allowed ++ * only if previous clock is still ON ++ * => deactivate CKPER only after switching clock ++ */ ++ if (ckper_disabled) { ++ ret = stm32_clk_configure_mux(priv, CLK_CKPER_DISABLED & CMD_MASK); ++ if (ret != 0) { ++ return ret; ++ } ++ } ++ ++ return 0; ++} ++ ++static unsigned long clk_stm32_pll_get_oscillator_rate(int sel) ++{ ++ int osc[] = { OSC_HSI, OSC_HSE, OSC_CSI }; ++ ++ return clk_stm32_get_rate_oscillateur(osc[sel]); ++} ++ ++static int clk_stm32_pll_compute_cfgr1(const struct stm32_clk_pll *pll, ++ struct stm32_pll_vco *vco, ++ uint32_t *value) ++{ ++ int sel = (vco->src & MUX_SEL_MASK) >> MUX_SEL_SHIFT; ++ uint32_t divm = vco->div_mn[PLL_CFG_M]; ++ uint32_t divn = vco->div_mn[PLL_CFG_N]; ++ unsigned long refclk = 0UL; ++ ++ refclk = clk_stm32_pll_get_oscillator_rate(sel) / (divm + 1U); ++ ++ if ((refclk < (stm32mp1_pll[pll->plltype].refclk_min * 1000000U)) || ++ (refclk > (stm32mp1_pll[pll->plltype].refclk_max * 1000000U))) ++ return -1; ++ ++ *value = 0; ++ ++ if ((pll->plltype == PLL_800) && (refclk >= 8000000U)) ++ *value = 1U << RCC_PLLNCFGR1_IFRGE_SHIFT; ++ ++ *value |= (divn << RCC_PLLNCFGR1_DIVN_SHIFT) & RCC_PLLNCFGR1_DIVN_MASK; ++ *value |= (divm << RCC_PLLNCFGR1_DIVM_SHIFT) & RCC_PLLNCFGR1_DIVM_MASK; ++ ++ return 0; ++} ++ ++static uint32_t clk_stm32_pll_compute_cfgr2(struct stm32_pll_output *out) ++{ ++ uint32_t value = 0; ++ ++ value |= (out->output[PLL_CFG_P] << RCC_PLLNCFGR2_DIVP_SHIFT) & RCC_PLLNCFGR2_DIVP_MASK; ++ value |= (out->output[PLL_CFG_Q] << RCC_PLLNCFGR2_DIVQ_SHIFT) & RCC_PLLNCFGR2_DIVQ_MASK; ++ value |= (out->output[PLL_CFG_R] << RCC_PLLNCFGR2_DIVR_SHIFT) & RCC_PLLNCFGR2_DIVR_MASK; ++ ++ return value; ++} ++ ++/* ++ * Check if PLL1 can be configured on the fly. ++ * @result (-1) => config on the fly is not possible. ++ * (0) => config on the fly is possible. ++ * (+1) => same parameters, no need to reconfigure. ++ * Return value is 0 if no error. ++ */ ++ ++static int clk_stm32_is_pll_config_on_the_fly(struct clk_stm32_priv *priv, ++ const struct stm32_clk_pll *pll, ++ struct stm32_pll_dt_cfg *pll_conf, ++ int *result) ++{ ++ uintptr_t pll_base = priv->base + pll->reg_pllxcr; ++ struct stm32_pll_vco *vco = &pll_conf->vco; ++ struct stm32_pll_output *out = &pll_conf->output; ++ uint32_t fracr = 0; ++ uint32_t value = 0; ++ int ret = 0; ++ int sel = 0; ++ ++ ret = clk_stm32_pll_compute_cfgr1(pll, vco, &value); ++ if (ret != 0) { ++ return ret; ++ } ++ ++ sel = (vco->src & MUX_SEL_MASK) >> MUX_SEL_SHIFT; ++ if (sel != clk_stm32_get_parent_mux(pll->mux_id)) { ++ /* Clock source of the PLL is different */ ++ *result = -1; ++ return 0; ++ } ++ ++ if (io_read32(pll_base + RCC_OFFSET_PLLXCFGR1) != value) { ++ /* Different DIVN/DIVM, can't config on the fly */ ++ *result = -1; ++ return 0; ++ } ++ ++ *result = 1; ++ ++ fracr = vco->frac << RCC_PLLNFRACR_FRACV_SHIFT; ++ fracr |= RCC_PLLNCFGR1_DIVM_MASK; ++ value = clk_stm32_pll_compute_cfgr2(out); ++ ++ if ((io_read32(pll_base + RCC_OFFSET_PLLXFRACR) == fracr) && ++ (io_read32(pll_base + RCC_OFFSET_PLLXCFGR2) == value)) ++ /* Same parameters, no need to config */ ++ *result = 1; ++ else ++ *result = 0; ++ ++ return 0; ++} ++ ++static int stm32_clk_hsidiv_configure(struct clk_stm32_priv *priv) ++{ ++ struct stm32_clk_platdata *pdata = priv->pdata; ++ struct stm32_osci_dt_cfg *osci = &pdata->osci[OSC_HSI]; ++ ++ return clk_stm32_set_rate_divider(DIV_HSI, osci->freq, MAX_HSI_HZ); ++} ++ ++static void clk_stm32_pll_config_vco(struct clk_stm32_priv *priv, ++ const struct stm32_clk_pll *pll, ++ struct stm32_pll_vco *vco) ++{ ++ uintptr_t pll_base = priv->base + pll->reg_pllxcr; ++ uint32_t value = 0; ++ ++ if (clk_stm32_pll_compute_cfgr1(pll, vco, &value) != 0) { ++ EMSG("Invalid Vref clock !\n"); ++ panic(); ++ } ++ ++ /* Write N / M / IFREGE fields */ ++ io_write32(pll_base + RCC_OFFSET_PLLXCFGR1, value); ++ ++ /* Fractional configuration */ ++ io_write32(pll_base + RCC_OFFSET_PLLXFRACR, 0); ++ ++ /* Frac must be enabled only once its configuration is loaded */ ++ io_write32(pll_base + RCC_OFFSET_PLLXFRACR, ++ vco->frac << RCC_PLLNFRACR_FRACV_SHIFT); ++ ++ io_setbits32(pll_base + RCC_OFFSET_PLLXFRACR, RCC_PLLNFRACR_FRACLE); ++} ++ ++static void clk_stm32_pll_config_csg(struct clk_stm32_priv *priv, ++ const struct stm32_clk_pll *pll, ++ struct stm32_pll_vco *vco) ++{ ++ uintptr_t pll_base = priv->base + pll->reg_pllxcr; ++ uint32_t mod_per = 0; ++ uint32_t inc_step = 0; ++ uint32_t sscg_mode = 0; ++ uint32_t value = 0; ++ ++ if (!vco->csg_enabled) ++ return; ++ ++ mod_per = vco->csg[PLL_CSG_MOD_PER]; ++ inc_step = vco->csg[PLL_CSG_INC_STEP]; ++ sscg_mode = vco->csg[PLL_CSG_SSCG_MODE]; ++ ++ value |= (mod_per << RCC_PLLNCSGR_MOD_PER_SHIFT) & RCC_PLLNCSGR_MOD_PER_MASK; ++ value |= (inc_step << RCC_PLLNCSGR_INC_STEP_SHIFT) & RCC_PLLNCSGR_INC_STEP_MASK; ++ value |= (sscg_mode << RCC_PLLNCSGR_SSCG_MODE_SHIFT) & RCC_PLLNCSGR_SSCG_MODE_MASK; ++ ++ io_write32(pll_base + RCC_OFFSET_PLLXCSGR, value); ++ io_setbits32(pll_base+ RCC_OFFSET_PLLXCR, RCC_PLLNCR_SSCG_CTRL); ++} ++ ++static void clk_stm32_pll_config_out(struct clk_stm32_priv *priv, ++ const struct stm32_clk_pll *pll, ++ struct stm32_pll_output *out) ++{ ++ uintptr_t pll_base = priv->base + pll->reg_pllxcr; ++ uint32_t value = 0; ++ ++ value = clk_stm32_pll_compute_cfgr2(out); ++ ++ io_write32(pll_base + RCC_OFFSET_PLLXCFGR2, value); ++} ++ ++static inline struct stm32_pll_dt_cfg *clk_stm32_pll_get_pdata(int pll_idx) ++{ ++ struct clk_stm32_priv *priv = clk_stm32_get_priv(); ++ struct stm32_clk_platdata *pdata = priv->pdata; ++ ++ return &pdata->pll[pll_idx]; ++} ++ ++static int clk_stm32_pll_init_switch_to_hsi_clk_system(int mux_sys) ++{ ++ int sel = 0; ++ ++ if (mux_sys == -1) ++ return -1; ++ ++ /* Make a backup to the current parent */ ++ sel = clk_stm32_get_parent_mux(mux_sys); ++ ++ /* Switch to HSI */ ++ clk_stm32_set_parent_mux(mux_sys, 0); ++ ++ return sel; ++} ++ ++static void clk_stm32_pll_init_restore_clk_system(int mux_sys, int sel) ++{ ++ if (mux_sys != -1) ++ /* Restore the parent */ ++ clk_stm32_set_parent_mux(mux_sys, sel); ++} ++ ++static uint32_t clk_stm32_pll_backup_output_diven(const struct stm32_clk_pll *pll) ++{ ++ struct clk_stm32_priv *priv = clk_stm32_get_priv(); ++ uintptr_t addr = priv->base + pll->reg_pllxcr; ++ uint32_t value = 0; ++ ++ value = io_read32(addr + RCC_OFFSET_PLLXCR); ++ value &= (RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN | RCC_PLLNCR_DIVREN); ++ ++ return value; ++} ++ ++static void clk_stm32_pll_restore_output_diven(const struct stm32_clk_pll *pll, ++ uint32_t value) ++{ ++ struct clk_stm32_priv *priv = clk_stm32_get_priv(); ++ uintptr_t addr = priv->base + pll->reg_pllxcr; ++ const uint32_t mask = RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN | ++ RCC_PLLNCR_DIVREN; ++ ++ io_clrsetbits32(addr, mask, value & mask); ++} ++ ++static int clk_stm32_pll_init(struct clk_stm32_priv *priv, int pll_idx, ++ struct stm32_pll_dt_cfg *pll_conf) ++{ ++ const struct stm32_clk_pll *pll = clk_stm32_pll_data(pll_idx); ++ int config_on_the_fly = -1; ++ int ret = 0; ++ uint8_t sel = 0; ++ uint32_t save_div_pqr_en = 0; ++ int mux_system[] = { MUX_MPU, MUX_AXI, MUX_MLAHB, -1 }; ++ int mux_sys = mux_system[pll_idx]; ++ ++ ret = clk_stm32_is_pll_config_on_the_fly(priv, pll, pll_conf, &config_on_the_fly); ++ if (ret != 0) ++ return ret; ++ ++ /* Backup status of DIV DIVPEN / DIVQEN / DIVREN */ ++ save_div_pqr_en = clk_stm32_pll_backup_output_diven(pll); ++ ++ if (config_on_the_fly == -1) { ++ /* Make a backup to the current parent and switch to HSI */ ++ sel = clk_stm32_pll_init_switch_to_hsi_clk_system(mux_sys); ++ ++ /* Stop the PLL before */ ++ if (clk_stm32_is_enabled_gate(pll->gate_id)) { ++ io_clrbits32(priv->base + pll->reg_pllxcr, ++ RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN | ++ RCC_PLLNCR_DIVREN); ++ ++ clk_stm32_gate_ready_endisable(pll->gate_id, false, true); ++ } ++ ++ /* Configure PLLs source */ ++ ret = stm32_clk_configure_mux(priv, pll_conf->vco.src); ++ if (ret) ++ return ret; ++ ++ clk_stm32_pll_config_vco(priv, pll, &pll_conf->vco); ++ } ++ ++ if (config_on_the_fly != 1) { ++ clk_stm32_pll_config_out(priv, pll, &pll_conf->output); ++ clk_stm32_pll_config_csg(priv, pll, &pll_conf->vco); ++ } ++ ++ if (!clk_stm32_is_enabled_gate(pll->gate_id)) { ++ ret =clk_stm32_gate_ready_endisable(pll->gate_id, true, true); ++ if (ret != 0) ++ return ret; ++ ++ clk_stm32_pll_restore_output_diven(pll, save_div_pqr_en); ++ } ++ ++ if (config_on_the_fly == -1) ++ /* Restore to backup parent */ ++ clk_stm32_pll_init_restore_clk_system(mux_sys, sel); ++ ++ return 0; ++} ++ ++static int stm32_clk_pll_configure(struct clk_stm32_priv *priv) ++{ ++ struct stm32_pll_dt_cfg *pll_conf = NULL; ++ size_t i = 0; ++ int plls[] = { PLL1_ID, PLL3_ID, PLL4_ID }; ++ ++ for (i = 0; i < ARRAY_SIZE(plls); i++) { ++ pll_conf = clk_stm32_pll_get_pdata(plls[i]); ++ ++ if (pll_conf->vco.status) { ++ int err = 0; ++ ++ err = clk_stm32_pll_init(priv, plls[i], pll_conf); ++ if (err) ++ return err; ++ } ++ } ++ ++ return 0; ++} ++ ++static int stm32mp1_init_clock_tree(struct clk_stm32_priv *priv, ++ struct stm32_clk_platdata *pdata) ++{ ++ int ret = 0; ++ ++ /* ++ * Switch ON oscillators found in device-tree. ++ * Note: HSI already ON after BootROM stage. ++ */ ++ stm32_clk_oscillators_enable(priv, pdata); ++ ++ ret = stm32_clk_hsidiv_configure(priv); ++ if (ret != 0) ++ return ret; ++ ++ ret = stm32_clk_dividers_configure(priv); ++ if (ret != 0) ++ panic(); ++ ++ ret = stm32_clk_pll_configure(priv); ++ if (ret != 0) ++ panic(); ++ ++ /* Wait LSE ready before to use it */ ++ ret = stm32_clk_oscillators_wait_lse_ready(priv, pdata); ++ if (ret != 0) ++ panic(); ++ ++ /* Configure with expected clock source */ ++ ret = stm32_clk_source_configure(priv); ++ if (ret != 0) ++ panic(); ++ ++ /* Configure LSE css after RTC source configuration */ ++ ret = stm32_clk_oscillators_lse_set_css(priv, pdata); ++ if (ret != 0) ++ panic(); ++ ++ /* Software Self-Refresh mode (SSR) during DDR initilialization */ ++ io_clrsetbits32(priv->base + RCC_DDRITFCR, RCC_DDRITFCR_DDRCKMOD_MASK, ++ RCC_DDRITFCR_DDRCKMOD_SSR << RCC_DDRITFCR_DDRCKMOD_SHIFT); ++ ++ return 0; ++} ++ ++#ifdef CFG_STM32_CLK_DEBUG ++static void clk_stm32_debug_display_pll_cfg(int pll_id, struct stm32_pll_dt_cfg *pll) ++{ ++ struct stm32_pll_vco *vco = &pll->vco; ++ struct stm32_pll_output *out = &pll->output; ++ size_t j = 0; ++ ++ printf("PLL%d : %s", pll_id + 1, vco->status ? "" : "disabled"); ++ ++ if (!vco->status) { ++ printf("\n"); ++ return; ++ } ++ ++ printf(" vco = < "); ++ ++ for (j = 0; j < PLL_DIV_MN_NB; j++) { ++ printf("%d ", vco->div_mn[j]); ++ } ++ printf("> "); ++ ++ printf("frac = 0x%x ", vco->frac); ++ ++ printf("src = 0x%x ", vco->src); ++ ++ if (vco->csg_enabled) { ++ printf("csg = < "); ++ ++ for (j = 0; j < PLL_CSG_NB; j++) { ++ printf("%d ", vco->csg[j]); ++ } ++ ++ printf("> "); ++ } ++ ++ printf("output = < "); ++ ++ for (j = 0; j < PLL_DIV_PQR_NB; j++) { ++ printf("%d ", out->output[j]); ++ } ++ printf(">\n"); ++} ++ ++static void clk_stm32_debug_display_opp_cfg(const char *opp_name, ++ struct stm32_clk_opp_cfg *opp_cfg) ++{ ++ size_t i = 0; ++ ++ printf("\nOPP %s :\n", opp_name); ++ ++ for (i = 0; i < MAX_OPP; i++) { ++ if(opp_cfg->frq == 0UL) ++ break; ++ ++ printf("frequence = %d src = 0x%x div = 0x%x ", opp_cfg->frq, ++ opp_cfg->src, opp_cfg->div); ++ ++ clk_stm32_debug_display_pll_cfg(PLL1_ID, &opp_cfg->pll_cfg); ++ ++ opp_cfg++; ++ } ++ ++ printf("\n"); ++} ++ ++static void clk_stm32_debug_display_opp_dt_cfg(struct clk_stm32_priv *priv) ++{ ++ struct stm32_clk_platdata *pdata = priv->pdata; ++ struct stm32_clk_opp_dt_cfg *opp = pdata->opp; ++ ++ clk_stm32_debug_display_opp_cfg("st,ck_mpu", opp->mpu_opp); ++ clk_stm32_debug_display_opp_cfg("st,ck_axi", pdata->opp->axi_opp); ++ clk_stm32_debug_display_opp_cfg("st,ck_mlahbs", pdata->opp->mlahbs_opp); ++} ++ ++static void clk_stm32_debug_display_pll_dt_cfg(struct clk_stm32_priv *priv) ++{ ++ struct stm32_clk_platdata *pdata = priv->pdata; ++ size_t i = 0; ++ ++ for (i = PLL1_ID; i < pdata->npll; i++) ++ clk_stm32_debug_display_pll_cfg(i, &pdata->pll[i]); ++} ++ ++static void clk_stm32_debug_display_osc_dt_cfg(struct clk_stm32_priv *priv) ++{ ++ struct stm32_clk_platdata *pdata = priv->pdata; ++ size_t nb = pdata->nosci; ++ size_t i = 0; ++ ++ printf("\nNumber of oscillators = %d\n", nb); ++ ++ for (i = 0; i < nb; i++) { ++ struct stm32_osci_dt_cfg *osc = &pdata->osci[i]; ++ struct clk_oscillator_data *osc_data = clk_oscillator_get_data(i); ++ ++ printf("%s %ld bypass = %d digbyp = %d css = %d drive = %d\n", ++ osc_data->name, ++ osc->freq, ++ osc->bypass, ++ osc->digbyp, ++ osc->css, ++ osc->drive); ++ } ++} ++ ++static void clk_stm32_debug_display_src_dt_cfg(struct clk_stm32_priv *priv) ++{ ++ struct stm32_clk_platdata *pdata = priv->pdata; ++ size_t i = 0; ++ ++ printf("nb st,clksrc = %d\n", pdata->nclksrc); ++ ++ for (i = 0; i < pdata->nclksrc; i++) ++ printf("\t0x%x\n", pdata->clksrc[i]); ++ ++ printf("\n"); ++} ++ ++static void clk_stm32_debug_display_div_dt_cfg(struct clk_stm32_priv *priv) ++{ ++ struct stm32_clk_platdata *pdata = priv->pdata; ++ size_t i = 0; ++ ++ printf("nb st,clkdiv = %d\n", pdata->nclkdiv); ++ ++ for (i = 0; i < pdata->nclkdiv; i++) ++ printf("\t%d\n", pdata->clkdiv[i]); ++ ++ printf("\n"); ++} ++ ++static void clk_stm32_debug_display_pdata(void) ++{ ++ struct clk_stm32_priv *priv = clk_stm32_get_priv(); ++ ++ clk_stm32_debug_display_pll_dt_cfg(priv); ++ clk_stm32_debug_display_opp_dt_cfg(priv); ++ clk_stm32_debug_display_osc_dt_cfg(priv); ++ clk_stm32_debug_display_src_dt_cfg(priv); ++ clk_stm32_debug_display_div_dt_cfg(priv); ++} ++#endif ++ ++static TEE_Result stm32_clk_parse_fdt_mco_pins(const void *fdt, int node, ++ struct stm32_clk_platdata *pdata) ++{ ++ return stm32_pinctrl_dt_get_by_index(fdt, node, 0, &pdata->pinctrl_cfg); ++} ++ ++static int clk_stm32_parse_oscillator_fdt(const void *fdt, int node, ++ const char *name, ++ struct stm32_osci_dt_cfg *osci) ++{ ++ int subnode = 0; ++ ++ /* default value oscillator not found, freq=0 */ ++ osci->freq = 0; ++ ++ fdt_for_each_subnode(subnode, fdt, node) { ++ const char *cchar = NULL; ++ const fdt32_t *cuint = NULL; ++ int ret = 0; ++ ++ cchar = fdt_get_name(fdt, subnode, &ret); ++ if (cchar == NULL) ++ return ret; ++ ++ if (strncmp(cchar, name, (size_t)ret) || ++ _fdt_get_status(fdt, subnode) == DT_STATUS_DISABLED) ++ continue; ++ ++ cuint = fdt_getprop(fdt, subnode, "clock-frequency", &ret); ++ if (cuint == NULL) ++ return ret; ++ ++ osci->freq = fdt32_to_cpu(*cuint); ++ ++ if (fdt_getprop(fdt, subnode, "st,bypass", NULL) != NULL) ++ osci->bypass = true; ++ ++ if (fdt_getprop(fdt, subnode, "st,digbypass", NULL) != NULL) ++ osci->digbyp = true; ++ ++ if (fdt_getprop(fdt, subnode, "st,css", NULL) != NULL) ++ osci->css = true; ++ ++ osci->drive = _fdt_read_uint32_default(fdt, subnode, "st,drive", ++ LSEDRV_MEDIUM_HIGH); ++ ++ return 0; ++ } ++ ++ return 0; ++} ++ ++static int stm32_clk_parse_fdt_all_oscillator(const void *fdt, ++ __maybe_unused int node, ++ struct stm32_clk_platdata *pdata) ++{ ++ int fdt_err = 0; ++ size_t i = 0; ++ int osc_node = 0; ++ ++ osc_node = fdt_path_offset(fdt, "/clocks"); ++ if (osc_node < 0) ++ return -FDT_ERR_NOTFOUND; ++ ++ for (i = 0; i < NB_OSCILLATOR; i++) { ++ struct clk_oscillator_data *osc_data = clk_oscillator_get_data(i); ++ struct stm32_osci_dt_cfg *osci = &pdata->osci[i]; ++ const char *name = osc_data->name; ++ ++ fdt_err = clk_stm32_parse_oscillator_fdt(fdt, osc_node, name, osci); ++ if (fdt_err < 0) ++ panic(); ++ } ++ ++ return 0; ++} ++ ++#define RCC_PLL_NAME_SIZE 12 ++ ++static int clk_stm32_load_vco_config(const void *fdt, int subnode, ++ struct stm32_pll_vco *vco) ++{ ++ int err = 0; ++ ++ err = _fdt_read_uint32_array(fdt, subnode, "divmn", vco->div_mn, ++ (int)PLL_DIV_MN_NB); ++ if (err != 0) ++ return err; ++ ++ err = _fdt_read_uint32_array(fdt, subnode, "csg", vco->csg, ++ (int)PLL_CSG_NB); ++ ++ vco->csg_enabled = (err == 0); ++ ++ if (err == -FDT_ERR_NOTFOUND) ++ err = 0; ++ ++ if (err != 0) ++ return err; ++ ++ vco->status = RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN | ++ RCC_PLLNCR_DIVREN | RCC_PLLNCR_PLLON; ++ ++ vco->frac = _fdt_read_uint32_default(fdt, subnode, "frac", 0); ++ ++ vco->src = _fdt_read_uint32_default(fdt, subnode, "src", UINT32_MAX); ++ ++ return 0; ++} ++ ++static int clk_stm32_load_output_config(const void *fdt, int subnode, ++ struct stm32_pll_output *output) ++{ ++ return _fdt_read_uint32_array(fdt, subnode, "st,pll_div_pqr", ++ output->output, (int)PLL_DIV_PQR_NB); ++} ++ ++static int clk_stm32_parse_pll_fdt(const void *fdt, int subnode, ++ struct stm32_pll_dt_cfg *pll) ++{ ++ const fdt32_t *cuint = NULL; ++ int subnode_pll = 0; ++ int subnode_vco = 0; ++ int err = 0; ++ ++ cuint = fdt_getprop(fdt, subnode, "st,pll", NULL); ++ if (!cuint) ++ return -FDT_ERR_NOTFOUND; ++ ++ subnode_pll = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(*cuint)); ++ if (subnode_pll < 0) ++ return -FDT_ERR_NOTFOUND; ++ ++ cuint = fdt_getprop(fdt, subnode_pll, "st,pll_vco", NULL); ++ if (!cuint) ++ return -FDT_ERR_NOTFOUND; ++ ++ subnode_vco = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(*cuint)); ++ if (subnode_vco < 0) ++ return -FDT_ERR_NOTFOUND; ++ ++ err = clk_stm32_load_vco_config(fdt, subnode_vco, &pll->vco); ++ if (err != 0) ++ return err; ++ ++ err = clk_stm32_load_output_config(fdt, subnode_pll, &pll->output); ++ if (err != 0) ++ return err; ++ ++ return 0; ++} ++ ++static int stm32_clk_parse_fdt_all_pll(const void *fdt, int node, ++ struct stm32_clk_platdata *pdata) ++{ ++ ++ size_t i = 0; ++ ++ for (i = PLL1_ID; i < pdata->npll; i++) { ++ struct stm32_pll_dt_cfg *pll = pdata->pll + i; ++ char name[RCC_PLL_NAME_SIZE]; ++ int subnode = 0; ++ int err = 0; ++ ++ snprintf(name, sizeof(name), "st,pll@%d", i); ++ ++ subnode = fdt_subnode_offset(fdt, node, name); ++ if (!_fdt_check_node(fdt, subnode)) ++ continue; ++ ++ err = clk_stm32_parse_pll_fdt(fdt, subnode, pll); ++ if (err != 0) ++ panic(); ++ } ++ ++ return 0; ++} ++ ++static int stm32_clk_parse_fdt_opp(const void *fdt, int node, ++ const char *opp_name, ++ struct stm32_clk_opp_cfg *opp_cfg) ++{ ++ int subnode = 0; ++ int nb_opp = 0; ++ ++ node = fdt_subnode_offset(fdt, node, opp_name); ++ if (!_fdt_check_node(fdt, node)) ++ return 0; ++ ++ fdt_for_each_subnode(subnode, fdt, node) { ++ if (nb_opp >= MAX_OPP) { ++ EMSG("%d MAX opp in %s !", MAX_OPP, opp_name); ++ break; ++ } ++ ++ opp_cfg->frq = _fdt_read_uint32_default(fdt, subnode,"hz", UINT32_MAX); ++ opp_cfg->src = _fdt_read_uint32_default(fdt, subnode, "st,clksrc", UINT32_MAX); ++ opp_cfg->div = _fdt_read_uint32_default(fdt, subnode, "st,clkdiv", UINT32_MAX); ++ ++ clk_stm32_parse_pll_fdt(fdt, subnode, &opp_cfg->pll_cfg); ++ ++ opp_cfg++; ++ nb_opp++; ++ } ++ ++ return 0; ++} ++ ++static int stm32_clk_parse_fdt_all_opp(const void *fdt, int node, ++ struct stm32_clk_platdata *pdata) ++{ ++ struct stm32_clk_opp_dt_cfg *opp = pdata->opp; ++ ++ node = fdt_subnode_offset(fdt, node, "st,clk_opp"); ++ if (!_fdt_check_node(fdt, node)) ++ /* no opp are defined */ ++ return 0; ++ ++ stm32_clk_parse_fdt_opp(fdt, node, "st,ck_mpu", opp->mpu_opp); ++ stm32_clk_parse_fdt_opp(fdt, node, "st,ck_axi", opp->axi_opp); ++ stm32_clk_parse_fdt_opp(fdt, node, "st,ck_mlahbs", opp->mlahbs_opp); ++ ++ return 0; ++} ++ ++static int stm32_clk_parse_fdt(const void *fdt, int node, ++ struct stm32_clk_platdata *pdata) ++{ ++ int err = 0; ++ ++ err = stm32_clk_parse_fdt_all_oscillator(fdt, node, pdata); ++ if (err != 0) ++ return err; ++ ++ err = stm32_clk_parse_fdt_all_pll(fdt, node, pdata); ++ if (err != 0) ++ return err; ++ ++ err = stm32_clk_parse_fdt_all_opp(fdt, node, pdata); ++ if (err != 0) ++ return err; ++ ++ err = clk_stm32_parse_fdt_by_name(fdt, node, "st,clkdiv", pdata->clkdiv, ++ &pdata->nclkdiv); ++ if (err != 0) ++ return err; ++ ++ err = clk_stm32_parse_fdt_by_name(fdt, node, "st,clksrc", pdata->clksrc, ++ &pdata->nclksrc); ++ if (err != 0) ++ return err; ++ ++ return 0; ++} ++ ++struct clk_stm32_pll_cfg { ++ uint32_t reg_pllxcr; ++ int gate_id; ++ int mux_id; ++}; ++ ++static size_t clk_stm32_pll_get_parent(struct clk *clk) ++{ ++ struct clk_stm32_pll_cfg *cfg = clk->priv; ++ ++ return clk_stm32_get_parent_mux(cfg->mux_id);; ++} ++ ++static unsigned long clk_stm32_pll_get_rate(struct clk *clk, ++ unsigned long prate) ++{ ++ struct clk_stm32_priv *priv = clk_stm32_get_priv(); ++ struct clk_stm32_pll_cfg *cfg = clk->priv; ++ uintptr_t pll_base = priv->base + cfg->reg_pllxcr; ++ uint32_t cfgr1 = 0; ++ uint32_t fracr = 0; ++ uint32_t divm = 0; ++ uint32_t divn = 0; ++ unsigned long fvco = 0UL; ++ ++ cfgr1 = io_read32(pll_base + RCC_OFFSET_PLLXCFGR1); ++ fracr = io_read32(pll_base + RCC_OFFSET_PLLXFRACR); ++ ++ divm = (cfgr1 & (RCC_PLLNCFGR1_DIVM_MASK)) >> RCC_PLLNCFGR1_DIVM_SHIFT; ++ divn = cfgr1 & RCC_PLLNCFGR1_DIVN_MASK; ++ ++ /* ++ * With FRACV : ++ * Fvco = Fck_ref * ((DIVN + 1) + FRACV / 2^13) / (DIVM + 1) ++ * Without FRACV ++ * Fvco = Fck_ref * ((DIVN + 1) / (DIVM + 1) ++ */ ++ if ((fracr & RCC_PLLNFRACR_FRACLE) != 0U) { ++ uint32_t fracv = (fracr & RCC_PLLNFRACR_FRACV_MASK) >> ++ RCC_PLLNFRACR_FRACV_SHIFT; ++ unsigned long long numerator = 0UL; ++ unsigned long long denominator = 0UL; ++ ++ numerator = (((unsigned long long)divn + 1U) << 13) + fracv; ++ numerator = prate * numerator; ++ denominator = ((unsigned long long)divm + 1U) << 13; ++ fvco = (unsigned long)(numerator / denominator); ++ } else { ++ fvco = (unsigned long)(prate * (divn + 1U) / (divm + 1U)); ++ } ++ ++ return UDIV_ROUND_NEAREST(fvco, 100000) * 100000; ++}; ++ ++static bool clk_stm32_pll_is_enabled(struct clk *clk) ++{ ++ struct clk_stm32_pll_cfg *cfg = clk->priv; ++ ++ return clk_stm32_is_enabled_gate(cfg->gate_id); ++} ++ ++static TEE_Result clk_stm32_pll_enable(struct clk *clk) ++{ ++ struct clk_stm32_pll_cfg *cfg = clk->priv; ++ ++ if (clk_stm32_pll_is_enabled(clk)) ++ return 0; ++ ++ return clk_stm32_gate_ready_endisable(cfg->gate_id, true, true); ++} ++ ++static void clk_stm32_pll_disable(struct clk *clk) ++{ ++ struct clk_stm32_pll_cfg *cfg = clk->priv; ++ struct clk_stm32_priv *priv = clk_stm32_get_priv(); ++ uintptr_t pll_base = priv->base + cfg->reg_pllxcr; ++ ++ if (!clk_stm32_pll_is_enabled(clk)) ++ return; ++ ++ /* Stop all output */ ++ io_clrbits32(pll_base, RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN | ++ RCC_PLLNCR_DIVREN); ++ ++ clk_stm32_gate_ready_endisable(cfg->gate_id, false, true); ++} ++ ++static const struct clk_ops clk_stm32_pll_ops = { ++ .get_parent = clk_stm32_pll_get_parent, ++ .get_rate = clk_stm32_pll_get_rate, ++ .enable = clk_stm32_pll_enable, ++ .disable = clk_stm32_pll_disable, ++ .is_enabled = clk_stm32_pll_is_enabled, ++}; ++ ++static TEE_Result clk_stm32_composite_get_duty_cycle(struct clk *clk, ++ struct clk_duty *duty) ++{ ++ struct clk_stm32_composite_cfg *cfg = clk->priv; ++ uint32_t val = clk_stm32_div_get_value(cfg->div_id); ++ ++ duty->num = (val + 1) / 2; ++ duty->den = (val + 1); ++ ++ return 0; ++} ++ ++static unsigned long clk_stm32_composite_round_rate(__maybe_unused struct clk *clk, ++ unsigned long rate, ++ unsigned long prate) ++{ ++ unsigned int div = 0U; ++ ++ div = UDIV_ROUND_NEAREST((uint64_t)prate, rate); ++ ++ return UDIV_ROUND_NEAREST((uint64_t)prate, div); ++} ++ ++static const struct clk_ops clk_stm32_composite_duty_cycle_ops = { ++ .get_parent = clk_stm32_composite_get_parent, ++ .set_parent = clk_stm32_composite_set_parent, ++ .get_rate = clk_stm32_composite_get_rate, ++ .set_rate = clk_stm32_composite_set_rate, ++ .enable = clk_stm32_composite_gate_enable, ++ .disable = clk_stm32_composite_gate_disable, ++ .is_enabled = clk_stm32_composite_gate_is_enabled, ++ .round_rate = clk_stm32_composite_round_rate, ++ .get_duty_cycle = clk_stm32_composite_get_duty_cycle, ++}; ++ ++static struct stm32_clk_opp_cfg *clk_stm32_get_opp_config(struct stm32_clk_opp_cfg *opp_cfg, ++ unsigned long rate) ++{ ++ size_t i = 0; ++ ++ for (i = 0; i < MAX_OPP; i++, opp_cfg++) { ++ if (opp_cfg->frq == 0UL) ++ break; ++ ++ if (opp_cfg->frq == rate) ++ return opp_cfg; ++ } ++ ++ return NULL; ++} ++ ++static TEE_Result clk_stm32_pll1_set_rate(__maybe_unused struct clk *clk, ++ unsigned long rate, ++ __maybe_unused unsigned long prate) ++{ ++ const struct stm32_clk_pll *pll = clk_stm32_pll_data(PLL1_ID); ++ struct clk_stm32_priv *priv = clk_stm32_get_priv(); ++ struct stm32_clk_platdata *pdata = priv->pdata; ++ struct stm32_pll_dt_cfg *pll_conf = NULL; ++ struct stm32_clk_opp_cfg *opp = NULL; ++ int config_on_the_fly = -1; ++ int err = 0; ++ size_t sel = clk_stm32_get_parent_mux(MUX_MPU); ++ ++ opp = clk_stm32_get_opp_config(pdata->opp->mpu_opp, rate); ++ if (opp == NULL) { ++ return 0; ++ } ++ ++ pll_conf = &opp->pll_cfg; ++ ++ err = clk_stm32_is_pll_config_on_the_fly(priv, pll, pll_conf, ++ &config_on_the_fly); ++ if (err != 0) ++ return err; ++ ++ if (config_on_the_fly == 1) { ++ return 0; ++ } ++ ++ if (config_on_the_fly == -1) { ++ /* Switch to HSI and stop PLL1 before reconfiguration */ ++ clk_stm32_set_parent_mux(MUX_MPU, 0); ++ clk_stm32_disable_gate(GATE_PLL1_DIVP); ++ clk_stm32_gate_rdy_disable(GATE_PLL1); ++ clk_stm32_pll_config_vco(priv, pll, &pll_conf->vco); ++ } ++ ++ clk_stm32_pll_config_out(priv, pll, &pll_conf->output); ++ clk_stm32_gate_rdy_enable(GATE_PLL1); ++ clk_stm32_enable_gate(GATE_PLL1_DIVP); ++ ++ /* Restore MPU source */ ++ clk_stm32_set_parent_mux(MUX_MPU, sel); ++ ++ return 0; ++} ++ ++static const struct clk_ops clk_stm32_pll1_ops = { ++ .set_rate = clk_stm32_pll1_set_rate, ++ .get_parent = clk_stm32_pll_get_parent, ++ .get_rate = clk_stm32_pll_get_rate, ++ .enable = clk_stm32_pll_enable, ++ .disable = clk_stm32_pll_disable, ++ .is_enabled = clk_stm32_pll_is_enabled, ++}; ++ ++static const struct clk_ops clk_stm32_pll1p_ops = { ++ .get_rate = clk_stm32_composite_get_rate, ++ .enable = clk_stm32_composite_gate_enable, ++ .disable = clk_stm32_composite_gate_disable, ++ .is_enabled = clk_stm32_composite_gate_is_enabled, ++}; ++ ++static TEE_Result clk_stm32_mpu_determine_rate(struct clk *clk, ++ struct clk_rate_request *req) ++{ ++ struct clk_stm32_priv *priv = clk_stm32_get_priv(); ++ struct stm32_clk_platdata *pdata = priv->pdata; ++ struct stm32_clk_opp_cfg *opp = NULL; ++ unsigned long rate = req->rate; ++ struct clk *parent = NULL; ++ int index = 0; ++ ++ opp = clk_stm32_get_opp_config(pdata->opp->mpu_opp, rate); ++ if (opp == NULL) ++ return 0; ++ ++ index = (opp->src & MUX_SEL_MASK) >> MUX_SEL_SHIFT; ++ ++ parent = clk_get_parent_by_index(clk, index); ++ ++ req->best_parent = parent; ++ req->best_parent_rate = req->rate; ++ ++ return 0; ++} ++ ++static const struct clk_ops clk_stm32_mpu_ops = { ++ .determine_rate = clk_stm32_mpu_determine_rate, ++ .get_parent = clk_stm32_composite_get_parent, ++ .set_parent = clk_stm32_composite_set_parent, ++}; ++ ++static TEE_Result clk_stm32_axi_determine_rate(struct clk *clk, ++ struct clk_rate_request *req) ++{ ++ struct clk_stm32_priv *priv = clk_stm32_get_priv(); ++ struct stm32_clk_platdata *pdata = priv->pdata; ++ struct stm32_clk_opp_cfg *opp = NULL; ++ unsigned long rate = req->rate; ++ struct clk *parent = NULL; ++ int index = 0; ++ ++ opp = clk_stm32_get_opp_config(pdata->opp->axi_opp, rate); ++ if (opp == NULL) ++ return 0; ++ ++ index = (opp->src & MUX_SEL_MASK) >> MUX_SEL_SHIFT; ++ parent = clk_get_parent_by_index(clk, index); ++ ++ req->best_parent = parent; ++ req->best_parent_rate = parent->rate; ++ ++ return 0; ++} ++ ++static const struct clk_ops clk_stm32_axi_ops = { ++ .determine_rate = clk_stm32_axi_determine_rate, ++ .get_parent = clk_stm32_composite_get_parent, ++ .set_parent = clk_stm32_composite_set_parent, ++ .set_rate = clk_stm32_composite_set_rate, ++ .get_rate = clk_stm32_composite_get_rate, ++}; ++ ++static TEE_Result clk_stm32_mlahb_determine_rate(struct clk *clk, struct clk_rate_request *req) ++{ ++ struct clk_stm32_priv *priv = clk_stm32_get_priv(); ++ struct stm32_clk_platdata *pdata = priv->pdata; ++ struct stm32_clk_opp_cfg *opp = NULL; ++ unsigned long rate = req->rate; ++ struct clk *parent = NULL; ++ int index = 0; ++ ++ opp = clk_stm32_get_opp_config(pdata->opp->mlahbs_opp, rate); ++ if (opp == NULL) ++ return 0; ++ ++ index = (opp->src & MUX_SEL_MASK) >> MUX_SEL_SHIFT; ++ parent = clk_get_parent_by_index(clk, index); ++ ++ req->best_parent = parent; ++ req->best_parent_rate = parent->rate; ++ ++ return 0; ++} ++ ++const struct clk_ops clk_stm32_mlahb_ops = { ++ .determine_rate = clk_stm32_mlahb_determine_rate, ++ .get_parent = clk_stm32_composite_get_parent, ++ .set_parent = clk_stm32_composite_set_parent, ++ .set_rate = clk_stm32_composite_set_rate, ++ .get_rate = clk_stm32_composite_get_rate, ++}; ++ ++#define APB_DIV_MASK GENMASK_32(2, 0) ++#define TIM_PRE_MASK BIT(0) ++ ++static unsigned long ck_timer_get_rate_ops(struct clk *clk, unsigned long prate) ++{ ++ struct clk_stm32_priv *priv = clk_stm32_get_priv(); ++ struct clk_stm32_timer_cfg *cfg = clk->priv; ++ uint32_t prescaler, timpre; ++ uintptr_t rcc_base = priv->base; ++ ++ prescaler = io_read32(rcc_base + cfg->apbdiv) & APB_DIV_MASK; ++ ++ timpre = io_read32(rcc_base + cfg->timpre) & TIM_PRE_MASK; ++ ++ if (prescaler == 0U) ++ return prate; ++ ++ return prate * (timpre + 1U) * 2U; ++}; ++ ++const struct clk_ops ck_timer_ops = { ++ .get_rate = ck_timer_get_rate_ops, ++}; ++ ++#define STM32_TIMER(_name, _parent, _flags, _apbdiv, _timpre)\ ++struct clk _name = {\ ++ .ops = &ck_timer_ops,\ ++ .priv = &(struct clk_stm32_timer_cfg) {\ ++ .apbdiv = (_apbdiv),\ ++ .timpre = (_timpre),\ ++ },\ ++ .name = #_name,\ ++ .flags = (0 | (_flags)),\ ++ .num_parents = 1,\ ++ .parents = { _parent },\ ++} ++ ++#define STM32_KCLK(_name, _nb_parents, _parents, _flags, _gate_id, _mux_id)\ ++struct clk _name = {\ ++ .ops = &clk_stm32_composite_ops,\ ++ .priv = &(struct clk_stm32_composite_cfg) {\ ++ .gate_id = (_gate_id),\ ++ .div_id = (NO_DIV),\ ++ .mux_id = (_mux_id),\ ++ },\ ++ .name = #_name,\ ++ .flags = (_flags),\ ++ .num_parents = (_nb_parents),\ ++ .parents = _parents,\ ++} ++ ++#define STM32_PLL_VCO(_name, _nb_parents, _parents, _flags, _reg,\ ++ _gate_id, _mux_id)\ ++struct clk _name = {\ ++ .ops = &clk_stm32_pll_ops,\ ++ .priv = &(struct clk_stm32_pll_cfg) {\ ++ .reg_pllxcr = (_reg),\ ++ .gate_id = (_gate_id),\ ++ .mux_id = (_mux_id),\ ++ },\ ++ .name = #_name,\ ++ .flags = (_flags),\ ++ .num_parents = (_nb_parents),\ ++ .parents = _parents,\ ++} ++ ++#define STM32_PLL_OUPUT(_name, _nb_parents, _parents, _flags,\ ++ _gate_id, _div_id, _mux_id)\ ++struct clk _name = {\ ++ .ops = &clk_stm32_composite_duty_cycle_ops,\ ++ .priv = &(struct clk_stm32_composite_cfg) {\ ++ .gate_id = (_gate_id),\ ++ .div_id = (_div_id),\ ++ .mux_id = (_mux_id),\ ++ },\ ++ .name = #_name,\ ++ .flags = (_flags),\ ++ .num_parents = (_nb_parents),\ ++ .parents = _parents,\ ++} ++ ++/* Oscillator clocks */ ++static STM32_GATE_READY(ck_hsi, NULL, 0, GATE_HSI); ++static STM32_GATE_READY(ck_hse, NULL, 0, GATE_HSE); ++static STM32_GATE_READY(ck_csi, NULL, 0, GATE_CSI); ++static STM32_GATE_READY(ck_lsi, NULL, 0, GATE_LSI); ++static STM32_GATE_READY(ck_lse, NULL, 0, GATE_LSE); ++ ++static STM32_FIXED_FACTOR(ck_i2sckin, NULL, 0, 1, 1); ++static STM32_FIXED_FACTOR(ck_hse_div2, &ck_hse, 0, 1, 2); ++ ++static STM32_FIXED_RATE(ck_off, 0UL); ++static STM32_FIXED_RATE(ck_usb_phy_48Mhz, USB_PHY_48_MHZ); ++ ++/* PLL1 clocks */ ++static struct clk ck_pll1_vco = { ++ .ops = &clk_stm32_pll1_ops, ++ .priv = &(struct clk_stm32_pll_cfg) { ++ .reg_pllxcr = RCC_PLL1CR, ++ .gate_id = GATE_PLL1, ++ .mux_id = MUX_PLL12, ++ }, ++ .name = "ck_pll1_vco", ++ .flags = 0, ++ .num_parents = 2, ++ .parents = { &ck_hsi, &ck_hse }, ++}; ++ ++static struct clk ck_pll1p = { ++ .ops = &clk_stm32_pll1p_ops, ++ .priv = &(struct clk_stm32_composite_cfg) { ++ .gate_id = GATE_PLL1_DIVP, ++ .div_id = DIV_PLL1DIVP, ++ .mux_id = NO_MUX, ++ },\ ++ .name = "ck_pll1p", ++ .flags = CLK_SET_RATE_PARENT,\ ++ .num_parents = 1, ++ .parents = { &ck_pll1_vco }, ++}; ++ ++const struct clk_ops clk_stm32_pll1p_div_ops = { ++ .get_rate = clk_stm32_divider_get_rate, ++}; ++ ++static struct clk ck_pll1p_div = { ++ .ops = &clk_stm32_pll1p_div_ops, ++ .priv = &(struct clk_stm32_div_cfg) { ++ .div_id = DIV_MPU, ++ }, ++ .name = "ck_pll1p_div", ++ .flags = CLK_SET_RATE_PARENT, ++ .num_parents = 1, ++ .parents = { &ck_pll1p }, ++}; ++ ++/* Other PLLs */ ++static STM32_PLL_VCO(ck_pll2_vco, 2, PARENT(&ck_hsi, &ck_hse), ++ 0, RCC_PLL2CR, GATE_PLL2, MUX_PLL12); ++ ++static STM32_PLL_VCO(ck_pll3_vco, 3, ++ PARENT(&ck_hsi, &ck_hse, &ck_csi), ++ 0, RCC_PLL3CR, GATE_PLL3, MUX_PLL3); ++ ++static STM32_PLL_VCO(ck_pll4_vco, 4, ++ PARENT(&ck_hsi, &ck_hse, &ck_csi, ++ &ck_i2sckin), ++ 0, RCC_PLL4CR, GATE_PLL4, MUX_PLL4); ++ ++static STM32_PLL_OUPUT(ck_pll2p, 1, PARENT(&ck_pll2_vco), 0, ++ GATE_PLL2_DIVP, DIV_PLL2DIVP, NO_MUX); ++ ++static STM32_PLL_OUPUT(ck_pll2q, 1, PARENT(&ck_pll2_vco), 0, ++ GATE_PLL2_DIVQ, DIV_PLL2DIVQ, NO_MUX); ++ ++static STM32_PLL_OUPUT(ck_pll2r, 1, PARENT(&ck_pll2_vco), 0, ++ GATE_PLL2_DIVR, DIV_PLL2DIVR, NO_MUX); ++ ++static STM32_PLL_OUPUT(ck_pll3p, 1, PARENT(&ck_pll3_vco), 0, ++ GATE_PLL3_DIVP, DIV_PLL3DIVP, NO_MUX); ++ ++static STM32_PLL_OUPUT(ck_pll3q, 1, PARENT(&ck_pll3_vco), 0, ++ GATE_PLL3_DIVQ, DIV_PLL3DIVQ, NO_MUX); ++ ++static STM32_PLL_OUPUT(ck_pll3r, 1, PARENT(&ck_pll3_vco), 0, ++ GATE_PLL3_DIVR, DIV_PLL3DIVR, NO_MUX); ++ ++static STM32_PLL_OUPUT(ck_pll4p, 1, PARENT(&ck_pll4_vco), 0, ++ GATE_PLL4_DIVP, DIV_PLL4DIVP, NO_MUX); ++ ++static STM32_PLL_OUPUT(ck_pll4q, 1, PARENT(&ck_pll4_vco), 0, ++ GATE_PLL4_DIVQ, DIV_PLL4DIVQ, NO_MUX); ++ ++static STM32_PLL_OUPUT(ck_pll4r, 1, PARENT(&ck_pll4_vco), 0, ++ GATE_PLL4_DIVR, DIV_PLL4DIVR, NO_MUX); ++ ++/* System clocks */ ++static struct clk ck_mpu = { ++ .ops = &clk_stm32_mpu_ops, ++ .priv = &(struct clk_stm32_composite_cfg) { ++ .mux_id = MUX_MPU, ++ }, ++ .name = "ck_mpu", ++ .flags = CLK_OPS_PARENT_ENABLE | CLK_SET_RATE_PARENT, ++ .num_parents = 4, ++ .parents = { &ck_hsi, &ck_hse, &ck_pll1p, &ck_pll1p_div }, ++}; ++ ++static struct clk ck_axi = { ++ .ops = &clk_stm32_axi_ops, ++ .priv = &(struct clk_stm32_composite_cfg) { ++ .mux_id = MUX_AXI, ++ .div_id = DIV_AXI, ++ }, ++ .name = "ck_axi", ++ .flags = 0, ++ .num_parents = 3, ++ .parents = { &ck_hsi, &ck_hse, &ck_pll2p }, ++}; ++ ++static struct clk ck_mlahb = { ++ .ops = &clk_stm32_mlahb_ops, ++ .priv = &(struct clk_stm32_composite_cfg) { ++ .mux_id = MUX_MLAHB, ++ .div_id = DIV_MLAHB, ++ }, ++ .name = "ck_mlahb", ++ .flags = 0, ++ .num_parents = 4, ++ .parents = { &ck_hsi, &ck_hse, &ck_csi, &ck_pll3p }, ++}; ++ ++static STM32_MUX(ck_per, 4, PARENT(&ck_hsi, &ck_csi, &ck_hse, ++ &ck_off), 0, MUX_CKPER); ++ ++/* Bus clocks */ ++static STM32_DIVIDER(ck_pclk1, &ck_mlahb, 0, DIV_APB1); ++static STM32_DIVIDER(ck_pclk2, &ck_mlahb, 0, DIV_APB2); ++static STM32_DIVIDER(ck_pclk3, &ck_mlahb, 0, DIV_APB3); ++static STM32_DIVIDER(ck_pclk4, &ck_axi, 0, DIV_APB4); ++static STM32_DIVIDER(ck_pclk5, &ck_axi, 0, DIV_APB5); ++static STM32_DIVIDER(ck_pclk6, &ck_mlahb, 0, DIV_APB6); ++ ++/* Timer Clocks */ ++static STM32_TIMER(ck_timg1, &ck_pclk1, 0, RCC_APB1DIVR, RCC_TIMG1PRER ); ++static STM32_TIMER(ck_timg2, &ck_pclk2, 0, RCC_APB2DIVR, RCC_TIMG2PRER ); ++static STM32_TIMER(ck_timg3, &ck_pclk6, 0, RCC_APB6DIVR, RCC_TIMG3PRER ); ++ ++/* Peripheral and Kernel Clocks */ ++static STM32_GATE(ck_ddrc1, &ck_axi, 0, GATE_DDRC1); ++static STM32_GATE(ck_ddrc1lp, &ck_axi, 0, GATE_DDRC1LP); ++static STM32_GATE(ck_ddrphyc, &ck_pll2r, 0, GATE_DDRPHYC); ++static STM32_GATE(ck_ddrphyclp, &ck_pll2r, 0, GATE_DDRPHYCLP); ++static STM32_GATE(ck_ddrcapb, &ck_pclk4, 0, GATE_DDRCAPB); ++static STM32_GATE(ck_ddrcapblp, &ck_pclk4, 0, GATE_DDRCAPBLP); ++static STM32_GATE(ck_axidcg, &ck_axi, 0, GATE_AXIDCG); ++static STM32_GATE(ck_ddrphycapb, &ck_pclk4, 0, 0); ++static STM32_GATE(ck_ddrphycapblp, &ck_pclk4, 0, GATE_DDRPHYCAPBLP); ++static STM32_GATE(ck_syscfg, &ck_pclk3, 0, GATE_SYSCFG); ++static STM32_GATE(ck_ddrperfm, &ck_pclk4, 0, GATE_DDRPERFM); ++static STM32_GATE(ck_iwdg2, &ck_pclk4, 0, GATE_IWDG2APB); ++static STM32_GATE(ck_rtcapb, &ck_pclk5, 0, GATE_RTCAPB); ++static STM32_GATE(ck_tzc, &ck_pclk5, 0, GATE_TZC); ++static STM32_GATE(ck_etzpcb, &ck_pclk5, 0, GATE_ETZPC); ++static STM32_GATE(ck_iwdg1apb, &ck_pclk5, 0, GATE_IWDG1APB); ++static STM32_GATE(ck_bsec, &ck_pclk5, 0, GATE_BSEC); ++static STM32_GATE(ck_tim12_k, &ck_timg3, 0, GATE_TIM12); ++static STM32_GATE(ck_tim15_k, &ck_timg3, 0, GATE_TIM15); ++static STM32_GATE(ck_gpioa, &ck_mlahb, 0, GATE_GPIOA); ++static STM32_GATE(ck_gpiob, &ck_mlahb, 0, GATE_GPIOB); ++static STM32_GATE(ck_gpioc, &ck_mlahb, 0, GATE_GPIOC); ++static STM32_GATE(ck_gpiod, &ck_mlahb, 0, GATE_GPIOD); ++static STM32_GATE(ck_gpioe, &ck_mlahb, 0, GATE_GPIOE); ++static STM32_GATE(ck_gpiof, &ck_mlahb, 0, GATE_GPIOF); ++static STM32_GATE(ck_gpiog, &ck_mlahb, 0, GATE_GPIOG); ++static STM32_GATE(ck_gpioh, &ck_mlahb, 0, GATE_GPIOH); ++static STM32_GATE(ck_gpioi, &ck_mlahb, 0, GATE_GPIOI); ++static STM32_GATE(ck_pka, &ck_axi, 0, GATE_PKA); ++static STM32_GATE(ck_cryp1, &ck_pclk5, 0, GATE_CRYP1); ++static STM32_GATE(ck_hash1, &ck_pclk5, 0, GATE_HASH1); ++static STM32_GATE(ck_bkpsram, &ck_pclk5, 0, GATE_BKPSRAM); ++static STM32_GATE(ck_dbg, &ck_axi, 0, GATE_DBGCK); ++static STM32_GATE(ck_mce, &ck_axi, 0, GATE_MCE); ++static STM32_GATE(ck_tim2_k, &ck_timg1, 0, GATE_TIM2); ++static STM32_GATE(ck_tim3_k, &ck_timg1, 0, GATE_TIM3); ++static STM32_GATE(ck_tim4_k, &ck_timg1, 0, GATE_TIM4); ++static STM32_GATE(ck_tim5_k, &ck_timg1, 0, GATE_TIM5); ++static STM32_GATE(ck_tim6_k, &ck_timg1, 0, GATE_TIM6); ++static STM32_GATE(ck_tim7_k, &ck_timg1, 0, GATE_TIM7); ++static STM32_GATE(ck_tim13_k, &ck_timg3, 0, GATE_TIM13); ++static STM32_GATE(ck_tim14_k, &ck_timg3, 0, GATE_TIM14); ++static STM32_GATE(ck_tim1_k, &ck_timg2, 0, GATE_TIM1); ++static STM32_GATE(ck_tim8_k, &ck_timg2, 0, GATE_TIM8); ++static STM32_GATE(ck_tim16_k, &ck_timg3, 0, GATE_TIM16); ++static STM32_GATE(ck_tim17_k, &ck_timg3, 0, GATE_TIM17); ++static STM32_GATE(ck_ltdc_px, &ck_pll4q, 0, GATE_LTDC); ++static STM32_GATE(ck_dma1, &ck_mlahb, 0, GATE_DMA1); ++static STM32_GATE(ck_dma2, &ck_mlahb, 0, GATE_DMA2); ++static STM32_GATE(ck_mdma, &ck_axi, 0, GATE_MDMA); ++static STM32_GATE(ck_eth1mac, &ck_axi, 0, GATE_ETH1MAC); ++static STM32_GATE(ck_usbh, &ck_axi, 0, GATE_USBH); ++static STM32_GATE(ck_vref, &ck_pclk3, 0, GATE_VREF); ++static STM32_GATE(ck_tmpsens, &ck_pclk3, 0, GATE_DTS); ++static STM32_GATE(ck_pmbctrl, &ck_pclk3, 0, GATE_HDP); ++static STM32_GATE(ck_hdp, &ck_pclk3, 0, GATE_PMBCTRL); ++static STM32_GATE(ck_stgenro, &ck_pclk4, 0, GATE_DCMIPP); ++static STM32_GATE(ck_dmamux1, &ck_axi, 0, GATE_DMAMUX1); ++static STM32_GATE(ck_dmamux2, &ck_axi, 0, GATE_DMAMUX2); ++static STM32_GATE(ck_dma3, &ck_axi, 0, GATE_DMAMUX2); ++static STM32_GATE(ck_tsc, &ck_axi, 0, GATE_TSC); ++static STM32_GATE(ck_aximc, &ck_axi, 0, GATE_AXIMC); ++static STM32_GATE(ck_crc1, &ck_axi, 0, GATE_ETH1TX); ++static STM32_GATE(ck_eth1tx, &ck_axi, 0, GATE_ETH1TX); ++static STM32_GATE(ck_eth1rx, &ck_axi, 0, GATE_ETH1RX); ++static STM32_GATE(ck_eth2tx, &ck_axi, 0, GATE_ETH2TX); ++static STM32_GATE(ck_eth2rx, &ck_axi, 0, GATE_ETH2RX); ++static STM32_GATE(ck_eth2mac, &ck_axi, 0, GATE_ETH2MAC); ++ ++/* Kernel Clocks */ ++static STM32_KCLK(ck_usbphy_k, 3, ++ PARENT(&ck_hse, &ck_pll4r, &ck_hse_div2), ++ 0, GATE_USBPHY, MUX_USBPHY); ++ ++static STM32_KCLK(ck_usbo_k, 2, ++ PARENT(&ck_pll4r, &ck_usb_phy_48Mhz), 0, ++ GATE_USBO, MUX_USBO); ++ ++static STM32_KCLK(ck_stgen_k, 2, ++ PARENT(&ck_hsi, &ck_hse), 0, GATE_STGENC, MUX_STGEN); ++ ++static STM32_KCLK(ck_usart1_k, 6, ++ PARENT(&ck_pclk6, &ck_pll3q, &ck_hsi, ++ &ck_csi, &ck_pll4q, &ck_hse), ++ 0, GATE_USART1, MUX_UART1); ++ ++static STM32_KCLK(ck_usart2_k, 6, ++ PARENT(&ck_pclk6, &ck_pll3q, &ck_hsi, ++ &ck_csi, &ck_pll4q, &ck_hse), ++ 0, GATE_USART2, MUX_UART2); ++ ++static STM32_KCLK(ck_i2c4_k, 4, ++ PARENT(&ck_pclk6, &ck_pll4r, &ck_hsi, ++ &ck_csi), ++ 0, GATE_I2C4, MUX_I2C4); ++ ++static STM32_KCLK(ck_rtc, 4, ++ PARENT(&ck_off, &ck_lse, &ck_lsi, ++ &ck_hse), ++ 0, GATE_RTCCK, MUX_RTC); ++ ++static STM32_KCLK(ck_saes_k, 4, ++ PARENT(&ck_axi, &ck_per, &ck_pll4r, ++ &ck_lsi), ++ 0, GATE_SAES, MUX_SAES); ++ ++static STM32_KCLK(ck_rng1_k, 4, ++ PARENT(&ck_csi, &ck_pll4r, &ck_off, ++ &ck_lsi), ++ 0, GATE_RNG1, MUX_RNG1); ++ ++static STM32_KCLK(ck_sdmmc1_k, 4, ++ PARENT(&ck_axi, &ck_pll3r, &ck_pll4p, ++ &ck_hsi), ++ 0, GATE_SDMMC1, MUX_SDMMC1); ++ ++static STM32_KCLK(ck_sdmmc2_k, 4, ++ PARENT(&ck_axi, &ck_pll3r, &ck_pll4p, ++ &ck_hsi), ++ 0, GATE_SDMMC2, MUX_SDMMC2); ++ ++static STM32_KCLK(ck_usart3_k, 5, ++ PARENT(&ck_pclk1, &ck_pll4q, &ck_hsi, &ck_csi, ++ &ck_hse), ++ 0, GATE_USART3, MUX_UART35); ++ ++static STM32_KCLK(ck_uart4_k, 5, ++ PARENT(&ck_pclk1, &ck_pll4q, &ck_hsi, &ck_csi, ++ &ck_hse), ++ 0, GATE_UART4, MUX_UART4); ++ ++static STM32_KCLK(ck_uart5_k, 5, ++ PARENT(&ck_pclk1, &ck_pll4q, &ck_hsi, &ck_csi, ++ &ck_hse), ++ 0, GATE_UART5, MUX_UART35); ++ ++static STM32_KCLK(ck_uart7_k, 5, ++ PARENT(&ck_pclk1, &ck_pll4q, &ck_hsi, &ck_csi, ++ &ck_hse), ++ 0, GATE_UART7, MUX_UART78); ++ ++static STM32_KCLK(ck_uart8_k, 5, ++ PARENT(&ck_pclk1, &ck_pll4q, &ck_hsi, &ck_csi, ++ &ck_hse), ++ 0, GATE_UART8, MUX_UART78); ++ ++static STM32_KCLK(ck_usart6_k, 5, ++ PARENT(&ck_pclk2, &ck_pll4q, &ck_hsi, &ck_csi, ++ &ck_hse), ++ 0, GATE_USART6, MUX_UART6); ++ ++static STM32_KCLK(ck_fmc_k, 4, ++ PARENT(&ck_axi, &ck_pll3r, &ck_pll4p, ++ &ck_per), ++ 0, GATE_FMC, MUX_FMC); ++ ++static STM32_KCLK(ck_qspi_k, 4, ++ PARENT(&ck_axi, &ck_pll3r, &ck_pll4p, ++ &ck_per), ++ 0, GATE_QSPI, MUX_QSPI); ++ ++static STM32_KCLK(ck_lptim1_k, 6, ++ PARENT(&ck_pclk1, &ck_pll4p, &ck_pll3q, ++ &ck_lse, &ck_lsi, &ck_per), ++ 0, GATE_LPTIM1, MUX_LPTIM1); ++ ++static STM32_KCLK(ck_spi2_k, 5, ++ PARENT(&ck_pll4p, &ck_pll3q, &ck_i2sckin, ++ &ck_per, &ck_pll3r), ++ 0, GATE_SPI2, MUX_SPI23); ++ ++static STM32_KCLK(ck_spi3_k, 5, ++ PARENT(&ck_pll4p, &ck_pll3q, &ck_i2sckin, ++ &ck_per, &ck_pll3r), ++ 0, GATE_SPI3, MUX_SPI23); ++ ++static STM32_KCLK(ck_spdif_k, 3, ++ PARENT(&ck_pll4p, &ck_pll3q, &ck_hsi), ++ 0, GATE_SPDIF, MUX_SPDIF); ++ ++static STM32_KCLK(ck_spi1_k, 5, ++ PARENT(&ck_pll4p, &ck_pll3q, &ck_i2sckin, ++ &ck_per, &ck_pll3r), ++ 0, GATE_SPI1, MUX_SPI1); ++ ++static STM32_KCLK(ck_spi4_k, 6, ++ PARENT(&ck_pclk6, &ck_pll4q, &ck_hsi, &ck_csi, ++ &ck_hse, &ck_i2sckin), ++ 0, GATE_SPI4, MUX_SPI4); ++ ++static STM32_KCLK(ck_spi5_k, 5, ++ PARENT(&ck_pclk6, &ck_pll4q, &ck_hsi, &ck_csi, ++ &ck_hse), ++ 0, GATE_SPI5, MUX_SPI5); ++ ++static STM32_KCLK(ck_sai1_k, 5, ++ PARENT(&ck_pll4q, &ck_pll3q, &ck_i2sckin, ++ &ck_per, &ck_pll3r), ++ 0, GATE_SAI1, MUX_SAI1); ++ ++static STM32_KCLK(ck_sai2_k, 6, ++ PARENT(&ck_pll4q, &ck_pll3q, &ck_i2sckin, ++ &ck_per, &ck_off, &ck_pll3r), ++ 0, GATE_SAI2, MUX_SAI2); ++ ++static STM32_KCLK(ck_dfsdm_k, 5, ++ PARENT(&ck_pll4q, &ck_pll3q, &ck_i2sckin, ++ &ck_per, &ck_pll3r), ++ 0, GATE_DFSDM, MUX_SAI1); ++ ++static STM32_KCLK(ck_fdcan_k, 4, ++ PARENT(&ck_hse, &ck_pll3q, &ck_pll4q, ++ &ck_pll4r), ++ 0, GATE_FDCAN, MUX_FDCAN); ++ ++static STM32_KCLK(ck_i2c1_k, 4, ++ PARENT(&ck_pclk1, &ck_pll4r, &ck_hsi, ++ &ck_csi), ++ 0, GATE_I2C1, MUX_I2C12); ++ ++static STM32_KCLK(ck_i2c2_k, 4, ++ PARENT(&ck_pclk1, &ck_pll4r, &ck_hsi,&ck_csi), ++ 0, GATE_I2C2, MUX_I2C12); ++ ++static STM32_KCLK(ck_adfsdm_k, 5, ++ PARENT(&ck_pll4q, &ck_pll3q, &ck_i2sckin, ++ &ck_per, &ck_pll3r), ++ 0, GATE_ADFSDM, MUX_SAI1); ++ ++static STM32_KCLK(ck_lptim2_k, 5, ++ PARENT(&ck_pclk3, &ck_pll4q, &ck_per, &ck_lse, ++ &ck_lsi), ++ 0, GATE_LPTIM2, MUX_LPTIM2); ++ ++static STM32_KCLK(ck_lptim3_k, 5, ++ PARENT(&ck_pclk3, &ck_pll4q, &ck_per, &ck_lse, ++ &ck_lsi), ++ 0, GATE_LPTIM3, MUX_LPTIM3); ++ ++static STM32_KCLK(ck_lptim4_k, 6, ++ PARENT(&ck_pclk3, &ck_pll4p, &ck_pll3q, ++ &ck_lse,&ck_lsi, &ck_per), ++ 0, GATE_LPTIM4, MUX_LPTIM45); ++ ++static STM32_KCLK(ck_lptim5_k, 6, ++ PARENT(&ck_pclk3, &ck_pll4p, &ck_pll3q, ++ &ck_lse, &ck_lsi, &ck_per), ++ 0, GATE_LPTIM5, MUX_LPTIM45); ++ ++static STM32_KCLK(ck_i2c3_k, 4, ++ PARENT(&ck_pclk6, &ck_pll4r, &ck_hsi, ++ &ck_csi), ++ 0, GATE_I2C3, MUX_I2C3); ++ ++static STM32_KCLK(ck_i2c5_k, 4, ++ PARENT(&ck_pclk6, &ck_pll4r, &ck_hsi, ++ &ck_csi), ++ 0, GATE_I2C5, MUX_I2C5); ++ ++static STM32_KCLK(ck_dcmipp_k, 4, ++ PARENT(&ck_axi, &ck_pll2q, &ck_pll4p, ++ &ck_per), ++ 0, GATE_DCMIPP, MUX_DCMIPP); ++ ++static STM32_KCLK(ck_adc1_k, 3, PARENT(&ck_pll4r, &ck_per, ++ &ck_pll3q), ++ 0, GATE_ADC1, MUX_ADC1); ++ ++static STM32_KCLK(ck_adc2_k, 3, PARENT(&ck_pll4r, &ck_per, ++ &ck_pll3q), ++ 0, GATE_ADC2, MUX_ADC2); ++ ++static STM32_KCLK(ck_eth1ck_k, 2, PARENT(&ck_pll4p, &ck_pll3q), ++ 0, GATE_ETH1CK, MUX_ETH1); ++ ++static STM32_KCLK(ck_eth2ck_k, 2, PARENT(&ck_pll4p, &ck_pll3q), ++ 0, GATE_ETH2CK, MUX_ETH2); ++ ++static STM32_COMPOSITE(ck_mco1, 5, ++ PARENT(&ck_hsi, &ck_hse, &ck_csi, ++ &ck_lsi, &ck_lse), 0, ++ GATE_MCO1, DIV_MCO1, MUX_MCO1); ++ ++static STM32_COMPOSITE(ck_mco2, 6, ++ PARENT(&ck_mpu, &ck_axi, &ck_mlahb, ++ &ck_pll4p, &ck_hse, &ck_hsi), ++ 0, GATE_MCO2, DIV_MCO2, MUX_MCO2); ++ ++static STM32_COMPOSITE(ck_trace, 1, PARENT(&ck_axi), ++ 0, GATE_TRACECK, DIV_TRACE, NO_MUX); ++ ++enum { ++ USB_PHY_48 = STM32MP1_LAST_CLK, ++ PLL1P_DIV, ++ CK_OFF, ++ I2S_CKIN, ++ STM32MP13_ALL_CLK_NB ++}; ++ ++struct clk *stm32mp13_clk_provided[STM32MP13_ALL_CLK_NB] = { ++ [CK_HSE] = &ck_hse, ++ [CK_CSI] = &ck_csi, ++ [CK_LSI] = &ck_lsi, ++ [CK_LSE] = &ck_lse, ++ [CK_HSI] = &ck_hsi, ++ [CK_HSE_DIV2] = &ck_hse_div2, ++ [PLL1] = &ck_pll1_vco, ++ [PLL2] = &ck_pll2_vco, ++ [PLL3] = &ck_pll3_vco, ++ [PLL4] = &ck_pll4_vco, ++ [PLL1_P] = &ck_pll1p, ++ [PLL2_P] = &ck_pll2p, ++ [PLL2_Q] = &ck_pll2q, ++ [PLL2_R] = &ck_pll2r, ++ [PLL3_P] = &ck_pll3p, ++ [PLL3_Q] = &ck_pll3q, ++ [PLL3_R] = &ck_pll3r, ++ [PLL4_P] = &ck_pll4p, ++ [PLL4_Q] = &ck_pll4q, ++ [PLL4_R] = &ck_pll4r, ++ [PLL1P_DIV] = &ck_pll1p_div, ++ [CK_MPU] = &ck_mpu, ++ [CK_AXI] = &ck_axi, ++ [CK_MLAHB] = &ck_mlahb, ++ [CK_PER] = &ck_per, ++ [PCLK1] = &ck_pclk1, ++ [PCLK2] = &ck_pclk2, ++ [PCLK3] = &ck_pclk3, ++ [PCLK4] = &ck_pclk4, ++ [PCLK5] = &ck_pclk5, ++ [PCLK6] = &ck_pclk6, ++ [CK_TIMG1] = &ck_timg1, ++ [CK_TIMG2] = &ck_timg2, ++ [CK_TIMG3] = &ck_timg3, ++ [DDRC1] = &ck_ddrc1, ++ [DDRC1LP] = &ck_ddrc1lp, ++ [DDRPHYC] = &ck_ddrphyc, ++ [DDRPHYCLP] = &ck_ddrphyclp, ++ [DDRCAPB] = &ck_ddrcapb, ++ [DDRCAPBLP] = &ck_ddrcapblp, ++ [AXIDCG] = &ck_axidcg, ++ [DDRPHYCAPB] = &ck_ddrphycapb, ++ [DDRPHYCAPBLP] = &ck_ddrphycapblp, ++ [SYSCFG] = &ck_syscfg, ++ [DDRPERFM] = &ck_ddrperfm, ++ [IWDG2] = &ck_iwdg2, ++ [USBPHY_K] = &ck_usbphy_k, ++ [USBO_K] = &ck_usbo_k, ++ [RTCAPB] = &ck_rtcapb, ++ [TZC] = &ck_tzc, ++ [TZPC] = &ck_etzpcb, ++ [IWDG1] = &ck_iwdg1apb, ++ [BSEC] = &ck_bsec, ++ [STGEN_K] = &ck_stgen_k, ++ [USART1_K] = &ck_usart1_k, ++ [USART2_K] = &ck_usart2_k, ++ [I2C4_K] = &ck_i2c4_k, ++ [TIM12_K] = &ck_tim12_k, ++ [TIM15_K] = &ck_tim15_k, ++ [RTC] = &ck_rtc, ++ [GPIOA] = &ck_gpioa, ++ [GPIOB] = &ck_gpiob, ++ [GPIOC] = &ck_gpioc, ++ [GPIOD] = &ck_gpiod, ++ [GPIOE] = &ck_gpioe, ++ [GPIOF] = &ck_gpiof, ++ [GPIOG] = &ck_gpiog, ++ [GPIOH] = &ck_gpioh, ++ [GPIOI] = &ck_gpioi, ++ [PKA] = &ck_pka, ++ [SAES_K] = &ck_saes_k, ++ [CRYP1] = &ck_cryp1, ++ [HASH1] = &ck_hash1, ++ [RNG1_K] = &ck_rng1_k, ++ [BKPSRAM] = &ck_bkpsram, ++ [SDMMC1_K] = &ck_sdmmc1_k, ++ [SDMMC2_K] = &ck_sdmmc2_k, ++ [CK_DBG] = &ck_dbg, ++ [MCE] = &ck_mce, ++ [TIM2_K] = &ck_tim2_k, ++ [TIM3_K] = &ck_tim3_k, ++ [TIM4_K] = &ck_tim4_k, ++ [TIM5_K] = &ck_tim5_k, ++ [TIM6_K] = &ck_tim6_k, ++ [TIM7_K] = &ck_tim7_k, ++ [TIM13_K] = &ck_tim13_k, ++ [TIM14_K] = &ck_tim14_k, ++ [TIM1_K] = &ck_tim1_k, ++ [TIM8_K] = &ck_tim8_k, ++ [TIM16_K] = &ck_tim16_k, ++ [TIM17_K] = &ck_tim17_k, ++ [LTDC_PX] = &ck_ltdc_px, ++ [DMA1] = &ck_dma1, ++ [DMA2] = &ck_dma2, ++ [MDMA] = &ck_mdma, ++ [ETH1MAC] = &ck_eth1mac, ++ [USBH] = &ck_usbh, ++ [VREF] = &ck_vref, ++ [TMPSENS] = &ck_tmpsens, ++ [PMBCTRL] = &ck_pmbctrl, ++ [HDP] = &ck_hdp, ++ [STGENRO] = &ck_stgenro, ++ [DMAMUX1] = &ck_dmamux1, ++ [DMAMUX2] = &ck_dmamux2, ++ [DMA3] = &ck_dma3, ++ [TSC] = &ck_tsc, ++ [AXIMC] = &ck_aximc, ++ [CRC1] = &ck_crc1, ++ [ETH1TX] = &ck_eth1tx, ++ [ETH1RX] = &ck_eth1rx, ++ [ETH2TX] = &ck_eth2tx, ++ [ETH2RX] = &ck_eth2rx, ++ [ETH2MAC] = &ck_eth2mac, ++ [USART3_K] = &ck_usart3_k, ++ [UART4_K] = &ck_uart4_k, ++ [UART5_K] = &ck_uart5_k, ++ [UART7_K] = &ck_uart7_k, ++ [UART8_K] = &ck_uart8_k, ++ [USART6_K] = &ck_usart6_k, ++ [FMC_K] = &ck_fmc_k, ++ [QSPI_K] = &ck_qspi_k, ++ [LPTIM1_K] = &ck_lptim1_k, ++ [SPI2_K] = &ck_spi2_k, ++ [SPI3_K] = &ck_spi3_k, ++ [SPDIF_K] = &ck_spdif_k, ++ [SPI1_K] = &ck_spi1_k, ++ [SPI4_K] = &ck_spi4_k, ++ [SPI5_K] = &ck_spi5_k, ++ [SAI1_K] = &ck_sai1_k, ++ [SAI2_K] = &ck_sai2_k, ++ [DFSDM_K] = &ck_dfsdm_k, ++ [FDCAN_K] = &ck_fdcan_k, ++ [I2C1_K] = &ck_i2c1_k, ++ [I2C2_K] = &ck_i2c2_k, ++ [ADFSDM_K] = &ck_adfsdm_k, ++ [LPTIM2_K] = &ck_lptim2_k, ++ [LPTIM3_K] = &ck_lptim3_k, ++ [LPTIM4_K] = &ck_lptim4_k, ++ [LPTIM5_K] = &ck_lptim5_k, ++ [I2C3_K] = &ck_i2c3_k, ++ [I2C5_K] = &ck_i2c5_k, ++ [DCMIPP_K] = &ck_dcmipp_k, ++ [ADC1_K] = &ck_adc1_k, ++ [ADC2_K] = &ck_adc2_k, ++ [ETH1CK_K] = &ck_eth1ck_k, ++ [ETH2CK_K] = &ck_eth2ck_k, ++ [CK_MCO1] = &ck_mco1, ++ [CK_MCO2] = &ck_mco2, ++ [CK_TRACE] = &ck_trace, ++ [CK_OFF] = &ck_off, ++ [USB_PHY_48] = &ck_usb_phy_48Mhz, ++ [I2S_CKIN] = &ck_i2sckin, ++}; ++ ++#ifdef CFG_PM ++static uint8_t clk_stm32_backup_mux[MUX_NB]; ++static uint32_t clk_stm32_backup_div[DIV_NB]; ++ ++static void clk_stm32_pm_save_mux(uint16_t mux_id) ++{ ++ clk_stm32_backup_mux[mux_id] = clk_stm32_get_parent_mux(mux_id); ++} ++ ++static void clk_stm32_pm_restore_mux(uint16_t mux_id) ++{ ++ clk_stm32_set_parent_mux(mux_id, clk_stm32_backup_mux[mux_id]); ++} ++ ++static void clk_stm32_pm_restore_div(uint16_t div_id) ++{ ++ clk_stm32_set_div_value(div_id, clk_stm32_backup_div[div_id]); ++} ++ ++static void clk_stm32_pm_save_div(uint16_t div_id) ++{ ++ clk_stm32_backup_div[div_id] = clk_stm32_div_get_value(div_id); ++} ++ ++static void clk_stm32_pm_force_set_all_oscillators(bool cmd) ++{ ++ size_t i = 0; ++ struct clk *clks[] = { &ck_hse, &ck_hsi, &ck_lse, ++ &ck_lsi, &ck_csi }; ++ ++ for (i = 0; i < ARRAY_SIZE(clks); i++) { ++ if (cmd) ++ clk_enable(clks[i]); ++ else ++ clk_disable(clks[i]); ++ } ++} ++ ++static void clk_stm32_pm_force_enable_all_oscillators(void) ++{ ++ clk_stm32_pm_force_set_all_oscillators(true); ++} ++ ++static void clk_stm32_pm_force_disable_all_oscillators(void) ++{ ++ clk_stm32_pm_force_set_all_oscillators(false); ++} ++ ++static void clk_stm32_pm_backup_mlahb(void) ++{ ++ clk_stm32_pm_save_mux(MUX_MLAHB); ++ clk_stm32_pm_save_div(DIV_MLAHB); ++} ++ ++static void clk_stm32_pm_restore_mlahb(void) ++{ ++ clk_stm32_pm_restore_mux(MUX_MLAHB); ++ clk_stm32_pm_restore_div(DIV_MLAHB); ++} ++ ++static uint32_t apb_iwdg1; ++static uint32_t apb_iwdg2; ++ ++static void clk_stm32_pm_backup_iwdg(void) ++{ ++ uintptr_t rcc_base = stm32_rcc_base(); ++ ++ apb_iwdg1 = (io_read32(rcc_base + RCC_MP_APB5ENSETR) & RCC_MP_APB5ENSETR_IWDG1APBEN); ++ apb_iwdg2 = (io_read32(rcc_base + RCC_MP_APB4ENSETR) & RCC_MP_APB4ENSETR_IWDG2APBEN); ++} ++ ++static void clk_stm32_pm_restore_iwdg(void) ++{ ++ uintptr_t rcc_base = stm32_rcc_base(); ++ ++ io_clrsetbits32(rcc_base + RCC_MP_APB5ENSETR, RCC_MP_APB5ENSETR_IWDG1APBEN, apb_iwdg1); ++ io_clrsetbits32(rcc_base + RCC_MP_APB4ENSETR, RCC_MP_APB4ENSETR_IWDG2APBEN, apb_iwdg2); ++} ++ ++static void clk_stm32_pm_backup_all_mux(void) ++{ ++ struct clk_stm32_priv *priv = clk_stm32_get_priv(); ++ uint32_t mux_id = 0U; ++ ++ for (mux_id = 0U; mux_id < priv->nb_muxes; mux_id++) ++ clk_stm32_backup_mux[mux_id] = clk_stm32_get_parent_mux(mux_id); ++} ++ ++static void clk_stm32_pm_restore_all_kernel_mux(void) ++{ ++ struct clk_stm32_priv *priv = clk_stm32_get_priv(); ++ uint16_t mux_id = 0U; ++ ++ for (mux_id = MUX_KERNEL_BEGIN; mux_id < priv->nb_muxes; mux_id++) ++ clk_stm32_pm_restore_mux(mux_id); ++} ++ ++static void clk_stm32_pm_backup_all_div(void) ++{ ++ struct clk_stm32_priv *priv = clk_stm32_get_priv(); ++ size_t div_id = 0; ++ ++ for (div_id = 0; div_id < priv->nb_div; div_id++) ++ clk_stm32_backup_div[div_id] = clk_stm32_div_get_value(div_id); ++} ++ ++static void clk_stm32_pm_restore_div_system(void) ++{ ++ size_t div_id = 0; ++ int div_tab[] = { DIV_MPU, ++ DIV_AXI, ++ DIV_MLAHB, ++ DIV_APB1, ++ DIV_APB2, ++ DIV_APB3, ++ DIV_APB4, ++ DIV_APB5, ++ DIV_APB6 ++ }; ++ ++ for (div_id = 0; div_id < ARRAY_SIZE(div_tab); div_id++) ++ clk_stm32_set_div_value(div_tab[div_id], ++ clk_stm32_backup_div[div_tab[div_id]]); ++} ++ ++/* Structure is used for set/clear registers and for regular registers */ ++struct backup_clock_cfg { ++ uint32_t offset; ++ uint32_t value; ++}; ++ ++static struct backup_clock_cfg backup_clock_sc_cfg[] = { ++ { .offset = RCC_MP_APB1ENSETR }, ++ { .offset = RCC_MP_APB2ENSETR }, ++ { .offset = RCC_MP_APB3ENSETR }, ++ { .offset = RCC_MP_S_APB3ENSETR }, ++ { .offset = RCC_MP_NS_APB3ENSETR }, ++ { .offset = RCC_MP_APB4ENSETR }, ++ { .offset = RCC_MP_S_APB4ENSETR }, ++ { .offset = RCC_MP_NS_APB4ENSETR }, ++ { .offset = RCC_MP_APB5ENSETR }, ++ { .offset = RCC_MP_APB6ENSETR }, ++ { .offset = RCC_MP_AHB2ENSETR }, ++ { .offset = RCC_MP_AHB4ENSETR }, ++ { .offset = RCC_MP_S_AHB4ENSETR }, ++ { .offset = RCC_MP_NS_AHB4ENSETR }, ++ { .offset = RCC_MP_AHB5ENSETR }, ++ { .offset = RCC_MP_AHB6ENSETR }, ++ { .offset = RCC_MP_S_AHB6ENSETR }, ++ { .offset = RCC_MP_NS_AHB6ENSETR }, ++}; ++ ++static struct backup_clock_cfg backup_clock_regular_cfg[] = { ++ { .offset = RCC_SECCFGR}, ++ { .offset = RCC_MCO1CFGR }, ++ { .offset = RCC_MCO2CFGR }, ++ { .offset = RCC_DBGCFGR }, ++ { .offset = RCC_DDRITFCR }, ++ { .offset = RCC_TIMG1PRER }, ++ { .offset = RCC_TIMG2PRER }, ++ { .offset = RCC_TIMG3PRER }, ++}; ++ ++static void clk_stm32_pm_backup_peripheral_clock_gating(void) ++{ ++ struct backup_clock_cfg *cfg = backup_clock_sc_cfg; ++ size_t count = ARRAY_SIZE(backup_clock_sc_cfg); ++ uintptr_t base = stm32_rcc_base(); ++ size_t i = 0; ++ ++ for (i = 0; i < count; i++) ++ cfg[i].value = io_read32(base + cfg[i].offset); ++} ++ ++static void clk_stm32_pm_restore_all_peripheral_clock_gating(void) ++{ ++ struct backup_clock_cfg *cfg = backup_clock_sc_cfg; ++ size_t count = ARRAY_SIZE(backup_clock_sc_cfg); ++ uintptr_t base = stm32_rcc_base(); ++ size_t i = 0; ++ ++ for (i = 0; i < count; i++) { ++ io_write32(base + cfg[i].offset, cfg[i].value); ++ io_write32(base + cfg[i].offset + RCC_MP_ENCLRR_OFFSET, ++ ~cfg[i].value); ++ } ++} ++ ++static void clk_stm32_pm_backup_regular_cfg(void) ++{ ++ struct backup_clock_cfg *cfg = backup_clock_regular_cfg; ++ size_t count = ARRAY_SIZE(backup_clock_regular_cfg); ++ uintptr_t base = stm32_rcc_base(); ++ size_t i = 0; ++ ++ for (i = 0; i < count; i++) ++ cfg[i].value = io_read32(base + cfg[i].offset); ++} ++ ++static void clk_stm32_pm_restore_regular_cfg(void) ++{ ++ struct backup_clock_cfg *cfg = backup_clock_regular_cfg; ++ size_t count = ARRAY_SIZE(backup_clock_regular_cfg); ++ uintptr_t base = stm32_rcc_base(); ++ size_t i = 0; ++ ++ for (i = 0; i < count; i++) ++ io_write32(base + cfg[i].offset, cfg[i].value); ++} ++ ++static void clk_stm32_pm_disable_ker_clocks(void) ++{ ++ struct clk_stm32_priv *priv = clk_stm32_get_priv(); ++ const uint32_t ker_mask = RCC_OCENR_HSIKERON | ++ RCC_OCENR_CSIKERON | ++ RCC_OCENR_HSEKERON; ++ ++ /* Disable all ck_xxx_ker clocks */ ++ io_write32(priv->base + RCC_OCENCLRR, ker_mask); ++} ++ ++static void clk_stm32_pm_enable_ker_clocks(void) ++{ ++ struct clk_stm32_priv *priv = clk_stm32_get_priv(); ++ const uint32_t ker_mask = RCC_OCENR_HSIKERON | ++ RCC_OCENR_CSIKERON | ++ RCC_OCENR_HSEKERON; ++ uint32_t reg = 0U; ++ ++ /* Enable ck_xxx_ker clocks if ck_xxx was on */ ++ reg = io_read32(priv->base + RCC_OCENSETR) << 1; ++ io_write32(priv->base + RCC_OCENSETR, reg & ker_mask); ++} ++ ++static void clear_rcc_reset_status(void) ++{ ++ /* Clear reset status fields */ ++ io_write32(stm32_rcc_base() + RCC_MP_RSTSCLRR, 0); ++} ++ ++struct stm32_pll_dt_cfg stm32_pll_backup_state[PLL_NB]; ++ ++static void clk_stm32_pm_pll_backup_status(int pll_idx) ++{ ++ struct stm32_pll_dt_cfg *pll_conf = &stm32_pll_backup_state[pll_idx]; ++ const struct stm32_clk_pll *pll = clk_stm32_pll_data(pll_idx); ++ struct clk_stm32_priv *priv = clk_stm32_get_priv(); ++ uintptr_t pll_base = priv->base + pll->reg_pllxcr; ++ uint32_t value = 0; ++ ++ value = io_read32(pll_base + RCC_OFFSET_PLLXCR); ++ value &= (RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN | RCC_PLLNCR_DIVREN | ++ RCC_PLLNCR_PLLON); ++ ++ pll_conf->vco.status = value; ++} ++ ++static void clk_stm32_pm_pll_backup_vco(struct clk_stm32_priv *priv, ++ const struct stm32_clk_pll *pll, ++ struct stm32_pll_vco *vco) ++{ ++ uintptr_t pll_base = priv->base + pll->reg_pllxcr; ++ uint32_t value = 0U; ++ uint32_t mod_per = 0U; ++ uint32_t inc_step = 0U; ++ uint32_t sscg_mode = 0U; ++ uint16_t mux_id = 0U; ++ int sel = 0; ++ ++ mux_id = pll->mux_id; ++ sel = clk_stm32_get_parent_mux(mux_id); ++ ++ vco->src = CMD_MUX << CMD_SHIFT | mux_id << MUX_ID_SHIFT | sel; ++ ++ /* Read N / M / IFREGE fields */ ++ value = io_read32(pll_base + RCC_OFFSET_PLLXCFGR1); ++ ++ vco->div_mn[PLL_CFG_M] = (value & RCC_PLLNCFGR1_DIVM_MASK) >> RCC_PLLNCFGR1_DIVM_SHIFT; ++ vco->div_mn[PLL_CFG_N] = (value & RCC_PLLNCFGR1_DIVN_MASK) >> RCC_PLLNCFGR1_DIVN_SHIFT; ++ ++ /* Read Frac */ ++ vco->frac = io_read32(pll_base + RCC_OFFSET_PLLXFRACR) & RCC_PLLNFRACR_FRACV_MASK; ++ vco->frac = vco->frac >> RCC_PLLNFRACR_FRACV_SHIFT; ++ ++ /* Read CSG */ ++ value = io_read32(pll_base + RCC_OFFSET_PLLXCSGR); ++ mod_per = (value & RCC_PLLNCSGR_MOD_PER_MASK) >> RCC_PLLNCSGR_MOD_PER_SHIFT; ++ inc_step = (value & RCC_PLLNCSGR_INC_STEP_MASK) >> RCC_PLLNCSGR_INC_STEP_SHIFT; ++ sscg_mode = (value & RCC_PLLNCSGR_SSCG_MODE_MASK) >> RCC_PLLNCSGR_SSCG_MODE_SHIFT; ++ ++ vco->csg[PLL_CSG_MOD_PER] = mod_per; ++ vco->csg[PLL_CSG_INC_STEP] = inc_step; ++ vco->csg[PLL_CSG_SSCG_MODE] = sscg_mode; ++ ++ vco->csg_enabled = io_read32(pll_base + RCC_OFFSET_PLLXCSGR) && RCC_PLLNCR_SSCG_CTRL; ++} ++ ++static void clk_stm32_pm_pll_backup_output(struct clk_stm32_priv *priv, ++ const struct stm32_clk_pll *pll, ++ struct stm32_pll_output *out) ++{ ++ uintptr_t pll_base = priv->base + pll->reg_pllxcr; ++ uint32_t value = 0; ++ ++ value = io_read32(pll_base + RCC_OFFSET_PLLXCFGR2); ++ ++ out->output[PLL_CFG_P] = (value & RCC_PLLNCFGR2_DIVP_MASK) >> RCC_PLLNCFGR2_DIVP_SHIFT; ++ out->output[PLL_CFG_Q] = (value & RCC_PLLNCFGR2_DIVQ_MASK) >> RCC_PLLNCFGR2_DIVQ_SHIFT; ++ out->output[PLL_CFG_R] = (value & RCC_PLLNCFGR2_DIVR_MASK) >> RCC_PLLNCFGR2_DIVR_SHIFT; ++} ++ ++static void stm32_clk_pm_pll_backup(int pll_idx) ++{ ++ struct stm32_pll_dt_cfg *pll_conf = &stm32_pll_backup_state[pll_idx]; ++ const struct stm32_clk_pll *pll = clk_stm32_pll_data(pll_idx); ++ struct clk_stm32_priv *priv = clk_stm32_get_priv(); ++ ++ clk_stm32_pm_pll_backup_status(pll_idx); ++ clk_stm32_pm_pll_backup_vco(priv, pll, &pll_conf->vco); ++ clk_stm32_pm_pll_backup_output(priv, pll, &pll_conf->output); ++} ++ ++static void clk_stm32_pm_backup_pll34(void) ++{ ++ int plls[] = { PLL3_ID, PLL4_ID }; ++ size_t i = 0; ++ ++ for (i = 0; i < ARRAY_SIZE(plls); i++) ++ clk_stm32_pm_pll_backup_status(plls[i]); ++ ++} ++ ++static void clk_stm32_pm_restore_pll34(void) ++{ ++ int plls[] = { PLL3_ID, PLL4_ID }; ++ size_t i = 0; ++ ++ for (i = 0; i < ARRAY_SIZE(plls); i++) { ++ int pll_id = plls[i]; ++ struct stm32_pll_dt_cfg *pll_conf = &stm32_pll_backup_state[pll_id]; ++ const struct stm32_clk_pll *pll = clk_stm32_pll_data(pll_id); ++ bool pll_status = clk_stm32_is_enabled_gate(pll->gate_id); ++ ++ if ((pll_conf->vco.status & RCC_PLLNCR_PLLON) && !pll_status) { ++ clk_stm32_gate_ready_endisable(pll->gate_id, true, true); ++ clk_stm32_pll_restore_output_diven(pll, pll_conf->vco.status); ++ } ++ } ++} ++ ++static void clk_stm32_pm_backup_all_pll(void) ++{ ++ int pll_id = 0; ++ ++ for (pll_id = PLL1_ID; pll_id < PLL_NB; pll_id++) ++ stm32_clk_pm_pll_backup(pll_id); ++} ++ ++static void clk_stm32_pm_restore_all_pll(void) ++{ ++ struct clk_stm32_priv *priv = clk_stm32_get_priv(); ++ int pll_id = 0; ++ ++ for (pll_id = PLL1_ID; pll_id < PLL_NB; pll_id++) { ++ struct stm32_pll_dt_cfg *pll_conf = &stm32_pll_backup_state[pll_id]; ++ int err = 0; ++ ++ err = clk_stm32_pll_init(priv, pll_id, pll_conf); ++ if (err) { ++ EMSG("Failed to restore PLL"); ++ panic(); ++ } ++ } ++} ++ ++static void clk_stm32_pm_restore_pll_output_status(void) ++{ ++ const uint32_t mask = RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN | RCC_PLLNCR_DIVREN; ++ struct clk_stm32_priv *priv = clk_stm32_get_priv(); ++ int pll_id = 0; ++ ++ for (pll_id = PLL1_ID; pll_id < PLL_NB; pll_id++) { ++ struct stm32_pll_dt_cfg *pll_conf = &stm32_pll_backup_state[pll_id]; ++ const struct stm32_clk_pll *pll = clk_stm32_pll_data(pll_id); ++ uintptr_t pllxcr = priv->base + pll->reg_pllxcr; ++ ++ /* If pll was on */ ++ if (pll_conf->vco.status & RCC_PLLNCR_PLLON) { ++ /* Set output */ ++ io_clrsetbits32(pllxcr, mask, pll_conf->vco.status & mask); ++ } ++ else { ++ /* Stop all output */ ++ io_clrbits32(pllxcr, RCC_PLLNCR_DIVPEN | ++ RCC_PLLNCR_DIVQEN | RCC_PLLNCR_DIVREN); ++ clk_stm32_gate_ready_endisable(pll->gate_id, false, true); ++ ++ } ++ } ++} ++ ++static void clk_stm32_pm_restore_mux_system(void) ++{ ++ clk_stm32_pm_restore_mux(MUX_MPU); ++ clk_stm32_pm_restore_mux(MUX_AXI); ++ clk_stm32_pm_restore_mux(MUX_MLAHB); ++ clk_stm32_pm_restore_mux(MUX_CKPER); ++} ++ ++void stm32mp1_clk_save_context_for_stop(void) ++{ ++ clk_stm32_pm_enable_ker_clocks(); ++ clk_stm32_pm_backup_mlahb(); ++ clk_stm32_pm_backup_pll34(); ++ clk_stm32_pm_backup_iwdg(); ++} ++ ++void stm32mp1_clk_restore_context_for_stop(void) ++{ ++ clk_stm32_pm_restore_pll34(); ++ clk_stm32_pm_restore_mlahb(); ++ clk_stm32_pm_disable_ker_clocks(); ++ clk_stm32_pm_restore_iwdg(); ++} ++ ++static void __maybe_unused stm32_clock_suspend(void) ++{ ++ clk_stm32_pm_backup_regular_cfg(); ++ clk_stm32_pm_backup_peripheral_clock_gating(); ++ clk_stm32_pm_backup_all_mux(); ++ clk_stm32_pm_backup_all_div(); ++ clk_stm32_pm_backup_all_pll(); ++ clk_stm32_pm_enable_ker_clocks(); ++ clear_rcc_reset_status(); ++} ++ ++static void __maybe_unused stm32_clock_resume(void) ++{ ++ clk_stm32_pm_force_enable_all_oscillators(); ++ clk_stm32_pm_restore_all_pll(); ++ clk_stm32_pm_restore_mux_system(); ++ clk_stm32_pm_restore_div_system(); ++ clk_stm32_pm_restore_regular_cfg(); ++ clk_stm32_pm_restore_all_kernel_mux(); ++ clk_stm32_pm_restore_all_peripheral_clock_gating(); ++ clk_stm32_pm_restore_pll_output_status(); ++ clk_stm32_pm_force_disable_all_oscillators(); ++ clk_stm32_pm_disable_ker_clocks(); ++} ++ ++static TEE_Result stm32_clock_pm(enum pm_op op, unsigned int pm_hint __unused, ++ const struct pm_callback_handle *hdl __unused) ++{ ++ static int standby_prepared; ++ ++ if (op == PM_OP_SUSPEND) { ++ if (pm_hint == STM32_PM_CSTOP_ALLOW_STANDBY_DDR_SR) { ++ stm32_clock_suspend(); ++ standby_prepared = 1; ++ } else { ++ stm32mp1_clk_save_context_for_stop(); ++ standby_prepared = 0; ++ } ++ } else { ++ /* back from standby */ ++ if (standby_prepared) ++ stm32_clock_resume(); ++ else ++ stm32mp1_clk_restore_context_for_stop(); ++ } ++ ++ return TEE_SUCCESS; ++} ++DECLARE_KEEP_PAGER(stm32_clock_pm); ++#else ++static TEE_Result stm32_clock_pm(enum pm_op op __unused, ++ unsigned int pm_hint __unused, ++ const struct pm_callback_handle *hdl __unused) ++{ ++ return TEE_ERROR_SECURITY; ++} ++ ++#endif ++ ++static bool clk_stm32_clock_is_critical(__maybe_unused struct clk *clk) ++{ ++ struct clk *clk_criticals[] = { ++ &ck_hsi, ++ &ck_hse, ++ &ck_csi, ++ &ck_lsi, ++ &ck_lse, ++ &ck_pll2r, ++ &ck_mpu, ++ &ck_ddrc1, ++ &ck_ddrc1lp, ++ &ck_ddrphyc, ++ &ck_ddrphyclp, ++ &ck_ddrcapb, ++ &ck_ddrcapblp, ++ &ck_axidcg, ++ &ck_ddrphycapb, ++ &ck_ddrphycapblp, ++ &ck_rtcapb, ++ &ck_tzc, ++ &ck_etzpcb, ++ &ck_iwdg1apb, ++ &ck_bsec, ++ &ck_stgen_k, ++ &ck_bkpsram, ++ &ck_mce, ++ &ck_mco1, ++ &ck_rng1_k ++ }; ++ size_t i = 0; ++ ++ for (i = 0; i < ARRAY_SIZE(clk_criticals); i++) { ++ struct clk *clk_critical = clk_criticals[i]; ++ ++ if (clk == clk_critical) ++ return true; ++ } ++ ++ return false; ++} ++ ++static bool clk_stm32_clock_is_ignored_unused(__maybe_unused struct clk *clk) ++{ ++ struct clk *clk_ignore_unused[] = { ++ &ck_uart4_k, ++ &ck_mco1, ++ &ck_syscfg ++ }; ++ size_t i = 0; ++ ++ for (i = 0; i < ARRAY_SIZE(clk_ignore_unused); i++) { ++ struct clk *clk_ignored_unused = clk_ignore_unused[i]; ++ ++ if (clk == clk_ignored_unused) ++ return true; ++ } ++ return false; ++} ++ ++static void clk_stm32_init_oscillators(const void *fdt, int node) ++{ ++ size_t i = 0; ++ TEE_Result __maybe_unused res = TEE_ERROR_GENERIC; ++ const char *name[] = { "clk-hse", "clk-hsi", "clk-lse", ++ "clk-lsi", "clk-csi", "clk-i2sin"}; ++ struct clk *clks[] = { &ck_hse, &ck_hsi, &ck_lse, ++ &ck_lsi, &ck_csi, &ck_i2sckin }; ++ ++ for (i = 0; i < ARRAY_SIZE(clks); i++) { ++ struct clk *clk = NULL; ++ res = clk_dt_get_by_name(fdt, node, name[i], &clk); ++ ++ DMSG("%s: %s : clk = 0x%p, res = %d, rate = %ld\n", __func__, ++ name[i], clk, res, clk_get_rate(clk)); ++ ++ clks[i]->parents[0] = clk; ++ } ++} ++ ++struct stm32_pll_dt_cfg mp13_pll[PLL_NB]; ++struct stm32_clk_opp_dt_cfg mp13_clk_opp; ++struct stm32_osci_dt_cfg mp13_osci[NB_OSCILLATOR]; ++uint32_t mp13_clksrc[MUX_NB]; ++uint32_t mp13_clkdiv[DIV_NB]; ++ ++struct stm32_clk_platdata stm32mp13_clock_pdata = { ++ .osci = mp13_osci, ++ .nosci = NB_OSCILLATOR, ++ .pll = mp13_pll, ++ .opp = &mp13_clk_opp, ++ .npll = PLL_NB, ++ .clksrc = mp13_clksrc, ++ .nclksrc = MUX_NB, ++ .clkdiv = mp13_clkdiv, ++ .nclkdiv = DIV_NB, ++}; ++ ++static struct clk_stm32_priv stm32mp13_clock_data = { ++ .muxes = parent_mp13, ++ .nb_muxes = ARRAY_SIZE(parent_mp13), ++ .gates = gates_mp13, ++ .nb_gates = ARRAY_SIZE(gates_mp13), ++ .div = dividers_mp13, ++ .nb_div = ARRAY_SIZE(dividers_mp13), ++ .pdata = &stm32mp13_clock_pdata, ++ .nb_clk_refs = STM32MP13_ALL_CLK_NB, ++ .clk_refs = stm32mp13_clk_provided, ++ .is_ignore_unused = clk_stm32_clock_is_ignored_unused, ++ .is_critical = clk_stm32_clock_is_critical, ++}; ++ ++static TEE_Result stm32mp13_clk_probe(const void *fdt, int node, ++ const void *compat_data __unused) ++{ ++ int fdt_rc = 0; ++ int rc = 0; ++ struct clk_stm32_priv *priv = &stm32mp13_clock_data; ++ struct stm32_clk_platdata *pdata = &stm32mp13_clock_pdata; ++ ++ fdt_rc = stm32_clk_parse_fdt(fdt, node, pdata); ++ if (fdt_rc) { ++ EMSG("Failed to parse clock FDT node: %d", fdt_rc); ++ return TEE_ERROR_GENERIC; ++ } ++ ++ rc = clk_stm32_init(priv, stm32_rcc_base()); ++ if (rc) ++ return TEE_ERROR_GENERIC; ++ ++#ifdef CFG_STM32_CLK_DEBUG ++ clk_stm32_debug_display_pdata(); ++#endif ++ ++ clk_stm32_init_oscillators(fdt, node); ++ rc = stm32mp1_init_clock_tree(priv, pdata); ++ if (rc) ++ return TEE_ERROR_GENERIC; ++ ++ register_pm_core_service_cb(stm32_clock_pm, NULL, "stm32-rcc_pm"); ++ stm32mp_clk_provider_probe_final(fdt, node, priv); ++ ++#ifdef CFG_STM32_CLK_DEBUG ++ clk_summary(); ++#endif ++ ++ return TEE_SUCCESS; ++} ++ ++CLK_DT_DECLARE(stm32mp13_clk, "st,stm32mp13-rcc", stm32mp13_clk_probe); ++ ++static TEE_Result stm32mp13_rcc_mco_probe(const void *fdt, int node, ++ const void *compat_data __unused) ++{ ++ struct stm32_clk_platdata *pdata = &stm32mp13_clock_pdata; ++ ++ return stm32_clk_parse_fdt_mco_pins(fdt, node, pdata); ++} ++ ++static const struct dt_device_match stm32mp13_rcc_mco_match_table[] = { ++ { .compatible = "st,stm32mp13-rcc-mco" }, ++ { } ++}; ++ ++DEFINE_DT_DRIVER(stm32mp13_rcc_mco_dt_driver) = { ++ .name = "stm32mp13_rcc_mco", ++ .match_table = stm32mp13_rcc_mco_match_table, ++ .probe = stm32mp13_rcc_mco_probe, ++}; +diff --git a/core/drivers/clk/clk-stm32mp15.c b/core/drivers/clk/clk-stm32mp15.c +index ff3f9a6ba..b4d4677af 100644 +--- a/core/drivers/clk/clk-stm32mp15.c ++++ b/core/drivers/clk/clk-stm32mp15.c +@@ -8,13 +8,17 @@ + #include + #include + #include ++#include + #include ++#include + #include + #include + #include ++#include + #include + #include + #include ++#include + #include + #include + #include +@@ -23,6 +27,26 @@ + #include + #include + ++#define DT_OPP_COMPAT "operating-points-v2" ++ ++/* PLL settings computation related definitions */ ++#define POST_DIVM_MIN 8000000 ++#define POST_DIVM_MAX 16000000 ++#define DIVM_MIN 0 ++#define DIVM_MAX 63 ++#define DIVN_MIN 24 ++#define DIVN_MAX 99 ++#define DIVP_MIN 0 ++#define DIVP_MAX 127 ++#define FRAC_MAX 8192 ++#define VCO_MIN 800000000 ++#define VCO_MAX 1600000000 ++ ++#define PLL1_SETTINGS_VALID_ID 0x504C4C31 /* "PLL1" */ ++ ++static TEE_Result stm32_clock_pm(enum pm_op op, unsigned int pm_hint __unused, ++ const struct pm_callback_handle *hdl __unused); ++ + /* Identifiers for root oscillators */ + enum stm32mp_osc_id { + OSC_HSI, +@@ -184,6 +208,16 @@ enum stm32mp1_div_id { + _DIV_NB, + }; + ++enum stm32mp1_pllcfg { ++ PLLCFG_M, ++ PLLCFG_N, ++ PLLCFG_P, ++ PLLCFG_Q, ++ PLLCFG_R, ++ PLLCFG_O, ++ PLLCFG_NB ++}; ++ + enum stm32mp1_plltype { + PLL_800, + PLL_1600, +@@ -236,6 +270,20 @@ struct stm32mp1_clk_pll { + enum stm32mp_osc_id refclk[REFCLK_SIZE]; + }; + ++struct stm32mp1_pll { ++ uint8_t refclk_min; ++ uint8_t refclk_max; ++}; ++ ++/* Compact structure of 32bit cells, copied raw when suspending */ ++struct stm32mp1_pll_settings { ++ uint32_t valid_id; ++ uint32_t freq[PLAT_MAX_OPP_NB]; ++ uint32_t volt[PLAT_MAX_OPP_NB]; ++ uint32_t cfg[PLAT_MAX_OPP_NB][PLAT_MAX_PLLCFG_NB]; ++ uint32_t frac[PLAT_MAX_OPP_NB]; ++}; ++ + #define N_S 0 /* Non-secure can access RCC interface */ + #define SEC 1 /* RCC[TZEN] protects RCC interface */ + +@@ -483,6 +531,18 @@ static const struct stm32mp1_clk_sel stm32mp1_clk_sel[_PARENT_SEL_NB] = { + _CLK_PARENT(_MCUSS_SEL, RCC_MSSCKSELR, 0, 0x3, mcuss_parents), + }; + ++/* Define characteristics of PLL according type */ ++static const struct stm32mp1_pll stm32mp1_pll[PLL_TYPE_NB] = { ++ [PLL_800] = { ++ .refclk_min = 4, ++ .refclk_max = 16, ++ }, ++ [PLL_1600] = { ++ .refclk_min = 8, ++ .refclk_max = 16, ++ }, ++}; ++ + /* PLLNCFGR2 register divider by output */ + static const uint8_t pllncfgr2[_DIV_NB] = { + [_DIV_P] = RCC_PLLNCFGR2_DIVP_SHIFT, +@@ -515,7 +575,7 @@ static const uint8_t stm32mp1_mcu_div[16] = { + 0, 1, 2, 3, 4, 6, 7, 8, 9, 9, 9, 9, 9, 9, 9, 9 + }; + +-/* div = /1 /2 /4 /8 /16 : same divider for PMU and APBX */ ++/* div = /1 /2 /4 /8 /16 : same divider for MPU and APBX */ + #define stm32mp1_mpu_div stm32mp1_mpu_apbx_div + #define stm32mp1_apbx_div stm32mp1_mpu_apbx_div + static const uint8_t stm32mp1_mpu_apbx_div[8] = { +@@ -572,6 +632,10 @@ static const char __maybe_unused *const stm32mp1_clk_parent_name[_PARENT_NB] = { + */ + static unsigned long stm32mp1_osc[NB_OSC]; + ++/* Storage of the precomputed SoC settings for PLL1 various OPPs */ ++static struct stm32mp1_pll_settings pll1_settings; ++static uint32_t current_opp_khz; ++ + static unsigned long osc_frequency(enum stm32mp_osc_id idx) + { + if (idx >= ARRAY_SIZE(stm32mp1_osc)) { +@@ -729,6 +793,159 @@ static unsigned long stm32mp1_read_pll_freq(enum stm32mp1_pll_id pll_id, + return dfout; + } + ++static void pll_start(enum stm32mp1_pll_id pll_id) ++{ ++ const struct stm32mp1_clk_pll *pll = pll_ref(pll_id); ++ uint32_t pllxcr = stm32_rcc_base() + pll->pllxcr; ++ ++ if (io_read32(pllxcr) & RCC_PLLNCR_PLLON) ++ return; ++ ++ io_clrsetbits32(pllxcr, RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN | ++ RCC_PLLNCR_DIVREN, RCC_PLLNCR_PLLON); ++} ++ ++#define PLLRDY_TIMEOUT_US (200 * 1000) ++ ++static int pll_output(enum stm32mp1_pll_id pll_id, uint32_t output) ++{ ++ const struct stm32mp1_clk_pll *pll = pll_ref(pll_id); ++ uint32_t pllxcr = stm32_rcc_base() + pll->pllxcr; ++ uint64_t start = 0; ++ ++ start = timeout_init_us(PLLRDY_TIMEOUT_US); ++ /* Wait PLL lock */ ++ while (!(io_read32(pllxcr) & RCC_PLLNCR_PLLRDY)) ++ if (timeout_elapsed(start)) ++ break; ++ ++ if (!(io_read32(pllxcr) & RCC_PLLNCR_PLLRDY)) { ++ EMSG("PLL%d start failed @ 0x%"PRIx32": 0x%"PRIx32, ++ pll_id, pllxcr, io_read32(pllxcr)); ++ ++ return -1; ++ } ++ ++ /* Start the requested output */ ++ io_setbits32(pllxcr, output << RCC_PLLNCR_DIVEN_SHIFT); ++ ++ return 0; ++} ++ ++static int pll_stop(enum stm32mp1_pll_id pll_id) ++{ ++ const struct stm32mp1_clk_pll *pll = pll_ref(pll_id); ++ uint32_t pllxcr = stm32_rcc_base() + pll->pllxcr; ++ uint64_t start = 0; ++ ++ /* Stop all output */ ++ io_clrbits32(pllxcr, RCC_PLLNCR_DIVPEN | RCC_PLLNCR_DIVQEN | ++ RCC_PLLNCR_DIVREN); ++ ++ /* Stop PLL */ ++ io_clrbits32(pllxcr, RCC_PLLNCR_PLLON); ++ ++ start = timeout_init_us(PLLRDY_TIMEOUT_US); ++ /* Wait PLL stopped */ ++ while (io_read32(pllxcr) & RCC_PLLNCR_PLLRDY) ++ if (timeout_elapsed(start)) ++ break; ++ ++ if (io_read32(pllxcr) & RCC_PLLNCR_PLLRDY) { ++ EMSG("PLL%d stop failed @ 0x%"PRIx32": 0x%"PRIx32, ++ pll_id, pllxcr, io_read32(pllxcr)); ++ ++ return -1; ++ } ++ ++ return 0; ++} ++ ++static uint32_t pll_compute_pllxcfgr2(uint32_t *pllcfg) ++{ ++ uint32_t value = 0; ++ ++ value = (pllcfg[PLLCFG_P] << RCC_PLLNCFGR2_DIVP_SHIFT) & ++ RCC_PLLNCFGR2_DIVP_MASK; ++ value |= (pllcfg[PLLCFG_Q] << RCC_PLLNCFGR2_DIVQ_SHIFT) & ++ RCC_PLLNCFGR2_DIVQ_MASK; ++ value |= (pllcfg[PLLCFG_R] << RCC_PLLNCFGR2_DIVR_SHIFT) & ++ RCC_PLLNCFGR2_DIVR_MASK; ++ ++ return value; ++} ++ ++static void pll_config_output(enum stm32mp1_pll_id pll_id, uint32_t *pllcfg) ++{ ++ const struct stm32mp1_clk_pll *pll = pll_ref(pll_id); ++ uintptr_t rcc_base = stm32_rcc_base(); ++ uint32_t value = 0; ++ ++ value = pll_compute_pllxcfgr2(pllcfg); ++ ++ io_write32(rcc_base + pll->pllxcfgr2, value); ++} ++ ++static int pll_compute_pllxcfgr1(const struct stm32mp1_clk_pll *pll, ++ uint32_t *pllcfg, uint32_t *cfgr1) ++{ ++ uint32_t rcc_base = stm32_rcc_base(); ++ enum stm32mp1_plltype type = pll->plltype; ++ unsigned long refclk = 0; ++ uint32_t ifrge = 0; ++ uint32_t src = 0; ++ ++ src = io_read32(rcc_base + pll->rckxselr) & ++ RCC_SELR_REFCLK_SRC_MASK; ++ ++ refclk = osc_frequency(pll->refclk[src]) / ++ (pllcfg[PLLCFG_M] + 1U); ++ ++ if ((refclk < (stm32mp1_pll[type].refclk_min * 1000000U)) || ++ (refclk > (stm32mp1_pll[type].refclk_max * 1000000U))) ++ return -1; ++ ++ if ((type == PLL_800) && (refclk >= 8000000U)) ++ ifrge = 1U; ++ ++ *cfgr1 = (pllcfg[PLLCFG_N] << RCC_PLLNCFGR1_DIVN_SHIFT) & ++ RCC_PLLNCFGR1_DIVN_MASK; ++ *cfgr1 |= (pllcfg[PLLCFG_M] << RCC_PLLNCFGR1_DIVM_SHIFT) & ++ RCC_PLLNCFGR1_DIVM_MASK; ++ *cfgr1 |= (ifrge << RCC_PLLNCFGR1_IFRGE_SHIFT) & ++ RCC_PLLNCFGR1_IFRGE_MASK; ++ ++ return 0; ++} ++ ++static int pll_config(enum stm32mp1_pll_id pll_id, uint32_t *pllcfg, ++ uint32_t fracv) ++{ ++ const struct stm32mp1_clk_pll *pll = pll_ref(pll_id); ++ uint32_t rcc_base = stm32_rcc_base(); ++ uint32_t value = 0; ++ int ret = 0; ++ ++ ret = pll_compute_pllxcfgr1(pll, pllcfg, &value); ++ if (ret) ++ return ret; ++ ++ io_write32(rcc_base + pll->pllxcfgr1, value); ++ ++ /* Fractional configuration */ ++ io_write32(rcc_base + pll->pllxfracr, value); ++ ++ /* Frac must be enabled only once its configuration is loaded */ ++ value = fracv << RCC_PLLNFRACR_FRACV_SHIFT; ++ io_write32(rcc_base + pll->pllxfracr, value); ++ value = io_read32(rcc_base + pll->pllxfracr); ++ io_write32(rcc_base + pll->pllxfracr, value | RCC_PLLNFRACR_FRACLE); ++ ++ pll_config_output(pll_id, pllcfg); ++ ++ return 0; ++} ++ + static unsigned long get_clock_rate(enum stm32mp1_parent_id p) + { + uint32_t reg = 0; +@@ -935,6 +1152,14 @@ static unsigned long get_clock_rate(enum stm32mp1_parent_id p) + return clock; + } + ++static bool __maybe_unused ++__clk_is_enabled(const struct stm32mp1_clk_gate *gate) ++{ ++ vaddr_t base = stm32_rcc_base(); ++ ++ return io_read32(base + gate->offset) & BIT(gate->bit); ++} ++ + static void __clk_enable(const struct stm32mp1_clk_gate *gate) + { + vaddr_t base = stm32_rcc_base(); +@@ -1147,6 +1372,16 @@ void stm32mp_register_clock_parents_secure(unsigned long clock_id) + secure_parent_clocks(parent_id); + } + ++static void save_current_opp(void) ++{ ++ unsigned long freq_khz = UDIV_ROUND_NEAREST(_stm32_clock_get_rate(CK_MPU), ++ 1000UL); ++ if (freq_khz > (unsigned long)UINT32_MAX) ++ panic(); ++ ++ current_opp_khz = (uint32_t)freq_khz; ++} ++ + #ifdef CFG_EMBED_DTB + static const char *stm32mp_osc_node_label[NB_OSC] = { + [OSC_LSI] = "clk-lsi", +@@ -1209,24 +1444,6 @@ static void get_osc_freq_from_dt(const void *fdt) + } + #endif /*CFG_EMBED_DTB*/ + +-static void enable_static_secure_clocks(void) +-{ +- unsigned int idx = 0; +- const unsigned long secure_enable[] = { +- DDRC1, DDRC1LP, DDRC2, DDRC2LP, DDRPHYC, DDRPHYCLP, DDRCAPB, +- AXIDCG, DDRPHYCAPB, DDRPHYCAPBLP, TZPC, TZC1, TZC2, STGEN_K, +- BSEC, +- }; +- +- for (idx = 0; idx < ARRAY_SIZE(secure_enable); idx++) { +- stm32_clock_enable(secure_enable[idx]); +- stm32mp_register_clock_parents_secure(secure_enable[idx]); +- } +- +- if (CFG_TEE_CORE_NB_CORE > 1) +- stm32_clock_enable(RTCAPB); +-} +- + static void __maybe_unused enable_rcc_tzen(void) + { + io_setbits32(stm32_rcc_base() + RCC_TZCR, RCC_TZCR_TZEN); +@@ -1268,14 +1485,15 @@ static TEE_Result stm32mp1_clk_fdt_init(const void *fdt, int node) + } + for (i = (enum stm32mp1_pll_id)0; i < _PLL_NB; i++) { + char name[] = "st,pll@X"; ++ int subnode = -1; + + snprintf(name, sizeof(name), "st,pll@%d", i); +- node = fdt_subnode_offset(fdt, node, name); +- if (node < 0) ++ subnode = fdt_subnode_offset(fdt, node, name); ++ if (subnode < 0) + continue; + +- if (fdt_getprop(fdt, node, "cfg", &len) || +- fdt_getprop(fdt, node, "frac", &len)) { ++ if (fdt_getprop(fdt, subnode, "cfg", &len) || ++ fdt_getprop(fdt, subnode, "frac", &len)) { + DMSG("Ignore PLL%u configurations from DT", i); + ignored++; + } +@@ -1288,6 +1506,369 @@ static TEE_Result stm32mp1_clk_fdt_init(const void *fdt, int node) + } + #endif /*CFG_EMBED_DTB*/ + ++void stm32mp1_clk_mcuss_protect(bool enable) ++{ ++ uintptr_t rcc_base = stm32_rcc_base(); ++ ++ if (enable) ++ io_setbits32(rcc_base + RCC_TZCR, RCC_TZCR_MCKPROT); ++ else ++ io_clrbits32(rcc_base + RCC_TZCR, RCC_TZCR_MCKPROT); ++} ++ ++/* ++ * Gets OPP parameters (frequency in KHz and voltage in mV) from an OPP table ++ * subnode. Platform HW support capabilities are also checked. ++ */ ++static int get_opp_freqvolt_from_dt_subnode(void *fdt, int subnode, ++ uint32_t *freq_khz, ++ uint32_t *voltage_mv) ++{ ++ const fdt64_t *cuint64 = NULL; ++ const fdt32_t *cuint32 = NULL; ++ uint64_t read_freq_64 = 0; ++ uint32_t read_voltage_32 = 0; ++ ++ assert(freq_khz); ++ assert(voltage_mv); ++ ++ cuint32 = fdt_getprop(fdt, subnode, "opp-supported-hw", NULL); ++ if (cuint32) ++ if (!stm32mp_supports_cpu_opp(fdt32_to_cpu(*cuint32))) { ++ DMSG("Invalid opp-supported-hw 0x%"PRIx32, ++ fdt32_to_cpu(*cuint32)); ++ return -FDT_ERR_BADVALUE; ++ } ++ ++ cuint64 = fdt_getprop(fdt, subnode, "opp-hz", NULL); ++ if (!cuint64) { ++ DMSG("Missing opp-hz"); ++ return -FDT_ERR_NOTFOUND; ++ } ++ ++ /* Frequency value expressed in KHz must fit on 32 bits */ ++ read_freq_64 = fdt64_to_cpu(*cuint64) / 1000ULL; ++ if (read_freq_64 > (uint64_t)UINT32_MAX) { ++ DMSG("Invalid opp-hz %"PRIu64, read_freq_64); ++ return -FDT_ERR_BADVALUE; ++ } ++ ++ cuint32 = fdt_getprop(fdt, subnode, "opp-microvolt", NULL); ++ if (!cuint32) { ++ DMSG("Missing opp-microvolt"); ++ return -FDT_ERR_NOTFOUND; ++ } ++ ++ /* Millivolt value must fit on 16 bits */ ++ read_voltage_32 = fdt32_to_cpu(*cuint32) / 1000U; ++ if (read_voltage_32 > UINT16_MAX) { ++ DMSG("Invalid opp-microvolt %"PRIu32, read_voltage_32); ++ return -FDT_ERR_BADVALUE; ++ } ++ ++ *freq_khz = (uint32_t)read_freq_64; ++ ++ *voltage_mv = read_voltage_32; ++ ++ return 0; ++} ++ ++/* ++ * Parses OPP table in DT and finds all parameters supported by the HW ++ * platform. If found, the corresponding frequency and voltage values are ++ * respectively stored in @pll1_settings structure. ++ * Note that @*count has to be set by caller to the effective size allocated ++ * for both tables. Its value is then replaced by the number of filled elements. ++ */ ++static int get_all_opp_freqvolt_from_dt(uint32_t *count) ++{ ++ void *fdt = NULL; ++ int node = 0; ++ int subnode = 0; ++ uint32_t idx = 0; ++ ++ assert(count); ++ ++ fdt = get_embedded_dt(); ++ node = fdt_node_offset_by_compatible(fdt, -1, DT_OPP_COMPAT); ++ if (node < 0) ++ return node; ++ ++ fdt_for_each_subnode(subnode, fdt, node) { ++ uint32_t read_freq = 0; ++ uint32_t read_voltage = 0; ++ ++ if (get_opp_freqvolt_from_dt_subnode(fdt, subnode, &read_freq, ++ &read_voltage)) ++ continue; ++ ++ if (idx >= *count) ++ return -FDT_ERR_NOSPACE; ++ ++ pll1_settings.freq[idx] = read_freq; ++ pll1_settings.volt[idx] = read_voltage; ++ idx++; ++ } ++ ++ if (!idx) ++ return -FDT_ERR_NOTFOUND; ++ ++ *count = idx; ++ ++ return 0; ++} ++ ++static int clk_compute_pll1_settings(unsigned long input_freq, int idx) ++{ ++ unsigned long post_divm = 0; ++ unsigned long long output_freq = pll1_settings.freq[idx] * 1000U; ++ unsigned long long freq = 0; ++ unsigned long long vco = 0; ++ int divm = 0; ++ int divn = 0; ++ int divp = 0; ++ int frac = 0; ++ int i = 0; ++ unsigned int diff = 0; ++ unsigned int best_diff = UINT_MAX; ++ ++ /* Following parameters have always the same value */ ++ pll1_settings.cfg[idx][PLLCFG_Q] = 0; ++ pll1_settings.cfg[idx][PLLCFG_R] = 0; ++ pll1_settings.cfg[idx][PLLCFG_O] = PQR(1, 0, 0); ++ ++ for (divm = DIVM_MAX; divm >= DIVM_MIN; divm--) { ++ post_divm = input_freq / (unsigned long)(divm + 1); ++ ++ if ((post_divm < POST_DIVM_MIN) || ++ (post_divm > POST_DIVM_MAX)) ++ continue; ++ ++ for (divp = DIVP_MIN; divp <= DIVP_MAX; divp++) { ++ ++ freq = output_freq * (divm + 1) * (divp + 1); ++ ++ divn = (int)((freq / input_freq) - 1); ++ if ((divn < DIVN_MIN) || (divn > DIVN_MAX)) ++ continue; ++ ++ frac = (int)(((freq * FRAC_MAX) / input_freq) - ++ ((divn + 1) * FRAC_MAX)); ++ ++ /* 2 loops to refine the fractional part */ ++ for (i = 2; i != 0; i--) { ++ if (frac > FRAC_MAX) ++ break; ++ ++ vco = (post_divm * (divn + 1)) + ++ ((post_divm * (unsigned long long)frac) / ++ FRAC_MAX); ++ ++ if ((vco < (VCO_MIN / 2)) || ++ (vco > (VCO_MAX / 2))) { ++ frac++; ++ continue; ++ } ++ ++ freq = vco / (divp + 1); ++ if (output_freq < freq) ++ diff = (unsigned int)(freq - ++ output_freq); ++ else ++ diff = (unsigned int)(output_freq - ++ freq); ++ ++ if (diff < best_diff) { ++ pll1_settings.cfg[idx][PLLCFG_M] = divm; ++ pll1_settings.cfg[idx][PLLCFG_N] = divn; ++ pll1_settings.cfg[idx][PLLCFG_P] = divp; ++ pll1_settings.frac[idx] = frac; ++ ++ if (!diff) ++ return 0; ++ ++ best_diff = diff; ++ } ++ ++ frac++; ++ } ++ } ++ } ++ ++ if (best_diff == UINT_MAX) { ++ pll1_settings.cfg[idx][PLLCFG_O] = 0; ++ return -1; ++ } ++ ++ return 0; ++} ++ ++static int clk_get_pll1_settings(uint32_t clksrc, int index) ++{ ++ unsigned long input_freq = 0; ++ unsigned int i = 0; ++ ++ for (i = 0; i < PLAT_MAX_OPP_NB; i++) ++ if (pll1_settings.freq[i] == pll1_settings.freq[index]) ++ break; ++ ++ if (((i == PLAT_MAX_OPP_NB) && ++ !stm32mp1_clk_pll1_settings_are_valid()) || ++ ((i < PLAT_MAX_OPP_NB) && !pll1_settings.cfg[i][PLLCFG_O])) { ++ /* ++ * Either PLL1 settings structure is completely empty, ++ * or these settings are not yet computed: do it. ++ */ ++ switch (clksrc) { ++ case CLK_PLL12_HSI: ++ input_freq = _stm32_clock_get_rate(CK_HSI); ++ break; ++ case CLK_PLL12_HSE: ++ input_freq = _stm32_clock_get_rate(CK_HSE); ++ break; ++ default: ++ panic(); ++ } ++ ++ return clk_compute_pll1_settings(input_freq, index); ++ } ++ ++ if (i < PLAT_MAX_OPP_NB) { ++ if (pll1_settings.cfg[i][PLLCFG_O]) ++ return 0; ++ ++ /* ++ * Index is in range and PLL1 settings are computed: ++ * use content to answer to the request. ++ */ ++ memcpy(&pll1_settings.cfg[index][0], &pll1_settings.cfg[i][0], ++ sizeof(uint32_t) * PLAT_MAX_PLLCFG_NB); ++ pll1_settings.frac[index] = pll1_settings.frac[i]; ++ ++ return 0; ++ } ++ ++ return -1; ++} ++ ++static int clk_save_current_pll1_settings(uint32_t buck1_voltage) ++{ ++ const struct stm32mp1_clk_pll *pll = pll_ref(_PLL1); ++ uint32_t rcc_base = stm32_rcc_base(); ++ uint32_t freq = 0; ++ unsigned int i = 0; ++ ++ freq = UDIV_ROUND_NEAREST(_stm32_clock_get_rate(CK_MPU), 1000L); ++ ++ for (i = 0; i < PLAT_MAX_OPP_NB; i++) ++ if (pll1_settings.freq[i] == freq) ++ break; ++ ++ if ((i == PLAT_MAX_OPP_NB) || ++ ((pll1_settings.volt[i] != buck1_voltage) && buck1_voltage)) ++ return -1; ++ ++ pll1_settings.cfg[i][PLLCFG_M] = (io_read32(rcc_base + pll->pllxcfgr1) & ++ RCC_PLLNCFGR1_DIVM_MASK) >> ++ RCC_PLLNCFGR1_DIVM_SHIFT; ++ ++ pll1_settings.cfg[i][PLLCFG_N] = (io_read32(rcc_base + pll->pllxcfgr1) & ++ RCC_PLLNCFGR1_DIVN_MASK) >> ++ RCC_PLLNCFGR1_DIVN_SHIFT; ++ ++ pll1_settings.cfg[i][PLLCFG_P] = (io_read32(rcc_base + pll->pllxcfgr2) & ++ RCC_PLLNCFGR2_DIVP_MASK) >> ++ RCC_PLLNCFGR2_DIVP_SHIFT; ++ ++ pll1_settings.cfg[i][PLLCFG_Q] = (io_read32(rcc_base + pll->pllxcfgr2) & ++ RCC_PLLNCFGR2_DIVQ_MASK) >> ++ RCC_PLLNCFGR2_DIVQ_SHIFT; ++ ++ pll1_settings.cfg[i][PLLCFG_R] = (io_read32(rcc_base + pll->pllxcfgr2) & ++ RCC_PLLNCFGR2_DIVR_MASK) >> ++ RCC_PLLNCFGR2_DIVR_SHIFT; ++ ++ pll1_settings.cfg[i][PLLCFG_O] = io_read32(rcc_base + pll->pllxcr) >> ++ RCC_PLLNCR_DIVEN_SHIFT; ++ ++ pll1_settings.frac[i] = (io_read32(rcc_base + pll->pllxfracr) & ++ RCC_PLLNFRACR_FRACV_MASK) >> ++ RCC_PLLNFRACR_FRACV_SHIFT; ++ ++ return i; ++} ++ ++static uint32_t stm32mp1_clk_get_pll1_current_clksrc(void) ++{ ++ uint32_t value = 0; ++ const struct stm32mp1_clk_pll *pll = pll_ref(_PLL1); ++ uint32_t rcc_base = stm32_rcc_base(); ++ ++ value = io_read32(rcc_base + pll->rckxselr); ++ ++ switch (value & RCC_SELR_REFCLK_SRC_MASK) { ++ case 0: ++ return CLK_PLL12_HSI; ++ case 1: ++ return CLK_PLL12_HSE; ++ default: ++ panic(); ++ } ++} ++ ++int stm32mp1_clk_compute_all_pll1_settings(uint32_t buck1_voltage) ++{ ++ unsigned int i = 0; ++ int ret = 0; ++ int index = 0; ++ uint32_t count = PLAT_MAX_OPP_NB; ++ uint32_t clksrc = 0; ++ ++ ret = get_all_opp_freqvolt_from_dt(&count); ++ switch (ret) { ++ case 0: ++ break; ++ case -FDT_ERR_NOTFOUND: ++ DMSG("Cannot find all OPP info in DT: use default settings."); ++ return 0; ++ default: ++ EMSG("Inconsistent OPP settings found in DT, ignored."); ++ return 0; ++ } ++ ++ index = clk_save_current_pll1_settings(buck1_voltage); ++ ++ clksrc = stm32mp1_clk_get_pll1_current_clksrc(); ++ ++ for (i = 0; i < count; i++) { ++ if (index >= 0 && i == (unsigned int)index) ++ continue; ++ ++ ret = clk_get_pll1_settings(clksrc, i); ++ if (ret != 0) ++ return ret; ++ } ++ ++ pll1_settings.valid_id = PLL1_SETTINGS_VALID_ID; ++ ++ return 0; ++} ++ ++void stm32mp1_clk_lp_save_opp_pll1_settings(uint8_t *data, size_t size) ++{ ++ if ((size != sizeof(pll1_settings)) || ++ !stm32mp1_clk_pll1_settings_are_valid()) ++ panic(); ++ ++ memcpy(data, &pll1_settings, size); ++} ++ ++bool stm32mp1_clk_pll1_settings_are_valid(void) ++{ ++ return pll1_settings.valid_id == PLL1_SETTINGS_VALID_ID; ++} ++ ++#ifdef CFG_DRIVERS_CLK + /* + * Conversion between clk references and clock gates and clock on internals + * +@@ -1368,6 +1949,11 @@ struct clk *stm32mp_rcc_clock_id_to_clk(unsigned long clock_id) + return clock_id_to_clk(clock_id); + } + ++unsigned int stm32mp_rcc_clk_to_clock_id(struct clk *clk) ++{ ++ return clk_to_clock_id(clk); ++} ++ + #if CFG_TEE_CORE_LOG_LEVEL >= TRACE_DEBUG + struct clk_name { + unsigned int clock_id; +@@ -1487,49 +2073,14 @@ static TEE_Result register_stm32mp1_clocks(void) + return TEE_SUCCESS; + } + +-/* Route platform legacy clock functions to clk driver functions */ +-bool stm32_clock_is_enabled(unsigned long clock_id) ++#ifdef CFG_DRIVERS_CLK_DT ++static struct clk *stm32mp1_clk_dt_get_clk(struct dt_driver_phandle_args *pargs, ++ void *data __unused, TEE_Result *res) + { +- struct clk *clk = clock_id_to_clk(clock_id); ++ unsigned long clock_id = pargs->args[0]; ++ struct clk *clk = NULL; + +- assert(clk); +- return clk_is_enabled(clk); +-} +- +-void stm32_clock_enable(unsigned long clock_id) +-{ +- struct clk *clk = clock_id_to_clk(clock_id); +- TEE_Result __maybe_unused res = TEE_ERROR_GENERIC; +- +- assert(clk); +- res = clk_enable(clk); +- assert(!res); +-} +- +-void stm32_clock_disable(unsigned long clock_id) +-{ +- struct clk *clk = clock_id_to_clk(clock_id); +- +- assert(clk); +- clk_disable(clk); +-} +- +-unsigned long stm32_clock_get_rate(unsigned long clock_id) +-{ +- struct clk *clk = clock_id_to_clk(clock_id); +- +- assert(clk); +- return clk_get_rate(clk); +-} +- +-#ifdef CFG_DRIVERS_CLK_DT +-static struct clk *stm32mp1_clk_dt_get_clk(struct dt_driver_phandle_args *pargs, +- void *data __unused, TEE_Result *res) +-{ +- unsigned long clock_id = pargs->args[0]; +- struct clk *clk = NULL; +- +- *res = TEE_ERROR_BAD_PARAMETERS; ++ *res = TEE_ERROR_BAD_PARAMETERS; + + if (pargs->args_count != 1) + return NULL; +@@ -1545,6 +2096,24 @@ static struct clk *stm32mp1_clk_dt_get_clk(struct dt_driver_phandle_args *pargs, + /* Non-null reference for compat data */ + static const uint8_t non_secure_rcc; + ++static void enable_static_secure_clocks(void) ++{ ++ unsigned int idx = 0; ++ const unsigned long secure_enable[] = { ++ DDRC1, DDRC1LP, DDRC2, DDRC2LP, DDRPHYC, DDRPHYCLP, DDRCAPB, ++ AXIDCG, DDRPHYCAPB, DDRPHYCAPBLP, TZPC, TZC1, TZC2, STGEN_K, ++ BSEC, ++ }; ++ ++ for (idx = 0; idx < ARRAY_SIZE(secure_enable); idx++) { ++ clk_enable(stm32mp_rcc_clock_id_to_clk(secure_enable[idx])); ++ stm32mp_register_clock_parents_secure(secure_enable[idx]); ++ } ++ ++ if (CFG_TEE_CORE_NB_CORE > 1) ++ clk_enable(stm32mp_rcc_clock_id_to_clk(RTCAPB)); ++} ++ + static TEE_Result stm32mp1_clock_provider_probe(const void *fdt, int offs, + const void *compat_data) + { +@@ -1575,6 +2144,10 @@ static TEE_Result stm32mp1_clock_provider_probe(const void *fdt, int offs, + } + + enable_static_secure_clocks(); ++ save_current_opp(); ++ ++ register_pm_core_service_cb(stm32_clock_pm, NULL, ++ "stm32mp15-clk-service"); + + return TEE_SUCCESS; + } +@@ -1592,6 +2165,52 @@ DEFINE_DT_DRIVER(stm32mp1_clock_dt_driver) = { + .probe = stm32mp1_clock_provider_probe, + }; + #else /*CFG_DRIVERS_CLK_DT*/ ++static void stm32_clock_enable(unsigned long clock_id) ++{ ++ int index = clock_id_to_gate_index(clock_id); ++ ++ if (index >= 0) ++ __clk_enable(stm32mp1_clk_gate + index); ++ ++ assert(clock_id_to_always_on_index(clock_id) >= 0); ++} ++ ++static void enable_static_secure_clocks(void) ++{ ++ unsigned int idx = 0; ++ const unsigned long secure_enable[] = { ++ DDRC1, DDRC1LP, DDRC2, DDRC2LP, DDRPHYC, DDRPHYCLP, DDRCAPB, ++ AXIDCG, DDRPHYCAPB, DDRPHYCAPBLP, TZPC, TZC1, TZC2, STGEN_K, ++ BSEC, ++ }; ++ for (idx = 0; idx < ARRAY_SIZE(secure_enable); idx++) { ++ stm32_clock_enable(secure_enable[idx]); ++ stm32mp_register_clock_parents_secure(secure_enable[idx]); ++ } ++ ++ if (CFG_TEE_CORE_NB_CORE > 1) ++ stm32_clock_enable(RTCAPB); ++} ++ ++int stm32mp1_clk_compute_all_pll1_settings(uint32_t buck1_voltage __unused) ++{ ++ return 0; ++} ++ ++void stm32mp1_clk_lp_save_opp_pll1_settings(uint8_t *data __unused, ++ size_t size __unused) ++{ ++} ++ ++bool stm32mp1_clk_pll1_settings_are_valid(void) ++{ ++ return false; ++} ++ ++static void enable_static_secure_clocks(void) ++{ ++} ++ + static TEE_Result stm32mp1_clk_early_init(void) + { + TEE_Result __maybe_unused res = TEE_ERROR_GENERIC; +@@ -1603,9 +2222,681 @@ static TEE_Result stm32mp1_clk_early_init(void) + } + + enable_static_secure_clocks(); ++ save_current_opp(); + + return TEE_SUCCESS; + } + + service_init(stm32mp1_clk_early_init); +-#endif /*CFG_DRIVERS_CLK_DT*/ ++#endif /* CFG_DRIVERS_CLK_DT */ ++ ++#endif /* CFG_DRIVERS_CLK */ ++ ++/* Start MPU OPP */ ++#define CLKSRC_TIMEOUT_US (200 * 1000) ++#define CLKDIV_TIMEOUT_US (200 * 1000) ++#define CLK_MPU_PLL1P 0x00000202 ++#define CLK_MPU_PLL1P_DIV 0x00000203 ++ ++static int stm32mp1_set_clksrc(unsigned int clksrc) ++{ ++ uintptr_t address = stm32_rcc_base() + (clksrc >> 4); ++ uint64_t timeout_ref = 0; ++ ++ io_clrsetbits32(address, RCC_SELR_SRC_MASK, clksrc & RCC_SELR_SRC_MASK); ++ ++ timeout_ref = timeout_init_us(CLKSRC_TIMEOUT_US); ++ while (!(io_read32(address) & RCC_SELR_SRCRDY)) ++ if (timeout_elapsed(timeout_ref)) ++ break; ++ ++ if (!(io_read32(address) & RCC_SELR_SRCRDY)) { ++ EMSG("CLKSRC %u start failed @ 0x%"PRIxPTR": 0x%"PRIx32, ++ clksrc, address, io_read32(address)); ++ ++ return -1; ++ } ++ ++ return 0; ++} ++ ++static int stm32mp1_set_clkdiv(unsigned int clkdiv, uintptr_t address) ++{ ++ uint64_t timeout_ref = 0; ++ ++ io_clrsetbits32(address, RCC_DIVR_DIV_MASK, clkdiv & RCC_DIVR_DIV_MASK); ++ ++ timeout_ref = timeout_init_us(CLKDIV_TIMEOUT_US); ++ while (!(io_read32(address) & RCC_DIVR_DIVRDY)) ++ if (timeout_elapsed(timeout_ref)) ++ break; ++ ++ if (!(io_read32(address) & RCC_DIVR_DIVRDY)) { ++ EMSG("CLKDIV 0x%x start failed @ 0x%"PRIxPTR": 0x%"PRIx32, ++ clkdiv, address, io_read32(address)); ++ ++ return -1; ++ } ++ ++ return 0; ++} ++ ++/* ++ * Check if PLL1 can be configured on the fly. ++ * @result (-1) => config on the fly is not possible. ++ * (0) => config on the fly is possible. ++ * (+1) => same parameters as those in place, no need to reconfig. ++ * Return value is 0 if no error. ++ */ ++static int is_pll_config_on_the_fly(enum stm32mp1_pll_id pll_id, ++ uint32_t *pllcfg, uint32_t fracv, ++ int *result) ++{ ++ const struct stm32mp1_clk_pll *pll = pll_ref(pll_id); ++ uintptr_t rcc_base = stm32_rcc_base(); ++ uint32_t fracr = 0; ++ uint32_t value = 0; ++ int ret = 0; ++ ++ ret = pll_compute_pllxcfgr1(pll, pllcfg, &value); ++ if (ret) ++ return ret; ++ ++ if (io_read32(rcc_base + pll->pllxcfgr1) != value) { ++ /* Different DIVN/DIVM, can't config on the fly */ ++ *result = -1; ++ return 0; ++ } ++ ++ *result = true; ++ ++ fracr = fracv << RCC_PLLNFRACR_FRACV_SHIFT; ++ fracr |= RCC_PLLNFRACR_FRACLE; ++ value = pll_compute_pllxcfgr2(pllcfg); ++ ++ if ((io_read32(rcc_base + pll->pllxfracr) == fracr) && ++ (io_read32(rcc_base + pll->pllxcfgr2) == value)) ++ /* Same parameters, no need to config */ ++ *result = 1; ++ else ++ *result = 0; ++ ++ return 0; ++} ++ ++static int stm32mp1_get_mpu_div(uint32_t freq_khz) ++{ ++ unsigned long freq_pll1_p; ++ unsigned long div; ++ ++ freq_pll1_p = get_clock_rate(_PLL1_P) / 1000UL; ++ if ((freq_pll1_p % freq_khz) != 0U) ++ return -1; ++ ++ div = freq_pll1_p / freq_khz; ++ ++ switch (div) { ++ case 1UL: ++ case 2UL: ++ case 4UL: ++ case 8UL: ++ case 16UL: ++ return __builtin_ffs(div) - 1; ++ default: ++ return -1; ++ } ++} ++ ++/* Configure PLL1 from input frequency OPP parameters */ ++static int pll1_config_from_opp_khz(uint32_t freq_khz) ++{ ++ unsigned int idx = 0; ++ int ret = 0; ++ int div = 0; ++ int config_on_the_fly = -1; ++ ++ for (idx = 0; idx < PLAT_MAX_OPP_NB; idx++) ++ if (pll1_settings.freq[idx] == freq_khz) ++ break; ++ ++ if (idx == PLAT_MAX_OPP_NB) ++ return -1; ++ ++ div = stm32mp1_get_mpu_div(freq_khz); ++ switch (div) { ++ case -1: ++ break; ++ case 0: ++ return stm32mp1_set_clksrc(CLK_MPU_PLL1P); ++ default: ++ ret = stm32mp1_set_clkdiv(div, stm32_rcc_base() + ++ RCC_MPCKDIVR); ++ if (ret == 0) ++ ret = stm32mp1_set_clksrc(CLK_MPU_PLL1P_DIV); ++ ++ return ret; ++ } ++ ++ ret = is_pll_config_on_the_fly(_PLL1, &pll1_settings.cfg[idx][0], ++ pll1_settings.frac[idx], ++ &config_on_the_fly); ++ if (ret) ++ return ret; ++ ++ if (config_on_the_fly == 1) ++ return 0; ++ ++ if (config_on_the_fly == -1) { ++ /* Switch to HSI and stop PLL1 before reconfiguration */ ++ ret = stm32mp1_set_clksrc(CLK_MPU_HSI); ++ if (ret) ++ return ret; ++ ++ ret = pll_stop(_PLL1); ++ if (ret) ++ return ret; ++ } ++ ++ ret = pll_config(_PLL1, &pll1_settings.cfg[idx][0], ++ pll1_settings.frac[idx]); ++ if (ret) ++ return ret; ++ ++ if (config_on_the_fly == -1) { ++ /* Start PLL1 and switch back to after reconfiguration */ ++ pll_start(_PLL1); ++ ++ ret = pll_output(_PLL1, pll1_settings.cfg[idx][PLLCFG_O]); ++ if (ret) ++ return ret; ++ ++ ret = stm32mp1_set_clksrc(CLK_MPU_PLL1P); ++ if (ret) ++ return ret; ++ } ++ ++ return 0; ++} ++ ++TEE_Result stm32mp1_set_opp_khz(uint32_t freq_khz) ++{ ++ uint32_t mpu_src = 0; ++ ++ if (freq_khz == current_opp_khz) ++ return TEE_SUCCESS; ++ ++ if (!stm32mp1_clk_pll1_settings_are_valid()) { ++ /* ++ * No OPP table in DT or an error occurred during PLL1 ++ * settings computation, system can only work on current ++ * operating point so return error. ++ */ ++ return TEE_ERROR_NO_DATA; ++ } ++ ++ /* Check that PLL1 is MPU clock source */ ++ mpu_src = io_read32(stm32_rcc_base() + RCC_MPCKSELR) & ++ RCC_SELR_SRC_MASK; ++ if ((mpu_src != RCC_MPCKSELR_PLL) && ++ (mpu_src != RCC_MPCKSELR_PLL_MPUDIV)) ++ return TEE_ERROR_BAD_STATE; ++ ++ if (pll1_config_from_opp_khz(freq_khz)) { ++ /* Restore original value */ ++ if (pll1_config_from_opp_khz(current_opp_khz)) { ++ EMSG("No CPU operating point can be set"); ++ panic(); ++ } ++ ++ return TEE_ERROR_GENERIC; ++ } ++ ++ current_opp_khz = freq_khz; ++ ++ return TEE_SUCCESS; ++} ++ ++int stm32mp1_round_opp_khz(uint32_t *freq_khz) ++{ ++ unsigned int i = 0; ++ uint32_t round_opp = 0; ++ ++ if (!stm32mp1_clk_pll1_settings_are_valid()) { ++ /* ++ * No OPP table in DT, or an error occurred during PLL1 ++ * settings computation, system can only work on current ++ * operating point, so return current CPU frequency. ++ */ ++ *freq_khz = current_opp_khz; ++ ++ return 0; ++ } ++ ++ for (i = 0; i < PLAT_MAX_OPP_NB; i++) ++ if ((pll1_settings.freq[i] <= *freq_khz) && ++ (pll1_settings.freq[i] > round_opp)) ++ round_opp = pll1_settings.freq[i]; ++ ++ *freq_khz = round_opp; ++ ++ return 0; ++} ++/* End MPU OPP */ ++ ++struct soc_stop_context { ++ uint32_t pll3cr; ++ uint32_t pll4cr; ++ uint32_t mssckselr; ++ uint32_t mcudivr; ++}; ++ ++static struct soc_stop_context soc_stop_ctx; ++ ++static void save_pll34_state(void) ++{ ++ uintptr_t rcc_base = stm32_rcc_base(); ++ struct soc_stop_context *ctx = &soc_stop_ctx; ++ ++ ctx->pll3cr = io_read32(rcc_base + RCC_PLL3CR); ++ ctx->pll4cr = io_read32(rcc_base + RCC_PLL4CR); ++} ++ ++static void save_mcu_subsys_clocks(void) ++{ ++ uintptr_t rcc_base = stm32_rcc_base(); ++ struct soc_stop_context *ctx = &soc_stop_ctx; ++ ++ ctx->mssckselr = io_read32(rcc_base + RCC_MSSCKSELR); ++ ctx->mcudivr = io_read32(rcc_base + RCC_MCUDIVR) & ++ RCC_MCUDIV_MASK; ++} ++ ++static void restore_pll34_state(void) ++{ ++ struct soc_stop_context *ctx = &soc_stop_ctx; ++ ++ /* Let PLL4 start while we're starting and waiting for PLL3 */ ++ if (ctx->pll4cr & RCC_PLLNCR_PLLON) ++ pll_start(_PLL4); ++ ++ if (ctx->pll3cr & RCC_PLLNCR_PLLON) { ++ pll_start(_PLL3); ++ if (pll_output(_PLL3, ctx->pll3cr >> RCC_PLLNCR_DIVEN_SHIFT)) { ++ EMSG("Failed to restore PLL3"); ++ panic(); ++ } ++ } ++ ++ if (ctx->pll4cr & RCC_PLLNCR_PLLON) { ++ if (pll_output(_PLL4, ctx->pll4cr >> RCC_PLLNCR_DIVEN_SHIFT)) { ++ EMSG("Failed to restore PLL4"); ++ panic(); ++ } ++ } ++} ++ ++static void restore_mcu_subsys_clocks(void) ++{ ++ uintptr_t rcc_base = stm32_rcc_base(); ++ struct soc_stop_context *ctx = &soc_stop_ctx; ++ ++ io_write32(rcc_base + RCC_MSSCKSELR, ctx->mssckselr); ++ ++ if (stm32mp1_set_clkdiv(ctx->mcudivr, rcc_base + RCC_MCUDIVR)) { ++ EMSG("Failed to restore MCUDIVR"); ++ panic(); ++ } ++} ++ ++/* ++ * Sequence to save/restore the non-secure configuration. ++ * Restoring clocks and muxes need IPs to run on kernel clock ++ * hence on configuration is restored at resume, kernel clock ++ * should be disable: this mandates secure access. ++ * ++ * backup_mux*_cfg for the clock muxes. ++ * backup_clock_sc_cfg for the set/clear clock gating registers ++ * backup_clock_cfg for the regular full write registers ++ */ ++ ++struct backup_mux_cfg { ++ uint16_t offset; ++ uint8_t value; ++ uint8_t bit_len; ++}; ++ ++#define MUXCFG(_offset, _bit_len) \ ++ { .offset = (_offset), .bit_len = (_bit_len) } ++ ++struct backup_mux_cfg backup_mux0_cfg[] = { ++ MUXCFG(RCC_SDMMC12CKSELR, 3), ++ MUXCFG(RCC_SPI2S23CKSELR, 3), ++ MUXCFG(RCC_SPI45CKSELR, 3), ++ MUXCFG(RCC_I2C12CKSELR, 3), ++ MUXCFG(RCC_I2C35CKSELR, 3), ++ MUXCFG(RCC_LPTIM23CKSELR, 3), ++ MUXCFG(RCC_LPTIM45CKSELR, 3), ++ MUXCFG(RCC_UART24CKSELR, 3), ++ MUXCFG(RCC_UART35CKSELR, 3), ++ MUXCFG(RCC_UART78CKSELR, 3), ++ MUXCFG(RCC_SAI1CKSELR, 3), ++ MUXCFG(RCC_ETHCKSELR, 2), ++ MUXCFG(RCC_I2C46CKSELR, 3), ++ MUXCFG(RCC_RNG2CKSELR, 2), ++ MUXCFG(RCC_SDMMC3CKSELR, 3), ++ MUXCFG(RCC_FMCCKSELR, 2), ++ MUXCFG(RCC_QSPICKSELR, 2), ++ MUXCFG(RCC_USBCKSELR, 2), ++ MUXCFG(RCC_SPDIFCKSELR, 2), ++ MUXCFG(RCC_SPI2S1CKSELR, 3), ++ MUXCFG(RCC_CECCKSELR, 2), ++ MUXCFG(RCC_LPTIM1CKSELR, 3), ++ MUXCFG(RCC_UART6CKSELR, 3), ++ MUXCFG(RCC_FDCANCKSELR, 2), ++ MUXCFG(RCC_SAI2CKSELR, 3), ++ MUXCFG(RCC_SAI3CKSELR, 3), ++ MUXCFG(RCC_SAI4CKSELR, 3), ++ MUXCFG(RCC_ADCCKSELR, 2), ++ MUXCFG(RCC_DSICKSELR, 1), ++ MUXCFG(RCC_CPERCKSELR, 2), ++ MUXCFG(RCC_RNG1CKSELR, 2), ++ MUXCFG(RCC_STGENCKSELR, 2), ++ MUXCFG(RCC_UART1CKSELR, 3), ++ MUXCFG(RCC_SPI6CKSELR, 3), ++}; ++ ++struct backup_mux_cfg backup_mux4_cfg[] = { ++ MUXCFG(RCC_USBCKSELR, 1), ++}; ++ ++static void backup_mux_cfg(void) ++{ ++ struct backup_mux_cfg *cfg = backup_mux0_cfg; ++ size_t count = ARRAY_SIZE(backup_mux0_cfg); ++ size_t i = 0; ++ uintptr_t base = stm32_rcc_base(); ++ ++ for (i = 0; i < count; i++) ++ cfg[i].value = io_read32(base + cfg[i].offset) & ++ GENMASK_32(cfg[i].bit_len - 1, 0); ++ ++ cfg = backup_mux4_cfg; ++ count = ARRAY_SIZE(backup_mux4_cfg); ++ ++ for (i = 0; i < count; i++) ++ cfg[i].value = io_read32(base + cfg[i].offset) & ++ GENMASK_32(4 + cfg[i].bit_len - 1, 4); ++} ++ ++static void restore_mux_cfg(void) ++{ ++ struct backup_mux_cfg *cfg = backup_mux0_cfg; ++ size_t count = ARRAY_SIZE(backup_mux0_cfg); ++ size_t i = 0; ++ uintptr_t base = stm32_rcc_base(); ++ ++ for (i = 0; i < count; i++) ++ io_clrsetbits32(base + cfg[i].offset, ++ GENMASK_32(cfg[i].bit_len - 1, 0), ++ cfg[i].value); ++ ++ cfg = backup_mux4_cfg; ++ count = ARRAY_SIZE(backup_mux4_cfg); ++ ++ for (i = 0; i < count; i++) ++ io_clrsetbits32(base + cfg[i].offset, ++ GENMASK_32(4 + cfg[i].bit_len - 1, 4), ++ cfg[i].value); ++} ++ ++/* Structure is used for set/clear registers and for regular registers */ ++struct backup_clock_cfg { ++ uint32_t offset; ++ uint32_t value; ++}; ++ ++static struct backup_clock_cfg backup_clock_sc_cfg[] = { ++ { .offset = RCC_MP_APB1ENSETR }, ++ { .offset = RCC_MP_APB2ENSETR }, ++ { .offset = RCC_MP_APB3ENSETR }, ++ { .offset = RCC_MP_APB4ENSETR }, ++ { .offset = RCC_MP_APB5ENSETR }, ++ { .offset = RCC_MP_AHB2ENSETR }, ++ { .offset = RCC_MP_AHB3ENSETR }, ++ { .offset = RCC_MP_AHB4ENSETR }, ++ { .offset = RCC_MP_AHB5ENSETR }, ++ { .offset = RCC_MP_AHB6ENSETR }, ++ { .offset = RCC_MP_MLAHBENSETR }, ++}; ++ ++static struct backup_clock_cfg backup_clock_cfg[] = { ++ { .offset = RCC_TZCR}, ++ { .offset = RCC_MCO1CFGR }, ++ { .offset = RCC_MCO2CFGR }, ++ { .offset = RCC_PLL3CR }, ++ { .offset = RCC_PLL4CR }, ++ { .offset = RCC_PLL4CFGR2 }, ++ { .offset = RCC_MCUDIVR }, ++ { .offset = RCC_MSSCKSELR }, ++}; ++ ++static void backup_sc_cfg(void) ++{ ++ struct backup_clock_cfg *cfg = backup_clock_sc_cfg; ++ size_t count = ARRAY_SIZE(backup_clock_sc_cfg); ++ size_t i = 0; ++ uintptr_t base = stm32_rcc_base(); ++ ++ for (i = 0; i < count; i++) ++ cfg[i].value = io_read32(base + cfg[i].offset); ++} ++ ++static void restore_sc_cfg(void) ++{ ++ struct backup_clock_cfg *cfg = backup_clock_sc_cfg; ++ size_t count = ARRAY_SIZE(backup_clock_sc_cfg); ++ size_t i = 0; ++ uintptr_t base = stm32_rcc_base(); ++ ++ for (i = 0; i < count; i++) { ++ io_write32(base + cfg[i].offset, cfg[i].value); ++ io_write32(base + cfg[i].offset + RCC_MP_ENCLRR_OFFSET, ++ ~cfg[i].value); ++ } ++} ++ ++static void backup_regular_cfg(void) ++{ ++ struct backup_clock_cfg *cfg = backup_clock_cfg; ++ size_t count = ARRAY_SIZE(backup_clock_cfg); ++ size_t i = 0; ++ uintptr_t base = stm32_rcc_base(); ++ ++ for (i = 0; i < count; i++) ++ cfg[i].value = io_read32(base + cfg[i].offset); ++} ++ ++static void restore_regular_cfg(void) ++{ ++ struct backup_clock_cfg *cfg = backup_clock_cfg; ++ size_t count = ARRAY_SIZE(backup_clock_cfg); ++ size_t i = 0; ++ uintptr_t base = stm32_rcc_base(); ++ ++ for (i = 0; i < count; i++) ++ io_write32(base + cfg[i].offset, cfg[i].value); ++} ++ ++static void disable_kernel_clocks(void) ++{ ++ const uint32_t ker_mask = RCC_OCENR_HSIKERON | ++ RCC_OCENR_CSIKERON | ++ RCC_OCENR_HSEKERON; ++ ++ /* Disable all ck_xxx_ker clocks */ ++ io_write32(stm32_rcc_base() + RCC_OCENCLRR, ker_mask); ++} ++ ++static void enable_kernel_clocks(void) ++{ ++ uintptr_t rcc_base = stm32_rcc_base(); ++ uint32_t reg = 0; ++ const uint32_t ker_mask = RCC_OCENR_HSIKERON | ++ RCC_OCENR_CSIKERON | ++ RCC_OCENR_HSEKERON; ++ ++ /* Enable ck_xxx_ker clocks if ck_xxx was on */ ++ reg = io_read32(rcc_base + RCC_OCENSETR) << 1; ++ io_write32(rcc_base + RCC_OCENSETR, reg & ker_mask); ++} ++ ++static void clear_rcc_reset_status(void) ++{ ++ /* Clear reset status fields */ ++ io_write32(stm32_rcc_base() + RCC_MP_RSTSCLRR, 0); ++} ++ ++void stm32mp1_clk_save_context_for_stop(void) ++{ ++ enable_kernel_clocks(); ++ save_mcu_subsys_clocks(); ++ save_pll34_state(); ++} ++ ++void stm32mp1_clk_restore_context_for_stop(void) ++{ ++ restore_pll34_state(); ++ /* Restore MCU clock source after PLL3 is ready */ ++ restore_mcu_subsys_clocks(); ++ disable_kernel_clocks(); ++} ++ ++static bool backup_clock_nsec[NB_GATES]; ++ ++static void sync_non_secure_clk_state(void) ++{ ++ unsigned int idx = 0; ++ ++ for (idx = 0; idx < NB_GATES; idx++) { ++ struct stm32mp1_clk_gate const *gate = gate_ref(idx); ++ struct clk *clk = NULL; ++ ++ clk = clock_id_to_clk(gate->clock_id); ++ assert(clk); ++ ++ if (__clk_is_enabled(gate) && !clk_is_enabled(clk)) ++ backup_clock_nsec[idx] = true; ++ else ++ backup_clock_nsec[idx] = false; ++ } ++} ++ ++static void stm32_clock_suspend(void) ++{ ++ sync_non_secure_clk_state(); ++ backup_regular_cfg(); ++ backup_sc_cfg(); ++ backup_mux_cfg(); ++ save_pll34_state(); ++ ++ enable_kernel_clocks(); ++ clear_rcc_reset_status(); ++} ++ ++static void stm32_clock_resume(void) ++{ ++ unsigned int idx = 0; ++ ++ restore_pll34_state(); ++ restore_mux_cfg(); ++ restore_sc_cfg(); ++ restore_regular_cfg(); ++ ++ /* Sync secure and shared clocks physical state on functional state */ ++ for (idx = 0; idx < NB_GATES; idx++) { ++ struct stm32mp1_clk_gate const *gate = gate_ref(idx); ++ struct clk *clk = NULL; ++ ++ if (backup_clock_nsec[idx]) ++ continue; ++ ++ clk = clock_id_to_clk(gate->clock_id); ++ assert(clk); ++ ++ if (clk_is_enabled(clk)) { ++ DMSG("Force clock %d enable", gate->clock_id); ++ __clk_enable(gate); ++ } else { ++ DMSG("Force clock %d disable", gate->clock_id); ++ __clk_disable(gate); ++ } ++ } ++ ++ disable_kernel_clocks(); ++} ++ ++static TEE_Result stm32_clock_pm(enum pm_op op, unsigned int pm_hint __unused, ++ const struct pm_callback_handle *hdl __unused) ++{ ++ if (op == PM_OP_SUSPEND) { ++ stm32_clock_suspend(); ++ } else { ++ pll1_config_from_opp_khz(current_opp_khz); ++ stm32_clock_resume(); ++ } ++ ++ return TEE_SUCCESS; ++} ++DECLARE_KEEP_PAGER(stm32_clock_pm); ++ ++#ifndef CFG_DRIVERS_CLK ++/* ++ * Minimial clock support when there's no embedded DTB. ++ * Implements legacy stm32_clock_*() function that only enables target clocks. ++ */ ++bool stm32_clock_is_enabled(unsigned long clock_id) ++{ ++ int index = clock_id_to_gate_index(clock_id); ++ ++ if (index >= 0) ++ return __clk_is_enabled(stm32mp1_clk_gate + index); ++ ++ assert(clock_id_to_always_on_index(clock_id) >= 0); ++ return true; ++} ++ ++void stm32_clock_enable(unsigned long clock_id) ++{ ++ int index = clock_id_to_gate_index(clock_id); ++ ++ if (index >= 0) ++ __clk_enable(stm32mp1_clk_gate + index); ++ ++ assert(clock_id_to_always_on_index(clock_id) >= 0); ++} ++ ++void stm32_clock_disable(unsigned long clock_id __unused) ++{ ++ /* No refcounting: do not disable the clock */ ++} ++#endif /*!CFG_DRIVERS_CLK*/ ++ ++static TEE_Result stm32mp1_rcc_mco_probe(const void *fdt, int node, ++ const void *compat_data __unused) ++{ ++ static struct stm32_pinctrl_list *pinctrl_cfg = NULL; ++ ++ /* Get pinctrl config loaded */ ++ return stm32_pinctrl_dt_get_by_index(fdt, node, 0, &pinctrl_cfg); ++} ++ ++static const struct dt_device_match stm32mp1_rcc_mco_match_table[] = { ++ { .compatible = "st,stm32mp1-rcc-mco" }, ++ { } ++}; ++ ++DEFINE_DT_DRIVER(stm32mp1_rcc_mco_dt_driver) = { ++ .name = "stm32mp1_rcc_mco", ++ .match_table = stm32mp1_rcc_mco_match_table, ++ .probe = stm32mp1_rcc_mco_probe, ++}; +diff --git a/core/drivers/clk/clk.c b/core/drivers/clk/clk.c +index 0d15095c3..2a4b24bea 100644 +--- a/core/drivers/clk/clk.c ++++ b/core/drivers/clk/clk.c +@@ -10,10 +10,14 @@ + #include + #include + #include ++#include + + /* Global clock tree lock */ + static unsigned int clk_lock = SPINLOCK_UNLOCK; + ++static STAILQ_HEAD(, clk) clock_list = ++ STAILQ_HEAD_INITIALIZER(clock_list); ++ + struct clk *clk_alloc(const char *name, const struct clk_ops *ops, + struct clk **parent_clks, size_t parent_count) + { +@@ -103,6 +107,8 @@ TEE_Result clk_register(struct clk *clk) + + DMSG("Registered clock %s, freq %lu", clk->name, clk_get_rate(clk)); + ++ STAILQ_INSERT_TAIL(&clock_list, clk, link); ++ + return TEE_SUCCESS; + } + +@@ -184,44 +190,21 @@ void clk_disable(struct clk *clk) + + unsigned long clk_get_rate(struct clk *clk) + { +- return clk->rate; +-} +- +-static TEE_Result clk_set_rate_no_lock(struct clk *clk, unsigned long rate) +-{ +- TEE_Result res = TEE_ERROR_GENERIC; +- unsigned long parent_rate = 0; +- +- if (clk->parent) +- parent_rate = clk_get_rate(clk->parent); +- +- res = clk->ops->set_rate(clk, rate, parent_rate); +- if (res) +- return res; +- +- clk_compute_rate_no_lock(clk); +- +- return TEE_SUCCESS; +-} +- +-TEE_Result clk_set_rate(struct clk *clk, unsigned long rate) +-{ +- uint32_t exceptions = 0; +- TEE_Result res = TEE_ERROR_GENERIC; ++ unsigned long rate = clk->rate; + +- if (!clk->ops->set_rate) +- return TEE_ERROR_NOT_SUPPORTED; ++ if (clk->ops->get_rate) { ++ unsigned long parent_rate = 0UL; + +- exceptions = cpu_spin_lock_xsave(&clk_lock); ++ if (clk->parent) ++ parent_rate = clk_get_rate(clk->parent); + +- if (clk->flags & CLK_SET_RATE_GATE && clk_is_enabled_no_lock(clk)) +- res = TEE_ERROR_BAD_STATE; +- else +- res = clk_set_rate_no_lock(clk, rate); ++ return clk->ops->get_rate(clk, parent_rate); ++ } + +- cpu_spin_unlock_xrestore(&clk_lock, exceptions); ++ if (clk->parent) ++ rate = clk_get_rate(clk->parent); + +- return res; ++ return rate; + } + + struct clk *clk_get_parent(struct clk *clk) +@@ -248,6 +231,7 @@ static TEE_Result clk_get_parent_idx(struct clk *clk, struct clk *parent, + static TEE_Result clk_set_parent_no_lock(struct clk *clk, struct clk *parent, + size_t pidx) + { ++ struct clk *old_parent = clk->parent; + TEE_Result res = TEE_ERROR_GENERIC; + bool was_enabled = false; + +@@ -256,9 +240,16 @@ static TEE_Result clk_set_parent_no_lock(struct clk *clk, struct clk *parent, + return TEE_SUCCESS; + + was_enabled = clk_is_enabled_no_lock(clk); +- /* Call is needed to decrement refcount on current parent tree */ +- if (was_enabled) +- clk_disable_no_lock(clk); ++ ++ if (clk->flags & CLK_OPS_PARENT_ENABLE) { ++ res = clk_enable_no_lock(parent); ++ if (res) ++ panic("Failed to re-enable clock after setting parent"); ++ ++ res = clk_enable_no_lock(old_parent); ++ if (res) ++ panic("Failed to re-enable clock after setting parent"); ++ } + + res = clk->ops->set_parent(clk, pidx); + if (res) +@@ -269,12 +260,18 @@ static TEE_Result clk_set_parent_no_lock(struct clk *clk, struct clk *parent, + /* The parent changed and the rate might also have changed */ + clk_compute_rate_no_lock(clk); + +-out: +- /* Call is needed to increment refcount on the new parent tree */ ++ /* Call is needed to decrement refcount on current parent tree */ + if (was_enabled) { +- res = clk_enable_no_lock(clk); ++ res = clk_enable_no_lock(parent); + if (res) + panic("Failed to re-enable clock after setting parent"); ++ ++ clk_disable_no_lock(old_parent); ++ } ++out: ++ if (clk->flags & CLK_OPS_PARENT_ENABLE) { ++ clk_disable_no_lock(old_parent); ++ clk_disable_no_lock(parent); + } + + return res; +@@ -301,3 +298,174 @@ out: + + return res; + } ++ ++static void clk_init_rate_req(struct clk *clk, ++ struct clk_rate_request *req) ++{ ++ struct clk *parent = clk->parent; ++ ++ if (parent) { ++ req->best_parent = parent; ++ req->best_parent_rate = parent->rate; ++ } else { ++ req->best_parent = NULL; ++ req->best_parent_rate = 0; ++ } ++} ++ ++static TEE_Result clk_set_rate_no_lock(struct clk *clk, unsigned long rate) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ unsigned long parent_rate = 0; ++ unsigned long new_rate = rate; ++ struct clk *parent = clk->parent; ++ ++ if (parent) ++ parent_rate = parent->rate; ++ ++ /* find the closest rate and parent clk/rate */ ++ if (clk->ops->determine_rate) { ++ struct clk_rate_request req; ++ struct clk *old_parent = clk->parent; ++ ++ req.rate = rate; ++ ++ clk_init_rate_req(clk, &req); ++ ++ res = clk->ops->determine_rate(clk, &req); ++ if (res) ++ return res; ++ ++ parent_rate = req.best_parent_rate; ++ new_rate = req.rate; ++ parent = req.best_parent; ++ ++ res = clk_set_rate_no_lock(parent, parent_rate); ++ ++ if (parent && parent != old_parent) { ++ if (parent && clk->num_parents > 1) { ++ size_t pidx = 0; ++ ++ if (clk_get_parent_idx(clk, parent, &pidx)) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ res = clk_set_parent_no_lock(clk, parent, pidx); ++ if (res) ++ return res; ++ clk->parent = parent; ++ } ++ } ++ } else if (!parent || !(clk->flags & CLK_SET_RATE_PARENT)) { ++ /* pass-through clock without adjustable parent */ ++ new_rate = rate; ++ } else { ++ /* pass-through clock with adjustable parent */ ++ res = clk_set_rate_no_lock(parent, rate); ++ if (res) ++ return res; ++ new_rate = parent->rate; ++ } ++ ++ if (clk->ops->set_rate) { ++ res = clk->ops->set_rate(clk, new_rate, parent_rate); ++ if (res) ++ return res; ++ } ++ ++ clk_compute_rate_no_lock(clk); ++ ++ return res; ++} ++ ++TEE_Result clk_set_rate(struct clk *clk, unsigned long rate) ++{ ++ uint32_t exceptions = 0; ++ TEE_Result res = TEE_ERROR_GENERIC; ++ ++ /* bail early if nothing to do */ ++ if (rate == clk_get_rate(clk)) ++ return TEE_SUCCESS; ++ ++ exceptions = cpu_spin_lock_xsave(&clk_lock); ++ ++ if (clk->flags & CLK_SET_RATE_GATE && clk_is_enabled_no_lock(clk)) ++ res = TEE_ERROR_BAD_STATE; ++ else ++ res = clk_set_rate_no_lock(clk, rate); ++ ++ cpu_spin_unlock_xrestore(&clk_lock, exceptions); ++ ++ return res; ++} ++ ++TEE_Result clk_get_duty_cyle(struct clk *clk, struct clk_duty *duty) ++{ ++ ++ if (clk->ops->get_duty_cycle) ++ return clk->ops->get_duty_cycle(clk, duty); ++ ++ return TEE_ERROR_GENERIC; ++} ++ ++unsigned long clk_round_rate(struct clk *clk, unsigned long rate) ++{ ++ if (clk->ops->round_rate) ++ return clk->ops->round_rate(clk, rate, clk->parent->rate); ++ ++ return TEE_ERROR_GENERIC; ++} ++ ++#include ++ ++static void clk_stm32_display_tree(struct clk *clk, int indent) ++{ ++ unsigned long rate; ++ const char *name; ++ int counter = clk->enabled_count.val; ++ int i; ++ int state; ++ ++ name = clk_get_name(clk); ++ rate = clk_get_rate(clk); ++ ++ state = clk->ops->is_enabled ? clk->ops->is_enabled(clk) : (counter > 0); ++ ++ printf("%02d %s ", counter, state ? "Y" : "N"); ++ ++ for (i = 0; i < indent * 4; i++) { ++ if ((i % 4) != 0) ++ printf("-"); ++ else ++ printf("|"); ++ } ++ ++ if (i != 0) ++ printf(" "); ++ ++ printf("%s (%ld)\n", name, rate); ++} ++ ++static void clk_stm32_tree(struct clk *clk_root, int indent) ++{ ++ struct clk *clk = NULL; ++ ++ STAILQ_FOREACH(clk, &clock_list, link) { ++ if (clk_get_parent(clk) == clk_root) { ++ clk_stm32_display_tree(clk, indent + 1); ++ clk_stm32_tree(clk, indent + 1); ++ } ++ } ++} ++ ++void clk_summary(void) ++{ ++ struct clk *clk = NULL; ++ ++ STAILQ_FOREACH(clk, &clock_list, link) { ++ if (clk_get_parent(clk)) ++ continue; ++ ++ clk_stm32_display_tree(clk, 0); ++ clk_stm32_tree(clk, 0); ++ } ++} +diff --git a/core/drivers/clk/sub.mk b/core/drivers/clk/sub.mk +index 86ced4a2d..6cc440962 100644 +--- a/core/drivers/clk/sub.mk ++++ b/core/drivers/clk/sub.mk +@@ -1,6 +1,8 @@ + srcs-y += clk.c + srcs-$(CFG_DRIVERS_CLK_DT) += clk_dt.c + srcs-$(CFG_DRIVERS_CLK_FIXED) += fixed_clk.c ++srcs-$(CFG_STM32MP_CLK_CORE) += clk-stm32-core.c ++srcs-$(CFG_STM32MP13_CLK) += clk-stm32mp13.c + srcs-$(CFG_STM32MP15_CLK) += clk-stm32mp15.c + +-subdirs-$(CFG_DRIVERS_SAM_CLK) += sam +\ No newline at end of file ++subdirs-$(CFG_DRIVERS_SAM_CLK) += sam +diff --git a/core/drivers/counter/counter.c b/core/drivers/counter/counter.c +new file mode 100644 +index 000000000..47b243a70 +--- /dev/null ++++ b/core/drivers/counter/counter.c +@@ -0,0 +1,233 @@ ++// SPDX-License-Identifier: BSD-2-Clause ++/* ++ * Copyright (c) 2020-2021, STMicroelectronics - All Rights Reserved ++ * Author(s): Ludovic Barre, for STMicroelectronics. ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++ ++static unsigned int list_lock = SPINLOCK_UNLOCK; ++ ++static LIST_HEAD(cnt_dev_list_head, counter_device) cnt_dev_list = ++ LIST_HEAD_INITIALIZER(cnt_dev_list_head); ++ ++TEE_Result counter_start(struct counter_device *counter, void *config) ++{ ++ uint32_t exceptions = 0; ++ ++ assert(counter && counter->ops); ++ ++ if (!counter->ops->start) ++ return TEE_ERROR_NOT_SUPPORTED; ++ ++ exceptions = cpu_spin_lock_xsave(&list_lock); ++ if (counter->is_used) { ++ cpu_spin_unlock_xrestore(&list_lock, exceptions); ++ return TEE_ERROR_BUSY; ++ } ++ ++ counter->is_used = true; ++ cpu_spin_unlock_xrestore(&list_lock, exceptions); ++ ++ return counter->ops->start(counter, config); ++} ++DECLARE_KEEP_PAGER(counter_start); ++ ++/* ++ * some counter device could not be stopped ++ * return TEE_SUCCESS if no stop ops ++ */ ++TEE_Result counter_stop(struct counter_device *counter) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ uint32_t exceptions = 0; ++ ++ assert(counter && counter->ops); ++ ++ if (!counter->ops->stop) ++ return TEE_SUCCESS; ++ ++ res = counter->ops->stop(counter); ++ if (!res) { ++ exceptions = cpu_spin_lock_xsave(&list_lock); ++ counter->is_used = false; ++ cpu_spin_unlock_xrestore(&list_lock, exceptions); ++ } ++ ++ return res; ++} ++DECLARE_KEEP_PAGER(counter_stop); ++ ++TEE_Result counter_get_value(struct counter_device *counter, unsigned int *ticks) ++{ ++ assert(counter && counter->ops); ++ ++ if (!counter->ops->get_value) ++ return TEE_ERROR_NOT_SUPPORTED; ++ ++ return counter->ops->get_value(counter, ticks); ++} ++ ++TEE_Result counter_set_alarm(struct counter_device *counter) ++{ ++ struct alarm_cfg *alarm = &counter->alarm; ++ TEE_Result res = TEE_SUCCESS; ++ ++ assert(counter && counter->ops); ++ ++ if (alarm->is_enabled || !alarm->callback || !counter->ops->set_alarm) ++ return TEE_ERROR_NOT_SUPPORTED; ++ ++ if (alarm->ticks > counter->max_ticks) ++ return TEE_ERROR_NOT_SUPPORTED; ++ ++ counter->alarm.callback = alarm->callback; ++ counter->alarm.ticks = alarm->ticks; ++ counter->alarm.priv = alarm->priv; ++ ++ res = counter->ops->set_alarm(counter); ++ if (!res) ++ counter->alarm.is_enabled = true; ++ ++ return res; ++} ++ ++TEE_Result counter_cancel_alarm(struct counter_device *counter) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ ++ assert(counter); ++ ++ if (!counter->ops->cancel_alarm) ++ return TEE_ERROR_NOT_SUPPORTED; ++ ++ res = counter->ops->cancel_alarm(counter); ++ if (!res) ++ counter->alarm.is_enabled = false; ++ ++ return res; ++} ++ ++void counter_release_config(struct counter_device *counter, ++ void *config) ++{ ++ assert(counter && config); ++ ++ if (counter->ops->release_config) ++ counter->ops->release_config(config); ++} ++ ++#ifdef CFG_DT ++struct counter_device *fdt_counter_get(const void *fdt, int offs, void **config) ++{ ++ struct counter_device *cnt_dev = NULL; ++ const fdt32_t *cuint = NULL; ++ int len = 0; ++ uint32_t exceptions = 0; ++ int phandle = 0; ++ ++ assert(fdt && config); ++ ++ cuint = fdt_getprop(fdt, offs, "counter", &len); ++ if (!cuint) ++ return NULL; ++ ++ phandle = fdt32_to_cpu(*cuint); ++ ++ exceptions = cpu_spin_lock_xsave(&list_lock); ++ ++ LIST_FOREACH(cnt_dev, &cnt_dev_list, dev_list) { ++ if (cnt_dev->phandle == phandle) ++ break; ++ } ++ ++ if ((uint32_t)len < sizeof(uint32_t)) ++ goto out; ++ ++ len -= sizeof(uint32_t); ++ cuint++; ++ ++ /* Len in bytes */ ++ len /= sizeof(uint32_t); ++ ++ if (cnt_dev && cnt_dev->ops->set_config) ++ cnt_dev->ops->set_config(cnt_dev, cuint, len, config); ++ ++out: ++ cpu_spin_unlock_xrestore(&list_lock, exceptions); ++ ++ return cnt_dev; ++} ++#else ++TEE_Result counter_get_config(struct counter_device *cnt_dev, ++ const struct counter_param *param, ++ void **config) ++{ ++ if (!param) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ if (!cnt_dev->ops->set_config) ++ return TEE_ERROR_NOT_SUPPORTED; ++ ++ if (cnt_dev && cnt_dev->ops->set_config) ++ cnt_dev->ops->set_config(cnt_dev, param->params, ++ param->len, config); ++ ++ return TEE_SUCCESS; ++} ++#endif ++ ++struct counter_device *counter_get_by_name(const char *name) ++{ ++ struct counter_device *cnt_dev = NULL; ++ uint32_t exceptions; ++ ++ assert(name); ++ ++ exceptions = cpu_spin_lock_xsave(&list_lock); ++ ++ LIST_FOREACH(cnt_dev, &cnt_dev_list, dev_list) { ++ if (!strcmp(cnt_dev->name, name)) ++ break; ++ } ++ ++ cpu_spin_unlock_xrestore(&list_lock, exceptions); ++ ++ return cnt_dev; ++} ++ ++struct counter_device *counter_dev_alloc(void) ++{ ++ return calloc(1, sizeof(struct counter_device)); ++} ++ ++void counter_dev_free(struct counter_device *counter) ++{ ++ uint32_t exceptions; ++ ++ assert(counter); ++ ++ exceptions = cpu_spin_lock_xsave(&list_lock); ++ LIST_REMOVE(counter, dev_list); ++ cpu_spin_unlock_xrestore(&list_lock, exceptions); ++ ++ free(counter); ++} ++ ++TEE_Result counter_dev_register(struct counter_device *counter) ++{ ++ uint32_t exceptions; ++ ++ assert(counter && counter->name && counter->ops); ++ ++ exceptions = cpu_spin_lock_xsave(&list_lock); ++ LIST_INSERT_HEAD(&cnt_dev_list, counter, dev_list); ++ cpu_spin_unlock_xrestore(&list_lock, exceptions); ++ ++ return TEE_SUCCESS; ++} +diff --git a/core/drivers/counter/stm32_lptimer.c b/core/drivers/counter/stm32_lptimer.c +new file mode 100644 +index 000000000..867715e47 +--- /dev/null ++++ b/core/drivers/counter/stm32_lptimer.c +@@ -0,0 +1,592 @@ ++// SPDX-License-Identifier: BSD-3-Clause ++/* ++ * Copyright (c) 2020-2022, STMicroelectronics ++ */ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++/* LPTIM offset register */ ++#define _LPTIM_ISR U(0x000) ++#define _LPTIM_ICR U(0x004) ++#define _LPTIM_IER U(0x008) ++#define _LPTIM_CFGR U(0x00C) ++#define _LPTIM_CR U(0x010) ++#define _LPTIM_CMP U(0x014) ++#define _LPTIM_ARR U(0x018) ++#define _LPTIM_CNT U(0x01C) ++#define _LPTIM_CFGR2 U(0x024) ++#define _LPTIM_HWCFGR U(0x3F0) ++#define _LPTIM_VERR U(0x3F4) ++ ++/* LPTIM_IXX register fields */ ++#define _LPTIM_IXX_CMPM BIT(0) ++#define _LPTIM_IXX_ARRM BIT(1) ++#define _LPTIM_IXX_EXTTRIG BIT(2) ++#define _LPTIM_IXX_CMPOK BIT(3) ++#define _LPTIM_IXX_ARROK BIT(4) ++#define _LPTIM_IXX_UP BIT(5) ++#define _LPTIM_IXX_DOWN BIT(6) ++ ++/* LPTIM_CFGR register fields */ ++#define _LPTIM_CFGR_CKSEL BIT(0) ++#define _LPTIM_CFGR_CKPOL_MASK GENMASK_32(2, 1) ++#define _LPTIM_CFGR_CKPOL_SHIFT 1 ++#define _LPTIM_CFGR_CKFLT_MASK GENMASK_32(4, 3) ++#define _LPTIM_CFGR_CKFLT_SHIFT 3 ++#define _LPTIM_CFGR_TRGFLT_MASK GENMASK_32(7, 6) ++#define _LPTIM_CFGR_TRGFLT_SHIFT 6 ++#define _LPTIM_CFGR_PRESC_MASK GENMASK_32(11, 9) ++#define _LPTIM_CFGR_PRESC_SHIFT 9 ++#define _LPTIM_CFGR_TRIGSEL_MASK GENMASK_32(15, 13) ++#define _LPTIM_CFGR_TRIGSEL_SHIFT 13 ++#define _LPTIM_CFGR_TRIGEN_MASK GENMASK_32(18, 17) ++#define _LPTIM_CFGR_TRIGEN_SHIFT 17 ++#define _LPTIM_CFGR_TIMOUT BIT(19) ++#define _LPTIM_CFGR_WAVE BIT(20) ++#define _LPTIM_CFGR_WAVEPOL BIT(21) ++#define _LPTIM_CFGR_PRELOAD BIT(22) ++#define _LPTIM_CFGR_COUNTMODE BIT(23) ++#define _LPTIM_CFGR_ENC BIT(24) ++ ++/* LPTIM_CR register fields */ ++#define _LPTIM_CR_ENABLE BIT(0) ++#define _LPTIM_CR_SNGSTRT BIT(1) ++#define _LPTIM_CR_CNTSTRT BIT(2) ++#define _LPTIM_CR_COUNTRST BIT(3) ++#define _LPTIM_CR_RSTARE BIT(4) ++ ++/* LPTIM_CMP register fields */ ++#define _LPTIM_CMP_MASK GENMASK_32(15, 0) ++#define _LPTIM_CMP_SHIFT 0 ++ ++/* LPTIM_ARR register fields */ ++#define _LPTIM_ARR_MASK GENMASK_32(15, 0) ++#define _LPTIM_ARR_SHIFT 0 ++ ++/* LPTIM_CNT register fields */ ++#define _LPTIM_CNT_MASK GENMASK_32(15, 0) ++#define _LPTIM_CNT_SHIFT 0 ++ ++/* LPTIM_CFGR2 register fields */ ++#define _LPTIM_CFGR2_IN1SEL_MASK GENMASK_32(1, 0) ++#define _LPTIM_CFGR2_IN1SEL_SHIFT 0 ++#define _LPTIM_CFGR2_IN2SEL_MASK GENMASK_32(5, 4) ++#define _LPTIM_CFGR2_IN2SEL_SHIFT 4 ++ ++/* LPTIM_HWCFGR register fields */ ++#define _LPTIM_HWCFGR_CFG1_MASK GENMASK_32(7, 0) ++#define _LPTIM_HWCFGR_CFG1_SHIFT 0 ++#define _LPTIM_HWCFGR_CFG2_MASK GENMASK_32(15, 8) ++#define _LPTIM_HWCFGR_CFG2_SHIFT 8 ++#define _LPTIM_HWCFGR_CFG3_MASK GENMASK_32(19, 16) ++#define _LPTIM_HWCFGR_CFG3_SHIFT 16 ++#define _LPTIM_HWCFGR_CFG4_MASK GENMASK_32(31, 24) ++#define _LPTIM_HWCFGR_CFG4_SHIFT 24 ++ ++/* LPTIM_VERR register fields */ ++#define _LPTIM_VERR_MINREV_MASK GENMASK_32(3, 0) ++#define _LPTIM_VERR_MINREV_SHIFT 0 ++#define _LPTIM_VERR_MAJREV_MASK GENMASK_32(7, 4) ++#define _LPTIM_VERR_MAJREV_SHIFT 4 ++ ++#define _LPTIM_FLD_PREP(field, value) (((uint32_t)(value) << (field ## _SHIFT)) & (field ## _MASK)) ++#define _LPTIM_FLD_GET(field, value) (((uint32_t)(value) & (field ## _MASK)) >> (field ## _SHIFT)) ++ ++/* ++ * no common errno between component ++ * define lptimer internal errno ++ */ ++#define LPTIM_ERR_NOMEM 12 /* Out of memory */ ++#define LPTIM_ERR_NODEV 19 /* No such device */ ++#define LPTIM_ERR_INVAL 22 /* Invalid argument */ ++#define LPTIM_ERR_NOTSUP 45 /* Operation not supported */ ++ ++struct lptimer_device { ++ const void *fdt; ++ int node; ++ struct stm32_lptimer_platdata pdata; ++ struct lptimer_driver_data *ddata; ++ struct counter_device *counter_dev; ++ struct itr_handler *itr; ++}; ++ ++struct stm32_lptimer_config { ++ struct lptimer_ext_input_cfg ext_input; ++ struct lptimer_ext_trigger_cfg ext_trigger; ++}; ++ ++static TEE_Result stm32_lpt_counter_set_alarm(struct counter_device *counter) ++{ ++ struct lptimer_device *lpt_dev = counter_priv(counter); ++ uintptr_t base = lpt_dev->pdata.base; ++ uint32_t cr = 0; ++ ++ cr = io_read32(base + _LPTIM_CR); ++ /* ++ * LPTIM_CMP & ARR register must only be modified ++ * when the LPTIM is enabled ++ */ ++ io_setbits32(base + _LPTIM_CR, _LPTIM_CR_ENABLE); ++ io_write32(base + _LPTIM_CMP, counter->alarm.ticks); ++ io_write32(base + _LPTIM_ARR, counter->alarm.ticks); ++ ++ /* ++ * LPTIM_IER register must only be modified ++ * when the LPTIM is disabled ++ */ ++ io_clrbits32(base + _LPTIM_CR, _LPTIM_CR_ENABLE); ++ /* enable, Compare match Interrupt */ ++ io_setbits32(base + _LPTIM_IER, _LPTIM_IXX_CMPM); ++ ++ /* restore cr */ ++ io_write32(base + _LPTIM_CR, cr); ++ ++ counter->alarm.is_enabled = true; ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result stm32_lpt_counter_cancel_alarm(struct counter_device *counter) ++{ ++ struct lptimer_device *lpt_dev = counter_priv(counter); ++ uintptr_t base = lpt_dev->pdata.base; ++ uint32_t cr = 0; ++ ++ cr = io_read32(base + _LPTIM_CR); ++ ++ /* ++ * LPTIM_IER register must only be modified ++ * when the LPTIM is disabled ++ */ ++ io_clrbits32(base + _LPTIM_CR, _LPTIM_CR_ENABLE); ++ io_clrbits32(base + _LPTIM_IER, _LPTIM_CR_ENABLE); ++ ++ /* ++ * LPTIM_CMP & ARR registers must only be modified ++ * when the LPTIM is disabled ++ */ ++ io_setbits32(base + _LPTIM_CR, _LPTIM_CR_ENABLE); ++ io_write32(base + _LPTIM_CMP, 0); ++ io_write32(base + _LPTIM_ARR, 0); ++ ++ /* restore cr */ ++ io_write32(base + _LPTIM_CR, cr); ++ ++ counter->alarm.is_enabled = false; ++ ++ return TEE_SUCCESS; ++} ++ ++static int _stm32_lpt_counter_get_value(struct lptimer_device *lpt_dev) ++{ ++ uintptr_t base = lpt_dev->pdata.base; ++ uint32_t cnt = 0; ++ uint32_t cnt_prev = 0; ++ ++ /* ++ * Note: a read access can be considered reliable when the ++ * values of the two consecutive read accesses are equal ++ */ ++ cnt = io_read32(base + _LPTIM_CNT); ++ do { ++ cnt_prev = cnt; ++ cnt = io_read32(base + _LPTIM_CNT); ++ } while (cnt_prev != cnt); ++ ++ return cnt; ++} ++ ++static TEE_Result stm32_lpt_counter_get_value(struct counter_device *counter, ++ uint32_t *ticks) ++{ ++ struct lptimer_device *lpt_dev = counter_priv(counter); ++ ++ if (!ticks) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ *ticks = _stm32_lpt_counter_get_value(lpt_dev); ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result stm32_lpt_counter_stop(struct counter_device *counter) ++{ ++ struct lptimer_device *lpt_dev = counter_priv(counter); ++ uintptr_t base = lpt_dev->pdata.base; ++ ++ io_write32(base + _LPTIM_CR, 0); ++ io_write32(base + _LPTIM_CMP, 0); ++ io_write32(base + _LPTIM_ARR, 0); ++ ++ clk_disable(lpt_dev->pdata.clock); ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result stm32_lpt_counter_start(struct counter_device *counter, ++ void *config) ++{ ++ struct lptimer_device *lpt_dev = counter_priv(counter); ++ struct stm32_lptimer_platdata *pdata = &lpt_dev->pdata; ++ struct stm32_lptimer_config *conf = ++ (struct stm32_lptimer_config *)config; ++ struct lptimer_ext_input_cfg *ext_input = &conf->ext_input; ++ struct lptimer_ext_trigger_cfg *ext_trigger = &conf->ext_trigger; ++ uintptr_t base = pdata->base; ++ uint32_t cfgr = 0; ++ uint32_t cfgr2 = 0; ++ ++ if (ext_input->num) { ++ cfgr += _LPTIM_CFGR_COUNTMODE; ++ ++ cfgr2 += _LPTIM_FLD_PREP(_LPTIM_CFGR2_IN1SEL, ++ ext_input->mux[0]); ++ if (ext_input->num == 2) ++ cfgr2 += _LPTIM_FLD_PREP(_LPTIM_CFGR2_IN2SEL, ++ ext_input->mux[1]); ++ } ++ ++ if (ext_trigger->num) { ++ cfgr += _LPTIM_FLD_PREP(_LPTIM_CFGR_TRIGSEL, ext_trigger->mux); ++ cfgr += _LPTIM_FLD_PREP(_LPTIM_CFGR_TRIGEN, ++ ext_trigger->polarity); ++ cfgr |= _LPTIM_CFGR_TIMOUT; ++ } ++ ++ clk_enable(lpt_dev->pdata.clock); ++ ++ /* ++ * LPTIM CFGR2 & IER registers must only be modified ++ * when the LPTIM is disabled ++ */ ++ io_write32(base + _LPTIM_CR, 0); ++ io_write32(base + _LPTIM_CFGR, cfgr); ++ io_write32(base + _LPTIM_CFGR2, cfgr2); ++ io_write32(base + _LPTIM_CR, _LPTIM_CR_ENABLE); ++ /* LPTIM_CR_CNTSTRT can be set only when the LPTIM is enable */ ++ io_write32(base + _LPTIM_CR, _LPTIM_CR_ENABLE | _LPTIM_CR_CNTSTRT); ++ ++ return TEE_SUCCESS; ++} ++ ++#ifdef CFG_EMBED_DTB ++static uint32_t get_param(const void *params, unsigned int step) ++{ ++ const fdt32_t *param_dt = (fdt32_t *)params; ++ ++ assert(param_dt); ++ ++ return fdt32_to_cpu(*(param_dt + step)); ++} ++#else ++static uint32_t get_param(const void *params, unsigned int step) ++{ ++ const uint32_t *tim_param = (uint32_t *)params; ++ ++ assert(tim_param); ++ ++ return tim_params[step]; ++} ++#endif ++ ++static TEE_Result ++stm32_lptimer_counter_set_config(struct counter_device *counter, ++ const void *param, ++ int len, void **config) ++{ ++ struct lptimer_device *lpt_dev = counter_priv(counter); ++ struct lptimer_driver_data *ddata = lpt_dev->ddata; ++ struct stm32_lptimer_config *conf; ++ struct lptimer_ext_input_cfg *ext_input; ++ struct lptimer_ext_trigger_cfg *ext_trigger; ++ ++ if (!param) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ if (len < 4 || len > 5) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ conf = calloc(1, sizeof(*conf)); ++ if (!conf) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ ext_trigger = &conf->ext_trigger; ++ ext_trigger->polarity = get_param(param, 0); ++ ext_trigger->mux = get_param(param, 1); ++ ext_trigger->num = 1; ++ ++ if (ext_trigger->mux >= ddata->nb_ext_trigger) { ++ free(conf); ++ return TEE_ERROR_BAD_PARAMETERS; ++ } ++ ++ DMSG("external trigger pol:%"PRId8" mux:%"PRId8, ++ ext_trigger->polarity, ext_trigger->mux); ++ ++ /* external-input = < polarity mux1 mux2 > */ ++ ext_input = &conf->ext_input; ++ ext_input->num = len - 4; ++ ext_input->polarity = get_param(param, 2); ++ ext_input->mux[0] = get_param(param, 3); ++ ++ if (ext_input->num == 2) ++ ext_input->mux[1] = get_param(param, 4); ++ ++ if (ext_input->mux[0] >= ddata->nb_ext_input || ++ ext_input->mux[1] >= ddata->nb_ext_input) { ++ free(conf); ++ return TEE_ERROR_BAD_PARAMETERS; ++ } ++ ++ DMSG("external input nb:%"PRIu8" pol:%"PRIu8" mux[0]:%"PRIu8" mux[1]:%"PRIu8, ++ ext_input->num, ext_input->polarity, ++ ext_input->mux[0], ext_input->mux[1]); ++ ++ *config = conf; ++ ++ return TEE_SUCCESS; ++} ++ ++static void stm32_lptimer_counter_release_config(void *config) ++{ ++ struct lptimer_config *conf = (struct lptimer_config *)config; ++ ++ free(conf); ++} ++ ++static const struct counter_ops stm32_lpt_counter_ops = { ++ .start = stm32_lpt_counter_start, ++ .stop = stm32_lpt_counter_stop, ++ .get_value = stm32_lpt_counter_get_value, ++ .set_alarm = stm32_lpt_counter_set_alarm, ++ .cancel_alarm = stm32_lpt_counter_cancel_alarm, ++ .set_config = stm32_lptimer_counter_set_config, ++ .release_config = stm32_lptimer_counter_release_config, ++}; ++ ++static enum itr_return stm32_lptimer_itr(struct itr_handler *h) ++{ ++ struct lptimer_device *lpt_dev = h->data; ++ uintptr_t base = lpt_dev->pdata.base; ++ uint32_t isr = 0; ++ ++ isr = io_read32(base + _LPTIM_ISR); ++ ++ if (isr & _LPTIM_IXX_CMPM) { ++ uint32_t ticks = _stm32_lpt_counter_get_value(lpt_dev); ++ void *priv = lpt_dev->counter_dev->alarm.priv; ++ ++ lpt_dev->counter_dev->alarm.callback(ticks, priv); ++ } ++ ++ io_write32(base + _LPTIM_ICR, isr); ++ ++ return ITRR_HANDLED; ++} ++ ++static void stm32_lptimer_set_driverdata(struct lptimer_device *lpt_dev) ++{ ++ struct lptimer_driver_data *ddata = lpt_dev->ddata; ++ uintptr_t base = lpt_dev->pdata.base; ++ uint32_t regval = 0; ++ ++ clk_enable(lpt_dev->pdata.clock); ++ ++ regval = io_read32(base + _LPTIM_HWCFGR); ++ ddata->nb_ext_input = _LPTIM_FLD_GET(_LPTIM_HWCFGR_CFG1, regval); ++ ddata->nb_ext_trigger = _LPTIM_FLD_GET(_LPTIM_HWCFGR_CFG2, regval); ++ ddata->encoder = _LPTIM_FLD_GET(_LPTIM_HWCFGR_CFG3, regval) != 0; ++ ++ regval = io_read8(base + _LPTIM_VERR); ++ ++ DMSG("LPTIM version %"PRIu32".%"PRIu32, ++ _LPTIM_FLD_GET(_LPTIM_VERR_MAJREV, regval), ++ _LPTIM_FLD_GET(_LPTIM_VERR_MINREV, regval)); ++ ++ DMSG("HW cap: ext_input:%"PRIu8" ext trigger:%"PRIu8" encoder:%s", ++ ddata->nb_ext_input, ddata->nb_ext_trigger, ++ ddata->encoder ? "true" : "false"); ++ ++ clk_disable(lpt_dev->pdata.clock); ++} ++ ++#ifdef CFG_EMBED_DTB ++#define LPTIMER_COUNTER_COMPAT "st,stm32-lptimer-counter" ++ ++struct dt_id_attr { ++ /* The effective size of the array is meaningless here */ ++ fdt32_t id_attr[1]; ++}; ++ ++static TEE_Result stm32_lptimer_parse_fdt(struct lptimer_device *lpt_dev) ++{ ++ struct stm32_lptimer_platdata *pdata = &lpt_dev->pdata; ++ struct dt_node_info dt_info = { }; ++ static struct io_pa_va base; ++ const void *fdt = lpt_dev->fdt; ++ int node = lpt_dev->node; ++ TEE_Result res = TEE_ERROR_GENERIC; ++ int len = 0; ++ ++ assert(lpt_dev && lpt_dev->fdt); ++ ++ _fdt_fill_device_info(fdt, &dt_info, node); ++ if (dt_info.status == DT_STATUS_DISABLED || ++ dt_info.reg == DT_INFO_INVALID_REG || ++ dt_info.clock == DT_INFO_INVALID_CLOCK || ++ dt_info.interrupt == DT_INFO_INVALID_INTERRUPT) ++ return -LPTIM_ERR_INVAL; ++ ++ pdata->name = fdt_get_name(fdt, node, &len); ++ pdata->phandle = fdt_get_phandle(fdt, node); ++ base.pa = dt_info.reg; ++ pdata->base = io_pa_or_va_secure(&base, 1); ++ pdata->irq = dt_info.interrupt; ++ ++ res = clk_dt_get_by_index(fdt, node, 0, &pdata->clock); ++ if (res) ++ return res; ++ ++ stm32_lptimer_set_driverdata(lpt_dev); ++ ++ node = fdt_subnode_offset(fdt, node, "counter"); ++ if (node >= 0 && ++ !fdt_node_check_compatible(fdt, node, LPTIMER_COUNTER_COMPAT) && ++ _fdt_get_status(fdt, node) != DT_STATUS_DISABLED) ++ pdata->mode = LPTIMER_MODE_COUNTER; ++ ++ return 0; ++} ++ ++__weak ++TEE_Result stm32_lptimer_get_platdata(struct stm32_lptimer_platdata *pdata __unused) ++{ ++ /* In DT config, the platform datas are fill by DT file */ ++ return TEE_SUCCESS; ++} ++ ++#else ++static TEE_Result stm32_lptimer_parse_fdt(struct lptimer_device *lpt_dev __unused) ++{ ++ return TEE_ERROR_NOT_IMPLEMENTED; ++} ++ ++/* ++ * This function could be overridden by platform to define ++ * pdata of lptimer driver ++ */ ++__weak TEE_Result stm32_lptimer_get_platdata(struct stm32_lptimer_platdata *pdata __unused) ++{ ++ return TEE_ERROR_NOT_IMPLEMENTED; ++} ++#endif /*CFG_EMBED_DTB*/ ++ ++static struct lptimer_device *stm32_lptimer_alloc(void) ++{ ++ struct lptimer_device *lpt_dev; ++ struct lptimer_driver_data *lpt_ddata; ++ ++ lpt_dev = calloc(1, sizeof(*lpt_dev)); ++ lpt_ddata = calloc(1, sizeof(*lpt_ddata)); ++ ++ if (lpt_dev && lpt_ddata) { ++ lpt_dev->ddata = lpt_ddata; ++ return lpt_dev; ++ } ++ ++ return NULL; ++} ++ ++static void stm32_lptimer_free(struct lptimer_device *lpt_dev) ++{ ++ if (lpt_dev->itr) ++ itr_free(lpt_dev->itr); ++ ++ if (lpt_dev->counter_dev) ++ counter_dev_free(lpt_dev->counter_dev); ++ ++ if (lpt_dev) { ++ free(lpt_dev->ddata); ++ free(lpt_dev); ++ } ++} ++ ++static TEE_Result probe_lpt_device(struct lptimer_device *lpt_dev) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ ++ assert(lpt_dev); ++ ++ res = stm32_lptimer_get_platdata(&lpt_dev->pdata); ++ if (res) ++ return res; ++ ++ res = stm32_lptimer_parse_fdt(lpt_dev); ++ if (res) ++ return res; ++ ++ if (!lpt_dev->ddata) ++ stm32_lptimer_set_driverdata(lpt_dev); ++ ++ lpt_dev->itr = itr_alloc_add(lpt_dev->pdata.irq, stm32_lptimer_itr, ++ ITRF_TRIGGER_LEVEL, lpt_dev); ++ if (!lpt_dev->itr) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ if (lpt_dev->pdata.mode & LPTIMER_MODE_COUNTER) { ++ lpt_dev->counter_dev = counter_dev_alloc(); ++ lpt_dev->counter_dev->ops = &stm32_lpt_counter_ops; ++ lpt_dev->counter_dev->max_ticks = _LPTIM_CNT_MASK; ++ lpt_dev->counter_dev->priv = lpt_dev; ++ lpt_dev->counter_dev->name = lpt_dev->pdata.name; ++ lpt_dev->counter_dev->phandle = lpt_dev->pdata.phandle; ++ ++ res = counter_dev_register(lpt_dev->counter_dev); ++ } ++ ++ return res; ++} ++ ++static TEE_Result stm32_lptimer_probe(const void *fdt, int node, ++ const void *compat_data __unused) ++{ ++ struct lptimer_device *lpt_dev = NULL; ++ TEE_Result res = TEE_ERROR_GENERIC; ++ ++ lpt_dev = stm32_lptimer_alloc(); ++ if (!lpt_dev) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ if (lpt_dev) { ++ lpt_dev->fdt = fdt; ++ lpt_dev->node = node; ++ res = probe_lpt_device(lpt_dev); ++ } ++ ++ if (res) ++ stm32_lptimer_free(lpt_dev); ++ ++ return res; ++} ++ ++static const struct dt_device_match stm32_lptimer_match_table[] = { ++ { .compatible = "st,stm32-lptimer" }, ++ { } ++}; ++ ++DEFINE_DT_DRIVER(stm32_lptimer_dt_driver) = { ++ .name = "stm32-lptimer", ++ .match_table = stm32_lptimer_match_table, ++ .probe = stm32_lptimer_probe, ++}; ++ +diff --git a/core/drivers/counter/stm32_tim.c b/core/drivers/counter/stm32_tim.c +new file mode 100644 +index 000000000..0ebebef0f +--- /dev/null ++++ b/core/drivers/counter/stm32_tim.c +@@ -0,0 +1,425 @@ ++// SPDX-License-Identifier: BSD-3-Clause ++/* ++ * Copyright (c) 2018-2022, STMicroelectronics ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++/* TIM offset register */ ++#define _TIM_CR1 0x00 /* Control Register 1 */ ++#define _TIM_CR2 0x04 /* Control Register 2 */ ++#define _TIM_SMCR 0x08 /* Slave mode control reg */ ++#define _TIM_DIER 0x0C /* DMA/interrupt register */ ++#define _TIM_SR 0x10 /* Status register */ ++#define _TIM_EGR 0x14 /* Event Generation Reg */ ++#define _TIM_CCMR1 0x18 /* Capt/Comp 1 Mode Reg */ ++#define _TIM_CCMR2 0x1C /* Capt/Comp 2 Mode Reg */ ++#define _TIM_CCER 0x20 /* Capt/Comp Enable Reg */ ++#define _TIM_CNT 0x24 /* Counter */ ++#define _TIM_PSC 0x28 /* Prescaler */ ++#define _TIM_ARR 0x2C /* Auto-Reload Register */ ++#define _TIM_CCR1 0x34 /* Capt/Comp Register 1 */ ++#define _TIM_CCR2 0x38 /* Capt/Comp Register 2 */ ++#define _TIM_CCR3 0x3C /* Capt/Comp Register 3 */ ++#define _TIM_CCR4 0x40 /* Capt/Comp Register 4 */ ++#define _TIM_BDTR 0x44 /* Break and Dead-Time Reg */ ++#define _TIM_DCR 0x48 /* DMA control register */ ++#define _TIM_DMAR 0x4C /* DMA transfer register */ ++#define _TIM_AF1 0x60 /* Alt Function Reg 1 */ ++#define _TIM_AF2 0x64 /* Alt Function Reg 2 */ ++#define _TIM_TISEL 0x68 /* Input Selection */ ++ ++/* TIM_CR1 register fields */ ++#define _TIM_CR1_CEN BIT(0) ++ ++/* TIM SMCR register fields */ ++#define _TIM_SMCR_SMS_MASK GENMASK_32(2, 0) ++#define _TIM_SMCR_SMS_SHIFT 0 ++#define _TIM_SMCR_TS_MASK GENMASK_32(6, 4) ++#define _TIM_SMCR_TS_SHIFT 4 ++#define _TIM_SMCR_SMS_RESET BIT(2) ++#define _TIM_SMCR_TS_TI1FP1 0x5 ++ ++/* TIM DIER register fileds */ ++#define _TIM_DIER_CC1E_MASK BIT(1) ++#define _TIM_DIER_CC1E_SHIFT 1 ++ ++/* TIM SR register fields */ ++#define _TIM_SR_CC1IF_MASK BIT(1) ++#define _TIM_SR_CC1IF_SHIFT 1 ++#define _TIM_SR_CC1OF_MASK BIT(9) ++#define _TIM_SR_CC1OF_SHIFT 9 ++ ++/* TIM CCMR register fields */ ++#define _TIM_CCMR1_CC1S_MASK GENMASK_32(1, 0) ++#define _TIM_CCMR1_CC1S_SHIFT 0 ++#define _TIM_CCMR1_CC1S_TI1 BIT(0) /* IC1/IC3 selects TI1/TI3 */ ++#define _TIM_CCMR1_CC1S_TI2 BIT(1) /* IC1/IC3 selects TI2/TI4 */ ++#define _TIM_CCMR1_CC2S_TI2 BIT(8) /* IC2/IC4 selects TI2/TI4 */ ++#define _TIM_CCMR1_CC2S_TI1 BIT(9) /* IC2/IC4 selects TI1/TI3 */ ++ ++/* TIM CNT register fields */ ++#define _TIM_CNT_MASK GENMASK_32(15, 0) ++ ++/* TIM CCER register fields */ ++#define _TIM_CCER_CC1E_MASK BIT(0) ++#define _TIM_CCER_CC1E_SHIFT 0 ++#define _TIM_CCER_CC1P_MASK BIT(1) ++#define _TIM_CCER_CC1P_SHIFT 1 ++ ++/* TIM TISEL register fields */ ++#define _TIM_TISEL_TI1SEL_MASK GENMASK_32(3, 0) ++#define _TIM_TISEL_TI1SEL_SHIFT 0 ++#define _TIM_TISEL_TI2SEL_MASK GENMASK_32(11, 8) ++#define _TIM_TISEL_TI2SEL_SHIFT 8 ++ ++#define _TIM_COUNTER_MODE BIT(0) ++#define _TIM_TIMEOUT_US 500000U ++ ++#define _TIM_FLD_PREP(field, value) (((uint32_t)(value) << (field ## _SHIFT)) & (field ## _MASK)) ++#define _TIM_FLD_GET(field, value) (((uint32_t)(value) & (field ## _MASK)) >> (field ## _SHIFT)) ++ ++/* ++ * No common errno between component. ++ * Define timer internal errno ++ */ ++#define TIM_ERR_NODEV 19 /* No such device */ ++#define TIM_ERR_NOTSUP 45 /* Operation not supported */ ++ ++struct timer_device { ++ const void *fdt; ++ int node; ++ struct stm32_timer_platdata pdata; ++ struct counter_device *counter_dev; ++}; ++ ++struct stm32_timer_config { ++ uint8_t num; ++ uint8_t polarity; ++ uint8_t input[2]; ++}; ++ ++static int _stm32_tim_counter_get_value(struct timer_device *tim_dev) ++{ ++ uintptr_t base = tim_dev->pdata.base; ++ uint32_t cnt = 0; ++ uint32_t cnt_prev = 0; ++ uint64_t timeout_ref = 0; ++ ++ cnt = io_read32(base + _TIM_CCR1); ++ do { ++ io_clrbits32(base + _TIM_SR, _TIM_SR_CC1IF_MASK); ++ cnt_prev = cnt; ++ timeout_ref = timeout_init_us(_TIM_TIMEOUT_US); ++ while (!timeout_elapsed(timeout_ref)) ++ if ((io_read32(base + _TIM_SR) & _TIM_SR_CC1IF_MASK)) ++ break; ++ ++ if (!(io_read32(base + _TIM_SR) & _TIM_SR_CC1IF_MASK)) { ++ cnt = 0; ++ break; ++ } ++ ++ cnt = io_read32(base + _TIM_CCR1); ++ } while (cnt_prev != cnt); ++ ++ return cnt; ++} ++ ++static TEE_Result stm32_tim_counter_get_value(struct counter_device *counter, ++ uint32_t *ticks) ++{ ++ struct timer_device *tim_dev = counter_priv(counter); ++ ++ if (!ticks) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ *ticks = _stm32_tim_counter_get_value(tim_dev); ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result stm32_tim_counter_stop(struct counter_device *counter) ++{ ++ struct timer_device *tim_dev = counter_priv(counter); ++ uintptr_t base = tim_dev->pdata.base; ++ ++ io_write32(base + _TIM_CR1, 0); ++ io_write32(base + _TIM_SR, 0); ++ io_write32(base + _TIM_DIER, 0); ++ io_write32(base + _TIM_CNT, 0); ++ io_write32(base + _TIM_SMCR, 0); ++ io_write32(base + _TIM_CCER, 0); ++ io_write32(base + _TIM_CCMR1, 0); ++ ++ clk_disable(tim_dev->pdata.clock); ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result stm32_tim_counter_start(struct counter_device *counter, ++ void *config __maybe_unused) ++{ ++ struct timer_device *tim_dev = counter_priv(counter); ++ struct stm32_timer_platdata *pdata = &tim_dev->pdata; ++ struct stm32_timer_config *conf = (struct stm32_timer_config *)config; ++ uintptr_t base = pdata->base; ++ uint32_t tisel = 0; ++ uint32_t ccmr1 = 0; ++ uint32_t ccer = 0; ++ uint32_t smcr = 0; ++ uint32_t dier = 0; ++ ++ assert(conf); ++ ++ if (conf->num) { ++ smcr = _TIM_FLD_PREP(_TIM_SMCR_TS, _TIM_SMCR_TS_TI1FP1); ++ smcr += _TIM_FLD_PREP(_TIM_SMCR_SMS, _TIM_SMCR_SMS_RESET); ++ tisel = _TIM_FLD_PREP(_TIM_TISEL_TI1SEL, conf->input[0]); ++ ccmr1 = _TIM_FLD_PREP(_TIM_CCMR1_CC1S, _TIM_CCMR1_CC1S_TI1); ++ ccer = _TIM_FLD_PREP(_TIM_CCER_CC1P, conf->polarity); ++ ccer += _TIM_FLD_PREP(_TIM_CCER_CC1E, 1); ++ dier = _TIM_FLD_PREP(_TIM_DIER_CC1E, 1); ++ } ++ ++ clk_enable(tim_dev->pdata.clock); ++ ++ io_write32(base + _TIM_CR1, 0); ++ io_write32(base + _TIM_CR2, 0); ++ io_write32(base + _TIM_SR, 0); ++ io_write32(base + _TIM_DIER, dier); ++ io_write32(base + _TIM_CCMR1, ccmr1); ++ io_write32(base + _TIM_CCER, ccer); ++ io_write32(base + _TIM_CNT, 0); ++ io_write32(base + _TIM_SMCR, smcr); ++ io_write32(base + _TIM_TISEL, tisel); ++ io_write32(base + _TIM_CR1, _TIM_CR1_CEN); ++ ++ return TEE_SUCCESS; ++} ++ ++#ifdef CFG_EMBED_DTB ++static uint32_t get_param(const void *params, unsigned int step) ++{ ++ const fdt32_t *param_dt = (fdt32_t *)params; ++ ++ assert(param_dt); ++ ++ return fdt32_to_cpu(*(param_dt + step)); ++} ++#else ++static uint32_t get_param(const void *params, unsigned int step) ++{ ++ const uint32_t *tim_param = (uint32_t *)params; ++ ++ assert(tim_param); ++ ++ return tim_params[step]; ++} ++#endif ++ ++static TEE_Result ++stm32_tim_counter_set_config(struct counter_device *counter __maybe_unused, ++ const void *params, ++ int len, void **config) ++{ ++ struct stm32_timer_config *conf = NULL; ++ ++ if (!params && !config) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ if (len < 2 || len > 3) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ conf = calloc(1, sizeof(*conf)); ++ if (!conf) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ conf->num = len - 1; ++ conf->polarity = get_param(params, 0); ++ conf->input[0] = get_param(params, 1); ++ ++ if (conf->num == 2) ++ conf->input[1] = get_param(params, 2); ++ ++ *config = conf; ++ ++ return TEE_SUCCESS; ++} ++ ++static void stm32_tim_counter_release_config(void *config) ++{ ++ struct stm32_timer_config *conf = (struct stm32_timer_config *)config; ++ ++ free(conf); ++} ++ ++static const struct counter_ops stm32_tim_counter_ops = { ++ .start = stm32_tim_counter_start, ++ .stop = stm32_tim_counter_stop, ++ .get_value = stm32_tim_counter_get_value, ++ .set_config = stm32_tim_counter_set_config, ++ .release_config = stm32_tim_counter_release_config, ++}; ++DECLARE_KEEP_PAGER(stm32_tim_counter_ops); ++ ++#ifdef CFG_EMBED_DTB ++#define TIMER_COUNTER_COMPAT "st,stm32-timer-counter" ++ ++struct dt_id_attr { ++ /* The effective size of the array is meaningless here */ ++ fdt32_t id_attr[1]; ++}; ++ ++static TEE_Result stm32_timer_parse_fdt(struct timer_device *tim_dev) ++{ ++ struct stm32_timer_platdata *pdata = &tim_dev->pdata; ++ struct dt_node_info dt_info = { }; ++ struct io_pa_va base = { }; ++ const void *fdt = tim_dev->fdt; ++ int node = tim_dev->node; ++ TEE_Result res = TEE_ERROR_GENERIC; ++ int len = 0; ++ ++ assert(tim_dev && tim_dev->fdt); ++ ++ _fdt_fill_device_info(fdt, &dt_info, node); ++ if (dt_info.reg == DT_INFO_INVALID_REG || ++ dt_info.reg_size == DT_INFO_INVALID_REG_SIZE || ++ dt_info.clock == DT_INFO_INVALID_CLOCK) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ pdata->name = fdt_get_name(fdt, node, &len); ++ pdata->phandle = fdt_get_phandle(fdt, node); ++ base.pa = dt_info.reg; ++ pdata->base = io_pa_or_va(&base, dt_info.reg_size); ++ res = clk_dt_get_by_index(fdt, node, 0, &pdata->clock); ++ if (res) ++ return res; ++ ++ node = fdt_subnode_offset(fdt, node, "counter"); ++ if (node >= 0 && ++ !fdt_node_check_compatible(fdt, node, TIMER_COUNTER_COMPAT) && ++ _fdt_get_status(fdt, node) != DT_STATUS_DISABLED) ++ pdata->mode = _TIM_COUNTER_MODE; ++ ++ return TEE_SUCCESS; ++} ++ ++__weak ++TEE_Result stm32_timer_get_platdata(struct stm32_timer_platdata *pdata __unused) ++{ ++ /* In DT config, the platform datas are filled by DT file */ ++ return TEE_SUCCESS; ++} ++ ++#else ++static TEE_Result stm32_timer_parse_fdt(struct timer_device *tim_dev) ++{ ++ return TEE_ERROR_NOT_IMPLEMENTED; ++} ++ ++/* ++ * This function could be overridden by platform to define ++ * pdata of timer driver ++ */ ++__weak TEE_Result stm32_timer_get_platdata(struct stm32_timer_platdata *pdata) ++{ ++ return TEE_ERROR_NOT_IMPLEMENTED; ++} ++#endif /*CFG_EMBED_DTB*/ ++ ++static struct timer_device *stm32_timer_alloc(void) ++{ ++ struct timer_device *tim_dev; ++ ++ tim_dev = calloc(1, sizeof(*tim_dev)); ++ ++ if (tim_dev) ++ return tim_dev; ++ ++ return NULL; ++} ++ ++static void stm32_timer_free(struct timer_device *tim_dev) ++{ ++ if (tim_dev->counter_dev) ++ counter_dev_free(tim_dev->counter_dev); ++ ++ if (tim_dev) ++ free(tim_dev); ++} ++ ++static TEE_Result stm32_timer_probe(struct timer_device *tim_dev) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ ++ assert(tim_dev); ++ ++ res = stm32_timer_get_platdata(&tim_dev->pdata); ++ if (res) ++ return res; ++ ++ res = stm32_timer_parse_fdt(tim_dev); ++ if (res && res != TEE_ERROR_NOT_IMPLEMENTED) ++ return res; ++ ++ if (tim_dev->pdata.mode & _TIM_COUNTER_MODE) { ++ tim_dev->counter_dev = counter_dev_alloc(); ++ if (!tim_dev->counter_dev) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ tim_dev->counter_dev->ops = &stm32_tim_counter_ops; ++ tim_dev->counter_dev->max_ticks = _TIM_CNT_MASK; ++ tim_dev->counter_dev->priv = tim_dev; ++ tim_dev->counter_dev->name = tim_dev->pdata.name; ++ tim_dev->counter_dev->phandle = tim_dev->pdata.phandle; ++ ++ res = counter_dev_register(tim_dev->counter_dev); ++ if (res) ++ counter_dev_free(tim_dev->counter_dev); ++ } ++ ++ return res; ++} ++ ++static TEE_Result stm32_timer_fdt_probe(const void *fdt, int node, ++ const void *compat_data __unused) ++{ ++ struct timer_device *tim_dev = NULL; ++ int err = 0; ++ ++ tim_dev = stm32_timer_alloc(); ++ if (tim_dev) { ++ tim_dev->fdt = fdt; ++ tim_dev->node = node; ++ err = stm32_timer_probe(tim_dev); ++ } ++ ++ if (!tim_dev || err) ++ stm32_timer_free(tim_dev); ++ ++ return TEE_SUCCESS; ++} ++ ++static const struct dt_device_match stm32_timer_match_table[] = { ++ { .compatible = "st,stm32-timers" }, ++ { } ++}; ++ ++DEFINE_DT_DRIVER(timer_dt_driver) = { ++ .name = "stm32-timer", ++ .match_table = stm32_timer_match_table, ++ .probe = &stm32_timer_fdt_probe, ++}; +diff --git a/core/drivers/counter/sub.mk b/core/drivers/counter/sub.mk +new file mode 100644 +index 000000000..db57742ff +--- /dev/null ++++ b/core/drivers/counter/sub.mk +@@ -0,0 +1,3 @@ ++srcs-$(CFG_COUNTER_DRIVER) += counter.c ++srcs-$(CFG_STM32_LPTIMER) += stm32_lptimer.c ++srcs-$(CFG_STM32_TIM) += stm32_tim.c +diff --git a/core/drivers/crypto/crypto_api/include/drvcrypt_acipher.h b/core/drivers/crypto/crypto_api/include/drvcrypt_acipher.h +index fb27a484a..589b362df 100644 +--- a/core/drivers/crypto/crypto_api/include/drvcrypt_acipher.h ++++ b/core/drivers/crypto/crypto_api/include/drvcrypt_acipher.h +@@ -144,7 +144,7 @@ struct drvcrypt_ecc { + /* Free ECC public key */ + void (*free_publickey)(struct ecc_public_key *key); + /* Generates the ECC keypair */ +- TEE_Result (*gen_keypair)(struct ecc_keypair *key, size_t size_bytes); ++ TEE_Result (*gen_keypair)(struct ecc_keypair *key, size_t size_bits); + /* ECC Sign a message and returns the signature */ + TEE_Result (*sign)(struct drvcrypt_sign_data *sdata); + /* ECC Verify a message's signature */ +diff --git a/core/drivers/crypto/stm32/cipher.c b/core/drivers/crypto/stm32/cipher.c +index 59755e966..5b8dc0361 100644 +--- a/core/drivers/crypto/stm32/cipher.c ++++ b/core/drivers/crypto/stm32/cipher.c +@@ -16,13 +16,113 @@ + + #include "common.h" + #include "stm32_cryp.h" ++#include "stm32_saes.h" + + #define DES3_KEY_SIZE 24 + ++struct cryp_ctx { ++ struct stm32_cryp_context ctx; ++ enum stm32_cryp_algo_mode algo; ++}; ++ ++struct saes_ctx { ++ struct stm32_saes_context ctx; ++ enum stm32_saes_chaining_mode algo; ++}; ++ ++union ip_ctx { ++ struct saes_ctx saes; ++ struct cryp_ctx cryp; ++}; ++ ++struct ip_cipher_ops { ++ TEE_Result (*init)(union ip_ctx *ctx, bool is_decrypt, ++ const uint8_t *key, size_t key_len, ++ const uint8_t *iv, size_t iv_len); ++ TEE_Result (*update)(union ip_ctx *ctx, bool last_block, uint8_t *src, ++ uint8_t *dst, size_t len); ++}; ++ + struct stm32_cipher_ctx { + struct crypto_cipher_ctx c_ctx; +- struct stm32_cryp_context cryp; +- enum stm32_cryp_algo_mode algo; ++ union ip_ctx ip_ctx; ++ const struct ip_cipher_ops *ops; ++}; ++ ++static TEE_Result cryp_init(union ip_ctx *ip_ctx __unused, ++ bool is_decrypt __unused, ++ const uint8_t *key __unused, ++ size_t key_len __unused, ++ const uint8_t *iv __unused, size_t iv_len __unused) ++{ ++#ifdef CFG_STM32_CRYP ++ uint8_t temp_key[DES3_KEY_SIZE] = { 0 }; ++ ++ if (key_len == 16 && ++ (ip_ctx->cryp.algo == STM32_CRYP_MODE_TDES_ECB || ++ ip_ctx->cryp.algo == STM32_CRYP_MODE_TDES_CBC)) { ++ /* Manage DES2: ie K=K1.K2.K1 */ ++ memcpy(temp_key, key, key_len); ++ memcpy(temp_key + key_len, key, key_len / 2); ++ key_len = DES3_KEY_SIZE; ++ key = temp_key; ++ } ++ ++ return stm32_cryp_init(&ip_ctx->cryp.ctx, is_decrypt, ip_ctx->cryp.algo, ++ key, key_len, iv, iv_len); ++#else ++ return TEE_ERROR_NOT_IMPLEMENTED; ++#endif ++} ++ ++static TEE_Result cryp_update(union ip_ctx *ip_ctx __unused, ++ bool last_block __unused, ++ uint8_t *src __unused, uint8_t *dst __unused, ++ size_t len __unused) ++{ ++#ifdef CFG_STM32_CRYP ++ return stm32_cryp_update(&ip_ctx->cryp.ctx, last_block, src, dst, len); ++#else ++ return TEE_ERROR_NOT_IMPLEMENTED; ++#endif ++} ++ ++static TEE_Result saes_init(union ip_ctx *ip_ctx __unused, ++ bool is_decrypt __unused, ++ const uint8_t *key __unused, ++ size_t key_len __unused, ++ const uint8_t *iv __unused, size_t iv_len __unused) ++{ ++#ifdef CFG_STM32_SAES ++ enum stm32_saes_key_selection key_sel = STM32_SAES_KEY_SOFT; ++ ++ return stm32_saes_init(&ip_ctx->saes.ctx, is_decrypt, ip_ctx->saes.algo, ++ key_sel, key, key_len, iv, iv_len); ++#else ++ return TEE_ERROR_NOT_IMPLEMENTED; ++#endif ++} ++ ++static TEE_Result saes_update(union ip_ctx *ip_ctx __unused, ++ bool last_block __unused, ++ uint8_t *src __unused, uint8_t *dst __unused, ++ size_t len __unused) ++{ ++#ifdef CFG_STM32_SAES ++ return stm32_saes_update(&ip_ctx->saes.ctx, last_block, src, dst, len); ++#else ++ return TEE_ERROR_NOT_IMPLEMENTED; ++#endif ++} ++ ++const struct ip_cipher_ops cryp_ops = { ++ .init = cryp_init, ++ .update = cryp_update, ++}; ++ ++const struct ip_cipher_ops saes_ops = { ++ .init = saes_init, ++ .update = saes_update, + }; + + static struct stm32_cipher_ctx * +@@ -36,27 +136,10 @@ to_stm32_cipher_ctx(struct crypto_cipher_ctx *ctx) + static TEE_Result stm32_cipher_initialize(struct drvcrypt_cipher_init *dinit) + { + struct stm32_cipher_ctx *c = to_stm32_cipher_ctx(dinit->ctx); +- uint8_t temp_key[DES3_KEY_SIZE] = { 0 }; +- uint8_t *key = NULL; +- size_t key_size = 0; +- +- if (dinit->key1.length == 16 && +- (c->algo == STM32_CRYP_MODE_TDES_ECB || +- c->algo == STM32_CRYP_MODE_TDES_CBC)) { +- /* Manage DES2: ie K=K1.K2.K1 */ +- memcpy(temp_key, dinit->key1.data, dinit->key1.length); +- memcpy(temp_key + dinit->key1.length, dinit->key1.data, +- dinit->key1.length / 2); +- key_size = DES3_KEY_SIZE; +- key = temp_key; +- } else { +- key_size = dinit->key1.length; +- key = dinit->key1.data; +- } + +- return stm32_cryp_init(&c->cryp, !dinit->encrypt, c->algo, +- key, key_size, dinit->iv.data, +- dinit->iv.length); ++ return c->ops->init(&c->ip_ctx, !dinit->encrypt, dinit->key1.data, ++ dinit->key1.length, dinit->iv.data, ++ dinit->iv.length); + } + + static TEE_Result stm32_cipher_update(struct drvcrypt_cipher_update *dupdate) +@@ -64,9 +147,8 @@ static TEE_Result stm32_cipher_update(struct drvcrypt_cipher_update *dupdate) + struct stm32_cipher_ctx *c = to_stm32_cipher_ctx(dupdate->ctx); + size_t len = MIN(dupdate->src.length, dupdate->dst.length); + +- return stm32_cryp_update(&c->cryp, dupdate->last, +- dupdate->src.data, dupdate->dst.data, +- len); ++ return c->ops->update(&c->ip_ctx, dupdate->last, dupdate->src.data, ++ dupdate->dst.data, len); + } + + static void stm32_cipher_final(void *ctx __unused) +@@ -88,52 +170,101 @@ static void stm32_cipher_copy_state(void *dst_ctx, void *src_ctx) + memcpy(dst, src, sizeof(*dst)); + } + +-static TEE_Result alloc_ctx(void **ctx, enum stm32_cryp_algo_mode algo) ++static TEE_Result alloc_cryp_ctx(void **ctx, enum stm32_cryp_algo_mode algo) + { + struct stm32_cipher_ctx *c = calloc(1, sizeof(*c)); + + if (!c) + return TEE_ERROR_OUT_OF_MEMORY; + +- c->algo = algo; ++ DMSG("Using CRYP %d", algo); ++ c->ip_ctx.cryp.algo = algo; ++ c->ops = &cryp_ops; ++ *ctx = &c->c_ctx; ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result alloc_saes_ctx(void **ctx, enum stm32_saes_chaining_mode algo) ++{ ++ struct stm32_cipher_ctx *c = calloc(1, sizeof(*c)); ++ ++ if (!c) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ DMSG("Using SAES %d", algo); ++ c->ip_ctx.saes.algo = algo; ++ c->ops = &saes_ops; + *ctx = &c->c_ctx; + + return TEE_SUCCESS; + } + + /* +- * Allocate the SW cipher data context. ++ * Allocate the SW cipher data context for CRYP ip. + * + * @ctx [out] Caller context variable + * @algo Algorithm ID of the context + */ +-static TEE_Result stm32_cipher_allocate(void **ctx, uint32_t algo) ++static TEE_Result stm32_cryp_cipher_allocate(void **ctx, uint32_t algo) + { + /* + * Convert TEE_ALGO id to internal id + */ + switch (algo) { + case TEE_ALG_DES_ECB_NOPAD: +- return alloc_ctx(ctx, STM32_CRYP_MODE_DES_ECB); ++ return alloc_cryp_ctx(ctx, STM32_CRYP_MODE_DES_ECB); + case TEE_ALG_DES_CBC_NOPAD: +- return alloc_ctx(ctx, STM32_CRYP_MODE_DES_CBC); ++ return alloc_cryp_ctx(ctx, STM32_CRYP_MODE_DES_CBC); + case TEE_ALG_DES3_ECB_NOPAD: +- return alloc_ctx(ctx, STM32_CRYP_MODE_TDES_ECB); ++ return alloc_cryp_ctx(ctx, STM32_CRYP_MODE_TDES_ECB); + case TEE_ALG_DES3_CBC_NOPAD: +- return alloc_ctx(ctx, STM32_CRYP_MODE_TDES_CBC); ++ return alloc_cryp_ctx(ctx, STM32_CRYP_MODE_TDES_CBC); + case TEE_ALG_AES_ECB_NOPAD: +- return alloc_ctx(ctx, STM32_CRYP_MODE_AES_ECB); ++ return alloc_cryp_ctx(ctx, STM32_CRYP_MODE_AES_ECB); + case TEE_ALG_AES_CBC_NOPAD: +- return alloc_ctx(ctx, STM32_CRYP_MODE_AES_CBC); ++ return alloc_cryp_ctx(ctx, STM32_CRYP_MODE_AES_CBC); + case TEE_ALG_AES_CTR: +- return alloc_ctx(ctx, STM32_CRYP_MODE_AES_CTR); ++ return alloc_cryp_ctx(ctx, STM32_CRYP_MODE_AES_CTR); + default: + return TEE_ERROR_NOT_IMPLEMENTED; + } + } + +-static struct drvcrypt_cipher driver_cipher = { +- .alloc_ctx = &stm32_cipher_allocate, ++/* ++ * Allocate the SW cipher data context for SAES ip. ++ * ++ * @ctx [out] Caller context variable ++ * @algo Algorithm ID of the context ++ */ ++static TEE_Result stm32_saes_cipher_allocate(void **ctx, uint32_t algo) ++{ ++ /* ++ * Convert TEE_ALGO id to internal id ++ */ ++ switch (algo) { ++ case TEE_ALG_AES_ECB_NOPAD: ++ return alloc_saes_ctx(ctx, STM32_SAES_MODE_ECB); ++ case TEE_ALG_AES_CBC_NOPAD: ++ return alloc_saes_ctx(ctx, STM32_SAES_MODE_CBC); ++ case TEE_ALG_AES_CTR: ++ return alloc_saes_ctx(ctx, STM32_SAES_MODE_CTR); ++ default: ++ return TEE_ERROR_NOT_IMPLEMENTED; ++ } ++} ++ ++static struct drvcrypt_cipher driver_cipher_cryp = { ++ .alloc_ctx = &stm32_cryp_cipher_allocate, ++ .free_ctx = &stm32_cipher_free, ++ .init = &stm32_cipher_initialize, ++ .update = &stm32_cipher_update, ++ .final = &stm32_cipher_final, ++ .copy_state = &stm32_cipher_copy_state, ++}; ++ ++static struct drvcrypt_cipher driver_cipher_saes = { ++ .alloc_ctx = &stm32_saes_cipher_allocate, + .free_ctx = &stm32_cipher_free, + .init = &stm32_cipher_initialize, + .update = &stm32_cipher_update, +@@ -141,7 +272,12 @@ static struct drvcrypt_cipher driver_cipher = { + .copy_state = &stm32_cipher_copy_state, + }; + +-TEE_Result stm32_register_cipher(void) ++TEE_Result stm32_register_cipher(enum cipher_ip_id cipher_ip) + { +- return drvcrypt_register_cipher(&driver_cipher); ++ if (cipher_ip == SAES_IP) ++ return drvcrypt_register_cipher(&driver_cipher_saes); ++ else if (cipher_ip == CRYP_IP) ++ return drvcrypt_register_cipher(&driver_cipher_cryp); ++ else ++ return TEE_ERROR_BAD_PARAMETERS; + } +diff --git a/core/drivers/crypto/stm32/common.h b/core/drivers/crypto/stm32/common.h +index d8f5f8532..c5ca4bb35 100644 +--- a/core/drivers/crypto/stm32/common.h ++++ b/core/drivers/crypto/stm32/common.h +@@ -8,7 +8,15 @@ + + #include + ++enum cipher_ip_id { ++ CRYP_IP, ++ SAES_IP, ++}; ++ + TEE_Result stm32_register_authenc(void); +-TEE_Result stm32_register_cipher(void); ++TEE_Result stm32_register_cipher(enum cipher_ip_id); ++TEE_Result stm32_register_ecc(void); ++TEE_Result stm32_register_hash(void); ++TEE_Result stm32_register_hmac(void); + + #endif /* __DRIVERS_CRYPTO_STM32_COMMON_H */ +diff --git a/core/drivers/crypto/stm32/crypto.mk b/core/drivers/crypto/stm32/crypto.mk +index bceb2dfd5..af14e08e2 100644 +--- a/core/drivers/crypto/stm32/crypto.mk ++++ b/core/drivers/crypto/stm32/crypto.mk +@@ -10,7 +10,7 @@ ifeq ($(CFG_STM32_CRYPTO_DRIVER),y) + $(call force,CFG_CRYPTO_DRIVER,y) + CFG_CRYPTO_DRIVER_DEBUG ?= 0 + +-ifeq ($(CFG_STM32_CRYP),y) ++ifeq ($(call cfg-one-enabled, CFG_STM32_CRYP CFG_STM32_SAES),y) + $(call force,CFG_CRYPTO_DRV_CIPHER,y,Mandated by CFG_STM32_CRYP) + endif + +@@ -18,4 +18,14 @@ ifeq ($(CFG_STM32_CRYP),y) + $(call force,CFG_CRYPTO_DRV_AUTHENC,y,Mandated by CFG_STM32_CRYP) + endif + ++ifeq ($(CFG_STM32_PKA),y) ++$(call force,CFG_CRYPTO_DRV_ECC,y,Mandated by CFG_STM32_PKA) ++$(call force,CFG_CRYPTO_DRV_ACIPHER,y,Mandated by CFG_STM32_PKA) ++endif ++ ++ifeq ($(CFG_STM32_HASH),y) ++$(call force,CFG_CRYPTO_DRV_HASH,y,Mandated by CFG_STM32_HASH) ++$(call force,CFG_CRYPTO_DRV_MAC,y,Mandated by CFG_STM32_HASH) ++endif ++ + endif # CFG_STM32_CRYPTO_DRIVER +diff --git a/core/drivers/crypto/stm32/ecc.c b/core/drivers/crypto/stm32/ecc.c +new file mode 100644 +index 000000000..ad7408022 +--- /dev/null ++++ b/core/drivers/crypto/stm32/ecc.c +@@ -0,0 +1,502 @@ ++// SPDX-License-Identifier: BSD-2-Clause ++/* ++ * Copyright (c) 2021, STMicroelectronics - All Rights Reserved ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++ ++#include "stm32_pka.h" ++#include "common.h" ++ ++static TEE_Result algo_to_pka_cid(uint32_t algo, ++ enum stm32_pka_curve_id *cid) ++{ ++ switch (algo) { ++ case TEE_ALG_ECDSA_P192: ++ *cid = PKA_NIST_P192; ++ break; ++ case TEE_ALG_ECDSA_P224: ++ *cid = PKA_NIST_P224; ++ break; ++ case TEE_ALG_ECDSA_P256: ++ *cid = PKA_NIST_P256; ++ break; ++ case TEE_ALG_ECDSA_P384: ++ *cid = PKA_NIST_P384; ++ break; ++ case TEE_ALG_ECDSA_P521: ++ *cid = PKA_NIST_P521; ++ break; ++ default: ++ EMSG("algorithm %#"PRIx32" not enabled", algo); ++ return TEE_ERROR_NOT_SUPPORTED; ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result curve_to_pka_cid(uint32_t curve, ++ enum stm32_pka_curve_id *cid) ++{ ++ switch (curve) { ++ case TEE_ECC_CURVE_NIST_P192: ++ *cid = PKA_NIST_P192; ++ break; ++ case TEE_ECC_CURVE_NIST_P224: ++ *cid = PKA_NIST_P224; ++ break; ++ case TEE_ECC_CURVE_NIST_P256: ++ *cid = PKA_NIST_P256; ++ break; ++ case TEE_ECC_CURVE_NIST_P384: ++ *cid = PKA_NIST_P384; ++ break; ++ case TEE_ECC_CURVE_NIST_P521: ++ *cid = PKA_NIST_P521; ++ break; ++ default: ++ EMSG("curve %#"PRIx32" not enabled", curve); ++ return TEE_ERROR_NOT_SUPPORTED; ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result stm32_gen_keypair(struct ecc_keypair *key, size_t size_bits) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ struct stm32_pka_bn d = { }; ++ struct stm32_pka_point pk = { }; ++ enum stm32_pka_curve_id cid = -1; ++ size_t bytes = 0; ++ ++ if (!key) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ DMSG("Using PKA"); ++ res = curve_to_pka_cid(key->curve, &cid); ++ if (res) ++ return res; ++ ++ res = stm32_pka_get_max_size(&bytes, NULL, cid); ++ if (res) ++ return res; ++ ++ if (size_bits > bytes * 8 || ++ crypto_bignum_num_bytes(key->d) > bytes || ++ crypto_bignum_num_bytes(key->x) > bytes || ++ crypto_bignum_num_bytes(key->y) > bytes) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ d.val = calloc(1, bytes); ++ d.size = bytes; ++ if (!d.val) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ /* Private key is a random vector */ ++ res = crypto_rng_read(d.val, d.size); ++ if (res) { ++ free(d.val); ++ return res; ++ } ++ ++ pk.x.val = calloc(1, bytes); ++ pk.x.size = bytes; ++ if (!pk.x.val) { ++ free(d.val); ++ return TEE_ERROR_OUT_OF_MEMORY; ++ } ++ ++ pk.y.val = calloc(1, bytes); ++ pk.y.size = bytes; ++ if (!pk.y.val) { ++ free(pk.x.val); ++ free(d.val); ++ return TEE_ERROR_OUT_OF_MEMORY; ++ } ++ ++ res = stm32_pka_edac_gen_pubkey(&d, &pk, cid); ++ if (res) ++ goto out; ++ ++ res = crypto_bignum_bin2bn(d.val, d.size, key->d); ++ if (res) ++ goto out; ++ ++ res = crypto_bignum_bin2bn(pk.x.val, pk.x.size, key->x); ++ if (res) ++ goto out; ++ ++ res = crypto_bignum_bin2bn(pk.y.val, pk.y.size, key->y); ++ ++out: ++ free(pk.y.val); ++ free(pk.x.val); ++ free(d.val); ++ ++ return res; ++} ++ ++static TEE_Result sign(uint32_t algo, struct ecc_keypair *key, ++ const uint8_t *msg, size_t msg_size, ++ uint8_t *sig, size_t *sig_len) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ enum stm32_pka_curve_id cid_from_algo = -1; ++ enum stm32_pka_curve_id cid = -2; ++ struct stm32_pka_bn d = { }; ++ struct stm32_pka_bn k = { }; ++ struct stm32_pka_bn sig_r = { }; ++ struct stm32_pka_bn sig_s = { }; ++ size_t bytes = 0; ++ ++ if (!key || !msg || !sig || !sig_len) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ if (curve_to_pka_cid(key->curve, &cid) || ++ algo_to_pka_cid(algo, &cid_from_algo) || ++ cid_from_algo != cid) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ res = stm32_pka_get_max_size(&bytes, NULL, cid); ++ if (res) ++ return res; ++ ++ if (*sig_len < 2 * bytes || ++ crypto_bignum_num_bytes(key->d) > bytes) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ *sig_len = 2 * bytes; ++ ++ d.size = crypto_bignum_num_bytes(key->d); ++ d.val = calloc(1, d.size); ++ if (!d.val) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ crypto_bignum_bn2bin(key->d, d.val); ++ ++ k.val = calloc(1, bytes); ++ k.size = bytes; ++ if (!k.val) { ++ free(d.val); ++ return TEE_ERROR_OUT_OF_MEMORY; ++ } ++ ++ res = crypto_rng_read(k.val, k.size); ++ if (res) ++ goto out; ++ ++ sig_r.val = (void *)sig; ++ sig_r.size = bytes; ++ sig_s.val = (void *)(sig + bytes); ++ sig_s.size = bytes; ++ ++ res = stm32_pka_ecdsa_sign(msg, msg_size, &sig_r, &sig_s, &d, &k, cid); ++ ++out: ++ free(k.val); ++ free(d.val); ++ ++ return res; ++} ++ ++static TEE_Result stm32_sign(struct drvcrypt_sign_data *sdata) ++{ ++ if (!sdata) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ DMSG("Using PKA"); ++ return sign(sdata->algo, ++ sdata->key, ++ sdata->message.data, ++ sdata->message.length, ++ sdata->signature.data, ++ &sdata->signature.length); ++} ++ ++static TEE_Result verify(uint32_t algo, struct ecc_public_key *key, ++ const uint8_t *msg, size_t msg_size, ++ const uint8_t *sig, size_t sig_size) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ struct stm32_pka_bn sig_r = { }; ++ struct stm32_pka_bn sig_s = { }; ++ struct stm32_pka_point pk = { }; ++ enum stm32_pka_curve_id cid_from_algo = -1; ++ enum stm32_pka_curve_id cid = -2; ++ size_t bytes = 0; ++ ++ if (!key || !msg || !sig) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ if (curve_to_pka_cid(key->curve, &cid) || ++ algo_to_pka_cid(algo, &cid_from_algo) || ++ cid_from_algo != cid) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ res = stm32_pka_get_max_size(&bytes, NULL, cid); ++ if (res) ++ return res; ++ ++ if (sig_size % 2) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ sig_r.val = (void *)sig; ++ sig_r.size = sig_size / 2; ++ sig_s.val = (void *)(sig + sig_size / 2); ++ sig_s.size = sig_size / 2; ++ ++ pk.x.size = crypto_bignum_num_bytes(key->x); ++ pk.x.val = calloc(1, pk.x.size); ++ if (!pk.x.val) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ crypto_bignum_bn2bin(key->x, pk.x.val); ++ ++ pk.y.size = crypto_bignum_num_bytes(key->y); ++ pk.y.val = calloc(1, pk.y.size); ++ if (!pk.y.val) { ++ free(pk.x.val); ++ return TEE_ERROR_OUT_OF_MEMORY; ++ } ++ ++ crypto_bignum_bn2bin(key->y, pk.y.val); ++ ++ res = stm32_pka_ecdsa_verif(msg, msg_size, &sig_r, &sig_s, &pk, cid); ++ ++ free(pk.y.val); ++ free(pk.x.val); ++ ++ return res; ++} ++ ++static TEE_Result stm32_verify(struct drvcrypt_sign_data *sdata) ++{ ++ if (!sdata) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ DMSG("Using PKA"); ++ return verify(sdata->algo, ++ sdata->key, ++ sdata->message.data, ++ sdata->message.length, ++ sdata->signature.data, ++ sdata->signature.length); ++} ++ ++static TEE_Result stm32_alloc_keypair(struct ecc_keypair *s, ++ size_t size_bits __unused) ++{ ++ if (!s) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ DMSG("Using PKA"); ++ memset(s, 0, sizeof(*s)); ++ ++ s->d = crypto_bignum_allocate(PKA_MAX_ECC_LEN); ++ if (!s->d) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ s->x = crypto_bignum_allocate(PKA_MAX_ECC_LEN); ++ if (!s->x) { ++ crypto_bignum_free(s->d); ++ return TEE_ERROR_OUT_OF_MEMORY; ++ } ++ ++ s->y = crypto_bignum_allocate(PKA_MAX_ECC_LEN); ++ if (!s->y) { ++ crypto_bignum_free(s->d); ++ crypto_bignum_free(s->x); ++ return TEE_ERROR_OUT_OF_MEMORY; ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result stm32_alloc_publickey(struct ecc_public_key *s, ++ size_t size_bits __unused) ++{ ++ if (!s) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ DMSG("Using PKA"); ++ memset(s, 0, sizeof(*s)); ++ ++ s->x = crypto_bignum_allocate(PKA_MAX_ECC_LEN); ++ if (!s->x) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ s->y = crypto_bignum_allocate(PKA_MAX_ECC_LEN); ++ if (!s->y) { ++ crypto_bignum_free(s->x); ++ return TEE_ERROR_OUT_OF_MEMORY; ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++static void stm32_free_publickey(struct ecc_public_key *s) ++{ ++ if (!s) ++ return; ++ ++ DMSG("Using PKA"); ++ crypto_bignum_free(s->x); ++ crypto_bignum_free(s->y); ++} ++ ++static TEE_Result is_point_on_curve(struct stm32_pka_point *point, ++ enum stm32_pka_curve_id cid) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ struct stm32_pka_bn r2modn = { }; ++ size_t bytes = 0; ++ ++ res = stm32_pka_get_max_size(&bytes, NULL, cid); ++ if (res) ++ return res; ++ ++ r2modn.val = calloc(1, bytes); ++ if (!r2modn.val) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ res = stm32_pka_ecc_compute_montgomery(&r2modn, cid); ++ if (res) ++ goto out; ++ ++ res = stm32_pka_is_point_on_curve(point, &r2modn, cid); ++out: ++ free(r2modn.val); ++ ++ return res; ++} ++ ++static TEE_Result shared_secret(struct ecc_keypair *private_key, ++ struct ecc_public_key *public_key, ++ void *secret, size_t *secret_len) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ enum stm32_pka_curve_id cid = -1; ++ struct stm32_pka_bn d = { }; ++ struct stm32_pka_point pk = { }; ++ struct stm32_pka_point result = { }; ++ size_t bytes = 0; ++ ++ if (!private_key || !public_key || !secret || !secret_len) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ if (private_key->curve != public_key->curve || ++ curve_to_pka_cid(public_key->curve, &cid)) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ res = stm32_pka_get_max_size(&bytes, NULL, cid); ++ if (res) ++ return res; ++ ++ /* Convert provided value to PKA format */ ++ pk.x.size = crypto_bignum_num_bytes(public_key->x); ++ pk.x.val = calloc(1, pk.x.size); ++ if (!pk.x.val) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ crypto_bignum_bn2bin(public_key->x, pk.x.val); ++ ++ pk.y.size = crypto_bignum_num_bytes(public_key->y); ++ pk.y.val = calloc(1, pk.y.size); ++ if (!pk.y.val) { ++ res = TEE_ERROR_OUT_OF_MEMORY; ++ goto free_pk_x_val; ++ } ++ ++ crypto_bignum_bn2bin(public_key->y, pk.y.val); ++ ++ d.size = crypto_bignum_num_bytes(private_key->d); ++ d.val = calloc(1, d.size); ++ if (!d.val) { ++ res = TEE_ERROR_OUT_OF_MEMORY; ++ goto free_pk_y_val; ++ } ++ ++ crypto_bignum_bn2bin(private_key->d, d.val); ++ ++ /* Allocate intermediate point */ ++ result.x.size = bytes; ++ result.x.val = calloc(1, result.x.size); ++ if (!result.x.val) { ++ res = TEE_ERROR_OUT_OF_MEMORY; ++ goto free_d_val; ++ } ++ ++ result.y.size = bytes; ++ result.y.val = calloc(1, result.y.size); ++ if (!result.y.val) { ++ res = TEE_ERROR_OUT_OF_MEMORY; ++ goto free_result_x_val; ++ } ++ ++ /* ++ * We should check that provided public_key point is on the selected ++ * curve. ++ */ ++ res = is_point_on_curve(&pk, cid); ++ if (res) ++ goto out; ++ ++ res = stm32_pka_ecc_scalar_mul(&d, &pk, &result, cid); ++ if (res) ++ goto out; ++ ++ if (*secret_len < result.x.size) { ++ res = TEE_ERROR_BAD_PARAMETERS; ++ goto out; ++ } ++ ++ memcpy(secret, result.x.val, result.x.size); ++ *secret_len = result.x.size; ++out: ++ free(result.y.val); ++free_result_x_val: ++ free(result.x.val); ++free_d_val: ++ free(d.val); ++free_pk_y_val: ++ free(pk.y.val); ++free_pk_x_val: ++ free(pk.x.val); ++ ++ return res; ++} ++ ++static TEE_Result stm32_shared_secret(struct drvcrypt_secret_data *sdata) ++{ ++ if (!sdata) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ DMSG("Using PKA"); ++ return shared_secret(sdata->key_priv, ++ sdata->key_pub, ++ sdata->secret.data, ++ &sdata->secret.length); ++} ++ ++/* ++ * Registration of the ECC Driver. ++ */ ++static struct drvcrypt_ecc driver_ecc = { ++ .alloc_keypair = stm32_alloc_keypair, ++ .alloc_publickey = stm32_alloc_publickey, ++ .free_publickey = stm32_free_publickey, ++ .gen_keypair = stm32_gen_keypair, ++ .sign = stm32_sign, ++ .verify = stm32_verify, ++ .shared_secret = stm32_shared_secret, ++}; ++ ++TEE_Result stm32_register_ecc(void) ++{ ++ return drvcrypt_register_ecc(&driver_ecc); ++} +diff --git a/core/drivers/crypto/stm32/hash.c b/core/drivers/crypto/stm32/hash.c +new file mode 100644 +index 000000000..00def7eec +--- /dev/null ++++ b/core/drivers/crypto/stm32/hash.c +@@ -0,0 +1,192 @@ ++// SPDX-License-Identifier: BSD-2-Clause ++/* ++ * Copyright (c) 2021, STMicroelectronics - All Rights Reserved ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#include "stm32_hash.h" ++#include "common.h" ++ ++static const struct crypto_hash_ops hash_ops; ++ ++struct stm32_hash_ctx { ++ struct crypto_hash_ctx ch_ctx; ++ struct stm32_hash_context hash; ++}; ++ ++static struct stm32_hash_ctx *to_stm32_hash_ctx(struct crypto_hash_ctx *ctx) ++{ ++ assert(ctx && ctx->ops == &hash_ops); ++ ++ return container_of(ctx, struct stm32_hash_ctx, ch_ctx); ++} ++ ++/* ++ * Initialization of the Hash operation ++ * ++ * @ctx Operation software context ++ */ ++static TEE_Result do_hash_init(struct crypto_hash_ctx *ctx) ++{ ++ struct stm32_hash_ctx *c = to_stm32_hash_ctx(ctx); ++ ++ return stm32_hash_init(&c->hash, NULL, 0); ++} ++ ++/* ++ * Update the Hash operation ++ * ++ * @ctx Operation software context ++ * @data Data to hash ++ * @len Data length ++ */ ++static TEE_Result do_hash_update(struct crypto_hash_ctx *ctx, ++ const uint8_t *data, size_t len) ++{ ++ struct stm32_hash_ctx *c = to_stm32_hash_ctx(ctx); ++ ++ return stm32_hash_update(&c->hash, data, len); ++} ++ ++/* ++ * Finalize the Hash operation ++ * ++ * @ctx Operation software context ++ * @digest [out] Hash digest buffer ++ * @len Digest buffer length ++ */ ++static TEE_Result do_hash_final(struct crypto_hash_ctx *ctx, uint8_t *digest, ++ size_t len) ++{ ++ struct stm32_hash_ctx *c = to_stm32_hash_ctx(ctx); ++ TEE_Result res = TEE_SUCCESS; ++ uint8_t *full_digest = NULL; ++ bool alloc = false; ++ ++ if (len < stm32_hash_digest_size(&c->hash)) { ++ full_digest = malloc(stm32_hash_digest_size(&c->hash)); ++ if (!full_digest) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ alloc = true; ++ } else { ++ full_digest = digest; ++ } ++ ++ res = stm32_hash_final(&c->hash, full_digest, NULL, 0); ++ ++ if (alloc) { ++ memcpy(digest, full_digest, len); ++ free(full_digest); ++ } ++ ++ return res; ++} ++ ++/* ++ * Free the SW hashing data context ++ * ++ * @ctx [in/out] Caller context variable ++ */ ++static void do_hash_free(struct crypto_hash_ctx *ctx) ++{ ++ struct stm32_hash_ctx *c = to_stm32_hash_ctx(ctx); ++ ++ stm32_hash_free(&c->hash); ++ free(c); ++} ++ ++/* ++ * Copy Software Hashing Context ++ * ++ * @dst_ctx [out] Reference the context destination ++ * @src_ctx Reference the context source ++ */ ++static void do_hash_copy_state(struct crypto_hash_ctx *dst_ctx, ++ struct crypto_hash_ctx *src_ctx) ++{ ++ struct stm32_hash_ctx *src = to_stm32_hash_ctx(src_ctx); ++ struct stm32_hash_ctx *dst = to_stm32_hash_ctx(dst_ctx); ++ ++ memcpy(&dst->ch_ctx, &src->ch_ctx, sizeof(dst->ch_ctx)); ++ stm32_hash_deep_copy(&dst->hash, &src->hash); ++} ++ ++/* ++ * Registration of the hash Driver ++ */ ++static const struct crypto_hash_ops hash_ops = { ++ .init = do_hash_init, ++ .update = do_hash_update, ++ .final = do_hash_final, ++ .free_ctx = do_hash_free, ++ .copy_state = do_hash_copy_state, ++}; ++ ++/* ++ * Allocate the internal hashing data context ++ * ++ * @ctx [out] Caller context variable ++ * @algo OP_TEE Algorithm ID ++ */ ++static TEE_Result stm32_hash_allocate(struct crypto_hash_ctx **ctx, ++ uint32_t algo) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ struct stm32_hash_ctx *c = NULL; ++ enum stm32_hash_algo stm32_algo = STM32_HASH_SHA256; ++ ++ /* Convert TEE Alog id to stm32 hash id */ ++ switch (TEE_ALG_GET_MAIN_ALG(algo)) { ++ case TEE_MAIN_ALGO_MD5: ++ stm32_algo = STM32_HASH_MD5; ++ break; ++ case TEE_MAIN_ALGO_SHA1: ++ stm32_algo = STM32_HASH_SHA1; ++ break; ++ case TEE_MAIN_ALGO_SHA224: ++ stm32_algo = STM32_HASH_SHA224; ++ break; ++ case TEE_MAIN_ALGO_SHA256: ++ stm32_algo = STM32_HASH_SHA256; ++ break; ++ case TEE_MAIN_ALGO_SHA384: ++ stm32_algo = STM32_HASH_SHA384; ++ break; ++ case TEE_MAIN_ALGO_SHA512: ++ stm32_algo = STM32_HASH_SHA512; ++ break; ++ default: ++ return TEE_ERROR_NOT_IMPLEMENTED; ++ } ++ ++ c = calloc(1, sizeof(*c)); ++ if (!c) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ /* ++ * Allocate internal stm32_hash. ++ */ ++ res = stm32_hash_alloc(&c->hash, STM32_HASH_MODE, stm32_algo); ++ if (res) { ++ free(c); ++ return res; ++ } ++ ++ DMSG("Using HASH %d", stm32_algo); ++ c->ch_ctx.ops = &hash_ops; ++ *ctx = &c->ch_ctx; ++ ++ return TEE_SUCCESS; ++} ++ ++TEE_Result stm32_register_hash(void) ++{ ++ return drvcrypt_register_hash(&stm32_hash_allocate); ++} +diff --git a/core/drivers/crypto/stm32/hmac.c b/core/drivers/crypto/stm32/hmac.c +new file mode 100644 +index 000000000..79f2a406c +--- /dev/null ++++ b/core/drivers/crypto/stm32/hmac.c +@@ -0,0 +1,223 @@ ++// SPDX-License-Identifier: BSD-2-Clause ++/* ++ * Copyright (c) 2021, STMicroelectronics - All Rights Reserved ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#include "stm32_hash.h" ++#include "common.h" ++ ++#define TOBE32(x) TEE_U32_BSWAP((x)) ++#define FROMBE32(x) TEE_U32_BSWAP((x)) ++ ++static const struct crypto_mac_ops hmac_ops; ++ ++struct stm32_hmac_ctx { ++ struct crypto_mac_ctx mac_ctx; ++ struct stm32_hash_context hash; ++ uint8_t *key; ++ size_t key_len; ++}; ++ ++static struct stm32_hmac_ctx *to_stm32_hmac_ctx(struct crypto_mac_ctx *ctx) ++{ ++ assert(ctx && ctx->ops == &hmac_ops); ++ ++ return container_of(ctx, struct stm32_hmac_ctx, mac_ctx); ++} ++ ++/* ++ * Initialization of the hmac operation ++ * ++ * @ctx Operation software context ++ */ ++static TEE_Result do_hmac_init(struct crypto_mac_ctx *ctx, const uint8_t *key, ++ size_t len) ++{ ++ struct stm32_hmac_ctx *c = to_stm32_hmac_ctx(ctx); ++ ++ /* ++ * If hmac_init() is called again, ++ * we won't need the previously saved key. ++ */ ++ if (c->key) ++ free(c->key); ++ ++ c->key = malloc(len); ++ if (!c->key) { ++ c->key_len = 0; ++ return TEE_ERROR_OUT_OF_MEMORY; ++ } ++ memcpy(c->key, key, len); ++ c->key_len = len; ++ ++ return stm32_hash_init(&c->hash, c->key, c->key_len); ++} ++ ++/* ++ * Update the hmac operation ++ * ++ * @ctx Operation software context ++ * @data Data to hmac ++ * @len Data length ++ */ ++static TEE_Result do_hmac_update(struct crypto_mac_ctx *ctx, ++ const uint8_t *data, size_t len) ++{ ++ struct stm32_hmac_ctx *c = to_stm32_hmac_ctx(ctx); ++ ++ return stm32_hash_update(&c->hash, data, len); ++} ++ ++/* ++ * Finalize the hmac operation ++ * ++ * @ctx Operation software context ++ * @digest [out] hmac digest buffer ++ * @len Digest buffer length ++ */ ++static TEE_Result do_hmac_final(struct crypto_mac_ctx *ctx, uint8_t *digest, ++ size_t len) ++{ ++ struct stm32_hmac_ctx *c = to_stm32_hmac_ctx(ctx); ++ TEE_Result res = TEE_SUCCESS; ++ uint8_t *full_digest = NULL; ++ bool alloc = false; ++ ++ if (len < stm32_hash_digest_size(&c->hash)) { ++ full_digest = malloc(stm32_hash_digest_size(&c->hash)); ++ if (!full_digest) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ alloc = true; ++ } else { ++ full_digest = digest; ++ } ++ ++ res = stm32_hash_final(&c->hash, full_digest, c->key, c->key_len); ++ ++ if (alloc) { ++ memcpy(digest, full_digest, len); ++ free(full_digest); ++ } ++ ++ return res; ++} ++ ++/* ++ * Free the SW hmac context ++ * ++ * @ctx [in/out] Caller context variable ++ */ ++static void do_hmac_free(struct crypto_mac_ctx *ctx) ++{ ++ struct stm32_hmac_ctx *c = to_stm32_hmac_ctx(ctx); ++ ++ free(c->key); ++ stm32_hash_free(&c->hash); ++ free(c); ++} ++ ++/* ++ * Copy Software hmacing Context ++ * ++ * @dst_ctx [out] Reference the context destination ++ * @src_ctx Reference the context source ++ */ ++static void do_hmac_copy_state(struct crypto_mac_ctx *dst_ctx, ++ struct crypto_mac_ctx *src_ctx) ++{ ++ struct stm32_hmac_ctx *src = to_stm32_hmac_ctx(src_ctx); ++ struct stm32_hmac_ctx *dst = to_stm32_hmac_ctx(dst_ctx); ++ ++ memcpy(&dst->mac_ctx, &src->mac_ctx, sizeof(dst->mac_ctx)); ++ stm32_hash_deep_copy(&dst->hash, &src->hash); ++ ++ dst->key_len = src->key_len; ++ ++ if (src->key) ++ dst->key = malloc(dst->key_len); ++ ++ if (dst->key) ++ memcpy(dst->key, src->key, dst->key_len); ++ else ++ dst->key_len = 0; ++} ++ ++/* ++ * Registration of the hmac Driver ++ */ ++static const struct crypto_mac_ops hmac_ops = { ++ .init = do_hmac_init, ++ .update = do_hmac_update, ++ .final = do_hmac_final, ++ .free_ctx = do_hmac_free, ++ .copy_state = do_hmac_copy_state, ++}; ++ ++/* ++ * Allocate the internal hmacing data context ++ * ++ * @ctx [out] Caller context variable ++ * @algo OP_TEE Algorithm ID ++ */ ++static TEE_Result stm32_hmac_allocate(struct crypto_mac_ctx **ctx, ++ uint32_t algo) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ enum stm32_hash_algo stm32_algo; ++ struct stm32_hmac_ctx *c = NULL; ++ ++ switch (TEE_ALG_GET_MAIN_ALG(algo)) { ++ case TEE_MAIN_ALGO_MD5: ++ stm32_algo = STM32_HASH_MD5; ++ break; ++ case TEE_MAIN_ALGO_SHA1: ++ stm32_algo = STM32_HASH_SHA1; ++ break; ++ case TEE_MAIN_ALGO_SHA224: ++ stm32_algo = STM32_HASH_SHA224; ++ break; ++ case TEE_MAIN_ALGO_SHA256: ++ stm32_algo = STM32_HASH_SHA256; ++ break; ++ case TEE_MAIN_ALGO_SHA384: ++ stm32_algo = STM32_HASH_SHA384; ++ break; ++ case TEE_MAIN_ALGO_SHA512: ++ stm32_algo = STM32_HASH_SHA512; ++ break; ++ default: ++ return TEE_ERROR_NOT_IMPLEMENTED; ++ } ++ ++ c = calloc(1, sizeof(*c)); ++ if (!c) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ /* ++ * Allocate internal stm32_hash. ++ */ ++ res = stm32_hash_alloc(&c->hash, STM32_HMAC_MODE, stm32_algo); ++ if (res) { ++ free(c); ++ return res; ++ } ++ ++ DMSG("Using HMAC %d", stm32_algo); ++ c->mac_ctx.ops = &hmac_ops; ++ *ctx = &c->mac_ctx; ++ ++ return TEE_SUCCESS; ++} ++ ++TEE_Result stm32_register_hmac(void) ++{ ++ return drvcrypt_register_hmac(&stm32_hmac_allocate); ++} +diff --git a/core/drivers/crypto/stm32/stm32_cryp.c b/core/drivers/crypto/stm32/stm32_cryp.c +index c02d28418..9530617e5 100644 +--- a/core/drivers/crypto/stm32/stm32_cryp.c ++++ b/core/drivers/crypto/stm32/stm32_cryp.c +@@ -1,6 +1,6 @@ + // SPDX-License-Identifier: BSD-2-Clause + /* +- * Copyright (c) 2021, STMicroelectronics - All Rights Reserved ++ * Copyright (c) 2021-2022, STMicroelectronics - All Rights Reserved + */ + #include + #include +@@ -195,7 +195,7 @@ static void clrsetbits(uint32_t *v, uint32_t mask, uint32_t bits) + static bool algo_mode_needs_iv(uint32_t cr) + { + return !IS_ALGOMODE(cr, TDES_ECB) && !IS_ALGOMODE(cr, DES_ECB) && +- !IS_ALGOMODE(cr, AES_ECB); ++ !IS_ALGOMODE(cr, AES_ECB); + } + + static bool algo_mode_is_ecb_cbc(uint32_t cr) +@@ -206,7 +206,7 @@ static bool algo_mode_is_ecb_cbc(uint32_t cr) + static bool algo_mode_is_aes(uint32_t cr) + { + return ((cr & _CRYP_CR_ALGOMODE_MSK) >> _CRYP_CR_ALGOMODE_OFF) >= +- _CRYP_CR_ALGOMODE_AES_ECB; ++ _CRYP_CR_ALGOMODE_AES_ECB; + } + + static bool is_decrypt(uint32_t cr) +@@ -233,7 +233,7 @@ static TEE_Result wait_sr_bits(vaddr_t base, uint32_t bits) + if (timeout_elapsed(timeout_ref)) + break; + +- if ((io_read32(base + _CRYP_SR) & bits) != bits) ++ if ((io_read32(base + _CRYP_SR) & bits) != bits) + return TEE_ERROR_BUSY; + + return TEE_SUCCESS; +@@ -890,7 +890,7 @@ TEE_Result stm32_cryp_update_load(struct stm32_cryp_context *ctx, + case _CRYP_CR_GCM_CCMPH_INIT: + res = do_from_init_to_phase(ctx, _CRYP_CR_GCM_CCMPH_PAYLOAD); + break; +- case _CRYP_CR_GCM_CCMPH_HEADER: ++ case _CRYP_CR_GCM_CCMPH_HEADER: + res = do_from_header_to_phase(ctx, _CRYP_CR_GCM_CCMPH_PAYLOAD); + break; + case _CRYP_CR_GCM_CCMPH_PAYLOAD: +@@ -954,7 +954,7 @@ TEE_Result stm32_cryp_update_load(struct stm32_cryp_context *ctx, + * We saved context, + * Complete block with 0 and send to CRYP to get {en,de}crypted data + * Store data to resend as last block in final() +- * or to complete next update_load() to get correct tag. ++ * or to complete next update_load() to get correct tag. + */ + if (i < data_size) { + uint32_t block_out[MAX_BLOCK_NB_U32] = { 0 }; +@@ -1010,14 +1010,14 @@ TEE_Result stm32_cryp_final(struct stm32_cryp_context *ctx, uint8_t *tag, + + mutex_lock(ctx->lock); + +- previous_phase = (ctx->cr & _CRYP_CR_GCM_CCMPH_MSK) >> ++ previous_phase = (ctx->cr & _CRYP_CR_GCM_CCMPH_MSK) >> + _CRYP_CR_GCM_CCMPH_OFF; + + switch (previous_phase) { + case _CRYP_CR_GCM_CCMPH_INIT: + res = do_from_init_to_phase(ctx, _CRYP_CR_GCM_CCMPH_FINAL); + break; +- case _CRYP_CR_GCM_CCMPH_HEADER: ++ case _CRYP_CR_GCM_CCMPH_HEADER: + res = do_from_header_to_phase(ctx, _CRYP_CR_GCM_CCMPH_FINAL); + break; + case _CRYP_CR_GCM_CCMPH_PAYLOAD: +@@ -1240,57 +1240,44 @@ out: + return res; + } + +-static int fdt_stm32_cryp(struct stm32_cryp_platdata *pdata) ++static TEE_Result fdt_stm32_cryp(struct stm32_cryp_platdata *pdata, ++ const void *fdt, int node) ++ + { +- int node = -1; ++ TEE_Result res = TEE_ERROR_GENERIC; + struct dt_node_info dt_cryp = { }; +- void *fdt = NULL; +- +- fdt = get_embedded_dt(); +- if (!fdt) +- return -FDT_ERR_NOTFOUND; +- +- node = fdt_node_offset_by_compatible(fdt, node, "st,stm32mp1-cryp"); +- if (node < 0) { +- EMSG("No CRYP entry in DT"); +- return -FDT_ERR_NOTFOUND; +- } + + _fdt_fill_device_info(fdt, &dt_cryp, node); + +- if (dt_cryp.status == DT_STATUS_DISABLED) +- return -FDT_ERR_NOTFOUND; +- +- if (dt_cryp.clock == DT_INFO_INVALID_CLOCK || +- dt_cryp.reset == DT_INFO_INVALID_RESET || +- dt_cryp.reg == DT_INFO_INVALID_REG) +- return -FDT_ERR_BADVALUE; ++ if (dt_cryp.reg == DT_INFO_INVALID_REG || ++ dt_cryp.reg_size == DT_INFO_INVALID_REG_SIZE || ++ dt_cryp.reset == DT_INFO_INVALID_RESET) ++ return TEE_ERROR_BAD_PARAMETERS; + + pdata->base.pa = dt_cryp.reg; +- io_pa_or_va_secure(&pdata->base, 1); ++ io_pa_or_va_secure(&pdata->base, dt_cryp.reg_size); ++ if (!pdata->base.va) ++ panic(); + +- pdata->clock_id = (unsigned long)dt_cryp.clock; + pdata->reset_id = (unsigned int)dt_cryp.reset; + +- return 0; ++ res= clk_dt_get_by_index(fdt, node, 0, &pdata->clock); ++ if (!pdata->clock) ++ return res; ++ ++ return TEE_SUCCESS; + } + +-static TEE_Result stm32_cryp_driver_init(void) ++static TEE_Result stm32_cryp_probe(const void *fdt, int node, ++ const void *compt_data __unused) + { + TEE_Result res = TEE_SUCCESS; + +- switch (fdt_stm32_cryp(&cryp_pdata)) { +- case 0: +- break; +- case -FDT_ERR_NOTFOUND: +- return TEE_SUCCESS; +- default: +- panic(); +- } +- +- stm32mp_register_secure_periph_iomem(cryp_pdata.base.pa); ++ res = fdt_stm32_cryp(&cryp_pdata, fdt, node); ++ if (res) ++ return res; + +- stm32_clock_enable(cryp_pdata.clock_id); ++ clk_enable(cryp_pdata.clock); + + if (stm32_reset_assert(cryp_pdata.reset_id, TIMEOUT_US_1MS)) + panic(); +@@ -1307,14 +1294,25 @@ static TEE_Result stm32_cryp_driver_init(void) + } + + if (IS_ENABLED(CFG_CRYPTO_DRV_CIPHER)) { +- res = stm32_register_cipher(); ++ res = stm32_register_cipher(CRYP_IP); + if (res) { + EMSG("Failed to register to cipher: %#"PRIx32, res); + panic(); + } + } + ++ stm32mp_register_secure_periph_iomem(cryp_pdata.base.pa); ++ + return TEE_SUCCESS; + } + +-driver_init(stm32_cryp_driver_init); ++static const struct dt_device_match stm32_cryp_match_table[] = { ++ { .compatible = "st,stm32mp1-cryp"}, ++ { } ++}; ++ ++DEFINE_DT_DRIVER(stm32_cryp_dt_driver) = { ++ .name = "stm32-cryp", ++ .match_table = stm32_cryp_match_table, ++ .probe = stm32_cryp_probe, ++}; +diff --git a/core/drivers/crypto/stm32/stm32_cryp.h b/core/drivers/crypto/stm32/stm32_cryp.h +index 9cd85198d..19fb6f109 100644 +--- a/core/drivers/crypto/stm32/stm32_cryp.h ++++ b/core/drivers/crypto/stm32/stm32_cryp.h +@@ -1,11 +1,13 @@ + /* SPDX-License-Identifier: BSD-2-Clause */ + /* +- * Copyright (c) 2021, STMicroelectronics - All Rights Reserved ++ * Copyright (c) 2021-2022, STMicroelectronics - All Rights Reserved + */ + + #ifndef STM32_CRYP_H + #define STM32_CRYP_H + ++#include ++#include + #include + #include + #include +@@ -14,7 +16,7 @@ + + struct stm32_cryp_platdata { + struct io_pa_va base; +- unsigned long clock_id; ++ struct clk *clock; + unsigned int reset_id; + }; + +diff --git a/core/drivers/crypto/stm32/stm32_hash.c b/core/drivers/crypto/stm32/stm32_hash.c +new file mode 100644 +index 000000000..3b590cffc +--- /dev/null ++++ b/core/drivers/crypto/stm32/stm32_hash.c +@@ -0,0 +1,918 @@ ++// SPDX-License-Identifier: BSD-2-Clause ++/* ++ * Copyright (c) 2021, STMicroelectronics - All Rights Reserved ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#include "stm32_hash.h" ++#include "common.h" ++ ++#define _HASH_CR 0x00U ++#define _HASH_DIN 0x04U ++#define _HASH_STR 0x08U ++#define _HASH_IMR 0x20U ++#define _HASH_SR 0x24U ++#define _HASH_HR(x) (0x310U + ((x) * 0x04U)) ++#define _HASH_VERR 0x3F4U ++#define _HASH_CSR(x) (0xF8U + ((x) * 0x04U)) ++ ++/* Control Register */ ++#define _HASH_CR_INIT BIT(2) ++#define _HASH_CR_MODE BIT(6) ++#define _HASH_CR_DATATYPE_SHIFT 4U ++#define _HASH_CR_DATATYPE_NONE (0U << _HASH_CR_DATATYPE_SHIFT) ++#define _HASH_CR_DATATYPE_HALFWORD (1U << _HASH_CR_DATATYPE_SHIFT) ++#define _HASH_CR_DATATYPE_BYTE (2U << _HASH_CR_DATATYPE_SHIFT) ++#define _HASH_CR_DATATYPE_BIT (3U << _HASH_CR_DATATYPE_SHIFT) ++#define _HASH_CR_LKEY BIT(16) ++ ++#define _HASH_CR_ALGO_SHIFT 17U ++#define _HASH_CR_ALGO_MD5 BIT(7) ++#define _HASH_CR_ALGO_SHA1 (0x0U << _HASH_CR_ALGO_SHIFT) ++#define _HASH_CR_ALGO_SHA224 (0x2U << _HASH_CR_ALGO_SHIFT) ++#define _HASH_CR_ALGO_SHA256 (0x3U << _HASH_CR_ALGO_SHIFT) ++#define _HASH_CR_ALGO_SHA256_IF_MD5 (BIT(18) | BIT(7)) ++#define _HASH_CR_ALGO_SHA384 (0xCU << _HASH_CR_ALGO_SHIFT) ++#define _HASH_CR_ALGO_SHA512_224 (0xDU << _HASH_CR_ALGO_SHIFT) ++#define _HASH_CR_ALGO_SHA512_256 (0xEU << _HASH_CR_ALGO_SHIFT) ++#define _HASH_CR_ALGO_SHA512 (0xFU << _HASH_CR_ALGO_SHIFT) ++#define _HASH_CR_ALGO_SHA3_224 (0x4U << _HASH_CR_ALGO_SHIFT) ++#define _HASH_CR_ALGO_SHA3_256 (0x5U << _HASH_CR_ALGO_SHIFT) ++#define _HASH_CR_ALGO_SHA3_384 (0x6U << _HASH_CR_ALGO_SHIFT) ++#define _HASH_CR_ALGO_SHA3_512 (0x7U << _HASH_CR_ALGO_SHIFT) ++#define _HASH_CR_ALGO_SHAKE128 (0x8U << _HASH_CR_ALGO_SHIFT) ++#define _HASH_CR_ALGO_SHAKE256 (0x9U << _HASH_CR_ALGO_SHIFT) ++#define _HASH_CR_ALGO_RAWSHAKE128 (0xAU << _HASH_CR_ALGO_SHIFT) ++#define _HASH_CR_ALGO_RAWSHAKE256 (0xBU << _HASH_CR_ALGO_SHIFT) ++ ++/* Status Flags */ ++#define _HASH_SR_DINIS BIT(0) ++#define _HASH_SR_DCIS BIT(1) ++#define _HASH_SR_BUSY BIT(3) ++#define _HASH_SR_NBWP_MASK GENMASK_32(13, 9) ++#define _HASH_SR_NBWP_OFF 9 ++#define _HASH_SR_NBWE_MASK GENMASK_32(21, 16) ++#define _HASH_SR_NBWE_OFF 16 ++ ++/* STR Register */ ++#define _HASH_STR_NBLW_MASK GENMASK_32(4, 0) ++#define _HASH_STR_DCAL BIT(8) ++ ++/* _iHASH_VERR bit fields */ ++#define _HASH_VERR_MINREV GENMASK_32(3, 0) ++#define _HASH_VERR_MAJREV GENMASK_32(7, 4) ++ ++/* Digest size in nb of uint32_t */ ++#define MD5_DIGEST_U32 4U ++#define SHA1_DIGEST_U32 5U ++#define SHA224_DIGEST_U32 7U ++#define SHA256_DIGEST_U32 8U ++#define SHA384_DIGEST_U32 12U ++#define SHA512_224_DIGEST_U32 7U ++#define SHA512_256_DIGEST_U32 8U ++#define SHA512_DIGEST_U32 16U ++#define SHA3_224_DIGEST_U32 7U ++#define SHA3_256_DIGEST_U32 8U ++#define SHA3_384_DIGEST_U32 12U ++#define SHA3_512_DIGEST_U32 16U ++ ++/* Internal block size */ ++#define MD5_BLOCK_SIZE 64U ++#define SHA1_BLOCK_SIZE 64U ++#define SHA224_BLOCK_SIZE 64U ++#define SHA256_BLOCK_SIZE 64U ++#define SHA384_BLOCK_SIZE 128U ++#define SHA512_224_BLOCK_SIZE 128U ++#define SHA512_256_BLOCK_SIZE 128U ++#define SHA512_BLOCK_SIZE 128U ++#define SHA3_224_BLOCK_SIZE 144U ++#define SHA3_256_BLOCK_SIZE 136U ++#define SHA3_384_BLOCK_SIZE 104U ++#define SHA3_512_BLOCK_SIZE 72U ++ ++/* Define the registers needed to save context */ ++#define SAVE_SMALL 1U ++#define SAVE_BIG 2U ++#define SAVE_SHA3 3U ++ ++#define SAVE_SMALL_NB_REG 22U ++#define SAVE_SMALL_FIRST_REG 0U ++#define SAVE_SMALL_HMAC_NB_REG 16U ++#define SAVE_SMALL_HMAC_FIRST_REG 38U ++#define SAVE_BIG_NB_REG 91U ++#define SAVE_BIG_FIRST_REG 0U ++#define SAVE_BIG_HMAC_NB_REG 12U ++#define SAVE_BIG_HMAC_FIRST_REG 91U ++#define SAVE_SHA3_NB_REG 72U ++#define SAVE_SHA3_FIRST_REG 0U ++#define SAVE_SHA3_HMAC_NB_REG 72U ++#define SAVE_SHA3_HMAC_FIRST_REG 16U ++ ++#define RESET_TIMEOUT_US_1MS 1000U ++#define HASH_TIMEOUT_US 10000U ++ ++#define TOBE32(x) TEE_U32_BSWAP((x)) ++#define FROMBE32(x) TEE_U32_BSWAP((x)) ++ ++/* Define capabilities */ ++#define CAPS_MD5 BIT(0) ++#define CAPS_SHA1 BIT(1) ++#define CAPS_SHA2_224 BIT(2) ++#define CAPS_SHA2_256 BIT(3) ++#define CAPS_SHA2_384 BIT(4) ++#define CAPS_SHA2_512 BIT(5) ++#define CAPS_SHA3 BIT(6) ++ ++struct stm32_hash_device { ++ struct stm32_hash_platdata pdata; ++ struct mutex lock; /* Protect HASH HW instance access */ ++}; ++ ++static struct stm32_hash_device *stm32_hash; ++ ++static inline vaddr_t hash_base(struct stm32_hash_context *c) ++{ ++ return io_pa_or_va(&c->dev->pdata.base, 1); ++} ++ ++static TEE_Result wait_end_busy(vaddr_t base) ++{ ++ uint64_t timeout = timeout_init_us(HASH_TIMEOUT_US); ++ ++ while (io_read32(base + _HASH_SR) & _HASH_SR_BUSY) ++ if (timeout_elapsed(timeout)) ++ break; ++ ++ /* Timeout may append due to a schedule after the while(test) */ ++ if (io_read32(base + _HASH_SR) & _HASH_SR_BUSY) { ++ DMSG("Busy timeout"); ++ return TEE_ERROR_BUSY; ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++static int wait_digest_ready(vaddr_t base) ++{ ++ uint64_t timeout = timeout_init_us(HASH_TIMEOUT_US); ++ ++ while ((io_read32(base + _HASH_SR) & _HASH_SR_DCIS) != _HASH_SR_DCIS) ++ if (timeout_elapsed(timeout)) ++ break; ++ ++ /* Timeout may append due to a schedule after the while(test) */ ++ if ((io_read32(base + _HASH_SR) & _HASH_SR_DCIS) != _HASH_SR_DCIS) { ++ DMSG("Ready timeout"); ++ return TEE_ERROR_BUSY; ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result hash_write_data(vaddr_t base, uint32_t data) ++{ ++ io_write32(base + _HASH_DIN, data); ++ ++ return wait_end_busy(base); ++} ++ ++static TEE_Result write_key(vaddr_t base, const uint8_t *key, ++ size_t len) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ uint32_t tmp_buf = 0; ++ ++ io_clrsetbits32(base + _HASH_STR, _HASH_STR_NBLW_MASK, ++ 8U * (len % sizeof(uint32_t))); ++ ++ while (len / sizeof(uint32_t)) { ++ memcpy(&tmp_buf, key, sizeof(uint32_t)); ++ res = hash_write_data(base, tmp_buf); ++ if (res) ++ return res; ++ ++ key += sizeof(uint32_t); ++ len -= sizeof(uint32_t); ++ } ++ ++ if (len) { ++ tmp_buf = 0; ++ memcpy(&tmp_buf, key, len); ++ res = hash_write_data(base, tmp_buf); ++ if (res) ++ return res; ++ } ++ ++ io_setbits32(base + _HASH_STR, _HASH_STR_DCAL); ++ ++ return TEE_SUCCESS; ++} ++ ++static void get_save_registers(struct stm32_hash_context *c, ++ size_t *nb_regs, size_t *first, ++ size_t *hmac_nb_regs, size_t *hmac_first) ++{ ++ if (c->save_mode == SAVE_SMALL) { ++ *nb_regs = SAVE_SMALL_NB_REG; ++ *first = SAVE_SMALL_FIRST_REG; ++ if (c->mode == STM32_HMAC_MODE) { ++ *hmac_nb_regs = SAVE_SMALL_HMAC_NB_REG; ++ *hmac_first = SAVE_SMALL_HMAC_FIRST_REG; ++ } ++ } ++ ++ if (c->save_mode == SAVE_BIG) { ++ *nb_regs = SAVE_BIG_NB_REG; ++ *first = SAVE_BIG_FIRST_REG; ++ if (c->mode == STM32_HMAC_MODE) { ++ *hmac_nb_regs = SAVE_BIG_HMAC_NB_REG; ++ *hmac_first = SAVE_BIG_HMAC_FIRST_REG; ++ } ++ } ++ ++ if (c->save_mode == SAVE_SHA3) { ++ *nb_regs = SAVE_SHA3_NB_REG; ++ *first = SAVE_SHA3_FIRST_REG; ++ if (c->mode == STM32_HMAC_MODE) { ++ *hmac_nb_regs = SAVE_SHA3_HMAC_NB_REG; ++ *hmac_first = SAVE_SHA3_HMAC_FIRST_REG; ++ } ++ } ++} ++ ++static TEE_Result save_context(struct stm32_hash_context *c) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ size_t i = 0; ++ size_t nb_reg = 0; ++ size_t first = 0; ++ size_t hmac_nb_reg = 0; ++ size_t hmac_first = 0; ++ vaddr_t base = hash_base(c); ++ ++ res = wait_end_busy(base); ++ if (res) ++ return res; ++ ++ /* Check that FIFO is empty */ ++ if (!(io_read32(base + _HASH_SR) & _HASH_SR_DINIS)) ++ return TEE_ERROR_BAD_STATE; ++ ++ c->imr = io_read32(base + _HASH_IMR); ++ c->str = io_read32(base + _HASH_STR); ++ c->cr = io_read32(base + _HASH_CR); ++ ++ get_save_registers(c, &nb_reg, &first, &hmac_nb_reg, &hmac_first); ++ ++ if (!c->csr) ++ return TEE_ERROR_BAD_STATE; ++ ++ /* Save context registers */ ++ for (i = 0; i < nb_reg; i++) ++ c->csr[i] = io_read32(base + _HASH_CSR(i + first)); ++ /* Save HMAC context registers */ ++ for (i = 0 ; i < hmac_nb_reg; i++) ++ c->csr[i + nb_reg] = io_read32(base + _HASH_CSR(i + ++ hmac_first)); ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result restore_context(struct stm32_hash_context *c) ++{ ++ size_t i = 0; ++ size_t nb_reg = 0; ++ size_t first = 0; ++ size_t hmac_nb_reg = 0; ++ size_t hmac_first = 0; ++ vaddr_t base = hash_base(c); ++ ++ io_write32(base + _HASH_IMR, c->imr); ++ io_write32(base + _HASH_STR, c->str); ++ io_write32(base + _HASH_CR, c->cr | _HASH_CR_INIT); ++ ++ get_save_registers(c, &nb_reg, &first, &hmac_nb_reg, &hmac_first); ++ ++ if (!c->csr) ++ return TEE_ERROR_BAD_STATE; ++ ++ /* Restore context registers */ ++ for (i = 0; i < nb_reg; i++) ++ io_write32(hash_base(c) + _HASH_CSR(i + first), c->csr[i]); ++ ++ /* Restore HMAC context registers */ ++ for (i = 0 ; i < hmac_nb_reg; i++) ++ io_write32(hash_base(c) + _HASH_CSR(i + hmac_first), ++ c->csr[i + nb_reg]); ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result hw_init(struct stm32_hash_context *c, const uint8_t *key, ++ size_t len) ++{ ++ uint32_t reg_cr = 0; ++ vaddr_t base = hash_base(c); ++ ++ reg_cr = _HASH_CR_INIT | _HASH_CR_DATATYPE_BYTE; ++ ++ switch (c->algo) { ++ case STM32_HASH_MD5: ++ reg_cr |= _HASH_CR_ALGO_MD5; ++ break; ++ case STM32_HASH_SHA1: ++ reg_cr |= _HASH_CR_ALGO_SHA1; ++ break; ++ case STM32_HASH_SHA224: ++ reg_cr |= _HASH_CR_ALGO_SHA224; ++ break; ++ case STM32_HASH_SHA384: ++ reg_cr |= _HASH_CR_ALGO_SHA384; ++ break; ++ case STM32_HASH_SHA512: ++ reg_cr |= _HASH_CR_ALGO_SHA512; ++ break; ++ case STM32_HASH_SHA3_224: ++ reg_cr |= _HASH_CR_ALGO_SHA3_224; ++ break; ++ case STM32_HASH_SHA3_256: ++ reg_cr |= _HASH_CR_ALGO_SHA3_256; ++ break; ++ case STM32_HASH_SHA3_384: ++ reg_cr |= _HASH_CR_ALGO_SHA3_384; ++ break; ++ case STM32_HASH_SHA3_512: ++ reg_cr |= _HASH_CR_ALGO_SHA3_512; ++ break; ++ /* Default selected algo is SHA256 */ ++ case STM32_HASH_SHA256: ++ if (c->dev->pdata.compat->caps & CAPS_MD5) ++ reg_cr |= _HASH_CR_ALGO_SHA256_IF_MD5; ++ else ++ reg_cr |= _HASH_CR_ALGO_SHA256; ++ ++ break; ++ default: ++ return TEE_ERROR_BAD_STATE; ++ } ++ ++ if (c->mode == STM32_HMAC_MODE) { ++ reg_cr |= _HASH_CR_MODE; ++ ++ if (len > c->block_size) ++ reg_cr |= _HASH_CR_LKEY; ++ ++ io_write32(base + _HASH_CR, reg_cr); ++ ++ return write_key(base, key, len); ++ } ++ ++ io_write32(base + _HASH_CR, reg_cr); ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result hash_get_digest(struct stm32_hash_context *c, uint8_t *digest) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ uint32_t i = 0U; ++ uint32_t dsg = 0U; ++ ++ res = wait_digest_ready(hash_base(c)); ++ if (res) ++ return res; ++ ++ for (i = 0U; i < c->digest_u32; i++) { ++ dsg = FROMBE32(io_read32(hash_base(c) + _HASH_HR(i))); ++ memcpy(digest + (i * sizeof(uint32_t)), &dsg, sizeof(uint32_t)); ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++size_t stm32_hash_digest_size(struct stm32_hash_context *c) ++{ ++ assert(c); ++ ++ return c->digest_u32 * sizeof(uint32_t); ++} ++ ++TEE_Result stm32_hash_deep_copy(struct stm32_hash_context *dst, ++ struct stm32_hash_context *src) ++{ ++ size_t nb_reg = 0; ++ size_t first = 0; ++ size_t hmac_nb_reg = 0; ++ size_t hmac_first = 0; ++ uint32_t *dst_buf = NULL; ++ uint32_t *dst_csr = NULL; ++ ++ if (!dst || !src || dst->mode != src->mode || dst->algo != src->algo) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ dst_buf = dst->remain.buf; ++ dst_csr = dst->csr; ++ memcpy(dst, src, sizeof(*dst)); ++ dst->remain.buf = dst_buf; ++ dst->csr = dst_csr; ++ ++ memcpy(dst->remain.buf, src->remain.buf, dst->remain.len); ++ get_save_registers(dst, &nb_reg, &first, &hmac_nb_reg, &hmac_first); ++ memcpy(dst->csr, src->csr, (nb_reg + hmac_nb_reg) * sizeof(uint32_t)); ++ ++ return TEE_SUCCESS; ++} ++ ++TEE_Result stm32_hash_alloc(struct stm32_hash_context *c, ++ enum stm32_hash_mode mode, ++ enum stm32_hash_algo algo) ++{ ++ size_t nb_reg = 0; ++ size_t first = 0; ++ size_t hmac_nb_reg = 0; ++ size_t hmac_first = 0; ++ ++ assert(c); ++ ++ /* Check if initialized */ ++ if (!stm32_hash) ++ return TEE_ERROR_NOT_IMPLEMENTED; ++ ++ c->dev = stm32_hash; ++ c->mode = mode; ++ c->algo = algo; ++ ++ switch (algo) { ++ case STM32_HASH_MD5: ++ if (!(c->dev->pdata.compat->caps & CAPS_MD5)) ++ return TEE_ERROR_NOT_IMPLEMENTED; ++ ++ c->digest_u32 = MD5_DIGEST_U32; ++ c->block_size = MD5_BLOCK_SIZE; ++ c->save_mode = SAVE_SMALL; ++ break; ++ case STM32_HASH_SHA1: ++ if (!(c->dev->pdata.compat->caps & CAPS_SHA1)) ++ return TEE_ERROR_NOT_IMPLEMENTED; ++ ++ c->digest_u32 = SHA1_DIGEST_U32; ++ c->block_size = SHA1_BLOCK_SIZE; ++ c->save_mode = SAVE_SMALL; ++ break; ++ case STM32_HASH_SHA224: ++ if (!(c->dev->pdata.compat->caps & CAPS_SHA2_224)) ++ return TEE_ERROR_NOT_IMPLEMENTED; ++ ++ c->digest_u32 = SHA224_DIGEST_U32; ++ c->block_size = SHA224_BLOCK_SIZE; ++ c->save_mode = SAVE_SMALL; ++ break; ++ case STM32_HASH_SHA384: ++ if (!(c->dev->pdata.compat->caps & CAPS_SHA2_384)) ++ return TEE_ERROR_NOT_IMPLEMENTED; ++ ++ c->digest_u32 = SHA384_DIGEST_U32; ++ c->block_size = SHA384_BLOCK_SIZE; ++ c->save_mode = SAVE_BIG; ++ break; ++ case STM32_HASH_SHA512: ++ if (!(c->dev->pdata.compat->caps & CAPS_SHA2_512)) ++ return TEE_ERROR_NOT_IMPLEMENTED; ++ ++ c->digest_u32 = SHA512_DIGEST_U32; ++ c->block_size = SHA512_BLOCK_SIZE; ++ c->save_mode = SAVE_BIG; ++ break; ++ case STM32_HASH_SHA3_224: ++ if (!(c->dev->pdata.compat->caps & CAPS_SHA3)) ++ return TEE_ERROR_NOT_IMPLEMENTED; ++ ++ c->digest_u32 = SHA3_224_DIGEST_U32; ++ c->block_size = SHA3_224_BLOCK_SIZE; ++ c->save_mode = SAVE_SHA3; ++ break; ++ case STM32_HASH_SHA3_256: ++ if (!(c->dev->pdata.compat->caps & CAPS_SHA3)) ++ return TEE_ERROR_NOT_IMPLEMENTED; ++ ++ c->digest_u32 = SHA3_256_DIGEST_U32; ++ c->block_size = SHA3_256_BLOCK_SIZE; ++ c->save_mode = SAVE_SHA3; ++ break; ++ case STM32_HASH_SHA3_384: ++ if (!(c->dev->pdata.compat->caps & CAPS_SHA3)) ++ return TEE_ERROR_NOT_IMPLEMENTED; ++ ++ c->digest_u32 = SHA3_384_DIGEST_U32; ++ c->block_size = SHA3_384_BLOCK_SIZE; ++ c->save_mode = SAVE_SHA3; ++ break; ++ case STM32_HASH_SHA3_512: ++ if (!(c->dev->pdata.compat->caps & CAPS_SHA3)) ++ return TEE_ERROR_NOT_IMPLEMENTED; ++ ++ c->digest_u32 = SHA3_512_DIGEST_U32; ++ c->block_size = SHA3_512_BLOCK_SIZE; ++ c->save_mode = SAVE_SHA3; ++ break; ++ /* Default selected algo is SHA256 */ ++ case STM32_HASH_SHA256: ++ if (!(c->dev->pdata.compat->caps & CAPS_SHA2_256)) ++ return TEE_ERROR_NOT_IMPLEMENTED; ++ ++ c->digest_u32 = SHA256_DIGEST_U32; ++ c->block_size = SHA256_BLOCK_SIZE; ++ c->save_mode = SAVE_SMALL; ++ break; ++ default: ++ return TEE_ERROR_NOT_IMPLEMENTED; ++ } ++ ++ /* ++ * The queue size is block_size + one register at first ++ * then block_size. ++ * So we may need to save at max queue_size + 3 bytes. ++ * Let allocate a number of uin32_t: queue_size + 4. ++ */ ++ c->remain.buf = calloc(c->block_size + sizeof(uint32_t), 1); ++ if (!c->remain.buf) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ get_save_registers(c, &nb_reg, &first, &hmac_nb_reg, &hmac_first); ++ ++ c->csr = calloc(nb_reg + hmac_nb_reg, sizeof(uint32_t)); ++ if (!c->csr) { ++ free(c->remain.buf); ++ return TEE_ERROR_OUT_OF_MEMORY; ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++void stm32_hash_free(struct stm32_hash_context *c) ++{ ++ if (!c) ++ return; ++ ++ free(c->remain.buf); ++ free(c->csr); ++} ++ ++TEE_Result stm32_hash_update(struct stm32_hash_context *c, ++ const uint8_t *buffer, size_t len) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ size_t next_queue_size = c->queue_size; ++ ++ assert(c); ++ ++ if (!len || !buffer) ++ return TEE_SUCCESS; ++ ++ mutex_lock(&c->dev->lock); ++ clk_enable(c->dev->pdata.clock); ++ ++ res = restore_context(c); ++ if (res) ++ goto exit; ++ ++ /* We cannot fill the fifo */ ++ if (c->remain.len + len < c->queue_size) { ++ if (!c->remain.buf) { ++ res = TEE_ERROR_BAD_STATE; ++ goto exit; ++ } ++ ++ memcpy(((uint8_t *)c->remain.buf) + c->remain.len, buffer, ++ len); ++ c->remain.len += len; ++ /* ++ * We don't need to save status as we didn't change IP ++ * internal state. ++ */ ++ goto exit; ++ } ++ ++ /* ++ * First write data saved in previous update ++ */ ++ if (c->remain.len) { ++ size_t align = 0; ++ size_t i = 0; ++ ++ if (!c->remain.buf) { ++ res = TEE_ERROR_BAD_STATE; ++ goto exit; ++ } ++ ++ /* Add bytes needed to align saved data */ ++ align = ROUNDUP(c->remain.len, sizeof(uint32_t)) - ++ c->remain.len; ++ memcpy(((uint8_t *)c->remain.buf) + c->remain.len, buffer, ++ align); ++ c->remain.len += align; ++ buffer += align; ++ len -= align; ++ ++ for (i = 0; i < c->remain.len / sizeof(uint32_t); i++) { ++ res = hash_write_data(hash_base(c), c->remain.buf[i]); ++ if (res) ++ goto exit; ++ c->remain.buf[i] = 0; /* Reset to 0 */ ++ } ++ ++ /* No more saved data */ ++ c->remain.len = 0; ++ } ++ ++ /* ++ * We will fill the queue for the first time, now queue flush will ++ * happen exactly after block_size bytes. ++ */ ++ if (len >= c->queue_size) ++ next_queue_size = c->block_size; ++ else ++ next_queue_size = c->queue_size; ++ ++ while (len >= c->queue_size || ++ !(io_read32(hash_base(c) + _HASH_SR) & _HASH_SR_DINIS)) { ++ uint32_t tmp_buf = 0; ++ ++ memcpy(&tmp_buf, buffer, sizeof(uint32_t)); ++ res = hash_write_data(hash_base(c), tmp_buf); ++ if (res) ++ goto exit; ++ ++ buffer += sizeof(uint32_t); ++ len -= sizeof(uint32_t); ++ } ++ ++ c->queue_size = next_queue_size; ++ ++ if (len) { ++ assert(c->remain.len == 0); ++ ++ if (!c->remain.buf) { ++ res = TEE_ERROR_BAD_STATE; ++ goto exit; ++ } ++ ++ memcpy((uint8_t *)c->remain.buf, buffer, len); ++ c->remain.len = len; ++ } ++ ++ res = save_context(c); ++ ++exit: ++ clk_disable(c->dev->pdata.clock); ++ mutex_unlock(&c->dev->lock); ++ ++ return res; ++} ++ ++TEE_Result stm32_hash_final(struct stm32_hash_context *c, uint8_t *digest, ++ const uint8_t *key, size_t len) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ vaddr_t base = hash_base(c); ++ ++ assert(c); ++ ++ if ((!key || !len) && c->mode != STM32_HASH_MODE) ++ return TEE_ERROR_BAD_STATE; ++ ++ mutex_lock(&c->dev->lock); ++ clk_enable(c->dev->pdata.clock); ++ ++ res = restore_context(c); ++ if (res) ++ goto exit; ++ ++ if (c->remain.len) { ++ size_t i = 0; ++ ++ if (!c->remain.buf) { ++ res = TEE_ERROR_BAD_STATE; ++ goto exit; ++ } ++ ++ for (i = 0; ++ i < ROUNDUP_DIV(c->remain.len, sizeof(uint32_t)); ++ i++) { ++ res = hash_write_data(base, c->remain.buf[i]); ++ if (res) ++ goto exit; ++ c->remain.buf[i] = 0; /* Reset to 0 */ ++ } ++ ++ io_clrsetbits32(base + _HASH_STR, _HASH_STR_NBLW_MASK, ++ 8U * (c->remain.len % sizeof(uint32_t))); ++ ++ /* No more saved data */ ++ c->remain.len = 0; ++ } else { ++ io_clrbits32(base + _HASH_STR, _HASH_STR_NBLW_MASK); ++ } ++ ++ io_setbits32(base + _HASH_STR, _HASH_STR_DCAL); ++ ++ if (c->mode == STM32_HMAC_MODE) { ++ res = write_key(base, key, len); ++ if (res) ++ goto exit; ++ } ++ ++ res = hash_get_digest(c, digest); ++ ++exit: ++ clk_disable(c->dev->pdata.clock); ++ mutex_unlock(&c->dev->lock); ++ ++ return res; ++} ++ ++TEE_Result stm32_hash_init(struct stm32_hash_context *c, ++ const uint8_t *key, size_t len) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ ++ assert(c); ++ ++ if ((!key || !len) && c->mode != STM32_HASH_MODE) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ mutex_lock(&c->dev->lock); ++ clk_enable(c->dev->pdata.clock); ++ ++ c->remain.len = 0; ++ /* First queue is block_size + one register */ ++ c->queue_size = c->block_size + sizeof(uint32_t); ++ memset(c->remain.buf, 0, c->queue_size); ++ ++ res = hw_init(c, key, len); ++ if (res) ++ goto exit; ++ ++ res = save_context(c); ++ ++exit: ++ clk_disable(c->dev->pdata.clock); ++ mutex_unlock(&c->dev->lock); ++ ++ return res; ++} ++ ++#ifdef CFG_EMBED_DTB ++static TEE_Result stm32_hash_parse_fdt(struct stm32_hash_platdata *pdata, ++ const void *fdt, int node, ++ const void *compat_data) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ struct dt_node_info dt_info = {}; ++ ++ _fdt_fill_device_info(fdt, &dt_info, node); ++ ++ if (dt_info.reg == DT_INFO_INVALID_REG || ++ dt_info.reg_size == DT_INFO_INVALID_REG_SIZE || ++ dt_info.reset == DT_INFO_INVALID_RESET) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ pdata->base.pa = dt_info.reg; ++ io_pa_or_va_secure(&pdata->base, dt_info.reg_size); ++ if (!pdata->base.va) ++ panic(); ++ ++ pdata->reset = (unsigned int)dt_info.reset; ++ ++ res = clk_dt_get_by_index(fdt, node, 0, &pdata->clock); ++ if (res) ++ return res; ++ ++ pdata->compat = (struct stm32_hash_compat *)compat_data; ++ ++ return TEE_SUCCESS; ++} ++ ++__weak ++TEE_Result stm32_hash_get_platdata(struct stm32_hash_platdata *pdata __unused) ++{ ++ /* In DT config, the platform data are filled by DT file */ ++ return TEE_SUCCESS; ++} ++#else /* CFG_EMBED_DTB */ ++static TEE_Result stm32_hash_parse_fdt(struct stm32_hash_platdata *pdata, ++ const void *fdt, int node, ++ const void *compat_data) ++{ ++ /* Do nothing, there is no fdt to parse in this case */ ++ return TEE_SUCCESS; ++} ++ ++/* ++ * This function can be overridden by platform to define pdata of HASH driver ++ */ ++__weak ++TEE_Result stm32_hash_get_platdata(struct stm32_hash_platdata *pdata __unused) ++{ ++ return TEE_ITEM_NO_FOUND; ++} ++#endif ++ ++/* ++ * Initialize HASH driver. ++ * ++ * return TEE_SUCCESS if OK. ++ */ ++static TEE_Result stm32_hash_probe(const void *fdt, int node, ++ const void *compat_data) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ vaddr_t base = 0; ++ uint32_t __unused rev = 0; ++ struct stm32_hash_platdata temp_pdata = { }; ++ ++ res = stm32_hash_get_platdata(&temp_pdata); ++ if (res) ++ return res; ++ ++ res = stm32_hash_parse_fdt(&temp_pdata, fdt, node, compat_data); ++ if (res) ++ return res; ++ ++ stm32_hash = calloc(1, sizeof(*stm32_hash)); ++ ++ if (!stm32_hash) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ memcpy(&stm32_hash->pdata, &temp_pdata, sizeof(temp_pdata)); ++ ++ clk_enable(stm32_hash->pdata.clock); ++ ++ base = io_pa_or_va(&stm32_hash->pdata.base, 1); ++ rev = io_read32(base + _HASH_VERR); ++ FMSG("STM32 HASH V%u/%u", (rev & _HASH_VERR_MAJREV) >> 4, ++ rev & _HASH_VERR_MINREV); ++ ++ if (stm32_reset_assert(stm32_hash->pdata.reset, RESET_TIMEOUT_US_1MS)) ++ panic(); ++ ++ if (stm32_reset_deassert(stm32_hash->pdata.reset, RESET_TIMEOUT_US_1MS)) ++ panic(); ++ ++ mutex_init(&stm32_hash->lock); ++ ++ clk_disable(stm32_hash->pdata.clock); ++ ++ if (IS_ENABLED(CFG_CRYPTO_DRV_HASH)) { ++ res = stm32_register_hash(); ++ if (res) { ++ EMSG("Failed to register to hash: %#"PRIx32, res); ++ panic(); ++ } ++ } ++ ++ if (IS_ENABLED(CFG_CRYPTO_DRV_MAC)) { ++ res = stm32_register_hmac(); ++ if (res) { ++ EMSG("Failed to register to mac: %#"PRIx32, res); ++ panic(); ++ } ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++#if CFG_EMBED_DTB ++static const struct stm32_hash_compat mp13_compat = { ++ .caps = (CAPS_SHA1 | CAPS_SHA2_224 | CAPS_SHA2_256 | CAPS_SHA2_384 | ++ CAPS_SHA2_512 | CAPS_SHA3), ++}; ++ ++static const struct stm32_hash_compat mp15_compat = { ++ .caps = CAPS_MD5 | CAPS_SHA1 | CAPS_SHA2_224 | CAPS_SHA2_256, ++}; ++ ++static const struct dt_device_match hash_match_table[] = { ++ { .compatible = "st,stm32mp13-hash", .compat_data = &mp13_compat}, ++ { .compatible = "st,stm32f756-hash", .compat_data = &mp15_compat}, ++ { } ++}; ++ ++DEFINE_DT_DRIVER(stm32_hash_dt_driver) = { ++ .name = "stm32-hash", ++ .match_table = hash_match_table, ++ .probe = &stm32_hash_probe, ++}; ++#endif +diff --git a/core/drivers/crypto/stm32/stm32_hash.h b/core/drivers/crypto/stm32/stm32_hash.h +new file mode 100644 +index 000000000..4400d080a +--- /dev/null ++++ b/core/drivers/crypto/stm32/stm32_hash.h +@@ -0,0 +1,78 @@ ++/* SPDX-License-Identifier: BSD-2-Clause */ ++/* ++ * Copyright (c) 2021, STMicroelectronics - All Rights Reserved ++ */ ++ ++#ifndef STM32_HASH_H ++#define STM32_HASH_H ++ ++#include ++#include ++#include ++ ++struct stm32_hash_compat { ++ uint32_t caps; ++}; ++ ++struct stm32_hash_platdata { ++ struct io_pa_va base; ++ struct clk *clock; ++ unsigned int reset; ++ struct stm32_hash_compat *compat; ++}; ++ ++enum stm32_hash_algo { ++ STM32_HASH_MD5, ++ STM32_HASH_SHA1, ++ STM32_HASH_SHA224, ++ STM32_HASH_SHA256, ++ STM32_HASH_SHA384, ++ STM32_HASH_SHA512, ++ STM32_HASH_SHA3_224, ++ STM32_HASH_SHA3_256, ++ STM32_HASH_SHA3_384, ++ STM32_HASH_SHA3_512, ++}; ++ ++enum stm32_hash_mode { ++ STM32_HMAC_MODE, ++ STM32_HASH_MODE, ++}; ++ ++struct stm32_hash_remain { ++ uint32_t *buf; ++ size_t len; ++}; ++ ++struct stm32_hash_context { ++ struct stm32_hash_device *dev; ++ size_t digest_u32; ++ size_t block_size; ++ size_t queue_size; ++ struct stm32_hash_remain remain; ++ enum stm32_hash_mode mode; ++ enum stm32_hash_algo algo; ++ uint32_t save_mode; ++ uint32_t imr; ++ uint32_t str; ++ uint32_t cr; ++ uint32_t *csr; ++}; ++ ++TEE_Result stm32_hash_get_platdata(struct stm32_hash_platdata *pdata); ++ ++size_t stm32_hash_digest_size(struct stm32_hash_context *c); ++TEE_Result stm32_hash_deep_copy(struct stm32_hash_context *dst, ++ struct stm32_hash_context *src); ++TEE_Result stm32_hash_alloc(struct stm32_hash_context *c, ++ enum stm32_hash_mode mode, ++ enum stm32_hash_algo algo); ++void stm32_hash_free(struct stm32_hash_context *c); ++ ++TEE_Result stm32_hash_update(struct stm32_hash_context *ctx, ++ const uint8_t *buffer, size_t length); ++TEE_Result stm32_hash_final(struct stm32_hash_context *c, uint8_t *digest, ++ const uint8_t *key, size_t len); ++TEE_Result stm32_hash_init(struct stm32_hash_context *ctx, ++ const uint8_t *key, size_t len); ++#endif /* STM32_HASH_H */ +diff --git a/core/drivers/crypto/stm32/stm32_pka.c b/core/drivers/crypto/stm32/stm32_pka.c +new file mode 100644 +index 000000000..57b6e0a65 +--- /dev/null ++++ b/core/drivers/crypto/stm32/stm32_pka.c +@@ -0,0 +1,1628 @@ ++// SPDX-License-Identifier: BSD-3-Clause ++/* ++ * Copyright (c) 2021, STMicroelectronics - All Rights Reserved ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#include "common.h" ++#include "stm32_pka.h" ++ ++/* ++ * For our comprehension in this file ++ * _len are in BITs ++ * _size are in BYTEs ++ * _nbw are in number of PKA_word (PKA_word = u64) ++ */ ++ ++#define INT8_LEN 8U ++#define INT64_LEN (INT8_LEN * sizeof(uint64_t)) ++#define WORD_SIZE (sizeof(uint64_t)) ++#define OP_NBW_FROM_LEN(len) (ROUNDUP_DIV(len, INT64_LEN) + 1) ++#define OP_NBW_FROM_SIZE(s) OP_NBW_FROM_LEN((s) * INT8_LEN) ++#define OP_SIZE_FROM_SIZE(s) (OP_NBW_FROM_SIZE(s) * WORD_SIZE) ++ ++#define MAX_EO_NBW OP_NBW_FROM_LEN(PKA_MAX_ECC_LEN) ++ ++/* PKA registers */ ++#define _PKA_CR 0x0U ++#define _PKA_SR 0x4U ++#define _PKA_CLRFR 0x8U ++#define _PKA_VERR 0x1FF4 ++#define _PKA_IPIDR 0x1FF8U ++ ++/* PKA control register fields */ ++#define _PKA_CR_MODE_MASK GENMASK_32(13, 8) ++#define _PKA_CR_MODE_SHIFT 8U ++#define _PKA_CR_MODE_ADD 0x9U ++#define _PKA_CR_MODE_R2MODN 0x01U ++#define _PKA_CR_MODE_POINT_CHECK 0x28U ++#define _PKA_CR_MODE_ECC_KP 0x20U ++#define _PKA_CR_MODE_ECDSA_SIGN 0x24U ++#define _PKA_CR_MODE_ECDSA_VERIF 0x26U ++#define _PKA_CR_START BIT(1) ++#define _PKA_CR_EN BIT(0) ++ ++/* PKA status register fields */ ++#define _PKA_SR_BUSY BIT(16) ++#define _PKA_SR_LMF BIT(1) ++#define _PKA_SR_INITOK BIT(0) ++ ++/* PKA it flag fields (used in CR, SR and CLRFR) */ ++#define _PKA_IT_MASK (GENMASK_32(21, 19) | BIT(17)) ++#define _PKA_IT_SHIFT 17U ++#define _PKA_IT_OPERR BIT(21) ++#define _PKA_IT_ADDRERR BIT(20) ++#define _PKA_IT_RAMERR BIT(19) ++#define _PKA_IT_PROCEND BIT(17) ++ ++/* PKA version register fields */ ++#define _PKA_VERR_MAJREV_MASK GENMASK_32(7, 4) ++#define _PKA_VERR_MAJREV_SHIFT 4U ++#define _PKA_VERR_MINREV_MASK GENMASK_32(3, 0) ++#define _PKA_VERR_MINREV_SHIFT 0U ++ ++/* PKA identification register value */ ++#define _PKA_IDID 0x00170072U ++ ++/* RAM magic offset */ ++#define _PKA_RAM_START 0x400U ++#define _PKA_RAM_SIZE 5336U ++ ++/* Montgomery parameter computation (R*R mod n) */ ++#define _PKA_RAM_R2MODN_N_LEN 0x408U /* 64 */ ++#define _PKA_RAM_R2MODN_PRIME_N 0x1088U /* EOS */ ++#define _PKA_RAM_R2MODN_OUT 0x620U /* EOS */ ++ ++/* ECC check if point P is on curve */ ++#define _PKA_RAM_ONCURVE_N_LEN 0x408U /* 64 */ ++#define _PKA_RAM_ONCURVE_A_SIGN 0x410U /* 64 */ ++#define _PKA_RAM_ONCURVE_A 0x418U /* EOS */ ++#define _PKA_RAM_ONCURVE_B 0x520U /* EOS */ ++#define _PKA_RAM_ONCURVE_P 0x470U /* EOS */ ++#define _PKA_RAM_ONCURVE_XP 0x578U /* EOS */ ++#define _PKA_RAM_ONCURVE_YP 0x5D0U /* EOS */ ++#define _PKA_RAM_ONCURVE_R2MODN 0x4C8U /* EOS */ ++#define _PKA_RAM_ONCURVE_RES 0x680U /* 64 */ ++#define _PKA_RAM_ONCURVE_RES_YES 0xD60DULL ++#define _PKA_RAM_ONCURVE_RES_NO 0xA3B7ULL ++#define _PKA_RAM_ONCURVE_RES_TOOBIG 0xF946ULL ++ ++/* ECC Fp scalar multiplication (kP) */ ++#define _PKA_RAM_KP_N_LEN 0x400U /* 64 */ ++#define _PKA_RAM_KP_P_LEN 0x408U /* 64 */ ++#define _PKA_RAM_KP_A_SIGN 0x410U /* 64 */ ++#define _PKA_RAM_KP_A 0x418U /* EOS */ ++#define _PKA_RAM_KP_B 0x520U /* EOS */ ++#define _PKA_RAM_KP_P 0x1088U /* EOS */ ++#define _PKA_RAM_KP_K 0x12A0U /* EOS */ ++#define _PKA_RAM_KP_XP 0x578U /* EOS */ ++#define _PKA_RAM_KP_YP 0x470U /* EOS */ ++#define _PKA_RAM_KP_PRIME_N 0xF88U /* EOS */ ++#define _PKA_RAM_KP_RES 0x680U /* 64 */ ++#define _PKA_RAM_KP_RES_SUCCESS 0xD60DULL ++#define _PKA_RAM_KP_RES_FAIL 0xCBC9ULL ++#define _PKA_RAM_KP_X 0x578U /* EOS*/ ++#define _PKA_RAM_KP_Y 0x5D0U /* EOS*/ ++ ++/* ECDSA sign */ ++#define _PKA_RAM_SIGN_N_LEN 0x400U /* 64 */ ++#define _PKA_RAM_SIGN_P_LEN 0x408U /* 64 */ ++#define _PKA_RAM_SIGN_A_SIGN 0x410U /* 64 */ ++#define _PKA_RAM_SIGN_A 0x418U /* EOS */ ++#define _PKA_RAM_SIGN_B 0x520U /* EOS */ ++#define _PKA_RAM_SIGN_P 0x1088U /* EOS */ ++#define _PKA_RAM_SIGN_K 0x12A0U /* EOS */ ++#define _PKA_RAM_SIGN_XG 0x578U /* EOS */ ++#define _PKA_RAM_SIGN_YG 0x470U /* EOS */ ++#define _PKA_RAM_SIGN_HASH_Z 0xFE8U /* EOS */ ++#define _PKA_RAM_SIGN_D 0xF28U /* EOS */ ++#define _PKA_RAM_SIGN_PRIME_N 0xF88U /* EOS */ ++#define _PKA_RAM_SIGN_RES 0xFE0U /* 64 */ ++#define _PKA_RAM_SIGN_RES_SUCCESS 0xD60DULL ++#define _PKA_RAM_SIGN_RES_FAIL 0xCBC9ULL ++#define _PKA_RAM_SIGN_RES_R0 0xA3B7ULL ++#define _PKA_RAM_SIGN_RES_S0 0xF946ULL ++#define _PKA_RAM_SIGN_R 0x730U /* EOS*/ ++#define _PKA_RAM_SIGN_S 0x788U /* EOS*/ ++ ++/* ECDSA verification */ ++#define _PKA_RAM_VERIF_N_LEN 0x408U /* 64 */ ++#define _PKA_RAM_VERIF_P_LEN 0x4C8U /* 64 */ ++#define _PKA_RAM_VERIF_A_SIGN 0x468U /* 64 */ ++#define _PKA_RAM_VERIF_A 0x470U /* EOS */ ++#define _PKA_RAM_VERIF_P 0x4D0U /* EOS */ ++#define _PKA_RAM_VERIF_XG 0x678U /* EOS */ ++#define _PKA_RAM_VERIF_YG 0x6D0U /* EOS */ ++#define _PKA_RAM_VERIF_XQ 0x12F8U /* EOS */ ++#define _PKA_RAM_VERIF_YQ 0x1350U /* EOS */ ++#define _PKA_RAM_VERIF_SIGN_R 0x10E0U /* EOS */ ++#define _PKA_RAM_VERIF_SIGN_S 0xC68U /* EOS */ ++#define _PKA_RAM_VERIF_HASH_Z 0x13A8U /* EOS */ ++#define _PKA_RAM_VERIF_PRIME_N 0x1088U /* EOS */ ++#define _PKA_RAM_VERIF_RES 0x5D0U /* 64 */ ++#define _PKA_RAM_VERIF_RES_VALID 0xD60DULL ++#define _PKA_RAM_VERIF_RES_INVALID 0xA3B7ULL ++ ++#define PKA_TIMEOUT_US 1000000U ++#define TIMEOUT_US_1MS 1000U ++#define PKA_RESET_DELAY 20U ++ ++enum pka_op { ++ sign, ++ verif, ++ scalar_mul, ++ on_curve, ++ ++ pka_op_last ++}; ++ ++enum pka_ram_index { ++ N_LEN, ++ P_LEN, ++ A_SIGN, ++ COEFF_A, ++ COEFF_B, ++ PRIME_N, ++ VAL_P, ++ GPOINT_X, ++ GPOINT_Y, ++ ++ PKA_RAM_INDEX_LAST, ++}; ++ ++static uint32_t pka_ram[pka_op_last][PKA_RAM_INDEX_LAST] = { ++ [sign] = { ++ [N_LEN] = _PKA_RAM_SIGN_N_LEN, ++ [P_LEN] = _PKA_RAM_SIGN_P_LEN, ++ [A_SIGN] = _PKA_RAM_SIGN_A_SIGN, ++ [COEFF_A] = _PKA_RAM_SIGN_A, ++ [COEFF_B] = _PKA_RAM_SIGN_B, ++ [PRIME_N] = _PKA_RAM_SIGN_PRIME_N, ++ [VAL_P] = _PKA_RAM_SIGN_P, ++ [GPOINT_X] = _PKA_RAM_SIGN_XG, ++ [GPOINT_Y] = _PKA_RAM_SIGN_YG ++ }, ++ [verif] = { ++ [N_LEN] = _PKA_RAM_VERIF_N_LEN, ++ [P_LEN] = _PKA_RAM_VERIF_P_LEN, ++ [A_SIGN] = _PKA_RAM_VERIF_A_SIGN, ++ [COEFF_A] = _PKA_RAM_VERIF_A, ++ [COEFF_B] = 0U, ++ [PRIME_N] = _PKA_RAM_VERIF_PRIME_N, ++ [VAL_P] = _PKA_RAM_VERIF_P, ++ [GPOINT_X] = _PKA_RAM_VERIF_XG, ++ [GPOINT_Y] = _PKA_RAM_VERIF_YG ++ }, ++ [scalar_mul] = { ++ [N_LEN] = _PKA_RAM_KP_N_LEN, ++ [P_LEN] = _PKA_RAM_KP_P_LEN, ++ [A_SIGN] = _PKA_RAM_KP_A_SIGN, ++ [COEFF_A] = _PKA_RAM_KP_A, ++ [COEFF_B] = _PKA_RAM_KP_B, ++ [PRIME_N] = _PKA_RAM_KP_PRIME_N, ++ [VAL_P] = _PKA_RAM_KP_P, ++ [GPOINT_X] = 0, ++ [GPOINT_Y] = 0, ++ }, ++ [on_curve] = { ++ [N_LEN] = _PKA_RAM_ONCURVE_N_LEN, ++ [P_LEN] = 0, ++ [A_SIGN] = _PKA_RAM_ONCURVE_A_SIGN, ++ [COEFF_A] = _PKA_RAM_ONCURVE_A, ++ [COEFF_B] = _PKA_RAM_ONCURVE_B, ++ [PRIME_N] = 0, ++ [VAL_P] = _PKA_RAM_ONCURVE_P, ++ [GPOINT_X] = 0, ++ [GPOINT_Y] = 0, ++ }, ++}; ++ ++struct curve_parameters { ++ uint32_t a_sign; /* 0 positive, 1 negative */ ++ struct stm32_pka_bn a; /* Curve coefficient |a| */ ++ struct stm32_pka_bn b; /* Curve coefficient b */ ++ struct stm32_pka_bn p; /* Curve modulus value */ ++ uint32_t p_len; /* We also need the p len in bits */ ++ struct stm32_pka_point g; /* Curve base G point */ ++ struct stm32_pka_bn n; /* Curve prime order n */ ++ uint32_t n_len; /* We also need the n len in bits */ ++}; ++ ++static const struct curve_parameters curve_def[] = { ++ [PKA_NIST_P192] = { ++ .p_len = 192U, ++ .p = { .val = (uint8_t[]){0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xfe, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff}, ++ .size = 24U, ++ }, ++ .n_len = 192U, ++ .n = { .val = (uint8_t[]){0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0x99, 0xde, 0xf8, 0x36, ++ 0x14, 0x6b, 0xc9, 0xb1, ++ 0xb4, 0xd2, 0x28, 0x31}, ++ .size = 24U, ++ }, ++ .a_sign = 1U, ++ .a = { .val = (uint8_t[]){0x03}, ++ .size = 1U, ++ }, ++ .b = { .val = (uint8_t[]){0x64, 0x21, 0x05, 0x19, ++ 0xe5, 0x9c, 0x80, 0xe7, ++ 0x0f, 0xa7, 0xe9, 0xab, ++ 0x72, 0x24, 0x30, 0x49, ++ 0xfe, 0xb8, 0xde, 0xec, ++ 0xc1, 0x46, 0xb9, 0xb1}, ++ .size = 24U, ++ }, ++ .g = { ++ .x = { .val = (uint8_t[]){0x18, 0x8d, 0xa8, 0x0e, ++ 0xb0, 0x30, 0x90, 0xf6, ++ 0x7c, 0xbf, 0x20, 0xeb, ++ 0x43, 0xa1, 0x88, 0x00, ++ 0xf4, 0xff, 0x0a, 0xfd, ++ 0x82, 0xff, 0x10, 0x12}, ++ .size = 24U, ++ }, ++ .y = { .val = (uint8_t[]){0x07, 0x19, 0x2b, 0x95, ++ 0xff, 0xc8, 0xda, 0x78, ++ 0x63, 0x10, 0x11, 0xed, ++ 0x6b, 0x24, 0xcd, 0xd5, ++ 0x73, 0xf9, 0x77, 0xa1, ++ 0x1e, 0x79, 0x48, 0x11}, ++ .size = 24U, ++ }, ++ }, ++ }, ++ [PKA_NIST_P224] = { ++ .p_len = 224U, ++ .p = { .val = (uint8_t[]){0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0x00, 0x00, 0x00, 0x00, ++ 0x00, 0x00, 0x00, 0x00, ++ 0x00, 0x00, 0x00, 0x01}, ++ .size = 28U, ++ }, ++ .n_len = 224U, ++ .n = { .val = (uint8_t[]){0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0x16, 0xa2, ++ 0xe0, 0xb8, 0xf0, 0x3e, ++ 0x13, 0xdd, 0x29, 0x45, ++ 0x5c, 0x5c, 0x2a, 0x3d}, ++ .size = 28U, ++ }, ++ .a_sign = 1U, ++ .a = { .val = (uint8_t[]){0x03}, ++ .size = 1U, ++ }, ++ .b = { .val = (uint8_t[]){0xb4, 0x05, 0x0a, 0x85, ++ 0x0c, 0x04, 0xb3, 0xab, ++ 0xf5, 0x41, 0x32, 0x56, ++ 0x50, 0x44, 0xb0, 0xb7, ++ 0xd7, 0xbf, 0xd8, 0xba, ++ 0x27, 0x0b, 0x39, 0x43, ++ 0x23, 0x55, 0xff, 0xb4}, ++ .size = 28U, ++ }, ++ .g = { ++ .x = { .val = (uint8_t[]){0xb7, 0x0e, 0x0c, 0xbd, ++ 0x6b, 0xb4, 0xbf, 0x7f, ++ 0x32, 0x13, 0x90, 0xb9, ++ 0x4a, 0x03, 0xc1, 0xd3, ++ 0x56, 0xc2, 0x11, 0x22, ++ 0x34, 0x32, 0x80, 0xd6, ++ 0x11, 0x5c, 0x1d, 0x21}, ++ .size = 28U, ++ }, ++ .y = { .val = (uint8_t[]){0xbd, 0x37, 0x63, 0x88, ++ 0xb5, 0xf7, 0x23, 0xfb, ++ 0x4c, 0x22, 0xdf, 0xe6, ++ 0xcd, 0x43, 0x75, 0xa0, ++ 0x5a, 0x07, 0x47, 0x64, ++ 0x44, 0xd5, 0x81, 0x99, ++ 0x85, 0x00, 0x7e, 0x34}, ++ .size = 28U, ++ }, ++ }, ++ }, ++ [PKA_NIST_P256] = { ++ .p_len = 256U, ++ .p = { .val = (uint8_t[]){0xff, 0xff, 0xff, 0xff, ++ 0x00, 0x00, 0x00, 0x01, ++ 0x00, 0x00, 0x00, 0x00, ++ 0x00, 0x00, 0x00, 0x00, ++ 0x00, 0x00, 0x00, 0x00, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff}, ++ .size = 32U, ++ }, ++ .n_len = 256U, ++ .n = { .val = (uint8_t[]){0xff, 0xff, 0xff, 0xff, ++ 0x00, 0x00, 0x00, 0x00, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xbc, 0xe6, 0xfa, 0xad, ++ 0xa7, 0x17, 0x9e, 0x84, ++ 0xf3, 0xb9, 0xca, 0xc2, ++ 0xfc, 0x63, 0x25, 0x51}, ++ .size = 32U, ++ }, ++ .a_sign = 1U, ++ .a = { .val = (uint8_t[]){0x03}, ++ .size = 1U, ++ }, ++ .b = { .val = (uint8_t[]){0x5a, 0xc6, 0x35, 0xd8, ++ 0xaa, 0x3a, 0x93, 0xe7, ++ 0xb3, 0xeb, 0xbd, 0x55, ++ 0x76, 0x98, 0x86, 0xbc, ++ 0x65, 0x1d, 0x06, 0xb0, ++ 0xcc, 0x53, 0xb0, 0xf6, ++ 0x3b, 0xce, 0x3c, 0x3e, ++ 0x27, 0xd2, 0x60, 0x4b}, ++ .size = 32U, ++ }, ++ .g = { ++ .x = { .val = (uint8_t[]){0x6b, 0x17, 0xd1, 0xf2, ++ 0xe1, 0x2c, 0x42, 0x47, ++ 0xf8, 0xbc, 0xe6, 0xe5, ++ 0x63, 0xa4, 0x40, 0xf2, ++ 0x77, 0x03, 0x7d, 0x81, ++ 0x2d, 0xeb, 0x33, 0xa0, ++ 0xf4, 0xa1, 0x39, 0x45, ++ 0xd8, 0x98, 0xc2, 0x96}, ++ .size = 32U, ++ }, ++ .y = { .val = (uint8_t[]){0x4f, 0xe3, 0x42, 0xe2, ++ 0xfe, 0x1a, 0x7f, 0x9b, ++ 0x8e, 0xe7, 0xeb, 0x4a, ++ 0x7c, 0x0f, 0x9e, 0x16, ++ 0x2b, 0xce, 0x33, 0x57, ++ 0x6b, 0x31, 0x5e, 0xce, ++ 0xcb, 0xb6, 0x40, 0x68, ++ 0x37, 0xbf, 0x51, 0xf5}, ++ .size = 32U, ++ }, ++ }, ++ }, ++ [PKA_NIST_P384] = { ++ .p_len = 384U, ++ .p = { .val = (uint8_t[]){0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xfe, ++ 0xff, 0xff, 0xff, 0xff, ++ 0x00, 0x00, 0x00, 0x00, ++ 0x00, 0x00, 0x00, 0x00, ++ 0xff, 0xff, 0xff, 0xff}, ++ .size = 48U, ++ }, ++ .n_len = 384U, ++ .n = { .val = (uint8_t[]){0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xc7, 0x63, 0x4d, 0x81, ++ 0xf4, 0x37, 0x2d, 0xdf, ++ 0x58, 0x1a, 0x0d, 0xb2, ++ 0x48, 0xb0, 0xa7, 0x7a, ++ 0xec, 0xec, 0x19, 0x6a, ++ 0xcc, 0xc5, 0x29, 0x73}, ++ .size = 48U, ++ }, ++ .a_sign = 1U, ++ .a = { .val = (uint8_t[]){0x03}, ++ .size = 1U, ++ }, ++ .b = { .val = (uint8_t[]){0xb3, 0x31, 0x2f, 0xa7, ++ 0xe2, 0x3e, 0xe7, 0xe4, ++ 0x98, 0x8e, 0x05, 0x6b, ++ 0xe3, 0xf8, 0x2d, 0x19, ++ 0x18, 0x1d, 0x9c, 0x6e, ++ 0xfe, 0x81, 0x41, 0x12, ++ 0x03, 0x14, 0x08, 0x8f, ++ 0x50, 0x13, 0x87, 0x5a, ++ 0xc6, 0x56, 0x39, 0x8d, ++ 0x8a, 0x2e, 0xd1, 0x9d, ++ 0x2a, 0x85, 0xc8, 0xed, ++ 0xd3, 0xec, 0x2a, 0xef}, ++ .size = 48U, ++ }, ++ .g = { ++ .x = { .val = (uint8_t[]){0xaa, 0x87, 0xca, 0x22, ++ 0xbe, 0x8b, 0x05, 0x37, ++ 0x8e, 0xb1, 0xc7, 0x1e, ++ 0xf3, 0x20, 0xad, 0x74, ++ 0x6e, 0x1d, 0x3b, 0x62, ++ 0x8b, 0xa7, 0x9b, 0x98, ++ 0x59, 0xf7, 0x41, 0xe0, ++ 0x82, 0x54, 0x2a, 0x38, ++ 0x55, 0x02, 0xf2, 0x5d, ++ 0xbf, 0x55, 0x29, 0x6c, ++ 0x3a, 0x54, 0x5e, 0x38, ++ 0x72, 0x76, 0x0a, 0xb7}, ++ .size = 48U, ++ }, ++ .y = { .val = (uint8_t[]){0x36, 0x17, 0xde, 0x4a, ++ 0x96, 0x26, 0x2c, 0x6f, ++ 0x5d, 0x9e, 0x98, 0xbf, ++ 0x92, 0x92, 0xdc, 0x29, ++ 0xf8, 0xf4, 0x1d, 0xbd, ++ 0x28, 0x9a, 0x14, 0x7c, ++ 0xe9, 0xda, 0x31, 0x13, ++ 0xb5, 0xf0, 0xb8, 0xc0, ++ 0x0a, 0x60, 0xb1, 0xce, ++ 0x1d, 0x7e, 0x81, 0x9d, ++ 0x7a, 0x43, 0x1d, 0x7c, ++ 0x90, 0xea, 0x0e, 0x5f}, ++ .size = 48U, ++ }, ++ }, ++ }, ++ [PKA_NIST_P521] = { ++ .p_len = 521U, ++ .p = { .val = (uint8_t[]){ 0x01, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff}, ++ .size = 66U, ++ }, ++ .n_len = 521U, ++ .n = { .val = (uint8_t[]){ 0x01, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xff, ++ 0xff, 0xff, 0xff, 0xfa, ++ 0x51, 0x86, 0x87, 0x83, ++ 0xbf, 0x2f, 0x96, 0x6b, ++ 0x7f, 0xcc, 0x01, 0x48, ++ 0xf7, 0x09, 0xa5, 0xd0, ++ 0x3b, 0xb5, 0xc9, 0xb8, ++ 0x89, 0x9c, 0x47, 0xae, ++ 0xbb, 0x6f, 0xb7, 0x1e, ++ 0x91, 0x38, 0x64, 0x09}, ++ .size = 66U, ++ }, ++ .a_sign = 1U, ++ .a = { .val = (uint8_t[]){0x03}, ++ .size = 1U, ++ }, ++ .b = { .val = (uint8_t[]){ 0x51, ++ 0x95, 0x3e, 0xb9, 0x61, ++ 0x8e, 0x1c, 0x9a, 0x1f, ++ 0x92, 0x9a, 0x21, 0xa0, ++ 0xb6, 0x85, 0x40, 0xee, ++ 0xa2, 0xda, 0x72, 0x5b, ++ 0x99, 0xb3, 0x15, 0xf3, ++ 0xb8, 0xb4, 0x89, 0x91, ++ 0x8e, 0xf1, 0x09, 0xe1, ++ 0x56, 0x19, 0x39, 0x51, ++ 0xec, 0x7e, 0x93, 0x7b, ++ 0x16, 0x52, 0xc0, 0xbd, ++ 0x3b, 0xb1, 0xbf, 0x07, ++ 0x35, 0x73, 0xdf, 0x88, ++ 0x3d, 0x2c, 0x34, 0xf1, ++ 0xef, 0x45, 0x1f, 0xd4, ++ 0x6b, 0x50, 0x3f, 0x00}, ++ .size = 65U, ++ }, ++ .g = { ++ .x = { .val = (uint8_t[]){ 0xc6, ++ 0x85, 0x8e, 0x06, 0xb7, ++ 0x04, 0x04, 0xe9, 0xcd, ++ 0x9e, 0x3e, 0xcb, 0x66, ++ 0x23, 0x95, 0xb4, 0x42, ++ 0x9c, 0x64, 0x81, 0x39, ++ 0x05, 0x3f, 0xb5, 0x21, ++ 0xf8, 0x28, 0xaf, 0x60, ++ 0x6b, 0x4d, 0x3d, 0xba, ++ 0xa1, 0x4b, 0x5e, 0x77, ++ 0xef, 0xe7, 0x59, 0x28, ++ 0xfe, 0x1d, 0xc1, 0x27, ++ 0xa2, 0xff, 0xa8, 0xde, ++ 0x33, 0x48, 0xb3, 0xc1, ++ 0x85, 0x6a, 0x42, 0x9b, ++ 0xf9, 0x7e, 0x7e, 0x31, ++ 0xc2, 0xe5, 0xbd, 0x66}, ++ .size = 65U, ++ }, ++ .y = { .val = (uint8_t[]){ 0x01, 0x18, ++ 0x39, 0x29, 0x6a, 0x78, ++ 0x9a, 0x3b, 0xc0, 0x04, ++ 0x5c, 0x8a, 0x5f, 0xb4, ++ 0x2c, 0x7d, 0x1b, 0xd9, ++ 0x98, 0xf5, 0x44, 0x49, ++ 0x57, 0x9b, 0x44, 0x68, ++ 0x17, 0xaf, 0xbd, 0x17, ++ 0x27, 0x3e, 0x66, 0x2c, ++ 0x97, 0xee, 0x72, 0x99, ++ 0x5e, 0xf4, 0x26, 0x40, ++ 0xc5, 0x50, 0xb9, 0x01, ++ 0x3f, 0xad, 0x07, 0x61, ++ 0x35, 0x3c, 0x70, 0x86, ++ 0xa2, 0x72, 0xc2, 0x40, ++ 0x88, 0xbe, 0x94, 0x76, ++ 0x9f, 0xd1, 0x66, 0x50}, ++ .size = 66U, ++ }, ++ }, ++ }, ++}; ++ ++static struct stm32_pka_platdata pka_pdata; ++static struct mutex pka_lock = MUTEX_INITIALIZER; ++ ++static TEE_Result pka_wait_bit(const vaddr_t base, const uint32_t bit) ++{ ++ uint64_t timeout = timeout_init_us(PKA_TIMEOUT_US); ++ ++ while ((io_read32(base + _PKA_SR) & bit) != bit) ++ if (timeout_elapsed(timeout)) ++ break; ++ ++ if ((io_read32(base + _PKA_SR) & bit) != bit) { ++ DMSG("timeout waiting 0x%x", bit); ++ return TEE_ERROR_BUSY; ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++static void pka_disable(const vaddr_t base) ++{ ++ io_clrbits32(base + _PKA_CR, _PKA_CR_EN); ++} ++ ++static TEE_Result pka_enable(const vaddr_t base, const uint32_t mode) ++{ ++ /* Set mode and disable interrupts */ ++ io_clrsetbits32(base + _PKA_CR, _PKA_IT_MASK | _PKA_CR_MODE_MASK, ++ _PKA_CR_MODE_MASK & (mode << _PKA_CR_MODE_SHIFT)); ++ ++ io_setbits32(base + _PKA_CR, _PKA_CR_EN); ++ ++ return pka_wait_bit(base, _PKA_SR_INITOK); ++} ++ ++/* ++ * Data are already loaded in PKA internal RAM, ++ * MODE is set, ++ * we start process, and wait for its end. ++ */ ++static TEE_Result stm32_pka_process(const vaddr_t base) ++{ ++ io_setbits32(base + _PKA_CR, _PKA_CR_START); ++ ++ return pka_wait_bit(base, _PKA_IT_PROCEND); ++} ++ ++/** ++ * @brief Read ECC operand from PKA RAM. ++ * @note PKA expects to read u64 word, each u64 are: the least significant bit ++ * is bit 0; the most significant bit is bit 63. ++ * We read eo_nbw (ECC operand Size) u64, value that depends on the ++ * chosen prime modulus length in bits. ++ * First less significant u64 is read from low address. ++ * Most significant u64 from higher address. ++ * And at last address we expect to read a u64(0x0). ++ * @note This function doesn't only manage endianness (as bswap64 do), but also ++ * complete most significant incomplete u64 with 0 (if data is not a u64 ++ * multiple), and check last u64 is 0. ++ * @param addr: PKA_RAM address to read from to the buffer 'data'. ++ * @param data[out]: will be a BYTE list with most significant bytes first. ++ * @param data_size[in/out]: [in]data size, ++ * [out]]nb of bytes in data. ++ * @param eo_nbw: is ECC Operand size in 64bits word (including the extra 0) ++ * (note it depends on the prime modulus length, not the data ++ * size). ++ * @retval TEE_SUCCESS if OK. ++ * TEE_ERROR_BAD_PARAMETERS if data_size and eo_nbw are inconsistent, ++ * i.e. data doesn't fit in defined eo_nbw, or eo_nbw bigger than ++ * hardware limit, or if [in]data_size is too small to get the data. ++ */ ++static TEE_Result read_eo_data(const vaddr_t addr, uint8_t *data, ++ const unsigned int data_size, ++ const unsigned int eo_nbw) ++{ ++ uint32_t word_index = 0; ++ int data_index = (int)data_size - 1; ++ uint64_t tmp = 0ULL; ++ ++ if (eo_nbw < OP_NBW_FROM_SIZE(data_size) || eo_nbw > MAX_EO_NBW) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ /* Fill value */ ++ for (word_index = 0U; word_index < eo_nbw - 1; word_index++) { ++ unsigned int i = 0U; /* Index in the tmp U64 word */ ++ ++ tmp = io_read64(addr + word_index * sizeof(tmp)); ++ ++ while ((i < sizeof(tmp)) && (data_index >= 0)) { ++ data[data_index] = tmp & 0xFF; ++ tmp = tmp >> INT8_LEN; ++ i++; /* Move byte index in current (u64)tmp */ ++ data_index--; /* Move to next most significant byte */ ++ } ++ } ++ ++ /* The last u64 should be 0 */ ++ tmp = io_read64(addr + word_index * sizeof(tmp)); ++ if (tmp) ++ return TEE_ERROR_SECURITY; ++ ++ return TEE_SUCCESS; ++} ++ ++/** ++ * @brief Write ECC operand to PKA RAM. ++ * @note PKA expects to write u64 word, each u64 are: the least significant bit ++ * is bit 0; the most significant bit is bit 63. ++ * We write eo_nbw (ECC operand Size) u64, value that depends on the ++ * chosen prime modulus length in bits. ++ * First less significant u64 is written to low address. ++ * Most significant u64 to higher address. ++ * And at last address we write a u64(0x0). ++ * @note This function doesn't only manage endianness (as bswap64 do), but also ++ * complete most significant incomplete u64 with 0 (if data is not a u64 ++ * multiple), and fill u64 last address with 0. ++ * @param addr: PKA_RAM address to write the buffer 'data'. ++ * @param data: is a BYTE list with most significant bytes first. ++ * @param data_size: nb of bytes in data. ++ * @param eo_nbw: is ECC Operand size in 64bits word (including the extra 0) ++ * (note it depends on the prime modulus length, not the data ++ * size). ++ * @retval TEE_SUCCESS if OK. ++ * TEE_ERROR_BAD_PARAMETERS if data_size and eo_nbw are inconsistent, ++ * ie data doesn't fit in defined eo_nbw, or eo_nbw bigger than hardware ++ * limit. ++ */ ++static TEE_Result write_eo_data(const vaddr_t addr, const uint8_t *data, ++ const unsigned int data_size, ++ const unsigned int eo_nbw) ++{ ++ uint32_t word_index = 0; ++ int data_index = (int)data_size - 1; ++ ++ if (eo_nbw < OP_NBW_FROM_SIZE(data_size) || eo_nbw > MAX_EO_NBW) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ /* Fill value */ ++ for (word_index = 0U; word_index < eo_nbw; word_index++) { ++ uint64_t tmp = 0ULL; ++ unsigned int i = 0U; /* Index in the tmp U64 word */ ++ ++ /* Stop if end of tmp or end of data */ ++ while ((i < sizeof(tmp)) && (data_index >= 0)) { ++ tmp |= (uint64_t)(data[data_index]) << (INT8_LEN * i); ++ i++; /* Move byte index in current (u64)tmp */ ++ data_index--; /* Move to next most significant byte */ ++ } ++ ++ io_write64(addr + word_index * sizeof(tmp), tmp); ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++static unsigned int get_ecc_op_nbword(const enum stm32_pka_curve_id cid) ++{ ++ if (cid < 0 || cid >= PKA_LAST_CID) ++ return 0; ++ ++ return OP_NBW_FROM_LEN(curve_def[cid].n_len); ++} ++ ++static TEE_Result stm32_pka_configure_curve(const vaddr_t base, const int op, ++ const enum stm32_pka_curve_id cid) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ unsigned int eo_nbw = get_ecc_op_nbword(cid); ++ ++ io_write64(base + pka_ram[op][N_LEN], curve_def[cid].n_len); ++ if (pka_ram[op][P_LEN]) ++ io_write64(base + pka_ram[op][P_LEN], curve_def[cid].p_len); ++ io_write64(base + pka_ram[op][A_SIGN], curve_def[cid].a_sign); ++ ++ res = write_eo_data(base + pka_ram[op][COEFF_A], curve_def[cid].a.val, ++ curve_def[cid].a.size, eo_nbw); ++ if (res) ++ return res; ++ ++ if (pka_ram[op][COEFF_B]) { ++ res = write_eo_data(base + pka_ram[op][COEFF_B], ++ curve_def[cid].b.val, curve_def[cid].b.size, ++ eo_nbw); ++ if (res) ++ return res; ++ } ++ ++ if (pka_ram[op][PRIME_N]) { ++ res = write_eo_data(base + pka_ram[op][PRIME_N], ++ curve_def[cid].n.val, curve_def[cid].n.size, ++ eo_nbw); ++ if (res) ++ return res; ++ } ++ ++ res = write_eo_data(base + pka_ram[op][VAL_P], curve_def[cid].p.val, ++ curve_def[cid].p.size, eo_nbw); ++ if (res) ++ return res; ++ ++ if (pka_ram[op][GPOINT_X]) { ++ res = write_eo_data(base + pka_ram[op][GPOINT_X], ++ curve_def[cid].g.x.val, ++ curve_def[cid].g.x.size, eo_nbw); ++ if (res) ++ return res; ++ } ++ ++ if (pka_ram[op][GPOINT_Y]) { ++ res = write_eo_data(base + pka_ram[op][GPOINT_Y], ++ curve_def[cid].g.y.val, ++ curve_def[cid].g.y.size, eo_nbw); ++ if (res) ++ return res; ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++/** ++ * @brief Check if BigInt stored in d is 0 ++ * ++ * @retval: true: if data represents a 0 value (i.e. all bytes == 0) ++ * false: if data represents a non-zero value. ++ */ ++static bool is_zero(const struct stm32_pka_bn *d) ++{ ++ unsigned int i = 0U; ++ ++ if (!d) ++ return true; ++ ++ for (i = 0U; i < d->size; i++) ++ if (d->val[i] != 0U) ++ return false; ++ ++ return true; ++} ++ ++/** ++ * @brief Compare two BigInt: ++ * ++ * @retval: true if a < b ++ * false if a >= b ++ */ ++static bool is_smaller(const struct stm32_pka_bn *a, ++ const struct stm32_pka_bn *b) ++{ ++ unsigned int i = MAX(a->size, b->size) + 1; ++ ++ do { ++ uint8_t _a = 0; ++ uint8_t _b = 0; ++ ++ i--; ++ if (a->size < i) ++ _a = 0U; ++ else ++ _a = a->val[a->size - i]; ++ ++ if (b->size < i) ++ _b = 0U; ++ else ++ _b = b->val[b->size - i]; ++ ++ if (_a < _b) ++ return true; ++ ++ if (_a > _b) ++ return false; ++ ++ } while (i != 0U); ++ ++ return false; ++} ++ ++TEE_Result stm32_pka_get_max_size(size_t *bytes, size_t *bits, ++ const enum stm32_pka_curve_id cid) ++{ ++ if (cid < 0 || cid >= PKA_LAST_CID) ++ return TEE_ERROR_NOT_SUPPORTED; ++ ++ if (bits) ++ *bits = curve_def[cid].n_len; ++ ++ if (bytes) ++ *bytes = curve_def[cid].n.size; ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result stm32_pka_compute_r2modn_ret(const vaddr_t base, ++ struct stm32_pka_bn *v, ++ const unsigned int eo_nbw) ++{ ++ uint32_t sr = 0; ++ ++ sr = io_read32(base + _PKA_SR); ++ if ((sr & (_PKA_IT_OPERR | _PKA_IT_ADDRERR | _PKA_IT_RAMERR)) != 0) { ++ IMSG("Detected error(s): %s%s%s", ++ (sr & _PKA_IT_OPERR) ? "Operation " : "", ++ (sr & _PKA_IT_ADDRERR) ? "Address " : "", ++ (sr & _PKA_IT_RAMERR) ? "RAM" : ""); ++ return TEE_ERROR_SECURITY; ++ } ++ ++ return read_eo_data(base + _PKA_RAM_R2MODN_OUT, v->val, v->size, ++ eo_nbw); ++} ++ ++TEE_Result stm32_pka_compute_montgomery(const struct stm32_pka_bn *n, ++ const size_t n_len, ++ struct stm32_pka_bn *r2modn) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ vaddr_t base = io_pa_or_va(&pka_pdata.pa_or_va, 1); ++ unsigned int eo_nbw = OP_NBW_FROM_LEN(n_len); ++ ++ if (!n_len || !n || !r2modn) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ mutex_lock(pka_pdata.lock); ++ ++ if ((io_read32(base + _PKA_SR) & _PKA_SR_BUSY) == _PKA_SR_BUSY) { ++ FMSG("busy"); ++ res = TEE_ERROR_BUSY; ++ goto out; ++ } ++ ++ /* Fill PKA RAM */ ++ /* with n_len */ ++ io_write64(base + _PKA_RAM_R2MODN_N_LEN, n_len); ++ ++ /* with n */ ++ res = write_eo_data(base + _PKA_RAM_R2MODN_PRIME_N, n->val, n->size, ++ eo_nbw); ++ if (res) ++ goto out; ++ ++ /* Set mode to Montgomery parameter computation */ ++ res = pka_enable(base, _PKA_CR_MODE_R2MODN); ++ if (res) { ++ IMSG("Set mode pka error %d", res); ++ goto out; ++ } ++ ++ /* Start processing and wait end */ ++ res = stm32_pka_process(base); ++ if (res) { ++ IMSG("process error %d", res); ++ goto out; ++ } ++ ++ /* Get return value */ ++ res = stm32_pka_compute_r2modn_ret(base, r2modn, eo_nbw); ++ ++ /* Unset end proc */ ++ io_setbits32(base + _PKA_CLRFR, _PKA_IT_PROCEND); ++ ++out: ++ /* Disable PKA (will stop all pending process and reset RAM) */ ++ pka_disable(base); ++ ++ mutex_unlock(pka_pdata.lock); ++ ++ return res; ++} ++ ++TEE_Result stm32_pka_ecc_compute_montgomery(struct stm32_pka_bn *r2modn, ++ const enum stm32_pka_curve_id cid) ++{ ++ /* ++ * TODO: I don't know if I should use p or n: both values seem to work. ++ */ ++ return stm32_pka_compute_montgomery(&curve_def[cid].n, ++ curve_def[cid].n_len, r2modn); ++} ++ ++static TEE_Result stm32_pka_is_point_on_param(const struct stm32_pka_point *p, ++ enum stm32_pka_curve_id cid) ++{ ++ /* Check Xp < p */ ++ if (!is_smaller(&p->x, &curve_def[cid].p)) { ++ IMSG("Xp < p inval"); ++ return TEE_ERROR_BAD_PARAMETERS; ++ } ++ ++ /* Check Yp < p */ ++ if (!is_smaller(&p->y, &curve_def[cid].p)) { ++ IMSG("Yp < p inval"); ++ return TEE_ERROR_BAD_PARAMETERS; ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result stm32_pka_is_point_on_curve_ret(const vaddr_t base) ++{ ++ uint64_t value = 0; ++ uint32_t sr = 0; ++ ++ sr = io_read32(base + _PKA_SR); ++ if ((sr & (_PKA_IT_OPERR | _PKA_IT_ADDRERR | _PKA_IT_RAMERR)) != 0) { ++ IMSG("Detected error(s): %s%s%s", ++ (sr & _PKA_IT_OPERR) ? "Operation " : "", ++ (sr & _PKA_IT_ADDRERR) ? "Address " : "", ++ (sr & _PKA_IT_RAMERR) ? "RAM" : ""); ++ return TEE_ERROR_SECURITY; ++ } ++ ++ value = io_read64(base + _PKA_RAM_ONCURVE_RES); ++ if (value == _PKA_RAM_ONCURVE_RES_YES) ++ return TEE_SUCCESS; ++ else ++ return TEE_ERROR_GENERIC; ++} ++ ++TEE_Result stm32_pka_is_point_on_curve(const struct stm32_pka_point *p, ++ const struct stm32_pka_bn *r2modn, ++ const enum stm32_pka_curve_id cid) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ vaddr_t base = io_pa_or_va(&pka_pdata.pa_or_va, 1); ++ unsigned int eo_nbw = get_ecc_op_nbword(cid); ++ ++ if (!eo_nbw || !p) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ mutex_lock(pka_pdata.lock); ++ ++ res = stm32_pka_is_point_on_param(p, cid); ++ if (res) { ++ FMSG("check param error %d", res); ++ goto out; ++ } ++ ++ if ((io_read32(base + _PKA_SR) & _PKA_SR_BUSY) == _PKA_SR_BUSY) { ++ FMSG("busy"); ++ res = TEE_ERROR_BUSY; ++ goto out; ++ } ++ ++ /* Fill PKA RAM */ ++ /* With curve id values */ ++ res = stm32_pka_configure_curve(base, on_curve, cid); ++ if (res) ++ goto out; ++ ++ /* With Montgomery parameter R*R mod n */ ++ res = write_eo_data(base + _PKA_RAM_ONCURVE_R2MODN, r2modn->val, ++ r2modn->size, eo_nbw); ++ if (res) ++ goto out; ++ ++ /* With P */ ++ res = write_eo_data(base + _PKA_RAM_ONCURVE_XP, p->x.val, p->x.size, ++ eo_nbw); ++ if (res) ++ goto out; ++ ++ res = write_eo_data(base + _PKA_RAM_ONCURVE_YP, p->y.val, p->y.size, ++ eo_nbw); ++ if (res) ++ goto out; ++ ++ /* Set mode to point on the curve check */ ++ res = pka_enable(base, _PKA_CR_MODE_POINT_CHECK); ++ if (res) { ++ IMSG("Set mode pka error %d", res); ++ goto out; ++ } ++ ++ /* Start processing and wait end */ ++ res = stm32_pka_process(base); ++ if (res) { ++ IMSG("process error %d", res); ++ goto out; ++ } ++ ++ /* Get return value */ ++ res = stm32_pka_is_point_on_curve_ret(base); ++ ++ /* Unset end proc */ ++ io_setbits32(base + _PKA_CLRFR, _PKA_IT_PROCEND); ++ ++out: ++ /* Disable PKA (will stop all pending process and reset RAM) */ ++ pka_disable(base); ++ ++ mutex_unlock(pka_pdata.lock); ++ ++ return res; ++} ++ ++static TEE_Result stm32_pka_ecdsa_verif_param(const struct stm32_pka_bn *sig_r, ++ const struct stm32_pka_bn *sig_s, ++ const struct stm32_pka_point *pk, ++ const enum stm32_pka_curve_id cid) ++{ ++ /* Public Key check */ ++ /* Check Xq < p */ ++ if (!is_smaller(&pk->x, &curve_def[cid].p)) { ++ IMSG("Xq < p inval"); ++ return TEE_ERROR_BAD_PARAMETERS; ++ } ++ ++ /* Check Yq < p */ ++ if (!is_smaller(&pk->y, &curve_def[cid].p)) { ++ IMSG("Yq < p inval"); ++ return TEE_ERROR_BAD_PARAMETERS; ++ } ++ ++ /* Signature check */ ++ /* Check 0 < r < n */ ++ if (!is_smaller(sig_r, &curve_def[cid].n) && is_zero(sig_r)) { ++ IMSG("0 < r < n inval"); ++ return TEE_ERROR_BAD_PARAMETERS; ++ } ++ ++ /* Check 0 < s < n */ ++ if (!is_smaller(sig_s, &curve_def[cid].n) && is_zero(sig_s)) { ++ IMSG("0 < s < n inval"); ++ return TEE_ERROR_BAD_PARAMETERS; ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result stm32_pka_ecdsa_verif_ret(const vaddr_t base) ++{ ++ uint64_t value = 0; ++ uint32_t sr = 0; ++ ++ sr = io_read32(base + _PKA_SR); ++ if ((sr & (_PKA_IT_OPERR | _PKA_IT_ADDRERR | _PKA_IT_RAMERR)) != 0) { ++ IMSG("Detected error(s): %s%s%s", ++ (sr & _PKA_IT_OPERR) ? "Operation " : "", ++ (sr & _PKA_IT_ADDRERR) ? "Address " : "", ++ (sr & _PKA_IT_RAMERR) ? "RAM" : ""); ++ return TEE_ERROR_SECURITY; ++ } ++ ++ value = io_read64(base + _PKA_RAM_VERIF_RES); ++ if (value == _PKA_RAM_VERIF_RES_VALID) ++ return TEE_SUCCESS; ++ ++ if (value == _PKA_RAM_VERIF_RES_INVALID) ++ return TEE_ERROR_SIGNATURE_INVALID; ++ ++ return TEE_ERROR_BAD_PARAMETERS; ++} ++ ++TEE_Result stm32_pka_ecdsa_verif(const void *hash, const unsigned int hash_size, ++ const struct stm32_pka_bn *sig_r, ++ const struct stm32_pka_bn *sig_s, ++ const struct stm32_pka_point *pk, ++ const enum stm32_pka_curve_id cid) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ vaddr_t base = io_pa_or_va(&pka_pdata.pa_or_va, 1); ++ unsigned int eo_nbw = get_ecc_op_nbword(cid); ++ ++ if (!eo_nbw || !hash || !sig_r || !sig_s || !pk) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ mutex_lock(pka_pdata.lock); ++ ++ res = stm32_pka_ecdsa_verif_param(sig_r, sig_s, pk, cid); ++ if (res) { ++ FMSG("check param error %d", res); ++ goto out; ++ } ++ ++ if ((io_read32(base + _PKA_SR) & _PKA_SR_BUSY) == _PKA_SR_BUSY) { ++ FMSG("busy"); ++ res = TEE_ERROR_BUSY; ++ goto out; ++ } ++ ++ /* Fill PKA RAM */ ++ /* With curve id values */ ++ res = stm32_pka_configure_curve(base, verif, cid); ++ if (res) ++ goto out; ++ ++ /* With pubkey */ ++ res = write_eo_data(base + _PKA_RAM_VERIF_XQ, pk->x.val, pk->x.size, ++ eo_nbw); ++ if (res) ++ goto out; ++ ++ res = write_eo_data(base + _PKA_RAM_VERIF_YQ, pk->y.val, pk->y.size, ++ eo_nbw); ++ if (res) ++ goto out; ++ ++ /* With hash */ ++ res = write_eo_data(base + _PKA_RAM_VERIF_HASH_Z, hash, hash_size, ++ eo_nbw); ++ if (res) ++ goto out; ++ ++ /* With signature */ ++ res = write_eo_data(base + _PKA_RAM_VERIF_SIGN_R, sig_r->val, ++ sig_r->size, eo_nbw); ++ if (res) ++ goto out; ++ ++ res = write_eo_data(base + _PKA_RAM_VERIF_SIGN_S, sig_s->val, ++ sig_s->size, eo_nbw); ++ if (res) ++ goto out; ++ ++ /* Set mode to ECDSA signature verification */ ++ res = pka_enable(base, _PKA_CR_MODE_ECDSA_VERIF); ++ if (res) { ++ IMSG("set mode pka error %d", res); ++ goto out; ++ } ++ ++ /* Start processing and wait end */ ++ res = stm32_pka_process(base); ++ if (res) { ++ IMSG("process error %d", res); ++ goto out; ++ } ++ ++ /* Check return status */ ++ res = stm32_pka_ecdsa_verif_ret(base); ++ ++ /* Unset end proc */ ++ io_setbits32(base + _PKA_CLRFR, _PKA_IT_PROCEND); ++ ++out: ++ /* Disable PKA (will stop all pending process and reset RAM) */ ++ pka_disable(base); ++ ++ mutex_unlock(pka_pdata.lock); ++ ++ return res; ++} ++ ++static TEE_Result stm32_pka_ecdsa_sign_param(const struct stm32_pka_bn *k) ++{ ++ if (is_zero(k) || k->size > PKA_MAX_ECC_SIZE) { ++ IMSG("0 < k < 2**640 inval"); ++ return TEE_ERROR_BAD_PARAMETERS; ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result stm32_pka_ecdsa_sign_ret(const vaddr_t base, ++ struct stm32_pka_bn *sig_r, ++ struct stm32_pka_bn *sig_s, ++ const unsigned int eo_nbw) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ uint64_t value = 0; ++ uint32_t sr = 0; ++ ++ sr = io_read32(base + _PKA_SR); ++ if ((sr & (_PKA_IT_OPERR | _PKA_IT_ADDRERR | _PKA_IT_RAMERR)) != 0) { ++ IMSG("Detected error(s): %s%s%s", ++ (sr & _PKA_IT_OPERR) ? "Operation " : "", ++ (sr & _PKA_IT_ADDRERR) ? "Address " : "", ++ (sr & _PKA_IT_RAMERR) ? "RAM" : ""); ++ return TEE_ERROR_SECURITY; ++ } ++ ++ value = io_read64(base + _PKA_RAM_SIGN_RES); ++ ++ if (value == _PKA_RAM_SIGN_RES_FAIL) ++ return TEE_ERROR_SECURITY; ++ ++ if (value == _PKA_RAM_SIGN_RES_R0) { ++ value = _PKA_RAM_SIGN_RES_SUCCESS; ++ memset(sig_r->val, 0, sig_r->size); ++ } else { ++ res = read_eo_data(base + _PKA_RAM_SIGN_R, sig_r->val, ++ sig_r->size, eo_nbw); ++ if (res) ++ return res; ++ } ++ ++ if (value == _PKA_RAM_SIGN_RES_S0) { ++ value = _PKA_RAM_SIGN_RES_SUCCESS; ++ memset(sig_s->val, 0, sig_s->size); ++ } else { ++ res = read_eo_data(base + _PKA_RAM_SIGN_S, sig_s->val, ++ sig_s->size, eo_nbw); ++ if (res) ++ return res; ++ } ++ ++ if (value != _PKA_RAM_SIGN_RES_SUCCESS) ++ return TEE_ERROR_GENERIC; ++ ++ return TEE_SUCCESS; ++} ++ ++TEE_Result stm32_pka_ecdsa_sign(const void *hash, const unsigned int hash_size, ++ struct stm32_pka_bn *sig_r, ++ struct stm32_pka_bn *sig_s, ++ const struct stm32_pka_bn *d, ++ const struct stm32_pka_bn *k, ++ const enum stm32_pka_curve_id cid) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ vaddr_t base = io_pa_or_va(&pka_pdata.pa_or_va, 1); ++ unsigned int eo_nbw = get_ecc_op_nbword(cid); ++ ++ if (!eo_nbw || !hash || !sig_r || !sig_s || !d || !k) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ mutex_lock(pka_pdata.lock); ++ ++ res = stm32_pka_ecdsa_sign_param(k); ++ if (res) { ++ FMSG("check param error %d", res); ++ goto out; ++ } ++ ++ if ((io_read32(base + _PKA_SR) & _PKA_SR_BUSY) == _PKA_SR_BUSY) { ++ FMSG("busy"); ++ res = TEE_ERROR_BUSY; ++ goto out; ++ } ++ ++ /* Fill PKA RAM */ ++ /* With curve id values */ ++ res = stm32_pka_configure_curve(base, sign, cid); ++ if (res) ++ goto out; ++ ++ /* With K (random number) */ ++ res = write_eo_data(base + _PKA_RAM_SIGN_K, k->val, k->size, eo_nbw); ++ if (res) ++ goto out; ++ ++ /* With private key d */ ++ res = write_eo_data(base + _PKA_RAM_SIGN_D, d->val, d->size, eo_nbw); ++ if (res) ++ goto out; ++ ++ /* With hash */ ++ res = write_eo_data(base + _PKA_RAM_SIGN_HASH_Z, hash, hash_size, ++ eo_nbw); ++ if (res) ++ goto out; ++ ++ /* Set mode to ECDSA signature */ ++ res = pka_enable(base, _PKA_CR_MODE_ECDSA_SIGN); ++ if (res) { ++ IMSG("Set mode pka error %d", res); ++ goto out; ++ } ++ ++ /* Start processing and wait end */ ++ res = stm32_pka_process(base); ++ if (res) { ++ IMSG("process error %d", res); ++ goto out; ++ } ++ ++ /* Get return value */ ++ res = stm32_pka_ecdsa_sign_ret(base, sig_r, sig_s, eo_nbw); ++ ++ /* Unset end proc */ ++ io_setbits32(base + _PKA_CLRFR, _PKA_IT_PROCEND); ++ ++out: ++ /* Disable PKA (will stop all pending process and reset RAM) */ ++ pka_disable(base); ++ ++ mutex_unlock(pka_pdata.lock); ++ ++ return res; ++} ++ ++static TEE_Result stm32_pka_ecc_sc_mul_param(const struct stm32_pka_bn *k, ++ const struct stm32_pka_point *p, ++ const enum stm32_pka_curve_id cid) ++{ ++ if (is_zero(k) || k->size > PKA_MAX_ECC_SIZE) { ++ IMSG("0 < k < 2**640 inval"); ++ return TEE_ERROR_BAD_PARAMETERS; ++ } ++ ++ if (!is_smaller(&p->x, &curve_def[cid].p)) { ++ IMSG("Xp < p inval"); ++ return TEE_ERROR_BAD_PARAMETERS; ++ } ++ ++ if (!is_smaller(&p->y, &curve_def[cid].p)) { ++ IMSG("Yp < p inval"); ++ return TEE_ERROR_BAD_PARAMETERS; ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result stm32_pka_ecc_kp_ret(const vaddr_t base, ++ struct stm32_pka_point *kp, ++ const unsigned int eo_nbw) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ uint64_t value = 0LLU; ++ uint32_t sr = 0U; ++ ++ sr = io_read32(base + _PKA_SR); ++ if ((sr & (_PKA_IT_OPERR | _PKA_IT_ADDRERR | _PKA_IT_RAMERR)) != 0) { ++ IMSG("Detected error(s): %s%s%s", ++ (sr & _PKA_IT_OPERR) ? "Operation " : "", ++ (sr & _PKA_IT_ADDRERR) ? "Address " : "", ++ (sr & _PKA_IT_RAMERR) ? "RAM" : ""); ++ return TEE_ERROR_SECURITY; ++ } ++ ++ value = io_read64(base + _PKA_RAM_KP_RES); ++ if (value == _PKA_RAM_KP_RES_FAIL) ++ return TEE_ERROR_SECURITY; ++ ++ if (value != _PKA_RAM_KP_RES_SUCCESS) ++ return TEE_ERROR_GENERIC; ++ ++ res = read_eo_data(base + _PKA_RAM_KP_X, kp->x.val, kp->x.size, eo_nbw); ++ if (res) ++ return res; ++ ++ res = read_eo_data(base + _PKA_RAM_KP_Y, kp->y.val, kp->y.size, eo_nbw); ++ ++ return res; ++} ++ ++TEE_Result stm32_pka_ecc_scalar_mul(const struct stm32_pka_bn *k, ++ const struct stm32_pka_point *p, ++ struct stm32_pka_point *kp, ++ const enum stm32_pka_curve_id cid) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ vaddr_t base = io_pa_or_va(&pka_pdata.pa_or_va, 1); ++ unsigned int eo_nbw = get_ecc_op_nbword(cid); ++ ++ if (!eo_nbw || !k || !p || !kp) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ mutex_lock(pka_pdata.lock); ++ ++ res = stm32_pka_ecc_sc_mul_param(k, p, cid); ++ if (res) { ++ FMSG("check param error %d", res); ++ goto out; ++ } ++ ++ if ((io_read32(base + _PKA_SR) & _PKA_SR_BUSY) == _PKA_SR_BUSY) { ++ FMSG("busy"); ++ res = TEE_ERROR_BUSY; ++ goto out; ++ } ++ ++ /* Fill PKA RAM */ ++ /* With curve id values */ ++ res = stm32_pka_configure_curve(base, scalar_mul, cid); ++ if (res) ++ goto out; ++ ++ /* With k */ ++ res = write_eo_data(base + _PKA_RAM_KP_K, k->val, k->size, eo_nbw); ++ if (res) ++ goto out; ++ ++ /* With xP */ ++ res = write_eo_data(base + _PKA_RAM_KP_XP, p->x.val, p->x.size, eo_nbw); ++ if (res) ++ goto out; ++ ++ /* With yP */ ++ res = write_eo_data(base + _PKA_RAM_KP_YP, p->y.val, p->y.size, eo_nbw); ++ if (res) ++ goto out; ++ ++ /* Set mode to ecc scalar multiplication */ ++ res = pka_enable(base, _PKA_CR_MODE_ECC_KP); ++ if (res) { ++ IMSG("Set mode pka error %d", res); ++ goto out; ++ } ++ ++ /* Start processing and wait end */ ++ res = stm32_pka_process(base); ++ if (res) { ++ IMSG("process error %d", res); ++ goto out; ++ } ++ ++ /* Get return value */ ++ res = stm32_pka_ecc_kp_ret(base, kp, eo_nbw); ++ ++ /* Unset end proc */ ++ io_setbits32(base + _PKA_CLRFR, _PKA_IT_PROCEND); ++ ++out: ++ /* Disable PKA (will stop all pending process and reset RAM) */ ++ pka_disable(base); ++ ++ mutex_unlock(pka_pdata.lock); ++ ++ return res; ++} ++ ++TEE_Result stm32_pka_edac_gen_pubkey(const struct stm32_pka_bn *k, ++ struct stm32_pka_point *pk, ++ const enum stm32_pka_curve_id cid) ++{ ++ return stm32_pka_ecc_scalar_mul(k, &curve_def[cid].g, pk, cid); ++} ++ ++#ifdef CFG_EMBED_DTB ++static TEE_Result stm32_pka_parse_fdt(struct stm32_pka_platdata *pdata, ++ const void *fdt, int node) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ struct dt_node_info info = { }; ++ ++ _fdt_fill_device_info(fdt, &info, node); ++ ++ if (info.reset == DT_INFO_INVALID_RESET || ++ info.reg_size == DT_INFO_INVALID_REG_SIZE || ++ info.reg == DT_INFO_INVALID_REG) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ pdata->pa_or_va.pa = info.reg; ++ io_pa_or_va_secure(&pdata->pa_or_va, info.reg_size); ++ if (!pdata->pa_or_va.va) ++ panic(); ++ ++ pdata->reset_id = (unsigned int)info.reset; ++ ++ res = clk_dt_get_by_index(fdt, node, 0, &pdata->clk); ++ if (res) ++ return res; ++ ++ return TEE_SUCCESS; ++} ++ ++__weak ++int stm32_pka_get_platdata(struct stm32_pka_platdata *pdata __unused) ++{ ++ /* In DT config, the platform data are filled by DT file */ ++ return 0; ++} ++#else /* CFG_EMBED_DTB */ ++static TEE_Result stm32_pka_parse_fdt(struct stm32_pka_platdata *pdata, ++ const void *fdt, int node) ++{ ++ /* Do nothing, there is no fdt to parse in this case */ ++ return TEE_SUCCESS; ++} ++ ++/* This function can be overridden by platform to define pdata of PKA driver */ ++__weak ++int stm32_pka_get_platdata(struct stm32_pka_platdata *pdata __unused) ++{ ++ return -1; ++} ++#endif ++ ++/* ++ * @brief Initialize the PKA driver. ++ * @param None. ++ * @retval 0 if OK, negative value else. ++ */ ++static TEE_Result stm32_pka_probe(const void *fdt, int node, ++ const void *compat_data __unused) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ vaddr_t __unused base = 0; ++ uint32_t __unused ver = 0U; ++ uint32_t __unused id = 0U; ++ ++ if (stm32_pka_get_platdata(&pka_pdata)) ++ return TEE_ERROR_NOT_SUPPORTED; ++ ++ res = stm32_pka_parse_fdt(&pka_pdata, fdt, node); ++ if (res) ++ return res; ++ ++ clk_enable(pka_pdata.clk); ++ ++ if (stm32_reset_assert(pka_pdata.reset_id, TIMEOUT_US_1MS) != 0) ++ panic(); ++ ++ udelay(PKA_RESET_DELAY); ++ if (stm32_reset_deassert(pka_pdata.reset_id, TIMEOUT_US_1MS) != 0) ++ panic(); ++ ++#if TRACE_LEVEL >= TRACE_FLOW ++ base = io_pa_or_va(&pka_pdata.pa_or_va, 1); ++ id = io_read32(base + _PKA_IPIDR); ++ ver = io_read32(base + _PKA_VERR); ++ ++ FMSG("STM32 PKA[%x] V%u.%u", id, ++ (ver & _PKA_VERR_MAJREV_MASK) >> _PKA_VERR_MAJREV_SHIFT, ++ (ver & _PKA_VERR_MINREV_MASK) >> _PKA_VERR_MINREV_SHIFT); ++#endif ++ ++ pka_pdata.lock = &pka_lock; ++ ++ if (IS_ENABLED(CFG_CRYPTO_DRV_ECC)) { ++ res = stm32_register_ecc(); ++ if (res) { ++ EMSG("Failed to register to ecc: %#"PRIx32, res); ++ panic(); ++ } ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++#ifdef CFG_EMBED_DTB ++static const struct dt_device_match pka_match_table[] = { ++ { .compatible = "st,stm32mp13-pka64" }, ++ { } ++}; ++ ++DEFINE_DT_DRIVER(stm32_pka_dt_driver) = { ++ .name = "stm32-pka64", ++ .match_table = pka_match_table, ++ .probe = &stm32_pka_probe, ++}; ++#endif +diff --git a/core/drivers/crypto/stm32/stm32_pka.h b/core/drivers/crypto/stm32/stm32_pka.h +new file mode 100644 +index 000000000..352f9757d +--- /dev/null ++++ b/core/drivers/crypto/stm32/stm32_pka.h +@@ -0,0 +1,80 @@ ++/* SPDX-License-Identifier: BSD-3-Clause */ ++/* ++ * Copyright (c) 2021, STMicroelectronics - All Rights Reserved ++ */ ++ ++#ifndef STM32_PKA_H ++#define STM32_PKA_H ++ ++#include ++#include ++#include ++#include ++ ++#define PKA_MAX_ECC_LEN 640 ++#define PKA_MAX_ECC_SIZE (PKA_MAX_ECC_LEN / 8) ++ ++enum stm32_pka_curve_id { ++ PKA_NIST_P192, ++ PKA_NIST_P224, ++ PKA_NIST_P256, ++ PKA_NIST_P384, ++ PKA_NIST_P521, ++ ++ PKA_LAST_CID, ++}; ++ ++/* ++ * struct stm32_pka_bn: internal representation of bin number ++ * ++ * @field val: a BYTE array with most significant bytes first ++ * @field size: nb of Byte of 'val' ++ */ ++struct stm32_pka_bn { ++ uint8_t *val; ++ size_t size; ++}; ++ ++struct stm32_pka_point { ++ struct stm32_pka_bn x; ++ struct stm32_pka_bn y; ++}; ++ ++struct stm32_pka_platdata { ++ struct io_pa_va pa_or_va; ++ struct clk *clk; ++ unsigned int reset_id; ++ struct mutex *lock; /* Protect PKA HW instance access */ ++}; ++ ++int stm32_pka_get_platdata(struct stm32_pka_platdata *pdata); ++ ++TEE_Result stm32_pka_get_max_size(size_t *bytes, size_t *bits, ++ const enum stm32_pka_curve_id cid); ++TEE_Result stm32_pka_compute_montgomery(const struct stm32_pka_bn *n, ++ const size_t n_len, ++ struct stm32_pka_bn *r2modn); ++TEE_Result stm32_pka_ecc_compute_montgomery(struct stm32_pka_bn *r2modn, ++ const enum stm32_pka_curve_id cid); ++TEE_Result stm32_pka_is_point_on_curve(const struct stm32_pka_point *p, ++ const struct stm32_pka_bn *r2modn, ++ const enum stm32_pka_curve_id cid); ++TEE_Result stm32_pka_ecc_scalar_mul(const struct stm32_pka_bn *k, ++ const struct stm32_pka_point *p, ++ struct stm32_pka_point *kp, ++ const enum stm32_pka_curve_id cid); ++TEE_Result stm32_pka_edac_gen_pubkey(const struct stm32_pka_bn *k, ++ struct stm32_pka_point *pk, ++ const enum stm32_pka_curve_id cid); ++TEE_Result stm32_pka_ecdsa_sign(const void *hash, const unsigned int hash_size, ++ struct stm32_pka_bn *sig_r, ++ struct stm32_pka_bn *sig_s, ++ const struct stm32_pka_bn *d, ++ const struct stm32_pka_bn *k, ++ const enum stm32_pka_curve_id cid); ++TEE_Result stm32_pka_ecdsa_verif(const void *hash, const unsigned int hash_size, ++ const struct stm32_pka_bn *sig_r, ++ const struct stm32_pka_bn *sig_s, ++ const struct stm32_pka_point *pk, ++ const enum stm32_pka_curve_id cid); ++#endif /* STM32_PKA_H */ +diff --git a/core/drivers/crypto/stm32/stm32_saes.c b/core/drivers/crypto/stm32/stm32_saes.c +new file mode 100644 +index 000000000..ce04b7473 +--- /dev/null ++++ b/core/drivers/crypto/stm32/stm32_saes.c +@@ -0,0 +1,1221 @@ ++// SPDX-License-Identifier: BSD-3-Clause ++/* ++ * Copyright (c) 2021, STMicroelectronics - All Rights Reserved ++ */ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#include "common.h" ++#include "stm32_saes.h" ++ ++#define INT8_BIT U(8) ++#define AES_BLOCK_SIZE_BIT 128U ++#define AES_BLOCK_SIZE (AES_BLOCK_SIZE_BIT / INT8_BIT) ++#define AES_BLOCK_NB_U32 (AES_BLOCK_SIZE / sizeof(uint32_t)) ++ ++#define AES_KEYSIZE_128 16U ++#define AES_KEYSIZE_256 32U ++#define AES_IVSIZE 16U ++ ++/* SAES control register */ ++#define _SAES_CR 0x0U ++/* SAES status register */ ++#define _SAES_SR 0x04U ++/* SAES data input register */ ++#define _SAES_DINR 0x08U ++/* SAES data output register */ ++#define _SAES_DOUTR 0x0CU ++/* SAES key registers [0-3] */ ++#define _SAES_KEYR0 0x10U ++#define _SAES_KEYR1 0x14U ++#define _SAES_KEYR2 0x18U ++#define _SAES_KEYR3 0x1CU ++/* SAES initialization vector registers [0-3] */ ++#define _SAES_IVR0 0x20U ++#define _SAES_IVR1 0x24U ++#define _SAES_IVR2 0x28U ++#define _SAES_IVR3 0x2CU ++/* SAES key registers [4-7] */ ++#define _SAES_KEYR4 0x30U ++#define _SAES_KEYR5 0x34U ++#define _SAES_KEYR6 0x38U ++#define _SAES_KEYR7 0x3CU ++/* SAES suspend registers [0-7] */ ++#define _SAES_SUSPR0 0x40U ++#define _SAES_SUSPR1 0x44U ++#define _SAES_SUSPR2 0x48U ++#define _SAES_SUSPR3 0x4CU ++#define _SAES_SUSPR4 0x50U ++#define _SAES_SUSPR5 0x54U ++#define _SAES_SUSPR6 0x58U ++#define _SAES_SUSPR7 0x5CU ++/* SAES Interrupt Enable Register */ ++#define _SAES_IER 0x300U ++/* SAES Interrupt Status Register */ ++#define _SAES_ISR 0x304U ++/* SAES Interrupt Clear Register */ ++#define _SAES_ICR 0x308U ++ ++/* SAES control register fields */ ++#define _SAES_CR_RESET_VALUE 0x0U ++#define _SAES_CR_IPRST BIT(31) ++#define _SAES_CR_KEYSEL_MASK GENMASK_32(30, 28) ++#define _SAES_CR_KEYSEL_SHIFT 28U ++#define _SAES_CR_KEYSEL_SOFT 0x0U ++#define _SAES_CR_KEYSEL_DHUK 0x1U ++#define _SAES_CR_KEYSEL_BHK 0x2U ++#define _SAES_CR_KEYSEL_BHU_XOR_BH_K 0x4U ++#define _SAES_CR_KEYSEL_TEST 0x7U ++#define _SAES_CR_KSHAREID_MASK GENMASK_32(27, 26) ++#define _SAES_CR_KSHAREID_SHIFT 26U ++#define _SAES_CR_KSHAREID_CRYP 0x0U ++#define _SAES_CR_KEYMOD_MASK GENMASK_32(25, 24) ++#define _SAES_CR_KEYMOD_SHIFT 24U ++#define _SAES_CR_KEYMOD_NORMAL 0x0U ++#define _SAES_CR_KEYMOD_WRAPPED 0x1U ++#define _SAES_CR_KEYMOD_SHARED 0x2U ++#define _SAES_CR_NPBLB_MASK GENMASK_32(23, 20) ++#define _SAES_CR_NPBLB_SHIFT 20U ++#define _SAES_CR_KEYPROT BIT(19) ++#define _SAES_CR_KEYSIZE BIT(18) ++#define _SAES_CR_GCMPH_MASK GENMASK_32(14, 13) ++#define _SAES_CR_GCMPH_SHIFT 13U ++#define _SAES_CR_GCMPH_INIT 0U ++#define _SAES_CR_GCMPH_HEADER 1U ++#define _SAES_CR_GCMPH_PAYLOAD 2U ++#define _SAES_CR_GCMPH_FINAL 3U ++#define _SAES_CR_DMAOUTEN BIT(12) ++#define _SAES_CR_DMAINEN BIT(11) ++#define _SAES_CR_CHMOD_MASK (BIT(16) | GENMASK_32(6, 5)) ++#define _SAES_CR_CHMOD_SHIFT 5U ++#define _SAES_CR_CHMOD_ECB 0x0U ++#define _SAES_CR_CHMOD_CBC 0x1U ++#define _SAES_CR_CHMOD_CTR 0x2U ++#define _SAES_CR_CHMOD_GCM 0x3U ++#define _SAES_CR_CHMOD_GMAC 0x3U ++#define _SAES_CR_CHMOD_CCM 0x800U ++#define _SAES_CR_MODE_MASK GENMASK_32(4, 3) ++#define _SAES_CR_MODE_SHIFT 3U ++#define _SAES_CR_MODE_ENC 0U ++#define _SAES_CR_MODE_KEYPREP 1U ++#define _SAES_CR_MODE_DEC 2U ++#define _SAES_CR_DATATYPE_MASK GENMASK_32(2, 1) ++#define _SAES_CR_DATATYPE_SHIFT 1U ++#define _SAES_CR_DATATYPE_NONE 0U ++#define _SAES_CR_DATATYPE_HALF_WORD 1U ++#define _SAES_CR_DATATYPE_BYTE 2U ++#define _SAES_CR_DATATYPE_BIT 3U ++#define _SAES_CR_EN BIT(0) ++ ++/* SAES status register fields */ ++#define _SAES_SR_KEYVALID BIT(7) ++#define _SAES_SR_BUSY BIT(3) ++#define _SAES_SR_WRERR BIT(2) ++#define _SAES_SR_RDERR BIT(1) ++#define _SAES_SR_CCF BIT(0) ++ ++/* SAES interrupt registers fields */ ++#define _SAES_I_RNG_ERR BIT(3) ++#define _SAES_I_KEY_ERR BIT(2) ++#define _SAES_I_RW_ERR BIT(1) ++#define _SAES_I_CC BIT(0) ++ ++#define SAES_TIMEOUT_US 100000U ++#define TIMEOUT_US_1MS 1000U ++ ++/* ++ * Macro to manage bit manipulation when we work on local variable ++ * before writing only once to the real register. ++ */ ++#define CLRBITS(v, bits) ((v) &= ~(bits)) ++#define SETBITS(v, bits) ((v) |= (bits)) ++ ++#define IS_CHAINING_MODE(mod, cr) \ ++ (((cr) & _SAES_CR_CHMOD_MASK) == (_SAES_CR_CHMOD_##mod << \ ++ _SAES_CR_CHMOD_SHIFT)) ++ ++#define SET_CHAINING_MODE(mod, cr) \ ++ clrsetbits(&(cr), _SAES_CR_CHMOD_MASK, (_SAES_CR_CHMOD_##mod << \ ++ _SAES_CR_CHMOD_SHIFT)) ++ ++#define TOBE32(x) TEE_U32_BSWAP((x)) ++#define FROMBE32(x) TEE_U32_BSWAP((x)) ++ ++static struct stm32_saes_platdata saes_pdata; ++static struct mutex saes_lock = MUTEX_INITIALIZER; ++ ++static void clrsetbits(uint32_t *v, uint32_t mask, uint32_t bits) ++{ ++ *v = (*v & ~mask) | bits; ++} ++ ++static bool does_chaining_mode_need_iv(uint32_t cr) ++{ ++ return !IS_CHAINING_MODE(ECB, cr); ++} ++ ++static bool is_encrypt(uint32_t cr) ++{ ++ return (cr & _SAES_CR_MODE_MASK) == (_SAES_CR_MODE_ENC << ++ _SAES_CR_MODE_SHIFT); ++} ++ ++static bool is_decrypt(uint32_t cr) ++{ ++ return (cr & _SAES_CR_MODE_MASK) == (_SAES_CR_MODE_DEC << ++ _SAES_CR_MODE_SHIFT); ++} ++ ++static bool does_need_npblb(uint32_t cr) ++{ ++ return (IS_CHAINING_MODE(GCM, cr) && is_encrypt(cr)) || ++ (IS_CHAINING_MODE(CCM, cr) && is_decrypt(cr)); ++} ++ ++static bool can_suspend(uint32_t cr) ++{ ++ return !IS_CHAINING_MODE(GCM, cr); ++} ++ ++static void write_aligned_block(vaddr_t base, uint32_t *data) ++{ ++ unsigned int i; ++ ++ for (i = 0U; i < AES_BLOCK_NB_U32; i++) { ++ /* No need to htobe() as we configure the HW to swap bytes */ ++ io_write32(base + _SAES_DINR, data[i]); ++ } ++} ++ ++static void write_block(vaddr_t base, uint8_t *data) ++{ ++ if (IS_ALIGNED_WITH_TYPE(data, uint32_t)) { ++ write_aligned_block(base, (void *)data); ++ } else { ++ uint32_t data_u32[AES_BLOCK_NB_U32]; ++ ++ memcpy(data_u32, data, sizeof(data_u32)); ++ write_aligned_block(base, data_u32); ++ } ++} ++ ++static void read_aligned_block(vaddr_t base, uint32_t *data) ++{ ++ unsigned int i = 0U; ++ ++ for (i = 0U; i < AES_BLOCK_NB_U32; i++) { ++ /* No need to htobe() as we configure the HW to swap bytes */ ++ data[i] = io_read32(base + _SAES_DOUTR); ++ } ++} ++ ++static void read_block(vaddr_t base, uint8_t *data) ++{ ++ if (IS_ALIGNED_WITH_TYPE(data, uint32_t)) { ++ read_aligned_block(base, (void *)data); ++ } else { ++ uint32_t data_u32[AES_BLOCK_NB_U32]; ++ ++ read_aligned_block(base, data_u32); ++ ++ memcpy(data, data_u32, sizeof(data_u32)); ++ } ++} ++ ++static TEE_Result wait_computation_completed(vaddr_t base) ++{ ++ uint64_t timeout_ref = timeout_init_us(SAES_TIMEOUT_US); ++ ++ while ((io_read32(base + _SAES_SR) & _SAES_SR_CCF) != _SAES_SR_CCF) ++ if (timeout_elapsed(timeout_ref)) ++ break; ++ ++ if ((io_read32(base + _SAES_SR) & _SAES_SR_CCF) != _SAES_SR_CCF) { ++ DMSG("CCF timeout"); ++ return TEE_ERROR_BUSY; ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++static void clear_computation_completed(uintptr_t base) ++{ ++ io_setbits32(base + _SAES_ICR, _SAES_I_CC); ++} ++ ++static TEE_Result saes_start(struct stm32_saes_context *ctx) ++{ ++ uint64_t timeout_ref = 0; ++ ++ /* Reset IP */ ++ io_setbits32(ctx->base + _SAES_CR, _SAES_CR_IPRST); ++ io_clrbits32(ctx->base + _SAES_CR, _SAES_CR_IPRST); ++ ++ timeout_ref = timeout_init_us(SAES_TIMEOUT_US); ++ while (io_read32(ctx->base + _SAES_SR) & _SAES_SR_BUSY) ++ if (timeout_elapsed(timeout_ref)) ++ break; ++ ++ if ((io_read32(ctx->base + _SAES_SR) & _SAES_SR_BUSY) == ++ _SAES_SR_BUSY) { ++ DMSG("busy timeout"); ++ return TEE_ERROR_BUSY; ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++static void saes_end(struct stm32_saes_context *ctx, int prev_error) ++{ ++ if (prev_error) { ++ /* Reset IP */ ++ io_setbits32(ctx->base + _SAES_CR, _SAES_CR_IPRST); ++ io_clrbits32(ctx->base + _SAES_CR, _SAES_CR_IPRST); ++ } ++ ++ /* Disable the SAES peripheral */ ++ io_clrbits32(ctx->base + _SAES_CR, _SAES_CR_EN); ++} ++ ++static void saes_write_iv(struct stm32_saes_context *ctx) ++{ ++ /* If chaining mode need to restore IV */ ++ if (does_chaining_mode_need_iv(ctx->cr)) { ++ uint8_t i = 0; ++ ++ /* Restore the _SAES_IVRx */ ++ for (i = 0; i < AES_IVSIZE / sizeof(uint32_t); i++) { ++ io_write32(ctx->base + _SAES_IVR0 + i * ++ sizeof(uint32_t), ctx->iv[i]); ++ } ++ } ++} ++ ++static void saes_save_suspend(struct stm32_saes_context *ctx) ++{ ++ uint8_t i = 0; ++ ++ for (i = 0; i < 8; i++) { ++ ctx->susp[i] = io_read32(ctx->base + _SAES_SUSPR0 + i ++ * sizeof(uint32_t)); ++ } ++} ++ ++static void saes_restore_suspend(struct stm32_saes_context *ctx) ++{ ++ uint8_t i = 0; ++ ++ for (i = 0; i < 8; i++) { ++ io_write32(ctx->base + _SAES_SUSPR0 + i * sizeof(uint32_t), ++ ctx->susp[i]); ++ } ++} ++ ++static void saes_write_key(struct stm32_saes_context *ctx) ++{ ++ /* Restore the _SAES_KEYRx if SOFTWARE key */ ++ if ((ctx->cr & _SAES_CR_KEYSEL_MASK) == ++ (_SAES_CR_KEYSEL_SOFT << _SAES_CR_KEYSEL_SHIFT)) { ++ uint8_t i = 0; ++ ++ for (i = 0; i < AES_KEYSIZE_128 / sizeof(uint32_t); i++) ++ io_write32(ctx->base + _SAES_KEYR0 + i * ++ sizeof(uint32_t), ++ ctx->key[i]); ++ ++ if ((ctx->cr & _SAES_CR_KEYSIZE) == _SAES_CR_KEYSIZE) { ++ for (i = 0; ++ i < (AES_KEYSIZE_256 / 2U) / sizeof(uint32_t); ++ i++) { ++ io_write32(ctx->base + _SAES_KEYR4 + i * ++ sizeof(uint32_t), ++ ctx->key[i + 4U]); ++ } ++ } ++ } ++} ++ ++static TEE_Result saes_prepare_key(struct stm32_saes_context *ctx) ++{ ++ /* Disable the SAES peripheral */ ++ io_clrbits32(ctx->base + _SAES_CR, _SAES_CR_EN); ++ ++ /* Set key size */ ++ if ((ctx->cr & _SAES_CR_KEYSIZE) != 0U) ++ io_setbits32(ctx->base + _SAES_CR, _SAES_CR_KEYSIZE); ++ else ++ io_clrbits32(ctx->base + _SAES_CR, _SAES_CR_KEYSIZE); ++ ++ saes_write_key(ctx); ++ ++ /* ++ * For ECB/CBC decryption, key preparation mode must be selected ++ * to populate the key. ++ */ ++ if ((IS_CHAINING_MODE(ECB, ctx->cr) || ++ IS_CHAINING_MODE(CBC, ctx->cr)) && is_decrypt(ctx->cr)) { ++ TEE_Result res = TEE_SUCCESS; ++ ++ /* Select Mode 2 */ ++ io_clrsetbits32(ctx->base + _SAES_CR, _SAES_CR_MODE_MASK, ++ _SAES_CR_MODE_KEYPREP << _SAES_CR_MODE_SHIFT); ++ ++ /* Enable SAES */ ++ io_setbits32(ctx->base + _SAES_CR, _SAES_CR_EN); ++ ++ res = wait_computation_completed(ctx->base); ++ if (res) ++ return res; ++ ++ clear_computation_completed(ctx->base); ++ ++ /* Set Mode 3 */ ++ io_clrsetbits32(ctx->base + _SAES_CR, _SAES_CR_MODE_MASK, ++ _SAES_CR_MODE_DEC << _SAES_CR_MODE_SHIFT); ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result save_context(struct stm32_saes_context *ctx) ++{ ++ if ((io_read32(ctx->base + _SAES_SR) & _SAES_SR_CCF) != 0U) { ++ /* Device should not be in a processing phase */ ++ return TEE_ERROR_BAD_STATE; ++ } ++ ++ /* Save CR */ ++ ctx->cr = io_read32(ctx->base + _SAES_CR); ++ ++ if (!can_suspend(ctx->cr)) ++ return TEE_SUCCESS; ++ ++ saes_save_suspend(ctx); ++ ++ /* If chaining mode need to save current IV */ ++ if (does_chaining_mode_need_iv(ctx->cr)) { ++ uint8_t i = 0; ++ ++ /* Save IV */ ++ for (i = 0; i < AES_IVSIZE / sizeof(uint32_t); i++) { ++ ctx->iv[i] = io_read32(ctx->base + _SAES_IVR0 + i * ++ sizeof(uint32_t)); ++ } ++ } ++ ++ /* Disable the SAES peripheral */ ++ io_clrbits32(ctx->base + _SAES_CR, _SAES_CR_EN); ++ ++ return TEE_SUCCESS; ++} ++ ++/* To resume the processing of a message */ ++static TEE_Result restore_context(struct stm32_saes_context *ctx) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ ++ /* IP should be disabled */ ++ if ((io_read32(ctx->base + _SAES_CR) & _SAES_CR_EN) != 0U) { ++ DMSG("Device is still enabled"); ++ return TEE_ERROR_BAD_STATE; ++ } ++ ++ /* Reset internal state */ ++ io_setbits32(ctx->base + _SAES_CR, _SAES_CR_IPRST); ++ ++ /* Restore the _SAES_CR */ ++ io_write32(ctx->base + _SAES_CR, ctx->cr); ++ ++ /* Write key and, in case of CBC or ECB decrypt, prepare it */ ++ res = saes_prepare_key(ctx); ++ if (res) ++ return res; ++ ++ saes_restore_suspend(ctx); ++ ++ saes_write_iv(ctx); ++ ++ /* Enable the SAES peripheral */ ++ io_setbits32(ctx->base + _SAES_CR, _SAES_CR_EN); ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result do_from_init_to_phase(struct stm32_saes_context *ctx, ++ uint32_t new_phase) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ ++ /* We didn't run the init phase yet */ ++ res = restore_context(ctx); ++ if (res) ++ return res; ++ ++ res = wait_computation_completed(ctx->base); ++ if (res) ++ return res; ++ ++ clear_computation_completed(ctx->base); ++ ++ /* Move to 'new_phase' */ ++ io_clrsetbits32(ctx->base + _SAES_CR, _SAES_CR_GCMPH_MASK, ++ new_phase << _SAES_CR_GCMPH_SHIFT); ++ ++ /* Enable the SAES peripheral (init disabled it) */ ++ io_setbits32(ctx->base + _SAES_CR, _SAES_CR_EN); ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result do_from_header_to_phase(struct stm32_saes_context *ctx, ++ uint32_t new_phase) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ ++ if (can_suspend(ctx->cr)) { ++ res = restore_context(ctx); ++ if (res) ++ return res; ++ } ++ ++ if (ctx->extra_size) { ++ /* Manage unaligned header data before moving to next phase */ ++ memset((uint8_t *)ctx->extra + ctx->extra_size, 0, ++ AES_BLOCK_SIZE - ctx->extra_size); ++ ++ write_aligned_block(ctx->base, ctx->extra); ++ ++ res = wait_computation_completed(ctx->base); ++ if (res) ++ return res; ++ ++ clear_computation_completed(ctx->base); ++ ++ ctx->assoc_len += (ctx->extra_size) * INT8_BIT; ++ ctx->extra_size = 0U; ++ } ++ ++ /* Move to 'new_phase' */ ++ io_clrsetbits32(ctx->base + _SAES_CR, _SAES_CR_GCMPH_MASK, ++ new_phase << _SAES_CR_GCMPH_SHIFT); ++ ++ return TEE_SUCCESS; ++} ++ ++/** ++ * @brief Start a AES computation. ++ * @param ctx: SAES process context ++ * @param is_dec: true if decryption, false if encryption ++ * @param ch_mode: define the chaining mode ++ * @param key_select: define where the key comes from ++ * @param key: pointer to key (if key_select is KEY_SOFT, else unused) ++ * @param key_size: key size ++ * @param iv: pointer to initialization vector (unused if ch_mode is ECB) ++ * @param iv_size: iv size ++ * @note this function doesn't access to hardware but stores in ctx the values ++ * ++ * @retval TEE_SUCCESS if OK. ++ */ ++TEE_Result stm32_saes_init(struct stm32_saes_context *ctx, bool is_dec, ++ enum stm32_saes_chaining_mode ch_mode, ++ enum stm32_saes_key_selection key_select, ++ const void *key, size_t key_size, const void *iv, ++ size_t iv_size) ++{ ++ unsigned int i = 0; ++ const uint32_t *iv_u32 = NULL; ++ uint32_t local_iv[4] = { 0 }; ++ const uint32_t *key_u32 = NULL; ++ uint32_t local_key[8] = { 0 }; ++ ++ if (!ctx) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ ctx->assoc_len = 0U; ++ ctx->load_len = 0U; ++ ctx->extra_size = 0U; ++ ctx->lock = &saes_lock; ++ ++ ctx->base = io_pa_or_va(&saes_pdata.base, 1); ++ ctx->cr = _SAES_CR_RESET_VALUE; ++ ++ /* We want buffer to be u32 aligned */ ++ if (IS_ALIGNED_WITH_TYPE(key, uint32_t)) { ++ key_u32 = key; ++ } else { ++ memcpy(local_key, key, key_size); ++ key_u32 = local_key; ++ } ++ ++ if (IS_ALIGNED_WITH_TYPE(iv, uint32_t)) { ++ iv_u32 = iv; ++ } else { ++ memcpy(local_iv, iv, iv_size); ++ iv_u32 = local_iv; ++ } ++ ++ if (is_dec) ++ clrsetbits(&ctx->cr, _SAES_CR_MODE_MASK, ++ _SAES_CR_MODE_DEC << _SAES_CR_MODE_SHIFT); ++ else ++ clrsetbits(&ctx->cr, _SAES_CR_MODE_MASK, ++ _SAES_CR_MODE_ENC << _SAES_CR_MODE_SHIFT); ++ ++ /* Save chaining mode */ ++ switch (ch_mode) { ++ case STM32_SAES_MODE_ECB: ++ SET_CHAINING_MODE(ECB, ctx->cr); ++ break; ++ case STM32_SAES_MODE_CBC: ++ SET_CHAINING_MODE(CBC, ctx->cr); ++ break; ++ case STM32_SAES_MODE_CTR: ++ SET_CHAINING_MODE(CTR, ctx->cr); ++ break; ++ case STM32_SAES_MODE_GCM: ++ SET_CHAINING_MODE(GCM, ctx->cr); ++ break; ++ case STM32_SAES_MODE_CCM: ++ SET_CHAINING_MODE(CCM, ctx->cr); ++ break; ++ default: ++ return TEE_ERROR_BAD_PARAMETERS; ++ } ++ ++ /* ++ * We will use HW Byte swap (_SAES_CR_DATATYPE_BYTE) for data. ++ * So we won't need to ++ * TOBE32(data) before write to DINR ++ * nor ++ * FROMBE32 after reading from DOUTR. ++ * ++ * But note that wrap key only accept _SAES_CR_DATATYPE_NONE. ++ */ ++ clrsetbits(&ctx->cr, _SAES_CR_DATATYPE_MASK, ++ _SAES_CR_DATATYPE_BYTE << _SAES_CR_DATATYPE_SHIFT); ++ ++ /* Configure keysize */ ++ switch (key_size) { ++ case AES_KEYSIZE_128: ++ CLRBITS(ctx->cr, _SAES_CR_KEYSIZE); ++ break; ++ case AES_KEYSIZE_256: ++ SETBITS(ctx->cr, _SAES_CR_KEYSIZE); ++ break; ++ default: ++ return TEE_ERROR_BAD_PARAMETERS; ++ } ++ ++ /* Configure key */ ++ switch (key_select) { ++ case STM32_SAES_KEY_SOFT: ++ clrsetbits(&ctx->cr, _SAES_CR_KEYSEL_MASK, ++ _SAES_CR_KEYSEL_SOFT << _SAES_CR_KEYSEL_SHIFT); ++ /* Save key */ ++ switch (key_size) { ++ case AES_KEYSIZE_128: ++ /* First 16 bytes == 4 u32 */ ++ for (i = 0U; i < AES_KEYSIZE_128 / sizeof(uint32_t); ++ i++) { ++ ctx->key[i] = TOBE32(key_u32[3 - i]); ++ /* ++ * /!\ we save the key in HW byte order ++ * and word order: key[i] is for _SAES_KEYRi. ++ */ ++ } ++ break; ++ case AES_KEYSIZE_256: ++ for (i = 0U; i < AES_KEYSIZE_256 / sizeof(uint32_t); ++ i++) { ++ ctx->key[i] = TOBE32(key_u32[7 - i]); ++ /* ++ * /!\ we save the key in HW byte order ++ * and word order: key[i] is for _SAES_KEYRi. ++ */ ++ } ++ break; ++ default: ++ return TEE_ERROR_BAD_PARAMETERS; ++ } ++ break; ++ case STM32_SAES_KEY_DHU: ++ clrsetbits(&ctx->cr, _SAES_CR_KEYSEL_MASK, ++ _SAES_CR_KEYSEL_DHUK << _SAES_CR_KEYSEL_SHIFT); ++ break; ++ case STM32_SAES_KEY_BH: ++ clrsetbits(&ctx->cr, _SAES_CR_KEYSEL_MASK, ++ _SAES_CR_KEYSEL_BHK << _SAES_CR_KEYSEL_SHIFT); ++ break; ++ case STM32_SAES_KEY_BHU_XOR_BH: ++ clrsetbits(&ctx->cr, _SAES_CR_KEYSEL_MASK, ++ _SAES_CR_KEYSEL_BHU_XOR_BH_K << ++ _SAES_CR_KEYSEL_SHIFT); ++ break; ++ case STM32_SAES_KEY_WRAPPED: ++ clrsetbits(&ctx->cr, _SAES_CR_KEYSEL_MASK, ++ _SAES_CR_KEYSEL_SOFT << _SAES_CR_KEYSEL_SHIFT); ++ break; ++ ++ default: ++ return TEE_ERROR_BAD_PARAMETERS; ++ } ++ ++ /* Save IV */ ++ if (ch_mode != STM32_SAES_MODE_ECB) { ++ if (!iv || iv_size != AES_IVSIZE) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ for (i = 0U; i < AES_IVSIZE / sizeof(uint32_t); i++) { ++ ctx->iv[i] = TOBE32(iv_u32[3 - i]); ++ /* /!\ We save the iv in HW byte order */ ++ } ++ } ++ ++ /* Reset suspend registers */ ++ memset(ctx->susp, 0, sizeof(ctx->susp)); ++ ++ return saes_start(ctx); ++} ++ ++/** ++ * @brief Update (or start) a AES authentificate process of ++ * associated data (CCM or GCM). ++ * @param ctx: SAES process context ++ * @param data: pointer to associated data ++ * @param data_size: data size ++ * ++ * @retval 0 if OK. ++ */ ++TEE_Result stm32_saes_update_assodata(struct stm32_saes_context *ctx, ++ uint8_t *data, size_t data_size) ++{ ++ TEE_Result res = 0; ++ unsigned int i = 0U; ++ uint32_t previous_phase = 0U; ++ ++ if (!ctx) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ /* If no associated data, nothing to do */ ++ if (!data || !data_size) ++ return TEE_SUCCESS; ++ ++ mutex_lock(ctx->lock); ++ ++ previous_phase = (ctx->cr & _SAES_CR_GCMPH_MASK) >> ++ _SAES_CR_GCMPH_SHIFT; ++ ++ switch (previous_phase) { ++ case _SAES_CR_GCMPH_INIT: ++ res = do_from_init_to_phase(ctx, _SAES_CR_GCMPH_HEADER); ++ break; ++ case _SAES_CR_GCMPH_HEADER: ++ /* ++ * Function update_assodata was already called. ++ * We only need to restore the context. ++ */ ++ if (can_suspend(ctx->cr)) ++ res = restore_context(ctx); ++ ++ break; ++ default: ++ DMSG("out of order call"); ++ res = TEE_ERROR_BAD_STATE; ++ } ++ ++ if (res) ++ goto out; ++ ++ /* Manage if remaining data from a previous update_assodata call */ ++ if (ctx->extra_size && ++ ((ctx->extra_size + data_size) >= AES_BLOCK_SIZE)) { ++ uint32_t block[AES_BLOCK_NB_U32] = { 0 }; ++ ++ memcpy(block, ctx->extra, ctx->extra_size); ++ memcpy((uint8_t *)block + ctx->extra_size, data, ++ AES_BLOCK_SIZE - ctx->extra_size); ++ ++ write_aligned_block(ctx->base, block); ++ ++ res = wait_computation_completed(ctx->base); ++ if (res) ++ goto out; ++ ++ clear_computation_completed(ctx->base); ++ ++ i += AES_BLOCK_SIZE - ctx->extra_size; ++ ctx->extra_size = 0; ++ ctx->assoc_len += AES_BLOCK_SIZE_BIT; ++ } ++ ++ while (data_size - i >= AES_BLOCK_SIZE) { ++ write_block(ctx->base, data + i); ++ ++ res = wait_computation_completed(ctx->base); ++ if (res) ++ goto out; ++ ++ clear_computation_completed(ctx->base); ++ ++ /* Process next block */ ++ i += AES_BLOCK_SIZE; ++ ctx->assoc_len += AES_BLOCK_SIZE_BIT; ++ } ++ ++ /* ++ * Manage last block if not a block size multiple: ++ * Save remaining data to manage them later (potentially with new ++ * associated data). ++ */ ++ if (i < data_size) { ++ memcpy((uint8_t *)ctx->extra + ctx->extra_size, data + i, ++ data_size - i); ++ ctx->extra_size += data_size - i; ++ } ++ ++ res = save_context(ctx); ++out: ++ if (res) ++ saes_end(ctx, res); ++ ++ mutex_unlock(ctx->lock); ++ ++ return res; ++} ++ ++/** ++ * @brief Update (or start) a AES authenticate and de/encrypt with ++ * payload data (CCM or GCM). ++ * @param ctx: SAES process context ++ * @param last_block: true if last payload data block ++ * @param data_in: pointer to payload ++ * @param data_out: pointer where to save de/encrypted payload ++ * @param data_size: payload size ++ * ++ * @retval TEE_SUCCESS if OK. ++ */ ++TEE_Result stm32_saes_update_load(struct stm32_saes_context *ctx, ++ bool last_block, uint8_t *data_in, ++ uint8_t *data_out, size_t data_size) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ unsigned int i = 0U; ++ uint32_t previous_phase = 0U; ++ ++ if (!ctx) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ /* If there is no data, nothing to do */ ++ if (!data_in || !data_size) ++ return TEE_SUCCESS; ++ ++ mutex_lock(ctx->lock); ++ ++ previous_phase = ((ctx->cr & _SAES_CR_GCMPH_MASK) >> ++ _SAES_CR_GCMPH_SHIFT); ++ ++ switch (previous_phase) { ++ case _SAES_CR_GCMPH_INIT: ++ res = do_from_init_to_phase(ctx, _SAES_CR_GCMPH_PAYLOAD); ++ break; ++ case _SAES_CR_GCMPH_HEADER: ++ res = do_from_header_to_phase(ctx, _SAES_CR_GCMPH_PAYLOAD); ++ break; ++ case _SAES_CR_GCMPH_PAYLOAD: ++ /* new update_load call, we only need to restore context */ ++ if (can_suspend(ctx->cr)) ++ res = restore_context(ctx); ++ ++ break; ++ default: ++ DMSG("out of order call"); ++ res = TEE_ERROR_BAD_STATE; ++ } ++ ++ if (res) ++ goto out; ++ ++ while (i < ROUNDDOWN(data_size, AES_BLOCK_SIZE)) { ++ write_block(ctx->base, data_in + i); ++ ++ res = wait_computation_completed(ctx->base); ++ if (res) ++ goto out; ++ ++ read_block(ctx->base, data_out + i); ++ ++ clear_computation_completed(ctx->base); ++ ++ /* Process next block */ ++ i += AES_BLOCK_SIZE; ++ ctx->load_len += AES_BLOCK_SIZE_BIT; ++ } ++ ++ /* Manage last block if not a block size multiple */ ++ if (last_block && i < data_size) { ++ uint32_t block_in[AES_BLOCK_NB_U32] = { 0 }; ++ uint32_t block_out[AES_BLOCK_NB_U32] = { 0 }; ++ ++ memcpy(block_in, data_in + i, data_size - i); ++ ++ if (does_need_npblb(ctx->cr)) { ++ uint32_t npblb = AES_BLOCK_SIZE - (data_size - i); ++ ++ io_clrsetbits32(ctx->base + _SAES_CR, ++ _SAES_CR_NPBLB_MASK, ++ npblb << _SAES_CR_NPBLB_SHIFT); ++ } ++ ++ write_aligned_block(ctx->base, block_in); ++ ++ res = wait_computation_completed(ctx->base); ++ if (res) ++ goto out; ++ ++ read_aligned_block(ctx->base, block_out); ++ ++ clear_computation_completed(ctx->base); ++ ++ memcpy(data_out + i, block_out, data_size - i); ++ ++ ctx->load_len += (data_size - i) * INT8_BIT; ++ } ++ ++ res = save_context(ctx); ++out: ++ if (res) ++ saes_end(ctx, res); ++ ++ mutex_unlock(ctx->lock); ++ ++ return res; ++} ++ ++/** ++ * @brief Get authentication tag for AES authenticated algorithms (CCM or GCM). ++ * @param ctx: SAES process context ++ * @param tag: pointer where to save the tag ++ * @param data_size: tag size ++ * ++ * @retval TEE_SUCCESS if OK. ++ */ ++TEE_Result stm32_saes_final(struct stm32_saes_context *ctx, uint8_t *tag, ++ size_t tag_size) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ uint32_t tag_u32[4] = { 0 }; ++ uint32_t previous_phase = 0U; ++ ++ if (!ctx) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ mutex_lock(ctx->lock); ++ ++ previous_phase = ((ctx->cr & _SAES_CR_GCMPH_MASK) >> ++ _SAES_CR_GCMPH_SHIFT); ++ ++ switch (previous_phase) { ++ case _SAES_CR_GCMPH_INIT: ++ res = do_from_init_to_phase(ctx, _SAES_CR_GCMPH_FINAL); ++ break; ++ case _SAES_CR_GCMPH_HEADER: ++ res = do_from_header_to_phase(ctx, _SAES_CR_GCMPH_FINAL); ++ break; ++ case _SAES_CR_GCMPH_PAYLOAD: ++ if (can_suspend(ctx->cr)) ++ res = restore_context(ctx); ++ ++ /* Move to final phase */ ++ io_clrsetbits32(ctx->base + _SAES_CR, _SAES_CR_GCMPH_MASK, ++ _SAES_CR_GCMPH_FINAL << _SAES_CR_GCMPH_SHIFT); ++ break; ++ default: ++ DMSG("out of order call"); ++ res = TEE_ERROR_BAD_STATE; ++ } ++ if (res) ++ goto out; ++ ++ if (IS_CHAINING_MODE(GCM, ctx->cr)) { ++ /* No need to htobe() as we configure the HW to swap bytes */ ++ io_write32(ctx->base + _SAES_DINR, 0U); ++ io_write32(ctx->base + _SAES_DINR, ctx->assoc_len); ++ io_write32(ctx->base + _SAES_DINR, 0U); ++ io_write32(ctx->base + _SAES_DINR, ctx->load_len); ++ } ++ ++ res = wait_computation_completed(ctx->base); ++ if (res) ++ goto out; ++ ++ read_aligned_block(ctx->base, tag_u32); ++ ++ clear_computation_completed(ctx->base); ++ ++ memcpy(tag, tag_u32, MIN(sizeof(tag_u32), tag_size)); ++ ++out: ++ saes_end(ctx, res); ++ mutex_unlock(ctx->lock); ++ ++ return res; ++} ++ ++/** ++ * @brief Update (or start) a AES de/encrypt process (ECB, CBC or CTR). ++ * @param ctx: SAES process context ++ * @param last_block: true if last payload data block ++ * @param data_in: pointer to payload ++ * @param data_out: pointer where to save de/encrypted payload ++ * @param data_size: payload size ++ * ++ * @retval TEE_SUCCESS if OK. ++ */ ++TEE_Result stm32_saes_update(struct stm32_saes_context *ctx, bool last_block, ++ uint8_t *data_in, uint8_t *data_out, ++ size_t data_size) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ unsigned int i = 0U; ++ ++ if (!ctx) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ mutex_lock(ctx->lock); ++ ++ /* ++ * In CBC encryption we need to manage specifically last 2 128bits ++ * blocks if total size in not a block size aligned ++ * work TODO. Currently return TEE_ERROR_NOT_IMPLEMENTED. ++ * Morevoer as we need to know last 2 blocks, if unaligned and ++ * call with less than two blocks, return TEE_ERROR_BAD_STATE. ++ */ ++ if (last_block && IS_CHAINING_MODE(CBC, ctx->cr) && ++ is_encrypt(ctx->cr) && ++ (ROUNDDOWN(data_size, AES_BLOCK_SIZE) != data_size)) { ++ if (data_size < AES_BLOCK_SIZE * 2) { ++ /* ++ * If CBC, size of the last part should be at ++ * least 2*AES_BLOCK_SIZE ++ */ ++ EMSG("Unexpected last block size"); ++ res = TEE_ERROR_BAD_STATE; ++ goto out; ++ } ++ /* ++ * Moreover the CBC specific padding for encrypt is not ++ * yet implemented, and not used in OPTEE ++ */ ++ res = TEE_ERROR_NOT_IMPLEMENTED; ++ goto out; ++ } ++ ++ /* Manage remaining CTR mask from previous update call */ ++ if (IS_CHAINING_MODE(CTR, ctx->cr) && ++ ctx->extra_size) { ++ unsigned int j = 0; ++ uint8_t *mask = (uint8_t *)ctx->extra; ++ ++ for (j = 0; j < ctx->extra_size && i < data_size; j++, i++) ++ data_out[i] = data_in[i] ^ mask[j]; ++ ++ if (j != ctx->extra_size) { ++ /* ++ * We didn't consume all saved mask, ++ * but no more data. ++ */ ++ ++ /* We save remaining mask and its new size */ ++ memmove(ctx->extra, ctx->extra + j, ++ ctx->extra_size - j); ++ ctx->extra_size -= j; ++ ++ /* ++ * We don't need to save HW context we didn't ++ * modify HW state. ++ */ ++ res = TEE_SUCCESS; ++ goto out; ++ } ++ /* All extra mask consumed */ ++ ctx->extra_size = 0U; ++ } ++ ++ res = restore_context(ctx); ++ if (res) ++ goto out; ++ ++ while (data_size - i >= AES_BLOCK_SIZE) { ++ write_block(ctx->base, data_in + i); ++ ++ res = wait_computation_completed(ctx->base); ++ if (res) ++ goto out; ++ ++ read_block(ctx->base, data_out + i); ++ ++ clear_computation_completed(ctx->base); ++ ++ /* Process next block */ ++ i += AES_BLOCK_SIZE; ++ } ++ ++ /* Manage last block if not a block size multiple */ ++ if (i < data_size) { ++ if (IS_CHAINING_MODE(CTR, ctx->cr)) { ++ /* ++ * For CTR we save the generated mask to use it at next ++ * update call. ++ */ ++ uint32_t block_in[AES_BLOCK_NB_U32] = { 0 }; ++ uint32_t block_out[AES_BLOCK_NB_U32] = { 0 }; ++ ++ memcpy(block_in, data_in + i, data_size - i); ++ ++ write_aligned_block(ctx->base, block_in); ++ ++ res = wait_computation_completed(ctx->base); ++ if (res) ++ goto out; ++ ++ read_aligned_block(ctx->base, block_out); ++ ++ clear_computation_completed(ctx->base); ++ ++ memcpy(data_out + i, block_out, data_size - i); ++ ++ /* Save mask for possibly next call */ ++ ctx->extra_size = AES_BLOCK_SIZE - (data_size - i); ++ memcpy(ctx->extra, (uint8_t *)block_out + data_size - i, ++ ctx->extra_size); ++ } else { ++ /* CBC and ECB can manage only multiple of block_size */ ++ res = TEE_ERROR_BAD_PARAMETERS; ++ goto out; ++ } ++ } ++ ++ if (!last_block) ++ res = save_context(ctx); ++ ++out: ++ /* If last block or error, end of SAES process */ ++ if (last_block || res) ++ saes_end(ctx, res); ++ ++ mutex_unlock(ctx->lock); ++ ++ return res; ++} ++ ++#ifdef CFG_EMBED_DTB ++static TEE_Result stm32_saes_parse_fdt(struct stm32_saes_platdata *pdata, ++ const void *fdt, int node) ++{ ++ struct dt_node_info dt_saes = { }; ++ ++ _fdt_fill_device_info(fdt, &dt_saes, node); ++ ++ if (dt_saes.reg == DT_INFO_INVALID_REG || ++ dt_saes.reg_size == DT_INFO_INVALID_REG_SIZE || ++ dt_saes.reset == DT_INFO_INVALID_RESET) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ pdata->base.pa = dt_saes.reg; ++ io_pa_or_va_secure(&pdata->base, dt_saes.reg_size); ++ if (!pdata->base.va) ++ panic(); ++ ++ pdata->reset_id = (unsigned int)dt_saes.reset; ++ ++ return clk_dt_get_by_index(fdt, node, 0, &pdata->clk); ++} ++ ++__weak ++TEE_Result stm32_saes_get_platdata(struct stm32_saes_platdata *pdata __unused) ++{ ++ /* In DT config, the platform data are filled by DT file */ ++ return TEE_SUCCESS; ++} ++#else /* CFG_EMBED_DTB */ ++static int stm32_saes_parse_fdt(struct stm32_tamp_platdata *pdata, ++ const void *fdt, int node) ++{ ++ /* Do nothing, there is no fdt to parse in this case */ ++ return 0; ++} ++ ++/* This function can be overridden by platform to define pdata of SAES driver */ ++__weak ++TEE_Result stm32_saes_get_platdata(struct stm32_saes_platdata *pdata __unused) ++{ ++ return TEE_ITEM_NO_FOUND; ++} ++#endif ++ ++/** ++ * @brief Initialize SAES driver. ++ * @param None. ++ * @retval TEE_SUCCESS if OK. ++ */ ++static TEE_Result stm32_saes_probe(const void *fdt, int node, ++ const void *compat_data __unused) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ vaddr_t base = 0; ++ ++ if (stm32_saes_get_platdata(&saes_pdata)) ++ return TEE_ERROR_NOT_SUPPORTED; ++ ++ res = stm32_saes_parse_fdt(&saes_pdata, fdt, node); ++ if (res) ++ return res; ++ ++ clk_enable(saes_pdata.clk); ++ ++ if (stm32_reset_assert(saes_pdata.reset_id, TIMEOUT_US_1MS) != 0) ++ panic(); ++ ++ if (stm32_reset_deassert(saes_pdata.reset_id, TIMEOUT_US_1MS) != 0) ++ panic(); ++ ++ base = io_pa_or_va(&saes_pdata.base, 1); ++ io_write32(base + _SAES_CR, 0XFFFFFFFF); ++ io_write32(base + _SAES_CR, 0X0); ++ ++ if (IS_ENABLED(CFG_CRYPTO_DRV_CIPHER)) { ++ res = stm32_register_cipher(SAES_IP); ++ if (res) { ++ EMSG("Failed to register to cipher: %#"PRIx32, res); ++ panic(); ++ } ++ } ++ ++ return res; ++} ++ ++#if CFG_EMBED_DTB ++static const struct dt_device_match saes_match_table[] = { ++ { .compatible = "st,stm32mp13-saes" }, ++ { } ++}; ++ ++DEFINE_DT_DRIVER(stm32_saes_dt_driver) = { ++ .name = "stm32-saes", ++ .match_table = saes_match_table, ++ .probe = &stm32_saes_probe, ++}; ++#endif +diff --git a/core/drivers/crypto/stm32/stm32_saes.h b/core/drivers/crypto/stm32/stm32_saes.h +new file mode 100644 +index 000000000..d11ff9810 +--- /dev/null ++++ b/core/drivers/crypto/stm32/stm32_saes.h +@@ -0,0 +1,68 @@ ++/* SPDX-License-Identifier: BSD-3-Clause */ ++/* ++ * Copyright (c) 2021, STMicroelectronics - All Rights Reserved ++ */ ++ ++#ifndef STM32_SAES_H ++#define STM32_SAES_H ++ ++#include ++#include ++#include ++#include ++#include ++#include ++ ++struct stm32_saes_platdata { ++ struct io_pa_va base; ++ struct clk *clk; ++ unsigned int reset_id; ++}; ++ ++enum stm32_saes_chaining_mode { ++ STM32_SAES_MODE_ECB, ++ STM32_SAES_MODE_CBC, ++ STM32_SAES_MODE_CTR, ++ STM32_SAES_MODE_GCM, ++ STM32_SAES_MODE_CCM, ++}; ++ ++enum stm32_saes_key_selection { ++ STM32_SAES_KEY_SOFT, ++ STM32_SAES_KEY_DHU, /* Derived HW unique key */ ++ STM32_SAES_KEY_BH, /* Boot HW key */ ++ STM32_SAES_KEY_BHU_XOR_BH, /* XOR of DHUK and BHK */ ++ STM32_SAES_KEY_WRAPPED ++}; ++ ++struct stm32_saes_context { ++ vaddr_t base; ++ uint32_t cr; ++ struct mutex *lock; /* Save the HW instance mutex */ ++ uint32_t assoc_len; ++ uint32_t load_len; ++ uint32_t key[8]; /* In HW byte order */ ++ uint32_t iv[4]; /* In HW byte order */ ++ uint32_t susp[8]; ++ uint32_t extra[4]; ++ size_t extra_size; ++}; ++ ++TEE_Result stm32_saes_get_platdata(struct stm32_saes_platdata *pdata); ++ ++TEE_Result stm32_saes_init(struct stm32_saes_context *ctx, bool is_decrypt, ++ enum stm32_saes_chaining_mode ch_mode, ++ enum stm32_saes_key_selection key_select, ++ const void *key, size_t key_len, const void *iv, ++ size_t iv_len); ++TEE_Result stm32_saes_update(struct stm32_saes_context *ctx, bool last_block, ++ uint8_t *data_in, uint8_t *data_out, ++ size_t data_len); ++TEE_Result stm32_saes_update_assodata(struct stm32_saes_context *ctx, ++ uint8_t *data, size_t data_len); ++TEE_Result stm32_saes_update_load(struct stm32_saes_context *ctx, ++ bool last_block, uint8_t *data_in, ++ uint8_t *data_out, size_t data_len); ++TEE_Result stm32_saes_final(struct stm32_saes_context *ctx, uint8_t *tag, ++ size_t tag_len); ++#endif +diff --git a/core/drivers/crypto/stm32/sub.mk b/core/drivers/crypto/stm32/sub.mk +index 675be037e..3084388b8 100644 +--- a/core/drivers/crypto/stm32/sub.mk ++++ b/core/drivers/crypto/stm32/sub.mk +@@ -1,3 +1,9 @@ + srcs-$(CFG_STM32_CRYP) += stm32_cryp.c ++srcs-$(CFG_STM32_SAES) += stm32_saes.c ++srcs-$(CFG_STM32_PKA) += stm32_pka.c ++srcs-$(CFG_STM32_HASH) += stm32_hash.c + srcs-$(CFG_CRYPTO_DRV_CIPHER) += cipher.c + srcs-$(CFG_CRYPTO_DRV_AUTHENC) += authenc.c ++srcs-$(CFG_CRYPTO_DRV_ECC) += ecc.c ++srcs-$(CFG_CRYPTO_DRV_HASH) += hash.c ++srcs-$(CFG_CRYPTO_DRV_MAC) += hmac.c +diff --git a/core/drivers/firewall/stm32_etzpc.c b/core/drivers/firewall/stm32_etzpc.c +new file mode 100644 +index 000000000..a36e07b5e +--- /dev/null ++++ b/core/drivers/firewall/stm32_etzpc.c +@@ -0,0 +1,815 @@ ++// SPDX-License-Identifier: BSD-3-Clause ++/* ++ * Copyright (c) 2015-2021, ARM Limited and Contributors. All rights reserved. ++ * Copyright (c) 2017-2021, STMicroelectronics ++ */ ++ ++/* ++ * STM32 ETPZC acts as a firewall on stm32mp SoC peripheral interfaces and ++ * internal memories. The driver expects a single instance of the controller ++ * in the platform. ++ * ++ * The driver API is defined in header file stm32_etzpc.h. ++ * ++ * Driver registers a PM callback for restoration of the access permissions ++ * when it resumes. ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++/* Devicetree compatibility */ ++#define ETZPC_LOCK_MASK BIT(0) ++#define ETZPC_MODE_SHIFT 8 ++#define ETZPC_MODE_MASK GENMASK_32(1, 0) ++#define ETZPC_ID_SHIFT 16 ++#define ETZPC_ID_MASK GENMASK_32(7, 0) ++ ++/* ID Registers */ ++#define ETZPC_TZMA0_SIZE 0x000U ++#define ETZPC_DECPROT0 0x010U ++#define ETZPC_DECPROT_LOCK0 0x030U ++#define ETZPC_HWCFGR 0x3F0U ++#define ETZPC_VERR 0x3F4U ++ ++/* ID Registers fields */ ++#define ETZPC_TZMA0_SIZE_LOCK BIT(31) ++#define ETZPC_DECPROT0_MASK GENMASK_32(1, 0) ++#define ETZPC_HWCFGR_NUM_TZMA_MASK GENMASK_32(7, 0) ++#define ETZPC_HWCFGR_NUM_TZMA_SHIFT 0 ++#define ETZPC_HWCFGR_NUM_PER_SEC_MASK GENMASK_32(15, 8) ++#define ETZPC_HWCFGR_NUM_PER_SEC_SHIFT 8 ++#define ETZPC_HWCFGR_NUM_AHB_SEC_MASK GENMASK_32(23, 16) ++#define ETZPC_HWCFGR_NUM_AHB_SEC_SHIFT 16 ++#define ETZPC_HWCFGR_CHUNCKS1N4_MASK GENMASK_32(31, 24) ++#define ETZPC_HWCFGR_CHUNCKS1N4_SHIFT 24 ++ ++#define DECPROT_SHIFT 1 ++#define IDS_PER_DECPROT_REGS 16U ++#define IDS_PER_DECPROT_LOCK_REGS 32U ++ ++#define PAGE_SIZE BIT(12) /* 4KB page size */ ++ ++/* ++ * Implementation uses uint8_t to store each securable DECPROT configuration ++ * and uint16_t to store each securable TZMA configuration. When resuming ++ * from deep suspend, the DECPROT configurations are restored. ++ */ ++#define PERIPH_PM_LOCK_BIT BIT(7) ++#define PERIPH_PM_ATTR_MASK GENMASK_32(2, 0) ++#define TZMA_PM_LOCK_BIT BIT(15) ++#define TZMA_PM_VALUE_MASK GENMASK_32(9, 0) ++ ++/* ++ * @num_tzma - number of TZMA zones, read from the hardware ++ * @num_per_sec - number of securable AHB & APB periphs, read from the hardware ++ * @num_ahb_sec - number of securable AHB master zones, read from the hardware ++ */ ++struct stm32_etzpc_driver_data { ++ unsigned int num_tzma; ++ unsigned int num_per_sec; ++ unsigned int num_ahb_sec; ++}; ++ ++struct etzpc_device { ++ struct stm32_etzpc_platdata pdata; ++ struct stm32_etzpc_driver_data *ddata; ++ struct stm32_firewall_device *fdev; ++ unsigned int lock; ++}; ++ ++static uint32_t etzpc_lock(struct etzpc_device *dev) ++{ ++ return may_spin_lock(&dev->lock); ++} ++ ++static void etzpc_unlock(struct etzpc_device *dev, uint32_t exceptions) ++{ ++ may_spin_unlock(&dev->lock, exceptions); ++} ++ ++static bool __maybe_unused valid_decprot_id(struct etzpc_device *etzpc_dev, ++ unsigned int id) ++{ ++ return id < etzpc_dev->ddata->num_per_sec; ++} ++ ++static bool __maybe_unused valid_tzma_id(struct etzpc_device *etzpc_dev, ++ unsigned int id) ++{ ++ return id < etzpc_dev->ddata->num_tzma; ++} ++ ++static enum etzpc_decprot_attributes etzpc_binding2decprot(uint32_t mode) ++{ ++ switch (mode) { ++ case DECPROT_S_RW: ++ return ETZPC_DECPROT_S_RW; ++ case DECPROT_NS_R_S_W: ++ return ETZPC_DECPROT_NS_R_S_W; ++#ifdef CFG_STM32MP15 ++ case DECPROT_MCU_ISOLATION: ++ return ETZPC_DECPROT_MCU_ISOLATION; ++#endif ++ case DECPROT_NS_RW: ++ return ETZPC_DECPROT_NS_RW; ++ default: ++ panic(); ++ } ++} ++ ++static void etzpc_configure_decprot(struct etzpc_device *etzpc_dev, ++ uint32_t decprot_id, ++ enum etzpc_decprot_attributes decprot_attr) ++{ ++ size_t offset = 4U * (decprot_id / IDS_PER_DECPROT_REGS); ++ uint32_t shift = (decprot_id % IDS_PER_DECPROT_REGS) << DECPROT_SHIFT; ++ uint32_t masked_decprot = (uint32_t)decprot_attr & ETZPC_DECPROT0_MASK; ++ vaddr_t base = etzpc_dev->pdata.base; ++ unsigned int exceptions = 0; ++ ++ assert(valid_decprot_id(etzpc_dev, decprot_id)); ++ ++ FMSG("ID : %"PRIu32", CONF %d", decprot_id, decprot_attr); ++ ++ exceptions = etzpc_lock(etzpc_dev); ++ ++ io_clrsetbits32(base + ETZPC_DECPROT0 + offset, ++ ETZPC_DECPROT0_MASK << shift, ++ masked_decprot << shift); ++ ++ etzpc_unlock(etzpc_dev, exceptions); ++} ++ ++static enum etzpc_decprot_attributes ++etzpc_get_decprot(struct etzpc_device *etzpc_dev, uint32_t decprot_id) ++{ ++ size_t offset = 4U * (decprot_id / IDS_PER_DECPROT_REGS); ++ uint32_t shift = (decprot_id % IDS_PER_DECPROT_REGS) << DECPROT_SHIFT; ++ vaddr_t base = etzpc_dev->pdata.base; ++ uint32_t value = 0; ++ ++ assert(valid_decprot_id(etzpc_dev, decprot_id)); ++ ++ value = (io_read32(base + ETZPC_DECPROT0 + offset) >> shift) & ++ ETZPC_DECPROT0_MASK; ++ ++ return (enum etzpc_decprot_attributes)value; ++} ++ ++static void etzpc_lock_decprot(struct etzpc_device *etzpc_dev, ++ uint32_t decprot_id) ++{ ++ size_t offset = 4U * (decprot_id / IDS_PER_DECPROT_LOCK_REGS); ++ uint32_t mask = BIT(decprot_id % IDS_PER_DECPROT_LOCK_REGS); ++ vaddr_t base = etzpc_dev->pdata.base; ++ uint32_t exceptions = 0; ++ ++ assert(valid_decprot_id(etzpc_dev, decprot_id)); ++ ++ exceptions = etzpc_lock(etzpc_dev); ++ ++ io_write32(base + offset + ETZPC_DECPROT_LOCK0, mask); ++ ++ etzpc_unlock(etzpc_dev, exceptions); ++} ++ ++static bool is_decprot_locked(struct etzpc_device *etzpc_dev, ++ uint32_t decprot_id) ++{ ++ size_t offset = 4U * (decprot_id / IDS_PER_DECPROT_LOCK_REGS); ++ uint32_t mask = BIT(decprot_id % IDS_PER_DECPROT_LOCK_REGS); ++ vaddr_t base = etzpc_dev->pdata.base; ++ ++ assert(valid_decprot_id(etzpc_dev, decprot_id)); ++ ++ return io_read32(base + offset + ETZPC_DECPROT_LOCK0) & mask; ++} ++ ++static void etzpc_configure_tzma(struct etzpc_device *etzpc_dev, ++ uint32_t tzma_id, uint16_t tzma_value) ++{ ++ size_t offset = sizeof(uint32_t) * tzma_id; ++ vaddr_t base = etzpc_dev->pdata.base; ++ uint32_t exceptions = 0; ++ ++ assert(valid_tzma_id(etzpc_dev, tzma_id)); ++ ++ exceptions = etzpc_lock(etzpc_dev); ++ ++ io_write32(base + ETZPC_TZMA0_SIZE + offset, tzma_value); ++ ++ etzpc_unlock(etzpc_dev, exceptions); ++} ++ ++static uint16_t etzpc_get_tzma(struct etzpc_device *etzpc_dev, ++ uint32_t tzma_id) ++{ ++ size_t offset = sizeof(uint32_t) * tzma_id; ++ vaddr_t base = etzpc_dev->pdata.base; ++ ++ assert(valid_tzma_id(etzpc_dev, tzma_id)); ++ ++ return io_read32(base + ETZPC_TZMA0_SIZE + offset); ++} ++ ++static void etzpc_lock_tzma(struct etzpc_device *etzpc_dev, uint32_t tzma_id) ++{ ++ size_t offset = sizeof(uint32_t) * tzma_id; ++ vaddr_t base = etzpc_dev->pdata.base; ++ uint32_t exceptions = 0; ++ ++ assert(valid_tzma_id(etzpc_dev, tzma_id)); ++ ++ exceptions = etzpc_lock(etzpc_dev); ++ ++ io_setbits32(base + ETZPC_TZMA0_SIZE + offset, ETZPC_TZMA0_SIZE_LOCK); ++ ++ etzpc_unlock(etzpc_dev, exceptions); ++} ++ ++static bool is_tzma_locked(struct etzpc_device *etzpc_dev, uint32_t tzma_id) ++{ ++ size_t offset = sizeof(uint32_t) * tzma_id; ++ vaddr_t base = etzpc_dev->pdata.base; ++ ++ assert(valid_tzma_id(etzpc_dev, tzma_id)); ++ ++ return io_read32(base + ETZPC_TZMA0_SIZE + offset) & ++ ETZPC_TZMA0_SIZE_LOCK; ++} ++ ++static TEE_Result etzpc_pm(enum pm_op op, unsigned int pm_hint __unused, ++ const struct pm_callback_handle *pm_handle) ++{ ++ struct stm32_firewall_device *fdev = ++ (struct stm32_firewall_device *)pm_handle->handle; ++ struct etzpc_device *etzpc_dev = stm32_firewall_priv(fdev); ++ struct stm32_etzpc_driver_data *ddata = etzpc_dev->ddata; ++ struct stm32_etzpc_platdata *pdata = &etzpc_dev->pdata; ++ unsigned int n = 0; ++ ++ if (op == PM_OP_SUSPEND) { ++ for (n = 0; n < ddata->num_per_sec; n++) { ++ pdata->periph_cfg[n] = ++ (uint8_t)etzpc_get_decprot(etzpc_dev, n); ++ if (is_decprot_locked(etzpc_dev, n)) ++ pdata->periph_cfg[n] |= PERIPH_PM_LOCK_BIT; ++ } ++ ++ for (n = 0; n < ddata->num_tzma; n++) { ++ pdata->tzma_cfg[n] = ++ (uint8_t)etzpc_get_tzma(etzpc_dev, n); ++ if (is_tzma_locked(etzpc_dev, n)) ++ pdata->tzma_cfg[n] |= TZMA_PM_LOCK_BIT; ++ } ++ ++ return TEE_SUCCESS; ++ } ++ ++ /* PM_OP_RESUME */ ++ for (n = 0; n < ddata->num_per_sec; n++) { ++ unsigned int attr = pdata->periph_cfg[n] & PERIPH_PM_ATTR_MASK; ++ ++ etzpc_configure_decprot(etzpc_dev, n, ++ (enum etzpc_decprot_attributes)attr); ++ ++ if (pdata->periph_cfg[n] & PERIPH_PM_LOCK_BIT) ++ etzpc_lock_decprot(etzpc_dev, n); ++ } ++ ++ for (n = 0; n < ddata->num_tzma; n++) { ++ uint16_t value = pdata->tzma_cfg[n] & TZMA_PM_VALUE_MASK; ++ ++ etzpc_configure_tzma(etzpc_dev, n, value); ++ ++ if (pdata->tzma_cfg[n] & TZMA_PM_LOCK_BIT) ++ etzpc_lock_tzma(etzpc_dev, n); ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++DECLARE_KEEP_PAGER(etzpc_pm); ++ ++static TEE_Result etzpc_mode_from_cfg(const struct stm32_firewall_cfg *cfg, ++ enum etzpc_decprot_attributes *attr, ++ bool *lock) ++{ ++ const struct stm32_firewall_cfg *config = cfg; ++ ++ assert(cfg && attr); ++ ++ *attr = ETZPC_DECPROT_MAX; ++ ++ while (config->access) { ++ uint32_t mode = 0; ++ ++ mode = config->access & FWLL_CONF_MASK; ++ if (mode == FWLL_NO_ACCESS) { ++ config++; ++ continue; ++ } ++ ++ if (lock && (config->access & FWLL_CONF_LOCK)) ++ *lock |= true; ++ ++ switch (config->access & FWLL_MASTER_MASK) { ++ case FWLL_MASTER(0): ++ if (mode & FWLL_SEC_RW) ++ *attr = ETZPC_DECPROT_S_RW; ++ ++ if (mode & FWLL_NSEC_READ) ++ *attr = ETZPC_DECPROT_NS_R_S_W; ++ ++ if (mode & FWLL_NSEC_WRITE) ++ *attr = ETZPC_DECPROT_NS_RW; ++ ++ break; ++#ifdef CFG_STM32MP15 ++ case FWLL_MASTER(1): ++ /* No other configuration set */ ++ if (mode & FWLL_FULL_ACCESS && ++ *attr == ETZPC_DECPROT_MAX) ++ *attr = ETZPC_DECPROT_MCU_ISOLATION; ++ ++ break; ++#endif ++ default: ++ return TEE_ERROR_BAD_PARAMETERS; ++ } ++ ++ config++; ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result stm32_etzpc_has_access(struct stm32_firewall_device *fdev, ++ unsigned int id, ++ paddr_t base, size_t sz, ++ const struct stm32_firewall_cfg *cfg) ++{ ++ struct etzpc_device *etzpc_dev = stm32_firewall_priv(fdev); ++ enum etzpc_decprot_attributes attr_req = ETZPC_DECPROT_MAX; ++ enum etzpc_decprot_attributes attr = ETZPC_DECPROT_MAX; ++ uint16_t size = 0; ++ uint32_t tzma_id = 0; ++ TEE_Result res = TEE_ERROR_GENERIC; ++ ++ assert(cfg); ++ ++ res = etzpc_mode_from_cfg(cfg, &attr_req, NULL); ++ if (res || attr_req == ETZPC_DECPROT_MAX) ++ return TEE_ERROR_SECURITY; ++ ++ if (id < etzpc_dev->ddata->num_per_sec) { ++ attr = etzpc_get_decprot(etzpc_dev, id); ++ FMSG("Check Access %u - attr %d - requested %d", id, ++ attr, attr_req); ++ if (attr == attr_req) ++ return TEE_SUCCESS; ++ else ++ return TEE_ERROR_ACCESS_DENIED; ++ } ++ ++ /* TZMA */ ++ tzma_id = (id - etzpc_dev->ddata->num_per_sec); ++ size = etzpc_get_tzma(etzpc_dev, tzma_id) * PAGE_SIZE; ++ if (base < fdev->compat->reg[id].addr + size && ++ base + sz - 1 < fdev->compat->reg[id].addr + size && ++ attr_req == ETZPC_DECPROT_S_RW) ++ return TEE_SUCCESS; ++ ++ if (base >= fdev->compat->reg[id].addr + size && ++ base + sz - 1 < fdev->compat->reg[id].addr + ++ fdev->compat->reg[id].size && ++ attr_req == ETZPC_DECPROT_NS_RW) ++ return TEE_SUCCESS; ++ ++ return TEE_ERROR_ACCESS_DENIED; ++} ++ ++static TEE_Result stm32_etzpc_configure(struct stm32_firewall_device *fdev, ++ unsigned int id, ++ paddr_t base, size_t sz, ++ const struct stm32_firewall_cfg *cfg) ++{ ++ struct etzpc_device *etzpc_dev = stm32_firewall_priv(fdev); ++ enum etzpc_decprot_attributes attr = ETZPC_DECPROT_MAX; ++ TEE_Result res = TEE_ERROR_GENERIC; ++ unsigned int total_sz = 0; ++ uint32_t tzma_id = 0; ++ bool lock = false; ++ ++ assert(cfg); ++ ++ res = etzpc_mode_from_cfg(cfg, &attr, &lock); ++ if (res || attr == ETZPC_DECPROT_MAX) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ if (id < etzpc_dev->ddata->num_per_sec) { ++ if (is_decprot_locked(etzpc_dev, id)) ++ return TEE_ERROR_ACCESS_DENIED; ++ ++ etzpc_configure_decprot(etzpc_dev, id, attr); ++ if (lock) ++ etzpc_lock_decprot(etzpc_dev, id); ++ ++ return TEE_SUCCESS; ++ } ++ ++ /* TZMA */ ++ if (base + sz > fdev->compat->reg[id].addr + fdev->compat->reg[id].size) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ switch (attr) { ++ case ETZPC_DECPROT_S_RW: ++ total_sz = base + sz - fdev->compat->reg[id].addr; ++ break; ++ case ETZPC_DECPROT_NS_RW: ++ total_sz = base - fdev->compat->reg[id].addr; ++ break; ++ default: ++ return TEE_ERROR_BAD_PARAMETERS; ++ } ++ ++ total_sz = ROUNDUP_DIV(total_sz, PAGE_SIZE); ++ tzma_id = id - etzpc_dev->ddata->num_per_sec; ++ etzpc_configure_tzma(etzpc_dev, tzma_id, total_sz); ++ if (lock) ++ etzpc_lock_tzma(etzpc_dev, tzma_id); ++ ++ return TEE_SUCCESS; ++} ++ ++static const struct stm32_firewall_ops stm32_etzpc_fw_ops = { ++ .has_access = stm32_etzpc_has_access, ++ .configure_access = stm32_etzpc_configure, ++}; ++ ++static struct etzpc_device *stm32_etzpc_alloc(void) ++{ ++ struct etzpc_device *etzpc_dev = calloc(1, sizeof(*etzpc_dev)); ++ struct stm32_etzpc_driver_data *ddata = calloc(1, sizeof(*ddata)); ++ ++ if (etzpc_dev && ddata) { ++ etzpc_dev->ddata = ddata; ++ return etzpc_dev; ++ } ++ ++ if (ddata) ++ free(ddata); ++ ++ if (etzpc_dev) ++ free(etzpc_dev); ++ ++ etzpc_dev->lock = SPINLOCK_UNLOCK; ++ ++ return NULL; ++} ++ ++/* Informative unused function */ ++static __unused void stm32_etzpc_free(struct etzpc_device *etzpc_dev) ++{ ++ if (etzpc_dev->fdev) ++ stm32_firewall_dev_free(etzpc_dev->fdev); ++ ++ if (etzpc_dev) { ++ free(etzpc_dev->ddata); ++ free(etzpc_dev); ++ } ++} ++ ++static void stm32_etzpc_set_driverdata(struct etzpc_device *dev) ++{ ++ struct stm32_etzpc_driver_data *ddata = dev->ddata; ++ uintptr_t base = dev->pdata.base; ++ uint32_t reg = io_read32(base + ETZPC_HWCFGR); ++ ++ ddata->num_tzma = (reg & ETZPC_HWCFGR_NUM_TZMA_MASK) >> ++ ETZPC_HWCFGR_NUM_TZMA_SHIFT; ++ ddata->num_per_sec = (reg & ETZPC_HWCFGR_NUM_PER_SEC_MASK) >> ++ ETZPC_HWCFGR_NUM_PER_SEC_SHIFT; ++ ddata->num_ahb_sec = (reg & ETZPC_HWCFGR_NUM_AHB_SEC_MASK) >> ++ ETZPC_HWCFGR_NUM_AHB_SEC_SHIFT; ++ ++ DMSG("ETZPC revision 0x%02" PRIu8 ", per_sec %u, ahb_sec %u, tzma %u", ++ io_read8(base + ETZPC_VERR), ++ ddata->num_per_sec, ddata->num_ahb_sec, ddata->num_tzma); ++} ++ ++#ifdef CFG_EMBED_DTB ++struct dt_id_attr { ++ /* The effective size of the array is meaningless here */ ++ fdt32_t id_attr[1]; ++}; ++ ++static void fdt_etzpc_conf_decprot(struct etzpc_device *dev, ++ const void *fdt, int node) ++{ ++ const struct dt_id_attr *conf_list = NULL; ++ size_t i = 0; ++ int len = 0; ++ ++ conf_list = (const struct dt_id_attr *)fdt_getprop(fdt, node, ++ "st,decprot", &len); ++ if (!conf_list) { ++ DMSG("No ETZPC DECPROT configuration in DT"); ++ return; ++ } ++ ++ clk_enable(dev->pdata.clk); ++ ++ for (i = 0; i < len / sizeof(uint32_t); i++) { ++ uint32_t value = fdt32_to_cpu(conf_list->id_attr[i]); ++ uint32_t id = (value >> ETZPC_ID_SHIFT) & ETZPC_ID_MASK; ++ uint32_t mode = (value >> ETZPC_MODE_SHIFT) & ETZPC_MODE_MASK; ++ bool lock = value & ETZPC_LOCK_MASK; ++ enum etzpc_decprot_attributes attr = ETZPC_DECPROT_MAX; ++ ++ if (!valid_decprot_id(dev, id)) { ++ DMSG("Invalid DECPROT %"PRIu32, id); ++ panic(); ++ } ++ ++ attr = etzpc_binding2decprot(mode); ++ etzpc_configure_decprot(dev, id, attr); ++ ++ if (lock) ++ etzpc_lock_decprot(dev, id); ++ } ++ ++ clk_disable(dev->pdata.clk); ++} ++ ++static TEE_Result init_etzpc_from_dt(struct etzpc_device *etzpc_dev, ++ const void *fdt, int node) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ struct dt_node_info etzpc_info = { }; ++ struct io_pa_va base = { }; ++ int len = 0; ++ ++ _fdt_fill_device_info(fdt, &etzpc_info, node); ++ if (etzpc_info.reg == DT_INFO_INVALID_REG || ++ etzpc_info.reg_size == DT_INFO_INVALID_REG_SIZE || ++ etzpc_info.clock == DT_INFO_INVALID_CLOCK) { ++ return TEE_ERROR_ITEM_NOT_FOUND; ++ } ++ ++ base.pa = etzpc_info.reg; ++ etzpc_dev->pdata.name = fdt_get_name(fdt, node, &len); ++ etzpc_dev->pdata.base = io_pa_or_va_secure(&base, etzpc_info.reg_size); ++ res = clk_dt_get_by_index(fdt, node, 0, &etzpc_dev->pdata.clk); ++ if (res) ++ return res; ++ ++ stm32_etzpc_set_driverdata(etzpc_dev); ++ ++ etzpc_dev->pdata.periph_cfg = ++ calloc(etzpc_dev->ddata->num_per_sec, ++ sizeof(etzpc_dev->pdata.periph_cfg)); ++ ++ etzpc_dev->pdata.tzma_cfg = ++ calloc(etzpc_dev->ddata->num_tzma, ++ sizeof(etzpc_dev->pdata.tzma_cfg)); ++ if (!etzpc_dev->pdata.periph_cfg || !etzpc_dev->pdata.tzma_cfg) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ fdt_etzpc_conf_decprot(etzpc_dev, fdt, node); ++ ++ return TEE_SUCCESS; ++} ++#else ++static TEE_Result init_etzpc_from_dt(struct etzpc_device *etzpc_dev __unsued, ++ const void *fdt __unsued, ++ int node __unsued, ++ const struct dt_device_match *dm __unsued) ++{ ++ return TEE_ERROR_NOT_SUPPORTED; ++} ++#endif ++ ++static TEE_Result stm32_etzpc_probe(const void *fdt, int node, ++ const void *compat_data) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ struct etzpc_device *etzpc_dev = stm32_etzpc_alloc(); ++ ++ if (!etzpc_dev) { ++ res = TEE_ERROR_OUT_OF_MEMORY; ++ goto err; ++ } ++ ++ res = init_etzpc_from_dt(etzpc_dev, fdt, node); ++ if (res) ++ goto err; ++ ++ etzpc_dev->fdev = stm32_firewall_dev_alloc(); ++ if (!etzpc_dev->fdev) { ++ res = TEE_ERROR_OUT_OF_MEMORY; ++ goto err; ++ } ++ ++ etzpc_dev->fdev->name = etzpc_dev->pdata.name; ++ etzpc_dev->fdev->ops = &stm32_etzpc_fw_ops; ++ etzpc_dev->fdev->compat = (struct stm32_firewall_compat *)compat_data; ++ etzpc_dev->fdev->priv = etzpc_dev; ++ ++ res = stm32_firewall_dev_register(etzpc_dev->fdev); ++ if (res) ++ goto err; ++ ++ stm32_firewall_bus_probe(etzpc_dev->fdev, fdt, node); ++ ++ register_pm_core_service_cb(etzpc_pm, etzpc_dev->fdev, "stm32-etzpc"); ++ ++ return TEE_SUCCESS; ++ ++err: ++ EMSG("ETZPC probe failed: %#"PRIx32, res); ++ panic(); ++} ++ ++#ifdef CFG_EMBED_DTB ++#ifdef CFG_STM32MP13 ++static struct stm32_firewall_reg ++etzpc_compat_reg_mp13[STM32MP1_ETZPC_MAX_ID + 2] = { ++ [STM32MP1_ETZPC_VREFBUF_ID] = { 0x50025000 }, ++ [STM32MP1_ETZPC_LPTIM2_ID] = { 0x50021000 }, ++ [STM32MP1_ETZPC_LPTIM3_ID] = { 0x50022000 }, ++ [STM32MP1_ETZPC_LTDC_ID] = { 0x5A001000 }, ++ [STM32MP1_ETZPC_DCMIPP_ID] = { 0x5A000000 }, ++ [STM32MP1_ETZPC_USBPHYCTRL_ID] = { 0x5A006000 }, ++ [STM32MP1_ETZPC_DDRCTRLPHY_ID] = { 0x5A003000 }, ++ [STM32MP1_ETZPC_IWDG1_ID] = { 0x5C003000 }, ++ [STM32MP1_ETZPC_STGENC_ID] = { 0x5C008000 }, ++ [STM32MP1_ETZPC_USART1_ID] = { 0x4C000000 }, ++ [STM32MP1_ETZPC_USART2_ID] = { 0x4C001000 }, ++ [STM32MP1_ETZPC_SPI4_ID] = { 0x4C002000 }, ++ [STM32MP1_ETZPC_SPI5_ID] = { 0x4C003000 }, ++ [STM32MP1_ETZPC_I2C3_ID] = { 0x4C004000 }, ++ [STM32MP1_ETZPC_I2C4_ID] = { 0x4C005000 }, ++ [STM32MP1_ETZPC_I2C5_ID] = { 0x4C006000 }, ++ [STM32MP1_ETZPC_TIM12_ID] = { 0x4C007000 }, ++ [STM32MP1_ETZPC_TIM13_ID] = { 0x4C008000 }, ++ [STM32MP1_ETZPC_TIM14_ID] = { 0x4C009000 }, ++ [STM32MP1_ETZPC_TIM15_ID] = { 0x4C00A000 }, ++ [STM32MP1_ETZPC_TIM16_ID] = { 0x4C00B000 }, ++ [STM32MP1_ETZPC_TIM17_ID] = { 0x4C00C000 }, ++ [STM32MP1_ETZPC_ADC1_ID] = { 0x48003000 }, ++ [STM32MP1_ETZPC_ADC2_ID] = { 0x48004000 }, ++ [STM32MP1_ETZPC_OTG_ID] = { 0x49000000 }, ++ [STM32MP1_ETZPC_TSC_ID] = { 0x5000B000 }, ++ [STM32MP1_ETZPC_RNG_ID] = { 0x54004000 }, ++ [STM32MP1_ETZPC_HASH_ID] = { 0x54003000 }, ++ [STM32MP1_ETZPC_CRYP_ID] = { 0x54002000 }, ++ [STM32MP1_ETZPC_SAES_ID] = { 0x54005000 }, ++ [STM32MP1_ETZPC_PKA_ID] = { 0x54006000 }, ++ [STM32MP1_ETZPC_BKPSRAM_ID] = { 0x54000000, 0x4000 }, ++ [STM32MP1_ETZPC_ETH1_ID] = { 0x5800A000 }, ++ [STM32MP1_ETZPC_ETH2_ID] = { 0x5800E000 }, ++ [STM32MP1_ETZPC_SDMMC1_ID] = { 0x58005000 }, ++ [STM32MP1_ETZPC_SDMMC2_ID] = { 0x58007000 }, ++ [STM32MP1_ETZPC_MCE_ID] = { 0x58001000 }, ++ [STM32MP1_ETZPC_FMC_ID] = { 0x58002000 }, ++ [STM32MP1_ETZPC_QSPI_ID] = { 0x58003000 }, ++ [STM32MP1_ETZPC_SRAM1_ID] = { 0x30000000, 0x4000 }, ++ [STM32MP1_ETZPC_SRAM2_ID] = { 0x30004000, 0x2000 }, ++ [STM32MP1_ETZPC_SRAM3_ID] = { 0x30006000, 0x2000 }, ++ /* MAX ID to catch the TZMA address */ ++ [STM32MP1_ETZPC_MAX_ID] = { 0x00000000, 0x20000}, ++ [STM32MP1_ETZPC_MAX_ID + 1] = { 0x2FFE0000, 0x20000}, ++}; ++ ++static const struct stm32_firewall_compat etzpc_compat[] = { ++ { ++ .reg = etzpc_compat_reg_mp13, ++ .compat_size = ARRAY_SIZE(etzpc_compat_reg_mp13) ++ }, ++}; ++#endif ++ ++#ifdef CFG_STM32MP15 ++static struct stm32_firewall_reg ++etzpc_compat_reg_mp15[STM32MP1_ETZPC_MAX_ID + 2] = { ++ [STM32MP1_ETZPC_STGENC_ID] = { 0x5C008000 }, ++ [STM32MP1_ETZPC_BKPSRAM_ID] = { 0x54000000, 0x1000 }, ++ [STM32MP1_ETZPC_IWDG1_ID] = { 0x5C003000 }, ++ [STM32MP1_ETZPC_USART1_ID] = { 0x5C000000 }, ++ [STM32MP1_ETZPC_SPI6_ID] = { 0x5C001000 }, ++ [STM32MP1_ETZPC_I2C4_ID] = { 0x5C002000 }, ++ [STM32MP1_ETZPC_RNG1_ID] = { 0x54003000 }, ++ [STM32MP1_ETZPC_HASH1_ID] = { 0x54002000 }, ++ [STM32MP1_ETZPC_CRYP1_ID] = { 0x54001000 }, ++ [STM32MP1_ETZPC_DDRCTRL_ID] = { 0x5A003000 }, ++ [STM32MP1_ETZPC_DDRPHYC_ID] = { 0x5A004000 }, ++ [STM32MP1_ETZPC_I2C6_ID] = { 0x5C009000 }, ++ [STM32MP1_ETZPC_TIM2_ID] = { 0x40000000 }, ++ [STM32MP1_ETZPC_TIM3_ID] = { 0x40001000 }, ++ [STM32MP1_ETZPC_TIM4_ID] = { 0x40002000 }, ++ [STM32MP1_ETZPC_TIM5_ID] = { 0x40003000 }, ++ [STM32MP1_ETZPC_TIM6_ID] = { 0x40004000 }, ++ [STM32MP1_ETZPC_TIM7_ID] = { 0x40005000 }, ++ [STM32MP1_ETZPC_TIM12_ID] = { 0x40006000 }, ++ [STM32MP1_ETZPC_TIM13_ID] = { 0x40007000 }, ++ [STM32MP1_ETZPC_TIM14_ID] = { 0x40008000 }, ++ [STM32MP1_ETZPC_LPTIM1_ID] = { 0x40009000 }, ++ [STM32MP1_ETZPC_WWDG1_ID] = { 0x4000A000 }, ++ [STM32MP1_ETZPC_SPI2_ID] = { 0x4000B000 }, ++ [STM32MP1_ETZPC_SPI3_ID] = { 0x4000C000 }, ++ [STM32MP1_ETZPC_SPDIFRX_ID] = { 0x4000D000 }, ++ [STM32MP1_ETZPC_USART2_ID] = { 0x4000E000 }, ++ [STM32MP1_ETZPC_USART3_ID] = { 0x4000F000 }, ++ [STM32MP1_ETZPC_UART4_ID] = { 0x40010000 }, ++ [STM32MP1_ETZPC_UART5_ID] = { 0x40011000 }, ++ [STM32MP1_ETZPC_I2C1_ID] = { 0x40012000 }, ++ [STM32MP1_ETZPC_I2C2_ID] = { 0x40013000 }, ++ [STM32MP1_ETZPC_I2C3_ID] = { 0x40014000 }, ++ [STM32MP1_ETZPC_I2C5_ID] = { 0x40015000 }, ++ [STM32MP1_ETZPC_CEC_ID] = { 0x40016000 }, ++ [STM32MP1_ETZPC_DAC_ID] = { 0x40017000 }, ++ [STM32MP1_ETZPC_UART7_ID] = { 0x40018000 }, ++ [STM32MP1_ETZPC_UART8_ID] = { 0x40019000 }, ++ [STM32MP1_ETZPC_MDIOS_ID] = { 0x4001C000 }, ++ [STM32MP1_ETZPC_TIM1_ID] = { 0x44000000 }, ++ [STM32MP1_ETZPC_TIM8_ID] = { 0x44001000 }, ++ [STM32MP1_ETZPC_USART6_ID] = { 0x44003000 }, ++ [STM32MP1_ETZPC_SPI1_ID] = { 0x44004000 }, ++ [STM32MP1_ETZPC_SPI4_ID] = { 0x44005000 }, ++ [STM32MP1_ETZPC_TIM15_ID] = { 0x44006000 }, ++ [STM32MP1_ETZPC_TIM16_ID] = { 0x44007000 }, ++ [STM32MP1_ETZPC_TIM17_ID] = { 0x44008000 }, ++ [STM32MP1_ETZPC_SPI5_ID] = { 0x44009000 }, ++ [STM32MP1_ETZPC_SAI1_ID] = { 0x4400A000 }, ++ [STM32MP1_ETZPC_SAI2_ID] = { 0x4400B000 }, ++ [STM32MP1_ETZPC_SAI3_ID] = { 0x4400C000 }, ++ [STM32MP1_ETZPC_DFSDM_ID] = { 0x4400D000 }, ++ [STM32MP1_ETZPC_TT_FDCAN_ID] = { 0x4400E000 }, ++ [STM32MP1_ETZPC_LPTIM2_ID] = { 0x50021000 }, ++ [STM32MP1_ETZPC_LPTIM3_ID] = { 0x50022000 }, ++ [STM32MP1_ETZPC_LPTIM4_ID] = { 0x50023000 }, ++ [STM32MP1_ETZPC_LPTIM5_ID] = { 0x50024000 }, ++ [STM32MP1_ETZPC_SAI4_ID] = { 0x50027000 }, ++ [STM32MP1_ETZPC_VREFBUF_ID] = { 0x50025000 }, ++ [STM32MP1_ETZPC_DCMI_ID] = { 0x4C006000 }, ++ [STM32MP1_ETZPC_CRC2_ID] = { 0x4C004000 }, ++ [STM32MP1_ETZPC_ADC_ID] = { 0x48003000 }, ++ [STM32MP1_ETZPC_HASH2_ID] = { 0x4C002000 }, ++ [STM32MP1_ETZPC_RNG2_ID] = { 0x4C003000 }, ++ [STM32MP1_ETZPC_CRYP2_ID] = { 0x4C005000 }, ++ [STM32MP1_ETZPC_SRAM1_ID] = { 0x10000000, 0x20000 }, ++ [STM32MP1_ETZPC_SRAM2_ID] = { 0x10020000, 0x20000 }, ++ [STM32MP1_ETZPC_SRAM3_ID] = { 0x10040000, 0x10000 }, ++ [STM32MP1_ETZPC_SRAM4_ID] = { 0x10050000, 0x10000 }, ++ [STM32MP1_ETZPC_RETRAM_ID] = { 0x38000000, 0x10000 }, ++ [STM32MP1_ETZPC_OTG_ID] = { 0x49000000 }, ++ [STM32MP1_ETZPC_SDMMC3_ID] = { 0x48004000 }, ++ [STM32MP1_ETZPC_DLYBSD3_ID] = { 0x48005000 }, ++ [STM32MP1_ETZPC_DMA1_ID] = { 0x48000000 }, ++ [STM32MP1_ETZPC_DMA2_ID] = { 0x48001000 }, ++ [STM32MP1_ETZPC_DMAMUX_ID] = { 0x48002000 }, ++ [STM32MP1_ETZPC_FMC_ID] = { 0x58002000 }, ++ [STM32MP1_ETZPC_QSPI_ID] = { 0x58003000 }, ++ [STM32MP1_ETZPC_DLYBQ_ID] = { 0x58004000 }, ++ [STM32MP1_ETZPC_ETH_ID] = { 0x5800A000 }, ++ /* MAX ID to catch the TZMA address */ ++ [STM32MP1_ETZPC_MAX_ID] = { 0x00000000, 0x20000}, ++ [STM32MP1_ETZPC_MAX_ID + 1] = { 0x2FFC0000, 0x40000}, ++}; ++ ++static const struct stm32_firewall_compat etzpc_compat[] = { ++ { ++ .reg = etzpc_compat_reg_mp15, ++ .compat_size = ARRAY_SIZE(etzpc_compat_reg_mp15) ++ }, ++}; ++#endif ++ ++static const struct dt_device_match etzpc_match_table[] = { ++ { .compatible = "st,stm32-etzpc", .compat_data = etzpc_compat }, ++ { } ++}; ++ ++DEFINE_DT_DRIVER(etzpc_dt_driver) = { ++ .name = "stm32-etzpc", ++ .match_table = etzpc_match_table, ++ .probe = stm32_etzpc_probe, ++}; ++#endif /* CFG_EMBED_DTB */ +diff --git a/core/drivers/firewall/stm32_firewall.c b/core/drivers/firewall/stm32_firewall.c +new file mode 100644 +index 000000000..bf95295da +--- /dev/null ++++ b/core/drivers/firewall/stm32_firewall.c +@@ -0,0 +1,217 @@ ++// SPDX-License-Identifier: BSD-2-Clause ++/* ++ * Copyright (C) 2021, STMicroelectronics - All Rights Reserved ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#define MAX_COMPAT_LENGTH 30 ++ ++static LIST_HEAD(fw_dev_list_head, stm32_firewall_device) fw_dev_list = ++ LIST_HEAD_INITIALIZER(fw_dev_list_head); ++ ++static unsigned int list_lock = SPINLOCK_UNLOCK; ++ ++static const char firewall_compatible_exceptions[][MAX_COMPAT_LENGTH] = { ++ "st,stm32mp15-i2c-non-secure", ++ "st,stm32-ltdc" ++}; ++ ++static struct stm32_firewall_device *find_device(paddr_t base, size_t size, ++ unsigned int *idx) ++{ ++ struct stm32_firewall_device *fdev = NULL; ++ unsigned int i = 0; ++ ++ assert(idx); ++ ++ LIST_FOREACH(fdev, &fw_dev_list, dev_list) { ++ for (i = 0; i < fdev->compat->compat_size; i++) { ++ struct stm32_firewall_reg *reg = fdev->compat->reg + i; ++ ++ if (!reg->addr && !reg->size) ++ continue; ++ ++ if (!reg->size) { ++ if (base != reg->addr) ++ continue; ++ ++ if (size) ++ IMSG("IOMEM %#"PRIxPA": non-null size", ++ base); ++ ++ *idx = i; ++ return fdev; ++ } ++ ++ if (reg->addr && reg->size && base >= reg->addr && ++ base + size - 1 <= reg->addr + reg->size - 1) { ++ *idx = i; ++ return fdev; ++ } ++ } ++ } ++ ++ return NULL; ++} ++ ++TEE_Result stm32_firewall_check_access(paddr_t base, size_t size, ++ const struct stm32_firewall_cfg *cfg) ++{ ++ TEE_Result res = TEE_ERROR_ITEM_NOT_FOUND; ++ struct stm32_firewall_device *fdev = NULL; ++ uint32_t exceptions = 0; ++ unsigned int index = 0; ++ ++ exceptions = cpu_spin_lock_xsave(&list_lock); ++ fdev = find_device(base, size, &index); ++ cpu_spin_unlock_xrestore(&list_lock, exceptions); ++ ++ if (fdev && fdev->ops->has_access) ++ res = fdev->ops->has_access(fdev, index, base, size, cfg); ++ ++ return res; ++} ++ ++TEE_Result stm32_firewall_set_config(paddr_t base, size_t size, ++ const struct stm32_firewall_cfg *cfg) ++{ ++ TEE_Result res = TEE_ERROR_ITEM_NOT_FOUND; ++ struct stm32_firewall_device *fdev = NULL; ++ uint32_t exceptions = 0; ++ unsigned int index = 0; ++ ++ exceptions = cpu_spin_lock_xsave(&list_lock); ++ fdev = find_device(base, size, &index); ++ cpu_spin_unlock_xrestore(&list_lock, exceptions); ++ ++ if (fdev && fdev->ops->configure_access) ++ res = fdev->ops->configure_access(fdev, index, base, size, cfg); ++ ++ return res; ++} ++ ++struct stm32_firewall_device *stm32_firewall_dev_alloc(void) ++{ ++ return calloc(1, sizeof(struct stm32_firewall_device)); ++} ++ ++void stm32_firewall_dev_free(struct stm32_firewall_device *fdev) ++{ ++ uint32_t exceptions = 0; ++ ++ assert(fdev); ++ ++ exceptions = cpu_spin_lock_xsave(&list_lock); ++ LIST_REMOVE(fdev, dev_list); ++ cpu_spin_unlock_xrestore(&list_lock, exceptions); ++ ++ free(fdev); ++} ++ ++TEE_Result stm32_firewall_dev_register(struct stm32_firewall_device *fdev) ++{ ++ uint32_t exceptions = 0; ++ ++ assert(fdev && fdev->name && fdev->ops); ++ ++ exceptions = cpu_spin_lock_xsave(&list_lock); ++ LIST_INSERT_HEAD(&fw_dev_list, fdev, dev_list); ++ cpu_spin_unlock_xrestore(&list_lock, exceptions); ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result ++stm32_firewall_bus_dt_register(struct stm32_firewall_device *fdev, ++ const void *fdt, int node) ++{ ++ const char __maybe_unused *name = NULL; ++ int idx = 0; ++ int len = 0; ++ int count = 0; ++ unsigned int id = 0; ++ const char *compat = NULL; ++ paddr_t pbase = 0; ++ ++ /* Check accesses */ ++ const struct stm32_firewall_cfg sec_cfg[] = { ++ { FWLL_SEC_RW | FWLL_MASTER(0) }, ++ { }, /* Null terminated */ ++ }; ++ ++ if (_fdt_get_status(fdt, node) == DT_STATUS_DISABLED) ++ return TEE_SUCCESS; ++ ++ count = fdt_stringlist_count(fdt, node, "compatible"); ++ if (count < 0) ++ return TEE_SUCCESS; ++ ++ pbase = _fdt_reg_base_address(fdt, node); ++ if (pbase == DT_INFO_INVALID_REG) ++ return TEE_ERROR_GENERIC; ++ ++ for (id = 0; id < fdev->compat->compat_size; id++) ++ if (fdev->compat->reg[id].addr == pbase) ++ break; ++ ++ if (id == fdev->compat->compat_size) ++ return TEE_ERROR_ITEM_NOT_FOUND; ++ ++ name = fdt_get_name(fdt, node, NULL); ++ ++ compat = fdt_getprop(fdt, node, "compatible", &len); ++ ++ if (fdev->ops->has_access(fdev, id, pbase, 0, sec_cfg)) { ++ const struct stm32_firewall_cfg non_sec_cfg[] = { ++ { FWLL_NSEC_RW | FWLL_MASTER(0) }, ++ { }, /* Null terminated */ ++ }; ++ int len_array = ARRAY_SIZE(firewall_compatible_exceptions); ++ bool exception_found = false; ++ ++ if (fdev->ops->has_access(fdev, id, pbase, 0, non_sec_cfg)) ++ return TEE_SUCCESS; ++ ++ for (int i = 0; i < len_array; i++) { ++ if (!strcmp(firewall_compatible_exceptions[i], ++ compat)) ++ exception_found = true; ++ } ++ ++ if (!exception_found) ++ return TEE_SUCCESS; ++ } ++ ++ for (idx = 0; idx < count; idx++) { ++ compat = fdt_stringlist_get(fdt, node, "compatible", idx, &len); ++ assert(compat && len > 0); ++ ++ add_probe_node_by_compat(fdt, node, compat); ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++TEE_Result stm32_firewall_bus_probe(struct stm32_firewall_device *fdev, ++ const void *fdt, int node) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ int subnode = 0; ++ ++ fdt_for_each_subnode(subnode, fdt, node) { ++ res = stm32_firewall_bus_dt_register(fdev, fdt, subnode); ++ if (res) { ++ DMSG("Failed on node %s with %#"PRIx32, ++ fdt_get_name(fdt, subnode, NULL), res); ++ panic(); ++ } ++ } ++ ++ return TEE_SUCCESS; ++} +diff --git a/core/drivers/firewall/sub.mk b/core/drivers/firewall/sub.mk +new file mode 100644 +index 000000000..5ca035b5f +--- /dev/null ++++ b/core/drivers/firewall/sub.mk +@@ -0,0 +1,2 @@ ++srcs-$(CFG_STM32_FIREWALL) += stm32_firewall.c ++srcs-$(CFG_STM32_ETZPC) += stm32_etzpc.c +diff --git a/core/drivers/frame_buffer.c b/core/drivers/frame_buffer.c +new file mode 100644 +index 000000000..77bf973b7 +--- /dev/null ++++ b/core/drivers/frame_buffer.c +@@ -0,0 +1,36 @@ ++// SPDX-License-Identifier: BSD-2-Clause ++/* ++ * Copyright (c) 2016-2021, Linaro Limited ++ */ ++ ++#include ++#include ++ ++size_t frame_buffer_get_image_size(struct frame_buffer *fb __unused, ++ size_t width, size_t height) ++{ ++ return width * height * sizeof(uint32_t); ++} ++ ++void frame_buffer_clear(struct frame_buffer *fb, uint32_t color) ++{ ++ uint32_t *base = fb->base; ++ size_t n = 0; ++ ++ for (n = 0; n < fb->width * fb->height; n++) ++ base[n] = color; ++} ++ ++void frame_buffer_set_image(struct frame_buffer *fb, size_t xpos, size_t ypos, ++ size_t width, size_t height, const void *image) ++{ ++ const uint32_t *img = image; ++ uint32_t *base = fb->base; ++ size_t x = 0; ++ size_t y = 0; ++ ++ for (y = 0; y < height && (y + ypos) < fb->height; y++) ++ for (x = 0; x < width && (x + xpos) < fb->width; x++) ++ base[x + xpos + (y + ypos) * fb->width] = ++ img[x + y * width]; ++} +diff --git a/core/drivers/gic.c b/core/drivers/gic.c +index 910c93d2d..20ab0a2bb 100644 +--- a/core/drivers/gic.c ++++ b/core/drivers/gic.c +@@ -8,14 +8,17 @@ + #include + #include + #include ++#include + #include + #include + #include + #include ++#include + #include ++#include + #include +-#include + #include ++#include + + /* Offsets from gic.gicc_base */ + #define GICC_CTLR (0x000) +@@ -36,9 +39,11 @@ + #define GICD_ICENABLER(n) (0x180 + (n) * 4) + #define GICD_ISPENDR(n) (0x200 + (n) * 4) + #define GICD_ICPENDR(n) (0x280 + (n) * 4) ++#define GICD_ISACTIVER(n) (0x300 + (n) * 4) + #define GICD_IPRIORITYR(n) (0x400 + (n) * 4) + #define GICD_ITARGETSR(n) (0x800 + (n) * 4) + #define GICD_IGROUPMODR(n) (0xd00 + (n) * 4) ++#define GICD_ICFGR(n) (0xc00 + (n) * 4) + #define GICD_SGIR (0xF00) + + #define GICD_CTLR_ENABLEGRP0 (1 << 0) +@@ -80,6 +85,25 @@ static void gic_op_raise_sgi(struct itr_chip *chip, size_t it, + static void gic_op_set_affinity(struct itr_chip *chip, size_t it, + uint8_t cpu_mask); + ++#if defined(CFG_ARM_GIC_PM) ++static void gic_pm_add_it(struct gic_data *gd, unsigned int it); ++static void gic_pm_register(struct gic_data *gd); ++#else ++static void gic_pm_add_it(struct gic_data *gd __unused, ++ unsigned int it __unused) ++{ ++} ++static void gic_pm_register(struct gic_data *gd __unused) ++{ ++} ++#endif ++ ++#if !defined(CFG_ARM_GICV3) ++static uint8_t gic_op_set_pmr(struct itr_chip *chip, uint8_t mask); ++static uint8_t gic_op_set_ipriority(struct itr_chip *chip, size_t it, ++ uint8_t mask); ++#endif ++ + static const struct itr_ops gic_ops = { + .add = gic_op_add, + .enable = gic_op_enable, +@@ -87,6 +111,10 @@ static const struct itr_ops gic_ops = { + .raise_pi = gic_op_raise_pi, + .raise_sgi = gic_op_raise_sgi, + .set_affinity = gic_op_set_affinity, ++#if !defined(CFG_ARM_GICV3) ++ .set_pmr = gic_op_set_pmr, ++ .set_ipriority = gic_op_set_ipriority, ++#endif + }; + DECLARE_KEEP_PAGER(gic_ops); + +@@ -152,10 +180,10 @@ void gic_cpu_init(struct gic_data *gd) + * allow the Non-secure world to adjust the priority mask itself + */ + #if defined(CFG_ARM_GICV3) +- write_icc_pmr(0x80); ++ write_icc_pmr(GIC_HIGHEST_NS_PRIORITY); + write_icc_igrpen1(1); + #else +- io_write32(gd->gicc_base + GICC_PMR, 0x80); ++ io_write32(gd->gicc_base + GICC_PMR, GIC_HIGHEST_NS_PRIORITY); + + /* Enable GIC */ + io_write32(gd->gicc_base + GICC_CTLR, +@@ -164,12 +192,9 @@ void gic_cpu_init(struct gic_data *gd) + #endif + } + +-void gic_init(struct gic_data *gd, vaddr_t gicc_base __maybe_unused, +- vaddr_t gicd_base) ++static void gic_setup_clear_it(struct gic_data *gd) + { +- size_t n; +- +- gic_init_base_addr(gd, gicc_base, gicd_base); ++ size_t n = 0; + + for (n = 0; n <= gd->max_it / NUM_INTS_PER_REG; n++) { + /* Disable interrupts */ +@@ -177,7 +202,14 @@ void gic_init(struct gic_data *gd, vaddr_t gicc_base __maybe_unused, + + /* Make interrupts non-pending */ + io_write32(gd->gicd_base + GICD_ICPENDR(n), 0xffffffff); ++ }; ++} + ++void gic_init_setup(struct gic_data *gd) ++{ ++ size_t n = 0; ++ ++ for (n = 0; n <= gd->max_it / NUM_INTS_PER_REG; n++) { + /* Mark interrupts non-secure */ + if (n == 0) { + /* per-CPU inerrupts config: +@@ -195,11 +227,11 @@ void gic_init(struct gic_data *gd, vaddr_t gicc_base __maybe_unused, + * allow the Non-secure world to adjust the priority mask itself + */ + #if defined(CFG_ARM_GICV3) +- write_icc_pmr(0x80); ++ write_icc_pmr(GIC_HIGHEST_NS_PRIORITY); + write_icc_igrpen1(1); + io_setbits32(gd->gicd_base + GICD_CTLR, GICD_CTLR_ENABLEGRP1S); + #else +- io_write32(gd->gicc_base + GICC_PMR, 0x80); ++ io_write32(gd->gicc_base + GICC_PMR, GIC_HIGHEST_NS_PRIORITY); + + /* Enable GIC */ + io_write32(gd->gicc_base + GICC_CTLR, GICC_CTLR_FIQEN | +@@ -249,6 +281,15 @@ void gic_init_base_addr(struct gic_data *gd, vaddr_t gicc_base __maybe_unused, + + if (IS_ENABLED(CFG_DT)) + gd->chip.dt_get_irq = gic_dt_get_irq; ++ ++ gic_pm_register(gd); ++} ++ ++void gic_init(struct gic_data *gd, vaddr_t gicc_base, vaddr_t gicd_base) ++{ ++ gic_init_base_addr(gd, gicc_base, gicd_base); ++ gic_setup_clear_it(gd); ++ gic_init_setup(gd); + } + + static void gic_it_add(struct gic_data *gd, size_t it) +@@ -273,7 +314,8 @@ static void gic_it_set_cpu_mask(struct gic_data *gd, size_t it, + { + size_t idx __maybe_unused = it / NUM_INTS_PER_REG; + uint32_t mask __maybe_unused = 1 << (it % NUM_INTS_PER_REG); +- uint32_t target, target_shift; ++ uint32_t target = 0; ++ uint32_t target_shift = 0; + vaddr_t itargetsr = gd->gicd_base + + GICD_ITARGETSR(it / NUM_TARGETS_PER_REG); + +@@ -295,15 +337,39 @@ static void gic_it_set_prio(struct gic_data *gd, size_t it, uint8_t prio) + size_t idx __maybe_unused = it / NUM_INTS_PER_REG; + uint32_t mask __maybe_unused = 1 << (it % NUM_INTS_PER_REG); + +- /* Assigned to group0 */ +- assert(!(io_read32(gd->gicd_base + GICD_IGROUPR(idx)) & mask)); +- + /* Set prio it to selected CPUs */ + DMSG("prio: writing 0x%x to 0x%" PRIxVA, + prio, gd->gicd_base + GICD_IPRIORITYR(0) + it); + io_write8(gd->gicd_base + GICD_IPRIORITYR(0) + it, prio); + } + ++static uint8_t gic_op_set_pmr(struct itr_chip *chip, uint8_t mask) ++{ ++ struct gic_data *gd = container_of(chip, struct gic_data, chip); ++ uint32_t pmr = io_read32(gd->gicc_base + GICC_PMR); ++ ++ /* ++ * Order memory updates w.r.t. PMR write, and ensure they're visible ++ * before potential out of band interrupt trigger because of PMR update. ++ */ ++ dsb_ishst(); ++ io_write32(gd->gicc_base + GICC_PMR, mask); ++ dsb_ishst(); ++ ++ return (uint8_t)pmr; ++} ++ ++static uint8_t gic_op_set_ipriority(struct itr_chip *chip, size_t it, ++ uint8_t mask) ++{ ++ struct gic_data *gd = container_of(chip, struct gic_data, chip); ++ uint8_t prio = io_read8(gd->gicd_base + GICD_IPRIORITYR(0) + it); ++ ++ gic_it_set_prio(gd, it, mask); ++ ++ return prio; ++} ++ + static void gic_it_enable(struct gic_data *gd, size_t it) + { + size_t idx = it / NUM_INTS_PER_REG; +@@ -421,8 +487,8 @@ void gic_dump_state(struct gic_data *gd) + + void gic_it_handle(struct gic_data *gd) + { +- uint32_t iar; +- uint32_t id; ++ uint32_t iar = 0; ++ uint32_t id = 0; + + iar = gic_read_iar(gd); + id = iar & GICC_IAR_IT_ID_MASK; +@@ -448,6 +514,8 @@ static void gic_op_add(struct itr_chip *chip, size_t it, + /* Set the CPU mask to deliver interrupts to any online core */ + gic_it_set_cpu_mask(gd, it, 0xff); + gic_it_set_prio(gd, it, 0x1); ++ ++ gic_pm_add_it(gd, it); + } + + static void gic_op_enable(struct itr_chip *chip, size_t it) +@@ -493,6 +561,7 @@ static void gic_op_raise_sgi(struct itr_chip *chip, size_t it, + else + gic_it_raise_sgi(gd, it, cpu_mask, 0); + } ++ + static void gic_op_set_affinity(struct itr_chip *chip, size_t it, + uint8_t cpu_mask) + { +@@ -503,3 +572,137 @@ static void gic_op_set_affinity(struct itr_chip *chip, size_t it, + + gic_it_set_cpu_mask(gd, it, cpu_mask); + } ++ ++#if defined(CFG_ARM_GIC_PM) ++/* ++ * Save/restore interrupts registered from the gic_op_add_it() handler ++ */ ++#define IT_PM_GPOUP1_BIT BIT(0) ++#define IT_PM_ENABLE_BIT BIT(1) ++#define IT_PM_PENDING_BIT BIT(2) ++#define IT_PM_ACTIVE_BIT BIT(3) ++#define IT_PM_CONFIG_MASK GENMASK_32(1, 0) ++ ++/* ++ * @it - interrupt ID/number ++ * @flags - bitflag IT_PM_*_BIT ++ * @iprio - 8bit prio from IPRIORITYR ++ * @itarget - 8bit target from ITARGETR ++ * @icfg - 2bit configuration from ICFGR and IT_PM_CONFIG_MASK ++ */ ++struct gic_it_pm { ++ uint16_t it; ++ uint8_t flags; ++ uint8_t iprio; ++ uint8_t itarget; ++ uint8_t icfg; ++}; ++ ++static void gic_pm_add_it(struct gic_data *gd, unsigned int it) ++{ ++ struct gic_pm *pm = &gd->pm; ++ ++ pm->count++; ++ pm->pm_cfg = realloc(pm->pm_cfg, pm->count * sizeof(*pm->pm_cfg)); ++ if (!pm->pm_cfg) ++ panic(); ++ ++ pm->pm_cfg[pm->count - 1] = (struct gic_it_pm){ .it = it }; ++} ++ ++static void gic_save_it(struct gic_data *gd, struct gic_it_pm *pm) ++{ ++ unsigned int it = pm->it; ++ size_t idx = 0; ++ uint32_t bit_mask = BIT(it % NUM_INTS_PER_REG); ++ uint32_t shift2 = it % (NUM_INTS_PER_REG / 2); ++ uint32_t shift8 = it % (NUM_INTS_PER_REG / 8); ++ uint32_t data32 = 0; ++ ++ pm->flags = 0; ++ idx = it / NUM_INTS_PER_REG; ++ ++ if (io_read32(gd->gicd_base + GICD_IGROUPR(idx)) & bit_mask) ++ pm->flags |= IT_PM_GPOUP1_BIT; ++ if (io_read32(gd->gicd_base + GICD_ISENABLER(idx)) & bit_mask) ++ pm->flags |= IT_PM_ENABLE_BIT; ++ if (io_read32(gd->gicd_base + GICD_ISPENDR(idx)) & bit_mask) ++ pm->flags |= IT_PM_PENDING_BIT; ++ if (io_read32(gd->gicd_base + GICD_ISACTIVER(idx)) & bit_mask) ++ pm->flags |= IT_PM_ACTIVE_BIT; ++ ++ idx = (8 * it) / NUM_INTS_PER_REG; ++ ++ data32 = io_read32(gd->gicd_base + GICD_IPRIORITYR(idx)) >> shift8; ++ pm->iprio = (uint8_t)data32; ++ ++ data32 = io_read32(gd->gicd_base + GICD_ITARGETSR(idx)) >> shift8; ++ pm->itarget = (uint8_t)data32; ++ ++ /* Note: ICFGR is RAO for SPIs and PPIs */ ++ idx = (2 * it) / NUM_INTS_PER_REG; ++ data32 = io_read32(gd->gicd_base + GICD_ICFGR(idx)) >> shift2; ++ pm->icfg = (uint8_t)data32 & IT_PM_CONFIG_MASK; ++} ++ ++static void gic_restore_it(struct gic_data *gd, struct gic_it_pm *pm) ++{ ++ unsigned int it = pm->it; ++ size_t idx = it / NUM_INTS_PER_REG; ++ uint32_t mask = BIT(it % NUM_INTS_PER_REG); ++ uint32_t shift2 = it % (NUM_INTS_PER_REG / 2); ++ uint32_t shift8 = it % (NUM_INTS_PER_REG / 8); ++ ++ io_mask32(gd->gicd_base + GICD_IGROUPR(idx), ++ (pm->flags & IT_PM_GPOUP1_BIT) ? mask : 0, mask); ++ ++ io_mask32(gd->gicd_base + GICD_ISENABLER(idx), ++ (pm->flags & IT_PM_ENABLE_BIT) ? mask : 0, mask); ++ ++ io_mask32(gd->gicd_base + GICD_ISPENDR(idx), ++ (pm->flags & IT_PM_PENDING_BIT) ? mask : 0, mask); ++ ++ io_mask32(gd->gicd_base + GICD_ISACTIVER(idx), ++ (pm->flags & IT_PM_ACTIVE_BIT) ? mask : 0, mask); ++ ++ idx = (8 * it) / NUM_INTS_PER_REG; ++ ++ io_mask32(gd->gicd_base + GICD_IPRIORITYR(idx), ++ (uint32_t)pm->iprio << shift8, UINT8_MAX << shift8); ++ ++ io_mask32(gd->gicd_base + GICD_ITARGETSR(idx), ++ (uint32_t)pm->itarget << shift8, UINT8_MAX << shift8); ++ ++ /* Note: ICFGR is WI for SPIs and PPIs */ ++ idx = (2 * it) / NUM_INTS_PER_REG; ++ io_mask32(gd->gicd_base + GICD_ICFGR(idx), ++ (uint32_t)pm->icfg << shift2, IT_PM_CONFIG_MASK << shift2); ++} ++ ++static TEE_Result gic_pm(enum pm_op op, unsigned int pm_hint __unused, ++ const struct pm_callback_handle *handle) ++{ ++ void (*sequence)(struct gic_data *gd, struct gic_it_pm *pm) = NULL; ++ struct gic_it_pm *cfg = NULL; ++ unsigned int n = 0; ++ struct gic_data *gd = (struct gic_data *)PM_CALLBACK_GET_HANDLE(handle); ++ struct gic_pm *pm = &gd->pm; ++ ++ if (op == PM_OP_SUSPEND) { ++ sequence = gic_save_it; ++ } else { ++ gic_init_setup(gd); ++ sequence = gic_restore_it; ++ } ++ for (n = 0, cfg = pm->pm_cfg; n < pm->count; n++, cfg++) ++ sequence(gd, cfg); ++ ++ return TEE_SUCCESS; ++} ++DECLARE_KEEP_PAGER(gic_pm); ++ ++static void gic_pm_register(struct gic_data *gd) ++{ ++ register_pm_core_service_cb(gic_pm, gd, "arm-gic"); ++} ++#endif /*CFG_ARM_GIC_PM*/ +diff --git a/core/drivers/regulator/core.c b/core/drivers/regulator/core.c +new file mode 100644 +index 000000000..d4dc3751d +--- /dev/null ++++ b/core/drivers/regulator/core.c +@@ -0,0 +1,1019 @@ ++// SPDX-License-Identifier: BSD-2-Clause ++/* ++ * Copyright (c) 2020-2021, STMicroelectronics - All Rights Reserved ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++static SLIST_HEAD(, rdev) regulator_device_list = SLIST_HEAD_INITIALIZER(rdev); ++ ++const char __weak *plat_get_lp_mode_name(int mode __unused) ++{ ++ return NULL; ++} ++ ++int __weak plat_get_lp_mode_count(void) ++{ ++ return 0; ++} ++ ++static void lock_driver(const struct rdev *rdev) ++{ ++ if (rdev->desc->ops->lock) ++ rdev->desc->ops->lock(rdev->desc); ++} ++ ++static void unlock_driver(const struct rdev *rdev) ++{ ++ if (rdev->desc->ops->unlock) ++ rdev->desc->ops->unlock(rdev->desc); ++} ++ ++static struct rdev *regulator_get_by_phandle(int32_t phandle) ++{ ++ struct rdev *rdev = NULL; ++ ++ SLIST_FOREACH(rdev, ®ulator_device_list, link) ++ if (rdev->phandle == phandle) ++ return rdev; ++ ++ DMSG("phandle %"PRId32" not found", phandle); ++ return NULL; ++} ++ ++/* ++ * Get a regulator from its node name ++ * ++ * @fdt - pointer to device tree memory ++ * @node_name - name of the node "ldo1" ++ * Return pointer to rdev if succeed, NULL else. ++ */ ++struct rdev *regulator_get_by_node_name(const char *node_name) ++{ ++ struct rdev *rdev = NULL; ++ ++ assert(node_name); ++ ++ SLIST_FOREACH(rdev, ®ulator_device_list, link) ++ if (!strcmp(rdev->desc->node_name, node_name)) ++ return rdev; ++ ++ EMSG("%s not found", node_name); ++ return NULL; ++} ++ ++/* ++ * Get a regulator from its regulator name property value ++ * ++ * @reg_name - target value of regulator-name property ++ * Return pointer to rdev if succeed, NULL else. ++ */ ++struct rdev *regulator_get_by_regulator_name(const char *reg_name) ++{ ++ struct rdev *rdev = NULL; ++ ++ assert(reg_name); ++ ++ SLIST_FOREACH(rdev, ®ulator_device_list, link) ++ if (rdev->reg_name && !strcmp(rdev->reg_name, reg_name)) ++ return rdev; ++ ++ EMSG("%s not found", reg_name); ++ return NULL; ++} ++ ++#ifdef CFG_EMBED_DTB ++#define MAX_PROPERTY_LEN 64 ++ ++static int32_t get_supply_phandle(const void *fdt, int node, const char *name) ++{ ++ const fdt32_t *cuint = NULL; ++ int __maybe_unused len = 0; ++ int supply_phandle = -FDT_ERR_NOTFOUND; ++ char prop_name[MAX_PROPERTY_LEN] = { 0 }; ++ ++ len = snprintf(prop_name, sizeof(prop_name) - 1, "%s-supply", name); ++ assert((len >= 0) && ((size_t)len < sizeof(prop_name) - 1)); ++ ++ cuint = fdt_getprop(fdt, node, prop_name, NULL); ++ if (cuint) { ++ supply_phandle = fdt32_to_cpu(*cuint); ++ FMSG("%s: supplied by %d", name, supply_phandle); ++ } ++ ++ return supply_phandle; ++} ++ ++/* ++ * Get a regulator from a supply name ++ * ++ * @fdt - pointer to device tree memory ++ * @node - offset of the node that contains the supply description ++ * @name - name of the supply, i.e. "vdd" for "vdd-supply" ++ * Return pointer to rdev if succeed, NULL else. ++ */ ++struct rdev *regulator_get_by_supply_name(const void *fdt, int node, ++ const char *name) ++{ ++ const int p = get_supply_phandle(fdt, node, name); ++ ++ if (p < 0) ++ return NULL; ++ ++ return regulator_get_by_phandle(p); ++} ++#endif /* CFG_EMBED_DTB */ ++ ++static TEE_Result _regulator_set_state(struct rdev *rdev, bool on_not_off) ++{ ++ if (!rdev->desc->ops->set_state) ++ return TEE_ERROR_GENERIC; ++ ++ return rdev->desc->ops->set_state(rdev->desc, on_not_off); ++} ++ ++static TEE_Result _regulator_enable(struct rdev *rdev) ++{ ++ FMSG("%s", rdev->desc->node_name); ++ ++ if (!rdev->desc->ops->set_state) ++ return TEE_ERROR_GENERIC; ++ ++ if (rdev->supply_dev) { ++ TEE_Result res = regulator_enable(rdev->supply_dev); ++ ++ if (res) ++ return res; ++ } ++ ++ lock_driver(rdev); ++ ++ if (!rdev->use_count) { ++ TEE_Result res = _regulator_set_state(rdev, true); ++ ++ if (res) { ++ EMSG("regul %s set state failed with %#"PRIx32, ++ rdev->desc->node_name, res); ++ ++ unlock_driver(rdev); ++ ++ if (rdev->supply_dev) { ++ res = regulator_disable(rdev->supply_dev); ++ if (res) ++ panic(); ++ } ++ ++ return res; ++ } ++ ++ udelay(rdev->enable_ramp_delay_us); ++ } ++ ++ rdev->use_count++; ++ assert(rdev->use_count != UINT8_MAX); ++ ++ unlock_driver(rdev); ++ ++ FMSG("%s refcount: %u", rdev->desc->node_name, rdev->use_count); ++ ++ return TEE_SUCCESS; ++} ++ ++/* ++ * Enable regulator and its supplies ++ * ++ * @rdev - regulator device ++ */ ++TEE_Result regulator_enable(struct rdev *rdev) ++{ ++ assert(rdev); ++ ++ if (rdev->flags & REGUL_ALWAYS_ON) ++ return TEE_SUCCESS; ++ ++ return _regulator_enable(rdev); ++} ++ ++static TEE_Result _regulator_disable(struct rdev *rdev) ++{ ++ FMSG("%s", rdev->desc->node_name); ++ ++ if (!rdev->desc->ops->set_state) ++ return TEE_ERROR_GENERIC; ++ ++ lock_driver(rdev); ++ ++ if (rdev->use_count == 1) { ++ TEE_Result res = _regulator_set_state(rdev, false); ++ ++ if (res) { ++ EMSG("regul %s set state failed with %#"PRIx32, ++ rdev->desc->node_name, res); ++ unlock_driver(rdev); ++ return res; ++ } ++ } ++ ++ if (!rdev->use_count) { ++ EMSG("Unbalanced %s", rdev->desc->node_name); ++ panic(); ++ } ++ ++ rdev->use_count--; ++ ++ FMSG("%s refcount:%u", rdev->desc->node_name, rdev->use_count); ++ ++ unlock_driver(rdev); ++ ++ if (rdev->supply_dev && regulator_disable(rdev->supply_dev)) { ++ /* We can't leave this unbalanced */ ++ panic("can't disable supply"); ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++/* ++ * Disable regulator and its supplies ++ * ++ * @rdev - regulator device ++ */ ++TEE_Result regulator_disable(struct rdev *rdev) ++{ ++ assert(rdev); ++ ++ if (rdev->flags & REGUL_ALWAYS_ON) ++ return TEE_SUCCESS; ++ ++ return _regulator_disable(rdev); ++} ++ ++/* ++ * Return whether the regulator is enabled or not ++ * ++ * @rdev - regulator device ++ */ ++bool regulator_is_enabled(const struct rdev *rdev) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ bool enabled = false; ++ ++ if (!rdev->desc->ops->get_state) ++ return TEE_ERROR_GENERIC; ++ ++ lock_driver(rdev); ++ res = rdev->desc->ops->get_state(rdev->desc, &enabled); ++ unlock_driver(rdev); ++ ++ if (res) ++ EMSG("regul %s get state failed with %#"PRIx32, ++ rdev->desc->node_name, res); ++ ++ return !res && enabled; ++} ++ ++/* ++ * Set regulator voltage ++ * ++ * @rdev - regulator device ++ * @mvolt - Target voltage level in millivolt ++ * Return 0 if succeed, non 0 else. ++ */ ++TEE_Result regulator_set_voltage(struct rdev *rdev, uint16_t mvolt) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ ++ assert(rdev); ++ ++ FMSG("%s %"PRIu16"mV", rdev->desc->node_name, mvolt); ++ ++ if (!rdev->desc->ops->set_voltage) ++ return TEE_ERROR_NOT_IMPLEMENTED; ++ ++ if (mvolt < rdev->min_mv || mvolt > rdev->max_mv) ++ return TEE_ERROR_ACCESS_DENIED; ++ ++ lock_driver(rdev); ++ res = rdev->desc->ops->set_voltage(rdev->desc, mvolt); ++ unlock_driver(rdev); ++ ++ if (res) { ++ EMSG("regul %s set volt failed with %#"PRIx32, ++ rdev->desc->node_name, res); ++ return res; ++ } ++ ++ if (rdev->ramp_delay_uv_per_us > 0) { ++ unsigned int d = 0; ++ ++ if (rdev->cur_mv > mvolt) ++ d = rdev->cur_mv - mvolt; ++ else ++ d = mvolt - rdev->cur_mv; ++ ++ d = (d * 1000) / rdev->ramp_delay_uv_per_us; ++ ++ FMSG("%s %"PRIu32"uS", rdev->desc->node_name, d); ++ udelay(d); ++ } ++ ++ rdev->cur_mv = mvolt; ++ ++ return res; ++} ++ ++/* Set regulator to its min voltage level */ ++TEE_Result regulator_set_min_voltage(struct rdev *rdev) ++{ ++ return regulator_set_voltage(rdev, rdev->min_mv); ++} ++ ++/* ++ * Get regulator voltage level in millivolt ++ * ++ * @rdev - regulator device ++ * @mv - Output voltage level ++ */ ++TEE_Result regulator_get_voltage(const struct rdev *rdev, uint16_t *level_mv) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ ++ assert(rdev && level_mv); ++ ++ if (!rdev->desc->ops->get_voltage) { ++ *level_mv = rdev->min_mv; ++ return TEE_SUCCESS; ++ } ++ ++ lock_driver(rdev); ++ res = rdev->desc->ops->get_voltage(rdev->desc, level_mv); ++ unlock_driver(rdev); ++ ++ if (res) ++ EMSG("regul %s get voltage failed with %#"PRIx32, ++ rdev->desc->node_name, res); ++ ++ return res; ++} ++ ++/* ++ * List regulator voltages ++ * ++ * @rdev - regulator device ++ * @levels - optional output ref to supported millitvolt levels from min to max ++ * @count - output number of supported millivolt levels ++ */ ++TEE_Result regulator_list_voltages(struct rdev *rdev, uint16_t **levels, ++ size_t *count) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ uint16_t *ref = NULL; ++ size_t ref_cnt = 0; ++ size_t n = 0; ++ ++ assert(rdev && count); ++ ++ if (!rdev->desc->ops->list_voltages) { ++ if (levels) ++ *levels = &rdev->min_mv; ++ *count = 1; ++ return TEE_SUCCESS; ++ } ++ ++ lock_driver(rdev); ++ res = rdev->desc->ops->list_voltages(rdev->desc, &ref, &ref_cnt); ++ unlock_driver(rdev); ++ ++ if (res) { ++ /* Caller may be query the level table size */ ++ if (res != TEE_ERROR_SHORT_BUFFER) ++ DMSG("regul %s list_voltages failed: res = %#"PRIx32, ++ rdev->desc->node_name, res); ++ ++ return res; ++ } ++ ++ /* Reduce the possible values depending on min and max from DT */ ++ n = ref_cnt; ++ while ((ref[n - 1U] > rdev->max_mv) && (n > 1)) ++ n--; ++ ++ /* Verify that max val is a valid value */ ++ if (rdev->max_mv != ref[n - 1]) { ++ EMSG("regul %s: max value %u is invalid", ++ rdev->desc->node_name, rdev->max_mv); ++ return TEE_ERROR_GENERIC; ++ } ++ ++ while (*ref < rdev->min_mv && n > 1) { ++ ref++; ++ n--; ++ } ++ ++ if (!n) { ++ EMSG("regul %s set min voltage is too high", ++ rdev->desc->node_name); ++ return TEE_ERROR_GENERIC; ++ } ++ ++ if (rdev->min_mv != *ref) { ++ EMSG("regul %s: min value %u is invalid", ++ rdev->desc->node_name, rdev->min_mv); ++ return TEE_ERROR_GENERIC; ++ } ++ ++ *count = n; ++ if (levels) ++ *levels = ref; ++ ++ FMSG("rdev->min_mv=%u rdev->max_mv=%u", rdev->min_mv, rdev->max_mv); ++ ++ return TEE_SUCCESS; ++} ++ ++/* ++ * Get regulator voltages range ++ * ++ * @rdev - regulator device ++ * @min_mv - out: min possible millivolt value ++ * @max_mv - out: max possible millivolt value ++ * Return 0 if succeed, non 0 else. ++ */ ++void regulator_get_range(const struct rdev *rdev, uint16_t *min_mv, ++ uint16_t *max_mv) ++{ ++ if (min_mv) ++ *min_mv = rdev->min_mv; ++ ++ if (max_mv) ++ *max_mv = rdev->max_mv; ++} ++ ++/* ++ * Set regulator flag ++ * ++ * @rdev - regulator device ++ * @flag - flag value to set (eg: REGUL_OCP) ++ */ ++TEE_Result regulator_set_flag(struct rdev *rdev, uint16_t flag) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ ++ /* check that only one bit is set on flag */ ++ if (!IS_POWER_OF_TWO(flag)) ++ return TEE_ERROR_GENERIC; ++ ++ /* REGUL_ALWAYS_ON and REGUL_BOOT_ON are internal properties of the core */ ++ if ((flag == REGUL_ALWAYS_ON) || (flag == REGUL_BOOT_ON)) { ++ rdev->flags |= flag; ++ return TEE_SUCCESS; ++ } ++ ++ if (!rdev->desc->ops->set_flag) { ++ EMSG("%s lacks set_flag handler", rdev->desc->node_name); ++ return TEE_ERROR_GENERIC; ++ } ++ ++ lock_driver(rdev); ++ res = rdev->desc->ops->set_flag(rdev->desc, flag); ++ unlock_driver(rdev); ++ ++ if (res) { ++ EMSG("%s: set flag %#"PRIx16" failed with %#"PRIx32, ++ rdev->desc->node_name, flag, res); ++ return res; ++ } ++ ++ rdev->flags |= flag; ++ ++ return TEE_SUCCESS; ++} ++ ++#ifdef CFG_EMBED_DTB ++struct regul_property { ++ const char *name; ++ uint16_t flag; ++}; ++ ++static struct regul_property flag_prop[] = { ++ { ++ .name = "regulator-always-on", ++ .flag = REGUL_ALWAYS_ON, ++ }, ++ { ++ .name = "regulator-boot-on", ++ .flag = REGUL_BOOT_ON, ++ }, ++ { ++ .name = "regulator-active-discharge", ++ .flag = REGUL_ACTIVE_DISCHARGE, ++ }, ++ { ++ .name = "regulator-over-current-protection", ++ .flag = REGUL_OCP, ++ }, ++ { ++ .name = "regulator-pull-down", ++ .flag = REGUL_PULL_DOWN, ++ }, ++ { ++ .name = "st,mask-reset", ++ .flag = REGUL_MASK_RESET, ++ }, ++ { ++ .name = "st,regulator-sink-source", ++ .flag = REGUL_SINK_SOURCE, ++ }, ++ { ++ .name = "st,regulator-bypass", ++ .flag = REGUL_ENABLE_BYPASS, ++ }, ++}; ++ ++static TEE_Result parse_properties(const void *fdt, struct rdev *rdev, int node) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ const fdt32_t *cuint = NULL; ++ struct regul_property *p = NULL; ++ ++ for (p = flag_prop; p < (flag_prop + ARRAY_SIZE(flag_prop)); p++) { ++ if (fdt_getprop(fdt, node, p->name, NULL)) { ++ FMSG("%s: %#"PRIx32, rdev->desc->node_name, p->flag); ++ ++ res = regulator_set_flag(rdev, p->flag); ++ if (res) ++ return res; ++ } ++ } ++ ++ cuint = fdt_getprop(fdt, node, "regulator-ramp-delay", NULL); ++ if (cuint) { ++ rdev->ramp_delay_uv_per_us = (uint32_t)(fdt32_to_cpu(*cuint)); ++ FMSG("%s: ramp delay = %u (uV/us)", rdev->desc->node_name, ++ rdev->ramp_delay_uv_per_us); ++ } ++ ++ cuint = fdt_getprop(fdt, node, "regulator-enable-ramp-delay", NULL); ++ if (cuint) { ++ rdev->enable_ramp_delay_us = (uint32_t)(fdt32_to_cpu(*cuint)); ++ FMSG("%s: enable ramp delay = %u (us)", rdev->desc->node_name, ++ rdev->enable_ramp_delay_us); ++ } ++ ++ rdev->reg_name = fdt_getprop(fdt, node, "regulator-name", NULL); ++ ++ return TEE_SUCCESS; ++} ++ ++static void parse_supply(const void *fdt, struct rdev *rdev, int node) ++{ ++ const char *name = rdev->desc->supply_name; ++ ++ if (!name) ++ name = rdev->desc->node_name; ++ ++ rdev->supply_phandle = get_supply_phandle(fdt, node, name); ++ if (rdev->supply_phandle < 0) { ++ node = fdt_parent_offset(fdt, node); ++ rdev->supply_phandle = get_supply_phandle(fdt, node, name); ++ } ++} ++ ++static void parse_low_power_mode(const void *fdt, struct rdev *rdev, int node, ++ int mode) ++{ ++ const fdt32_t *cuint = NULL; ++ ++ rdev->lp_state[mode] = 0; ++ ++ if (fdt_getprop(fdt, node, "regulator-off-in-suspend", NULL)) { ++ FMSG("%s: mode:%d OFF", rdev->desc->node_name, mode); ++ rdev->lp_state[mode] |= LP_STATE_OFF; ++ } else if (fdt_getprop(fdt, node, "regulator-on-in-suspend", NULL)) { ++ FMSG("%s: mode:%d ON", rdev->desc->node_name, mode); ++ rdev->lp_state[mode] |= LP_STATE_ON; ++ } else { ++ rdev->lp_state[mode] |= LP_STATE_UNCHANGED; ++ } ++ ++ cuint = fdt_getprop(fdt, node, "regulator-suspend-microvolt", NULL); ++ if (cuint) { ++ uint16_t mv = (uint16_t)(fdt32_to_cpu(*cuint) / 1000U); ++ ++ FMSG("%s: mode:%d suspend mv=%u", rdev->desc->node_name, ++ mode, mv); ++ ++ rdev->lp_state[mode] |= LP_STATE_SET_VOLT; ++ rdev->lp_mv[mode] = mv; ++ } ++} ++ ++static void parse_low_power_modes(const void *fdt, struct rdev *rdev, int node) ++{ ++ int mode_count = plat_get_lp_mode_count(); ++ int mode = 0; ++ ++ for (mode = 0; mode < mode_count; mode++) { ++ const char *lp_mode_name = plat_get_lp_mode_name(mode); ++ int n = 0; ++ ++ if (lp_mode_name) { ++ /* Get the configs from regulator_state_node subnode */ ++ n = fdt_subnode_offset(fdt, node, lp_mode_name); ++ if (n >= 0) ++ parse_low_power_mode(fdt, rdev, n, mode); ++ } ++ } ++} ++ ++/* ++ * Parse the device-tree for a regulator ++ * ++ * Read min/max voltage from dt and check its validity ++ * Read the properties, and call the driver to set flags ++ * Read power supply phandle ++ * Read and store low power mode states ++ * ++ * @rdev - regulator device ++ * @node - device-tree node offset of the regulator ++ */ ++static TEE_Result parse_dt(struct rdev *rdev, int node) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ const fdt32_t *cuint = NULL; ++ uint16_t *levels = NULL; ++ void *fdt = NULL; ++ size_t size = 0; ++ ++ FMSG("%s: parse embedded DTB", rdev->desc->node_name); ++ ++ fdt = get_embedded_dt(); ++ if (!fdt) ++ return TEE_SUCCESS; ++ ++ /* Save phandle and DTB reference to identify the regulator */ ++ rdev->phandle = fdt_get_phandle(fdt, node); ++ ++ cuint = fdt_getprop(fdt, node, "regulator-min-microvolt", NULL); ++ if (cuint) { ++ uint16_t min_mv = (uint16_t)(fdt32_to_cpu(*cuint) / 1000U); ++ ++ FMSG("%s: min_mv=%"PRIu16, rdev->desc->node_name, min_mv); ++ if (min_mv <= rdev->max_mv) { ++ rdev->min_mv = min_mv; ++ } else { ++ EMSG("%s: min_mv=%"PRIu16" is too high", ++ rdev->desc->node_name, min_mv); ++ return TEE_ERROR_GENERIC; ++ } ++ } ++ ++ cuint = fdt_getprop(fdt, node, "regulator-max-microvolt", NULL); ++ if (cuint) { ++ uint16_t max_mv = (uint16_t)(fdt32_to_cpu(*cuint) / 1000U); ++ ++ FMSG("%s: max_mv=%"PRIu16, rdev->desc->node_name, max_mv); ++ if (max_mv >= rdev->min_mv) { ++ rdev->max_mv = max_mv; ++ } else { ++ EMSG("%s: max_mv=%"PRIu16" is too low", ++ rdev->desc->node_name, max_mv); ++ return TEE_ERROR_GENERIC; ++ } ++ } ++ ++ /* validate that min and max values can be used */ ++ res = regulator_list_voltages(rdev, &levels, &size); ++ if (res) ++ return res; ++ ++ res = parse_properties(fdt, rdev, node); ++ if (res) ++ return res; ++ ++ parse_supply(fdt, rdev, node); ++ ++ parse_low_power_modes(fdt, rdev, node); ++ ++ return TEE_SUCCESS; ++} ++#else /* CFG_EMBED_DTB */ ++static TEE_Result parse_dt(struct rdev *rdev __unused, int node __unused) ++{ ++ return TEE_SUCCESS; ++} ++#endif /* CFG_EMBED_DTB */ ++ ++/* ++ * Register a regulator driver in regulator framework. ++ * Initialize voltage range from driver description ++ * ++ * @desc - pointer to the regulator description ++ * @node - device-tree node offset of the regulator, or 0 if unused ++ */ ++TEE_Result regulator_register(const struct regul_desc *desc, int node) ++{ ++ TEE_Result res = TEE_ERROR_OUT_OF_MEMORY; ++ struct rdev *rdev = NULL; ++ size_t lp_mode_count = plat_get_lp_mode_count(); ++ ++ assert(desc); ++ ++ FMSG("register %s", desc->node_name); ++ ++ rdev = calloc(1, sizeof(*rdev)); ++ if (!rdev) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ if (lp_mode_count) { ++ rdev->lp_state = calloc(lp_mode_count, sizeof(*rdev->lp_state)); ++ rdev->lp_mv = calloc(lp_mode_count, sizeof(*rdev->lp_mv)); ++ ++ if (!rdev->lp_state || !rdev->lp_mv) { ++ res = TEE_ERROR_OUT_OF_MEMORY; ++ goto out; ++ } ++ } ++ ++ rdev->ramp_delay_uv_per_us = desc->ramp_delay_uv_per_us; ++ rdev->enable_ramp_delay_us = desc->enable_ramp_delay_us; ++ rdev->desc = desc; ++ ++ if (desc->ops->list_voltages) { ++ uint16_t *levels = NULL; ++ size_t count = 0; ++ ++ lock_driver(rdev); ++ res = desc->ops->list_voltages(desc, &levels, &count); ++ unlock_driver(rdev); ++ ++ if (res) { ++ EMSG("%s list_voltages failed with %#"PRIx32, ++ desc->node_name, res); ++ goto out; ++ } ++ assert(levels && count); ++ ++ rdev->min_mv = levels[0]; ++ rdev->max_mv = levels[count - 1]; ++ } else { ++ rdev->max_mv = UINT16_MAX; ++ } ++ ++ res = parse_dt(rdev, node); ++ if (res) ++ goto out; ++ ++ SLIST_INSERT_HEAD(®ulator_device_list, rdev, link); ++ ++out: ++ if (res) { ++ free(rdev->lp_state); ++ free(rdev->lp_mv); ++ } ++ ++ return res; ++} ++ ++/* ++ * Suspend a single regulator before low power entry ++ * Call regulator suspend call back, ++ * Enable the regulator if boot_on flag is set as regulator is needed during ++ * boot/resume from suspend sequences. ++ * ++ * @rdev - regulator device ++ * @mode - low power mode index ++ */ ++static TEE_Result suspend_regulator(struct rdev *rdev, unsigned int pm_hint) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ int mode = pm_hint; ++ ++ if (rdev->desc->ops->suspend) { ++ lock_driver(rdev); ++ res = rdev->desc->ops->suspend(rdev->desc, rdev->lp_state[mode], ++ rdev->lp_mv[mode]); ++ unlock_driver(rdev); ++ ++ if (res) { ++ EMSG("%s suspend failed with %#"PRIx32, ++ rdev->desc->node_name, res); ++ return res; ++ } ++ } ++ ++ if (rdev->flags & REGUL_BOOT_ON) ++ res = regulator_enable(rdev); ++ ++ return res; ++} ++ ++/* ++ * Resume a single regulator after low power ++ * ++ * @rdev - regulator device ++ */ ++static int resume_regulator(struct rdev *rdev) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ ++ if (rdev->flags & REGUL_BOOT_ON) { ++ /* Balance refcount in suspend sequence */ ++ res = regulator_disable(rdev); ++ if (res) ++ EMSG("%s resume failed with %#"PRIx32, ++ rdev->desc->node_name, res); ++ } ++ ++ return res; ++} ++ ++static TEE_Result regulator_pm(enum pm_op op, unsigned int pm_hint, ++ const struct pm_callback_handle *hdl __unused) ++{ ++ struct rdev *rdev = NULL; ++ ++ assert(op == PM_OP_SUSPEND || op == PM_OP_RESUME); ++ ++ if (op == PM_OP_SUSPEND) { ++ FMSG("Regulator core suspend"); ++ assert(pm_hint < (unsigned int)plat_get_lp_mode_count()); ++ ++ SLIST_FOREACH(rdev, ®ulator_device_list, link) ++ if (suspend_regulator(rdev, pm_hint)) ++ panic(); ++ ++ regulator_core_dump(); ++ } else { ++ FMSG("Regulator core resume"); ++ ++ SLIST_FOREACH(rdev, ®ulator_device_list, link) ++ if (resume_regulator(rdev)) ++ panic(); ++ } ++ ++ return TEE_SUCCESS; ++} ++DECLARE_KEEP_PAGER(regulator_pm); ++ ++static void __maybe_unused sprint_name(char *dest, const char *src, int len) ++{ ++ int l = 0; ++ ++ if (!src) ++ src = ""; ++ ++ l = strlen(src); ++ if (l > len) ++ l = len; ++ ++ memset(dest, ' ', len); ++ memcpy(dest, src, l); ++ dest[len] = '\0'; ++} ++ ++/* ++ * Log regulators state ++ */ ++void regulator_core_dump(void) ++{ ++ struct rdev *rdev = NULL; ++ ++ if (TRACE_LEVEL < TRACE_FLOW) ++ return; ++ ++ FMSG("Dump Regulators\n"); ++ FMSG("reg name use\ten\tmV\tmin\tmax\tflags\tsupply\n"); ++ ++ SLIST_FOREACH(rdev, ®ulator_device_list, link) { ++ uint16_t mv = 0; ++ uint16_t min_mv = 0; ++ uint16_t max_mv = 0; ++ char reg[9] = ""; ++ char name[9] = { 0 }; ++ const char __maybe_unused *supply = ""; ++ ++ sprint_name(name, rdev->desc->node_name, 8); ++ sprint_name(reg, rdev->reg_name, 8); ++ ++ if (regulator_get_voltage(rdev, &mv)) ++ mv = UINT16_MAX; ++ ++ regulator_get_range(rdev, &min_mv, &max_mv); ++ if (rdev->supply_dev) ++ supply = rdev->supply_dev->desc->node_name; ++ ++ FMSG("%s %s %d\t%d\t%"PRIu16"\t%"PRIu16"\t%"PRIu16"\t%#"PRIx32"\t%s\n", ++ reg, name, rdev->use_count, regulator_is_enabled(rdev), mv, ++ min_mv, max_mv, rdev->flags, supply); ++ } ++} ++ ++/* ++ * Connect each regulator to its supply ++ * Apply min voltage if the voltage is outside the authorized range ++ * Enable always-on regulators ++ */ ++static TEE_Result regulator_core_config(void) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ struct rdev *rdev = NULL; ++ ++ FMSG(""); ++ ++ SLIST_FOREACH(rdev, ®ulator_device_list, link) { ++ if (rdev->supply_phandle >= 0) { ++ struct rdev *s = NULL; ++ ++ FMSG("%s: connect supply", rdev->desc->node_name); ++ ++ s = regulator_get_by_phandle(rdev->supply_phandle); ++ if (!s) ++ return TEE_ERROR_GENERIC; ++ ++ rdev->supply_dev = s; ++ } ++ } ++ ++ SLIST_FOREACH(rdev, ®ulator_device_list, link) { ++ uint16_t mv = 0; ++ uint16_t min_mv = 0; ++ uint16_t max_mv = 0; ++ ++ regulator_get_range(rdev, &min_mv, &max_mv); ++ ++ res = regulator_get_voltage(rdev, &mv); ++ if (res) ++ break; ++ ++ rdev->cur_mv = mv; ++ ++ if ((mv < min_mv) || (mv > max_mv)) { ++ res = regulator_set_voltage(rdev, min_mv); ++ if (res) ++ break; ++ } ++ ++ /* ++ * Enable always-on regulator and increment its use_count so that ++ * the regulator is not being disabled during clean-up sequence. ++ */ ++ if (rdev->flags & REGUL_ALWAYS_ON) { ++ res = _regulator_enable(rdev); ++ if (res) ++ break; ++ } ++ } ++ ++ regulator_core_dump(); ++ ++ return res; ++} ++ ++static TEE_Result regulator_core_init(void) ++{ ++ if (regulator_core_config()) ++ panic(); ++ ++ register_pm_driver_cb(regulator_pm, NULL, "regulator-driver"); ++ ++ return TEE_SUCCESS; ++} ++driver_init_late(regulator_core_init); ++ ++/* ++ * Clean-up regulators that are not used. ++ */ ++static TEE_Result regulator_core_cleanup(void) ++{ ++ struct rdev *rdev = NULL; ++ ++ FMSG(""); ++ ++ SLIST_FOREACH(rdev, ®ulator_device_list, link) { ++ if (!rdev->use_count) { ++ DMSG("disable %s", rdev->desc->node_name); ++ lock_driver(rdev); ++ _regulator_set_state(rdev, false /* disable */); ++ unlock_driver(rdev); ++ } ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++release_init_resource(regulator_core_cleanup); +diff --git a/core/drivers/regulator/regulator_fixed.c b/core/drivers/regulator/regulator_fixed.c +new file mode 100644 +index 000000000..146fa6c8e +--- /dev/null ++++ b/core/drivers/regulator/regulator_fixed.c +@@ -0,0 +1,86 @@ ++// SPDX-License-Identifier: BSD-2-Clause ++/* ++ * Copyright (c) 2020-2021, STMicroelectronics ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#define FIXED_REGULATOR_NAME_LEN 16 ++ ++struct fixed_regul { ++ char name[FIXED_REGULATOR_NAME_LEN]; ++ struct regul_desc desc; ++}; ++ ++static TEE_Result fixed_set_state(const struct regul_desc *desc __unused, ++ bool enabled __unused) ++{ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result fixed_get_state(const struct regul_desc *desc __unused, ++ bool *enabled) ++{ ++ *enabled = true; ++ ++ return TEE_SUCCESS; ++} ++ ++static struct regul_ops fixed_regul_ops = { ++ .set_state = fixed_set_state, ++ .get_state = fixed_get_state, ++}; ++DECLARE_KEEP_PAGER(fixed_regul_ops); ++ ++static TEE_Result fixed_regulator_probe(const void *fdt, int node, ++ const void *compat_data __unused) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ ++ size_t __maybe_unused len = 0; ++ struct fixed_regul *fr = NULL; ++ const char *reg_name = NULL; ++ ++ fr = calloc(1, sizeof(*fr)); ++ if (!fr) ++ panic(); ++ ++ reg_name = fdt_get_name(fdt, node, NULL); ++ len = snprintf(fr->name, sizeof(fr->name) - 1, "%s", reg_name); ++ assert(len > 0 && len < (sizeof(fr->name) - 1)); ++ ++ fr->desc.node_name = fr->name; ++ fr->desc.driver_data = fr; ++ fr->desc.ops = &fixed_regul_ops; ++ ++ res = regulator_register(&fr->desc, node); ++ if (res) { ++ EMSG("regulator_register(%s) failed with %#"PRIx32, ++ reg_name, res); ++ panic(); ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++static const struct dt_device_match regulator_match_table[] = { ++ { .compatible = "regulator-fixed" }, ++ { } ++}; ++ ++DEFINE_DT_DRIVER(fixed_regulator_dt_driver) = { ++ .name = "fixed-regulator", ++ .match_table = regulator_match_table, ++ .probe = fixed_regulator_probe, ++}; +diff --git a/core/drivers/regulator/stm32_regulator_gpio.c b/core/drivers/regulator/stm32_regulator_gpio.c +new file mode 100644 +index 000000000..e45f9a8f6 +--- /dev/null ++++ b/core/drivers/regulator/stm32_regulator_gpio.c +@@ -0,0 +1,169 @@ ++// SPDX-License-Identifier: BSD-2-Clause ++/* ++ * Copyright (c) 2020-2021, STMicroelectronics ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#define GPIO_REGULATOR_NAME_LEN 16 ++#define GPIO_REGULATOR_MAX_STATES 2 ++ ++struct gpio_regul { ++ char name[GPIO_REGULATOR_NAME_LEN]; ++ struct regul_desc desc; ++ struct stm32_pinctrl_list *pinctrl; ++ uint16_t gpio_low_mv; ++ uint16_t gpio_high_mv; ++ uint16_t gpio_voltage_table[2]; ++}; ++ ++static TEE_Result gpio_get_voltage(const struct regul_desc *desc, uint16_t *mv) ++{ ++ struct gpio_regul *gr = (struct gpio_regul *)desc->driver_data; ++ struct stm32_pinctrl *pinctrl = NULL; ++ unsigned int gpio = 0; ++ ++ FMSG("%s: get volt", desc->node_name); ++ ++ pinctrl = STAILQ_FIRST(gr->pinctrl); ++ if (!pinctrl) ++ panic(); ++ ++ gpio = stm32_pinctrl_get_gpio_id(pinctrl); ++ ++ if (stm32_gpio_get_ops()->get_value(NULL, gpio) == GPIO_LEVEL_HIGH) ++ *mv = gr->gpio_high_mv; ++ else ++ *mv = gr->gpio_low_mv; ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result gpio_set_voltage(const struct regul_desc *desc, uint16_t mv) ++{ ++ struct gpio_regul *gr = (struct gpio_regul *)desc->driver_data; ++ struct stm32_pinctrl *pinctrl = NULL; ++ unsigned int gpio = 0; ++ enum gpio_level level = GPIO_LEVEL_LOW; ++ ++ FMSG("%s: set volt", desc->node_name); ++ ++ pinctrl = STAILQ_FIRST(gr->pinctrl); ++ if (!pinctrl) ++ panic(); ++ ++ gpio = stm32_pinctrl_get_gpio_id(pinctrl); ++ ++ if (mv == gr->gpio_high_mv) ++ level = GPIO_LEVEL_HIGH; ++ else if (mv != gr->gpio_low_mv) ++ panic(); ++ ++ stm32_gpio_get_ops()->set_value(NULL, gpio, level); ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result gpio_list_voltages(const struct regul_desc *desc, ++ uint16_t **levels, size_t *count) ++{ ++ struct gpio_regul *gr = (struct gpio_regul *)desc->driver_data; ++ ++ *count = 2; ++ *levels = gr->gpio_voltage_table; ++ ++ return TEE_SUCCESS; ++} ++ ++static struct regul_ops gpio_regul_ops = { ++ .set_voltage = gpio_set_voltage, ++ .get_voltage = gpio_get_voltage, ++ .list_voltages = gpio_list_voltages, ++}; ++DECLARE_KEEP_PAGER(gpio_regul_ops); ++ ++static TEE_Result gpio_regulator_probe(const void *fdt, int node, ++ const void *compat_data __unused) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ ++ size_t len = 0; ++ struct gpio_regul *gr = NULL; ++ const char *reg_name = NULL; ++ const fdt32_t *cuint = NULL; ++ struct stm32_pinctrl *pinctrl = NULL; ++ ++ gr = calloc(1, sizeof(*gr)); ++ if (!gr) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ reg_name = fdt_get_name(fdt, node, NULL); ++ len = snprintf(gr->name, sizeof(gr->name) - 1, "%s", reg_name); ++ assert(len > 0 && len < (sizeof(gr->name) - 1)); ++ ++ gr->desc.node_name = gr->name; ++ gr->desc.driver_data = gr; ++ gr->desc.ops = &gpio_regul_ops; ++ ++ res = stm32_pinctrl_dt_get_by_index(fdt, node, 0, &gr->pinctrl); ++ if(res) ++ return res; ++ ++ len = 0; ++ ++ STAILQ_FOREACH(pinctrl, gr->pinctrl, link) ++ len++; ++ ++ if (len > 1) ++ panic("Too many PINCTRLs found"); ++ ++ cuint = fdt_getprop(fdt, node, "low-level-microvolt", NULL); ++ if (cuint) ++ gr->gpio_low_mv = (uint16_t)(fdt32_to_cpu(*cuint) / 1000U); ++ ++ cuint = fdt_getprop(fdt, node, "high-level-microvolt", NULL); ++ if (cuint) ++ gr->gpio_high_mv = (uint16_t)(fdt32_to_cpu(*cuint) / 1000U); ++ ++ if (gr->gpio_low_mv < gr->gpio_high_mv) { ++ gr->gpio_voltage_table[0] = gr->gpio_low_mv; ++ gr->gpio_voltage_table[1] = gr->gpio_high_mv; ++ } else { ++ gr->gpio_voltage_table[0] = gr->gpio_high_mv; ++ gr->gpio_voltage_table[1] = gr->gpio_low_mv; ++ } ++ ++ res = regulator_register(&gr->desc, node); ++ if (res) { ++ EMSG("regulator_register(%s) failed with %#"PRIx32, ++ reg_name, res); ++ panic(); ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++static const struct dt_device_match gpio_regulator_match_table[] = { ++ { .compatible = "st,stm32-regulator-gpio" }, ++ { } ++}; ++ ++DEFINE_DT_DRIVER(stm32_gpio_regulator_dt_driver) = { ++ .name = "stm32-gpio-regulator", ++ .match_table = gpio_regulator_match_table, ++ .probe = gpio_regulator_probe, ++}; +diff --git a/core/drivers/regulator/stm32_vrefbuf.c b/core/drivers/regulator/stm32_vrefbuf.c +new file mode 100644 +index 000000000..d2500e361 +--- /dev/null ++++ b/core/drivers/regulator/stm32_vrefbuf.c +@@ -0,0 +1,295 @@ ++// SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0+) ++/* ++ * Copyright (c) 2021, STMicroelectronics ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++/* STM32 VREFBUF registers */ ++#define STM32_VREFBUF_CSR U(0x00) ++ ++/* STM32 VREFBUF CSR bitfields */ ++ ++/* VRS bit 3 is unused because the voltage is not specified */ ++#define STM32_VRS GENMASK_32(5, 4) ++#define STM32_VRS_SHIFT U(4) ++#define INV_VRS(x) ((~(x)) & STM32_VRS) ++ ++#define STM32_VRR BIT(3) ++#define STM32_HIZ BIT(1) ++#define STM32_ENVR BIT(0) ++ ++#define VREFBUF_REGULATOR_NAME_LEN U(24) ++ ++#define TIMEOUT_US_10MS U(10 * 1000) ++#define TIMEOUT_US_1MS U(1 * 1000) ++ ++struct vrefbuf_regul { ++ char name[VREFBUF_REGULATOR_NAME_LEN]; ++ struct io_pa_va base; ++ struct regul_desc desc; ++ struct clk *clock; ++ uint32_t pm_val; ++ uint64_t disable_timeout; ++}; ++ ++static uint16_t stm32_vrefbuf_voltages[] = { ++ /* Matches resp. VRS = 011b, 010b, 001b, 000b */ ++ 1650, 1800, 2048, 2500, ++}; ++ ++static vaddr_t stm32mp_vrefbuf_base(struct vrefbuf_regul *vr) ++{ ++ return io_pa_or_va_secure(&vr->base, 1); ++} ++ ++static struct mutex vrefbuf_mu = MUTEX_INITIALIZER; ++ ++static TEE_Result vrefbuf_wait_ready(struct vrefbuf_regul *vr) ++{ ++ uint64_t to = timeout_init_us(TIMEOUT_US_10MS); ++ uintptr_t reg = stm32mp_vrefbuf_base(vr) + STM32_VREFBUF_CSR; ++ ++ while (!timeout_elapsed(to)) ++ if ((io_read32(reg) & STM32_VRR)) ++ return TEE_SUCCESS; ++ ++ return TEE_ERROR_GENERIC; ++} ++ ++static TEE_Result vrefbuf_set_state(const struct regul_desc *desc, bool enable) ++{ ++ struct vrefbuf_regul *vr = (struct vrefbuf_regul *)desc->driver_data; ++ uintptr_t reg = stm32mp_vrefbuf_base(vr) + STM32_VREFBUF_CSR; ++ ++ clk_enable(vr->clock); ++ ++ if (enable) { ++ io_clrbits32(reg, STM32_HIZ); ++ ++ /* ++ * If first enable after boot, ++ * or if it was disabled since less than 1ms, ++ * then wait for 1ms in pull down mode to avoid an overshoot. ++ */ ++ if (vr->disable_timeout == 0 || ++ !timeout_elapsed(vr->disable_timeout)) ++ udelay(1000); ++ ++ io_setbits32(reg, STM32_ENVR); ++ ++ if (vrefbuf_wait_ready(vr) != TEE_SUCCESS) { ++ clk_disable(vr->clock); ++ ++ return TEE_ERROR_GENERIC; ++ } ++ } else { ++ io_clrbits32(reg, STM32_ENVR); ++ ++ vr->disable_timeout = timeout_init_us(TIMEOUT_US_1MS); ++ } ++ ++ clk_disable(vr->clock); ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result vrefbuf_get_state(const struct regul_desc *desc __unused, ++ bool *enabled) ++{ ++ struct vrefbuf_regul *vr = (struct vrefbuf_regul *)desc->driver_data; ++ uintptr_t reg = stm32mp_vrefbuf_base(vr) + STM32_VREFBUF_CSR; ++ ++ clk_enable(vr->clock); ++ ++ *enabled = io_read32(reg) & STM32_VRR; ++ ++ clk_disable(vr->clock); ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result vrefbuf_get_voltage(const struct regul_desc *desc __unused, ++ uint16_t *mv) ++{ ++ struct vrefbuf_regul *vr = (struct vrefbuf_regul *)desc->driver_data; ++ uintptr_t reg = stm32mp_vrefbuf_base(vr) + STM32_VREFBUF_CSR; ++ uint32_t index = 0; ++ ++ clk_enable(vr->clock); ++ ++ index = io_read32(reg) & STM32_VRS; ++ index = INV_VRS(index) >> STM32_VRS_SHIFT; ++ ++ clk_disable(vr->clock); ++ ++ *mv = stm32_vrefbuf_voltages[index]; ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result vrefbuf_set_voltage(const struct regul_desc *desc __unused, ++ uint16_t mv) ++{ ++ struct vrefbuf_regul *vr = (struct vrefbuf_regul *)desc->driver_data; ++ uintptr_t reg = stm32mp_vrefbuf_base(vr) + STM32_VREFBUF_CSR; ++ uint8_t i = 0; ++ ++ for (i = 0 ; i < ARRAY_SIZE(stm32_vrefbuf_voltages) ; i++) ++ if (stm32_vrefbuf_voltages[i] == mv) { ++ uint32_t val = INV_VRS(i << STM32_VRS_SHIFT); ++ ++ clk_enable(vr->clock); ++ ++ io_clrsetbits32(reg, STM32_VRS, val); ++ ++ clk_disable(vr->clock); ++ ++ return TEE_SUCCESS; ++ } ++ ++ EMSG("Failed to set voltage on vrefbuf"); ++ ++ return TEE_ERROR_GENERIC; ++} ++ ++static TEE_Result vrefbuf_list_voltages(const struct regul_desc *desc __unused, ++ uint16_t **levels, size_t *count) ++{ ++ *count = ARRAY_SIZE(stm32_vrefbuf_voltages); ++ *levels = stm32_vrefbuf_voltages; ++ ++ return TEE_SUCCESS; ++} ++ ++static void vrefbuf_lock(const struct regul_desc *desc __unused) ++{ ++ if (thread_get_id_may_fail() != THREAD_ID_INVALID) ++ mutex_lock(&vrefbuf_mu); ++} ++ ++static void vrefbuf_unlock(const struct regul_desc *desc __unused) ++{ ++ if (thread_get_id_may_fail() != THREAD_ID_INVALID) ++ mutex_unlock(&vrefbuf_mu); ++} ++ ++struct regul_ops vrefbuf_ops = { ++ .set_state = vrefbuf_set_state, ++ .get_state = vrefbuf_get_state, ++ .set_voltage = vrefbuf_set_voltage, ++ .get_voltage = vrefbuf_get_voltage, ++ .list_voltages = vrefbuf_list_voltages, ++ .lock = vrefbuf_lock, ++ .unlock = vrefbuf_unlock, ++}; ++ ++static TEE_Result vrefbuf_pm(enum pm_op op, unsigned int pm_hint __unused, ++ const struct pm_callback_handle *hdl __unused) ++{ ++ struct vrefbuf_regul *vr = (struct vrefbuf_regul *)hdl->handle; ++ uintptr_t reg = stm32mp_vrefbuf_base(vr) + STM32_VREFBUF_CSR; ++ ++ assert(op == PM_OP_SUSPEND || op == PM_OP_RESUME); ++ ++ clk_enable(vr->clock); ++ ++ if (op == PM_OP_SUSPEND) { ++ FMSG("Vrefbuf suspend"); ++ vr->pm_val = io_read32(reg); ++ ++ if ((vr->pm_val & STM32_ENVR) && ++ (vrefbuf_wait_ready(vr) != TEE_SUCCESS)) { ++ clk_disable(vr->clock); ++ ++ return TEE_ERROR_GENERIC; ++ } ++ } else { ++ FMSG("Vrefbuf resume"); ++ ++ io_clrsetbits32(reg, STM32_VRS, vr->pm_val); ++ ++ if (vr->pm_val | STM32_ENVR) { ++ vr->disable_timeout = 0; ++ vrefbuf_set_state(&vr->desc, true); ++ } ++ } ++ ++ clk_disable(vr->clock); ++ ++ return TEE_SUCCESS; ++} ++DECLARE_KEEP_PAGER(vrefbuf_pm); ++ ++static TEE_Result stm32_vrefbuf_regulator_probe(const void *fdt, int node, ++ const void *comp_data __unused) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ struct dt_node_info info = { }; ++ size_t __maybe_unused len = 0; ++ const char *reg_name = NULL; ++ struct vrefbuf_regul *vr = NULL; ++ struct io_pa_va base = { }; ++ struct clk *clk = NULL; ++ ++ FMSG("vrefbuf init"); ++ ++ res = clk_dt_get_by_index(fdt, node, 0, &clk); ++ if (res) ++ return res; ++ ++ _fdt_fill_device_info(fdt, &info, node); ++ ++ vr = calloc(1, sizeof(*vr)); ++ if (!vr) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ reg_name = fdt_get_name(fdt, node, NULL); ++ len = snprintf(vr->name, sizeof(vr->name) - 1, "%s", reg_name); ++ assert(len > 0 && len < (sizeof(vr->name) - 1)); ++ ++ base.pa = info.reg; ++ io_pa_or_va_secure(&base, info.reg_size); ++ if (!base.va) ++ panic(); ++ ++ vr->base = base; ++ vr->desc.node_name = vr->name; ++ vr->desc.driver_data = vr; ++ vr->desc.ops = &vrefbuf_ops; ++ vr->desc.supply_name = "vdda"; ++ vr->clock = clk; ++ ++ res = regulator_register(&vr->desc, node); ++ if (res != 0) { ++ EMSG("Failed to register vrefbuf"); ++ return res; ++ } ++ ++ register_pm_driver_cb(vrefbuf_pm, (void *)vr, "vrefbuf-driver"); ++ ++ return 0; ++} ++ ++static const struct dt_device_match vrefbuf_match_table[] = { ++ { .compatible = "st,stm32mp13-vrefbuf" }, ++ { } ++}; ++ ++DEFINE_DT_DRIVER(vrefbuf_regulator_dt_driver) = { ++ .name = "stm32-vrefbuf-regulator", ++ .match_table = vrefbuf_match_table, ++ .probe = &stm32_vrefbuf_regulator_probe, ++}; +diff --git a/core/drivers/regulator/sub.mk b/core/drivers/regulator/sub.mk +new file mode 100644 +index 000000000..8e18c5dbe +--- /dev/null ++++ b/core/drivers/regulator/sub.mk +@@ -0,0 +1,4 @@ ++srcs-y += core.c ++srcs-$(CFG_REGULATOR_FIXED) += regulator_fixed.c ++srcs-$(CFG_STM32_REGULATOR_GPIO) += stm32_regulator_gpio.c ++srcs-$(CFG_STM32_VREFBUF) += stm32_vrefbuf.c +diff --git a/core/drivers/scmi-msg/base.c b/core/drivers/scmi-msg/base.c +index 8a07130a9..6efd8c4f4 100644 +--- a/core/drivers/scmi-msg/base.c ++++ b/core/drivers/scmi-msg/base.c +@@ -163,7 +163,8 @@ static void discover_list_protocols(struct scmi_msg *msg) + memcpy(outargs, &p2a, sizeof(p2a)); + memcpy(outargs + sizeof(p2a), list + a2p->skip, count); + +- scmi_write_response(msg, outargs, sizeof(outargs)); ++ scmi_write_response(msg, outargs, ++ sizeof(p2a) + ROUNDUP(count, sizeof(uint32_t))); + } + + static const scmi_msg_handler_t scmi_base_handler_table[] = { +diff --git a/core/drivers/scmi-msg/clock.c b/core/drivers/scmi-msg/clock.c +index aa8db259e..1972360d3 100644 +--- a/core/drivers/scmi-msg/clock.c ++++ b/core/drivers/scmi-msg/clock.c +@@ -1,10 +1,11 @@ + // SPDX-License-Identifier: BSD-3-Clause + /* +- * Copyright (c) 2015-2019, Arm Limited and Contributors. All rights reserved. + * Copyright (c) 2019, Linaro Limited ++ * Copyright (c) 2015-2020, Arm Limited and Contributors. All rights reserved. + */ + #include + #include ++#include + #include + #include + #include +@@ -68,6 +69,13 @@ int32_t __weak plat_scmi_clock_set_state(unsigned int channel_id __unused, + return SCMI_NOT_SUPPORTED; + } + ++int32_t __weak plat_scmi_clock_get_duty_cycle(unsigned int channel_id __unused, ++ unsigned int scmi_id __unused, ++ struct clk_duty *duty __unused) ++{ ++ return SCMI_NOT_SUPPORTED; ++} ++ + static void report_version(struct scmi_msg *msg) + { + struct scmi_protocol_version_p2a return_values = { +@@ -184,6 +192,37 @@ static void scmi_clock_rate_get(struct scmi_msg *msg) + scmi_write_response(msg, &return_values, sizeof(return_values)); + } + ++static void scmi_clock_round_rate_get(struct scmi_msg *msg) ++{ ++ const struct scmi_clock_rate_set_a2p *in_args = (void *)msg->in; ++ unsigned long rate = 0; ++ struct scmi_clock_rate_get_p2a return_values = { }; ++ unsigned int clock_id = 0; ++ uint64_t rate_64 = 0; ++ ++ if (msg->in_size != sizeof(*in_args)) { ++ scmi_status_response(msg, SCMI_PROTOCOL_ERROR); ++ return; ++ } ++ ++ if (in_args->clock_id >= plat_scmi_clock_count(msg->channel_id)) { ++ scmi_status_response(msg, SCMI_INVALID_PARAMETERS); ++ return; ++ } ++ ++ clock_id = confine_array_index(in_args->clock_id, ++ plat_scmi_clock_count(msg->channel_id)); ++ ++ rate_64 = reg_pair_to_64(in_args->rate[1], in_args->rate[0]); ++ rate = rate_64; ++ ++ rate = plat_scmi_clock_round_rate(msg->channel_id, clock_id, rate); ++ ++ reg_pair_from_64(rate, return_values.rate + 1, return_values.rate); ++ ++ scmi_write_response(msg, &return_values, sizeof(return_values)); ++} ++ + static void scmi_clock_rate_set(struct scmi_msg *msg) + { + const struct scmi_clock_rate_set_a2p *in_args = (void *)msg->in; +@@ -213,6 +252,34 @@ static void scmi_clock_rate_set(struct scmi_msg *msg) + scmi_status_response(msg, status); + } + ++static void scmi_clock_duty_cycle_get(struct scmi_msg *msg) ++{ ++ const struct scmi_clock_duty_cycle_get_a2p *in_args = (void *)msg->in; ++ struct scmi_clock_duty_cycle_get_p2a return_values = { }; ++ struct clk_duty duty = { 1, 2}; ++ unsigned int clock_id = 0; ++ ++ if (msg->in_size != sizeof(*in_args)) { ++ scmi_status_response(msg, SCMI_PROTOCOL_ERROR); ++ return; ++ } ++ ++ if (in_args->clock_id >= plat_scmi_clock_count(msg->channel_id)) { ++ scmi_status_response(msg, SCMI_INVALID_PARAMETERS); ++ return; ++ } ++ ++ clock_id = confine_array_index(in_args->clock_id, ++ plat_scmi_clock_count(msg->channel_id)); ++ ++ plat_scmi_clock_get_duty_cycle(msg->channel_id, clock_id, &duty); ++ ++ return_values.num = duty.num; ++ return_values.den = duty.den; ++ ++ scmi_write_response(msg, &return_values, sizeof(return_values)); ++} ++ + static void scmi_clock_config_set(struct scmi_msg *msg) + { + const struct scmi_clock_config_set_a2p *in_args = (void *)msg->in; +@@ -355,9 +422,11 @@ static const scmi_msg_handler_t scmi_clock_handler_table[] = { + [SCMI_CLOCK_RATE_SET] = scmi_clock_rate_set, + [SCMI_CLOCK_RATE_GET] = scmi_clock_rate_get, + [SCMI_CLOCK_CONFIG_SET] = scmi_clock_config_set, ++ [SCMI_CLOCK_DUTY_CYCLE_GET] = scmi_clock_duty_cycle_get, ++ [SCMI_CLOCK_ROUND_RATE_GET] = scmi_clock_round_rate_get, + }; + +-static bool message_id_is_supported(size_t message_id) ++static bool message_id_is_supported(unsigned int message_id) + { + return message_id < ARRAY_SIZE(scmi_clock_handler_table) && + scmi_clock_handler_table[message_id]; +diff --git a/core/drivers/scmi-msg/clock.h b/core/drivers/scmi-msg/clock.h +index a20678c33..7cc9a24be 100644 +--- a/core/drivers/scmi-msg/clock.h ++++ b/core/drivers/scmi-msg/clock.h +@@ -23,6 +23,8 @@ enum scmi_clock_command_id { + SCMI_CLOCK_RATE_SET = 0x005, + SCMI_CLOCK_RATE_GET = 0x006, + SCMI_CLOCK_CONFIG_SET = 0x007, ++ SCMI_CLOCK_DUTY_CYCLE_GET = 0x008, ++ SCMI_CLOCK_ROUND_RATE_GET = 0x009, + }; + + /* Protocol attributes */ +@@ -58,6 +60,16 @@ struct scmi_clock_rate_get_p2a { + uint32_t rate[2]; + }; + ++struct scmi_clock_duty_cycle_get_a2p { ++ uint32_t clock_id; ++}; ++ ++struct scmi_clock_duty_cycle_get_p2a { ++ int32_t status; ++ uint32_t num; ++ uint32_t den; ++}; ++ + /* + * Clock Rate Set + */ +diff --git a/core/drivers/scmi-msg/entry.c b/core/drivers/scmi-msg/entry.c +index 4c019e4d6..0ec0fa9a6 100644 +--- a/core/drivers/scmi-msg/entry.c ++++ b/core/drivers/scmi-msg/entry.c +@@ -10,6 +10,7 @@ + #include "base.h" + #include "clock.h" + #include "common.h" ++#include "perf_domain.h" + #include "reset_domain.h" + #include "voltage_domain.h" + +@@ -52,6 +53,9 @@ void scmi_process_message(struct scmi_msg *msg) + case SCMI_PROTOCOL_ID_VOLTAGE_DOMAIN: + handler = scmi_msg_get_voltd_handler(msg); + break; ++ case SCMI_PROTOCOL_ID_PERF: ++ handler = scmi_msg_get_perf_handler(msg); ++ break; + default: + break; + } +diff --git a/core/drivers/scmi-msg/perf_domain.c b/core/drivers/scmi-msg/perf_domain.c +new file mode 100644 +index 000000000..3e62e3bd2 +--- /dev/null ++++ b/core/drivers/scmi-msg/perf_domain.c +@@ -0,0 +1,385 @@ ++// SPDX-License-Identifier: BSD-3-Clause ++/* ++ * Copyright (c) 2015-2019, Arm Limited and Contributors. All rights reserved. ++ * Copyright (c) 2021, Linaro Limited ++ */ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#include "common.h" ++#include "perf_domain.h" ++ ++#define VERBOSE_MSG(...) FMSG(__VA_ARGS__) ++ ++static bool message_id_is_supported(unsigned int message_id); ++ ++/* Weak handlers platform shall override on purpose */ ++size_t __weak plat_scmi_perf_count(unsigned int channel_id __unused) ++{ ++ return 0; ++} ++ ++void __weak *plat_scmi_perf_statistics_buf(unsigned int channel_id __unused, ++ size_t *stats_len) ++{ ++ *stats_len = 0; ++ ++ return NULL; ++} ++ ++const char __weak *plat_scmi_perf_domain_name(unsigned int channel_id __unused, ++ unsigned int domain_id __unused) ++{ ++ return NULL; ++} ++ ++int32_t __weak plat_scmi_perf_levels_array(unsigned int channel_id __unused, ++ unsigned int domain_id __unused, ++ size_t start_index __unused, ++ unsigned int *elt __unused, ++ size_t *nb_elts __unused) ++{ ++ return SCMI_NOT_SUPPORTED; ++} ++ ++int32_t __weak plat_scmi_perf_level_latency_us(unsigned int channel_id __unused, ++ unsigned int domain_id __unused, ++ unsigned int level __unused, ++ unsigned int *latency __unused) ++{ ++ return SCMI_NOT_SUPPORTED; ++} ++ ++int32_t __weak plat_scmi_perf_level_power_cost(unsigned int channel_id __unused, ++ unsigned int domain_id __unused, ++ unsigned int level __unused, ++ unsigned int *cost __unused) ++{ ++ return SCMI_NOT_SUPPORTED; ++} ++ ++int32_t __weak plat_scmi_perf_level_get(unsigned int channel_id __unused, ++ unsigned int domain_id __unused, ++ unsigned int *level __unused) ++{ ++ return SCMI_NOT_SUPPORTED; ++} ++ ++int32_t __weak plat_scmi_perf_level_set(unsigned int channel_id __unused, ++ unsigned int domain_id __unused, ++ unsigned int level __unused) ++{ ++ return SCMI_NOT_SUPPORTED; ++} ++ ++static void protocol_version(struct scmi_msg *msg) ++{ ++ struct scmi_protocol_version_p2a return_values = { ++ .status = SCMI_SUCCESS, ++ .version = SCMI_PROTOCOL_VERSION_PERF_DOMAIN, ++ }; ++ ++ VERBOSE_MSG("SCMI perf %#"PRIx32, SCMI_PROTOCOL_VERSION_PERF_DOMAIN); ++ ++ if (msg->in_size) { ++ scmi_status_response(msg, SCMI_PROTOCOL_ERROR); ++ return; ++ } ++ ++ scmi_write_response(msg, &return_values, sizeof(return_values)); ++} ++ ++static void protocol_attributes(struct scmi_msg *msg) ++{ ++ unsigned int channel_id = msg->channel_id; ++ size_t count = plat_scmi_perf_count(channel_id); ++ uint32_t power_in_mw = 0; ++ struct scmi_perf_protocol_attributes_p2a return_values = { ++ .status = SCMI_SUCCESS, ++ .attributes = SCMI_PERF_PROTOCOL_ATTRIBUTES(power_in_mw, count), ++ }; ++ void *stats_buf = NULL; ++ size_t stats_len = 0; ++ ++ VERBOSE_MSG("channel %u: %zu performance domains", channel_id, count); ++ ++ if (msg->in_size) { ++ scmi_status_response(msg, SCMI_PROTOCOL_ERROR); ++ return; ++ } ++ ++ stats_buf = plat_scmi_perf_statistics_buf(channel_id, &stats_len); ++ if (stats_len) { ++ paddr_t stats_pa = virt_to_phys(stats_buf); ++ ++ if (stats_pa && tee_vbuf_is_non_sec(stats_buf, stats_len)) { ++ return_values.statistics_len = stats_len; ++ reg_pair_from_64((uint64_t)stats_pa, ++ &return_values.statistics_address_high, ++ &return_values.statistics_address_low); ++ } else { ++ IMSG("Disable SCMI perf statistics: invalid buffer"); ++ DMSG("Stats buffer va %p, pa %#"PRIxPA", size %zu", ++ stats_buf, stats_pa, stats_len); ++ } ++ } ++ ++ scmi_write_response(msg, &return_values, sizeof(return_values)); ++} ++ ++static void protocol_message_attributes(struct scmi_msg *msg) ++{ ++ struct scmi_protocol_message_attributes_a2p *in_args = (void *)msg->in; ++ struct scmi_protocol_message_attributes_p2a return_values = { ++ .status = SCMI_SUCCESS, ++ /* For this protocol, attributes shall be zero */ ++ .attributes = 0, ++ }; ++ ++ if (msg->in_size != sizeof(*in_args)) { ++ scmi_status_response(msg, SCMI_PROTOCOL_ERROR); ++ return; ++ } ++ ++ if (!message_id_is_supported(in_args->message_id)) { ++ scmi_status_response(msg, SCMI_NOT_FOUND); ++ return; ++ } ++ ++ scmi_write_response(msg, &return_values, sizeof(return_values)); ++} ++ ++static int32_t sanitize_message(struct scmi_msg *msg, unsigned int *domain_id, ++ size_t exp_in_size) ++{ ++ size_t domain_count = plat_scmi_perf_count(msg->channel_id); ++ ++ if (msg->in_size != exp_in_size) ++ return SCMI_PROTOCOL_ERROR; ++ ++ if (*domain_id >= domain_count) ++ return SCMI_INVALID_PARAMETERS; ++ ++ *domain_id = confine_array_index(*domain_id, domain_count); ++ ++ return SCMI_SUCCESS; ++} ++ ++static void scmi_perf_domain_attributes(struct scmi_msg *msg) ++{ ++ const struct scmi_perf_attributes_a2p *in_args = (void *)msg->in; ++ /* It is safe to read in_args->domain_id before sanitize_message() */ ++ unsigned int domain_id = in_args->domain_id; ++ int32_t status = SCMI_GENERIC_ERROR; ++ struct scmi_perf_attributes_p2a return_values = { ++ .attributes = SCMI_PERF_DOMAIN_ATTRIBUTES_CAN_SET_LEVEL, ++ .status = SCMI_SUCCESS, ++ }; ++ const char *name = NULL; ++ ++ FMSG("channel %u: domain %u", msg->channel_id, domain_id); ++ ++ status = sanitize_message(msg, &domain_id, sizeof(*in_args)); ++ if (status) { ++ scmi_status_response(msg, status); ++ return; ++ } ++ ++ name = plat_scmi_perf_domain_name(msg->channel_id, domain_id); ++ if (!name) { ++ scmi_status_response(msg, SCMI_NOT_FOUND); ++ return; ++ } ++ ++ COPY_NAME_IDENTIFIER(return_values.name, name); ++ ++ /* ++ * .rate_limit, .sustained_freq and .sustained_perf_level are ++ * implicitly set to 0. ++ */ ++ ++ VERBOSE_MSG("channel %u: domain %u: name \"%s\"", msg->channel_id, ++ domain_id, name); ++ ++ scmi_write_response(msg, &return_values, sizeof(return_values)); ++} ++ ++static void scmi_perf_level_get(struct scmi_msg *msg) ++{ ++ const struct scmi_perf_level_get_a2p *in_args = (void *)msg->in; ++ /* It is safe to read in_args->domain_id before sanitize_message() */ ++ unsigned int domain_id = in_args->domain_id; ++ int32_t status = SCMI_GENERIC_ERROR; ++ unsigned int level = 0; ++ struct scmi_perf_level_get_p2a return_values = { ++ .status = SCMI_SUCCESS, ++ }; ++ ++ VERBOSE_MSG("channel %u, domain %u", msg->channel_id, domain_id); ++ ++ status = sanitize_message(msg, &domain_id, sizeof(*in_args)); ++ if (status) { ++ scmi_status_response(msg, status); ++ return; ++ } ++ ++ status = plat_scmi_perf_level_get(msg->channel_id, domain_id, &level); ++ if (status) { ++ scmi_status_response(msg, status); ++ return; ++ } ++ ++ assert(level <= UINT32_MAX); ++ return_values.performance_level = level; ++ ++ VERBOSE_MSG("channel %u, domain %u: level %u", msg->channel_id, ++ domain_id, level); ++ ++ scmi_write_response(msg, &return_values, sizeof(return_values)); ++} ++ ++static void scmi_perf_level_set(struct scmi_msg *msg) ++{ ++ const struct scmi_perf_level_set_a2p *in_args = (void *)msg->in; ++ unsigned int channel_id = msg->channel_id; ++ /* It is safe to read in in_args before sanitize_message() */ ++ unsigned int domain_id = in_args->domain_id; ++ unsigned int level = in_args->performance_level; ++ int32_t status = SCMI_GENERIC_ERROR; ++ ++ VERBOSE_MSG("channel %u, domain %u: set level %u", channel_id, ++ domain_id, level); ++ ++ status = sanitize_message(msg, &domain_id, sizeof(*in_args)); ++ if (status == SCMI_SUCCESS) ++ status = plat_scmi_perf_level_set(channel_id, domain_id, level); ++ ++ scmi_status_response(msg, status); ++} ++ ++/* List levels array by small chunks fitting in SCMI message max payload size */ ++#define LEVELS_ARRAY_SIZE \ ++ ((SCMI_PLAYLOAD_MAX - sizeof(struct scmi_perf_describe_levels_a2p)) / \ ++ sizeof(struct scmi_perf_level)) ++ ++static void scmi_perf_describe_levels(struct scmi_msg *msg) ++{ ++ const struct scmi_perf_describe_levels_a2p *in_args = (void *)msg->in; ++ size_t nb_levels = 0; ++ /* It is safe to read in_args->domain_id before sanitize_message() */ ++ unsigned int domain_id = in_args->domain_id; ++ int32_t status = SCMI_GENERIC_ERROR; ++ /* Use the stack to get the returned a portion of the level array */ ++ unsigned int plat_levels[LEVELS_ARRAY_SIZE] = { 0 }; ++ size_t ret_nb = 0; ++ size_t rem_nb = 0; ++ ++ VERBOSE_MSG("channel %u, domain %u", msg->channel_id, domain_id); ++ ++ status = sanitize_message(msg, &domain_id, sizeof(*in_args)); ++ if (status) ++ goto err; ++ ++ status = plat_scmi_perf_levels_array(msg->channel_id, domain_id, 0, ++ NULL, &nb_levels); ++ if (status) ++ goto err; ++ ++ if (in_args->level_index >= nb_levels) { ++ status = SCMI_INVALID_PARAMETERS; ++ goto err; ++ } ++ ++ ret_nb = MIN(ARRAY_SIZE(plat_levels), nb_levels - in_args->level_index); ++ rem_nb = nb_levels - in_args->level_index - ret_nb; ++ ++ status = plat_scmi_perf_levels_array(msg->channel_id, domain_id, ++ in_args->level_index, plat_levels, ++ &ret_nb); ++ ++ if (status == SCMI_SUCCESS) { ++ struct scmi_perf_describe_levels_p2a p2a = { }; ++ struct scmi_perf_level *levels = NULL; ++ size_t n = 0; ++ ++ p2a.status = SCMI_SUCCESS; ++ p2a.num_levels = SCMI_PERF_NUM_LEVELS(ret_nb, rem_nb); ++ memcpy(msg->out, &p2a, sizeof(p2a)); ++ ++ /* By construction these values are 32bit aligned */ ++ levels = (void *)(uintptr_t)(msg->out + sizeof(p2a)); ++ ++ for (n = 0; n < ret_nb; n++) { ++ unsigned int latency = 0; ++ unsigned int power_cost = 0; ++ int32_t rc = SCMI_SUCCESS; ++ ++ rc = plat_scmi_perf_level_latency_us(msg->channel_id, ++ domain_id, ++ plat_levels[n], ++ &latency); ++ if (rc != SCMI_SUCCESS && rc != SCMI_NOT_SUPPORTED) ++ panic(); ++ ++ latency &= SCMI_PERF_LEVEL_ATTRIBUTES_LATENCY_US_MASK; ++ ++ rc = plat_scmi_perf_level_power_cost(msg->channel_id, ++ domain_id, ++ plat_levels[n], ++ &power_cost); ++ if (rc != SCMI_SUCCESS && rc != SCMI_NOT_SUPPORTED) ++ panic(); ++ ++ levels[n] = (struct scmi_perf_level){ ++ .performance_level = plat_levels[n], ++ .power_cost = power_cost, ++ .attributes = latency, ++ }; ++ } ++ ++ msg->out_size_out = ++ sizeof(p2a) + ret_nb * sizeof(struct scmi_perf_level); ++ ++ return; ++ } ++ ++err: ++ assert(status); ++ scmi_status_response(msg, status); ++} ++ ++static const scmi_msg_handler_t scmi_perf_handler_table[] = { ++ [SCMI_PROTOCOL_VERSION] = protocol_version, ++ [SCMI_PROTOCOL_ATTRIBUTES] = protocol_attributes, ++ [SCMI_PROTOCOL_MESSAGE_ATTRIBUTES] = protocol_message_attributes, ++ [SCMI_PERF_DOMAIN_ATTRIBUTES] = scmi_perf_domain_attributes, ++ [SCMI_PERF_DESCRIBE_LEVELS] = scmi_perf_describe_levels, ++ [SCMI_PERF_LEVEL_SET] = scmi_perf_level_set, ++ [SCMI_PERF_LEVEL_GET] = scmi_perf_level_get, ++}; ++ ++static bool message_id_is_supported(size_t message_id) ++{ ++ return message_id < ARRAY_SIZE(scmi_perf_handler_table) && ++ scmi_perf_handler_table[message_id]; ++} ++ ++scmi_msg_handler_t scmi_msg_get_perf_handler(struct scmi_msg *msg) ++{ ++ const size_t array_size = ARRAY_SIZE(scmi_perf_handler_table); ++ unsigned int message_id = 0; ++ ++ if (msg->message_id >= array_size) { ++ DMSG("handle not found %u", msg->message_id); ++ return NULL; ++ } ++ ++ message_id = confine_array_index(msg->message_id, array_size); ++ ++ return scmi_perf_handler_table[message_id]; ++} +diff --git a/core/drivers/scmi-msg/perf_domain.h b/core/drivers/scmi-msg/perf_domain.h +new file mode 100644 +index 000000000..a36d0f2af +--- /dev/null ++++ b/core/drivers/scmi-msg/perf_domain.h +@@ -0,0 +1,139 @@ ++/* SPDX-License-Identifier: BSD-3-Clause */ ++/* ++ * Copyright (c) 2015-2020, Arm Limited and Contributors. All rights reserved. ++ * Copyright (c) 2021, Linaro Limited ++ */ ++ ++#ifndef SCMI_MSG_PERF_DOMAIN_H ++#define SCMI_MSG_PERF_DOMAIN_H ++ ++#include ++#include ++ ++#include "common.h" ++ ++#define SCMI_PROTOCOL_VERSION_PERF_DOMAIN 0x10000 ++ ++/* ++ * Identifiers of the SCMI Performance Domain Management Protocol commands ++ */ ++enum scmi_perf_domain_command_id { ++ SCMI_PERF_DOMAIN_ATTRIBUTES = 0x3, ++ SCMI_PERF_DESCRIBE_LEVELS = 0x4, ++ SCMI_PERF_LIMITS_SET = 0x5, /* Not supported */ ++ SCMI_PERF_LIMITS_GET = 0x6, /* Not supported */ ++ SCMI_PERF_LEVEL_SET = 0x7, ++ SCMI_PERF_LEVEL_GET = 0x8, ++ SCMI_PERF_NOTIFY_LIMITS = 0x9, /* Not supported */ ++ SCMI_PERF_NOTIFY_LEVEL = 0xa, /* Not supported */ ++}; ++ ++/* ++ * Payloads for SCMI_PROTOCOL_ATTRIBUTES for Performance Domains ++ */ ++#define SCMI_PERF_ATTRIBUTES_POWER_MW_BIT BIT(16) ++#define SCMI_PERF_ATTRIBUTES_NUM_DOMAINS_MASK GENMASK_32(15, 0) ++ ++#define SCMI_PERF_PROTOCOL_ATTRIBUTES(_power_mw, _num_domains) \ ++ (((_power_mw) ? SCMI_PERF_ATTRIBUTES_POWER_MW_BIT : 0) | \ ++ ((_num_domains) & SCMI_PERF_ATTRIBUTES_NUM_DOMAINS_MASK)) ++ ++struct scmi_perf_protocol_attributes_p2a { ++ int32_t status; ++ uint32_t attributes; ++ uint32_t statistics_address_low; ++ uint32_t statistics_address_high; ++ uint32_t statistics_len; ++}; ++ ++/* ++ * Payloads for SCMI_PERF_DOMAIN_ATTRIBUTES ++ */ ++struct scmi_perf_attributes_a2p { ++ uint32_t domain_id; ++}; ++ ++/* Macro for scmi_perf_domain_attributes_p2a:attributes */ ++#define SCMI_PERF_DOMAIN_ATTRIBUTES_CAN_SET_LEVEL BIT(30) ++ ++/* Macro for scmi_perf_domain_attributes_p2a:rate_limit */ ++#define SCMI_PERF_DOMAIN_RATE_LIMIT_MASK GENMASK_32(15, 0) ++ ++/* Macro for scmi_perf_domain_attributes_p2a:name */ ++#define SCMI_PERF_DOMAIN_ATTR_NAME_SZ 16 ++ ++struct scmi_perf_attributes_p2a { ++ int32_t status; ++ uint32_t attributes; ++ uint32_t rate_limit; ++ uint32_t sustained_freq; ++ uint32_t sustained_perf_level; ++ char name[SCMI_PERF_DOMAIN_ATTR_NAME_SZ]; ++}; ++ ++/* ++ * Payloads for SCMI_PERF_DESCRIBE_LEVELS ++ */ ++#define SCMI_PERF_LEVEL_ATTRIBUTES_LATENCY_US_MASK GENMASK_32(15, 0) ++ ++struct scmi_perf_level { ++ uint32_t performance_level; ++ uint32_t power_cost; ++ uint32_t attributes; ++}; ++ ++struct scmi_perf_describe_levels_a2p { ++ uint32_t domain_id; ++ uint32_t level_index; ++}; ++ ++#define SCMI_PERF_NUM_LEVELS_NUM_LEVELS_MASK GENMASK_32(11, 0) ++#define SCMI_PERF_NUM_LEVELS_REMAINING_LEVELS_MASK GENMASK_32(31, 16) ++#define SCMI_PERF_NUM_LEVELS_REMAINING_LEVELS_POS 16 ++ ++#define SCMI_PERF_NUM_LEVELS(_num_levels, _rem_levels) \ ++ (((_num_levels) & SCMI_PERF_NUM_LEVELS_NUM_LEVELS_MASK) | \ ++ (((_rem_levels) << SCMI_PERF_NUM_LEVELS_REMAINING_LEVELS_POS) & \ ++ SCMI_PERF_NUM_LEVELS_REMAINING_LEVELS_MASK)) ++ ++struct scmi_perf_describe_levels_p2a { ++ int32_t status; ++ uint32_t num_levels; ++ struct scmi_perf_level perf_levels[]; ++}; ++ ++/* Payloads for SCMI_PERF_LEVEL_SET */ ++struct scmi_perf_level_set_a2p { ++ uint32_t domain_id; ++ uint32_t performance_level; ++}; ++ ++struct scmi_perf_level_set_p2a { ++ int32_t status; ++}; ++ ++/* Payloads for SCMI_PERF_LEVEL_GET */ ++struct scmi_perf_level_get_a2p { ++ uint32_t domain_id; ++}; ++ ++struct scmi_perf_level_get_p2a { ++ int32_t status; ++ uint32_t performance_level; ++}; ++ ++#ifdef CFG_SCMI_MSG_PERF_DOMAIN ++/* ++ * scmi_msg_get_perf_handler - Return a handler for a performance domain message ++ * @msg - message to process ++ * Return a function handler for the message or NULL ++ */ ++scmi_msg_handler_t scmi_msg_get_perf_handler(struct scmi_msg *msg); ++#else ++static inline ++scmi_msg_handler_t scmi_msg_get_perf_handler(struct scmi_msg *msg __unused) ++{ ++ return NULL; ++} ++#endif ++#endif /* SCMI_MSG_PERF_DOMAIN_H */ +diff --git a/core/drivers/scmi-msg/regulator_consumer.c b/core/drivers/scmi-msg/regulator_consumer.c +new file mode 100644 +index 000000000..c5fd22686 +--- /dev/null ++++ b/core/drivers/scmi-msg/regulator_consumer.c +@@ -0,0 +1,354 @@ ++// SPDX-License-Identifier: BSD-3-Clause ++/* ++ * Copyright (c) 2020-2021, STMicroelectronics ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#define VERBOSE_VOLTD(_chan_id, _dom_id, fmt, ...) \ ++ FMSG("(channel %u/domain %u) " fmt, (_chan_id), (_dom_id), __VA_ARGS__) ++ ++struct scmi_voltd { ++ uint32_t domain_id; ++ struct rdev *rdev; ++ bool enabled; ++}; ++ ++struct scmi_voltd_channel { ++ unsigned int channel_id; ++ struct scmi_voltd *voltd_array; ++ size_t voltd_count; ++}; ++ ++struct scmi_voltd_channel *voltd_channel; ++static size_t voltd_channel_count; ++ ++static struct scmi_voltd_channel *channel_from_id(unsigned int channel_id) ++{ ++ size_t i = 0; ++ ++ for (i = 0; i < voltd_channel_count; i++) ++ if (channel_id == voltd_channel[i].channel_id) ++ return voltd_channel + i; ++ ++ return NULL; ++} ++ ++static struct scmi_voltd *voltd_from_id(unsigned int channel_id, ++ unsigned int domain_id) ++{ ++ struct scmi_voltd_channel *chan = channel_from_id(channel_id); ++ ++ if (!chan || domain_id >= chan->voltd_count) ++ return NULL; ++ ++ return chan->voltd_array + domain_id; ++} ++ ++size_t plat_scmi_voltd_count(unsigned int channel_id) ++{ ++ struct scmi_voltd_channel *chan = channel_from_id(channel_id); ++ ++ if (chan) ++ return chan->voltd_count; ++ ++ return 0; ++} ++ ++const char *plat_scmi_voltd_get_name(unsigned int channel_id, ++ unsigned int domain_id) ++{ ++ struct scmi_voltd *voltd = voltd_from_id(channel_id, domain_id); ++ ++ if (!voltd || !voltd->rdev) ++ return NULL; ++ ++ return voltd->rdev->reg_name; ++} ++ ++int32_t plat_scmi_voltd_levels_array(unsigned int channel_id, ++ unsigned int domain_id, ++ size_t start_index, ++ long *microvolt, size_t *nb_elts) ++{ ++ struct scmi_voltd *voltd = voltd_from_id(channel_id, domain_id); ++ TEE_Result res = TEE_ERROR_GENERIC; ++ uint16_t *levels = NULL; ++ size_t full_count = 0; ++ size_t out_count = 0; ++ size_t i = 0; ++ ++ if (!voltd) ++ return SCMI_NOT_FOUND; ++ if (!voltd->rdev) ++ return SCMI_DENIED; ++ ++ res = regulator_list_voltages(voltd->rdev, &levels, &full_count); ++ if (res) ++ return SCMI_GENERIC_ERROR; ++ ++ if (!microvolt) { ++ *nb_elts = full_count - start_index; ++ ++ return SCMI_SUCCESS; ++ } ++ ++ if (SUB_OVERFLOW(full_count, start_index, &out_count)) ++ return SCMI_GENERIC_ERROR; ++ ++ out_count = MIN(out_count, *nb_elts); ++ ++ VERBOSE_VOLTD(channel_id, domain_id, ++ "%zu levels, start %zu req %zu out %zu", ++ full_count, start_index, *nb_elts, out_count); ++ ++ for (i = 0; i < out_count; i++) ++ microvolt[i] = (long)levels[start_index + i] * 1000; ++ ++ *nb_elts = out_count; ++ ++ return SCMI_SUCCESS; ++} ++ ++long plat_scmi_voltd_get_level(unsigned int channel_id, ++ unsigned int domain_id) ++{ ++ struct scmi_voltd *voltd = voltd_from_id(channel_id, domain_id); ++ uint16_t level_mv = 0; ++ ++ if (!voltd) ++ return SCMI_NOT_FOUND; ++ if (!voltd->rdev) ++ return SCMI_DENIED; ++ ++ if (regulator_get_voltage(voltd->rdev, &level_mv)) ++ return SCMI_GENERIC_ERROR; ++ ++ VERBOSE_VOLTD(channel_id, domain_id, "get voltage = %lduV", ++ (long)level_mv * 1000); ++ ++ return (long)level_mv * 1000; ++} ++ ++int32_t plat_scmi_voltd_set_level(unsigned int channel_id, ++ unsigned int domain_id, ++ long microvolt) ++{ ++ struct scmi_voltd *voltd = voltd_from_id(channel_id, domain_id); ++ ++ if (!voltd) ++ return SCMI_NOT_FOUND; ++ if (!voltd->rdev) ++ return SCMI_DENIED; ++ ++ VERBOSE_VOLTD(channel_id, domain_id, "set voltage to %lduV", microvolt); ++ ++ if (regulator_set_voltage(voltd->rdev, microvolt / 1000)) ++ return SCMI_GENERIC_ERROR; ++ ++ return SCMI_SUCCESS; ++} ++ ++int32_t plat_scmi_voltd_get_config(unsigned int channel_id, ++ unsigned int domain_id, ++ uint32_t *config) ++{ ++ struct scmi_voltd *voltd = voltd_from_id(channel_id, domain_id); ++ ++ if (!voltd) ++ return SCMI_NOT_FOUND; ++ if (!voltd->rdev) ++ return SCMI_DENIED; ++ ++ if (voltd->enabled) ++ *config = SCMI_VOLTAGE_DOMAIN_CONFIG_ARCH_ON; ++ else ++ *config = SCMI_VOLTAGE_DOMAIN_CONFIG_ARCH_OFF; ++ ++ VERBOSE_VOLTD(channel_id, domain_id, "config %#"PRIx32, *config); ++ ++ return SCMI_SUCCESS; ++} ++ ++int32_t plat_scmi_voltd_set_config(unsigned int channel_id, ++ unsigned int domain_id, ++ uint32_t config) ++{ ++ struct scmi_voltd *voltd = voltd_from_id(channel_id, domain_id); ++ ++ if (!voltd) ++ return SCMI_NOT_FOUND; ++ if (!voltd->rdev) ++ return SCMI_DENIED; ++ ++ VERBOSE_VOLTD(channel_id, domain_id, "set config to %u", config); ++ ++ switch (config) { ++ case SCMI_VOLTAGE_DOMAIN_CONFIG_ARCH_ON: ++ if (!voltd->enabled) { ++ if (voltd->rdev->flags & REGUL_ALWAYS_ON) ++ return SCMI_SUCCESS; ++ ++ if (regulator_enable(voltd->rdev)) ++ return SCMI_GENERIC_ERROR; ++ ++ voltd->enabled = true; ++ } ++ ++ return TEE_SUCCESS; ++ case SCMI_VOLTAGE_DOMAIN_CONFIG_ARCH_OFF: ++ if (voltd->enabled) { ++ if (voltd->rdev->flags & REGUL_ALWAYS_ON) ++ return SCMI_SUCCESS; ++ ++ if (regulator_disable(voltd->rdev)) ++ return SCMI_GENERIC_ERROR; ++ ++ voltd->enabled = false; ++ } ++ ++ return SCMI_SUCCESS; ++ default: ++ return SCMI_INVALID_PARAMETERS; ++ } ++} ++ ++static TEE_Result scmi_regulator_consumer_init(void) ++{ ++ int node = 0; ++ int subnode = 0; ++ void *fdt = NULL; ++ bool using_reg = false; ++ bool using_name = false; ++ const fdt32_t *cuint = NULL; ++ unsigned int channel_id = 0; ++ struct scmi_voltd_channel *chan = NULL; ++ ++ fdt = get_embedded_dt(); ++ if (!fdt) ++ return TEE_SUCCESS; ++ ++ node = fdt_node_offset_by_compatible(fdt, -1, ++ "st,scmi-regulator-consumer"); ++ if (node < 0) ++ return TEE_SUCCESS; ++ ++ /* ++ * Currently supports a single device ++ * TODO: add a reference to the SCMI agent/channel ++ */ ++ if (fdt_node_offset_by_compatible(fdt, node, ++ "st,scmi-regulator-consumer") != ++ -FDT_ERR_NOTFOUND) ++ panic(); ++ ++ if (node < 0) ++ return TEE_SUCCESS; ++ ++ cuint = fdt_getprop(fdt, node, "scmi-channel-id", NULL); ++ if (!cuint) { ++ DMSG("Missing property scmi-channel-id"); ++ panic(); ++ } ++ channel_id = fdt32_to_cpu(*cuint); ++ ++ assert(!voltd_channel_count && !voltd_channel); ++ voltd_channel_count = 1; ++ voltd_channel = calloc(voltd_channel_count, sizeof(*voltd_channel)); ++ assert(voltd_channel); ++ ++ chan = voltd_channel; ++ chan->channel_id = channel_id; ++ ++ fdt_for_each_subnode(subnode, fdt, node) { ++ struct scmi_voltd *voltd = NULL; ++ struct rdev *rdev = NULL; ++ const char *name = NULL; ++ uint32_t domain_id = 0; ++ ++ name = fdt_getprop(fdt, subnode, "voltd-name", NULL); ++ if (!name) ++ continue; ++ ++ /* Check that the given voltage domain name is unique */ ++ for (voltd = chan->voltd_array; ++ voltd < chan->voltd_array + chan->voltd_count; voltd++) { ++ if (voltd->rdev && ++ !strcmp(voltd->rdev->reg_name, name)) { ++ EMSG("voltd-name %s re-used", name); ++ return TEE_ERROR_GENERIC; ++ } ++ } ++ ++ rdev = regulator_get_by_regulator_name(name); ++ if (!rdev) { ++ EMSG("regulator %s not found, skipped", name); ++ continue; ++ } ++ ++ /* Get an ID for the domain */ ++ cuint = fdt_getprop(fdt, subnode, "reg", NULL); ++ if (cuint) { ++ domain_id = fdt32_to_cpu(*cuint); ++ using_reg = true; ++ } else { ++ domain_id = chan->voltd_count; ++ using_name = true; ++ } ++ ++ if (using_reg && using_name) { ++ EMSG("Mixed reg usage in device-tree"); ++ return TEE_ERROR_GENERIC; ++ } ++ ++ /* Realloc voltd_array with zeroing new cells */ ++ voltd = calloc(chan->voltd_count + 1, sizeof(*voltd)); ++ if (!voltd) ++ panic(); ++ ++ memcpy(voltd, chan->voltd_array, ++ chan->voltd_count * sizeof(*voltd)); ++ chan->voltd_array = voltd; ++ chan->voltd_count++; ++ ++ voltd = chan->voltd_array + domain_id; ++ ++ /* Check that the domain_id is not already used */ ++ if (voltd->rdev) { ++ EMSG("Domain ID %"PRIu32" already used", domain_id); ++ return TEE_ERROR_GENERIC; ++ } ++ ++ voltd->rdev = rdev; ++ ++ /* ++ * Synchronize SCMI regulator current configuration ++ * Boot-on can be disabled by non secure ++ * Always-on can not be updated but status will be synchronized ++ * in non secure. ++ */ ++ if (rdev->flags & REGUL_BOOT_ON || ++ rdev->flags & REGUL_ALWAYS_ON) { ++ if (regulator_enable(rdev)) ++ IMSG("Failed to enable SCMI regul"); ++ else ++ voltd->enabled = true; ++ } ++ ++ DMSG("scmi voltd shares %s (node %s) on domain ID %"PRIu32, ++ rdev->reg_name, rdev->desc->node_name, domain_id); ++ } ++ ++ return TEE_SUCCESS; ++} ++driver_init_late(scmi_regulator_consumer_init); +diff --git a/core/drivers/scmi-msg/sub.mk b/core/drivers/scmi-msg/sub.mk +index a02a8f38e..7b08916ea 100644 +--- a/core/drivers/scmi-msg/sub.mk ++++ b/core/drivers/scmi-msg/sub.mk +@@ -1,6 +1,8 @@ + srcs-y += base.c + srcs-$(CFG_SCMI_MSG_CLOCK) += clock.c + srcs-y += entry.c ++srcs-$(CFG_SCMI_MSG_PERF_DOMAIN) += perf_domain.c ++srcs-$(CFG_SCMI_MSG_REGULATOR_CONSUMER) += regulator_consumer.c + srcs-$(CFG_SCMI_MSG_RESET_DOMAIN) += reset_domain.c + srcs-$(CFG_SCMI_MSG_SMT) += smt.c + srcs-$(CFG_SCMI_MSG_VOLTAGE_DOMAIN) += voltage_domain.c +diff --git a/core/drivers/stm32_bsec.c b/core/drivers/stm32_bsec.c +index 88b60c7f2..69b8a84cc 100644 +--- a/core/drivers/stm32_bsec.c ++++ b/core/drivers/stm32_bsec.c +@@ -10,6 +10,7 @@ + #include + #include + #include ++#include + #include + #include + #include +@@ -21,69 +22,74 @@ + #include + #include + ++#ifdef CFG_STM32MP13 ++#define DT_BSEC_COMPAT "st,stm32mp13-bsec" ++#endif ++#ifdef CFG_STM32MP15 ++#define DT_BSEC_COMPAT "st,stm32mp15-bsec" ++#endif ++ + #define BSEC_OTP_MASK GENMASK_32(4, 0) +-#define BSEC_OTP_BANK_SHIFT 5 ++#define BSEC_OTP_BANK_SHIFT U(5) + + /* Permanent lock bitmasks */ +-#define ADDR_LOWER_OTP_PERLOCK_SHIFT 3 +-#define DATA_LOWER_OTP_PERLOCK_BIT 3 +-#define DATA_LOWER_OTP_PERLOCK_MASK GENMASK_32(2, 0) +-#define ADDR_UPPER_OTP_PERLOCK_SHIFT 4 +-#define DATA_UPPER_OTP_PERLOCK_BIT 1 +-#define DATA_UPPER_OTP_PERLOCK_MASK GENMASK_32(3, 0) ++#define DATA_LOWER_OTP_PERLOCK_BIT U(3) ++#define DATA_UPPER_OTP_PERLOCK_BIT U(1) + + /* BSEC register offset */ +-#define BSEC_OTP_CONF_OFF 0x000U +-#define BSEC_OTP_CTRL_OFF 0x004U +-#define BSEC_OTP_WRDATA_OFF 0x008U +-#define BSEC_OTP_STATUS_OFF 0x00CU +-#define BSEC_OTP_LOCK_OFF 0x010U +-#define BSEC_DEN_OFF 0x014U +-#define BSEC_FEN_OFF 0x018U +-#define BSEC_DISTURBED_OFF 0x01CU +-#define BSEC_DISTURBED1_OFF 0x020U +-#define BSEC_DISTURBED2_OFF 0x024U +-#define BSEC_ERROR_OFF 0x034U +-#define BSEC_ERROR1_OFF 0x038U +-#define BSEC_ERROR2_OFF 0x03CU +-#define BSEC_WRLOCK_OFF 0x04CU +-#define BSEC_WRLOCK1_OFF 0x050U +-#define BSEC_WRLOCK2_OFF 0x054U +-#define BSEC_SPLOCK_OFF 0x064U +-#define BSEC_SPLOCK1_OFF 0x068U +-#define BSEC_SPLOCK2_OFF 0x06CU +-#define BSEC_SWLOCK_OFF 0x07CU +-#define BSEC_SWLOCK1_OFF 0x080U +-#define BSEC_SWLOCK2_OFF 0x084U +-#define BSEC_SRLOCK_OFF 0x094U +-#define BSEC_SRLOCK1_OFF 0x098U +-#define BSEC_SRLOCK2_OFF 0x09CU +-#define BSEC_JTAG_IN_OFF 0x0ACU +-#define BSEC_JTAG_OUT_OFF 0x0B0U +-#define BSEC_SCRATCH_OFF 0x0B4U +-#define BSEC_OTP_DATA_OFF 0x200U +-#define BSEC_IPHW_CFG_OFF 0xFF0U +-#define BSEC_IPVR_OFF 0xFF4U +-#define BSEC_IP_ID_OFF 0xFF8U +-#define BSEC_IP_MAGIC_ID_OFF 0xFFCU ++#define BSEC_OTP_CONF_OFF U(0x000) ++#define BSEC_OTP_CTRL_OFF U(0x004) ++#define BSEC_OTP_WRDATA_OFF U(0x008) ++#define BSEC_OTP_STATUS_OFF U(0x00C) ++#define BSEC_OTP_LOCK_OFF U(0x010) ++#define BSEC_DEN_OFF U(0x014) ++#define BSEC_FEN_OFF U(0x018) ++#define BSEC_DISTURBED_OFF U(0x01C) ++#define BSEC_DISTURBED1_OFF U(0x020) ++#define BSEC_DISTURBED2_OFF U(0x024) ++#define BSEC_ERROR_OFF U(0x034) ++#define BSEC_ERROR1_OFF U(0x038) ++#define BSEC_ERROR2_OFF U(0x03C) ++#define BSEC_WRLOCK_OFF U(0x04C) ++#define BSEC_WRLOCK1_OFF U(0x050) ++#define BSEC_WRLOCK2_OFF U(0x054) ++#define BSEC_SPLOCK_OFF U(0x064) ++#define BSEC_SPLOCK1_OFF U(0x068) ++#define BSEC_SPLOCK2_OFF U(0x06C) ++#define BSEC_SWLOCK_OFF U(0x07C) ++#define BSEC_SWLOCK1_OFF U(0x080) ++#define BSEC_SWLOCK2_OFF U(0x084) ++#define BSEC_SRLOCK_OFF U(0x094) ++#define BSEC_SRLOCK1_OFF U(0x098) ++#define BSEC_SRLOCK2_OFF U(0x09C) ++#define BSEC_JTAG_IN_OFF U(0x0AC) ++#define BSEC_JTAG_OUT_OFF U(0x0B0) ++#define BSEC_SCRATCH_OFF U(0x0B4) ++#define BSEC_OTP_DATA_OFF U(0x200) ++#define BSEC_IPHW_CFG_OFF U(0xFF0) ++#define BSEC_IPVR_OFF U(0xFF4) ++#define BSEC_IP_ID_OFF U(0xFF8) ++#define BSEC_IP_MAGIC_ID_OFF U(0xFFC) + + /* BSEC_CONFIGURATION Register */ + #define BSEC_CONF_POWER_UP_MASK BIT(0) +-#define BSEC_CONF_POWER_UP_SHIFT 0 ++#define BSEC_CONF_POWER_UP_SHIFT U(0) + #define BSEC_CONF_FRQ_MASK GENMASK_32(2, 1) +-#define BSEC_CONF_FRQ_SHIFT 1 ++#define BSEC_CONF_FRQ_SHIFT U(1) + #define BSEC_CONF_PRG_WIDTH_MASK GENMASK_32(6, 3) +-#define BSEC_CONF_PRG_WIDTH_SHIFT 3 ++#define BSEC_CONF_PRG_WIDTH_SHIFT U(3) + #define BSEC_CONF_TREAD_MASK GENMASK_32(8, 7) +-#define BSEC_CONF_TREAD_SHIFT 7 ++#define BSEC_CONF_TREAD_SHIFT U(7) + + /* BSEC_CONTROL Register */ +-#define BSEC_READ 0x000U +-#define BSEC_WRITE 0x100U +-#define BSEC_LOCK 0x200U ++#define BSEC_READ U(0x000) ++#define BSEC_WRITE U(0x100) ++#define BSEC_LOCK U(0x200) + + /* BSEC_STATUS Register */ + #define BSEC_MODE_STATUS_MASK GENMASK_32(2, 0) ++#define BSEC_MODE_SECURED BIT(0) ++#define BSEC_MODE_INVALID BIT(2) + #define BSEC_MODE_BUSY_MASK BIT(3) + #define BSEC_MODE_PROGFAIL_MASK BIT(4) + #define BSEC_MODE_PWR_MASK BIT(5) +@@ -91,24 +97,18 @@ + #define BSEC_MODE_BIST2_LOCK_MASK BIT(7) + + /* BSEC_DEBUG */ +-#define BSEC_HDPEN BIT(4) +-#define BSEC_SPIDEN BIT(5) +-#define BSEC_SPINDEN BIT(6) +-#define BSEC_DBGSWGEN BIT(10) + #define BSEC_DEN_ALL_MSK GENMASK_32(10, 0) + + /* + * OTP Lock services definition + * Value must corresponding to the bit position in the register + */ +-#define BSEC_LOCK_UPPER_OTP 0x00 +-#define BSEC_LOCK_DEBUG 0x02 +-#define BSEC_LOCK_PROGRAM 0x04 ++#define BSEC_LOCK_UPPER_OTP U(0x00) ++#define BSEC_LOCK_DEBUG U(0x02) ++#define BSEC_LOCK_PROGRAM U(0x04) + + /* Timeout when polling on status */ +-#define BSEC_TIMEOUT_US 10000 +- +-#define BITS_PER_WORD (CHAR_BIT * sizeof(uint32_t)) ++#define BSEC_TIMEOUT_US U(10000) + + struct bsec_dev { + struct io_pa_va base; +@@ -161,6 +161,33 @@ static uint32_t bsec_status(void) + return io_read32(bsec_base() + BSEC_OTP_STATUS_OFF); + } + ++static bool is_invalid_mode(void) ++{ ++ return (bsec_status() & BSEC_MODE_INVALID) != 0; ++} ++ ++static bool is_secured_mode(void) ++{ ++ return (bsec_status() & BSEC_MODE_SECURED) != 0; ++} ++ ++static bool is_closed_mode(void) ++{ ++ uint32_t otp_cfg = 0; ++ uint32_t close_mode = 0; ++ TEE_Result res = TEE_ERROR_GENERIC; ++ const uint32_t mask = CFG0_CLOSED_MASK; ++ ++ res = stm32_bsec_find_otp_in_nvmem_layout("cfg0_otp", &otp_cfg, NULL); ++ if (res) ++ panic("CFG0 OTP not found"); ++ ++ if (stm32_bsec_read_otp(&close_mode, otp_cfg)) ++ panic("Unable to read OTP"); ++ ++ return (close_mode & mask) == mask; ++} ++ + /* + * Check that BSEC interface does not report an error + * @otp_id : OTP number +@@ -238,6 +265,9 @@ TEE_Result stm32_bsec_shadow_register(uint32_t otp_id) + if (locked) + DMSG("BSEC shadow warning: OTP locked"); + ++ if (is_invalid_mode()) ++ return TEE_ERROR_SECURITY; ++ + exceptions = bsec_lock(); + + result = power_up_safmem(); +@@ -269,6 +299,9 @@ TEE_Result stm32_bsec_read_otp(uint32_t *value, uint32_t otp_id) + if (otp_id > otp_max_id()) + return TEE_ERROR_BAD_PARAMETERS; + ++ if (is_invalid_mode()) ++ return TEE_ERROR_SECURITY; ++ + *value = io_read32(bsec_base() + BSEC_OTP_DATA_OFF + + (otp_id * sizeof(uint32_t))); + +@@ -307,6 +340,9 @@ TEE_Result stm32_bsec_write_otp(uint32_t value, uint32_t otp_id) + if (locked) + DMSG("BSEC write warning: OTP locked"); + ++ if (is_invalid_mode()) ++ return TEE_ERROR_SECURITY; ++ + exceptions = bsec_lock(); + + io_write32(otp_data_base + (otp_id * sizeof(uint32_t)), value); +@@ -335,6 +371,9 @@ TEE_Result stm32_bsec_program_otp(uint32_t value, uint32_t otp_id) + if (io_read32(bsec_base() + BSEC_OTP_LOCK_OFF) & BIT(BSEC_LOCK_PROGRAM)) + DMSG("BSEC program warning: GPLOCK activated"); + ++ if (is_invalid_mode()) ++ return TEE_ERROR_SECURITY; ++ + exceptions = bsec_lock(); + + result = power_up_safmem(); +@@ -373,20 +412,26 @@ TEE_Result stm32_bsec_permanent_lock_otp(uint32_t otp_id) + uint32_t exceptions = 0; + vaddr_t base = bsec_base(); + uint64_t timeout_ref = 0; ++ uint32_t upper_base = otp_upper_base(); + + if (otp_id > otp_max_id()) + return TEE_ERROR_BAD_PARAMETERS; + +- if (otp_id < otp_upper_base()) { +- addr = otp_id >> ADDR_LOWER_OTP_PERLOCK_SHIFT; +- data = DATA_LOWER_OTP_PERLOCK_BIT << +- ((otp_id & DATA_LOWER_OTP_PERLOCK_MASK) << 1U); ++ /* ++ * 2 bits word for lower OTPs, 1 bit per word for upper OTPs ++ * and only 16 bits used in WRDATA: lower=8 OTPs by word, upper=16 ++ */ ++ if (otp_id < upper_base) { ++ addr = otp_id / 8U; ++ data = DATA_LOWER_OTP_PERLOCK_BIT << ((otp_id * 2U) & 0xF); + } else { +- addr = (otp_id >> ADDR_UPPER_OTP_PERLOCK_SHIFT) + 2U; +- data = DATA_UPPER_OTP_PERLOCK_BIT << +- (otp_id & DATA_UPPER_OTP_PERLOCK_MASK); ++ addr = upper_base / 8U + (otp_id - upper_base) / 16U; ++ data = DATA_UPPER_OTP_PERLOCK_BIT << (otp_id & 0xF); + } + ++ if (is_invalid_mode()) ++ return TEE_ERROR_SECURITY; ++ + exceptions = bsec_lock(); + + result = power_up_safmem(); +@@ -408,6 +453,10 @@ TEE_Result stm32_bsec_permanent_lock_otp(uint32_t otp_id) + else + result = check_no_error(otp_id, false /* not-disturbed */); + ++#ifdef CFG_STM32MP13 ++ io_write32(base + BSEC_OTP_CTRL_OFF, addr | BSEC_READ | BSEC_LOCK); ++#endif ++ + power_down_safmem(); + + out: +@@ -416,13 +465,15 @@ out: + return result; + } + +-#ifdef CFG_STM32_BSEC_WRITE + TEE_Result stm32_bsec_write_debug_conf(uint32_t value) + { + TEE_Result result = TEE_ERROR_GENERIC; + uint32_t masked_val = value & BSEC_DEN_ALL_MSK; + uint32_t exceptions = 0; + ++ if (is_invalid_mode()) ++ return TEE_ERROR_SECURITY; ++ + exceptions = bsec_lock(); + + io_write32(bsec_base() + BSEC_DEN_OFF, value); +@@ -434,7 +485,6 @@ TEE_Result stm32_bsec_write_debug_conf(uint32_t value) + + return result; + } +-#endif /*CFG_STM32_BSEC_WRITE*/ + + uint32_t stm32_bsec_read_debug_conf(void) + { +@@ -451,6 +501,9 @@ static TEE_Result set_bsec_lock(uint32_t otp_id, size_t lock_offset) + if (otp_id > STM32MP1_OTP_MAX_ID) + return TEE_ERROR_BAD_PARAMETERS; + ++ if (is_invalid_mode()) ++ return TEE_ERROR_SECURITY; ++ + exceptions = bsec_lock(); + + io_write32(lock_addr, otp_mask); +@@ -485,6 +538,9 @@ static TEE_Result read_bsec_lock(uint32_t otp_id, bool *locked, + if (otp_id > STM32MP1_OTP_MAX_ID) + return TEE_ERROR_BAD_PARAMETERS; + ++ if (is_invalid_mode()) ++ return TEE_ERROR_SECURITY; ++ + *locked = (io_read32(lock_addr) & otp_mask) != 0; + + return TEE_SUCCESS; +@@ -514,6 +570,9 @@ TEE_Result stm32_bsec_otp_lock(uint32_t service) + { + vaddr_t addr = bsec_base() + BSEC_OTP_LOCK_OFF; + ++ if (is_invalid_mode()) ++ return TEE_ERROR_SECURITY; ++ + switch (service) { + case BSEC_LOCK_UPPER_OTP: + io_write32(addr, BIT(BSEC_LOCK_UPPER_OTP)); +@@ -535,7 +594,7 @@ static size_t nsec_access_array_size(void) + { + size_t upper_count = otp_max_id() - otp_upper_base() + 1; + +- return ROUNDUP(upper_count, BITS_PER_WORD) / BITS_PER_WORD; ++ return ROUNDUP_DIV(upper_count, BSEC_BITS_PER_WORD); + } + + static bool nsec_access_granted(unsigned int index) +@@ -543,8 +602,20 @@ static bool nsec_access_granted(unsigned int index) + uint32_t *array = bsec_dev.nsec_access; + + return array && +- (index / BITS_PER_WORD) < nsec_access_array_size() && +- array[index / BITS_PER_WORD] & BIT(index % BITS_PER_WORD); ++ (index / BSEC_BITS_PER_WORD) < nsec_access_array_size() && ++ array[index / BSEC_BITS_PER_WORD] & ++ BIT(index % BSEC_BITS_PER_WORD); ++} ++ ++bool stm32_bsec_can_access_otp(uint32_t otp_id) ++{ ++ if (otp_id > otp_max_id()) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ if (is_invalid_mode()) ++ return TEE_ERROR_SECURITY; ++ ++ return TEE_SUCCESS; + } + + bool stm32_bsec_nsec_can_access_otp(uint32_t otp_id) +@@ -553,10 +624,91 @@ bool stm32_bsec_nsec_can_access_otp(uint32_t otp_id) + nsec_access_granted(otp_id - otp_upper_base()); + } + ++struct nvmem_layout { ++ char *name; ++ uint32_t otp_id; ++ size_t bit_len; ++}; ++ ++static struct nvmem_layout *nvmem_layout; ++static size_t nvmem_layout_count; ++ ++TEE_Result stm32_bsec_find_otp_in_nvmem_layout(const char *name, ++ uint32_t *otp_id, ++ size_t *otp_bit_len) ++{ ++ size_t i = 0; ++ ++ if (!name) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ for (i = 0; i < nvmem_layout_count; i++) { ++ if (!nvmem_layout[i].name || strcmp(name, nvmem_layout[i].name)) ++ continue; ++ ++ if (otp_id) ++ *otp_id = nvmem_layout[i].otp_id; ++ ++ if (otp_bit_len) ++ *otp_bit_len = nvmem_layout[i].bit_len; ++ ++ DMSG("nvmem %s = %zu: %"PRId32" %zu", name, i, ++ nvmem_layout[i].otp_id, nvmem_layout[i].bit_len); ++ ++ return TEE_SUCCESS; ++ } ++ ++ DMSG("nvmem %s failed", name); ++ ++ return TEE_ERROR_ITEM_NOT_FOUND; ++} ++ ++TEE_Result stm32_bsec_get_state(uint32_t *state) ++{ ++ uint32_t otp_enc_id = 0; ++ size_t otp_bit_len = 0; ++ TEE_Result res = TEE_SUCCESS; ++ ++ if (!state) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ if (is_invalid_mode() || !is_secured_mode()) { ++ *state = BSEC_STATE_INVALID; ++ } else { ++ if (is_closed_mode()) ++ *state = BSEC_STATE_SEC_CLOSED; ++ else ++ *state = BSEC_STATE_SEC_OPEN; ++ } ++ ++ if (!IS_ENABLED(CFG_STM32MP13)) ++ return TEE_SUCCESS; ++ ++ res = stm32_bsec_find_otp_in_nvmem_layout("oem_enc_key", ++ &otp_enc_id, &otp_bit_len); ++ if (!res && otp_bit_len) { ++ unsigned int start = otp_enc_id / BSEC_BITS_PER_WORD; ++ size_t otp_nb = ROUNDUP_DIV(otp_bit_len, BSEC_BITS_PER_WORD); ++ unsigned int idx = 0; ++ ++ for (idx = start; idx < start + otp_nb; idx++) { ++ bool locked = false; ++ ++ res = stm32_bsec_read_sp_lock(idx, &locked); ++ if (res || !locked) ++ return TEE_SUCCESS; ++ } ++ ++ *state |= BSEC_HARDWARE_KEY; ++ } ++ ++ return TEE_SUCCESS; ++} ++ + #ifdef CFG_EMBED_DTB + static void enable_nsec_access(unsigned int otp_id) + { +- unsigned int idx = (otp_id - otp_upper_base()) / BITS_PER_WORD; ++ unsigned int idx = (otp_id - otp_upper_base()) / BSEC_BITS_PER_WORD; + + if (otp_id < otp_upper_base()) + return; +@@ -564,7 +716,7 @@ static void enable_nsec_access(unsigned int otp_id) + if (otp_id > otp_max_id() || stm32_bsec_shadow_register(otp_id)) + panic(); + +- bsec_dev.nsec_access[idx] |= BIT(otp_id % BITS_PER_WORD); ++ bsec_dev.nsec_access[idx] |= BIT(otp_id % BSEC_BITS_PER_WORD); + } + + static void bsec_dt_otp_nsec_access(void *fdt, int bsec_node) +@@ -615,15 +767,40 @@ static void bsec_dt_otp_nsec_access(void *fdt, int bsec_node) + } + } + +- if (!fdt_getprop(fdt, bsec_subnode, "st,non-secure-otp", NULL)) ++ /* Handle different kinds of non-secure accesses */ ++ if (fdt_getprop(fdt, bsec_subnode, ++ "st,non-secure-otp-provisioning", NULL)) { ++ uint32_t read_otp_value = 0; ++ bool locked = false; ++ ++ /* Check if write of OTP is locked */ ++ if (stm32_bsec_read_sw_lock(otp_id, &locked)) ++ panic("BSEC: Couldn't read sw lock at init"); ++ ++ if (locked) { ++ DMSG("BSEC: OTP locked"); ++ continue; ++ } ++ ++ /* Check if fuses are empty */ ++ if (stm32_bsec_read_otp(&read_otp_value, otp_id)) ++ panic("Couldn't check if fuses are empty"); ++ ++ if (read_otp_value) { ++ DMSG("Fuses not empty"); ++ continue; ++ } ++ } else if (!fdt_getprop(fdt, bsec_subnode, "st,non-secure-otp", ++ NULL)) { + continue; ++ } + + if ((offset % sizeof(uint32_t)) || (length % sizeof(uint32_t))) + panic("Unaligned non-secure OTP"); + + size = length / sizeof(uint32_t); + +- if (otp_id + size > STM32MP1_OTP_MAX_ID) ++ if (otp_id + size > OTP_MAX_SIZE) + panic("OTP range oversized"); + + for (i = otp_id; i < otp_id + size; i++) +@@ -631,6 +808,68 @@ static void bsec_dt_otp_nsec_access(void *fdt, int bsec_node) + } + } + ++static void save_dt_nvmem_layout(void *fdt, int bsec_node) ++{ ++ int cell_max = 0; ++ int cell_cnt = 0; ++ int node = 0; ++ ++ fdt_for_each_subnode(node, fdt, bsec_node) ++ cell_max++; ++ if (!cell_max) ++ return; ++ ++ nvmem_layout = calloc(cell_max, sizeof(*nvmem_layout)); ++ if (!nvmem_layout) ++ panic(); ++ ++ fdt_for_each_subnode(node, fdt, bsec_node) { ++ const fdt32_t *cuint = NULL; ++ const char *string = NULL; ++ const char *s = NULL; ++ int len = 0; ++ struct nvmem_layout *layout_cell = &nvmem_layout[cell_cnt]; ++ ++ string = fdt_get_name(fdt, node, &len); ++ if (!string || !len) ++ continue; ++ ++ cuint = fdt_getprop(fdt, node, "reg", &len); ++ if (!cuint || (len != (2 * (int)sizeof(uint32_t)))) { ++ IMSG("Malformed nvmem %s: ignored", string); ++ continue; ++ } ++ ++ if (fdt32_to_cpu(*cuint) % sizeof(uint32_t)) { ++ DMSG("Misaligned nvmem %s: ignored", string); ++ continue; ++ } ++ layout_cell->otp_id = fdt32_to_cpu(*cuint) / sizeof(uint32_t); ++ layout_cell->bit_len = fdt32_to_cpu(*(cuint + 1)) * CHAR_BIT; ++ ++ s = strchr(string, '@'); ++ if (s) ++ len = s - string; ++ ++ layout_cell->name = calloc(1, len + 1); ++ if (!layout_cell->name) ++ panic(); ++ ++ memcpy(layout_cell->name, string, len); ++ cell_cnt++; ++ DMSG("nvmem[%d] = %s %"PRId32" %zu", cell_cnt, ++ layout_cell->name, layout_cell->otp_id, layout_cell->bit_len); ++ } ++ ++ if (cell_cnt != cell_max) { ++ nvmem_layout = realloc(nvmem_layout, cell_cnt * sizeof(*nvmem_layout)); ++ if (!nvmem_layout) ++ panic(); ++ } ++ ++ nvmem_layout_count = cell_cnt; ++} ++ + static void initialize_bsec_from_dt(void) + { + void *fdt = NULL; +@@ -638,7 +877,7 @@ static void initialize_bsec_from_dt(void) + struct dt_node_info bsec_info = { }; + + fdt = get_embedded_dt(); +- node = fdt_node_offset_by_compatible(fdt, 0, "st,stm32mp15-bsec"); ++ node = fdt_node_offset_by_compatible(fdt, 0, DT_BSEC_COMPAT); + if (node < 0) + panic(); + +@@ -649,6 +888,8 @@ static void initialize_bsec_from_dt(void) + panic(); + + bsec_dt_otp_nsec_access(fdt, node); ++ ++ save_dt_nvmem_layout(fdt, node); + } + #else + static void initialize_bsec_from_dt(void) +@@ -656,6 +897,21 @@ static void initialize_bsec_from_dt(void) + } + #endif /*CFG_EMBED_DTB*/ + ++static TEE_Result bsec_pm(enum pm_op op, uint32_t pm_hint __unused, ++ const struct pm_callback_handle *hdl __unused) ++{ ++ static uint32_t debug_conf; ++ ++ if (op == PM_OP_SUSPEND) ++ debug_conf = stm32_bsec_read_debug_conf(); ++ ++ if (op == PM_OP_RESUME) ++ stm32_bsec_write_debug_conf(debug_conf); ++ ++ return TEE_SUCCESS; ++} ++DECLARE_KEEP_PAGER(bsec_pm); ++ + static TEE_Result initialize_bsec(void) + { + struct stm32_bsec_static_cfg cfg = { }; +@@ -666,9 +922,14 @@ static TEE_Result initialize_bsec(void) + bsec_dev.upper_base = cfg.upper_start; + bsec_dev.max_id = cfg.max_id; + ++ if (is_invalid_mode()) ++ panic(); ++ + if (IS_ENABLED(CFG_EMBED_DTB)) + initialize_bsec_from_dt(); + ++ register_pm_core_service_cb(bsec_pm, NULL, "bsec-service"); ++ + return TEE_SUCCESS; + } + +diff --git a/core/drivers/stm32_etzpc.c b/core/drivers/stm32_etzpc.c +deleted file mode 100644 +index 9b7163051..000000000 +--- a/core/drivers/stm32_etzpc.c ++++ /dev/null +@@ -1,346 +0,0 @@ +-// SPDX-License-Identifier: BSD-3-Clause +-/* +- * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved. +- * Copyright (c) 2017-2019, STMicroelectronics +- */ +- +-/* +- * STM32 ETPZC acts as a firewall on stm32mp SoC peripheral interfaces and +- * internal memories. The driver expects a single instance of the controller +- * in the platform. +- * +- * The driver API is defined in header file stm32_etzpc.h. +- * +- * Driver registers a PM callback for restoration of the access permissions +- * when it resumes. +- */ +- +-#include +-#include +-#include +-#include +-#include +-#include +-#include +-#include +-#include +-#include +-#include +-#include +- +-/* Devicetree compatibility */ +-#define ETZPC_COMPAT "st,stm32-etzpc" +- +-/* ID Registers */ +-#define ETZPC_TZMA0_SIZE 0x000U +-#define ETZPC_DECPROT0 0x010U +-#define ETZPC_DECPROT_LOCK0 0x030U +-#define ETZPC_HWCFGR 0x3F0U +-#define ETZPC_VERR 0x3F4U +- +-/* ID Registers fields */ +-#define ETZPC_TZMA0_SIZE_LOCK BIT(31) +-#define ETZPC_DECPROT0_MASK GENMASK_32(1, 0) +-#define ETZPC_HWCFGR_NUM_TZMA_MASK GENMASK_32(7, 0) +-#define ETZPC_HWCFGR_NUM_TZMA_SHIFT 0 +-#define ETZPC_HWCFGR_NUM_PER_SEC_MASK GENMASK_32(15, 8) +-#define ETZPC_HWCFGR_NUM_PER_SEC_SHIFT 8 +-#define ETZPC_HWCFGR_NUM_AHB_SEC_MASK GENMASK_32(23, 16) +-#define ETZPC_HWCFGR_NUM_AHB_SEC_SHIFT 16 +-#define ETZPC_HWCFGR_CHUNCKS1N4_MASK GENMASK_32(31, 24) +-#define ETZPC_HWCFGR_CHUNCKS1N4_SHIFT 24 +- +-#define DECPROT_SHIFT 1 +-#define IDS_PER_DECPROT_REGS 16U +-#define IDS_PER_DECPROT_LOCK_REGS 32U +- +-/* +- * Implementation uses uint8_t to store each securable DECPROT configuration +- * and uint16_t to store each securable TZMA configuration. When resuming +- * from deep suspend, the DECPROT configurations are restored. +- */ +-#define PERIPH_PM_LOCK_BIT BIT(7) +-#define PERIPH_PM_ATTR_MASK GENMASK_32(2, 0) +-#define TZMA_PM_LOCK_BIT BIT(15) +-#define TZMA_PM_VALUE_MASK GENMASK_32(9, 0) +- +-/* +- * @base - iobase for interface base address +- * @num_tzma - number of TZMA zone, read from the hardware +- * @num_ahb_sec - number of securable AHB master zone, read from the hardware +- * @num_per_sec - number of securable AHB & APB periphs, read from the hardware +- * @periph_cfg - Backup for restoring DECPROT when resuming (PERIH_PM_*) +- * @tzma_cfg - Backup for restoring TZMA when resuming (TZMA_PM_*) +- */ +-struct etzpc_instance { +- struct io_pa_va base; +- unsigned int num_tzma; +- unsigned int num_per_sec; +- unsigned int num_ahb_sec; +- uint8_t *periph_cfg; +- uint16_t *tzma_cfg; +-}; +- +-/* Only 1 instance of the ETZPC is expected per platform */ +-static struct etzpc_instance etzpc_dev; +- +-static vaddr_t etzpc_base(void) +-{ +- return io_pa_or_va_secure(&etzpc_dev.base, 1); +-} +- +-static bool __maybe_unused valid_decprot_id(unsigned int id) +-{ +- return id < etzpc_dev.num_per_sec; +-} +- +-static bool __maybe_unused valid_tzma_id(unsigned int id) +-{ +- return id < etzpc_dev.num_tzma; +-} +- +-void etzpc_configure_decprot(uint32_t decprot_id, +- enum etzpc_decprot_attributes decprot_attr) +-{ +- size_t offset = 4U * (decprot_id / IDS_PER_DECPROT_REGS); +- uint32_t shift = (decprot_id % IDS_PER_DECPROT_REGS) << DECPROT_SHIFT; +- uint32_t masked_decprot = (uint32_t)decprot_attr & ETZPC_DECPROT0_MASK; +- vaddr_t base = etzpc_base(); +- +- assert(valid_decprot_id(decprot_id)); +- +- io_clrsetbits32(base + ETZPC_DECPROT0 + offset, +- ETZPC_DECPROT0_MASK << shift, +- masked_decprot << shift); +- +- /* Save for PM */ +- assert((decprot_attr & ~PERIPH_PM_ATTR_MASK) == 0); +- COMPILE_TIME_ASSERT(ETZPC_DECPROT_MAX <= UINT8_MAX); +- +- etzpc_dev.periph_cfg[decprot_id] &= ~PERIPH_PM_ATTR_MASK; +- etzpc_dev.periph_cfg[decprot_id] |= (uint8_t)decprot_attr; +-} +- +-enum etzpc_decprot_attributes etzpc_get_decprot(uint32_t decprot_id) +-{ +- size_t offset = 4U * (decprot_id / IDS_PER_DECPROT_REGS); +- uint32_t shift = (decprot_id % IDS_PER_DECPROT_REGS) << DECPROT_SHIFT; +- vaddr_t base = etzpc_base(); +- uint32_t value = 0; +- +- assert(valid_decprot_id(decprot_id)); +- +- value = (io_read32(base + ETZPC_DECPROT0 + offset) >> shift) & +- ETZPC_DECPROT0_MASK; +- +- return (enum etzpc_decprot_attributes)value; +-} +- +-void etzpc_lock_decprot(uint32_t decprot_id) +-{ +- size_t offset = 4U * (decprot_id / IDS_PER_DECPROT_LOCK_REGS); +- uint32_t mask = BIT(decprot_id % IDS_PER_DECPROT_LOCK_REGS); +- vaddr_t base = etzpc_base(); +- +- assert(valid_decprot_id(decprot_id)); +- +- io_write32(base + offset + ETZPC_DECPROT_LOCK0, mask); +- +- /* Save for PM */ +- etzpc_dev.periph_cfg[decprot_id] |= PERIPH_PM_LOCK_BIT; +-} +- +-bool etzpc_get_lock_decprot(uint32_t decprot_id) +-{ +- size_t offset = 4U * (decprot_id / IDS_PER_DECPROT_LOCK_REGS); +- uint32_t mask = BIT(decprot_id % IDS_PER_DECPROT_LOCK_REGS); +- vaddr_t base = etzpc_base(); +- +- assert(valid_decprot_id(decprot_id)); +- +- return io_read32(base + offset + ETZPC_DECPROT_LOCK0) & mask; +-} +- +-void etzpc_configure_tzma(uint32_t tzma_id, uint16_t tzma_value) +-{ +- size_t offset = sizeof(uint32_t) * tzma_id; +- vaddr_t base = etzpc_base(); +- +- assert(valid_tzma_id(tzma_id)); +- +- io_write32(base + ETZPC_TZMA0_SIZE + offset, tzma_value); +- +- /* Save for PM */ +- assert((tzma_value & ~TZMA_PM_VALUE_MASK) == 0); +- etzpc_dev.tzma_cfg[tzma_id] &= ~TZMA_PM_VALUE_MASK; +- etzpc_dev.tzma_cfg[tzma_id] |= tzma_value; +-} +- +-uint16_t etzpc_get_tzma(uint32_t tzma_id) +-{ +- size_t offset = sizeof(uint32_t) * tzma_id; +- vaddr_t base = etzpc_base(); +- +- assert(valid_tzma_id(tzma_id)); +- +- return io_read32(base + ETZPC_TZMA0_SIZE + offset); +-} +- +-void etzpc_lock_tzma(uint32_t tzma_id) +-{ +- size_t offset = sizeof(uint32_t) * tzma_id; +- vaddr_t base = etzpc_base(); +- +- assert(valid_tzma_id(tzma_id)); +- +- io_setbits32(base + ETZPC_TZMA0_SIZE + offset, ETZPC_TZMA0_SIZE_LOCK); +- +- /* Save for PM */ +- etzpc_dev.tzma_cfg[tzma_id] |= TZMA_PM_LOCK_BIT; +-} +- +-bool etzpc_get_lock_tzma(uint32_t tzma_id) +-{ +- size_t offset = sizeof(uint32_t) * tzma_id; +- vaddr_t base = etzpc_base(); +- +- assert(valid_tzma_id(tzma_id)); +- +- return io_read32(base + ETZPC_TZMA0_SIZE + offset) & +- ETZPC_TZMA0_SIZE_LOCK; +-} +- +-static TEE_Result etzpc_pm(enum pm_op op, unsigned int pm_hint __unused, +- const struct pm_callback_handle *pm_handle) +-{ +- struct etzpc_instance *dev = NULL; +- unsigned int n = 0; +- +- if (op != PM_OP_RESUME) +- return TEE_SUCCESS; +- +- dev = (struct etzpc_instance *)PM_CALLBACK_GET_HANDLE(pm_handle); +- +- for (n = 0; n < dev->num_per_sec; n++) { +- unsigned int attr = dev->periph_cfg[n] & PERIPH_PM_ATTR_MASK; +- +- etzpc_configure_decprot(n, (enum etzpc_decprot_attributes)attr); +- +- if (dev->periph_cfg[n] & PERIPH_PM_LOCK_BIT) +- etzpc_lock_decprot(n); +- } +- +- for (n = 0; n < dev->num_tzma; n++) { +- uint16_t value = dev->tzma_cfg[n] & TZMA_PM_VALUE_MASK; +- +- etzpc_configure_tzma(n, value); +- +- if (dev->tzma_cfg[n] & TZMA_PM_LOCK_BIT) +- etzpc_lock_tzma(n); +- } +- +- return TEE_SUCCESS; +-} +-DECLARE_KEEP_PAGER(etzpc_pm); +- +-static void init_pm(struct etzpc_instance *dev) +-{ +- unsigned int n = 0; +- +- dev->periph_cfg = calloc(dev->num_per_sec, sizeof(*dev->periph_cfg)); +- dev->tzma_cfg = calloc(dev->num_tzma, sizeof(*dev->tzma_cfg)); +- if (!dev->periph_cfg || !dev->tzma_cfg) +- panic(); +- +- for (n = 0; n < dev->num_per_sec; n++) { +- dev->periph_cfg[n] = (uint8_t)etzpc_get_decprot(n); +- if (etzpc_get_lock_decprot(n)) +- dev->periph_cfg[n] |= PERIPH_PM_LOCK_BIT; +- } +- +- for (n = 0; n < dev->num_tzma; n++) { +- dev->tzma_cfg[n] = (uint8_t)etzpc_get_tzma(n); +- if (etzpc_get_lock_tzma(n)) +- dev->tzma_cfg[n] |= TZMA_PM_LOCK_BIT; +- } +- +- register_pm_core_service_cb(etzpc_pm, dev, "stm32-etzpc"); +-} +- +-struct etzpc_hwcfg { +- unsigned int num_tzma; +- unsigned int num_per_sec; +- unsigned int num_ahb_sec; +- unsigned int chunk_size; +-}; +- +-static void get_hwcfg(struct etzpc_hwcfg *hwcfg) +-{ +- uint32_t reg = io_read32(etzpc_base() + ETZPC_HWCFGR); +- +- hwcfg->num_tzma = (reg & ETZPC_HWCFGR_NUM_TZMA_MASK) >> +- ETZPC_HWCFGR_NUM_TZMA_SHIFT; +- hwcfg->num_per_sec = (reg & ETZPC_HWCFGR_NUM_PER_SEC_MASK) >> +- ETZPC_HWCFGR_NUM_PER_SEC_SHIFT; +- hwcfg->num_ahb_sec = (reg & ETZPC_HWCFGR_NUM_AHB_SEC_MASK) >> +- ETZPC_HWCFGR_NUM_AHB_SEC_SHIFT; +- hwcfg->chunk_size = (reg & ETZPC_HWCFGR_CHUNCKS1N4_MASK) >> +- ETZPC_HWCFGR_CHUNCKS1N4_SHIFT; +-} +- +-static void init_device_from_hw_config(struct etzpc_instance *dev, +- paddr_t pbase) +-{ +- struct etzpc_hwcfg hwcfg = { }; +- +- assert(!dev->base.pa && cpu_mmu_enabled()); +- dev->base.pa = pbase; +- dev->base.va = (vaddr_t)phys_to_virt(dev->base.pa, MEM_AREA_IO_SEC, 1); +- assert(etzpc_base()); +- +- get_hwcfg(&hwcfg); +- dev->num_tzma = hwcfg.num_tzma; +- dev->num_per_sec = hwcfg.num_per_sec; +- dev->num_ahb_sec = hwcfg.num_ahb_sec; +- +- DMSG("ETZPC revison 0x02%" PRIu8 ", per_sec %u, ahb_sec %u, tzma %u", +- io_read8(etzpc_base() + ETZPC_VERR), +- hwcfg.num_per_sec, hwcfg.num_ahb_sec, hwcfg.num_tzma); +- +- init_pm(dev); +-} +- +-void stm32_etzpc_init(paddr_t base) +-{ +- init_device_from_hw_config(&etzpc_dev, base); +-} +- +-#ifdef CFG_EMBED_DTB +-static TEE_Result init_etzpc_from_dt(void) +-{ +- void *fdt = get_embedded_dt(); +- int node = fdt_node_offset_by_compatible(fdt, -1, ETZPC_COMPAT); +- int status = 0; +- paddr_t pbase = 0; +- +- /* When using DT, expect one and only one instance, secure enabled */ +- +- if (node < 0) +- panic(); +- assert(fdt_node_offset_by_compatible(fdt, node, ETZPC_COMPAT) < 0); +- +- status = _fdt_get_status(fdt, node); +- if (!(status & DT_STATUS_OK_SEC)) +- panic(); +- +- pbase = _fdt_reg_base_address(fdt, node); +- if (pbase == (paddr_t)-1) +- panic(); +- +- init_device_from_hw_config(&etzpc_dev, pbase); +- +- return TEE_SUCCESS; +-} +- +-service_init(init_etzpc_from_dt); +-#endif /*CFG_EMBED_DTB*/ +diff --git a/core/drivers/stm32_exti.c b/core/drivers/stm32_exti.c +new file mode 100644 +index 000000000..5aaac88f9 +--- /dev/null ++++ b/core/drivers/stm32_exti.c +@@ -0,0 +1,380 @@ ++// SPDX-License-Identifier: BSD-3-Clause ++/* ++ * Copyright (c) 2021, STMicroelectronics ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++/* Registers */ ++#define _EXTI_CR1 0x60U ++#define _EXTI_CR2 0x64U ++#define _EXTI_CR3 0x68U ++#define _EXTI_CR4 0x6CU ++ ++struct stm32_exti_bank { ++ uint32_t imr_ofst; ++ uint32_t rtsr_ofst; ++ uint32_t ftsr_ofst; ++ uint32_t rpr_ofst; ++ uint32_t fpr_ofst; ++ uint32_t tzenr_ofst; ++ uint32_t wakeup; ++ uint32_t imr_cache; ++ uint32_t rtsr_bkp; ++ uint32_t ftsr_bkp; ++ uint32_t tz_bkp; ++}; ++ ++#ifdef CFG_PM ++static uint32_t port_sel_bkp[4]; ++#endif ++ ++struct stm32_exti_priv { ++ vaddr_t base; ++}; ++ ++static struct stm32_exti_priv stm32_exti; ++ ++static struct stm32_exti_bank stm32mp1_exti_b1 = { ++ .imr_ofst = 0x80, ++ .rtsr_ofst = 0x00, ++ .ftsr_ofst = 0x04, ++ .rpr_ofst = 0x0C, ++ .fpr_ofst = 0x10, ++ .tzenr_ofst = 0x14, ++}; ++ ++static struct stm32_exti_bank stm32mp1_exti_b2 = { ++ .imr_ofst = 0x90, ++ .rtsr_ofst = 0x20, ++ .ftsr_ofst = 0x24, ++ .rpr_ofst = 0x2C, ++ .fpr_ofst = 0x30, ++ .tzenr_ofst = 0x34, ++}; ++ ++static struct stm32_exti_bank stm32mp1_exti_b3 = { ++ .imr_ofst = 0xA0, ++ .rtsr_ofst = 0x40, ++ .ftsr_ofst = 0x44, ++ .rpr_ofst = 0x4C, ++ .fpr_ofst = 0x50, ++ .tzenr_ofst = 0x54, ++}; ++ ++static struct stm32_exti_bank *stm32mp1_exti_banks[] = { ++ &stm32mp1_exti_b1, ++ &stm32mp1_exti_b2, ++ &stm32mp1_exti_b3, ++}; ++ ++/* EXTI access protection */ ++static unsigned int lock = SPINLOCK_UNLOCK; ++ ++static uint32_t stm32_exti_get_bank(uint32_t exti_line) ++{ ++ uint32_t bank = 0; ++ ++ if (exti_line <= 31) ++ bank = 0; ++ else if (exti_line <= 63) ++ bank = 1; ++ else if (exti_line <= 95) ++ bank = 2; ++ else ++ panic(); ++ ++ return bank; ++} ++ ++void stm32_exti_set_type(uint32_t exti_line, uint32_t type) ++{ ++ uint32_t mask = BIT(exti_line % 32); ++ uint32_t r_trig = 0; ++ uint32_t f_trig = 0; ++ uint32_t exceptions = 0; ++ unsigned int i = 0; ++ ++ switch (type) { ++ case EXTI_TYPE_RISING: ++ r_trig |= mask; ++ f_trig &= ~mask; ++ break; ++ case EXTI_TYPE_FALLING: ++ r_trig &= ~mask; ++ f_trig |= mask; ++ break; ++ case EXTI_TYPE_BOTH: ++ r_trig |= mask; ++ f_trig |= mask; ++ break; ++ default: ++ panic(); ++ } ++ ++ i = stm32_exti_get_bank(exti_line); ++ ++ exceptions = cpu_spin_lock_xsave(&lock); ++ ++ io_mask32(stm32_exti.base + stm32mp1_exti_banks[i]->rtsr_ofst, r_trig, ++ mask); ++ io_mask32(stm32_exti.base + stm32mp1_exti_banks[i]->ftsr_ofst, f_trig, ++ mask); ++ ++ cpu_spin_unlock_xrestore(&lock, exceptions); ++} ++ ++void stm32_exti_mask(uint32_t exti_line) ++{ ++ uint32_t val = 0; ++ uint32_t mask = BIT(exti_line % 32); ++ uint32_t exceptions = 0; ++ unsigned int i = 0; ++ ++ i = stm32_exti_get_bank(exti_line); ++ ++ exceptions = cpu_spin_lock_xsave(&lock); ++ ++ val = io_read32(stm32_exti.base + stm32mp1_exti_banks[i]->imr_ofst); ++ val &= ~mask; ++ io_write32(stm32_exti.base + stm32mp1_exti_banks[i]->imr_ofst, val); ++ stm32mp1_exti_banks[i]->imr_cache = val; ++ ++ cpu_spin_unlock_xrestore(&lock, exceptions); ++} ++ ++void stm32_exti_unmask(uint32_t exti_line) ++{ ++ uint32_t val = 0; ++ uint32_t mask = BIT(exti_line % 32); ++ uint32_t exceptions = 0; ++ unsigned int i = 0; ++ ++ i = stm32_exti_get_bank(exti_line); ++ ++ exceptions = cpu_spin_lock_xsave(&lock); ++ ++ val = io_read32(stm32_exti.base + stm32mp1_exti_banks[i]->imr_ofst); ++ val |= mask; ++ io_write32(stm32_exti.base + stm32mp1_exti_banks[i]->imr_ofst, val); ++ stm32mp1_exti_banks[i]->imr_cache = val; ++ ++ cpu_spin_unlock_xrestore(&lock, exceptions); ++} ++ ++void stm32_exti_enable_wake(uint32_t exti_line) ++{ ++ uint32_t mask = BIT(exti_line % 32); ++ uint32_t exceptions = 0; ++ unsigned int i = 0; ++ ++ i = stm32_exti_get_bank(exti_line); ++ ++ exceptions = cpu_spin_lock_xsave(&lock); ++ ++ stm32mp1_exti_banks[i]->wakeup |= mask; ++ ++ cpu_spin_unlock_xrestore(&lock, exceptions); ++} ++ ++void stm32_exti_disable_wake(uint32_t exti_line) ++{ ++ uint32_t mask = BIT(exti_line % 32); ++ uint32_t exceptions = 0; ++ unsigned int i = 0; ++ ++ i = stm32_exti_get_bank(exti_line); ++ ++ exceptions = cpu_spin_lock_xsave(&lock); ++ ++ stm32mp1_exti_banks[i]->wakeup &= ~mask; ++ ++ cpu_spin_unlock_xrestore(&lock, exceptions); ++} ++ ++void stm32_exti_clear(uint32_t exti_line) ++{ ++ uint32_t mask = BIT(exti_line % 32); ++ uint32_t exceptions = 0; ++ unsigned int i = 0; ++ ++ i = stm32_exti_get_bank(exti_line); ++ ++ exceptions = cpu_spin_lock_xsave(&lock); ++ ++ io_mask32(stm32_exti.base + stm32mp1_exti_banks[i]->rpr_ofst, mask, ++ mask); ++ io_mask32(stm32_exti.base + stm32mp1_exti_banks[i]->fpr_ofst, mask, ++ mask); ++ ++ cpu_spin_unlock_xrestore(&lock, exceptions); ++} ++ ++void stm32_exti_set_tz(uint32_t exti_line) ++{ ++ uint32_t mask = BIT(exti_line % 32); ++ uint32_t exceptions = 0; ++ unsigned int i = 0; ++ ++ i = stm32_exti_get_bank(exti_line); ++ ++ exceptions = cpu_spin_lock_xsave(&lock); ++ ++ io_mask32(stm32_exti.base + stm32mp1_exti_banks[i]->tzenr_ofst, mask, ++ mask); ++ ++ cpu_spin_unlock_xrestore(&lock, exceptions); ++} ++ ++void stm32_exti_set_gpio_port_sel(uint8_t bank, uint8_t pin) ++{ ++ uint32_t reg = _EXTI_CR1 + (pin / 4) * 4; ++ uint32_t shift = (pin % 4) * 8; ++ uint32_t val = bank << shift; ++ uint32_t mask = 0xff << shift; ++ uint32_t exceptions = 0; ++ ++ exceptions = cpu_spin_lock_xsave(&lock); ++ ++ io_mask32(stm32_exti.base + reg, val, mask); ++ ++ cpu_spin_unlock_xrestore(&lock, exceptions); ++} ++ ++#ifdef CFG_PM ++static void stm32_exti_pm_suspend(void) ++{ ++ uint32_t mask = 0; ++ uint32_t base_cr = 0; ++ uint32_t i = 0; ++ ++ /* Save ftsr, rtsr and tzen registers */ ++ stm32mp1_exti_b1.ftsr_bkp = io_read32(stm32_exti.base + ++ stm32mp1_exti_b1.ftsr_ofst); ++ stm32mp1_exti_b1.rtsr_bkp = io_read32(stm32_exti.base + ++ stm32mp1_exti_b1.rtsr_ofst); ++ stm32mp1_exti_b1.tz_bkp = io_read32(stm32_exti.base + ++ stm32mp1_exti_b1.tzenr_ofst); ++ ++ stm32mp1_exti_b2.ftsr_bkp = io_read32(stm32_exti.base + ++ stm32mp1_exti_b2.ftsr_ofst); ++ stm32mp1_exti_b2.rtsr_bkp = io_read32(stm32_exti.base + ++ stm32mp1_exti_b2.rtsr_ofst); ++ stm32mp1_exti_b2.tz_bkp = io_read32(stm32_exti.base + ++ stm32mp1_exti_b2.tzenr_ofst); ++ ++ stm32mp1_exti_b3.ftsr_bkp = io_read32(stm32_exti.base + ++ stm32mp1_exti_b3.ftsr_ofst); ++ stm32mp1_exti_b3.rtsr_bkp = io_read32(stm32_exti.base + ++ stm32mp1_exti_b3.rtsr_ofst); ++ stm32mp1_exti_b3.tz_bkp = io_read32(stm32_exti.base + ++ stm32mp1_exti_b3.tzenr_ofst); ++ ++ /* Let enabled only the wakeup sources set*/ ++ mask = stm32mp1_exti_b1.wakeup; ++ io_mask32(stm32_exti.base + stm32mp1_exti_b1.imr_ofst, mask, mask); ++ mask = stm32mp1_exti_b2.wakeup; ++ io_mask32(stm32_exti.base + stm32mp1_exti_b2.imr_ofst, mask, mask); ++ mask = stm32mp1_exti_b3.wakeup; ++ io_mask32(stm32_exti.base + stm32mp1_exti_b3.imr_ofst, mask, mask); ++ ++ /* Save EXTI port selection */ ++ for (i = 0; i < 4; i++) { ++ base_cr = stm32_exti.base + _EXTI_CR1; ++ port_sel_bkp[i] = io_read32(base_cr + i * 4); ++ } ++} ++ ++static void stm32_exti_pm_resume(void) ++{ ++ uint32_t base_cr = 0; ++ uint32_t i = 0; ++ ++ /* Restore ftsr and rtsr registers */ ++ io_write32(stm32_exti.base + stm32mp1_exti_b1.ftsr_ofst, ++ stm32mp1_exti_b1.ftsr_bkp); ++ io_write32(stm32_exti.base + stm32mp1_exti_b2.ftsr_ofst, ++ stm32mp1_exti_b2.ftsr_bkp); ++ io_write32(stm32_exti.base + stm32mp1_exti_b3.ftsr_ofst, ++ stm32mp1_exti_b3.ftsr_bkp); ++ io_write32(stm32_exti.base + stm32mp1_exti_b1.rtsr_ofst, ++ stm32mp1_exti_b1.rtsr_bkp); ++ io_write32(stm32_exti.base + stm32mp1_exti_b2.rtsr_ofst, ++ stm32mp1_exti_b2.rtsr_bkp); ++ io_write32(stm32_exti.base + stm32mp1_exti_b3.rtsr_ofst, ++ stm32mp1_exti_b3.rtsr_bkp); ++ io_write32(stm32_exti.base + stm32mp1_exti_b1.tzenr_ofst, ++ stm32mp1_exti_b1.tz_bkp); ++ io_write32(stm32_exti.base + stm32mp1_exti_b2.tzenr_ofst, ++ stm32mp1_exti_b2.tz_bkp); ++ io_write32(stm32_exti.base + stm32mp1_exti_b3.tzenr_ofst, ++ stm32mp1_exti_b3.tz_bkp); ++ ++ /* Restore imr */ ++ io_write32(stm32_exti.base + stm32mp1_exti_b1.imr_ofst, ++ stm32mp1_exti_b1.imr_cache); ++ io_write32(stm32_exti.base + stm32mp1_exti_b2.imr_ofst, ++ stm32mp1_exti_b2.imr_cache); ++ io_write32(stm32_exti.base + stm32mp1_exti_b3.imr_ofst, ++ stm32mp1_exti_b3.imr_cache); ++ ++ /* Restore EXTI port selection */ ++ for (i = 0; i < 4; i++) { ++ base_cr = stm32_exti.base + _EXTI_CR1; ++ io_write32(base_cr + i * 4, port_sel_bkp[i]); ++ } ++} ++ ++static TEE_Result ++stm32_exti_pm(enum pm_op op, unsigned int pm_hint __unused, ++ const struct pm_callback_handle *pm_handle __unused) ++{ ++ if (op == PM_OP_SUSPEND) ++ stm32_exti_pm_suspend(); ++ else ++ stm32_exti_pm_resume(); ++ ++ return TEE_SUCCESS; ++} ++#endif ++ ++#ifdef CFG_EMBED_DTB ++static TEE_Result stm32_exti_probe(const void *fdt, int node, ++ const void *comp_data __unused) ++{ ++ struct dt_node_info dt_info = { }; ++ struct io_pa_va base = { }; ++ ++ _fdt_fill_device_info(fdt, &dt_info, node); ++ ++ base.pa = dt_info.reg; ++ ++ stm32_exti.base = io_pa_or_va_secure(&base, dt_info.reg_size); ++ assert(stm32_exti.base); ++ ++#ifdef CFG_PM ++ register_pm_core_service_cb(stm32_exti_pm, NULL, "stm32-exti"); ++#endif ++ ++ return TEE_SUCCESS; ++} ++ ++static const struct dt_device_match stm32_exti_match_table[] = { ++ { .compatible = "st,stm32mp13-exti" }, ++ { } ++}; ++ ++DEFINE_DT_DRIVER(stm32_exti_dt_driver) = { ++ .name = "stm32-exti", ++ .match_table = stm32_exti_match_table, ++ .probe = &stm32_exti_probe, ++}; ++#endif +diff --git a/core/drivers/stm32_gpio.c b/core/drivers/stm32_gpio.c +index 4c440503b..4d7eb243b 100644 +--- a/core/drivers/stm32_gpio.c ++++ b/core/drivers/stm32_gpio.c +@@ -1,6 +1,6 @@ + // SPDX-License-Identifier: BSD-3-Clause + /* +- * Copyright (c) 2017-2021, STMicroelectronics ++ * Copyright (c) 2017-2022, STMicroelectronics + * + * STM32 GPIO driver is used as pin controller for stm32mp SoCs. + * The driver API is defined in header file stm32_gpio.h. +@@ -10,212 +10,412 @@ + #include + #include + #include ++#include + #include + #include + #include + #include ++#include + #include + #include + #include + #include ++#include + #include + #include + #include + +-#define GPIO_PIN_MAX 15 ++#define GPIO_MODER_OFFSET U(0x00) ++#define GPIO_OTYPER_OFFSET U(0x04) ++#define GPIO_OSPEEDR_OFFSET U(0x08) ++#define GPIO_PUPDR_OFFSET U(0x0c) ++#define GPIO_IDR_OFFSET U(0x10) ++#define GPIO_ODR_OFFSET U(0x14) ++#define GPIO_BSRR_OFFSET U(0x18) ++#define GPIO_AFRL_OFFSET U(0x20) ++#define GPIO_AFRH_OFFSET U(0x24) ++#define GPIO_SECR_OFFSET U(0x30) + +-#define GPIO_MODER_OFFSET 0x00 +-#define GPIO_OTYPER_OFFSET 0x04 +-#define GPIO_OSPEEDR_OFFSET 0x08 +-#define GPIO_PUPDR_OFFSET 0x0c +-#define GPIO_IDR_OFFSET 0x10 +-#define GPIO_ODR_OFFSET 0x14 +-#define GPIO_BSRR_OFFSET 0x18 +-#define GPIO_AFRL_OFFSET 0x20 +-#define GPIO_AFRH_OFFSET 0x24 +-#define GPIO_SECR_OFFSET 0x30 +- +-#define GPIO_ALT_LOWER_LIMIT 0x8 ++#define GPIO_ALT_LOWER_LIMIT U(0x8) + + #define GPIO_MODE_MASK GENMASK_32(1, 0) + #define GPIO_OSPEED_MASK GENMASK_32(1, 0) + #define GPIO_PUPD_PULL_MASK GENMASK_32(1, 0) + #define GPIO_ALTERNATE_MASK GENMASK_32(3, 0) + +-#define DT_GPIO_BANK_SHIFT 12 ++#define DT_GPIO_BANK_SHIFT U(12) + #define DT_GPIO_BANK_MASK GENMASK_32(16, 12) +-#define DT_GPIO_PIN_SHIFT 8 ++#define DT_GPIO_PIN_SHIFT U(8) + #define DT_GPIO_PIN_MASK GENMASK_32(11, 8) + #define DT_GPIO_MODE_MASK GENMASK_32(7, 0) + +-static unsigned int gpio_lock; ++/* Banks are named "GPIOX" with X upper case letter starting from 'A' */ ++#define DT_GPIO_BANK_NAME0 "GPIOA" ++ ++#define PROP_NAME_MAX U(20) ++ ++struct stm32_pinctrl_backup { ++ struct stm32_pinctrl pinctrl; + +-/* Save to output @cfg the current GPIO (@bank/@pin) configuration */ +-static void get_gpio_cfg(uint32_t bank, uint32_t pin, struct gpio_cfg *cfg) ++ STAILQ_ENTRY(stm32_pinctrl_backup) link; ++}; ++ ++STAILQ_HEAD(stm32_pinctrl_backup_head, stm32_pinctrl_backup); ++ ++/** ++ * struct stm32_gpio_bank describes a GPIO bank instance ++ * @base: base address of the GPIO controller registers. ++ * @clock: clock identifier. ++ * @ngpios: number of GPIOs. ++ * @bank_id: Id of the bank. ++ * @lock: lock protecting the GPIO bank access. ++ * @sec_support: True if bank supports pin security protection, otherwise false ++ * @seccfgr: Secure configuration register value. ++ * @link: Link in bank list ++ * @backups: Backup copy of applied pinctrl ++ */ ++struct stm32_gpio_bank { ++ vaddr_t base; ++ struct clk *clock; ++ unsigned int ngpios; ++ unsigned int bank_id; ++ unsigned int lock; ++ bool sec_support; ++ uint32_t seccfgr; ++ struct stm32_pinctrl_backup_head backups; ++ ++ STAILQ_ENTRY(stm32_gpio_bank) link; ++}; ++ ++/** ++ * struct stm32_data_pinctrl - Data used by DT_DRIVER provider interface ++ * @fdt: FDT base address ++ * @phandle: Pinctrl node phandle. ++ */ ++struct stm32_data_pinctrl { ++ const void *fdt; ++ uint32_t phandle; ++}; ++ ++static STAILQ_HEAD(, stm32_gpio_bank) bank_list = ++ STAILQ_HEAD_INITIALIZER(bank_list); ++ ++/** ++ * struct stm32_gpio_compat_data describes GPIO associated data ++ * for compatible list. ++ * ++ * @secure_control: identify gpio security bank capability. ++ */ ++struct stm32_gpio_compat_data { ++ bool secure_control; ++}; ++ ++static const struct stm32_gpio_compat_data stm32_gpio_non_sec = { ++ .secure_control = false, ++}; ++ ++static const struct stm32_gpio_compat_data stm32_gpio_secure_capable = { ++ .secure_control = true, ++}; ++ ++static struct stm32_gpio_bank *stm32_gpio_get_bank(unsigned int bank_id) + { +- vaddr_t base = stm32_get_gpio_bank_base(bank); +- struct clk *clk = stm32_get_gpio_bank_clk(bank); ++ struct stm32_gpio_bank *bank = NULL; + +- clk_enable(clk); ++ STAILQ_FOREACH(bank, &bank_list, link) ++ if (bank_id == bank->bank_id) ++ break; + +- /* +- * Save GPIO configuration bits spread over the few bank registers. +- * 1bit fields are accessed at bit position being the pin index. +- * 2bit fields are accessed at bit position being twice the pin index. +- * 4bit fields are accessed at bit position being fourth the pin index +- * but accessed from 2 32bit registers at incremental addresses. +- */ +- cfg->mode = (io_read32(base + GPIO_MODER_OFFSET) >> (pin << 1)) & +- GPIO_MODE_MASK; ++ return bank; ++} + +- cfg->otype = (io_read32(base + GPIO_OTYPER_OFFSET) >> pin) & 1; ++static int __maybe_unused stm32_gpio_bank_get_count(unsigned int bank_id) ++{ ++ struct stm32_gpio_bank *bank = stm32_gpio_get_bank(bank_id); + +- cfg->ospeed = (io_read32(base + GPIO_OSPEEDR_OFFSET) >> (pin << 1)) & +- GPIO_OSPEED_MASK; ++ if (!bank) { ++ EMSG("Error: can not find GPIO bank %c", bank_id + 'A'); ++ return -1; ++ } + +- cfg->pupd = (io_read32(base + GPIO_PUPDR_OFFSET) >> (pin << 1)) & +- GPIO_PUPD_PULL_MASK; ++ return bank->ngpios; ++} + +- cfg->od = (io_read32(base + GPIO_ODR_OFFSET) >> (pin << 1)) & 1; ++static enum gpio_level ++stm32_gpio_get_level(struct gpio_chip *chip __maybe_unused, ++ unsigned int gpio) ++{ ++ uint32_t bank_id = (gpio & DT_GPIO_BANK_MASK) >> DT_GPIO_BANK_SHIFT; ++ uint32_t pin = (gpio & DT_GPIO_PIN_MASK) >> DT_GPIO_PIN_SHIFT; ++ struct stm32_gpio_bank *bank = stm32_gpio_get_bank(bank_id); ++ enum gpio_level level = GPIO_LEVEL_LOW; ++ ++ if (!bank) { ++ EMSG("Error: can not find GPIO bank %c", bank_id + 'A'); ++ return level; ++ } ++ ++ if (io_read32(bank->base + GPIO_IDR_OFFSET) & BIT(pin)) ++ level = GPIO_LEVEL_HIGH; ++ ++ return level; ++} ++ ++static void stm32_gpio_set_level(struct gpio_chip *chip __maybe_unused, ++ unsigned int gpio, enum gpio_level level) ++{ ++ uint32_t bank_id = (gpio & DT_GPIO_BANK_MASK) >> DT_GPIO_BANK_SHIFT; ++ uint32_t pin = (gpio & DT_GPIO_PIN_MASK) >> DT_GPIO_PIN_SHIFT; ++ struct stm32_gpio_bank *bank = stm32_gpio_get_bank(bank_id); ++ ++ if (!bank) { ++ EMSG("Error: can not find GPIO bank %c", bank_id + 'A'); ++ return; ++ } + +- if (pin < GPIO_ALT_LOWER_LIMIT) +- cfg->af = (io_read32(base + GPIO_AFRL_OFFSET) >> (pin << 2)) & +- GPIO_ALTERNATE_MASK; ++ if (level == GPIO_LEVEL_HIGH) ++ io_write32(bank->base + GPIO_BSRR_OFFSET, BIT(pin)); + else +- cfg->af = (io_read32(base + GPIO_AFRH_OFFSET) >> +- ((pin - GPIO_ALT_LOWER_LIMIT) << 2)) & +- GPIO_ALTERNATE_MASK; ++ io_write32(bank->base + GPIO_BSRR_OFFSET, BIT(pin + 16)); ++} ++ ++static const struct gpio_ops stm32_gpio_ops = { ++ .get_value = stm32_gpio_get_level, ++ .set_value = stm32_gpio_set_level, ++}; ++ ++const struct gpio_ops *stm32_gpio_get_ops(void) ++{ ++ return &stm32_gpio_ops; ++} + +- clk_disable(clk); ++unsigned int stm32_pinctrl_get_gpio_id(struct stm32_pinctrl *pin) ++{ ++ return ((unsigned int)pin->bank << DT_GPIO_BANK_SHIFT) | ++ ((unsigned int)pin->pin << DT_GPIO_PIN_SHIFT); + } + + /* Apply GPIO (@bank/@pin) configuration described by @cfg */ +-static void set_gpio_cfg(uint32_t bank, uint32_t pin, struct gpio_cfg *cfg) ++static void set_gpio_cfg(uint32_t bank_id, uint32_t pin, struct gpio_cfg *cfg) + { +- vaddr_t base = stm32_get_gpio_bank_base(bank); +- struct clk *clk = stm32_get_gpio_bank_clk(bank); +- uint32_t exceptions = cpu_spin_lock_xsave(&gpio_lock); ++ struct stm32_gpio_bank *bank = stm32_gpio_get_bank(bank_id); ++ uint32_t exceptions = 0; ++ ++ if (!bank) { ++ EMSG("Error: can not find GPIO bank %c", bank_id + 'A'); ++ panic(); ++ } + +- clk_enable(clk); ++ exceptions = cpu_spin_lock_xsave(&bank->lock); + + /* Load GPIO MODE value, 2bit value shifted by twice the pin number */ +- io_clrsetbits32(base + GPIO_MODER_OFFSET, ++ io_clrsetbits32(bank->base + GPIO_MODER_OFFSET, + GPIO_MODE_MASK << (pin << 1), + cfg->mode << (pin << 1)); + + /* Load GPIO Output TYPE value, 1bit shifted by pin number value */ +- io_clrsetbits32(base + GPIO_OTYPER_OFFSET, BIT(pin), cfg->otype << pin); ++ io_clrsetbits32(bank->base + GPIO_OTYPER_OFFSET, BIT(pin), ++ cfg->otype << pin); + + /* Load GPIO Output Speed confguration, 2bit value */ +- io_clrsetbits32(base + GPIO_OSPEEDR_OFFSET, ++ io_clrsetbits32(bank->base + GPIO_OSPEEDR_OFFSET, + GPIO_OSPEED_MASK << (pin << 1), + cfg->ospeed << (pin << 1)); + + /* Load GPIO pull configuration, 2bit value */ +- io_clrsetbits32(base + GPIO_PUPDR_OFFSET, BIT(pin), ++ io_clrsetbits32(bank->base + GPIO_PUPDR_OFFSET, ++ GPIO_PUPD_PULL_MASK << (pin << 1), + cfg->pupd << (pin << 1)); + + /* Load pin mux Alternate Function configuration, 4bit value */ + if (pin < GPIO_ALT_LOWER_LIMIT) { +- io_clrsetbits32(base + GPIO_AFRL_OFFSET, ++ io_clrsetbits32(bank->base + GPIO_AFRL_OFFSET, + GPIO_ALTERNATE_MASK << (pin << 2), + cfg->af << (pin << 2)); + } else { + size_t shift = (pin - GPIO_ALT_LOWER_LIMIT) << 2; + +- io_clrsetbits32(base + GPIO_AFRH_OFFSET, ++ io_clrsetbits32(bank->base + GPIO_AFRH_OFFSET, + GPIO_ALTERNATE_MASK << shift, + cfg->af << shift); + } + + /* Load GPIO Output direction confuguration, 1bit */ +- io_clrsetbits32(base + GPIO_ODR_OFFSET, BIT(pin), cfg->od << pin); ++ io_clrsetbits32(bank->base + GPIO_ODR_OFFSET, BIT(pin), cfg->od << pin); ++ ++ cpu_spin_unlock_xrestore(&bank->lock, exceptions); ++} + +- clk_disable(clk); +- cpu_spin_unlock_xrestore(&gpio_lock, exceptions); ++/* Read current GPIO pin configuration and save it to 16bit formatted @cfg */ ++static void get_gpio_cfg(struct stm32_gpio_bank *bank, unsigned int pin, ++ struct gpio_cfg *cfg) ++{ ++ cfg->mode = (io_read32(bank->base + GPIO_MODER_OFFSET) >> (pin << 1)) & ++ GPIO_MODE_MASK; ++ ++ cfg->otype = (io_read32(bank->base + GPIO_OTYPER_OFFSET) >> pin) & 1; ++ ++ cfg->ospeed = (io_read32(bank->base + GPIO_OSPEEDR_OFFSET) >> ++ (pin << 1)) & GPIO_OSPEED_MASK; ++ ++ cfg->pupd = (io_read32(bank->base + GPIO_PUPDR_OFFSET) >> (pin << 1)) & ++ GPIO_PUPD_PULL_MASK; ++ ++ if (pin < GPIO_ALT_LOWER_LIMIT) { ++ size_t shift = pin << 2; ++ ++ cfg->af = (io_read32(bank->base + GPIO_AFRL_OFFSET) >> shift) & ++ GPIO_ALTERNATE_MASK; ++ } else { ++ size_t shift = (pin - GPIO_ALT_LOWER_LIMIT) << 2; ++ ++ cfg->af = (io_read32(bank->base + GPIO_AFRH_OFFSET) >> shift) & ++ GPIO_ALTERNATE_MASK; ++ } ++ ++ cfg->od = (io_read32(bank->base + GPIO_ODR_OFFSET) >> pin) & 1; + } + +-void stm32_pinctrl_load_active_cfg(struct stm32_pinctrl *pinctrl, size_t cnt) ++static TEE_Result stm32_pinctrl_backup(struct stm32_pinctrl *pinctrl) + { +- size_t n = 0; ++ struct stm32_pinctrl_backup *backup = NULL; ++ struct stm32_gpio_bank *bank = stm32_gpio_get_bank(pinctrl->bank); ++ ++ if (!bank) { ++ EMSG("Error: can not find GPIO bank %c", pinctrl->bank + 'A'); ++ return TEE_ERROR_GENERIC; ++ } ++ ++ backup = calloc(1, sizeof(*backup)); ++ if (!backup) { ++ EMSG("Error: can't backup pinctrl of %c%d", pinctrl->bank + 'A', ++ pinctrl->pin); ++ return TEE_ERROR_OUT_OF_MEMORY; ++ } + +- for (n = 0; n < cnt; n++) +- set_gpio_cfg(pinctrl[n].bank, pinctrl[n].pin, +- &pinctrl[n].active_cfg); ++ backup->pinctrl.bank = pinctrl->bank; ++ backup->pinctrl.pin = pinctrl->pin; ++ backup->pinctrl.config = pinctrl->config; ++ ++ STAILQ_INSERT_TAIL(&bank->backups, backup, link); ++ ++ return TEE_SUCCESS; + } + +-void stm32_pinctrl_load_standby_cfg(struct stm32_pinctrl *pinctrl, size_t cnt) ++void stm32_pinctrl_load_config(struct stm32_pinctrl_list *list) + { +- size_t n = 0; ++ struct stm32_pinctrl *p = NULL; + +- for (n = 0; n < cnt; n++) +- set_gpio_cfg(pinctrl[n].bank, pinctrl[n].pin, +- &pinctrl[n].standby_cfg); ++ STAILQ_FOREACH(p, list, link) ++ set_gpio_cfg(p->bank, p->pin, &p->config); + } + +-void stm32_pinctrl_store_standby_cfg(struct stm32_pinctrl *pinctrl, size_t cnt) ++static TEE_Result add_pm_pin(struct stm32_gpio_bank *bank, unsigned int pin) + { +- size_t n = 0; ++ struct stm32_pinctrl pinctrl = { ++ .bank = bank->bank_id, ++ .pin = pin, ++ }; ++ struct stm32_pinctrl_backup *backup = NULL; ++ ++ STAILQ_FOREACH(backup, &bank->backups, link) ++ if (backup->pinctrl.bank == bank->bank_id && ++ backup->pinctrl.pin == pin) ++ return TEE_SUCCESS; + +- for (n = 0; n < cnt; n++) +- get_gpio_cfg(pinctrl[n].bank, pinctrl[n].pin, +- &pinctrl[n].standby_cfg); ++ DMSG("Add PM on pin %c%d", bank->bank_id + 'A', pin); ++ ++ return stm32_pinctrl_backup(&pinctrl); + } + +-#ifdef CFG_DT +-/* Panic if GPIO bank information from platform do not match DTB description */ +-static void ckeck_gpio_bank(void *fdt, uint32_t bank, int pinctrl_node) ++static void remove_pm_pin(struct stm32_gpio_bank *bank, unsigned int pin) + { +- int pinctrl_subnode = 0; ++ struct stm32_pinctrl_backup *backup = NULL; ++ struct stm32_pinctrl_backup *next = NULL; ++ ++ STAILQ_FOREACH_SAFE(backup, &bank->backups, link, next) { ++ if (backup->pinctrl.bank == bank->bank_id && ++ backup->pinctrl.pin == pin) { ++ DMSG("Remove PM on pin %c%d", bank->bank_id + 'A', pin); ++ STAILQ_REMOVE(&bank->backups, backup, ++ stm32_pinctrl_backup, link); ++ free(backup); ++ break; ++ } ++ } ++} + +- fdt_for_each_subnode(pinctrl_subnode, fdt, pinctrl_node) { +- TEE_Result res = TEE_ERROR_GENERIC; +- const fdt32_t *cuint = NULL; +- struct clk *clk = NULL; ++/* ++ * Load GPIO pin secure state and ensure pin configuration of secure pins is ++ * restored on PM resume transition. ++ */ ++TEE_Result stm32_gpio_set_secure_cfg(unsigned int bank_id, unsigned int pin, ++ bool secure) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ struct stm32_gpio_bank *bank = NULL; ++ uint32_t exceptions = 0; + +- if (fdt_getprop(fdt, pinctrl_subnode, +- "gpio-controller", NULL) == NULL) +- continue; ++ bank = stm32_gpio_get_bank(bank_id); ++ assert(bank); + +- /* Check bank register offset matches platform assumptions */ +- cuint = fdt_getprop(fdt, pinctrl_subnode, "reg", NULL); +- if (fdt32_to_cpu(*cuint) != stm32_get_gpio_bank_offset(bank)) +- continue; ++ if (!bank->sec_support) { ++ assert(!secure); ++ return TEE_SUCCESS; ++ } + +- /* Check bank clock matches platform assumptions */ +- res = clk_dt_get_by_index(fdt, pinctrl_subnode, 0, &clk); +- if (res || clk != stm32_get_gpio_bank_clk(bank)) +- panic(); ++ exceptions = cpu_spin_lock_xsave(&bank->lock); + +- /* Check controller is enabled */ +- if (_fdt_get_status(fdt, pinctrl_subnode) == DT_STATUS_DISABLED) +- panic(); ++ if (secure) { ++ res = add_pm_pin(bank, pin); ++ if (res) ++ return res; + +- return; ++ io_setbits32(bank->base + GPIO_SECR_OFFSET, BIT(pin)); ++ } else { ++ remove_pm_pin(bank, pin); ++ io_clrbits32(bank->base + GPIO_SECR_OFFSET, BIT(pin)); ++ } ++ ++ cpu_spin_unlock_xrestore(&bank->lock, exceptions); ++ ++ return TEE_SUCCESS; ++} ++ ++TEE_Result stm32_pinctrl_set_secure_cfg(struct stm32_pinctrl_list *list, ++ bool secure) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ struct stm32_pinctrl *p = NULL; ++ ++ STAILQ_FOREACH(p, list, link) { ++ res = stm32_gpio_set_secure_cfg(p->bank, p->pin, secure); ++ if (res) ++ return res; + } + +- panic(); ++ return TEE_SUCCESS; + } + + /* Count pins described in the DT node and get related data if possible */ +-static int get_pinctrl_from_fdt(void *fdt, int node, +- struct stm32_pinctrl *pinctrl, size_t count) ++static TEE_Result get_pinctrl_from_fdt(const void *fdt, int node, ++ struct stm32_pinctrl_list *list) + { +- const fdt32_t *cuint, *slewrate; ++ const fdt32_t *cuint = NULL; ++ const fdt32_t *slewrate = NULL; + int len = 0; + int pinctrl_node = 0; + uint32_t i = 0; + uint32_t speed = GPIO_OSPEED_LOW; + uint32_t pull = GPIO_PUPD_NO_PULL; +- size_t found = 0; + + cuint = fdt_getprop(fdt, node, "pinmux", &len); + if (!cuint) +- return -FDT_ERR_NOTFOUND; ++ panic(); + + pinctrl_node = fdt_parent_offset(fdt, fdt_parent_offset(fdt, node)); + if (pinctrl_node < 0) +- return -FDT_ERR_NOTFOUND; ++ panic(); + + slewrate = fdt_getprop(fdt, node, "slew-rate", NULL); + if (slewrate) +@@ -227,13 +427,19 @@ static int get_pinctrl_from_fdt(void *fdt, int node, + pull = GPIO_PUPD_PULL_DOWN; + + for (i = 0; i < ((uint32_t)len / sizeof(uint32_t)); i++) { ++ struct stm32_pinctrl *ref = NULL; + uint32_t pincfg = 0; + uint32_t bank = 0; + uint32_t pin = 0; + uint32_t mode = 0; + uint32_t alternate = 0; ++ uint32_t od = 0; + bool opendrain = false; + ++ ref = calloc(1, sizeof(*ref)); ++ if (!ref) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ + pincfg = fdt32_to_cpu(*cuint); + cuint++; + +@@ -277,165 +483,459 @@ static int get_pinctrl_from_fdt(void *fdt, int node, + if (fdt_getprop(fdt, node, "drive-open-drain", NULL)) + opendrain = true; + +- /* Check GPIO bank clock/base address against platform */ +- ckeck_gpio_bank(fdt, bank, pinctrl_node); +- +- if (found < count) { +- struct stm32_pinctrl *ref = &pinctrl[found]; +- +- ref->bank = (uint8_t)bank; +- ref->pin = (uint8_t)pin; +- ref->active_cfg.mode = mode; +- ref->active_cfg.otype = opendrain ? 1 : 0; +- ref->active_cfg.ospeed = speed; +- ref->active_cfg.pupd = pull; +- ref->active_cfg.od = 0; +- ref->active_cfg.af = alternate; +- /* Default to analog mode for standby state */ +- ref->standby_cfg.mode = GPIO_MODE_ANALOG; +- ref->standby_cfg.pupd = GPIO_PUPD_NO_PULL; ++ if (fdt_getprop(fdt, node, "output-high", NULL)) { ++ if (mode == GPIO_MODE_INPUT) { ++ mode = GPIO_MODE_OUTPUT; ++ od = 1; ++ } + } + +- found++; ++ if (fdt_getprop(fdt, node, "output-low", NULL)) { ++ if (mode == GPIO_MODE_INPUT) { ++ mode = GPIO_MODE_OUTPUT; ++ od = 0; ++ } ++ } ++ ++ ref->bank = (uint8_t)bank; ++ ref->pin = (uint8_t)pin; ++ ref->config.mode = mode; ++ ref->config.otype = opendrain ? 1 : 0; ++ ref->config.ospeed = speed; ++ ref->config.pupd = pull; ++ ref->config.od = od; ++ ref->config.af = alternate; ++ ++ STAILQ_INSERT_TAIL(list, ref, link); + } + +- return (int)found; ++ return TEE_SUCCESS; + } + +-int stm32_pinctrl_fdt_get_pinctrl(void *fdt, int device_node, +- struct stm32_pinctrl *pinctrl, size_t count) ++static TEE_Result add_pinctrl(const void *fdt, const int phandle, ++ struct stm32_pinctrl_list **list) + { +- const fdt32_t *cuint = NULL; +- int lenp = 0; +- int i = 0; +- size_t found = 0; ++ TEE_Result res = TEE_ERROR_GENERIC; ++ int node = 0; ++ int subnode = 0; + +- cuint = fdt_getprop(fdt, device_node, "pinctrl-0", &lenp); +- if (!cuint) +- return -FDT_ERR_NOTFOUND; ++ assert(list); ++ if (!*list) { ++ *list = calloc(1, sizeof(**list)); ++ if (!*list) ++ return TEE_ERROR_OUT_OF_MEMORY; + +- for (i = 0; i < (lenp / 4); i++) { +- int node = 0; +- int subnode = 0; ++ STAILQ_INIT(*list); ++ } + +- node = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(*cuint)); +- if (node < 0) +- return -FDT_ERR_NOTFOUND; ++ node = fdt_node_offset_by_phandle(fdt, phandle); ++ if (node < 0) ++ panic(); + +- fdt_for_each_subnode(subnode, fdt, node) { +- size_t n = 0; +- int rc = 0; ++ fdt_for_each_subnode(subnode, fdt, node) { ++ res = get_pinctrl_from_fdt(fdt, subnode, *list); ++ if (res) { ++ EMSG("Failed to get pinctrl: %#"PRIx32, res); ++ return res; ++ } ++ } + +- if (count > found) +- n = count - found; +- else +- n = 0; ++ return TEE_SUCCESS; ++} + +- rc = get_pinctrl_from_fdt(fdt, subnode, +- &pinctrl[found], n); +- if (rc < 0) +- return rc; ++static __unused struct stm32_pinctrl_list ++*stm32_pinctrl_dt_get(struct dt_driver_phandle_args *args __maybe_unused, ++ void *data, TEE_Result *res) ++{ ++ struct stm32_pinctrl_list *list = NULL; ++ struct stm32_data_pinctrl *data_pin = ++ (struct stm32_data_pinctrl *)data; ++ ++ *res = add_pinctrl(data_pin->fdt, data_pin->phandle, &list); ++ return list; ++} ++ ++/* Informative unused helper function */ ++static __unused void free_banks(void) ++{ ++ struct stm32_gpio_bank *bank = NULL; + +- found += (size_t)rc; ++ STAILQ_FOREACH(bank, &bank_list, link) ++ free(bank); ++} ++ ++static TEE_Result ++stm32_pinctrl_dt_get_by_idx_prop(const char *prop_name, const void *fdt, ++ int nodeoffset, ++ struct stm32_pinctrl_list **plist) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ int len = 0; ++ unsigned int i = 0; ++ struct stm32_pinctrl_list *glist = NULL; ++ struct stm32_pinctrl_list *list = NULL; ++ struct stm32_pinctrl *pinctrl = NULL; ++ ++ fdt_getprop(fdt, nodeoffset, prop_name, &len); ++ if (len <= 0) ++ return TEE_ERROR_ITEM_NOT_FOUND; ++ ++ for (i = 0; i < (len / sizeof(uint32_t)); i++) { ++ list = dt_driver_device_from_node_idx_prop(prop_name, ++ fdt, nodeoffset, i, ++ DT_DRIVER_PINCTRL, ++ &res); ++ if (res) ++ goto err; ++ ++ if (!glist) ++ glist = list; ++ else ++ STAILQ_CONCAT(glist, list); ++ } ++ ++ stm32_pinctrl_load_config(glist); ++ ++ *plist = glist; ++ ++ return TEE_SUCCESS; ++ ++err: ++ if (glist) { ++ while (!STAILQ_EMPTY(glist)) { ++ pinctrl = STAILQ_FIRST(glist); ++ STAILQ_REMOVE_HEAD(glist, link); ++ free(pinctrl); + } ++ free(glist); ++ } + +- cuint++; ++ return res; ++} ++ ++TEE_Result stm32_pinctrl_dt_get_by_index(const void *fdt, int nodeoffset, ++ unsigned int index, ++ struct stm32_pinctrl_list **plist) ++{ ++ char prop_name[PROP_NAME_MAX] = { }; ++ int check = 0; ++ ++ check = snprintf(prop_name, sizeof(prop_name), "pinctrl-%d", index); ++ if (check < 0 || check >= (int)sizeof(prop_name)) { ++ DMSG("Wrong property name for pinctrl"); ++ return TEE_ERROR_GENERIC; + } + +- return (int)found; ++ return stm32_pinctrl_dt_get_by_idx_prop(prop_name, fdt, ++ nodeoffset, plist); + } + +-int stm32_get_gpio_count(void *fdt, int pinctrl_node, unsigned int bank) ++TEE_Result stm32_pinctrl_dt_get_by_name(const void *fdt, int nodeoffset, ++ const char *name, ++ struct stm32_pinctrl_list **plist) + { +- int node = 0; +- const fdt32_t *cuint = NULL; ++ int idx = 0; ++ int check = 0; ++ char prop_name[PROP_NAME_MAX] = { }; ++ ++ idx = fdt_stringlist_search(fdt, nodeoffset, "pinctrl-names", name); ++ if (idx < 0) ++ return TEE_ERROR_GENERIC; ++ ++ check = snprintf(prop_name, sizeof(prop_name), "pinctrl-%d", idx); ++ if (check < 0 || check >= (int)sizeof(prop_name)) { ++ DMSG("Unexpected name property %s", name); ++ return TEE_ERROR_GENERIC; ++ } + +- fdt_for_each_subnode(node, fdt, pinctrl_node) { +- if (!fdt_getprop(fdt, node, "gpio-controller", NULL)) +- continue; ++ return stm32_pinctrl_dt_get_by_idx_prop(prop_name, fdt, ++ nodeoffset, plist); ++} + +- cuint = fdt_getprop(fdt, node, "reg", NULL); +- if (!cuint) +- continue; ++static struct stm32_gpio_bank *_fdt_stm32_gpio_controller(const void *fdt, ++ int node, ++ const void *data, ++ int range_offset, ++ TEE_Result *res) ++{ ++ int i = 0; ++ size_t blen = 0; ++ int len = 0; ++ const fdt32_t *cuint = NULL; ++ struct stm32_gpio_bank *bank = NULL; ++ struct clk *clk = NULL; ++ struct io_pa_va pa_va = { }; ++ const int dt_name_len = strlen(DT_GPIO_BANK_NAME0); ++ const struct stm32_gpio_compat_data *comp_data = ++ (struct stm32_gpio_compat_data *)data; ++ ++ *res = clk_dt_get_by_index(fdt, node, 0, &clk); ++ if (*res) ++ return NULL; ++ ++ bank = calloc(1, sizeof(*bank)); ++ if (!bank) { ++ *res = TEE_ERROR_OUT_OF_MEMORY; ++ return NULL; ++ } + +- if (fdt32_to_cpu(*cuint) != stm32_get_gpio_bank_offset(bank)) +- continue; ++ bank->clock = clk; + +- cuint = fdt_getprop(fdt, node, "ngpios", NULL); +- if (!cuint) +- panic(); ++ /* ++ * Do not rely *only* on the "reg" property to get the address, ++ * but consider also the "ranges" translation property ++ */ ++ cuint = fdt_getprop(fdt, node, "reg", NULL); ++ if (!cuint) ++ panic("missing reg property"); ++ ++ pa_va.pa = fdt32_to_cpu(*cuint) + range_offset; ++ blen = _fdt_reg_size(fdt, node); ++ if (blen == DT_INFO_INVALID_REG_SIZE) ++ panic("missing reg size property"); ++ ++ DMSG("Bank name %s", fdt_get_name(fdt, node, NULL)); ++ /* Parse "st,bank-name" to get its id (eg: GPIOA -> 0) */ ++ cuint = fdt_getprop(fdt, node, "st,bank-name", &len); ++ if (!cuint || (len != dt_name_len + 1)) ++ panic("missing/wrong st,bank-name property"); ++ ++ if (strncmp((const char *)cuint, DT_GPIO_BANK_NAME0, ++ dt_name_len - 1) != 0) ++ panic("wrong st,bank-name property"); ++ ++ bank->bank_id = strcmp((const char *)cuint, DT_GPIO_BANK_NAME0); ++ ++ bank->sec_support = comp_data->secure_control; ++ if (bank->sec_support) { ++ /* Secure configuration */ ++ bank->base = io_pa_or_va_secure(&pa_va, blen); ++ cuint = fdt_getprop(fdt, node, "st,protreg", NULL); ++ if (cuint) ++ bank->seccfgr = fdt32_to_cpu(*cuint); ++ else ++ DMSG("GPIO bank %c assigned to non-secure", ++ bank->bank_id + 'A'); ++ } else { ++ bank->base = io_pa_or_va_nsec(&pa_va, blen); ++ } + +- return (int)fdt32_to_cpu(*cuint); ++ /* Parse gpio-ranges with its 4 parameters */ ++ cuint = fdt_getprop(fdt, node, "gpio-ranges", &len); ++ len /= sizeof(*cuint); ++ if ((len % 4) != 0) ++ panic("wrong gpio-ranges syntax"); ++ ++ /* Get the last defined gpio line (offset + nb of pins) */ ++ for (i = 0; i < len / 4; i++) { ++ bank->ngpios = MAX(bank->ngpios, ++ (unsigned int)(fdt32_to_cpu(*(cuint + 1)) + ++ fdt32_to_cpu(*(cuint + 3)))); ++ cuint += 4; + } + +- return -1; ++ *res = TEE_SUCCESS; ++ ++ return bank; + } +-#endif /*CFG_DT*/ + +-static __maybe_unused bool valid_gpio_config(unsigned int bank, +- unsigned int pin, bool input) ++static TEE_Result stm32_gpio_parse_pinctrl_node(const void *fdt, int node, ++ const void *data) + { +- vaddr_t base = stm32_get_gpio_bank_base(bank); +- uint32_t mode = (io_read32(base + GPIO_MODER_OFFSET) >> (pin << 1)) & +- GPIO_MODE_MASK; ++ get_of_device_func get_func = (get_of_device_func)stm32_pinctrl_dt_get; ++ TEE_Result res = TEE_SUCCESS; ++ const fdt32_t *cuint = NULL; ++ int subnode = 0; ++ int len = 0; ++ int range_offset = 0; + +- if (pin > GPIO_PIN_MAX) +- return false; ++ /* Read the ranges property (for regs memory translation) */ ++ cuint = fdt_getprop(fdt, node, "ranges", &len); ++ if (!cuint) ++ panic("missing ranges property"); ++ ++ len /= sizeof(*cuint); ++ if (len == 3) ++ range_offset = fdt32_to_cpu(*(cuint + 1)) - ++ fdt32_to_cpu(*cuint); ++ ++ fdt_for_each_subnode(subnode, fdt, node) { ++ /* ++ * Parse all pinctrl sub-nodes which can be ++ * "gpio-controller" or pinctrl definitions. ++ */ ++ struct stm32_gpio_bank *bank = NULL; ++ ++ cuint = fdt_getprop(fdt, subnode, "gpio-controller", NULL); ++ if (!cuint) { ++ struct stm32_data_pinctrl *pdata = NULL; ++ uint32_t phandle = fdt_get_phandle(fdt, subnode); ++ ++ if (!phandle) { ++ /* Node without phandles will not be consumed */ ++ continue; ++ } ++ ++ pdata = calloc(1, sizeof(*pdata)); ++ pdata->fdt = fdt; ++ pdata->phandle = phandle; ++ ++ res = dt_driver_register_provider(fdt, subnode, ++ get_func, ++ (void *)pdata, ++ DT_DRIVER_PINCTRL); ++ if (res) ++ return res; ++ } else { ++ /* Register "gpio-controller" */ ++ if (_fdt_get_status(fdt, subnode) == DT_STATUS_DISABLED) ++ continue; ++ ++ bank = _fdt_stm32_gpio_controller(fdt, subnode, data, ++ range_offset, &res); ++ if (bank) ++ STAILQ_INSERT_TAIL(&bank_list, bank, link); ++ else if (res) ++ return res; ++ } ++ } + +- if (input) +- return mode == GPIO_MODE_INPUT; +- else +- return mode == GPIO_MODE_OUTPUT; ++ return res; + } + +-int stm32_gpio_get_input_level(unsigned int bank, unsigned int pin) ++static void stm32_gpio_get_conf_sec(struct stm32_gpio_bank *bank) + { +- vaddr_t base = stm32_get_gpio_bank_base(bank); +- struct clk *clk = stm32_get_gpio_bank_clk(bank); +- int rc = 0; ++ if (bank->sec_support) ++ bank->seccfgr = io_read32(bank->base + GPIO_SECR_OFFSET); ++} ++ ++static void stm32_gpio_set_conf_sec(struct stm32_gpio_bank *bank) ++{ ++ if (bank->sec_support) ++ io_write32(bank->base + GPIO_SECR_OFFSET, bank->seccfgr); ++} + +- assert(valid_gpio_config(bank, pin, true)); ++static TEE_Result stm32_gpio_pm_resume(void) ++{ ++ struct stm32_gpio_bank *bank = NULL; ++ struct stm32_pinctrl_backup *backup = NULL; + +- clk_enable(clk); ++ STAILQ_FOREACH(bank, &bank_list, link) { ++ stm32_gpio_set_conf_sec(bank); + +- if (io_read32(base + GPIO_IDR_OFFSET) == BIT(pin)) +- rc = 1; ++ STAILQ_FOREACH(backup, &bank->backups, link) ++ set_gpio_cfg(backup->pinctrl.bank, backup->pinctrl.pin, ++ &backup->pinctrl.config); ++ } + +- clk_disable(clk); ++ stm32mp_syscfg_enable_io_comp(); + +- return rc; ++ return TEE_SUCCESS; + } + +-void stm32_gpio_set_output_level(unsigned int bank, unsigned int pin, int level) ++static TEE_Result stm32_gpio_pm_suspend(void) + { +- vaddr_t base = stm32_get_gpio_bank_base(bank); +- struct clk *clk = stm32_get_gpio_bank_clk(bank); ++ struct stm32_gpio_bank *bank = NULL; ++ struct stm32_pinctrl_backup *backup = NULL; + +- assert(valid_gpio_config(bank, pin, false)); ++ stm32mp_syscfg_disable_io_comp(); + +- clk_enable(clk); ++ STAILQ_FOREACH(bank, &bank_list, link) { ++ stm32_gpio_get_conf_sec(bank); + +- if (level) +- io_write32(base + GPIO_BSRR_OFFSET, BIT(pin)); ++ STAILQ_FOREACH(backup, &bank->backups, link) ++ get_gpio_cfg(bank, backup->pinctrl.pin, ++ &backup->pinctrl.config); ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result ++stm32_gpio_pm(enum pm_op op, unsigned int pm_hint __unused, ++ const struct pm_callback_handle *pm_handle __unused) ++{ ++ TEE_Result ret = 0; ++ ++ if (op == PM_OP_RESUME) ++ ret = stm32_gpio_pm_resume(); + else +- io_write32(base + GPIO_BSRR_OFFSET, BIT(pin + 16)); ++ ret = stm32_gpio_pm_suspend(); + +- clk_disable(clk); ++ return ret; + } ++DECLARE_KEEP_PAGER(stm32_gpio_pm); + +-void stm32_gpio_set_secure_cfg(unsigned int bank, unsigned int pin, bool secure) ++static void init_secure_gpio_bank_pins(struct stm32_gpio_bank *bank) + { +- vaddr_t base = stm32_get_gpio_bank_base(bank); +- struct clk *clk = stm32_get_gpio_bank_clk(bank); +- uint32_t exceptions = cpu_spin_lock_xsave(&gpio_lock); ++ unsigned int pin = 0; + +- clk_enable(clk); ++ if (!bank->sec_support) ++ return; + +- if (secure) +- io_setbits32(base + GPIO_SECR_OFFSET, BIT(pin)); +- else +- io_clrbits32(base + GPIO_SECR_OFFSET, BIT(pin)); ++ for (pin = 0; pin < bank->ngpios; pin++) ++ if (stm32_gpio_set_secure_cfg(bank->bank_id, pin, ++ bank->seccfgr & BIT(pin))) ++ panic(); ++} ++ ++static TEE_Result stm32_gpio_probe(const void *fdt, int offs, ++ const void *compat_data) ++{ ++ static bool pm_register; ++ TEE_Result res = TEE_ERROR_GENERIC; ++ struct stm32_gpio_bank *bank = NULL; ++ ++ /* Look for gpio banks inside that node */ ++ res = stm32_gpio_parse_pinctrl_node(fdt, offs, compat_data); ++ if (res) ++ return res; + +- clk_disable(clk); +- cpu_spin_unlock_xrestore(&gpio_lock, exceptions); ++ if (STAILQ_EMPTY(&bank_list)) ++ DMSG("no gpio bank for that driver"); ++ ++ STAILQ_FOREACH(bank, &bank_list, link) { ++ STAILQ_INIT(&bank->backups); ++ ++ clk_enable(bank->clock); ++ init_secure_gpio_bank_pins(bank); ++ ++ DMSG("Registered GPIO bank %c (%d pins) @%lx", ++ bank->bank_id + 'A', bank->ngpios, bank->base); ++ } ++ ++ if (!pm_register) { ++ /* Enable IO Compensation Cells */ ++ stm32mp_syscfg_enable_io_comp(); ++ ++ /* Register to PM once for all probed banks */ ++ register_pm_core_service_cb(stm32_gpio_pm, NULL, ++ "stm32-gpio-service"); ++ pm_register = true; ++ } ++ ++ ++ return TEE_SUCCESS; + } ++ ++static const struct dt_device_match stm32_gpio_match_table[] = { ++ { ++ .compatible = "st,stm32mp135-pinctrl", ++ .compat_data = (void *)&stm32_gpio_secure_capable, ++ }, ++ { ++ .compatible = "st,stm32mp157-pinctrl", ++ .compat_data = (void *)&stm32_gpio_non_sec, ++ }, ++ { ++ .compatible = "st,stm32mp157-z-pinctrl", ++ .compat_data = (void *)&stm32_gpio_secure_capable, ++ }, ++ { } ++}; ++ ++DEFINE_DT_DRIVER(stm32_gpio_dt_driver) = { ++ .name = "stm32_gpio", ++ .match_table = stm32_gpio_match_table, ++ .probe = stm32_gpio_probe, ++}; +diff --git a/core/drivers/stm32_i2c.c b/core/drivers/stm32_i2c.c +index 988d88571..1304b15ed 100644 +--- a/core/drivers/stm32_i2c.c ++++ b/core/drivers/stm32_i2c.c +@@ -10,13 +10,15 @@ + */ + + #include ++#include + #include + #include + #include + #include ++#include + #include + #include +-#include ++#include + #include + #include + #include +@@ -290,6 +292,9 @@ struct i2c_request { + unsigned int timeout_ms; + }; + ++/* Non-null reference for compat data */ ++static const bool secure_i2c_r; ++ + static vaddr_t get_base(struct i2c_handle_s *hi2c) + { + return io_pa_or_va_secure(&hi2c->base, hi2c->reg_size); +@@ -376,7 +381,7 @@ static void __maybe_unused dump_i2c(struct i2c_handle_s *hi2c) + * @timing: Pointer to the final computed timing result + * Return 0 on success or a negative value + */ +-static int i2c_compute_timing(struct stm32_i2c_init_s *init, ++static int i2c_compute_timing(struct stm32_i2c_platform_data *init, + unsigned long clock_src, uint32_t *timing) + { + const struct i2c_spec_s *specs = NULL; +@@ -592,7 +597,7 @@ static uint32_t get_lower_rate(uint32_t rate) + * @retval 0 if OK, negative value else + */ + static int i2c_setup_timing(struct i2c_handle_s *hi2c, +- struct stm32_i2c_init_s *init, ++ struct stm32_i2c_platform_data *init, + uint32_t *timing) + { + int rc = 0; +@@ -681,15 +686,13 @@ static int i2c_config_analog_filter(struct i2c_handle_s *hi2c, + return 0; + } + +-TEE_Result stm32_i2c_get_setup_from_fdt(void *fdt, int node, +- struct stm32_i2c_init_s *init, +- struct stm32_pinctrl **pinctrl, +- size_t *pinctrl_count) ++TEE_Result stm32_i2c_get_setup_from_fdt(const void *fdt, int node, ++ struct stm32_i2c_platform_data *init, ++ struct stm32_pinctrl_list **pinctrl) + { + TEE_Result res = TEE_ERROR_GENERIC; + const fdt32_t *cuint = NULL; + struct dt_node_info info = { .status = 0 }; +- int count = 0; + + /* Default STM32 specific configs caller may need to overwrite */ + memset(init, 0, sizeof(*init)); +@@ -706,6 +709,10 @@ TEE_Result stm32_i2c_get_setup_from_fdt(void *fdt, int node, + if (res) + return res; + ++ res = stm32_pinctrl_dt_get_by_index(fdt, node, 0, pinctrl); ++ if (res) ++ return res; ++ + cuint = fdt_getprop(fdt, node, "i2c-scl-rising-time-ns", NULL); + if (cuint) + init->rise_time = fdt32_to_cpu(*cuint); +@@ -731,44 +738,19 @@ TEE_Result stm32_i2c_get_setup_from_fdt(void *fdt, int node, + init->bus_rate = I2C_STANDARD_RATE; + } + +- count = stm32_pinctrl_fdt_get_pinctrl(fdt, node, NULL, 0); +- if (count <= 0) { +- *pinctrl = NULL; +- *pinctrl_count = count; +- DMSG("Failed to get pinctrl: FDT errno %d", count); +- return TEE_ERROR_GENERIC; +- } +- +- if (count > 2) { +- DMSG("Too many PINCTRLs found: %zd", count); +- return TEE_ERROR_GENERIC; +- } +- +- *pinctrl = calloc(count, sizeof(**pinctrl)); +- if (!*pinctrl) +- return TEE_ERROR_OUT_OF_MEMORY; +- +- *pinctrl_count = stm32_pinctrl_fdt_get_pinctrl(fdt, node, +- *pinctrl, count); +- assert(*pinctrl_count == (unsigned int)count); + + return TEE_SUCCESS; + } + + int stm32_i2c_init(struct i2c_handle_s *hi2c, +- struct stm32_i2c_init_s *init_data) ++ struct stm32_i2c_platform_data *i2c_pdata) + { + int rc = 0; + uint32_t timing = 0; + vaddr_t base = 0; + uint32_t val = 0; + +- hi2c->dt_status = init_data->dt_status; +- hi2c->base.pa = init_data->pbase; +- hi2c->reg_size = init_data->reg_size; +- hi2c->clock = init_data->clock; +- +- rc = i2c_setup_timing(hi2c, init_data, &timing); ++ rc = i2c_setup_timing(hi2c, i2c_pdata, &timing); + if (rc) + return rc; + +@@ -787,17 +769,17 @@ int stm32_i2c_init(struct i2c_handle_s *hi2c, + io_write32(base + I2C_OAR1, 0); + + /* Configure I2Cx: Own Address1 and ack own address1 mode */ +- if (init_data->addr_mode_10b_not_7b) ++ if (i2c_pdata->addr_mode_10b_not_7b) + io_write32(base + I2C_OAR1, + I2C_OAR1_OA1EN | I2C_OAR1_OA1MODE | +- init_data->own_address1); ++ i2c_pdata->own_address1); + else + io_write32(base + I2C_OAR1, +- I2C_OAR1_OA1EN | init_data->own_address1); ++ I2C_OAR1_OA1EN | i2c_pdata->own_address1); + + /* Configure I2Cx: Addressing Master mode */ + io_write32(base + I2C_CR2, 0); +- if (init_data->addr_mode_10b_not_7b) ++ if (i2c_pdata->addr_mode_10b_not_7b) + io_setbits32(base + I2C_CR2, I2C_CR2_ADD10); + + /* +@@ -810,16 +792,16 @@ int stm32_i2c_init(struct i2c_handle_s *hi2c, + io_write32(base + I2C_OAR2, 0); + + /* Configure I2Cx: Dual mode and Own Address2 */ +- if (init_data->dual_address_mode) ++ if (i2c_pdata->dual_address_mode) + io_write32(base + I2C_OAR2, +- I2C_OAR2_OA2EN | init_data->own_address2 | +- (init_data->own_address2_masks << 8)); ++ I2C_OAR2_OA2EN | i2c_pdata->own_address2 | ++ (i2c_pdata->own_address2_masks << 8)); + + /* Configure I2Cx: Generalcall and NoStretch mode */ + val = 0; +- if (init_data->general_call_mode) ++ if (i2c_pdata->general_call_mode) + val |= I2C_CR1_GCEN; +- if (init_data->no_stretch_mode) ++ if (i2c_pdata->no_stretch_mode) + val |= I2C_CR1_NOSTRETCH; + io_write32(base + I2C_CR1, val); + +@@ -829,7 +811,7 @@ int stm32_i2c_init(struct i2c_handle_s *hi2c, + hi2c->i2c_err = I2C_ERROR_NONE; + hi2c->i2c_state = I2C_STATE_READY; + +- rc = i2c_config_analog_filter(hi2c, init_data->analog_filter); ++ rc = i2c_config_analog_filter(hi2c, i2c_pdata->analog_filter); + if (rc) + DMSG("I2C analog filter error %d", rc); + +@@ -1517,13 +1499,14 @@ void stm32_i2c_resume(struct i2c_handle_s *hi2c) + (hi2c->i2c_state != I2C_STATE_SUSPENDED)) + panic(); + +- stm32_pinctrl_load_active_cfg(hi2c->pinctrl, hi2c->pinctrl_count); +- + if (hi2c->i2c_state == I2C_STATE_RESET) { + /* There is no valid I2C configuration to be loaded yet */ + return; + } + ++ if (!hi2c->secure_i2c) ++ stm32_pinctrl_load_config(hi2c->pinctrl_list); ++ + restore_cfg(hi2c, &hi2c->sec_cfg); + + hi2c->i2c_state = I2C_STATE_READY; +@@ -1538,7 +1521,108 @@ void stm32_i2c_suspend(struct i2c_handle_s *hi2c) + panic(); + + save_cfg(hi2c, &hi2c->sec_cfg); +- stm32_pinctrl_load_standby_cfg(hi2c->pinctrl, hi2c->pinctrl_count); + + hi2c->i2c_state = I2C_STATE_SUSPENDED; + } ++ ++static struct i2c_handle_s * ++stm32_get_i2c_handle(struct dt_driver_phandle_args *pargs __unused, ++ void *data, TEE_Result *res) ++{ ++ *res = TEE_SUCCESS; ++ return (struct i2c_handle_s *)data; ++} ++ ++TEE_Result i2c_dt_get_by_subnode(const void *fdt, int child_node, ++ struct i2c_handle_s **child_handle) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ int node = 0; ++ ++ node = fdt_parent_offset(fdt, child_node); ++ if (node < 0) ++ return TEE_ERROR_ITEM_NOT_FOUND; ++ ++ *child_handle = (struct i2c_handle_s *) ++ dt_driver_device_from_node(node, DT_DRIVER_I2C, &res); ++ ++ return res; ++} ++ ++static TEE_Result stm32_i2c_probe(const void *fdt, int node, ++ const void *compat_data __unused) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ int subnode = 0; ++ struct i2c_handle_s *i2c_handle_p = NULL; ++ struct stm32_pinctrl_list *pinctrl_l = NULL; ++ struct stm32_i2c_platform_data i2c_pdata = { }; ++ ++ res = stm32_i2c_get_setup_from_fdt(fdt, node, &i2c_pdata, &pinctrl_l); ++ if (res) ++ return res; ++ ++ i2c_handle_p = calloc(1, sizeof(struct i2c_handle_s)); ++ if (!i2c_handle_p) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ i2c_handle_p->dt_status = i2c_pdata.dt_status; ++ i2c_handle_p->reg_size = i2c_pdata.reg_size; ++ i2c_handle_p->clock = i2c_pdata.clock; ++ i2c_handle_p->base.pa = i2c_pdata.pbase; ++ i2c_handle_p->base.va = io_pa_or_va(&i2c_handle_p->base, ++ i2c_pdata.reg_size); ++ assert(i2c_handle_p->base.va); ++ i2c_handle_p->clock = i2c_pdata.clock; ++ i2c_handle_p->i2c_state = I2C_STATE_RESET; ++ i2c_handle_p->pinctrl_list = pinctrl_l; ++ ++ i2c_pdata.analog_filter = true; ++ i2c_pdata.digital_filter_coef = 0; ++ ++ if (compat_data == &secure_i2c_r) ++ i2c_handle_p->secure_i2c = true; ++ ++ res = stm32_i2c_init(i2c_handle_p, &i2c_pdata); ++ if (res) ++ panic("Couldn't initialise I2C"); ++ ++ res = i2c_register_provider(fdt, node, stm32_get_i2c_handle, ++ i2c_handle_p); ++ if (res) ++ panic("Couldn't register I2C provider"); ++ ++ fdt_for_each_subnode(subnode, fdt, node) { ++ res = dt_driver_maybe_add_probe_node(fdt, subnode); ++ if (res) { ++ EMSG("Failed on node %s with %#"PRIx32, ++ fdt_get_name(fdt, subnode, NULL), res); ++ panic(); ++ } ++ } ++ ++ return res; ++} ++ ++static const struct dt_device_match stm32_i2c_match_table[] = { ++ { ++ .compatible = "st,stm32mp15-i2c", ++ .compat_data = &secure_i2c_r ++ }, ++ { ++ .compatible = "st,stm32mp13-i2c", ++ .compat_data = &secure_i2c_r ++ }, ++ { ++ .compatible = "st,stm32mp15-i2c-non-secure", ++ .compat_data = NULL ++ }, ++ { } ++}; ++ ++DEFINE_DT_DRIVER(stm32_i2c_dt_driver) = { ++ .name = "stm32_i2c", ++ .match_table = stm32_i2c_match_table, ++ .probe = stm32_i2c_probe, ++ .type = DT_DRIVER_I2C ++}; +diff --git a/core/drivers/stm32_iwdg.c b/core/drivers/stm32_iwdg.c +new file mode 100644 +index 000000000..49e4fad50 +--- /dev/null ++++ b/core/drivers/stm32_iwdg.c +@@ -0,0 +1,297 @@ ++// SPDX-License-Identifier: BSD-3-Clause ++/* ++ * Copyright (c) 2017-2021, STMicroelectronics - All Rights Reserved ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++/* IWDG Compatibility */ ++#define IWDG_TIMEOUT_US U(100000) ++ ++/* IWDG registers offsets */ ++#define IWDG_KR_OFFSET U(0x00) ++#define IWDG_PR_OFFSET U(0x04) ++#define IWDG_RLR_OFFSET U(0x08) ++#define IWDG_SR_OFFSET U(0x0C) ++#define IWDG_EWCR_OFFSET U(0x14) ++ ++/* Registers values */ ++#define IWDG_KR_ACCESS_KEY U(0x5555) ++#define IWDG_KR_RELOAD_KEY U(0xAAAA) ++#define IWDG_KR_START_KEY U(0xCCCC) ++ ++#define IWDG_PR_DIV_4 U(0x00) ++#define IWDG_PR_DIV_256 U(0x06) ++ ++#define IWDG_RLR_RL_MASK GENMASK_32(11, 0) ++ ++#define IWDG_SR_EWU BIT(3) ++ ++#define IWDG_EWCR_EWIE BIT(15) ++#define IWDG_EWCR_EWIC BIT(14) ++#define IWDG_EWCR_EWIT_MASK GENMASK_32(11, 0) ++ ++struct stm32_iwdg_device { ++ struct stm32_iwdg_platdata pdata; ++ struct itr_handler *itr; ++ SLIST_ENTRY(stm32_iwdg_device) link; ++}; ++ ++static unsigned int list_lock = SPINLOCK_UNLOCK; ++ ++static SLIST_HEAD(iwdg_dev_list_head, stm32_iwdg_device) iwdg_dev_list = ++ SLIST_HEAD_INITIALIZER(iwdg_dev_list_head); ++ ++static vaddr_t get_base(struct stm32_iwdg_device *iwdg) ++{ ++ return io_pa_or_va(&iwdg->pdata.base, 1); ++} ++ ++static void iwdg_refresh(struct stm32_iwdg_device *iwdg) ++{ ++ assert(iwdg); ++ ++ clk_enable(iwdg->pdata.clock); ++ ++ io_write32(get_base(iwdg) + IWDG_KR_OFFSET, IWDG_KR_RELOAD_KEY); ++ ++ clk_disable(iwdg->pdata.clock); ++} ++ ++static enum itr_return stm32_iwdg_it_handler(struct itr_handler *handler) ++{ ++ unsigned int __maybe_unused cpu = get_core_pos(); ++ struct stm32_iwdg_device *iwdg = handler->data; ++ vaddr_t iwdg_base = get_base(iwdg); ++ ++ DMSG("CPU %u IT Watchdog %#"PRIxPA, cpu, ++ virt_to_phys((void *)iwdg->pdata.base.pa)); ++ ++ stm32mp_dump_core_registers(true); ++ ++ iwdg_refresh(iwdg); ++ ++ clk_enable(iwdg->pdata.clock); ++ ++ io_setbits32(iwdg_base + IWDG_EWCR_OFFSET, IWDG_EWCR_EWIC); ++ ++ clk_disable(iwdg->pdata.clock); ++ ++ /* ++ * Ack interrupt as we do not return from next call ++ * and interrupt is no more considered as pending here. ++ */ ++ stm32mp_gic_set_end_of_interrupt(handler->it); ++ ++ panic("Watchdog"); ++ ++ return ITRR_HANDLED; ++} ++DECLARE_KEEP_PAGER(stm32_iwdg_it_handler); ++ ++void stm32_iwdg_refresh(void) ++{ ++ struct stm32_iwdg_device *iwdg = NULL; ++ uint32_t exceptions = cpu_spin_lock_xsave(&list_lock); ++ ++ SLIST_FOREACH(iwdg, &iwdg_dev_list, link) ++ iwdg_refresh(iwdg); ++ ++ cpu_spin_unlock_xrestore(&list_lock, exceptions); ++} ++ ++static TEE_Result stm32_iwdg_conf_etimeout(struct stm32_iwdg_device *iwdg) ++{ ++ uint32_t reload = 0; ++ uint32_t status = 0; ++ uint64_t timeout_ref = 0; ++ unsigned long long reload_ll = 0; ++ vaddr_t iwdg_base = get_base(iwdg); ++ ++ /* Prescaler fix to 256 */ ++ reload_ll = (unsigned long long)iwdg->pdata.sec_timeout * ++ clk_get_rate(iwdg->pdata.clk_lsi); ++ ++ reload = ((uint32_t)(reload_ll >> 8) - 1) & IWDG_EWCR_EWIT_MASK; ++ ++ clk_enable(iwdg->pdata.clock); ++ clk_enable(iwdg->pdata.clk_lsi); ++ ++ io_write32(iwdg_base + IWDG_KR_OFFSET, IWDG_KR_START_KEY); ++ io_write32(iwdg_base + IWDG_KR_OFFSET, IWDG_KR_ACCESS_KEY); ++ io_write32(iwdg_base + IWDG_PR_OFFSET, IWDG_PR_DIV_256); ++ io_write32(iwdg_base + IWDG_EWCR_OFFSET, reload | IWDG_EWCR_EWIE); ++ ++ timeout_ref = timeout_init_us(IWDG_TIMEOUT_US); ++ while (!timeout_elapsed(timeout_ref)) ++ if (!(io_read32(iwdg_base + IWDG_SR_OFFSET) & IWDG_SR_EWU)) ++ break; ++ ++ status = io_read32(iwdg_base + IWDG_SR_OFFSET) & IWDG_SR_EWU; ++ clk_disable(iwdg->pdata.clock); ++ if (status) ++ return TEE_ERROR_GENERIC; ++ ++ iwdg->itr = itr_alloc_add(iwdg->pdata.irq, stm32_iwdg_it_handler, ++ ITRF_TRIGGER_LEVEL, iwdg); ++ ++ if (!iwdg->itr) ++ return TEE_ERROR_GENERIC; ++ ++ itr_enable(iwdg->itr->it); ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result stm32_iwdg_parse_fdt(struct stm32_iwdg_device *iwdg_dev, ++ const void *fdt, int node) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ struct dt_node_info dt_info = { }; ++ const fdt32_t *cuint = NULL; ++ struct clk *clk = NULL; ++ struct io_pa_va *base = &iwdg_dev->pdata.base; ++ const struct stm32_firewall_cfg sec_cfg[] = { ++ { FWLL_SEC_RW | FWLL_MASTER(0) }, ++ { }, /* Null terminated */ ++ }; ++ ++ _fdt_fill_device_info(fdt, &dt_info, node); ++ if (dt_info.reg == DT_INFO_INVALID_REG || ++ dt_info.reg_size == DT_INFO_INVALID_REG_SIZE || ++ dt_info.clock == DT_INFO_INVALID_CLOCK || ++ dt_info.interrupt == DT_INFO_INVALID_INTERRUPT) ++ return TEE_ERROR_ITEM_NOT_FOUND; ++ ++ base->pa = dt_info.reg; ++ ++ res = stm32_firewall_check_access(base->pa, dt_info.reg_size, sec_cfg); ++ if (res == TEE_SUCCESS) ++ io_pa_or_va_secure(base, dt_info.reg_size); ++ else ++ io_pa_or_va_nsec(base, dt_info.reg_size); ++ ++ assert(base->va); ++ ++ iwdg_dev->pdata.irq = dt_info.interrupt; ++ ++ res = clk_dt_get_by_index(fdt, node, 0, &clk); ++ if (res) ++ return res; ++ ++ iwdg_dev->pdata.clock = clk; ++ ++ res = clk_dt_get_by_index(fdt, node, 1, &clk); ++ if (!clk) ++ return res; ++ ++ iwdg_dev->pdata.clk_lsi = clk; ++ ++ cuint = fdt_getprop(fdt, node, "timeout-sec", NULL); ++ if (!cuint) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ iwdg_dev->pdata.timeout = (int)fdt32_to_cpu(*cuint); ++ ++ cuint = fdt_getprop(fdt, node, "secure-timeout-sec", NULL); ++ if (!cuint) ++ iwdg_dev->pdata.sec_timeout = -1; ++ else ++ iwdg_dev->pdata.sec_timeout = (int)fdt32_to_cpu(*cuint); ++ ++ /* DT can specify low power cases */ ++ if (!fdt_getprop(fdt, node, "stm32,enable-on-stop", NULL)) ++ iwdg_dev->pdata.flags |= IWDG_DISABLE_ON_STOP; ++ ++ if (!fdt_getprop(fdt, node, "stm32,enable-on-standby", NULL)) ++ iwdg_dev->pdata.flags |= IWDG_DISABLE_ON_STANDBY; ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result stm32_iwdg_setup(struct stm32_iwdg_device *iwdg_dev, ++ const void *fdt, int node) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ uint32_t hw_init = 0; ++ ++ assert(iwdg_dev); ++ ++ res = stm32_iwdg_parse_fdt(iwdg_dev, fdt, node); ++ if (res) ++ return res; ++ ++ hw_init = stm32_get_iwdg_otp_config(iwdg_dev->pdata.base.pa); ++ if (hw_init & IWDG_HW_ENABLED) ++ iwdg_dev->pdata.flags |= IWDG_HW_ENABLED; ++ ++ if (hw_init & IWDG_DISABLE_ON_STOP) ++ iwdg_dev->pdata.flags |= IWDG_DISABLE_ON_STOP; ++ ++ if (hw_init & IWDG_DISABLE_ON_STANDBY) ++ iwdg_dev->pdata.flags |= IWDG_DISABLE_ON_STANDBY; ++ ++ if (iwdg_dev->pdata.sec_timeout > 0) { ++ res = stm32_iwdg_conf_etimeout(iwdg_dev); ++ if (res) { ++ EMSG("IWDG early timeout config failed: %#"PRIx32, res); ++ panic(); ++ } ++ } ++ ++ return res; ++} ++ ++static TEE_Result stm32_iwdg_probe(const void *fdt, int node, ++ const void *compat_data __unused) ++{ ++ struct stm32_iwdg_device *iwdg_dev = NULL; ++ TEE_Result res = TEE_SUCCESS; ++ uint32_t exceptions = 0; ++ ++ iwdg_dev = calloc(1, sizeof(*iwdg_dev)); ++ if (!iwdg_dev) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ res = stm32_iwdg_setup(iwdg_dev, fdt, node); ++ if (res) { ++ free(iwdg_dev); ++ return res; ++ } ++ ++ exceptions = cpu_spin_lock_xsave(&list_lock); ++ SLIST_INSERT_HEAD(&iwdg_dev_list, iwdg_dev, link); ++ cpu_spin_unlock_xrestore(&list_lock, exceptions); ++ ++ return TEE_SUCCESS; ++} ++ ++static const struct dt_device_match stm32_iwdg_match_table[] = { ++ { .compatible = "st,stm32mp1-iwdg" }, ++ { } ++}; ++ ++DEFINE_DT_DRIVER(stm32_iwdg_dt_driver) = { ++ .name = "stm32-iwdg", ++ .match_table = stm32_iwdg_match_table, ++ .probe = stm32_iwdg_probe, ++}; +diff --git a/core/drivers/stm32_ltdc.c b/core/drivers/stm32_ltdc.c +new file mode 100644 +index 000000000..a602752b7 +--- /dev/null ++++ b/core/drivers/stm32_ltdc.c +@@ -0,0 +1,502 @@ ++// SPDX-License-Identifier: BSD-2-Clause ++/* ++ * Copyright (c) 2021, STMicroelectronics ++ * Author: Yannick Fertre for STMicroelectronics. ++ */ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++struct ltdc_device { ++ vaddr_t regs; ++ struct io_pa_va io_base; ++ struct clk *clock; ++ struct stm32_pinctrl_list *pinctrl_list; ++ struct itr_handler *itr0; ++ struct itr_handler *itr1; ++ int pinctrl_count; ++ bool end_of_frame; ++ bool activate; ++}; ++ ++#define LTDC_IDR 0x0000 ++#define LTDC_LCR 0x0004 ++#define LTDC_SSCR 0x0008 ++#define LTDC_BPCR 0x000C ++#define LTDC_AWCR 0x0010 ++#define LTDC_TWCR 0x0014 ++#define LTDC_GCR 0x0018 ++#define LTDC_SRCR 0x0024 ++#define LTDC_IER2 0x0064 ++#define LTDC_ISR2 0x0068 ++#define LTDC_ICR2 0x006C ++#define IER_LIE BIT(0) /* Line Interrupt Enable */ ++#define IER_FUWIE BIT(1) /* Fifo Underrun Warning Interrupt Enable */ ++#define IER_TERRIE BIT(2) /* Transfer ERRor Interrupt Enable */ ++#define IER_RRIE BIT(3) /* Register Reload Interrupt Enable */ ++#define IER_FUKIE BIT(6) /* Fifo Underrun Killing Interrupt Enable */ ++#define IER_CRCIE BIT(7) /* CRC Error Interrupt Enable */ ++#define IER_FURIE BIT(8) /* Fifo Underrun at Rotation Interrupt Enable */ ++#define ISR_LIF BIT(0) /* Line Interrupt Flag */ ++#define ISR_FUWIF BIT(1) /* Fifo Underrun Warning Interrupt Flag */ ++#define ISR_TERRIF BIT(2) /* Transfer ERRor Interrupt Flag */ ++#define ISR_RRIF BIT(3) /* Register Reload Interrupt Flag */ ++#define ISR_FUKIF BIT(6) /* Fifo Underrun Killing Interrupt Flag */ ++#define ISR_CRCIF BIT(7) /* CRC Error Interrupt Flag */ ++#define ISR_FURIF BIT(8) /* Fifo Underrun at Rotation Interrupt Flag */ ++ ++#define ID_HWVER_40100 0x040100 ++#define GCR_LTDCEN BIT(0) ++#define LTDC_BPCR_AHBP GENMASK_32(27, 16) ++#define LTDC_BPCR_AVBP GENMASK_32(10, 0) ++#define LTDC_LXWHPCR_WHSTPOS GENMASK_32(11, 0) ++#define LTDC_LXWHPCR_WHSPPOS GENMASK_32(31, 16) ++#define LTDC_LXWVPCR_WVSTPOS GENMASK_32(11, 0) ++#define LTDC_LXWVPCR_WVSPPOS GENMASK_32(31, 16) ++#define LTDC_LXCFBLR_CFBLL GENMASK_32(12, 0) ++#define LTDC_LXCFBLR_CFBP GENMASK_32(28, 16) ++#define LTDC_LXPFCR_PF GENMASK_32(2, 0) ++#define LTDC_LXCACR_CONSTA GENMASK_32(7, 0) ++#define LXBFCR_BF2 GENMASK_32(2, 0) ++#define LXBFCR_BF1 GENMASK_32(10, 8) ++#define LTDC_LXCFBLNR_CFBLNBR GENMASK_32(10, 0) ++#define LTDC_LXDCCR_DCBLUE GENMASK_32(7, 0) ++#define LTDC_LXDCCR_DCGREEN GENMASK_32(15, 8) ++#define LTDC_LXDCCR_DCRED GENMASK_32(23, 16) ++#define LTDC_LXDCCR_DCALPHA GENMASK_32(31, 24) ++#define LTDC_LXCFBAR_CFBADD GENMASK_32(31, 0) ++ ++enum ltdc_pix_fmt { ++ LXPFCR_PF_ARGB8888, ++ LXPFCR_PF_ABGR8888, ++ LXPFCR_PF_RGBA8888, ++ LXPFCR_PF_BGRA8888, ++ LXPFCR_PF_RGB565, ++ LXPFCR_PF_BGR565, ++ LXPFCR_PF_RGB888 ++}; ++ ++/* Within mask LTDC_LXBFCR_BF1 */ ++#define LXBFCR_BF1_PAXCA 0x600 /* Pixel Alpha x Constant Alpha */ ++#define LXBFCR_BF1_CA 0x400 /* Constant Alpha */ ++/* Within mask LTDC_LXBFCR_BF2 */ ++#define LXBFCR_BF2_PAXCA 0x007 /* 1 - (Pixel Alpha x Constant Alpha) */ ++#define LXBFCR_BF2_CA 0x005 /* 1 - Constant Alpha */ ++ ++#define LAY_OFS(a) (0x100 + (a)) ++#define LTDC_L2RCR LAY_OFS(0x108) ++#define LXCR_RCR_IMR BIT(0) ++#define LXCR_RCR_VBR BIT(1) ++#define LTDC_L2CR LAY_OFS(0x10c) ++#define LXCR_LEN BIT(0) ++#define LTDC_L2WHPCR LAY_OFS(0x110) ++#define LTDC_L2WVPCR LAY_OFS(0x114) ++#define LTDC_L2PFCR LAY_OFS(0x11c) ++#define LTDC_L2CACR LAY_OFS(0x120) ++#define LTDC_L2DCCR LAY_OFS(0x124) ++#define LTDC_L2BFCR LAY_OFS(0x128) ++#define LTDC_L2CFBAR LAY_OFS(0x134) ++#define LTDC_L2CFBLR LAY_OFS(0x138) ++#define LTDC_L2CFBLNR LAY_OFS(0x13c) ++ ++/* Timeout when polling on status */ ++#define LTDC_TIMEOUT_US U(100000) ++ ++#if (TRACE_LEVEL < TRACE_DEBUG) ++#define dump_reg_ltdc(...) ((void)0) ++#else ++#define DUMP_REG(name) DMSG("%-16s (%#05x) 0x%08x\n", \ ++ #name, name, io_read32(base + name)) ++ ++static void dump_reg_ltdc(vaddr_t base) ++{ ++ DUMP_REG(LTDC_IDR); ++ DUMP_REG(LTDC_LCR); ++ DUMP_REG(LTDC_SSCR); ++ DUMP_REG(LTDC_BPCR); ++ DUMP_REG(LTDC_AWCR); ++ DUMP_REG(LTDC_TWCR); ++ ++ DUMP_REG(LTDC_L2CR); ++ DUMP_REG(LTDC_L2WHPCR); ++ DUMP_REG(LTDC_L2WVPCR); ++ DUMP_REG(LTDC_L2PFCR); ++ DUMP_REG(LTDC_L2CACR); ++ DUMP_REG(LTDC_L2DCCR); ++ DUMP_REG(LTDC_L2BFCR); ++ DUMP_REG(LTDC_L2CFBAR); ++ DUMP_REG(LTDC_L2CFBLR); ++ DUMP_REG(LTDC_L2CFBLNR); ++} ++#endif ++ ++static TEE_Result stm32_ltdc_init(void *device) ++{ ++ struct ltdc_device *ldev = device; ++ TEE_Result ret = TEE_SUCCESS; ++ uint32_t gcr = 0; ++ const struct stm32_firewall_cfg sec_cfg[] = { ++ { FWLL_SEC_RW | FWLL_NSEC_READ | FWLL_MASTER(0) }, ++ { }, /* Null terminated */ ++ }; ++ ++ clk_enable(ldev->clock); ++ ++ gcr = io_read32(ldev->regs + LTDC_GCR); ++ if (!(gcr & GCR_LTDCEN)) { ++ EMSG("CRTC must be started first"); ++ ret = TEE_ERROR_GENERIC; ++ goto err; ++ } ++ ++ ret = stm32_pinctrl_set_secure_cfg(ldev->pinctrl_list, true); ++ if (ret) { ++ /* Restore pins to non-secure state, should not fail */ ++ if (stm32_pinctrl_set_secure_cfg(ldev->pinctrl_list, false)) ++ panic(); ++ ++ goto err; ++ } ++ ++ /* LTDC goes non-secure read, secure write */ ++ ret = stm32_firewall_set_config(virt_to_phys((void *)ldev->regs), ++ 0, sec_cfg); ++ if (ret) ++ goto err; ++ ++ return ret; ++err: ++ clk_disable(ldev->clock); ++ ++ return ret; ++} ++ ++static TEE_Result stm32_ltdc_final(void *device) ++{ ++ TEE_Result ret = TEE_ERROR_GENERIC; ++ struct ltdc_device *ldev = device; ++ uint64_t timeout_ref = 0; ++ const struct stm32_firewall_cfg sec_cfg[] = { ++ { FWLL_NSEC_RW | FWLL_MASTER(0) }, ++ { }, /* Null terminated */ ++ }; ++ ++ if (!ldev->activate) ++ goto out; ++ ++ /* Disable secure layer */ ++ io_clrbits32(ldev->regs + LTDC_L2CR, LXCR_LEN); ++ ++ /* Reload configuration immediately. */ ++ io_write32(ldev->regs + LTDC_L2RCR, LXCR_RCR_VBR); ++ ++ ldev->end_of_frame = false; ++ ++ /* Enable line IRQ */ ++ io_setbits32(ldev->regs + LTDC_IER2, IER_LIE); ++ ++ /* wait end of frame */ ++ timeout_ref = timeout_init_us(LTDC_TIMEOUT_US); ++ while (!timeout_elapsed(timeout_ref)) ++ if (ldev->end_of_frame) ++ break; ++ ++ /* Disable IRQs */ ++ io_clrbits32(ldev->regs + LTDC_IER2, IER_LIE | IER_FUKIE | IER_TERRIE); ++ ++ ret = stm32_firewall_set_config(virt_to_phys((void *)ldev->regs), ++ 0, sec_cfg); ++ if (ret) ++ goto out; ++ ++ ret = stm32_pinctrl_set_secure_cfg(ldev->pinctrl_list, false); ++ /* Restoring non-secure state for pins should not fail */ ++ assert(ret == TEE_SUCCESS); ++ ++out: ++ clk_disable(ldev->clock); ++ ++ ldev->activate = false; ++ ++ return ret; ++} ++ ++static TEE_Result stm32_ltdc_activate(void *device, ++ const struct frame_buffer *fb, ++ uint32_t x0, uint32_t y0) ++{ ++ struct ltdc_device *ldev = device; ++ paddr_t fb_pbase = virt_to_phys(fb->base); ++ TEE_Result ret = TEE_SUCCESS; ++ uint32_t value = 0; ++ uint32_t x1 = 0; ++ uint32_t y1 = 0; ++ uint32_t width_crtc = 0; ++ uint32_t height_crtc = 0; ++ uint32_t bpcr = 0; ++ uint32_t awcr = 0; ++ ++ if (!fb) { ++ ret = TEE_ERROR_GENERIC; ++ goto err; ++ } ++ ++ x1 = x0 + fb->width; ++ y1 = y0 + fb->height; ++ ++ /* Check framebuffer size */ ++ awcr = io_read32(ldev->regs + LTDC_AWCR); ++ bpcr = io_read32(ldev->regs + LTDC_BPCR); ++ ++ height_crtc = (awcr & 0xffff) - (bpcr & 0xffff); ++ width_crtc = (awcr >> 16) - (bpcr >> 16); ++ ++ if (fb->height > height_crtc || fb->width > width_crtc) { ++ EMSG("framebuffer size %dx%d exceed size of panel %dx%d", ++ fb->width, fb->height, width_crtc, height_crtc); ++ ret = TEE_ERROR_GENERIC; ++ goto err; ++ } ++ ++ DMSG("LTDC base %"PRIxVA", FB base %#"PRIxPA, ldev->regs, fb_pbase); ++ ++ /* Configure the horizontal start and stop position */ ++ value = (x0 + ((bpcr & LTDC_BPCR_AHBP) >> 16) + 1) | ++ ((x1 + ((bpcr & LTDC_BPCR_AHBP) >> 16)) << 16); ++ io_clrsetbits32(ldev->regs + LTDC_L2WHPCR, ++ LTDC_LXWHPCR_WHSTPOS | LTDC_LXWHPCR_WHSPPOS, value); ++ ++ /* Configure the vertical start and stop position */ ++ value = (y0 + (bpcr & LTDC_BPCR_AVBP) + 1) | ++ ((y1 + (bpcr & LTDC_BPCR_AVBP)) << 16); ++ ++ io_clrsetbits32(ldev->regs + LTDC_L2WVPCR, ++ LTDC_LXWVPCR_WVSTPOS | LTDC_LXWVPCR_WVSPPOS, value); ++ ++ /* Specifies the pixel format, hard coded */ ++ io_clrbits32(ldev->regs + LTDC_L2PFCR, LTDC_LXPFCR_PF); ++ io_setbits32(ldev->regs + LTDC_L2PFCR, LXPFCR_PF_ARGB8888); ++ ++ /* Configure the default color values, hard coded */ ++ io_clrbits32(ldev->regs + LTDC_L2DCCR, ++ LTDC_LXDCCR_DCBLUE | LTDC_LXDCCR_DCGREEN | ++ LTDC_LXDCCR_DCRED | LTDC_LXDCCR_DCALPHA); ++ io_setbits32(ldev->regs + LTDC_L2DCCR, 0x00FFFFFF); ++ ++ /* Specifies the constant alpha value, hard coded. */ ++ io_clrbits32(ldev->regs + LTDC_L2CACR, LTDC_LXCACR_CONSTA); ++ io_setbits32(ldev->regs + LTDC_L2CACR, 0xFF); ++ ++ /* Specifies the blending factors, hard coded. */ ++ io_clrbits32(ldev->regs + LTDC_L2BFCR, LXBFCR_BF2 | LXBFCR_BF1); ++ io_setbits32(ldev->regs + LTDC_L2BFCR, ++ LXBFCR_BF1_PAXCA | LXBFCR_BF2_PAXCA); ++ ++ /* Configure the color frame buffer start address. */ ++ io_clrbits32(ldev->regs + LTDC_L2CFBAR, LTDC_LXCFBAR_CFBADD); ++ io_setbits32(ldev->regs + LTDC_L2CFBAR, fb_pbase); ++ ++ /* Configure the color frame buffer pitch in byte, assuming ARGB32. */ ++ value = ((fb->width * 4) << 16) | (((x1 - x0) * 4) + 3); ++ io_clrsetbits32(ldev->regs + LTDC_L2CFBLR, ++ LTDC_LXCFBLR_CFBLL | LTDC_LXCFBLR_CFBP, value); ++ ++ /* Configure the frame buffer line number. */ ++ io_clrsetbits32(ldev->regs + LTDC_L2CFBLNR, ++ LTDC_LXCFBLNR_CFBLNBR, fb->height); ++ ++ /* Enable LTDC_Layer by setting LEN bit. */ ++ io_setbits32(ldev->regs + LTDC_L2CR, LXCR_LEN); ++ ++ /* Reload configuration at next vertical blanking. */ ++ io_write32(ldev->regs + LTDC_L2RCR, LXCR_RCR_VBR); ++ ++ /* Enable IRQs */ ++ io_setbits32(ldev->regs + LTDC_IER2, IER_FUKIE | IER_TERRIE); ++ ++ ldev->activate = true; ++ ++ return ret; ++err: ++ dump_reg_ltdc(ldev->regs); ++ ++ return ret; ++} ++ ++static TEE_Result stm32_ltdc_get_display_size(void *device, ++ uint32_t *width, ++ uint32_t *height) ++{ ++ struct ltdc_device *ldev = device; ++ TEE_Result ret = TEE_SUCCESS; ++ uint32_t bpcr = 0; ++ uint32_t awcr = 0; ++ uint32_t gcr = 0; ++ ++ assert(ldev && ldev->regs); ++ ++ if (!width || !height) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ clk_enable(ldev->clock); ++ ++ gcr = io_read32(ldev->regs + LTDC_GCR); ++ if (!(gcr & GCR_LTDCEN)) { ++ EMSG("CRTC must be started first"); ++ ret = TEE_ERROR_GENERIC; ++ goto out; ++ } ++ ++ awcr = io_read32(ldev->regs + LTDC_AWCR); ++ bpcr = io_read32(ldev->regs + LTDC_BPCR); ++ ++ *height = (awcr & 0xffff) - (bpcr & 0xffff); ++ *width = (awcr >> 16) - (bpcr >> 16); ++out: ++ clk_disable(ldev->clock); ++ ++ return ret; ++} ++ ++static enum itr_return stm32_ltdc_it_handler(struct itr_handler *handler) ++{ ++ struct ltdc_device *ldev = handler->data; ++ uint32_t irq_status = 0; ++ ++ irq_status = io_read32(ldev->regs + LTDC_ISR2); ++ io_write32(ldev->regs + LTDC_ICR2, irq_status); ++ ++ if (irq_status & ISR_FUKIF) ++ EMSG("ltdc fifo underrun: please verify display mode"); ++ ++ if (irq_status & ISR_TERRIF) ++ EMSG("ltdc transfer error"); ++ ++ if (irq_status & ISR_LIF) ++ ldev->end_of_frame = true; ++ ++ return ITRR_HANDLED; ++} ++DECLARE_KEEP_PAGER(stm32_ltdc_it_handler); ++ ++static struct disp_dev_list ltdc_dev = { ++ .device_init = stm32_ltdc_init, ++ .device_final = stm32_ltdc_final, ++ .device_activate = stm32_ltdc_activate, ++ .device_get_display_size = stm32_ltdc_get_display_size, ++}; ++ ++static TEE_Result stm32_ltdc_probe(const void *fdt, int node, ++ const void *compat_data __unused) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ struct ltdc_device *ldev = NULL; ++ struct dt_node_info dt_info = { }; ++ uint32_t hwid = 0; ++ const uint32_t *cuint = NULL; ++ uint32_t interrupt0 = 0; ++ uint32_t interrupt1 = 0; ++ int len = 0; ++ ++ ldev = calloc(1, sizeof(*ldev)); ++ if (!ldev) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ _fdt_fill_device_info(fdt, &dt_info, node); ++ ++ if (dt_info.reg == DT_INFO_INVALID_REG || ++ dt_info.reg_size == DT_INFO_INVALID_REG_SIZE || ++ dt_info.clock == DT_INFO_INVALID_CLOCK || ++ dt_info.interrupt == DT_INFO_INVALID_INTERRUPT) ++ goto err; ++ ++ ldev->io_base.pa = dt_info.reg; ++ if (ldev->io_base.pa == 0) ++ panic("LTDC iomem missing"); ++ ++ ldev->regs = io_pa_or_va_secure(&ldev->io_base, dt_info.reg_size); ++ ++ res = clk_dt_get_by_index(fdt, node, 0, &ldev->clock); ++ if (res) ++ goto err; ++ ++ clk_enable(ldev->clock); ++ ++ hwid = io_read32(ldev->regs + LTDC_IDR); ++ ++ if (hwid != ID_HWVER_40100) { ++ EMSG("LTDC hardware version not supported: 0x%x", hwid); ++ res = TEE_ERROR_NOT_SUPPORTED; ++ goto err; ++ } ++ ++ cuint = fdt_getprop(fdt, node, "interrupts", &len); ++ if (cuint) { ++ interrupt0 = (uint32_t)fdt32_to_cpu(*(cuint + 1)) + 32; ++ interrupt1 = (uint32_t)fdt32_to_cpu(*(cuint + 4)) + 32; ++ } ++ ++ ldev->itr0 = itr_alloc_add((size_t)interrupt0, ++ stm32_ltdc_it_handler, ++ ITRF_TRIGGER_LEVEL, ++ (void *)ldev); ++ if (!ldev->itr0) { ++ res = TEE_ERROR_OUT_OF_MEMORY; ++ goto err; ++ } ++ ++ ldev->itr1 = itr_alloc_add((size_t)interrupt1, ++ stm32_ltdc_it_handler, ++ ITRF_TRIGGER_LEVEL, ++ (void *)ldev); ++ if (!ldev->itr1) { ++ res = TEE_ERROR_OUT_OF_MEMORY; ++ goto err; ++ } ++ ++ itr_enable(ldev->itr0->it); ++ itr_enable(ldev->itr1->it); ++ ++ res = stm32_pinctrl_dt_get_by_index(fdt, node, 0, &ldev->pinctrl_list); ++ if (res) ++ goto err; ++ ++ ltdc_dev.device = ldev; ++ display_register_device(<dc_dev); ++ ++ /* Force the LTDC to non secure access */ ++ stm32_ltdc_final(ldev); ++ ++ return TEE_SUCCESS; ++err: ++ free(ldev); ++ ++ return res; ++} ++ ++static const struct dt_device_match ltdc_match_table[] = { ++ { .compatible = "st,stm32-ltdc" }, ++ { } ++}; ++ ++DEFINE_DT_DRIVER(stm32_ltdc_dt_driver) = { ++ .name = "stm32-ltdc", ++ .match_table = ltdc_match_table, ++ .probe = stm32_ltdc_probe, ++}; +diff --git a/core/drivers/stm32_rng.c b/core/drivers/stm32_rng.c +index 29c852469..4e1da48c4 100644 +--- a/core/drivers/stm32_rng.c ++++ b/core/drivers/stm32_rng.c +@@ -1,9 +1,10 @@ + // SPDX-License-Identifier: BSD-3-Clause + /* +- * Copyright (c) 2018-2019, STMicroelectronics ++ * Copyright (c) 2018-2022, STMicroelectronics + */ + + #include ++#include + #include + #include + #include +@@ -13,20 +14,24 @@ + #include + #include + #include ++#include + #include +-#include ++#include + #include + #include + #include ++#include + +-#define DT_RNG_COMPAT "st,stm32-rng" +-#define RNG_CR 0x00U +-#define RNG_SR 0x04U +-#define RNG_DR 0x08U ++#define RNG_CR U(0x00) ++#define RNG_SR U(0x04) ++#define RNG_DR U(0x08) + + #define RNG_CR_RNGEN BIT(2) + #define RNG_CR_IE BIT(3) + #define RNG_CR_CED BIT(5) ++#define RNG_CR_CONDRST BIT(30) ++#define RNG_CR_CLKDIV GENMASK_32(19, 16) ++#define RNG_CR_CLKDIV_SHIFT U(16) + + #define RNG_SR_DRDY BIT(0) + #define RNG_SR_CECS BIT(1) +@@ -34,218 +39,428 @@ + #define RNG_SR_CEIS BIT(5) + #define RNG_SR_SEIS BIT(6) + +-#define RNG_TIMEOUT_US 10000 ++#define RNG_TIMEOUT_US_10MS U(10000) ++#define RNG_TIMEOUT_US_1MS U(1000) ++#define RNG_FIFO_BYTE_DEPTH U(16) + +-struct stm32_rng_instance { +- struct io_pa_va base; +- struct clk *clock; ++#define RNG_NIST_CONFIG_A U(0x0F00D00) ++#define RNG_NIST_CONFIG_B U(0x1801000) ++#define RNG_NIST_CONFIG_MASK GENMASK_32(25, 8) ++ ++#define RNG_MAX_CLOCK_FREQ U(750000) ++ ++struct stm32_rng_driver_data { ++ bool has_cond_reset; ++}; ++ ++struct stm32_rng_device { ++ struct stm32_rng_platdata pdata; ++ struct stm32_rng_driver_data *ddata; + unsigned int lock; +- unsigned int refcount; ++ bool error_conceal; ++ uint64_t error_to_ref; ++ uint32_t pm_cr; + }; + +-static struct stm32_rng_instance *stm32_rng; ++/* Expect a single RNG device */ ++static struct stm32_rng_device stm32_rng; + +-/* +- * Extracts from the STM32 RNG specification: +- * +- * When a noise source (or seed) error occurs, the RNG stops generating +- * random numbers and sets to “1” both SEIS and SECS bits to indicate +- * that a seed error occurred. (...) +- +- * The following sequence shall be used to fully recover from a seed +- * error after the RNG initialization: +- * 1. Clear the SEIS bit by writing it to “0”. +- * 2. Read out 12 words from the RNG_DR register, and discard each of +- * them in order to clean the pipeline. +- * 3. Confirm that SEIS is still cleared. Random number generation is +- * back to normal. +- */ +-static void conceal_seed_error(vaddr_t rng_base) ++static vaddr_t get_base(struct stm32_rng_device *dev) ++{ ++ return io_pa_or_va(&dev->pdata.base, 1); ++} ++ ++/* Seed error management */ ++static void conceal_seed_error(struct stm32_rng_device *dev) + { +- if (io_read32(rng_base + RNG_SR) & (RNG_SR_SECS | RNG_SR_SEIS)) { ++ struct stm32_rng_driver_data *ddata = dev->ddata; ++ vaddr_t base = get_base(dev); ++ uint32_t sr = 0; ++ ++ if (ddata->has_cond_reset) { ++ if (!dev->error_conceal) { ++ /* Conceal by resetting the subsystem */ ++ sr = io_read32(base + RNG_SR); ++ if (sr & RNG_SR_SECS) { ++ io_setbits32(base + RNG_CR, RNG_CR_CONDRST); ++ io_clrbits32(base + RNG_CR, RNG_CR_CONDRST); ++ ++ dev->error_to_ref = ++ timeout_init_us(RNG_TIMEOUT_US_10MS); ++ dev->error_conceal = true; ++ ++ /* Wait subsystem reset cycle completes */ ++ return; ++ } ++ } else { ++ /* Measure time before possible reschedule */ ++ bool timed_out = timeout_elapsed(dev->error_to_ref); ++ ++ if ((io_read32(base + RNG_CR) & RNG_CR_CONDRST)) { ++ if (timed_out) ++ panic(); ++ ++ /* Wait subsystem reset cycle completes */ ++ return; ++ } ++ ++ dev->error_conceal = false; ++ } ++ ++ io_clrbits32(base + RNG_SR, RNG_SR_SEIS); ++ } else { + size_t i = 0; + +- io_mask32(rng_base + RNG_SR, 0, RNG_SR_SEIS); ++ io_mask32(base + RNG_SR, 0, RNG_SR_SEIS); + + for (i = 12; i != 0; i--) +- (void)io_read32(rng_base + RNG_DR); +- +- if (io_read32(rng_base + RNG_SR) & RNG_SR_SEIS) +- panic("RNG noise"); ++ (void)io_read32(base + RNG_DR); + } +-} + +-#define RNG_FIFO_BYTE_DEPTH 16u ++ if (io_read32(base + RNG_SR) & RNG_SR_SEIS) ++ panic("RNG noise"); ++} + +-TEE_Result stm32_rng_read_raw(vaddr_t rng_base, uint8_t *out, size_t *size) ++static TEE_Result stm32_rng_read_available(struct stm32_rng_device *dev, ++ uint8_t *out, size_t size) + { +- bool enabled = false; +- TEE_Result rc = TEE_ERROR_SECURITY; +- uint32_t exceptions = thread_mask_exceptions(THREAD_EXCP_ALL); +- uint64_t timeout_ref = timeout_init_us(RNG_TIMEOUT_US); ++ vaddr_t base = get_base(dev); ++ uint8_t *buf = out; ++ ++ assert(size <= RNG_FIFO_BYTE_DEPTH); + +- if (!(io_read32(rng_base + RNG_CR) & RNG_CR_RNGEN)) { +- /* Enable RNG if not, clock error is disabled */ +- io_write32(rng_base + RNG_CR, RNG_CR_RNGEN | RNG_CR_CED); +- enabled = true; ++ if (dev->error_conceal || io_read32(base + RNG_SR) & RNG_SR_SEIS) { ++ conceal_seed_error(dev); ++ return TEE_ERROR_NO_DATA; + } + +- /* Wait RNG has produced well seeded random samples */ +- while (!timeout_elapsed(timeout_ref)) { +- conceal_seed_error(rng_base); ++ if (!(io_read32(base + RNG_SR) & RNG_SR_DRDY)) ++ return TEE_ERROR_NO_DATA; + +- if (io_read32(rng_base + RNG_SR) & RNG_SR_DRDY) +- break; ++ /* RNG is ready: read up to 4 32bit words */ ++ while (size) { ++ uint32_t data32 = io_read32(base + RNG_DR); ++ size_t sz = MIN(size, sizeof(uint32_t)); ++ ++ memcpy(buf, &data32, sz); ++ buf += sz; ++ size -= sz; + } + +- if (io_read32(rng_base + RNG_SR) & RNG_SR_DRDY) { +- uint8_t *buf = out; +- size_t req_size = MIN(RNG_FIFO_BYTE_DEPTH, *size); +- size_t len = req_size; ++ return TEE_SUCCESS; ++} + +- /* RNG is ready: read up to 4 32bit words */ +- while (len) { +- uint32_t data32 = io_read32(rng_base + RNG_DR); +- size_t sz = MIN(len, sizeof(uint32_t)); ++static uint32_t stm32_rng_clock_freq_restrain(struct stm32_rng_device *dev) ++{ ++ unsigned long clock_rate = 0; ++ uint32_t clock_div = 0; + +- memcpy(buf, &data32, sz); +- buf += sz; +- len -= sz; +- } +- rc = TEE_SUCCESS; +- *size = req_size; ++ clock_rate = clk_get_rate(dev->pdata.clock); ++ ++ /* ++ * Get the exponent to apply on the CLKDIV field in RNG_CR register ++ * No need to handle the case when clock-div > 0xF as it is physically ++ * impossible ++ */ ++ while ((clock_rate >> clock_div) > RNG_MAX_CLOCK_FREQ) ++ clock_div++; ++ ++ DMSG("RNG clk rate : %lu", clk_get_rate(dev->pdata.clock) >> clock_div); ++ ++ return clock_div; ++} ++ ++static TEE_Result stm32_rng_init(struct stm32_rng_device *dev) ++{ ++ struct stm32_rng_platdata *pdata = &dev->pdata; ++ struct stm32_rng_driver_data *ddata = dev->ddata; ++ vaddr_t base = get_base(dev); ++ uint64_t timeout_ref = 0; ++ uint32_t clock_div = 0; ++ uint32_t sr = 0; ++ uint32_t cr_ced_mask = 0; ++ ++ if (!pdata->clock_error) ++ cr_ced_mask = RNG_CR_CED; ++ ++ /* Clean error indications */ ++ io_write32(base + RNG_SR, 0); ++ ++ if (ddata->has_cond_reset) { ++ clock_div = stm32_rng_clock_freq_restrain(dev); ++ ++ /* Update configuration fields */ ++ io_clrsetbits32(base + RNG_CR, RNG_NIST_CONFIG_MASK, ++ RNG_NIST_CONFIG_B | RNG_CR_CONDRST | ++ cr_ced_mask); ++ io_clrsetbits32(base + RNG_CR, RNG_CR_CLKDIV, ++ (clock_div << RNG_CR_CLKDIV_SHIFT)); ++ ++ /* No need to wait for RNG_CR_CONDRST toggle as we enable clk */ ++ io_clrsetbits32(base + RNG_CR, RNG_CR_CONDRST, RNG_CR_RNGEN); ++ } else { ++ io_setbits32(base + RNG_CR, RNG_CR_RNGEN | cr_ced_mask); + } + +- if (enabled) +- io_write32(rng_base + RNG_CR, 0); ++ timeout_ref = timeout_init_us(RNG_TIMEOUT_US_10MS); ++ while (!(io_read32(base + RNG_SR) & RNG_SR_DRDY)) ++ if (timeout_elapsed(timeout_ref)) ++ break; ++ ++ if (!(io_read32(base + RNG_SR) & RNG_SR_DRDY)) ++ return TEE_ERROR_GENERIC; + +- thread_unmask_exceptions(exceptions); ++ sr = io_read32(base + RNG_SR); ++ if (sr & (RNG_SR_CEIS | RNG_SR_CECS)) ++ DMSG("Clock error detected"); + +- return rc; ++ if (sr & (RNG_SR_SECS | RNG_SR_SEIS)) ++ conceal_seed_error(dev); ++ ++ return TEE_SUCCESS; + } + +-static void gate_rng(bool enable, struct stm32_rng_instance *dev) ++/* Masks interrupts while reading available data from RNG FIFO */ ++static TEE_Result get_rng_bytes_relaxed(uint8_t *out, size_t len) + { +- vaddr_t rng_cr = io_pa_or_va(&dev->base, 1) + RNG_CR; +- uint32_t exceptions = may_spin_lock(&dev->lock); ++ uint64_t timeout_ref = timeout_init_us(RNG_TIMEOUT_US_10MS); ++ TEE_Result res = TEE_ERROR_GENERIC; ++ uint32_t exceptions = 0; ++ bool timed_out = false; ++ uint8_t *buf = out; + +- if (enable) { +- /* incr_refcnt return non zero if resource shall be enabled */ +- if (incr_refcnt(&dev->refcount)) { +- clk_enable(dev->clock); +- io_write32(rng_cr, 0); +- io_write32(rng_cr, RNG_CR_RNGEN | RNG_CR_CED); +- } +- } else { +- /* decr_refcnt return non zero if resource shall be disabled */ +- if (decr_refcnt(&dev->refcount)) { +- io_write32(rng_cr, 0); +- clk_disable(dev->clock); ++ clk_enable(stm32_rng.pdata.clock); ++ ++ while (len) { ++ size_t burst_len = MIN(len, RNG_FIFO_BYTE_DEPTH); ++ ++ exceptions = may_spin_lock(&stm32_rng.lock); ++ ++ res = stm32_rng_read_available(&stm32_rng, buf, burst_len); ++ if (res) { ++ assert(res == TEE_ERROR_NO_DATA); ++ ++ /* Measure timeout before we're possibly rescheduled */ ++ timed_out = timeout_elapsed(timeout_ref); ++ } else { ++ buf += burst_len; ++ len -= burst_len; ++ timeout_ref = timeout_init_us(RNG_TIMEOUT_US_10MS); + } ++ ++ may_spin_unlock(&stm32_rng.lock, exceptions); ++ ++ if (len && timed_out) ++ break; + } + +- may_spin_unlock(&dev->lock, exceptions); ++ clk_disable(stm32_rng.pdata.clock); ++ ++ if (!len) ++ return TEE_SUCCESS; ++ ++ if (timed_out) ++ return TEE_ERROR_NO_DATA; ++ ++ return TEE_ERROR_GENERIC; + } + +-TEE_Result stm32_rng_read(uint8_t *out, size_t size) ++#ifndef CFG_WITH_SOFTWARE_PRNG ++void plat_rng_init(void) + { +- TEE_Result rc = 0; +- uint32_t exceptions = 0; +- vaddr_t rng_base = io_pa_or_va(&stm32_rng->base, 1); +- uint8_t *out_ptr = out; +- size_t out_size = 0; ++ if (!stm32_rng.pdata.base.pa) ++ panic("RNG not available"); ++} ++ ++TEE_Result crypto_rng_read(void *buf, size_t blen) ++#else ++TEE_Result stm32_rng_read(void *buf, size_t blen) ++#endif ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ uint8_t *b = buf; + +- if (!stm32_rng) { +- DMSG("No RNG"); ++ if (!b) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ if (!stm32_rng.pdata.base.va) { ++ EMSG("No RNG"); + return TEE_ERROR_NOT_SUPPORTED; + } + +- gate_rng(true, stm32_rng); ++ res = get_rng_bytes_relaxed(b, blen); ++ if (res) ++ memset(b, 0, blen); ++ ++ return res; ++} ++ ++uint8_t hw_get_random_byte(void) ++{ ++ uint8_t data = 0; + +- while (out_size < size) { +- /* Read by chunks of the size the RNG FIFO depth */ +- size_t sz = size - out_size; ++ if (get_rng_bytes_relaxed(&data, 1)) ++ panic(); + +- exceptions = may_spin_lock(&stm32_rng->lock); ++ return data; ++} + +- rc = stm32_rng_read_raw(rng_base, out_ptr, &sz); ++static TEE_Result stm32_rng_pm_resume(struct stm32_rng_device *dev) ++{ ++ io_write32(get_base(dev) + RNG_CR, RNG_CR_RNGEN | dev->pm_cr); + +- may_spin_unlock(&stm32_rng->lock, exceptions); ++ return TEE_SUCCESS; ++} + +- if (rc) +- goto bail; ++static TEE_Result stm32_rng_pm_suspend(struct stm32_rng_device *dev) ++{ ++ dev->pm_cr = io_read32(get_base(dev) + RNG_CR); + +- out_size += sz; +- out_ptr += sz; +- } ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result ++stm32_rng_pm(enum pm_op op, unsigned int pm_hint __unused, ++ const struct pm_callback_handle *pm_handle __unused) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ ++ res = clk_enable(stm32_rng.pdata.clock); ++ if (res) ++ return res; ++ ++ if (op == PM_OP_RESUME) ++ res = stm32_rng_pm_resume(&stm32_rng); ++ else ++ res = stm32_rng_pm_suspend(&stm32_rng); + +-bail: +- gate_rng(false, stm32_rng); +- if (rc) +- memset(out, 0, size); ++ clk_disable(stm32_rng.pdata.clock); + +- return rc; ++ return res; + } ++DECLARE_KEEP_PAGER(stm32_rng_pm); + + #ifdef CFG_EMBED_DTB +-static TEE_Result stm32_rng_init(void) ++static TEE_Result stm32_rng_parse_fdt(const void *fdt, int node, ++ struct stm32_rng_device *dev) + { +- void *fdt = NULL; +- int node = -1; +- struct dt_node_info dt_info; +- enum teecore_memtypes mtype = MEM_AREA_END; + TEE_Result res = TEE_ERROR_GENERIC; ++ struct dt_node_info dt_rng = { }; ++ struct stm32_rng_platdata *pdata = &dev->pdata; ++ ++ _fdt_fill_device_info(fdt, &dt_rng, node); ++ if (dt_rng.clock == DT_INFO_INVALID_CLOCK || ++ dt_rng.reg == DT_INFO_INVALID_REG || ++ dt_rng.reset == DT_INFO_INVALID_RESET) { ++ return TEE_ERROR_BAD_PARAMETERS; ++ } + +- memset(&dt_info, 0, sizeof(dt_info)); ++ pdata->base.pa = dt_rng.reg; + +- fdt = get_embedded_dt(); +- if (!fdt) +- panic(); ++ pdata->base.va = io_pa_or_va_secure(&pdata->base, dt_rng.reg_size); ++ assert(pdata->base.va); + +- while (true) { +- node = fdt_node_offset_by_compatible(fdt, node, DT_RNG_COMPAT); +- if (node < 0) +- break; ++ pdata->reset = dt_rng.reset; ++ pdata->clock_error = false; + +- _fdt_fill_device_info(fdt, &dt_info, node); ++ res = clk_dt_get_by_index(fdt, node, 0, &pdata->clock); ++ if (res) ++ return res; + +- if (!(dt_info.status & DT_STATUS_OK_SEC)) +- continue; ++ if (fdt_getprop(fdt, node, "clock-error-detect", NULL)) ++ pdata->clock_error = true; + +- if (stm32_rng) +- panic(); ++ return TEE_SUCCESS; ++} + +- stm32_rng = calloc(1, sizeof(*stm32_rng)); +- if (!stm32_rng) +- panic(); ++TEE_Result __weak ++stm32_rng_get_platdata(struct stm32_rng_platdata *pdata __unused) ++{ ++ /* In DT config, the platform data are filled by DT file */ ++ return TEE_SUCCESS; ++} ++#else /* CFG_EMBED_DTB */ ++static TEE_Result stm32_rng_parse_fdt(struct stm32_rng_device *dev __unused) ++{ ++ /* Do nothing, there is no fdt to parse in this case */ ++ return TEE_SUCCESS; ++} ++ ++/* ++ * This function can be overridden by platform to define pdata of stm32 driver ++ */ ++TEE_Result __weak ++stm32_rng_get_platdata(struct stm32_rng_platdata *pdata __unused) ++{ ++ return TEE_ERROR_NOT_IMPLEMENTED; ++} ++#endif /* CFG_EMBED_DTB */ + +- assert(dt_info.clock != DT_INFO_INVALID_CLOCK && +- dt_info.reg != DT_INFO_INVALID_REG && +- dt_info.reg_size != DT_INFO_INVALID_REG_SIZE); ++static TEE_Result stm32_rng_probe(const void *fdt, int offs, ++ const void *compat_data) ++{ ++ TEE_Result res = TEE_SUCCESS; + +- if (dt_info.status & DT_STATUS_OK_NSEC) { +- stm32mp_register_non_secure_periph_iomem(dt_info.reg); +- mtype = MEM_AREA_IO_NSEC; +- } else { +- stm32mp_register_secure_periph_iomem(dt_info.reg); +- mtype = MEM_AREA_IO_SEC; +- } ++ /* Expect a single RNG instance */ ++ assert(!stm32_rng.pdata.base.pa); + +- stm32_rng->base.pa = dt_info.reg; +- stm32_rng->base.va = (vaddr_t)phys_to_virt(dt_info.reg, mtype, +- dt_info.reg_size); ++ res = stm32_rng_get_platdata(&stm32_rng.pdata); ++ if (res) ++ goto err; + +- res = clk_dt_get_by_index(fdt, node, 0, &stm32_rng->clock); +- if (res) +- return res; ++ res = stm32_rng_parse_fdt(fdt, offs, &stm32_rng); ++ if (res) ++ goto err; + +- assert(stm32_rng->clock); ++ stm32_rng.ddata = (struct stm32_rng_driver_data *)compat_data; + +- DMSG("RNG init"); +- } ++ res = clk_enable(stm32_rng.pdata.clock); ++ if (res) ++ goto err; ++ ++ res = stm32_reset_assert(stm32_rng.pdata.reset, RNG_TIMEOUT_US_1MS); ++ if (res) ++ goto err; ++ ++ res = stm32_reset_deassert(stm32_rng.pdata.reset, RNG_TIMEOUT_US_1MS); ++ if (res) ++ goto err; ++ ++ res = stm32_rng_init(&stm32_rng); ++ if (res) ++ goto err; ++ ++ clk_disable(stm32_rng.pdata.clock); ++ ++#ifdef CFG_STM32MP15 ++ stm32mp_register_secure_periph_iomem(stm32_rng.pdata.base.pa); ++#endif ++ ++ register_pm_core_service_cb(stm32_rng_pm, &stm32_rng, "rng-service"); + + return TEE_SUCCESS; ++ ++err: ++ memset(&stm32_rng, 0, sizeof(stm32_rng)); ++ return res; + } + +-driver_init(stm32_rng_init); +-#endif /*CFG_EMBED_DTB*/ ++static const struct stm32_rng_driver_data mp13_data[] = { ++ { .has_cond_reset = true }, ++}; ++ ++static const struct stm32_rng_driver_data mp15_data[] = { ++ { .has_cond_reset = false }, ++}; ++ ++static const struct dt_device_match rng_match_table[] = { ++ { .compatible = "st,stm32mp13-rng", .compat_data = &mp13_data }, ++ { .compatible = "st,stm32-rng", .compat_data = &mp15_data }, ++ { } ++}; ++ ++DEFINE_DT_DRIVER(stm32_rng_dt_driver) = { ++ .name = "stm32_rng", ++ .match_table = rng_match_table, ++ .probe = stm32_rng_probe, ++}; +diff --git a/core/drivers/stm32_rtc.c b/core/drivers/stm32_rtc.c +new file mode 100644 +index 000000000..27a914044 +--- /dev/null ++++ b/core/drivers/stm32_rtc.c +@@ -0,0 +1,502 @@ ++// SPDX-License-Identifier: BSD-3-Clause ++/* ++ * Copyright (c) 2018-2022, STMicroelectronics - All Rights Reserved ++ * ++ */ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#define RTC_TR U(0x00) ++#define RTC_DR U(0x04) ++#define RTC_SSR U(0x08) ++#define RTC_ICSR U(0x0C) ++#define RTC_PRER U(0x10) ++#define RTC_WUTR U(0x14) ++#define RTC_CR U(0x18) ++#define RTC_PRIVCFGR U(0x1C) ++/* RTC_SMCR is linked to RTC3v1_2 */ ++#define RTC_SMCR U(0x20) ++/* RTC_SECCFGR is linked to RTC3v3_2 and above */ ++#define RTC_SECCFGR U(0x20) ++#define RTC_WPR U(0x24) ++#define RTC_CALR U(0x28) ++#define RTC_SHIFTR U(0x2C) ++#define RTC_TSTR U(0x30) ++#define RTC_TSDR U(0x34) ++#define RTC_TSSSR U(0x38) ++#define RTC_ALRMAR U(0x40) ++#define RTC_ALRMASSR U(0x44) ++#define RTC_ALRMBR U(0x48) ++#define RTC_ALRMBSSR U(0x4C) ++#define RTC_SR U(0x50) ++#define RTC_SCR U(0x5C) ++#define RTC_OR U(0x60) ++ ++#define RTC_TR_SU_MASK GENMASK_32(3, 0) ++#define RTC_TR_ST_MASK GENMASK_32(6, 4) ++#define RTC_TR_ST_SHIFT U(4) ++#define RTC_TR_MNU_MASK GENMASK_32(11, 8) ++#define RTC_TR_MNU_SHIFT U(8) ++#define RTC_TR_MNT_MASK GENMASK_32(14, 12) ++#define RTC_TR_MNT_SHIFT U(12) ++#define RTC_TR_HU_MASK GENMASK_32(19, 16) ++#define RTC_TR_HU_SHIFT U(16) ++#define RTC_TR_HT_MASK GENMASK_32(21, 20) ++#define RTC_TR_HT_SHIFT U(20) ++#define RTC_TR_PM BIT(22) ++ ++#define RTC_DR_DU_MASK GENMASK_32(3, 0) ++#define RTC_DR_DT_MASK GENMASK_32(5, 4) ++#define RTC_DR_DT_SHIFT U(4) ++#define RTC_DR_MU_MASK GENMASK_32(11, 8) ++#define RTC_DR_MU_SHIFT U(8) ++#define RTC_DR_MT BIT(12) ++#define RTC_DR_MT_SHIFT U(12) ++#define RTC_DR_WDU_MASK GENMASK_32(15, 13) ++#define RTC_DR_WDU_SHIFT U(13) ++#define RTC_DR_YU_MASK GENMASK_32(19, 16) ++#define RTC_DR_YU_SHIFT U(16) ++#define RTC_DR_YT_MASK GENMASK_32(23, 20) ++#define RTC_DR_YT_SHIFT U(20) ++ ++#define RTC_SSR_SS_MASK GENMASK_32(15, 0) ++ ++#define RTC_ICSR_RSF BIT(5) ++ ++#define RTC_PRER_PREDIV_S_MASK GENMASK_32(14, 0) ++ ++#define RTC_CR_BYPSHAD BIT(5) ++#define RTC_CR_BYPSHAD_SHIFT U(5) ++#define RTC_CR_TAMPTS BIT(25) ++ ++#define RTC_SMCR_TS_DPROT BIT(3) ++#define RTC_SR_TSF BIT(3) ++#define RTC_SCR_CTSF BIT(3) ++#define RTC_SR_TSOVF BIT(4) ++#define RTC_SCR_CTSOVF BIT(4) ++ ++#define RTC_TSDR_MU_MASK GENMASK_32(11, 8) ++#define RTC_TSDR_MU_SHIFT U(8) ++#define RTC_TSDR_DT_MASK GENMASK_32(5, 4) ++#define RTC_TSDR_DT_SHIFT U(4) ++#define RTC_TSDR_DU_MASK GENMASK_32(3, 0) ++#define RTC_TSDR_DU_SHIFT U(0) ++ ++#define RTC_WPR_KEY1 U(0xCA) ++#define RTC_WPR_KEY2 U(0x53) ++#define RTC_WPR_KEY_LOCK U(0xFF) ++ ++#define RTC_PRIVCFGR_FULL_PRIV BIT(15) ++#define RTC_PRIVCFGR_VALUES GENMASK_32(3, 0) ++#define RTC_PRIVCFGR_VALUES_TO_SHIFT GENMASK_32(5, 4) ++#define RTC_PRIVCFGR_SHIFT U(9) ++#define RTC_PRIVCFGR_MASK (GENMASK_32(14, 13) | GENMASK_32(3, 0)) ++ ++#define RTC_SECCFGR_FULL_SEC BIT(15) ++#define RTC_SECCFGR_VALUES GENMASK_32(3, 0) ++#define RTC_SECCFGR_VALUES_TO_SHIFT GENMASK_32(5, 4) ++#define RTC_SECCFGR_SHIFT U(9) ++#define RTC_SECCFGR_TS_SEC BIT(3) ++#define RTC_SECCFGR_MASK (GENMASK_32(14, 13) | GENMASK_32(3, 0)) ++ ++#define RTC_FLAGS_READ_TWICE BIT(0) ++#define RTC_FLAGS_SECURE BIT(1) ++ ++#define TIMEOUT_US_RTC_SHADOW U(10000) ++ ++struct rtc_compat { ++ bool has_seccfgr; ++}; ++ ++struct rtc_device { ++ struct io_pa_va base; ++ struct rtc_compat compat; ++ struct clk *pclk; ++ struct clk *rtc_ck; ++ uint8_t flags; ++}; ++ ++/* Expect a single RTC instance */ ++static struct rtc_device rtc_dev; ++ ++static vaddr_t get_base(void) ++{ ++ assert(rtc_dev.base.pa); ++ ++ return io_pa_or_va(&rtc_dev.base, 1); ++} ++ ++static void stm32_rtc_write_unprotect(void) ++{ ++ vaddr_t rtc_base = get_base(); ++ ++ io_write32(rtc_base + RTC_WPR, RTC_WPR_KEY1); ++ io_write32(rtc_base + RTC_WPR, RTC_WPR_KEY2); ++} ++ ++static void stm32_rtc_write_protect(void) ++{ ++ vaddr_t rtc_base = get_base(); ++ ++ io_write32(rtc_base + RTC_WPR, RTC_WPR_KEY_LOCK); ++} ++ ++static bool stm32_rtc_get_bypshad(void) ++{ ++ return io_read32(get_base() + RTC_CR) & RTC_CR_BYPSHAD; ++} ++ ++/* Get calendar data from RTC devicecalendar valueregister values */ ++static void stm32_rtc_read_calendar(struct stm32_rtc_calendar *calendar) ++{ ++ vaddr_t rtc_base = get_base(); ++ bool bypshad = stm32_rtc_get_bypshad(); ++ ++ if (!bypshad) { ++ uint64_t to = timeout_init_us(TIMEOUT_US_RTC_SHADOW); ++ ++ /* Wait calendar registers are ready */ ++ io_clrbits32(rtc_base + RTC_ICSR, RTC_ICSR_RSF); ++ ++ while (!(io_read32(rtc_base + RTC_ICSR) & RTC_ICSR_RSF)) ++ if (timeout_elapsed(to)) ++ break; ++ ++ if (!(io_read32(rtc_base + RTC_ICSR) & RTC_ICSR_RSF)) ++ panic(); ++ } ++ ++ calendar->ssr = io_read32(rtc_base + RTC_SSR); ++ calendar->tr = io_read32(rtc_base + RTC_TR); ++ calendar->dr = io_read32(rtc_base + RTC_DR); ++} ++ ++/* Fill the RTC timestamp structure from a given RTC time-in-day value */ ++static void stm32_rtc_get_time(struct stm32_rtc_calendar *cal, ++ struct stm32_rtc_time *tm) ++{ ++ tm->hour = (((cal->tr & RTC_TR_HT_MASK) >> RTC_TR_HT_SHIFT) * 10) + ++ ((cal->tr & RTC_TR_HU_MASK) >> RTC_TR_HU_SHIFT); ++ ++ if (cal->tr & RTC_TR_PM) ++ tm->hour += 12; ++ ++ tm->min = (((cal->tr & RTC_TR_MNT_MASK) >> RTC_TR_MNT_SHIFT) * 10) + ++ ((cal->tr & RTC_TR_MNU_MASK) >> RTC_TR_MNU_SHIFT); ++ tm->sec = (((cal->tr & RTC_TR_ST_MASK) >> RTC_TR_ST_SHIFT) * 10) + ++ (cal->tr & RTC_TR_SU_MASK); ++} ++ ++/* Fill the RTC timestamp structure from a given RTC date value */ ++static void stm32_rtc_get_date(struct stm32_rtc_calendar *cal, ++ struct stm32_rtc_time *tm) ++{ ++ tm->wday = (((cal->dr & RTC_DR_WDU_MASK) >> RTC_DR_WDU_SHIFT)); ++ ++ tm->day = (((cal->dr & RTC_DR_DT_MASK) >> RTC_DR_DT_SHIFT) * 10) + ++ (cal->dr & RTC_DR_DU_MASK); ++ ++ tm->month = (((cal->dr & RTC_DR_MT) >> RTC_DR_MT_SHIFT) * 10) + ++ ((cal->dr & RTC_DR_MU_MASK) >> RTC_DR_MU_SHIFT); ++ ++ tm->year = (((cal->dr & RTC_DR_YT_MASK) >> RTC_DR_YT_SHIFT) * 10) + ++ ((cal->dr & RTC_DR_YU_MASK) >> RTC_DR_YU_SHIFT) + 2000; ++} ++ ++/* Update time value with RTC timestamp */ ++static void stm32_rtc_read_timestamp(struct stm32_rtc_time *time) ++{ ++ struct stm32_rtc_calendar cal_tamp = { }; ++ vaddr_t rtc_base = get_base(); ++ ++ cal_tamp.tr = io_read32(rtc_base + RTC_TSTR); ++ cal_tamp.dr = io_read32(rtc_base + RTC_TSDR); ++ stm32_rtc_get_time(&cal_tamp, time); ++ stm32_rtc_get_date(&cal_tamp, time); ++} ++ ++void stm32_rtc_get_calendar(struct stm32_rtc_calendar *calendar) ++{ ++ clk_enable(rtc_dev.pclk); ++ ++ stm32_rtc_read_calendar(calendar); ++ ++ /* RTC may need to be read twice, depending of clocks configuration */ ++ if (rtc_dev.flags & RTC_FLAGS_READ_TWICE) { ++ uint32_t tr_save = calendar->tr; ++ ++ stm32_rtc_read_calendar(calendar); ++ ++ if (calendar->tr != tr_save) ++ stm32_rtc_read_calendar(calendar); ++ } ++ ++ clk_disable(rtc_dev.pclk); ++} ++ ++/* Return difference in milliseconds on second fraction */ ++static uint32_t stm32_rtc_get_second_fraction(struct stm32_rtc_calendar *cal) ++{ ++ uint32_t prediv_s = io_read32(get_base() + RTC_PRER) & ++ RTC_PRER_PREDIV_S_MASK; ++ uint32_t ss = cal->ssr & RTC_SSR_SS_MASK; ++ ++ return ((prediv_s - ss) * 1000) / (prediv_s + 1); ++} ++ ++/* Return absolute difference in milliseconds on second fraction */ ++static signed long long stm32_rtc_diff_frac(struct stm32_rtc_calendar *cur, ++ struct stm32_rtc_calendar *ref) ++{ ++ return (signed long long)stm32_rtc_get_second_fraction(cur) - ++ (signed long long)stm32_rtc_get_second_fraction(ref); ++} ++ ++/* Return absolute difference in milliseconds on seconds-in-day fraction */ ++static signed long long stm32_rtc_diff_time(struct stm32_rtc_time *current, ++ struct stm32_rtc_time *ref) ++{ ++ signed long long curr_s = 0; ++ signed long long ref_s = 0; ++ ++ curr_s = (signed long long)current->sec + ++ (((signed long long)current->min + ++ (((signed long long)current->hour * 60))) * 60); ++ ++ ref_s = (signed long long)ref->sec + ++ (((signed long long)ref->min + ++ (((signed long long)ref->hour * 60))) * 60); ++ ++ return (curr_s - ref_s) * 1000U; ++} ++ ++static bool stm32_is_a_leap_year(uint32_t year) ++{ ++ return ((year % 4) == 0) && ++ (((year % 100) != 0) || ((year % 400) == 0)); ++} ++ ++/* Return absolute difference in milliseconds on day-in-year fraction */ ++static signed long long stm32_rtc_diff_date(struct stm32_rtc_time *current, ++ struct stm32_rtc_time *ref) ++{ ++ uint32_t diff_in_days = 0; ++ uint32_t m = 0; ++ const uint8_t month_len[NB_MONTHS] = { ++ 31, 28, 31, 30, 31, 30, ++ 31, 31, 30, 31, 30, 31 ++ }; ++ ++ /* Get the number of non-entire month days */ ++ if (current->day >= ref->day) ++ diff_in_days += current->day - ref->day; ++ else ++ diff_in_days += month_len[ref->month - 1] - ++ ref->day + current->day; ++ ++ /* Get the number of entire months, and compute the related days */ ++ if (current->month > (ref->month + 1)) ++ for (m = ref->month + 1; m < current->month && m < 12; m++) ++ diff_in_days += month_len[m - 1]; ++ ++ if (current->month < (ref->month - 1)) { ++ for (m = 1; m < current->month && m < 12; m++) ++ diff_in_days += month_len[m - 1]; ++ ++ for (m = ref->month + 1; m < 12; m++) ++ diff_in_days += month_len[m - 1]; ++ } ++ ++ /* Get complete years */ ++ if (current->year > (ref->year + 1)) ++ diff_in_days += (current->year - ref->year - 1) * 365; ++ ++ /* Particular cases: leap years (one day more) */ ++ if (diff_in_days > 0) { ++ if (current->year == ref->year) { ++ if (stm32_is_a_leap_year(current->year) && ++ ref->month <= 2 && ++ current->month >= 3 && current->day <= 28) ++ diff_in_days++; ++ } else { ++ uint32_t y = 0; ++ ++ /* Ref year is leap */ ++ if (stm32_is_a_leap_year(ref->year) && ++ ref->month <= 2 && ref->day <= 28) ++ diff_in_days++; ++ ++ /* Current year is leap */ ++ if (stm32_is_a_leap_year(current->year) && ++ current->month >= 3) ++ diff_in_days++; ++ ++ /* Interleaved years are leap */ ++ for (y = ref->year + 1; y < current->year; y++) ++ if (stm32_is_a_leap_year(y)) ++ diff_in_days++; ++ } ++ } ++ ++ return (24 * 60 * 60 * 1000) * (signed long long)diff_in_days; ++} ++ ++unsigned long long stm32_rtc_diff_calendar(struct stm32_rtc_calendar *cur, ++ struct stm32_rtc_calendar *ref) ++{ ++ signed long long diff_in_ms = 0; ++ struct stm32_rtc_time curr_t = { }; ++ struct stm32_rtc_time ref_t = { }; ++ ++ stm32_rtc_get_date(cur, &curr_t); ++ stm32_rtc_get_date(ref, &ref_t); ++ stm32_rtc_get_time(cur, &curr_t); ++ stm32_rtc_get_time(ref, &ref_t); ++ ++ diff_in_ms += stm32_rtc_diff_frac(cur, ref); ++ diff_in_ms += stm32_rtc_diff_time(&curr_t, &ref_t); ++ diff_in_ms += stm32_rtc_diff_date(&curr_t, &ref_t); ++ ++ return (unsigned long long)diff_in_ms; ++} ++ ++void stm32_rtc_get_timestamp(struct stm32_rtc_time *tamp_ts) ++{ ++ vaddr_t rtc_base = get_base(); ++ ++ clk_enable(rtc_dev.pclk); ++ ++ if (io_read32(rtc_base + RTC_SR) & RTC_SR_TSF) { ++ /* Timestamp for tamper event */ ++ stm32_rtc_read_timestamp(tamp_ts); ++ io_setbits32(rtc_base + RTC_SCR, RTC_SCR_CTSF); ++ ++ /* Overflow detection */ ++ if (io_read32(rtc_base + RTC_SR) & RTC_SR_TSOVF) ++ io_setbits32(rtc_base + RTC_SCR, RTC_SCR_CTSOVF); ++ } ++ ++ clk_disable(rtc_dev.pclk); ++} ++ ++void stm32_rtc_set_tamper_timestamp(void) ++{ ++ vaddr_t rtc_base = get_base(); ++ ++ clk_enable(rtc_dev.pclk); ++ ++ stm32_rtc_write_unprotect(); ++ ++ /* Enable tamper timestamper */ ++ io_setbits32(rtc_base + RTC_CR, RTC_CR_TAMPTS); ++ ++ /* Secure Timestamp bit */ ++ if (!rtc_dev.compat.has_seccfgr) { ++ io_clrbits32(rtc_base + RTC_SMCR, RTC_SMCR_TS_DPROT); ++ } else { ++ /* Inverted logic */ ++ io_setbits32(rtc_base + RTC_SECCFGR, RTC_SECCFGR_TS_SEC); ++ } ++ ++ stm32_rtc_write_protect(); ++ ++ clk_disable(rtc_dev.pclk); ++} ++ ++bool stm32_rtc_is_timestamp_enable(void) ++{ ++ bool ret = false; ++ ++ clk_enable(rtc_dev.pclk); ++ ++ ret = io_read32(get_base() + RTC_CR) & RTC_CR_TAMPTS; ++ ++ clk_disable(rtc_dev.pclk); ++ ++ return ret; ++} ++ ++static TEE_Result parse_dt(const void *fdt, int node, ++ const void *compat_data) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ struct dt_node_info dt_info = { }; ++ ++ _fdt_fill_device_info(fdt, &dt_info, node); ++ ++ rtc_dev.base.pa = dt_info.reg; ++ rtc_dev.flags |= RTC_FLAGS_SECURE; ++ io_pa_or_va(&rtc_dev.base, dt_info.reg_size); ++ assert(rtc_dev.base.va); ++ ++ res = clk_dt_get_by_name(fdt, node, "pclk", &rtc_dev.pclk); ++ if (res) ++ return res; ++ ++ res = clk_dt_get_by_name(fdt, node, "rtc_ck", &rtc_dev.rtc_ck); ++ if (!rtc_dev.rtc_ck) ++ return res; ++ ++ rtc_dev.compat = *(struct rtc_compat *)compat_data; ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result stm32_rtc_probe(const void *fdt, int node, ++ const void *compat_data) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ ++ res = parse_dt(fdt, node, compat_data); ++ if (res) { ++ memset(&rtc_dev, 0, sizeof(rtc_dev)); ++ return res; ++ } ++ ++ /* Unbalanced clock enable to ensure RTC core clock is always on */ ++ res = clk_enable(rtc_dev.rtc_ck); ++ if (res) ++ panic("Couldn't enable RTC clock"); ++ ++ if (clk_get_rate(rtc_dev.pclk) < (clk_get_rate(rtc_dev.rtc_ck) * 7)) ++ rtc_dev.flags |= RTC_FLAGS_READ_TWICE; ++ ++ return res; ++} ++ ++static struct rtc_compat mp15_compat = { ++ .has_seccfgr = false, ++}; ++ ++static struct rtc_compat mp13_compat = { ++ .has_seccfgr = true, ++}; ++ ++static const struct dt_device_match stm32_rtc_match_table[] = { ++ { ++ .compatible = "st,stm32mp1-rtc", ++ .compat_data = &mp15_compat, ++ }, ++ { ++ .compatible = "st,stm32mp13-rtc", ++ .compat_data = &mp13_compat, ++ }, ++ { } ++}; ++ ++DEFINE_DT_DRIVER(stm32_rtc_dt_driver) = { ++ .name = "stm32-rtc", ++ .match_table = stm32_rtc_match_table, ++ .probe = stm32_rtc_probe, ++}; +diff --git a/core/drivers/stm32_tamp.c b/core/drivers/stm32_tamp.c +new file mode 100644 +index 000000000..5a2174e18 +--- /dev/null ++++ b/core/drivers/stm32_tamp.c +@@ -0,0 +1,1441 @@ ++// SPDX-License-Identifier: BSD-3-Clause ++/* ++ * Copyright (c) 2021, STMicroelectronics ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++ ++/* STM32 Registers */ ++#define _TAMP_CR1 0x00U ++#define _TAMP_CR2 0x04U ++#define _TAMP_CR3 0x08U ++#define _TAMP_FLTCR 0x0CU ++#define _TAMP_ATCR1 0x10U ++#define _TAMP_ATSEEDR 0x14U ++#define _TAMP_ATOR 0x18U ++#define _TAMP_ATCR2 0x1CU ++#define _TAMP_SECCFGR 0x20U ++#define _TAMP_SMCR 0x20U ++#define _TAMP_PRIVCFGR 0x24U ++#define _TAMP_IER 0x2CU ++#define _TAMP_SR 0x30U ++#define _TAMP_MISR 0x34U ++#define _TAMP_SMISR 0x38U ++#define _TAMP_SCR 0x3CU ++#define _TAMP_COUNTR 0x40U ++#define _TAMP_COUNT2R 0x44U ++#define _TAMP_OR 0x50U ++#define _TAMP_ERCFGR 0X54U ++#define _TAMP_HWCFGR2 0x3ECU ++#define _TAMP_HWCFGR1 0x3F0U ++#define _TAMP_VERR 0x3F4U ++#define _TAMP_IPIDR 0x3F8U ++#define _TAMP_SIDR 0x3FCU ++ ++/* _TAMP_CR1 bit fields */ ++#define _TAMP_CR1_ITAMP(id) BIT((id) - INT_TAMP1 + 16U) ++#define _TAMP_CR1_ETAMP(id) BIT((id) - EXT_TAMP1) ++ ++/* _TAMP_CR2 bit fields */ ++#define _TAMP_CR2_ETAMPTRG(id) BIT((id) - EXT_TAMP1 + 24U) ++#define _TAMP_CR2_BKERASE BIT(23) ++#define _TAMP_CR2_BKBLOCK BIT(22) ++#define _TAMP_CR2_ETAMPMSK_MAX_ID 3U ++#define _TAMP_CR2_ETAMPMSK(id) BIT((id) - EXT_TAMP1 + 16U) ++#define _TAMP_CR2_ETAMPNOER(id) BIT((id) - EXT_TAMP1) ++ ++/* _TAMP_CR3 bit fields */ ++#define _TAMP_CR3_ITAMPNOER_ALL GENMASK_32(12, 0) ++#define _TAMP_CR3_ITAMPNOER(id) BIT((id) - INT_TAMP1) ++ ++/* _TAMP_FLTCR bit fields */ ++#define _TAMP_FLTCR_TAMPFREQ_MASK GENMASK_32(2, 0) ++#define _TAMP_FLTCR_TAMPFREQ_SHIFT 0U ++#define _TAMP_FLTCR_TAMPFLT_MASK GENMASK_32(4, 3) ++#define _TAMP_FLTCR_TAMPFLT_SHIFT 3U ++#define _TAMP_FLTCR_TAMPPRCH_MASK GENMASK_32(6, 5) ++#define _TAMP_FLTCR_TAMPPRCH_SHIFT 5U ++#define _TAMP_FLTCR_TAMPPUDIS BIT(7) ++ ++/* _TAMP_ATCR bit fields */ ++#define _TAMP_ATCR1_ATCKSEL_MASK GENMASK_32(19, 16) ++#define _TAMP_ATCR1_ATCKSEL_SHIFT 16U ++#define _TAMP_ATCR1_ATPER_MASK GENMASK_32(26, 24) ++#define _TAMP_ATCR1_ATPER_SHIFT 24U ++#define _TAMP_ATCR1_ATOSHARE BIT(30) ++#define _TAMP_ATCR1_FLTEN BIT(31) ++#define _TAMP_ATCR1_COMMON_MASK GENMASK_32(31, 16) ++#define _TAMP_ATCR1_ETAMPAM(id) BIT((id) - EXT_TAMP1) ++#define _TAMP_ATCR1_ATOSEL_MASK(id) GENMASK_32(((id) - EXT_TAMP1 + 1) * \ ++ 2U + 7U, \ ++ ((id) - EXT_TAMP1) * 2U + 8U) ++#define _TAMP_ATCR1_ATOSEL(id, od) SHIFT_U32(((od) - OUT_TAMP1), \ ++ (((id) - EXT_TAMP1) * 2U + 8U)) ++ ++/* _TAMP_ATOR bit fields */ ++#define _TAMP_PRNG GENMASK_32(7, 0) ++#define _TAMP_SEEDF BIT(14) ++#define _TAMP_INITS BIT(15) ++ ++/* _TAMP_IER bit fields */ ++#define _TAMP_IER_ITAMP(id) BIT((id) - INT_TAMP1 + 16U) ++#define _TAMP_IER_ETAMP(id) BIT((id) - EXT_TAMP1) ++ ++/* _TAMP_SR bit fields */ ++#define _TAMP_SR_ETAMPXF_MASK GENMASK_32(7, 0) ++#define _TAMP_SR_ITAMPXF_MASK GENMASK_32(31, 16) ++#define _TAMP_SR_ITAMP(id) BIT((id) - INT_TAMP1 + 16U) ++#define _TAMP_SR_ETAMP(id) BIT((id) - EXT_TAMP1) ++ ++/* _TAMP_SCR bit fields */ ++#define _TAMP_SCR_ITAMP(id) BIT((id) - INT_TAMP1 + 16U) ++#define _TAMP_SCR_ETAMP(id) BIT((id) - EXT_TAMP1) ++ ++/* _TAMP_SECCFGR bit fields */ ++#define _TAMP_SECCFGR_BKPRWSEC_MASK GENMASK_32(7, 0) ++#define _TAMP_SECCFGR_BKPRWSEC_SHIFT 0U ++#define _TAMP_SECCFGR_CNT2SEC BIT(14) ++#define _TAMP_SECCFGR_CNT1SEC BIT(15) ++#define _TAMP_SECCFGR_BKPWSEC_MASK GENMASK_32(23, 16) ++#define _TAMP_SECCFGR_BKPWSEC_SHIFT 16U ++#define _TAMP_SECCFGR_BHKLOCK BIT(30) ++#define _TAMP_SECCFGR_TAMPSEC BIT(31) ++#define _TAMP_SECCFGR_BUT_BKP_MASK (GENMASK_32(31, 30) | \ ++ GENMASK_32(15, 14)) ++ ++/* _TAMP_SMCR bit fields */ ++#define _TAMP_SMCR_BKPRWDPROT_MASK GENMASK_32(7, 0) ++#define _TAMP_SMCR_BKPRWDPROT_SHIFT 0U ++#define _TAMP_SMCR_BKPWDPROT_MASK GENMASK_32(23, 16) ++#define _TAMP_SMCR_BKPWDPROT_SHIFT 16U ++#define _TAMP_SMCR_DPROT BIT(31) ++ ++/* ++ * _TAMP_PRIVCFGR bit fields ++ */ ++#define _TAMP_PRIVCFG_CNT2PRIV BIT(14) ++#define _TAMP_PRIVCFG_CNT1PRIV BIT(15) ++#define _TAMP_PRIVCFG_BKPRWPRIV BIT(29) ++#define _TAMP_PRIVCFG_BKPWPRIV BIT(30) ++#define _TAMP_PRIVCFG_TAMPPRIV BIT(31) ++#define _TAMP_PRIVCFGR_MASK (GENMASK_32(31, 29) | \ ++ GENMASK_32(15, 14)) ++ ++/* _TAMP_ATCR2 bit fields */ ++#define _TAMP_ATCR2_ATOSEL_MASK(id) GENMASK_32(((id) - EXT_TAMP1 + 1) * \ ++ 3U + 7U, \ ++ ((id) - EXT_TAMP1) * 3U + 8U) ++#define _TAMP_ATCR2_ATOSEL(id, od) SHIFT_U32(((od) - OUT_TAMP1), \ ++ (((id) - EXT_TAMP1) * 3U + 8U)) ++ ++/* _TAMP_OR bit fields */ ++#define _TAMP_OR_IN1RMP_PF10 0U ++#define _TAMP_OR_IN1RMP_PC13 BIT(0) ++#define _TAMP_OR_IN2RMP_PA6 0U ++#define _TAMP_OR_IN2RMP_PI1 BIT(1) ++#define _TAMP_OR_IN3RMP_PC0 0U ++#define _TAMP_OR_IN3RMP_PI2 BIT(2) ++#define _TAMP_OR_IN4RMP_PG8 0U ++#define _TAMP_OR_IN4RMP_PI3 BIT(3) ++ ++#define _TAMP_OR_OUT3RMP_PI8 0U ++#define _TAMP_OR_OUT3RMP_PC13 BIT(0) ++ ++/* _TAMP_ERCFGR bit fields */ ++#define _TAMP_ERCFGR_ERCFG0 BIT(0) ++#define _TAMP_ERCFGR_ERCFG_MASK BIT(0) ++ ++/* _TAMP_HWCFGR2 bit fields */ ++#define _TAMP_HWCFGR2_TZ GENMASK_32(11, 8) ++#define _TAMP_HWCFGR2_OR GENMASK_32(7, 0) ++ ++/* _TAMP_HWCFGR1 bit fields */ ++#define _TAMP_HWCFGR1_BKPREG GENMASK_32(7, 0) ++#define _TAMP_HWCFGR1_TAMPER GENMASK_32(11, 8) ++#define _TAMP_HWCFGR1_ACTIVE GENMASK_32(15, 12) ++#define _TAMP_HWCFGR1_INTERN GENMASK_32(31, 16) ++#define _TAMP_HWCFGR1_ITAMP_MAX_ID 16U ++#define _TAMP_HWCFGR1_ITAMP(id) BIT((id) - INT_TAMP1 + 16U) ++ ++/* _TAMP_VERR bit fields */ ++#define _TAMP_VERR_MINREV GENMASK_32(3, 0) ++#define _TAMP_VERR_MAJREV GENMASK_32(7, 4) ++ ++#define SEED_TIMEOUT_US 1000U ++ ++/* Define TAMPER modes from DT */ ++#define TAMP_TRIG_ON BIT(16) ++#define TAMP_ACTIVE BIT(17) ++#define TAMP_IN_DT BIT(18) ++ ++#define TAMP_EXTI_WKUP 18 ++ ++enum stm32_tamp_out_id { ++ OUT_TAMP1 = LAST_TAMP, ++ OUT_TAMP2, ++ OUT_TAMP3, ++ OUT_TAMP4, ++ OUT_TAMP5, ++ OUT_TAMP6, ++ OUT_TAMP7, ++ OUT_TAMP8, ++ INVALID_OUT_TAMP = INVALID_TAMP ++}; ++ ++struct stm32_tamp_conf { ++ const uint32_t id; ++ uint32_t out_id; ++ uint32_t mode; ++ uint32_t (*func)(int id); ++}; ++ ++struct stm32_tamp_pin_map { ++ uint32_t id; ++ uint8_t bank; ++ uint8_t pin; ++ bool out; ++ uint32_t conf; ++}; ++ ++struct stm32_tamp_instance { ++ struct stm32_tamp_platdata pdata; ++ struct itr_handler *itr; ++ uint32_t hwconf1; ++ uint32_t hwconf2; ++}; ++ ++static struct stm32_tamp_conf int_tamp_mp13[] = { ++ { .id = INT_TAMP1 }, { .id = INT_TAMP2 }, { .id = INT_TAMP3 }, ++ { .id = INT_TAMP4 }, { .id = INT_TAMP5 }, { .id = INT_TAMP6 }, ++ { .id = INT_TAMP7 }, { .id = INT_TAMP8 }, { .id = INT_TAMP9 }, ++ { .id = INT_TAMP10 }, { .id = INT_TAMP11 }, ++ { .id = INT_TAMP12 }, { .id = INT_TAMP13 }, ++}; ++ ++static struct stm32_tamp_conf int_tamp_mp15[] = { ++ { .id = INT_TAMP1 }, { .id = INT_TAMP2 }, { .id = INT_TAMP3 }, ++ { .id = INT_TAMP4 }, { .id = INT_TAMP5 }, { .id = INT_TAMP8 }, ++}; ++ ++static struct stm32_tamp_conf ext_tamp_mp13[] = { ++ { .id = EXT_TAMP1 }, { .id = EXT_TAMP2 }, { .id = EXT_TAMP3 }, ++ { .id = EXT_TAMP4 }, { .id = EXT_TAMP5 }, { .id = EXT_TAMP6 }, ++ { .id = EXT_TAMP7 }, { .id = EXT_TAMP8 }, ++}; ++ ++static struct stm32_tamp_conf ext_tamp_mp15[] = { ++ { .id = EXT_TAMP1 }, { .id = EXT_TAMP2 }, { .id = EXT_TAMP3 }, ++}; ++ ++#define GPIO_BANK(port) ((port) - 'A') ++ ++static const struct stm32_tamp_pin_map pin_map_mp13[] = { ++ { ++ .id = EXT_TAMP1, .bank = GPIO_BANK('C'), .pin = 13, ++ .out = false, .conf = _TAMP_OR_IN1RMP_PC13 ++ }, ++ { ++ .id = EXT_TAMP2, .bank = GPIO_BANK('I'), .pin = 1, ++ .out = false, .conf = _TAMP_OR_IN2RMP_PI1 ++ }, ++ { ++ .id = EXT_TAMP3, .bank = GPIO_BANK('I'), .pin = 2, ++ .out = false, .conf = _TAMP_OR_IN3RMP_PI2 ++ }, ++ { ++ .id = EXT_TAMP4, .bank = GPIO_BANK('I'), .pin = 3, ++ .out = false, .conf = _TAMP_OR_IN4RMP_PI3 ++ }, ++}; ++ ++static const struct stm32_tamp_pin_map pin_map_mp15[] = { ++ { ++ .id = OUT_TAMP3, .bank = GPIO_BANK('C'), .pin = 13, ++ .out = true, .conf = _TAMP_OR_OUT3RMP_PC13 ++ }, ++}; ++ ++/* ++ * Only 1 instance of TAMP is expected per platform ++ */ ++static struct stm32_tamp_instance stm32_tamp; ++ ++static enum itr_return stm32_tamp_it_handler(struct itr_handler *h); ++ ++static void stm32_tamp_set_secret_list(struct stm32_tamp_instance *tamp, ++ uint32_t secret_list_conf) ++{ ++ vaddr_t base = io_pa_or_va(&tamp->pdata.base, 1); ++ ++ if (tamp->pdata.compat && ++ (tamp->pdata.compat->tags & TAMP_HAS_REGISTER_ERCFGR)) { ++ ++ io_clrsetbits32(base + _TAMP_ERCFGR, ++ _TAMP_ERCFGR_ERCFG_MASK, ++ secret_list_conf & _TAMP_ERCFGR_ERCFG_MASK); ++ } ++} ++ ++static void stm32_tamp_set_secure(struct stm32_tamp_instance *tamp, ++ uint32_t mode) ++{ ++ vaddr_t base = io_pa_or_va(&tamp->pdata.base, 1); ++ ++ if (tamp->pdata.compat && ++ (tamp->pdata.compat->tags & TAMP_HAS_REGISTER_SECCFG)) { ++ io_clrsetbits32(base + _TAMP_SECCFGR, ++ _TAMP_SECCFGR_BUT_BKP_MASK, ++ mode & _TAMP_SECCFGR_BUT_BKP_MASK); ++ } else { ++ /* Note: MP15 doesn't use SECCFG register ++ * and inverts the secure bit ++ */ ++ if (mode & _TAMP_SECCFGR_TAMPSEC) ++ io_clrbits32(base + _TAMP_SMCR, _TAMP_SMCR_DPROT); ++ else ++ io_setbits32(base + _TAMP_SMCR, _TAMP_SMCR_DPROT); ++ } ++} ++ ++static void stm32_tamp_set_privilege(struct stm32_tamp_instance *tamp, ++ uint32_t mode) ++{ ++ vaddr_t base = io_pa_or_va(&tamp->pdata.base, 1); ++ ++ if (tamp->pdata.compat && ++ (tamp->pdata.compat->tags & TAMP_HAS_REGISTER_PRIVCFGR)) ++ io_clrsetbits32(base + _TAMP_PRIVCFGR, _TAMP_PRIVCFGR_MASK, ++ mode & _TAMP_PRIVCFGR_MASK); ++} ++ ++static void stm32_tamp_set_pins(vaddr_t base, uint32_t mode) ++{ ++ io_setbits32(base + _TAMP_OR, mode); ++} ++ ++static TEE_Result stm32_tamp_set_seed(vaddr_t base) ++{ ++ /* Need RNG access. */ ++ uint64_t timeout_ref = timeout_init_us(SEED_TIMEOUT_US); ++ int idx = 0; ++ ++ for (idx = 0; idx < 4; idx++) { ++ uint32_t rnd = 0; ++ ++ if (crypto_rng_read((uint8_t *)&rnd, sizeof(uint32_t))) ++ return TEE_ERROR_BAD_STATE; ++ ++ io_write32(base + _TAMP_ATSEEDR, rnd); ++ } ++ ++ while (io_read32(base + _TAMP_ATOR) & _TAMP_SEEDF) ++ if (timeout_elapsed(timeout_ref)) ++ break; ++ ++ if (io_read32(base + _TAMP_ATOR) & _TAMP_SEEDF) ++ return TEE_ERROR_BAD_STATE; ++ ++ return TEE_SUCCESS; ++} ++ ++static bool is_int_tamp_id_valid(enum stm32_tamp_id id) ++{ ++ return (id - INT_TAMP1 <= _TAMP_HWCFGR1_ITAMP_MAX_ID) && ++ (stm32_tamp.hwconf1 & _TAMP_HWCFGR1_ITAMP(id)); ++} ++ ++static bool is_ext_tamp_id_valid(enum stm32_tamp_id id) ++{ ++ return (stm32_tamp.pdata.compat) && ++ (id - EXT_TAMP1 <= stm32_tamp.pdata.compat->ext_tamp_size); ++} ++ ++static TEE_Result stm32_tamp_set_int_config(struct stm32_tamp_compat *tcompat, ++ uint32_t itamp_index, uint32_t *cr1, ++ uint32_t *cr3, uint32_t *ier) ++{ ++ enum stm32_tamp_id id = INVALID_TAMP; ++ struct stm32_tamp_conf *tamp_int = NULL; ++ ++ if (!tcompat) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ tamp_int = &tcompat->int_tamp[itamp_index]; ++ id = tamp_int->id; ++ ++ if (!is_int_tamp_id_valid(id)) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ /* ++ * If there is no callback ++ * this tamper is disabled, we reset its configuration. ++ */ ++ if (!tamp_int->func) { ++ *cr1 &= ~_TAMP_CR1_ITAMP(id); ++ *ier &= ~_TAMP_IER_ITAMP(id); ++ if (tcompat->tags & TAMP_HAS_REGISTER_CR3) ++ *cr3 &= ~_TAMP_CR3_ITAMPNOER(id); ++ ++ FMSG("INT_TAMP%d disabled", id - INT_TAMP1 + 1); ++ return TEE_SUCCESS; ++ } ++ ++ *cr1 |= _TAMP_CR1_ITAMP(id); ++ *ier |= _TAMP_IER_ITAMP(id); ++ ++ if (tcompat->tags & TAMP_HAS_REGISTER_CR3) { ++ if (tamp_int->mode & TAMP_NOERASE) ++ *cr3 |= _TAMP_CR3_ITAMPNOER(id); ++ else ++ *cr3 &= ~_TAMP_CR3_ITAMPNOER(id); ++ } ++ ++ DMSG("INT_TAMP%d enabled as a %s tamper", id - INT_TAMP1 + 1, ++ (tamp_int->mode & TAMP_NOERASE) ? "potential" : "full"); ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result stm32_tamp_set_ext_config(struct stm32_tamp_compat *tcompat, ++ uint32_t etamp_index, ++ uint32_t *cr1, uint32_t *cr2, ++ uint32_t *atcr1, uint32_t *atcr2, ++ uint32_t *ier) ++{ ++ enum stm32_tamp_id id = INVALID_TAMP; ++ struct stm32_tamp_conf *tamp_ext = NULL; ++ ++ if (!tcompat) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ tamp_ext = &tcompat->ext_tamp[etamp_index]; ++ id = tamp_ext->id; ++ ++ /* Exit if not a valid TAMP_ID */ ++ if (!is_ext_tamp_id_valid(id)) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ /* ++ * If there is no callback or this TAMPER wasn't defined in DT, ++ * this tamper is disabled, we reset its configuration. ++ */ ++ if (!tamp_ext->func || !(tamp_ext->mode & TAMP_IN_DT)) { ++ *cr1 &= ~_TAMP_CR1_ETAMP(id); ++ *cr2 &= ~_TAMP_CR2_ETAMPMSK(id); ++ *cr2 &= ~_TAMP_CR2_ETAMPTRG(id); ++ *cr2 &= ~_TAMP_CR2_ETAMPNOER(id); ++ *ier &= ~_TAMP_IER_ETAMP(id); ++ ++ FMSG("EXT_TAMP%d disabled", id - EXT_TAMP1 + 1); ++ return TEE_SUCCESS; ++ } ++ ++ *cr1 |= _TAMP_CR1_ETAMP(id); ++ ++ if (tamp_ext->mode & TAMP_TRIG_ON) ++ *cr2 |= _TAMP_CR2_ETAMPTRG(id); ++ else ++ *cr2 &= ~_TAMP_CR2_ETAMPTRG(id); ++ ++ if (tamp_ext->mode & TAMP_ACTIVE) { ++ *atcr1 |= _TAMP_ATCR1_ETAMPAM(id); ++ ++ /* Configure output pin if ATOSHARE is selected */ ++ if (*atcr1 & _TAMP_ATCR1_ATOSHARE) { ++ if (tcompat->tags & TAMP_HAS_REGISTER_ATCR2) ++ *atcr2 = (*atcr2 & ++ ~_TAMP_ATCR2_ATOSEL_MASK(id)) | ++ _TAMP_ATCR2_ATOSEL(id, ++ tamp_ext->out_id); ++ else ++ *atcr1 = (*atcr1 & ++ ~_TAMP_ATCR1_ATOSEL_MASK(id)) | ++ _TAMP_ATCR1_ATOSEL(id, ++ tamp_ext->out_id); ++ } ++ } else { ++ *atcr1 &= ~_TAMP_ATCR1_ETAMPAM(id); ++ } ++ ++ if (tamp_ext->mode & TAMP_NOERASE) ++ *cr2 |= _TAMP_CR2_ETAMPNOER(id); ++ else ++ *cr2 &= ~_TAMP_CR2_ETAMPNOER(id); ++ ++ if (id < _TAMP_CR2_ETAMPMSK_MAX_ID) { ++ /* ++ * Only external TAMP 1, 2 and 3 can be masked ++ * and we may want them masked at startup. ++ */ ++ if (tamp_ext->mode & TAMP_EVT_MASK) { ++ /* ++ * ETAMP(id) event generates a trigger event. This ++ * ETAMP(id) is masked and internally cleared by ++ * hardware. ++ * The secrets are not erased. ++ */ ++ *ier &= ~_TAMP_IER_ETAMP(id); ++ *cr2 |= _TAMP_CR2_ETAMPMSK(id); ++ } else { ++ /* ++ * normal ETAMP interrupt: ++ * ETAMP(id) event generates a trigger event and ++ * TAMP(id) must be cleared by software to allow ++ * next tamper event detection. ++ */ ++ *ier |= _TAMP_IER_ETAMP(id); ++ *cr2 &= ~_TAMP_CR2_ETAMPMSK(id); ++ } ++ } else { ++ /* Other than 1,2,3 external TAMP, we want its interrupt */ ++ *ier |= _TAMP_IER_ETAMP(id); ++ } ++ ++ DMSG("EXT_TAMP%d enabled as a %s %s tamper trig_%s%s", ++ id - EXT_TAMP1 + 1, ++ (tamp_ext->mode & TAMP_ACTIVE) ? "active" : "passive", ++ (tamp_ext->mode & TAMP_NOERASE) ? "potential" : "full", ++ (tamp_ext->mode & TAMP_TRIG_ON) ? "on" : "off", ++ (tamp_ext->mode & TAMP_EVT_MASK) ? " (masked)" : ""); ++ ++ if (tamp_ext->mode & TAMP_ACTIVE) ++ DMSG(" linked with OUT_TAMP%d", ++ tamp_ext->out_id - OUT_TAMP1 + 1); ++ ++ return TEE_SUCCESS; ++} ++ ++TEE_Result stm32_tamp_set_secure_bkpregs(struct stm32_bkpregs_conf *bkr_conf) ++{ ++ uint32_t first_z2 = 0; ++ uint32_t first_z3 = 0; ++ vaddr_t base = io_pa_or_va(&stm32_tamp.pdata.base, 1); ++ ++ if (!bkr_conf) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ first_z2 = bkr_conf->nb_zone1_regs; ++ first_z3 = bkr_conf->nb_zone1_regs + bkr_conf->nb_zone2_regs; ++ ++ if ((first_z2 > (stm32_tamp.hwconf1 & _TAMP_HWCFGR1_BKPREG)) || ++ (first_z3 > (stm32_tamp.hwconf1 & _TAMP_HWCFGR1_BKPREG))) ++ return TEE_ERROR_NOT_SUPPORTED; ++ ++ if (stm32_tamp.pdata.compat && ++ (stm32_tamp.pdata.compat->tags & TAMP_HAS_REGISTER_SECCFG)) { ++ io_clrsetbits32(base + _TAMP_SECCFGR, ++ _TAMP_SECCFGR_BKPRWSEC_MASK, ++ (first_z2 << _TAMP_SECCFGR_BKPRWSEC_SHIFT) & ++ _TAMP_SECCFGR_BKPRWSEC_MASK); ++ ++ io_clrsetbits32(base + _TAMP_SECCFGR, ++ _TAMP_SECCFGR_BKPWSEC_MASK, ++ (first_z3 << _TAMP_SECCFGR_BKPWSEC_SHIFT) & ++ _TAMP_SECCFGR_BKPWSEC_MASK); ++ } else { ++ io_clrsetbits32(base + _TAMP_SMCR, ++ _TAMP_SMCR_BKPRWDPROT_MASK, ++ (first_z2 << _TAMP_SMCR_BKPRWDPROT_SHIFT) & ++ _TAMP_SMCR_BKPRWDPROT_MASK); ++ ++ io_clrsetbits32(base + _TAMP_SMCR, ++ _TAMP_SMCR_BKPWDPROT_MASK, ++ (first_z3 << _TAMP_SMCR_BKPWDPROT_SHIFT) & ++ _TAMP_SMCR_BKPWDPROT_MASK); ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++/* ++ * Count number of 1 in bitmask ++ * Cannot use __builtin_popcount(): libgcc.a for ARMV7 use hardfloat ABI, ++ * but optee is compiled with softfloaf ABI. ++ */ ++static int popcount(uint32_t bitmask) ++{ ++ int nb = 0; ++ ++ while (bitmask) { ++ if (bitmask & 1) ++ nb++; ++ bitmask >>= 1; ++ } ++ ++ return nb; ++} ++ ++static void stm32_tamp_set_atper(uint32_t pins_out_bits, uint32_t *atcr1) ++{ ++ uint32_t conf = 0; ++ ++ switch (popcount(pins_out_bits)) { ++ case 0: ++ fallthrough; ++ case 1: ++ conf = 0; ++ break; ++ case 2: ++ conf = 1; ++ break; ++ case 3: ++ fallthrough; ++ case 4: ++ conf = 2; ++ break; ++ default: ++ conf = 3; ++ break; ++ } ++ ++ *atcr1 |= (conf << _TAMP_ATCR1_ATPER_SHIFT) & _TAMP_ATCR1_ATPER_MASK; ++} ++ ++TEE_Result stm32_tamp_set_config(void) ++{ ++ TEE_Result ret = TEE_SUCCESS; ++ size_t i = 0; ++ uint32_t cr1 = 0; ++ uint32_t cr2 = 0; ++ uint32_t cr3 = 0; ++ uint32_t atcr1 = 0; ++ uint32_t atcr2 = 0; ++ uint32_t fltcr = 0; ++ uint32_t ier = 0; ++ ++ vaddr_t base = io_pa_or_va(&stm32_tamp.pdata.base, 1); ++ ++ if (!stm32_tamp.pdata.compat || ++ !stm32_tamp.pdata.compat->int_tamp || ++ !stm32_tamp.pdata.compat->ext_tamp) ++ return TEE_ERROR_BAD_STATE; ++ ++ /* Set passive filter configuration */ ++ fltcr = stm32_tamp.pdata.passive_conf; ++ ++ /* Set active mode configuration */ ++ atcr1 = stm32_tamp.pdata.active_conf & _TAMP_ATCR1_COMMON_MASK; ++ stm32_tamp_set_atper(stm32_tamp.pdata.out_pins, &atcr1); ++ ++ for (i = 0U; i < stm32_tamp.pdata.compat->int_tamp_size; i++) { ++ ret = stm32_tamp_set_int_config(stm32_tamp.pdata.compat, i, ++ &cr1, &cr3, &ier); ++ if (ret) ++ return ret; ++ } ++ ++ for (i = 0U; i < stm32_tamp.pdata.compat->ext_tamp_size; i++) { ++ ret = stm32_tamp_set_ext_config(stm32_tamp.pdata.compat, i, ++ &cr1, &cr2, &atcr1, &atcr2, ++ &ier); ++ if (ret) ++ return ret; ++ } ++ ++ /* ++ * We apply configuration all in a row: ++ * As for active ext tamper "all the needed tampers must be enabled in ++ * the same write access". ++ */ ++ io_write32(base + _TAMP_FLTCR, fltcr); ++ FMSG("Set passive conf %08x", fltcr); ++ ++ /* Active configuration applied only if not already done. */ ++ if (((io_read32(base + _TAMP_ATOR) & _TAMP_INITS) != _TAMP_INITS)) { ++ io_write32(base + _TAMP_ATCR1, atcr1); ++ FMSG("Set active conf1 %08x", atcr1); ++ ++ if (stm32_tamp.pdata.compat->tags & TAMP_HAS_REGISTER_ATCR2) { ++ io_write32(base + _TAMP_ATCR2, atcr2); ++ FMSG("Set active conf2 %08x", atcr2); ++ } ++ } ++ ++ io_write32(base + _TAMP_CR1, cr1); ++ io_write32(base + _TAMP_CR2, cr2); ++ if (stm32_tamp.pdata.compat->tags & TAMP_HAS_REGISTER_CR3) ++ io_write32(base + _TAMP_CR3, cr3); ++ ++ /* If active tamper we reinit the seed. */ ++ if (stm32_tamp.pdata.active_conf) { ++ if (stm32_tamp_set_seed(base) != TEE_SUCCESS) { ++ EMSG("Active tamper: SEED not initialized"); ++ return TEE_ERROR_BAD_STATE; ++ } ++ } ++ ++ /* ++ * We installed callback, ++ * we force managing TAMPER events that could have occured while boot. ++ */ ++ stm32_tamp_it_handler(stm32_tamp.itr); ++ ++ /* Enable interrupts. */ ++ io_write32(base + _TAMP_IER, ier); ++ ++ return TEE_SUCCESS; ++} ++ ++/* ++ * If ETAMP(id) event generates a trigger event, this ++ * ETAMP(id) is masked and internally cleared by hardware. ++ * The secrets are not erased. ++ */ ++TEE_Result stm32_tamp_set_mask(enum stm32_tamp_id id) ++{ ++ vaddr_t base = io_pa_or_va(&stm32_tamp.pdata.base, 1); ++ ++ /* Only EXT_TAMP1, EXT_TAMP2, EXT_TAMP3 can be masked. */ ++ if (id < EXT_TAMP1 && id > (EXT_TAMP1 + _TAMP_CR2_ETAMPMSK_MAX_ID)) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ /* We cannot mask the event if pending. */ ++ if (io_read32(base + _TAMP_SR) & _TAMP_SR_ETAMP(id)) ++ return TEE_ERROR_BAD_STATE; ++ ++ /* We disable the IT */ ++ io_clrbits32(base + _TAMP_IER, _TAMP_IER_ETAMP(id)); ++ /* We mask the event */ ++ io_setbits32(base + _TAMP_CR2, _TAMP_CR2_ETAMPMSK(id)); ++ ++ return TEE_SUCCESS; ++} ++ ++/* ++ * Move to a normal ETAMP interrupt: ++ * ETAMP(id) event generates a trigger event and ++ * ETAMP(id) must be cleared by software to allow ++ * next tamper event detection. ++ */ ++TEE_Result stm32_tamp_unset_mask(enum stm32_tamp_id id) ++{ ++ vaddr_t base = io_pa_or_va(&stm32_tamp.pdata.base, 1); ++ ++ /* Only EXT_TAMP1, EXT_TAMP2, EXT_TAMP3 can be masked. */ ++ if (id < EXT_TAMP1 && id > (EXT_TAMP1 + _TAMP_CR2_ETAMPMSK_MAX_ID)) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ /* We unmask the event */ ++ io_clrbits32(base + _TAMP_CR2, _TAMP_CR2_ETAMPMSK(id)); ++ /* We enable the IT */ ++ io_setbits32(base + _TAMP_IER, _TAMP_IER_ETAMP(id)); ++ ++ return TEE_SUCCESS; ++} ++ ++TEE_Result stm32_tamp_write_mcounter(int cnt_idx) ++{ ++ vaddr_t base = io_pa_or_va(&stm32_tamp.pdata.base, 1); ++ ++ if (cnt_idx < 0 || !stm32_tamp.pdata.compat || ++ cnt_idx > stm32_tamp.pdata.compat->nb_monotonic_counter) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ io_write32(base + _TAMP_COUNTR + cnt_idx * sizeof(uint32_t), 1U); ++ ++ return TEE_SUCCESS; ++} ++ ++uint32_t stm32_tamp_read_mcounter(int cnt_idx) ++{ ++ vaddr_t base = io_pa_or_va(&stm32_tamp.pdata.base, 1); ++ ++ if (cnt_idx < 0 || !stm32_tamp.pdata.compat || ++ cnt_idx > stm32_tamp.pdata.compat->nb_monotonic_counter) ++ return 0U; ++ ++ return io_read32(base + _TAMP_COUNTR + cnt_idx * sizeof(uint32_t)); ++} ++ ++static TEE_Result stm32_tamp_configure_int(struct stm32_tamp_conf *tamp_int, ++ uint32_t mode, ++ uint32_t (*cb)(int id)) ++{ ++ if (mode & TAMP_EVT_MASK) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ tamp_int->mode |= (mode & TAMP_MODE_MASK); ++ tamp_int->func = cb; ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result stm32_tamp_configure_ext(struct stm32_tamp_conf *tamp_ext, ++ uint32_t mode, ++ uint32_t (*cb)(int id)) ++{ ++ enum stm32_tamp_id id = tamp_ext->id; ++ ++ if (mode & TAMP_EVT_MASK && ++ (id < EXT_TAMP1 && id > (EXT_TAMP1 + _TAMP_CR2_ETAMPMSK_MAX_ID))) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ if (!(tamp_ext->mode & TAMP_IN_DT)) ++ return TEE_ERROR_ITEM_NOT_FOUND; ++ ++ tamp_ext->mode |= (mode & TAMP_MODE_MASK); ++ tamp_ext->func = cb; ++ ++ return TEE_SUCCESS; ++} ++ ++TEE_Result stm32_tamp_activate_tamp(enum stm32_tamp_id id, uint32_t mode, ++ uint32_t (*cb)(int id)) ++{ ++ size_t i = 0; ++ struct stm32_tamp_conf *tamp_conf = NULL; ++ ++ if (!stm32_tamp.pdata.compat) ++ return TEE_ERROR_BAD_STATE; ++ ++ if (!cb) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ /* Find internal Tamp struct*/ ++ for (i = 0U; i < stm32_tamp.pdata.compat->int_tamp_size; i++) ++ if (stm32_tamp.pdata.compat->int_tamp[i].id == id) { ++ tamp_conf = &stm32_tamp.pdata.compat->int_tamp[i]; ++ return stm32_tamp_configure_int(tamp_conf, mode, cb); ++ } ++ ++ /* Find external Tamp struct */ ++ for (i = 0U; i < stm32_tamp.pdata.compat->ext_tamp_size; i++) ++ if (stm32_tamp.pdata.compat->ext_tamp[i].id == id) { ++ tamp_conf = &stm32_tamp.pdata.compat->ext_tamp[i]; ++ return stm32_tamp_configure_ext(tamp_conf, mode, cb); ++ } ++ ++ return TEE_ERROR_BAD_PARAMETERS; ++} ++ ++bool stm32_tamp_are_secrets_blocked(void) ++{ ++ if (stm32_tamp.pdata.compat && ++ (stm32_tamp.pdata.compat->tags & TAMP_HAS_CR2_SECRET_STATUS)) { ++ vaddr_t base = io_pa_or_va(&stm32_tamp.pdata.base, 1); ++ ++ return (((io_read32(base + _TAMP_CR2) & ++ _TAMP_CR2_BKBLOCK) == _TAMP_CR2_BKBLOCK) || ++ io_read32(base + _TAMP_SR)); ++ } else { ++ return false; ++ } ++} ++ ++void stm32_tamp_block_secrets(void) ++{ ++ vaddr_t base = io_pa_or_va(&stm32_tamp.pdata.base, 1); ++ ++ if (stm32_tamp.pdata.compat && ++ (stm32_tamp.pdata.compat->tags & TAMP_HAS_CR2_SECRET_STATUS)) ++ io_setbits32(base + _TAMP_CR2, _TAMP_CR2_BKBLOCK); ++} ++ ++void stm32_tamp_unblock_secrets(void) ++{ ++ vaddr_t base = io_pa_or_va(&stm32_tamp.pdata.base, 1); ++ ++ if (stm32_tamp.pdata.compat && ++ (stm32_tamp.pdata.compat->tags & TAMP_HAS_CR2_SECRET_STATUS)) ++ io_clrbits32(base + _TAMP_CR2, _TAMP_CR2_BKBLOCK); ++} ++ ++void stm32_tamp_erase_secrets(void) ++{ ++ vaddr_t base = io_pa_or_va(&stm32_tamp.pdata.base, 1); ++ ++ if (stm32_tamp.pdata.compat && ++ (stm32_tamp.pdata.compat->tags & TAMP_HAS_CR2_SECRET_STATUS)) ++ io_setbits32(base + _TAMP_CR2, _TAMP_CR2_BKERASE); ++} ++ ++void stm32_tamp_lock_boot_hardware_key(void) ++{ ++ vaddr_t base = io_pa_or_va(&stm32_tamp.pdata.base, 1); ++ ++ if (stm32_tamp.pdata.compat && ++ (stm32_tamp.pdata.compat->tags & TAMP_HAS_REGISTER_SECCFG)) ++ io_setbits32(base + _TAMP_SECCFGR, _TAMP_SECCFGR_BHKLOCK); ++} ++ ++static enum itr_return stm32_tamp_it_handler(struct itr_handler *h) ++{ ++ struct stm32_tamp_instance *tamp = h->data; ++ vaddr_t base = io_pa_or_va(&tamp->pdata.base, 1); ++ uint32_t it = io_read32(base + _TAMP_SR); ++ uint32_t int_it = it & _TAMP_SR_ITAMPXF_MASK; ++ uint32_t ext_it = it & _TAMP_SR_ETAMPXF_MASK; ++ size_t i = 0; ++ struct stm32_rtc_time tamp_ts = { }; ++ ++ if (stm32_rtc_is_timestamp_enable() && it) { ++ stm32_rtc_get_timestamp(&tamp_ts); ++ FMSG("Tamper Event Occurred"); ++ FMSG("Date: %u/%u\n \t Time: %u:%u:%u", ++ tamp_ts.day, tamp_ts.month, tamp_ts.hour, ++ tamp_ts.min, tamp_ts.sec); ++ } ++ ++ while (int_it && i < stm32_tamp.pdata.compat->int_tamp_size) { ++ uint32_t id = tamp->pdata.compat->int_tamp[i].id; ++ ++ if (int_it & _TAMP_SR_ITAMP(id)) { ++ uint32_t ret = 0; ++ ++ int_it &= ~_TAMP_SR_ITAMP(id); ++ ++ if (tamp->pdata.compat->int_tamp[i].func) ++ ret = tamp->pdata.compat->int_tamp[i].func(id); ++ ++ if (ret & TAMP_CB_ACK) ++ io_setbits32(base + _TAMP_SCR, ++ _TAMP_SCR_ITAMP(id)); ++ ++ if (ret & TAMP_CB_RESET) ++ psci_system_reset(); ++ } ++ i++; ++ } ++ ++ i = 0; ++ /* External tamper interrupt */ ++ while (ext_it && i < stm32_tamp.pdata.compat->ext_tamp_size) { ++ uint32_t id = tamp->pdata.compat->ext_tamp[i].id; ++ ++ if (ext_it & _TAMP_SR_ETAMP(id)) { ++ uint32_t ret = 0; ++ ++ ext_it &= ~_TAMP_SR_ETAMP(id); ++ ++ if (tamp->pdata.compat->ext_tamp[i].func) ++ ret = tamp->pdata.compat->ext_tamp[i].func(id); ++ ++ if (ret & TAMP_CB_ACK) ++ io_setbits32(base + _TAMP_SCR, ++ _TAMP_SCR_ETAMP(id)); ++ ++ if (ret & TAMP_CB_RESET) ++ psci_system_reset(); ++ } ++ i++; ++ } ++ ++ return ITRR_HANDLED; ++} ++ ++#ifdef CFG_EMBED_DTB ++static void stm32_tamp_configure_pin(uint32_t id, ++ struct stm32_pinctrl *pinctrl, bool out, ++ struct stm32_tamp_platdata *pdata) ++{ ++ size_t i = 0; ++ struct stm32_tamp_compat *compat = pdata->compat; ++ ++ if (!compat) ++ return; ++ ++ /* Configure option registers */ ++ for (i = 0U; i < compat->pin_map_size; i++) ++ if (id == compat->pin_map[i].id && ++ pinctrl->bank == compat->pin_map[i].bank && ++ pinctrl->pin == compat->pin_map[i].pin && ++ !out == !compat->pin_map[i].out) { ++ pdata->pins_conf |= compat->pin_map[i].conf; ++ break; ++ } ++ ++ /* TAMPER pins are always secure (without effect, but keep coherency) */ ++ if (stm32_gpio_set_secure_cfg(pinctrl->bank, pinctrl->pin, true)) ++ panic(); ++} ++ ++static ++TEE_Result stm32_tamp_configure_pin_from_dt(const void *fdt, int node, ++ struct stm32_tamp_platdata *pdata) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ struct stm32_pinctrl *pinctrl[2] = { }; ++ struct stm32_pinctrl_list *pinctrl_list = NULL; ++ size_t i = 0U; ++ enum stm32_tamp_id id = INVALID_TAMP; ++ enum stm32_tamp_out_id out_id = INVALID_OUT_TAMP; ++ struct stm32_tamp_conf *tamp_ext = NULL; ++ const fdt32_t *cuint = NULL; ++ const fdt32_t *tampid = NULL; ++ int lenp = 0; ++ int pinnode = -1; ++ bool active = false; ++ ++ if (_fdt_get_status(fdt, node) == DT_STATUS_DISABLED) ++ return 0; ++ ++ /* ++ * First pin in the "pinctrl-0" node is the EXT_TAMP. ++ * If two pins are defined, first is a pin for an active tamper and the ++ * second is the OUT pin linked with the first. ++ * If only one found, this is a pin for a passive tamper. ++ */ ++ res = stm32_pinctrl_dt_get_by_index(fdt, node, 0, &pinctrl_list); ++ if (res) ++ return res; ++ ++ pinctrl[0] = STAILQ_FIRST(pinctrl_list); ++ pinctrl[1] = STAILQ_NEXT(pinctrl[0], link); ++ ++ cuint = fdt_getprop(fdt, node, "pinctrl-0", &lenp); ++ if (!pinctrl[0] || !cuint || (pinctrl[1] && ++ (lenp / sizeof(*cuint)) < 2)) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ if (pinctrl[1]) { ++ pinnode = fdt_node_offset_by_phandle(fdt, ++ fdt32_to_cpu(cuint[1])); ++ tampid = fdt_getprop(fdt, fdt_first_subnode(fdt, pinnode), ++ "st,tamp_id", NULL); ++ if (!tampid) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ active = true; ++ out_id = OUT_TAMP1 + fdt32_to_cpu(*tampid) - 1; ++ if (out_id - OUT_TAMP1 > pdata->compat->ext_tamp_size) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ stm32_tamp_configure_pin(out_id, pinctrl[1], true, pdata); ++ } ++ ++ /* We now configure first pin */ ++ pinnode = fdt_node_offset_by_phandle(fdt, fdt32_to_cpu(cuint[0])); ++ tampid = fdt_getprop(fdt, fdt_first_subnode(fdt, pinnode), ++ "st,tamp_id", NULL); ++ if (!tampid) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ id = fdt32_to_cpu(*tampid) + EXT_TAMP1 - 1; ++ ++ /* Find external Tamp struct */ ++ for (i = 0U; i < pdata->compat->ext_tamp_size; i++) ++ if (pdata->compat->ext_tamp[i].id == id) { ++ tamp_ext = &pdata->compat->ext_tamp[i]; ++ break; ++ } ++ ++ if (!tamp_ext) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ if (active) { ++ tamp_ext->mode |= TAMP_ACTIVE; ++ tamp_ext->out_id = out_id; ++ pdata->out_pins |= BIT(tamp_ext->out_id - OUT_TAMP1); ++ ++ if (out_id - OUT_TAMP1 != id - EXT_TAMP1) ++ pdata->active_conf |= _TAMP_ATCR1_ATOSHARE; ++ } else { ++ if (fdt_getprop(fdt, node, "st,trig_on", NULL)) ++ tamp_ext->mode |= TAMP_TRIG_ON; ++ } ++ ++ tamp_ext->mode |= TAMP_IN_DT; ++ ++ stm32_tamp_configure_pin(id, pinctrl[0], false, pdata); ++ ++ return TEE_SUCCESS; ++} ++ ++static int stm32_tamp_parse_passive_conf(const void *fdt, int node, ++ struct stm32_tamp_platdata *pdata) ++{ ++ const fdt32_t *cuint = NULL; ++ uint32_t precharge = 0U; ++ uint32_t nb_sample = 0U; ++ uint32_t clk_div = 32768U; ++ uint32_t conf = 0U; ++ ++ cuint = fdt_getprop(fdt, node, "st,tamp_passive_precharge", NULL); ++ if (cuint) ++ precharge = fdt32_to_cpu(*cuint); ++ ++ cuint = fdt_getprop(fdt, node, "st,tamp_passive_nb_sample", NULL); ++ if (cuint) ++ nb_sample = fdt32_to_cpu(*cuint); ++ ++ cuint = fdt_getprop(fdt, node, "st,tamp_passive_sample_clk_div", NULL); ++ if (cuint) ++ clk_div = fdt32_to_cpu(*cuint); ++ ++ DMSG("Passive conf from dt: precharge=%"PRId32", nb_sample=%"PRId32 ++ ", clk_div=%"PRId32, precharge, nb_sample, clk_div); ++ ++ switch (precharge) { ++ case 0: ++ /* No precharge, => we disable the pull-up */ ++ conf |= _TAMP_FLTCR_TAMPPUDIS; ++ break; ++ case 1: ++ /* Precharge for one cycle value stay 0 */ ++ break; ++ case 2: ++ /* Precharge passive pin 2 cycles */ ++ conf |= 1 << _TAMP_FLTCR_TAMPPRCH_SHIFT; ++ break; ++ case 4: ++ /* Precharge passive pin 4 cycles */ ++ conf |= 2 << _TAMP_FLTCR_TAMPPRCH_SHIFT; ++ break; ++ case 8: ++ /* Precharge passive pin 8 cycles */ ++ conf |= 3 << _TAMP_FLTCR_TAMPPRCH_SHIFT; ++ break; ++ default: ++ return -1; ++ } ++ ++ switch (nb_sample) { ++ case 0: ++ /* Activation on edge, no pull-up: value stay 0 */ ++ break; ++ case 2: ++ /* ++ * Tamper event is activated after 2 consecutive samples at ++ * active level. ++ */ ++ conf |= 1 << _TAMP_FLTCR_TAMPFLT_SHIFT; ++ break; ++ case 4: ++ /* ++ * Tamper event is activated after 4 consecutive samples at ++ * active level. ++ */ ++ conf |= 2 << _TAMP_FLTCR_TAMPFLT_SHIFT; ++ break; ++ case 8: ++ /* ++ * Tamper event is activated after 8 consecutive samples at ++ * active level. ++ */ ++ conf |= 3 << _TAMP_FLTCR_TAMPFLT_SHIFT; ++ break; ++ default: ++ return -1; ++ } ++ ++ switch (clk_div) { ++ case 32768: ++ /* RTCCLK / 32768 (1 Hz when RTCCLK = 32768 Hz): stay 0 */ ++ break; ++ case 16384: ++ /* RTCCLK / 16384 (2 Hz when RTCCLK = 32768 Hz) */ ++ conf |= 1 << _TAMP_FLTCR_TAMPFREQ_SHIFT; ++ break; ++ case 8192: ++ /* RTCCLK / 8192 (4 Hz when RTCCLK = 32768 Hz) */ ++ conf |= 2 << _TAMP_FLTCR_TAMPFREQ_SHIFT; ++ break; ++ case 4096: ++ /* RTCCLK / 4096 (8 Hz when RTCCLK = 32768 Hz) */ ++ conf |= 3 << _TAMP_FLTCR_TAMPFREQ_SHIFT; ++ break; ++ case 2048: ++ /* RTCCLK / 2048 (16 Hz when RTCCLK = 32768 Hz) */ ++ conf |= 4 << _TAMP_FLTCR_TAMPFREQ_SHIFT; ++ break; ++ case 1024: ++ /* RTCCLK / 1024 (32 Hz when RTCCLK = 32768 Hz) */ ++ conf |= 5 << _TAMP_FLTCR_TAMPFREQ_SHIFT; ++ break; ++ case 512: ++ /* RTCCLK / 512 (64 Hz when RTCCLK = 32768 Hz) */ ++ conf |= 6 << _TAMP_FLTCR_TAMPFREQ_SHIFT; ++ break; ++ case 256: ++ /* RTCCLK / 256 (128 Hz when RTCCLK = 32768 Hz) */ ++ conf |= 7 << _TAMP_FLTCR_TAMPFREQ_SHIFT; ++ break; ++ default: ++ return -1; ++ } ++ ++ pdata->passive_conf = conf; ++ ++ return 0; ++} ++ ++static int stm32_tamp_parse_active_conf(const void *fdt, int node, ++ struct stm32_tamp_platdata *pdata) ++{ ++ const fdt32_t *cuint = NULL; ++ uint32_t clk_div = 1U; ++ uint32_t conf = 0U; ++ ++ cuint = fdt_getprop(fdt, node, "st,tamp_active_filter", NULL); ++ if (cuint) ++ conf |= _TAMP_ATCR1_FLTEN; ++ ++ /* ++ * Here we will select a diviser for the RTCCLK. ++ * Note that RTCCLK is also divided by (RTC_PRER_PREDIV_A - 1). ++ */ ++ cuint = fdt_getprop(fdt, node, "st,tamp_active_clk_div", NULL); ++ if (cuint) ++ clk_div = fdt32_to_cpu(*cuint); ++ ++ DMSG("Active conf from dt: %s clk_div=%"PRId32, ++ (conf & _TAMP_ATCR1_FLTEN) ? "filter" : "no filter", clk_div); ++ ++ switch (clk_div) { ++ case 1: ++ /* RTCCLK / 32768 (1 Hz when RTCCLK = 32768 Hz): stay 0 */ ++ break; ++ case 2: ++ /* RTCCLK / 16384 (2 Hz when RTCCLK = 32768 Hz) */ ++ conf |= 1 << _TAMP_ATCR1_ATCKSEL_SHIFT; ++ break; ++ case 4: ++ /* RTCCLK / 8192 (4 Hz when RTCCLK = 32768 Hz) */ ++ conf |= 2 << _TAMP_ATCR1_ATCKSEL_SHIFT; ++ break; ++ case 8: ++ /* RTCCLK / 4096 (8 Hz when RTCCLK = 32768 Hz) */ ++ conf |= 3 << _TAMP_ATCR1_ATCKSEL_SHIFT; ++ break; ++ case 16: ++ /* RTCCLK / 2048 (16 Hz when RTCCLK = 32768 Hz) */ ++ conf |= 4 << _TAMP_ATCR1_ATCKSEL_SHIFT; ++ break; ++ case 32: ++ /* RTCCLK / 1024 (32 Hz when RTCCLK = 32768 Hz) */ ++ conf |= 5 << _TAMP_ATCR1_ATCKSEL_SHIFT; ++ break; ++ case 64: ++ /* RTCCLK / 512 (64 Hz when RTCCLK = 32768 Hz) */ ++ conf |= 6 << _TAMP_ATCR1_ATCKSEL_SHIFT; ++ break; ++ case 128: ++ /* RTCCLK / 256 (128 Hz when RTCCLK = 32768 Hz) */ ++ conf |= 7 << _TAMP_ATCR1_ATCKSEL_SHIFT; ++ break; ++ case 2048: ++ if (pdata->compat && ++ (pdata->compat->tags & TAMP_SIZE_ATCR1_ATCKSEL_IS_4)){ ++ /* RTCCLK/2048 when (PREDIV_A+1) = 128 and (PREDIV_S+1) ++ * is a multiple of 16. */ ++ conf |= 11 << _TAMP_ATCR1_ATCKSEL_SHIFT; ++ break; ++ } ++ ++ return -1; ++ default: ++ return -1; ++ } ++ ++ pdata->active_conf = conf; ++ ++ return 0; ++} ++ ++static ++TEE_Result stm32_tamp_parse_fdt(struct stm32_tamp_platdata *pdata, ++ const void *fdt, int node, const void *compat) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ int pinnode = -1; ++ struct dt_node_info dt_tamp = { }; ++ ++ _fdt_fill_device_info(fdt, &dt_tamp, node); ++ ++ if (dt_tamp.reg == DT_INFO_INVALID_REG || ++ dt_tamp.reg_size == DT_INFO_INVALID_REG_SIZE || ++ dt_tamp.interrupt == DT_INFO_INVALID_INTERRUPT) { ++ return TEE_ERROR_BAD_PARAMETERS; ++ } ++ ++ pdata->compat = (struct stm32_tamp_compat *)compat; ++ pdata->it = dt_tamp.interrupt; ++ pdata->base.pa = dt_tamp.reg; ++ io_pa_or_va_secure(&pdata->base, dt_tamp.reg_size); ++ ++ res = clk_dt_get_by_index(fdt, node, 0, &pdata->clock); ++ if (res) ++ return res; ++ ++ pdata->pins_conf = 0; ++ ++ stm32_tamp_parse_passive_conf(fdt, node, pdata); ++ stm32_tamp_parse_active_conf(fdt, node, pdata); ++ ++ fdt_for_each_subnode(pinnode, fdt, node) { ++ res = stm32_tamp_configure_pin_from_dt(fdt, pinnode, pdata); ++ if (res) ++ return res; ++ } ++ ++ if (pinnode < 0 && pinnode != -FDT_ERR_NOTFOUND) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ if (fdt_getprop(fdt, node, "wakeup-source", NULL)) ++ pdata->is_wakeup_source = true; ++ ++ return TEE_SUCCESS; ++} ++ ++__weak ++TEE_Result stm32_tamp_get_platdata(struct stm32_tamp_platdata *pdata __unused) ++{ ++ /* In DT config, the platform data are filled by DT file */ ++ return TEE_SUCCESS; ++} ++#else /* CFG_EMBED_DTB */ ++static ++TEE_Result stm32_tamp_parse_fdt(struct stm32_tamp_platdata *pdata, ++ const void *fdt, int node, const void *compat) ++{ ++ /* Do nothing, there is no fdt to parse in this case */ ++ return TEE_SUCCESS; ++} ++ ++/* This function can be overridden by platform to define pdata of tamp driver */ ++__weak ++TEE_Result stm32_tamp_get_platdata(struct stm32_tamp_platdata *pdata __unused) ++{ ++ return TEE_ERROR_NOT_SUPPORTED; ++} ++#endif /* CFG_EMBED_DTB */ ++ ++static TEE_Result stm32_tamp_probe(const void *fdt, int node, ++ const void *compat_data) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ vaddr_t base = 0; ++ ++ res = stm32_tamp_get_platdata(&stm32_tamp.pdata); ++ if (res) ++ return res; ++ ++ res = stm32_tamp_parse_fdt(&stm32_tamp.pdata, fdt, node, compat_data); ++ if (res) ++ return res; ++ ++ /* Init Tamp clock */ ++ clk_enable(stm32_tamp.pdata.clock); ++ ++ base = io_pa_or_va(&stm32_tamp.pdata.base, 1); ++ ++ stm32_tamp.hwconf1 = io_read32(base + _TAMP_HWCFGR1); ++ stm32_tamp.hwconf2 = io_read32(base + _TAMP_HWCFGR2); ++ ++ if (TRACE_LEVEL >= TRACE_FLOW) { ++ uint32_t __maybe_unused rev = io_read32(base + _TAMP_VERR); ++ ++ FMSG("STM32 TAMPER V%u.%u", (rev & _TAMP_VERR_MAJREV) >> 4, ++ rev & _TAMP_VERR_MINREV); ++ } ++ ++ if (!(stm32_tamp.hwconf2 & _TAMP_HWCFGR2_TZ)) { ++ EMSG("Tamper IP doesn't support trustzone"); ++ return TEE_ERROR_NOT_SUPPORTED; ++ } ++ ++ stm32_tamp_set_pins(base, stm32_tamp.pdata.pins_conf); ++ ++ /* ++ * Select extra IP to add in the deleted/blocked IP in case of ++ * tamper event ++ * ++ * No IP added. ++ */ ++ stm32_tamp_set_secret_list(&stm32_tamp, 0); ++ ++ /* ++ * Select access in secure or unsecure ++ * ++ * IP is always SECURE, on mp13 both counter can be access from ++ * secure and unsecure world. ++ * */ ++ stm32_tamp_set_secure(&stm32_tamp, _TAMP_SECCFGR_TAMPSEC); ++ ++ /* ++ * Select access in privileged mode or unprivileged mode ++ * ++ * TAMP ip need secure access, ++ * backup register zone 1 can be read/writen only from secure ++ * backup register zone 2 can be writen only from secure. ++ * monotic counters can be read/writen from secure and unsecure. ++ */ ++ stm32_tamp_set_privilege(&stm32_tamp, _TAMP_PRIVCFG_TAMPPRIV | ++ _TAMP_PRIVCFG_BKPRWPRIV | ++ _TAMP_PRIVCFG_BKPWPRIV); ++ ++ stm32_tamp.itr = itr_alloc_add(stm32_tamp.pdata.it, ++ stm32_tamp_it_handler, ++ ITRF_TRIGGER_LEVEL, &stm32_tamp); ++ if (!stm32_tamp.itr) { ++ return TEE_ERROR_BAD_PARAMETERS; ++ } ++ if (stm32_tamp.pdata.is_wakeup_source) { ++ if (IS_ENABLED(CFG_STM32_EXTI)) ++ stm32_exti_enable_wake(TAMP_EXTI_WKUP); ++ else ++ IMSG("TAMP event are not configured as wakeup source"); ++ } ++ ++ itr_enable(stm32_tamp.itr->it); ++ ++ return TEE_SUCCESS; ++} ++ ++static const struct stm32_tamp_compat mp13_compat = { ++ .nb_monotonic_counter = 2, ++ .tags = TAMP_HAS_REGISTER_SECCFG | ++ TAMP_HAS_REGISTER_PRIVCFGR | ++ TAMP_HAS_REGISTER_ERCFGR | ++ TAMP_HAS_REGISTER_CR3 | ++ TAMP_HAS_REGISTER_ATCR2 | ++ TAMP_HAS_CR2_SECRET_STATUS | ++ TAMP_SIZE_ATCR1_ATCKSEL_IS_4, ++ .int_tamp = int_tamp_mp13, ++ .int_tamp_size = ARRAY_SIZE(int_tamp_mp13), ++ .ext_tamp = ext_tamp_mp13, ++ .ext_tamp_size = ARRAY_SIZE(ext_tamp_mp13), ++ .pin_map = pin_map_mp13, ++ .pin_map_size = ARRAY_SIZE(pin_map_mp13), ++}; ++ ++static const struct stm32_tamp_compat mp15_compat = { ++ .nb_monotonic_counter = 1, ++ .tags = 0, ++ .int_tamp = int_tamp_mp15, ++ .int_tamp_size = ARRAY_SIZE(int_tamp_mp15), ++ .ext_tamp = ext_tamp_mp15, ++ .ext_tamp_size = ARRAY_SIZE(ext_tamp_mp15), ++ .pin_map = pin_map_mp15, ++ .pin_map_size = ARRAY_SIZE(pin_map_mp15), ++}; ++ ++static const struct dt_device_match stm32_tamp_match_table[] = { ++ { .compatible = "st,stm32mp13-tamp", .compat_data = &mp13_compat }, ++ { .compatible = "st,stm32-tamp", .compat_data = &mp15_compat }, ++ { } ++}; ++ ++DEFINE_DT_DRIVER(stm32_tamp_dt_driver) = { ++ .name = "stm32-tamp", ++ .match_table = stm32_tamp_match_table, ++ .probe = stm32_tamp_probe, ++}; +diff --git a/core/drivers/stm32_uart.c b/core/drivers/stm32_uart.c +index 8c0ad779e..5105b14bc 100644 +--- a/core/drivers/stm32_uart.c ++++ b/core/drivers/stm32_uart.c +@@ -111,22 +111,16 @@ void stm32_uart_init(struct stm32_uart_pdata *pd, vaddr_t base) + #ifdef CFG_DT + static void register_secure_uart(struct stm32_uart_pdata *pd) + { +- size_t n = 0; +- + stm32mp_register_secure_periph_iomem(pd->base.pa); +- for (n = 0; n < pd->pinctrl_count; n++) +- stm32mp_register_secure_gpio(pd->pinctrl[n].bank, +- pd->pinctrl[n].pin); ++ if (stm32_pinctrl_set_secure_cfg(pd->pinctrl, true)) ++ panic(); + } + + static void register_non_secure_uart(struct stm32_uart_pdata *pd) + { +- size_t n = 0; +- + stm32mp_register_non_secure_periph_iomem(pd->base.pa); +- for (n = 0; n < pd->pinctrl_count; n++) +- stm32mp_register_non_secure_gpio(pd->pinctrl[n].bank, +- pd->pinctrl[n].pin); ++ if (stm32_pinctrl_set_secure_cfg(pd->pinctrl, false)) ++ panic(); + } + + struct stm32_uart_pdata *stm32_uart_init_from_dt_node(void *fdt, int node) +@@ -134,8 +128,6 @@ struct stm32_uart_pdata *stm32_uart_init_from_dt_node(void *fdt, int node) + TEE_Result res = TEE_ERROR_GENERIC; + struct stm32_uart_pdata *pd = NULL; + struct dt_node_info info = { }; +- struct stm32_pinctrl *pinctrl_cfg = NULL; +- int count = 0; + + _fdt_fill_device_info(fdt, &info, node); + +@@ -168,21 +160,10 @@ struct stm32_uart_pdata *stm32_uart_init_from_dt_node(void *fdt, int node) + pd->secure ? MEM_AREA_IO_SEC : + MEM_AREA_IO_NSEC, info.reg_size); + +- count = stm32_pinctrl_fdt_get_pinctrl(fdt, node, NULL, 0); +- if (count < 0) ++ res = stm32_pinctrl_dt_get_by_index(fdt, node, 0, &pd->pinctrl); ++ if (res) + panic(); + +- if (count) { +- pinctrl_cfg = calloc(count, sizeof(*pinctrl_cfg)); +- if (!pinctrl_cfg) +- panic(); +- +- stm32_pinctrl_fdt_get_pinctrl(fdt, node, pinctrl_cfg, count); +- stm32_pinctrl_load_active_cfg(pinctrl_cfg, count); +- } +- pd->pinctrl = pinctrl_cfg; +- pd->pinctrl_count = count; +- + if (pd->secure) + register_secure_uart(pd); + else +diff --git a/core/drivers/stpmic1.c b/core/drivers/stpmic1.c +index d3d9cbdd6..b558d6bd0 100644 +--- a/core/drivers/stpmic1.c ++++ b/core/drivers/stpmic1.c +@@ -1,6 +1,6 @@ + // SPDX-License-Identifier: BSD-3-Clause + /* +- * Copyright (c) 2016-2020, STMicroelectronics - All Rights Reserved ++ * Copyright (c) 2016-2021, STMicroelectronics - All Rights Reserved + */ + + #include +@@ -25,10 +25,17 @@ struct regul_struct { + uint8_t pull_down_pos; + uint8_t mask_reset_reg; + uint8_t mask_reset_pos; ++ uint8_t icc_reg; ++ uint8_t icc_mask; + }; + + static struct i2c_handle_s *pmic_i2c_handle; + static uint16_t pmic_i2c_addr; ++/* ++ * Special mode corresponds to LDO3 in sink source mode or in bypass mode. ++ * LDO3 doesn't switch back from special to normal mode. ++ */ ++static bool ldo3_special_mode; + + /* Voltage tables in mV */ + static const uint16_t buck1_voltage_table[] = { +@@ -349,8 +356,16 @@ static const uint16_t ldo3_voltage_table[] = { + 3300, + 3300, + 3300, ++#ifndef CFG_REGULATOR_DRIVERS ++ /* Old specific config to be replaced with ldo3_special_mode support */ + 500, /* VOUT2/2 (Sink/source mode) */ + 0xFFFF, /* VREFDDR */ ++#endif ++}; ++ ++/* Special mode table is used for sink source OR bypass mode */ ++static const uint16_t ldo3_special_mode_table[] = { ++ 0, + }; + + static const uint16_t ldo5_voltage_table[] = { +@@ -440,6 +455,8 @@ static const struct regul_struct regulators_table[] = { + .pull_down_pos = BUCK1_PULL_DOWN_SHIFT, + .mask_reset_reg = MASK_RESET_BUCK_REG, + .mask_reset_pos = BUCK1_MASK_RESET_SHIFT, ++ .icc_reg = BUCK_ICC_TURNOFF_REG, ++ .icc_mask = BUCK1_ICC_SHIFT, + }, + { + .dt_node_name = "buck2", +@@ -452,6 +469,8 @@ static const struct regul_struct regulators_table[] = { + .pull_down_pos = BUCK2_PULL_DOWN_SHIFT, + .mask_reset_reg = MASK_RESET_BUCK_REG, + .mask_reset_pos = BUCK2_MASK_RESET_SHIFT, ++ .icc_reg = BUCK_ICC_TURNOFF_REG, ++ .icc_mask = BUCK2_ICC_SHIFT, + }, + { + .dt_node_name = "buck3", +@@ -464,6 +483,8 @@ static const struct regul_struct regulators_table[] = { + .pull_down_pos = BUCK3_PULL_DOWN_SHIFT, + .mask_reset_reg = MASK_RESET_BUCK_REG, + .mask_reset_pos = BUCK3_MASK_RESET_SHIFT, ++ .icc_reg = BUCK_ICC_TURNOFF_REG, ++ .icc_mask = BUCK3_ICC_SHIFT, + }, + { + .dt_node_name = "buck4", +@@ -476,6 +497,8 @@ static const struct regul_struct regulators_table[] = { + .pull_down_pos = BUCK4_PULL_DOWN_SHIFT, + .mask_reset_reg = MASK_RESET_BUCK_REG, + .mask_reset_pos = BUCK4_MASK_RESET_SHIFT, ++ .icc_reg = BUCK_ICC_TURNOFF_REG, ++ .icc_mask = BUCK4_ICC_SHIFT, + }, + { + .dt_node_name = "ldo1", +@@ -486,6 +509,8 @@ static const struct regul_struct regulators_table[] = { + .enable_pos = LDO_BUCK_ENABLE_POS, + .mask_reset_reg = MASK_RESET_LDO_REG, + .mask_reset_pos = LDO1_MASK_RESET_SHIFT, ++ .icc_reg = LDO_ICC_TURNOFF_REG, ++ .icc_mask = LDO1_ICC_SHIFT, + }, + { + .dt_node_name = "ldo2", +@@ -496,6 +521,8 @@ static const struct regul_struct regulators_table[] = { + .enable_pos = LDO_BUCK_ENABLE_POS, + .mask_reset_reg = MASK_RESET_LDO_REG, + .mask_reset_pos = LDO2_MASK_RESET_SHIFT, ++ .icc_reg = LDO_ICC_TURNOFF_REG, ++ .icc_mask = LDO2_ICC_SHIFT, + }, + { + .dt_node_name = "ldo3", +@@ -506,6 +533,8 @@ static const struct regul_struct regulators_table[] = { + .enable_pos = LDO_BUCK_ENABLE_POS, + .mask_reset_reg = MASK_RESET_LDO_REG, + .mask_reset_pos = LDO3_MASK_RESET_SHIFT, ++ .icc_reg = LDO_ICC_TURNOFF_REG, ++ .icc_mask = LDO3_ICC_SHIFT, + }, + { + .dt_node_name = "ldo4", +@@ -516,6 +545,8 @@ static const struct regul_struct regulators_table[] = { + .enable_pos = LDO_BUCK_ENABLE_POS, + .mask_reset_reg = MASK_RESET_LDO_REG, + .mask_reset_pos = LDO4_MASK_RESET_SHIFT, ++ .icc_reg = LDO_ICC_TURNOFF_REG, ++ .icc_mask = LDO4_ICC_SHIFT, + }, + { + .dt_node_name = "ldo5", +@@ -526,6 +557,8 @@ static const struct regul_struct regulators_table[] = { + .enable_pos = LDO_BUCK_ENABLE_POS, + .mask_reset_reg = MASK_RESET_LDO_REG, + .mask_reset_pos = LDO5_MASK_RESET_SHIFT, ++ .icc_reg = LDO_ICC_TURNOFF_REG, ++ .icc_mask = LDO5_ICC_SHIFT, + }, + { + .dt_node_name = "ldo6", +@@ -536,6 +569,8 @@ static const struct regul_struct regulators_table[] = { + .enable_pos = LDO_BUCK_ENABLE_POS, + .mask_reset_reg = MASK_RESET_LDO_REG, + .mask_reset_pos = LDO6_MASK_RESET_SHIFT, ++ .icc_reg = LDO_ICC_TURNOFF_REG, ++ .icc_mask = LDO6_ICC_SHIFT, + }, + { + .dt_node_name = "vref_ddr", +@@ -553,6 +588,8 @@ static const struct regul_struct regulators_table[] = { + .voltage_table_size = ARRAY_SIZE(fixed_5v_voltage_table), + .control_reg = USB_CONTROL_REG, + .enable_pos = BOOST_ENABLED_POS, ++ .icc_reg = BUCK_ICC_TURNOFF_REG, ++ .icc_mask = BOOST_ICC_SHIFT, + }, + { + .dt_node_name = "pwr_sw1", +@@ -560,6 +597,8 @@ static const struct regul_struct regulators_table[] = { + .voltage_table_size = ARRAY_SIZE(fixed_5v_voltage_table), + .control_reg = USB_CONTROL_REG, + .enable_pos = USBSW_OTG_SWITCH_ENABLED_POS, ++ .icc_reg = BUCK_ICC_TURNOFF_REG, ++ .icc_mask = PWR_SW1_ICC_SHIFT, + }, + { + .dt_node_name = "pwr_sw2", +@@ -567,6 +606,8 @@ static const struct regul_struct regulators_table[] = { + .voltage_table_size = ARRAY_SIZE(fixed_5v_voltage_table), + .control_reg = USB_CONTROL_REG, + .enable_pos = SWIN_SWOUT_ENABLED_POS, ++ .icc_reg = BUCK_ICC_TURNOFF_REG, ++ .icc_mask = PWR_SW2_ICC_SHIFT, + }, + }; + +@@ -587,21 +628,6 @@ bool stpmic1_regulator_is_valid(const char *name) + return get_regulator_data(name); + } + +-void stpmic1_regulator_levels_mv(const char *name, +- const uint16_t **levels, +- size_t *levels_count) +-{ +- const struct regul_struct *regul = get_regulator_data(name); +- +- assert(regul); +- +- if (levels_count) +- *levels_count = regul->voltage_table_size; +- +- if (levels) +- *levels = regul->voltage_table; +-} +- + static size_t voltage_to_index(const char *name, uint16_t millivolts) + { + const struct regul_struct *regul = get_regulator_data(name); +@@ -676,6 +702,16 @@ int stpmic1_regulator_voltage_set(const char *name, uint16_t millivolts) + if (voltage_index == VOLTAGE_INDEX_INVALID) + return -1; + ++ if ((strncmp(name, "ldo3", 4) == 0) && ldo3_special_mode) { ++ /* ++ * when the LDO3 is in special mode, we do not change voltage, ++ * because by setting voltage, the LDO would leaves sink-source ++ * mode. There is obviously no reason to leave sink-source mode ++ * at runtime. ++ */ ++ return 0; ++ } ++ + mask = find_plat_mask(name); + if (!mask) + return 0; +@@ -685,6 +721,19 @@ int stpmic1_regulator_voltage_set(const char *name, uint16_t millivolts) + mask); + } + ++int stpmic1_regulator_pull_down_set(const char *name) ++{ ++ const struct regul_struct *regul = get_regulator_data(name); ++ ++ if (regul->pull_down_reg) ++ return stpmic1_register_update(regul->pull_down_reg, ++ BIT(regul->pull_down_pos), ++ LDO_BUCK_PULL_DOWN_MASK << ++ regul->pull_down_pos); ++ ++ return 0; ++} ++ + int stpmic1_regulator_mask_reset_set(const char *name) + { + const struct regul_struct *regul = get_regulator_data(name); +@@ -700,6 +749,108 @@ int stpmic1_regulator_mask_reset_set(const char *name) + regul->mask_reset_pos); + } + ++int stpmic1_regulator_icc_set(const char *name) ++{ ++ const struct regul_struct *regul = get_regulator_data(name); ++ ++ if (!regul->mask_reset_reg) ++ return -1; ++ ++ return stpmic1_register_update(regul->icc_reg, ++ BIT(regul->icc_mask), ++ BIT(regul->icc_mask)); ++} ++ ++int stpmic1_regulator_sink_mode_set(const char *name) ++{ ++ if (strncmp(name, "ldo3", 4)) ++ return -1; ++ ++ ldo3_special_mode = true; ++ ++ /* disable bypass mode, enable sink mode */ ++ return stpmic1_register_update(LDO3_CONTROL_REG, ++ LDO3_DDR_SEL << LDO_BUCK_VOLTAGE_SHIFT, ++ LDO3_BYPASS | LDO_VOLTAGE_MASK); ++} ++ ++int stpmic1_regulator_bypass_mode_set(const char *name) ++{ ++ if (strncmp(name, "ldo3", 4)) ++ return -1; ++ ++ ldo3_special_mode = true; ++ ++ /* enable bypass mode, disable sink mode */ ++ return stpmic1_register_update(LDO3_CONTROL_REG, ++ LDO3_BYPASS, ++ LDO3_BYPASS | LDO_VOLTAGE_MASK); ++} ++ ++int stpmic1_active_discharge_mode_set(const char *name) ++{ ++ if (!strncmp(name, "pwr_sw1", 7)) ++ return stpmic1_register_update(USB_CONTROL_REG, ++ BIT(VBUS_OTG_DISCHARGE_POS), ++ BIT(VBUS_OTG_DISCHARGE_POS)); ++ ++ if (!strncmp(name, "pwr_sw2", 7)) ++ return stpmic1_register_update(USB_CONTROL_REG, ++ BIT(SW_OUT_DISCHARGE_POS), ++ BIT(SW_OUT_DISCHARGE_POS)); ++ ++ return -1; ++} ++ ++bool stpmic1_regu_has_lp_cfg(const char *name) ++{ ++ return get_regulator_data(name)->low_power_reg; ++} ++ ++int stpmic1_lp_copy_reg(const char *name) ++{ ++ const struct regul_struct *regul = get_regulator_data(name); ++ uint8_t val = 0; ++ int status = 0; ++ ++ if (!regul->low_power_reg) ++ return -1; ++ ++ status = stpmic1_register_read(regul->control_reg, &val); ++ if (status) ++ return status; ++ ++ return stpmic1_register_write(regul->low_power_reg, val); ++} ++ ++int stpmic1_lp_reg_on_off(const char *name, uint8_t enable) ++{ ++ const struct regul_struct *regul = get_regulator_data(name); ++ ++ if (!regul->low_power_reg) ++ return -1; ++ ++ return stpmic1_register_update(regul->low_power_reg, enable, ++ LDO_BUCK_ENABLE_MASK); ++} ++ ++int stpmic1_lp_set_voltage(const char *name, uint16_t millivolts) ++{ ++ size_t voltage_index = voltage_to_index(name, millivolts); ++ const struct regul_struct *regul = get_regulator_data(name); ++ uint8_t mask = 0; ++ ++ assert(voltage_index != VOLTAGE_INDEX_INVALID); ++ ++ mask = find_plat_mask(name); ++ if (!mask) ++ return 0; ++ ++ return stpmic1_register_update(regul->low_power_reg, voltage_index << 2, ++ mask); ++} ++ ++#ifdef CFG_STM32MP15 + int stpmic1_bo_enable_cfg(const char *name, struct stpmic1_bo_cfg *cfg) + { + const struct regul_struct *regul = get_regulator_data(name); +@@ -803,48 +954,6 @@ int stpmic1_bo_mask_reset_unpg(struct stpmic1_bo_cfg *cfg) + cfg->mrst_mask); + } + +-int stpmic1_regulator_voltage_get(const char *name) +-{ +- const struct regul_struct *regul = get_regulator_data(name); +- uint8_t value = 0; +- uint8_t mask = 0; +- +- mask = find_plat_mask(name); +- if (!mask) +- return 0; +- +- if (stpmic1_register_read(regul->control_reg, &value)) +- return -1; +- +- value = (value & mask) >> LDO_BUCK_VOLTAGE_SHIFT; +- +- if (value > regul->voltage_table_size) +- return -1; +- +- return regul->voltage_table[value]; +-} +- +-int stpmic1_lp_copy_reg(const char *name) +-{ +- const struct regul_struct *regul = get_regulator_data(name); +- uint8_t val = 0; +- int status = 0; +- +- if (!regul->low_power_reg) +- return -1; +- +- status = stpmic1_register_read(regul->control_reg, &val); +- if (status) +- return status; +- +- return stpmic1_register_write(regul->low_power_reg, val); +-} +- +-bool stpmic1_regu_has_lp_cfg(const char *name) +-{ +- return get_regulator_data(name)->low_power_reg; +-} +- + int stpmic1_lp_cfg(const char *name, struct stpmic1_lp_cfg *cfg) + { + const struct regul_struct *regul = get_regulator_data(name); +@@ -872,17 +981,6 @@ int stpmic1_lp_load_unpg(struct stpmic1_lp_cfg *cfg) + return status; + } + +-int stpmic1_lp_reg_on_off(const char *name, uint8_t enable) +-{ +- const struct regul_struct *regul = get_regulator_data(name); +- +- if (!regul->low_power_reg) +- return -1; +- +- return stpmic1_register_update(regul->low_power_reg, enable, +- LDO_BUCK_ENABLE_MASK); +-} +- + int stpmic1_lp_on_off_unpg(struct stpmic1_lp_cfg *cfg, int enable) + { + assert(cfg->lp_reg && (enable == 0 || enable == 1)); +@@ -910,22 +1008,6 @@ int stpmic1_lp_mode_unpg(struct stpmic1_lp_cfg *cfg, unsigned int mode) + BIT(LDO_BUCK_HPLP_POS)); + } + +-int stpmic1_lp_set_voltage(const char *name, uint16_t millivolts) +-{ +- size_t voltage_index = voltage_to_index(name, millivolts); +- const struct regul_struct *regul = get_regulator_data(name); +- uint8_t mask = 0; +- +- assert(voltage_index != VOLTAGE_INDEX_INVALID); +- +- mask = find_plat_mask(name); +- if (!mask) +- return 0; +- +- return stpmic1_register_update(regul->low_power_reg, voltage_index << 2, +- mask); +-} +- + /* Returns 1 if no configuration are expected applied at runtime, 0 otherwise */ + int stpmic1_lp_voltage_cfg(const char *name, uint16_t millivolts, + struct stpmic1_lp_cfg *cfg) +@@ -953,6 +1035,54 @@ int stpmic1_lp_voltage_unpg(struct stpmic1_lp_cfg *cfg) + + return stpmic1_register_update(cfg->lp_reg, cfg->value, cfg->mask); + } ++#endif ++ ++TEE_Result stpmic1_regulator_levels_mv(const char *name, ++ const uint16_t **levels, ++ size_t *levels_count) ++{ ++ const struct regul_struct *regul = get_regulator_data(name); ++ ++ if (!regul) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ if ((strncmp(name, "ldo3", 4) == 0) && ldo3_special_mode) { ++ if (levels_count) ++ *levels_count = ARRAY_SIZE(ldo3_special_mode_table); ++ if (levels) ++ *levels = ldo3_special_mode_table; ++ } else { ++ if (levels_count) ++ *levels_count = regul->voltage_table_size; ++ if (levels) ++ *levels = regul->voltage_table; ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++int stpmic1_regulator_voltage_get(const char *name) ++{ ++ const struct regul_struct *regul = get_regulator_data(name); ++ uint8_t value = 0; ++ uint8_t mask = 0; ++ ++ if ((strncmp(name, "ldo3", 4) == 0) && ldo3_special_mode) ++ return 0; ++ ++ mask = find_plat_mask(name); ++ if (mask) { ++ if (stpmic1_register_read(regul->control_reg, &value)) ++ return -1; ++ ++ value = (value & mask) >> LDO_BUCK_VOLTAGE_SHIFT; ++ } ++ ++ if (value > regul->voltage_table_size) ++ return -1; ++ ++ return regul->voltage_table[value]; ++} + + int stpmic1_register_read(uint8_t register_id, uint8_t *value) + { +diff --git a/core/drivers/sub.mk b/core/drivers/sub.mk +index f41f1951b..38f03282e 100644 +--- a/core/drivers/sub.mk ++++ b/core/drivers/sub.mk +@@ -2,6 +2,7 @@ srcs-$(CFG_CDNS_UART) += cdns_uart.c + srcs-$(CFG_PL011) += pl011.c + srcs-$(CFG_TZC400) += tzc400.c + srcs-$(CFG_TZC380) += tzc380.c ++srcs-$(CFG_FRAME_BUFFER) += frame_buffer.c + srcs-$(CFG_GIC) += gic.c + srcs-$(CFG_PL061) += pl061_gpio.c + srcs-$(CFG_PL022) += pl022_spi.c +@@ -26,11 +27,15 @@ srcs-$(CFG_ATMEL_SHDWC) += atmel_shdwc.c atmel_shdwc_a32.S + srcs-$(CFG_AMLOGIC_UART) += amlogic_uart.c + srcs-$(CFG_MVEBU_UART) += mvebu_uart.c + srcs-$(CFG_STM32_BSEC) += stm32_bsec.c +-srcs-$(CFG_STM32_ETZPC) += stm32_etzpc.c ++srcs-$(CFG_STM32_EXTI) += stm32_exti.c + srcs-$(CFG_STM32_GPIO) += stm32_gpio.c ++srcs-$(CFG_STM32_IWDG) += stm32_iwdg.c + srcs-$(CFG_STM32_I2C) += stm32_i2c.c + srcs-$(CFG_STM32_RNG) += stm32_rng.c ++srcs-$(CFG_STM32_RTC) += stm32_rtc.c ++srcs-$(CFG_STM32_TAMP) += stm32_tamp.c + srcs-$(CFG_STM32_UART) += stm32_uart.c ++srcs-$(CFG_STM32_LTDC) += stm32_ltdc.c + srcs-$(CFG_STPMIC1) += stpmic1.c + srcs-$(CFG_BCM_HWRNG) += bcm_hwrng.c + srcs-$(CFG_BCM_SOTP) += bcm_sotp.c +@@ -47,9 +52,12 @@ srcs-$(CFG_ZYNQMP_CSU_AES) += zynqmp_csu_aes.c + srcs-$(CFG_ZYNQMP_PM) += zynqmp_pm.c + srcs-$(CFG_ZYNQMP_HUK) += zynqmp_huk.c + ++subdirs-y += counter + subdirs-y += crypto ++subdirs-y += firewall + subdirs-$(CFG_BNXT_FW) += bnxt + subdirs-$(CFG_DRIVERS_CLK) += clk + subdirs-$(CFG_DRIVERS_RSTCTRL) += rstctrl ++subdirs-$(CFG_REGULATOR_DRIVERS) += regulator + subdirs-$(CFG_SCMI_MSG_DRIVERS) += scmi-msg + subdirs-y += imx +diff --git a/core/include/display.h b/core/include/display.h +new file mode 100644 +index 000000000..92a310da5 +--- /dev/null ++++ b/core/include/display.h +@@ -0,0 +1,52 @@ ++/* SPDX-License-Identifier: BSD-2-Clause */ ++/* ++ * Copyright (c) 2016-2021, Linaro Limited ++ */ ++ ++#ifndef __DISPLAY_H ++#define __DISPLAY_H ++ ++#include ++#include ++#include ++ ++struct disp_dev_list { ++ void *device; ++ TEE_Result (*device_init)(void *device); ++ TEE_Result (*device_final)(void *device); ++ TEE_Result (*device_activate)(void *device, ++ const struct frame_buffer *fb, ++ uint32_t x0, uint32_t y0); ++ TEE_Result (*device_get_display_size)(void *device, uint32_t *width, ++ uint32_t *height); ++ SLIST_ENTRY(disp_dev_list) link; ++}; ++ ++#ifdef CFG_DISPLAY ++TEE_Result display_init(void); ++struct frame_buffer *display_get_frame_buffer(void); ++void display_final(void); ++void display_register_device(struct disp_dev_list *ddev); ++TEE_Result display_get_fb_addr_from_dtb(paddr_t *pa, size_t *size); ++#else ++static inline TEE_Result display_init(void) ++{ return TEE_ERROR_NOT_SUPPORTED; } ++ ++static inline struct frame_buffer *display_get_frame_buffer(void) ++{ return NULL; } ++ ++static inline void display_final(void) ++{ } ++ ++static inline void display_register_device(struct disp_dev_list *ddev __unused) ++{ } ++ ++static inline TEE_Result display_get_fb_addr_from_dtb(paddr_t *pa __unused, ++ size_t *size __unused) ++{ ++ return TEE_ERROR_NOT_SUPPORTED; ++} ++#endif /* CFG_DISPLAY */ ++ ++#endif /* __DISPLAY_H */ ++ +diff --git a/core/include/drivers/clk.h b/core/include/drivers/clk.h +index 083a44f09..69492ebcb 100644 +--- a/core/include/drivers/clk.h ++++ b/core/include/drivers/clk.h +@@ -8,11 +8,14 @@ + + #include + #include ++#include + #include + + /* Flags for clock */ + #define CLK_SET_RATE_GATE BIT(0) /* must be gated across rate change */ + #define CLK_SET_PARENT_GATE BIT(1) /* must be gated across re-parent */ ++#define CLK_OPS_PARENT_ENABLE BIT(2) /* parent need enable during re-parent */ ++#define CLK_SET_RATE_PARENT BIT(3) /* propagate rate change up one level */ + + /** + * struct clk - Clock structure +@@ -28,6 +31,8 @@ + * @parents: Array of possible parents of the clock + */ + struct clk { ++ STAILQ_ENTRY(clk) link; ++ + const char *name; + void *priv; + const struct clk_ops *ops; +@@ -39,9 +44,38 @@ struct clk { + struct clk *parents[]; + }; + ++/** ++ * struct clk_rate_request ++ * ++ * @rate: Requested clock rate. This field will be adjusted by ++ * clock drivers according to hardware capabilities. ++ * @best_parent_rate: The best parent rate a parent can provide to fulfill the ++ * requested constraints. ++ * @best_parent: The most appropriate parent clock that fulfills the ++ * requested constraints. ++ * ++ */ ++struct clk_rate_request { ++ unsigned long rate; ++ unsigned long best_parent_rate; ++ struct clk *best_parent; ++}; ++ ++/** ++ * struct clk_duty - Struture encoding the duty cycle ratio of a clock ++ * ++ * @num: Numerator of the duty cycle ratio ++ * @den: Denominator of the duty cycle ratio ++ */ ++struct clk_duty { ++ unsigned int num; ++ unsigned int den; ++}; ++ + /** + * struct clk_ops + * ++ * @is_enabled: Get status of the clock (on / off) + * @enable: Enable the clock + * @disable: Disable the clock + * @set_parent: Set the clock parent based on index +@@ -50,6 +84,7 @@ struct clk { + * @get_rate: Get the clock rate + */ + struct clk_ops { ++ bool (*is_enabled)(struct clk *clk); + TEE_Result (*enable)(struct clk *clk); + void (*disable)(struct clk *clk); + TEE_Result (*set_parent)(struct clk *clk, size_t index); +@@ -58,6 +93,13 @@ struct clk_ops { + unsigned long parent_rate); + unsigned long (*get_rate)(struct clk *clk, + unsigned long parent_rate); ++ TEE_Result (*get_duty_cycle)(struct clk *clk, ++ struct clk_duty *duty); ++ unsigned long (*round_rate)(struct clk *clk, ++ unsigned long rate, ++ unsigned long parent_rate); ++ TEE_Result (*determine_rate)(struct clk *clk, ++ struct clk_rate_request *req); + }; + + /** +@@ -178,4 +220,22 @@ struct clk *clk_get_parent_by_index(struct clk *clk, size_t pidx); + */ + TEE_Result clk_set_parent(struct clk *clk, struct clk *parent); + ++/** ++ * clk_get_duty_cyle - Get clock duty cycle ++ * ++ * @clk: Clock for which the duty cycle is needed ++ * Returns the duty cycle structure ++ */ ++TEE_Result clk_get_duty_cyle(struct clk *clk, struct clk_duty *duty); ++ ++/** ++ * clk_round_rate - Round the given rate for a clock ++ * @clk: Clock for which the round rate is need ++ * @rate: The rate which is to be rounded ++ * Returns the closest rate actually supported by the clock. ++ */ ++unsigned long clk_round_rate(struct clk *clk, unsigned long rate); ++ ++void clk_summary(void); ++ + #endif /* __DRIVERS_CLK_H */ +diff --git a/core/include/drivers/clk_dt.h b/core/include/drivers/clk_dt.h +index e061e8405..106d3bf24 100644 +--- a/core/include/drivers/clk_dt.h ++++ b/core/include/drivers/clk_dt.h +@@ -6,6 +6,7 @@ + #ifndef __DRIVERS_CLK_DT_H + #define __DRIVERS_CLK_DT_H + ++#include + #include + #include + #include +diff --git a/core/include/drivers/counter.h b/core/include/drivers/counter.h +new file mode 100644 +index 000000000..3689756ff +--- /dev/null ++++ b/core/include/drivers/counter.h +@@ -0,0 +1,140 @@ ++/* SPDX-License-Identifier: BSD-2-Clause */ ++/* ++ * Copyright (c) 2020-2021, STMicroelectronics - All Rights Reserved ++ */ ++ ++#ifndef COUNTER_H ++#define COUNTER_H ++ ++#include ++#include ++#include ++#include ++#include ++ ++/** ++ * @brief Alarm structure. ++ * ++ * @param callback Callback called on alarm (cannot be NULL). ++ * @param ticks Number of ticks that triggers the alarm (absolute value). ++ * @param priv Private data returned in callback. ++ * @param is_enabled True if alarm is enabled, false otherwise. ++ */ ++struct alarm_cfg { ++ void (*callback)(unsigned int ticks, void *priv); ++ unsigned int ticks; ++ void *priv; ++ bool is_enabled; ++}; ++ ++struct counter_device; ++ ++struct counter_param { ++ uint32_t *params; ++ size_t len; ++}; ++ ++struct counter_ops { ++ TEE_Result (*start)(struct counter_device *counter, void *config); ++ TEE_Result (*stop)(struct counter_device *counter); ++ TEE_Result (*get_value)(struct counter_device *counter, unsigned int *ticks); ++ TEE_Result (*set_alarm)(struct counter_device *counter); ++ TEE_Result (*cancel_alarm)(struct counter_device *counter); ++ TEE_Result (*set_config)(struct counter_device *counter, ++ const void *param, ++ int len, void **config); ++ void (*release_config)(void *config); ++}; ++ ++/** ++ * @brief Start counter device in free running mode. ++ * ++ * @param name name of device. ++ * @param phandle dt phandle. ++ * @param dev_list list on counter device. ++ * @param is_used True if counter is used (exclusive consumer). ++ * @param ops Operation table of the counter. ++ * @param alarm Alarm configuration of the counter. ++ * @param max_ticks Tick max value supported by the counter. ++ * @param priv Optional private data supplied by driver. ++ */ ++struct counter_device { ++ const char *name; ++ int phandle; ++ LIST_ENTRY(counter_device) dev_list; ++ bool is_used; ++ const struct counter_ops *ops; ++ struct alarm_cfg alarm; ++ unsigned int max_ticks; ++ void *priv; ++}; ++ ++/** ++ * @brief Start counter device in free running mode. ++ */ ++TEE_Result counter_start(struct counter_device *counter, void *config); ++ ++/** ++ * @brief Stop counter device. ++ */ ++TEE_Result counter_stop(struct counter_device *counter); ++ ++/** ++ * @brief Get current counter value. ++ */ ++TEE_Result counter_get_value(struct counter_device *counter, unsigned int *ticks); ++ ++/** ++ * @brief Set an alarm. ++ */ ++TEE_Result counter_set_alarm(struct counter_device *counter); ++ ++/** ++ * @brief Cancel an alarm. ++ */ ++TEE_Result counter_cancel_alarm(struct counter_device *counter); ++ ++/** ++ * @brief Release the counter configuration. ++ */ ++void counter_release_config(struct counter_device *counter, void *config); ++ ++#ifdef CFG_DT ++/** ++ * @brief Parse and lookup a counter referenced by a device node. ++ * Retrieve an associated configuration. ++ * ++ * @retval counter device if successful, else 0 on error. ++ */ ++struct counter_device *fdt_counter_get(const void *fdt, ++ int offs, void **config); ++#else ++/** ++ * @brief Give the counter associated configuration link to given ++ * parameters. ++ * ++ * @retval TEE_SUCCESS if config is returned, error value otherwise. ++ */ ++TEE_Result counter_get_config(struct counter_device *cnt_dev, ++ const struct counter_param *param, ++ void **config); ++#endif ++ ++/** ++ * @brief get a reference on counter device. ++ * ++ * @retval counter device if successful, else 0 on error. ++ */ ++struct counter_device *counter_get_by_name(const char *name); ++ ++/* API for provider */ ++static inline void *counter_priv(struct counter_device *counter) ++{ ++ return (void *)counter->priv; ++} ++ ++struct counter_device *counter_dev_alloc(void); ++void counter_dev_free(struct counter_device *counter); ++TEE_Result counter_dev_register(struct counter_device *counter); ++ ++#endif /* COUNTER_H */ +diff --git a/core/include/drivers/frame_buffer.h b/core/include/drivers/frame_buffer.h +new file mode 100644 +index 000000000..4e504ea0d +--- /dev/null ++++ b/core/include/drivers/frame_buffer.h +@@ -0,0 +1,29 @@ ++/* SPDX-License-Identifier: BSD-2-Clause */ ++/* ++ * Copyright (c) 2016-2021, Linaro Limited ++ */ ++#ifndef __DRIVERS__FRAME_BUFFER_H ++#define __DRIVERS__FRAME_BUFFER_H ++ ++#include ++ ++enum frame_buffer_bpp { ++ FB_24BPP, ++}; ++ ++struct frame_buffer { ++ size_t width; ++ size_t height; ++ size_t width_dpi; ++ size_t height_dpi; ++ enum frame_buffer_bpp bpp; ++ void *base; ++}; ++ ++size_t frame_buffer_get_image_size(struct frame_buffer *fb, size_t width, ++ size_t height); ++void frame_buffer_clear(struct frame_buffer *fb, uint32_t color); ++void frame_buffer_set_image(struct frame_buffer *fb, size_t xpos, size_t ypos, ++ size_t width, size_t height, const void *image); ++ ++#endif /*__DRIVERS__FRAME_BUFFER_H*/ +diff --git a/core/include/drivers/gic.h b/core/include/drivers/gic.h +index 8a39aa346..94d8571c6 100644 +--- a/core/include/drivers/gic.h ++++ b/core/include/drivers/gic.h +@@ -9,6 +9,13 @@ + #include + #include + ++/* Constants to categorize priorities */ ++#define GIC_HIGHEST_SEC_PRIORITY 0x0U ++#define GIC_LOWEST_SEC_PRIORITY 0x7fU ++#define GIC_HIGHEST_NS_PRIORITY 0x80U ++#define GIC_LOWEST_NS_PRIORITY 0xfeU ++/* 0xff would disable all interrupts */ ++ + #define GIC_DIST_REG_SIZE 0x10000 + #define GIC_CPU_REG_SIZE 0x10000 + +@@ -19,11 +26,25 @@ + #define GIC_PPI(x) ((x) + GIC_PPI_BASE) + #define GIC_SPI(x) ((x) + GIC_SPI_BASE) + ++/* ++ * Save and restore some interrupts configuration during low power sequences. ++ * This is used on platforms using OP-TEE secure monitor. ++ */ ++struct gic_it_pm; ++ ++struct gic_pm { ++ struct gic_it_pm *pm_cfg; ++ size_t count; ++}; ++ + struct gic_data { + vaddr_t gicc_base; + vaddr_t gicd_base; + size_t max_it; + struct itr_chip chip; ++#if defined(CFG_ARM_GIC_PM) ++ struct gic_pm pm; ++#endif + }; + + /* +@@ -35,6 +56,9 @@ void gic_init(struct gic_data *gd, vaddr_t gicc_base, vaddr_t gicd_base); + /* initial base address only */ + void gic_init_base_addr(struct gic_data *gd, vaddr_t gicc_base, + vaddr_t gicd_base); ++/* Setup GIC default configuration */ ++void gic_init_setup(struct gic_data *gd); ++ + /* initial cpu if only, mainly use for secondary cpu setup cpu interface */ + void gic_cpu_init(struct gic_data *gd); + +diff --git a/core/include/drivers/regulator.h b/core/include/drivers/regulator.h +new file mode 100644 +index 000000000..827b6810f +--- /dev/null ++++ b/core/include/drivers/regulator.h +@@ -0,0 +1,135 @@ ++/* SPDX-License-Identifier: BSD-2-Clause */ ++/* ++ * Copyright (c) 2020-2021, STMicroelectronics - All Rights Reserved ++ */ ++#ifndef REGULATOR_H ++#define REGULATOR_H ++ ++#include ++#include ++#include ++ ++const char *plat_get_lp_mode_name(int mode); ++int plat_get_lp_mode_count(void); ++ ++/* ++ * Consumer interface ++ */ ++ ++/* regulator-always-on : regulator should never be disabled */ ++#define REGUL_ALWAYS_ON BIT(0) ++/* ++ * regulator-boot-on: ++ * It's expected that this regulator was left on by the bootloader. ++ * The core shouldn't prevent it from being turned off later. ++ * The regulator is needed to exit from suspend so it is turned on during suspend entry. ++ */ ++#define REGUL_BOOT_ON BIT(1) ++ ++/* ++ * ST proprietary flags: to be in a private struct ++ * To move to driver private. ++ */ ++ ++/* regulator-over-current-protection: Enable over current protection. */ ++#define REGUL_OCP BIT(2) ++/* regulator-active-discharge: enable active discharge. */ ++#define REGUL_ACTIVE_DISCHARGE BIT(3) ++/* regulator-pull-down: Enable pull down resistor when the regulator is disabled. */ ++#define REGUL_PULL_DOWN BIT(4) ++/* ++ * st,mask-reset: set mask reset for the regulator, meaning that the regulator ++ * setting is maintained during pmic reset. ++ */ ++#define REGUL_MASK_RESET BIT(5) ++/* st,regulator-sink-source: set the regulator in sink source mode */ ++#define REGUL_SINK_SOURCE BIT(6) ++/* st,regulator-bypass: set the regulator in bypass mode */ ++#define REGUL_ENABLE_BYPASS BIT(7) ++ ++struct rdev *regulator_get_by_node_name(const char *node_name); ++struct rdev *regulator_get_by_regulator_name(const char *reg_name); ++struct rdev *regulator_get_by_supply_name(const void *fdt, int node, ++ const char *name); ++ ++TEE_Result regulator_enable(struct rdev *rdev); ++TEE_Result regulator_disable(struct rdev *rdev); ++bool regulator_is_enabled(const struct rdev *rdev); ++ ++TEE_Result regulator_set_voltage(struct rdev *rdev, uint16_t level_mv); ++TEE_Result regulator_set_min_voltage(struct rdev *rdev); ++TEE_Result regulator_get_voltage(const struct rdev *rdev, uint16_t *level_mv); ++ ++TEE_Result regulator_list_voltages(struct rdev *rdev, uint16_t **levels, ++ size_t *count); ++void regulator_get_range(const struct rdev *rdev, uint16_t *min_mv, ++ uint16_t *max_mv); ++TEE_Result regulator_set_flag(struct rdev *rdev, uint16_t flag); ++ ++/* ++ * Driver Interface ++ */ ++ ++/* suspend() arguments */ ++#define LP_STATE_OFF BIT(0) ++#define LP_STATE_ON BIT(1) ++#define LP_STATE_UNCHANGED BIT(2) ++#define LP_STATE_SET_VOLT BIT(3) ++ ++struct regul_desc { ++ const char *node_name; ++ const struct regul_ops *ops; ++ const void *driver_data; ++ const char *supply_name; ++ const uint32_t ramp_delay_uv_per_us; ++; const uint32_t enable_ramp_delay_us; ++}; ++ ++struct regul_ops { ++ TEE_Result (*set_state)(const struct regul_desc *desc, bool enabled); ++ TEE_Result (*get_state)(const struct regul_desc *desc, bool *enabled); ++ TEE_Result (*set_voltage)(const struct regul_desc *desc, uint16_t mv); ++ TEE_Result (*get_voltage)(const struct regul_desc *desc, uint16_t *mv); ++ TEE_Result (*list_voltages)(const struct regul_desc *desc, ++ uint16_t **levels, size_t *count); ++ TEE_Result (*set_flag)(const struct regul_desc *desc, uint16_t flag); ++ void (*lock)(const struct regul_desc *desc); ++ void (*unlock)(const struct regul_desc *desc); ++ TEE_Result (*suspend)(const struct regul_desc *desc, uint8_t state, ++ uint16_t mv); ++}; ++ ++TEE_Result regulator_register(const struct regul_desc *desc, int node); ++ ++/* ++ * Internal regulator structure ++ * The structure is internal to the core, and the content should not be used ++ * by a consumer nor a driver. ++ */ ++struct rdev { ++ const struct regul_desc *desc; ++ int32_t phandle; ++ uint16_t min_mv; ++ uint16_t max_mv; ++ uint16_t cur_mv; ++ uint16_t flags; ++ uint32_t ramp_delay_uv_per_us; ++ unsigned int enable_ramp_delay_us; ++ const char *reg_name; ++ uint8_t use_count; ++ int32_t supply_phandle; ++ struct rdev *supply_dev; ++ uint8_t *lp_state; ++ uint16_t *lp_mv; ++ SLIST_ENTRY(rdev) link; ++}; ++ ++#ifdef CFG_REGULATOR_DRIVERS ++void regulator_core_dump(void); ++#else ++static inline void regulator_core_dump(void) ++{ ++} ++#endif ++ ++#endif /* REGULATOR_H */ +diff --git a/core/include/drivers/scmi-msg.h b/core/include/drivers/scmi-msg.h +index d6e24ebb2..b5eff3957 100644 +--- a/core/include/drivers/scmi-msg.h ++++ b/core/include/drivers/scmi-msg.h +@@ -209,6 +209,9 @@ int32_t plat_scmi_clock_rates_by_step(unsigned int channel_id, + unsigned long plat_scmi_clock_get_rate(unsigned int channel_id, + unsigned int scmi_id); + ++unsigned long plat_scmi_clock_round_rate(unsigned int channel_id, ++ unsigned int scmi_id, unsigned long rate); ++ + /* + * Set clock rate in Hertz + * @channel_id: SCMI channel ID +@@ -238,6 +241,11 @@ int32_t plat_scmi_clock_get_state(unsigned int channel_id, + int32_t plat_scmi_clock_set_state(unsigned int channel_id, unsigned int scmi_id, + bool enable_not_disable); + ++struct clk_duty; ++ ++int32_t plat_scmi_clock_get_duty_cycle(unsigned int channel_id, unsigned int scmi_id, ++ struct clk_duty *duty); ++ + /* Handlers for SCMI Reset Domain protocol services */ + + /* +@@ -286,7 +294,7 @@ int32_t plat_scmi_rd_set_state(unsigned int channel_id, unsigned int scmi_id, + size_t plat_scmi_voltd_count(unsigned int channel_id); + + /* +- * Get clock controller string ID (aka name) ++ * Get voltage domain string ID (aka name) + * @channel_id: SCMI channel ID + * @scmi_id: SCMI voltage domain ID + * Return pointer to name or NULL +@@ -324,14 +332,14 @@ int32_t plat_scmi_voltd_levels_by_step(unsigned int channel_id, + * Get current voltage domain level in microvolt + * @channel_id: SCMI channel ID + * @scmi_id: SCMI voltage domain ID +- * Return clock rate or 0 if not supported ++ * Return voltage domain level or 0 if not supported + */ + long plat_scmi_voltd_get_level(unsigned int channel_id, unsigned int scmi_id); + + /* + * Set voltage domain level voltage domain + * @channel_id: SCMI channel ID +- * @scmi_id: SCMI clock ID ++ * @scmi_id: SCMI voltage domain ID + * @level: Target voltage domain level in microvolt + * Return a compliant SCMI error code + */ +@@ -358,4 +366,89 @@ int32_t plat_scmi_voltd_get_config(unsigned int channel_id, + int32_t plat_scmi_voltd_set_config(unsigned int channel_id, + unsigned int scmi_id, uint32_t config); + ++/* Handlers for SCMI Performance Domain Management protocol services */ ++ ++/* ++ * Return number of performance domains for the channel ++ * @channel_id: SCMI channel ID ++ * Return number of performance domains for the channel ++ */ ++size_t plat_scmi_perf_count(unsigned int channel_id); ++ ++/* ++ * Return number of performance domains for the channel ++ * @channel_id: SCMI channel ID ++ * @stats_len: Output byte size of the statistics buffer or zero ++ * Return pointer to SCMI server statistics buffer for the channel or NULL ++ */ ++void *plat_scmi_perf_statistics_buf(unsigned int channel_id, size_t *stats_len); ++ ++/* ++ * Get performance domain string ID (aka name) ++ * @channel_id: SCMI channel ID ++ * @domain_id: SCMI performance domain ID ++ * Return pointer to name or NULL ++ */ ++const char *plat_scmi_perf_domain_name(unsigned int channel_id, ++ unsigned int domain_id); ++ ++/* ++ * Get performance domain possible levels as an array of unsigned int ++ * ++ * @channel_id: SCMI channel ID ++ * @domain_id: SCMI performance domain ID ++ * @start_index: Level index to start from. ++ * @levels: If NULL, function returns, else output level array ++ * @nb_elts: Array size of @levels. ++ * Return an SCMI compliant error code ++ */ ++int32_t plat_scmi_perf_levels_array(unsigned int channel_id, ++ unsigned int domain_id, size_t start_index, ++ unsigned int *elt, size_t *nb_elts); ++ ++/* ++ * Get latency is microseconds for transition to target performance level ++ * @channel_id: SCMI channel ID ++ * @domain_id: SCMI performance domain ID ++ * @level: Target performance level ++ * @latency: Output latency value (microsecond) for the target level ++ * Return a compliant SCMI error code ++ */ ++int32_t plat_scmi_perf_level_latency_us(unsigned int channel_id, ++ unsigned int domain_id, ++ unsigned int level, ++ unsigned int *latency); ++ ++/* ++ * Get power cost value related to target performance level ++ * @channel_id: SCMI channel ID ++ * @domain_id: SCMI performance domain ID ++ * @level: Target performance level ++ * @power_cost: Output power cost for the performance level ++ * Return a compliant SCMI error code ++ */ ++int32_t plat_scmi_perf_level_power_cost(unsigned int channel_id, ++ unsigned int domain_id, ++ unsigned int level, ++ unsigned int *power_cost); ++ ++/* ++ * Get current performance level of the domain ++ * @channel_id: SCMI channel ID ++ * @domain_id: SCMI performance domain ID ++ * @level: Output performance level ++ * Return a compliant SCMI error code ++ */ ++int32_t plat_scmi_perf_level_get(unsigned int channel_id, ++ unsigned int domain_id, unsigned int *level); ++ ++/* ++ * Request change of performance level for the domain ++ * @channel_id: SCMI channel ID ++ * @domain_id: SCMI performance domain ID ++ * @level: Target performance level ++ * Return a compliant SCMI error code ++ */ ++int32_t plat_scmi_perf_level_set(unsigned int channel_id, ++ unsigned int domain_id, unsigned int level); + #endif /* SCMI_MSG_H */ +diff --git a/core/include/drivers/scmi_regulator_consumer.h b/core/include/drivers/scmi_regulator_consumer.h +new file mode 100644 +index 000000000..b4273fba2 +--- /dev/null ++++ b/core/include/drivers/scmi_regulator_consumer.h +@@ -0,0 +1,12 @@ ++/* ++ * Copyright (c) 2020-2021, STMicroelectronics - All Rights Reserved ++ * ++ * SPDX-License-Identifier: BSD-3-Clause ++ */ ++ ++#ifndef SCMI_VOLTD_CONSUMER_H ++#define SCMI_VOLTD_CONSUMER_H ++ ++int scmi_voltd_consumer_init(void); ++ ++#endif /* SCMI_VOLTD_CONSUMER_H */ +diff --git a/core/include/drivers/stm32_bsec.h b/core/include/drivers/stm32_bsec.h +index b6afbf8c4..71f2061d2 100644 +--- a/core/include/drivers/stm32_bsec.h ++++ b/core/include/drivers/stm32_bsec.h +@@ -1,6 +1,6 @@ + /* SPDX-License-Identifier: BSD-3-Clause */ + /* +- * Copyright (c) 2017-2020, STMicroelectronics ++ * Copyright (c) 2017-2021, STMicroelectronics + */ + + #ifndef __STM32_BSEC_H +@@ -10,6 +10,19 @@ + #include + #include + ++/* BSEC_DEBUG */ ++#define BSEC_HDPEN BIT(4) ++#define BSEC_SPIDEN BIT(5) ++#define BSEC_SPINDEN BIT(6) ++#define BSEC_DBGSWGEN BIT(10) ++#define BSEC_DEBUG_ALL (BSEC_HDPEN | \ ++ BSEC_SPIDEN | \ ++ BSEC_SPINDEN | \ ++ BSEC_DBGSWGEN) ++ ++#define BSEC_BITS_PER_WORD (8U * sizeof(uint32_t)) ++#define BSEC_BYTES_PER_WORD (sizeof(uint32_t)) ++ + /* + * Load OTP from SAFMEM and provide its value + * @value: Output read value +@@ -69,14 +82,7 @@ TEE_Result stm32_bsec_permanent_lock_otp(uint32_t otp_id); + * @value: Value to write + * Return a TEE_Result compliant return value + */ +-#ifdef CFG_STM32_BSEC_WRITE + TEE_Result stm32_bsec_write_debug_conf(uint32_t value); +-#else +-static inline TEE_Result stm32_bsec_write_debug_conf(uint32_t value __unused) +-{ +- return TEE_ERROR_NOT_SUPPORTED; +-} +-#endif + + /* Return debug configuration read from BSEC */ + uint32_t stm32_bsec_read_debug_conf(void); +@@ -141,10 +147,45 @@ TEE_Result stm32_bsec_read_permanent_lock(uint32_t otp_id, bool *locked); + */ + TEE_Result stm32_bsec_otp_lock(uint32_t service); + ++/* ++ * Return true if OTP can be read checking ID and invalid state ++ * @otp_id: OTP number ++ */ ++bool stm32_bsec_can_access_otp(uint32_t otp_id); ++ + /* + * Return true if non-secure world is allowed to read the target OTP + * @otp_id: OTP number + */ + bool stm32_bsec_nsec_can_access_otp(uint32_t otp_id); + ++/* ++ * Find and get OTP location from its name. ++ * @name: sub-node name to look up. ++ * @otp_id: pointer to read OTP number or NULL. ++ * @otp_bit_len: pointer to read OTP length in bits or NULL. ++ * Return a TEE_Result compliant status ++ */ ++TEE_Result stm32_bsec_find_otp_in_nvmem_layout(const char *name, ++ uint32_t *otp_id, ++ size_t *otp_bit_len); ++ ++/* ++ * get BSEC global state. ++ * @state: global state ++ * [1:0] BSEC state ++ * 00b: Sec Open ++ * 01b: Sec Closed ++ * 11b: Invalid ++ * [8]: Hardware Key set = 1b ++ * Return a TEE_Result compliant status ++ */ ++TEE_Result stm32_bsec_get_state(uint32_t *state); ++ ++#define BSEC_STATE_SEC_OPEN U(0x0) ++#define BSEC_STATE_SEC_CLOSED U(0x1) ++#define BSEC_STATE_INVALID U(0x3) ++ ++#define BSEC_HARDWARE_KEY BIT(8) ++ + #endif /*__STM32_BSEC_H*/ +diff --git a/core/include/drivers/stm32_etzpc.h b/core/include/drivers/stm32_etzpc.h +index 50bbc40bb..7900b8f5a 100644 +--- a/core/include/drivers/stm32_etzpc.h ++++ b/core/include/drivers/stm32_etzpc.h +@@ -1,12 +1,13 @@ + /* SPDX-License-Identifier: BSD-3-Clause */ + /* +- * Copyright (c) 2014, ARM Limited and Contributors. All rights reserved. +- * Copyright (c) 2018-2019, STMicroelectronics ++ * Copyright (c) 2014-2021, ARM Limited and Contributors. All rights reserved. ++ * Copyright (c) 2018-2021, STMicroelectronics + */ + + #ifndef __STM32_ETZPC_H__ + #define __STM32_ETZPC_H__ + ++#include + #include + #include + +@@ -18,71 +19,20 @@ enum etzpc_decprot_attributes { + ETZPC_DECPROT_MAX = 4, + }; + ++struct stm32_etzpc_platdata { ++ const char *name; ++ uintptr_t base; ++ struct clk *clk; ++ uint8_t *periph_cfg; ++ uint16_t *tzma_cfg; ++}; ++ + #define ETZPC_TZMA_ALL_SECURE GENMASK_32(9, 0) + #define ETZPC_TZMA_ALL_NO_SECURE 0x0 + +-/* +- * Load a DECPROT configuration +- * @decprot_id: ID that is the index of the DECPROT in the ETZPC interface +- * @decprot_attr: Restriction access attributes +- */ +-void etzpc_configure_decprot(uint32_t decprot_id, +- enum etzpc_decprot_attributes decprot_attr); +- +-/* +- * Get the DECPROT attribute +- * @decprot_id: ID that is the index of the DECPROT in the ETZPC interface +- * Return attribute of this DECPROT +- */ +-enum etzpc_decprot_attributes etzpc_get_decprot(uint32_t decprot_id); +- +-/* +- * Lock access to the DECPROT attributes +- * @decprot_id: ID that is the index of the DECPROT in the ETZPC interface +- */ +-void etzpc_lock_decprot(uint32_t decprot_id); +- +-/* +- * Return the lock status of the target DECPROT +- * @decprot_id: ID that is the index of the DECPROT in the ETZPC interface +- */ +-bool etzpc_get_lock_decprot(uint32_t decprot_id); +- +-/* +- * Configure the target TZMA read only size +- * @tzma_id: ID that is the index of the TZMA in the ETZPC interface +- * @tzma_value: Read-only size +- */ +-void etzpc_configure_tzma(uint32_t tzma_id, uint16_t tzma_value); +- +-/* +- * Get the target TZMA read only size +- * @tzma_id: ID that is the index of the TZMA in the ETZPC interface +- * Return the size of read-only area +- */ +-uint16_t etzpc_get_tzma(uint32_t tzma_id); +- +-/* +- * Lock the target TZMA +- * @tzma_id: ID that is the index of the TZMA in the ETZPC interface +- */ +-void etzpc_lock_tzma(uint32_t tzma_id); +- +-/* +- * Return the lock status of the target TZMA +- * @tzma_id: ID that is the index of the TZMA in the ETZPC interface +- * Return true if TZMA is locked, false otherwise +- */ +-bool etzpc_get_lock_tzma(uint32_t tzma_id); +- +-/* +- * Init the ETZPC device, nedded when not using the device tree +- * @base: ETZPC interface registers physcal base address +- */ + #ifdef CFG_STM32_ETZPC + void stm32_etzpc_init(paddr_t base); + #else + static inline void stm32_etzpc_init(paddr_t __unused base) {} + #endif +- + #endif /*__STM32_ETZPC_H__*/ +diff --git a/core/include/drivers/stm32_exti.h b/core/include/drivers/stm32_exti.h +new file mode 100644 +index 000000000..7a758d16c +--- /dev/null ++++ b/core/include/drivers/stm32_exti.h +@@ -0,0 +1,73 @@ ++/* SPDX-License-Identifier: BSD-3-Clause */ ++/* ++ * Copyright (c) 2021, STMicroelectronics ++ */ ++ ++#ifndef __STM32_EXTI_H ++#define __STM32_EXTI_H ++ ++#include ++ ++#define EXTI_TYPE_RISING 1 ++#define EXTI_TYPE_FALLING 2 ++#define EXTI_TYPE_BOTH (EXTI_TYPE_RISING | EXTI_TYPE_FALLING) ++ ++/* ++ * Set EXTI type in RTSR and FTSR EXTI registers ++ * @exti_line: EXTI line number ++ * @type: type (rising, falling or both) to set ++ * Return a TEE_Result compliant return value ++ */ ++void stm32_exti_set_type(uint32_t exti, uint32_t type); ++ ++/* ++ * Mask EXTI Interrupt (IMR) ++ * @exti_line: EXTI line number ++ * Return a TEE_Result compliant return value ++ */ ++void stm32_exti_mask(uint32_t exti_line); ++ ++/* ++ * Unmask EXTI Interrupt (IMR) ++ * @exti_line: EXTI line number ++ * Return a TEE_Result compliant return value ++ */ ++void stm32_exti_unmask(uint32_t exti_line); ++ ++/* ++ * Enable EXTI line as wakeup interrupt ++ * @exti_line: EXTI line number ++ * Return a TEE_Result compliant return value ++ */ ++void stm32_exti_enable_wake(uint32_t exti_line); ++ ++/* ++ * Disable EXTI line as wakeup interrupt ++ * @exti_line: EXTI line number ++ * Return a TEE_Result compliant return value ++ */ ++void stm32_exti_disable_wake(uint32_t exti_line); ++ ++/* ++ * Clear pending EXTI interrupts ++ * @exti_line: EXTI line number ++ * Return a TEE_Result compliant return value ++ */ ++void stm32_exti_clear(uint32_t exti_line); ++ ++/* ++ * Configure EXTI mux for GPIO irq ++ * @bank: GPIO bank id ++ * @pin: GPIO number in the bank ++ * Return a TEE_Result compliant return value ++ */ ++void stm32_exti_set_gpio_port_sel(uint8_t bank, uint8_t pin); ++ ++/* ++ * Securize the EXTI line ++ * @exti_line: EXTI line number ++ * Return a TEE_Result compliant return value ++ */ ++void stm32_exti_set_tz(uint32_t exti_line); ++ ++#endif /*__STM32_EXTI_H*/ +diff --git a/core/include/drivers/stm32_firewall.h b/core/include/drivers/stm32_firewall.h +new file mode 100644 +index 000000000..104bc6d5b +--- /dev/null ++++ b/core/include/drivers/stm32_firewall.h +@@ -0,0 +1,212 @@ ++/* SPDX-License-Identifier: BSD-2-Clause */ ++/* ++ * Copyright (c) 2021, STMicroelectronics - All Rights Reserved ++ */ ++ ++#ifndef __DRIVERS_STM32_FIREWALL_H ++#define __DRIVERS_STM32_FIREWALL_H ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++/* ++ * Firewall configuration ++ * ++ * 32b configuration ID contains, from LSbits to MSbits: ++ * - 8 bit access permissions bit flags (rw, u/p, s/ns) ++ * - 8 bit attribute flags (locking) ++ * - 16 bit platform specific ID of the access master ++ */ ++ ++/* Master configuration bits */ ++#define FWLL_MASTER_SHIFT 16 ++#define FWLL_MASTER(x) ((x) << FWLL_MASTER_SHIFT) ++#define FWLL_MASTER_MASK GENMASK_32(31, 16) ++ ++/* Lock configuration */ ++#define FWLL_CONF_LOCK BIT(15) ++ ++/* Access configuration */ ++#define FWLL_CONF_MASK GENMASK_32(7, 0) ++#define FWLL_NO_ACCESS 0 ++#define FWLL_SEC_READ BIT(0) ++#define FWLL_SEC_WRITE BIT(1) ++#define FWLL_SEC_PRIV BIT(2) ++#define FWLL_SEC_UNPRIV BIT(3) ++#define FWLL_NSEC_READ BIT(4) ++#define FWLL_NSEC_WRITE BIT(5) ++#define FWLL_NSEC_PRIV BIT(6) ++#define FWLL_NSEC_UNPRIV BIT(7) ++ ++#define FWLL_SEC_RW (FWLL_SEC_READ | \ ++ FWLL_SEC_WRITE | \ ++ FWLL_SEC_PRIV | \ ++ FWLL_SEC_UNPRIV) ++ ++#define FWLL_NSEC_RW (FWLL_NSEC_READ | \ ++ FWLL_NSEC_WRITE | \ ++ FWLL_NSEC_PRIV | \ ++ FWLL_NSEC_UNPRIV) ++ ++#define FWLL_FULL_ACCESS (FWLL_SEC_RW | \ ++ FWLL_NSEC_RW) ++ ++struct stm32_firewall_device; ++ ++/** ++ * @brief struct stm32_firewall_reg - define the firewall device management ++ * ++ * @addr : controlled physical address. ++ * @size : length of the controlled area. ++ */ ++struct stm32_firewall_reg { ++ paddr_t addr; ++ size_t size; ++}; ++ ++/** ++ * @brief struct stm32_firewall_compat - Device tree compatibility table ++ * ++ * @reg: stm32_firewall_reg structure. ++ * @compat_size: Array size of the stm32_firewall_reg structure. ++ */ ++struct stm32_firewall_compat { ++ struct stm32_firewall_reg *reg; ++ size_t compat_size; ++}; ++ ++/** ++ * @brief struct stm32_firewall_cfg - Firewall configuration structure ++ * ++ * @access: Defined configuration value defining master and its ++ * associated firewall configuration. ++ */ ++struct stm32_firewall_cfg { ++ uint32_t access; ++}; ++ ++/** ++ * @brief struct stm32_firewall_ops ++ * ++ * @has_access: Check the access regarding the requested configuration. ++ * @configure_access: Configure access based on requested configuration. ++ * @request_access: Lock access for the requester. ++ * @release_access: Unlock access of the requester. ++ */ ++struct stm32_firewall_ops { ++ TEE_Result (*has_access)(struct stm32_firewall_device *fdev, ++ unsigned int id, ++ paddr_t addr, size_t sz, ++ const struct stm32_firewall_cfg *cfg); ++ TEE_Result (*configure_access)(struct stm32_firewall_device *fdev, ++ unsigned int id, ++ paddr_t addr, size_t sz, ++ const struct stm32_firewall_cfg *cfg); ++ TEE_Result (*request_access)(struct stm32_firewall_device *fdev, ++ unsigned int id, ++ paddr_t addr, size_t sz, ++ const struct stm32_firewall_cfg *cfg); ++ TEE_Result (*release_access)(struct stm32_firewall_device *fdev, ++ unsigned int id, ++ paddr_t addr, size_t sz, ++ const struct stm32_firewall_cfg *cfg); ++}; ++ ++/** ++ * @brief struct stm32_firewall_device ++ * ++ * @name: device name. ++ * @dev_list: list on counter device. ++ * @ops: Operation table of the firewall. ++ * @priv: Optional private data supplied by driver. ++ */ ++struct stm32_firewall_device { ++ const char *name; ++ const struct stm32_firewall_ops *ops; ++ const struct stm32_firewall_compat *compat; ++ void *priv; ++ ++ LIST_ENTRY(stm32_firewall_device) dev_list; ++}; ++ ++/* API for provider */ ++ ++/** ++ * @brief stm32_firewall_priv - Retrieve firewall private data ++ * ++ * @fdev: stm32_firewall_device struct to be freed. ++ * ++ * Return firewall private data ++ */ ++static inline void *stm32_firewall_priv(struct stm32_firewall_device *fdev) ++{ ++ return fdev->priv; ++} ++ ++/** ++ * @brief stm32_firewall_check_access - Check the configuration access ++ * regarding a defined range. ++ * ++ * @base: Physical address ++ * @size: Size to be checked ++ * @cfg: Tested configuration ++ * ++ * Returns a TEE_Result compliant value. ++ */ ++TEE_Result stm32_firewall_check_access(paddr_t base, size_t size, ++ const struct stm32_firewall_cfg *cfg); ++ ++/** ++ * @brief stm32_firewall_set_config - Define the configuration access ++ * regarding a defined range. ++ * ++ * @base: Physical address ++ * @size: Size to be checked ++ * @cfg: Tested configuration ++ * ++ * Returns a TEE_Result compliant value. ++ */ ++TEE_Result stm32_firewall_set_config(paddr_t base, size_t size, ++ const struct stm32_firewall_cfg *cfg); ++ ++/** ++ * @brief stm32_firewall_dev_alloc - Allocate a new firewall device. ++ * ++ * Returns a stm32_firewall_device struct or NULL if allocation failed. ++ */ ++struct stm32_firewall_device *stm32_firewall_dev_alloc(void); ++ ++/** ++ * @brief stm32_firewall_dev_free - Free a firewall device. ++ * ++ * @fdev: stm32_firewall_device struct to be freed. ++ */ ++void stm32_firewall_dev_free(struct stm32_firewall_device *fdev); ++ ++/** ++ * @brief stm32_firewall_dev_register - Register a firewall device ++ * list. ++ * ++ * @fdev: stm32_firewall_device struct to be added. ++ * ++ * Returns a TEE_Result compliant value. ++ */ ++TEE_Result stm32_firewall_dev_register(struct stm32_firewall_device *fdev); ++ ++/** ++ * @brief stm32_firewall_bus_probe - Probe a firewall bus driver child. ++ * ++ * @fdev: firewall_device struct to be probed. ++ * @fdt: device tree address. ++ * @node: firewall bus node. ++ * ++ * Return a TEE_Result compliant value. ++ */ ++TEE_Result stm32_firewall_bus_probe(struct stm32_firewall_device *fdev, ++ const void *fdt, int node); ++#endif /* __DRIVERS_STM32_FIREWALL_H */ +diff --git a/core/include/drivers/stm32_gpio.h b/core/include/drivers/stm32_gpio.h +index 6a241bab3..7c0ae4e79 100644 +--- a/core/include/drivers/stm32_gpio.h ++++ b/core/include/drivers/stm32_gpio.h +@@ -17,37 +17,40 @@ + #include + #include + #include ++#include ++#include ++#include + +-#define GPIO_MODE_INPUT 0x0 +-#define GPIO_MODE_OUTPUT 0x1 +-#define GPIO_MODE_ALTERNATE 0x2 +-#define GPIO_MODE_ANALOG 0x3 ++#define GPIO_MODE_INPUT U(0x0) ++#define GPIO_MODE_OUTPUT U(0x1) ++#define GPIO_MODE_ALTERNATE U(0x2) ++#define GPIO_MODE_ANALOG U(0x3) + +-#define GPIO_OTYPE_PUSH_PULL 0x0 +-#define GPIO_OTYPE_OPEN_DRAIN 0x1 ++#define GPIO_OTYPE_PUSH_PULL U(0x0) ++#define GPIO_OTYPE_OPEN_DRAIN U(0x1) + +-#define GPIO_OSPEED_LOW 0x0 +-#define GPIO_OSPEED_MEDIUM 0x1 +-#define GPIO_OSPEED_HIGH 0x2 +-#define GPIO_OSPEED_VERY_HIGH 0x3 ++#define GPIO_OSPEED_LOW U(0x0) ++#define GPIO_OSPEED_MEDIUM U(0x1) ++#define GPIO_OSPEED_HIGH U(0x2) ++#define GPIO_OSPEED_VERY_HIGH U(0x3) + +-#define GPIO_PUPD_NO_PULL 0x0 +-#define GPIO_PUPD_PULL_UP 0x1 +-#define GPIO_PUPD_PULL_DOWN 0x2 ++#define GPIO_PUPD_NO_PULL U(0x0) ++#define GPIO_PUPD_PULL_UP U(0x1) ++#define GPIO_PUPD_PULL_DOWN U(0x2) + +-#define GPIO_OD_LEVEL_LOW 0x0 +-#define GPIO_OD_LEVEL_HIGH 0x1 ++#define GPIO_OD_LEVEL_LOW U(0x0) ++#define GPIO_OD_LEVEL_HIGH U(0x1) + + /* + * GPIO configuration description structured as single 16bit word + * for efficient save/restore when GPIO pin suspends or resumes. + * +- * @mode: One of GPIO_MODE_* +- * @otype: One of GPIO_OTYPE_* +- * @ospeed: One of GPIO_OSPEED_* +- * @pupd: One of GPIO_PUPD_* +- * @od: One of GPIO_OD_* +- * @af: Alternate function numerical ID between 0 and 15 ++ * @mode: One of GPIO_MODE_* ++ * @otype: One of GPIO_OTYPE_* ++ * @ospeed: One of GPIO_OSPEED_* ++ * @pupd: One of GPIO_PUPD_* ++ * @od: One of GPIO_OD_* ++ * @af: Alternate function numerical ID between 0 and 15 + */ + struct gpio_cfg { + uint16_t mode: 2; +@@ -63,94 +66,69 @@ struct gpio_cfg { + * + * @bank: GPIO bank identifier as assigned by the platform + * @pin: Pin number in the GPIO bank +- * @active_cfg: Configuratioh in active state +- * @standby_cfg: Configuratioh in standby state ++ * @config: Configuratioh in active state ++ * @link: Link to chain stm32_pinctrl structure in the list + */ + struct stm32_pinctrl { + uint8_t bank; + uint8_t pin; +- struct gpio_cfg active_cfg; +- struct gpio_cfg standby_cfg; ++ struct gpio_cfg config; ++ ++ STAILQ_ENTRY(stm32_pinctrl) link; + }; + + /* +- * Apply series of pin muxing configuration, active state and standby state ++ * Structure used to define list of stm32_pinctrl + * +- * @pinctrl: array of pinctrl references +- * @count: Number of entries in @pinctrl ++ * @stm32_pinctrl_list Structure name ++ * @stm32_pinctrl Element of the structure + */ +-void stm32_pinctrl_load_active_cfg(struct stm32_pinctrl *pinctrl, size_t cnt); +-void stm32_pinctrl_load_standby_cfg(struct stm32_pinctrl *pinctrl, size_t cnt); ++STAILQ_HEAD(stm32_pinctrl_list, stm32_pinctrl); ++ ++/* Get thr GPIO ID related to the pin referred by @pin */ ++unsigned int stm32_pinctrl_get_gpio_id(struct stm32_pinctrl *pin); + + /* +- * Save the current pin configuration as the standby state for a pin series ++ * Apply series of pin muxing configuration + * +- * @pinctrl: array of pinctrl references +- * @count: Number of entries in @pinctrl ++ * @list: List of the pinctrl configuration to load + */ +-void stm32_pinctrl_store_standby_cfg(struct stm32_pinctrl *pinctrl, size_t cnt); ++void stm32_pinctrl_load_config(struct stm32_pinctrl_list *list); + + /* +- * Save pinctrl instances defined in DT node: identifiers and power states ++ * Get a pinctrl configuration reference from an indexed DT pinctrl property + * + * @fdt: device tree + * @node: device node in the device tree +- * @pinctrl: NULL or pointer to array of struct stm32_pinctrl +- * @count: number of elements pointed by argument cfg +- * +- * Return the number of pinctrl instances found or a negative value on error. +- * +- * When @count is 0, @pinctrl may be NULL. The function will return only the +- * number of pinctrl instances found in the device tree for the target +- * device node. ++ * @index: Index of the pinctrl property ++ * @list: Output pinctrl list reference + * +- * If more instances than @count are found then the function returns the +- * effective number of pincltr instance found in the node but fills +- * output array @pinctrl only for the input @count first entries. ++ * Return a TEE_Result compliant code + */ +-int stm32_pinctrl_fdt_get_pinctrl(void *fdt, int node, +- struct stm32_pinctrl *pinctrl, size_t count); ++TEE_Result stm32_pinctrl_dt_get_by_index(const void *fdt, int nodeoffset, ++ unsigned int index, ++ struct stm32_pinctrl_list **list); + + /* +- * Set target output GPIO pin to high or low level ++ * Get a pinctrl configuration reference from a named DT pinctrl property + * +- * @bank: GPIO bank identifier as assigned by the platform +- * @pin: GPIO pin position in the GPIO bank +- * @high: 1 to set GPIO to high level, 0 to set to GPIO low level +- */ +-void stm32_gpio_set_output_level(unsigned int bank, unsigned int pin, int high); +- +-/* +- * Set output GPIO pin referenced by @pinctrl to high or low level ++ * @fdt: device tree ++ * @node: device node in the device tree ++ * @name: Name of the pinctrl ++ * @list: Output pinctrl list reference + * +- * @pinctrl: Reference to pinctrl +- * @high: 1 to set GPIO to high level, 0 to set to GPIO low level ++ * Return a TEE_Result compliant code + */ +-static inline void stm32_pinctrl_set_gpio_level(struct stm32_pinctrl *pinctrl, +- int high) +-{ +- stm32_gpio_set_output_level(pinctrl->bank, pinctrl->pin, high); +-} ++TEE_Result stm32_pinctrl_dt_get_by_name(const void *fdt, int nodeoffset, ++ const char *name, ++ struct stm32_pinctrl_list **plist); + + /* +- * Get input GPIO pin current level, high or low ++ * Get the gpio_ops handle for a GPIO + * +- * @bank: GPIO bank identifier as assigned by the platform +- * @pin: GPIO pin position in the GPIO bank +- * Return 1 if GPIO level is high, 0 if it is low ++ * Return the generic gpio_ops for GPIOS + */ +-int stm32_gpio_get_input_level(unsigned int bank, unsigned int pin); +- +-/* +- * Set target output GPIO pin to high or low level +- * +- * @pinctrl: Reference to pinctrl +- * Return 1 if GPIO level is high, 0 if it is low +- */ +-static inline int stm32_pinctrl_get_gpio_level(struct stm32_pinctrl *pinctrl) +-{ +- return stm32_gpio_get_input_level(pinctrl->bank, pinctrl->pin); +-} ++const struct gpio_ops *stm32_gpio_get_ops(void); + + #ifdef CFG_STM32_GPIO + /* +@@ -159,26 +137,35 @@ static inline int stm32_pinctrl_get_gpio_level(struct stm32_pinctrl *pinctrl) + * @bank: GPIO bank identifier as assigned by the platform + * @pin: Pin number in the GPIO bank + * @secure: True if pin is secure, false otherwise ++ * ++ * Return a TEE_Resutl compliant code + */ +-void stm32_gpio_set_secure_cfg(unsigned int bank, unsigned int pin, +- bool secure); +-#else +-static inline void stm32_gpio_set_secure_cfg(unsigned int bank __unused, +- unsigned int pin __unused, +- bool secure __unused) +-{ +- assert(0); +-} +-#endif ++TEE_Result stm32_gpio_set_secure_cfg(unsigned int bank, unsigned int pin, ++ bool secure); + + /* +- * Get the number of GPIO pins supported by a target GPIO bank ++ * Configure pin muxing access permission: can be secure or not ++ * ++ * @list: Pinctrl list reference ++ * @secure: True if referenced pins are secure, false otherwise + * +- * @fdt: device tree reference +- * @pinctrl_node: pinctrl node which GPIO bank node belongs to +- * @bank: target GPIO bank ID +- * Return number of GPIO pins (>= 0) or a negative value on error ++ * Return a TEE_Resutl compliant code + */ +-int stm32_get_gpio_count(void *fdt, int pinctrl_node, unsigned int bank); ++TEE_Result stm32_pinctrl_set_secure_cfg(struct stm32_pinctrl_list *list, ++ bool secure); ++#else ++static inline TEE_Result stm32_gpio_set_secure_cfg(unsigned int bank __unused, ++ unsigned int pin __unused, ++ bool secure __unused) ++{ ++ return TEE_ERROR_NOT_SUPPORTED; ++} + ++static inline ++TEE_Result stm32_pinctrl_set_secure_cfg(struct stm32_pinctrl_list *l __unused, ++ bool secure __unused) ++{ ++ return TEE_ERROR_NOT_SUPPORTED; ++} ++#endif + #endif /*__STM32_GPIO_H*/ +diff --git a/core/include/drivers/stm32_i2c.h b/core/include/drivers/stm32_i2c.h +index 39b324466..cbc503f67 100644 +--- a/core/include/drivers/stm32_i2c.h ++++ b/core/include/drivers/stm32_i2c.h +@@ -1,6 +1,6 @@ + /* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */ + /* +- * Copyright (c) 2017-2019, STMicroelectronics ++ * Copyright (c) 2017-2022, STMicroelectronics + */ + + #ifndef __STM32_I2C_H +@@ -9,6 +9,7 @@ + #include + #include + #include ++#include + #include + #include + #include +@@ -47,7 +48,7 @@ + * @analog_filter: True if enabling analog filter + * @digital_filter_coef: filter coef (below STM32_I2C_DIGITAL_FILTER_MAX) + */ +-struct stm32_i2c_init_s { ++struct stm32_i2c_platform_data { + unsigned int dt_status; + paddr_t pbase; + size_t reg_size; +@@ -67,8 +68,8 @@ struct stm32_i2c_init_s { + }; + + enum i2c_state_e { +- I2C_STATE_RESET, /* Not yet initialized */ +- I2C_STATE_READY, /* Ready for use */ ++ I2C_STATE_RESET, /* Not yet initialized */ ++ I2C_STATE_READY, /* Ready for use */ + I2C_STATE_BUSY, /* Internal process ongoing */ + I2C_STATE_BUSY_TX, /* Data Transmission ongoing */ + I2C_STATE_BUSY_RX, /* Data Reception ongoing */ +@@ -77,7 +78,7 @@ enum i2c_state_e { + + enum i2c_mode_e { + I2C_MODE_NONE, /* No active communication */ +- I2C_MODE_MASTER, /* Communication in Master Mode */ ++ I2C_MODE_MASTER, /* Communication in Master Mode */ + I2C_MODE_SLAVE, /* Communication in Slave Mode */ + I2C_MODE_MEM, /* Communication in Memory Mode */ + }; +@@ -111,8 +112,7 @@ struct i2c_cfg { + * @saved_timing: Saved timing value if already computed + * @saved_frequency: Saved frequency value if already computed + * @sec_cfg: I2C registers configuration storage +- * @pinctrl: PINCTRLs configuration for the I2C PINs +- * @pinctrl_count: Number of PINCTRLs elements ++ * @pinctrl: Reference to PINCTRLs + */ + struct i2c_handle_s { + struct io_pa_va base; +@@ -124,8 +124,8 @@ struct i2c_handle_s { + uint32_t saved_timing; + unsigned long saved_frequency; + struct i2c_cfg sec_cfg; +- struct stm32_pinctrl *pinctrl; +- size_t pinctrl_count; ++ struct stm32_pinctrl_list *pinctrl_list; ++ bool secure_i2c; + }; + + /* STM32 specific defines */ +@@ -136,29 +136,27 @@ struct i2c_handle_s { + #define STM32_I2C_DIGITAL_FILTER_MAX U(16) + + /* +- * Fill struct stm32_i2c_init_s from DT content for a given I2C node ++ * Fill struct stm32_i2c_platform_data from DT content for a given I2C node + * + * @fdt: Reference to DT + * @node: Target I2C node in the DT +- * @init: Output stm32_i2c_init_s structure +- * @pinctrl: Reference to output pinctrl array +- * @pinctrl_count: Input @pinctrl array size, output expected size upon success ++ * @init: Output stm32_i2c_platform_data structure ++ * @pinctrl: Reference to output pinctrl list + * Return a TEE_Result compliant value + */ +-TEE_Result stm32_i2c_get_setup_from_fdt(void *fdt, int node, +- struct stm32_i2c_init_s *init, +- struct stm32_pinctrl **pinctrl, +- size_t *pinctrl_count); ++TEE_Result stm32_i2c_get_setup_from_fdt(const void *fdt, int node, ++ struct stm32_i2c_platform_data *init, ++ struct stm32_pinctrl_list **pinctrl); + + /* + * Initialize I2C bus handle from input configuration directives + * + * @hi2c: Reference to I2C bus handle structure +- * @init_data: Input stm32_i2c_init_s structure ++ * @init_data: Input stm32_i2c_platform_data structure + * Return 0 on success else a negative value + */ + int stm32_i2c_init(struct i2c_handle_s *hi2c, +- struct stm32_i2c_init_s *init_data); ++ struct stm32_i2c_platform_data *init_data); + + /* + * Send a memory write request in the I2C bus +@@ -265,4 +263,56 @@ static inline bool i2c_is_secure(struct i2c_handle_s *hi2c) + return hi2c->dt_status == DT_STATUS_OK_SEC; + } + ++/* ++ * i2c_get_handle_by_node - Fills the child i2c handle structure ++ * with an i2c handle structure given by the i2c provider, ++ * linked to the parameter node ++ * @subnode the node of the caller (i2c child node) ++ * @i2c_handle the pointer to the struct to fill ++ * ++ * Returns TEE_SUCCESS in case of success ++ * TEE_ERROR_DEFER_DRIVER_INIT if reset controller is not initialized ++ * Any TEE_Result compliant code in case of error. ++ */ ++ ++TEE_Result i2c_dt_get_by_subnode(const void *fdt, int subnode, ++ struct i2c_handle_s **i2c_handle); ++ ++/* ++ * i2c_dt_get_func - Typedef of function to get i2c from ++ * devicetree properties ++ * ++ * @a: Pointer to devicetree description of the i2c to parse ++ * @data: Pointer to data given at i2c_dt_register_provider() call ++ * @res: Output result code of the operation: ++ * TEE_SUCCESS in case of success ++ * TEE_ERROR_DEFER_DRIVER_INIT if reset controller is not initialized ++ * Any TEE_Result compliant code in case of error. ++ * ++ * Returns a struct i2c pointer pointing to an i2c matching ++ * the devicetree description or NULL if invalid description in which case ++ * @res provides the error code. ++ */ ++typedef struct i2c_handle_s *(*i2c_dt_get_func) ++ (struct dt_driver_phandle_args *a, void *data, TEE_Result *res); ++ ++/* ++ * i2c_dt_register_provider - Register an i2c provider ++ * ++ * @fdt: Device tree to work on ++ * @nodeoffset: Node offset of the reset controller ++ * @get_dt_i2c: Callback to match the reset controller with a struct i2c ++ * @data: Data which will be passed to the get_dt_i2c callback ++ * Returns TEE_Result value ++ */ ++static inline ++TEE_Result i2c_register_provider(const void *fdt, int nodeoffset, ++ i2c_dt_get_func get_dt_i2c, ++ void *data) ++{ ++ return dt_driver_register_provider(fdt, nodeoffset, ++ (get_of_device_func)get_dt_i2c, ++ data, DT_DRIVER_I2C); ++} ++ + #endif /* __STM32_I2C_H */ +diff --git a/core/include/drivers/stm32_iwdg.h b/core/include/drivers/stm32_iwdg.h +new file mode 100644 +index 000000000..8140165f6 +--- /dev/null ++++ b/core/include/drivers/stm32_iwdg.h +@@ -0,0 +1,33 @@ ++/* SPDX-License-Identifier: BSD-3-Clause */ ++/* ++ * Copyright (c) 2018-2021, STMicroelectronics - All Rights Reserved ++ */ ++ ++#ifndef __STM32_IWDG_H__ ++#define __STM32_IWDG_H__ ++ ++#include ++#include ++#include ++#include ++ ++/* Values for struct stm32_iwdg_platdata::flags */ ++#define IWDG_HW_ENABLED BIT(0) ++#define IWDG_DISABLE_ON_STOP BIT(1) ++#define IWDG_DISABLE_ON_STANDBY BIT(2) ++ ++struct stm32_iwdg_platdata { ++ struct io_pa_va base; ++ struct clk *clock; ++ struct clk *clk_lsi; ++ int irq; ++ uint8_t flags; ++ int timeout; ++ int sec_timeout; ++}; ++ ++__weak int stm32_iwdg_get_platdata(struct stm32_iwdg_platdata *pdata); ++ ++void stm32_iwdg_refresh(void); ++ ++#endif /*__STM32_IWDG_H__*/ +diff --git a/core/include/drivers/stm32_lptimer.h b/core/include/drivers/stm32_lptimer.h +new file mode 100644 +index 000000000..b7a12261a +--- /dev/null ++++ b/core/include/drivers/stm32_lptimer.h +@@ -0,0 +1,48 @@ ++/* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */ ++/* ++ * Copyright (c) 2020-2022, STMicroelectronics ++ */ ++#ifndef __STM32_LPTIMER_H ++#define __STM32_LPTIMER_H ++ ++#ifndef TFM_ENV ++/* optee */ ++#include ++#endif ++ ++struct lptimer_ext_input_cfg { ++ /* 2 input mux could be available */ ++ uint8_t mux[2]; ++ uint8_t polarity; ++ uint8_t num; ++}; ++ ++struct lptimer_ext_trigger_cfg { ++ uint8_t mux; ++ uint8_t polarity; ++ uint8_t num; ++}; ++ ++struct lptimer_driver_data { ++ uint32_t version; ++ uint8_t nb_ext_input; ++ uint8_t nb_ext_trigger; ++ uint8_t encoder:1; ++}; ++ ++#define LPTIMER_MODE_COUNTER BIT(0) ++ ++struct stm32_lptimer_platdata { ++ const char *name; ++ int phandle; ++ uintptr_t base; ++ struct clk *clock; ++ int irq; ++ struct lptimer_ext_trigger_cfg ext_trigger; ++ struct lptimer_ext_input_cfg ext_input; ++ uint8_t mode; ++}; ++ ++TEE_Result stm32_lptimer_get_platdata(struct stm32_lptimer_platdata *pdata); ++int stm32_lptimer_init(void); ++#endif /* __STM32_LPTIMER_H */ +diff --git a/core/include/drivers/stm32_rng.h b/core/include/drivers/stm32_rng.h +index f2be3cf7a..83e08aa08 100644 +--- a/core/include/drivers/stm32_rng.h ++++ b/core/include/drivers/stm32_rng.h +@@ -1,36 +1,36 @@ + /* SPDX-License-Identifier: BSD-3-Clause */ + /* +- * Copyright (c) 2018-2019, STMicroelectronics ++ * Copyright (c) 2018-2021, STMicroelectronics + */ + + #ifndef __STM32_RNG_H__ + #define __STM32_RNG_H__ + ++#include ++#include + #include + #include + #include + #include + ++struct stm32_rng_platdata { ++ struct io_pa_va base; ++ struct clk *clock; ++ unsigned long reset; ++ unsigned int lock; ++ bool clock_error; ++}; ++ ++#ifdef CFG_WITH_SOFTWARE_PRNG + /* + * Fill buffer with bytes from the STM32_RNG + * @out: Output buffer + * @size: Byte size of the output buffer + * Return a TEE_Result compliant sttus + */ +-TEE_Result stm32_rng_read(uint8_t *out, size_t size); +- +-/* +- * As stm32_rng_read() but excluding clocks/reset dependencies. +- * +- * @rng_base: Caller provides the RNG interface base address +- * @out: Output buffer +- * @size: Pointer to input/output byte size of the output buffer +- * Return a TEE_Result compliant sttus +- * +- * When successfully returning, @size stores the number of bytes +- * effectively generated in the output buffer @out. The input value +- * of @size gives the size available in buffer @out. +- */ +-TEE_Result stm32_rng_read_raw(vaddr_t rng_base, uint8_t *out, size_t *size); ++TEE_Result stm32_rng_read(void *buf, size_t blen); ++#endif + ++/* Set RNG platform data the driver shall initialize with */ ++TEE_Result stm32_rng_get_platdata(struct stm32_rng_platdata *pdata); + #endif /*__STM32_RNG_H__*/ +diff --git a/core/include/drivers/stm32_rtc.h b/core/include/drivers/stm32_rtc.h +new file mode 100644 +index 000000000..ab1887e0b +--- /dev/null ++++ b/core/include/drivers/stm32_rtc.h +@@ -0,0 +1,60 @@ ++/* SPDX-License-Identifier: BSD-3-Clause */ ++/* ++ * Copyright (c) 2017-2018, STMicroelectronics - All Rights Reserved ++ * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved. ++ */ ++ ++#ifndef __PLAT_RTC_H__ ++#define __PLAT_RTC_H__ ++ ++#include ++ ++struct stm32_rtc_calendar { ++ uint32_t ssr; ++ uint32_t tr; ++ uint32_t dr; ++}; ++ ++enum months { ++ JANUARY = 1, ++ FEBRUARY, ++ MARCH, ++ APRIL, ++ MAY, ++ JUNE, ++ JULY, ++ AUGUST, ++ SEPTEMBER, ++ OCTOBER, ++ NOVEMBER, ++ DECEMBER, ++ NB_MONTHS = 12 ++}; ++ ++struct stm32_rtc_time { ++ uint32_t hour; ++ uint32_t min; ++ uint32_t sec; ++ uint32_t wday; ++ uint32_t day; ++ enum months month; ++ uint32_t year; ++}; ++ ++/* Get calendar formatted time from RTC device */ ++void stm32_rtc_get_calendar(struct stm32_rtc_calendar *calendar); ++ ++/* Return time diff in milliseconds between current and reference time */ ++unsigned long long stm32_rtc_diff_calendar(struct stm32_rtc_calendar *cur, ++ struct stm32_rtc_calendar *ref); ++ ++/* Enable tamper and secure timestamp access in RTC */ ++void stm32_rtc_set_tamper_timestamp(void); ++ ++/* Return true if and only if RTC timestamp is enable */ ++bool stm32_rtc_is_timestamp_enable(void); ++ ++/* Get RTC timestamp for current time */ ++void stm32_rtc_get_timestamp(struct stm32_rtc_time *tamp_ts); ++ ++#endif /* __PLAT_RTC_H__ */ +diff --git a/core/include/drivers/stm32_tamp.h b/core/include/drivers/stm32_tamp.h +new file mode 100644 +index 000000000..8f52492d0 +--- /dev/null ++++ b/core/include/drivers/stm32_tamp.h +@@ -0,0 +1,207 @@ ++/* SPDX-License-Identifier: BSD-3-Clause */ ++/* ++ * Copyright (c) 2021, STMicroelectronics ++ */ ++ ++#ifndef __STM32_TAMP_H__ ++#define __STM32_TAMP_H__ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++ ++/* Tamper ident */ ++enum stm32_tamp_id { ++ INT_TAMP1 = 0, ++ INT_TAMP2, ++ INT_TAMP3, ++ INT_TAMP4, ++ INT_TAMP5, ++ INT_TAMP6, ++ INT_TAMP7, ++ INT_TAMP8, ++ INT_TAMP9, ++ INT_TAMP10, ++ INT_TAMP11, ++ INT_TAMP12, ++ INT_TAMP13, ++ INT_TAMP14, ++ INT_TAMP15, ++ INT_TAMP16, ++ ++ EXT_TAMP1, ++ EXT_TAMP2, ++ EXT_TAMP3, ++ EXT_TAMP4, ++ EXT_TAMP5, ++ EXT_TAMP6, ++ EXT_TAMP7, ++ EXT_TAMP8, ++ ++ LAST_TAMP, ++ INVALID_TAMP = 0xFFFF, ++}; ++ ++/* Callback return bitmask values */ ++#define TAMP_CB_ACK BIT(0) ++#define TAMP_CB_RESET BIT(1) ++#define TAMP_CB_ACK_AND_RESET (TAMP_CB_RESET | TAMP_CB_ACK) ++ ++/* ++ * Define number of backup registers in zone 1 and zone 2 (remaining are in ++ * zone 3) ++ * ++ * backup registers in zone 1: read/write only in secure mode ++ * zone 2: write only in secure mode, read in secure ++ * and non-secure mode ++ * zone 3: read/write in secure and non-secure mode ++ * ++ * Protection zone 1 if nb_zone1_regs == 0 no backup register are in zone 1 ++ * else backup registers from TAMP_BKP0R to TAMP_BKPxR ++ * with x = nb_zone1_regs - 1 are in zone 1. ++ * Protection zone 2 if nb_zone2_regs == 0 no backup register are in zone 2 ++ * else backup registers from ++ * TAMP_BKPyR with y = nb_zone1_regs ++ * to ++ * TAMP_BKPzR with z = (nb_zone1_regs1 + nb_zone2_regs - 1) ++ * are in zone 2. ++ * Protection zone 3 backup registers from TAMP_BKPtR ++ * with t = nb_zone1_regs1 + nb_zone2_regs to last backup ++ * register are in zone 3. ++ */ ++struct stm32_bkpregs_conf { ++ uint32_t nb_zone1_regs; ++ uint32_t nb_zone2_regs; ++}; ++ ++/* Define TAMPER modes */ ++#define TAMP_ERASE 0x0U ++#define TAMP_NOERASE BIT(1) ++#define TAMP_NO_EVT_MASK 0x0U ++#define TAMP_EVT_MASK BIT(2) ++#define TAMP_MODE_MASK GENMASK_32(15, 0) ++ ++/* ++ * stm32_tamp_write_mcounter: Increment monotonic counter[counter_idx]. ++ */ ++TEE_Result stm32_tamp_write_mcounter(int counter_idx); ++uint32_t stm32_tamp_read_mcounter(int counter_idx); ++ ++bool stm32_tamp_are_secrets_blocked(void); ++void stm32_tamp_block_secrets(void); ++void stm32_tamp_unblock_secrets(void); ++void stm32_tamp_erase_secrets(void); ++void stm32_tamp_lock_boot_hardware_key(void); ++ ++/* ++ * stm32_tamp_activate_tamp: Configure and activate one tamper (internal or ++ * external). ++ * ++ * id: tamper id ++ * mode: bitmask from TAMPER modes define: ++ * TAMP_ERASE/TAMP_NOERASE: ++ * TAMP_ERASE: when this tamp event raises secret will be erased. ++ * TAMP_NOERASE: when this event raises secured IPs will be locked ++ * until the acknowledge. If the callback confirms the TAMPER, it ++ * can manually erase secrets with stm32_tamp_erase_secrets(). ++ * TAMP_NO_EVT_MASK/TAMP_EVT_MASK: ++ * TAMP_NO_EVT_MASK: normal behavior. ++ * TAMP_EVT_MASK: if the event is triggered, the event is masked and ++ * internally cleared by hardware. Secrets are not erased. Only ++ * applicable for EXT_TAMP1,2,3. This defines only the status at ++ * boot. To change mask while runtime stm32_tamp_set_mask() and ++ * stm32_tamp_unset_mask can be used. ++ * callback: function to call when tamper is raised (cannot be NULL), ++ * called in interrupt context, ++ * Callback function returns a bitmask defining the action to take by ++ * the driver: ++ * TAMP_CB_RESET: will reset the board. ++ * TAMP_CB_ACK: this specific tamp is acknowledged (in case ++ * of no-erase tamper, blocked secret are unblocked). ++ * ++ * return: TEE_ERROR_BAD_PARAMETERS: ++ * if 'id' is not a valid tamp id, ++ * if callback is NULL, ++ * if TAMP_EVT_MASK mode is set for a non supported 'id'. ++ * TEE_ERROR BAD_STATE ++ * if driver wasn't previously initialized. ++ * TEE_ERROR ITEM_NOT_FOUND ++ * if the activated external tamper wasn't previously ++ * defined in the device tree. ++ * else TEE_SUCCESS. ++ */ ++TEE_Result stm32_tamp_activate_tamp(enum stm32_tamp_id id, uint32_t mode, ++ uint32_t (*callback)(int id)); ++ ++TEE_Result stm32_tamp_set_mask(enum stm32_tamp_id id); ++TEE_Result stm32_tamp_unset_mask(enum stm32_tamp_id id); ++ ++/* ++ * stm32_tamp_set_secure_bkprwregs: Configure backup registers zone. ++ * registers in zone 1: read/write only in secure mode ++ * zone 2: write only in secure mode, read in secure and ++ * non-secure mode ++ * zone 3: read/write in secure and non-secure mode ++ * ++ * bkpregs_conf: a pointer to struct bkpregs_conf that define the number of ++ * registers in zone 1 and zone 2 (remaining backup registers will be in ++ * zone 3). ++ * ++ * return TEE_ERROR_NOT_SUPPORTED: if zone 1 and/or zone 2 definition are out ++ * of range. ++ * TEE_ERROR_BAD_PARAMETERS: if bkpregs_cond is NULL. ++ * TEE_SUCCESS : if OK. ++ */ ++TEE_Result stm32_tamp_set_secure_bkpregs(struct stm32_bkpregs_conf ++ *bkpregs_conf); ++ ++/* ++ * stm32_tamp_set_config: Apply configuration. ++ * Default one if no previous call to any of: ++ * stm32_tamp_configure_passive() ++ * stm32_tamp_configure_active() ++ * stm32_tamp_configure_internal() ++ * stm32_tamp_configure_external() ++ * stm32_tamp_configure_secret_list() ++ * ++ */ ++TEE_Result stm32_tamp_set_config(void); ++ ++/* Compatibility tags */ ++#define TAMP_HAS_REGISTER_SECCFG BIT(0) ++#define TAMP_HAS_REGISTER_PRIVCFGR BIT(1) ++#define TAMP_HAS_REGISTER_ERCFGR BIT(2) ++#define TAMP_HAS_REGISTER_ATCR2 BIT(3) ++#define TAMP_HAS_REGISTER_CR3 BIT(4) ++#define TAMP_HAS_CR2_SECRET_STATUS BIT(5) ++#define TAMP_SIZE_ATCR1_ATCKSEL_IS_4 BIT(7) ++ ++struct stm32_tamp_compat { ++ int nb_monotonic_counter; ++ uint32_t tags; ++ struct stm32_tamp_conf *int_tamp; ++ uint32_t int_tamp_size; ++ struct stm32_tamp_conf *ext_tamp; ++ uint32_t ext_tamp_size; ++ const struct stm32_tamp_pin_map *pin_map; ++ uint32_t pin_map_size; ++}; ++ ++struct stm32_tamp_platdata { ++ struct io_pa_va base; ++ struct clk *clock; ++ int it; ++ uint32_t passive_conf; ++ uint32_t active_conf; ++ uint32_t pins_conf; ++ uint32_t out_pins; ++ bool is_wakeup_source; ++ struct stm32_tamp_compat *compat; ++}; ++ ++TEE_Result stm32_tamp_get_platdata(struct stm32_tamp_platdata *pdata); ++ ++#endif /* __STM32_TAMP_H__ */ +diff --git a/core/include/drivers/stm32_tim.h b/core/include/drivers/stm32_tim.h +new file mode 100644 +index 000000000..3b057b716 +--- /dev/null ++++ b/core/include/drivers/stm32_tim.h +@@ -0,0 +1,19 @@ ++/* SPDX-License-Identifier: BSD-3-Clause */ ++/* ++ * Copyright (c) 2018-2022, STMicroelectronics ++ */ ++ ++#ifndef __STM32_TIM_H ++#define __STM32_TIM_H ++ ++struct stm32_timer_platdata { ++ const char *name; ++ int phandle; ++ uintptr_t base; ++ struct clk *clock; ++ int irq; ++ uint8_t mode; ++}; ++ ++TEE_Result stm32_timer_get_platdata(struct stm32_timer_platdata *pdata __unused); ++#endif /* __STM32_TIM_H */ +diff --git a/core/include/drivers/stm32_uart.h b/core/include/drivers/stm32_uart.h +index 021b2b9d8..d82781c8a 100644 +--- a/core/include/drivers/stm32_uart.h ++++ b/core/include/drivers/stm32_uart.h +@@ -15,8 +15,7 @@ struct stm32_uart_pdata { + struct serial_chip chip; + bool secure; + struct clk *clock; +- struct stm32_pinctrl *pinctrl; +- size_t pinctrl_count; ++ struct stm32_pinctrl_list *pinctrl; + }; + + /* +diff --git a/core/include/drivers/stm32mp13_rcc.h b/core/include/drivers/stm32mp13_rcc.h +new file mode 100644 +index 000000000..892890f70 +--- /dev/null ++++ b/core/include/drivers/stm32mp13_rcc.h +@@ -0,0 +1,1879 @@ ++/* SPDX-License-Identifier: BSD-3-Clause */ ++/* ++ * Copyright (c) 2021, STMicroelectronics - All Rights Reserved ++ */ ++ ++#ifndef __DRIVERS_STM32MP13_RCC_H__ ++#define __DRIVERS_STM32MP13_RCC_H__ ++ ++#define RCC_SECCFGR U(0X0) ++#define RCC_MP_SREQSETR U(0X100) ++#define RCC_MP_SREQCLRR U(0X104) ++#define RCC_MP_APRSTCR U(0X108) ++#define RCC_MP_APRSTSR U(0X10C) ++#define RCC_PWRLPDLYCR U(0X110) ++#define RCC_MP_GRSTCSETR U(0X114) ++#define RCC_BR_RSTSCLRR U(0X118) ++#define RCC_MP_RSTSSETR U(0X11C) ++#define RCC_MP_RSTSCLRR U(0X120) ++#define RCC_MP_IWDGFZSETR U(0X124) ++#define RCC_MP_IWDGFZCLRR U(0X128) ++#define RCC_MP_CIER U(0X200) ++#define RCC_MP_CIFR U(0X204) ++#define RCC_BDCR U(0X400) ++#define RCC_RDLSICR U(0X404) ++#define RCC_OCENSETR U(0X420) ++#define RCC_OCENCLRR U(0X424) ++#define RCC_OCRDYR U(0X428) ++#define RCC_HSICFGR U(0X440) ++#define RCC_CSICFGR U(0X444) ++#define RCC_MCO1CFGR U(0X460) ++#define RCC_MCO2CFGR U(0X464) ++#define RCC_DBGCFGR U(0X468) ++#define RCC_RCK12SELR U(0X480) ++#define RCC_RCK3SELR U(0X484) ++#define RCC_RCK4SELR U(0X488) ++#define RCC_PLL1CR U(0X4A0) ++#define RCC_PLL1CFGR1 U(0X4A4) ++#define RCC_PLL1CFGR2 U(0X4A8) ++#define RCC_PLL1FRACR U(0X4AC) ++#define RCC_PLL1CSGR U(0X4B0) ++#define RCC_PLL2CR U(0X4D0) ++#define RCC_PLL2CFGR1 U(0X4D4) ++#define RCC_PLL2CFGR2 U(0X4D8) ++#define RCC_PLL2FRACR U(0X4DC) ++#define RCC_PLL2CSGR U(0X4E0) ++#define RCC_PLL3CR U(0X500) ++#define RCC_PLL3CFGR1 U(0X504) ++#define RCC_PLL3CFGR2 U(0X508) ++#define RCC_PLL3FRACR U(0X50C) ++#define RCC_PLL3CSGR U(0X510) ++#define RCC_PLL4CR U(0X520) ++#define RCC_PLL4CFGR1 U(0X524) ++#define RCC_PLL4CFGR2 U(0X528) ++#define RCC_PLL4FRACR U(0X52C) ++#define RCC_PLL4CSGR U(0X530) ++#define RCC_MPCKSELR U(0X540) ++#define RCC_ASSCKSELR U(0X544) ++#define RCC_MSSCKSELR U(0X548) ++#define RCC_CPERCKSELR U(0X54C) ++#define RCC_RTCDIVR U(0X560) ++#define RCC_MPCKDIVR U(0X564) ++#define RCC_AXIDIVR U(0X568) ++#define RCC_MLAHBDIVR U(0X56C) ++#define RCC_APB1DIVR U(0X570) ++#define RCC_APB2DIVR U(0X574) ++#define RCC_APB3DIVR U(0X578) ++#define RCC_APB4DIVR U(0X57C) ++#define RCC_APB5DIVR U(0X580) ++#define RCC_APB6DIVR U(0X584) ++#define RCC_TIMG1PRER U(0X5A0) ++#define RCC_TIMG2PRER U(0X5A4) ++#define RCC_TIMG3PRER U(0X5A8) ++#define RCC_DDRITFCR U(0X5C0) ++#define RCC_I2C12CKSELR U(0X600) ++#define RCC_I2C345CKSELR U(0X604) ++#define RCC_SPI2S1CKSELR U(0X608) ++#define RCC_SPI2S23CKSELR U(0X60C) ++#define RCC_SPI45CKSELR U(0X610) ++#define RCC_UART12CKSELR U(0X614) ++#define RCC_UART35CKSELR U(0X618) ++#define RCC_UART4CKSELR U(0X61C) ++#define RCC_UART6CKSELR U(0X620) ++#define RCC_UART78CKSELR U(0X624) ++#define RCC_LPTIM1CKSELR U(0X628) ++#define RCC_LPTIM23CKSELR U(0X62C) ++#define RCC_LPTIM45CKSELR U(0X630) ++#define RCC_SAI1CKSELR U(0X634) ++#define RCC_SAI2CKSELR U(0X638) ++#define RCC_FDCANCKSELR U(0X63C) ++#define RCC_SPDIFCKSELR U(0X640) ++#define RCC_ADC12CKSELR U(0X644) ++#define RCC_SDMMC12CKSELR U(0X648) ++#define RCC_ETH12CKSELR U(0X64C) ++#define RCC_USBCKSELR U(0X650) ++#define RCC_QSPICKSELR U(0X654) ++#define RCC_FMCCKSELR U(0X658) ++#define RCC_RNG1CKSELR U(0X65C) ++#define RCC_STGENCKSELR U(0X660) ++#define RCC_DCMIPPCKSELR U(0X664) ++#define RCC_SAESCKSELR U(0X668) ++#define RCC_APB1RSTSETR U(0X6A0) ++#define RCC_APB1RSTCLRR U(0X6A4) ++#define RCC_APB2RSTSETR U(0X6A8) ++#define RCC_APB2RSTCLRR U(0X6AC) ++#define RCC_APB3RSTSETR U(0X6B0) ++#define RCC_APB3RSTCLRR U(0X6B4) ++#define RCC_APB4RSTSETR U(0X6B8) ++#define RCC_APB4RSTCLRR U(0X6BC) ++#define RCC_APB5RSTSETR U(0X6C0) ++#define RCC_APB5RSTCLRR U(0X6C4) ++#define RCC_APB6RSTSETR U(0X6C8) ++#define RCC_APB6RSTCLRR U(0X6CC) ++#define RCC_AHB2RSTSETR U(0X6D0) ++#define RCC_AHB2RSTCLRR U(0X6D4) ++#define RCC_AHB4RSTSETR U(0X6E0) ++#define RCC_AHB4RSTCLRR U(0X6E4) ++#define RCC_AHB5RSTSETR U(0X6E8) ++#define RCC_AHB5RSTCLRR U(0X6EC) ++#define RCC_AHB6RSTSETR U(0X6F0) ++#define RCC_AHB6RSTCLRR U(0X6F4) ++#define RCC_MP_APB1ENSETR U(0X700) ++#define RCC_MP_APB1ENCLRR U(0X704) ++#define RCC_MP_APB2ENSETR U(0X708) ++#define RCC_MP_APB2ENCLRR U(0X70C) ++#define RCC_MP_APB3ENSETR U(0X710) ++#define RCC_MP_APB3ENCLRR U(0X714) ++#define RCC_MP_S_APB3ENSETR U(0X718) ++#define RCC_MP_S_APB3ENCLRR U(0X71C) ++#define RCC_MP_NS_APB3ENSETR U(0X720) ++#define RCC_MP_NS_APB3ENCLRR U(0X724) ++#define RCC_MP_APB4ENSETR U(0X728) ++#define RCC_MP_APB4ENCLRR U(0X72C) ++#define RCC_MP_S_APB4ENSETR U(0X730) ++#define RCC_MP_S_APB4ENCLRR U(0X734) ++#define RCC_MP_NS_APB4ENSETR U(0X738) ++#define RCC_MP_NS_APB4ENCLRR U(0X73C) ++#define RCC_MP_APB5ENSETR U(0X740) ++#define RCC_MP_APB5ENCLRR U(0X744) ++#define RCC_MP_APB6ENSETR U(0X748) ++#define RCC_MP_APB6ENCLRR U(0X74C) ++#define RCC_MP_AHB2ENSETR U(0X750) ++#define RCC_MP_AHB2ENCLRR U(0X754) ++#define RCC_MP_AHB4ENSETR U(0X760) ++#define RCC_MP_AHB4ENCLRR U(0X764) ++#define RCC_MP_S_AHB4ENSETR U(0X768) ++#define RCC_MP_S_AHB4ENCLRR U(0X76C) ++#define RCC_MP_NS_AHB4ENSETR U(0X770) ++#define RCC_MP_NS_AHB4ENCLRR U(0X774) ++#define RCC_MP_AHB5ENSETR U(0X778) ++#define RCC_MP_AHB5ENCLRR U(0X77C) ++#define RCC_MP_AHB6ENSETR U(0X780) ++#define RCC_MP_AHB6ENCLRR U(0X784) ++#define RCC_MP_S_AHB6ENSETR U(0X788) ++#define RCC_MP_S_AHB6ENCLRR U(0X78C) ++#define RCC_MP_NS_AHB6ENSETR U(0X790) ++#define RCC_MP_NS_AHB6ENCLRR U(0X794) ++#define RCC_MP_APB1LPENSETR U(0X800) ++#define RCC_MP_APB1LPENCLRR U(0X804) ++#define RCC_MP_APB2LPENSETR U(0X808) ++#define RCC_MP_APB2LPENCLRR U(0X80C) ++#define RCC_MP_APB3LPENSETR U(0X810) ++#define RCC_MP_APB3LPENCLRR U(0X814) ++#define RCC_MP_S_APB3LPENSETR U(0X818) ++#define RCC_MP_S_APB3LPENCLRR U(0X81C) ++#define RCC_MP_NS_APB3LPENSETR U(0X820) ++#define RCC_MP_NS_APB3LPENCLRR U(0X824) ++#define RCC_MP_APB4LPENSETR U(0X828) ++#define RCC_MP_APB4LPENCLRR U(0X82C) ++#define RCC_MP_S_APB4LPENSETR U(0X830) ++#define RCC_MP_S_APB4LPENCLRR U(0X834) ++#define RCC_MP_NS_APB4LPENSETR U(0X838) ++#define RCC_MP_NS_APB4LPENCLRR U(0X83C) ++#define RCC_MP_APB5LPENSETR U(0X840) ++#define RCC_MP_APB5LPENCLRR U(0X844) ++#define RCC_MP_APB6LPENSETR U(0X848) ++#define RCC_MP_APB6LPENCLRR U(0X84C) ++#define RCC_MP_AHB2LPENSETR U(0X850) ++#define RCC_MP_AHB2LPENCLRR U(0X854) ++#define RCC_MP_AHB4LPENSETR U(0X858) ++#define RCC_MP_AHB4LPENCLRR U(0X85C) ++#define RCC_MP_S_AHB4LPENSETR U(0X868) ++#define RCC_MP_S_AHB4LPENCLRR U(0X86C) ++#define RCC_MP_NS_AHB4LPENSETR U(0X870) ++#define RCC_MP_NS_AHB4LPENCLRR U(0X874) ++#define RCC_MP_AHB5LPENSETR U(0X878) ++#define RCC_MP_AHB5LPENCLRR U(0X87C) ++#define RCC_MP_AHB6LPENSETR U(0X880) ++#define RCC_MP_AHB6LPENCLRR U(0X884) ++#define RCC_MP_S_AHB6LPENSETR U(0X888) ++#define RCC_MP_S_AHB6LPENCLRR U(0X88C) ++#define RCC_MP_NS_AHB6LPENSETR U(0X890) ++#define RCC_MP_NS_AHB6LPENCLRR U(0X894) ++#define RCC_MP_S_AXIMLPENSETR U(0X898) ++#define RCC_MP_S_AXIMLPENCLRR U(0X89C) ++#define RCC_MP_NS_AXIMLPENSETR U(0X8A0) ++#define RCC_MP_NS_AXIMLPENCLRR U(0X8A4) ++#define RCC_MP_MLAHBLPENSETR U(0X8A8) ++#define RCC_MP_MLAHBLPENCLRR U(0X8AC) ++#define RCC_APB3SECSR U(0X8C0) ++#define RCC_APB4SECSR U(0X8C4) ++#define RCC_APB5SECSR U(0X8C8) ++#define RCC_APB6SECSR U(0X8CC) ++#define RCC_AHB2SECSR U(0X8D0) ++#define RCC_AHB4SECSR U(0X8D4) ++#define RCC_AHB5SECSR U(0X8D8) ++#define RCC_AHB6SECSR U(0X8DC) ++#define RCC_VERR U(0XFF4) ++#define RCC_IDR U(0XFF8) ++#define RCC_SIDR U(0XFFC) ++ ++/* RCC_SECCFGR register fields */ ++#define RCC_SECCFGR_HSISEC BIT(0) ++#define RCC_SECCFGR_CSISEC BIT(1) ++#define RCC_SECCFGR_HSESEC BIT(2) ++#define RCC_SECCFGR_LSISEC BIT(3) ++#define RCC_SECCFGR_LSESEC BIT(4) ++#define RCC_SECCFGR_PLL12SEC BIT(8) ++#define RCC_SECCFGR_PLL3SEC BIT(9) ++#define RCC_SECCFGR_PLL4SEC BIT(10) ++#define RCC_SECCFGR_MPUSEC BIT(11) ++#define RCC_SECCFGR_AXISEC BIT(12) ++#define RCC_SECCFGR_MLAHBSEC BIT(13) ++#define RCC_SECCFGR_APB3DIVSEC BIT(16) ++#define RCC_SECCFGR_APB4DIVSEC BIT(17) ++#define RCC_SECCFGR_APB5DIVSEC BIT(18) ++#define RCC_SECCFGR_APB6DIVSEC BIT(19) ++#define RCC_SECCFGR_TIMG3SEC BIT(20) ++#define RCC_SECCFGR_CPERSEC BIT(21) ++#define RCC_SECCFGR_MCO1SEC BIT(22) ++#define RCC_SECCFGR_MCO2SEC BIT(23) ++#define RCC_SECCFGR_STPSEC BIT(24) ++#define RCC_SECCFGR_RSTSEC BIT(25) ++#define RCC_SECCFGR_PWRSEC BIT(31) ++ ++/* RCC_MP_SREQSETR register fields */ ++#define RCC_MP_SREQSETR_STPREQ_P0 BIT(0) ++ ++/* RCC_MP_SREQCLRR register fields */ ++#define RCC_MP_SREQCLRR_STPREQ_P0 BIT(0) ++ ++/* RCC_MP_APRSTCR register fields */ ++#define RCC_MP_APRSTCR_RDCTLEN BIT(0) ++#define RCC_MP_APRSTCR_RSTTO_MASK GENMASK_32(14, 8) ++#define RCC_MP_APRSTCR_RSTTO_SHIFT 8 ++ ++/* RCC_MP_APRSTSR register fields */ ++#define RCC_MP_APRSTSR_RSTTOV_MASK GENMASK_32(14, 8) ++#define RCC_MP_APRSTSR_RSTTOV_SHIFT 8 ++ ++/* RCC_PWRLPDLYCR register fields */ ++#define RCC_PWRLPDLYCR_PWRLP_DLY_MASK GENMASK_32(21, 0) ++#define RCC_PWRLPDLYCR_PWRLP_DLY_SHIFT 0 ++ ++/* RCC_MP_GRSTCSETR register fields */ ++#define RCC_MP_GRSTCSETR_MPSYSRST BIT(0) ++#define RCC_MP_GRSTCSETR_MPUP0RST BIT(4) ++ ++/* RCC_BR_RSTSCLRR register fields */ ++#define RCC_BR_RSTSCLRR_PORRSTF BIT(0) ++#define RCC_BR_RSTSCLRR_BORRSTF BIT(1) ++#define RCC_BR_RSTSCLRR_PADRSTF BIT(2) ++#define RCC_BR_RSTSCLRR_HCSSRSTF BIT(3) ++#define RCC_BR_RSTSCLRR_VCORERSTF BIT(4) ++#define RCC_BR_RSTSCLRR_VCPURSTF BIT(5) ++#define RCC_BR_RSTSCLRR_MPSYSRSTF BIT(6) ++#define RCC_BR_RSTSCLRR_IWDG1RSTF BIT(8) ++#define RCC_BR_RSTSCLRR_IWDG2RSTF BIT(9) ++#define RCC_BR_RSTSCLRR_MPUP0RSTF BIT(13) ++ ++/* RCC_MP_RSTSSETR register fields */ ++#define RCC_MP_RSTSSETR_PORRSTF BIT(0) ++#define RCC_MP_RSTSSETR_BORRSTF BIT(1) ++#define RCC_MP_RSTSSETR_PADRSTF BIT(2) ++#define RCC_MP_RSTSSETR_HCSSRSTF BIT(3) ++#define RCC_MP_RSTSSETR_VCORERSTF BIT(4) ++#define RCC_MP_RSTSSETR_VCPURSTF BIT(5) ++#define RCC_MP_RSTSSETR_MPSYSRSTF BIT(6) ++#define RCC_MP_RSTSSETR_IWDG1RSTF BIT(8) ++#define RCC_MP_RSTSSETR_IWDG2RSTF BIT(9) ++#define RCC_MP_RSTSSETR_STP2RSTF BIT(10) ++#define RCC_MP_RSTSSETR_STDBYRSTF BIT(11) ++#define RCC_MP_RSTSSETR_CSTDBYRSTF BIT(12) ++#define RCC_MP_RSTSSETR_MPUP0RSTF BIT(13) ++#define RCC_MP_RSTSSETR_SPARE BIT(15) ++ ++/* RCC_MP_RSTSCLRR register fields */ ++#define RCC_MP_RSTSCLRR_PORRSTF BIT(0) ++#define RCC_MP_RSTSCLRR_BORRSTF BIT(1) ++#define RCC_MP_RSTSCLRR_PADRSTF BIT(2) ++#define RCC_MP_RSTSCLRR_HCSSRSTF BIT(3) ++#define RCC_MP_RSTSCLRR_VCORERSTF BIT(4) ++#define RCC_MP_RSTSCLRR_VCPURSTF BIT(5) ++#define RCC_MP_RSTSCLRR_MPSYSRSTF BIT(6) ++#define RCC_MP_RSTSCLRR_IWDG1RSTF BIT(8) ++#define RCC_MP_RSTSCLRR_IWDG2RSTF BIT(9) ++#define RCC_MP_RSTSCLRR_STP2RSTF BIT(10) ++#define RCC_MP_RSTSCLRR_STDBYRSTF BIT(11) ++#define RCC_MP_RSTSCLRR_CSTDBYRSTF BIT(12) ++#define RCC_MP_RSTSCLRR_MPUP0RSTF BIT(13) ++#define RCC_MP_RSTSCLRR_SPARE BIT(15) ++ ++/* RCC_MP_IWDGFZSETR register fields */ ++#define RCC_MP_IWDGFZSETR_FZ_IWDG1 BIT(0) ++#define RCC_MP_IWDGFZSETR_FZ_IWDG2 BIT(1) ++ ++/* RCC_MP_IWDGFZCLRR register fields */ ++#define RCC_MP_IWDGFZCLRR_FZ_IWDG1 BIT(0) ++#define RCC_MP_IWDGFZCLRR_FZ_IWDG2 BIT(1) ++ ++/* RCC_MP_CIER register fields */ ++#define RCC_MP_CIER_LSIRDYIE BIT(0) ++#define RCC_MP_CIER_LSERDYIE BIT(1) ++#define RCC_MP_CIER_HSIRDYIE BIT(2) ++#define RCC_MP_CIER_HSERDYIE BIT(3) ++#define RCC_MP_CIER_CSIRDYIE BIT(4) ++#define RCC_MP_CIER_PLL1DYIE BIT(8) ++#define RCC_MP_CIER_PLL2DYIE BIT(9) ++#define RCC_MP_CIER_PLL3DYIE BIT(10) ++#define RCC_MP_CIER_PLL4DYIE BIT(11) ++#define RCC_MP_CIER_LSECSSIE BIT(16) ++#define RCC_MP_CIER_WKUPIE BIT(20) ++ ++/* RCC_MP_CIFR register fields */ ++#define RCC_MP_CIFR_LSIRDYF BIT(0) ++#define RCC_MP_CIFR_LSERDYF BIT(1) ++#define RCC_MP_CIFR_HSIRDYF BIT(2) ++#define RCC_MP_CIFR_HSERDYF BIT(3) ++#define RCC_MP_CIFR_CSIRDYF BIT(4) ++#define RCC_MP_CIFR_PLL1DYF BIT(8) ++#define RCC_MP_CIFR_PLL2DYF BIT(9) ++#define RCC_MP_CIFR_PLL3DYF BIT(10) ++#define RCC_MP_CIFR_PLL4DYF BIT(11) ++#define RCC_MP_CIFR_LSECSSF BIT(16) ++#define RCC_MP_CIFR_WKUPF BIT(20) ++ ++/* RCC_BDCR register fields */ ++#define RCC_BDCR_LSEON BIT(0) ++#define RCC_BDCR_LSEBYP BIT(1) ++#define RCC_BDCR_LSERDY BIT(2) ++#define RCC_BDCR_DIGBYP BIT(3) ++#define RCC_BDCR_LSEDRV_MASK GENMASK_32(5, 4) ++#define RCC_BDCR_LSEDRV_SHIFT 4 ++#define RCC_BDCR_LSECSSON BIT(8) ++#define RCC_BDCR_LSECSSD BIT(9) ++#define RCC_BDCR_RTCSRC_MASK GENMASK_32(17, 16) ++#define RCC_BDCR_RTCSRC_SHIFT 16 ++#define RCC_BDCR_RTCCKEN BIT(20) ++#define RCC_BDCR_VSWRST BIT(31) ++ ++#define RCC_BDCR_LSEBYP_BIT 1 ++#define RCC_BDCR_LSERDY_BIT 2 ++#define RCC_BDCR_DIGBYP_BIT 3 ++#define RCC_BDCR_LSECSSON_BIT 8 ++ ++#define RCC_BDCR_LSEDRV_WIDTH 2 ++ ++/* RCC_RDLSICR register fields */ ++#define RCC_RDLSICR_LSION BIT(0) ++#define RCC_RDLSICR_LSIRDY BIT(1) ++#define RCC_RDLSICR_MRD_MASK GENMASK_32(20, 16) ++#define RCC_RDLSICR_MRD_SHIFT 16 ++#define RCC_RDLSICR_EADLY_MASK GENMASK_32(26, 24) ++#define RCC_RDLSICR_EADLY_SHIFT 24 ++#define RCC_RDLSICR_SPARE_MASK GENMASK_32(31, 27) ++#define RCC_RDLSICR_SPARE_SHIFT 27 ++ ++#define RCC_RDLSICR_LSIRDY_BIT 1 ++ ++/* RCC_OCENSETR register fields */ ++#define RCC_OCENSETR_HSION BIT(0) ++#define RCC_OCENSETR_HSIKERON BIT(1) ++#define RCC_OCENSETR_CSION BIT(4) ++#define RCC_OCENSETR_CSIKERON BIT(5) ++#define RCC_OCENSETR_DIGBYP BIT(7) ++#define RCC_OCENSETR_HSEON BIT(8) ++#define RCC_OCENSETR_HSEKERON BIT(9) ++#define RCC_OCENSETR_HSEBYP BIT(10) ++#define RCC_OCENSETR_HSECSSON BIT(11) ++ ++#define RCC_OCENR_DIGBYP_BIT 7 ++#define RCC_OCENR_HSEBYP_BIT 10 ++#define RCC_OCENR_HSECSSON_BIT 11 ++ ++/* RCC_OCENCLRR register fields */ ++#define RCC_OCENCLRR_HSION BIT(0) ++#define RCC_OCENCLRR_HSIKERON BIT(1) ++#define RCC_OCENCLRR_CSION BIT(4) ++#define RCC_OCENCLRR_CSIKERON BIT(5) ++#define RCC_OCENCLRR_DIGBYP BIT(7) ++#define RCC_OCENCLRR_HSEON BIT(8) ++#define RCC_OCENCLRR_HSEKERON BIT(9) ++#define RCC_OCENCLRR_HSEBYP BIT(10) ++ ++/* RCC_OCRDYR register fields */ ++#define RCC_OCRDYR_HSIRDY BIT(0) ++#define RCC_OCRDYR_HSIDIVRDY BIT(2) ++#define RCC_OCRDYR_CSIRDY BIT(4) ++#define RCC_OCRDYR_HSERDY BIT(8) ++#define RCC_OCRDYR_MPUCKRDY BIT(23) ++#define RCC_OCRDYR_AXICKRDY BIT(24) ++ ++#define RCC_OCRDYR_HSIRDY_BIT 0 ++#define RCC_OCRDYR_HSIDIVRDY_BIT 2 ++#define RCC_OCRDYR_CSIRDY_BIT 4 ++#define RCC_OCRDYR_HSERDY_BIT 8 ++ ++/* RCC_HSICFGR register fields */ ++#define RCC_HSICFGR_HSIDIV_MASK GENMASK_32(1, 0) ++#define RCC_HSICFGR_HSIDIV_SHIFT 0 ++#define RCC_HSICFGR_HSITRIM_MASK GENMASK_32(14, 8) ++#define RCC_HSICFGR_HSITRIM_SHIFT 8 ++#define RCC_HSICFGR_HSICAL_MASK GENMASK_32(27, 16) ++#define RCC_HSICFGR_HSICAL_SHIFT 16 ++ ++/* RCC_CSICFGR register fields */ ++#define RCC_CSICFGR_CSITRIM_MASK GENMASK_32(12, 8) ++#define RCC_CSICFGR_CSITRIM_SHIFT 8 ++#define RCC_CSICFGR_CSICAL_MASK GENMASK_32(23, 16) ++#define RCC_CSICFGR_CSICAL_SHIFT 16 ++ ++/* RCC_MCO1CFGR register fields */ ++#define RCC_MCO1CFGR_MCO1SEL_MASK GENMASK_32(2, 0) ++#define RCC_MCO1CFGR_MCO1SEL_SHIFT 0 ++#define RCC_MCO1CFGR_MCO1DIV_MASK GENMASK_32(7, 4) ++#define RCC_MCO1CFGR_MCO1DIV_SHIFT 4 ++#define RCC_MCO1CFGR_MCO1ON BIT(12) ++ ++/* RCC_MCO2CFGR register fields */ ++#define RCC_MCO2CFGR_MCO2SEL_MASK GENMASK_32(2, 0) ++#define RCC_MCO2CFGR_MCO2SEL_SHIFT 0 ++#define RCC_MCO2CFGR_MCO2DIV_MASK GENMASK_32(7, 4) ++#define RCC_MCO2CFGR_MCO2DIV_SHIFT 4 ++#define RCC_MCO2CFGR_MCO2ON BIT(12) ++ ++/* RCC_DBGCFGR register fields */ ++#define RCC_DBGCFGR_TRACEDIV_MASK GENMASK_32(2, 0) ++#define RCC_DBGCFGR_TRACEDIV_SHIFT 0 ++#define RCC_DBGCFGR_DBGCKEN BIT(8) ++#define RCC_DBGCFGR_TRACECKEN BIT(9) ++#define RCC_DBGCFGR_DBGRST BIT(12) ++ ++/* RCC_RCK12SELR register fields */ ++#define RCC_RCK12SELR_PLL12SRC_MASK GENMASK_32(1, 0) ++#define RCC_RCK12SELR_PLL12SRC_SHIFT 0 ++#define RCC_RCK12SELR_PLL12SRCRDY BIT(31) ++ ++/* RCC_RCK3SELR register fields */ ++#define RCC_RCK3SELR_PLL3SRC_MASK GENMASK_32(1, 0) ++#define RCC_RCK3SELR_PLL3SRC_SHIFT 0 ++#define RCC_RCK3SELR_PLL3SRCRDY BIT(31) ++ ++/* RCC_RCK4SELR register fields */ ++#define RCC_RCK4SELR_PLL4SRC_MASK GENMASK_32(1, 0) ++#define RCC_RCK4SELR_PLL4SRC_SHIFT 0 ++#define RCC_RCK4SELR_PLL4SRCRDY BIT(31) ++ ++/* RCC_PLL1CR register fields */ ++#define RCC_PLL1CR_PLLON BIT(0) ++#define RCC_PLL1CR_PLL1RDY BIT(1) ++#define RCC_PLL1CR_SSCG_CTRL BIT(2) ++#define RCC_PLL1CR_DIVPEN BIT(4) ++#define RCC_PLL1CR_DIVQEN BIT(5) ++#define RCC_PLL1CR_DIVREN BIT(6) ++ ++/* RCC_PLL1CFGR1 register fields */ ++#define RCC_PLL1CFGR1_DIVN_MASK GENMASK_32(8, 0) ++#define RCC_PLL1CFGR1_DIVN_SHIFT 0 ++#define RCC_PLL1CFGR1_DIVM1_MASK GENMASK_32(21, 16) ++#define RCC_PLL1CFGR1_DIVM1_SHIFT 16 ++ ++/* RCC_PLL1CFGR2 register fields */ ++#define RCC_PLL1CFGR2_DIVP_MASK GENMASK_32(6, 0) ++#define RCC_PLL1CFGR2_DIVP_SHIFT 0 ++#define RCC_PLL1CFGR2_DIVQ_MASK GENMASK_32(14, 8) ++#define RCC_PLL1CFGR2_DIVQ_SHIFT 8 ++#define RCC_PLL1CFGR2_DIVR_MASK GENMASK_32(22, 16) ++#define RCC_PLL1CFGR2_DIVR_SHIFT 16 ++ ++/* RCC_PLL1FRACR register fields */ ++#define RCC_PLL1FRACR_FRACV_MASK GENMASK_32(15, 3) ++#define RCC_PLL1FRACR_FRACV_SHIFT 3 ++#define RCC_PLL1FRACR_FRACLE BIT(16) ++ ++/* RCC_PLL1CSGR register fields */ ++#define RCC_PLL1CSGR_MOD_PER_MASK GENMASK_32(12, 0) ++#define RCC_PLL1CSGR_MOD_PER_SHIFT 0 ++#define RCC_PLL1CSGR_TPDFN_DIS BIT(13) ++#define RCC_PLL1CSGR_RPDFN_DIS BIT(14) ++#define RCC_PLL1CSGR_SSCG_MODE BIT(15) ++#define RCC_PLL1CSGR_INC_STEP_MASK GENMASK_32(30, 16) ++#define RCC_PLL1CSGR_INC_STEP_SHIFT 16 ++ ++/* RCC_PLL2CR register fields */ ++#define RCC_PLL2CR_PLLON BIT(0) ++#define RCC_PLL2CR_PLL2RDY BIT(1) ++#define RCC_PLL2CR_SSCG_CTRL BIT(2) ++#define RCC_PLL2CR_DIVPEN BIT(4) ++#define RCC_PLL2CR_DIVQEN BIT(5) ++#define RCC_PLL2CR_DIVREN BIT(6) ++ ++/* RCC_PLL2CFGR1 register fields */ ++#define RCC_PLL2CFGR1_DIVN_MASK GENMASK_32(8, 0) ++#define RCC_PLL2CFGR1_DIVN_SHIFT 0 ++#define RCC_PLL2CFGR1_DIVM2_MASK GENMASK_32(21, 16) ++#define RCC_PLL2CFGR1_DIVM2_SHIFT 16 ++ ++/* RCC_PLL2CFGR2 register fields */ ++#define RCC_PLL2CFGR2_DIVP_MASK GENMASK_32(6, 0) ++#define RCC_PLL2CFGR2_DIVP_SHIFT 0 ++#define RCC_PLL2CFGR2_DIVQ_MASK GENMASK_32(14, 8) ++#define RCC_PLL2CFGR2_DIVQ_SHIFT 8 ++#define RCC_PLL2CFGR2_DIVR_MASK GENMASK_32(22, 16) ++#define RCC_PLL2CFGR2_DIVR_SHIFT 16 ++ ++/* RCC_PLL2FRACR register fields */ ++#define RCC_PLL2FRACR_FRACV_MASK GENMASK_32(15, 3) ++#define RCC_PLL2FRACR_FRACV_SHIFT 3 ++#define RCC_PLL2FRACR_FRACLE BIT(16) ++ ++/* RCC_PLL2CSGR register fields */ ++#define RCC_PLL2CSGR_MOD_PER_MASK GENMASK_32(12, 0) ++#define RCC_PLL2CSGR_MOD_PER_SHIFT 0 ++#define RCC_PLL2CSGR_TPDFN_DIS BIT(13) ++#define RCC_PLL2CSGR_RPDFN_DIS BIT(14) ++#define RCC_PLL2CSGR_SSCG_MODE BIT(15) ++#define RCC_PLL2CSGR_INC_STEP_MASK GENMASK_32(30, 16) ++#define RCC_PLL2CSGR_INC_STEP_SHIFT 16 ++ ++/* RCC_PLL3CR register fields */ ++#define RCC_PLL3CR_PLLON BIT(0) ++#define RCC_PLL3CR_PLL3RDY BIT(1) ++#define RCC_PLL3CR_SSCG_CTRL BIT(2) ++#define RCC_PLL3CR_DIVPEN BIT(4) ++#define RCC_PLL3CR_DIVQEN BIT(5) ++#define RCC_PLL3CR_DIVREN BIT(6) ++ ++/* RCC_PLL3CFGR1 register fields */ ++#define RCC_PLL3CFGR1_DIVN_MASK GENMASK_32(8, 0) ++#define RCC_PLL3CFGR1_DIVN_SHIFT 0 ++#define RCC_PLL3CFGR1_DIVM3_MASK GENMASK_32(21, 16) ++#define RCC_PLL3CFGR1_DIVM3_SHIFT 16 ++#define RCC_PLL3CFGR1_IFRGE_MASK GENMASK_32(25, 24) ++#define RCC_PLL3CFGR1_IFRGE_SHIFT 24 ++ ++/* RCC_PLL3CFGR2 register fields */ ++#define RCC_PLL3CFGR2_DIVP_MASK GENMASK_32(6, 0) ++#define RCC_PLL3CFGR2_DIVP_SHIFT 0 ++#define RCC_PLL3CFGR2_DIVQ_MASK GENMASK_32(14, 8) ++#define RCC_PLL3CFGR2_DIVQ_SHIFT 8 ++#define RCC_PLL3CFGR2_DIVR_MASK GENMASK_32(22, 16) ++#define RCC_PLL3CFGR2_DIVR_SHIFT 16 ++ ++/* RCC_PLL3FRACR register fields */ ++#define RCC_PLL3FRACR_FRACV_MASK GENMASK_32(15, 3) ++#define RCC_PLL3FRACR_FRACV_SHIFT 3 ++#define RCC_PLL3FRACR_FRACLE BIT(16) ++ ++/* RCC_PLL3CSGR register fields */ ++#define RCC_PLL3CSGR_MOD_PER_MASK GENMASK_32(12, 0) ++#define RCC_PLL3CSGR_MOD_PER_SHIFT 0 ++#define RCC_PLL3CSGR_TPDFN_DIS BIT(13) ++#define RCC_PLL3CSGR_RPDFN_DIS BIT(14) ++#define RCC_PLL3CSGR_SSCG_MODE BIT(15) ++#define RCC_PLL3CSGR_INC_STEP_MASK GENMASK_32(30, 16) ++#define RCC_PLL3CSGR_INC_STEP_SHIFT 16 ++ ++/* RCC_PLL4CR register fields */ ++#define RCC_PLL4CR_PLLON BIT(0) ++#define RCC_PLL4CR_PLL4RDY BIT(1) ++#define RCC_PLL4CR_SSCG_CTRL BIT(2) ++#define RCC_PLL4CR_DIVPEN BIT(4) ++#define RCC_PLL4CR_DIVQEN BIT(5) ++#define RCC_PLL4CR_DIVREN BIT(6) ++ ++/* RCC_PLL4CFGR1 register fields */ ++#define RCC_PLL4CFGR1_DIVN_MASK GENMASK_32(8, 0) ++#define RCC_PLL4CFGR1_DIVN_SHIFT 0 ++#define RCC_PLL4CFGR1_DIVM4_MASK GENMASK_32(21, 16) ++#define RCC_PLL4CFGR1_DIVM4_SHIFT 16 ++#define RCC_PLL4CFGR1_IFRGE_MASK GENMASK_32(25, 24) ++#define RCC_PLL4CFGR1_IFRGE_SHIFT 24 ++ ++/* RCC_PLL4CFGR2 register fields */ ++#define RCC_PLL4CFGR2_DIVP_MASK GENMASK_32(6, 0) ++#define RCC_PLL4CFGR2_DIVP_SHIFT 0 ++#define RCC_PLL4CFGR2_DIVQ_MASK GENMASK_32(14, 8) ++#define RCC_PLL4CFGR2_DIVQ_SHIFT 8 ++#define RCC_PLL4CFGR2_DIVR_MASK GENMASK_32(22, 16) ++#define RCC_PLL4CFGR2_DIVR_SHIFT 16 ++ ++/* RCC_PLL4FRACR register fields */ ++#define RCC_PLL4FRACR_FRACV_MASK GENMASK_32(15, 3) ++#define RCC_PLL4FRACR_FRACV_SHIFT 3 ++#define RCC_PLL4FRACR_FRACLE BIT(16) ++ ++/* RCC_PLL4CSGR register fields */ ++#define RCC_PLL4CSGR_MOD_PER_MASK GENMASK_32(12, 0) ++#define RCC_PLL4CSGR_MOD_PER_SHIFT 0 ++#define RCC_PLL4CSGR_TPDFN_DIS BIT(13) ++#define RCC_PLL4CSGR_RPDFN_DIS BIT(14) ++#define RCC_PLL4CSGR_SSCG_MODE BIT(15) ++#define RCC_PLL4CSGR_INC_STEP_MASK GENMASK_32(30, 16) ++#define RCC_PLL4CSGR_INC_STEP_SHIFT 16 ++ ++/* RCC_MPCKSELR register fields */ ++#define RCC_MPCKSELR_MPUSRC_MASK GENMASK_32(1, 0) ++#define RCC_MPCKSELR_MPUSRC_SHIFT 0 ++#define RCC_MPCKSELR_MPUSRCRDY BIT(31) ++ ++/* RCC_ASSCKSELR register fields */ ++#define RCC_ASSCKSELR_AXISSRC_MASK GENMASK_32(2, 0) ++#define RCC_ASSCKSELR_AXISSRC_SHIFT 0 ++#define RCC_ASSCKSELR_AXISSRCRDY BIT(31) ++ ++/* RCC_MSSCKSELR register fields */ ++#define RCC_MSSCKSELR_MLAHBSSRC_MASK GENMASK_32(1, 0) ++#define RCC_MSSCKSELR_MLAHBSSRC_SHIFT 0 ++#define RCC_MSSCKSELR_MLAHBSSRCRDY BIT(31) ++ ++/* RCC_CPERCKSELR register fields */ ++#define RCC_CPERCKSELR_CKPERSRC_MASK GENMASK_32(1, 0) ++#define RCC_CPERCKSELR_CKPERSRC_SHIFT 0 ++ ++/* RCC_RTCDIVR register fields */ ++#define RCC_RTCDIVR_RTCDIV_MASK GENMASK_32(5, 0) ++#define RCC_RTCDIVR_RTCDIV_SHIFT 0 ++ ++/* RCC_MPCKDIVR register fields */ ++#define RCC_MPCKDIVR_MPUDIV_MASK GENMASK_32(3, 0) ++#define RCC_MPCKDIVR_MPUDIV_SHIFT 0 ++#define RCC_MPCKDIVR_MPUDIVRDY BIT(31) ++ ++/* RCC_AXIDIVR register fields */ ++#define RCC_AXIDIVR_AXIDIV_MASK GENMASK_32(2, 0) ++#define RCC_AXIDIVR_AXIDIV_SHIFT 0 ++#define RCC_AXIDIVR_AXIDIVRDY BIT(31) ++ ++/* RCC_MLAHBDIVR register fields */ ++#define RCC_MLAHBDIVR_MLAHBDIV_MASK GENMASK_32(3, 0) ++#define RCC_MLAHBDIVR_MLAHBDIV_SHIFT 0 ++#define RCC_MLAHBDIVR_MLAHBDIVRDY BIT(31) ++ ++/* RCC_APB1DIVR register fields */ ++#define RCC_APB1DIVR_APB1DIV_MASK GENMASK_32(2, 0) ++#define RCC_APB1DIVR_APB1DIV_SHIFT 0 ++#define RCC_APB1DIVR_APB1DIVRDY BIT(31) ++ ++/* RCC_APB2DIVR register fields */ ++#define RCC_APB2DIVR_APB2DIV_MASK GENMASK_32(2, 0) ++#define RCC_APB2DIVR_APB2DIV_SHIFT 0 ++#define RCC_APB2DIVR_APB2DIVRDY BIT(31) ++ ++/* RCC_APB3DIVR register fields */ ++#define RCC_APB3DIVR_APB3DIV_MASK GENMASK_32(2, 0) ++#define RCC_APB3DIVR_APB3DIV_SHIFT 0 ++#define RCC_APB3DIVR_APB3DIVRDY BIT(31) ++ ++/* RCC_APB4DIVR register fields */ ++#define RCC_APB4DIVR_APB4DIV_MASK GENMASK_32(2, 0) ++#define RCC_APB4DIVR_APB4DIV_SHIFT 0 ++#define RCC_APB4DIVR_APB4DIVRDY BIT(31) ++ ++/* RCC_APB5DIVR register fields */ ++#define RCC_APB5DIVR_APB5DIV_MASK GENMASK_32(2, 0) ++#define RCC_APB5DIVR_APB5DIV_SHIFT 0 ++#define RCC_APB5DIVR_APB5DIVRDY BIT(31) ++ ++/* RCC_APB6DIVR register fields */ ++#define RCC_APB6DIVR_APB6DIV_MASK GENMASK_32(2, 0) ++#define RCC_APB6DIVR_APB6DIV_SHIFT 0 ++#define RCC_APB6DIVR_APB6DIVRDY BIT(31) ++ ++/* RCC_TIMG1PRER register fields */ ++#define RCC_TIMG1PRER_TIMG1PRE BIT(0) ++#define RCC_TIMG1PRER_TIMG1PRERDY BIT(31) ++ ++/* RCC_TIMG2PRER register fields */ ++#define RCC_TIMG2PRER_TIMG2PRE BIT(0) ++#define RCC_TIMG2PRER_TIMG2PRERDY BIT(31) ++ ++/* RCC_TIMG3PRER register fields */ ++#define RCC_TIMG3PRER_TIMG3PRE BIT(0) ++#define RCC_TIMG3PRER_TIMG3PRERDY BIT(31) ++ ++/* RCC_DDRITFCR register fields */ ++#define RCC_DDRITFCR_DDRC1EN BIT(0) ++#define RCC_DDRITFCR_DDRC1LPEN BIT(1) ++#define RCC_DDRITFCR_DDRPHYCEN BIT(4) ++#define RCC_DDRITFCR_DDRPHYCLPEN BIT(5) ++#define RCC_DDRITFCR_DDRCAPBEN BIT(6) ++#define RCC_DDRITFCR_DDRCAPBLPEN BIT(7) ++#define RCC_DDRITFCR_AXIDCGEN BIT(8) ++#define RCC_DDRITFCR_DDRPHYCAPBEN BIT(9) ++#define RCC_DDRITFCR_DDRPHYCAPBLPEN BIT(10) ++#define RCC_DDRITFCR_KERDCG_DLY_MASK GENMASK_32(13, 11) ++#define RCC_DDRITFCR_KERDCG_DLY_SHIFT 11 ++#define RCC_DDRITFCR_DDRCAPBRST BIT(14) ++#define RCC_DDRITFCR_DDRCAXIRST BIT(15) ++#define RCC_DDRITFCR_DDRCORERST BIT(16) ++#define RCC_DDRITFCR_DPHYAPBRST BIT(17) ++#define RCC_DDRITFCR_DPHYRST BIT(18) ++#define RCC_DDRITFCR_DPHYCTLRST BIT(19) ++#define RCC_DDRITFCR_DDRCKMOD_MASK GENMASK_32(22, 20) ++#define RCC_DDRITFCR_DDRCKMOD_SHIFT 20 ++#define RCC_DDRITFCR_GSKPMOD BIT(23) ++#define RCC_DDRITFCR_GSKPCTRL BIT(24) ++#define RCC_DDRITFCR_DFILP_WIDTH_MASK GENMASK_32(27, 25) ++#define RCC_DDRITFCR_DFILP_WIDTH_SHIFT 25 ++#define RCC_DDRITFCR_GSKP_DUR_MASK GENMASK_32(31, 28) ++#define RCC_DDRITFCR_GSKP_DUR_SHIFT 28 ++ ++/* RCC_I2C12CKSELR register fields */ ++#define RCC_I2C12CKSELR_I2C12SRC_MASK GENMASK_32(2, 0) ++#define RCC_I2C12CKSELR_I2C12SRC_SHIFT 0 ++ ++/* RCC_I2C345CKSELR register fields */ ++#define RCC_I2C345CKSELR_I2C3SRC_MASK GENMASK_32(2, 0) ++#define RCC_I2C345CKSELR_I2C3SRC_SHIFT 0 ++#define RCC_I2C345CKSELR_I2C4SRC_MASK GENMASK_32(5, 3) ++#define RCC_I2C345CKSELR_I2C4SRC_SHIFT 3 ++#define RCC_I2C345CKSELR_I2C5SRC_MASK GENMASK_32(8, 6) ++#define RCC_I2C345CKSELR_I2C5SRC_SHIFT 6 ++ ++/* RCC_SPI2S1CKSELR register fields */ ++#define RCC_SPI2S1CKSELR_SPI1SRC_MASK GENMASK_32(2, 0) ++#define RCC_SPI2S1CKSELR_SPI1SRC_SHIFT 0 ++ ++/* RCC_SPI2S23CKSELR register fields */ ++#define RCC_SPI2S23CKSELR_SPI23SRC_MASK GENMASK_32(2, 0) ++#define RCC_SPI2S23CKSELR_SPI23SRC_SHIFT 0 ++ ++/* RCC_SPI45CKSELR register fields */ ++#define RCC_SPI45CKSELR_SPI4SRC_MASK GENMASK_32(2, 0) ++#define RCC_SPI45CKSELR_SPI4SRC_SHIFT 0 ++#define RCC_SPI45CKSELR_SPI5SRC_MASK GENMASK_32(5, 3) ++#define RCC_SPI45CKSELR_SPI5SRC_SHIFT 3 ++ ++/* RCC_UART12CKSELR register fields */ ++#define RCC_UART12CKSELR_UART1SRC_MASK GENMASK_32(2, 0) ++#define RCC_UART12CKSELR_UART1SRC_SHIFT 0 ++#define RCC_UART12CKSELR_UART2SRC_MASK GENMASK_32(5, 3) ++#define RCC_UART12CKSELR_UART2SRC_SHIFT 3 ++ ++/* RCC_UART35CKSELR register fields */ ++#define RCC_UART35CKSELR_UART35SRC_MASK GENMASK_32(2, 0) ++#define RCC_UART35CKSELR_UART35SRC_SHIFT 0 ++ ++/* RCC_UART4CKSELR register fields */ ++#define RCC_UART4CKSELR_UART4SRC_MASK GENMASK_32(2, 0) ++#define RCC_UART4CKSELR_UART4SRC_SHIFT 0 ++ ++/* RCC_UART6CKSELR register fields */ ++#define RCC_UART6CKSELR_UART6SRC_MASK GENMASK_32(2, 0) ++#define RCC_UART6CKSELR_UART6SRC_SHIFT 0 ++ ++/* RCC_UART78CKSELR register fields */ ++#define RCC_UART78CKSELR_UART78SRC_MASK GENMASK_32(2, 0) ++#define RCC_UART78CKSELR_UART78SRC_SHIFT 0 ++ ++/* RCC_LPTIM1CKSELR register fields */ ++#define RCC_LPTIM1CKSELR_LPTIM1SRC_MASK GENMASK_32(2, 0) ++#define RCC_LPTIM1CKSELR_LPTIM1SRC_SHIFT 0 ++ ++/* RCC_LPTIM23CKSELR register fields */ ++#define RCC_LPTIM23CKSELR_LPTIM2SRC_MASK GENMASK_32(2, 0) ++#define RCC_LPTIM23CKSELR_LPTIM2SRC_SHIFT 0 ++#define RCC_LPTIM23CKSELR_LPTIM3SRC_MASK GENMASK_32(5, 3) ++#define RCC_LPTIM23CKSELR_LPTIM3SRC_SHIFT 3 ++ ++/* RCC_LPTIM45CKSELR register fields */ ++#define RCC_LPTIM45CKSELR_LPTIM45SRC_MASK GENMASK_32(2, 0) ++#define RCC_LPTIM45CKSELR_LPTIM45SRC_SHIFT 0 ++ ++/* RCC_SAI1CKSELR register fields */ ++#define RCC_SAI1CKSELR_SAI1SRC_MASK GENMASK_32(2, 0) ++#define RCC_SAI1CKSELR_SAI1SRC_SHIFT 0 ++ ++/* RCC_SAI2CKSELR register fields */ ++#define RCC_SAI2CKSELR_SAI2SRC_MASK GENMASK_32(2, 0) ++#define RCC_SAI2CKSELR_SAI2SRC_SHIFT 0 ++ ++/* RCC_FDCANCKSELR register fields */ ++#define RCC_FDCANCKSELR_FDCANSRC_MASK GENMASK_32(1, 0) ++#define RCC_FDCANCKSELR_FDCANSRC_SHIFT 0 ++ ++/* RCC_SPDIFCKSELR register fields */ ++#define RCC_SPDIFCKSELR_SPDIFSRC_MASK GENMASK_32(1, 0) ++#define RCC_SPDIFCKSELR_SPDIFSRC_SHIFT 0 ++ ++/* RCC_ADC12CKSELR register fields */ ++#define RCC_ADC12CKSELR_ADC1SRC_MASK GENMASK_32(1, 0) ++#define RCC_ADC12CKSELR_ADC1SRC_SHIFT 0 ++#define RCC_ADC12CKSELR_ADC2SRC_MASK GENMASK_32(3, 2) ++#define RCC_ADC12CKSELR_ADC2SRC_SHIFT 2 ++ ++/* RCC_SDMMC12CKSELR register fields */ ++#define RCC_SDMMC12CKSELR_SDMMC1SRC_MASK GENMASK_32(2, 0) ++#define RCC_SDMMC12CKSELR_SDMMC1SRC_SHIFT 0 ++#define RCC_SDMMC12CKSELR_SDMMC2SRC_MASK GENMASK_32(5, 3) ++#define RCC_SDMMC12CKSELR_SDMMC2SRC_SHIFT 3 ++ ++/* RCC_ETH12CKSELR register fields */ ++#define RCC_ETH12CKSELR_ETH1SRC_MASK GENMASK_32(1, 0) ++#define RCC_ETH12CKSELR_ETH1SRC_SHIFT 0 ++#define RCC_ETH12CKSELR_ETH1PTPDIV_MASK GENMASK_32(7, 4) ++#define RCC_ETH12CKSELR_ETH1PTPDIV_SHIFT 4 ++#define RCC_ETH12CKSELR_ETH2SRC_MASK GENMASK_32(9, 8) ++#define RCC_ETH12CKSELR_ETH2SRC_SHIFT 8 ++#define RCC_ETH12CKSELR_ETH2PTPDIV_MASK GENMASK_32(15, 12) ++#define RCC_ETH12CKSELR_ETH2PTPDIV_SHIFT 12 ++ ++/* RCC_USBCKSELR register fields */ ++#define RCC_USBCKSELR_USBPHYSRC_MASK GENMASK_32(1, 0) ++#define RCC_USBCKSELR_USBPHYSRC_SHIFT 0 ++#define RCC_USBCKSELR_USBOSRC BIT(4) ++ ++/* RCC_QSPICKSELR register fields */ ++#define RCC_QSPICKSELR_QSPISRC_MASK GENMASK_32(1, 0) ++#define RCC_QSPICKSELR_QSPISRC_SHIFT 0 ++ ++/* RCC_FMCCKSELR register fields */ ++#define RCC_FMCCKSELR_FMCSRC_MASK GENMASK_32(1, 0) ++#define RCC_FMCCKSELR_FMCSRC_SHIFT 0 ++ ++/* RCC_RNG1CKSELR register fields */ ++#define RCC_RNG1CKSELR_RNG1SRC_MASK GENMASK_32(1, 0) ++#define RCC_RNG1CKSELR_RNG1SRC_SHIFT 0 ++ ++/* RCC_STGENCKSELR register fields */ ++#define RCC_STGENCKSELR_STGENSRC_MASK GENMASK_32(1, 0) ++#define RCC_STGENCKSELR_STGENSRC_SHIFT 0 ++ ++/* RCC_DCMIPPCKSELR register fields */ ++#define RCC_DCMIPPCKSELR_DCMIPPSRC_MASK GENMASK_32(1, 0) ++#define RCC_DCMIPPCKSELR_DCMIPPSRC_SHIFT 0 ++ ++/* RCC_SAESCKSELR register fields */ ++#define RCC_SAESCKSELR_SAESSRC_MASK GENMASK_32(1, 0) ++#define RCC_SAESCKSELR_SAESSRC_SHIFT 0 ++ ++/* RCC_APB1RSTSETR register fields */ ++#define RCC_APB1RSTSETR_TIM2RST BIT(0) ++#define RCC_APB1RSTSETR_TIM3RST BIT(1) ++#define RCC_APB1RSTSETR_TIM4RST BIT(2) ++#define RCC_APB1RSTSETR_TIM5RST BIT(3) ++#define RCC_APB1RSTSETR_TIM6RST BIT(4) ++#define RCC_APB1RSTSETR_TIM7RST BIT(5) ++#define RCC_APB1RSTSETR_LPTIM1RST BIT(9) ++#define RCC_APB1RSTSETR_SPI2RST BIT(11) ++#define RCC_APB1RSTSETR_SPI3RST BIT(12) ++#define RCC_APB1RSTSETR_USART3RST BIT(15) ++#define RCC_APB1RSTSETR_UART4RST BIT(16) ++#define RCC_APB1RSTSETR_UART5RST BIT(17) ++#define RCC_APB1RSTSETR_UART7RST BIT(18) ++#define RCC_APB1RSTSETR_UART8RST BIT(19) ++#define RCC_APB1RSTSETR_I2C1RST BIT(21) ++#define RCC_APB1RSTSETR_I2C2RST BIT(22) ++#define RCC_APB1RSTSETR_SPDIFRST BIT(26) ++ ++/* RCC_APB1RSTCLRR register fields */ ++#define RCC_APB1RSTCLRR_TIM2RST BIT(0) ++#define RCC_APB1RSTCLRR_TIM3RST BIT(1) ++#define RCC_APB1RSTCLRR_TIM4RST BIT(2) ++#define RCC_APB1RSTCLRR_TIM5RST BIT(3) ++#define RCC_APB1RSTCLRR_TIM6RST BIT(4) ++#define RCC_APB1RSTCLRR_TIM7RST BIT(5) ++#define RCC_APB1RSTCLRR_LPTIM1RST BIT(9) ++#define RCC_APB1RSTCLRR_SPI2RST BIT(11) ++#define RCC_APB1RSTCLRR_SPI3RST BIT(12) ++#define RCC_APB1RSTCLRR_USART3RST BIT(15) ++#define RCC_APB1RSTCLRR_UART4RST BIT(16) ++#define RCC_APB1RSTCLRR_UART5RST BIT(17) ++#define RCC_APB1RSTCLRR_UART7RST BIT(18) ++#define RCC_APB1RSTCLRR_UART8RST BIT(19) ++#define RCC_APB1RSTCLRR_I2C1RST BIT(21) ++#define RCC_APB1RSTCLRR_I2C2RST BIT(22) ++#define RCC_APB1RSTCLRR_SPDIFRST BIT(26) ++ ++/* RCC_APB2RSTSETR register fields */ ++#define RCC_APB2RSTSETR_TIM1RST BIT(0) ++#define RCC_APB2RSTSETR_TIM8RST BIT(1) ++#define RCC_APB2RSTSETR_SPI1RST BIT(8) ++#define RCC_APB2RSTSETR_USART6RST BIT(13) ++#define RCC_APB2RSTSETR_SAI1RST BIT(16) ++#define RCC_APB2RSTSETR_SAI2RST BIT(17) ++#define RCC_APB2RSTSETR_DFSDMRST BIT(20) ++#define RCC_APB2RSTSETR_FDCANRST BIT(24) ++ ++/* RCC_APB2RSTCLRR register fields */ ++#define RCC_APB2RSTCLRR_TIM1RST BIT(0) ++#define RCC_APB2RSTCLRR_TIM8RST BIT(1) ++#define RCC_APB2RSTCLRR_SPI1RST BIT(8) ++#define RCC_APB2RSTCLRR_USART6RST BIT(13) ++#define RCC_APB2RSTCLRR_SAI1RST BIT(16) ++#define RCC_APB2RSTCLRR_SAI2RST BIT(17) ++#define RCC_APB2RSTCLRR_DFSDMRST BIT(20) ++#define RCC_APB2RSTCLRR_FDCANRST BIT(24) ++ ++/* RCC_APB3RSTSETR register fields */ ++#define RCC_APB3RSTSETR_LPTIM2RST BIT(0) ++#define RCC_APB3RSTSETR_LPTIM3RST BIT(1) ++#define RCC_APB3RSTSETR_LPTIM4RST BIT(2) ++#define RCC_APB3RSTSETR_LPTIM5RST BIT(3) ++#define RCC_APB3RSTSETR_SYSCFGRST BIT(11) ++#define RCC_APB3RSTSETR_VREFRST BIT(13) ++#define RCC_APB3RSTSETR_DTSRST BIT(16) ++#define RCC_APB3RSTSETR_PMBCTRLRST BIT(17) ++ ++/* RCC_APB3RSTCLRR register fields */ ++#define RCC_APB3RSTCLRR_LPTIM2RST BIT(0) ++#define RCC_APB3RSTCLRR_LPTIM3RST BIT(1) ++#define RCC_APB3RSTCLRR_LPTIM4RST BIT(2) ++#define RCC_APB3RSTCLRR_LPTIM5RST BIT(3) ++#define RCC_APB3RSTCLRR_SYSCFGRST BIT(11) ++#define RCC_APB3RSTCLRR_VREFRST BIT(13) ++#define RCC_APB3RSTCLRR_DTSRST BIT(16) ++#define RCC_APB3RSTCLRR_PMBCTRLRST BIT(17) ++ ++/* RCC_APB4RSTSETR register fields */ ++#define RCC_APB4RSTSETR_LTDCRST BIT(0) ++#define RCC_APB4RSTSETR_DCMIPPRST BIT(1) ++#define RCC_APB4RSTSETR_DDRPERFMRST BIT(8) ++#define RCC_APB4RSTSETR_USBPHYRST BIT(16) ++ ++/* RCC_APB4RSTCLRR register fields */ ++#define RCC_APB4RSTCLRR_LTDCRST BIT(0) ++#define RCC_APB4RSTCLRR_DCMIPPRST BIT(1) ++#define RCC_APB4RSTCLRR_DDRPERFMRST BIT(8) ++#define RCC_APB4RSTCLRR_USBPHYRST BIT(16) ++ ++/* RCC_APB5RSTSETR register fields */ ++#define RCC_APB5RSTSETR_STGENRST BIT(20) ++ ++/* RCC_APB5RSTCLRR register fields */ ++#define RCC_APB5RSTCLRR_STGENRST BIT(20) ++ ++/* RCC_APB6RSTSETR register fields */ ++#define RCC_APB6RSTSETR_USART1RST BIT(0) ++#define RCC_APB6RSTSETR_USART2RST BIT(1) ++#define RCC_APB6RSTSETR_SPI4RST BIT(2) ++#define RCC_APB6RSTSETR_SPI5RST BIT(3) ++#define RCC_APB6RSTSETR_I2C3RST BIT(4) ++#define RCC_APB6RSTSETR_I2C4RST BIT(5) ++#define RCC_APB6RSTSETR_I2C5RST BIT(6) ++#define RCC_APB6RSTSETR_TIM12RST BIT(7) ++#define RCC_APB6RSTSETR_TIM13RST BIT(8) ++#define RCC_APB6RSTSETR_TIM14RST BIT(9) ++#define RCC_APB6RSTSETR_TIM15RST BIT(10) ++#define RCC_APB6RSTSETR_TIM16RST BIT(11) ++#define RCC_APB6RSTSETR_TIM17RST BIT(12) ++ ++/* RCC_APB6RSTCLRR register fields */ ++#define RCC_APB6RSTCLRR_USART1RST BIT(0) ++#define RCC_APB6RSTCLRR_USART2RST BIT(1) ++#define RCC_APB6RSTCLRR_SPI4RST BIT(2) ++#define RCC_APB6RSTCLRR_SPI5RST BIT(3) ++#define RCC_APB6RSTCLRR_I2C3RST BIT(4) ++#define RCC_APB6RSTCLRR_I2C4RST BIT(5) ++#define RCC_APB6RSTCLRR_I2C5RST BIT(6) ++#define RCC_APB6RSTCLRR_TIM12RST BIT(7) ++#define RCC_APB6RSTCLRR_TIM13RST BIT(8) ++#define RCC_APB6RSTCLRR_TIM14RST BIT(9) ++#define RCC_APB6RSTCLRR_TIM15RST BIT(10) ++#define RCC_APB6RSTCLRR_TIM16RST BIT(11) ++#define RCC_APB6RSTCLRR_TIM17RST BIT(12) ++ ++/* RCC_AHB2RSTSETR register fields */ ++#define RCC_AHB2RSTSETR_DMA1RST BIT(0) ++#define RCC_AHB2RSTSETR_DMA2RST BIT(1) ++#define RCC_AHB2RSTSETR_DMAMUX1RST BIT(2) ++#define RCC_AHB2RSTSETR_DMA3RST BIT(3) ++#define RCC_AHB2RSTSETR_DMAMUX2RST BIT(4) ++#define RCC_AHB2RSTSETR_ADC1RST BIT(5) ++#define RCC_AHB2RSTSETR_ADC2RST BIT(6) ++#define RCC_AHB2RSTSETR_USBORST BIT(8) ++ ++/* RCC_AHB2RSTCLRR register fields */ ++#define RCC_AHB2RSTCLRR_DMA1RST BIT(0) ++#define RCC_AHB2RSTCLRR_DMA2RST BIT(1) ++#define RCC_AHB2RSTCLRR_DMAMUX1RST BIT(2) ++#define RCC_AHB2RSTCLRR_DMA3RST BIT(3) ++#define RCC_AHB2RSTCLRR_DMAMUX2RST BIT(4) ++#define RCC_AHB2RSTCLRR_ADC1RST BIT(5) ++#define RCC_AHB2RSTCLRR_ADC2RST BIT(6) ++#define RCC_AHB2RSTCLRR_USBORST BIT(8) ++ ++/* RCC_AHB4RSTSETR register fields */ ++#define RCC_AHB4RSTSETR_GPIOARST BIT(0) ++#define RCC_AHB4RSTSETR_GPIOBRST BIT(1) ++#define RCC_AHB4RSTSETR_GPIOCRST BIT(2) ++#define RCC_AHB4RSTSETR_GPIODRST BIT(3) ++#define RCC_AHB4RSTSETR_GPIOERST BIT(4) ++#define RCC_AHB4RSTSETR_GPIOFRST BIT(5) ++#define RCC_AHB4RSTSETR_GPIOGRST BIT(6) ++#define RCC_AHB4RSTSETR_GPIOHRST BIT(7) ++#define RCC_AHB4RSTSETR_GPIOIRST BIT(8) ++#define RCC_AHB4RSTSETR_TSCRST BIT(15) ++ ++/* RCC_AHB4RSTCLRR register fields */ ++#define RCC_AHB4RSTCLRR_GPIOARST BIT(0) ++#define RCC_AHB4RSTCLRR_GPIOBRST BIT(1) ++#define RCC_AHB4RSTCLRR_GPIOCRST BIT(2) ++#define RCC_AHB4RSTCLRR_GPIODRST BIT(3) ++#define RCC_AHB4RSTCLRR_GPIOERST BIT(4) ++#define RCC_AHB4RSTCLRR_GPIOFRST BIT(5) ++#define RCC_AHB4RSTCLRR_GPIOGRST BIT(6) ++#define RCC_AHB4RSTCLRR_GPIOHRST BIT(7) ++#define RCC_AHB4RSTCLRR_GPIOIRST BIT(8) ++#define RCC_AHB4RSTCLRR_TSCRST BIT(15) ++ ++/* RCC_AHB5RSTSETR register fields */ ++#define RCC_AHB5RSTSETR_PKARST BIT(2) ++#define RCC_AHB5RSTSETR_SAESRST BIT(3) ++#define RCC_AHB5RSTSETR_CRYP1RST BIT(4) ++#define RCC_AHB5RSTSETR_HASH1RST BIT(5) ++#define RCC_AHB5RSTSETR_RNG1RST BIT(6) ++#define RCC_AHB5RSTSETR_AXIMCRST BIT(16) ++ ++/* RCC_AHB5RSTCLRR register fields */ ++#define RCC_AHB5RSTCLRR_PKARST BIT(2) ++#define RCC_AHB5RSTCLRR_SAESRST BIT(3) ++#define RCC_AHB5RSTCLRR_CRYP1RST BIT(4) ++#define RCC_AHB5RSTCLRR_HASH1RST BIT(5) ++#define RCC_AHB5RSTCLRR_RNG1RST BIT(6) ++#define RCC_AHB5RSTCLRR_AXIMCRST BIT(16) ++ ++/* RCC_AHB6RSTSETR register fields */ ++#define RCC_AHB6RSTSETR_MDMARST BIT(0) ++#define RCC_AHB6RSTSETR_MCERST BIT(1) ++#define RCC_AHB6RSTSETR_ETH1MACRST BIT(10) ++#define RCC_AHB6RSTSETR_FMCRST BIT(12) ++#define RCC_AHB6RSTSETR_QSPIRST BIT(14) ++#define RCC_AHB6RSTSETR_SDMMC1RST BIT(16) ++#define RCC_AHB6RSTSETR_SDMMC2RST BIT(17) ++#define RCC_AHB6RSTSETR_CRC1RST BIT(20) ++#define RCC_AHB6RSTSETR_USBHRST BIT(24) ++#define RCC_AHB6RSTSETR_ETH2MACRST BIT(30) ++ ++/* RCC_AHB6RSTCLRR register fields */ ++#define RCC_AHB6RSTCLRR_MDMARST BIT(0) ++#define RCC_AHB6RSTCLRR_MCERST BIT(1) ++#define RCC_AHB6RSTCLRR_ETH1MACRST BIT(10) ++#define RCC_AHB6RSTCLRR_FMCRST BIT(12) ++#define RCC_AHB6RSTCLRR_QSPIRST BIT(14) ++#define RCC_AHB6RSTCLRR_SDMMC1RST BIT(16) ++#define RCC_AHB6RSTCLRR_SDMMC2RST BIT(17) ++#define RCC_AHB6RSTCLRR_CRC1RST BIT(20) ++#define RCC_AHB6RSTCLRR_USBHRST BIT(24) ++#define RCC_AHB6RSTCLRR_ETH2MACRST BIT(30) ++ ++/* RCC_MP_APB1ENSETR register fields */ ++#define RCC_MP_APB1ENSETR_TIM2EN BIT(0) ++#define RCC_MP_APB1ENSETR_TIM3EN BIT(1) ++#define RCC_MP_APB1ENSETR_TIM4EN BIT(2) ++#define RCC_MP_APB1ENSETR_TIM5EN BIT(3) ++#define RCC_MP_APB1ENSETR_TIM6EN BIT(4) ++#define RCC_MP_APB1ENSETR_TIM7EN BIT(5) ++#define RCC_MP_APB1ENSETR_LPTIM1EN BIT(9) ++#define RCC_MP_APB1ENSETR_SPI2EN BIT(11) ++#define RCC_MP_APB1ENSETR_SPI3EN BIT(12) ++#define RCC_MP_APB1ENSETR_USART3EN BIT(15) ++#define RCC_MP_APB1ENSETR_UART4EN BIT(16) ++#define RCC_MP_APB1ENSETR_UART5EN BIT(17) ++#define RCC_MP_APB1ENSETR_UART7EN BIT(18) ++#define RCC_MP_APB1ENSETR_UART8EN BIT(19) ++#define RCC_MP_APB1ENSETR_I2C1EN BIT(21) ++#define RCC_MP_APB1ENSETR_I2C2EN BIT(22) ++#define RCC_MP_APB1ENSETR_SPDIFEN BIT(26) ++ ++/* RCC_MP_APB1ENCLRR register fields */ ++#define RCC_MP_APB1ENCLRR_TIM2EN BIT(0) ++#define RCC_MP_APB1ENCLRR_TIM3EN BIT(1) ++#define RCC_MP_APB1ENCLRR_TIM4EN BIT(2) ++#define RCC_MP_APB1ENCLRR_TIM5EN BIT(3) ++#define RCC_MP_APB1ENCLRR_TIM6EN BIT(4) ++#define RCC_MP_APB1ENCLRR_TIM7EN BIT(5) ++#define RCC_MP_APB1ENCLRR_LPTIM1EN BIT(9) ++#define RCC_MP_APB1ENCLRR_SPI2EN BIT(11) ++#define RCC_MP_APB1ENCLRR_SPI3EN BIT(12) ++#define RCC_MP_APB1ENCLRR_USART3EN BIT(15) ++#define RCC_MP_APB1ENCLRR_UART4EN BIT(16) ++#define RCC_MP_APB1ENCLRR_UART5EN BIT(17) ++#define RCC_MP_APB1ENCLRR_UART7EN BIT(18) ++#define RCC_MP_APB1ENCLRR_UART8EN BIT(19) ++#define RCC_MP_APB1ENCLRR_I2C1EN BIT(21) ++#define RCC_MP_APB1ENCLRR_I2C2EN BIT(22) ++#define RCC_MP_APB1ENCLRR_SPDIFEN BIT(26) ++ ++/* RCC_MP_APB2ENSETR register fields */ ++#define RCC_MP_APB2ENSETR_TIM1EN BIT(0) ++#define RCC_MP_APB2ENSETR_TIM8EN BIT(1) ++#define RCC_MP_APB2ENSETR_SPI1EN BIT(8) ++#define RCC_MP_APB2ENSETR_USART6EN BIT(13) ++#define RCC_MP_APB2ENSETR_SAI1EN BIT(16) ++#define RCC_MP_APB2ENSETR_SAI2EN BIT(17) ++#define RCC_MP_APB2ENSETR_DFSDMEN BIT(20) ++#define RCC_MP_APB2ENSETR_ADFSDMEN BIT(21) ++#define RCC_MP_APB2ENSETR_FDCANEN BIT(24) ++ ++/* RCC_MP_APB2ENCLRR register fields */ ++#define RCC_MP_APB2ENCLRR_TIM1EN BIT(0) ++#define RCC_MP_APB2ENCLRR_TIM8EN BIT(1) ++#define RCC_MP_APB2ENCLRR_SPI1EN BIT(8) ++#define RCC_MP_APB2ENCLRR_USART6EN BIT(13) ++#define RCC_MP_APB2ENCLRR_SAI1EN BIT(16) ++#define RCC_MP_APB2ENCLRR_SAI2EN BIT(17) ++#define RCC_MP_APB2ENCLRR_DFSDMEN BIT(20) ++#define RCC_MP_APB2ENCLRR_ADFSDMEN BIT(21) ++#define RCC_MP_APB2ENCLRR_FDCANEN BIT(24) ++ ++/* RCC_MP_APB3ENSETR register fields */ ++#define RCC_MP_APB3ENSETR_LPTIM2EN BIT(0) ++#define RCC_MP_APB3ENSETR_LPTIM3EN BIT(1) ++#define RCC_MP_APB3ENSETR_LPTIM4EN BIT(2) ++#define RCC_MP_APB3ENSETR_LPTIM5EN BIT(3) ++#define RCC_MP_APB3ENSETR_VREFEN BIT(13) ++#define RCC_MP_APB3ENSETR_DTSEN BIT(16) ++#define RCC_MP_APB3ENSETR_PMBCTRLEN BIT(17) ++#define RCC_MP_APB3ENSETR_HDPEN BIT(20) ++ ++/* RCC_MP_APB3ENCLRR register fields */ ++#define RCC_MP_APB3ENCLRR_LPTIM2EN BIT(0) ++#define RCC_MP_APB3ENCLRR_LPTIM3EN BIT(1) ++#define RCC_MP_APB3ENCLRR_LPTIM4EN BIT(2) ++#define RCC_MP_APB3ENCLRR_LPTIM5EN BIT(3) ++#define RCC_MP_APB3ENCLRR_VREFEN BIT(13) ++#define RCC_MP_APB3ENCLRR_DTSEN BIT(16) ++#define RCC_MP_APB3ENCLRR_PMBCTRLEN BIT(17) ++#define RCC_MP_APB3ENCLRR_HDPEN BIT(20) ++ ++/* RCC_MP_S_APB3ENSETR register fields */ ++#define RCC_MP_S_APB3ENSETR_SYSCFGEN BIT(0) ++ ++/* RCC_MP_S_APB3ENCLRR register fields */ ++#define RCC_MP_S_APB3ENCLRR_SYSCFGEN BIT(0) ++ ++/* RCC_MP_NS_APB3ENSETR register fields */ ++#define RCC_MP_NS_APB3ENSETR_SYSCFGEN BIT(0) ++ ++/* RCC_MP_NS_APB3ENCLRR register fields */ ++#define RCC_MP_NS_APB3ENCLRR_SYSCFGEN BIT(0) ++ ++/* RCC_MP_APB4ENSETR register fields */ ++#define RCC_MP_APB4ENSETR_DCMIPPEN BIT(1) ++#define RCC_MP_APB4ENSETR_DDRPERFMEN BIT(8) ++#define RCC_MP_APB4ENSETR_IWDG2APBEN BIT(15) ++#define RCC_MP_APB4ENSETR_USBPHYEN BIT(16) ++#define RCC_MP_APB4ENSETR_STGENROEN BIT(20) ++ ++/* RCC_MP_APB4ENCLRR register fields */ ++#define RCC_MP_APB4ENCLRR_DCMIPPEN BIT(1) ++#define RCC_MP_APB4ENCLRR_DDRPERFMEN BIT(8) ++#define RCC_MP_APB4ENCLRR_IWDG2APBEN BIT(15) ++#define RCC_MP_APB4ENCLRR_USBPHYEN BIT(16) ++#define RCC_MP_APB4ENCLRR_STGENROEN BIT(20) ++ ++/* RCC_MP_S_APB4ENSETR register fields */ ++#define RCC_MP_S_APB4ENSETR_LTDCEN BIT(0) ++ ++/* RCC_MP_S_APB4ENCLRR register fields */ ++#define RCC_MP_S_APB4ENCLRR_LTDCEN BIT(0) ++ ++/* RCC_MP_NS_APB4ENSETR register fields */ ++#define RCC_MP_NS_APB4ENSETR_LTDCEN BIT(0) ++ ++/* RCC_MP_NS_APB4ENCLRR register fields */ ++#define RCC_MP_NS_APB4ENCLRR_LTDCEN BIT(0) ++ ++/* RCC_MP_APB5ENSETR register fields */ ++#define RCC_MP_APB5ENSETR_RTCAPBEN BIT(8) ++#define RCC_MP_APB5ENSETR_TZCEN BIT(11) ++#define RCC_MP_APB5ENSETR_ETZPCEN BIT(13) ++#define RCC_MP_APB5ENSETR_IWDG1APBEN BIT(15) ++#define RCC_MP_APB5ENSETR_BSECEN BIT(16) ++#define RCC_MP_APB5ENSETR_STGENCEN BIT(20) ++ ++/* RCC_MP_APB5ENCLRR register fields */ ++#define RCC_MP_APB5ENCLRR_RTCAPBEN BIT(8) ++#define RCC_MP_APB5ENCLRR_TZCEN BIT(11) ++#define RCC_MP_APB5ENCLRR_ETZPCEN BIT(13) ++#define RCC_MP_APB5ENCLRR_IWDG1APBEN BIT(15) ++#define RCC_MP_APB5ENCLRR_BSECEN BIT(16) ++#define RCC_MP_APB5ENCLRR_STGENCEN BIT(20) ++ ++/* RCC_MP_APB6ENSETR register fields */ ++#define RCC_MP_APB6ENSETR_USART1EN BIT(0) ++#define RCC_MP_APB6ENSETR_USART2EN BIT(1) ++#define RCC_MP_APB6ENSETR_SPI4EN BIT(2) ++#define RCC_MP_APB6ENSETR_SPI5EN BIT(3) ++#define RCC_MP_APB6ENSETR_I2C3EN BIT(4) ++#define RCC_MP_APB6ENSETR_I2C4EN BIT(5) ++#define RCC_MP_APB6ENSETR_I2C5EN BIT(6) ++#define RCC_MP_APB6ENSETR_TIM12EN BIT(7) ++#define RCC_MP_APB6ENSETR_TIM13EN BIT(8) ++#define RCC_MP_APB6ENSETR_TIM14EN BIT(9) ++#define RCC_MP_APB6ENSETR_TIM15EN BIT(10) ++#define RCC_MP_APB6ENSETR_TIM16EN BIT(11) ++#define RCC_MP_APB6ENSETR_TIM17EN BIT(12) ++ ++/* RCC_MP_APB6ENCLRR register fields */ ++#define RCC_MP_APB6ENCLRR_USART1EN BIT(0) ++#define RCC_MP_APB6ENCLRR_USART2EN BIT(1) ++#define RCC_MP_APB6ENCLRR_SPI4EN BIT(2) ++#define RCC_MP_APB6ENCLRR_SPI5EN BIT(3) ++#define RCC_MP_APB6ENCLRR_I2C3EN BIT(4) ++#define RCC_MP_APB6ENCLRR_I2C4EN BIT(5) ++#define RCC_MP_APB6ENCLRR_I2C5EN BIT(6) ++#define RCC_MP_APB6ENCLRR_TIM12EN BIT(7) ++#define RCC_MP_APB6ENCLRR_TIM13EN BIT(8) ++#define RCC_MP_APB6ENCLRR_TIM14EN BIT(9) ++#define RCC_MP_APB6ENCLRR_TIM15EN BIT(10) ++#define RCC_MP_APB6ENCLRR_TIM16EN BIT(11) ++#define RCC_MP_APB6ENCLRR_TIM17EN BIT(12) ++ ++/* RCC_MP_AHB2ENSETR register fields */ ++#define RCC_MP_AHB2ENSETR_DMA1EN BIT(0) ++#define RCC_MP_AHB2ENSETR_DMA2EN BIT(1) ++#define RCC_MP_AHB2ENSETR_DMAMUX1EN BIT(2) ++#define RCC_MP_AHB2ENSETR_DMA3EN BIT(3) ++#define RCC_MP_AHB2ENSETR_DMAMUX2EN BIT(4) ++#define RCC_MP_AHB2ENSETR_ADC1EN BIT(5) ++#define RCC_MP_AHB2ENSETR_ADC2EN BIT(6) ++#define RCC_MP_AHB2ENSETR_USBOEN BIT(8) ++ ++/* RCC_MP_AHB2ENCLRR register fields */ ++#define RCC_MP_AHB2ENCLRR_DMA1EN BIT(0) ++#define RCC_MP_AHB2ENCLRR_DMA2EN BIT(1) ++#define RCC_MP_AHB2ENCLRR_DMAMUX1EN BIT(2) ++#define RCC_MP_AHB2ENCLRR_DMA3EN BIT(3) ++#define RCC_MP_AHB2ENCLRR_DMAMUX2EN BIT(4) ++#define RCC_MP_AHB2ENCLRR_ADC1EN BIT(5) ++#define RCC_MP_AHB2ENCLRR_ADC2EN BIT(6) ++#define RCC_MP_AHB2ENCLRR_USBOEN BIT(8) ++ ++/* RCC_MP_AHB4ENSETR register fields */ ++#define RCC_MP_AHB4ENSETR_TSCEN BIT(15) ++ ++/* RCC_MP_AHB4ENCLRR register fields */ ++#define RCC_MP_AHB4ENCLRR_TSCEN BIT(15) ++ ++/* RCC_MP_S_AHB4ENSETR register fields */ ++#define RCC_MP_S_AHB4ENSETR_GPIOAEN BIT(0) ++#define RCC_MP_S_AHB4ENSETR_GPIOBEN BIT(1) ++#define RCC_MP_S_AHB4ENSETR_GPIOCEN BIT(2) ++#define RCC_MP_S_AHB4ENSETR_GPIODEN BIT(3) ++#define RCC_MP_S_AHB4ENSETR_GPIOEEN BIT(4) ++#define RCC_MP_S_AHB4ENSETR_GPIOFEN BIT(5) ++#define RCC_MP_S_AHB4ENSETR_GPIOGEN BIT(6) ++#define RCC_MP_S_AHB4ENSETR_GPIOHEN BIT(7) ++#define RCC_MP_S_AHB4ENSETR_GPIOIEN BIT(8) ++ ++/* RCC_MP_S_AHB4ENCLRR register fields */ ++#define RCC_MP_S_AHB4ENCLRR_GPIOAEN BIT(0) ++#define RCC_MP_S_AHB4ENCLRR_GPIOBEN BIT(1) ++#define RCC_MP_S_AHB4ENCLRR_GPIOCEN BIT(2) ++#define RCC_MP_S_AHB4ENCLRR_GPIODEN BIT(3) ++#define RCC_MP_S_AHB4ENCLRR_GPIOEEN BIT(4) ++#define RCC_MP_S_AHB4ENCLRR_GPIOFEN BIT(5) ++#define RCC_MP_S_AHB4ENCLRR_GPIOGEN BIT(6) ++#define RCC_MP_S_AHB4ENCLRR_GPIOHEN BIT(7) ++#define RCC_MP_S_AHB4ENCLRR_GPIOIEN BIT(8) ++ ++/* RCC_MP_NS_AHB4ENSETR register fields */ ++#define RCC_MP_NS_AHB4ENSETR_GPIOAEN BIT(0) ++#define RCC_MP_NS_AHB4ENSETR_GPIOBEN BIT(1) ++#define RCC_MP_NS_AHB4ENSETR_GPIOCEN BIT(2) ++#define RCC_MP_NS_AHB4ENSETR_GPIODEN BIT(3) ++#define RCC_MP_NS_AHB4ENSETR_GPIOEEN BIT(4) ++#define RCC_MP_NS_AHB4ENSETR_GPIOFEN BIT(5) ++#define RCC_MP_NS_AHB4ENSETR_GPIOGEN BIT(6) ++#define RCC_MP_NS_AHB4ENSETR_GPIOHEN BIT(7) ++#define RCC_MP_NS_AHB4ENSETR_GPIOIEN BIT(8) ++ ++/* RCC_MP_NS_AHB4ENCLRR register fields */ ++#define RCC_MP_NS_AHB4ENCLRR_GPIOAEN BIT(0) ++#define RCC_MP_NS_AHB4ENCLRR_GPIOBEN BIT(1) ++#define RCC_MP_NS_AHB4ENCLRR_GPIOCEN BIT(2) ++#define RCC_MP_NS_AHB4ENCLRR_GPIODEN BIT(3) ++#define RCC_MP_NS_AHB4ENCLRR_GPIOEEN BIT(4) ++#define RCC_MP_NS_AHB4ENCLRR_GPIOFEN BIT(5) ++#define RCC_MP_NS_AHB4ENCLRR_GPIOGEN BIT(6) ++#define RCC_MP_NS_AHB4ENCLRR_GPIOHEN BIT(7) ++#define RCC_MP_NS_AHB4ENCLRR_GPIOIEN BIT(8) ++ ++/* RCC_MP_AHB5ENSETR register fields */ ++#define RCC_MP_AHB5ENSETR_PKAEN BIT(2) ++#define RCC_MP_AHB5ENSETR_SAESEN BIT(3) ++#define RCC_MP_AHB5ENSETR_CRYP1EN BIT(4) ++#define RCC_MP_AHB5ENSETR_HASH1EN BIT(5) ++#define RCC_MP_AHB5ENSETR_RNG1EN BIT(6) ++#define RCC_MP_AHB5ENSETR_BKPSRAMEN BIT(8) ++#define RCC_MP_AHB5ENSETR_AXIMCEN BIT(16) ++ ++/* RCC_MP_AHB5ENCLRR register fields */ ++#define RCC_MP_AHB5ENCLRR_PKAEN BIT(2) ++#define RCC_MP_AHB5ENCLRR_SAESEN BIT(3) ++#define RCC_MP_AHB5ENCLRR_CRYP1EN BIT(4) ++#define RCC_MP_AHB5ENCLRR_HASH1EN BIT(5) ++#define RCC_MP_AHB5ENCLRR_RNG1EN BIT(6) ++#define RCC_MP_AHB5ENCLRR_BKPSRAMEN BIT(8) ++#define RCC_MP_AHB5ENCLRR_AXIMCEN BIT(16) ++ ++/* RCC_MP_AHB6ENSETR register fields */ ++#define RCC_MP_AHB6ENSETR_MCEEN BIT(1) ++#define RCC_MP_AHB6ENSETR_ETH1CKEN BIT(7) ++#define RCC_MP_AHB6ENSETR_ETH1TXEN BIT(8) ++#define RCC_MP_AHB6ENSETR_ETH1RXEN BIT(9) ++#define RCC_MP_AHB6ENSETR_ETH1MACEN BIT(10) ++#define RCC_MP_AHB6ENSETR_FMCEN BIT(12) ++#define RCC_MP_AHB6ENSETR_QSPIEN BIT(14) ++#define RCC_MP_AHB6ENSETR_SDMMC1EN BIT(16) ++#define RCC_MP_AHB6ENSETR_SDMMC2EN BIT(17) ++#define RCC_MP_AHB6ENSETR_CRC1EN BIT(20) ++#define RCC_MP_AHB6ENSETR_USBHEN BIT(24) ++#define RCC_MP_AHB6ENSETR_ETH2CKEN BIT(27) ++#define RCC_MP_AHB6ENSETR_ETH2TXEN BIT(28) ++#define RCC_MP_AHB6ENSETR_ETH2RXEN BIT(29) ++#define RCC_MP_AHB6ENSETR_ETH2MACEN BIT(30) ++ ++/* RCC_MP_AHB6ENCLRR register fields */ ++#define RCC_MP_AHB6ENCLRR_MCEEN BIT(1) ++#define RCC_MP_AHB6ENCLRR_ETH1CKEN BIT(7) ++#define RCC_MP_AHB6ENCLRR_ETH1TXEN BIT(8) ++#define RCC_MP_AHB6ENCLRR_ETH1RXEN BIT(9) ++#define RCC_MP_AHB6ENCLRR_ETH1MACEN BIT(10) ++#define RCC_MP_AHB6ENCLRR_FMCEN BIT(12) ++#define RCC_MP_AHB6ENCLRR_QSPIEN BIT(14) ++#define RCC_MP_AHB6ENCLRR_SDMMC1EN BIT(16) ++#define RCC_MP_AHB6ENCLRR_SDMMC2EN BIT(17) ++#define RCC_MP_AHB6ENCLRR_CRC1EN BIT(20) ++#define RCC_MP_AHB6ENCLRR_USBHEN BIT(24) ++#define RCC_MP_AHB6ENCLRR_ETH2CKEN BIT(27) ++#define RCC_MP_AHB6ENCLRR_ETH2TXEN BIT(28) ++#define RCC_MP_AHB6ENCLRR_ETH2RXEN BIT(29) ++#define RCC_MP_AHB6ENCLRR_ETH2MACEN BIT(30) ++ ++/* RCC_MP_S_AHB6ENSETR register fields */ ++#define RCC_MP_S_AHB6ENSETR_MDMAEN BIT(0) ++ ++/* RCC_MP_S_AHB6ENCLRR register fields */ ++#define RCC_MP_S_AHB6ENCLRR_MDMAEN BIT(0) ++ ++/* RCC_MP_NS_AHB6ENSETR register fields */ ++#define RCC_MP_NS_AHB6ENSETR_MDMAEN BIT(0) ++ ++/* RCC_MP_NS_AHB6ENCLRR register fields */ ++#define RCC_MP_NS_AHB6ENCLRR_MDMAEN BIT(0) ++ ++/* RCC_MP_APB1LPENSETR register fields */ ++#define RCC_MP_APB1LPENSETR_TIM2LPEN BIT(0) ++#define RCC_MP_APB1LPENSETR_TIM3LPEN BIT(1) ++#define RCC_MP_APB1LPENSETR_TIM4LPEN BIT(2) ++#define RCC_MP_APB1LPENSETR_TIM5LPEN BIT(3) ++#define RCC_MP_APB1LPENSETR_TIM6LPEN BIT(4) ++#define RCC_MP_APB1LPENSETR_TIM7LPEN BIT(5) ++#define RCC_MP_APB1LPENSETR_LPTIM1LPEN BIT(9) ++#define RCC_MP_APB1LPENSETR_SPI2LPEN BIT(11) ++#define RCC_MP_APB1LPENSETR_SPI3LPEN BIT(12) ++#define RCC_MP_APB1LPENSETR_USART3LPEN BIT(15) ++#define RCC_MP_APB1LPENSETR_UART4LPEN BIT(16) ++#define RCC_MP_APB1LPENSETR_UART5LPEN BIT(17) ++#define RCC_MP_APB1LPENSETR_UART7LPEN BIT(18) ++#define RCC_MP_APB1LPENSETR_UART8LPEN BIT(19) ++#define RCC_MP_APB1LPENSETR_I2C1LPEN BIT(21) ++#define RCC_MP_APB1LPENSETR_I2C2LPEN BIT(22) ++#define RCC_MP_APB1LPENSETR_SPDIFLPEN BIT(26) ++ ++/* RCC_MP_APB1LPENCLRR register fields */ ++#define RCC_MP_APB1LPENCLRR_TIM2LPEN BIT(0) ++#define RCC_MP_APB1LPENCLRR_TIM3LPEN BIT(1) ++#define RCC_MP_APB1LPENCLRR_TIM4LPEN BIT(2) ++#define RCC_MP_APB1LPENCLRR_TIM5LPEN BIT(3) ++#define RCC_MP_APB1LPENCLRR_TIM6LPEN BIT(4) ++#define RCC_MP_APB1LPENCLRR_TIM7LPEN BIT(5) ++#define RCC_MP_APB1LPENCLRR_LPTIM1LPEN BIT(9) ++#define RCC_MP_APB1LPENCLRR_SPI2LPEN BIT(11) ++#define RCC_MP_APB1LPENCLRR_SPI3LPEN BIT(12) ++#define RCC_MP_APB1LPENCLRR_USART3LPEN BIT(15) ++#define RCC_MP_APB1LPENCLRR_UART4LPEN BIT(16) ++#define RCC_MP_APB1LPENCLRR_UART5LPEN BIT(17) ++#define RCC_MP_APB1LPENCLRR_UART7LPEN BIT(18) ++#define RCC_MP_APB1LPENCLRR_UART8LPEN BIT(19) ++#define RCC_MP_APB1LPENCLRR_I2C1LPEN BIT(21) ++#define RCC_MP_APB1LPENCLRR_I2C2LPEN BIT(22) ++#define RCC_MP_APB1LPENCLRR_SPDIFLPEN BIT(26) ++ ++/* RCC_MP_APB2LPENSETR register fields */ ++#define RCC_MP_APB2LPENSETR_TIM1LPEN BIT(0) ++#define RCC_MP_APB2LPENSETR_TIM8LPEN BIT(1) ++#define RCC_MP_APB2LPENSETR_SPI1LPEN BIT(8) ++#define RCC_MP_APB2LPENSETR_USART6LPEN BIT(13) ++#define RCC_MP_APB2LPENSETR_SAI1LPEN BIT(16) ++#define RCC_MP_APB2LPENSETR_SAI2LPEN BIT(17) ++#define RCC_MP_APB2LPENSETR_DFSDMLPEN BIT(20) ++#define RCC_MP_APB2LPENSETR_ADFSDMLPEN BIT(21) ++#define RCC_MP_APB2LPENSETR_FDCANLPEN BIT(24) ++ ++/* RCC_MP_APB2LPENCLRR register fields */ ++#define RCC_MP_APB2LPENCLRR_TIM1LPEN BIT(0) ++#define RCC_MP_APB2LPENCLRR_TIM8LPEN BIT(1) ++#define RCC_MP_APB2LPENCLRR_SPI1LPEN BIT(8) ++#define RCC_MP_APB2LPENCLRR_USART6LPEN BIT(13) ++#define RCC_MP_APB2LPENCLRR_SAI1LPEN BIT(16) ++#define RCC_MP_APB2LPENCLRR_SAI2LPEN BIT(17) ++#define RCC_MP_APB2LPENCLRR_DFSDMLPEN BIT(20) ++#define RCC_MP_APB2LPENCLRR_ADFSDMLPEN BIT(21) ++#define RCC_MP_APB2LPENCLRR_FDCANLPEN BIT(24) ++ ++/* RCC_MP_APB3LPENSETR register fields */ ++#define RCC_MP_APB3LPENSETR_LPTIM2LPEN BIT(0) ++#define RCC_MP_APB3LPENSETR_LPTIM3LPEN BIT(1) ++#define RCC_MP_APB3LPENSETR_LPTIM4LPEN BIT(2) ++#define RCC_MP_APB3LPENSETR_LPTIM5LPEN BIT(3) ++#define RCC_MP_APB3LPENSETR_VREFLPEN BIT(13) ++#define RCC_MP_APB3LPENSETR_DTSLPEN BIT(16) ++#define RCC_MP_APB3LPENSETR_PMBCTRLLPEN BIT(17) ++ ++/* RCC_MP_APB3LPENCLRR register fields */ ++#define RCC_MP_APB3LPENCLRR_LPTIM2LPEN BIT(0) ++#define RCC_MP_APB3LPENCLRR_LPTIM3LPEN BIT(1) ++#define RCC_MP_APB3LPENCLRR_LPTIM4LPEN BIT(2) ++#define RCC_MP_APB3LPENCLRR_LPTIM5LPEN BIT(3) ++#define RCC_MP_APB3LPENCLRR_VREFLPEN BIT(13) ++#define RCC_MP_APB3LPENCLRR_DTSLPEN BIT(16) ++#define RCC_MP_APB3LPENCLRR_PMBCTRLLPEN BIT(17) ++ ++/* RCC_MP_S_APB3LPENSETR register fields */ ++#define RCC_MP_S_APB3LPENSETR_SYSCFGLPEN BIT(0) ++ ++/* RCC_MP_S_APB3LPENCLRR register fields */ ++#define RCC_MP_S_APB3LPENCLRR_SYSCFGLPEN BIT(0) ++ ++/* RCC_MP_NS_APB3LPENSETR register fields */ ++#define RCC_MP_NS_APB3LPENSETR_SYSCFGLPEN BIT(0) ++ ++/* RCC_MP_NS_APB3LPENCLRR register fields */ ++#define RCC_MP_NS_APB3LPENCLRR_SYSCFGLPEN BIT(0) ++ ++/* RCC_MP_APB4LPENSETR register fields */ ++#define RCC_MP_APB4LPENSETR_DCMIPPLPEN BIT(1) ++#define RCC_MP_APB4LPENSETR_DDRPERFMLPEN BIT(8) ++#define RCC_MP_APB4LPENSETR_IWDG2APBLPEN BIT(15) ++#define RCC_MP_APB4LPENSETR_USBPHYLPEN BIT(16) ++#define RCC_MP_APB4LPENSETR_STGENROLPEN BIT(20) ++#define RCC_MP_APB4LPENSETR_STGENROSTPEN BIT(21) ++ ++/* RCC_MP_APB4LPENCLRR register fields */ ++#define RCC_MP_APB4LPENCLRR_DCMIPPLPEN BIT(1) ++#define RCC_MP_APB4LPENCLRR_DDRPERFMLPEN BIT(8) ++#define RCC_MP_APB4LPENCLRR_IWDG2APBLPEN BIT(15) ++#define RCC_MP_APB4LPENCLRR_USBPHYLPEN BIT(16) ++#define RCC_MP_APB4LPENCLRR_STGENROLPEN BIT(20) ++#define RCC_MP_APB4LPENCLRR_STGENROSTPEN BIT(21) ++ ++/* RCC_MP_S_APB4LPENSETR register fields */ ++#define RCC_MP_S_APB4LPENSETR_LTDCLPEN BIT(0) ++ ++/* RCC_MP_S_APB4LPENCLRR register fields */ ++#define RCC_MP_S_APB4LPENCLRR_LTDCLPEN BIT(0) ++ ++/* RCC_MP_NS_APB4LPENSETR register fields */ ++#define RCC_MP_NS_APB4LPENSETR_LTDCLPEN BIT(0) ++ ++/* RCC_MP_NS_APB4LPENCLRR register fields */ ++#define RCC_MP_NS_APB4LPENCLRR_LTDCLPEN BIT(0) ++ ++/* RCC_MP_APB5LPENSETR register fields */ ++#define RCC_MP_APB5LPENSETR_RTCAPBLPEN BIT(8) ++#define RCC_MP_APB5LPENSETR_TZCLPEN BIT(11) ++#define RCC_MP_APB5LPENSETR_ETZPCLPEN BIT(13) ++#define RCC_MP_APB5LPENSETR_IWDG1APBLPEN BIT(15) ++#define RCC_MP_APB5LPENSETR_BSECLPEN BIT(16) ++#define RCC_MP_APB5LPENSETR_STGENCLPEN BIT(20) ++#define RCC_MP_APB5LPENSETR_STGENCSTPEN BIT(21) ++ ++/* RCC_MP_APB5LPENCLRR register fields */ ++#define RCC_MP_APB5LPENCLRR_RTCAPBLPEN BIT(8) ++#define RCC_MP_APB5LPENCLRR_TZCLPEN BIT(11) ++#define RCC_MP_APB5LPENCLRR_ETZPCLPEN BIT(13) ++#define RCC_MP_APB5LPENCLRR_IWDG1APBLPEN BIT(15) ++#define RCC_MP_APB5LPENCLRR_BSECLPEN BIT(16) ++#define RCC_MP_APB5LPENCLRR_STGENCLPEN BIT(20) ++#define RCC_MP_APB5LPENCLRR_STGENCSTPEN BIT(21) ++ ++/* RCC_MP_APB6LPENSETR register fields */ ++#define RCC_MP_APB6LPENSETR_USART1LPEN BIT(0) ++#define RCC_MP_APB6LPENSETR_USART2LPEN BIT(1) ++#define RCC_MP_APB6LPENSETR_SPI4LPEN BIT(2) ++#define RCC_MP_APB6LPENSETR_SPI5LPEN BIT(3) ++#define RCC_MP_APB6LPENSETR_I2C3LPEN BIT(4) ++#define RCC_MP_APB6LPENSETR_I2C4LPEN BIT(5) ++#define RCC_MP_APB6LPENSETR_I2C5LPEN BIT(6) ++#define RCC_MP_APB6LPENSETR_TIM12LPEN BIT(7) ++#define RCC_MP_APB6LPENSETR_TIM13LPEN BIT(8) ++#define RCC_MP_APB6LPENSETR_TIM14LPEN BIT(9) ++#define RCC_MP_APB6LPENSETR_TIM15LPEN BIT(10) ++#define RCC_MP_APB6LPENSETR_TIM16LPEN BIT(11) ++#define RCC_MP_APB6LPENSETR_TIM17LPEN BIT(12) ++ ++/* RCC_MP_APB6LPENCLRR register fields */ ++#define RCC_MP_APB6LPENCLRR_USART1LPEN BIT(0) ++#define RCC_MP_APB6LPENCLRR_USART2LPEN BIT(1) ++#define RCC_MP_APB6LPENCLRR_SPI4LPEN BIT(2) ++#define RCC_MP_APB6LPENCLRR_SPI5LPEN BIT(3) ++#define RCC_MP_APB6LPENCLRR_I2C3LPEN BIT(4) ++#define RCC_MP_APB6LPENCLRR_I2C4LPEN BIT(5) ++#define RCC_MP_APB6LPENCLRR_I2C5LPEN BIT(6) ++#define RCC_MP_APB6LPENCLRR_TIM12LPEN BIT(7) ++#define RCC_MP_APB6LPENCLRR_TIM13LPEN BIT(8) ++#define RCC_MP_APB6LPENCLRR_TIM14LPEN BIT(9) ++#define RCC_MP_APB6LPENCLRR_TIM15LPEN BIT(10) ++#define RCC_MP_APB6LPENCLRR_TIM16LPEN BIT(11) ++#define RCC_MP_APB6LPENCLRR_TIM17LPEN BIT(12) ++ ++/* RCC_MP_AHB2LPENSETR register fields */ ++#define RCC_MP_AHB2LPENSETR_DMA1LPEN BIT(0) ++#define RCC_MP_AHB2LPENSETR_DMA2LPEN BIT(1) ++#define RCC_MP_AHB2LPENSETR_DMAMUX1LPEN BIT(2) ++#define RCC_MP_AHB2LPENSETR_DMA3LPEN BIT(3) ++#define RCC_MP_AHB2LPENSETR_DMAMUX2LPEN BIT(4) ++#define RCC_MP_AHB2LPENSETR_ADC1LPEN BIT(5) ++#define RCC_MP_AHB2LPENSETR_ADC2LPEN BIT(6) ++#define RCC_MP_AHB2LPENSETR_USBOLPEN BIT(8) ++ ++/* RCC_MP_AHB2LPENCLRR register fields */ ++#define RCC_MP_AHB2LPENCLRR_DMA1LPEN BIT(0) ++#define RCC_MP_AHB2LPENCLRR_DMA2LPEN BIT(1) ++#define RCC_MP_AHB2LPENCLRR_DMAMUX1LPEN BIT(2) ++#define RCC_MP_AHB2LPENCLRR_DMA3LPEN BIT(3) ++#define RCC_MP_AHB2LPENCLRR_DMAMUX2LPEN BIT(4) ++#define RCC_MP_AHB2LPENCLRR_ADC1LPEN BIT(5) ++#define RCC_MP_AHB2LPENCLRR_ADC2LPEN BIT(6) ++#define RCC_MP_AHB2LPENCLRR_USBOLPEN BIT(8) ++ ++/* RCC_MP_AHB4LPENSETR register fields */ ++#define RCC_MP_AHB4LPENSETR_TSCLPEN BIT(15) ++ ++/* RCC_MP_AHB4LPENCLRR register fields */ ++#define RCC_MP_AHB4LPENCLRR_TSCLPEN BIT(15) ++ ++/* RCC_MP_S_AHB4LPENSETR register fields */ ++#define RCC_MP_S_AHB4LPENSETR_GPIOALPEN BIT(0) ++#define RCC_MP_S_AHB4LPENSETR_GPIOBLPEN BIT(1) ++#define RCC_MP_S_AHB4LPENSETR_GPIOCLPEN BIT(2) ++#define RCC_MP_S_AHB4LPENSETR_GPIODLPEN BIT(3) ++#define RCC_MP_S_AHB4LPENSETR_GPIOELPEN BIT(4) ++#define RCC_MP_S_AHB4LPENSETR_GPIOFLPEN BIT(5) ++#define RCC_MP_S_AHB4LPENSETR_GPIOGLPEN BIT(6) ++#define RCC_MP_S_AHB4LPENSETR_GPIOHLPEN BIT(7) ++#define RCC_MP_S_AHB4LPENSETR_GPIOILPEN BIT(8) ++ ++/* RCC_MP_S_AHB4LPENCLRR register fields */ ++#define RCC_MP_S_AHB4LPENCLRR_GPIOALPEN BIT(0) ++#define RCC_MP_S_AHB4LPENCLRR_GPIOBLPEN BIT(1) ++#define RCC_MP_S_AHB4LPENCLRR_GPIOCLPEN BIT(2) ++#define RCC_MP_S_AHB4LPENCLRR_GPIODLPEN BIT(3) ++#define RCC_MP_S_AHB4LPENCLRR_GPIOELPEN BIT(4) ++#define RCC_MP_S_AHB4LPENCLRR_GPIOFLPEN BIT(5) ++#define RCC_MP_S_AHB4LPENCLRR_GPIOGLPEN BIT(6) ++#define RCC_MP_S_AHB4LPENCLRR_GPIOHLPEN BIT(7) ++#define RCC_MP_S_AHB4LPENCLRR_GPIOILPEN BIT(8) ++ ++/* RCC_MP_NS_AHB4LPENSETR register fields */ ++#define RCC_MP_NS_AHB4LPENSETR_GPIOALPEN BIT(0) ++#define RCC_MP_NS_AHB4LPENSETR_GPIOBLPEN BIT(1) ++#define RCC_MP_NS_AHB4LPENSETR_GPIOCLPEN BIT(2) ++#define RCC_MP_NS_AHB4LPENSETR_GPIODLPEN BIT(3) ++#define RCC_MP_NS_AHB4LPENSETR_GPIOELPEN BIT(4) ++#define RCC_MP_NS_AHB4LPENSETR_GPIOFLPEN BIT(5) ++#define RCC_MP_NS_AHB4LPENSETR_GPIOGLPEN BIT(6) ++#define RCC_MP_NS_AHB4LPENSETR_GPIOHLPEN BIT(7) ++#define RCC_MP_NS_AHB4LPENSETR_GPIOILPEN BIT(8) ++ ++/* RCC_MP_NS_AHB4LPENCLRR register fields */ ++#define RCC_MP_NS_AHB4LPENCLRR_GPIOALPEN BIT(0) ++#define RCC_MP_NS_AHB4LPENCLRR_GPIOBLPEN BIT(1) ++#define RCC_MP_NS_AHB4LPENCLRR_GPIOCLPEN BIT(2) ++#define RCC_MP_NS_AHB4LPENCLRR_GPIODLPEN BIT(3) ++#define RCC_MP_NS_AHB4LPENCLRR_GPIOELPEN BIT(4) ++#define RCC_MP_NS_AHB4LPENCLRR_GPIOFLPEN BIT(5) ++#define RCC_MP_NS_AHB4LPENCLRR_GPIOGLPEN BIT(6) ++#define RCC_MP_NS_AHB4LPENCLRR_GPIOHLPEN BIT(7) ++#define RCC_MP_NS_AHB4LPENCLRR_GPIOILPEN BIT(8) ++ ++/* RCC_MP_AHB5LPENSETR register fields */ ++#define RCC_MP_AHB5LPENSETR_PKALPEN BIT(2) ++#define RCC_MP_AHB5LPENSETR_SAESLPEN BIT(3) ++#define RCC_MP_AHB5LPENSETR_CRYP1LPEN BIT(4) ++#define RCC_MP_AHB5LPENSETR_HASH1LPEN BIT(5) ++#define RCC_MP_AHB5LPENSETR_RNG1LPEN BIT(6) ++#define RCC_MP_AHB5LPENSETR_BKPSRAMLPEN BIT(8) ++ ++/* RCC_MP_AHB5LPENCLRR register fields */ ++#define RCC_MP_AHB5LPENCLRR_PKALPEN BIT(2) ++#define RCC_MP_AHB5LPENCLRR_SAESLPEN BIT(3) ++#define RCC_MP_AHB5LPENCLRR_CRYP1LPEN BIT(4) ++#define RCC_MP_AHB5LPENCLRR_HASH1LPEN BIT(5) ++#define RCC_MP_AHB5LPENCLRR_RNG1LPEN BIT(6) ++#define RCC_MP_AHB5LPENCLRR_BKPSRAMLPEN BIT(8) ++ ++/* RCC_MP_AHB6LPENSETR register fields */ ++#define RCC_MP_AHB6LPENSETR_MCELPEN BIT(1) ++#define RCC_MP_AHB6LPENSETR_ETH1CKLPEN BIT(7) ++#define RCC_MP_AHB6LPENSETR_ETH1TXLPEN BIT(8) ++#define RCC_MP_AHB6LPENSETR_ETH1RXLPEN BIT(9) ++#define RCC_MP_AHB6LPENSETR_ETH1MACLPEN BIT(10) ++#define RCC_MP_AHB6LPENSETR_ETH1STPEN BIT(11) ++#define RCC_MP_AHB6LPENSETR_FMCLPEN BIT(12) ++#define RCC_MP_AHB6LPENSETR_QSPILPEN BIT(14) ++#define RCC_MP_AHB6LPENSETR_SDMMC1LPEN BIT(16) ++#define RCC_MP_AHB6LPENSETR_SDMMC2LPEN BIT(17) ++#define RCC_MP_AHB6LPENSETR_CRC1LPEN BIT(20) ++#define RCC_MP_AHB6LPENSETR_USBHLPEN BIT(24) ++#define RCC_MP_AHB6LPENSETR_ETH2CKLPEN BIT(27) ++#define RCC_MP_AHB6LPENSETR_ETH2TXLPEN BIT(28) ++#define RCC_MP_AHB6LPENSETR_ETH2RXLPEN BIT(29) ++#define RCC_MP_AHB6LPENSETR_ETH2MACLPEN BIT(30) ++#define RCC_MP_AHB6LPENSETR_ETH2STPEN BIT(31) ++ ++/* RCC_MP_AHB6LPENCLRR register fields */ ++#define RCC_MP_AHB6LPENCLRR_MCELPEN BIT(1) ++#define RCC_MP_AHB6LPENCLRR_ETH1CKLPEN BIT(7) ++#define RCC_MP_AHB6LPENCLRR_ETH1TXLPEN BIT(8) ++#define RCC_MP_AHB6LPENCLRR_ETH1RXLPEN BIT(9) ++#define RCC_MP_AHB6LPENCLRR_ETH1MACLPEN BIT(10) ++#define RCC_MP_AHB6LPENCLRR_ETH1STPEN BIT(11) ++#define RCC_MP_AHB6LPENCLRR_FMCLPEN BIT(12) ++#define RCC_MP_AHB6LPENCLRR_QSPILPEN BIT(14) ++#define RCC_MP_AHB6LPENCLRR_SDMMC1LPEN BIT(16) ++#define RCC_MP_AHB6LPENCLRR_SDMMC2LPEN BIT(17) ++#define RCC_MP_AHB6LPENCLRR_CRC1LPEN BIT(20) ++#define RCC_MP_AHB6LPENCLRR_USBHLPEN BIT(24) ++#define RCC_MP_AHB6LPENCLRR_ETH2CKLPEN BIT(27) ++#define RCC_MP_AHB6LPENCLRR_ETH2TXLPEN BIT(28) ++#define RCC_MP_AHB6LPENCLRR_ETH2RXLPEN BIT(29) ++#define RCC_MP_AHB6LPENCLRR_ETH2MACLPEN BIT(30) ++#define RCC_MP_AHB6LPENCLRR_ETH2STPEN BIT(31) ++ ++/* RCC_MP_S_AHB6LPENSETR register fields */ ++#define RCC_MP_S_AHB6LPENSETR_MDMALPEN BIT(0) ++ ++/* RCC_MP_S_AHB6LPENCLRR register fields */ ++#define RCC_MP_S_AHB6LPENCLRR_MDMALPEN BIT(0) ++ ++/* RCC_MP_NS_AHB6LPENSETR register fields */ ++#define RCC_MP_NS_AHB6LPENSETR_MDMALPEN BIT(0) ++ ++/* RCC_MP_NS_AHB6LPENCLRR register fields */ ++#define RCC_MP_NS_AHB6LPENCLRR_MDMALPEN BIT(0) ++ ++/* RCC_MP_S_AXIMLPENSETR register fields */ ++#define RCC_MP_S_AXIMLPENSETR_SYSRAMLPEN BIT(0) ++ ++/* RCC_MP_S_AXIMLPENCLRR register fields */ ++#define RCC_MP_S_AXIMLPENCLRR_SYSRAMLPEN BIT(0) ++ ++/* RCC_MP_NS_AXIMLPENSETR register fields */ ++#define RCC_MP_NS_AXIMLPENSETR_SYSRAMLPEN BIT(0) ++ ++/* RCC_MP_NS_AXIMLPENCLRR register fields */ ++#define RCC_MP_NS_AXIMLPENCLRR_SYSRAMLPEN BIT(0) ++ ++/* RCC_MP_MLAHBLPENSETR register fields */ ++#define RCC_MP_MLAHBLPENSETR_SRAM1LPEN BIT(0) ++#define RCC_MP_MLAHBLPENSETR_SRAM2LPEN BIT(1) ++#define RCC_MP_MLAHBLPENSETR_SRAM3LPEN BIT(2) ++ ++/* RCC_MP_MLAHBLPENCLRR register fields */ ++#define RCC_MP_MLAHBLPENCLRR_SRAM1LPEN BIT(0) ++#define RCC_MP_MLAHBLPENCLRR_SRAM2LPEN BIT(1) ++#define RCC_MP_MLAHBLPENCLRR_SRAM3LPEN BIT(2) ++ ++/* RCC_APB3SECSR register fields */ ++#define RCC_APB3SECSR_LPTIM2SECF BIT(0) ++#define RCC_APB3SECSR_LPTIM3SECF BIT(1) ++#define RCC_APB3SECSR_VREFSECF BIT(13) ++ ++/* RCC_APB4SECSR register fields */ ++#define RCC_APB4SECSR_DCMIPPSECF BIT(1) ++#define RCC_APB4SECSR_USBPHYSECF BIT(16) ++ ++/* RCC_APB5SECSR register fields */ ++#define RCC_APB5SECSR_RTCSECF BIT(8) ++#define RCC_APB5SECSR_TZCSECF BIT(11) ++#define RCC_APB5SECSR_ETZPCSECF BIT(13) ++#define RCC_APB5SECSR_IWDG1SECF BIT(15) ++#define RCC_APB5SECSR_BSECSECF BIT(16) ++#define RCC_APB5SECSR_STGENCSECF_MASK GENMASK_32(21, 20) ++#define RCC_APB5SECSR_STGENCSECF_SHIFT 20 ++ ++/* RCC_APB6SECSR register fields */ ++#define RCC_APB6SECSR_USART1SECF BIT(0) ++#define RCC_APB6SECSR_USART2SECF BIT(1) ++#define RCC_APB6SECSR_SPI4SECF BIT(2) ++#define RCC_APB6SECSR_SPI5SECF BIT(3) ++#define RCC_APB6SECSR_I2C3SECF BIT(4) ++#define RCC_APB6SECSR_I2C4SECF BIT(5) ++#define RCC_APB6SECSR_I2C5SECF BIT(6) ++#define RCC_APB6SECSR_TIM12SECF BIT(7) ++#define RCC_APB6SECSR_TIM13SECF BIT(8) ++#define RCC_APB6SECSR_TIM14SECF BIT(9) ++#define RCC_APB6SECSR_TIM15SECF BIT(10) ++#define RCC_APB6SECSR_TIM16SECF BIT(11) ++#define RCC_APB6SECSR_TIM17SECF BIT(12) ++ ++/* RCC_AHB2SECSR register fields */ ++#define RCC_AHB2SECSR_DMA3SECF BIT(3) ++#define RCC_AHB2SECSR_DMAMUX2SECF BIT(4) ++#define RCC_AHB2SECSR_ADC1SECF BIT(5) ++#define RCC_AHB2SECSR_ADC2SECF BIT(6) ++#define RCC_AHB2SECSR_USBOSECF BIT(8) ++ ++/* RCC_AHB4SECSR register fields */ ++#define RCC_AHB4SECSR_TSCSECF BIT(15) ++ ++/* RCC_AHB5SECSR register fields */ ++#define RCC_AHB5SECSR_PKASECF BIT(2) ++#define RCC_AHB5SECSR_SAESSECF BIT(3) ++#define RCC_AHB5SECSR_CRYP1SECF BIT(4) ++#define RCC_AHB5SECSR_HASH1SECF BIT(5) ++#define RCC_AHB5SECSR_RNG1SECF BIT(6) ++#define RCC_AHB5SECSR_BKPSRAMSECF BIT(8) ++ ++/* RCC_AHB6SECSR register fields */ ++#define RCC_AHB6SECSR_MCESECF BIT(1) ++#define RCC_AHB6SECSR_ETH1SECF_MASK GENMASK_32(11, 7) ++#define RCC_AHB6SECSR_ETH1SECF_SHIFT 7 ++#define RCC_AHB6SECSR_FMCSECF BIT(12) ++#define RCC_AHB6SECSR_QSPISECF BIT(14) ++#define RCC_AHB6SECSR_SDMMC1SECF BIT(16) ++#define RCC_AHB6SECSR_SDMMC2SECF BIT(17) ++#define RCC_AHB6SECSR_ETH2SECF_MASK GENMASK_32(31, 27) ++#define RCC_AHB6SECSR_ETH2SECF_SHIFT 27 ++ ++/* RCC_VERR register fields */ ++#define RCC_VERR_MINREV_MASK GENMASK_32(3, 0) ++#define RCC_VERR_MINREV_SHIFT 0 ++#define RCC_VERR_MAJREV_MASK GENMASK_32(7, 4) ++#define RCC_VERR_MAJREV_SHIFT 4 ++ ++/* RCC_IDR register fields */ ++#define RCC_IDR_ID_MASK GENMASK_32(31, 0) ++#define RCC_IDR_ID_SHIFT 0 ++ ++/* RCC_SIDR register fields */ ++#define RCC_SIDR_SID_MASK GENMASK_32(31, 0) ++#define RCC_SIDR_SID_SHIFT 0 ++ ++/* Used for all RCC_PLLCR registers */ ++#define RCC_PLLNCR_PLLON BIT(0) ++#define RCC_PLLNCR_PLLRDY BIT(1) ++#define RCC_PLLNCR_SSCG_CTRL BIT(2) ++#define RCC_PLLNCR_DIVPEN BIT(4) ++#define RCC_PLLNCR_DIVQEN BIT(5) ++#define RCC_PLLNCR_DIVREN BIT(6) ++#define RCC_PLLNCR_DIVEN_SHIFT 4 ++ ++/* Used for all RCC_PLLCFGR1 registers */ ++#define RCC_PLLNCFGR1_DIVM_SHIFT 16 ++#define RCC_PLLNCFGR1_DIVM_MASK GENMASK_32(21, 16) ++#define RCC_PLLNCFGR1_DIVN_SHIFT 0 ++#define RCC_PLLNCFGR1_DIVN_MASK GENMASK_32(8, 0) ++ ++/* Only for PLL3 and PLL4 */ ++#define RCC_PLLNCFGR1_IFRGE_SHIFT 24 ++#define RCC_PLLNCFGR1_IFRGE_MASK GENMASK_32(25, 24) ++ ++/* Used for all RCC_PLLCFGR2 registers */ ++#define RCC_PLLNCFGR2_DIVX_MASK GENMASK_32(6, 0) ++#define RCC_PLLNCFGR2_DIVP_SHIFT 0 ++#define RCC_PLLNCFGR2_DIVP_MASK GENMASK_32(6, 0) ++#define RCC_PLLNCFGR2_DIVQ_SHIFT 8 ++#define RCC_PLLNCFGR2_DIVQ_MASK GENMASK_32(14, 8) ++#define RCC_PLLNCFGR2_DIVR_SHIFT 16 ++#define RCC_PLLNCFGR2_DIVR_MASK GENMASK_32(22, 16) ++ ++/* Used for all RCC_PLLFRACR registers */ ++#define RCC_PLLNFRACR_FRACV_SHIFT 3 ++#define RCC_PLLNFRACR_FRACV_MASK GENMASK_32(15, 3) ++#define RCC_PLLNFRACR_FRACLE BIT(16) ++ ++/* Used for all RCC_PLLCSGR registers */ ++#define RCC_PLLNCSGR_INC_STEP_SHIFT 16 ++#define RCC_PLLNCSGR_INC_STEP_MASK GENMASK_32(30, 16) ++#define RCC_PLLNCSGR_MOD_PER_SHIFT 0 ++#define RCC_PLLNCSGR_MOD_PER_MASK GENMASK_32(12, 0) ++#define RCC_PLLNCSGR_SSCG_MODE_SHIFT 15 ++#define RCC_PLLNCSGR_SSCG_MODE_MASK BIT(15) ++ ++/* Used for most of RCC_SELR registers */ ++#define RCC_SELR_SRC_MASK GENMASK_32(2, 0) ++#define RCC_SELR_REFCLK_SRC_MASK GENMASK_32(1, 0) ++#define RCC_SELR_SRCRDY BIT(31) ++ ++/* Values of RCC_MPCKSELR register */ ++#define RCC_MPCKSELR_HSI 0x00000000 ++#define RCC_MPCKSELR_HSE 0x00000001 ++#define RCC_MPCKSELR_PLL 0x00000002 ++#define RCC_MPCKSELR_PLL_MPUDIV 0x00000003 ++ ++/* Values of RCC_ASSCKSELR register */ ++#define RCC_ASSCKSELR_HSI 0x00000000 ++#define RCC_ASSCKSELR_HSE 0x00000001 ++#define RCC_ASSCKSELR_PLL 0x00000002 ++ ++/* Values of RCC_MSSCKSELR register */ ++#define RCC_MSSCKSELR_HSI 0x00000000 ++#define RCC_MSSCKSELR_HSE 0x00000001 ++#define RCC_MSSCKSELR_CSI 0x00000002 ++#define RCC_MSSCKSELR_PLL 0x00000003 ++ ++/* Values of RCC_CPERCKSELR register */ ++#define RCC_CPERCKSELR_HSI 0x00000000 ++#define RCC_CPERCKSELR_CSI 0x00000001 ++#define RCC_CPERCKSELR_HSE 0x00000002 ++ ++/* Used for most of DIVR register: max div for RTC */ ++#define RCC_DIVR_DIV_MASK GENMASK_32(5, 0) ++#define RCC_DIVR_DIVRDY BIT(31) ++ ++/* Masks for specific DIVR registers */ ++#define RCC_APBXDIV_MASK GENMASK_32(2, 0) ++#define RCC_MPUDIV_MASK GENMASK_32(2, 0) ++#define RCC_AXIDIV_MASK GENMASK_32(2, 0) ++#define RCC_MLAHBDIV_MASK GENMASK_32(3, 0) ++ ++/* Used for TIMER Prescaler */ ++#define RCC_TIMGXPRER_TIMGXPRE BIT(0) ++ ++/* Offset between RCC_MP_xxxENSETR and RCC_MP_xxxENCLRR registers */ ++#define RCC_MP_ENCLRR_OFFSET U(4) ++ ++/* Offset between RCC_xxxRSTSETR and RCC_xxxRSTCLRR registers */ ++#define RCC_RSTCLRR_OFFSET U(4) ++ ++/* RCC_OCENSETR register fields */ ++#define RCC_OCENR_HSION BIT(0) ++#define RCC_OCENR_HSIKERON BIT(1) ++#define RCC_OCENR_CSION BIT(4) ++#define RCC_OCENR_CSIKERON BIT(5) ++#define RCC_OCENR_DIGBYP BIT(7) ++#define RCC_OCENR_HSEON BIT(8) ++#define RCC_OCENR_HSEKERON BIT(9) ++#define RCC_OCENR_HSEBYP BIT(10) ++#define RCC_OCENR_HSECSSON BIT(11) ++ ++#define RCC_OCENR_DIGBYP_BIT 7 ++#define RCC_OCENR_HSEBYP_BIT 10 ++#define RCC_OCENR_HSECSSON_BIT 11 ++ ++/* Used for RCC_MCO related operations */ ++#define RCC_MCOCFG_MCOON BIT(12) ++#define RCC_MCOCFG_MCODIV_MASK GENMASK_32(7, 4) ++#define RCC_MCOCFG_MCODIV_SHIFT 4 ++#define RCC_MCOCFG_MCOSRC_MASK GENMASK_32(2, 0) ++ ++#define RCC_UART4CKSELR_HSI 0x00000002 ++ ++#define RCC_CPERCKSELR_PERSRC_MASK GENMASK_32(1, 0) ++#define RCC_CPERCKSELR_PERSRC_SHIFT 0 ++ ++#define RCC_USBCKSELR_USBOSRC_MASK BIT(4) ++#define RCC_USBCKSELR_USBOSRC_SHIFT 4 ++ ++#define RCC_DDRITFCR_DDRCKMOD_SSR 0 ++#define RCC_DDRITFCR_DDRCKMOD_ASR1 BIT(20) ++#define RCC_DDRITFCR_DDRCKMOD_HSR1 BIT(21) ++ ++#define RCC_DDRITFCR_DDRC2EN BIT(0) ++#define RCC_DDRITFCR_DDRC2LPEN BIT(1) ++ ++#define RCC_MP_CIFR_MASK U(0x110F1F) ++#define RCC_OFFSET_MASK GENMASK_32(11, 0) ++ ++#include ++ ++vaddr_t stm32_rcc_base(void); ++ ++#endif /*__DRIVERS_STM32MP13_RCC_H__*/ +diff --git a/core/include/drivers/stm32mp1_rcc.h b/core/include/drivers/stm32mp1_rcc.h +index b0411183e..a9e0ecea7 100644 +--- a/core/include/drivers/stm32mp1_rcc.h ++++ b/core/include/drivers/stm32mp1_rcc.h +@@ -6,8 +6,6 @@ + #ifndef __DRIVERS_STM32MP1_RCC_H__ + #define __DRIVERS_STM32MP1_RCC_H__ + +-#include +-#include + #include + + #define RCC_TZCR 0x00 +@@ -455,6 +453,9 @@ + #define RCC_MP_SREQCLRR_STPREQ_P0 BIT(0) + #define RCC_MP_SREQCLRR_STPREQ_P1 BIT(1) + ++/* Values of RCC_PWRLPDLYCR register */ ++#define RCC_PWRLPDLYCR_PWRLP_DLY_MASK GENMASK_32(21, 0) ++ + /* Global Control Register */ + #define RCC_MP_GCR_BOOT_MCU BIT(0) + +@@ -511,6 +512,9 @@ + #define RCC_AHB5RSTSETR_RNG1RST BIT(6) + #define RCC_AHB5RSTSETR_AXIMCRST BIT(16) + ++/* RCC_MP_AHB6RST(SET|CLR)R bit fields */ ++#define RCC_AHB6RSTSETR_GPURST BIT(5) ++ + /* RCC_MP_AHB5EN(SET|CLR)R bit fields */ + #define RCC_MP_AHB5ENSETR_GPIOZEN_POS 0 + #define RCC_MP_AHB5ENSETR_CRYP1EN_POS 4 +@@ -545,6 +549,10 @@ + #define DT_RCC_SECURE_CLK_COMPAT "st,stm32mp1-rcc-secure" + + #ifndef __ASSEMBLER__ ++#include ++#include ++#include ++ + vaddr_t stm32_rcc_base(void); + + static inline bool stm32_rcc_is_secure(void) +diff --git a/core/include/drivers/stm32mp_dt_bindings.h b/core/include/drivers/stm32mp_dt_bindings.h +new file mode 100644 +index 000000000..a36edc9d7 +--- /dev/null ++++ b/core/include/drivers/stm32mp_dt_bindings.h +@@ -0,0 +1,26 @@ ++/* SPDX-License-Identifier: BSD-2-Clause */ ++/* ++ * Copyright (c) 2021, STMicroelectronics ++ */ ++#ifndef STM32MP_DT_BINDINGS_H ++#define STM32MP_DT_BINDINGS_H ++ ++#ifdef CFG_STM32MP13 ++#include ++#include ++#include ++#include ++#include ++#endif ++ ++#ifdef CFG_STM32MP15 ++#include ++#include ++#include ++#include ++#include ++#endif ++ ++#include ++ ++#endif /* STM32MP_DT_BINDINGS_H */ +diff --git a/core/include/drivers/stpmic1.h b/core/include/drivers/stpmic1.h +index 7840efb4d..5fe48232f 100644 +--- a/core/include/drivers/stpmic1.h ++++ b/core/include/drivers/stpmic1.h +@@ -102,6 +102,22 @@ + #define BUCK4_PULL_DOWN_SHIFT 6 + #define VREF_DDR_PULL_DOWN_SHIFT 4 + ++/* ICC register */ ++#define BUCK1_ICC_SHIFT 0 ++#define BUCK2_ICC_SHIFT 1 ++#define BUCK3_ICC_SHIFT 2 ++#define BUCK4_ICC_SHIFT 3 ++#define PWR_SW1_ICC_SHIFT 4 ++#define PWR_SW2_ICC_SHIFT 5 ++#define BOOST_ICC_SHIFT 6 ++ ++#define LDO1_ICC_SHIFT 0 ++#define LDO2_ICC_SHIFT 1 ++#define LDO3_ICC_SHIFT 2 ++#define LDO4_ICC_SHIFT 3 ++#define LDO5_ICC_SHIFT 4 ++#define LDO6_ICC_SHIFT 5 ++ + /* Buck Mask reset register */ + #define BUCK1_MASK_RESET_SHIFT 0 + #define BUCK2_MASK_RESET_SHIFT 1 +@@ -117,6 +133,10 @@ + #define LDO6_MASK_RESET_SHIFT 5 + #define VREF_DDR_MASK_RESET_SHIFT 6 + ++/* LDO3 Special modes */ ++#define LDO3_BYPASS BIT(7) ++#define LDO3_DDR_SEL 31U ++ + /* Main PMIC Control Register (MAIN_CONTROL_REG) */ + #define ICC_EVENT_ENABLED BIT(4) + #define PWRCTRL_POLARITY_HIGH BIT(3) +@@ -144,11 +164,48 @@ + /* USB Control Register */ + #define BOOST_OVP_DISABLED_POS 7 + #define VBUS_OTG_DETECTION_DISABLED_POS 6 ++#define SW_OUT_DISCHARGE_POS 5 ++#define VBUS_OTG_DISCHARGE_POS 4 + #define OCP_LIMIT_HIGH_POS 3 + #define SWIN_SWOUT_ENABLED_POS 2 + #define USBSW_OTG_SWITCH_ENABLED_POS 1 + #define BOOST_ENABLED_POS 0 + ++/* IRQ definitions */ ++#define IT_PONKEY_F 0 ++#define IT_PONKEY_R 1 ++#define IT_WAKEUP_F 2 ++#define IT_WAKEUP_R 3 ++#define IT_VBUS_OTG_F 4 ++#define IT_VBUS_OTG_R 5 ++#define IT_SWOUT_F 6 ++#define IT_SWOUT_R 7 ++ ++#define IT_CURLIM_BUCK1 0 ++#define IT_CURLIM_BUCK2 1 ++#define IT_CURLIM_BUCK3 2 ++#define IT_CURLIM_BUCK4 3 ++#define IT_OCP_OTG 4 ++#define IT_OCP_SWOUT 5 ++#define IT_OCP_BOOST 6 ++#define IT_OVP_BOOST 7 ++ ++#define IT_CURLIM_LDO1 0 ++#define IT_CURLIM_LDO2 1 ++#define IT_CURLIM_LDO3 2 ++#define IT_CURLIM_LDO4 3 ++#define IT_CURLIM_LDO5 4 ++#define IT_CURLIM_LDO6 5 ++#define IT_SHORT_SWOTG 6 ++#define IT_SHORT_SWOUT 7 ++ ++#define IT_TWARN_F 0 ++#define IT_TWARN_R 1 ++#define IT_VINLOW_F 2 ++#define IT_VINLOW_R 3 ++#define IT_SWIN_F 4 ++#define IT_SWIN_R 5 ++ + /* + * Bind SPMIC1 device driver with a specific I2C bus instance + * @i2c_handle: target I2C instance to use +@@ -179,21 +236,30 @@ int stpmic1_regulator_enable(const char *name); + int stpmic1_regulator_disable(const char *name); + bool stpmic1_is_regulator_enabled(const char *name); + +-void stpmic1_regulator_levels_mv(const char *name, +- const uint16_t **levels, +- size_t *levels_count); ++TEE_Result stpmic1_regulator_levels_mv(const char *name, ++ const uint16_t **levels, ++ size_t *levels_count); + + /* API for voltage cnotrol of regulators driven from STPMIC1 device */ + int stpmic1_regulator_voltage_set(const char *name, uint16_t millivolts); + int stpmic1_regulator_voltage_get(const char *name); ++int stpmic1_regulator_pull_down_set(const char *name); + int stpmic1_regulator_mask_reset_set(const char *name); + ++/* LDOx specific configurations */ ++int stpmic1_regulator_icc_set(const char *name); ++int stpmic1_regulator_sink_mode_set(const char *name); ++int stpmic1_regulator_bypass_mode_set(const char *name); ++int stpmic1_active_discharge_mode_set(const char *name); ++ + /* API for low power configuration of regulators driven from STPMIC1 device */ ++bool stpmic1_regu_has_lp_cfg(const char *name); + int stpmic1_lp_copy_reg(const char *name); + int stpmic1_lp_reg_on_off(const char *name, uint8_t enable); + int stpmic1_lp_set_mode(const char *name, uint8_t hplp); + int stpmic1_lp_set_voltage(const char *name, uint16_t millivolts); + ++#ifdef CFG_STM32MP15 + /* + * Specific API for controlling regulators driven from STPMIC1 device + * from unpaged execution context of the STPMIC1 driver. +@@ -244,7 +310,6 @@ int stpmic1_bo_pull_down_unpg(struct stpmic1_bo_cfg *cfg); + int stpmic1_bo_mask_reset_cfg(const char *name, struct stpmic1_bo_cfg *cfg); + int stpmic1_bo_mask_reset_unpg(struct stpmic1_bo_cfg *cfg); + +-bool stpmic1_regu_has_lp_cfg(const char *name); + int stpmic1_lp_cfg(const char *name, struct stpmic1_lp_cfg *cfg); + int stpmic1_lp_load_unpg(struct stpmic1_lp_cfg *cfg); + int stpmic1_lp_on_off_unpg(struct stpmic1_lp_cfg *cfg, int enable); +@@ -253,5 +318,6 @@ int stpmic1_lp_mode_unpg(struct stpmic1_lp_cfg *cfg, + int stpmic1_lp_voltage_cfg(const char *name, uint16_t millivolts, + struct stpmic1_lp_cfg *cfg); + int stpmic1_lp_voltage_unpg(struct stpmic1_lp_cfg *cfg); ++#endif /* CFG_STM32MP15 */ + + #endif /*__STPMIC1_H__*/ +diff --git a/core/include/dt-bindings/clock/stm32mp1-clks.h b/core/include/dt-bindings/clock/stm32mp1-clks.h +index 39bf83639..615152e7f 100644 +--- a/core/include/dt-bindings/clock/stm32mp1-clks.h ++++ b/core/include/dt-bindings/clock/stm32mp1-clks.h +@@ -252,30 +252,26 @@ + #define ETHMAC_K ETHCK_K + + /* SCMI clock identifiers */ +-#define CK_SCMI0_HSE 0 +-#define CK_SCMI0_HSI 1 +-#define CK_SCMI0_CSI 2 +-#define CK_SCMI0_LSE 3 +-#define CK_SCMI0_LSI 4 +-#define CK_SCMI0_PLL2_Q 5 +-#define CK_SCMI0_PLL2_R 6 +-#define CK_SCMI0_MPU 7 +-#define CK_SCMI0_AXI 8 +-#define CK_SCMI0_BSEC 9 +-#define CK_SCMI0_CRYP1 10 +-#define CK_SCMI0_GPIOZ 11 +-#define CK_SCMI0_HASH1 12 +-#define CK_SCMI0_I2C4 13 +-#define CK_SCMI0_I2C6 14 +-#define CK_SCMI0_IWDG1 15 +-#define CK_SCMI0_RNG1 16 +-#define CK_SCMI0_RTC 17 +-#define CK_SCMI0_RTCAPB 18 +-#define CK_SCMI0_SPI6 19 +-#define CK_SCMI0_USART1 20 +- +-#define CK_SCMI1_PLL3_Q 0 +-#define CK_SCMI1_PLL3_R 1 +-#define CK_SCMI1_MCU 2 ++#define CK_SCMI_HSE 0 ++#define CK_SCMI_HSI 1 ++#define CK_SCMI_CSI 2 ++#define CK_SCMI_LSE 3 ++#define CK_SCMI_LSI 4 ++#define CK_SCMI_PLL2_Q 5 ++#define CK_SCMI_PLL2_R 6 ++#define CK_SCMI_MPU 7 ++#define CK_SCMI_AXI 8 ++#define CK_SCMI_BSEC 9 ++#define CK_SCMI_CRYP1 10 ++#define CK_SCMI_GPIOZ 11 ++#define CK_SCMI_HASH1 12 ++#define CK_SCMI_I2C4 13 ++#define CK_SCMI_I2C6 14 ++#define CK_SCMI_IWDG1 15 ++#define CK_SCMI_RNG1 16 ++#define CK_SCMI_RTC 17 ++#define CK_SCMI_RTCAPB 18 ++#define CK_SCMI_SPI6 19 ++#define CK_SCMI_USART1 20 + + #endif /* _DT_BINDINGS_STM32MP1_CLKS_H_ */ +diff --git a/core/include/dt-bindings/clock/stm32mp1-clksrc.h b/core/include/dt-bindings/clock/stm32mp1-clksrc.h +new file mode 100644 +index 000000000..de7d1604c +--- /dev/null ++++ b/core/include/dt-bindings/clock/stm32mp1-clksrc.h +@@ -0,0 +1,284 @@ ++/* ++ * Copyright (C) 2017, STMicroelectronics - All Rights Reserved ++ * ++ * SPDX-License-Identifier: GPL-2.0+ BSD-3-Clause ++ */ ++ ++#ifndef _DT_BINDINGS_CLOCK_STM32MP1_CLKSRC_H_ ++#define _DT_BINDINGS_CLOCK_STM32MP1_CLKSRC_H_ ++ ++/* PLL output is enable when x=1, with x=p,q or r */ ++#define PQR(p, q, r) (((p) & 1) | (((q) & 1) << 1) | (((r) & 1) << 2)) ++ ++/* st,clksrc: mandatory clock source */ ++ ++#define CLK_MPU_HSI 0x00000200 ++#define CLK_MPU_HSE 0x00000201 ++#define CLK_MPU_PLL1P 0x00000202 ++#define CLK_MPU_PLL1P_DIV 0x00000203 ++ ++#define CLK_AXI_HSI 0x00000240 ++#define CLK_AXI_HSE 0x00000241 ++#define CLK_AXI_PLL2P 0x00000242 ++ ++#define CLK_MCU_HSI 0x00000480 ++#define CLK_MCU_HSE 0x00000481 ++#define CLK_MCU_CSI 0x00000482 ++#define CLK_MCU_PLL3P 0x00000483 ++ ++#define CLK_PLL12_HSI 0x00000280 ++#define CLK_PLL12_HSE 0x00000281 ++ ++#define CLK_PLL3_HSI 0x00008200 ++#define CLK_PLL3_HSE 0x00008201 ++#define CLK_PLL3_CSI 0x00008202 ++ ++#define CLK_PLL4_HSI 0x00008240 ++#define CLK_PLL4_HSE 0x00008241 ++#define CLK_PLL4_CSI 0x00008242 ++#define CLK_PLL4_I2SCKIN 0x00008243 ++ ++#define CLK_RTC_DISABLED 0x00001400 ++#define CLK_RTC_LSE 0x00001401 ++#define CLK_RTC_LSI 0x00001402 ++#define CLK_RTC_HSE 0x00001403 ++ ++#define CLK_MCO1_HSI 0x00008000 ++#define CLK_MCO1_HSE 0x00008001 ++#define CLK_MCO1_CSI 0x00008002 ++#define CLK_MCO1_LSI 0x00008003 ++#define CLK_MCO1_LSE 0x00008004 ++#define CLK_MCO1_DISABLED 0x0000800F ++ ++#define CLK_MCO2_MPU 0x00008040 ++#define CLK_MCO2_AXI 0x00008041 ++#define CLK_MCO2_MCU 0x00008042 ++#define CLK_MCO2_PLL4P 0x00008043 ++#define CLK_MCO2_HSE 0x00008044 ++#define CLK_MCO2_HSI 0x00008045 ++#define CLK_MCO2_DISABLED 0x0000804F ++ ++/* st,pkcs: peripheral kernel clock source */ ++ ++#define CLK_I2C12_PCLK1 0x00008C00 ++#define CLK_I2C12_PLL4R 0x00008C01 ++#define CLK_I2C12_HSI 0x00008C02 ++#define CLK_I2C12_CSI 0x00008C03 ++#define CLK_I2C12_DISABLED 0x00008C07 ++ ++#define CLK_I2C35_PCLK1 0x00008C40 ++#define CLK_I2C35_PLL4R 0x00008C41 ++#define CLK_I2C35_HSI 0x00008C42 ++#define CLK_I2C35_CSI 0x00008C43 ++#define CLK_I2C35_DISABLED 0x00008C47 ++ ++#define CLK_I2C46_PCLK5 0x00000C00 ++#define CLK_I2C46_PLL3Q 0x00000C01 ++#define CLK_I2C46_HSI 0x00000C02 ++#define CLK_I2C46_CSI 0x00000C03 ++#define CLK_I2C46_DISABLED 0x00000C07 ++ ++#define CLK_SAI1_PLL4Q 0x00008C80 ++#define CLK_SAI1_PLL3Q 0x00008C81 ++#define CLK_SAI1_I2SCKIN 0x00008C82 ++#define CLK_SAI1_CKPER 0x00008C83 ++#define CLK_SAI1_PLL3R 0x00008C84 ++#define CLK_SAI1_DISABLED 0x00008C87 ++ ++#define CLK_SAI2_PLL4Q 0x00008CC0 ++#define CLK_SAI2_PLL3Q 0x00008CC1 ++#define CLK_SAI2_I2SCKIN 0x00008CC2 ++#define CLK_SAI2_CKPER 0x00008CC3 ++#define CLK_SAI2_SPDIF 0x00008CC4 ++#define CLK_SAI2_PLL3R 0x00008CC5 ++#define CLK_SAI2_DISABLED 0x00008CC7 ++ ++#define CLK_SAI3_PLL4Q 0x00008D00 ++#define CLK_SAI3_PLL3Q 0x00008D01 ++#define CLK_SAI3_I2SCKIN 0x00008D02 ++#define CLK_SAI3_CKPER 0x00008D03 ++#define CLK_SAI3_PLL3R 0x00008D04 ++#define CLK_SAI3_DISABLED 0x00008D07 ++ ++#define CLK_SAI4_PLL4Q 0x00008D40 ++#define CLK_SAI4_PLL3Q 0x00008D41 ++#define CLK_SAI4_I2SCKIN 0x00008D42 ++#define CLK_SAI4_CKPER 0x00008D43 ++#define CLK_SAI4_PLL3R 0x00008D44 ++#define CLK_SAI4_DISABLED 0x00008D47 ++ ++#define CLK_SPI2S1_PLL4P 0x00008D80 ++#define CLK_SPI2S1_PLL3Q 0x00008D81 ++#define CLK_SPI2S1_I2SCKIN 0x00008D82 ++#define CLK_SPI2S1_CKPER 0x00008D83 ++#define CLK_SPI2S1_PLL3R 0x00008D84 ++#define CLK_SPI2S1_DISABLED 0x00008D87 ++ ++#define CLK_SPI2S23_PLL4P 0x00008DC0 ++#define CLK_SPI2S23_PLL3Q 0x00008DC1 ++#define CLK_SPI2S23_I2SCKIN 0x00008DC2 ++#define CLK_SPI2S23_CKPER 0x00008DC3 ++#define CLK_SPI2S23_PLL3R 0x00008DC4 ++#define CLK_SPI2S23_DISABLED 0x00008DC7 ++ ++#define CLK_SPI45_PCLK2 0x00008E00 ++#define CLK_SPI45_PLL4Q 0x00008E01 ++#define CLK_SPI45_HSI 0x00008E02 ++#define CLK_SPI45_CSI 0x00008E03 ++#define CLK_SPI45_HSE 0x00008E04 ++#define CLK_SPI45_DISABLED 0x00008E07 ++ ++#define CLK_SPI6_PCLK5 0x00000C40 ++#define CLK_SPI6_PLL4Q 0x00000C41 ++#define CLK_SPI6_HSI 0x00000C42 ++#define CLK_SPI6_CSI 0x00000C43 ++#define CLK_SPI6_HSE 0x00000C44 ++#define CLK_SPI6_PLL3Q 0x00000C45 ++#define CLK_SPI6_DISABLED 0x00000C47 ++ ++#define CLK_UART6_PCLK2 0x00008E40 ++#define CLK_UART6_PLL4Q 0x00008E41 ++#define CLK_UART6_HSI 0x00008E42 ++#define CLK_UART6_CSI 0x00008E43 ++#define CLK_UART6_HSE 0x00008E44 ++#define CLK_UART6_DISABLED 0x00008E47 ++ ++#define CLK_UART24_PCLK1 0x00008E80 ++#define CLK_UART24_PLL4Q 0x00008E81 ++#define CLK_UART24_HSI 0x00008E82 ++#define CLK_UART24_CSI 0x00008E83 ++#define CLK_UART24_HSE 0x00008E84 ++#define CLK_UART24_DISABLED 0x00008E87 ++ ++#define CLK_UART35_PCLK1 0x00008EC0 ++#define CLK_UART35_PLL4Q 0x00008EC1 ++#define CLK_UART35_HSI 0x00008EC2 ++#define CLK_UART35_CSI 0x00008EC3 ++#define CLK_UART35_HSE 0x00008EC4 ++#define CLK_UART35_DISABLED 0x00008EC7 ++ ++#define CLK_UART78_PCLK1 0x00008F00 ++#define CLK_UART78_PLL4Q 0x00008F01 ++#define CLK_UART78_HSI 0x00008F02 ++#define CLK_UART78_CSI 0x00008F03 ++#define CLK_UART78_HSE 0x00008F04 ++#define CLK_UART78_DISABLED 0x00008F07 ++ ++#define CLK_UART1_PCLK5 0x00000C80 ++#define CLK_UART1_PLL3Q 0x00000C81 ++#define CLK_UART1_HSI 0x00000C82 ++#define CLK_UART1_CSI 0x00000C83 ++#define CLK_UART1_PLL4Q 0x00000C84 ++#define CLK_UART1_HSE 0x00000C85 ++#define CLK_UART1_DISABLED 0x00000C87 ++ ++#define CLK_SDMMC12_HCLK6 0x00008F40 ++#define CLK_SDMMC12_PLL3R 0x00008F41 ++#define CLK_SDMMC12_PLL4P 0x00008F42 ++#define CLK_SDMMC12_HSI 0x00008F43 ++#define CLK_SDMMC12_DISABLED 0x00008F47 ++ ++#define CLK_SDMMC3_HCLK2 0x00008F80 ++#define CLK_SDMMC3_PLL3R 0x00008F81 ++#define CLK_SDMMC3_PLL4P 0x00008F82 ++#define CLK_SDMMC3_HSI 0x00008F83 ++#define CLK_SDMMC3_DISABLED 0x00008F87 ++ ++#define CLK_ETH_PLL4P 0x00008FC0 ++#define CLK_ETH_PLL3Q 0x00008FC1 ++#define CLK_ETH_DISABLED 0x00008FC3 ++ ++#define CLK_QSPI_ACLK 0x00009000 ++#define CLK_QSPI_PLL3R 0x00009001 ++#define CLK_QSPI_PLL4P 0x00009002 ++#define CLK_QSPI_CKPER 0x00009003 ++ ++#define CLK_FMC_ACLK 0x00009040 ++#define CLK_FMC_PLL3R 0x00009041 ++#define CLK_FMC_PLL4P 0x00009042 ++#define CLK_FMC_CKPER 0x00009043 ++ ++#define CLK_FDCAN_HSE 0x000090C0 ++#define CLK_FDCAN_PLL3Q 0x000090C1 ++#define CLK_FDCAN_PLL4Q 0x000090C2 ++#define CLK_FDCAN_PLL4R 0x000090C3 ++ ++#define CLK_SPDIF_PLL4P 0x00009140 ++#define CLK_SPDIF_PLL3Q 0x00009141 ++#define CLK_SPDIF_HSI 0x00009142 ++#define CLK_SPDIF_DISABLED 0x00009143 ++ ++#define CLK_CEC_LSE 0x00009180 ++#define CLK_CEC_LSI 0x00009181 ++#define CLK_CEC_CSI_DIV122 0x00009182 ++#define CLK_CEC_DISABLED 0x00009183 ++ ++#define CLK_USBPHY_HSE 0x000091C0 ++#define CLK_USBPHY_PLL4R 0x000091C1 ++#define CLK_USBPHY_HSE_DIV2 0x000091C2 ++#define CLK_USBPHY_DISABLED 0x000091C3 ++ ++#define CLK_USBO_PLL4R 0x800091C0 ++#define CLK_USBO_USBPHY 0x800091C1 ++ ++#define CLK_RNG1_CSI 0x00000CC0 ++#define CLK_RNG1_PLL4R 0x00000CC1 ++#define CLK_RNG1_LSE 0x00000CC2 ++#define CLK_RNG1_LSI 0x00000CC3 ++ ++#define CLK_RNG2_CSI 0x00009200 ++#define CLK_RNG2_PLL4R 0x00009201 ++#define CLK_RNG2_LSE 0x00009202 ++#define CLK_RNG2_LSI 0x00009203 ++ ++#define CLK_CKPER_HSI 0x00000D00 ++#define CLK_CKPER_CSI 0x00000D01 ++#define CLK_CKPER_HSE 0x00000D02 ++#define CLK_CKPER_DISABLED 0x00000D03 ++ ++#define CLK_STGEN_HSI 0x00000D40 ++#define CLK_STGEN_HSE 0x00000D41 ++#define CLK_STGEN_DISABLED 0x00000D43 ++ ++#define CLK_DSI_DSIPLL 0x00009240 ++#define CLK_DSI_PLL4P 0x00009241 ++ ++#define CLK_ADC_PLL4R 0x00009280 ++#define CLK_ADC_CKPER 0x00009281 ++#define CLK_ADC_PLL3Q 0x00009282 ++#define CLK_ADC_DISABLED 0x00009283 ++ ++#define CLK_LPTIM45_PCLK3 0x000092C0 ++#define CLK_LPTIM45_PLL4P 0x000092C1 ++#define CLK_LPTIM45_PLL3Q 0x000092C2 ++#define CLK_LPTIM45_LSE 0x000092C3 ++#define CLK_LPTIM45_LSI 0x000092C4 ++#define CLK_LPTIM45_CKPER 0x000092C5 ++#define CLK_LPTIM45_DISABLED 0x000092C7 ++ ++#define CLK_LPTIM23_PCLK3 0x00009300 ++#define CLK_LPTIM23_PLL4Q 0x00009301 ++#define CLK_LPTIM23_CKPER 0x00009302 ++#define CLK_LPTIM23_LSE 0x00009303 ++#define CLK_LPTIM23_LSI 0x00009304 ++#define CLK_LPTIM23_DISABLED 0x00009307 ++ ++#define CLK_LPTIM1_PCLK1 0x00009340 ++#define CLK_LPTIM1_PLL4P 0x00009341 ++#define CLK_LPTIM1_PLL3Q 0x00009342 ++#define CLK_LPTIM1_LSE 0x00009343 ++#define CLK_LPTIM1_LSI 0x00009344 ++#define CLK_LPTIM1_CKPER 0x00009345 ++#define CLK_LPTIM1_DISABLED 0x00009347 ++ ++/* define for st,pll /csg */ ++#define SSCG_MODE_CENTER_SPREAD 0 ++#define SSCG_MODE_DOWN_SPREAD 1 ++ ++/* define for st,drive */ ++#define LSEDRV_LOWEST 0 ++#define LSEDRV_MEDIUM_LOW 1 ++#define LSEDRV_MEDIUM_HIGH 2 ++#define LSEDRV_HIGHEST 3 ++ ++#endif +diff --git a/core/include/dt-bindings/clock/stm32mp13-clks.h b/core/include/dt-bindings/clock/stm32mp13-clks.h +new file mode 100644 +index 000000000..3e8d8eeb9 +--- /dev/null ++++ b/core/include/dt-bindings/clock/stm32mp13-clks.h +@@ -0,0 +1,232 @@ ++/* SPDX-License-Identifier: GPL-2.0+ or BSD-3-Clause */ ++/* ++ * Copyright (C) STMicroelectronics 2020 - All Rights Reserved ++ * Author: Gabriel Fernandez for STMicroelectronics. ++ */ ++ ++#ifndef _DT_BINDINGS_STM32MP13_CLKS_H_ ++#define _DT_BINDINGS_STM32MP13_CLKS_H_ ++ ++/* OSCILLATOR clocks */ ++#define CK_HSE 0 ++#define CK_CSI 1 ++#define CK_LSI 2 ++#define CK_LSE 3 ++#define CK_HSI 4 ++#define CK_HSE_DIV2 5 ++ ++/* PLL */ ++#define PLL1 6 ++#define PLL2 7 ++#define PLL3 8 ++#define PLL4 9 ++ ++/* ODF */ ++#define PLL1_P 10 ++#define PLL1_Q 11 ++#define PLL1_R 12 ++#define PLL2_P 13 ++#define PLL2_Q 14 ++#define PLL2_R 15 ++#define PLL3_P 16 ++#define PLL3_Q 17 ++#define PLL3_R 18 ++#define PLL4_P 19 ++#define PLL4_Q 20 ++#define PLL4_R 21 ++ ++/* SYSTEM CLOCK */ ++#define CK_PER 22 ++#define CK_MPU 23 ++#define CK_AXI 24 ++#define CK_MLAHB 25 ++ ++#define PCLK1 26 ++#define PCLK2 27 ++#define PCLK3 28 ++#define PCLK4 29 ++#define PCLK5 30 ++#define PCLK6 31 ++ ++ ++ ++/* BASE TIMER */ ++#define CK_TIMG1 32 ++#define CK_TIMG2 33 ++#define CK_TIMG3 34 ++ ++/* AUX */ ++#define RTC 35 ++ ++/* TRACE & DEBUG clocks */ ++#define CK_DBG 36 ++#define CK_TRACE 37 ++ ++/* MCO clocks */ ++#define CK_MCO1 38 ++#define CK_MCO2 39 ++ ++/* IP clocks */ ++#define SYSCFG 40 ++#define VREF 41 ++#define TMPSENS 42 ++#define PMBCTRL 43 ++#define HDP 44 ++#define IWDG2 45 ++#define STGENRO 46 ++#define USART1 47 ++#define RTCAPB 48 ++#define TZC 49 ++#define TZPC 50 ++#define IWDG1 51 ++#define BSEC 52 ++#define DMA1 53 ++#define DMA2 54 ++#define DMAMUX1 55 ++#define DMAMUX2 56 ++#define GPIOA 57 ++#define GPIOB 58 ++#define GPIOC 59 ++#define GPIOD 60 ++#define GPIOE 61 ++#define GPIOF 62 ++#define GPIOG 63 ++#define GPIOH 64 ++#define GPIOI 65 ++#define CRYP1 66 ++#define HASH1 67 ++#define BKPSRAM 68 ++#define MDMA 69 ++#define CRC1 70 ++#define USBH 71 ++#define DMA3 72 ++#define TSC 73 ++#define PKA 74 ++#define AXIMC 75 ++#define MCE 76 ++#define ETH1TX 77 ++#define ETH2TX 78 ++#define ETH1RX 79 ++#define ETH2RX 80 ++#define ETH1MAC 81 ++#define ETH2MAC 82 ++#define ETH1STP 83 ++#define ETH2STP 84 ++ ++/* IP clocks with parents */ ++#define SDMMC1_K 85 ++#define SDMMC2_K 86 ++#define ADC1_K 87 ++#define ADC2_K 88 ++#define FMC_K 89 ++#define QSPI_K 90 ++#define RNG1_K 91 ++#define USBPHY_K 92 ++#define STGEN_K 93 ++#define SPDIF_K 94 ++#define SPI1_K 95 ++#define SPI2_K 96 ++#define SPI3_K 97 ++#define SPI4_K 98 ++#define SPI5_K 99 ++#define I2C1_K 100 ++#define I2C2_K 101 ++#define I2C3_K 102 ++#define I2C4_K 103 ++#define I2C5_K 104 ++#define TIM2_K 105 ++#define TIM3_K 106 ++#define TIM4_K 107 ++#define TIM5_K 108 ++#define TIM6_K 109 ++#define TIM7_K 110 ++#define TIM12_K 111 ++#define TIM13_K 112 ++#define TIM14_K 113 ++#define TIM1_K 114 ++#define TIM8_K 115 ++#define TIM15_K 116 ++#define TIM16_K 117 ++#define TIM17_K 118 ++#define LPTIM1_K 119 ++#define LPTIM2_K 120 ++#define LPTIM3_K 121 ++#define LPTIM4_K 122 ++#define LPTIM5_K 123 ++#define USART1_K 124 ++#define USART2_K 125 ++#define USART3_K 126 ++#define UART4_K 127 ++#define UART5_K 128 ++#define USART6_K 129 ++#define UART7_K 130 ++#define UART8_K 131 ++#define DFSDM_K 132 ++#define FDCAN_K 133 ++#define SAI1_K 134 ++#define SAI2_K 135 ++#define ADFSDM_K 136 ++#define USBO_K 137 ++#define LTDC_PX 138 ++#define ETH1CK_K 139 ++#define ETH1PTP_K 140 ++#define ETH2CK_K 141 ++#define ETH2PTP_K 142 ++#define DCMIPP_K 143 ++#define SAES_K 144 ++#define DTS_K 145 ++ ++/* DDR */ ++#define DDRC1 146 ++#define DDRC1LP 147 ++#define DDRC2 148 ++#define DDRC2LP 149 ++#define DDRPHYC 150 ++#define DDRPHYCLP 151 ++#define DDRCAPB 152 ++#define DDRCAPBLP 153 ++#define AXIDCG 154 ++#define DDRPHYCAPB 155 ++#define DDRPHYCAPBLP 156 ++#define DDRPERFM 157 ++ ++#define ADC1 158 ++#define ADC2 159 ++#define SAI1 160 ++#define SAI2 161 ++ ++#define STM32MP1_LAST_CLK 162 ++ ++/* SCMI clock identifiers */ ++#define CK_SCMI_HSE 0 ++#define CK_SCMI_HSI 1 ++#define CK_SCMI_CSI 2 ++#define CK_SCMI_LSE 3 ++#define CK_SCMI_LSI 4 ++#define CK_SCMI_HSE_DIV2 5 ++#define CK_SCMI_PLL2_Q 6 ++#define CK_SCMI_PLL2_R 7 ++#define CK_SCMI_PLL3_P 8 ++#define CK_SCMI_PLL3_Q 9 ++#define CK_SCMI_PLL3_R 10 ++#define CK_SCMI_PLL4_P 11 ++#define CK_SCMI_PLL4_Q 12 ++#define CK_SCMI_PLL4_R 13 ++#define CK_SCMI_MPU 14 ++#define CK_SCMI_AXI 15 ++#define CK_SCMI_MLAHB 16 ++#define CK_SCMI_CKPER 17 ++#define CK_SCMI_PCLK1 18 ++#define CK_SCMI_PCLK2 19 ++#define CK_SCMI_PCLK3 20 ++#define CK_SCMI_PCLK4 21 ++#define CK_SCMI_PCLK5 22 ++#define CK_SCMI_PCLK6 23 ++#define CK_SCMI_CKTIMG1 24 ++#define CK_SCMI_CKTIMG2 25 ++#define CK_SCMI_CKTIMG3 26 ++#define CK_SCMI_RTC 27 ++#define CK_SCMI_RTCAPB 28 ++#define CK_SCMI_BSEC 29 ++ ++#endif /* _DT_BINDINGS_STM32MP13_CLKS_H_ */ +diff --git a/core/include/dt-bindings/clock/stm32mp13-clksrc.h b/core/include/dt-bindings/clock/stm32mp13-clksrc.h +new file mode 100644 +index 000000000..d3ff566b0 +--- /dev/null ++++ b/core/include/dt-bindings/clock/stm32mp13-clksrc.h +@@ -0,0 +1,396 @@ ++/* ++ * Copyright (C) 2018-2020, STMicroelectronics - All Rights Reserved ++ * ++ * SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause ++ */ ++ ++#ifndef _DT_BINDINGS_CLOCK_STM32MP13_CLKSRC_H_ ++#define _DT_BINDINGS_CLOCK_STM32MP13_CLKSRC_H_ ++ ++#define CMD_DIV 0 ++#define CMD_MUX 1 ++#define CMD_CLK 2 ++#define CMD_RESERVED1 3 ++ ++#define CMD_SHIFT 26 ++#define CMD_MASK 0xFC000000 ++#define CMD_DATA_MASK 0x03FFFFFF ++ ++#define DIV_ID_SHIFT 8 ++#define DIV_ID_MASK 0x0000FF00 ++ ++#define DIV_DIVN_SHIFT 0 ++#define DIV_DIVN_MASK 0x000000FF ++ ++#define MUX_ID_SHIFT 4 ++#define MUX_ID_MASK 0x00000FF0 ++ ++#define MUX_SEL_SHIFT 0 ++#define MUX_SEL_MASK 0x0000000F ++ ++#define CLK_ID_MASK GENMASK_32(19, 11) ++#define CLK_ID_SHIFT 11 ++#define CLK_ON_MASK 0x00000400 ++#define CLK_ON_SHIFT 10 ++#define CLK_DIV_MASK GENMASK_32(9, 4) ++#define CLK_DIV_SHIFT 4 ++#define CLK_SEL_MASK GENMASK_32(3, 0) ++#define CLK_SEL_SHIFT 0 ++ ++#define DIV_PLL1DIVP 0 ++#define DIV_PLL2DIVP 1 ++#define DIV_PLL2DIVQ 2 ++#define DIV_PLL2DIVR 3 ++#define DIV_PLL3DIVP 4 ++#define DIV_PLL3DIVQ 5 ++#define DIV_PLL3DIVR 6 ++#define DIV_PLL4DIVP 7 ++#define DIV_PLL4DIVQ 8 ++#define DIV_PLL4DIVR 9 ++#define DIV_MPU 10 ++#define DIV_AXI 11 ++#define DIV_MLAHB 12 ++#define DIV_APB1 13 ++#define DIV_APB2 14 ++#define DIV_APB3 15 ++#define DIV_APB4 16 ++#define DIV_APB5 17 ++#define DIV_APB6 18 ++#define DIV_RTC 19 ++#define DIV_MCO1 20 ++#define DIV_MCO2 21 ++#define DIV_HSI 22 ++#define DIV_TRACE 23 ++#define DIV_ETH1PTP 24 ++#define DIV_ETH2PTP 25 ++#define DIV_NB 26 ++ ++#define DIV(div_id, div) ((CMD_DIV << CMD_SHIFT) |\ ++ ((div_id) << DIV_ID_SHIFT |\ ++ (div))) ++ ++#define CLKSRC(mux_id, sel) ((CMD_MUX << CMD_SHIFT) |\ ++ ((mux_id) << MUX_ID_SHIFT |\ ++ (sel))) ++ ++/* MCO output is enable */ ++#define MCO_SRC(mco_id, sel) ((CMD_CLK << CMD_SHIFT) |\ ++ (((mco_id) << CLK_ID_SHIFT) |\ ++ (sel)) | CLK_ON_MASK) ++ ++#define MCO_DISABLED(mco_id) ((CMD_CLK << CMD_SHIFT) |\ ++ ((mco_id) << CLK_ID_SHIFT)) ++ ++/* CLK output is enable */ ++#define CLK_SRC(clk_id, sel) ((CMD_CLK << CMD_SHIFT) |\ ++ (((clk_id) << CLK_ID_SHIFT) |\ ++ (sel)) | CLK_ON_MASK) ++ ++#define CLK_DISABLED(clk_id) ((CMD_CLK << CMD_SHIFT) |\ ++ ((clk_id) << CLK_ID_SHIFT)) ++ ++#define MUX_MPU 0 ++#define MUX_AXI 1 ++#define MUX_MLAHB 2 ++#define MUX_PLL12 3 ++#define MUX_PLL3 4 ++#define MUX_PLL4 5 ++#define MUX_RTC 6 ++#define MUX_MCO1 7 ++#define MUX_MCO2 8 ++#define MUX_CKPER 9 ++#define MUX_ADC1 10 ++#define MUX_ADC2 11 ++#define MUX_DCMIPP 12 ++#define MUX_ETH1 13 ++#define MUX_ETH2 14 ++#define MUX_FDCAN 15 ++#define MUX_FMC 16 ++#define MUX_I2C12 17 ++#define MUX_I2C3 18 ++#define MUX_I2C4 19 ++#define MUX_I2C5 20 ++#define MUX_LPTIM1 21 ++#define MUX_LPTIM2 22 ++#define MUX_LPTIM3 23 ++#define MUX_LPTIM45 24 ++#define MUX_QSPI 25 ++#define MUX_RNG1 26 ++#define MUX_SAES 27 ++#define MUX_SAI1 28 ++#define MUX_SAI2 29 ++#define MUX_SDMMC1 30 ++#define MUX_SDMMC2 31 ++#define MUX_SPDIF 32 ++#define MUX_SPI1 33 ++#define MUX_SPI23 34 ++#define MUX_SPI4 35 ++#define MUX_SPI5 36 ++#define MUX_STGEN 37 ++#define MUX_UART1 38 ++#define MUX_UART2 39 ++#define MUX_UART35 40 ++#define MUX_UART4 41 ++#define MUX_UART6 42 ++#define MUX_UART78 43 ++#define MUX_USBO 44 ++#define MUX_USBPHY 45 ++#define MUX_NB 46 ++ ++/* ADC MUX is the first Kernel MUX */ ++#define MUX_KERNEL_BEGIN MUX_ADC1 ++ ++#define CLK_MPU_HSI CLKSRC(MUX_MPU, 0) ++#define CLK_MPU_HSE CLKSRC(MUX_MPU, 1) ++#define CLK_MPU_PLL1P CLKSRC(MUX_MPU, 2) ++#define CLK_MPU_PLL1P_DIV CLKSRC(MUX_MPU, 3) ++ ++#define CLK_AXI_HSI CLKSRC(MUX_AXI, 0) ++#define CLK_AXI_HSE CLKSRC(MUX_AXI, 1) ++#define CLK_AXI_PLL2P CLKSRC(MUX_AXI, 2) ++ ++#define CLK_MLAHBS_HSI CLKSRC(MUX_MLAHB, 0) ++#define CLK_MLAHBS_HSE CLKSRC(MUX_MLAHB, 1) ++#define CLK_MLAHBS_CSI CLKSRC(MUX_MLAHB, 2) ++#define CLK_MLAHBS_PLL3 CLKSRC(MUX_MLAHB, 3) ++ ++#define CLK_PLL12_HSI CLKSRC(MUX_PLL12, 0) ++#define CLK_PLL12_HSE CLKSRC(MUX_PLL12, 1) ++ ++#define CLK_PLL3_HSI CLKSRC(MUX_PLL3, 0) ++#define CLK_PLL3_HSE CLKSRC(MUX_PLL3, 1) ++#define CLK_PLL3_CSI CLKSRC(MUX_PLL3, 2) ++ ++#define CLK_PLL4_HSI CLKSRC(MUX_PLL4, 0) ++#define CLK_PLL4_HSE CLKSRC(MUX_PLL4, 1) ++#define CLK_PLL4_CSI CLKSRC(MUX_PLL4, 2) ++ ++#define CLK_RTC_DISABLED CLKSRC(RTC, 0) ++#define CLK_RTC_LSE CLKSRC(RTC, 1) ++#define CLK_RTC_LSI CLKSRC(RTC, 2) ++#define CLK_RTC_HSE CLKSRC(RTC, 3) ++ ++#define CLK_MCO1_HSI CLK_SRC(CK_MCO1, 0) ++#define CLK_MCO1_HSE CLK_SRC(CK_MCO1, 1) ++#define CLK_MCO1_CSI CLK_SRC(CK_MCO1, 2) ++#define CLK_MCO1_LSI CLK_SRC(CK_MCO1, 3) ++#define CLK_MCO1_LSE CLK_SRC(CK_MCO1, 4) ++#define CLK_MCO1_DISABLED CLK_DISABLED(CK_MCO1) ++ ++#define CLK_MCO2_MPU CLK_SRC(CK_MCO2, 0) ++#define CLK_MCO2_AXI CLK_SRC(CK_MCO2, 1) ++#define CLK_MCO2_MLAHB CLK_SRC(CK_MCO2, 2) ++#define CLK_MCO2_PLL4 CLK_SRC(CK_MCO2, 3) ++#define CLK_MCO2_HSE CLK_SRC(CK_MCO2, 4) ++#define CLK_MCO2_HSI CLK_SRC(CK_MCO2, 5) ++#define CLK_MCO2_DISABLED CLK_DISABLED(CK_MCO2) ++ ++#define CLK_CKPER_HSI CLKSRC(MUX_CKPER, 0) ++#define CLK_CKPER_CSI CLKSRC(MUX_CKPER, 1) ++#define CLK_CKPER_HSE CLKSRC(MUX_CKPER, 2) ++#define CLK_CKPER_DISABLED CLKSRC(MUX_CKPER, 3) ++ ++#define CLK_I2C12_PCLK1 CLKSRC(MUX_I2C12, 0) ++#define CLK_I2C12_PLL4R CLKSRC(MUX_I2C12, 1) ++#define CLK_I2C12_HSI CLKSRC(MUX_I2C12, 2) ++#define CLK_I2C12_CSI CLKSRC(MUX_I2C12, 3) ++ ++#define CLK_I2C3_PCLK6 CLKSRC(MUX_I2C3, 0) ++#define CLK_I2C3_PLL4R CLKSRC(MUX_I2C3, 1) ++#define CLK_I2C3_HSI CLKSRC(MUX_I2C3, 2) ++#define CLK_I2C3_CSI CLKSRC(MUX_I2C3, 3) ++ ++#define CLK_I2C4_PCLK6 CLKSRC(MUX_I2C4, 0) ++#define CLK_I2C4_PLL4R CLKSRC(MUX_I2C4, 1) ++#define CLK_I2C4_HSI CLKSRC(MUX_I2C4, 2) ++#define CLK_I2C4_CSI CLKSRC(MUX_I2C4, 3) ++ ++#define CLK_I2C5_PCLK6 CLKSRC(MUX_I2C5, 0) ++#define CLK_I2C5_PLL4R CLKSRC(MUX_I2C5, 1) ++#define CLK_I2C5_HSI CLKSRC(MUX_I2C5, 2) ++#define CLK_I2C5_CSI CLKSRC(MUX_I2C5, 3) ++ ++#define CLK_SPI1_PLL4P CLKSRC(MUX_SPI1, 0) ++#define CLK_SPI1_PLL3Q CLKSRC(MUX_SPI1, 1) ++#define CLK_SPI1_I2SCKIN CLKSRC(MUX_SPI1, 2) ++#define CLK_SPI1_CKPER CLKSRC(MUX_SPI1, 3) ++#define CLK_SPI1_PLL3R CLKSRC(MUX_SPI1, 4) ++ ++#define CLK_SPI23_PLL4P CLKSRC(MUX_SPI23, 0) ++#define CLK_SPI23_PLL3Q CLKSRC(MUX_SPI23, 1) ++#define CLK_SPI23_I2SCKIN CLKSRC(MUX_SPI23, 2) ++#define CLK_SPI23_CKPER CLKSRC(MUX_SPI23, 3) ++#define CLK_SPI23_PLL3R CLKSRC(MUX_SPI23, 4) ++ ++#define CLK_SPI4_PCLK6 CLKSRC(MUX_SPI4, 0) ++#define CLK_SPI4_PLL4Q CLKSRC(MUX_SPI4, 1) ++#define CLK_SPI4_HSI CLKSRC(MUX_SPI4, 2) ++#define CLK_SPI4_CSI CLKSRC(MUX_SPI4, 3) ++#define CLK_SPI4_HSE CLKSRC(MUX_SPI4, 4) ++#define CLK_SPI4_I2SCKIN CLKSRC(MUX_SPI4, 5) ++ ++#define CLK_SPI5_PCLK6 CLKSRC(MUX_SPI5, 0) ++#define CLK_SPI5_PLL4Q CLKSRC(MUX_SPI5, 1) ++#define CLK_SPI5_HSI CLKSRC(MUX_SPI5, 2) ++#define CLK_SPI5_CSI CLKSRC(MUX_SPI5, 3) ++#define CLK_SPI5_HSE CLKSRC(MUX_SPI5, 4) ++ ++#define CLK_UART1_PCLK6 CLKSRC(MUX_UART1, 0) ++#define CLK_UART1_PLL3Q CLKSRC(MUX_UART1, 1) ++#define CLK_UART1_HSI CLKSRC(MUX_UART1, 2) ++#define CLK_UART1_CSI CLKSRC(MUX_UART1, 3) ++#define CLK_UART1_PLL4Q CLKSRC(MUX_UART1, 4) ++#define CLK_UART1_HSE CLKSRC(MUX_UART1, 5) ++ ++#define CLK_UART2_PCLK6 CLKSRC(MUX_UART2, 0) ++#define CLK_UART2_PLL3Q CLKSRC(MUX_UART2, 1) ++#define CLK_UART2_HSI CLKSRC(MUX_UART2, 2) ++#define CLK_UART2_CSI CLKSRC(MUX_UART2, 3) ++#define CLK_UART2_PLL4Q CLKSRC(MUX_UART2, 4) ++#define CLK_UART2_HSE CLKSRC(MUX_UART2, 5) ++ ++#define CLK_UART35_PCLK1 CLKSRC(MUX_UART35, 0) ++#define CLK_UART35_PLL4Q CLKSRC(MUX_UART35, 1) ++#define CLK_UART35_HSI CLKSRC(MUX_UART35, 2) ++#define CLK_UART35_CSI CLKSRC(MUX_UART35, 3) ++#define CLK_UART35_HSE CLKSRC(MUX_UART35, 4) ++ ++#define CLK_UART4_PCLK1 CLKSRC(MUX_UART4, 0) ++#define CLK_UART4_PLL4Q CLKSRC(MUX_UART4, 1) ++#define CLK_UART4_HSI CLKSRC(MUX_UART4, 2) ++#define CLK_UART4_CSI CLKSRC(MUX_UART4, 3) ++#define CLK_UART4_HSE CLKSRC(MUX_UART4, 4) ++ ++#define CLK_UART6_PCLK2 CLKSRC(MUX_UART6, 0) ++#define CLK_UART6_PLL4Q CLKSRC(MUX_UART6, 1) ++#define CLK_UART6_HSI CLKSRC(MUX_UART6, 2) ++#define CLK_UART6_CSI CLKSRC(MUX_UART6, 3) ++#define CLK_UART6_HSE CLKSRC(MUX_UART6, 4) ++ ++#define CLK_UART78_PCLK1 CLKSRC(MUX_UART78, 0) ++#define CLK_UART78_PLL4Q CLKSRC(MUX_UART78, 1) ++#define CLK_UART78_HSI CLKSRC(MUX_UART78, 2) ++#define CLK_UART78_CSI CLKSRC(MUX_UART78, 3) ++#define CLK_UART78_HSE CLKSRC(MUX_UART78, 4) ++ ++#define CLK_LPTIM1_PCLK1 CLKSRC(MUX_LPTIM1, 0) ++#define CLK_LPTIM1_PLL4P CLKSRC(MUX_LPTIM1, 1) ++#define CLK_LPTIM1_PLL3Q CLKSRC(MUX_LPTIM1, 2) ++#define CLK_LPTIM1_LSE CLKSRC(MUX_LPTIM1, 3) ++#define CLK_LPTIM1_LSI CLKSRC(MUX_LPTIM1, 4) ++#define CLK_LPTIM1_CKPER CLKSRC(MUX_LPTIM1, 5) ++ ++#define CLK_LPTIM2_PCLK3 CLKSRC(MUX_LPTIM2, 0) ++#define CLK_LPTIM2_PLL4Q CLKSRC(MUX_LPTIM2, 1) ++#define CLK_LPTIM2_CKPER CLKSRC(MUX_LPTIM2, 2) ++#define CLK_LPTIM2_LSE CLKSRC(MUX_LPTIM2, 3) ++#define CLK_LPTIM2_LSI CLKSRC(MUX_LPTIM2, 4) ++ ++#define CLK_LPTIM3_PCLK3 CLKSRC(MUX_LPTIM3, 0) ++#define CLK_LPTIM3_PLL4Q CLKSRC(MUX_LPTIM3, 1) ++#define CLK_LPTIM3_CKPER CLKSRC(MUX_LPTIM3, 2) ++#define CLK_LPTIM3_LSE CLKSRC(MUX_LPTIM3, 3) ++#define CLK_LPTIM3_LSI CLKSRC(MUX_LPTIM3, 4) ++ ++#define CLK_LPTIM45_PCLK3 CLKSRC(MUX_LPTIM45, 0) ++#define CLK_LPTIM45_PLL4P CLKSRC(MUX_LPTIM45, 1) ++#define CLK_LPTIM45_PLL3Q CLKSRC(MUX_LPTIM45, 2) ++#define CLK_LPTIM45_LSE CLKSRC(MUX_LPTIM45, 3) ++#define CLK_LPTIM45_LSI CLKSRC(MUX_LPTIM45, 4) ++#define CLK_LPTIM45_CKPER CLKSRC(MUX_LPTIM45, 5) ++ ++#define CLK_SAI1_PLL4Q CLKSRC(MUX_SAI1, 0) ++#define CLK_SAI1_PLL3Q CLKSRC(MUX_SAI1, 1) ++#define CLK_SAI1_I2SCKIN CLKSRC(MUX_SAI1, 2) ++#define CLK_SAI1_CKPER CLKSRC(MUX_SAI1, 3) ++#define CLK_SAI1_PLL3R CLKSRC(MUX_SAI1, 4) ++ ++#define CLK_SAI2_PLL4Q CLKSRC(MUX_SAI2, 0) ++#define CLK_SAI2_PLL3Q CLKSRC(MUX_SAI2, 1) ++#define CLK_SAI2_I2SCKIN CLKSRC(MUX_SAI2, 2) ++#define CLK_SAI2_CKPER CLKSRC(MUX_SAI2, 3) ++#define CLK_SAI2_SPDIF CLKSRC(MUX_SAI2, 4) ++#define CLK_SAI2_PLL3R CLKSRC(MUX_SAI2, 5) ++ ++#define CLK_FDCAN_HSE CLKSRC(MUX_FDCAN, 0) ++#define CLK_FDCAN_PLL3Q CLKSRC(MUX_FDCAN, 1) ++#define CLK_FDCAN_PLL4Q CLKSRC(MUX_FDCAN, 2) ++#define CLK_FDCAN_PLL4R CLKSRC(MUX_FDCAN, 3) ++ ++#define CLK_SPDIF_PLL4P CLKSRC(MUX_SPDIF, 0) ++#define CLK_SPDIF_PLL3Q CLKSRC(MUX_SPDIF, 1) ++#define CLK_SPDIF_HSI CLKSRC(MUX_SPDIF, 2) ++ ++#define CLK_ADC1_PLL4R CLKSRC(MUX_ADC1, 0) ++#define CLK_ADC1_CKPER CLKSRC(MUX_ADC1, 1) ++#define CLK_ADC1_PLL3Q CLKSRC(MUX_ADC1, 2) ++ ++#define CLK_ADC2_PLL4R CLKSRC(MUX_ADC2, 0) ++#define CLK_ADC2_CKPER CLKSRC(MUX_ADC2, 1) ++#define CLK_ADC2_PLL3Q CLKSRC(MUX_ADC2, 2) ++ ++#define CLK_SDMMC1_HCLK6 CLKSRC(MUX_SDMMC1, 0) ++#define CLK_SDMMC1_PLL3R CLKSRC(MUX_SDMMC1, 1) ++#define CLK_SDMMC1_PLL4P CLKSRC(MUX_SDMMC1, 2) ++#define CLK_SDMMC1_HSI CLKSRC(MUX_SDMMC1, 3) ++ ++#define CLK_SDMMC2_HCLK6 CLKSRC(MUX_SDMMC2, 0) ++#define CLK_SDMMC2_PLL3R CLKSRC(MUX_SDMMC2, 1) ++#define CLK_SDMMC2_PLL4P CLKSRC(MUX_SDMMC2, 2) ++#define CLK_SDMMC2_HSI CLKSRC(MUX_SDMMC2, 3) ++ ++#define CLK_ETH1_PLL4P CLKSRC(MUX_ETH1, 0) ++#define CLK_ETH1_PLL3Q CLKSRC(MUX_ETH1, 1) ++ ++#define CLK_ETH2_PLL4P CLKSRC(MUX_ETH2, 0) ++#define CLK_ETH2_PLL3Q CLKSRC(MUX_ETH2, 1) ++ ++#define CLK_USBPHY_HSE CLKSRC(MUX_USBPHY, 0) ++#define CLK_USBPHY_PLL4R CLKSRC(MUX_USBPHY, 1) ++#define CLK_USBPHY_HSE_DIV2 CLKSRC(MUX_USBPHY, 2) ++ ++#define CLK_USBO_PLL4R CLKSRC(MUX_USBO, 0) ++#define CLK_USBO_USBPHY CLKSRC(MUX_USBO, 1) ++ ++#define CLK_QSPI_ACLK CLKSRC(MUX_QSPI, 0) ++#define CLK_QSPI_PLL3R CLKSRC(MUX_QSPI, 1) ++#define CLK_QSPI_PLL4P CLKSRC(MUX_QSPI, 2) ++#define CLK_QSPI_CKPER CLKSRC(MUX_QSPI, 3) ++ ++#define CLK_FMC_ACLK CLKSRC(MUX_FMC, 0) ++#define CLK_FMC_PLL3R CLKSRC(MUX_FMC, 1) ++#define CLK_FMC_PLL4P CLKSRC(MUX_FMC, 2) ++#define CLK_FMC_CKPER CLKSRC(MUX_FMC, 3) ++ ++#define CLK_RNG1_CSI CLKSRC(MUX_RNG1, 0) ++#define CLK_RNG1_PLL4R CLKSRC(MUX_RNG1, 1) ++/* WARNING: POSITION 2 OF RNG1 MUX IS RESERVED */ ++#define CLK_RNG1_LSI CLKSRC(MUX_RNG1, 3) ++ ++#define CLK_STGEN_HSI CLKSRC(MUX_STGEN, 0) ++#define CLK_STGEN_HSE CLKSRC(MUX_STGEN, 1) ++ ++#define CLK_DCMIPP_ACLK CLKSRC(MUX_DCMIPP, 0) ++#define CLK_DCMIPP_PLL2Q CLKSRC(MUX_DCMIPP, 1) ++#define CLK_DCMIPP_PLL4P CLKSRC(MUX_DCMIPP, 2) ++#define CLK_DCMIPP_CKPER CLKSRC(MUX_DCMIPP, 3) ++ ++#define CLK_SAES_AXI CLKSRC(MUX_SAES, 0) ++#define CLK_SAES_CKPER CLKSRC(MUX_SAES, 1) ++#define CLK_SAES_PLL4R CLKSRC(MUX_SAES, 2) ++#define CLK_SAES_LSI CLKSRC(MUX_SAES, 3) ++ ++/* PLL output is enable when x=1, with x=p,q or r */ ++#define PQR(p, q, r) (((p) & 1) | (((q) & 1) << 1) | (((r) & 1) << 2)) ++ ++/* define for st,pll /csg */ ++#define SSCG_MODE_CENTER_SPREAD 0 ++#define SSCG_MODE_DOWN_SPREAD 1 ++ ++/* define for st,drive */ ++#define LSEDRV_LOWEST 0 ++#define LSEDRV_MEDIUM_LOW 1 ++#define LSEDRV_MEDIUM_HIGH 2 ++#define LSEDRV_HIGHEST 3 ++ ++#endif /* _DT_BINDINGS_CLOCK_STM32MP13_CLKSRC_H_ */ +diff --git a/core/include/dt-bindings/gpio/gpio.h b/core/include/dt-bindings/gpio/gpio.h +index 68727f18b..a6355547b 100644 +--- a/core/include/dt-bindings/gpio/gpio.h ++++ b/core/include/dt-bindings/gpio/gpio.h +@@ -33,4 +33,10 @@ + #define GPIO_PERSISTENT 0 + #define GPIO_TRANSITORY 8 + ++/* Bit 4 express pull up */ ++#define GPIO_PULL_UP 16 ++ ++/* Bit 5 express pull down */ ++#define GPIO_PULL_DOWN 32 ++ + #endif +diff --git a/core/include/dt-bindings/gpio/stm32mp_gpio.h b/core/include/dt-bindings/gpio/stm32mp_gpio.h +new file mode 100644 +index 000000000..d2b6d8c25 +--- /dev/null ++++ b/core/include/dt-bindings/gpio/stm32mp_gpio.h +@@ -0,0 +1,15 @@ ++/* SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause */ ++/* ++ * Copyright (C) STMicroelectronics 2022 - All Rights Reserved ++ * Author: Gatien Chevallier for STMicroelectronics. ++ */ ++ ++#ifndef _DT_BINDINGS_GPIO_STM32MP_GPIO_H ++#define _DT_BINDINGS_GPIO_STM32MP_GPIO_H ++ ++#include ++ ++/* Macro to define the security for GPIO */ ++#define TZPROT(id) (1 << id) ++ ++#endif +diff --git a/core/include/dt-bindings/power/stm32mp1-power.h b/core/include/dt-bindings/power/stm32mp1-power.h +new file mode 100644 +index 000000000..fe394a826 +--- /dev/null ++++ b/core/include/dt-bindings/power/stm32mp1-power.h +@@ -0,0 +1,20 @@ ++/* SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause */ ++/* ++ * Copyright (C) STMicroelectronics 2018 - All Rights Reserved ++ * Author: Yann Gautier for STMicroelectronics. ++ */ ++ ++#ifndef DT_BINDINGS_STM32MP1_POWER_H ++#define DT_BINDINGS_STM32MP1_POWER_H ++ ++#define STM32_PM_CSLEEP_RUN 0 ++#define STM32_PM_CSTOP_ALLOW_STOP 1 ++#define STM32_PM_CSTOP_ALLOW_LP_STOP 2 ++#define STM32_PM_CSTOP_ALLOW_LPLV_STOP 3 ++#define STM32_PM_CSTOP_ALLOW_LPLV_STOP2 4 ++#define STM32_PM_CSTOP_ALLOW_STANDBY_DDR_SR 5 ++#define STM32_PM_CSTOP_ALLOW_STANDBY_DDR_OFF 6 ++#define STM32_PM_SHUTDOWN 7 ++#define STM32_PM_MAX_SOC_MODE 8 ++ ++#endif /* DT_BINDINGS_STM32MP1_POWER_H */ +diff --git a/core/include/dt-bindings/regulator/st,stm32mp15-regulator.h b/core/include/dt-bindings/regulator/st,stm32mp15-regulator.h +index c6c6547e9..69e51c9dd 100644 +--- a/core/include/dt-bindings/regulator/st,stm32mp15-regulator.h ++++ b/core/include/dt-bindings/regulator/st,stm32mp15-regulator.h +@@ -8,8 +8,8 @@ + + /* SCMI voltage domain identifiers */ + +-#define VOLTD_SCMI0_REG11 0 +-#define VOLTD_SCMI0_REG18 1 +-#define VOLTD_SCMI0_USB33 2 ++#define VOLTD_SCMI_REG11 0 ++#define VOLTD_SCMI_REG18 1 ++#define VOLTD_SCMI_USB33 2 + + #endif /*__DT_BINDINGS_REGULATOR_ST_STM32MP15_REGULATOR_H */ +diff --git a/core/include/dt-bindings/reset/stm32mp1-resets.h b/core/include/dt-bindings/reset/stm32mp1-resets.h +index f3a0ed317..4ffa7c361 100644 +--- a/core/include/dt-bindings/reset/stm32mp1-resets.h ++++ b/core/include/dt-bindings/reset/stm32mp1-resets.h +@@ -107,17 +107,17 @@ + #define GPIOK_R 19786 + + /* SCMI reset domain identifiers */ +-#define RST_SCMI0_SPI6 0 +-#define RST_SCMI0_I2C4 1 +-#define RST_SCMI0_I2C6 2 +-#define RST_SCMI0_USART1 3 +-#define RST_SCMI0_STGEN 4 +-#define RST_SCMI0_GPIOZ 5 +-#define RST_SCMI0_CRYP1 6 +-#define RST_SCMI0_HASH1 7 +-#define RST_SCMI0_RNG1 8 +-#define RST_SCMI0_MDMA 9 +-#define RST_SCMI0_MCU 10 +-#define RST_SCMI0_MCU_HOLD_BOOT 11 ++#define RST_SCMI_SPI6 0 ++#define RST_SCMI_I2C4 1 ++#define RST_SCMI_I2C6 2 ++#define RST_SCMI_USART1 3 ++#define RST_SCMI_STGEN 4 ++#define RST_SCMI_GPIOZ 5 ++#define RST_SCMI_CRYP1 6 ++#define RST_SCMI_HASH1 7 ++#define RST_SCMI_RNG1 8 ++#define RST_SCMI_MDMA 9 ++#define RST_SCMI_MCU 10 ++#define RST_SCMI_MCU_HOLD_BOOT 11 + + #endif /* _DT_BINDINGS_STM32MP1_RESET_H_ */ +diff --git a/core/include/dt-bindings/reset/stm32mp13-resets.h b/core/include/dt-bindings/reset/stm32mp13-resets.h +new file mode 100644 +index 000000000..934864e90 +--- /dev/null ++++ b/core/include/dt-bindings/reset/stm32mp13-resets.h +@@ -0,0 +1,100 @@ ++/* SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause */ ++/* ++ * Copyright (C) STMicroelectronics 2018 - All Rights Reserved ++ * Author: Gabriel Fernandez for STMicroelectronics. ++ */ ++ ++#ifndef _DT_BINDINGS_STM32MP13_RESET_H_ ++#define _DT_BINDINGS_STM32MP13_RESET_H_ ++ ++#define TIM2_R 13568 ++#define TIM3_R 13569 ++#define TIM4_R 13570 ++#define TIM5_R 13571 ++#define TIM6_R 13572 ++#define TIM7_R 13573 ++#define LPTIM1_R 13577 ++#define SPI2_R 13579 ++#define SPI3_R 13580 ++#define USART3_R 13583 ++#define UART4_R 13584 ++#define UART5_R 13585 ++#define UART7_R 13586 ++#define UART8_R 13587 ++#define I2C1_R 13589 ++#define I2C2_R 13590 ++#define SPDIF_R 13594 ++#define TIM1_R 13632 ++#define TIM8_R 13633 ++#define SPI1_R 13640 ++#define USART6_R 13645 ++#define SAI1_R 13648 ++#define SAI2_R 13649 ++#define DFSDM_R 13652 ++#define FDCAN_R 13656 ++#define LPTIM2_R 13696 ++#define LPTIM3_R 13697 ++#define LPTIM4_R 13698 ++#define LPTIM5_R 13699 ++#define SYSCFG_R 13707 ++#define VREF_R 13709 ++#define DTS_R 13712 ++#define PMBCTRL_R 13713 ++#define LTDC_R 13760 ++#define DCMIPP_R 13761 ++#define DDRPERFM_R 13768 ++#define USBPHY_R 13776 ++#define STGEN_R 13844 ++#define USART1_R 13888 ++#define USART2_R 13889 ++#define SPI4_R 13890 ++#define SPI5_R 13891 ++#define I2C3_R 13892 ++#define I2C4_R 13893 ++#define I2C5_R 13894 ++#define TIM12_R 13895 ++#define TIM13_R 13896 ++#define TIM14_R 13897 ++#define TIM15_R 13898 ++#define TIM16_R 13899 ++#define TIM17_R 13900 ++#define DMA1_R 13952 ++#define DMA2_R 13953 ++#define DMAMUX1_R 13954 ++#define DMA3_R 13955 ++#define DMAMUX2_R 13956 ++#define ADC1_R 13957 ++#define ADC2_R 13958 ++#define USBO_R 13960 ++#define GPIOA_R 14080 ++#define GPIOB_R 14081 ++#define GPIOC_R 14082 ++#define GPIOD_R 14083 ++#define GPIOE_R 14084 ++#define GPIOF_R 14085 ++#define GPIOG_R 14086 ++#define GPIOH_R 14087 ++#define GPIOI_R 14088 ++#define TSC_R 14095 ++#define PKA_R 14146 ++#define SAES_R 14147 ++#define CRYP1_R 14148 ++#define HASH1_R 14149 ++#define RNG1_R 14150 ++#define AXIMC_R 14160 ++#define MDMA_R 14208 ++#define MCE_R 14209 ++#define ETH1MAC_R 14218 ++#define FMC_R 14220 ++#define QSPI_R 14222 ++#define SDMMC1_R 14224 ++#define SDMMC2_R 14225 ++#define CRC1_R 14228 ++#define USBH_R 14232 ++#define ETH2MAC_R 14238 ++ ++/* SCMI reset domain identifiers */ ++#define RST_SCMI_LTDC 0 ++#define RST_SCMI_MDMA 1 ++ ++#endif /* _DT_BINDINGS_STM32MP13_RESET_H_ */ +diff --git a/core/include/dt-bindings/rtc/rtc-stm32.h b/core/include/dt-bindings/rtc/rtc-stm32.h +new file mode 100644 +index 000000000..7f5ce0f6b +--- /dev/null ++++ b/core/include/dt-bindings/rtc/rtc-stm32.h +@@ -0,0 +1,13 @@ ++/* SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause */ ++/* ++ * This header provides constants for STM32_RTC bindings. ++ */ ++ ++#ifndef _DT_BINDINGS_RTC_STM32_H ++#define _DT_BINDINGS_RTC_STM32_H ++ ++#define RTC_OUT1 0 ++#define RTC_OUT2 1 ++#define RTC_OUT2_RMP 2 ++ ++#endif /*_DT_BINDINGS_RTC_STM32_H*/ +diff --git a/core/include/dt-bindings/soc/stm32mp-tzc400-macro.h b/core/include/dt-bindings/soc/stm32mp-tzc400-macro.h +new file mode 100644 +index 000000000..b697b1e88 +--- /dev/null ++++ b/core/include/dt-bindings/soc/stm32mp-tzc400-macro.h +@@ -0,0 +1,29 @@ ++/* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */ ++/* ++ * Copyright (C) 2020-2021, STMicroelectronics - All Rights Reserved ++ */ ++ ++#ifndef _DT_BINDINGS_STM32MP_TZC400_MACRO_H ++#define _DT_BINDINGS_STM32MP_TZC400_MACRO_H ++ ++#define TZC_REGION_S_NONE 0 ++#define TZC_REGION_S_RD 1 ++#define TZC_REGION_S_WR 2 ++#define TZC_REGION_S_RDWR 3 ++ ++#define REGION_ID_ACCESS_NSAID_WR_EN_SHIFT 16 ++#define REGION_ID_ACCESS_NSAID_RD_EN_SHIFT 0 ++#define REGION_ID_ACCESS_NSAID_ID_MASK 0xf ++ ++#define TZC_REGION_ACCESS_RD(id) \ ++ ((1 << ((id) & REGION_ID_ACCESS_NSAID_ID_MASK)) << \ ++ REGION_ID_ACCESS_NSAID_RD_EN_SHIFT) ++ ++#define TZC_REGION_ACCESS_WR(id) \ ++ ((1 << ((id) & REGION_ID_ACCESS_NSAID_ID_MASK)) << \ ++ REGION_ID_ACCESS_NSAID_WR_EN_SHIFT) ++ ++#define TZC_REGION_ACCESS_RDWR(id) \ ++ (TZC_REGION_ACCESS_RD(id) | TZC_REGION_ACCESS_WR(id)) ++ ++#endif /* _DT_BINDINGS_STM32MP_TZC400_MACRO_H */ +diff --git a/core/include/dt-bindings/soc/stm32mp13-etzpc.h b/core/include/dt-bindings/soc/stm32mp13-etzpc.h +new file mode 100644 +index 000000000..c44afb071 +--- /dev/null ++++ b/core/include/dt-bindings/soc/stm32mp13-etzpc.h +@@ -0,0 +1,68 @@ ++/* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */ ++/* ++ * Copyright (C) 2017-2021, STMicroelectronics - All Rights Reserved ++ */ ++ ++#ifndef _DT_BINDINGS_STM32MP13_ETZPC_H ++#define _DT_BINDINGS_STM32MP13_ETZPC_H ++ ++/* define DECPROT modes */ ++#define DECPROT_S_RW 0x0 ++#define DECPROT_NS_R_S_W 0x1 ++#define DECPROT_NS_RW 0x3 ++ ++/* define DECPROT lock */ ++#define DECPROT_UNLOCK 0x0 ++#define DECPROT_LOCK 0x1 ++ ++ ++/* define ETZPC ID */ ++#define STM32MP1_ETZPC_VREFBUF_ID 0 ++#define STM32MP1_ETZPC_LPTIM2_ID 1 ++#define STM32MP1_ETZPC_LPTIM3_ID 2 ++#define STM32MP1_ETZPC_LTDC_ID 3 ++#define STM32MP1_ETZPC_DCMIPP_ID 4 ++#define STM32MP1_ETZPC_USBPHYCTRL_ID 5 ++#define STM32MP1_ETZPC_DDRCTRLPHY_ID 6 ++#define STM32MP1_ETZPC_IWDG1_ID 12 ++#define STM32MP1_ETZPC_STGENC_ID 13 ++#define STM32MP1_ETZPC_USART1_ID 16 ++#define STM32MP1_ETZPC_USART2_ID 17 ++#define STM32MP1_ETZPC_SPI4_ID 18 ++#define STM32MP1_ETZPC_SPI5_ID 19 ++#define STM32MP1_ETZPC_I2C3_ID 20 ++#define STM32MP1_ETZPC_I2C4_ID 21 ++#define STM32MP1_ETZPC_I2C5_ID 22 ++#define STM32MP1_ETZPC_TIM12_ID 23 ++#define STM32MP1_ETZPC_TIM13_ID 24 ++#define STM32MP1_ETZPC_TIM14_ID 25 ++#define STM32MP1_ETZPC_TIM15_ID 26 ++#define STM32MP1_ETZPC_TIM16_ID 27 ++#define STM32MP1_ETZPC_TIM17_ID 28 ++#define STM32MP1_ETZPC_ADC1_ID 32 ++#define STM32MP1_ETZPC_ADC2_ID 33 ++#define STM32MP1_ETZPC_OTG_ID 34 ++#define STM32MP1_ETZPC_TSC_ID 37 ++#define STM32MP1_ETZPC_RNG_ID 40 ++#define STM32MP1_ETZPC_HASH_ID 41 ++#define STM32MP1_ETZPC_CRYP_ID 42 ++#define STM32MP1_ETZPC_SAES_ID 43 ++#define STM32MP1_ETZPC_PKA_ID 44 ++#define STM32MP1_ETZPC_BKPSRAM_ID 45 ++#define STM32MP1_ETZPC_ETH1_ID 48 ++#define STM32MP1_ETZPC_ETH2_ID 49 ++#define STM32MP1_ETZPC_SDMMC1_ID 50 ++#define STM32MP1_ETZPC_SDMMC2_ID 51 ++#define STM32MP1_ETZPC_MCE_ID 53 ++#define STM32MP1_ETZPC_FMC_ID 54 ++#define STM32MP1_ETZPC_QSPI_ID 55 ++#define STM32MP1_ETZPC_SRAM1_ID 60 ++#define STM32MP1_ETZPC_SRAM2_ID 61 ++#define STM32MP1_ETZPC_SRAM3_ID 62 ++ ++#define STM32MP1_ETZPC_MAX_ID 64 ++ ++#define DECPROT(id, mode, lock) (((id) << 16) | ((mode) << 8) | (lock)) ++ ++#endif /* _DT_BINDINGS_STM32MP13_ETZPC_H */ ++ +diff --git a/core/include/dt-bindings/soc/stm32mp13-tzc400.h b/core/include/dt-bindings/soc/stm32mp13-tzc400.h +new file mode 100644 +index 000000000..c654716b9 +--- /dev/null ++++ b/core/include/dt-bindings/soc/stm32mp13-tzc400.h +@@ -0,0 +1,33 @@ ++/* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */ ++/* ++ * Copyright (C) 2020-2021, STMicroelectronics - All Rights Reserved ++ */ ++ ++#ifndef _DT_BINDINGS_STM32MP13_TZC400_H ++#define _DT_BINDINGS_STM32MP13_TZC400_H ++ ++/* NSAID */ ++#define STM32MP1_TZC_A7_ID 0 ++#define STM32MP1_TZC_LCD_ID 3 ++#define STM32MP1_TZC_MDMA_ID 5 ++#define STM32MP1_TZC_DMA_ID 6 ++#define STM32MP1_TZC_USB_HOST_ID 7 ++#define STM32MP1_TZC_USB_OTG_ID 8 ++#define STM32MP1_TZC_SDMMC_ID 9 ++#define STM32MP1_TZC_ETH_ID 10 ++#define STM32MP1_TZC_DCMIPP_ID 11 ++#define STM32MP1_TZC_DAP_ID 15 ++ ++#define TZC_REGION_NSEC_ALL_ACCESS_RDWR \ ++ (TZC_REGION_ACCESS_RDWR(STM32MP1_TZC_A7_ID) | \ ++ TZC_REGION_ACCESS_RDWR(STM32MP1_TZC_LCD_ID) | \ ++ TZC_REGION_ACCESS_RDWR(STM32MP1_TZC_MDMA_ID) | \ ++ TZC_REGION_ACCESS_RDWR(STM32MP1_TZC_DMA_ID) | \ ++ TZC_REGION_ACCESS_RDWR(STM32MP1_TZC_USB_HOST_ID) | \ ++ TZC_REGION_ACCESS_RDWR(STM32MP1_TZC_USB_OTG_ID) | \ ++ TZC_REGION_ACCESS_RDWR(STM32MP1_TZC_SDMMC_ID) | \ ++ TZC_REGION_ACCESS_RDWR(STM32MP1_TZC_ETH_ID) | \ ++ TZC_REGION_ACCESS_RDWR(STM32MP1_TZC_DCMIPP_ID) | \ ++ TZC_REGION_ACCESS_RDWR(STM32MP1_TZC_DAP_ID)) ++ ++#endif /* _DT_BINDINGS_STM32MP13_TZC400_H */ +diff --git a/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_etzpc.h b/core/include/dt-bindings/soc/stm32mp15-etzpc.h +similarity index 83% +rename from core/arch/arm/plat-stm32mp1/drivers/stm32mp1_etzpc.h +rename to core/include/dt-bindings/soc/stm32mp15-etzpc.h +index 338a39283..4d143e76e 100644 +--- a/core/arch/arm/plat-stm32mp1/drivers/stm32mp1_etzpc.h ++++ b/core/include/dt-bindings/soc/stm32mp15-etzpc.h +@@ -1,26 +1,34 @@ +-/* SPDX-License-Identifier: GPL-2.0 or BSD-3-Clause */ ++/* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */ + /* +- * Copyright (C) 2018-2019, STMicroelectronics ++ * Copyright (C) 2017-2021, STMicroelectronics - All Rights Reserved + */ + +-#ifndef __STM32MP1_ETZPC_H +-#define __STM32MP1_ETZPC_H ++#ifndef _DT_BINDINGS_STM32MP15_ETZPC_H ++#define _DT_BINDINGS_STM32MP15_ETZPC_H + +-/* Define DECPROT IDs for stm32mp1 familly */ ++/* define DECPROT modes */ ++#define DECPROT_S_RW 0x0 ++#define DECPROT_NS_R_S_W 0x1 ++#define DECPROT_MCU_ISOLATION 0x2 ++#define DECPROT_NS_RW 0x3 ++ ++/* define DECPROT lock */ ++#define DECPROT_UNLOCK 0x0 ++#define DECPROT_LOCK 0x1 ++ ++/* define ETZPC ID */ + #define STM32MP1_ETZPC_STGENC_ID 0 + #define STM32MP1_ETZPC_BKPSRAM_ID 1 + #define STM32MP1_ETZPC_IWDG1_ID 2 + #define STM32MP1_ETZPC_USART1_ID 3 + #define STM32MP1_ETZPC_SPI6_ID 4 + #define STM32MP1_ETZPC_I2C4_ID 5 +-#define STM32MP1_ETZPC_GPIOZ_ID 6 + #define STM32MP1_ETZPC_RNG1_ID 7 + #define STM32MP1_ETZPC_HASH1_ID 8 + #define STM32MP1_ETZPC_CRYP1_ID 9 + #define STM32MP1_ETZPC_DDRCTRL_ID 10 + #define STM32MP1_ETZPC_DDRPHYC_ID 11 + #define STM32MP1_ETZPC_I2C6_ID 12 +-/* 13-15 Reserved */ + #define STM32MP1_ETZPC_TIM2_ID 16 + #define STM32MP1_ETZPC_TIM3_ID 17 + #define STM32MP1_ETZPC_TIM4_ID 18 +@@ -47,12 +55,9 @@ + #define STM32MP1_ETZPC_DAC_ID 39 + #define STM32MP1_ETZPC_UART7_ID 40 + #define STM32MP1_ETZPC_UART8_ID 41 +-/* 42-43 Reserved */ + #define STM32MP1_ETZPC_MDIOS_ID 44 +-/* 45-47 Reserved */ + #define STM32MP1_ETZPC_TIM1_ID 48 + #define STM32MP1_ETZPC_TIM8_ID 49 +-/* 50 Reserved */ + #define STM32MP1_ETZPC_USART6_ID 51 + #define STM32MP1_ETZPC_SPI1_ID 52 + #define STM32MP1_ETZPC_SPI4_ID 53 +@@ -65,7 +70,6 @@ + #define STM32MP1_ETZPC_SAI3_ID 60 + #define STM32MP1_ETZPC_DFSDM_ID 61 + #define STM32MP1_ETZPC_TT_FDCAN_ID 62 +-/* 63 Reserved */ + #define STM32MP1_ETZPC_LPTIM2_ID 64 + #define STM32MP1_ETZPC_LPTIM3_ID 65 + #define STM32MP1_ETZPC_LPTIM4_ID 66 +@@ -78,7 +82,6 @@ + #define STM32MP1_ETZPC_HASH2_ID 73 + #define STM32MP1_ETZPC_RNG2_ID 74 + #define STM32MP1_ETZPC_CRYP2_ID 75 +-/* 76-79 Reserved */ + #define STM32MP1_ETZPC_SRAM1_ID 80 + #define STM32MP1_ETZPC_SRAM2_ID 81 + #define STM32MP1_ETZPC_SRAM3_ID 82 +@@ -94,7 +97,10 @@ + #define STM32MP1_ETZPC_QSPI_ID 92 + #define STM32MP1_ETZPC_DLYBQ_ID 93 + #define STM32MP1_ETZPC_ETH_ID 94 +-/* 95 Reserved */ ++ + #define STM32MP1_ETZPC_MAX_ID 96 + +-#endif /*__STM32MP1_ETZPC_H*/ ++#define DECPROT(id, mode, lock) (((id) << 16) | ((mode) << 8) | (lock)) ++ ++#endif /* _DT_BINDINGS_STM32MP15_ETZPC_H */ ++ +diff --git a/core/include/dt-bindings/soc/stm32mp15-tzc400.h b/core/include/dt-bindings/soc/stm32mp15-tzc400.h +new file mode 100644 +index 000000000..b704d70a2 +--- /dev/null ++++ b/core/include/dt-bindings/soc/stm32mp15-tzc400.h +@@ -0,0 +1,35 @@ ++/* SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause */ ++/* ++ * Copyright (C) 2020-2021, STMicroelectronics - All Rights Reserved ++ */ ++ ++#ifndef _DT_BINDINGS_STM32MP15_TZC400_H ++#define _DT_BINDINGS_STM32MP15_TZC400_H ++ ++/* NSAID */ ++#define STM32MP1_TZC_A7_ID 0 ++#define STM32MP1_TZC_M4_ID 1 ++#define STM32MP1_TZC_LCD_ID 3 ++#define STM32MP1_TZC_GPU_ID 4 ++#define STM32MP1_TZC_MDMA_ID 5 ++#define STM32MP1_TZC_DMA_ID 6 ++#define STM32MP1_TZC_USB_HOST_ID 7 ++#define STM32MP1_TZC_USB_OTG_ID 8 ++#define STM32MP1_TZC_SDMMC_ID 9 ++#define STM32MP1_TZC_ETH_ID 10 ++#define STM32MP1_TZC_DAP_ID 15 ++ ++#define TZC_REGION_NSEC_ALL_ACCESS_RDWR \ ++ (TZC_REGION_ACCESS_RDWR(STM32MP1_TZC_A7_ID) | \ ++ TZC_REGION_ACCESS_RDWR(STM32MP1_TZC_GPU_ID) | \ ++ TZC_REGION_ACCESS_RDWR(STM32MP1_TZC_LCD_ID) | \ ++ TZC_REGION_ACCESS_RDWR(STM32MP1_TZC_MDMA_ID) | \ ++ TZC_REGION_ACCESS_RDWR(STM32MP1_TZC_M4_ID) | \ ++ TZC_REGION_ACCESS_RDWR(STM32MP1_TZC_DMA_ID) | \ ++ TZC_REGION_ACCESS_RDWR(STM32MP1_TZC_USB_HOST_ID) | \ ++ TZC_REGION_ACCESS_RDWR(STM32MP1_TZC_USB_OTG_ID) | \ ++ TZC_REGION_ACCESS_RDWR(STM32MP1_TZC_SDMMC_ID) | \ ++ TZC_REGION_ACCESS_RDWR(STM32MP1_TZC_ETH_ID) | \ ++ TZC_REGION_ACCESS_RDWR(STM32MP1_TZC_DAP_ID)) ++ ++#endif /* _DT_BINDINGS_STM32MP15_TZC400_H */ +diff --git a/core/include/io.h b/core/include/io.h +index 03f7ce167..d7c37b169 100644 +--- a/core/include/io.h ++++ b/core/include/io.h +@@ -35,6 +35,11 @@ static inline void io_write32(vaddr_t addr, uint32_t val) + *(volatile uint32_t *)addr = val; + } + ++static inline void io_write64(vaddr_t addr, uint64_t val) ++{ ++ *(volatile uint64_t *)addr = val; ++} ++ + static inline uint8_t io_read8(vaddr_t addr) + { + return *(volatile uint8_t *)addr; +@@ -50,6 +55,11 @@ static inline uint32_t io_read32(vaddr_t addr) + return *(volatile uint32_t *)addr; + } + ++static inline uint64_t io_read64(vaddr_t addr) ++{ ++ return *(volatile uint64_t *)addr; ++} ++ + static inline void io_mask8(vaddr_t addr, uint8_t val, uint8_t mask) + { + io_write8(addr, (io_read8(addr) & ~mask) | (val & mask)); +diff --git a/core/include/kernel/dt.h b/core/include/kernel/dt.h +index 109a1a32f..4cd195cb1 100644 +--- a/core/include/kernel/dt.h ++++ b/core/include/kernel/dt.h +@@ -67,6 +67,8 @@ enum dt_driver_type { + DT_DRIVER_UART, + DT_DRIVER_CLK, + DT_DRIVER_RSTCTRL, ++ DT_DRIVER_PINCTRL, ++ DT_DRIVER_I2C, + }; + + /* +@@ -193,6 +195,39 @@ int _fdt_get_status(const void *fdt, int offs); + */ + void _fdt_fill_device_info(const void *fdt, struct dt_node_info *info, + int node); ++/* ++ * Read cells from a given property of the given node. Any number of 32-bit ++ * cells of the property can be read. Returns 0 on success, or a negative ++ * FDT error value otherwise. ++ */ ++int _fdt_read_uint32_array(const void *fdt, int node, const char *prop_name, ++ uint32_t *array, uint32_t count); ++ ++/* ++ * Read one cell from a given property of the given node. ++ * Returns 0 on success, or a negative ++ * FDT error value otherwise. ++ */ ++int _fdt_read_uint32(const void *fdt, int node, const char *prop_name, ++ uint32_t *value); ++ ++/* ++ * Read one cell from a given property of the given node. ++ * if cell is empty then return the default value (dflt_value) ++ * Returns 0 on success, or a negative ++ * FDT error value otherwise. ++ */ ++uint32_t _fdt_read_uint32_default(const void *fdt, int node, ++ const char *prop_name, uint32_t dflt_value); ++ ++/* ++ * Check whether the node at @node contains a property. ++ * ++ * @node is the offset of the node that describes the device in @fdt. ++ * ++ * Returns true on success or false if no property ++ */ ++bool _fdt_check_node(const void *fdt, int node); + + #else /* !CFG_DT */ + +@@ -233,8 +268,36 @@ static inline void _fdt_fill_device_info(const void *fdt __unused, + { + panic(); + } ++ ++static inline int _fdt_read_uint32_array(const void *fdt, int node, ++ const char *prop_name, ++ uint32_t *array, uint32_t count) { ++ return -1; ++} ++ ++static inline int _fdt_read_uint32(const void *fdt, int node, ++ const char *prop_name, ++ uint32_t *value) { ++ return -1; ++} ++ ++static inline uint32_t _fdt_read_uint32_default(const void *fdt, int node, ++ const char *prop_name, ++ uint32_t dflt_value) ++{ ++ return dflt_value; ++} ++ ++static inline bool _fdt_check_node(const void *fdt, int node) ++{ ++ return false; ++} ++ + #endif /* !CFG_DT */ + ++TEE_Result add_probe_node_by_compat(const void *fdt, int node, ++ const char *compat); ++ + #define for_each_dt_driver(drv) \ + for (drv = SCATTERED_ARRAY_BEGIN(dt_drivers, struct dt_driver); \ + drv < SCATTERED_ARRAY_END(dt_drivers, struct dt_driver); \ +diff --git a/core/include/kernel/dt_driver.h b/core/include/kernel/dt_driver.h +index 8054a0a01..4fe140622 100644 +--- a/core/include/kernel/dt_driver.h ++++ b/core/include/kernel/dt_driver.h +@@ -2,7 +2,6 @@ + /* + * Copyright (c) 2021, Linaro Limited + * Copyright (c) 2021, Bootlin +- * Copyright (c) 2021, Linaro Limited + * Copyright (c) 2021, STMicroelectronics + */ + +@@ -100,6 +99,23 @@ void dt_driver_crypt_init_complete(void); + static inline void dt_driver_crypt_init_complete(void) {} + #endif + ++/* ++ * dt_driver_device_from_node - Return a device instance based on a ++ * driver type and FDT information ++ * ++ * @nodeoffset: node offset in the FDT ++ * @type: Driver type ++ * @res: Output result code of the operation: TEE_ERROR_DEFER_DRIVER_INIT ++ * is target device is not yet initialized, otherwise any other compliant ++ * code. ++ * ++ * Return a device opaque reference, e.g. a struct i2c_handle_s pointer ++ * for an I2C driver, or NULL if not found in which case @res provides ++ * the error code. ++ */ ++void *dt_driver_device_from_node(int nodeoffset, enum dt_driver_type type, ++ TEE_Result *res); ++ + /* + * Return driver provider reference from its node offset value in the FDT + */ +diff --git a/core/include/kernel/interrupt.h b/core/include/kernel/interrupt.h +index 9870049a9..84c2edb4f 100644 +--- a/core/include/kernel/interrupt.h ++++ b/core/include/kernel/interrupt.h +@@ -38,6 +38,11 @@ struct itr_ops { + uint8_t cpu_mask); + void (*set_affinity)(struct itr_chip *chip, size_t it, + uint8_t cpu_mask); ++#if !defined(CFG_ARM_GICV3) ++ uint8_t (*set_pmr)(struct itr_chip *chip, uint8_t mask); ++ uint8_t (*set_ipriority)(struct itr_chip *chip, size_t it, ++ uint8_t mask); ++#endif + }; + + enum itr_return { +@@ -115,6 +120,17 @@ void itr_set_affinity(size_t it, uint8_t cpu_mask); + */ + void itr_core_handler(void); + ++/* ++ * Set the Priority Mask Regarding and return its previous value ++ */ ++uint8_t itr_set_pmr(uint8_t mask); ++ ++/* ++ * Set the targe tinterrupt priority mask and return its previous value ++ */ ++uint8_t itr_set_ipriority(size_t it, uint8_t mask); ++ ++ + static inline void itr_add(struct itr_handler *handler) + { + itr_add_type_prio(handler, IRQ_TYPE_NONE, 0); +@@ -128,4 +144,5 @@ static inline struct itr_handler *itr_alloc_add(size_t it, + 0); + } + ++ + #endif /*__KERNEL_INTERRUPT_H*/ +diff --git a/core/include/kernel/panic.h b/core/include/kernel/panic.h +index 9dc38dce1..308ea37d0 100644 +--- a/core/include/kernel/panic.h ++++ b/core/include/kernel/panic.h +@@ -8,6 +8,12 @@ + + #include + ++/* ++ * Platform can define a panic sequence to trap cpu/reset core or system ++ * after eventual debug trace. ++ */ ++void plat_panic(void); ++ + /* debug disabled => __FILE__, ... and panic message are not built. */ + #if defined(CFG_TEE_CORE_DEBUG) + #define __panic(str) __do_panic(__FILE__, __LINE__, __func__, str) +diff --git a/core/include/optee_rpc_cmd.h b/core/include/optee_rpc_cmd.h +index 816f0a24f..735fb1710 100644 +--- a/core/include/optee_rpc_cmd.h ++++ b/core/include/optee_rpc_cmd.h +@@ -182,6 +182,18 @@ + * Definition of protocol for command OPTEE_RPC_CMD_FS + */ + ++/* ++ * Send an OCALL to the Client Application ++ * ++ * [in] value[0].a CA Command ID (i.e., OCALL# for the CA to execute) ++ * [out] value[0].b OCALL return value ++ * [out] value[0].c OCALL return value origin ++ * [in] value[1].a UUID of TA whence OCALL originated (HI bits) ++ * [out] value[1].b UUID of TA whence OCALL originated (LO bits) ++ * [in/out] any[2..5].* OCALL parameters as specified by the TA, if any ++ */ ++#define OPTEE_RPC_CMD_OCALL 22 ++ + /* + * Open a file + * +diff --git a/core/include/tee/tui.h b/core/include/tee/tui.h +new file mode 100644 +index 000000000..1385908c5 +--- /dev/null ++++ b/core/include/tee/tui.h +@@ -0,0 +1,51 @@ ++/* SPDX-License-Identifier: BSD-2-Clause */ ++/* ++ * Copyright (c) 2016-2021, Linaro Limited ++ */ ++ ++#ifndef __TEE__TUI_H ++#define __TEE__TUI_H ++ ++#include ++#include ++#include ++ ++/* TUI session timeout in milliseconds */ ++#define TUI_SESSION_TIMEOUT 10000 ++ ++#define TUI_PROP_SECURITY_INDICATOR 0 ++ ++struct tee_ta_session; ++ ++#ifdef CFG_WITH_TUI ++TEE_Result tui_init_session(struct ts_session *s); ++ ++TEE_Result tui_set_session_busy(struct ts_session *s); ++ ++TEE_Result tui_clear_session_busy(struct ts_session *s); ++ ++TEE_Result tui_close_session(struct ts_session *s); ++ ++TEE_Result tui_get_time_to_session_timeout(struct ts_session *s, ++ const TEE_Time *current_time, ++ uint32_t *millis); ++ ++TEE_Result tui_input_enable(void); ++void tui_input_disable(void); ++void tui_async_input(uint16_t key, uint16_t x, uint16_t y); ++TEE_Result tui_input_get(struct ts_session *s, size_t timeout, ++ bool *is_timedout, bool *is_key, ++ uint32_t *key, size_t *xpos, size_t *ypos); ++ ++#else ++static inline void tui_async_input(uint16_t key __unused, uint16_t x __unused, ++ uint16_t y __unused) ++{ ++} ++ ++static inline TEE_Result tui_close_session(struct ts_session *s __unused) ++{ ++ return TEE_SUCCESS; ++} ++#endif ++#endif /*__TEE__TUI_H*/ +diff --git a/core/kernel/dt.c b/core/kernel/dt.c +index b227e469d..5dfcf6b79 100644 +--- a/core/kernel/dt.c ++++ b/core/kernel/dt.c +@@ -281,3 +281,52 @@ void _fdt_fill_device_info(const void *fdt, struct dt_node_info *info, int offs) + + *info = dinfo; + } ++ ++int _fdt_read_uint32_array(const void *fdt, int node, const char *prop_name, ++ uint32_t *array, uint32_t count) ++{ ++ const fdt32_t *cuint = NULL; ++ int len = 0; ++ uint32_t i = 0; ++ ++ cuint = fdt_getprop(fdt, node, prop_name, &len); ++ if (!cuint) ++ return -FDT_ERR_NOTFOUND; ++ ++ if ((uint32_t)len != (count * sizeof(uint32_t))) ++ return -FDT_ERR_BADLAYOUT; ++ ++ for (i = 0; i < ((uint32_t)len / sizeof(uint32_t)); i++) { ++ *array = fdt32_to_cpu(*cuint); ++ array++; ++ cuint++; ++ } ++ ++ return 0; ++} ++ ++int _fdt_read_uint32(const void *fdt, int node, const char *prop_name, ++ uint32_t *value) ++{ ++ return _fdt_read_uint32_array(fdt, node, prop_name, value, 1); ++} ++ ++uint32_t _fdt_read_uint32_default(const void *fdt, int node, ++ const char *prop_name, uint32_t dflt_value) ++{ ++ uint32_t ret = dflt_value; ++ int err = _fdt_read_uint32(fdt, node, prop_name, &ret); ++ ++ if (err < 0) ++ return dflt_value; ++ ++ return ret; ++} ++ ++bool _fdt_check_node(const void *fdt, int node) ++{ ++ int len = 0; ++ const char *cchar = fdt_get_name(fdt, node, &len); ++ ++ return cchar && (len >= 0); ++} +diff --git a/core/kernel/dt_driver.c b/core/kernel/dt_driver.c +index f016a7ece..e4d141709 100644 +--- a/core/kernel/dt_driver.c ++++ b/core/kernel/dt_driver.c +@@ -2,8 +2,7 @@ + /* + * Copyright (c) 2021, Linaro Limited + * Copyright (c) 2021, Bootlin +- * Copyright (c) 2021, Linaro Limited +- * Copyright (c) 2021, STMicroelectronics ++ * Copyright (c) 2022, STMicroelectronics + */ + + #include +@@ -106,6 +105,8 @@ static void assert_type_is_valid(enum dt_driver_type type) + case DT_DRIVER_CLK: + case DT_DRIVER_RSTCTRL: + case DT_DRIVER_UART: ++ case DT_DRIVER_PINCTRL: ++ case DT_DRIVER_I2C: + return; + default: + assert(0); +@@ -125,17 +126,32 @@ TEE_Result dt_driver_register_provider(const void *fdt, int nodeoffset, + uint32_t phandle = 0; + + assert_type_is_valid(type); ++ switch (type) { ++ case DT_DRIVER_CLK: ++ provider_cells = fdt_get_dt_driver_cells(fdt, nodeoffset, type); ++ if (provider_cells < 0) { ++ DMSG("Failed to find provider cells: %d", ++ provider_cells); ++ return TEE_ERROR_GENERIC; ++ } + +- provider_cells = fdt_get_dt_driver_cells(fdt, nodeoffset, type); +- if (provider_cells < 0) { +- DMSG("Failed to find provider cells: %d", provider_cells); +- return TEE_ERROR_GENERIC; +- } +- +- phandle = fdt_get_phandle(fdt, nodeoffset); +- if (!phandle || phandle == (uint32_t)-1) { +- DMSG("Failed to find provide phandle"); +- return TEE_ERROR_GENERIC; ++ phandle = fdt_get_phandle(fdt, nodeoffset); ++ if (!phandle || phandle == (uint32_t)-1) { ++ DMSG("Failed to find provide phandle"); ++ return TEE_ERROR_GENERIC; ++ } ++ break; ++ case DT_DRIVER_PINCTRL: ++ phandle = fdt_get_phandle(fdt, nodeoffset); ++ if (!phandle || phandle == (uint32_t)-1) { ++ DMSG("Failed to find provide phandle"); ++ return TEE_ERROR_GENERIC; ++ } ++ break; ++ case DT_DRIVER_I2C: ++ break; ++ default: ++ panic("Trying to register unknown type of provider"); + } + + prv = calloc(1, sizeof(*prv)); +@@ -172,6 +188,8 @@ int fdt_get_dt_driver_cells(const void *fdt, int nodeoffset, + case DT_DRIVER_RSTCTRL: + cells_name = "#reset-cells"; + break; ++ case DT_DRIVER_PINCTRL: ++ return 0; + default: + panic(); + } +@@ -288,6 +306,21 @@ void *dt_driver_device_from_node_idx_prop(const char *prop_name, + return NULL; + } + ++void *dt_driver_device_from_node(int nodeoffset, enum dt_driver_type type, ++ TEE_Result *res) ++{ ++ struct dt_driver_provider *prv = NULL; ++ ++ prv = dt_driver_get_provider_by_node(nodeoffset, type); ++ if (!prv) { ++ /* No provider registered yet */ ++ *res = TEE_ERROR_DEFER_DRIVER_INIT; ++ return NULL; ++ } ++ ++ return prv->get_of_device(NULL, prv->priv_data, res); ++} ++ + static void __maybe_unused print_probe_list(const void *fdt __maybe_unused) + { + struct dt_driver_probe *elt = NULL; +@@ -562,8 +595,8 @@ static TEE_Result add_node_to_probe(const void *fdt, int node, + * TEE_ERROR_ITEM_NOT_FOUND if no matching driver + * TEE_ERROR_OUT_OF_MEMORY if heap is exhausted + */ +-static TEE_Result add_probe_node_by_compat(const void *fdt, int node, +- const char *compat) ++TEE_Result add_probe_node_by_compat(const void *fdt, int node, ++ const char *compat) + { + TEE_Result res = TEE_ERROR_ITEM_NOT_FOUND; + const struct dt_driver *dt_drv = NULL; +@@ -605,7 +638,6 @@ TEE_Result dt_driver_maybe_add_probe_node(const void *fdt, int node) + int len = 0; + int count = 0; + const char *compat = NULL; +- TEE_Result res = TEE_ERROR_GENERIC; + + if (_fdt_get_status(fdt, node) == DT_STATUS_DISABLED) + return TEE_SUCCESS; +@@ -618,11 +650,8 @@ TEE_Result dt_driver_maybe_add_probe_node(const void *fdt, int node) + compat = fdt_stringlist_get(fdt, node, "compatible", idx, &len); + assert(compat && len > 0); + +- res = add_probe_node_by_compat(fdt, node, compat); + +- /* Stop lookup if something was found */ +- if (res != TEE_ERROR_ITEM_NOT_FOUND) +- return res; ++ add_probe_node_by_compat(fdt, node, compat); + } + + return TEE_SUCCESS; +diff --git a/core/kernel/interrupt.c b/core/kernel/interrupt.c +index 0986eddf6..59d2f86e6 100644 +--- a/core/kernel/interrupt.c ++++ b/core/kernel/interrupt.c +@@ -138,3 +138,13 @@ void __weak __noreturn itr_core_handler(void) + { + panic("Secure interrupt handler not defined"); + } ++ ++uint8_t itr_set_pmr(uint8_t mask) ++{ ++ return itr_chip->ops->set_pmr(itr_chip, mask); ++} ++ ++uint8_t itr_set_ipriority(size_t it, uint8_t mask) ++{ ++ return itr_chip->ops->set_ipriority(itr_chip, it, mask); ++} +diff --git a/core/kernel/notif.c b/core/kernel/notif.c +index 9951c2652..62ce180f7 100644 +--- a/core/kernel/notif.c ++++ b/core/kernel/notif.c +@@ -97,7 +97,6 @@ void notif_send_async(uint32_t value) + + COMPILE_TIME_ASSERT(NOTIF_VALUE_DO_BOTTOM_HALF == + OPTEE_SMC_ASYNC_NOTIF_VALUE_DO_BOTTOM_HALF); +- COMPILE_TIME_ASSERT(CFG_CORE_ASYNC_NOTIF_GIC_INTID >= GIC_SPI_BASE); + + assert(value <= NOTIF_ASYNC_VALUE_MAX); + old_itr_status = cpu_spin_lock_xsave(¬if_lock); +diff --git a/core/kernel/panic.c b/core/kernel/panic.c +index 18028fbfd..20b4978bf 100644 +--- a/core/kernel/panic.c ++++ b/core/kernel/panic.c +@@ -7,6 +7,7 @@ + #include + #include + #include ++#include + #include + + void __do_panic(const char *file __maybe_unused, +@@ -17,8 +18,6 @@ void __do_panic(const char *file __maybe_unused, + /* disable prehemption */ + (void)thread_mask_exceptions(THREAD_EXCP_ALL); + +- /* TODO: notify other cores */ +- + /* trace: Panic ['panic-string-message' ]at FILE:LINE []" */ + if (!file && !func && !msg) + EMSG_RAW("Panic"); +@@ -29,11 +28,19 @@ void __do_panic(const char *file __maybe_unused, + func ? "<" : "", func ? func : "", func ? ">" : ""); + + print_kernel_stack(); +- /* abort current execution */ +- while (1) ++ plat_panic(); ++ ++ EMSG("platform failed to abort execution"); ++ while (true) + cpu_idle(); + } + + void __weak cpu_idle(void) + { + } ++ ++void __weak __noreturn plat_panic(void) ++{ ++ while (true) ++ cpu_idle(); ++} +diff --git a/core/kernel/tee_ta_manager.c b/core/kernel/tee_ta_manager.c +index 8faeeec05..13ea7db6c 100644 +--- a/core/kernel/tee_ta_manager.c ++++ b/core/kernel/tee_ta_manager.c +@@ -29,6 +29,7 @@ + #include + #include + #include ++#include + #include + #include + #include +@@ -296,6 +297,7 @@ static void destroy_session(struct tee_ta_session *s, + } + #endif + ++ tui_close_session(&s->ts_sess); + tee_ta_unlink_session(s, open_sessions); + #if defined(CFG_TA_GPROF_SUPPORT) + free(s->ts_sess.sbuf); +@@ -765,7 +767,7 @@ TEE_Result tee_ta_open_session(TEE_ErrorOrigin *err, + *err = s->err_origin; + + if (res != TEE_SUCCESS) +- EMSG("Failed. Return error 0x%x", res); ++ EMSG("Failed. TA %pUl Return error 0x%x", uuid, res); + + return res; + } +diff --git a/core/mm/mobj.c b/core/mm/mobj.c +index c7c34548a..c4d02824b 100644 +--- a/core/mm/mobj.c ++++ b/core/mm/mobj.c +@@ -51,6 +51,7 @@ static void *mobj_phys_get_va(struct mobj *mobj, size_t offset, size_t len) + + return (void *)(moph->va + offset); + } ++DECLARE_KEEP_PAGER(mobj_phys_get_va); + + static TEE_Result mobj_phys_get_pa(struct mobj *mobj, size_t offs, + size_t granule, paddr_t *pa) +@@ -259,7 +260,7 @@ static void *mobj_mm_get_va(struct mobj *mobj, size_t offs, size_t len) + return mobj_get_va(to_mobj_mm(mobj)->parent_mobj, + mobj_mm_offs(mobj, offs), len); + } +- ++DECLARE_KEEP_PAGER(mobj_mm_get_va); + + static TEE_Result mobj_mm_get_pa(struct mobj *mobj, size_t offs, + size_t granule, paddr_t *pa) +@@ -731,4 +732,4 @@ static TEE_Result mobj_init(void) + return TEE_SUCCESS; + } + +-driver_init_late(mobj_init); ++service_init(mobj_init); +diff --git a/core/pta/scmi.c b/core/pta/scmi.c +index e85d70b76..7d00a3056 100644 +--- a/core/pta/scmi.c ++++ b/core/pta/scmi.c +@@ -7,9 +7,17 @@ + #include + #include + #include ++#include + #include + #include + #include ++#include ++#include ++ ++static bool valid_caps(unsigned int caps) ++{ ++ return (caps & ~PTA_SCMI_CAPS_VALID_MASK) == 0; ++} + + static TEE_Result cmd_capabilities(uint32_t ptypes, + TEE_Param param[TEE_NUM_PARAMS]) +@@ -25,6 +33,8 @@ static TEE_Result cmd_capabilities(uint32_t ptypes, + + if (IS_ENABLED(CFG_SCMI_MSG_SMT)) + caps |= PTA_SCMI_CAPS_SMT_HEADER; ++ if (IS_ENABLED(CFG_CORE_OCALL)) ++ caps |= PTA_SCMI_CAPS_OCALL_THREAD; + + param[0].value.a = caps; + param[0].value.b = 0; +@@ -97,6 +107,116 @@ static TEE_Result cmd_process_smt_message(uint32_t ptypes, + return TEE_ERROR_NOT_SUPPORTED; + } + ++/* Process an OCALL RPC to client and report status */ ++static enum optee_scmi_ocall_reply pta_scmi_ocall(uint32_t channel_id) ++{ ++ static const TEE_UUID uuid = PTA_SCMI_UUID; ++ static uint64_t uuid_octet[2]; ++ static bool uuid_ready; ++ struct thread_param params[THREAD_RPC_MAX_NUM_PARAMS] = { ++ /* Ocall command, sub command */ ++ THREAD_PARAM_VALUE(INOUT, 0, 0, 0), ++ /* UUID of Ocall initiator */ ++ THREAD_PARAM_VALUE(IN, 0, 0, 0), ++ /* Output value argument to get REE feedback */ ++ THREAD_PARAM_VALUE(OUT, 0, 0, 0), ++ }; ++ uint64_t ocall_res = 0; ++ uint64_t __maybe_unused ocall_ori = 0; ++ enum optee_scmi_ocall_reply agent_request = PTA_SCMI_OCALL_ERROR; ++ TEE_Result res = TEE_ERROR_GENERIC; ++ ++ if (!IS_ENABLED(CFG_CORE_OCALL)) ++ return PTA_SCMI_OCALL_ERROR; ++ ++ if (!uuid_ready) { ++ tee_uuid_to_octets((uint8_t *)uuid_octet, &uuid); ++ uuid_ready = true; ++ } ++ ++ params[0].u.value.a = PTA_SCMI_OCALL_CMD_THREAD_READY; ++ params[1].u.value.a = uuid_octet[0]; ++ params[1].u.value.b = uuid_octet[1]; ++ ++ params[0] = THREAD_PARAM_VALUE(INOUT, PTA_SCMI_OCALL_CMD_THREAD_READY, ++ 0, 0); ++ params[1] = THREAD_PARAM_VALUE(IN, uuid_octet[0], uuid_octet[1], 0); ++ params[2] = THREAD_PARAM_VALUE(OUT, 0, 0, 0); ++ ++ res = thread_rpc_cmd(OPTEE_RPC_CMD_OCALL, ARRAY_SIZE(params), params); ++ if (res) { ++ DMSG("Close thread on RPC error %#"PRIx32, res); ++ return PTA_SCMI_OCALL_ERROR; ++ } ++ ++ ocall_res = params[0].u.value.b; ++ ocall_ori = params[0].u.value.c; ++ if (ocall_res) { ++ DMSG("SCMI RPC thread failed %#"PRIx64" from %#"PRIx64, ++ ocall_res, ocall_ori); ++ return PTA_SCMI_OCALL_ERROR; ++ } ++ ++ agent_request = (enum optee_scmi_ocall_reply)params[2].u.value.a; ++ ++ switch (agent_request) { ++ case PTA_SCMI_OCALL_PROCESS_SMT_CHANNEL: ++ FMSG("Posting message on channel %u"PRIu32, channel_id); ++ if (IS_ENABLED(CFG_SCMI_MSG_SMT)) ++ scmi_smt_threaded_entry(channel_id); ++ else ++ panic(); ++ break; ++ case PTA_SCMI_OCALL_CLOSE_THREAD: ++ FMSG("Closing channel %u"PRIu32, channel_id); ++ break; ++ case PTA_SCMI_OCALL_ERROR: ++ FMSG("Error on channel %u"PRIu32, channel_id); ++ break; ++ default: ++ DMSG("Invalid Ocall cmd %#x on channel %u"PRIu32, ++ agent_request, channel_id); ++ return PTA_SCMI_OCALL_CLOSE_THREAD; ++ } ++ ++ return agent_request; ++} ++ ++static TEE_Result cmd_scmi_ocall_thread(uint32_t ptypes, ++ TEE_Param params[TEE_NUM_PARAMS]) ++{ ++ const uint32_t exp_ptypes = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_NONE, ++ TEE_PARAM_TYPE_NONE, ++ TEE_PARAM_TYPE_NONE); ++ ++ uint32_t channel_id = (int)params[0].value.a; ++ struct scmi_msg_channel *channel = NULL; ++ ++ if (ptypes != exp_ptypes) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ if (IS_ENABLED(CFG_SCMI_MSG_SMT)) ++ channel = plat_scmi_get_channel(channel_id); ++ else ++ return TEE_ERROR_NOT_SUPPORTED; ++ ++ if (!channel) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ FMSG("Enter Ocall thread on channel %u"PRIu32, channel_id); ++ while (1) { ++ switch (pta_scmi_ocall(channel_id)) { ++ case PTA_SCMI_OCALL_PROCESS_SMT_CHANNEL: ++ continue; ++ case PTA_SCMI_OCALL_CLOSE_THREAD: ++ return TEE_SUCCESS; ++ default: ++ return TEE_ERROR_GENERIC; ++ } ++ } ++} ++ + static TEE_Result cmd_get_channel_handle(uint32_t ptypes, + TEE_Param params[TEE_NUM_PARAMS]) + { +@@ -107,13 +227,13 @@ static TEE_Result cmd_get_channel_handle(uint32_t ptypes, + unsigned int channel_id = params[0].value.a; + unsigned int caps = params[0].value.b; + +- if (ptypes != exp_ptypes) ++ if (ptypes != exp_ptypes || !valid_caps(caps)) + return TEE_ERROR_BAD_PARAMETERS; + + if (IS_ENABLED(CFG_SCMI_MSG_SMT)) { + struct scmi_msg_channel *channel = NULL; + +- if (caps != PTA_SCMI_CAPS_SMT_HEADER) ++ if (!(caps & PTA_SCMI_CAPS_SMT_HEADER)) + return TEE_ERROR_NOT_SUPPORTED; + + channel = plat_scmi_get_channel(channel_id); +@@ -163,6 +283,8 @@ static TEE_Result pta_scmi_invoke_command(void *session __unused, uint32_t cmd, + return cmd_process_smt_message(ptypes, params); + case PTA_SCMI_CMD_GET_CHANNEL_HANDLE: + return cmd_get_channel_handle(ptypes, params); ++ case PTA_SCMI_CMD_OCALL_THREAD: ++ return cmd_scmi_ocall_thread(ptypes, params); + default: + return TEE_ERROR_NOT_SUPPORTED; + } +diff --git a/core/pta/sub.mk b/core/pta/sub.mk +index 60e747530..e43b787a2 100644 +--- a/core/pta/sub.mk ++++ b/core/pta/sub.mk +@@ -12,5 +12,6 @@ srcs-$(CFG_SCP03_PTA) += scp03.c + srcs-$(CFG_APDU_PTA) += apdu.c + srcs-$(CFG_SCMI_PTA) += scmi.c + srcs-$(CFG_HWRNG_PTA) += hwrng.c ++srcs-$(CFG_WITH_TUI) += tui.c + + subdirs-y += bcm +diff --git a/core/pta/tui.c b/core/pta/tui.c +new file mode 100644 +index 000000000..8c01be81d +--- /dev/null ++++ b/core/pta/tui.c +@@ -0,0 +1,213 @@ ++// SPDX-License-Identifier: BSD-2-Clause ++/* ++ * Copyright (c) 2016-2021, Linaro Limited ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++static TEE_Result taf_get_screen_info(struct ts_session *s __unused, ++ uint32_t param_types, ++ TEE_Param params[TEE_NUM_PARAMS]) ++{ ++ struct frame_buffer *fb; ++ uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_OUTPUT, ++ TEE_PARAM_TYPE_VALUE_OUTPUT, ++ TEE_PARAM_TYPE_VALUE_OUTPUT, ++ TEE_PARAM_TYPE_NONE); ++ ++ if (exp_pt != param_types) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ fb = display_get_frame_buffer(); ++ params[0].value.a = fb->width; ++ params[0].value.b = fb->height; ++ params[1].value.a = fb->width_dpi; ++ params[1].value.b = fb->height_dpi; ++ COMPILE_TIME_ASSERT(PTA_TUI_SCREEN_24BPP == FB_24BPP); ++ params[2].value.a = fb->bpp; ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result taf_init_session(struct ts_session *s, ++ uint32_t param_types, ++ TEE_Param params[TEE_NUM_PARAMS] __unused) ++{ ++ TEE_Result res; ++ uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_NONE, ++ TEE_PARAM_TYPE_NONE, ++ TEE_PARAM_TYPE_NONE, ++ TEE_PARAM_TYPE_NONE); ++ ++ if (exp_pt != param_types) ++ return TEE_ERROR_BAD_PARAMETERS; ++ res = tui_init_session(s); ++ if (res == TEE_SUCCESS) ++ tui_clear_session_busy(s); ++ return res; ++} ++ ++static TEE_Result taf_close_session(struct ts_session *s, ++ uint32_t param_types, ++ TEE_Param params[TEE_NUM_PARAMS] __unused) ++{ ++ uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_NONE, ++ TEE_PARAM_TYPE_NONE, ++ TEE_PARAM_TYPE_NONE, ++ TEE_PARAM_TYPE_NONE); ++ ++ if (exp_pt != param_types) ++ return TEE_ERROR_BAD_PARAMETERS; ++ return tui_close_session(s); ++} ++ ++static TEE_Result taf_init_screen(struct ts_session *s, ++ uint32_t param_types, ++ TEE_Param params[TEE_NUM_PARAMS]) ++{ ++ TEE_Result res; ++ struct frame_buffer *fb; ++ uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_NONE, ++ TEE_PARAM_TYPE_NONE, ++ TEE_PARAM_TYPE_NONE); ++ ++ if (exp_pt != param_types) ++ return TEE_ERROR_BAD_PARAMETERS; ++ res = tui_set_session_busy(s); ++ if (res != TEE_SUCCESS) ++ return res; ++ fb = display_get_frame_buffer(); ++ frame_buffer_clear(fb, params[0].value.a); ++ tui_clear_session_busy(s); ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result taf_set_screen_image(struct ts_session *s, ++ uint32_t param_types, ++ TEE_Param params[TEE_NUM_PARAMS]) ++{ ++ TEE_Result res; ++ struct frame_buffer *fb; ++ uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_MEMREF_INPUT, ++ TEE_PARAM_TYPE_NONE); ++ ++ if (exp_pt != param_types || !params[2].memref.buffer) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ fb = display_get_frame_buffer(); ++ if (params[2].memref.size != ++ frame_buffer_get_image_size(fb, params[1].value.a, ++ params[1].value.b)) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ res = tui_set_session_busy(s); ++ if (res != TEE_SUCCESS) ++ return res; ++ ++ frame_buffer_set_image(fb, params[0].value.a, params[0].value.b, ++ params[1].value.a, params[1].value.b, ++ params[2].memref.buffer); ++ ++ tui_clear_session_busy(s); ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result taf_display_screen(struct ts_session *s, ++ uint32_t param_types, ++ TEE_Param params[TEE_NUM_PARAMS]) ++{ ++ TEE_Result res; ++ bool clear_input; ++ bool is_key; ++ bool is_timedout; ++ size_t timeout; ++ uint32_t key; ++ size_t xpos; ++ size_t ypos; ++ uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_VALUE_OUTPUT, ++ TEE_PARAM_TYPE_VALUE_OUTPUT, ++ TEE_PARAM_TYPE_VALUE_OUTPUT); ++ ++ if (exp_pt != param_types) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ res = tui_set_session_busy(s); ++ if (res != TEE_SUCCESS) ++ return res; ++ ++ clear_input = params[0].value.a; ++ timeout = params[0].value.b; ++ ++ if (clear_input) ++ tui_input_enable(); ++ ++ res = tui_input_get(s, timeout, &is_timedout, ++ &is_key, &key, &xpos, &ypos); ++ ++ params[1].value.a = is_timedout; ++ params[2].value.a = is_key; ++ params[2].value.b = key; ++ params[3].value.a = xpos; ++ params[3].value.b = ypos; ++ tui_clear_session_busy(s); ++ return res; ++} ++ ++typedef TEE_Result (*ta_func)(struct ts_session *s, uint32_t param_types, ++ TEE_Param params[TEE_NUM_PARAMS]); ++ ++static const ta_func ta_funcs[] = { ++ [PTA_TUI_GET_SCREEN_INFO] = taf_get_screen_info, ++ [PTA_TUI_INIT_SESSION] = taf_init_session, ++ [PTA_TUI_CLOSE_SESSION] = taf_close_session, ++ [PTA_TUI_INIT_SCREEN] = taf_init_screen, ++ [PTA_TUI_SET_SCREEN_IMAGE] = taf_set_screen_image, ++ [PTA_TUI_DISPLAY_SCREEN] = taf_display_screen, ++}; ++ ++static TEE_Result open_session(uint32_t param_types __unused, ++ TEE_Param pParams[TEE_NUM_PARAMS] __unused, ++ void **sess_ctx __unused) ++{ ++ struct ts_session *s = NULL; ++ ++ /* Check that we're called from a TA */ ++ s = ts_get_calling_session(); ++ if (!s) ++ return TEE_ERROR_ACCESS_DENIED; ++ if (!is_user_ta_ctx(s->ctx)) ++ return TEE_ERROR_ACCESS_DENIED; ++ ++ return TEE_SUCCESS; ++} ++ ++static void close_session(void *sess_ctx __unused) ++{ ++} ++ ++static TEE_Result invoke_command(void *sess_ctx __unused, uint32_t cmd_id, ++ uint32_t param_types, ++ TEE_Param params[TEE_NUM_PARAMS]) ++{ ++ struct ts_session *s = ts_get_calling_session(); ++ ++ if (cmd_id < ARRAY_SIZE(ta_funcs) && ta_funcs[cmd_id]) ++ return ta_funcs[cmd_id](s, param_types, params); ++ ++ return TEE_ERROR_NOT_IMPLEMENTED; ++} ++ ++pseudo_ta_register(.uuid = PTA_TUI_UUID, .name = "tui", ++ .flags = PTA_DEFAULT_FLAGS, ++ .open_session_entry_point = open_session, ++ .close_session_entry_point = close_session, ++ .invoke_command_entry_point = invoke_command); +diff --git a/core/tee/entry_std.c b/core/tee/entry_std.c +index 5f44521d7..9ba766293 100644 +--- a/core/tee/entry_std.c ++++ b/core/tee/entry_std.c +@@ -606,4 +606,4 @@ static TEE_Result default_mobj_init(void) + return TEE_SUCCESS; + } + +-driver_init_late(default_mobj_init); ++service_init(default_mobj_init); +diff --git a/core/tee/sub.mk b/core/tee/sub.mk +index 6847e0669..04a42a67d 100644 +--- a/core/tee/sub.mk ++++ b/core/tee/sub.mk +@@ -48,3 +48,4 @@ endif + + srcs-y += uuid.c + srcs-y += tee_supp_plugin_rpc.c ++srcs-$(CFG_WITH_TUI) += tui.c +diff --git a/core/tee/tee_svc.c b/core/tee/tee_svc.c +index 4d6dcd075..908a5e731 100644 +--- a/core/tee/tee_svc.c ++++ b/core/tee/tee_svc.c +@@ -21,6 +21,7 @@ + #include + #include + #include ++#include + #include + #include + #include +@@ -323,6 +324,20 @@ const struct tee_props tee_propset_tee[] = { + }, + #endif + ++#ifdef CFG_WITH_TUI ++ { ++ .name = "gpd.tee.tui.securityIndicator", ++ .prop_type = USER_TA_PROP_TYPE_BOOL, ++ .data = &(uint32_t){ TUI_PROP_SECURITY_INDICATOR }, ++ .len = sizeof(uint32_t) ++ }, ++ { ++ .name = "gpd.tee.tui.session.timeout", ++ .prop_type = USER_TA_PROP_TYPE_U32, ++ .data = &(const uint32_t){ TUI_SESSION_TIMEOUT }, ++ .len = sizeof(uint32_t) ++ }, ++#endif + /* + * Following properties are processed directly in libutee: + * gpd.tee.arith.maxBigIntSize +diff --git a/core/tee/tui.c b/core/tee/tui.c +new file mode 100644 +index 000000000..363bf82dc +--- /dev/null ++++ b/core/tee/tui.c +@@ -0,0 +1,177 @@ ++// SPDX-License-Identifier: BSD-2-Clause ++/* ++ * Copyright (c) 2016-2021, Linaro Limited ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++ ++static struct { ++ struct mutex mu; /* Use to lock tui session */ ++ void *s; ++ void *ctx; ++ bool busy; ++ TEE_Time timeout_time; ++} tui_sess = { .mu = MUTEX_INITIALIZER }; ++ ++TEE_Result tui_init_session(struct ts_session *s) ++{ ++ TEE_Result res; ++ TEE_Time t; ++ const TEE_Time timeout = { ++ .seconds = TUI_SESSION_TIMEOUT / TEE_TIME_MILLIS_BASE, ++ .millis = TUI_SESSION_TIMEOUT % TEE_TIME_MILLIS_BASE, ++ }; ++ ++ res = tee_time_get_sys_time(&t); ++ if (res != TEE_SUCCESS) ++ return res; ++ ++ mutex_lock(&tui_sess.mu); ++ ++ if (tui_sess.s && tui_sess.s != s && !tui_sess.busy && ++ TEE_TIME_LE(t, tui_sess.timeout_time)) { ++ /* ++ * There's a current session and it's either busy of hasn't ++ * timed out yet. ++ */ ++ res = TEE_ERROR_BUSY; ++ goto out; ++ } ++ ++ tui_sess.busy = true; ++ TEE_TIME_ADD(t, timeout, tui_sess.timeout_time); ++ tui_sess.s = s; ++ /* ++ * We may only dereference sess if our thread is ++ * using sess so save the context pointer for later ++ * use here. ++ */ ++ tui_sess.ctx = s->ctx; ++out: ++ mutex_unlock(&tui_sess.mu); ++ if (res == TEE_SUCCESS) ++ display_init(); ++ return res; ++} ++ ++static TEE_Result time_to_session_timeout_unlocked(struct ts_session *s, ++ const TEE_Time *current_time, ++ uint32_t *millis) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ TEE_Time t; ++ const TEE_Time *ct = current_time; ++ ++ if (tui_sess.s != s) ++ return TEE_ERROR_BUSY; ++ ++ if (!ct) { ++ res = tee_time_get_sys_time(&t); ++ if (res != TEE_SUCCESS) ++ return res; ++ ct = &t; ++ } ++ ++ if (TEE_TIME_LE(tui_sess.timeout_time, *ct)) ++ return TEE_ERROR_BAD_STATE; ++ ++ if (millis) { ++ TEE_TIME_SUB(*ct, tui_sess.timeout_time, t); ++ *millis = t.seconds * TEE_TIME_MILLIS_BASE + t.millis; ++ ++ /* ++ * Check if it wrapped ("can't happen"), "bad state" is the ++ * best we can return then. ++ */ ++ if ((*millis) / TEE_TIME_MILLIS_BASE < t.seconds) ++ res = TEE_ERROR_BAD_STATE; ++ } ++ ++ return res; ++} ++ ++TEE_Result tui_get_time_to_session_timeout(struct ts_session *s, ++ const TEE_Time *current_time, ++ uint32_t *millis) ++{ ++ TEE_Result res; ++ ++ mutex_lock(&tui_sess.mu); ++ res = time_to_session_timeout_unlocked(s, current_time, millis); ++ mutex_unlock(&tui_sess.mu); ++ return res; ++} ++ ++TEE_Result tui_set_session_busy(struct ts_session *s) ++{ ++ TEE_Result res; ++ TEE_Time t; ++ const TEE_Time timeout = { ++ .seconds = TUI_SESSION_TIMEOUT / TEE_TIME_MILLIS_BASE, ++ .millis = TUI_SESSION_TIMEOUT % TEE_TIME_MILLIS_BASE, ++ }; ++ ++ res = tee_time_get_sys_time(&t); ++ if (res != TEE_SUCCESS) ++ return res; ++ ++ mutex_lock(&tui_sess.mu); ++ res = time_to_session_timeout_unlocked(s, &t, NULL); ++ if (res == TEE_SUCCESS) { ++ TEE_TIME_ADD(t, timeout, tui_sess.timeout_time); ++ tui_sess.busy = true; ++ } ++ mutex_unlock(&tui_sess.mu); ++ return res; ++} ++ ++TEE_Result tui_clear_session_busy(struct ts_session *s) ++{ ++ TEE_Result res = TEE_ERROR_BAD_STATE; ++ ++ mutex_lock(&tui_sess.mu); ++ if (tui_sess.s == s) { ++ tui_sess.busy = false; ++ res = TEE_SUCCESS; ++ } ++ mutex_unlock(&tui_sess.mu); ++ return res; ++} ++ ++TEE_Result tui_close_session(struct ts_session *s) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ ++ mutex_lock(&tui_sess.mu); ++ if (s != tui_sess.s) { ++ /* ++ * TEE_ERROR_BUSY: If the TUI resources are currently in ++ * use, i.e. a TUI screen is displayed. This error code can ++ * only be returned by a TEE implementation supporting ++ * multi-threading within a TA and will occur when a thread ++ * tries to close a TUI session that is displaying a TUI ++ * screen in another thread. ++ */ ++ if (s->ctx == tui_sess.ctx) ++ res = TEE_ERROR_BUSY; ++ else ++ res = TEE_ERROR_BAD_STATE; ++ } ++ mutex_unlock(&tui_sess.mu); ++ ++ if (res != TEE_SUCCESS) ++ return res; ++ ++ display_final(); ++ tui_input_disable(); ++ mutex_lock(&tui_sess.mu); ++ tui_sess.s = NULL; ++ tui_sess.ctx = NULL; ++ mutex_unlock(&tui_sess.mu); ++ return TEE_SUCCESS; ++} +diff --git a/documentation/devicetree/bindings/arm/cpus.yaml b/documentation/devicetree/bindings/arm/cpus.yaml +new file mode 100644 +index 000000000..9a2432a88 +--- /dev/null ++++ b/documentation/devicetree/bindings/arm/cpus.yaml +@@ -0,0 +1,544 @@ ++# SPDX-License-Identifier: GPL-2.0 ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/arm/cpus.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: ARM CPUs bindings ++ ++maintainers: ++ - Lorenzo Pieralisi ++ ++description: |+ ++ The device tree allows to describe the layout of CPUs in a system through ++ the "cpus" node, which in turn contains a number of subnodes (ie "cpu") ++ defining properties for every cpu. ++ ++ Bindings for CPU nodes follow the Devicetree Specification, available from: ++ ++ https://www.devicetree.org/specifications/ ++ ++ with updates for 32-bit and 64-bit ARM systems provided in this document. ++ ++ ================================ ++ Convention used in this document ++ ================================ ++ ++ This document follows the conventions described in the Devicetree ++ Specification, with the addition: ++ ++ - square brackets define bitfields, eg reg[7:0] value of the bitfield in ++ the reg property contained in bits 7 down to 0 ++ ++ ===================================== ++ cpus and cpu node bindings definition ++ ===================================== ++ ++ The ARM architecture, in accordance with the Devicetree Specification, ++ requires the cpus and cpu nodes to be present and contain the properties ++ described below. ++ ++properties: ++ reg: ++ maxItems: 1 ++ description: | ++ Usage and definition depend on ARM architecture version and ++ configuration: ++ ++ On uniprocessor ARM architectures previous to v7 ++ this property is required and must be set to 0. ++ ++ On ARM 11 MPcore based systems this property is ++ required and matches the CPUID[11:0] register bits. ++ ++ Bits [11:0] in the reg cell must be set to ++ bits [11:0] in CPU ID register. ++ ++ All other bits in the reg cell must be set to 0. ++ ++ On 32-bit ARM v7 or later systems this property is ++ required and matches the CPU MPIDR[23:0] register ++ bits. ++ ++ Bits [23:0] in the reg cell must be set to ++ bits [23:0] in MPIDR. ++ ++ All other bits in the reg cell must be set to 0. ++ ++ On ARM v8 64-bit systems this property is required ++ and matches the MPIDR_EL1 register affinity bits. ++ ++ * If cpus node's #address-cells property is set to 2 ++ ++ The first reg cell bits [7:0] must be set to ++ bits [39:32] of MPIDR_EL1. ++ ++ The second reg cell bits [23:0] must be set to ++ bits [23:0] of MPIDR_EL1. ++ ++ * If cpus node's #address-cells property is set to 1 ++ ++ The reg cell bits [23:0] must be set to bits [23:0] ++ of MPIDR_EL1. ++ ++ All other bits in the reg cells must be set to 0. ++ ++ compatible: ++ enum: ++ - apple,icestorm ++ - apple,firestorm ++ - arm,arm710t ++ - arm,arm720t ++ - arm,arm740t ++ - arm,arm7ej-s ++ - arm,arm7tdmi ++ - arm,arm7tdmi-s ++ - arm,arm9es ++ - arm,arm9ej-s ++ - arm,arm920t ++ - arm,arm922t ++ - arm,arm925 ++ - arm,arm926e-s ++ - arm,arm926ej-s ++ - arm,arm940t ++ - arm,arm946e-s ++ - arm,arm966e-s ++ - arm,arm968e-s ++ - arm,arm9tdmi ++ - arm,arm1020e ++ - arm,arm1020t ++ - arm,arm1022e ++ - arm,arm1026ej-s ++ - arm,arm1136j-s ++ - arm,arm1136jf-s ++ - arm,arm1156t2-s ++ - arm,arm1156t2f-s ++ - arm,arm1176jzf ++ - arm,arm1176jz-s ++ - arm,arm1176jzf-s ++ - arm,arm11mpcore ++ - arm,armv8 # Only for s/w models ++ - arm,cortex-a5 ++ - arm,cortex-a7 ++ - arm,cortex-a8 ++ - arm,cortex-a9 ++ - arm,cortex-a12 ++ - arm,cortex-a15 ++ - arm,cortex-a17 ++ - arm,cortex-a32 ++ - arm,cortex-a34 ++ - arm,cortex-a35 ++ - arm,cortex-a53 ++ - arm,cortex-a55 ++ - arm,cortex-a57 ++ - arm,cortex-a65 ++ - arm,cortex-a72 ++ - arm,cortex-a73 ++ - arm,cortex-a75 ++ - arm,cortex-a76 ++ - arm,cortex-a77 ++ - arm,cortex-m0 ++ - arm,cortex-m0+ ++ - arm,cortex-m1 ++ - arm,cortex-m3 ++ - arm,cortex-m4 ++ - arm,cortex-r4 ++ - arm,cortex-r5 ++ - arm,cortex-r7 ++ - arm,neoverse-e1 ++ - arm,neoverse-n1 ++ - brcm,brahma-b15 ++ - brcm,brahma-b53 ++ - brcm,vulcan ++ - cavium,thunder ++ - cavium,thunder2 ++ - faraday,fa526 ++ - intel,sa110 ++ - intel,sa1100 ++ - marvell,feroceon ++ - marvell,mohawk ++ - marvell,pj4a ++ - marvell,pj4b ++ - marvell,sheeva-v5 ++ - marvell,sheeva-v7 ++ - nvidia,tegra132-denver ++ - nvidia,tegra186-denver ++ - nvidia,tegra194-carmel ++ - qcom,krait ++ - qcom,kryo ++ - qcom,kryo260 ++ - qcom,kryo280 ++ - qcom,kryo385 ++ - qcom,kryo468 ++ - qcom,kryo485 ++ - qcom,kryo685 ++ - qcom,scorpion ++ ++ enable-method: ++ $ref: '/schemas/types.yaml#/definitions/string' ++ oneOf: ++ # On ARM v8 64-bit this property is required ++ - enum: ++ - psci ++ - spin-table ++ # On ARM 32-bit systems this property is optional ++ - enum: ++ - actions,s500-smp ++ - allwinner,sun6i-a31 ++ - allwinner,sun8i-a23 ++ - allwinner,sun9i-a80-smp ++ - allwinner,sun8i-a83t-smp ++ - amlogic,meson8-smp ++ - amlogic,meson8b-smp ++ - arm,realview-smp ++ - aspeed,ast2600-smp ++ - brcm,bcm11351-cpu-method ++ - brcm,bcm23550 ++ - brcm,bcm2836-smp ++ - brcm,bcm63138 ++ - brcm,bcm-nsp-smp ++ - brcm,brahma-b15 ++ - marvell,armada-375-smp ++ - marvell,armada-380-smp ++ - marvell,armada-390-smp ++ - marvell,armada-xp-smp ++ - marvell,98dx3236-smp ++ - marvell,mmp3-smp ++ - mediatek,mt6589-smp ++ - mediatek,mt81xx-tz-smp ++ - qcom,gcc-msm8660 ++ - qcom,kpss-acc-v1 ++ - qcom,kpss-acc-v2 ++ - renesas,apmu ++ - renesas,r9a06g032-smp ++ - rockchip,rk3036-smp ++ - rockchip,rk3066-smp ++ - socionext,milbeaut-m10v-smp ++ - ste,dbx500-smp ++ - ti,am3352 ++ - ti,am4372 ++ ++ cpu-release-addr: ++ $ref: '/schemas/types.yaml#/definitions/uint64' ++ ++ description: ++ Required for systems that have an "enable-method" ++ property value of "spin-table". ++ On ARM v8 64-bit systems must be a two cell ++ property identifying a 64-bit zero-initialised ++ memory location. ++ ++ cpu-idle-states: ++ $ref: '/schemas/types.yaml#/definitions/phandle-array' ++ description: | ++ List of phandles to idle state nodes supported ++ by this cpu (see ./idle-states.yaml). ++ ++ capacity-dmips-mhz: ++ description: ++ u32 value representing CPU capacity (see ./cpu-capacity.txt) in ++ DMIPS/MHz, relative to highest capacity-dmips-mhz ++ in the system. ++ ++ dynamic-power-coefficient: ++ $ref: '/schemas/types.yaml#/definitions/uint32' ++ description: ++ A u32 value that represents the running time dynamic ++ power coefficient in units of uW/MHz/V^2. The ++ coefficient can either be calculated from power ++ measurements or derived by analysis. ++ ++ The dynamic power consumption of the CPU is ++ proportional to the square of the Voltage (V) and ++ the clock frequency (f). The coefficient is used to ++ calculate the dynamic power as below - ++ ++ Pdyn = dynamic-power-coefficient * V^2 * f ++ ++ where voltage is in V, frequency is in MHz. ++ ++ performance-domains: ++ maxItems: 1 ++ description: ++ List of phandles and performance domain specifiers, as defined by ++ bindings of the performance domain provider. See also ++ dvfs/performance-domain.yaml. ++ ++ power-domains: ++ description: ++ List of phandles and PM domain specifiers, as defined by bindings of the ++ PM domain provider (see also ../power_domain.txt). ++ ++ power-domain-names: ++ description: ++ A list of power domain name strings sorted in the same order as the ++ power-domains property. ++ ++ For PSCI based platforms, the name corresponding to the index of the PSCI ++ PM domain provider, must be "psci". ++ ++ qcom,saw: ++ $ref: '/schemas/types.yaml#/definitions/phandle' ++ description: | ++ Specifies the SAW* node associated with this CPU. ++ ++ Required for systems that have an "enable-method" property ++ value of "qcom,kpss-acc-v1" or "qcom,kpss-acc-v2" ++ ++ * arm/msm/qcom,saw2.txt ++ ++ qcom,acc: ++ $ref: '/schemas/types.yaml#/definitions/phandle' ++ description: | ++ Specifies the ACC* node associated with this CPU. ++ ++ Required for systems that have an "enable-method" property ++ value of "qcom,kpss-acc-v1" or "qcom,kpss-acc-v2" ++ ++ * arm/msm/qcom,kpss-acc.txt ++ ++ rockchip,pmu: ++ $ref: '/schemas/types.yaml#/definitions/phandle' ++ description: | ++ Specifies the syscon node controlling the cpu core power domains. ++ ++ Optional for systems that have an "enable-method" ++ property value of "rockchip,rk3066-smp" ++ While optional, it is the preferred way to get access to ++ the cpu-core power-domains. ++ ++ secondary-boot-reg: ++ $ref: '/schemas/types.yaml#/definitions/uint32' ++ description: | ++ Required for systems that have an "enable-method" property value of ++ "brcm,bcm11351-cpu-method", "brcm,bcm23550" or "brcm,bcm-nsp-smp". ++ ++ This includes the following SoCs: | ++ BCM11130, BCM11140, BCM11351, BCM28145, BCM28155, BCM21664, BCM23550 ++ BCM58522, BCM58525, BCM58535, BCM58622, BCM58623, BCM58625, BCM88312 ++ ++ The secondary-boot-reg property is a u32 value that specifies the ++ physical address of the register used to request the ROM holding pen ++ code release a secondary CPU. The value written to the register is ++ formed by encoding the target CPU id into the low bits of the ++ physical start address it should jump to. ++ ++if: ++ # If the enable-method property contains one of those values ++ properties: ++ enable-method: ++ contains: ++ enum: ++ - brcm,bcm11351-cpu-method ++ - brcm,bcm23550 ++ - brcm,bcm-nsp-smp ++ # and if enable-method is present ++ required: ++ - enable-method ++ ++then: ++ required: ++ - secondary-boot-reg ++ ++required: ++ - device_type ++ - reg ++ - compatible ++ ++dependencies: ++ rockchip,pmu: [enable-method] ++ ++additionalProperties: true ++ ++examples: ++ - | ++ cpus { ++ #size-cells = <0>; ++ #address-cells = <1>; ++ ++ cpu@0 { ++ device_type = "cpu"; ++ compatible = "arm,cortex-a15"; ++ reg = <0x0>; ++ }; ++ ++ cpu@1 { ++ device_type = "cpu"; ++ compatible = "arm,cortex-a15"; ++ reg = <0x1>; ++ }; ++ ++ cpu@100 { ++ device_type = "cpu"; ++ compatible = "arm,cortex-a7"; ++ reg = <0x100>; ++ }; ++ ++ cpu@101 { ++ device_type = "cpu"; ++ compatible = "arm,cortex-a7"; ++ reg = <0x101>; ++ }; ++ }; ++ ++ - | ++ // Example 2 (Cortex-A8 uniprocessor 32-bit system): ++ cpus { ++ #size-cells = <0>; ++ #address-cells = <1>; ++ ++ cpu@0 { ++ device_type = "cpu"; ++ compatible = "arm,cortex-a8"; ++ reg = <0x0>; ++ }; ++ }; ++ ++ - | ++ // Example 3 (ARM 926EJ-S uniprocessor 32-bit system): ++ cpus { ++ #size-cells = <0>; ++ #address-cells = <1>; ++ ++ cpu@0 { ++ device_type = "cpu"; ++ compatible = "arm,arm926ej-s"; ++ reg = <0x0>; ++ }; ++ }; ++ ++ - | ++ // Example 4 (ARM Cortex-A57 64-bit system): ++ cpus { ++ #size-cells = <0>; ++ #address-cells = <2>; ++ ++ cpu@0 { ++ device_type = "cpu"; ++ compatible = "arm,cortex-a57"; ++ reg = <0x0 0x0>; ++ enable-method = "spin-table"; ++ cpu-release-addr = <0 0x20000000>; ++ }; ++ ++ cpu@1 { ++ device_type = "cpu"; ++ compatible = "arm,cortex-a57"; ++ reg = <0x0 0x1>; ++ enable-method = "spin-table"; ++ cpu-release-addr = <0 0x20000000>; ++ }; ++ ++ cpu@100 { ++ device_type = "cpu"; ++ compatible = "arm,cortex-a57"; ++ reg = <0x0 0x100>; ++ enable-method = "spin-table"; ++ cpu-release-addr = <0 0x20000000>; ++ }; ++ ++ cpu@101 { ++ device_type = "cpu"; ++ compatible = "arm,cortex-a57"; ++ reg = <0x0 0x101>; ++ enable-method = "spin-table"; ++ cpu-release-addr = <0 0x20000000>; ++ }; ++ ++ cpu@10000 { ++ device_type = "cpu"; ++ compatible = "arm,cortex-a57"; ++ reg = <0x0 0x10000>; ++ enable-method = "spin-table"; ++ cpu-release-addr = <0 0x20000000>; ++ }; ++ ++ cpu@10001 { ++ device_type = "cpu"; ++ compatible = "arm,cortex-a57"; ++ reg = <0x0 0x10001>; ++ enable-method = "spin-table"; ++ cpu-release-addr = <0 0x20000000>; ++ }; ++ ++ cpu@10100 { ++ device_type = "cpu"; ++ compatible = "arm,cortex-a57"; ++ reg = <0x0 0x10100>; ++ enable-method = "spin-table"; ++ cpu-release-addr = <0 0x20000000>; ++ }; ++ ++ cpu@10101 { ++ device_type = "cpu"; ++ compatible = "arm,cortex-a57"; ++ reg = <0x0 0x10101>; ++ enable-method = "spin-table"; ++ cpu-release-addr = <0 0x20000000>; ++ }; ++ ++ cpu@100000000 { ++ device_type = "cpu"; ++ compatible = "arm,cortex-a57"; ++ reg = <0x1 0x0>; ++ enable-method = "spin-table"; ++ cpu-release-addr = <0 0x20000000>; ++ }; ++ ++ cpu@100000001 { ++ device_type = "cpu"; ++ compatible = "arm,cortex-a57"; ++ reg = <0x1 0x1>; ++ enable-method = "spin-table"; ++ cpu-release-addr = <0 0x20000000>; ++ }; ++ ++ cpu@100000100 { ++ device_type = "cpu"; ++ compatible = "arm,cortex-a57"; ++ reg = <0x1 0x100>; ++ enable-method = "spin-table"; ++ cpu-release-addr = <0 0x20000000>; ++ }; ++ ++ cpu@100000101 { ++ device_type = "cpu"; ++ compatible = "arm,cortex-a57"; ++ reg = <0x1 0x101>; ++ enable-method = "spin-table"; ++ cpu-release-addr = <0 0x20000000>; ++ }; ++ ++ cpu@100010000 { ++ device_type = "cpu"; ++ compatible = "arm,cortex-a57"; ++ reg = <0x1 0x10000>; ++ enable-method = "spin-table"; ++ cpu-release-addr = <0 0x20000000>; ++ }; ++ ++ cpu@100010001 { ++ device_type = "cpu"; ++ compatible = "arm,cortex-a57"; ++ reg = <0x1 0x10001>; ++ enable-method = "spin-table"; ++ cpu-release-addr = <0 0x20000000>; ++ }; ++ ++ cpu@100010100 { ++ device_type = "cpu"; ++ compatible = "arm,cortex-a57"; ++ reg = <0x1 0x10100>; ++ enable-method = "spin-table"; ++ cpu-release-addr = <0 0x20000000>; ++ }; ++ ++ cpu@100010101 { ++ device_type = "cpu"; ++ compatible = "arm,cortex-a57"; ++ reg = <0x1 0x10101>; ++ enable-method = "spin-table"; ++ cpu-release-addr = <0 0x20000000>; ++ }; ++ }; ++... +diff --git a/documentation/devicetree/bindings/arm/psci.yaml b/documentation/devicetree/bindings/arm/psci.yaml +new file mode 100644 +index 000000000..8b77cf83a +--- /dev/null ++++ b/documentation/devicetree/bindings/arm/psci.yaml +@@ -0,0 +1,268 @@ ++# SPDX-License-Identifier: GPL-2.0 ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/arm/psci.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: Power State Coordination Interface (PSCI) ++ ++maintainers: ++ - Lorenzo Pieralisi ++ ++description: |+ ++ Firmware implementing the PSCI functions described in ARM document number ++ ARM DEN 0022A ("Power State Coordination Interface System Software on ARM ++ processors") can be used by Linux to initiate various CPU-centric power ++ operations. ++ ++ Issue A of the specification describes functions for CPU suspend, hotplug ++ and migration of secure software. ++ ++ Functions are invoked by trapping to the privilege level of the PSCI ++ firmware (specified as part of the binding below) and passing arguments ++ in a manner similar to that specified by AAPCS: ++ ++ r0 => 32-bit Function ID / return value ++ {r1 - r3} => Parameters ++ ++ Note that the immediate field of the trapping instruction must be set ++ to #0. ++ ++ [2] Power State Coordination Interface (PSCI) specification ++ http://infocenter.arm.com/help/topic/com.arm.doc.den0022c/DEN0022C_Power_State_Coordination_Interface.pdf ++ ++properties: ++ $nodename: ++ const: psci ++ ++ compatible: ++ oneOf: ++ - description: ++ For implementations complying to PSCI versions prior to 0.2. ++ const: arm,psci ++ ++ - description: ++ For implementations complying to PSCI 0.2. ++ const: arm,psci-0.2 ++ ++ - description: ++ For implementations complying to PSCI 0.2. ++ Function IDs are not required and should be ignored by an OS with ++ PSCI 0.2 support, but are permitted to be present for compatibility ++ with existing software when "arm,psci" is later in the compatible ++ list. ++ items: ++ - const: arm,psci-0.2 ++ - const: arm,psci ++ ++ - description: ++ For implementations complying to PSCI 1.0. ++ const: arm,psci-1.0 ++ ++ - description: ++ For implementations complying to PSCI 1.0. ++ PSCI 1.0 is backward compatible with PSCI 0.2 with minor ++ specification updates, as defined in the PSCI specification[2]. ++ items: ++ - const: arm,psci-1.0 ++ - const: arm,psci-0.2 ++ ++ method: ++ description: The method of calling the PSCI firmware. ++ $ref: /schemas/types.yaml#/definitions/string-array ++ enum: ++ - smc ++ # HVC #0, with the register assignments specified in this binding. ++ - hvc ++ ++ cpu_suspend: ++ $ref: /schemas/types.yaml#/definitions/uint32 ++ description: Function ID for CPU_SUSPEND operation ++ ++ cpu_off: ++ $ref: /schemas/types.yaml#/definitions/uint32 ++ description: Function ID for CPU_OFF operation ++ ++ cpu_on: ++ $ref: /schemas/types.yaml#/definitions/uint32 ++ description: Function ID for CPU_ON operation ++ ++ migrate: ++ $ref: /schemas/types.yaml#/definitions/uint32 ++ description: Function ID for MIGRATE operation ++ ++ arm,psci-suspend-param: ++ $ref: /schemas/types.yaml#/definitions/uint32 ++ description: | ++ power_state parameter to pass to the PSCI suspend call. ++ ++ Device tree nodes that require usage of PSCI CPU_SUSPEND function (ie ++ idle state nodes with entry-method property is set to "psci", as per ++ bindings in [1]) must specify this property. ++ ++ [1] Kernel documentation - ARM idle states bindings ++ Documentation/devicetree/bindings/arm/idle-states.yaml ++ ++patternProperties: ++ "^power-domain-": ++ $ref: "../power/power-domain.yaml#" ++ ++ type: object ++ description: | ++ ARM systems can have multiple cores, sometimes in an hierarchical ++ arrangement. This often, but not always, maps directly to the processor ++ power topology of the system. Individual nodes in a topology have their ++ own specific power states and can be better represented hierarchically. ++ ++ For these cases, the definitions of the idle states for the CPUs and the ++ CPU topology, must conform to the binding in [3]. The idle states ++ themselves must conform to the binding in [4] and must specify the ++ arm,psci-suspend-param property. ++ ++ It should also be noted that, in PSCI firmware v1.0 the OS-Initiated ++ (OSI) CPU suspend mode is introduced. Using a hierarchical representation ++ helps to implement support for OSI mode and OS implementations may choose ++ to mandate it. ++ ++ [3] Documentation/devicetree/bindings/power/power-domain.yaml ++ [4] Documentation/devicetree/bindings/power/domain-idle-state.yaml ++ ++required: ++ - compatible ++ - method ++ ++allOf: ++ - if: ++ properties: ++ compatible: ++ contains: ++ const: arm,psci ++ then: ++ required: ++ - cpu_off ++ - cpu_on ++ ++additionalProperties: false ++ ++examples: ++ - |+ ++ ++ // Case 1: PSCI v0.1 only. ++ ++ psci { ++ compatible = "arm,psci"; ++ method = "smc"; ++ cpu_suspend = <0x95c10000>; ++ cpu_off = <0x95c10001>; ++ cpu_on = <0x95c10002>; ++ migrate = <0x95c10003>; ++ }; ++ ++ - |+ ++ ++ // Case 2: PSCI v0.2 only ++ ++ psci { ++ compatible = "arm,psci-0.2"; ++ method = "smc"; ++ }; ++ ++ ++ - |+ ++ ++ // Case 3: PSCI v0.2 and PSCI v0.1. ++ ++ /* ++ * A DTB may provide IDs for use by kernels without PSCI 0.2 support, ++ * enabling firmware and hypervisors to support existing and new kernels. ++ * These IDs will be ignored by kernels with PSCI 0.2 support, which will ++ * use the standard PSCI 0.2 IDs exclusively. ++ */ ++ ++ psci { ++ compatible = "arm,psci-0.2", "arm,psci"; ++ method = "hvc"; ++ ++ cpu_on = <0x95c10002>; ++ cpu_off = <0x95c10001>; ++ }; ++ ++ - |+ ++ ++ // Case 4: CPUs and CPU idle states described using the hierarchical model. ++ ++ cpus { ++ #size-cells = <0>; ++ #address-cells = <1>; ++ ++ CPU0: cpu@0 { ++ device_type = "cpu"; ++ compatible = "arm,cortex-a53"; ++ reg = <0x0>; ++ enable-method = "psci"; ++ power-domains = <&CPU_PD0>; ++ power-domain-names = "psci"; ++ }; ++ ++ CPU1: cpu@1 { ++ device_type = "cpu"; ++ compatible = "arm,cortex-a53"; ++ reg = <0x100>; ++ enable-method = "psci"; ++ power-domains = <&CPU_PD1>; ++ power-domain-names = "psci"; ++ }; ++ ++ idle-states { ++ ++ CPU_PWRDN: cpu-power-down { ++ compatible = "arm,idle-state"; ++ arm,psci-suspend-param = <0x0000001>; ++ entry-latency-us = <10>; ++ exit-latency-us = <10>; ++ min-residency-us = <100>; ++ }; ++ }; ++ ++ domain-idle-states { ++ ++ CLUSTER_RET: cluster-retention { ++ compatible = "domain-idle-state"; ++ arm,psci-suspend-param = <0x1000011>; ++ entry-latency-us = <500>; ++ exit-latency-us = <500>; ++ min-residency-us = <2000>; ++ }; ++ ++ CLUSTER_PWRDN: cluster-power-down { ++ compatible = "domain-idle-state"; ++ arm,psci-suspend-param = <0x1000031>; ++ entry-latency-us = <2000>; ++ exit-latency-us = <2000>; ++ min-residency-us = <6000>; ++ }; ++ }; ++ }; ++ ++ psci { ++ compatible = "arm,psci-1.0"; ++ method = "smc"; ++ ++ CPU_PD0: power-domain-cpu0 { ++ #power-domain-cells = <0>; ++ domain-idle-states = <&CPU_PWRDN>; ++ power-domains = <&CLUSTER_PD>; ++ }; ++ ++ CPU_PD1: power-domain-cpu1 { ++ #power-domain-cells = <0>; ++ domain-idle-states = <&CPU_PWRDN>; ++ power-domains = <&CLUSTER_PD>; ++ }; ++ ++ CLUSTER_PD: power-domain-cluster { ++ #power-domain-cells = <0>; ++ domain-idle-states = <&CLUSTER_RET>, <&CLUSTER_PWRDN>; ++ }; ++ }; ++... +diff --git a/documentation/devicetree/bindings/arm/stm32/st,stm32-syscon.yaml b/documentation/devicetree/bindings/arm/stm32/st,stm32-syscon.yaml +new file mode 100644 +index 000000000..149afb5df +--- /dev/null ++++ b/documentation/devicetree/bindings/arm/stm32/st,stm32-syscon.yaml +@@ -0,0 +1,60 @@ ++# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) ++%YAML 1.2 ++--- ++$id: "http://devicetree.org/schemas/arm/stm32/st,stm32-syscon.yaml#" ++$schema: "http://devicetree.org/meta-schemas/core.yaml#" ++ ++title: STMicroelectronics STM32 Platforms System Controller bindings ++ ++maintainers: ++ - Alexandre Torgue ++ - Christophe Roullier ++ ++properties: ++ compatible: ++ oneOf: ++ - items: ++ - enum: ++ - st,stm32mp157-syscfg ++ - st,stm32mp151-pwr-mcu ++ - st,stm32-syscfg ++ - st,stm32-power-config ++ - st,stm32-tamp ++ - const: syscon ++ - items: ++ - const: st,stm32-tamp ++ - const: syscon ++ - const: simple-mfd ++ ++ reg: ++ maxItems: 1 ++ ++ clocks: ++ maxItems: 1 ++ ++required: ++ - compatible ++ - reg ++ ++if: ++ properties: ++ compatible: ++ contains: ++ enum: ++ - st,stm32mp157-syscfg ++then: ++ required: ++ - clocks ++ ++additionalProperties: false ++ ++examples: ++ - | ++ #include ++ syscfg: syscon@50020000 { ++ compatible = "st,stm32mp157-syscfg", "syscon"; ++ reg = <0x50020000 0x400>; ++ clocks = <&rcc SYSCFG>; ++ }; ++ ++... +diff --git a/documentation/devicetree/bindings/arm/stm32/stm32.yaml b/documentation/devicetree/bindings/arm/stm32/stm32.yaml +new file mode 100644 +index 000000000..9ac7da01c +--- /dev/null ++++ b/documentation/devicetree/bindings/arm/stm32/stm32.yaml +@@ -0,0 +1,108 @@ ++# SPDX-License-Identifier: GPL-2.0 ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/arm/stm32/stm32.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: STMicroelectronics STM32 Platforms Device Tree Bindings ++ ++maintainers: ++ - Alexandre Torgue ++ ++properties: ++ $nodename: ++ const: "/" ++ compatible: ++ oneOf: ++ - description: DH STM32MP1 SoM based Boards ++ items: ++ - enum: ++ - arrow,stm32mp157a-avenger96 # Avenger96 ++ - dh,stm32mp153c-dhcom-drc02 ++ - dh,stm32mp157c-dhcom-pdk2 ++ - dh,stm32mp157c-dhcom-picoitx ++ - enum: ++ - dh,stm32mp153c-dhcom-som ++ - dh,stm32mp157a-dhcor-som ++ - dh,stm32mp157c-dhcom-som ++ - enum: ++ - st,stm32mp153 ++ - st,stm32mp157 ++ - items: ++ - enum: ++ - st,stm32f429i-disco ++ - st,stm32429i-eval ++ - const: st,stm32f429 ++ - items: ++ - enum: ++ - st,stm32f469i-disco ++ - const: st,stm32f469 ++ - items: ++ - enum: ++ - st,stm32f746-disco ++ - st,stm32746g-eval ++ - const: st,stm32f746 ++ - items: ++ - enum: ++ - st,stm32f769-disco ++ - const: st,stm32f769 ++ - items: ++ - enum: ++ - st,stm32h743i-disco ++ - st,stm32h743i-eval ++ - const: st,stm32h743 ++ - items: ++ - enum: ++ - st,stm32h750i-art-pi ++ - const: st,stm32h750 ++ - items: ++ - enum: ++ - st,stm32mp135f-dk ++ - const: st,stm32mp135 ++ - items: ++ - enum: ++ - shiratech,stm32mp157a-iot-box # IoT Box ++ - shiratech,stm32mp157a-stinger96 # Stinger96 ++ - st,stm32mp157c-ed1 ++ - st,stm32mp157a-dk1 ++ - st,stm32mp157c-dk2 ++ ++ - const: st,stm32mp157 ++ - items: ++ - const: st,stm32mp157c-ev1 ++ - const: st,stm32mp157c-ed1 ++ - const: st,stm32mp157 ++ ++ - description: Engicam i.Core STM32MP1 SoM based Boards ++ items: ++ - enum: ++ - engicam,icore-stm32mp1-ctouch2 # STM32MP1 Engicam i.Core STM32MP1 C.TOUCH 2.0 ++ - engicam,icore-stm32mp1-edimm2.2 # STM32MP1 Engicam i.Core STM32MP1 EDIMM2.2 Starter Kit ++ - const: engicam,icore-stm32mp1 # STM32MP1 Engicam i.Core STM32MP1 SoM ++ - const: st,stm32mp157 ++ ++ - description: Engicam MicroGEA STM32MP1 SoM based Boards ++ items: ++ - enum: ++ - engicam,microgea-stm32mp1-microdev2.0 ++ - engicam,microgea-stm32mp1-microdev2.0-of7 ++ - const: engicam,microgea-stm32mp1 ++ - const: st,stm32mp157 ++ ++ - description: Octavo OSD32MP15x System-in-Package based boards ++ items: ++ - enum: ++ - lxa,stm32mp157c-mc1 # Linux Automation MC-1 ++ - const: oct,stm32mp15xx-osd32 ++ - enum: ++ - st,stm32mp157 ++ - description: Odyssey STM32MP1 SoM based Boards ++ items: ++ - enum: ++ - seeed,stm32mp157c-odyssey ++ - const: seeed,stm32mp157c-odyssey-som ++ - const: st,stm32mp157 ++ ++additionalProperties: true ++ ++... +diff --git a/documentation/devicetree/bindings/clock/clock-bindings.txt b/documentation/devicetree/bindings/clock/clock-bindings.txt +new file mode 100644 +index 000000000..f2ea53832 +--- /dev/null ++++ b/documentation/devicetree/bindings/clock/clock-bindings.txt +@@ -0,0 +1,186 @@ ++This binding is a work-in-progress, and are based on some experimental ++work by benh[1]. ++ ++Sources of clock signal can be represented by any node in the device ++tree. Those nodes are designated as clock providers. Clock consumer ++nodes use a phandle and clock specifier pair to connect clock provider ++outputs to clock inputs. Similar to the gpio specifiers, a clock ++specifier is an array of zero, one or more cells identifying the clock ++output on a device. The length of a clock specifier is defined by the ++value of a #clock-cells property in the clock provider node. ++ ++[1] https://patchwork.ozlabs.org/patch/31551/ ++ ++==Clock providers== ++ ++Required properties: ++#clock-cells: Number of cells in a clock specifier; Typically 0 for nodes ++ with a single clock output and 1 for nodes with multiple ++ clock outputs. ++ ++Optional properties: ++clock-output-names: Recommended to be a list of strings of clock output signal ++ names indexed by the first cell in the clock specifier. ++ However, the meaning of clock-output-names is domain ++ specific to the clock provider, and is only provided to ++ encourage using the same meaning for the majority of clock ++ providers. This format may not work for clock providers ++ using a complex clock specifier format. In those cases it ++ is recommended to omit this property and create a binding ++ specific names property. ++ ++ Clock consumer nodes must never directly reference ++ the provider's clock-output-names property. ++ ++For example: ++ ++ oscillator { ++ #clock-cells = <1>; ++ clock-output-names = "ckil", "ckih"; ++ }; ++ ++- this node defines a device with two clock outputs, the first named ++ "ckil" and the second named "ckih". Consumer nodes always reference ++ clocks by index. The names should reflect the clock output signal ++ names for the device. ++ ++clock-indices: If the identifying number for the clocks in the node ++ is not linear from zero, then this allows the mapping of ++ identifiers into the clock-output-names array. ++ ++For example, if we have two clocks <&oscillator 1> and <&oscillator 3>: ++ ++ oscillator { ++ compatible = "myclocktype"; ++ #clock-cells = <1>; ++ clock-indices = <1>, <3>; ++ clock-output-names = "clka", "clkb"; ++ } ++ ++ This ensures we do not have any empty strings in clock-output-names ++ ++ ++==Clock consumers== ++ ++Required properties: ++clocks: List of phandle and clock specifier pairs, one pair ++ for each clock input to the device. Note: if the ++ clock provider specifies '0' for #clock-cells, then ++ only the phandle portion of the pair will appear. ++ ++Optional properties: ++clock-names: List of clock input name strings sorted in the same ++ order as the clocks property. Consumers drivers ++ will use clock-names to match clock input names ++ with clocks specifiers. ++clock-ranges: Empty property indicating that child nodes can inherit named ++ clocks from this node. Useful for bus nodes to provide a ++ clock to their children. ++ ++For example: ++ ++ device { ++ clocks = <&osc 1>, <&ref 0>; ++ clock-names = "baud", "register"; ++ }; ++ ++ ++This represents a device with two clock inputs, named "baud" and "register". ++The baud clock is connected to output 1 of the &osc device, and the register ++clock is connected to output 0 of the &ref. ++ ++==Example== ++ ++ /* external oscillator */ ++ osc: oscillator { ++ compatible = "fixed-clock"; ++ #clock-cells = <0>; ++ clock-frequency = <32678>; ++ clock-output-names = "osc"; ++ }; ++ ++ /* phase-locked-loop device, generates a higher frequency clock ++ * from the external oscillator reference */ ++ pll: pll@4c000 { ++ compatible = "vendor,some-pll-interface" ++ #clock-cells = <1>; ++ clocks = <&osc 0>; ++ clock-names = "ref"; ++ reg = <0x4c000 0x1000>; ++ clock-output-names = "pll", "pll-switched"; ++ }; ++ ++ /* UART, using the low frequency oscillator for the baud clock, ++ * and the high frequency switched PLL output for register ++ * clocking */ ++ uart@a000 { ++ compatible = "fsl,imx-uart"; ++ reg = <0xa000 0x1000>; ++ interrupts = <33>; ++ clocks = <&osc 0>, <&pll 1>; ++ clock-names = "baud", "register"; ++ }; ++ ++This DT fragment defines three devices: an external oscillator to provide a ++low-frequency reference clock, a PLL device to generate a higher frequency ++clock signal, and a UART. ++ ++* The oscillator is fixed-frequency, and provides one clock output, named "osc". ++* The PLL is both a clock provider and a clock consumer. It uses the clock ++ signal generated by the external oscillator, and provides two output signals ++ ("pll" and "pll-switched"). ++* The UART has its baud clock connected the external oscillator and its ++ register clock connected to the PLL clock (the "pll-switched" signal) ++ ++==Assigned clock parents and rates== ++ ++Some platforms may require initial configuration of default parent clocks ++and clock frequencies. Such a configuration can be specified in a device tree ++node through assigned-clocks, assigned-clock-parents and assigned-clock-rates ++properties. The assigned-clock-parents property should contain a list of parent ++clocks in the form of a phandle and clock specifier pair and the ++assigned-clock-rates property should contain a list of frequencies in Hz. Both ++these properties should correspond to the clocks listed in the assigned-clocks ++property. ++ ++To skip setting parent or rate of a clock its corresponding entry should be ++set to 0, or can be omitted if it is not followed by any non-zero entry. ++ ++ uart@a000 { ++ compatible = "fsl,imx-uart"; ++ reg = <0xa000 0x1000>; ++ ... ++ clocks = <&osc 0>, <&pll 1>; ++ clock-names = "baud", "register"; ++ ++ assigned-clocks = <&clkcon 0>, <&pll 2>; ++ assigned-clock-parents = <&pll 2>; ++ assigned-clock-rates = <0>, <460800>; ++ }; ++ ++In this example the <&pll 2> clock is set as parent of clock <&clkcon 0> and ++the <&pll 2> clock is assigned a frequency value of 460800 Hz. ++ ++Configuring a clock's parent and rate through the device node that consumes ++the clock can be done only for clocks that have a single user. Specifying ++conflicting parent or rate configuration in multiple consumer nodes for ++a shared clock is forbidden. ++ ++Configuration of common clocks, which affect multiple consumer devices can ++be similarly specified in the clock provider node. ++ ++==Protected clocks== ++ ++Some platforms or firmwares may not fully expose all the clocks to the OS, such ++as in situations where those clks are used by drivers running in ARM secure ++execution levels. Such a configuration can be specified in device tree with the ++protected-clocks property in the form of a clock specifier list. This property should ++only be specified in the node that is providing the clocks being protected: ++ ++ clock-controller@a000f000 { ++ compatible = "vendor,clk95; ++ reg = <0xa000f000 0x1000> ++ #clocks-cells = <1>; ++ ... ++ protected-clocks = , ; ++ }; +diff --git a/documentation/devicetree/bindings/clock/fixed-clock.yaml b/documentation/devicetree/bindings/clock/fixed-clock.yaml +new file mode 100644 +index 000000000..412f46eca +--- /dev/null ++++ b/documentation/devicetree/bindings/clock/fixed-clock.yaml +@@ -0,0 +1,87 @@ ++# SPDX-License-Identifier: GPL-2.0 ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/clock/fixed-clock.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: Binding for simple fixed-rate clock sources ++ ++maintainers: ++ - Michael Turquette ++ - Stephen Boyd ++ ++properties: ++ compatible: ++ const: fixed-clock ++ ++ "#clock-cells": ++ const: 0 ++ ++ clock-frequency: true ++ ++ clock-accuracy: ++ description: accuracy of clock in ppb (parts per billion). ++ $ref: /schemas/types.yaml#/definitions/uint32 ++ ++ clock-output-names: ++ maxItems: 1 ++ ++ st,bypass: ++ description: configures the oscillator bypass mode (HSEBYP, LSEBYP) ++ type: boolean ++ st,digbypass: ++ description: configures the bypass mode as full-swing digital signal (DIGBYP) ++ type: boolean ++ st,css: ++ description: activates the clock security system (HSECSSON, LSECSSON) ++ type: boolean ++ st,drive: ++ description: | ++ only for LSE, contains the value of the drive for the oscillator ++ (see LSEDRV_ defined in the file dt-bindings/clock/stm32mp13-clksrc.h) ++ $ref: "/schemas/types.yaml#/definitions/uint32" ++ ++required: ++ - compatible ++ - "#clock-cells" ++ - clock-frequency ++ ++additionalProperties: false ++ ++examples: ++ - | ++ #include ++ clock { ++ compatible = "fixed-clock"; ++ #clock-cells = <0>; ++ clock-frequency = <1000000000>; ++ clock-accuracy = <100>; ++ }; ++ ++ // STM32MP13 examples ++ clk-hse { ++ #clock-cells = <0>; ++ compatible = "fixed-clock"; ++ clock-frequency = <24000000>; ++ st,bypass; ++ }; ++ ++ clk-lse { ++ #clock-cells = <0>; ++ compatible = "fixed-clock"; ++ clock-frequency = <32768>; ++ st,css; ++ st,drive = ; ++ }; ++ ++ //Example with HSIDIV = /1 ++ //clk-hsi is the clock after HSIDIV (clk_hsi in RCC doc). ++ //So this clock frequency is used to compute the expected HSI_DIV ++ //for the clock tree initialization. ++ ++ clk-hsi { ++ #clock-cells = <0>; ++ compatible = "fixed-clock"; ++ clock-frequency = <64000000>; ++ }; ++... +diff --git a/documentation/devicetree/bindings/clock/st,stm32-calibration.yaml b/documentation/devicetree/bindings/clock/st,stm32-calibration.yaml +new file mode 100644 +index 000000000..89cb5664a +--- /dev/null ++++ b/documentation/devicetree/bindings/clock/st,stm32-calibration.yaml +@@ -0,0 +1,61 @@ ++# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/clock/st,stm32-calibration.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: STMicroelectronics STM32 clock calibration bindings ++ ++maintainers: ++ - Gatien Chevallier ++ ++description: | ++ STMicroelectronics's STM32 MPUs embed a clock calibration system for HSI ++ and CSI clocks. The calibration relies on a counter. It is a digital ++ calibration circuit with 0.95 ppm resolution, used to compensate quartz ++ crystal inaccuracy. ++ ++properties: ++ compatible: ++ const: "st,osc-calibration" ++ ++patternProperties: ++ "^[a-z]{3}-calibration": ++ type: object ++ properties: ++ compatible: ++ enum: ++ - "st,csi-cal" ++ - "st,hsi-cal" ++ counter: ++ description: counter used to calibrate ++ Refer to counter.yaml for more details. ++ maxItems: 1 ++ ++ required: ++ - compatible ++ - counter ++ ++required: ++ - compatible ++ ++additionalProperties: false ++ ++examples: ++- | ++ osc_calibration: osc-calibration { ++ compatible = "st,osc-calibration"; ++ ++ csi_calibration: csi-calibration { ++ compatible = "st,csi-cal"; ++ counter = <&timers15 0 8>; ++ status = "disabled"; ++ }; ++ ++ hsi_calibration: hsi-calibration { ++ compatible = "st,hsi-cal"; ++ counter = <&timers15 0 7>; ++ status = "disabled"; ++ }; ++ }; ++... +diff --git a/documentation/devicetree/bindings/clock/st,stm32mp13-rcc.yaml b/documentation/devicetree/bindings/clock/st,stm32mp13-rcc.yaml +new file mode 100644 +index 000000000..f6225d1db +--- /dev/null ++++ b/documentation/devicetree/bindings/clock/st,stm32mp13-rcc.yaml +@@ -0,0 +1,487 @@ ++# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/clock/st,stm32mp13-rcc.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: Reset Clock Controller Binding ++ ++maintainers: ++ - Gabriel Fernandez ++ ++description: | ++ The RCC IP is both a reset and a clock controller. ++ RCC makes also power management (resume/supend and wakeup interrupt). ++ Please also refer to reset.txt for common reset controller binding usage. ++ ++ This binding uses common clock bindings ++ Documentation/devicetree/bindings/clock/clock-bindings.txt ++ ++ Specifying clocks ++ ================= ++ ++ All available clocks are defined as preprocessor macros in ++ dt-bindings/clock/stm32mp13-clks.h and can be used in device ++ tree sources. ++ ++ Specifying softreset control of devices ++ ======================================= ++ ++ Device nodes should specify the reset channel required in their "resets" ++ property, containing a phandle to the reset device node and an index specifying ++ which channel to use. ++ The index is the bit number within the RCC registers bank, starting from RCC ++ base address. ++ It is calculated as: index = register_offset / 4 * 32 + bit_offset. ++ Where bit_offset is the bit offset within the register. ++ ++ For example on STM32MP13, for I2C2 reset: ++ i2c2 = APB1_RSTSETR_offset / 4 * 32 + I2C2_bit_offset ++ = 0x6A0 / 4 * 32 + 22 = 13590 ++ ++ The list of valid indices for STM32MP13 is available in: ++ include/dt-bindings/reset/stm32mp13-resets.h ++ ++properties: ++ compatible: ++ oneOf: ++ - items: ++ - const: st,stm32mp13-rcc ++ - const: st,stm32mp13-rcc-mco ++ - const: syscon ++ - items: ++ - const: st,stm32mp13-rcc ++ - const: syscon ++ ++ "#address-cells": ++ const: 1 ++ ++ "#size-cells": ++ const: 0 ++ ++ "#clock-cells": ++ const: 1 ++ ++ "#reset-cells": ++ const: 1 ++ ++ reg: ++ maxItems: 1 ++ ++ interrupts: ++ description: contains the general interrupt line (from CSTOP). ++ maxItems: 1 ++ ++ st,clkdiv: ++ description: | ++ - The property is a list of dividers defined by macros DIV(DIV_, ++ value) as defined by header file dt-bindings/clock/stm32mp13-clksrc.h. ++ - For the STM32MP13 family there are 12 dividers values expected: ++ MPU AXI MLAHB APB1 APB2 APB3 APB4 APB5 APB6 RTC MCO1 MCO2 ++ - Each divider value uses the DIV coding defined in RCC associated register ++ RCC_xxxDIVR. In most cases, it is: ++ 0x0: not divided ++ 0x1: division by 2 ++ 0x2: division by 4 ++ 0x3: division by 8 ++ ... ++ - For RTC, MCO1, MCO2 and DIV_MPU the coding is different so refer to the ++ ref man. ++ - Property can be used to configure the clock main dividers value: ++ st,clkdiv = < ++ DIV(DIV_MPU, 1) ++ DIV(DIV_AXI, 0) ++ DIV(DIV_MLAHB, 0) ++ DIV(DIV_APB1, 1) ++ DIV(DIV_APB2, 1) ++ DIV(DIV_APB3, 1) ++ DIV(DIV_APB4, 1) ++ DIV(DIV_APB5, 2) ++ DIV(DIV_APB6, 1) ++ DIV(DIV_RTC, 23) ++ DIV(DIV_MCO1, 0) ++ DIV(DIV_MCO2, 0) ++ >; ++ $ref: "/schemas/types.yaml#/definitions/uint32-array" ++ ++ st,clksrc: ++ description: | ++ - used to configure the peripherals kernel clock selection. ++ The property is a list of peripheral kernel clock source identifiers defined ++ by macros CLK__ as defined by header file ++ dt-bindings/clock/stm32mp13-clksrc.h. ++ - st,clksrc may not list all the kernel clocks and has no ordering requirements. ++ - Property can be used to configure the clock distribution tree: ++ st,clksrc = < ++ CLK_MPU_PLL1P ++ CLK_AXI_PLL2P ++ CLK_MLAHBS_PLL3 ++ CLK_RTC_LSE ++ CLK_MCO1_DISABLED ++ CLK_MCO2_DISABLED ++ CLK_ETH1_PLL4P ++ CLK_ETH2_PLL4P ++ CLK_SDMMC1_PLL4P ++ CLK_SDMMC2_PLL4P ++ CLK_STGEN_HSE ++ CLK_USBPHY_HSE ++ CLK_I2C4_HSI ++ >; ++ $ref: "/schemas/types.yaml#/definitions/uint32-array" ++ ++ pinctrl-0: ++ description: | ++ Describes the default pin control for MCO usage. ++ Contains the pinmux and the bias config. ++ minItems: 1 ++ ++patternProperties: ++ '^st,pll@[0-9]*$': ++ description: ++ Each PLL children node for PLL1 to PLL4 (see ref manual for details) ++ are listed with associated reg 0 to 3. ++ PLL2, PLL3 or PLL4 are off when their associated nodes are absent or ++ deactivated. ++ type: object ++ properties: ++ compatible: ++ const: st,stm32mp1-pll ++ reg: ++ description: index of the pll instance. ++ minItems: 1 ++ st,pll: ++ description: ++ Phandle of the default pll configuration. ++ A pll could have several configuration (5 max) and should be described in a subnode ++ just below. ++ $ref: "/schemas/types.yaml#/definitions/phandle-array" ++ patternProperties: ++ '^pll[0-9]_cfg[0-9]*$': ++ type: object ++ properties: ++ st,pll_vco: ++ description: ++ Phandle of pll vco configuration. ++ See description of 'st,pll_vco' node. ++ $ref: "/schemas/types.yaml#/definitions/phandle-array" ++ minItems: 1 ++ ++ st,pll_div_pqr: ++ $ref: "/schemas/types.yaml#/definitions/uint32-array" ++ description: | ++ The parameters for PLL configuration in the following order: ++ Output. ++ DIVx values are defined as in RCC spec: ++ 0x0: bypass (division by 1) ++ 0x1: division by 2 ++ 0x2: division by 3 ++ 0x3: division by 4 ++ ... ++ required: ++ - st,pll_vco ++ additionalProperties: false ++ required: ++ - compatible ++ additionalProperties: false ++ ++ '^st,pll_vco': ++ description: | ++ This node will contain all vco configuration of all PLLs in subnodes. ++ We will found theses parameters in each subnodes nammed as following, ++ type: object ++ patternProperties: ++ '^pll[0-9]-vco-': ++ type: object ++ properties: ++ src: ++ description: ++ Clock source configuration values are defined by macros CLK__ ++ from dt-bindings/clock/stm32mp13-clksrc.h. ++ minItems: 1 ++ divmn: ++ description: ++ The parameters for PLL divider (DIVM) and multiplication factor (DIVN) ++ configuration. ++ minItems: 1 ++ frac: ++ description: ++ Fractional part of the multiplication factor ++ (optional, PLL is in integer mode when absent). ++ minItems: 1 ++ csg: ++ description: | ++ Clock Spreading Generator (optional) with parameters in the ++ following order: MOD_PER INC_STEP SSCG_MODE. ++ MOD_PER: Modulation Period Adjustment ++ INC_STEP: Modulation Depth Adjustment ++ SSCG_MODE: Spread spectrum clock generator mode, with associated ++ defines from stm32mp13-clksrc.h: ++ - SSCG_MODE_CENTER_SPREAD = 0 ++ - SSCG_MODE_DOWN_SPREAD = 1 ++ minItems: 1 ++ additionalProperties: false ++ additionalProperties: false ++ ++ '^st,clk_opp': ++ description: | ++ This node will contain all operating point configurations. The OPP ++ configuration will be applied at the run-time and will override the clock tree ++ configuration. ++ Each node will contain a list of operating point configurations (cfg_1, cfg_2...) ++ type: object ++ patternProperties: ++ '^st,ck_[a-z]*$': ++ type: object ++ description: | ++ three kind of operating points: st,ck_mpu, st,ck_axi, st,ck_mlahbs. ++ patternProperties: ++ 'cfg_[0-9]*$': ++ type: object ++ description: list of operating point configuration. ++ properties: ++ hz: ++ description: Operating point frequency in hertz ++ minItems: 1 ++ st,clksrc: ++ description: The clock source (same syntax of 'st,clksrc' property below) ++ $ref: "/schemas/types.yaml#/definitions/uint32-array" ++ minItems: 1 ++ st,clkdiv: ++ description: The clock source (same syntax of 'st,clkdiv' property below) ++ $ref: "/schemas/types.yaml#/definitions/uint32-array" ++ minItems: 1 ++ st,pll: ++ description: Phandle of the pll configuration (See description of 'pllx_cfgx' node) ++ $ref: "/schemas/types.yaml#/definitions/phandle-array" ++ minItems: 1 ++ required: ++ - hz ++ - st,clksrc ++ additionalProperties: false ++ additionalProperties: false ++ ++required: ++ - "#address-cells" ++ - "#size-cells" ++ - "#clock-cells" ++ - "#reset-cells" ++ - compatible ++ - reg ++ - interrupts ++ ++if: ++ properties: ++ compatible: ++ contains: ++ enum: ++ # Micro-controller clock output (MCO) pins may be available ++ - st,stm32mp13-rcc-mco ++then: ++ required: ++ # They generate clock signals, therefore need a pinctrl ++ - pinctrl-0 ++ ++additionalProperties: false ++ ++examples: ++ - | ++ #include ++ #include ++ #include ++ //Example 1 ++ rcc@50000000 { ++ compatible = "st,stm32mp13-rcc", "syscon"; ++ reg = <0x50000000 0x1000>; ++ #address-cells = <1>; ++ #size-cells = <0>; ++ #clock-cells = <1>; ++ #reset-cells = <1>; ++ interrupts = ; ++ st,clksrc = < ++ CLK_MPU_PLL1P ++ CLK_AXI_PLL2P ++ CLK_MLAHBS_PLL3 ++ CLK_RTC_LSE ++ CLK_MCO1_DISABLED ++ CLK_MCO2_DISABLED ++ CLK_CKPER_HSE ++ CLK_ETH1_PLL4P ++ CLK_ETH2_PLL4P ++ CLK_SDMMC1_PLL4P ++ CLK_SDMMC2_PLL4P ++ CLK_STGEN_HSE ++ CLK_USBPHY_HSE ++ CLK_I2C4_HSI ++ CLK_USBO_USBPHY ++ CLK_ADC2_CKPER ++ CLK_I2C12_HSI ++ CLK_UART2_HSI ++ CLK_UART4_HSI ++ CLK_SAES_AXI ++ CLK_DCMIPP_PLL2Q ++ CLK_LPTIM3_PCLK3 ++ >; ++ ++ st,clkdiv = < ++ DIV(DIV_MPU, 1) ++ DIV(DIV_AXI, 0) ++ DIV(DIV_MLAHB, 0) ++ DIV(DIV_APB1, 1) ++ DIV(DIV_APB2, 1) ++ DIV(DIV_APB3, 1) ++ DIV(DIV_APB4, 1) ++ DIV(DIV_APB5, 2) ++ DIV(DIV_APB6, 1) ++ DIV(DIV_RTC, 0) ++ DIV(DIV_MCO1, 0) ++ DIV(DIV_MCO2, 0) ++ >; ++ ++ st,pll_vco { ++ pll1_vco_1800Mhz: pll1-vco-1800mhz { ++ src = < CLK_PLL12_HSE >; ++ divmn = < 1 74 >; ++ }; ++ ++ pll1_vco_1300Mhz: pll1-vco-1300Mhz { ++ src = < CLK_PLL12_HSE >; ++ divmn = < 2 80 >; ++ frac = < 0x800 >; ++ }; ++ ++ pll2_vco_1066Mhz: pll2-vco-1066Mhz { ++ src = < CLK_PLL12_HSE >; ++ divmn = < 2 65 >; ++ frac = < 0x1400 >; ++ }; ++ ++ pll3_vco_417_8Mhz: pll3-vco-417_8Mhz { ++ src = < CLK_PLL3_HSE >; ++ divmn = < 1 33 >; ++ frac = < 0x1a04 >; ++ }; ++ ++ pll4_vco_600Mhz: pll4-vco-600Mhz { ++ src = < CLK_PLL4_HSE >; ++ divmn = < 1 49 >; ++ }; ++ }; ++ ++ /* VCO = 1300.0 MHz => P = 650 (CPU) */ ++ pll1:st,pll@0 { ++ compatible = "st,stm32mp1-pll"; ++ reg = <0>; ++ st,pll = < &pll1_cfg1 >; ++ ++ pll1_cfg1: pll1_cfg1 { ++ st,pll_vco = < &pll1_vco_1300Mhz >; ++ st,pll_div_pqr = < 0 1 1 >; ++ }; ++ ++ pll1_cfg2: pll1_cfg2 { ++ st,pll_vco = < &pll1_vco_1800Mhz >; ++ st,pll_div_pqr = < 0 1 1 >; ++ }; ++ }; ++ ++ /* VCO = 1066.0 MHz => P = 266 (AXI), Q = 266, R = 533 (DDR) */ ++ pll2:st,pll@1 { ++ compatible = "st,stm32mp1-pll"; ++ reg = <1>; ++ st,pll = < &pll2_cfg1 >; ++ ++ pll2_cfg1: pll2_cfg1 { ++ st,pll_vco = < &pll2_vco_1066Mhz >; ++ st,pll_div_pqr = < 1 0 1 >; ++ }; ++ }; ++ ++ /* VCO = 417.8 MHz => P = 209, Q = 24, R = 11 */ ++ pll3:st,pll@2 { ++ compatible = "st,stm32mp1-pll"; ++ reg = <2>; ++ st,pll = < &pll3_cfg1 >; ++ ++ pll3_cfg1: pll3_cfg1 { ++ st,pll_vco = < &pll3_vco_417_8Mhz >; ++ st,pll_div_pqr = < 1 16 1 >; ++ }; ++ }; ++ ++ /* VCO = 600.0 MHz => P = 50, Q = 10, R = 100 */ ++ pll4:st,pll@3 { ++ compatible = "st,stm32mp1-pll"; ++ reg = <3>; ++ st,pll = < &pll4_cfg1 >; ++ ++ pll4_cfg1: pll4_cfg1 { ++ st,pll_vco = < &pll4_vco_600Mhz >; ++ st,pll_div_pqr = < 11 59 5 >; ++ }; ++ }; ++ ++ st,clk_opp { ++ st,ck_mpu { ++ cfg_1 { ++ hz = < 900000000 >; ++ st,clksrc = < CLK_MPU_PLL1P >; ++ st,pll = < &pll1_cfg1 >; ++ }; ++ ++ cfg_2 { ++ hz = < 650000000 >; ++ st,clksrc = < CLK_MPU_PLL1P >; ++ st,pll = < &pll1_cfg2 >; ++ }; ++ ++ cfg_3 { ++ hz = < 450000000 >; ++ st,clksrc = < CLK_MPU_PLL1P_DIV >; ++ st,clkdiv = < DIV(DIV_MPU, 1) >; ++ st,pll = < &pll1_cfg1 >; ++ }; ++ ++ cfg_4 { ++ hz = <64000000>; ++ st,clksrc = < CLK_MPU_HSI >; ++ }; ++ ++ cfg_5 { ++ hz = <24000000>; ++ st,clksrc = < CLK_MPU_HSE >; ++ }; ++ }; ++ ++ st,ck_axi { ++ cfg_1 { ++ hz = <266500000>; ++ st,clksrc = < CLK_AXI_PLL2P >; ++ st,clkdiv = < DIV(DIV_AXI, 0) >; ++ st,pll = < &pll2_cfg1 >; ++ }; ++ ++ cfg_2 { ++ hz = <64000000>; ++ st,clksrc = < CLK_AXI_HSI>; ++ st,clkdiv = < DIV(DIV_AXI, 0) >; ++ }; ++ }; ++ ++ st,ck_mlahbs { ++ cfg_1 { ++ hz = <208877930>; ++ st,clksrc = < CLK_MLAHBS_PLL3 >; ++ st,clkdiv = < DIV(DIV_MLAHB, 0) >; ++ st,pll = < &pll3_cfg1 >; ++ }; ++ ++ cfg_2 { ++ hz = <12000000>; ++ st,clksrc = < CLK_MLAHBS_HSE>; ++ st,clkdiv = < DIV(DIV_MLAHB, 1) >; ++ }; ++ }; ++ }; ++ }; ++ ++... +diff --git a/documentation/devicetree/bindings/counter/counter.yaml b/documentation/devicetree/bindings/counter/counter.yaml +new file mode 100644 +index 000000000..c0e3c2eb9 +--- /dev/null ++++ b/documentation/devicetree/bindings/counter/counter.yaml +@@ -0,0 +1,36 @@ ++# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/counter/counter.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: Generic counter bindings ++ ++maintainers: ++ - Lionel Debieve ++ ++description: | ++ Generic counter bindings to provide a way for a driver using ++ the counter framework to provide trigger and channel information ++ to the driver ++ ++select: false ++ ++properties: ++ counter: ++ description: phandle to the associated counter registered and the ++ channel configuration to be used. ++ items: ++ - description: Phandle to the counter ++ $ref: /schemas/types.yaml#/definitions/phandle ++ - description: External trigger polarity ++ - description: External trigger mux entry ++ - description: External output polarity ++ - description: External input mux entry ++ ++required: ++ - counter ++ ++additionalProperties: false ++ ++... +diff --git a/documentation/devicetree/bindings/crypto/st,stm32-cryp.yaml b/documentation/devicetree/bindings/crypto/st,stm32-cryp.yaml +new file mode 100644 +index 000000000..c43c1dfe7 +--- /dev/null ++++ b/documentation/devicetree/bindings/crypto/st,stm32-cryp.yaml +@@ -0,0 +1,53 @@ ++# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/crypto/st,stm32-cryp.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: STMicroelectronics STM32 CRYP bindings ++ ++maintainers: ++ - Lionel Debieve ++ - Nicolas Toromanoff ++ ++properties: ++ compatible: ++ enum: ++ - st,stm32f756-cryp ++ - st,stm32mp1-cryp ++ ++ reg: ++ maxItems: 1 ++ ++ clocks: ++ maxItems: 1 ++ ++ interrupts: ++ maxItems: 1 ++ ++ resets: ++ maxItems: 1 ++ ++required: ++ - compatible ++ - reg ++ - clocks ++ - interrupts ++ - resets ++ ++additionalProperties: false ++ ++examples: ++ - | ++ #include ++ #include ++ #include ++ cryp@54001000 { ++ compatible = "st,stm32mp1-cryp"; ++ reg = <0x54001000 0x400>; ++ interrupts = ; ++ clocks = <&rcc CRYP1>; ++ resets = <&rcc CRYP1_R>; ++ }; ++ ++... +diff --git a/documentation/devicetree/bindings/crypto/st,stm32-hash.yaml b/documentation/devicetree/bindings/crypto/st,stm32-hash.yaml +new file mode 100644 +index 000000000..9129c6833 +--- /dev/null ++++ b/documentation/devicetree/bindings/crypto/st,stm32-hash.yaml +@@ -0,0 +1,80 @@ ++# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/crypto/st,stm32-hash.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: STMicroelectronics STM32 HASH bindings ++ ++maintainers: ++ - Lionel Debieve ++ - Nicolas Toromanoff ++ ++properties: ++ compatible: ++ enum: ++ - st,stm32f456-hash ++ - st,stm32f756-hash ++ - st,stm32mp13-hash ++ ++ reg: ++ maxItems: 1 ++ ++ clocks: ++ maxItems: 1 ++ ++ interrupts: ++ maxItems: 1 ++ ++ resets: ++ maxItems: 1 ++ ++ dmas: ++ maxItems: 1 ++ ++ dma-names: ++ items: ++ - const: in ++ ++ dma-maxburst: ++ description: Set number of maximum dma burst supported ++ $ref: /schemas/types.yaml#/definitions/uint32 ++ minimum: 0 ++ maximum: 2 ++ default: 0 ++ ++required: ++ - compatible ++ - reg ++ - clocks ++ - interrupts ++ - resets ++ ++additionalProperties: false ++ ++examples: ++ - | ++ #include ++ #include ++ #include ++ hash@54002000 { ++ compatible = "st,stm32f756-hash"; ++ reg = <0x54002000 0x400>; ++ interrupts = ; ++ clocks = <&rcc HASH1>; ++ resets = <&rcc HASH1_R>; ++ dmas = <&mdma1 31 0x10 0x1000A02 0x0 0x0>; ++ dma-names = "in"; ++ dma-maxburst = <2>; ++ }; ++ ++ //MP13 example ++ hash: hash@54003000 { ++ compatible = "st,stm32mp13-hash"; ++ reg = <0x54003000 0x400>; ++ clocks = <&rcc HASH1>; ++ resets = <&rcc HASH1_R>; ++ status = "disabled"; ++ }; ++ ++... +diff --git a/documentation/devicetree/bindings/crypto/st,stm32-pka.yaml b/documentation/devicetree/bindings/crypto/st,stm32-pka.yaml +new file mode 100644 +index 000000000..e59a0c9e4 +--- /dev/null ++++ b/documentation/devicetree/bindings/crypto/st,stm32-pka.yaml +@@ -0,0 +1,47 @@ ++# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/crypto/st,stm32-pka.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: STMicroelectronics STM32 Public Key Accelerator bindings ++ ++maintainers: ++ - Nicolas Toromanoff ++ ++properties: ++ compatible: ++ enum: ++ - st,stm32mp13-pka64 ++ ++ reg: ++ maxItems: 1 ++ ++ clocks: ++ maxItems: 1 ++ ++ interrupts: ++ maxItems: 1 ++ ++ resets: ++ maxItems: 1 ++ ++required: ++ - compatible ++ - reg ++ - clocks ++ - resets ++ ++additionalProperties: false ++ ++examples: ++ - | ++ #include ++ #include ++ pka: pka@54006000 { ++ compatible = "st,stm32mp13-pka64"; ++ reg = <0x54006000 0x2000>; ++ clocks = <&rcc PKA>; ++ resets = <&rcc PKA_R>; ++ }; ++... +diff --git a/documentation/devicetree/bindings/crypto/st,stm32-saes.yaml b/documentation/devicetree/bindings/crypto/st,stm32-saes.yaml +new file mode 100644 +index 000000000..83ece911a +--- /dev/null ++++ b/documentation/devicetree/bindings/crypto/st,stm32-saes.yaml +@@ -0,0 +1,47 @@ ++# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/crypto/st,stm32-saes.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: STMicroelectronics STM32 Secure AES bindings ++ ++maintainers: ++ - Nicolas Toromanoff ++ ++properties: ++ compatible: ++ enum: ++ - st,stm32mp13-saes ++ ++ reg: ++ maxItems: 1 ++ ++ clocks: ++ maxItems: 1 ++ ++ interrupts: ++ maxItems: 1 ++ ++ resets: ++ maxItems: 1 ++ ++required: ++ - compatible ++ - reg ++ - clocks ++ - resets ++ ++additionalProperties: false ++ ++examples: ++ - | ++ #include ++ #include ++ saes: saes@54005000 { ++ compatible = "st,stm32mp13-saes"; ++ reg = <0x54005000 0x400>; ++ clocks = <&rcc SAES_K>; ++ resets = <&rcc SAES_R>; ++ }; ++... +diff --git a/documentation/devicetree/bindings/display/st,stm32-ltdc.yaml b/documentation/devicetree/bindings/display/st,stm32-ltdc.yaml +new file mode 100644 +index 000000000..4ae3d7549 +--- /dev/null ++++ b/documentation/devicetree/bindings/display/st,stm32-ltdc.yaml +@@ -0,0 +1,78 @@ ++# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/display/st,stm32-ltdc.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: STMicroelectronics STM32 lcd-tft display controller ++ ++maintainers: ++ - Philippe Cornu ++ - Yannick Fertre ++ ++properties: ++ compatible: ++ const: st,stm32-ltdc ++ ++ reg: ++ maxItems: 1 ++ ++ interrupts: ++ items: ++ - description: events interrupt line. ++ - description: errors interrupt line. ++ minItems: 1 ++ ++ clocks: ++ maxItems: 1 ++ ++ clock-names: ++ items: ++ - const: lcd ++ ++ resets: ++ maxItems: 1 ++ ++ port: ++ $ref: /schemas/graph.yaml#/properties/port ++ description: | ++ Video port for DPI RGB output. ++ ltdc has one video port with up to 2 endpoints: ++ - for external dpi rgb panel or bridge, using gpios. ++ - for internal dpi input of the MIPI DSI host controller. ++ Note: These 2 endpoints cannot be activated simultaneously. ++ ++required: ++ - compatible ++ - reg ++ - interrupts ++ - clocks ++ - clock-names ++ - resets ++ - port ++ ++additionalProperties: false ++ ++examples: ++ - | ++ #include ++ #include ++ #include ++ ltdc: display-controller@40016800 { ++ compatible = "st,stm32-ltdc"; ++ reg = <0x5a001000 0x400>; ++ interrupts = , ++ ; ++ clocks = <&rcc LTDC_PX>; ++ clock-names = "lcd"; ++ resets = <&rcc LTDC_R>; ++ ++ port { ++ ltdc_out_dsi: endpoint { ++ remote-endpoint = <&dsi_in>; ++ }; ++ }; ++ }; ++ ++... ++ +diff --git a/documentation/devicetree/bindings/hwmon/st,stm32-hse-monitoring.yaml b/documentation/devicetree/bindings/hwmon/st,stm32-hse-monitoring.yaml +new file mode 100644 +index 000000000..107e4a941 +--- /dev/null ++++ b/documentation/devicetree/bindings/hwmon/st,stm32-hse-monitoring.yaml +@@ -0,0 +1,38 @@ ++# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/hwmon/st,stm32-hse-monitoring.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: STMicroelectronics STM32 HSE frequency monitoring bindings ++ ++maintainers: ++ - Lionel Debieve ++ ++description: | ++ STMicroelectronics's STM32 MPUs are able to monitor High Speed External ++ oscillator frequency. Based on a lptimer, it is possible to detect a ++ overclock frequency which can corrupt the system. The frequency overclock ++ detection is detected thanks to High Speed Internal oscillator reference. ++ ++properties: ++ compatible: ++ const: st,freq-monitor ++ ++ counter: ++ description: Counter used to monitor the frequency. ++ Refer to counter.yaml for more details. ++ ++required: ++ - compatible ++ ++additionalProperties: false ++ ++examples: ++- | ++ hse_monitor: hse-monitor { ++ compatible = "st,freq-monitor"; ++ counter = <&lptimer3 1 1 0 0>; ++ }; ++ ++... +diff --git a/documentation/devicetree/bindings/hwmon/st,stm32-tamp.yaml b/documentation/devicetree/bindings/hwmon/st,stm32-tamp.yaml +new file mode 100644 +index 000000000..3f1f85ef9 +--- /dev/null ++++ b/documentation/devicetree/bindings/hwmon/st,stm32-tamp.yaml +@@ -0,0 +1,91 @@ ++# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/hwmon/st,stm32-tamp.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: STMicroelectronics STM32 Tamper bindings ++ ++maintainers: ++ - Nicolas Toromanoff ++ ++properties: ++ compatible: ++ enum: ++ - st,stm32-tamp ++ - st,stm32mp13-tamp ++ ++ reg: ++ maxItems: 1 ++ ++ clocks: ++ maxItems: 1 ++ ++ interrupts: ++ maxItems: 1 ++ ++ st,tamp_passive_precharge: ++ description: 0 is no precharge, so the pull-up is disabled for all external passive tamper. ++ 1, 2, 3, 4, 8 define the number of cycle the STM32 precharge the pin before ++ checking the value (low/high) for all external tamper. ++ $ref: /schemas/types.yaml#/definitions/uint32 ++ enum: [0, 1, 2, 4, 8] ++ ++ st,tamp_passive_nb_sample: ++ description: The number of sample to read in a row to raise a passive tamper. ++ $ref: /schemas/types.yaml#/definitions/uint32 ++ enum: [0, 2, 4, 8] ++ ++ st,tamp_passive_sample_clk_div: ++ description: Clock divider to define frequency to read a sample for a passive tamper. ++ $ref: /schemas/types.yaml#/definitions/uint32 ++ ++ st,tamp_active_filter: ++ description: Set the active tamper filtering. 0 when active tamper filtering is disabled. ++ 1 when active tamper filtering is enabled. A tamper event is detected when 2 comparison ++ mismatches occur out of 4 consecutive samples. ++ $ref: /schemas/types.yaml#/definitions/uint32 ++ enum: [0, 1] ++ ++ st,tamp_active_clk_div: ++ description: clock divider to define the frequency of read/write cycle for all active tampers. ++ $ref: /schemas/types.yaml#/definitions/uint32 ++ ++ st,trig_on: ++ description: Defines the active level to activate this passive tamper ++ (not used if pintctrl-0 as 2 entries). ++ It also depends on st,tamp_passive_precharge value. When st,tamp_passive_precharge=0 ++ this passive tamper activate at edge. When st,tamp_passive_precharge != 0, with st,trig_on ++ defined, this passive tamper will activate if st,tamp_passive_nb_sample in a row are high. ++ If not defined the passive tamper event is raised if st,tamp_passive_nb_sample in a row ++ are low. ++ type: boolean ++ ++patternProperties: ++ "^tamp_passive@[0-9]+$": ++ description: Describe the passive tamper with its allocated pinctrl. ++ type: object ++ ++ "^tamp_active@[0-9]+$": ++ type: object ++ description: ++ Describe the active tamper with its allocated pinctrl. ++ ++required: ++ - compatible ++ - reg ++ - clocks ++ ++additionalProperties: false ++ ++examples: ++ - | ++ #include ++ #include ++ tamp: tamp@5c00a000 { ++ compatible = "st,stm32mp13-tamp"; ++ reg = <0x5c00a000 0x400>; ++ interrupts = ; ++ clocks = <&rcc RTCAPB>; ++ }; ++... +diff --git a/documentation/devicetree/bindings/i2c/st,stm32-i2c.yaml b/documentation/devicetree/bindings/i2c/st,stm32-i2c.yaml +new file mode 100644 +index 000000000..96213a6cb +--- /dev/null ++++ b/documentation/devicetree/bindings/i2c/st,stm32-i2c.yaml +@@ -0,0 +1,150 @@ ++# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/i2c/st,stm32-i2c.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: I2C controller embedded in STMicroelectronics STM32 I2C platform ++ ++maintainers: ++ - Pierre-Yves MORDRET ++ ++allOf: ++ - $ref: /schemas/i2c/i2c-controller.yaml# ++ - if: ++ properties: ++ compatible: ++ contains: ++ enum: ++ - st,stm32f7-i2c ++ - st,stm32mp13-i2c ++ - st,stm32mp15-i2c ++ # This compatible allows I2C to be probed as non-secure by OP-TEE ++ - st,stm32mp15-i2c-non-secure ++ then: ++ properties: ++ i2c-scl-rising-time-ns: ++ default: 25 ++ ++ i2c-scl-falling-time-ns: ++ default: 10 ++ ++ st,syscfg-fmp: ++ description: Use to set Fast Mode Plus bit within SYSCFG when ++ Fast Mode Plus speed is selected by slave. ++ Format is phandle to syscfg / register offset within ++ syscfg / register bitmask for FMP bit. ++ $ref: "/schemas/types.yaml#/definitions/phandle-array" ++ items: ++ minItems: 3 ++ maxItems: 3 ++ ++ - if: ++ properties: ++ compatible: ++ contains: ++ enum: ++ - st,stm32f4-i2c ++ then: ++ properties: ++ clock-frequency: ++ enum: [100000, 400000] ++ ++properties: ++ compatible: ++ enum: ++ - st,stm32f4-i2c ++ - st,stm32f7-i2c ++ - st,stm32mp13-i2c ++ - st,stm32mp15-i2c ++ # This compatible allows I2C to be probed as non-secure by OP-TEE ++ - st,stm32mp15-i2c-non-secure ++ ++ reg: ++ maxItems: 1 ++ ++ interrupts: ++ items: ++ - description: interrupt ID for I2C event ++ - description: interrupt ID for I2C error ++ ++ resets: ++ maxItems: 1 ++ ++ clocks: ++ maxItems: 1 ++ ++ dmas: ++ items: ++ - description: RX DMA Channel phandle ++ - description: TX DMA Channel phandle ++ ++ dma-names: ++ items: ++ - const: rx ++ - const: tx ++ ++ clock-frequency: ++ description: Desired I2C bus clock frequency in Hz. If not specified, ++ the default 100 kHz frequency will be used. ++ For STM32F7, STM32H7 and STM32MP1 SoCs, if timing parameters ++ match, the bus clock frequency can be from 1Hz to 1MHz. ++ default: 100000 ++ minimum: 1 ++ maximum: 1000000 ++ ++required: ++ - compatible ++ - reg ++ - interrupts ++ - resets ++ - clocks ++ ++unevaluatedProperties: false ++ ++examples: ++ - | ++ #include ++ #include ++ //Example 1 (with st,stm32f4-i2c compatible) ++ i2c@40005400 { ++ compatible = "st,stm32f4-i2c"; ++ #address-cells = <1>; ++ #size-cells = <0>; ++ reg = <0x40005400 0x400>; ++ interrupts = <31>, ++ <32>; ++ resets = <&rcc 277>; ++ clocks = <&rcc 0 149>; ++ }; ++ ++ //Example 2 (with st,stm32f7-i2c compatible) ++ i2c@40005800 { ++ compatible = "st,stm32f7-i2c"; ++ #address-cells = <1>; ++ #size-cells = <0>; ++ reg = <0x40005800 0x400>; ++ interrupts = <31>, ++ <32>; ++ resets = <&rcc STM32F7_APB1_RESET(I2C1)>; ++ clocks = <&rcc 1 CLK_I2C1>; ++ }; ++ ++ //Example 3 (with st,stm32mp15-i2c compatible on stm32mp) ++ #include ++ #include ++ #include ++ i2c@40013000 { ++ compatible = "st,stm32mp15-i2c"; ++ #address-cells = <1>; ++ #size-cells = <0>; ++ reg = <0x40013000 0x400>; ++ interrupts = , ++ ; ++ clocks = <&rcc I2C2_K>; ++ resets = <&rcc I2C2_R>; ++ i2c-scl-rising-time-ns = <185>; ++ i2c-scl-falling-time-ns = <20>; ++ st,syscfg-fmp = <&syscfg 0x4 0x2>; ++ }; ++... +diff --git a/documentation/devicetree/bindings/interrupt-controller/arm,gic.yaml b/documentation/devicetree/bindings/interrupt-controller/arm,gic.yaml +new file mode 100644 +index 000000000..ba282f4c9 +--- /dev/null ++++ b/documentation/devicetree/bindings/interrupt-controller/arm,gic.yaml +@@ -0,0 +1,242 @@ ++# SPDX-License-Identifier: GPL-2.0 ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/interrupt-controller/arm,gic.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: ARM Generic Interrupt Controller v1 and v2 ++ ++maintainers: ++ - Marc Zyngier ++ ++description: |+ ++ ARM SMP cores are often associated with a GIC, providing per processor ++ interrupts (PPI), shared processor interrupts (SPI) and software ++ generated interrupts (SGI). ++ ++ Primary GIC is attached directly to the CPU and typically has PPIs and SGIs. ++ Secondary GICs are cascaded into the upward interrupt controller and do not ++ have PPIs or SGIs. ++ ++allOf: ++ - $ref: /schemas/interrupt-controller.yaml# ++ ++properties: ++ compatible: ++ oneOf: ++ - items: ++ - enum: ++ - arm,arm11mp-gic ++ - arm,cortex-a15-gic ++ - arm,cortex-a7-gic ++ - arm,cortex-a5-gic ++ - arm,cortex-a9-gic ++ - arm,eb11mp-gic ++ - arm,gic-400 ++ - arm,pl390 ++ - arm,tc11mp-gic ++ - qcom,msm-8660-qgic ++ - qcom,msm-qgic2 ++ ++ - items: ++ - const: arm,gic-400 ++ - enum: ++ - arm,cortex-a15-gic ++ - arm,cortex-a7-gic ++ ++ - items: ++ - const: arm,arm1176jzf-devchip-gic ++ - const: arm,arm11mp-gic ++ ++ - items: ++ - const: brcm,brahma-b15-gic ++ - const: arm,cortex-a15-gic ++ ++ - oneOf: ++ - const: nvidia,tegra210-agic ++ - items: ++ - enum: ++ - nvidia,tegra186-agic ++ - nvidia,tegra194-agic ++ - const: nvidia,tegra210-agic ++ ++ interrupt-controller: true ++ ++ "#address-cells": ++ enum: [ 0, 1 ] ++ "#size-cells": ++ const: 1 ++ ++ "#interrupt-cells": ++ const: 3 ++ description: | ++ The 1st cell is the interrupt type; 0 for SPI interrupts, 1 for PPI ++ interrupts. ++ ++ The 2nd cell contains the interrupt number for the interrupt type. ++ SPI interrupts are in the range [0-987]. PPI interrupts are in the ++ range [0-15]. ++ ++ The 3rd cell is the flags, encoded as follows: ++ bits[3:0] trigger type and level flags. ++ 1 = low-to-high edge triggered ++ 2 = high-to-low edge triggered (invalid for SPIs) ++ 4 = active high level-sensitive ++ 8 = active low level-sensitive (invalid for SPIs). ++ bits[15:8] PPI interrupt cpu mask. Each bit corresponds to each of ++ the 8 possible cpus attached to the GIC. A bit set to '1' indicated ++ the interrupt is wired to that CPU. Only valid for PPI interrupts. ++ Also note that the configurability of PPI interrupts is IMPLEMENTATION ++ DEFINED and as such not guaranteed to be present (most SoC available ++ in 2014 seem to ignore the setting of this flag and use the hardware ++ default value). ++ ++ reg: ++ description: | ++ Specifies base physical address(s) and size of the GIC registers. The ++ first region is the GIC distributor register base and size. The 2nd region ++ is the GIC cpu interface register base and size. ++ ++ For GICv2 with virtualization extensions, additional regions are ++ required for specifying the base physical address and size of the VGIC ++ registers. The first additional region is the GIC virtual interface ++ control register base and size. The 2nd additional region is the GIC ++ virtual cpu interface register base and size. ++ minItems: 2 ++ maxItems: 4 ++ ++ ranges: true ++ ++ interrupts: ++ description: Interrupt source of the parent interrupt controller on ++ secondary GICs, or VGIC maintenance interrupt on primary GIC (see ++ below). ++ maxItems: 1 ++ ++ cpu-offset: ++ description: per-cpu offset within the distributor and cpu interface ++ regions, used when the GIC doesn't have banked registers. The offset ++ is cpu-offset * cpu-nr. ++ $ref: /schemas/types.yaml#/definitions/uint32 ++ ++ clocks: ++ minItems: 1 ++ maxItems: 2 ++ ++ clock-names: ++ description: List of names for the GIC clock input(s). Valid clock names ++ depend on the GIC variant. ++ oneOf: ++ - const: ic_clk # for "arm,arm11mp-gic" ++ - const: PERIPHCLKEN # for "arm,cortex-a15-gic" ++ - items: # for "arm,cortex-a9-gic" ++ - const: PERIPHCLK ++ - const: PERIPHCLKEN ++ - const: clk # for "arm,gic-400" and "nvidia,tegra210" ++ - const: gclk #for "arm,pl390" ++ ++ power-domains: ++ maxItems: 1 ++ ++ resets: ++ maxItems: 1 ++ ++required: ++ - compatible ++ - reg ++ ++patternProperties: ++ "^v2m@[0-9a-f]+$": ++ type: object ++ description: | ++ * GICv2m extension for MSI/MSI-x support (Optional) ++ ++ Certain revisions of GIC-400 supports MSI/MSI-x via V2M register frame(s). ++ This is enabled by specifying v2m sub-node(s). ++ ++ properties: ++ compatible: ++ const: arm,gic-v2m-frame ++ ++ msi-controller: true ++ ++ reg: ++ maxItems: 1 ++ description: GICv2m MSI interface register base and size ++ ++ arm,msi-base-spi: ++ description: When the MSI_TYPER register contains an incorrect value, ++ this property should contain the SPI base of the MSI frame, overriding ++ the HW value. ++ $ref: /schemas/types.yaml#/definitions/uint32 ++ ++ arm,msi-num-spis: ++ description: When the MSI_TYPER register contains an incorrect value, ++ this property should contain the number of SPIs assigned to the ++ frame, overriding the HW value. ++ $ref: /schemas/types.yaml#/definitions/uint32 ++ ++ required: ++ - compatible ++ - msi-controller ++ - reg ++ ++ additionalProperties: false ++ ++additionalProperties: false ++ ++examples: ++ - | ++ // GICv1 ++ intc: interrupt-controller@fff11000 { ++ compatible = "arm,cortex-a9-gic"; ++ #interrupt-cells = <3>; ++ #address-cells = <1>; ++ interrupt-controller; ++ reg = <0xfff11000 0x1000>, ++ <0xfff10100 0x100>; ++ }; ++ ++ - | ++ // GICv2 ++ interrupt-controller@2c001000 { ++ compatible = "arm,cortex-a15-gic"; ++ #interrupt-cells = <3>; ++ interrupt-controller; ++ reg = <0x2c001000 0x1000>, ++ <0x2c002000 0x2000>, ++ <0x2c004000 0x2000>, ++ <0x2c006000 0x2000>; ++ interrupts = <1 9 0xf04>; ++ }; ++ ++ - | ++ // GICv2m extension for MSI/MSI-x support ++ interrupt-controller@e1101000 { ++ compatible = "arm,gic-400"; ++ #interrupt-cells = <3>; ++ #address-cells = <1>; ++ #size-cells = <1>; ++ interrupt-controller; ++ interrupts = <1 8 0xf04>; ++ ranges = <0 0xe1100000 0x100000>; ++ reg = <0xe1110000 0x01000>, ++ <0xe112f000 0x02000>, ++ <0xe1140000 0x10000>, ++ <0xe1160000 0x10000>; ++ ++ v2m0: v2m@80000 { ++ compatible = "arm,gic-v2m-frame"; ++ msi-controller; ++ reg = <0x80000 0x1000>; ++ }; ++ ++ //... ++ ++ v2mN: v2m@90000 { ++ compatible = "arm,gic-v2m-frame"; ++ msi-controller; ++ reg = <0x90000 0x1000>; ++ }; ++ }; ++... +diff --git a/documentation/devicetree/bindings/interrupt-controller/st,stm32-exti.yaml b/documentation/devicetree/bindings/interrupt-controller/st,stm32-exti.yaml +new file mode 100644 +index 000000000..253e07503 +--- /dev/null ++++ b/documentation/devicetree/bindings/interrupt-controller/st,stm32-exti.yaml +@@ -0,0 +1,120 @@ ++# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/interrupt-controller/st,stm32-exti.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: STM32 External Interrupt Controller Device Tree Bindings ++ ++maintainers: ++ - Alexandre Torgue ++ - Ludovic Barre ++ ++properties: ++ compatible: ++ oneOf: ++ - items: ++ - enum: ++ - st,stm32-exti ++ - st,stm32h7-exti ++ - items: ++ - enum: ++ - st,stm32mp1-exti ++ - st,stm32mp13-exti ++ - const: syscon ++ ++ "#interrupt-cells": ++ const: 2 ++ ++ reg: ++ maxItems: 1 ++ ++ interrupt-controller: true ++ ++ hwlocks: ++ maxItems: 1 ++ description: ++ Reference to a phandle of a hardware spinlock provider node. ++ ++ interrupts: ++ minItems: 1 ++ maxItems: 96 ++ description: ++ Interrupts references to primary interrupt controller ++ ++patternProperties: ++ '^exti[0-9a-f]*$': ++ type: object ++ properties: ++ interrupt-controller: true ++ "#interrupt-cells": ++ const: 2 ++ ++ st,irq-number: ++ description: ++ Interrupt number mapped on the parent. ++ $ref: "/schemas/types.yaml#/definitions/uint32" ++ ++ required: ++ - "#interrupt-cells" ++ - interrupt-controller ++ - st,irq-number ++ - interrupt-parent ++ ++required: ++ - "#interrupt-cells" ++ - compatible ++ - reg ++ - interrupt-controller ++ ++allOf: ++ - $ref: /schemas/interrupt-controller.yaml# ++ - if: ++ properties: ++ compatible: ++ contains: ++ enum: ++ - st,stm32-exti ++ then: ++ properties: ++ interrupts: ++ minItems: 1 ++ maxItems: 32 ++ required: ++ - interrupts ++ - if: ++ properties: ++ compatible: ++ contains: ++ enum: ++ - st,stm32h7-exti ++ then: ++ properties: ++ interrupts: ++ minItems: 1 ++ maxItems: 96 ++ required: ++ - interrupts ++ ++additionalProperties: false ++ ++examples: ++ - | ++ //Example 1 ++ exti1: interrupt-controller@5000d000 { ++ compatible = "st,stm32mp1-exti", "syscon"; ++ interrupt-controller; ++ #interrupt-cells = <2>; ++ reg = <0x5000d000 0x400>; ++ }; ++ ++ //Example 2 ++ exti2: interrupt-controller@40013c00 { ++ compatible = "st,stm32-exti"; ++ interrupt-controller; ++ #interrupt-cells = <2>; ++ reg = <0x40013C00 0x400>; ++ interrupts = <1>, <2>, <3>, <6>, <7>, <8>, <9>, <10>, <23>, <40>, <41>, <42>, <62>, <76>; ++ }; ++ ++... +diff --git a/documentation/devicetree/bindings/mfd/st,stm32-lptimer.yaml b/documentation/devicetree/bindings/mfd/st,stm32-lptimer.yaml +new file mode 100644 +index 000000000..643059166 +--- /dev/null ++++ b/documentation/devicetree/bindings/mfd/st,stm32-lptimer.yaml +@@ -0,0 +1,147 @@ ++# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/mfd/st,stm32-lptimer.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: STMicroelectronics STM32 Low-Power Timers bindings ++ ++description: | ++ The STM32 Low-Power Timer (LPTIM) is a 16-bit timer that provides several ++ functions ++ - PWM output (with programmable prescaler, configurable polarity) ++ - Trigger source for STM32 ADC/DAC (LPTIM_OUT) ++ - Several counter modes: ++ - quadrature encoder to detect angular position and direction of rotary ++ elements, from IN1 and IN2 input signals. ++ - simple counter from IN1 input signal. ++ ++maintainers: ++ - Fabrice Gasnier ++ ++properties: ++ compatible: ++ const: st,stm32-lptimer ++ ++ reg: ++ maxItems: 1 ++ ++ clocks: ++ maxItems: 1 ++ ++ clock-names: ++ items: ++ - const: mux ++ ++ interrupts: ++ maxItems: 1 ++ ++ "#address-cells": ++ const: 1 ++ ++ "#size-cells": ++ const: 0 ++ ++ resets: ++ maxItems: 1 ++ ++ wakeup-source: true ++ ++ power-domains: ++ maxItems: 1 ++ ++ pwm: ++ type: object ++ ++ properties: ++ compatible: ++ const: st,stm32-pwm-lp ++ ++ "#pwm-cells": ++ const: 3 ++ ++ required: ++ - "#pwm-cells" ++ - compatible ++ ++patternProperties: ++ "^trigger@[0-9]+$": ++ type: object ++ ++ properties: ++ compatible: ++ const: st,stm32-lptimer-trigger ++ ++ reg: ++ description: Identify trigger hardware block. ++ items: ++ minimum: 0 ++ maximum: 2 ++ ++ required: ++ - compatible ++ - reg ++ ++ counter: ++ type: object ++ ++ properties: ++ compatible: ++ const: st,stm32-lptimer-counter ++ ++ required: ++ - compatible ++ ++ timer: ++ type: object ++ ++ properties: ++ compatible: ++ const: st,stm32-lptimer-timer ++ ++ required: ++ - compatible ++ ++required: ++ - "#address-cells" ++ - "#size-cells" ++ - compatible ++ - reg ++ - clocks ++ - clock-names ++ ++additionalProperties: false ++ ++examples: ++ - | ++ #include ++ #include ++ timer@40002400 { ++ compatible = "st,stm32-lptimer"; ++ reg = <0x40002400 0x400>; ++ clocks = <&timer_clk>; ++ clock-names = "mux"; ++ interrupts-extended = <&exti 47 IRQ_TYPE_LEVEL_HIGH>; ++ #address-cells = <1>; ++ #size-cells = <0>; ++ ++ pwm { ++ compatible = "st,stm32-pwm-lp"; ++ #pwm-cells = <3>; ++ }; ++ ++ trigger@0 { ++ compatible = "st,stm32-lptimer-trigger"; ++ reg = <0>; ++ }; ++ ++ counter { ++ compatible = "st,stm32-lptimer-counter"; ++ }; ++ ++ timer { ++ compatible = "st,stm32-lptimer-timer"; ++ }; ++ }; ++ ++... +diff --git a/documentation/devicetree/bindings/mfd/st,stm32-timers.yaml b/documentation/devicetree/bindings/mfd/st,stm32-timers.yaml +new file mode 100644 +index 000000000..dace35362 +--- /dev/null ++++ b/documentation/devicetree/bindings/mfd/st,stm32-timers.yaml +@@ -0,0 +1,159 @@ ++# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/mfd/st,stm32-timers.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: STMicroelectronics STM32 Timers bindings ++ ++description: | ++ This hardware block provides 3 types of timer along with PWM functionality: ++ - advanced-control timers consist of a 16-bit auto-reload counter driven ++ by a programmable prescaler, break input feature, PWM outputs and ++ complementary PWM outputs channels. ++ - general-purpose timers consist of a 16-bit or 32-bit auto-reload counter ++ driven by a programmable prescaler and PWM outputs. ++ - basic timers consist of a 16-bit auto-reload counter driven by a ++ programmable prescaler. ++ ++maintainers: ++ - Benjamin Gaignard ++ - Fabrice Gasnier ++ ++properties: ++ compatible: ++ const: st,stm32-timers ++ ++ reg: ++ maxItems: 1 ++ ++ clocks: ++ maxItems: 1 ++ ++ clock-names: ++ items: ++ - const: int ++ ++ reset: ++ maxItems: 1 ++ ++ dmas: ++ minItems: 1 ++ maxItems: 7 ++ ++ dma-names: ++ items: ++ enum: [ ch1, ch2, ch3, ch4, up, trig, com ] ++ minItems: 1 ++ maxItems: 7 ++ ++ "#address-cells": ++ const: 1 ++ ++ "#size-cells": ++ const: 0 ++ ++ pwm: ++ type: object ++ ++ properties: ++ compatible: ++ const: st,stm32-pwm ++ ++ "#pwm-cells": ++ const: 3 ++ ++ st,breakinput: ++ description: ++ One or two to describe break input ++ configurations. ++ $ref: /schemas/types.yaml#/definitions/uint32-matrix ++ items: ++ items: ++ - description: | ++ "index" indicates on which break input (0 or 1) the ++ configuration should be applied. ++ enum: [0, 1] ++ - description: | ++ "level" gives the active level (0=low or 1=high) of the ++ input signal for this configuration ++ enum: [0, 1] ++ - description: | ++ "filter" gives the filtering value (up to 15) to be applied. ++ maximum: 15 ++ minItems: 1 ++ maxItems: 2 ++ ++ required: ++ - "#pwm-cells" ++ - compatible ++ ++patternProperties: ++ "^timer@[0-9]+$": ++ type: object ++ ++ properties: ++ compatible: ++ enum: ++ - st,stm32-timer-trigger ++ - st,stm32h7-timer-trigger ++ ++ reg: ++ description: Identify trigger hardware block. ++ items: ++ minimum: 0 ++ maximum: 16 ++ ++ required: ++ - compatible ++ - reg ++ ++ counter: ++ type: object ++ ++ properties: ++ compatible: ++ const: st,stm32-timer-counter ++ ++ required: ++ - compatible ++ ++required: ++ - compatible ++ - reg ++ - clocks ++ - clock-names ++ ++additionalProperties: false ++ ++examples: ++ - | ++ #include ++ timers2: timer@40000000 { ++ #address-cells = <1>; ++ #size-cells = <0>; ++ compatible = "st,stm32-timers"; ++ reg = <0x40000000 0x400>; ++ clocks = <&rcc TIM2_K>; ++ clock-names = "int"; ++ dmas = <&dmamux1 18 0x400 0x1>, ++ <&dmamux1 19 0x400 0x1>, ++ <&dmamux1 20 0x400 0x1>, ++ <&dmamux1 21 0x400 0x1>, ++ <&dmamux1 22 0x400 0x1>; ++ dma-names = "ch1", "ch2", "ch3", "ch4", "up"; ++ pwm { ++ compatible = "st,stm32-pwm"; ++ #pwm-cells = <3>; ++ st,breakinput = <0 1 5>; ++ }; ++ timer@1 { ++ compatible = "st,stm32-timer-trigger"; ++ reg = <1>; ++ }; ++ counter { ++ compatible = "st,stm32-timer-counter"; ++ }; ++ }; ++ ++... +diff --git a/documentation/devicetree/bindings/mfd/st,stpmic1.yaml b/documentation/devicetree/bindings/mfd/st,stpmic1.yaml +new file mode 100644 +index 000000000..344623f1f +--- /dev/null ++++ b/documentation/devicetree/bindings/mfd/st,stpmic1.yaml +@@ -0,0 +1,377 @@ ++# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/mfd/st,stpmic1.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: STMicroelectonics STPMIC1 Power Management IC bindings ++ ++description: STMicroelectronics STPMIC1 Power Management IC ++ ++maintainers: ++ - pascal Paillet ++ ++properties: ++ compatible: ++ const: st,stpmic1 ++ ++ reg: ++ const: 0x33 ++ ++ interrupts: ++ maxItems: 1 ++ ++ "#interrupt-cells": ++ const: 2 ++ ++ interrupt-controller: true ++ ++ onkey: ++ type: object ++ ++ $ref: ../input/input.yaml ++ ++ properties: ++ compatible: ++ const: st,stpmic1-onkey ++ ++ interrupts: ++ items: ++ - description: onkey-falling, happens when onkey is pressed. IT_PONKEY_F of pmic ++ - description: onkey-rising, happens when onkey is released. IT_PONKEY_R of pmic ++ ++ interrupt-names: ++ items: ++ - const: onkey-falling ++ - const: onkey-rising ++ ++ st,onkey-clear-cc-flag: ++ description: onkey is able power on after an over-current shutdown event. ++ $ref: /schemas/types.yaml#/definitions/flag ++ ++ st,onkey-pu-inactive: ++ description: onkey pull up is not active ++ $ref: /schemas/types.yaml#/definitions/flag ++ ++ power-off-time-sec: ++ minimum: 1 ++ maximum: 16 ++ ++ required: ++ - compatible ++ - interrupts ++ - interrupt-names ++ ++ additionalProperties: false ++ ++ watchdog: ++ type: object ++ ++ $ref: ../watchdog/watchdog.yaml ++ ++ properties: ++ compatible: ++ const: st,stpmic1-wdt ++ ++ timeout-sec: true ++ ++ required: ++ - compatible ++ ++ additionalProperties: false ++ ++ regulators: ++ type: object ++ ++ description: | ++ Available Regulators in STPMIC1 device are: ++ - buck1 for Buck BUCK1 ++ - buck2 for Buck BUCK2 ++ - buck3 for Buck BUCK3 ++ - buck4 for Buck BUCK4 ++ - ldo1 for LDO LDO1 ++ - ldo2 for LDO LDO2 ++ - ldo3 for LDO LDO3 ++ - ldo4 for LDO LDO4 ++ - ldo5 for LDO LDO5 ++ - ldo6 for LDO LDO6 ++ - vref_ddr for LDO Vref DDR ++ - boost for Buck BOOST ++ - pwr_sw1 for VBUS_OTG switch ++ - pwr_sw2 for SW_OUT switch ++ Switches are fixed voltage regulators with only enable/disable capability. ++ ++ properties: ++ compatible: ++ const: st,stpmic1-regulators ++ ++ ldo3: ++ type: object ++ ++ properties: ++ interrupts: ++ maxItems: 1 ++ ++ st,mask-reset: ++ description: mask reset for this regulator, the regulator configuration ++ is maintained during pmic reset. ++ $ref: /schemas/types.yaml#/definitions/flag ++ ++ regulator-name: true ++ regulator-boot-on: true ++ regulator-always-on: true ++ regulator-min-microvolt: true ++ regulator-max-microvolt: true ++ regulator-allow-bypass: true ++ regulator-over-current-protection: true ++ ++ additionalProperties: false ++ ++ ldo4: ++ type: object ++ ++ properties: ++ interrupts: ++ maxItems: 1 ++ ++ st,mask-reset: ++ description: mask reset for this regulator, the regulator configuration ++ is maintained during pmic reset. ++ $ref: /schemas/types.yaml#/definitions/flag ++ ++ regulator-name: true ++ regulator-boot-on: true ++ regulator-always-on: true ++ regulator-over-current-protection: true ++ ++ additionalProperties: false ++ ++ vref_ddr: ++ type: object ++ ++ properties: ++ interrupts: ++ maxItems: 1 ++ ++ st,mask-reset: ++ description: mask reset for this regulator, the regulator configuration ++ is maintained during pmic reset. ++ $ref: /schemas/types.yaml#/definitions/flag ++ ++ regulator-name: true ++ regulator-boot-on: true ++ regulator-always-on: true ++ ++ additionalProperties: false ++ ++ boost: ++ type: object ++ ++ properties: ++ interrupts: ++ maxItems: 1 ++ ++ st,mask-reset: ++ description: mask reset for this regulator, the regulator configuration ++ is maintained during pmic reset. ++ $ref: /schemas/types.yaml#/definitions/flag ++ ++ regulator-name: true ++ regulator-boot-on: true ++ regulator-always-on: true ++ regulator-over-current-protection: true ++ ++ additionalProperties: false ++ ++ patternProperties: ++ "^(buck[1-4]|ldo[1-6]|boost|pwr_sw[1-2])-supply$": ++ description: STPMIC1 voltage regulators supplies ++ ++ "^(buck[1-4]|ldo[1-6]|boost|vref_ddr|pwr_sw[1-2])$": ++ $ref: ../regulator/regulator.yaml ++ ++ "^ldo[1-2,5-6]$": ++ type: object ++ ++ properties: ++ interrupts: ++ maxItems: 1 ++ ++ st,mask-reset: ++ description: mask reset for this regulator, the regulator configuration ++ is maintained during pmic reset. ++ $ref: /schemas/types.yaml#/definitions/flag ++ ++ regulator-name: true ++ regulator-boot-on: true ++ regulator-always-on: true ++ regulator-min-microvolt: true ++ regulator-max-microvolt: true ++ regulator-over-current-protection: true ++ regulator-enable-ramp-delay: true ++ ++ additionalProperties: false ++ ++ "^buck[1-4]$": ++ type: object ++ ++ properties: ++ interrupts: ++ maxItems: 1 ++ ++ st,mask-reset: ++ description: mask reset for this regulator, the regulator configuration ++ is maintained during pmic reset. ++ $ref: /schemas/types.yaml#/definitions/flag ++ ++ regulator-name: true ++ regulator-boot-on: true ++ regulator-always-on: true ++ regulator-min-microvolt: true ++ regulator-max-microvolt: true ++ regulator-initial-mode: true ++ regulator-pull-down: true ++ regulator-over-current-protection: true ++ regulator-enable-ramp-delay: true ++ ++ additionalProperties: false ++ ++ "^pwr_sw[1-2]$": ++ type: object ++ ++ properties: ++ interrupts: ++ maxItems: 1 ++ ++ regulator-name: true ++ regulator-boot-on: true ++ regulator-always-on: true ++ regulator-over-current-protection: true ++ regulator-active-discharge: true ++ ++ additionalProperties: false ++ ++ required: ++ - compatible ++ ++ additionalProperties: false ++ ++ st,wakeup-pin-number: ++ description: Pin used as wake-up source when for MP13 ++ $ref: /schemas/types.yaml#/definitions/uint32 ++ minItems: 1 ++ maxItems: 1 ++ ++additionalProperties: false ++ ++required: ++ - compatible ++ - reg ++ - interrupts ++ - "#interrupt-cells" ++ - interrupt-controller ++ ++examples: ++ - | ++ #include ++ #include ++ #include ++ i2c { ++ #address-cells = <1>; ++ #size-cells = <0>; ++ pmic@33 { ++ compatible = "st,stpmic1"; ++ reg = <0x33>; ++ interrupt-parent = <&gpioa>; ++ interrupts = <0 2>; ++ ++ interrupt-controller; ++ #interrupt-cells = <2>; ++ ++ st,wakeup-pin-number = <1>; ++ ++ onkey { ++ compatible = "st,stpmic1-onkey"; ++ interrupts = ,; ++ interrupt-names = "onkey-falling", "onkey-rising"; ++ power-off-time-sec = <10>; ++ }; ++ ++ watchdog { ++ compatible = "st,stpmic1-wdt"; ++ }; ++ ++ regulators { ++ compatible = "st,stpmic1-regulators"; ++ ++ ldo6-supply = <&v3v3>; ++ ++ buck1 { ++ regulator-name = "vdd_core"; ++ interrupts = ; ++ st,mask-reset; ++ regulator-boot-on; ++ regulator-min-microvolt = <700000>; ++ regulator-max-microvolt = <1200000>; ++ lp-stop { ++ regulator-suspend-microvolt = <1250000>; ++ }; ++ lplv-stop { ++ regulator-suspend-microvolt = <900000>; ++ }; ++ lplv-stop2 { ++ regulator-off-in-suspend; ++ }; ++ standby-ddr-sr { ++ regulator-off-in-suspend; ++ }; ++ standby-ddr-off { ++ regulator-off-in-suspend; ++ }; ++ ++ buck3 { ++ regulator-name = "vdd"; ++ regulator-min-microvolt = <3300000>; ++ regulator-max-microvolt = <3300000>; ++ regulator-boot-on; ++ regulator-pull-down; ++ }; ++ ++ buck4 { ++ regulator-name = "v3v3"; ++ interrupts = ; ++ regulator-min-microvolt = <3300000>; ++ regulator-max-microvolt = <3300000>; ++ regulator-always-on; ++ regulator-over-current-protection; ++ ++ lplv-stop { ++ regulator-suspend-microvolt = <900000>; ++ }; ++ lplv-stop2 { ++ regulator-suspend-microvolt = <900000>; ++ }; ++ standby-ddr-sr { ++ regulator-off-in-suspend; ++ }; ++ standby-ddr-off { ++ regulator-off-in-suspend; ++ }; ++ }; ++ ++ ldo6 { ++ regulator-name = "v1v8"; ++ regulator-min-microvolt = <1800000>; ++ regulator-max-microvolt = <1800000>; ++ regulator-over-current-protection; ++ standby-ddr-sr { ++ regulator-off-in-suspend; ++ }; ++ standby-ddr-off { ++ regulator-off-in-suspend; ++ }; ++ }; ++ }; ++ }; ++ }; ++... +diff --git a/documentation/devicetree/bindings/nvmem/nvmem.yaml b/documentation/devicetree/bindings/nvmem/nvmem.yaml +new file mode 100644 +index 000000000..456fb8081 +--- /dev/null ++++ b/documentation/devicetree/bindings/nvmem/nvmem.yaml +@@ -0,0 +1,103 @@ ++# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/nvmem/nvmem.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: NVMEM (Non Volatile Memory) Device Tree Bindings ++ ++maintainers: ++ - Srinivas Kandagatla ++ ++description: | ++ This binding is intended to represent the location of hardware ++ configuration data stored in NVMEMs like eeprom, efuses and so on. ++ ++ On a significant proportion of boards, the manufacturer has stored ++ some data on NVMEM, for the OS to be able to retrieve these ++ information and act upon it. Obviously, the OS has to know about ++ where to retrieve these data from, and where they are stored on the ++ storage device. ++ ++properties: ++ "#address-cells": ++ const: 1 ++ ++ "#size-cells": ++ const: 1 ++ ++ read-only: ++ $ref: /schemas/types.yaml#/definitions/flag ++ description: ++ Mark the provider as read only. ++ ++ wp-gpios: ++ description: ++ GPIO to which the write-protect pin of the chip is connected. ++ The write-protect GPIO is asserted, when it's driven high ++ (logical '1') to block the write operation. It's deasserted, ++ when it's driven low (logical '0') to allow writing. ++ maxItems: 1 ++ ++patternProperties: ++ "@[0-9a-f]+(,[0-7])?$": ++ type: object ++ ++ properties: ++ reg: ++ maxItems: 1 ++ description: ++ Offset and size in bytes within the storage device. ++ ++ bits: ++ maxItems: 1 ++ items: ++ items: ++ - minimum: 0 ++ maximum: 7 ++ description: ++ Offset in bit within the address range specified by reg. ++ - minimum: 1 ++ description: ++ Size in bit within the address range specified by reg. ++ ++ required: ++ - reg ++ ++additionalProperties: true ++ ++examples: ++ - | ++ #include ++ ++ qfprom: eeprom@700000 { ++ #address-cells = <1>; ++ #size-cells = <1>; ++ reg = <0x00700000 0x100000>; ++ ++ wp-gpios = <&gpio1 3 GPIO_ACTIVE_HIGH>; ++ ++ /* ... */ ++ ++ /* Data cells */ ++ tsens_calibration: calib@404 { ++ reg = <0x404 0x10>; ++ }; ++ ++ tsens_calibration_bckp: calib_bckp@504 { ++ reg = <0x504 0x11>; ++ bits = <6 128>; ++ }; ++ ++ pvs_version: pvs-version@6 { ++ reg = <0x6 0x2>; ++ bits = <7 2>; ++ }; ++ ++ speed_bin: speed-bin@c{ ++ reg = <0xc 0x1>; ++ bits = <2 3>; ++ }; ++ }; ++ ++... +diff --git a/documentation/devicetree/bindings/nvmem/st,stm32-romem.yaml b/documentation/devicetree/bindings/nvmem/st,stm32-romem.yaml +new file mode 100644 +index 000000000..1b8819fe6 +--- /dev/null ++++ b/documentation/devicetree/bindings/nvmem/st,stm32-romem.yaml +@@ -0,0 +1,86 @@ ++# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/nvmem/st,stm32-romem.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: STMicroelectronics STM32 Factory-programmed data bindings ++ ++description: | ++ This represents STM32 Factory-programmed read only non-volatile area: locked ++ flash, OTP, read-only HW regs... This contains various information such as: ++ analog calibration data for temperature sensor (e.g. TS_CAL1, TS_CAL2), ++ internal vref (VREFIN_CAL), unique device ID... ++ ++maintainers: ++ - Fabrice Gasnier ++ ++allOf: ++ - $ref: "nvmem.yaml#" ++ ++properties: ++ compatible: ++ enum: ++ - st,stm32f4-otp ++ - st,stm32mp13-bsec ++ - st,stm32mp15-bsec ++ ++ clocks: ++ maxItems: 1 ++ description: | ++ - It's not present on stm32f4. ++ - It's not present on stm32mp13. ++ - It's optional on stm32mp15. ++ ++patternProperties: ++ "^.*@[0-9a-f]+$": ++ type: object ++ ++ properties: ++ st,non-secure-otp: ++ description: | ++ This property explicits a factory programmed area that both secure ++ and non-secure worlds can access. It is needed when, by default, the ++ related area can only be reached by the secure world. ++ It requires to be 32 bits word aligned to allow non-secure sharing. ++ type: boolean ++ st,non-secure-otp-provisioning: ++ description: | ++ This property explicits a factory programmed area that allows a non-secure ++ read and write until the first programmed value. Once the provisioning of ++ the specified area is done, access from non-secure is forbidden. ++ It is needed when, by default, the related area is empty and must be provisioned. ++ type: boolean ++ ++required: ++ - "#address-cells" ++ - "#size-cells" ++ - compatible ++ - reg ++ ++unevaluatedProperties: false ++ ++examples: ++ - | ++ efuse@1fff7800 { ++ compatible = "st,stm32f4-otp"; ++ reg = <0x1fff7800 0x400>; ++ #address-cells = <1>; ++ #size-cells = <1>; ++ ++ calib@22c { ++ reg = <0x22c 0x2>; ++ }; ++ ++ mac_addr@e4 { ++ reg = <0xe4 0x8>; ++ st,non-secure-otp; ++ }; ++ ++ oem_enc_key@170 { ++ reg = <0x170 0x10>; ++ st,non-secure-otp-provisioning; ++ }; ++ }; ++ ++... +diff --git a/documentation/devicetree/bindings/opp/opp-v2-base.yaml b/documentation/devicetree/bindings/opp/opp-v2-base.yaml +new file mode 100644 +index 000000000..ae3ae4d39 +--- /dev/null ++++ b/documentation/devicetree/bindings/opp/opp-v2-base.yaml +@@ -0,0 +1,214 @@ ++# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/opp/opp-v2-base.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: Generic OPP (Operating Performance Points) Common Binding ++ ++maintainers: ++ - Viresh Kumar ++ ++description: | ++ Devices work at voltage-current-frequency combinations and some implementations ++ have the liberty of choosing these. These combinations are called Operating ++ Performance Points aka OPPs. This document defines bindings for these OPPs ++ applicable across wide range of devices. For illustration purpose, this document ++ uses CPU as a device. ++ ++ This describes the OPPs belonging to a device. ++ ++select: false ++ ++properties: ++ $nodename: ++ pattern: '^opp-table(-[a-z0-9]+)?$' ++ ++ opp-shared: ++ description: ++ Indicates that device nodes using this OPP Table Node's phandle switch ++ their DVFS state together, i.e. they share clock/voltage/current lines. ++ Missing property means devices have independent clock/voltage/current ++ lines, but they share OPP tables. ++ type: boolean ++ ++patternProperties: ++ '^opp-?[0-9]+$': ++ type: object ++ description: ++ One or more OPP nodes describing voltage-current-frequency combinations. ++ Their name isn't significant but their phandle can be used to reference an ++ OPP. These are mandatory except for the case where the OPP table is ++ present only to indicate dependency between devices using the opp-shared ++ property. ++ ++ properties: ++ opp-hz: ++ description: ++ Frequency in Hz, expressed as a 64-bit big-endian integer. This is a ++ required property for all device nodes, unless another "required" ++ property to uniquely identify the OPP nodes exists. Devices like power ++ domains must have another (implementation dependent) property. ++ ++ opp-microvolt: ++ description: | ++ Voltage for the OPP ++ ++ A single regulator's voltage is specified with an array of size one or three. ++ Single entry is for target voltage and three entries are for ++ voltages. ++ ++ Entries for multiple regulators shall be provided in the same field separated ++ by angular brackets <>. The OPP binding doesn't provide any provisions to ++ relate the values to their power supplies or the order in which the supplies ++ need to be configured and that is left for the implementation specific ++ binding. ++ ++ Entries for all regulators shall be of the same size, i.e. either all use a ++ single value or triplets. ++ minItems: 1 ++ maxItems: 8 # Should be enough regulators ++ items: ++ minItems: 1 ++ maxItems: 3 ++ ++ opp-microamp: ++ description: | ++ The maximum current drawn by the device in microamperes considering ++ system specific parameters (such as transients, process, aging, ++ maximum operating temperature range etc.) as necessary. This may be ++ used to set the most efficient regulator operating mode. ++ ++ Should only be set if opp-microvolt or opp-microvolt- is set for ++ the OPP. ++ ++ Entries for multiple regulators shall be provided in the same field ++ separated by angular brackets <>. If current values aren't required ++ for a regulator, then it shall be filled with 0. If current values ++ aren't required for any of the regulators, then this field is not ++ required. The OPP binding doesn't provide any provisions to relate the ++ values to their power supplies or the order in which the supplies need ++ to be configured and that is left for the implementation specific ++ binding. ++ minItems: 1 ++ maxItems: 8 # Should be enough regulators ++ ++ opp-level: ++ description: ++ A value representing the performance level of the device. ++ $ref: /schemas/types.yaml#/definitions/uint32 ++ ++ opp-peak-kBps: ++ description: ++ Peak bandwidth in kilobytes per second, expressed as an array of ++ 32-bit big-endian integers. Each element of the array represents the ++ peak bandwidth value of each interconnect path. The number of elements ++ should match the number of interconnect paths. ++ minItems: 1 ++ maxItems: 32 # Should be enough ++ ++ opp-avg-kBps: ++ description: ++ Average bandwidth in kilobytes per second, expressed as an array ++ of 32-bit big-endian integers. Each element of the array represents the ++ average bandwidth value of each interconnect path. The number of elements ++ should match the number of interconnect paths. This property is only ++ meaningful in OPP tables where opp-peak-kBps is present. ++ minItems: 1 ++ maxItems: 32 # Should be enough ++ ++ clock-latency-ns: ++ description: ++ Specifies the maximum possible transition latency (in nanoseconds) for ++ switching to this OPP from any other OPP. ++ ++ turbo-mode: ++ description: ++ Marks the OPP to be used only for turbo modes. Turbo mode is available ++ on some platforms, where the device can run over its operating ++ frequency for a short duration of time limited by the device's power, ++ current and thermal limits. ++ type: boolean ++ ++ opp-suspend: ++ description: ++ Marks the OPP to be used during device suspend. If multiple OPPs in ++ the table have this, the OPP with highest opp-hz will be used. ++ type: boolean ++ ++ opp-supported-hw: ++ description: | ++ This property allows a platform to enable only a subset of the OPPs ++ from the larger set present in the OPP table, based on the current ++ version of the hardware (already known to the operating system). ++ ++ Each block present in the array of blocks in this property, represents ++ a sub-group of hardware versions supported by the OPP. i.e. , , etc. The OPP will be enabled if _any_ of these ++ sub-groups match the hardware's version. ++ ++ Each sub-group is a platform defined array representing the hierarchy ++ of hardware versions supported by the platform. For a platform with ++ three hierarchical levels of version (X.Y.Z), this field shall look ++ like ++ ++ opp-supported-hw = , , . ++ ++ Each level (eg. X1) in version hierarchy is represented by a 32 bit ++ value, one bit per version and so there can be maximum 32 versions per ++ level. Logical AND (&) operation is performed for each level with the ++ hardware's level version and a non-zero output for _all_ the levels in ++ a sub-group means the OPP is supported by hardware. A value of ++ 0xFFFFFFFF for each level in the sub-group will enable the OPP for all ++ versions for the hardware. ++ $ref: /schemas/types.yaml#/definitions/uint32-matrix ++ maxItems: 32 ++ items: ++ minItems: 1 ++ maxItems: 4 ++ ++ required-opps: ++ description: ++ This contains phandle to an OPP node in another device's OPP table. It ++ may contain an array of phandles, where each phandle points to an OPP ++ of a different device. It should not contain multiple phandles to the ++ OPP nodes in the same OPP table. This specifies the minimum required ++ OPP of the device(s), whose OPP's phandle is present in this property, ++ for the functioning of the current device at the current OPP (where ++ this property is present). ++ $ref: /schemas/types.yaml#/definitions/phandle-array ++ ++ patternProperties: ++ '^opp-microvolt-': ++ description: ++ Named opp-microvolt property. This is exactly similar to the above ++ opp-microvolt property, but allows multiple voltage ranges to be ++ provided for the same OPP. At runtime, the platform can pick a ++ and matching opp-microvolt- property will be enabled for all ++ OPPs. If the platform doesn't pick a specific or the ++ doesn't match with any opp-microvolt- properties, then ++ opp-microvolt property shall be used, if present. ++ $ref: /schemas/types.yaml#/definitions/uint32-matrix ++ minItems: 1 ++ maxItems: 8 # Should be enough regulators ++ items: ++ minItems: 1 ++ maxItems: 3 ++ ++ '^opp-microamp-': ++ description: ++ Named opp-microamp property. Similar to opp-microvolt- property, ++ but for microamp instead. ++ $ref: /schemas/types.yaml#/definitions/uint32-array ++ minItems: 1 ++ maxItems: 8 # Should be enough regulators ++ ++ dependencies: ++ opp-avg-kBps: [ opp-peak-kBps ] ++ ++required: ++ - compatible ++ ++additionalProperties: true ++ ++... +diff --git a/documentation/devicetree/bindings/opp/opp-v2.yaml b/documentation/devicetree/bindings/opp/opp-v2.yaml +new file mode 100644 +index 000000000..eaf8fba2c +--- /dev/null ++++ b/documentation/devicetree/bindings/opp/opp-v2.yaml +@@ -0,0 +1,475 @@ ++# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/opp/opp-v2.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: Generic OPP (Operating Performance Points) Bindings ++ ++maintainers: ++ - Viresh Kumar ++ ++allOf: ++ - $ref: opp-v2-base.yaml# ++ ++properties: ++ compatible: ++ const: operating-points-v2 ++ ++unevaluatedProperties: false ++ ++examples: ++ - | ++ /* ++ * Example 1: Single cluster Dual-core ARM cortex A9, switch DVFS states ++ * together. ++ */ ++ cpus { ++ #address-cells = <1>; ++ #size-cells = <0>; ++ ++ cpu@0 { ++ compatible = "arm,cortex-a9"; ++ device_type = "cpu"; ++ reg = <0>; ++ next-level-cache = <&L2>; ++ clocks = <&clk_controller 0>; ++ clock-names = "cpu"; ++ cpu-supply = <&cpu_supply0>; ++ operating-points-v2 = <&cpu0_opp_table0>; ++ }; ++ ++ cpu@1 { ++ compatible = "arm,cortex-a9"; ++ device_type = "cpu"; ++ reg = <1>; ++ next-level-cache = <&L2>; ++ clocks = <&clk_controller 0>; ++ clock-names = "cpu"; ++ cpu-supply = <&cpu_supply0>; ++ operating-points-v2 = <&cpu0_opp_table0>; ++ }; ++ }; ++ ++ cpu0_opp_table0: opp-table { ++ compatible = "operating-points-v2"; ++ opp-shared; ++ ++ opp-1000000000 { ++ opp-hz = /bits/ 64 <1000000000>; ++ opp-microvolt = <975000 970000 985000>; ++ opp-microamp = <70000>; ++ clock-latency-ns = <300000>; ++ opp-suspend; ++ }; ++ opp-1100000000 { ++ opp-hz = /bits/ 64 <1100000000>; ++ opp-microvolt = <1000000 980000 1010000>; ++ opp-microamp = <80000>; ++ clock-latency-ns = <310000>; ++ }; ++ opp-1200000000 { ++ opp-hz = /bits/ 64 <1200000000>; ++ opp-microvolt = <1025000>; ++ clock-latency-ns = <290000>; ++ turbo-mode; ++ }; ++ }; ++ ++ - | ++ /* ++ * Example 2: Single cluster, Quad-core Qualcom-krait, switches DVFS states ++ * independently. ++ */ ++ cpus { ++ #address-cells = <1>; ++ #size-cells = <0>; ++ ++ cpu@0 { ++ compatible = "qcom,krait"; ++ device_type = "cpu"; ++ reg = <0>; ++ next-level-cache = <&L2>; ++ clocks = <&clk_controller 0>; ++ clock-names = "cpu"; ++ cpu-supply = <&cpu_supply0>; ++ operating-points-v2 = <&cpu_opp_table>; ++ }; ++ ++ cpu@1 { ++ compatible = "qcom,krait"; ++ device_type = "cpu"; ++ reg = <1>; ++ next-level-cache = <&L2>; ++ clocks = <&clk_controller 1>; ++ clock-names = "cpu"; ++ cpu-supply = <&cpu_supply1>; ++ operating-points-v2 = <&cpu_opp_table>; ++ }; ++ ++ cpu@2 { ++ compatible = "qcom,krait"; ++ device_type = "cpu"; ++ reg = <2>; ++ next-level-cache = <&L2>; ++ clocks = <&clk_controller 2>; ++ clock-names = "cpu"; ++ cpu-supply = <&cpu_supply2>; ++ operating-points-v2 = <&cpu_opp_table>; ++ }; ++ ++ cpu@3 { ++ compatible = "qcom,krait"; ++ device_type = "cpu"; ++ reg = <3>; ++ next-level-cache = <&L2>; ++ clocks = <&clk_controller 3>; ++ clock-names = "cpu"; ++ cpu-supply = <&cpu_supply3>; ++ operating-points-v2 = <&cpu_opp_table>; ++ }; ++ }; ++ ++ cpu_opp_table: opp-table { ++ compatible = "operating-points-v2"; ++ ++ /* ++ * Missing opp-shared property means CPUs switch DVFS states ++ * independently. ++ */ ++ ++ opp-1000000000 { ++ opp-hz = /bits/ 64 <1000000000>; ++ opp-microvolt = <975000 970000 985000>; ++ opp-microamp = <70000>; ++ clock-latency-ns = <300000>; ++ opp-suspend; ++ }; ++ opp-1100000000 { ++ opp-hz = /bits/ 64 <1100000000>; ++ opp-microvolt = <1000000 980000 1010000>; ++ opp-microamp = <80000>; ++ clock-latency-ns = <310000>; ++ }; ++ opp-1200000000 { ++ opp-hz = /bits/ 64 <1200000000>; ++ opp-microvolt = <1025000>; ++ opp-microamp = <90000>; ++ lock-latency-ns = <290000>; ++ turbo-mode; ++ }; ++ }; ++ ++ - | ++ /* ++ * Example 3: Dual-cluster, Dual-core per cluster. CPUs within a cluster switch ++ * DVFS state together. ++ */ ++ cpus { ++ #address-cells = <1>; ++ #size-cells = <0>; ++ ++ cpu@0 { ++ compatible = "arm,cortex-a7"; ++ device_type = "cpu"; ++ reg = <0>; ++ next-level-cache = <&L2>; ++ clocks = <&clk_controller 0>; ++ clock-names = "cpu"; ++ cpu-supply = <&cpu_supply0>; ++ operating-points-v2 = <&cluster0_opp>; ++ }; ++ ++ cpu@1 { ++ compatible = "arm,cortex-a7"; ++ device_type = "cpu"; ++ reg = <1>; ++ next-level-cache = <&L2>; ++ clocks = <&clk_controller 0>; ++ clock-names = "cpu"; ++ cpu-supply = <&cpu_supply0>; ++ operating-points-v2 = <&cluster0_opp>; ++ }; ++ ++ cpu@100 { ++ compatible = "arm,cortex-a15"; ++ device_type = "cpu"; ++ reg = <100>; ++ next-level-cache = <&L2>; ++ clocks = <&clk_controller 1>; ++ clock-names = "cpu"; ++ cpu-supply = <&cpu_supply1>; ++ operating-points-v2 = <&cluster1_opp>; ++ }; ++ ++ cpu@101 { ++ compatible = "arm,cortex-a15"; ++ device_type = "cpu"; ++ reg = <101>; ++ next-level-cache = <&L2>; ++ clocks = <&clk_controller 1>; ++ clock-names = "cpu"; ++ cpu-supply = <&cpu_supply1>; ++ operating-points-v2 = <&cluster1_opp>; ++ }; ++ }; ++ ++ cluster0_opp: opp-table-0 { ++ compatible = "operating-points-v2"; ++ opp-shared; ++ ++ opp-1000000000 { ++ opp-hz = /bits/ 64 <1000000000>; ++ opp-microvolt = <975000 970000 985000>; ++ opp-microamp = <70000>; ++ clock-latency-ns = <300000>; ++ opp-suspend; ++ }; ++ opp-1100000000 { ++ opp-hz = /bits/ 64 <1100000000>; ++ opp-microvolt = <1000000 980000 1010000>; ++ opp-microamp = <80000>; ++ clock-latency-ns = <310000>; ++ }; ++ opp-1200000000 { ++ opp-hz = /bits/ 64 <1200000000>; ++ opp-microvolt = <1025000>; ++ opp-microamp = <90000>; ++ clock-latency-ns = <290000>; ++ turbo-mode; ++ }; ++ }; ++ ++ cluster1_opp: opp-table-1 { ++ compatible = "operating-points-v2"; ++ opp-shared; ++ ++ opp-1300000000 { ++ opp-hz = /bits/ 64 <1300000000>; ++ opp-microvolt = <1050000 1045000 1055000>; ++ opp-microamp = <95000>; ++ clock-latency-ns = <400000>; ++ opp-suspend; ++ }; ++ opp-1400000000 { ++ opp-hz = /bits/ 64 <1400000000>; ++ opp-microvolt = <1075000>; ++ opp-microamp = <100000>; ++ clock-latency-ns = <400000>; ++ }; ++ opp-1500000000 { ++ opp-hz = /bits/ 64 <1500000000>; ++ opp-microvolt = <1100000 1010000 1110000>; ++ opp-microamp = <95000>; ++ clock-latency-ns = <400000>; ++ turbo-mode; ++ }; ++ }; ++ ++ - | ++ /* Example 4: Handling multiple regulators */ ++ cpus { ++ #address-cells = <1>; ++ #size-cells = <0>; ++ ++ cpu@0 { ++ compatible = "foo,cpu-type"; ++ device_type = "cpu"; ++ reg = <0>; ++ ++ vcc0-supply = <&cpu_supply0>; ++ vcc1-supply = <&cpu_supply1>; ++ vcc2-supply = <&cpu_supply2>; ++ operating-points-v2 = <&cpu0_opp_table4>; ++ }; ++ }; ++ ++ cpu0_opp_table4: opp-table-0 { ++ compatible = "operating-points-v2"; ++ opp-shared; ++ ++ opp-1000000000 { ++ opp-hz = /bits/ 64 <1000000000>; ++ opp-microvolt = <970000>, /* Supply 0 */ ++ <960000>, /* Supply 1 */ ++ <960000>; /* Supply 2 */ ++ opp-microamp = <70000>, /* Supply 0 */ ++ <70000>, /* Supply 1 */ ++ <70000>; /* Supply 2 */ ++ clock-latency-ns = <300000>; ++ }; ++ ++ /* OR */ ++ ++ opp-1000000001 { ++ opp-hz = /bits/ 64 <1000000001>; ++ opp-microvolt = <975000 970000 985000>, /* Supply 0 */ ++ <965000 960000 975000>, /* Supply 1 */ ++ <965000 960000 975000>; /* Supply 2 */ ++ opp-microamp = <70000>, /* Supply 0 */ ++ <70000>, /* Supply 1 */ ++ <70000>; /* Supply 2 */ ++ clock-latency-ns = <300000>; ++ }; ++ ++ /* OR */ ++ ++ opp-1000000002 { ++ opp-hz = /bits/ 64 <1000000002>; ++ opp-microvolt = <975000 970000 985000>, /* Supply 0 */ ++ <965000 960000 975000>, /* Supply 1 */ ++ <965000 960000 975000>; /* Supply 2 */ ++ opp-microamp = <70000>, /* Supply 0 */ ++ <0>, /* Supply 1 doesn't need this */ ++ <70000>; /* Supply 2 */ ++ clock-latency-ns = <300000>; ++ }; ++ }; ++ ++ - | ++ /* ++ * Example 5: opp-supported-hw ++ * (example: three level hierarchy of versions: cuts, substrate and process) ++ */ ++ cpus { ++ #address-cells = <1>; ++ #size-cells = <0>; ++ ++ cpu@0 { ++ compatible = "arm,cortex-a7"; ++ device_type = "cpu"; ++ reg = <0>; ++ cpu-supply = <&cpu_supply>; ++ operating-points-v2 = <&cpu0_opp_table_slow>; ++ }; ++ }; ++ ++ cpu0_opp_table_slow: opp-table { ++ compatible = "operating-points-v2"; ++ opp-shared; ++ ++ opp-600000000 { ++ /* ++ * Supports all substrate and process versions for 0xF ++ * cuts, i.e. only first four cuts. ++ */ ++ opp-supported-hw = <0xF 0xFFFFFFFF 0xFFFFFFFF>; ++ opp-hz = /bits/ 64 <600000000>; ++ }; ++ ++ opp-800000000 { ++ /* ++ * Supports: ++ * - cuts: only one, 6th cut (represented by 6th bit). ++ * - substrate: supports 16 different substrate versions ++ * - process: supports 9 different process versions ++ */ ++ opp-supported-hw = <0x20 0xff0000ff 0x0000f4f0>; ++ opp-hz = /bits/ 64 <800000000>; ++ }; ++ ++ opp-900000000 { ++ /* ++ * Supports: ++ * - All cuts and substrate where process version is 0x2. ++ * - All cuts and process where substrate version is 0x2. ++ */ ++ opp-supported-hw = <0xFFFFFFFF 0xFFFFFFFF 0x02>, ++ <0xFFFFFFFF 0x01 0xFFFFFFFF>; ++ opp-hz = /bits/ 64 <900000000>; ++ }; ++ }; ++ ++ - | ++ /* ++ * Example 6: opp-microvolt-, opp-microamp-: ++ * (example: device with two possible microvolt ranges: slow and fast) ++ */ ++ cpus { ++ #address-cells = <1>; ++ #size-cells = <0>; ++ ++ cpu@0 { ++ compatible = "arm,cortex-a7"; ++ device_type = "cpu"; ++ reg = <0>; ++ operating-points-v2 = <&cpu0_opp_table6>; ++ }; ++ }; ++ ++ cpu0_opp_table6: opp-table-0 { ++ compatible = "operating-points-v2"; ++ opp-shared; ++ ++ opp-1000000000 { ++ opp-hz = /bits/ 64 <1000000000>; ++ opp-microvolt-slow = <915000 900000 925000>; ++ opp-microvolt-fast = <975000 970000 985000>; ++ opp-microamp-slow = <70000>; ++ opp-microamp-fast = <71000>; ++ }; ++ ++ opp-1200000000 { ++ opp-hz = /bits/ 64 <1200000000>; ++ opp-microvolt-slow = <915000 900000 925000>, /* Supply vcc0 */ ++ <925000 910000 935000>; /* Supply vcc1 */ ++ opp-microvolt-fast = <975000 970000 985000>, /* Supply vcc0 */ ++ <965000 960000 975000>; /* Supply vcc1 */ ++ opp-microamp = <70000>; /* Will be used for both slow/fast */ ++ }; ++ }; ++ ++ - | ++ /* ++ * Example 7: Single cluster Quad-core ARM cortex A53, OPP points from firmware, ++ * distinct clock controls but two sets of clock/voltage/current lines. ++ */ ++ cpus { ++ #address-cells = <2>; ++ #size-cells = <0>; ++ ++ cpu@0 { ++ compatible = "arm,cortex-a53"; ++ device_type = "cpu"; ++ reg = <0x0 0x100>; ++ next-level-cache = <&A53_L2>; ++ clocks = <&dvfs_controller 0>; ++ operating-points-v2 = <&cpu_opp0_table>; ++ }; ++ cpu@1 { ++ compatible = "arm,cortex-a53"; ++ device_type = "cpu"; ++ reg = <0x0 0x101>; ++ next-level-cache = <&A53_L2>; ++ clocks = <&dvfs_controller 1>; ++ operating-points-v2 = <&cpu_opp0_table>; ++ }; ++ cpu@2 { ++ compatible = "arm,cortex-a53"; ++ device_type = "cpu"; ++ reg = <0x0 0x102>; ++ next-level-cache = <&A53_L2>; ++ clocks = <&dvfs_controller 2>; ++ operating-points-v2 = <&cpu_opp1_table>; ++ }; ++ cpu@3 { ++ compatible = "arm,cortex-a53"; ++ device_type = "cpu"; ++ reg = <0x0 0x103>; ++ next-level-cache = <&A53_L2>; ++ clocks = <&dvfs_controller 3>; ++ operating-points-v2 = <&cpu_opp1_table>; ++ }; ++ ++ }; ++ ++ cpu_opp0_table: opp-table-0 { ++ compatible = "operating-points-v2"; ++ opp-shared; ++ }; ++ ++ cpu_opp1_table: opp-table-1 { ++ compatible = "operating-points-v2"; ++ opp-shared; ++ }; ++... +diff --git a/documentation/devicetree/bindings/pinctrl/st,stm32-pinctrl.yaml b/documentation/devicetree/bindings/pinctrl/st,stm32-pinctrl.yaml +new file mode 100644 +index 000000000..5659dd02d +--- /dev/null ++++ b/documentation/devicetree/bindings/pinctrl/st,stm32-pinctrl.yaml +@@ -0,0 +1,305 @@ ++# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) ++# Copyright (C) STMicroelectronics 2019. ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/pinctrl/st,stm32-pinctrl.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: STM32 GPIO and Pin Mux/Config controller ++ ++maintainers: ++ - Alexandre TORGUE ++ ++description: | ++ STMicroelectronics's STM32 MCUs intregrate a GPIO and Pin mux/config hardware ++ controller. It controls the input/output settings on the available pins and ++ also provides ability to multiplex and configure the output of various ++ on-chip controllers onto these pads. ++ ++properties: ++ compatible: ++ enum: ++ - st,stm32f429-pinctrl ++ - st,stm32f469-pinctrl ++ - st,stm32f746-pinctrl ++ - st,stm32f769-pinctrl ++ - st,stm32h743-pinctrl ++ - st,stm32mp135-pinctrl ++ - st,stm32mp157-pinctrl ++ - st,stm32mp157-z-pinctrl ++ ++ '#address-cells': ++ const: 1 ++ '#size-cells': ++ const: 1 ++ ++ ranges: true ++ pins-are-numbered: true ++ hwlocks: true ++ ++ interrupts: ++ maxItems: 1 ++ ++ st,syscfg: ++ description: Should be phandle/offset/mask ++ - Phandle to the syscon node which includes IRQ mux selection. ++ - The offset of the IRQ mux selection register. ++ - The field mask of IRQ mux, needed if different of 0xf. ++ $ref: "/schemas/types.yaml#/definitions/phandle-array" ++ ++ st,package: ++ description: ++ Indicates the SOC package used. ++ More details in include/dt-bindings/pinctrl/stm32-pinfunc.h ++ $ref: /schemas/types.yaml#/definitions/uint32 ++ enum: [1, 2, 4, 8] ++ ++patternProperties: ++ '^gpio@[0-9a-f]*$': ++ type: object ++ properties: ++ gpio-controller: true ++ '#gpio-cells': ++ const: 2 ++ ++ reg: ++ maxItems: 1 ++ clocks: ++ maxItems: 1 ++ reset: ++ minItems: 1 ++ maxItems: 1 ++ gpio-ranges: ++ minItems: 1 ++ maxItems: 16 ++ ngpios: ++ description: ++ Number of available gpios in a bank. ++ minimum: 1 ++ maximum: 16 ++ ++ st,bank-name: ++ description: ++ Should be a name string for this bank as specified in the datasheet. ++ $ref: "/schemas/types.yaml#/definitions/string" ++ enum: ++ - GPIOA ++ - GPIOB ++ - GPIOC ++ - GPIOD ++ - GPIOE ++ - GPIOF ++ - GPIOG ++ - GPIOH ++ - GPIOI ++ - GPIOJ ++ - GPIOK ++ - GPIOZ ++ ++ st,bank-ioport: ++ description: ++ Should correspond to the EXTI IOport selection (EXTI line used ++ to select GPIOs as interrupts). ++ $ref: "/schemas/types.yaml#/definitions/uint32" ++ minimum: 0 ++ maximum: 11 ++ ++ st,protreg: ++ description: ++ Defines the secure and access pin configuration to apply to the bank ++ It can use the TZPROT macro to define the associated bit. ++ $ref: "/schemas/types.yaml#/definitions/uint32" ++ minItems: 1 ++ maxItems: 1 ++ ++ required: ++ - gpio-controller ++ - '#gpio-cells' ++ - reg ++ - clocks ++ - st,bank-name ++ ++ '-[0-9]*$': ++ type: object ++ patternProperties: ++ '^pins': ++ type: object ++ description: | ++ A pinctrl node should contain at least one subnode representing the ++ pinctrl group available on the machine. Each subnode will list the ++ pins it needs, and how they should be configured, with regard to muxer ++ configuration, pullups, drive, output high/low and output speed. ++ properties: ++ pinmux: ++ $ref: "/schemas/types.yaml#/definitions/uint32-array" ++ description: | ++ Integer array, represents gpio pin number and mux setting. ++ Supported pin number and mux varies for different SoCs, and are ++ defined in dt-bindings/pinctrl/-pinfunc.h directly. ++ These defines are calculated as: ((port * 16 + line) << 8) | function ++ With: ++ - port: The gpio port index (PA = 0, PB = 1, ..., PK = 11) ++ - line: The line offset within the port (PA0 = 0, PA1 = 1, ..., PA15 = 15) ++ - function: The function number, can be: ++ * 0 : GPIO ++ * 1 : Alternate Function 0 ++ * 2 : Alternate Function 1 ++ * 3 : Alternate Function 2 ++ * ... ++ * 16 : Alternate Function 15 ++ * 17 : Analog ++ * 18 : Reserved ++ To simplify the usage, macro is available to generate "pinmux" field. ++ This macro is available here: ++ - include/dt-bindings/pinctrl/stm32-pinfunc.h ++ Setting the pinmux's function to the Reserved (RSVD) value is used to inform ++ the driver that it shall not apply the mux setting. This can be used to ++ reserve some pins, for example to a co-processor not running Linux. ++ Some examples of using macro: ++ /* GPIO A9 set as alernate function 2 */ ++ ... { ++ pinmux = ; ++ }; ++ /* GPIO A9 set as GPIO */ ++ ... { ++ pinmux = ; ++ }; ++ /* GPIO A9 set as analog */ ++ ... { ++ pinmux = ; ++ }; ++ /* GPIO A9 reserved for co-processor */ ++ ... { ++ pinmux = ; ++ }; ++ ++ bias-disable: ++ type: boolean ++ bias-pull-down: ++ type: boolean ++ bias-pull-up: ++ type: boolean ++ drive-push-pull: ++ type: boolean ++ drive-open-drain: ++ type: boolean ++ output-low: ++ type: boolean ++ output-high: ++ type: boolean ++ slew-rate: ++ description: | ++ 0: Low speed ++ 1: Medium speed ++ 2: Fast speed ++ 3: High speed ++ $ref: /schemas/types.yaml#/definitions/uint32 ++ enum: [0, 1, 2, 3] ++ ++ required: ++ - pinmux ++ ++required: ++ - compatible ++ - '#address-cells' ++ - '#size-cells' ++ - ranges ++ - pins-are-numbered ++ ++additionalProperties: false ++ ++examples: ++ - | ++ #include ++ #include ++ //Example 1 ++ pinctrl@40020000 { ++ #address-cells = <1>; ++ #size-cells = <1>; ++ compatible = "st,stm32f429-pinctrl"; ++ ranges = <0 0x40020000 0x3000>; ++ pins-are-numbered; ++ ++ gpioa: gpio@0 { ++ gpio-controller; ++ #gpio-cells = <2>; ++ reg = <0x0 0x400>; ++ resets = <&reset_ahb1 0>; ++ clocks = <&rcc 0 STM32F4_AHB1_CLOCK(GPIOA)>; ++ st,bank-name = "GPIOA"; ++ }; ++ }; ++ ++ //Example 2 (using gpio-ranges) ++ pinctrl@50020000 { ++ #address-cells = <1>; ++ #size-cells = <1>; ++ compatible = "st,stm32f429-pinctrl"; ++ ranges = <0 0x50020000 0x3000>; ++ pins-are-numbered; ++ ++ gpiob: gpio@1000 { ++ gpio-controller; ++ #gpio-cells = <2>; ++ reg = <0x1000 0x400>; ++ resets = <&reset_ahb1 0>; ++ clocks = <&rcc 0 STM32F4_AHB1_CLOCK(GPIOB)>; ++ st,bank-name = "GPIOB"; ++ gpio-ranges = <&pinctrl 0 0 16>; ++ }; ++ ++ gpioc: gpio@2000 { ++ gpio-controller; ++ #gpio-cells = <2>; ++ reg = <0x2000 0x400>; ++ resets = <&reset_ahb1 0>; ++ clocks = <&rcc 0 STM32F4_AHB1_CLOCK(GPIOC)>; ++ st,bank-name = "GPIOC"; ++ ngpios = <5>; ++ gpio-ranges = <&pinctrl 0 16 3>, ++ <&pinctrl 14 30 2>; ++ }; ++ }; ++ ++ //Example 3 pin groups ++ pinctrl { ++ usart1_pins_a: usart1-0 { ++ pins1 { ++ pinmux = ; ++ bias-disable; ++ drive-push-pull; ++ slew-rate = <0>; ++ }; ++ pins2 { ++ pinmux = ; ++ bias-disable; ++ }; ++ }; ++ }; ++ ++ usart1 { ++ pinctrl-0 = <&usart1_pins_a>; ++ pinctrl-names = "default"; ++ }; ++ ++ //Example 4 (Set gpio bank B with PIN 0 and 4 secure) ++ #include ++ pinctrl@50020000 { ++ #address-cells = <1>; ++ #size-cells = <1>; ++ compatible = "st,stm32mp135-pinctrl"; ++ ranges = <0 0x50002000 0x8400>; ++ pins-are-numbered; ++ ++ gpiob: gpio@1000 { ++ gpio-controller; ++ #gpio-cells = <2>; ++ reg = <0x1000 0x400>; ++ resets = <&reset_ahb1 0>; ++ clocks = <&rcc 0 STM32F4_AHB1_CLOCK(GPIOB)>; ++ st,bank-name = "GPIOB"; ++ gpio-ranges = <&pinctrl 0 0 16>; ++ st,protreg = <(TZPROT(0) | TZPROT(4))>; ++ }; ++ }; ++... +diff --git a/documentation/devicetree/bindings/power/power-domain.yaml b/documentation/devicetree/bindings/power/power-domain.yaml +new file mode 100644 +index 000000000..3143ed9a3 +--- /dev/null ++++ b/documentation/devicetree/bindings/power/power-domain.yaml +@@ -0,0 +1,133 @@ ++# SPDX-License-Identifier: GPL-2.0 ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/power/power-domain.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: Generic PM domains ++ ++maintainers: ++ - Rafael J. Wysocki ++ - Kevin Hilman ++ - Ulf Hansson ++ ++description: |+ ++ System on chip designs are often divided into multiple PM domains that can be ++ used for power gating of selected IP blocks for power saving by reduced leakage ++ current. ++ ++ This device tree binding can be used to bind PM domain consumer devices with ++ their PM domains provided by PM domain providers. A PM domain provider can be ++ represented by any node in the device tree and can provide one or more PM ++ domains. A consumer node can refer to the provider by a phandle and a set of ++ phandle arguments (so called PM domain specifiers) of length specified by the ++ \#power-domain-cells property in the PM domain provider node. ++ ++properties: ++ $nodename: ++ pattern: "^(power-controller|power-domain)([@-].*)?$" ++ ++ domain-idle-states: ++ $ref: /schemas/types.yaml#/definitions/phandle-array ++ description: | ++ Phandles of idle states that defines the available states for the ++ power-domain provider. The idle state definitions are compatible with the ++ domain-idle-state bindings, specified in ./domain-idle-state.yaml. ++ ++ Note that, the domain-idle-state property reflects the idle states of this ++ PM domain and not the idle states of the devices or sub-domains in the PM ++ domain. Devices and sub-domains have their own idle states independent of ++ the parent domain's idle states. In the absence of this property, the ++ domain would be considered as capable of being powered-on or powered-off. ++ ++ operating-points-v2: ++ $ref: /schemas/types.yaml#/definitions/phandle-array ++ description: ++ Phandles to the OPP tables of power domains provided by a power domain ++ provider. If the provider provides a single power domain only or all ++ the power domains provided by the provider have identical OPP tables, ++ then this shall contain a single phandle. Refer to ../opp/opp-v2-base.yaml ++ for more information. ++ ++ "#power-domain-cells": ++ description: ++ Number of cells in a PM domain specifier. Typically 0 for nodes ++ representing a single PM domain and 1 for nodes providing multiple PM ++ domains (e.g. power controllers), but can be any value as specified ++ by device tree binding documentation of particular provider. ++ ++ power-domains: ++ description: ++ A phandle and PM domain specifier as defined by bindings of the power ++ controller specified by phandle. Some power domains might be powered ++ from another power domain (or have other hardware specific ++ dependencies). For representing such dependency a standard PM domain ++ consumer binding is used. When provided, all domains created ++ by the given provider should be subdomains of the domain specified ++ by this binding. ++ ++required: ++ - "#power-domain-cells" ++ ++additionalProperties: true ++ ++examples: ++ - | ++ power: power-controller@12340000 { ++ compatible = "foo,power-controller"; ++ reg = <0x12340000 0x1000>; ++ #power-domain-cells = <1>; ++ }; ++ ++ // The node above defines a power controller that is a PM domain provider and ++ // expects one cell as its phandle argument. ++ ++ - | ++ parent2: power-controller@12340000 { ++ compatible = "foo,power-controller"; ++ reg = <0x12340000 0x1000>; ++ #power-domain-cells = <1>; ++ }; ++ ++ child2: power-controller@12341000 { ++ compatible = "foo,power-controller"; ++ reg = <0x12341000 0x1000>; ++ power-domains = <&parent2 0>; ++ #power-domain-cells = <1>; ++ }; ++ ++ // The nodes above define two power controllers: 'parent' and 'child'. ++ // Domains created by the 'child' power controller are subdomains of '0' power ++ // domain provided by the 'parent' power controller. ++ ++ - | ++ parent3: power-controller@12340000 { ++ compatible = "foo,power-controller"; ++ reg = <0x12340000 0x1000>; ++ #power-domain-cells = <0>; ++ domain-idle-states = <&DOMAIN_RET>, <&DOMAIN_PWR_DN>; ++ }; ++ ++ child3: power-controller@12341000 { ++ compatible = "foo,power-controller"; ++ reg = <0x12341000 0x1000>; ++ power-domains = <&parent3>; ++ #power-domain-cells = <0>; ++ domain-idle-states = <&DOMAIN_PWR_DN>; ++ }; ++ ++ domain-idle-states { ++ DOMAIN_RET: domain-retention { ++ compatible = "domain-idle-state"; ++ entry-latency-us = <1000>; ++ exit-latency-us = <2000>; ++ min-residency-us = <10000>; ++ }; ++ ++ DOMAIN_PWR_DN: domain-pwr-dn { ++ compatible = "domain-idle-state"; ++ entry-latency-us = <5000>; ++ exit-latency-us = <8000>; ++ min-residency-us = <7000>; ++ }; ++ }; +diff --git a/documentation/devicetree/bindings/regulator/fixed-regulator.yaml b/documentation/devicetree/bindings/regulator/fixed-regulator.yaml +new file mode 100644 +index 000000000..9b131c6fa +--- /dev/null ++++ b/documentation/devicetree/bindings/regulator/fixed-regulator.yaml +@@ -0,0 +1,139 @@ ++# SPDX-License-Identifier: GPL-2.0 ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/regulator/fixed-regulator.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: Fixed Voltage regulators ++ ++maintainers: ++ - Liam Girdwood ++ - Mark Brown ++ ++description: ++ Any property defined as part of the core regulator binding, defined in ++ regulator.yaml, can also be used. However a fixed voltage regulator is ++ expected to have the regulator-min-microvolt and regulator-max-microvolt ++ to be the same. ++ ++allOf: ++ - $ref: "regulator.yaml#" ++ ++if: ++ properties: ++ compatible: ++ contains: ++ const: regulator-fixed-clock ++ required: ++ - clocks ++else: ++ if: ++ properties: ++ compatible: ++ contains: ++ const: regulator-fixed-domain ++ required: ++ - power-domains ++ - required-opps ++ ++properties: ++ compatible: ++ enum: ++ - regulator-fixed ++ - regulator-fixed-clock ++ - regulator-fixed-domain ++ ++ regulator-name: true ++ ++ gpio: ++ description: gpio to use for enable control ++ maxItems: 1 ++ ++ clocks: ++ description: ++ clock to use for enable control. This binding is only available if ++ the compatible is chosen to regulator-fixed-clock. The clock binding ++ is mandatory if compatible is chosen to regulator-fixed-clock. ++ maxItems: 1 ++ ++ power-domains: ++ deprecated: true ++ description: ++ Power domain to use for enable control. This binding is only ++ available if the compatible is chosen to regulator-fixed-domain. ++ maxItems: 1 ++ ++ required-opps: ++ deprecated: true ++ description: ++ Performance state to use for enable control. This binding is only ++ available if the compatible is chosen to regulator-fixed-domain. The ++ power-domain binding is mandatory if compatible is chosen to ++ regulator-fixed-domain. ++ maxItems: 1 ++ ++ startup-delay-us: ++ description: startup time in microseconds ++ ++ off-on-delay-us: ++ description: off delay time in microseconds ++ ++ enable-active-high: ++ description: ++ Polarity of GPIO is Active high. If this property is missing, ++ the default assumed is Active low. ++ type: boolean ++ ++ gpio-open-drain: ++ description: ++ GPIO is open drain type. If this property is missing then default ++ assumption is false. ++ type: boolean ++ ++ vin-supply: ++ description: Input supply phandle. ++ ++required: ++ - compatible ++ - regulator-name ++ ++unevaluatedProperties: false ++ ++examples: ++ - | ++ reg_1v8: regulator-1v8 { ++ compatible = "regulator-fixed"; ++ regulator-name = "1v8"; ++ regulator-min-microvolt = <1800000>; ++ regulator-max-microvolt = <1800000>; ++ gpio = <&gpio1 16 0>; ++ startup-delay-us = <70000>; ++ enable-active-high; ++ regulator-boot-on; ++ gpio-open-drain; ++ vin-supply = <&parent_reg>; ++ }; ++ reg_1v8_clk: regulator-1v8-clk { ++ compatible = "regulator-fixed-clock"; ++ regulator-name = "1v8"; ++ regulator-min-microvolt = <1800000>; ++ regulator-max-microvolt = <1800000>; ++ clocks = <&clock1>; ++ startup-delay-us = <70000>; ++ enable-active-high; ++ regulator-boot-on; ++ vin-supply = <&parent_reg>; ++ }; ++ reg_1v8_domain: regulator-1v8-domain { ++ compatible = "regulator-fixed-domain"; ++ regulator-name = "1v8"; ++ regulator-min-microvolt = <1800000>; ++ regulator-max-microvolt = <1800000>; ++ power-domains = <&domain1>; ++ required-opps = <&domain1_state1>; ++ startup-delay-us = <70000>; ++ enable-active-high; ++ regulator-boot-on; ++ vin-supply = <&parent_reg>; ++ }; ++... +diff --git a/documentation/devicetree/bindings/regulator/regulator.yaml b/documentation/devicetree/bindings/regulator/regulator.yaml +new file mode 100644 +index 000000000..a6ae9ecae +--- /dev/null ++++ b/documentation/devicetree/bindings/regulator/regulator.yaml +@@ -0,0 +1,288 @@ ++# SPDX-License-Identifier: GPL-2.0 ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/regulator/regulator.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: Voltage/Current Regulators ++ ++maintainers: ++ - Liam Girdwood ++ - Mark Brown ++ ++properties: ++ regulator-name: ++ description: A string used as a descriptive name for regulator outputs ++ $ref: "/schemas/types.yaml#/definitions/string" ++ ++ regulator-min-microvolt: ++ description: smallest voltage consumers may set ++ ++ regulator-max-microvolt: ++ description: largest voltage consumers may set ++ ++ regulator-microvolt-offset: ++ description: Offset applied to voltages to compensate for voltage drops ++ ++ regulator-min-microamp: ++ description: smallest current consumers may set ++ ++ regulator-max-microamp: ++ description: largest current consumers may set ++ ++ regulator-input-current-limit-microamp: ++ description: maximum input current regulator allows ++ ++ regulator-always-on: ++ description: boolean, regulator should never be disabled ++ type: boolean ++ ++ regulator-boot-on: ++ description: bootloader/firmware enabled regulator. ++ It's expected that this regulator was left on by the bootloader. ++ If the bootloader didn't leave it on then OS should turn it on ++ at boot but shouldn't prevent it from being turned off later. ++ This property is intended to only be used for regulators where ++ software cannot read the state of the regulator. ++ type: boolean ++ ++ regulator-allow-bypass: ++ description: allow the regulator to go into bypass mode ++ type: boolean ++ ++ regulator-allow-set-load: ++ description: allow the regulator performance level to be configured ++ type: boolean ++ ++ regulator-ramp-delay: ++ description: ramp delay for regulator(in uV/us) For hardware which supports ++ disabling ramp rate, it should be explicitly initialised to zero (regulator-ramp-delay ++ = <0>) for disabling ramp delay. ++ $ref: "/schemas/types.yaml#/definitions/uint32" ++ ++ regulator-enable-ramp-delay: ++ description: The time taken, in microseconds, for the supply rail to ++ reach the target voltage, plus/minus whatever tolerance the board ++ design requires. This property describes the total system ramp time ++ required due to the combination of internal ramping of the regulator ++ itself, and board design issues such as trace capacitance and load ++ on the supply. ++ $ref: "/schemas/types.yaml#/definitions/uint32" ++ ++ regulator-settling-time-us: ++ description: Settling time, in microseconds, for voltage change if regulator ++ have the constant time for any level voltage change. This is useful ++ when regulator have exponential voltage change. ++ ++ regulator-settling-time-up-us: ++ description: Settling time, in microseconds, for voltage increase if ++ the regulator needs a constant time to settle after voltage increases ++ of any level. This is useful for regulators with exponential voltage ++ changes. ++ ++ regulator-settling-time-down-us: ++ description: Settling time, in microseconds, for voltage decrease if ++ the regulator needs a constant time to settle after voltage decreases ++ of any level. This is useful for regulators with exponential voltage ++ changes. ++ ++ regulator-soft-start: ++ description: Enable soft start so that voltage ramps slowly ++ type: boolean ++ ++ regulator-initial-mode: ++ description: initial operating mode. The set of possible operating modes ++ depends on the capabilities of every hardware so each device binding ++ documentation explains which values the regulator supports. ++ $ref: "/schemas/types.yaml#/definitions/uint32" ++ ++ regulator-allowed-modes: ++ description: list of operating modes that software is allowed to configure ++ for the regulator at run-time. Elements may be specified in any order. ++ The set of possible operating modes depends on the capabilities of ++ every hardware so each device binding document explains which values ++ the regulator supports. ++ $ref: "/schemas/types.yaml#/definitions/uint32-array" ++ ++ regulator-system-load: ++ description: Load in uA present on regulator that is not captured by ++ any consumer request. ++ $ref: "/schemas/types.yaml#/definitions/uint32" ++ ++ regulator-pull-down: ++ description: Enable pull down resistor when the regulator is disabled. ++ type: boolean ++ ++ regulator-over-current-protection: ++ description: Enable over current protection. ++ type: boolean ++ ++ regulator-oc-protection-microamp: ++ description: Set over current protection limit. This is a limit where ++ hardware performs emergency shutdown. Zero can be passed to disable ++ protection and value '1' indicates that protection should be enabled but ++ limit setting can be omitted. ++ ++ regulator-oc-error-microamp: ++ description: Set over current error limit. This is a limit where part of ++ the hardware propably is malfunctional and damage prevention is requested. ++ Zero can be passed to disable error detection and value '1' indicates ++ that detection should be enabled but limit setting can be omitted. ++ ++ regulator-oc-warn-microamp: ++ description: Set over current warning limit. This is a limit where hardware ++ is assumed still to be functional but approaching limit where it gets ++ damaged. Recovery actions should be initiated. Zero can be passed to ++ disable detection and value '1' indicates that detection should ++ be enabled but limit setting can be omitted. ++ ++ regulator-ov-protection-microvolt: ++ description: Set over voltage protection limit. This is a limit where ++ hardware performs emergency shutdown. Zero can be passed to disable ++ protection and value '1' indicates that protection should be enabled but ++ limit setting can be omitted. Limit is given as microvolt offset from ++ voltage set to regulator. ++ ++ regulator-ov-error-microvolt: ++ description: Set over voltage error limit. This is a limit where part of ++ the hardware propably is malfunctional and damage prevention is requested ++ Zero can be passed to disable error detection and value '1' indicates ++ that detection should be enabled but limit setting can be omitted. Limit ++ is given as microvolt offset from voltage set to regulator. ++ ++ regulator-ov-warn-microvolt: ++ description: Set over voltage warning limit. This is a limit where hardware ++ is assumed still to be functional but approaching limit where it gets ++ damaged. Recovery actions should be initiated. Zero can be passed to ++ disable detection and value '1' indicates that detection should ++ be enabled but limit setting can be omitted. Limit is given as microvolt ++ offset from voltage set to regulator. ++ ++ regulator-uv-protection-microvolt: ++ description: Set over under voltage protection limit. This is a limit where ++ hardware performs emergency shutdown. Zero can be passed to disable ++ protection and value '1' indicates that protection should be enabled but ++ limit setting can be omitted. Limit is given as microvolt offset from ++ voltage set to regulator. ++ ++ regulator-uv-error-microvolt: ++ description: Set under voltage error limit. This is a limit where part of ++ the hardware propably is malfunctional and damage prevention is requested ++ Zero can be passed to disable error detection and value '1' indicates ++ that detection should be enabled but limit setting can be omitted. Limit ++ is given as microvolt offset from voltage set to regulator. ++ ++ regulator-uv-warn-microvolt: ++ description: Set over under voltage warning limit. This is a limit where ++ hardware is assumed still to be functional but approaching limit where ++ it gets damaged. Recovery actions should be initiated. Zero can be passed ++ to disable detection and value '1' indicates that detection should ++ be enabled but limit setting can be omitted. Limit is given as microvolt ++ offset from voltage set to regulator. ++ ++ regulator-temp-protection-kelvin: ++ description: Set over temperature protection limit. This is a limit where ++ hardware performs emergency shutdown. Zero can be passed to disable ++ protection and value '1' indicates that protection should be enabled but ++ limit setting can be omitted. ++ ++ regulator-temp-error-kelvin: ++ description: Set over temperature error limit. This is a limit where part of ++ the hardware propably is malfunctional and damage prevention is requested ++ Zero can be passed to disable error detection and value '1' indicates ++ that detection should be enabled but limit setting can be omitted. ++ ++ regulator-temp-warn-kelvin: ++ description: Set over temperature warning limit. This is a limit where ++ hardware is assumed still to be functional but approaching limit where it ++ gets damaged. Recovery actions should be initiated. Zero can be passed to ++ disable detection and value '1' indicates that detection should ++ be enabled but limit setting can be omitted. ++ ++ regulator-active-discharge: ++ description: | ++ tristate, enable/disable active discharge of regulators. The values are: ++ 0: Disable active discharge. ++ 1: Enable active discharge. ++ Absence of this property will leave configuration to default. ++ $ref: "/schemas/types.yaml#/definitions/uint32" ++ enum: [0, 1] ++ ++ regulator-coupled-with: ++ description: Regulators with which the regulator is coupled. The linkage ++ is 2-way - all coupled regulators should be linked with each other. ++ A regulator should not be coupled with its supplier. ++ $ref: "/schemas/types.yaml#/definitions/phandle-array" ++ ++ regulator-coupled-max-spread: ++ description: Array of maximum spread between voltages of coupled regulators ++ in microvolts, each value in the array relates to the corresponding ++ couple specified by the regulator-coupled-with property. ++ $ref: "/schemas/types.yaml#/definitions/uint32" ++ ++ regulator-max-step-microvolt: ++ description: Maximum difference between current and target voltages ++ that can be changed safely in a single step. ++ ++patternProperties: ++ ".*-supply$": ++ description: Input supply phandle(s) for this node ++ ++ regulator-state-(standby|mem|disk): ++ type: object ++ description: ++ sub-nodes for regulator state in Standby, Suspend-to-RAM, and ++ Suspend-to-DISK modes. Equivalent with standby, mem, and disk Linux ++ sleep states. ++ ++ properties: ++ regulator-on-in-suspend: ++ description: regulator should be on in suspend state. ++ type: boolean ++ ++ regulator-off-in-suspend: ++ description: regulator should be off in suspend state. ++ type: boolean ++ ++ regulator-suspend-min-microvolt: ++ description: minimum voltage may be set in suspend state. ++ ++ regulator-suspend-max-microvolt: ++ description: maximum voltage may be set in suspend state. ++ ++ regulator-suspend-microvolt: ++ description: the default voltage which regulator would be set in ++ suspend. This property is now deprecated, instead setting voltage ++ for suspend mode via the API which regulator driver provides is ++ recommended. ++ ++ regulator-changeable-in-suspend: ++ description: whether the default voltage and the regulator on/off ++ in suspend can be changed in runtime. ++ type: boolean ++ ++ regulator-mode: ++ description: operating mode in the given suspend state. The set ++ of possible operating modes depends on the capabilities of every ++ hardware so the valid modes are documented on each regulator device ++ tree binding document. ++ $ref: "/schemas/types.yaml#/definitions/uint32" ++ ++ additionalProperties: false ++ ++additionalProperties: true ++ ++examples: ++ - | ++ xyzreg: regulator { ++ regulator-min-microvolt = <1000000>; ++ regulator-max-microvolt = <2500000>; ++ regulator-always-on; ++ vin-supply = <&vin>; ++ ++ regulator-state-mem { ++ regulator-on-in-suspend; ++ }; ++ }; ++ ++... +diff --git a/documentation/devicetree/bindings/regulator/st,stm32-vrefbuf.yaml b/documentation/devicetree/bindings/regulator/st,stm32-vrefbuf.yaml +new file mode 100644 +index 000000000..fe9c5e83c +--- /dev/null ++++ b/documentation/devicetree/bindings/regulator/st,stm32-vrefbuf.yaml +@@ -0,0 +1,56 @@ ++# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/regulator/st,stm32-vrefbuf.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: STMicroelectronics STM32 Voltage reference buffer bindings ++ ++description: | ++ Some STM32 devices embed a voltage reference buffer which can be used as ++ voltage reference for ADCs, DACs and also as voltage reference for external ++ components through the dedicated VREF+ pin. ++ ++maintainers: ++ - Fabrice Gasnier ++ ++allOf: ++ - $ref: "regulator.yaml#" ++ ++properties: ++ compatible: ++ enum: ++ - st,stm32-vrefbuf ++ - st,stm32mp13-vrefbuf ++ ++ reg: ++ maxItems: 1 ++ ++ clocks: ++ maxItems: 1 ++ ++ vdda-supply: ++ description: phandle to the vdda input analog voltage. ++ ++required: ++ - compatible ++ - reg ++ - clocks ++ - vdda-supply ++ ++unevaluatedProperties: false ++ ++examples: ++ - | ++ #include ++ vrefbuf@50025000 { ++ compatible = "st,stm32-vrefbuf"; ++ reg = <0x50025000 0x8>; ++ regulator-min-microvolt = <1500000>; ++ regulator-max-microvolt = <2500000>; ++ clocks = <&rcc VREF>; ++ vdda-supply = <&vdda>; ++ }; ++ ++... ++ +diff --git a/documentation/devicetree/bindings/regulator/st,stm32mp1-pwr-reg.yaml b/documentation/devicetree/bindings/regulator/st,stm32mp1-pwr-reg.yaml +new file mode 100644 +index 000000000..422e052bf +--- /dev/null ++++ b/documentation/devicetree/bindings/regulator/st,stm32mp1-pwr-reg.yaml +@@ -0,0 +1,104 @@ ++# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/regulator/st,stm32mp1-pwr-reg.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: STM32MP1 PWR voltage regulators ++ ++maintainers: ++ - Pascal Paillet ++ ++properties: ++ compatible: ++ const: st,stm32mp1,pwr-reg ++ ++ reg: ++ maxItems: 1 ++ ++ st,tzcr: ++ description: | ++ Phandle to the RCC node to control the RCC status ++ secure or non secure with the register address and bit. ++ minItems: 1 ++ $ref: "/schemas/types.yaml#/definitions/uint32-array" ++ ++ system_suspend_supported_soc_modes: ++ description: | ++ List of supported low power SOC modes for system suspend, ++ from the least to the most deep state. The possible values are: ++ STM32_PM_CSLEEP_RUN : wfi ++ STM32_PM_CSTOP_ALLOW_STOP : cstop ++ STM32_PM_CSTOP_ALLOW_LP_STOP : cstop allowing lp-stop ++ STM32_PM_CSTOP_ALLOW_LPLV_STOP : cstop allowing lplv-stop ++ STM32_PM_CSTOP_ALLOW_LPLV_STOP2 : cstop allowing lp-stop2 for STM32MP13 ++ STM32_PM_CSTOP_ALLOW_STANDBY_DDR_SR : cstop allowing standby with DDR in self-refresh ++ $ref: "/schemas/types.yaml#/definitions/uint32" ++ ++ system_off_soc_mode: ++ description: | ++ Mode used for shutdown. The possible values are ++ STM32_PM_CSTOP_ALLOW_STANDBY_DDR_OFF: standby with DDR off ++ STM32_PM_SHUTDOWN: pmic swith off ++ minItems: 1 ++ maxItems: 8 ++ $ref: "/schemas/types.yaml#/definitions/uint32" ++ ++ vdd-supply: ++ description: Input supply phandle(s) for vdd input ++ ++ vdd_3v3_usbfs-supply: ++ description: Input supply phandle(s) for vdd_3v3_usbfs input ++ ++patternProperties: ++ "^(reg11|reg18|usb33)$": ++ type: object ++ ++ $ref: "regulator.yaml#" ++ ++required: ++ - compatible ++ - reg ++ - system_suspend_supported_soc_modes ++ ++additionalProperties: false ++ ++examples: ++ - | ++ //At SOC level: ++ pwr_regulators: pwr@50001000 { ++ compatible = "st,stm32mp1,pwr-reg"; ++ reg = <0x50001000 0x10>; ++ st,tzcr = <&rcc 0x0 0x1>; ++ ++ reg11: reg11 { ++ regulator-name = "reg11"; ++ regulator-min-microvolt = <1100000>; ++ regulator-max-microvolt = <1100000>; ++ }; ++ ++ reg18: reg18 { ++ regulator-name = "reg18"; ++ regulator-min-microvolt = <1800000>; ++ regulator-max-microvolt = <1800000>; ++ }; ++ ++ usb33: usb33 { ++ regulator-name = "usb33"; ++ regulator-min-microvolt = <3300000>; ++ regulator-max-microvolt = <3300000>; ++ }; ++ }; ++ ++ //At board level: ++ &pwr_regulators { ++ system_suspend_supported_soc_modes = < ++ STM32_PM_CSLEEP_RUN ++ STM32_PM_CSTOP_ALLOW_LP_STOP ++ STM32_PM_CSTOP_ALLOW_STANDBY_DDR_SR ++ >; ++ system_off_soc_mode = ; ++ vdd-supply = <&vdd>; ++ vdd_3v3_usbfs-supply = <&vdd_usb>; ++ }; ++... +diff --git a/documentation/devicetree/bindings/reserved-memory/reserved-memory.txt b/documentation/devicetree/bindings/reserved-memory/reserved-memory.txt +new file mode 100644 +index 000000000..39b5f4c5a +--- /dev/null ++++ b/documentation/devicetree/bindings/reserved-memory/reserved-memory.txt +@@ -0,0 +1,171 @@ ++*** Reserved memory regions *** ++ ++Reserved memory is specified as a node under the /reserved-memory node. ++The operating system shall exclude reserved memory from normal usage ++one can create child nodes describing particular reserved (excluded from ++normal use) memory regions. Such memory regions are usually designed for ++the special usage by various device drivers. ++ ++Parameters for each memory region can be encoded into the device tree ++with the following nodes: ++ ++/reserved-memory node ++--------------------- ++#address-cells, #size-cells (required) - standard definition ++ - Should use the same values as the root node ++ranges (required) - standard definition ++ - Should be empty ++ ++/reserved-memory/ child nodes ++----------------------------- ++Each child of the reserved-memory node specifies one or more regions of ++reserved memory. Each child node may either use a 'reg' property to ++specify a specific range of reserved memory, or a 'size' property with ++optional constraints to request a dynamically allocated block of memory. ++ ++Following the generic-names recommended practice, node names should ++reflect the purpose of the node (ie. "framebuffer" or "dma-pool"). Unit ++address (@
) should be appended to the name if the node is a ++static allocation. ++ ++Properties: ++Requires either a) or b) below. ++a) static allocation ++ reg (required) - standard definition ++b) dynamic allocation ++ size (required) - length based on parent's #size-cells ++ - Size in bytes of memory to reserve. ++ alignment (optional) - length based on parent's #size-cells ++ - Address boundary for alignment of allocation. ++ alloc-ranges (optional) - prop-encoded-array (address, length pairs). ++ - Specifies regions of memory that are ++ acceptable to allocate from. ++ ++If both reg and size are present, then the reg property takes precedence ++and size is ignored. ++ ++Additional properties: ++compatible (optional) - standard definition ++ - may contain the following strings: ++ - shared-dma-pool: This indicates a region of memory meant to be ++ used as a shared pool of DMA buffers for a set of devices. It can ++ be used by an operating system to instantiate the necessary pool ++ management subsystem if necessary. ++ - restricted-dma-pool: This indicates a region of memory meant to be ++ used as a pool of restricted DMA buffers for a set of devices. The ++ memory region would be the only region accessible to those devices. ++ When using this, the no-map and reusable properties must not be set, ++ so the operating system can create a virtual mapping that will be used ++ for synchronization. The main purpose for restricted DMA is to ++ mitigate the lack of DMA access control on systems without an IOMMU, ++ which could result in the DMA accessing the system memory at ++ unexpected times and/or unexpected addresses, possibly leading to data ++ leakage or corruption. The feature on its own provides a basic level ++ of protection against the DMA overwriting buffer contents at ++ unexpected times. However, to protect against general data leakage and ++ system memory corruption, the system needs to provide way to lock down ++ the memory access, e.g., MPU. Note that since coherent allocation ++ needs remapping, one must set up another device coherent pool by ++ shared-dma-pool and use dma_alloc_from_dev_coherent instead for atomic ++ coherent allocation. ++ - vendor specific string in the form ,[-] ++no-map (optional) - empty property ++ - Indicates the operating system must not create a virtual mapping ++ of the region as part of its standard mapping of system memory, ++ nor permit speculative access to it under any circumstances other ++ than under the control of the device driver using the region. ++reusable (optional) - empty property ++ - The operating system can use the memory in this region with the ++ limitation that the device driver(s) owning the region need to be ++ able to reclaim it back. Typically that means that the operating ++ system can use that region to store volatile or cached data that ++ can be otherwise regenerated or migrated elsewhere. ++ ++A node must not carry both the no-map and the reusable property as these are ++logically contradictory. ++ ++Linux implementation note: ++- If a "linux,cma-default" property is present, then Linux will use the ++ region for the default pool of the contiguous memory allocator. ++ ++- If a "linux,dma-default" property is present, then Linux will use the ++ region for the default pool of the consistent DMA allocator. ++ ++Device node references to reserved memory ++----------------------------------------- ++Regions in the /reserved-memory node may be referenced by other device ++nodes by adding a memory-region property to the device node. ++ ++memory-region (optional) - phandle, specifier pairs to children of /reserved-memory ++memory-region-names (optional) - a list of names, one for each corresponding ++ entry in the memory-region property ++ ++Example ++------- ++This example defines 4 contiguous regions for Linux kernel: ++one default of all device drivers (named linux,cma@72000000 and 64MiB in size), ++one dedicated to the framebuffer device (named framebuffer@78000000, 8MiB), ++one for multimedia processing (named multimedia-memory@77000000, 64MiB), and ++one for restricted dma pool (named restricted_dma_reserved@0x50000000, 64MiB). ++ ++/ { ++ #address-cells = <1>; ++ #size-cells = <1>; ++ ++ memory { ++ reg = <0x40000000 0x40000000>; ++ }; ++ ++ reserved-memory { ++ #address-cells = <1>; ++ #size-cells = <1>; ++ ranges; ++ ++ /* global autoconfigured region for contiguous allocations */ ++ linux,cma { ++ compatible = "shared-dma-pool"; ++ reusable; ++ size = <0x4000000>; ++ alignment = <0x2000>; ++ linux,cma-default; ++ }; ++ ++ display_reserved: framebuffer@78000000 { ++ reg = <0x78000000 0x800000>; ++ }; ++ ++ multimedia_reserved: multimedia@77000000 { ++ compatible = "acme,multimedia-memory"; ++ reg = <0x77000000 0x4000000>; ++ }; ++ ++ restricted_dma_reserved: restricted_dma_reserved { ++ compatible = "restricted-dma-pool"; ++ reg = <0x50000000 0x4000000>; ++ }; ++ }; ++ ++ /* ... */ ++ ++ fb0: video@12300000 { ++ memory-region = <&display_reserved>; ++ /* ... */ ++ }; ++ ++ scaler: scaler@12500000 { ++ memory-region = <&multimedia_reserved>; ++ /* ... */ ++ }; ++ ++ codec: codec@12600000 { ++ memory-region = <&multimedia_reserved>; ++ /* ... */ ++ }; ++ ++ pcie_device: pcie_device@0,0 { ++ reg = <0x83010000 0x0 0x00000000 0x0 0x00100000 ++ 0x83010000 0x0 0x00100000 0x0 0x00100000>; ++ memory-region = <&restricted_dma_reserved>; ++ /* ... */ ++ }; ++}; +diff --git a/documentation/devicetree/bindings/reset/reset.txt b/documentation/devicetree/bindings/reset/reset.txt +new file mode 100644 +index 000000000..31db6ff84 +--- /dev/null ++++ b/documentation/devicetree/bindings/reset/reset.txt +@@ -0,0 +1,75 @@ ++= Reset Signal Device Tree Bindings = ++ ++This binding is intended to represent the hardware reset signals present ++internally in most IC (SoC, FPGA, ...) designs. Reset signals for whole ++standalone chips are most likely better represented as GPIOs, although there ++are likely to be exceptions to this rule. ++ ++Hardware blocks typically receive a reset signal. This signal is generated by ++a reset provider (e.g. power management or clock module) and received by a ++reset consumer (the module being reset, or a module managing when a sub- ++ordinate module is reset). This binding exists to represent the provider and ++consumer, and provide a way to couple the two together. ++ ++A reset signal is represented by the phandle of the provider, plus a reset ++specifier - a list of DT cells that represents the reset signal within the ++provider. The length (number of cells) and semantics of the reset specifier ++are dictated by the binding of the reset provider, although common schemes ++are described below. ++ ++A word on where to place reset signal consumers in device tree: It is possible ++in hardware for a reset signal to affect multiple logically separate HW blocks ++at once. In this case, it would be unwise to represent this reset signal in ++the DT node of each affected HW block, since if activated, an unrelated block ++may be reset. Instead, reset signals should be represented in the DT node ++where it makes most sense to control it; this may be a bus node if all ++children of the bus are affected by the reset signal, or an individual HW ++block node for dedicated reset signals. The intent of this binding is to give ++appropriate software access to the reset signals in order to manage the HW, ++rather than to slavishly enumerate the reset signal that affects each HW ++block. ++ ++= Reset providers = ++ ++Required properties: ++#reset-cells: Number of cells in a reset specifier; Typically 0 for nodes ++ with a single reset output and 1 for nodes with multiple ++ reset outputs. ++ ++For example: ++ ++ rst: reset-controller { ++ #reset-cells = <1>; ++ }; ++ ++= Reset consumers = ++ ++Required properties: ++resets: List of phandle and reset specifier pairs, one pair ++ for each reset signal that affects the device, or that the ++ device manages. Note: if the reset provider specifies '0' for ++ #reset-cells, then only the phandle portion of the pair will ++ appear. ++ ++Optional properties: ++reset-names: List of reset signal name strings sorted in the same order as ++ the resets property. Consumers drivers will use reset-names to ++ match reset signal names with reset specifiers. ++ ++For example: ++ ++ device { ++ resets = <&rst 20>; ++ reset-names = "reset"; ++ }; ++ ++This represents a device with a single reset signal named "reset". ++ ++ bus { ++ resets = <&rst 10> <&rst 11> <&rst 12> <&rst 11>; ++ reset-names = "i2s1", "i2s2", "dma", "mixer"; ++ }; ++ ++This represents a bus that controls the reset signal of each of four sub- ++ordinate devices. Consider for example a bus that fails to operate unless no ++child device has reset asserted. +diff --git a/documentation/devicetree/bindings/rng/st,stm32-rng.yaml b/documentation/devicetree/bindings/rng/st,stm32-rng.yaml +new file mode 100644 +index 000000000..4fdfc1f9e +--- /dev/null ++++ b/documentation/devicetree/bindings/rng/st,stm32-rng.yaml +@@ -0,0 +1,54 @@ ++# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/rng/st,stm32-rng.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: STMicroelectronics STM32 RNG bindings ++ ++description: | ++ The STM32 hardware random number generator is a simple fixed purpose ++ IP and is fully separated from other crypto functions. ++ ++maintainers: ++ - Lionel Debieve ++ ++properties: ++ compatible: ++ oneOf: ++ - items: ++ - enum: ++ - st,stm32mp13-rng ++ - const: st,stm32-rng ++ ++ reg: ++ maxItems: 1 ++ ++ clocks: ++ maxItems: 1 ++ ++ resets: ++ maxItems: 1 ++ ++ clock-error-detect: ++ description: If set enable the clock detection management ++ ++required: ++ - compatible ++ - reg ++ - clocks ++ ++additionalProperties: false ++ ++examples: ++ - | ++ #include ++ #include ++ rng@54003000 { ++ compatible = "st,stm32-rng"; ++ reg = <0x54003000 0x400>; ++ clocks = <&rcc RNG1_K>; ++ resets = <&rcc RNG1_R>; ++ }; ++ ++... +diff --git a/documentation/devicetree/bindings/rtc/st,stm32-rtc.yaml b/documentation/devicetree/bindings/rtc/st,stm32-rtc.yaml +new file mode 100644 +index 000000000..c3fb8c738 +--- /dev/null ++++ b/documentation/devicetree/bindings/rtc/st,stm32-rtc.yaml +@@ -0,0 +1,180 @@ ++# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/rtc/st,stm32-rtc.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: STMicroelectronics STM32 Real Time Clock Bindings ++ ++maintainers: ++ - Gabriel Fernandez ++ ++properties: ++ compatible: ++ enum: ++ - st,stm32-rtc ++ - st,stm32h7-rtc ++ - st,stm32mp1-rtc ++ - st,stm32mp13-rtc ++ ++ reg: ++ maxItems: 1 ++ ++ clocks: ++ minItems: 1 ++ maxItems: 2 ++ ++ clock-names: ++ items: ++ - const: pclk ++ - const: rtc_ck ++ ++ interrupts: ++ maxItems: 1 ++ ++ st,syscfg: ++ $ref: "/schemas/types.yaml#/definitions/phandle-array" ++ items: ++ minItems: 3 ++ maxItems: 3 ++ description: | ++ Phandle/offset/mask triplet. The phandle to pwrcfg used to ++ access control register at offset, and change the dbp (Disable Backup ++ Protection) bit represented by the mask, mandatory to disable/enable backup ++ domain (RTC registers) write protection. ++ ++ assigned-clocks: ++ description: | ++ override default rtc_ck parent clock reference to the rtc_ck clock entry ++ maxItems: 1 ++ ++ assigned-clock-parents: ++ description: | ++ override default rtc_ck parent clock phandle of the new parent clock of rtc_ck ++ maxItems: 1 ++ ++ st,lsco: ++ $ref: "/schemas/types.yaml#/definitions/uint32" ++ description: | ++ To select and enable RTC Low Speed Clock Output. ++ Refer to for the supported values. ++ Pinctrl state named "default" may be defined to reserve pin for RTC output. ++ ++ st,alarm: ++ $ref: "/schemas/types.yaml#/definitions/uint32" ++ description: | ++ To select and enable RTC Alarm A output. ++ Refer to for the supported values. ++ Pinctrl state named "default" may be defined to reserve pin for RTC output. ++ ++allOf: ++ - if: ++ properties: ++ compatible: ++ contains: ++ const: st,stm32-rtc ++ ++ then: ++ properties: ++ clocks: ++ minItems: 1 ++ maxItems: 1 ++ ++ st,lsco: ++ maxItems: 0 ++ ++ st,alarm: ++ maxItems: 0 ++ ++ clock-names: false ++ ++ required: ++ - st,syscfg ++ ++ - if: ++ properties: ++ compatible: ++ contains: ++ const: st,stm32h7-rtc ++ ++ then: ++ properties: ++ clocks: ++ minItems: 2 ++ maxItems: 2 ++ ++ st,lsco: ++ maxItems: 0 ++ ++ st,alarm: ++ maxItems: 0 ++ ++ required: ++ - clock-names ++ - st,syscfg ++ ++ - if: ++ properties: ++ compatible: ++ contains: ++ enum: ++ - st,stm32mp1-rtc ++ - st,stm32mp13-rtc ++ ++ then: ++ properties: ++ clocks: ++ minItems: 2 ++ maxItems: 2 ++ ++ assigned-clocks: false ++ assigned-clock-parents: false ++ ++ st,lsco: ++ maxItems: 1 ++ ++ st,alarm: ++ maxItems: 1 ++ ++ required: ++ - clock-names ++ ++required: ++ - compatible ++ - reg ++ - clocks ++ - interrupts ++ ++additionalProperties: false ++ ++examples: ++ - | ++ #include ++ #include ++ rtc@40002800 { ++ compatible = "st,stm32-rtc"; ++ reg = <0x40002800 0x400>; ++ clocks = <&rcc 1 CLK_RTC>; ++ assigned-clocks = <&rcc 1 CLK_RTC>; ++ assigned-clock-parents = <&rcc 1 CLK_LSE>; ++ interrupt-parent = <&exti>; ++ interrupts = <17 1>; ++ st,syscfg = <&pwrcfg 0x00 0x100>; ++ }; ++ ++ #include ++ #include ++ #include ++ rtc@5c004000 { ++ compatible = "st,stm32mp1-rtc"; ++ reg = <0x5c004000 0x400>; ++ clocks = <&rcc RTCAPB>, <&rcc RTC>; ++ clock-names = "pclk", "rtc_ck"; ++ interrupts = ; ++ st,alarm = ; ++ st,lsco = ; ++ pinctrl-0 = <&rtc_out1_pins_a &rtc_out2_rmp_pins_a>; ++ pinctrl-names = "default"; ++ }; ++ ++... +diff --git a/documentation/devicetree/bindings/serial/rs485.yaml b/documentation/devicetree/bindings/serial/rs485.yaml +new file mode 100644 +index 000000000..603d74cf2 +--- /dev/null ++++ b/documentation/devicetree/bindings/serial/rs485.yaml +@@ -0,0 +1,65 @@ ++# SPDX-License-Identifier: GPL-2.0 ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/serial/rs485.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: RS485 serial communications Bindings ++ ++description: The RTS signal is capable of automatically controlling line ++ direction for the built-in half-duplex mode. The properties described ++ hereafter shall be given to a half-duplex capable UART node. ++ ++maintainers: ++ - Rob Herring ++ ++properties: ++ rs485-rts-delay: ++ description: prop-encoded-array ++ $ref: /schemas/types.yaml#/definitions/uint32-array ++ items: ++ items: ++ - description: Delay between rts signal and beginning of data sent in ++ milliseconds. It corresponds to the delay before sending data. ++ default: 0 ++ maximum: 1000 ++ - description: Delay between end of data sent and rts signal in milliseconds. ++ It corresponds to the delay after sending data and actual release ++ of the line. ++ default: 0 ++ maximum: 1000 ++ ++ rs485-rts-delay-ns: ++ description: prop-encoded-array ++ items: ++ items: ++ - description: Delay between rts signal and beginning of data sent in ++ nanoseconds. It corresponds to the delay before sending data. ++ default: 0 ++ maximum: 1000 ++ - description: Delay between end of data sent and rts signal in nanoseconds. ++ It corresponds to the delay after sending data and actual release ++ of the line. ++ default: 0 ++ maximum: 1000 ++ ++ rs485-rts-active-low: ++ description: drive RTS low when sending (default is high). ++ $ref: /schemas/types.yaml#/definitions/flag ++ ++ linux,rs485-enabled-at-boot-time: ++ description: enables the rs485 feature at boot time. It can be disabled ++ later with proper ioctl. ++ $ref: /schemas/types.yaml#/definitions/flag ++ ++ rs485-rx-during-tx: ++ description: enables the receiving of data even while sending data. ++ $ref: /schemas/types.yaml#/definitions/flag ++ ++ rs485-term-gpios: ++ description: GPIO pin to enable RS485 bus termination. ++ maxItems: 1 ++ ++additionalProperties: true ++ ++... +diff --git a/documentation/devicetree/bindings/serial/serial.yaml b/documentation/devicetree/bindings/serial/serial.yaml +new file mode 100644 +index 000000000..c75ba3fb6 +--- /dev/null ++++ b/documentation/devicetree/bindings/serial/serial.yaml +@@ -0,0 +1,153 @@ ++# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) ++%YAML 1.2 ++--- ++$id: "http://devicetree.org/schemas/serial/serial.yaml#" ++$schema: "http://devicetree.org/meta-schemas/core.yaml#" ++ ++title: Serial Interface Generic DT Bindings ++ ++maintainers: ++ - Rob Herring ++ - Greg Kroah-Hartman ++ ++description: ++ This document lists a set of generic properties for describing UARTs in a ++ device tree. Whether these properties apply to a particular device depends ++ on the DT bindings for the actual device. ++ ++ Each enabled UART may have an optional "serialN" alias in the "aliases" node, ++ where N is the port number (non-negative decimal integer) as printed on the ++ label next to the physical port. ++ ++properties: ++ $nodename: ++ pattern: "^serial(@.*)?$" ++ ++ label: true ++ ++ cts-gpios: ++ maxItems: 1 ++ description: ++ Must contain a GPIO specifier, referring to the GPIO pin to be used as ++ the UART's CTS line. ++ ++ dcd-gpios: ++ maxItems: 1 ++ description: ++ Must contain a GPIO specifier, referring to the GPIO pin to be used as ++ the UART's DCD line. ++ ++ dsr-gpios: ++ maxItems: 1 ++ description: ++ Must contain a GPIO specifier, referring to the GPIO pin to be used as ++ the UART's DSR line. ++ ++ dtr-gpios: ++ maxItems: 1 ++ description: ++ Must contain a GPIO specifier, referring to the GPIO pin to be used as ++ the UART's DTR line. ++ ++ rng-gpios: ++ maxItems: 1 ++ description: ++ Must contain a GPIO specifier, referring to the GPIO pin to be used as ++ the UART's RNG line. ++ ++ rts-gpios: ++ maxItems: 1 ++ description: ++ Must contain a GPIO specifier, referring to the GPIO pin to be used as ++ the UART's RTS line. ++ ++ uart-has-rtscts: ++ $ref: /schemas/types.yaml#/definitions/flag ++ description: ++ The presence of this property indicates that the UART has dedicated lines ++ for RTS/CTS hardware flow control, and that they are available for use ++ (wired and enabled by pinmux configuration). This depends on both the ++ UART hardware and the board wiring. ++ ++ rx-tx-swap: ++ type: boolean ++ description: RX and TX pins are swapped. ++ ++ cts-rts-swap: ++ type: boolean ++ description: CTS and RTS pins are swapped. ++ ++ rx-threshold: ++ $ref: /schemas/types.yaml#/definitions/uint32 ++ description: ++ RX FIFO threshold configuration (in bytes). ++ ++ tx-threshold: ++ $ref: /schemas/types.yaml#/definitions/uint32 ++ description: ++ TX FIFO threshold configuration (in bytes). ++ ++if: ++ required: ++ - uart-has-rtscts ++then: ++ properties: ++ cts-gpios: false ++ rts-gpios: false ++ ++patternProperties: ++ ".*": ++ if: ++ type: object ++ then: ++ description: ++ Serial attached devices shall be a child node of the host UART device ++ the slave device is attached to. It is expected that the attached ++ device is the only child node of the UART device. The slave device node ++ name shall reflect the generic type of device for the node. ++ ++ properties: ++ compatible: ++ description: ++ Compatible of the device connected to the serial port. ++ ++ max-speed: ++ $ref: /schemas/types.yaml#/definitions/uint32 ++ description: ++ The maximum baud rate the device operates at. ++ This should only be present if the maximum is less than the slave ++ device can support. For example, a particular board has some ++ signal quality issue or the host processor can't support higher ++ baud rates. ++ ++ current-speed: ++ $ref: /schemas/types.yaml#/definitions/uint32 ++ description: | ++ The current baud rate the device operates at. ++ This should only be present in case a driver has no chance to know ++ the baud rate of the slave device. ++ Examples: ++ * device supports auto-baud ++ * the rate is setup by a bootloader and there is no way to reset ++ the device ++ * device baud rate is configured by its firmware but there is no ++ way to request the actual settings ++ ++ required: ++ - compatible ++ ++additionalProperties: true ++ ++examples: ++ - | ++ serial@1234 { ++ compatible = "ns16550a"; ++ reg = <0x1234 0x20>; ++ interrupts = <1>; ++ ++ bluetooth { ++ compatible = "brcm,bcm4330-bt"; ++ interrupt-parent = <&gpio>; ++ interrupts = <10>; ++ }; ++ }; +diff --git a/documentation/devicetree/bindings/serial/st,stm32-uart.yaml b/documentation/devicetree/bindings/serial/st,stm32-uart.yaml +new file mode 100644 +index 000000000..93ca9aa8f +--- /dev/null ++++ b/documentation/devicetree/bindings/serial/st,stm32-uart.yaml +@@ -0,0 +1,138 @@ ++# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/serial/st,stm32-uart.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++maintainers: ++ - Erwan Le Ray ++ ++title: STMicroelectronics STM32 USART bindings ++ ++properties: ++ compatible: ++ enum: ++ - st,stm32-uart ++ - st,stm32f7-uart ++ - st,stm32h7-uart ++ ++ reg: ++ maxItems: 1 ++ ++ interrupts: ++ maxItems: 1 ++ ++ clocks: ++ maxItems: 1 ++ ++ resets: ++ maxItems: 1 ++ ++ label: ++ description: label associated with this uart ++ ++ st,hw-flow-ctrl: ++ description: enable hardware flow control (deprecated) ++ $ref: /schemas/types.yaml#/definitions/flag ++ ++ uart-has-rtscts: true ++ ++ rx-tx-swap: true ++ ++ dmas: ++ minItems: 1 ++ maxItems: 2 ++ ++ dma-names: ++ items: ++ enum: [ rx, tx ] ++ minItems: 1 ++ maxItems: 2 ++ ++# cts-gpios and rts-gpios properties can be used instead of 'uart-has-rtscts' ++# or 'st,hw-flow-ctrl' (deprecated) for making use of any gpio pins for flow ++# control instead of dedicated pins. ++# ++# It should be noted that both cts-gpios/rts-gpios and 'uart-has-rtscts' or ++# 'st,hw-flow-ctrl' (deprecated) properties cannot co-exist in a design. ++ cts-gpios: true ++ rts-gpios: true ++ ++ wakeup-source: true ++ ++ rs485-rts-delay: true ++ rs485-rts-delay-ns: true ++ rs485-rts-active-low: true ++ linux,rs485-enabled-at-boot-time: true ++ rs485-rx-during-tx: true ++ ++ power-domains: ++ $ref: ../power/power-domain.yaml ++ maxItems: 1 ++ ++ rx-threshold: ++ description: ++ If value is set to 1, RX FIFO threshold is disabled. ++ enum: [1, 2, 4, 8, 12, 14, 16] ++ default: 8 ++ ++ tx-threshold: ++ description: ++ If value is set to 1, TX FIFO threshold is disabled. ++ enum: [1, 2, 4, 8, 12, 14, 16] ++ default: 8 ++ ++allOf: ++ - $ref: rs485.yaml# ++ - $ref: serial.yaml# ++ - if: ++ required: ++ - st,hw-flow-ctrl ++ then: ++ properties: ++ cts-gpios: false ++ rts-gpios: false ++ - if: ++ properties: ++ compatible: ++ const: st,stm32-uart ++ then: ++ properties: ++ rx-tx-swap: false ++ - if: ++ properties: ++ compatible: ++ contains: ++ enum: ++ - st,stm32-uart ++ - st,stm32f7-uart ++ then: ++ properties: ++ rx-threshold: false ++ tx-threshold: false ++ ++required: ++ - compatible ++ - reg ++ - interrupts ++ - clocks ++ ++unevaluatedProperties: false ++ ++examples: ++ - | ++ #include ++ usart1: serial@40011000 { ++ compatible = "st,stm32h7-uart"; ++ reg = <0x40011000 0x400>; ++ interrupts = <37>; ++ clocks = <&rcc 0 164>; ++ dmas = <&dma2 2 4 0x414 0x0>, ++ <&dma2 7 4 0x414 0x0>; ++ dma-names = "rx", "tx"; ++ rx-threshold = <4>; ++ tx-threshold = <4>; ++ rs485-rts-active-low; ++ }; ++ ++... +diff --git a/documentation/devicetree/bindings/soc/stm32/st,stm32-etzpc.yaml b/documentation/devicetree/bindings/soc/stm32/st,stm32-etzpc.yaml +new file mode 100644 +index 000000000..2245147f5 +--- /dev/null ++++ b/documentation/devicetree/bindings/soc/stm32/st,stm32-etzpc.yaml +@@ -0,0 +1,76 @@ ++# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/soc/stm32/st,stm32-etzpc.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: STM32 Extended TrustZone protection controller bindings ++ ++maintainers: ++ - Lionel Debieve ++ ++properties: ++ compatible: ++ const: "st,stm32-etzpc" ++ ++ "#address-cells": ++ const: 1 ++ ++ "#size-cells": ++ const: 1 ++ ++ reg: ++ maxItems: 1 ++ ++ clocks: ++ maxItems: 1 ++ ++ st,decprot: ++ description: | ++ - Define a list of peripherals with the associated configuration. ++ It is defined by macros DECPROT(STM32MP1_ETZPC_XXX_ID, DECPROT_XX, DECPROT_LOCK) ++ as defined by header file dt-bindings/soc/stm32mp13-etzpc.h. ++ - A peripheral ID is required as in dt-bindings/soc/stm32mp13-etzpc.h for STM32MP13 and ++ dt-bindings/soc/stm32mp15-etzpc.h for STM32MP15 ++ - Peripheral access which can be defined using following description ++ DECPROT_S_RW: Secure Read/Write ++ DECPROT_NS_R_S_W: Non-secure Read / Secure write ++ DECPROT_MCU_ISOLATION: CoProcessor isolated ++ DECPROT_NS_RW: Non-secure Read/Write ++ - description: Configuration lock state (DECPROT_UNLOCK / DECPROT_LOCK) ++ $ref: "/schemas/types.yaml#/definitions/uint32-array" ++ ++required: ++ - compatible ++ - "#address-cells" ++ - "#size-cells" ++ - reg ++ - clocks ++ ++additionalProperties: false ++ ++examples: ++ - | ++ #include ++ #include ++ #include ++ etzpc: etzpc@5c007000 { ++ compatible = "st,stm32-etzpc"; ++ reg = <0x5C007000 0x400>; ++ clocks = <&rcc TZPC>; ++ #address-cells = <1>; ++ #size-cells = <1>; ++ st,decprot = < ++ DECPROT(STM32MP1_ETZPC_ADC1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_ADC2_ID, DECPROT_NS_RW, DECPROT_LOCK) ++ DECPROT(STM32MP1_ETZPC_BKPSRAM_ID, DECPROT_S_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_CRYP_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_DCMIPP_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_DDRCTRLPHY_ID, DECPROT_NS_R_S_W, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_ETH1_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_ETH2_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ DECPROT(STM32MP1_ETZPC_FMC_ID, DECPROT_NS_RW, DECPROT_UNLOCK) ++ >: ++ }; ++ ++... +diff --git a/documentation/devicetree/bindings/soc/stm32/st,stm32mp1-tzc.yaml b/documentation/devicetree/bindings/soc/stm32/st,stm32mp1-tzc.yaml +new file mode 100644 +index 000000000..6b5097146 +--- /dev/null ++++ b/documentation/devicetree/bindings/soc/stm32/st,stm32mp1-tzc.yaml +@@ -0,0 +1,88 @@ ++# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/soc/stm32/st,stm32mp1-tzc.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: STM32MP1 TrustZone address space bindings ++ ++description: | ++ The STM32MP1 TZC is an ARM TZC400 based DDR access controller. It uses filters ++ to control read/write access to the DDR. ++ TZC node specifies a list of phandles to specific reserved memory regions. ++ It uses the defined bindings in reserved-memory.txt and adding a specific property ++ for the access right management. ++ ++maintainers: ++ - Lionel Debieve ++ ++properties: ++ compatible: ++ const: st,stm32mp1-tzc ++ ++ reg: ++ maxItems: 1 ++ ++ clocks: ++ minItems: 1 ++ maxItems: 2 ++ ++ interrupts: ++ maxItems: 1 ++ ++ memory-region: ++ description: | ++ Phandle to a node describing memory area to be configured by the TZC. ++ The reserved region must describe a st,protreg property that will contain the specific ++ access management for a region. ++ - The "st,protreg" property defines the memory access type with a secure access and non ++ secure access field. Secure access initial u32 uses: ++ TZC_REGION_S_NONE: No secure access available ++ TZC_REGION_S_RD: Secure access Read only ++ TZC_REGION_S_WR: Secure access Write only ++ TZC_REGION_S_RDWR: Secure access Read/Write ++ The non secure access uses a u32 refering to the Non secure master. The list of the NSAID ++ are described in the dt-bindings/soc/stm32mp13-tzc400.h for STM32MP13 and in ++ dt-bindings/soc/stm32mp15-tzc400.h for STM32MP15. The TZC400 macro can be used to ++ create the u32. Ex: ++ (TZC_REGION_ACCESS_RD(1) | TZC_REGION_ACCESS_WD(2) | TZC_REGION_ACCESS_RDWR(3)) ++ $ref: "/schemas/types.yaml#/definitions/uint32-array" ++ ++required: ++ - compatible ++ - reg ++ - clocks ++ - interrupts ++ ++additionalProperties: false ++ ++examples: ++ - | ++ #include ++ #include ++ #include ++ #include ++ #include ++ ++ reserved-memory { ++ #address-cells = <1>; ++ #size-cells = <1>; ++ ranges; ++ ++ optee_framebuffer: optee-framebuffer@dd000000 { ++ /* Secure framebuffer memory */ ++ reg = <0xdd000000 0x1000000>; ++ /* Secure read/write access with no non secure access */ ++ st,protreg = ; ++ }; ++ }; ++ ++ tzc@5c006000 { ++ compatible = "st,stm32mp1-tzc"; ++ reg = <0x5c006000 0x1000>; ++ interrupts = ; ++ clocks = <&rcc TZC>; ++ memory-region = <&optee_framebuffer>; ++ }; ++ ++... +diff --git a/documentation/devicetree/bindings/watchdog/st,stm32-iwdg.yaml b/documentation/devicetree/bindings/watchdog/st,stm32-iwdg.yaml +new file mode 100644 +index 000000000..395001873 +--- /dev/null ++++ b/documentation/devicetree/bindings/watchdog/st,stm32-iwdg.yaml +@@ -0,0 +1,67 @@ ++# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/watchdog/st,stm32-iwdg.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: STMicroelectronics STM32 Independent WatchDoG (IWDG) bindings ++ ++maintainers: ++ - Yannick Fertre ++ - Christophe Roullier ++ ++allOf: ++ - $ref: "watchdog.yaml#" ++ ++properties: ++ compatible: ++ enum: ++ - st,stm32-iwdg ++ - st,stm32mp1-iwdg ++ ++ reg: ++ maxItems: 1 ++ ++ clocks: ++ items: ++ - description: Low speed clock ++ - description: Optional peripheral clock ++ minItems: 1 ++ ++ clock-names: ++ items: ++ enum: ++ - lsi ++ - pclk ++ minItems: 1 ++ maxItems: 2 ++ ++ interrupts: ++ maxItems: 1 ++ ++ secure-timeout-sec: ++ description: Timeout in second used to specify the early watchdog interrupt timeout. ++ When defined, the watchdog is automatically started. ++ ++required: ++ - compatible ++ - reg ++ - clocks ++ - clock-names ++ ++unevaluatedProperties: false ++ ++examples: ++ - | ++ #include ++ watchdog@5a002000 { ++ compatible = "st,stm32mp1-iwdg"; ++ reg = <0x5a002000 0x400>; ++ interrupts = ; ++ clocks = <&rcc IWDG2>, <&rcc CK_LSI>; ++ clock-names = "pclk", "lsi"; ++ timeout-sec = <32>; ++ secure-timeout = <10>; ++ }; ++ ++... +diff --git a/documentation/devicetree/bindings/watchdog/watchdog.yaml b/documentation/devicetree/bindings/watchdog/watchdog.yaml +new file mode 100644 +index 000000000..e3dfb02f0 +--- /dev/null ++++ b/documentation/devicetree/bindings/watchdog/watchdog.yaml +@@ -0,0 +1,27 @@ ++# SPDX-License-Identifier: GPL-2.0 ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/watchdog/watchdog.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: Watchdog Generic Bindings ++ ++maintainers: ++ - Guenter Roeck ++ - Wim Van Sebroeck ++ ++description: | ++ This document describes generic bindings which can be used to ++ describe watchdog devices in a device tree. ++ ++properties: ++ $nodename: ++ pattern: "^watchdog(@.*|-[0-9a-f])?$" ++ ++ timeout-sec: ++ description: ++ Contains the watchdog timeout in seconds. ++ ++additionalProperties: true ++ ++... +diff --git a/keys/default_rproc.pem b/keys/default_rproc.pem +new file mode 100644 +index 000000000..d54476f35 +--- /dev/null ++++ b/keys/default_rproc.pem +@@ -0,0 +1,27 @@ ++-----BEGIN RSA PRIVATE KEY----- ++MIIEpAIBAAKCAQEA1TD3AMwyJhzsliOhEStPrCrgnG9Gy8z+OCtialVngW/rkqQB ++cb/oOdiMPjfLoSio8zaUA86mTHo6DINZIopTZvs9pysnpvrOwbjCVRXTcIhgM/YZ ++GyN9c+Qo5fCbOlCsxD0PG40hP4O0dktWyzmWQfjy0+9BDAyMoW59lPeYZcJAKSWT ++M10V5h3JTabA4dqVroeztuTow3ftNImNuzMFYDqGDUcJy0EdluRsBfhOOKXE7ZaQ ++RXnwY9CTCGqgsuNKwE1g8evkseaLcJk4/JpVFOgZp4fUgsxU6EBRD2i+C+Jq9KXg ++qBcK0QwXNr2IwG3i76QmLzGGkpW7bKPn/QhGMQIDAQABAoIBAQCHBnAqzSmmAgPG ++Q+KroSbhAHcqHUBVrAwHP1Mhzd20mVI2mjFf/g/zMzf/4A7Uj5ASGqs8jhG9tlw1 ++uKsnuTyBqPavfiGrHIb/IynSAfTc/UMRJflYuu2mDQfqOq3WDWqfD50V8hjwxVXy ++5lyecma8ehQyLwKfwwL+66AWTYr0Rx+OdXkGdGj1SXbJU39nv7UH8ZggpICFhUXO ++r7NgJr2UOyhEje4stTGSb86Srv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GES +new file mode 100644 +index 000000000..f0b0a9342 +--- /dev/null ++++ b/lib/libpng/CHANGES +@@ -0,0 +1,6109 @@ ++CHANGES - changes for libpng ++ ++version 0.1 [March 29, 1995] ++ initial work-in-progress release ++ ++version 0.2 [April 1, 1995] ++ added reader into png.h ++ fixed small problems in stub file ++ ++version 0.3 [April 8, 1995] ++ added pull reader ++ split up pngwrite.c to several files ++ added pnglib.txt ++ added example.c ++ cleaned up writer, adding a few new transformations ++ fixed some bugs in writer ++ interfaced with zlib 0.5 ++ added K&R support ++ added check for 64 KB blocks for 16 bit machines ++ ++version 0.4 [April 26, 1995] ++ cleaned up code and commented code ++ simplified time handling into png_time ++ created png_color_16 and png_color_8 to handle color needs ++ cleaned up color type defines ++ fixed various bugs ++ made various names more consistent ++ interfaced with zlib 0.71 ++ cleaned up zTXt reader and writer (using zlib's Reset functions) ++ split transformations into pngrtran.c and pngwtran.c ++ ++version 0.5 [April 30, 1995] ++ interfaced with zlib 0.8 ++ fixed many reading and writing bugs ++ saved using 3 spaces instead of tabs ++ ++version 0.6 [May 1, 1995] ++ first beta release ++ added png_large_malloc() and png_large_free() ++ added png_size_t ++ cleaned up some compiler warnings ++ added png_start_read_image() ++ ++version 0.7 [June 24, 1995] ++ cleaned up lots of bugs ++ finished dithering and other stuff ++ added test program ++ changed name from pnglib to libpng ++ ++version 0.71 [June 26, 1995] ++ changed pngtest.png for zlib 0.93 ++ fixed error in libpng.txt and example.c ++ ++version 0.8 [August 20, 1995] ++ cleaned up some bugs ++ added png_set_filler() ++ split up pngstub.c into pngmem.c, pngio.c, and pngerror.c ++ added #define's to remove unwanted code ++ moved png_info_init() to png.c ++ added old_size into png_realloc() ++ added functions to manually set filtering and compression info ++ changed compression parameters based on image type ++ optimized filter selection code ++ added version info ++ changed external functions passing floats to doubles (k&r problems?) ++ put all the configurable stuff in pngconf.h ++ enabled png_set_shift to work with paletted images on read ++ added png_read_update_info() - updates info structure with transformations ++ ++Version 0.81 [August, 1995] ++ incorporated Tim Wegner's medium model code (thanks, Tim) ++ ++Version 0.82 [September, 1995] ++ [unspecified changes] ++ ++Version 0.85 [December, 1995] ++ added more medium model code (almost everything's a far) ++ added i/o, error, and memory callback functions ++ fixed some bugs (16-bit, 4-bit interlaced, etc.) ++ added first run progressive reader (barely tested) ++ ++Version 0.86 [January, 1996] ++ fixed bugs ++ improved documentation ++ ++Version 0.87 [January, 1996] ++ fixed medium model bugs ++ fixed other bugs introduced in 0.85 and 0.86 ++ added some minor documentation ++ ++Version 0.88 [January, 1996] ++ fixed progressive bugs ++ replaced tabs with spaces ++ cleaned up documentation ++ added callbacks for read/write and warning/error functions ++ ++Version 0.89 [June 5, 1996] ++ Added new initialization API to make libpng work better with shared libs ++ we now have png_create_read_struct(), png_create_write_struct(), ++ png_create_info_struct(), png_destroy_read_struct(), and ++ png_destroy_write_struct() instead of the separate calls to ++ malloc and png_read_init(), png_info_init(), and png_write_init() ++ Changed warning/error callback functions to fix bug - this means you ++ should use the new initialization API if you were using the old ++ png_set_message_fn() calls, and that the old API no longer exists ++ so that people are aware that they need to change their code ++ Changed filter selection API to allow selection of multiple filters ++ since it didn't work in previous versions of libpng anyways ++ Optimized filter selection code ++ Fixed png_set_background() to allow using an arbitrary RGB color for ++ paletted images ++ Fixed gamma and background correction for paletted images, so ++ png_correct_palette is not needed unless you are correcting an ++ external palette (you will need to #define PNG_CORRECT_PALETTE_SUPPORTED ++ in pngconf.h) - if nobody uses this, it may disappear in the future. ++ Fixed bug with Borland 64K memory allocation (Alexander Lehmann) ++ Fixed bug in interlace handling (Smarasderagd, I think) ++ Added more error checking for writing and image to reduce invalid files ++ Separated read and write functions so that they won't both be linked ++ into a binary when only reading or writing functionality is used ++ New pngtest image also has interlacing and zTXt ++ Updated documentation to reflect new API ++ ++Version 0.89c [June 17, 1996] ++ Bug fixes. ++ ++Version 0.90 [January, 1997] ++ Made CRC errors/warnings on critical and ancillary chunks configurable ++ libpng will use the zlib CRC routines by (compile-time) default ++ Changed DOS small/medium model memory support - needs zlib 1.04 (Tim Wegner) ++ Added external C++ wrapper statements to png.h (Gilles Dauphin) ++ Allow PNG file to be read when some or all of file signature has already ++ been read from the beginning of the stream. ****This affects the size ++ of info_struct and invalidates all programs that use a shared libpng**** ++ Fixed png_filler() declarations ++ Fixed? background color conversions ++ Fixed order of error function pointers to match documentation ++ Current chunk name is now available in png_struct to reduce the number ++ of nearly identical error messages (will simplify multi-lingual ++ support when available) ++ Try to get ready for unknown-chunk callback functions: ++ - previously read critical chunks are flagged, so the chunk handling ++ routines can determine if the chunk is in the right place ++ - all chunk handling routines have the same prototypes, so we will ++ be able to handle all chunks via a callback mechanism ++ Try to fix Linux "setjmp" buffer size problems ++ Removed png_large_malloc, png_large_free, and png_realloc functions. ++ ++Version 0.95 [March, 1997] ++ Fixed bug in pngwutil.c allocating "up_row" twice and "avg_row" never ++ Fixed bug in PNG file signature compares when start != 0 ++ Changed parameter type of png_set_filler(...filler...) from png_byte ++ to png_uint_32 ++ Added test for MACOS to ensure that both math.h and fp.h are not #included ++ Added macros for libpng to be compiled as a Windows DLL (Andreas Kupries) ++ Added "packswap" transformation, which changes the endianness of ++ packed-pixel bytes (Kevin Bracey) ++ Added "strip_alpha" transformation, which removes the alpha channel of ++ input images without using it (not necessarily a good idea) ++ Added "swap_alpha" transformation, which puts the alpha channel in front ++ of the color bytes instead of after ++ Removed all implicit variable tests which assume NULL == 0 (I think) ++ Changed several variables to "png_size_t" to show 16/32-bit limitations ++ Added new pCAL chunk read/write support ++ Added experimental filter selection weighting (Greg Roelofs) ++ Removed old png_set_rgbx() and png_set_xrgb() functions that have been ++ obsolete for about 2 years now (use png_set_filler() instead) ++ Added macros to read 16- and 32-bit ints directly from buffer, to be ++ used only on those systems that support it (namely PowerPC and 680x0) ++ With some testing, this may become the default for MACOS/PPC systems. ++ Only calculate CRC on data if we are going to use it ++ Added macros for zTXt compression type PNG_zTXt_COMPRESSION_??? ++ Added macros for simple libpng debugging output selectable at compile time ++ Removed PNG_READ_END_MODE in progressive reader (Smarasderagd) ++ More description of info_struct in libpng.txt and png.h ++ More instructions in example.c ++ More chunk types tested in pngtest.c ++ Renamed pngrcb.c to pngset.c, and all png_read_ functions to be ++ png_set_. We now have corresponding png_get_ ++ functions in pngget.c to get information in info_ptr. This isolates ++ the application from the internal organization of png_info_struct ++ (good for shared library implementations). ++ ++Version 0.96 [May, 1997] ++ Fixed serious bug with < 8bpp images introduced in 0.95 ++ Fixed 256-color transparency bug (Greg Roelofs) ++ Fixed up documentation (Greg Roelofs, Laszlo Nyul) ++ Fixed "error" in pngconf.h for Linux setjmp() behavior ++ Fixed DOS medium model support (Tim Wegner) ++ Fixed png_check_keyword() for case with error in static string text ++ Added read of CRC after IEND chunk for embedded PNGs (Laszlo Nyul) ++ Added typecasts to quiet compiler errors ++ Added more debugging info ++ ++Version 0.97 [January, 1998] ++ Removed PNG_USE_OWN_CRC capability ++ Relocated png_set_crc_action from pngrutil.c to pngrtran.c ++ Fixed typecasts of "new_key", etc. (Andreas Dilger) ++ Added RFC 1152 [sic] date support ++ Fixed bug in gamma handling of 4-bit grayscale ++ Added 2-bit grayscale gamma handling (Glenn R-P) ++ Added more typecasts. 65536L becomes (png_uint_32)65536L, etc. (Glenn R-P) ++ Minor corrections in libpng.txt ++ Added simple sRGB support (Glenn R-P) ++ Easier conditional compiling, e.g., ++ define PNG_READ/WRITE_NOT_FULLY_SUPPORTED; ++ all configurable options can be selected from command-line instead ++ of having to edit pngconf.h (Glenn R-P) ++ Fixed memory leak in pngwrite.c (free info_ptr->text) (Glenn R-P) ++ Added more conditions for png_do_background, to avoid changing ++ black pixels to background when a background is supplied and ++ no pixels are transparent ++ Repaired PNG_NO_STDIO behavior ++ Tested NODIV support and made it default behavior (Greg Roelofs) ++ Added "-m" option and PNGTEST_DEBUG_MEMORY to pngtest (John Bowler) ++ Regularized version numbering scheme and bumped shared-library major ++ version number to 2 to avoid problems with libpng 0.89 apps ++ (Greg Roelofs) ++ ++Version 0.98 [January, 1998] ++ Cleaned up some typos in libpng.txt and in code documentation ++ Fixed memory leaks in pCAL chunk processing (Glenn R-P and John Bowler) ++ Cosmetic change "display_gamma" to "screen_gamma" in pngrtran.c ++ Changed recommendation about file_gamma for PC images to .51 from .45, ++ in example.c and libpng.txt, added comments to distinguish between ++ screen_gamma, viewing_gamma, and display_gamma. ++ Changed all references to RFC1152 to read RFC1123 and changed the ++ PNG_TIME_RFC1152_SUPPORTED macro to PNG_TIME_RFC1123_SUPPORTED ++ Added png_invert_alpha capability (Glenn R-P -- suggestion by Jon Vincent) ++ Changed srgb_intent from png_byte to int to avoid compiler bugs ++ ++Version 0.99 [January 30, 1998] ++ Free info_ptr->text instead of end_info_ptr->text in pngread.c (John Bowler) ++ Fixed a longstanding "packswap" bug in pngtrans.c ++ Fixed some inconsistencies in pngconf.h that prevented compiling with ++ PNG_READ_GAMMA_SUPPORTED and PNG_READ_hIST_SUPPORTED undefined ++ Fixed some typos and made other minor rearrangement of libpng.txt (Andreas) ++ Changed recommendation about file_gamma for PC images to .50 from .51 in ++ example.c and libpng.txt, and changed file_gamma for sRGB images to .45 ++ Added a number of functions to access information from the png structure ++ png_get_image_height(), etc. (Glenn R-P, suggestion by Brad Pettit) ++ Added TARGET_MACOS similar to zlib-1.0.8 ++ Define PNG_ALWAYS_EXTERN when __MWERKS__ && WIN32 are defined ++ Added type casting to all png_malloc() function calls ++ ++Version 0.99a [January 31, 1998] ++ Added type casts and parentheses to all returns that return a value.(Tim W.) ++ ++Version 0.99b [February 4, 1998] ++ Added type cast png_uint_32 on malloc function calls where needed. ++ Changed type of num_hist from png_uint_32 to int (same as num_palette). ++ Added checks for rowbytes overflow, in case png_size_t is less than 32 bits. ++ Renamed makefile.elf to makefile.lnx. ++ ++Version 0.99c [February 7, 1998] ++ More type casting. Removed erroneous overflow test in pngmem.c. ++ Added png_buffered_memcpy() and png_buffered_memset(), apply them to rowbytes. ++ Added UNIX manual pages libpng.3 (incorporating libpng.txt) and png.5. ++ ++Version 0.99d [February 11, 1998] ++ Renamed "far_to_near()" "png_far_to_near()" ++ Revised libpng.3 ++ Version 99c "buffered" operations didn't work as intended. Replaced them ++ with png_memcpy_check() and png_memset_check(). ++ Added many "if (png_ptr == NULL) return" to quell compiler warnings about ++ unused png_ptr, mostly in pngget.c and pngset.c. ++ Check for overlength tRNS chunk present when indexed-color PLTE is read. ++ Cleaned up spelling errors in libpng.3/libpng.txt ++ Corrected a problem with png_get_tRNS() which returned undefined trans array ++ ++Version 0.99e [February 28, 1998] ++ Corrected png_get_tRNS() again. ++ Add parentheses for easier reading of pngget.c, fixed "||" should be "&&". ++ Touched up example.c to make more of it compileable, although the entire ++ file still can't be compiled (Willem van Schaik) ++ Fixed a bug in png_do_shift() (Bryan Tsai) ++ Added a space in png.h prototype for png_write_chunk_start() ++ Replaced pngtest.png with one created with zlib 1.1.1 ++ Changed pngtest to report PASS even when file size is different (Jean-loup G.) ++ Corrected some logic errors in png_do_invert_alpha() (Chris Patterson) ++ ++Version 0.99f [March 5, 1998] ++ Corrected a bug in pngpread() introduced in version 99c (Kevin Bracey) ++ Moved makefiles into a "scripts" directory, and added INSTALL instruction file ++ Added makefile.os2 and pngos2.def (A. Zabolotny) and makefile.s2x (W. Sebok) ++ Added pointers to "note on libpng versions" in makefile.lnx and README ++ Added row callback feature when reading and writing nonprogressive rows ++ and added a test of this feature in pngtest.c ++ Added user transform callbacks, with test of the feature in pngtest.c ++ ++Version 0.99g [March 6, 1998, morning] ++ Minor changes to pngtest.c to suppress compiler warnings. ++ Removed "beta" language from documentation. ++ ++Version 0.99h [March 6, 1998, evening] ++ Minor changes to previous minor changes to pngtest.c ++ Changed PNG_READ_NOT_FULLY_SUPPORTED to PNG_READ_TRANSFORMS_NOT_SUPPORTED ++ and added PNG_PROGRESSIVE_READ_NOT_SUPPORTED macro ++ Added user transform capability ++ ++Version 1.00 [March 7, 1998] ++ Changed several typedefs in pngrutil.c ++ Added makefile.wat (Pawel Mrochen), updated makefile.tc3 (Willem van Schaik) ++ Replaced "while(1)" with "for(;;)" ++ Added PNGARG() to prototypes in pngtest.c and removed some prototypes ++ Updated some of the makefiles (Tom Lane) ++ Changed some typedefs (s_start, etc.) in pngrutil.c ++ Fixed dimensions of "short_months" array in pngwrite.c ++ Replaced ansi2knr.c with the one from jpeg-v6 ++ ++Version 1.0.0 [March 8, 1998] ++ Changed name from 1.00 to 1.0.0 (Adam Costello) ++ Added smakefile.ppc (with SCOPTIONS.ppc) for Amiga PPC (Andreas Kleinert) ++ ++Version 1.0.0a [March 9, 1998] ++ Fixed three bugs in pngrtran.c to make gamma+background handling consistent ++ (Greg Roelofs) ++ Changed format of the PNG_LIBPNG_VER integer to xyyzz instead of xyz ++ for major, minor, and bugfix releases. This is 10001. (Adam Costello, ++ Tom Lane) ++ Make months range from 1-12 in png_convert_to_rfc1123 ++ ++Version 1.0.0b [March 13, 1998] ++ Quieted compiler complaints about two empty "for" loops in pngrutil.c ++ Minor changes to makefile.s2x ++ Removed #ifdef/#endif around a png_free() in pngread.c ++ ++Version 1.0.1 [March 14, 1998] ++ Changed makefile.s2x to reduce security risk of using a relative pathname ++ Fixed some typos in the documentation (Greg). ++ Fixed a problem with value of "channels" returned by png_read_update_info() ++ ++Version 1.0.1a [April 21, 1998] ++ Optimized Paeth calculations by replacing abs() function calls with intrinsics ++ plus other loop optimizations. Improves avg decoding speed by about 20%. ++ Commented out i386istic "align" compiler flags in makefile.lnx. ++ Reduced the default warning level in some makefiles, to make them consistent. ++ Removed references to IJG and JPEG in the ansi2knr.c copyright statement. ++ Fixed a bug in png_do_strip_filler with XXRRGGBB => RRGGBB transformation. ++ Added grayscale and 16-bit capability to png_do_read_filler(). ++ Fixed a bug in pngset.c, introduced in version 0.99c, that sets rowbytes ++ too large when writing an image with bit_depth < 8 (Bob Dellaca). ++ Corrected some bugs in the experimental weighted filtering heuristics. ++ Moved a misplaced pngrutil code block that truncates tRNS if it has more ++ than num_palette entries -- test was done before num_palette was defined. ++ Fixed a png_convert_to_rfc1123() bug that converts day 31 to 0 (Steve Eddins). ++ Changed compiler flags in makefile.wat for better optimization ++ (Pawel Mrochen). ++ ++Version 1.0.1b [May 2, 1998] ++ Relocated png_do_gray_to_rgb() within png_do_read_transformations() (Greg). ++ Relocated the png_composite macros from pngrtran.c to png.h (Greg). ++ Added makefile.sco (contributed by Mike Hopkirk). ++ Fixed two bugs (missing definitions of "istop") introduced in libpng-1.0.1a. ++ Fixed a bug in pngrtran.c that would set channels=5 under some circumstances. ++ More work on the Paeth-filtering, achieving imperceptible speedup ++ (A Kleinert). ++ More work on loop optimization which may help when compiled with C++ ++ compilers. ++ Added warnings when people try to use transforms they've defined out. ++ Collapsed 4 "i" and "c" loops into single "i" loops in pngrtran and pngwtran. ++ Revised paragraph about png_set_expand() in libpng.txt and libpng.3 (Greg) ++ ++Version 1.0.1c [May 11, 1998] ++ Fixed a bug in pngrtran.c (introduced in libpng-1.0.1a) where the masks for ++ filler bytes should have been 0xff instead of 0xf. ++ Added max_pixel_depth=32 in pngrutil.c when using FILLER with palette images. ++ Moved PNG_WRITE_WEIGHTED_FILTER_SUPPORTED and PNG_WRITE_FLUSH_SUPPORTED ++ out of the PNG_WRITE_TRANSFORMS_NOT_SUPPORTED block of pngconf.h ++ Added "PNG_NO_WRITE_TRANSFORMS" etc., as alternatives for *_NOT_SUPPORTED, ++ for consistency, in pngconf.h ++ Added individual "ifndef PNG_NO_[CAPABILITY]" in pngconf.h to make it easier ++ to remove unwanted capabilities via the compile line ++ Made some corrections to grammar (which, it's) in documentation (Greg). ++ Corrected example.c, use of row_pointers in png_write_image(). ++ ++Version 1.0.1d [May 24, 1998] ++ Corrected several statements that used side effects illegally in pngrutil.c ++ and pngtrans.c, that were introduced in version 1.0.1b ++ Revised png_read_rows() to avoid repeated if-testing for NULL (A Kleinert) ++ More corrections to example.c, use of row_pointers in png_write_image() ++ and png_read_rows(). ++ Added pngdll.mak and pngdef.pas to scripts directory, contributed by ++ Bob Dellaca, to make a png32bd.dll with Borland C++ 4.5 ++ Fixed error in example.c with png_set_text: num_text is 3, not 2 (Guido V.) ++ Changed several loops from count-down to count-up, for consistency. ++ ++Version 1.0.1e [June 6, 1998] ++ Revised libpng.txt and libpng.3 description of png_set_read|write_fn(), and ++ added warnings when people try to set png_read_fn and png_write_fn in ++ the same structure. ++ Added a test such that png_do_gamma will be done when num_trans==0 ++ for truecolor images that have defined a background. This corrects an ++ error that was introduced in libpng-0.90 that can cause gamma processing ++ to be skipped. ++ Added tests in png.h to include "trans" and "trans_values" in structures ++ when PNG_READ_BACKGROUND_SUPPORTED or PNG_READ_EXPAND_SUPPORTED is defined. ++ Add png_free(png_ptr->time_buffer) in png_destroy_read_struct() ++ Moved png_convert_to_rfc_1123() from pngwrite.c to png.c ++ Added capability for user-provided malloc_fn() and free_fn() functions, ++ and revised pngtest.c to demonstrate their use, replacing the ++ PNGTEST_DEBUG_MEM feature. ++ Added makefile.w32, for Microsoft C++ 4.0 and later (Tim Wegner). ++ ++Version 1.0.2 [June 14, 1998] ++ Fixed two bugs in makefile.bor . ++ ++Version 1.0.2a [December 30, 1998] ++ Replaced and extended code that was removed from png_set_filler() in 1.0.1a. ++ Fixed a bug in png_do_filler() that made it fail to write filler bytes in ++ the left-most pixel of each row (Kevin Bracey). ++ Changed "static pngcharp tIME_string" to "static char tIME_string[30]" ++ in pngtest.c (Duncan Simpson). ++ Fixed a bug in pngtest.c that caused pngtest to try to write a tIME chunk ++ even when no tIME chunk was present in the source file. ++ Fixed a problem in pngrutil.c: gray_to_rgb didn't always work with 16-bit. ++ Fixed a problem in png_read_push_finish_row(), which would not skip some ++ passes that it should skip, for images that are less than 3 pixels high. ++ Interchanged the order of calls to png_do_swap() and png_do_shift() ++ in pngwtran.c (John Cromer). ++ Added #ifdef PNG_DEBUG/#endif surrounding use of PNG_DEBUG in png.h . ++ Changed "bad adaptive filter type" from error to warning in pngrutil.c . ++ Fixed a documentation error about default filtering with 8-bit indexed-color. ++ Separated the PNG_NO_STDIO macro into PNG_NO_STDIO and PNG_NO_CONSOLE_IO ++ (L. Peter Deutsch). ++ Added png_set_rgb_to_gray() and png_get_rgb_to_gray_status() functions. ++ Added png_get_copyright() and png_get_header_version() functions. ++ Revised comments on png_set_progressive_read_fn() in libpng.txt and example.c ++ Added information about debugging in libpng.txt and libpng.3 . ++ Changed "ln -sf" to "ln -s -f" in makefile.s2x, makefile.lnx, and ++ makefile.sco. ++ Removed lines after Dynamic Dependencies" in makefile.aco . ++ Revised makefile.dec to make a shared library (Jeremie Petit). ++ Removed trailing blanks from all files. ++ ++Version 1.0.2a [January 6, 1999] ++ Removed misplaced #endif and #ifdef PNG_NO_EXTERN near the end of png.h ++ Added "if" tests to silence complaints about unused png_ptr in png.h and png.c ++ Changed "check_if_png" function in example.c to return true (nonzero) if PNG. ++ Changed libpng.txt to demonstrate png_sig_cmp() instead of png_check_sig() ++ which is obsolete. ++ ++Version 1.0.3 [January 14, 1999] ++ Added makefile.hux, for Hewlett Packard HPUX 10.20 and 11.00 (Jim Rice) ++ Added a statement of Y2K compliance in png.h, libpng.3, and Y2KINFO. ++ ++Version 1.0.3a [August 12, 1999] ++ Added check for PNG_READ_INTERLACE_SUPPORTED in pngread.c; issue a warning ++ if an attempt is made to read an interlaced image when it's not supported. ++ Added check if png_ptr->trans is defined before freeing it in pngread.c ++ Modified the Y2K statement to include versions back to version 0.71 ++ Fixed a bug in the check for valid IHDR bit_depth/color_types in pngrutil.c ++ Modified makefile.wat (added -zp8 flag, ".symbolic", changed some comments) ++ Replaced leading blanks with tab characters in makefile.hux ++ Changed "dworkin.wustl.edu" to "ccrc.wustl.edu" in various documents. ++ Changed (float)red and (float)green to (double)red, (double)green ++ in png_set_rgb_to_gray() to avoid "promotion" problems in AIX. ++ Fixed a bug in pngconf.h that omitted when PNG_DEBUG==0 (K Bracey). ++ Reformatted libpng.3 and libpngpf.3 with proper fonts (script by J. vanZandt). ++ Updated documentation to refer to the PNG-1.2 specification. ++ Removed ansi2knr.c and left pointers to the latest source for ansi2knr.c ++ in makefile.knr, INSTALL, and README (L. Peter Deutsch) ++ Fixed bugs in calculation of the length of rowbytes when adding alpha ++ channels to 16-bit images, in pngrtran.c (Chris Nokleberg) ++ Added function png_set_user_transform_info() to store user_transform_ptr, ++ user_depth, and user_channels into the png_struct, and a function ++ png_get_user_transform_ptr() to retrieve the pointer (Chris Nokleberg) ++ Added function png_set_empty_plte_permitted() to make libpng useable ++ in MNG applications. ++ Corrected the typedef for png_free_ptr in png.h (Jesse Jones). ++ Correct gamma with srgb is 45455 instead of 45000 in pngrutil.c, to be ++ consistent with PNG-1.2, and allow variance of 500 before complaining. ++ Added assembler code contributed by Intel in file pngvcrd.c and modified ++ makefile.w32 to use it (Nirav Chhatrapati, INTEL Corporation, ++ Gilles Vollant) ++ Changed "ln -s -f" to "ln -f -s" in the makefiles to make Solaris happy. ++ Added some aliases for png_set_expand() in pngrtran.c, namely ++ png_set_expand_PLTE(), png_set_expand_depth(), and png_set_expand_tRNS() ++ (Greg Roelofs, in "PNG: The Definitive Guide"). ++ Added makefile.beo for BEOS on X86, contributed by Sander Stok. ++ ++Version 1.0.3b [August 26, 1999] ++ Replaced 2147483647L several places with PNG_MAX_UINT macro, defined in png.h ++ Changed leading blanks to tabs in all makefiles. ++ Define PNG_USE_PNGVCRD in makefile.w32, to get MMX assembler code. ++ Made alternate versions of png_set_expand() in pngrtran.c, namely ++ png_set_gray_1_2_4_to_8, png_set_palette_to_rgb, and png_set_tRNS_to_alpha ++ (Greg Roelofs, in "PNG: The Definitive Guide"). Deleted the 1.0.3a aliases. ++ Relocated start of 'extern "C"' block in png.h so it doesn't include pngconf.h ++ Revised calculation of num_blocks in pngmem.c to avoid a potentially ++ negative shift distance, whose results are undefined in the C language. ++ Added a check in pngset.c to prevent writing multiple tIME chunks. ++ Added a check in pngwrite.c to detect invalid small window_bits sizes. ++ ++Version 1.0.3d [September 4, 1999] ++ Fixed type casting of igamma in pngrutil.c ++ Added new png_expand functions to scripts/pngdef.pas and pngos2.def ++ Added a demo read_user_transform_fn that examines the row filters in pngtest.c ++ ++Version 1.0.4 [September 24, 1999, not distributed publicly] ++ Define PNG_ALWAYS_EXTERN in pngconf.h if __STDC__ is defined ++ Delete #define PNG_INTERNAL and include "png.h" from pngasmrd.h ++ Made several minor corrections to pngtest.c ++ Renamed the makefiles with longer but more user friendly extensions. ++ Copied the PNG copyright and license to a separate LICENSE file. ++ Revised documentation, png.h, and example.c to remove reference to ++ "viewing_gamma" which no longer appears in the PNG specification. ++ Revised pngvcrd.c to use MMX code for interlacing only on the final pass. ++ Updated pngvcrd.c to use the faster C filter algorithms from libpng-1.0.1a ++ Split makefile.win32vc into two versions, makefile.vcawin32 (uses MMX ++ assembler code) and makefile.vcwin32 (doesn't). ++ Added a CPU timing report to pngtest.c (enabled by defining PNGTEST_TIMING) ++ Added a copy of pngnow.png to the distribution. ++ ++Version 1.0.4a [September 25, 1999] ++ Increase max_pixel_depth in pngrutil.c if a user transform needs it. ++ Changed several division operations to right-shifts in pngvcrd.c ++ ++Version 1.0.4b [September 30, 1999] ++ Added parentheses in line 3732 of pngvcrd.c ++ Added a comment in makefile.linux warning about buggy -O3 in pgcc 2.95.1 ++ ++Version 1.0.4c [October 1, 1999] ++ Added a "png_check_version" function in png.c and pngtest.c that will generate ++ a helpful compiler error if an old png.h is found in the search path. ++ Changed type of png_user_transform_depth|channels from int to png_byte. ++ Added "Libpng is OSI Certified Open Source Software" statement to png.h ++ ++Version 1.0.4d [October 6, 1999] ++ Changed 0.45 to 0.45455 in png_set_sRGB() ++ Removed unused PLTE entries from pngnow.png ++ Re-enabled some parts of pngvcrd.c (png_combine_row) that work properly. ++ ++Version 1.0.4e [October 10, 1999] ++ Fixed sign error in pngvcrd.c (Greg Roelofs) ++ Replaced some instances of memcpy with simple assignments in pngvcrd (GR-P) ++ ++Version 1.0.4f [October 15, 1999] ++ Surrounded example.c code with #if 0 .. #endif to prevent people from ++ inadvertently trying to compile it. ++ Changed png_get_header_version() from a function to a macro in png.h ++ Added type casting mostly in pngrtran.c and pngwtran.c ++ Removed some pointless "ptr = NULL" in pngmem.c ++ Added a "contrib" directory containing the source code from Greg's book. ++ ++Version 1.0.5 [October 15, 1999] ++ Minor editing of the INSTALL and README files. ++ ++Version 1.0.5a [October 23, 1999] ++ Added contrib/pngsuite and contrib/pngminus (Willem van Schaik) ++ Fixed a typo in the png_set_sRGB() function call in example.c (Jan Nijtmans) ++ Further optimization and bugfix of pngvcrd.c ++ Revised pngset.c so that it does not allocate or free memory in the user's ++ text_ptr structure. Instead, it makes its own copy. ++ Created separate write_end_info_struct in pngtest.c for a more severe test. ++ Added code in pngwrite.c to free info_ptr->text[i].key to stop a memory leak. ++ ++Version 1.0.5b [November 23, 1999] ++ Moved PNG_FLAG_HAVE_CHUNK_HEADER, PNG_FLAG_BACKGROUND_IS_GRAY and ++ PNG_FLAG_WROTE_tIME from flags to mode. ++ Added png_write_info_before_PLTE() function. ++ Fixed some typecasting in contrib/gregbook/*.c ++ Updated scripts/makevms.com and added makevms.com to contrib/gregbook ++ and contrib/pngminus (Martin Zinser) ++ ++Version 1.0.5c [November 26, 1999] ++ Moved png_get_header_version from png.h to png.c, to accommodate ansi2knr. ++ Removed all global arrays (according to PNG_NO_GLOBAL_ARRAYS macro), to ++ accommodate making DLL's: Moved usr_png_ver from global variable to function ++ png_get_header_ver() in png.c. Moved png_sig to png_sig_bytes in png.c and ++ eliminated use of png_sig in pngwutil.c. Moved the various png_CHNK arrays ++ into pngtypes.h. Eliminated use of global png_pass arrays. Declared the ++ png_CHNK and png_pass arrays to be "const". Made the global arrays ++ available to applications (although none are used in libpng itself) when ++ PNG_NO_GLOBAL_ARRAYS is not defined or when PNG_GLOBAL_ARRAYS is defined. ++ Removed some extraneous "-I" from contrib/pngminus/makefile.std ++ Changed the PNG_sRGB_INTENT macros in png.h to be consistent with PNG-1.2. ++ Change PNG_SRGB_INTENT to PNG_sRGB_INTENT in libpng.txt and libpng.3 ++ ++Version 1.0.5d [November 29, 1999] ++ Add type cast (png_const_charp) two places in png.c ++ Eliminated pngtypes.h; use macros instead to declare PNG_CHNK arrays. ++ Renamed "PNG_GLOBAL_ARRAYS" to "PNG_USE_GLOBAL_ARRAYS" and made available ++ to applications a macro "PNG_USE_LOCAL_ARRAYS". ++ comment out (with #ifdef) all the new declarations when ++ PNG_USE_GLOBAL_ARRAYS is defined. ++ Added PNG_EXPORT_VAR macro to accommodate making DLL's. ++ ++Version 1.0.5e [November 30, 1999] ++ Added iCCP, iTXt, and sPLT support; added "lang" member to the png_text ++ structure; refactored the inflate/deflate support to make adding new chunks ++ with trailing compressed parts easier in the future, and added new functions ++ png_free_iCCP, png_free_pCAL, png_free_sPLT, png_free_text, png_get_iCCP, ++ png_get_spalettes, png_set_iCCP, png_set_spalettes (Eric S. Raymond). ++ NOTE: Applications that write text chunks MUST define png_text->lang ++ before calling png_set_text(). It must be set to NULL if you want to ++ write tEXt or zTXt chunks. If you want your application to be able to ++ run with older versions of libpng, use ++ ++ #ifdef PNG_iTXt_SUPPORTED ++ png_text[i].lang = NULL; ++ #endif ++ ++ Changed png_get_oFFs() and png_set_oFFs() to use signed rather than unsigned ++ offsets (Eric S. Raymond). ++ Combined PNG_READ_cHNK_SUPPORTED and PNG_WRITE_cHNK_SUPPORTED macros into ++ PNG_cHNK_SUPPORTED and combined the three types of PNG_text_SUPPORTED ++ macros, leaving the separate macros also available. ++ Removed comments on #endifs at the end of many short, non-nested #if-blocks. ++ ++Version 1.0.5f [December 6, 1999] ++ Changed makefile.solaris to issue a warning about potential problems when ++ the ucb "ld" is in the path ahead of the ccs "ld". ++ Removed "- [date]" from the "synopsis" line in libpng.3 and libpngpf.3. ++ Added sCAL chunk support (Eric S. Raymond). ++ ++Version 1.0.5g [December 7, 1999] ++ Fixed "png_free_spallettes" typo in png.h ++ Added code to handle new chunks in pngpread.c ++ Moved PNG_CHNK string macro definitions outside of PNG_NO_EXTERN block ++ Added "translated_key" to png_text structure and png_write_iTXt(). ++ Added code in pngwrite.c to work around a newly discovered zlib bug. ++ ++Version 1.0.5h [December 10, 1999] ++ NOTE: regarding the note for version 1.0.5e, the following must also ++ be included in your code: ++ png_text[i].translated_key = NULL; ++ Unknown chunk handling is now supported. ++ Option to eliminate all floating point support was added. Some new ++ fixed-point functions such as png_set_gAMA_fixed() were added. ++ Expanded tabs and removed trailing blanks in source files. ++ ++Version 1.0.5i [December 13, 1999] ++ Added some type casts to silence compiler warnings. ++ Renamed "png_free_spalette" to "png_free_spalettes" for consistency. ++ Removed leading blanks from a #define in pngvcrd.c ++ Added some parameters to the new png_set_keep_unknown_chunks() function. ++ Added a test for up->location != 0 in the first instance of writing ++ unknown chunks in pngwrite.c ++ Changed "num" to "i" in png_free_spalettes() and png_free_unknowns() to ++ prevent recursion. ++ Added png_free_hIST() function. ++ Various patches to fix bugs in the sCAL and integer cHRM processing, ++ and to add some convenience macros for use with sCAL. ++ ++Version 1.0.5j [December 21, 1999] ++ Changed "unit" parameter of png_write_sCAL from png_byte to int, to work ++ around buggy compilers. ++ Added new type "png_fixed_point" for integers that hold float*100000 values ++ Restored backward compatibility of tEXt/zTXt chunk processing: ++ Restored the first four members of png_text to the same order as v.1.0.5d. ++ Added members "lang_key" and "itxt_length" to png_text struct. Set ++ text_length=0 when "text" contains iTXt data. Use the "compression" ++ member to distinguish among tEXt/zTXt/iTXt types. Added ++ PNG_ITXT_COMPRESSION_NONE (1) and PNG_ITXT_COMPRESSION_zTXt(2) macros. ++ The "Note" above, about backward incompatibility of libpng-1.0.5e, no ++ longer applies. ++ Fixed png_read|write_iTXt() to read|write parameters in the right order, ++ and to write the iTXt chunk after IDAT if it appears in the end_ptr. ++ Added pnggccrd.c, version of pngvcrd.c Intel assembler for gcc (Greg Roelofs) ++ Reversed the order of trying to write floating-point and fixed-point gAMA. ++ ++Version 1.0.5k [December 27, 1999] ++ Added many parentheses, e.g., "if (a && b & c)" becomes "if (a && (b & c))" ++ Added png_handle_as_unknown() function (Glenn) ++ Added png_free_chunk_list() function and chunk_list and num_chunk_list members ++ of png_ptr. ++ Eliminated erroneous warnings about multiple sPLT chunks and sPLT-after-PLTE. ++ Fixed a libpng-1.0.5h bug in pngrutil.c that was issuing erroneous warnings ++ about ignoring incorrect gAMA with sRGB (gAMA was in fact not ignored) ++ Added png_free_tRNS(); png_set_tRNS() now malloc's its own trans array (ESR). ++ Define png_get_int_32 when oFFs chunk is supported as well as when pCAL is. ++ Changed type of proflen from png_int_32 to png_uint_32 in png_get_iCCP(). ++ ++Version 1.0.5l [January 1, 2000] ++ Added functions png_set_read_user_chunk_fn() and png_get_user_chunk_ptr() ++ for setting a callback function to handle unknown chunks and for ++ retrieving the associated user pointer (Glenn). ++ ++Version 1.0.5m [January 7, 2000] ++ Added high-level functions png_read_png(), png_write_png(), png_free_pixels(). ++ ++Version 1.0.5n [January 9, 2000] ++ Added png_free_PLTE() function, and modified png_set_PLTE() to malloc its ++ own memory for info_ptr->palette. This makes it safe for the calling ++ application to free its copy of the palette any time after it calls ++ png_set_PLTE(). ++ ++Version 1.0.5o [January 20, 2000] ++ Cosmetic changes only (removed some trailing blanks and TABs) ++ ++Version 1.0.5p [January 31, 2000] ++ Renamed pngdll.mak to makefile.bd32 ++ Cosmetic changes in pngtest.c ++ ++Version 1.0.5q [February 5, 2000] ++ Relocated the makefile.solaris warning about PATH problems. ++ Fixed pngvcrd.c bug by pushing/popping registers in mmxsupport (Bruce Oberg) ++ Revised makefile.gcmmx ++ Added PNG_SETJMP_SUPPORTED, PNG_SETJMP_NOT_SUPPORTED, and PNG_ABORT() macros ++ ++Version 1.0.5r [February 7, 2000] ++ Removed superfluous prototype for png_get_itxt from png.h ++ Fixed a bug in pngrtran.c that improperly expanded the background color. ++ Return *num_text=0 from png_get_text() when appropriate, and fix documentation ++ of png_get_text() in libpng.txt/libpng.3. ++ ++Version 1.0.5s [February 18, 2000] ++ Added "png_jmp_env()" macro to pngconf.h, to help people migrate to the ++ new error handler that's planned for the next libpng release, and changed ++ example.c, pngtest.c, and contrib programs to use this macro. ++ Revised some of the DLL-export macros in pngconf.h (Greg Roelofs) ++ Fixed a bug in png_read_png() that caused it to fail to expand some images ++ that it should have expanded. ++ Fixed some mistakes in the unused and undocumented INCH_CONVERSIONS functions ++ in pngget.c ++ Changed the allocation of palette, history, and trans arrays back to ++ the version 1.0.5 method (linking instead of copying) which restores ++ backward compatibility with version 1.0.5. Added some remarks about ++ that in example.c. Added "free_me" member to info_ptr and png_ptr ++ and added png_free_data() function. ++ Updated makefile.linux and makefile.gccmmx to make directories conditionally. ++ Made cosmetic changes to pngasmrd.h ++ Added png_set_rows() and png_get_rows(), for use with png_read|write_png(). ++ Modified png_read_png() to allocate info_ptr->row_pointers only if it ++ hasn't already been allocated. ++ ++Version 1.0.5t [March 4, 2000] ++ Changed png_jmp_env() migration aiding macro to png_jmpbuf(). ++ Fixed "interlace" typo (should be "interlaced") in contrib/gregbook/read2-x.c ++ Fixed bug with use of PNG_BEFORE_IHDR bit in png_ptr->mode, introduced when ++ PNG_FLAG_HAVE_CHUNK_HEADER was moved into png_ptr->mode in version 1.0.5b ++ Files in contrib/gregbook were revised to use png_jmpbuf() and to select ++ a 24-bit visual if one is available, and to allow abbreviated options. ++ Files in contrib/pngminus were revised to use the png_jmpbuf() macro. ++ Removed spaces in makefile.linux and makefile.gcmmx, introduced in 1.0.5s ++ ++Version 1.0.5u [March 5, 2000] ++ Simplified the code that detects old png.h in png.c and pngtest.c ++ Renamed png_spalette (_p, _pp) to png_sPLT_t (_tp, _tpp) ++ Increased precision of rgb_to_gray calculations from 8 to 15 bits and ++ added png_set_rgb_to_gray_fixed() function. ++ Added makefile.bc32 (32-bit Borland C++, C mode) ++ ++Version 1.0.5v [March 11, 2000] ++ Added some parentheses to the png_jmpbuf macro definition. ++ Updated references to the zlib home page, which has moved to freesoftware.com. ++ Corrected bugs in documentation regarding png_read_row() and png_write_row(). ++ Updated documentation of png_rgb_to_gray calculations in libpng.3/libpng.txt. ++ Renamed makefile.borland,turboc3 back to makefile.bor,tc3 as in version 1.0.3, ++ revised borland makefiles; added makefile.ibmvac3 and makefile.gcc (Cosmin) ++ ++Version 1.0.6 [March 20, 2000] ++ Minor revisions of makefile.bor, libpng.txt, and gregbook/rpng2-win.c ++ Added makefile.sggcc (SGI IRIX with gcc) ++ ++Version 1.0.6d [April 7, 2000] ++ Changed sprintf() to strcpy() in png_write_sCAL_s() to work without STDIO ++ Added data_length parameter to png_decompress_chunk() function ++ Revised documentation to remove reference to abandoned png_free_chnk functions ++ Fixed an error in png_rgb_to_gray_fixed() ++ Revised example.c, usage of png_destroy_write_struct(). ++ Renamed makefile.ibmvac3 to makefile.ibmc, added libpng.icc IBM project file ++ Added a check for info_ptr->free_me&PNG_FREE_TEXT when freeing text in png.c ++ Simplify png_sig_bytes() function to remove use of non-ISO-C strdup(). ++ ++Version 1.0.6e [April 9, 2000] ++ Added png_data_freer() function. ++ In the code that checks for over-length tRNS chunks, added check of ++ info_ptr->num_trans as well as png_ptr->num_trans (Matthias Benckmann) ++ Minor revisions of libpng.txt/libpng.3. ++ Check for existing data and free it if the free_me flag is set, in png_set_*() ++ and png_handle_*(). ++ Only define PNG_WEIGHTED_FILTERS_SUPPORTED when PNG_FLOATING_POINT_SUPPORTED ++ is defined. ++ Changed several instances of PNG_NO_CONSOLE_ID to PNG_NO_STDIO in pngrutil.c ++ and mentioned the purposes of the two macros in libpng.txt/libpng.3. ++ ++Version 1.0.6f [April 14, 2000] ++ Revised png_set_iCCP() and png_set_rows() to avoid prematurely freeing data. ++ Add checks in png_set_text() for NULL members of the input text structure. ++ Revised libpng.txt/libpng.3. ++ Removed superfluous prototype for png_set_iTXt from png.h ++ Removed "else" from pngread.c, after png_error(), and changed "0" to "length". ++ Changed several png_errors about malformed ancillary chunks to png_warnings. ++ ++Version 1.0.6g [April 24, 2000] ++ Added png_pass-* arrays to pnggccrd.c when PNG_USE_LOCAL_ARRAYS is defined. ++ Relocated paragraph about png_set_background() in libpng.3/libpng.txt ++ and other revisions (Matthias Benckmann) ++ Relocated info_ptr->free_me, png_ptr->free_me, and other info_ptr and ++ png_ptr members to restore binary compatibility with libpng-1.0.5 ++ (breaks compatibility with libpng-1.0.6). ++ ++Version 1.0.6h [April 24, 2000] ++ Changed shared library so-number pattern from 2.x.y.z to xy.z (this builds ++ libpng.so.10 & libpng.so.10.6h instead of libpng.so.2 & libpng.so.2.1.0.6h) ++ This is a temporary change for test purposes. ++ ++Version 1.0.6i [May 2, 2000] ++ Rearranged some members at the end of png_info and png_struct, to put ++ unknown_chunks_num and free_me within the original size of the png_structs ++ and free_me, png_read_user_fn, and png_free_fn within the original png_info, ++ because some old applications allocate the structs directly instead of ++ using png_create_*(). ++ Added documentation of user memory functions in libpng.txt/libpng.3 ++ Modified png_read_png so that it will use user_allocated row_pointers ++ if present, unless free_me directs that it be freed, and added description ++ of the use of png_set_rows() and png_get_rows() in libpng.txt/libpng.3. ++ Added PNG_LEGACY_SUPPORTED macro, and #ifdef out all new (since version ++ 1.00) members of png_struct and png_info, to regain binary compatibility ++ when you define this macro. Capabilities lost in this event ++ are user transforms (new in version 1.0.0),the user transform pointer ++ (new in version 1.0.2), rgb_to_gray (new in 1.0.5), iCCP, sCAL, sPLT, ++ the high-level interface, and unknown chunks support (all new in 1.0.6). ++ This was necessary because of old applications that allocate the structs ++ directly as authors were instructed to do in libpng-0.88 and earlier, ++ instead of using png_create_*(). ++ Added modes PNG_CREATED_READ_STRUCT and PNG_CREATED_WRITE_STRUCT which ++ can be used to detect codes that directly allocate the structs, and ++ code to check these modes in png_read_init() and png_write_init() and ++ generate a libpng error if the modes aren't set and PNG_LEGACY_SUPPORTED ++ was not defined. ++ Added makefile.intel and updated makefile.watcom (Pawel Mrochen) ++ ++Version 1.0.6j [May 3, 2000] ++ Overloaded png_read_init() and png_write_init() with macros that convert ++ calls to png_read_init_2() or png_write_init_2() that check the version ++ and structure sizes. ++ ++Version 1.0.7beta11 [May 7, 2000] ++ Removed the new PNG_CREATED_READ_STRUCT and PNG_CREATED_WRITE_STRUCT modes ++ which are no longer used. ++ Eliminated the three new members of png_text when PNG_LEGACY_SUPPORTED is ++ defined or when neither PNG_READ_iTXt_SUPPORTED nor PNG_WRITE_iTXt_SUPPORTED ++ is defined. ++ Made PNG_NO_READ|WRITE_iTXt the default setting, to avoid memory ++ overrun when old applications fill the info_ptr->text structure directly. ++ Added PNGAPI macro, and added it to the definitions of all exported functions. ++ Relocated version macro definitions ahead of the includes of zlib.h and ++ pngconf.h in png.h. ++ ++Version 1.0.7beta12 [May 12, 2000] ++ Revised pngset.c to avoid a problem with expanding the png_debug macro. ++ Deleted some extraneous defines from pngconf.h ++ Made PNG_NO_CONSOLE_IO the default condition when PNG_BUILD_DLL is defined. ++ Use MSC _RPTn debugging instead of fprintf if _MSC_VER is defined. ++ Added png_access_version_number() function. ++ Check for mask&PNG_FREE_CHNK (for TEXT, SCAL, PCAL) in png_free_data(). ++ Expanded libpng.3/libpng.txt information about png_data_freer(). ++ ++Version 1.0.7beta14 [May 17, 2000] (beta13 was not published) ++ Changed pnggccrd.c and pngvcrd.c to handle bad adaptive filter types as ++ warnings instead of errors, as pngrutil.c does. ++ Set the PNG_INFO_IDAT valid flag in png_set_rows() so png_write_png() ++ will actually write IDATs. ++ Made the default PNG_USE_LOCAL_ARRAYS depend on PNG_DLL instead of WIN32. ++ Make png_free_data() ignore its final parameter except when freeing data ++ that can have multiple instances (text, sPLT, unknowns). ++ Fixed a new bug in png_set_rows(). ++ Removed info_ptr->valid tests from png_free_data(), as in version 1.0.5. ++ Added png_set_invalid() function. ++ Fixed incorrect illustrations of png_destroy_write_struct() in example.c. ++ ++Version 1.0.7beta15 [May 30, 2000] ++ Revised the deliberately erroneous Linux setjmp code in pngconf.h to produce ++ fewer error messages. ++ Rearranged checks for Z_OK to check the most likely path first in pngpread.c ++ and pngwutil.c. ++ Added checks in pngtest.c for png_create_*() returning NULL, and mentioned ++ in libpng.txt/libpng.3 the need for applications to check this. ++ Changed names of png_default_*() functions in pngtest to pngtest_*(). ++ Changed return type of png_get_x|y_offset_*() from png_uint_32 to png_int_32. ++ Fixed some bugs in the unused PNG_INCH_CONVERSIONS functions in pngget.c ++ Set each pointer to NULL after freeing it in png_free_data(). ++ Worked around a problem in pngconf.h; AIX's strings.h defines an "index" ++ macro that conflicts with libpng's png_color_16.index. (Dimitri ++ Papadapoulos) ++ Added "msvc" directory with MSVC++ project files (Simon-Pierre Cadieux). ++ ++Version 1.0.7beta16 [June 4, 2000] ++ Revised the workaround of AIX string.h "index" bug. ++ Added a check for overlength PLTE chunk in pngrutil.c. ++ Added PNG_NO_POINTER_INDEXING macro to use array-indexing instead of pointer ++ indexing in pngrutil.c and pngwutil.c to accommodate a buggy compiler. ++ Added a warning in png_decompress_chunk() when it runs out of data, e.g. ++ when it tries to read an erroneous PhotoShop iCCP chunk. ++ Added PNG_USE_DLL macro. ++ Revised the copyright/disclaimer/license notice. ++ Added contrib/msvctest directory ++ ++Version 1.0.7rc1 [June 9, 2000] ++ Corrected the definition of PNG_TRANSFORM_INVERT_ALPHA (0x0400 not 0x0200) ++ Added contrib/visupng directory (Willem van Schaik) ++ ++Version 1.0.7beta18 [June 23, 2000] ++ Revised PNGAPI definition, and pngvcrd.c to work with __GCC__ ++ and do not redefine PNGAPI if it is passed in via a compiler directive. ++ Revised visupng/PngFile.c to remove returns from within the Try block. ++ Removed leading underscores from "_PNG_H" and "_PNG_SAVE_BSD_SOURCE" macros. ++ Updated contrib/visupng/cexcept.h to version 1.0.0. ++ Fixed bugs in pngwrite.c and pngwutil.c that prevented writing iCCP chunks. ++ ++Version 1.0.7rc2 [June 28, 2000] ++ Updated license to include disclaimers required by UCITA. ++ Fixed "DJBPP" typo in pnggccrd.c introduced in beta18. ++ ++Version 1.0.7 [July 1, 2000] ++ Revised the definition of "trans_values" in libpng.3/libpng.txt ++ ++Version 1.0.8beta1 [July 8, 2000] ++ Added png_free(png_ptr, key) two places in pngpread.c to stop memory leaks. ++ Changed PNG_NO_STDIO to PNG_NO_CONSOLE_IO, several places in pngrutil.c and ++ pngwutil.c. ++ Changed PNG_EXPORT_VAR to use PNG_IMPEXP, in pngconf.h. ++ Removed unused "#include " from png.c ++ Added WindowsCE support. ++ Revised pnggccrd.c to work with gcc-2.95.2 and in the Cygwin environment. ++ ++Version 1.0.8beta2 [July 10, 2000] ++ Added project files to the wince directory and made further revisions ++ of pngtest.c, pngrio.c, and pngwio.c in support of WindowsCE. ++ ++Version 1.0.8beta3 [July 11, 2000] ++ Only set the PNG_FLAG_FREE_TRNS or PNG_FREE_TRNS flag in png_handle_tRNS() ++ for indexed-color input files to avoid potential double-freeing trans array ++ under some unusual conditions; problem was introduced in version 1.0.6f. ++ Further revisions to pngtest.c and files in the wince subdirectory. ++ ++Version 1.0.8beta4 [July 14, 2000] ++ Added the files pngbar.png and pngbar.jpg to the distribution. ++ Added makefile.cygwin, and cygwin support in pngconf.h ++ Added PNG_NO_ZALLOC_ZERO macro (makes png_zalloc skip zeroing memory) ++ ++Version 1.0.8rc1 [July 16, 2000] ++ Revised png_debug() macros and statements to eliminate compiler warnings. ++ ++Version 1.0.8 [July 24, 2000] ++ Added png_flush() in pngwrite.c, after png_write_IEND(). ++ Updated makefile.hpux to build a shared library. ++ ++Version 1.0.9beta1 [November 10, 2000] ++ Fixed typo in scripts/makefile.hpux ++ Updated makevms.com in scripts and contrib/* and contrib/* (Martin Zinser) ++ Fixed seqence-point bug in contrib/pngminus/png2pnm (Martin Zinser) ++ Changed "cdrom.com" in documentation to "libpng.org" ++ Revised pnggccrd.c to get it all working, and updated makefile.gcmmx (Greg). ++ Changed type of "params" from voidp to png_voidp in png_read|write_png(). ++ Make sure PNGAPI and PNG_IMPEXP are defined in pngconf.h. ++ Revised the 3 instances of WRITEFILE in pngtest.c. ++ Relocated "msvc" and "wince" project subdirectories into "dll" subdirectory. ++ Updated png.rc in dll/msvc project ++ Revised makefile.dec to define and use LIBPATH and INCPATH ++ Increased size of global png_libpng_ver[] array from 12 to 18 chars. ++ Made global png_libpng_ver[], png_sig[] and png_pass_*[] arrays const. ++ Removed duplicate png_crc_finish() from png_handle_bKGD() function. ++ Added a warning when application calls png_read_update_info() multiple times. ++ Revised makefile.cygwin ++ Fixed bugs in iCCP support in pngrutil.c and pngwutil.c. ++ Replaced png_set_empty_plte_permitted() with png_permit_mng_features(). ++ ++Version 1.0.9beta2 [November 19, 2000] ++ Renamed the "dll" subdirectory "projects". ++ Added borland project files to "projects" subdirectory. ++ Set VS_FF_PRERELEASE and VS_FF_PATCHED flags in msvc/png.rc when appropriate. ++ Add error message in png_set_compression_buffer_size() when malloc fails. ++ ++Version 1.0.9beta3 [November 23, 2000] ++ Revised PNG_LIBPNG_BUILD_TYPE macro in png.h, used in the msvc project. ++ Removed the png_flush() in pngwrite.c that crashes some applications ++ that don't set png_output_flush_fn. ++ Added makefile.macosx and makefile.aix to scripts directory. ++ ++Version 1.0.9beta4 [December 1, 2000] ++ Change png_chunk_warning to png_warning in png_check_keyword(). ++ Increased the first part of msg buffer from 16 to 18 in png_chunk_error(). ++ ++Version 1.0.9beta5 [December 15, 2000] ++ Added support for filter method 64 (for PNG datastreams embedded in MNG). ++ ++Version 1.0.9beta6 [December 18, 2000] ++ Revised png_set_filter() to accept filter method 64 when appropriate. ++ Added new PNG_HAVE_PNG_SIGNATURE bit to png_ptr->mode and use it to ++ help prevent applications from using MNG features in PNG datastreams. ++ Added png_permit_mng_features() function. ++ Revised libpng.3/libpng.txt. Changed "filter type" to "filter method". ++ ++Version 1.0.9rc1 [December 23, 2000] ++ Revised test for PNG_HAVE_PNG_SIGNATURE in pngrutil.c ++ Fixed error handling of unknown compression type in png_decompress_chunk(). ++ In pngconf.h, define __cdecl when _MSC_VER is defined. ++ ++Version 1.0.9beta7 [December 28, 2000] ++ Changed PNG_TEXT_COMPRESSION_zTXt to PNG_COMPRESSION_TYPE_BASE several places. ++ Revised memory management in png_set_hIST and png_handle_hIST in a backward ++ compatible manner. PLTE and tRNS were revised similarly. ++ Revised the iCCP chunk reader to ignore trailing garbage. ++ ++Version 1.0.9beta8 [January 12, 2001] ++ Moved pngasmrd.h into pngconf.h. ++ Improved handling of out-of-spec garbage iCCP chunks generated by PhotoShop. ++ ++Version 1.0.9beta9 [January 15, 2001] ++ Added png_set_invalid, png_permit_mng_features, and png_mmx_supported to ++ wince and msvc project module definition files. ++ Minor revision of makefile.cygwin. ++ Fixed bug with progressive reading of narrow interlaced images in pngpread.c ++ ++Version 1.0.9beta10 [January 16, 2001] ++ Do not typedef png_FILE_p in pngconf.h when PNG_NO_STDIO is defined. ++ Fixed "png_mmx_supported" typo in project definition files. ++ ++Version 1.0.9beta11 [January 19, 2001] ++ Updated makefile.sgi to make shared library. ++ Removed png_mmx_support() function and disabled PNG_MNG_FEATURES_SUPPORTED ++ by default, for the benefit of DLL forward compatibility. These will ++ be re-enabled in version 1.2.0. ++ ++Version 1.0.9rc2 [January 22, 2001] ++ Revised cygwin support. ++ ++Version 1.0.9 [January 31, 2001] ++ Added check of cygwin's ALL_STATIC in pngconf.h ++ Added "-nommx" parameter to contrib/gregbook/rpng2-win and rpng2-x demos. ++ ++Version 1.0.10beta1 [March 14, 2001] ++ Revised makefile.dec, makefile.sgi, and makefile.sggcc; added makefile.hpgcc. ++ Reformatted libpng.3 to eliminate bad line breaks. ++ Added checks for _mmx_supported in the read_filter_row function of pnggccrd.c ++ Added prototype for png_mmx_support() near the top of pnggccrd.c ++ Moved some error checking from png_handle_IHDR to png_set_IHDR. ++ Added PNG_NO_READ_SUPPORTED and PNG_NO_WRITE_SUPPORTED macros. ++ Revised png_mmx_support() function in pnggccrd.c ++ Restored version 1.0.8 PNG_WRITE_EMPTY_PLTE_SUPPORTED behavior in pngwutil.c ++ Fixed memory leak in contrib/visupng/PngFile.c ++ Fixed bugs in png_combine_row() in pnggccrd.c and pngvcrd.c (C version) ++ Added warnings when retrieving or setting gamma=0. ++ Increased the first part of msg buffer from 16 to 18 in png_chunk_warning(). ++ ++Version 1.0.10rc1 [March 23, 2001] ++ Changed all instances of memcpy, strcpy, and strlen to png_memcpy, png_strcpy, ++ and png_strlen. ++ Revised png_mmx_supported() function in pnggccrd.c to return proper value. ++ Fixed bug in progressive reading (pngpread.c) with small images (height < 8). ++ ++Version 1.0.10 [March 30, 2001] ++ Deleted extraneous space (introduced in 1.0.9) from line 42 of makefile.cygwin ++ Added beos project files (Chris Herborth) ++ ++Version 1.0.11beta1 [April 3, 2001] ++ Added type casts on several png_malloc() calls (Dimitri Papadapoulos). ++ Removed a no-longer needed AIX work-around from pngconf.h ++ Changed several "//" single-line comments to C-style in pnggccrd.c ++ ++Version 1.0.11beta2 [April 11, 2001] ++ Removed PNGAPI from several functions whose prototypes did not have PNGAPI. ++ Updated scripts/pngos2.def ++ ++Version 1.0.11beta3 [April 14, 2001] ++ Added checking the results of many instances of png_malloc() for NULL ++ ++Version 1.0.11beta4 [April 20, 2001] ++ Undid the changes from version 1.0.11beta3. Added a check for NULL return ++ from user's malloc_fn(). ++ Removed some useless type casts of the NULL pointer. ++ Added makefile.netbsd ++ ++Version 1.0.11 [April 27, 2001] ++ Revised makefile.netbsd ++ ++Version 1.0.12beta1 [May 14, 2001] ++ Test for Windows platform in pngconf.h when including malloc.h (Emmanuel Blot) ++ Updated makefile.cygwin and handling of Cygwin's ALL_STATIC in pngconf.h ++ Added some never-to-be-executed code in pnggccrd.c to quiet compiler warnings. ++ Eliminated the png_error about apps using png_read|write_init(). Instead, ++ libpng will reallocate the png_struct and info_struct if they are too small. ++ This retains future binary compatibility for old applications written for ++ libpng-0.88 and earlier. ++ ++Version 1.2.0beta1 [May 6, 2001] ++ Bumped DLLNUM to 2. ++ Re-enabled PNG_MNG_FEATURES_SUPPORTED and enabled PNG_ASSEMBLER_CODE_SUPPORTED ++ by default. ++ Added runtime selection of MMX features. ++ Added png_set_strip_error_numbers function and related macros. ++ ++Version 1.2.0beta2 [May 7, 2001] ++ Finished merging 1.2.0beta1 with version 1.0.11 ++ Added a check for attempts to read or write PLTE in grayscale PNG datastreams. ++ ++Version 1.2.0beta3 [May 17, 2001] ++ Enabled user memory function by default. ++ Modified png_create_struct so it passes user mem_ptr to user memory allocator. ++ Increased png_mng_features flag from png_byte to png_uint_32. ++ Bumped shared-library (so-number) and dll-number to 3. ++ ++Version 1.2.0beta4 [June 23, 2001] ++ Check for missing profile length field in iCCP chunk and free chunk_data ++ in case of truncated iCCP chunk. ++ Bumped shared-library number to 3 in makefile.sgi and makefile.sggcc ++ Bumped dll-number from 2 to 3 in makefile.cygwin ++ Revised contrib/gregbook/rpng*-x.c to avoid a memory leak and to exit cleanly ++ if user attempts to run it on an 8-bit display. ++ Updated contrib/gregbook ++ Use png_malloc instead of png_zalloc to allocate palette in pngset.c ++ Updated makefile.ibmc ++ Added some typecasts to eliminate gcc 3.0 warnings. Changed prototypes ++ of png_write_oFFS width and height from png_uint_32 to png_int_32. ++ Updated example.c ++ Revised prototypes for png_debug_malloc and png_debug_free in pngtest.c ++ ++Version 1.2.0beta5 [August 8, 2001] ++ Revised contrib/gregbook ++ Revised makefile.gcmmx ++ Revised pnggccrd.c to conditionally compile some thread-unsafe code only ++ when PNG_THREAD_UNSAFE_OK is defined. ++ Added tests to prevent pngwutil.c from writing a bKGD or tRNS chunk with ++ value exceeding 2^bit_depth-1 ++ Revised makefile.sgi and makefile.sggcc ++ Replaced calls to fprintf(stderr,...) with png_warning() in pnggccrd.c ++ Removed restriction that do_invert_mono only operate on 1-bit opaque files ++ ++Version 1.2.0 [September 1, 2001] ++ Changed a png_warning() to png_debug() in pnggccrd.c ++ Fixed contrib/gregbook/rpng-x.c, rpng2-x.c to avoid crash with XFreeGC(). ++ ++Version 1.2.1beta1 [October 19, 2001] ++ Revised makefile.std in contrib/pngminus ++ Include background_1 in png_struct regardless of gamma support. ++ Revised makefile.netbsd and makefile.macosx, added makefile.darwin. ++ Revised example.c to provide more details about using row_callback(). ++ ++Version 1.2.1beta2 [October 25, 2001] ++ Added type cast to each NULL appearing in a function call, except for ++ WINCE functions. ++ Added makefile.so9. ++ ++Version 1.2.1beta3 [October 27, 2001] ++ Removed type casts from all NULLs. ++ Simplified png_create_struct_2(). ++ ++Version 1.2.1beta4 [November 7, 2001] ++ Revised png_create_info_struct() and png_creat_struct_2(). ++ Added error message if png_write_info() was omitted. ++ Type cast NULLs appearing in function calls when _NO_PROTO or ++ PNG_TYPECAST_NULL is defined. ++ ++Version 1.2.1rc1 [November 24, 2001] ++ Type cast NULLs appearing in function calls except when PNG_NO_TYPECAST_NULL ++ is defined. ++ Changed typecast of "size" argument to png_size_t in pngmem.c calls to ++ the user malloc_fn, to agree with the prototype in png.h ++ Added a pop/push operation to pnggccrd.c, to preserve Eflag (Maxim Sobolev) ++ Updated makefile.sgi to recognize LIBPATH and INCPATH. ++ Updated various makefiles so "make clean" does not remove previous major ++ version of the shared library. ++ ++Version 1.2.1rc2 [December 4, 2001] ++ Always allocate 256-entry internal palette, hist, and trans arrays, to ++ avoid out-of-bounds memory reference caused by invalid PNG datastreams. ++ Added a check for prefix_length > data_length in iCCP chunk handler. ++ ++Version 1.2.1 [December 7, 2001] ++ None. ++ ++Version 1.2.2beta1 [February 22, 2002] ++ Fixed a bug with reading the length of iCCP profiles (Larry Reeves). ++ Revised makefile.linux, makefile.gcmmx, and makefile.sgi to generate ++ libpng.a, libpng12.so (not libpng.so.3), and libpng12/png.h ++ Revised makefile.darwin to remove "-undefined suppress" option. ++ Added checks for gamma and chromaticity values over 21474.83, which exceed ++ the limit for PNG unsigned 32-bit integers when encoded. ++ Revised calls to png_create_read_struct() and png_create_write_struct() ++ for simpler debugging. ++ Revised png_zalloc() so zlib handles errors (uses PNG_FLAG_MALLOC_NULL_MEM_OK) ++ ++Version 1.2.2beta2 [February 23, 2002] ++ Check chunk_length and idat_size for invalid (over PNG_MAX_UINT) lengths. ++ Check for invalid image dimensions in png_get_IHDR. ++ Added missing "fi;" in the install target of the SGI makefiles. ++ Added install-static to all makefiles that make shared libraries. ++ Always do gamma compensation when image is partially transparent. ++ ++Version 1.2.2beta3 [March 7, 2002] ++ Compute background.gray and background_1.gray even when color_type is RGB ++ in case image gets reduced to gray later. ++ Modified shared-library makefiles to install pkgconfig/libpngNN.pc. ++ Export (with PNGAPI) png_zalloc, png_zfree, and png_handle_as_unknown ++ Removed unused png_write_destroy_info prototype from png.h ++ Eliminated incorrect use of width_mmx from pnggccrd.c in pixel_bytes == 8 case ++ Added install-shared target to all makefiles that make shared libraries. ++ Stopped a double free of palette, hist, and trans when not using free_me. ++ Added makefile.32sunu for Sun Ultra 32 and makefile.64sunu for Sun Ultra 64. ++ ++Version 1.2.2beta4 [March 8, 2002] ++ Compute background.gray and background_1.gray even when color_type is RGB ++ in case image gets reduced to gray later (Jason Summers). ++ Relocated a misplaced /bin/rm in the "install-shared" makefile targets ++ Added PNG_1_0_X macro which can be used to build a 1.0.x-compatible library. ++ ++Version 1.2.2beta5 [March 26, 2002] ++ Added missing PNGAPI to several function definitions. ++ Check for invalid bit_depth or color_type in png_get_IHDR(), and ++ check for missing PLTE or IHDR in png_push_read_chunk() (Matthias Clasen). ++ Revised iTXt support to accept NULL for lang and lang_key. ++ Compute gamma for color components of background even when color_type is gray. ++ Changed "()" to "{}" in scripts/libpng.pc.in. ++ Revised makefiles to put png.h and pngconf.h only in $prefix/include/libpngNN ++ Revised makefiles to make symlink to libpng.so.NN in addition to libpngNN.so ++ ++Version 1.2.2beta6 [March 31, 2002] ++ ++Version 1.0.13beta1 [March 31, 2002] ++ Prevent png_zalloc() from trying to memset memory that it failed to acquire. ++ Add typecasts of PNG_MAX_UINT in pngset_cHRM_fixed() (Matt Holgate). ++ Ensure that the right function (user or default) is used to free the ++ png_struct after an error in png_create_read_struct_2(). ++ ++Version 1.2.2rc1 [April 7, 2002] ++ ++Version 1.0.13rc1 [April 7, 2002] ++ Save the ebx register in pnggccrd.c (Sami Farin) ++ Add "mem_ptr = png_ptr->mem_ptr" in png_destroy_write_struct() (Paul Gardner). ++ Updated makefiles to put headers in include/libpng and remove old include/*.h. ++ ++Version 1.2.2 [April 15, 2002] ++ ++Version 1.0.13 [April 15, 2002] ++ Revised description of png_set_filter() in libpng.3/libpng.txt. ++ Revised makefile.netbsd and added makefile.neNNbsd and makefile.freebsd ++ ++Version 1.0.13patch01 [April 17, 2002] ++ ++Version 1.2.2patch01 [April 17, 2002] ++ Changed ${PNGMAJ}.${PNGVER} bug to ${PNGVER} in makefile.sgi and ++ makefile.sggcc ++ Fixed VER -> PNGVER typo in makefile.macosx and added install-static to ++ install ++ Added install: target to makefile.32sunu and makefile.64sunu ++ ++Version 1.0.13patch03 [April 18, 2002] ++ ++Version 1.2.2patch03 [April 18, 2002] ++ Revised 15 makefiles to link libpng.a to libpngNN.a and the include libpng ++ subdirectory to libpngNN subdirectory without the full pathname. ++ Moved generation of libpng.pc from "install" to "all" in 15 makefiles. ++ ++Version 1.2.3rc1 [April 28, 2002] ++ Added install-man target to 15 makefiles (Dimitri Papadopolous-Orfanos). ++ Added $(DESTDIR) feature to 24 makefiles (Tim Mooney) ++ Fixed bug with $prefix, should be $(prefix) in makefile.hpux. ++ Updated cygwin-specific portion of pngconf.h and revised makefile.cygwin ++ Added a link from libpngNN.pc to libpng.pc in 15 makefiles. ++ Added links from include/libpngNN/*.h to include/*.h in 24 makefiles. ++ Revised makefile.darwin to make relative links without full pathname. ++ Added setjmp() at the end of png_create_*_struct_2() in case user forgets ++ to put one in their application. ++ Restored png_zalloc() and png_zfree() prototypes to version 1.2.1 and ++ removed them from module definition files. ++ ++Version 1.2.3rc2 [May 1, 2002] ++ Fixed bug in reporting number of channels in pngget.c and pngset.c, ++ that was introduced in version 1.2.2beta5. ++ Exported png_zalloc(), png_zfree(), png_default_read(), png_default_write(), ++ png_default_flush(), and png_push_fill_buffer() and included them in ++ module definition files. ++ Added "libpng.pc" dependency to the "install-shared" target in 15 makefiles. ++ ++Version 1.2.3rc3 [May 1, 2002] ++ Revised prototype for png_default_flush() ++ Remove old libpng.pc and libpngNN.pc before installing new ones. ++ ++Version 1.2.3rc4 [May 2, 2002] ++ Typos in *.def files (png_default_read|write -> png_default_read|write_data) ++ In makefiles, changed rm libpng.NN.pc to rm libpngNN.pc ++ Added libpng-config and libpngNN-config and modified makefiles to install ++ them. ++ Changed $(MANPATH) to $(DESTDIR)$(MANPATH) in makefiles ++ Added "Win32 DLL VB" configuration to projects/msvc/libpng.dsp ++ ++Version 1.2.3rc5 [May 11, 2002] ++ Changed "error" and "message" in prototypes to "error_message" and ++ "warning_message" to avoid namespace conflict. ++ Revised 15 makefiles to build libpng-config from libpng-config-*.in ++ Once more restored png_zalloc and png_zfree to regular nonexported form. ++ Restored png_default_read|write_data, png_default_flush, png_read_fill_buffer ++ to nonexported form, but with PNGAPI, and removed them from module def ++ files. ++ ++Version 1.2.3rc6 [May 14, 2002] ++ Removed "PNGAPI" from png_zalloc() and png_zfree() in png.c ++ Changed "Gz" to "Gd" in projects/msvc/libpng.dsp and zlib.dsp. ++ Removed leftover libpng-config "sed" script from four makefiles. ++ Revised libpng-config creating script in 16 makefiles. ++ ++Version 1.2.3 [May 22, 2002] ++ Revised libpng-config target in makefile.cygwin. ++ Removed description of png_set_mem_fn() from documentation. ++ Revised makefile.freebsd. ++ Minor cosmetic changes to 15 makefiles, e.g., $(DI) = $(DESTDIR)/$(INCDIR). ++ Revised projects/msvc/README.txt ++ Changed -lpng to -lpngNN in LDFLAGS in several makefiles. ++ ++Version 1.2.4beta1 [May 24, 2002] ++ Added libpng.pc and libpng-config to "all:" target in 16 makefiles. ++ Fixed bug in 16 makefiles: $(DESTDIR)/$(LIBPATH) to $(DESTDIR)$(LIBPATH) ++ Added missing "\" before closing double quote in makefile.gcmmx. ++ Plugged various memory leaks; added png_malloc_warn() and png_set_text_2() ++ functions. ++ ++Version 1.2.4beta2 [June 25, 2002] ++ Plugged memory leak of png_ptr->current_text (Matt Holgate). ++ Check for buffer overflow before reading CRC in pngpread.c (Warwick Allison) ++ Added -soname to the loader flags in makefile.dec, makefile.sgi, and ++ makefile.sggcc. ++ Added "test-installed" target to makefile.linux, makefile.gcmmx, ++ makefile.sgi, and makefile.sggcc. ++ ++Version 1.2.4beta3 [June 28, 2002] ++ Plugged memory leak of row_buf in pngtest.c when there is a png_error(). ++ Detect buffer overflow in pngpread.c when IDAT is corrupted with extra data. ++ Added "test-installed" target to makefile.32sunu, makefile.64sunu, ++ makefile.beos, makefile.darwin, makefile.dec, makefile.macosx, ++ makefile.solaris, makefile.hpux, makefile.hpgcc, and makefile.so9. ++ ++Version 1.2.4rc1 and 1.0.14rc1 [July 2, 2002] ++ Added "test-installed" target to makefile.cygwin and makefile.sco. ++ Revised pnggccrd.c to be able to back out version 1.0.x via PNG_1_0_X macro. ++ ++Version 1.2.4 and 1.0.14 [July 8, 2002] ++ Changed png_warning() to png_error() when width is too large to process. ++ ++Version 1.2.4patch01 [July 20, 2002] ++ Revised makefile.cygwin to use DLL number 12 instead of 13. ++ ++Version 1.2.5beta1 [August 6, 2002] ++ Added code to contrib/gregbook/readpng2.c to ignore unused chunks. ++ Replaced toucan.png in contrib/gregbook (it has been corrupt since 1.0.11) ++ Removed some stray *.o files from contrib/gregbook. ++ Changed png_error() to png_warning() about "Too much data" in pngpread.c ++ and about "Extra compressed data" in pngrutil.c. ++ Prevent png_ptr->pass from exceeding 7 in png_push_finish_row(). ++ Updated makefile.hpgcc ++ Updated png.c and pnggccrd.c handling of return from png_mmx_support() ++ ++Version 1.2.5beta2 [August 15, 2002] ++ Only issue png_warning() about "Too much data" in pngpread.c when avail_in ++ is nonzero. ++ Updated makefiles to install a separate libpng.so.3 with its own rpath. ++ ++Version 1.2.5rc1 and 1.0.15rc1 [August 24, 2002] ++ Revised makefiles to not remove previous minor versions of shared libraries. ++ ++Version 1.2.5rc2 and 1.0.15rc2 [September 16, 2002] ++ Revised 13 makefiles to remove "-lz" and "-L$(ZLIBLIB)", etc., from shared ++ library loader directive. ++ Added missing "$OBJSDLL" line to makefile.gcmmx. ++ Added missing "; fi" to makefile.32sunu. ++ ++Version 1.2.5rc3 and 1.0.15rc3 [September 18, 2002] ++ Revised libpng-config script. ++ ++Version 1.2.5 and 1.0.15 [October 3, 2002] ++ Revised makefile.macosx, makefile.darwin, makefile.hpgcc, and makefile.hpux, ++ and makefile.aix. ++ Relocated two misplaced PNGAPI lines in pngtest.c ++ ++Version 1.2.6beta1 [October 22, 2002] ++ Commented out warning about uninitialized mmx_support in pnggccrd.c. ++ Changed "IBMCPP__" flag to "__IBMCPP__" in pngconf.h. ++ Relocated two more misplaced PNGAPI lines in pngtest.c ++ Fixed memory overrun bug in png_do_read_filler() with 16-bit datastreams, ++ introduced in version 1.0.2. ++ Revised makefile.macosx, makefile.dec, makefile.aix, and makefile.32sunu. ++ ++Version 1.2.6beta2 [November 1, 2002] ++ Added libpng-config "--ldopts" output. ++ Added "AR=ar" and "ARFLAGS=rc" and changed "ar rc" to "$(AR) $(ARFLAGS)" ++ in makefiles. ++ ++Version 1.2.6beta3 [July 18, 2004] ++ Reverted makefile changes from version 1.2.6beta2 and some of the changes ++ from version 1.2.6beta1; these will be postponed until version 1.2.7. ++ Version 1.2.6 is going to be a simple bugfix release. ++ Changed the one instance of "ln -sf" to "ln -f -s" in each Sun makefile. ++ Fixed potential overrun in pngerror.c by using strncpy instead of memcpy. ++ Added "#!/bin/sh" at the top of configure, for recognition of the ++ 'x' flag under Cygwin (Cosmin). ++ Optimized vacuous tests that silence compiler warnings, in png.c (Cosmin). ++ Added support for PNG_USER_CONFIG, in pngconf.h (Cosmin). ++ Fixed the special memory handler for Borland C under DOS, in pngmem.c ++ (Cosmin). ++ Removed some spurious assignments in pngrutil.c (Cosmin). ++ Replaced 65536 with 65536L, and 0xffff with 0xffffL, to silence warnings ++ on 16-bit platforms (Cosmin). ++ Enclosed shift op expressions in parentheses, to silence warnings (Cosmin). ++ Used proper type png_fixed_point, to avoid problems on 16-bit platforms, ++ in png_handle_sRGB() (Cosmin). ++ Added compression_type to png_struct, and optimized the window size ++ inside the deflate stream (Cosmin). ++ Fixed definition of isnonalpha(), in pngerror.c and pngrutil.c (Cosmin). ++ Fixed handling of unknown chunks that come after IDAT (Cosmin). ++ Allowed png_error() and png_warning() to work even if png_ptr == NULL ++ (Cosmin). ++ Replaced row_info->rowbytes with row_bytes in png_write_find_filter() ++ (Cosmin). ++ Fixed definition of PNG_LIBPNG_VER_DLLNUM (Simon-Pierre). ++ Used PNG_LIBPNG_VER and PNG_LIBPNG_VER_STRING instead of the hardcoded ++ values in png.c (Simon-Pierre, Cosmin). ++ Initialized png_libpng_ver[] with PNG_LIBPNG_VER_STRING (Simon-Pierre). ++ Replaced PNG_LIBPNG_VER_MAJOR with PNG_LIBPNG_VER_DLLNUM in png.rc ++ (Simon-Pierre). ++ Moved the definition of PNG_HEADER_VERSION_STRING near the definitions ++ of the other PNG_LIBPNG_VER_... symbols in png.h (Cosmin). ++ Relocated #ifndef PNGAPI guards in pngconf.h (Simon-Pierre, Cosmin). ++ Updated scripts/makefile.vc(a)win32 (Cosmin). ++ Updated the MSVC project (Simon-Pierre, Cosmin). ++ Updated the Borland C++ Builder project (Cosmin). ++ Avoided access to asm_flags in pngvcrd.c, if PNG_1_0_X is defined (Cosmin). ++ Commented out warning about uninitialized mmx_support in pngvcrd.c (Cosmin). ++ Removed scripts/makefile.bd32 and scripts/pngdef.pas (Cosmin). ++ Added extra guard around inclusion of Turbo C memory headers, in pngconf.h ++ (Cosmin). ++ Renamed projects/msvc/ to projects/visualc6/, and projects/borland/ to ++ projects/cbuilder5/ (Cosmin). ++ Moved projects/visualc6/png32ms.def to scripts/pngw32.def, ++ and projects/visualc6/png.rc to scripts/pngw32.rc (Cosmin). ++ Added projects/visualc6/pngtest.dsp; removed contrib/msvctest/ (Cosmin). ++ Changed line endings to DOS style in cbuilder5 and visualc6 files, even ++ in the tar.* distributions (Cosmin). ++ Updated contrib/visupng/VisualPng.dsp (Cosmin). ++ Updated contrib/visupng/cexcept.h to version 2.0.0 (Cosmin). ++ Added a separate distribution with "configure" and supporting files (Junichi). ++ ++Version 1.2.6beta4 [July 28, 2004] ++ Added user ability to change png_size_t via a PNG_SIZE_T macro. ++ Added png_sizeof() and png_convert_size() functions. ++ Added PNG_SIZE_MAX (maximum value of a png_size_t variable. ++ Added check in png_malloc_default() for (size_t)size != (png_uint_32)size ++ which would indicate an overflow. ++ Changed sPLT failure action from png_error to png_warning and abandon chunk. ++ Changed sCAL and iCCP failures from png_error to png_warning and abandon. ++ Added png_get_uint_31(png_ptr, buf) function. ++ Added PNG_UINT_32_MAX macro. ++ Renamed PNG_MAX_UINT to PNG_UINT_31_MAX. ++ Made png_zalloc() issue a png_warning and return NULL on potential ++ overflow. ++ Turn on PNG_NO_ZALLOC_ZERO by default in version 1.2.x ++ Revised "clobber list" in pnggccrd.c so it will compile under gcc-3.4. ++ Revised Borland portion of png_malloc() to return NULL or issue ++ png_error() according to setting of PNG_FLAG_MALLOC_NULL_MEM_OK. ++ Added PNG_NO_SEQUENTIAL_READ_SUPPORTED macro to conditionally remove ++ sequential read support. ++ Added some "#if PNG_WRITE_SUPPORTED" blocks. ++ Added #ifdef to remove some redundancy in png_malloc_default(). ++ Use png_malloc instead of png_zalloc to allocate the palette. ++ ++Version 1.0.16rc1 and 1.2.6rc1 [August 4, 2004] ++ Fixed buffer overflow vulnerability (CVE-2004-0597) in png_handle_tRNS(). ++ Fixed NULL dereference vulnerability (CVE-2004-0598) in png_handle_iCCP(). ++ Fixed integer overflow vulnerability (CVE-2004-0599) in png_read_png(). ++ Fixed some harmless bugs in png_handle_sBIT, etc, that would cause ++ duplicate chunk types to go undetected. ++ Fixed some timestamps in the -config version ++ Rearranged order of processing of color types in png_handle_tRNS(). ++ Added ROWBYTES macro to calculate rowbytes without integer overflow. ++ Updated makefile.darwin and removed makefile.macosx from scripts directory. ++ Imposed default one million column, one-million row limits on the image ++ dimensions, and added png_set_user_limits() function to override them. ++ Revised use of PNG_SET_USER_LIMITS_SUPPORTED macro. ++ Fixed wrong cast of returns from png_get_user_width|height_max(). ++ Changed some "keep the compiler happy" from empty statements to returns, ++ Revised libpng.txt to remove 1.2.x stuff from the 1.0.x distribution ++ ++Version 1.0.16rc2 and 1.2.6rc2 [August 7, 2004] ++ Revised makefile.darwin and makefile.solaris. Removed makefile.macosx. ++ Revised pngtest's png_debug_malloc() to use png_malloc() instead of ++ png_malloc_default() which is not supposed to be exported. ++ Fixed off-by-one error in one of the conversions to PNG_ROWBYTES() in ++ pngpread.c. Bug was introduced in 1.2.6rc1. ++ Fixed bug in RGB to RGBX transformation introduced in 1.2.6rc1. ++ Fixed old bug in RGB to Gray transformation. ++ Fixed problem with 64-bit compilers by casting arguments to abs() ++ to png_int_32. ++ Changed "ln -sf" to "ln -f -s" in three makefiles (solaris, sco, so9). ++ Changed "HANDLE_CHUNK_*" to "PNG_HANDLE_CHUNK_*" (Cosmin) ++ Added "-@/bin/rm -f $(DL)/$(LIBNAME).so.$(PNGMAJ)" to 15 *NIX makefiles. ++ Added code to update the row_info->colortype in png_do_read_filler() (MSB). ++ ++Version 1.0.16rc3 and 1.2.6rc3 [August 9, 2004] ++ Eliminated use of "abs()" in testing cHRM and gAMA values, to avoid ++ trouble with some 64-bit compilers. Created PNG_OUT_OF_RANGE() macro. ++ Revised documentation of png_set_keep_unknown_chunks(). ++ Check handle_as_unknown status in pngpread.c, as in pngread.c previously. ++ Moved "PNG_HANDLE_CHUNK_*" macros out of PNG_INTERNAL section of png.h ++ Added "rim" definitions for CONST4 and CONST6 in pnggccrd.c ++ ++Version 1.0.16rc4 and 1.2.6rc4 [August 10, 2004] ++ Fixed mistake in pngtest.c introduced in 1.2.6rc2 (declaration of ++ "pinfo" was out of place). ++ ++Version 1.0.16rc5 and 1.2.6rc5 [August 10, 2004] ++ Moved "PNG_HANDLE_CHUNK_*" macros out of PNG_ASSEMBLER_CODE_SUPPORTED ++ section of png.h where they were inadvertently placed in version rc3. ++ ++Version 1.2.6 and 1.0.16 [August 15, 2004] ++ Revised pngtest so memory allocation testing is only done when PNG_DEBUG==1. ++ ++Version 1.2.7beta1 [August 26, 2004] ++ Removed unused pngasmrd.h file. ++ Removed references to uu.net for archived files. Added references to ++ PNG Spec (second edition) and the PNG ISO/IEC Standard. ++ Added "test-dd" target in 15 makefiles, to run pngtest in DESTDIR. ++ Fixed bug with "optimized window size" in the IDAT datastream, that ++ causes libpng to write PNG files with incorrect zlib header bytes. ++ ++Version 1.2.7beta2 [August 28, 2004] ++ Fixed bug with sCAL chunk and big-endian machines (David Munro). ++ Undid new code added in 1.2.6rc2 to update the color_type in ++ png_set_filler(). ++ Added png_set_add_alpha() that updates color type. ++ ++Version 1.0.17rc1 and 1.2.7rc1 [September 4, 2004] ++ Revised png_set_strip_filler() to not remove alpha if color_type has alpha. ++ ++Version 1.2.7 and 1.0.17 [September 12, 2004] ++ Added makefile.hp64 ++ Changed projects/msvc/png32ms.def to scripts/png32ms.def in makefile.cygwin ++ ++Version 1.2.8beta1 [November 1, 2004] ++ Fixed bug in png_text_compress() that would fail to complete a large block. ++ Fixed bug, introduced in libpng-1.2.7, that overruns a buffer during ++ strip alpha operation in png_do_strip_filler(). ++ Added PNG_1_2_X definition in pngconf.h ++ Use #ifdef to comment out png_info_init in png.c and png_read_init in ++ pngread.c (as of 1.3.0) ++ ++Version 1.2.8beta2 [November 2, 2004] ++ Reduce color_type to a nonalpha type after strip alpha operation in ++ png_do_strip_filler(). ++ ++Version 1.2.8beta3 [November 3, 2004] ++ Revised definitions of PNG_MAX_UINT_32, PNG_MAX_SIZE, and PNG_MAXSUM ++ ++Version 1.2.8beta4 [November 12, 2004] ++ Fixed (again) definition of PNG_LIBPNG_VER_DLLNUM in png.h (Cosmin). ++ Added PNG_LIBPNG_BUILD_PRIVATE in png.h (Cosmin). ++ Set png_ptr->zstream.data_type to Z_BINARY, to avoid unnecessary detection ++ of data type in deflate (Cosmin). ++ Deprecated but continue to support SPECIALBUILD and PRIVATEBUILD in favor of ++ PNG_LIBPNG_BUILD_SPECIAL_STRING and PNG_LIBPNG_BUILD_PRIVATE_STRING. ++ ++Version 1.2.8beta5 [November 20, 2004] ++ Use png_ptr->flags instead of png_ptr->transformations to pass ++ PNG_STRIP_ALPHA info to png_do_strip_filler(), to preserve ABI ++ compatibility. ++ Revised handling of SPECIALBUILD, PRIVATEBUILD, ++ PNG_LIBPNG_BUILD_SPECIAL_STRING and PNG_LIBPNG_BUILD_PRIVATE_STRING. ++ ++Version 1.2.8rc1 [November 24, 2004] ++ Moved handling of BUILD macros from pngconf.h to png.h ++ Added definition of PNG_LIBPNG_BASE_TYPE in png.h, inadvertently ++ omitted from beta5. ++ Revised scripts/pngw32.rc ++ Despammed mailing addresses by masking "@" with "at". ++ Inadvertently installed a supposedly faster test version of pngrutil.c ++ ++Version 1.2.8rc2 [November 26, 2004] ++ Added two missing "\" in png.h ++ Change tests in pngread.c and pngpread.c to ++ if (png_ptr->transformations || (png_ptr->flags&PNG_FLAG_STRIP_ALPHA)) ++ png_do_read_transformations(png_ptr); ++ ++Version 1.2.8rc3 [November 28, 2004] ++ Reverted pngrutil.c to version libpng-1.2.8beta5. ++ Added scripts/makefile.elf with supporting code in pngconf.h for symbol ++ versioning (John Bowler). ++ ++Version 1.2.8rc4 [November 29, 2004] ++ Added projects/visualc7 (Simon-pierre). ++ ++Version 1.2.8rc5 [November 29, 2004] ++ Fixed new typo in scripts/pngw32.rc ++ ++Version 1.2.8 [December 3, 2004] ++ Removed projects/visualc7, added projects/visualc71. ++ ++Version 1.2.9beta1 [February 21, 2006] ++ Initialized some structure members in pngwutil.c to avoid gcc-4.0.0 complaints ++ Revised man page and libpng.txt to make it clear that one should not call ++ png_read_end or png_write_end after png_read_png or png_write_png. ++ Updated references to png-mng-implement mailing list. ++ Fixed an incorrect typecast in pngrutil.c ++ Added PNG_NO_READ_SUPPORTED conditional for making a write-only library. ++ Added PNG_NO_WRITE_INTERLACING_SUPPORTED conditional. ++ Optimized alpha-inversion loops in pngwtran.c ++ Moved test for nonzero gamma outside of png_build_gamma_table() in pngrtran.c ++ Make sure num_trans is <= 256 before copying data in png_set_tRNS(). ++ Make sure num_palette is <= 256 before copying data in png_set_PLTE(). ++ Interchanged order of write_swap_alpha and write_invert_alpha transforms. ++ Added parentheses in the definition of PNG_LIBPNG_BUILD_TYPE (Cosmin). ++ Optimized zlib window flag (CINFO) in contrib/pngsuite/*.png (Cosmin). ++ Updated scripts/makefile.bc32 for Borland C++ 5.6 (Cosmin). ++ Exported png_get_uint_32, png_save_uint_32, png_get_uint_16, png_save_uint_16, ++ png_get_int_32, png_save_int_32, png_get_uint_31 (Cosmin). ++ Added type cast (png_byte) in png_write_sCAL() (Cosmin). ++ Fixed scripts/makefile.cygwin (Christian Biesinger, Cosmin). ++ Default iTXt support was inadvertently enabled. ++ ++Version 1.2.9beta2 [February 21, 2006] ++ Check for png_rgb_to_gray and png_gray_to_rgb read transformations before ++ checking for png_read_dither in pngrtran.c ++ Revised checking of chromaticity limits to accommodate extended RGB ++ colorspace (John Denker). ++ Changed line endings in some of the project files to CRLF, even in the ++ "Unix" tar distributions (Cosmin). ++ Made png_get_int_32 and png_save_int_32 always available (Cosmin). ++ Updated scripts/pngos2.def, scripts/pngw32.def and projects/wince/png32ce.def ++ with the newly exported functions. ++ Eliminated distributions without the "configure" script. ++ Updated INSTALL instructions. ++ ++Version 1.2.9beta3 [February 24, 2006] ++ Fixed CRCRLF line endings in contrib/visupng/VisualPng.dsp ++ Made libpng.pc respect EXEC_PREFIX (D. P. Kreil, J. Bowler) ++ Removed reference to pngasmrd.h from Makefile.am ++ Renamed CHANGES to ChangeLog. ++ Renamed LICENSE to COPYING. ++ Renamed ANNOUNCE to NEWS. ++ Created AUTHORS file. ++ ++Version 1.2.9beta4 [March 3, 2006] ++ Changed definition of PKGCONFIG from $prefix/lib to $libdir in configure.ac ++ Reverted to filenames LICENSE and ANNOUNCE; removed AUTHORS and COPYING. ++ Removed newline from the end of some error and warning messages. ++ Removed test for sqrt() from configure.ac and configure. ++ Made swap tables in pngtrans.c PNG_CONST (Carlo Bramix). ++ Disabled default iTXt support that was inadvertently enabled in ++ libpng-1.2.9beta1. ++ Added "OS2" to list of systems that don't need underscores, in pnggccrd.c ++ Removed libpng version and date from *.c files. ++ ++Version 1.2.9beta5 [March 4, 2006] ++ Removed trailing blanks from source files. ++ Put version and date of latest change in each source file, and changed ++ copyright year accordingly. ++ More cleanup of configure.ac, Makefile.am, and associated scripts. ++ Restored scripts/makefile.elf which was inadvertently deleted. ++ ++Version 1.2.9beta6 [March 6, 2006] ++ Fixed typo (RELEASE) in configuration files. ++ ++Version 1.2.9beta7 [March 7, 2006] ++ Removed libpng.vers and libpng.sym from libpng12_la_SOURCES in Makefile.am ++ Fixed inconsistent #ifdef's around png_sig_bytes() and png_set_sCAL_s() ++ in png.h. ++ Updated makefile.elf as suggested by debian. ++ Made cosmetic changes to some makefiles, adding LN_SF and other macros. ++ Made some makefiles accept "exec_prefix". ++ ++Version 1.2.9beta8 [March 9, 2006] ++ Fixed some "#if defined (..." which should be "#if defined(..." ++ Bug introduced in libpng-1.2.8. ++ Fixed inconsistency in definition of png_default_read_data() ++ Restored blank that was lost from makefile.sggcc "clean" target in beta7. ++ Revised calculation of "current" and "major" for irix in ltmain.sh ++ Changed "mkdir" to "MKDIR_P" in some makefiles. ++ Separated PNG_EXPAND and PNG_EXPAND_tRNS. ++ Added png_set_expand_gray_1_2_4_to_8() and deprecated ++ png_set_gray_1_2_4_to_8() which also expands tRNS to alpha. ++ ++Version 1.2.9beta9 [March 10, 2006] ++ Include "config.h" in pngconf.h when available. ++ Added some checks for NULL png_ptr or NULL info_ptr (timeless) ++ ++Version 1.2.9beta10 [March 20, 2006] ++ Removed extra CR from contrib/visualpng/VisualPng.dsw (Cosmin) ++ Made pnggccrd.c PIC-compliant (Christian Aichinger). ++ Added makefile.mingw (Wolfgang Glas). ++ Revised pngconf.h MMX checking. ++ ++Version 1.2.9beta11 [March 22, 2006] ++ Fixed out-of-order declaration in pngwrite.c that was introduced in beta9 ++ Simplified some makefiles by using LIBSO, LIBSOMAJ, and LIBSOVER macros. ++ ++Version 1.2.9rc1 [March 31, 2006] ++ Defined PNG_USER_PRIVATEBUILD when including "pngusr.h" (Cosmin). ++ Removed nonsensical assertion check from pngtest.c (Cosmin). ++ ++Version 1.2.9 [April 14, 2006] ++ Revised makefile.beos and added "none" selector in ltmain.sh ++ ++Version 1.2.10beta1 [April 15, 2006] ++ Renamed "config.h" to "png_conf.h" and revised Makefile.am to add ++ -DPNG_BUILDING_LIBPNG to compile directive, and modified pngconf.h ++ to include png_conf.h only when PNG_BUILDING_LIBPNG is defined. ++ ++Version 1.2.10beta2 [April 15, 2006] ++ Manually updated Makefile.in and configure. Changed png_conf.h.in ++ back to config.h. ++ ++Version 1.2.10beta3 [April 15, 2006] ++ Change png_conf.h back to config.h in pngconf.h. ++ ++Version 1.2.10beta4 [April 16, 2006] ++ Change PNG_BUILDING_LIBPNG to PNG_CONFIGURE_LIBPNG in config/Makefile*. ++ ++Version 1.2.10beta5 [April 16, 2006] ++ Added a configure check for compiling assembler code in pnggccrd.c ++ ++Version 1.2.10beta6 [April 17, 2006] ++ Revised the configure check for pnggccrd.c ++ Moved -DPNG_CONFIGURE_LIBPNG into @LIBPNG_DEFINES@ ++ Added @LIBPNG_DEFINES@ to arguments when building libpng.sym ++ ++Version 1.2.10beta7 [April 18, 2006] ++ Change "exec_prefix=$prefix" to "exec_prefix=$(prefix)" in makefiles. ++ ++Version 1.2.10rc1 [April 19, 2006] ++ Ensure pngconf.h doesn't define both PNG_USE_PNGGCCRD and PNG_USE_PNGVCRD ++ Fixed "LN_FS" typo in makefile.sco and makefile.solaris. ++ ++Version 1.2.10rc2 [April 20, 2006] ++ Added a backslash between -DPNG_CONFIGURE_LIBPNG and -DPNG_NO_ASSEMBLER_CODE ++ in configure.ac and configure ++ Made the configure warning about versioned symbols less arrogant. ++ ++Version 1.2.10rc3 [April 21, 2006] ++ Added a note in libpng.txt that png_set_sig_bytes(8) can be used when ++ writing an embedded PNG without the 8-byte signature. ++ Revised makefiles and configure to avoid making links to libpng.so.* ++ ++Version 1.2.10 [April 23, 2006] ++ Reverted configure to "rc2" state. ++ ++Version 1.2.11beta1 [May 31, 2006] ++ scripts/libpng.pc.in contained "configure" style version info and would ++ not work with makefiles. ++ The shared-library makefiles were linking to libpng.so.0 instead of ++ libpng.so.3 compatibility as the library. ++ ++Version 1.2.11beta2 [June 2, 2006] ++ Increased sprintf buffer from 50 to 52 chars in pngrutil.c to avoid ++ buffer overflow. ++ Fixed bug in example.c (png_set_palette_rgb -> png_set_palette_to_rgb) ++ ++Version 1.2.11beta3 [June 5, 2006] ++ Prepended "#! /bin/sh" to ltmail.sh and contrib/pngminus/*.sh (Cosmin). ++ Removed the accidental leftover Makefile.in~ (Cosmin). ++ Avoided potential buffer overflow and optimized buffer in ++ png_write_sCAL(), png_write_sCAL_s() (Cosmin). ++ Removed the include directories and libraries from CFLAGS and LDFLAGS ++ in scripts/makefile.gcc (Nelson A. de Oliveira, Cosmin). ++ ++Version 1.2.11beta4 [June 6, 2006] ++ Allow zero-length IDAT chunks after the entire zlib datastream, but not ++ after another intervening chunk type. ++ ++Version 1.0.19rc1, 1.2.11rc1 [June 13, 2006] ++ Deleted extraneous square brackets from [config.h] in configure.ac ++ ++Version 1.0.19rc2, 1.2.11rc2 [June 14, 2006] ++ Added prototypes for PNG_INCH_CONVERSIONS functions to png.h ++ Revised INSTALL and autogen.sh ++ Fixed typo in several makefiles (-W1 should be -Wl) ++ Added typedef for png_int_32 and png_uint_32 on 64-bit systems. ++ ++Version 1.0.19rc3, 1.2.11rc3 [June 15, 2006] ++ Removed the new typedefs for 64-bit systems (delay until version 1.4.0) ++ Added one zero element to png_gamma_shift[] array in pngrtran.c to avoid ++ reading out of bounds. ++ ++Version 1.0.19rc4, 1.2.11rc4 [June 15, 2006] ++ Really removed the new typedefs for 64-bit systems. ++ ++Version 1.0.19rc5, 1.2.11rc5 [June 22, 2006] ++ Removed png_sig_bytes entry from scripts/pngw32.def ++ ++Version 1.0.19, 1.2.11 [June 26, 2006] ++ None. ++ ++Version 1.0.20, 1.2.12 [June 27, 2006] ++ Really increased sprintf buffer from 50 to 52 chars in pngrutil.c to avoid ++ buffer overflow. ++ ++Version 1.2.13beta1 [October 2, 2006] ++ Removed AC_FUNC_MALLOC from configure.ac ++ Work around Intel-Mac compiler bug by setting PNG_NO_MMX_CODE in pngconf.h ++ Change "logical" to "bitwise" throughout documentation. ++ Detect and fix attempt to write wrong iCCP profile length (CVE-2006-7244) ++ ++Version 1.0.21, 1.2.13 [November 14, 2006] ++ Fix potential buffer overflow in sPLT chunk handler. ++ Fix Makefile.am to not try to link to noexistent files. ++ Check all exported functions for NULL png_ptr. ++ ++Version 1.2.14beta1 [November 17, 2006] ++ Relocated three misplaced tests for NULL png_ptr. ++ Built Makefile.in with automake-1.9.6 instead of 1.9.2. ++ Build configure with autoconf-2.60 instead of 2.59 ++ ++Version 1.2.14beta2 [November 17, 2006] ++ Added some typecasts in png_zalloc(). ++ ++Version 1.2.14rc1 [November 20, 2006] ++ Changed "strtod" to "png_strtod" in pngrutil.c ++ ++Version 1.0.22, 1.2.14 [November 27, 2006] ++ Added missing "$(srcdir)" in Makefile.am and Makefile.in ++ ++Version 1.2.15beta1 [December 3, 2006] ++ Generated configure with autoconf-2.61 instead of 2.60 ++ Revised configure.ac to update libpng.pc and libpng-config. ++ ++Version 1.2.15beta2 [December 3, 2006] ++ Always export MMX asm functions, just stubs if not building pnggccrd.c ++ ++Version 1.2.15beta3 [December 4, 2006] ++ Add "png_bytep" typecast to profile while calculating length in pngwutil.c ++ ++Version 1.2.15beta4 [December 7, 2006] ++ Added scripts/CMakeLists.txt ++ Changed PNG_NO_ASSEMBLER_CODE to PNG_NO_MMX_CODE in scripts, like 1.4.0beta ++ ++Version 1.2.15beta5 [December 7, 2006] ++ Changed some instances of PNG_ASSEMBLER_* to PNG_MMX_* in pnggccrd.c ++ Revised scripts/CMakeLists.txt ++ ++Version 1.2.15beta6 [December 13, 2006] ++ Revised scripts/CMakeLists.txt and configure.ac ++ ++Version 1.2.15rc1 [December 18, 2006] ++ Revised scripts/CMakeLists.txt ++ ++Version 1.2.15rc2 [December 21, 2006] ++ Added conditional #undef jmpbuf in pngtest.c to undo #define in AIX headers. ++ Added scripts/makefile.nommx ++ ++Version 1.2.15rc3 [December 25, 2006] ++ Fixed shared library numbering error that was introduced in 1.2.15beta6. ++ ++Version 1.2.15rc4 [December 27, 2006] ++ Fixed handling of rgb_to_gray when png_ptr->color.gray isn't set. ++ ++Version 1.2.15rc5 [December 31, 2006] ++ Revised handling of rgb_to_gray. ++ ++Version 1.2.15 [January 5, 2007] ++ Added some (unsigned long) typecasts in pngtest.c to avoid printing errors. ++ ++Version 1.2.16beta1 [January 6, 2007] ++ Fix bugs in makefile.nommx ++ ++Version 1.2.16beta2 [January 16, 2007] ++ Revised scripts/CMakeLists.txt ++ ++Version 1.2.16 [January 31, 2007] ++ No changes. ++ ++Version 1.2.17beta1 [March 6, 2007] ++ Revised scripts/CMakeLists.txt to install both shared and static libraries. ++ Deleted a redundant line from pngset.c. ++ ++Version 1.2.17beta2 [April 26, 2007] ++ Relocated misplaced test for png_ptr == NULL in pngpread.c ++ Change "==" to "&" for testing PNG_RGB_TO_GRAY_ERR & PNG_RGB_TO_GRAY_WARN ++ flags. ++ Changed remaining instances of PNG_ASSEMBLER_* to PNG_MMX_* ++ Added pngerror() when write_IHDR fails in deflateInit2(). ++ Added "const" to some array declarations. ++ Mention examples of libpng usage in the libpng*.txt and libpng.3 documents. ++ ++Version 1.2.17rc1 [May 4, 2007] ++ No changes. ++ ++Version 1.2.17rc2 [May 8, 2007] ++ Moved several PNG_HAVE_* macros out of PNG_INTERNAL because applications ++ calling set_unknown_chunk_location() need them. ++ Changed transformation flag from PNG_EXPAND_tRNS to PNG_EXPAND in ++ png_set_expand_gray_1_2_4_to_8(). ++ Added png_ptr->unknown_chunk to hold working unknown chunk data, so it ++ can be free'ed in case of error. Revised unknown chunk handling in ++ pngrutil.c and pngpread.c to use this structure. ++ ++Version 1.2.17rc3 [May 8, 2007] ++ Revised symbol-handling in configure script. ++ ++Version 1.2.17rc4 [May 10, 2007] ++ Revised unknown chunk handling to avoid storing unknown critical chunks. ++ ++Version 1.0.25 [May 15, 2007] ++Version 1.2.17 [May 15, 2007] ++ Added "png_ptr->num_trans=0" before error return in png_handle_tRNS, ++ to eliminate a vulnerability (CVE-2007-2445, CERT VU#684664) ++ ++Version 1.0.26 [May 15, 2007] ++Version 1.2.18 [May 15, 2007] ++ Reverted the libpng-1.2.17rc3 change to symbol-handling in configure script ++ ++Version 1.2.19beta1 [May 18, 2007] ++ Changed "const static" to "static PNG_CONST" everywhere, mostly undoing ++ change of libpng-1.2.17beta2. Changed other "const" to "PNG_CONST" ++ Changed some handling of unused parameters, to avoid compiler warnings. ++ "if (unused == NULL) return;" becomes "unused = unused". ++ ++Version 1.2.19beta2 [May 18, 2007] ++ Only use the valid bits of tRNS value in png_do_expand() (Brian Cartier) ++ ++Version 1.2.19beta3 [May 19, 2007] ++ Add some "png_byte" typecasts in png_check_keyword() and write new_key ++ instead of key in zTXt chunk (Kevin Ryde). ++ ++Version 1.2.19beta4 [May 21, 2007] ++ Add png_snprintf() function and use it in place of sprint() for improved ++ defense against buffer overflows. ++ ++Version 1.2.19beta5 [May 21, 2007] ++ Fixed png_handle_tRNS() to only use the valid bits of tRNS value. ++ Changed handling of more unused parameters, to avoid compiler warnings. ++ Removed some PNG_CONST in pngwutil.c to avoid compiler warnings. ++ ++Version 1.2.19beta6 [May 22, 2007] ++ Added some #ifdef PNG_MMX_CODE_SUPPORTED where needed in pngvcrd.c ++ Added a special "_MSC_VER" case that defines png_snprintf to _snprintf ++ ++Version 1.2.19beta7 [May 22, 2007] ++ Squelched png_squelch_warnings() in pnggccrd.c and added ++ an #ifdef PNG_MMX_CODE_SUPPORTED block around the declarations that caused ++ the warnings that png_squelch_warnings was squelching. ++ ++Version 1.2.19beta8 [May 22, 2007] ++ Removed __MMX__ from test in pngconf.h. ++ ++Version 1.2.19beta9 [May 23, 2007] ++ Made png_squelch_warnings() available via PNG_SQUELCH_WARNINGS macro. ++ Revised png_squelch_warnings() so it might work. ++ Updated makefile.sgcc and makefile.solaris; added makefile.solaris-x86. ++ ++Version 1.2.19beta10 [May 24, 2007] ++ Resquelched png_squelch_warnings(), use "__attribute__((used))" instead. ++ ++Version 1.4.0beta1 [April 20, 2006] ++ Enabled iTXt support (changes png_struct, thus requires so-number change). ++ Cleaned up PNG_ASSEMBLER_CODE_SUPPORTED vs PNG_MMX_CODE_SUPPORTED ++ Eliminated PNG_1_0_X and PNG_1_2_X macros. ++ Removed deprecated functions png_read_init, png_write_init, png_info_init, ++ png_permit_empty_plte, png_set_gray_1_2_4_to_8, png_check_sig, and ++ removed the deprecated macro PNG_MAX_UINT. ++ Moved "PNG_INTERNAL" parts of png.h and pngconf.h into pngintrn.h ++ Removed many WIN32_WCE #ifdefs (Cosmin). ++ Reduced dependency on C-runtime library when on Windows (Simon-Pierre) ++ Replaced sprintf() with png_sprintf() (Simon-Pierre) ++ ++Version 1.4.0beta2 [April 20, 2006] ++ Revised makefiles and configure to avoid making links to libpng.so.* ++ Moved some leftover MMX-related defines from pngconf.h to pngintrn.h ++ Updated scripts/pngos2.def, pngw32.def, and projects/wince/png32ce.def ++ ++Version 1.4.0beta3 [May 10, 2006] ++ Updated scripts/pngw32.def to comment out MMX functions. ++ Added PNG_NO_GET_INT_32 and PNG_NO_SAVE_INT_32 macros. ++ Scripts/libpng.pc.in contained "configure" style version info and would ++ not work with makefiles. ++ Revised pngconf.h and added pngconf.h.in, so makefiles and configure can ++ pass defines to libpng and applications. ++ ++Version 1.4.0beta4 [May 11, 2006] ++ Revised configure.ac, Makefile.am, and many of the makefiles to write ++ their defines in pngconf.h. ++ ++Version 1.4.0beta5 [May 15, 2006] ++ Added a missing semicolon in Makefile.am and Makefile.in ++ Deleted extraneous square brackets from configure.ac ++ ++Version 1.4.0beta6 [June 2, 2006] ++ Increased sprintf buffer from 50 to 52 chars in pngrutil.c to avoid ++ buffer overflow. ++ Changed sonum from 0 to 1. ++ Removed unused prototype for png_check_sig() from png.h ++ ++Version 1.4.0beta7 [June 16, 2006] ++ Exported png_write_sig (Cosmin). ++ Optimized buffer in png_handle_cHRM() (Cosmin). ++ Set pHYs = 2835 x 2835 pixels per meter, and added ++ sCAL = 0.352778e-3 x 0.352778e-3 meters, in pngtest.png (Cosmin). ++ Added png_set_benign_errors(), png_benign_error(), png_chunk_benign_error(). ++ Added typedef for png_int_32 and png_uint_32 on 64-bit systems. ++ Added "(unsigned long)" typecast on png_uint_32 variables in printf lists. ++ ++Version 1.4.0beta8 [June 22, 2006] ++ Added demonstration of user chunk support in pngtest.c, to support the ++ public sTER chunk and a private vpAg chunk. ++ ++Version 1.4.0beta9 [July 3, 2006] ++ Removed ordinals from scripts/pngw32.def and removed png_info_int and ++ png_set_gray_1_2_4_to_8 entries. ++ Inline call of png_get_uint_32() in png_get_uint_31(). ++ Use png_get_uint_31() to get vpAg width and height in pngtest.c ++ Removed WINCE and Netware projects. ++ Removed standalone Y2KINFO file. ++ ++Version 1.4.0beta10 [July 12, 2006] ++ Eliminated automatic copy of pngconf.h to pngconf.h.in from configure and ++ some makefiles, because it was not working reliably. Instead, distribute ++ pngconf.h.in along with pngconf.h and cause configure and some of the ++ makefiles to update pngconf.h from pngconf.h.in. ++ Added pngconf.h to DEPENDENCIES in Makefile.am ++ ++Version 1.4.0beta11 [August 19, 2006] ++ Removed AC_FUNC_MALLOC from configure.ac. ++ Added a warning when writing iCCP profile with mismatched profile length. ++ Patched pnggccrd.c to assemble on x86_64 platforms. ++ Moved chunk header reading into a separate function png_read_chunk_header() ++ in pngrutil.c. The chunk header (len+sig) is now serialized in a single ++ operation (Cosmin). ++ Implemented support for I/O states. Added png_ptr member io_state, and ++ functions png_get_io_chunk_name() and png_get_io_state() in pngget.c ++ (Cosmin). ++ Added png_get_io_chunk_name and png_get_io_state to scripts/*.def (Cosmin). ++ Renamed scripts/pngw32.* to scripts/pngwin.* (Cosmin). ++ Removed the include directories and libraries from CFLAGS and LDFLAGS ++ in scripts/makefile.gcc (Cosmin). ++ Used png_save_uint_32() to set vpAg width and height in pngtest.c (Cosmin). ++ Cast to proper type when getting/setting vpAg units in pngtest.c (Cosmin). ++ Added pngintrn.h to the Visual C++ projects (Cosmin). ++ Removed scripts/list (Cosmin). ++ Updated copyright year in scripts/pngwin.def (Cosmin). ++ Removed PNG_TYPECAST_NULL and used standard NULL consistently (Cosmin). ++ Disallowed the user to redefine png_size_t, and enforced a consistent use ++ of png_size_t across libpng (Cosmin). ++ Changed the type of png_ptr->rowbytes, PNG_ROWBYTES() and friends ++ to png_size_t (Cosmin). ++ Removed png_convert_size() and replaced png_sizeof with sizeof (Cosmin). ++ Removed some unnecessary type casts (Cosmin). ++ Changed prototype of png_get_compression_buffer_size() and ++ png_set_compression_buffer_size() to work with png_size_t instead of ++ png_uint_32 (Cosmin). ++ Removed png_memcpy_check() and png_memset_check() (Cosmin). ++ Fixed a typo (png_byte --> png_bytep) in libpng.3 and libpng.txt (Cosmin). ++ Clarified that png_zalloc() does not clear the allocated memory, ++ and png_zalloc() and png_zfree() cannot be PNGAPI (Cosmin). ++ Renamed png_mem_size_t to png_alloc_size_t, fixed its definition in ++ pngconf.h, and used it in all memory allocation functions (Cosmin). ++ Renamed pngintrn.h to pngpriv.h, added a comment at the top of the file ++ mentioning that the symbols declared in that file are private, and ++ updated the scripts and the Visual C++ projects accordingly (Cosmin). ++ Removed circular references between pngconf.h and pngconf.h.in in ++ scripts/makefile.vc*win32 (Cosmin). ++ Removing trailing '.' from the warning and error messages (Cosmin). ++ Added pngdefs.h that is built by makefile or configure, instead of ++ pngconf.h.in (Glenn). ++ Detect and fix attempt to write wrong iCCP profile length. ++ ++Version 1.4.0beta12 [October 19, 2006] ++ Changed "logical" to "bitwise" in the documentation. ++ Work around Intel-Mac compiler bug by setting PNG_NO_MMX_CODE in pngconf.h ++ Add a typecast to stifle compiler warning in pngrutil.c ++ ++Version 1.4.0beta13 [November 10, 2006] ++ Fix potential buffer overflow in sPLT chunk handler. ++ Fix Makefile.am to not try to link to noexistent files. ++ ++Version 1.4.0beta14 [November 15, 2006] ++ Check all exported functions for NULL png_ptr. ++ ++Version 1.4.0beta15 [November 17, 2006] ++ Relocated two misplaced tests for NULL png_ptr. ++ Built Makefile.in with automake-1.9.6 instead of 1.9.2. ++ Build configure with autoconf-2.60 instead of 2.59 ++ Add "install: all" in Makefile.am so "configure; make install" will work. ++ ++Version 1.4.0beta16 [November 17, 2006] ++ Added a typecast in png_zalloc(). ++ ++Version 1.4.0beta17 [December 4, 2006] ++ Changed "new_key[79] = '\0';" to "(*new_key)[79] = '\0';" in pngwutil.c ++ Add "png_bytep" typecast to profile while calculating length in pngwutil.c ++ ++Version 1.4.0beta18 [December 7, 2006] ++ Added scripts/CMakeLists.txt ++ ++Version 1.4.0beta19 [May 16, 2007] ++ Revised scripts/CMakeLists.txt ++ Rebuilt configure and Makefile.in with newer tools. ++ Added conditional #undef jmpbuf in pngtest.c to undo #define in AIX headers. ++ Added scripts/makefile.nommx ++ ++Version 1.4.0beta20 [July 9, 2008] ++ Moved several PNG_HAVE_* macros from pngpriv.h to png.h because applications ++ calling set_unknown_chunk_location() need them. ++ Moved several macro definitions from pngpriv.h to pngconf.h ++ Merge with changes to the 1.2.X branch, as of 1.2.30beta04. ++ Deleted all use of the MMX assembler code and Intel-licensed optimizations. ++ Revised makefile.mingw ++ ++Version 1.4.0beta21 [July 21, 2008] ++ Moved local array "chunkdata" from pngrutil.c to the png_struct, so ++ it will be freed by png_read_destroy() in case of a read error (Kurt ++ Christensen). ++ ++Version 1.4.0beta22 [July 21, 2008] ++ Change "purpose" and "buffer" to png_ptr->chunkdata to avoid memory leaking. ++ ++Version 1.4.0beta23 [July 22, 2008] ++ Change "chunkdata = NULL" to "png_ptr->chunkdata = NULL" several places in ++ png_decompress_chunk(). ++ ++Version 1.4.0beta24 [July 25, 2008] ++ Change all remaining "chunkdata" to "png_ptr->chunkdata" in ++ png_decompress_chunk(), and remove "chunkdata" from parameter list. ++ Put a call to png_check_chunk_name() in png_read_chunk_header(). ++ Revised png_check_chunk_name() to reject a name with a lowercase 3rd byte. ++ Removed two calls to png_check_chunk_name() occurring later in the process. ++ Define PNG_NO_ERROR_NUMBERS by default in pngconf.h ++ ++Version 1.4.0beta25 [July 30, 2008] ++ Added a call to png_check_chunk_name() in pngpread.c ++ Reverted png_check_chunk_name() to accept a name with a lowercase 3rd byte. ++ Added png_push_have_buffer() function to pngpread.c ++ Eliminated PNG_BIG_ENDIAN_SUPPORTED and associated png_get_* macros. ++ Made inline expansion of png_get_*() optional with PNG_USE_READ_MACROS. ++ Eliminated all PNG_USELESS_TESTS and PNG_CORRECT_PALETTE_SUPPORTED code. ++ Synced contrib directory and configure files with libpng-1.2.30beta06. ++ Eliminated no-longer-used pngdefs.h (but it's still built in the makefiles) ++ Relocated a misplaced "#endif /* PNG_NO_WRITE_FILTER */" in pngwutil.c ++ ++Version 1.4.0beta26 [August 4, 2008] ++ Removed png_push_have_buffer() function in pngpread.c. It increased the ++ compiled library size slightly. ++ Changed "-Wall" to "-W -Wall" in the CFLAGS in all makefiles (Cosmin Truta) ++ Declared png_ptr "volatile" in pngread.c and pngwrite.c to avoid warnings. ++ Updated contrib/visupng/cexcept.h to version 2.0.1 ++ Added PNG_LITERAL_CHARACTER macros for #, [, and ]. ++ ++Version 1.4.0beta27 [August 5, 2008] ++ Revised usage of PNG_LITERAL_SHARP in pngerror.c. ++ Moved newline character from individual png_debug messages into the ++ png_debug macros. ++ Allow user to #define their own png_debug, png_debug1, and png_debug2. ++ ++Version 1.4.0beta28 [August 5, 2008] ++ Revised usage of PNG_LITERAL_SHARP in pngerror.c. ++ Added PNG_STRING_NEWLINE macro ++ ++Version 1.4.0beta29 [August 9, 2008] ++ Revised usage of PNG_STRING_NEWLINE to work on non-ISO compilers. ++ Added PNG_STRING_COPYRIGHT macro. ++ Added non-ISO versions of png_debug macros. ++ ++Version 1.4.0beta30 [August 14, 2008] ++ Added premultiplied alpha feature (Volker Wiendl). ++ ++Version 1.4.0beta31 [August 18, 2008] ++ Moved png_set_premultiply_alpha from pngtrans.c to pngrtran.c ++ Removed extra crc check at the end of png_handle_cHRM(). Bug introduced ++ in libpng-1.4.0beta20. ++ ++Version 1.4.0beta32 [August 19, 2008] ++ Added PNG_WRITE_FLUSH_SUPPORTED block around new png_flush() call. ++ Revised PNG_NO_STDIO version of png_write_flush() ++ ++Version 1.4.0beta33 [August 20, 2008] ++ Added png_get|set_chunk_cache_max() to limit the total number of sPLT, ++ text, and unknown chunks that can be stored. ++ ++Version 1.4.0beta34 [September 6, 2008] ++ Shortened tIME_string to 29 bytes in pngtest.c ++ Fixed off-by-one error introduced in png_push_read_zTXt() function in ++ libpng-1.2.30beta04/pngpread.c (Harald van Dijk) ++ ++Version 1.4.0beta35 [October 6, 2008] ++ Changed "trans_values" to "trans_color". ++ Changed so-number from 0 to 14. Some OS do not like 0. ++ Revised makefile.darwin to fix shared library numbering. ++ Change png_set_gray_1_2_4_to_8() to png_set_expand_gray_1_2_4_to_8() ++ in example.c (debian bug report) ++ ++Version 1.4.0beta36 [October 25, 2008] ++ Sync with tEXt vulnerability fix in libpng-1.2.33rc02. ++ ++Version 1.4.0beta37 [November 13, 2008] ++ Added png_check_cHRM in png.c and moved checking from pngget.c, pngrutil.c, ++ and pngwrite.c ++ ++Version 1.4.0beta38 [November 22, 2008] ++ Added check for zero-area RGB cHRM triangle in png_check_cHRM() and ++ png_check_cHRM_fixed(). ++ ++Version 1.4.0beta39 [November 23, 2008] ++ Revised png_warning() to write its message on standard output by default ++ when warning_fn is NULL. ++ ++Version 1.4.0beta40 [November 24, 2008] ++ Eliminated png_check_cHRM(). Instead, always use png_check_cHRM_fixed(). ++ In png_check_cHRM_fixed(), ensure white_y is > 0, and removed redundant ++ check for all-zero coordinates that is detected by the triangle check. ++ ++Version 1.4.0beta41 [November 26, 2008] ++ Fixed string vs pointer-to-string error in png_check_keyword(). ++ Rearranged test expressions in png_check_cHRM_fixed() to avoid internal ++ overflows. ++ Added PNG_NO_CHECK_cHRM conditional. ++ ++Version 1.4.0beta42, 43 [December 1, 2008] ++ Merge png_debug with version 1.2.34beta04. ++ ++Version 1.4.0beta44 [December 6, 2008] ++ Removed redundant check for key==NULL before calling png_check_keyword() ++ to ensure that new_key gets initialized and removed extra warning ++ (Merge with version 1.2.34beta05 -- Arvan Pritchard). ++ ++Version 1.4.0beta45 [December 9, 2008] ++ In png_write_png(), respect the placement of the filler bytes in an earlier ++ call to png_set_filler() (Jim Barry). ++ ++Version 1.4.0beta46 [December 10, 2008] ++ Undid previous change and added PNG_TRANSFORM_STRIP_FILLER_BEFORE and ++ PNG_TRANSFORM_STRIP_FILLER_AFTER conditionals and deprecated ++ PNG_TRANSFORM_STRIP_FILLER (Jim Barry). ++ ++Version 1.4.0beta47 [December 15, 2008] ++ Support for dithering was disabled by default, because it has never ++ been well tested and doesn't work very well. The code has not ++ been removed, however, and can be enabled by building libpng with ++ PNG_READ_DITHER_SUPPORTED defined. ++ ++Version 1.4.0beta48 [February 14, 2009] ++ Added new exported function png_calloc(). ++ Combined several instances of png_malloc(); png_memset() into png_calloc(). ++ Removed prototype for png_freeptr() that was added in libpng-1.4.0beta24 ++ but was never defined. ++ ++Version 1.4.0beta49 [February 28, 2009] ++ Added png_fileno() macro to pngconf.h, used in pngwio.c ++ Corrected order of #ifdef's in png_debug definition in png.h ++ Fixed bug introduced in libpng-1.4.0beta48 with the memset arguments ++ for pcal_params. ++ Fixed order of #ifdef directives in the png_debug defines in png.h ++ (bug introduced in libpng-1.2.34/1.4.0beta29). ++ Revised comments in png_set_read_fn() and png_set_write_fn(). ++ ++Version 1.4.0beta50 [March 18, 2009] ++ Use png_calloc() instead of png_malloc() to allocate big_row_buf when ++ reading an interlaced file, to avoid a possible UMR. ++ Undid revision of PNG_NO_STDIO version of png_write_flush(). Users ++ having trouble with fflush() can build with PNG_NO_WRITE_FLUSH defined ++ or supply their own flush_fn() replacement. ++ Revised libpng*.txt and png.h documentation about use of png_write_flush() ++ and png_set_write_fn(). ++ Removed fflush() from pngtest.c. ++ Added "#define PNG_NO_WRITE_FLUSH" to contrib/pngminim/encoder/pngusr.h ++ ++Version 1.4.0beta51 [March 21, 2009] ++ Removed new png_fileno() macro from pngconf.h . ++ ++Version 1.4.0beta52 [March 27, 2009] ++ Relocated png_do_chop() ahead of building gamma tables in pngrtran.c ++ This avoids building 16-bit gamma tables unnecessarily. ++ Removed fflush() from pngtest.c. ++ Added "#define PNG_NO_WRITE_FLUSH" to contrib/pngminim/encoder/pngusr.h ++ Added a section on differences between 1.0.x and 1.2.x to libpng.3/libpng.txt ++ ++Version 1.4.0beta53 [April 1, 2009] ++ Removed some remaining MMX macros from pngpriv.h ++ Fixed potential memory leak of "new_name" in png_write_iCCP() (Ralph Giles) ++ ++Version 1.4.0beta54 [April 13, 2009] ++ Added "ifndef PNG_SKIP_SETJMP_CHECK" block in pngconf.h to allow ++ application code writers to bypass the check for multiple inclusion ++ of setjmp.h when they know that it is safe to ignore the situation. ++ Eliminated internal use of setjmp() in pngread.c and pngwrite.c ++ Reordered ancillary chunks in pngtest.png to be the same as what ++ pngtest now produces, and made some cosmetic changes to pngtest output. ++ Eliminated deprecated png_read_init_3() and png_write_init_3() functions. ++ ++Version 1.4.0beta55 [April 15, 2009] ++ Simplified error handling in pngread.c and pngwrite.c by putting ++ the new png_read_cleanup() and png_write_cleanup() functions inline. ++ ++Version 1.4.0beta56 [April 25, 2009] ++ Renamed "user_chunk_data" to "my_user_chunk_data" in pngtest.c to suppress ++ "shadowed declaration" warning from gcc-4.3.3. ++ Renamed "gamma" to "png_gamma" in pngset.c to avoid "shadowed declaration" ++ warning about a global "gamma" variable in math.h on some platforms. ++ ++Version 1.4.0beta57 [May 2, 2009] ++ Removed prototype for png_freeptr() that was added in libpng-1.4.0beta24 ++ but was never defined (again). ++ Rebuilt configure scripts with autoconf-2.63 instead of 2.62 ++ Removed pngprefs.h and MMX from makefiles ++ ++Version 1.4.0beta58 [May 14, 2009] ++ Changed pngw32.def to pngwin.def in makefile.mingw (typo was introduced ++ in beta57). ++ Clarified usage of sig_bit versus sig_bit_p in example.c (Vincent Torri) ++ ++Version 1.4.0beta59 [May 15, 2009] ++ Reformated sources in libpng style (3-space intentation, comment format) ++ Fixed typo in libpng docs (PNG_FILTER_AVE should be PNG_FILTER_AVG) ++ Added sections about the git repository and our coding style to the ++ documentation ++ Relocated misplaced #endif in pngwrite.c, sCAL chunk handler. ++ ++Version 1.4.0beta60 [May 19, 2009] ++ Conditionally compile png_read_finish_row() which is not used by ++ progressive readers. ++ Added contrib/pngminim/preader to demonstrate building minimal progressive ++ decoder, based on contrib/gregbook with embedded libpng and zlib. ++ ++Version 1.4.0beta61 [May 20, 2009] ++ In contrib/pngminim/*, renamed "makefile.std" to "makefile", since there ++ is only one makefile in those directories, and revised the README files ++ accordingly. ++ More reformatting of comments, mostly to capitalize sentences. ++ ++Version 1.4.0beta62 [June 2, 2009] ++ Added "#define PNG_NO_WRITE_SWAP" to contrib/pngminim/encoder/pngusr.h ++ and "define PNG_NO_READ_SWAP" to decoder/pngusr.h and preader/pngusr.h ++ Reformatted several remaining "else statement" into two lines. ++ Added a section to the libpng documentation about using png_get_io_ptr() ++ in configure scripts to detect the presence of libpng. ++ ++Version 1.4.0beta63 [June 15, 2009] ++ Revised libpng*.txt and libpng.3 to mention calling png_set_IHDR() ++ multiple times and to specify the sample order in the tRNS chunk, ++ because the ISO PNG specification has a typo in the tRNS table. ++ Changed several PNG_UNKNOWN_CHUNK_SUPPORTED to ++ PNG_HANDLE_AS_UNKNOWN_SUPPORTED, to make the png_set_keep mechanism ++ available for ignoring known chunks even when not saving unknown chunks. ++ Adopted preference for consistent use of "#ifdef" and "#ifndef" versus ++ "#if defined()" and "if !defined()" where possible. ++ ++Version 1.4.0beta64 [June 24, 2009] ++ Eliminated PNG_LEGACY_SUPPORTED code. ++ Moved the various unknown chunk macro definitions outside of the ++ PNG_READ|WRITE_ANCILLARY_CHUNK_SUPPORTED blocks. ++ ++Version 1.4.0beta65 [June 26, 2009] ++ Added a reference to the libpng license in each file. ++ ++Version 1.4.0beta66 [June 27, 2009] ++ Refer to the libpng license instead of the libpng license in each file. ++ ++Version 1.4.0beta67 [July 6, 2009] ++ Relocated INVERT_ALPHA within png_read_png() and png_write_png(). ++ Added high-level API transform PNG_TRANSFORM_GRAY_TO_RGB. ++ Added an "xcode" project to the projects directory (Alam Arias). ++ ++Version 1.4.0beta68 [July 19, 2009] ++ Avoid some tests in filter selection in pngwutil.c ++ ++Version 1.4.0beta69 [July 25, 2009] ++ Simplified the new filter-selection test. This runs faster in the ++ common "PNG_ALL_FILTERS" and PNG_FILTER_NONE cases. ++ Removed extraneous declaration from the new call to png_read_gray_to_rgb() ++ (bug introduced in libpng-1.4.0beta67). ++ Fixed up xcode project (Alam Arias) ++ Added a prototype for png_64bit_product() in png.c ++ ++Version 1.4.0beta70 [July 27, 2009] ++ Avoid a possible NULL dereference in debug build, in png_set_text_2(). ++ (bug introduced in libpng-0.95, discovered by Evan Rouault) ++ ++Version 1.4.0beta71 [July 29, 2009] ++ Rebuilt configure scripts with autoconf-2.64. ++ ++Version 1.4.0beta72 [August 1, 2009] ++ Replaced *.tar.lzma with *.tar.xz in distribution. Get the xz codec ++ from . ++ ++Version 1.4.0beta73 [August 1, 2009] ++ Reject attempt to write iCCP chunk with negative embedded profile length ++ (JD Chen) (CVE-2009-5063). ++ ++Version 1.4.0beta74 [August 8, 2009] ++ Changed png_ptr and info_ptr member "trans" to "trans_alpha". ++ ++Version 1.4.0beta75 [August 21, 2009] ++ Removed an extra png_debug() recently added to png_write_find_filter(). ++ Fixed incorrect #ifdef in pngset.c regarding unknown chunk support. ++ ++Version 1.4.0beta76 [August 22, 2009] ++ Moved an incorrectly located test in png_read_row() in pngread.c ++ ++Version 1.4.0beta77 [August 27, 2009] ++ Removed lpXYZ.tar.bz2 (with CRLF), KNOWNBUG, libpng-x.y.z-KNOWNBUG.txt, ++ and the "noconfig" files from the distribution. ++ Moved CMakeLists.txt from scripts into the main libpng directory. ++ Various bugfixes and improvements to CMakeLists.txt (Philip Lowman) ++ ++Version 1.4.0beta78 [August 31, 2009] ++ Converted all PNG_NO_* tests to PNG_*_SUPPORTED everywhere except pngconf.h ++ Eliminated PNG_NO_FREE_ME and PNG_FREE_ME_SUPPORTED macros. ++ Use png_malloc plus a loop instead of png_calloc() to initialize ++ row_pointers in png_read_png(). ++ ++Version 1.4.0beta79 [September 1, 2009] ++ Eliminated PNG_GLOBAL_ARRAYS and PNG_LOCAL_ARRAYS; always use local arrays. ++ Eliminated PNG_CALLOC_SUPPORTED macro and always provide png_calloc(). ++ ++Version 1.4.0beta80 [September 17, 2009] ++ Removed scripts/libpng.icc ++ Changed typecast of filler from png_byte to png_uint_16 in png_set_filler(). ++ (Dennis Gustafsson) ++ Fixed typo introduced in beta78 in pngtest.c ("#if def " should be "#ifdef ") ++ ++Version 1.4.0beta81 [September 23, 2009] ++ Eliminated unused PNG_FLAG_FREE_* defines from pngpriv.h ++ Expanded TAB characters in pngrtran.c ++ Removed PNG_CONST from all "PNG_CONST PNG_CHNK" declarations to avoid ++ compiler complaints about doubly declaring things "const". ++ Changed all "#if [!]defined(X)" to "if[n]def X" where possible. ++ Eliminated unused png_ptr->row_buf_size ++ ++Version 1.4.0beta82 [September 25, 2009] ++ Moved redundant IHDR checking into new png_check_IHDR() in png.c ++ and report all errors found in the IHDR data. ++ Eliminated useless call to png_check_cHRM() from pngset.c ++ ++Version 1.4.0beta83 [September 25, 2009] ++ Revised png_check_IHDR() to eliminate bogus complaint about filter_type. ++ ++Version 1.4.0beta84 [September 30, 2009] ++ Fixed some inconsistent indentation in pngconf.h ++ Revised png_check_IHDR() to add a test for width variable less than 32-bit. ++ ++Version 1.4.0beta85 [October 1, 2009] ++ Revised png_check_IHDR() again, to check info_ptr members instead of ++ the contents of the returned parameters. ++ ++Version 1.4.0beta86 [October 9, 2009] ++ Updated the "xcode" project (Alam Arias). ++ Eliminated a shadowed declaration of "pp" in png_handle_sPLT(). ++ ++Version 1.4.0rc01 [October 19, 2009] ++ Trivial cosmetic changes. ++ ++Version 1.4.0beta87 [October 30, 2009] ++ Moved version 1.4.0 back into beta. ++ ++Version 1.4.0beta88 [October 30, 2009] ++ Revised libpng*.txt section about differences between 1.2.x and 1.4.0 ++ because most of the new features have now been ported back to 1.2.41 ++ ++Version 1.4.0beta89 [November 1, 2009] ++ More bugfixes and improvements to CMakeLists.txt (Philip Lowman) ++ Removed a harmless extra png_set_invert_alpha() from pngwrite.c ++ Apply png_user_chunk_cache_max within png_decompress_chunk(). ++ Merged libpng-1.2.41.txt with libpng-1.4.0.txt where appropriate. ++ ++Version 1.4.0beta90 [November 2, 2009] ++ Removed all remaining WIN32_WCE #ifdefs except those involving the ++ time.h "tm" structure ++ ++Version 1.4.0beta91 [November 3, 2009] ++ Updated scripts/pngw32.def and projects/wince/png32ce.def ++ Copied projects/wince/png32ce.def to the scripts directory. ++ Added scripts/makefile.wce ++ Patched ltmain.sh for wince support. ++ Added PNG_CONVERT_tIME_SUPPORTED macro. ++ ++Version 1.4.0beta92 [November 4, 2009] ++ Make inclusion of time.h in pngconf.h depend on PNG_CONVERT_tIME_SUPPORTED ++ Make #define PNG_CONVERT_tIME_SUPPORTED depend on PNG_WRITE_tIME_SUPPORTED ++ Revised libpng*.txt to describe differences from 1.2.40 to 1.4.0 (instead ++ of differences from 1.2.41 to 1.4.0) ++ ++Version 1.4.0beta93 [November 7, 2009] ++ Added PNG_DEPSTRUCT, PNG_DEPRECATED, PNG_USE_RESULT, PNG_NORETURN, and ++ PNG_ALLOCATED macros to detect deprecated direct access to the ++ png_struct or info_struct members and other deprecated usage in ++ applications (John Bowler). ++ Updated scripts/makefile* to add "-DPNG_CONFIGURE_LIBPNG" to CFLAGS, ++ to prevent warnings about direct access to png structs by libpng ++ functions while building libpng. They need to be tested, especially ++ those using compilers other than gcc. ++ Updated projects/visualc6 and visualc71 with "/d PNG_CONFIGURE_LIBPNG". ++ They should work but still need to be updated to remove ++ references to pnggccrd.c or pngvcrd.c and ASM building. ++ Added README.txt to the beos, cbuilder5, netware, and xcode projects warning ++ that they need to be updated, to remove references to pnggccrd.c and ++ pngvcrd.c and to depend on pngpriv.h ++ Removed three direct references to read_info_ptr members in pngtest.c ++ that were detected by the new PNG_DEPSTRUCT macro. ++ Moved the png_debug macro definitions and the png_read_destroy(), ++ png_write_destroy() and png_far_to_near() prototypes from png.h ++ to pngpriv.h (John Bowler) ++ Moved the synopsis lines for png_read_destroy(), png_write_destroy() ++ png_debug(), png_debug1(), and png_debug2() from libpng.3 to libpngpf.3. ++ ++Version 1.4.0beta94 [November 9, 2009] ++ Removed the obsolete, unused pnggccrd.c and pngvcrd.c files. ++ Updated CMakeLists.txt to add "-DPNG_CONFIGURE_LIBPNG" to the definitions. ++ Removed dependency of pngtest.o on pngpriv.h in the makefiles. ++ Only #define PNG_DEPSTRUCT, etc. in pngconf.h if not already defined. ++ ++Version 1.4.0beta95 [November 10, 2009] ++ Changed png_check_sig() to !png_sig_cmp() in contrib programs. ++ Added -DPNG_CONFIGURE_LIBPNG to contrib/pngminm/*/makefile ++ Changed png_check_sig() to !png_sig_cmp() in contrib programs. ++ Corrected the png_get_IHDR() call in contrib/gregbook/readpng2.c ++ Changed pngminim/*/gather.sh to stop trying to remove pnggccrd.c and pngvcrd.c ++ Added dependency on pngpriv.h in contrib/pngminim/*/makefile ++ ++Version 1.4.0beta96 [November 12, 2009] ++ Renamed scripts/makefile.wce to scripts/makefile.cegcc ++ Revised Makefile.am to use libpng.sys while building libpng.so ++ so that only PNG_EXPORT functions are exported. ++ Removed the deprecated png_check_sig() function/macro. ++ Removed recently removed function names from scripts/*.def ++ Revised pngtest.png to put chunks in the same order written by pngtest ++ (evidently the same change made in libpng-1.0beta54 was lost). ++ Added PNG_PRIVATE macro definition in pngconf.h for possible future use. ++ ++Version 1.4.0beta97 [November 13, 2009] ++ Restored pngtest.png to the libpng-1.4.0beta7 version. ++ Removed projects/beos and netware.txt; no one seems to be supporting them. ++ Revised Makefile.in ++ ++Version 1.4.0beta98 [November 13, 2009] ++ Added the "xcode" project to zip distributions, ++ Fixed a typo in scripts/pngwin.def introduced in beta97. ++ ++Version 1.4.0beta99 [November 14, 2009] ++ Moved libpng-config.in and libpng.pc-configure.in out of the scripts ++ directory, to libpng-config.in and libpng-pc.in, respectively, and ++ modified Makefile.am and configure.ac accordingly. Now "configure" ++ needs nothing from the "scripts" directory. ++ Avoid redefining PNG_CONST in pngconf.h ++ ++Version 1.4.0beta100 [November 14, 2009] ++ Removed ASM builds from projects/visualc6 and projects/visualc71 ++ Removed scripts/makefile.nommx and makefile.vcawin32 ++ Revised CMakeLists.txt to account for new location of libpng-config.in ++ and libpng-pc.in ++ Updated INSTALL to reflect removal and relocation of files. ++ ++Version 1.4.0beta101 [November 14, 2009] ++ Restored the binary files (*.jpg, *.png, some project files) that were ++ accidentally deleted from the zip and 7z distributions when the xcode ++ project was added. ++ ++Version 1.4.0beta102 [November 18, 2009] ++ Added libpng-config.in and libpng-pc.in to the zip and 7z distributions. ++ Fixed a typo in projects/visualc6/pngtest.dsp, introduced in beta100. ++ Moved descriptions of makefiles and other scripts out of INSTALL into ++ scripts/README.txt ++ Updated the copyright year in scripts/pngwin.rc from 2006 to 2009. ++ ++Version 1.4.0beta103 [November 21, 2009] ++ Removed obsolete comments about ASM from projects/visualc71/README_zlib.txt ++ Align row_buf on 16-byte boundary in memory. ++ Restored the PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED guard around the call ++ to png_flush() after png_write_IEND(). See 1.4.0beta32, 1.4.0beta50 ++ changes above and 1.2.30, 1.2.30rc01 and rc03 in 1.2.41 CHANGES. Someone ++ needs this feature. ++ Make the 'png_jmpbuf' macro expand to a call that records the correct ++ longjmp function as well as returning a pointer to the setjmp ++ jmp_buf buffer, and marked direct access to jmpbuf 'deprecated'. ++ (John Bowler) ++ ++Version 1.4.0beta104 [November 22, 2009] ++ Removed png_longjmp_ptr from scripts/*.def and libpng.3 ++ Rebuilt configure scripts with autoconf-2.65 ++ ++Version 1.4.0beta105 [November 25, 2009] ++ Use fast integer PNG_DIVIDE_BY_255() or PNG_DIVIDE_BY_65535() ++ to accomplish alpha premultiplication when ++ PNG_READ_COMPOSITE_NODIV_SUPPORTED is defined. ++ Changed "/255" to "/255.0" in background calculations to make it clear ++ that the 255 is used as a double. ++ ++Version 1.4.0beta106 [November 27, 2009] ++ Removed premultiplied alpha feature. ++ ++Version 1.4.0beta107 [December 4, 2009] ++ Updated README ++ Added "#define PNG_NO_PEDANTIC_WARNINGS" in the libpng source files. ++ Removed "-DPNG_CONFIGURE_LIBPNG" from the makefiles and projects. ++ Revised scripts/makefile.netbsd, makefile.openbsd, and makefile.sco ++ to put png.h and pngconf.h in $prefix/include, like the other scripts, ++ instead of in $prefix/include/libpng. Also revised makefile.sco ++ to put them in $prefix/include/libpng15 instead of in ++ $prefix/include/libpng/libpng15. ++ ++Version 1.4.0beta108 [December 11, 2009] ++ Removed leftover "-DPNG_CONFIGURE_LIBPNG" from contrib/pngminim/*/makefile ++ Relocated png_do_chop() to its original position in pngrtran.c; the ++ change in version 1.2.41beta08 caused transparency to be handled wrong ++ in some 16-bit datastreams (Yusaku Sugai). ++ ++Version 1.4.0beta109 [December 13, 2009] ++ Added "bit_depth" parameter to the private png_build_gamma_table() function. ++ Pass bit_depth=8 to png_build_gamma_table() when bit_depth is 16 but the ++ PNG_16_TO_8 transform has been set, to avoid unnecessary build of 16-bit ++ tables. ++ ++Version 1.4.0rc02 [December 20, 2009] ++ Declared png_cleanup_needed "volatile" in pngread.c and pngwrite.c ++ ++Version 1.4.0rc03 [December 22, 2009] ++ Renamed libpng-pc.in back to libpng.pc.in and revised CMakeLists.txt ++ (revising the change in 1.4.0beta99) ++ ++Version 1.4.0rc04 [December 25, 2009] ++ Swapped PNG_UNKNOWN_CHUNKS_SUPPORTED and PNG_HANDLE_AS_UNKNOWN_SUPPORTED ++ in pngset.c to be consistent with other changes in version 1.2.38. ++ ++Version 1.4.0rc05 [December 25, 2009] ++ Changed "libpng-pc.in" to "libpng.pc.in" in configure.ac, configure, and ++ Makefile.in to be consistent with changes in libpng-1.4.0rc03 ++ ++Version 1.4.0rc06 [December 29, 2009] ++ Reverted the gamma_table changes from libpng-1.4.0beta109. ++ Fixed some indentation errors. ++ ++Version 1.4.0rc07 [January 1, 2010] ++ Revised libpng*.txt and libpng.3 about 1.2.x->1.4.x differences. ++ Use png_calloc() instead of png_malloc(); png_memset() in pngrutil.c ++ Update copyright year to 2010. ++ ++Version 1.4.0rc08 [January 2, 2010] ++ Avoid deprecated references to png_ptr-io_ptr and png_ptr->error_ptr ++ in pngtest.c ++ ++Version 1.4.0 [January 3, 2010] ++ No changes. ++ ++Version 1.4.1beta01 [January 8, 2010] ++ Updated CMakeLists.txt for consistent indentation and to avoid an ++ unclosed if-statement warning (Philip Lowman). ++ Revised Makefile.am and Makefile.in to remove references to Y2KINFO, ++ KNOWNBUG, and libpng.la (Robert Schwebel). ++ Revised the makefiles to install the same files and symbolic ++ links as configure, except for libpng.la and libpng14.la. ++ Make png_set|get_compression_buffer_size() available even when ++ PNG_WRITE_SUPPORTED is not enabled. ++ Revised Makefile.am and Makefile.in to simplify their maintenance. ++ Revised scripts/makefile.linux to install a link to libpng14.so.14.1 ++ ++Version 1.4.1beta02 [January 9, 2010] ++ Revised the rest of the makefiles to install a link to libpng14.so.14.1 ++ ++Version 1.4.1beta03 [January 10, 2010] ++ Removed png_set_premultiply_alpha() from scripts/*.def ++ ++Version 1.4.1rc01 [January 16, 2010] ++ No changes. ++ ++Version 1.4.1beta04 [January 23, 2010] ++ Revised png_decompress_chunk() to improve speed and memory usage when ++ decoding large chunks. ++ Added png_set|get_chunk_malloc_max() functions. ++ ++Version 1.4.1beta05 [January 26, 2010] ++ Relocated "int k" declaration in pngtest.c to minimize its scope. ++ ++Version 1.4.1beta06 [January 28, 2010] ++ Revised png_decompress_chunk() to use a two-pass method suggested by ++ John Bowler. ++ ++Version 1.4.1beta07 [February 6, 2010] ++ Folded some long lines in the source files. ++ Added defineable PNG_USER_CHUNK_CACHE_MAX, PNG_USER_CHUNK_MALLOC_MAX, ++ and a PNG_USER_LIMITS_SUPPORTED flag. ++ Eliminated use of png_ptr->irowbytes and reused the slot in png_ptr as ++ png_ptr->png_user_chunk_malloc_max. ++ Revised png_push_save_buffer() to do fewer but larger png_malloc() calls. ++ ++Version 1.4.1beta08 [February 6, 2010] ++ Minor cleanup and updating of dates and copyright year. ++ ++Version 1.5.0beta01 [February 7, 2010] ++ Moved declaration of png_struct into private pngstruct.h and png_info ++ into pnginfo.h ++ ++Version 1.4.1beta09 and 1.5.0beta02 [February 7, 2010] ++ Reverted to original png_push_save_buffer() code. ++ ++Version 1.4.1beta10 and 1.5.0beta03 [February 8, 2010] ++ Return allocated "old_buffer" in png_push_save_buffer() before ++ calling png_error(), to avoid a potential memory leak. ++ Updated configure script to use SO number 15. ++ ++Version 1.5.0beta04 [February 9, 2010] ++ Removed malformed "incomplete struct declaration" of png_info from png.h ++ ++Version 1.5.0beta05 [February 12, 2010] ++ Removed PNG_DEPSTRUCT markup in pngstruct.h and pnginfo.h, and undid the ++ linewrapping that it entailed. ++ Revised comments in pngstruct.h and pnginfo.h and added pointers to ++ the libpng license. ++ Changed PNG_INTERNAL to PNG_EXPOSE_INTERNAL_STRUCTURES ++ Removed the cbuilder5 project, which has not been updated to 1.4.0. ++ ++Version 1.4.1beta12 and 1.5.0beta06 [February 14, 2010] ++ Fixed type declaration of png_get_chunk_malloc_max() in pngget.c (Daisuke ++ Nishikawa) ++ ++Version 1.5.0beta07 [omitted] ++ ++Version 1.5.0beta08 [February 19, 2010] ++ Changed #ifdef PNG_NO_STDIO_SUPPORTED to #ifdef PNG_NO_CONSOLE_IO_SUPPORTED ++ wherever png_snprintf() is used to construct error and warning messages. ++ Noted in scripts/makefile.mingw that it expects to be run under MSYS. ++ Removed obsolete unused MMX-querying support from contrib/gregbook ++ Added exported png_longjmp() function. ++ Removed the AIX redefinition of jmpbuf in png.h ++ Added -D_ALLSOURCE in configure.ac, makefile.aix, and CMakeLists.txt ++ when building on AIX. ++ ++Version 1.5.0beta09 [February 19, 2010] ++ Removed -D_ALLSOURCE from configure.ac, makefile.aix, and CMakeLists.txt. ++ Changed the name of png_ptr->jmpbuf to png_ptr->png_jmpbuf in pngstruct.h ++ ++Version 1.5.0beta10 [February 25, 2010] ++ Removed unused gzio.c from contrib/pngminim gather and makefile scripts ++ Removed replacement error handlers from contrib/gregbook. Because of ++ the new png_longjmp() function they are no longer needed. ++ ++Version 1.5.0beta11 [March 6, 2010] ++ Removed checking for already-included setjmp.h from pngconf.h ++ Fixed inconsistent indentations and made numerous cosmetic changes. ++ Revised the "SEE ALSO" style of libpng.3, libpngpf.3, and png.5 ++ ++Version 1.5.0beta12 [March 9, 2010] ++ Moved "#include png.h" inside pngpriv.h and removed "#include png.h" from ++ the source files, along with "#define PNG_EXPOSE_INTERNAL_STRUCTURES" ++ and "#define PNG_NO_PEDANTIC_WARNINGS" (John Bowler). ++ Created new pngdebug.h and moved debug definitions there. ++ ++Version 1.5.0beta13 [March 10, 2010] ++ Protect pngstruct.h, pnginfo.h, and pngdebug.h from being included twice. ++ Revise the "#ifdef" blocks in png_inflate() so it will compile when neither ++ PNG_USER_CHUNK_MALLOC_MAX nor PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED ++ is defined. ++ Removed unused png_measure_compressed_chunk() from pngpriv.h and libpngpf.3 ++ Moved the 'config.h' support from pngconf.h to pngpriv.h ++ Removed PNGAPI from the png_longjmp_ptr typedef. ++ Eliminated dependence of pngtest.c on the private pngdebug.h file. ++ Make all png_debug macros into *unterminated* statements or ++ expressions (i.e. a trailing ';' must always be added) and correct ++ the format statements in various png_debug messages. ++ ++Version 1.5.0beta14 [March 14, 2010] ++ Removed direct access to png_ptr->io_ptr from the Windows code in pngtest.c ++ Revised Makefile.am to account for recent additions and replacements. ++ Corrected CE and OS/2 DEF files (scripts/png*def) for symbols removed and ++ added ordinal numbers to the Windows DEF file and corrected the duplicated ++ ordinal numbers on CE symbols that are commented out. ++ Added back in export symbols that can be present in the Windows build but ++ are disabled by default. ++ PNG_EXPORT changed to include an 'ordinal' field for DEF file generation. ++ PNG_CALLBACK added to make callback definitions uniform. PNGAPI split ++ into PNGCAPI (base C form), PNGAPI (exports) and PNGCBAPI (callbacks), ++ and appropriate changes made to all files. Cygwin builds re-hinged to ++ allow procedure call standard changes and to remove the need for the DEF ++ file (fixes build on Cygwin). ++ Enabled 'attribute' warnings that are relevant to library APIs and callbacks. ++ Changed rules for generation of the various symbol files and added a new ++ rule for a DEF file (which is also added to the distribution). ++ Updated the symbol file generation to stop it adding spurious spaces ++ to EOL (coming from preprocessor macro expansion). Added a facility ++ to join tokens in the output and rewrite *.dfn to use this. ++ Eliminated scripts/*.def in favor of libpng.def; updated projects/visualc71 ++ and removed scripts/makefile.cygwin. ++ Made PNG_BUILD_DLL safe: it can be set whenever a DLL is being built. ++ Removed the include of sys/types.h - apparently unnecessary now on the ++ platforms on which it happened (all but Mac OS and RISC OS). ++ Moved the Mac OS test into pngpriv.h (the only place it is used.) ++ ++Version 1.5.0beta15 [March 17, 2010] ++ Added symbols.chk target to Makefile.am to validate the symbols in png.h ++ against the new DEF file scripts/symbols.def. ++ Changed the default DEF file back to pngwin.def. ++ Removed makefile.mingw. ++ Eliminated PNG_NO_EXTERN and PNG_ALL_EXTERN ++ ++Version 1.5.0beta16 [April 1, 2010] ++ Make png_text_struct independent of PNG_iTXt_SUPPORTED, so that ++ fields are initialized in all configurations. The READ/WRITE ++ macros (PNG_(READ|WRITE)_iTXt_SUPPORTED) still function as ++ before to disable code to actually read or write iTXt chunks ++ and iTXt_SUPPORTED can be used to detect presence of either ++ read or write support (but it is probably better to check for ++ the one actually required - read or write.) ++ Combined multiple png_warning() calls for a single error. ++ Restored the macro definition of png_check_sig(). ++ ++Version 1.5.0beta17 [April 17, 2010] ++ Added some "(long)" typecasts to printf calls in png_handle_cHRM(). ++ Documented the fact that png_set_dither() was disabled since libpng-1.4.0. ++ Reenabled png_set_dither() but renamed it to png_set_quantize() to reflect ++ more accurately what it actually does. At the same time, renamed ++ the PNG_DITHER_[RED,GREEN_BLUE]_BITS macros to ++ PNG_QUANTIZE_[RED,GREEN,BLUE]_BITS. ++ Added some "(long)" typecasts to printf calls in png_handle_cHRM(). ++ Freeze build-time only configuration in the build. ++ In all prior versions of libpng most configuration options ++ controlled by compiler #defines had to be repeated by the ++ application code that used libpng. This patch changes this ++ so that compilation options that can only be changed at build ++ time are frozen in the build. Options that are compiler ++ dependent (and those that are system dependent) are evaluated ++ each time - pngconf.h holds these. Options that can be changed ++ per-file in the application are in png.h. Frozen options are ++ in the new installed header file pnglibconf.h (John Bowler) ++ Removed the xcode project because it has not been updated to work ++ with libpng-1.5.0. ++ Removed the ability to include optional pngusr.h ++ ++Version 1.5.0beta18 [April 17, 2010] ++ Restored the ability to include optional pngusr.h ++ Moved replacements for png_error() and png_warning() from the ++ contrib/pngminim project to pngerror.c, for use when warnings or ++ errors are disabled via PNG_NO_WARN or PNG_NO_ERROR_TEXT, to avoid ++ storing unneeded error/warning text. ++ Updated contrib/pngminim project to work with the new pnglibconf.h ++ Added some PNG_NO_* defines to contrib/pngminim/*/pngusr.h to save space. ++ ++Version 1.5.0beta19 [April 24, 2010] ++ Added PNG_{READ,WRITE}_INT_FUNCTIONS_SUPPORTED. This allows the functions ++ to read and write ints to be disabled independently of PNG_USE_READ_MACROS, ++ which allows libpng to be built with the functions even though the default ++ is to use the macros - this allows applications to choose at app build ++ time whether or not to use macros (previously impossible because the ++ functions weren't in the default build.) ++ Changed Windows calling convention back to __cdecl for API functions. ++ For Windows/x86 platforms only: ++ __stdcall is no longer needed for Visual Basic, so libpng-1.5.0 uses ++ __cdecl throughout (both API functions and callbacks) on Windows/x86 ++ platforms. ++ Replaced visualc6 and visualc71 projects with new vstudio project ++ Relaxed the overly-restrictive permissions of some files. ++ ++Version 1.5.0beta20 [April 24, 2010] ++ Relaxed more overly-restrictive permissions of some files. ++ ++Version 1.5.0beta21 [April 27, 2010] ++ Removed some unwanted binary bytes and changed CRLF to NEWLINE in the new ++ vstudio project files, and some trivial editing of some files in the ++ scripts directory. ++ Set PNG_NO_READ_BGR, PNG_NO_IO_STATE, and PNG_NO_TIME_RFC1123 in ++ contrib/pngminim/decoder/pngusr.h to make a smaller decoder application. ++ ++Version 1.5.0beta22 [April 28, 2010] ++ Fixed dependencies of GET_INT_32 - it does not require READ_INT_FUNCTIONS ++ because it has a macro equivalent. ++ Improved the options.awk script; added an "everything off" option. ++ Revised contrib/pngminim to use the "everything off" option in pngusr.dfa. ++ ++Version 1.5.0beta23 [April 29, 2010] ++ Corrected PNG_REMOVED macro to take five arguments. ++ The macro was documented with two arguments (name,ordinal), however ++ the symbol checking .dfn files assumed five arguments. The five ++ argument form seems more useful so it is changed to that. ++ Corrected PNG_UNKNOWN_CHUNKS_SUPPORTED to PNG_HANDLE_AS_UNKNOWN_SUPPORTED ++ in gregbook/readpng2.c ++ Corrected protection of png_get_user_transform_ptr. The API declaration in ++ png.h is removed if both READ and WRITE USER_TRANSFORM are turned off ++ but was left defined in pngtrans.c ++ Added logunsupported=1 to cause pnglibconf.h to document disabled options. ++ This makes the installed pnglibconf.h more readable but causes no ++ other change. The intention is that users of libpng will find it ++ easier to understand if an API they need is missing. ++ Include png_reset_zstream() in png.c only when PNG_READ_SUPPORTED is defined. ++ Removed dummy_inflate.c from contrib/pngminim/encoder ++ Removed contrib/pngminim/*/gather.sh; gathering is now done in the makefile. ++ ++Version 1.5.0beta24 [May 7, 2010] ++ Use bitwise "&" instead of arithmetic mod in pngrutil.c calculation of the ++ offset of the png_ptr->rowbuf pointer into png_ptr->big_row_buf. ++ Added more blank lines for readability. ++ ++Version 1.5.0beta25 [June 18, 2010] ++ In pngpread.c: png_push_have_row() add check for new_row > height ++ Removed the now-redundant check for out-of-bounds new_row from example.c ++ ++Version 1.5.0beta26 [June 18, 2010] ++ In pngpread.c: png_push_process_row() add check for too many rows. ++ ++Version 1.5.0beta27 [June 18, 2010] ++ Removed the check added in beta25 as it is now redundant. ++ ++Version 1.5.0beta28 [June 20, 2010] ++ Rewrote png_process_IDAT_data to consistently treat extra data as warnings ++ and handle end conditions more cleanly. ++ Removed the new (beta26) check in png_push_process_row(). ++ ++Version 1.5.0beta29 [June 21, 2010] ++ Revised scripts/options.awk to work on Sunos (but still doesn't work) ++ Added comment to options.awk and contrib/pngminim/*/makefile to try nawk. ++ ++Version 1.5.0beta30 [June 22, 2010] ++ Stop memory leak when reading a malformed sCAL chunk. ++ ++Version 1.5.0beta31 [June 26, 2010] ++ Revised pngpread.c patch of beta28 to avoid an endless loop. ++ Removed some trailing blanks. ++ ++Version 1.5.0beta32 [June 26, 2010] ++ Removed leftover scripts/options.patch and scripts/options.rej ++ ++Version 1.5.0beta33 [July 6, 3010] ++ Made FIXED and FLOATING options consistent in the APIs they enable and ++ disable. Corrected scripts/options.awk to handle both command line ++ options and options specified in the .dfa files. ++ Changed char *msg to PNG_CONST char *msg in pngrutil.c ++ Make png_set_sRGB_gAMA_and_cHRM set values using either the fixed or ++ floating point APIs, but not both. ++ Reversed patch to remove error handler when the jmp_buf is stored in the ++ main program structure, not the png_struct. ++ The error handler is needed because the default handler in libpng will ++ always use the jmp_buf in the library control structure; this is never ++ set. The gregbook code is a useful example because, even though it ++ uses setjmp/longjmp, it shows how error handling can be implemented ++ using control mechanisms not directly supported by libpng. The ++ technique will work correctly with mechanisms such as Microsoft ++ Structure Exceptions or C++ exceptions (compiler willing - note that gcc ++ does not by default support interworking of C and C++ error handling.) ++ Reverted changes to call png_longjmp in contrib/gregbook where it is not ++ appropriate. If mainprog->jmpbuf is used by setjmp, then png_longjmp ++ cannot be used. ++ Changed "extern PNG_EXPORT" to "PNG_EXPORT" in png.h (Jan Nijtmans) ++ Changed "extern" to "PNG_EXTERN" in pngpriv.h (except for the 'extern "C" {') ++ ++Version 1.5.0beta34 [July 12, 2010] ++ Put #ifndef PNG_EXTERN, #endif around the define PNG_EXTERN in pngpriv.h ++ ++Version 1.5.0beta35 [July 24, 2010] ++ Removed some newly-added TAB characters. ++ Added -DNO_PNG_SNPRINTF to CFLAGS in scripts/makefile.dj2 ++ Moved the definition of png_snprintf() outside of the enclosing ++ #ifdef blocks in pngconf.h ++ ++Version 1.5.0beta36 [July 29, 2010] ++ Patches by John Bowler: ++ Fixed point APIs are now supported throughout (no missing APIs). ++ Internal fixed point arithmetic support exists for all internal floating ++ point operations. ++ sCAL validates the floating point strings it is passed. ++ Safe, albeit rudimentary, Watcom support is provided by PNG_API_RULE==2 ++ Two new APIs exist to get the number of passes without turning on the ++ PNG_INTERLACE transform and to get the number of rows in the current ++ pass. ++ A new test program, pngvalid.c, validates the gamma code. ++ Errors in the 16-bit gamma correction (overflows) have been corrected. ++ cHRM chunk testing is done consistently (previously the floating point ++ API bypassed it, because the test really didn't work on FP, now the test ++ is performed on the actual values to be stored in the PNG file so it ++ works in the FP case too.) ++ Most floating point APIs now simply call the fixed point APIs after ++ converting the values to the fixed point form used in the PNG file. ++ The standard headers no longer include zlib.h, which is currently only ++ required for pngstruct.h and can therefore be internal. ++ Revised png_get_int_32 to undo the PNG two's complement representation of ++ negative numbers. ++ ++Version 1.5.0beta37 [July 30, 2010] ++ Added a typecast in png_get_int_32() in png.h and pngrutil.h to avoid ++ a compiler warning. ++ Replaced oFFs 0,0 with oFFs -10,20 in pngtest.png ++ ++Version 1.5.0beta38 [July 31, 2010] ++ Implemented remaining "_fixed" functions. ++ Corrected a number of recently introduced warnings mostly resulting from ++ safe but uncast assignments to shorter integers. Also added a zlib ++ VStudio release library project because the latest zlib Official Windows ++ build does not include such a thing. ++ Revised png_get_int_16() to be similar to png_get_int_32(). ++ Restored projects/visualc71. ++ ++Version 1.5.0beta39 [August 2, 2010] ++ VisualC/GCC warning fixes, VisualC build fixes ++ The changes include support for function attributes in VC in addition to ++ those already present in GCC - necessary because without these some ++ warnings are unavoidable. Fixes include signed/unsigned fixes in ++ pngvalid and checks with gcc -Wall -Wextra -Wunused. ++ VC requires function attributes on function definitions as well as ++ declarations, PNG_FUNCTION has been added to enable this and the ++ relevant function definitions changed. ++ ++Version 1.5.0beta40 [August 6, 2010] ++ Correct use of _WINDOWS_ in pngconf.h ++ Removed png_mem_ #defines; they are no longer used. ++ Added the sRGB chunk to pngtest.png ++ ++Version 1.5.0beta41 [August 11, 2010] ++ Added the cHRM chunk to pngtest.png ++ Don't try to use version-script with cygwin/mingw. ++ Revised contrib/gregbook to work under cygwin/mingw. ++ ++Version 1.5.0beta42 [August 18, 2010] ++ Add .dll.a to the list of extensions to be symlinked by Makefile.am (Yaakov) ++ Made all API functions that have const arguments and constant string ++ literal pointers declare them (John Bowler). ++ ++Version 1.5.0beta43 [August 20, 2010] ++ Removed spurious tabs, shorten long lines (no source change) ++ Also added scripts/chkfmt to validate the format of all the files that can ++ reasonably be validated (it is suggested to run "make distclean" before ++ checking, because some machine generated files have long lines.) ++ Reformatted the CHANGES file to be more consistent throughout. ++ Made changes to address various issues identified by GCC, mostly ++ signed/unsigned and shortening problems on assignment but also a few ++ difficult to optimize (for GCC) loops. ++ Fixed non-GCC fixed point builds. In png.c a declaration was misplaced ++ in an earlier update. Fixed to declare the auto variables at the head. ++ Use cexcept.h in pngvalid.c. ++ ++Version 1.5.0beta44 [August 24, 2010] ++ Updated CMakeLists.txt to use CMAKE_INSTALL_LIBDIR variable; useful for ++ installing libpng in /usr/lib64 (Funda Wang). ++ Revised CMakeLists.txt to put the man pages in share/man/man* not man/man* ++ Revised CMakeLists.txt to make symlinks instead of copies when installing. ++ Changed PNG_LIB_NAME from pngNN to libpngNN in CMakeLists.txt (Philip Lowman) ++ Implemented memory checks within pngvalid ++ Reformatted/rearranged pngvalid.c to assist use of progressive reader. ++ Check interlaced images in pngvalid ++ Clarified pngusr.h comments in pnglibconf.dfa ++ Simplified the pngvalid error-handling code now that cexcept.h is in place. ++ Implemented progressive reader in pngvalid.c for standard tests ++ Implemented progressive read in pngvalid.c gamma tests ++ Turn on progressive reader in pngvalid.c by default and tidy code. ++ ++Version 1.5.0beta45 [August 26, 2010] ++ Added an explicit make step to projects/vstudio for pnglibconf.h ++ Also corrected zlib.vcxproj into which Visual Studio had introduced ++ what it calls an "authoring error". The change to make pnglibconf.h ++ simply copies the file; in the future it may actually generate the ++ file from scripts/pnglibconf.dfa as the other build systems do. ++ Changed pngvalid to work when floating point APIs are disabled ++ Renamed the prebuilt scripts/pnglibconf.h to scripts/pnglibconf.h.prebuilt ++ Supply default values for PNG_USER_PRIVATEBUILD and PNG_USER_DLLFNAME_POSTFIX ++ in pngpriv.h in case the user neglected to define them in their pngusr.h ++ ++Version 1.5.0beta46 [August 28, 2010] ++ Added new private header files to libpng_sources in CMakeLists.txt ++ Added PNG_READ_16BIT, PNG_WRITE_16BIT, and PNG_16BIT options. ++ Added reference to scripts/pnglibconf.h.prebuilt in the visualc71 project. ++ ++Version 1.5.0beta47 [September 11, 2010] ++ Fixed a number of problems with 64-bit compilation reported by Visual ++ Studio 2010 (John Bowler). ++ ++Version 1.5.0beta48 [October 4, 2010] ++ Updated CMakeLists.txt (Philip Lowman). ++ Revised autogen.sh to recognize and use $AUTOCONF, $AUTOMAKE, $AUTOHEADER, ++ $AUTOPOINT, $ACLOCAL and $LIBTOOLIZE ++ Fixed problem with symbols creation in Makefile.am which was assuming that ++ all versions of ccp write to standard output by default (Martin Banky). The ++ bug was introduced in libpng-1.2.9beta5. ++ Removed unused mkinstalldirs. ++ ++Version 1.5.0beta49 [October 8, 2010] ++ Undid Makefile.am revision of 1.5.0beta48. ++ ++Version 1.5.0beta50 [October 14, 2010] ++ Revised Makefile.in to account for mkinstalldirs being removed. ++ Added some "(unsigned long)" typecasts in printf statements in pngvalid.c. ++ Suppressed a compiler warning in png_handle_sPLT(). ++ Check for out-of-range text compression mode in png_set_text(). ++ ++Version 1.5.0beta51 [October 15, 2010] ++ Changed embedded dates to "(PENDING RELEASE) in beta releases (and future ++ rc releases) to minimize the difference between releases. ++ ++Version 1.5.0beta52 [October 16, 2010] ++ Restored some of the embedded dates (in png.h, png.c, documentation, etc.) ++ ++Version 1.5.0beta53 [October 18, 2010] ++ Updated INSTALL to mention using "make maintainer-clean" and to remove ++ obsolete statement about a custom ltmain.sh ++ Disabled "color-tests" by default in Makefile.am so it will work with ++ automake versions earlier than 1.11.1 ++ Use document name "libpng-manual.txt" instead of "libpng-.txt" ++ to simplify version differences. ++ Removed obsolete remarks about setjmp handling from INSTALL. ++ Revised and renamed the typedef in png.h and png.c that was designed ++ to catch library and header mismatch. ++ ++Version 1.5.0beta54 [November 10, 2010] ++ Require 48 bytes, not 64 bytes, for big_row_buf in overflow checks. ++ Used a consistent structure for the pngget.c functions. ++ ++Version 1.5.0beta55 [November 21, 2010] ++ Revised png_get_uint_32, png_get_int_32, png_get_uint_16 (Cosmin) ++ Moved reading of file signature into png_read_sig (Cosmin) ++ Fixed atomicity of chunk header serialization (Cosmin) ++ Added test for io_state in pngtest.c (Cosmin) ++ Added "#!/bin/sh" at the top of contrib/pngminim/*/gather.sh scripts. ++ Changes to remove gcc warnings (John Bowler) ++ Certain optional gcc warning flags resulted in warnings in libpng code. ++ With these changes only -Wconversion and -Wcast-qual cannot be turned on. ++ Changes are trivial rearrangements of code. -Wconversion is not possible ++ for pngrutil.c (because of the widespread use of += et al on variables ++ smaller than (int) or (unsigned int)) and -Wcast-qual is not possible ++ with pngwio.c and pngwutil.c because the 'write' callback and zlib ++ compression both fail to declare their input buffers with 'const'. ++ ++Version 1.5.0beta56 [December 7, 2010] ++ Added the private PNG_UNUSED() macro definition in pngpriv.h. ++ Added some commentary about PNG_EXPORT in png.h and pngconf.h ++ Revised PNG_EXPORT() macro and added PNG_EXPORTA() macro, with the ++ objective of simplifying and improving the cosmetic appearance of png.h. ++ Fixed some incorrect "=" macro names in pnglibconf.dfa ++ Included documentation of changes in 1.5.0 from 1.4.x in libpng-manual.txt ++ ++Version 1.5.0beta57 [December 9, 2010] ++ Documented the pngvalid gamma error summary with additional comments and ++ print statements. ++ Improved missing symbol handling in checksym.awk; symbols missing in both ++ the old and new files can now be optionally ignored, treated as errors ++ or warnings. ++ Removed references to pngvcrd.c and pnggccrd.c from the vstudio project. ++ Updated "libpng14" to "libpng15" in the visualc71 project. ++ Enabled the strip16 tests in pngvalid.` ++ Don't display test results (except PASS/FAIL) when running "make test". ++ Instead put them in pngtest-log.txt ++ Added "--with-zprefix=" to configure.ac ++ Updated the prebuilt configuration files to autoconf version 2.68 ++ ++Version 1.5.0beta58 [December 19, 2010] ++ Fixed interlace image handling and add test cases (John Bowler) ++ Fixed the clean rule in Makefile.am to remove pngtest-log.txt ++ Made minor changes to work around warnings in gcc 3.4 ++ ++Version 1.5.0rc01 [December 27, 2010] ++ No changes. ++ ++Version 1.5.0rc02 [December 27, 2010] ++ Eliminated references to the scripts/*.def files in project/visualc71. ++ ++Version 1.5.0rc03 [December 28, 2010] ++ Eliminated scripts/*.def and revised Makefile.am accordingly ++ ++Version 1.5.0rc04 [December 29, 2010] ++ Fixed bug in background transformation handling in pngrtran.c (it was ++ looking for the flag in png_ptr->transformations instead of in ++ png_ptr->flags) (David Raymond). ++ ++Version 1.5.0rc05 [December 31, 2010] ++ Fixed typo in a comment in CMakeLists.txt (libpng14 => libpng15) (Cosmin) ++ ++Version 1.5.0rc06 [January 4, 2011] ++ Changed the new configure option "zprefix=string" to "zlib-prefix=string" ++ ++Version 1.5.0rc07 [January 4, 2011] ++ Updated copyright year. ++ ++Version 1.5.0 [January 6, 2011] ++ No changes. ++ ++version 1.5.1beta01 [January 8, 2011] ++ Added description of png_set_crc_action() to the manual. ++ Added a note in the manual that the type of the iCCP profile was changed ++ from png_charpp to png_bytepp in png_get_iCCP(). This change happened ++ in version 1.5.0beta36 but is not noted in the CHANGES. Similarly, ++ it was changed from png_charpp to png_const_bytepp in png_set_iCCP(). ++ Ensure that png_rgb_to_gray ignores palette mapped images, if libpng ++ internally happens to call it with one, and fixed a failure to handle ++ palette mapped images correctly. This fixes CVE-2690. ++ ++Version 1.5.1beta02 [January 14, 2011] ++ Fixed a bug in handling of interlaced images (bero at arklinux.org). ++ Updated CMakeLists.txt (Clifford Yapp) ++ ++Version 1.5.1beta03 [January 14, 2011] ++ Fixed typecasting of some png_debug() statements (Cosmin) ++ ++Version 1.5.1beta04 [January 16, 2011] ++ Updated documentation of png_set|get_tRNS() (Thomas Klausner). ++ Mentioned in the documentation that applications must #include "zlib.h" ++ if they need access to anything in zlib.h, and that a number of ++ macros such as png_memset() are no longer accessible by applications. ++ Corrected pngvalid gamma test "sample" function to access all of the color ++ samples of each pixel, instead of sampling the red channel three times. ++ Prefixed variable names index, div, exp, gamma with "png_" to avoid "shadow" ++ warnings, and (mistakenly) changed png_exp() to exp(). ++ ++Version 1.5.1beta05 [January 16, 2011] ++ Changed variable names png_index, png_div, png_exp, and png_gamma to ++ char_index, divisor, exp_b10, and gamma_val, respectively, and ++ changed exp() back to png_exp(). ++ ++Version 1.5.1beta06 [January 20, 2011] ++ Prevent png_push_crc_skip() from hanging while reading an unknown chunk ++ or an over-large compressed zTXt chunk with the progressive reader. ++ Eliminated more GCC "shadow" warnings. ++ Revised png_fixed() in png.c to avoid compiler warning about reaching the ++ end without returning anything. ++ ++Version 1.5.1beta07 [January 22, 2011] ++ In the manual, describe the png_get_IHDR() arguments in the correct order. ++ Added const_png_structp and const_png_infop types, and used them in ++ prototypes for most png_get_*() functions. ++ ++Version 1.5.1beta08 [January 23, 2011] ++ Added png_get_io_chunk_type() and deprecated png_get_io_chunk_name() ++ Added synopses for the IO_STATE functions and other missing synopses ++ to the manual. Removed the synopses from libpngpf.3 because they ++ were out of date and no longer useful. Better information can be ++ obtained by reading the prototypes and comments in pngpriv.h ++ Attempted to fix cpp on Solaris with S. Studio 12 cc, fix build ++ Added a make macro DFNCPP that is a CPP that will accept the tokens in ++ a .dfn file and adds configure stuff to test for such a CPP. ./configure ++ should fail if one is not available. ++ Corrected const_png_ in png.h to png_const_ to avoid polluting the namespace. ++ Added png_get_current_row_number and png_get_current_pass_number for the ++ benefit of the user transform callback. ++ Added png_process_data_pause and png_process_data_skip for the benefit of ++ progressive readers that need to stop data processing or want to optimize ++ skipping of unread data (e.g., if the reader marks a chunk to be skipped.) ++ ++Version 1.5.1beta09 [January 24, 2011] ++ Enhanced pngvalid, corrected an error in gray_to_rgb, corrected doc error. ++ pngvalid contains tests of transforms, which tests are currently disabled ++ because they are incompletely tested. gray_to_rgb was failing to expand ++ the bit depth for smaller bit depth images; this seems to be a long ++ standing error and resulted, apparently, in invalid output ++ (CVE-2011-0408, CERT VU#643140). The documentation did not accurately ++ describe what libpng really does when converting RGB to gray. ++ ++Version 1.5.1beta10 [January 27, 2010] ++ Fixed incorrect examples of callback prototypes in the manual, that were ++ introduced in libpng-1.0.0. ++ In addition the order of the png_get_uint macros with respect to the ++ relevant function definitions has been reversed. This helps the ++ preprocessing of the symbol files be more robust. Furthermore, the ++ symbol file preprocessing now uses -DPNG_NO_USE_READ_MACROS even when ++ the library may actually be built with PNG_USE_READ_MACROS; this stops ++ the read macros interfering with the symbol file format. ++ Made the manual, synopses, and function prototypes use the function ++ argument names file_gamma, int_file_gamma, and srgb_intent consistently. ++ ++Version 1.5.1beta11 [January 28, 2011] ++ Changed PNG_UNUSED from "param=param;" to "{if(param){}}". ++ Corrected local variable type in new API png_process_data_skip() ++ The type was self-evidently incorrect but only causes problems on 64-bit ++ architectures. ++ Added transform tests to pngvalid and simplified the arguments. ++ ++Version 1.5.1rc01 [January 29, 2011] ++ No changes. ++ ++Version 1.5.1rc02 [January 31, 2011] ++ Added a request in the manual that applications do not use "png_" or ++ "PNG_" to begin any of their own symbols. ++ Changed PNG_UNUSED to "(void)param;" and updated the commentary in pngpriv.h ++ ++Version 1.5.1 [February 3, 2011] ++ No changes. ++ ++Version 1.5.2beta01 [February 13, 2011] ++ More -Wshadow fixes for older gcc compilers. Older gcc versions apparently ++ check formal parameters names in function declarations (as well as ++ definitions) to see if they match a name in the global namespace. ++ Revised PNG_EXPORTA macro to not use an empty parameter, to accommodate the ++ old VisualC++ preprocessor. ++ Turned on interlace handling in png_read_png(). ++ Fixed gcc pedantic warnings. ++ Handle longjmp in Cygwin. ++ Fixed png_get_current_row_number() in the interlaced case. ++ Cleaned up ALPHA flags and transformations. ++ Implemented expansion to 16 bits. ++ ++Version 1.5.2beta02 [February 19, 2011] ++ Fixed mistake in the descriptions of user read_transform and write_transform ++ function prototypes in the manual. The row_info struct is png_row_infop. ++ Reverted png_get_current_row_number() to previous (1.5.2beta01) behavior. ++ Corrected png_get_current_row_number documentation ++ Fixed the read/write row callback documentation. ++ This documents the current behavior, where the callback is called after ++ every row with information pertaining to the next row. ++ ++Version 1.5.2beta03 [March 3, 2011] ++ Fixed scripts/makefile.vcwin32 ++ Updated contrib/pngsuite/README to add the word "modify". ++ Define PNG_ALLOCATED to blank when _MSC_VER<1300. ++ ++Version 1.5.2rc01 [March 19, 2011] ++ Define remaining attributes to blank when MSC_VER<1300. ++ ifdef out mask arrays in pngread.c when interlacing is not supported. ++ ++Version 1.5.2rc02 [March 22, 2011] ++ Added a hint to try CPP=/bin/cpp if "cpp -E" fails in scripts/pnglibconf.mak ++ and in contrib/pngminim/*/makefile, eg., on SunOS 5.10, and removed "strip" ++ from the makefiles. ++ Fixed a bug (present since libpng-1.0.7) that makes png_handle_sPLT() fail ++ to compile when PNG_NO_POINTER_INDEXING is defined (Chubanov Kirill) ++ ++Version 1.5.2rc03 [March 24, 2011] ++ Don't include standard header files in png.h while building the symbol table, ++ to avoid cpp failure on SunOS (introduced PNG_BUILDING_SYMBOL_TABLE macro). ++ ++Version 1.5.2 [March 31, 2011] ++ No changes. ++ ++Version 1.5.3beta01 [April 1, 2011] ++ Re-initialize the zlib compressor before compressing non-IDAT chunks. ++ Added API functions (png_set_text_compression_level() and four others) to ++ set parameters for zlib compression of non-IDAT chunks. ++ ++Version 1.5.3beta02 [April 3, 2011] ++ Updated scripts/symbols.def with new API functions. ++ Only compile the new zlib re-initializing code when text or iCCP is ++ supported, using PNG_WRITE_COMPRESSED_TEXT_SUPPORTED macro. ++ Improved the optimization of the zlib CMF byte (see libpng-1.2.6beta03). ++ Optimize the zlib CMF byte in non-IDAT compressed chunks ++ ++Version 1.5.3beta03 [April 16, 2011] ++ Fixed gcc -ansi -pedantic compile. A strict ANSI system does not have ++ snprintf, and the "__STRICT_ANSI__" detects that condition more reliably ++ than __STDC__ (John Bowler). ++ Removed the PNG_PTR_NORETURN attribute because it too dangerous. It tells ++ the compiler that a user supplied callback (the error handler) does not ++ return, yet there is no guarantee in practice that the application code ++ will correctly implement the error handler because the compiler only ++ issues a warning if there is a mistake (John Bowler). ++ Removed the no-longer-used PNG_DEPSTRUCT macro. ++ Updated the zlib version to 1.2.5 in the VStudio project. ++ Fixed 64-bit builds where png_uint_32 is smaller than png_size_t in ++ pngwutil.c (John Bowler). ++ Fixed bug with stripping the filler or alpha channel when writing, that ++ was introduced in libpng-1.5.2beta01 (bug report by Andrew Church). ++ ++Version 1.5.3beta04 [April 27, 2011] ++ Updated pngtest.png with the new zlib CMF optimization. ++ Cleaned up conditional compilation code and of background/gamma handling ++ Internal changes only except a new option to avoid compiling the ++ png_build_grayscale_palette API (which is not used at all internally.) ++ The main change is to move the transform tests (READ_TRANSFORMS, ++ WRITE_TRANSFORMS) up one level to the caller of the APIs. This avoids ++ calls to spurious functions if all transforms are disabled and slightly ++ simplifies those functions. Pngvalid modified to handle this. ++ A minor change is to stop the strip_16 and expand_16 interfaces from ++ disabling each other; this allows the future alpha premultiplication ++ code to use 16-bit intermediate values while still producing 8-bit output. ++ png_do_background and png_do_gamma have been simplified to take a single ++ pointer to the png_struct rather than pointers to every item required ++ from the png_struct. This makes no practical difference to the internal ++ code. ++ A serious bug in the pngvalid internal routine 'standard_display_init' has ++ been fixed - this failed to initialize the red channel and accidentally ++ initialized the alpha channel twice. ++ Changed png_struct jmp_buf member name from png_jmpbuf to tmp_jmpbuf to ++ avoid a possible clash with the png_jmpbuf macro on some platforms. ++ ++Version 1.5.3beta05 [May 6, 2011] ++ Added the "_POSIX_SOURCE" feature test macro to ensure libpng sees the ++ correct API. _POSIX_SOURCE is defined in pngpriv.h, pngtest.c and ++ pngvalid.c to ensure that POSIX conformant systems disable non-POSIX APIs. ++ Removed png_snprintf and added formatted warning messages. This change adds ++ internal APIs to allow png_warning messages to have parameters without ++ requiring the host OS to implement snprintf. As a side effect the ++ dependency of the tIME-supporting RFC1132 code on stdio is removed and ++ PNG_NO_WARNINGS does actually work now. ++ Pass "" instead of '\0' to png_default_error() in png_err(). This mistake ++ was introduced in libpng-1.2.20beta01. This fixes CVE-2011-2691. ++ Added PNG_WRITE_OPTIMIZE_CMF_SUPPORTED macro to make the zlib "CMF" byte ++ optimization configurable. ++ IDAT compression failed if preceded by a compressed text chunk (bug ++ introduced in libpng-1.5.3beta01-02). This was because the attempt to ++ reset the zlib stream in png_write_IDAT happened after the first IDAT ++ chunk had been deflated - much too late. In this change internal ++ functions were added to claim/release the z_stream and, hopefully, make ++ the code more robust. Also deflateEnd checking is added - previously ++ libpng would ignore an error at the end of the stream. ++ ++Version 1.5.3beta06 [May 8, 2011] ++ Removed the -D_ALL_SOURCE from definitions for AIX in CMakeLists.txt ++ Implemented premultiplied alpha support: png_set_alpha_mode API ++ ++Version 1.5.3beta07 [May 11, 2011] ++ Added expand_16 support to the high level interface. ++ Added named value and 'flag' gamma support to png_set_gamma. Made a minor ++ change from the previous (unreleased) ABI/API to hide the exact value used ++ for Macs - it's not a good idea to embed this in the ABI! ++ Moved macro definitions for PNG_HAVE_IHDR, PNG_HAVE_PLTE, and PNG_AFTER_IDAT ++ from pngpriv.h to png.h because they must be visible to applications ++ that call png_set_unknown_chunks(). ++ Check for up->location !PNG_AFTER_IDAT when writing unknown chunks ++ before IDAT. ++ ++Version 1.5.3beta08 [May 16, 2011] ++ Improved "pngvalid --speed" to exclude more of pngvalid from the time. ++ Documented png_set_alpha_mode(), other changes in libpng.3/libpng-manual.txt ++ The cHRM chunk now sets the defaults for png_set_rgb_to_gray() (when negative ++ parameters are supplied by the caller), while in the absence of cHRM ++ sRGB/Rec 709 values are still used. This introduced a divide-by-zero ++ bug in png_handle_cHRM(). ++ The bKGD chunk no longer overwrites the background value set by ++ png_set_background(), allowing the latter to be used before the file ++ header is read. It never performed any useful function to override ++ the default anyway. ++ Added memory overwrite and palette image checks to pngvalid.c ++ Previously palette image code was poorly checked. Since the transformation ++ code has a special palette path in most cases this was a severe weakness. ++ Minor cleanup and some extra checking in pngrutil.c and pngrtran.c. When ++ expanding an indexed image, always expand to RGBA if transparency is ++ present. ++ ++Version 1.5.3beta09 [May 17, 2011] ++ Reversed earlier 1.5.3 change of transformation order; move png_expand_16 ++ back where it was. The change doesn't work because it requires 16-bit ++ gamma tables when the code only generates 8-bit ones. This fails ++ silently; the libpng code just doesn't do any gamma correction. Moving ++ the tests back leaves the old, inaccurate, 8-bit gamma calculations, but ++ these are clearly better than none! ++ ++Version 1.5.3beta10 [May 20, 2011] ++ ++ png_set_background() and png_expand_16() did not work together correctly. ++ This problem is present in 1.5.2; if png_set_background is called with ++ need_expand false and the matching 16 bit color libpng erroneously just ++ treats it as an 8-bit color because of where png_do_expand_16 is in the ++ transform list. This simple fix reduces the supplied colour to 8-bits, ++ so it gets smashed, but this is better than the current behavior. ++ Added tests for expand16, more fixes for palette image tests to pngvalid. ++ Corrects the code for palette image tests and disables attempts to ++ validate palette colors. ++ ++Version 1.5.3rc01 [June 3, 2011] ++ No changes. ++ ++Version 1.5.3rc02 [June 8, 2011] ++ Fixed uninitialized memory read in png_format_buffer() (Bug report by ++ Frank Busse, CVE-2011-2501, related to CVE-2004-0421). ++ ++Version 1.5.3beta11 [June 11, 2011] ++ Fixed png_handle_sCAL which is broken in 1.5. This fixes CVE 2011-2692. ++ Added sCAL to pngtest.png ++ Revised documentation about png_set_user_limits() to say that it also affects ++ png writing. ++ Revised handling of png_set_user_limits() so that it can increase the ++ limit beyond the PNG_USER_WIDTH|HEIGHT_MAX; previously it could only ++ reduce it. ++ Make the 16-to-8 scaling accurate. Dividing by 256 with no rounding is ++ wrong (high by one) 25% of the time. Dividing by 257 with rounding is ++ wrong in 128 out of 65536 cases. Getting the right answer all the time ++ without division is easy. ++ Added "_SUPPORTED" to the PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION macro. ++ Added projects/owatcom, an IDE project for OpenWatcom to replace ++ scripts/makefile.watcom. This project works with OpenWatcom 1.9. The ++ IDE autogenerates appropriate makefiles (libpng.mk) for batch processing. ++ The project is configurable, unlike the Visual Studio project, so long ++ as the developer has an awk. ++ Changed png_set_gAMA to limit the gamma value range so that the inverse ++ of the stored value cannot overflow the fixed point representation, ++ and changed other things OpenWatcom warns about. ++ Revised pngvalid.c to test PNG_ALPHA_MODE_SUPPORTED correctly. This allows ++ pngvalid to build when ALPHA_MODE is not supported, which is required if ++ it is to build on libpng 1.4. ++ Removed string/memory macros that are no longer used and are not ++ necessarily fully supportable, particularly png_strncpy and png_snprintf. ++ Added log option to pngvalid.c and attempted to improve gamma messages. ++ ++Version 1.5.3 [omitted] ++ People found the presence of a beta release following an rc release ++ to be confusing; therefore we bump the version to libpng-1.5.4beta01 ++ and there will be no libpng-1.5.3 release. ++ ++Version 1.5.4beta01 [June 14, 2011] ++ Made it possible to undefine PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED ++ to get the same (inaccurate) output as libpng-1.5.2 and earlier. ++ Moved definitions of PNG_HAVE_IHDR, PNG_AFTER_IDAT, and PNG_HAVE_PLTE ++ outside of an unknown-chunk block in png.h because they are also ++ needed for other uses. ++ ++Version 1.5.4beta02 [June 14, 2011] ++ Fixed and clarified LEGACY 16-to-8 scaling code. ++ Added png_set_chop_16() API, to match inaccurate results from previous ++ libpng versions. ++ Removed the ACCURATE and LEGACY options (they are no longer useable) ++ Use the old scaling method for background if png_set_chop_16() was ++ called. ++ Made png_set_chop_16() API removeable by disabling PNG_CHOP_16_TO_8_SUPPORTED ++ ++Version 1.5.4beta03 [June 15, 2011] ++ Fixed a problem in png_do_expand_palette() exposed by optimization in ++ 1.5.3beta06 ++ Also removed a spurious and confusing "trans" member ("trans") from png_info. ++ The palette expand optimization prevented expansion to an intermediate RGBA ++ form if tRNS was present but alpha was marked to be stripped; this exposed ++ a check for tRNS in png_do_expand_palette() which is inconsistent with the ++ code elsewhere in libpng. ++ Correction to the expand_16 code; removed extra instance of ++ png_set_scale_16_to_8 from pngpriv.h ++ ++Version 1.5.4beta04 [June 16, 2011] ++ Added a missing "#ifdef PNG_READ_BACKGROUND_SUPPORTED/#endif" in pngrtran.c ++ Added PNG_TRANSFORM_CHOP_16 to the high-level read transforms. ++ Made PNG_READ_16_TO_8_ACCURATE_SCALE configurable again. If this is ++ not enabled, png_set_strip_16() and png_do_scale_16_to_8() aren't built. ++ Revised contrib/visupng, gregbook, and pngminim to demonstrate chop_16_to_8 ++ ++Version 1.5.4beta05 [June 16, 2011] ++ Renamed png_set_strip_16() to png_set_scale_16() and renamed ++ png_set_chop_16() to png_set_strip(16) in an attempt to minimize the ++ behavior changes between libpng14 and libpng15. ++ ++Version 1.5.4beta06 [June 18, 2011] ++ Fixed new bug that was causing both strip_16 and scale_16 to be applied. ++ ++Version 1.5.4beta07 [June 19, 2011] ++ Fixed pngvalid, simplified macros, added checking for 0 in sCAL. ++ The ACCURATE scale macro is no longer defined in 1.5 - call the ++ png_scale_16_to_8 API. Made sure that PNG_READ_16_TO_8 is still defined ++ if the png_strip_16_to_8 API is present. png_check_fp_number now ++ maintains some state so that positive, negative and zero values are ++ identified. sCAL uses these to be strictly spec conformant. ++ ++Version 1.5.4beta08 [June 23, 2011] ++ Fixed pngvalid if ACCURATE_SCALE is defined. ++ Updated scripts/pnglibconf.h.prebuilt. ++ ++Version 1.5.4rc01 [June 30, 2011] ++ Define PNG_ALLOCATED to "restrict" only if MSC_VER >= 1400. ++ ++Version 1.5.4 [July 7, 2011] ++ No changes. ++ ++Version 1.5.5beta01 [July 13, 2011] ++ Fixed some typos and made other minor changes in the manual. ++ Updated contrib/pngminus/makefile.std (Samuli Souminen) ++ ++Version 1.5.5beta02 [July 14, 2011] ++ Revised Makefile.am and Makefile.in to look in the right directory for ++ pnglibconf.h.prebuilt ++ ++Version 1.5.5beta03 [July 27, 2011] ++ Enabled compilation with g++ compiler. This compiler does not recognize ++ the file extension, so it always compiles with C++ rules. Made minor ++ changes to pngrutil.c to cast results where C++ expects it but C does not. ++ Minor editing of libpng.3 and libpng-manual.txt. ++ ++Version 1.5.5beta04 [July 29, 2011] ++ Revised CMakeLists.txt (Clifford Yapp) ++ Updated commentary about the png_rgb_to_gray() default coefficients ++ in the manual and in pngrtran.c ++ ++Version 1.5.5beta05 [August 17, 2011] ++ Prevent unexpected API exports from non-libpng DLLs on Windows. The "_DLL" ++ is removed from the test of whether a DLL is being built (this erroneously ++ caused the libpng APIs to be marked as DLL exports in static builds under ++ Microsoft Visual Studio). Almost all of the libpng building configuration ++ is moved from pngconf.h to pngpriv.h, but PNG_DLL_EXPORT remains in ++ pngconf.h, though, so that it is colocated with the import definition (it ++ is no longer used anywhere in the installed headers). The VStudio project ++ definitions have been cleaned up: "_USRDLL" has been removed from the ++ static library builds (this was incorrect), and PNG_USE_DLL has been added ++ to pngvalid to test the functionality (pngtest does not supply it, ++ deliberately). The spurious "_EXPORTS" has been removed from the ++ libpng build (all these errors were a result of copy/paste between project ++ configurations.) ++ Added new types and internal functions for CIE RGB end point handling to ++ pngpriv.h (functions yet to be implemented). ++ ++Version 1.5.5beta06 [August 26, 2011] ++ Ensure the CMAKE_LIBRARY_OUTPUT_DIRECTORY is set in CMakeLists.txt ++ (Clifford Yap) ++ Fixes to rgb_to_gray and cHRM XYZ APIs (John Bowler): ++ The rgb_to_gray code had errors when combined with gamma correction. ++ Some pixels were treated as true grey when they weren't and such pixels ++ and true grey ones were not gamma corrected (the original value of the ++ red component was used instead). APIs to get and set cHRM using color ++ space end points have been added and the rgb_to_gray code that defaults ++ based on cHRM, and the divide-by-zero bug in png_handle_cHRM (CERT ++ VU#477046, CVE-2011-3328, introduced in 1.5.4) have been corrected. ++ A considerable number of tests has been added to pngvalid for the ++ rgb_to_gray transform. ++ Arithmetic errors in rgb_to_gray whereby the calculated gray value was ++ truncated to the bit depth rather than rounded have been fixed except in ++ the 8-bit non-gamma-corrected case (where consistency seems more important ++ than correctness.) The code still has considerable inaccuracies in the ++ 8-bit case because 8-bit linear arithmetic is used. ++ ++Version 1.5.5beta07 [September 7, 2011] ++ Added "$(ARCH)" option to makefile.darwin ++ Added SunOS support to configure.ac and Makefile.am ++ Changed png_chunk_benign_error() to png_warning() in png.c, in ++ png_XYZ_from_xy_checked(). ++ ++Version 1.5.5beta08 [September 10, 2011] ++ Fixed 64-bit compilation errors (gcc). The errors fixed relate ++ to conditions where types that are 32 bits in the GCC 32-bit ++ world (uLong and png_size_t) become 64 bits in the 64-bit ++ world. This produces potential truncation errors which the ++ compiler correctly flags. ++ Relocated new HAVE_SOLARIS_LD definition in configure.ac ++ Constant changes for 64-bit compatibility (removal of L suffixes). The ++ 16-bit cases still use "L" as we don't have a 16-bit test system. ++ ++Version 1.5.5rc01 [September 15, 2011] ++ Removed "L" suffixes in pngpriv.h ++ ++Version 1.5.5 [September 22, 2011] ++ No changes. ++ ++Version 1.5.6beta01 [September 22, 2011] ++ Fixed some 64-bit type conversion warnings in pngrtran.c ++ Moved row_info from png_struct to a local variable. ++ The various interlace mask arrays have been made into arrays of ++ bytes and made PNG_CONST and static (previously some arrays were ++ marked PNG_CONST and some weren't). ++ Additional checks have been added to the transform code to validate the ++ pixel depths after the transforms on both read and write. ++ Removed some redundant code from pngwrite.c, in png_destroy_write_struct(). ++ Changed chunk reading/writing code to use png_uint_32 instead of png_byte[4]. ++ This removes the need to allocate temporary strings for chunk names on ++ the stack in the read/write code. Unknown chunk handling still uses the ++ string form because this is exposed in the API. ++ ++Version 1.5.6beta02 [September 26, 2011] ++ Added a note in the manual the png_read_update_info() must be called only ++ once with a particular info_ptr. ++ Fixed a typo in the definition of the new PNG_STRING_FROM_CHUNK(s,c) macro. ++ ++Version 1.5.6beta03 [September 28, 2011] ++ Revised test-pngtest.sh to report FAIL when pngtest fails. ++ Added "--strict" option to pngtest, to report FAIL when the failure is ++ only because the resulting valid files are different. ++ Revised CMakeLists.txt to work with mingw and removed some material from ++ CMakeLists.txt that is no longer useful in libpng-1.5. ++ ++Version 1.5.6beta04 [October 5, 2011] ++ Fixed typo in Makefile.in and Makefile.am ("-M Wl" should be "-M -Wl")." ++ ++Version 1.5.6beta05 [October 12, 2011] ++ Speed up png_combine_row() for interlaced images. This reduces the generality ++ of the code, allowing it to be optimized for Adam7 interlace. The masks ++ passed to png_combine_row() are now generated internally, avoiding ++ some code duplication and localizing the interlace handling somewhat. ++ Align png_struct::row_buf - previously it was always unaligned, caused by ++ a bug in the code that attempted to align it; the code needs to subtract ++ one from the pointer to take account of the filter byte prepended to ++ each row. ++ Optimized png_combine_row() when rows are aligned. This gains a small ++ percentage for 16-bit and 32-bit pixels in the typical case where the ++ output row buffers are appropriately aligned. The optimization was not ++ previously possible because the png_struct buffer was always misaligned. ++ Fixed bug in png_write_chunk_header() debug print, introduced in 1.5.6beta01. ++ ++Version 1.5.6beta06 [October 17, 2011] ++ Removed two redundant tests for uninitialized row. ++ Fixed a relatively harmless memory overwrite in compressed text writing ++ with a 1 byte zlib buffer. ++ Add ability to call png_read_update_info multiple times to pngvalid.c. ++ Fixes for multiple calls to png_read_update_info. These fixes attend to ++ most of the errors revealed in pngvalid, however doing the gamma work ++ twice results in inaccuracies that can't be easily fixed. There is now ++ a warning in the code if this is going to happen. ++ Turned on multiple png_read_update_info in pngvalid transform tests. ++ Prevent libpng from overwriting unused bits at the end of the image when ++ it is not byte aligned, while reading. Prior to libpng-1.5.6 libpng would ++ overwrite the partial byte at the end of each row if the row width was not ++ an exact multiple of 8 bits and the image is not interlaced. ++ ++Version 1.5.6beta07 [October 21, 2011] ++ Made png_ptr->prev_row an aligned pointer into png_ptr->big_prev_row ++ (Mans Rullgard). ++ ++Version 1.5.6rc01 [October 26, 2011] ++ Changed misleading "Missing PLTE before cHRM" warning to "Out of place cHRM" ++ ++Version 1.5.6rc02 [October 27, 2011] ++ Added LSR() macro to defend against buggy compilers that evaluate non-taken ++ code branches and complain about out-of-range shifts. ++ ++Version 1.5.6rc03 [October 28, 2011] ++ Renamed the LSR() macro to PNG_LSR() and added PNG_LSL() macro. ++ Fixed compiler warnings with Intel and MSYS compilers. The logical shift ++ fix for Microsoft Visual C is required by other compilers, so this ++ enables that fix for all compilers when using compile-time constants. ++ Under MSYS 'byte' is a name declared in a system header file, so we ++ changed the name of a local variable to avoid the warnings that result. ++ Added #define PNG_ALIGN_TYPE PNG_ALIGN_NONE to contrib/pngminim/*/pngusr.h ++ ++Version 1.5.6 [November 3, 2011] ++ No changes. ++ ++Version 1.5.7beta01 [November 4, 2011] ++ Added support for ARM processor, when decoding all PNG up-filtered rows ++ and any other-filtered rows with 3 or 4 bytes per pixel (Mans Rullgard). ++ Fixed bug in pngvalid on early allocation failure; fixed type cast in ++ pngmem.c; pngvalid would attempt to call png_error() if the allocation ++ of a png_struct or png_info failed. This would probably have led to a ++ crash. The pngmem.c implementation of png_malloc() included a cast ++ to png_size_t which would fail on large allocations on 16-bit systems. ++ Fix for the preprocessor of the Intel C compiler. The preprocessor ++ splits adjacent @ signs with a space; this changes the concatenation ++ token from @-@-@ to PNG_JOIN; that should work with all compiler ++ preprocessors. ++ Paeth filter speed improvements from work by Siarhei Siamashka. This ++ changes the 'Paeth' reconstruction function to improve the GCC code ++ generation on x86. The changes are only part of the suggested ones; ++ just the changes that definitely improve speed and remain simple. ++ The changes also slightly increase the clarity of the code. ++ ++Version 1.5.7beta02 [November 11, 2011] ++ Check compression_type parameter in png_get_iCCP and remove spurious ++ casts. The compression_type parameter is always assigned to, so must ++ be non-NULL. The cast of the profile length potentially truncated the ++ value unnecessarily on a 16-bit int system, so the cast of the (byte) ++ compression type to (int) is specified by ANSI-C anyway. ++ Fixed FP division by zero in pngvalid.c; the 'test_pixel' code left ++ the sBIT fields in the test pixel as 0, which resulted in a floating ++ point division by zero which was irrelevant but causes systems where ++ FP exceptions cause a crash. Added code to pngvalid to turn on FP ++ exceptions if the appropriate glibc support is there to ensure this is ++ tested in the future. ++ Updated scripts/pnglibconf.mak and scripts/makefile.std to handle the ++ new PNG_JOIN macro. ++ Added versioning to pnglibconf.h comments. ++ Simplified read/write API initial version; basic read/write tested on ++ a variety of images, limited documentation (in the header file.) ++ Installed more accurate linear to sRGB conversion tables. The slightly ++ modified tables reduce the number of 16-bit values that ++ convert to an off-by-one 8-bit value. The "makesRGB.c" code that was used ++ to generate the tables is now in a contrib/sRGBtables sub-directory. ++ ++Version 1.5.7beta03 [November 17, 2011] ++ Removed PNG_CONST from the sRGB table declarations in pngpriv.h and png.c ++ Added run-time detection of NEON support. ++ Added contrib/libtests; includes simplified API test and timing test and ++ a color conversion utility for rapid checking of failed 'pngstest' results. ++ Multiple transform bug fixes plus a work-round for double gamma correction. ++ libpng does not support more than one transform that requires linear data ++ at once - if this is tried typically the results is double gamma ++ correction. Since the simplified APIs can need rgb to gray combined with ++ a compose operation it is necessary to do one of these outside the main ++ libpng transform code. This check-in also contains fixes to various bugs ++ in the simplified APIs themselves and to some bugs in compose and rgb to ++ gray (on palette) itself. ++ Fixes for C++ compilation using g++ When libpng source is compiled ++ using g++. The compiler imposes C++ rules on the C source; thus it ++ is desirable to make the source work with either C or C++ rules ++ without throwing away useful error information. This change adds ++ png_voidcast to allow C semantic (void*) cases or the corresponding ++ C++ static_cast operation, as appropriate. ++ Added --noexecstack to assembler file compilation. GCC does not set ++ this on assembler compilation, even though it does on C compilation. ++ This creates security issues if assembler code is enabled; the ++ work-around is to set it by default in the flags for $(CCAS) ++ Work around compilers that don't support declaration of const data. Some ++ compilers fault 'extern const' data declarations (because the data is ++ not initialized); this turns on const-ness only for compilers where ++ this is known to work. ++ ++Version 1.5.7beta04 [November 17, 2011] ++ Since the gcc driver does not recognize the --noexecstack flag, we must ++ use the -Wa prefix to have it passed through to the assembler. ++ Also removed a duplicate setting of this flag. ++ Added files that were omitted from the libpng-1.5.7beta03 zip distribution. ++ ++Version 1.5.7beta05 [November 25, 2011] ++ Removed "zTXt" from warning in generic chunk decompression function. ++ Validate time settings passed to png_set_tIME() and png_convert_to_rfc1123() ++ (Frank Busse). Note: This prevented CVE-2015-7981 from affecting ++ libpng-1.5.7 and later. ++ Added MINGW support to CMakeLists.txt ++ Reject invalid compression flag or method when reading the iTXt chunk. ++ Backed out 'simplified' API changes. The API seems too complex and there ++ is a lack of consensus or enthusiasm for the proposals. The API also ++ reveals significant bugs inside libpng (double gamma correction and the ++ known bug of being unable to retrieve a corrected palette). It seems ++ better to wait until the bugs, at least, are corrected. ++ Moved pngvalid.c into contrib/libtests ++ Rebuilt Makefile.in, configure, etc., with autoconf-2.68 ++ ++Version 1.5.7rc01 [December 1, 2011] ++ Replaced an "#if" with "#ifdef" in pngrtran.c ++ Revised #if PNG_DO_BC block in png.c (use #ifdef and add #else) ++ ++Version 1.5.7rc02 [December 5, 2011] ++ Revised project files and contrib/pngvalid/pngvalid.c to account for ++ the relocation of pngvalid into contrib/libtests. ++ Revised pngconf.h to use " __declspec(restrict)" only when MSC_VER >= 1400, ++ as in libpng-1.5.4. ++ Put CRLF line endings in the owatcom project files. ++ ++Version 1.5.7rc03 [December 7, 2011] ++ Updated CMakeLists.txt to account for the relocation of pngvalid.c ++ ++Version 1.5.7 [December 15, 2011] ++ Minor fixes to pngvalid.c for gcc 4.6.2 compatibility to remove warnings ++ reported by earlier versions. ++ Fixed minor memset/sizeof errors in pngvalid.c. ++ ++Version 1.6.0beta01 [December 15, 2011] ++ Removed machine-generated configure files from the GIT repository (they will ++ continue to appear in the tarball distributions and in the libpng15 and ++ earlier GIT branches). ++ Restored the new 'simplified' API, which was started in libpng-1.5.7beta02 ++ but later deleted from libpng-1.5.7beta05. ++ Added example programs for the new 'simplified' API. ++ Added ANSI-C (C90) headers and require them, and take advantage of the ++ change. Also fixed some of the projects/* and contrib/* files that needed ++ updates for libpng16 and the move of pngvalid.c. ++ With this change the required ANSI-C header files are assumed to exist: the ++ implementation must provide float.h, limits.h, stdarg.h and stddef.h and ++ libpng relies on limits.h and stddef.h existing and behaving as defined ++ (the other two required headers aren't used). Non-ANSI systems that don't ++ have stddef.h or limits.h will have to provide an appropriate fake ++ containing the relevant types and #defines. ++ Dropped support for 16-bit platforms. The use of FAR/far has been eliminated ++ and the definition of png_alloc_size_t is now controlled by a flag so ++ that 'small size_t' systems can select it if necessary. Libpng 1.6 may ++ not currently work on such systems -- it seems likely that it will ++ ask 'malloc' for more than 65535 bytes with any image that has a ++ sufficiently large row size (rather than simply failing to read such ++ images). ++ New tools directory containing tools used to generate libpng code. ++ Fixed race conditions in parallel make builds. With higher degrees of ++ parallelism during 'make' the use of the same temporary file names such ++ as 'dfn*' can result in a race where a temporary file from one arm of the ++ build is deleted or overwritten in another arm. This changes the ++ temporary files for suffix rules to always use $* and ensures that the ++ non-suffix rules use unique file names. ++ ++Version 1.6.0beta02 [December 21, 2011] ++ Correct configure builds where build and source directories are separate. ++ The include path of 'config.h' was erroneously made relative in pngvalid.c ++ in libpng 1.5.7. ++ ++Version 1.6.0beta03 [December 22, 2011] ++ Start-up code size improvements, error handler flexibility. These changes ++ alter how the tricky allocation of the initial png_struct and png_info ++ structures are handled. png_info is now handled in pretty much the same ++ way as everything else, except that the allocations handle NULL return ++ silently. png_struct is changed in a similar way on allocation and on ++ deallocation a 'safety' error handler is put in place (which should never ++ be required). The error handler itself is changed to permit mismatches ++ in the application and libpng error buffer size; however, this means a ++ silent change to the API to return the jmp_buf if the size doesn't match ++ the size from the libpng compilation; libpng now allocates the memory and ++ this may fail. Overall these changes result in slight code size ++ reductions; however, this is a reduction in code that is always executed ++ so is particularly valuable. Overall on a 64-bit system the libpng DLL ++ decreases in code size by 1733 bytes. pngerror.o increases in size by ++ about 465 bytes because of the new functionality. ++ Added png_convert_to_rfc1123_buffer() and deprecated png_convert_to_rfc1123() ++ to avoid including a spurious buffer in the png_struct. ++ ++Version 1.6.0beta04 [December 30, 2011] ++ Regenerated configure scripts with automake-1.11.2 ++ Eliminated png_info_destroy(). It is now used only in png.c and only calls ++ one other internal function and memset(). ++ Enabled png_get_sCAL_fixed() if floating point APIs are enabled. Previously ++ it was disabled whenever internal fixed point arithmetic was selected, ++ which meant it didn't exist even on systems where FP was available but not ++ preferred. ++ Added pngvalid.c compile time checks for const APIs. ++ Implemented 'restrict' for png_info and png_struct. Because of the way ++ libpng works both png_info and png_struct are always accessed via a ++ single pointer. This means adding C99 'restrict' to the pointer gives ++ the compiler some opportunity to optimize the code. This change allows ++ that. ++ Moved AC_MSG_CHECKING([if libraries can be versioned]) later to the proper ++ location in configure.ac (Gilles Espinasse). ++ Changed png_memcpy to C assignment where appropriate. Changed all those ++ uses of png_memcpy that were doing a simple assignment to assignments ++ (all those cases where the thing being copied is a non-array C L-value). ++ Added some error checking to png_set_*() routines. ++ Removed the reference to the non-exported function png_memcpy() from ++ example.c. ++ Fixed the Visual C 64-bit build - it requires jmp_buf to be aligned, but ++ it had become misaligned. ++ Revised contrib/pngminus/pnm2png.c to avoid warnings when png_uint_32 ++ and unsigned long are of different sizes. ++ ++Version 1.6.0beta05 [January 15, 2012] ++ Updated manual with description of the simplified API (copied from png.h) ++ Fix bug in pngerror.c: some long warnings were being improperly truncated ++ (CVE-2011-3464, bug introduced in libpng-1.5.3beta05). ++ ++Version 1.6.0beta06 [January 24, 2012] ++ Added palette support to the simplified APIs. This commit ++ changes some of the macro definitions in png.h, app code ++ may need corresponding changes. ++ Increased the formatted warning buffer to 192 bytes. ++ Added color-map support to simplified API. This is an initial version for ++ review; the documentation has not yet been updated. ++ Fixed Min/GW uninstall to remove libpng.dll.a ++ ++Version 1.6.0beta07 [January 28, 2012] ++ Eliminated Intel icc/icl compiler warnings. The Intel (GCC derived) ++ compiler issues slightly different warnings from those issued by the ++ current vesions of GCC. This eliminates those warnings by ++ adding/removing casts and small code rewrites. ++ Updated configure.ac from autoupdate: added --enable-werror option. ++ Also some layout regularization and removal of introduced tab characters ++ (replaced with 3-character indentation). Obsolete macros identified by ++ autoupdate have been removed; the replacements are all in 2.59 so ++ the pre-req hasn't been changed. --enable-werror checks for support ++ for -Werror (or the given argument) in the compiler. This mimics the ++ gcc configure option by allowing -Werror to be turned on safely; without ++ the option the tests written in configure itself fail compilation because ++ they cause compiler warnings. ++ Rewrote autogen.sh to run autoreconf instead of running tools one-by-one. ++ Conditionalize the install rules for MINGW and CYGWIN in CMakeLists.txt and ++ set CMAKE_LIBRARY_OUTPUT_DIRECTORY to "lib" on all platforms (C. Yapp). ++ Freeze libtool files in the 'scripts' directory. This version of autogen.sh ++ attempts to dissuade people from running it when it is not, or should not, ++ be necessary. In fact, autogen.sh does not work when run in a libpng ++ directory extracted from a tar distribution anymore. You must run it in ++ a GIT clone instead. ++ Added two images to contrib/pngsuite (1-bit and 2-bit transparent grayscale), ++ and renamed three whose names were inconsistent with those in ++ pngsuite/README.txt. ++ ++Version 1.6.0beta08 [February 1, 2012] ++ Fixed Image::colormap misalignment in pngstest.c ++ Check libtool/libtoolize version number (2.4.2) in configure.ac ++ Divide test-pngstest.sh into separate pngstest runs for basic and ++ transparent images. ++ Moved automake options to AM_INIT_AUTOMAKE in configure.ac ++ Added color-tests, silent-rules (Not yet implemented in Makefile.am) and ++ version checking to configure.ac ++ Improved pngstest speed by not doing redundant tests and add const to ++ the background parameter of png_image_finish_read. The --background ++ option is now done automagically only when required, so that commandline ++ option no longer exists. ++ Cleaned up pngpriv.h to consistently declare all functions and data. ++ Also eliminated PNG_CONST_DATA, which is apparently not needed but we ++ can't be sure until it is gone. ++ Added symbol prefixing that allows all the libpng external symbols ++ to be prefixed (suggested by Reuben Hawkins). ++ Updated "ftbb*.png" list in the owatcom and vstudio projects. ++ Fixed 'prefix' builds on clean systems. The generation of pngprefix.h ++ should not require itself. ++ Updated INSTALL to explain that autogen.sh must be run in a GIT clone, ++ not in a libpng directory extracted from a tar distribution. ++ ++Version 1.6.0beta09 [February 1, 2012] ++ Reverted the prebuilt configure files to libpng-1.6.0beta05 condition. ++ ++Version 1.6.0beta10 [February 3, 2012] ++ Added Z_SOLO for zlib-1.2.6+ and correct pngstest tests ++ Updated list of test images in CMakeLists.txt ++ Updated the prebuilt configure files to current condition. ++ Revised INSTALL information about autogen.sh; it works in tar distributions. ++ ++Version 1.6.0beta11 [February 16, 2012] ++ Fix character count in pngstest command in projects/owatcom/pngstest.tgt ++ Revised test-pngstest.sh to report PASS/FAIL for each image. ++ Updated documentation about the simplified API. ++ Corrected estimate of error in libpng png_set_rgb_to_gray API. The API is ++ extremely inaccurate for sRGB conversions because it uses an 8-bit ++ intermediate linear value and it does not use the sRGB transform, so it ++ suffers from the known instability in gamma transforms for values close ++ to 0 (see Poynton). The net result is that the calculation has a maximum ++ error of 14.99/255; 0.5/255^(1/2.2). pngstest now uses 15 for the ++ permitted 8-bit error. This may still not be enough because of arithmetic ++ error. ++ Removed some unused arrays (with #ifdef) from png_read_push_finish_row(). ++ Fixed a memory overwrite bug in simplified read of RGB PNG with ++ non-linear gamma Also bugs in the error checking in pngread.c and changed ++ quite a lot of the checks in pngstest.c to be correct; either correctly ++ written or not over-optimistic. The pngstest changes are insufficient to ++ allow all possible RGB transforms to be passed; pngstest cmppixel needs ++ to be rewritten to make it clearer which errors it allows and then changed ++ to permit known inaccuracies. ++ Removed tests for no-longer-used *_EMPTY_PLTE_SUPPORTED from pngstruct.h ++ Fixed fixed/float API export conditionals. 1) If FIXED_POINT or ++ FLOATING_POINT options were switched off, png.h ended up with lone ';' ++ characters. This is not valid ANSI-C outside a function. The ';' ++ characters have been moved inside the definition of PNG_FP_EXPORT and ++ PNG_FIXED_EXPORT. 2) If either option was switched off, the declaration ++ of the corresponding functions were completely omitted, even though some ++ of them are still used internally. The result is still valid, but ++ produces warnings from gcc with some warning options (including -Wall). The ++ fix is to cause png.h to declare the functions with PNG_INTERNAL_FUNCTION ++ when png.h is included from pngpriv.h. ++ Check for invalid palette index while reading paletted PNG. When one is ++ found, issue a warning and increase png_ptr->num_palette accordingly. ++ Apps are responsible for checking to see if that happened. ++ ++Version 1.6.0beta12 [February 18, 2012] ++ Do not increase num_palette on invalid_index. ++ Relocated check for invalid palette index to pngrtran.c, after unpacking ++ the sub-8-bit pixels. ++ Fixed CVE-2011-3026 buffer overrun bug. This bug was introduced when ++ iCCP chunk support was added at libpng-1.0.6. Deal more correctly with the ++ test on iCCP chunk length. Also removed spurious casts that may hide ++ problems on 16-bit systems. ++ ++Version 1.6.0beta13 [February 24, 2012] ++ Eliminated redundant png_push_read_tEXt|zTXt|iTXt|unknown code from ++ pngpread.c and use the sequential png_handle_tEXt, etc., in pngrutil.c; ++ now that png_ptr->buffer is inaccessible to applications, the special ++ handling is no longer useful. ++ Added PNG_SAFE_LIMITS feature to pnglibconf.dfa, pngpriv.h, and new ++ pngusr.dfa to reset the user limits to safe ones if PNG_SAFE_LIMITS is ++ defined. To enable, use "CPPFLAGS=-DPNG_SAFE_LIMITS_SUPPORTED=1" on the ++ configure command or put #define PNG_SAFE_LIMITS_SUPPORTED in ++ pnglibconf.h.prebuilt and pnglibconf.h. ++ ++Version 1.6.0beta14 [February 27, 2012] ++ Added information about the new limits in the manual. ++ Updated Makefile.in ++ ++Version 1.6.0beta15 [March 2, 2012] ++ Removed unused "current_text" members of png_struct and the png_free() ++ of png_ptr->current_text from pngread.c ++ Rewrote pngstest.c for substantial speed improvement. ++ Fixed transparent pixel and 16-bit rgb tests in pngstest and removed a ++ spurious check in pngwrite.c ++ Added PNG_IMAGE_FLAG_FAST for the benefit of applications that store ++ intermediate files, or intermediate in-memory data, while processing ++ image data with the simplified API. The option makes the files larger ++ but faster to write and read. pngstest now uses this by default; this ++ can be disabled with the --slow option. ++ Improved pngstest fine tuning of error numbers, new test file generator. ++ The generator generates images that test the full range of sample values, ++ allow the error numbers in pngstest to be tuned and checked. makepng ++ also allows generation of images with extra chunks, although this is ++ still work-in-progress. ++ Added check for invalid palette index while reading. ++ Fixed some bugs in ICC profile writing. The code should now accept ++ all potentially valid ICC profiles and reject obviously invalid ones. ++ It now uses png_error() to do so rather than casually writing a PNG ++ without the necessary color data. ++ Removed whitespace from the end of lines in all source files and scripts. ++ ++Version 1.6.0beta16 [March 6, 2012] ++ Relocated palette-index checking function from pngrutil.c to pngtrans.c ++ Added palette-index checking while writing. ++ Changed png_inflate() and calling routines to avoid overflow problems. ++ This is an intermediate check-in that solves the immediate problems and ++ introduces one performance improvement (avoiding a copy via png_ptr->zbuf.) ++ Further changes will be made to make ICC profile handling more secure. ++ Fixed build warnings (MSVC, GCC, GCC v3). Cygwin GCC with default options ++ declares 'index' as a global, causing a warning if it is used as a local ++ variable. GCC 64-bit warns about assigning a (size_t) (unsigned 64-bit) ++ to an (int) (signed 32-bit). MSVC, however, warns about using the ++ unary '-' operator on an unsigned value (even though it is well defined ++ by ANSI-C to be ~x+1). The padding calculation was changed to use a ++ different method. Removed the tests on png_ptr->pass. ++ Added contrib/libtests/tarith.c to test internal arithmetic functions from ++ png.c. This is a libpng maintainer program used to validate changes to the ++ internal arithmetic functions. ++ Made read 'inflate' handling like write 'deflate' handling. The read ++ code now claims and releases png_ptr->zstream, like the write code. ++ The bug whereby the progressive reader failed to release the zstream ++ is now fixed, all initialization is delayed, and the code checks for ++ changed parameters on deflate rather than always calling ++ deflatedEnd/deflateInit. ++ Validate the zTXt strings in pngvalid. ++ Added code to validate the windowBits value passed to deflateInit2(). ++ If the call to deflateInit2() is wrong a png_warning will be issued ++ (in fact this is harmless, but the PNG data produced may be sub-optimal). ++ ++Version 1.6.0beta17 [March 10, 2012] ++ Fixed PNG_LIBPNG_BUILD_BASE_TYPE definition. ++ Reject all iCCP chunks after the first, even if the first one is invalid. ++ Deflate/inflate was reworked to move common zlib calls into single ++ functions [rw]util.c. A new shared keyword check routine was also added ++ and the 'zbuf' is no longer allocated on progressive read. It is now ++ possible to call png_inflate() incrementally. A warning is no longer ++ issued if the language tag or translated keyword in the iTXt chunk ++ has zero length. ++ If benign errors are disabled use maximum window on ancillary inflate. ++ This works round a bug introduced in 1.5.4 where compressed ancillary ++ chunks could end up with a too-small windowBits value in the deflate ++ header. ++ ++Version 1.6.0beta18 [March 16, 2012] ++ Issue a png_benign_error() instead of png_warning() about bad palette index. ++ In pngtest, treat benign errors as errors if "-strict" is present. ++ Fixed an off-by-one error in the palette index checking function. ++ Fixed a compiler warning under Cygwin (Windows-7, 32-bit system) ++ Revised example.c to put text strings in a temporary character array ++ instead of directly assigning string constants to png_textp members. ++ This avoids compiler warnings when -Wwrite-strings is enabled. ++ Added output flushing to aid debugging under Visual Studio. Unfortunately ++ this is necessary because the VS2010 output window otherwise simply loses ++ the error messages on error (they weren't flushed to the window before ++ the process exited, apparently!) ++ Added configuration support for benign errors and changed the read ++ default. Also changed some warnings in the iCCP and sRGB handling ++ from to benign errors. Configuration now makes read benign ++ errors warnings and write benign errors to errors by default (thus ++ changing the behavior on read). The simplified API always forces ++ read benign errors to warnings (regardless of the system default, unless ++ this is disabled in which case the simplified API can't be built.) ++ ++Version 1.6.0beta19 [March 18, 2012] ++ Work around for duplicate row start calls; added warning messages. ++ This turns on PNG_FLAG_DETECT_UNINITIALIZED to detect app code that ++ fails to call one of the 'start' routines (not enabled in libpng-1.5 ++ because it is technically an API change, since it did normally work ++ before.) It also makes duplicate calls to png_read_start_row (an ++ internal function called at the start of the image read) benign, as ++ they were before changes to use png_inflate_claim. Somehow webkit is ++ causing this to happen; this is probably a mis-feature in the zlib ++ changes so this commit is only a work-round. ++ Removed erroneous setting of DETECT_UNINITIALIZED and added more ++ checks. The code now does a png_error if an attempt is made to do the ++ row initialization twice; this is an application error and it has ++ serious consequences because the transform data in png_struct is ++ changed by each call. ++ Added application error reporting and added chunk names to read ++ benign errors; also added --strict to pngstest - not enabled ++ yet because a warning is produced. ++ Avoid the double gamma correction warning in the simplified API. ++ This allows the --strict option to pass in the pngstest checks ++ ++Version 1.6.0beta20 [March 29, 2012] ++ Changed chunk handler warnings into benign errors, incrementally load iCCP ++ Added checksum-icc.c to contrib/tools ++ Prevent PNG_EXPAND+PNG_SHIFT doing the shift twice. ++ Recognize known sRGB ICC profiles while reading; prefer writing the ++ iCCP profile over writing the sRGB chunk, controlled by the ++ PNG_sRGB_PROFILE_CHECKS option. ++ Revised png_set_text_2() to avoid potential memory corruption (fixes ++ CVE-2011-3048, also known as CVE-2012-3425). ++ ++Version 1.6.0beta21 [April 27, 2012] ++ Revised scripts/makefile.darwin: use system zlib; remove quotes around ++ architecture list; add missing ppc architecture; add architecture options ++ to shared library link; don't try to create a shared lib based on missing ++ RELEASE variable. ++ Enable png_set_check_for_invalid_index() for both read and write. ++ Removed #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED in pngpriv.h around ++ declaration of png_handle_unknown(). ++ Added -lssp_nonshared in a comment in scripts/makefile.freebsd ++ and changed deprecated NOOBJ and NOPROFILE to NO_OBJ and NO_PROFILE. ++ ++Version 1.6.0beta22 [May 23, 2012] ++ Removed need for -Wno-cast-align with clang. clang correctly warns on ++ alignment increasing pointer casts when -Wcast-align is passed. This ++ fixes the cases that clang warns about either by eliminating the ++ casts from png_bytep to png_uint_16p (pngread.c), or, for pngrutil.c ++ where the cast is previously verified or pngstest.c where it is OK, by ++ introducing new png_aligncast macros to do the cast in a way that clang ++ accepts. ++ ++Version 1.6.0beta23 [June 6, 2012] ++ Revised CMakeLists.txt to not attempt to make a symlink under mingw. ++ Made fixes for new optimization warnings from gcc 4.7.0. The compiler ++ performs an optimization which is safe; however it then warns about it. ++ Changing the type of 'palette_number' in pngvalid.c removes the warning. ++ Do not depend upon a GCC feature macro being available for use in generating ++ the linker mapfile symbol prefix. ++ Improved performance of new do_check_palette_indexes() function (only ++ update the value when it actually increases, move test for whether ++ the check is wanted out of the function. ++ ++Version 1.6.0beta24 [June 7, 2012] ++ Don't check palette indexes if num_palette is 0 (as it can be in MNG files). ++ ++Version 1.6.0beta25 [June 16, 2012] ++ Revised png_set_keep_unknown_chunks() so num_chunks < 0 means ignore all ++ unknown chunks and all known chunks except for IHDR, PLTE, tRNS, IDAT, ++ and IEND. Previously it only meant ignore all unknown chunks, the ++ same as num_chunks == 0. Revised png_image_skip_unused_chunks() to ++ provide a list of chunks to be processed instead of a list of chunks to ++ ignore. Revised contrib/gregbook/readpng2.c accordingly. ++ ++Version 1.6.0beta26 [July 10, 2012] ++ Removed scripts/makefile.cegcc from the *.zip and *.7z distributions; it ++ depends on configure, which is not included in those archives. ++ Moved scripts/chkfmt to contrib/tools. ++ Changed "a+w" to "u+w" in Makefile.in to fix CVE-2012-3386. ++ ++Version 1.6.0beta27 [August 11, 2012] ++ Do not compile PNG_DEPRECATED, PNG_ALLOC and PNG_PRIVATE when __GNUC__ < 3. ++ Do not use __restrict when GNUC is <= 3.1 ++ Removed references to png_zalloc() and png_zfree() from the manual. ++ Fixed configurations where floating point is completely disabled. Because ++ of the changes to support symbol prefixing PNG_INTERNAL_FUNCTION declares ++ floating point APIs during libpng builds even if they are completely ++ disabled. This requires the png floating point types (png_double*) to be ++ declared even though the functions are never actually defined. This ++ change provides a dummy definition so that the declarations work, yet any ++ implementation will fail to compile because of an incomplete type. ++ Re-eliminated the use of strcpy() in pngtest.c. An unnecessary use of ++ strcpy() was accidentally re-introduced in libpng16; this change replaces ++ it with strncpy(). ++ Eliminated use of png_sizeof(); use sizeof() instead. ++ Use a consistent style for (sizeof type) and (sizeof (array)) ++ Cleanup of png_set_filler(). This function does very different things on ++ read and write. In libpng 1.6 the two cases can be distinguished and ++ considerable code cleanup, and extra error checking, is possible. This ++ makes calls on the write side that have no effect be ignored with a ++ png_app_error(), which can be disabled in the app using ++ png_set_benign_errors(), and removes the spurious use of usr_channels ++ on the read side. ++ Insist on autotools 1.12.1 for git builds because there are security issues ++ with 1.12 and insisting on anything less would allow 1.12 to be used. ++ Removed info_ptr->signature[8] from WRITE-only builds. ++ Add some conditions for compiling png_fixed(). This is a small function ++ but it requires "-lm" on some platforms. ++ Cause pngtest --strict to fail on any warning from libpng (not just errors) ++ and cause it not to fail at the comparison step if libpng lacks support ++ for writing chunks that it reads from the input (currently only implemented ++ for compressed text chunks). ++ Make all three "make check" test programs work without READ or WRITE support. ++ Now "make check" will succeed even if libpng is compiled with -DPNG_NO_READ ++ or -DPNG_NO_WRITE. The tests performed are reduced, but the basic reading ++ and writing of a PNG file is always tested by one or more of the tests. ++ Consistently use strlen(), memset(), memcpy(), and memcmp() instead of the ++ png_strlen(), png_memset(), png_memcpy(), and png_memcmp() macros. ++ Removed the png_sizeof(), png_strlen(), png_memset(), png_memcpy(), and ++ png_memcmp() macros. ++ Work around gcc 3.x and Microsoft Visual Studio 2010 complaints. Both object ++ to the split initialization of num_chunks. ++ ++Version 1.6.0beta28 [August 29, 2012] ++ Unknown handling fixes and clean up. This adds more correct option ++ control of the unknown handling, corrects the pre-existing bug where ++ the per-chunk 'keep' setting is ignored and makes it possible to skip ++ IDAT chunks in the sequential reader (broken in earlier 1.6 versions). ++ There is a new test program, test-unknown.c, which is a work in progress ++ (not currently part of the test suite). Comments in the header files now ++ explain how the unknown handling works. ++ Allow fine grain control of unknown chunk APIs. This change allows ++ png_set_keep_unknown_chunks() to be turned off if not required and causes ++ both read and write to behave appropriately (on read this is only possible ++ if the user callback is used to handle unknown chunks). The change ++ also removes the support for storing unknown chunks in the info_struct ++ if the only unknown handling enabled is via the callback, allowing libpng ++ to be configured with callback reading and none of the unnecessary code. ++ Corrected fix for unknown handling in pngtest. This reinstates the ++ libpng handling of unknown chunks other than vpAg and sTER (including ++ unsafe-to-copy chunks which were dropped before) and eliminates the ++ repositioning of vpAg and sTER in pngtest.png by changing pngtest.png ++ (so the chunks are where libpng would put them). ++ Added "tunknown" test and corrected a logic error in png_handle_unknown() ++ when SAVE support is absent. Moved the shell test scripts for ++ contrib/libtests from the libpng top directory to contrib/libtests. ++ png_handle_unknown() must always read or skip the chunk, if ++ SAVE_UNKNOWN_CHUNKS is turned off *and* the application does not set ++ a user callback an unknown chunk will not be read, leading to a read ++ error, which was revealed by the "tunknown" test. ++ Cleaned up and corrected ICC profile handling. ++ contrib/libtests/makepng: corrected 'rgb' and 'gray' cases. profile_error ++ messages could be truncated; made a correct buffer size calculation and ++ adjusted pngerror.c appropriately. png_icc_check_* checking improved; ++ changed the functions to receive the correct color type of the PNG on read ++ or write and check that it matches the color space of the profile (despite ++ what the comments said before, there is danger in assuming the app will ++ cope correctly with an RGB profile on a grayscale image and, since it ++ violates the PNG spec, allowing it is certain to produce inconsistent ++ app behavior and might even cause app crashes.) Check that profiles ++ contain the tags needed to process the PNG (tags all required by the ICC ++ spec). Removed unused PNG_STATIC from pngpriv.h. ++ ++Version 1.6.0beta29 [September 4, 2012] ++ Fixed the simplified API example programs to add the *colormap parameter ++ to several of he API and improved the error message if the version field ++ is not set. ++ Added contrib/examples/* to the *.zip and *.7z distributions. ++ Updated simplified API synopses and description of the png_image structure ++ in the manual. ++ Made makepng and pngtest produce identical PNGs, add "--relaxed" option ++ to pngtest. The "--relaxed" option turns off the benign errors that are ++ enabled by default in pre-RC builds. makepng can now write ICC profiles ++ where the length has not been extended to a multiple of 4, and pngtest ++ now intercepts all libpng errors, allowing the previously-introduced ++ "--strict test" on no warnings to actually work. ++ Improved ICC profile handling including cHRM chunk generation and fixed ++ Cygwin+MSVC build errors. The ICC profile handling now includes more ++ checking. Several errors that caused rejection of the profile are now ++ handled with a warning in such a way that the invalid profiles will be ++ read by default in release (but not pre-RC) builds but will not be ++ written by default. The easy part of handling the cHRM chunk is written, ++ where the ICC profile contains the required data. The more difficult ++ part plus guessing a gAMA value requires code to pass selected RGB values ++ through the profile. ++ ++Version 1.6.0beta30 [October 24, 2012] ++ Changed ICC profile matrix/vector types to not depend on array type rules. ++ By the ANSI-C standard the new types should be identical to the previous ++ versions, and all known versions of gcc tested with the previous versions ++ except for GCC-4.2.1 work with this version. The change makes the ANSI-C ++ rule that const applied to an array of elements applies instead to the ++ elements in the array moot by explicitly applying const to the base ++ elements of the png_icc_matrix and png_icc_vector types. The accidental ++ (harmless) 'const' previously applied to the parameters of two of the ++ functions have also been removed. ++ Added a work around for GCC 4.2 optimization bug. ++ Marked the broken (bad white point) original HP sRGB profiles correctly and ++ correct comments. ++ Added -DZ_SOLO to contrib/pngminim/*/makefile to work with zlib-1.2.7 ++ Use /MDd for vstudio debug builds. Also added pngunkown to the vstudio ++ builds, fixed build errors and corrected a minor exit code error in ++ pngvalid if the 'touch' file name is invalid. ++ Add updated WARNING file to projects/vstudio from libpng 1.5/vstudio ++ Fixed build when using #define PNG_NO_READ_GAMMA in png_do_compose() in ++ pngrtran.c (Domani Hannes). ++ ++Version 1.6.0beta31 [November 1, 2012] ++ Undid the erroneous change to vstudio/pngvalid build in libpng-1.6.0beta30. ++ Made pngvalid so that it will build outside the libpng source tree. ++ Made builds -DPNG_NO_READ_GAMMA compile (the unit tests still fail). ++ Made PNG_NO_READ_GAMMA switch off interfaces that depend on READ_GAMMA. ++ Prior to 1.6.0 switching off READ_GAMMA did unpredictable things to the ++ interfaces that use it (specifically, png_do_background in 1.4 would ++ simply display composite for grayscale images but do composition ++ with the incorrect arithmetic for color ones). In 1.6 the semantic ++ of -DPNG_NO_READ_GAMMA is changed to simply disable any interface that ++ depends on it; this obliges people who set it to consider whether they ++ really want it off if they happen to use any of the interfaces in ++ question (typically most users who disable it won't). ++ Fixed GUIDs in projects/vstudio. Some were duplicated or missing, ++ resulting in VS2010 having to update the files. ++ Removed non-working ICC profile support code that was mostly added to ++ libpng-1.6.0beta29 and beta30. There was too much code for too little ++ gain; implementing full ICC color correction may be desirable but is left ++ up to applications. ++ ++Version 1.6.0beta32 [November 25, 2012] ++ Fixed an intermittent SEGV in pngstest due to an uninitialized array element. ++ Added the ability for contrib/libtests/makepng.c to make a PNG with just one ++ color. This is useful for debugging pngstest color inaccuracy reports. ++ Fixed error checking in the simplified write API (Olaf van der Spek) ++ Made png_user_version_check() ok to use with libpng version 1.10.x and later. ++ ++Version 1.6.0beta33 [December 15, 2012] ++ Fixed typo in png.c (PNG_SET_CHUNK_MALLOC_MAX should be PNG_CHUNK_MALLOC_MAX) ++ that causes the MALLOC_MAX limit not to work (John Bowler) ++ Change png_warning() to png_app_error() in pngwrite.c and comment the ++ fall-through condition. ++ Change png_warning() to png_app_warning() in png_write_tRNS(). ++ Rearranged the ARM-NEON optimizations: Isolated the machine specific code ++ to the hardware subdirectory and added comments to pngrutil.c so that ++ implementors of other optimizations know what to do. ++ Fixed cases of unquoted DESTDIR in Makefile.am ++ Rebuilt Makefile.in, etc., with autoconf-2.69 and automake-1.12.5. ++ ++Version 1.6.0beta34 [December 19, 2012] ++ Cleaned up whitespace in the synopsis portion of the manpage "libpng.3" ++ Disassembled the version number in scripts/options.awk (necessary for ++ building on SunOs). ++ ++Version 1.6.0beta35 [December 23, 2012] ++ Made default Zlib compression settings be configurable. This adds #defines to ++ pnglibconf.h to control the defaults. ++ Fixed Windows build issues, enabled ARM compilation. Various warnings issued ++ by earlier versions of GCC fixed for Cygwin and Min/GW (which both use old ++ GCCs.) ARM support is enabled by default in zlib.props (unsupported by ++ Microsoft) and ARM compilation is made possible by deleting the check for ++ x86. The test programs cannot be run because they are not signed. ++ ++Version 1.6.0beta36 [January 2, 2013] ++ Discontinued distributing libpng-1.x.x.tar.bz2. ++ Discontinued distributing libpng-1.7.0-1.6.0-diff.txt and similar. ++ Rebuilt configure with autoconf-2.69 (inadvertently not done in beta33) ++ Fixed 'make distcheck' on SUN OS - libpng.so was not being removed ++ ++Version 1.6.0beta37 [January 10, 2013] ++ Fixed conceivable but difficult to repro overflow. Also added two test ++ programs to generate and test a PNG which should have the problem. ++ ++Version 1.6.0beta39 [January 19, 2013] ++ Again corrected attempt at overflow detection in png_set_unknown_chunks() ++ (CVE-2013-7353). Added overflow detection in png_set_sPLT() and ++ png_set_text_2() (CVE-2013-7354). ++ ++Version 1.6.0beta40 [January 20, 2013] ++ Use consistent handling of overflows in text, sPLT and unknown png_set_* APIs ++ ++Version 1.6.0rc01 [January 26, 2013] ++ No changes. ++ ++Version 1.6.0rc02 [February 4, 2013] ++ Added png_get_palette_max() function. ++ ++Version 1.6.0rc03 [February 5, 2013] ++ Fixed the png_get_palette_max API. ++ ++Version 1.6.0rc04 [February 7, 2013] ++ Turn serial tests back on (recently turned off by autotools upgrade). ++ ++Version 1.6.0rc05 [February 8, 2013] ++ Update manual about png_get_palette_max(). ++ ++Version 1.6.0rc06 [February 9, 2013] ++ Fixed missing dependency in --prefix builds The intermediate ++ internal 'prefix.h' file can only be generated correctly after ++ pnglibconf.h, however the dependency was not in Makefile.am. The ++ symptoms are unpredictable depending on the order make chooses to ++ build pngprefix.h and pnglibconf.h, often the error goes unnoticed ++ because there is a system pnglibconf.h to use instead. ++ ++Version 1.6.0rc07 [February 10, 2013] ++ Enclosed the new png_get_palette_max in #ifdef PNG_GET_PALETTE_MAX_SUPPORTED ++ block, and revised pnglibconf.h and pnglibconf.h.prebuilt accordingly. ++ ++Version 1.6.0rc08 [February 10, 2013] ++ Fix typo in png.h #ifdef ++ ++Version 1.6.0 [February 14, 2013] ++ No changes. ++ ++Version 1.6.1beta01 [February 16, 2013] ++ Made symbol prefixing work with the ARM neon optimizations. Also allow ++ pngpriv.h to be included for preprocessor definitions only, so it can ++ be used in non-C/C++ files. Back ported from libpng 1.7. ++ Made sRGB check numbers consistent. ++ Ported libpng 1.5 options.awk/dfn file handling to 1.6, fixed one bug. ++ Removed cc -E workround, corrected png_get_palette_max API Tested on ++ SUN OS cc 5.9, which demonstrates the tokenization problem previously ++ avoided by using /lib/cpp. Since all .dfn output is now protected in ++ double quotes unless it is to be macro substituted the fix should ++ work everywhere. ++ Enabled parallel tests - back ported from libpng-1.7. ++ scripts/pnglibconf.dfa formatting improvements back ported from libpng17. ++ Fixed a race condition in the creation of the build 'scripts' directory ++ while building with a parallel make. ++ Use approved/supported Android method to check for NEON, use Linux/POSIX ++ 1003.1 API to check /proc/self/auxv avoiding buffer allocation and other ++ library calls (ported from libpng15). ++ ++Version 1.6.1beta02 [February 19, 2013] ++ Use parentheses more consistently in "#if defined(MACRO)" tests. ++ Folded long lines. ++ Reenabled code to allow zero length PLTE chunks for MNG. ++ ++Version 1.6.1beta03 [February 22, 2013] ++ Fixed ALIGNED_MEMORY support. ++ Added a new configure option: ++ --enable-arm-neon=always will stop the run-time checks. New checks ++ within arm/arm_init.c will cause the code not to be compiled unless ++ __ARM_NEON__ is set. This should make it fail safe (if someone asks ++ for it on then the build will fail if it can't be done.) ++ Updated the INSTALL document. ++ ++Version 1.6.1beta04 [February 27, 2013] ++ Revised INSTALL to recommend using CPPFLAGS instead of INCLUDES. ++ Revised scripts/makefile.freebsd to respect ZLIBLIB and ZLIBINC. ++ Revised scripts/dfn.awk to work with the buggy MSYS awk that has trouble ++ with CRLF line endings. ++ ++Version 1.6.1beta05 [March 1, 2013] ++ Avoid a possible memory leak in contrib/gregbook/readpng.c ++ ++Version 1.6.1beta06 [March 4, 2013] ++ Better documentation of unknown handling API interactions. ++ Corrected Android builds and corrected libpng.vers with symbol ++ prefixing. It also makes those tests compile and link on Android. ++ Added an API png_set_option() to set optimization options externally, ++ providing an alternative and general solution for the non-portable ++ run-time tests used by the ARM Neon code, using the PNG_ARM_NEON option. ++ The order of settings vs options in pnglibconf.h is reversed to allow ++ settings to depend on options and options can now set (or override) the ++ defaults for settings. ++ ++Version 1.6.1beta07 [March 7, 2013] ++ Corrected simplified API default gamma for color-mapped output, added ++ a flag to change default. In 1.6.0 when the simplified API was used ++ to produce color-mapped output from an input image with no gamma ++ information the gamma assumed for the input could be different from ++ that assumed for non-color-mapped output. In particular 16-bit depth ++ input files were assumed to be sRGB encoded, whereas in the 'direct' ++ case they were assumed to have linear data. This was an error. The ++ fix makes the simplified API treat all input files the same way and ++ adds a new flag to the png_image::flags member to allow the ++ application/user to specify that 16-bit files contain sRGB data ++ rather than the default linear. ++ Fixed bugs in the pngpixel and makepng test programs. ++ ++Version 1.6.1beta08 [March 7, 2013] ++ Fixed CMakelists.txt to allow building a single variant of the library ++ (Claudio Bley): ++ Introduced a PNG_LIB_TARGETS variable that lists all activated library ++ targets. It is an error if this variable ends up empty, ie. you have ++ to build at least one library variant. ++ Made the *_COPY targets only depend on library targets actually being build. ++ Use PNG_LIB_TARGETS to unify a code path. ++ Changed the CREATE_SYMLINK macro to expect the full path to a file as the ++ first argument. When symlinking the filename component of that path is ++ determined and used as the link target. ++ Use copy_if_different in the CREATE_SYMLINK macro. ++ ++Version 1.6.1beta09 [March 13, 2013] ++ Eliminated two warnings from the Intel C compiler. The warnings are ++ technically valid, although a reasonable treatment of division would ++ show it to be incorrect. ++ ++Version 1.6.1rc01 [March 21, 2013] ++ No changes. ++ ++Version 1.6.1 [March 28, 2013] ++ No changes. ++ ++Version 1.6.2beta01 [April 14, 2013] ++ Updated documentation of 1.5.x to 1.6.x changes in iCCP chunk handling. ++ Fixed incorrect warning of excess deflate data. End condition - the ++ warning would be produced if the end of the deflate stream wasn't read ++ in the last row. The warning is harmless. ++ Corrected the test on user transform changes on read. It was in the ++ png_set of the transform function, but that doesn't matter unless the ++ transform function changes the rowbuf size, and that is only valid if ++ transform_info is called. ++ Corrected a misplaced closing bracket in contrib/libtests/pngvalid.c ++ (Flavio Medeiros). ++ Corrected length written to uncompressed iTXt chunks (Samuli Suominen). ++ Bug was introduced in libpng-1.6.0. ++ ++Version 1.6.2rc01 [April 18, 2013] ++ Added contrib/tools/fixitxt.c, to repair the erroneous iTXt chunk length ++ written by libpng-1.6.0 and 1.6.1. ++ Disallow storing sRGB information when the sRGB is not supported. ++ ++Version 1.6.2rc02 [April 18, 2013] ++ Merge pngtest.c with libpng-1.7.0 ++ ++Version 1.6.2rc03 [April 22, 2013] ++ Trivial spelling cleanup. ++ ++Version 1.6.2rc04 and 1.6.2rc05 [omitted] ++ ++Version 1.6.2rc06 [April 24, 2013] ++ Reverted to version 1.6.2rc03. Recent changes to arm/neon support ++ have been ported to libpng-1.7.0beta09 and will reappear in version ++ 1.6.3beta01. ++ ++Version 1.6.2 [April 25, 2013] ++ No changes. ++ ++Version 1.6.3beta01 [April 25, 2013] ++ Revised stack marking in arm/filter_neon.S and configure.ac. ++ Ensure that NEON filter stuff is completely disabled when switched 'off'. ++ Previously the ARM NEON specific files were still built if the option ++ was switched 'off' as opposed to being explicitly disabled. ++ ++Version 1.6.3beta02 [April 26, 2013] ++ Test for 'arm*' not just 'arm' in the host_cpu configure variable. ++ Rebuilt the configure scripts. ++ ++Version 1.6.3beta03 [April 30, 2013] ++ Expanded manual paragraph about writing private chunks, particularly ++ the need to call png_set_keep_unknown_chunks() when writing them. ++ Avoid dereferencing NULL pointer possibly returned from ++ png_create_write_struct() (Andrew Church). ++ ++Version 1.6.3beta05 [May 9, 2013] ++ Calculate our own zlib windowBits when decoding rather than trusting the ++ CMF bytes in the PNG datastream. ++ Added an option to force maximum window size for inflating, which was ++ the behavior of libpng15 and earlier, via a new PNG_MAXIMUM_INFLATE_WINDOW ++ option for png_set_options(). ++ Added png-fix-itxt and png-fix-too-far-back to the built programs and ++ removed warnings from the source code and timepng that are revealed as ++ a result. ++ Detect wrong libpng versions linked to png-fix-too-far-back, which currently ++ only works with libpng versions that can be made to reliably fail when ++ the deflate data contains an out-of-window reference. This means only ++ 1.6 and later. ++ Fixed gnu issues: g++ needs a static_cast, gcc 4.4.7 has a broken warning ++ message which it is easier to work round than ignore. ++ Updated contrib/pngminus/pnm2png.c (Paul Stewart): ++ Check for EOF ++ Ignore "#" delimited comments in input file to pnm2png.c. ++ Fixed whitespace handling ++ Added a call to png_set_packing() ++ Initialize dimension values so if sscanf fails at least we have known ++ invalid values. ++ Attempt to detect configuration issues with png-fix-too-far-back, which ++ requires both the correct libpng and the correct zlib to function ++ correctly. ++ Check ZLIB_VERNUM for mismatches, enclose #error in quotes ++ Added information in the documentation about problems with and fixes for ++ the bad CRC and bad iTXt chunk situations. ++ ++Version 1.6.3beta06 [May 12, 2013] ++ Allow contrib/pngminus/pnm2png.c to compile without WRITE_INVERT and ++ WRITE_PACK supported (writes error message that it can't read P1 or ++ P4 PBM files). ++ Improved png-fix-too-far-back usage message, added --suffix option. ++ Revised contrib/pngminim/*/makefile to generate pnglibconf.h with the ++ right zlib header files. ++ Separated CPPFLAGS and CFLAGS in contrib/pngminim/*/makefile ++ ++Version 1.6.3beta07 [June 8, 2013] ++ Removed a redundant test in png_set_IHDR(). ++ Added set(CMAKE_CONFIGURATION_TYPES ...) to CMakeLists.txt (Andrew Hundt) ++ Deleted set(CMAKE_BUILD_TYPE) block from CMakeLists.txt ++ Enclose the prototypes for the simplified write API in ++ #ifdef PNG_STDIO_SUPPORTED/#endif ++ Make ARM NEON support work at compile time (not just configure time). ++ This moves the test on __ARM_NEON__ into pngconf.h to avoid issues when ++ using a compiler that compiles for multiple architectures at one time. ++ Removed PNG_FILTER_OPTIMIZATIONS and PNG_ARM_NEON_SUPPORTED from ++ pnglibconf.h, allowing more of the decisions to be made internally ++ (pngpriv.h) during the compile. Without this, symbol prefixing is broken ++ under certain circumstances on ARM platforms. Now only the API parts of ++ the optimizations ('check' vs 'api') are exposed in the public header files ++ except that the new setting PNG_ARM_NEON_OPT documents how libpng makes the ++ decision about whether or not to use the optimizations. ++ Protect symbol prefixing against CC/CPPFLAGS/CFLAGS usage. ++ Previous iOS/Xcode fixes for the ARM NEON optimizations moved the test ++ on __ARM_NEON__ from configure time to compile time. This breaks symbol ++ prefixing because the definition of the special png_init_filter_functions ++ call was hidden at configure time if the relevant compiler arguments are ++ passed in CFLAGS as opposed to CC. This change attempts to avoid all ++ the confusion that would result by declaring the init function even when ++ it is not used, so that it will always get prefixed. ++ ++Version 1.6.3beta08 [June 18, 2013] ++ Revised libpng.3 so that "doclifter" can process it. ++ ++Version 1.6.3beta09 [June 27, 2013] ++ Revised example.c to illustrate use of PNG_DEFAULT_sRGB and PNG_GAMMA_MAC_18 ++ as parameters for png_set_gamma(). These have been available since ++ libpng-1.5.4. ++ Renamed contrib/tools/png-fix-too-far-back.c to pngfix.c and revised it ++ to check all compressed chunks known to libpng. ++ ++Version 1.6.3beta10 [July 5, 2013] ++ Updated documentation to show default behavior of benign errors correctly. ++ Only compile ARM code when PNG_READ_SUPPORTED is defined. ++ Fixed undefined behavior in contrib/tools/pngfix.c and added new strip ++ option. pngfix relied on undefined behavior and even a simple change from ++ gcc to g++ caused it to fail. The new strip option 'unsafe' has been ++ implemented and is the default if --max is given. Option names have ++ been clarified, with --strip=transform now stripping the bKGD chunk, ++ which was stripped previously with --strip=unused. ++ Added all documented chunk types to pngpriv.h ++ Unified pngfix.c source with libpng17. ++ ++Version 1.6.3rc01 [July 11, 2013] ++ No changes. ++ ++Version 1.6.3 [July 18, 2013] ++ Revised manual about changes in iTXt chunk handling made in libpng-1.6.0. ++ Added "/* SAFE */" comments in pngrutil.c and pngrtran.c where warnings ++ may be erroneously issued by code-checking applications. ++ ++Version 1.6.4beta01 [August 21, 2013] ++ Added information about png_set_options() to the manual. ++ Delay calling png_init_filter_functions() until a row with nonzero filter ++ is found. ++ ++Version 1.6.4beta02 [August 30, 2013] ++ Fixed inconsistent conditional compilation of png_chunk_unknown_handling() ++ prototype, definition, and usage. Made it depend on ++ PNG_HANDLE_AS_UNKNOWN_SUPPORTED everywhere. ++ ++Version 1.6.4rc01 [September 5, 2013] ++ No changes. ++ ++Version 1.6.4 [September 12, 2013] ++ No changes. ++ ++Version 1.6.5 [September 14, 2013] ++ Removed two stray lines of code from arm/arm_init.c. ++ ++Version 1.6.6 [September 16, 2013] ++ Removed two stray lines of code from arm/arm_init.c, again. ++ ++Version 1.6.7beta01 [September 30, 2013] ++ Revised unknown chunk code to correct several bugs in the NO_SAVE_/NO_WRITE ++ combination ++ Allow HANDLE_AS_UNKNOWN to work when other options are configured off. Also ++ fixed the pngminim makefiles to work when $(MAKEFLAGS) contains stuff ++ which terminates the make options (as by default in recent versions of ++ Gentoo). ++ Avoid up-cast warnings in pngvalid.c. On ARM the alignment requirements of ++ png_modifier are greater than that of png_store and as a consequence ++ compilation of pngvalid.c results in a warning about increased alignment ++ requirements because of the bare cast to (png_modifier*). The code is safe, ++ because the pointer is known to point to a stack allocated png_modifier, ++ but this change avoids the warning. ++ Fixed default behavior of ARM_NEON_API. If the ARM NEON API option was ++ compiled without the CHECK option it defaulted to on, not off. ++ Check user callback behavior in pngunknown.c. Previous versions compiled ++ if SAVE_UNKNOWN was not available but did nothing since the callback ++ was never implemented. ++ Merged pngunknown.c with 1.7 version and back ported 1.7 improvements/fixes ++ ++Version 1.6.7beta02 [October 12, 2013] ++ Made changes for compatibility with automake 1.14: ++ 1) Added the 'compile' program to the list of programs that must be cleaned ++ in autogen.sh ++ 2) Added 'subdir-objects' which causes .c files in sub-directories to be ++ compiled such that the corresponding .o files are also in the ++ sub-directory. This is because automake 1.14 warns that the ++ current behavior of compiling to the top level directory may be removed ++ in the future. ++ 3) Updated dependencies on pnglibconf.h to match the new .o locations and ++ added all the files in contrib/libtests and contrib/tools that depend ++ on pnglibconf.h ++ 4) Added 'BUILD_SOURCES = pnglibconf.h'; this is the automake recommended ++ way of handling the dependencies of sources that are machine generated; ++ unfortunately it only works if the user does 'make all' or 'make check', ++ so the dependencies (3) are still required. ++ Cleaned up (char*) casts of zlib messages. The latest version of the Intel C ++ compiler complains about casting a string literal as (char*), so copied the ++ treatment of z_const from the library code into pngfix.c ++ Simplified error message code in pngunknown. The simplification has the ++ useful side effect of avoiding a bogus warning generated by the latest ++ version of the Intel C compiler (it objects to ++ condition ? string-literal : string-literal). ++ Make autogen.sh work with automake 1.13 as well as 1.14. Do this by always ++ removing the 1.14 'compile' script but never checking for it. ++ ++Version 1.6.7beta03 [October 19, 2013] ++ Added ARMv8 support (James Yu ). Added file ++ arm/filter_neon_intrinsics.c; enable with -mfpu=neon. ++ Revised pngvalid to generate size images with as many filters as it can ++ manage, limited by the number of rows. ++ Cleaned up ARM NEON compilation handling. The tests are now in pngpriv.h ++ and detect the broken GCC compilers. ++ ++Version 1.6.7beta04 [October 26, 2013] ++ Allow clang derived from older GCC versions to use ARM intrinsics. This ++ causes all clang builds that use -mfpu=neon to use the intrinsics code, ++ not the assembler code. This has only been tested on iOS 7. It may be ++ necessary to exclude some earlier clang versions but this seems unlikely. ++ Changed NEON implementation selection mechanism. This allows assembler ++ or intrinsics to be turned on at compile time during the build by defining ++ PNG_ARM_NEON_IMPLEMENTATION to the correct value (2 or 1). This macro ++ is undefined by default and the build type is selected in pngpriv.h. ++ ++Version 1.6.7rc01 [November 2, 2013] ++ No changes. ++ ++Version 1.6.7rc02 [November 7, 2013] ++ Fixed #include in filter_neon_intrinsics.c and ctype macros. The ctype char ++ checking macros take an unsigned char argument, not a signed char. ++ ++Version 1.6.7 [November 14, 2013] ++ No changes. ++ ++Version 1.6.8beta01 [November 24, 2013] ++ Moved prototype for png_handle_unknown() in pngpriv.h outside of ++ the #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED/#endif block. ++ Added "-Wall" to CFLAGS in contrib/pngminim/*/makefile ++ Conditionally compile some unused functions reported by -Wall in ++ pngminim. ++ Fixed 'minimal' builds. Various obviously useful minimal configurations ++ don't build because of missing contrib/libtests test programs and ++ overly complex dependencies in scripts/pnglibconf.dfa. This change ++ adds contrib/conftest/*.dfa files that can be used in automatic build ++ scripts to ensure that these configurations continue to build. ++ Enabled WRITE_INVERT and WRITE_PACK in contrib/pngminim/encoder. ++ Fixed pngvalid 'fail' function declaration on the Intel C Compiler. ++ This reverts to the previous 'static' implementation and works round ++ the 'unused static function' warning by using PNG_UNUSED(). ++ ++Version 1.6.8beta02 [November 30, 2013] ++ Removed or marked PNG_UNUSED some harmless "dead assignments" reported ++ by clang scan-build. ++ Changed tabs to 3 spaces in png_debug macros and changed '"%s"m' ++ to '"%s" m' to improve portability among compilers. ++ Changed png_free_default() to free() in pngtest.c ++ ++Version 1.6.8rc01 [December 12, 2013] ++ Tidied up pngfix inits and fixed pngtest no-write builds. ++ ++Version 1.6.8rc02 [December 14, 2013] ++ Handle zero-length PLTE chunk or NULL palette with png_error() ++ instead of png_chunk_report(), which by default issues a warning ++ rather than an error, leading to later reading from a NULL pointer ++ (png_ptr->palette) in png_do_expand_palette(). This is CVE-2013-6954 ++ and VU#650142. Libpng-1.6.1 through 1.6.7 are vulnerable. ++ Libpng-1.6.0 and earlier do not have this bug. ++ ++Version 1.6.8 [December 19, 2013] ++ No changes. ++ ++Version 1.6.9beta01 [December 26, 2013] ++ Bookkeeping: Moved functions around (no changes). Moved transform ++ function definitions before the place where they are called so that ++ they can be made static. Move the intrapixel functions and the ++ grayscale palette builder out of the png?tran.c files. The latter ++ isn't a transform function and is no longer used internally, and the ++ former MNG specific functions are better placed in pngread/pngwrite.c ++ Made transform implementation functions static. This makes the internal ++ functions called by png_do_{read|write}_transformations static. On an ++ x86-64 DLL build (Gentoo Linux) this reduces the size of the text ++ segment of the DLL by 1208 bytes, about 0.6%. It also simplifies ++ maintenance by removing the declarations from pngpriv.h and allowing ++ easier changes to the internal interfaces. ++ Rebuilt configure scripts with automake-1.14.1 and autoconf-2.69 ++ in the tar distributions. ++ ++Version 1.6.9beta02 [January 1, 2014] ++ Added checks for libpng 1.5 to pngvalid.c. This supports the use of ++ this version of pngvalid in libpng 1.5 ++ Merged with pngvalid.c from libpng-1.7 changes to create a single ++ pngvalid.c ++ Removed #error macro from contrib/tools/pngfix.c (Thomas Klausner). ++ Merged pngrio.c, pngtrans.c, pngwio.c, and pngerror.c with libpng-1.7.0 ++ Merged libpng-1.7.0 changes to make no-interlace configurations work ++ with test programs. ++ Revised pngvalid.c to support libpng 1.5, which does not support the ++ PNG_MAXIMUM_INFLATE_WINDOW option, so #define it out when appropriate in ++ pngvalid.c ++ Allow unversioned links created on install to be disabled in configure. ++ In configure builds 'make install' changes/adds links like png.h ++ and libpng.a to point to the newly installed, versioned, files (e.g. ++ libpng17/png.h and libpng17.a). Three new configure options and some ++ rearrangement of Makefile.am allow creation of these links to be disabled. ++ ++Version 1.6.9beta03 [January 10, 2014] ++ Removed potentially misleading warning from png_check_IHDR(). ++ ++Version 1.6.9beta04 [January 20, 2014] ++ Updated scripts/makefile.* to use CPPFLAGS (Cosmin). ++ Added clang attribute support (Cosmin). ++ ++Version 1.6.9rc01 [January 28, 2014] ++ No changes. ++ ++Version 1.6.9rc02 [January 30, 2014] ++ Quiet an uninitialized memory warning from VC2013 in png_get_png(). ++ ++Version 1.6.9 [February 6, 2014] ++ ++Version 1.6.10beta01 [February 9, 2014] ++ Backported changes from libpng-1.7.0beta30 and beta31: ++ Fixed a large number of instances where PNGCBAPI was omitted from ++ function definitions. ++ Added pngimage test program for png_read_png() and png_write_png() ++ with two new test scripts. ++ Removed dependence on !PNG_READ_EXPAND_SUPPORTED for calling ++ png_set_packing() in png_read_png(). ++ Fixed combination of ~alpha with shift. On read invert alpha, processing ++ occurred after shift processing, which causes the final values to be ++ outside the range that should be produced by the shift. Reversing the ++ order on read makes the two transforms work together correctly and mirrors ++ the order used on write. ++ Do not read invalid sBIT chunks. Previously libpng only checked sBIT ++ values on write, so a malicious PNG writer could therefore cause ++ the read code to return an invalid sBIT chunk, which might lead to ++ application errors or crashes. Such chunks are now skipped (with ++ chunk_benign_error). ++ Make png_read_png() and png_write_png() prototypes in png.h depend ++ upon PNG_READ_SUPPORTED and PNG_WRITE_SUPPORTED. ++ Support builds with unsupported PNG_TRANSFORM_* values. All of the ++ PNG_TRANSFORM_* values are always defined in png.h and, because they ++ are used for both read and write in some cases, it is not reliable ++ to #if out ones that are totally unsupported. This change adds error ++ detection in png_read_image() and png_write_image() to do a ++ png_app_error() if the app requests something that cannot be done ++ and it adds corresponding code to pngimage.c to handle such options ++ by not attempting to test them. ++ ++Version 1.6.10beta02 [February 23, 2014] ++ Moved redefines of png_error(), png_warning(), png_chunk_error(), ++ and png_chunk_warning() from pngpriv.h to png.h to make them visible ++ to libpng-calling applications. ++ Moved OS dependent code from arm/arm_init.c, to allow the included ++ implementation of the ARM NEON discovery function to be set at ++ build-time and provide sample implementations from the current code in the ++ contrib/arm-neon subdirectory. The __linux__ code has also been changed to ++ compile and link on Android by using /proc/cpuinfo, and the old linux code ++ is in contrib/arm-neon/linux-auxv.c. The new code avoids POSIX and Linux ++ dependencies apart from opening /proc/cpuinfo and is C90 compliant. ++ Check for info_ptr == NULL early in png_read_end() so we don't need to ++ run all the png_handle_*() and depend on them to return if info_ptr == NULL. ++ This improves the performance of png_read_end(png_ptr, NULL) and makes ++ it more robust against future programming errors. ++ Check for __has_extension before using it in pngconf.h, to ++ support older Clang versions (Jeremy Sequoia). ++ Treat CRC error handling with png_set_crc_action(), instead of with ++ png_set_benign_errors(), which has been the case since libpng-1.6.0beta18. ++ Use a user warning handler in contrib/gregbook/readpng2.c instead of default, ++ so warnings will be put on stderr even if libpng has CONSOLE_IO disabled. ++ Added png_ptr->process_mode = PNG_READ_IDAT_MODE in png_push_read_chunk ++ after recognizing the IDAT chunk, which avoids an infinite loop while ++ reading a datastream whose first IDAT chunk is of zero-length. ++ This fixes CERT VU#684412 and CVE-2014-0333. ++ Don't recognize known sRGB profiles as sRGB if they have been hacked, ++ but don't reject them and don't issue a copyright violation warning. ++ ++Version 1.6.10beta03 [February 25, 2014] ++ Moved some documentation from png.h to libpng.3 and libpng-manual.txt ++ Minor editing of contrib/arm-neon/README and contrib/examples/*.c ++ ++Version 1.6.10rc01 [February 27, 2014] ++ Fixed typos in the manual and in scripts/pnglibconf.dfa (CFLAGS -> CPPFLAGS ++ and PNG_USR_CONFIG -> PNG_USER_CONFIG). ++ ++Version 1.6.10rc02 [February 28, 2014] ++ Removed unreachable return statement after png_chunk_error() ++ in pngrutil.c ++ ++Version 1.6.10rc03 [March 4, 2014] ++ Un-deprecated png_data_freer(). ++ ++Version 1.6.10 [March 6, 2014] ++ No changes. ++ ++Version 1.6.11beta01 [March 17, 2014] ++ Use "if (value != 0)" instead of "if (value)" consistently. ++ Changed ZlibSrcDir from 1.2.5 to 1.2.8 in projects/vstudio. ++ Moved configuration information from the manual to the INSTALL file. ++ ++Version 1.6.11beta02 [April 6, 2014] ++ Removed #if/#else/#endif from inside two pow() calls in pngvalid.c because ++ they were handled improperly by Portland Group's PGI-14.1 - PGI-14.3 ++ when using its "__builtin_pow()" function. ++ Silence 'unused parameter' build warnings (Cosmin Truta). ++ $(CP) is now used alongside $(RM_F). Also, use 'copy' instead of 'cp' ++ where applicable, and applied other minor makefile changes (Cosmin). ++ Don't warn about invalid dimensions exceeding user limits (Cosmin). ++ Allow an easy replacement of the default pre-built configuration ++ header with a custom header, via the make PNGLIBCONF_H_PREBUILT ++ macro (Cosmin). ++ ++Version 1.6.11beta03 [April 6, 2014] ++ Fixed a typo in pngrutil.c, introduced in libpng-1.5.6, that interferes ++ with "blocky" expansion of sub-8-bit interlaced PNG files (Eric Huss). ++ Optionally use __builtin_bswap16() in png_do_swap(). ++ ++Version 1.6.11beta04 [April 19, 2014] ++ Made progressive reading of interlaced images consistent with the ++ behavior of the sequential reader and consistent with the manual, by ++ moving some code out of the PNG_READ_INTERLACING_SUPPORTED blocks. The ++ row_callback now receives the proper pass number and unexpanded rows, when ++ png_combine_row() isn't built or used, and png_set_interlace_handling() ++ is not called. ++ Allow PNG_sRGB_PROFILE_CHECKING = (-1) to mean no sRGB profile checking. ++ ++Version 1.6.11beta05 [April 26, 2014] ++ Do not reject ICC V2 profiles that lack padding (Kai-Uwe Behrmann). ++ Relocated closing bracket of the sRGB profile test loop to avoid getting ++ "Not recognizing known sRGB profile that has been edited" warning for ++ ICC V2 profiles that lack the MD5 signature in the profile header. ++ ++Version 1.6.11beta06 [May 19, 2014] ++ Added PNG_SKIP_sRGB_CHECK_PROFILE choice for png_set_option(). ++ ++Version 1.6.11rc01 [May 27, 2014] ++ No changes. ++ ++Version 1.6.11rc02 [June 3, 2014] ++ Test ZLIB_VERNUM instead of PNG_ZLIB_VERNUM in contrib/tools/pngfix.c ++ ++Version 1.6.11 [June 5, 2014] ++ No changes. ++ ++Version 1.6.12rc01 [June 6, 2014] ++ Relocated new code from 1.6.11beta06 in png.c to a point after the ++ declarations (Max Stepin). ++ ++Version 1.6.12rc02 [June 7, 2014] ++ Changed file permissions of contrib/tools/intgamma.sh, ++ test-driver, and compile from 0644 to 0755 (Cosmin). ++ ++Version 1.6.12rc03 [June 8, 2014] ++ Ensure "__has_attribute()" macro exists before trying to use it with ++ old clang compilers (MacPorts Ticket #43939). ++ ++Version 1.6.12 [June 12, 2014] ++ No changes. ++ ++Version 1.6.13beta01 [July 4, 2014] ++ Quieted -Wsign-compare and -Wclobber compiler warnings in ++ contrib/pngminus/*.c ++ Added "(void) png_ptr;" where needed in contrib/gregbook to quiet ++ compiler complaints about unused pointers. ++ Split a long output string in contrib/gregbook/rpng2-x.c. ++ Added "PNG_SET_OPTION" requirement for sRGB chunk support to pnglibconf.dfa, ++ Needed for write-only support (John Bowler). ++ Changed "if defined(__ARM_NEON__)" to ++ "if (defined(__ARM_NEON__) || defined(__ARM_NEON))" (James Wu). ++ Fixed clang no-warning builds: png_digit was defined but never used. ++ ++Version 1.6.13beta02 [July 21, 2014] ++ Fixed an incorrect separator ("/" should be "\") in scripts/makefile.vcwin32 ++ (bug report from Wolfgang S. Kechel). Bug was introduced in libpng-1.6.11. ++ Also fixed makefile.bc32, makefile.bor, makefile.msc, makefile.intel, and ++ makefile.tc3 similarly. ++ ++Version 1.6.13beta03 [August 3, 2014] ++ Removed scripts/makefile.elf. It has not worked since libpng-1.5.0beta14 ++ due to elimination of the PNG_FUNCTION_EXPORT and PNG_DATA_EXPORT ++ definitions from pngconf.h. ++ Ensure that CMakeLists.txt makes the target "lib" directory before making ++ symbolic link into it (SourceForge bug report #226 by Rolf Timmermans). ++ ++Version 1.6.13beta04 [August 8, 2014] ++ Added opinion that the ECCN (Export Control Classification Number) for ++ libpng is EAR99 to the README file. ++ Eliminated use of "$<" in makefile explicit rules, when copying ++ $PNGLIBCONF_H_PREBUILT. This does not work on some versions of make; ++ bug introduced in libpng version 1.6.11. ++ ++Version 1.6.13rc01 [August 14, 2014] ++ Made "ccopts" agree with "CFLAGS" in scripts/makefile.hp* and makefile.*sunu ++ ++Version 1.6.13 [August 21, 2014] ++ No changes. ++ ++Version 1.6.14beta01 [September 14, 2014] ++ Guard usage of png_ptr->options with #ifdef PNG_SET_OPTION_SUPPORTED. ++ Do not build contrib/tools/pngfix.c when PNG_SETJMP_NOT_SUPPORTED, ++ to allow "make" to complete without setjmp support (bug report by ++ Claudio Fontana) ++ Add "#include " to contrib/tools/pngfix.c (John Bowler) ++ ++Version 1.6.14beta02 [September 18, 2014] ++ Use nanosleep() instead of usleep() in contrib/gregbook/rpng2-x.c ++ because usleep() is deprecated. ++ Define usleep() in contrib/gregbook/rpng2-x.c if not already defined ++ in unistd.h and nanosleep() is not available; fixes error introduced ++ in libpng-1.6.13. ++ Disable floating point exception handling in pngvalid.c when ++ PNG_FLOATING_ARITHMETIC is not supported (bug report by "zootus ++ at users.sourceforge.net"). ++ ++Version 1.6.14beta03 [September 19, 2014] ++ Define FE_DIVBYZERO, FE_INVALID, and FE_OVERFLOW in pngvalid.c if not ++ already defined. Revert floating point exception handling in pngvalid.c ++ to version 1.6.14beta01 behavior. ++ ++Version 1.6.14beta04 [September 27, 2014] ++ Fixed incorrect handling of the iTXt compression flag in pngrutil.c ++ (bug report by Shunsaku Hirata). Bug was introduced in libpng-1.6.0. ++ ++Version 1.6.14beta05 [October 1, 2014] ++ Added "option READ_iCCP enables READ_COMPRESSED_TEXT" to pnglibconf.dfa ++ ++Version 1.6.14beta06 [October 5, 2014] ++ Removed unused "text_len" parameter from private function png_write_zTXt(). ++ Conditionally compile some code in png_deflate_claim(), when ++ PNG_WARNINGS_SUPPORTED and PNG_ERROR_TEXT_SUPPORTED are disabled. ++ Replaced repeated code in pngpread.c with PNG_PUSH_SAVE_BUFFER_IF_FULL. ++ Added "chunk iTXt enables TEXT" and "chunk zTXt enables TEXT" ++ to pnglibconf.dfa. ++ Removed "option READ_COMPRESSED_TEXT enables READ_TEXT" from pnglibconf.dfa, ++ to make it possible to configure a libpng that supports iCCP but not TEXT. ++ ++Version 1.6.14beta07 [October 7, 2014] ++ Removed "option WRITE_COMPRESSED_TEXT enables WRITE_TEXT" from pnglibconf.dfa ++ Only mark text chunks as written after successfully writing them. ++ ++Version 1.6.14rc01 [October 15, 2014] ++ Fixed some typos in comments. ++ ++Version 1.6.14rc02 [October 17, 2014] ++ Changed png_convert_to_rfc_1123() to png_convert_to_rfc_1123_buffer() ++ in the manual, to reflect the change made in libpng-1.6.0. ++ Updated README file to explain that direct access to the png_struct ++ and info_struct members has not been permitted since libpng-1.5.0. ++ ++Version 1.6.14 [October 23, 2014] ++ No changes. ++ ++Version 1.6.15beta01 [October 29, 2014] ++ Changed "if (!x)" to "if (x == 0)" and "if (x)" to "if (x != 0)" ++ Simplified png_free_data(). ++ Added missing "ptr = NULL" after some instances of png_free(). ++ ++Version 1.6.15beta02 [November 1, 2014] ++ Changed remaining "if (!x)" to "if (x == 0)" and "if (x)" to "if (x != 0)" ++ ++Version 1.6.15beta03 [November 3, 2014] ++ Added PNG_USE_ARM_NEON configuration flag (Marcin Juszkiewicz). ++ ++Version 1.6.15beta04 [November 4, 2014] ++ Removed new PNG_USE_ARM_NEON configuration flag and made a one-line ++ revision to configure.ac to support ARM on aarch64 instead (John Bowler). ++ ++Version 1.6.15beta05 [November 5, 2014] ++ Use png_get_libpng_ver(NULL) instead of PNG_LIBPNG_VER_STRING in ++ example.c, pngtest.c, and applications in the contrib directory. ++ Fixed an out-of-range read in png_user_version_check() (Bug report from ++ Qixue Xiao, CVE-2015-8540). ++ Simplified and future-proofed png_user_version_check(). ++ Fixed GCC unsigned int->float warnings. Various versions of GCC ++ seem to generate warnings when an unsigned value is implicitly ++ converted to double. This is probably a GCC bug but this change ++ avoids the issue by explicitly converting to (int) where safe. ++ Free all allocated memory in pngimage. The file buffer cache was left ++ allocated at the end of the program, harmless but it causes memory ++ leak reports from clang. ++ Fixed array size calculations to avoid warnings. At various points ++ in the code the number of elements in an array is calculated using ++ sizeof. This generates a compile time constant of type (size_t) which ++ is then typically assigned to an (unsigned int) or (int). Some versions ++ of GCC on 64-bit systems warn about the apparent narrowing, even though ++ the same compiler does apparently generate the correct, in-range, ++ numeric constant. This adds appropriate, safe, casts to make the ++ warnings go away. ++ ++Version 1.6.15beta06 [November 6, 2014] ++ Reverted use png_get_libpng_ver(NULL) instead of PNG_LIBPNG_VER_STRING ++ in the manual, example.c, pngtest.c, and applications in the contrib ++ directory. It was incorrect advice. ++ ++Version 1.6.15beta07 [November 7, 2014] ++ Removed #ifdef PNG_16BIT_SUPPORTED/#endif around png_product2(); it is ++ needed by png_reciprocal2(). ++ Added #ifdef PNG_16BIT_SUPPORTED/#endif around png_log16bit() and ++ png_do_swap(). ++ Changed all "#endif /* PNG_FEATURE_SUPPORTED */" to "#endif /* FEATURE */" ++ ++Version 1.6.15beta08 [November 8, 2014] ++ More housecleaning in *.h ++ ++Version 1.6.15rc01 [November 13, 2014] ++ ++Version 1.6.15rc02 [November 14, 2014] ++ The macros passed in the command line to Borland make were ignored if ++ similarly-named macros were already defined in makefiles. This behavior ++ is different from POSIX make and other make programs. Surround the ++ macro definitions with ifndef guards (Cosmin). ++ ++Version 1.6.15rc03 [November 16, 2014] ++ Added "-D_CRT_SECURE_NO_WARNINGS" to CFLAGS in scripts/makefile.vcwin32. ++ Removed the obsolete $ARCH variable from scripts/makefile.darwin. ++ ++Version 1.6.15 [November 20, 2014] ++ No changes. ++ ++Version 1.6.16beta01 [December 14, 2014] ++ Added ".align 2" to arm/filter_neon.S to support old GAS assemblers that ++ don't do alignment correctly. ++ Revised Makefile.am and scripts/symbols.dfn to work with MinGW/MSYS ++ (Bob Friesenhahn). ++ ++Version 1.6.16beta02 [December 15, 2014] ++ Revised Makefile.am and scripts/*.dfn again to work with MinGW/MSYS; ++ renamed scripts/*.dfn to scripts/*.c (John Bowler). ++ ++Version 1.6.16beta03 [December 21, 2014] ++ Quiet a "comparison always true" warning in pngstest.c (John Bowler). ++ ++Version 1.6.16rc01 [December 21, 2014] ++ Restored a test on width that was removed from png.c at libpng-1.6.9 ++ (Bug report by Alex Eubanks, CVE-2015-0973). ++ ++Version 1.6.16rc02 [December 21, 2014] ++ Undid the update to pngrutil.c in 1.6.16rc01. ++ ++Version 1.6.16rc03 [December 21, 2014] ++ Fixed an overflow in png_combine_row() with very wide interlaced images ++ (Bug report and fix by John Bowler, CVE-2014-9495). ++ ++Version 1.6.16 [December 22, 2014] ++ No changes. ++ ++Version 1.6.17beta01 [January 29, 2015] ++ Removed duplicate PNG_SAFE_LIMITS_SUPPORTED handling from pngconf.h ++ Corrected the width limit calculation in png_check_IHDR(). ++ Removed user limits from pngfix. Also pass NULL pointers to ++ png_read_row to skip the unnecessary row de-interlace stuff. ++ Added testing of png_set_packing() to pngvalid.c ++ Regenerated configure scripts in the *.tar distributions with libtool-2.4.4 ++ Implement previously untested cases of libpng transforms in pngvalid.c ++ Fixed byte order in png_do_read_filler() with 16-bit input. Previously ++ the high and low bytes of the filler, from png_set_filler() or from ++ png_set_add_alpha(), were read in the wrong order. ++ Made the check for out-of-range values in png_set_tRNS() detect ++ values that are exactly 2^bit_depth, and work on 16-bit platforms. ++ Merged some parts of libpng-1.6.17beta01 and libpng-1.7.0beta47. ++ Added #ifndef __COVERITY__ where needed in png.c, pngrutil.c and ++ pngset.c to avoid warnings about dead code. ++ Added "& 0xff" to many instances of expressions that are typecast ++ to (png_byte), to avoid Coverity warnings. ++ ++Version 1.6.17beta02 [February 7, 2015] ++ Work around one more Coverity-scan dead-code warning. ++ Do not build png_product2() when it is unused. ++ ++Version 1.6.17beta03 [February 17, 2015] ++ Display user limits in the output from pngtest. ++ Eliminated the PNG_SAFE_LIMITS macro and restored the 1-million-column ++ and 1-million-row default limits in pnglibconf.dfa, that can be reset ++ by the user at build time or run time. This provides a more robust ++ defense against DOS and as-yet undiscovered overflows. ++ ++Version 1.6.17beta04 [February 21, 2015] ++ Added PNG_WRITE_CUSTOMIZE_COMPRESSION_SUPPORTED macro, on by default. ++ Allow user to call png_get_IHDR() with NULL arguments (Reuben Hawkins). ++ Rebuilt configure scripts with automake-1.15 and libtool-2.4.6 ++ ++Version 1.6.17beta05 [February 25, 2015] ++ Restored compiling of png_reciprocal2 with PNG_NO_16BIT. ++ ++Version 1.6.17beta06 [February 27, 2015] ++ Moved png_set_filter() prototype into a PNG_WRITE_SUPPORTED block ++ of png.h. ++ Avoid runtime checks when converting integer to png_byte with ++ Visual Studio (Sergey Kosarevsky) ++ ++Version 1.6.17rc01 [March 4, 2015] ++ No changes. ++ ++Version 1.6.17rc02 [March 9, 2015] ++ Removed some comments that the configure script did not handle ++ properly from scripts/pnglibconf.dfa and pnglibconf.h.prebuilt. ++ Free the unknown_chunks structure even when it contains no data. ++ ++Version 1.6.17rc03 [March 12, 2015] ++ Updated CMakeLists.txt to add OSX framework, change YES/NO to ON/OFF ++ for consistency, and remove some useless tests (Alexey Petruchik). ++ ++Version 1.6.17rc04 [March 16, 2015] ++ Remove pnglibconf.h, pnglibconf.c, and pnglibconf.out instead of ++ pnglibconf.* in "make clean" (Cosmin). ++ Fix bug in calculation of maxbits, in png_write_sBIT, introduced ++ in libpng-1.6.17beta01 (John Bowler). ++ ++Version 1.6.17rc05 [March 21, 2015] ++ Define PNG_FILTER_* and PNG_FILTER_VALUE_* in png.h even when WRITE ++ is not supported (John Bowler). This fixes an error introduced in ++ libpng-1.6.17beta06. ++ Reverted "& 0xff" additions of version 1.6.17beta01. Libpng passes ++ the Coverity scan without them. ++ ++Version 1.6.17rc06 [March 23, 2015] ++ Remove pnglibconf.dfn and pnglibconf.pre with "make clean". ++ Reformatted some "&0xff" instances to "& 0xff". ++ Fixed simplified 8-bit-linear to sRGB alpha. The calculated alpha ++ value was wrong. It's not clear if this affected the final stored ++ value; in the obvious code path the upper and lower 8-bits of the ++ alpha value were identical and the alpha was truncated to 8-bits ++ rather than dividing by 257 (John Bowler). ++ ++Version 1.6.17 [March 26, 2015] ++ No changes. ++ ++Version 1.6.18beta01 [April 1, 2015] ++ Removed PNG_SET_CHUNK_[CACHE|MALLOC]_LIMIT_SUPPORTED macros. They ++ have been combined with PNG_SET_USER_LIMITS_SUPPORTED (resolves ++ bug report by Andrew Church). ++ Fixed rgb_to_gray checks and added tRNS checks to pngvalid.c. This ++ fixes some arithmetic errors that caused some tests to fail on ++ some 32-bit platforms (Bug reports by Peter Breitenlohner [i686] ++ and Petr Gajdos [i586]). ++ ++Version 1.6.18beta02 [April 26, 2015] ++ Suppressed some warnings from the Borland C++ 5.5.1/5.82 compiler ++ (Bug report by Viktor Szakats). ++ ++Version 1.6.18beta03 [May 6, 2015] ++ Replaced "unexpected" with an integer (0xabadca11) in pngset.c ++ where a long was expected, to avoid a compiler warning when PNG_DEBUG > 1. ++ Added contrib/examples/simpleover.c, to demonstrate how to handle ++ alpha compositing of multiple images, using the "simplified API" ++ and an example PNG generation tool, contrib/examples/genpng.c ++ (John Bowler). ++ ++Version 1.6.18beta04 [May 20, 2015] ++ PNG_RELEASE_BUILD replaces tests where the code depended on the build base ++ type and can be defined on the command line, allowing testing in beta ++ builds (John Bowler). ++ Avoid Coverity issue 80858 (REVERSE NULL) in pngtest.c PNG_DEBUG builds. ++ Avoid a harmless potential integer overflow in png_XYZ_from_xy() (Bug ++ report from Christopher Ferris). ++ ++Version 1.6.18beta05 [May 31, 2015] ++ Backport filter selection code from libpng-1.7.0beta51, to combine ++ sub_row, up_row, avg_row, and paeth_row into try_row and tst_row. ++ Changed png_voidcast(), etc., to voidcast(), etc., in contrib/tools/pngfix.c ++ to avoid confusion with the libpng private macros. ++ Fixed old cut&paste bug in the weighted filter selection code in ++ pngwutil.c, introduced in libpng-0.95, March 1997. ++ ++Version 1.6.18beta06 [June 1, 2015] ++ Removed WRITE_WEIGHTED_FILTERED code, to save a few kbytes of the ++ compiled library size. It never worked properly and as far as we can ++ tell, no one uses it. The png_set_filter_heuristics() and ++ png_set_filter_heuristics_fixed() APIs are retained but deprecated ++ and do nothing. ++ ++Version 1.6.18beta07 [June 6, 2015] ++ Removed non-working progressive reader 'skip' function. This ++ function has apparently never been used. It was implemented ++ to support back-door modification of png_struct in libpng-1.4.x ++ but (because it does nothing and cannot do anything) was apparently ++ never tested (John Bowler). ++ Fixed cexcept.h in which GCC 5 now reports that one of the auto ++ variables in the Try macro needs to be volatile to prevent value ++ being lost over the setjmp (John Bowler). ++ Fixed NO_WRITE_FILTER and -Wconversion build breaks (John Bowler). ++ Fix g++ build breaks (John Bowler). ++ Quieted some Coverity issues in pngfix.c, png-fix-itxt.c, pngvalid.c, ++ pngstest.c, and pngimage.c. Most seem harmless, but png-fix-itxt ++ would only work with iTXt chunks with length 255 or less. ++ Added #ifdef's to contrib/examples programs so people don't try ++ to compile them without the minimum required support enabled ++ (suggested by Flavio Medeiros). ++ ++Version 1.6.18beta08 [June 30, 2015] ++ Eliminated the final two Coverity defects (insecure temporary file ++ handling in contrib/libtests/pngstest.c; possible overflow of ++ unsigned char in contrib/tools/png-fix-itxt.c). To use the "secure" ++ file handling, define PNG_USE_MKSTEMP, otherwise "tmpfile()" will ++ be used. ++ Removed some unused WEIGHTED_FILTER macros from png.h and pngstruct.h ++ ++Version 1.6.18beta09 [July 5, 2015] ++ Removed some useless typecasts from contrib/tools/png-fix-itxt.c ++ Fixed a new signed-unsigned comparison in pngrtran.c (Max Stepin). ++ Replaced arbitrary use of 'extern' with #define PNG_LINKAGE_*. To ++ preserve API compatibility, the new defines all default to "extern" ++ (requested by Jan Nijtmans). ++ ++Version 1.6.18rc01 [July 9, 2015] ++ Belatedly added Mans Rullgard and James Yu to the list of Contributing ++ Authors. ++ ++Version 1.6.18rc02 [July 12, 2015] ++ Restored unused FILTER_HEURISTIC macros removed at libpng-1.6.18beta08 ++ to png.h to avoid compatibility warnings. ++ ++Version 1.6.18rc03 [July 15, 2015] ++ Minor changes to the man page ++ ++Version 1.6.18 [July 23, 2015] ++ No changes. ++ ++Version 1.6.19beta01 [July 30, 2015] ++ Updated obsolete information about the simplified API macros in the ++ manual pages (Bug report by Arc Riley). ++ Avoid potentially dereferencing NULL info_ptr in png_info_init_3(). ++ Rearranged png.h to put the major sections in the same order as ++ in libpng17. ++ Eliminated unused PNG_COST_SHIFT, PNG_WEIGHT_SHIFT, PNG_COST_FACTOR, and ++ PNG_WEIGHT_FACTOR macros. ++ Suppressed some warnings from the Borland C++ 5.5.1/5.82 compiler ++ (Bug report by Viktor Szakats). Several warnings remain and are ++ unavoidable, where we test for overflow. ++ Fixed potential leak of png_pixels in contrib/pngminus/pnm2png.c ++ Fixed uninitialized variable in contrib/gregbook/rpng2-x.c ++ ++Version 1.6.19beta02 [August 19, 2015] ++ Moved config.h.in~ from the "libpng_autotools_files" list to the ++ "libpng_autotools_extra" list in autogen.sh because it was causing a ++ false positive for missing files (bug report by Robert C. Seacord). ++ Removed unreachable "break" statements in png.c, pngread.c, and pngrtran.c ++ to suppress clang warnings (Bug report by Viktor Szakats). ++ Fixed some bad links in the man page. ++ Changed "n bit" to "n-bit" in comments. ++ Added signed/unsigned 16-bit safety net. This removes the dubious ++ 0x8000 flag definitions on 16-bit systems. They aren't supported ++ yet the defs *probably* work, however it seems much safer to do this ++ and be advised if anyone, contrary to advice, is building libpng 1.6 ++ on a 16-bit system. It also adds back various switch default clauses ++ for GCC; GCC errors out if they are not present (with an appropriately ++ high level of warnings). ++ Safely convert num_bytes to a png_byte in png_set_sig_bytes() (Robert ++ Seacord). ++ Fixed the recently reported 1's complement security issue by replacing ++ the value that is illegal in the PNG spec, in both signed and unsigned ++ values, with 0. Illegal unsigned values (anything greater than or equal ++ to 0x80000000) can still pass through, but since these are not illegal ++ in ANSI-C (unlike 0x80000000 in the signed case) the checking that ++ occurs later can catch them (John Bowler). ++ ++Version 1.6.19beta03 [September 26, 2015] ++ Fixed png_save_int_32 when int is not 2's complement (John Bowler). ++ Updated libpng16 with all the recent test changes from libpng17, ++ including changes to pngvalid.c to ensure that the original, ++ distributed, version of contrib/visupng/cexcept.h can be used ++ (John Bowler). ++ pngvalid contains the correction to the use of SAVE/STORE_ ++ UNKNOWN_CHUNKS; a bug revealed by changes in libpng 1.7. More ++ tests contain the --strict option to detect warnings and the ++ pngvalid-standard test has been corrected so that it does not ++ turn on progressive-read. There is a separate test which does ++ that. (John Bowler) ++ Also made some signed/unsigned fixes. ++ Make pngstest error limits version specific. Splitting the machine ++ generated error structs out to a file allows the values to be updated ++ without changing pngstest.c itself. Since libpng 1.6 and 1.7 have ++ slightly different error limits this simplifies maintenance. The ++ makepngs.sh script has also been updated to more accurately reflect ++ current problems in libpng 1.7 (John Bowler). ++ Incorporated new test PNG files into make check. tests/pngstest-* ++ are changed so that the new test files are divided into 8 groups by ++ gamma and alpha channel. These tests have considerably better code ++ and pixel-value coverage than contrib/pngsuite; however,coverage is ++ still incomplete (John Bowler). ++ Removed the '--strict' in 1.6 because of the double-gamma-correction ++ warning, updated pngstest-errors.h for the errors detected with the ++ new contrib/testspngs PNG test files (John Bowler). ++ ++Version 1.6.19beta04 [October 15, 2015] ++ Worked around rgb-to-gray issues in libpng 1.6. The previous ++ attempts to ignore the errors in the code aren't quite enough to ++ deal with the 'channel selection' encoding added to libpng 1.7; abort. ++ pngvalid.c is changed to drop this encoding in prior versions. ++ Fixed 'pow' macros in pngvalid.c. It is legal for 'pow' to be a ++ macro, therefore the argument list cannot contain preprocessing ++ directives. Make sure pow is a function where this happens. This is ++ a minimal safe fix, the issue only arises in non-performance-critical ++ code (bug report by Curtis Leach, fix by John Bowler). ++ Added sPLT support to pngtest.c ++ ++Version 1.6.19rc01 [October 23, 2015] ++ No changes. ++ ++Version 1.6.19rc02 [October 31, 2015] ++ Prevent setting or writing over-length PLTE chunk (Cosmin Truta). ++ Silently truncate over-length PLTE chunk while reading. ++ Libpng incorrectly calculated the output rowbytes when the application ++ decreased either the number of channels or the bit depth (or both) in ++ a user transform. This was safe; libpng overallocated buffer space ++ (potentially by quite a lot; up to 4 times the amount required) but, ++ from 1.5.4 on, resulted in a png_error (John Bowler). ++ ++Version 1.6.19rc03 [November 3, 2015] ++ Fixed some inconsequential cut-and-paste typos in png_set_cHRM_XYZ_fixed(). ++ Clarified COPYRIGHT information to state explicitly that versions ++ are derived from previous versions. ++ Removed much of the long list of previous versions from png.h and ++ libpng.3. ++ ++Version 1.6.19rc04 [November 5, 2015] ++ Fixed new bug with CRC error after reading an over-length palette ++ (bug report by Cosmin Truta) (CVE-2015-8126). ++ ++Version 1.6.19 [November 12, 2015] ++ Cleaned up coding style in png_handle_PLTE(). ++ ++Version 1.6.20beta01 [November 20, 2015] ++ Avoid potential pointer overflow/underflow in png_handle_sPLT() and ++ png_handle_pCAL() (Bug report by John Regehr). ++ ++Version 1.6.20beta02 [November 23, 2015] ++ Fixed incorrect implementation of png_set_PLTE() that uses png_ptr ++ not info_ptr, that left png_set_PLTE() open to the CVE-2015-8126 ++ vulnerability. Fixes CVE-2015-8472. ++ ++Version 1.6.20beta03 [November 24, 2015] ++ Backported tests from libpng-1.7.0beta69. ++ ++Version 1.6.20rc01 [November 26, 2015] ++ Fixed an error in handling of bad zlib CMINFO field in pngfix, found by ++ American Fuzzy Lop, reported by Brian Carpenter. inflate() doesn't ++ immediately fault a bad CMINFO field; instead a 'too far back' error ++ happens later (at least some times). pngfix failed to limit CMINFO to ++ the allowed values but then assumed that window_bits was in range, ++ triggering an assert. The bug is mostly harmless; the PNG file cannot ++ be fixed. ++ ++Version 1.6.20rc02 [November 29, 2015] ++ In libpng 1.6 zlib initialization was changed to use the window size ++ in the zlib stream, not a fixed value. This causes some invalid images, ++ where CINFO is too large, to display 'correctly' if the rest of the ++ data is valid. This provides a workaround for zlib versions where the ++ error arises (ones that support the API change to use the window size ++ in the stream). ++ ++Version 1.6.20 [December 3, 2015] ++ No changes. ++ ++Version 1.6.21beta01 [December 11, 2015] ++ Fixed syntax "$(command)" in tests/pngstest that some shells other than ++ bash could not parse (Bug report by Nelson Beebe). Use `command` instead. ++ ++Version 1.6.21beta02 [December 14, 2015] ++ Moved png_check_keyword() from pngwutil.c to pngset.c ++ Removed LE/BE dependencies in pngvalid, to 'fix' the current problem ++ in the BigEndian tests by not testing it, making the BE code the same ++ as the LE version. ++ Fixes to pngvalid for various reduced build configurations (eliminate unused ++ statics) and a fix for the case in rgb_to_gray when the digitize option ++ reduces graylo to 0, producing a large error. ++ ++Version 1.6.21beta03 [December 18, 2015] ++ Widened the 'limit' check on the internally calculated error limits in ++ the 'DIGITIZE' case (the code used prior to 1.7 for rgb_to_gray error ++ checks) and changed the check to only operate in non-release builds ++ (base build type not RC or RELEASE.) ++ Fixed undefined behavior in pngvalid.c, undefined because ++ (png_byte) << shift is undefined if it changes the signed bit ++ (because png_byte is promoted to int). The libpng exported functions ++ png_get_uint_32 and png_get_uint_16 handle this. (Bug reported by ++ David Drysdale as a result of reports from UBSAN in clang 3.8). ++ This changes pngvalid to use BE random numbers; this used to produce ++ errors but these should not be fixed as a result of the previous changes. ++ ++Version 1.6.21rc01 [January 4, 2016] ++ In projects/vstudio, combined readme.txt and WARNING into README.txt ++ ++Version 1.6.21rc02 [January 7, 2016] ++ Relocated assert() in contrib/tools/pngfix.c, bug found by American ++ Fuzzy Lop, reported by Brian Carpenter. ++ Marked 'limit' UNUSED in transform_range_check(). This only affects ++ release builds. ++ ++Version 1.6.21 [January 15, 2016] ++ Worked around a false-positive Coverity issue in pngvalid.c. ++ ++Version 1.6.22beta01 [January 23, 2016] ++ Changed PNG_USE_MKSTEMP to __COVERITY__ to select alternate ++ "tmpfile()" implementation in contrib/libtests/pngstest.c ++ Fixed NO_STDIO build of pngunknown.c to skip calling png_init_io() ++ if there is no stdio.h support. ++ Added a png_image_write_to_memory() API and a number of assist macros ++ to allow an application that uses the simplified API write to bypass ++ stdio and write directly to memory. ++ Added some warnings (png.h) and some check code to detect *possible* ++ overflow in the ROW_STRIDE and simplified image SIZE macros. This ++ disallows image width/height/format that *might* overflow. This is ++ a quiet API change that limits in-memory image size (uncompressed) to ++ less than 4GByte and image row size (stride) to less than 2GByte. ++ Revised workaround for false-positive Coverity issue in pngvalid.c. ++ ++Version 1.6.22beta02 [February 8, 2016] ++ Only use exit(77) in configure builds. ++ Corrected error in PNG_IMAGE_PNG_SIZE_MAX. This new macro underreported ++ the palette size because it failed to take into account that the memory ++ palette has to be expanded to full RGB when it is written to PNG. ++ Updated CMakeLists.txt, added supporting scripts/gen*.cmake.in ++ and test.cmake.in (Roger Leigh). ++ Relaxed limit checks on gamma values in pngrtran.c. As suggested in ++ the comments gamma values outside the range currently permitted ++ by png_set_alpha_mode are useful for HDR data encoding. These values ++ are already permitted by png_set_gamma so it is reasonable caution to ++ extend the png_set_alpha_mode range as HDR imaging systems are starting ++ to emerge. ++ ++Version 1.6.22beta03 [March 9, 2016] ++ Added a common-law trademark notice and export control information ++ to the LICENSE file, png.h, and the man page. ++ Restored "& 0xff" in png_save_uint_16() and png_save_uint_32() that ++ were accidentally removed from libpng-1.6.17. ++ Changed PNG_INFO_cHNK and PNG_FREE_cHNK from 0xnnnn to 0xnnnnU in png.h ++ (Robert C. Seacord). ++ Removed dubious "#if INT_MAX" test from png.h that was added to ++ libpng-1.6.19beta02 (John Bowler). ++ Add ${INCLUDES} in scripts/genout.cmake.in (Bug report by Nixon Kwok). ++ Updated LICENSE to say files in the contrib directory are not ++ necessarily under the libpng license, and that some makefiles have ++ other copyright owners. ++ Added INTEL-SSE2 support (Mike Klein and Matt Sarett, Google, Inc.). ++ Made contrib/libtests/timepng more robust. The code no longer gives ++ up/fails on invalid PNG data, it just skips it (with error messages). ++ The code no longer fails on PNG files with data beyond IEND. Options ++ exist to use png_read_png (reading the whole image, not by row) and, in ++ that case, to apply any of the supported transforms. This makes for ++ more realistic testing; the decoded data actually gets used in a ++ meaningful fashion (John Bowler). ++ Fixed some misleading indentation (Krishnaraj Bhat). ++ ++Version 1.6.22beta04 [April 5, 2016] ++ Force GCC compilation to C89 if needed (Dagobert Michelsen). ++ SSE filter speed improvements for bpp=3: ++ memcpy-free implementations of load3() / store3(). ++ call load3() only when needed at the end of a scanline. ++ ++Version 1.6.22beta05 [April 27, 2016] ++ Added PNG_FAST_FILTERS macro (defined as ++ PNG_FILTER_NONE|PNG_FILTER_SUB|PNG_FILTER_UP). ++ Various fixes for contrib/libtests/timepng.c ++ Moved INTEL-SSE code from pngpriv.h into contrib/intel/intel_sse.patch. ++ Fixed typo (missing underscore) in #define PNG_READ_16_TO_8_SUPPORTED ++ (Bug report by Y.Ohashik). ++ ++Version 1.6.22beta06 [May 5, 2016] ++ Rebased contrib/intel_sse.patch. ++ Quieted two Coverity issues in contrib/libtests/timepng.c. ++ Fixed issues with scripts/genout.cmake.in (David Capello, Nixon Kwok): ++ Added support to use multiple directories in ZLIBINCDIR variable, ++ Fixed CMAKE_C_FLAGS with multiple values when genout is compiled on MSVC, ++ Fixed pnglibconf.c compilation on OS X including the sysroot path. ++ ++Version 1.6.22rc01 [May 14, 2016] ++ No changes. ++ ++Version 1.6.22rc02 [May 16, 2016] ++ Removed contrib/timepng from default build; it does not build on platforms ++ that don't supply clock_gettime(). ++ ++Version 1.6.22rc03 [May 17, 2016] ++ Restored contrib/timepng to default build but check for the presence ++ of clock_gettime() in configure.ac and Makefile.am. ++ ++Version 1.6.22 [May 26, 2016] ++ No changes. ++ ++Version 1.6.23beta01 [May 29, 2016] ++ Stop a potential memory leak in png_set_tRNS() (Bug report by Ted Ying). ++ Fixed the progressive reader to handle empty first IDAT chunk properly ++ (patch by Timothy Nikkel). This bug was introduced in libpng-1.6.0 and ++ only affected the libpng16 branch. ++ Added tests in pngvalid.c to check zero-length IDAT chunks in various ++ positions. Fixed the sequential reader to handle these more robustly ++ (John Bowler). ++ ++Version 1.6.23rc01 [June 2, 2016] ++ Corrected progressive read input buffer in pngvalid.c. The previous version ++ the code invariably passed just one byte at a time to libpng. The intent ++ was to pass a random number of bytes in the range 0..511. ++ Moved sse2 prototype from pngpriv.h to contrib/intel/intel_sse.patch. ++ Added missing ")" in pngerror.c (Matt Sarrett). ++ ++Version 1.6.23rc02 [June 4, 2016] ++ Fixed undefined behavior in png_push_save_buffer(). Do not call ++ memcpy() with a null source, even if count is zero (Leon Scroggins III). ++ ++Version 1.6.23 [June 9, 2016] ++ Fixed bad link to RFC2083 in png.5 (Nikola Forro). ++ ++Version 1.6.24beta01 [June 11, 2016] ++ Avoid potential overflow of the PNG_IMAGE_SIZE macro. This macro ++ is not used within libpng, but is used in some of the examples. ++ ++Version 1.6.24beta02 [June 23, 2016] ++ Correct filter heuristic overflow handling. This was broken when the ++ write filter code was moved out-of-line; if there is a single filter and ++ the heuristic sum overflows the calculation of the filtered line is not ++ completed. In versions prior to 1.6 the code was duplicated in-line ++ and the check not performed, so the filter operation completed; however, ++ in the multi-filter case where the sum is performed the 'none' filter would ++ be selected if all the sums overflowed, even if it wasn't in the filter ++ list. The fix to the first problem is simply to provide PNG_SIZE_MAX as ++ the current lmins sum value; this means the sum can never exceed it and ++ overflows silently. A reasonable compiler that does choose to inline ++ the code will simply eliminate the sum check. ++ The fix to the second problem is to use high precision arithmetic (this is ++ implemented in 1.7), however a simple safe fix here is to chose the lowest ++ numbered filter in the list from png_set_filter (this only works if the ++ first problem is also fixed) (John Bowler). ++ Use a more efficient absolute value calculation on SSE2 (Matthieu Darbois). ++ Fixed the case where PNG_IMAGE_BUFFER_SIZE can overflow in the application ++ as a result of the application using an increased 'row_stride'; previously ++ png_image_finish_read only checked for overflow on the base calculation of ++ components. (I.e. it checked for overflow of a 32-bit number on the total ++ number of pixel components in the output format, not the possibly padded row ++ length and not the number of bytes, which for linear formats is twice the ++ number of components.) ++ MSVC does not like '-(unsigned)', so replaced it with 0U-(unsigned) ++ MSVC does not like (uInt) = -(unsigned) (i.e. as an initializer), unless ++ the conversion is explicitly invoked by a cast. ++ Put the SKIP definition in the correct place. It needs to come after the ++ png.h include (see all the other .c files in contrib/libtests) because it ++ depends on PNG_LIBPNG_VER. ++ Removed the three compile warning options from the individual project ++ files into the zlib.props globals. It increases the warning level from 4 ++ to All and adds a list of the warnings that need to be turned off. This is ++ semi-documentary; the intent is to tell libpng users which warnings have ++ been examined and judged non-fixable at present. The warning about ++ structure padding is fixable, but it would be a significant change (moving ++ structure members around). ++ ++Version 1.6.24beta03 [July 4, 2016] ++ Optimized absolute value calculation in filter selection, similar to ++ code in the PAETH decoder in pngrutil.c. Build with PNG_USE_ABS to ++ use this. ++ Added pngcp to the build together with a pngcp.dfa configuration test. ++ Added high resolution timing to pngcp. ++ Added "Common linking failures" section to INSTALL. ++ Relocated misplaced #endif in png.c sRGB profile checking. ++ Fixed two Coverity issues in pngcp.c. ++ ++Version 1.6.24beta04 [July 8, 2016] ++ Avoid filter-selection heuristic sum calculations in cases where only one ++ filter is a candidate for selection. This trades off code size (added ++ private png_setup_*_row_only() functions) for speed. ++ ++Version 1.6.24beta05 [July 13, 2016] ++ Fixed some indentation to comply with our coding style. ++ Added contrib/tools/reindent. ++ ++Version 1.6.24beta06 [July 18, 2016] ++ Fixed more indentation to comply with our coding style. ++ Eliminated unnecessary tests of boolean png_isaligned() vs 0. ++ ++Version 1.6.24rc01 [July 25, 2016] ++ No changes. ++ ++Version 1.6.24rc02 [August 1, 2016] ++ Conditionally compile SSE2 headers in contrib/intel/intel_sse.patch ++ Conditionally compile png_decompress_chunk(). ++ ++Version 1.6.24rc03 [August 2, 2016] ++ Conditionally compile ARM_NEON headers in pngpriv.h ++ Updated contrib/intel/intel_sse.patch ++ ++Version 1.6.24[August 4, 2016] ++ No changes. ++ ++Version 1.6.25beta01 [August 12, 2016] ++ Reject oversized iCCP profile immediately. ++ Cleaned up PNG_DEBUG compile of pngtest.c. ++ Conditionally compile png_inflate(). ++ ++Version 1.6.25beta02 [August 18, 2016] ++ Don't install pngcp; it conflicts with pngcp in the pngtools package. ++ Minor editing of INSTALL, (whitespace, added copyright line) ++ ++Version 1.6.25rc01 [August 24, 2016] ++ No changes. ++ ++Version 1.6.25rc02 [August 29, 2016] ++ Added MIPS support (Mandar Sahastrabuddhe ). ++ Only the UP filter is currently implemented. ++ ++Version 1.6.25rc03 [August 29, 2016] ++ Rebased contrib/intel/intel_sse.patch after the MIPS implementation. ++ ++Version 1.6.25rc04 [August 30, 2016] ++ Added MIPS support for SUB, AVG, and PAETH filters (Mandar Sahastrabuddhe). ++ ++Version 1.6.25rc05 [August 30, 2016] ++ Rebased contrib/intel/intel_sse.patch after the MIPS implementation update.. ++ ++Version 1.6.25 [September 1, 2016] ++ No changes. ++ ++Version 1.6.26beta01 [September 26, 2016] ++ Fixed handling zero length IDAT in pngfix (bug report by Agostino Sarubbo, ++ bugfix by John Bowler). ++ Do not issue a png_error() on read in png_set_pCAL() because png_handle_pCAL ++ has allocated memory that libpng needs to free. ++ Conditionally compile png_set_benign_errors() in pngread.c and pngtest.c ++ Issue a png_benign_error instead of a png_error on ADLER32 mismatch ++ while decoding compressed data chunks. ++ Changed PNG_ZLIB_VERNUM to ZLIB_VERNUM in pngpriv.h, pngstruct.h, and ++ pngrutil.c. ++ If CRC handling of critical chunks has been set to PNG_CRC_QUIET_USE, ++ ignore the ADLER32 checksum in the IDAT chunk as well as the chunk CRCs. ++ Issue png_benign_error() on ADLER32 checksum mismatch instead of png_error(). ++ Add tests/badcrc.png and tests/badadler.png to tests/pngtest. ++ Merged pngtest.c with libpng-1.7.0beta84/pngtest.c ++ ++Version 1.6.26beta02 [October 1, 2016] ++ Updated the documentation about CRC and ADLER32 handling. ++ Quieted 117 warnings from clang-3.8 in pngtrans.c, pngread.c, ++ pngwrite.c, pngunknown.c, and pngvalid.c. ++ Quieted 58 (out of 144) -Wconversion compiler warnings by changing ++ flag definitions in pngpriv.h from 0xnnnn to 0xnnnnU and trivial changes ++ in png.c, pngread.c, and pngwutil.c. ++ ++Version 1.6.26beta03 [October 2, 2016] ++ Removed contrib/libtests/*.orig and *.rej that slipped into the tarballs. ++ Quieted the 86 remaining -Wconversion compiler warnings by ++ revising the png_isaligned() macro and trivial changes in png.c, ++ pngerror.c, pngget.c, pngmem.c, pngset.c, pngrtran.c, pngrutil.c, ++ pngwtran.c, pngwrite.c, and pngwutil.c. ++ ++Version 1.6.26beta04 [October 3, 2016] ++ Quieted (bogus?) clang warnings about "absolute value has no effect" ++ when PNG_USE_ABS is defined. ++ Fixed offsets in contrib/intel/intel_sse.patch ++ ++Version 1.6.26beta05 [October 6, 2016] ++ Changed integer constant 4294967294 to unsigned 4294967294U in pngconf.h ++ to avoid a signed/unsigned compare in the preprocessor. ++ ++Version 1.6.26beta06 [October 7, 2016] ++ Use zlib-1.2.8.1 inflateValidate() instead of inflateReset2() to ++ optionally avoid ADLER32 evaluation. ++ ++Version 1.6.26rc01 [October 12, 2016] ++ No changes. ++ ++Version 1.6.26 [October 20, 2016] ++ Cosmetic change, "ptr != 0" to "ptr != NULL" in png.c and pngrutil.c ++ Despammed email addresses (replaced "@" with " at "). ++ ++Version 1.6.27beta01 [November 2, 2016] ++ Restrict the new ADLER32-skipping to IDAT chunks. It broke iCCP chunk ++ handling: an erroneous iCCP chunk would throw a png_error and reject the ++ entire PNG image instead of rejecting just the iCCP chunk with a warning, ++ if built with zlib-1.2.8.1. ++ ++Version 1.6.27rc01 [December 27, 2016] ++ Control ADLER32 checking with new PNG_IGNORE_ADLER32 option. Fixes ++ an endless loop when handling erroneous ADLER32 checksums; bug ++ introduced in libpng-1.6.26. ++ Removed the use of a macro containing the pre-processor 'defined' ++ operator. It is unclear whether this is valid; a macro that ++ "generates" 'defined' is not permitted, but the use of the word ++ "generates" within the C90 standard seems to imply more than simple ++ substitution of an expression itself containing a well-formed defined ++ operation. ++ Added ARM support to CMakeLists.txt (Andreas Franek). ++ ++Version 1.6.27 [December 29, 2016] ++ Fixed a potential null pointer dereference in png_set_text_2() (bug report ++ and patch by Patrick Keshishian, CVE-2016-10087). ++ ++Version 1.6.28rc01 [January 3, 2017] ++ Fixed arm/aarch64 detection in CMakeLists.txt (Gianfranco Costamagna). ++ Added option to Cmake build allowing a custom location of zlib to be ++ specified in a scenario where libpng is being built as a subproject ++ alongside zlib by another project (Sam Serrels). ++ Changed png_ptr->options from a png_byte to png_uint_32, to accommodate ++ up to 16 options. ++ ++Version 1.6.28rc02 [January 4, 2017] ++ Added "include(GNUInstallDirs)" to CMakeLists.txt (Gianfranco Costamagna). ++ Moved SSE2 optimization code into the main libpng source directory. ++ Configure libpng with "configure --enable-intel-sse" or compile ++ libpng with "-DPNG_INTEL_SSE" in CPPFLAGS to enable it. ++ ++Version 1.6.28rc03 [January 4, 2017] ++ Backed out the SSE optimization and last CMakeLists.txt to allow time for QA. ++ ++Version 1.6.28 [January 5, 2017] ++ No changes. ++ ++Version 1.6.29beta01 [January 12, 2017] ++ Readded "include(GNUInstallDirs)" to CMakeLists.txt (Gianfranco Costamagna). ++ Moved SSE2 optimization code into the main libpng source directory. ++ Configure libpng with "configure --enable-intel-sse" or compile ++ libpng with "-DPNG_INTEL_SSE" in CPPFLAGS to enable it. ++ Simplified conditional compilation in pngvalid.c, for AIX (Michael Felt). ++ ++Version 1.6.29beta02 [February 22, 2017] ++ Avoid conditional directives that break statements in pngrutil.c (Romero ++ Malaquias) ++ The contrib/examples/pngtopng.c recovery code was in the wrong "if" ++ branches; the comments were correct. ++ Added code for PowerPC VSX optimisation (Vadim Barkov). ++ ++Version 1.6.29beta03 [March 1, 2017] ++ Avoid potential overflow of shift operations in png_do_expand() (Aaron Boxer). ++ Change test ZLIB_VERNUM >= 0x1281 to ZLIB_VERNUM >= 0x1290 in pngrutil.c ++ because Solaris 11 distributes zlib-1.2.8.f that is older than 1.2.8.1, ++ as suggested in zlib FAQ, item 24. ++ Suppress clang warnings about implicit sign changes in png.c ++ ++Version 1.6.29 [March 16, 2017] ++ No changes. ++ ++Version 1.6.30beta01 [April 1, 2017] ++ Added missing "$(CPPFLAGS)" to the compile line for c.pic.o in ++ makefile.linux and makefile.solaris-x86 (Cosmin). ++ Revised documentation of png_get_error_ptr() in the libpng manual. ++ Silence clang -Wcomma and const drop warnings (Viktor Szakats). ++ Update Sourceforge URLs in documentation (https instead of http). ++ ++Version 1.6.30beta02 [April 22, 2017] ++ Document need to check for integer overflow when allocating a pixel ++ buffer for multiple rows in contrib/gregbook, contrib/pngminus, ++ example.c, and in the manual (suggested by Jaeseung Choi). This ++ is similar to the bug reported against pngquant in CVE-2016-5735. ++ Removed reference to the obsolete PNG_SAFE_LIMITS macro in the documentation. ++ ++Version 1.6.30beta03 [May 22, 2017] ++ Check for integer overflow in contrib/visupng and contrib/tools/genpng. ++ Do not double evaluate CMAKE_SYSTEM_PROCESSOR in CMakeLists.txt. ++ Test CMAKE_HOST_WIN32 instead of WIN32 in CMakeLists.txt. ++ Fix some URL in documentation. ++ ++Version 1.6.30beta04 [June 7, 2017] ++ Avoid writing an empty IDAT when the last IDAT exactly fills the ++ compression buffer (bug report by Brian Baird). This bug was ++ introduced in libpng-1.6.0. ++ ++Version 1.6.30rc01 [June 14, 2017] ++ No changes. ++ ++Version 1.6.30rc02 [June 25, 2017] ++ Update copyright year in pnglibconf.h, make ltmain.sh executable. ++ Add a reference to the libpng.download site in README. ++ ++Version 1.6.30 [June 28, 2017] ++ No changes. ++ ++Version 1.6.31beta01 [July 5, 2017] ++ Guard the definition of _POSIX_SOURCE in pngpriv.h (AIX already defines it; ++ bug report by Michael Felt). ++ Revised pngpriv.h to work around failure to compile arm/filter_neon.S ++ ("typedef" directive is unrecognized by the assembler). The problem ++ was introduced in libpng-1.6.30beta01. ++ Added "Requires: zlib" to libpng.pc.in (Pieter Neerincx). ++ Added special case for FreeBSD in arm/filter_neon.S (Maya Rashish). ++ ++Version 1.6.31beta02 [July 8, 2017] ++ Added instructions for disabling hardware optimizations in INSTALL. ++ Added "--enable-hardware-optimizations" configuration flag to enable ++ or disable all hardware optimizations with one flag. ++ ++Version 1.6.31beta03 [July 9, 2017] ++ Updated CMakeLists.txt to add INTEL_SSE and MIPS_MSA platforms. ++ Changed "int" to "png_size_t" in intel/filter_sse2.c to prevent ++ possible integer overflow (Bug report by John Bowler). ++ Quieted "declaration after statement" warnings in intel/filter_sse2.c. ++ Added scripts/makefile-linux-opt, which has hardware optimizations enabled. ++ ++Version 1.6.31beta04 [July 11, 2017] ++ Removed one of the GCC-7.1.0 'strict-overflow' warnings that result when ++ integers appear on both sides of a compare. Worked around the others by ++ forcing the strict-overflow setting in the relevant functions to a level ++ where they are not reported (John Bowler). ++ Changed "FALL THROUGH" comments to "FALLTHROUGH" because GCC doesn't like ++ the space. ++ Worked around some C-style casts from (void*) because g++ 5.4.0 objects ++ to them. ++ Increased the buffer size for 'sprint' to pass the gcc 7.1.0 'sprint ++ overflow' check that is on by default with -Wall -Wextra. ++ ++Version 1.6.31beta05 [July 13, 2017] ++ Added eXIf chunk support. ++ ++Version 1.6.31beta06 [July 17, 2017] ++ Added a minimal eXIf chunk (with Orientation and FocalLengthIn35mmFilm ++ tags) to pngtest.png. ++ ++Version 1.6.31beta07 [July 18, 2017] ++ Revised the eXIf chunk in pngtest.png to fix "Bad IFD1 Directory" warning. ++ ++Version 1.6.31rc01 [July 19, 2017] ++ No changes. ++ ++Version 1.6.31rc02 [July 25, 2017] ++ Fixed typo in example.c (png_free_image should be png_image_free) (Bug ++ report by John Smith) ++ ++Version 1.6.31 [July 27, 2017] ++ No changes. ++ ++Version 1.6.32beta01 [July 31, 2017] ++ Avoid possible NULL dereference in png_handle_eXIf when benign_errors ++ are allowed. Avoid leaking the input buffer "eXIf_buf". ++ Eliminated png_ptr->num_exif member from pngstruct.h and added num_exif ++ to arguments for png_get_eXIf() and png_set_eXIf(). ++ Added calls to png_handle_eXIf(() in pngread.c and png_write_eXIf() in ++ pngwrite.c, and made various other fixes to png_write_eXIf(). ++ Changed name of png_get_eXIF and png_set_eXIf() to png_get_eXIf_1() and ++ png_set_eXIf_1(), respectively, to avoid breaking API compatibility ++ with libpng-1.6.31. ++ ++Version 1.6.32beta02 [August 1, 2017] ++ Updated contrib/libtests/pngunknown.c with eXIf chunk. ++ ++Version 1.6.32beta03 [August 2, 2017] ++ Initialized btoa[] in pngstest.c ++ Stop memory leak when returning from png_handle_eXIf() with an error ++ (Bug report from the OSS-fuzz project). ++ ++Version 1.6.32beta04 [August 2, 2017] ++ Replaced local eXIf_buf with info_ptr-eXIf_buf in png_handle_eXIf(). ++ Update libpng.3 and libpng-manual.txt about eXIf functions. ++ ++Version 1.6.32beta05 [August 2, 2017] ++ Restored png_get_eXIf() and png_set_eXIf() to maintain API compatibility. ++ ++Version 1.6.32beta06 [August 2, 2017] ++ Removed png_get_eXIf_1() and png_set_eXIf_1(). ++ ++Version 1.6.32beta07 [August 3, 2017] ++ Check length of all chunks except IDAT against user limit to fix an ++ OSS-fuzz issue (Fixes CVE-2017-12652). ++ ++Version 1.6.32beta08 [August 3, 2017] ++ Check length of IDAT against maximum possible IDAT size, accounting ++ for height, rowbytes, interlacing and zlib/deflate overhead. ++ Restored png_get_eXIf_1() and png_set_eXIf_1(), because strlen(eXIf_buf) ++ does not work (the eXIf chunk data can contain zeroes). ++ ++Version 1.6.32beta09 [August 3, 2017] ++ Require cmake-2.8.8 in CMakeLists.txt. Revised symlink creation, ++ no longer using deprecated cmake LOCATION feature (Clifford Yapp). ++ Fixed five-byte error in the calculation of IDAT maximum possible size. ++ ++Version 1.6.32beta10 [August 5, 2017] ++ Moved chunk-length check into a png_check_chunk_length() private ++ function (Suggested by Max Stepin). ++ Moved bad pngs from tests to contrib/libtests/crashers ++ Moved testing of bad pngs into a separate tests/pngtest-badpngs script ++ Added the --xfail (expected FAIL) option to pngtest.c. It writes XFAIL ++ in the output but PASS for the libpng test. ++ Require cmake-3.0.2 in CMakeLists.txt (Clifford Yapp). ++ Fix "const" declaration info_ptr argument to png_get_eXIf_1() and the ++ num_exif argument to png_get_eXIf_1() (Github Issue 171). ++ ++Version 1.6.32beta11 [August 7, 2017] ++ Added "eXIf" to "chunks_to_ignore[]" in png_set_keep_unknown_chunks(). ++ Added huge_IDAT.png and empty_ancillary_chunks.png to testpngs/crashers. ++ Make pngtest --strict, --relax, --xfail options imply -m (multiple). ++ Removed unused chunk_name parameter from png_check_chunk_length(). ++ Relocated setting free_me for eXIf data, to stop an OSS-fuzz leak. ++ Initialize profile_header[] in png_handle_iCCP() to fix OSS-fuzz issue. ++ Initialize png_ptr->row_buf[0] to 255 in png_read_row() to fix OSS-fuzz UMR. ++ Attempt to fix a UMR in png_set_text_2() to fix OSS-fuzz issue. ++ Increase minimum zlib stream from 9 to 14 in png_handle_iCCP(), to account ++ for the minimum 'deflate' stream, and relocate the test to a point ++ after the keyword has been read. ++ Check that the eXIf chunk has at least 2 bytes and begins with "II" or "MM". ++ ++Version 1.6.32rc01 [August 18, 2017] ++ Added a set of "huge_xxxx_chunk.png" files to contrib/testpngs/crashers, ++ one for each known chunk type, with length = 2GB-1. ++ Check for 0 return from png_get_rowbytes() and added some (size_t) typecasts ++ in contrib/pngminus/*.c to stop some Coverity issues (162705, 162706, ++ and 162707). ++ Renamed chunks in contrib/testpngs/crashers to avoid having files whose ++ names differ only in case; this causes problems with some platforms ++ (github issue #172). ++ ++Version 1.6.32rc02 [August 22, 2017] ++ Added contrib/oss-fuzz directory which contains files used by the oss-fuzz ++ project (https://github.com/google/oss-fuzz/tree/master/projects/libpng). ++ ++Version 1.6.32 [August 24, 2017] ++ No changes. ++ ++Version 1.6.33beta01 [August 28, 2017] ++ Added PNGMINUS_UNUSED macro to contrib/pngminus/p*.c and added missing ++ parenthesis in contrib/pngminus/pnm2png.c (bug report by Christian Hesse). ++ Fixed off-by-one error in png_do_check_palette_indexes() (Bug report ++ by Mick P., Source Forge Issue #269). ++ ++Version 1.6.33beta02 [September 3, 2017] ++ Initialize png_handler.row_ptr in contrib/oss-fuzz/libpng_read_fuzzer.cc ++ to fix shortlived oss-fuzz issue 3234. ++ Compute a larger limit on IDAT because some applications write a deflate ++ buffer for each row (Bug report by Andrew Church). ++ Use current date (DATE) instead of release-date (RDATE) in last ++ changed date of contrib/oss-fuzz files. ++ Enabled ARM support in CMakeLists.txt (Bernd Kuhls). ++ ++Version 1.6.33beta03 [September 14, 2017] ++ Fixed incorrect typecast of some arguments to png_malloc() and ++ png_calloc() that were png_uint_32 instead of png_alloc_size_t ++ (Bug report by "irwir" in Github libpng issue #175). ++ Use pnglibconf.h.prebuilt when building for ANDROID with cmake (Github ++ issue 162, by rcdailey). ++ ++Version 1.6.33rc01 [September 20, 2017] ++ Initialize memory allocated by png_inflate to zero, using memset, to ++ stop an oss-fuzz "use of uninitialized value" detection in png_set_text_2() ++ due to truncated iTXt or zTXt chunk. ++ Initialize memory allocated by png_read_buffer to zero, using memset, to ++ stop an oss-fuzz "use of uninitialized value" detection in ++ png_icc_check_tag_table() due to truncated iCCP chunk. ++ Removed a redundant test (suggested by "irwir" in Github issue #180). ++ ++Version 1.6.33rc02 [September 23, 2017] ++ Added an interlaced version of each file in contrib/pngsuite. ++ Relocate new memset() call in pngrutil.c. ++ Removed more redundant tests (suggested by "irwir" in Github issue #180). ++ Add support for loading images with associated alpha in the Simplified ++ API (Samuel Williams). ++ ++Version 1.6.33 [September 28, 2017] ++ Revert contrib/oss-fuzz/libpng_read_fuzzer.cc to libpng-1.6.32 state. ++ Initialize png_handler.row_ptr in contrib/oss-fuzz/libpng_read_fuzzer.cc ++ Add end_info structure and png_read_end() to the libpng fuzzer. ++ ++Version 1.6.34 [September 29, 2017] ++ Removed contrib/pngsuite/i*.png; some of them caused test failures. ++ ++Version 1.6.35beta01 [March 6, 2018] ++ Restored 21 of the contrib/pngsuite/i*.png, which do not cause test ++ failures. Placed the remainder in contrib/pngsuite/interlaced/i*.png. ++ Added calls to png_set_*() transforms commonly used by browsers to ++ the fuzzer. ++ Removed some unnecessary brackets in pngrtran.c ++ Fixed miscellaneous typos (Patch by github user "luzpaz"). ++ Change "ASM C" to "C ASM" in CMakeLists.txt ++ Fixed incorrect handling of bKGD chunk in sub-8-bit files (Cosmin) ++ Added hardware optimization directories to zip and 7z distributions. ++ Fixed incorrect bitmask for options. ++ Fixed many spelling typos. ++ ++Version 1.6.35beta02 [March 28, 2018] ++ Make png_get_iCCP consistent with man page (allow compression-type argument ++ to be NULL, bug report by Lenard Szolnoki). ++ ++Version 1.6.35 [July 15, 2018] ++ Replaced the remaining uses of png_size_t with size_t (Cosmin) ++ Fixed the calculation of row_factor in png_check_chunk_length ++ (reported by Thuan Pham in SourceForge issue #278) ++ Added missing parentheses to a macro definition ++ (suggested by "irwir" in GitHub issue #216) ++ ++Version 1.6.36 [December 1, 2018] ++ Optimized png_do_expand_palette for ARM processors. ++ Improved performance by around 10-22% on a recent ARM Chromebook. ++ (Contributed by Richard Townsend, ARM Holdings) ++ Fixed manipulation of machine-specific optimization options. ++ (Contributed by Vicki Pfau) ++ Used memcpy instead of manual pointer arithmetic on Intel SSE2. ++ (Contributed by Samuel Williams) ++ Fixed build errors with MSVC on ARM64. ++ (Contributed by Zhijie Liang) ++ Fixed detection of libm in CMakeLists. ++ (Contributed by Cameron Cawley) ++ Fixed incorrect creation of pkg-config file in CMakeLists. ++ (Contributed by Kyle Bentley) ++ Fixed the CMake build on Windows MSYS by avoiding symlinks. ++ Fixed a build warning on OpenBSD. ++ (Contributed by Theo Buehler) ++ Fixed various typos in comments. ++ (Contributed by "luz.paz") ++ Raised the minimum required CMake version from 3.0.2 to 3.1. ++ Removed yet more of the vestigial support for pre-ANSI C compilers. ++ Removed ancient makefiles for ancient systems that have been broken ++ across all previous libpng-1.6.x versions. ++ Removed the Y2K compliance statement and the export control ++ information. ++ Applied various code style and documentation fixes. ++ ++Version 1.6.37 [April 14, 2019] ++ Fixed a use-after-free vulnerability (CVE-2019-7317) in png_image_free. ++ Fixed a memory leak in the ARM NEON implementation of png_do_expand_palette. ++ Fixed a memory leak in pngtest.c. ++ Fixed two vulnerabilities (CVE-2018-14048, CVE-2018-14550) in ++ contrib/pngminus; refactor. ++ Changed the license of contrib/pngminus to MIT; refresh makefile and docs. ++ (Contributed by Willem van Schaik) ++ Fixed a typo in the libpng license v2. ++ (Contributed by Miguel Ojeda) ++ Added makefiles for AddressSanitizer-enabled builds. ++ Cleaned up various makefiles. ++ ++Send comments/corrections/commendations to png-mng-implement at lists.sf.net. ++Subscription is required; visit ++https://lists.sourceforge.net/lists/listinfo/png-mng-implement ++to subscribe. +diff --git a/lib/libpng/INSTALL b/lib/libpng/INSTALL +new file mode 100644 +index 000000000..4c1702251 +--- /dev/null ++++ b/lib/libpng/INSTALL +@@ -0,0 +1,465 @@ ++ ++ Installing libpng ++ ++Contents ++ ++ I. Simple installation ++ II. Rebuilding the configure scripts ++ III. Using scripts/makefile* ++ IV. Using cmake ++ V. Directory structure ++ VI. Building with project files ++ VII. Building with makefiles ++ VIII. Configuring libpng for 16-bit platforms ++ IX. Configuring for DOS ++ X. Configuring for Medium Model ++ XI. Prepending a prefix to exported symbols ++ XII. Configuring for compiler xxx: ++ XIII. Removing unwanted object code ++ XIV. Enabling or disabling hardware optimizations ++ XV. Changes to the build and configuration of libpng in libpng-1.5.x ++ XVI. Setjmp/longjmp issues ++ XVII. Common linking failures ++ XVIII. Other sources of information about libpng ++ ++I. Simple installation ++ ++On Unix/Linux and similar systems, you can simply type ++ ++ ./configure [--prefix=/path] ++ make check ++ make install ++ ++and ignore the rest of this document. "/path" is the path to the directory ++where you want to install the libpng "lib", "include", and "bin" ++subdirectories. ++ ++If you downloaded a GIT clone, you will need to run ./autogen.sh before ++running ./configure, to create "configure" and "Makefile.in" which are ++not included in the GIT repository. ++ ++Note that "configure" is only included in the "*.tar" distributions and not ++in the "*.zip" or "*.7z" distributions. If you downloaded one of those ++distributions, see "Building with project files" or "Building with makefiles", ++below. ++ ++II. Rebuilding the configure scripts ++ ++If configure does not work on your system, or if you have a need to ++change configure.ac or Makefile.am, and you have a reasonably ++up-to-date set of tools, running ./autogen.sh in a git clone before ++running ./configure may fix the problem. To be really sure that you ++aren't using any of the included pre-built scripts, especially if you ++are building from a tar distribution instead of a git distribution, ++do this: ++ ++ ./configure --enable-maintainer-mode ++ make maintainer-clean ++ ./autogen.sh --maintainer --clean ++ ./autogen.sh --maintainer ++ ./configure [--prefix=/path] [other options] ++ make ++ make install ++ make check ++ ++III. Using scripts/makefile* ++ ++Instead, you can use one of the custom-built makefiles in the ++"scripts" directory ++ ++ cp scripts/pnglibconf.h.prebuilt pnglibconf.h ++ cp scripts/makefile.system makefile ++ make test ++ make install ++ ++The files that are presently available in the scripts directory ++are listed and described in scripts/README.txt. ++ ++Or you can use one of the "projects" in the "projects" directory. ++ ++Before installing libpng, you must first install zlib, if it ++is not already on your system. zlib can usually be found ++wherever you got libpng; otherwise go to https://zlib.net/. You can ++place zlib in the same directory as libpng or in another directory. ++ ++If your system already has a preinstalled zlib you will still need ++to have access to the zlib.h and zconf.h include files that ++correspond to the version of zlib that's installed. ++ ++If you wish to test with a particular zlib that is not first in the ++standard library search path, put ZLIBLIB, ZLIBINC, CPPFLAGS, LDFLAGS, ++and LD_LIBRARY_PATH in your environment before running "make test" ++or "make distcheck": ++ ++ ZLIBLIB=/path/to/lib export ZLIBLIB ++ ZLIBINC=/path/to/include export ZLIBINC ++ CPPFLAGS="-I$ZLIBINC" export CPPFLAGS ++ LDFLAGS="-L$ZLIBLIB" export LDFLAGS ++ LD_LIBRARY_PATH="$ZLIBLIB:$LD_LIBRARY_PATH" export LD_LIBRARY_PATH ++ ++If you are using one of the makefile scripts, put ZLIBLIB and ZLIBINC ++in your environment and type ++ ++ make ZLIBLIB=$ZLIBLIB ZLIBINC=$ZLIBINC test ++ ++IV. Using cmake ++ ++If you want to use "cmake" (see www.cmake.org), type ++ ++ cmake . -DCMAKE_INSTALL_PREFIX=/path ++ make ++ make install ++ ++As when using the simple configure method described above, "/path" points to ++the installation directory where you want to put the libpng "lib", "include", ++and "bin" subdirectories. ++ ++V. Directory structure ++ ++You can rename the directories that you downloaded (they ++might be called "libpng-x.y.z" or "libpngNN" and "zlib-1.2.8" ++or "zlib128") so that you have directories called "zlib" and "libpng". ++ ++Your directory structure should look like this: ++ ++ .. (the parent directory) ++ libpng (this directory) ++ INSTALL (this file) ++ README ++ *.h, *.c => libpng source files ++ CMakeLists.txt => "cmake" script ++ configuration files: ++ configure.ac, configure, Makefile.am, Makefile.in, ++ autogen.sh, config.guess, ltmain.sh, missing, libpng.pc.in, ++ libpng-config.in, aclocal.m4, config.h.in, config.sub, ++ depcomp, install-sh, mkinstalldirs, test-pngtest.sh ++ contrib ++ arm-neon, conftest, examples, gregbook, libtests, pngminim, ++ pngminus, pngsuite, tools, visupng ++ projects ++ cbuilder5, owatcom, visualc71, vstudio, xcode ++ scripts ++ makefile.* ++ *.def (module definition files) ++ etc. ++ pngtest.png ++ etc. ++ zlib ++ README, *.h, *.c contrib, etc. ++ ++If the line endings in the files look funny, you may wish to get the other ++distribution of libpng. It is available in both tar.gz (UNIX style line ++endings) and zip (DOS style line endings) formats. ++ ++VI. Building with project files ++ ++If you are building libpng with MSVC, you can enter the ++libpng projects\visualc71 or vstudio directory and follow the instructions ++in README.txt. ++ ++Otherwise enter the zlib directory and follow the instructions in zlib/README, ++then come back here and run "configure" or choose the appropriate ++makefile.sys in the scripts directory. ++ ++VII. Building with makefiles ++ ++Copy the file (or files) that you need from the ++scripts directory into this directory, for example ++ ++MSDOS example: ++ ++ copy scripts\makefile.msc makefile ++ copy scripts\pnglibconf.h.prebuilt pnglibconf.h ++ ++UNIX example: ++ ++ cp scripts/makefile.std makefile ++ cp scripts/pnglibconf.h.prebuilt pnglibconf.h ++ ++Read the makefile to see if you need to change any source or ++target directories to match your preferences. ++ ++Then read pnglibconf.dfa to see if you want to make any configuration ++changes. ++ ++Then just run "make" which will create the libpng library in ++this directory and "make test" which will run a quick test that reads ++the "pngtest.png" file and writes a "pngout.png" file that should be ++identical to it. Look for "9782 zero samples" in the output of the ++test. For more confidence, you can run another test by typing ++"pngtest pngnow.png" and looking for "289 zero samples" in the output. ++Also, you can run "pngtest -m contrib/pngsuite/*.png" and compare ++your output with the result shown in contrib/pngsuite/README. ++ ++Most of the makefiles will allow you to run "make install" to ++put the library in its final resting place (if you want to ++do that, run "make install" in the zlib directory first if necessary). ++Some also allow you to run "make test-installed" after you have ++run "make install". ++ ++VIII. Configuring libpng for 16-bit platforms ++ ++You will want to look into zconf.h to tell zlib (and thus libpng) that ++it cannot allocate more than 64K at a time. Even if you can, the memory ++won't be accessible. So limit zlib and libpng to 64K by defining MAXSEG_64K. ++ ++IX. Configuring for DOS ++ ++For DOS users who only have access to the lower 640K, you will ++have to limit zlib's memory usage via a png_set_compression_mem_level() ++call. See zlib.h or zconf.h in the zlib library for more information. ++ ++X. Configuring for Medium Model ++ ++Libpng's support for medium model has been tested on most of the popular ++compilers. Make sure MAXSEG_64K gets defined, USE_FAR_KEYWORD gets ++defined, and FAR gets defined to far in pngconf.h, and you should be ++all set. Everything in the library (except for zlib's structure) is ++expecting far data. You must use the typedefs with the p or pp on ++the end for pointers (or at least look at them and be careful). Make ++note that the rows of data are defined as png_bytepp, which is ++an "unsigned char far * far *". ++ ++XI. Prepending a prefix to exported symbols ++ ++Starting with libpng-1.6.0, you can configure libpng (when using the ++"configure" script) to prefix all exported symbols by means of the ++configuration option "--with-libpng-prefix=FOO_", where FOO_ can be any ++string beginning with a letter and containing only uppercase ++and lowercase letters, digits, and the underscore (i.e., a C language ++identifier). This creates a set of macros in pnglibconf.h, so this is ++transparent to applications; their function calls get transformed by ++the macros to use the modified names. ++ ++XII. Configuring for compiler xxx: ++ ++All includes for libpng are in pngconf.h. If you need to add, change ++or delete an include, this is the place to do it. ++The includes that are not needed outside libpng are placed in pngpriv.h, ++which is only used by the routines inside libpng itself. ++The files in libpng proper only include pngpriv.h and png.h, which ++in turn includes pngconf.h and, as of libpng-1.5.0, pnglibconf.h. ++As of libpng-1.5.0, pngpriv.h also includes three other private header ++files, pngstruct.h, pnginfo.h, and pngdebug.h, which contain material ++that previously appeared in the public headers. ++ ++XIII. Removing unwanted object code ++ ++There are a bunch of #define's in pngconf.h that control what parts of ++libpng are compiled. All the defines end in _SUPPORTED. If you are ++never going to use a capability, you can change the #define to #undef ++before recompiling libpng and save yourself code and data space, or ++you can turn off individual capabilities with defines that begin with ++"PNG_NO_". ++ ++In libpng-1.5.0 and later, the #define's are in pnglibconf.h instead. ++ ++You can also turn all of the transforms and ancillary chunk capabilities ++off en masse with compiler directives that define ++PNG_NO_READ[or WRITE]_TRANSFORMS, or PNG_NO_READ[or WRITE]_ANCILLARY_CHUNKS, ++or all four, along with directives to turn on any of the capabilities that ++you do want. The PNG_NO_READ[or WRITE]_TRANSFORMS directives disable the ++extra transformations but still leave the library fully capable of reading ++and writing PNG files with all known public chunks. Use of the ++PNG_NO_READ[or WRITE]_ANCILLARY_CHUNKS directive produces a library ++that is incapable of reading or writing ancillary chunks. If you are ++not using the progressive reading capability, you can turn that off ++with PNG_NO_PROGRESSIVE_READ (don't confuse this with the INTERLACING ++capability, which you'll still have). ++ ++All the reading and writing specific code are in separate files, so the ++linker should only grab the files it needs. However, if you want to ++make sure, or if you are building a stand alone library, all the ++reading files start with "pngr" and all the writing files start with "pngw". ++The files that don't match either (like png.c, pngtrans.c, etc.) ++are used for both reading and writing, and always need to be included. ++The progressive reader is in pngpread.c ++ ++If you are creating or distributing a dynamically linked library (a .so ++or DLL file), you should not remove or disable any parts of the library, ++as this will cause applications linked with different versions of the ++library to fail if they call functions not available in your library. ++The size of the library itself should not be an issue, because only ++those sections that are actually used will be loaded into memory. ++ ++XIV. Enabling or disabling hardware optimizations ++ ++Certain hardware capabilities, such as the Intel SSE instructions, ++are normally detected at run time. Enable them with configure options ++such as one of ++ ++ --enable-arm-neon=yes ++ --enable-mips-msa=yes ++ --enable-intel-sse=yes ++ --enable-powerpc-vsx=yes ++ ++or enable them all at once with ++ ++ --enable-hardware-optimizations=yes ++ ++or, if you are not using "configure", you can use one ++or more of ++ ++ CPPFLAGS += "-DPNG_ARM_NEON" ++ CPPFLAGS += "-DPNG_MIPS_MSA" ++ CPPFLAGS += "-DPNG_INTEL_SSE" ++ CPPFLAGS += "-DPNG_POWERPC_VSX" ++ ++See for example scripts/makefile.linux-opt ++ ++If you wish to avoid using them, ++you can disable them via the configure option ++ ++ --disable-hardware-optimizations ++ ++to disable them all, or ++ ++ --enable-intel-sse=no ++ ++to disable a particular one, ++or via compiler-command options such as ++ ++ CPPFLAGS += "-DPNG_ARM_NEON_OPT=0, -DPNG_MIPS_MSA_OPT=0, ++ -DPNG_INTEL_SSE_OPT=0, -DPNG_POWERPC_VSX_OPT=0" ++ ++If you are using cmake, hardware optimizations are "on" ++by default. To disable them, use ++ ++ cmake . -DPNG_ARM_NEON=no -DPNG_INTEL_SSE=no \ ++ -DPNG_MIPS_MSA=no -DPNG_POWERPC_VSX=no ++ ++or disable them all at once with ++ ++ cmake . -DPNG_HARDWARE_OPTIMIZATIONS=no ++ ++XV. Changes to the build and configuration of libpng in libpng-1.5.x ++ ++Details of internal changes to the library code can be found in the CHANGES ++file and in the GIT repository logs. These will be of no concern to the vast ++majority of library users or builders; however, the few who configure libpng ++to a non-default feature set may need to change how this is done. ++ ++There should be no need for library builders to alter build scripts if ++these use the distributed build support - configure or the makefiles - ++however, users of the makefiles may care to update their build scripts ++to build pnglibconf.h where the corresponding makefile does not do so. ++ ++Building libpng with a non-default configuration has changed completely. ++The old method using pngusr.h should still work correctly even though the ++way pngusr.h is used in the build has been changed; however, library ++builders will probably want to examine the changes to take advantage of ++new capabilities and to simplify their build system. ++ ++A. Specific changes to library configuration capabilities ++ ++The exact mechanism used to control attributes of API functions has ++changed. A single set of operating system independent macro definitions ++is used and operating system specific directives are defined in ++pnglibconf.h ++ ++As part of this the mechanism used to choose procedure call standards on ++those systems that allow a choice has been changed. At present this only ++affects certain Microsoft (DOS, Windows) and IBM (OS/2) operating systems ++running on Intel processors. As before, PNGAPI is defined where required ++to control the exported API functions; however, two new macros, PNGCBAPI ++and PNGCAPI, are used instead for callback functions (PNGCBAPI) and ++(PNGCAPI) for functions that must match a C library prototype (currently ++only png_longjmp_ptr, which must match the C longjmp function.) The new ++approach is documented in pngconf.h ++ ++Despite these changes, libpng 1.5.0 only supports the native C function ++calling standard on those platforms tested so far ("__cdecl" on Microsoft ++Windows). This is because the support requirements for alternative ++calling conventions seem to no longer exist. Developers who find it ++necessary to set PNG_API_RULE to 1 should advise the mailing list ++(png-mng-implement) of this and library builders who use Openwatcom and ++therefore set PNG_API_RULE to 2 should also contact the mailing list. ++ ++B. Changes to the configuration mechanism ++ ++Prior to libpng-1.5.0 library builders who needed to configure libpng ++had either to modify the exported pngconf.h header file to add system ++specific configuration or had to write feature selection macros into ++pngusr.h and cause this to be included into pngconf.h by defining ++PNG_USER_CONFIG. The latter mechanism had the disadvantage that an ++application built without PNG_USER_CONFIG defined would see the ++unmodified, default, libpng API and thus would probably fail to link. ++ ++These mechanisms still work in the configure build and in any makefile ++build that builds pnglibconf.h, although the feature selection macros ++have changed somewhat as described above. In 1.5.0, however, pngusr.h is ++processed only once, at the time the exported header file pnglibconf.h is ++built. pngconf.h no longer includes pngusr.h; therefore, pngusr.h is ignored ++after the build of pnglibconf.h and it is never included in an application ++build. ++ ++The formerly used alternative of adding a list of feature macros to the ++CPPFLAGS setting in the build also still works; however, the macros will be ++copied to pnglibconf.h and this may produce macro redefinition warnings ++when the individual C files are compiled. ++ ++All configuration now only works if pnglibconf.h is built from ++scripts/pnglibconf.dfa. This requires the program awk. Brian Kernighan ++(the original author of awk) maintains C source code of that awk and this ++and all known later implementations (often called by subtly different ++names - nawk and gawk for example) are adequate to build pnglibconf.h. ++The Sun Microsystems (now Oracle) program 'awk' is an earlier version ++and does not work; this may also apply to other systems that have a ++functioning awk called 'nawk'. ++ ++Configuration options are now documented in scripts/pnglibconf.dfa. This ++file also includes dependency information that ensures a configuration is ++consistent; that is, if a feature is switched off, dependent features are ++also switched off. As a recommended alternative to using feature macros in ++pngusr.h a system builder may also define equivalent options in pngusr.dfa ++(or, indeed, any file) and add that to the configuration by setting ++DFA_XTRA to the file name. The makefiles in contrib/pngminim illustrate ++how to do this, and also illustrate a case where pngusr.h is still required. ++ ++After you have built libpng, the definitions that were recorded in ++pnglibconf.h are available to your application (pnglibconf.h is included ++in png.h and gets installed alongside png.h and pngconf.h in your ++$PREFIX/include directory). Do not edit pnglibconf.h after you have built ++libpng, because than the settings would not accurately reflect the settings ++that were used to build libpng. ++ ++XVI. Setjmp/longjmp issues ++ ++Libpng uses setjmp()/longjmp() for error handling. Unfortunately setjmp() ++is known to be not thread-safe on some platforms and we don't know of ++any platform where it is guaranteed to be thread-safe. Therefore, if ++your application is going to be using multiple threads, you should ++configure libpng with PNG_NO_SETJMP in your pngusr.dfa file, with ++-DPNG_NO_SETJMP on your compile line, or with ++ ++ #undef PNG_SETJMP_SUPPORTED ++ ++in your pnglibconf.h or pngusr.h. ++ ++Starting with libpng-1.6.0, the library included a "simplified API". ++This requires setjmp/longjmp, so you must either build the library ++with PNG_SETJMP_SUPPORTED defined, or with PNG_SIMPLIFIED_READ_SUPPORTED ++and PNG_SIMPLIFIED_WRITE_SUPPORTED undefined. ++ ++XVII. Common linking failures ++ ++If your application fails to find libpng or zlib entries while linking: ++ ++ Be sure "-lz" appears after "-lpng" on your linking command. ++ ++ Be sure you have built libpng, zlib, and your application for the ++ same platform (e.g., 32-bit or 64-bit). ++ ++ If you are using the vstudio project, observe the WARNING in ++ project/vstudio/README.txt. ++ ++XVIII. Other sources of information about libpng: ++ ++Further information can be found in the README and libpng-manual.txt ++files, in the individual makefiles, in png.h, and the manual pages ++libpng.3 and png.5. ++ ++Copyright (c) 1998-2002,2006-2016 Glenn Randers-Pehrson ++This document is released under the libpng license. ++For conditions of distribution and use, see the disclaimer ++and license in png.h. +diff --git a/lib/libpng/LICENSE b/lib/libpng/LICENSE +new file mode 100644 +index 000000000..e0c5b531c +--- /dev/null ++++ b/lib/libpng/LICENSE +@@ -0,0 +1,134 @@ ++COPYRIGHT NOTICE, DISCLAIMER, and LICENSE ++========================================= ++ ++PNG Reference Library License version 2 ++--------------------------------------- ++ ++ * Copyright (c) 1995-2019 The PNG Reference Library Authors. ++ * Copyright (c) 2018-2019 Cosmin Truta. ++ * Copyright (c) 2000-2002, 2004, 2006-2018 Glenn Randers-Pehrson. ++ * Copyright (c) 1996-1997 Andreas Dilger. ++ * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc. ++ ++The software is supplied "as is", without warranty of any kind, ++express or implied, including, without limitation, the warranties ++of merchantability, fitness for a particular purpose, title, and ++non-infringement. In no event shall the Copyright owners, or ++anyone distributing the software, be liable for any damages or ++other liability, whether in contract, tort or otherwise, arising ++from, out of, or in connection with the software, or the use or ++other dealings in the software, even if advised of the possibility ++of such damage. ++ ++Permission is hereby granted to use, copy, modify, and distribute ++this software, or portions hereof, for any purpose, without fee, ++subject to the following restrictions: ++ ++ 1. The origin of this software must not be misrepresented; you ++ must not claim that you wrote the original software. If you ++ use this software in a product, an acknowledgment in the product ++ documentation would be appreciated, but is not required. ++ ++ 2. Altered source versions must be plainly marked as such, and must ++ not be misrepresented as being the original software. ++ ++ 3. This Copyright notice may not be removed or altered from any ++ source or altered source distribution. ++ ++ ++PNG Reference Library License version 1 (for libpng 0.5 through 1.6.35) ++----------------------------------------------------------------------- ++ ++libpng versions 1.0.7, July 1, 2000, through 1.6.35, July 15, 2018 are ++Copyright (c) 2000-2002, 2004, 2006-2018 Glenn Randers-Pehrson, are ++derived from libpng-1.0.6, and are distributed according to the same ++disclaimer and license as libpng-1.0.6 with the following individuals ++added to the list of Contributing Authors: ++ ++ Simon-Pierre Cadieux ++ Eric S. Raymond ++ Mans Rullgard ++ Cosmin Truta ++ Gilles Vollant ++ James Yu ++ Mandar Sahastrabuddhe ++ Google Inc. ++ Vadim Barkov ++ ++and with the following additions to the disclaimer: ++ ++ There is no warranty against interference with your enjoyment of ++ the library or against infringement. There is no warranty that our ++ efforts or the library will fulfill any of your particular purposes ++ or needs. This library is provided with all faults, and the entire ++ risk of satisfactory quality, performance, accuracy, and effort is ++ with the user. ++ ++Some files in the "contrib" directory and some configure-generated ++files that are distributed with libpng have other copyright owners, and ++are released under other open source licenses. ++ ++libpng versions 0.97, January 1998, through 1.0.6, March 20, 2000, are ++Copyright (c) 1998-2000 Glenn Randers-Pehrson, are derived from ++libpng-0.96, and are distributed according to the same disclaimer and ++license as libpng-0.96, with the following individuals added to the ++list of Contributing Authors: ++ ++ Tom Lane ++ Glenn Randers-Pehrson ++ Willem van Schaik ++ ++libpng versions 0.89, June 1996, through 0.96, May 1997, are ++Copyright (c) 1996-1997 Andreas Dilger, are derived from libpng-0.88, ++and are distributed according to the same disclaimer and license as ++libpng-0.88, with the following individuals added to the list of ++Contributing Authors: ++ ++ John Bowler ++ Kevin Bracey ++ Sam Bushell ++ Magnus Holmgren ++ Greg Roelofs ++ Tom Tanner ++ ++Some files in the "scripts" directory have other copyright owners, ++but are released under this license. ++ ++libpng versions 0.5, May 1995, through 0.88, January 1996, are ++Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc. ++ ++For the purposes of this copyright and license, "Contributing Authors" ++is defined as the following set of individuals: ++ ++ Andreas Dilger ++ Dave Martindale ++ Guy Eric Schalnat ++ Paul Schmidt ++ Tim Wegner ++ ++The PNG Reference Library is supplied "AS IS". The Contributing ++Authors and Group 42, Inc. disclaim all warranties, expressed or ++implied, including, without limitation, the warranties of ++merchantability and of fitness for any purpose. The Contributing ++Authors and Group 42, Inc. assume no liability for direct, indirect, ++incidental, special, exemplary, or consequential damages, which may ++result from the use of the PNG Reference Library, even if advised of ++the possibility of such damage. ++ ++Permission is hereby granted to use, copy, modify, and distribute this ++source code, or portions hereof, for any purpose, without fee, subject ++to the following restrictions: ++ ++ 1. The origin of this source code must not be misrepresented. ++ ++ 2. Altered versions must be plainly marked as such and must not ++ be misrepresented as being the original source. ++ ++ 3. This Copyright notice may not be removed or altered from any ++ source or altered source distribution. ++ ++The Contributing Authors and Group 42, Inc. specifically permit, ++without fee, and encourage the use of this source code as a component ++to supporting the PNG file format in commercial products. If you use ++this source code in a product, acknowledgment is not required but would ++be appreciated. +diff --git a/lib/libpng/README b/lib/libpng/README +new file mode 100644 +index 000000000..cfc1f0e3d +--- /dev/null ++++ b/lib/libpng/README +@@ -0,0 +1,183 @@ ++README for libpng version 1.6.37 - April 14, 2019 ++================================================= ++ ++See the note about version numbers near the top of png.h. ++See INSTALL for instructions on how to install libpng. ++ ++Libpng comes in several distribution formats. Get libpng-*.tar.gz or ++libpng-*.tar.xz or if you want UNIX-style line endings in the text ++files, or lpng*.7z or lpng*.zip if you want DOS-style line endings. ++ ++Version 0.89 was the first official release of libpng. Don't let the ++fact that it's the first release fool you. The libpng library has been ++in extensive use and testing since mid-1995. By late 1997 it had ++finally gotten to the stage where there hadn't been significant ++changes to the API in some time, and people have a bad feeling about ++libraries with versions < 1.0. Version 1.0.0 was released in ++March 1998. ++ ++**** ++Note that some of the changes to the png_info structure render this ++version of the library binary incompatible with libpng-0.89 or ++earlier versions if you are using a shared library. The type of the ++"filler" parameter for png_set_filler() has changed from png_byte to ++png_uint_32, which will affect shared-library applications that use ++this function. ++ ++To avoid problems with changes to the internals of the png info_struct, ++new APIs have been made available in 0.95 to avoid direct application ++access to info_ptr. These functions are the png_set_ and ++png_get_ functions. These functions should be used when ++accessing/storing the info_struct data, rather than manipulating it ++directly, to avoid such problems in the future. ++ ++It is important to note that the APIs did not make current programs ++that access the info struct directly incompatible with the new ++library, through libpng-1.2.x. In libpng-1.4.x, which was meant to ++be a transitional release, members of the png_struct and the ++info_struct can still be accessed, but the compiler will issue a ++warning about deprecated usage. Since libpng-1.5.0, direct access ++to these structs is not allowed, and the definitions of the structs ++reside in private pngstruct.h and pnginfo.h header files that are not ++accessible to applications. It is strongly suggested that new ++programs use the new APIs (as shown in example.c and pngtest.c), and ++older programs be converted to the new format, to facilitate upgrades ++in the future. ++**** ++ ++Additions since 0.90 include the ability to compile libpng as a ++Windows DLL, and new APIs for accessing data in the info struct. ++Experimental functions include the ability to set weighting and cost ++factors for row filter selection, direct reads of integers from buffers ++on big-endian processors that support misaligned data access, faster ++methods of doing alpha composition, and more accurate 16->8 bit color ++conversion. ++ ++The additions since 0.89 include the ability to read from a PNG stream ++which has had some (or all) of the signature bytes read by the calling ++application. This also allows the reading of embedded PNG streams that ++do not have the PNG file signature. As well, it is now possible to set ++the library action on the detection of chunk CRC errors. It is possible ++to set different actions based on whether the CRC error occurred in a ++critical or an ancillary chunk. ++ ++For a detailed description on using libpng, read libpng-manual.txt. ++For examples of libpng in a program, see example.c and pngtest.c. For ++usage information and restrictions (what little they are) on libpng, ++see png.h. For a description on using zlib (the compression library ++used by libpng) and zlib's restrictions, see zlib.h ++ ++I have included a general makefile, as well as several machine and ++compiler specific ones, but you may have to modify one for your own ++needs. ++ ++You should use zlib 1.0.4 or later to run this, but it MAY work with ++versions as old as zlib 0.95. Even so, there are bugs in older zlib ++versions which can cause the output of invalid compression streams for ++some images. ++ ++You should also note that zlib is a compression library that is useful ++for more things than just PNG files. You can use zlib as a drop-in ++replacement for fread() and fwrite(), if you are so inclined. ++ ++zlib should be available at the same place that libpng is, or at ++https://zlib.net. ++ ++You may also want a copy of the PNG specification. It is available ++as an RFC, a W3C Recommendation, and an ISO/IEC Standard. You can find ++these at http://www.libpng.org/pub/png/pngdocs.html . ++ ++This code is currently being archived at libpng.sourceforge.io in the ++[DOWNLOAD] area, and at http://libpng.download/src . ++ ++This release, based in a large way on Glenn's, Guy's and Andreas' ++earlier work, was created and will be supported by myself and the PNG ++development group. ++ ++Send comments/corrections/commendations to png-mng-implement at ++lists.sourceforge.net (subscription required; visit ++https://lists.sourceforge.net/lists/listinfo/png-mng-implement ++to subscribe). ++ ++Send general questions about the PNG specification to png-mng-misc ++at lists.sourceforge.net (subscription required; visit ++https://lists.sourceforge.net/lists/listinfo/png-mng-misc to ++subscribe). ++ ++Files in this distribution: ++ ++ ANNOUNCE => Announcement of this version, with recent changes ++ AUTHORS => List of contributing authors ++ CHANGES => Description of changes between libpng versions ++ KNOWNBUG => List of known bugs and deficiencies ++ LICENSE => License to use and redistribute libpng ++ README => This file ++ TODO => Things not implemented in the current library ++ TRADEMARK => Trademark information ++ example.c => Example code for using libpng functions ++ libpng.3 => manual page for libpng (includes libpng-manual.txt) ++ libpng-manual.txt => Description of libpng and its functions ++ libpngpf.3 => manual page for libpng's private functions ++ png.5 => manual page for the PNG format ++ png.c => Basic interface functions common to library ++ png.h => Library function and interface declarations (public) ++ pngpriv.h => Library function and interface declarations (private) ++ pngconf.h => System specific library configuration (public) ++ pngstruct.h => png_struct declaration (private) ++ pnginfo.h => png_info struct declaration (private) ++ pngdebug.h => debugging macros (private) ++ pngerror.c => Error/warning message I/O functions ++ pngget.c => Functions for retrieving info from struct ++ pngmem.c => Memory handling functions ++ pngbar.png => PNG logo, 88x31 ++ pngnow.png => PNG logo, 98x31 ++ pngpread.c => Progressive reading functions ++ pngread.c => Read data/helper high-level functions ++ pngrio.c => Lowest-level data read I/O functions ++ pngrtran.c => Read data transformation functions ++ pngrutil.c => Read data utility functions ++ pngset.c => Functions for storing data into the info_struct ++ pngtest.c => Library test program ++ pngtest.png => Library test sample image ++ pngtrans.c => Common data transformation functions ++ pngwio.c => Lowest-level write I/O functions ++ pngwrite.c => High-level write functions ++ pngwtran.c => Write data transformations ++ pngwutil.c => Write utility functions ++ arm => Contains optimized code for the ARM platform ++ powerpc => Contains optimized code for the PowerPC platform ++ contrib => Contributions ++ arm-neon => Optimized code for ARM-NEON platform ++ powerpc-vsx => Optimized code for POWERPC-VSX platform ++ examples => Example programs ++ gregbook => source code for PNG reading and writing, from ++ Greg Roelofs' "PNG: The Definitive Guide", ++ O'Reilly, 1999 ++ libtests => Test programs ++ mips-msa => Optimized code for MIPS-MSA platform ++ pngminim => Minimal decoder, encoder, and progressive decoder ++ programs demonstrating use of pngusr.dfa ++ pngminus => Simple pnm2png and png2pnm programs ++ pngsuite => Test images ++ testpngs ++ tools => Various tools ++ visupng => Contains a MSVC workspace for VisualPng ++ intel => Optimized code for INTEL-SSE2 platform ++ mips => Optimized code for MIPS platform ++ projects => Contains project files and workspaces for ++ building a DLL ++ owatcom => Contains a WATCOM project for building libpng ++ visualc71 => Contains a Microsoft Visual C++ (MSVC) ++ workspace for building libpng and zlib ++ vstudio => Contains a Microsoft Visual C++ (MSVC) ++ workspace for building libpng and zlib ++ scripts => Directory containing scripts for building libpng: ++ (see scripts/README.txt for the list of scripts) ++ ++Good luck, and happy coding! ++ ++ * Cosmin Truta (current maintainer, since 2018) ++ * Glenn Randers-Pehrson (former maintainer, 1998-2018) ++ * Andreas Eric Dilger (former maintainer, 1996-1997) ++ * Guy Eric Schalnat (original author and former maintainer, 1995-1996) ++ (formerly of Group 42, Inc.) +diff --git a/lib/libpng/TODO b/lib/libpng/TODO +new file mode 100644 +index 000000000..562dab069 +--- /dev/null ++++ b/lib/libpng/TODO +@@ -0,0 +1,23 @@ ++TODO - list of things to do for libpng: ++ ++* Fix all defects (duh!) ++* Better C++ wrapper / full C++ implementation (?) ++* Fix the problems with C++ and 'extern "C"'. ++* cHRM transformation. ++* Palette creation. ++* "grayscale->palette" transformation and "palette->grayscale" detection. ++* Improved dithering. ++* Multi-lingual error and warning message support. ++* Complete sRGB transformation. (Currently it simply uses gamma=0.45455.) ++* Man pages for function calls. ++* Better documentation. ++* Better filter selection ++ (e.g., counting huffman bits/precompression; filter inertia; filter costs). ++* Histogram creation. ++* Text conversion between different code pages (e.g., Latin-1 -> Mac). ++* Avoid building gamma tables whenever possible. ++* Greater precision in changing to linear gamma for compositing against ++ background, and in doing rgb-to-gray transformations. ++* Investigate pre-incremented loop counters and other loop constructions. ++* Interpolated method of handling interlacing. ++* More validations for libpng transformations. +diff --git a/lib/libpng/include/png.h b/lib/libpng/include/png.h +new file mode 100644 +index 000000000..72984f4b7 +--- /dev/null ++++ b/lib/libpng/include/png.h +@@ -0,0 +1,3247 @@ ++ ++/* png.h - header file for PNG reference library ++ * ++ * libpng version 1.6.37 - April 14, 2019 ++ * ++ * Copyright (c) 2018-2021 Cosmin Truta ++ * Copyright (c) 1998-2021,2004,2006-2018 Glenn Randers-Pehrson ++ * Copyright (c) 1996-2021 Andreas Dilger ++ * Copyright (c) 1995-2021 Guy Eric Schalnat, Group 42, Inc. ++ * ++ * This code is released under the libpng license. (See LICENSE, below.) ++ * ++ * Authors and maintainers: ++ * libpng versions 0.71, May 1995, through 0.88, January 1996: Guy Schalnat ++ * libpng versions 0.89, June 1996, through 0.96, May 1997: Andreas Dilger ++ * libpng versions 0.97, January 1998, through 1.6.35, July 2018: ++ * Glenn Randers-Pehrson ++ * libpng versions 1.6.36, December 2018, through 1.6.37, April 2019: ++ * Cosmin Truta ++ * See also "Contributing Authors", below. ++ */ ++ ++/* ++ * COPYRIGHT NOTICE, DISCLAIMER, and LICENSE ++ * ========================================= ++ * ++ * PNG Reference Library License version 2 ++ * --------------------------------------- ++ * ++ * * Copyright (c) 1995-2021 The PNG Reference Library Authors. ++ * * Copyright (c) 2018-2021 Cosmin Truta. ++ * * Copyright (c) 2000-2021, 2004, 2006-2018 Glenn Randers-Pehrson. ++ * * Copyright (c) 1996-2021 Andreas Dilger. ++ * * Copyright (c) 1995-2021 Guy Eric Schalnat, Group 42, Inc. ++ * ++ * The software is supplied "as is", without warranty of any kind, ++ * express or implied, including, without limitation, the warranties ++ * of merchantability, fitness for a particular purpose, title, and ++ * non-infringement. In no event shall the Copyright owners, or ++ * anyone distributing the software, be liable for any damages or ++ * other liability, whether in contract, tort or otherwise, arising ++ * from, out of, or in connection with the software, or the use or ++ * other dealings in the software, even if advised of the possibility ++ * of such damage. ++ * ++ * Permission is hereby granted to use, copy, modify, and distribute ++ * this software, or portions hereof, for any purpose, without fee, ++ * subject to the following restrictions: ++ * ++ * 1. The origin of this software must not be misrepresented; you ++ * must not claim that you wrote the original software. If you ++ * use this software in a product, an acknowledgment in the product ++ * documentation would be appreciated, but is not required. ++ * ++ * 2. Altered source versions must be plainly marked as such, and must ++ * not be misrepresented as being the original software. ++ * ++ * 3. This Copyright notice may not be removed or altered from any ++ * source or altered source distribution. ++ * ++ * ++ * PNG Reference Library License version 1 (for libpng 0.5 through 1.6.35) ++ * ----------------------------------------------------------------------- ++ * ++ * libpng versions 1.0.7, July 1, 2000, through 1.6.35, July 15, 2018 are ++ * Copyright (c) 2000-2021, 2004, 2006-2018 Glenn Randers-Pehrson, are ++ * derived from libpng-1.0.6, and are distributed according to the same ++ * disclaimer and license as libpng-1.0.6 with the following individuals ++ * added to the list of Contributing Authors: ++ * ++ * Simon-Pierre Cadieux ++ * Eric S. Raymond ++ * Mans Rullgard ++ * Cosmin Truta ++ * Gilles Vollant ++ * James Yu ++ * Mandar Sahastrabuddhe ++ * Google Inc. ++ * Vadim Barkov ++ * ++ * and with the following additions to the disclaimer: ++ * ++ * There is no warranty against interference with your enjoyment of ++ * the library or against infringement. There is no warranty that our ++ * efforts or the library will fulfill any of your particular purposes ++ * or needs. This library is provided with all faults, and the entire ++ * risk of satisfactory quality, performance, accuracy, and effort is ++ * with the user. ++ * ++ * Some files in the "contrib" directory and some configure-generated ++ * files that are distributed with libpng have other copyright owners, and ++ * are released under other open source licenses. ++ * ++ * libpng versions 0.97, January 1998, through 1.0.6, March 20, 2000, are ++ * Copyright (c) 1998-2021 Glenn Randers-Pehrson, are derived from ++ * libpng-0.96, and are distributed according to the same disclaimer and ++ * license as libpng-0.96, with the following individuals added to the ++ * list of Contributing Authors: ++ * ++ * Tom Lane ++ * Glenn Randers-Pehrson ++ * Willem van Schaik ++ * ++ * libpng versions 0.89, June 1996, through 0.96, May 1997, are ++ * Copyright (c) 1996-2021 Andreas Dilger, are derived from libpng-0.88, ++ * and are distributed according to the same disclaimer and license as ++ * libpng-0.88, with the following individuals added to the list of ++ * Contributing Authors: ++ * ++ * John Bowler ++ * Kevin Bracey ++ * Sam Bushell ++ * Magnus Holmgren ++ * Greg Roelofs ++ * Tom Tanner ++ * ++ * Some files in the "scripts" directory have other copyright owners, ++ * but are released under this license. ++ * ++ * libpng versions 0.5, May 1995, through 0.88, January 1996, are ++ * Copyright (c) 1995-2021 Guy Eric Schalnat, Group 42, Inc. ++ * ++ * For the purposes of this copyright and license, "Contributing Authors" ++ * is defined as the following set of individuals: ++ * ++ * Andreas Dilger ++ * Dave Martindale ++ * Guy Eric Schalnat ++ * Paul Schmidt ++ * Tim Wegner ++ * ++ * The PNG Reference Library is supplied "AS IS". The Contributing ++ * Authors and Group 42, Inc. disclaim all warranties, expressed or ++ * implied, including, without limitation, the warranties of ++ * merchantability and of fitness for any purpose. The Contributing ++ * Authors and Group 42, Inc. assume no liability for direct, indirect, ++ * incidental, special, exemplary, or consequential damages, which may ++ * result from the use of the PNG Reference Library, even if advised of ++ * the possibility of such damage. ++ * ++ * Permission is hereby granted to use, copy, modify, and distribute this ++ * source code, or portions hereof, for any purpose, without fee, subject ++ * to the following restrictions: ++ * ++ * 1. The origin of this source code must not be misrepresented. ++ * ++ * 2. Altered versions must be plainly marked as such and must not ++ * be misrepresented as being the original source. ++ * ++ * 3. This Copyright notice may not be removed or altered from any ++ * source or altered source distribution. ++ * ++ * The Contributing Authors and Group 42, Inc. specifically permit, ++ * without fee, and encourage the use of this source code as a component ++ * to supporting the PNG file format in commercial products. If you use ++ * this source code in a product, acknowledgment is not required but would ++ * be appreciated. ++ * ++ * END OF COPYRIGHT NOTICE, DISCLAIMER, and LICENSE. ++ * ++ * TRADEMARK ++ * ========= ++ * ++ * The name "libpng" has not been registered by the Copyright owners ++ * as a trademark in any jurisdiction. However, because libpng has ++ * been distributed and maintained world-wide, continually since 1995, ++ * the Copyright owners claim "common-law trademark protection" in any ++ * jurisdiction where common-law trademark is recognized. ++ */ ++ ++/* ++ * A "png_get_copyright" function is available, for convenient use in "about" ++ * boxes and the like: ++ * ++ * printf("%s", png_get_copyright(NULL)); ++ * ++ * Also, the PNG logo (in PNG format, of course) is supplied in the ++ * files "pngbar.png" and "pngbar.jpg (88x31) and "pngnow.png" (98x31). ++ */ ++ ++/* ++ * The contributing authors would like to thank all those who helped ++ * with testing, bug fixes, and patience. This wouldn't have been ++ * possible without all of you. ++ * ++ * Thanks to Frank J. T. Wojcik for helping with the documentation. ++ */ ++ ++/* Note about libpng version numbers: ++ * ++ * Due to various miscommunications, unforeseen code incompatibilities ++ * and occasional factors outside the authors' control, version numbering ++ * on the library has not always been consistent and straightforward. ++ * The following table summarizes matters since version 0.89c, which was ++ * the first widely used release: ++ * ++ * source png.h png.h shared-lib ++ * version string int version ++ * ------- ------ ----- ---------- ++ * 0.89c "1.0 beta 3" 0.89 89 1.0.89 ++ * 0.90 "1.0 beta 4" 0.90 90 0.90 [should have been 2.0.90] ++ * 0.95 "1.0 beta 5" 0.95 95 0.95 [should have been 2.0.95] ++ * 0.96 "1.0 beta 6" 0.96 96 0.96 [should have been 2.0.96] ++ * 0.97b "1.00.97 beta 7" 1.00.97 97 1.0.1 [should have been 2.0.97] ++ * 0.97c 0.97 97 2.0.97 ++ * 0.98 0.98 98 2.0.98 ++ * 0.99 0.99 98 2.0.99 ++ * 0.99a-m 0.99 99 2.0.99 ++ * 1.00 1.00 100 2.1.0 [100 should be 10000] ++ * 1.0.0 (from here on, the 100 2.1.0 [100 should be 10000] ++ * 1.0.1 png.h string is 10001 2.1.0 ++ * 1.0.1a-e identical to the 10002 from here on, the shared library ++ * 1.0.2 source version) 10002 is 2.V where V is the source code ++ * 1.0.2a-b 10003 version, except as noted. ++ * 1.0.3 10003 ++ * 1.0.3a-d 10004 ++ * 1.0.4 10004 ++ * 1.0.4a-f 10005 ++ * 1.0.5 (+ 2 patches) 10005 ++ * 1.0.5a-d 10006 ++ * 1.0.5e-r 10100 (not source compatible) ++ * 1.0.5s-v 10006 (not binary compatible) ++ * 1.0.6 (+ 3 patches) 10006 (still binary incompatible) ++ * 1.0.6d-f 10007 (still binary incompatible) ++ * 1.0.6g 10007 ++ * 1.0.6h 10007 10.6h (testing xy.z so-numbering) ++ * 1.0.6i 10007 10.6i ++ * 1.0.6j 10007 2.1.0.6j (incompatible with 1.0.0) ++ * 1.0.7beta11-14 DLLNUM 10007 2.1.0.7beta11-14 (binary compatible) ++ * 1.0.7beta15-18 1 10007 2.1.0.7beta15-18 (binary compatible) ++ * 1.0.7rc1-2 1 10007 2.1.0.7rc1-2 (binary compatible) ++ * 1.0.7 1 10007 (still compatible) ++ * ... ++ * 1.0.69 10 10069 10.so.0.69[.0] ++ * ... ++ * 1.2.59 13 10259 12.so.0.59[.0] ++ * ... ++ * 1.4.20 14 10420 14.so.0.20[.0] ++ * ... ++ * 1.5.30 15 10530 15.so.15.30[.0] ++ * ... ++ * 1.6.37 16 10637 16.so.16.37[.0] ++ * ++ * Henceforth the source version will match the shared-library major and ++ * minor numbers; the shared-library major version number will be used for ++ * changes in backward compatibility, as it is intended. ++ * The PNG_LIBPNG_VER macro, which is not used within libpng but is ++ * available for applications, is an unsigned integer of the form XYYZZ ++ * corresponding to the source version X.Y.Z (leading zeros in Y and Z). ++ * Beta versions were given the previous public release number plus a ++ * letter, until version 1.0.6j; from then on they were given the upcoming ++ * public release number plus "betaNN" or "rcNN". ++ * ++ * Binary incompatibility exists only when applications make direct access ++ * to the info_ptr or png_ptr members through png.h, and the compiled ++ * application is loaded with a different version of the library. ++ * ++ * DLLNUM will change each time there are forward or backward changes ++ * in binary compatibility (e.g., when a new feature is added). ++ * ++ * See libpng.txt or libpng.3 for more information. The PNG specification ++ * is available as a W3C Recommendation and as an ISO/IEC Standard; see ++ * ++ */ ++ ++#ifndef PNG_H ++#define PNG_H ++ ++/* This is not the place to learn how to use libpng. The file libpng-manual.txt ++ * describes how to use libpng, and the file example.c summarizes it ++ * with some code on which to build. This file is useful for looking ++ * at the actual function definitions and structure components. If that ++ * file has been stripped from your copy of libpng, you can find it at ++ * ++ * ++ * If you just need to read a PNG file and don't want to read the documentation ++ * skip to the end of this file and read the section entitled 'simplified API'. ++ */ ++ ++/* Version information for png.h - this should match the version in png.c */ ++#define PNG_LIBPNG_VER_STRING "1.6.37" ++#define PNG_HEADER_VERSION_STRING " libpng version 1.6.37 - April 14, 2019\n" ++ ++#define PNG_LIBPNG_VER_SONUM 16 ++#define PNG_LIBPNG_VER_DLLNUM 16 ++ ++/* These should match the first 3 components of PNG_LIBPNG_VER_STRING: */ ++#define PNG_LIBPNG_VER_MAJOR 1 ++#define PNG_LIBPNG_VER_MINOR 6 ++#define PNG_LIBPNG_VER_RELEASE 37 ++ ++/* This should be zero for a public release, or non-zero for a ++ * development version. [Deprecated] ++ */ ++#define PNG_LIBPNG_VER_BUILD 0 ++ ++/* Release Status */ ++#define PNG_LIBPNG_BUILD_ALPHA 1 ++#define PNG_LIBPNG_BUILD_BETA 2 ++#define PNG_LIBPNG_BUILD_RC 3 ++#define PNG_LIBPNG_BUILD_STABLE 4 ++#define PNG_LIBPNG_BUILD_RELEASE_STATUS_MASK 7 ++ ++/* Release-Specific Flags */ ++#define PNG_LIBPNG_BUILD_PATCH 8 /* Can be OR'ed with ++ PNG_LIBPNG_BUILD_STABLE only */ ++#define PNG_LIBPNG_BUILD_PRIVATE 16 /* Cannot be OR'ed with ++ PNG_LIBPNG_BUILD_SPECIAL */ ++#define PNG_LIBPNG_BUILD_SPECIAL 32 /* Cannot be OR'ed with ++ PNG_LIBPNG_BUILD_PRIVATE */ ++ ++#define PNG_LIBPNG_BUILD_BASE_TYPE PNG_LIBPNG_BUILD_STABLE ++ ++/* Careful here. At one time, Guy wanted to use 082, but that ++ * would be octal. We must not include leading zeros. ++ * Versions 0.7 through 1.0.0 were in the range 0 to 100 here ++ * (only version 1.0.0 was mis-numbered 100 instead of 10000). ++ * From version 1.0.1 it is: ++ * XXYYZZ, where XX=major, YY=minor, ZZ=release ++ */ ++#define PNG_LIBPNG_VER 10637 /* 1.6.37 */ ++ ++/* Library configuration: these options cannot be changed after ++ * the library has been built. ++ */ ++#ifndef PNGLCONF_H ++/* If pnglibconf.h is missing, you can ++ * copy scripts/pnglibconf.h.prebuilt to pnglibconf.h ++ */ ++# include "pnglibconf.h" ++#endif ++ ++#ifndef PNG_VERSION_INFO_ONLY ++/* Machine specific configuration. */ ++# include "pngconf.h" ++#endif ++ ++/* ++ * Added at libpng-1.2.8 ++ * ++ * Ref MSDN: Private as priority over Special ++ * VS_FF_PRIVATEBUILD File *was not* built using standard release ++ * procedures. If this value is given, the StringFileInfo block must ++ * contain a PrivateBuild string. ++ * ++ * VS_FF_SPECIALBUILD File *was* built by the original company using ++ * standard release procedures but is a variation of the standard ++ * file of the same version number. If this value is given, the ++ * StringFileInfo block must contain a SpecialBuild string. ++ */ ++ ++#ifdef PNG_USER_PRIVATEBUILD /* From pnglibconf.h */ ++# define PNG_LIBPNG_BUILD_TYPE \ ++ (PNG_LIBPNG_BUILD_BASE_TYPE | PNG_LIBPNG_BUILD_PRIVATE) ++#else ++# ifdef PNG_LIBPNG_SPECIALBUILD ++# define PNG_LIBPNG_BUILD_TYPE \ ++ (PNG_LIBPNG_BUILD_BASE_TYPE | PNG_LIBPNG_BUILD_SPECIAL) ++# else ++# define PNG_LIBPNG_BUILD_TYPE (PNG_LIBPNG_BUILD_BASE_TYPE) ++# endif ++#endif ++ ++#ifndef PNG_VERSION_INFO_ONLY ++ ++/* Inhibit C++ name-mangling for libpng functions but not for system calls. */ ++#ifdef __cplusplus ++extern "C" { ++#endif /* __cplusplus */ ++ ++/* Version information for C files, stored in png.c. This had better match ++ * the version above. ++ */ ++#define png_libpng_ver png_get_header_ver(NULL) ++ ++/* This file is arranged in several sections: ++ * ++ * 1. [omitted] ++ * 2. Any configuration options that can be specified by for the application ++ * code when it is built. (Build time configuration is in pnglibconf.h) ++ * 3. Type definitions (base types are defined in pngconf.h), structure ++ * definitions. ++ * 4. Exported library functions. ++ * 5. Simplified API. ++ * 6. Implementation options. ++ * ++ * The library source code has additional files (principally pngpriv.h) that ++ * allow configuration of the library. ++ */ ++ ++/* Section 1: [omitted] */ ++ ++/* Section 2: run time configuration ++ * See pnglibconf.h for build time configuration ++ * ++ * Run time configuration allows the application to choose between ++ * implementations of certain arithmetic APIs. The default is set ++ * at build time and recorded in pnglibconf.h, but it is safe to ++ * override these (and only these) settings. Note that this won't ++ * change what the library does, only application code, and the ++ * settings can (and probably should) be made on a per-file basis ++ * by setting the #defines before including png.h ++ * ++ * Use macros to read integers from PNG data or use the exported ++ * functions? ++ * PNG_USE_READ_MACROS: use the macros (see below) Note that ++ * the macros evaluate their argument multiple times. ++ * PNG_NO_USE_READ_MACROS: call the relevant library function. ++ * ++ * Use the alternative algorithm for compositing alpha samples that ++ * does not use division? ++ * PNG_READ_COMPOSITE_NODIV_SUPPORTED: use the 'no division' ++ * algorithm. ++ * PNG_NO_READ_COMPOSITE_NODIV: use the 'division' algorithm. ++ * ++ * How to handle benign errors if PNG_ALLOW_BENIGN_ERRORS is ++ * false? ++ * PNG_ALLOW_BENIGN_ERRORS: map calls to the benign error ++ * APIs to png_warning. ++ * Otherwise the calls are mapped to png_error. ++ */ ++ ++/* Section 3: type definitions, including structures and compile time ++ * constants. ++ * See pngconf.h for base types that vary by machine/system ++ */ ++ ++/* This triggers a compiler error in png.c, if png.c and png.h ++ * do not agree upon the version number. ++ */ ++typedef char* png_libpng_version_1_6_37; ++ ++/* Basic control structions. Read libpng-manual.txt or libpng.3 for more info. ++ * ++ * png_struct is the cache of information used while reading or writing a single ++ * PNG file. One of these is always required, although the simplified API ++ * (below) hides the creation and destruction of it. ++ */ ++typedef struct png_struct_def png_struct; ++typedef const png_struct * png_const_structp; ++typedef png_struct * png_structp; ++typedef png_struct * * png_structpp; ++ ++/* png_info contains information read from or to be written to a PNG file. One ++ * or more of these must exist while reading or creating a PNG file. The ++ * information is not used by libpng during read but is used to control what ++ * gets written when a PNG file is created. "png_get_" function calls read ++ * information during read and "png_set_" functions calls write information ++ * when creating a PNG. ++ * been moved into a separate header file that is not accessible to ++ * applications. Read libpng-manual.txt or libpng.3 for more info. ++ */ ++typedef struct png_info_def png_info; ++typedef png_info * png_infop; ++typedef const png_info * png_const_infop; ++typedef png_info * * png_infopp; ++ ++/* Types with names ending 'p' are pointer types. The corresponding types with ++ * names ending 'rp' are identical pointer types except that the pointer is ++ * marked 'restrict', which means that it is the only pointer to the object ++ * passed to the function. Applications should not use the 'restrict' types; ++ * it is always valid to pass 'p' to a pointer with a function argument of the ++ * corresponding 'rp' type. Different compilers have different rules with ++ * regard to type matching in the presence of 'restrict'. For backward ++ * compatibility libpng callbacks never have 'restrict' in their parameters and, ++ * consequentially, writing portable application code is extremely difficult if ++ * an attempt is made to use 'restrict'. ++ */ ++typedef png_struct * PNG_RESTRICT png_structrp; ++typedef const png_struct * PNG_RESTRICT png_const_structrp; ++typedef png_info * PNG_RESTRICT png_inforp; ++typedef const png_info * PNG_RESTRICT png_const_inforp; ++ ++/* Three color definitions. The order of the red, green, and blue, (and the ++ * exact size) is not important, although the size of the fields need to ++ * be png_byte or png_uint_16 (as defined below). ++ */ ++typedef struct png_color_struct ++{ ++ png_byte red; ++ png_byte green; ++ png_byte blue; ++} png_color; ++typedef png_color * png_colorp; ++typedef const png_color * png_const_colorp; ++typedef png_color * * png_colorpp; ++ ++typedef struct png_color_16_struct ++{ ++ png_byte index; /* used for palette files */ ++ png_uint_16 red; /* for use in red green blue files */ ++ png_uint_16 green; ++ png_uint_16 blue; ++ png_uint_16 gray; /* for use in grayscale files */ ++} png_color_16; ++typedef png_color_16 * png_color_16p; ++typedef const png_color_16 * png_const_color_16p; ++typedef png_color_16 * * png_color_16pp; ++ ++typedef struct png_color_8_struct ++{ ++ png_byte red; /* for use in red green blue files */ ++ png_byte green; ++ png_byte blue; ++ png_byte gray; /* for use in grayscale files */ ++ png_byte alpha; /* for alpha channel files */ ++} png_color_8; ++typedef png_color_8 * png_color_8p; ++typedef const png_color_8 * png_const_color_8p; ++typedef png_color_8 * * png_color_8pp; ++ ++/* ++ * The following two structures are used for the in-core representation ++ * of sPLT chunks. ++ */ ++typedef struct png_sPLT_entry_struct ++{ ++ png_uint_16 red; ++ png_uint_16 green; ++ png_uint_16 blue; ++ png_uint_16 alpha; ++ png_uint_16 frequency; ++} png_sPLT_entry; ++typedef png_sPLT_entry * png_sPLT_entryp; ++typedef const png_sPLT_entry * png_const_sPLT_entryp; ++typedef png_sPLT_entry * * png_sPLT_entrypp; ++ ++/* When the depth of the sPLT palette is 8 bits, the color and alpha samples ++ * occupy the LSB of their respective members, and the MSB of each member ++ * is zero-filled. The frequency member always occupies the full 16 bits. ++ */ ++ ++typedef struct png_sPLT_struct ++{ ++ png_charp name; /* palette name */ ++ png_byte depth; /* depth of palette samples */ ++ png_sPLT_entryp entries; /* palette entries */ ++ png_int_32 nentries; /* number of palette entries */ ++} png_sPLT_t; ++typedef png_sPLT_t * png_sPLT_tp; ++typedef const png_sPLT_t * png_const_sPLT_tp; ++typedef png_sPLT_t * * png_sPLT_tpp; ++ ++#ifdef PNG_TEXT_SUPPORTED ++/* png_text holds the contents of a text/ztxt/itxt chunk in a PNG file, ++ * and whether that contents is compressed or not. The "key" field ++ * points to a regular zero-terminated C string. The "text" fields can be a ++ * regular C string, an empty string, or a NULL pointer. ++ * However, the structure returned by png_get_text() will always contain ++ * the "text" field as a regular zero-terminated C string (possibly ++ * empty), never a NULL pointer, so it can be safely used in printf() and ++ * other string-handling functions. Note that the "itxt_length", "lang", and ++ * "lang_key" members of the structure only exist when the library is built ++ * with iTXt chunk support. Prior to libpng-1.4.0 the library was built by ++ * default without iTXt support. Also note that when iTXt *is* supported, ++ * the "lang" and "lang_key" fields contain NULL pointers when the ++ * "compression" field contains * PNG_TEXT_COMPRESSION_NONE or ++ * PNG_TEXT_COMPRESSION_zTXt. Note that the "compression value" is not the ++ * same as what appears in the PNG tEXt/zTXt/iTXt chunk's "compression flag" ++ * which is always 0 or 1, or its "compression method" which is always 0. ++ */ ++typedef struct png_text_struct ++{ ++ int compression; /* compression value: ++ -1: tEXt, none ++ 0: zTXt, deflate ++ 1: iTXt, none ++ 2: iTXt, deflate */ ++ png_charp key; /* keyword, 1-79 character description of "text" */ ++ png_charp text; /* comment, may be an empty string (ie "") ++ or a NULL pointer */ ++ size_t text_length; /* length of the text string */ ++ size_t itxt_length; /* length of the itxt string */ ++ png_charp lang; /* language code, 0-79 characters ++ or a NULL pointer */ ++ png_charp lang_key; /* keyword translated UTF-8 string, 0 or more ++ chars or a NULL pointer */ ++} png_text; ++typedef png_text * png_textp; ++typedef const png_text * png_const_textp; ++typedef png_text * * png_textpp; ++#endif ++ ++/* Supported compression types for text in PNG files (tEXt, and zTXt). ++ * The values of the PNG_TEXT_COMPRESSION_ defines should NOT be changed. */ ++#define PNG_TEXT_COMPRESSION_NONE_WR -3 ++#define PNG_TEXT_COMPRESSION_zTXt_WR -2 ++#define PNG_TEXT_COMPRESSION_NONE -1 ++#define PNG_TEXT_COMPRESSION_zTXt 0 ++#define PNG_ITXT_COMPRESSION_NONE 1 ++#define PNG_ITXT_COMPRESSION_zTXt 2 ++#define PNG_TEXT_COMPRESSION_LAST 3 /* Not a valid value */ ++ ++/* png_time is a way to hold the time in an machine independent way. ++ * Two conversions are provided, both from time_t and struct tm. There ++ * is no portable way to convert to either of these structures, as far ++ * as I know. If you know of a portable way, send it to me. As a side ++ * note - PNG has always been Year 2000 compliant! ++ */ ++typedef struct png_time_struct ++{ ++ png_uint_16 year; /* full year, as in, 1995 */ ++ png_byte month; /* month of year, 1 - 12 */ ++ png_byte day; /* day of month, 1 - 31 */ ++ png_byte hour; /* hour of day, 0 - 23 */ ++ png_byte minute; /* minute of hour, 0 - 59 */ ++ png_byte second; /* second of minute, 0 - 60 (for leap seconds) */ ++} png_time; ++typedef png_time * png_timep; ++typedef const png_time * png_const_timep; ++typedef png_time * * png_timepp; ++ ++#if defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED) ||\ ++ defined(PNG_USER_CHUNKS_SUPPORTED) ++/* png_unknown_chunk is a structure to hold queued chunks for which there is ++ * no specific support. The idea is that we can use this to queue ++ * up private chunks for output even though the library doesn't actually ++ * know about their semantics. ++ * ++ * The data in the structure is set by libpng on read and used on write. ++ */ ++typedef struct png_unknown_chunk_t ++{ ++ png_byte name[5]; /* Textual chunk name with '\0' terminator */ ++ png_byte *data; /* Data, should not be modified on read! */ ++ size_t size; ++ ++ /* On write 'location' must be set using the flag values listed below. ++ * Notice that on read it is set by libpng however the values stored have ++ * more bits set than are listed below. Always treat the value as a ++ * bitmask. On write set only one bit - setting multiple bits may cause the ++ * chunk to be written in multiple places. ++ */ ++ png_byte location; /* mode of operation at read time */ ++} ++png_unknown_chunk; ++ ++typedef png_unknown_chunk * png_unknown_chunkp; ++typedef const png_unknown_chunk * png_const_unknown_chunkp; ++typedef png_unknown_chunk * * png_unknown_chunkpp; ++#endif ++ ++/* Flag values for the unknown chunk location byte. */ ++#define PNG_HAVE_IHDR 0x01 ++#define PNG_HAVE_PLTE 0x02 ++#define PNG_AFTER_IDAT 0x08 ++ ++/* Maximum positive integer used in PNG is (2^31)-1 */ ++#define PNG_UINT_31_MAX ((png_uint_32)0x7fffffffL) ++#define PNG_UINT_32_MAX ((png_uint_32)(-1)) ++#define PNG_SIZE_MAX ((size_t)(-1)) ++ ++/* These are constants for fixed point values encoded in the ++ * PNG specification manner (x100000) ++ */ ++#define PNG_FP_1 100000 ++#define PNG_FP_HALF 50000 ++#define PNG_FP_MAX ((png_fixed_point)0x7fffffffL) ++#define PNG_FP_MIN (-PNG_FP_MAX) ++ ++/* These describe the color_type field in png_info. */ ++/* color type masks */ ++#define PNG_COLOR_MASK_PALETTE 1 ++#define PNG_COLOR_MASK_COLOR 2 ++#define PNG_COLOR_MASK_ALPHA 4 ++ ++/* color types. Note that not all combinations are legal */ ++#define PNG_COLOR_TYPE_GRAY 0 ++#define PNG_COLOR_TYPE_PALETTE (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE) ++#define PNG_COLOR_TYPE_RGB (PNG_COLOR_MASK_COLOR) ++#define PNG_COLOR_TYPE_RGB_ALPHA (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_ALPHA) ++#define PNG_COLOR_TYPE_GRAY_ALPHA (PNG_COLOR_MASK_ALPHA) ++/* aliases */ ++#define PNG_COLOR_TYPE_RGBA PNG_COLOR_TYPE_RGB_ALPHA ++#define PNG_COLOR_TYPE_GA PNG_COLOR_TYPE_GRAY_ALPHA ++ ++/* This is for compression type. PNG 1.0-1.2 only define the single type. */ ++#define PNG_COMPRESSION_TYPE_BASE 0 /* Deflate method 8, 32K window */ ++#define PNG_COMPRESSION_TYPE_DEFAULT PNG_COMPRESSION_TYPE_BASE ++ ++/* This is for filter type. PNG 1.0-1.2 only define the single type. */ ++#define PNG_FILTER_TYPE_BASE 0 /* Single row per-byte filtering */ ++#define PNG_INTRAPIXEL_DIFFERENCING 64 /* Used only in MNG datastreams */ ++#define PNG_FILTER_TYPE_DEFAULT PNG_FILTER_TYPE_BASE ++ ++/* These are for the interlacing type. These values should NOT be changed. */ ++#define PNG_INTERLACE_NONE 0 /* Non-interlaced image */ ++#define PNG_INTERLACE_ADAM7 1 /* Adam7 interlacing */ ++#define PNG_INTERLACE_LAST 2 /* Not a valid value */ ++ ++/* These are for the oFFs chunk. These values should NOT be changed. */ ++#define PNG_OFFSET_PIXEL 0 /* Offset in pixels */ ++#define PNG_OFFSET_MICROMETER 1 /* Offset in micrometers (1/10^6 meter) */ ++#define PNG_OFFSET_LAST 2 /* Not a valid value */ ++ ++/* These are for the pCAL chunk. These values should NOT be changed. */ ++#define PNG_EQUATION_LINEAR 0 /* Linear transformation */ ++#define PNG_EQUATION_BASE_E 1 /* Exponential base e transform */ ++#define PNG_EQUATION_ARBITRARY 2 /* Arbitrary base exponential transform */ ++#define PNG_EQUATION_HYPERBOLIC 3 /* Hyperbolic sine transformation */ ++#define PNG_EQUATION_LAST 4 /* Not a valid value */ ++ ++/* These are for the sCAL chunk. These values should NOT be changed. */ ++#define PNG_SCALE_UNKNOWN 0 /* unknown unit (image scale) */ ++#define PNG_SCALE_METER 1 /* meters per pixel */ ++#define PNG_SCALE_RADIAN 2 /* radians per pixel */ ++#define PNG_SCALE_LAST 3 /* Not a valid value */ ++ ++/* These are for the pHYs chunk. These values should NOT be changed. */ ++#define PNG_RESOLUTION_UNKNOWN 0 /* pixels/unknown unit (aspect ratio) */ ++#define PNG_RESOLUTION_METER 1 /* pixels/meter */ ++#define PNG_RESOLUTION_LAST 2 /* Not a valid value */ ++ ++/* These are for the sRGB chunk. These values should NOT be changed. */ ++#define PNG_sRGB_INTENT_PERCEPTUAL 0 ++#define PNG_sRGB_INTENT_RELATIVE 1 ++#define PNG_sRGB_INTENT_SATURATION 2 ++#define PNG_sRGB_INTENT_ABSOLUTE 3 ++#define PNG_sRGB_INTENT_LAST 4 /* Not a valid value */ ++ ++/* This is for text chunks */ ++#define PNG_KEYWORD_MAX_LENGTH 79 ++ ++/* Maximum number of entries in PLTE/sPLT/tRNS arrays */ ++#define PNG_MAX_PALETTE_LENGTH 256 ++ ++/* These determine if an ancillary chunk's data has been successfully read ++ * from the PNG header, or if the application has filled in the corresponding ++ * data in the info_struct to be written into the output file. The values ++ * of the PNG_INFO_ defines should NOT be changed. ++ */ ++#define PNG_INFO_gAMA 0x0001U ++#define PNG_INFO_sBIT 0x0002U ++#define PNG_INFO_cHRM 0x0004U ++#define PNG_INFO_PLTE 0x0008U ++#define PNG_INFO_tRNS 0x0010U ++#define PNG_INFO_bKGD 0x0020U ++#define PNG_INFO_hIST 0x0040U ++#define PNG_INFO_pHYs 0x0080U ++#define PNG_INFO_oFFs 0x0100U ++#define PNG_INFO_tIME 0x0200U ++#define PNG_INFO_pCAL 0x0400U ++#define PNG_INFO_sRGB 0x0800U /* GR-P, 0.96a */ ++#define PNG_INFO_iCCP 0x1000U /* ESR, 1.0.6 */ ++#define PNG_INFO_sPLT 0x2000U /* ESR, 1.0.6 */ ++#define PNG_INFO_sCAL 0x4000U /* ESR, 1.0.6 */ ++#define PNG_INFO_IDAT 0x8000U /* ESR, 1.0.6 */ ++#define PNG_INFO_eXIf 0x10000U /* GR-P, 1.6.31 */ ++ ++/* This is used for the transformation routines, as some of them ++ * change these values for the row. It also should enable using ++ * the routines for other purposes. ++ */ ++typedef struct png_row_info_struct ++{ ++ png_uint_32 width; /* width of row */ ++ size_t rowbytes; /* number of bytes in row */ ++ png_byte color_type; /* color type of row */ ++ png_byte bit_depth; /* bit depth of row */ ++ png_byte channels; /* number of channels (1, 2, 3, or 4) */ ++ png_byte pixel_depth; /* bits per pixel (depth * channels) */ ++} png_row_info; ++ ++typedef png_row_info * png_row_infop; ++typedef png_row_info * * png_row_infopp; ++ ++/* These are the function types for the I/O functions and for the functions ++ * that allow the user to override the default I/O functions with his or her ++ * own. The png_error_ptr type should match that of user-supplied warning ++ * and error functions, while the png_rw_ptr type should match that of the ++ * user read/write data functions. Note that the 'write' function must not ++ * modify the buffer it is passed. The 'read' function, on the other hand, is ++ * expected to return the read data in the buffer. ++ */ ++typedef PNG_CALLBACK(void, *png_error_ptr, (png_structp, png_const_charp)); ++typedef PNG_CALLBACK(void, *png_rw_ptr, (png_structp, png_bytep, size_t)); ++typedef PNG_CALLBACK(void, *png_flush_ptr, (png_structp)); ++typedef PNG_CALLBACK(void, *png_read_status_ptr, (png_structp, png_uint_32, ++ int)); ++typedef PNG_CALLBACK(void, *png_write_status_ptr, (png_structp, png_uint_32, ++ int)); ++ ++#ifdef PNG_PROGRESSIVE_READ_SUPPORTED ++typedef PNG_CALLBACK(void, *png_progressive_info_ptr, (png_structp, png_infop)); ++typedef PNG_CALLBACK(void, *png_progressive_end_ptr, (png_structp, png_infop)); ++ ++/* The following callback receives png_uint_32 row_number, int pass for the ++ * png_bytep data of the row. When transforming an interlaced image the ++ * row number is the row number within the sub-image of the interlace pass, so ++ * the value will increase to the height of the sub-image (not the full image) ++ * then reset to 0 for the next pass. ++ * ++ * Use PNG_ROW_FROM_PASS_ROW(row, pass) and PNG_COL_FROM_PASS_COL(col, pass) to ++ * find the output pixel (x,y) given an interlaced sub-image pixel ++ * (row,col,pass). (See below for these macros.) ++ */ ++typedef PNG_CALLBACK(void, *png_progressive_row_ptr, (png_structp, png_bytep, ++ png_uint_32, int)); ++#endif ++ ++#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \ ++ defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) ++typedef PNG_CALLBACK(void, *png_user_transform_ptr, (png_structp, png_row_infop, ++ png_bytep)); ++#endif ++ ++#ifdef PNG_USER_CHUNKS_SUPPORTED ++typedef PNG_CALLBACK(int, *png_user_chunk_ptr, (png_structp, ++ png_unknown_chunkp)); ++#endif ++#ifdef PNG_UNKNOWN_CHUNKS_SUPPORTED ++/* not used anywhere */ ++/* typedef PNG_CALLBACK(void, *png_unknown_chunk_ptr, (png_structp)); */ ++#endif ++ ++#ifdef PNG_SETJMP_SUPPORTED ++/* This must match the function definition in , and the application ++ * must include this before png.h to obtain the definition of jmp_buf. The ++ * function is required to be PNG_NORETURN, but this is not checked. If the ++ * function does return the application will crash via an abort() or similar ++ * system level call. ++ * ++ * If you get a warning here while building the library you may need to make ++ * changes to ensure that pnglibconf.h records the calling convention used by ++ * your compiler. This may be very difficult - try using a different compiler ++ * to build the library! ++ */ ++PNG_FUNCTION(void, (PNGCAPI *png_longjmp_ptr), PNGARG((jmp_buf, int)), typedef); ++#endif ++ ++/* Transform masks for the high-level interface */ ++#define PNG_TRANSFORM_IDENTITY 0x0000 /* read and write */ ++#define PNG_TRANSFORM_STRIP_16 0x0001 /* read only */ ++#define PNG_TRANSFORM_STRIP_ALPHA 0x0002 /* read only */ ++#define PNG_TRANSFORM_PACKING 0x0004 /* read and write */ ++#define PNG_TRANSFORM_PACKSWAP 0x0008 /* read and write */ ++#define PNG_TRANSFORM_EXPAND 0x0010 /* read only */ ++#define PNG_TRANSFORM_INVERT_MONO 0x0020 /* read and write */ ++#define PNG_TRANSFORM_SHIFT 0x0040 /* read and write */ ++#define PNG_TRANSFORM_BGR 0x0080 /* read and write */ ++#define PNG_TRANSFORM_SWAP_ALPHA 0x0100 /* read and write */ ++#define PNG_TRANSFORM_SWAP_ENDIAN 0x0200 /* read and write */ ++#define PNG_TRANSFORM_INVERT_ALPHA 0x0400 /* read and write */ ++#define PNG_TRANSFORM_STRIP_FILLER 0x0800 /* write only */ ++/* Added to libpng-1.2.34 */ ++#define PNG_TRANSFORM_STRIP_FILLER_BEFORE PNG_TRANSFORM_STRIP_FILLER ++#define PNG_TRANSFORM_STRIP_FILLER_AFTER 0x1000 /* write only */ ++/* Added to libpng-1.4.0 */ ++#define PNG_TRANSFORM_GRAY_TO_RGB 0x2000 /* read only */ ++/* Added to libpng-1.5.4 */ ++#define PNG_TRANSFORM_EXPAND_16 0x4000 /* read only */ ++#if INT_MAX >= 0x8000 /* else this might break */ ++#define PNG_TRANSFORM_SCALE_16 0x8000 /* read only */ ++#endif ++ ++/* Flags for MNG supported features */ ++#define PNG_FLAG_MNG_EMPTY_PLTE 0x01 ++#define PNG_FLAG_MNG_FILTER_64 0x04 ++#define PNG_ALL_MNG_FEATURES 0x05 ++ ++/* NOTE: prior to 1.5 these functions had no 'API' style declaration, ++ * this allowed the zlib default functions to be used on Windows ++ * platforms. In 1.5 the zlib default malloc (which just calls malloc and ++ * ignores the first argument) should be completely compatible with the ++ * following. ++ */ ++typedef PNG_CALLBACK(png_voidp, *png_malloc_ptr, (png_structp, ++ png_alloc_size_t)); ++typedef PNG_CALLBACK(void, *png_free_ptr, (png_structp, png_voidp)); ++ ++/* Section 4: exported functions ++ * Here are the function definitions most commonly used. This is not ++ * the place to find out how to use libpng. See libpng-manual.txt for the ++ * full explanation, see example.c for the summary. This just provides ++ * a simple one line description of the use of each function. ++ * ++ * The PNG_EXPORT() and PNG_EXPORTA() macros used below are defined in ++ * pngconf.h and in the *.dfn files in the scripts directory. ++ * ++ * PNG_EXPORT(ordinal, type, name, (args)); ++ * ++ * ordinal: ordinal that is used while building ++ * *.def files. The ordinal value is only ++ * relevant when preprocessing png.h with ++ * the *.dfn files for building symbol table ++ * entries, and are removed by pngconf.h. ++ * type: return type of the function ++ * name: function name ++ * args: function arguments, with types ++ * ++ * When we wish to append attributes to a function prototype we use ++ * the PNG_EXPORTA() macro instead. ++ * ++ * PNG_EXPORTA(ordinal, type, name, (args), attributes); ++ * ++ * ordinal, type, name, and args: same as in PNG_EXPORT(). ++ * attributes: function attributes ++ */ ++ ++/* Returns the version number of the library */ ++PNG_EXPORT(1, png_uint_32, png_access_version_number, (void)); ++ ++/* Tell lib we have already handled the first magic bytes. ++ * Handling more than 8 bytes from the beginning of the file is an error. ++ */ ++PNG_EXPORT(2, void, png_set_sig_bytes, (png_structrp png_ptr, int num_bytes)); ++ ++/* Check sig[start] through sig[start + num_to_check - 1] to see if it's a ++ * PNG file. Returns zero if the supplied bytes match the 8-byte PNG ++ * signature, and non-zero otherwise. Having num_to_check == 0 or ++ * start > 7 will always fail (ie return non-zero). ++ */ ++PNG_EXPORT(3, int, png_sig_cmp, (png_const_bytep sig, size_t start, ++ size_t num_to_check)); ++ ++/* Simple signature checking function. This is the same as calling ++ * png_check_sig(sig, n) := !png_sig_cmp(sig, 0, n). ++ */ ++#define png_check_sig(sig, n) !png_sig_cmp((sig), 0, (n)) ++ ++/* Allocate and initialize png_ptr struct for reading, and any other memory. */ ++PNG_EXPORTA(4, png_structp, png_create_read_struct, ++ (png_const_charp user_png_ver, png_voidp error_ptr, ++ png_error_ptr error_fn, png_error_ptr warn_fn), ++ PNG_ALLOCATED); ++ ++/* Allocate and initialize png_ptr struct for writing, and any other memory */ ++PNG_EXPORTA(5, png_structp, png_create_write_struct, ++ (png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, ++ png_error_ptr warn_fn), ++ PNG_ALLOCATED); ++ ++PNG_EXPORT(6, size_t, png_get_compression_buffer_size, ++ (png_const_structrp png_ptr)); ++ ++PNG_EXPORT(7, void, png_set_compression_buffer_size, (png_structrp png_ptr, ++ size_t size)); ++ ++/* Moved from pngconf.h in 1.4.0 and modified to ensure setjmp/longjmp ++ * match up. ++ */ ++#ifdef PNG_SETJMP_SUPPORTED ++/* This function returns the jmp_buf built in to *png_ptr. It must be ++ * supplied with an appropriate 'longjmp' function to use on that jmp_buf ++ * unless the default error function is overridden in which case NULL is ++ * acceptable. The size of the jmp_buf is checked against the actual size ++ * allocated by the library - the call will return NULL on a mismatch ++ * indicating an ABI mismatch. ++ */ ++PNG_EXPORT(8, jmp_buf*, png_set_longjmp_fn, (png_structrp png_ptr, ++ png_longjmp_ptr longjmp_fn, size_t jmp_buf_size)); ++# define png_jmpbuf(png_ptr) \ ++ (*png_set_longjmp_fn((png_ptr), longjmp, (sizeof (jmp_buf)))) ++#else ++# define png_jmpbuf(png_ptr) \ ++ (LIBPNG_WAS_COMPILED_WITH__PNG_NO_SETJMP) ++#endif ++/* This function should be used by libpng applications in place of ++ * longjmp(png_ptr->jmpbuf, val). If longjmp_fn() has been set, it ++ * will use it; otherwise it will call PNG_ABORT(). This function was ++ * added in libpng-1.5.0. ++ */ ++PNG_EXPORTA(9, void, png_longjmp, (png_const_structrp png_ptr, int val), ++ PNG_NORETURN); ++ ++#ifdef PNG_READ_SUPPORTED ++/* Reset the compression stream */ ++PNG_EXPORTA(10, int, png_reset_zstream, (png_structrp png_ptr), PNG_DEPRECATED); ++#endif ++ ++/* New functions added in libpng-1.0.2 (not enabled by default until 1.2.0) */ ++#ifdef PNG_USER_MEM_SUPPORTED ++PNG_EXPORTA(11, png_structp, png_create_read_struct_2, ++ (png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, ++ png_error_ptr warn_fn, ++ png_voidp mem_ptr, png_malloc_ptr malloc_fn, png_free_ptr free_fn), ++ PNG_ALLOCATED); ++PNG_EXPORTA(12, png_structp, png_create_write_struct_2, ++ (png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, ++ png_error_ptr warn_fn, ++ png_voidp mem_ptr, png_malloc_ptr malloc_fn, png_free_ptr free_fn), ++ PNG_ALLOCATED); ++#endif ++ ++/* Write the PNG file signature. */ ++PNG_EXPORT(13, void, png_write_sig, (png_structrp png_ptr)); ++ ++/* Write a PNG chunk - size, type, (optional) data, CRC. */ ++PNG_EXPORT(14, void, png_write_chunk, (png_structrp png_ptr, png_const_bytep ++ chunk_name, png_const_bytep data, size_t length)); ++ ++/* Write the start of a PNG chunk - length and chunk name. */ ++PNG_EXPORT(15, void, png_write_chunk_start, (png_structrp png_ptr, ++ png_const_bytep chunk_name, png_uint_32 length)); ++ ++/* Write the data of a PNG chunk started with png_write_chunk_start(). */ ++PNG_EXPORT(16, void, png_write_chunk_data, (png_structrp png_ptr, ++ png_const_bytep data, size_t length)); ++ ++/* Finish a chunk started with png_write_chunk_start() (includes CRC). */ ++PNG_EXPORT(17, void, png_write_chunk_end, (png_structrp png_ptr)); ++ ++/* Allocate and initialize the info structure */ ++PNG_EXPORTA(18, png_infop, png_create_info_struct, (png_const_structrp png_ptr), ++ PNG_ALLOCATED); ++ ++/* DEPRECATED: this function allowed init structures to be created using the ++ * default allocation method (typically malloc). Use is deprecated in 1.6.0 and ++ * the API will be removed in the future. ++ */ ++PNG_EXPORTA(19, void, png_info_init_3, (png_infopp info_ptr, ++ size_t png_info_struct_size), PNG_DEPRECATED); ++ ++/* Writes all the PNG information before the image. */ ++PNG_EXPORT(20, void, png_write_info_before_PLTE, ++ (png_structrp png_ptr, png_const_inforp info_ptr)); ++PNG_EXPORT(21, void, png_write_info, ++ (png_structrp png_ptr, png_const_inforp info_ptr)); ++ ++#ifdef PNG_SEQUENTIAL_READ_SUPPORTED ++/* Read the information before the actual image data. */ ++PNG_EXPORT(22, void, png_read_info, ++ (png_structrp png_ptr, png_inforp info_ptr)); ++#endif ++ ++#ifdef PNG_TIME_RFC1123_SUPPORTED ++ /* Convert to a US string format: there is no localization support in this ++ * routine. The original implementation used a 29 character buffer in ++ * png_struct, this will be removed in future versions. ++ */ ++#if PNG_LIBPNG_VER < 10700 ++/* To do: remove this from libpng17 (and from libpng17/png.c and pngstruct.h) */ ++PNG_EXPORTA(23, png_const_charp, png_convert_to_rfc1123, (png_structrp png_ptr, ++ png_const_timep ptime),PNG_DEPRECATED); ++#endif ++PNG_EXPORT(241, int, png_convert_to_rfc1123_buffer, (char out[29], ++ png_const_timep ptime)); ++#endif ++ ++#ifdef PNG_CONVERT_tIME_SUPPORTED ++/* Convert from a struct tm to png_time */ ++PNG_EXPORT(24, void, png_convert_from_struct_tm, (png_timep ptime, ++ const struct tm * ttime)); ++ ++/* Convert from time_t to png_time. Uses gmtime() */ ++PNG_EXPORT(25, void, png_convert_from_time_t, (png_timep ptime, time_t ttime)); ++#endif /* CONVERT_tIME */ ++ ++#ifdef PNG_READ_EXPAND_SUPPORTED ++/* Expand data to 24-bit RGB, or 8-bit grayscale, with alpha if available. */ ++PNG_EXPORT(26, void, png_set_expand, (png_structrp png_ptr)); ++PNG_EXPORT(27, void, png_set_expand_gray_1_2_4_to_8, (png_structrp png_ptr)); ++PNG_EXPORT(28, void, png_set_palette_to_rgb, (png_structrp png_ptr)); ++PNG_EXPORT(29, void, png_set_tRNS_to_alpha, (png_structrp png_ptr)); ++#endif ++ ++#ifdef PNG_READ_EXPAND_16_SUPPORTED ++/* Expand to 16-bit channels, forces conversion of palette to RGB and expansion ++ * of a tRNS chunk if present. ++ */ ++PNG_EXPORT(221, void, png_set_expand_16, (png_structrp png_ptr)); ++#endif ++ ++#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED) ++/* Use blue, green, red order for pixels. */ ++PNG_EXPORT(30, void, png_set_bgr, (png_structrp png_ptr)); ++#endif ++ ++#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED ++/* Expand the grayscale to 24-bit RGB if necessary. */ ++PNG_EXPORT(31, void, png_set_gray_to_rgb, (png_structrp png_ptr)); ++#endif ++ ++#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED ++/* Reduce RGB to grayscale. */ ++#define PNG_ERROR_ACTION_NONE 1 ++#define PNG_ERROR_ACTION_WARN 2 ++#define PNG_ERROR_ACTION_ERROR 3 ++#define PNG_RGB_TO_GRAY_DEFAULT (-1)/*for red/green coefficients*/ ++ ++PNG_FP_EXPORT(32, void, png_set_rgb_to_gray, (png_structrp png_ptr, ++ int error_action, double red, double green)) ++PNG_FIXED_EXPORT(33, void, png_set_rgb_to_gray_fixed, (png_structrp png_ptr, ++ int error_action, png_fixed_point red, png_fixed_point green)) ++ ++PNG_EXPORT(34, png_byte, png_get_rgb_to_gray_status, (png_const_structrp ++ png_ptr)); ++#endif ++ ++#ifdef PNG_BUILD_GRAYSCALE_PALETTE_SUPPORTED ++PNG_EXPORT(35, void, png_build_grayscale_palette, (int bit_depth, ++ png_colorp palette)); ++#endif ++ ++#ifdef PNG_READ_ALPHA_MODE_SUPPORTED ++/* How the alpha channel is interpreted - this affects how the color channels ++ * of a PNG file are returned to the calling application when an alpha channel, ++ * or a tRNS chunk in a palette file, is present. ++ * ++ * This has no effect on the way pixels are written into a PNG output ++ * datastream. The color samples in a PNG datastream are never premultiplied ++ * with the alpha samples. ++ * ++ * The default is to return data according to the PNG specification: the alpha ++ * channel is a linear measure of the contribution of the pixel to the ++ * corresponding composited pixel, and the color channels are unassociated ++ * (not premultiplied). The gamma encoded color channels must be scaled ++ * according to the contribution and to do this it is necessary to undo ++ * the encoding, scale the color values, perform the composition and re-encode ++ * the values. This is the 'PNG' mode. ++ * ++ * The alternative is to 'associate' the alpha with the color information by ++ * storing color channel values that have been scaled by the alpha. ++ * image. These are the 'STANDARD', 'ASSOCIATED' or 'PREMULTIPLIED' modes ++ * (the latter being the two common names for associated alpha color channels). ++ * ++ * For the 'OPTIMIZED' mode, a pixel is treated as opaque only if the alpha ++ * value is equal to the maximum value. ++ * ++ * The final choice is to gamma encode the alpha channel as well. This is ++ * broken because, in practice, no implementation that uses this choice ++ * correctly undoes the encoding before handling alpha composition. Use this ++ * choice only if other serious errors in the software or hardware you use ++ * mandate it; the typical serious error is for dark halos to appear around ++ * opaque areas of the composited PNG image because of arithmetic overflow. ++ * ++ * The API function png_set_alpha_mode specifies which of these choices to use ++ * with an enumerated 'mode' value and the gamma of the required output: ++ */ ++#define PNG_ALPHA_PNG 0 /* according to the PNG standard */ ++#define PNG_ALPHA_STANDARD 1 /* according to Porter/Duff */ ++#define PNG_ALPHA_ASSOCIATED 1 /* as above; this is the normal practice */ ++#define PNG_ALPHA_PREMULTIPLIED 1 /* as above */ ++#define PNG_ALPHA_OPTIMIZED 2 /* 'PNG' for opaque pixels, else 'STANDARD' */ ++#define PNG_ALPHA_BROKEN 3 /* the alpha channel is gamma encoded */ ++ ++PNG_FP_EXPORT(227, void, png_set_alpha_mode, (png_structrp png_ptr, int mode, ++ double output_gamma)) ++PNG_FIXED_EXPORT(228, void, png_set_alpha_mode_fixed, (png_structrp png_ptr, ++ int mode, png_fixed_point output_gamma)) ++#endif ++ ++#if defined(PNG_GAMMA_SUPPORTED) || defined(PNG_READ_ALPHA_MODE_SUPPORTED) ++/* The output_gamma value is a screen gamma in libpng terminology: it expresses ++ * how to decode the output values, not how they are encoded. ++ */ ++#define PNG_DEFAULT_sRGB -1 /* sRGB gamma and color space */ ++#define PNG_GAMMA_MAC_18 -2 /* Old Mac '1.8' gamma and color space */ ++#define PNG_GAMMA_sRGB 220000 /* Television standards--matches sRGB gamma */ ++#define PNG_GAMMA_LINEAR PNG_FP_1 /* Linear */ ++#endif ++ ++/* The following are examples of calls to png_set_alpha_mode to achieve the ++ * required overall gamma correction and, where necessary, alpha ++ * premultiplication. ++ * ++ * png_set_alpha_mode(pp, PNG_ALPHA_PNG, PNG_DEFAULT_sRGB); ++ * This is the default libpng handling of the alpha channel - it is not ++ * pre-multiplied into the color components. In addition the call states ++ * that the output is for a sRGB system and causes all PNG files without gAMA ++ * chunks to be assumed to be encoded using sRGB. ++ * ++ * png_set_alpha_mode(pp, PNG_ALPHA_PNG, PNG_GAMMA_MAC); ++ * In this case the output is assumed to be something like an sRGB conformant ++ * display preceded by a power-law lookup table of power 1.45. This is how ++ * early Mac systems behaved. ++ * ++ * png_set_alpha_mode(pp, PNG_ALPHA_STANDARD, PNG_GAMMA_LINEAR); ++ * This is the classic Jim Blinn approach and will work in academic ++ * environments where everything is done by the book. It has the shortcoming ++ * of assuming that input PNG data with no gamma information is linear - this ++ * is unlikely to be correct unless the PNG files where generated locally. ++ * Most of the time the output precision will be so low as to show ++ * significant banding in dark areas of the image. ++ * ++ * png_set_expand_16(pp); ++ * png_set_alpha_mode(pp, PNG_ALPHA_STANDARD, PNG_DEFAULT_sRGB); ++ * This is a somewhat more realistic Jim Blinn inspired approach. PNG files ++ * are assumed to have the sRGB encoding if not marked with a gamma value and ++ * the output is always 16 bits per component. This permits accurate scaling ++ * and processing of the data. If you know that your input PNG files were ++ * generated locally you might need to replace PNG_DEFAULT_sRGB with the ++ * correct value for your system. ++ * ++ * png_set_alpha_mode(pp, PNG_ALPHA_OPTIMIZED, PNG_DEFAULT_sRGB); ++ * If you just need to composite the PNG image onto an existing background ++ * and if you control the code that does this you can use the optimization ++ * setting. In this case you just copy completely opaque pixels to the ++ * output. For pixels that are not completely transparent (you just skip ++ * those) you do the composition math using png_composite or png_composite_16 ++ * below then encode the resultant 8-bit or 16-bit values to match the output ++ * encoding. ++ * ++ * Other cases ++ * If neither the PNG nor the standard linear encoding work for you because ++ * of the software or hardware you use then you have a big problem. The PNG ++ * case will probably result in halos around the image. The linear encoding ++ * will probably result in a washed out, too bright, image (it's actually too ++ * contrasty.) Try the ALPHA_OPTIMIZED mode above - this will probably ++ * substantially reduce the halos. Alternatively try: ++ * ++ * png_set_alpha_mode(pp, PNG_ALPHA_BROKEN, PNG_DEFAULT_sRGB); ++ * This option will also reduce the halos, but there will be slight dark ++ * halos round the opaque parts of the image where the background is light. ++ * In the OPTIMIZED mode the halos will be light halos where the background ++ * is dark. Take your pick - the halos are unavoidable unless you can get ++ * your hardware/software fixed! (The OPTIMIZED approach is slightly ++ * faster.) ++ * ++ * When the default gamma of PNG files doesn't match the output gamma. ++ * If you have PNG files with no gamma information png_set_alpha_mode allows ++ * you to provide a default gamma, but it also sets the output gamma to the ++ * matching value. If you know your PNG files have a gamma that doesn't ++ * match the output you can take advantage of the fact that ++ * png_set_alpha_mode always sets the output gamma but only sets the PNG ++ * default if it is not already set: ++ * ++ * png_set_alpha_mode(pp, PNG_ALPHA_PNG, PNG_DEFAULT_sRGB); ++ * png_set_alpha_mode(pp, PNG_ALPHA_PNG, PNG_GAMMA_MAC); ++ * The first call sets both the default and the output gamma values, the ++ * second call overrides the output gamma without changing the default. This ++ * is easier than achieving the same effect with png_set_gamma. You must use ++ * PNG_ALPHA_PNG for the first call - internal checking in png_set_alpha will ++ * fire if more than one call to png_set_alpha_mode and png_set_background is ++ * made in the same read operation, however multiple calls with PNG_ALPHA_PNG ++ * are ignored. ++ */ ++ ++#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED ++PNG_EXPORT(36, void, png_set_strip_alpha, (png_structrp png_ptr)); ++#endif ++ ++#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED) || \ ++ defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED) ++PNG_EXPORT(37, void, png_set_swap_alpha, (png_structrp png_ptr)); ++#endif ++ ++#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) || \ ++ defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED) ++PNG_EXPORT(38, void, png_set_invert_alpha, (png_structrp png_ptr)); ++#endif ++ ++#if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED) ++/* Add a filler byte to 8-bit or 16-bit Gray or 24-bit or 48-bit RGB images. */ ++PNG_EXPORT(39, void, png_set_filler, (png_structrp png_ptr, png_uint_32 filler, ++ int flags)); ++/* The values of the PNG_FILLER_ defines should NOT be changed */ ++# define PNG_FILLER_BEFORE 0 ++# define PNG_FILLER_AFTER 1 ++/* Add an alpha byte to 8-bit or 16-bit Gray or 24-bit or 48-bit RGB images. */ ++PNG_EXPORT(40, void, png_set_add_alpha, (png_structrp png_ptr, ++ png_uint_32 filler, int flags)); ++#endif /* READ_FILLER || WRITE_FILLER */ ++ ++#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED) ++/* Swap bytes in 16-bit depth files. */ ++PNG_EXPORT(41, void, png_set_swap, (png_structrp png_ptr)); ++#endif ++ ++#if defined(PNG_READ_PACK_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED) ++/* Use 1 byte per pixel in 1, 2, or 4-bit depth files. */ ++PNG_EXPORT(42, void, png_set_packing, (png_structrp png_ptr)); ++#endif ++ ++#if defined(PNG_READ_PACKSWAP_SUPPORTED) || \ ++ defined(PNG_WRITE_PACKSWAP_SUPPORTED) ++/* Swap packing order of pixels in bytes. */ ++PNG_EXPORT(43, void, png_set_packswap, (png_structrp png_ptr)); ++#endif ++ ++#if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED) ++/* Converts files to legal bit depths. */ ++PNG_EXPORT(44, void, png_set_shift, (png_structrp png_ptr, png_const_color_8p ++ true_bits)); ++#endif ++ ++#if defined(PNG_READ_INTERLACING_SUPPORTED) || \ ++ defined(PNG_WRITE_INTERLACING_SUPPORTED) ++/* Have the code handle the interlacing. Returns the number of passes. ++ * MUST be called before png_read_update_info or png_start_read_image, ++ * otherwise it will not have the desired effect. Note that it is still ++ * necessary to call png_read_row or png_read_rows png_get_image_height ++ * times for each pass. ++*/ ++PNG_EXPORT(45, int, png_set_interlace_handling, (png_structrp png_ptr)); ++#endif ++ ++#if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED) ++/* Invert monochrome files */ ++PNG_EXPORT(46, void, png_set_invert_mono, (png_structrp png_ptr)); ++#endif ++ ++#ifdef PNG_READ_BACKGROUND_SUPPORTED ++/* Handle alpha and tRNS by replacing with a background color. Prior to ++ * libpng-1.5.4 this API must not be called before the PNG file header has been ++ * read. Doing so will result in unexpected behavior and possible warnings or ++ * errors if the PNG file contains a bKGD chunk. ++ */ ++PNG_FP_EXPORT(47, void, png_set_background, (png_structrp png_ptr, ++ png_const_color_16p background_color, int background_gamma_code, ++ int need_expand, double background_gamma)) ++PNG_FIXED_EXPORT(215, void, png_set_background_fixed, (png_structrp png_ptr, ++ png_const_color_16p background_color, int background_gamma_code, ++ int need_expand, png_fixed_point background_gamma)) ++#endif ++#ifdef PNG_READ_BACKGROUND_SUPPORTED ++# define PNG_BACKGROUND_GAMMA_UNKNOWN 0 ++# define PNG_BACKGROUND_GAMMA_SCREEN 1 ++# define PNG_BACKGROUND_GAMMA_FILE 2 ++# define PNG_BACKGROUND_GAMMA_UNIQUE 3 ++#endif ++ ++#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED ++/* Scale a 16-bit depth file down to 8-bit, accurately. */ ++PNG_EXPORT(229, void, png_set_scale_16, (png_structrp png_ptr)); ++#endif ++ ++#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED ++#define PNG_READ_16_TO_8_SUPPORTED /* Name prior to 1.5.4 */ ++/* Strip the second byte of information from a 16-bit depth file. */ ++PNG_EXPORT(48, void, png_set_strip_16, (png_structrp png_ptr)); ++#endif ++ ++#ifdef PNG_READ_QUANTIZE_SUPPORTED ++/* Turn on quantizing, and reduce the palette to the number of colors ++ * available. ++ */ ++PNG_EXPORT(49, void, png_set_quantize, (png_structrp png_ptr, ++ png_colorp palette, int num_palette, int maximum_colors, ++ png_const_uint_16p histogram, int full_quantize)); ++#endif ++ ++#ifdef PNG_READ_GAMMA_SUPPORTED ++/* The threshold on gamma processing is configurable but hard-wired into the ++ * library. The following is the floating point variant. ++ */ ++#define PNG_GAMMA_THRESHOLD (PNG_GAMMA_THRESHOLD_FIXED*.00001) ++ ++/* Handle gamma correction. Screen_gamma=(display_exponent). ++ * NOTE: this API simply sets the screen and file gamma values. It will ++ * therefore override the value for gamma in a PNG file if it is called after ++ * the file header has been read - use with care - call before reading the PNG ++ * file for best results! ++ * ++ * These routines accept the same gamma values as png_set_alpha_mode (described ++ * above). The PNG_GAMMA_ defines and PNG_DEFAULT_sRGB can be passed to either ++ * API (floating point or fixed.) Notice, however, that the 'file_gamma' value ++ * is the inverse of a 'screen gamma' value. ++ */ ++PNG_FP_EXPORT(50, void, png_set_gamma, (png_structrp png_ptr, ++ double screen_gamma, double override_file_gamma)) ++PNG_FIXED_EXPORT(208, void, png_set_gamma_fixed, (png_structrp png_ptr, ++ png_fixed_point screen_gamma, png_fixed_point override_file_gamma)) ++#endif ++ ++#ifdef PNG_WRITE_FLUSH_SUPPORTED ++/* Set how many lines between output flushes - 0 for no flushing */ ++PNG_EXPORT(51, void, png_set_flush, (png_structrp png_ptr, int nrows)); ++/* Flush the current PNG output buffer */ ++PNG_EXPORT(52, void, png_write_flush, (png_structrp png_ptr)); ++#endif ++ ++/* Optional update palette with requested transformations */ ++PNG_EXPORT(53, void, png_start_read_image, (png_structrp png_ptr)); ++ ++/* Optional call to update the users info structure */ ++PNG_EXPORT(54, void, png_read_update_info, (png_structrp png_ptr, ++ png_inforp info_ptr)); ++ ++#ifdef PNG_SEQUENTIAL_READ_SUPPORTED ++/* Read one or more rows of image data. */ ++PNG_EXPORT(55, void, png_read_rows, (png_structrp png_ptr, png_bytepp row, ++ png_bytepp display_row, png_uint_32 num_rows)); ++#endif ++ ++#ifdef PNG_SEQUENTIAL_READ_SUPPORTED ++/* Read a row of data. */ ++PNG_EXPORT(56, void, png_read_row, (png_structrp png_ptr, png_bytep row, ++ png_bytep display_row)); ++#endif ++ ++#ifdef PNG_SEQUENTIAL_READ_SUPPORTED ++/* Read the whole image into memory at once. */ ++PNG_EXPORT(57, void, png_read_image, (png_structrp png_ptr, png_bytepp image)); ++#endif ++ ++/* Write a row of image data */ ++PNG_EXPORT(58, void, png_write_row, (png_structrp png_ptr, ++ png_const_bytep row)); ++ ++/* Write a few rows of image data: (*row) is not written; however, the type ++ * is declared as writeable to maintain compatibility with previous versions ++ * of libpng and to allow the 'display_row' array from read_rows to be passed ++ * unchanged to write_rows. ++ */ ++PNG_EXPORT(59, void, png_write_rows, (png_structrp png_ptr, png_bytepp row, ++ png_uint_32 num_rows)); ++ ++/* Write the image data */ ++PNG_EXPORT(60, void, png_write_image, (png_structrp png_ptr, png_bytepp image)); ++ ++/* Write the end of the PNG file. */ ++PNG_EXPORT(61, void, png_write_end, (png_structrp png_ptr, ++ png_inforp info_ptr)); ++ ++#ifdef PNG_SEQUENTIAL_READ_SUPPORTED ++/* Read the end of the PNG file. */ ++PNG_EXPORT(62, void, png_read_end, (png_structrp png_ptr, png_inforp info_ptr)); ++#endif ++ ++/* Free any memory associated with the png_info_struct */ ++PNG_EXPORT(63, void, png_destroy_info_struct, (png_const_structrp png_ptr, ++ png_infopp info_ptr_ptr)); ++ ++/* Free any memory associated with the png_struct and the png_info_structs */ ++PNG_EXPORT(64, void, png_destroy_read_struct, (png_structpp png_ptr_ptr, ++ png_infopp info_ptr_ptr, png_infopp end_info_ptr_ptr)); ++ ++/* Free any memory associated with the png_struct and the png_info_structs */ ++PNG_EXPORT(65, void, png_destroy_write_struct, (png_structpp png_ptr_ptr, ++ png_infopp info_ptr_ptr)); ++ ++/* Set the libpng method of handling chunk CRC errors */ ++PNG_EXPORT(66, void, png_set_crc_action, (png_structrp png_ptr, int crit_action, ++ int ancil_action)); ++ ++/* Values for png_set_crc_action() say how to handle CRC errors in ++ * ancillary and critical chunks, and whether to use the data contained ++ * therein. Note that it is impossible to "discard" data in a critical ++ * chunk. For versions prior to 0.90, the action was always error/quit, ++ * whereas in version 0.90 and later, the action for CRC errors in ancillary ++ * chunks is warn/discard. These values should NOT be changed. ++ * ++ * value action:critical action:ancillary ++ */ ++#define PNG_CRC_DEFAULT 0 /* error/quit warn/discard data */ ++#define PNG_CRC_ERROR_QUIT 1 /* error/quit error/quit */ ++#define PNG_CRC_WARN_DISCARD 2 /* (INVALID) warn/discard data */ ++#define PNG_CRC_WARN_USE 3 /* warn/use data warn/use data */ ++#define PNG_CRC_QUIET_USE 4 /* quiet/use data quiet/use data */ ++#define PNG_CRC_NO_CHANGE 5 /* use current value use current value */ ++ ++#ifdef PNG_WRITE_SUPPORTED ++/* These functions give the user control over the scan-line filtering in ++ * libpng and the compression methods used by zlib. These functions are ++ * mainly useful for testing, as the defaults should work with most users. ++ * Those users who are tight on memory or want faster performance at the ++ * expense of compression can modify them. See the compression library ++ * header file (zlib.h) for an explination of the compression functions. ++ */ ++ ++/* Set the filtering method(s) used by libpng. Currently, the only valid ++ * value for "method" is 0. ++ */ ++PNG_EXPORT(67, void, png_set_filter, (png_structrp png_ptr, int method, ++ int filters)); ++#endif /* WRITE */ ++ ++/* Flags for png_set_filter() to say which filters to use. The flags ++ * are chosen so that they don't conflict with real filter types ++ * below, in case they are supplied instead of the #defined constants. ++ * These values should NOT be changed. ++ */ ++#define PNG_NO_FILTERS 0x00 ++#define PNG_FILTER_NONE 0x08 ++#define PNG_FILTER_SUB 0x10 ++#define PNG_FILTER_UP 0x20 ++#define PNG_FILTER_AVG 0x40 ++#define PNG_FILTER_PAETH 0x80 ++#define PNG_FAST_FILTERS (PNG_FILTER_NONE | PNG_FILTER_SUB | PNG_FILTER_UP) ++#define PNG_ALL_FILTERS (PNG_FAST_FILTERS | PNG_FILTER_AVG | PNG_FILTER_PAETH) ++ ++/* Filter values (not flags) - used in pngwrite.c, pngwutil.c for now. ++ * These defines should NOT be changed. ++ */ ++#define PNG_FILTER_VALUE_NONE 0 ++#define PNG_FILTER_VALUE_SUB 1 ++#define PNG_FILTER_VALUE_UP 2 ++#define PNG_FILTER_VALUE_AVG 3 ++#define PNG_FILTER_VALUE_PAETH 4 ++#define PNG_FILTER_VALUE_LAST 5 ++ ++#ifdef PNG_WRITE_SUPPORTED ++#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* DEPRECATED */ ++PNG_FP_EXPORT(68, void, png_set_filter_heuristics, (png_structrp png_ptr, ++ int heuristic_method, int num_weights, png_const_doublep filter_weights, ++ png_const_doublep filter_costs)) ++PNG_FIXED_EXPORT(209, void, png_set_filter_heuristics_fixed, ++ (png_structrp png_ptr, int heuristic_method, int num_weights, ++ png_const_fixed_point_p filter_weights, ++ png_const_fixed_point_p filter_costs)) ++#endif /* WRITE_WEIGHTED_FILTER */ ++ ++/* The following are no longer used and will be removed from libpng-1.7: */ ++#define PNG_FILTER_HEURISTIC_DEFAULT 0 /* Currently "UNWEIGHTED" */ ++#define PNG_FILTER_HEURISTIC_UNWEIGHTED 1 /* Used by libpng < 0.95 */ ++#define PNG_FILTER_HEURISTIC_WEIGHTED 2 /* Experimental feature */ ++#define PNG_FILTER_HEURISTIC_LAST 3 /* Not a valid value */ ++ ++/* Set the library compression level. Currently, valid values range from ++ * 0 - 9, corresponding directly to the zlib compression levels 0 - 9 ++ * (0 - no compression, 9 - "maximal" compression). Note that tests have ++ * shown that zlib compression levels 3-6 usually perform as well as level 9 ++ * for PNG images, and do considerably fewer caclulations. In the future, ++ * these values may not correspond directly to the zlib compression levels. ++ */ ++#ifdef PNG_WRITE_CUSTOMIZE_COMPRESSION_SUPPORTED ++PNG_EXPORT(69, void, png_set_compression_level, (png_structrp png_ptr, ++ int level)); ++ ++PNG_EXPORT(70, void, png_set_compression_mem_level, (png_structrp png_ptr, ++ int mem_level)); ++ ++PNG_EXPORT(71, void, png_set_compression_strategy, (png_structrp png_ptr, ++ int strategy)); ++ ++/* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a ++ * smaller value of window_bits if it can do so safely. ++ */ ++PNG_EXPORT(72, void, png_set_compression_window_bits, (png_structrp png_ptr, ++ int window_bits)); ++ ++PNG_EXPORT(73, void, png_set_compression_method, (png_structrp png_ptr, ++ int method)); ++#endif /* WRITE_CUSTOMIZE_COMPRESSION */ ++ ++#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED ++/* Also set zlib parameters for compressing non-IDAT chunks */ ++PNG_EXPORT(222, void, png_set_text_compression_level, (png_structrp png_ptr, ++ int level)); ++ ++PNG_EXPORT(223, void, png_set_text_compression_mem_level, (png_structrp png_ptr, ++ int mem_level)); ++ ++PNG_EXPORT(224, void, png_set_text_compression_strategy, (png_structrp png_ptr, ++ int strategy)); ++ ++/* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a ++ * smaller value of window_bits if it can do so safely. ++ */ ++PNG_EXPORT(225, void, png_set_text_compression_window_bits, ++ (png_structrp png_ptr, int window_bits)); ++ ++PNG_EXPORT(226, void, png_set_text_compression_method, (png_structrp png_ptr, ++ int method)); ++#endif /* WRITE_CUSTOMIZE_ZTXT_COMPRESSION */ ++#endif /* WRITE */ ++ ++/* These next functions are called for input/output, memory, and error ++ * handling. They are in the file pngrio.c, pngwio.c, and pngerror.c, ++ * and call standard C I/O routines such as fread(), fwrite(), and ++ * fprintf(). These functions can be made to use other I/O routines ++ * at run time for those applications that need to handle I/O in a ++ * different manner by calling png_set_???_fn(). See libpng-manual.txt for ++ * more information. ++ */ ++ ++#ifdef PNG_STDIO_SUPPORTED ++/* Initialize the input/output for the PNG file to the default functions. */ ++PNG_EXPORT(74, void, png_init_io, (png_structrp png_ptr, png_FILE_p fp)); ++#endif ++ ++/* Replace the (error and abort), and warning functions with user ++ * supplied functions. If no messages are to be printed you must still ++ * write and use replacement functions. The replacement error_fn should ++ * still do a longjmp to the last setjmp location if you are using this ++ * method of error handling. If error_fn or warning_fn is NULL, the ++ * default function will be used. ++ */ ++ ++PNG_EXPORT(75, void, png_set_error_fn, (png_structrp png_ptr, ++ png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warning_fn)); ++ ++/* Return the user pointer associated with the error functions */ ++PNG_EXPORT(76, png_voidp, png_get_error_ptr, (png_const_structrp png_ptr)); ++ ++/* Replace the default data output functions with a user supplied one(s). ++ * If buffered output is not used, then output_flush_fn can be set to NULL. ++ * If PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile time ++ * output_flush_fn will be ignored (and thus can be NULL). ++ * It is probably a mistake to use NULL for output_flush_fn if ++ * write_data_fn is not also NULL unless you have built libpng with ++ * PNG_WRITE_FLUSH_SUPPORTED undefined, because in this case libpng's ++ * default flush function, which uses the standard *FILE structure, will ++ * be used. ++ */ ++PNG_EXPORT(77, void, png_set_write_fn, (png_structrp png_ptr, png_voidp io_ptr, ++ png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn)); ++ ++/* Replace the default data input function with a user supplied one. */ ++PNG_EXPORT(78, void, png_set_read_fn, (png_structrp png_ptr, png_voidp io_ptr, ++ png_rw_ptr read_data_fn)); ++ ++/* Return the user pointer associated with the I/O functions */ ++PNG_EXPORT(79, png_voidp, png_get_io_ptr, (png_const_structrp png_ptr)); ++ ++PNG_EXPORT(80, void, png_set_read_status_fn, (png_structrp png_ptr, ++ png_read_status_ptr read_row_fn)); ++ ++PNG_EXPORT(81, void, png_set_write_status_fn, (png_structrp png_ptr, ++ png_write_status_ptr write_row_fn)); ++ ++#ifdef PNG_USER_MEM_SUPPORTED ++/* Replace the default memory allocation functions with user supplied one(s). */ ++PNG_EXPORT(82, void, png_set_mem_fn, (png_structrp png_ptr, png_voidp mem_ptr, ++ png_malloc_ptr malloc_fn, png_free_ptr free_fn)); ++/* Return the user pointer associated with the memory functions */ ++PNG_EXPORT(83, png_voidp, png_get_mem_ptr, (png_const_structrp png_ptr)); ++#endif ++ ++#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED ++PNG_EXPORT(84, void, png_set_read_user_transform_fn, (png_structrp png_ptr, ++ png_user_transform_ptr read_user_transform_fn)); ++#endif ++ ++#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED ++PNG_EXPORT(85, void, png_set_write_user_transform_fn, (png_structrp png_ptr, ++ png_user_transform_ptr write_user_transform_fn)); ++#endif ++ ++#ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED ++PNG_EXPORT(86, void, png_set_user_transform_info, (png_structrp png_ptr, ++ png_voidp user_transform_ptr, int user_transform_depth, ++ int user_transform_channels)); ++/* Return the user pointer associated with the user transform functions */ ++PNG_EXPORT(87, png_voidp, png_get_user_transform_ptr, ++ (png_const_structrp png_ptr)); ++#endif ++ ++#ifdef PNG_USER_TRANSFORM_INFO_SUPPORTED ++/* Return information about the row currently being processed. Note that these ++ * APIs do not fail but will return unexpected results if called outside a user ++ * transform callback. Also note that when transforming an interlaced image the ++ * row number is the row number within the sub-image of the interlace pass, so ++ * the value will increase to the height of the sub-image (not the full image) ++ * then reset to 0 for the next pass. ++ * ++ * Use PNG_ROW_FROM_PASS_ROW(row, pass) and PNG_COL_FROM_PASS_COL(col, pass) to ++ * find the output pixel (x,y) given an interlaced sub-image pixel ++ * (row,col,pass). (See below for these macros.) ++ */ ++PNG_EXPORT(217, png_uint_32, png_get_current_row_number, (png_const_structrp)); ++PNG_EXPORT(218, png_byte, png_get_current_pass_number, (png_const_structrp)); ++#endif ++ ++#ifdef PNG_READ_USER_CHUNKS_SUPPORTED ++/* This callback is called only for *unknown* chunks. If ++ * PNG_HANDLE_AS_UNKNOWN_SUPPORTED is set then it is possible to set known ++ * chunks to be treated as unknown, however in this case the callback must do ++ * any processing required by the chunk (e.g. by calling the appropriate ++ * png_set_ APIs.) ++ * ++ * There is no write support - on write, by default, all the chunks in the ++ * 'unknown' list are written in the specified position. ++ * ++ * The integer return from the callback function is interpreted thus: ++ * ++ * negative: An error occurred; png_chunk_error will be called. ++ * zero: The chunk was not handled, the chunk will be saved. A critical ++ * chunk will cause an error at this point unless it is to be saved. ++ * positive: The chunk was handled, libpng will ignore/discard it. ++ * ++ * See "INTERACTION WITH USER CHUNK CALLBACKS" below for important notes about ++ * how this behavior will change in libpng 1.7 ++ */ ++PNG_EXPORT(88, void, png_set_read_user_chunk_fn, (png_structrp png_ptr, ++ png_voidp user_chunk_ptr, png_user_chunk_ptr read_user_chunk_fn)); ++#endif ++ ++#ifdef PNG_USER_CHUNKS_SUPPORTED ++PNG_EXPORT(89, png_voidp, png_get_user_chunk_ptr, (png_const_structrp png_ptr)); ++#endif ++ ++#ifdef PNG_PROGRESSIVE_READ_SUPPORTED ++/* Sets the function callbacks for the push reader, and a pointer to a ++ * user-defined structure available to the callback functions. ++ */ ++PNG_EXPORT(90, void, png_set_progressive_read_fn, (png_structrp png_ptr, ++ png_voidp progressive_ptr, png_progressive_info_ptr info_fn, ++ png_progressive_row_ptr row_fn, png_progressive_end_ptr end_fn)); ++ ++/* Returns the user pointer associated with the push read functions */ ++PNG_EXPORT(91, png_voidp, png_get_progressive_ptr, ++ (png_const_structrp png_ptr)); ++ ++/* Function to be called when data becomes available */ ++PNG_EXPORT(92, void, png_process_data, (png_structrp png_ptr, ++ png_inforp info_ptr, png_bytep buffer, size_t buffer_size)); ++ ++/* A function which may be called *only* within png_process_data to stop the ++ * processing of any more data. The function returns the number of bytes ++ * remaining, excluding any that libpng has cached internally. A subsequent ++ * call to png_process_data must supply these bytes again. If the argument ++ * 'save' is set to true the routine will first save all the pending data and ++ * will always return 0. ++ */ ++PNG_EXPORT(219, size_t, png_process_data_pause, (png_structrp, int save)); ++ ++/* A function which may be called *only* outside (after) a call to ++ * png_process_data. It returns the number of bytes of data to skip in the ++ * input. Normally it will return 0, but if it returns a non-zero value the ++ * application must skip than number of bytes of input data and pass the ++ * following data to the next call to png_process_data. ++ */ ++PNG_EXPORT(220, png_uint_32, png_process_data_skip, (png_structrp)); ++ ++/* Function that combines rows. 'new_row' is a flag that should come from ++ * the callback and be non-NULL if anything needs to be done; the library ++ * stores its own version of the new data internally and ignores the passed ++ * in value. ++ */ ++PNG_EXPORT(93, void, png_progressive_combine_row, (png_const_structrp png_ptr, ++ png_bytep old_row, png_const_bytep new_row)); ++#endif /* PROGRESSIVE_READ */ ++ ++PNG_EXPORTA(94, png_voidp, png_malloc, (png_const_structrp png_ptr, ++ png_alloc_size_t size), PNG_ALLOCATED); ++/* Added at libpng version 1.4.0 */ ++PNG_EXPORTA(95, png_voidp, png_calloc, (png_const_structrp png_ptr, ++ png_alloc_size_t size), PNG_ALLOCATED); ++ ++/* Added at libpng version 1.2.4 */ ++PNG_EXPORTA(96, png_voidp, png_malloc_warn, (png_const_structrp png_ptr, ++ png_alloc_size_t size), PNG_ALLOCATED); ++ ++/* Frees a pointer allocated by png_malloc() */ ++PNG_EXPORT(97, void, png_free, (png_const_structrp png_ptr, png_voidp ptr)); ++ ++/* Free data that was allocated internally */ ++PNG_EXPORT(98, void, png_free_data, (png_const_structrp png_ptr, ++ png_inforp info_ptr, png_uint_32 free_me, int num)); ++ ++/* Reassign responsibility for freeing existing data, whether allocated ++ * by libpng or by the application; this works on the png_info structure passed ++ * in, it does not change the state for other png_info structures. ++ * ++ * It is unlikely that this function works correctly as of 1.6.0 and using it ++ * may result either in memory leaks or double free of allocated data. ++ */ ++PNG_EXPORT(99, void, png_data_freer, (png_const_structrp png_ptr, ++ png_inforp info_ptr, int freer, png_uint_32 mask)); ++ ++/* Assignments for png_data_freer */ ++#define PNG_DESTROY_WILL_FREE_DATA 1 ++#define PNG_SET_WILL_FREE_DATA 1 ++#define PNG_USER_WILL_FREE_DATA 2 ++/* Flags for png_ptr->free_me and info_ptr->free_me */ ++#define PNG_FREE_HIST 0x0008U ++#define PNG_FREE_ICCP 0x0010U ++#define PNG_FREE_SPLT 0x0020U ++#define PNG_FREE_ROWS 0x0040U ++#define PNG_FREE_PCAL 0x0080U ++#define PNG_FREE_SCAL 0x0100U ++#ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED ++# define PNG_FREE_UNKN 0x0200U ++#endif ++/* PNG_FREE_LIST 0x0400U removed in 1.6.0 because it is ignored */ ++#define PNG_FREE_PLTE 0x1000U ++#define PNG_FREE_TRNS 0x2000U ++#define PNG_FREE_TEXT 0x4000U ++#define PNG_FREE_EXIF 0x8000U /* Added at libpng-1.6.31 */ ++#define PNG_FREE_ALL 0xffffU ++#define PNG_FREE_MUL 0x4220U /* PNG_FREE_SPLT|PNG_FREE_TEXT|PNG_FREE_UNKN */ ++ ++#ifdef PNG_USER_MEM_SUPPORTED ++PNG_EXPORTA(100, png_voidp, png_malloc_default, (png_const_structrp png_ptr, ++ png_alloc_size_t size), PNG_ALLOCATED PNG_DEPRECATED); ++PNG_EXPORTA(101, void, png_free_default, (png_const_structrp png_ptr, ++ png_voidp ptr), PNG_DEPRECATED); ++#endif ++ ++#ifdef PNG_ERROR_TEXT_SUPPORTED ++/* Fatal error in PNG image of libpng - can't continue */ ++PNG_EXPORTA(102, void, png_error, (png_const_structrp png_ptr, ++ png_const_charp error_message), PNG_NORETURN); ++ ++/* The same, but the chunk name is prepended to the error string. */ ++PNG_EXPORTA(103, void, png_chunk_error, (png_const_structrp png_ptr, ++ png_const_charp error_message), PNG_NORETURN); ++ ++#else ++/* Fatal error in PNG image of libpng - can't continue */ ++PNG_EXPORTA(104, void, png_err, (png_const_structrp png_ptr), PNG_NORETURN); ++# define png_error(s1,s2) png_err(s1) ++# define png_chunk_error(s1,s2) png_err(s1) ++#endif ++ ++#ifdef PNG_WARNINGS_SUPPORTED ++/* Non-fatal error in libpng. Can continue, but may have a problem. */ ++PNG_EXPORT(105, void, png_warning, (png_const_structrp png_ptr, ++ png_const_charp warning_message)); ++ ++/* Non-fatal error in libpng, chunk name is prepended to message. */ ++PNG_EXPORT(106, void, png_chunk_warning, (png_const_structrp png_ptr, ++ png_const_charp warning_message)); ++#else ++# define png_warning(s1,s2) ((void)(s1)) ++# define png_chunk_warning(s1,s2) ((void)(s1)) ++#endif ++ ++#ifdef PNG_BENIGN_ERRORS_SUPPORTED ++/* Benign error in libpng. Can continue, but may have a problem. ++ * User can choose whether to handle as a fatal error or as a warning. */ ++PNG_EXPORT(107, void, png_benign_error, (png_const_structrp png_ptr, ++ png_const_charp warning_message)); ++ ++#ifdef PNG_READ_SUPPORTED ++/* Same, chunk name is prepended to message (only during read) */ ++PNG_EXPORT(108, void, png_chunk_benign_error, (png_const_structrp png_ptr, ++ png_const_charp warning_message)); ++#endif ++ ++PNG_EXPORT(109, void, png_set_benign_errors, ++ (png_structrp png_ptr, int allowed)); ++#else ++# ifdef PNG_ALLOW_BENIGN_ERRORS ++# define png_benign_error png_warning ++# define png_chunk_benign_error png_chunk_warning ++# else ++# define png_benign_error png_error ++# define png_chunk_benign_error png_chunk_error ++# endif ++#endif ++ ++/* The png_set_ functions are for storing values in the png_info_struct. ++ * Similarly, the png_get_ calls are used to read values from the ++ * png_info_struct, either storing the parameters in the passed variables, or ++ * setting pointers into the png_info_struct where the data is stored. The ++ * png_get_ functions return a non-zero value if the data was available ++ * in info_ptr, or return zero and do not change any of the parameters if the ++ * data was not available. ++ * ++ * These functions should be used instead of directly accessing png_info ++ * to avoid problems with future changes in the size and internal layout of ++ * png_info_struct. ++ */ ++/* Returns "flag" if chunk data is valid in info_ptr. */ ++PNG_EXPORT(110, png_uint_32, png_get_valid, (png_const_structrp png_ptr, ++ png_const_inforp info_ptr, png_uint_32 flag)); ++ ++/* Returns number of bytes needed to hold a transformed row. */ ++PNG_EXPORT(111, size_t, png_get_rowbytes, (png_const_structrp png_ptr, ++ png_const_inforp info_ptr)); ++ ++#ifdef PNG_INFO_IMAGE_SUPPORTED ++/* Returns row_pointers, which is an array of pointers to scanlines that was ++ * returned from png_read_png(). ++ */ ++PNG_EXPORT(112, png_bytepp, png_get_rows, (png_const_structrp png_ptr, ++ png_const_inforp info_ptr)); ++ ++/* Set row_pointers, which is an array of pointers to scanlines for use ++ * by png_write_png(). ++ */ ++PNG_EXPORT(113, void, png_set_rows, (png_const_structrp png_ptr, ++ png_inforp info_ptr, png_bytepp row_pointers)); ++#endif ++ ++/* Returns number of color channels in image. */ ++PNG_EXPORT(114, png_byte, png_get_channels, (png_const_structrp png_ptr, ++ png_const_inforp info_ptr)); ++ ++#ifdef PNG_EASY_ACCESS_SUPPORTED ++/* Returns image width in pixels. */ ++PNG_EXPORT(115, png_uint_32, png_get_image_width, (png_const_structrp png_ptr, ++ png_const_inforp info_ptr)); ++ ++/* Returns image height in pixels. */ ++PNG_EXPORT(116, png_uint_32, png_get_image_height, (png_const_structrp png_ptr, ++ png_const_inforp info_ptr)); ++ ++/* Returns image bit_depth. */ ++PNG_EXPORT(117, png_byte, png_get_bit_depth, (png_const_structrp png_ptr, ++ png_const_inforp info_ptr)); ++ ++/* Returns image color_type. */ ++PNG_EXPORT(118, png_byte, png_get_color_type, (png_const_structrp png_ptr, ++ png_const_inforp info_ptr)); ++ ++/* Returns image filter_type. */ ++PNG_EXPORT(119, png_byte, png_get_filter_type, (png_const_structrp png_ptr, ++ png_const_inforp info_ptr)); ++ ++/* Returns image interlace_type. */ ++PNG_EXPORT(120, png_byte, png_get_interlace_type, (png_const_structrp png_ptr, ++ png_const_inforp info_ptr)); ++ ++/* Returns image compression_type. */ ++PNG_EXPORT(121, png_byte, png_get_compression_type, (png_const_structrp png_ptr, ++ png_const_inforp info_ptr)); ++ ++/* Returns image resolution in pixels per meter, from pHYs chunk data. */ ++PNG_EXPORT(122, png_uint_32, png_get_pixels_per_meter, ++ (png_const_structrp png_ptr, png_const_inforp info_ptr)); ++PNG_EXPORT(123, png_uint_32, png_get_x_pixels_per_meter, ++ (png_const_structrp png_ptr, png_const_inforp info_ptr)); ++PNG_EXPORT(124, png_uint_32, png_get_y_pixels_per_meter, ++ (png_const_structrp png_ptr, png_const_inforp info_ptr)); ++ ++/* Returns pixel aspect ratio, computed from pHYs chunk data. */ ++PNG_FP_EXPORT(125, float, png_get_pixel_aspect_ratio, ++ (png_const_structrp png_ptr, png_const_inforp info_ptr)) ++PNG_FIXED_EXPORT(210, png_fixed_point, png_get_pixel_aspect_ratio_fixed, ++ (png_const_structrp png_ptr, png_const_inforp info_ptr)) ++ ++/* Returns image x, y offset in pixels or microns, from oFFs chunk data. */ ++PNG_EXPORT(126, png_int_32, png_get_x_offset_pixels, ++ (png_const_structrp png_ptr, png_const_inforp info_ptr)); ++PNG_EXPORT(127, png_int_32, png_get_y_offset_pixels, ++ (png_const_structrp png_ptr, png_const_inforp info_ptr)); ++PNG_EXPORT(128, png_int_32, png_get_x_offset_microns, ++ (png_const_structrp png_ptr, png_const_inforp info_ptr)); ++PNG_EXPORT(129, png_int_32, png_get_y_offset_microns, ++ (png_const_structrp png_ptr, png_const_inforp info_ptr)); ++ ++#endif /* EASY_ACCESS */ ++ ++#ifdef PNG_READ_SUPPORTED ++/* Returns pointer to signature string read from PNG header */ ++PNG_EXPORT(130, png_const_bytep, png_get_signature, (png_const_structrp png_ptr, ++ png_const_inforp info_ptr)); ++#endif ++ ++#ifdef PNG_bKGD_SUPPORTED ++PNG_EXPORT(131, png_uint_32, png_get_bKGD, (png_const_structrp png_ptr, ++ png_inforp info_ptr, png_color_16p *background)); ++#endif ++ ++#ifdef PNG_bKGD_SUPPORTED ++PNG_EXPORT(132, void, png_set_bKGD, (png_const_structrp png_ptr, ++ png_inforp info_ptr, png_const_color_16p background)); ++#endif ++ ++#ifdef PNG_cHRM_SUPPORTED ++PNG_FP_EXPORT(133, png_uint_32, png_get_cHRM, (png_const_structrp png_ptr, ++ png_const_inforp info_ptr, double *white_x, double *white_y, double *red_x, ++ double *red_y, double *green_x, double *green_y, double *blue_x, ++ double *blue_y)) ++PNG_FP_EXPORT(230, png_uint_32, png_get_cHRM_XYZ, (png_const_structrp png_ptr, ++ png_const_inforp info_ptr, double *red_X, double *red_Y, double *red_Z, ++ double *green_X, double *green_Y, double *green_Z, double *blue_X, ++ double *blue_Y, double *blue_Z)) ++PNG_FIXED_EXPORT(134, png_uint_32, png_get_cHRM_fixed, ++ (png_const_structrp png_ptr, png_const_inforp info_ptr, ++ png_fixed_point *int_white_x, png_fixed_point *int_white_y, ++ png_fixed_point *int_red_x, png_fixed_point *int_red_y, ++ png_fixed_point *int_green_x, png_fixed_point *int_green_y, ++ png_fixed_point *int_blue_x, png_fixed_point *int_blue_y)) ++PNG_FIXED_EXPORT(231, png_uint_32, png_get_cHRM_XYZ_fixed, ++ (png_const_structrp png_ptr, png_const_inforp info_ptr, ++ png_fixed_point *int_red_X, png_fixed_point *int_red_Y, ++ png_fixed_point *int_red_Z, png_fixed_point *int_green_X, ++ png_fixed_point *int_green_Y, png_fixed_point *int_green_Z, ++ png_fixed_point *int_blue_X, png_fixed_point *int_blue_Y, ++ png_fixed_point *int_blue_Z)) ++#endif ++ ++#ifdef PNG_cHRM_SUPPORTED ++PNG_FP_EXPORT(135, void, png_set_cHRM, (png_const_structrp png_ptr, ++ png_inforp info_ptr, ++ double white_x, double white_y, double red_x, double red_y, double green_x, ++ double green_y, double blue_x, double blue_y)) ++PNG_FP_EXPORT(232, void, png_set_cHRM_XYZ, (png_const_structrp png_ptr, ++ png_inforp info_ptr, double red_X, double red_Y, double red_Z, ++ double green_X, double green_Y, double green_Z, double blue_X, ++ double blue_Y, double blue_Z)) ++PNG_FIXED_EXPORT(136, void, png_set_cHRM_fixed, (png_const_structrp png_ptr, ++ png_inforp info_ptr, png_fixed_point int_white_x, ++ png_fixed_point int_white_y, png_fixed_point int_red_x, ++ png_fixed_point int_red_y, png_fixed_point int_green_x, ++ png_fixed_point int_green_y, png_fixed_point int_blue_x, ++ png_fixed_point int_blue_y)) ++PNG_FIXED_EXPORT(233, void, png_set_cHRM_XYZ_fixed, (png_const_structrp png_ptr, ++ png_inforp info_ptr, png_fixed_point int_red_X, png_fixed_point int_red_Y, ++ png_fixed_point int_red_Z, png_fixed_point int_green_X, ++ png_fixed_point int_green_Y, png_fixed_point int_green_Z, ++ png_fixed_point int_blue_X, png_fixed_point int_blue_Y, ++ png_fixed_point int_blue_Z)) ++#endif ++ ++#ifdef PNG_eXIf_SUPPORTED ++PNG_EXPORT(246, png_uint_32, png_get_eXIf, (png_const_structrp png_ptr, ++ png_inforp info_ptr, png_bytep *exif)); ++PNG_EXPORT(247, void, png_set_eXIf, (png_const_structrp png_ptr, ++ png_inforp info_ptr, png_bytep exif)); ++ ++PNG_EXPORT(248, png_uint_32, png_get_eXIf_1, (png_const_structrp png_ptr, ++ png_const_inforp info_ptr, png_uint_32 *num_exif, png_bytep *exif)); ++PNG_EXPORT(249, void, png_set_eXIf_1, (png_const_structrp png_ptr, ++ png_inforp info_ptr, png_uint_32 num_exif, png_bytep exif)); ++#endif ++ ++#ifdef PNG_gAMA_SUPPORTED ++PNG_FP_EXPORT(137, png_uint_32, png_get_gAMA, (png_const_structrp png_ptr, ++ png_const_inforp info_ptr, double *file_gamma)) ++PNG_FIXED_EXPORT(138, png_uint_32, png_get_gAMA_fixed, ++ (png_const_structrp png_ptr, png_const_inforp info_ptr, ++ png_fixed_point *int_file_gamma)) ++#endif ++ ++#ifdef PNG_gAMA_SUPPORTED ++PNG_FP_EXPORT(139, void, png_set_gAMA, (png_const_structrp png_ptr, ++ png_inforp info_ptr, double file_gamma)) ++PNG_FIXED_EXPORT(140, void, png_set_gAMA_fixed, (png_const_structrp png_ptr, ++ png_inforp info_ptr, png_fixed_point int_file_gamma)) ++#endif ++ ++#ifdef PNG_hIST_SUPPORTED ++PNG_EXPORT(141, png_uint_32, png_get_hIST, (png_const_structrp png_ptr, ++ png_inforp info_ptr, png_uint_16p *hist)); ++PNG_EXPORT(142, void, png_set_hIST, (png_const_structrp png_ptr, ++ png_inforp info_ptr, png_const_uint_16p hist)); ++#endif ++ ++PNG_EXPORT(143, png_uint_32, png_get_IHDR, (png_const_structrp png_ptr, ++ png_const_inforp info_ptr, png_uint_32 *width, png_uint_32 *height, ++ int *bit_depth, int *color_type, int *interlace_method, ++ int *compression_method, int *filter_method)); ++ ++PNG_EXPORT(144, void, png_set_IHDR, (png_const_structrp png_ptr, ++ png_inforp info_ptr, png_uint_32 width, png_uint_32 height, int bit_depth, ++ int color_type, int interlace_method, int compression_method, ++ int filter_method)); ++ ++#ifdef PNG_oFFs_SUPPORTED ++PNG_EXPORT(145, png_uint_32, png_get_oFFs, (png_const_structrp png_ptr, ++ png_const_inforp info_ptr, png_int_32 *offset_x, png_int_32 *offset_y, ++ int *unit_type)); ++#endif ++ ++#ifdef PNG_oFFs_SUPPORTED ++PNG_EXPORT(146, void, png_set_oFFs, (png_const_structrp png_ptr, ++ png_inforp info_ptr, png_int_32 offset_x, png_int_32 offset_y, ++ int unit_type)); ++#endif ++ ++#ifdef PNG_pCAL_SUPPORTED ++PNG_EXPORT(147, png_uint_32, png_get_pCAL, (png_const_structrp png_ptr, ++ png_inforp info_ptr, png_charp *purpose, png_int_32 *X0, ++ png_int_32 *X1, int *type, int *nparams, png_charp *units, ++ png_charpp *params)); ++#endif ++ ++#ifdef PNG_pCAL_SUPPORTED ++PNG_EXPORT(148, void, png_set_pCAL, (png_const_structrp png_ptr, ++ png_inforp info_ptr, png_const_charp purpose, png_int_32 X0, png_int_32 X1, ++ int type, int nparams, png_const_charp units, png_charpp params)); ++#endif ++ ++#ifdef PNG_pHYs_SUPPORTED ++PNG_EXPORT(149, png_uint_32, png_get_pHYs, (png_const_structrp png_ptr, ++ png_const_inforp info_ptr, png_uint_32 *res_x, png_uint_32 *res_y, ++ int *unit_type)); ++#endif ++ ++#ifdef PNG_pHYs_SUPPORTED ++PNG_EXPORT(150, void, png_set_pHYs, (png_const_structrp png_ptr, ++ png_inforp info_ptr, png_uint_32 res_x, png_uint_32 res_y, int unit_type)); ++#endif ++ ++PNG_EXPORT(151, png_uint_32, png_get_PLTE, (png_const_structrp png_ptr, ++ png_inforp info_ptr, png_colorp *palette, int *num_palette)); ++ ++PNG_EXPORT(152, void, png_set_PLTE, (png_structrp png_ptr, ++ png_inforp info_ptr, png_const_colorp palette, int num_palette)); ++ ++#ifdef PNG_sBIT_SUPPORTED ++PNG_EXPORT(153, png_uint_32, png_get_sBIT, (png_const_structrp png_ptr, ++ png_inforp info_ptr, png_color_8p *sig_bit)); ++#endif ++ ++#ifdef PNG_sBIT_SUPPORTED ++PNG_EXPORT(154, void, png_set_sBIT, (png_const_structrp png_ptr, ++ png_inforp info_ptr, png_const_color_8p sig_bit)); ++#endif ++ ++#ifdef PNG_sRGB_SUPPORTED ++PNG_EXPORT(155, png_uint_32, png_get_sRGB, (png_const_structrp png_ptr, ++ png_const_inforp info_ptr, int *file_srgb_intent)); ++#endif ++ ++#ifdef PNG_sRGB_SUPPORTED ++PNG_EXPORT(156, void, png_set_sRGB, (png_const_structrp png_ptr, ++ png_inforp info_ptr, int srgb_intent)); ++PNG_EXPORT(157, void, png_set_sRGB_gAMA_and_cHRM, (png_const_structrp png_ptr, ++ png_inforp info_ptr, int srgb_intent)); ++#endif ++ ++#ifdef PNG_iCCP_SUPPORTED ++PNG_EXPORT(158, png_uint_32, png_get_iCCP, (png_const_structrp png_ptr, ++ png_inforp info_ptr, png_charpp name, int *compression_type, ++ png_bytepp profile, png_uint_32 *proflen)); ++#endif ++ ++#ifdef PNG_iCCP_SUPPORTED ++PNG_EXPORT(159, void, png_set_iCCP, (png_const_structrp png_ptr, ++ png_inforp info_ptr, png_const_charp name, int compression_type, ++ png_const_bytep profile, png_uint_32 proflen)); ++#endif ++ ++#ifdef PNG_sPLT_SUPPORTED ++PNG_EXPORT(160, int, png_get_sPLT, (png_const_structrp png_ptr, ++ png_inforp info_ptr, png_sPLT_tpp entries)); ++#endif ++ ++#ifdef PNG_sPLT_SUPPORTED ++PNG_EXPORT(161, void, png_set_sPLT, (png_const_structrp png_ptr, ++ png_inforp info_ptr, png_const_sPLT_tp entries, int nentries)); ++#endif ++ ++#ifdef PNG_TEXT_SUPPORTED ++/* png_get_text also returns the number of text chunks in *num_text */ ++PNG_EXPORT(162, int, png_get_text, (png_const_structrp png_ptr, ++ png_inforp info_ptr, png_textp *text_ptr, int *num_text)); ++#endif ++ ++/* Note while png_set_text() will accept a structure whose text, ++ * language, and translated keywords are NULL pointers, the structure ++ * returned by png_get_text will always contain regular ++ * zero-terminated C strings. They might be empty strings but ++ * they will never be NULL pointers. ++ */ ++ ++#ifdef PNG_TEXT_SUPPORTED ++PNG_EXPORT(163, void, png_set_text, (png_const_structrp png_ptr, ++ png_inforp info_ptr, png_const_textp text_ptr, int num_text)); ++#endif ++ ++#ifdef PNG_tIME_SUPPORTED ++PNG_EXPORT(164, png_uint_32, png_get_tIME, (png_const_structrp png_ptr, ++ png_inforp info_ptr, png_timep *mod_time)); ++#endif ++ ++#ifdef PNG_tIME_SUPPORTED ++PNG_EXPORT(165, void, png_set_tIME, (png_const_structrp png_ptr, ++ png_inforp info_ptr, png_const_timep mod_time)); ++#endif ++ ++#ifdef PNG_tRNS_SUPPORTED ++PNG_EXPORT(166, png_uint_32, png_get_tRNS, (png_const_structrp png_ptr, ++ png_inforp info_ptr, png_bytep *trans_alpha, int *num_trans, ++ png_color_16p *trans_color)); ++#endif ++ ++#ifdef PNG_tRNS_SUPPORTED ++PNG_EXPORT(167, void, png_set_tRNS, (png_structrp png_ptr, ++ png_inforp info_ptr, png_const_bytep trans_alpha, int num_trans, ++ png_const_color_16p trans_color)); ++#endif ++ ++#ifdef PNG_sCAL_SUPPORTED ++PNG_FP_EXPORT(168, png_uint_32, png_get_sCAL, (png_const_structrp png_ptr, ++ png_const_inforp info_ptr, int *unit, double *width, double *height)) ++#if defined(PNG_FLOATING_ARITHMETIC_SUPPORTED) || \ ++ defined(PNG_FLOATING_POINT_SUPPORTED) ++/* NOTE: this API is currently implemented using floating point arithmetic, ++ * consequently it can only be used on systems with floating point support. ++ * In any case the range of values supported by png_fixed_point is small and it ++ * is highly recommended that png_get_sCAL_s be used instead. ++ */ ++PNG_FIXED_EXPORT(214, png_uint_32, png_get_sCAL_fixed, ++ (png_const_structrp png_ptr, png_const_inforp info_ptr, int *unit, ++ png_fixed_point *width, png_fixed_point *height)) ++#endif ++PNG_EXPORT(169, png_uint_32, png_get_sCAL_s, ++ (png_const_structrp png_ptr, png_const_inforp info_ptr, int *unit, ++ png_charpp swidth, png_charpp sheight)); ++ ++PNG_FP_EXPORT(170, void, png_set_sCAL, (png_const_structrp png_ptr, ++ png_inforp info_ptr, int unit, double width, double height)) ++PNG_FIXED_EXPORT(213, void, png_set_sCAL_fixed, (png_const_structrp png_ptr, ++ png_inforp info_ptr, int unit, png_fixed_point width, ++ png_fixed_point height)) ++PNG_EXPORT(171, void, png_set_sCAL_s, (png_const_structrp png_ptr, ++ png_inforp info_ptr, int unit, ++ png_const_charp swidth, png_const_charp sheight)); ++#endif /* sCAL */ ++ ++#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED ++/* Provide the default handling for all unknown chunks or, optionally, for ++ * specific unknown chunks. ++ * ++ * NOTE: prior to 1.6.0 the handling specified for particular chunks on read was ++ * ignored and the default was used, the per-chunk setting only had an effect on ++ * write. If you wish to have chunk-specific handling on read in code that must ++ * work on earlier versions you must use a user chunk callback to specify the ++ * desired handling (keep or discard.) ++ * ++ * The 'keep' parameter is a PNG_HANDLE_CHUNK_ value as listed below. The ++ * parameter is interpreted as follows: ++ * ++ * READ: ++ * PNG_HANDLE_CHUNK_AS_DEFAULT: ++ * Known chunks: do normal libpng processing, do not keep the chunk (but ++ * see the comments below about PNG_HANDLE_AS_UNKNOWN_SUPPORTED) ++ * Unknown chunks: for a specific chunk use the global default, when used ++ * as the default discard the chunk data. ++ * PNG_HANDLE_CHUNK_NEVER: ++ * Discard the chunk data. ++ * PNG_HANDLE_CHUNK_IF_SAFE: ++ * Keep the chunk data if the chunk is not critical else raise a chunk ++ * error. ++ * PNG_HANDLE_CHUNK_ALWAYS: ++ * Keep the chunk data. ++ * ++ * If the chunk data is saved it can be retrieved using png_get_unknown_chunks, ++ * below. Notice that specifying "AS_DEFAULT" as a global default is equivalent ++ * to specifying "NEVER", however when "AS_DEFAULT" is used for specific chunks ++ * it simply resets the behavior to the libpng default. ++ * ++ * INTERACTION WITH USER CHUNK CALLBACKS: ++ * The per-chunk handling is always used when there is a png_user_chunk_ptr ++ * callback and the callback returns 0; the chunk is then always stored *unless* ++ * it is critical and the per-chunk setting is other than ALWAYS. Notice that ++ * the global default is *not* used in this case. (In effect the per-chunk ++ * value is incremented to at least IF_SAFE.) ++ * ++ * IMPORTANT NOTE: this behavior will change in libpng 1.7 - the global and ++ * per-chunk defaults will be honored. If you want to preserve the current ++ * behavior when your callback returns 0 you must set PNG_HANDLE_CHUNK_IF_SAFE ++ * as the default - if you don't do this libpng 1.6 will issue a warning. ++ * ++ * If you want unhandled unknown chunks to be discarded in libpng 1.6 and ++ * earlier simply return '1' (handled). ++ * ++ * PNG_HANDLE_AS_UNKNOWN_SUPPORTED: ++ * If this is *not* set known chunks will always be handled by libpng and ++ * will never be stored in the unknown chunk list. Known chunks listed to ++ * png_set_keep_unknown_chunks will have no effect. If it is set then known ++ * chunks listed with a keep other than AS_DEFAULT will *never* be processed ++ * by libpng, in addition critical chunks must either be processed by the ++ * callback or saved. ++ * ++ * The IHDR and IEND chunks must not be listed. Because this turns off the ++ * default handling for chunks that would otherwise be recognized the ++ * behavior of libpng transformations may well become incorrect! ++ * ++ * WRITE: ++ * When writing chunks the options only apply to the chunks specified by ++ * png_set_unknown_chunks (below), libpng will *always* write known chunks ++ * required by png_set_ calls and will always write the core critical chunks ++ * (as required for PLTE). ++ * ++ * Each chunk in the png_set_unknown_chunks list is looked up in the ++ * png_set_keep_unknown_chunks list to find the keep setting, this is then ++ * interpreted as follows: ++ * ++ * PNG_HANDLE_CHUNK_AS_DEFAULT: ++ * Write safe-to-copy chunks and write other chunks if the global ++ * default is set to _ALWAYS, otherwise don't write this chunk. ++ * PNG_HANDLE_CHUNK_NEVER: ++ * Do not write the chunk. ++ * PNG_HANDLE_CHUNK_IF_SAFE: ++ * Write the chunk if it is safe-to-copy, otherwise do not write it. ++ * PNG_HANDLE_CHUNK_ALWAYS: ++ * Write the chunk. ++ * ++ * Note that the default behavior is effectively the opposite of the read case - ++ * in read unknown chunks are not stored by default, in write they are written ++ * by default. Also the behavior of PNG_HANDLE_CHUNK_IF_SAFE is very different ++ * - on write the safe-to-copy bit is checked, on read the critical bit is ++ * checked and on read if the chunk is critical an error will be raised. ++ * ++ * num_chunks: ++ * =========== ++ * If num_chunks is positive, then the "keep" parameter specifies the manner ++ * for handling only those chunks appearing in the chunk_list array, ++ * otherwise the chunk list array is ignored. ++ * ++ * If num_chunks is 0 the "keep" parameter specifies the default behavior for ++ * unknown chunks, as described above. ++ * ++ * If num_chunks is negative, then the "keep" parameter specifies the manner ++ * for handling all unknown chunks plus all chunks recognized by libpng ++ * except for the IHDR, PLTE, tRNS, IDAT, and IEND chunks (which continue to ++ * be processed by libpng. ++ */ ++#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED ++PNG_EXPORT(172, void, png_set_keep_unknown_chunks, (png_structrp png_ptr, ++ int keep, png_const_bytep chunk_list, int num_chunks)); ++#endif /* HANDLE_AS_UNKNOWN */ ++ ++/* The "keep" PNG_HANDLE_CHUNK_ parameter for the specified chunk is returned; ++ * the result is therefore true (non-zero) if special handling is required, ++ * false for the default handling. ++ */ ++PNG_EXPORT(173, int, png_handle_as_unknown, (png_const_structrp png_ptr, ++ png_const_bytep chunk_name)); ++#endif /* SET_UNKNOWN_CHUNKS */ ++ ++#ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED ++PNG_EXPORT(174, void, png_set_unknown_chunks, (png_const_structrp png_ptr, ++ png_inforp info_ptr, png_const_unknown_chunkp unknowns, ++ int num_unknowns)); ++ /* NOTE: prior to 1.6.0 this routine set the 'location' field of the added ++ * unknowns to the location currently stored in the png_struct. This is ++ * invariably the wrong value on write. To fix this call the following API ++ * for each chunk in the list with the correct location. If you know your ++ * code won't be compiled on earlier versions you can rely on ++ * png_set_unknown_chunks(write-ptr, png_get_unknown_chunks(read-ptr)) doing ++ * the correct thing. ++ */ ++ ++PNG_EXPORT(175, void, png_set_unknown_chunk_location, ++ (png_const_structrp png_ptr, png_inforp info_ptr, int chunk, int location)); ++ ++PNG_EXPORT(176, int, png_get_unknown_chunks, (png_const_structrp png_ptr, ++ png_inforp info_ptr, png_unknown_chunkpp entries)); ++#endif ++ ++/* Png_free_data() will turn off the "valid" flag for anything it frees. ++ * If you need to turn it off for a chunk that your application has freed, ++ * you can use png_set_invalid(png_ptr, info_ptr, PNG_INFO_CHNK); ++ */ ++PNG_EXPORT(177, void, png_set_invalid, (png_const_structrp png_ptr, ++ png_inforp info_ptr, int mask)); ++ ++#ifdef PNG_INFO_IMAGE_SUPPORTED ++/* The "params" pointer is currently not used and is for future expansion. */ ++#ifdef PNG_SEQUENTIAL_READ_SUPPORTED ++PNG_EXPORT(178, void, png_read_png, (png_structrp png_ptr, png_inforp info_ptr, ++ int transforms, png_voidp params)); ++#endif ++#ifdef PNG_WRITE_SUPPORTED ++PNG_EXPORT(179, void, png_write_png, (png_structrp png_ptr, png_inforp info_ptr, ++ int transforms, png_voidp params)); ++#endif ++#endif ++ ++PNG_EXPORT(180, png_const_charp, png_get_copyright, ++ (png_const_structrp png_ptr)); ++PNG_EXPORT(181, png_const_charp, png_get_header_ver, ++ (png_const_structrp png_ptr)); ++PNG_EXPORT(182, png_const_charp, png_get_header_version, ++ (png_const_structrp png_ptr)); ++PNG_EXPORT(183, png_const_charp, png_get_libpng_ver, ++ (png_const_structrp png_ptr)); ++ ++#ifdef PNG_MNG_FEATURES_SUPPORTED ++PNG_EXPORT(184, png_uint_32, png_permit_mng_features, (png_structrp png_ptr, ++ png_uint_32 mng_features_permitted)); ++#endif ++ ++/* For use in png_set_keep_unknown, added to version 1.2.6 */ ++#define PNG_HANDLE_CHUNK_AS_DEFAULT 0 ++#define PNG_HANDLE_CHUNK_NEVER 1 ++#define PNG_HANDLE_CHUNK_IF_SAFE 2 ++#define PNG_HANDLE_CHUNK_ALWAYS 3 ++#define PNG_HANDLE_CHUNK_LAST 4 ++ ++/* Strip the prepended error numbers ("#nnn ") from error and warning ++ * messages before passing them to the error or warning handler. ++ */ ++#ifdef PNG_ERROR_NUMBERS_SUPPORTED ++PNG_EXPORT(185, void, png_set_strip_error_numbers, (png_structrp png_ptr, ++ png_uint_32 strip_mode)); ++#endif ++ ++/* Added in libpng-1.2.6 */ ++#ifdef PNG_SET_USER_LIMITS_SUPPORTED ++PNG_EXPORT(186, void, png_set_user_limits, (png_structrp png_ptr, ++ png_uint_32 user_width_max, png_uint_32 user_height_max)); ++PNG_EXPORT(187, png_uint_32, png_get_user_width_max, ++ (png_const_structrp png_ptr)); ++PNG_EXPORT(188, png_uint_32, png_get_user_height_max, ++ (png_const_structrp png_ptr)); ++/* Added in libpng-1.4.0 */ ++PNG_EXPORT(189, void, png_set_chunk_cache_max, (png_structrp png_ptr, ++ png_uint_32 user_chunk_cache_max)); ++PNG_EXPORT(190, png_uint_32, png_get_chunk_cache_max, ++ (png_const_structrp png_ptr)); ++/* Added in libpng-1.4.1 */ ++PNG_EXPORT(191, void, png_set_chunk_malloc_max, (png_structrp png_ptr, ++ png_alloc_size_t user_chunk_cache_max)); ++PNG_EXPORT(192, png_alloc_size_t, png_get_chunk_malloc_max, ++ (png_const_structrp png_ptr)); ++#endif ++ ++#if defined(PNG_INCH_CONVERSIONS_SUPPORTED) ++PNG_EXPORT(193, png_uint_32, png_get_pixels_per_inch, ++ (png_const_structrp png_ptr, png_const_inforp info_ptr)); ++ ++PNG_EXPORT(194, png_uint_32, png_get_x_pixels_per_inch, ++ (png_const_structrp png_ptr, png_const_inforp info_ptr)); ++ ++PNG_EXPORT(195, png_uint_32, png_get_y_pixels_per_inch, ++ (png_const_structrp png_ptr, png_const_inforp info_ptr)); ++ ++PNG_FP_EXPORT(196, float, png_get_x_offset_inches, ++ (png_const_structrp png_ptr, png_const_inforp info_ptr)) ++#ifdef PNG_FIXED_POINT_SUPPORTED /* otherwise not implemented. */ ++PNG_FIXED_EXPORT(211, png_fixed_point, png_get_x_offset_inches_fixed, ++ (png_const_structrp png_ptr, png_const_inforp info_ptr)) ++#endif ++ ++PNG_FP_EXPORT(197, float, png_get_y_offset_inches, (png_const_structrp png_ptr, ++ png_const_inforp info_ptr)) ++#ifdef PNG_FIXED_POINT_SUPPORTED /* otherwise not implemented. */ ++PNG_FIXED_EXPORT(212, png_fixed_point, png_get_y_offset_inches_fixed, ++ (png_const_structrp png_ptr, png_const_inforp info_ptr)) ++#endif ++ ++# ifdef PNG_pHYs_SUPPORTED ++PNG_EXPORT(198, png_uint_32, png_get_pHYs_dpi, (png_const_structrp png_ptr, ++ png_const_inforp info_ptr, png_uint_32 *res_x, png_uint_32 *res_y, ++ int *unit_type)); ++# endif /* pHYs */ ++#endif /* INCH_CONVERSIONS */ ++ ++/* Added in libpng-1.4.0 */ ++#ifdef PNG_IO_STATE_SUPPORTED ++PNG_EXPORT(199, png_uint_32, png_get_io_state, (png_const_structrp png_ptr)); ++ ++/* Removed from libpng 1.6; use png_get_io_chunk_type. */ ++PNG_REMOVED(200, png_const_bytep, png_get_io_chunk_name, (png_structrp png_ptr), ++ PNG_DEPRECATED) ++ ++PNG_EXPORT(216, png_uint_32, png_get_io_chunk_type, ++ (png_const_structrp png_ptr)); ++ ++/* The flags returned by png_get_io_state() are the following: */ ++# define PNG_IO_NONE 0x0000 /* no I/O at this moment */ ++# define PNG_IO_READING 0x0001 /* currently reading */ ++# define PNG_IO_WRITING 0x0002 /* currently writing */ ++# define PNG_IO_SIGNATURE 0x0010 /* currently at the file signature */ ++# define PNG_IO_CHUNK_HDR 0x0020 /* currently at the chunk header */ ++# define PNG_IO_CHUNK_DATA 0x0040 /* currently at the chunk data */ ++# define PNG_IO_CHUNK_CRC 0x0080 /* currently at the chunk crc */ ++# define PNG_IO_MASK_OP 0x000f /* current operation: reading/writing */ ++# define PNG_IO_MASK_LOC 0x00f0 /* current location: sig/hdr/data/crc */ ++#endif /* IO_STATE */ ++ ++/* Interlace support. The following macros are always defined so that if ++ * libpng interlace handling is turned off the macros may be used to handle ++ * interlaced images within the application. ++ */ ++#define PNG_INTERLACE_ADAM7_PASSES 7 ++ ++/* Two macros to return the first row and first column of the original, ++ * full, image which appears in a given pass. 'pass' is in the range 0 ++ * to 6 and the result is in the range 0 to 7. ++ */ ++#define PNG_PASS_START_ROW(pass) (((1&~(pass))<<(3-((pass)>>1)))&7) ++#define PNG_PASS_START_COL(pass) (((1& (pass))<<(3-(((pass)+1)>>1)))&7) ++ ++/* A macro to return the offset between pixels in the output row for a pair of ++ * pixels in the input - effectively the inverse of the 'COL_SHIFT' macro that ++ * follows. Note that ROW_OFFSET is the offset from one row to the next whereas ++ * COL_OFFSET is from one column to the next, within a row. ++ */ ++#define PNG_PASS_ROW_OFFSET(pass) ((pass)>2?(8>>(((pass)-1)>>1)):8) ++#define PNG_PASS_COL_OFFSET(pass) (1<<((7-(pass))>>1)) ++ ++/* Two macros to help evaluate the number of rows or columns in each ++ * pass. This is expressed as a shift - effectively log2 of the number or ++ * rows or columns in each 8x8 tile of the original image. ++ */ ++#define PNG_PASS_ROW_SHIFT(pass) ((pass)>2?(8-(pass))>>1:3) ++#define PNG_PASS_COL_SHIFT(pass) ((pass)>1?(7-(pass))>>1:3) ++ ++/* Hence two macros to determine the number of rows or columns in a given ++ * pass of an image given its height or width. In fact these macros may ++ * return non-zero even though the sub-image is empty, because the other ++ * dimension may be empty for a small image. ++ */ ++#define PNG_PASS_ROWS(height, pass) (((height)+(((1<>PNG_PASS_ROW_SHIFT(pass)) ++#define PNG_PASS_COLS(width, pass) (((width)+(((1<>PNG_PASS_COL_SHIFT(pass)) ++ ++/* For the reader row callbacks (both progressive and sequential) it is ++ * necessary to find the row in the output image given a row in an interlaced ++ * image, so two more macros: ++ */ ++#define PNG_ROW_FROM_PASS_ROW(y_in, pass) \ ++ (((y_in)<>(((7-(off))-(pass))<<2)) & 0xF) | \ ++ ((0x01145AF0>>(((7-(off))-(pass))<<2)) & 0xF0)) ++ ++#define PNG_ROW_IN_INTERLACE_PASS(y, pass) \ ++ ((PNG_PASS_MASK(pass,0) >> ((y)&7)) & 1) ++#define PNG_COL_IN_INTERLACE_PASS(x, pass) \ ++ ((PNG_PASS_MASK(pass,1) >> ((x)&7)) & 1) ++ ++#ifdef PNG_READ_COMPOSITE_NODIV_SUPPORTED ++/* With these routines we avoid an integer divide, which will be slower on ++ * most machines. However, it does take more operations than the corresponding ++ * divide method, so it may be slower on a few RISC systems. There are two ++ * shifts (by 8 or 16 bits) and an addition, versus a single integer divide. ++ * ++ * Note that the rounding factors are NOT supposed to be the same! 128 and ++ * 32768 are correct for the NODIV code; 127 and 32767 are correct for the ++ * standard method. ++ * ++ * [Optimized code by Greg Roelofs and Mark Adler...blame us for bugs. :-) ] ++ */ ++ ++ /* fg and bg should be in `gamma 1.0' space; alpha is the opacity */ ++ ++# define png_composite(composite, fg, alpha, bg) \ ++ { \ ++ png_uint_16 temp = (png_uint_16)((png_uint_16)(fg) \ ++ * (png_uint_16)(alpha) \ ++ + (png_uint_16)(bg)*(png_uint_16)(255 \ ++ - (png_uint_16)(alpha)) + 128); \ ++ (composite) = (png_byte)(((temp + (temp >> 8)) >> 8) & 0xff); \ ++ } ++ ++# define png_composite_16(composite, fg, alpha, bg) \ ++ { \ ++ png_uint_32 temp = (png_uint_32)((png_uint_32)(fg) \ ++ * (png_uint_32)(alpha) \ ++ + (png_uint_32)(bg)*(65535 \ ++ - (png_uint_32)(alpha)) + 32768); \ ++ (composite) = (png_uint_16)(0xffff & ((temp + (temp >> 16)) >> 16)); \ ++ } ++ ++#else /* Standard method using integer division */ ++ ++# define png_composite(composite, fg, alpha, bg) \ ++ (composite) = \ ++ (png_byte)(0xff & (((png_uint_16)(fg) * (png_uint_16)(alpha) + \ ++ (png_uint_16)(bg) * (png_uint_16)(255 - (png_uint_16)(alpha)) + \ ++ 127) / 255)) ++ ++# define png_composite_16(composite, fg, alpha, bg) \ ++ (composite) = \ ++ (png_uint_16)(0xffff & (((png_uint_32)(fg) * (png_uint_32)(alpha) + \ ++ (png_uint_32)(bg)*(png_uint_32)(65535 - (png_uint_32)(alpha)) + \ ++ 32767) / 65535)) ++#endif /* READ_COMPOSITE_NODIV */ ++ ++#ifdef PNG_READ_INT_FUNCTIONS_SUPPORTED ++PNG_EXPORT(201, png_uint_32, png_get_uint_32, (png_const_bytep buf)); ++PNG_EXPORT(202, png_uint_16, png_get_uint_16, (png_const_bytep buf)); ++PNG_EXPORT(203, png_int_32, png_get_int_32, (png_const_bytep buf)); ++#endif ++ ++PNG_EXPORT(204, png_uint_32, png_get_uint_31, (png_const_structrp png_ptr, ++ png_const_bytep buf)); ++/* No png_get_int_16 -- may be added if there's a real need for it. */ ++ ++/* Place a 32-bit number into a buffer in PNG byte order (big-endian). */ ++#ifdef PNG_WRITE_INT_FUNCTIONS_SUPPORTED ++PNG_EXPORT(205, void, png_save_uint_32, (png_bytep buf, png_uint_32 i)); ++#endif ++#ifdef PNG_SAVE_INT_32_SUPPORTED ++PNG_EXPORT(206, void, png_save_int_32, (png_bytep buf, png_int_32 i)); ++#endif ++ ++/* Place a 16-bit number into a buffer in PNG byte order. ++ * The parameter is declared unsigned int, not png_uint_16, ++ * just to avoid potential problems on pre-ANSI C compilers. ++ */ ++#ifdef PNG_WRITE_INT_FUNCTIONS_SUPPORTED ++PNG_EXPORT(207, void, png_save_uint_16, (png_bytep buf, unsigned int i)); ++/* No png_save_int_16 -- may be added if there's a real need for it. */ ++#endif ++ ++#ifdef PNG_USE_READ_MACROS ++/* Inline macros to do direct reads of bytes from the input buffer. ++ * The png_get_int_32() routine assumes we are using two's complement ++ * format for negative values, which is almost certainly true. ++ */ ++# define PNG_get_uint_32(buf) \ ++ (((png_uint_32)(*(buf)) << 24) + \ ++ ((png_uint_32)(*((buf) + 1)) << 16) + \ ++ ((png_uint_32)(*((buf) + 2)) << 8) + \ ++ ((png_uint_32)(*((buf) + 3)))) ++ ++ /* From libpng-1.4.0 until 1.4.4, the png_get_uint_16 macro (but not the ++ * function) incorrectly returned a value of type png_uint_32. ++ */ ++# define PNG_get_uint_16(buf) \ ++ ((png_uint_16) \ ++ (((unsigned int)(*(buf)) << 8) + \ ++ ((unsigned int)(*((buf) + 1))))) ++ ++# define PNG_get_int_32(buf) \ ++ ((png_int_32)((*(buf) & 0x80) \ ++ ? -((png_int_32)(((png_get_uint_32(buf)^0xffffffffU)+1U)&0x7fffffffU)) \ ++ : (png_int_32)png_get_uint_32(buf))) ++ ++/* If PNG_PREFIX is defined the same thing as below happens in pnglibconf.h, ++ * but defining a macro name prefixed with PNG_PREFIX. ++ */ ++# ifndef PNG_PREFIX ++# define png_get_uint_32(buf) PNG_get_uint_32(buf) ++# define png_get_uint_16(buf) PNG_get_uint_16(buf) ++# define png_get_int_32(buf) PNG_get_int_32(buf) ++# endif ++#else ++# ifdef PNG_PREFIX ++ /* No macros; revert to the (redefined) function */ ++# define PNG_get_uint_32 (png_get_uint_32) ++# define PNG_get_uint_16 (png_get_uint_16) ++# define PNG_get_int_32 (png_get_int_32) ++# endif ++#endif ++ ++#ifdef PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED ++PNG_EXPORT(242, void, png_set_check_for_invalid_index, ++ (png_structrp png_ptr, int allowed)); ++# ifdef PNG_GET_PALETTE_MAX_SUPPORTED ++PNG_EXPORT(243, int, png_get_palette_max, (png_const_structp png_ptr, ++ png_const_infop info_ptr)); ++# endif ++#endif /* CHECK_FOR_INVALID_INDEX */ ++ ++/******************************************************************************* ++ * Section 5: SIMPLIFIED API ++ ******************************************************************************* ++ * ++ * Please read the documentation in libpng-manual.txt (TODO: write said ++ * documentation) if you don't understand what follows. ++ * ++ * The simplified API hides the details of both libpng and the PNG file format ++ * itself. It allows PNG files to be read into a very limited number of ++ * in-memory bitmap formats or to be written from the same formats. If these ++ * formats do not accommodate your needs then you can, and should, use the more ++ * sophisticated APIs above - these support a wide variety of in-memory formats ++ * and a wide variety of sophisticated transformations to those formats as well ++ * as a wide variety of APIs to manipulate ancillary information. ++ * ++ * To read a PNG file using the simplified API: ++ * ++ * 1) Declare a 'png_image' structure (see below) on the stack, set the ++ * version field to PNG_IMAGE_VERSION and the 'opaque' pointer to NULL ++ * (this is REQUIRED, your program may crash if you don't do it.) ++ * 2) Call the appropriate png_image_begin_read... function. ++ * 3) Set the png_image 'format' member to the required sample format. ++ * 4) Allocate a buffer for the image and, if required, the color-map. ++ * 5) Call png_image_finish_read to read the image and, if required, the ++ * color-map into your buffers. ++ * ++ * There are no restrictions on the format of the PNG input itself; all valid ++ * color types, bit depths, and interlace methods are acceptable, and the ++ * input image is transformed as necessary to the requested in-memory format ++ * during the png_image_finish_read() step. The only caveat is that if you ++ * request a color-mapped image from a PNG that is full-color or makes ++ * complex use of an alpha channel the transformation is extremely lossy and the ++ * result may look terrible. ++ * ++ * To write a PNG file using the simplified API: ++ * ++ * 1) Declare a 'png_image' structure on the stack and memset() it to all zero. ++ * 2) Initialize the members of the structure that describe the image, setting ++ * the 'format' member to the format of the image samples. ++ * 3) Call the appropriate png_image_write... function with a pointer to the ++ * image and, if necessary, the color-map to write the PNG data. ++ * ++ * png_image is a structure that describes the in-memory format of an image ++ * when it is being read or defines the in-memory format of an image that you ++ * need to write: ++ */ ++#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) || \ ++ defined(PNG_SIMPLIFIED_WRITE_SUPPORTED) ++ ++#define PNG_IMAGE_VERSION 1 ++ ++typedef struct png_control *png_controlp; ++typedef struct ++{ ++ png_controlp opaque; /* Initialize to NULL, free with png_image_free */ ++ png_uint_32 version; /* Set to PNG_IMAGE_VERSION */ ++ png_uint_32 width; /* Image width in pixels (columns) */ ++ png_uint_32 height; /* Image height in pixels (rows) */ ++ png_uint_32 format; /* Image format as defined below */ ++ png_uint_32 flags; /* A bit mask containing informational flags */ ++ png_uint_32 colormap_entries; ++ /* Number of entries in the color-map */ ++ ++ /* In the event of an error or warning the following field will be set to a ++ * non-zero value and the 'message' field will contain a '\0' terminated ++ * string with the libpng error or warning message. If both warnings and ++ * an error were encountered, only the error is recorded. If there ++ * are multiple warnings, only the first one is recorded. ++ * ++ * The upper 30 bits of this value are reserved, the low two bits contain ++ * a value as follows: ++ */ ++# define PNG_IMAGE_WARNING 1 ++# define PNG_IMAGE_ERROR 2 ++ /* ++ * The result is a two-bit code such that a value more than 1 indicates ++ * a failure in the API just called: ++ * ++ * 0 - no warning or error ++ * 1 - warning ++ * 2 - error ++ * 3 - error preceded by warning ++ */ ++# define PNG_IMAGE_FAILED(png_cntrl) ((((png_cntrl).warning_or_error)&0x03)>1) ++ ++ png_uint_32 warning_or_error; ++ ++ char message[64]; ++} png_image, *png_imagep; ++ ++/* The samples of the image have one to four channels whose components have ++ * original values in the range 0 to 1.0: ++ * ++ * 1: A single gray or luminance channel (G). ++ * 2: A gray/luminance channel and an alpha channel (GA). ++ * 3: Three red, green, blue color channels (RGB). ++ * 4: Three color channels and an alpha channel (RGBA). ++ * ++ * The components are encoded in one of two ways: ++ * ++ * a) As a small integer, value 0..255, contained in a single byte. For the ++ * alpha channel the original value is simply value/255. For the color or ++ * luminance channels the value is encoded according to the sRGB specification ++ * and matches the 8-bit format expected by typical display devices. ++ * ++ * The color/gray channels are not scaled (pre-multiplied) by the alpha ++ * channel and are suitable for passing to color management software. ++ * ++ * b) As a value in the range 0..65535, contained in a 2-byte integer. All ++ * channels can be converted to the original value by dividing by 65535; all ++ * channels are linear. Color channels use the RGB encoding (RGB end-points) of ++ * the sRGB specification. This encoding is identified by the ++ * PNG_FORMAT_FLAG_LINEAR flag below. ++ * ++ * When the simplified API needs to convert between sRGB and linear colorspaces, ++ * the actual sRGB transfer curve defined in the sRGB specification (see the ++ * article at ) is used, not the gamma=1/2.2 ++ * approximation used elsewhere in libpng. ++ * ++ * When an alpha channel is present it is expected to denote pixel coverage ++ * of the color or luminance channels and is returned as an associated alpha ++ * channel: the color/gray channels are scaled (pre-multiplied) by the alpha ++ * value. ++ * ++ * The samples are either contained directly in the image data, between 1 and 8 ++ * bytes per pixel according to the encoding, or are held in a color-map indexed ++ * by bytes in the image data. In the case of a color-map the color-map entries ++ * are individual samples, encoded as above, and the image data has one byte per ++ * pixel to select the relevant sample from the color-map. ++ */ ++ ++/* PNG_FORMAT_* ++ * ++ * #defines to be used in png_image::format. Each #define identifies a ++ * particular layout of sample data and, if present, alpha values. There are ++ * separate defines for each of the two component encodings. ++ * ++ * A format is built up using single bit flag values. All combinations are ++ * valid. Formats can be built up from the flag values or you can use one of ++ * the predefined values below. When testing formats always use the FORMAT_FLAG ++ * macros to test for individual features - future versions of the library may ++ * add new flags. ++ * ++ * When reading or writing color-mapped images the format should be set to the ++ * format of the entries in the color-map then png_image_{read,write}_colormap ++ * called to read or write the color-map and set the format correctly for the ++ * image data. Do not set the PNG_FORMAT_FLAG_COLORMAP bit directly! ++ * ++ * NOTE: libpng can be built with particular features disabled. If you see ++ * compiler errors because the definition of one of the following flags has been ++ * compiled out it is because libpng does not have the required support. It is ++ * possible, however, for the libpng configuration to enable the format on just ++ * read or just write; in that case you may see an error at run time. You can ++ * guard against this by checking for the definition of the appropriate ++ * "_SUPPORTED" macro, one of: ++ * ++ * PNG_SIMPLIFIED_{READ,WRITE}_{BGR,AFIRST}_SUPPORTED ++ */ ++#define PNG_FORMAT_FLAG_ALPHA 0x01U /* format with an alpha channel */ ++#define PNG_FORMAT_FLAG_COLOR 0x02U /* color format: otherwise grayscale */ ++#define PNG_FORMAT_FLAG_LINEAR 0x04U /* 2-byte channels else 1-byte */ ++#define PNG_FORMAT_FLAG_COLORMAP 0x08U /* image data is color-mapped */ ++ ++#ifdef PNG_FORMAT_BGR_SUPPORTED ++# define PNG_FORMAT_FLAG_BGR 0x10U /* BGR colors, else order is RGB */ ++#endif ++ ++#ifdef PNG_FORMAT_AFIRST_SUPPORTED ++# define PNG_FORMAT_FLAG_AFIRST 0x20U /* alpha channel comes first */ ++#endif ++ ++#define PNG_FORMAT_FLAG_ASSOCIATED_ALPHA 0x40U /* alpha channel is associated */ ++ ++/* Commonly used formats have predefined macros. ++ * ++ * First the single byte (sRGB) formats: ++ */ ++#define PNG_FORMAT_GRAY 0 ++#define PNG_FORMAT_GA PNG_FORMAT_FLAG_ALPHA ++#define PNG_FORMAT_AG (PNG_FORMAT_GA|PNG_FORMAT_FLAG_AFIRST) ++#define PNG_FORMAT_RGB PNG_FORMAT_FLAG_COLOR ++#define PNG_FORMAT_BGR (PNG_FORMAT_FLAG_COLOR|PNG_FORMAT_FLAG_BGR) ++#define PNG_FORMAT_RGBA (PNG_FORMAT_RGB|PNG_FORMAT_FLAG_ALPHA) ++#define PNG_FORMAT_ARGB (PNG_FORMAT_RGBA|PNG_FORMAT_FLAG_AFIRST) ++#define PNG_FORMAT_BGRA (PNG_FORMAT_BGR|PNG_FORMAT_FLAG_ALPHA) ++#define PNG_FORMAT_ABGR (PNG_FORMAT_BGRA|PNG_FORMAT_FLAG_AFIRST) ++ ++/* Then the linear 2-byte formats. When naming these "Y" is used to ++ * indicate a luminance (gray) channel. ++ */ ++#define PNG_FORMAT_LINEAR_Y PNG_FORMAT_FLAG_LINEAR ++#define PNG_FORMAT_LINEAR_Y_ALPHA (PNG_FORMAT_FLAG_LINEAR|PNG_FORMAT_FLAG_ALPHA) ++#define PNG_FORMAT_LINEAR_RGB (PNG_FORMAT_FLAG_LINEAR|PNG_FORMAT_FLAG_COLOR) ++#define PNG_FORMAT_LINEAR_RGB_ALPHA \ ++ (PNG_FORMAT_FLAG_LINEAR|PNG_FORMAT_FLAG_COLOR|PNG_FORMAT_FLAG_ALPHA) ++ ++/* With color-mapped formats the image data is one byte for each pixel, the byte ++ * is an index into the color-map which is formatted as above. To obtain a ++ * color-mapped format it is sufficient just to add the PNG_FOMAT_FLAG_COLORMAP ++ * to one of the above definitions, or you can use one of the definitions below. ++ */ ++#define PNG_FORMAT_RGB_COLORMAP (PNG_FORMAT_RGB|PNG_FORMAT_FLAG_COLORMAP) ++#define PNG_FORMAT_BGR_COLORMAP (PNG_FORMAT_BGR|PNG_FORMAT_FLAG_COLORMAP) ++#define PNG_FORMAT_RGBA_COLORMAP (PNG_FORMAT_RGBA|PNG_FORMAT_FLAG_COLORMAP) ++#define PNG_FORMAT_ARGB_COLORMAP (PNG_FORMAT_ARGB|PNG_FORMAT_FLAG_COLORMAP) ++#define PNG_FORMAT_BGRA_COLORMAP (PNG_FORMAT_BGRA|PNG_FORMAT_FLAG_COLORMAP) ++#define PNG_FORMAT_ABGR_COLORMAP (PNG_FORMAT_ABGR|PNG_FORMAT_FLAG_COLORMAP) ++ ++/* PNG_IMAGE macros ++ * ++ * These are convenience macros to derive information from a png_image ++ * structure. The PNG_IMAGE_SAMPLE_ macros return values appropriate to the ++ * actual image sample values - either the entries in the color-map or the ++ * pixels in the image. The PNG_IMAGE_PIXEL_ macros return corresponding values ++ * for the pixels and will always return 1 for color-mapped formats. The ++ * remaining macros return information about the rows in the image and the ++ * complete image. ++ * ++ * NOTE: All the macros that take a png_image::format parameter are compile time ++ * constants if the format parameter is, itself, a constant. Therefore these ++ * macros can be used in array declarations and case labels where required. ++ * Similarly the macros are also pre-processor constants (sizeof is not used) so ++ * they can be used in #if tests. ++ * ++ * First the information about the samples. ++ */ ++#define PNG_IMAGE_SAMPLE_CHANNELS(fmt)\ ++ (((fmt)&(PNG_FORMAT_FLAG_COLOR|PNG_FORMAT_FLAG_ALPHA))+1) ++ /* Return the total number of channels in a given format: 1..4 */ ++ ++#define PNG_IMAGE_SAMPLE_COMPONENT_SIZE(fmt)\ ++ ((((fmt) & PNG_FORMAT_FLAG_LINEAR) >> 2)+1) ++ /* Return the size in bytes of a single component of a pixel or color-map ++ * entry (as appropriate) in the image: 1 or 2. ++ */ ++ ++#define PNG_IMAGE_SAMPLE_SIZE(fmt)\ ++ (PNG_IMAGE_SAMPLE_CHANNELS(fmt) * PNG_IMAGE_SAMPLE_COMPONENT_SIZE(fmt)) ++ /* This is the size of the sample data for one sample. If the image is ++ * color-mapped it is the size of one color-map entry (and image pixels are ++ * one byte in size), otherwise it is the size of one image pixel. ++ */ ++ ++#define PNG_IMAGE_MAXIMUM_COLORMAP_COMPONENTS(fmt)\ ++ (PNG_IMAGE_SAMPLE_CHANNELS(fmt) * 256) ++ /* The maximum size of the color-map required by the format expressed in a ++ * count of components. This can be used to compile-time allocate a ++ * color-map: ++ * ++ * png_uint_16 colormap[PNG_IMAGE_MAXIMUM_COLORMAP_COMPONENTS(linear_fmt)]; ++ * ++ * png_byte colormap[PNG_IMAGE_MAXIMUM_COLORMAP_COMPONENTS(sRGB_fmt)]; ++ * ++ * Alternatively use the PNG_IMAGE_COLORMAP_SIZE macro below to use the ++ * information from one of the png_image_begin_read_ APIs and dynamically ++ * allocate the required memory. ++ */ ++ ++/* Corresponding information about the pixels */ ++#define PNG_IMAGE_PIXEL_(test,fmt)\ ++ (((fmt)&PNG_FORMAT_FLAG_COLORMAP)?1:test(fmt)) ++ ++#define PNG_IMAGE_PIXEL_CHANNELS(fmt)\ ++ PNG_IMAGE_PIXEL_(PNG_IMAGE_SAMPLE_CHANNELS,fmt) ++ /* The number of separate channels (components) in a pixel; 1 for a ++ * color-mapped image. ++ */ ++ ++#define PNG_IMAGE_PIXEL_COMPONENT_SIZE(fmt)\ ++ PNG_IMAGE_PIXEL_(PNG_IMAGE_SAMPLE_COMPONENT_SIZE,fmt) ++ /* The size, in bytes, of each component in a pixel; 1 for a color-mapped ++ * image. ++ */ ++ ++#define PNG_IMAGE_PIXEL_SIZE(fmt) PNG_IMAGE_PIXEL_(PNG_IMAGE_SAMPLE_SIZE,fmt) ++ /* The size, in bytes, of a complete pixel; 1 for a color-mapped image. */ ++ ++/* Information about the whole row, or whole image */ ++#define PNG_IMAGE_ROW_STRIDE(image)\ ++ (PNG_IMAGE_PIXEL_CHANNELS((image).format) * (image).width) ++ /* Return the total number of components in a single row of the image; this ++ * is the minimum 'row stride', the minimum count of components between each ++ * row. For a color-mapped image this is the minimum number of bytes in a ++ * row. ++ * ++ * WARNING: this macro overflows for some images with more than one component ++ * and very large image widths. libpng will refuse to process an image where ++ * this macro would overflow. ++ */ ++ ++#define PNG_IMAGE_BUFFER_SIZE(image, row_stride)\ ++ (PNG_IMAGE_PIXEL_COMPONENT_SIZE((image).format)*(image).height*(row_stride)) ++ /* Return the size, in bytes, of an image buffer given a png_image and a row ++ * stride - the number of components to leave space for in each row. ++ * ++ * WARNING: this macro overflows a 32-bit integer for some large PNG images, ++ * libpng will refuse to process an image where such an overflow would occur. ++ */ ++ ++#define PNG_IMAGE_SIZE(image)\ ++ PNG_IMAGE_BUFFER_SIZE(image, PNG_IMAGE_ROW_STRIDE(image)) ++ /* Return the size, in bytes, of the image in memory given just a png_image; ++ * the row stride is the minimum stride required for the image. ++ */ ++ ++#define PNG_IMAGE_COLORMAP_SIZE(image)\ ++ (PNG_IMAGE_SAMPLE_SIZE((image).format) * (image).colormap_entries) ++ /* Return the size, in bytes, of the color-map of this image. If the image ++ * format is not a color-map format this will return a size sufficient for ++ * 256 entries in the given format; check PNG_FORMAT_FLAG_COLORMAP if ++ * you don't want to allocate a color-map in this case. ++ */ ++ ++/* PNG_IMAGE_FLAG_* ++ * ++ * Flags containing additional information about the image are held in the ++ * 'flags' field of png_image. ++ */ ++#define PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB 0x01 ++ /* This indicates that the RGB values of the in-memory bitmap do not ++ * correspond to the red, green and blue end-points defined by sRGB. ++ */ ++ ++#define PNG_IMAGE_FLAG_FAST 0x02 ++ /* On write emphasise speed over compression; the resultant PNG file will be ++ * larger but will be produced significantly faster, particular for large ++ * images. Do not use this option for images which will be distributed, only ++ * used it when producing intermediate files that will be read back in ++ * repeatedly. For a typical 24-bit image the option will double the read ++ * speed at the cost of increasing the image size by 25%, however for many ++ * more compressible images the PNG file can be 10 times larger with only a ++ * slight speed gain. ++ */ ++ ++#define PNG_IMAGE_FLAG_16BIT_sRGB 0x04 ++ /* On read if the image is a 16-bit per component image and there is no gAMA ++ * or sRGB chunk assume that the components are sRGB encoded. Notice that ++ * images output by the simplified API always have gamma information; setting ++ * this flag only affects the interpretation of 16-bit images from an ++ * external source. It is recommended that the application expose this flag ++ * to the user; the user can normally easily recognize the difference between ++ * linear and sRGB encoding. This flag has no effect on write - the data ++ * passed to the write APIs must have the correct encoding (as defined ++ * above.) ++ * ++ * If the flag is not set (the default) input 16-bit per component data is ++ * assumed to be linear. ++ * ++ * NOTE: the flag can only be set after the png_image_begin_read_ call, ++ * because that call initializes the 'flags' field. ++ */ ++ ++#ifdef PNG_SIMPLIFIED_READ_SUPPORTED ++/* READ APIs ++ * --------- ++ * ++ * The png_image passed to the read APIs must have been initialized by setting ++ * the png_controlp field 'opaque' to NULL (or, safer, memset the whole thing.) ++ */ ++#ifdef PNG_STDIO_SUPPORTED ++PNG_EXPORT(234, int, png_image_begin_read_from_file, (png_imagep image, ++ const char *file_name)); ++ /* The named file is opened for read and the image header is filled in ++ * from the PNG header in the file. ++ */ ++ ++PNG_EXPORT(235, int, png_image_begin_read_from_stdio, (png_imagep image, ++ FILE* file)); ++ /* The PNG header is read from the stdio FILE object. */ ++#endif /* STDIO */ ++ ++PNG_EXPORT(236, int, png_image_begin_read_from_memory, (png_imagep image, ++ png_const_voidp memory, size_t size)); ++ /* The PNG header is read from the given memory buffer. */ ++ ++PNG_EXPORT(237, int, png_image_finish_read, (png_imagep image, ++ png_const_colorp background, void *buffer, png_int_32 row_stride, ++ void *colormap)); ++ /* Finish reading the image into the supplied buffer and clean up the ++ * png_image structure. ++ * ++ * row_stride is the step, in byte or 2-byte units as appropriate, ++ * between adjacent rows. A positive stride indicates that the top-most row ++ * is first in the buffer - the normal top-down arrangement. A negative ++ * stride indicates that the bottom-most row is first in the buffer. ++ * ++ * background need only be supplied if an alpha channel must be removed from ++ * a png_byte format and the removal is to be done by compositing on a solid ++ * color; otherwise it may be NULL and any composition will be done directly ++ * onto the buffer. The value is an sRGB color to use for the background, ++ * for grayscale output the green channel is used. ++ * ++ * background must be supplied when an alpha channel must be removed from a ++ * single byte color-mapped output format, in other words if: ++ * ++ * 1) The original format from png_image_begin_read_from_* had ++ * PNG_FORMAT_FLAG_ALPHA set. ++ * 2) The format set by the application does not. ++ * 3) The format set by the application has PNG_FORMAT_FLAG_COLORMAP set and ++ * PNG_FORMAT_FLAG_LINEAR *not* set. ++ * ++ * For linear output removing the alpha channel is always done by compositing ++ * on black and background is ignored. ++ * ++ * colormap must be supplied when PNG_FORMAT_FLAG_COLORMAP is set. It must ++ * be at least the size (in bytes) returned by PNG_IMAGE_COLORMAP_SIZE. ++ * image->colormap_entries will be updated to the actual number of entries ++ * written to the colormap; this may be less than the original value. ++ */ ++ ++PNG_EXPORT(238, void, png_image_free, (png_imagep image)); ++ /* Free any data allocated by libpng in image->opaque, setting the pointer to ++ * NULL. May be called at any time after the structure is initialized. ++ */ ++#endif /* SIMPLIFIED_READ */ ++ ++#ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED ++/* WRITE APIS ++ * ---------- ++ * For write you must initialize a png_image structure to describe the image to ++ * be written. To do this use memset to set the whole structure to 0 then ++ * initialize fields describing your image. ++ * ++ * version: must be set to PNG_IMAGE_VERSION ++ * opaque: must be initialized to NULL ++ * width: image width in pixels ++ * height: image height in rows ++ * format: the format of the data (image and color-map) you wish to write ++ * flags: set to 0 unless one of the defined flags applies; set ++ * PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB for color format images where the RGB ++ * values do not correspond to the colors in sRGB. ++ * colormap_entries: set to the number of entries in the color-map (0 to 256) ++ */ ++#ifdef PNG_SIMPLIFIED_WRITE_STDIO_SUPPORTED ++PNG_EXPORT(239, int, png_image_write_to_file, (png_imagep image, ++ const char *file, int convert_to_8bit, const void *buffer, ++ png_int_32 row_stride, const void *colormap)); ++ /* Write the image to the named file. */ ++ ++PNG_EXPORT(240, int, png_image_write_to_stdio, (png_imagep image, FILE *file, ++ int convert_to_8_bit, const void *buffer, png_int_32 row_stride, ++ const void *colormap)); ++ /* Write the image to the given (FILE*). */ ++#endif /* SIMPLIFIED_WRITE_STDIO */ ++ ++/* With all write APIs if image is in one of the linear formats with 16-bit ++ * data then setting convert_to_8_bit will cause the output to be an 8-bit PNG ++ * gamma encoded according to the sRGB specification, otherwise a 16-bit linear ++ * encoded PNG file is written. ++ * ++ * With color-mapped data formats the colormap parameter point to a color-map ++ * with at least image->colormap_entries encoded in the specified format. If ++ * the format is linear the written PNG color-map will be converted to sRGB ++ * regardless of the convert_to_8_bit flag. ++ * ++ * With all APIs row_stride is handled as in the read APIs - it is the spacing ++ * from one row to the next in component sized units (1 or 2 bytes) and if ++ * negative indicates a bottom-up row layout in the buffer. If row_stride is ++ * zero, libpng will calculate it for you from the image width and number of ++ * channels. ++ * ++ * Note that the write API does not support interlacing, sub-8-bit pixels or ++ * most ancillary chunks. If you need to write text chunks (e.g. for copyright ++ * notices) you need to use one of the other APIs. ++ */ ++ ++PNG_EXPORT(245, int, png_image_write_to_memory, (png_imagep image, void *memory, ++ png_alloc_size_t * PNG_RESTRICT memory_bytes, int convert_to_8_bit, ++ const void *buffer, png_int_32 row_stride, const void *colormap)); ++ /* Write the image to the given memory buffer. The function both writes the ++ * whole PNG data stream to *memory and updates *memory_bytes with the count ++ * of bytes written. ++ * ++ * 'memory' may be NULL. In this case *memory_bytes is not read however on ++ * success the number of bytes which would have been written will still be ++ * stored in *memory_bytes. On failure *memory_bytes will contain 0. ++ * ++ * If 'memory' is not NULL it must point to memory[*memory_bytes] of ++ * writeable memory. ++ * ++ * If the function returns success memory[*memory_bytes] (if 'memory' is not ++ * NULL) contains the written PNG data. *memory_bytes will always be less ++ * than or equal to the original value. ++ * ++ * If the function returns false and *memory_bytes was not changed an error ++ * occurred during write. If *memory_bytes was changed, or is not 0 if ++ * 'memory' was NULL, the write would have succeeded but for the memory ++ * buffer being too small. *memory_bytes contains the required number of ++ * bytes and will be bigger that the original value. ++ */ ++ ++#define png_image_write_get_memory_size(image, size, convert_to_8_bit, buffer,\ ++ row_stride, colormap)\ ++ png_image_write_to_memory(&(image), 0, &(size), convert_to_8_bit, buffer,\ ++ row_stride, colormap) ++ /* Return the amount of memory in 'size' required to compress this image. ++ * The png_image structure 'image' must be filled in as in the above ++ * function and must not be changed before the actual write call, the buffer ++ * and all other parameters must also be identical to that in the final ++ * write call. The 'size' variable need not be initialized. ++ * ++ * NOTE: the macro returns true/false, if false is returned 'size' will be ++ * set to zero and the write failed and probably will fail if tried again. ++ */ ++ ++/* You can pre-allocate the buffer by making sure it is of sufficient size ++ * regardless of the amount of compression achieved. The buffer size will ++ * always be bigger than the original image and it will never be filled. The ++ * following macros are provided to assist in allocating the buffer. ++ */ ++#define PNG_IMAGE_DATA_SIZE(image) (PNG_IMAGE_SIZE(image)+(image).height) ++ /* The number of uncompressed bytes in the PNG byte encoding of the image; ++ * uncompressing the PNG IDAT data will give this number of bytes. ++ * ++ * NOTE: while PNG_IMAGE_SIZE cannot overflow for an image in memory this ++ * macro can because of the extra bytes used in the PNG byte encoding. You ++ * need to avoid this macro if your image size approaches 2^30 in width or ++ * height. The same goes for the remainder of these macros; they all produce ++ * bigger numbers than the actual in-memory image size. ++ */ ++#ifndef PNG_ZLIB_MAX_SIZE ++# define PNG_ZLIB_MAX_SIZE(b) ((b)+(((b)+7U)>>3)+(((b)+63U)>>6)+11U) ++ /* An upper bound on the number of compressed bytes given 'b' uncompressed ++ * bytes. This is based on deflateBounds() in zlib; different ++ * implementations of zlib compression may conceivably produce more data so ++ * if your zlib implementation is not zlib itself redefine this macro ++ * appropriately. ++ */ ++#endif ++ ++#define PNG_IMAGE_COMPRESSED_SIZE_MAX(image)\ ++ PNG_ZLIB_MAX_SIZE((png_alloc_size_t)PNG_IMAGE_DATA_SIZE(image)) ++ /* An upper bound on the size of the data in the PNG IDAT chunks. */ ++ ++#define PNG_IMAGE_PNG_SIZE_MAX_(image, image_size)\ ++ ((8U/*sig*/+25U/*IHDR*/+16U/*gAMA*/+44U/*cHRM*/+12U/*IEND*/+\ ++ (((image).format&PNG_FORMAT_FLAG_COLORMAP)?/*colormap: PLTE, tRNS*/\ ++ 12U+3U*(image).colormap_entries/*PLTE data*/+\ ++ (((image).format&PNG_FORMAT_FLAG_ALPHA)?\ ++ 12U/*tRNS*/+(image).colormap_entries:0U):0U)+\ ++ 12U)+(12U*((image_size)/PNG_ZBUF_SIZE))/*IDAT*/+(image_size)) ++ /* A helper for the following macro; if your compiler cannot handle the ++ * following macro use this one with the result of ++ * PNG_IMAGE_COMPRESSED_SIZE_MAX(image) as the second argument (most ++ * compilers should handle this just fine.) ++ */ ++ ++#define PNG_IMAGE_PNG_SIZE_MAX(image)\ ++ PNG_IMAGE_PNG_SIZE_MAX_(image, PNG_IMAGE_COMPRESSED_SIZE_MAX(image)) ++ /* An upper bound on the total length of the PNG data stream for 'image'. ++ * The result is of type png_alloc_size_t, on 32-bit systems this may ++ * overflow even though PNG_IMAGE_DATA_SIZE does not overflow; the write will ++ * run out of buffer space but return a corrected size which should work. ++ */ ++#endif /* SIMPLIFIED_WRITE */ ++/******************************************************************************* ++ * END OF SIMPLIFIED API ++ ******************************************************************************/ ++#endif /* SIMPLIFIED_{READ|WRITE} */ ++ ++/******************************************************************************* ++ * Section 6: IMPLEMENTATION OPTIONS ++ ******************************************************************************* ++ * ++ * Support for arbitrary implementation-specific optimizations. The API allows ++ * particular options to be turned on or off. 'Option' is the number of the ++ * option and 'onoff' is 0 (off) or non-0 (on). The value returned is given ++ * by the PNG_OPTION_ defines below. ++ * ++ * HARDWARE: normally hardware capabilities, such as the Intel SSE instructions, ++ * are detected at run time, however sometimes it may be impossible ++ * to do this in user mode, in which case it is necessary to discover ++ * the capabilities in an OS specific way. Such capabilities are ++ * listed here when libpng has support for them and must be turned ++ * ON by the application if present. ++ * ++ * SOFTWARE: sometimes software optimizations actually result in performance ++ * decrease on some architectures or systems, or with some sets of ++ * PNG images. 'Software' options allow such optimizations to be ++ * selected at run time. ++ */ ++#ifdef PNG_SET_OPTION_SUPPORTED ++#ifdef PNG_ARM_NEON_API_SUPPORTED ++# define PNG_ARM_NEON 0 /* HARDWARE: ARM Neon SIMD instructions supported */ ++#endif ++#define PNG_MAXIMUM_INFLATE_WINDOW 2 /* SOFTWARE: force maximum window */ ++#define PNG_SKIP_sRGB_CHECK_PROFILE 4 /* SOFTWARE: Check ICC profile for sRGB */ ++#ifdef PNG_MIPS_MSA_API_SUPPORTED ++# define PNG_MIPS_MSA 6 /* HARDWARE: MIPS Msa SIMD instructions supported */ ++#endif ++#define PNG_IGNORE_ADLER32 8 ++#ifdef PNG_POWERPC_VSX_API_SUPPORTED ++# define PNG_POWERPC_VSX 10 /* HARDWARE: PowerPC VSX SIMD instructions supported */ ++#endif ++#define PNG_OPTION_NEXT 12 /* Next option - numbers must be even */ ++ ++/* Return values: NOTE: there are four values and 'off' is *not* zero */ ++#define PNG_OPTION_UNSET 0 /* Unset - defaults to off */ ++#define PNG_OPTION_INVALID 1 /* Option number out of range */ ++#define PNG_OPTION_OFF 2 ++#define PNG_OPTION_ON 3 ++ ++PNG_EXPORT(244, int, png_set_option, (png_structrp png_ptr, int option, ++ int onoff)); ++#endif /* SET_OPTION */ ++ ++/******************************************************************************* ++ * END OF HARDWARE AND SOFTWARE OPTIONS ++ ******************************************************************************/ ++ ++/* Maintainer: Put new public prototypes here ^, in libpng.3, in project ++ * defs, and in scripts/symbols.def. ++ */ ++ ++/* The last ordinal number (this is the *last* one already used; the next ++ * one to use is one more than this.) ++ */ ++#ifdef PNG_EXPORT_LAST_ORDINAL ++ PNG_EXPORT_LAST_ORDINAL(249); ++#endif ++ ++#ifdef __cplusplus ++} ++#endif ++ ++#endif /* PNG_VERSION_INFO_ONLY */ ++/* Do not put anything past this line */ ++#endif /* PNG_H */ +diff --git a/lib/libpng/include/pngconf.h b/lib/libpng/include/pngconf.h +new file mode 100644 +index 000000000..927a769db +--- /dev/null ++++ b/lib/libpng/include/pngconf.h +@@ -0,0 +1,623 @@ ++ ++/* pngconf.h - machine-configurable file for libpng ++ * ++ * libpng version 1.6.37 ++ * ++ * Copyright (c) 2018-2019 Cosmin Truta ++ * Copyright (c) 1998-2002,2004,2006-2016,2018 Glenn Randers-Pehrson ++ * Copyright (c) 1996-1997 Andreas Dilger ++ * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc. ++ * ++ * This code is released under the libpng license. ++ * For conditions of distribution and use, see the disclaimer ++ * and license in png.h ++ * ++ * Any machine specific code is near the front of this file, so if you ++ * are configuring libpng for a machine, you may want to read the section ++ * starting here down to where it starts to typedef png_color, png_text, ++ * and png_info. ++ */ ++ ++#ifndef PNGCONF_H ++#define PNGCONF_H ++ ++#ifndef PNG_BUILDING_SYMBOL_TABLE /* else includes may cause problems */ ++ ++/* From libpng 1.6.0 libpng requires an ANSI X3.159-1989 ("ISOC90") compliant C ++ * compiler for correct compilation. The following header files are required by ++ * the standard. If your compiler doesn't provide these header files, or they ++ * do not match the standard, you will need to provide/improve them. ++ */ ++#include ++#include ++ ++/* Library header files. These header files are all defined by ISOC90; libpng ++ * expects conformant implementations, however, an ISOC90 conformant system need ++ * not provide these header files if the functionality cannot be implemented. ++ * In this case it will be necessary to disable the relevant parts of libpng in ++ * the build of pnglibconf.h. ++ * ++ * Prior to 1.6.0 string.h was included here; the API changes in 1.6.0 to not ++ * include this unnecessary header file. ++ */ ++ ++#ifdef PNG_STDIO_SUPPORTED ++ /* Required for the definition of FILE: */ ++# include ++#endif ++ ++#ifdef PNG_SETJMP_SUPPORTED ++ /* Required for the definition of jmp_buf and the declaration of longjmp: */ ++# include ++#endif ++ ++#ifdef PNG_CONVERT_tIME_SUPPORTED ++ /* Required for struct tm: */ ++# include ++#endif ++ ++#endif /* PNG_BUILDING_SYMBOL_TABLE */ ++ ++/* Prior to 1.6.0, it was possible to turn off 'const' in declarations, ++ * using PNG_NO_CONST. This is no longer supported. ++ */ ++#define PNG_CONST const /* backward compatibility only */ ++ ++/* This controls optimization of the reading of 16-bit and 32-bit ++ * values from PNG files. It can be set on a per-app-file basis: it ++ * just changes whether a macro is used when the function is called. ++ * The library builder sets the default; if read functions are not ++ * built into the library the macro implementation is forced on. ++ */ ++#ifndef PNG_READ_INT_FUNCTIONS_SUPPORTED ++# define PNG_USE_READ_MACROS ++#endif ++#if !defined(PNG_NO_USE_READ_MACROS) && !defined(PNG_USE_READ_MACROS) ++# if PNG_DEFAULT_READ_MACROS ++# define PNG_USE_READ_MACROS ++# endif ++#endif ++ ++/* COMPILER SPECIFIC OPTIONS. ++ * ++ * These options are provided so that a variety of difficult compilers ++ * can be used. Some are fixed at build time (e.g. PNG_API_RULE ++ * below) but still have compiler specific implementations, others ++ * may be changed on a per-file basis when compiling against libpng. ++ */ ++ ++/* The PNGARG macro was used in versions of libpng prior to 1.6.0 to protect ++ * against legacy (pre ISOC90) compilers that did not understand function ++ * prototypes. It is not required for modern C compilers. ++ */ ++#ifndef PNGARG ++# define PNGARG(arglist) arglist ++#endif ++ ++/* Function calling conventions. ++ * ============================= ++ * Normally it is not necessary to specify to the compiler how to call ++ * a function - it just does it - however on x86 systems derived from ++ * Microsoft and Borland C compilers ('IBM PC', 'DOS', 'Windows' systems ++ * and some others) there are multiple ways to call a function and the ++ * default can be changed on the compiler command line. For this reason ++ * libpng specifies the calling convention of every exported function and ++ * every function called via a user supplied function pointer. This is ++ * done in this file by defining the following macros: ++ * ++ * PNGAPI Calling convention for exported functions. ++ * PNGCBAPI Calling convention for user provided (callback) functions. ++ * PNGCAPI Calling convention used by the ANSI-C library (required ++ * for longjmp callbacks and sometimes used internally to ++ * specify the calling convention for zlib). ++ * ++ * These macros should never be overridden. If it is necessary to ++ * change calling convention in a private build this can be done ++ * by setting PNG_API_RULE (which defaults to 0) to one of the values ++ * below to select the correct 'API' variants. ++ * ++ * PNG_API_RULE=0 Use PNGCAPI - the 'C' calling convention - throughout. ++ * This is correct in every known environment. ++ * PNG_API_RULE=1 Use the operating system convention for PNGAPI and ++ * the 'C' calling convention (from PNGCAPI) for ++ * callbacks (PNGCBAPI). This is no longer required ++ * in any known environment - if it has to be used ++ * please post an explanation of the problem to the ++ * libpng mailing list. ++ * ++ * These cases only differ if the operating system does not use the C ++ * calling convention, at present this just means the above cases ++ * (x86 DOS/Windows systems) and, even then, this does not apply to ++ * Cygwin running on those systems. ++ * ++ * Note that the value must be defined in pnglibconf.h so that what ++ * the application uses to call the library matches the conventions ++ * set when building the library. ++ */ ++ ++/* Symbol export ++ * ============= ++ * When building a shared library it is almost always necessary to tell ++ * the compiler which symbols to export. The png.h macro 'PNG_EXPORT' ++ * is used to mark the symbols. On some systems these symbols can be ++ * extracted at link time and need no special processing by the compiler, ++ * on other systems the symbols are flagged by the compiler and just ++ * the declaration requires a special tag applied (unfortunately) in a ++ * compiler dependent way. Some systems can do either. ++ * ++ * A small number of older systems also require a symbol from a DLL to ++ * be flagged to the program that calls it. This is a problem because ++ * we do not know in the header file included by application code that ++ * the symbol will come from a shared library, as opposed to a statically ++ * linked one. For this reason the application must tell us by setting ++ * the magic flag PNG_USE_DLL to turn on the special processing before ++ * it includes png.h. ++ * ++ * Four additional macros are used to make this happen: ++ * ++ * PNG_IMPEXP The magic (if any) to cause a symbol to be exported from ++ * the build or imported if PNG_USE_DLL is set - compiler ++ * and system specific. ++ * ++ * PNG_EXPORT_TYPE(type) A macro that pre or appends PNG_IMPEXP to ++ * 'type', compiler specific. ++ * ++ * PNG_DLL_EXPORT Set to the magic to use during a libpng build to ++ * make a symbol exported from the DLL. Not used in the ++ * public header files; see pngpriv.h for how it is used ++ * in the libpng build. ++ * ++ * PNG_DLL_IMPORT Set to the magic to force the libpng symbols to come ++ * from a DLL - used to define PNG_IMPEXP when ++ * PNG_USE_DLL is set. ++ */ ++ ++/* System specific discovery. ++ * ========================== ++ * This code is used at build time to find PNG_IMPEXP, the API settings ++ * and PNG_EXPORT_TYPE(), it may also set a macro to indicate the DLL ++ * import processing is possible. On Windows systems it also sets ++ * compiler-specific macros to the values required to change the calling ++ * conventions of the various functions. ++ */ ++#if defined(_Windows) || defined(_WINDOWS) || defined(WIN32) ||\ ++ defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) ++ /* Windows system (DOS doesn't support DLLs). Includes builds under Cygwin or ++ * MinGW on any architecture currently supported by Windows. Also includes ++ * Watcom builds but these need special treatment because they are not ++ * compatible with GCC or Visual C because of different calling conventions. ++ */ ++# if PNG_API_RULE == 2 ++ /* If this line results in an error, either because __watcall is not ++ * understood or because of a redefine just below you cannot use *this* ++ * build of the library with the compiler you are using. *This* build was ++ * build using Watcom and applications must also be built using Watcom! ++ */ ++# define PNGCAPI __watcall ++# endif ++ ++# if defined(__GNUC__) || (defined(_MSC_VER) && (_MSC_VER >= 800)) ++# define PNGCAPI __cdecl ++# if PNG_API_RULE == 1 ++ /* If this line results in an error __stdcall is not understood and ++ * PNG_API_RULE should not have been set to '1'. ++ */ ++# define PNGAPI __stdcall ++# endif ++# else ++ /* An older compiler, or one not detected (erroneously) above, ++ * if necessary override on the command line to get the correct ++ * variants for the compiler. ++ */ ++# ifndef PNGCAPI ++# define PNGCAPI _cdecl ++# endif ++# if PNG_API_RULE == 1 && !defined(PNGAPI) ++# define PNGAPI _stdcall ++# endif ++# endif /* compiler/api */ ++ ++ /* NOTE: PNGCBAPI always defaults to PNGCAPI. */ ++ ++# if defined(PNGAPI) && !defined(PNG_USER_PRIVATEBUILD) ++# error "PNG_USER_PRIVATEBUILD must be defined if PNGAPI is changed" ++# endif ++ ++# if (defined(_MSC_VER) && _MSC_VER < 800) ||\ ++ (defined(__BORLANDC__) && __BORLANDC__ < 0x500) ++ /* older Borland and MSC ++ * compilers used '__export' and required this to be after ++ * the type. ++ */ ++# ifndef PNG_EXPORT_TYPE ++# define PNG_EXPORT_TYPE(type) type PNG_IMPEXP ++# endif ++# define PNG_DLL_EXPORT __export ++# else /* newer compiler */ ++# define PNG_DLL_EXPORT __declspec(dllexport) ++# ifndef PNG_DLL_IMPORT ++# define PNG_DLL_IMPORT __declspec(dllimport) ++# endif ++# endif /* compiler */ ++ ++#else /* !Windows */ ++# if (defined(__IBMC__) || defined(__IBMCPP__)) && defined(__OS2__) ++# define PNGAPI _System ++# else /* !Windows/x86 && !OS/2 */ ++ /* Use the defaults, or define PNG*API on the command line (but ++ * this will have to be done for every compile!) ++ */ ++# endif /* other system, !OS/2 */ ++#endif /* !Windows/x86 */ ++ ++/* Now do all the defaulting . */ ++#ifndef PNGCAPI ++# define PNGCAPI ++#endif ++#ifndef PNGCBAPI ++# define PNGCBAPI PNGCAPI ++#endif ++#ifndef PNGAPI ++# define PNGAPI PNGCAPI ++#endif ++ ++/* PNG_IMPEXP may be set on the compilation system command line or (if not set) ++ * then in an internal header file when building the library, otherwise (when ++ * using the library) it is set here. ++ */ ++#ifndef PNG_IMPEXP ++# if defined(PNG_USE_DLL) && defined(PNG_DLL_IMPORT) ++ /* This forces use of a DLL, disallowing static linking */ ++# define PNG_IMPEXP PNG_DLL_IMPORT ++# endif ++ ++# ifndef PNG_IMPEXP ++# define PNG_IMPEXP ++# endif ++#endif ++ ++/* In 1.5.2 the definition of PNG_FUNCTION has been changed to always treat ++ * 'attributes' as a storage class - the attributes go at the start of the ++ * function definition, and attributes are always appended regardless of the ++ * compiler. This considerably simplifies these macros but may cause problems ++ * if any compilers both need function attributes and fail to handle them as ++ * a storage class (this is unlikely.) ++ */ ++#ifndef PNG_FUNCTION ++# define PNG_FUNCTION(type, name, args, attributes) attributes type name args ++#endif ++ ++#ifndef PNG_EXPORT_TYPE ++# define PNG_EXPORT_TYPE(type) PNG_IMPEXP type ++#endif ++ ++ /* The ordinal value is only relevant when preprocessing png.h for symbol ++ * table entries, so we discard it here. See the .dfn files in the ++ * scripts directory. ++ */ ++ ++#ifndef PNG_EXPORTA ++# define PNG_EXPORTA(ordinal, type, name, args, attributes) \ ++ PNG_FUNCTION(PNG_EXPORT_TYPE(type), (PNGAPI name), PNGARG(args), \ ++ PNG_LINKAGE_API attributes) ++#endif ++ ++/* ANSI-C (C90) does not permit a macro to be invoked with an empty argument, ++ * so make something non-empty to satisfy the requirement: ++ */ ++#define PNG_EMPTY /*empty list*/ ++ ++#define PNG_EXPORT(ordinal, type, name, args) \ ++ PNG_EXPORTA(ordinal, type, name, args, PNG_EMPTY) ++ ++/* Use PNG_REMOVED to comment out a removed interface. */ ++#ifndef PNG_REMOVED ++# define PNG_REMOVED(ordinal, type, name, args, attributes) ++#endif ++ ++#ifndef PNG_CALLBACK ++# define PNG_CALLBACK(type, name, args) type (PNGCBAPI name) PNGARG(args) ++#endif ++ ++/* Support for compiler specific function attributes. These are used ++ * so that where compiler support is available incorrect use of API ++ * functions in png.h will generate compiler warnings. ++ * ++ * Added at libpng-1.2.41. ++ */ ++ ++#ifndef PNG_NO_PEDANTIC_WARNINGS ++# ifndef PNG_PEDANTIC_WARNINGS_SUPPORTED ++# define PNG_PEDANTIC_WARNINGS_SUPPORTED ++# endif ++#endif ++ ++#ifdef PNG_PEDANTIC_WARNINGS_SUPPORTED ++ /* Support for compiler specific function attributes. These are used ++ * so that where compiler support is available, incorrect use of API ++ * functions in png.h will generate compiler warnings. Added at libpng ++ * version 1.2.41. Disabling these removes the warnings but may also produce ++ * less efficient code. ++ */ ++# if defined(__clang__) && defined(__has_attribute) ++ /* Clang defines both __clang__ and __GNUC__. Check __clang__ first. */ ++# if !defined(PNG_USE_RESULT) && __has_attribute(__warn_unused_result__) ++# define PNG_USE_RESULT __attribute__((__warn_unused_result__)) ++# endif ++# if !defined(PNG_NORETURN) && __has_attribute(__noreturn__) ++# define PNG_NORETURN __attribute__((__noreturn__)) ++# endif ++# if !defined(PNG_ALLOCATED) && __has_attribute(__malloc__) ++# define PNG_ALLOCATED __attribute__((__malloc__)) ++# endif ++# if !defined(PNG_DEPRECATED) && __has_attribute(__deprecated__) ++# define PNG_DEPRECATED __attribute__((__deprecated__)) ++# endif ++# if !defined(PNG_PRIVATE) ++# ifdef __has_extension ++# if __has_extension(attribute_unavailable_with_message) ++# define PNG_PRIVATE __attribute__((__unavailable__(\ ++ "This function is not exported by libpng."))) ++# endif ++# endif ++# endif ++# ifndef PNG_RESTRICT ++# define PNG_RESTRICT __restrict ++# endif ++ ++# elif defined(__GNUC__) ++# ifndef PNG_USE_RESULT ++# define PNG_USE_RESULT __attribute__((__warn_unused_result__)) ++# endif ++# ifndef PNG_NORETURN ++# define PNG_NORETURN __attribute__((__noreturn__)) ++# endif ++# if __GNUC__ >= 3 ++# ifndef PNG_ALLOCATED ++# define PNG_ALLOCATED __attribute__((__malloc__)) ++# endif ++# ifndef PNG_DEPRECATED ++# define PNG_DEPRECATED __attribute__((__deprecated__)) ++# endif ++# ifndef PNG_PRIVATE ++# if 0 /* Doesn't work so we use deprecated instead*/ ++# define PNG_PRIVATE \ ++ __attribute__((warning("This function is not exported by libpng."))) ++# else ++# define PNG_PRIVATE \ ++ __attribute__((__deprecated__)) ++# endif ++# endif ++# if ((__GNUC__ > 3) || !defined(__GNUC_MINOR__) || (__GNUC_MINOR__ >= 1)) ++# ifndef PNG_RESTRICT ++# define PNG_RESTRICT __restrict ++# endif ++# endif /* __GNUC__.__GNUC_MINOR__ > 3.0 */ ++# endif /* __GNUC__ >= 3 */ ++ ++# elif defined(_MSC_VER) && (_MSC_VER >= 1300) ++# ifndef PNG_USE_RESULT ++# define PNG_USE_RESULT /* not supported */ ++# endif ++# ifndef PNG_NORETURN ++# define PNG_NORETURN __declspec(noreturn) ++# endif ++# ifndef PNG_ALLOCATED ++# if (_MSC_VER >= 1400) ++# define PNG_ALLOCATED __declspec(restrict) ++# endif ++# endif ++# ifndef PNG_DEPRECATED ++# define PNG_DEPRECATED __declspec(deprecated) ++# endif ++# ifndef PNG_PRIVATE ++# define PNG_PRIVATE __declspec(deprecated) ++# endif ++# ifndef PNG_RESTRICT ++# if (_MSC_VER >= 1400) ++# define PNG_RESTRICT __restrict ++# endif ++# endif ++ ++# elif defined(__WATCOMC__) ++# ifndef PNG_RESTRICT ++# define PNG_RESTRICT __restrict ++# endif ++# endif ++#endif /* PNG_PEDANTIC_WARNINGS */ ++ ++#ifndef PNG_DEPRECATED ++# define PNG_DEPRECATED /* Use of this function is deprecated */ ++#endif ++#ifndef PNG_USE_RESULT ++# define PNG_USE_RESULT /* The result of this function must be checked */ ++#endif ++#ifndef PNG_NORETURN ++# define PNG_NORETURN /* This function does not return */ ++#endif ++#ifndef PNG_ALLOCATED ++# define PNG_ALLOCATED /* The result of the function is new memory */ ++#endif ++#ifndef PNG_PRIVATE ++# define PNG_PRIVATE /* This is a private libpng function */ ++#endif ++#ifndef PNG_RESTRICT ++# define PNG_RESTRICT /* The C99 "restrict" feature */ ++#endif ++ ++#ifndef PNG_FP_EXPORT /* A floating point API. */ ++# ifdef PNG_FLOATING_POINT_SUPPORTED ++# define PNG_FP_EXPORT(ordinal, type, name, args)\ ++ PNG_EXPORT(ordinal, type, name, args); ++# else /* No floating point APIs */ ++# define PNG_FP_EXPORT(ordinal, type, name, args) ++# endif ++#endif ++#ifndef PNG_FIXED_EXPORT /* A fixed point API. */ ++# ifdef PNG_FIXED_POINT_SUPPORTED ++# define PNG_FIXED_EXPORT(ordinal, type, name, args)\ ++ PNG_EXPORT(ordinal, type, name, args); ++# else /* No fixed point APIs */ ++# define PNG_FIXED_EXPORT(ordinal, type, name, args) ++# endif ++#endif ++ ++#ifndef PNG_BUILDING_SYMBOL_TABLE ++/* Some typedefs to get us started. These should be safe on most of the common ++ * platforms. ++ * ++ * png_uint_32 and png_int_32 may, currently, be larger than required to hold a ++ * 32-bit value however this is not normally advisable. ++ * ++ * png_uint_16 and png_int_16 should always be two bytes in size - this is ++ * verified at library build time. ++ * ++ * png_byte must always be one byte in size. ++ * ++ * The checks below use constants from limits.h, as defined by the ISOC90 ++ * standard. ++ */ ++#if CHAR_BIT == 8 && UCHAR_MAX == 255 ++ typedef unsigned char png_byte; ++#else ++# error "libpng requires 8-bit bytes" ++#endif ++ ++#if INT_MIN == -32768 && INT_MAX == 32767 ++ typedef int png_int_16; ++#elif SHRT_MIN == -32768 && SHRT_MAX == 32767 ++ typedef short png_int_16; ++#else ++# error "libpng requires a signed 16-bit type" ++#endif ++ ++#if UINT_MAX == 65535 ++ typedef unsigned int png_uint_16; ++#elif USHRT_MAX == 65535 ++ typedef unsigned short png_uint_16; ++#else ++# error "libpng requires an unsigned 16-bit type" ++#endif ++ ++#if INT_MIN < -2147483646 && INT_MAX > 2147483646 ++ typedef int png_int_32; ++#elif LONG_MIN < -2147483646 && LONG_MAX > 2147483646 ++ typedef long int png_int_32; ++#else ++# error "libpng requires a signed 32-bit (or more) type" ++#endif ++ ++#if UINT_MAX > 4294967294U ++ typedef unsigned int png_uint_32; ++#elif ULONG_MAX > 4294967294U ++ typedef unsigned long int png_uint_32; ++#else ++# error "libpng requires an unsigned 32-bit (or more) type" ++#endif ++ ++/* Prior to 1.6.0, it was possible to disable the use of size_t and ptrdiff_t. ++ * From 1.6.0 onwards, an ISO C90 compiler, as well as a standard-compliant ++ * behavior of sizeof and ptrdiff_t are required. ++ * The legacy typedefs are provided here for backwards compatibility. ++ */ ++typedef size_t png_size_t; ++typedef ptrdiff_t png_ptrdiff_t; ++ ++/* libpng needs to know the maximum value of 'size_t' and this controls the ++ * definition of png_alloc_size_t, below. This maximum value of size_t limits ++ * but does not control the maximum allocations the library makes - there is ++ * direct application control of this through png_set_user_limits(). ++ */ ++#ifndef PNG_SMALL_SIZE_T ++ /* Compiler specific tests for systems where size_t is known to be less than ++ * 32 bits (some of these systems may no longer work because of the lack of ++ * 'far' support; see above.) ++ */ ++# if (defined(__TURBOC__) && !defined(__FLAT__)) ||\ ++ (defined(_MSC_VER) && defined(MAXSEG_64K)) ++# define PNG_SMALL_SIZE_T ++# endif ++#endif ++ ++/* png_alloc_size_t is guaranteed to be no smaller than size_t, and no smaller ++ * than png_uint_32. Casts from size_t or png_uint_32 to png_alloc_size_t are ++ * not necessary; in fact, it is recommended not to use them at all, so that ++ * the compiler can complain when something turns out to be problematic. ++ * ++ * Casts in the other direction (from png_alloc_size_t to size_t or ++ * png_uint_32) should be explicitly applied; however, we do not expect to ++ * encounter practical situations that require such conversions. ++ * ++ * PNG_SMALL_SIZE_T must be defined if the maximum value of size_t is less than ++ * 4294967295 - i.e. less than the maximum value of png_uint_32. ++ */ ++#ifdef PNG_SMALL_SIZE_T ++ typedef png_uint_32 png_alloc_size_t; ++#else ++ typedef size_t png_alloc_size_t; ++#endif ++ ++/* Prior to 1.6.0 libpng offered limited support for Microsoft C compiler ++ * implementations of Intel CPU specific support of user-mode segmented address ++ * spaces, where 16-bit pointers address more than 65536 bytes of memory using ++ * separate 'segment' registers. The implementation requires two different ++ * types of pointer (only one of which includes the segment value.) ++ * ++ * If required this support is available in version 1.2 of libpng and may be ++ * available in versions through 1.5, although the correctness of the code has ++ * not been verified recently. ++ */ ++ ++/* Typedef for floating-point numbers that are converted to fixed-point with a ++ * multiple of 100,000, e.g., gamma ++ */ ++typedef png_int_32 png_fixed_point; ++ ++/* Add typedefs for pointers */ ++typedef void * png_voidp; ++typedef const void * png_const_voidp; ++typedef png_byte * png_bytep; ++typedef const png_byte * png_const_bytep; ++typedef png_uint_32 * png_uint_32p; ++typedef const png_uint_32 * png_const_uint_32p; ++typedef png_int_32 * png_int_32p; ++typedef const png_int_32 * png_const_int_32p; ++typedef png_uint_16 * png_uint_16p; ++typedef const png_uint_16 * png_const_uint_16p; ++typedef png_int_16 * png_int_16p; ++typedef const png_int_16 * png_const_int_16p; ++typedef char * png_charp; ++typedef const char * png_const_charp; ++typedef png_fixed_point * png_fixed_point_p; ++typedef const png_fixed_point * png_const_fixed_point_p; ++typedef size_t * png_size_tp; ++typedef const size_t * png_const_size_tp; ++ ++#ifdef PNG_STDIO_SUPPORTED ++typedef FILE * png_FILE_p; ++#endif ++ ++#ifdef PNG_FLOATING_POINT_SUPPORTED ++typedef double * png_doublep; ++typedef const double * png_const_doublep; ++#endif ++ ++/* Pointers to pointers; i.e. arrays */ ++typedef png_byte * * png_bytepp; ++typedef png_uint_32 * * png_uint_32pp; ++typedef png_int_32 * * png_int_32pp; ++typedef png_uint_16 * * png_uint_16pp; ++typedef png_int_16 * * png_int_16pp; ++typedef const char * * png_const_charpp; ++typedef char * * png_charpp; ++typedef png_fixed_point * * png_fixed_point_pp; ++#ifdef PNG_FLOATING_POINT_SUPPORTED ++typedef double * * png_doublepp; ++#endif ++ ++/* Pointers to pointers to pointers; i.e., pointer to array */ ++typedef char * * * png_charppp; ++ ++#endif /* PNG_BUILDING_SYMBOL_TABLE */ ++ ++#endif /* PNGCONF_H */ +diff --git a/lib/libpng/include/pnglibconf.h b/lib/libpng/include/pnglibconf.h +new file mode 100644 +index 000000000..8875c6766 +--- /dev/null ++++ b/lib/libpng/include/pnglibconf.h +@@ -0,0 +1,218 @@ ++/* pnglibconf.h - library build configuration */ ++ ++/* libpng version 1.6.37 */ ++ ++/* Copyright (c) 2018-2019 Cosmin Truta */ ++/* Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson */ ++ ++/* This code is released under the libpng license. */ ++/* For conditions of distribution and use, see the disclaimer */ ++/* and license in png.h */ ++ ++/* pnglibconf.h */ ++/* Machine generated file: DO NOT EDIT */ ++/* Derived from: scripts/pnglibconf.dfa */ ++#ifndef PNGLCONF_H ++#define PNGLCONF_H ++/* options */ ++#define PNG_16BIT_SUPPORTED ++#define PNG_ALIGNED_MEMORY_SUPPORTED ++/*#undef PNG_ARM_NEON_API_SUPPORTED*/ ++/*#undef PNG_ARM_NEON_CHECK_SUPPORTED*/ ++#define PNG_BENIGN_ERRORS_SUPPORTED ++#define PNG_BENIGN_READ_ERRORS_SUPPORTED ++/*#undef PNG_BENIGN_WRITE_ERRORS_SUPPORTED*/ ++#define PNG_BUILD_GRAYSCALE_PALETTE_SUPPORTED ++#define PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED ++#define PNG_COLORSPACE_SUPPORTED ++/*#undef PNG_CONSOLE_IO_SUPPORTED */ ++/*#undef PNG_CONVERT_tIME_SUPPORTED */ ++#define PNG_EASY_ACCESS_SUPPORTED ++/*#undef PNG_ERROR_NUMBERS_SUPPORTED*/ ++#define PNG_ERROR_TEXT_SUPPORTED ++#define PNG_FIXED_POINT_SUPPORTED ++/*#define PNG_FLOATING_ARITHMETIC_SUPPORTED*/ ++/*#define PNG_FLOATING_POINT_SUPPORTED*/ ++#define PNG_FORMAT_AFIRST_SUPPORTED ++#define PNG_FORMAT_BGR_SUPPORTED ++#define PNG_GAMMA_SUPPORTED ++#define PNG_GET_PALETTE_MAX_SUPPORTED ++#define PNG_HANDLE_AS_UNKNOWN_SUPPORTED ++#define PNG_INCH_CONVERSIONS_SUPPORTED ++#define PNG_INFO_IMAGE_SUPPORTED ++#define PNG_IO_STATE_SUPPORTED ++#define PNG_MNG_FEATURES_SUPPORTED ++#define PNG_POINTER_INDEXING_SUPPORTED ++/*#undef PNG_POWERPC_VSX_API_SUPPORTED*/ ++/*#undef PNG_POWERPC_VSX_CHECK_SUPPORTED*/ ++#define PNG_PROGRESSIVE_READ_SUPPORTED ++#define PNG_READ_16BIT_SUPPORTED ++#define PNG_READ_ALPHA_MODE_SUPPORTED ++#define PNG_READ_ANCILLARY_CHUNKS_SUPPORTED ++#define PNG_READ_BACKGROUND_SUPPORTED ++#define PNG_READ_BGR_SUPPORTED ++#define PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED ++#define PNG_READ_COMPOSITE_NODIV_SUPPORTED ++#define PNG_READ_COMPRESSED_TEXT_SUPPORTED ++#define PNG_READ_EXPAND_16_SUPPORTED ++#define PNG_READ_EXPAND_SUPPORTED ++#define PNG_READ_FILLER_SUPPORTED ++#define PNG_READ_GAMMA_SUPPORTED ++#define PNG_READ_GET_PALETTE_MAX_SUPPORTED ++#define PNG_READ_GRAY_TO_RGB_SUPPORTED ++#define PNG_READ_INTERLACING_SUPPORTED ++#define PNG_READ_INT_FUNCTIONS_SUPPORTED ++#define PNG_READ_INVERT_ALPHA_SUPPORTED ++#define PNG_READ_INVERT_SUPPORTED ++#define PNG_READ_OPT_PLTE_SUPPORTED ++#define PNG_READ_PACKSWAP_SUPPORTED ++#define PNG_READ_PACK_SUPPORTED ++#define PNG_READ_QUANTIZE_SUPPORTED ++#define PNG_READ_RGB_TO_GRAY_SUPPORTED ++#define PNG_READ_SCALE_16_TO_8_SUPPORTED ++#define PNG_READ_SHIFT_SUPPORTED ++#define PNG_READ_STRIP_16_TO_8_SUPPORTED ++#define PNG_READ_STRIP_ALPHA_SUPPORTED ++#define PNG_READ_SUPPORTED ++#define PNG_READ_SWAP_ALPHA_SUPPORTED ++#define PNG_READ_SWAP_SUPPORTED ++#define PNG_READ_TEXT_SUPPORTED ++#define PNG_READ_TRANSFORMS_SUPPORTED ++#define PNG_READ_UNKNOWN_CHUNKS_SUPPORTED ++#define PNG_READ_USER_CHUNKS_SUPPORTED ++#define PNG_READ_USER_TRANSFORM_SUPPORTED ++#define PNG_READ_bKGD_SUPPORTED ++#define PNG_READ_cHRM_SUPPORTED ++#define PNG_READ_eXIf_SUPPORTED ++#define PNG_READ_gAMA_SUPPORTED ++#define PNG_READ_hIST_SUPPORTED ++#define PNG_READ_iCCP_SUPPORTED ++#define PNG_READ_iTXt_SUPPORTED ++#define PNG_READ_oFFs_SUPPORTED ++#define PNG_READ_pCAL_SUPPORTED ++#define PNG_READ_pHYs_SUPPORTED ++#define PNG_READ_sBIT_SUPPORTED ++#define PNG_READ_sCAL_SUPPORTED ++#define PNG_READ_sPLT_SUPPORTED ++#define PNG_READ_sRGB_SUPPORTED ++#define PNG_READ_tEXt_SUPPORTED ++#define PNG_READ_tIME_SUPPORTED ++#define PNG_READ_tRNS_SUPPORTED ++#define PNG_READ_zTXt_SUPPORTED ++#define PNG_SAVE_INT_32_SUPPORTED ++#define PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED ++#define PNG_SEQUENTIAL_READ_SUPPORTED ++#define PNG_SETJMP_SUPPORTED ++#define PNG_SET_OPTION_SUPPORTED ++#define PNG_SET_UNKNOWN_CHUNKS_SUPPORTED ++#define PNG_SET_USER_LIMITS_SUPPORTED ++#define PNG_SIMPLIFIED_READ_AFIRST_SUPPORTED ++#define PNG_SIMPLIFIED_READ_BGR_SUPPORTED ++#define PNG_SIMPLIFIED_READ_SUPPORTED ++#define PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED ++#define PNG_SIMPLIFIED_WRITE_BGR_SUPPORTED ++#define PNG_SIMPLIFIED_WRITE_SUPPORTED ++/*#define PNG_STDIO_SUPPORTED*/ ++#define PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED ++#define PNG_TEXT_SUPPORTED ++#define PNG_TIME_RFC1123_SUPPORTED ++#define PNG_UNKNOWN_CHUNKS_SUPPORTED ++#define PNG_USER_CHUNKS_SUPPORTED ++#define PNG_USER_LIMITS_SUPPORTED ++#define PNG_USER_MEM_SUPPORTED ++#define PNG_USER_TRANSFORM_INFO_SUPPORTED ++#define PNG_USER_TRANSFORM_PTR_SUPPORTED ++#define PNG_WARNINGS_SUPPORTED ++#define PNG_WRITE_16BIT_SUPPORTED ++#define PNG_WRITE_ANCILLARY_CHUNKS_SUPPORTED ++#define PNG_WRITE_BGR_SUPPORTED ++#define PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED ++#define PNG_WRITE_COMPRESSED_TEXT_SUPPORTED ++#define PNG_WRITE_CUSTOMIZE_COMPRESSION_SUPPORTED ++#define PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED ++#define PNG_WRITE_FILLER_SUPPORTED ++#define PNG_WRITE_FILTER_SUPPORTED ++#define PNG_WRITE_FLUSH_SUPPORTED ++#define PNG_WRITE_GET_PALETTE_MAX_SUPPORTED ++#define PNG_WRITE_INTERLACING_SUPPORTED ++#define PNG_WRITE_INT_FUNCTIONS_SUPPORTED ++#define PNG_WRITE_INVERT_ALPHA_SUPPORTED ++#define PNG_WRITE_INVERT_SUPPORTED ++#define PNG_WRITE_OPTIMIZE_CMF_SUPPORTED ++#define PNG_WRITE_PACKSWAP_SUPPORTED ++#define PNG_WRITE_PACK_SUPPORTED ++#define PNG_WRITE_SHIFT_SUPPORTED ++#define PNG_WRITE_SUPPORTED ++#define PNG_WRITE_SWAP_ALPHA_SUPPORTED ++#define PNG_WRITE_SWAP_SUPPORTED ++#define PNG_WRITE_TEXT_SUPPORTED ++#define PNG_WRITE_TRANSFORMS_SUPPORTED ++#define PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED ++#define PNG_WRITE_USER_TRANSFORM_SUPPORTED ++#define PNG_WRITE_WEIGHTED_FILTER_SUPPORTED ++#define PNG_WRITE_bKGD_SUPPORTED ++#define PNG_WRITE_cHRM_SUPPORTED ++#define PNG_WRITE_eXIf_SUPPORTED ++#define PNG_WRITE_gAMA_SUPPORTED ++#define PNG_WRITE_hIST_SUPPORTED ++#define PNG_WRITE_iCCP_SUPPORTED ++#define PNG_WRITE_iTXt_SUPPORTED ++#define PNG_WRITE_oFFs_SUPPORTED ++#define PNG_WRITE_pCAL_SUPPORTED ++#define PNG_WRITE_pHYs_SUPPORTED ++#define PNG_WRITE_sBIT_SUPPORTED ++#define PNG_WRITE_sCAL_SUPPORTED ++#define PNG_WRITE_sPLT_SUPPORTED ++#define PNG_WRITE_sRGB_SUPPORTED ++#define PNG_WRITE_tEXt_SUPPORTED ++#define PNG_WRITE_tIME_SUPPORTED ++#define PNG_WRITE_tRNS_SUPPORTED ++#define PNG_WRITE_zTXt_SUPPORTED ++#define PNG_bKGD_SUPPORTED ++#define PNG_cHRM_SUPPORTED ++#define PNG_eXIf_SUPPORTED ++#define PNG_gAMA_SUPPORTED ++#define PNG_hIST_SUPPORTED ++#define PNG_iCCP_SUPPORTED ++#define PNG_iTXt_SUPPORTED ++#define PNG_oFFs_SUPPORTED ++#define PNG_pCAL_SUPPORTED ++#define PNG_pHYs_SUPPORTED ++#define PNG_sBIT_SUPPORTED ++#define PNG_sCAL_SUPPORTED ++#define PNG_sPLT_SUPPORTED ++#define PNG_sRGB_SUPPORTED ++#define PNG_tEXt_SUPPORTED ++#define PNG_tIME_SUPPORTED ++#define PNG_tRNS_SUPPORTED ++#define PNG_zTXt_SUPPORTED ++/* end of options */ ++/* settings */ ++#define PNG_API_RULE 0 ++#define PNG_DEFAULT_READ_MACROS 1 ++#define PNG_GAMMA_THRESHOLD_FIXED 5000 ++#define PNG_IDAT_READ_SIZE PNG_ZBUF_SIZE ++#define PNG_INFLATE_BUF_SIZE 1024 ++#define PNG_LINKAGE_API extern ++#define PNG_LINKAGE_CALLBACK extern ++#define PNG_LINKAGE_DATA extern ++#define PNG_LINKAGE_FUNCTION extern ++#define PNG_MAX_GAMMA_8 11 ++#define PNG_QUANTIZE_BLUE_BITS 5 ++#define PNG_QUANTIZE_GREEN_BITS 5 ++#define PNG_QUANTIZE_RED_BITS 5 ++#define PNG_TEXT_Z_DEFAULT_COMPRESSION (-1) ++#define PNG_TEXT_Z_DEFAULT_STRATEGY 0 ++#define PNG_USER_CHUNK_CACHE_MAX 1000 ++#define PNG_USER_CHUNK_MALLOC_MAX 8000000 ++#define PNG_USER_HEIGHT_MAX 1000000 ++#define PNG_USER_WIDTH_MAX 1000000 ++#define PNG_ZBUF_SIZE 8192 ++#define PNG_ZLIB_VERNUM 0 /* unknown */ ++#define PNG_Z_DEFAULT_COMPRESSION (-1) ++#define PNG_Z_DEFAULT_NOFILTER_STRATEGY 0 ++#define PNG_Z_DEFAULT_STRATEGY 1 ++#define PNG_sCAL_PRECISION 5 ++#define PNG_sRGB_PROFILE_CHECKS 2 ++/* end of settings */ ++#endif /* PNGLCONF_H */ +diff --git a/lib/libpng/libpng-manual.txt b/lib/libpng/libpng-manual.txt +new file mode 100644 +index 000000000..5dad92fbf +--- /dev/null ++++ b/lib/libpng/libpng-manual.txt +@@ -0,0 +1,5409 @@ ++libpng-manual.txt - A description on how to use and modify libpng ++ ++ Copyright (c) 2018-2019 Cosmin Truta ++ Copyright (c) 1998-2018 Glenn Randers-Pehrson ++ ++ This document is released under the libpng license. ++ For conditions of distribution and use, see the disclaimer ++ and license in png.h ++ ++ Based on: ++ ++ libpng version 1.6.36, December 2018, through 1.6.37 - April 2019 ++ Updated and distributed by Cosmin Truta ++ Copyright (c) 2018-2019 Cosmin Truta ++ ++ libpng versions 0.97, January 1998, through 1.6.35 - July 2018 ++ Updated and distributed by Glenn Randers-Pehrson ++ Copyright (c) 1998-2018 Glenn Randers-Pehrson ++ ++ libpng 1.0 beta 6 - version 0.96 - May 28, 1997 ++ Updated and distributed by Andreas Dilger ++ Copyright (c) 1996, 1997 Andreas Dilger ++ ++ libpng 1.0 beta 2 - version 0.88 - January 26, 1996 ++ For conditions of distribution and use, see copyright ++ notice in png.h. Copyright (c) 1995, 1996 Guy Eric ++ Schalnat, Group 42, Inc. ++ ++ Updated/rewritten per request in the libpng FAQ ++ Copyright (c) 1995, 1996 Frank J. T. Wojcik ++ December 18, 1995 & January 20, 1996 ++ ++ TABLE OF CONTENTS ++ ++ I. Introduction ++ II. Structures ++ III. Reading ++ IV. Writing ++ V. Simplified API ++ VI. Modifying/Customizing libpng ++ VII. MNG support ++ VIII. Changes to Libpng from version 0.88 ++ IX. Changes to Libpng from version 1.0.x to 1.2.x ++ X. Changes to Libpng from version 1.0.x/1.2.x to 1.4.x ++ XI. Changes to Libpng from version 1.4.x to 1.5.x ++ XII. Changes to Libpng from version 1.5.x to 1.6.x ++ XIII. Detecting libpng ++ XIV. Source code repository ++ XV. Coding style ++ ++I. Introduction ++ ++This file describes how to use and modify the PNG reference library ++(known as libpng) for your own use. In addition to this ++file, example.c is a good starting point for using the library, as ++it is heavily commented and should include everything most people ++will need. We assume that libpng is already installed; see the ++INSTALL file for instructions on how to configure and install libpng. ++ ++For examples of libpng usage, see the files "example.c", "pngtest.c", ++and the files in the "contrib" directory, all of which are included in ++the libpng distribution. ++ ++Libpng was written as a companion to the PNG specification, as a way ++of reducing the amount of time and effort it takes to support the PNG ++file format in application programs. ++ ++The PNG specification (second edition), November 2003, is available as ++a W3C Recommendation and as an ISO Standard (ISO/IEC 15948:2004 (E)) at ++. ++The W3C and ISO documents have identical technical content. ++ ++The PNG-1.2 specification is available at ++. ++It is technically equivalent ++to the PNG specification (second edition) but has some additional material. ++ ++The PNG-1.0 specification is available as RFC 2083 at ++ and as a ++W3C Recommendation at . ++ ++Some additional chunks are described in the special-purpose public chunks ++documents at ++ ++Other information ++about PNG, and the latest version of libpng, can be found at the PNG home ++page, . ++ ++Most users will not have to modify the library significantly; advanced ++users may want to modify it more. All attempts were made to make it as ++complete as possible, while keeping the code easy to understand. ++Currently, this library only supports C. Support for other languages ++is being considered. ++ ++Libpng has been designed to handle multiple sessions at one time, ++to be easily modifiable, to be portable to the vast majority of ++machines (ANSI, K&R, 16-, 32-, and 64-bit) available, and to be easy ++to use. The ultimate goal of libpng is to promote the acceptance of ++the PNG file format in whatever way possible. While there is still ++work to be done (see the TODO file), libpng should cover the ++majority of the needs of its users. ++ ++Libpng uses zlib for its compression and decompression of PNG files. ++Further information about zlib, and the latest version of zlib, can ++be found at the zlib home page, . ++The zlib compression utility is a general purpose utility that is ++useful for more than PNG files, and can be used without libpng. ++See the documentation delivered with zlib for more details. ++You can usually find the source files for the zlib utility wherever you ++find the libpng source files. ++ ++Libpng is thread safe, provided the threads are using different ++instances of the structures. Each thread should have its own ++png_struct and png_info instances, and thus its own image. ++Libpng does not protect itself against two threads using the ++same instance of a structure. ++ ++II. Structures ++ ++There are two main structures that are important to libpng, png_struct ++and png_info. Both are internal structures that are no longer exposed ++in the libpng interface (as of libpng 1.5.0). ++ ++The png_info structure is designed to provide information about the ++PNG file. At one time, the fields of png_info were intended to be ++directly accessible to the user. However, this tended to cause problems ++with applications using dynamically loaded libraries, and as a result ++a set of interface functions for png_info (the png_get_*() and png_set_*() ++functions) was developed, and direct access to the png_info fields was ++deprecated.. ++ ++The png_struct structure is the object used by the library to decode a ++single image. As of 1.5.0 this structure is also not exposed. ++ ++Almost all libpng APIs require a pointer to a png_struct as the first argument. ++Many (in particular the png_set and png_get APIs) also require a pointer ++to png_info as the second argument. Some application visible macros ++defined in png.h designed for basic data access (reading and writing ++integers in the PNG format) don't take a png_info pointer, but it's almost ++always safe to assume that a (png_struct*) has to be passed to call an API ++function. ++ ++You can have more than one png_info structure associated with an image, ++as illustrated in pngtest.c, one for information valid prior to the ++IDAT chunks and another (called "end_info" below) for things after them. ++ ++The png.h header file is an invaluable reference for programming with libpng. ++And while I'm on the topic, make sure you include the libpng header file: ++ ++#include ++ ++and also (as of libpng-1.5.0) the zlib header file, if you need it: ++ ++#include ++ ++Types ++ ++The png.h header file defines a number of integral types used by the ++APIs. Most of these are fairly obvious; for example types corresponding ++to integers of particular sizes and types for passing color values. ++ ++One exception is how non-integral numbers are handled. For application ++convenience most APIs that take such numbers have C (double) arguments; ++however, internally PNG, and libpng, use 32 bit signed integers and encode ++the value by multiplying by 100,000. As of libpng 1.5.0 a convenience ++macro PNG_FP_1 is defined in png.h along with a type (png_fixed_point) ++which is simply (png_int_32). ++ ++All APIs that take (double) arguments also have a matching API that ++takes the corresponding fixed point integer arguments. The fixed point ++API has the same name as the floating point one with "_fixed" appended. ++The actual range of values permitted in the APIs is frequently less than ++the full range of (png_fixed_point) (-21474 to +21474). When APIs require ++a non-negative argument the type is recorded as png_uint_32 above. Consult ++the header file and the text below for more information. ++ ++Special care must be take with sCAL chunk handling because the chunk itself ++uses non-integral values encoded as strings containing decimal floating point ++numbers. See the comments in the header file. ++ ++Configuration ++ ++The main header file function declarations are frequently protected by C ++preprocessing directives of the form: ++ ++ #ifdef PNG_feature_SUPPORTED ++ declare-function ++ #endif ++ ... ++ #ifdef PNG_feature_SUPPORTED ++ use-function ++ #endif ++ ++The library can be built without support for these APIs, although a ++standard build will have all implemented APIs. Application programs ++should check the feature macros before using an API for maximum ++portability. From libpng 1.5.0 the feature macros set during the build ++of libpng are recorded in the header file "pnglibconf.h" and this file ++is always included by png.h. ++ ++If you don't need to change the library configuration from the default, skip to ++the next section ("Reading"). ++ ++Notice that some of the makefiles in the 'scripts' directory and (in 1.5.0) all ++of the build project files in the 'projects' directory simply copy ++scripts/pnglibconf.h.prebuilt to pnglibconf.h. This means that these build ++systems do not permit easy auto-configuration of the library - they only ++support the default configuration. ++ ++The easiest way to make minor changes to the libpng configuration when ++auto-configuration is supported is to add definitions to the command line ++using (typically) CPPFLAGS. For example: ++ ++CPPFLAGS=-DPNG_NO_FLOATING_ARITHMETIC ++ ++will change the internal libpng math implementation for gamma correction and ++other arithmetic calculations to fixed point, avoiding the need for fast ++floating point support. The result can be seen in the generated pnglibconf.h - ++make sure it contains the changed feature macro setting. ++ ++If you need to make more extensive configuration changes - more than one or two ++feature macro settings - you can either add -DPNG_USER_CONFIG to the build ++command line and put a list of feature macro settings in pngusr.h or you can set ++DFA_XTRA (a makefile variable) to a file containing the same information in the ++form of 'option' settings. ++ ++A. Changing pnglibconf.h ++ ++A variety of methods exist to build libpng. Not all of these support ++reconfiguration of pnglibconf.h. To reconfigure pnglibconf.h it must either be ++rebuilt from scripts/pnglibconf.dfa using awk or it must be edited by hand. ++ ++Hand editing is achieved by copying scripts/pnglibconf.h.prebuilt to ++pnglibconf.h and changing the lines defining the supported features, paying ++very close attention to the 'option' information in scripts/pnglibconf.dfa ++that describes those features and their requirements. This is easy to get ++wrong. ++ ++B. Configuration using DFA_XTRA ++ ++Rebuilding from pnglibconf.dfa is easy if a functioning 'awk', or a later ++variant such as 'nawk' or 'gawk', is available. The configure build will ++automatically find an appropriate awk and build pnglibconf.h. ++The scripts/pnglibconf.mak file contains a set of make rules for doing the ++same thing if configure is not used, and many of the makefiles in the scripts ++directory use this approach. ++ ++When rebuilding simply write a new file containing changed options and set ++DFA_XTRA to the name of this file. This causes the build to append the new file ++to the end of scripts/pnglibconf.dfa. The pngusr.dfa file should contain lines ++of the following forms: ++ ++everything = off ++ ++This turns all optional features off. Include it at the start of pngusr.dfa to ++make it easier to build a minimal configuration. You will need to turn at least ++some features on afterward to enable either reading or writing code, or both. ++ ++option feature on ++option feature off ++ ++Enable or disable a single feature. This will automatically enable other ++features required by a feature that is turned on or disable other features that ++require a feature which is turned off. Conflicting settings will cause an error ++message to be emitted by awk. ++ ++setting feature default value ++ ++Changes the default value of setting 'feature' to 'value'. There are a small ++number of settings listed at the top of pnglibconf.h, they are documented in the ++source code. Most of these values have performance implications for the library ++but most of them have no visible effect on the API. Some can also be overridden ++from the API. ++ ++This method of building a customized pnglibconf.h is illustrated in ++contrib/pngminim/*. See the "$(PNGCONF):" target in the makefile and ++pngusr.dfa in these directories. ++ ++C. Configuration using PNG_USER_CONFIG ++ ++If -DPNG_USER_CONFIG is added to the CPPFLAGS when pnglibconf.h is built, ++the file pngusr.h will automatically be included before the options in ++scripts/pnglibconf.dfa are processed. Your pngusr.h file should contain only ++macro definitions turning features on or off or setting settings. ++ ++Apart from the global setting "everything = off" all the options listed above ++can be set using macros in pngusr.h: ++ ++#define PNG_feature_SUPPORTED ++ ++is equivalent to: ++ ++option feature on ++ ++#define PNG_NO_feature ++ ++is equivalent to: ++ ++option feature off ++ ++#define PNG_feature value ++ ++is equivalent to: ++ ++setting feature default value ++ ++Notice that in both cases, pngusr.dfa and pngusr.h, the contents of the ++pngusr file you supply override the contents of scripts/pnglibconf.dfa ++ ++If confusing or incomprehensible behavior results it is possible to ++examine the intermediate file pnglibconf.dfn to find the full set of ++dependency information for each setting and option. Simply locate the ++feature in the file and read the C comments that precede it. ++ ++This method is also illustrated in the contrib/pngminim/* makefiles and ++pngusr.h. ++ ++III. Reading ++ ++We'll now walk you through the possible functions to call when reading ++in a PNG file sequentially, briefly explaining the syntax and purpose ++of each one. See example.c and png.h for more detail. While ++progressive reading is covered in the next section, you will still ++need some of the functions discussed in this section to read a PNG ++file. ++ ++Setup ++ ++You will want to do the I/O initialization(*) before you get into libpng, ++so if it doesn't work, you don't have much to undo. Of course, you ++will also want to insure that you are, in fact, dealing with a PNG ++file. Libpng provides a simple check to see if a file is a PNG file. ++To use it, pass in the first 1 to 8 bytes of the file to the function ++png_sig_cmp(), and it will return 0 (false) if the bytes match the ++corresponding bytes of the PNG signature, or nonzero (true) otherwise. ++Of course, the more bytes you pass in, the greater the accuracy of the ++prediction. ++ ++If you are intending to keep the file pointer open for use in libpng, ++you must ensure you don't read more than 8 bytes from the beginning ++of the file, and you also have to make a call to png_set_sig_bytes() ++with the number of bytes you read from the beginning. Libpng will ++then only check the bytes (if any) that your program didn't read. ++ ++(*): If you are not using the standard I/O functions, you will need ++to replace them with custom functions. See the discussion under ++Customizing libpng. ++ ++ FILE *fp = fopen(file_name, "rb"); ++ if (!fp) ++ { ++ return ERROR; ++ } ++ ++ if (fread(header, 1, number, fp) != number) ++ { ++ return ERROR; ++ } ++ ++ is_png = !png_sig_cmp(header, 0, number); ++ if (!is_png) ++ { ++ return NOT_PNG; ++ } ++ ++Next, png_struct and png_info need to be allocated and initialized. In ++order to ensure that the size of these structures is correct even with a ++dynamically linked libpng, there are functions to initialize and ++allocate the structures. We also pass the library version, optional ++pointers to error handling functions, and a pointer to a data struct for ++use by the error functions, if necessary (the pointer and functions can ++be NULL if the default error handlers are to be used). See the section ++on Changes to Libpng below regarding the old initialization functions. ++The structure allocation functions quietly return NULL if they fail to ++create the structure, so your application should check for that. ++ ++ png_structp png_ptr = png_create_read_struct ++ (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr, ++ user_error_fn, user_warning_fn); ++ ++ if (!png_ptr) ++ return ERROR; ++ ++ png_infop info_ptr = png_create_info_struct(png_ptr); ++ ++ if (!info_ptr) ++ { ++ png_destroy_read_struct(&png_ptr, ++ (png_infopp)NULL, (png_infopp)NULL); ++ return ERROR; ++ } ++ ++If you want to use your own memory allocation routines, ++use a libpng that was built with PNG_USER_MEM_SUPPORTED defined, and use ++png_create_read_struct_2() instead of png_create_read_struct(): ++ ++ png_structp png_ptr = png_create_read_struct_2 ++ (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr, ++ user_error_fn, user_warning_fn, (png_voidp) ++ user_mem_ptr, user_malloc_fn, user_free_fn); ++ ++The error handling routines passed to png_create_read_struct() ++and the memory alloc/free routines passed to png_create_struct_2() ++are only necessary if you are not using the libpng supplied error ++handling and memory alloc/free functions. ++ ++When libpng encounters an error, it expects to longjmp back ++to your routine. Therefore, you will need to call setjmp and pass ++your png_jmpbuf(png_ptr). If you read the file from different ++routines, you will need to update the longjmp buffer every time you enter ++a new routine that will call a png_*() function. ++ ++See your documentation of setjmp/longjmp for your compiler for more ++information on setjmp/longjmp. See the discussion on libpng error ++handling in the Customizing Libpng section below for more information ++on the libpng error handling. If an error occurs, and libpng longjmp's ++back to your setjmp, you will want to call png_destroy_read_struct() to ++free any memory. ++ ++ if (setjmp(png_jmpbuf(png_ptr))) ++ { ++ png_destroy_read_struct(&png_ptr, &info_ptr, ++ &end_info); ++ fclose(fp); ++ return ERROR; ++ } ++ ++Pass (png_infopp)NULL instead of &end_info if you didn't create ++an end_info structure. ++ ++If you would rather avoid the complexity of setjmp/longjmp issues, ++you can compile libpng with PNG_NO_SETJMP, in which case ++errors will result in a call to PNG_ABORT() which defaults to abort(). ++ ++You can #define PNG_ABORT() to a function that does something ++more useful than abort(), as long as your function does not ++return. ++ ++Now you need to set up the input code. The default for libpng is to ++use the C function fread(). If you use this, you will need to pass a ++valid FILE * in the function png_init_io(). Be sure that the file is ++opened in binary mode. If you wish to handle reading data in another ++way, you need not call the png_init_io() function, but you must then ++implement the libpng I/O methods discussed in the Customizing Libpng ++section below. ++ ++ png_init_io(png_ptr, fp); ++ ++If you had previously opened the file and read any of the signature from ++the beginning in order to see if this was a PNG file, you need to let ++libpng know that there are some bytes missing from the start of the file. ++ ++ png_set_sig_bytes(png_ptr, number); ++ ++You can change the zlib compression buffer size to be used while ++reading compressed data with ++ ++ png_set_compression_buffer_size(png_ptr, buffer_size); ++ ++where the default size is 8192 bytes. Note that the buffer size ++is changed immediately and the buffer is reallocated immediately, ++instead of setting a flag to be acted upon later. ++ ++If you want CRC errors to be handled in a different manner than ++the default, use ++ ++ png_set_crc_action(png_ptr, crit_action, ancil_action); ++ ++The values for png_set_crc_action() say how libpng is to handle CRC errors in ++ancillary and critical chunks, and whether to use the data contained ++therein. Starting with libpng-1.6.26, this also governs how an ADLER32 error ++is handled while reading the IDAT chunk. Note that it is impossible to ++"discard" data in a critical chunk. ++ ++Choices for (int) crit_action are ++ PNG_CRC_DEFAULT 0 error/quit ++ PNG_CRC_ERROR_QUIT 1 error/quit ++ PNG_CRC_WARN_USE 3 warn/use data ++ PNG_CRC_QUIET_USE 4 quiet/use data ++ PNG_CRC_NO_CHANGE 5 use the current value ++ ++Choices for (int) ancil_action are ++ PNG_CRC_DEFAULT 0 error/quit ++ PNG_CRC_ERROR_QUIT 1 error/quit ++ PNG_CRC_WARN_DISCARD 2 warn/discard data ++ PNG_CRC_WARN_USE 3 warn/use data ++ PNG_CRC_QUIET_USE 4 quiet/use data ++ PNG_CRC_NO_CHANGE 5 use the current value ++ ++When the setting for crit_action is PNG_CRC_QUIET_USE, the CRC and ADLER32 ++checksums are not only ignored, but they are not evaluated. ++ ++Setting up callback code ++ ++You can set up a callback function to handle any unknown chunks in the ++input stream. You must supply the function ++ ++ read_chunk_callback(png_structp png_ptr, ++ png_unknown_chunkp chunk); ++ { ++ /* The unknown chunk structure contains your ++ chunk data, along with similar data for any other ++ unknown chunks: */ ++ ++ png_byte name[5]; ++ png_byte *data; ++ size_t size; ++ ++ /* Note that libpng has already taken care of ++ the CRC handling */ ++ ++ /* put your code here. Search for your chunk in the ++ unknown chunk structure, process it, and return one ++ of the following: */ ++ ++ return -n; /* chunk had an error */ ++ return 0; /* did not recognize */ ++ return n; /* success */ ++ } ++ ++(You can give your function another name that you like instead of ++"read_chunk_callback") ++ ++To inform libpng about your function, use ++ ++ png_set_read_user_chunk_fn(png_ptr, user_chunk_ptr, ++ read_chunk_callback); ++ ++This names not only the callback function, but also a user pointer that ++you can retrieve with ++ ++ png_get_user_chunk_ptr(png_ptr); ++ ++If you call the png_set_read_user_chunk_fn() function, then all unknown ++chunks which the callback does not handle will be saved when read. You can ++cause them to be discarded by returning '1' ("handled") instead of '0'. This ++behavior will change in libpng 1.7 and the default handling set by the ++png_set_keep_unknown_chunks() function, described below, will be used when the ++callback returns 0. If you want the existing behavior you should set the global ++default to PNG_HANDLE_CHUNK_IF_SAFE now; this is compatible with all current ++versions of libpng and with 1.7. Libpng 1.6 issues a warning if you keep the ++default, or PNG_HANDLE_CHUNK_NEVER, and the callback returns 0. ++ ++At this point, you can set up a callback function that will be ++called after each row has been read, which you can use to control ++a progress meter or the like. It's demonstrated in pngtest.c. ++You must supply a function ++ ++ void read_row_callback(png_structp png_ptr, ++ png_uint_32 row, int pass); ++ { ++ /* put your code here */ ++ } ++ ++(You can give it another name that you like instead of "read_row_callback") ++ ++To inform libpng about your function, use ++ ++ png_set_read_status_fn(png_ptr, read_row_callback); ++ ++When this function is called the row has already been completely processed and ++the 'row' and 'pass' refer to the next row to be handled. For the ++non-interlaced case the row that was just handled is simply one less than the ++passed in row number, and pass will always be 0. For the interlaced case the ++same applies unless the row value is 0, in which case the row just handled was ++the last one from one of the preceding passes. Because interlacing may skip a ++pass you cannot be sure that the preceding pass is just 'pass-1'; if you really ++need to know what the last pass is record (row,pass) from the callback and use ++the last recorded value each time. ++ ++As with the user transform you can find the output row using the ++PNG_ROW_FROM_PASS_ROW macro. ++ ++Unknown-chunk handling ++ ++Now you get to set the way the library processes unknown chunks in the ++input PNG stream. Both known and unknown chunks will be read. Normal ++behavior is that known chunks will be parsed into information in ++various info_ptr members while unknown chunks will be discarded. This ++behavior can be wasteful if your application will never use some known ++chunk types. To change this, you can call: ++ ++ png_set_keep_unknown_chunks(png_ptr, keep, ++ chunk_list, num_chunks); ++ ++ keep - 0: default unknown chunk handling ++ 1: ignore; do not keep ++ 2: keep only if safe-to-copy ++ 3: keep even if unsafe-to-copy ++ ++ You can use these definitions: ++ PNG_HANDLE_CHUNK_AS_DEFAULT 0 ++ PNG_HANDLE_CHUNK_NEVER 1 ++ PNG_HANDLE_CHUNK_IF_SAFE 2 ++ PNG_HANDLE_CHUNK_ALWAYS 3 ++ ++ chunk_list - list of chunks affected (a byte string, ++ five bytes per chunk, NULL or '\0' if ++ num_chunks is positive; ignored if ++ numchunks <= 0). ++ ++ num_chunks - number of chunks affected; if 0, all ++ unknown chunks are affected. If positive, ++ only the chunks in the list are affected, ++ and if negative all unknown chunks and ++ all known chunks except for the IHDR, ++ PLTE, tRNS, IDAT, and IEND chunks are ++ affected. ++ ++Unknown chunks declared in this way will be saved as raw data onto a ++list of png_unknown_chunk structures. If a chunk that is normally ++known to libpng is named in the list, it will be handled as unknown, ++according to the "keep" directive. If a chunk is named in successive ++instances of png_set_keep_unknown_chunks(), the final instance will ++take precedence. The IHDR and IEND chunks should not be named in ++chunk_list; if they are, libpng will process them normally anyway. ++If you know that your application will never make use of some particular ++chunks, use PNG_HANDLE_CHUNK_NEVER (or 1) as demonstrated below. ++ ++Here is an example of the usage of png_set_keep_unknown_chunks(), ++where the private "vpAg" chunk will later be processed by a user chunk ++callback function: ++ ++ png_byte vpAg[5]={118, 112, 65, 103, (png_byte) '\0'}; ++ ++ #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED) ++ png_byte unused_chunks[]= ++ { ++ 104, 73, 83, 84, (png_byte) '\0', /* hIST */ ++ 105, 84, 88, 116, (png_byte) '\0', /* iTXt */ ++ 112, 67, 65, 76, (png_byte) '\0', /* pCAL */ ++ 115, 67, 65, 76, (png_byte) '\0', /* sCAL */ ++ 115, 80, 76, 84, (png_byte) '\0', /* sPLT */ ++ 116, 73, 77, 69, (png_byte) '\0', /* tIME */ ++ }; ++ #endif ++ ++ ... ++ ++ #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED) ++ /* ignore all unknown chunks ++ * (use global setting "2" for libpng16 and earlier): ++ */ ++ png_set_keep_unknown_chunks(read_ptr, 2, NULL, 0); ++ ++ /* except for vpAg: */ ++ png_set_keep_unknown_chunks(read_ptr, 2, vpAg, 1); ++ ++ /* also ignore unused known chunks: */ ++ png_set_keep_unknown_chunks(read_ptr, 1, unused_chunks, ++ (int)(sizeof unused_chunks)/5); ++ #endif ++ ++User limits ++ ++The PNG specification allows the width and height of an image to be as ++large as 2^31-1 (0x7fffffff), or about 2.147 billion rows and columns. ++For safety, libpng imposes a default limit of 1 million rows and columns. ++Larger images will be rejected immediately with a png_error() call. If ++you wish to change these limits, you can use ++ ++ png_set_user_limits(png_ptr, width_max, height_max); ++ ++to set your own limits (libpng may reject some very wide images ++anyway because of potential buffer overflow conditions). ++ ++You should put this statement after you create the PNG structure and ++before calling png_read_info(), png_read_png(), or png_process_data(). ++ ++When writing a PNG datastream, put this statement before calling ++png_write_info() or png_write_png(). ++ ++If you need to retrieve the limits that are being applied, use ++ ++ width_max = png_get_user_width_max(png_ptr); ++ height_max = png_get_user_height_max(png_ptr); ++ ++The PNG specification sets no limit on the number of ancillary chunks ++allowed in a PNG datastream. By default, libpng imposes a limit of ++a total of 1000 sPLT, tEXt, iTXt, zTXt, and unknown chunks to be stored. ++If you have set up both info_ptr and end_info_ptr, the limit applies ++separately to each. You can change the limit on the total number of such ++chunks that will be stored, with ++ ++ png_set_chunk_cache_max(png_ptr, user_chunk_cache_max); ++ ++where 0x7fffffffL means unlimited. You can retrieve this limit with ++ ++ chunk_cache_max = png_get_chunk_cache_max(png_ptr); ++ ++Libpng imposes a limit of 8 Megabytes (8,000,000 bytes) on the amount of ++memory that any chunk other than IDAT can occupy, originally or when ++decompressed (prior to libpng-1.6.32 the limit was only applied to compressed ++chunks after decompression). You can change this limit with ++ ++ png_set_chunk_malloc_max(png_ptr, user_chunk_malloc_max); ++ ++and you can retrieve the limit with ++ ++ chunk_malloc_max = png_get_chunk_malloc_max(png_ptr); ++ ++Any chunks that would cause either of these limits to be exceeded will ++be ignored. ++ ++Information about your system ++ ++If you intend to display the PNG or to incorporate it in other image data you ++need to tell libpng information about your display or drawing surface so that ++libpng can convert the values in the image to match the display. ++ ++From libpng-1.5.4 this information can be set before reading the PNG file ++header. In earlier versions png_set_gamma() existed but behaved incorrectly if ++called before the PNG file header had been read and png_set_alpha_mode() did not ++exist. ++ ++If you need to support versions prior to libpng-1.5.4 test the version number ++as illustrated below using "PNG_LIBPNG_VER >= 10504" and follow the procedures ++described in the appropriate manual page. ++ ++You give libpng the encoding expected by your system expressed as a 'gamma' ++value. You can also specify a default encoding for the PNG file in ++case the required information is missing from the file. By default libpng ++assumes that the PNG data matches your system, to keep this default call: ++ ++ png_set_gamma(png_ptr, screen_gamma, output_gamma); ++ ++or you can use the fixed point equivalent: ++ ++ png_set_gamma_fixed(png_ptr, PNG_FP_1*screen_gamma, ++ PNG_FP_1*output_gamma); ++ ++If you don't know the gamma for your system it is probably 2.2 - a good ++approximation to the IEC standard for display systems (sRGB). If images are ++too contrasty or washed out you got the value wrong - check your system ++documentation! ++ ++Many systems permit the system gamma to be changed via a lookup table in the ++display driver, a few systems, including older Macs, change the response by ++default. As of 1.5.4 three special values are available to handle common ++situations: ++ ++ PNG_DEFAULT_sRGB: Indicates that the system conforms to the ++ IEC 61966-2-1 standard. This matches almost ++ all systems. ++ PNG_GAMMA_MAC_18: Indicates that the system is an older ++ (pre Mac OS 10.6) Apple Macintosh system with ++ the default settings. ++ PNG_GAMMA_LINEAR: Just the fixed point value for 1.0 - indicates ++ that the system expects data with no gamma ++ encoding. ++ ++You would use the linear (unencoded) value if you need to process the pixel ++values further because this avoids the need to decode and re-encode each ++component value whenever arithmetic is performed. A lot of graphics software ++uses linear values for this reason, often with higher precision component values ++to preserve overall accuracy. ++ ++ ++The output_gamma value expresses how to decode the output values, not how ++they are encoded. The values used correspond to the normal numbers used to ++describe the overall gamma of a computer display system; for example 2.2 for ++an sRGB conformant system. The values are scaled by 100000 in the _fixed ++version of the API (so 220000 for sRGB.) ++ ++The inverse of the value is always used to provide a default for the PNG file ++encoding if it has no gAMA chunk and if png_set_gamma() has not been called ++to override the PNG gamma information. ++ ++When the ALPHA_OPTIMIZED mode is selected the output gamma is used to encode ++opaque pixels however pixels with lower alpha values are not encoded, ++regardless of the output gamma setting. ++ ++When the standard Porter Duff handling is requested with mode 1 the output ++encoding is set to be linear and the output_gamma value is only relevant ++as a default for input data that has no gamma information. The linear output ++encoding will be overridden if png_set_gamma() is called - the results may be ++highly unexpected! ++ ++The following numbers are derived from the sRGB standard and the research ++behind it. sRGB is defined to be approximated by a PNG gAMA chunk value of ++0.45455 (1/2.2) for PNG. The value implicitly includes any viewing ++correction required to take account of any differences in the color ++environment of the original scene and the intended display environment; the ++value expresses how to *decode* the image for display, not how the original ++data was *encoded*. ++ ++sRGB provides a peg for the PNG standard by defining a viewing environment. ++sRGB itself, and earlier TV standards, actually use a more complex transform ++(a linear portion then a gamma 2.4 power law) than PNG can express. (PNG is ++limited to simple power laws.) By saying that an image for direct display on ++an sRGB conformant system should be stored with a gAMA chunk value of 45455 ++(11.3.3.2 and 11.3.3.5 of the ISO PNG specification) the PNG specification ++makes it possible to derive values for other display systems and ++environments. ++ ++The Mac value is deduced from the sRGB based on an assumption that the actual ++extra viewing correction used in early Mac display systems was implemented as ++a power 1.45 lookup table. ++ ++Any system where a programmable lookup table is used or where the behavior of ++the final display device characteristics can be changed requires system ++specific code to obtain the current characteristic. However this can be ++difficult and most PNG gamma correction only requires an approximate value. ++ ++By default, if png_set_alpha_mode() is not called, libpng assumes that all ++values are unencoded, linear, values and that the output device also has a ++linear characteristic. This is only very rarely correct - it is invariably ++better to call png_set_alpha_mode() with PNG_DEFAULT_sRGB than rely on the ++default if you don't know what the right answer is! ++ ++The special value PNG_GAMMA_MAC_18 indicates an older Mac system (pre Mac OS ++10.6) which used a correction table to implement a somewhat lower gamma on an ++otherwise sRGB system. ++ ++Both these values are reserved (not simple gamma values) in order to allow ++more precise correction internally in the future. ++ ++NOTE: the values can be passed to either the fixed or floating ++point APIs, but the floating point API will also accept floating point ++values. ++ ++The second thing you may need to tell libpng about is how your system handles ++alpha channel information. Some, but not all, PNG files contain an alpha ++channel. To display these files correctly you need to compose the data onto a ++suitable background, as described in the PNG specification. ++ ++Libpng only supports composing onto a single color (using png_set_background; ++see below). Otherwise you must do the composition yourself and, in this case, ++you may need to call png_set_alpha_mode: ++ ++ #if PNG_LIBPNG_VER >= 10504 ++ png_set_alpha_mode(png_ptr, mode, screen_gamma); ++ #else ++ png_set_gamma(png_ptr, screen_gamma, 1.0/screen_gamma); ++ #endif ++ ++The screen_gamma value is the same as the argument to png_set_gamma; however, ++how it affects the output depends on the mode. png_set_alpha_mode() sets the ++file gamma default to 1/screen_gamma, so normally you don't need to call ++png_set_gamma. If you need different defaults call png_set_gamma() before ++png_set_alpha_mode() - if you call it after it will override the settings made ++by png_set_alpha_mode(). ++ ++The mode is as follows: ++ ++ PNG_ALPHA_PNG: The data is encoded according to the PNG ++specification. Red, green and blue, or gray, components are ++gamma encoded color values and are not premultiplied by the ++alpha value. The alpha value is a linear measure of the ++contribution of the pixel to the corresponding final output pixel. ++ ++You should normally use this format if you intend to perform ++color correction on the color values; most, maybe all, color ++correction software has no handling for the alpha channel and, ++anyway, the math to handle pre-multiplied component values is ++unnecessarily complex. ++ ++Before you do any arithmetic on the component values you need ++to remove the gamma encoding and multiply out the alpha ++channel. See the PNG specification for more detail. It is ++important to note that when an image with an alpha channel is ++scaled, linear encoded, pre-multiplied component values must ++be used! ++ ++The remaining modes assume you don't need to do any further color correction or ++that if you do, your color correction software knows all about alpha (it ++probably doesn't!). They 'associate' the alpha with the color information by ++storing color channel values that have been scaled by the alpha. The ++advantage is that the color channels can be resampled (the image can be ++scaled) in this form. The disadvantage is that normal practice is to store ++linear, not (gamma) encoded, values and this requires 16-bit channels for ++still images rather than the 8-bit channels that are just about sufficient if ++gamma encoding is used. In addition all non-transparent pixel values, ++including completely opaque ones, must be gamma encoded to produce the final ++image. These are the 'STANDARD', 'ASSOCIATED' or 'PREMULTIPLIED' modes ++described below (the latter being the two common names for associated alpha ++color channels). Note that PNG files always contain non-associated color ++channels; png_set_alpha_mode() with one of the modes causes the decoder to ++convert the pixels to an associated form before returning them to your ++application. ++ ++Since it is not necessary to perform arithmetic on opaque color values so ++long as they are not to be resampled and are in the final color space it is ++possible to optimize the handling of alpha by storing the opaque pixels in ++the PNG format (adjusted for the output color space) while storing partially ++opaque pixels in the standard, linear, format. The accuracy required for ++standard alpha composition is relatively low, because the pixels are ++isolated, therefore typically the accuracy loss in storing 8-bit linear ++values is acceptable. (This is not true if the alpha channel is used to ++simulate transparency over large areas - use 16 bits or the PNG mode in ++this case!) This is the 'OPTIMIZED' mode. For this mode a pixel is ++treated as opaque only if the alpha value is equal to the maximum value. ++ ++ PNG_ALPHA_STANDARD: The data libpng produces is encoded in the ++standard way assumed by most correctly written graphics software. ++The gamma encoding will be removed by libpng and the ++linear component values will be pre-multiplied by the ++alpha channel. ++ ++With this format the final image must be re-encoded to ++match the display gamma before the image is displayed. ++If your system doesn't do that, yet still seems to ++perform arithmetic on the pixels without decoding them, ++it is broken - check out the modes below. ++ ++With PNG_ALPHA_STANDARD libpng always produces linear ++component values, whatever screen_gamma you supply. The ++screen_gamma value is, however, used as a default for ++the file gamma if the PNG file has no gamma information. ++ ++If you call png_set_gamma() after png_set_alpha_mode() you ++will override the linear encoding. Instead the ++pre-multiplied pixel values will be gamma encoded but ++the alpha channel will still be linear. This may ++actually match the requirements of some broken software, ++but it is unlikely. ++ ++While linear 8-bit data is often used it has ++insufficient precision for any image with a reasonable ++dynamic range. To avoid problems, and if your software ++supports it, use png_set_expand_16() to force all ++components to 16 bits. ++ ++ PNG_ALPHA_OPTIMIZED: This mode is the same as PNG_ALPHA_STANDARD ++except that completely opaque pixels are gamma encoded according to ++the screen_gamma value. Pixels with alpha less than 1.0 ++will still have linear components. ++ ++Use this format if you have control over your ++compositing software and so don't do other arithmetic ++(such as scaling) on the data you get from libpng. Your ++compositing software can simply copy opaque pixels to ++the output but still has linear values for the ++non-opaque pixels. ++ ++In normal compositing, where the alpha channel encodes ++partial pixel coverage (as opposed to broad area ++translucency), the inaccuracies of the 8-bit ++representation of non-opaque pixels are irrelevant. ++ ++You can also try this format if your software is broken; ++it might look better. ++ ++ PNG_ALPHA_BROKEN: This is PNG_ALPHA_STANDARD; however, all component ++values, including the alpha channel are gamma encoded. This is ++broken because, in practice, no implementation that uses this choice ++correctly undoes the encoding before handling alpha composition. Use this ++choice only if other serious errors in the software or hardware you use ++mandate it. In most cases of broken software or hardware the bug in the ++final display manifests as a subtle halo around composited parts of the ++image. You may not even perceive this as a halo; the composited part of ++the image may simply appear separate from the background, as though it had ++been cut out of paper and pasted on afterward. ++ ++If you don't have to deal with bugs in software or hardware, or if you can fix ++them, there are three recommended ways of using png_set_alpha_mode(): ++ ++ png_set_alpha_mode(png_ptr, PNG_ALPHA_PNG, ++ screen_gamma); ++ ++You can do color correction on the result (libpng does not currently ++support color correction internally). When you handle the alpha channel ++you need to undo the gamma encoding and multiply out the alpha. ++ ++ png_set_alpha_mode(png_ptr, PNG_ALPHA_STANDARD, ++ screen_gamma); ++ png_set_expand_16(png_ptr); ++ ++If you are using the high level interface, don't call png_set_expand_16(); ++instead pass PNG_TRANSFORM_EXPAND_16 to the interface. ++ ++With this mode you can't do color correction, but you can do arithmetic, ++including composition and scaling, on the data without further processing. ++ ++ png_set_alpha_mode(png_ptr, PNG_ALPHA_OPTIMIZED, ++ screen_gamma); ++ ++You can avoid the expansion to 16-bit components with this mode, but you ++lose the ability to scale the image or perform other linear arithmetic. ++All you can do is compose the result onto a matching output. Since this ++mode is libpng-specific you also need to write your own composition ++software. ++ ++The following are examples of calls to png_set_alpha_mode to achieve the ++required overall gamma correction and, where necessary, alpha ++premultiplication. ++ ++ png_set_alpha_mode(pp, PNG_ALPHA_PNG, PNG_DEFAULT_sRGB); ++ ++Choices for the alpha_mode are ++ ++ PNG_ALPHA_PNG 0 /* according to the PNG standard */ ++ PNG_ALPHA_STANDARD 1 /* according to Porter/Duff */ ++ PNG_ALPHA_ASSOCIATED 1 /* as above; this is the normal practice */ ++ PNG_ALPHA_PREMULTIPLIED 1 /* as above */ ++ PNG_ALPHA_OPTIMIZED 2 /* 'PNG' for opaque pixels, else 'STANDARD' */ ++ PNG_ALPHA_BROKEN 3 /* the alpha channel is gamma encoded */ ++ ++PNG_ALPHA_PNG is the default libpng handling of the alpha channel. It is not ++pre-multiplied into the color components. In addition the call states ++that the output is for a sRGB system and causes all PNG files without gAMA ++chunks to be assumed to be encoded using sRGB. ++ ++ png_set_alpha_mode(pp, PNG_ALPHA_PNG, PNG_GAMMA_MAC); ++ ++In this case the output is assumed to be something like an sRGB conformant ++display preceded by a power-law lookup table of power 1.45. This is how ++early Mac systems behaved. ++ ++ png_set_alpha_mode(pp, PNG_ALPHA_STANDARD, PNG_GAMMA_LINEAR); ++ ++This is the classic Jim Blinn approach and will work in academic ++environments where everything is done by the book. It has the shortcoming ++of assuming that input PNG data with no gamma information is linear - this ++is unlikely to be correct unless the PNG files were generated locally. ++Most of the time the output precision will be so low as to show ++significant banding in dark areas of the image. ++ ++ png_set_expand_16(pp); ++ png_set_alpha_mode(pp, PNG_ALPHA_STANDARD, PNG_DEFAULT_sRGB); ++ ++This is a somewhat more realistic Jim Blinn inspired approach. PNG files ++are assumed to have the sRGB encoding if not marked with a gamma value and ++the output is always 16 bits per component. This permits accurate scaling ++and processing of the data. If you know that your input PNG files were ++generated locally you might need to replace PNG_DEFAULT_sRGB with the ++correct value for your system. ++ ++ png_set_alpha_mode(pp, PNG_ALPHA_OPTIMIZED, PNG_DEFAULT_sRGB); ++ ++If you just need to composite the PNG image onto an existing background ++and if you control the code that does this you can use the optimization ++setting. In this case you just copy completely opaque pixels to the ++output. For pixels that are not completely transparent (you just skip ++those) you do the composition math using png_composite or png_composite_16 ++below then encode the resultant 8-bit or 16-bit values to match the output ++encoding. ++ ++ Other cases ++ ++If neither the PNG nor the standard linear encoding work for you because ++of the software or hardware you use then you have a big problem. The PNG ++case will probably result in halos around the image. The linear encoding ++will probably result in a washed out, too bright, image (it's actually too ++contrasty.) Try the ALPHA_OPTIMIZED mode above - this will probably ++substantially reduce the halos. Alternatively try: ++ ++ png_set_alpha_mode(pp, PNG_ALPHA_BROKEN, PNG_DEFAULT_sRGB); ++ ++This option will also reduce the halos, but there will be slight dark ++halos round the opaque parts of the image where the background is light. ++In the OPTIMIZED mode the halos will be light halos where the background ++is dark. Take your pick - the halos are unavoidable unless you can get ++your hardware/software fixed! (The OPTIMIZED approach is slightly ++faster.) ++ ++When the default gamma of PNG files doesn't match the output gamma. ++If you have PNG files with no gamma information png_set_alpha_mode allows ++you to provide a default gamma, but it also sets the output gamma to the ++matching value. If you know your PNG files have a gamma that doesn't ++match the output you can take advantage of the fact that ++png_set_alpha_mode always sets the output gamma but only sets the PNG ++default if it is not already set: ++ ++ png_set_alpha_mode(pp, PNG_ALPHA_PNG, PNG_DEFAULT_sRGB); ++ png_set_alpha_mode(pp, PNG_ALPHA_PNG, PNG_GAMMA_MAC); ++ ++The first call sets both the default and the output gamma values, the ++second call overrides the output gamma without changing the default. This ++is easier than achieving the same effect with png_set_gamma. You must use ++PNG_ALPHA_PNG for the first call - internal checking in png_set_alpha will ++fire if more than one call to png_set_alpha_mode and png_set_background is ++made in the same read operation, however multiple calls with PNG_ALPHA_PNG ++are ignored. ++ ++If you don't need, or can't handle, the alpha channel you can call ++png_set_background() to remove it by compositing against a fixed color. Don't ++call png_set_strip_alpha() to do this - it will leave spurious pixel values in ++transparent parts of this image. ++ ++ png_set_background(png_ptr, &background_color, ++ PNG_BACKGROUND_GAMMA_SCREEN, 0, 1); ++ ++The background_color is an RGB or grayscale value according to the data format ++libpng will produce for you. Because you don't yet know the format of the PNG ++file, if you call png_set_background at this point you must arrange for the ++format produced by libpng to always have 8-bit or 16-bit components and then ++store the color as an 8-bit or 16-bit color as appropriate. The color contains ++separate gray and RGB component values, so you can let libpng produce gray or ++RGB output according to the input format, but low bit depth grayscale images ++must always be converted to at least 8-bit format. (Even though low bit depth ++grayscale images can't have an alpha channel they can have a transparent ++color!) ++ ++You set the transforms you need later, either as flags to the high level ++interface or libpng API calls for the low level interface. For reference the ++settings and API calls required are: ++ ++8-bit values: ++ PNG_TRANSFORM_SCALE_16 | PNG_EXPAND ++ png_set_expand(png_ptr); png_set_scale_16(png_ptr); ++ ++ If you must get exactly the same inaccurate results ++ produced by default in versions prior to libpng-1.5.4, ++ use PNG_TRANSFORM_STRIP_16 and png_set_strip_16(png_ptr) ++ instead. ++ ++16-bit values: ++ PNG_TRANSFORM_EXPAND_16 ++ png_set_expand_16(png_ptr); ++ ++In either case palette image data will be expanded to RGB. If you just want ++color data you can add PNG_TRANSFORM_GRAY_TO_RGB or png_set_gray_to_rgb(png_ptr) ++to the list. ++ ++Calling png_set_background before the PNG file header is read will not work ++prior to libpng-1.5.4. Because the failure may result in unexpected warnings or ++errors it is therefore much safer to call png_set_background after the head has ++been read. Unfortunately this means that prior to libpng-1.5.4 it cannot be ++used with the high level interface. ++ ++The high-level read interface ++ ++At this point there are two ways to proceed; through the high-level ++read interface, or through a sequence of low-level read operations. ++You can use the high-level interface if (a) you are willing to read ++the entire image into memory, and (b) the input transformations ++you want to do are limited to the following set: ++ ++ PNG_TRANSFORM_IDENTITY No transformation ++ PNG_TRANSFORM_SCALE_16 Strip 16-bit samples to ++ 8-bit accurately ++ PNG_TRANSFORM_STRIP_16 Chop 16-bit samples to ++ 8-bit less accurately ++ PNG_TRANSFORM_STRIP_ALPHA Discard the alpha channel ++ PNG_TRANSFORM_PACKING Expand 1, 2 and 4-bit ++ samples to bytes ++ PNG_TRANSFORM_PACKSWAP Change order of packed ++ pixels to LSB first ++ PNG_TRANSFORM_EXPAND Perform set_expand() ++ PNG_TRANSFORM_INVERT_MONO Invert monochrome images ++ PNG_TRANSFORM_SHIFT Normalize pixels to the ++ sBIT depth ++ PNG_TRANSFORM_BGR Flip RGB to BGR, RGBA ++ to BGRA ++ PNG_TRANSFORM_SWAP_ALPHA Flip RGBA to ARGB or GA ++ to AG ++ PNG_TRANSFORM_INVERT_ALPHA Change alpha from opacity ++ to transparency ++ PNG_TRANSFORM_SWAP_ENDIAN Byte-swap 16-bit samples ++ PNG_TRANSFORM_GRAY_TO_RGB Expand grayscale samples ++ to RGB (or GA to RGBA) ++ PNG_TRANSFORM_EXPAND_16 Expand samples to 16 bits ++ ++(This excludes setting a background color, doing gamma transformation, ++quantizing, and setting filler.) If this is the case, simply do this: ++ ++ png_read_png(png_ptr, info_ptr, png_transforms, NULL) ++ ++where png_transforms is an integer containing the bitwise OR of some ++set of transformation flags. This call is equivalent to png_read_info(), ++followed the set of transformations indicated by the transform mask, ++then png_read_image(), and finally png_read_end(). ++ ++(The final parameter of this call is not yet used. Someday it might point ++to transformation parameters required by some future input transform.) ++ ++You must use png_transforms and not call any png_set_transform() functions ++when you use png_read_png(). ++ ++After you have called png_read_png(), you can retrieve the image data ++with ++ ++ row_pointers = png_get_rows(png_ptr, info_ptr); ++ ++where row_pointers is an array of pointers to the pixel data for each row: ++ ++ png_bytep row_pointers[height]; ++ ++If you know your image size and pixel size ahead of time, you can allocate ++row_pointers prior to calling png_read_png() with ++ ++ if (height > PNG_UINT_32_MAX/(sizeof (png_byte))) ++ png_error (png_ptr, ++ "Image is too tall to process in memory"); ++ ++ if (width > PNG_UINT_32_MAX/pixel_size) ++ png_error (png_ptr, ++ "Image is too wide to process in memory"); ++ ++ row_pointers = png_malloc(png_ptr, ++ height*(sizeof (png_bytep))); ++ ++ for (int i=0; i PNG_SIZE_MAX/(width*pixel_size)) { ++ png_error(png_ptr,"image_data buffer would be too large"); ++ } ++ ++ png_bytep buffer=png_malloc(png_ptr,height*width*pixel_size); ++ ++ for (int i=0; i) and ++png_get_(png_ptr, info_ptr, ...) functions return non-zero if the ++data has been read, or zero if it is missing. The parameters to the ++png_get_ are set directly if they are simple data types, or a ++pointer into the info_ptr is returned for any complex types. ++ ++The colorspace data from gAMA, cHRM, sRGB, iCCP, and sBIT chunks ++is simply returned to give the application information about how the ++image was encoded. Libpng itself only does transformations using the file ++gamma when combining semitransparent pixels with the background color, and, ++since libpng-1.6.0, when converting between 8-bit sRGB and 16-bit linear pixels ++within the simplified API. Libpng also uses the file gamma when converting ++RGB to gray, beginning with libpng-1.0.5, if the application calls ++png_set_rgb_to_gray()). ++ ++ png_get_PLTE(png_ptr, info_ptr, &palette, ++ &num_palette); ++ ++ palette - the palette for the file ++ (array of png_color) ++ ++ num_palette - number of entries in the palette ++ ++ png_get_gAMA(png_ptr, info_ptr, &file_gamma); ++ png_get_gAMA_fixed(png_ptr, info_ptr, &int_file_gamma); ++ ++ file_gamma - the gamma at which the file is ++ written (PNG_INFO_gAMA) ++ ++ int_file_gamma - 100,000 times the gamma at which the ++ file is written ++ ++ png_get_cHRM(png_ptr, info_ptr, &white_x, &white_y, &red_x, ++ &red_y, &green_x, &green_y, &blue_x, &blue_y) ++ png_get_cHRM_XYZ(png_ptr, info_ptr, &red_X, &red_Y, &red_Z, ++ &green_X, &green_Y, &green_Z, &blue_X, &blue_Y, ++ &blue_Z) ++ png_get_cHRM_fixed(png_ptr, info_ptr, &int_white_x, ++ &int_white_y, &int_red_x, &int_red_y, ++ &int_green_x, &int_green_y, &int_blue_x, ++ &int_blue_y) ++ png_get_cHRM_XYZ_fixed(png_ptr, info_ptr, &int_red_X, &int_red_Y, ++ &int_red_Z, &int_green_X, &int_green_Y, ++ &int_green_Z, &int_blue_X, &int_blue_Y, ++ &int_blue_Z) ++ ++ {white,red,green,blue}_{x,y} ++ A color space encoding specified using the ++ chromaticities of the end points and the ++ white point. (PNG_INFO_cHRM) ++ ++ {red,green,blue}_{X,Y,Z} ++ A color space encoding specified using the ++ encoding end points - the CIE tristimulus ++ specification of the intended color of the red, ++ green and blue channels in the PNG RGB data. ++ The white point is simply the sum of the three ++ end points. (PNG_INFO_cHRM) ++ ++ png_get_sRGB(png_ptr, info_ptr, &srgb_intent); ++ ++ srgb_intent - the rendering intent (PNG_INFO_sRGB) ++ The presence of the sRGB chunk ++ means that the pixel data is in the ++ sRGB color space. This chunk also ++ implies specific values of gAMA and ++ cHRM. ++ ++ png_get_iCCP(png_ptr, info_ptr, &name, ++ &compression_type, &profile, &proflen); ++ ++ name - The profile name. ++ ++ compression_type - The compression type; always ++ PNG_COMPRESSION_TYPE_BASE for PNG 1.0. ++ You may give NULL to this argument to ++ ignore it. ++ ++ profile - International Color Consortium color ++ profile data. May contain NULs. ++ ++ proflen - length of profile data in bytes. ++ ++ png_get_sBIT(png_ptr, info_ptr, &sig_bit); ++ ++ sig_bit - the number of significant bits for ++ (PNG_INFO_sBIT) each of the gray, ++ red, green, and blue channels, ++ whichever are appropriate for the ++ given color type (png_color_16) ++ ++ png_get_tRNS(png_ptr, info_ptr, &trans_alpha, ++ &num_trans, &trans_color); ++ ++ trans_alpha - array of alpha (transparency) ++ entries for palette (PNG_INFO_tRNS) ++ ++ num_trans - number of transparent entries ++ (PNG_INFO_tRNS) ++ ++ trans_color - graylevel or color sample values of ++ the single transparent color for ++ non-paletted images (PNG_INFO_tRNS) ++ ++ png_get_eXIf_1(png_ptr, info_ptr, &num_exif, &exif); ++ (PNG_INFO_eXIf) ++ ++ exif - Exif profile (array of png_byte) ++ ++ png_get_hIST(png_ptr, info_ptr, &hist); ++ (PNG_INFO_hIST) ++ ++ hist - histogram of palette (array of ++ png_uint_16) ++ ++ png_get_tIME(png_ptr, info_ptr, &mod_time); ++ ++ mod_time - time image was last modified ++ (PNG_VALID_tIME) ++ ++ png_get_bKGD(png_ptr, info_ptr, &background); ++ ++ background - background color (of type ++ png_color_16p) (PNG_VALID_bKGD) ++ valid 16-bit red, green and blue ++ values, regardless of color_type ++ ++ num_comments = png_get_text(png_ptr, info_ptr, ++ &text_ptr, &num_text); ++ ++ num_comments - number of comments ++ ++ text_ptr - array of png_text holding image ++ comments ++ ++ text_ptr[i].compression - type of compression used ++ on "text" PNG_TEXT_COMPRESSION_NONE ++ PNG_TEXT_COMPRESSION_zTXt ++ PNG_ITXT_COMPRESSION_NONE ++ PNG_ITXT_COMPRESSION_zTXt ++ ++ text_ptr[i].key - keyword for comment. Must contain ++ 1-79 characters. ++ ++ text_ptr[i].text - text comments for current ++ keyword. Can be empty. ++ ++ text_ptr[i].text_length - length of text string, ++ after decompression, 0 for iTXt ++ ++ text_ptr[i].itxt_length - length of itxt string, ++ after decompression, 0 for tEXt/zTXt ++ ++ text_ptr[i].lang - language of comment (empty ++ string for unknown). ++ ++ text_ptr[i].lang_key - keyword in UTF-8 ++ (empty string for unknown). ++ ++ Note that the itxt_length, lang, and lang_key ++ members of the text_ptr structure only exist when the ++ library is built with iTXt chunk support. Prior to ++ libpng-1.4.0 the library was built by default without ++ iTXt support. Also note that when iTXt is supported, ++ they contain NULL pointers when the "compression" ++ field contains PNG_TEXT_COMPRESSION_NONE or ++ PNG_TEXT_COMPRESSION_zTXt. ++ ++ num_text - number of comments (same as ++ num_comments; you can put NULL here ++ to avoid the duplication) ++ ++ Note while png_set_text() will accept text, language, ++ and translated keywords that can be NULL pointers, the ++ structure returned by png_get_text will always contain ++ regular zero-terminated C strings. They might be ++ empty strings but they will never be NULL pointers. ++ ++ num_spalettes = png_get_sPLT(png_ptr, info_ptr, ++ &palette_ptr); ++ ++ num_spalettes - number of sPLT chunks read. ++ ++ palette_ptr - array of palette structures holding ++ contents of one or more sPLT chunks ++ read. ++ ++ png_get_oFFs(png_ptr, info_ptr, &offset_x, &offset_y, ++ &unit_type); ++ ++ offset_x - positive offset from the left edge ++ of the screen (can be negative) ++ ++ offset_y - positive offset from the top edge ++ of the screen (can be negative) ++ ++ unit_type - PNG_OFFSET_PIXEL, PNG_OFFSET_MICROMETER ++ ++ png_get_pHYs(png_ptr, info_ptr, &res_x, &res_y, ++ &unit_type); ++ ++ res_x - pixels/unit physical resolution in ++ x direction ++ ++ res_y - pixels/unit physical resolution in ++ x direction ++ ++ unit_type - PNG_RESOLUTION_UNKNOWN, ++ PNG_RESOLUTION_METER ++ ++ png_get_sCAL(png_ptr, info_ptr, &unit, &width, ++ &height) ++ ++ unit - physical scale units (an integer) ++ ++ width - width of a pixel in physical scale units ++ ++ height - height of a pixel in physical scale units ++ (width and height are doubles) ++ ++ png_get_sCAL_s(png_ptr, info_ptr, &unit, &width, ++ &height) ++ ++ unit - physical scale units (an integer) ++ ++ width - width of a pixel in physical scale units ++ (expressed as a string) ++ ++ height - height of a pixel in physical scale units ++ (width and height are strings like "2.54") ++ ++ num_unknown_chunks = png_get_unknown_chunks(png_ptr, ++ info_ptr, &unknowns) ++ ++ unknowns - array of png_unknown_chunk ++ structures holding unknown chunks ++ ++ unknowns[i].name - name of unknown chunk ++ ++ unknowns[i].data - data of unknown chunk ++ ++ unknowns[i].size - size of unknown chunk's data ++ ++ unknowns[i].location - position of chunk in file ++ ++ The value of "i" corresponds to the order in which the ++ chunks were read from the PNG file or inserted with the ++ png_set_unknown_chunks() function. ++ ++ The value of "location" is a bitwise "or" of ++ ++ PNG_HAVE_IHDR (0x01) ++ PNG_HAVE_PLTE (0x02) ++ PNG_AFTER_IDAT (0x08) ++ ++The data from the pHYs chunk can be retrieved in several convenient ++forms: ++ ++ res_x = png_get_x_pixels_per_meter(png_ptr, ++ info_ptr) ++ ++ res_y = png_get_y_pixels_per_meter(png_ptr, ++ info_ptr) ++ ++ res_x_and_y = png_get_pixels_per_meter(png_ptr, ++ info_ptr) ++ ++ res_x = png_get_x_pixels_per_inch(png_ptr, ++ info_ptr) ++ ++ res_y = png_get_y_pixels_per_inch(png_ptr, ++ info_ptr) ++ ++ res_x_and_y = png_get_pixels_per_inch(png_ptr, ++ info_ptr) ++ ++ aspect_ratio = png_get_pixel_aspect_ratio(png_ptr, ++ info_ptr) ++ ++ Each of these returns 0 [signifying "unknown"] if ++ the data is not present or if res_x is 0; ++ res_x_and_y is 0 if res_x != res_y ++ ++ Note that because of the way the resolutions are ++ stored internally, the inch conversions won't ++ come out to exactly even number. For example, ++ 72 dpi is stored as 0.28346 pixels/meter, and ++ when this is retrieved it is 71.9988 dpi, so ++ be sure to round the returned value appropriately ++ if you want to display a reasonable-looking result. ++ ++The data from the oFFs chunk can be retrieved in several convenient ++forms: ++ ++ x_offset = png_get_x_offset_microns(png_ptr, info_ptr); ++ ++ y_offset = png_get_y_offset_microns(png_ptr, info_ptr); ++ ++ x_offset = png_get_x_offset_inches(png_ptr, info_ptr); ++ ++ y_offset = png_get_y_offset_inches(png_ptr, info_ptr); ++ ++ Each of these returns 0 [signifying "unknown" if both ++ x and y are 0] if the data is not present or if the ++ chunk is present but the unit is the pixel. The ++ remark about inexact inch conversions applies here ++ as well, because a value in inches can't always be ++ converted to microns and back without some loss ++ of precision. ++ ++For more information, see the ++PNG specification for chunk contents. Be careful with trusting ++rowbytes, as some of the transformations could increase the space ++needed to hold a row (expand, filler, gray_to_rgb, etc.). ++See png_read_update_info(), below. ++ ++A quick word about text_ptr and num_text. PNG stores comments in ++keyword/text pairs, one pair per chunk, with no limit on the number ++of text chunks, and a 2^31 byte limit on their size. While there are ++suggested keywords, there is no requirement to restrict the use to these ++strings. It is strongly suggested that keywords and text be sensible ++to humans (that's the point), so don't use abbreviations. Non-printing ++symbols are not allowed. See the PNG specification for more details. ++There is also no requirement to have text after the keyword. ++ ++Keywords should be limited to 79 Latin-1 characters without leading or ++trailing spaces, but non-consecutive spaces are allowed within the ++keyword. It is possible to have the same keyword any number of times. ++The text_ptr is an array of png_text structures, each holding a ++pointer to a language string, a pointer to a keyword and a pointer to ++a text string. The text string, language code, and translated ++keyword may be empty or NULL pointers. The keyword/text ++pairs are put into the array in the order that they are received. ++However, some or all of the text chunks may be after the image, so, to ++make sure you have read all the text chunks, don't mess with these ++until after you read the stuff after the image. This will be ++mentioned again below in the discussion that goes with png_read_end(). ++ ++Input transformations ++ ++After you've read the header information, you can set up the library ++to handle any special transformations of the image data. The various ++ways to transform the data will be described in the order that they ++should occur. This is important, as some of these change the color ++type and/or bit depth of the data, and some others only work on ++certain color types and bit depths. ++ ++Transformations you request are ignored if they don't have any meaning for a ++particular input data format. However some transformations can have an effect ++as a result of a previous transformation. If you specify a contradictory set of ++transformations, for example both adding and removing the alpha channel, you ++cannot predict the final result. ++ ++The color used for the transparency values should be supplied in the same ++format/depth as the current image data. It is stored in the same format/depth ++as the image data in a tRNS chunk, so this is what libpng expects for this data. ++ ++The color used for the background value depends on the need_expand argument as ++described below. ++ ++Data will be decoded into the supplied row buffers packed into bytes ++unless the library has been told to transform it into another format. ++For example, 4 bit/pixel paletted or grayscale data will be returned ++2 pixels/byte with the leftmost pixel in the high-order bits of the byte, ++unless png_set_packing() is called. 8-bit RGB data will be stored ++in RGB RGB RGB format unless png_set_filler() or png_set_add_alpha() ++is called to insert filler bytes, either before or after each RGB triplet. ++ ++16-bit RGB data will be returned RRGGBB RRGGBB, with the most significant ++byte of the color value first, unless png_set_scale_16() is called to ++transform it to regular RGB RGB triplets, or png_set_filler() or ++png_set_add alpha() is called to insert two filler bytes, either before ++or after each RRGGBB triplet. Similarly, 8-bit or 16-bit grayscale data can ++be modified with png_set_filler(), png_set_add_alpha(), png_set_strip_16(), ++or png_set_scale_16(). ++ ++The following code transforms grayscale images of less than 8 to 8 bits, ++changes paletted images to RGB, and adds a full alpha channel if there is ++transparency information in a tRNS chunk. This is most useful on ++grayscale images with bit depths of 2 or 4 or if there is a multiple-image ++viewing application that wishes to treat all images in the same way. ++ ++ if (color_type == PNG_COLOR_TYPE_PALETTE) ++ png_set_palette_to_rgb(png_ptr); ++ ++ if (png_get_valid(png_ptr, info_ptr, ++ PNG_INFO_tRNS)) png_set_tRNS_to_alpha(png_ptr); ++ ++ if (color_type == PNG_COLOR_TYPE_GRAY && ++ bit_depth < 8) png_set_expand_gray_1_2_4_to_8(png_ptr); ++ ++The first two functions are actually aliases for png_set_expand(), added ++in libpng version 1.0.4, with the function names expanded to improve code ++readability. In some future version they may actually do different ++things. ++ ++As of libpng version 1.2.9, png_set_expand_gray_1_2_4_to_8() was ++added. It expands the sample depth without changing tRNS to alpha. ++ ++As of libpng version 1.5.2, png_set_expand_16() was added. It behaves as ++png_set_expand(); however, the resultant channels have 16 bits rather than 8. ++Use this when the output color or gray channels are made linear to avoid fairly ++severe accuracy loss. ++ ++ if (bit_depth < 16) ++ png_set_expand_16(png_ptr); ++ ++PNG can have files with 16 bits per channel. If you only can handle ++8 bits per channel, this will strip the pixels down to 8-bit. ++ ++ if (bit_depth == 16) ++#if PNG_LIBPNG_VER >= 10504 ++ png_set_scale_16(png_ptr); ++#else ++ png_set_strip_16(png_ptr); ++#endif ++ ++(The more accurate "png_set_scale_16()" API became available in libpng version ++1.5.4). ++ ++If you need to process the alpha channel on the image separately from the image ++data (for example if you convert it to a bitmap mask) it is possible to have ++libpng strip the channel leaving just RGB or gray data: ++ ++ if (color_type & PNG_COLOR_MASK_ALPHA) ++ png_set_strip_alpha(png_ptr); ++ ++If you strip the alpha channel you need to find some other way of dealing with ++the information. If, instead, you want to convert the image to an opaque ++version with no alpha channel use png_set_background; see below. ++ ++As of libpng version 1.5.2, almost all useful expansions are supported, the ++major ommissions are conversion of grayscale to indexed images (which can be ++done trivially in the application) and conversion of indexed to grayscale (which ++can be done by a trivial manipulation of the palette.) ++ ++In the following table, the 01 means grayscale with depth<8, 31 means ++indexed with depth<8, other numerals represent the color type, "T" means ++the tRNS chunk is present, A means an alpha channel is present, and O ++means tRNS or alpha is present but all pixels in the image are opaque. ++ ++ FROM 01 31 0 0T 0O 2 2T 2O 3 3T 3O 4A 4O 6A 6O ++ TO ++ 01 - [G] - - - - - - - - - - - - - ++ 31 [Q] Q [Q] [Q] [Q] Q Q Q Q Q Q [Q] [Q] Q Q ++ 0 1 G + . . G G G G G G B B GB GB ++ 0T lt Gt t + . Gt G G Gt G G Bt Bt GBt GBt ++ 0O lt Gt t . + Gt Gt G Gt Gt G Bt Bt GBt GBt ++ 2 C P C C C + . . C - - CB CB B B ++ 2T Ct - Ct C C t + t - - - CBt CBt Bt Bt ++ 2O Ct - Ct C C t t + - - - CBt CBt Bt Bt ++ 3 [Q] p [Q] [Q] [Q] Q Q Q + . . [Q] [Q] Q Q ++ 3T [Qt] p [Qt][Q] [Q] Qt Qt Qt t + t [Qt][Qt] Qt Qt ++ 3O [Qt] p [Qt][Q] [Q] Qt Qt Qt t t + [Qt][Qt] Qt Qt ++ 4A lA G A T T GA GT GT GA GT GT + BA G GBA ++ 4O lA GBA A T T GA GT GT GA GT GT BA + GBA G ++ 6A CA PA CA C C A T tT PA P P C CBA + BA ++ 6O CA PBA CA C C A tT T PA P P CBA C BA + ++ ++Within the matrix, ++ "+" identifies entries where 'from' and 'to' are the same. ++ "-" means the transformation is not supported. ++ "." means nothing is necessary (a tRNS chunk can just be ignored). ++ "t" means the transformation is obtained by png_set_tRNS. ++ "A" means the transformation is obtained by png_set_add_alpha(). ++ "X" means the transformation is obtained by png_set_expand(). ++ "1" means the transformation is obtained by ++ png_set_expand_gray_1_2_4_to_8() (and by png_set_expand() ++ if there is no transparency in the original or the final ++ format). ++ "C" means the transformation is obtained by png_set_gray_to_rgb(). ++ "G" means the transformation is obtained by png_set_rgb_to_gray(). ++ "P" means the transformation is obtained by ++ png_set_expand_palette_to_rgb(). ++ "p" means the transformation is obtained by png_set_packing(). ++ "Q" means the transformation is obtained by png_set_quantize(). ++ "T" means the transformation is obtained by ++ png_set_tRNS_to_alpha(). ++ "B" means the transformation is obtained by ++ png_set_background(), or png_strip_alpha(). ++ ++When an entry has multiple transforms listed all are required to cause the ++right overall transformation. When two transforms are separated by a comma ++either will do the job. When transforms are enclosed in [] the transform should ++do the job but this is currently unimplemented - a different format will result ++if the suggested transformations are used. ++ ++In PNG files, the alpha channel in an image ++is the level of opacity. If you need the alpha channel in an image to ++be the level of transparency instead of opacity, you can invert the ++alpha channel (or the tRNS chunk data) after it's read, so that 0 is ++fully opaque and 255 (in 8-bit or paletted images) or 65535 (in 16-bit ++images) is fully transparent, with ++ ++ png_set_invert_alpha(png_ptr); ++ ++PNG files pack pixels of bit depths 1, 2, and 4 into bytes as small as ++they can, resulting in, for example, 8 pixels per byte for 1 bit ++files. This code expands to 1 pixel per byte without changing the ++values of the pixels: ++ ++ if (bit_depth < 8) ++ png_set_packing(png_ptr); ++ ++PNG files have possible bit depths of 1, 2, 4, 8, and 16. All pixels ++stored in a PNG image have been "scaled" or "shifted" up to the next ++higher possible bit depth (e.g. from 5 bits/sample in the range [0,31] ++to 8 bits/sample in the range [0, 255]). However, it is also possible ++to convert the PNG pixel data back to the original bit depth of the ++image. This call reduces the pixels back down to the original bit depth: ++ ++ png_color_8p sig_bit; ++ ++ if (png_get_sBIT(png_ptr, info_ptr, &sig_bit)) ++ png_set_shift(png_ptr, sig_bit); ++ ++PNG files store 3-color pixels in red, green, blue order. This code ++changes the storage of the pixels to blue, green, red: ++ ++ if (color_type == PNG_COLOR_TYPE_RGB || ++ color_type == PNG_COLOR_TYPE_RGB_ALPHA) ++ png_set_bgr(png_ptr); ++ ++PNG files store RGB pixels packed into 3 or 6 bytes. This code expands them ++into 4 or 8 bytes for windowing systems that need them in this format: ++ ++ if (color_type == PNG_COLOR_TYPE_RGB) ++ png_set_filler(png_ptr, filler, PNG_FILLER_BEFORE); ++ ++where "filler" is the 8-bit or 16-bit number to fill with, and the location ++is either PNG_FILLER_BEFORE or PNG_FILLER_AFTER, depending upon whether ++you want the filler before the RGB or after. When filling an 8-bit pixel, ++the least significant 8 bits of the number are used, if a 16-bit number is ++supplied. This transformation does not affect images that already have full ++alpha channels. To add an opaque alpha channel, use filler=0xffff and ++PNG_FILLER_AFTER which will generate RGBA pixels. ++ ++Note that png_set_filler() does not change the color type. If you want ++to do that, you can add a true alpha channel with ++ ++ if (color_type == PNG_COLOR_TYPE_RGB || ++ color_type == PNG_COLOR_TYPE_GRAY) ++ png_set_add_alpha(png_ptr, filler, PNG_FILLER_AFTER); ++ ++where "filler" contains the alpha value to assign to each pixel. ++The png_set_add_alpha() function was added in libpng-1.2.7. ++ ++If you are reading an image with an alpha channel, and you need the ++data as ARGB instead of the normal PNG format RGBA: ++ ++ if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) ++ png_set_swap_alpha(png_ptr); ++ ++For some uses, you may want a grayscale image to be represented as ++RGB. This code will do that conversion: ++ ++ if (color_type == PNG_COLOR_TYPE_GRAY || ++ color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ++ png_set_gray_to_rgb(png_ptr); ++ ++Conversely, you can convert an RGB or RGBA image to grayscale or grayscale ++with alpha. ++ ++ if (color_type == PNG_COLOR_TYPE_RGB || ++ color_type == PNG_COLOR_TYPE_RGB_ALPHA) ++ png_set_rgb_to_gray(png_ptr, error_action, ++ double red_weight, double green_weight); ++ ++ error_action = 1: silently do the conversion ++ ++ error_action = 2: issue a warning if the original ++ image has any pixel where ++ red != green or red != blue ++ ++ error_action = 3: issue an error and abort the ++ conversion if the original ++ image has any pixel where ++ red != green or red != blue ++ ++ red_weight: weight of red component ++ ++ green_weight: weight of green component ++ If either weight is negative, default ++ weights are used. ++ ++In the corresponding fixed point API the red_weight and green_weight values are ++simply scaled by 100,000: ++ ++ png_set_rgb_to_gray(png_ptr, error_action, ++ png_fixed_point red_weight, ++ png_fixed_point green_weight); ++ ++If you have set error_action = 1 or 2, you can ++later check whether the image really was gray, after processing ++the image rows, with the png_get_rgb_to_gray_status(png_ptr) function. ++It will return a png_byte that is zero if the image was gray or ++1 if there were any non-gray pixels. Background and sBIT data ++will be silently converted to grayscale, using the green channel ++data for sBIT, regardless of the error_action setting. ++ ++The default values come from the PNG file cHRM chunk if present; otherwise, the ++defaults correspond to the ITU-R recommendation 709, and also the sRGB color ++space, as recommended in the Charles Poynton's Colour FAQ, ++Copyright (c) 2006-11-28 Charles Poynton, in section 9: ++ ++ ++ ++ Y = 0.2126 * R + 0.7152 * G + 0.0722 * B ++ ++Previous versions of this document, 1998 through 2002, recommended a slightly ++different formula: ++ ++ Y = 0.212671 * R + 0.715160 * G + 0.072169 * B ++ ++Libpng uses an integer approximation: ++ ++ Y = (6968 * R + 23434 * G + 2366 * B)/32768 ++ ++The calculation is done in a linear colorspace, if the image gamma ++can be determined. ++ ++The png_set_background() function has been described already; it tells libpng to ++composite images with alpha or simple transparency against the supplied ++background color. For compatibility with versions of libpng earlier than ++libpng-1.5.4 it is recommended that you call the function after reading the file ++header, even if you don't want to use the color in a bKGD chunk, if one exists. ++ ++If the PNG file contains a bKGD chunk (PNG_INFO_bKGD valid), ++you may use this color, or supply another color more suitable for ++the current display (e.g., the background color from a web page). You ++need to tell libpng how the color is represented, both the format of the ++component values in the color (the number of bits) and the gamma encoding of the ++color. The function takes two arguments, background_gamma_mode and need_expand ++to convey this information; however, only two combinations are likely to be ++useful: ++ ++ png_color_16 my_background; ++ png_color_16p image_background; ++ ++ if (png_get_bKGD(png_ptr, info_ptr, &image_background)) ++ png_set_background(png_ptr, image_background, ++ PNG_BACKGROUND_GAMMA_FILE, 1/*needs to be expanded*/, 1); ++ else ++ png_set_background(png_ptr, &my_background, ++ PNG_BACKGROUND_GAMMA_SCREEN, 0/*do not expand*/, 1); ++ ++The second call was described above - my_background is in the format of the ++final, display, output produced by libpng. Because you now know the format of ++the PNG it is possible to avoid the need to choose either 8-bit or 16-bit ++output and to retain palette images (the palette colors will be modified ++appropriately and the tRNS chunk removed.) However, if you are doing this, ++take great care not to ask for transformations without checking first that ++they apply! ++ ++In the first call the background color has the original bit depth and color type ++of the PNG file. So, for palette images the color is supplied as a palette ++index and for low bit greyscale images the color is a reduced bit value in ++image_background->gray. ++ ++If you didn't call png_set_gamma() before reading the file header, for example ++if you need your code to remain compatible with older versions of libpng prior ++to libpng-1.5.4, this is the place to call it. ++ ++Do not call it if you called png_set_alpha_mode(); doing so will damage the ++settings put in place by png_set_alpha_mode(). (If png_set_alpha_mode() is ++supported then you can certainly do png_set_gamma() before reading the PNG ++header.) ++ ++This API unconditionally sets the screen and file gamma values, so it will ++override the value in the PNG file unless it is called before the PNG file ++reading starts. For this reason you must always call it with the PNG file ++value when you call it in this position: ++ ++ if (png_get_gAMA(png_ptr, info_ptr, &file_gamma)) ++ png_set_gamma(png_ptr, screen_gamma, file_gamma); ++ ++ else ++ png_set_gamma(png_ptr, screen_gamma, 0.45455); ++ ++If you need to reduce an RGB file to a paletted file, or if a paletted ++file has more entries than will fit on your screen, png_set_quantize() ++will do that. Note that this is a simple match quantization that merely ++finds the closest color available. This should work fairly well with ++optimized palettes, but fairly badly with linear color cubes. If you ++pass a palette that is larger than maximum_colors, the file will ++reduce the number of colors in the palette so it will fit into ++maximum_colors. If there is a histogram, libpng will use it to make ++more intelligent choices when reducing the palette. If there is no ++histogram, it may not do as good a job. ++ ++ if (color_type & PNG_COLOR_MASK_COLOR) ++ { ++ if (png_get_valid(png_ptr, info_ptr, ++ PNG_INFO_PLTE)) ++ { ++ png_uint_16p histogram = NULL; ++ ++ png_get_hIST(png_ptr, info_ptr, ++ &histogram); ++ png_set_quantize(png_ptr, palette, num_palette, ++ max_screen_colors, histogram, 1); ++ } ++ ++ else ++ { ++ png_color std_color_cube[MAX_SCREEN_COLORS] = ++ { ... colors ... }; ++ ++ png_set_quantize(png_ptr, std_color_cube, ++ MAX_SCREEN_COLORS, MAX_SCREEN_COLORS, ++ NULL,0); ++ } ++ } ++ ++PNG files describe monochrome as black being zero and white being one. ++The following code will reverse this (make black be one and white be ++zero): ++ ++ if (bit_depth == 1 && color_type == PNG_COLOR_TYPE_GRAY) ++ png_set_invert_mono(png_ptr); ++ ++This function can also be used to invert grayscale and gray-alpha images: ++ ++ if (color_type == PNG_COLOR_TYPE_GRAY || ++ color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ++ png_set_invert_mono(png_ptr); ++ ++PNG files store 16-bit pixels in network byte order (big-endian, ++ie. most significant bits first). This code changes the storage to the ++other way (little-endian, i.e. least significant bits first, the ++way PCs store them): ++ ++ if (bit_depth == 16) ++ png_set_swap(png_ptr); ++ ++If you are using packed-pixel images (1, 2, or 4 bits/pixel), and you ++need to change the order the pixels are packed into bytes, you can use: ++ ++ if (bit_depth < 8) ++ png_set_packswap(png_ptr); ++ ++Finally, you can write your own transformation function if none of ++the existing ones meets your needs. This is done by setting a callback ++with ++ ++ png_set_read_user_transform_fn(png_ptr, ++ read_transform_fn); ++ ++You must supply the function ++ ++ void read_transform_fn(png_structp png_ptr, png_row_infop ++ row_info, png_bytep data) ++ ++See pngtest.c for a working example. Your function will be called ++after all of the other transformations have been processed. Take care with ++interlaced images if you do the interlace yourself - the width of the row is the ++width in 'row_info', not the overall image width. ++ ++If supported, libpng provides two information routines that you can use to find ++where you are in processing the image: ++ ++ png_get_current_pass_number(png_structp png_ptr); ++ png_get_current_row_number(png_structp png_ptr); ++ ++Don't try using these outside a transform callback - firstly they are only ++supported if user transforms are supported, secondly they may well return ++unexpected results unless the row is actually being processed at the moment they ++are called. ++ ++With interlaced ++images the value returned is the row in the input sub-image image. Use ++PNG_ROW_FROM_PASS_ROW(row, pass) and PNG_COL_FROM_PASS_COL(col, pass) to ++find the output pixel (x,y) given an interlaced sub-image pixel (row,col,pass). ++ ++The discussion of interlace handling above contains more information on how to ++use these values. ++ ++You can also set up a pointer to a user structure for use by your ++callback function, and you can inform libpng that your transform ++function will change the number of channels or bit depth with the ++function ++ ++ png_set_user_transform_info(png_ptr, user_ptr, ++ user_depth, user_channels); ++ ++The user's application, not libpng, is responsible for allocating and ++freeing any memory required for the user structure. ++ ++You can retrieve the pointer via the function ++png_get_user_transform_ptr(). For example: ++ ++ voidp read_user_transform_ptr = ++ png_get_user_transform_ptr(png_ptr); ++ ++The last thing to handle is interlacing; this is covered in detail below, ++but you must call the function here if you want libpng to handle expansion ++of the interlaced image. ++ ++ number_of_passes = png_set_interlace_handling(png_ptr); ++ ++After setting the transformations, libpng can update your png_info ++structure to reflect any transformations you've requested with this ++call. ++ ++ png_read_update_info(png_ptr, info_ptr); ++ ++This is most useful to update the info structure's rowbytes ++field so you can use it to allocate your image memory. This function ++will also update your palette with the correct screen_gamma and ++background if these have been given with the calls above. You may ++only call png_read_update_info() once with a particular info_ptr. ++ ++After you call png_read_update_info(), you can allocate any ++memory you need to hold the image. The row data is simply ++raw byte data for all forms of images. As the actual allocation ++varies among applications, no example will be given. If you ++are allocating one large chunk, you will need to build an ++array of pointers to each row, as it will be needed for some ++of the functions below. ++ ++Be sure that your platform can allocate the buffer that you'll need. ++libpng internally checks for oversize width, but you'll need to ++do your own check for number_of_rows*width*pixel_size if you are using ++a multiple-row buffer: ++ ++ /* Guard against integer overflow */ ++ if (number_of_rows > PNG_SIZE_MAX/(width*pixel_size)) { ++ png_error(png_ptr,"image_data buffer would be too large"); ++ } ++ ++Remember: Before you call png_read_update_info(), the png_get_*() ++functions return the values corresponding to the original PNG image. ++After you call png_read_update_info the values refer to the image ++that libpng will output. Consequently you must call all the png_set_ ++functions before you call png_read_update_info(). This is particularly ++important for png_set_interlace_handling() - if you are going to call ++png_read_update_info() you must call png_set_interlace_handling() before ++it unless you want to receive interlaced output. ++ ++Reading image data ++ ++After you've allocated memory, you can read the image data. ++The simplest way to do this is in one function call. If you are ++allocating enough memory to hold the whole image, you can just ++call png_read_image() and libpng will read in all the image data ++and put it in the memory area supplied. You will need to pass in ++an array of pointers to each row. ++ ++This function automatically handles interlacing, so you don't ++need to call png_set_interlace_handling() (unless you call ++png_read_update_info()) or call this function multiple times, or any ++of that other stuff necessary with png_read_rows(). ++ ++ png_read_image(png_ptr, row_pointers); ++ ++where row_pointers is: ++ ++ png_bytep row_pointers[height]; ++ ++You can point to void or char or whatever you use for pixels. ++ ++If you don't want to read in the whole image at once, you can ++use png_read_rows() instead. If there is no interlacing (check ++interlace_type == PNG_INTERLACE_NONE), this is simple: ++ ++ png_read_rows(png_ptr, row_pointers, NULL, ++ number_of_rows); ++ ++where row_pointers is the same as in the png_read_image() call. ++ ++If you are doing this just one row at a time, you can do this with ++a single row_pointer instead of an array of row_pointers: ++ ++ png_bytep row_pointer = row; ++ png_read_row(png_ptr, row_pointer, NULL); ++ ++If the file is interlaced (interlace_type != 0 in the IHDR chunk), things ++get somewhat harder. The only current (PNG Specification version 1.2) ++interlacing type for PNG is (interlace_type == PNG_INTERLACE_ADAM7); ++a somewhat complicated 2D interlace scheme, known as Adam7, that ++breaks down an image into seven smaller images of varying size, based ++on an 8x8 grid. This number is defined (from libpng 1.5) as ++PNG_INTERLACE_ADAM7_PASSES in png.h ++ ++libpng can fill out those images or it can give them to you "as is". ++It is almost always better to have libpng handle the interlacing for you. ++If you want the images filled out, there are two ways to do that. The one ++mentioned in the PNG specification is to expand each pixel to cover ++those pixels that have not been read yet (the "rectangle" method). ++This results in a blocky image for the first pass, which gradually ++smooths out as more pixels are read. The other method is the "sparkle" ++method, where pixels are drawn only in their final locations, with the ++rest of the image remaining whatever colors they were initialized to ++before the start of the read. The first method usually looks better, ++but tends to be slower, as there are more pixels to put in the rows. ++ ++If, as is likely, you want libpng to expand the images, call this before ++calling png_start_read_image() or png_read_update_info(): ++ ++ if (interlace_type == PNG_INTERLACE_ADAM7) ++ number_of_passes ++ = png_set_interlace_handling(png_ptr); ++ ++This will return the number of passes needed. Currently, this is seven, ++but may change if another interlace type is added. This function can be ++called even if the file is not interlaced, where it will return one pass. ++You then need to read the whole image 'number_of_passes' times. Each time ++will distribute the pixels from the current pass to the correct place in ++the output image, so you need to supply the same rows to png_read_rows in ++each pass. ++ ++If you are not going to display the image after each pass, but are ++going to wait until the entire image is read in, use the sparkle ++effect. This effect is faster and the end result of either method ++is exactly the same. If you are planning on displaying the image ++after each pass, the "rectangle" effect is generally considered the ++better looking one. ++ ++If you only want the "sparkle" effect, just call png_read_row() or ++png_read_rows() as ++normal, with the third parameter NULL. Make sure you make pass over ++the image number_of_passes times, and you don't change the data in the ++rows between calls. You can change the locations of the data, just ++not the data. Each pass only writes the pixels appropriate for that ++pass, and assumes the data from previous passes is still valid. ++ ++ png_read_rows(png_ptr, row_pointers, NULL, ++ number_of_rows); ++ or ++ png_read_row(png_ptr, row_pointers, NULL); ++ ++If you only want the first effect (the rectangles), do the same as ++before except pass the row buffer in the third parameter, and leave ++the second parameter NULL. ++ ++ png_read_rows(png_ptr, NULL, row_pointers, ++ number_of_rows); ++ or ++ png_read_row(png_ptr, NULL, row_pointers); ++ ++If you don't want libpng to handle the interlacing details, just call ++png_read_rows() PNG_INTERLACE_ADAM7_PASSES times to read in all the images. ++Each of the images is a valid image by itself; however, you will almost ++certainly need to distribute the pixels from each sub-image to the ++correct place. This is where everything gets very tricky. ++ ++If you want to retrieve the separate images you must pass the correct ++number of rows to each successive call of png_read_rows(). The calculation ++gets pretty complicated for small images, where some sub-images may ++not even exist because either their width or height ends up zero. ++libpng provides two macros to help you in 1.5 and later versions: ++ ++ png_uint_32 width = PNG_PASS_COLS(image_width, pass_number); ++ png_uint_32 height = PNG_PASS_ROWS(image_height, pass_number); ++ ++Respectively these tell you the width and height of the sub-image ++corresponding to the numbered pass. 'pass' is in in the range 0 to 6 - ++this can be confusing because the specification refers to the same passes ++as 1 to 7! Be careful, you must check both the width and height before ++calling png_read_rows() and not call it for that pass if either is zero. ++ ++You can, of course, read each sub-image row by row. If you want to ++produce optimal code to make a pixel-by-pixel transformation of an ++interlaced image this is the best approach; read each row of each pass, ++transform it, and write it out to a new interlaced image. ++ ++If you want to de-interlace the image yourself libpng provides further ++macros to help that tell you where to place the pixels in the output image. ++Because the interlacing scheme is rectangular - sub-image pixels are always ++arranged on a rectangular grid - all you need to know for each pass is the ++starting column and row in the output image of the first pixel plus the ++spacing between each pixel. As of libpng 1.5 there are four macros to ++retrieve this information: ++ ++ png_uint_32 x = PNG_PASS_START_COL(pass); ++ png_uint_32 y = PNG_PASS_START_ROW(pass); ++ png_uint_32 xStep = 1U << PNG_PASS_COL_SHIFT(pass); ++ png_uint_32 yStep = 1U << PNG_PASS_ROW_SHIFT(pass); ++ ++These allow you to write the obvious loop: ++ ++ png_uint_32 input_y = 0; ++ png_uint_32 output_y = PNG_PASS_START_ROW(pass); ++ ++ while (output_y < output_image_height) ++ { ++ png_uint_32 input_x = 0; ++ png_uint_32 output_x = PNG_PASS_START_COL(pass); ++ ++ while (output_x < output_image_width) ++ { ++ image[output_y][output_x] = ++ subimage[pass][input_y][input_x++]; ++ ++ output_x += xStep; ++ } ++ ++ ++input_y; ++ output_y += yStep; ++ } ++ ++Notice that the steps between successive output rows and columns are ++returned as shifts. This is possible because the pixels in the subimages ++are always a power of 2 apart - 1, 2, 4 or 8 pixels - in the original ++image. In practice you may need to directly calculate the output coordinate ++given an input coordinate. libpng provides two further macros for this ++purpose: ++ ++ png_uint_32 output_x = PNG_COL_FROM_PASS_COL(input_x, pass); ++ png_uint_32 output_y = PNG_ROW_FROM_PASS_ROW(input_y, pass); ++ ++Finally a pair of macros are provided to tell you if a particular image ++row or column appears in a given pass: ++ ++ int col_in_pass = PNG_COL_IN_INTERLACE_PASS(output_x, pass); ++ int row_in_pass = PNG_ROW_IN_INTERLACE_PASS(output_y, pass); ++ ++Bear in mind that you will probably also need to check the width and height ++of the pass in addition to the above to be sure the pass even exists! ++ ++With any luck you are convinced by now that you don't want to do your own ++interlace handling. In reality normally the only good reason for doing this ++is if you are processing PNG files on a pixel-by-pixel basis and don't want ++to load the whole file into memory when it is interlaced. ++ ++libpng includes a test program, pngvalid, that illustrates reading and ++writing of interlaced images. If you can't get interlacing to work in your ++code and don't want to leave it to libpng (the recommended approach), see ++how pngvalid.c does it. ++ ++Finishing a sequential read ++ ++After you are finished reading the image through the ++low-level interface, you can finish reading the file. ++ ++If you want to use a different crc action for handling CRC errors in ++chunks after the image data, you can call png_set_crc_action() ++again at this point. ++ ++If you are interested in comments or time, which may be stored either ++before or after the image data, you should pass the separate png_info ++struct if you want to keep the comments from before and after the image ++separate. ++ ++ png_infop end_info = png_create_info_struct(png_ptr); ++ ++ if (!end_info) ++ { ++ png_destroy_read_struct(&png_ptr, &info_ptr, ++ (png_infopp)NULL); ++ return ERROR; ++ } ++ ++ png_read_end(png_ptr, end_info); ++ ++If you are not interested, you should still call png_read_end() ++but you can pass NULL, avoiding the need to create an end_info structure. ++If you do this, libpng will not process any chunks after IDAT other than ++skipping over them and perhaps (depending on whether you have called ++png_set_crc_action) checking their CRCs while looking for the IEND chunk. ++ ++ png_read_end(png_ptr, (png_infop)NULL); ++ ++If you don't call png_read_end(), then your file pointer will be ++left pointing to the first chunk after the last IDAT, which is probably ++not what you want if you expect to read something beyond the end of ++the PNG datastream. ++ ++When you are done, you can free all memory allocated by libpng like this: ++ ++ png_destroy_read_struct(&png_ptr, &info_ptr, ++ &end_info); ++ ++or, if you didn't create an end_info structure, ++ ++ png_destroy_read_struct(&png_ptr, &info_ptr, ++ (png_infopp)NULL); ++ ++It is also possible to individually free the info_ptr members that ++point to libpng-allocated storage with the following function: ++ ++ png_free_data(png_ptr, info_ptr, mask, seq) ++ ++ mask - identifies data to be freed, a mask ++ containing the bitwise OR of one or ++ more of ++ PNG_FREE_PLTE, PNG_FREE_TRNS, ++ PNG_FREE_HIST, PNG_FREE_ICCP, ++ PNG_FREE_PCAL, PNG_FREE_ROWS, ++ PNG_FREE_SCAL, PNG_FREE_SPLT, ++ PNG_FREE_TEXT, PNG_FREE_UNKN, ++ or simply PNG_FREE_ALL ++ ++ seq - sequence number of item to be freed ++ (-1 for all items) ++ ++This function may be safely called when the relevant storage has ++already been freed, or has not yet been allocated, or was allocated ++by the user and not by libpng, and will in those cases do nothing. ++The "seq" parameter is ignored if only one item of the selected data ++type, such as PLTE, is allowed. If "seq" is not -1, and multiple items ++are allowed for the data type identified in the mask, such as text or ++sPLT, only the n'th item in the structure is freed, where n is "seq". ++ ++The default behavior is only to free data that was allocated internally ++by libpng. This can be changed, so that libpng will not free the data, ++or so that it will free data that was allocated by the user with png_malloc() ++or png_calloc() and passed in via a png_set_*() function, with ++ ++ png_data_freer(png_ptr, info_ptr, freer, mask) ++ ++ freer - one of ++ PNG_DESTROY_WILL_FREE_DATA ++ PNG_SET_WILL_FREE_DATA ++ PNG_USER_WILL_FREE_DATA ++ ++ mask - which data elements are affected ++ same choices as in png_free_data() ++ ++This function only affects data that has already been allocated. ++You can call this function after reading the PNG data but before calling ++any png_set_*() functions, to control whether the user or the png_set_*() ++function is responsible for freeing any existing data that might be present, ++and again after the png_set_*() functions to control whether the user ++or png_destroy_*() is supposed to free the data. When the user assumes ++responsibility for libpng-allocated data, the application must use ++png_free() to free it, and when the user transfers responsibility to libpng ++for data that the user has allocated, the user must have used png_malloc() ++or png_calloc() to allocate it. ++ ++If you allocated your row_pointers in a single block, as suggested above in ++the description of the high level read interface, you must not transfer ++responsibility for freeing it to the png_set_rows or png_read_destroy function, ++because they would also try to free the individual row_pointers[i]. ++ ++If you allocated text_ptr.text, text_ptr.lang, and text_ptr.translated_keyword ++separately, do not transfer responsibility for freeing text_ptr to libpng, ++because when libpng fills a png_text structure it combines these members with ++the key member, and png_free_data() will free only text_ptr.key. Similarly, ++if you transfer responsibility for free'ing text_ptr from libpng to your ++application, your application must not separately free those members. ++ ++The png_free_data() function will turn off the "valid" flag for anything ++it frees. If you need to turn the flag off for a chunk that was freed by ++your application instead of by libpng, you can use ++ ++ png_set_invalid(png_ptr, info_ptr, mask); ++ ++ mask - identifies the chunks to be made invalid, ++ containing the bitwise OR of one or ++ more of ++ PNG_INFO_gAMA, PNG_INFO_sBIT, ++ PNG_INFO_cHRM, PNG_INFO_PLTE, ++ PNG_INFO_tRNS, PNG_INFO_bKGD, ++ PNG_INFO_eXIf, ++ PNG_INFO_hIST, PNG_INFO_pHYs, ++ PNG_INFO_oFFs, PNG_INFO_tIME, ++ PNG_INFO_pCAL, PNG_INFO_sRGB, ++ PNG_INFO_iCCP, PNG_INFO_sPLT, ++ PNG_INFO_sCAL, PNG_INFO_IDAT ++ ++For a more compact example of reading a PNG image, see the file example.c. ++ ++Reading PNG files progressively ++ ++The progressive reader is slightly different from the non-progressive ++reader. Instead of calling png_read_info(), png_read_rows(), and ++png_read_end(), you make one call to png_process_data(), which calls ++callbacks when it has the info, a row, or the end of the image. You ++set up these callbacks with png_set_progressive_read_fn(). You don't ++have to worry about the input/output functions of libpng, as you are ++giving the library the data directly in png_process_data(). I will ++assume that you have read the section on reading PNG files above, ++so I will only highlight the differences (although I will show ++all of the code). ++ ++png_structp png_ptr; ++png_infop info_ptr; ++ ++ /* An example code fragment of how you would ++ initialize the progressive reader in your ++ application. */ ++ int ++ initialize_png_reader() ++ { ++ png_ptr = png_create_read_struct ++ (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr, ++ user_error_fn, user_warning_fn); ++ ++ if (!png_ptr) ++ return ERROR; ++ ++ info_ptr = png_create_info_struct(png_ptr); ++ ++ if (!info_ptr) ++ { ++ png_destroy_read_struct(&png_ptr, ++ (png_infopp)NULL, (png_infopp)NULL); ++ return ERROR; ++ } ++ ++ if (setjmp(png_jmpbuf(png_ptr))) ++ { ++ png_destroy_read_struct(&png_ptr, &info_ptr, ++ (png_infopp)NULL); ++ return ERROR; ++ } ++ ++ /* This one's new. You can provide functions ++ to be called when the header info is valid, ++ when each row is completed, and when the image ++ is finished. If you aren't using all functions, ++ you can specify NULL parameters. Even when all ++ three functions are NULL, you need to call ++ png_set_progressive_read_fn(). You can use ++ any struct as the user_ptr (cast to a void pointer ++ for the function call), and retrieve the pointer ++ from inside the callbacks using the function ++ ++ png_get_progressive_ptr(png_ptr); ++ ++ which will return a void pointer, which you have ++ to cast appropriately. ++ */ ++ png_set_progressive_read_fn(png_ptr, (void *)user_ptr, ++ info_callback, row_callback, end_callback); ++ ++ return 0; ++ } ++ ++ /* A code fragment that you call as you receive blocks ++ of data */ ++ int ++ process_data(png_bytep buffer, png_uint_32 length) ++ { ++ if (setjmp(png_jmpbuf(png_ptr))) ++ { ++ png_destroy_read_struct(&png_ptr, &info_ptr, ++ (png_infopp)NULL); ++ return ERROR; ++ } ++ ++ /* This one's new also. Simply give it a chunk ++ of data from the file stream (in order, of ++ course). On machines with segmented memory ++ models machines, don't give it any more than ++ 64K. The library seems to run fine with sizes ++ of 4K. Although you can give it much less if ++ necessary (I assume you can give it chunks of ++ 1 byte, I haven't tried less than 256 bytes ++ yet). When this function returns, you may ++ want to display any rows that were generated ++ in the row callback if you don't already do ++ so there. ++ */ ++ png_process_data(png_ptr, info_ptr, buffer, length); ++ ++ /* At this point you can call png_process_data_skip if ++ you want to handle data the library will skip yourself; ++ it simply returns the number of bytes to skip (and stops ++ libpng skipping that number of bytes on the next ++ png_process_data call). ++ return 0; ++ } ++ ++ /* This function is called (as set by ++ png_set_progressive_read_fn() above) when enough data ++ has been supplied so all of the header has been ++ read. ++ */ ++ void ++ info_callback(png_structp png_ptr, png_infop info) ++ { ++ /* Do any setup here, including setting any of ++ the transformations mentioned in the Reading ++ PNG files section. For now, you _must_ call ++ either png_start_read_image() or ++ png_read_update_info() after all the ++ transformations are set (even if you don't set ++ any). You may start getting rows before ++ png_process_data() returns, so this is your ++ last chance to prepare for that. ++ ++ This is where you turn on interlace handling, ++ assuming you don't want to do it yourself. ++ ++ If you need to you can stop the processing of ++ your original input data at this point by calling ++ png_process_data_pause. This returns the number ++ of unprocessed bytes from the last png_process_data ++ call - it is up to you to ensure that the next call ++ sees these bytes again. If you don't want to bother ++ with this you can get libpng to cache the unread ++ bytes by setting the 'save' parameter (see png.h) but ++ then libpng will have to copy the data internally. ++ */ ++ } ++ ++ /* This function is called when each row of image ++ data is complete */ ++ void ++ row_callback(png_structp png_ptr, png_bytep new_row, ++ png_uint_32 row_num, int pass) ++ { ++ /* If the image is interlaced, and you turned ++ on the interlace handler, this function will ++ be called for every row in every pass. Some ++ of these rows will not be changed from the ++ previous pass. When the row is not changed, ++ the new_row variable will be NULL. The rows ++ and passes are called in order, so you don't ++ really need the row_num and pass, but I'm ++ supplying them because it may make your life ++ easier. ++ ++ If you did not turn on interlace handling then ++ the callback is called for each row of each ++ sub-image when the image is interlaced. In this ++ case 'row_num' is the row in the sub-image, not ++ the row in the output image as it is in all other ++ cases. ++ ++ For the non-NULL rows of interlaced images when ++ you have switched on libpng interlace handling, ++ you must call png_progressive_combine_row() ++ passing in the row and the old row. You can ++ call this function for NULL rows (it will just ++ return) and for non-interlaced images (it just ++ does the memcpy for you) if it will make the ++ code easier. Thus, you can just do this for ++ all cases if you switch on interlace handling; ++ */ ++ ++ png_progressive_combine_row(png_ptr, old_row, ++ new_row); ++ ++ /* where old_row is what was displayed ++ previously for the row. Note that the first ++ pass (pass == 0, really) will completely cover ++ the old row, so the rows do not have to be ++ initialized. After the first pass (and only ++ for interlaced images), you will have to pass ++ the current row, and the function will combine ++ the old row and the new row. ++ ++ You can also call png_process_data_pause in this ++ callback - see above. ++ */ ++ } ++ ++ void ++ end_callback(png_structp png_ptr, png_infop info) ++ { ++ /* This function is called after the whole image ++ has been read, including any chunks after the ++ image (up to and including the IEND). You ++ will usually have the same info chunk as you ++ had in the header, although some data may have ++ been added to the comments and time fields. ++ ++ Most people won't do much here, perhaps setting ++ a flag that marks the image as finished. ++ */ ++ } ++ ++ ++ ++IV. Writing ++ ++Much of this is very similar to reading. However, everything of ++importance is repeated here, so you won't have to constantly look ++back up in the reading section to understand writing. ++ ++Setup ++ ++You will want to do the I/O initialization before you get into libpng, ++so if it doesn't work, you don't have anything to undo. If you are not ++using the standard I/O functions, you will need to replace them with ++custom writing functions. See the discussion under Customizing libpng. ++ ++ FILE *fp = fopen(file_name, "wb"); ++ ++ if (!fp) ++ return ERROR; ++ ++Next, png_struct and png_info need to be allocated and initialized. ++As these can be both relatively large, you may not want to store these ++on the stack, unless you have stack space to spare. Of course, you ++will want to check if they return NULL. If you are also reading, ++you won't want to name your read structure and your write structure ++both "png_ptr"; you can call them anything you like, such as ++"read_ptr" and "write_ptr". Look at pngtest.c, for example. ++ ++ png_structp png_ptr = png_create_write_struct ++ (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr, ++ user_error_fn, user_warning_fn); ++ ++ if (!png_ptr) ++ return ERROR; ++ ++ png_infop info_ptr = png_create_info_struct(png_ptr); ++ if (!info_ptr) ++ { ++ png_destroy_write_struct(&png_ptr, ++ (png_infopp)NULL); ++ return ERROR; ++ } ++ ++If you want to use your own memory allocation routines, ++define PNG_USER_MEM_SUPPORTED and use ++png_create_write_struct_2() instead of png_create_write_struct(): ++ ++ png_structp png_ptr = png_create_write_struct_2 ++ (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr, ++ user_error_fn, user_warning_fn, (png_voidp) ++ user_mem_ptr, user_malloc_fn, user_free_fn); ++ ++After you have these structures, you will need to set up the ++error handling. When libpng encounters an error, it expects to ++longjmp() back to your routine. Therefore, you will need to call ++setjmp() and pass the png_jmpbuf(png_ptr). If you ++write the file from different routines, you will need to update ++the png_jmpbuf(png_ptr) every time you enter a new routine that will ++call a png_*() function. See your documentation of setjmp/longjmp ++for your compiler for more information on setjmp/longjmp. See ++the discussion on libpng error handling in the Customizing Libpng ++section below for more information on the libpng error handling. ++ ++ if (setjmp(png_jmpbuf(png_ptr))) ++ { ++ png_destroy_write_struct(&png_ptr, &info_ptr); ++ fclose(fp); ++ return ERROR; ++ } ++ ... ++ return; ++ ++If you would rather avoid the complexity of setjmp/longjmp issues, ++you can compile libpng with PNG_NO_SETJMP, in which case ++errors will result in a call to PNG_ABORT() which defaults to abort(). ++ ++You can #define PNG_ABORT() to a function that does something ++more useful than abort(), as long as your function does not ++return. ++ ++Checking for invalid palette index on write was added at libpng ++1.5.10. If a pixel contains an invalid (out-of-range) index libpng issues ++a benign error. This is enabled by default because this condition is an ++error according to the PNG specification, Clause 11.3.2, but the error can ++be ignored in each png_ptr with ++ ++ png_set_check_for_invalid_index(png_ptr, 0); ++ ++If the error is ignored, or if png_benign_error() treats it as a warning, ++any invalid pixels are written as-is by the encoder, resulting in an ++invalid PNG datastream as output. In this case the application is ++responsible for ensuring that the pixel indexes are in range when it writes ++a PLTE chunk with fewer entries than the bit depth would allow. ++ ++Now you need to set up the output code. The default for libpng is to ++use the C function fwrite(). If you use this, you will need to pass a ++valid FILE * in the function png_init_io(). Be sure that the file is ++opened in binary mode. Again, if you wish to handle writing data in ++another way, see the discussion on libpng I/O handling in the Customizing ++Libpng section below. ++ ++ png_init_io(png_ptr, fp); ++ ++If you are embedding your PNG into a datastream such as MNG, and don't ++want libpng to write the 8-byte signature, or if you have already ++written the signature in your application, use ++ ++ png_set_sig_bytes(png_ptr, 8); ++ ++to inform libpng that it should not write a signature. ++ ++Write callbacks ++ ++At this point, you can set up a callback function that will be ++called after each row has been written, which you can use to control ++a progress meter or the like. It's demonstrated in pngtest.c. ++You must supply a function ++ ++ void write_row_callback(png_structp png_ptr, png_uint_32 row, ++ int pass); ++ { ++ /* put your code here */ ++ } ++ ++(You can give it another name that you like instead of "write_row_callback") ++ ++To inform libpng about your function, use ++ ++ png_set_write_status_fn(png_ptr, write_row_callback); ++ ++When this function is called the row has already been completely processed and ++it has also been written out. The 'row' and 'pass' refer to the next row to be ++handled. For the ++non-interlaced case the row that was just handled is simply one less than the ++passed in row number, and pass will always be 0. For the interlaced case the ++same applies unless the row value is 0, in which case the row just handled was ++the last one from one of the preceding passes. Because interlacing may skip a ++pass you cannot be sure that the preceding pass is just 'pass-1', if you really ++need to know what the last pass is record (row,pass) from the callback and use ++the last recorded value each time. ++ ++As with the user transform you can find the output row using the ++PNG_ROW_FROM_PASS_ROW macro. ++ ++You now have the option of modifying how the compression library will ++run. The following functions are mainly for testing, but may be useful ++in some cases, like if you need to write PNG files extremely fast and ++are willing to give up some compression, or if you want to get the ++maximum possible compression at the expense of slower writing. If you ++have no special needs in this area, let the library do what it wants by ++not calling this function at all, as it has been tuned to deliver a good ++speed/compression ratio. The second parameter to png_set_filter() is ++the filter method, for which the only valid values are 0 (as of the ++July 1999 PNG specification, version 1.2) or 64 (if you are writing ++a PNG datastream that is to be embedded in a MNG datastream). The third ++parameter is a flag that indicates which filter type(s) are to be tested ++for each scanline. See the PNG specification for details on the specific ++filter types. ++ ++ ++ /* turn on or off filtering, and/or choose ++ specific filters. You can use either a single ++ PNG_FILTER_VALUE_NAME or the bitwise OR of one ++ or more PNG_FILTER_NAME masks. ++ */ ++ png_set_filter(png_ptr, 0, ++ PNG_FILTER_NONE | PNG_FILTER_VALUE_NONE | ++ PNG_FILTER_SUB | PNG_FILTER_VALUE_SUB | ++ PNG_FILTER_UP | PNG_FILTER_VALUE_UP | ++ PNG_FILTER_AVG | PNG_FILTER_VALUE_AVG | ++ PNG_FILTER_PAETH | PNG_FILTER_VALUE_PAETH| ++ PNG_ALL_FILTERS | PNG_FAST_FILTERS); ++ ++If an application wants to start and stop using particular filters during ++compression, it should start out with all of the filters (to ensure that ++the previous row of pixels will be stored in case it's needed later), ++and then add and remove them after the start of compression. ++ ++If you are writing a PNG datastream that is to be embedded in a MNG ++datastream, the second parameter can be either 0 or 64. ++ ++The png_set_compression_*() functions interface to the zlib compression ++library, and should mostly be ignored unless you really know what you are ++doing. The only generally useful call is png_set_compression_level() ++which changes how much time zlib spends on trying to compress the image ++data. See the Compression Library (zlib.h and algorithm.txt, distributed ++with zlib) for details on the compression levels. ++ ++ #include zlib.h ++ ++ /* Set the zlib compression level */ ++ png_set_compression_level(png_ptr, ++ Z_BEST_COMPRESSION); ++ ++ /* Set other zlib parameters for compressing IDAT */ ++ png_set_compression_mem_level(png_ptr, 8); ++ png_set_compression_strategy(png_ptr, ++ Z_DEFAULT_STRATEGY); ++ png_set_compression_window_bits(png_ptr, 15); ++ png_set_compression_method(png_ptr, 8); ++ png_set_compression_buffer_size(png_ptr, 8192) ++ ++ /* Set zlib parameters for text compression ++ * If you don't call these, the parameters ++ * fall back on those defined for IDAT chunks ++ */ ++ png_set_text_compression_mem_level(png_ptr, 8); ++ png_set_text_compression_strategy(png_ptr, ++ Z_DEFAULT_STRATEGY); ++ png_set_text_compression_window_bits(png_ptr, 15); ++ png_set_text_compression_method(png_ptr, 8); ++ ++Setting the contents of info for output ++ ++You now need to fill in the png_info structure with all the data you ++wish to write before the actual image. Note that the only thing you ++are allowed to write after the image is the text chunks and the time ++chunk (as of PNG Specification 1.2, anyway). See png_write_end() and ++the latest PNG specification for more information on that. If you ++wish to write them before the image, fill them in now, and flag that ++data as being valid. If you want to wait until after the data, don't ++fill them until png_write_end(). For all the fields in png_info and ++their data types, see png.h. For explanations of what the fields ++contain, see the PNG specification. ++ ++Some of the more important parts of the png_info are: ++ ++ png_set_IHDR(png_ptr, info_ptr, width, height, ++ bit_depth, color_type, interlace_type, ++ compression_type, filter_method) ++ ++ width - holds the width of the image ++ in pixels (up to 2^31). ++ ++ height - holds the height of the image ++ in pixels (up to 2^31). ++ ++ bit_depth - holds the bit depth of one of the ++ image channels. ++ (valid values are 1, 2, 4, 8, 16 ++ and depend also on the ++ color_type. See also significant ++ bits (sBIT) below). ++ ++ color_type - describes which color/alpha ++ channels are present. ++ PNG_COLOR_TYPE_GRAY ++ (bit depths 1, 2, 4, 8, 16) ++ PNG_COLOR_TYPE_GRAY_ALPHA ++ (bit depths 8, 16) ++ PNG_COLOR_TYPE_PALETTE ++ (bit depths 1, 2, 4, 8) ++ PNG_COLOR_TYPE_RGB ++ (bit_depths 8, 16) ++ PNG_COLOR_TYPE_RGB_ALPHA ++ (bit_depths 8, 16) ++ ++ PNG_COLOR_MASK_PALETTE ++ PNG_COLOR_MASK_COLOR ++ PNG_COLOR_MASK_ALPHA ++ ++ interlace_type - PNG_INTERLACE_NONE or ++ PNG_INTERLACE_ADAM7 ++ ++ compression_type - (must be ++ PNG_COMPRESSION_TYPE_DEFAULT) ++ ++ filter_method - (must be PNG_FILTER_TYPE_DEFAULT ++ or, if you are writing a PNG to ++ be embedded in a MNG datastream, ++ can also be ++ PNG_INTRAPIXEL_DIFFERENCING) ++ ++If you call png_set_IHDR(), the call must appear before any of the ++other png_set_*() functions, because they might require access to some of ++the IHDR settings. The remaining png_set_*() functions can be called ++in any order. ++ ++If you wish, you can reset the compression_type, interlace_type, or ++filter_method later by calling png_set_IHDR() again; if you do this, the ++width, height, bit_depth, and color_type must be the same in each call. ++ ++ png_set_PLTE(png_ptr, info_ptr, palette, ++ num_palette); ++ ++ palette - the palette for the file ++ (array of png_color) ++ num_palette - number of entries in the palette ++ ++ ++ png_set_gAMA(png_ptr, info_ptr, file_gamma); ++ png_set_gAMA_fixed(png_ptr, info_ptr, int_file_gamma); ++ ++ file_gamma - the gamma at which the image was ++ created (PNG_INFO_gAMA) ++ ++ int_file_gamma - 100,000 times the gamma at which ++ the image was created ++ ++ png_set_cHRM(png_ptr, info_ptr, white_x, white_y, red_x, red_y, ++ green_x, green_y, blue_x, blue_y) ++ png_set_cHRM_XYZ(png_ptr, info_ptr, red_X, red_Y, red_Z, green_X, ++ green_Y, green_Z, blue_X, blue_Y, blue_Z) ++ png_set_cHRM_fixed(png_ptr, info_ptr, int_white_x, int_white_y, ++ int_red_x, int_red_y, int_green_x, int_green_y, ++ int_blue_x, int_blue_y) ++ png_set_cHRM_XYZ_fixed(png_ptr, info_ptr, int_red_X, int_red_Y, ++ int_red_Z, int_green_X, int_green_Y, int_green_Z, ++ int_blue_X, int_blue_Y, int_blue_Z) ++ ++ {white,red,green,blue}_{x,y} ++ A color space encoding specified using the chromaticities ++ of the end points and the white point. ++ ++ {red,green,blue}_{X,Y,Z} ++ A color space encoding specified using the encoding end ++ points - the CIE tristimulus specification of the intended ++ color of the red, green and blue channels in the PNG RGB ++ data. The white point is simply the sum of the three end ++ points. ++ ++ png_set_sRGB(png_ptr, info_ptr, srgb_intent); ++ ++ srgb_intent - the rendering intent ++ (PNG_INFO_sRGB) The presence of ++ the sRGB chunk means that the pixel ++ data is in the sRGB color space. ++ This chunk also implies specific ++ values of gAMA and cHRM. Rendering ++ intent is the CSS-1 property that ++ has been defined by the International ++ Color Consortium ++ (http://www.color.org). ++ It can be one of ++ PNG_sRGB_INTENT_SATURATION, ++ PNG_sRGB_INTENT_PERCEPTUAL, ++ PNG_sRGB_INTENT_ABSOLUTE, or ++ PNG_sRGB_INTENT_RELATIVE. ++ ++ ++ png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, ++ srgb_intent); ++ ++ srgb_intent - the rendering intent ++ (PNG_INFO_sRGB) The presence of the ++ sRGB chunk means that the pixel ++ data is in the sRGB color space. ++ This function also causes gAMA and ++ cHRM chunks with the specific values ++ that are consistent with sRGB to be ++ written. ++ ++ png_set_iCCP(png_ptr, info_ptr, name, compression_type, ++ profile, proflen); ++ ++ name - The profile name. ++ ++ compression_type - The compression type; always ++ PNG_COMPRESSION_TYPE_BASE for PNG 1.0. ++ You may give NULL to this argument to ++ ignore it. ++ ++ profile - International Color Consortium color ++ profile data. May contain NULs. ++ ++ proflen - length of profile data in bytes. ++ ++ png_set_sBIT(png_ptr, info_ptr, sig_bit); ++ ++ sig_bit - the number of significant bits for ++ (PNG_INFO_sBIT) each of the gray, red, ++ green, and blue channels, whichever are ++ appropriate for the given color type ++ (png_color_16) ++ ++ png_set_tRNS(png_ptr, info_ptr, trans_alpha, ++ num_trans, trans_color); ++ ++ trans_alpha - array of alpha (transparency) ++ entries for palette (PNG_INFO_tRNS) ++ ++ num_trans - number of transparent entries ++ (PNG_INFO_tRNS) ++ ++ trans_color - graylevel or color sample values ++ (in order red, green, blue) of the ++ single transparent color for ++ non-paletted images (PNG_INFO_tRNS) ++ ++ png_set_eXIf_1(png_ptr, info_ptr, num_exif, exif); ++ ++ exif - Exif profile (array of ++ png_byte) (PNG_INFO_eXIf) ++ ++ png_set_hIST(png_ptr, info_ptr, hist); ++ ++ hist - histogram of palette (array of ++ png_uint_16) (PNG_INFO_hIST) ++ ++ png_set_tIME(png_ptr, info_ptr, mod_time); ++ ++ mod_time - time image was last modified ++ (PNG_VALID_tIME) ++ ++ png_set_bKGD(png_ptr, info_ptr, background); ++ ++ background - background color (of type ++ png_color_16p) (PNG_VALID_bKGD) ++ ++ png_set_text(png_ptr, info_ptr, text_ptr, num_text); ++ ++ text_ptr - array of png_text holding image ++ comments ++ ++ text_ptr[i].compression - type of compression used ++ on "text" PNG_TEXT_COMPRESSION_NONE ++ PNG_TEXT_COMPRESSION_zTXt ++ PNG_ITXT_COMPRESSION_NONE ++ PNG_ITXT_COMPRESSION_zTXt ++ text_ptr[i].key - keyword for comment. Must contain ++ 1-79 characters. ++ text_ptr[i].text - text comments for current ++ keyword. Can be NULL or empty. ++ text_ptr[i].text_length - length of text string, ++ after decompression, 0 for iTXt ++ text_ptr[i].itxt_length - length of itxt string, ++ after decompression, 0 for tEXt/zTXt ++ text_ptr[i].lang - language of comment (NULL or ++ empty for unknown). ++ text_ptr[i].translated_keyword - keyword in UTF-8 (NULL ++ or empty for unknown). ++ ++ Note that the itxt_length, lang, and lang_key ++ members of the text_ptr structure only exist when the ++ library is built with iTXt chunk support. Prior to ++ libpng-1.4.0 the library was built by default without ++ iTXt support. Also note that when iTXt is supported, ++ they contain NULL pointers when the "compression" ++ field contains PNG_TEXT_COMPRESSION_NONE or ++ PNG_TEXT_COMPRESSION_zTXt. ++ ++ num_text - number of comments ++ ++ png_set_sPLT(png_ptr, info_ptr, &palette_ptr, ++ num_spalettes); ++ ++ palette_ptr - array of png_sPLT_struct structures ++ to be added to the list of palettes ++ in the info structure. ++ num_spalettes - number of palette structures to be ++ added. ++ ++ png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, ++ unit_type); ++ ++ offset_x - positive offset from the left ++ edge of the screen ++ ++ offset_y - positive offset from the top ++ edge of the screen ++ ++ unit_type - PNG_OFFSET_PIXEL, PNG_OFFSET_MICROMETER ++ ++ png_set_pHYs(png_ptr, info_ptr, res_x, res_y, ++ unit_type); ++ ++ res_x - pixels/unit physical resolution ++ in x direction ++ ++ res_y - pixels/unit physical resolution ++ in y direction ++ ++ unit_type - PNG_RESOLUTION_UNKNOWN, ++ PNG_RESOLUTION_METER ++ ++ png_set_sCAL(png_ptr, info_ptr, unit, width, height) ++ ++ unit - physical scale units (an integer) ++ ++ width - width of a pixel in physical scale units ++ ++ height - height of a pixel in physical scale units ++ (width and height are doubles) ++ ++ png_set_sCAL_s(png_ptr, info_ptr, unit, width, height) ++ ++ unit - physical scale units (an integer) ++ ++ width - width of a pixel in physical scale units ++ expressed as a string ++ ++ height - height of a pixel in physical scale units ++ (width and height are strings like "2.54") ++ ++ png_set_unknown_chunks(png_ptr, info_ptr, &unknowns, ++ num_unknowns) ++ ++ unknowns - array of png_unknown_chunk ++ structures holding unknown chunks ++ unknowns[i].name - name of unknown chunk ++ unknowns[i].data - data of unknown chunk ++ unknowns[i].size - size of unknown chunk's data ++ unknowns[i].location - position to write chunk in file ++ 0: do not write chunk ++ PNG_HAVE_IHDR: before PLTE ++ PNG_HAVE_PLTE: before IDAT ++ PNG_AFTER_IDAT: after IDAT ++ ++The "location" member is set automatically according to ++what part of the output file has already been written. ++You can change its value after calling png_set_unknown_chunks() ++as demonstrated in pngtest.c. Within each of the "locations", ++the chunks are sequenced according to their position in the ++structure (that is, the value of "i", which is the order in which ++the chunk was either read from the input file or defined with ++png_set_unknown_chunks). ++ ++A quick word about text and num_text. text is an array of png_text ++structures. num_text is the number of valid structures in the array. ++Each png_text structure holds a language code, a keyword, a text value, ++and a compression type. ++ ++The compression types have the same valid numbers as the compression ++types of the image data. Currently, the only valid number is zero. ++However, you can store text either compressed or uncompressed, unlike ++images, which always have to be compressed. So if you don't want the ++text compressed, set the compression type to PNG_TEXT_COMPRESSION_NONE. ++Because tEXt and zTXt chunks don't have a language field, if you ++specify PNG_TEXT_COMPRESSION_NONE or PNG_TEXT_COMPRESSION_zTXt ++any language code or translated keyword will not be written out. ++ ++Until text gets around a few hundred bytes, it is not worth compressing it. ++After the text has been written out to the file, the compression type ++is set to PNG_TEXT_COMPRESSION_NONE_WR or PNG_TEXT_COMPRESSION_zTXt_WR, ++so that it isn't written out again at the end (in case you are calling ++png_write_end() with the same struct). ++ ++The keywords that are given in the PNG Specification are: ++ ++ Title Short (one line) title or ++ caption for image ++ ++ Author Name of image's creator ++ ++ Description Description of image (possibly long) ++ ++ Copyright Copyright notice ++ ++ Creation Time Time of original image creation ++ (usually RFC 1123 format, see below) ++ ++ Software Software used to create the image ++ ++ Disclaimer Legal disclaimer ++ ++ Warning Warning of nature of content ++ ++ Source Device used to create the image ++ ++ Comment Miscellaneous comment; conversion ++ from other image format ++ ++The keyword-text pairs work like this. Keywords should be short ++simple descriptions of what the comment is about. Some typical ++keywords are found in the PNG specification, as is some recommendations ++on keywords. You can repeat keywords in a file. You can even write ++some text before the image and some after. For example, you may want ++to put a description of the image before the image, but leave the ++disclaimer until after, so viewers working over modem connections ++don't have to wait for the disclaimer to go over the modem before ++they start seeing the image. Finally, keywords should be full ++words, not abbreviations. Keywords and text are in the ISO 8859-1 ++(Latin-1) character set (a superset of regular ASCII) and can not ++contain NUL characters, and should not contain control or other ++unprintable characters. To make the comments widely readable, stick ++with basic ASCII, and avoid machine specific character set extensions ++like the IBM-PC character set. The keyword must be present, but ++you can leave off the text string on non-compressed pairs. ++Compressed pairs must have a text string, as only the text string ++is compressed anyway, so the compression would be meaningless. ++ ++PNG supports modification time via the png_time structure. Two ++conversion routines are provided, png_convert_from_time_t() for ++time_t and png_convert_from_struct_tm() for struct tm. The ++time_t routine uses gmtime(). You don't have to use either of ++these, but if you wish to fill in the png_time structure directly, ++you should provide the time in universal time (GMT) if possible ++instead of your local time. Note that the year number is the full ++year (e.g. 1998, rather than 98 - PNG is year 2000 compliant!), and ++that months start with 1. ++ ++If you want to store the time of the original image creation, you should ++use a plain tEXt chunk with the "Creation Time" keyword. This is ++necessary because the "creation time" of a PNG image is somewhat vague, ++depending on whether you mean the PNG file, the time the image was ++created in a non-PNG format, a still photo from which the image was ++scanned, or possibly the subject matter itself. In order to facilitate ++machine-readable dates, it is recommended that the "Creation Time" ++tEXt chunk use RFC 1123 format dates (e.g. "22 May 1997 18:07:10 GMT"), ++although this isn't a requirement. Unlike the tIME chunk, the ++"Creation Time" tEXt chunk is not expected to be automatically changed ++by the software. To facilitate the use of RFC 1123 dates, a function ++png_convert_to_rfc1123_buffer(buffer, png_timep) is provided to ++convert from PNG time to an RFC 1123 format string. The caller must provide ++a writeable buffer of at least 29 bytes. ++ ++Writing unknown chunks ++ ++You can use the png_set_unknown_chunks function to queue up private chunks ++for writing. You give it a chunk name, location, raw data, and a size. You ++also must use png_set_keep_unknown_chunks() to ensure that libpng will ++handle them. That's all there is to it. The chunks will be written by the ++next following png_write_info_before_PLTE, png_write_info, or png_write_end ++function, depending upon the specified location. Any chunks previously ++read into the info structure's unknown-chunk list will also be written out ++in a sequence that satisfies the PNG specification's ordering rules. ++ ++Here is an example of writing two private chunks, prVt and miNE: ++ ++ #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED ++ /* Set unknown chunk data */ ++ png_unknown_chunk unk_chunk[2]; ++ strcpy((char *) unk_chunk[0].name, "prVt"; ++ unk_chunk[0].data = (unsigned char *) "PRIVATE DATA"; ++ unk_chunk[0].size = strlen(unk_chunk[0].data)+1; ++ unk_chunk[0].location = PNG_HAVE_IHDR; ++ strcpy((char *) unk_chunk[1].name, "miNE"; ++ unk_chunk[1].data = (unsigned char *) "MY CHUNK DATA"; ++ unk_chunk[1].size = strlen(unk_chunk[0].data)+1; ++ unk_chunk[1].location = PNG_AFTER_IDAT; ++ png_set_unknown_chunks(write_ptr, write_info_ptr, ++ unk_chunk, 2); ++ /* Needed because miNE is not safe-to-copy */ ++ png_set_keep_unknown_chunks(png, PNG_HANDLE_CHUNK_ALWAYS, ++ (png_bytep) "miNE", 1); ++ # if PNG_LIBPNG_VER < 10600 ++ /* Deal with unknown chunk location bug in 1.5.x and earlier */ ++ png_set_unknown_chunk_location(png, info, 0, PNG_HAVE_IHDR); ++ png_set_unknown_chunk_location(png, info, 1, PNG_AFTER_IDAT); ++ # endif ++ # if PNG_LIBPNG_VER < 10500 ++ /* PNG_AFTER_IDAT writes two copies of the chunk prior to libpng-1.5.0, ++ * one before IDAT and another after IDAT, so don't use it; only use ++ * PNG_HAVE_IHDR location. This call resets the location previously ++ * set by assignment and png_set_unknown_chunk_location() for chunk 1. ++ */ ++ png_set_unknown_chunk_location(png, info, 1, PNG_HAVE_IHDR); ++ # endif ++ #endif ++ ++The high-level write interface ++ ++At this point there are two ways to proceed; through the high-level ++write interface, or through a sequence of low-level write operations. ++You can use the high-level interface if your image data is present ++in the info structure. All defined output ++transformations are permitted, enabled by the following masks. ++ ++ PNG_TRANSFORM_IDENTITY No transformation ++ PNG_TRANSFORM_PACKING Pack 1, 2 and 4-bit samples ++ PNG_TRANSFORM_PACKSWAP Change order of packed ++ pixels to LSB first ++ PNG_TRANSFORM_INVERT_MONO Invert monochrome images ++ PNG_TRANSFORM_SHIFT Normalize pixels to the ++ sBIT depth ++ PNG_TRANSFORM_BGR Flip RGB to BGR, RGBA ++ to BGRA ++ PNG_TRANSFORM_SWAP_ALPHA Flip RGBA to ARGB or GA ++ to AG ++ PNG_TRANSFORM_INVERT_ALPHA Change alpha from opacity ++ to transparency ++ PNG_TRANSFORM_SWAP_ENDIAN Byte-swap 16-bit samples ++ PNG_TRANSFORM_STRIP_FILLER Strip out filler ++ bytes (deprecated). ++ PNG_TRANSFORM_STRIP_FILLER_BEFORE Strip out leading ++ filler bytes ++ PNG_TRANSFORM_STRIP_FILLER_AFTER Strip out trailing ++ filler bytes ++ ++If you have valid image data in the info structure (you can use ++png_set_rows() to put image data in the info structure), simply do this: ++ ++ png_write_png(png_ptr, info_ptr, png_transforms, NULL) ++ ++where png_transforms is an integer containing the bitwise OR of some set of ++transformation flags. This call is equivalent to png_write_info(), ++followed the set of transformations indicated by the transform mask, ++then png_write_image(), and finally png_write_end(). ++ ++(The final parameter of this call is not yet used. Someday it might point ++to transformation parameters required by some future output transform.) ++ ++You must use png_transforms and not call any png_set_transform() functions ++when you use png_write_png(). ++ ++The low-level write interface ++ ++If you are going the low-level route instead, you are now ready to ++write all the file information up to the actual image data. You do ++this with a call to png_write_info(). ++ ++ png_write_info(png_ptr, info_ptr); ++ ++Note that there is one transformation you may need to do before ++png_write_info(). In PNG files, the alpha channel in an image is the ++level of opacity. If your data is supplied as a level of transparency, ++you can invert the alpha channel before you write it, so that 0 is ++fully transparent and 255 (in 8-bit or paletted images) or 65535 ++(in 16-bit images) is fully opaque, with ++ ++ png_set_invert_alpha(png_ptr); ++ ++This must appear before png_write_info() instead of later with the ++other transformations because in the case of paletted images the tRNS ++chunk data has to be inverted before the tRNS chunk is written. If ++your image is not a paletted image, the tRNS data (which in such cases ++represents a single color to be rendered as transparent) won't need to ++be changed, and you can safely do this transformation after your ++png_write_info() call. ++ ++If you need to write a private chunk that you want to appear before ++the PLTE chunk when PLTE is present, you can write the PNG info in ++two steps, and insert code to write your own chunk between them: ++ ++ png_write_info_before_PLTE(png_ptr, info_ptr); ++ png_set_unknown_chunks(png_ptr, info_ptr, ...); ++ png_write_info(png_ptr, info_ptr); ++ ++After you've written the file information, you can set up the library ++to handle any special transformations of the image data. The various ++ways to transform the data will be described in the order that they ++should occur. This is important, as some of these change the color ++type and/or bit depth of the data, and some others only work on ++certain color types and bit depths. Even though each transformation ++checks to see if it has data that it can do something with, you should ++make sure to only enable a transformation if it will be valid for the ++data. For example, don't swap red and blue on grayscale data. ++ ++PNG files store RGB pixels packed into 3 or 6 bytes. This code tells ++the library to strip input data that has 4 or 8 bytes per pixel down ++to 3 or 6 bytes (or strip 2 or 4-byte grayscale+filler data to 1 or 2 ++bytes per pixel). ++ ++ png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE); ++ ++where the 0 is unused, and the location is either PNG_FILLER_BEFORE or ++PNG_FILLER_AFTER, depending upon whether the filler byte in the pixel ++is stored XRGB or RGBX. ++ ++PNG files pack pixels of bit depths 1, 2, and 4 into bytes as small as ++they can, resulting in, for example, 8 pixels per byte for 1 bit files. ++If the data is supplied at 1 pixel per byte, use this code, which will ++correctly pack the pixels into a single byte: ++ ++ png_set_packing(png_ptr); ++ ++PNG files reduce possible bit depths to 1, 2, 4, 8, and 16. If your ++data is of another bit depth, you can write an sBIT chunk into the ++file so that decoders can recover the original data if desired. ++ ++ /* Set the true bit depth of the image data */ ++ if (color_type & PNG_COLOR_MASK_COLOR) ++ { ++ sig_bit.red = true_bit_depth; ++ sig_bit.green = true_bit_depth; ++ sig_bit.blue = true_bit_depth; ++ } ++ ++ else ++ { ++ sig_bit.gray = true_bit_depth; ++ } ++ ++ if (color_type & PNG_COLOR_MASK_ALPHA) ++ { ++ sig_bit.alpha = true_bit_depth; ++ } ++ ++ png_set_sBIT(png_ptr, info_ptr, &sig_bit); ++ ++If the data is stored in the row buffer in a bit depth other than ++one supported by PNG (e.g. 3 bit data in the range 0-7 for a 4-bit PNG), ++this will scale the values to appear to be the correct bit depth as ++is required by PNG. ++ ++ png_set_shift(png_ptr, &sig_bit); ++ ++PNG files store 16-bit pixels in network byte order (big-endian, ++ie. most significant bits first). This code would be used if they are ++supplied the other way (little-endian, i.e. least significant bits ++first, the way PCs store them): ++ ++ if (bit_depth > 8) ++ png_set_swap(png_ptr); ++ ++If you are using packed-pixel images (1, 2, or 4 bits/pixel), and you ++need to change the order the pixels are packed into bytes, you can use: ++ ++ if (bit_depth < 8) ++ png_set_packswap(png_ptr); ++ ++PNG files store 3 color pixels in red, green, blue order. This code ++would be used if they are supplied as blue, green, red: ++ ++ png_set_bgr(png_ptr); ++ ++PNG files describe monochrome as black being zero and white being ++one. This code would be used if the pixels are supplied with this reversed ++(black being one and white being zero): ++ ++ png_set_invert_mono(png_ptr); ++ ++Finally, you can write your own transformation function if none of ++the existing ones meets your needs. This is done by setting a callback ++with ++ ++ png_set_write_user_transform_fn(png_ptr, ++ write_transform_fn); ++ ++You must supply the function ++ ++ void write_transform_fn(png_structp png_ptr, png_row_infop ++ row_info, png_bytep data) ++ ++See pngtest.c for a working example. Your function will be called ++before any of the other transformations are processed. If supported ++libpng also supplies an information routine that may be called from ++your callback: ++ ++ png_get_current_row_number(png_ptr); ++ png_get_current_pass_number(png_ptr); ++ ++This returns the current row passed to the transform. With interlaced ++images the value returned is the row in the input sub-image image. Use ++PNG_ROW_FROM_PASS_ROW(row, pass) and PNG_COL_FROM_PASS_COL(col, pass) to ++find the output pixel (x,y) given an interlaced sub-image pixel (row,col,pass). ++ ++The discussion of interlace handling above contains more information on how to ++use these values. ++ ++You can also set up a pointer to a user structure for use by your ++callback function. ++ ++ png_set_user_transform_info(png_ptr, user_ptr, 0, 0); ++ ++The user_channels and user_depth parameters of this function are ignored ++when writing; you can set them to zero as shown. ++ ++You can retrieve the pointer via the function png_get_user_transform_ptr(). ++For example: ++ ++ voidp write_user_transform_ptr = ++ png_get_user_transform_ptr(png_ptr); ++ ++It is possible to have libpng flush any pending output, either manually, ++or automatically after a certain number of lines have been written. To ++flush the output stream a single time call: ++ ++ png_write_flush(png_ptr); ++ ++and to have libpng flush the output stream periodically after a certain ++number of scanlines have been written, call: ++ ++ png_set_flush(png_ptr, nrows); ++ ++Note that the distance between rows is from the last time png_write_flush() ++was called, or the first row of the image if it has never been called. ++So if you write 50 lines, and then png_set_flush 25, it will flush the ++output on the next scanline, and every 25 lines thereafter, unless ++png_write_flush() is called before 25 more lines have been written. ++If nrows is too small (less than about 10 lines for a 640 pixel wide ++RGB image) the image compression may decrease noticeably (although this ++may be acceptable for real-time applications). Infrequent flushing will ++only degrade the compression performance by a few percent over images ++that do not use flushing. ++ ++Writing the image data ++ ++That's it for the transformations. Now you can write the image data. ++The simplest way to do this is in one function call. If you have the ++whole image in memory, you can just call png_write_image() and libpng ++will write the image. You will need to pass in an array of pointers to ++each row. This function automatically handles interlacing, so you don't ++need to call png_set_interlace_handling() or call this function multiple ++times, or any of that other stuff necessary with png_write_rows(). ++ ++ png_write_image(png_ptr, row_pointers); ++ ++where row_pointers is: ++ ++ png_byte *row_pointers[height]; ++ ++You can point to void or char or whatever you use for pixels. ++ ++If you don't want to write the whole image at once, you can ++use png_write_rows() instead. If the file is not interlaced, ++this is simple: ++ ++ png_write_rows(png_ptr, row_pointers, ++ number_of_rows); ++ ++row_pointers is the same as in the png_write_image() call. ++ ++If you are just writing one row at a time, you can do this with ++a single row_pointer instead of an array of row_pointers: ++ ++ png_bytep row_pointer = row; ++ ++ png_write_row(png_ptr, row_pointer); ++ ++When the file is interlaced, things can get a good deal more complicated. ++The only currently (as of the PNG Specification version 1.2, dated July ++1999) defined interlacing scheme for PNG files is the "Adam7" interlace ++scheme, that breaks down an image into seven smaller images of varying ++size. libpng will build these images for you, or you can do them ++yourself. If you want to build them yourself, see the PNG specification ++for details of which pixels to write when. ++ ++If you don't want libpng to handle the interlacing details, just ++use png_set_interlace_handling() and call png_write_rows() the ++correct number of times to write all the sub-images ++(png_set_interlace_handling() returns the number of sub-images.) ++ ++If you want libpng to build the sub-images, call this before you start ++writing any rows: ++ ++ number_of_passes = png_set_interlace_handling(png_ptr); ++ ++This will return the number of passes needed. Currently, this is seven, ++but may change if another interlace type is added. ++ ++Then write the complete image number_of_passes times. ++ ++ png_write_rows(png_ptr, row_pointers, number_of_rows); ++ ++Think carefully before you write an interlaced image. Typically code that ++reads such images reads all the image data into memory, uncompressed, before ++doing any processing. Only code that can display an image on the fly can ++take advantage of the interlacing and even then the image has to be exactly ++the correct size for the output device, because scaling an image requires ++adjacent pixels and these are not available until all the passes have been ++read. ++ ++If you do write an interlaced image you will hardly ever need to handle ++the interlacing yourself. Call png_set_interlace_handling() and use the ++approach described above. ++ ++The only time it is conceivable that you will really need to write an ++interlaced image pass-by-pass is when you have read one pass by pass and ++made some pixel-by-pixel transformation to it, as described in the read ++code above. In this case use the PNG_PASS_ROWS and PNG_PASS_COLS macros ++to determine the size of each sub-image in turn and simply write the rows ++you obtained from the read code. ++ ++Finishing a sequential write ++ ++After you are finished writing the image, you should finish writing ++the file. If you are interested in writing comments or time, you should ++pass an appropriately filled png_info pointer. If you are not interested, ++you can pass NULL. ++ ++ png_write_end(png_ptr, info_ptr); ++ ++When you are done, you can free all memory used by libpng like this: ++ ++ png_destroy_write_struct(&png_ptr, &info_ptr); ++ ++It is also possible to individually free the info_ptr members that ++point to libpng-allocated storage with the following function: ++ ++ png_free_data(png_ptr, info_ptr, mask, seq) ++ ++ mask - identifies data to be freed, a mask ++ containing the bitwise OR of one or ++ more of ++ PNG_FREE_PLTE, PNG_FREE_TRNS, ++ PNG_FREE_HIST, PNG_FREE_ICCP, ++ PNG_FREE_PCAL, PNG_FREE_ROWS, ++ PNG_FREE_SCAL, PNG_FREE_SPLT, ++ PNG_FREE_TEXT, PNG_FREE_UNKN, ++ or simply PNG_FREE_ALL ++ ++ seq - sequence number of item to be freed ++ (-1 for all items) ++ ++This function may be safely called when the relevant storage has ++already been freed, or has not yet been allocated, or was allocated ++by the user and not by libpng, and will in those cases do nothing. ++The "seq" parameter is ignored if only one item of the selected data ++type, such as PLTE, is allowed. If "seq" is not -1, and multiple items ++are allowed for the data type identified in the mask, such as text or ++sPLT, only the n'th item in the structure is freed, where n is "seq". ++ ++If you allocated data such as a palette that you passed in to libpng ++with png_set_*, you must not free it until just before the call to ++png_destroy_write_struct(). ++ ++The default behavior is only to free data that was allocated internally ++by libpng. This can be changed, so that libpng will not free the data, ++or so that it will free data that was allocated by the user with png_malloc() ++or png_calloc() and passed in via a png_set_*() function, with ++ ++ png_data_freer(png_ptr, info_ptr, freer, mask) ++ ++ freer - one of ++ PNG_DESTROY_WILL_FREE_DATA ++ PNG_SET_WILL_FREE_DATA ++ PNG_USER_WILL_FREE_DATA ++ ++ mask - which data elements are affected ++ same choices as in png_free_data() ++ ++For example, to transfer responsibility for some data from a read structure ++to a write structure, you could use ++ ++ png_data_freer(read_ptr, read_info_ptr, ++ PNG_USER_WILL_FREE_DATA, ++ PNG_FREE_PLTE|PNG_FREE_tRNS|PNG_FREE_hIST) ++ ++ png_data_freer(write_ptr, write_info_ptr, ++ PNG_DESTROY_WILL_FREE_DATA, ++ PNG_FREE_PLTE|PNG_FREE_tRNS|PNG_FREE_hIST) ++ ++thereby briefly reassigning responsibility for freeing to the user but ++immediately afterwards reassigning it once more to the write_destroy ++function. Having done this, it would then be safe to destroy the read ++structure and continue to use the PLTE, tRNS, and hIST data in the write ++structure. ++ ++This function only affects data that has already been allocated. ++You can call this function before calling after the png_set_*() functions ++to control whether the user or png_destroy_*() is supposed to free the data. ++When the user assumes responsibility for libpng-allocated data, the ++application must use ++png_free() to free it, and when the user transfers responsibility to libpng ++for data that the user has allocated, the user must have used png_malloc() ++or png_calloc() to allocate it. ++ ++If you allocated text_ptr.text, text_ptr.lang, and text_ptr.translated_keyword ++separately, do not transfer responsibility for freeing text_ptr to libpng, ++because when libpng fills a png_text structure it combines these members with ++the key member, and png_free_data() will free only text_ptr.key. Similarly, ++if you transfer responsibility for free'ing text_ptr from libpng to your ++application, your application must not separately free those members. ++For a more compact example of writing a PNG image, see the file example.c. ++ ++V. Simplified API ++ ++The simplified API, which became available in libpng-1.6.0, hides the details ++of both libpng and the PNG file format itself. ++It allows PNG files to be read into a very limited number of ++in-memory bitmap formats or to be written from the same formats. If these ++formats do not accommodate your needs then you can, and should, use the more ++sophisticated APIs above - these support a wide variety of in-memory formats ++and a wide variety of sophisticated transformations to those formats as well ++as a wide variety of APIs to manipulate ancillary information. ++ ++To read a PNG file using the simplified API: ++ ++ 1) Declare a 'png_image' structure (see below) on the stack, set the ++ version field to PNG_IMAGE_VERSION and the 'opaque' pointer to NULL ++ (this is REQUIRED, your program may crash if you don't do it.) ++ ++ 2) Call the appropriate png_image_begin_read... function. ++ ++ 3) Set the png_image 'format' member to the required sample format. ++ ++ 4) Allocate a buffer for the image and, if required, the color-map. ++ ++ 5) Call png_image_finish_read to read the image and, if required, the ++ color-map into your buffers. ++ ++There are no restrictions on the format of the PNG input itself; all valid ++color types, bit depths, and interlace methods are acceptable, and the ++input image is transformed as necessary to the requested in-memory format ++during the png_image_finish_read() step. The only caveat is that if you ++request a color-mapped image from a PNG that is full-color or makes ++complex use of an alpha channel the transformation is extremely lossy and the ++result may look terrible. ++ ++To write a PNG file using the simplified API: ++ ++ 1) Declare a 'png_image' structure on the stack and memset() ++ it to all zero. ++ ++ 2) Initialize the members of the structure that describe the ++ image, setting the 'format' member to the format of the ++ image samples. ++ ++ 3) Call the appropriate png_image_write... function with a ++ pointer to the image and, if necessary, the color-map to write ++ the PNG data. ++ ++png_image is a structure that describes the in-memory format of an image ++when it is being read or defines the in-memory format of an image that you ++need to write. The "png_image" structure contains the following members: ++ ++ png_controlp opaque Initialize to NULL, free with png_image_free ++ png_uint_32 version Set to PNG_IMAGE_VERSION ++ png_uint_32 width Image width in pixels (columns) ++ png_uint_32 height Image height in pixels (rows) ++ png_uint_32 format Image format as defined below ++ png_uint_32 flags A bit mask containing informational flags ++ png_uint_32 colormap_entries; Number of entries in the color-map ++ png_uint_32 warning_or_error; ++ char message[64]; ++ ++In the event of an error or warning the "warning_or_error" ++field will be set to a non-zero value and the 'message' field will contain ++a '\0' terminated string with the libpng error or warning message. If both ++warnings and an error were encountered, only the error is recorded. If there ++are multiple warnings, only the first one is recorded. ++ ++The upper 30 bits of the "warning_or_error" value are reserved; the low two ++bits contain a two bit code such that a value more than 1 indicates a failure ++in the API just called: ++ ++ 0 - no warning or error ++ 1 - warning ++ 2 - error ++ 3 - error preceded by warning ++ ++The pixels (samples) of the image have one to four channels whose components ++have original values in the range 0 to 1.0: ++ ++ 1: A single gray or luminance channel (G). ++ 2: A gray/luminance channel and an alpha channel (GA). ++ 3: Three red, green, blue color channels (RGB). ++ 4: Three color channels and an alpha channel (RGBA). ++ ++The channels are encoded in one of two ways: ++ ++ a) As a small integer, value 0..255, contained in a single byte. For the ++alpha channel the original value is simply value/255. For the color or ++luminance channels the value is encoded according to the sRGB specification ++and matches the 8-bit format expected by typical display devices. ++ ++The color/gray channels are not scaled (pre-multiplied) by the alpha ++channel and are suitable for passing to color management software. ++ ++ b) As a value in the range 0..65535, contained in a 2-byte integer, in ++the native byte order of the platform on which the application is running. ++All channels can be converted to the original value by dividing by 65535; all ++channels are linear. Color channels use the RGB encoding (RGB end-points) of ++the sRGB specification. This encoding is identified by the ++PNG_FORMAT_FLAG_LINEAR flag below. ++ ++When the simplified API needs to convert between sRGB and linear colorspaces, ++the actual sRGB transfer curve defined in the sRGB specification (see the ++article at https://en.wikipedia.org/wiki/SRGB) is used, not the gamma=1/2.2 ++approximation used elsewhere in libpng. ++ ++When an alpha channel is present it is expected to denote pixel coverage ++of the color or luminance channels and is returned as an associated alpha ++channel: the color/gray channels are scaled (pre-multiplied) by the alpha ++value. ++ ++The samples are either contained directly in the image data, between 1 and 8 ++bytes per pixel according to the encoding, or are held in a color-map indexed ++by bytes in the image data. In the case of a color-map the color-map entries ++are individual samples, encoded as above, and the image data has one byte per ++pixel to select the relevant sample from the color-map. ++ ++PNG_FORMAT_* ++ ++The #defines to be used in png_image::format. Each #define identifies a ++particular layout of channel data and, if present, alpha values. There are ++separate defines for each of the two component encodings. ++ ++A format is built up using single bit flag values. All combinations are ++valid. Formats can be built up from the flag values or you can use one of ++the predefined values below. When testing formats always use the FORMAT_FLAG ++macros to test for individual features - future versions of the library may ++add new flags. ++ ++When reading or writing color-mapped images the format should be set to the ++format of the entries in the color-map then png_image_{read,write}_colormap ++called to read or write the color-map and set the format correctly for the ++image data. Do not set the PNG_FORMAT_FLAG_COLORMAP bit directly! ++ ++NOTE: libpng can be built with particular features disabled. If you see ++compiler errors because the definition of one of the following flags has been ++compiled out it is because libpng does not have the required support. It is ++possible, however, for the libpng configuration to enable the format on just ++read or just write; in that case you may see an error at run time. ++You can guard against this by checking for the definition of the ++appropriate "_SUPPORTED" macro, one of: ++ ++ PNG_SIMPLIFIED_{READ,WRITE}_{BGR,AFIRST}_SUPPORTED ++ ++ PNG_FORMAT_FLAG_ALPHA format with an alpha channel ++ PNG_FORMAT_FLAG_COLOR color format: otherwise grayscale ++ PNG_FORMAT_FLAG_LINEAR 2-byte channels else 1-byte ++ PNG_FORMAT_FLAG_COLORMAP image data is color-mapped ++ PNG_FORMAT_FLAG_BGR BGR colors, else order is RGB ++ PNG_FORMAT_FLAG_AFIRST alpha channel comes first ++ ++Supported formats are as follows. Future versions of libpng may support more ++formats; for compatibility with older versions simply check if the format ++macro is defined using #ifdef. These defines describe the in-memory layout ++of the components of the pixels of the image. ++ ++First the single byte (sRGB) formats: ++ ++ PNG_FORMAT_GRAY ++ PNG_FORMAT_GA ++ PNG_FORMAT_AG ++ PNG_FORMAT_RGB ++ PNG_FORMAT_BGR ++ PNG_FORMAT_RGBA ++ PNG_FORMAT_ARGB ++ PNG_FORMAT_BGRA ++ PNG_FORMAT_ABGR ++ ++Then the linear 2-byte formats. When naming these "Y" is used to ++indicate a luminance (gray) channel. The component order within the pixel ++is always the same - there is no provision for swapping the order of the ++components in the linear format. The components are 16-bit integers in ++the native byte order for your platform, and there is no provision for ++swapping the bytes to a different endian condition. ++ ++ PNG_FORMAT_LINEAR_Y ++ PNG_FORMAT_LINEAR_Y_ALPHA ++ PNG_FORMAT_LINEAR_RGB ++ PNG_FORMAT_LINEAR_RGB_ALPHA ++ ++With color-mapped formats the image data is one byte for each pixel. The byte ++is an index into the color-map which is formatted as above. To obtain a ++color-mapped format it is sufficient just to add the PNG_FOMAT_FLAG_COLORMAP ++to one of the above definitions, or you can use one of the definitions below. ++ ++ PNG_FORMAT_RGB_COLORMAP ++ PNG_FORMAT_BGR_COLORMAP ++ PNG_FORMAT_RGBA_COLORMAP ++ PNG_FORMAT_ARGB_COLORMAP ++ PNG_FORMAT_BGRA_COLORMAP ++ PNG_FORMAT_ABGR_COLORMAP ++ ++PNG_IMAGE macros ++ ++These are convenience macros to derive information from a png_image ++structure. The PNG_IMAGE_SAMPLE_ macros return values appropriate to the ++actual image sample values - either the entries in the color-map or the ++pixels in the image. The PNG_IMAGE_PIXEL_ macros return corresponding values ++for the pixels and will always return 1 for color-mapped formats. The ++remaining macros return information about the rows in the image and the ++complete image. ++ ++NOTE: All the macros that take a png_image::format parameter are compile time ++constants if the format parameter is, itself, a constant. Therefore these ++macros can be used in array declarations and case labels where required. ++Similarly the macros are also pre-processor constants (sizeof is not used) so ++they can be used in #if tests. ++ ++ PNG_IMAGE_SAMPLE_CHANNELS(fmt) ++ Returns the total number of channels in a given format: 1..4 ++ ++ PNG_IMAGE_SAMPLE_COMPONENT_SIZE(fmt) ++ Returns the size in bytes of a single component of a pixel or color-map ++ entry (as appropriate) in the image: 1 or 2. ++ ++ PNG_IMAGE_SAMPLE_SIZE(fmt) ++ This is the size of the sample data for one sample. If the image is ++ color-mapped it is the size of one color-map entry (and image pixels are ++ one byte in size), otherwise it is the size of one image pixel. ++ ++ PNG_IMAGE_MAXIMUM_COLORMAP_COMPONENTS(fmt) ++ The maximum size of the color-map required by the format expressed in a ++ count of components. This can be used to compile-time allocate a ++ color-map: ++ ++ png_uint_16 colormap[PNG_IMAGE_MAXIMUM_COLORMAP_COMPONENTS(linear_fmt)]; ++ ++ png_byte colormap[PNG_IMAGE_MAXIMUM_COLORMAP_COMPONENTS(sRGB_fmt)]; ++ ++ Alternatively use the PNG_IMAGE_COLORMAP_SIZE macro below to use the ++ information from one of the png_image_begin_read_ APIs and dynamically ++ allocate the required memory. ++ ++ PNG_IMAGE_COLORMAP_SIZE(fmt) ++ The size of the color-map required by the format; this is the size of the ++ color-map buffer passed to the png_image_{read,write}_colormap APIs. It is ++ a fixed number determined by the format so can easily be allocated on the ++ stack if necessary. ++ ++Corresponding information about the pixels ++ ++ PNG_IMAGE_PIXEL_CHANNELS(fmt) ++ The number of separate channels (components) in a pixel; 1 for a ++ color-mapped image. ++ ++ PNG_IMAGE_PIXEL_COMPONENT_SIZE(fmt)\ ++ The size, in bytes, of each component in a pixel; 1 for a color-mapped ++ image. ++ ++ PNG_IMAGE_PIXEL_SIZE(fmt) ++ The size, in bytes, of a complete pixel; 1 for a color-mapped image. ++ ++Information about the whole row, or whole image ++ ++ PNG_IMAGE_ROW_STRIDE(image) ++ Returns the total number of components in a single row of the image; this ++ is the minimum 'row stride', the minimum count of components between each ++ row. For a color-mapped image this is the minimum number of bytes in a ++ row. ++ ++ If you need the stride measured in bytes, row_stride_bytes is ++ PNG_IMAGE_ROW_STRIDE(image) * PNG_IMAGE_PIXEL_COMPONENT_SIZE(fmt) ++ plus any padding bytes that your application might need, for example ++ to start the next row on a 4-byte boundary. ++ ++ PNG_IMAGE_BUFFER_SIZE(image, row_stride) ++ Return the size, in bytes, of an image buffer given a png_image and a row ++ stride - the number of components to leave space for in each row. ++ ++ PNG_IMAGE_SIZE(image) ++ Return the size, in bytes, of the image in memory given just a png_image; ++ the row stride is the minimum stride required for the image. ++ ++ PNG_IMAGE_COLORMAP_SIZE(image) ++ Return the size, in bytes, of the color-map of this image. If the image ++ format is not a color-map format this will return a size sufficient for ++ 256 entries in the given format; check PNG_FORMAT_FLAG_COLORMAP if ++ you don't want to allocate a color-map in this case. ++ ++PNG_IMAGE_FLAG_* ++ ++Flags containing additional information about the image are held in ++the 'flags' field of png_image. ++ ++ PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB == 0x01 ++ This indicates that the RGB values of the in-memory bitmap do not ++ correspond to the red, green and blue end-points defined by sRGB. ++ ++ PNG_IMAGE_FLAG_FAST == 0x02 ++ On write emphasise speed over compression; the resultant PNG file will be ++ larger but will be produced significantly faster, particular for large ++ images. Do not use this option for images which will be distributed, only ++ used it when producing intermediate files that will be read back in ++ repeatedly. For a typical 24-bit image the option will double the read ++ speed at the cost of increasing the image size by 25%, however for many ++ more compressible images the PNG file can be 10 times larger with only a ++ slight speed gain. ++ ++ PNG_IMAGE_FLAG_16BIT_sRGB == 0x04 ++ On read if the image is a 16-bit per component image and there is no gAMA ++ or sRGB chunk assume that the components are sRGB encoded. Notice that ++ images output by the simplified API always have gamma information; setting ++ this flag only affects the interpretation of 16-bit images from an ++ external source. It is recommended that the application expose this flag ++ to the user; the user can normally easily recognize the difference between ++ linear and sRGB encoding. This flag has no effect on write - the data ++ passed to the write APIs must have the correct encoding (as defined ++ above.) ++ ++ If the flag is not set (the default) input 16-bit per component data is ++ assumed to be linear. ++ ++ NOTE: the flag can only be set after the png_image_begin_read_ call, ++ because that call initializes the 'flags' field. ++ ++READ APIs ++ ++ The png_image passed to the read APIs must have been initialized by setting ++ the png_controlp field 'opaque' to NULL (or, better, memset the whole thing.) ++ ++ int png_image_begin_read_from_file( png_imagep image, ++ const char *file_name) ++ ++ The named file is opened for read and the image header ++ is filled in from the PNG header in the file. ++ ++ int png_image_begin_read_from_stdio (png_imagep image, ++ FILE* file) ++ ++ The PNG header is read from the stdio FILE object. ++ ++ int png_image_begin_read_from_memory(png_imagep image, ++ png_const_voidp memory, size_t size) ++ ++ The PNG header is read from the given memory buffer. ++ ++ int png_image_finish_read(png_imagep image, ++ png_colorp background, void *buffer, ++ png_int_32 row_stride, void *colormap)); ++ ++ Finish reading the image into the supplied buffer and ++ clean up the png_image structure. ++ ++ row_stride is the step, in png_byte or png_uint_16 units ++ as appropriate, between adjacent rows. A positive stride ++ indicates that the top-most row is first in the buffer - ++ the normal top-down arrangement. A negative stride ++ indicates that the bottom-most row is first in the buffer. ++ ++ background need only be supplied if an alpha channel must ++ be removed from a png_byte format and the removal is to be ++ done by compositing on a solid color; otherwise it may be ++ NULL and any composition will be done directly onto the ++ buffer. The value is an sRGB color to use for the ++ background, for grayscale output the green channel is used. ++ ++ For linear output removing the alpha channel is always done ++ by compositing on black. ++ ++ void png_image_free(png_imagep image) ++ ++ Free any data allocated by libpng in image->opaque, ++ setting the pointer to NULL. May be called at any time ++ after the structure is initialized. ++ ++When the simplified API needs to convert between sRGB and linear colorspaces, ++the actual sRGB transfer curve defined in the sRGB specification (see the ++article at https://en.wikipedia.org/wiki/SRGB) is used, not the gamma=1/2.2 ++approximation used elsewhere in libpng. ++ ++WRITE APIS ++ ++For write you must initialize a png_image structure to describe the image to ++be written: ++ ++ version: must be set to PNG_IMAGE_VERSION ++ opaque: must be initialized to NULL ++ width: image width in pixels ++ height: image height in rows ++ format: the format of the data you wish to write ++ flags: set to 0 unless one of the defined flags applies; set ++ PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB for color format images ++ where the RGB values do not correspond to the colors in sRGB. ++ colormap_entries: set to the number of entries in the color-map (0 to 256) ++ ++ int png_image_write_to_file, (png_imagep image, ++ const char *file, int convert_to_8bit, const void *buffer, ++ png_int_32 row_stride, const void *colormap)); ++ ++ Write the image to the named file. ++ ++ int png_image_write_to_memory (png_imagep image, void *memory, ++ png_alloc_size_t * PNG_RESTRICT memory_bytes, ++ int convert_to_8_bit, const void *buffer, ptrdiff_t row_stride, ++ const void *colormap)); ++ ++ Write the image to memory. ++ ++ int png_image_write_to_stdio(png_imagep image, FILE *file, ++ int convert_to_8_bit, const void *buffer, ++ png_int_32 row_stride, const void *colormap) ++ ++ Write the image to the given (FILE*). ++ ++With all write APIs if image is in one of the linear formats with ++(png_uint_16) data then setting convert_to_8_bit will cause the output to be ++a (png_byte) PNG gamma encoded according to the sRGB specification, otherwise ++a 16-bit linear encoded PNG file is written. ++ ++With all APIs row_stride is handled as in the read APIs - it is the spacing ++from one row to the next in component sized units (float) and if negative ++indicates a bottom-up row layout in the buffer. If you pass zero, libpng will ++calculate the row_stride for you from the width and number of channels. ++ ++Note that the write API does not support interlacing, sub-8-bit pixels, ++indexed (paletted) images, or most ancillary chunks. ++ ++VI. Modifying/Customizing libpng ++ ++There are two issues here. The first is changing how libpng does ++standard things like memory allocation, input/output, and error handling. ++The second deals with more complicated things like adding new chunks, ++adding new transformations, and generally changing how libpng works. ++Both of those are compile-time issues; that is, they are generally ++determined at the time the code is written, and there is rarely a need ++to provide the user with a means of changing them. ++ ++Memory allocation, input/output, and error handling ++ ++All of the memory allocation, input/output, and error handling in libpng ++goes through callbacks that are user-settable. The default routines are ++in pngmem.c, pngrio.c, pngwio.c, and pngerror.c, respectively. To change ++these functions, call the appropriate png_set_*_fn() function. ++ ++Memory allocation is done through the functions png_malloc(), png_calloc(), ++and png_free(). The png_malloc() and png_free() functions currently just ++call the standard C functions and png_calloc() calls png_malloc() and then ++clears the newly allocated memory to zero; note that png_calloc(png_ptr, size) ++is not the same as the calloc(number, size) function provided by stdlib.h. ++There is limited support for certain systems with segmented memory ++architectures and the types of pointers declared by png.h match this; you ++will have to use appropriate pointers in your application. If you prefer ++to use a different method of allocating and freeing data, you can use ++png_create_read_struct_2() or png_create_write_struct_2() to register your ++own functions as described above. These functions also provide a void ++pointer that can be retrieved via ++ ++ mem_ptr=png_get_mem_ptr(png_ptr); ++ ++Your replacement memory functions must have prototypes as follows: ++ ++ png_voidp malloc_fn(png_structp png_ptr, ++ png_alloc_size_t size); ++ ++ void free_fn(png_structp png_ptr, png_voidp ptr); ++ ++Your malloc_fn() must return NULL in case of failure. The png_malloc() ++function will normally call png_error() if it receives a NULL from the ++system memory allocator or from your replacement malloc_fn(). ++ ++Your free_fn() will never be called with a NULL ptr, since libpng's ++png_free() checks for NULL before calling free_fn(). ++ ++Input/Output in libpng is done through png_read() and png_write(), ++which currently just call fread() and fwrite(). The FILE * is stored in ++png_struct and is initialized via png_init_io(). If you wish to change ++the method of I/O, the library supplies callbacks that you can set ++through the function png_set_read_fn() and png_set_write_fn() at run ++time, instead of calling the png_init_io() function. These functions ++also provide a void pointer that can be retrieved via the function ++png_get_io_ptr(). For example: ++ ++ png_set_read_fn(png_structp read_ptr, ++ voidp read_io_ptr, png_rw_ptr read_data_fn) ++ ++ png_set_write_fn(png_structp write_ptr, ++ voidp write_io_ptr, png_rw_ptr write_data_fn, ++ png_flush_ptr output_flush_fn); ++ ++ voidp read_io_ptr = png_get_io_ptr(read_ptr); ++ voidp write_io_ptr = png_get_io_ptr(write_ptr); ++ ++The replacement I/O functions must have prototypes as follows: ++ ++ void user_read_data(png_structp png_ptr, ++ png_bytep data, size_t length); ++ ++ void user_write_data(png_structp png_ptr, ++ png_bytep data, size_t length); ++ ++ void user_flush_data(png_structp png_ptr); ++ ++The user_read_data() function is responsible for detecting and ++handling end-of-data errors. ++ ++Supplying NULL for the read, write, or flush functions sets them back ++to using the default C stream functions, which expect the io_ptr to ++point to a standard *FILE structure. It is probably a mistake ++to use NULL for one of write_data_fn and output_flush_fn but not both ++of them, unless you have built libpng with PNG_NO_WRITE_FLUSH defined. ++It is an error to read from a write stream, and vice versa. ++ ++Error handling in libpng is done through png_error() and png_warning(). ++Errors handled through png_error() are fatal, meaning that png_error() ++should never return to its caller. Currently, this is handled via ++setjmp() and longjmp() (unless you have compiled libpng with ++PNG_NO_SETJMP, in which case it is handled via PNG_ABORT()), ++but you could change this to do things like exit() if you should wish, ++as long as your function does not return. ++ ++On non-fatal errors, png_warning() is called ++to print a warning message, and then control returns to the calling code. ++By default png_error() and png_warning() print a message on stderr via ++fprintf() unless the library is compiled with PNG_NO_CONSOLE_IO defined ++(because you don't want the messages) or PNG_NO_STDIO defined (because ++fprintf() isn't available). If you wish to change the behavior of the error ++functions, you will need to set up your own message callbacks. These ++functions are normally supplied at the time that the png_struct is created. ++It is also possible to redirect errors and warnings to your own replacement ++functions after png_create_*_struct() has been called by calling: ++ ++ png_set_error_fn(png_structp png_ptr, ++ png_voidp error_ptr, png_error_ptr error_fn, ++ png_error_ptr warning_fn); ++ ++If NULL is supplied for either error_fn or warning_fn, then the libpng ++default function will be used, calling fprintf() and/or longjmp() if a ++problem is encountered. The replacement error functions should have ++parameters as follows: ++ ++ void user_error_fn(png_structp png_ptr, ++ png_const_charp error_msg); ++ ++ void user_warning_fn(png_structp png_ptr, ++ png_const_charp warning_msg); ++ ++Then, within your user_error_fn or user_warning_fn, you can retrieve ++the error_ptr if you need it, by calling ++ ++ png_voidp error_ptr = png_get_error_ptr(png_ptr); ++ ++The motivation behind using setjmp() and longjmp() is the C++ throw and ++catch exception handling methods. This makes the code much easier to write, ++as there is no need to check every return code of every function call. ++However, there are some uncertainties about the status of local variables ++after a longjmp, so the user may want to be careful about doing anything ++after setjmp returns non-zero besides returning itself. Consult your ++compiler documentation for more details. For an alternative approach, you ++may wish to use the "cexcept" facility (see https://cexcept.sourceforge.io/), ++which is illustrated in pngvalid.c and in contrib/visupng. ++ ++Beginning in libpng-1.4.0, the png_set_benign_errors() API became available. ++You can use this to handle certain errors (normally handled as errors) ++as warnings. ++ ++ png_set_benign_errors (png_ptr, int allowed); ++ ++ allowed: 0: treat png_benign_error() as an error. ++ 1: treat png_benign_error() as a warning. ++ ++As of libpng-1.6.0, the default condition is to treat benign errors as ++warnings while reading and as errors while writing. ++ ++Custom chunks ++ ++If you need to read or write custom chunks, you may need to get deeper ++into the libpng code. The library now has mechanisms for storing ++and writing chunks of unknown type; you can even declare callbacks ++for custom chunks. However, this may not be good enough if the ++library code itself needs to know about interactions between your ++chunk and existing `intrinsic' chunks. ++ ++If you need to write a new intrinsic chunk, first read the PNG ++specification. Acquire a first level of understanding of how it works. ++Pay particular attention to the sections that describe chunk names, ++and look at how other chunks were designed, so you can do things ++similarly. Second, check out the sections of libpng that read and ++write chunks. Try to find a chunk that is similar to yours and use ++it as a template. More details can be found in the comments inside ++the code. It is best to handle private or unknown chunks in a generic method, ++via callback functions, instead of by modifying libpng functions. This ++is illustrated in pngtest.c, which uses a callback function to handle a ++private "vpAg" chunk and the new "sTER" chunk, which are both unknown to ++libpng. ++ ++If you wish to write your own transformation for the data, look through ++the part of the code that does the transformations, and check out some of ++the simpler ones to get an idea of how they work. Try to find a similar ++transformation to the one you want to add and copy off of it. More details ++can be found in the comments inside the code itself. ++ ++Configuring for gui/windowing platforms: ++ ++You will need to write new error and warning functions that use the GUI ++interface, as described previously, and set them to be the error and ++warning functions at the time that png_create_*_struct() is called, ++in order to have them available during the structure initialization. ++They can be changed later via png_set_error_fn(). On some compilers, ++you may also have to change the memory allocators (png_malloc, etc.). ++ ++Configuring zlib: ++ ++There are special functions to configure the compression. Perhaps the ++most useful one changes the compression level, which currently uses ++input compression values in the range 0 - 9. The library normally ++uses the default compression level (Z_DEFAULT_COMPRESSION = 6). Tests ++have shown that for a large majority of images, compression values in ++the range 3-6 compress nearly as well as higher levels, and do so much ++faster. For online applications it may be desirable to have maximum speed ++(Z_BEST_SPEED = 1). With versions of zlib after v0.99, you can also ++specify no compression (Z_NO_COMPRESSION = 0), but this would create ++files larger than just storing the raw bitmap. You can specify the ++compression level by calling: ++ ++ #include zlib.h ++ png_set_compression_level(png_ptr, level); ++ ++Another useful one is to reduce the memory level used by the library. ++The memory level defaults to 8, but it can be lowered if you are ++short on memory (running DOS, for example, where you only have 640K). ++Note that the memory level does have an effect on compression; among ++other things, lower levels will result in sections of incompressible ++data being emitted in smaller stored blocks, with a correspondingly ++larger relative overhead of up to 15% in the worst case. ++ ++ #include zlib.h ++ png_set_compression_mem_level(png_ptr, level); ++ ++The other functions are for configuring zlib. They are not recommended ++for normal use and may result in writing an invalid PNG file. See ++zlib.h for more information on what these mean. ++ ++ #include zlib.h ++ png_set_compression_strategy(png_ptr, ++ strategy); ++ ++ png_set_compression_window_bits(png_ptr, ++ window_bits); ++ ++ png_set_compression_method(png_ptr, method); ++ ++This controls the size of the IDAT chunks (default 8192): ++ ++ png_set_compression_buffer_size(png_ptr, size); ++ ++As of libpng version 1.5.4, additional APIs became ++available to set these separately for non-IDAT ++compressed chunks such as zTXt, iTXt, and iCCP: ++ ++ #include zlib.h ++ #if PNG_LIBPNG_VER >= 10504 ++ png_set_text_compression_level(png_ptr, level); ++ ++ png_set_text_compression_mem_level(png_ptr, level); ++ ++ png_set_text_compression_strategy(png_ptr, ++ strategy); ++ ++ png_set_text_compression_window_bits(png_ptr, ++ window_bits); ++ ++ png_set_text_compression_method(png_ptr, method); ++ #endif ++ ++Controlling row filtering ++ ++If you want to control whether libpng uses filtering or not, which ++filters are used, and how it goes about picking row filters, you ++can call one of these functions. The selection and configuration ++of row filters can have a significant impact on the size and ++encoding speed and a somewhat lesser impact on the decoding speed ++of an image. Filtering is enabled by default for RGB and grayscale ++images (with and without alpha), but not for paletted images nor ++for any images with bit depths less than 8 bits/pixel. ++ ++The 'method' parameter sets the main filtering method, which is ++currently only '0' in the PNG 1.2 specification. The 'filters' ++parameter sets which filter(s), if any, should be used for each ++scanline. Possible values are PNG_ALL_FILTERS, PNG_NO_FILTERS, ++or PNG_FAST_FILTERS to turn filtering on and off, or to turn on ++just the fast-decoding subset of filters, respectively. ++ ++Individual filter types are PNG_FILTER_NONE, PNG_FILTER_SUB, ++PNG_FILTER_UP, PNG_FILTER_AVG, PNG_FILTER_PAETH, which can be bitwise ++ORed together with '|' to specify one or more filters to use. ++These filters are described in more detail in the PNG specification. ++If you intend to change the filter type during the course of writing ++the image, you should start with flags set for all of the filters ++you intend to use so that libpng can initialize its internal ++structures appropriately for all of the filter types. (Note that this ++means the first row must always be adaptively filtered, because libpng ++currently does not allocate the filter buffers until png_write_row() ++is called for the first time.) ++ ++ filters = PNG_NO_FILTERS; ++ filters = PNG_ALL_FILTERS; ++ filters = PNG_FAST_FILTERS; ++ ++ or ++ ++ filters = PNG_FILTER_NONE | PNG_FILTER_SUB | ++ PNG_FILTER_UP | PNG_FILTER_AVG | ++ PNG_FILTER_PAETH; ++ ++ png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, ++ filters); ++ ++ The second parameter can also be ++ PNG_INTRAPIXEL_DIFFERENCING if you are ++ writing a PNG to be embedded in a MNG ++ datastream. This parameter must be the ++ same as the value of filter_method used ++ in png_set_IHDR(). ++ ++Requesting debug printout ++ ++The macro definition PNG_DEBUG can be used to request debugging ++printout. Set it to an integer value in the range 0 to 3. Higher ++numbers result in increasing amounts of debugging information. The ++information is printed to the "stderr" file, unless another file ++name is specified in the PNG_DEBUG_FILE macro definition. ++ ++When PNG_DEBUG > 0, the following functions (macros) become available: ++ ++ png_debug(level, message) ++ png_debug1(level, message, p1) ++ png_debug2(level, message, p1, p2) ++ ++in which "level" is compared to PNG_DEBUG to decide whether to print ++the message, "message" is the formatted string to be printed, ++and p1 and p2 are parameters that are to be embedded in the string ++according to printf-style formatting directives. For example, ++ ++ png_debug1(2, "foo=%d", foo); ++ ++is expanded to ++ ++ if (PNG_DEBUG > 2) ++ fprintf(PNG_DEBUG_FILE, "foo=%d\n", foo); ++ ++When PNG_DEBUG is defined but is zero, the macros aren't defined, but you ++can still use PNG_DEBUG to control your own debugging: ++ ++ #ifdef PNG_DEBUG ++ fprintf(stderr, ... ++ #endif ++ ++When PNG_DEBUG = 1, the macros are defined, but only png_debug statements ++having level = 0 will be printed. There aren't any such statements in ++this version of libpng, but if you insert some they will be printed. ++ ++VII. MNG support ++ ++The MNG specification (available at http://www.libpng.org/pub/mng) allows ++certain extensions to PNG for PNG images that are embedded in MNG datastreams. ++Libpng can support some of these extensions. To enable them, use the ++png_permit_mng_features() function: ++ ++ feature_set = png_permit_mng_features(png_ptr, mask) ++ ++ mask is a png_uint_32 containing the bitwise OR of the ++ features you want to enable. These include ++ PNG_FLAG_MNG_EMPTY_PLTE ++ PNG_FLAG_MNG_FILTER_64 ++ PNG_ALL_MNG_FEATURES ++ ++ feature_set is a png_uint_32 that is the bitwise AND of ++ your mask with the set of MNG features that is ++ supported by the version of libpng that you are using. ++ ++It is an error to use this function when reading or writing a standalone ++PNG file with the PNG 8-byte signature. The PNG datastream must be wrapped ++in a MNG datastream. As a minimum, it must have the MNG 8-byte signature ++and the MHDR and MEND chunks. Libpng does not provide support for these ++or any other MNG chunks; your application must provide its own support for ++them. You may wish to consider using libmng (available at ++https://www.libmng.com/) instead. ++ ++VIII. Changes to Libpng from version 0.88 ++ ++It should be noted that versions of libpng later than 0.96 are not ++distributed by the original libpng author, Guy Schalnat, nor by ++Andreas Dilger, who had taken over from Guy during 1996 and 1997, and ++distributed versions 0.89 through 0.96, but rather by another member ++of the original PNG Group, Glenn Randers-Pehrson. Guy and Andreas are ++still alive and well, but they have moved on to other things. ++ ++The old libpng functions png_read_init(), png_write_init(), ++png_info_init(), png_read_destroy(), and png_write_destroy() have been ++moved to PNG_INTERNAL in version 0.95 to discourage their use. These ++functions will be removed from libpng version 1.4.0. ++ ++The preferred method of creating and initializing the libpng structures is ++via the png_create_read_struct(), png_create_write_struct(), and ++png_create_info_struct() because they isolate the size of the structures ++from the application, allow version error checking, and also allow the ++use of custom error handling routines during the initialization, which ++the old functions do not. The functions png_read_destroy() and ++png_write_destroy() do not actually free the memory that libpng ++allocated for these structs, but just reset the data structures, so they ++can be used instead of png_destroy_read_struct() and ++png_destroy_write_struct() if you feel there is too much system overhead ++allocating and freeing the png_struct for each image read. ++ ++Setting the error callbacks via png_set_message_fn() before ++png_read_init() as was suggested in libpng-0.88 is no longer supported ++because this caused applications that do not use custom error functions ++to fail if the png_ptr was not initialized to zero. It is still possible ++to set the error callbacks AFTER png_read_init(), or to change them with ++png_set_error_fn(), which is essentially the same function, but with a new ++name to force compilation errors with applications that try to use the old ++method. ++ ++Support for the sCAL, iCCP, iTXt, and sPLT chunks was added at libpng-1.0.6; ++however, iTXt support was not enabled by default. ++ ++Starting with version 1.0.7, you can find out which version of the library ++you are using at run-time: ++ ++ png_uint_32 libpng_vn = png_access_version_number(); ++ ++The number libpng_vn is constructed from the major version, minor ++version with leading zero, and release number with leading zero, ++(e.g., libpng_vn for version 1.0.7 is 10007). ++ ++Note that this function does not take a png_ptr, so you can call it ++before you've created one. ++ ++You can also check which version of png.h you used when compiling your ++application: ++ ++ png_uint_32 application_vn = PNG_LIBPNG_VER; ++ ++IX. Changes to Libpng from version 1.0.x to 1.2.x ++ ++Support for user memory management was enabled by default. To ++accomplish this, the functions png_create_read_struct_2(), ++png_create_write_struct_2(), png_set_mem_fn(), png_get_mem_ptr(), ++png_malloc_default(), and png_free_default() were added. ++ ++Support for the iTXt chunk has been enabled by default as of ++version 1.2.41. ++ ++Support for certain MNG features was enabled. ++ ++Support for numbered error messages was added. However, we never got ++around to actually numbering the error messages. The function ++png_set_strip_error_numbers() was added (Note: the prototype for this ++function was inadvertently removed from png.h in PNG_NO_ASSEMBLER_CODE ++builds of libpng-1.2.15. It was restored in libpng-1.2.36). ++ ++The png_malloc_warn() function was added at libpng-1.2.3. This issues ++a png_warning and returns NULL instead of aborting when it fails to ++acquire the requested memory allocation. ++ ++Support for setting user limits on image width and height was enabled ++by default. The functions png_set_user_limits(), png_get_user_width_max(), ++and png_get_user_height_max() were added at libpng-1.2.6. ++ ++The png_set_add_alpha() function was added at libpng-1.2.7. ++ ++The function png_set_expand_gray_1_2_4_to_8() was added at libpng-1.2.9. ++Unlike png_set_gray_1_2_4_to_8(), the new function does not expand the ++tRNS chunk to alpha. The png_set_gray_1_2_4_to_8() function is ++deprecated. ++ ++A number of macro definitions in support of runtime selection of ++assembler code features (especially Intel MMX code support) were ++added at libpng-1.2.0: ++ ++ PNG_ASM_FLAG_MMX_SUPPORT_COMPILED ++ PNG_ASM_FLAG_MMX_SUPPORT_IN_CPU ++ PNG_ASM_FLAG_MMX_READ_COMBINE_ROW ++ PNG_ASM_FLAG_MMX_READ_INTERLACE ++ PNG_ASM_FLAG_MMX_READ_FILTER_SUB ++ PNG_ASM_FLAG_MMX_READ_FILTER_UP ++ PNG_ASM_FLAG_MMX_READ_FILTER_AVG ++ PNG_ASM_FLAG_MMX_READ_FILTER_PAETH ++ PNG_ASM_FLAGS_INITIALIZED ++ PNG_MMX_READ_FLAGS ++ PNG_MMX_FLAGS ++ PNG_MMX_WRITE_FLAGS ++ PNG_MMX_FLAGS ++ ++We added the following functions in support of runtime ++selection of assembler code features: ++ ++ png_get_mmx_flagmask() ++ png_set_mmx_thresholds() ++ png_get_asm_flags() ++ png_get_mmx_bitdepth_threshold() ++ png_get_mmx_rowbytes_threshold() ++ png_set_asm_flags() ++ ++We replaced all of these functions with simple stubs in libpng-1.2.20, ++when the Intel assembler code was removed due to a licensing issue. ++ ++These macros are deprecated: ++ ++ PNG_READ_TRANSFORMS_NOT_SUPPORTED ++ PNG_PROGRESSIVE_READ_NOT_SUPPORTED ++ PNG_NO_SEQUENTIAL_READ_SUPPORTED ++ PNG_WRITE_TRANSFORMS_NOT_SUPPORTED ++ PNG_READ_ANCILLARY_CHUNKS_NOT_SUPPORTED ++ PNG_WRITE_ANCILLARY_CHUNKS_NOT_SUPPORTED ++ ++They have been replaced, respectively, by: ++ ++ PNG_NO_READ_TRANSFORMS ++ PNG_NO_PROGRESSIVE_READ ++ PNG_NO_SEQUENTIAL_READ ++ PNG_NO_WRITE_TRANSFORMS ++ PNG_NO_READ_ANCILLARY_CHUNKS ++ PNG_NO_WRITE_ANCILLARY_CHUNKS ++ ++PNG_MAX_UINT was replaced with PNG_UINT_31_MAX. It has been ++deprecated since libpng-1.0.16 and libpng-1.2.6. ++ ++The function ++ png_check_sig(sig, num) ++was replaced with ++ !png_sig_cmp(sig, 0, num) ++It has been deprecated since libpng-0.90. ++ ++The function ++ png_set_gray_1_2_4_to_8() ++which also expands tRNS to alpha was replaced with ++ png_set_expand_gray_1_2_4_to_8() ++which does not. It has been deprecated since libpng-1.0.18 and 1.2.9. ++ ++X. Changes to Libpng from version 1.0.x/1.2.x to 1.4.x ++ ++Private libpng prototypes and macro definitions were moved from ++png.h and pngconf.h into a new pngpriv.h header file. ++ ++Functions png_set_benign_errors(), png_benign_error(), and ++png_chunk_benign_error() were added. ++ ++Support for setting the maximum amount of memory that the application ++will allocate for reading chunks was added, as a security measure. ++The functions png_set_chunk_cache_max() and png_get_chunk_cache_max() ++were added to the library. ++ ++We implemented support for I/O states by adding png_ptr member io_state ++and functions png_get_io_chunk_name() and png_get_io_state() in pngget.c ++ ++We added PNG_TRANSFORM_GRAY_TO_RGB to the available high-level ++input transforms. ++ ++Checking for and reporting of errors in the IHDR chunk is more thorough. ++ ++Support for global arrays was removed, to improve thread safety. ++ ++Some obsolete/deprecated macros and functions have been removed. ++ ++Typecasted NULL definitions such as ++ #define png_voidp_NULL (png_voidp)NULL ++were eliminated. If you used these in your application, just use ++NULL instead. ++ ++The png_struct and info_struct members "trans" and "trans_values" were ++changed to "trans_alpha" and "trans_color", respectively. ++ ++The obsolete, unused pnggccrd.c and pngvcrd.c files and related makefiles ++were removed. ++ ++The PNG_1_0_X and PNG_1_2_X macros were eliminated. ++ ++The PNG_LEGACY_SUPPORTED macro was eliminated. ++ ++Many WIN32_WCE #ifdefs were removed. ++ ++The functions png_read_init(info_ptr), png_write_init(info_ptr), ++png_info_init(info_ptr), png_read_destroy(), and png_write_destroy() ++have been removed. They have been deprecated since libpng-0.95. ++ ++The png_permit_empty_plte() was removed. It has been deprecated ++since libpng-1.0.9. Use png_permit_mng_features() instead. ++ ++We removed the obsolete stub functions png_get_mmx_flagmask(), ++png_set_mmx_thresholds(), png_get_asm_flags(), ++png_get_mmx_bitdepth_threshold(), png_get_mmx_rowbytes_threshold(), ++png_set_asm_flags(), and png_mmx_supported() ++ ++We removed the obsolete png_check_sig(), png_memcpy_check(), and ++png_memset_check() functions. Instead use !png_sig_cmp(), memcpy(), ++and memset(), respectively. ++ ++The function png_set_gray_1_2_4_to_8() was removed. It has been ++deprecated since libpng-1.0.18 and 1.2.9, when it was replaced with ++png_set_expand_gray_1_2_4_to_8() because the former function also ++expanded any tRNS chunk to an alpha channel. ++ ++Macros for png_get_uint_16, png_get_uint_32, and png_get_int_32 ++were added and are used by default instead of the corresponding ++functions. Unfortunately, ++from libpng-1.4.0 until 1.4.4, the png_get_uint_16 macro (but not the ++function) incorrectly returned a value of type png_uint_32. ++ ++We changed the prototype for png_malloc() from ++ png_malloc(png_structp png_ptr, png_uint_32 size) ++to ++ png_malloc(png_structp png_ptr, png_alloc_size_t size) ++ ++This also applies to the prototype for the user replacement malloc_fn(). ++ ++The png_calloc() function was added and is used in place of ++of "png_malloc(); memset();" except in the case in png_read_png() ++where the array consists of pointers; in this case a "for" loop is used ++after the png_malloc() to set the pointers to NULL, to give robust. ++behavior in case the application runs out of memory part-way through ++the process. ++ ++We changed the prototypes of png_get_compression_buffer_size() and ++png_set_compression_buffer_size() to work with size_t instead of ++png_uint_32. ++ ++Support for numbered error messages was removed by default, since we ++never got around to actually numbering the error messages. The function ++png_set_strip_error_numbers() was removed from the library by default. ++ ++The png_zalloc() and png_zfree() functions are no longer exported. ++The png_zalloc() function no longer zeroes out the memory that it ++allocates. Applications that called png_zalloc(png_ptr, number, size) ++can call png_calloc(png_ptr, number*size) instead, and can call ++png_free() instead of png_zfree(). ++ ++Support for dithering was disabled by default in libpng-1.4.0, because ++it has not been well tested and doesn't actually "dither". ++The code was not ++removed, however, and could be enabled by building libpng with ++PNG_READ_DITHER_SUPPORTED defined. In libpng-1.4.2, this support ++was re-enabled, but the function was renamed png_set_quantize() to ++reflect more accurately what it actually does. At the same time, ++the PNG_DITHER_[RED,GREEN_BLUE]_BITS macros were also renamed to ++PNG_QUANTIZE_[RED,GREEN,BLUE]_BITS, and PNG_READ_DITHER_SUPPORTED ++was renamed to PNG_READ_QUANTIZE_SUPPORTED. ++ ++We removed the trailing '.' from the warning and error messages. ++ ++XI. Changes to Libpng from version 1.4.x to 1.5.x ++ ++From libpng-1.4.0 until 1.4.4, the png_get_uint_16 macro (but not the ++function) incorrectly returned a value of type png_uint_32. ++The incorrect macro was removed from libpng-1.4.5. ++ ++Checking for invalid palette index on write was added at libpng ++1.5.10. If a pixel contains an invalid (out-of-range) index libpng issues ++a benign error. This is enabled by default because this condition is an ++error according to the PNG specification, Clause 11.3.2, but the error can ++be ignored in each png_ptr with ++ ++ png_set_check_for_invalid_index(png_ptr, allowed); ++ ++ allowed - one of ++ 0: disable benign error (accept the ++ invalid data without warning). ++ 1: enable benign error (treat the ++ invalid data as an error or a ++ warning). ++ ++If the error is ignored, or if png_benign_error() treats it as a warning, ++any invalid pixels are decoded as opaque black by the decoder and written ++as-is by the encoder. ++ ++Retrieving the maximum palette index found was added at libpng-1.5.15. ++This statement must appear after png_read_png() or png_read_image() while ++reading, and after png_write_png() or png_write_image() while writing. ++ ++ int max_palette = png_get_palette_max(png_ptr, info_ptr); ++ ++This will return the maximum palette index found in the image, or "-1" if ++the palette was not checked, or "0" if no palette was found. Note that this ++does not account for any palette index used by ancillary chunks such as the ++bKGD chunk; you must check those separately to determine the maximum ++palette index actually used. ++ ++There are no substantial API changes between the non-deprecated parts of ++the 1.4.5 API and the 1.5.0 API; however, the ability to directly access ++members of the main libpng control structures, png_struct and png_info, ++deprecated in earlier versions of libpng, has been completely removed from ++libpng 1.5, and new private "pngstruct.h", "pnginfo.h", and "pngdebug.h" ++header files were created. ++ ++We no longer include zlib.h in png.h. The include statement has been moved ++to pngstruct.h, where it is not accessible by applications. Applications that ++need access to information in zlib.h will need to add the '#include "zlib.h"' ++directive. It does not matter whether this is placed prior to or after ++the '"#include png.h"' directive. ++ ++The png_sprintf(), png_strcpy(), and png_strncpy() macros are no longer used ++and were removed. ++ ++We moved the png_strlen(), png_memcpy(), png_memset(), and png_memcmp() ++macros into a private header file (pngpriv.h) that is not accessible to ++applications. ++ ++In png_get_iCCP, the type of "profile" was changed from png_charpp ++to png_bytepp, and in png_set_iCCP, from png_charp to png_const_bytep. ++ ++There are changes of form in png.h, including new and changed macros to ++declare parts of the API. Some API functions with arguments that are ++pointers to data not modified within the function have been corrected to ++declare these arguments with const. ++ ++Much of the internal use of C macros to control the library build has also ++changed and some of this is visible in the exported header files, in ++particular the use of macros to control data and API elements visible ++during application compilation may require significant revision to ++application code. (It is extremely rare for an application to do this.) ++ ++Any program that compiled against libpng 1.4 and did not use deprecated ++features or access internal library structures should compile and work ++against libpng 1.5, except for the change in the prototype for ++png_get_iCCP() and png_set_iCCP() API functions mentioned above. ++ ++libpng 1.5.0 adds PNG_ PASS macros to help in the reading and writing of ++interlaced images. The macros return the number of rows and columns in ++each pass and information that can be used to de-interlace and (if ++absolutely necessary) interlace an image. ++ ++libpng 1.5.0 adds an API png_longjmp(png_ptr, value). This API calls ++the application-provided png_longjmp_ptr on the internal, but application ++initialized, longjmp buffer. It is provided as a convenience to avoid ++the need to use the png_jmpbuf macro, which had the unnecessary side ++effect of resetting the internal png_longjmp_ptr value. ++ ++libpng 1.5.0 includes a complete fixed point API. By default this is ++present along with the corresponding floating point API. In general the ++fixed point API is faster and smaller than the floating point one because ++the PNG file format used fixed point, not floating point. This applies ++even if the library uses floating point in internal calculations. A new ++macro, PNG_FLOATING_ARITHMETIC_SUPPORTED, reveals whether the library ++uses floating point arithmetic (the default) or fixed point arithmetic ++internally for performance critical calculations such as gamma correction. ++In some cases, the gamma calculations may produce slightly different ++results. This has changed the results in png_rgb_to_gray and in alpha ++composition (png_set_background for example). This applies even if the ++original image was already linear (gamma == 1.0) and, therefore, it is ++not necessary to linearize the image. This is because libpng has *not* ++been changed to optimize that case correctly, yet. ++ ++Fixed point support for the sCAL chunk comes with an important caveat; ++the sCAL specification uses a decimal encoding of floating point values ++and the accuracy of PNG fixed point values is insufficient for ++representation of these values. Consequently a "string" API ++(png_get_sCAL_s and png_set_sCAL_s) is the only reliable way of reading ++arbitrary sCAL chunks in the absence of either the floating point API or ++internal floating point calculations. Starting with libpng-1.5.0, both ++of these functions are present when PNG_sCAL_SUPPORTED is defined. Prior ++to libpng-1.5.0, their presence also depended upon PNG_FIXED_POINT_SUPPORTED ++being defined and PNG_FLOATING_POINT_SUPPORTED not being defined. ++ ++Applications no longer need to include the optional distribution header ++file pngusr.h or define the corresponding macros during application ++build in order to see the correct variant of the libpng API. From 1.5.0 ++application code can check for the corresponding _SUPPORTED macro: ++ ++#ifdef PNG_INCH_CONVERSIONS_SUPPORTED ++ /* code that uses the inch conversion APIs. */ ++#endif ++ ++This macro will only be defined if the inch conversion functions have been ++compiled into libpng. The full set of macros, and whether or not support ++has been compiled in, are available in the header file pnglibconf.h. ++This header file is specific to the libpng build. Notice that prior to ++1.5.0 the _SUPPORTED macros would always have the default definition unless ++reset by pngusr.h or by explicit settings on the compiler command line. ++These settings may produce compiler warnings or errors in 1.5.0 because ++of macro redefinition. ++ ++Applications can now choose whether to use these macros or to call the ++corresponding function by defining PNG_USE_READ_MACROS or ++PNG_NO_USE_READ_MACROS before including png.h. Notice that this is ++only supported from 1.5.0; defining PNG_NO_USE_READ_MACROS prior to 1.5.0 ++will lead to a link failure. ++ ++Prior to libpng-1.5.4, the zlib compressor used the same set of parameters ++when compressing the IDAT data and textual data such as zTXt and iCCP. ++In libpng-1.5.4 we reinitialized the zlib stream for each type of data. ++We added five png_set_text_*() functions for setting the parameters to ++use with textual data. ++ ++Prior to libpng-1.5.4, the PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED ++option was off by default, and slightly inaccurate scaling occurred. ++This option can no longer be turned off, and the choice of accurate ++or inaccurate 16-to-8 scaling is by using the new png_set_scale_16_to_8() ++API for accurate scaling or the old png_set_strip_16_to_8() API for simple ++chopping. In libpng-1.5.4, the PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED ++macro became PNG_READ_SCALE_16_TO_8_SUPPORTED, and the PNG_READ_16_TO_8 ++macro became PNG_READ_STRIP_16_TO_8_SUPPORTED, to enable the two ++png_set_*_16_to_8() functions separately. ++ ++Prior to libpng-1.5.4, the png_set_user_limits() function could only be ++used to reduce the width and height limits from the value of ++PNG_USER_WIDTH_MAX and PNG_USER_HEIGHT_MAX, although this document said ++that it could be used to override them. Now this function will reduce or ++increase the limits. ++ ++Starting in libpng-1.5.22, default user limits were established. These ++can be overridden by application calls to png_set_user_limits(), ++png_set_user_chunk_cache_max(), and/or png_set_user_malloc_max(). ++The limits are now ++ max possible default ++ png_user_width_max 0x7fffffff 1,000,000 ++ png_user_height_max 0x7fffffff 1,000,000 ++ png_user_chunk_cache_max 0 (unlimited) 1000 ++ png_user_chunk_malloc_max 0 (unlimited) 8,000,000 ++ ++The png_set_option() function (and the "options" member of the png struct) was ++added to libpng-1.5.15, with option PNG_ARM_NEON. ++ ++The library now supports a complete fixed point implementation and can ++thus be used on systems that have no floating point support or very ++limited or slow support. Previously gamma correction, an essential part ++of complete PNG support, required reasonably fast floating point. ++ ++As part of this the choice of internal implementation has been made ++independent of the choice of fixed versus floating point APIs and all the ++missing fixed point APIs have been implemented. ++ ++The exact mechanism used to control attributes of API functions has ++changed, as described in the INSTALL file. ++ ++A new test program, pngvalid, is provided in addition to pngtest. ++pngvalid validates the arithmetic accuracy of the gamma correction ++calculations and includes a number of validations of the file format. ++A subset of the full range of tests is run when "make check" is done ++(in the 'configure' build.) pngvalid also allows total allocated memory ++usage to be evaluated and performs additional memory overwrite validation. ++ ++Many changes to individual feature macros have been made. The following ++are the changes most likely to be noticed by library builders who ++configure libpng: ++ ++1) All feature macros now have consistent naming: ++ ++#define PNG_NO_feature turns the feature off ++#define PNG_feature_SUPPORTED turns the feature on ++ ++pnglibconf.h contains one line for each feature macro which is either: ++ ++#define PNG_feature_SUPPORTED ++ ++if the feature is supported or: ++ ++/*#undef PNG_feature_SUPPORTED*/ ++ ++if it is not. Library code consistently checks for the 'SUPPORTED' macro. ++It does not, and libpng applications should not, check for the 'NO' macro ++which will not normally be defined even if the feature is not supported. ++The 'NO' macros are only used internally for setting or not setting the ++corresponding 'SUPPORTED' macros. ++ ++Compatibility with the old names is provided as follows: ++ ++PNG_INCH_CONVERSIONS turns on PNG_INCH_CONVERSIONS_SUPPORTED ++ ++And the following definitions disable the corresponding feature: ++ ++PNG_SETJMP_NOT_SUPPORTED disables SETJMP ++PNG_READ_TRANSFORMS_NOT_SUPPORTED disables READ_TRANSFORMS ++PNG_NO_READ_COMPOSITED_NODIV disables READ_COMPOSITE_NODIV ++PNG_WRITE_TRANSFORMS_NOT_SUPPORTED disables WRITE_TRANSFORMS ++PNG_READ_ANCILLARY_CHUNKS_NOT_SUPPORTED disables READ_ANCILLARY_CHUNKS ++PNG_WRITE_ANCILLARY_CHUNKS_NOT_SUPPORTED disables WRITE_ANCILLARY_CHUNKS ++ ++Library builders should remove use of the above, inconsistent, names. ++ ++2) Warning and error message formatting was previously conditional on ++the STDIO feature. The library has been changed to use the ++CONSOLE_IO feature instead. This means that if CONSOLE_IO is disabled ++the library no longer uses the printf(3) functions, even though the ++default read/write implementations use (FILE) style stdio.h functions. ++ ++3) Three feature macros now control the fixed/floating point decisions: ++ ++PNG_FLOATING_POINT_SUPPORTED enables the floating point APIs ++ ++PNG_FIXED_POINT_SUPPORTED enables the fixed point APIs; however, in ++practice these are normally required internally anyway (because the PNG ++file format is fixed point), therefore in most cases PNG_NO_FIXED_POINT ++merely stops the function from being exported. ++ ++PNG_FLOATING_ARITHMETIC_SUPPORTED chooses between the internal floating ++point implementation or the fixed point one. Typically the fixed point ++implementation is larger and slower than the floating point implementation ++on a system that supports floating point; however, it may be faster on a ++system which lacks floating point hardware and therefore uses a software ++emulation. ++ ++4) Added PNG_{READ,WRITE}_INT_FUNCTIONS_SUPPORTED. This allows the ++functions to read and write ints to be disabled independently of ++PNG_USE_READ_MACROS, which allows libpng to be built with the functions ++even though the default is to use the macros - this allows applications ++to choose at app buildtime whether or not to use macros (previously ++impossible because the functions weren't in the default build.) ++ ++XII. Changes to Libpng from version 1.5.x to 1.6.x ++ ++A "simplified API" has been added (see documentation in png.h and a simple ++example in contrib/examples/pngtopng.c). The new publicly visible API ++includes the following: ++ ++ macros: ++ PNG_FORMAT_* ++ PNG_IMAGE_* ++ structures: ++ png_control ++ png_image ++ read functions ++ png_image_begin_read_from_file() ++ png_image_begin_read_from_stdio() ++ png_image_begin_read_from_memory() ++ png_image_finish_read() ++ png_image_free() ++ write functions ++ png_image_write_to_file() ++ png_image_write_to_memory() ++ png_image_write_to_stdio() ++ ++Starting with libpng-1.6.0, you can configure libpng to prefix all exported ++symbols, using the PNG_PREFIX macro. ++ ++We no longer include string.h in png.h. The include statement has been moved ++to pngpriv.h, where it is not accessible by applications. Applications that ++need access to information in string.h must add an '#include ' ++directive. It does not matter whether this is placed prior to or after ++the '#include "png.h"' directive. ++ ++The following API are now DEPRECATED: ++ png_info_init_3() ++ png_convert_to_rfc1123() which has been replaced ++ with png_convert_to_rfc1123_buffer() ++ png_malloc_default() ++ png_free_default() ++ png_reset_zstream() ++ ++The following have been removed: ++ png_get_io_chunk_name(), which has been replaced ++ with png_get_io_chunk_type(). The new ++ function returns a 32-bit integer instead of ++ a string. ++ The png_sizeof(), png_strlen(), png_memcpy(), png_memcmp(), and ++ png_memset() macros are no longer used in the libpng sources and ++ have been removed. These had already been made invisible to applications ++ (i.e., defined in the private pngpriv.h header file) since libpng-1.5.0. ++ ++The signatures of many exported functions were changed, such that ++ png_structp became png_structrp or png_const_structrp ++ png_infop became png_inforp or png_const_inforp ++where "rp" indicates a "restricted pointer". ++ ++Dropped support for 16-bit platforms. The support for FAR/far types has ++been eliminated and the definition of png_alloc_size_t is now controlled ++by a flag so that 'small size_t' systems can select it if necessary. ++ ++Error detection in some chunks has improved; in particular the iCCP chunk ++reader now does pretty complete validation of the basic format. Some bad ++profiles that were previously accepted are now accepted with a warning or ++rejected, depending upon the png_set_benign_errors() setting, in particular ++the very old broken Microsoft/HP 3144-byte sRGB profile. Starting with ++libpng-1.6.11, recognizing and checking sRGB profiles can be avoided by ++means of ++ ++ #if defined(PNG_SKIP_sRGB_CHECK_PROFILE) && \ ++ defined(PNG_SET_OPTION_SUPPORTED) ++ png_set_option(png_ptr, PNG_SKIP_sRGB_CHECK_PROFILE, ++ PNG_OPTION_ON); ++ #endif ++ ++It's not a good idea to do this if you are using the "simplified API", ++which needs to be able to recognize sRGB profiles conveyed via the iCCP ++chunk. ++ ++The PNG spec requirement that only grayscale profiles may appear in images ++with color type 0 or 4 and that even if the image only contains gray pixels, ++only RGB profiles may appear in images with color type 2, 3, or 6, is now ++enforced. The sRGB chunk is allowed to appear in images with any color type ++and is interpreted by libpng to convey a one-tracer-curve gray profile or a ++three-tracer-curve RGB profile as appropriate. ++ ++Libpng 1.5.x erroneously used /MD for Debug DLL builds; if you used the debug ++builds in your app and you changed your app to use /MD you will need to ++change it back to /MDd for libpng 1.6.x. ++ ++Prior to libpng-1.6.0 a warning would be issued if the iTXt chunk contained ++an empty language field or an empty translated keyword. Both of these ++are allowed by the PNG specification, so these warnings are no longer issued. ++ ++The library now issues an error if the application attempts to set a ++transform after it calls png_read_update_info() or if it attempts to call ++both png_read_update_info() and png_start_read_image() or to call either ++of them more than once. ++ ++The default condition for benign_errors is now to treat benign errors as ++warnings while reading and as errors while writing. ++ ++The library now issues a warning if both background processing and RGB to ++gray are used when gamma correction happens. As with previous versions of ++the library the results are numerically very incorrect in this case. ++ ++There are some minor arithmetic changes in some transforms such as ++png_set_background(), that might be detected by certain regression tests. ++ ++Unknown chunk handling has been improved internally, without any API change. ++This adds more correct option control of the unknown handling, corrects ++a pre-existing bug where the per-chunk 'keep' setting is ignored, and makes ++it possible to skip IDAT chunks in the sequential reader. ++ ++The machine-generated configure files are no longer included in branches ++libpng16 and later of the GIT repository. They continue to be included ++in the tarball releases, however. ++ ++Libpng-1.6.0 through 1.6.2 used the CMF bytes at the beginning of the IDAT ++stream to set the size of the sliding window for reading instead of using the ++default 32-kbyte sliding window size. It was discovered that there are ++hundreds of PNG files in the wild that have incorrect CMF bytes that caused ++zlib to issue the "invalid distance too far back" error and reject the file. ++Libpng-1.6.3 and later calculate their own safe CMF from the image dimensions, ++provide a way to revert to the libpng-1.5.x behavior (ignoring the CMF bytes ++and using a 32-kbyte sliding window), by using ++ ++ png_set_option(png_ptr, PNG_MAXIMUM_INFLATE_WINDOW, ++ PNG_OPTION_ON); ++ ++and provide a tool (contrib/tools/pngfix) for rewriting a PNG file while ++optimizing the CMF bytes in its IDAT chunk correctly. ++ ++Libpng-1.6.0 and libpng-1.6.1 wrote uncompressed iTXt chunks with the wrong ++length, which resulted in PNG files that cannot be read beyond the bad iTXt ++chunk. This error was fixed in libpng-1.6.3, and a tool (called ++contrib/tools/png-fix-itxt) has been added to the libpng distribution. ++ ++Starting with libpng-1.6.17, the PNG_SAFE_LIMITS macro was eliminated ++and safe limits are used by default (users who need larger limits ++can still override them at compile time or run time, as described above). ++ ++The new limits are ++ default spec limit ++ png_user_width_max 1,000,000 2,147,483,647 ++ png_user_height_max 1,000,000 2,147,483,647 ++ png_user_chunk_cache_max 128 unlimited ++ png_user_chunk_malloc_max 8,000,000 unlimited ++ ++Starting with libpng-1.6.18, a PNG_RELEASE_BUILD macro was added, which allows ++library builders to control compilation for an installed system (a release build). ++It can be set for testing debug or beta builds to ensure that they will compile ++when the build type is switched to RC or STABLE. In essence this overrides the ++PNG_LIBPNG_BUILD_BASE_TYPE definition which is not directly user controllable. ++ ++Starting with libpng-1.6.19, attempting to set an over-length PLTE chunk ++is an error. Previously this requirement of the PNG specification was not ++enforced, and the palette was always limited to 256 entries. An over-length ++PLTE chunk found in an input PNG is silently truncated. ++ ++Starting with libpng-1.6.31, the eXIf chunk is supported. Libpng does not ++attempt to decode the Exif profile; it simply returns a byte array ++containing the profile to the calling application which must do its own ++decoding. ++ ++XIII. Detecting libpng ++ ++The png_get_io_ptr() function has been present since libpng-0.88, has never ++changed, and is unaffected by conditional compilation macros. It is the ++best choice for use in configure scripts for detecting the presence of any ++libpng version since 0.88. In an autoconf "configure.in" you could use ++ ++ AC_CHECK_LIB(png, png_get_io_ptr, ... ++ ++XV. Source code repository ++ ++Since about February 2009, version 1.2.34, libpng has been under "git" source ++control. The git repository was built from old libpng-x.y.z.tar.gz files ++going back to version 0.70. You can access the git repository (read only) ++at ++ ++ https://github.com/glennrp/libpng or ++ https://git.code.sf.net/p/libpng/code.git ++ ++or you can browse it with a web browser at ++ ++ https://github.com/glennrp/libpng or ++ https://sourceforge.net/p/libpng/code/ci/libpng16/tree/ ++ ++Patches can be sent to png-mng-implement at lists.sourceforge.net or ++uploaded to the libpng bug tracker at ++ ++ https://libpng.sourceforge.io/ ++ ++or as a "pull request" to ++ ++ https://github.com/glennrp/libpng/pulls ++ ++We also accept patches built from the tar or zip distributions, and ++simple verbal descriptions of bug fixes, reported either to the ++SourceForge bug tracker, to the png-mng-implement at lists.sf.net ++mailing list, as github issues. ++ ++XV. Coding style ++ ++Our coding style is similar to the "Allman" style ++(See https://en.wikipedia.org/wiki/Indent_style#Allman_style), with curly ++braces on separate lines: ++ ++ if (condition) ++ { ++ action; ++ } ++ ++ else if (another condition) ++ { ++ another action; ++ } ++ ++The braces can be omitted from simple one-line actions: ++ ++ if (condition) ++ return 0; ++ ++We use 3-space indentation, except for continued statements which ++are usually indented the same as the first line of the statement ++plus four more spaces. ++ ++For macro definitions we use 2-space indentation, always leaving the "#" ++in the first column. ++ ++ #ifndef PNG_NO_FEATURE ++ # ifndef PNG_FEATURE_SUPPORTED ++ # define PNG_FEATURE_SUPPORTED ++ # endif ++ #endif ++ ++Comments appear with the leading "/*" at the same indentation as ++the statement that follows the comment: ++ ++ /* Single-line comment */ ++ statement; ++ ++ /* This is a multiple-line ++ * comment. ++ */ ++ statement; ++ ++Very short comments can be placed after the end of the statement ++to which they pertain: ++ ++ statement; /* comment */ ++ ++We don't use C++ style ("//") comments. We have, however, ++used them in the past in some now-abandoned MMX assembler ++code. ++ ++Functions and their curly braces are not indented, and ++exported functions are marked with PNGAPI: ++ ++ /* This is a public function that is visible to ++ * application programmers. It does thus-and-so. ++ */ ++ void PNGAPI ++ png_exported_function(png_ptr, png_info, foo) ++ { ++ body; ++ } ++ ++The return type and decorations are placed on a separate line ++ahead of the function name, as illustrated above. ++ ++The prototypes for all exported functions appear in png.h, ++above the comment that says ++ ++ /* Maintainer: Put new public prototypes here ... */ ++ ++We mark all non-exported functions with "/* PRIVATE */"": ++ ++ void /* PRIVATE */ ++ png_non_exported_function(png_ptr, png_info, foo) ++ { ++ body; ++ } ++ ++The prototypes for non-exported functions (except for those in ++pngtest) appear in pngpriv.h above the comment that says ++ ++ /* Maintainer: Put new private prototypes here ^ */ ++ ++To avoid polluting the global namespace, the names of all exported ++functions and variables begin with "png_", and all publicly visible C ++preprocessor macros begin with "PNG". We request that applications that ++use libpng *not* begin any of their own symbols with either of these strings. ++ ++We put a space after the "sizeof" operator and we omit the ++optional parentheses around its argument when the argument ++is an expression, not a type name, and we always enclose the ++sizeof operator, with its argument, in parentheses: ++ ++ (sizeof (png_uint_32)) ++ (sizeof array) ++ ++Prior to libpng-1.6.0 we used a "png_sizeof()" macro, formatted as ++though it were a function. ++ ++Control keywords if, for, while, and switch are always followed by a space ++to distinguish them from function calls, which have no trailing space. ++ ++We put a space after each comma and after each semicolon ++in "for" statements, and we put spaces before and after each ++C binary operator and after "for" or "while", and before ++"?". We don't put a space between a typecast and the expression ++being cast, nor do we put one between a function name and the ++left parenthesis that follows it: ++ ++ for (i = 2; i > 0; --i) ++ y[i] = a(x) + (int)b; ++ ++We prefer #ifdef and #ifndef to #if defined() and #if !defined() ++when there is only one macro being tested. We always use parentheses ++with "defined". ++ ++We express integer constants that are used as bit masks in hex format, ++with an even number of lower-case hex digits, and to make them unsigned ++(e.g., 0x00U, 0xffU, 0x0100U) and long if they are greater than 0x7fff ++(e.g., 0xffffUL). ++ ++We prefer to use underscores rather than camelCase in names, except ++for a few type names that we inherit from zlib.h. ++ ++We prefer "if (something != 0)" and "if (something == 0)" over ++"if (something)" and if "(!something)", respectively, and for pointers ++we prefer "if (some_pointer != NULL)" or "if (some_pointer == NULL)". ++ ++We do not use the TAB character for indentation in the C sources. ++ ++Lines do not exceed 80 characters. ++ ++Other rules can be inferred by inspecting the libpng source. +diff --git a/lib/libpng/libpng.3 b/lib/libpng/libpng.3 +new file mode 100644 +index 000000000..f37423540 +--- /dev/null ++++ b/lib/libpng/libpng.3 +@@ -0,0 +1,6052 @@ ++.TH LIBPNG 3 "April 14, 2019" ++.SH NAME ++libpng \- Portable Network Graphics (PNG) Reference Library 1.6.37 ++ ++.SH SYNOPSIS ++\fB#include \fP ++ ++\fBpng_uint_32 png_access_version_number (void);\fP ++ ++\fBvoid png_benign_error (png_structp \fP\fIpng_ptr\fP\fB, png_const_charp \fIerror\fP\fB);\fP ++ ++\fBvoid png_build_grayscale_palette (int \fP\fIbit_depth\fP\fB, png_colorp \fIpalette\fP\fB);\fP ++ ++\fBpng_voidp png_calloc (png_structp \fP\fIpng_ptr\fP\fB, png_alloc_size_t \fIsize\fP\fB);\fP ++ ++\fBvoid png_chunk_benign_error (png_structp \fP\fIpng_ptr\fP\fB, png_const_charp \fIerror\fP\fB);\fP ++ ++\fBvoid png_chunk_error (png_structp \fP\fIpng_ptr\fP\fB, png_const_charp \fIerror\fP\fB);\fP ++ ++\fBvoid png_chunk_warning (png_structp \fP\fIpng_ptr\fP\fB, png_const_charp \fImessage\fP\fB);\fP ++ ++\fBvoid png_convert_from_struct_tm (png_timep \fP\fIptime\fP\fB, struct tm FAR * \fIttime\fP\fB);\fP ++ ++\fBvoid png_convert_from_time_t (png_timep \fP\fIptime\fP\fB, time_t \fIttime\fP\fB);\fP ++ ++\fBpng_charp png_convert_to_rfc1123 (png_structp \fP\fIpng_ptr\fP\fB, png_timep \fIptime\fP\fB);\fP ++ ++\fBpng_infop png_create_info_struct (png_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBpng_structp png_create_read_struct (png_const_charp \fP\fIuser_png_ver\fP\fB, png_voidp \fP\fIerror_ptr\fP\fB, png_error_ptr \fP\fIerror_fn\fP\fB, png_error_ptr \fIwarn_fn\fP\fB);\fP ++ ++\fBpng_structp png_create_read_struct_2 (png_const_charp \fP\fIuser_png_ver\fP\fB, png_voidp \fP\fIerror_ptr\fP\fB, png_error_ptr \fP\fIerror_fn\fP\fB, png_error_ptr \fP\fIwarn_fn\fP\fB, png_voidp \fP\fImem_ptr\fP\fB, png_malloc_ptr \fP\fImalloc_fn\fP\fB, png_free_ptr \fIfree_fn\fP\fB);\fP ++ ++\fBpng_structp png_create_write_struct (png_const_charp \fP\fIuser_png_ver\fP\fB, png_voidp \fP\fIerror_ptr\fP\fB, png_error_ptr \fP\fIerror_fn\fP\fB, png_error_ptr \fIwarn_fn\fP\fB);\fP ++ ++\fBpng_structp png_create_write_struct_2 (png_const_charp \fP\fIuser_png_ver\fP\fB, png_voidp \fP\fIerror_ptr\fP\fB, png_error_ptr \fP\fIerror_fn\fP\fB, png_error_ptr \fP\fIwarn_fn\fP\fB, png_voidp \fP\fImem_ptr\fP\fB, png_malloc_ptr \fP\fImalloc_fn\fP\fB, png_free_ptr \fIfree_fn\fP\fB);\fP ++ ++\fBvoid png_data_freer (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, int \fP\fIfreer\fP\fB, png_uint_32 \fImask\fP\fB);\fP ++ ++\fBvoid png_destroy_info_struct (png_structp \fP\fIpng_ptr\fP\fB, png_infopp \fIinfo_ptr_ptr\fP\fB);\fP ++ ++\fBvoid png_destroy_read_struct (png_structpp \fP\fIpng_ptr_ptr\fP\fB, png_infopp \fP\fIinfo_ptr_ptr\fP\fB, png_infopp \fIend_info_ptr_ptr\fP\fB);\fP ++ ++\fBvoid png_destroy_write_struct (png_structpp \fP\fIpng_ptr_ptr\fP\fB, png_infopp \fIinfo_ptr_ptr\fP\fB);\fP ++ ++\fBvoid png_err (png_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBvoid png_error (png_structp \fP\fIpng_ptr\fP\fB, png_const_charp \fIerror\fP\fB);\fP ++ ++\fBvoid png_free (png_structp \fP\fIpng_ptr\fP\fB, png_voidp \fIptr\fP\fB);\fP ++ ++\fBvoid png_free_chunk_list (png_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBvoid png_free_default (png_structp \fP\fIpng_ptr\fP\fB, png_voidp \fIptr\fP\fB);\fP ++ ++\fBvoid png_free_data (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, int \fInum\fP\fB);\fP ++ ++\fBpng_byte png_get_bit_depth (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fIinfo_ptr\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_bKGD (png_const_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_color_16p \fI*background\fP\fB);\fP ++ ++\fBpng_byte png_get_channels (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fIinfo_ptr\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_cHRM (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fP\fIinfo_ptr\fP\fB, double \fP\fI*white_x\fP\fB, double \fP\fI*white_y\fP\fB, double \fP\fI*red_x\fP\fB, double \fP\fI*red_y\fP\fB, double \fP\fI*green_x\fP\fB, double \fP\fI*green_y\fP\fB, double \fP\fI*blue_x\fP\fB, double \fI*blue_y\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_cHRM_fixed (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fP\fIinfo_ptr\fP\fB, png_uint_32 \fP\fI*white_x\fP\fB, png_uint_32 \fP\fI*white_y\fP\fB, png_uint_32 \fP\fI*red_x\fP\fB, png_uint_32 \fP\fI*red_y\fP\fB, png_uint_32 \fP\fI*green_x\fP\fB, png_uint_32 \fP\fI*green_y\fP\fB, png_uint_32 \fP\fI*blue_x\fP\fB, png_uint_32 \fI*blue_y\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_cHRM_XYZ (png_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fP\fIinfo_ptr\fP\fB, double \fP\fI*red_X\fP\fB, double \fP\fI*red_Y\fP\fB, double \fP\fI*red_Z\fP\fB, double \fP\fI*green_X\fP\fB, double \fP\fI*green_Y\fP\fB, double \fP\fI*green_Z\fP\fB, double \fP\fI*blue_X\fP\fB, double \fP\fI*blue_Y\fP\fB, double \fI*blue_Z\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_cHRM_XYZ_fixed (png_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fP\fIinfo_ptr\fP\fB, png_fixed_point \fP\fI*int_red_X\fP\fB, png_fixed_point \fP\fI*int_red_Y\fP\fB, png_fixed_point \fP\fI*int_red_Z\fP\fB, png_fixed_point \fP\fI*int_green_X\fP\fB, png_fixed_point \fP\fI*int_green_Y\fP\fB, png_fixed_point \fP\fI*int_green_Z\fP\fB, png_fixed_point \fP\fI*int_blue_X\fP\fB, png_fixed_point \fP\fI*int_blue_Y\fP\fB, png_fixed_point \fI*int_blue_Z\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_chunk_cache_max (png_const_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBpng_alloc_size_t png_get_chunk_malloc_max (png_const_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBpng_byte png_get_color_type (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fIinfo_ptr\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_compression_buffer_size (png_const_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBpng_byte png_get_compression_type (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fIinfo_ptr\fP\fB);\fP ++ ++\fBpng_byte png_get_copyright (png_const_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_current_row_number \fI(png_const_structp\fP\fB);\fP ++ ++\fBpng_byte png_get_current_pass_number \fI(png_const_structp\fP\fB);\fP ++ ++\fBpng_voidp png_get_error_ptr (png_const_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBpng_byte png_get_filter_type (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fIinfo_ptr\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_gAMA (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fP\fIinfo_ptr\fP\fB, double \fI*file_gamma\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_gAMA_fixed (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fP\fIinfo_ptr\fP\fB, png_uint_32 \fI*int_file_gamma\fP\fB);\fP ++ ++\fBpng_byte png_get_header_ver (png_const_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBpng_byte png_get_header_version (png_const_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_eXIf (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fP\fIinfo_ptr\fP\fB, png_bytep \fI*exif\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_eXIf_1 (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fP\fIinfo_ptr\fP\fB, png_unit_32 \fP\fI*num_exif\fP\fB, png_bytep \fI*exif\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_hIST (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fP\fIinfo_ptr\fP\fB, png_uint_16p \fI*hist\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_iCCP (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fP\fIinfo_ptr\fP\fB, png_charpp \fP\fIname\fP\fB, int \fP\fI*compression_type\fP\fB, png_bytepp \fP\fIprofile\fP\fB, png_uint_32 \fI*proflen\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_IHDR (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_uint_32 \fP\fI*width\fP\fB, png_uint_32 \fP\fI*height\fP\fB, int \fP\fI*bit_depth\fP\fB, int \fP\fI*color_type\fP\fB, int \fP\fI*interlace_type\fP\fB, int \fP\fI*compression_type\fP\fB, int \fI*filter_type\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_image_height (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fIinfo_ptr\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_image_width (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fIinfo_ptr\fP\fB);\fP ++ ++\fBpng_int_32 png_get_int_32 (png_bytep \fIbuf\fP\fB);\fP ++ ++\fBpng_byte png_get_interlace_type (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fIinfo_ptr\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_io_chunk_type (png_const_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBpng_voidp png_get_io_ptr (png_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_io_state (png_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBpng_byte png_get_libpng_ver (png_const_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBint png_get_palette_max(png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fIinfo_ptr\fP\fB);\fP ++ ++\fBpng_voidp png_get_mem_ptr (png_const_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_oFFs (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fP\fIinfo_ptr\fP\fB, png_uint_32 \fP\fI*offset_x\fP\fB, png_uint_32 \fP\fI*offset_y\fP\fB, int \fI*unit_type\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_pCAL (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fP\fIinfo_ptr\fP\fB, png_charp \fP\fI*purpose\fP\fB, png_int_32 \fP\fI*X0\fP\fB, png_int_32 \fP\fI*X1\fP\fB, int \fP\fI*type\fP\fB, int \fP\fI*nparams\fP\fB, png_charp \fP\fI*units\fP\fB, png_charpp \fI*params\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_pHYs (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fP\fIinfo_ptr\fP\fB, png_uint_32 \fP\fI*res_x\fP\fB, png_uint_32 \fP\fI*res_y\fP\fB, int \fI*unit_type\fP\fB);\fP ++ ++\fBfloat png_get_pixel_aspect_ratio (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fIinfo_ptr\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_pHYs_dpi (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fP\fIinfo_ptr\fP\fB, png_uint_32 \fP\fI*res_x\fP\fB, png_uint_32 \fP\fI*res_y\fP\fB, int \fI*unit_type\fP\fB);\fP ++ ++\fBpng_fixed_point png_get_pixel_aspect_ratio_fixed (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fIinfo_ptr\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_pixels_per_inch (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fIinfo_ptr\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_pixels_per_meter (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fIinfo_ptr\fP\fB);\fP ++ ++\fBpng_voidp png_get_progressive_ptr (png_const_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_PLTE (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fP\fIinfo_ptr\fP\fB, png_colorp \fP\fI*palette\fP\fB, int \fI*num_palette\fP\fB);\fP ++ ++\fBpng_byte png_get_rgb_to_gray_status (png_const_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_rowbytes (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fIinfo_ptr\fP\fB);\fP ++ ++\fBpng_bytepp png_get_rows (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fIinfo_ptr\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_sBIT (png_const_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_color_8p \fI*sig_bit\fP\fB);\fP ++ ++\fBvoid png_get_sCAL (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fP\fIinfo_ptr\fP\fB, int* \fP\fIunit\fP\fB, double* \fP\fIwidth\fP\fB, double* \fIheight\fP\fB);\fP ++ ++\fBvoid png_get_sCAL_fixed (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fP\fIinfo_ptr\fP\fB, int* \fP\fIunit\fP\fB, png_fixed_pointp \fP\fIwidth\fP\fB, png_fixed_pointp \fIheight\fP\fB);\fP ++ ++\fBvoid png_get_sCAL_s (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fP\fIinfo_ptr\fP\fB, int* \fP\fIunit\fP\fB, png_charpp \fP\fIwidth\fP\fB, png_charpp \fIheight\fP\fB);\fP ++ ++\fBpng_bytep png_get_signature (png_const_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_sPLT (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fP\fIinfo_ptr\fP\fB, png_spalette_p \fI*splt_ptr\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_sRGB (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fP\fIinfo_ptr\fP\fB, int \fI*file_srgb_intent\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_text (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fP\fIinfo_ptr\fP\fB, png_textp \fP\fI*text_ptr\fP\fB, int \fI*num_text\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_tIME (png_const_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_timep \fI*mod_time\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_tRNS (png_const_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_bytep \fP\fI*trans_alpha\fP\fB, int \fP\fI*num_trans\fP\fB, png_color_16p \fI*trans_color\fP\fB);\fP ++ ++\fB/* This function is really an inline macro. \fI*/ ++ ++\fBpng_uint_16 png_get_uint_16 (png_bytep \fIbuf\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_uint_31 (png_structp \fP\fIpng_ptr\fP\fB, png_bytep \fIbuf\fP\fB);\fP ++ ++\fB/* This function is really an inline macro. \fI*/ ++ ++\fBpng_uint_32 png_get_uint_32 (png_bytep \fIbuf\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_unknown_chunks (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fP\fIinfo_ptr\fP\fB, png_unknown_chunkpp \fIunknowns\fP\fB);\fP ++ ++\fBpng_voidp png_get_user_chunk_ptr (png_const_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_user_height_max (png_const_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBpng_voidp png_get_user_transform_ptr (png_const_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_user_width_max (png_const_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_valid (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fP\fIinfo_ptr\fP\fB, png_uint_32 \fIflag\fP\fB);\fP ++ ++\fBfloat png_get_x_offset_inches (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fIinfo_ptr\fP\fB);\fP ++ ++\fBpng_fixed_point png_get_x_offset_inches_fixed (png_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fIinfo_ptr\fP\fB);\fP ++ ++\fBpng_int_32 png_get_x_offset_microns (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fIinfo_ptr\fP\fB);\fP ++ ++\fBpng_int_32 png_get_x_offset_pixels (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fIinfo_ptr\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_x_pixels_per_inch (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fIinfo_ptr\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_x_pixels_per_meter (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fIinfo_ptr\fP\fB);\fP ++ ++\fBfloat png_get_y_offset_inches (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fIinfo_ptr\fP\fB);\fP ++ ++\fBpng_fixed_point png_get_y_offset_inches_fixed (png_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fIinfo_ptr\fP\fB);\fP ++ ++\fBpng_int_32 png_get_y_offset_microns (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fIinfo_ptr\fP\fB);\fP ++ ++\fBpng_int_32 png_get_y_offset_pixels (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fIinfo_ptr\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_y_pixels_per_inch (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fIinfo_ptr\fP\fB);\fP ++ ++\fBpng_uint_32 png_get_y_pixels_per_meter (png_const_structp \fP\fIpng_ptr\fP\fB, png_const_infop \fIinfo_ptr\fP\fB);\fP ++ ++\fBint png_handle_as_unknown (png_structp \fP\fIpng_ptr\fP\fB, png_bytep \fIchunk_name\fP\fB);\fP ++ ++\fBint png_image_begin_read_from_file (png_imagep \fP\fIimage\fP\fB, const char \fI*file_name\fP\fB);\fP ++ ++\fBint png_image_begin_read_from_stdio (png_imagep \fP\fIimage\fP\fB, FILE* \fIfile\fP\fB);\fP ++ ++\fBint, png_image_begin_read_from_memory (png_imagep \fP\fIimage\fP\fB, png_const_voidp \fP\fImemory\fP\fB, size_t \fIsize\fP\fB);\fP ++ ++\fBint png_image_finish_read (png_imagep \fP\fIimage\fP\fB, png_colorp \fP\fIbackground\fP\fB, void \fP\fI*buffer\fP\fB, png_int_32 \fP\fIrow_stride\fP\fB, void \fI*colormap\fP\fB);\fP ++ ++\fBvoid png_image_free (png_imagep \fIimage\fP\fB);\fP ++ ++\fBint png_image_write_to_file (png_imagep \fP\fIimage\fP\fB, const char \fP\fI*file\fP\fB, int \fP\fIconvert_to_8bit\fP\fB, const void \fP\fI*buffer\fP\fB, png_int_32 \fP\fIrow_stride\fP\fB, void \fI*colormap\fP\fB);\fP ++ ++\fBint png_image_write_to_memory (png_imagep \fP\fIimage\fP\fB, void \fP\fI*memory\fP\fB, png_alloc_size_t * PNG_RESTRICT \fP\fImemory_bytes\fP\fB, int \fP\fIconvert_to_8_bit\fP\fB, const void \fP\fI*buffer\fP\fB, png_int_32 \fP\fIrow_stride\fP\fB, const void \fI*colormap\fP\fB);\fP ++ ++\fBint png_image_write_to_stdio (png_imagep \fP\fIimage\fP\fB, FILE \fP\fI*file\fP\fB, int \fP\fIconvert_to_8_bit\fP\fB, const void \fP\fI*buffer\fP\fB, png_int_32 \fP\fIrow_stride\fP\fB, void \fI*colormap\fP\fB);\fP ++ ++\fBvoid png_info_init_3 (png_infopp \fP\fIinfo_ptr\fP\fB, size_t \fIpng_info_struct_size\fP\fB);\fP ++ ++\fBvoid png_init_io (png_structp \fP\fIpng_ptr\fP\fB, FILE \fI*fp\fP\fB);\fP ++ ++\fBvoid png_longjmp (png_structp \fP\fIpng_ptr\fP\fB, int \fIval\fP\fB);\fP ++ ++\fBpng_voidp png_malloc (png_structp \fP\fIpng_ptr\fP\fB, png_alloc_size_t \fIsize\fP\fB);\fP ++ ++\fBpng_voidp png_malloc_default (png_structp \fP\fIpng_ptr\fP\fB, png_alloc_size_t \fIsize\fP\fB);\fP ++ ++\fBpng_voidp png_malloc_warn (png_structp \fP\fIpng_ptr\fP\fB, png_alloc_size_t \fIsize\fP\fB);\fP ++ ++\fBpng_uint_32 png_permit_mng_features (png_structp \fP\fIpng_ptr\fP\fB, png_uint_32 \fImng_features_permitted\fP\fB);\fP ++ ++\fBvoid png_process_data (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_bytep \fP\fIbuffer\fP\fB, size_t \fIbuffer_size\fP\fB);\fP ++ ++\fBsize_t png_process_data_pause (png_structp \fP\fIpng_ptr\fP\fB, int \fIsave\fP\fB);\fP ++ ++\fBpng_uint_32 png_process_data_skip (png_structp \fP\fIpng_ptr\fP\fB);\fP ++ ++\fBvoid png_progressive_combine_row (png_structp \fP\fIpng_ptr\fP\fB, png_bytep \fP\fIold_row\fP\fB, png_bytep \fInew_row\fP\fB);\fP ++ ++\fBvoid png_read_end (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP ++ ++\fBvoid png_read_image (png_structp \fP\fIpng_ptr\fP\fB, png_bytepp \fIimage\fP\fB);\fP ++ ++\fBvoid png_read_info (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP ++ ++\fBvoid png_read_png (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, int \fP\fItransforms\fP\fB, png_voidp \fIparams\fP\fB);\fP ++ ++\fBvoid png_read_row (png_structp \fP\fIpng_ptr\fP\fB, png_bytep \fP\fIrow\fP\fB, png_bytep \fIdisplay_row\fP\fB);\fP ++ ++\fBvoid png_read_rows (png_structp \fP\fIpng_ptr\fP\fB, png_bytepp \fP\fIrow\fP\fB, png_bytepp \fP\fIdisplay_row\fP\fB, png_uint_32 \fInum_rows\fP\fB);\fP ++ ++\fBvoid png_read_update_info (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP ++ ++\fBint png_reset_zstream (png_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBvoid png_save_int_32 (png_bytep \fP\fIbuf\fP\fB, png_int_32 \fIi\fP\fB);\fP ++ ++\fBvoid png_save_uint_16 (png_bytep \fP\fIbuf\fP\fB, unsigned int \fIi\fP\fB);\fP ++ ++\fBvoid png_save_uint_32 (png_bytep \fP\fIbuf\fP\fB, png_uint_32 \fIi\fP\fB);\fP ++ ++\fBvoid png_set_add_alpha (png_structp \fP\fIpng_ptr\fP\fB, png_uint_32 \fP\fIfiller\fP\fB, int \fIflags\fP\fB);\fP ++ ++\fBvoid png_set_alpha_mode (png_structp \fP\fIpng_ptr\fP\fB, int \fP\fImode\fP\fB, double \fIoutput_gamma\fP\fB);\fP ++ ++\fBvoid png_set_alpha_mode_fixed (png_structp \fP\fIpng_ptr\fP\fB, int \fP\fImode\fP\fB, png_fixed_point \fIoutput_gamma\fP\fB);\fP ++ ++\fBvoid png_set_background (png_structp \fP\fIpng_ptr\fP\fB, png_color_16p \fP\fIbackground_color\fP\fB, int \fP\fIbackground_gamma_code\fP\fB, int \fP\fIneed_expand\fP\fB, double \fIbackground_gamma\fP\fB);\fP ++ ++\fBvoid png_set_background_fixed (png_structp \fP\fIpng_ptr\fP\fB, png_color_16p \fP\fIbackground_color\fP\fB, int \fP\fIbackground_gamma_code\fP\fB, int \fP\fIneed_expand\fP\fB, png_uint_32 \fIbackground_gamma\fP\fB);\fP ++ ++\fBvoid png_set_benign_errors (png_structp \fP\fIpng_ptr\fP\fB, int \fIallowed\fP\fB);\fP ++ ++\fBvoid png_set_bgr (png_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBvoid png_set_bKGD (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_color_16p \fIbackground\fP\fB);\fP ++ ++\fBvoid png_set_check_for_invalid_index (png_structrp \fP\fIpng_ptr\fP\fB, int \fIallowed\fP\fB);\fP ++ ++\fBvoid png_set_cHRM (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, double \fP\fIwhite_x\fP\fB, double \fP\fIwhite_y\fP\fB, double \fP\fIred_x\fP\fB, double \fP\fIred_y\fP\fB, double \fP\fIgreen_x\fP\fB, double \fP\fIgreen_y\fP\fB, double \fP\fIblue_x\fP\fB, double \fIblue_y\fP\fB);\fP ++ ++\fBvoid png_set_cHRM_fixed (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_uint_32 \fP\fIwhite_x\fP\fB, png_uint_32 \fP\fIwhite_y\fP\fB, png_uint_32 \fP\fIred_x\fP\fB, png_uint_32 \fP\fIred_y\fP\fB, png_uint_32 \fP\fIgreen_x\fP\fB, png_uint_32 \fP\fIgreen_y\fP\fB, png_uint_32 \fP\fIblue_x\fP\fB, png_uint_32 \fIblue_y\fP\fB);\fP ++ ++\fBvoid png_set_cHRM_XYZ (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, double \fP\fIred_X\fP\fB, double \fP\fIred_Y\fP\fB, double \fP\fIred_Z\fP\fB, double \fP\fIgreen_X\fP\fB, double \fP\fIgreen_Y\fP\fB, double \fP\fIgreen_Z\fP\fB, double \fP\fIblue_X\fP\fB, double \fP\fIblue_Y\fP\fB, double \fIblue_Z\fP\fB);\fP ++ ++\fBvoid png_set_cHRM_XYZ_fixed (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_fixed_point \fP\fIint_red_X\fP\fB, png_fixed_point \fP\fIint_red_Y\fP\fB, png_fixed_point \fP\fIint_red_Z\fP\fB, png_fixed_point \fP\fIint_green_X\fP\fB, png_fixed_point \fP\fIint_green_Y\fP\fB, png_fixed_point \fP\fIint_green_Z\fP\fB, png_fixed_point \fP\fIint_blue_X\fP\fB, png_fixed_point \fP\fIint_blue_Y\fP\fB, png_fixed_point \fIint_blue_Z\fP\fB);\fP ++ ++\fBvoid png_set_chunk_cache_max (png_structp \fP\fIpng_ptr\fP\fB, png_uint_32 \fIuser_chunk_cache_max\fP\fB);\fP ++ ++\fBvoid png_set_compression_level (png_structp \fP\fIpng_ptr\fP\fB, int \fIlevel\fP\fB);\fP ++ ++\fBvoid png_set_compression_mem_level (png_structp \fP\fIpng_ptr\fP\fB, int \fImem_level\fP\fB);\fP ++ ++\fBvoid png_set_compression_method (png_structp \fP\fIpng_ptr\fP\fB, int \fImethod\fP\fB);\fP ++ ++\fBvoid png_set_compression_strategy (png_structp \fP\fIpng_ptr\fP\fB, int \fIstrategy\fP\fB);\fP ++ ++\fBvoid png_set_compression_window_bits (png_structp \fP\fIpng_ptr\fP\fB, int \fIwindow_bits\fP\fB);\fP ++ ++\fBvoid png_set_crc_action (png_structp \fP\fIpng_ptr\fP\fB, int \fP\fIcrit_action\fP\fB, int \fIancil_action\fP\fB);\fP ++ ++\fBvoid png_set_error_fn (png_structp \fP\fIpng_ptr\fP\fB, png_voidp \fP\fIerror_ptr\fP\fB, png_error_ptr \fP\fIerror_fn\fP\fB, png_error_ptr \fIwarning_fn\fP\fB);\fP ++ ++\fBvoid png_set_expand (png_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBvoid png_set_expand_16 (png_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBvoid png_set_expand_gray_1_2_4_to_8 (png_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBvoid png_set_filler (png_structp \fP\fIpng_ptr\fP\fB, png_uint_32 \fP\fIfiller\fP\fB, int \fIflags\fP\fB);\fP ++ ++\fBvoid png_set_filter (png_structp \fP\fIpng_ptr\fP\fB, int \fP\fImethod\fP\fB, int \fIfilters\fP\fB);\fP ++ ++\fBvoid png_set_filter_heuristics (png_structp \fP\fIpng_ptr\fP\fB, int \fP\fIheuristic_method\fP\fB, int \fP\fInum_weights\fP\fB, png_doublep \fP\fIfilter_weights\fP\fB, png_doublep \fIfilter_costs\fP\fB);\fP ++ ++\fBvoid png_set_filter_heuristics_fixed (png_structp \fP\fIpng_ptr\fP\fB, int \fP\fIheuristic_method\fP\fB, int \fP\fInum_weights\fP\fB, png_fixed_point_p \fP\fIfilter_weights\fP\fB, png_fixed_point_p \fIfilter_costs\fP\fB);\fP ++ ++\fBvoid png_set_flush (png_structp \fP\fIpng_ptr\fP\fB, int \fInrows\fP\fB);\fP ++ ++\fBvoid png_set_gamma (png_structp \fP\fIpng_ptr\fP\fB, double \fP\fIscreen_gamma\fP\fB, double \fIdefault_file_gamma\fP\fB);\fP ++ ++\fBvoid png_set_gamma_fixed (png_structp \fP\fIpng_ptr\fP\fB, png_uint_32 \fP\fIscreen_gamma\fP\fB, png_uint_32 \fIdefault_file_gamma\fP\fB);\fP ++ ++\fBvoid png_set_gAMA (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, double \fIfile_gamma\fP\fB);\fP ++ ++\fBvoid png_set_gAMA_fixed (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_uint_32 \fIfile_gamma\fP\fB);\fP ++ ++\fBvoid png_set_gray_1_2_4_to_8 (png_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBvoid png_set_gray_to_rgb (png_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBvoid png_set_eXIf (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_bytep \fIexif\fP\fB);\fP ++ ++\fBvoid png_set_eXIf_1 (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_uint_32 \fP\fInum_exif\fP\fB, png_bytep \fIexif\fP\fB);\fP ++ ++\fBvoid png_set_hIST (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_uint_16p \fIhist\fP\fB);\fP ++ ++\fBvoid png_set_iCCP (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_const_charp \fP\fIname\fP\fB, int \fP\fIcompression_type\fP\fB, png_const_bytep \fP\fIprofile\fP\fB, png_uint_32 \fIproflen\fP\fB);\fP ++ ++\fBint png_set_interlace_handling (png_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBvoid png_set_invalid (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, int \fImask\fP\fB);\fP ++ ++\fBvoid png_set_invert_alpha (png_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBvoid png_set_invert_mono (png_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBvoid png_set_IHDR (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_uint_32 \fP\fIwidth\fP\fB, png_uint_32 \fP\fIheight\fP\fB, int \fP\fIbit_depth\fP\fB, int \fP\fIcolor_type\fP\fB, int \fP\fIinterlace_type\fP\fB, int \fP\fIcompression_type\fP\fB, int \fIfilter_type\fP\fB);\fP ++ ++\fBvoid png_set_keep_unknown_chunks (png_structp \fP\fIpng_ptr\fP\fB, int \fP\fIkeep\fP\fB, png_bytep \fP\fIchunk_list\fP\fB, int \fInum_chunks\fP\fB);\fP ++ ++\fBjmp_buf* png_set_longjmp_fn (png_structp \fP\fIpng_ptr\fP\fB, png_longjmp_ptr \fP\fIlongjmp_fn\fP\fB, size_t \fIjmp_buf_size\fP\fB);\fP ++ ++\fBvoid png_set_chunk_malloc_max (png_structp \fP\fIpng_ptr\fP\fB, png_alloc_size_t \fIuser_chunk_cache_max\fP\fB);\fP ++ ++\fBvoid png_set_compression_buffer_size (png_structp \fP\fIpng_ptr\fP\fB, png_uint_32 \fIsize\fP\fB);\fP ++ ++\fBvoid png_set_mem_fn (png_structp \fP\fIpng_ptr\fP\fB, png_voidp \fP\fImem_ptr\fP\fB, png_malloc_ptr \fP\fImalloc_fn\fP\fB, png_free_ptr \fIfree_fn\fP\fB);\fP ++ ++\fBvoid png_set_oFFs (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_uint_32 \fP\fIoffset_x\fP\fB, png_uint_32 \fP\fIoffset_y\fP\fB, int \fIunit_type\fP\fB);\fP ++ ++\fBint png_set_option(png_structrp \fP\fIpng_ptr\fP\fB, int \fP\fIoption\fP\fB, int \fIonoff\fP\fB);\fP ++ ++\fBvoid png_set_packing (png_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBvoid png_set_packswap (png_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBvoid png_set_palette_to_rgb (png_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBvoid png_set_pCAL (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_charp \fP\fIpurpose\fP\fB, png_int_32 \fP\fIX0\fP\fB, png_int_32 \fP\fIX1\fP\fB, int \fP\fItype\fP\fB, int \fP\fInparams\fP\fB, png_charp \fP\fIunits\fP\fB, png_charpp \fIparams\fP\fB);\fP ++ ++\fBvoid png_set_pHYs (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_uint_32 \fP\fIres_x\fP\fB, png_uint_32 \fP\fIres_y\fP\fB, int \fIunit_type\fP\fB);\fP ++ ++\fBvoid png_set_progressive_read_fn (png_structp \fP\fIpng_ptr\fP\fB, png_voidp \fP\fIprogressive_ptr\fP\fB, png_progressive_info_ptr \fP\fIinfo_fn\fP\fB, png_progressive_row_ptr \fP\fIrow_fn\fP\fB, png_progressive_end_ptr \fIend_fn\fP\fB);\fP ++ ++\fBvoid png_set_PLTE (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_colorp \fP\fIpalette\fP\fB, int \fInum_palette\fP\fB);\fP ++ ++\fBvoid png_set_quantize (png_structp \fP\fIpng_ptr\fP\fB, png_colorp \fP\fIpalette\fP\fB, int \fP\fInum_palette\fP\fB, int \fP\fImaximum_colors\fP\fB, png_uint_16p \fP\fIhistogram\fP\fB, int \fIfull_quantize\fP\fB);\fP ++ ++\fBvoid png_set_read_fn (png_structp \fP\fIpng_ptr\fP\fB, png_voidp \fP\fIio_ptr\fP\fB, png_rw_ptr \fIread_data_fn\fP\fB);\fP ++ ++\fBvoid png_set_read_status_fn (png_structp \fP\fIpng_ptr\fP\fB, png_read_status_ptr \fIread_row_fn\fP\fB);\fP ++ ++\fBvoid png_set_read_user_chunk_fn (png_structp \fP\fIpng_ptr\fP\fB, png_voidp \fP\fIuser_chunk_ptr\fP\fB, png_user_chunk_ptr \fIread_user_chunk_fn\fP\fB);\fP ++ ++\fBvoid png_set_read_user_transform_fn (png_structp \fP\fIpng_ptr\fP\fB, png_user_transform_ptr \fIread_user_transform_fn\fP\fB);\fP ++ ++\fBvoid png_set_rgb_to_gray (png_structp \fP\fIpng_ptr\fP\fB, int \fP\fIerror_action\fP\fB, double \fP\fIred\fP\fB, double \fIgreen\fP\fB);\fP ++ ++\fBvoid png_set_rgb_to_gray_fixed (png_structp \fP\fIpng_ptr\fP\fB, int error_action png_uint_32 \fP\fIred\fP\fB, png_uint_32 \fIgreen\fP\fB);\fP ++ ++\fBvoid png_set_rows (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_bytepp \fIrow_pointers\fP\fB);\fP ++ ++\fBvoid png_set_sBIT (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_color_8p \fIsig_bit\fP\fB);\fP ++ ++\fBvoid png_set_sCAL (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, int \fP\fIunit\fP\fB, double \fP\fIwidth\fP\fB, double \fIheight\fP\fB);\fP ++ ++\fBvoid png_set_sCAL_fixed (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, int \fP\fIunit\fP\fB, png_fixed_point \fP\fIwidth\fP\fB, png_fixed_point \fIheight\fP\fB);\fP ++ ++\fBvoid png_set_sCAL_s (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, int \fP\fIunit\fP\fB, png_charp \fP\fIwidth\fP\fB, png_charp \fIheight\fP\fB);\fP ++ ++\fBvoid png_set_scale_16 (png_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBvoid png_set_shift (png_structp \fP\fIpng_ptr\fP\fB, png_color_8p \fItrue_bits\fP\fB);\fP ++ ++\fBvoid png_set_sig_bytes (png_structp \fP\fIpng_ptr\fP\fB, int \fInum_bytes\fP\fB);\fP ++ ++\fBvoid png_set_sPLT (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_spalette_p \fP\fIsplt_ptr\fP\fB, int \fInum_spalettes\fP\fB);\fP ++ ++\fBvoid png_set_sRGB (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, int \fIsrgb_intent\fP\fB);\fP ++ ++\fBvoid png_set_sRGB_gAMA_and_cHRM (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, int \fIsrgb_intent\fP\fB);\fP ++ ++\fBvoid png_set_strip_16 (png_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBvoid png_set_strip_alpha (png_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBvoid png_set_strip_error_numbers (png_structp \fP\fIpng_ptr\fP\fB, png_uint_32 \fIstrip_mode\fP\fB);\fP ++ ++\fBvoid png_set_swap (png_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBvoid png_set_swap_alpha (png_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBvoid png_set_text (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_textp \fP\fItext_ptr\fP\fB, int \fInum_text\fP\fB);\fP ++ ++\fBvoid png_set_text_compression_level (png_structp \fP\fIpng_ptr\fP\fB, int \fIlevel\fP\fB);\fP ++ ++\fBvoid png_set_text_compression_mem_level (png_structp \fP\fIpng_ptr\fP\fB, int \fImem_level\fP\fB);\fP ++ ++\fBvoid png_set_text_compression_strategy (png_structp \fP\fIpng_ptr\fP\fB, int \fIstrategy\fP\fB);\fP ++ ++\fBvoid png_set_text_compression_window_bits (png_structp \fP\fIpng_ptr\fP\fB, int \fIwindow_bits\fP\fB);\fP ++ ++\fBvoid png_set_text_compression_method (png_structp \fP\fIpng_ptr\fP\fB, int \fImethod\fP\fB);\fP ++ ++\fBvoid png_set_tIME (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_timep \fImod_time\fP\fB);\fP ++ ++\fBvoid png_set_tRNS (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_bytep \fP\fItrans_alpha\fP\fB, int \fP\fInum_trans\fP\fB, png_color_16p \fItrans_color\fP\fB);\fP ++ ++\fBvoid png_set_tRNS_to_alpha (png_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBpng_uint_32 png_set_unknown_chunks (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, png_unknown_chunkp \fP\fIunknowns\fP\fB, int \fP\fInum\fP\fB, int \fIlocation\fP\fB);\fP ++ ++\fBvoid png_set_unknown_chunk_location (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, int \fP\fIchunk\fP\fB, int \fIlocation\fP\fB);\fP ++ ++\fBvoid png_set_user_limits (png_structp \fP\fIpng_ptr\fP\fB, png_uint_32 \fP\fIuser_width_max\fP\fB, png_uint_32 \fIuser_height_max\fP\fB);\fP ++ ++\fBvoid png_set_user_transform_info (png_structp \fP\fIpng_ptr\fP\fB, png_voidp \fP\fIuser_transform_ptr\fP\fB, int \fP\fIuser_transform_depth\fP\fB, int \fIuser_transform_channels\fP\fB);\fP ++ ++\fBvoid png_set_write_fn (png_structp \fP\fIpng_ptr\fP\fB, png_voidp \fP\fIio_ptr\fP\fB, png_rw_ptr \fP\fIwrite_data_fn\fP\fB, png_flush_ptr \fIoutput_flush_fn\fP\fB);\fP ++ ++\fBvoid png_set_write_status_fn (png_structp \fP\fIpng_ptr\fP\fB, png_write_status_ptr \fIwrite_row_fn\fP\fB);\fP ++ ++\fBvoid png_set_write_user_transform_fn (png_structp \fP\fIpng_ptr\fP\fB, png_user_transform_ptr \fIwrite_user_transform_fn\fP\fB);\fP ++ ++\fBint png_sig_cmp (png_bytep \fP\fIsig\fP\fB, size_t \fP\fIstart\fP\fB, size_t \fInum_to_check\fP\fB);\fP ++ ++\fBvoid png_start_read_image (png_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBvoid png_warning (png_structp \fP\fIpng_ptr\fP\fB, png_const_charp \fImessage\fP\fB);\fP ++ ++\fBvoid png_write_chunk (png_structp \fP\fIpng_ptr\fP\fB, png_bytep \fP\fIchunk_name\fP\fB, png_bytep \fP\fIdata\fP\fB, size_t \fIlength\fP\fB);\fP ++ ++\fBvoid png_write_chunk_data (png_structp \fP\fIpng_ptr\fP\fB, png_bytep \fP\fIdata\fP\fB, size_t \fIlength\fP\fB);\fP ++ ++\fBvoid png_write_chunk_end (png_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBvoid png_write_chunk_start (png_structp \fP\fIpng_ptr\fP\fB, png_bytep \fP\fIchunk_name\fP\fB, png_uint_32 \fIlength\fP\fB);\fP ++ ++\fBvoid png_write_end (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP ++ ++\fBvoid png_write_flush (png_structp \fIpng_ptr\fP\fB);\fP ++ ++\fBvoid png_write_image (png_structp \fP\fIpng_ptr\fP\fB, png_bytepp \fIimage\fP\fB);\fP ++ ++\fBvoid png_write_info (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP ++ ++\fBvoid png_write_info_before_PLTE (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fIinfo_ptr\fP\fB);\fP ++ ++\fBvoid png_write_png (png_structp \fP\fIpng_ptr\fP\fB, png_infop \fP\fIinfo_ptr\fP\fB, int \fP\fItransforms\fP\fB, png_voidp \fIparams\fP\fB);\fP ++ ++\fBvoid png_write_row (png_structp \fP\fIpng_ptr\fP\fB, png_bytep \fIrow\fP\fB);\fP ++ ++\fBvoid png_write_rows (png_structp \fP\fIpng_ptr\fP\fB, png_bytepp \fP\fIrow\fP\fB, png_uint_32 \fInum_rows\fP\fB);\fP ++ ++\fBvoid png_write_sig (png_structp \fIpng_ptr\fP\fB);\fP ++ ++.SH DESCRIPTION ++The ++.I libpng ++library supports encoding, decoding, and various manipulations of ++the Portable Network Graphics (PNG) format image files. It uses the ++.IR zlib(3) ++compression library. ++Following is a copy of the libpng-manual.txt file that accompanies libpng. ++ ++.SH LIBPNG.TXT ++libpng-manual.txt - A description on how to use and modify libpng ++ ++ Copyright (c) 2018-2019 Cosmin Truta ++ Copyright (c) 1998-2018 Glenn Randers-Pehrson ++ ++ This document is released under the libpng license. ++ For conditions of distribution and use, see the disclaimer ++ and license in png.h ++ ++ Based on: ++ ++ libpng version 1.6.36, December 2018, through 1.6.37 - April 2019 ++ Updated and distributed by Cosmin Truta ++ Copyright (c) 2018-2019 Cosmin Truta ++ ++ libpng versions 0.97, January 1998, through 1.6.35 - July 2018 ++ Updated and distributed by Glenn Randers-Pehrson ++ Copyright (c) 1998-2018 Glenn Randers-Pehrson ++ ++ libpng 1.0 beta 6 - version 0.96 - May 28, 1997 ++ Updated and distributed by Andreas Dilger ++ Copyright (c) 1996, 1997 Andreas Dilger ++ ++ libpng 1.0 beta 2 - version 0.88 - January 26, 1996 ++ For conditions of distribution and use, see copyright ++ notice in png.h. Copyright (c) 1995, 1996 Guy Eric ++ Schalnat, Group 42, Inc. ++ ++ Updated/rewritten per request in the libpng FAQ ++ Copyright (c) 1995, 1996 Frank J. T. Wojcik ++ December 18, 1995 & January 20, 1996 ++ ++ TABLE OF CONTENTS ++ ++ I. Introduction ++ II. Structures ++ III. Reading ++ IV. Writing ++ V. Simplified API ++ VI. Modifying/Customizing libpng ++ VII. MNG support ++ VIII. Changes to Libpng from version 0.88 ++ IX. Changes to Libpng from version 1.0.x to 1.2.x ++ X. Changes to Libpng from version 1.0.x/1.2.x to 1.4.x ++ XI. Changes to Libpng from version 1.4.x to 1.5.x ++ XII. Changes to Libpng from version 1.5.x to 1.6.x ++ XIII. Detecting libpng ++ XIV. Source code repository ++ XV. Coding style ++ ++.SH I. Introduction ++ ++This file describes how to use and modify the PNG reference library ++(known as libpng) for your own use. In addition to this ++file, example.c is a good starting point for using the library, as ++it is heavily commented and should include everything most people ++will need. We assume that libpng is already installed; see the ++INSTALL file for instructions on how to configure and install libpng. ++ ++For examples of libpng usage, see the files "example.c", "pngtest.c", ++and the files in the "contrib" directory, all of which are included in ++the libpng distribution. ++ ++Libpng was written as a companion to the PNG specification, as a way ++of reducing the amount of time and effort it takes to support the PNG ++file format in application programs. ++ ++The PNG specification (second edition), November 2003, is available as ++a W3C Recommendation and as an ISO Standard (ISO/IEC 15948:2004 (E)) at ++. ++The W3C and ISO documents have identical technical content. ++ ++The PNG-1.2 specification is available at ++. ++It is technically equivalent ++to the PNG specification (second edition) but has some additional material. ++ ++The PNG-1.0 specification is available as RFC 2083 at ++ and as a ++W3C Recommendation at . ++ ++Some additional chunks are described in the special-purpose public chunks ++documents at ++ ++Other information ++about PNG, and the latest version of libpng, can be found at the PNG home ++page, . ++ ++Most users will not have to modify the library significantly; advanced ++users may want to modify it more. All attempts were made to make it as ++complete as possible, while keeping the code easy to understand. ++Currently, this library only supports C. Support for other languages ++is being considered. ++ ++Libpng has been designed to handle multiple sessions at one time, ++to be easily modifiable, to be portable to the vast majority of ++machines (ANSI, K&R, 16-, 32-, and 64-bit) available, and to be easy ++to use. The ultimate goal of libpng is to promote the acceptance of ++the PNG file format in whatever way possible. While there is still ++work to be done (see the TODO file), libpng should cover the ++majority of the needs of its users. ++ ++Libpng uses zlib for its compression and decompression of PNG files. ++Further information about zlib, and the latest version of zlib, can ++be found at the zlib home page, . ++The zlib compression utility is a general purpose utility that is ++useful for more than PNG files, and can be used without libpng. ++See the documentation delivered with zlib for more details. ++You can usually find the source files for the zlib utility wherever you ++find the libpng source files. ++ ++Libpng is thread safe, provided the threads are using different ++instances of the structures. Each thread should have its own ++png_struct and png_info instances, and thus its own image. ++Libpng does not protect itself against two threads using the ++same instance of a structure. ++ ++.SH II. Structures ++ ++There are two main structures that are important to libpng, png_struct ++and png_info. Both are internal structures that are no longer exposed ++in the libpng interface (as of libpng 1.5.0). ++ ++The png_info structure is designed to provide information about the ++PNG file. At one time, the fields of png_info were intended to be ++directly accessible to the user. However, this tended to cause problems ++with applications using dynamically loaded libraries, and as a result ++a set of interface functions for png_info (the png_get_*() and png_set_*() ++functions) was developed, and direct access to the png_info fields was ++deprecated.. ++ ++The png_struct structure is the object used by the library to decode a ++single image. As of 1.5.0 this structure is also not exposed. ++ ++Almost all libpng APIs require a pointer to a png_struct as the first argument. ++Many (in particular the png_set and png_get APIs) also require a pointer ++to png_info as the second argument. Some application visible macros ++defined in png.h designed for basic data access (reading and writing ++integers in the PNG format) don't take a png_info pointer, but it's almost ++always safe to assume that a (png_struct*) has to be passed to call an API ++function. ++ ++You can have more than one png_info structure associated with an image, ++as illustrated in pngtest.c, one for information valid prior to the ++IDAT chunks and another (called "end_info" below) for things after them. ++ ++The png.h header file is an invaluable reference for programming with libpng. ++And while I'm on the topic, make sure you include the libpng header file: ++ ++#include ++ ++and also (as of libpng-1.5.0) the zlib header file, if you need it: ++ ++#include ++ ++.SS Types ++ ++The png.h header file defines a number of integral types used by the ++APIs. Most of these are fairly obvious; for example types corresponding ++to integers of particular sizes and types for passing color values. ++ ++One exception is how non-integral numbers are handled. For application ++convenience most APIs that take such numbers have C (double) arguments; ++however, internally PNG, and libpng, use 32 bit signed integers and encode ++the value by multiplying by 100,000. As of libpng 1.5.0 a convenience ++macro PNG_FP_1 is defined in png.h along with a type (png_fixed_point) ++which is simply (png_int_32). ++ ++All APIs that take (double) arguments also have a matching API that ++takes the corresponding fixed point integer arguments. The fixed point ++API has the same name as the floating point one with "_fixed" appended. ++The actual range of values permitted in the APIs is frequently less than ++the full range of (png_fixed_point) (\-21474 to +21474). When APIs require ++a non-negative argument the type is recorded as png_uint_32 above. Consult ++the header file and the text below for more information. ++ ++Special care must be take with sCAL chunk handling because the chunk itself ++uses non-integral values encoded as strings containing decimal floating point ++numbers. See the comments in the header file. ++ ++.SS Configuration ++ ++The main header file function declarations are frequently protected by C ++preprocessing directives of the form: ++ ++ #ifdef PNG_feature_SUPPORTED ++ declare-function ++ #endif ++ ... ++ #ifdef PNG_feature_SUPPORTED ++ use-function ++ #endif ++ ++The library can be built without support for these APIs, although a ++standard build will have all implemented APIs. Application programs ++should check the feature macros before using an API for maximum ++portability. From libpng 1.5.0 the feature macros set during the build ++of libpng are recorded in the header file "pnglibconf.h" and this file ++is always included by png.h. ++ ++If you don't need to change the library configuration from the default, skip to ++the next section ("Reading"). ++ ++Notice that some of the makefiles in the 'scripts' directory and (in 1.5.0) all ++of the build project files in the 'projects' directory simply copy ++scripts/pnglibconf.h.prebuilt to pnglibconf.h. This means that these build ++systems do not permit easy auto-configuration of the library - they only ++support the default configuration. ++ ++The easiest way to make minor changes to the libpng configuration when ++auto-configuration is supported is to add definitions to the command line ++using (typically) CPPFLAGS. For example: ++ ++CPPFLAGS=\-DPNG_NO_FLOATING_ARITHMETIC ++ ++will change the internal libpng math implementation for gamma correction and ++other arithmetic calculations to fixed point, avoiding the need for fast ++floating point support. The result can be seen in the generated pnglibconf.h - ++make sure it contains the changed feature macro setting. ++ ++If you need to make more extensive configuration changes - more than one or two ++feature macro settings - you can either add \-DPNG_USER_CONFIG to the build ++command line and put a list of feature macro settings in pngusr.h or you can set ++DFA_XTRA (a makefile variable) to a file containing the same information in the ++form of 'option' settings. ++ ++A. Changing pnglibconf.h ++ ++A variety of methods exist to build libpng. Not all of these support ++reconfiguration of pnglibconf.h. To reconfigure pnglibconf.h it must either be ++rebuilt from scripts/pnglibconf.dfa using awk or it must be edited by hand. ++ ++Hand editing is achieved by copying scripts/pnglibconf.h.prebuilt to ++pnglibconf.h and changing the lines defining the supported features, paying ++very close attention to the 'option' information in scripts/pnglibconf.dfa ++that describes those features and their requirements. This is easy to get ++wrong. ++ ++B. Configuration using DFA_XTRA ++ ++Rebuilding from pnglibconf.dfa is easy if a functioning 'awk', or a later ++variant such as 'nawk' or 'gawk', is available. The configure build will ++automatically find an appropriate awk and build pnglibconf.h. ++The scripts/pnglibconf.mak file contains a set of make rules for doing the ++same thing if configure is not used, and many of the makefiles in the scripts ++directory use this approach. ++ ++When rebuilding simply write a new file containing changed options and set ++DFA_XTRA to the name of this file. This causes the build to append the new file ++to the end of scripts/pnglibconf.dfa. The pngusr.dfa file should contain lines ++of the following forms: ++ ++everything = off ++ ++This turns all optional features off. Include it at the start of pngusr.dfa to ++make it easier to build a minimal configuration. You will need to turn at least ++some features on afterward to enable either reading or writing code, or both. ++ ++option feature on ++option feature off ++ ++Enable or disable a single feature. This will automatically enable other ++features required by a feature that is turned on or disable other features that ++require a feature which is turned off. Conflicting settings will cause an error ++message to be emitted by awk. ++ ++setting feature default value ++ ++Changes the default value of setting 'feature' to 'value'. There are a small ++number of settings listed at the top of pnglibconf.h, they are documented in the ++source code. Most of these values have performance implications for the library ++but most of them have no visible effect on the API. Some can also be overridden ++from the API. ++ ++This method of building a customized pnglibconf.h is illustrated in ++contrib/pngminim/*. See the "$(PNGCONF):" target in the makefile and ++pngusr.dfa in these directories. ++ ++C. Configuration using PNG_USER_CONFIG ++ ++If \-DPNG_USER_CONFIG is added to the CPPFLAGS when pnglibconf.h is built, ++the file pngusr.h will automatically be included before the options in ++scripts/pnglibconf.dfa are processed. Your pngusr.h file should contain only ++macro definitions turning features on or off or setting settings. ++ ++Apart from the global setting "everything = off" all the options listed above ++can be set using macros in pngusr.h: ++ ++#define PNG_feature_SUPPORTED ++ ++is equivalent to: ++ ++option feature on ++ ++#define PNG_NO_feature ++ ++is equivalent to: ++ ++option feature off ++ ++#define PNG_feature value ++ ++is equivalent to: ++ ++setting feature default value ++ ++Notice that in both cases, pngusr.dfa and pngusr.h, the contents of the ++pngusr file you supply override the contents of scripts/pnglibconf.dfa ++ ++If confusing or incomprehensible behavior results it is possible to ++examine the intermediate file pnglibconf.dfn to find the full set of ++dependency information for each setting and option. Simply locate the ++feature in the file and read the C comments that precede it. ++ ++This method is also illustrated in the contrib/pngminim/* makefiles and ++pngusr.h. ++ ++.SH III. Reading ++ ++We'll now walk you through the possible functions to call when reading ++in a PNG file sequentially, briefly explaining the syntax and purpose ++of each one. See example.c and png.h for more detail. While ++progressive reading is covered in the next section, you will still ++need some of the functions discussed in this section to read a PNG ++file. ++ ++.SS Setup ++ ++You will want to do the I/O initialization(*) before you get into libpng, ++so if it doesn't work, you don't have much to undo. Of course, you ++will also want to insure that you are, in fact, dealing with a PNG ++file. Libpng provides a simple check to see if a file is a PNG file. ++To use it, pass in the first 1 to 8 bytes of the file to the function ++png_sig_cmp(), and it will return 0 (false) if the bytes match the ++corresponding bytes of the PNG signature, or nonzero (true) otherwise. ++Of course, the more bytes you pass in, the greater the accuracy of the ++prediction. ++ ++If you are intending to keep the file pointer open for use in libpng, ++you must ensure you don't read more than 8 bytes from the beginning ++of the file, and you also have to make a call to png_set_sig_bytes() ++with the number of bytes you read from the beginning. Libpng will ++then only check the bytes (if any) that your program didn't read. ++ ++(*): If you are not using the standard I/O functions, you will need ++to replace them with custom functions. See the discussion under ++Customizing libpng. ++ ++ FILE *fp = fopen(file_name, "rb"); ++ if (!fp) ++ { ++ return ERROR; ++ } ++ ++ if (fread(header, 1, number, fp) != number) ++ { ++ return ERROR; ++ } ++ ++ is_png = !png_sig_cmp(header, 0, number); ++ if (!is_png) ++ { ++ return NOT_PNG; ++ } ++ ++Next, png_struct and png_info need to be allocated and initialized. In ++order to ensure that the size of these structures is correct even with a ++dynamically linked libpng, there are functions to initialize and ++allocate the structures. We also pass the library version, optional ++pointers to error handling functions, and a pointer to a data struct for ++use by the error functions, if necessary (the pointer and functions can ++be NULL if the default error handlers are to be used). See the section ++on Changes to Libpng below regarding the old initialization functions. ++The structure allocation functions quietly return NULL if they fail to ++create the structure, so your application should check for that. ++ ++ png_structp png_ptr = png_create_read_struct ++ (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr, ++ user_error_fn, user_warning_fn); ++ ++ if (!png_ptr) ++ return ERROR; ++ ++ png_infop info_ptr = png_create_info_struct(png_ptr); ++ ++ if (!info_ptr) ++ { ++ png_destroy_read_struct(&png_ptr, ++ (png_infopp)NULL, (png_infopp)NULL); ++ return ERROR; ++ } ++ ++If you want to use your own memory allocation routines, ++use a libpng that was built with PNG_USER_MEM_SUPPORTED defined, and use ++png_create_read_struct_2() instead of png_create_read_struct(): ++ ++ png_structp png_ptr = png_create_read_struct_2 ++ (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr, ++ user_error_fn, user_warning_fn, (png_voidp) ++ user_mem_ptr, user_malloc_fn, user_free_fn); ++ ++The error handling routines passed to png_create_read_struct() ++and the memory alloc/free routines passed to png_create_struct_2() ++are only necessary if you are not using the libpng supplied error ++handling and memory alloc/free functions. ++ ++When libpng encounters an error, it expects to longjmp back ++to your routine. Therefore, you will need to call setjmp and pass ++your png_jmpbuf(png_ptr). If you read the file from different ++routines, you will need to update the longjmp buffer every time you enter ++a new routine that will call a png_*() function. ++ ++See your documentation of setjmp/longjmp for your compiler for more ++information on setjmp/longjmp. See the discussion on libpng error ++handling in the Customizing Libpng section below for more information ++on the libpng error handling. If an error occurs, and libpng longjmp's ++back to your setjmp, you will want to call png_destroy_read_struct() to ++free any memory. ++ ++ if (setjmp(png_jmpbuf(png_ptr))) ++ { ++ png_destroy_read_struct(&png_ptr, &info_ptr, ++ &end_info); ++ fclose(fp); ++ return ERROR; ++ } ++ ++Pass (png_infopp)NULL instead of &end_info if you didn't create ++an end_info structure. ++ ++If you would rather avoid the complexity of setjmp/longjmp issues, ++you can compile libpng with PNG_NO_SETJMP, in which case ++errors will result in a call to PNG_ABORT() which defaults to abort(). ++ ++You can #define PNG_ABORT() to a function that does something ++more useful than abort(), as long as your function does not ++return. ++ ++Now you need to set up the input code. The default for libpng is to ++use the C function fread(). If you use this, you will need to pass a ++valid FILE * in the function png_init_io(). Be sure that the file is ++opened in binary mode. If you wish to handle reading data in another ++way, you need not call the png_init_io() function, but you must then ++implement the libpng I/O methods discussed in the Customizing Libpng ++section below. ++ ++ png_init_io(png_ptr, fp); ++ ++If you had previously opened the file and read any of the signature from ++the beginning in order to see if this was a PNG file, you need to let ++libpng know that there are some bytes missing from the start of the file. ++ ++ png_set_sig_bytes(png_ptr, number); ++ ++You can change the zlib compression buffer size to be used while ++reading compressed data with ++ ++ png_set_compression_buffer_size(png_ptr, buffer_size); ++ ++where the default size is 8192 bytes. Note that the buffer size ++is changed immediately and the buffer is reallocated immediately, ++instead of setting a flag to be acted upon later. ++ ++If you want CRC errors to be handled in a different manner than ++the default, use ++ ++ png_set_crc_action(png_ptr, crit_action, ancil_action); ++ ++The values for png_set_crc_action() say how libpng is to handle CRC errors in ++ancillary and critical chunks, and whether to use the data contained ++therein. Starting with libpng-1.6.26, this also governs how an ADLER32 error ++is handled while reading the IDAT chunk. Note that it is impossible to ++"discard" data in a critical chunk. ++ ++Choices for (int) crit_action are ++ PNG_CRC_DEFAULT 0 error/quit ++ PNG_CRC_ERROR_QUIT 1 error/quit ++ PNG_CRC_WARN_USE 3 warn/use data ++ PNG_CRC_QUIET_USE 4 quiet/use data ++ PNG_CRC_NO_CHANGE 5 use the current value ++ ++Choices for (int) ancil_action are ++ PNG_CRC_DEFAULT 0 error/quit ++ PNG_CRC_ERROR_QUIT 1 error/quit ++ PNG_CRC_WARN_DISCARD 2 warn/discard data ++ PNG_CRC_WARN_USE 3 warn/use data ++ PNG_CRC_QUIET_USE 4 quiet/use data ++ PNG_CRC_NO_CHANGE 5 use the current value ++ ++When the setting for crit_action is PNG_CRC_QUIET_USE, the CRC and ADLER32 ++checksums are not only ignored, but they are not evaluated. ++ ++.SS Setting up callback code ++ ++You can set up a callback function to handle any unknown chunks in the ++input stream. You must supply the function ++ ++ read_chunk_callback(png_structp png_ptr, ++ png_unknown_chunkp chunk); ++ { ++ /* The unknown chunk structure contains your ++ chunk data, along with similar data for any other ++ unknown chunks: */ ++ ++ png_byte name[5]; ++ png_byte *data; ++ size_t size; ++ ++ /* Note that libpng has already taken care of ++ the CRC handling */ ++ ++ /* put your code here. Search for your chunk in the ++ unknown chunk structure, process it, and return one ++ of the following: */ ++ ++ return \-n; /* chunk had an error */ ++ return 0; /* did not recognize */ ++ return n; /* success */ ++ } ++ ++(You can give your function another name that you like instead of ++"read_chunk_callback") ++ ++To inform libpng about your function, use ++ ++ png_set_read_user_chunk_fn(png_ptr, user_chunk_ptr, ++ read_chunk_callback); ++ ++This names not only the callback function, but also a user pointer that ++you can retrieve with ++ ++ png_get_user_chunk_ptr(png_ptr); ++ ++If you call the png_set_read_user_chunk_fn() function, then all unknown ++chunks which the callback does not handle will be saved when read. You can ++cause them to be discarded by returning '1' ("handled") instead of '0'. This ++behavior will change in libpng 1.7 and the default handling set by the ++png_set_keep_unknown_chunks() function, described below, will be used when the ++callback returns 0. If you want the existing behavior you should set the global ++default to PNG_HANDLE_CHUNK_IF_SAFE now; this is compatible with all current ++versions of libpng and with 1.7. Libpng 1.6 issues a warning if you keep the ++default, or PNG_HANDLE_CHUNK_NEVER, and the callback returns 0. ++ ++At this point, you can set up a callback function that will be ++called after each row has been read, which you can use to control ++a progress meter or the like. It's demonstrated in pngtest.c. ++You must supply a function ++ ++ void read_row_callback(png_structp png_ptr, ++ png_uint_32 row, int pass); ++ { ++ /* put your code here */ ++ } ++ ++(You can give it another name that you like instead of "read_row_callback") ++ ++To inform libpng about your function, use ++ ++ png_set_read_status_fn(png_ptr, read_row_callback); ++ ++When this function is called the row has already been completely processed and ++the 'row' and 'pass' refer to the next row to be handled. For the ++non-interlaced case the row that was just handled is simply one less than the ++passed in row number, and pass will always be 0. For the interlaced case the ++same applies unless the row value is 0, in which case the row just handled was ++the last one from one of the preceding passes. Because interlacing may skip a ++pass you cannot be sure that the preceding pass is just 'pass\-1'; if you really ++need to know what the last pass is record (row,pass) from the callback and use ++the last recorded value each time. ++ ++As with the user transform you can find the output row using the ++PNG_ROW_FROM_PASS_ROW macro. ++ ++.SS Unknown-chunk handling ++ ++Now you get to set the way the library processes unknown chunks in the ++input PNG stream. Both known and unknown chunks will be read. Normal ++behavior is that known chunks will be parsed into information in ++various info_ptr members while unknown chunks will be discarded. This ++behavior can be wasteful if your application will never use some known ++chunk types. To change this, you can call: ++ ++ png_set_keep_unknown_chunks(png_ptr, keep, ++ chunk_list, num_chunks); ++ ++ keep - 0: default unknown chunk handling ++ 1: ignore; do not keep ++ 2: keep only if safe-to-copy ++ 3: keep even if unsafe-to-copy ++ ++ You can use these definitions: ++ PNG_HANDLE_CHUNK_AS_DEFAULT 0 ++ PNG_HANDLE_CHUNK_NEVER 1 ++ PNG_HANDLE_CHUNK_IF_SAFE 2 ++ PNG_HANDLE_CHUNK_ALWAYS 3 ++ ++ chunk_list - list of chunks affected (a byte string, ++ five bytes per chunk, NULL or '\0' if ++ num_chunks is positive; ignored if ++ numchunks <= 0). ++ ++ num_chunks - number of chunks affected; if 0, all ++ unknown chunks are affected. If positive, ++ only the chunks in the list are affected, ++ and if negative all unknown chunks and ++ all known chunks except for the IHDR, ++ PLTE, tRNS, IDAT, and IEND chunks are ++ affected. ++ ++Unknown chunks declared in this way will be saved as raw data onto a ++list of png_unknown_chunk structures. If a chunk that is normally ++known to libpng is named in the list, it will be handled as unknown, ++according to the "keep" directive. If a chunk is named in successive ++instances of png_set_keep_unknown_chunks(), the final instance will ++take precedence. The IHDR and IEND chunks should not be named in ++chunk_list; if they are, libpng will process them normally anyway. ++If you know that your application will never make use of some particular ++chunks, use PNG_HANDLE_CHUNK_NEVER (or 1) as demonstrated below. ++ ++Here is an example of the usage of png_set_keep_unknown_chunks(), ++where the private "vpAg" chunk will later be processed by a user chunk ++callback function: ++ ++ png_byte vpAg[5]={118, 112, 65, 103, (png_byte) '\0'}; ++ ++ #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED) ++ png_byte unused_chunks[]= ++ { ++ 104, 73, 83, 84, (png_byte) '\0', /* hIST */ ++ 105, 84, 88, 116, (png_byte) '\0', /* iTXt */ ++ 112, 67, 65, 76, (png_byte) '\0', /* pCAL */ ++ 115, 67, 65, 76, (png_byte) '\0', /* sCAL */ ++ 115, 80, 76, 84, (png_byte) '\0', /* sPLT */ ++ 116, 73, 77, 69, (png_byte) '\0', /* tIME */ ++ }; ++ #endif ++ ++ ... ++ ++ #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED) ++ /* ignore all unknown chunks ++ * (use global setting "2" for libpng16 and earlier): ++ */ ++ png_set_keep_unknown_chunks(read_ptr, 2, NULL, 0); ++ ++ /* except for vpAg: */ ++ png_set_keep_unknown_chunks(read_ptr, 2, vpAg, 1); ++ ++ /* also ignore unused known chunks: */ ++ png_set_keep_unknown_chunks(read_ptr, 1, unused_chunks, ++ (int)(sizeof unused_chunks)/5); ++ #endif ++ ++.SS User limits ++ ++The PNG specification allows the width and height of an image to be as ++large as 2^(31\-1 (0x7fffffff), or about 2.147 billion rows and columns. ++For safety, libpng imposes a default limit of 1 million rows and columns. ++Larger images will be rejected immediately with a png_error() call. If ++you wish to change these limits, you can use ++ ++ png_set_user_limits(png_ptr, width_max, height_max); ++ ++to set your own limits (libpng may reject some very wide images ++anyway because of potential buffer overflow conditions). ++ ++You should put this statement after you create the PNG structure and ++before calling png_read_info(), png_read_png(), or png_process_data(). ++ ++When writing a PNG datastream, put this statement before calling ++png_write_info() or png_write_png(). ++ ++If you need to retrieve the limits that are being applied, use ++ ++ width_max = png_get_user_width_max(png_ptr); ++ height_max = png_get_user_height_max(png_ptr); ++ ++The PNG specification sets no limit on the number of ancillary chunks ++allowed in a PNG datastream. By default, libpng imposes a limit of ++a total of 1000 sPLT, tEXt, iTXt, zTXt, and unknown chunks to be stored. ++If you have set up both info_ptr and end_info_ptr, the limit applies ++separately to each. You can change the limit on the total number of such ++chunks that will be stored, with ++ ++ png_set_chunk_cache_max(png_ptr, user_chunk_cache_max); ++ ++where 0x7fffffffL means unlimited. You can retrieve this limit with ++ ++ chunk_cache_max = png_get_chunk_cache_max(png_ptr); ++ ++Libpng imposes a limit of 8 Megabytes (8,000,000 bytes) on the amount of ++memory that any chunk other than IDAT can occupy, originally or when ++decompressed (prior to libpng-1.6.32 the limit was only applied to compressed ++chunks after decompression). You can change this limit with ++ ++ png_set_chunk_malloc_max(png_ptr, user_chunk_malloc_max); ++ ++and you can retrieve the limit with ++ ++ chunk_malloc_max = png_get_chunk_malloc_max(png_ptr); ++ ++Any chunks that would cause either of these limits to be exceeded will ++be ignored. ++ ++.SS Information about your system ++ ++If you intend to display the PNG or to incorporate it in other image data you ++need to tell libpng information about your display or drawing surface so that ++libpng can convert the values in the image to match the display. ++ ++From libpng-1.5.4 this information can be set before reading the PNG file ++header. In earlier versions png_set_gamma() existed but behaved incorrectly if ++called before the PNG file header had been read and png_set_alpha_mode() did not ++exist. ++ ++If you need to support versions prior to libpng-1.5.4 test the version number ++as illustrated below using "PNG_LIBPNG_VER >= 10504" and follow the procedures ++described in the appropriate manual page. ++ ++You give libpng the encoding expected by your system expressed as a 'gamma' ++value. You can also specify a default encoding for the PNG file in ++case the required information is missing from the file. By default libpng ++assumes that the PNG data matches your system, to keep this default call: ++ ++ png_set_gamma(png_ptr, screen_gamma, output_gamma); ++ ++or you can use the fixed point equivalent: ++ ++ png_set_gamma_fixed(png_ptr, PNG_FP_1*screen_gamma, ++ PNG_FP_1*output_gamma); ++ ++If you don't know the gamma for your system it is probably 2.2 - a good ++approximation to the IEC standard for display systems (sRGB). If images are ++too contrasty or washed out you got the value wrong - check your system ++documentation! ++ ++Many systems permit the system gamma to be changed via a lookup table in the ++display driver, a few systems, including older Macs, change the response by ++default. As of 1.5.4 three special values are available to handle common ++situations: ++ ++ PNG_DEFAULT_sRGB: Indicates that the system conforms to the ++ IEC 61966-2-1 standard. This matches almost ++ all systems. ++ PNG_GAMMA_MAC_18: Indicates that the system is an older ++ (pre Mac OS 10.6) Apple Macintosh system with ++ the default settings. ++ PNG_GAMMA_LINEAR: Just the fixed point value for 1.0 - indicates ++ that the system expects data with no gamma ++ encoding. ++ ++You would use the linear (unencoded) value if you need to process the pixel ++values further because this avoids the need to decode and re-encode each ++component value whenever arithmetic is performed. A lot of graphics software ++uses linear values for this reason, often with higher precision component values ++to preserve overall accuracy. ++ ++ ++The output_gamma value expresses how to decode the output values, not how ++they are encoded. The values used correspond to the normal numbers used to ++describe the overall gamma of a computer display system; for example 2.2 for ++an sRGB conformant system. The values are scaled by 100000 in the _fixed ++version of the API (so 220000 for sRGB.) ++ ++The inverse of the value is always used to provide a default for the PNG file ++encoding if it has no gAMA chunk and if png_set_gamma() has not been called ++to override the PNG gamma information. ++ ++When the ALPHA_OPTIMIZED mode is selected the output gamma is used to encode ++opaque pixels however pixels with lower alpha values are not encoded, ++regardless of the output gamma setting. ++ ++When the standard Porter Duff handling is requested with mode 1 the output ++encoding is set to be linear and the output_gamma value is only relevant ++as a default for input data that has no gamma information. The linear output ++encoding will be overridden if png_set_gamma() is called - the results may be ++highly unexpected! ++ ++The following numbers are derived from the sRGB standard and the research ++behind it. sRGB is defined to be approximated by a PNG gAMA chunk value of ++0.45455 (1/2.2) for PNG. The value implicitly includes any viewing ++correction required to take account of any differences in the color ++environment of the original scene and the intended display environment; the ++value expresses how to *decode* the image for display, not how the original ++data was *encoded*. ++ ++sRGB provides a peg for the PNG standard by defining a viewing environment. ++sRGB itself, and earlier TV standards, actually use a more complex transform ++(a linear portion then a gamma 2.4 power law) than PNG can express. (PNG is ++limited to simple power laws.) By saying that an image for direct display on ++an sRGB conformant system should be stored with a gAMA chunk value of 45455 ++(11.3.3.2 and 11.3.3.5 of the ISO PNG specification) the PNG specification ++makes it possible to derive values for other display systems and ++environments. ++ ++The Mac value is deduced from the sRGB based on an assumption that the actual ++extra viewing correction used in early Mac display systems was implemented as ++a power 1.45 lookup table. ++ ++Any system where a programmable lookup table is used or where the behavior of ++the final display device characteristics can be changed requires system ++specific code to obtain the current characteristic. However this can be ++difficult and most PNG gamma correction only requires an approximate value. ++ ++By default, if png_set_alpha_mode() is not called, libpng assumes that all ++values are unencoded, linear, values and that the output device also has a ++linear characteristic. This is only very rarely correct - it is invariably ++better to call png_set_alpha_mode() with PNG_DEFAULT_sRGB than rely on the ++default if you don't know what the right answer is! ++ ++The special value PNG_GAMMA_MAC_18 indicates an older Mac system (pre Mac OS ++10.6) which used a correction table to implement a somewhat lower gamma on an ++otherwise sRGB system. ++ ++Both these values are reserved (not simple gamma values) in order to allow ++more precise correction internally in the future. ++ ++NOTE: the values can be passed to either the fixed or floating ++point APIs, but the floating point API will also accept floating point ++values. ++ ++The second thing you may need to tell libpng about is how your system handles ++alpha channel information. Some, but not all, PNG files contain an alpha ++channel. To display these files correctly you need to compose the data onto a ++suitable background, as described in the PNG specification. ++ ++Libpng only supports composing onto a single color (using png_set_background; ++see below). Otherwise you must do the composition yourself and, in this case, ++you may need to call png_set_alpha_mode: ++ ++ #if PNG_LIBPNG_VER >= 10504 ++ png_set_alpha_mode(png_ptr, mode, screen_gamma); ++ #else ++ png_set_gamma(png_ptr, screen_gamma, 1.0/screen_gamma); ++ #endif ++ ++The screen_gamma value is the same as the argument to png_set_gamma; however, ++how it affects the output depends on the mode. png_set_alpha_mode() sets the ++file gamma default to 1/screen_gamma, so normally you don't need to call ++png_set_gamma. If you need different defaults call png_set_gamma() before ++png_set_alpha_mode() - if you call it after it will override the settings made ++by png_set_alpha_mode(). ++ ++The mode is as follows: ++ ++ PNG_ALPHA_PNG: The data is encoded according to the PNG ++specification. Red, green and blue, or gray, components are ++gamma encoded color values and are not premultiplied by the ++alpha value. The alpha value is a linear measure of the ++contribution of the pixel to the corresponding final output pixel. ++ ++You should normally use this format if you intend to perform ++color correction on the color values; most, maybe all, color ++correction software has no handling for the alpha channel and, ++anyway, the math to handle pre-multiplied component values is ++unnecessarily complex. ++ ++Before you do any arithmetic on the component values you need ++to remove the gamma encoding and multiply out the alpha ++channel. See the PNG specification for more detail. It is ++important to note that when an image with an alpha channel is ++scaled, linear encoded, pre-multiplied component values must ++be used! ++ ++The remaining modes assume you don't need to do any further color correction or ++that if you do, your color correction software knows all about alpha (it ++probably doesn't!). They 'associate' the alpha with the color information by ++storing color channel values that have been scaled by the alpha. The ++advantage is that the color channels can be resampled (the image can be ++scaled) in this form. The disadvantage is that normal practice is to store ++linear, not (gamma) encoded, values and this requires 16-bit channels for ++still images rather than the 8-bit channels that are just about sufficient if ++gamma encoding is used. In addition all non-transparent pixel values, ++including completely opaque ones, must be gamma encoded to produce the final ++image. These are the 'STANDARD', 'ASSOCIATED' or 'PREMULTIPLIED' modes ++described below (the latter being the two common names for associated alpha ++color channels). Note that PNG files always contain non-associated color ++channels; png_set_alpha_mode() with one of the modes causes the decoder to ++convert the pixels to an associated form before returning them to your ++application. ++ ++Since it is not necessary to perform arithmetic on opaque color values so ++long as they are not to be resampled and are in the final color space it is ++possible to optimize the handling of alpha by storing the opaque pixels in ++the PNG format (adjusted for the output color space) while storing partially ++opaque pixels in the standard, linear, format. The accuracy required for ++standard alpha composition is relatively low, because the pixels are ++isolated, therefore typically the accuracy loss in storing 8-bit linear ++values is acceptable. (This is not true if the alpha channel is used to ++simulate transparency over large areas - use 16 bits or the PNG mode in ++this case!) This is the 'OPTIMIZED' mode. For this mode a pixel is ++treated as opaque only if the alpha value is equal to the maximum value. ++ ++ PNG_ALPHA_STANDARD: The data libpng produces is encoded in the ++standard way assumed by most correctly written graphics software. ++The gamma encoding will be removed by libpng and the ++linear component values will be pre-multiplied by the ++alpha channel. ++ ++With this format the final image must be re-encoded to ++match the display gamma before the image is displayed. ++If your system doesn't do that, yet still seems to ++perform arithmetic on the pixels without decoding them, ++it is broken - check out the modes below. ++ ++With PNG_ALPHA_STANDARD libpng always produces linear ++component values, whatever screen_gamma you supply. The ++screen_gamma value is, however, used as a default for ++the file gamma if the PNG file has no gamma information. ++ ++If you call png_set_gamma() after png_set_alpha_mode() you ++will override the linear encoding. Instead the ++pre-multiplied pixel values will be gamma encoded but ++the alpha channel will still be linear. This may ++actually match the requirements of some broken software, ++but it is unlikely. ++ ++While linear 8-bit data is often used it has ++insufficient precision for any image with a reasonable ++dynamic range. To avoid problems, and if your software ++supports it, use png_set_expand_16() to force all ++components to 16 bits. ++ ++ PNG_ALPHA_OPTIMIZED: This mode is the same as PNG_ALPHA_STANDARD ++except that completely opaque pixels are gamma encoded according to ++the screen_gamma value. Pixels with alpha less than 1.0 ++will still have linear components. ++ ++Use this format if you have control over your ++compositing software and so don't do other arithmetic ++(such as scaling) on the data you get from libpng. Your ++compositing software can simply copy opaque pixels to ++the output but still has linear values for the ++non-opaque pixels. ++ ++In normal compositing, where the alpha channel encodes ++partial pixel coverage (as opposed to broad area ++translucency), the inaccuracies of the 8-bit ++representation of non-opaque pixels are irrelevant. ++ ++You can also try this format if your software is broken; ++it might look better. ++ ++ PNG_ALPHA_BROKEN: This is PNG_ALPHA_STANDARD; however, all component ++values, including the alpha channel are gamma encoded. This is ++broken because, in practice, no implementation that uses this choice ++correctly undoes the encoding before handling alpha composition. Use this ++choice only if other serious errors in the software or hardware you use ++mandate it. In most cases of broken software or hardware the bug in the ++final display manifests as a subtle halo around composited parts of the ++image. You may not even perceive this as a halo; the composited part of ++the image may simply appear separate from the background, as though it had ++been cut out of paper and pasted on afterward. ++ ++If you don't have to deal with bugs in software or hardware, or if you can fix ++them, there are three recommended ways of using png_set_alpha_mode(): ++ ++ png_set_alpha_mode(png_ptr, PNG_ALPHA_PNG, ++ screen_gamma); ++ ++You can do color correction on the result (libpng does not currently ++support color correction internally). When you handle the alpha channel ++you need to undo the gamma encoding and multiply out the alpha. ++ ++ png_set_alpha_mode(png_ptr, PNG_ALPHA_STANDARD, ++ screen_gamma); ++ png_set_expand_16(png_ptr); ++ ++If you are using the high level interface, don't call png_set_expand_16(); ++instead pass PNG_TRANSFORM_EXPAND_16 to the interface. ++ ++With this mode you can't do color correction, but you can do arithmetic, ++including composition and scaling, on the data without further processing. ++ ++ png_set_alpha_mode(png_ptr, PNG_ALPHA_OPTIMIZED, ++ screen_gamma); ++ ++You can avoid the expansion to 16-bit components with this mode, but you ++lose the ability to scale the image or perform other linear arithmetic. ++All you can do is compose the result onto a matching output. Since this ++mode is libpng-specific you also need to write your own composition ++software. ++ ++The following are examples of calls to png_set_alpha_mode to achieve the ++required overall gamma correction and, where necessary, alpha ++premultiplication. ++ ++ png_set_alpha_mode(pp, PNG_ALPHA_PNG, PNG_DEFAULT_sRGB); ++ ++Choices for the alpha_mode are ++ ++ PNG_ALPHA_PNG 0 /* according to the PNG standard */ ++ PNG_ALPHA_STANDARD 1 /* according to Porter/Duff */ ++ PNG_ALPHA_ASSOCIATED 1 /* as above; this is the normal practice */ ++ PNG_ALPHA_PREMULTIPLIED 1 /* as above */ ++ PNG_ALPHA_OPTIMIZED 2 /* 'PNG' for opaque pixels, else 'STANDARD' */ ++ PNG_ALPHA_BROKEN 3 /* the alpha channel is gamma encoded */ ++ ++PNG_ALPHA_PNG is the default libpng handling of the alpha channel. It is not ++pre-multiplied into the color components. In addition the call states ++that the output is for a sRGB system and causes all PNG files without gAMA ++chunks to be assumed to be encoded using sRGB. ++ ++ png_set_alpha_mode(pp, PNG_ALPHA_PNG, PNG_GAMMA_MAC); ++ ++In this case the output is assumed to be something like an sRGB conformant ++display preceded by a power-law lookup table of power 1.45. This is how ++early Mac systems behaved. ++ ++ png_set_alpha_mode(pp, PNG_ALPHA_STANDARD, PNG_GAMMA_LINEAR); ++ ++This is the classic Jim Blinn approach and will work in academic ++environments where everything is done by the book. It has the shortcoming ++of assuming that input PNG data with no gamma information is linear - this ++is unlikely to be correct unless the PNG files were generated locally. ++Most of the time the output precision will be so low as to show ++significant banding in dark areas of the image. ++ ++ png_set_expand_16(pp); ++ png_set_alpha_mode(pp, PNG_ALPHA_STANDARD, PNG_DEFAULT_sRGB); ++ ++This is a somewhat more realistic Jim Blinn inspired approach. PNG files ++are assumed to have the sRGB encoding if not marked with a gamma value and ++the output is always 16 bits per component. This permits accurate scaling ++and processing of the data. If you know that your input PNG files were ++generated locally you might need to replace PNG_DEFAULT_sRGB with the ++correct value for your system. ++ ++ png_set_alpha_mode(pp, PNG_ALPHA_OPTIMIZED, PNG_DEFAULT_sRGB); ++ ++If you just need to composite the PNG image onto an existing background ++and if you control the code that does this you can use the optimization ++setting. In this case you just copy completely opaque pixels to the ++output. For pixels that are not completely transparent (you just skip ++those) you do the composition math using png_composite or png_composite_16 ++below then encode the resultant 8-bit or 16-bit values to match the output ++encoding. ++ ++ Other cases ++ ++If neither the PNG nor the standard linear encoding work for you because ++of the software or hardware you use then you have a big problem. The PNG ++case will probably result in halos around the image. The linear encoding ++will probably result in a washed out, too bright, image (it's actually too ++contrasty.) Try the ALPHA_OPTIMIZED mode above - this will probably ++substantially reduce the halos. Alternatively try: ++ ++ png_set_alpha_mode(pp, PNG_ALPHA_BROKEN, PNG_DEFAULT_sRGB); ++ ++This option will also reduce the halos, but there will be slight dark ++halos round the opaque parts of the image where the background is light. ++In the OPTIMIZED mode the halos will be light halos where the background ++is dark. Take your pick - the halos are unavoidable unless you can get ++your hardware/software fixed! (The OPTIMIZED approach is slightly ++faster.) ++ ++When the default gamma of PNG files doesn't match the output gamma. ++If you have PNG files with no gamma information png_set_alpha_mode allows ++you to provide a default gamma, but it also sets the output gamma to the ++matching value. If you know your PNG files have a gamma that doesn't ++match the output you can take advantage of the fact that ++png_set_alpha_mode always sets the output gamma but only sets the PNG ++default if it is not already set: ++ ++ png_set_alpha_mode(pp, PNG_ALPHA_PNG, PNG_DEFAULT_sRGB); ++ png_set_alpha_mode(pp, PNG_ALPHA_PNG, PNG_GAMMA_MAC); ++ ++The first call sets both the default and the output gamma values, the ++second call overrides the output gamma without changing the default. This ++is easier than achieving the same effect with png_set_gamma. You must use ++PNG_ALPHA_PNG for the first call - internal checking in png_set_alpha will ++fire if more than one call to png_set_alpha_mode and png_set_background is ++made in the same read operation, however multiple calls with PNG_ALPHA_PNG ++are ignored. ++ ++If you don't need, or can't handle, the alpha channel you can call ++png_set_background() to remove it by compositing against a fixed color. Don't ++call png_set_strip_alpha() to do this - it will leave spurious pixel values in ++transparent parts of this image. ++ ++ png_set_background(png_ptr, &background_color, ++ PNG_BACKGROUND_GAMMA_SCREEN, 0, 1); ++ ++The background_color is an RGB or grayscale value according to the data format ++libpng will produce for you. Because you don't yet know the format of the PNG ++file, if you call png_set_background at this point you must arrange for the ++format produced by libpng to always have 8-bit or 16-bit components and then ++store the color as an 8-bit or 16-bit color as appropriate. The color contains ++separate gray and RGB component values, so you can let libpng produce gray or ++RGB output according to the input format, but low bit depth grayscale images ++must always be converted to at least 8-bit format. (Even though low bit depth ++grayscale images can't have an alpha channel they can have a transparent ++color!) ++ ++You set the transforms you need later, either as flags to the high level ++interface or libpng API calls for the low level interface. For reference the ++settings and API calls required are: ++ ++8-bit values: ++ PNG_TRANSFORM_SCALE_16 | PNG_EXPAND ++ png_set_expand(png_ptr); png_set_scale_16(png_ptr); ++ ++ If you must get exactly the same inaccurate results ++ produced by default in versions prior to libpng-1.5.4, ++ use PNG_TRANSFORM_STRIP_16 and png_set_strip_16(png_ptr) ++ instead. ++ ++16-bit values: ++ PNG_TRANSFORM_EXPAND_16 ++ png_set_expand_16(png_ptr); ++ ++In either case palette image data will be expanded to RGB. If you just want ++color data you can add PNG_TRANSFORM_GRAY_TO_RGB or png_set_gray_to_rgb(png_ptr) ++to the list. ++ ++Calling png_set_background before the PNG file header is read will not work ++prior to libpng-1.5.4. Because the failure may result in unexpected warnings or ++errors it is therefore much safer to call png_set_background after the head has ++been read. Unfortunately this means that prior to libpng-1.5.4 it cannot be ++used with the high level interface. ++ ++.SS The high-level read interface ++ ++At this point there are two ways to proceed; through the high-level ++read interface, or through a sequence of low-level read operations. ++You can use the high-level interface if (a) you are willing to read ++the entire image into memory, and (b) the input transformations ++you want to do are limited to the following set: ++ ++ PNG_TRANSFORM_IDENTITY No transformation ++ PNG_TRANSFORM_SCALE_16 Strip 16-bit samples to ++ 8-bit accurately ++ PNG_TRANSFORM_STRIP_16 Chop 16-bit samples to ++ 8-bit less accurately ++ PNG_TRANSFORM_STRIP_ALPHA Discard the alpha channel ++ PNG_TRANSFORM_PACKING Expand 1, 2 and 4-bit ++ samples to bytes ++ PNG_TRANSFORM_PACKSWAP Change order of packed ++ pixels to LSB first ++ PNG_TRANSFORM_EXPAND Perform set_expand() ++ PNG_TRANSFORM_INVERT_MONO Invert monochrome images ++ PNG_TRANSFORM_SHIFT Normalize pixels to the ++ sBIT depth ++ PNG_TRANSFORM_BGR Flip RGB to BGR, RGBA ++ to BGRA ++ PNG_TRANSFORM_SWAP_ALPHA Flip RGBA to ARGB or GA ++ to AG ++ PNG_TRANSFORM_INVERT_ALPHA Change alpha from opacity ++ to transparency ++ PNG_TRANSFORM_SWAP_ENDIAN Byte-swap 16-bit samples ++ PNG_TRANSFORM_GRAY_TO_RGB Expand grayscale samples ++ to RGB (or GA to RGBA) ++ PNG_TRANSFORM_EXPAND_16 Expand samples to 16 bits ++ ++(This excludes setting a background color, doing gamma transformation, ++quantizing, and setting filler.) If this is the case, simply do this: ++ ++ png_read_png(png_ptr, info_ptr, png_transforms, NULL) ++ ++where png_transforms is an integer containing the bitwise OR of some ++set of transformation flags. This call is equivalent to png_read_info(), ++followed the set of transformations indicated by the transform mask, ++then png_read_image(), and finally png_read_end(). ++ ++(The final parameter of this call is not yet used. Someday it might point ++to transformation parameters required by some future input transform.) ++ ++You must use png_transforms and not call any png_set_transform() functions ++when you use png_read_png(). ++ ++After you have called png_read_png(), you can retrieve the image data ++with ++ ++ row_pointers = png_get_rows(png_ptr, info_ptr); ++ ++where row_pointers is an array of pointers to the pixel data for each row: ++ ++ png_bytep row_pointers[height]; ++ ++If you know your image size and pixel size ahead of time, you can allocate ++row_pointers prior to calling png_read_png() with ++ ++ if (height > PNG_UINT_32_MAX/(sizeof (png_byte))) ++ png_error (png_ptr, ++ "Image is too tall to process in memory"); ++ ++ if (width > PNG_UINT_32_MAX/pixel_size) ++ png_error (png_ptr, ++ "Image is too wide to process in memory"); ++ ++ row_pointers = png_malloc(png_ptr, ++ height*(sizeof (png_bytep))); ++ ++ for (int i=0; i PNG_SIZE_MAX/(width*pixel_size)) { ++ png_error(png_ptr,"image_data buffer would be too large"); ++ } ++ ++ png_bytep buffer=png_malloc(png_ptr,height*width*pixel_size); ++ ++ for (int i=0; i) and ++png_get_(png_ptr, info_ptr, ...) functions return non-zero if the ++data has been read, or zero if it is missing. The parameters to the ++png_get_ are set directly if they are simple data types, or a ++pointer into the info_ptr is returned for any complex types. ++ ++The colorspace data from gAMA, cHRM, sRGB, iCCP, and sBIT chunks ++is simply returned to give the application information about how the ++image was encoded. Libpng itself only does transformations using the file ++gamma when combining semitransparent pixels with the background color, and, ++since libpng-1.6.0, when converting between 8-bit sRGB and 16-bit linear pixels ++within the simplified API. Libpng also uses the file gamma when converting ++RGB to gray, beginning with libpng-1.0.5, if the application calls ++png_set_rgb_to_gray()). ++ ++ png_get_PLTE(png_ptr, info_ptr, &palette, ++ &num_palette); ++ ++ palette - the palette for the file ++ (array of png_color) ++ ++ num_palette - number of entries in the palette ++ ++ png_get_gAMA(png_ptr, info_ptr, &file_gamma); ++ png_get_gAMA_fixed(png_ptr, info_ptr, &int_file_gamma); ++ ++ file_gamma - the gamma at which the file is ++ written (PNG_INFO_gAMA) ++ ++ int_file_gamma - 100,000 times the gamma at which the ++ file is written ++ ++ png_get_cHRM(png_ptr, info_ptr, &white_x, &white_y, &red_x, ++ &red_y, &green_x, &green_y, &blue_x, &blue_y) ++ png_get_cHRM_XYZ(png_ptr, info_ptr, &red_X, &red_Y, &red_Z, ++ &green_X, &green_Y, &green_Z, &blue_X, &blue_Y, ++ &blue_Z) ++ png_get_cHRM_fixed(png_ptr, info_ptr, &int_white_x, ++ &int_white_y, &int_red_x, &int_red_y, ++ &int_green_x, &int_green_y, &int_blue_x, ++ &int_blue_y) ++ png_get_cHRM_XYZ_fixed(png_ptr, info_ptr, &int_red_X, &int_red_Y, ++ &int_red_Z, &int_green_X, &int_green_Y, ++ &int_green_Z, &int_blue_X, &int_blue_Y, ++ &int_blue_Z) ++ ++ {white,red,green,blue}_{x,y} ++ A color space encoding specified using the ++ chromaticities of the end points and the ++ white point. (PNG_INFO_cHRM) ++ ++ {red,green,blue}_{X,Y,Z} ++ A color space encoding specified using the ++ encoding end points - the CIE tristimulus ++ specification of the intended color of the red, ++ green and blue channels in the PNG RGB data. ++ The white point is simply the sum of the three ++ end points. (PNG_INFO_cHRM) ++ ++ png_get_sRGB(png_ptr, info_ptr, &srgb_intent); ++ ++ srgb_intent - the rendering intent (PNG_INFO_sRGB) ++ The presence of the sRGB chunk ++ means that the pixel data is in the ++ sRGB color space. This chunk also ++ implies specific values of gAMA and ++ cHRM. ++ ++ png_get_iCCP(png_ptr, info_ptr, &name, ++ &compression_type, &profile, &proflen); ++ ++ name - The profile name. ++ ++ compression_type - The compression type; always ++ PNG_COMPRESSION_TYPE_BASE for PNG 1.0. ++ You may give NULL to this argument to ++ ignore it. ++ ++ profile - International Color Consortium color ++ profile data. May contain NULs. ++ ++ proflen - length of profile data in bytes. ++ ++ png_get_sBIT(png_ptr, info_ptr, &sig_bit); ++ ++ sig_bit - the number of significant bits for ++ (PNG_INFO_sBIT) each of the gray, ++ red, green, and blue channels, ++ whichever are appropriate for the ++ given color type (png_color_16) ++ ++ png_get_tRNS(png_ptr, info_ptr, &trans_alpha, ++ &num_trans, &trans_color); ++ ++ trans_alpha - array of alpha (transparency) ++ entries for palette (PNG_INFO_tRNS) ++ ++ num_trans - number of transparent entries ++ (PNG_INFO_tRNS) ++ ++ trans_color - graylevel or color sample values of ++ the single transparent color for ++ non-paletted images (PNG_INFO_tRNS) ++ ++ png_get_eXIf_1(png_ptr, info_ptr, &num_exif, &exif); ++ (PNG_INFO_eXIf) ++ ++ exif - Exif profile (array of png_byte) ++ ++ png_get_hIST(png_ptr, info_ptr, &hist); ++ (PNG_INFO_hIST) ++ ++ hist - histogram of palette (array of ++ png_uint_16) ++ ++ png_get_tIME(png_ptr, info_ptr, &mod_time); ++ ++ mod_time - time image was last modified ++ (PNG_VALID_tIME) ++ ++ png_get_bKGD(png_ptr, info_ptr, &background); ++ ++ background - background color (of type ++ png_color_16p) (PNG_VALID_bKGD) ++ valid 16-bit red, green and blue ++ values, regardless of color_type ++ ++ num_comments = png_get_text(png_ptr, info_ptr, ++ &text_ptr, &num_text); ++ ++ num_comments - number of comments ++ ++ text_ptr - array of png_text holding image ++ comments ++ ++ text_ptr[i].compression - type of compression used ++ on "text" PNG_TEXT_COMPRESSION_NONE ++ PNG_TEXT_COMPRESSION_zTXt ++ PNG_ITXT_COMPRESSION_NONE ++ PNG_ITXT_COMPRESSION_zTXt ++ ++ text_ptr[i].key - keyword for comment. Must contain ++ 1-79 characters. ++ ++ text_ptr[i].text - text comments for current ++ keyword. Can be empty. ++ ++ text_ptr[i].text_length - length of text string, ++ after decompression, 0 for iTXt ++ ++ text_ptr[i].itxt_length - length of itxt string, ++ after decompression, 0 for tEXt/zTXt ++ ++ text_ptr[i].lang - language of comment (empty ++ string for unknown). ++ ++ text_ptr[i].lang_key - keyword in UTF-8 ++ (empty string for unknown). ++ ++ Note that the itxt_length, lang, and lang_key ++ members of the text_ptr structure only exist when the ++ library is built with iTXt chunk support. Prior to ++ libpng-1.4.0 the library was built by default without ++ iTXt support. Also note that when iTXt is supported, ++ they contain NULL pointers when the "compression" ++ field contains PNG_TEXT_COMPRESSION_NONE or ++ PNG_TEXT_COMPRESSION_zTXt. ++ ++ num_text - number of comments (same as ++ num_comments; you can put NULL here ++ to avoid the duplication) ++ ++ Note while png_set_text() will accept text, language, ++ and translated keywords that can be NULL pointers, the ++ structure returned by png_get_text will always contain ++ regular zero-terminated C strings. They might be ++ empty strings but they will never be NULL pointers. ++ ++ num_spalettes = png_get_sPLT(png_ptr, info_ptr, ++ &palette_ptr); ++ ++ num_spalettes - number of sPLT chunks read. ++ ++ palette_ptr - array of palette structures holding ++ contents of one or more sPLT chunks ++ read. ++ ++ png_get_oFFs(png_ptr, info_ptr, &offset_x, &offset_y, ++ &unit_type); ++ ++ offset_x - positive offset from the left edge ++ of the screen (can be negative) ++ ++ offset_y - positive offset from the top edge ++ of the screen (can be negative) ++ ++ unit_type - PNG_OFFSET_PIXEL, PNG_OFFSET_MICROMETER ++ ++ png_get_pHYs(png_ptr, info_ptr, &res_x, &res_y, ++ &unit_type); ++ ++ res_x - pixels/unit physical resolution in ++ x direction ++ ++ res_y - pixels/unit physical resolution in ++ x direction ++ ++ unit_type - PNG_RESOLUTION_UNKNOWN, ++ PNG_RESOLUTION_METER ++ ++ png_get_sCAL(png_ptr, info_ptr, &unit, &width, ++ &height) ++ ++ unit - physical scale units (an integer) ++ ++ width - width of a pixel in physical scale units ++ ++ height - height of a pixel in physical scale units ++ (width and height are doubles) ++ ++ png_get_sCAL_s(png_ptr, info_ptr, &unit, &width, ++ &height) ++ ++ unit - physical scale units (an integer) ++ ++ width - width of a pixel in physical scale units ++ (expressed as a string) ++ ++ height - height of a pixel in physical scale units ++ (width and height are strings like "2.54") ++ ++ num_unknown_chunks = png_get_unknown_chunks(png_ptr, ++ info_ptr, &unknowns) ++ ++ unknowns - array of png_unknown_chunk ++ structures holding unknown chunks ++ ++ unknowns[i].name - name of unknown chunk ++ ++ unknowns[i].data - data of unknown chunk ++ ++ unknowns[i].size - size of unknown chunk's data ++ ++ unknowns[i].location - position of chunk in file ++ ++ The value of "i" corresponds to the order in which the ++ chunks were read from the PNG file or inserted with the ++ png_set_unknown_chunks() function. ++ ++ The value of "location" is a bitwise "or" of ++ ++ PNG_HAVE_IHDR (0x01) ++ PNG_HAVE_PLTE (0x02) ++ PNG_AFTER_IDAT (0x08) ++ ++The data from the pHYs chunk can be retrieved in several convenient ++forms: ++ ++ res_x = png_get_x_pixels_per_meter(png_ptr, ++ info_ptr) ++ ++ res_y = png_get_y_pixels_per_meter(png_ptr, ++ info_ptr) ++ ++ res_x_and_y = png_get_pixels_per_meter(png_ptr, ++ info_ptr) ++ ++ res_x = png_get_x_pixels_per_inch(png_ptr, ++ info_ptr) ++ ++ res_y = png_get_y_pixels_per_inch(png_ptr, ++ info_ptr) ++ ++ res_x_and_y = png_get_pixels_per_inch(png_ptr, ++ info_ptr) ++ ++ aspect_ratio = png_get_pixel_aspect_ratio(png_ptr, ++ info_ptr) ++ ++ Each of these returns 0 [signifying "unknown"] if ++ the data is not present or if res_x is 0; ++ res_x_and_y is 0 if res_x != res_y ++ ++ Note that because of the way the resolutions are ++ stored internally, the inch conversions won't ++ come out to exactly even number. For example, ++ 72 dpi is stored as 0.28346 pixels/meter, and ++ when this is retrieved it is 71.9988 dpi, so ++ be sure to round the returned value appropriately ++ if you want to display a reasonable-looking result. ++ ++The data from the oFFs chunk can be retrieved in several convenient ++forms: ++ ++ x_offset = png_get_x_offset_microns(png_ptr, info_ptr); ++ ++ y_offset = png_get_y_offset_microns(png_ptr, info_ptr); ++ ++ x_offset = png_get_x_offset_inches(png_ptr, info_ptr); ++ ++ y_offset = png_get_y_offset_inches(png_ptr, info_ptr); ++ ++ Each of these returns 0 [signifying "unknown" if both ++ x and y are 0] if the data is not present or if the ++ chunk is present but the unit is the pixel. The ++ remark about inexact inch conversions applies here ++ as well, because a value in inches can't always be ++ converted to microns and back without some loss ++ of precision. ++ ++For more information, see the ++PNG specification for chunk contents. Be careful with trusting ++rowbytes, as some of the transformations could increase the space ++needed to hold a row (expand, filler, gray_to_rgb, etc.). ++See png_read_update_info(), below. ++ ++A quick word about text_ptr and num_text. PNG stores comments in ++keyword/text pairs, one pair per chunk, with no limit on the number ++of text chunks, and a 2^31 byte limit on their size. While there are ++suggested keywords, there is no requirement to restrict the use to these ++strings. It is strongly suggested that keywords and text be sensible ++to humans (that's the point), so don't use abbreviations. Non-printing ++symbols are not allowed. See the PNG specification for more details. ++There is also no requirement to have text after the keyword. ++ ++Keywords should be limited to 79 Latin-1 characters without leading or ++trailing spaces, but non-consecutive spaces are allowed within the ++keyword. It is possible to have the same keyword any number of times. ++The text_ptr is an array of png_text structures, each holding a ++pointer to a language string, a pointer to a keyword and a pointer to ++a text string. The text string, language code, and translated ++keyword may be empty or NULL pointers. The keyword/text ++pairs are put into the array in the order that they are received. ++However, some or all of the text chunks may be after the image, so, to ++make sure you have read all the text chunks, don't mess with these ++until after you read the stuff after the image. This will be ++mentioned again below in the discussion that goes with png_read_end(). ++ ++.SS Input transformations ++ ++After you've read the header information, you can set up the library ++to handle any special transformations of the image data. The various ++ways to transform the data will be described in the order that they ++should occur. This is important, as some of these change the color ++type and/or bit depth of the data, and some others only work on ++certain color types and bit depths. ++ ++Transformations you request are ignored if they don't have any meaning for a ++particular input data format. However some transformations can have an effect ++as a result of a previous transformation. If you specify a contradictory set of ++transformations, for example both adding and removing the alpha channel, you ++cannot predict the final result. ++ ++The color used for the transparency values should be supplied in the same ++format/depth as the current image data. It is stored in the same format/depth ++as the image data in a tRNS chunk, so this is what libpng expects for this data. ++ ++The color used for the background value depends on the need_expand argument as ++described below. ++ ++Data will be decoded into the supplied row buffers packed into bytes ++unless the library has been told to transform it into another format. ++For example, 4 bit/pixel paletted or grayscale data will be returned ++2 pixels/byte with the leftmost pixel in the high-order bits of the byte, ++unless png_set_packing() is called. 8-bit RGB data will be stored ++in RGB RGB RGB format unless png_set_filler() or png_set_add_alpha() ++is called to insert filler bytes, either before or after each RGB triplet. ++ ++16-bit RGB data will be returned RRGGBB RRGGBB, with the most significant ++byte of the color value first, unless png_set_scale_16() is called to ++transform it to regular RGB RGB triplets, or png_set_filler() or ++png_set_add alpha() is called to insert two filler bytes, either before ++or after each RRGGBB triplet. Similarly, 8-bit or 16-bit grayscale data can ++be modified with png_set_filler(), png_set_add_alpha(), png_set_strip_16(), ++or png_set_scale_16(). ++ ++The following code transforms grayscale images of less than 8 to 8 bits, ++changes paletted images to RGB, and adds a full alpha channel if there is ++transparency information in a tRNS chunk. This is most useful on ++grayscale images with bit depths of 2 or 4 or if there is a multiple-image ++viewing application that wishes to treat all images in the same way. ++ ++ if (color_type == PNG_COLOR_TYPE_PALETTE) ++ png_set_palette_to_rgb(png_ptr); ++ ++ if (png_get_valid(png_ptr, info_ptr, ++ PNG_INFO_tRNS)) png_set_tRNS_to_alpha(png_ptr); ++ ++ if (color_type == PNG_COLOR_TYPE_GRAY && ++ bit_depth < 8) png_set_expand_gray_1_2_4_to_8(png_ptr); ++ ++The first two functions are actually aliases for png_set_expand(), added ++in libpng version 1.0.4, with the function names expanded to improve code ++readability. In some future version they may actually do different ++things. ++ ++As of libpng version 1.2.9, png_set_expand_gray_1_2_4_to_8() was ++added. It expands the sample depth without changing tRNS to alpha. ++ ++As of libpng version 1.5.2, png_set_expand_16() was added. It behaves as ++png_set_expand(); however, the resultant channels have 16 bits rather than 8. ++Use this when the output color or gray channels are made linear to avoid fairly ++severe accuracy loss. ++ ++ if (bit_depth < 16) ++ png_set_expand_16(png_ptr); ++ ++PNG can have files with 16 bits per channel. If you only can handle ++8 bits per channel, this will strip the pixels down to 8-bit. ++ ++ if (bit_depth == 16) ++#if PNG_LIBPNG_VER >= 10504 ++ png_set_scale_16(png_ptr); ++#else ++ png_set_strip_16(png_ptr); ++#endif ++ ++(The more accurate "png_set_scale_16()" API became available in libpng version ++1.5.4). ++ ++If you need to process the alpha channel on the image separately from the image ++data (for example if you convert it to a bitmap mask) it is possible to have ++libpng strip the channel leaving just RGB or gray data: ++ ++ if (color_type & PNG_COLOR_MASK_ALPHA) ++ png_set_strip_alpha(png_ptr); ++ ++If you strip the alpha channel you need to find some other way of dealing with ++the information. If, instead, you want to convert the image to an opaque ++version with no alpha channel use png_set_background; see below. ++ ++As of libpng version 1.5.2, almost all useful expansions are supported, the ++major ommissions are conversion of grayscale to indexed images (which can be ++done trivially in the application) and conversion of indexed to grayscale (which ++can be done by a trivial manipulation of the palette.) ++ ++In the following table, the 01 means grayscale with depth<8, 31 means ++indexed with depth<8, other numerals represent the color type, "T" means ++the tRNS chunk is present, A means an alpha channel is present, and O ++means tRNS or alpha is present but all pixels in the image are opaque. ++ ++ FROM 01 31 0 0T 0O 2 2T 2O 3 3T 3O 4A 4O 6A 6O ++ TO ++ 01 - [G] - - - - - - - - - - - - - ++ 31 [Q] Q [Q] [Q] [Q] Q Q Q Q Q Q [Q] [Q] Q Q ++ 0 1 G + . . G G G G G G B B GB GB ++ 0T lt Gt t + . Gt G G Gt G G Bt Bt GBt GBt ++ 0O lt Gt t . + Gt Gt G Gt Gt G Bt Bt GBt GBt ++ 2 C P C C C + . . C - - CB CB B B ++ 2T Ct - Ct C C t + t - - - CBt CBt Bt Bt ++ 2O Ct - Ct C C t t + - - - CBt CBt Bt Bt ++ 3 [Q] p [Q] [Q] [Q] Q Q Q + . . [Q] [Q] Q Q ++ 3T [Qt] p [Qt][Q] [Q] Qt Qt Qt t + t [Qt][Qt] Qt Qt ++ 3O [Qt] p [Qt][Q] [Q] Qt Qt Qt t t + [Qt][Qt] Qt Qt ++ 4A lA G A T T GA GT GT GA GT GT + BA G GBA ++ 4O lA GBA A T T GA GT GT GA GT GT BA + GBA G ++ 6A CA PA CA C C A T tT PA P P C CBA + BA ++ 6O CA PBA CA C C A tT T PA P P CBA C BA + ++ ++Within the matrix, ++ "+" identifies entries where 'from' and 'to' are the same. ++ "-" means the transformation is not supported. ++ "." means nothing is necessary (a tRNS chunk can just be ignored). ++ "t" means the transformation is obtained by png_set_tRNS. ++ "A" means the transformation is obtained by png_set_add_alpha(). ++ "X" means the transformation is obtained by png_set_expand(). ++ "1" means the transformation is obtained by ++ png_set_expand_gray_1_2_4_to_8() (and by png_set_expand() ++ if there is no transparency in the original or the final ++ format). ++ "C" means the transformation is obtained by png_set_gray_to_rgb(). ++ "G" means the transformation is obtained by png_set_rgb_to_gray(). ++ "P" means the transformation is obtained by ++ png_set_expand_palette_to_rgb(). ++ "p" means the transformation is obtained by png_set_packing(). ++ "Q" means the transformation is obtained by png_set_quantize(). ++ "T" means the transformation is obtained by ++ png_set_tRNS_to_alpha(). ++ "B" means the transformation is obtained by ++ png_set_background(), or png_strip_alpha(). ++ ++When an entry has multiple transforms listed all are required to cause the ++right overall transformation. When two transforms are separated by a comma ++either will do the job. When transforms are enclosed in [] the transform should ++do the job but this is currently unimplemented - a different format will result ++if the suggested transformations are used. ++ ++In PNG files, the alpha channel in an image ++is the level of opacity. If you need the alpha channel in an image to ++be the level of transparency instead of opacity, you can invert the ++alpha channel (or the tRNS chunk data) after it's read, so that 0 is ++fully opaque and 255 (in 8-bit or paletted images) or 65535 (in 16-bit ++images) is fully transparent, with ++ ++ png_set_invert_alpha(png_ptr); ++ ++PNG files pack pixels of bit depths 1, 2, and 4 into bytes as small as ++they can, resulting in, for example, 8 pixels per byte for 1 bit ++files. This code expands to 1 pixel per byte without changing the ++values of the pixels: ++ ++ if (bit_depth < 8) ++ png_set_packing(png_ptr); ++ ++PNG files have possible bit depths of 1, 2, 4, 8, and 16. All pixels ++stored in a PNG image have been "scaled" or "shifted" up to the next ++higher possible bit depth (e.g. from 5 bits/sample in the range [0,31] ++to 8 bits/sample in the range [0, 255]). However, it is also possible ++to convert the PNG pixel data back to the original bit depth of the ++image. This call reduces the pixels back down to the original bit depth: ++ ++ png_color_8p sig_bit; ++ ++ if (png_get_sBIT(png_ptr, info_ptr, &sig_bit)) ++ png_set_shift(png_ptr, sig_bit); ++ ++PNG files store 3-color pixels in red, green, blue order. This code ++changes the storage of the pixels to blue, green, red: ++ ++ if (color_type == PNG_COLOR_TYPE_RGB || ++ color_type == PNG_COLOR_TYPE_RGB_ALPHA) ++ png_set_bgr(png_ptr); ++ ++PNG files store RGB pixels packed into 3 or 6 bytes. This code expands them ++into 4 or 8 bytes for windowing systems that need them in this format: ++ ++ if (color_type == PNG_COLOR_TYPE_RGB) ++ png_set_filler(png_ptr, filler, PNG_FILLER_BEFORE); ++ ++where "filler" is the 8-bit or 16-bit number to fill with, and the location ++is either PNG_FILLER_BEFORE or PNG_FILLER_AFTER, depending upon whether ++you want the filler before the RGB or after. When filling an 8-bit pixel, ++the least significant 8 bits of the number are used, if a 16-bit number is ++supplied. This transformation does not affect images that already have full ++alpha channels. To add an opaque alpha channel, use filler=0xffff and ++PNG_FILLER_AFTER which will generate RGBA pixels. ++ ++Note that png_set_filler() does not change the color type. If you want ++to do that, you can add a true alpha channel with ++ ++ if (color_type == PNG_COLOR_TYPE_RGB || ++ color_type == PNG_COLOR_TYPE_GRAY) ++ png_set_add_alpha(png_ptr, filler, PNG_FILLER_AFTER); ++ ++where "filler" contains the alpha value to assign to each pixel. ++The png_set_add_alpha() function was added in libpng-1.2.7. ++ ++If you are reading an image with an alpha channel, and you need the ++data as ARGB instead of the normal PNG format RGBA: ++ ++ if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) ++ png_set_swap_alpha(png_ptr); ++ ++For some uses, you may want a grayscale image to be represented as ++RGB. This code will do that conversion: ++ ++ if (color_type == PNG_COLOR_TYPE_GRAY || ++ color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ++ png_set_gray_to_rgb(png_ptr); ++ ++Conversely, you can convert an RGB or RGBA image to grayscale or grayscale ++with alpha. ++ ++ if (color_type == PNG_COLOR_TYPE_RGB || ++ color_type == PNG_COLOR_TYPE_RGB_ALPHA) ++ png_set_rgb_to_gray(png_ptr, error_action, ++ double red_weight, double green_weight); ++ ++ error_action = 1: silently do the conversion ++ ++ error_action = 2: issue a warning if the original ++ image has any pixel where ++ red != green or red != blue ++ ++ error_action = 3: issue an error and abort the ++ conversion if the original ++ image has any pixel where ++ red != green or red != blue ++ ++ red_weight: weight of red component ++ ++ green_weight: weight of green component ++ If either weight is negative, default ++ weights are used. ++ ++In the corresponding fixed point API the red_weight and green_weight values are ++simply scaled by 100,000: ++ ++ png_set_rgb_to_gray(png_ptr, error_action, ++ png_fixed_point red_weight, ++ png_fixed_point green_weight); ++ ++If you have set error_action = 1 or 2, you can ++later check whether the image really was gray, after processing ++the image rows, with the png_get_rgb_to_gray_status(png_ptr) function. ++It will return a png_byte that is zero if the image was gray or ++1 if there were any non-gray pixels. Background and sBIT data ++will be silently converted to grayscale, using the green channel ++data for sBIT, regardless of the error_action setting. ++ ++The default values come from the PNG file cHRM chunk if present; otherwise, the ++defaults correspond to the ITU-R recommendation 709, and also the sRGB color ++space, as recommended in the Charles Poynton's Colour FAQ, ++Copyright (c) 2006-11-28 Charles Poynton, in section 9: ++ ++ ++ ++ Y = 0.2126 * R + 0.7152 * G + 0.0722 * B ++ ++Previous versions of this document, 1998 through 2002, recommended a slightly ++different formula: ++ ++ Y = 0.212671 * R + 0.715160 * G + 0.072169 * B ++ ++Libpng uses an integer approximation: ++ ++ Y = (6968 * R + 23434 * G + 2366 * B)/32768 ++ ++The calculation is done in a linear colorspace, if the image gamma ++can be determined. ++ ++The png_set_background() function has been described already; it tells libpng to ++composite images with alpha or simple transparency against the supplied ++background color. For compatibility with versions of libpng earlier than ++libpng-1.5.4 it is recommended that you call the function after reading the file ++header, even if you don't want to use the color in a bKGD chunk, if one exists. ++ ++If the PNG file contains a bKGD chunk (PNG_INFO_bKGD valid), ++you may use this color, or supply another color more suitable for ++the current display (e.g., the background color from a web page). You ++need to tell libpng how the color is represented, both the format of the ++component values in the color (the number of bits) and the gamma encoding of the ++color. The function takes two arguments, background_gamma_mode and need_expand ++to convey this information; however, only two combinations are likely to be ++useful: ++ ++ png_color_16 my_background; ++ png_color_16p image_background; ++ ++ if (png_get_bKGD(png_ptr, info_ptr, &image_background)) ++ png_set_background(png_ptr, image_background, ++ PNG_BACKGROUND_GAMMA_FILE, 1/*needs to be expanded*/, 1); ++ else ++ png_set_background(png_ptr, &my_background, ++ PNG_BACKGROUND_GAMMA_SCREEN, 0/*do not expand*/, 1); ++ ++The second call was described above - my_background is in the format of the ++final, display, output produced by libpng. Because you now know the format of ++the PNG it is possible to avoid the need to choose either 8-bit or 16-bit ++output and to retain palette images (the palette colors will be modified ++appropriately and the tRNS chunk removed.) However, if you are doing this, ++take great care not to ask for transformations without checking first that ++they apply! ++ ++In the first call the background color has the original bit depth and color type ++of the PNG file. So, for palette images the color is supplied as a palette ++index and for low bit greyscale images the color is a reduced bit value in ++image_background->gray. ++ ++If you didn't call png_set_gamma() before reading the file header, for example ++if you need your code to remain compatible with older versions of libpng prior ++to libpng-1.5.4, this is the place to call it. ++ ++Do not call it if you called png_set_alpha_mode(); doing so will damage the ++settings put in place by png_set_alpha_mode(). (If png_set_alpha_mode() is ++supported then you can certainly do png_set_gamma() before reading the PNG ++header.) ++ ++This API unconditionally sets the screen and file gamma values, so it will ++override the value in the PNG file unless it is called before the PNG file ++reading starts. For this reason you must always call it with the PNG file ++value when you call it in this position: ++ ++ if (png_get_gAMA(png_ptr, info_ptr, &file_gamma)) ++ png_set_gamma(png_ptr, screen_gamma, file_gamma); ++ ++ else ++ png_set_gamma(png_ptr, screen_gamma, 0.45455); ++ ++If you need to reduce an RGB file to a paletted file, or if a paletted ++file has more entries than will fit on your screen, png_set_quantize() ++will do that. Note that this is a simple match quantization that merely ++finds the closest color available. This should work fairly well with ++optimized palettes, but fairly badly with linear color cubes. If you ++pass a palette that is larger than maximum_colors, the file will ++reduce the number of colors in the palette so it will fit into ++maximum_colors. If there is a histogram, libpng will use it to make ++more intelligent choices when reducing the palette. If there is no ++histogram, it may not do as good a job. ++ ++ if (color_type & PNG_COLOR_MASK_COLOR) ++ { ++ if (png_get_valid(png_ptr, info_ptr, ++ PNG_INFO_PLTE)) ++ { ++ png_uint_16p histogram = NULL; ++ ++ png_get_hIST(png_ptr, info_ptr, ++ &histogram); ++ png_set_quantize(png_ptr, palette, num_palette, ++ max_screen_colors, histogram, 1); ++ } ++ ++ else ++ { ++ png_color std_color_cube[MAX_SCREEN_COLORS] = ++ { ... colors ... }; ++ ++ png_set_quantize(png_ptr, std_color_cube, ++ MAX_SCREEN_COLORS, MAX_SCREEN_COLORS, ++ NULL,0); ++ } ++ } ++ ++PNG files describe monochrome as black being zero and white being one. ++The following code will reverse this (make black be one and white be ++zero): ++ ++ if (bit_depth == 1 && color_type == PNG_COLOR_TYPE_GRAY) ++ png_set_invert_mono(png_ptr); ++ ++This function can also be used to invert grayscale and gray-alpha images: ++ ++ if (color_type == PNG_COLOR_TYPE_GRAY || ++ color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ++ png_set_invert_mono(png_ptr); ++ ++PNG files store 16-bit pixels in network byte order (big-endian, ++ie. most significant bits first). This code changes the storage to the ++other way (little-endian, i.e. least significant bits first, the ++way PCs store them): ++ ++ if (bit_depth == 16) ++ png_set_swap(png_ptr); ++ ++If you are using packed-pixel images (1, 2, or 4 bits/pixel), and you ++need to change the order the pixels are packed into bytes, you can use: ++ ++ if (bit_depth < 8) ++ png_set_packswap(png_ptr); ++ ++Finally, you can write your own transformation function if none of ++the existing ones meets your needs. This is done by setting a callback ++with ++ ++ png_set_read_user_transform_fn(png_ptr, ++ read_transform_fn); ++ ++You must supply the function ++ ++ void read_transform_fn(png_structp png_ptr, png_row_infop ++ row_info, png_bytep data) ++ ++See pngtest.c for a working example. Your function will be called ++after all of the other transformations have been processed. Take care with ++interlaced images if you do the interlace yourself - the width of the row is the ++width in 'row_info', not the overall image width. ++ ++If supported, libpng provides two information routines that you can use to find ++where you are in processing the image: ++ ++ png_get_current_pass_number(png_structp png_ptr); ++ png_get_current_row_number(png_structp png_ptr); ++ ++Don't try using these outside a transform callback - firstly they are only ++supported if user transforms are supported, secondly they may well return ++unexpected results unless the row is actually being processed at the moment they ++are called. ++ ++With interlaced ++images the value returned is the row in the input sub-image image. Use ++PNG_ROW_FROM_PASS_ROW(row, pass) and PNG_COL_FROM_PASS_COL(col, pass) to ++find the output pixel (x,y) given an interlaced sub-image pixel (row,col,pass). ++ ++The discussion of interlace handling above contains more information on how to ++use these values. ++ ++You can also set up a pointer to a user structure for use by your ++callback function, and you can inform libpng that your transform ++function will change the number of channels or bit depth with the ++function ++ ++ png_set_user_transform_info(png_ptr, user_ptr, ++ user_depth, user_channels); ++ ++The user's application, not libpng, is responsible for allocating and ++freeing any memory required for the user structure. ++ ++You can retrieve the pointer via the function ++png_get_user_transform_ptr(). For example: ++ ++ voidp read_user_transform_ptr = ++ png_get_user_transform_ptr(png_ptr); ++ ++The last thing to handle is interlacing; this is covered in detail below, ++but you must call the function here if you want libpng to handle expansion ++of the interlaced image. ++ ++ number_of_passes = png_set_interlace_handling(png_ptr); ++ ++After setting the transformations, libpng can update your png_info ++structure to reflect any transformations you've requested with this ++call. ++ ++ png_read_update_info(png_ptr, info_ptr); ++ ++This is most useful to update the info structure's rowbytes ++field so you can use it to allocate your image memory. This function ++will also update your palette with the correct screen_gamma and ++background if these have been given with the calls above. You may ++only call png_read_update_info() once with a particular info_ptr. ++ ++After you call png_read_update_info(), you can allocate any ++memory you need to hold the image. The row data is simply ++raw byte data for all forms of images. As the actual allocation ++varies among applications, no example will be given. If you ++are allocating one large chunk, you will need to build an ++array of pointers to each row, as it will be needed for some ++of the functions below. ++ ++Be sure that your platform can allocate the buffer that you'll need. ++libpng internally checks for oversize width, but you'll need to ++do your own check for number_of_rows*width*pixel_size if you are using ++a multiple-row buffer: ++ ++ /* Guard against integer overflow */ ++ if (number_of_rows > PNG_SIZE_MAX/(width*pixel_size)) { ++ png_error(png_ptr,"image_data buffer would be too large"); ++ } ++ ++Remember: Before you call png_read_update_info(), the png_get_*() ++functions return the values corresponding to the original PNG image. ++After you call png_read_update_info the values refer to the image ++that libpng will output. Consequently you must call all the png_set_ ++functions before you call png_read_update_info(). This is particularly ++important for png_set_interlace_handling() - if you are going to call ++png_read_update_info() you must call png_set_interlace_handling() before ++it unless you want to receive interlaced output. ++ ++.SS Reading image data ++ ++After you've allocated memory, you can read the image data. ++The simplest way to do this is in one function call. If you are ++allocating enough memory to hold the whole image, you can just ++call png_read_image() and libpng will read in all the image data ++and put it in the memory area supplied. You will need to pass in ++an array of pointers to each row. ++ ++This function automatically handles interlacing, so you don't ++need to call png_set_interlace_handling() (unless you call ++png_read_update_info()) or call this function multiple times, or any ++of that other stuff necessary with png_read_rows(). ++ ++ png_read_image(png_ptr, row_pointers); ++ ++where row_pointers is: ++ ++ png_bytep row_pointers[height]; ++ ++You can point to void or char or whatever you use for pixels. ++ ++If you don't want to read in the whole image at once, you can ++use png_read_rows() instead. If there is no interlacing (check ++interlace_type == PNG_INTERLACE_NONE), this is simple: ++ ++ png_read_rows(png_ptr, row_pointers, NULL, ++ number_of_rows); ++ ++where row_pointers is the same as in the png_read_image() call. ++ ++If you are doing this just one row at a time, you can do this with ++a single row_pointer instead of an array of row_pointers: ++ ++ png_bytep row_pointer = row; ++ png_read_row(png_ptr, row_pointer, NULL); ++ ++If the file is interlaced (interlace_type != 0 in the IHDR chunk), things ++get somewhat harder. The only current (PNG Specification version 1.2) ++interlacing type for PNG is (interlace_type == PNG_INTERLACE_ADAM7); ++a somewhat complicated 2D interlace scheme, known as Adam7, that ++breaks down an image into seven smaller images of varying size, based ++on an 8x8 grid. This number is defined (from libpng 1.5) as ++PNG_INTERLACE_ADAM7_PASSES in png.h ++ ++libpng can fill out those images or it can give them to you "as is". ++It is almost always better to have libpng handle the interlacing for you. ++If you want the images filled out, there are two ways to do that. The one ++mentioned in the PNG specification is to expand each pixel to cover ++those pixels that have not been read yet (the "rectangle" method). ++This results in a blocky image for the first pass, which gradually ++smooths out as more pixels are read. The other method is the "sparkle" ++method, where pixels are drawn only in their final locations, with the ++rest of the image remaining whatever colors they were initialized to ++before the start of the read. The first method usually looks better, ++but tends to be slower, as there are more pixels to put in the rows. ++ ++If, as is likely, you want libpng to expand the images, call this before ++calling png_start_read_image() or png_read_update_info(): ++ ++ if (interlace_type == PNG_INTERLACE_ADAM7) ++ number_of_passes ++ = png_set_interlace_handling(png_ptr); ++ ++This will return the number of passes needed. Currently, this is seven, ++but may change if another interlace type is added. This function can be ++called even if the file is not interlaced, where it will return one pass. ++You then need to read the whole image 'number_of_passes' times. Each time ++will distribute the pixels from the current pass to the correct place in ++the output image, so you need to supply the same rows to png_read_rows in ++each pass. ++ ++If you are not going to display the image after each pass, but are ++going to wait until the entire image is read in, use the sparkle ++effect. This effect is faster and the end result of either method ++is exactly the same. If you are planning on displaying the image ++after each pass, the "rectangle" effect is generally considered the ++better looking one. ++ ++If you only want the "sparkle" effect, just call png_read_row() or ++png_read_rows() as ++normal, with the third parameter NULL. Make sure you make pass over ++the image number_of_passes times, and you don't change the data in the ++rows between calls. You can change the locations of the data, just ++not the data. Each pass only writes the pixels appropriate for that ++pass, and assumes the data from previous passes is still valid. ++ ++ png_read_rows(png_ptr, row_pointers, NULL, ++ number_of_rows); ++ or ++ png_read_row(png_ptr, row_pointers, NULL); ++ ++If you only want the first effect (the rectangles), do the same as ++before except pass the row buffer in the third parameter, and leave ++the second parameter NULL. ++ ++ png_read_rows(png_ptr, NULL, row_pointers, ++ number_of_rows); ++ or ++ png_read_row(png_ptr, NULL, row_pointers); ++ ++If you don't want libpng to handle the interlacing details, just call ++png_read_rows() PNG_INTERLACE_ADAM7_PASSES times to read in all the images. ++Each of the images is a valid image by itself; however, you will almost ++certainly need to distribute the pixels from each sub-image to the ++correct place. This is where everything gets very tricky. ++ ++If you want to retrieve the separate images you must pass the correct ++number of rows to each successive call of png_read_rows(). The calculation ++gets pretty complicated for small images, where some sub-images may ++not even exist because either their width or height ends up zero. ++libpng provides two macros to help you in 1.5 and later versions: ++ ++ png_uint_32 width = PNG_PASS_COLS(image_width, pass_number); ++ png_uint_32 height = PNG_PASS_ROWS(image_height, pass_number); ++ ++Respectively these tell you the width and height of the sub-image ++corresponding to the numbered pass. 'pass' is in in the range 0 to 6 - ++this can be confusing because the specification refers to the same passes ++as 1 to 7! Be careful, you must check both the width and height before ++calling png_read_rows() and not call it for that pass if either is zero. ++ ++You can, of course, read each sub-image row by row. If you want to ++produce optimal code to make a pixel-by-pixel transformation of an ++interlaced image this is the best approach; read each row of each pass, ++transform it, and write it out to a new interlaced image. ++ ++If you want to de-interlace the image yourself libpng provides further ++macros to help that tell you where to place the pixels in the output image. ++Because the interlacing scheme is rectangular - sub-image pixels are always ++arranged on a rectangular grid - all you need to know for each pass is the ++starting column and row in the output image of the first pixel plus the ++spacing between each pixel. As of libpng 1.5 there are four macros to ++retrieve this information: ++ ++ png_uint_32 x = PNG_PASS_START_COL(pass); ++ png_uint_32 y = PNG_PASS_START_ROW(pass); ++ png_uint_32 xStep = 1U << PNG_PASS_COL_SHIFT(pass); ++ png_uint_32 yStep = 1U << PNG_PASS_ROW_SHIFT(pass); ++ ++These allow you to write the obvious loop: ++ ++ png_uint_32 input_y = 0; ++ png_uint_32 output_y = PNG_PASS_START_ROW(pass); ++ ++ while (output_y < output_image_height) ++ { ++ png_uint_32 input_x = 0; ++ png_uint_32 output_x = PNG_PASS_START_COL(pass); ++ ++ while (output_x < output_image_width) ++ { ++ image[output_y][output_x] = ++ subimage[pass][input_y][input_x++]; ++ ++ output_x += xStep; ++ } ++ ++ ++input_y; ++ output_y += yStep; ++ } ++ ++Notice that the steps between successive output rows and columns are ++returned as shifts. This is possible because the pixels in the subimages ++are always a power of 2 apart - 1, 2, 4 or 8 pixels - in the original ++image. In practice you may need to directly calculate the output coordinate ++given an input coordinate. libpng provides two further macros for this ++purpose: ++ ++ png_uint_32 output_x = PNG_COL_FROM_PASS_COL(input_x, pass); ++ png_uint_32 output_y = PNG_ROW_FROM_PASS_ROW(input_y, pass); ++ ++Finally a pair of macros are provided to tell you if a particular image ++row or column appears in a given pass: ++ ++ int col_in_pass = PNG_COL_IN_INTERLACE_PASS(output_x, pass); ++ int row_in_pass = PNG_ROW_IN_INTERLACE_PASS(output_y, pass); ++ ++Bear in mind that you will probably also need to check the width and height ++of the pass in addition to the above to be sure the pass even exists! ++ ++With any luck you are convinced by now that you don't want to do your own ++interlace handling. In reality normally the only good reason for doing this ++is if you are processing PNG files on a pixel-by-pixel basis and don't want ++to load the whole file into memory when it is interlaced. ++ ++libpng includes a test program, pngvalid, that illustrates reading and ++writing of interlaced images. If you can't get interlacing to work in your ++code and don't want to leave it to libpng (the recommended approach), see ++how pngvalid.c does it. ++ ++.SS Finishing a sequential read ++ ++After you are finished reading the image through the ++low-level interface, you can finish reading the file. ++ ++If you want to use a different crc action for handling CRC errors in ++chunks after the image data, you can call png_set_crc_action() ++again at this point. ++ ++If you are interested in comments or time, which may be stored either ++before or after the image data, you should pass the separate png_info ++struct if you want to keep the comments from before and after the image ++separate. ++ ++ png_infop end_info = png_create_info_struct(png_ptr); ++ ++ if (!end_info) ++ { ++ png_destroy_read_struct(&png_ptr, &info_ptr, ++ (png_infopp)NULL); ++ return ERROR; ++ } ++ ++ png_read_end(png_ptr, end_info); ++ ++If you are not interested, you should still call png_read_end() ++but you can pass NULL, avoiding the need to create an end_info structure. ++If you do this, libpng will not process any chunks after IDAT other than ++skipping over them and perhaps (depending on whether you have called ++png_set_crc_action) checking their CRCs while looking for the IEND chunk. ++ ++ png_read_end(png_ptr, (png_infop)NULL); ++ ++If you don't call png_read_end(), then your file pointer will be ++left pointing to the first chunk after the last IDAT, which is probably ++not what you want if you expect to read something beyond the end of ++the PNG datastream. ++ ++When you are done, you can free all memory allocated by libpng like this: ++ ++ png_destroy_read_struct(&png_ptr, &info_ptr, ++ &end_info); ++ ++or, if you didn't create an end_info structure, ++ ++ png_destroy_read_struct(&png_ptr, &info_ptr, ++ (png_infopp)NULL); ++ ++It is also possible to individually free the info_ptr members that ++point to libpng-allocated storage with the following function: ++ ++ png_free_data(png_ptr, info_ptr, mask, seq) ++ ++ mask - identifies data to be freed, a mask ++ containing the bitwise OR of one or ++ more of ++ PNG_FREE_PLTE, PNG_FREE_TRNS, ++ PNG_FREE_HIST, PNG_FREE_ICCP, ++ PNG_FREE_PCAL, PNG_FREE_ROWS, ++ PNG_FREE_SCAL, PNG_FREE_SPLT, ++ PNG_FREE_TEXT, PNG_FREE_UNKN, ++ or simply PNG_FREE_ALL ++ ++ seq - sequence number of item to be freed ++ (\-1 for all items) ++ ++This function may be safely called when the relevant storage has ++already been freed, or has not yet been allocated, or was allocated ++by the user and not by libpng, and will in those cases do nothing. ++The "seq" parameter is ignored if only one item of the selected data ++type, such as PLTE, is allowed. If "seq" is not \-1, and multiple items ++are allowed for the data type identified in the mask, such as text or ++sPLT, only the n'th item in the structure is freed, where n is "seq". ++ ++The default behavior is only to free data that was allocated internally ++by libpng. This can be changed, so that libpng will not free the data, ++or so that it will free data that was allocated by the user with png_malloc() ++or png_calloc() and passed in via a png_set_*() function, with ++ ++ png_data_freer(png_ptr, info_ptr, freer, mask) ++ ++ freer - one of ++ PNG_DESTROY_WILL_FREE_DATA ++ PNG_SET_WILL_FREE_DATA ++ PNG_USER_WILL_FREE_DATA ++ ++ mask - which data elements are affected ++ same choices as in png_free_data() ++ ++This function only affects data that has already been allocated. ++You can call this function after reading the PNG data but before calling ++any png_set_*() functions, to control whether the user or the png_set_*() ++function is responsible for freeing any existing data that might be present, ++and again after the png_set_*() functions to control whether the user ++or png_destroy_*() is supposed to free the data. When the user assumes ++responsibility for libpng-allocated data, the application must use ++png_free() to free it, and when the user transfers responsibility to libpng ++for data that the user has allocated, the user must have used png_malloc() ++or png_calloc() to allocate it. ++ ++If you allocated your row_pointers in a single block, as suggested above in ++the description of the high level read interface, you must not transfer ++responsibility for freeing it to the png_set_rows or png_read_destroy function, ++because they would also try to free the individual row_pointers[i]. ++ ++If you allocated text_ptr.text, text_ptr.lang, and text_ptr.translated_keyword ++separately, do not transfer responsibility for freeing text_ptr to libpng, ++because when libpng fills a png_text structure it combines these members with ++the key member, and png_free_data() will free only text_ptr.key. Similarly, ++if you transfer responsibility for free'ing text_ptr from libpng to your ++application, your application must not separately free those members. ++ ++The png_free_data() function will turn off the "valid" flag for anything ++it frees. If you need to turn the flag off for a chunk that was freed by ++your application instead of by libpng, you can use ++ ++ png_set_invalid(png_ptr, info_ptr, mask); ++ ++ mask - identifies the chunks to be made invalid, ++ containing the bitwise OR of one or ++ more of ++ PNG_INFO_gAMA, PNG_INFO_sBIT, ++ PNG_INFO_cHRM, PNG_INFO_PLTE, ++ PNG_INFO_tRNS, PNG_INFO_bKGD, ++ PNG_INFO_eXIf, ++ PNG_INFO_hIST, PNG_INFO_pHYs, ++ PNG_INFO_oFFs, PNG_INFO_tIME, ++ PNG_INFO_pCAL, PNG_INFO_sRGB, ++ PNG_INFO_iCCP, PNG_INFO_sPLT, ++ PNG_INFO_sCAL, PNG_INFO_IDAT ++ ++For a more compact example of reading a PNG image, see the file example.c. ++ ++.SS Reading PNG files progressively ++ ++The progressive reader is slightly different from the non-progressive ++reader. Instead of calling png_read_info(), png_read_rows(), and ++png_read_end(), you make one call to png_process_data(), which calls ++callbacks when it has the info, a row, or the end of the image. You ++set up these callbacks with png_set_progressive_read_fn(). You don't ++have to worry about the input/output functions of libpng, as you are ++giving the library the data directly in png_process_data(). I will ++assume that you have read the section on reading PNG files above, ++so I will only highlight the differences (although I will show ++all of the code). ++ ++png_structp png_ptr; ++png_infop info_ptr; ++ ++ /* An example code fragment of how you would ++ initialize the progressive reader in your ++ application. */ ++ int ++ initialize_png_reader() ++ { ++ png_ptr = png_create_read_struct ++ (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr, ++ user_error_fn, user_warning_fn); ++ ++ if (!png_ptr) ++ return ERROR; ++ ++ info_ptr = png_create_info_struct(png_ptr); ++ ++ if (!info_ptr) ++ { ++ png_destroy_read_struct(&png_ptr, ++ (png_infopp)NULL, (png_infopp)NULL); ++ return ERROR; ++ } ++ ++ if (setjmp(png_jmpbuf(png_ptr))) ++ { ++ png_destroy_read_struct(&png_ptr, &info_ptr, ++ (png_infopp)NULL); ++ return ERROR; ++ } ++ ++ /* This one's new. You can provide functions ++ to be called when the header info is valid, ++ when each row is completed, and when the image ++ is finished. If you aren't using all functions, ++ you can specify NULL parameters. Even when all ++ three functions are NULL, you need to call ++ png_set_progressive_read_fn(). You can use ++ any struct as the user_ptr (cast to a void pointer ++ for the function call), and retrieve the pointer ++ from inside the callbacks using the function ++ ++ png_get_progressive_ptr(png_ptr); ++ ++ which will return a void pointer, which you have ++ to cast appropriately. ++ */ ++ png_set_progressive_read_fn(png_ptr, (void *)user_ptr, ++ info_callback, row_callback, end_callback); ++ ++ return 0; ++ } ++ ++ /* A code fragment that you call as you receive blocks ++ of data */ ++ int ++ process_data(png_bytep buffer, png_uint_32 length) ++ { ++ if (setjmp(png_jmpbuf(png_ptr))) ++ { ++ png_destroy_read_struct(&png_ptr, &info_ptr, ++ (png_infopp)NULL); ++ return ERROR; ++ } ++ ++ /* This one's new also. Simply give it a chunk ++ of data from the file stream (in order, of ++ course). On machines with segmented memory ++ models machines, don't give it any more than ++ 64K. The library seems to run fine with sizes ++ of 4K. Although you can give it much less if ++ necessary (I assume you can give it chunks of ++ 1 byte, I haven't tried less than 256 bytes ++ yet). When this function returns, you may ++ want to display any rows that were generated ++ in the row callback if you don't already do ++ so there. ++ */ ++ png_process_data(png_ptr, info_ptr, buffer, length); ++ ++ /* At this point you can call png_process_data_skip if ++ you want to handle data the library will skip yourself; ++ it simply returns the number of bytes to skip (and stops ++ libpng skipping that number of bytes on the next ++ png_process_data call). ++ return 0; ++ } ++ ++ /* This function is called (as set by ++ png_set_progressive_read_fn() above) when enough data ++ has been supplied so all of the header has been ++ read. ++ */ ++ void ++ info_callback(png_structp png_ptr, png_infop info) ++ { ++ /* Do any setup here, including setting any of ++ the transformations mentioned in the Reading ++ PNG files section. For now, you _must_ call ++ either png_start_read_image() or ++ png_read_update_info() after all the ++ transformations are set (even if you don't set ++ any). You may start getting rows before ++ png_process_data() returns, so this is your ++ last chance to prepare for that. ++ ++ This is where you turn on interlace handling, ++ assuming you don't want to do it yourself. ++ ++ If you need to you can stop the processing of ++ your original input data at this point by calling ++ png_process_data_pause. This returns the number ++ of unprocessed bytes from the last png_process_data ++ call - it is up to you to ensure that the next call ++ sees these bytes again. If you don't want to bother ++ with this you can get libpng to cache the unread ++ bytes by setting the 'save' parameter (see png.h) but ++ then libpng will have to copy the data internally. ++ */ ++ } ++ ++ /* This function is called when each row of image ++ data is complete */ ++ void ++ row_callback(png_structp png_ptr, png_bytep new_row, ++ png_uint_32 row_num, int pass) ++ { ++ /* If the image is interlaced, and you turned ++ on the interlace handler, this function will ++ be called for every row in every pass. Some ++ of these rows will not be changed from the ++ previous pass. When the row is not changed, ++ the new_row variable will be NULL. The rows ++ and passes are called in order, so you don't ++ really need the row_num and pass, but I'm ++ supplying them because it may make your life ++ easier. ++ ++ If you did not turn on interlace handling then ++ the callback is called for each row of each ++ sub-image when the image is interlaced. In this ++ case 'row_num' is the row in the sub-image, not ++ the row in the output image as it is in all other ++ cases. ++ ++ For the non-NULL rows of interlaced images when ++ you have switched on libpng interlace handling, ++ you must call png_progressive_combine_row() ++ passing in the row and the old row. You can ++ call this function for NULL rows (it will just ++ return) and for non-interlaced images (it just ++ does the memcpy for you) if it will make the ++ code easier. Thus, you can just do this for ++ all cases if you switch on interlace handling; ++ */ ++ ++ png_progressive_combine_row(png_ptr, old_row, ++ new_row); ++ ++ /* where old_row is what was displayed ++ previously for the row. Note that the first ++ pass (pass == 0, really) will completely cover ++ the old row, so the rows do not have to be ++ initialized. After the first pass (and only ++ for interlaced images), you will have to pass ++ the current row, and the function will combine ++ the old row and the new row. ++ ++ You can also call png_process_data_pause in this ++ callback - see above. ++ */ ++ } ++ ++ void ++ end_callback(png_structp png_ptr, png_infop info) ++ { ++ /* This function is called after the whole image ++ has been read, including any chunks after the ++ image (up to and including the IEND). You ++ will usually have the same info chunk as you ++ had in the header, although some data may have ++ been added to the comments and time fields. ++ ++ Most people won't do much here, perhaps setting ++ a flag that marks the image as finished. ++ */ ++ } ++ ++ ++ ++.SH IV. Writing ++ ++Much of this is very similar to reading. However, everything of ++importance is repeated here, so you won't have to constantly look ++back up in the reading section to understand writing. ++ ++.SS Setup ++ ++You will want to do the I/O initialization before you get into libpng, ++so if it doesn't work, you don't have anything to undo. If you are not ++using the standard I/O functions, you will need to replace them with ++custom writing functions. See the discussion under Customizing libpng. ++ ++ FILE *fp = fopen(file_name, "wb"); ++ ++ if (!fp) ++ return ERROR; ++ ++Next, png_struct and png_info need to be allocated and initialized. ++As these can be both relatively large, you may not want to store these ++on the stack, unless you have stack space to spare. Of course, you ++will want to check if they return NULL. If you are also reading, ++you won't want to name your read structure and your write structure ++both "png_ptr"; you can call them anything you like, such as ++"read_ptr" and "write_ptr". Look at pngtest.c, for example. ++ ++ png_structp png_ptr = png_create_write_struct ++ (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr, ++ user_error_fn, user_warning_fn); ++ ++ if (!png_ptr) ++ return ERROR; ++ ++ png_infop info_ptr = png_create_info_struct(png_ptr); ++ if (!info_ptr) ++ { ++ png_destroy_write_struct(&png_ptr, ++ (png_infopp)NULL); ++ return ERROR; ++ } ++ ++If you want to use your own memory allocation routines, ++define PNG_USER_MEM_SUPPORTED and use ++png_create_write_struct_2() instead of png_create_write_struct(): ++ ++ png_structp png_ptr = png_create_write_struct_2 ++ (PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr, ++ user_error_fn, user_warning_fn, (png_voidp) ++ user_mem_ptr, user_malloc_fn, user_free_fn); ++ ++After you have these structures, you will need to set up the ++error handling. When libpng encounters an error, it expects to ++longjmp() back to your routine. Therefore, you will need to call ++setjmp() and pass the png_jmpbuf(png_ptr). If you ++write the file from different routines, you will need to update ++the png_jmpbuf(png_ptr) every time you enter a new routine that will ++call a png_*() function. See your documentation of setjmp/longjmp ++for your compiler for more information on setjmp/longjmp. See ++the discussion on libpng error handling in the Customizing Libpng ++section below for more information on the libpng error handling. ++ ++ if (setjmp(png_jmpbuf(png_ptr))) ++ { ++ png_destroy_write_struct(&png_ptr, &info_ptr); ++ fclose(fp); ++ return ERROR; ++ } ++ ... ++ return; ++ ++If you would rather avoid the complexity of setjmp/longjmp issues, ++you can compile libpng with PNG_NO_SETJMP, in which case ++errors will result in a call to PNG_ABORT() which defaults to abort(). ++ ++You can #define PNG_ABORT() to a function that does something ++more useful than abort(), as long as your function does not ++return. ++ ++Checking for invalid palette index on write was added at libpng ++1.5.10. If a pixel contains an invalid (out-of-range) index libpng issues ++a benign error. This is enabled by default because this condition is an ++error according to the PNG specification, Clause 11.3.2, but the error can ++be ignored in each png_ptr with ++ ++ png_set_check_for_invalid_index(png_ptr, 0); ++ ++If the error is ignored, or if png_benign_error() treats it as a warning, ++any invalid pixels are written as-is by the encoder, resulting in an ++invalid PNG datastream as output. In this case the application is ++responsible for ensuring that the pixel indexes are in range when it writes ++a PLTE chunk with fewer entries than the bit depth would allow. ++ ++Now you need to set up the output code. The default for libpng is to ++use the C function fwrite(). If you use this, you will need to pass a ++valid FILE * in the function png_init_io(). Be sure that the file is ++opened in binary mode. Again, if you wish to handle writing data in ++another way, see the discussion on libpng I/O handling in the Customizing ++Libpng section below. ++ ++ png_init_io(png_ptr, fp); ++ ++If you are embedding your PNG into a datastream such as MNG, and don't ++want libpng to write the 8-byte signature, or if you have already ++written the signature in your application, use ++ ++ png_set_sig_bytes(png_ptr, 8); ++ ++to inform libpng that it should not write a signature. ++ ++.SS Write callbacks ++ ++At this point, you can set up a callback function that will be ++called after each row has been written, which you can use to control ++a progress meter or the like. It's demonstrated in pngtest.c. ++You must supply a function ++ ++ void write_row_callback(png_structp png_ptr, png_uint_32 row, ++ int pass); ++ { ++ /* put your code here */ ++ } ++ ++(You can give it another name that you like instead of "write_row_callback") ++ ++To inform libpng about your function, use ++ ++ png_set_write_status_fn(png_ptr, write_row_callback); ++ ++When this function is called the row has already been completely processed and ++it has also been written out. The 'row' and 'pass' refer to the next row to be ++handled. For the ++non-interlaced case the row that was just handled is simply one less than the ++passed in row number, and pass will always be 0. For the interlaced case the ++same applies unless the row value is 0, in which case the row just handled was ++the last one from one of the preceding passes. Because interlacing may skip a ++pass you cannot be sure that the preceding pass is just 'pass\-1', if you really ++need to know what the last pass is record (row,pass) from the callback and use ++the last recorded value each time. ++ ++As with the user transform you can find the output row using the ++PNG_ROW_FROM_PASS_ROW macro. ++ ++You now have the option of modifying how the compression library will ++run. The following functions are mainly for testing, but may be useful ++in some cases, like if you need to write PNG files extremely fast and ++are willing to give up some compression, or if you want to get the ++maximum possible compression at the expense of slower writing. If you ++have no special needs in this area, let the library do what it wants by ++not calling this function at all, as it has been tuned to deliver a good ++speed/compression ratio. The second parameter to png_set_filter() is ++the filter method, for which the only valid values are 0 (as of the ++July 1999 PNG specification, version 1.2) or 64 (if you are writing ++a PNG datastream that is to be embedded in a MNG datastream). The third ++parameter is a flag that indicates which filter type(s) are to be tested ++for each scanline. See the PNG specification for details on the specific ++filter types. ++ ++ ++ /* turn on or off filtering, and/or choose ++ specific filters. You can use either a single ++ PNG_FILTER_VALUE_NAME or the bitwise OR of one ++ or more PNG_FILTER_NAME masks. ++ */ ++ png_set_filter(png_ptr, 0, ++ PNG_FILTER_NONE | PNG_FILTER_VALUE_NONE | ++ PNG_FILTER_SUB | PNG_FILTER_VALUE_SUB | ++ PNG_FILTER_UP | PNG_FILTER_VALUE_UP | ++ PNG_FILTER_AVG | PNG_FILTER_VALUE_AVG | ++ PNG_FILTER_PAETH | PNG_FILTER_VALUE_PAETH| ++ PNG_ALL_FILTERS | PNG_FAST_FILTERS); ++ ++If an application wants to start and stop using particular filters during ++compression, it should start out with all of the filters (to ensure that ++the previous row of pixels will be stored in case it's needed later), ++and then add and remove them after the start of compression. ++ ++If you are writing a PNG datastream that is to be embedded in a MNG ++datastream, the second parameter can be either 0 or 64. ++ ++The png_set_compression_*() functions interface to the zlib compression ++library, and should mostly be ignored unless you really know what you are ++doing. The only generally useful call is png_set_compression_level() ++which changes how much time zlib spends on trying to compress the image ++data. See the Compression Library (zlib.h and algorithm.txt, distributed ++with zlib) for details on the compression levels. ++ ++ #include zlib.h ++ ++ /* Set the zlib compression level */ ++ png_set_compression_level(png_ptr, ++ Z_BEST_COMPRESSION); ++ ++ /* Set other zlib parameters for compressing IDAT */ ++ png_set_compression_mem_level(png_ptr, 8); ++ png_set_compression_strategy(png_ptr, ++ Z_DEFAULT_STRATEGY); ++ png_set_compression_window_bits(png_ptr, 15); ++ png_set_compression_method(png_ptr, 8); ++ png_set_compression_buffer_size(png_ptr, 8192) ++ ++ /* Set zlib parameters for text compression ++ * If you don't call these, the parameters ++ * fall back on those defined for IDAT chunks ++ */ ++ png_set_text_compression_mem_level(png_ptr, 8); ++ png_set_text_compression_strategy(png_ptr, ++ Z_DEFAULT_STRATEGY); ++ png_set_text_compression_window_bits(png_ptr, 15); ++ png_set_text_compression_method(png_ptr, 8); ++ ++.SS Setting the contents of info for output ++ ++You now need to fill in the png_info structure with all the data you ++wish to write before the actual image. Note that the only thing you ++are allowed to write after the image is the text chunks and the time ++chunk (as of PNG Specification 1.2, anyway). See png_write_end() and ++the latest PNG specification for more information on that. If you ++wish to write them before the image, fill them in now, and flag that ++data as being valid. If you want to wait until after the data, don't ++fill them until png_write_end(). For all the fields in png_info and ++their data types, see png.h. For explanations of what the fields ++contain, see the PNG specification. ++ ++Some of the more important parts of the png_info are: ++ ++ png_set_IHDR(png_ptr, info_ptr, width, height, ++ bit_depth, color_type, interlace_type, ++ compression_type, filter_method) ++ ++ width - holds the width of the image ++ in pixels (up to 2^31). ++ ++ height - holds the height of the image ++ in pixels (up to 2^31). ++ ++ bit_depth - holds the bit depth of one of the ++ image channels. ++ (valid values are 1, 2, 4, 8, 16 ++ and depend also on the ++ color_type. See also significant ++ bits (sBIT) below). ++ ++ color_type - describes which color/alpha ++ channels are present. ++ PNG_COLOR_TYPE_GRAY ++ (bit depths 1, 2, 4, 8, 16) ++ PNG_COLOR_TYPE_GRAY_ALPHA ++ (bit depths 8, 16) ++ PNG_COLOR_TYPE_PALETTE ++ (bit depths 1, 2, 4, 8) ++ PNG_COLOR_TYPE_RGB ++ (bit_depths 8, 16) ++ PNG_COLOR_TYPE_RGB_ALPHA ++ (bit_depths 8, 16) ++ ++ PNG_COLOR_MASK_PALETTE ++ PNG_COLOR_MASK_COLOR ++ PNG_COLOR_MASK_ALPHA ++ ++ interlace_type - PNG_INTERLACE_NONE or ++ PNG_INTERLACE_ADAM7 ++ ++ compression_type - (must be ++ PNG_COMPRESSION_TYPE_DEFAULT) ++ ++ filter_method - (must be PNG_FILTER_TYPE_DEFAULT ++ or, if you are writing a PNG to ++ be embedded in a MNG datastream, ++ can also be ++ PNG_INTRAPIXEL_DIFFERENCING) ++ ++If you call png_set_IHDR(), the call must appear before any of the ++other png_set_*() functions, because they might require access to some of ++the IHDR settings. The remaining png_set_*() functions can be called ++in any order. ++ ++If you wish, you can reset the compression_type, interlace_type, or ++filter_method later by calling png_set_IHDR() again; if you do this, the ++width, height, bit_depth, and color_type must be the same in each call. ++ ++ png_set_PLTE(png_ptr, info_ptr, palette, ++ num_palette); ++ ++ palette - the palette for the file ++ (array of png_color) ++ num_palette - number of entries in the palette ++ ++ ++ png_set_gAMA(png_ptr, info_ptr, file_gamma); ++ png_set_gAMA_fixed(png_ptr, info_ptr, int_file_gamma); ++ ++ file_gamma - the gamma at which the image was ++ created (PNG_INFO_gAMA) ++ ++ int_file_gamma - 100,000 times the gamma at which ++ the image was created ++ ++ png_set_cHRM(png_ptr, info_ptr, white_x, white_y, red_x, red_y, ++ green_x, green_y, blue_x, blue_y) ++ png_set_cHRM_XYZ(png_ptr, info_ptr, red_X, red_Y, red_Z, green_X, ++ green_Y, green_Z, blue_X, blue_Y, blue_Z) ++ png_set_cHRM_fixed(png_ptr, info_ptr, int_white_x, int_white_y, ++ int_red_x, int_red_y, int_green_x, int_green_y, ++ int_blue_x, int_blue_y) ++ png_set_cHRM_XYZ_fixed(png_ptr, info_ptr, int_red_X, int_red_Y, ++ int_red_Z, int_green_X, int_green_Y, int_green_Z, ++ int_blue_X, int_blue_Y, int_blue_Z) ++ ++ {white,red,green,blue}_{x,y} ++ A color space encoding specified using the chromaticities ++ of the end points and the white point. ++ ++ {red,green,blue}_{X,Y,Z} ++ A color space encoding specified using the encoding end ++ points - the CIE tristimulus specification of the intended ++ color of the red, green and blue channels in the PNG RGB ++ data. The white point is simply the sum of the three end ++ points. ++ ++ png_set_sRGB(png_ptr, info_ptr, srgb_intent); ++ ++ srgb_intent - the rendering intent ++ (PNG_INFO_sRGB) The presence of ++ the sRGB chunk means that the pixel ++ data is in the sRGB color space. ++ This chunk also implies specific ++ values of gAMA and cHRM. Rendering ++ intent is the CSS-1 property that ++ has been defined by the International ++ Color Consortium ++ (http://www.color.org). ++ It can be one of ++ PNG_sRGB_INTENT_SATURATION, ++ PNG_sRGB_INTENT_PERCEPTUAL, ++ PNG_sRGB_INTENT_ABSOLUTE, or ++ PNG_sRGB_INTENT_RELATIVE. ++ ++ ++ png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr, ++ srgb_intent); ++ ++ srgb_intent - the rendering intent ++ (PNG_INFO_sRGB) The presence of the ++ sRGB chunk means that the pixel ++ data is in the sRGB color space. ++ This function also causes gAMA and ++ cHRM chunks with the specific values ++ that are consistent with sRGB to be ++ written. ++ ++ png_set_iCCP(png_ptr, info_ptr, name, compression_type, ++ profile, proflen); ++ ++ name - The profile name. ++ ++ compression_type - The compression type; always ++ PNG_COMPRESSION_TYPE_BASE for PNG 1.0. ++ You may give NULL to this argument to ++ ignore it. ++ ++ profile - International Color Consortium color ++ profile data. May contain NULs. ++ ++ proflen - length of profile data in bytes. ++ ++ png_set_sBIT(png_ptr, info_ptr, sig_bit); ++ ++ sig_bit - the number of significant bits for ++ (PNG_INFO_sBIT) each of the gray, red, ++ green, and blue channels, whichever are ++ appropriate for the given color type ++ (png_color_16) ++ ++ png_set_tRNS(png_ptr, info_ptr, trans_alpha, ++ num_trans, trans_color); ++ ++ trans_alpha - array of alpha (transparency) ++ entries for palette (PNG_INFO_tRNS) ++ ++ num_trans - number of transparent entries ++ (PNG_INFO_tRNS) ++ ++ trans_color - graylevel or color sample values ++ (in order red, green, blue) of the ++ single transparent color for ++ non-paletted images (PNG_INFO_tRNS) ++ ++ png_set_eXIf_1(png_ptr, info_ptr, num_exif, exif); ++ ++ exif - Exif profile (array of ++ png_byte) (PNG_INFO_eXIf) ++ ++ png_set_hIST(png_ptr, info_ptr, hist); ++ ++ hist - histogram of palette (array of ++ png_uint_16) (PNG_INFO_hIST) ++ ++ png_set_tIME(png_ptr, info_ptr, mod_time); ++ ++ mod_time - time image was last modified ++ (PNG_VALID_tIME) ++ ++ png_set_bKGD(png_ptr, info_ptr, background); ++ ++ background - background color (of type ++ png_color_16p) (PNG_VALID_bKGD) ++ ++ png_set_text(png_ptr, info_ptr, text_ptr, num_text); ++ ++ text_ptr - array of png_text holding image ++ comments ++ ++ text_ptr[i].compression - type of compression used ++ on "text" PNG_TEXT_COMPRESSION_NONE ++ PNG_TEXT_COMPRESSION_zTXt ++ PNG_ITXT_COMPRESSION_NONE ++ PNG_ITXT_COMPRESSION_zTXt ++ text_ptr[i].key - keyword for comment. Must contain ++ 1-79 characters. ++ text_ptr[i].text - text comments for current ++ keyword. Can be NULL or empty. ++ text_ptr[i].text_length - length of text string, ++ after decompression, 0 for iTXt ++ text_ptr[i].itxt_length - length of itxt string, ++ after decompression, 0 for tEXt/zTXt ++ text_ptr[i].lang - language of comment (NULL or ++ empty for unknown). ++ text_ptr[i].translated_keyword - keyword in UTF-8 (NULL ++ or empty for unknown). ++ ++ Note that the itxt_length, lang, and lang_key ++ members of the text_ptr structure only exist when the ++ library is built with iTXt chunk support. Prior to ++ libpng-1.4.0 the library was built by default without ++ iTXt support. Also note that when iTXt is supported, ++ they contain NULL pointers when the "compression" ++ field contains PNG_TEXT_COMPRESSION_NONE or ++ PNG_TEXT_COMPRESSION_zTXt. ++ ++ num_text - number of comments ++ ++ png_set_sPLT(png_ptr, info_ptr, &palette_ptr, ++ num_spalettes); ++ ++ palette_ptr - array of png_sPLT_struct structures ++ to be added to the list of palettes ++ in the info structure. ++ num_spalettes - number of palette structures to be ++ added. ++ ++ png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, ++ unit_type); ++ ++ offset_x - positive offset from the left ++ edge of the screen ++ ++ offset_y - positive offset from the top ++ edge of the screen ++ ++ unit_type - PNG_OFFSET_PIXEL, PNG_OFFSET_MICROMETER ++ ++ png_set_pHYs(png_ptr, info_ptr, res_x, res_y, ++ unit_type); ++ ++ res_x - pixels/unit physical resolution ++ in x direction ++ ++ res_y - pixels/unit physical resolution ++ in y direction ++ ++ unit_type - PNG_RESOLUTION_UNKNOWN, ++ PNG_RESOLUTION_METER ++ ++ png_set_sCAL(png_ptr, info_ptr, unit, width, height) ++ ++ unit - physical scale units (an integer) ++ ++ width - width of a pixel in physical scale units ++ ++ height - height of a pixel in physical scale units ++ (width and height are doubles) ++ ++ png_set_sCAL_s(png_ptr, info_ptr, unit, width, height) ++ ++ unit - physical scale units (an integer) ++ ++ width - width of a pixel in physical scale units ++ expressed as a string ++ ++ height - height of a pixel in physical scale units ++ (width and height are strings like "2.54") ++ ++ png_set_unknown_chunks(png_ptr, info_ptr, &unknowns, ++ num_unknowns) ++ ++ unknowns - array of png_unknown_chunk ++ structures holding unknown chunks ++ unknowns[i].name - name of unknown chunk ++ unknowns[i].data - data of unknown chunk ++ unknowns[i].size - size of unknown chunk's data ++ unknowns[i].location - position to write chunk in file ++ 0: do not write chunk ++ PNG_HAVE_IHDR: before PLTE ++ PNG_HAVE_PLTE: before IDAT ++ PNG_AFTER_IDAT: after IDAT ++ ++The "location" member is set automatically according to ++what part of the output file has already been written. ++You can change its value after calling png_set_unknown_chunks() ++as demonstrated in pngtest.c. Within each of the "locations", ++the chunks are sequenced according to their position in the ++structure (that is, the value of "i", which is the order in which ++the chunk was either read from the input file or defined with ++png_set_unknown_chunks). ++ ++A quick word about text and num_text. text is an array of png_text ++structures. num_text is the number of valid structures in the array. ++Each png_text structure holds a language code, a keyword, a text value, ++and a compression type. ++ ++The compression types have the same valid numbers as the compression ++types of the image data. Currently, the only valid number is zero. ++However, you can store text either compressed or uncompressed, unlike ++images, which always have to be compressed. So if you don't want the ++text compressed, set the compression type to PNG_TEXT_COMPRESSION_NONE. ++Because tEXt and zTXt chunks don't have a language field, if you ++specify PNG_TEXT_COMPRESSION_NONE or PNG_TEXT_COMPRESSION_zTXt ++any language code or translated keyword will not be written out. ++ ++Until text gets around a few hundred bytes, it is not worth compressing it. ++After the text has been written out to the file, the compression type ++is set to PNG_TEXT_COMPRESSION_NONE_WR or PNG_TEXT_COMPRESSION_zTXt_WR, ++so that it isn't written out again at the end (in case you are calling ++png_write_end() with the same struct). ++ ++The keywords that are given in the PNG Specification are: ++ ++ Title Short (one line) title or ++ caption for image ++ ++ Author Name of image's creator ++ ++ Description Description of image (possibly long) ++ ++ Copyright Copyright notice ++ ++ Creation Time Time of original image creation ++ (usually RFC 1123 format, see below) ++ ++ Software Software used to create the image ++ ++ Disclaimer Legal disclaimer ++ ++ Warning Warning of nature of content ++ ++ Source Device used to create the image ++ ++ Comment Miscellaneous comment; conversion ++ from other image format ++ ++The keyword-text pairs work like this. Keywords should be short ++simple descriptions of what the comment is about. Some typical ++keywords are found in the PNG specification, as is some recommendations ++on keywords. You can repeat keywords in a file. You can even write ++some text before the image and some after. For example, you may want ++to put a description of the image before the image, but leave the ++disclaimer until after, so viewers working over modem connections ++don't have to wait for the disclaimer to go over the modem before ++they start seeing the image. Finally, keywords should be full ++words, not abbreviations. Keywords and text are in the ISO 8859-1 ++(Latin-1) character set (a superset of regular ASCII) and can not ++contain NUL characters, and should not contain control or other ++unprintable characters. To make the comments widely readable, stick ++with basic ASCII, and avoid machine specific character set extensions ++like the IBM-PC character set. The keyword must be present, but ++you can leave off the text string on non-compressed pairs. ++Compressed pairs must have a text string, as only the text string ++is compressed anyway, so the compression would be meaningless. ++ ++PNG supports modification time via the png_time structure. Two ++conversion routines are provided, png_convert_from_time_t() for ++time_t and png_convert_from_struct_tm() for struct tm. The ++time_t routine uses gmtime(). You don't have to use either of ++these, but if you wish to fill in the png_time structure directly, ++you should provide the time in universal time (GMT) if possible ++instead of your local time. Note that the year number is the full ++year (e.g. 1998, rather than 98 - PNG is year 2000 compliant!), and ++that months start with 1. ++ ++If you want to store the time of the original image creation, you should ++use a plain tEXt chunk with the "Creation Time" keyword. This is ++necessary because the "creation time" of a PNG image is somewhat vague, ++depending on whether you mean the PNG file, the time the image was ++created in a non-PNG format, a still photo from which the image was ++scanned, or possibly the subject matter itself. In order to facilitate ++machine-readable dates, it is recommended that the "Creation Time" ++tEXt chunk use RFC 1123 format dates (e.g. "22 May 1997 18:07:10 GMT"), ++although this isn't a requirement. Unlike the tIME chunk, the ++"Creation Time" tEXt chunk is not expected to be automatically changed ++by the software. To facilitate the use of RFC 1123 dates, a function ++png_convert_to_rfc1123_buffer(buffer, png_timep) is provided to ++convert from PNG time to an RFC 1123 format string. The caller must provide ++a writeable buffer of at least 29 bytes. ++ ++.SS Writing unknown chunks ++ ++You can use the png_set_unknown_chunks function to queue up private chunks ++for writing. You give it a chunk name, location, raw data, and a size. You ++also must use png_set_keep_unknown_chunks() to ensure that libpng will ++handle them. That's all there is to it. The chunks will be written by the ++next following png_write_info_before_PLTE, png_write_info, or png_write_end ++function, depending upon the specified location. Any chunks previously ++read into the info structure's unknown-chunk list will also be written out ++in a sequence that satisfies the PNG specification's ordering rules. ++ ++Here is an example of writing two private chunks, prVt and miNE: ++ ++ #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED ++ /* Set unknown chunk data */ ++ png_unknown_chunk unk_chunk[2]; ++ strcpy((char *) unk_chunk[0].name, "prVt"; ++ unk_chunk[0].data = (unsigned char *) "PRIVATE DATA"; ++ unk_chunk[0].size = strlen(unk_chunk[0].data)+1; ++ unk_chunk[0].location = PNG_HAVE_IHDR; ++ strcpy((char *) unk_chunk[1].name, "miNE"; ++ unk_chunk[1].data = (unsigned char *) "MY CHUNK DATA"; ++ unk_chunk[1].size = strlen(unk_chunk[0].data)+1; ++ unk_chunk[1].location = PNG_AFTER_IDAT; ++ png_set_unknown_chunks(write_ptr, write_info_ptr, ++ unk_chunk, 2); ++ /* Needed because miNE is not safe-to-copy */ ++ png_set_keep_unknown_chunks(png, PNG_HANDLE_CHUNK_ALWAYS, ++ (png_bytep) "miNE", 1); ++ # if PNG_LIBPNG_VER < 10600 ++ /* Deal with unknown chunk location bug in 1.5.x and earlier */ ++ png_set_unknown_chunk_location(png, info, 0, PNG_HAVE_IHDR); ++ png_set_unknown_chunk_location(png, info, 1, PNG_AFTER_IDAT); ++ # endif ++ # if PNG_LIBPNG_VER < 10500 ++ /* PNG_AFTER_IDAT writes two copies of the chunk prior to libpng-1.5.0, ++ * one before IDAT and another after IDAT, so don't use it; only use ++ * PNG_HAVE_IHDR location. This call resets the location previously ++ * set by assignment and png_set_unknown_chunk_location() for chunk 1. ++ */ ++ png_set_unknown_chunk_location(png, info, 1, PNG_HAVE_IHDR); ++ # endif ++ #endif ++ ++.SS The high-level write interface ++ ++At this point there are two ways to proceed; through the high-level ++write interface, or through a sequence of low-level write operations. ++You can use the high-level interface if your image data is present ++in the info structure. All defined output ++transformations are permitted, enabled by the following masks. ++ ++ PNG_TRANSFORM_IDENTITY No transformation ++ PNG_TRANSFORM_PACKING Pack 1, 2 and 4-bit samples ++ PNG_TRANSFORM_PACKSWAP Change order of packed ++ pixels to LSB first ++ PNG_TRANSFORM_INVERT_MONO Invert monochrome images ++ PNG_TRANSFORM_SHIFT Normalize pixels to the ++ sBIT depth ++ PNG_TRANSFORM_BGR Flip RGB to BGR, RGBA ++ to BGRA ++ PNG_TRANSFORM_SWAP_ALPHA Flip RGBA to ARGB or GA ++ to AG ++ PNG_TRANSFORM_INVERT_ALPHA Change alpha from opacity ++ to transparency ++ PNG_TRANSFORM_SWAP_ENDIAN Byte-swap 16-bit samples ++ PNG_TRANSFORM_STRIP_FILLER Strip out filler ++ bytes (deprecated). ++ PNG_TRANSFORM_STRIP_FILLER_BEFORE Strip out leading ++ filler bytes ++ PNG_TRANSFORM_STRIP_FILLER_AFTER Strip out trailing ++ filler bytes ++ ++If you have valid image data in the info structure (you can use ++png_set_rows() to put image data in the info structure), simply do this: ++ ++ png_write_png(png_ptr, info_ptr, png_transforms, NULL) ++ ++where png_transforms is an integer containing the bitwise OR of some set of ++transformation flags. This call is equivalent to png_write_info(), ++followed the set of transformations indicated by the transform mask, ++then png_write_image(), and finally png_write_end(). ++ ++(The final parameter of this call is not yet used. Someday it might point ++to transformation parameters required by some future output transform.) ++ ++You must use png_transforms and not call any png_set_transform() functions ++when you use png_write_png(). ++ ++.SS The low-level write interface ++ ++If you are going the low-level route instead, you are now ready to ++write all the file information up to the actual image data. You do ++this with a call to png_write_info(). ++ ++ png_write_info(png_ptr, info_ptr); ++ ++Note that there is one transformation you may need to do before ++png_write_info(). In PNG files, the alpha channel in an image is the ++level of opacity. If your data is supplied as a level of transparency, ++you can invert the alpha channel before you write it, so that 0 is ++fully transparent and 255 (in 8-bit or paletted images) or 65535 ++(in 16-bit images) is fully opaque, with ++ ++ png_set_invert_alpha(png_ptr); ++ ++This must appear before png_write_info() instead of later with the ++other transformations because in the case of paletted images the tRNS ++chunk data has to be inverted before the tRNS chunk is written. If ++your image is not a paletted image, the tRNS data (which in such cases ++represents a single color to be rendered as transparent) won't need to ++be changed, and you can safely do this transformation after your ++png_write_info() call. ++ ++If you need to write a private chunk that you want to appear before ++the PLTE chunk when PLTE is present, you can write the PNG info in ++two steps, and insert code to write your own chunk between them: ++ ++ png_write_info_before_PLTE(png_ptr, info_ptr); ++ png_set_unknown_chunks(png_ptr, info_ptr, ...); ++ png_write_info(png_ptr, info_ptr); ++ ++After you've written the file information, you can set up the library ++to handle any special transformations of the image data. The various ++ways to transform the data will be described in the order that they ++should occur. This is important, as some of these change the color ++type and/or bit depth of the data, and some others only work on ++certain color types and bit depths. Even though each transformation ++checks to see if it has data that it can do something with, you should ++make sure to only enable a transformation if it will be valid for the ++data. For example, don't swap red and blue on grayscale data. ++ ++PNG files store RGB pixels packed into 3 or 6 bytes. This code tells ++the library to strip input data that has 4 or 8 bytes per pixel down ++to 3 or 6 bytes (or strip 2 or 4-byte grayscale+filler data to 1 or 2 ++bytes per pixel). ++ ++ png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE); ++ ++where the 0 is unused, and the location is either PNG_FILLER_BEFORE or ++PNG_FILLER_AFTER, depending upon whether the filler byte in the pixel ++is stored XRGB or RGBX. ++ ++PNG files pack pixels of bit depths 1, 2, and 4 into bytes as small as ++they can, resulting in, for example, 8 pixels per byte for 1 bit files. ++If the data is supplied at 1 pixel per byte, use this code, which will ++correctly pack the pixels into a single byte: ++ ++ png_set_packing(png_ptr); ++ ++PNG files reduce possible bit depths to 1, 2, 4, 8, and 16. If your ++data is of another bit depth, you can write an sBIT chunk into the ++file so that decoders can recover the original data if desired. ++ ++ /* Set the true bit depth of the image data */ ++ if (color_type & PNG_COLOR_MASK_COLOR) ++ { ++ sig_bit.red = true_bit_depth; ++ sig_bit.green = true_bit_depth; ++ sig_bit.blue = true_bit_depth; ++ } ++ ++ else ++ { ++ sig_bit.gray = true_bit_depth; ++ } ++ ++ if (color_type & PNG_COLOR_MASK_ALPHA) ++ { ++ sig_bit.alpha = true_bit_depth; ++ } ++ ++ png_set_sBIT(png_ptr, info_ptr, &sig_bit); ++ ++If the data is stored in the row buffer in a bit depth other than ++one supported by PNG (e.g. 3 bit data in the range 0-7 for a 4-bit PNG), ++this will scale the values to appear to be the correct bit depth as ++is required by PNG. ++ ++ png_set_shift(png_ptr, &sig_bit); ++ ++PNG files store 16-bit pixels in network byte order (big-endian, ++ie. most significant bits first). This code would be used if they are ++supplied the other way (little-endian, i.e. least significant bits ++first, the way PCs store them): ++ ++ if (bit_depth > 8) ++ png_set_swap(png_ptr); ++ ++If you are using packed-pixel images (1, 2, or 4 bits/pixel), and you ++need to change the order the pixels are packed into bytes, you can use: ++ ++ if (bit_depth < 8) ++ png_set_packswap(png_ptr); ++ ++PNG files store 3 color pixels in red, green, blue order. This code ++would be used if they are supplied as blue, green, red: ++ ++ png_set_bgr(png_ptr); ++ ++PNG files describe monochrome as black being zero and white being ++one. This code would be used if the pixels are supplied with this reversed ++(black being one and white being zero): ++ ++ png_set_invert_mono(png_ptr); ++ ++Finally, you can write your own transformation function if none of ++the existing ones meets your needs. This is done by setting a callback ++with ++ ++ png_set_write_user_transform_fn(png_ptr, ++ write_transform_fn); ++ ++You must supply the function ++ ++ void write_transform_fn(png_structp png_ptr, png_row_infop ++ row_info, png_bytep data) ++ ++See pngtest.c for a working example. Your function will be called ++before any of the other transformations are processed. If supported ++libpng also supplies an information routine that may be called from ++your callback: ++ ++ png_get_current_row_number(png_ptr); ++ png_get_current_pass_number(png_ptr); ++ ++This returns the current row passed to the transform. With interlaced ++images the value returned is the row in the input sub-image image. Use ++PNG_ROW_FROM_PASS_ROW(row, pass) and PNG_COL_FROM_PASS_COL(col, pass) to ++find the output pixel (x,y) given an interlaced sub-image pixel (row,col,pass). ++ ++The discussion of interlace handling above contains more information on how to ++use these values. ++ ++You can also set up a pointer to a user structure for use by your ++callback function. ++ ++ png_set_user_transform_info(png_ptr, user_ptr, 0, 0); ++ ++The user_channels and user_depth parameters of this function are ignored ++when writing; you can set them to zero as shown. ++ ++You can retrieve the pointer via the function png_get_user_transform_ptr(). ++For example: ++ ++ voidp write_user_transform_ptr = ++ png_get_user_transform_ptr(png_ptr); ++ ++It is possible to have libpng flush any pending output, either manually, ++or automatically after a certain number of lines have been written. To ++flush the output stream a single time call: ++ ++ png_write_flush(png_ptr); ++ ++and to have libpng flush the output stream periodically after a certain ++number of scanlines have been written, call: ++ ++ png_set_flush(png_ptr, nrows); ++ ++Note that the distance between rows is from the last time png_write_flush() ++was called, or the first row of the image if it has never been called. ++So if you write 50 lines, and then png_set_flush 25, it will flush the ++output on the next scanline, and every 25 lines thereafter, unless ++png_write_flush() is called before 25 more lines have been written. ++If nrows is too small (less than about 10 lines for a 640 pixel wide ++RGB image) the image compression may decrease noticeably (although this ++may be acceptable for real-time applications). Infrequent flushing will ++only degrade the compression performance by a few percent over images ++that do not use flushing. ++ ++.SS Writing the image data ++ ++That's it for the transformations. Now you can write the image data. ++The simplest way to do this is in one function call. If you have the ++whole image in memory, you can just call png_write_image() and libpng ++will write the image. You will need to pass in an array of pointers to ++each row. This function automatically handles interlacing, so you don't ++need to call png_set_interlace_handling() or call this function multiple ++times, or any of that other stuff necessary with png_write_rows(). ++ ++ png_write_image(png_ptr, row_pointers); ++ ++where row_pointers is: ++ ++ png_byte *row_pointers[height]; ++ ++You can point to void or char or whatever you use for pixels. ++ ++If you don't want to write the whole image at once, you can ++use png_write_rows() instead. If the file is not interlaced, ++this is simple: ++ ++ png_write_rows(png_ptr, row_pointers, ++ number_of_rows); ++ ++row_pointers is the same as in the png_write_image() call. ++ ++If you are just writing one row at a time, you can do this with ++a single row_pointer instead of an array of row_pointers: ++ ++ png_bytep row_pointer = row; ++ ++ png_write_row(png_ptr, row_pointer); ++ ++When the file is interlaced, things can get a good deal more complicated. ++The only currently (as of the PNG Specification version 1.2, dated July ++1999) defined interlacing scheme for PNG files is the "Adam7" interlace ++scheme, that breaks down an image into seven smaller images of varying ++size. libpng will build these images for you, or you can do them ++yourself. If you want to build them yourself, see the PNG specification ++for details of which pixels to write when. ++ ++If you don't want libpng to handle the interlacing details, just ++use png_set_interlace_handling() and call png_write_rows() the ++correct number of times to write all the sub-images ++(png_set_interlace_handling() returns the number of sub-images.) ++ ++If you want libpng to build the sub-images, call this before you start ++writing any rows: ++ ++ number_of_passes = png_set_interlace_handling(png_ptr); ++ ++This will return the number of passes needed. Currently, this is seven, ++but may change if another interlace type is added. ++ ++Then write the complete image number_of_passes times. ++ ++ png_write_rows(png_ptr, row_pointers, number_of_rows); ++ ++Think carefully before you write an interlaced image. Typically code that ++reads such images reads all the image data into memory, uncompressed, before ++doing any processing. Only code that can display an image on the fly can ++take advantage of the interlacing and even then the image has to be exactly ++the correct size for the output device, because scaling an image requires ++adjacent pixels and these are not available until all the passes have been ++read. ++ ++If you do write an interlaced image you will hardly ever need to handle ++the interlacing yourself. Call png_set_interlace_handling() and use the ++approach described above. ++ ++The only time it is conceivable that you will really need to write an ++interlaced image pass-by-pass is when you have read one pass by pass and ++made some pixel-by-pixel transformation to it, as described in the read ++code above. In this case use the PNG_PASS_ROWS and PNG_PASS_COLS macros ++to determine the size of each sub-image in turn and simply write the rows ++you obtained from the read code. ++ ++.SS Finishing a sequential write ++ ++After you are finished writing the image, you should finish writing ++the file. If you are interested in writing comments or time, you should ++pass an appropriately filled png_info pointer. If you are not interested, ++you can pass NULL. ++ ++ png_write_end(png_ptr, info_ptr); ++ ++When you are done, you can free all memory used by libpng like this: ++ ++ png_destroy_write_struct(&png_ptr, &info_ptr); ++ ++It is also possible to individually free the info_ptr members that ++point to libpng-allocated storage with the following function: ++ ++ png_free_data(png_ptr, info_ptr, mask, seq) ++ ++ mask - identifies data to be freed, a mask ++ containing the bitwise OR of one or ++ more of ++ PNG_FREE_PLTE, PNG_FREE_TRNS, ++ PNG_FREE_HIST, PNG_FREE_ICCP, ++ PNG_FREE_PCAL, PNG_FREE_ROWS, ++ PNG_FREE_SCAL, PNG_FREE_SPLT, ++ PNG_FREE_TEXT, PNG_FREE_UNKN, ++ or simply PNG_FREE_ALL ++ ++ seq - sequence number of item to be freed ++ (\-1 for all items) ++ ++This function may be safely called when the relevant storage has ++already been freed, or has not yet been allocated, or was allocated ++by the user and not by libpng, and will in those cases do nothing. ++The "seq" parameter is ignored if only one item of the selected data ++type, such as PLTE, is allowed. If "seq" is not \-1, and multiple items ++are allowed for the data type identified in the mask, such as text or ++sPLT, only the n'th item in the structure is freed, where n is "seq". ++ ++If you allocated data such as a palette that you passed in to libpng ++with png_set_*, you must not free it until just before the call to ++png_destroy_write_struct(). ++ ++The default behavior is only to free data that was allocated internally ++by libpng. This can be changed, so that libpng will not free the data, ++or so that it will free data that was allocated by the user with png_malloc() ++or png_calloc() and passed in via a png_set_*() function, with ++ ++ png_data_freer(png_ptr, info_ptr, freer, mask) ++ ++ freer - one of ++ PNG_DESTROY_WILL_FREE_DATA ++ PNG_SET_WILL_FREE_DATA ++ PNG_USER_WILL_FREE_DATA ++ ++ mask - which data elements are affected ++ same choices as in png_free_data() ++ ++For example, to transfer responsibility for some data from a read structure ++to a write structure, you could use ++ ++ png_data_freer(read_ptr, read_info_ptr, ++ PNG_USER_WILL_FREE_DATA, ++ PNG_FREE_PLTE|PNG_FREE_tRNS|PNG_FREE_hIST) ++ ++ png_data_freer(write_ptr, write_info_ptr, ++ PNG_DESTROY_WILL_FREE_DATA, ++ PNG_FREE_PLTE|PNG_FREE_tRNS|PNG_FREE_hIST) ++ ++thereby briefly reassigning responsibility for freeing to the user but ++immediately afterwards reassigning it once more to the write_destroy ++function. Having done this, it would then be safe to destroy the read ++structure and continue to use the PLTE, tRNS, and hIST data in the write ++structure. ++ ++This function only affects data that has already been allocated. ++You can call this function before calling after the png_set_*() functions ++to control whether the user or png_destroy_*() is supposed to free the data. ++When the user assumes responsibility for libpng-allocated data, the ++application must use ++png_free() to free it, and when the user transfers responsibility to libpng ++for data that the user has allocated, the user must have used png_malloc() ++or png_calloc() to allocate it. ++ ++If you allocated text_ptr.text, text_ptr.lang, and text_ptr.translated_keyword ++separately, do not transfer responsibility for freeing text_ptr to libpng, ++because when libpng fills a png_text structure it combines these members with ++the key member, and png_free_data() will free only text_ptr.key. Similarly, ++if you transfer responsibility for free'ing text_ptr from libpng to your ++application, your application must not separately free those members. ++For a more compact example of writing a PNG image, see the file example.c. ++ ++.SH V. Simplified API ++ ++The simplified API, which became available in libpng-1.6.0, hides the details ++of both libpng and the PNG file format itself. ++It allows PNG files to be read into a very limited number of ++in-memory bitmap formats or to be written from the same formats. If these ++formats do not accommodate your needs then you can, and should, use the more ++sophisticated APIs above - these support a wide variety of in-memory formats ++and a wide variety of sophisticated transformations to those formats as well ++as a wide variety of APIs to manipulate ancillary information. ++ ++To read a PNG file using the simplified API: ++ ++ 1) Declare a 'png_image' structure (see below) on the stack, set the ++ version field to PNG_IMAGE_VERSION and the 'opaque' pointer to NULL ++ (this is REQUIRED, your program may crash if you don't do it.) ++ ++ 2) Call the appropriate png_image_begin_read... function. ++ ++ 3) Set the png_image 'format' member to the required sample format. ++ ++ 4) Allocate a buffer for the image and, if required, the color-map. ++ ++ 5) Call png_image_finish_read to read the image and, if required, the ++ color-map into your buffers. ++ ++There are no restrictions on the format of the PNG input itself; all valid ++color types, bit depths, and interlace methods are acceptable, and the ++input image is transformed as necessary to the requested in-memory format ++during the png_image_finish_read() step. The only caveat is that if you ++request a color-mapped image from a PNG that is full-color or makes ++complex use of an alpha channel the transformation is extremely lossy and the ++result may look terrible. ++ ++To write a PNG file using the simplified API: ++ ++ 1) Declare a 'png_image' structure on the stack and memset() ++ it to all zero. ++ ++ 2) Initialize the members of the structure that describe the ++ image, setting the 'format' member to the format of the ++ image samples. ++ ++ 3) Call the appropriate png_image_write... function with a ++ pointer to the image and, if necessary, the color-map to write ++ the PNG data. ++ ++png_image is a structure that describes the in-memory format of an image ++when it is being read or defines the in-memory format of an image that you ++need to write. The "png_image" structure contains the following members: ++ ++ png_controlp opaque Initialize to NULL, free with png_image_free ++ png_uint_32 version Set to PNG_IMAGE_VERSION ++ png_uint_32 width Image width in pixels (columns) ++ png_uint_32 height Image height in pixels (rows) ++ png_uint_32 format Image format as defined below ++ png_uint_32 flags A bit mask containing informational flags ++ png_uint_32 colormap_entries; Number of entries in the color-map ++ png_uint_32 warning_or_error; ++ char message[64]; ++ ++In the event of an error or warning the "warning_or_error" ++field will be set to a non-zero value and the 'message' field will contain ++a '\0' terminated string with the libpng error or warning message. If both ++warnings and an error were encountered, only the error is recorded. If there ++are multiple warnings, only the first one is recorded. ++ ++The upper 30 bits of the "warning_or_error" value are reserved; the low two ++bits contain a two bit code such that a value more than 1 indicates a failure ++in the API just called: ++ ++ 0 - no warning or error ++ 1 - warning ++ 2 - error ++ 3 - error preceded by warning ++ ++The pixels (samples) of the image have one to four channels whose components ++have original values in the range 0 to 1.0: ++ ++ 1: A single gray or luminance channel (G). ++ 2: A gray/luminance channel and an alpha channel (GA). ++ 3: Three red, green, blue color channels (RGB). ++ 4: Three color channels and an alpha channel (RGBA). ++ ++The channels are encoded in one of two ways: ++ ++ a) As a small integer, value 0..255, contained in a single byte. For the ++alpha channel the original value is simply value/255. For the color or ++luminance channels the value is encoded according to the sRGB specification ++and matches the 8-bit format expected by typical display devices. ++ ++The color/gray channels are not scaled (pre-multiplied) by the alpha ++channel and are suitable for passing to color management software. ++ ++ b) As a value in the range 0..65535, contained in a 2-byte integer, in ++the native byte order of the platform on which the application is running. ++All channels can be converted to the original value by dividing by 65535; all ++channels are linear. Color channels use the RGB encoding (RGB end-points) of ++the sRGB specification. This encoding is identified by the ++PNG_FORMAT_FLAG_LINEAR flag below. ++ ++When the simplified API needs to convert between sRGB and linear colorspaces, ++the actual sRGB transfer curve defined in the sRGB specification (see the ++article at https://en.wikipedia.org/wiki/SRGB) is used, not the gamma=1/2.2 ++approximation used elsewhere in libpng. ++ ++When an alpha channel is present it is expected to denote pixel coverage ++of the color or luminance channels and is returned as an associated alpha ++channel: the color/gray channels are scaled (pre-multiplied) by the alpha ++value. ++ ++The samples are either contained directly in the image data, between 1 and 8 ++bytes per pixel according to the encoding, or are held in a color-map indexed ++by bytes in the image data. In the case of a color-map the color-map entries ++are individual samples, encoded as above, and the image data has one byte per ++pixel to select the relevant sample from the color-map. ++ ++PNG_FORMAT_* ++ ++The #defines to be used in png_image::format. Each #define identifies a ++particular layout of channel data and, if present, alpha values. There are ++separate defines for each of the two component encodings. ++ ++A format is built up using single bit flag values. All combinations are ++valid. Formats can be built up from the flag values or you can use one of ++the predefined values below. When testing formats always use the FORMAT_FLAG ++macros to test for individual features - future versions of the library may ++add new flags. ++ ++When reading or writing color-mapped images the format should be set to the ++format of the entries in the color-map then png_image_{read,write}_colormap ++called to read or write the color-map and set the format correctly for the ++image data. Do not set the PNG_FORMAT_FLAG_COLORMAP bit directly! ++ ++NOTE: libpng can be built with particular features disabled. If you see ++compiler errors because the definition of one of the following flags has been ++compiled out it is because libpng does not have the required support. It is ++possible, however, for the libpng configuration to enable the format on just ++read or just write; in that case you may see an error at run time. ++You can guard against this by checking for the definition of the ++appropriate "_SUPPORTED" macro, one of: ++ ++ PNG_SIMPLIFIED_{READ,WRITE}_{BGR,AFIRST}_SUPPORTED ++ ++ PNG_FORMAT_FLAG_ALPHA format with an alpha channel ++ PNG_FORMAT_FLAG_COLOR color format: otherwise grayscale ++ PNG_FORMAT_FLAG_LINEAR 2-byte channels else 1-byte ++ PNG_FORMAT_FLAG_COLORMAP image data is color-mapped ++ PNG_FORMAT_FLAG_BGR BGR colors, else order is RGB ++ PNG_FORMAT_FLAG_AFIRST alpha channel comes first ++ ++Supported formats are as follows. Future versions of libpng may support more ++formats; for compatibility with older versions simply check if the format ++macro is defined using #ifdef. These defines describe the in-memory layout ++of the components of the pixels of the image. ++ ++First the single byte (sRGB) formats: ++ ++ PNG_FORMAT_GRAY ++ PNG_FORMAT_GA ++ PNG_FORMAT_AG ++ PNG_FORMAT_RGB ++ PNG_FORMAT_BGR ++ PNG_FORMAT_RGBA ++ PNG_FORMAT_ARGB ++ PNG_FORMAT_BGRA ++ PNG_FORMAT_ABGR ++ ++Then the linear 2-byte formats. When naming these "Y" is used to ++indicate a luminance (gray) channel. The component order within the pixel ++is always the same - there is no provision for swapping the order of the ++components in the linear format. The components are 16-bit integers in ++the native byte order for your platform, and there is no provision for ++swapping the bytes to a different endian condition. ++ ++ PNG_FORMAT_LINEAR_Y ++ PNG_FORMAT_LINEAR_Y_ALPHA ++ PNG_FORMAT_LINEAR_RGB ++ PNG_FORMAT_LINEAR_RGB_ALPHA ++ ++With color-mapped formats the image data is one byte for each pixel. The byte ++is an index into the color-map which is formatted as above. To obtain a ++color-mapped format it is sufficient just to add the PNG_FOMAT_FLAG_COLORMAP ++to one of the above definitions, or you can use one of the definitions below. ++ ++ PNG_FORMAT_RGB_COLORMAP ++ PNG_FORMAT_BGR_COLORMAP ++ PNG_FORMAT_RGBA_COLORMAP ++ PNG_FORMAT_ARGB_COLORMAP ++ PNG_FORMAT_BGRA_COLORMAP ++ PNG_FORMAT_ABGR_COLORMAP ++ ++PNG_IMAGE macros ++ ++These are convenience macros to derive information from a png_image ++structure. The PNG_IMAGE_SAMPLE_ macros return values appropriate to the ++actual image sample values - either the entries in the color-map or the ++pixels in the image. The PNG_IMAGE_PIXEL_ macros return corresponding values ++for the pixels and will always return 1 for color-mapped formats. The ++remaining macros return information about the rows in the image and the ++complete image. ++ ++NOTE: All the macros that take a png_image::format parameter are compile time ++constants if the format parameter is, itself, a constant. Therefore these ++macros can be used in array declarations and case labels where required. ++Similarly the macros are also pre-processor constants (sizeof is not used) so ++they can be used in #if tests. ++ ++ PNG_IMAGE_SAMPLE_CHANNELS(fmt) ++ Returns the total number of channels in a given format: 1..4 ++ ++ PNG_IMAGE_SAMPLE_COMPONENT_SIZE(fmt) ++ Returns the size in bytes of a single component of a pixel or color-map ++ entry (as appropriate) in the image: 1 or 2. ++ ++ PNG_IMAGE_SAMPLE_SIZE(fmt) ++ This is the size of the sample data for one sample. If the image is ++ color-mapped it is the size of one color-map entry (and image pixels are ++ one byte in size), otherwise it is the size of one image pixel. ++ ++ PNG_IMAGE_MAXIMUM_COLORMAP_COMPONENTS(fmt) ++ The maximum size of the color-map required by the format expressed in a ++ count of components. This can be used to compile-time allocate a ++ color-map: ++ ++ png_uint_16 colormap[PNG_IMAGE_MAXIMUM_COLORMAP_COMPONENTS(linear_fmt)]; ++ ++ png_byte colormap[PNG_IMAGE_MAXIMUM_COLORMAP_COMPONENTS(sRGB_fmt)]; ++ ++ Alternatively use the PNG_IMAGE_COLORMAP_SIZE macro below to use the ++ information from one of the png_image_begin_read_ APIs and dynamically ++ allocate the required memory. ++ ++ PNG_IMAGE_COLORMAP_SIZE(fmt) ++ The size of the color-map required by the format; this is the size of the ++ color-map buffer passed to the png_image_{read,write}_colormap APIs. It is ++ a fixed number determined by the format so can easily be allocated on the ++ stack if necessary. ++ ++Corresponding information about the pixels ++ ++ PNG_IMAGE_PIXEL_CHANNELS(fmt) ++ The number of separate channels (components) in a pixel; 1 for a ++ color-mapped image. ++ ++ PNG_IMAGE_PIXEL_COMPONENT_SIZE(fmt)\ ++ The size, in bytes, of each component in a pixel; 1 for a color-mapped ++ image. ++ ++ PNG_IMAGE_PIXEL_SIZE(fmt) ++ The size, in bytes, of a complete pixel; 1 for a color-mapped image. ++ ++Information about the whole row, or whole image ++ ++ PNG_IMAGE_ROW_STRIDE(image) ++ Returns the total number of components in a single row of the image; this ++ is the minimum 'row stride', the minimum count of components between each ++ row. For a color-mapped image this is the minimum number of bytes in a ++ row. ++ ++ If you need the stride measured in bytes, row_stride_bytes is ++ PNG_IMAGE_ROW_STRIDE(image) * PNG_IMAGE_PIXEL_COMPONENT_SIZE(fmt) ++ plus any padding bytes that your application might need, for example ++ to start the next row on a 4-byte boundary. ++ ++ PNG_IMAGE_BUFFER_SIZE(image, row_stride) ++ Return the size, in bytes, of an image buffer given a png_image and a row ++ stride - the number of components to leave space for in each row. ++ ++ PNG_IMAGE_SIZE(image) ++ Return the size, in bytes, of the image in memory given just a png_image; ++ the row stride is the minimum stride required for the image. ++ ++ PNG_IMAGE_COLORMAP_SIZE(image) ++ Return the size, in bytes, of the color-map of this image. If the image ++ format is not a color-map format this will return a size sufficient for ++ 256 entries in the given format; check PNG_FORMAT_FLAG_COLORMAP if ++ you don't want to allocate a color-map in this case. ++ ++PNG_IMAGE_FLAG_* ++ ++Flags containing additional information about the image are held in ++the 'flags' field of png_image. ++ ++ PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB == 0x01 ++ This indicates that the RGB values of the in-memory bitmap do not ++ correspond to the red, green and blue end-points defined by sRGB. ++ ++ PNG_IMAGE_FLAG_FAST == 0x02 ++ On write emphasise speed over compression; the resultant PNG file will be ++ larger but will be produced significantly faster, particular for large ++ images. Do not use this option for images which will be distributed, only ++ used it when producing intermediate files that will be read back in ++ repeatedly. For a typical 24-bit image the option will double the read ++ speed at the cost of increasing the image size by 25%, however for many ++ more compressible images the PNG file can be 10 times larger with only a ++ slight speed gain. ++ ++ PNG_IMAGE_FLAG_16BIT_sRGB == 0x04 ++ On read if the image is a 16-bit per component image and there is no gAMA ++ or sRGB chunk assume that the components are sRGB encoded. Notice that ++ images output by the simplified API always have gamma information; setting ++ this flag only affects the interpretation of 16-bit images from an ++ external source. It is recommended that the application expose this flag ++ to the user; the user can normally easily recognize the difference between ++ linear and sRGB encoding. This flag has no effect on write - the data ++ passed to the write APIs must have the correct encoding (as defined ++ above.) ++ ++ If the flag is not set (the default) input 16-bit per component data is ++ assumed to be linear. ++ ++ NOTE: the flag can only be set after the png_image_begin_read_ call, ++ because that call initializes the 'flags' field. ++ ++READ APIs ++ ++ The png_image passed to the read APIs must have been initialized by setting ++ the png_controlp field 'opaque' to NULL (or, better, memset the whole thing.) ++ ++ int png_image_begin_read_from_file( png_imagep image, ++ const char *file_name) ++ ++ The named file is opened for read and the image header ++ is filled in from the PNG header in the file. ++ ++ int png_image_begin_read_from_stdio (png_imagep image, ++ FILE* file) ++ ++ The PNG header is read from the stdio FILE object. ++ ++ int png_image_begin_read_from_memory(png_imagep image, ++ png_const_voidp memory, size_t size) ++ ++ The PNG header is read from the given memory buffer. ++ ++ int png_image_finish_read(png_imagep image, ++ png_colorp background, void *buffer, ++ png_int_32 row_stride, void *colormap)); ++ ++ Finish reading the image into the supplied buffer and ++ clean up the png_image structure. ++ ++ row_stride is the step, in png_byte or png_uint_16 units ++ as appropriate, between adjacent rows. A positive stride ++ indicates that the top-most row is first in the buffer - ++ the normal top-down arrangement. A negative stride ++ indicates that the bottom-most row is first in the buffer. ++ ++ background need only be supplied if an alpha channel must ++ be removed from a png_byte format and the removal is to be ++ done by compositing on a solid color; otherwise it may be ++ NULL and any composition will be done directly onto the ++ buffer. The value is an sRGB color to use for the ++ background, for grayscale output the green channel is used. ++ ++ For linear output removing the alpha channel is always done ++ by compositing on black. ++ ++ void png_image_free(png_imagep image) ++ ++ Free any data allocated by libpng in image->opaque, ++ setting the pointer to NULL. May be called at any time ++ after the structure is initialized. ++ ++When the simplified API needs to convert between sRGB and linear colorspaces, ++the actual sRGB transfer curve defined in the sRGB specification (see the ++article at https://en.wikipedia.org/wiki/SRGB) is used, not the gamma=1/2.2 ++approximation used elsewhere in libpng. ++ ++WRITE APIS ++ ++For write you must initialize a png_image structure to describe the image to ++be written: ++ ++ version: must be set to PNG_IMAGE_VERSION ++ opaque: must be initialized to NULL ++ width: image width in pixels ++ height: image height in rows ++ format: the format of the data you wish to write ++ flags: set to 0 unless one of the defined flags applies; set ++ PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB for color format images ++ where the RGB values do not correspond to the colors in sRGB. ++ colormap_entries: set to the number of entries in the color-map (0 to 256) ++ ++ int png_image_write_to_file, (png_imagep image, ++ const char *file, int convert_to_8bit, const void *buffer, ++ png_int_32 row_stride, const void *colormap)); ++ ++ Write the image to the named file. ++ ++ int png_image_write_to_memory (png_imagep image, void *memory, ++ png_alloc_size_t * PNG_RESTRICT memory_bytes, ++ int convert_to_8_bit, const void *buffer, ptrdiff_t row_stride, ++ const void *colormap)); ++ ++ Write the image to memory. ++ ++ int png_image_write_to_stdio(png_imagep image, FILE *file, ++ int convert_to_8_bit, const void *buffer, ++ png_int_32 row_stride, const void *colormap) ++ ++ Write the image to the given (FILE*). ++ ++With all write APIs if image is in one of the linear formats with ++(png_uint_16) data then setting convert_to_8_bit will cause the output to be ++a (png_byte) PNG gamma encoded according to the sRGB specification, otherwise ++a 16-bit linear encoded PNG file is written. ++ ++With all APIs row_stride is handled as in the read APIs - it is the spacing ++from one row to the next in component sized units (float) and if negative ++indicates a bottom-up row layout in the buffer. If you pass zero, libpng will ++calculate the row_stride for you from the width and number of channels. ++ ++Note that the write API does not support interlacing, sub-8-bit pixels, ++indexed (paletted) images, or most ancillary chunks. ++ ++.SH VI. Modifying/Customizing libpng ++ ++There are two issues here. The first is changing how libpng does ++standard things like memory allocation, input/output, and error handling. ++The second deals with more complicated things like adding new chunks, ++adding new transformations, and generally changing how libpng works. ++Both of those are compile-time issues; that is, they are generally ++determined at the time the code is written, and there is rarely a need ++to provide the user with a means of changing them. ++ ++Memory allocation, input/output, and error handling ++ ++All of the memory allocation, input/output, and error handling in libpng ++goes through callbacks that are user-settable. The default routines are ++in pngmem.c, pngrio.c, pngwio.c, and pngerror.c, respectively. To change ++these functions, call the appropriate png_set_*_fn() function. ++ ++Memory allocation is done through the functions png_malloc(), png_calloc(), ++and png_free(). The png_malloc() and png_free() functions currently just ++call the standard C functions and png_calloc() calls png_malloc() and then ++clears the newly allocated memory to zero; note that png_calloc(png_ptr, size) ++is not the same as the calloc(number, size) function provided by stdlib.h. ++There is limited support for certain systems with segmented memory ++architectures and the types of pointers declared by png.h match this; you ++will have to use appropriate pointers in your application. If you prefer ++to use a different method of allocating and freeing data, you can use ++png_create_read_struct_2() or png_create_write_struct_2() to register your ++own functions as described above. These functions also provide a void ++pointer that can be retrieved via ++ ++ mem_ptr=png_get_mem_ptr(png_ptr); ++ ++Your replacement memory functions must have prototypes as follows: ++ ++ png_voidp malloc_fn(png_structp png_ptr, ++ png_alloc_size_t size); ++ ++ void free_fn(png_structp png_ptr, png_voidp ptr); ++ ++Your malloc_fn() must return NULL in case of failure. The png_malloc() ++function will normally call png_error() if it receives a NULL from the ++system memory allocator or from your replacement malloc_fn(). ++ ++Your free_fn() will never be called with a NULL ptr, since libpng's ++png_free() checks for NULL before calling free_fn(). ++ ++Input/Output in libpng is done through png_read() and png_write(), ++which currently just call fread() and fwrite(). The FILE * is stored in ++png_struct and is initialized via png_init_io(). If you wish to change ++the method of I/O, the library supplies callbacks that you can set ++through the function png_set_read_fn() and png_set_write_fn() at run ++time, instead of calling the png_init_io() function. These functions ++also provide a void pointer that can be retrieved via the function ++png_get_io_ptr(). For example: ++ ++ png_set_read_fn(png_structp read_ptr, ++ voidp read_io_ptr, png_rw_ptr read_data_fn) ++ ++ png_set_write_fn(png_structp write_ptr, ++ voidp write_io_ptr, png_rw_ptr write_data_fn, ++ png_flush_ptr output_flush_fn); ++ ++ voidp read_io_ptr = png_get_io_ptr(read_ptr); ++ voidp write_io_ptr = png_get_io_ptr(write_ptr); ++ ++The replacement I/O functions must have prototypes as follows: ++ ++ void user_read_data(png_structp png_ptr, ++ png_bytep data, size_t length); ++ ++ void user_write_data(png_structp png_ptr, ++ png_bytep data, size_t length); ++ ++ void user_flush_data(png_structp png_ptr); ++ ++The user_read_data() function is responsible for detecting and ++handling end-of-data errors. ++ ++Supplying NULL for the read, write, or flush functions sets them back ++to using the default C stream functions, which expect the io_ptr to ++point to a standard *FILE structure. It is probably a mistake ++to use NULL for one of write_data_fn and output_flush_fn but not both ++of them, unless you have built libpng with PNG_NO_WRITE_FLUSH defined. ++It is an error to read from a write stream, and vice versa. ++ ++Error handling in libpng is done through png_error() and png_warning(). ++Errors handled through png_error() are fatal, meaning that png_error() ++should never return to its caller. Currently, this is handled via ++setjmp() and longjmp() (unless you have compiled libpng with ++PNG_NO_SETJMP, in which case it is handled via PNG_ABORT()), ++but you could change this to do things like exit() if you should wish, ++as long as your function does not return. ++ ++On non-fatal errors, png_warning() is called ++to print a warning message, and then control returns to the calling code. ++By default png_error() and png_warning() print a message on stderr via ++fprintf() unless the library is compiled with PNG_NO_CONSOLE_IO defined ++(because you don't want the messages) or PNG_NO_STDIO defined (because ++fprintf() isn't available). If you wish to change the behavior of the error ++functions, you will need to set up your own message callbacks. These ++functions are normally supplied at the time that the png_struct is created. ++It is also possible to redirect errors and warnings to your own replacement ++functions after png_create_*_struct() has been called by calling: ++ ++ png_set_error_fn(png_structp png_ptr, ++ png_voidp error_ptr, png_error_ptr error_fn, ++ png_error_ptr warning_fn); ++ ++If NULL is supplied for either error_fn or warning_fn, then the libpng ++default function will be used, calling fprintf() and/or longjmp() if a ++problem is encountered. The replacement error functions should have ++parameters as follows: ++ ++ void user_error_fn(png_structp png_ptr, ++ png_const_charp error_msg); ++ ++ void user_warning_fn(png_structp png_ptr, ++ png_const_charp warning_msg); ++ ++Then, within your user_error_fn or user_warning_fn, you can retrieve ++the error_ptr if you need it, by calling ++ ++ png_voidp error_ptr = png_get_error_ptr(png_ptr); ++ ++The motivation behind using setjmp() and longjmp() is the C++ throw and ++catch exception handling methods. This makes the code much easier to write, ++as there is no need to check every return code of every function call. ++However, there are some uncertainties about the status of local variables ++after a longjmp, so the user may want to be careful about doing anything ++after setjmp returns non-zero besides returning itself. Consult your ++compiler documentation for more details. For an alternative approach, you ++may wish to use the "cexcept" facility (see https://cexcept.sourceforge.io/), ++which is illustrated in pngvalid.c and in contrib/visupng. ++ ++Beginning in libpng-1.4.0, the png_set_benign_errors() API became available. ++You can use this to handle certain errors (normally handled as errors) ++as warnings. ++ ++ png_set_benign_errors (png_ptr, int allowed); ++ ++ allowed: 0: treat png_benign_error() as an error. ++ 1: treat png_benign_error() as a warning. ++ ++As of libpng-1.6.0, the default condition is to treat benign errors as ++warnings while reading and as errors while writing. ++ ++.SS Custom chunks ++ ++If you need to read or write custom chunks, you may need to get deeper ++into the libpng code. The library now has mechanisms for storing ++and writing chunks of unknown type; you can even declare callbacks ++for custom chunks. However, this may not be good enough if the ++library code itself needs to know about interactions between your ++chunk and existing `intrinsic' chunks. ++ ++If you need to write a new intrinsic chunk, first read the PNG ++specification. Acquire a first level of understanding of how it works. ++Pay particular attention to the sections that describe chunk names, ++and look at how other chunks were designed, so you can do things ++similarly. Second, check out the sections of libpng that read and ++write chunks. Try to find a chunk that is similar to yours and use ++it as a template. More details can be found in the comments inside ++the code. It is best to handle private or unknown chunks in a generic method, ++via callback functions, instead of by modifying libpng functions. This ++is illustrated in pngtest.c, which uses a callback function to handle a ++private "vpAg" chunk and the new "sTER" chunk, which are both unknown to ++libpng. ++ ++If you wish to write your own transformation for the data, look through ++the part of the code that does the transformations, and check out some of ++the simpler ones to get an idea of how they work. Try to find a similar ++transformation to the one you want to add and copy off of it. More details ++can be found in the comments inside the code itself. ++ ++.SS Configuring for gui/windowing platforms: ++ ++You will need to write new error and warning functions that use the GUI ++interface, as described previously, and set them to be the error and ++warning functions at the time that png_create_*_struct() is called, ++in order to have them available during the structure initialization. ++They can be changed later via png_set_error_fn(). On some compilers, ++you may also have to change the memory allocators (png_malloc, etc.). ++ ++.SS Configuring zlib: ++ ++There are special functions to configure the compression. Perhaps the ++most useful one changes the compression level, which currently uses ++input compression values in the range 0 - 9. The library normally ++uses the default compression level (Z_DEFAULT_COMPRESSION = 6). Tests ++have shown that for a large majority of images, compression values in ++the range 3-6 compress nearly as well as higher levels, and do so much ++faster. For online applications it may be desirable to have maximum speed ++(Z_BEST_SPEED = 1). With versions of zlib after v0.99, you can also ++specify no compression (Z_NO_COMPRESSION = 0), but this would create ++files larger than just storing the raw bitmap. You can specify the ++compression level by calling: ++ ++ #include zlib.h ++ png_set_compression_level(png_ptr, level); ++ ++Another useful one is to reduce the memory level used by the library. ++The memory level defaults to 8, but it can be lowered if you are ++short on memory (running DOS, for example, where you only have 640K). ++Note that the memory level does have an effect on compression; among ++other things, lower levels will result in sections of incompressible ++data being emitted in smaller stored blocks, with a correspondingly ++larger relative overhead of up to 15% in the worst case. ++ ++ #include zlib.h ++ png_set_compression_mem_level(png_ptr, level); ++ ++The other functions are for configuring zlib. They are not recommended ++for normal use and may result in writing an invalid PNG file. See ++zlib.h for more information on what these mean. ++ ++ #include zlib.h ++ png_set_compression_strategy(png_ptr, ++ strategy); ++ ++ png_set_compression_window_bits(png_ptr, ++ window_bits); ++ ++ png_set_compression_method(png_ptr, method); ++ ++This controls the size of the IDAT chunks (default 8192): ++ ++ png_set_compression_buffer_size(png_ptr, size); ++ ++As of libpng version 1.5.4, additional APIs became ++available to set these separately for non-IDAT ++compressed chunks such as zTXt, iTXt, and iCCP: ++ ++ #include zlib.h ++ #if PNG_LIBPNG_VER >= 10504 ++ png_set_text_compression_level(png_ptr, level); ++ ++ png_set_text_compression_mem_level(png_ptr, level); ++ ++ png_set_text_compression_strategy(png_ptr, ++ strategy); ++ ++ png_set_text_compression_window_bits(png_ptr, ++ window_bits); ++ ++ png_set_text_compression_method(png_ptr, method); ++ #endif ++ ++.SS Controlling row filtering ++ ++If you want to control whether libpng uses filtering or not, which ++filters are used, and how it goes about picking row filters, you ++can call one of these functions. The selection and configuration ++of row filters can have a significant impact on the size and ++encoding speed and a somewhat lesser impact on the decoding speed ++of an image. Filtering is enabled by default for RGB and grayscale ++images (with and without alpha), but not for paletted images nor ++for any images with bit depths less than 8 bits/pixel. ++ ++The 'method' parameter sets the main filtering method, which is ++currently only '0' in the PNG 1.2 specification. The 'filters' ++parameter sets which filter(s), if any, should be used for each ++scanline. Possible values are PNG_ALL_FILTERS, PNG_NO_FILTERS, ++or PNG_FAST_FILTERS to turn filtering on and off, or to turn on ++just the fast-decoding subset of filters, respectively. ++ ++Individual filter types are PNG_FILTER_NONE, PNG_FILTER_SUB, ++PNG_FILTER_UP, PNG_FILTER_AVG, PNG_FILTER_PAETH, which can be bitwise ++ORed together with '|' to specify one or more filters to use. ++These filters are described in more detail in the PNG specification. ++If you intend to change the filter type during the course of writing ++the image, you should start with flags set for all of the filters ++you intend to use so that libpng can initialize its internal ++structures appropriately for all of the filter types. (Note that this ++means the first row must always be adaptively filtered, because libpng ++currently does not allocate the filter buffers until png_write_row() ++is called for the first time.) ++ ++ filters = PNG_NO_FILTERS; ++ filters = PNG_ALL_FILTERS; ++ filters = PNG_FAST_FILTERS; ++ ++ or ++ ++ filters = PNG_FILTER_NONE | PNG_FILTER_SUB | ++ PNG_FILTER_UP | PNG_FILTER_AVG | ++ PNG_FILTER_PAETH; ++ ++ png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, ++ filters); ++ ++ The second parameter can also be ++ PNG_INTRAPIXEL_DIFFERENCING if you are ++ writing a PNG to be embedded in a MNG ++ datastream. This parameter must be the ++ same as the value of filter_method used ++ in png_set_IHDR(). ++ ++.SS Requesting debug printout ++ ++The macro definition PNG_DEBUG can be used to request debugging ++printout. Set it to an integer value in the range 0 to 3. Higher ++numbers result in increasing amounts of debugging information. The ++information is printed to the "stderr" file, unless another file ++name is specified in the PNG_DEBUG_FILE macro definition. ++ ++When PNG_DEBUG > 0, the following functions (macros) become available: ++ ++ png_debug(level, message) ++ png_debug1(level, message, p1) ++ png_debug2(level, message, p1, p2) ++ ++in which "level" is compared to PNG_DEBUG to decide whether to print ++the message, "message" is the formatted string to be printed, ++and p1 and p2 are parameters that are to be embedded in the string ++according to printf-style formatting directives. For example, ++ ++ png_debug1(2, "foo=%d", foo); ++ ++is expanded to ++ ++ if (PNG_DEBUG > 2) ++ fprintf(PNG_DEBUG_FILE, "foo=%d\en", foo); ++ ++When PNG_DEBUG is defined but is zero, the macros aren't defined, but you ++can still use PNG_DEBUG to control your own debugging: ++ ++ #ifdef PNG_DEBUG ++ fprintf(stderr, ... ++ #endif ++ ++When PNG_DEBUG = 1, the macros are defined, but only png_debug statements ++having level = 0 will be printed. There aren't any such statements in ++this version of libpng, but if you insert some they will be printed. ++ ++.SH VII. MNG support ++ ++The MNG specification (available at http://www.libpng.org/pub/mng) allows ++certain extensions to PNG for PNG images that are embedded in MNG datastreams. ++Libpng can support some of these extensions. To enable them, use the ++png_permit_mng_features() function: ++ ++ feature_set = png_permit_mng_features(png_ptr, mask) ++ ++ mask is a png_uint_32 containing the bitwise OR of the ++ features you want to enable. These include ++ PNG_FLAG_MNG_EMPTY_PLTE ++ PNG_FLAG_MNG_FILTER_64 ++ PNG_ALL_MNG_FEATURES ++ ++ feature_set is a png_uint_32 that is the bitwise AND of ++ your mask with the set of MNG features that is ++ supported by the version of libpng that you are using. ++ ++It is an error to use this function when reading or writing a standalone ++PNG file with the PNG 8-byte signature. The PNG datastream must be wrapped ++in a MNG datastream. As a minimum, it must have the MNG 8-byte signature ++and the MHDR and MEND chunks. Libpng does not provide support for these ++or any other MNG chunks; your application must provide its own support for ++them. You may wish to consider using libmng (available at ++https://www.libmng.com/) instead. ++ ++.SH VIII. Changes to Libpng from version 0.88 ++ ++It should be noted that versions of libpng later than 0.96 are not ++distributed by the original libpng author, Guy Schalnat, nor by ++Andreas Dilger, who had taken over from Guy during 1996 and 1997, and ++distributed versions 0.89 through 0.96, but rather by another member ++of the original PNG Group, Glenn Randers-Pehrson. Guy and Andreas are ++still alive and well, but they have moved on to other things. ++ ++The old libpng functions png_read_init(), png_write_init(), ++png_info_init(), png_read_destroy(), and png_write_destroy() have been ++moved to PNG_INTERNAL in version 0.95 to discourage their use. These ++functions will be removed from libpng version 1.4.0. ++ ++The preferred method of creating and initializing the libpng structures is ++via the png_create_read_struct(), png_create_write_struct(), and ++png_create_info_struct() because they isolate the size of the structures ++from the application, allow version error checking, and also allow the ++use of custom error handling routines during the initialization, which ++the old functions do not. The functions png_read_destroy() and ++png_write_destroy() do not actually free the memory that libpng ++allocated for these structs, but just reset the data structures, so they ++can be used instead of png_destroy_read_struct() and ++png_destroy_write_struct() if you feel there is too much system overhead ++allocating and freeing the png_struct for each image read. ++ ++Setting the error callbacks via png_set_message_fn() before ++png_read_init() as was suggested in libpng-0.88 is no longer supported ++because this caused applications that do not use custom error functions ++to fail if the png_ptr was not initialized to zero. It is still possible ++to set the error callbacks AFTER png_read_init(), or to change them with ++png_set_error_fn(), which is essentially the same function, but with a new ++name to force compilation errors with applications that try to use the old ++method. ++ ++Support for the sCAL, iCCP, iTXt, and sPLT chunks was added at libpng-1.0.6; ++however, iTXt support was not enabled by default. ++ ++Starting with version 1.0.7, you can find out which version of the library ++you are using at run-time: ++ ++ png_uint_32 libpng_vn = png_access_version_number(); ++ ++The number libpng_vn is constructed from the major version, minor ++version with leading zero, and release number with leading zero, ++(e.g., libpng_vn for version 1.0.7 is 10007). ++ ++Note that this function does not take a png_ptr, so you can call it ++before you've created one. ++ ++You can also check which version of png.h you used when compiling your ++application: ++ ++ png_uint_32 application_vn = PNG_LIBPNG_VER; ++ ++.SH IX. Changes to Libpng from version 1.0.x to 1.2.x ++ ++Support for user memory management was enabled by default. To ++accomplish this, the functions png_create_read_struct_2(), ++png_create_write_struct_2(), png_set_mem_fn(), png_get_mem_ptr(), ++png_malloc_default(), and png_free_default() were added. ++ ++Support for the iTXt chunk has been enabled by default as of ++version 1.2.41. ++ ++Support for certain MNG features was enabled. ++ ++Support for numbered error messages was added. However, we never got ++around to actually numbering the error messages. The function ++png_set_strip_error_numbers() was added (Note: the prototype for this ++function was inadvertently removed from png.h in PNG_NO_ASSEMBLER_CODE ++builds of libpng-1.2.15. It was restored in libpng-1.2.36). ++ ++The png_malloc_warn() function was added at libpng-1.2.3. This issues ++a png_warning and returns NULL instead of aborting when it fails to ++acquire the requested memory allocation. ++ ++Support for setting user limits on image width and height was enabled ++by default. The functions png_set_user_limits(), png_get_user_width_max(), ++and png_get_user_height_max() were added at libpng-1.2.6. ++ ++The png_set_add_alpha() function was added at libpng-1.2.7. ++ ++The function png_set_expand_gray_1_2_4_to_8() was added at libpng-1.2.9. ++Unlike png_set_gray_1_2_4_to_8(), the new function does not expand the ++tRNS chunk to alpha. The png_set_gray_1_2_4_to_8() function is ++deprecated. ++ ++A number of macro definitions in support of runtime selection of ++assembler code features (especially Intel MMX code support) were ++added at libpng-1.2.0: ++ ++ PNG_ASM_FLAG_MMX_SUPPORT_COMPILED ++ PNG_ASM_FLAG_MMX_SUPPORT_IN_CPU ++ PNG_ASM_FLAG_MMX_READ_COMBINE_ROW ++ PNG_ASM_FLAG_MMX_READ_INTERLACE ++ PNG_ASM_FLAG_MMX_READ_FILTER_SUB ++ PNG_ASM_FLAG_MMX_READ_FILTER_UP ++ PNG_ASM_FLAG_MMX_READ_FILTER_AVG ++ PNG_ASM_FLAG_MMX_READ_FILTER_PAETH ++ PNG_ASM_FLAGS_INITIALIZED ++ PNG_MMX_READ_FLAGS ++ PNG_MMX_FLAGS ++ PNG_MMX_WRITE_FLAGS ++ PNG_MMX_FLAGS ++ ++We added the following functions in support of runtime ++selection of assembler code features: ++ ++ png_get_mmx_flagmask() ++ png_set_mmx_thresholds() ++ png_get_asm_flags() ++ png_get_mmx_bitdepth_threshold() ++ png_get_mmx_rowbytes_threshold() ++ png_set_asm_flags() ++ ++We replaced all of these functions with simple stubs in libpng-1.2.20, ++when the Intel assembler code was removed due to a licensing issue. ++ ++These macros are deprecated: ++ ++ PNG_READ_TRANSFORMS_NOT_SUPPORTED ++ PNG_PROGRESSIVE_READ_NOT_SUPPORTED ++ PNG_NO_SEQUENTIAL_READ_SUPPORTED ++ PNG_WRITE_TRANSFORMS_NOT_SUPPORTED ++ PNG_READ_ANCILLARY_CHUNKS_NOT_SUPPORTED ++ PNG_WRITE_ANCILLARY_CHUNKS_NOT_SUPPORTED ++ ++They have been replaced, respectively, by: ++ ++ PNG_NO_READ_TRANSFORMS ++ PNG_NO_PROGRESSIVE_READ ++ PNG_NO_SEQUENTIAL_READ ++ PNG_NO_WRITE_TRANSFORMS ++ PNG_NO_READ_ANCILLARY_CHUNKS ++ PNG_NO_WRITE_ANCILLARY_CHUNKS ++ ++PNG_MAX_UINT was replaced with PNG_UINT_31_MAX. It has been ++deprecated since libpng-1.0.16 and libpng-1.2.6. ++ ++The function ++ png_check_sig(sig, num) ++was replaced with ++ !png_sig_cmp(sig, 0, num) ++It has been deprecated since libpng-0.90. ++ ++The function ++ png_set_gray_1_2_4_to_8() ++which also expands tRNS to alpha was replaced with ++ png_set_expand_gray_1_2_4_to_8() ++which does not. It has been deprecated since libpng-1.0.18 and 1.2.9. ++ ++.SH X. Changes to Libpng from version 1.0.x/1.2.x to 1.4.x ++ ++Private libpng prototypes and macro definitions were moved from ++png.h and pngconf.h into a new pngpriv.h header file. ++ ++Functions png_set_benign_errors(), png_benign_error(), and ++png_chunk_benign_error() were added. ++ ++Support for setting the maximum amount of memory that the application ++will allocate for reading chunks was added, as a security measure. ++The functions png_set_chunk_cache_max() and png_get_chunk_cache_max() ++were added to the library. ++ ++We implemented support for I/O states by adding png_ptr member io_state ++and functions png_get_io_chunk_name() and png_get_io_state() in pngget.c ++ ++We added PNG_TRANSFORM_GRAY_TO_RGB to the available high-level ++input transforms. ++ ++Checking for and reporting of errors in the IHDR chunk is more thorough. ++ ++Support for global arrays was removed, to improve thread safety. ++ ++Some obsolete/deprecated macros and functions have been removed. ++ ++Typecasted NULL definitions such as ++ #define png_voidp_NULL (png_voidp)NULL ++were eliminated. If you used these in your application, just use ++NULL instead. ++ ++The png_struct and info_struct members "trans" and "trans_values" were ++changed to "trans_alpha" and "trans_color", respectively. ++ ++The obsolete, unused pnggccrd.c and pngvcrd.c files and related makefiles ++were removed. ++ ++The PNG_1_0_X and PNG_1_2_X macros were eliminated. ++ ++The PNG_LEGACY_SUPPORTED macro was eliminated. ++ ++Many WIN32_WCE #ifdefs were removed. ++ ++The functions png_read_init(info_ptr), png_write_init(info_ptr), ++png_info_init(info_ptr), png_read_destroy(), and png_write_destroy() ++have been removed. They have been deprecated since libpng-0.95. ++ ++The png_permit_empty_plte() was removed. It has been deprecated ++since libpng-1.0.9. Use png_permit_mng_features() instead. ++ ++We removed the obsolete stub functions png_get_mmx_flagmask(), ++png_set_mmx_thresholds(), png_get_asm_flags(), ++png_get_mmx_bitdepth_threshold(), png_get_mmx_rowbytes_threshold(), ++png_set_asm_flags(), and png_mmx_supported() ++ ++We removed the obsolete png_check_sig(), png_memcpy_check(), and ++png_memset_check() functions. Instead use !png_sig_cmp(), memcpy(), ++and memset(), respectively. ++ ++The function png_set_gray_1_2_4_to_8() was removed. It has been ++deprecated since libpng-1.0.18 and 1.2.9, when it was replaced with ++png_set_expand_gray_1_2_4_to_8() because the former function also ++expanded any tRNS chunk to an alpha channel. ++ ++Macros for png_get_uint_16, png_get_uint_32, and png_get_int_32 ++were added and are used by default instead of the corresponding ++functions. Unfortunately, ++from libpng-1.4.0 until 1.4.4, the png_get_uint_16 macro (but not the ++function) incorrectly returned a value of type png_uint_32. ++ ++We changed the prototype for png_malloc() from ++ png_malloc(png_structp png_ptr, png_uint_32 size) ++to ++ png_malloc(png_structp png_ptr, png_alloc_size_t size) ++ ++This also applies to the prototype for the user replacement malloc_fn(). ++ ++The png_calloc() function was added and is used in place of ++of "png_malloc(); memset();" except in the case in png_read_png() ++where the array consists of pointers; in this case a "for" loop is used ++after the png_malloc() to set the pointers to NULL, to give robust. ++behavior in case the application runs out of memory part-way through ++the process. ++ ++We changed the prototypes of png_get_compression_buffer_size() and ++png_set_compression_buffer_size() to work with size_t instead of ++png_uint_32. ++ ++Support for numbered error messages was removed by default, since we ++never got around to actually numbering the error messages. The function ++png_set_strip_error_numbers() was removed from the library by default. ++ ++The png_zalloc() and png_zfree() functions are no longer exported. ++The png_zalloc() function no longer zeroes out the memory that it ++allocates. Applications that called png_zalloc(png_ptr, number, size) ++can call png_calloc(png_ptr, number*size) instead, and can call ++png_free() instead of png_zfree(). ++ ++Support for dithering was disabled by default in libpng-1.4.0, because ++it has not been well tested and doesn't actually "dither". ++The code was not ++removed, however, and could be enabled by building libpng with ++PNG_READ_DITHER_SUPPORTED defined. In libpng-1.4.2, this support ++was re-enabled, but the function was renamed png_set_quantize() to ++reflect more accurately what it actually does. At the same time, ++the PNG_DITHER_[RED,GREEN_BLUE]_BITS macros were also renamed to ++PNG_QUANTIZE_[RED,GREEN,BLUE]_BITS, and PNG_READ_DITHER_SUPPORTED ++was renamed to PNG_READ_QUANTIZE_SUPPORTED. ++ ++We removed the trailing '.' from the warning and error messages. ++ ++.SH XI. Changes to Libpng from version 1.4.x to 1.5.x ++ ++From libpng-1.4.0 until 1.4.4, the png_get_uint_16 macro (but not the ++function) incorrectly returned a value of type png_uint_32. ++The incorrect macro was removed from libpng-1.4.5. ++ ++Checking for invalid palette index on write was added at libpng ++1.5.10. If a pixel contains an invalid (out-of-range) index libpng issues ++a benign error. This is enabled by default because this condition is an ++error according to the PNG specification, Clause 11.3.2, but the error can ++be ignored in each png_ptr with ++ ++ png_set_check_for_invalid_index(png_ptr, allowed); ++ ++ allowed - one of ++ 0: disable benign error (accept the ++ invalid data without warning). ++ 1: enable benign error (treat the ++ invalid data as an error or a ++ warning). ++ ++If the error is ignored, or if png_benign_error() treats it as a warning, ++any invalid pixels are decoded as opaque black by the decoder and written ++as-is by the encoder. ++ ++Retrieving the maximum palette index found was added at libpng-1.5.15. ++This statement must appear after png_read_png() or png_read_image() while ++reading, and after png_write_png() or png_write_image() while writing. ++ ++ int max_palette = png_get_palette_max(png_ptr, info_ptr); ++ ++This will return the maximum palette index found in the image, or "\-1" if ++the palette was not checked, or "0" if no palette was found. Note that this ++does not account for any palette index used by ancillary chunks such as the ++bKGD chunk; you must check those separately to determine the maximum ++palette index actually used. ++ ++There are no substantial API changes between the non-deprecated parts of ++the 1.4.5 API and the 1.5.0 API; however, the ability to directly access ++members of the main libpng control structures, png_struct and png_info, ++deprecated in earlier versions of libpng, has been completely removed from ++libpng 1.5, and new private "pngstruct.h", "pnginfo.h", and "pngdebug.h" ++header files were created. ++ ++We no longer include zlib.h in png.h. The include statement has been moved ++to pngstruct.h, where it is not accessible by applications. Applications that ++need access to information in zlib.h will need to add the '#include "zlib.h"' ++directive. It does not matter whether this is placed prior to or after ++the '"#include png.h"' directive. ++ ++The png_sprintf(), png_strcpy(), and png_strncpy() macros are no longer used ++and were removed. ++ ++We moved the png_strlen(), png_memcpy(), png_memset(), and png_memcmp() ++macros into a private header file (pngpriv.h) that is not accessible to ++applications. ++ ++In png_get_iCCP, the type of "profile" was changed from png_charpp ++to png_bytepp, and in png_set_iCCP, from png_charp to png_const_bytep. ++ ++There are changes of form in png.h, including new and changed macros to ++declare parts of the API. Some API functions with arguments that are ++pointers to data not modified within the function have been corrected to ++declare these arguments with const. ++ ++Much of the internal use of C macros to control the library build has also ++changed and some of this is visible in the exported header files, in ++particular the use of macros to control data and API elements visible ++during application compilation may require significant revision to ++application code. (It is extremely rare for an application to do this.) ++ ++Any program that compiled against libpng 1.4 and did not use deprecated ++features or access internal library structures should compile and work ++against libpng 1.5, except for the change in the prototype for ++png_get_iCCP() and png_set_iCCP() API functions mentioned above. ++ ++libpng 1.5.0 adds PNG_ PASS macros to help in the reading and writing of ++interlaced images. The macros return the number of rows and columns in ++each pass and information that can be used to de-interlace and (if ++absolutely necessary) interlace an image. ++ ++libpng 1.5.0 adds an API png_longjmp(png_ptr, value). This API calls ++the application-provided png_longjmp_ptr on the internal, but application ++initialized, longjmp buffer. It is provided as a convenience to avoid ++the need to use the png_jmpbuf macro, which had the unnecessary side ++effect of resetting the internal png_longjmp_ptr value. ++ ++libpng 1.5.0 includes a complete fixed point API. By default this is ++present along with the corresponding floating point API. In general the ++fixed point API is faster and smaller than the floating point one because ++the PNG file format used fixed point, not floating point. This applies ++even if the library uses floating point in internal calculations. A new ++macro, PNG_FLOATING_ARITHMETIC_SUPPORTED, reveals whether the library ++uses floating point arithmetic (the default) or fixed point arithmetic ++internally for performance critical calculations such as gamma correction. ++In some cases, the gamma calculations may produce slightly different ++results. This has changed the results in png_rgb_to_gray and in alpha ++composition (png_set_background for example). This applies even if the ++original image was already linear (gamma == 1.0) and, therefore, it is ++not necessary to linearize the image. This is because libpng has *not* ++been changed to optimize that case correctly, yet. ++ ++Fixed point support for the sCAL chunk comes with an important caveat; ++the sCAL specification uses a decimal encoding of floating point values ++and the accuracy of PNG fixed point values is insufficient for ++representation of these values. Consequently a "string" API ++(png_get_sCAL_s and png_set_sCAL_s) is the only reliable way of reading ++arbitrary sCAL chunks in the absence of either the floating point API or ++internal floating point calculations. Starting with libpng-1.5.0, both ++of these functions are present when PNG_sCAL_SUPPORTED is defined. Prior ++to libpng-1.5.0, their presence also depended upon PNG_FIXED_POINT_SUPPORTED ++being defined and PNG_FLOATING_POINT_SUPPORTED not being defined. ++ ++Applications no longer need to include the optional distribution header ++file pngusr.h or define the corresponding macros during application ++build in order to see the correct variant of the libpng API. From 1.5.0 ++application code can check for the corresponding _SUPPORTED macro: ++ ++#ifdef PNG_INCH_CONVERSIONS_SUPPORTED ++ /* code that uses the inch conversion APIs. */ ++#endif ++ ++This macro will only be defined if the inch conversion functions have been ++compiled into libpng. The full set of macros, and whether or not support ++has been compiled in, are available in the header file pnglibconf.h. ++This header file is specific to the libpng build. Notice that prior to ++1.5.0 the _SUPPORTED macros would always have the default definition unless ++reset by pngusr.h or by explicit settings on the compiler command line. ++These settings may produce compiler warnings or errors in 1.5.0 because ++of macro redefinition. ++ ++Applications can now choose whether to use these macros or to call the ++corresponding function by defining PNG_USE_READ_MACROS or ++PNG_NO_USE_READ_MACROS before including png.h. Notice that this is ++only supported from 1.5.0; defining PNG_NO_USE_READ_MACROS prior to 1.5.0 ++will lead to a link failure. ++ ++Prior to libpng-1.5.4, the zlib compressor used the same set of parameters ++when compressing the IDAT data and textual data such as zTXt and iCCP. ++In libpng-1.5.4 we reinitialized the zlib stream for each type of data. ++We added five png_set_text_*() functions for setting the parameters to ++use with textual data. ++ ++Prior to libpng-1.5.4, the PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED ++option was off by default, and slightly inaccurate scaling occurred. ++This option can no longer be turned off, and the choice of accurate ++or inaccurate 16-to-8 scaling is by using the new png_set_scale_16_to_8() ++API for accurate scaling or the old png_set_strip_16_to_8() API for simple ++chopping. In libpng-1.5.4, the PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED ++macro became PNG_READ_SCALE_16_TO_8_SUPPORTED, and the PNG_READ_16_TO_8 ++macro became PNG_READ_STRIP_16_TO_8_SUPPORTED, to enable the two ++png_set_*_16_to_8() functions separately. ++ ++Prior to libpng-1.5.4, the png_set_user_limits() function could only be ++used to reduce the width and height limits from the value of ++PNG_USER_WIDTH_MAX and PNG_USER_HEIGHT_MAX, although this document said ++that it could be used to override them. Now this function will reduce or ++increase the limits. ++ ++Starting in libpng-1.5.22, default user limits were established. These ++can be overridden by application calls to png_set_user_limits(), ++png_set_user_chunk_cache_max(), and/or png_set_user_malloc_max(). ++The limits are now ++ max possible default ++ png_user_width_max 0x7fffffff 1,000,000 ++ png_user_height_max 0x7fffffff 1,000,000 ++ png_user_chunk_cache_max 0 (unlimited) 1000 ++ png_user_chunk_malloc_max 0 (unlimited) 8,000,000 ++ ++The png_set_option() function (and the "options" member of the png struct) was ++added to libpng-1.5.15, with option PNG_ARM_NEON. ++ ++The library now supports a complete fixed point implementation and can ++thus be used on systems that have no floating point support or very ++limited or slow support. Previously gamma correction, an essential part ++of complete PNG support, required reasonably fast floating point. ++ ++As part of this the choice of internal implementation has been made ++independent of the choice of fixed versus floating point APIs and all the ++missing fixed point APIs have been implemented. ++ ++The exact mechanism used to control attributes of API functions has ++changed, as described in the INSTALL file. ++ ++A new test program, pngvalid, is provided in addition to pngtest. ++pngvalid validates the arithmetic accuracy of the gamma correction ++calculations and includes a number of validations of the file format. ++A subset of the full range of tests is run when "make check" is done ++(in the 'configure' build.) pngvalid also allows total allocated memory ++usage to be evaluated and performs additional memory overwrite validation. ++ ++Many changes to individual feature macros have been made. The following ++are the changes most likely to be noticed by library builders who ++configure libpng: ++ ++1) All feature macros now have consistent naming: ++ ++#define PNG_NO_feature turns the feature off ++#define PNG_feature_SUPPORTED turns the feature on ++ ++pnglibconf.h contains one line for each feature macro which is either: ++ ++#define PNG_feature_SUPPORTED ++ ++if the feature is supported or: ++ ++/*#undef PNG_feature_SUPPORTED*/ ++ ++if it is not. Library code consistently checks for the 'SUPPORTED' macro. ++It does not, and libpng applications should not, check for the 'NO' macro ++which will not normally be defined even if the feature is not supported. ++The 'NO' macros are only used internally for setting or not setting the ++corresponding 'SUPPORTED' macros. ++ ++Compatibility with the old names is provided as follows: ++ ++PNG_INCH_CONVERSIONS turns on PNG_INCH_CONVERSIONS_SUPPORTED ++ ++And the following definitions disable the corresponding feature: ++ ++PNG_SETJMP_NOT_SUPPORTED disables SETJMP ++PNG_READ_TRANSFORMS_NOT_SUPPORTED disables READ_TRANSFORMS ++PNG_NO_READ_COMPOSITED_NODIV disables READ_COMPOSITE_NODIV ++PNG_WRITE_TRANSFORMS_NOT_SUPPORTED disables WRITE_TRANSFORMS ++PNG_READ_ANCILLARY_CHUNKS_NOT_SUPPORTED disables READ_ANCILLARY_CHUNKS ++PNG_WRITE_ANCILLARY_CHUNKS_NOT_SUPPORTED disables WRITE_ANCILLARY_CHUNKS ++ ++Library builders should remove use of the above, inconsistent, names. ++ ++2) Warning and error message formatting was previously conditional on ++the STDIO feature. The library has been changed to use the ++CONSOLE_IO feature instead. This means that if CONSOLE_IO is disabled ++the library no longer uses the printf(3) functions, even though the ++default read/write implementations use (FILE) style stdio.h functions. ++ ++3) Three feature macros now control the fixed/floating point decisions: ++ ++PNG_FLOATING_POINT_SUPPORTED enables the floating point APIs ++ ++PNG_FIXED_POINT_SUPPORTED enables the fixed point APIs; however, in ++practice these are normally required internally anyway (because the PNG ++file format is fixed point), therefore in most cases PNG_NO_FIXED_POINT ++merely stops the function from being exported. ++ ++PNG_FLOATING_ARITHMETIC_SUPPORTED chooses between the internal floating ++point implementation or the fixed point one. Typically the fixed point ++implementation is larger and slower than the floating point implementation ++on a system that supports floating point; however, it may be faster on a ++system which lacks floating point hardware and therefore uses a software ++emulation. ++ ++4) Added PNG_{READ,WRITE}_INT_FUNCTIONS_SUPPORTED. This allows the ++functions to read and write ints to be disabled independently of ++PNG_USE_READ_MACROS, which allows libpng to be built with the functions ++even though the default is to use the macros - this allows applications ++to choose at app buildtime whether or not to use macros (previously ++impossible because the functions weren't in the default build.) ++ ++.SH XII. Changes to Libpng from version 1.5.x to 1.6.x ++ ++A "simplified API" has been added (see documentation in png.h and a simple ++example in contrib/examples/pngtopng.c). The new publicly visible API ++includes the following: ++ ++ macros: ++ PNG_FORMAT_* ++ PNG_IMAGE_* ++ structures: ++ png_control ++ png_image ++ read functions ++ png_image_begin_read_from_file() ++ png_image_begin_read_from_stdio() ++ png_image_begin_read_from_memory() ++ png_image_finish_read() ++ png_image_free() ++ write functions ++ png_image_write_to_file() ++ png_image_write_to_memory() ++ png_image_write_to_stdio() ++ ++Starting with libpng-1.6.0, you can configure libpng to prefix all exported ++symbols, using the PNG_PREFIX macro. ++ ++We no longer include string.h in png.h. The include statement has been moved ++to pngpriv.h, where it is not accessible by applications. Applications that ++need access to information in string.h must add an '#include ' ++directive. It does not matter whether this is placed prior to or after ++the '#include "png.h"' directive. ++ ++The following API are now DEPRECATED: ++ png_info_init_3() ++ png_convert_to_rfc1123() which has been replaced ++ with png_convert_to_rfc1123_buffer() ++ png_malloc_default() ++ png_free_default() ++ png_reset_zstream() ++ ++The following have been removed: ++ png_get_io_chunk_name(), which has been replaced ++ with png_get_io_chunk_type(). The new ++ function returns a 32-bit integer instead of ++ a string. ++ The png_sizeof(), png_strlen(), png_memcpy(), png_memcmp(), and ++ png_memset() macros are no longer used in the libpng sources and ++ have been removed. These had already been made invisible to applications ++ (i.e., defined in the private pngpriv.h header file) since libpng-1.5.0. ++ ++The signatures of many exported functions were changed, such that ++ png_structp became png_structrp or png_const_structrp ++ png_infop became png_inforp or png_const_inforp ++where "rp" indicates a "restricted pointer". ++ ++Dropped support for 16-bit platforms. The support for FAR/far types has ++been eliminated and the definition of png_alloc_size_t is now controlled ++by a flag so that 'small size_t' systems can select it if necessary. ++ ++Error detection in some chunks has improved; in particular the iCCP chunk ++reader now does pretty complete validation of the basic format. Some bad ++profiles that were previously accepted are now accepted with a warning or ++rejected, depending upon the png_set_benign_errors() setting, in particular ++the very old broken Microsoft/HP 3144-byte sRGB profile. Starting with ++libpng-1.6.11, recognizing and checking sRGB profiles can be avoided by ++means of ++ ++ #if defined(PNG_SKIP_sRGB_CHECK_PROFILE) && \ ++ defined(PNG_SET_OPTION_SUPPORTED) ++ png_set_option(png_ptr, PNG_SKIP_sRGB_CHECK_PROFILE, ++ PNG_OPTION_ON); ++ #endif ++ ++It's not a good idea to do this if you are using the "simplified API", ++which needs to be able to recognize sRGB profiles conveyed via the iCCP ++chunk. ++ ++The PNG spec requirement that only grayscale profiles may appear in images ++with color type 0 or 4 and that even if the image only contains gray pixels, ++only RGB profiles may appear in images with color type 2, 3, or 6, is now ++enforced. The sRGB chunk is allowed to appear in images with any color type ++and is interpreted by libpng to convey a one-tracer-curve gray profile or a ++three-tracer-curve RGB profile as appropriate. ++ ++Libpng 1.5.x erroneously used /MD for Debug DLL builds; if you used the debug ++builds in your app and you changed your app to use /MD you will need to ++change it back to /MDd for libpng 1.6.x. ++ ++Prior to libpng-1.6.0 a warning would be issued if the iTXt chunk contained ++an empty language field or an empty translated keyword. Both of these ++are allowed by the PNG specification, so these warnings are no longer issued. ++ ++The library now issues an error if the application attempts to set a ++transform after it calls png_read_update_info() or if it attempts to call ++both png_read_update_info() and png_start_read_image() or to call either ++of them more than once. ++ ++The default condition for benign_errors is now to treat benign errors as ++warnings while reading and as errors while writing. ++ ++The library now issues a warning if both background processing and RGB to ++gray are used when gamma correction happens. As with previous versions of ++the library the results are numerically very incorrect in this case. ++ ++There are some minor arithmetic changes in some transforms such as ++png_set_background(), that might be detected by certain regression tests. ++ ++Unknown chunk handling has been improved internally, without any API change. ++This adds more correct option control of the unknown handling, corrects ++a pre-existing bug where the per-chunk 'keep' setting is ignored, and makes ++it possible to skip IDAT chunks in the sequential reader. ++ ++The machine-generated configure files are no longer included in branches ++libpng16 and later of the GIT repository. They continue to be included ++in the tarball releases, however. ++ ++Libpng-1.6.0 through 1.6.2 used the CMF bytes at the beginning of the IDAT ++stream to set the size of the sliding window for reading instead of using the ++default 32-kbyte sliding window size. It was discovered that there are ++hundreds of PNG files in the wild that have incorrect CMF bytes that caused ++zlib to issue the "invalid distance too far back" error and reject the file. ++Libpng-1.6.3 and later calculate their own safe CMF from the image dimensions, ++provide a way to revert to the libpng-1.5.x behavior (ignoring the CMF bytes ++and using a 32-kbyte sliding window), by using ++ ++ png_set_option(png_ptr, PNG_MAXIMUM_INFLATE_WINDOW, ++ PNG_OPTION_ON); ++ ++and provide a tool (contrib/tools/pngfix) for rewriting a PNG file while ++optimizing the CMF bytes in its IDAT chunk correctly. ++ ++Libpng-1.6.0 and libpng-1.6.1 wrote uncompressed iTXt chunks with the wrong ++length, which resulted in PNG files that cannot be read beyond the bad iTXt ++chunk. This error was fixed in libpng-1.6.3, and a tool (called ++contrib/tools/png-fix-itxt) has been added to the libpng distribution. ++ ++Starting with libpng-1.6.17, the PNG_SAFE_LIMITS macro was eliminated ++and safe limits are used by default (users who need larger limits ++can still override them at compile time or run time, as described above). ++ ++The new limits are ++ default spec limit ++ png_user_width_max 1,000,000 2,147,483,647 ++ png_user_height_max 1,000,000 2,147,483,647 ++ png_user_chunk_cache_max 128 unlimited ++ png_user_chunk_malloc_max 8,000,000 unlimited ++ ++Starting with libpng-1.6.18, a PNG_RELEASE_BUILD macro was added, which allows ++library builders to control compilation for an installed system (a release build). ++It can be set for testing debug or beta builds to ensure that they will compile ++when the build type is switched to RC or STABLE. In essence this overrides the ++PNG_LIBPNG_BUILD_BASE_TYPE definition which is not directly user controllable. ++ ++Starting with libpng-1.6.19, attempting to set an over-length PLTE chunk ++is an error. Previously this requirement of the PNG specification was not ++enforced, and the palette was always limited to 256 entries. An over-length ++PLTE chunk found in an input PNG is silently truncated. ++ ++Starting with libpng-1.6.31, the eXIf chunk is supported. Libpng does not ++attempt to decode the Exif profile; it simply returns a byte array ++containing the profile to the calling application which must do its own ++decoding. ++ ++.SH XIII. Detecting libpng ++ ++The png_get_io_ptr() function has been present since libpng-0.88, has never ++changed, and is unaffected by conditional compilation macros. It is the ++best choice for use in configure scripts for detecting the presence of any ++libpng version since 0.88. In an autoconf "configure.in" you could use ++ ++ AC_CHECK_LIB(png, png_get_io_ptr, ... ++ ++.SH XV. Source code repository ++ ++Since about February 2009, version 1.2.34, libpng has been under "git" source ++control. The git repository was built from old libpng-x.y.z.tar.gz files ++going back to version 0.70. You can access the git repository (read only) ++at ++ ++ https://github.com/glennrp/libpng or ++ https://git.code.sf.net/p/libpng/code.git ++ ++or you can browse it with a web browser at ++ ++ https://github.com/glennrp/libpng or ++ https://sourceforge.net/p/libpng/code/ci/libpng16/tree/ ++ ++Patches can be sent to png-mng-implement at lists.sourceforge.net or ++uploaded to the libpng bug tracker at ++ ++ https://libpng.sourceforge.io/ ++ ++or as a "pull request" to ++ ++ https://github.com/glennrp/libpng/pulls ++ ++We also accept patches built from the tar or zip distributions, and ++simple verbal descriptions of bug fixes, reported either to the ++SourceForge bug tracker, to the png-mng-implement at lists.sf.net ++mailing list, as github issues. ++ ++.SH XV. Coding style ++ ++Our coding style is similar to the "Allman" style ++(See https://en.wikipedia.org/wiki/Indent_style#Allman_style), with curly ++braces on separate lines: ++ ++ if (condition) ++ { ++ action; ++ } ++ ++ else if (another condition) ++ { ++ another action; ++ } ++ ++The braces can be omitted from simple one-line actions: ++ ++ if (condition) ++ return 0; ++ ++We use 3-space indentation, except for continued statements which ++are usually indented the same as the first line of the statement ++plus four more spaces. ++ ++For macro definitions we use 2-space indentation, always leaving the "#" ++in the first column. ++ ++ #ifndef PNG_NO_FEATURE ++ # ifndef PNG_FEATURE_SUPPORTED ++ # define PNG_FEATURE_SUPPORTED ++ # endif ++ #endif ++ ++Comments appear with the leading "/*" at the same indentation as ++the statement that follows the comment: ++ ++ /* Single-line comment */ ++ statement; ++ ++ /* This is a multiple-line ++ * comment. ++ */ ++ statement; ++ ++Very short comments can be placed after the end of the statement ++to which they pertain: ++ ++ statement; /* comment */ ++ ++We don't use C++ style ("//") comments. We have, however, ++used them in the past in some now-abandoned MMX assembler ++code. ++ ++Functions and their curly braces are not indented, and ++exported functions are marked with PNGAPI: ++ ++ /* This is a public function that is visible to ++ * application programmers. It does thus-and-so. ++ */ ++ void PNGAPI ++ png_exported_function(png_ptr, png_info, foo) ++ { ++ body; ++ } ++ ++The return type and decorations are placed on a separate line ++ahead of the function name, as illustrated above. ++ ++The prototypes for all exported functions appear in png.h, ++above the comment that says ++ ++ /* Maintainer: Put new public prototypes here ... */ ++ ++We mark all non-exported functions with "/* PRIVATE */"": ++ ++ void /* PRIVATE */ ++ png_non_exported_function(png_ptr, png_info, foo) ++ { ++ body; ++ } ++ ++The prototypes for non-exported functions (except for those in ++pngtest) appear in pngpriv.h above the comment that says ++ ++ /* Maintainer: Put new private prototypes here ^ */ ++ ++To avoid polluting the global namespace, the names of all exported ++functions and variables begin with "png_", and all publicly visible C ++preprocessor macros begin with "PNG". We request that applications that ++use libpng *not* begin any of their own symbols with either of these strings. ++ ++We put a space after the "sizeof" operator and we omit the ++optional parentheses around its argument when the argument ++is an expression, not a type name, and we always enclose the ++sizeof operator, with its argument, in parentheses: ++ ++ (sizeof (png_uint_32)) ++ (sizeof array) ++ ++Prior to libpng-1.6.0 we used a "png_sizeof()" macro, formatted as ++though it were a function. ++ ++Control keywords if, for, while, and switch are always followed by a space ++to distinguish them from function calls, which have no trailing space. ++ ++We put a space after each comma and after each semicolon ++in "for" statements, and we put spaces before and after each ++C binary operator and after "for" or "while", and before ++"?". We don't put a space between a typecast and the expression ++being cast, nor do we put one between a function name and the ++left parenthesis that follows it: ++ ++ for (i = 2; i > 0; \-\-i) ++ y[i] = a(x) + (int)b; ++ ++We prefer #ifdef and #ifndef to #if defined() and #if !defined() ++when there is only one macro being tested. We always use parentheses ++with "defined". ++ ++We express integer constants that are used as bit masks in hex format, ++with an even number of lower-case hex digits, and to make them unsigned ++(e.g., 0x00U, 0xffU, 0x0100U) and long if they are greater than 0x7fff ++(e.g., 0xffffUL). ++ ++We prefer to use underscores rather than camelCase in names, except ++for a few type names that we inherit from zlib.h. ++ ++We prefer "if (something != 0)" and "if (something == 0)" over ++"if (something)" and if "(!something)", respectively, and for pointers ++we prefer "if (some_pointer != NULL)" or "if (some_pointer == NULL)". ++ ++We do not use the TAB character for indentation in the C sources. ++ ++Lines do not exceed 80 characters. ++ ++Other rules can be inferred by inspecting the libpng source. ++ ++.SH NOTE ++ ++Note about libpng version numbers: ++ ++Due to various miscommunications, unforeseen code incompatibilities ++and occasional factors outside the authors' control, version numbering ++on the library has not always been consistent and straightforward. ++The following table summarizes matters since version 0.89c, which was ++the first widely used release: ++ ++ source png.h png.h shared-lib ++ version string int version ++ ------- ------ ----- ---------- ++ 0.89c "1.0 beta 3" 0.89 89 1.0.89 ++ 0.90 "1.0 beta 4" 0.90 90 0.90 [should have been 2.0.90] ++ 0.95 "1.0 beta 5" 0.95 95 0.95 [should have been 2.0.95] ++ 0.96 "1.0 beta 6" 0.96 96 0.96 [should have been 2.0.96] ++ 0.97b "1.00.97 beta 7" 1.00.97 97 1.0.1 [should have been 2.0.97] ++ 0.97c 0.97 97 2.0.97 ++ 0.98 0.98 98 2.0.98 ++ 0.99 0.99 98 2.0.99 ++ 0.99a-m 0.99 99 2.0.99 ++ 1.00 1.00 100 2.1.0 [100 should be 10000] ++ 1.0.0 (from here on, the 100 2.1.0 [100 should be 10000] ++ 1.0.1 png.h string is 10001 2.1.0 ++ 1.0.1a-e identical to the 10002 from here on, the shared library ++ 1.0.2 source version) 10002 is 2.V where V is the source code ++ 1.0.2a-b 10003 version, except as noted. ++ 1.0.3 10003 ++ 1.0.3a-d 10004 ++ 1.0.4 10004 ++ 1.0.4a-f 10005 ++ 1.0.5 (+ 2 patches) 10005 ++ 1.0.5a-d 10006 ++ 1.0.5e-r 10100 (not source compatible) ++ 1.0.5s-v 10006 (not binary compatible) ++ 1.0.6 (+ 3 patches) 10006 (still binary incompatible) ++ 1.0.6d-f 10007 (still binary incompatible) ++ 1.0.6g 10007 ++ 1.0.6h 10007 10.6h (testing xy.z so-numbering) ++ 1.0.6i 10007 10.6i ++ 1.0.6j 10007 2.1.0.6j (incompatible with 1.0.0) ++ 1.0.7beta11-14 DLLNUM 10007 2.1.0.7beta11-14 (binary compatible) ++ 1.0.7beta15-18 1 10007 2.1.0.7beta15-18 (binary compatible) ++ 1.0.7rc1-2 1 10007 2.1.0.7rc1-2 (binary compatible) ++ 1.0.7 1 10007 (still compatible) ++ ... ++ 1.0.69 10 10069 10.so.0.69[.0] ++ ... ++ 1.2.59 13 10259 12.so.0.59[.0] ++ ... ++ 1.4.20 14 10420 14.so.0.20[.0] ++ ... ++ 1.5.30 15 10530 15.so.15.30[.0] ++ ... ++ 1.6.35 16 10635 16.so.16.35[.0] ++ ++Henceforth the source version will match the shared-library minor and ++patch numbers; the shared-library major version number will be used for ++changes in backward compatibility, as it is intended. ++The PNG_PNGLIB_VER macro, which is not used within libpng but is ++available for applications, is an unsigned integer of the form XYYZZ ++corresponding to the source version X.Y.Z (leading zeros in Y and Z). ++Beta versions were given the previous public release number plus a ++letter, until version 1.0.6j; from then on they were given the upcoming ++public release number plus "betaNN" or "rcNN". ++ ++.SH "SEE ALSO" ++.IR libpngpf(3) ", " png(5) ++.LP ++.IR libpng : ++.IP ++https://libpng.sourceforge.io/ (follow the [DOWNLOAD] link) ++http://www.libpng.org/pub/png ++ ++.LP ++.IR zlib : ++.IP ++(generally) at the same location as ++.I libpng ++or at ++.br ++https://zlib.net/ ++ ++.LP ++.IR PNG specification: RFC 2083 ++.IP ++(generally) at the same location as ++.I libpng ++or at ++.br ++https://www.ietf.org/rfc/rfc2083.txt ++.br ++or (as a W3C Recommendation) at ++.br ++https://www.w3.org/TR/REC-png.html ++ ++.LP ++In the case of any inconsistency between the PNG specification ++and this library, the specification takes precedence. ++ ++.SH AUTHORS ++This man page: ++Initially created by Glenn Randers-Pehrson. ++Maintained by Cosmin Truta. ++ ++The contributing authors would like to thank all those who helped ++with testing, bug fixes, and patience. This wouldn't have been ++possible without all of you. ++ ++Thanks to Frank J. T. Wojcik for helping with the documentation. ++ ++Libpng: ++Initially created in 1995 by Guy Eric Schalnat, then of Group 42, Inc. ++Maintained by Cosmin Truta. ++ ++Supported by the PNG development group ++.br ++png-mng-implement at lists.sourceforge.net (subscription required; visit ++https://lists.sourceforge.net/lists/listinfo/png-mng-implement ++to subscribe). ++ ++.\" end of man page +diff --git a/lib/libpng/libpngpf.3 b/lib/libpng/libpngpf.3 +new file mode 100644 +index 000000000..6909c70a3 +--- /dev/null ++++ b/lib/libpng/libpngpf.3 +@@ -0,0 +1,24 @@ ++.TH LIBPNGPF 3 "April 14, 2019" ++.SH NAME ++libpng \- Portable Network Graphics (PNG) Reference Library 1.6.37 ++(private functions) ++ ++.SH SYNOPSIS ++\fB#include \fI"pngpriv.h" ++ ++\fBAs of libpng version \fP\fI1.5.1\fP\fB, this section is no longer ++\fP\fImaintained\fP\fB, now that the private function prototypes are hidden in ++\fP\fIpngpriv.h\fP\fB and not accessible to applications. Look in ++\fP\fIpngpriv.h\fP\fB for the prototypes and a short description of each ++function. ++ ++.SH DESCRIPTION ++The functions previously listed here are used privately by libpng and are not ++available for use by applications. They are not "exported" to applications ++using shared libraries. ++ ++.SH "SEE ALSO" ++.BR "png"(5), " libpng"(3), " zlib"(3), " deflate"(5), " " and " zlib"(5) ++ ++.SH AUTHORS ++Cosmin Truta, Glenn Randers-Pehrson +diff --git a/lib/libpng/png.5 b/lib/libpng/png.5 +new file mode 100644 +index 000000000..c2da95c45 +--- /dev/null ++++ b/lib/libpng/png.5 +@@ -0,0 +1,84 @@ ++.TH PNG 5 "April 14, 2019" ++.SH NAME ++png \- Portable Network Graphics (PNG) format ++ ++.SH DESCRIPTION ++PNG (Portable Network Graphics) is an extensible file format for the ++lossless, portable, well-compressed storage of raster images. PNG ++provides a patent-free replacement for GIF, and can also replace many ++common uses of TIFF. Indexed-color, grayscale, and truecolor images are ++supported, plus an optional alpha channel. Sample depths range from ++1 to 16 bits. ++.br ++PNG is designed to work well in online viewing applications, such ++as the World Wide Web, so it is fully streamable with a progressive ++display option. PNG is robust, providing both full file integrity ++checking and fast, simple detection of common transmission errors. ++Also, PNG can store gamma and chromaticity data for improved color ++matching on heterogeneous platforms. ++ ++.SH "SEE ALSO" ++.BR "libpng"(3), " libpngpf"(3), " zlib"(3), " deflate"(5), " " and " zlib"(5) ++.LP ++PNG Specification (Second Edition), November 2003: ++.IP ++.br ++https://www.w3.org/TR/2003/REC-PNG-20031110/ ++.LP ++PNG 1.2 Specification, July 1999: ++.IP ++.br ++https://png-mng.sourceforge.io/pub/png/spec/1.2/ ++.LP ++PNG 1.0 Specification, October 1996: ++.IP ++.br ++RFC 2083 ++.br ++https://www.ietf.org/rfc/rfc2083.txt ++.IP ++.br ++or W3C Recommendation ++.br ++https://www.w3.org/TR/REC-png-961001 ++ ++.SH AUTHORS ++This man page: Cosmin Truta, Glenn Randers-Pehrson ++.LP ++Portable Network Graphics (PNG) Specification (Second Edition) ++Information technology - Computer graphics and image processing - ++Portable Network Graphics (PNG): Functional specification. ++ISO/IEC 15948:2003 (E) (November 10, 2003): David Duce and others. ++.LP ++Portable Network Graphics (PNG) Specification Version 1.2 (July 8, 1999): ++Glenn Randers-Pehrson and others (png-list). ++.LP ++Portable Network Graphics (PNG) Specification Version 1.0 (October 1, 1996): ++Thomas Boutell and others (png-list). ++ ++.SH COPYRIGHT ++.LP ++This man page is ++.br ++Copyright (c) 2018-2019 Cosmin Truta. ++.br ++Copyright (c) 1998-2006 Glenn Randers-Pehrson. ++.br ++See png.h for conditions of use and distribution. ++.LP ++The PNG Specification (Second Edition) is ++.br ++Copyright (c) 2003 W3C. (MIT, ERCIM, Keio), All Rights Reserved. ++.LP ++The PNG-1.2 Specification is ++.br ++Copyright (c) 1999 Glenn Randers-Pehrson. ++.br ++See the specification for conditions of use and distribution. ++.LP ++The PNG-1.0 Specification is ++.br ++Copyright (c) 1996 Massachusetts Institute of Technology. ++.br ++See the specification for conditions of use and distribution. ++.\" end of man page +diff --git a/lib/libpng/png.c b/lib/libpng/png.c +new file mode 100644 +index 000000000..757c755f9 +--- /dev/null ++++ b/lib/libpng/png.c +@@ -0,0 +1,4607 @@ ++ ++/* png.c - location for general purpose libpng functions ++ * ++ * Copyright (c) 2018-2019 Cosmin Truta ++ * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson ++ * Copyright (c) 1996-1997 Andreas Dilger ++ * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc. ++ * ++ * This code is released under the libpng license. ++ * For conditions of distribution and use, see the disclaimer ++ * and license in png.h ++ */ ++ ++#include "pngpriv.h" ++ ++/* Generate a compiler error if there is an old png.h in the search path. */ ++typedef png_libpng_version_1_6_37 Your_png_h_is_not_version_1_6_37; ++ ++#ifdef __GNUC__ ++/* The version tests may need to be added to, but the problem warning has ++ * consistently been fixed in GCC versions which obtain wide-spread release. ++ * The problem is that many versions of GCC rearrange comparison expressions in ++ * the optimizer in such a way that the results of the comparison will change ++ * if signed integer overflow occurs. Such comparisons are not permitted in ++ * ANSI C90, however GCC isn't clever enough to work out that that do not occur ++ * below in png_ascii_from_fp and png_muldiv, so it produces a warning with ++ * -Wextra. Unfortunately this is highly dependent on the optimizer and the ++ * machine architecture so the warning comes and goes unpredictably and is ++ * impossible to "fix", even were that a good idea. ++ */ ++#if __GNUC__ == 7 && __GNUC_MINOR__ == 1 ++#define GCC_STRICT_OVERFLOW 1 ++#endif /* GNU 7.1.x */ ++#endif /* GNU */ ++#ifndef GCC_STRICT_OVERFLOW ++#define GCC_STRICT_OVERFLOW 0 ++#endif ++ ++/* Tells libpng that we have already handled the first "num_bytes" bytes ++ * of the PNG file signature. If the PNG data is embedded into another ++ * stream we can set num_bytes = 8 so that libpng will not attempt to read ++ * or write any of the magic bytes before it starts on the IHDR. ++ */ ++ ++#ifdef PNG_READ_SUPPORTED ++void PNGAPI ++png_set_sig_bytes(png_structrp png_ptr, int num_bytes) ++{ ++ unsigned int nb = (unsigned int)num_bytes; ++ ++ png_debug(1, "in png_set_sig_bytes"); ++ ++ if (png_ptr == NULL) ++ return; ++ ++ if (num_bytes < 0) ++ nb = 0; ++ ++ if (nb > 8) ++ png_error(png_ptr, "Too many bytes for PNG signature"); ++ ++ png_ptr->sig_bytes = (png_byte)nb; ++} ++ ++/* Checks whether the supplied bytes match the PNG signature. We allow ++ * checking less than the full 8-byte signature so that those apps that ++ * already read the first few bytes of a file to determine the file type ++ * can simply check the remaining bytes for extra assurance. Returns ++ * an integer less than, equal to, or greater than zero if sig is found, ++ * respectively, to be less than, to match, or be greater than the correct ++ * PNG signature (this is the same behavior as strcmp, memcmp, etc). ++ */ ++int PNGAPI ++png_sig_cmp(png_const_bytep sig, size_t start, size_t num_to_check) ++{ ++ png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10}; ++ ++ if (num_to_check > 8) ++ num_to_check = 8; ++ ++ else if (num_to_check < 1) ++ return (-1); ++ ++ if (start > 7) ++ return (-1); ++ ++ if (start + num_to_check > 8) ++ num_to_check = 8 - start; ++ ++ return ((int)(memcmp(&sig[start], &png_signature[start], num_to_check))); ++} ++ ++#endif /* READ */ ++ ++#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) ++/* Function to allocate memory for zlib */ ++PNG_FUNCTION(voidpf /* PRIVATE */, ++png_zalloc,(voidpf png_ptr, uInt items, uInt size),PNG_ALLOCATED) ++{ ++ png_alloc_size_t num_bytes = size; ++ ++ if (png_ptr == NULL) ++ return NULL; ++ ++ if (items >= (~(png_alloc_size_t)0)/size) ++ { ++ png_warning (png_voidcast(png_structrp, png_ptr), ++ "Potential overflow in png_zalloc()"); ++ return NULL; ++ } ++ ++ num_bytes *= items; ++ return png_malloc_warn(png_voidcast(png_structrp, png_ptr), num_bytes); ++} ++ ++/* Function to free memory for zlib */ ++void /* PRIVATE */ ++png_zfree(voidpf png_ptr, voidpf ptr) ++{ ++ png_free(png_voidcast(png_const_structrp,png_ptr), ptr); ++} ++ ++/* Reset the CRC variable to 32 bits of 1's. Care must be taken ++ * in case CRC is > 32 bits to leave the top bits 0. ++ */ ++void /* PRIVATE */ ++png_reset_crc(png_structrp png_ptr) ++{ ++ /* The cast is safe because the crc is a 32-bit value. */ ++ png_ptr->crc = (png_uint_32)crc32(0, Z_NULL, 0); ++} ++ ++/* Calculate the CRC over a section of data. We can only pass as ++ * much data to this routine as the largest single buffer size. We ++ * also check that this data will actually be used before going to the ++ * trouble of calculating it. ++ */ ++void /* PRIVATE */ ++png_calculate_crc(png_structrp png_ptr, png_const_bytep ptr, size_t length) ++{ ++ int need_crc = 1; ++ ++ if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) != 0) ++ { ++ if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) == ++ (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN)) ++ need_crc = 0; ++ } ++ ++ else /* critical */ ++ { ++ if ((png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) != 0) ++ need_crc = 0; ++ } ++ ++ /* 'uLong' is defined in zlib.h as unsigned long; this means that on some ++ * systems it is a 64-bit value. crc32, however, returns 32 bits so the ++ * following cast is safe. 'uInt' may be no more than 16 bits, so it is ++ * necessary to perform a loop here. ++ */ ++ if (need_crc != 0 && length > 0) ++ { ++ uLong crc = png_ptr->crc; /* Should never issue a warning */ ++ ++ do ++ { ++ uInt safe_length = (uInt)length; ++#ifndef __COVERITY__ ++ if (safe_length == 0) ++ safe_length = (uInt)-1; /* evil, but safe */ ++#endif ++ ++ crc = crc32(crc, ptr, safe_length); ++ ++ /* The following should never issue compiler warnings; if they do the ++ * target system has characteristics that will probably violate other ++ * assumptions within the libpng code. ++ */ ++ ptr += safe_length; ++ length -= safe_length; ++ } ++ while (length > 0); ++ ++ /* And the following is always safe because the crc is only 32 bits. */ ++ png_ptr->crc = (png_uint_32)crc; ++ } ++} ++ ++/* Check a user supplied version number, called from both read and write ++ * functions that create a png_struct. ++ */ ++int ++png_user_version_check(png_structrp png_ptr, png_const_charp user_png_ver) ++{ ++ /* Libpng versions 1.0.0 and later are binary compatible if the version ++ * string matches through the second '.'; we must recompile any ++ * applications that use any older library version. ++ */ ++ ++ if (user_png_ver != NULL) ++ { ++ int i = -1; ++ int found_dots = 0; ++ ++ do ++ { ++ i++; ++ if (user_png_ver[i] != PNG_LIBPNG_VER_STRING[i]) ++ png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH; ++ if (user_png_ver[i] == '.') ++ found_dots++; ++ } while (found_dots < 2 && user_png_ver[i] != 0 && ++ PNG_LIBPNG_VER_STRING[i] != 0); ++ } ++ ++ else ++ png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH; ++ ++ if ((png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH) != 0) ++ { ++#ifdef PNG_WARNINGS_SUPPORTED ++ size_t pos = 0; ++ char m[128]; ++ ++ pos = png_safecat(m, (sizeof m), pos, ++ "Application built with libpng-"); ++ pos = png_safecat(m, (sizeof m), pos, user_png_ver); ++ pos = png_safecat(m, (sizeof m), pos, " but running with "); ++ pos = png_safecat(m, (sizeof m), pos, PNG_LIBPNG_VER_STRING); ++ PNG_UNUSED(pos) ++ ++ png_warning(png_ptr, m); ++#endif ++ ++#ifdef PNG_ERROR_NUMBERS_SUPPORTED ++ png_ptr->flags = 0; ++#endif ++ ++ return 0; ++ } ++ ++ /* Success return. */ ++ return 1; ++} ++ ++/* Generic function to create a png_struct for either read or write - this ++ * contains the common initialization. ++ */ ++PNG_FUNCTION(png_structp /* PRIVATE */, ++png_create_png_struct,(png_const_charp user_png_ver, png_voidp error_ptr, ++ png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr, ++ png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED) ++{ ++ png_struct create_struct; ++# ifdef PNG_SETJMP_SUPPORTED ++ jmp_buf create_jmp_buf; ++# endif ++ ++ /* This temporary stack-allocated structure is used to provide a place to ++ * build enough context to allow the user provided memory allocator (if any) ++ * to be called. ++ */ ++ memset(&create_struct, 0, (sizeof create_struct)); ++ ++ /* Added at libpng-1.2.6 */ ++# ifdef PNG_USER_LIMITS_SUPPORTED ++ create_struct.user_width_max = PNG_USER_WIDTH_MAX; ++ create_struct.user_height_max = PNG_USER_HEIGHT_MAX; ++ ++# ifdef PNG_USER_CHUNK_CACHE_MAX ++ /* Added at libpng-1.2.43 and 1.4.0 */ ++ create_struct.user_chunk_cache_max = PNG_USER_CHUNK_CACHE_MAX; ++# endif ++ ++# ifdef PNG_USER_CHUNK_MALLOC_MAX ++ /* Added at libpng-1.2.43 and 1.4.1, required only for read but exists ++ * in png_struct regardless. ++ */ ++ create_struct.user_chunk_malloc_max = PNG_USER_CHUNK_MALLOC_MAX; ++# endif ++# endif ++ ++ /* The following two API calls simply set fields in png_struct, so it is safe ++ * to do them now even though error handling is not yet set up. ++ */ ++# ifdef PNG_USER_MEM_SUPPORTED ++ png_set_mem_fn(&create_struct, mem_ptr, malloc_fn, free_fn); ++# else ++ PNG_UNUSED(mem_ptr) ++ PNG_UNUSED(malloc_fn) ++ PNG_UNUSED(free_fn) ++# endif ++ ++ /* (*error_fn) can return control to the caller after the error_ptr is set, ++ * this will result in a memory leak unless the error_fn does something ++ * extremely sophisticated. The design lacks merit but is implicit in the ++ * API. ++ */ ++ png_set_error_fn(&create_struct, error_ptr, error_fn, warn_fn); ++ ++# ifdef PNG_SETJMP_SUPPORTED ++ if (!setjmp(create_jmp_buf)) ++# endif ++ { ++# ifdef PNG_SETJMP_SUPPORTED ++ /* Temporarily fake out the longjmp information until we have ++ * successfully completed this function. This only works if we have ++ * setjmp() support compiled in, but it is safe - this stuff should ++ * never happen. ++ */ ++ create_struct.jmp_buf_ptr = &create_jmp_buf; ++ create_struct.jmp_buf_size = 0; /*stack allocation*/ ++ create_struct.longjmp_fn = longjmp; ++# endif ++ /* Call the general version checker (shared with read and write code): ++ */ ++ if (png_user_version_check(&create_struct, user_png_ver) != 0) ++ { ++ png_structrp png_ptr = png_voidcast(png_structrp, ++ png_malloc_warn(&create_struct, (sizeof *png_ptr))); ++ ++ if (png_ptr != NULL) ++ { ++ /* png_ptr->zstream holds a back-pointer to the png_struct, so ++ * this can only be done now: ++ */ ++ create_struct.zstream.zalloc = png_zalloc; ++ create_struct.zstream.zfree = png_zfree; ++ create_struct.zstream.opaque = png_ptr; ++ ++# ifdef PNG_SETJMP_SUPPORTED ++ /* Eliminate the local error handling: */ ++ create_struct.jmp_buf_ptr = NULL; ++ create_struct.jmp_buf_size = 0; ++ create_struct.longjmp_fn = 0; ++# endif ++ ++ *png_ptr = create_struct; ++ ++ /* This is the successful return point */ ++ return png_ptr; ++ } ++ } ++ } ++ ++ /* A longjmp because of a bug in the application storage allocator or a ++ * simple failure to allocate the png_struct. ++ */ ++ return NULL; ++} ++ ++/* Allocate the memory for an info_struct for the application. */ ++PNG_FUNCTION(png_infop,PNGAPI ++png_create_info_struct,(png_const_structrp png_ptr),PNG_ALLOCATED) ++{ ++ png_inforp info_ptr; ++ ++ png_debug(1, "in png_create_info_struct"); ++ ++ if (png_ptr == NULL) ++ return NULL; ++ ++ /* Use the internal API that does not (or at least should not) error out, so ++ * that this call always returns ok. The application typically sets up the ++ * error handling *after* creating the info_struct because this is the way it ++ * has always been done in 'example.c'. ++ */ ++ info_ptr = png_voidcast(png_inforp, png_malloc_base(png_ptr, ++ (sizeof *info_ptr))); ++ ++ if (info_ptr != NULL) ++ memset(info_ptr, 0, (sizeof *info_ptr)); ++ ++ return info_ptr; ++} ++ ++/* This function frees the memory associated with a single info struct. ++ * Normally, one would use either png_destroy_read_struct() or ++ * png_destroy_write_struct() to free an info struct, but this may be ++ * useful for some applications. From libpng 1.6.0 this function is also used ++ * internally to implement the png_info release part of the 'struct' destroy ++ * APIs. This ensures that all possible approaches free the same data (all of ++ * it). ++ */ ++void PNGAPI ++png_destroy_info_struct(png_const_structrp png_ptr, png_infopp info_ptr_ptr) ++{ ++ png_inforp info_ptr = NULL; ++ ++ png_debug(1, "in png_destroy_info_struct"); ++ ++ if (png_ptr == NULL) ++ return; ++ ++ if (info_ptr_ptr != NULL) ++ info_ptr = *info_ptr_ptr; ++ ++ if (info_ptr != NULL) ++ { ++ /* Do this first in case of an error below; if the app implements its own ++ * memory management this can lead to png_free calling png_error, which ++ * will abort this routine and return control to the app error handler. ++ * An infinite loop may result if it then tries to free the same info ++ * ptr. ++ */ ++ *info_ptr_ptr = NULL; ++ ++ png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1); ++ memset(info_ptr, 0, (sizeof *info_ptr)); ++ png_free(png_ptr, info_ptr); ++ } ++} ++ ++/* Initialize the info structure. This is now an internal function (0.89) ++ * and applications using it are urged to use png_create_info_struct() ++ * instead. Use deprecated in 1.6.0, internal use removed (used internally it ++ * is just a memset). ++ * ++ * NOTE: it is almost inconceivable that this API is used because it bypasses ++ * the user-memory mechanism and the user error handling/warning mechanisms in ++ * those cases where it does anything other than a memset. ++ */ ++PNG_FUNCTION(void,PNGAPI ++png_info_init_3,(png_infopp ptr_ptr, size_t png_info_struct_size), ++ PNG_DEPRECATED) ++{ ++ png_inforp info_ptr = *ptr_ptr; ++ ++ png_debug(1, "in png_info_init_3"); ++ ++ if (info_ptr == NULL) ++ return; ++ ++ if ((sizeof (png_info)) > png_info_struct_size) ++ { ++ *ptr_ptr = NULL; ++ /* The following line is why this API should not be used: */ ++ free(info_ptr); ++ info_ptr = png_voidcast(png_inforp, png_malloc_base(NULL, ++ (sizeof *info_ptr))); ++ if (info_ptr == NULL) ++ return; ++ *ptr_ptr = info_ptr; ++ } ++ ++ /* Set everything to 0 */ ++ memset(info_ptr, 0, (sizeof *info_ptr)); ++} ++ ++/* The following API is not called internally */ ++void PNGAPI ++png_data_freer(png_const_structrp png_ptr, png_inforp info_ptr, ++ int freer, png_uint_32 mask) ++{ ++ png_debug(1, "in png_data_freer"); ++ ++ if (png_ptr == NULL || info_ptr == NULL) ++ return; ++ ++ if (freer == PNG_DESTROY_WILL_FREE_DATA) ++ info_ptr->free_me |= mask; ++ ++ else if (freer == PNG_USER_WILL_FREE_DATA) ++ info_ptr->free_me &= ~mask; ++ ++ else ++ png_error(png_ptr, "Unknown freer parameter in png_data_freer"); ++} ++ ++void PNGAPI ++png_free_data(png_const_structrp png_ptr, png_inforp info_ptr, png_uint_32 mask, ++ int num) ++{ ++ png_debug(1, "in png_free_data"); ++ ++ if (png_ptr == NULL || info_ptr == NULL) ++ return; ++ ++#ifdef PNG_TEXT_SUPPORTED ++ /* Free text item num or (if num == -1) all text items */ ++ if (info_ptr->text != NULL && ++ ((mask & PNG_FREE_TEXT) & info_ptr->free_me) != 0) ++ { ++ if (num != -1) ++ { ++ png_free(png_ptr, info_ptr->text[num].key); ++ info_ptr->text[num].key = NULL; ++ } ++ ++ else ++ { ++ int i; ++ ++ for (i = 0; i < info_ptr->num_text; i++) ++ png_free(png_ptr, info_ptr->text[i].key); ++ ++ png_free(png_ptr, info_ptr->text); ++ info_ptr->text = NULL; ++ info_ptr->num_text = 0; ++ info_ptr->max_text = 0; ++ } ++ } ++#endif ++ ++#ifdef PNG_tRNS_SUPPORTED ++ /* Free any tRNS entry */ ++ if (((mask & PNG_FREE_TRNS) & info_ptr->free_me) != 0) ++ { ++ info_ptr->valid &= ~PNG_INFO_tRNS; ++ png_free(png_ptr, info_ptr->trans_alpha); ++ info_ptr->trans_alpha = NULL; ++ info_ptr->num_trans = 0; ++ } ++#endif ++ ++#ifdef PNG_sCAL_SUPPORTED ++ /* Free any sCAL entry */ ++ if (((mask & PNG_FREE_SCAL) & info_ptr->free_me) != 0) ++ { ++ png_free(png_ptr, info_ptr->scal_s_width); ++ png_free(png_ptr, info_ptr->scal_s_height); ++ info_ptr->scal_s_width = NULL; ++ info_ptr->scal_s_height = NULL; ++ info_ptr->valid &= ~PNG_INFO_sCAL; ++ } ++#endif ++ ++#ifdef PNG_pCAL_SUPPORTED ++ /* Free any pCAL entry */ ++ if (((mask & PNG_FREE_PCAL) & info_ptr->free_me) != 0) ++ { ++ png_free(png_ptr, info_ptr->pcal_purpose); ++ png_free(png_ptr, info_ptr->pcal_units); ++ info_ptr->pcal_purpose = NULL; ++ info_ptr->pcal_units = NULL; ++ ++ if (info_ptr->pcal_params != NULL) ++ { ++ int i; ++ ++ for (i = 0; i < info_ptr->pcal_nparams; i++) ++ png_free(png_ptr, info_ptr->pcal_params[i]); ++ ++ png_free(png_ptr, info_ptr->pcal_params); ++ info_ptr->pcal_params = NULL; ++ } ++ info_ptr->valid &= ~PNG_INFO_pCAL; ++ } ++#endif ++ ++#ifdef PNG_iCCP_SUPPORTED ++ /* Free any profile entry */ ++ if (((mask & PNG_FREE_ICCP) & info_ptr->free_me) != 0) ++ { ++ png_free(png_ptr, info_ptr->iccp_name); ++ png_free(png_ptr, info_ptr->iccp_profile); ++ info_ptr->iccp_name = NULL; ++ info_ptr->iccp_profile = NULL; ++ info_ptr->valid &= ~PNG_INFO_iCCP; ++ } ++#endif ++ ++#ifdef PNG_sPLT_SUPPORTED ++ /* Free a given sPLT entry, or (if num == -1) all sPLT entries */ ++ if (info_ptr->splt_palettes != NULL && ++ ((mask & PNG_FREE_SPLT) & info_ptr->free_me) != 0) ++ { ++ if (num != -1) ++ { ++ png_free(png_ptr, info_ptr->splt_palettes[num].name); ++ png_free(png_ptr, info_ptr->splt_palettes[num].entries); ++ info_ptr->splt_palettes[num].name = NULL; ++ info_ptr->splt_palettes[num].entries = NULL; ++ } ++ ++ else ++ { ++ int i; ++ ++ for (i = 0; i < info_ptr->splt_palettes_num; i++) ++ { ++ png_free(png_ptr, info_ptr->splt_palettes[i].name); ++ png_free(png_ptr, info_ptr->splt_palettes[i].entries); ++ } ++ ++ png_free(png_ptr, info_ptr->splt_palettes); ++ info_ptr->splt_palettes = NULL; ++ info_ptr->splt_palettes_num = 0; ++ info_ptr->valid &= ~PNG_INFO_sPLT; ++ } ++ } ++#endif ++ ++#ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED ++ if (info_ptr->unknown_chunks != NULL && ++ ((mask & PNG_FREE_UNKN) & info_ptr->free_me) != 0) ++ { ++ if (num != -1) ++ { ++ png_free(png_ptr, info_ptr->unknown_chunks[num].data); ++ info_ptr->unknown_chunks[num].data = NULL; ++ } ++ ++ else ++ { ++ int i; ++ ++ for (i = 0; i < info_ptr->unknown_chunks_num; i++) ++ png_free(png_ptr, info_ptr->unknown_chunks[i].data); ++ ++ png_free(png_ptr, info_ptr->unknown_chunks); ++ info_ptr->unknown_chunks = NULL; ++ info_ptr->unknown_chunks_num = 0; ++ } ++ } ++#endif ++ ++#ifdef PNG_eXIf_SUPPORTED ++ /* Free any eXIf entry */ ++ if (((mask & PNG_FREE_EXIF) & info_ptr->free_me) != 0) ++ { ++# ifdef PNG_READ_eXIf_SUPPORTED ++ if (info_ptr->eXIf_buf) ++ { ++ png_free(png_ptr, info_ptr->eXIf_buf); ++ info_ptr->eXIf_buf = NULL; ++ } ++# endif ++ if (info_ptr->exif) ++ { ++ png_free(png_ptr, info_ptr->exif); ++ info_ptr->exif = NULL; ++ } ++ info_ptr->valid &= ~PNG_INFO_eXIf; ++ } ++#endif ++ ++#ifdef PNG_hIST_SUPPORTED ++ /* Free any hIST entry */ ++ if (((mask & PNG_FREE_HIST) & info_ptr->free_me) != 0) ++ { ++ png_free(png_ptr, info_ptr->hist); ++ info_ptr->hist = NULL; ++ info_ptr->valid &= ~PNG_INFO_hIST; ++ } ++#endif ++ ++ /* Free any PLTE entry that was internally allocated */ ++ if (((mask & PNG_FREE_PLTE) & info_ptr->free_me) != 0) ++ { ++ png_free(png_ptr, info_ptr->palette); ++ info_ptr->palette = NULL; ++ info_ptr->valid &= ~PNG_INFO_PLTE; ++ info_ptr->num_palette = 0; ++ } ++ ++#ifdef PNG_INFO_IMAGE_SUPPORTED ++ /* Free any image bits attached to the info structure */ ++ if (((mask & PNG_FREE_ROWS) & info_ptr->free_me) != 0) ++ { ++ if (info_ptr->row_pointers != NULL) ++ { ++ png_uint_32 row; ++ for (row = 0; row < info_ptr->height; row++) ++ png_free(png_ptr, info_ptr->row_pointers[row]); ++ ++ png_free(png_ptr, info_ptr->row_pointers); ++ info_ptr->row_pointers = NULL; ++ } ++ info_ptr->valid &= ~PNG_INFO_IDAT; ++ } ++#endif ++ ++ if (num != -1) ++ mask &= ~PNG_FREE_MUL; ++ ++ info_ptr->free_me &= ~mask; ++} ++#endif /* READ || WRITE */ ++ ++/* This function returns a pointer to the io_ptr associated with the user ++ * functions. The application should free any memory associated with this ++ * pointer before png_write_destroy() or png_read_destroy() are called. ++ */ ++png_voidp PNGAPI ++png_get_io_ptr(png_const_structrp png_ptr) ++{ ++ if (png_ptr == NULL) ++ return (NULL); ++ ++ return (png_ptr->io_ptr); ++} ++ ++#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) ++# ifdef PNG_STDIO_SUPPORTED ++/* Initialize the default input/output functions for the PNG file. If you ++ * use your own read or write routines, you can call either png_set_read_fn() ++ * or png_set_write_fn() instead of png_init_io(). If you have defined ++ * PNG_NO_STDIO or otherwise disabled PNG_STDIO_SUPPORTED, you must use a ++ * function of your own because "FILE *" isn't necessarily available. ++ */ ++void PNGAPI ++png_init_io(png_structrp png_ptr, png_FILE_p fp) ++{ ++ png_debug(1, "in png_init_io"); ++ ++ if (png_ptr == NULL) ++ return; ++ ++ png_ptr->io_ptr = (png_voidp)fp; ++} ++# endif ++ ++# ifdef PNG_SAVE_INT_32_SUPPORTED ++/* PNG signed integers are saved in 32-bit 2's complement format. ANSI C-90 ++ * defines a cast of a signed integer to an unsigned integer either to preserve ++ * the value, if it is positive, or to calculate: ++ * ++ * (UNSIGNED_MAX+1) + integer ++ * ++ * Where UNSIGNED_MAX is the appropriate maximum unsigned value, so when the ++ * negative integral value is added the result will be an unsigned value ++ * correspnding to the 2's complement representation. ++ */ ++void PNGAPI ++png_save_int_32(png_bytep buf, png_int_32 i) ++{ ++ png_save_uint_32(buf, (png_uint_32)i); ++} ++# endif ++ ++# ifdef PNG_TIME_RFC1123_SUPPORTED ++/* Convert the supplied time into an RFC 1123 string suitable for use in ++ * a "Creation Time" or other text-based time string. ++ */ ++int PNGAPI ++png_convert_to_rfc1123_buffer(char out[29], png_const_timep ptime) ++{ ++ static const char short_months[12][4] = ++ {"Jan", "Feb", "Mar", "Apr", "May", "Jun", ++ "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; ++ ++ if (out == NULL) ++ return 0; ++ ++ if (ptime->year > 9999 /* RFC1123 limitation */ || ++ ptime->month == 0 || ptime->month > 12 || ++ ptime->day == 0 || ptime->day > 31 || ++ ptime->hour > 23 || ptime->minute > 59 || ++ ptime->second > 60) ++ return 0; ++ ++ { ++ size_t pos = 0; ++ char number_buf[5]; /* enough for a four-digit year */ ++ ++# define APPEND_STRING(string) pos = png_safecat(out, 29, pos, (string)) ++# define APPEND_NUMBER(format, value)\ ++ APPEND_STRING(PNG_FORMAT_NUMBER(number_buf, format, (value))) ++# define APPEND(ch) if (pos < 28) out[pos++] = (ch) ++ ++ APPEND_NUMBER(PNG_NUMBER_FORMAT_u, (unsigned)ptime->day); ++ APPEND(' '); ++ APPEND_STRING(short_months[(ptime->month - 1)]); ++ APPEND(' '); ++ APPEND_NUMBER(PNG_NUMBER_FORMAT_u, ptime->year); ++ APPEND(' '); ++ APPEND_NUMBER(PNG_NUMBER_FORMAT_02u, (unsigned)ptime->hour); ++ APPEND(':'); ++ APPEND_NUMBER(PNG_NUMBER_FORMAT_02u, (unsigned)ptime->minute); ++ APPEND(':'); ++ APPEND_NUMBER(PNG_NUMBER_FORMAT_02u, (unsigned)ptime->second); ++ APPEND_STRING(" +0000"); /* This reliably terminates the buffer */ ++ PNG_UNUSED (pos) ++ ++# undef APPEND ++# undef APPEND_NUMBER ++# undef APPEND_STRING ++ } ++ ++ return 1; ++} ++ ++# if PNG_LIBPNG_VER < 10700 ++/* To do: remove the following from libpng-1.7 */ ++/* Original API that uses a private buffer in png_struct. ++ * Deprecated because it causes png_struct to carry a spurious temporary ++ * buffer (png_struct::time_buffer), better to have the caller pass this in. ++ */ ++png_const_charp PNGAPI ++png_convert_to_rfc1123(png_structrp png_ptr, png_const_timep ptime) ++{ ++ if (png_ptr != NULL) ++ { ++ /* The only failure above if png_ptr != NULL is from an invalid ptime */ ++ if (png_convert_to_rfc1123_buffer(png_ptr->time_buffer, ptime) == 0) ++ png_warning(png_ptr, "Ignoring invalid time value"); ++ ++ else ++ return png_ptr->time_buffer; ++ } ++ ++ return NULL; ++} ++# endif /* LIBPNG_VER < 10700 */ ++# endif /* TIME_RFC1123 */ ++ ++#endif /* READ || WRITE */ ++ ++png_const_charp PNGAPI ++png_get_copyright(png_const_structrp png_ptr) ++{ ++ PNG_UNUSED(png_ptr) /* Silence compiler warning about unused png_ptr */ ++#ifdef PNG_STRING_COPYRIGHT ++ return PNG_STRING_COPYRIGHT ++#else ++ return PNG_STRING_NEWLINE \ ++ "libpng version 1.6.37" PNG_STRING_NEWLINE \ ++ "Copyright (c) 2018-2019 Cosmin Truta" PNG_STRING_NEWLINE \ ++ "Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson" \ ++ PNG_STRING_NEWLINE \ ++ "Copyright (c) 1996-1997 Andreas Dilger" PNG_STRING_NEWLINE \ ++ "Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc." \ ++ PNG_STRING_NEWLINE; ++#endif ++} ++ ++/* The following return the library version as a short string in the ++ * format 1.0.0 through 99.99.99zz. To get the version of *.h files ++ * used with your application, print out PNG_LIBPNG_VER_STRING, which ++ * is defined in png.h. ++ * Note: now there is no difference between png_get_libpng_ver() and ++ * png_get_header_ver(). Due to the version_nn_nn_nn typedef guard, ++ * it is guaranteed that png.c uses the correct version of png.h. ++ */ ++png_const_charp PNGAPI ++png_get_libpng_ver(png_const_structrp png_ptr) ++{ ++ /* Version of *.c files used when building libpng */ ++ return png_get_header_ver(png_ptr); ++} ++ ++png_const_charp PNGAPI ++png_get_header_ver(png_const_structrp png_ptr) ++{ ++ /* Version of *.h files used when building libpng */ ++ PNG_UNUSED(png_ptr) /* Silence compiler warning about unused png_ptr */ ++ return PNG_LIBPNG_VER_STRING; ++} ++ ++png_const_charp PNGAPI ++png_get_header_version(png_const_structrp png_ptr) ++{ ++ /* Returns longer string containing both version and date */ ++ PNG_UNUSED(png_ptr) /* Silence compiler warning about unused png_ptr */ ++#ifdef __STDC__ ++ return PNG_HEADER_VERSION_STRING ++# ifndef PNG_READ_SUPPORTED ++ " (NO READ SUPPORT)" ++# endif ++ PNG_STRING_NEWLINE; ++#else ++ return PNG_HEADER_VERSION_STRING; ++#endif ++} ++ ++#ifdef PNG_BUILD_GRAYSCALE_PALETTE_SUPPORTED ++/* NOTE: this routine is not used internally! */ ++/* Build a grayscale palette. Palette is assumed to be 1 << bit_depth ++ * large of png_color. This lets grayscale images be treated as ++ * paletted. Most useful for gamma correction and simplification ++ * of code. This API is not used internally. ++ */ ++void PNGAPI ++png_build_grayscale_palette(int bit_depth, png_colorp palette) ++{ ++ int num_palette; ++ int color_inc; ++ int i; ++ int v; ++ ++ png_debug(1, "in png_do_build_grayscale_palette"); ++ ++ if (palette == NULL) ++ return; ++ ++ switch (bit_depth) ++ { ++ case 1: ++ num_palette = 2; ++ color_inc = 0xff; ++ break; ++ ++ case 2: ++ num_palette = 4; ++ color_inc = 0x55; ++ break; ++ ++ case 4: ++ num_palette = 16; ++ color_inc = 0x11; ++ break; ++ ++ case 8: ++ num_palette = 256; ++ color_inc = 1; ++ break; ++ ++ default: ++ num_palette = 0; ++ color_inc = 0; ++ break; ++ } ++ ++ for (i = 0, v = 0; i < num_palette; i++, v += color_inc) ++ { ++ palette[i].red = (png_byte)(v & 0xff); ++ palette[i].green = (png_byte)(v & 0xff); ++ palette[i].blue = (png_byte)(v & 0xff); ++ } ++} ++#endif ++ ++#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED ++int PNGAPI ++png_handle_as_unknown(png_const_structrp png_ptr, png_const_bytep chunk_name) ++{ ++ /* Check chunk_name and return "keep" value if it's on the list, else 0 */ ++ png_const_bytep p, p_end; ++ ++ if (png_ptr == NULL || chunk_name == NULL || png_ptr->num_chunk_list == 0) ++ return PNG_HANDLE_CHUNK_AS_DEFAULT; ++ ++ p_end = png_ptr->chunk_list; ++ p = p_end + png_ptr->num_chunk_list*5; /* beyond end */ ++ ++ /* The code is the fifth byte after each four byte string. Historically this ++ * code was always searched from the end of the list, this is no longer ++ * necessary because the 'set' routine handles duplicate entries correctly. ++ */ ++ do /* num_chunk_list > 0, so at least one */ ++ { ++ p -= 5; ++ ++ if (memcmp(chunk_name, p, 4) == 0) ++ return p[4]; ++ } ++ while (p > p_end); ++ ++ /* This means that known chunks should be processed and unknown chunks should ++ * be handled according to the value of png_ptr->unknown_default; this can be ++ * confusing because, as a result, there are two levels of defaulting for ++ * unknown chunks. ++ */ ++ return PNG_HANDLE_CHUNK_AS_DEFAULT; ++} ++ ++#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) ||\ ++ defined(PNG_HANDLE_AS_UNKNOWN_SUPPORTED) ++int /* PRIVATE */ ++png_chunk_unknown_handling(png_const_structrp png_ptr, png_uint_32 chunk_name) ++{ ++ png_byte chunk_string[5]; ++ ++ PNG_CSTRING_FROM_CHUNK(chunk_string, chunk_name); ++ return png_handle_as_unknown(png_ptr, chunk_string); ++} ++#endif /* READ_UNKNOWN_CHUNKS || HANDLE_AS_UNKNOWN */ ++#endif /* SET_UNKNOWN_CHUNKS */ ++ ++#ifdef PNG_READ_SUPPORTED ++/* This function, added to libpng-1.0.6g, is untested. */ ++int PNGAPI ++png_reset_zstream(png_structrp png_ptr) ++{ ++ if (png_ptr == NULL) ++ return Z_STREAM_ERROR; ++ ++ /* WARNING: this resets the window bits to the maximum! */ ++ return (inflateReset(&png_ptr->zstream)); ++} ++#endif /* READ */ ++ ++/* This function was added to libpng-1.0.7 */ ++png_uint_32 PNGAPI ++png_access_version_number(void) ++{ ++ /* Version of *.c files used when building libpng */ ++ return((png_uint_32)PNG_LIBPNG_VER); ++} ++ ++#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) ++/* Ensure that png_ptr->zstream.msg holds some appropriate error message string. ++ * If it doesn't 'ret' is used to set it to something appropriate, even in cases ++ * like Z_OK or Z_STREAM_END where the error code is apparently a success code. ++ */ ++void /* PRIVATE */ ++png_zstream_error(png_structrp png_ptr, int ret) ++{ ++ /* Translate 'ret' into an appropriate error string, priority is given to the ++ * one in zstream if set. This always returns a string, even in cases like ++ * Z_OK or Z_STREAM_END where the error code is a success code. ++ */ ++ if (png_ptr->zstream.msg == NULL) switch (ret) ++ { ++ default: ++ case Z_OK: ++ png_ptr->zstream.msg = PNGZ_MSG_CAST("unexpected zlib return code"); ++ break; ++ ++ case Z_STREAM_END: ++ /* Normal exit */ ++ png_ptr->zstream.msg = PNGZ_MSG_CAST("unexpected end of LZ stream"); ++ break; ++ ++ case Z_NEED_DICT: ++ /* This means the deflate stream did not have a dictionary; this ++ * indicates a bogus PNG. ++ */ ++ png_ptr->zstream.msg = PNGZ_MSG_CAST("missing LZ dictionary"); ++ break; ++ ++ case Z_ERRNO: ++ /* gz APIs only: should not happen */ ++ png_ptr->zstream.msg = PNGZ_MSG_CAST("zlib IO error"); ++ break; ++ ++ case Z_STREAM_ERROR: ++ /* internal libpng error */ ++ png_ptr->zstream.msg = PNGZ_MSG_CAST("bad parameters to zlib"); ++ break; ++ ++ case Z_DATA_ERROR: ++ png_ptr->zstream.msg = PNGZ_MSG_CAST("damaged LZ stream"); ++ break; ++ ++ case Z_MEM_ERROR: ++ png_ptr->zstream.msg = PNGZ_MSG_CAST("insufficient memory"); ++ break; ++ ++ case Z_BUF_ERROR: ++ /* End of input or output; not a problem if the caller is doing ++ * incremental read or write. ++ */ ++ png_ptr->zstream.msg = PNGZ_MSG_CAST("truncated"); ++ break; ++ ++ case Z_VERSION_ERROR: ++ png_ptr->zstream.msg = PNGZ_MSG_CAST("unsupported zlib version"); ++ break; ++ ++ case PNG_UNEXPECTED_ZLIB_RETURN: ++ /* Compile errors here mean that zlib now uses the value co-opted in ++ * pngpriv.h for PNG_UNEXPECTED_ZLIB_RETURN; update the switch above ++ * and change pngpriv.h. Note that this message is "... return", ++ * whereas the default/Z_OK one is "... return code". ++ */ ++ png_ptr->zstream.msg = PNGZ_MSG_CAST("unexpected zlib return"); ++ break; ++ } ++} ++ ++/* png_convert_size: a PNGAPI but no longer in png.h, so deleted ++ * at libpng 1.5.5! ++ */ ++ ++/* Added at libpng version 1.2.34 and 1.4.0 (moved from pngset.c) */ ++#ifdef PNG_GAMMA_SUPPORTED /* always set if COLORSPACE */ ++static int ++png_colorspace_check_gamma(png_const_structrp png_ptr, ++ png_colorspacerp colorspace, png_fixed_point gAMA, int from) ++ /* This is called to check a new gamma value against an existing one. The ++ * routine returns false if the new gamma value should not be written. ++ * ++ * 'from' says where the new gamma value comes from: ++ * ++ * 0: the new gamma value is the libpng estimate for an ICC profile ++ * 1: the new gamma value comes from a gAMA chunk ++ * 2: the new gamma value comes from an sRGB chunk ++ */ ++{ ++ png_fixed_point gtest; ++ ++ if ((colorspace->flags & PNG_COLORSPACE_HAVE_GAMMA) != 0 && ++ (png_muldiv(>est, colorspace->gamma, PNG_FP_1, gAMA) == 0 || ++ png_gamma_significant(gtest) != 0)) ++ { ++ /* Either this is an sRGB image, in which case the calculated gamma ++ * approximation should match, or this is an image with a profile and the ++ * value libpng calculates for the gamma of the profile does not match the ++ * value recorded in the file. The former, sRGB, case is an error, the ++ * latter is just a warning. ++ */ ++ if ((colorspace->flags & PNG_COLORSPACE_FROM_sRGB) != 0 || from == 2) ++ { ++ png_chunk_report(png_ptr, "gamma value does not match sRGB", ++ PNG_CHUNK_ERROR); ++ /* Do not overwrite an sRGB value */ ++ return from == 2; ++ } ++ ++ else /* sRGB tag not involved */ ++ { ++ png_chunk_report(png_ptr, "gamma value does not match libpng estimate", ++ PNG_CHUNK_WARNING); ++ return from == 1; ++ } ++ } ++ ++ return 1; ++} ++ ++void /* PRIVATE */ ++png_colorspace_set_gamma(png_const_structrp png_ptr, ++ png_colorspacerp colorspace, png_fixed_point gAMA) ++{ ++ /* Changed in libpng-1.5.4 to limit the values to ensure overflow can't ++ * occur. Since the fixed point representation is asymmetrical it is ++ * possible for 1/gamma to overflow the limit of 21474 and this means the ++ * gamma value must be at least 5/100000 and hence at most 20000.0. For ++ * safety the limits here are a little narrower. The values are 0.00016 to ++ * 6250.0, which are truly ridiculous gamma values (and will produce ++ * displays that are all black or all white.) ++ * ++ * In 1.6.0 this test replaces the ones in pngrutil.c, in the gAMA chunk ++ * handling code, which only required the value to be >0. ++ */ ++ png_const_charp errmsg; ++ ++ if (gAMA < 16 || gAMA > 625000000) ++ errmsg = "gamma value out of range"; ++ ++# ifdef PNG_READ_gAMA_SUPPORTED ++ /* Allow the application to set the gamma value more than once */ ++ else if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0 && ++ (colorspace->flags & PNG_COLORSPACE_FROM_gAMA) != 0) ++ errmsg = "duplicate"; ++# endif ++ ++ /* Do nothing if the colorspace is already invalid */ ++ else if ((colorspace->flags & PNG_COLORSPACE_INVALID) != 0) ++ return; ++ ++ else ++ { ++ if (png_colorspace_check_gamma(png_ptr, colorspace, gAMA, ++ 1/*from gAMA*/) != 0) ++ { ++ /* Store this gamma value. */ ++ colorspace->gamma = gAMA; ++ colorspace->flags |= ++ (PNG_COLORSPACE_HAVE_GAMMA | PNG_COLORSPACE_FROM_gAMA); ++ } ++ ++ /* At present if the check_gamma test fails the gamma of the colorspace is ++ * not updated however the colorspace is not invalidated. This ++ * corresponds to the case where the existing gamma comes from an sRGB ++ * chunk or profile. An error message has already been output. ++ */ ++ return; ++ } ++ ++ /* Error exit - errmsg has been set. */ ++ colorspace->flags |= PNG_COLORSPACE_INVALID; ++ png_chunk_report(png_ptr, errmsg, PNG_CHUNK_WRITE_ERROR); ++} ++ ++void /* PRIVATE */ ++png_colorspace_sync_info(png_const_structrp png_ptr, png_inforp info_ptr) ++{ ++ if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0) ++ { ++ /* Everything is invalid */ ++ info_ptr->valid &= ~(PNG_INFO_gAMA|PNG_INFO_cHRM|PNG_INFO_sRGB| ++ PNG_INFO_iCCP); ++ ++# ifdef PNG_COLORSPACE_SUPPORTED ++ /* Clean up the iCCP profile now if it won't be used. */ ++ png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, -1/*not used*/); ++# else ++ PNG_UNUSED(png_ptr) ++# endif ++ } ++ ++ else ++ { ++# ifdef PNG_COLORSPACE_SUPPORTED ++ /* Leave the INFO_iCCP flag set if the pngset.c code has already set ++ * it; this allows a PNG to contain a profile which matches sRGB and ++ * yet still have that profile retrievable by the application. ++ */ ++ if ((info_ptr->colorspace.flags & PNG_COLORSPACE_MATCHES_sRGB) != 0) ++ info_ptr->valid |= PNG_INFO_sRGB; ++ ++ else ++ info_ptr->valid &= ~PNG_INFO_sRGB; ++ ++ if ((info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_ENDPOINTS) != 0) ++ info_ptr->valid |= PNG_INFO_cHRM; ++ ++ else ++ info_ptr->valid &= ~PNG_INFO_cHRM; ++# endif ++ ++ if ((info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_GAMMA) != 0) ++ info_ptr->valid |= PNG_INFO_gAMA; ++ ++ else ++ info_ptr->valid &= ~PNG_INFO_gAMA; ++ } ++} ++ ++#ifdef PNG_READ_SUPPORTED ++void /* PRIVATE */ ++png_colorspace_sync(png_const_structrp png_ptr, png_inforp info_ptr) ++{ ++ if (info_ptr == NULL) /* reduce code size; check here not in the caller */ ++ return; ++ ++ info_ptr->colorspace = png_ptr->colorspace; ++ png_colorspace_sync_info(png_ptr, info_ptr); ++} ++#endif ++#endif /* GAMMA */ ++ ++#ifdef PNG_COLORSPACE_SUPPORTED ++/* Added at libpng-1.5.5 to support read and write of true CIEXYZ values for ++ * cHRM, as opposed to using chromaticities. These internal APIs return ++ * non-zero on a parameter error. The X, Y and Z values are required to be ++ * positive and less than 1.0. ++ */ ++static int ++png_xy_from_XYZ(png_xy *xy, const png_XYZ *XYZ) ++{ ++ png_int_32 d, dwhite, whiteX, whiteY; ++ ++ d = XYZ->red_X + XYZ->red_Y + XYZ->red_Z; ++ if (png_muldiv(&xy->redx, XYZ->red_X, PNG_FP_1, d) == 0) ++ return 1; ++ if (png_muldiv(&xy->redy, XYZ->red_Y, PNG_FP_1, d) == 0) ++ return 1; ++ dwhite = d; ++ whiteX = XYZ->red_X; ++ whiteY = XYZ->red_Y; ++ ++ d = XYZ->green_X + XYZ->green_Y + XYZ->green_Z; ++ if (png_muldiv(&xy->greenx, XYZ->green_X, PNG_FP_1, d) == 0) ++ return 1; ++ if (png_muldiv(&xy->greeny, XYZ->green_Y, PNG_FP_1, d) == 0) ++ return 1; ++ dwhite += d; ++ whiteX += XYZ->green_X; ++ whiteY += XYZ->green_Y; ++ ++ d = XYZ->blue_X + XYZ->blue_Y + XYZ->blue_Z; ++ if (png_muldiv(&xy->bluex, XYZ->blue_X, PNG_FP_1, d) == 0) ++ return 1; ++ if (png_muldiv(&xy->bluey, XYZ->blue_Y, PNG_FP_1, d) == 0) ++ return 1; ++ dwhite += d; ++ whiteX += XYZ->blue_X; ++ whiteY += XYZ->blue_Y; ++ ++ /* The reference white is simply the sum of the end-point (X,Y,Z) vectors, ++ * thus: ++ */ ++ if (png_muldiv(&xy->whitex, whiteX, PNG_FP_1, dwhite) == 0) ++ return 1; ++ if (png_muldiv(&xy->whitey, whiteY, PNG_FP_1, dwhite) == 0) ++ return 1; ++ ++ return 0; ++} ++ ++static int ++png_XYZ_from_xy(png_XYZ *XYZ, const png_xy *xy) ++{ ++ png_fixed_point red_inverse, green_inverse, blue_scale; ++ png_fixed_point left, right, denominator; ++ ++ /* Check xy and, implicitly, z. Note that wide gamut color spaces typically ++ * have end points with 0 tristimulus values (these are impossible end ++ * points, but they are used to cover the possible colors). We check ++ * xy->whitey against 5, not 0, to avoid a possible integer overflow. ++ */ ++ if (xy->redx < 0 || xy->redx > PNG_FP_1) return 1; ++ if (xy->redy < 0 || xy->redy > PNG_FP_1-xy->redx) return 1; ++ if (xy->greenx < 0 || xy->greenx > PNG_FP_1) return 1; ++ if (xy->greeny < 0 || xy->greeny > PNG_FP_1-xy->greenx) return 1; ++ if (xy->bluex < 0 || xy->bluex > PNG_FP_1) return 1; ++ if (xy->bluey < 0 || xy->bluey > PNG_FP_1-xy->bluex) return 1; ++ if (xy->whitex < 0 || xy->whitex > PNG_FP_1) return 1; ++ if (xy->whitey < 5 || xy->whitey > PNG_FP_1-xy->whitex) return 1; ++ ++ /* The reverse calculation is more difficult because the original tristimulus ++ * value had 9 independent values (red,green,blue)x(X,Y,Z) however only 8 ++ * derived values were recorded in the cHRM chunk; ++ * (red,green,blue,white)x(x,y). This loses one degree of freedom and ++ * therefore an arbitrary ninth value has to be introduced to undo the ++ * original transformations. ++ * ++ * Think of the original end-points as points in (X,Y,Z) space. The ++ * chromaticity values (c) have the property: ++ * ++ * C ++ * c = --------- ++ * X + Y + Z ++ * ++ * For each c (x,y,z) from the corresponding original C (X,Y,Z). Thus the ++ * three chromaticity values (x,y,z) for each end-point obey the ++ * relationship: ++ * ++ * x + y + z = 1 ++ * ++ * This describes the plane in (X,Y,Z) space that intersects each axis at the ++ * value 1.0; call this the chromaticity plane. Thus the chromaticity ++ * calculation has scaled each end-point so that it is on the x+y+z=1 plane ++ * and chromaticity is the intersection of the vector from the origin to the ++ * (X,Y,Z) value with the chromaticity plane. ++ * ++ * To fully invert the chromaticity calculation we would need the three ++ * end-point scale factors, (red-scale, green-scale, blue-scale), but these ++ * were not recorded. Instead we calculated the reference white (X,Y,Z) and ++ * recorded the chromaticity of this. The reference white (X,Y,Z) would have ++ * given all three of the scale factors since: ++ * ++ * color-C = color-c * color-scale ++ * white-C = red-C + green-C + blue-C ++ * = red-c*red-scale + green-c*green-scale + blue-c*blue-scale ++ * ++ * But cHRM records only white-x and white-y, so we have lost the white scale ++ * factor: ++ * ++ * white-C = white-c*white-scale ++ * ++ * To handle this the inverse transformation makes an arbitrary assumption ++ * about white-scale: ++ * ++ * Assume: white-Y = 1.0 ++ * Hence: white-scale = 1/white-y ++ * Or: red-Y + green-Y + blue-Y = 1.0 ++ * ++ * Notice the last statement of the assumption gives an equation in three of ++ * the nine values we want to calculate. 8 more equations come from the ++ * above routine as summarised at the top above (the chromaticity ++ * calculation): ++ * ++ * Given: color-x = color-X / (color-X + color-Y + color-Z) ++ * Hence: (color-x - 1)*color-X + color.x*color-Y + color.x*color-Z = 0 ++ * ++ * This is 9 simultaneous equations in the 9 variables "color-C" and can be ++ * solved by Cramer's rule. Cramer's rule requires calculating 10 9x9 matrix ++ * determinants, however this is not as bad as it seems because only 28 of ++ * the total of 90 terms in the various matrices are non-zero. Nevertheless ++ * Cramer's rule is notoriously numerically unstable because the determinant ++ * calculation involves the difference of large, but similar, numbers. It is ++ * difficult to be sure that the calculation is stable for real world values ++ * and it is certain that it becomes unstable where the end points are close ++ * together. ++ * ++ * So this code uses the perhaps slightly less optimal but more ++ * understandable and totally obvious approach of calculating color-scale. ++ * ++ * This algorithm depends on the precision in white-scale and that is ++ * (1/white-y), so we can immediately see that as white-y approaches 0 the ++ * accuracy inherent in the cHRM chunk drops off substantially. ++ * ++ * libpng arithmetic: a simple inversion of the above equations ++ * ------------------------------------------------------------ ++ * ++ * white_scale = 1/white-y ++ * white-X = white-x * white-scale ++ * white-Y = 1.0 ++ * white-Z = (1 - white-x - white-y) * white_scale ++ * ++ * white-C = red-C + green-C + blue-C ++ * = red-c*red-scale + green-c*green-scale + blue-c*blue-scale ++ * ++ * This gives us three equations in (red-scale,green-scale,blue-scale) where ++ * all the coefficients are now known: ++ * ++ * red-x*red-scale + green-x*green-scale + blue-x*blue-scale ++ * = white-x/white-y ++ * red-y*red-scale + green-y*green-scale + blue-y*blue-scale = 1 ++ * red-z*red-scale + green-z*green-scale + blue-z*blue-scale ++ * = (1 - white-x - white-y)/white-y ++ * ++ * In the last equation color-z is (1 - color-x - color-y) so we can add all ++ * three equations together to get an alternative third: ++ * ++ * red-scale + green-scale + blue-scale = 1/white-y = white-scale ++ * ++ * So now we have a Cramer's rule solution where the determinants are just ++ * 3x3 - far more tractible. Unfortunately 3x3 determinants still involve ++ * multiplication of three coefficients so we can't guarantee to avoid ++ * overflow in the libpng fixed point representation. Using Cramer's rule in ++ * floating point is probably a good choice here, but it's not an option for ++ * fixed point. Instead proceed to simplify the first two equations by ++ * eliminating what is likely to be the largest value, blue-scale: ++ * ++ * blue-scale = white-scale - red-scale - green-scale ++ * ++ * Hence: ++ * ++ * (red-x - blue-x)*red-scale + (green-x - blue-x)*green-scale = ++ * (white-x - blue-x)*white-scale ++ * ++ * (red-y - blue-y)*red-scale + (green-y - blue-y)*green-scale = ++ * 1 - blue-y*white-scale ++ * ++ * And now we can trivially solve for (red-scale,green-scale): ++ * ++ * green-scale = ++ * (white-x - blue-x)*white-scale - (red-x - blue-x)*red-scale ++ * ----------------------------------------------------------- ++ * green-x - blue-x ++ * ++ * red-scale = ++ * 1 - blue-y*white-scale - (green-y - blue-y) * green-scale ++ * --------------------------------------------------------- ++ * red-y - blue-y ++ * ++ * Hence: ++ * ++ * red-scale = ++ * ( (green-x - blue-x) * (white-y - blue-y) - ++ * (green-y - blue-y) * (white-x - blue-x) ) / white-y ++ * ------------------------------------------------------------------------- ++ * (green-x - blue-x)*(red-y - blue-y)-(green-y - blue-y)*(red-x - blue-x) ++ * ++ * green-scale = ++ * ( (red-y - blue-y) * (white-x - blue-x) - ++ * (red-x - blue-x) * (white-y - blue-y) ) / white-y ++ * ------------------------------------------------------------------------- ++ * (green-x - blue-x)*(red-y - blue-y)-(green-y - blue-y)*(red-x - blue-x) ++ * ++ * Accuracy: ++ * The input values have 5 decimal digits of accuracy. The values are all in ++ * the range 0 < value < 1, so simple products are in the same range but may ++ * need up to 10 decimal digits to preserve the original precision and avoid ++ * underflow. Because we are using a 32-bit signed representation we cannot ++ * match this; the best is a little over 9 decimal digits, less than 10. ++ * ++ * The approach used here is to preserve the maximum precision within the ++ * signed representation. Because the red-scale calculation above uses the ++ * difference between two products of values that must be in the range -1..+1 ++ * it is sufficient to divide the product by 7; ceil(100,000/32767*2). The ++ * factor is irrelevant in the calculation because it is applied to both ++ * numerator and denominator. ++ * ++ * Note that the values of the differences of the products of the ++ * chromaticities in the above equations tend to be small, for example for ++ * the sRGB chromaticities they are: ++ * ++ * red numerator: -0.04751 ++ * green numerator: -0.08788 ++ * denominator: -0.2241 (without white-y multiplication) ++ * ++ * The resultant Y coefficients from the chromaticities of some widely used ++ * color space definitions are (to 15 decimal places): ++ * ++ * sRGB ++ * 0.212639005871510 0.715168678767756 0.072192315360734 ++ * Kodak ProPhoto ++ * 0.288071128229293 0.711843217810102 0.000085653960605 ++ * Adobe RGB ++ * 0.297344975250536 0.627363566255466 0.075291458493998 ++ * Adobe Wide Gamut RGB ++ * 0.258728243040113 0.724682314948566 0.016589442011321 ++ */ ++ /* By the argument, above overflow should be impossible here. The return ++ * value of 2 indicates an internal error to the caller. ++ */ ++ if (png_muldiv(&left, xy->greenx-xy->bluex, xy->redy - xy->bluey, 7) == 0) ++ return 2; ++ if (png_muldiv(&right, xy->greeny-xy->bluey, xy->redx - xy->bluex, 7) == 0) ++ return 2; ++ denominator = left - right; ++ ++ /* Now find the red numerator. */ ++ if (png_muldiv(&left, xy->greenx-xy->bluex, xy->whitey-xy->bluey, 7) == 0) ++ return 2; ++ if (png_muldiv(&right, xy->greeny-xy->bluey, xy->whitex-xy->bluex, 7) == 0) ++ return 2; ++ ++ /* Overflow is possible here and it indicates an extreme set of PNG cHRM ++ * chunk values. This calculation actually returns the reciprocal of the ++ * scale value because this allows us to delay the multiplication of white-y ++ * into the denominator, which tends to produce a small number. ++ */ ++ if (png_muldiv(&red_inverse, xy->whitey, denominator, left-right) == 0 || ++ red_inverse <= xy->whitey /* r+g+b scales = white scale */) ++ return 1; ++ ++ /* Similarly for green_inverse: */ ++ if (png_muldiv(&left, xy->redy-xy->bluey, xy->whitex-xy->bluex, 7) == 0) ++ return 2; ++ if (png_muldiv(&right, xy->redx-xy->bluex, xy->whitey-xy->bluey, 7) == 0) ++ return 2; ++ if (png_muldiv(&green_inverse, xy->whitey, denominator, left-right) == 0 || ++ green_inverse <= xy->whitey) ++ return 1; ++ ++ /* And the blue scale, the checks above guarantee this can't overflow but it ++ * can still produce 0 for extreme cHRM values. ++ */ ++ blue_scale = png_reciprocal(xy->whitey) - png_reciprocal(red_inverse) - ++ png_reciprocal(green_inverse); ++ if (blue_scale <= 0) ++ return 1; ++ ++ ++ /* And fill in the png_XYZ: */ ++ if (png_muldiv(&XYZ->red_X, xy->redx, PNG_FP_1, red_inverse) == 0) ++ return 1; ++ if (png_muldiv(&XYZ->red_Y, xy->redy, PNG_FP_1, red_inverse) == 0) ++ return 1; ++ if (png_muldiv(&XYZ->red_Z, PNG_FP_1 - xy->redx - xy->redy, PNG_FP_1, ++ red_inverse) == 0) ++ return 1; ++ ++ if (png_muldiv(&XYZ->green_X, xy->greenx, PNG_FP_1, green_inverse) == 0) ++ return 1; ++ if (png_muldiv(&XYZ->green_Y, xy->greeny, PNG_FP_1, green_inverse) == 0) ++ return 1; ++ if (png_muldiv(&XYZ->green_Z, PNG_FP_1 - xy->greenx - xy->greeny, PNG_FP_1, ++ green_inverse) == 0) ++ return 1; ++ ++ if (png_muldiv(&XYZ->blue_X, xy->bluex, blue_scale, PNG_FP_1) == 0) ++ return 1; ++ if (png_muldiv(&XYZ->blue_Y, xy->bluey, blue_scale, PNG_FP_1) == 0) ++ return 1; ++ if (png_muldiv(&XYZ->blue_Z, PNG_FP_1 - xy->bluex - xy->bluey, blue_scale, ++ PNG_FP_1) == 0) ++ return 1; ++ ++ return 0; /*success*/ ++} ++ ++static int ++png_XYZ_normalize(png_XYZ *XYZ) ++{ ++ png_int_32 Y; ++ ++ if (XYZ->red_Y < 0 || XYZ->green_Y < 0 || XYZ->blue_Y < 0 || ++ XYZ->red_X < 0 || XYZ->green_X < 0 || XYZ->blue_X < 0 || ++ XYZ->red_Z < 0 || XYZ->green_Z < 0 || XYZ->blue_Z < 0) ++ return 1; ++ ++ /* Normalize by scaling so the sum of the end-point Y values is PNG_FP_1. ++ * IMPLEMENTATION NOTE: ANSI requires signed overflow not to occur, therefore ++ * relying on addition of two positive values producing a negative one is not ++ * safe. ++ */ ++ Y = XYZ->red_Y; ++ if (0x7fffffff - Y < XYZ->green_X) ++ return 1; ++ Y += XYZ->green_Y; ++ if (0x7fffffff - Y < XYZ->blue_X) ++ return 1; ++ Y += XYZ->blue_Y; ++ ++ if (Y != PNG_FP_1) ++ { ++ if (png_muldiv(&XYZ->red_X, XYZ->red_X, PNG_FP_1, Y) == 0) ++ return 1; ++ if (png_muldiv(&XYZ->red_Y, XYZ->red_Y, PNG_FP_1, Y) == 0) ++ return 1; ++ if (png_muldiv(&XYZ->red_Z, XYZ->red_Z, PNG_FP_1, Y) == 0) ++ return 1; ++ ++ if (png_muldiv(&XYZ->green_X, XYZ->green_X, PNG_FP_1, Y) == 0) ++ return 1; ++ if (png_muldiv(&XYZ->green_Y, XYZ->green_Y, PNG_FP_1, Y) == 0) ++ return 1; ++ if (png_muldiv(&XYZ->green_Z, XYZ->green_Z, PNG_FP_1, Y) == 0) ++ return 1; ++ ++ if (png_muldiv(&XYZ->blue_X, XYZ->blue_X, PNG_FP_1, Y) == 0) ++ return 1; ++ if (png_muldiv(&XYZ->blue_Y, XYZ->blue_Y, PNG_FP_1, Y) == 0) ++ return 1; ++ if (png_muldiv(&XYZ->blue_Z, XYZ->blue_Z, PNG_FP_1, Y) == 0) ++ return 1; ++ } ++ ++ return 0; ++} ++ ++static int ++png_colorspace_endpoints_match(const png_xy *xy1, const png_xy *xy2, int delta) ++{ ++ /* Allow an error of +/-0.01 (absolute value) on each chromaticity */ ++ if (PNG_OUT_OF_RANGE(xy1->whitex, xy2->whitex,delta) || ++ PNG_OUT_OF_RANGE(xy1->whitey, xy2->whitey,delta) || ++ PNG_OUT_OF_RANGE(xy1->redx, xy2->redx, delta) || ++ PNG_OUT_OF_RANGE(xy1->redy, xy2->redy, delta) || ++ PNG_OUT_OF_RANGE(xy1->greenx, xy2->greenx,delta) || ++ PNG_OUT_OF_RANGE(xy1->greeny, xy2->greeny,delta) || ++ PNG_OUT_OF_RANGE(xy1->bluex, xy2->bluex, delta) || ++ PNG_OUT_OF_RANGE(xy1->bluey, xy2->bluey, delta)) ++ return 0; ++ return 1; ++} ++ ++/* Added in libpng-1.6.0, a different check for the validity of a set of cHRM ++ * chunk chromaticities. Earlier checks used to simply look for the overflow ++ * condition (where the determinant of the matrix to solve for XYZ ends up zero ++ * because the chromaticity values are not all distinct.) Despite this it is ++ * theoretically possible to produce chromaticities that are apparently valid ++ * but that rapidly degrade to invalid, potentially crashing, sets because of ++ * arithmetic inaccuracies when calculations are performed on them. The new ++ * check is to round-trip xy -> XYZ -> xy and then check that the result is ++ * within a small percentage of the original. ++ */ ++static int ++png_colorspace_check_xy(png_XYZ *XYZ, const png_xy *xy) ++{ ++ int result; ++ png_xy xy_test; ++ ++ /* As a side-effect this routine also returns the XYZ endpoints. */ ++ result = png_XYZ_from_xy(XYZ, xy); ++ if (result != 0) ++ return result; ++ ++ result = png_xy_from_XYZ(&xy_test, XYZ); ++ if (result != 0) ++ return result; ++ ++ if (png_colorspace_endpoints_match(xy, &xy_test, ++ 5/*actually, the math is pretty accurate*/) != 0) ++ return 0; ++ ++ /* Too much slip */ ++ return 1; ++} ++ ++/* This is the check going the other way. The XYZ is modified to normalize it ++ * (another side-effect) and the xy chromaticities are returned. ++ */ ++static int ++png_colorspace_check_XYZ(png_xy *xy, png_XYZ *XYZ) ++{ ++ int result; ++ png_XYZ XYZtemp; ++ ++ result = png_XYZ_normalize(XYZ); ++ if (result != 0) ++ return result; ++ ++ result = png_xy_from_XYZ(xy, XYZ); ++ if (result != 0) ++ return result; ++ ++ XYZtemp = *XYZ; ++ return png_colorspace_check_xy(&XYZtemp, xy); ++} ++ ++/* Used to check for an endpoint match against sRGB */ ++static const png_xy sRGB_xy = /* From ITU-R BT.709-3 */ ++{ ++ /* color x y */ ++ /* red */ 64000, 33000, ++ /* green */ 30000, 60000, ++ /* blue */ 15000, 6000, ++ /* white */ 31270, 32900 ++}; ++ ++static int ++png_colorspace_set_xy_and_XYZ(png_const_structrp png_ptr, ++ png_colorspacerp colorspace, const png_xy *xy, const png_XYZ *XYZ, ++ int preferred) ++{ ++ if ((colorspace->flags & PNG_COLORSPACE_INVALID) != 0) ++ return 0; ++ ++ /* The consistency check is performed on the chromaticities; this factors out ++ * variations because of the normalization (or not) of the end point Y ++ * values. ++ */ ++ if (preferred < 2 && ++ (colorspace->flags & PNG_COLORSPACE_HAVE_ENDPOINTS) != 0) ++ { ++ /* The end points must be reasonably close to any we already have. The ++ * following allows an error of up to +/-.001 ++ */ ++ if (png_colorspace_endpoints_match(xy, &colorspace->end_points_xy, ++ 100) == 0) ++ { ++ colorspace->flags |= PNG_COLORSPACE_INVALID; ++ png_benign_error(png_ptr, "inconsistent chromaticities"); ++ return 0; /* failed */ ++ } ++ ++ /* Only overwrite with preferred values */ ++ if (preferred == 0) ++ return 1; /* ok, but no change */ ++ } ++ ++ colorspace->end_points_xy = *xy; ++ colorspace->end_points_XYZ = *XYZ; ++ colorspace->flags |= PNG_COLORSPACE_HAVE_ENDPOINTS; ++ ++ /* The end points are normally quoted to two decimal digits, so allow +/-0.01 ++ * on this test. ++ */ ++ if (png_colorspace_endpoints_match(xy, &sRGB_xy, 1000) != 0) ++ colorspace->flags |= PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB; ++ ++ else ++ colorspace->flags &= PNG_COLORSPACE_CANCEL( ++ PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB); ++ ++ return 2; /* ok and changed */ ++} ++ ++int /* PRIVATE */ ++png_colorspace_set_chromaticities(png_const_structrp png_ptr, ++ png_colorspacerp colorspace, const png_xy *xy, int preferred) ++{ ++ /* We must check the end points to ensure they are reasonable - in the past ++ * color management systems have crashed as a result of getting bogus ++ * colorant values, while this isn't the fault of libpng it is the ++ * responsibility of libpng because PNG carries the bomb and libpng is in a ++ * position to protect against it. ++ */ ++ png_XYZ XYZ; ++ ++ switch (png_colorspace_check_xy(&XYZ, xy)) ++ { ++ case 0: /* success */ ++ return png_colorspace_set_xy_and_XYZ(png_ptr, colorspace, xy, &XYZ, ++ preferred); ++ ++ case 1: ++ /* We can't invert the chromaticities so we can't produce value XYZ ++ * values. Likely as not a color management system will fail too. ++ */ ++ colorspace->flags |= PNG_COLORSPACE_INVALID; ++ png_benign_error(png_ptr, "invalid chromaticities"); ++ break; ++ ++ default: ++ /* libpng is broken; this should be a warning but if it happens we ++ * want error reports so for the moment it is an error. ++ */ ++ colorspace->flags |= PNG_COLORSPACE_INVALID; ++ png_error(png_ptr, "internal error checking chromaticities"); ++ } ++ ++ return 0; /* failed */ ++} ++ ++int /* PRIVATE */ ++png_colorspace_set_endpoints(png_const_structrp png_ptr, ++ png_colorspacerp colorspace, const png_XYZ *XYZ_in, int preferred) ++{ ++ png_XYZ XYZ = *XYZ_in; ++ png_xy xy; ++ ++ switch (png_colorspace_check_XYZ(&xy, &XYZ)) ++ { ++ case 0: ++ return png_colorspace_set_xy_and_XYZ(png_ptr, colorspace, &xy, &XYZ, ++ preferred); ++ ++ case 1: ++ /* End points are invalid. */ ++ colorspace->flags |= PNG_COLORSPACE_INVALID; ++ png_benign_error(png_ptr, "invalid end points"); ++ break; ++ ++ default: ++ colorspace->flags |= PNG_COLORSPACE_INVALID; ++ png_error(png_ptr, "internal error checking chromaticities"); ++ } ++ ++ return 0; /* failed */ ++} ++ ++#if defined(PNG_sRGB_SUPPORTED) || defined(PNG_iCCP_SUPPORTED) ++/* Error message generation */ ++static char ++png_icc_tag_char(png_uint_32 byte) ++{ ++ byte &= 0xff; ++ if (byte >= 32 && byte <= 126) ++ return (char)byte; ++ else ++ return '?'; ++} ++ ++static void ++png_icc_tag_name(char *name, png_uint_32 tag) ++{ ++ name[0] = '\''; ++ name[1] = png_icc_tag_char(tag >> 24); ++ name[2] = png_icc_tag_char(tag >> 16); ++ name[3] = png_icc_tag_char(tag >> 8); ++ name[4] = png_icc_tag_char(tag ); ++ name[5] = '\''; ++} ++ ++static int ++is_ICC_signature_char(png_alloc_size_t it) ++{ ++ return it == 32 || (it >= 48 && it <= 57) || (it >= 65 && it <= 90) || ++ (it >= 97 && it <= 122); ++} ++ ++static int ++is_ICC_signature(png_alloc_size_t it) ++{ ++ return is_ICC_signature_char(it >> 24) /* checks all the top bits */ && ++ is_ICC_signature_char((it >> 16) & 0xff) && ++ is_ICC_signature_char((it >> 8) & 0xff) && ++ is_ICC_signature_char(it & 0xff); ++} ++ ++static int ++png_icc_profile_error(png_const_structrp png_ptr, png_colorspacerp colorspace, ++ png_const_charp name, png_alloc_size_t value, png_const_charp reason) ++{ ++ size_t pos; ++ char message[196]; /* see below for calculation */ ++ ++ if (colorspace != NULL) ++ colorspace->flags |= PNG_COLORSPACE_INVALID; ++ ++ pos = png_safecat(message, (sizeof message), 0, "profile '"); /* 9 chars */ ++ pos = png_safecat(message, pos+79, pos, name); /* Truncate to 79 chars */ ++ pos = png_safecat(message, (sizeof message), pos, "': "); /* +2 = 90 */ ++ if (is_ICC_signature(value) != 0) ++ { ++ /* So 'value' is at most 4 bytes and the following cast is safe */ ++ png_icc_tag_name(message+pos, (png_uint_32)value); ++ pos += 6; /* total +8; less than the else clause */ ++ message[pos++] = ':'; ++ message[pos++] = ' '; ++ } ++# ifdef PNG_WARNINGS_SUPPORTED ++ else ++ { ++ char number[PNG_NUMBER_BUFFER_SIZE]; /* +24 = 114*/ ++ ++ pos = png_safecat(message, (sizeof message), pos, ++ png_format_number(number, number+(sizeof number), ++ PNG_NUMBER_FORMAT_x, value)); ++ pos = png_safecat(message, (sizeof message), pos, "h: "); /*+2 = 116*/ ++ } ++# endif ++ /* The 'reason' is an arbitrary message, allow +79 maximum 195 */ ++ pos = png_safecat(message, (sizeof message), pos, reason); ++ PNG_UNUSED(pos) ++ ++ /* This is recoverable, but make it unconditionally an app_error on write to ++ * avoid writing invalid ICC profiles into PNG files (i.e., we handle them ++ * on read, with a warning, but on write unless the app turns off ++ * application errors the PNG won't be written.) ++ */ ++ png_chunk_report(png_ptr, message, ++ (colorspace != NULL) ? PNG_CHUNK_ERROR : PNG_CHUNK_WRITE_ERROR); ++ ++ return 0; ++} ++#endif /* sRGB || iCCP */ ++ ++#ifdef PNG_sRGB_SUPPORTED ++int /* PRIVATE */ ++png_colorspace_set_sRGB(png_const_structrp png_ptr, png_colorspacerp colorspace, ++ int intent) ++{ ++ /* sRGB sets known gamma, end points and (from the chunk) intent. */ ++ /* IMPORTANT: these are not necessarily the values found in an ICC profile ++ * because ICC profiles store values adapted to a D50 environment; it is ++ * expected that the ICC profile mediaWhitePointTag will be D50; see the ++ * checks and code elsewhere to understand this better. ++ * ++ * These XYZ values, which are accurate to 5dp, produce rgb to gray ++ * coefficients of (6968,23435,2366), which are reduced (because they add up ++ * to 32769 not 32768) to (6968,23434,2366). These are the values that ++ * libpng has traditionally used (and are the best values given the 15bit ++ * algorithm used by the rgb to gray code.) ++ */ ++ static const png_XYZ sRGB_XYZ = /* D65 XYZ (*not* the D50 adapted values!) */ ++ { ++ /* color X Y Z */ ++ /* red */ 41239, 21264, 1933, ++ /* green */ 35758, 71517, 11919, ++ /* blue */ 18048, 7219, 95053 ++ }; ++ ++ /* Do nothing if the colorspace is already invalidated. */ ++ if ((colorspace->flags & PNG_COLORSPACE_INVALID) != 0) ++ return 0; ++ ++ /* Check the intent, then check for existing settings. It is valid for the ++ * PNG file to have cHRM or gAMA chunks along with sRGB, but the values must ++ * be consistent with the correct values. If, however, this function is ++ * called below because an iCCP chunk matches sRGB then it is quite ++ * conceivable that an older app recorded incorrect gAMA and cHRM because of ++ * an incorrect calculation based on the values in the profile - this does ++ * *not* invalidate the profile (though it still produces an error, which can ++ * be ignored.) ++ */ ++ if (intent < 0 || intent >= PNG_sRGB_INTENT_LAST) ++ return png_icc_profile_error(png_ptr, colorspace, "sRGB", ++ (png_alloc_size_t)intent, "invalid sRGB rendering intent"); ++ ++ if ((colorspace->flags & PNG_COLORSPACE_HAVE_INTENT) != 0 && ++ colorspace->rendering_intent != intent) ++ return png_icc_profile_error(png_ptr, colorspace, "sRGB", ++ (png_alloc_size_t)intent, "inconsistent rendering intents"); ++ ++ if ((colorspace->flags & PNG_COLORSPACE_FROM_sRGB) != 0) ++ { ++ png_benign_error(png_ptr, "duplicate sRGB information ignored"); ++ return 0; ++ } ++ ++ /* If the standard sRGB cHRM chunk does not match the one from the PNG file ++ * warn but overwrite the value with the correct one. ++ */ ++ if ((colorspace->flags & PNG_COLORSPACE_HAVE_ENDPOINTS) != 0 && ++ !png_colorspace_endpoints_match(&sRGB_xy, &colorspace->end_points_xy, ++ 100)) ++ png_chunk_report(png_ptr, "cHRM chunk does not match sRGB", ++ PNG_CHUNK_ERROR); ++ ++ /* This check is just done for the error reporting - the routine always ++ * returns true when the 'from' argument corresponds to sRGB (2). ++ */ ++ (void)png_colorspace_check_gamma(png_ptr, colorspace, PNG_GAMMA_sRGB_INVERSE, ++ 2/*from sRGB*/); ++ ++ /* intent: bugs in GCC force 'int' to be used as the parameter type. */ ++ colorspace->rendering_intent = (png_uint_16)intent; ++ colorspace->flags |= PNG_COLORSPACE_HAVE_INTENT; ++ ++ /* endpoints */ ++ colorspace->end_points_xy = sRGB_xy; ++ colorspace->end_points_XYZ = sRGB_XYZ; ++ colorspace->flags |= ++ (PNG_COLORSPACE_HAVE_ENDPOINTS|PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB); ++ ++ /* gamma */ ++ colorspace->gamma = PNG_GAMMA_sRGB_INVERSE; ++ colorspace->flags |= PNG_COLORSPACE_HAVE_GAMMA; ++ ++ /* Finally record that we have an sRGB profile */ ++ colorspace->flags |= ++ (PNG_COLORSPACE_MATCHES_sRGB|PNG_COLORSPACE_FROM_sRGB); ++ ++ return 1; /* set */ ++} ++#endif /* sRGB */ ++ ++#ifdef PNG_iCCP_SUPPORTED ++/* Encoded value of D50 as an ICC XYZNumber. From the ICC 2010 spec the value ++ * is XYZ(0.9642,1.0,0.8249), which scales to: ++ * ++ * (63189.8112, 65536, 54060.6464) ++ */ ++static const png_byte D50_nCIEXYZ[12] = ++ { 0x00, 0x00, 0xf6, 0xd6, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0xd3, 0x2d }; ++ ++static int /* bool */ ++icc_check_length(png_const_structrp png_ptr, png_colorspacerp colorspace, ++ png_const_charp name, png_uint_32 profile_length) ++{ ++ if (profile_length < 132) ++ return png_icc_profile_error(png_ptr, colorspace, name, profile_length, ++ "too short"); ++ return 1; ++} ++ ++#ifdef PNG_READ_iCCP_SUPPORTED ++int /* PRIVATE */ ++png_icc_check_length(png_const_structrp png_ptr, png_colorspacerp colorspace, ++ png_const_charp name, png_uint_32 profile_length) ++{ ++ if (!icc_check_length(png_ptr, colorspace, name, profile_length)) ++ return 0; ++ ++ /* This needs to be here because the 'normal' check is in ++ * png_decompress_chunk, yet this happens after the attempt to ++ * png_malloc_base the required data. We only need this on read; on write ++ * the caller supplies the profile buffer so libpng doesn't allocate it. See ++ * the call to icc_check_length below (the write case). ++ */ ++# ifdef PNG_SET_USER_LIMITS_SUPPORTED ++ else if (png_ptr->user_chunk_malloc_max > 0 && ++ png_ptr->user_chunk_malloc_max < profile_length) ++ return png_icc_profile_error(png_ptr, colorspace, name, profile_length, ++ "exceeds application limits"); ++# elif PNG_USER_CHUNK_MALLOC_MAX > 0 ++ else if (PNG_USER_CHUNK_MALLOC_MAX < profile_length) ++ return png_icc_profile_error(png_ptr, colorspace, name, profile_length, ++ "exceeds libpng limits"); ++# else /* !SET_USER_LIMITS */ ++ /* This will get compiled out on all 32-bit and better systems. */ ++ else if (PNG_SIZE_MAX < profile_length) ++ return png_icc_profile_error(png_ptr, colorspace, name, profile_length, ++ "exceeds system limits"); ++# endif /* !SET_USER_LIMITS */ ++ ++ return 1; ++} ++#endif /* READ_iCCP */ ++ ++int /* PRIVATE */ ++png_icc_check_header(png_const_structrp png_ptr, png_colorspacerp colorspace, ++ png_const_charp name, png_uint_32 profile_length, ++ png_const_bytep profile/* first 132 bytes only */, int color_type) ++{ ++ png_uint_32 temp; ++ ++ /* Length check; this cannot be ignored in this code because profile_length ++ * is used later to check the tag table, so even if the profile seems over ++ * long profile_length from the caller must be correct. The caller can fix ++ * this up on read or write by just passing in the profile header length. ++ */ ++ temp = png_get_uint_32(profile); ++ if (temp != profile_length) ++ return png_icc_profile_error(png_ptr, colorspace, name, temp, ++ "length does not match profile"); ++ ++ temp = (png_uint_32) (*(profile+8)); ++ if (temp > 3 && (profile_length & 3)) ++ return png_icc_profile_error(png_ptr, colorspace, name, profile_length, ++ "invalid length"); ++ ++ temp = png_get_uint_32(profile+128); /* tag count: 12 bytes/tag */ ++ if (temp > 357913930 || /* (2^32-4-132)/12: maximum possible tag count */ ++ profile_length < 132+12*temp) /* truncated tag table */ ++ return png_icc_profile_error(png_ptr, colorspace, name, temp, ++ "tag count too large"); ++ ++ /* The 'intent' must be valid or we can't store it, ICC limits the intent to ++ * 16 bits. ++ */ ++ temp = png_get_uint_32(profile+64); ++ if (temp >= 0xffff) /* The ICC limit */ ++ return png_icc_profile_error(png_ptr, colorspace, name, temp, ++ "invalid rendering intent"); ++ ++ /* This is just a warning because the profile may be valid in future ++ * versions. ++ */ ++ if (temp >= PNG_sRGB_INTENT_LAST) ++ (void)png_icc_profile_error(png_ptr, NULL, name, temp, ++ "intent outside defined range"); ++ ++ /* At this point the tag table can't be checked because it hasn't necessarily ++ * been loaded; however, various header fields can be checked. These checks ++ * are for values permitted by the PNG spec in an ICC profile; the PNG spec ++ * restricts the profiles that can be passed in an iCCP chunk (they must be ++ * appropriate to processing PNG data!) ++ */ ++ ++ /* Data checks (could be skipped). These checks must be independent of the ++ * version number; however, the version number doesn't accommodate changes in ++ * the header fields (just the known tags and the interpretation of the ++ * data.) ++ */ ++ temp = png_get_uint_32(profile+36); /* signature 'ascp' */ ++ if (temp != 0x61637370) ++ return png_icc_profile_error(png_ptr, colorspace, name, temp, ++ "invalid signature"); ++ ++ /* Currently the PCS illuminant/adopted white point (the computational ++ * white point) are required to be D50, ++ * however the profile contains a record of the illuminant so perhaps ICC ++ * expects to be able to change this in the future (despite the rationale in ++ * the introduction for using a fixed PCS adopted white.) Consequently the ++ * following is just a warning. ++ */ ++ if (memcmp(profile+68, D50_nCIEXYZ, 12) != 0) ++ (void)png_icc_profile_error(png_ptr, NULL, name, 0/*no tag value*/, ++ "PCS illuminant is not D50"); ++ ++ /* The PNG spec requires this: ++ * "If the iCCP chunk is present, the image samples conform to the colour ++ * space represented by the embedded ICC profile as defined by the ++ * International Color Consortium [ICC]. The colour space of the ICC profile ++ * shall be an RGB colour space for colour images (PNG colour types 2, 3, and ++ * 6), or a greyscale colour space for greyscale images (PNG colour types 0 ++ * and 4)." ++ * ++ * This checking code ensures the embedded profile (on either read or write) ++ * conforms to the specification requirements. Notice that an ICC 'gray' ++ * color-space profile contains the information to transform the monochrome ++ * data to XYZ or L*a*b (according to which PCS the profile uses) and this ++ * should be used in preference to the standard libpng K channel replication ++ * into R, G and B channels. ++ * ++ * Previously it was suggested that an RGB profile on grayscale data could be ++ * handled. However it it is clear that using an RGB profile in this context ++ * must be an error - there is no specification of what it means. Thus it is ++ * almost certainly more correct to ignore the profile. ++ */ ++ temp = png_get_uint_32(profile+16); /* data colour space field */ ++ switch (temp) ++ { ++ case 0x52474220: /* 'RGB ' */ ++ if ((color_type & PNG_COLOR_MASK_COLOR) == 0) ++ return png_icc_profile_error(png_ptr, colorspace, name, temp, ++ "RGB color space not permitted on grayscale PNG"); ++ break; ++ ++ case 0x47524159: /* 'GRAY' */ ++ if ((color_type & PNG_COLOR_MASK_COLOR) != 0) ++ return png_icc_profile_error(png_ptr, colorspace, name, temp, ++ "Gray color space not permitted on RGB PNG"); ++ break; ++ ++ default: ++ return png_icc_profile_error(png_ptr, colorspace, name, temp, ++ "invalid ICC profile color space"); ++ } ++ ++ /* It is up to the application to check that the profile class matches the ++ * application requirements; the spec provides no guidance, but it's pretty ++ * weird if the profile is not scanner ('scnr'), monitor ('mntr'), printer ++ * ('prtr') or 'spac' (for generic color spaces). Issue a warning in these ++ * cases. Issue an error for device link or abstract profiles - these don't ++ * contain the records necessary to transform the color-space to anything ++ * other than the target device (and not even that for an abstract profile). ++ * Profiles of these classes may not be embedded in images. ++ */ ++ temp = png_get_uint_32(profile+12); /* profile/device class */ ++ switch (temp) ++ { ++ case 0x73636e72: /* 'scnr' */ ++ case 0x6d6e7472: /* 'mntr' */ ++ case 0x70727472: /* 'prtr' */ ++ case 0x73706163: /* 'spac' */ ++ /* All supported */ ++ break; ++ ++ case 0x61627374: /* 'abst' */ ++ /* May not be embedded in an image */ ++ return png_icc_profile_error(png_ptr, colorspace, name, temp, ++ "invalid embedded Abstract ICC profile"); ++ ++ case 0x6c696e6b: /* 'link' */ ++ /* DeviceLink profiles cannot be interpreted in a non-device specific ++ * fashion, if an app uses the AToB0Tag in the profile the results are ++ * undefined unless the result is sent to the intended device, ++ * therefore a DeviceLink profile should not be found embedded in a ++ * PNG. ++ */ ++ return png_icc_profile_error(png_ptr, colorspace, name, temp, ++ "unexpected DeviceLink ICC profile class"); ++ ++ case 0x6e6d636c: /* 'nmcl' */ ++ /* A NamedColor profile is also device specific, however it doesn't ++ * contain an AToB0 tag that is open to misinterpretation. Almost ++ * certainly it will fail the tests below. ++ */ ++ (void)png_icc_profile_error(png_ptr, NULL, name, temp, ++ "unexpected NamedColor ICC profile class"); ++ break; ++ ++ default: ++ /* To allow for future enhancements to the profile accept unrecognized ++ * profile classes with a warning, these then hit the test below on the ++ * tag content to ensure they are backward compatible with one of the ++ * understood profiles. ++ */ ++ (void)png_icc_profile_error(png_ptr, NULL, name, temp, ++ "unrecognized ICC profile class"); ++ break; ++ } ++ ++ /* For any profile other than a device link one the PCS must be encoded ++ * either in XYZ or Lab. ++ */ ++ temp = png_get_uint_32(profile+20); ++ switch (temp) ++ { ++ case 0x58595a20: /* 'XYZ ' */ ++ case 0x4c616220: /* 'Lab ' */ ++ break; ++ ++ default: ++ return png_icc_profile_error(png_ptr, colorspace, name, temp, ++ "unexpected ICC PCS encoding"); ++ } ++ ++ return 1; ++} ++ ++int /* PRIVATE */ ++png_icc_check_tag_table(png_const_structrp png_ptr, png_colorspacerp colorspace, ++ png_const_charp name, png_uint_32 profile_length, ++ png_const_bytep profile /* header plus whole tag table */) ++{ ++ png_uint_32 tag_count = png_get_uint_32(profile+128); ++ png_uint_32 itag; ++ png_const_bytep tag = profile+132; /* The first tag */ ++ ++ /* First scan all the tags in the table and add bits to the icc_info value ++ * (temporarily in 'tags'). ++ */ ++ for (itag=0; itag < tag_count; ++itag, tag += 12) ++ { ++ png_uint_32 tag_id = png_get_uint_32(tag+0); ++ png_uint_32 tag_start = png_get_uint_32(tag+4); /* must be aligned */ ++ png_uint_32 tag_length = png_get_uint_32(tag+8);/* not padded */ ++ ++ /* The ICC specification does not exclude zero length tags, therefore the ++ * start might actually be anywhere if there is no data, but this would be ++ * a clear abuse of the intent of the standard so the start is checked for ++ * being in range. All defined tag types have an 8 byte header - a 4 byte ++ * type signature then 0. ++ */ ++ ++ /* This is a hard error; potentially it can cause read outside the ++ * profile. ++ */ ++ if (tag_start > profile_length || tag_length > profile_length - tag_start) ++ return png_icc_profile_error(png_ptr, colorspace, name, tag_id, ++ "ICC profile tag outside profile"); ++ ++ if ((tag_start & 3) != 0) ++ { ++ /* CNHP730S.icc shipped with Microsoft Windows 64 violates this; it is ++ * only a warning here because libpng does not care about the ++ * alignment. ++ */ ++ (void)png_icc_profile_error(png_ptr, NULL, name, tag_id, ++ "ICC profile tag start not a multiple of 4"); ++ } ++ } ++ ++ return 1; /* success, maybe with warnings */ ++} ++ ++#ifdef PNG_sRGB_SUPPORTED ++#if PNG_sRGB_PROFILE_CHECKS >= 0 ++/* Information about the known ICC sRGB profiles */ ++static const struct ++{ ++ png_uint_32 adler, crc, length; ++ png_uint_32 md5[4]; ++ png_byte have_md5; ++ png_byte is_broken; ++ png_uint_16 intent; ++ ++# define PNG_MD5(a,b,c,d) { a, b, c, d }, (a!=0)||(b!=0)||(c!=0)||(d!=0) ++# define PNG_ICC_CHECKSUM(adler, crc, md5, intent, broke, date, length, fname)\ ++ { adler, crc, length, md5, broke, intent }, ++ ++} png_sRGB_checks[] = ++{ ++ /* This data comes from contrib/tools/checksum-icc run on downloads of ++ * all four ICC sRGB profiles from www.color.org. ++ */ ++ /* adler32, crc32, MD5[4], intent, date, length, file-name */ ++ PNG_ICC_CHECKSUM(0x0a3fd9f6, 0x3b8772b9, ++ PNG_MD5(0x29f83dde, 0xaff255ae, 0x7842fae4, 0xca83390d), 0, 0, ++ "2009/03/27 21:36:31", 3048, "sRGB_IEC61966-2-1_black_scaled.icc") ++ ++ /* ICC sRGB v2 perceptual no black-compensation: */ ++ PNG_ICC_CHECKSUM(0x4909e5e1, 0x427ebb21, ++ PNG_MD5(0xc95bd637, 0xe95d8a3b, 0x0df38f99, 0xc1320389), 1, 0, ++ "2009/03/27 21:37:45", 3052, "sRGB_IEC61966-2-1_no_black_scaling.icc") ++ ++ PNG_ICC_CHECKSUM(0xfd2144a1, 0x306fd8ae, ++ PNG_MD5(0xfc663378, 0x37e2886b, 0xfd72e983, 0x8228f1b8), 0, 0, ++ "2009/08/10 17:28:01", 60988, "sRGB_v4_ICC_preference_displayclass.icc") ++ ++ /* ICC sRGB v4 perceptual */ ++ PNG_ICC_CHECKSUM(0x209c35d2, 0xbbef7812, ++ PNG_MD5(0x34562abf, 0x994ccd06, 0x6d2c5721, 0xd0d68c5d), 0, 0, ++ "2007/07/25 00:05:37", 60960, "sRGB_v4_ICC_preference.icc") ++ ++ /* The following profiles have no known MD5 checksum. If there is a match ++ * on the (empty) MD5 the other fields are used to attempt a match and ++ * a warning is produced. The first two of these profiles have a 'cprt' tag ++ * which suggests that they were also made by Hewlett Packard. ++ */ ++ PNG_ICC_CHECKSUM(0xa054d762, 0x5d5129ce, ++ PNG_MD5(0x00000000, 0x00000000, 0x00000000, 0x00000000), 1, 0, ++ "2004/07/21 18:57:42", 3024, "sRGB_IEC61966-2-1_noBPC.icc") ++ ++ /* This is a 'mntr' (display) profile with a mediaWhitePointTag that does not ++ * match the D50 PCS illuminant in the header (it is in fact the D65 values, ++ * so the white point is recorded as the un-adapted value.) The profiles ++ * below only differ in one byte - the intent - and are basically the same as ++ * the previous profile except for the mediaWhitePointTag error and a missing ++ * chromaticAdaptationTag. ++ */ ++ PNG_ICC_CHECKSUM(0xf784f3fb, 0x182ea552, ++ PNG_MD5(0x00000000, 0x00000000, 0x00000000, 0x00000000), 0, 1/*broken*/, ++ "1998/02/09 06:49:00", 3144, "HP-Microsoft sRGB v2 perceptual") ++ ++ PNG_ICC_CHECKSUM(0x0398f3fc, 0xf29e526d, ++ PNG_MD5(0x00000000, 0x00000000, 0x00000000, 0x00000000), 1, 1/*broken*/, ++ "1998/02/09 06:49:00", 3144, "HP-Microsoft sRGB v2 media-relative") ++}; ++ ++static int ++png_compare_ICC_profile_with_sRGB(png_const_structrp png_ptr, ++ png_const_bytep profile, uLong adler) ++{ ++ /* The quick check is to verify just the MD5 signature and trust the ++ * rest of the data. Because the profile has already been verified for ++ * correctness this is safe. png_colorspace_set_sRGB will check the 'intent' ++ * field too, so if the profile has been edited with an intent not defined ++ * by sRGB (but maybe defined by a later ICC specification) the read of ++ * the profile will fail at that point. ++ */ ++ ++ png_uint_32 length = 0; ++ png_uint_32 intent = 0x10000; /* invalid */ ++#if PNG_sRGB_PROFILE_CHECKS > 1 ++ uLong crc = 0; /* the value for 0 length data */ ++#endif ++ unsigned int i; ++ ++#ifdef PNG_SET_OPTION_SUPPORTED ++ /* First see if PNG_SKIP_sRGB_CHECK_PROFILE has been set to "on" */ ++ if (((png_ptr->options >> PNG_SKIP_sRGB_CHECK_PROFILE) & 3) == ++ PNG_OPTION_ON) ++ return 0; ++#endif ++ ++ for (i=0; i < (sizeof png_sRGB_checks) / (sizeof png_sRGB_checks[0]); ++i) ++ { ++ if (png_get_uint_32(profile+84) == png_sRGB_checks[i].md5[0] && ++ png_get_uint_32(profile+88) == png_sRGB_checks[i].md5[1] && ++ png_get_uint_32(profile+92) == png_sRGB_checks[i].md5[2] && ++ png_get_uint_32(profile+96) == png_sRGB_checks[i].md5[3]) ++ { ++ /* This may be one of the old HP profiles without an MD5, in that ++ * case we can only use the length and Adler32 (note that these ++ * are not used by default if there is an MD5!) ++ */ ++# if PNG_sRGB_PROFILE_CHECKS == 0 ++ if (png_sRGB_checks[i].have_md5 != 0) ++ return 1+png_sRGB_checks[i].is_broken; ++# endif ++ ++ /* Profile is unsigned or more checks have been configured in. */ ++ if (length == 0) ++ { ++ length = png_get_uint_32(profile); ++ intent = png_get_uint_32(profile+64); ++ } ++ ++ /* Length *and* intent must match */ ++ if (length == (png_uint_32) png_sRGB_checks[i].length && ++ intent == (png_uint_32) png_sRGB_checks[i].intent) ++ { ++ /* Now calculate the adler32 if not done already. */ ++ if (adler == 0) ++ { ++ adler = adler32(0, NULL, 0); ++ adler = adler32(adler, profile, length); ++ } ++ ++ if (adler == png_sRGB_checks[i].adler) ++ { ++ /* These basic checks suggest that the data has not been ++ * modified, but if the check level is more than 1 perform ++ * our own crc32 checksum on the data. ++ */ ++# if PNG_sRGB_PROFILE_CHECKS > 1 ++ if (crc == 0) ++ { ++ crc = crc32(0, NULL, 0); ++ crc = crc32(crc, profile, length); ++ } ++ ++ /* So this check must pass for the 'return' below to happen. ++ */ ++ if (crc == png_sRGB_checks[i].crc) ++# endif ++ { ++ if (png_sRGB_checks[i].is_broken != 0) ++ { ++ /* These profiles are known to have bad data that may cause ++ * problems if they are used, therefore attempt to ++ * discourage their use, skip the 'have_md5' warning below, ++ * which is made irrelevant by this error. ++ */ ++ png_chunk_report(png_ptr, "known incorrect sRGB profile", ++ PNG_CHUNK_ERROR); ++ } ++ ++ /* Warn that this being done; this isn't even an error since ++ * the profile is perfectly valid, but it would be nice if ++ * people used the up-to-date ones. ++ */ ++ else if (png_sRGB_checks[i].have_md5 == 0) ++ { ++ png_chunk_report(png_ptr, ++ "out-of-date sRGB profile with no signature", ++ PNG_CHUNK_WARNING); ++ } ++ ++ return 1+png_sRGB_checks[i].is_broken; ++ } ++ } ++ ++# if PNG_sRGB_PROFILE_CHECKS > 0 ++ /* The signature matched, but the profile had been changed in some ++ * way. This probably indicates a data error or uninformed hacking. ++ * Fall through to "no match". ++ */ ++ png_chunk_report(png_ptr, ++ "Not recognizing known sRGB profile that has been edited", ++ PNG_CHUNK_WARNING); ++ break; ++# endif ++ } ++ } ++ } ++ ++ return 0; /* no match */ ++} ++ ++void /* PRIVATE */ ++png_icc_set_sRGB(png_const_structrp png_ptr, ++ png_colorspacerp colorspace, png_const_bytep profile, uLong adler) ++{ ++ /* Is this profile one of the known ICC sRGB profiles? If it is, just set ++ * the sRGB information. ++ */ ++ if (png_compare_ICC_profile_with_sRGB(png_ptr, profile, adler) != 0) ++ (void)png_colorspace_set_sRGB(png_ptr, colorspace, ++ (int)/*already checked*/png_get_uint_32(profile+64)); ++} ++#endif /* PNG_sRGB_PROFILE_CHECKS >= 0 */ ++#endif /* sRGB */ ++ ++int /* PRIVATE */ ++png_colorspace_set_ICC(png_const_structrp png_ptr, png_colorspacerp colorspace, ++ png_const_charp name, png_uint_32 profile_length, png_const_bytep profile, ++ int color_type) ++{ ++ if ((colorspace->flags & PNG_COLORSPACE_INVALID) != 0) ++ return 0; ++ ++ if (icc_check_length(png_ptr, colorspace, name, profile_length) != 0 && ++ png_icc_check_header(png_ptr, colorspace, name, profile_length, profile, ++ color_type) != 0 && ++ png_icc_check_tag_table(png_ptr, colorspace, name, profile_length, ++ profile) != 0) ++ { ++# if defined(PNG_sRGB_SUPPORTED) && PNG_sRGB_PROFILE_CHECKS >= 0 ++ /* If no sRGB support, don't try storing sRGB information */ ++ png_icc_set_sRGB(png_ptr, colorspace, profile, 0); ++# endif ++ return 1; ++ } ++ ++ /* Failure case */ ++ return 0; ++} ++#endif /* iCCP */ ++ ++#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED ++void /* PRIVATE */ ++png_colorspace_set_rgb_coefficients(png_structrp png_ptr) ++{ ++ /* Set the rgb_to_gray coefficients from the colorspace. */ ++ if (png_ptr->rgb_to_gray_coefficients_set == 0 && ++ (png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_ENDPOINTS) != 0) ++ { ++ /* png_set_background has not been called, get the coefficients from the Y ++ * values of the colorspace colorants. ++ */ ++ png_fixed_point r = png_ptr->colorspace.end_points_XYZ.red_Y; ++ png_fixed_point g = png_ptr->colorspace.end_points_XYZ.green_Y; ++ png_fixed_point b = png_ptr->colorspace.end_points_XYZ.blue_Y; ++ png_fixed_point total = r+g+b; ++ ++ if (total > 0 && ++ r >= 0 && png_muldiv(&r, r, 32768, total) && r >= 0 && r <= 32768 && ++ g >= 0 && png_muldiv(&g, g, 32768, total) && g >= 0 && g <= 32768 && ++ b >= 0 && png_muldiv(&b, b, 32768, total) && b >= 0 && b <= 32768 && ++ r+g+b <= 32769) ++ { ++ /* We allow 0 coefficients here. r+g+b may be 32769 if two or ++ * all of the coefficients were rounded up. Handle this by ++ * reducing the *largest* coefficient by 1; this matches the ++ * approach used for the default coefficients in pngrtran.c ++ */ ++ int add = 0; ++ ++ if (r+g+b > 32768) ++ add = -1; ++ else if (r+g+b < 32768) ++ add = 1; ++ ++ if (add != 0) ++ { ++ if (g >= r && g >= b) ++ g += add; ++ else if (r >= g && r >= b) ++ r += add; ++ else ++ b += add; ++ } ++ ++ /* Check for an internal error. */ ++ if (r+g+b != 32768) ++ png_error(png_ptr, ++ "internal error handling cHRM coefficients"); ++ ++ else ++ { ++ png_ptr->rgb_to_gray_red_coeff = (png_uint_16)r; ++ png_ptr->rgb_to_gray_green_coeff = (png_uint_16)g; ++ } ++ } ++ ++ /* This is a png_error at present even though it could be ignored - ++ * it should never happen, but it is important that if it does, the ++ * bug is fixed. ++ */ ++ else ++ png_error(png_ptr, "internal error handling cHRM->XYZ"); ++ } ++} ++#endif /* READ_RGB_TO_GRAY */ ++ ++#endif /* COLORSPACE */ ++ ++#ifdef __GNUC__ ++/* This exists solely to work round a warning from GNU C. */ ++static int /* PRIVATE */ ++png_gt(size_t a, size_t b) ++{ ++ return a > b; ++} ++#else ++# define png_gt(a,b) ((a) > (b)) ++#endif ++ ++void /* PRIVATE */ ++png_check_IHDR(png_const_structrp png_ptr, ++ png_uint_32 width, png_uint_32 height, int bit_depth, ++ int color_type, int interlace_type, int compression_type, ++ int filter_type) ++{ ++ int error = 0; ++ ++ /* Check for width and height valid values */ ++ if (width == 0) ++ { ++ png_warning(png_ptr, "Image width is zero in IHDR"); ++ error = 1; ++ } ++ ++ if (width > PNG_UINT_31_MAX) ++ { ++ png_warning(png_ptr, "Invalid image width in IHDR"); ++ error = 1; ++ } ++ ++ if (png_gt(((width + 7) & (~7U)), ++ ((PNG_SIZE_MAX ++ - 48 /* big_row_buf hack */ ++ - 1) /* filter byte */ ++ / 8) /* 8-byte RGBA pixels */ ++ - 1)) /* extra max_pixel_depth pad */ ++ { ++ /* The size of the row must be within the limits of this architecture. ++ * Because the read code can perform arbitrary transformations the ++ * maximum size is checked here. Because the code in png_read_start_row ++ * adds extra space "for safety's sake" in several places a conservative ++ * limit is used here. ++ * ++ * NOTE: it would be far better to check the size that is actually used, ++ * but the effect in the real world is minor and the changes are more ++ * extensive, therefore much more dangerous and much more difficult to ++ * write in a way that avoids compiler warnings. ++ */ ++ png_warning(png_ptr, "Image width is too large for this architecture"); ++ error = 1; ++ } ++ ++#ifdef PNG_SET_USER_LIMITS_SUPPORTED ++ if (width > png_ptr->user_width_max) ++#else ++ if (width > PNG_USER_WIDTH_MAX) ++#endif ++ { ++ png_warning(png_ptr, "Image width exceeds user limit in IHDR"); ++ error = 1; ++ } ++ ++ if (height == 0) ++ { ++ png_warning(png_ptr, "Image height is zero in IHDR"); ++ error = 1; ++ } ++ ++ if (height > PNG_UINT_31_MAX) ++ { ++ png_warning(png_ptr, "Invalid image height in IHDR"); ++ error = 1; ++ } ++ ++#ifdef PNG_SET_USER_LIMITS_SUPPORTED ++ if (height > png_ptr->user_height_max) ++#else ++ if (height > PNG_USER_HEIGHT_MAX) ++#endif ++ { ++ png_warning(png_ptr, "Image height exceeds user limit in IHDR"); ++ error = 1; ++ } ++ ++ /* Check other values */ ++ if (bit_depth != 1 && bit_depth != 2 && bit_depth != 4 && ++ bit_depth != 8 && bit_depth != 16) ++ { ++ png_warning(png_ptr, "Invalid bit depth in IHDR"); ++ error = 1; ++ } ++ ++ if (color_type < 0 || color_type == 1 || ++ color_type == 5 || color_type > 6) ++ { ++ png_warning(png_ptr, "Invalid color type in IHDR"); ++ error = 1; ++ } ++ ++ if (((color_type == PNG_COLOR_TYPE_PALETTE) && bit_depth > 8) || ++ ((color_type == PNG_COLOR_TYPE_RGB || ++ color_type == PNG_COLOR_TYPE_GRAY_ALPHA || ++ color_type == PNG_COLOR_TYPE_RGB_ALPHA) && bit_depth < 8)) ++ { ++ png_warning(png_ptr, "Invalid color type/bit depth combination in IHDR"); ++ error = 1; ++ } ++ ++ if (interlace_type >= PNG_INTERLACE_LAST) ++ { ++ png_warning(png_ptr, "Unknown interlace method in IHDR"); ++ error = 1; ++ } ++ ++ if (compression_type != PNG_COMPRESSION_TYPE_BASE) ++ { ++ png_warning(png_ptr, "Unknown compression method in IHDR"); ++ error = 1; ++ } ++ ++#ifdef PNG_MNG_FEATURES_SUPPORTED ++ /* Accept filter_method 64 (intrapixel differencing) only if ++ * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and ++ * 2. Libpng did not read a PNG signature (this filter_method is only ++ * used in PNG datastreams that are embedded in MNG datastreams) and ++ * 3. The application called png_permit_mng_features with a mask that ++ * included PNG_FLAG_MNG_FILTER_64 and ++ * 4. The filter_method is 64 and ++ * 5. The color_type is RGB or RGBA ++ */ ++ if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) != 0 && ++ png_ptr->mng_features_permitted != 0) ++ png_warning(png_ptr, "MNG features are not allowed in a PNG datastream"); ++ ++ if (filter_type != PNG_FILTER_TYPE_BASE) ++ { ++ if (!((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 && ++ (filter_type == PNG_INTRAPIXEL_DIFFERENCING) && ++ ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) == 0) && ++ (color_type == PNG_COLOR_TYPE_RGB || ++ color_type == PNG_COLOR_TYPE_RGB_ALPHA))) ++ { ++ png_warning(png_ptr, "Unknown filter method in IHDR"); ++ error = 1; ++ } ++ ++ if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) != 0) ++ { ++ png_warning(png_ptr, "Invalid filter method in IHDR"); ++ error = 1; ++ } ++ } ++ ++#else ++ if (filter_type != PNG_FILTER_TYPE_BASE) ++ { ++ png_warning(png_ptr, "Unknown filter method in IHDR"); ++ error = 1; ++ } ++#endif ++ ++ if (error == 1) ++ png_error(png_ptr, "Invalid IHDR data"); ++} ++ ++#if defined(PNG_sCAL_SUPPORTED) || defined(PNG_pCAL_SUPPORTED) ++/* ASCII to fp functions */ ++/* Check an ASCII formatted floating point value, see the more detailed ++ * comments in pngpriv.h ++ */ ++/* The following is used internally to preserve the sticky flags */ ++#define png_fp_add(state, flags) ((state) |= (flags)) ++#define png_fp_set(state, value) ((state) = (value) | ((state) & PNG_FP_STICKY)) ++ ++int /* PRIVATE */ ++png_check_fp_number(png_const_charp string, size_t size, int *statep, ++ png_size_tp whereami) ++{ ++ int state = *statep; ++ size_t i = *whereami; ++ ++ while (i < size) ++ { ++ int type; ++ /* First find the type of the next character */ ++ switch (string[i]) ++ { ++ case 43: type = PNG_FP_SAW_SIGN; break; ++ case 45: type = PNG_FP_SAW_SIGN + PNG_FP_NEGATIVE; break; ++ case 46: type = PNG_FP_SAW_DOT; break; ++ case 48: type = PNG_FP_SAW_DIGIT; break; ++ case 49: case 50: case 51: case 52: ++ case 53: case 54: case 55: case 56: ++ case 57: type = PNG_FP_SAW_DIGIT + PNG_FP_NONZERO; break; ++ case 69: ++ case 101: type = PNG_FP_SAW_E; break; ++ default: goto PNG_FP_End; ++ } ++ ++ /* Now deal with this type according to the current ++ * state, the type is arranged to not overlap the ++ * bits of the PNG_FP_STATE. ++ */ ++ switch ((state & PNG_FP_STATE) + (type & PNG_FP_SAW_ANY)) ++ { ++ case PNG_FP_INTEGER + PNG_FP_SAW_SIGN: ++ if ((state & PNG_FP_SAW_ANY) != 0) ++ goto PNG_FP_End; /* not a part of the number */ ++ ++ png_fp_add(state, type); ++ break; ++ ++ case PNG_FP_INTEGER + PNG_FP_SAW_DOT: ++ /* Ok as trailer, ok as lead of fraction. */ ++ if ((state & PNG_FP_SAW_DOT) != 0) /* two dots */ ++ goto PNG_FP_End; ++ ++ else if ((state & PNG_FP_SAW_DIGIT) != 0) /* trailing dot? */ ++ png_fp_add(state, type); ++ ++ else ++ png_fp_set(state, PNG_FP_FRACTION | type); ++ ++ break; ++ ++ case PNG_FP_INTEGER + PNG_FP_SAW_DIGIT: ++ if ((state & PNG_FP_SAW_DOT) != 0) /* delayed fraction */ ++ png_fp_set(state, PNG_FP_FRACTION | PNG_FP_SAW_DOT); ++ ++ png_fp_add(state, type | PNG_FP_WAS_VALID); ++ ++ break; ++ ++ case PNG_FP_INTEGER + PNG_FP_SAW_E: ++ if ((state & PNG_FP_SAW_DIGIT) == 0) ++ goto PNG_FP_End; ++ ++ png_fp_set(state, PNG_FP_EXPONENT); ++ ++ break; ++ ++ /* case PNG_FP_FRACTION + PNG_FP_SAW_SIGN: ++ goto PNG_FP_End; ** no sign in fraction */ ++ ++ /* case PNG_FP_FRACTION + PNG_FP_SAW_DOT: ++ goto PNG_FP_End; ** Because SAW_DOT is always set */ ++ ++ case PNG_FP_FRACTION + PNG_FP_SAW_DIGIT: ++ png_fp_add(state, type | PNG_FP_WAS_VALID); ++ break; ++ ++ case PNG_FP_FRACTION + PNG_FP_SAW_E: ++ /* This is correct because the trailing '.' on an ++ * integer is handled above - so we can only get here ++ * with the sequence ".E" (with no preceding digits). ++ */ ++ if ((state & PNG_FP_SAW_DIGIT) == 0) ++ goto PNG_FP_End; ++ ++ png_fp_set(state, PNG_FP_EXPONENT); ++ ++ break; ++ ++ case PNG_FP_EXPONENT + PNG_FP_SAW_SIGN: ++ if ((state & PNG_FP_SAW_ANY) != 0) ++ goto PNG_FP_End; /* not a part of the number */ ++ ++ png_fp_add(state, PNG_FP_SAW_SIGN); ++ ++ break; ++ ++ /* case PNG_FP_EXPONENT + PNG_FP_SAW_DOT: ++ goto PNG_FP_End; */ ++ ++ case PNG_FP_EXPONENT + PNG_FP_SAW_DIGIT: ++ png_fp_add(state, PNG_FP_SAW_DIGIT | PNG_FP_WAS_VALID); ++ ++ break; ++ ++ /* case PNG_FP_EXPONEXT + PNG_FP_SAW_E: ++ goto PNG_FP_End; */ ++ ++ default: goto PNG_FP_End; /* I.e. break 2 */ ++ } ++ ++ /* The character seems ok, continue. */ ++ ++i; ++ } ++ ++PNG_FP_End: ++ /* Here at the end, update the state and return the correct ++ * return code. ++ */ ++ *statep = state; ++ *whereami = i; ++ ++ return (state & PNG_FP_SAW_DIGIT) != 0; ++} ++ ++ ++/* The same but for a complete string. */ ++int ++png_check_fp_string(png_const_charp string, size_t size) ++{ ++ int state=0; ++ size_t char_index=0; ++ ++ if (png_check_fp_number(string, size, &state, &char_index) != 0 && ++ (char_index == size || string[char_index] == 0)) ++ return state /* must be non-zero - see above */; ++ ++ return 0; /* i.e. fail */ ++} ++#endif /* pCAL || sCAL */ ++ ++#ifdef PNG_sCAL_SUPPORTED ++# ifdef PNG_FLOATING_POINT_SUPPORTED ++/* Utility used below - a simple accurate power of ten from an integral ++ * exponent. ++ */ ++static double ++png_pow10(int power) ++{ ++ int recip = 0; ++ double d = 1; ++ ++ /* Handle negative exponent with a reciprocal at the end because ++ * 10 is exact whereas .1 is inexact in base 2 ++ */ ++ if (power < 0) ++ { ++ if (power < DBL_MIN_10_EXP) return 0; ++ recip = 1; power = -power; ++ } ++ ++ if (power > 0) ++ { ++ /* Decompose power bitwise. */ ++ double mult = 10; ++ do ++ { ++ if (power & 1) d *= mult; ++ mult *= mult; ++ power >>= 1; ++ } ++ while (power > 0); ++ ++ if (recip != 0) d = 1/d; ++ } ++ /* else power is 0 and d is 1 */ ++ ++ return d; ++} ++ ++/* Function to format a floating point value in ASCII with a given ++ * precision. ++ */ ++#if GCC_STRICT_OVERFLOW ++#pragma GCC diagnostic push ++/* The problem arises below with exp_b10, which can never overflow because it ++ * comes, originally, from frexp and is therefore limited to a range which is ++ * typically +/-710 (log2(DBL_MAX)/log2(DBL_MIN)). ++ */ ++#pragma GCC diagnostic warning "-Wstrict-overflow=2" ++#endif /* GCC_STRICT_OVERFLOW */ ++void /* PRIVATE */ ++png_ascii_from_fp(png_const_structrp png_ptr, png_charp ascii, size_t size, ++ double fp, unsigned int precision) ++{ ++ /* We use standard functions from math.h, but not printf because ++ * that would require stdio. The caller must supply a buffer of ++ * sufficient size or we will png_error. The tests on size and ++ * the space in ascii[] consumed are indicated below. ++ */ ++ if (precision < 1) ++ precision = DBL_DIG; ++ ++ /* Enforce the limit of the implementation precision too. */ ++ if (precision > DBL_DIG+1) ++ precision = DBL_DIG+1; ++ ++ /* Basic sanity checks */ ++ if (size >= precision+5) /* See the requirements below. */ ++ { ++ if (fp < 0) ++ { ++ fp = -fp; ++ *ascii++ = 45; /* '-' PLUS 1 TOTAL 1 */ ++ --size; ++ } ++ ++ if (fp >= DBL_MIN && fp <= DBL_MAX) ++ { ++ int exp_b10; /* A base 10 exponent */ ++ double base; /* 10^exp_b10 */ ++ ++ /* First extract a base 10 exponent of the number, ++ * the calculation below rounds down when converting ++ * from base 2 to base 10 (multiply by log10(2) - ++ * 0.3010, but 77/256 is 0.3008, so exp_b10 needs to ++ * be increased. Note that the arithmetic shift ++ * performs a floor() unlike C arithmetic - using a ++ * C multiply would break the following for negative ++ * exponents. ++ */ ++ (void)frexp(fp, &exp_b10); /* exponent to base 2 */ ++ ++ exp_b10 = (exp_b10 * 77) >> 8; /* <= exponent to base 10 */ ++ ++ /* Avoid underflow here. */ ++ base = png_pow10(exp_b10); /* May underflow */ ++ ++ while (base < DBL_MIN || base < fp) ++ { ++ /* And this may overflow. */ ++ double test = png_pow10(exp_b10+1); ++ ++ if (test <= DBL_MAX) ++ { ++ ++exp_b10; base = test; ++ } ++ ++ else ++ break; ++ } ++ ++ /* Normalize fp and correct exp_b10, after this fp is in the ++ * range [.1,1) and exp_b10 is both the exponent and the digit ++ * *before* which the decimal point should be inserted ++ * (starting with 0 for the first digit). Note that this ++ * works even if 10^exp_b10 is out of range because of the ++ * test on DBL_MAX above. ++ */ ++ fp /= base; ++ while (fp >= 1) ++ { ++ fp /= 10; ++exp_b10; ++ } ++ ++ /* Because of the code above fp may, at this point, be ++ * less than .1, this is ok because the code below can ++ * handle the leading zeros this generates, so no attempt ++ * is made to correct that here. ++ */ ++ ++ { ++ unsigned int czero, clead, cdigits; ++ char exponent[10]; ++ ++ /* Allow up to two leading zeros - this will not lengthen ++ * the number compared to using E-n. ++ */ ++ if (exp_b10 < 0 && exp_b10 > -3) /* PLUS 3 TOTAL 4 */ ++ { ++ czero = 0U-exp_b10; /* PLUS 2 digits: TOTAL 3 */ ++ exp_b10 = 0; /* Dot added below before first output. */ ++ } ++ else ++ czero = 0; /* No zeros to add */ ++ ++ /* Generate the digit list, stripping trailing zeros and ++ * inserting a '.' before a digit if the exponent is 0. ++ */ ++ clead = czero; /* Count of leading zeros */ ++ cdigits = 0; /* Count of digits in list. */ ++ ++ do ++ { ++ double d; ++ ++ fp *= 10; ++ /* Use modf here, not floor and subtract, so that ++ * the separation is done in one step. At the end ++ * of the loop don't break the number into parts so ++ * that the final digit is rounded. ++ */ ++ if (cdigits+czero+1 < precision+clead) ++ fp = modf(fp, &d); ++ ++ else ++ { ++ d = floor(fp + .5); ++ ++ if (d > 9) ++ { ++ /* Rounding up to 10, handle that here. */ ++ if (czero > 0) ++ { ++ --czero; d = 1; ++ if (cdigits == 0) --clead; ++ } ++ else ++ { ++ while (cdigits > 0 && d > 9) ++ { ++ int ch = *--ascii; ++ ++ if (exp_b10 != (-1)) ++ ++exp_b10; ++ ++ else if (ch == 46) ++ { ++ ch = *--ascii; ++size; ++ /* Advance exp_b10 to '1', so that the ++ * decimal point happens after the ++ * previous digit. ++ */ ++ exp_b10 = 1; ++ } ++ ++ --cdigits; ++ d = ch - 47; /* I.e. 1+(ch-48) */ ++ } ++ ++ /* Did we reach the beginning? If so adjust the ++ * exponent but take into account the leading ++ * decimal point. ++ */ ++ if (d > 9) /* cdigits == 0 */ ++ { ++ if (exp_b10 == (-1)) ++ { ++ /* Leading decimal point (plus zeros?), if ++ * we lose the decimal point here it must ++ * be reentered below. ++ */ ++ int ch = *--ascii; ++ ++ if (ch == 46) ++ { ++ ++size; exp_b10 = 1; ++ } ++ ++ /* Else lost a leading zero, so 'exp_b10' is ++ * still ok at (-1) ++ */ ++ } ++ else ++ ++exp_b10; ++ ++ /* In all cases we output a '1' */ ++ d = 1; ++ } ++ } ++ } ++ fp = 0; /* Guarantees termination below. */ ++ } ++ ++ if (d == 0) ++ { ++ ++czero; ++ if (cdigits == 0) ++clead; ++ } ++ else ++ { ++ /* Included embedded zeros in the digit count. */ ++ cdigits += czero - clead; ++ clead = 0; ++ ++ while (czero > 0) ++ { ++ /* exp_b10 == (-1) means we just output the decimal ++ * place - after the DP don't adjust 'exp_b10' any ++ * more! ++ */ ++ if (exp_b10 != (-1)) ++ { ++ if (exp_b10 == 0) ++ { ++ *ascii++ = 46; --size; ++ } ++ /* PLUS 1: TOTAL 4 */ ++ --exp_b10; ++ } ++ *ascii++ = 48; --czero; ++ } ++ ++ if (exp_b10 != (-1)) ++ { ++ if (exp_b10 == 0) ++ { ++ *ascii++ = 46; --size; /* counted above */ ++ } ++ ++ --exp_b10; ++ } ++ *ascii++ = (char)(48 + (int)d); ++cdigits; ++ } ++ } ++ while (cdigits+czero < precision+clead && fp > DBL_MIN); ++ ++ /* The total output count (max) is now 4+precision */ ++ ++ /* Check for an exponent, if we don't need one we are ++ * done and just need to terminate the string. At this ++ * point, exp_b10==(-1) is effectively a flag: it got ++ * to '-1' because of the decrement, after outputting ++ * the decimal point above. (The exponent required is ++ * *not* -1.) ++ */ ++ if (exp_b10 >= (-1) && exp_b10 <= 2) ++ { ++ /* The following only happens if we didn't output the ++ * leading zeros above for negative exponent, so this ++ * doesn't add to the digit requirement. Note that the ++ * two zeros here can only be output if the two leading ++ * zeros were *not* output, so this doesn't increase ++ * the output count. ++ */ ++ while (exp_b10-- > 0) *ascii++ = 48; ++ ++ *ascii = 0; ++ ++ /* Total buffer requirement (including the '\0') is ++ * 5+precision - see check at the start. ++ */ ++ return; ++ } ++ ++ /* Here if an exponent is required, adjust size for ++ * the digits we output but did not count. The total ++ * digit output here so far is at most 1+precision - no ++ * decimal point and no leading or trailing zeros have ++ * been output. ++ */ ++ size -= cdigits; ++ ++ *ascii++ = 69; --size; /* 'E': PLUS 1 TOTAL 2+precision */ ++ ++ /* The following use of an unsigned temporary avoids ambiguities in ++ * the signed arithmetic on exp_b10 and permits GCC at least to do ++ * better optimization. ++ */ ++ { ++ unsigned int uexp_b10; ++ ++ if (exp_b10 < 0) ++ { ++ *ascii++ = 45; --size; /* '-': PLUS 1 TOTAL 3+precision */ ++ uexp_b10 = 0U-exp_b10; ++ } ++ ++ else ++ uexp_b10 = 0U+exp_b10; ++ ++ cdigits = 0; ++ ++ while (uexp_b10 > 0) ++ { ++ exponent[cdigits++] = (char)(48 + uexp_b10 % 10); ++ uexp_b10 /= 10; ++ } ++ } ++ ++ /* Need another size check here for the exponent digits, so ++ * this need not be considered above. ++ */ ++ if (size > cdigits) ++ { ++ while (cdigits > 0) *ascii++ = exponent[--cdigits]; ++ ++ *ascii = 0; ++ ++ return; ++ } ++ } ++ } ++ else if (!(fp >= DBL_MIN)) ++ { ++ *ascii++ = 48; /* '0' */ ++ *ascii = 0; ++ return; ++ } ++ else ++ { ++ *ascii++ = 105; /* 'i' */ ++ *ascii++ = 110; /* 'n' */ ++ *ascii++ = 102; /* 'f' */ ++ *ascii = 0; ++ return; ++ } ++ } ++ ++ /* Here on buffer too small. */ ++ png_error(png_ptr, "ASCII conversion buffer too small"); ++} ++#if GCC_STRICT_OVERFLOW ++#pragma GCC diagnostic pop ++#endif /* GCC_STRICT_OVERFLOW */ ++ ++# endif /* FLOATING_POINT */ ++ ++# ifdef PNG_FIXED_POINT_SUPPORTED ++/* Function to format a fixed point value in ASCII. ++ */ ++void /* PRIVATE */ ++png_ascii_from_fixed(png_const_structrp png_ptr, png_charp ascii, ++ size_t size, png_fixed_point fp) ++{ ++ /* Require space for 10 decimal digits, a decimal point, a minus sign and a ++ * trailing \0, 13 characters: ++ */ ++ if (size > 12) ++ { ++ png_uint_32 num; ++ ++ /* Avoid overflow here on the minimum integer. */ ++ if (fp < 0) ++ { ++ *ascii++ = 45; num = (png_uint_32)(-fp); ++ } ++ else ++ num = (png_uint_32)fp; ++ ++ if (num <= 0x80000000) /* else overflowed */ ++ { ++ unsigned int ndigits = 0, first = 16 /* flag value */; ++ char digits[10]; ++ ++ while (num) ++ { ++ /* Split the low digit off num: */ ++ unsigned int tmp = num/10; ++ num -= tmp*10; ++ digits[ndigits++] = (char)(48 + num); ++ /* Record the first non-zero digit, note that this is a number ++ * starting at 1, it's not actually the array index. ++ */ ++ if (first == 16 && num > 0) ++ first = ndigits; ++ num = tmp; ++ } ++ ++ if (ndigits > 0) ++ { ++ while (ndigits > 5) *ascii++ = digits[--ndigits]; ++ /* The remaining digits are fractional digits, ndigits is '5' or ++ * smaller at this point. It is certainly not zero. Check for a ++ * non-zero fractional digit: ++ */ ++ if (first <= 5) ++ { ++ unsigned int i; ++ *ascii++ = 46; /* decimal point */ ++ /* ndigits may be <5 for small numbers, output leading zeros ++ * then ndigits digits to first: ++ */ ++ i = 5; ++ while (ndigits < i) ++ { ++ *ascii++ = 48; --i; ++ } ++ while (ndigits >= first) *ascii++ = digits[--ndigits]; ++ /* Don't output the trailing zeros! */ ++ } ++ } ++ else ++ *ascii++ = 48; ++ ++ /* And null terminate the string: */ ++ *ascii = 0; ++ return; ++ } ++ } ++ ++ /* Here on buffer too small. */ ++ png_error(png_ptr, "ASCII conversion buffer too small"); ++} ++# endif /* FIXED_POINT */ ++#endif /* SCAL */ ++ ++#if defined(PNG_FLOATING_POINT_SUPPORTED) && \ ++ !defined(PNG_FIXED_POINT_MACRO_SUPPORTED) && \ ++ (defined(PNG_gAMA_SUPPORTED) || defined(PNG_cHRM_SUPPORTED) || \ ++ defined(PNG_sCAL_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) || \ ++ defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)) || \ ++ (defined(PNG_sCAL_SUPPORTED) && \ ++ defined(PNG_FLOATING_ARITHMETIC_SUPPORTED)) ++png_fixed_point ++png_fixed(png_const_structrp png_ptr, double fp, png_const_charp text) ++{ ++ double r = floor(100000 * fp + .5); ++ ++ if (r > 2147483647. || r < -2147483648.) ++ png_fixed_error(png_ptr, text); ++ ++# ifndef PNG_ERROR_TEXT_SUPPORTED ++ PNG_UNUSED(text) ++# endif ++ ++ return (png_fixed_point)r; ++} ++#endif ++ ++#if defined(PNG_GAMMA_SUPPORTED) || defined(PNG_COLORSPACE_SUPPORTED) ||\ ++ defined(PNG_INCH_CONVERSIONS_SUPPORTED) || defined(PNG_READ_pHYs_SUPPORTED) ++/* muldiv functions */ ++/* This API takes signed arguments and rounds the result to the nearest ++ * integer (or, for a fixed point number - the standard argument - to ++ * the nearest .00001). Overflow and divide by zero are signalled in ++ * the result, a boolean - true on success, false on overflow. ++ */ ++#if GCC_STRICT_OVERFLOW /* from above */ ++/* It is not obvious which comparison below gets optimized in such a way that ++ * signed overflow would change the result; looking through the code does not ++ * reveal any tests which have the form GCC complains about, so presumably the ++ * optimizer is moving an add or subtract into the 'if' somewhere. ++ */ ++#pragma GCC diagnostic push ++#pragma GCC diagnostic warning "-Wstrict-overflow=2" ++#endif /* GCC_STRICT_OVERFLOW */ ++int ++png_muldiv(png_fixed_point_p res, png_fixed_point a, png_int_32 times, ++ png_int_32 divisor) ++{ ++ /* Return a * times / divisor, rounded. */ ++ if (divisor != 0) ++ { ++ if (a == 0 || times == 0) ++ { ++ *res = 0; ++ return 1; ++ } ++ else ++ { ++#ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED ++ double r = a; ++ r *= times; ++ r /= divisor; ++ r = floor(r+.5); ++ ++ /* A png_fixed_point is a 32-bit integer. */ ++ if (r <= 2147483647. && r >= -2147483648.) ++ { ++ *res = (png_fixed_point)r; ++ return 1; ++ } ++#else ++ int negative = 0; ++ png_uint_32 A, T, D; ++ png_uint_32 s16, s32, s00; ++ ++ if (a < 0) ++ negative = 1, A = -a; ++ else ++ A = a; ++ ++ if (times < 0) ++ negative = !negative, T = -times; ++ else ++ T = times; ++ ++ if (divisor < 0) ++ negative = !negative, D = -divisor; ++ else ++ D = divisor; ++ ++ /* Following can't overflow because the arguments only ++ * have 31 bits each, however the result may be 32 bits. ++ */ ++ s16 = (A >> 16) * (T & 0xffff) + ++ (A & 0xffff) * (T >> 16); ++ /* Can't overflow because the a*times bit is only 30 ++ * bits at most. ++ */ ++ s32 = (A >> 16) * (T >> 16) + (s16 >> 16); ++ s00 = (A & 0xffff) * (T & 0xffff); ++ ++ s16 = (s16 & 0xffff) << 16; ++ s00 += s16; ++ ++ if (s00 < s16) ++ ++s32; /* carry */ ++ ++ if (s32 < D) /* else overflow */ ++ { ++ /* s32.s00 is now the 64-bit product, do a standard ++ * division, we know that s32 < D, so the maximum ++ * required shift is 31. ++ */ ++ int bitshift = 32; ++ png_fixed_point result = 0; /* NOTE: signed */ ++ ++ while (--bitshift >= 0) ++ { ++ png_uint_32 d32, d00; ++ ++ if (bitshift > 0) ++ d32 = D >> (32-bitshift), d00 = D << bitshift; ++ ++ else ++ d32 = 0, d00 = D; ++ ++ if (s32 > d32) ++ { ++ if (s00 < d00) --s32; /* carry */ ++ s32 -= d32, s00 -= d00, result += 1<= d00) ++ s32 = 0, s00 -= d00, result += 1<= (D >> 1)) ++ ++result; ++ ++ if (negative != 0) ++ result = -result; ++ ++ /* Check for overflow. */ ++ if ((negative != 0 && result <= 0) || ++ (negative == 0 && result >= 0)) ++ { ++ *res = result; ++ return 1; ++ } ++ } ++#endif ++ } ++ } ++ ++ return 0; ++} ++#if GCC_STRICT_OVERFLOW ++#pragma GCC diagnostic pop ++#endif /* GCC_STRICT_OVERFLOW */ ++#endif /* READ_GAMMA || INCH_CONVERSIONS */ ++ ++#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_INCH_CONVERSIONS_SUPPORTED) ++/* The following is for when the caller doesn't much care about the ++ * result. ++ */ ++png_fixed_point ++png_muldiv_warn(png_const_structrp png_ptr, png_fixed_point a, png_int_32 times, ++ png_int_32 divisor) ++{ ++ png_fixed_point result; ++ ++ if (png_muldiv(&result, a, times, divisor) != 0) ++ return result; ++ ++ png_warning(png_ptr, "fixed point overflow ignored"); ++ return 0; ++} ++#endif ++ ++#ifdef PNG_GAMMA_SUPPORTED /* more fixed point functions for gamma */ ++/* Calculate a reciprocal, return 0 on div-by-zero or overflow. */ ++png_fixed_point ++png_reciprocal(png_fixed_point a) ++{ ++#ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED ++ double r = floor(1E10/a+.5); ++ ++ if (r <= 2147483647. && r >= -2147483648.) ++ return (png_fixed_point)r; ++#else ++ png_fixed_point res; ++ ++ if (png_muldiv(&res, 100000, 100000, a) != 0) ++ return res; ++#endif ++ ++ return 0; /* error/overflow */ ++} ++ ++/* This is the shared test on whether a gamma value is 'significant' - whether ++ * it is worth doing gamma correction. ++ */ ++int /* PRIVATE */ ++png_gamma_significant(png_fixed_point gamma_val) ++{ ++ return gamma_val < PNG_FP_1 - PNG_GAMMA_THRESHOLD_FIXED || ++ gamma_val > PNG_FP_1 + PNG_GAMMA_THRESHOLD_FIXED; ++} ++#endif ++ ++#ifdef PNG_READ_GAMMA_SUPPORTED ++#ifdef PNG_16BIT_SUPPORTED ++/* A local convenience routine. */ ++static png_fixed_point ++png_product2(png_fixed_point a, png_fixed_point b) ++{ ++ /* The required result is 1/a * 1/b; the following preserves accuracy. */ ++#ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED ++ double r = a * 1E-5; ++ r *= b; ++ r = floor(r+.5); ++ ++ if (r <= 2147483647. && r >= -2147483648.) ++ return (png_fixed_point)r; ++#else ++ png_fixed_point res; ++ ++ if (png_muldiv(&res, a, b, 100000) != 0) ++ return res; ++#endif ++ ++ return 0; /* overflow */ ++} ++#endif /* 16BIT */ ++ ++/* The inverse of the above. */ ++png_fixed_point ++png_reciprocal2(png_fixed_point a, png_fixed_point b) ++{ ++ /* The required result is 1/a * 1/b; the following preserves accuracy. */ ++#ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED ++ if (a != 0 && b != 0) ++ { ++ double r = 1E15/a; ++ r /= b; ++ r = floor(r+.5); ++ ++ if (r <= 2147483647. && r >= -2147483648.) ++ return (png_fixed_point)r; ++ } ++#else ++ /* This may overflow because the range of png_fixed_point isn't symmetric, ++ * but this API is only used for the product of file and screen gamma so it ++ * doesn't matter that the smallest number it can produce is 1/21474, not ++ * 1/100000 ++ */ ++ png_fixed_point res = png_product2(a, b); ++ ++ if (res != 0) ++ return png_reciprocal(res); ++#endif ++ ++ return 0; /* overflow */ ++} ++#endif /* READ_GAMMA */ ++ ++#ifdef PNG_READ_GAMMA_SUPPORTED /* gamma table code */ ++#ifndef PNG_FLOATING_ARITHMETIC_SUPPORTED ++/* Fixed point gamma. ++ * ++ * The code to calculate the tables used below can be found in the shell script ++ * contrib/tools/intgamma.sh ++ * ++ * To calculate gamma this code implements fast log() and exp() calls using only ++ * fixed point arithmetic. This code has sufficient precision for either 8-bit ++ * or 16-bit sample values. ++ * ++ * The tables used here were calculated using simple 'bc' programs, but C double ++ * precision floating point arithmetic would work fine. ++ * ++ * 8-bit log table ++ * This is a table of -log(value/255)/log(2) for 'value' in the range 128 to ++ * 255, so it's the base 2 logarithm of a normalized 8-bit floating point ++ * mantissa. The numbers are 32-bit fractions. ++ */ ++static const png_uint_32 ++png_8bit_l2[128] = ++{ ++ 4270715492U, 4222494797U, 4174646467U, 4127164793U, 4080044201U, 4033279239U, ++ 3986864580U, 3940795015U, 3895065449U, 3849670902U, 3804606499U, 3759867474U, ++ 3715449162U, 3671346997U, 3627556511U, 3584073329U, 3540893168U, 3498011834U, ++ 3455425220U, 3413129301U, 3371120137U, 3329393864U, 3287946700U, 3246774933U, ++ 3205874930U, 3165243125U, 3124876025U, 3084770202U, 3044922296U, 3005329011U, ++ 2965987113U, 2926893432U, 2888044853U, 2849438323U, 2811070844U, 2772939474U, ++ 2735041326U, 2697373562U, 2659933400U, 2622718104U, 2585724991U, 2548951424U, ++ 2512394810U, 2476052606U, 2439922311U, 2404001468U, 2368287663U, 2332778523U, ++ 2297471715U, 2262364947U, 2227455964U, 2192742551U, 2158222529U, 2123893754U, ++ 2089754119U, 2055801552U, 2022034013U, 1988449497U, 1955046031U, 1921821672U, ++ 1888774511U, 1855902668U, 1823204291U, 1790677560U, 1758320682U, 1726131893U, ++ 1694109454U, 1662251657U, 1630556815U, 1599023271U, 1567649391U, 1536433567U, ++ 1505374214U, 1474469770U, 1443718700U, 1413119487U, 1382670639U, 1352370686U, ++ 1322218179U, 1292211689U, 1262349810U, 1232631153U, 1203054352U, 1173618059U, ++ 1144320946U, 1115161701U, 1086139034U, 1057251672U, 1028498358U, 999877854U, ++ 971388940U, 943030410U, 914801076U, 886699767U, 858725327U, 830876614U, ++ 803152505U, 775551890U, 748073672U, 720716771U, 693480120U, 666362667U, ++ 639363374U, 612481215U, 585715177U, 559064263U, 532527486U, 506103872U, ++ 479792461U, 453592303U, 427502463U, 401522014U, 375650043U, 349885648U, ++ 324227938U, 298676034U, 273229066U, 247886176U, 222646516U, 197509248U, ++ 172473545U, 147538590U, 122703574U, 97967701U, 73330182U, 48790236U, ++ 24347096U, 0U ++ ++#if 0 ++ /* The following are the values for 16-bit tables - these work fine for the ++ * 8-bit conversions but produce very slightly larger errors in the 16-bit ++ * log (about 1.2 as opposed to 0.7 absolute error in the final value). To ++ * use these all the shifts below must be adjusted appropriately. ++ */ ++ 65166, 64430, 63700, 62976, 62257, 61543, 60835, 60132, 59434, 58741, 58054, ++ 57371, 56693, 56020, 55352, 54689, 54030, 53375, 52726, 52080, 51439, 50803, ++ 50170, 49542, 48918, 48298, 47682, 47070, 46462, 45858, 45257, 44661, 44068, ++ 43479, 42894, 42312, 41733, 41159, 40587, 40020, 39455, 38894, 38336, 37782, ++ 37230, 36682, 36137, 35595, 35057, 34521, 33988, 33459, 32932, 32408, 31887, ++ 31369, 30854, 30341, 29832, 29325, 28820, 28319, 27820, 27324, 26830, 26339, ++ 25850, 25364, 24880, 24399, 23920, 23444, 22970, 22499, 22029, 21562, 21098, ++ 20636, 20175, 19718, 19262, 18808, 18357, 17908, 17461, 17016, 16573, 16132, ++ 15694, 15257, 14822, 14390, 13959, 13530, 13103, 12678, 12255, 11834, 11415, ++ 10997, 10582, 10168, 9756, 9346, 8937, 8531, 8126, 7723, 7321, 6921, 6523, ++ 6127, 5732, 5339, 4947, 4557, 4169, 3782, 3397, 3014, 2632, 2251, 1872, 1495, ++ 1119, 744, 372 ++#endif ++}; ++ ++static png_int_32 ++png_log8bit(unsigned int x) ++{ ++ unsigned int lg2 = 0; ++ /* Each time 'x' is multiplied by 2, 1 must be subtracted off the final log, ++ * because the log is actually negate that means adding 1. The final ++ * returned value thus has the range 0 (for 255 input) to 7.994 (for 1 ++ * input), return -1 for the overflow (log 0) case, - so the result is ++ * always at most 19 bits. ++ */ ++ if ((x &= 0xff) == 0) ++ return -1; ++ ++ if ((x & 0xf0) == 0) ++ lg2 = 4, x <<= 4; ++ ++ if ((x & 0xc0) == 0) ++ lg2 += 2, x <<= 2; ++ ++ if ((x & 0x80) == 0) ++ lg2 += 1, x <<= 1; ++ ++ /* result is at most 19 bits, so this cast is safe: */ ++ return (png_int_32)((lg2 << 16) + ((png_8bit_l2[x-128]+32768)>>16)); ++} ++ ++/* The above gives exact (to 16 binary places) log2 values for 8-bit images, ++ * for 16-bit images we use the most significant 8 bits of the 16-bit value to ++ * get an approximation then multiply the approximation by a correction factor ++ * determined by the remaining up to 8 bits. This requires an additional step ++ * in the 16-bit case. ++ * ++ * We want log2(value/65535), we have log2(v'/255), where: ++ * ++ * value = v' * 256 + v'' ++ * = v' * f ++ * ++ * So f is value/v', which is equal to (256+v''/v') since v' is in the range 128 ++ * to 255 and v'' is in the range 0 to 255 f will be in the range 256 to less ++ * than 258. The final factor also needs to correct for the fact that our 8-bit ++ * value is scaled by 255, whereas the 16-bit values must be scaled by 65535. ++ * ++ * This gives a final formula using a calculated value 'x' which is value/v' and ++ * scaling by 65536 to match the above table: ++ * ++ * log2(x/257) * 65536 ++ * ++ * Since these numbers are so close to '1' we can use simple linear ++ * interpolation between the two end values 256/257 (result -368.61) and 258/257 ++ * (result 367.179). The values used below are scaled by a further 64 to give ++ * 16-bit precision in the interpolation: ++ * ++ * Start (256): -23591 ++ * Zero (257): 0 ++ * End (258): 23499 ++ */ ++#ifdef PNG_16BIT_SUPPORTED ++static png_int_32 ++png_log16bit(png_uint_32 x) ++{ ++ unsigned int lg2 = 0; ++ ++ /* As above, but now the input has 16 bits. */ ++ if ((x &= 0xffff) == 0) ++ return -1; ++ ++ if ((x & 0xff00) == 0) ++ lg2 = 8, x <<= 8; ++ ++ if ((x & 0xf000) == 0) ++ lg2 += 4, x <<= 4; ++ ++ if ((x & 0xc000) == 0) ++ lg2 += 2, x <<= 2; ++ ++ if ((x & 0x8000) == 0) ++ lg2 += 1, x <<= 1; ++ ++ /* Calculate the base logarithm from the top 8 bits as a 28-bit fractional ++ * value. ++ */ ++ lg2 <<= 28; ++ lg2 += (png_8bit_l2[(x>>8)-128]+8) >> 4; ++ ++ /* Now we need to interpolate the factor, this requires a division by the top ++ * 8 bits. Do this with maximum precision. ++ */ ++ x = ((x << 16) + (x >> 9)) / (x >> 8); ++ ++ /* Since we divided by the top 8 bits of 'x' there will be a '1' at 1<<24, ++ * the value at 1<<16 (ignoring this) will be 0 or 1; this gives us exactly ++ * 16 bits to interpolate to get the low bits of the result. Round the ++ * answer. Note that the end point values are scaled by 64 to retain overall ++ * precision and that 'lg2' is current scaled by an extra 12 bits, so adjust ++ * the overall scaling by 6-12. Round at every step. ++ */ ++ x -= 1U << 24; ++ ++ if (x <= 65536U) /* <= '257' */ ++ lg2 += ((23591U * (65536U-x)) + (1U << (16+6-12-1))) >> (16+6-12); ++ ++ else ++ lg2 -= ((23499U * (x-65536U)) + (1U << (16+6-12-1))) >> (16+6-12); ++ ++ /* Safe, because the result can't have more than 20 bits: */ ++ return (png_int_32)((lg2 + 2048) >> 12); ++} ++#endif /* 16BIT */ ++ ++/* The 'exp()' case must invert the above, taking a 20-bit fixed point ++ * logarithmic value and returning a 16 or 8-bit number as appropriate. In ++ * each case only the low 16 bits are relevant - the fraction - since the ++ * integer bits (the top 4) simply determine a shift. ++ * ++ * The worst case is the 16-bit distinction between 65535 and 65534. This ++ * requires perhaps spurious accuracy in the decoding of the logarithm to ++ * distinguish log2(65535/65534.5) - 10^-5 or 17 bits. There is little chance ++ * of getting this accuracy in practice. ++ * ++ * To deal with this the following exp() function works out the exponent of the ++ * fractional part of the logarithm by using an accurate 32-bit value from the ++ * top four fractional bits then multiplying in the remaining bits. ++ */ ++static const png_uint_32 ++png_32bit_exp[16] = ++{ ++ /* NOTE: the first entry is deliberately set to the maximum 32-bit value. */ ++ 4294967295U, 4112874773U, 3938502376U, 3771522796U, 3611622603U, 3458501653U, ++ 3311872529U, 3171459999U, 3037000500U, 2908241642U, 2784941738U, 2666869345U, ++ 2553802834U, 2445529972U, 2341847524U, 2242560872U ++}; ++ ++/* Adjustment table; provided to explain the numbers in the code below. */ ++#if 0 ++for (i=11;i>=0;--i){ print i, " ", (1 - e(-(2^i)/65536*l(2))) * 2^(32-i), "\n"} ++ 11 44937.64284865548751208448 ++ 10 45180.98734845585101160448 ++ 9 45303.31936980687359311872 ++ 8 45364.65110595323018870784 ++ 7 45395.35850361789624614912 ++ 6 45410.72259715102037508096 ++ 5 45418.40724413220722311168 ++ 4 45422.25021786898173001728 ++ 3 45424.17186732298419044352 ++ 2 45425.13273269940811464704 ++ 1 45425.61317555035558641664 ++ 0 45425.85339951654943850496 ++#endif ++ ++static png_uint_32 ++png_exp(png_fixed_point x) ++{ ++ if (x > 0 && x <= 0xfffff) /* Else overflow or zero (underflow) */ ++ { ++ /* Obtain a 4-bit approximation */ ++ png_uint_32 e = png_32bit_exp[(x >> 12) & 0x0f]; ++ ++ /* Incorporate the low 12 bits - these decrease the returned value by ++ * multiplying by a number less than 1 if the bit is set. The multiplier ++ * is determined by the above table and the shift. Notice that the values ++ * converge on 45426 and this is used to allow linear interpolation of the ++ * low bits. ++ */ ++ if (x & 0x800) ++ e -= (((e >> 16) * 44938U) + 16U) >> 5; ++ ++ if (x & 0x400) ++ e -= (((e >> 16) * 45181U) + 32U) >> 6; ++ ++ if (x & 0x200) ++ e -= (((e >> 16) * 45303U) + 64U) >> 7; ++ ++ if (x & 0x100) ++ e -= (((e >> 16) * 45365U) + 128U) >> 8; ++ ++ if (x & 0x080) ++ e -= (((e >> 16) * 45395U) + 256U) >> 9; ++ ++ if (x & 0x040) ++ e -= (((e >> 16) * 45410U) + 512U) >> 10; ++ ++ /* And handle the low 6 bits in a single block. */ ++ e -= (((e >> 16) * 355U * (x & 0x3fU)) + 256U) >> 9; ++ ++ /* Handle the upper bits of x. */ ++ e >>= x >> 16; ++ return e; ++ } ++ ++ /* Check for overflow */ ++ if (x <= 0) ++ return png_32bit_exp[0]; ++ ++ /* Else underflow */ ++ return 0; ++} ++ ++static png_byte ++png_exp8bit(png_fixed_point lg2) ++{ ++ /* Get a 32-bit value: */ ++ png_uint_32 x = png_exp(lg2); ++ ++ /* Convert the 32-bit value to 0..255 by multiplying by 256-1. Note that the ++ * second, rounding, step can't overflow because of the first, subtraction, ++ * step. ++ */ ++ x -= x >> 8; ++ return (png_byte)(((x + 0x7fffffU) >> 24) & 0xff); ++} ++ ++#ifdef PNG_16BIT_SUPPORTED ++static png_uint_16 ++png_exp16bit(png_fixed_point lg2) ++{ ++ /* Get a 32-bit value: */ ++ png_uint_32 x = png_exp(lg2); ++ ++ /* Convert the 32-bit value to 0..65535 by multiplying by 65536-1: */ ++ x -= x >> 16; ++ return (png_uint_16)((x + 32767U) >> 16); ++} ++#endif /* 16BIT */ ++#endif /* FLOATING_ARITHMETIC */ ++ ++png_byte ++png_gamma_8bit_correct(unsigned int value, png_fixed_point gamma_val) ++{ ++ if (value > 0 && value < 255) ++ { ++# ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED ++ /* 'value' is unsigned, ANSI-C90 requires the compiler to correctly ++ * convert this to a floating point value. This includes values that ++ * would overflow if 'value' were to be converted to 'int'. ++ * ++ * Apparently GCC, however, does an intermediate conversion to (int) ++ * on some (ARM) but not all (x86) platforms, possibly because of ++ * hardware FP limitations. (E.g. if the hardware conversion always ++ * assumes the integer register contains a signed value.) This results ++ * in ANSI-C undefined behavior for large values. ++ * ++ * Other implementations on the same machine might actually be ANSI-C90 ++ * conformant and therefore compile spurious extra code for the large ++ * values. ++ * ++ * We can be reasonably sure that an unsigned to float conversion ++ * won't be faster than an int to float one. Therefore this code ++ * assumes responsibility for the undefined behavior, which it knows ++ * can't happen because of the check above. ++ * ++ * Note the argument to this routine is an (unsigned int) because, on ++ * 16-bit platforms, it is assigned a value which might be out of ++ * range for an (int); that would result in undefined behavior in the ++ * caller if the *argument* ('value') were to be declared (int). ++ */ ++ double r = floor(255*pow((int)/*SAFE*/value/255.,gamma_val*.00001)+.5); ++ return (png_byte)r; ++# else ++ png_int_32 lg2 = png_log8bit(value); ++ png_fixed_point res; ++ ++ if (png_muldiv(&res, gamma_val, lg2, PNG_FP_1) != 0) ++ return png_exp8bit(res); ++ ++ /* Overflow. */ ++ value = 0; ++# endif ++ } ++ ++ return (png_byte)(value & 0xff); ++} ++ ++#ifdef PNG_16BIT_SUPPORTED ++png_uint_16 ++png_gamma_16bit_correct(unsigned int value, png_fixed_point gamma_val) ++{ ++ if (value > 0 && value < 65535) ++ { ++# ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED ++ /* The same (unsigned int)->(double) constraints apply here as above, ++ * however in this case the (unsigned int) to (int) conversion can ++ * overflow on an ANSI-C90 compliant system so the cast needs to ensure ++ * that this is not possible. ++ */ ++ double r = floor(65535*pow((png_int_32)value/65535., ++ gamma_val*.00001)+.5); ++ return (png_uint_16)r; ++# else ++ png_int_32 lg2 = png_log16bit(value); ++ png_fixed_point res; ++ ++ if (png_muldiv(&res, gamma_val, lg2, PNG_FP_1) != 0) ++ return png_exp16bit(res); ++ ++ /* Overflow. */ ++ value = 0; ++# endif ++ } ++ ++ return (png_uint_16)value; ++} ++#endif /* 16BIT */ ++ ++/* This does the right thing based on the bit_depth field of the ++ * png_struct, interpreting values as 8-bit or 16-bit. While the result ++ * is nominally a 16-bit value if bit depth is 8 then the result is ++ * 8-bit (as are the arguments.) ++ */ ++png_uint_16 /* PRIVATE */ ++png_gamma_correct(png_structrp png_ptr, unsigned int value, ++ png_fixed_point gamma_val) ++{ ++ if (png_ptr->bit_depth == 8) ++ return png_gamma_8bit_correct(value, gamma_val); ++ ++#ifdef PNG_16BIT_SUPPORTED ++ else ++ return png_gamma_16bit_correct(value, gamma_val); ++#else ++ /* should not reach this */ ++ return 0; ++#endif /* 16BIT */ ++} ++ ++#ifdef PNG_16BIT_SUPPORTED ++/* Internal function to build a single 16-bit table - the table consists of ++ * 'num' 256 entry subtables, where 'num' is determined by 'shift' - the amount ++ * to shift the input values right (or 16-number_of_signifiant_bits). ++ * ++ * The caller is responsible for ensuring that the table gets cleaned up on ++ * png_error (i.e. if one of the mallocs below fails) - i.e. the *table argument ++ * should be somewhere that will be cleaned. ++ */ ++static void ++png_build_16bit_table(png_structrp png_ptr, png_uint_16pp *ptable, ++ unsigned int shift, png_fixed_point gamma_val) ++{ ++ /* Various values derived from 'shift': */ ++ unsigned int num = 1U << (8U - shift); ++#ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED ++ /* CSE the division and work round wacky GCC warnings (see the comments ++ * in png_gamma_8bit_correct for where these come from.) ++ */ ++ double fmax = 1.0 / (((png_int_32)1 << (16U - shift)) - 1); ++#endif ++ unsigned int max = (1U << (16U - shift)) - 1U; ++ unsigned int max_by_2 = 1U << (15U - shift); ++ unsigned int i; ++ ++ png_uint_16pp table = *ptable = ++ (png_uint_16pp)png_calloc(png_ptr, num * (sizeof (png_uint_16p))); ++ ++ for (i = 0; i < num; i++) ++ { ++ png_uint_16p sub_table = table[i] = ++ (png_uint_16p)png_malloc(png_ptr, 256 * (sizeof (png_uint_16))); ++ ++ /* The 'threshold' test is repeated here because it can arise for one of ++ * the 16-bit tables even if the others don't hit it. ++ */ ++ if (png_gamma_significant(gamma_val) != 0) ++ { ++ /* The old code would overflow at the end and this would cause the ++ * 'pow' function to return a result >1, resulting in an ++ * arithmetic error. This code follows the spec exactly; ig is ++ * the recovered input sample, it always has 8-16 bits. ++ * ++ * We want input * 65535/max, rounded, the arithmetic fits in 32 ++ * bits (unsigned) so long as max <= 32767. ++ */ ++ unsigned int j; ++ for (j = 0; j < 256; j++) ++ { ++ png_uint_32 ig = (j << (8-shift)) + i; ++# ifdef PNG_FLOATING_ARITHMETIC_SUPPORTED ++ /* Inline the 'max' scaling operation: */ ++ /* See png_gamma_8bit_correct for why the cast to (int) is ++ * required here. ++ */ ++ double d = floor(65535.*pow(ig*fmax, gamma_val*.00001)+.5); ++ sub_table[j] = (png_uint_16)d; ++# else ++ if (shift != 0) ++ ig = (ig * 65535U + max_by_2)/max; ++ ++ sub_table[j] = png_gamma_16bit_correct(ig, gamma_val); ++# endif ++ } ++ } ++ else ++ { ++ /* We must still build a table, but do it the fast way. */ ++ unsigned int j; ++ ++ for (j = 0; j < 256; j++) ++ { ++ png_uint_32 ig = (j << (8-shift)) + i; ++ ++ if (shift != 0) ++ ig = (ig * 65535U + max_by_2)/max; ++ ++ sub_table[j] = (png_uint_16)ig; ++ } ++ } ++ } ++} ++ ++/* NOTE: this function expects the *inverse* of the overall gamma transformation ++ * required. ++ */ ++static void ++png_build_16to8_table(png_structrp png_ptr, png_uint_16pp *ptable, ++ unsigned int shift, png_fixed_point gamma_val) ++{ ++ unsigned int num = 1U << (8U - shift); ++ unsigned int max = (1U << (16U - shift))-1U; ++ unsigned int i; ++ png_uint_32 last; ++ ++ png_uint_16pp table = *ptable = ++ (png_uint_16pp)png_calloc(png_ptr, num * (sizeof (png_uint_16p))); ++ ++ /* 'num' is the number of tables and also the number of low bits of low ++ * bits of the input 16-bit value used to select a table. Each table is ++ * itself indexed by the high 8 bits of the value. ++ */ ++ for (i = 0; i < num; i++) ++ table[i] = (png_uint_16p)png_malloc(png_ptr, ++ 256 * (sizeof (png_uint_16))); ++ ++ /* 'gamma_val' is set to the reciprocal of the value calculated above, so ++ * pow(out,g) is an *input* value. 'last' is the last input value set. ++ * ++ * In the loop 'i' is used to find output values. Since the output is ++ * 8-bit there are only 256 possible values. The tables are set up to ++ * select the closest possible output value for each input by finding ++ * the input value at the boundary between each pair of output values ++ * and filling the table up to that boundary with the lower output ++ * value. ++ * ++ * The boundary values are 0.5,1.5..253.5,254.5. Since these are 9-bit ++ * values the code below uses a 16-bit value in i; the values start at ++ * 128.5 (for 0.5) and step by 257, for a total of 254 values (the last ++ * entries are filled with 255). Start i at 128 and fill all 'last' ++ * table entries <= 'max' ++ */ ++ last = 0; ++ for (i = 0; i < 255; ++i) /* 8-bit output value */ ++ { ++ /* Find the corresponding maximum input value */ ++ png_uint_16 out = (png_uint_16)(i * 257U); /* 16-bit output value */ ++ ++ /* Find the boundary value in 16 bits: */ ++ png_uint_32 bound = png_gamma_16bit_correct(out+128U, gamma_val); ++ ++ /* Adjust (round) to (16-shift) bits: */ ++ bound = (bound * max + 32768U)/65535U + 1U; ++ ++ while (last < bound) ++ { ++ table[last & (0xffU >> shift)][last >> (8U - shift)] = out; ++ last++; ++ } ++ } ++ ++ /* And fill in the final entries. */ ++ while (last < (num << 8)) ++ { ++ table[last & (0xff >> shift)][last >> (8U - shift)] = 65535U; ++ last++; ++ } ++} ++#endif /* 16BIT */ ++ ++/* Build a single 8-bit table: same as the 16-bit case but much simpler (and ++ * typically much faster). Note that libpng currently does no sBIT processing ++ * (apparently contrary to the spec) so a 256-entry table is always generated. ++ */ ++static void ++png_build_8bit_table(png_structrp png_ptr, png_bytepp ptable, ++ png_fixed_point gamma_val) ++{ ++ unsigned int i; ++ png_bytep table = *ptable = (png_bytep)png_malloc(png_ptr, 256); ++ ++ if (png_gamma_significant(gamma_val) != 0) ++ for (i=0; i<256; i++) ++ table[i] = png_gamma_8bit_correct(i, gamma_val); ++ ++ else ++ for (i=0; i<256; ++i) ++ table[i] = (png_byte)(i & 0xff); ++} ++ ++/* Used from png_read_destroy and below to release the memory used by the gamma ++ * tables. ++ */ ++void /* PRIVATE */ ++png_destroy_gamma_table(png_structrp png_ptr) ++{ ++ png_free(png_ptr, png_ptr->gamma_table); ++ png_ptr->gamma_table = NULL; ++ ++#ifdef PNG_16BIT_SUPPORTED ++ if (png_ptr->gamma_16_table != NULL) ++ { ++ int i; ++ int istop = (1 << (8 - png_ptr->gamma_shift)); ++ for (i = 0; i < istop; i++) ++ { ++ png_free(png_ptr, png_ptr->gamma_16_table[i]); ++ } ++ png_free(png_ptr, png_ptr->gamma_16_table); ++ png_ptr->gamma_16_table = NULL; ++ } ++#endif /* 16BIT */ ++ ++#if defined(PNG_READ_BACKGROUND_SUPPORTED) || \ ++ defined(PNG_READ_ALPHA_MODE_SUPPORTED) || \ ++ defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) ++ png_free(png_ptr, png_ptr->gamma_from_1); ++ png_ptr->gamma_from_1 = NULL; ++ png_free(png_ptr, png_ptr->gamma_to_1); ++ png_ptr->gamma_to_1 = NULL; ++ ++#ifdef PNG_16BIT_SUPPORTED ++ if (png_ptr->gamma_16_from_1 != NULL) ++ { ++ int i; ++ int istop = (1 << (8 - png_ptr->gamma_shift)); ++ for (i = 0; i < istop; i++) ++ { ++ png_free(png_ptr, png_ptr->gamma_16_from_1[i]); ++ } ++ png_free(png_ptr, png_ptr->gamma_16_from_1); ++ png_ptr->gamma_16_from_1 = NULL; ++ } ++ if (png_ptr->gamma_16_to_1 != NULL) ++ { ++ int i; ++ int istop = (1 << (8 - png_ptr->gamma_shift)); ++ for (i = 0; i < istop; i++) ++ { ++ png_free(png_ptr, png_ptr->gamma_16_to_1[i]); ++ } ++ png_free(png_ptr, png_ptr->gamma_16_to_1); ++ png_ptr->gamma_16_to_1 = NULL; ++ } ++#endif /* 16BIT */ ++#endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */ ++} ++ ++/* We build the 8- or 16-bit gamma tables here. Note that for 16-bit ++ * tables, we don't make a full table if we are reducing to 8-bit in ++ * the future. Note also how the gamma_16 tables are segmented so that ++ * we don't need to allocate > 64K chunks for a full 16-bit table. ++ */ ++void /* PRIVATE */ ++png_build_gamma_table(png_structrp png_ptr, int bit_depth) ++{ ++ png_debug(1, "in png_build_gamma_table"); ++ ++ /* Remove any existing table; this copes with multiple calls to ++ * png_read_update_info. The warning is because building the gamma tables ++ * multiple times is a performance hit - it's harmless but the ability to ++ * call png_read_update_info() multiple times is new in 1.5.6 so it seems ++ * sensible to warn if the app introduces such a hit. ++ */ ++ if (png_ptr->gamma_table != NULL || png_ptr->gamma_16_table != NULL) ++ { ++ png_warning(png_ptr, "gamma table being rebuilt"); ++ png_destroy_gamma_table(png_ptr); ++ } ++ ++ if (bit_depth <= 8) ++ { ++ png_build_8bit_table(png_ptr, &png_ptr->gamma_table, ++ png_ptr->screen_gamma > 0 ? ++ png_reciprocal2(png_ptr->colorspace.gamma, ++ png_ptr->screen_gamma) : PNG_FP_1); ++ ++#if defined(PNG_READ_BACKGROUND_SUPPORTED) || \ ++ defined(PNG_READ_ALPHA_MODE_SUPPORTED) || \ ++ defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) ++ if ((png_ptr->transformations & (PNG_COMPOSE | PNG_RGB_TO_GRAY)) != 0) ++ { ++ png_build_8bit_table(png_ptr, &png_ptr->gamma_to_1, ++ png_reciprocal(png_ptr->colorspace.gamma)); ++ ++ png_build_8bit_table(png_ptr, &png_ptr->gamma_from_1, ++ png_ptr->screen_gamma > 0 ? ++ png_reciprocal(png_ptr->screen_gamma) : ++ png_ptr->colorspace.gamma/* Probably doing rgb_to_gray */); ++ } ++#endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */ ++ } ++#ifdef PNG_16BIT_SUPPORTED ++ else ++ { ++ png_byte shift, sig_bit; ++ ++ if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0) ++ { ++ sig_bit = png_ptr->sig_bit.red; ++ ++ if (png_ptr->sig_bit.green > sig_bit) ++ sig_bit = png_ptr->sig_bit.green; ++ ++ if (png_ptr->sig_bit.blue > sig_bit) ++ sig_bit = png_ptr->sig_bit.blue; ++ } ++ else ++ sig_bit = png_ptr->sig_bit.gray; ++ ++ /* 16-bit gamma code uses this equation: ++ * ++ * ov = table[(iv & 0xff) >> gamma_shift][iv >> 8] ++ * ++ * Where 'iv' is the input color value and 'ov' is the output value - ++ * pow(iv, gamma). ++ * ++ * Thus the gamma table consists of up to 256 256-entry tables. The table ++ * is selected by the (8-gamma_shift) most significant of the low 8 bits ++ * of the color value then indexed by the upper 8 bits: ++ * ++ * table[low bits][high 8 bits] ++ * ++ * So the table 'n' corresponds to all those 'iv' of: ++ * ++ * ..<(n+1 << gamma_shift)-1> ++ * ++ */ ++ if (sig_bit > 0 && sig_bit < 16U) ++ /* shift == insignificant bits */ ++ shift = (png_byte)((16U - sig_bit) & 0xff); ++ ++ else ++ shift = 0; /* keep all 16 bits */ ++ ++ if ((png_ptr->transformations & (PNG_16_TO_8 | PNG_SCALE_16_TO_8)) != 0) ++ { ++ /* PNG_MAX_GAMMA_8 is the number of bits to keep - effectively ++ * the significant bits in the *input* when the output will ++ * eventually be 8 bits. By default it is 11. ++ */ ++ if (shift < (16U - PNG_MAX_GAMMA_8)) ++ shift = (16U - PNG_MAX_GAMMA_8); ++ } ++ ++ if (shift > 8U) ++ shift = 8U; /* Guarantees at least one table! */ ++ ++ png_ptr->gamma_shift = shift; ++ ++ /* NOTE: prior to 1.5.4 this test used to include PNG_BACKGROUND (now ++ * PNG_COMPOSE). This effectively smashed the background calculation for ++ * 16-bit output because the 8-bit table assumes the result will be ++ * reduced to 8 bits. ++ */ ++ if ((png_ptr->transformations & (PNG_16_TO_8 | PNG_SCALE_16_TO_8)) != 0) ++ png_build_16to8_table(png_ptr, &png_ptr->gamma_16_table, shift, ++ png_ptr->screen_gamma > 0 ? png_product2(png_ptr->colorspace.gamma, ++ png_ptr->screen_gamma) : PNG_FP_1); ++ ++ else ++ png_build_16bit_table(png_ptr, &png_ptr->gamma_16_table, shift, ++ png_ptr->screen_gamma > 0 ? png_reciprocal2(png_ptr->colorspace.gamma, ++ png_ptr->screen_gamma) : PNG_FP_1); ++ ++#if defined(PNG_READ_BACKGROUND_SUPPORTED) || \ ++ defined(PNG_READ_ALPHA_MODE_SUPPORTED) || \ ++ defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) ++ if ((png_ptr->transformations & (PNG_COMPOSE | PNG_RGB_TO_GRAY)) != 0) ++ { ++ png_build_16bit_table(png_ptr, &png_ptr->gamma_16_to_1, shift, ++ png_reciprocal(png_ptr->colorspace.gamma)); ++ ++ /* Notice that the '16 from 1' table should be full precision, however ++ * the lookup on this table still uses gamma_shift, so it can't be. ++ * TODO: fix this. ++ */ ++ png_build_16bit_table(png_ptr, &png_ptr->gamma_16_from_1, shift, ++ png_ptr->screen_gamma > 0 ? png_reciprocal(png_ptr->screen_gamma) : ++ png_ptr->colorspace.gamma/* Probably doing rgb_to_gray */); ++ } ++#endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */ ++ } ++#endif /* 16BIT */ ++} ++#endif /* READ_GAMMA */ ++ ++/* HARDWARE OR SOFTWARE OPTION SUPPORT */ ++#ifdef PNG_SET_OPTION_SUPPORTED ++int PNGAPI ++png_set_option(png_structrp png_ptr, int option, int onoff) ++{ ++ if (png_ptr != NULL && option >= 0 && option < PNG_OPTION_NEXT && ++ (option & 1) == 0) ++ { ++ png_uint_32 mask = 3U << option; ++ png_uint_32 setting = (2U + (onoff != 0)) << option; ++ png_uint_32 current = png_ptr->options; ++ ++ png_ptr->options = (png_uint_32)((current & ~mask) | setting); ++ ++ return (int)(current & mask) >> option; ++ } ++ ++ return PNG_OPTION_INVALID; ++} ++#endif ++ ++/* sRGB support */ ++#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\ ++ defined(PNG_SIMPLIFIED_WRITE_SUPPORTED) ++/* sRGB conversion tables; these are machine generated with the code in ++ * contrib/tools/makesRGB.c. The actual sRGB transfer curve defined in the ++ * specification (see the article at https://en.wikipedia.org/wiki/SRGB) ++ * is used, not the gamma=1/2.2 approximation use elsewhere in libpng. ++ * The sRGB to linear table is exact (to the nearest 16-bit linear fraction). ++ * The inverse (linear to sRGB) table has accuracies as follows: ++ * ++ * For all possible (255*65535+1) input values: ++ * ++ * error: -0.515566 - 0.625971, 79441 (0.475369%) of readings inexact ++ * ++ * For the input values corresponding to the 65536 16-bit values: ++ * ++ * error: -0.513727 - 0.607759, 308 (0.469978%) of readings inexact ++ * ++ * In all cases the inexact readings are only off by one. ++ */ ++ ++#ifdef PNG_SIMPLIFIED_READ_SUPPORTED ++/* The convert-to-sRGB table is only currently required for read. */ ++const png_uint_16 png_sRGB_table[256] = ++{ ++ 0,20,40,60,80,99,119,139, ++ 159,179,199,219,241,264,288,313, ++ 340,367,396,427,458,491,526,562, ++ 599,637,677,718,761,805,851,898, ++ 947,997,1048,1101,1156,1212,1270,1330, ++ 1391,1453,1517,1583,1651,1720,1790,1863, ++ 1937,2013,2090,2170,2250,2333,2418,2504, ++ 2592,2681,2773,2866,2961,3058,3157,3258, ++ 3360,3464,3570,3678,3788,3900,4014,4129, ++ 4247,4366,4488,4611,4736,4864,4993,5124, ++ 5257,5392,5530,5669,5810,5953,6099,6246, ++ 6395,6547,6700,6856,7014,7174,7335,7500, ++ 7666,7834,8004,8177,8352,8528,8708,8889, ++ 9072,9258,9445,9635,9828,10022,10219,10417, ++ 10619,10822,11028,11235,11446,11658,11873,12090, ++ 12309,12530,12754,12980,13209,13440,13673,13909, ++ 14146,14387,14629,14874,15122,15371,15623,15878, ++ 16135,16394,16656,16920,17187,17456,17727,18001, ++ 18277,18556,18837,19121,19407,19696,19987,20281, ++ 20577,20876,21177,21481,21787,22096,22407,22721, ++ 23038,23357,23678,24002,24329,24658,24990,25325, ++ 25662,26001,26344,26688,27036,27386,27739,28094, ++ 28452,28813,29176,29542,29911,30282,30656,31033, ++ 31412,31794,32179,32567,32957,33350,33745,34143, ++ 34544,34948,35355,35764,36176,36591,37008,37429, ++ 37852,38278,38706,39138,39572,40009,40449,40891, ++ 41337,41785,42236,42690,43147,43606,44069,44534, ++ 45002,45473,45947,46423,46903,47385,47871,48359, ++ 48850,49344,49841,50341,50844,51349,51858,52369, ++ 52884,53401,53921,54445,54971,55500,56032,56567, ++ 57105,57646,58190,58737,59287,59840,60396,60955, ++ 61517,62082,62650,63221,63795,64372,64952,65535 ++}; ++#endif /* SIMPLIFIED_READ */ ++ ++/* The base/delta tables are required for both read and write (but currently ++ * only the simplified versions.) ++ */ ++const png_uint_16 png_sRGB_base[512] = ++{ ++ 128,1782,3383,4644,5675,6564,7357,8074, ++ 8732,9346,9921,10463,10977,11466,11935,12384, ++ 12816,13233,13634,14024,14402,14769,15125,15473, ++ 15812,16142,16466,16781,17090,17393,17690,17981, ++ 18266,18546,18822,19093,19359,19621,19879,20133, ++ 20383,20630,20873,21113,21349,21583,21813,22041, ++ 22265,22487,22707,22923,23138,23350,23559,23767, ++ 23972,24175,24376,24575,24772,24967,25160,25352, ++ 25542,25730,25916,26101,26284,26465,26645,26823, ++ 27000,27176,27350,27523,27695,27865,28034,28201, ++ 28368,28533,28697,28860,29021,29182,29341,29500, ++ 29657,29813,29969,30123,30276,30429,30580,30730, ++ 30880,31028,31176,31323,31469,31614,31758,31902, ++ 32045,32186,32327,32468,32607,32746,32884,33021, ++ 33158,33294,33429,33564,33697,33831,33963,34095, ++ 34226,34357,34486,34616,34744,34873,35000,35127, ++ 35253,35379,35504,35629,35753,35876,35999,36122, ++ 36244,36365,36486,36606,36726,36845,36964,37083, ++ 37201,37318,37435,37551,37668,37783,37898,38013, ++ 38127,38241,38354,38467,38580,38692,38803,38915, ++ 39026,39136,39246,39356,39465,39574,39682,39790, ++ 39898,40005,40112,40219,40325,40431,40537,40642, ++ 40747,40851,40955,41059,41163,41266,41369,41471, ++ 41573,41675,41777,41878,41979,42079,42179,42279, ++ 42379,42478,42577,42676,42775,42873,42971,43068, ++ 43165,43262,43359,43456,43552,43648,43743,43839, ++ 43934,44028,44123,44217,44311,44405,44499,44592, ++ 44685,44778,44870,44962,45054,45146,45238,45329, ++ 45420,45511,45601,45692,45782,45872,45961,46051, ++ 46140,46229,46318,46406,46494,46583,46670,46758, ++ 46846,46933,47020,47107,47193,47280,47366,47452, ++ 47538,47623,47709,47794,47879,47964,48048,48133, ++ 48217,48301,48385,48468,48552,48635,48718,48801, ++ 48884,48966,49048,49131,49213,49294,49376,49458, ++ 49539,49620,49701,49782,49862,49943,50023,50103, ++ 50183,50263,50342,50422,50501,50580,50659,50738, ++ 50816,50895,50973,51051,51129,51207,51285,51362, ++ 51439,51517,51594,51671,51747,51824,51900,51977, ++ 52053,52129,52205,52280,52356,52432,52507,52582, ++ 52657,52732,52807,52881,52956,53030,53104,53178, ++ 53252,53326,53400,53473,53546,53620,53693,53766, ++ 53839,53911,53984,54056,54129,54201,54273,54345, ++ 54417,54489,54560,54632,54703,54774,54845,54916, ++ 54987,55058,55129,55199,55269,55340,55410,55480, ++ 55550,55620,55689,55759,55828,55898,55967,56036, ++ 56105,56174,56243,56311,56380,56448,56517,56585, ++ 56653,56721,56789,56857,56924,56992,57059,57127, ++ 57194,57261,57328,57395,57462,57529,57595,57662, ++ 57728,57795,57861,57927,57993,58059,58125,58191, ++ 58256,58322,58387,58453,58518,58583,58648,58713, ++ 58778,58843,58908,58972,59037,59101,59165,59230, ++ 59294,59358,59422,59486,59549,59613,59677,59740, ++ 59804,59867,59930,59993,60056,60119,60182,60245, ++ 60308,60370,60433,60495,60558,60620,60682,60744, ++ 60806,60868,60930,60992,61054,61115,61177,61238, ++ 61300,61361,61422,61483,61544,61605,61666,61727, ++ 61788,61848,61909,61969,62030,62090,62150,62211, ++ 62271,62331,62391,62450,62510,62570,62630,62689, ++ 62749,62808,62867,62927,62986,63045,63104,63163, ++ 63222,63281,63340,63398,63457,63515,63574,63632, ++ 63691,63749,63807,63865,63923,63981,64039,64097, ++ 64155,64212,64270,64328,64385,64443,64500,64557, ++ 64614,64672,64729,64786,64843,64900,64956,65013, ++ 65070,65126,65183,65239,65296,65352,65409,65465 ++}; ++ ++const png_byte png_sRGB_delta[512] = ++{ ++ 207,201,158,129,113,100,90,82,77,72,68,64,61,59,56,54, ++ 52,50,49,47,46,45,43,42,41,40,39,39,38,37,36,36, ++ 35,34,34,33,33,32,32,31,31,30,30,30,29,29,28,28, ++ 28,27,27,27,27,26,26,26,25,25,25,25,24,24,24,24, ++ 23,23,23,23,23,22,22,22,22,22,22,21,21,21,21,21, ++ 21,20,20,20,20,20,20,20,20,19,19,19,19,19,19,19, ++ 19,18,18,18,18,18,18,18,18,18,18,17,17,17,17,17, ++ 17,17,17,17,17,17,16,16,16,16,16,16,16,16,16,16, ++ 16,16,16,16,15,15,15,15,15,15,15,15,15,15,15,15, ++ 15,15,15,15,14,14,14,14,14,14,14,14,14,14,14,14, ++ 14,14,14,14,14,14,14,13,13,13,13,13,13,13,13,13, ++ 13,13,13,13,13,13,13,13,13,13,13,13,13,13,12,12, ++ 12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12, ++ 12,12,12,12,12,12,12,12,12,12,12,12,11,11,11,11, ++ 11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11, ++ 11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11, ++ 11,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10, ++ 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10, ++ 10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10, ++ 10,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, ++ 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, ++ 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, ++ 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9, ++ 9,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, ++ 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, ++ 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, ++ 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, ++ 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, ++ 8,8,8,8,8,8,8,8,8,7,7,7,7,7,7,7, ++ 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, ++ 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, ++ 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7 ++}; ++#endif /* SIMPLIFIED READ/WRITE sRGB support */ ++ ++/* SIMPLIFIED READ/WRITE SUPPORT */ ++#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\ ++ defined(PNG_SIMPLIFIED_WRITE_SUPPORTED) ++static int ++png_image_free_function(png_voidp argument) ++{ ++ png_imagep image = png_voidcast(png_imagep, argument); ++ png_controlp cp = image->opaque; ++ png_control c; ++ ++ /* Double check that we have a png_ptr - it should be impossible to get here ++ * without one. ++ */ ++ if (cp->png_ptr == NULL) ++ return 0; ++ ++ /* First free any data held in the control structure. */ ++# ifdef PNG_STDIO_SUPPORTED ++ if (cp->owned_file != 0) ++ { ++ FILE *fp = png_voidcast(FILE*, cp->png_ptr->io_ptr); ++ cp->owned_file = 0; ++ ++ /* Ignore errors here. */ ++ if (fp != NULL) ++ { ++ cp->png_ptr->io_ptr = NULL; ++ (void)fclose(fp); ++ } ++ } ++# endif ++ ++ /* Copy the control structure so that the original, allocated, version can be ++ * safely freed. Notice that a png_error here stops the remainder of the ++ * cleanup, but this is probably fine because that would indicate bad memory ++ * problems anyway. ++ */ ++ c = *cp; ++ image->opaque = &c; ++ png_free(c.png_ptr, cp); ++ ++ /* Then the structures, calling the correct API. */ ++ if (c.for_write != 0) ++ { ++# ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED ++ png_destroy_write_struct(&c.png_ptr, &c.info_ptr); ++# else ++ png_error(c.png_ptr, "simplified write not supported"); ++# endif ++ } ++ else ++ { ++# ifdef PNG_SIMPLIFIED_READ_SUPPORTED ++ png_destroy_read_struct(&c.png_ptr, &c.info_ptr, NULL); ++# else ++ png_error(c.png_ptr, "simplified read not supported"); ++# endif ++ } ++ ++ /* Success. */ ++ return 1; ++} ++ ++void PNGAPI ++png_image_free(png_imagep image) ++{ ++ /* Safely call the real function, but only if doing so is safe at this point ++ * (if not inside an error handling context). Otherwise assume ++ * png_safe_execute will call this API after the return. ++ */ ++ if (image != NULL && image->opaque != NULL && ++ image->opaque->error_buf == NULL) ++ { ++ png_image_free_function(image); ++ image->opaque = NULL; ++ } ++} ++ ++int /* PRIVATE */ ++png_image_error(png_imagep image, png_const_charp error_message) ++{ ++ /* Utility to log an error. */ ++ png_safecat(image->message, (sizeof image->message), 0, error_message); ++ image->warning_or_error |= PNG_IMAGE_ERROR; ++ png_image_free(image); ++ return 0; ++} ++ ++#endif /* SIMPLIFIED READ/WRITE */ ++#endif /* READ || WRITE */ +diff --git a/lib/libpng/pngdebug.h b/lib/libpng/pngdebug.h +new file mode 100644 +index 000000000..00d5a4569 +--- /dev/null ++++ b/lib/libpng/pngdebug.h +@@ -0,0 +1,153 @@ ++ ++/* pngdebug.h - Debugging macros for libpng, also used in pngtest.c ++ * ++ * Copyright (c) 2018 Cosmin Truta ++ * Copyright (c) 1998-2002,2004,2006-2013 Glenn Randers-Pehrson ++ * Copyright (c) 1996-1997 Andreas Dilger ++ * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc. ++ * ++ * This code is released under the libpng license. ++ * For conditions of distribution and use, see the disclaimer ++ * and license in png.h ++ */ ++ ++/* Define PNG_DEBUG at compile time for debugging information. Higher ++ * numbers for PNG_DEBUG mean more debugging information. This has ++ * only been added since version 0.95 so it is not implemented throughout ++ * libpng yet, but more support will be added as needed. ++ * ++ * png_debug[1-2]?(level, message ,arg{0-2}) ++ * Expands to a statement (either a simple expression or a compound ++ * do..while(0) statement) that outputs a message with parameter ++ * substitution if PNG_DEBUG is defined to 2 or more. If PNG_DEBUG ++ * is undefined, 0 or 1 every png_debug expands to a simple expression ++ * (actually ((void)0)). ++ * ++ * level: level of detail of message, starting at 0. A level 'n' ++ * message is preceded by 'n' 3-space indentations (not implemented ++ * on Microsoft compilers unless PNG_DEBUG_FILE is also ++ * defined, to allow debug DLL compilation with no standard IO). ++ * message: a printf(3) style text string. A trailing '\n' is added ++ * to the message. ++ * arg: 0 to 2 arguments for printf(3) style substitution in message. ++ */ ++#ifndef PNGDEBUG_H ++#define PNGDEBUG_H ++/* These settings control the formatting of messages in png.c and pngerror.c */ ++/* Moved to pngdebug.h at 1.5.0 */ ++# ifndef PNG_LITERAL_SHARP ++# define PNG_LITERAL_SHARP 0x23 ++# endif ++# ifndef PNG_LITERAL_LEFT_SQUARE_BRACKET ++# define PNG_LITERAL_LEFT_SQUARE_BRACKET 0x5b ++# endif ++# ifndef PNG_LITERAL_RIGHT_SQUARE_BRACKET ++# define PNG_LITERAL_RIGHT_SQUARE_BRACKET 0x5d ++# endif ++# ifndef PNG_STRING_NEWLINE ++# define PNG_STRING_NEWLINE "\n" ++# endif ++ ++#ifdef PNG_DEBUG ++# if (PNG_DEBUG > 0) ++# if !defined(PNG_DEBUG_FILE) && defined(_MSC_VER) ++# include ++# if (PNG_DEBUG > 1) ++# ifndef _DEBUG ++# define _DEBUG ++# endif ++# ifndef png_debug ++# define png_debug(l,m) _RPT0(_CRT_WARN,m PNG_STRING_NEWLINE) ++# endif ++# ifndef png_debug1 ++# define png_debug1(l,m,p1) _RPT1(_CRT_WARN,m PNG_STRING_NEWLINE,p1) ++# endif ++# ifndef png_debug2 ++# define png_debug2(l,m,p1,p2) \ ++ _RPT2(_CRT_WARN,m PNG_STRING_NEWLINE,p1,p2) ++# endif ++# endif ++# else /* PNG_DEBUG_FILE || !_MSC_VER */ ++# ifndef PNG_STDIO_SUPPORTED ++# include /* not included yet */ ++# endif ++# ifndef PNG_DEBUG_FILE ++# define PNG_DEBUG_FILE stderr ++# endif /* PNG_DEBUG_FILE */ ++ ++# if (PNG_DEBUG > 1) ++# ifdef __STDC__ ++# ifndef png_debug ++# define png_debug(l,m) \ ++ do { \ ++ int num_tabs=l; \ ++ fprintf(PNG_DEBUG_FILE,"%s" m PNG_STRING_NEWLINE,(num_tabs==1 ? " " : \ ++ (num_tabs==2 ? " " : (num_tabs>2 ? " " : "")))); \ ++ } while (0) ++# endif ++# ifndef png_debug1 ++# define png_debug1(l,m,p1) \ ++ do { \ ++ int num_tabs=l; \ ++ fprintf(PNG_DEBUG_FILE,"%s" m PNG_STRING_NEWLINE,(num_tabs==1 ? " " : \ ++ (num_tabs==2 ? " " : (num_tabs>2 ? " " : ""))),p1); \ ++ } while (0) ++# endif ++# ifndef png_debug2 ++# define png_debug2(l,m,p1,p2) \ ++ do { \ ++ int num_tabs=l; \ ++ fprintf(PNG_DEBUG_FILE,"%s" m PNG_STRING_NEWLINE,(num_tabs==1 ? " " : \ ++ (num_tabs==2 ? " " : (num_tabs>2 ? " " : ""))),p1,p2);\ ++ } while (0) ++# endif ++# else /* __STDC __ */ ++# ifndef png_debug ++# define png_debug(l,m) \ ++ do { \ ++ int num_tabs=l; \ ++ char format[256]; \ ++ snprintf(format,256,"%s%s%s",(num_tabs==1 ? "\t" : \ ++ (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":""))), \ ++ m,PNG_STRING_NEWLINE); \ ++ fprintf(PNG_DEBUG_FILE,format); \ ++ } while (0) ++# endif ++# ifndef png_debug1 ++# define png_debug1(l,m,p1) \ ++ do { \ ++ int num_tabs=l; \ ++ char format[256]; \ ++ snprintf(format,256,"%s%s%s",(num_tabs==1 ? "\t" : \ ++ (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":""))), \ ++ m,PNG_STRING_NEWLINE); \ ++ fprintf(PNG_DEBUG_FILE,format,p1); \ ++ } while (0) ++# endif ++# ifndef png_debug2 ++# define png_debug2(l,m,p1,p2) \ ++ do { \ ++ int num_tabs=l; \ ++ char format[256]; \ ++ snprintf(format,256,"%s%s%s",(num_tabs==1 ? "\t" : \ ++ (num_tabs==2 ? "\t\t":(num_tabs>2 ? "\t\t\t":""))), \ ++ m,PNG_STRING_NEWLINE); \ ++ fprintf(PNG_DEBUG_FILE,format,p1,p2); \ ++ } while (0) ++# endif ++# endif /* __STDC __ */ ++# endif /* (PNG_DEBUG > 1) */ ++ ++# endif /* _MSC_VER */ ++# endif /* (PNG_DEBUG > 0) */ ++#endif /* PNG_DEBUG */ ++#ifndef png_debug ++# define png_debug(l, m) ((void)0) ++#endif ++#ifndef png_debug1 ++# define png_debug1(l, m, p1) ((void)0) ++#endif ++#ifndef png_debug2 ++# define png_debug2(l, m, p1, p2) ((void)0) ++#endif ++#endif /* PNGDEBUG_H */ +diff --git a/lib/libpng/pngerror.c b/lib/libpng/pngerror.c +new file mode 100644 +index 000000000..ec3a709b9 +--- /dev/null ++++ b/lib/libpng/pngerror.c +@@ -0,0 +1,963 @@ ++ ++/* pngerror.c - stub functions for i/o and memory allocation ++ * ++ * Copyright (c) 2018 Cosmin Truta ++ * Copyright (c) 1998-2002,2004,2006-2017 Glenn Randers-Pehrson ++ * Copyright (c) 1996-1997 Andreas Dilger ++ * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc. ++ * ++ * This code is released under the libpng license. ++ * For conditions of distribution and use, see the disclaimer ++ * and license in png.h ++ * ++ * This file provides a location for all error handling. Users who ++ * need special error handling are expected to write replacement functions ++ * and use png_set_error_fn() to use those functions. See the instructions ++ * at each function. ++ */ ++ ++#include "pngpriv.h" ++ ++#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) ++ ++static PNG_FUNCTION(void, png_default_error,PNGARG((png_const_structrp png_ptr, ++ png_const_charp error_message)),PNG_NORETURN); ++ ++#ifdef PNG_WARNINGS_SUPPORTED ++static void /* PRIVATE */ ++png_default_warning PNGARG((png_const_structrp png_ptr, ++ png_const_charp warning_message)); ++#endif /* WARNINGS */ ++ ++/* This function is called whenever there is a fatal error. This function ++ * should not be changed. If there is a need to handle errors differently, ++ * you should supply a replacement error function and use png_set_error_fn() ++ * to replace the error function at run-time. ++ */ ++#ifdef PNG_ERROR_TEXT_SUPPORTED ++PNG_FUNCTION(void,PNGAPI ++png_error,(png_const_structrp png_ptr, png_const_charp error_message), ++ PNG_NORETURN) ++{ ++#ifdef PNG_ERROR_NUMBERS_SUPPORTED ++ char msg[16]; ++ if (png_ptr != NULL) ++ { ++ if ((png_ptr->flags & ++ (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT)) != 0) ++ { ++ if (*error_message == PNG_LITERAL_SHARP) ++ { ++ /* Strip "#nnnn " from beginning of error message. */ ++ int offset; ++ for (offset = 1; offset<15; offset++) ++ if (error_message[offset] == ' ') ++ break; ++ ++ if ((png_ptr->flags & PNG_FLAG_STRIP_ERROR_TEXT) != 0) ++ { ++ int i; ++ for (i = 0; i < offset - 1; i++) ++ msg[i] = error_message[i + 1]; ++ msg[i - 1] = '\0'; ++ error_message = msg; ++ } ++ ++ else ++ error_message += offset; ++ } ++ ++ else ++ { ++ if ((png_ptr->flags & PNG_FLAG_STRIP_ERROR_TEXT) != 0) ++ { ++ msg[0] = '0'; ++ msg[1] = '\0'; ++ error_message = msg; ++ } ++ } ++ } ++ } ++#endif ++ if (png_ptr != NULL && png_ptr->error_fn != NULL) ++ (*(png_ptr->error_fn))(png_constcast(png_structrp,png_ptr), ++ error_message); ++ ++ /* If the custom handler doesn't exist, or if it returns, ++ use the default handler, which will not return. */ ++ png_default_error(png_ptr, error_message); ++} ++#else ++PNG_FUNCTION(void,PNGAPI ++png_err,(png_const_structrp png_ptr),PNG_NORETURN) ++{ ++ /* Prior to 1.5.2 the error_fn received a NULL pointer, expressed ++ * erroneously as '\0', instead of the empty string "". This was ++ * apparently an error, introduced in libpng-1.2.20, and png_default_error ++ * will crash in this case. ++ */ ++ if (png_ptr != NULL && png_ptr->error_fn != NULL) ++ (*(png_ptr->error_fn))(png_constcast(png_structrp,png_ptr), ""); ++ ++ /* If the custom handler doesn't exist, or if it returns, ++ use the default handler, which will not return. */ ++ png_default_error(png_ptr, ""); ++} ++#endif /* ERROR_TEXT */ ++ ++/* Utility to safely appends strings to a buffer. This never errors out so ++ * error checking is not required in the caller. ++ */ ++size_t ++png_safecat(png_charp buffer, size_t bufsize, size_t pos, ++ png_const_charp string) ++{ ++ if (buffer != NULL && pos < bufsize) ++ { ++ if (string != NULL) ++ while (*string != '\0' && pos < bufsize-1) ++ buffer[pos++] = *string++; ++ ++ buffer[pos] = '\0'; ++ } ++ ++ return pos; ++} ++ ++#if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_TIME_RFC1123_SUPPORTED) ++/* Utility to dump an unsigned value into a buffer, given a start pointer and ++ * and end pointer (which should point just *beyond* the end of the buffer!) ++ * Returns the pointer to the start of the formatted string. ++ */ ++png_charp ++png_format_number(png_const_charp start, png_charp end, int format, ++ png_alloc_size_t number) ++{ ++ int count = 0; /* number of digits output */ ++ int mincount = 1; /* minimum number required */ ++ int output = 0; /* digit output (for the fixed point format) */ ++ ++ *--end = '\0'; ++ ++ /* This is written so that the loop always runs at least once, even with ++ * number zero. ++ */ ++ while (end > start && (number != 0 || count < mincount)) ++ { ++ ++ static const char digits[] = "0123456789ABCDEF"; ++ ++ switch (format) ++ { ++ case PNG_NUMBER_FORMAT_fixed: ++ /* Needs five digits (the fraction) */ ++ mincount = 5; ++ if (output != 0 || number % 10 != 0) ++ { ++ *--end = digits[number % 10]; ++ output = 1; ++ } ++ number /= 10; ++ break; ++ ++ case PNG_NUMBER_FORMAT_02u: ++ /* Expects at least 2 digits. */ ++ mincount = 2; ++ /* FALLTHROUGH */ ++ ++ case PNG_NUMBER_FORMAT_u: ++ *--end = digits[number % 10]; ++ number /= 10; ++ break; ++ ++ case PNG_NUMBER_FORMAT_02x: ++ /* This format expects at least two digits */ ++ mincount = 2; ++ /* FALLTHROUGH */ ++ ++ case PNG_NUMBER_FORMAT_x: ++ *--end = digits[number & 0xf]; ++ number >>= 4; ++ break; ++ ++ default: /* an error */ ++ number = 0; ++ break; ++ } ++ ++ /* Keep track of the number of digits added */ ++ ++count; ++ ++ /* Float a fixed number here: */ ++ if ((format == PNG_NUMBER_FORMAT_fixed) && (count == 5) && (end > start)) ++ { ++ /* End of the fraction, but maybe nothing was output? In that case ++ * drop the decimal point. If the number is a true zero handle that ++ * here. ++ */ ++ if (output != 0) ++ *--end = '.'; ++ else if (number == 0) /* and !output */ ++ *--end = '0'; ++ } ++ } ++ ++ return end; ++} ++#endif ++ ++#ifdef PNG_WARNINGS_SUPPORTED ++/* This function is called whenever there is a non-fatal error. This function ++ * should not be changed. If there is a need to handle warnings differently, ++ * you should supply a replacement warning function and use ++ * png_set_error_fn() to replace the warning function at run-time. ++ */ ++void PNGAPI ++png_warning(png_const_structrp png_ptr, png_const_charp warning_message) ++{ ++ int offset = 0; ++ if (png_ptr != NULL) ++ { ++#ifdef PNG_ERROR_NUMBERS_SUPPORTED ++ if ((png_ptr->flags & ++ (PNG_FLAG_STRIP_ERROR_NUMBERS|PNG_FLAG_STRIP_ERROR_TEXT)) != 0) ++#endif ++ { ++ if (*warning_message == PNG_LITERAL_SHARP) ++ { ++ for (offset = 1; offset < 15; offset++) ++ if (warning_message[offset] == ' ') ++ break; ++ } ++ } ++ } ++ if (png_ptr != NULL && png_ptr->warning_fn != NULL) ++ (*(png_ptr->warning_fn))(png_constcast(png_structrp,png_ptr), ++ warning_message + offset); ++ else ++ png_default_warning(png_ptr, warning_message + offset); ++} ++ ++/* These functions support 'formatted' warning messages with up to ++ * PNG_WARNING_PARAMETER_COUNT parameters. In the format string the parameter ++ * is introduced by @, where 'number' starts at 1. This follows the ++ * standard established by X/Open for internationalizable error messages. ++ */ ++void ++png_warning_parameter(png_warning_parameters p, int number, ++ png_const_charp string) ++{ ++ if (number > 0 && number <= PNG_WARNING_PARAMETER_COUNT) ++ (void)png_safecat(p[number-1], (sizeof p[number-1]), 0, string); ++} ++ ++void ++png_warning_parameter_unsigned(png_warning_parameters p, int number, int format, ++ png_alloc_size_t value) ++{ ++ char buffer[PNG_NUMBER_BUFFER_SIZE]; ++ png_warning_parameter(p, number, PNG_FORMAT_NUMBER(buffer, format, value)); ++} ++ ++void ++png_warning_parameter_signed(png_warning_parameters p, int number, int format, ++ png_int_32 value) ++{ ++ png_alloc_size_t u; ++ png_charp str; ++ char buffer[PNG_NUMBER_BUFFER_SIZE]; ++ ++ /* Avoid overflow by doing the negate in a png_alloc_size_t: */ ++ u = (png_alloc_size_t)value; ++ if (value < 0) ++ u = ~u + 1; ++ ++ str = PNG_FORMAT_NUMBER(buffer, format, u); ++ ++ if (value < 0 && str > buffer) ++ *--str = '-'; ++ ++ png_warning_parameter(p, number, str); ++} ++ ++void ++png_formatted_warning(png_const_structrp png_ptr, png_warning_parameters p, ++ png_const_charp message) ++{ ++ /* The internal buffer is just 192 bytes - enough for all our messages, ++ * overflow doesn't happen because this code checks! If someone figures ++ * out how to send us a message longer than 192 bytes, all that will ++ * happen is that the message will be truncated appropriately. ++ */ ++ size_t i = 0; /* Index in the msg[] buffer: */ ++ char msg[192]; ++ ++ /* Each iteration through the following loop writes at most one character ++ * to msg[i++] then returns here to validate that there is still space for ++ * the trailing '\0'. It may (in the case of a parameter) read more than ++ * one character from message[]; it must check for '\0' and continue to the ++ * test if it finds the end of string. ++ */ ++ while (i<(sizeof msg)-1 && *message != '\0') ++ { ++ /* '@' at end of string is now just printed (previously it was skipped); ++ * it is an error in the calling code to terminate the string with @. ++ */ ++ if (p != NULL && *message == '@' && message[1] != '\0') ++ { ++ int parameter_char = *++message; /* Consume the '@' */ ++ static const char valid_parameters[] = "123456789"; ++ int parameter = 0; ++ ++ /* Search for the parameter digit, the index in the string is the ++ * parameter to use. ++ */ ++ while (valid_parameters[parameter] != parameter_char && ++ valid_parameters[parameter] != '\0') ++ ++parameter; ++ ++ /* If the parameter digit is out of range it will just get printed. */ ++ if (parameter < PNG_WARNING_PARAMETER_COUNT) ++ { ++ /* Append this parameter */ ++ png_const_charp parm = p[parameter]; ++ png_const_charp pend = p[parameter] + (sizeof p[parameter]); ++ ++ /* No need to copy the trailing '\0' here, but there is no guarantee ++ * that parm[] has been initialized, so there is no guarantee of a ++ * trailing '\0': ++ */ ++ while (i<(sizeof msg)-1 && *parm != '\0' && parm < pend) ++ msg[i++] = *parm++; ++ ++ /* Consume the parameter digit too: */ ++ ++message; ++ continue; ++ } ++ ++ /* else not a parameter and there is a character after the @ sign; just ++ * copy that. This is known not to be '\0' because of the test above. ++ */ ++ } ++ ++ /* At this point *message can't be '\0', even in the bad parameter case ++ * above where there is a lone '@' at the end of the message string. ++ */ ++ msg[i++] = *message++; ++ } ++ ++ /* i is always less than (sizeof msg), so: */ ++ msg[i] = '\0'; ++ ++ /* And this is the formatted message. It may be larger than ++ * PNG_MAX_ERROR_TEXT, but that is only used for 'chunk' errors and these ++ * are not (currently) formatted. ++ */ ++ png_warning(png_ptr, msg); ++} ++#endif /* WARNINGS */ ++ ++#ifdef PNG_BENIGN_ERRORS_SUPPORTED ++void PNGAPI ++png_benign_error(png_const_structrp png_ptr, png_const_charp error_message) ++{ ++ if ((png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN) != 0) ++ { ++# ifdef PNG_READ_SUPPORTED ++ if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0 && ++ png_ptr->chunk_name != 0) ++ png_chunk_warning(png_ptr, error_message); ++ else ++# endif ++ png_warning(png_ptr, error_message); ++ } ++ ++ else ++ { ++# ifdef PNG_READ_SUPPORTED ++ if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0 && ++ png_ptr->chunk_name != 0) ++ png_chunk_error(png_ptr, error_message); ++ else ++# endif ++ png_error(png_ptr, error_message); ++ } ++ ++# ifndef PNG_ERROR_TEXT_SUPPORTED ++ PNG_UNUSED(error_message) ++# endif ++} ++ ++void /* PRIVATE */ ++png_app_warning(png_const_structrp png_ptr, png_const_charp error_message) ++{ ++ if ((png_ptr->flags & PNG_FLAG_APP_WARNINGS_WARN) != 0) ++ png_warning(png_ptr, error_message); ++ else ++ png_error(png_ptr, error_message); ++ ++# ifndef PNG_ERROR_TEXT_SUPPORTED ++ PNG_UNUSED(error_message) ++# endif ++} ++ ++void /* PRIVATE */ ++png_app_error(png_const_structrp png_ptr, png_const_charp error_message) ++{ ++ if ((png_ptr->flags & PNG_FLAG_APP_ERRORS_WARN) != 0) ++ png_warning(png_ptr, error_message); ++ else ++ png_error(png_ptr, error_message); ++ ++# ifndef PNG_ERROR_TEXT_SUPPORTED ++ PNG_UNUSED(error_message) ++# endif ++} ++#endif /* BENIGN_ERRORS */ ++ ++#define PNG_MAX_ERROR_TEXT 196 /* Currently limited by profile_error in png.c */ ++#if defined(PNG_WARNINGS_SUPPORTED) || \ ++ (defined(PNG_READ_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED)) ++/* These utilities are used internally to build an error message that relates ++ * to the current chunk. The chunk name comes from png_ptr->chunk_name, ++ * which is used to prefix the message. The message is limited in length ++ * to 63 bytes. The name characters are output as hex digits wrapped in [] ++ * if the character is invalid. ++ */ ++#define isnonalpha(c) ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97)) ++static const char png_digit[16] = { ++ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ++ 'A', 'B', 'C', 'D', 'E', 'F' ++}; ++ ++static void /* PRIVATE */ ++png_format_buffer(png_const_structrp png_ptr, png_charp buffer, png_const_charp ++ error_message) ++{ ++ png_uint_32 chunk_name = png_ptr->chunk_name; ++ int iout = 0, ishift = 24; ++ ++ while (ishift >= 0) ++ { ++ int c = (int)(chunk_name >> ishift) & 0xff; ++ ++ ishift -= 8; ++ if (isnonalpha(c) != 0) ++ { ++ buffer[iout++] = PNG_LITERAL_LEFT_SQUARE_BRACKET; ++ buffer[iout++] = png_digit[(c & 0xf0) >> 4]; ++ buffer[iout++] = png_digit[c & 0x0f]; ++ buffer[iout++] = PNG_LITERAL_RIGHT_SQUARE_BRACKET; ++ } ++ ++ else ++ { ++ buffer[iout++] = (char)c; ++ } ++ } ++ ++ if (error_message == NULL) ++ buffer[iout] = '\0'; ++ ++ else ++ { ++ int iin = 0; ++ ++ buffer[iout++] = ':'; ++ buffer[iout++] = ' '; ++ ++ while (iin < PNG_MAX_ERROR_TEXT-1 && error_message[iin] != '\0') ++ buffer[iout++] = error_message[iin++]; ++ ++ /* iin < PNG_MAX_ERROR_TEXT, so the following is safe: */ ++ buffer[iout] = '\0'; ++ } ++} ++#endif /* WARNINGS || ERROR_TEXT */ ++ ++#if defined(PNG_READ_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED) ++PNG_FUNCTION(void,PNGAPI ++png_chunk_error,(png_const_structrp png_ptr, png_const_charp error_message), ++ PNG_NORETURN) ++{ ++ char msg[18+PNG_MAX_ERROR_TEXT]; ++ if (png_ptr == NULL) ++ png_error(png_ptr, error_message); ++ ++ else ++ { ++ png_format_buffer(png_ptr, msg, error_message); ++ png_error(png_ptr, msg); ++ } ++} ++#endif /* READ && ERROR_TEXT */ ++ ++#ifdef PNG_WARNINGS_SUPPORTED ++void PNGAPI ++png_chunk_warning(png_const_structrp png_ptr, png_const_charp warning_message) ++{ ++ char msg[18+PNG_MAX_ERROR_TEXT]; ++ if (png_ptr == NULL) ++ png_warning(png_ptr, warning_message); ++ ++ else ++ { ++ png_format_buffer(png_ptr, msg, warning_message); ++ png_warning(png_ptr, msg); ++ } ++} ++#endif /* WARNINGS */ ++ ++#ifdef PNG_READ_SUPPORTED ++#ifdef PNG_BENIGN_ERRORS_SUPPORTED ++void PNGAPI ++png_chunk_benign_error(png_const_structrp png_ptr, png_const_charp ++ error_message) ++{ ++ if ((png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN) != 0) ++ png_chunk_warning(png_ptr, error_message); ++ ++ else ++ png_chunk_error(png_ptr, error_message); ++ ++# ifndef PNG_ERROR_TEXT_SUPPORTED ++ PNG_UNUSED(error_message) ++# endif ++} ++#endif ++#endif /* READ */ ++ ++void /* PRIVATE */ ++png_chunk_report(png_const_structrp png_ptr, png_const_charp message, int error) ++{ ++# ifndef PNG_WARNINGS_SUPPORTED ++ PNG_UNUSED(message) ++# endif ++ ++ /* This is always supported, but for just read or just write it ++ * unconditionally does the right thing. ++ */ ++# if defined(PNG_READ_SUPPORTED) && defined(PNG_WRITE_SUPPORTED) ++ if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0) ++# endif ++ ++# ifdef PNG_READ_SUPPORTED ++ { ++ if (error < PNG_CHUNK_ERROR) ++ png_chunk_warning(png_ptr, message); ++ ++ else ++ png_chunk_benign_error(png_ptr, message); ++ } ++# endif ++ ++# if defined(PNG_READ_SUPPORTED) && defined(PNG_WRITE_SUPPORTED) ++ else if ((png_ptr->mode & PNG_IS_READ_STRUCT) == 0) ++# endif ++ ++# ifdef PNG_WRITE_SUPPORTED ++ { ++ if (error < PNG_CHUNK_WRITE_ERROR) ++ png_app_warning(png_ptr, message); ++ ++ else ++ png_app_error(png_ptr, message); ++ } ++# endif ++} ++ ++#ifdef PNG_ERROR_TEXT_SUPPORTED ++#ifdef PNG_FLOATING_POINT_SUPPORTED ++PNG_FUNCTION(void, ++png_fixed_error,(png_const_structrp png_ptr, png_const_charp name),PNG_NORETURN) ++{ ++# define fixed_message "fixed point overflow in " ++# define fixed_message_ln ((sizeof fixed_message)-1) ++ unsigned int iin; ++ char msg[fixed_message_ln+PNG_MAX_ERROR_TEXT]; ++ memcpy(msg, fixed_message, fixed_message_ln); ++ iin = 0; ++ if (name != NULL) ++ while (iin < (PNG_MAX_ERROR_TEXT-1) && name[iin] != 0) ++ { ++ msg[fixed_message_ln + iin] = name[iin]; ++ ++iin; ++ } ++ msg[fixed_message_ln + iin] = 0; ++ png_error(png_ptr, msg); ++} ++#endif ++#endif ++ ++#ifdef PNG_SETJMP_SUPPORTED ++/* This API only exists if ANSI-C style error handling is used, ++ * otherwise it is necessary for png_default_error to be overridden. ++ */ ++jmp_buf* PNGAPI ++png_set_longjmp_fn(png_structrp png_ptr, png_longjmp_ptr longjmp_fn, ++ size_t jmp_buf_size) ++{ ++ /* From libpng 1.6.0 the app gets one chance to set a 'jmpbuf_size' value ++ * and it must not change after that. Libpng doesn't care how big the ++ * buffer is, just that it doesn't change. ++ * ++ * If the buffer size is no *larger* than the size of jmp_buf when libpng is ++ * compiled a built in jmp_buf is returned; this preserves the pre-1.6.0 ++ * semantics that this call will not fail. If the size is larger, however, ++ * the buffer is allocated and this may fail, causing the function to return ++ * NULL. ++ */ ++ if (png_ptr == NULL) ++ return NULL; ++ ++ if (png_ptr->jmp_buf_ptr == NULL) ++ { ++ png_ptr->jmp_buf_size = 0; /* not allocated */ ++ ++ if (jmp_buf_size <= (sizeof png_ptr->jmp_buf_local)) ++ png_ptr->jmp_buf_ptr = &png_ptr->jmp_buf_local; ++ ++ else ++ { ++ png_ptr->jmp_buf_ptr = png_voidcast(jmp_buf *, ++ png_malloc_warn(png_ptr, jmp_buf_size)); ++ ++ if (png_ptr->jmp_buf_ptr == NULL) ++ return NULL; /* new NULL return on OOM */ ++ ++ png_ptr->jmp_buf_size = jmp_buf_size; ++ } ++ } ++ ++ else /* Already allocated: check the size */ ++ { ++ size_t size = png_ptr->jmp_buf_size; ++ ++ if (size == 0) ++ { ++ size = (sizeof png_ptr->jmp_buf_local); ++ if (png_ptr->jmp_buf_ptr != &png_ptr->jmp_buf_local) ++ { ++ /* This is an internal error in libpng: somehow we have been left ++ * with a stack allocated jmp_buf when the application regained ++ * control. It's always possible to fix this up, but for the moment ++ * this is a png_error because that makes it easy to detect. ++ */ ++ png_error(png_ptr, "Libpng jmp_buf still allocated"); ++ /* png_ptr->jmp_buf_ptr = &png_ptr->jmp_buf_local; */ ++ } ++ } ++ ++ if (size != jmp_buf_size) ++ { ++ png_warning(png_ptr, "Application jmp_buf size changed"); ++ return NULL; /* caller will probably crash: no choice here */ ++ } ++ } ++ ++ /* Finally fill in the function, now we have a satisfactory buffer. It is ++ * valid to change the function on every call. ++ */ ++ png_ptr->longjmp_fn = longjmp_fn; ++ return png_ptr->jmp_buf_ptr; ++} ++ ++void /* PRIVATE */ ++png_free_jmpbuf(png_structrp png_ptr) ++{ ++ if (png_ptr != NULL) ++ { ++ jmp_buf *jb = png_ptr->jmp_buf_ptr; ++ ++ /* A size of 0 is used to indicate a local, stack, allocation of the ++ * pointer; used here and in png.c ++ */ ++ if (jb != NULL && png_ptr->jmp_buf_size > 0) ++ { ++ ++ /* This stuff is so that a failure to free the error control structure ++ * does not leave libpng in a state with no valid error handling: the ++ * free always succeeds, if there is an error it gets ignored. ++ */ ++ if (jb != &png_ptr->jmp_buf_local) ++ { ++ /* Make an internal, libpng, jmp_buf to return here */ ++ jmp_buf free_jmp_buf; ++ ++ if (!setjmp(free_jmp_buf)) ++ { ++ png_ptr->jmp_buf_ptr = &free_jmp_buf; /* come back here */ ++ png_ptr->jmp_buf_size = 0; /* stack allocation */ ++ png_ptr->longjmp_fn = longjmp; ++ png_free(png_ptr, jb); /* Return to setjmp on error */ ++ } ++ } ++ } ++ ++ /* *Always* cancel everything out: */ ++ png_ptr->jmp_buf_size = 0; ++ png_ptr->jmp_buf_ptr = NULL; ++ png_ptr->longjmp_fn = 0; ++ } ++} ++#endif ++ ++/* This is the default error handling function. Note that replacements for ++ * this function MUST NOT RETURN, or the program will likely crash. This ++ * function is used by default, or if the program supplies NULL for the ++ * error function pointer in png_set_error_fn(). ++ */ ++static PNG_FUNCTION(void /* PRIVATE */, ++png_default_error,(png_const_structrp png_ptr, png_const_charp error_message), ++ PNG_NORETURN) ++{ ++#ifdef PNG_CONSOLE_IO_SUPPORTED ++#ifdef PNG_ERROR_NUMBERS_SUPPORTED ++ /* Check on NULL only added in 1.5.4 */ ++ if (error_message != NULL && *error_message == PNG_LITERAL_SHARP) ++ { ++ /* Strip "#nnnn " from beginning of error message. */ ++ int offset; ++ char error_number[16]; ++ for (offset = 0; offset<15; offset++) ++ { ++ error_number[offset] = error_message[offset + 1]; ++ if (error_message[offset] == ' ') ++ break; ++ } ++ ++ if ((offset > 1) && (offset < 15)) ++ { ++ error_number[offset - 1] = '\0'; ++ fprintf(stderr, "libpng error no. %s: %s", ++ error_number, error_message + offset + 1); ++ fprintf(stderr, PNG_STRING_NEWLINE); ++ } ++ ++ else ++ { ++ fprintf(stderr, "libpng error: %s, offset=%d", ++ error_message, offset); ++ fprintf(stderr, PNG_STRING_NEWLINE); ++ } ++ } ++ else ++#endif ++ { ++ fprintf(stderr, "libpng error: %s", error_message ? error_message : ++ "undefined"); ++ fprintf(stderr, PNG_STRING_NEWLINE); ++ } ++#else ++ PNG_UNUSED(error_message) /* Make compiler happy */ ++#endif ++ png_longjmp(png_ptr, 1); ++} ++ ++PNG_FUNCTION(void,PNGAPI ++png_longjmp,(png_const_structrp png_ptr, int val),PNG_NORETURN) ++{ ++#ifdef PNG_SETJMP_SUPPORTED ++ if (png_ptr != NULL && png_ptr->longjmp_fn != NULL && ++ png_ptr->jmp_buf_ptr != NULL) ++ png_ptr->longjmp_fn(*png_ptr->jmp_buf_ptr, val); ++#else ++ PNG_UNUSED(png_ptr) ++ PNG_UNUSED(val) ++#endif ++ ++ /* If control reaches this point, png_longjmp() must not return. The only ++ * choice is to terminate the whole process (or maybe the thread); to do ++ * this the ANSI-C abort() function is used unless a different method is ++ * implemented by overriding the default configuration setting for ++ * PNG_ABORT(). ++ */ ++ PNG_ABORT(); ++} ++ ++#ifdef PNG_WARNINGS_SUPPORTED ++/* This function is called when there is a warning, but the library thinks ++ * it can continue anyway. Replacement functions don't have to do anything ++ * here if you don't want them to. In the default configuration, png_ptr is ++ * not used, but it is passed in case it may be useful. ++ */ ++static void /* PRIVATE */ ++png_default_warning(png_const_structrp png_ptr, png_const_charp warning_message) ++{ ++#ifdef PNG_CONSOLE_IO_SUPPORTED ++# ifdef PNG_ERROR_NUMBERS_SUPPORTED ++ if (*warning_message == PNG_LITERAL_SHARP) ++ { ++ int offset; ++ char warning_number[16]; ++ for (offset = 0; offset < 15; offset++) ++ { ++ warning_number[offset] = warning_message[offset + 1]; ++ if (warning_message[offset] == ' ') ++ break; ++ } ++ ++ if ((offset > 1) && (offset < 15)) ++ { ++ warning_number[offset + 1] = '\0'; ++ fprintf(stderr, "libpng warning no. %s: %s", ++ warning_number, warning_message + offset); ++ fprintf(stderr, PNG_STRING_NEWLINE); ++ } ++ ++ else ++ { ++ fprintf(stderr, "libpng warning: %s", ++ warning_message); ++ fprintf(stderr, PNG_STRING_NEWLINE); ++ } ++ } ++ else ++# endif ++ ++ { ++ fprintf(stderr, "libpng warning: %s", warning_message); ++ fprintf(stderr, PNG_STRING_NEWLINE); ++ } ++#else ++ PNG_UNUSED(warning_message) /* Make compiler happy */ ++#endif ++ PNG_UNUSED(png_ptr) /* Make compiler happy */ ++} ++#endif /* WARNINGS */ ++ ++/* This function is called when the application wants to use another method ++ * of handling errors and warnings. Note that the error function MUST NOT ++ * return to the calling routine or serious problems will occur. The return ++ * method used in the default routine calls longjmp(png_ptr->jmp_buf_ptr, 1) ++ */ ++void PNGAPI ++png_set_error_fn(png_structrp png_ptr, png_voidp error_ptr, ++ png_error_ptr error_fn, png_error_ptr warning_fn) ++{ ++ if (png_ptr == NULL) ++ return; ++ ++ png_ptr->error_ptr = error_ptr; ++ png_ptr->error_fn = error_fn; ++#ifdef PNG_WARNINGS_SUPPORTED ++ png_ptr->warning_fn = warning_fn; ++#else ++ PNG_UNUSED(warning_fn) ++#endif ++} ++ ++ ++/* This function returns a pointer to the error_ptr associated with the user ++ * functions. The application should free any memory associated with this ++ * pointer before png_write_destroy and png_read_destroy are called. ++ */ ++png_voidp PNGAPI ++png_get_error_ptr(png_const_structrp png_ptr) ++{ ++ if (png_ptr == NULL) ++ return NULL; ++ ++ return ((png_voidp)png_ptr->error_ptr); ++} ++ ++ ++#ifdef PNG_ERROR_NUMBERS_SUPPORTED ++void PNGAPI ++png_set_strip_error_numbers(png_structrp png_ptr, png_uint_32 strip_mode) ++{ ++ if (png_ptr != NULL) ++ { ++ png_ptr->flags &= ++ ((~(PNG_FLAG_STRIP_ERROR_NUMBERS | ++ PNG_FLAG_STRIP_ERROR_TEXT))&strip_mode); ++ } ++} ++#endif ++ ++#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\ ++ defined(PNG_SIMPLIFIED_WRITE_SUPPORTED) ++ /* Currently the above both depend on SETJMP_SUPPORTED, however it would be ++ * possible to implement without setjmp support just so long as there is some ++ * way to handle the error return here: ++ */ ++PNG_FUNCTION(void /* PRIVATE */, (PNGCBAPI ++png_safe_error),(png_structp png_nonconst_ptr, png_const_charp error_message), ++ PNG_NORETURN) ++{ ++ png_const_structrp png_ptr = png_nonconst_ptr; ++ png_imagep image = png_voidcast(png_imagep, png_ptr->error_ptr); ++ ++ /* An error is always logged here, overwriting anything (typically a warning) ++ * that is already there: ++ */ ++ if (image != NULL) ++ { ++ png_safecat(image->message, (sizeof image->message), 0, error_message); ++ image->warning_or_error |= PNG_IMAGE_ERROR; ++ ++ /* Retrieve the jmp_buf from within the png_control, making this work for ++ * C++ compilation too is pretty tricky: C++ wants a pointer to the first ++ * element of a jmp_buf, but C doesn't tell us the type of that. ++ */ ++ if (image->opaque != NULL && image->opaque->error_buf != NULL) ++ longjmp(png_control_jmp_buf(image->opaque), 1); ++ ++ /* Missing longjmp buffer, the following is to help debugging: */ ++ { ++ size_t pos = png_safecat(image->message, (sizeof image->message), 0, ++ "bad longjmp: "); ++ png_safecat(image->message, (sizeof image->message), pos, ++ error_message); ++ } ++ } ++ ++ /* Here on an internal programming error. */ ++ abort(); ++} ++ ++#ifdef PNG_WARNINGS_SUPPORTED ++void /* PRIVATE */ PNGCBAPI ++png_safe_warning(png_structp png_nonconst_ptr, png_const_charp warning_message) ++{ ++ png_const_structrp png_ptr = png_nonconst_ptr; ++ png_imagep image = png_voidcast(png_imagep, png_ptr->error_ptr); ++ ++ /* A warning is only logged if there is no prior warning or error. */ ++ if (image->warning_or_error == 0) ++ { ++ png_safecat(image->message, (sizeof image->message), 0, warning_message); ++ image->warning_or_error |= PNG_IMAGE_WARNING; ++ } ++} ++#endif ++ ++int /* PRIVATE */ ++png_safe_execute(png_imagep image_in, int (*function)(png_voidp), png_voidp arg) ++{ ++ volatile png_imagep image = image_in; ++ volatile int result; ++ volatile png_voidp saved_error_buf; ++ jmp_buf safe_jmpbuf; ++ ++ /* Safely execute function(arg) with png_error returning to this function. */ ++ saved_error_buf = image->opaque->error_buf; ++ result = setjmp(safe_jmpbuf) == 0; ++ ++ if (result != 0) ++ { ++ ++ image->opaque->error_buf = safe_jmpbuf; ++ result = function(arg); ++ } ++ ++ image->opaque->error_buf = saved_error_buf; ++ ++ /* And do the cleanup prior to any failure return. */ ++ if (result == 0) ++ png_image_free(image); ++ ++ return result; ++} ++#endif /* SIMPLIFIED READ || SIMPLIFIED_WRITE */ ++#endif /* READ || WRITE */ +diff --git a/lib/libpng/pngget.c b/lib/libpng/pngget.c +new file mode 100644 +index 000000000..5abf1efd9 +--- /dev/null ++++ b/lib/libpng/pngget.c +@@ -0,0 +1,1249 @@ ++ ++/* pngget.c - retrieval of values from info struct ++ * ++ * Copyright (c) 2018 Cosmin Truta ++ * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson ++ * Copyright (c) 1996-1997 Andreas Dilger ++ * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc. ++ * ++ * This code is released under the libpng license. ++ * For conditions of distribution and use, see the disclaimer ++ * and license in png.h ++ * ++ */ ++ ++#include "pngpriv.h" ++ ++#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) ++ ++png_uint_32 PNGAPI ++png_get_valid(png_const_structrp png_ptr, png_const_inforp info_ptr, ++ png_uint_32 flag) ++{ ++ if (png_ptr != NULL && info_ptr != NULL) ++ return(info_ptr->valid & flag); ++ ++ return(0); ++} ++ ++size_t PNGAPI ++png_get_rowbytes(png_const_structrp png_ptr, png_const_inforp info_ptr) ++{ ++ if (png_ptr != NULL && info_ptr != NULL) ++ return(info_ptr->rowbytes); ++ ++ return(0); ++} ++ ++#ifdef PNG_INFO_IMAGE_SUPPORTED ++png_bytepp PNGAPI ++png_get_rows(png_const_structrp png_ptr, png_const_inforp info_ptr) ++{ ++ if (png_ptr != NULL && info_ptr != NULL) ++ return(info_ptr->row_pointers); ++ ++ return(0); ++} ++#endif ++ ++#ifdef PNG_EASY_ACCESS_SUPPORTED ++/* Easy access to info, added in libpng-0.99 */ ++png_uint_32 PNGAPI ++png_get_image_width(png_const_structrp png_ptr, png_const_inforp info_ptr) ++{ ++ if (png_ptr != NULL && info_ptr != NULL) ++ return info_ptr->width; ++ ++ return (0); ++} ++ ++png_uint_32 PNGAPI ++png_get_image_height(png_const_structrp png_ptr, png_const_inforp info_ptr) ++{ ++ if (png_ptr != NULL && info_ptr != NULL) ++ return info_ptr->height; ++ ++ return (0); ++} ++ ++png_byte PNGAPI ++png_get_bit_depth(png_const_structrp png_ptr, png_const_inforp info_ptr) ++{ ++ if (png_ptr != NULL && info_ptr != NULL) ++ return info_ptr->bit_depth; ++ ++ return (0); ++} ++ ++png_byte PNGAPI ++png_get_color_type(png_const_structrp png_ptr, png_const_inforp info_ptr) ++{ ++ if (png_ptr != NULL && info_ptr != NULL) ++ return info_ptr->color_type; ++ ++ return (0); ++} ++ ++png_byte PNGAPI ++png_get_filter_type(png_const_structrp png_ptr, png_const_inforp info_ptr) ++{ ++ if (png_ptr != NULL && info_ptr != NULL) ++ return info_ptr->filter_type; ++ ++ return (0); ++} ++ ++png_byte PNGAPI ++png_get_interlace_type(png_const_structrp png_ptr, png_const_inforp info_ptr) ++{ ++ if (png_ptr != NULL && info_ptr != NULL) ++ return info_ptr->interlace_type; ++ ++ return (0); ++} ++ ++png_byte PNGAPI ++png_get_compression_type(png_const_structrp png_ptr, png_const_inforp info_ptr) ++{ ++ if (png_ptr != NULL && info_ptr != NULL) ++ return info_ptr->compression_type; ++ ++ return (0); ++} ++ ++png_uint_32 PNGAPI ++png_get_x_pixels_per_meter(png_const_structrp png_ptr, png_const_inforp ++ info_ptr) ++{ ++#ifdef PNG_pHYs_SUPPORTED ++ if (png_ptr != NULL && info_ptr != NULL && ++ (info_ptr->valid & PNG_INFO_pHYs) != 0) ++ { ++ png_debug1(1, "in %s retrieval function", ++ "png_get_x_pixels_per_meter"); ++ ++ if (info_ptr->phys_unit_type == PNG_RESOLUTION_METER) ++ return (info_ptr->x_pixels_per_unit); ++ } ++#else ++ PNG_UNUSED(png_ptr) ++ PNG_UNUSED(info_ptr) ++#endif ++ ++ return (0); ++} ++ ++png_uint_32 PNGAPI ++png_get_y_pixels_per_meter(png_const_structrp png_ptr, png_const_inforp ++ info_ptr) ++{ ++#ifdef PNG_pHYs_SUPPORTED ++ if (png_ptr != NULL && info_ptr != NULL && ++ (info_ptr->valid & PNG_INFO_pHYs) != 0) ++ { ++ png_debug1(1, "in %s retrieval function", ++ "png_get_y_pixels_per_meter"); ++ ++ if (info_ptr->phys_unit_type == PNG_RESOLUTION_METER) ++ return (info_ptr->y_pixels_per_unit); ++ } ++#else ++ PNG_UNUSED(png_ptr) ++ PNG_UNUSED(info_ptr) ++#endif ++ ++ return (0); ++} ++ ++png_uint_32 PNGAPI ++png_get_pixels_per_meter(png_const_structrp png_ptr, png_const_inforp info_ptr) ++{ ++#ifdef PNG_pHYs_SUPPORTED ++ if (png_ptr != NULL && info_ptr != NULL && ++ (info_ptr->valid & PNG_INFO_pHYs) != 0) ++ { ++ png_debug1(1, "in %s retrieval function", "png_get_pixels_per_meter"); ++ ++ if (info_ptr->phys_unit_type == PNG_RESOLUTION_METER && ++ info_ptr->x_pixels_per_unit == info_ptr->y_pixels_per_unit) ++ return (info_ptr->x_pixels_per_unit); ++ } ++#else ++ PNG_UNUSED(png_ptr) ++ PNG_UNUSED(info_ptr) ++#endif ++ ++ return (0); ++} ++ ++#ifdef PNG_FLOATING_POINT_SUPPORTED ++float PNGAPI ++png_get_pixel_aspect_ratio(png_const_structrp png_ptr, png_const_inforp ++ info_ptr) ++{ ++#ifdef PNG_READ_pHYs_SUPPORTED ++ if (png_ptr != NULL && info_ptr != NULL && ++ (info_ptr->valid & PNG_INFO_pHYs) != 0) ++ { ++ png_debug1(1, "in %s retrieval function", "png_get_aspect_ratio"); ++ ++ if (info_ptr->x_pixels_per_unit != 0) ++ return ((float)((float)info_ptr->y_pixels_per_unit ++ /(float)info_ptr->x_pixels_per_unit)); ++ } ++#else ++ PNG_UNUSED(png_ptr) ++ PNG_UNUSED(info_ptr) ++#endif ++ ++ return ((float)0.0); ++} ++#endif ++ ++#ifdef PNG_FIXED_POINT_SUPPORTED ++png_fixed_point PNGAPI ++png_get_pixel_aspect_ratio_fixed(png_const_structrp png_ptr, ++ png_const_inforp info_ptr) ++{ ++#ifdef PNG_READ_pHYs_SUPPORTED ++ if (png_ptr != NULL && info_ptr != NULL && ++ (info_ptr->valid & PNG_INFO_pHYs) != 0 && ++ info_ptr->x_pixels_per_unit > 0 && info_ptr->y_pixels_per_unit > 0 && ++ info_ptr->x_pixels_per_unit <= PNG_UINT_31_MAX && ++ info_ptr->y_pixels_per_unit <= PNG_UINT_31_MAX) ++ { ++ png_fixed_point res; ++ ++ png_debug1(1, "in %s retrieval function", "png_get_aspect_ratio_fixed"); ++ ++ /* The following casts work because a PNG 4 byte integer only has a valid ++ * range of 0..2^31-1; otherwise the cast might overflow. ++ */ ++ if (png_muldiv(&res, (png_int_32)info_ptr->y_pixels_per_unit, PNG_FP_1, ++ (png_int_32)info_ptr->x_pixels_per_unit) != 0) ++ return res; ++ } ++#else ++ PNG_UNUSED(png_ptr) ++ PNG_UNUSED(info_ptr) ++#endif ++ ++ return 0; ++} ++#endif ++ ++png_int_32 PNGAPI ++png_get_x_offset_microns(png_const_structrp png_ptr, png_const_inforp info_ptr) ++{ ++#ifdef PNG_oFFs_SUPPORTED ++ if (png_ptr != NULL && info_ptr != NULL && ++ (info_ptr->valid & PNG_INFO_oFFs) != 0) ++ { ++ png_debug1(1, "in %s retrieval function", "png_get_x_offset_microns"); ++ ++ if (info_ptr->offset_unit_type == PNG_OFFSET_MICROMETER) ++ return (info_ptr->x_offset); ++ } ++#else ++ PNG_UNUSED(png_ptr) ++ PNG_UNUSED(info_ptr) ++#endif ++ ++ return (0); ++} ++ ++png_int_32 PNGAPI ++png_get_y_offset_microns(png_const_structrp png_ptr, png_const_inforp info_ptr) ++{ ++#ifdef PNG_oFFs_SUPPORTED ++ if (png_ptr != NULL && info_ptr != NULL && ++ (info_ptr->valid & PNG_INFO_oFFs) != 0) ++ { ++ png_debug1(1, "in %s retrieval function", "png_get_y_offset_microns"); ++ ++ if (info_ptr->offset_unit_type == PNG_OFFSET_MICROMETER) ++ return (info_ptr->y_offset); ++ } ++#else ++ PNG_UNUSED(png_ptr) ++ PNG_UNUSED(info_ptr) ++#endif ++ ++ return (0); ++} ++ ++png_int_32 PNGAPI ++png_get_x_offset_pixels(png_const_structrp png_ptr, png_const_inforp info_ptr) ++{ ++#ifdef PNG_oFFs_SUPPORTED ++ if (png_ptr != NULL && info_ptr != NULL && ++ (info_ptr->valid & PNG_INFO_oFFs) != 0) ++ { ++ png_debug1(1, "in %s retrieval function", "png_get_x_offset_pixels"); ++ ++ if (info_ptr->offset_unit_type == PNG_OFFSET_PIXEL) ++ return (info_ptr->x_offset); ++ } ++#else ++ PNG_UNUSED(png_ptr) ++ PNG_UNUSED(info_ptr) ++#endif ++ ++ return (0); ++} ++ ++png_int_32 PNGAPI ++png_get_y_offset_pixels(png_const_structrp png_ptr, png_const_inforp info_ptr) ++{ ++#ifdef PNG_oFFs_SUPPORTED ++ if (png_ptr != NULL && info_ptr != NULL && ++ (info_ptr->valid & PNG_INFO_oFFs) != 0) ++ { ++ png_debug1(1, "in %s retrieval function", "png_get_y_offset_pixels"); ++ ++ if (info_ptr->offset_unit_type == PNG_OFFSET_PIXEL) ++ return (info_ptr->y_offset); ++ } ++#else ++ PNG_UNUSED(png_ptr) ++ PNG_UNUSED(info_ptr) ++#endif ++ ++ return (0); ++} ++ ++#ifdef PNG_INCH_CONVERSIONS_SUPPORTED ++static png_uint_32 ++ppi_from_ppm(png_uint_32 ppm) ++{ ++#if 0 ++ /* The conversion is *(2.54/100), in binary (32 digits): ++ * .00000110100000001001110101001001 ++ */ ++ png_uint_32 t1001, t1101; ++ ppm >>= 1; /* .1 */ ++ t1001 = ppm + (ppm >> 3); /* .1001 */ ++ t1101 = t1001 + (ppm >> 1); /* .1101 */ ++ ppm >>= 20; /* .000000000000000000001 */ ++ t1101 += t1101 >> 15; /* .1101000000000001101 */ ++ t1001 >>= 11; /* .000000000001001 */ ++ t1001 += t1001 >> 12; /* .000000000001001000000001001 */ ++ ppm += t1001; /* .000000000001001000001001001 */ ++ ppm += t1101; /* .110100000001001110101001001 */ ++ return (ppm + 16) >> 5;/* .00000110100000001001110101001001 */ ++#else ++ /* The argument is a PNG unsigned integer, so it is not permitted ++ * to be bigger than 2^31. ++ */ ++ png_fixed_point result; ++ if (ppm <= PNG_UINT_31_MAX && png_muldiv(&result, (png_int_32)ppm, 127, ++ 5000) != 0) ++ return (png_uint_32)result; ++ ++ /* Overflow. */ ++ return 0; ++#endif ++} ++ ++png_uint_32 PNGAPI ++png_get_pixels_per_inch(png_const_structrp png_ptr, png_const_inforp info_ptr) ++{ ++ return ppi_from_ppm(png_get_pixels_per_meter(png_ptr, info_ptr)); ++} ++ ++png_uint_32 PNGAPI ++png_get_x_pixels_per_inch(png_const_structrp png_ptr, png_const_inforp info_ptr) ++{ ++ return ppi_from_ppm(png_get_x_pixels_per_meter(png_ptr, info_ptr)); ++} ++ ++png_uint_32 PNGAPI ++png_get_y_pixels_per_inch(png_const_structrp png_ptr, png_const_inforp info_ptr) ++{ ++ return ppi_from_ppm(png_get_y_pixels_per_meter(png_ptr, info_ptr)); ++} ++ ++#ifdef PNG_FIXED_POINT_SUPPORTED ++static png_fixed_point ++png_fixed_inches_from_microns(png_const_structrp png_ptr, png_int_32 microns) ++{ ++ /* Convert from meters * 1,000,000 to inches * 100,000, meters to ++ * inches is simply *(100/2.54), so we want *(10/2.54) == 500/127. ++ * Notice that this can overflow - a warning is output and 0 is ++ * returned. ++ */ ++ return png_muldiv_warn(png_ptr, microns, 500, 127); ++} ++ ++png_fixed_point PNGAPI ++png_get_x_offset_inches_fixed(png_const_structrp png_ptr, ++ png_const_inforp info_ptr) ++{ ++ return png_fixed_inches_from_microns(png_ptr, ++ png_get_x_offset_microns(png_ptr, info_ptr)); ++} ++#endif ++ ++#ifdef PNG_FIXED_POINT_SUPPORTED ++png_fixed_point PNGAPI ++png_get_y_offset_inches_fixed(png_const_structrp png_ptr, ++ png_const_inforp info_ptr) ++{ ++ return png_fixed_inches_from_microns(png_ptr, ++ png_get_y_offset_microns(png_ptr, info_ptr)); ++} ++#endif ++ ++#ifdef PNG_FLOATING_POINT_SUPPORTED ++float PNGAPI ++png_get_x_offset_inches(png_const_structrp png_ptr, png_const_inforp info_ptr) ++{ ++ /* To avoid the overflow do the conversion directly in floating ++ * point. ++ */ ++ return (float)(png_get_x_offset_microns(png_ptr, info_ptr) * .00003937); ++} ++#endif ++ ++#ifdef PNG_FLOATING_POINT_SUPPORTED ++float PNGAPI ++png_get_y_offset_inches(png_const_structrp png_ptr, png_const_inforp info_ptr) ++{ ++ /* To avoid the overflow do the conversion directly in floating ++ * point. ++ */ ++ return (float)(png_get_y_offset_microns(png_ptr, info_ptr) * .00003937); ++} ++#endif ++ ++#ifdef PNG_pHYs_SUPPORTED ++png_uint_32 PNGAPI ++png_get_pHYs_dpi(png_const_structrp png_ptr, png_const_inforp info_ptr, ++ png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type) ++{ ++ png_uint_32 retval = 0; ++ ++ if (png_ptr != NULL && info_ptr != NULL && ++ (info_ptr->valid & PNG_INFO_pHYs) != 0) ++ { ++ png_debug1(1, "in %s retrieval function", "pHYs"); ++ ++ if (res_x != NULL) ++ { ++ *res_x = info_ptr->x_pixels_per_unit; ++ retval |= PNG_INFO_pHYs; ++ } ++ ++ if (res_y != NULL) ++ { ++ *res_y = info_ptr->y_pixels_per_unit; ++ retval |= PNG_INFO_pHYs; ++ } ++ ++ if (unit_type != NULL) ++ { ++ *unit_type = (int)info_ptr->phys_unit_type; ++ retval |= PNG_INFO_pHYs; ++ ++ if (*unit_type == 1) ++ { ++ if (res_x != NULL) *res_x = (png_uint_32)(*res_x * .0254 + .50); ++ if (res_y != NULL) *res_y = (png_uint_32)(*res_y * .0254 + .50); ++ } ++ } ++ } ++ ++ return (retval); ++} ++#endif /* pHYs */ ++#endif /* INCH_CONVERSIONS */ ++ ++/* png_get_channels really belongs in here, too, but it's been around longer */ ++ ++#endif /* EASY_ACCESS */ ++ ++ ++png_byte PNGAPI ++png_get_channels(png_const_structrp png_ptr, png_const_inforp info_ptr) ++{ ++ if (png_ptr != NULL && info_ptr != NULL) ++ return(info_ptr->channels); ++ ++ return (0); ++} ++ ++#ifdef PNG_READ_SUPPORTED ++png_const_bytep PNGAPI ++png_get_signature(png_const_structrp png_ptr, png_const_inforp info_ptr) ++{ ++ if (png_ptr != NULL && info_ptr != NULL) ++ return(info_ptr->signature); ++ ++ return (NULL); ++} ++#endif ++ ++#ifdef PNG_bKGD_SUPPORTED ++png_uint_32 PNGAPI ++png_get_bKGD(png_const_structrp png_ptr, png_inforp info_ptr, ++ png_color_16p *background) ++{ ++ if (png_ptr != NULL && info_ptr != NULL && ++ (info_ptr->valid & PNG_INFO_bKGD) != 0 && ++ background != NULL) ++ { ++ png_debug1(1, "in %s retrieval function", "bKGD"); ++ ++ *background = &(info_ptr->background); ++ return (PNG_INFO_bKGD); ++ } ++ ++ return (0); ++} ++#endif ++ ++#ifdef PNG_cHRM_SUPPORTED ++/* The XYZ APIs were added in 1.5.5 to take advantage of the code added at the ++ * same time to correct the rgb grayscale coefficient defaults obtained from the ++ * cHRM chunk in 1.5.4 ++ */ ++# ifdef PNG_FLOATING_POINT_SUPPORTED ++png_uint_32 PNGAPI ++png_get_cHRM(png_const_structrp png_ptr, png_const_inforp info_ptr, ++ double *white_x, double *white_y, double *red_x, double *red_y, ++ double *green_x, double *green_y, double *blue_x, double *blue_y) ++{ ++ /* Quiet API change: this code used to only return the end points if a cHRM ++ * chunk was present, but the end points can also come from iCCP or sRGB ++ * chunks, so in 1.6.0 the png_get_ APIs return the end points regardless and ++ * the png_set_ APIs merely check that set end points are mutually ++ * consistent. ++ */ ++ if (png_ptr != NULL && info_ptr != NULL && ++ (info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_ENDPOINTS) != 0) ++ { ++ png_debug1(1, "in %s retrieval function", "cHRM"); ++ ++ if (white_x != NULL) ++ *white_x = png_float(png_ptr, ++ info_ptr->colorspace.end_points_xy.whitex, "cHRM white X"); ++ if (white_y != NULL) ++ *white_y = png_float(png_ptr, ++ info_ptr->colorspace.end_points_xy.whitey, "cHRM white Y"); ++ if (red_x != NULL) ++ *red_x = png_float(png_ptr, info_ptr->colorspace.end_points_xy.redx, ++ "cHRM red X"); ++ if (red_y != NULL) ++ *red_y = png_float(png_ptr, info_ptr->colorspace.end_points_xy.redy, ++ "cHRM red Y"); ++ if (green_x != NULL) ++ *green_x = png_float(png_ptr, ++ info_ptr->colorspace.end_points_xy.greenx, "cHRM green X"); ++ if (green_y != NULL) ++ *green_y = png_float(png_ptr, ++ info_ptr->colorspace.end_points_xy.greeny, "cHRM green Y"); ++ if (blue_x != NULL) ++ *blue_x = png_float(png_ptr, info_ptr->colorspace.end_points_xy.bluex, ++ "cHRM blue X"); ++ if (blue_y != NULL) ++ *blue_y = png_float(png_ptr, info_ptr->colorspace.end_points_xy.bluey, ++ "cHRM blue Y"); ++ return (PNG_INFO_cHRM); ++ } ++ ++ return (0); ++} ++ ++png_uint_32 PNGAPI ++png_get_cHRM_XYZ(png_const_structrp png_ptr, png_const_inforp info_ptr, ++ double *red_X, double *red_Y, double *red_Z, double *green_X, ++ double *green_Y, double *green_Z, double *blue_X, double *blue_Y, ++ double *blue_Z) ++{ ++ if (png_ptr != NULL && info_ptr != NULL && ++ (info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_ENDPOINTS) != 0) ++ { ++ png_debug1(1, "in %s retrieval function", "cHRM_XYZ(float)"); ++ ++ if (red_X != NULL) ++ *red_X = png_float(png_ptr, info_ptr->colorspace.end_points_XYZ.red_X, ++ "cHRM red X"); ++ if (red_Y != NULL) ++ *red_Y = png_float(png_ptr, info_ptr->colorspace.end_points_XYZ.red_Y, ++ "cHRM red Y"); ++ if (red_Z != NULL) ++ *red_Z = png_float(png_ptr, info_ptr->colorspace.end_points_XYZ.red_Z, ++ "cHRM red Z"); ++ if (green_X != NULL) ++ *green_X = png_float(png_ptr, ++ info_ptr->colorspace.end_points_XYZ.green_X, "cHRM green X"); ++ if (green_Y != NULL) ++ *green_Y = png_float(png_ptr, ++ info_ptr->colorspace.end_points_XYZ.green_Y, "cHRM green Y"); ++ if (green_Z != NULL) ++ *green_Z = png_float(png_ptr, ++ info_ptr->colorspace.end_points_XYZ.green_Z, "cHRM green Z"); ++ if (blue_X != NULL) ++ *blue_X = png_float(png_ptr, ++ info_ptr->colorspace.end_points_XYZ.blue_X, "cHRM blue X"); ++ if (blue_Y != NULL) ++ *blue_Y = png_float(png_ptr, ++ info_ptr->colorspace.end_points_XYZ.blue_Y, "cHRM blue Y"); ++ if (blue_Z != NULL) ++ *blue_Z = png_float(png_ptr, ++ info_ptr->colorspace.end_points_XYZ.blue_Z, "cHRM blue Z"); ++ return (PNG_INFO_cHRM); ++ } ++ ++ return (0); ++} ++# endif ++ ++# ifdef PNG_FIXED_POINT_SUPPORTED ++png_uint_32 PNGAPI ++png_get_cHRM_XYZ_fixed(png_const_structrp png_ptr, png_const_inforp info_ptr, ++ png_fixed_point *int_red_X, png_fixed_point *int_red_Y, ++ png_fixed_point *int_red_Z, png_fixed_point *int_green_X, ++ png_fixed_point *int_green_Y, png_fixed_point *int_green_Z, ++ png_fixed_point *int_blue_X, png_fixed_point *int_blue_Y, ++ png_fixed_point *int_blue_Z) ++{ ++ if (png_ptr != NULL && info_ptr != NULL && ++ (info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_ENDPOINTS) != 0) ++ { ++ png_debug1(1, "in %s retrieval function", "cHRM_XYZ"); ++ ++ if (int_red_X != NULL) ++ *int_red_X = info_ptr->colorspace.end_points_XYZ.red_X; ++ if (int_red_Y != NULL) ++ *int_red_Y = info_ptr->colorspace.end_points_XYZ.red_Y; ++ if (int_red_Z != NULL) ++ *int_red_Z = info_ptr->colorspace.end_points_XYZ.red_Z; ++ if (int_green_X != NULL) ++ *int_green_X = info_ptr->colorspace.end_points_XYZ.green_X; ++ if (int_green_Y != NULL) ++ *int_green_Y = info_ptr->colorspace.end_points_XYZ.green_Y; ++ if (int_green_Z != NULL) ++ *int_green_Z = info_ptr->colorspace.end_points_XYZ.green_Z; ++ if (int_blue_X != NULL) ++ *int_blue_X = info_ptr->colorspace.end_points_XYZ.blue_X; ++ if (int_blue_Y != NULL) ++ *int_blue_Y = info_ptr->colorspace.end_points_XYZ.blue_Y; ++ if (int_blue_Z != NULL) ++ *int_blue_Z = info_ptr->colorspace.end_points_XYZ.blue_Z; ++ return (PNG_INFO_cHRM); ++ } ++ ++ return (0); ++} ++ ++png_uint_32 PNGAPI ++png_get_cHRM_fixed(png_const_structrp png_ptr, png_const_inforp info_ptr, ++ png_fixed_point *white_x, png_fixed_point *white_y, png_fixed_point *red_x, ++ png_fixed_point *red_y, png_fixed_point *green_x, png_fixed_point *green_y, ++ png_fixed_point *blue_x, png_fixed_point *blue_y) ++{ ++ png_debug1(1, "in %s retrieval function", "cHRM"); ++ ++ if (png_ptr != NULL && info_ptr != NULL && ++ (info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_ENDPOINTS) != 0) ++ { ++ if (white_x != NULL) ++ *white_x = info_ptr->colorspace.end_points_xy.whitex; ++ if (white_y != NULL) ++ *white_y = info_ptr->colorspace.end_points_xy.whitey; ++ if (red_x != NULL) ++ *red_x = info_ptr->colorspace.end_points_xy.redx; ++ if (red_y != NULL) ++ *red_y = info_ptr->colorspace.end_points_xy.redy; ++ if (green_x != NULL) ++ *green_x = info_ptr->colorspace.end_points_xy.greenx; ++ if (green_y != NULL) ++ *green_y = info_ptr->colorspace.end_points_xy.greeny; ++ if (blue_x != NULL) ++ *blue_x = info_ptr->colorspace.end_points_xy.bluex; ++ if (blue_y != NULL) ++ *blue_y = info_ptr->colorspace.end_points_xy.bluey; ++ return (PNG_INFO_cHRM); ++ } ++ ++ return (0); ++} ++# endif ++#endif ++ ++#ifdef PNG_gAMA_SUPPORTED ++# ifdef PNG_FIXED_POINT_SUPPORTED ++png_uint_32 PNGAPI ++png_get_gAMA_fixed(png_const_structrp png_ptr, png_const_inforp info_ptr, ++ png_fixed_point *file_gamma) ++{ ++ png_debug1(1, "in %s retrieval function", "gAMA"); ++ ++ if (png_ptr != NULL && info_ptr != NULL && ++ (info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_GAMMA) != 0 && ++ file_gamma != NULL) ++ { ++ *file_gamma = info_ptr->colorspace.gamma; ++ return (PNG_INFO_gAMA); ++ } ++ ++ return (0); ++} ++# endif ++ ++# ifdef PNG_FLOATING_POINT_SUPPORTED ++png_uint_32 PNGAPI ++png_get_gAMA(png_const_structrp png_ptr, png_const_inforp info_ptr, ++ double *file_gamma) ++{ ++ png_debug1(1, "in %s retrieval function", "gAMA(float)"); ++ ++ if (png_ptr != NULL && info_ptr != NULL && ++ (info_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_GAMMA) != 0 && ++ file_gamma != NULL) ++ { ++ *file_gamma = png_float(png_ptr, info_ptr->colorspace.gamma, ++ "png_get_gAMA"); ++ return (PNG_INFO_gAMA); ++ } ++ ++ return (0); ++} ++# endif ++#endif ++ ++#ifdef PNG_sRGB_SUPPORTED ++png_uint_32 PNGAPI ++png_get_sRGB(png_const_structrp png_ptr, png_const_inforp info_ptr, ++ int *file_srgb_intent) ++{ ++ png_debug1(1, "in %s retrieval function", "sRGB"); ++ ++ if (png_ptr != NULL && info_ptr != NULL && ++ (info_ptr->valid & PNG_INFO_sRGB) != 0 && file_srgb_intent != NULL) ++ { ++ *file_srgb_intent = info_ptr->colorspace.rendering_intent; ++ return (PNG_INFO_sRGB); ++ } ++ ++ return (0); ++} ++#endif ++ ++#ifdef PNG_iCCP_SUPPORTED ++png_uint_32 PNGAPI ++png_get_iCCP(png_const_structrp png_ptr, png_inforp info_ptr, ++ png_charpp name, int *compression_type, ++ png_bytepp profile, png_uint_32 *proflen) ++{ ++ png_debug1(1, "in %s retrieval function", "iCCP"); ++ ++ if (png_ptr != NULL && info_ptr != NULL && ++ (info_ptr->valid & PNG_INFO_iCCP) != 0 && ++ name != NULL && profile != NULL && proflen != NULL) ++ { ++ *name = info_ptr->iccp_name; ++ *profile = info_ptr->iccp_profile; ++ *proflen = png_get_uint_32(info_ptr->iccp_profile); ++ /* This is somewhat irrelevant since the profile data returned has ++ * actually been uncompressed. ++ */ ++ if (compression_type != NULL) ++ *compression_type = PNG_COMPRESSION_TYPE_BASE; ++ return (PNG_INFO_iCCP); ++ } ++ ++ return (0); ++ ++} ++#endif ++ ++#ifdef PNG_sPLT_SUPPORTED ++int PNGAPI ++png_get_sPLT(png_const_structrp png_ptr, png_inforp info_ptr, ++ png_sPLT_tpp spalettes) ++{ ++ if (png_ptr != NULL && info_ptr != NULL && spalettes != NULL) ++ { ++ *spalettes = info_ptr->splt_palettes; ++ return info_ptr->splt_palettes_num; ++ } ++ ++ return (0); ++} ++#endif ++ ++#ifdef PNG_eXIf_SUPPORTED ++png_uint_32 PNGAPI ++png_get_eXIf(png_const_structrp png_ptr, png_inforp info_ptr, ++ png_bytep *exif) ++{ ++ png_warning(png_ptr, "png_get_eXIf does not work; use png_get_eXIf_1"); ++ PNG_UNUSED(info_ptr) ++ PNG_UNUSED(exif) ++ return 0; ++} ++ ++png_uint_32 PNGAPI ++png_get_eXIf_1(png_const_structrp png_ptr, png_const_inforp info_ptr, ++ png_uint_32 *num_exif, png_bytep *exif) ++{ ++ png_debug1(1, "in %s retrieval function", "eXIf"); ++ ++ if (png_ptr != NULL && info_ptr != NULL && ++ (info_ptr->valid & PNG_INFO_eXIf) != 0 && exif != NULL) ++ { ++ *num_exif = info_ptr->num_exif; ++ *exif = info_ptr->exif; ++ return (PNG_INFO_eXIf); ++ } ++ ++ return (0); ++} ++#endif ++ ++#ifdef PNG_hIST_SUPPORTED ++png_uint_32 PNGAPI ++png_get_hIST(png_const_structrp png_ptr, png_inforp info_ptr, ++ png_uint_16p *hist) ++{ ++ png_debug1(1, "in %s retrieval function", "hIST"); ++ ++ if (png_ptr != NULL && info_ptr != NULL && ++ (info_ptr->valid & PNG_INFO_hIST) != 0 && hist != NULL) ++ { ++ *hist = info_ptr->hist; ++ return (PNG_INFO_hIST); ++ } ++ ++ return (0); ++} ++#endif ++ ++png_uint_32 PNGAPI ++png_get_IHDR(png_const_structrp png_ptr, png_const_inforp info_ptr, ++ png_uint_32 *width, png_uint_32 *height, int *bit_depth, ++ int *color_type, int *interlace_type, int *compression_type, ++ int *filter_type) ++{ ++ png_debug1(1, "in %s retrieval function", "IHDR"); ++ ++ if (png_ptr == NULL || info_ptr == NULL) ++ return (0); ++ ++ if (width != NULL) ++ *width = info_ptr->width; ++ ++ if (height != NULL) ++ *height = info_ptr->height; ++ ++ if (bit_depth != NULL) ++ *bit_depth = info_ptr->bit_depth; ++ ++ if (color_type != NULL) ++ *color_type = info_ptr->color_type; ++ ++ if (compression_type != NULL) ++ *compression_type = info_ptr->compression_type; ++ ++ if (filter_type != NULL) ++ *filter_type = info_ptr->filter_type; ++ ++ if (interlace_type != NULL) ++ *interlace_type = info_ptr->interlace_type; ++ ++ /* This is redundant if we can be sure that the info_ptr values were all ++ * assigned in png_set_IHDR(). We do the check anyhow in case an ++ * application has ignored our advice not to mess with the members ++ * of info_ptr directly. ++ */ ++ png_check_IHDR(png_ptr, info_ptr->width, info_ptr->height, ++ info_ptr->bit_depth, info_ptr->color_type, info_ptr->interlace_type, ++ info_ptr->compression_type, info_ptr->filter_type); ++ ++ return (1); ++} ++ ++#ifdef PNG_oFFs_SUPPORTED ++png_uint_32 PNGAPI ++png_get_oFFs(png_const_structrp png_ptr, png_const_inforp info_ptr, ++ png_int_32 *offset_x, png_int_32 *offset_y, int *unit_type) ++{ ++ png_debug1(1, "in %s retrieval function", "oFFs"); ++ ++ if (png_ptr != NULL && info_ptr != NULL && ++ (info_ptr->valid & PNG_INFO_oFFs) != 0 && ++ offset_x != NULL && offset_y != NULL && unit_type != NULL) ++ { ++ *offset_x = info_ptr->x_offset; ++ *offset_y = info_ptr->y_offset; ++ *unit_type = (int)info_ptr->offset_unit_type; ++ return (PNG_INFO_oFFs); ++ } ++ ++ return (0); ++} ++#endif ++ ++#ifdef PNG_pCAL_SUPPORTED ++png_uint_32 PNGAPI ++png_get_pCAL(png_const_structrp png_ptr, png_inforp info_ptr, ++ png_charp *purpose, png_int_32 *X0, png_int_32 *X1, int *type, int *nparams, ++ png_charp *units, png_charpp *params) ++{ ++ png_debug1(1, "in %s retrieval function", "pCAL"); ++ ++ if (png_ptr != NULL && info_ptr != NULL && ++ (info_ptr->valid & PNG_INFO_pCAL) != 0 && ++ purpose != NULL && X0 != NULL && X1 != NULL && type != NULL && ++ nparams != NULL && units != NULL && params != NULL) ++ { ++ *purpose = info_ptr->pcal_purpose; ++ *X0 = info_ptr->pcal_X0; ++ *X1 = info_ptr->pcal_X1; ++ *type = (int)info_ptr->pcal_type; ++ *nparams = (int)info_ptr->pcal_nparams; ++ *units = info_ptr->pcal_units; ++ *params = info_ptr->pcal_params; ++ return (PNG_INFO_pCAL); ++ } ++ ++ return (0); ++} ++#endif ++ ++#ifdef PNG_sCAL_SUPPORTED ++# ifdef PNG_FIXED_POINT_SUPPORTED ++# if defined(PNG_FLOATING_ARITHMETIC_SUPPORTED) || \ ++ defined(PNG_FLOATING_POINT_SUPPORTED) ++png_uint_32 PNGAPI ++png_get_sCAL_fixed(png_const_structrp png_ptr, png_const_inforp info_ptr, ++ int *unit, png_fixed_point *width, png_fixed_point *height) ++{ ++ if (png_ptr != NULL && info_ptr != NULL && ++ (info_ptr->valid & PNG_INFO_sCAL) != 0) ++ { ++ *unit = info_ptr->scal_unit; ++ /*TODO: make this work without FP support; the API is currently eliminated ++ * if neither floating point APIs nor internal floating point arithmetic ++ * are enabled. ++ */ ++ *width = png_fixed(png_ptr, atof(info_ptr->scal_s_width), "sCAL width"); ++ *height = png_fixed(png_ptr, atof(info_ptr->scal_s_height), ++ "sCAL height"); ++ return (PNG_INFO_sCAL); ++ } ++ ++ return(0); ++} ++# endif /* FLOATING_ARITHMETIC */ ++# endif /* FIXED_POINT */ ++# ifdef PNG_FLOATING_POINT_SUPPORTED ++png_uint_32 PNGAPI ++png_get_sCAL(png_const_structrp png_ptr, png_const_inforp info_ptr, ++ int *unit, double *width, double *height) ++{ ++ if (png_ptr != NULL && info_ptr != NULL && ++ (info_ptr->valid & PNG_INFO_sCAL) != 0) ++ { ++ *unit = info_ptr->scal_unit; ++ *width = atof(info_ptr->scal_s_width); ++ *height = atof(info_ptr->scal_s_height); ++ return (PNG_INFO_sCAL); ++ } ++ ++ return(0); ++} ++# endif /* FLOATING POINT */ ++png_uint_32 PNGAPI ++png_get_sCAL_s(png_const_structrp png_ptr, png_const_inforp info_ptr, ++ int *unit, png_charpp width, png_charpp height) ++{ ++ if (png_ptr != NULL && info_ptr != NULL && ++ (info_ptr->valid & PNG_INFO_sCAL) != 0) ++ { ++ *unit = info_ptr->scal_unit; ++ *width = info_ptr->scal_s_width; ++ *height = info_ptr->scal_s_height; ++ return (PNG_INFO_sCAL); ++ } ++ ++ return(0); ++} ++#endif /* sCAL */ ++ ++#ifdef PNG_pHYs_SUPPORTED ++png_uint_32 PNGAPI ++png_get_pHYs(png_const_structrp png_ptr, png_const_inforp info_ptr, ++ png_uint_32 *res_x, png_uint_32 *res_y, int *unit_type) ++{ ++ png_uint_32 retval = 0; ++ ++ png_debug1(1, "in %s retrieval function", "pHYs"); ++ ++ if (png_ptr != NULL && info_ptr != NULL && ++ (info_ptr->valid & PNG_INFO_pHYs) != 0) ++ { ++ if (res_x != NULL) ++ { ++ *res_x = info_ptr->x_pixels_per_unit; ++ retval |= PNG_INFO_pHYs; ++ } ++ ++ if (res_y != NULL) ++ { ++ *res_y = info_ptr->y_pixels_per_unit; ++ retval |= PNG_INFO_pHYs; ++ } ++ ++ if (unit_type != NULL) ++ { ++ *unit_type = (int)info_ptr->phys_unit_type; ++ retval |= PNG_INFO_pHYs; ++ } ++ } ++ ++ return (retval); ++} ++#endif /* pHYs */ ++ ++png_uint_32 PNGAPI ++png_get_PLTE(png_const_structrp png_ptr, png_inforp info_ptr, ++ png_colorp *palette, int *num_palette) ++{ ++ png_debug1(1, "in %s retrieval function", "PLTE"); ++ ++ if (png_ptr != NULL && info_ptr != NULL && ++ (info_ptr->valid & PNG_INFO_PLTE) != 0 && palette != NULL) ++ { ++ *palette = info_ptr->palette; ++ *num_palette = info_ptr->num_palette; ++ png_debug1(3, "num_palette = %d", *num_palette); ++ return (PNG_INFO_PLTE); ++ } ++ ++ return (0); ++} ++ ++#ifdef PNG_sBIT_SUPPORTED ++png_uint_32 PNGAPI ++png_get_sBIT(png_const_structrp png_ptr, png_inforp info_ptr, ++ png_color_8p *sig_bit) ++{ ++ png_debug1(1, "in %s retrieval function", "sBIT"); ++ ++ if (png_ptr != NULL && info_ptr != NULL && ++ (info_ptr->valid & PNG_INFO_sBIT) != 0 && sig_bit != NULL) ++ { ++ *sig_bit = &(info_ptr->sig_bit); ++ return (PNG_INFO_sBIT); ++ } ++ ++ return (0); ++} ++#endif ++ ++#ifdef PNG_TEXT_SUPPORTED ++int PNGAPI ++png_get_text(png_const_structrp png_ptr, png_inforp info_ptr, ++ png_textp *text_ptr, int *num_text) ++{ ++ if (png_ptr != NULL && info_ptr != NULL && info_ptr->num_text > 0) ++ { ++ png_debug1(1, "in 0x%lx retrieval function", ++ (unsigned long)png_ptr->chunk_name); ++ ++ if (text_ptr != NULL) ++ *text_ptr = info_ptr->text; ++ ++ if (num_text != NULL) ++ *num_text = info_ptr->num_text; ++ ++ return info_ptr->num_text; ++ } ++ ++ if (num_text != NULL) ++ *num_text = 0; ++ ++ return(0); ++} ++#endif ++ ++#ifdef PNG_tIME_SUPPORTED ++png_uint_32 PNGAPI ++png_get_tIME(png_const_structrp png_ptr, png_inforp info_ptr, ++ png_timep *mod_time) ++{ ++ png_debug1(1, "in %s retrieval function", "tIME"); ++ ++ if (png_ptr != NULL && info_ptr != NULL && ++ (info_ptr->valid & PNG_INFO_tIME) != 0 && mod_time != NULL) ++ { ++ *mod_time = &(info_ptr->mod_time); ++ return (PNG_INFO_tIME); ++ } ++ ++ return (0); ++} ++#endif ++ ++#ifdef PNG_tRNS_SUPPORTED ++png_uint_32 PNGAPI ++png_get_tRNS(png_const_structrp png_ptr, png_inforp info_ptr, ++ png_bytep *trans_alpha, int *num_trans, png_color_16p *trans_color) ++{ ++ png_uint_32 retval = 0; ++ if (png_ptr != NULL && info_ptr != NULL && ++ (info_ptr->valid & PNG_INFO_tRNS) != 0) ++ { ++ png_debug1(1, "in %s retrieval function", "tRNS"); ++ ++ if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ++ { ++ if (trans_alpha != NULL) ++ { ++ *trans_alpha = info_ptr->trans_alpha; ++ retval |= PNG_INFO_tRNS; ++ } ++ ++ if (trans_color != NULL) ++ *trans_color = &(info_ptr->trans_color); ++ } ++ ++ else /* if (info_ptr->color_type != PNG_COLOR_TYPE_PALETTE) */ ++ { ++ if (trans_color != NULL) ++ { ++ *trans_color = &(info_ptr->trans_color); ++ retval |= PNG_INFO_tRNS; ++ } ++ ++ if (trans_alpha != NULL) ++ *trans_alpha = NULL; ++ } ++ ++ if (num_trans != NULL) ++ { ++ *num_trans = info_ptr->num_trans; ++ retval |= PNG_INFO_tRNS; ++ } ++ } ++ ++ return (retval); ++} ++#endif ++ ++#ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED ++int PNGAPI ++png_get_unknown_chunks(png_const_structrp png_ptr, png_inforp info_ptr, ++ png_unknown_chunkpp unknowns) ++{ ++ if (png_ptr != NULL && info_ptr != NULL && unknowns != NULL) ++ { ++ *unknowns = info_ptr->unknown_chunks; ++ return info_ptr->unknown_chunks_num; ++ } ++ ++ return (0); ++} ++#endif ++ ++#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED ++png_byte PNGAPI ++png_get_rgb_to_gray_status (png_const_structrp png_ptr) ++{ ++ return (png_byte)(png_ptr ? png_ptr->rgb_to_gray_status : 0); ++} ++#endif ++ ++#ifdef PNG_USER_CHUNKS_SUPPORTED ++png_voidp PNGAPI ++png_get_user_chunk_ptr(png_const_structrp png_ptr) ++{ ++ return (png_ptr ? png_ptr->user_chunk_ptr : NULL); ++} ++#endif ++ ++size_t PNGAPI ++png_get_compression_buffer_size(png_const_structrp png_ptr) ++{ ++ if (png_ptr == NULL) ++ return 0; ++ ++#ifdef PNG_WRITE_SUPPORTED ++ if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0) ++#endif ++ { ++#ifdef PNG_SEQUENTIAL_READ_SUPPORTED ++ return png_ptr->IDAT_read_size; ++#else ++ return PNG_IDAT_READ_SIZE; ++#endif ++ } ++ ++#ifdef PNG_WRITE_SUPPORTED ++ else ++ return png_ptr->zbuffer_size; ++#endif ++} ++ ++#ifdef PNG_SET_USER_LIMITS_SUPPORTED ++/* These functions were added to libpng 1.2.6 and were enabled ++ * by default in libpng-1.4.0 */ ++png_uint_32 PNGAPI ++png_get_user_width_max (png_const_structrp png_ptr) ++{ ++ return (png_ptr ? png_ptr->user_width_max : 0); ++} ++ ++png_uint_32 PNGAPI ++png_get_user_height_max (png_const_structrp png_ptr) ++{ ++ return (png_ptr ? png_ptr->user_height_max : 0); ++} ++ ++/* This function was added to libpng 1.4.0 */ ++png_uint_32 PNGAPI ++png_get_chunk_cache_max (png_const_structrp png_ptr) ++{ ++ return (png_ptr ? png_ptr->user_chunk_cache_max : 0); ++} ++ ++/* This function was added to libpng 1.4.1 */ ++png_alloc_size_t PNGAPI ++png_get_chunk_malloc_max (png_const_structrp png_ptr) ++{ ++ return (png_ptr ? png_ptr->user_chunk_malloc_max : 0); ++} ++#endif /* SET_USER_LIMITS */ ++ ++/* These functions were added to libpng 1.4.0 */ ++#ifdef PNG_IO_STATE_SUPPORTED ++png_uint_32 PNGAPI ++png_get_io_state (png_const_structrp png_ptr) ++{ ++ return png_ptr->io_state; ++} ++ ++png_uint_32 PNGAPI ++png_get_io_chunk_type (png_const_structrp png_ptr) ++{ ++ return png_ptr->chunk_name; ++} ++#endif /* IO_STATE */ ++ ++#ifdef PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED ++# ifdef PNG_GET_PALETTE_MAX_SUPPORTED ++int PNGAPI ++png_get_palette_max(png_const_structp png_ptr, png_const_infop info_ptr) ++{ ++ if (png_ptr != NULL && info_ptr != NULL) ++ return png_ptr->num_palette_max; ++ ++ return (-1); ++} ++# endif ++#endif ++ ++#endif /* READ || WRITE */ +diff --git a/lib/libpng/pnginfo.h b/lib/libpng/pnginfo.h +new file mode 100644 +index 000000000..1f98dedc4 +--- /dev/null ++++ b/lib/libpng/pnginfo.h +@@ -0,0 +1,267 @@ ++ ++/* pnginfo.h - header file for PNG reference library ++ * ++ * Copyright (c) 2018 Cosmin Truta ++ * Copyright (c) 1998-2002,2004,2006-2013,2018 Glenn Randers-Pehrson ++ * Copyright (c) 1996-1997 Andreas Dilger ++ * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc. ++ * ++ * This code is released under the libpng license. ++ * For conditions of distribution and use, see the disclaimer ++ * and license in png.h ++ */ ++ ++ /* png_info is a structure that holds the information in a PNG file so ++ * that the application can find out the characteristics of the image. ++ * If you are reading the file, this structure will tell you what is ++ * in the PNG file. If you are writing the file, fill in the information ++ * you want to put into the PNG file, using png_set_*() functions, then ++ * call png_write_info(). ++ * ++ * The names chosen should be very close to the PNG specification, so ++ * consult that document for information about the meaning of each field. ++ * ++ * With libpng < 0.95, it was only possible to directly set and read the ++ * the values in the png_info_struct, which meant that the contents and ++ * order of the values had to remain fixed. With libpng 0.95 and later, ++ * however, there are now functions that abstract the contents of ++ * png_info_struct from the application, so this makes it easier to use ++ * libpng with dynamic libraries, and even makes it possible to use ++ * libraries that don't have all of the libpng ancillary chunk-handing ++ * functionality. In libpng-1.5.0 this was moved into a separate private ++ * file that is not visible to applications. ++ * ++ * The following members may have allocated storage attached that should be ++ * cleaned up before the structure is discarded: palette, trans, text, ++ * pcal_purpose, pcal_units, pcal_params, hist, iccp_name, iccp_profile, ++ * splt_palettes, scal_unit, row_pointers, and unknowns. By default, these ++ * are automatically freed when the info structure is deallocated, if they were ++ * allocated internally by libpng. This behavior can be changed by means ++ * of the png_data_freer() function. ++ * ++ * More allocation details: all the chunk-reading functions that ++ * change these members go through the corresponding png_set_* ++ * functions. A function to clear these members is available: see ++ * png_free_data(). The png_set_* functions do not depend on being ++ * able to point info structure members to any of the storage they are ++ * passed (they make their own copies), EXCEPT that the png_set_text ++ * functions use the same storage passed to them in the text_ptr or ++ * itxt_ptr structure argument, and the png_set_rows and png_set_unknowns ++ * functions do not make their own copies. ++ */ ++#ifndef PNGINFO_H ++#define PNGINFO_H ++ ++struct png_info_def ++{ ++ /* The following are necessary for every PNG file */ ++ png_uint_32 width; /* width of image in pixels (from IHDR) */ ++ png_uint_32 height; /* height of image in pixels (from IHDR) */ ++ png_uint_32 valid; /* valid chunk data (see PNG_INFO_ below) */ ++ size_t rowbytes; /* bytes needed to hold an untransformed row */ ++ png_colorp palette; /* array of color values (valid & PNG_INFO_PLTE) */ ++ png_uint_16 num_palette; /* number of color entries in "palette" (PLTE) */ ++ png_uint_16 num_trans; /* number of transparent palette color (tRNS) */ ++ png_byte bit_depth; /* 1, 2, 4, 8, or 16 bits/channel (from IHDR) */ ++ png_byte color_type; /* see PNG_COLOR_TYPE_ below (from IHDR) */ ++ /* The following three should have been named *_method not *_type */ ++ png_byte compression_type; /* must be PNG_COMPRESSION_TYPE_BASE (IHDR) */ ++ png_byte filter_type; /* must be PNG_FILTER_TYPE_BASE (from IHDR) */ ++ png_byte interlace_type; /* One of PNG_INTERLACE_NONE, PNG_INTERLACE_ADAM7 */ ++ ++ /* The following are set by png_set_IHDR, called from the application on ++ * write, but the are never actually used by the write code. ++ */ ++ png_byte channels; /* number of data channels per pixel (1, 2, 3, 4) */ ++ png_byte pixel_depth; /* number of bits per pixel */ ++ png_byte spare_byte; /* to align the data, and for future use */ ++ ++#ifdef PNG_READ_SUPPORTED ++ /* This is never set during write */ ++ png_byte signature[8]; /* magic bytes read by libpng from start of file */ ++#endif ++ ++ /* The rest of the data is optional. If you are reading, check the ++ * valid field to see if the information in these are valid. If you ++ * are writing, set the valid field to those chunks you want written, ++ * and initialize the appropriate fields below. ++ */ ++ ++#if defined(PNG_COLORSPACE_SUPPORTED) || defined(PNG_GAMMA_SUPPORTED) ++ /* png_colorspace only contains 'flags' if neither GAMMA or COLORSPACE are ++ * defined. When COLORSPACE is switched on all the colorspace-defining ++ * chunks should be enabled, when GAMMA is switched on all the gamma-defining ++ * chunks should be enabled. If this is not done it becomes possible to read ++ * inconsistent PNG files and assign a probably incorrect interpretation to ++ * the information. (In other words, by carefully choosing which chunks to ++ * recognize the system configuration can select an interpretation for PNG ++ * files containing ambiguous data and this will result in inconsistent ++ * behavior between different libpng builds!) ++ */ ++ png_colorspace colorspace; ++#endif ++ ++#ifdef PNG_iCCP_SUPPORTED ++ /* iCCP chunk data. */ ++ png_charp iccp_name; /* profile name */ ++ png_bytep iccp_profile; /* International Color Consortium profile data */ ++ png_uint_32 iccp_proflen; /* ICC profile data length */ ++#endif ++ ++#ifdef PNG_TEXT_SUPPORTED ++ /* The tEXt, and zTXt chunks contain human-readable textual data in ++ * uncompressed, compressed, and optionally compressed forms, respectively. ++ * The data in "text" is an array of pointers to uncompressed, ++ * null-terminated C strings. Each chunk has a keyword that describes the ++ * textual data contained in that chunk. Keywords are not required to be ++ * unique, and the text string may be empty. Any number of text chunks may ++ * be in an image. ++ */ ++ int num_text; /* number of comments read or comments to write */ ++ int max_text; /* current size of text array */ ++ png_textp text; /* array of comments read or comments to write */ ++#endif /* TEXT */ ++ ++#ifdef PNG_tIME_SUPPORTED ++ /* The tIME chunk holds the last time the displayed image data was ++ * modified. See the png_time struct for the contents of this struct. ++ */ ++ png_time mod_time; ++#endif ++ ++#ifdef PNG_sBIT_SUPPORTED ++ /* The sBIT chunk specifies the number of significant high-order bits ++ * in the pixel data. Values are in the range [1, bit_depth], and are ++ * only specified for the channels in the pixel data. The contents of ++ * the low-order bits is not specified. Data is valid if ++ * (valid & PNG_INFO_sBIT) is non-zero. ++ */ ++ png_color_8 sig_bit; /* significant bits in color channels */ ++#endif ++ ++#if defined(PNG_tRNS_SUPPORTED) || defined(PNG_READ_EXPAND_SUPPORTED) || \ ++defined(PNG_READ_BACKGROUND_SUPPORTED) ++ /* The tRNS chunk supplies transparency data for paletted images and ++ * other image types that don't need a full alpha channel. There are ++ * "num_trans" transparency values for a paletted image, stored in the ++ * same order as the palette colors, starting from index 0. Values ++ * for the data are in the range [0, 255], ranging from fully transparent ++ * to fully opaque, respectively. For non-paletted images, there is a ++ * single color specified that should be treated as fully transparent. ++ * Data is valid if (valid & PNG_INFO_tRNS) is non-zero. ++ */ ++ png_bytep trans_alpha; /* alpha values for paletted image */ ++ png_color_16 trans_color; /* transparent color for non-palette image */ ++#endif ++ ++#if defined(PNG_bKGD_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) ++ /* The bKGD chunk gives the suggested image background color if the ++ * display program does not have its own background color and the image ++ * is needs to composited onto a background before display. The colors ++ * in "background" are normally in the same color space/depth as the ++ * pixel data. Data is valid if (valid & PNG_INFO_bKGD) is non-zero. ++ */ ++ png_color_16 background; ++#endif ++ ++#ifdef PNG_oFFs_SUPPORTED ++ /* The oFFs chunk gives the offset in "offset_unit_type" units rightwards ++ * and downwards from the top-left corner of the display, page, or other ++ * application-specific co-ordinate space. See the PNG_OFFSET_ defines ++ * below for the unit types. Valid if (valid & PNG_INFO_oFFs) non-zero. ++ */ ++ png_int_32 x_offset; /* x offset on page */ ++ png_int_32 y_offset; /* y offset on page */ ++ png_byte offset_unit_type; /* offset units type */ ++#endif ++ ++#ifdef PNG_pHYs_SUPPORTED ++ /* The pHYs chunk gives the physical pixel density of the image for ++ * display or printing in "phys_unit_type" units (see PNG_RESOLUTION_ ++ * defines below). Data is valid if (valid & PNG_INFO_pHYs) is non-zero. ++ */ ++ png_uint_32 x_pixels_per_unit; /* horizontal pixel density */ ++ png_uint_32 y_pixels_per_unit; /* vertical pixel density */ ++ png_byte phys_unit_type; /* resolution type (see PNG_RESOLUTION_ below) */ ++#endif ++ ++#ifdef PNG_eXIf_SUPPORTED ++ int num_exif; /* Added at libpng-1.6.31 */ ++ png_bytep exif; ++# ifdef PNG_READ_eXIf_SUPPORTED ++ png_bytep eXIf_buf; /* Added at libpng-1.6.32 */ ++# endif ++#endif ++ ++#ifdef PNG_hIST_SUPPORTED ++ /* The hIST chunk contains the relative frequency or importance of the ++ * various palette entries, so that a viewer can intelligently select a ++ * reduced-color palette, if required. Data is an array of "num_palette" ++ * values in the range [0,65535]. Data valid if (valid & PNG_INFO_hIST) ++ * is non-zero. ++ */ ++ png_uint_16p hist; ++#endif ++ ++#ifdef PNG_pCAL_SUPPORTED ++ /* The pCAL chunk describes a transformation between the stored pixel ++ * values and original physical data values used to create the image. ++ * The integer range [0, 2^bit_depth - 1] maps to the floating-point ++ * range given by [pcal_X0, pcal_X1], and are further transformed by a ++ * (possibly non-linear) transformation function given by "pcal_type" ++ * and "pcal_params" into "pcal_units". Please see the PNG_EQUATION_ ++ * defines below, and the PNG-Group's PNG extensions document for a ++ * complete description of the transformations and how they should be ++ * implemented, and for a description of the ASCII parameter strings. ++ * Data values are valid if (valid & PNG_INFO_pCAL) non-zero. ++ */ ++ png_charp pcal_purpose; /* pCAL chunk description string */ ++ png_int_32 pcal_X0; /* minimum value */ ++ png_int_32 pcal_X1; /* maximum value */ ++ png_charp pcal_units; /* Latin-1 string giving physical units */ ++ png_charpp pcal_params; /* ASCII strings containing parameter values */ ++ png_byte pcal_type; /* equation type (see PNG_EQUATION_ below) */ ++ png_byte pcal_nparams; /* number of parameters given in pcal_params */ ++#endif ++ ++/* New members added in libpng-1.0.6 */ ++ png_uint_32 free_me; /* flags items libpng is responsible for freeing */ ++ ++#ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED ++ /* Storage for unknown chunks that the library doesn't recognize. */ ++ png_unknown_chunkp unknown_chunks; ++ ++ /* The type of this field is limited by the type of ++ * png_struct::user_chunk_cache_max, else overflow can occur. ++ */ ++ int unknown_chunks_num; ++#endif ++ ++#ifdef PNG_sPLT_SUPPORTED ++ /* Data on sPLT chunks (there may be more than one). */ ++ png_sPLT_tp splt_palettes; ++ int splt_palettes_num; /* Match type returned by png_get API */ ++#endif ++ ++#ifdef PNG_sCAL_SUPPORTED ++ /* The sCAL chunk describes the actual physical dimensions of the ++ * subject matter of the graphic. The chunk contains a unit specification ++ * a byte value, and two ASCII strings representing floating-point ++ * values. The values are width and height corresponding to one pixel ++ * in the image. Data values are valid if (valid & PNG_INFO_sCAL) is ++ * non-zero. ++ */ ++ png_byte scal_unit; /* unit of physical scale */ ++ png_charp scal_s_width; /* string containing height */ ++ png_charp scal_s_height; /* string containing width */ ++#endif ++ ++#ifdef PNG_INFO_IMAGE_SUPPORTED ++ /* Memory has been allocated if (valid & PNG_ALLOCATED_INFO_ROWS) ++ non-zero */ ++ /* Data valid if (valid & PNG_INFO_IDAT) non-zero */ ++ png_bytepp row_pointers; /* the image bits */ ++#endif ++ ++}; ++#endif /* PNGINFO_H */ +diff --git a/lib/libpng/pngmem.c b/lib/libpng/pngmem.c +new file mode 100644 +index 000000000..09ed9c1c9 +--- /dev/null ++++ b/lib/libpng/pngmem.c +@@ -0,0 +1,284 @@ ++ ++/* pngmem.c - stub functions for memory allocation ++ * ++ * Copyright (c) 2018 Cosmin Truta ++ * Copyright (c) 1998-2002,2004,2006-2014,2016 Glenn Randers-Pehrson ++ * Copyright (c) 1996-1997 Andreas Dilger ++ * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc. ++ * ++ * This code is released under the libpng license. ++ * For conditions of distribution and use, see the disclaimer ++ * and license in png.h ++ * ++ * This file provides a location for all memory allocation. Users who ++ * need special memory handling are expected to supply replacement ++ * functions for png_malloc() and png_free(), and to use ++ * png_create_read_struct_2() and png_create_write_struct_2() to ++ * identify the replacement functions. ++ */ ++ ++#include "pngpriv.h" ++ ++#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) ++/* Free a png_struct */ ++void /* PRIVATE */ ++png_destroy_png_struct(png_structrp png_ptr) ++{ ++ if (png_ptr != NULL) ++ { ++ /* png_free might call png_error and may certainly call ++ * png_get_mem_ptr, so fake a temporary png_struct to support this. ++ */ ++ png_struct dummy_struct = *png_ptr; ++ memset(png_ptr, 0, (sizeof *png_ptr)); ++ png_free(&dummy_struct, png_ptr); ++ ++# ifdef PNG_SETJMP_SUPPORTED ++ /* We may have a jmp_buf left to deallocate. */ ++ png_free_jmpbuf(&dummy_struct); ++# endif ++ } ++} ++ ++/* Allocate memory. For reasonable files, size should never exceed ++ * 64K. However, zlib may allocate more than 64K if you don't tell ++ * it not to. See zconf.h and png.h for more information. zlib does ++ * need to allocate exactly 64K, so whatever you call here must ++ * have the ability to do that. ++ */ ++PNG_FUNCTION(png_voidp,PNGAPI ++png_calloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED) ++{ ++ png_voidp ret; ++ ++ ret = png_malloc(png_ptr, size); ++ ++ if (ret != NULL) ++ memset(ret, 0, size); ++ ++ return ret; ++} ++ ++/* png_malloc_base, an internal function added at libpng 1.6.0, does the work of ++ * allocating memory, taking into account limits and PNG_USER_MEM_SUPPORTED. ++ * Checking and error handling must happen outside this routine; it returns NULL ++ * if the allocation cannot be done (for any reason.) ++ */ ++PNG_FUNCTION(png_voidp /* PRIVATE */, ++png_malloc_base,(png_const_structrp png_ptr, png_alloc_size_t size), ++ PNG_ALLOCATED) ++{ ++ /* Moved to png_malloc_base from png_malloc_default in 1.6.0; the DOS ++ * allocators have also been removed in 1.6.0, so any 16-bit system now has ++ * to implement a user memory handler. This checks to be sure it isn't ++ * called with big numbers. ++ */ ++#ifndef PNG_USER_MEM_SUPPORTED ++ PNG_UNUSED(png_ptr) ++#endif ++ ++ /* Some compilers complain that this is always true. However, it ++ * can be false when integer overflow happens. ++ */ ++ if (size > 0 && size <= PNG_SIZE_MAX ++# ifdef PNG_MAX_MALLOC_64K ++ && size <= 65536U ++# endif ++ ) ++ { ++#ifdef PNG_USER_MEM_SUPPORTED ++ if (png_ptr != NULL && png_ptr->malloc_fn != NULL) ++ return png_ptr->malloc_fn(png_constcast(png_structrp,png_ptr), size); ++ ++ else ++#endif ++ return malloc((size_t)size); /* checked for truncation above */ ++ } ++ ++ else ++ return NULL; ++} ++ ++#if defined(PNG_TEXT_SUPPORTED) || defined(PNG_sPLT_SUPPORTED) ||\ ++ defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED) ++/* This is really here only to work round a spurious warning in GCC 4.6 and 4.7 ++ * that arises because of the checks in png_realloc_array that are repeated in ++ * png_malloc_array. ++ */ ++static png_voidp ++png_malloc_array_checked(png_const_structrp png_ptr, int nelements, ++ size_t element_size) ++{ ++ png_alloc_size_t req = (png_alloc_size_t)nelements; /* known to be > 0 */ ++ ++ if (req <= PNG_SIZE_MAX/element_size) ++ return png_malloc_base(png_ptr, req * element_size); ++ ++ /* The failure case when the request is too large */ ++ return NULL; ++} ++ ++PNG_FUNCTION(png_voidp /* PRIVATE */, ++png_malloc_array,(png_const_structrp png_ptr, int nelements, ++ size_t element_size),PNG_ALLOCATED) ++{ ++ if (nelements <= 0 || element_size == 0) ++ png_error(png_ptr, "internal error: array alloc"); ++ ++ return png_malloc_array_checked(png_ptr, nelements, element_size); ++} ++ ++PNG_FUNCTION(png_voidp /* PRIVATE */, ++png_realloc_array,(png_const_structrp png_ptr, png_const_voidp old_array, ++ int old_elements, int add_elements, size_t element_size),PNG_ALLOCATED) ++{ ++ /* These are internal errors: */ ++ if (add_elements <= 0 || element_size == 0 || old_elements < 0 || ++ (old_array == NULL && old_elements > 0)) ++ png_error(png_ptr, "internal error: array realloc"); ++ ++ /* Check for overflow on the elements count (so the caller does not have to ++ * check.) ++ */ ++ if (add_elements <= INT_MAX - old_elements) ++ { ++ png_voidp new_array = png_malloc_array_checked(png_ptr, ++ old_elements+add_elements, element_size); ++ ++ if (new_array != NULL) ++ { ++ /* Because png_malloc_array worked the size calculations below cannot ++ * overflow. ++ */ ++ if (old_elements > 0) ++ memcpy(new_array, old_array, element_size*(unsigned)old_elements); ++ ++ memset((char*)new_array + element_size*(unsigned)old_elements, 0, ++ element_size*(unsigned)add_elements); ++ ++ return new_array; ++ } ++ } ++ ++ return NULL; /* error */ ++} ++#endif /* TEXT || sPLT || STORE_UNKNOWN_CHUNKS */ ++ ++/* Various functions that have different error handling are derived from this. ++ * png_malloc always exists, but if PNG_USER_MEM_SUPPORTED is defined a separate ++ * function png_malloc_default is also provided. ++ */ ++PNG_FUNCTION(png_voidp,PNGAPI ++png_malloc,(png_const_structrp png_ptr, png_alloc_size_t size),PNG_ALLOCATED) ++{ ++ png_voidp ret; ++ ++ if (png_ptr == NULL) ++ return NULL; ++ ++ ret = png_malloc_base(png_ptr, size); ++ ++ if (ret == NULL) ++ png_error(png_ptr, "Out of memory"); /* 'm' means png_malloc */ ++ ++ return ret; ++} ++ ++#ifdef PNG_USER_MEM_SUPPORTED ++PNG_FUNCTION(png_voidp,PNGAPI ++png_malloc_default,(png_const_structrp png_ptr, png_alloc_size_t size), ++ PNG_ALLOCATED PNG_DEPRECATED) ++{ ++ png_voidp ret; ++ ++ if (png_ptr == NULL) ++ return NULL; ++ ++ /* Passing 'NULL' here bypasses the application provided memory handler. */ ++ ret = png_malloc_base(NULL/*use malloc*/, size); ++ ++ if (ret == NULL) ++ png_error(png_ptr, "Out of Memory"); /* 'M' means png_malloc_default */ ++ ++ return ret; ++} ++#endif /* USER_MEM */ ++ ++/* This function was added at libpng version 1.2.3. The png_malloc_warn() ++ * function will issue a png_warning and return NULL instead of issuing a ++ * png_error, if it fails to allocate the requested memory. ++ */ ++PNG_FUNCTION(png_voidp,PNGAPI ++png_malloc_warn,(png_const_structrp png_ptr, png_alloc_size_t size), ++ PNG_ALLOCATED) ++{ ++ if (png_ptr != NULL) ++ { ++ png_voidp ret = png_malloc_base(png_ptr, size); ++ ++ if (ret != NULL) ++ return ret; ++ ++ png_warning(png_ptr, "Out of memory"); ++ } ++ ++ return NULL; ++} ++ ++/* Free a pointer allocated by png_malloc(). If ptr is NULL, return ++ * without taking any action. ++ */ ++void PNGAPI ++png_free(png_const_structrp png_ptr, png_voidp ptr) ++{ ++ if (png_ptr == NULL || ptr == NULL) ++ return; ++ ++#ifdef PNG_USER_MEM_SUPPORTED ++ if (png_ptr->free_fn != NULL) ++ png_ptr->free_fn(png_constcast(png_structrp,png_ptr), ptr); ++ ++ else ++ png_free_default(png_ptr, ptr); ++} ++ ++PNG_FUNCTION(void,PNGAPI ++png_free_default,(png_const_structrp png_ptr, png_voidp ptr),PNG_DEPRECATED) ++{ ++ if (png_ptr == NULL || ptr == NULL) ++ return; ++#endif /* USER_MEM */ ++ ++ free(ptr); ++} ++ ++#ifdef PNG_USER_MEM_SUPPORTED ++/* This function is called when the application wants to use another method ++ * of allocating and freeing memory. ++ */ ++void PNGAPI ++png_set_mem_fn(png_structrp png_ptr, png_voidp mem_ptr, png_malloc_ptr ++ malloc_fn, png_free_ptr free_fn) ++{ ++ if (png_ptr != NULL) ++ { ++ png_ptr->mem_ptr = mem_ptr; ++ png_ptr->malloc_fn = malloc_fn; ++ png_ptr->free_fn = free_fn; ++ } ++} ++ ++/* This function returns a pointer to the mem_ptr associated with the user ++ * functions. The application should free any memory associated with this ++ * pointer before png_write_destroy and png_read_destroy are called. ++ */ ++png_voidp PNGAPI ++png_get_mem_ptr(png_const_structrp png_ptr) ++{ ++ if (png_ptr == NULL) ++ return NULL; ++ ++ return png_ptr->mem_ptr; ++} ++#endif /* USER_MEM */ ++#endif /* READ || WRITE */ +diff --git a/lib/libpng/pngpread.c b/lib/libpng/pngpread.c +new file mode 100644 +index 000000000..e283627b7 +--- /dev/null ++++ b/lib/libpng/pngpread.c +@@ -0,0 +1,1096 @@ ++ ++/* pngpread.c - read a png file in push mode ++ * ++ * Copyright (c) 2018 Cosmin Truta ++ * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson ++ * Copyright (c) 1996-1997 Andreas Dilger ++ * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc. ++ * ++ * This code is released under the libpng license. ++ * For conditions of distribution and use, see the disclaimer ++ * and license in png.h ++ */ ++ ++#include "pngpriv.h" ++ ++#ifdef PNG_PROGRESSIVE_READ_SUPPORTED ++ ++/* Push model modes */ ++#define PNG_READ_SIG_MODE 0 ++#define PNG_READ_CHUNK_MODE 1 ++#define PNG_READ_IDAT_MODE 2 ++#define PNG_READ_tEXt_MODE 4 ++#define PNG_READ_zTXt_MODE 5 ++#define PNG_READ_DONE_MODE 6 ++#define PNG_READ_iTXt_MODE 7 ++#define PNG_ERROR_MODE 8 ++ ++#define PNG_PUSH_SAVE_BUFFER_IF_FULL \ ++if (png_ptr->push_length + 4 > png_ptr->buffer_size) \ ++ { png_push_save_buffer(png_ptr); return; } ++#define PNG_PUSH_SAVE_BUFFER_IF_LT(N) \ ++if (png_ptr->buffer_size < N) \ ++ { png_push_save_buffer(png_ptr); return; } ++ ++void PNGAPI ++png_process_data(png_structrp png_ptr, png_inforp info_ptr, ++ png_bytep buffer, size_t buffer_size) ++{ ++ if (png_ptr == NULL || info_ptr == NULL) ++ return; ++ ++ png_push_restore_buffer(png_ptr, buffer, buffer_size); ++ ++ while (png_ptr->buffer_size) ++ { ++ png_process_some_data(png_ptr, info_ptr); ++ } ++} ++ ++size_t PNGAPI ++png_process_data_pause(png_structrp png_ptr, int save) ++{ ++ if (png_ptr != NULL) ++ { ++ /* It's easiest for the caller if we do the save; then the caller doesn't ++ * have to supply the same data again: ++ */ ++ if (save != 0) ++ png_push_save_buffer(png_ptr); ++ else ++ { ++ /* This includes any pending saved bytes: */ ++ size_t remaining = png_ptr->buffer_size; ++ png_ptr->buffer_size = 0; ++ ++ /* So subtract the saved buffer size, unless all the data ++ * is actually 'saved', in which case we just return 0 ++ */ ++ if (png_ptr->save_buffer_size < remaining) ++ return remaining - png_ptr->save_buffer_size; ++ } ++ } ++ ++ return 0; ++} ++ ++png_uint_32 PNGAPI ++png_process_data_skip(png_structrp png_ptr) ++{ ++/* TODO: Deprecate and remove this API. ++ * Somewhere the implementation of this seems to have been lost, ++ * or abandoned. It was only to support some internal back-door access ++ * to png_struct) in libpng-1.4.x. ++ */ ++ png_app_warning(png_ptr, ++"png_process_data_skip is not implemented in any current version of libpng"); ++ return 0; ++} ++ ++/* What we do with the incoming data depends on what we were previously ++ * doing before we ran out of data... ++ */ ++void /* PRIVATE */ ++png_process_some_data(png_structrp png_ptr, png_inforp info_ptr) ++{ ++ if (png_ptr == NULL) ++ return; ++ ++ switch (png_ptr->process_mode) ++ { ++ case PNG_READ_SIG_MODE: ++ { ++ png_push_read_sig(png_ptr, info_ptr); ++ break; ++ } ++ ++ case PNG_READ_CHUNK_MODE: ++ { ++ png_push_read_chunk(png_ptr, info_ptr); ++ break; ++ } ++ ++ case PNG_READ_IDAT_MODE: ++ { ++ png_push_read_IDAT(png_ptr); ++ break; ++ } ++ ++ default: ++ { ++ png_ptr->buffer_size = 0; ++ break; ++ } ++ } ++} ++ ++/* Read any remaining signature bytes from the stream and compare them with ++ * the correct PNG signature. It is possible that this routine is called ++ * with bytes already read from the signature, either because they have been ++ * checked by the calling application, or because of multiple calls to this ++ * routine. ++ */ ++void /* PRIVATE */ ++png_push_read_sig(png_structrp png_ptr, png_inforp info_ptr) ++{ ++ size_t num_checked = png_ptr->sig_bytes; /* SAFE, does not exceed 8 */ ++ size_t num_to_check = 8 - num_checked; ++ ++ if (png_ptr->buffer_size < num_to_check) ++ { ++ num_to_check = png_ptr->buffer_size; ++ } ++ ++ png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]), ++ num_to_check); ++ png_ptr->sig_bytes = (png_byte)(png_ptr->sig_bytes + num_to_check); ++ ++ if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check)) ++ { ++ if (num_checked < 4 && ++ png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4)) ++ png_error(png_ptr, "Not a PNG file"); ++ ++ else ++ png_error(png_ptr, "PNG file corrupted by ASCII conversion"); ++ } ++ else ++ { ++ if (png_ptr->sig_bytes >= 8) ++ { ++ png_ptr->process_mode = PNG_READ_CHUNK_MODE; ++ } ++ } ++} ++ ++void /* PRIVATE */ ++png_push_read_chunk(png_structrp png_ptr, png_inforp info_ptr) ++{ ++ png_uint_32 chunk_name; ++#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED ++ int keep; /* unknown handling method */ ++#endif ++ ++ /* First we make sure we have enough data for the 4-byte chunk name ++ * and the 4-byte chunk length before proceeding with decoding the ++ * chunk data. To fully decode each of these chunks, we also make ++ * sure we have enough data in the buffer for the 4-byte CRC at the ++ * end of every chunk (except IDAT, which is handled separately). ++ */ ++ if ((png_ptr->mode & PNG_HAVE_CHUNK_HEADER) == 0) ++ { ++ png_byte chunk_length[4]; ++ png_byte chunk_tag[4]; ++ ++ PNG_PUSH_SAVE_BUFFER_IF_LT(8) ++ png_push_fill_buffer(png_ptr, chunk_length, 4); ++ png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length); ++ png_reset_crc(png_ptr); ++ png_crc_read(png_ptr, chunk_tag, 4); ++ png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag); ++ png_check_chunk_name(png_ptr, png_ptr->chunk_name); ++ png_check_chunk_length(png_ptr, png_ptr->push_length); ++ png_ptr->mode |= PNG_HAVE_CHUNK_HEADER; ++ } ++ ++ chunk_name = png_ptr->chunk_name; ++ ++ if (chunk_name == png_IDAT) ++ { ++ if ((png_ptr->mode & PNG_AFTER_IDAT) != 0) ++ png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT; ++ ++ /* If we reach an IDAT chunk, this means we have read all of the ++ * header chunks, and we can start reading the image (or if this ++ * is called after the image has been read - we have an error). ++ */ ++ if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) ++ png_error(png_ptr, "Missing IHDR before IDAT"); ++ ++ else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && ++ (png_ptr->mode & PNG_HAVE_PLTE) == 0) ++ png_error(png_ptr, "Missing PLTE before IDAT"); ++ ++ png_ptr->process_mode = PNG_READ_IDAT_MODE; ++ ++ if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) ++ if ((png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) == 0) ++ if (png_ptr->push_length == 0) ++ return; ++ ++ png_ptr->mode |= PNG_HAVE_IDAT; ++ ++ if ((png_ptr->mode & PNG_AFTER_IDAT) != 0) ++ png_benign_error(png_ptr, "Too many IDATs found"); ++ } ++ ++ if (chunk_name == png_IHDR) ++ { ++ if (png_ptr->push_length != 13) ++ png_error(png_ptr, "Invalid IHDR length"); ++ ++ PNG_PUSH_SAVE_BUFFER_IF_FULL ++ png_handle_IHDR(png_ptr, info_ptr, png_ptr->push_length); ++ } ++ ++ else if (chunk_name == png_IEND) ++ { ++ PNG_PUSH_SAVE_BUFFER_IF_FULL ++ png_handle_IEND(png_ptr, info_ptr, png_ptr->push_length); ++ ++ png_ptr->process_mode = PNG_READ_DONE_MODE; ++ png_push_have_end(png_ptr, info_ptr); ++ } ++ ++#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED ++ else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0) ++ { ++ PNG_PUSH_SAVE_BUFFER_IF_FULL ++ png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length, keep); ++ ++ if (chunk_name == png_PLTE) ++ png_ptr->mode |= PNG_HAVE_PLTE; ++ } ++#endif ++ ++ else if (chunk_name == png_PLTE) ++ { ++ PNG_PUSH_SAVE_BUFFER_IF_FULL ++ png_handle_PLTE(png_ptr, info_ptr, png_ptr->push_length); ++ } ++ ++ else if (chunk_name == png_IDAT) ++ { ++ png_ptr->idat_size = png_ptr->push_length; ++ png_ptr->process_mode = PNG_READ_IDAT_MODE; ++ png_push_have_info(png_ptr, info_ptr); ++ png_ptr->zstream.avail_out = ++ (uInt) PNG_ROWBYTES(png_ptr->pixel_depth, ++ png_ptr->iwidth) + 1; ++ png_ptr->zstream.next_out = png_ptr->row_buf; ++ return; ++ } ++ ++#ifdef PNG_READ_gAMA_SUPPORTED ++ else if (png_ptr->chunk_name == png_gAMA) ++ { ++ PNG_PUSH_SAVE_BUFFER_IF_FULL ++ png_handle_gAMA(png_ptr, info_ptr, png_ptr->push_length); ++ } ++ ++#endif ++#ifdef PNG_READ_sBIT_SUPPORTED ++ else if (png_ptr->chunk_name == png_sBIT) ++ { ++ PNG_PUSH_SAVE_BUFFER_IF_FULL ++ png_handle_sBIT(png_ptr, info_ptr, png_ptr->push_length); ++ } ++ ++#endif ++#ifdef PNG_READ_cHRM_SUPPORTED ++ else if (png_ptr->chunk_name == png_cHRM) ++ { ++ PNG_PUSH_SAVE_BUFFER_IF_FULL ++ png_handle_cHRM(png_ptr, info_ptr, png_ptr->push_length); ++ } ++ ++#endif ++#ifdef PNG_READ_sRGB_SUPPORTED ++ else if (chunk_name == png_sRGB) ++ { ++ PNG_PUSH_SAVE_BUFFER_IF_FULL ++ png_handle_sRGB(png_ptr, info_ptr, png_ptr->push_length); ++ } ++ ++#endif ++#ifdef PNG_READ_iCCP_SUPPORTED ++ else if (png_ptr->chunk_name == png_iCCP) ++ { ++ PNG_PUSH_SAVE_BUFFER_IF_FULL ++ png_handle_iCCP(png_ptr, info_ptr, png_ptr->push_length); ++ } ++ ++#endif ++#ifdef PNG_READ_sPLT_SUPPORTED ++ else if (chunk_name == png_sPLT) ++ { ++ PNG_PUSH_SAVE_BUFFER_IF_FULL ++ png_handle_sPLT(png_ptr, info_ptr, png_ptr->push_length); ++ } ++ ++#endif ++#ifdef PNG_READ_tRNS_SUPPORTED ++ else if (chunk_name == png_tRNS) ++ { ++ PNG_PUSH_SAVE_BUFFER_IF_FULL ++ png_handle_tRNS(png_ptr, info_ptr, png_ptr->push_length); ++ } ++ ++#endif ++#ifdef PNG_READ_bKGD_SUPPORTED ++ else if (chunk_name == png_bKGD) ++ { ++ PNG_PUSH_SAVE_BUFFER_IF_FULL ++ png_handle_bKGD(png_ptr, info_ptr, png_ptr->push_length); ++ } ++ ++#endif ++#ifdef PNG_READ_hIST_SUPPORTED ++ else if (chunk_name == png_hIST) ++ { ++ PNG_PUSH_SAVE_BUFFER_IF_FULL ++ png_handle_hIST(png_ptr, info_ptr, png_ptr->push_length); ++ } ++ ++#endif ++#ifdef PNG_READ_pHYs_SUPPORTED ++ else if (chunk_name == png_pHYs) ++ { ++ PNG_PUSH_SAVE_BUFFER_IF_FULL ++ png_handle_pHYs(png_ptr, info_ptr, png_ptr->push_length); ++ } ++ ++#endif ++#ifdef PNG_READ_oFFs_SUPPORTED ++ else if (chunk_name == png_oFFs) ++ { ++ PNG_PUSH_SAVE_BUFFER_IF_FULL ++ png_handle_oFFs(png_ptr, info_ptr, png_ptr->push_length); ++ } ++#endif ++ ++#ifdef PNG_READ_pCAL_SUPPORTED ++ else if (chunk_name == png_pCAL) ++ { ++ PNG_PUSH_SAVE_BUFFER_IF_FULL ++ png_handle_pCAL(png_ptr, info_ptr, png_ptr->push_length); ++ } ++ ++#endif ++#ifdef PNG_READ_sCAL_SUPPORTED ++ else if (chunk_name == png_sCAL) ++ { ++ PNG_PUSH_SAVE_BUFFER_IF_FULL ++ png_handle_sCAL(png_ptr, info_ptr, png_ptr->push_length); ++ } ++ ++#endif ++#ifdef PNG_READ_tIME_SUPPORTED ++ else if (chunk_name == png_tIME) ++ { ++ PNG_PUSH_SAVE_BUFFER_IF_FULL ++ png_handle_tIME(png_ptr, info_ptr, png_ptr->push_length); ++ } ++ ++#endif ++#ifdef PNG_READ_tEXt_SUPPORTED ++ else if (chunk_name == png_tEXt) ++ { ++ PNG_PUSH_SAVE_BUFFER_IF_FULL ++ png_handle_tEXt(png_ptr, info_ptr, png_ptr->push_length); ++ } ++ ++#endif ++#ifdef PNG_READ_zTXt_SUPPORTED ++ else if (chunk_name == png_zTXt) ++ { ++ PNG_PUSH_SAVE_BUFFER_IF_FULL ++ png_handle_zTXt(png_ptr, info_ptr, png_ptr->push_length); ++ } ++ ++#endif ++#ifdef PNG_READ_iTXt_SUPPORTED ++ else if (chunk_name == png_iTXt) ++ { ++ PNG_PUSH_SAVE_BUFFER_IF_FULL ++ png_handle_iTXt(png_ptr, info_ptr, png_ptr->push_length); ++ } ++#endif ++ ++ else ++ { ++ PNG_PUSH_SAVE_BUFFER_IF_FULL ++ png_handle_unknown(png_ptr, info_ptr, png_ptr->push_length, ++ PNG_HANDLE_CHUNK_AS_DEFAULT); ++ } ++ ++ png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER; ++} ++ ++void PNGCBAPI ++png_push_fill_buffer(png_structp png_ptr, png_bytep buffer, size_t length) ++{ ++ png_bytep ptr; ++ ++ if (png_ptr == NULL) ++ return; ++ ++ ptr = buffer; ++ if (png_ptr->save_buffer_size != 0) ++ { ++ size_t save_size; ++ ++ if (length < png_ptr->save_buffer_size) ++ save_size = length; ++ ++ else ++ save_size = png_ptr->save_buffer_size; ++ ++ memcpy(ptr, png_ptr->save_buffer_ptr, save_size); ++ length -= save_size; ++ ptr += save_size; ++ png_ptr->buffer_size -= save_size; ++ png_ptr->save_buffer_size -= save_size; ++ png_ptr->save_buffer_ptr += save_size; ++ } ++ if (length != 0 && png_ptr->current_buffer_size != 0) ++ { ++ size_t save_size; ++ ++ if (length < png_ptr->current_buffer_size) ++ save_size = length; ++ ++ else ++ save_size = png_ptr->current_buffer_size; ++ ++ memcpy(ptr, png_ptr->current_buffer_ptr, save_size); ++ png_ptr->buffer_size -= save_size; ++ png_ptr->current_buffer_size -= save_size; ++ png_ptr->current_buffer_ptr += save_size; ++ } ++} ++ ++void /* PRIVATE */ ++png_push_save_buffer(png_structrp png_ptr) ++{ ++ if (png_ptr->save_buffer_size != 0) ++ { ++ if (png_ptr->save_buffer_ptr != png_ptr->save_buffer) ++ { ++ size_t i, istop; ++ png_bytep sp; ++ png_bytep dp; ++ ++ istop = png_ptr->save_buffer_size; ++ for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer; ++ i < istop; i++, sp++, dp++) ++ { ++ *dp = *sp; ++ } ++ } ++ } ++ if (png_ptr->save_buffer_size + png_ptr->current_buffer_size > ++ png_ptr->save_buffer_max) ++ { ++ size_t new_max; ++ png_bytep old_buffer; ++ ++ if (png_ptr->save_buffer_size > PNG_SIZE_MAX - ++ (png_ptr->current_buffer_size + 256)) ++ { ++ png_error(png_ptr, "Potential overflow of save_buffer"); ++ } ++ ++ new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256; ++ old_buffer = png_ptr->save_buffer; ++ png_ptr->save_buffer = (png_bytep)png_malloc_warn(png_ptr, ++ (size_t)new_max); ++ ++ if (png_ptr->save_buffer == NULL) ++ { ++ png_free(png_ptr, old_buffer); ++ png_error(png_ptr, "Insufficient memory for save_buffer"); ++ } ++ ++ if (old_buffer) ++ memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size); ++ else if (png_ptr->save_buffer_size) ++ png_error(png_ptr, "save_buffer error"); ++ png_free(png_ptr, old_buffer); ++ png_ptr->save_buffer_max = new_max; ++ } ++ if (png_ptr->current_buffer_size) ++ { ++ memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size, ++ png_ptr->current_buffer_ptr, png_ptr->current_buffer_size); ++ png_ptr->save_buffer_size += png_ptr->current_buffer_size; ++ png_ptr->current_buffer_size = 0; ++ } ++ png_ptr->save_buffer_ptr = png_ptr->save_buffer; ++ png_ptr->buffer_size = 0; ++} ++ ++void /* PRIVATE */ ++png_push_restore_buffer(png_structrp png_ptr, png_bytep buffer, ++ size_t buffer_length) ++{ ++ png_ptr->current_buffer = buffer; ++ png_ptr->current_buffer_size = buffer_length; ++ png_ptr->buffer_size = buffer_length + png_ptr->save_buffer_size; ++ png_ptr->current_buffer_ptr = png_ptr->current_buffer; ++} ++ ++void /* PRIVATE */ ++png_push_read_IDAT(png_structrp png_ptr) ++{ ++ if ((png_ptr->mode & PNG_HAVE_CHUNK_HEADER) == 0) ++ { ++ png_byte chunk_length[4]; ++ png_byte chunk_tag[4]; ++ ++ /* TODO: this code can be commoned up with the same code in push_read */ ++ PNG_PUSH_SAVE_BUFFER_IF_LT(8) ++ png_push_fill_buffer(png_ptr, chunk_length, 4); ++ png_ptr->push_length = png_get_uint_31(png_ptr, chunk_length); ++ png_reset_crc(png_ptr); ++ png_crc_read(png_ptr, chunk_tag, 4); ++ png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(chunk_tag); ++ png_ptr->mode |= PNG_HAVE_CHUNK_HEADER; ++ ++ if (png_ptr->chunk_name != png_IDAT) ++ { ++ png_ptr->process_mode = PNG_READ_CHUNK_MODE; ++ ++ if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0) ++ png_error(png_ptr, "Not enough compressed data"); ++ ++ return; ++ } ++ ++ png_ptr->idat_size = png_ptr->push_length; ++ } ++ ++ if (png_ptr->idat_size != 0 && png_ptr->save_buffer_size != 0) ++ { ++ size_t save_size = png_ptr->save_buffer_size; ++ png_uint_32 idat_size = png_ptr->idat_size; ++ ++ /* We want the smaller of 'idat_size' and 'current_buffer_size', but they ++ * are of different types and we don't know which variable has the fewest ++ * bits. Carefully select the smaller and cast it to the type of the ++ * larger - this cannot overflow. Do not cast in the following test - it ++ * will break on either 16-bit or 64-bit platforms. ++ */ ++ if (idat_size < save_size) ++ save_size = (size_t)idat_size; ++ ++ else ++ idat_size = (png_uint_32)save_size; ++ ++ png_calculate_crc(png_ptr, png_ptr->save_buffer_ptr, save_size); ++ ++ png_process_IDAT_data(png_ptr, png_ptr->save_buffer_ptr, save_size); ++ ++ png_ptr->idat_size -= idat_size; ++ png_ptr->buffer_size -= save_size; ++ png_ptr->save_buffer_size -= save_size; ++ png_ptr->save_buffer_ptr += save_size; ++ } ++ ++ if (png_ptr->idat_size != 0 && png_ptr->current_buffer_size != 0) ++ { ++ size_t save_size = png_ptr->current_buffer_size; ++ png_uint_32 idat_size = png_ptr->idat_size; ++ ++ /* We want the smaller of 'idat_size' and 'current_buffer_size', but they ++ * are of different types and we don't know which variable has the fewest ++ * bits. Carefully select the smaller and cast it to the type of the ++ * larger - this cannot overflow. ++ */ ++ if (idat_size < save_size) ++ save_size = (size_t)idat_size; ++ ++ else ++ idat_size = (png_uint_32)save_size; ++ ++ png_calculate_crc(png_ptr, png_ptr->current_buffer_ptr, save_size); ++ ++ png_process_IDAT_data(png_ptr, png_ptr->current_buffer_ptr, save_size); ++ ++ png_ptr->idat_size -= idat_size; ++ png_ptr->buffer_size -= save_size; ++ png_ptr->current_buffer_size -= save_size; ++ png_ptr->current_buffer_ptr += save_size; ++ } ++ ++ if (png_ptr->idat_size == 0) ++ { ++ PNG_PUSH_SAVE_BUFFER_IF_LT(4) ++ png_crc_finish(png_ptr, 0); ++ png_ptr->mode &= ~PNG_HAVE_CHUNK_HEADER; ++ png_ptr->mode |= PNG_AFTER_IDAT; ++ png_ptr->zowner = 0; ++ } ++} ++ ++void /* PRIVATE */ ++png_process_IDAT_data(png_structrp png_ptr, png_bytep buffer, ++ size_t buffer_length) ++{ ++ /* The caller checks for a non-zero buffer length. */ ++ if (!(buffer_length > 0) || buffer == NULL) ++ png_error(png_ptr, "No IDAT data (internal error)"); ++ ++ /* This routine must process all the data it has been given ++ * before returning, calling the row callback as required to ++ * handle the uncompressed results. ++ */ ++ png_ptr->zstream.next_in = buffer; ++ /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */ ++ png_ptr->zstream.avail_in = (uInt)buffer_length; ++ ++ /* Keep going until the decompressed data is all processed ++ * or the stream marked as finished. ++ */ ++ while (png_ptr->zstream.avail_in > 0 && ++ (png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0) ++ { ++ int ret; ++ ++ /* We have data for zlib, but we must check that zlib ++ * has someplace to put the results. It doesn't matter ++ * if we don't expect any results -- it may be the input ++ * data is just the LZ end code. ++ */ ++ if (!(png_ptr->zstream.avail_out > 0)) ++ { ++ /* TODO: WARNING: TRUNCATION ERROR: DANGER WILL ROBINSON: */ ++ png_ptr->zstream.avail_out = (uInt)(PNG_ROWBYTES(png_ptr->pixel_depth, ++ png_ptr->iwidth) + 1); ++ ++ png_ptr->zstream.next_out = png_ptr->row_buf; ++ } ++ ++ /* Using Z_SYNC_FLUSH here means that an unterminated ++ * LZ stream (a stream with a missing end code) can still ++ * be handled, otherwise (Z_NO_FLUSH) a future zlib ++ * implementation might defer output and therefore ++ * change the current behavior (see comments in inflate.c ++ * for why this doesn't happen at present with zlib 1.2.5). ++ */ ++ ret = PNG_INFLATE(png_ptr, Z_SYNC_FLUSH); ++ ++ /* Check for any failure before proceeding. */ ++ if (ret != Z_OK && ret != Z_STREAM_END) ++ { ++ /* Terminate the decompression. */ ++ png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED; ++ png_ptr->zowner = 0; ++ ++ /* This may be a truncated stream (missing or ++ * damaged end code). Treat that as a warning. ++ */ ++ if (png_ptr->row_number >= png_ptr->num_rows || ++ png_ptr->pass > 6) ++ png_warning(png_ptr, "Truncated compressed data in IDAT"); ++ ++ else ++ { ++ if (ret == Z_DATA_ERROR) ++ png_benign_error(png_ptr, "IDAT: ADLER32 checksum mismatch"); ++ else ++ png_error(png_ptr, "Decompression error in IDAT"); ++ } ++ ++ /* Skip the check on unprocessed input */ ++ return; ++ } ++ ++ /* Did inflate output any data? */ ++ if (png_ptr->zstream.next_out != png_ptr->row_buf) ++ { ++ /* Is this unexpected data after the last row? ++ * If it is, artificially terminate the LZ output ++ * here. ++ */ ++ if (png_ptr->row_number >= png_ptr->num_rows || ++ png_ptr->pass > 6) ++ { ++ /* Extra data. */ ++ png_warning(png_ptr, "Extra compressed data in IDAT"); ++ png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED; ++ png_ptr->zowner = 0; ++ ++ /* Do no more processing; skip the unprocessed ++ * input check below. ++ */ ++ return; ++ } ++ ++ /* Do we have a complete row? */ ++ if (png_ptr->zstream.avail_out == 0) ++ png_push_process_row(png_ptr); ++ } ++ ++ /* And check for the end of the stream. */ ++ if (ret == Z_STREAM_END) ++ png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED; ++ } ++ ++ /* All the data should have been processed, if anything ++ * is left at this point we have bytes of IDAT data ++ * after the zlib end code. ++ */ ++ if (png_ptr->zstream.avail_in > 0) ++ png_warning(png_ptr, "Extra compression data in IDAT"); ++} ++ ++void /* PRIVATE */ ++png_push_process_row(png_structrp png_ptr) ++{ ++ /* 1.5.6: row_info moved out of png_struct to a local here. */ ++ png_row_info row_info; ++ ++ row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */ ++ row_info.color_type = png_ptr->color_type; ++ row_info.bit_depth = png_ptr->bit_depth; ++ row_info.channels = png_ptr->channels; ++ row_info.pixel_depth = png_ptr->pixel_depth; ++ row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width); ++ ++ if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE) ++ { ++ if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST) ++ png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1, ++ png_ptr->prev_row + 1, png_ptr->row_buf[0]); ++ else ++ png_error(png_ptr, "bad adaptive filter value"); ++ } ++ ++ /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before ++ * 1.5.6, while the buffer really is this big in current versions of libpng ++ * it may not be in the future, so this was changed just to copy the ++ * interlaced row count: ++ */ ++ memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1); ++ ++#ifdef PNG_READ_TRANSFORMS_SUPPORTED ++ if (png_ptr->transformations != 0) ++ png_do_read_transformations(png_ptr, &row_info); ++#endif ++ ++ /* The transformed pixel depth should match the depth now in row_info. */ ++ if (png_ptr->transformed_pixel_depth == 0) ++ { ++ png_ptr->transformed_pixel_depth = row_info.pixel_depth; ++ if (row_info.pixel_depth > png_ptr->maximum_pixel_depth) ++ png_error(png_ptr, "progressive row overflow"); ++ } ++ ++ else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth) ++ png_error(png_ptr, "internal progressive row size calculation error"); ++ ++ ++#ifdef PNG_READ_INTERLACING_SUPPORTED ++ /* Expand interlaced rows to full size */ ++ if (png_ptr->interlaced != 0 && ++ (png_ptr->transformations & PNG_INTERLACE) != 0) ++ { ++ if (png_ptr->pass < 6) ++ png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass, ++ png_ptr->transformations); ++ ++ switch (png_ptr->pass) ++ { ++ case 0: ++ { ++ int i; ++ for (i = 0; i < 8 && png_ptr->pass == 0; i++) ++ { ++ png_push_have_row(png_ptr, png_ptr->row_buf + 1); ++ png_read_push_finish_row(png_ptr); /* Updates png_ptr->pass */ ++ } ++ ++ if (png_ptr->pass == 2) /* Pass 1 might be empty */ ++ { ++ for (i = 0; i < 4 && png_ptr->pass == 2; i++) ++ { ++ png_push_have_row(png_ptr, NULL); ++ png_read_push_finish_row(png_ptr); ++ } ++ } ++ ++ if (png_ptr->pass == 4 && png_ptr->height <= 4) ++ { ++ for (i = 0; i < 2 && png_ptr->pass == 4; i++) ++ { ++ png_push_have_row(png_ptr, NULL); ++ png_read_push_finish_row(png_ptr); ++ } ++ } ++ ++ if (png_ptr->pass == 6 && png_ptr->height <= 4) ++ { ++ png_push_have_row(png_ptr, NULL); ++ png_read_push_finish_row(png_ptr); ++ } ++ ++ break; ++ } ++ ++ case 1: ++ { ++ int i; ++ for (i = 0; i < 8 && png_ptr->pass == 1; i++) ++ { ++ png_push_have_row(png_ptr, png_ptr->row_buf + 1); ++ png_read_push_finish_row(png_ptr); ++ } ++ ++ if (png_ptr->pass == 2) /* Skip top 4 generated rows */ ++ { ++ for (i = 0; i < 4 && png_ptr->pass == 2; i++) ++ { ++ png_push_have_row(png_ptr, NULL); ++ png_read_push_finish_row(png_ptr); ++ } ++ } ++ ++ break; ++ } ++ ++ case 2: ++ { ++ int i; ++ ++ for (i = 0; i < 4 && png_ptr->pass == 2; i++) ++ { ++ png_push_have_row(png_ptr, png_ptr->row_buf + 1); ++ png_read_push_finish_row(png_ptr); ++ } ++ ++ for (i = 0; i < 4 && png_ptr->pass == 2; i++) ++ { ++ png_push_have_row(png_ptr, NULL); ++ png_read_push_finish_row(png_ptr); ++ } ++ ++ if (png_ptr->pass == 4) /* Pass 3 might be empty */ ++ { ++ for (i = 0; i < 2 && png_ptr->pass == 4; i++) ++ { ++ png_push_have_row(png_ptr, NULL); ++ png_read_push_finish_row(png_ptr); ++ } ++ } ++ ++ break; ++ } ++ ++ case 3: ++ { ++ int i; ++ ++ for (i = 0; i < 4 && png_ptr->pass == 3; i++) ++ { ++ png_push_have_row(png_ptr, png_ptr->row_buf + 1); ++ png_read_push_finish_row(png_ptr); ++ } ++ ++ if (png_ptr->pass == 4) /* Skip top two generated rows */ ++ { ++ for (i = 0; i < 2 && png_ptr->pass == 4; i++) ++ { ++ png_push_have_row(png_ptr, NULL); ++ png_read_push_finish_row(png_ptr); ++ } ++ } ++ ++ break; ++ } ++ ++ case 4: ++ { ++ int i; ++ ++ for (i = 0; i < 2 && png_ptr->pass == 4; i++) ++ { ++ png_push_have_row(png_ptr, png_ptr->row_buf + 1); ++ png_read_push_finish_row(png_ptr); ++ } ++ ++ for (i = 0; i < 2 && png_ptr->pass == 4; i++) ++ { ++ png_push_have_row(png_ptr, NULL); ++ png_read_push_finish_row(png_ptr); ++ } ++ ++ if (png_ptr->pass == 6) /* Pass 5 might be empty */ ++ { ++ png_push_have_row(png_ptr, NULL); ++ png_read_push_finish_row(png_ptr); ++ } ++ ++ break; ++ } ++ ++ case 5: ++ { ++ int i; ++ ++ for (i = 0; i < 2 && png_ptr->pass == 5; i++) ++ { ++ png_push_have_row(png_ptr, png_ptr->row_buf + 1); ++ png_read_push_finish_row(png_ptr); ++ } ++ ++ if (png_ptr->pass == 6) /* Skip top generated row */ ++ { ++ png_push_have_row(png_ptr, NULL); ++ png_read_push_finish_row(png_ptr); ++ } ++ ++ break; ++ } ++ ++ default: ++ case 6: ++ { ++ png_push_have_row(png_ptr, png_ptr->row_buf + 1); ++ png_read_push_finish_row(png_ptr); ++ ++ if (png_ptr->pass != 6) ++ break; ++ ++ png_push_have_row(png_ptr, NULL); ++ png_read_push_finish_row(png_ptr); ++ } ++ } ++ } ++ else ++#endif ++ { ++ png_push_have_row(png_ptr, png_ptr->row_buf + 1); ++ png_read_push_finish_row(png_ptr); ++ } ++} ++ ++void /* PRIVATE */ ++png_read_push_finish_row(png_structrp png_ptr) ++{ ++#ifdef PNG_READ_INTERLACING_SUPPORTED ++ /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ ++ ++ /* Start of interlace block */ ++ static const png_byte png_pass_start[] = {0, 4, 0, 2, 0, 1, 0}; ++ ++ /* Offset to next interlace block */ ++ static const png_byte png_pass_inc[] = {8, 8, 4, 4, 2, 2, 1}; ++ ++ /* Start of interlace block in the y direction */ ++ static const png_byte png_pass_ystart[] = {0, 0, 4, 0, 2, 0, 1}; ++ ++ /* Offset to next interlace block in the y direction */ ++ static const png_byte png_pass_yinc[] = {8, 8, 8, 4, 4, 2, 2}; ++ ++ /* Height of interlace block. This is not currently used - if you need ++ * it, uncomment it here and in png.h ++ static const png_byte png_pass_height[] = {8, 8, 4, 4, 2, 2, 1}; ++ */ ++#endif ++ ++ png_ptr->row_number++; ++ if (png_ptr->row_number < png_ptr->num_rows) ++ return; ++ ++#ifdef PNG_READ_INTERLACING_SUPPORTED ++ if (png_ptr->interlaced != 0) ++ { ++ png_ptr->row_number = 0; ++ memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1); ++ ++ do ++ { ++ png_ptr->pass++; ++ if ((png_ptr->pass == 1 && png_ptr->width < 5) || ++ (png_ptr->pass == 3 && png_ptr->width < 3) || ++ (png_ptr->pass == 5 && png_ptr->width < 2)) ++ png_ptr->pass++; ++ ++ if (png_ptr->pass > 7) ++ png_ptr->pass--; ++ ++ if (png_ptr->pass >= 7) ++ break; ++ ++ png_ptr->iwidth = (png_ptr->width + ++ png_pass_inc[png_ptr->pass] - 1 - ++ png_pass_start[png_ptr->pass]) / ++ png_pass_inc[png_ptr->pass]; ++ ++ if ((png_ptr->transformations & PNG_INTERLACE) != 0) ++ break; ++ ++ png_ptr->num_rows = (png_ptr->height + ++ png_pass_yinc[png_ptr->pass] - 1 - ++ png_pass_ystart[png_ptr->pass]) / ++ png_pass_yinc[png_ptr->pass]; ++ ++ } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0); ++ } ++#endif /* READ_INTERLACING */ ++} ++ ++void /* PRIVATE */ ++png_push_have_info(png_structrp png_ptr, png_inforp info_ptr) ++{ ++ if (png_ptr->info_fn != NULL) ++ (*(png_ptr->info_fn))(png_ptr, info_ptr); ++} ++ ++void /* PRIVATE */ ++png_push_have_end(png_structrp png_ptr, png_inforp info_ptr) ++{ ++ if (png_ptr->end_fn != NULL) ++ (*(png_ptr->end_fn))(png_ptr, info_ptr); ++} ++ ++void /* PRIVATE */ ++png_push_have_row(png_structrp png_ptr, png_bytep row) ++{ ++ if (png_ptr->row_fn != NULL) ++ (*(png_ptr->row_fn))(png_ptr, row, png_ptr->row_number, ++ (int)png_ptr->pass); ++} ++ ++#ifdef PNG_READ_INTERLACING_SUPPORTED ++void PNGAPI ++png_progressive_combine_row(png_const_structrp png_ptr, png_bytep old_row, ++ png_const_bytep new_row) ++{ ++ if (png_ptr == NULL) ++ return; ++ ++ /* new_row is a flag here - if it is NULL then the app callback was called ++ * from an empty row (see the calls to png_struct::row_fn below), otherwise ++ * it must be png_ptr->row_buf+1 ++ */ ++ if (new_row != NULL) ++ png_combine_row(png_ptr, old_row, 1/*blocky display*/); ++} ++#endif /* READ_INTERLACING */ ++ ++void PNGAPI ++png_set_progressive_read_fn(png_structrp png_ptr, png_voidp progressive_ptr, ++ png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn, ++ png_progressive_end_ptr end_fn) ++{ ++ if (png_ptr == NULL) ++ return; ++ ++ png_ptr->info_fn = info_fn; ++ png_ptr->row_fn = row_fn; ++ png_ptr->end_fn = end_fn; ++ ++ png_set_read_fn(png_ptr, progressive_ptr, png_push_fill_buffer); ++} ++ ++png_voidp PNGAPI ++png_get_progressive_ptr(png_const_structrp png_ptr) ++{ ++ if (png_ptr == NULL) ++ return (NULL); ++ ++ return png_ptr->io_ptr; ++} ++#endif /* PROGRESSIVE_READ */ +diff --git a/lib/libpng/pngpriv.h b/lib/libpng/pngpriv.h +new file mode 100644 +index 000000000..ed0034607 +--- /dev/null ++++ b/lib/libpng/pngpriv.h +@@ -0,0 +1,2152 @@ ++ ++/* pngpriv.h - private declarations for use inside libpng ++ * ++ * Copyright (c) 2018-2019 Cosmin Truta ++ * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson ++ * Copyright (c) 1996-1997 Andreas Dilger ++ * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc. ++ * ++ * This code is released under the libpng license. ++ * For conditions of distribution and use, see the disclaimer ++ * and license in png.h ++ */ ++ ++/* The symbols declared in this file (including the functions declared ++ * as extern) are PRIVATE. They are not part of the libpng public ++ * interface, and are not recommended for use by regular applications. ++ * Some of them may become public in the future; others may stay private, ++ * change in an incompatible way, or even disappear. ++ * Although the libpng users are not forbidden to include this header, ++ * they should be well aware of the issues that may arise from doing so. ++ */ ++ ++#ifndef PNGPRIV_H ++#define PNGPRIV_H ++ ++/* Feature Test Macros. The following are defined here to ensure that correctly ++ * implemented libraries reveal the APIs libpng needs to build and hide those ++ * that are not needed and potentially damaging to the compilation. ++ * ++ * Feature Test Macros must be defined before any system header is included (see ++ * POSIX 1003.1 2.8.2 "POSIX Symbols." ++ * ++ * These macros only have an effect if the operating system supports either ++ * POSIX 1003.1 or C99, or both. On other operating systems (particularly ++ * Windows/Visual Studio) there is no effect; the OS specific tests below are ++ * still required (as of 2011-05-02.) ++ */ ++#ifndef _POSIX_SOURCE ++# define _POSIX_SOURCE 1 /* Just the POSIX 1003.1 and C89 APIs */ ++#endif ++ ++#ifndef PNG_VERSION_INFO_ONLY ++/* Standard library headers not required by png.h: */ ++# include ++# include ++#endif ++ ++#define PNGLIB_BUILD /*libpng is being built, not used*/ ++ ++/* If HAVE_CONFIG_H is defined during the build then the build system must ++ * provide an appropriate "config.h" file on the include path. The header file ++ * must provide definitions as required below (search for "HAVE_CONFIG_H"); ++ * see configure.ac for more details of the requirements. The macro ++ * "PNG_NO_CONFIG_H" is provided for maintainers to test for dependencies on ++ * 'configure'; define this macro to prevent the configure build including the ++ * configure generated config.h. Libpng is expected to compile without *any* ++ * special build system support on a reasonably ANSI-C compliant system. ++ */ ++#if defined(HAVE_CONFIG_H) && !defined(PNG_NO_CONFIG_H) ++# include ++ ++ /* Pick up the definition of 'restrict' from config.h if it was read: */ ++# define PNG_RESTRICT restrict ++#endif ++ ++/* To support symbol prefixing it is necessary to know *before* including png.h ++ * whether the fixed point (and maybe other) APIs are exported, because if they ++ * are not internal definitions may be required. This is handled below just ++ * before png.h is included, but load the configuration now if it is available. ++ */ ++#ifndef PNGLCONF_H ++# include "pnglibconf.h" ++#endif ++ ++/* Local renames may change non-exported API functions from png.h */ ++#if defined(PNG_PREFIX) && !defined(PNGPREFIX_H) ++# include "pngprefix.h" ++#endif ++ ++#ifdef PNG_USER_CONFIG ++# include "pngusr.h" ++ /* These should have been defined in pngusr.h */ ++# ifndef PNG_USER_PRIVATEBUILD ++# define PNG_USER_PRIVATEBUILD "Custom libpng build" ++# endif ++# ifndef PNG_USER_DLLFNAME_POSTFIX ++# define PNG_USER_DLLFNAME_POSTFIX "Cb" ++# endif ++#endif ++ ++/* Compile time options. ++ * ===================== ++ * In a multi-arch build the compiler may compile the code several times for the ++ * same object module, producing different binaries for different architectures. ++ * When this happens configure-time setting of the target host options cannot be ++ * done and this interferes with the handling of the ARM NEON optimizations, and ++ * possibly other similar optimizations. Put additional tests here; in general ++ * this is needed when the same option can be changed at both compile time and ++ * run time depending on the target OS (i.e. iOS vs Android.) ++ * ++ * NOTE: symbol prefixing does not pass $(CFLAGS) to the preprocessor, because ++ * this is not possible with certain compilers (Oracle SUN OS CC), as a result ++ * it is necessary to ensure that all extern functions that *might* be used ++ * regardless of $(CFLAGS) get declared in this file. The test on __ARM_NEON__ ++ * below is one example of this behavior because it is controlled by the ++ * presence or not of -mfpu=neon on the GCC command line, it is possible to do ++ * this in $(CC), e.g. "CC=gcc -mfpu=neon", but people who build libpng rarely ++ * do this. ++ */ ++#ifndef PNG_ARM_NEON_OPT ++ /* ARM NEON optimizations are being controlled by the compiler settings, ++ * typically the target FPU. If the FPU has been set to NEON (-mfpu=neon ++ * with GCC) then the compiler will define __ARM_NEON__ and we can rely ++ * unconditionally on NEON instructions not crashing, otherwise we must ++ * disable use of NEON instructions. ++ * ++ * NOTE: at present these optimizations depend on 'ALIGNED_MEMORY', so they ++ * can only be turned on automatically if that is supported too. If ++ * PNG_ARM_NEON_OPT is set in CPPFLAGS (to >0) then arm/arm_init.c will fail ++ * to compile with an appropriate #error if ALIGNED_MEMORY has been turned ++ * off. ++ * ++ * Note that gcc-4.9 defines __ARM_NEON instead of the deprecated ++ * __ARM_NEON__, so we check both variants. ++ * ++ * To disable ARM_NEON optimizations entirely, and skip compiling the ++ * associated assembler code, pass --enable-arm-neon=no to configure ++ * or put -DPNG_ARM_NEON_OPT=0 in CPPFLAGS. ++ */ ++# if (defined(__ARM_NEON__) || defined(__ARM_NEON)) && \ ++ defined(PNG_ALIGNED_MEMORY_SUPPORTED) ++# define PNG_ARM_NEON_OPT 2 ++# else ++# define PNG_ARM_NEON_OPT 0 ++# endif ++#endif ++ ++#if PNG_ARM_NEON_OPT > 0 ++ /* NEON optimizations are to be at least considered by libpng, so enable the ++ * callbacks to do this. ++ */ ++# define PNG_FILTER_OPTIMIZATIONS png_init_filter_functions_neon ++ ++ /* By default the 'intrinsics' code in arm/filter_neon_intrinsics.c is used ++ * if possible - if __ARM_NEON__ is set and the compiler version is not known ++ * to be broken. This is controlled by PNG_ARM_NEON_IMPLEMENTATION which can ++ * be: ++ * ++ * 1 The intrinsics code (the default with __ARM_NEON__) ++ * 2 The hand coded assembler (the default without __ARM_NEON__) ++ * ++ * It is possible to set PNG_ARM_NEON_IMPLEMENTATION in CPPFLAGS, however ++ * this is *NOT* supported and may cease to work even after a minor revision ++ * to libpng. It *is* valid to do this for testing purposes, e.g. speed ++ * testing or a new compiler, but the results should be communicated to the ++ * libpng implementation list for incorporation in the next minor release. ++ */ ++# ifndef PNG_ARM_NEON_IMPLEMENTATION ++# if defined(__ARM_NEON__) || defined(__ARM_NEON) ++# if defined(__clang__) ++ /* At present it is unknown by the libpng developers which versions ++ * of clang support the intrinsics, however some or perhaps all ++ * versions do not work with the assembler so this may be ++ * irrelevant, so just use the default (do nothing here.) ++ */ ++# elif defined(__GNUC__) ++ /* GCC 4.5.4 NEON support is known to be broken. 4.6.3 is known to ++ * work, so if this *is* GCC, or G++, look for a version >4.5 ++ */ ++# if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 6) ++# define PNG_ARM_NEON_IMPLEMENTATION 2 ++# endif /* no GNUC support */ ++# endif /* __GNUC__ */ ++# else /* !defined __ARM_NEON__ */ ++ /* The 'intrinsics' code simply won't compile without this -mfpu=neon: ++ */ ++# if !defined(__aarch64__) ++ /* The assembler code currently does not work on ARM64 */ ++# define PNG_ARM_NEON_IMPLEMENTATION 2 ++# endif /* __aarch64__ */ ++# endif /* __ARM_NEON__ */ ++# endif /* !PNG_ARM_NEON_IMPLEMENTATION */ ++ ++# ifndef PNG_ARM_NEON_IMPLEMENTATION ++ /* Use the intrinsics code by default. */ ++# define PNG_ARM_NEON_IMPLEMENTATION 1 ++# endif ++#endif /* PNG_ARM_NEON_OPT > 0 */ ++ ++#ifndef PNG_MIPS_MSA_OPT ++# if defined(__mips_msa) && (__mips_isa_rev >= 5) && defined(PNG_ALIGNED_MEMORY_SUPPORTED) ++# define PNG_MIPS_MSA_OPT 2 ++# else ++# define PNG_MIPS_MSA_OPT 0 ++# endif ++#endif ++ ++#ifndef PNG_POWERPC_VSX_OPT ++# if defined(__PPC64__) && defined(__ALTIVEC__) && defined(__VSX__) ++# define PNG_POWERPC_VSX_OPT 2 ++# else ++# define PNG_POWERPC_VSX_OPT 0 ++# endif ++#endif ++ ++#ifndef PNG_INTEL_SSE_OPT ++# ifdef PNG_INTEL_SSE ++ /* Only check for SSE if the build configuration has been modified to ++ * enable SSE optimizations. This means that these optimizations will ++ * be off by default. See contrib/intel for more details. ++ */ ++# if defined(__SSE4_1__) || defined(__AVX__) || defined(__SSSE3__) || \ ++ defined(__SSE2__) || defined(_M_X64) || defined(_M_AMD64) || \ ++ (defined(_M_IX86_FP) && _M_IX86_FP >= 2) ++# define PNG_INTEL_SSE_OPT 1 ++# else ++# define PNG_INTEL_SSE_OPT 0 ++# endif ++# else ++# define PNG_INTEL_SSE_OPT 0 ++# endif ++#endif ++ ++#if PNG_INTEL_SSE_OPT > 0 ++# ifndef PNG_INTEL_SSE_IMPLEMENTATION ++# if defined(__SSE4_1__) || defined(__AVX__) ++ /* We are not actually using AVX, but checking for AVX is the best ++ way we can detect SSE4.1 and SSSE3 on MSVC. ++ */ ++# define PNG_INTEL_SSE_IMPLEMENTATION 3 ++# elif defined(__SSSE3__) ++# define PNG_INTEL_SSE_IMPLEMENTATION 2 ++# elif defined(__SSE2__) || defined(_M_X64) || defined(_M_AMD64) || \ ++ (defined(_M_IX86_FP) && _M_IX86_FP >= 2) ++# define PNG_INTEL_SSE_IMPLEMENTATION 1 ++# else ++# define PNG_INTEL_SSE_IMPLEMENTATION 0 ++# endif ++# endif ++ ++# if PNG_INTEL_SSE_IMPLEMENTATION > 0 ++# define PNG_FILTER_OPTIMIZATIONS png_init_filter_functions_sse2 ++# endif ++#else ++# define PNG_INTEL_SSE_IMPLEMENTATION 0 ++#endif ++ ++#if PNG_MIPS_MSA_OPT > 0 ++# define PNG_FILTER_OPTIMIZATIONS png_init_filter_functions_msa ++# ifndef PNG_MIPS_MSA_IMPLEMENTATION ++# if defined(__mips_msa) ++# if defined(__clang__) ++# elif defined(__GNUC__) ++# if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 7) ++# define PNG_MIPS_MSA_IMPLEMENTATION 2 ++# endif /* no GNUC support */ ++# endif /* __GNUC__ */ ++# else /* !defined __mips_msa */ ++# define PNG_MIPS_MSA_IMPLEMENTATION 2 ++# endif /* __mips_msa */ ++# endif /* !PNG_MIPS_MSA_IMPLEMENTATION */ ++ ++# ifndef PNG_MIPS_MSA_IMPLEMENTATION ++# define PNG_MIPS_MSA_IMPLEMENTATION 1 ++# endif ++#endif /* PNG_MIPS_MSA_OPT > 0 */ ++ ++#if PNG_POWERPC_VSX_OPT > 0 ++# define PNG_FILTER_OPTIMIZATIONS png_init_filter_functions_vsx ++# define PNG_POWERPC_VSX_IMPLEMENTATION 1 ++#endif ++ ++ ++/* Is this a build of a DLL where compilation of the object modules requires ++ * different preprocessor settings to those required for a simple library? If ++ * so PNG_BUILD_DLL must be set. ++ * ++ * If libpng is used inside a DLL but that DLL does not export the libpng APIs ++ * PNG_BUILD_DLL must not be set. To avoid the code below kicking in build a ++ * static library of libpng then link the DLL against that. ++ */ ++#ifndef PNG_BUILD_DLL ++# ifdef DLL_EXPORT ++ /* This is set by libtool when files are compiled for a DLL; libtool ++ * always compiles twice, even on systems where it isn't necessary. Set ++ * PNG_BUILD_DLL in case it is necessary: ++ */ ++# define PNG_BUILD_DLL ++# else ++# ifdef _WINDLL ++ /* This is set by the Microsoft Visual Studio IDE in projects that ++ * build a DLL. It can't easily be removed from those projects (it ++ * isn't visible in the Visual Studio UI) so it is a fairly reliable ++ * indication that PNG_IMPEXP needs to be set to the DLL export ++ * attributes. ++ */ ++# define PNG_BUILD_DLL ++# else ++# ifdef __DLL__ ++ /* This is set by the Borland C system when compiling for a DLL ++ * (as above.) ++ */ ++# define PNG_BUILD_DLL ++# else ++ /* Add additional compiler cases here. */ ++# endif ++# endif ++# endif ++#endif /* Setting PNG_BUILD_DLL if required */ ++ ++/* See pngconf.h for more details: the builder of the library may set this on ++ * the command line to the right thing for the specific compilation system or it ++ * may be automagically set above (at present we know of no system where it does ++ * need to be set on the command line.) ++ * ++ * PNG_IMPEXP must be set here when building the library to prevent pngconf.h ++ * setting it to the "import" setting for a DLL build. ++ */ ++#ifndef PNG_IMPEXP ++# ifdef PNG_BUILD_DLL ++# define PNG_IMPEXP PNG_DLL_EXPORT ++# else ++ /* Not building a DLL, or the DLL doesn't require specific export ++ * definitions. ++ */ ++# define PNG_IMPEXP ++# endif ++#endif ++ ++/* No warnings for private or deprecated functions in the build: */ ++#ifndef PNG_DEPRECATED ++# define PNG_DEPRECATED ++#endif ++#ifndef PNG_PRIVATE ++# define PNG_PRIVATE ++#endif ++ ++/* Symbol preprocessing support. ++ * ++ * To enable listing global, but internal, symbols the following macros should ++ * always be used to declare an extern data or function object in this file. ++ */ ++#ifndef PNG_INTERNAL_DATA ++# define PNG_INTERNAL_DATA(type, name, array) PNG_LINKAGE_DATA type name array ++#endif ++ ++#ifndef PNG_INTERNAL_FUNCTION ++# define PNG_INTERNAL_FUNCTION(type, name, args, attributes)\ ++ PNG_LINKAGE_FUNCTION PNG_FUNCTION(type, name, args, PNG_EMPTY attributes) ++#endif ++ ++#ifndef PNG_INTERNAL_CALLBACK ++# define PNG_INTERNAL_CALLBACK(type, name, args, attributes)\ ++ PNG_LINKAGE_CALLBACK PNG_FUNCTION(type, (PNGCBAPI name), args,\ ++ PNG_EMPTY attributes) ++#endif ++ ++/* If floating or fixed point APIs are disabled they may still be compiled ++ * internally. To handle this make sure they are declared as the appropriate ++ * internal extern function (otherwise the symbol prefixing stuff won't work and ++ * the functions will be used without definitions.) ++ * ++ * NOTE: although all the API functions are declared here they are not all ++ * actually built! Because the declarations are still made it is necessary to ++ * fake out types that they depend on. ++ */ ++#ifndef PNG_FP_EXPORT ++# ifndef PNG_FLOATING_POINT_SUPPORTED ++# define PNG_FP_EXPORT(ordinal, type, name, args)\ ++ PNG_INTERNAL_FUNCTION(type, name, args, PNG_EMPTY); ++# ifndef PNG_VERSION_INFO_ONLY ++ typedef struct png_incomplete png_double; ++ typedef png_double* png_doublep; ++ typedef const png_double* png_const_doublep; ++ typedef png_double** png_doublepp; ++# endif ++# endif ++#endif ++#ifndef PNG_FIXED_EXPORT ++# ifndef PNG_FIXED_POINT_SUPPORTED ++# define PNG_FIXED_EXPORT(ordinal, type, name, args)\ ++ PNG_INTERNAL_FUNCTION(type, name, args, PNG_EMPTY); ++# endif ++#endif ++ ++#include "png.h" ++ ++/* pngconf.h does not set PNG_DLL_EXPORT unless it is required, so: */ ++#ifndef PNG_DLL_EXPORT ++# define PNG_DLL_EXPORT ++#endif ++ ++/* This is a global switch to set the compilation for an installed system ++ * (a release build). It can be set for testing debug builds to ensure that ++ * they will compile when the build type is switched to RC or STABLE, the ++ * default is just to use PNG_LIBPNG_BUILD_BASE_TYPE. Set this in CPPFLAGS ++ * with either: ++ * ++ * -DPNG_RELEASE_BUILD Turns on the release compile path ++ * -DPNG_RELEASE_BUILD=0 Turns it off ++ * or in your pngusr.h with ++ * #define PNG_RELEASE_BUILD=1 Turns on the release compile path ++ * #define PNG_RELEASE_BUILD=0 Turns it off ++ */ ++#ifndef PNG_RELEASE_BUILD ++# define PNG_RELEASE_BUILD (PNG_LIBPNG_BUILD_BASE_TYPE >= PNG_LIBPNG_BUILD_RC) ++#endif ++ ++/* SECURITY and SAFETY: ++ * ++ * libpng is built with support for internal limits on image dimensions and ++ * memory usage. These are documented in scripts/pnglibconf.dfa of the ++ * source and recorded in the machine generated header file pnglibconf.h. ++ */ ++ ++/* If you are running on a machine where you cannot allocate more ++ * than 64K of memory at once, uncomment this. While libpng will not ++ * normally need that much memory in a chunk (unless you load up a very ++ * large file), zlib needs to know how big of a chunk it can use, and ++ * libpng thus makes sure to check any memory allocation to verify it ++ * will fit into memory. ++ * ++ * zlib provides 'MAXSEG_64K' which, if defined, indicates the ++ * same limit and pngconf.h (already included) sets the limit ++ * if certain operating systems are detected. ++ */ ++#if defined(MAXSEG_64K) && !defined(PNG_MAX_MALLOC_64K) ++# define PNG_MAX_MALLOC_64K ++#endif ++ ++#ifndef PNG_UNUSED ++/* Unused formal parameter warnings are silenced using the following macro ++ * which is expected to have no bad effects on performance (optimizing ++ * compilers will probably remove it entirely). Note that if you replace ++ * it with something other than whitespace, you must include the terminating ++ * semicolon. ++ */ ++# define PNG_UNUSED(param) (void)param; ++#endif ++ ++/* Just a little check that someone hasn't tried to define something ++ * contradictory. ++ */ ++#if (PNG_ZBUF_SIZE > 65536L) && defined(PNG_MAX_MALLOC_64K) ++# undef PNG_ZBUF_SIZE ++# define PNG_ZBUF_SIZE 65536L ++#endif ++ ++/* If warnings or errors are turned off the code is disabled or redirected here. ++ * From 1.5.4 functions have been added to allow very limited formatting of ++ * error and warning messages - this code will also be disabled here. ++ */ ++#ifdef PNG_WARNINGS_SUPPORTED ++# define PNG_WARNING_PARAMETERS(p) png_warning_parameters p; ++#else ++# define png_warning_parameter(p,number,string) ((void)0) ++# define png_warning_parameter_unsigned(p,number,format,value) ((void)0) ++# define png_warning_parameter_signed(p,number,format,value) ((void)0) ++# define png_formatted_warning(pp,p,message) ((void)(pp)) ++# define PNG_WARNING_PARAMETERS(p) ++#endif ++#ifndef PNG_ERROR_TEXT_SUPPORTED ++# define png_fixed_error(s1,s2) png_err(s1) ++#endif ++ ++/* Some fixed point APIs are still required even if not exported because ++ * they get used by the corresponding floating point APIs. This magic ++ * deals with this: ++ */ ++#ifdef PNG_FIXED_POINT_SUPPORTED ++# define PNGFAPI PNGAPI ++#else ++# define PNGFAPI /* PRIVATE */ ++#endif ++ ++#ifndef PNG_VERSION_INFO_ONLY ++/* Other defines specific to compilers can go here. Try to keep ++ * them inside an appropriate ifdef/endif pair for portability. ++ */ ++ ++/* C allows up-casts from (void*) to any pointer and (const void*) to any ++ * pointer to a const object. C++ regards this as a type error and requires an ++ * explicit, static, cast and provides the static_cast<> rune to ensure that ++ * const is not cast away. ++ */ ++#ifdef __cplusplus ++# define png_voidcast(type, value) static_cast(value) ++# define png_constcast(type, value) const_cast(value) ++# define png_aligncast(type, value) \ ++ static_cast(static_cast(value)) ++# define png_aligncastconst(type, value) \ ++ static_cast(static_cast(value)) ++#else ++# define png_voidcast(type, value) (value) ++# ifdef _WIN64 ++# ifdef __GNUC__ ++ typedef unsigned long long png_ptruint; ++# else ++ typedef unsigned __int64 png_ptruint; ++# endif ++# else ++ typedef unsigned long png_ptruint; ++# endif ++# define png_constcast(type, value) ((type)(png_ptruint)(const void*)(value)) ++# define png_aligncast(type, value) ((void*)(value)) ++# define png_aligncastconst(type, value) ((const void*)(value)) ++#endif /* __cplusplus */ ++ ++#if defined(PNG_FLOATING_POINT_SUPPORTED) ||\ ++ defined(PNG_FLOATING_ARITHMETIC_SUPPORTED) ++ /* png.c requires the following ANSI-C constants if the conversion of ++ * floating point to ASCII is implemented therein: ++ * ++ * DBL_DIG Maximum number of decimal digits (can be set to any constant) ++ * DBL_MIN Smallest normalized fp number (can be set to an arbitrary value) ++ * DBL_MAX Maximum floating point number (can be set to an arbitrary value) ++ */ ++# include ++ ++# if (defined(__MWERKS__) && defined(macintosh)) || defined(applec) || \ ++ defined(THINK_C) || defined(__SC__) || defined(TARGET_OS_MAC) ++ /* We need to check that hasn't already been included earlier ++ * as it seems it doesn't agree with , yet we should really use ++ * if possible. ++ */ ++# if !defined(__MATH_H__) && !defined(__MATH_H) && !defined(__cmath__) ++# include ++# endif ++# else ++# include ++# endif ++# if defined(_AMIGA) && defined(__SASC) && defined(_M68881) ++ /* Amiga SAS/C: We must include builtin FPU functions when compiling using ++ * MATH=68881 ++ */ ++# include ++# endif ++#endif ++ ++/* This provides the non-ANSI (far) memory allocation routines. */ ++#if defined(__TURBOC__) && defined(__MSDOS__) ++# include ++# include ++#endif ++ ++#if defined(WIN32) || defined(_Windows) || defined(_WINDOWS) || \ ++ defined(_WIN32) || defined(__WIN32__) ++# include /* defines _WINDOWS_ macro */ ++#endif ++#endif /* PNG_VERSION_INFO_ONLY */ ++ ++/* Moved here around 1.5.0beta36 from pngconf.h */ ++/* Users may want to use these so they are not private. Any library ++ * functions that are passed far data must be model-independent. ++ */ ++ ++/* Memory model/platform independent fns */ ++#ifndef PNG_ABORT ++# ifdef _WINDOWS_ ++# define PNG_ABORT() ExitProcess(0) ++# else ++# define PNG_ABORT() abort() ++# endif ++#endif ++ ++/* These macros may need to be architecture dependent. */ ++#define PNG_ALIGN_NONE 0 /* do not use data alignment */ ++#define PNG_ALIGN_ALWAYS 1 /* assume unaligned accesses are OK */ ++#ifdef offsetof ++# define PNG_ALIGN_OFFSET 2 /* use offsetof to determine alignment */ ++#else ++# define PNG_ALIGN_OFFSET -1 /* prevent the use of this */ ++#endif ++#define PNG_ALIGN_SIZE 3 /* use sizeof to determine alignment */ ++ ++#ifndef PNG_ALIGN_TYPE ++ /* Default to using aligned access optimizations and requiring alignment to a ++ * multiple of the data type size. Override in a compiler specific fashion ++ * if necessary by inserting tests here: ++ */ ++# define PNG_ALIGN_TYPE PNG_ALIGN_SIZE ++#endif ++ ++#if PNG_ALIGN_TYPE == PNG_ALIGN_SIZE ++ /* This is used because in some compiler implementations non-aligned ++ * structure members are supported, so the offsetof approach below fails. ++ * Set PNG_ALIGN_SIZE=0 for compiler combinations where unaligned access ++ * is good for performance. Do not do this unless you have tested the result ++ * and understand it. ++ */ ++# define png_alignof(type) (sizeof (type)) ++#else ++# if PNG_ALIGN_TYPE == PNG_ALIGN_OFFSET ++# define png_alignof(type) offsetof(struct{char c; type t;}, t) ++# else ++# if PNG_ALIGN_TYPE == PNG_ALIGN_ALWAYS ++# define png_alignof(type) (1) ++# endif ++ /* Else leave png_alignof undefined to prevent use thereof */ ++# endif ++#endif ++ ++/* This implicitly assumes alignment is always to a power of 2. */ ++#ifdef png_alignof ++# define png_isaligned(ptr, type)\ ++ (((type)((const char*)ptr-(const char*)0) & \ ++ (type)(png_alignof(type)-1)) == 0) ++#else ++# define png_isaligned(ptr, type) 0 ++#endif ++ ++/* End of memory model/platform independent support */ ++/* End of 1.5.0beta36 move from pngconf.h */ ++ ++/* CONSTANTS and UTILITY MACROS ++ * These are used internally by libpng and not exposed in the API ++ */ ++ ++/* Various modes of operation. Note that after an init, mode is set to ++ * zero automatically when the structure is created. Three of these ++ * are defined in png.h because they need to be visible to applications ++ * that call png_set_unknown_chunk(). ++ */ ++/* #define PNG_HAVE_IHDR 0x01U (defined in png.h) */ ++/* #define PNG_HAVE_PLTE 0x02U (defined in png.h) */ ++#define PNG_HAVE_IDAT 0x04U ++/* #define PNG_AFTER_IDAT 0x08U (defined in png.h) */ ++#define PNG_HAVE_IEND 0x10U ++ /* 0x20U (unused) */ ++ /* 0x40U (unused) */ ++ /* 0x80U (unused) */ ++#define PNG_HAVE_CHUNK_HEADER 0x100U ++#define PNG_WROTE_tIME 0x200U ++#define PNG_WROTE_INFO_BEFORE_PLTE 0x400U ++#define PNG_BACKGROUND_IS_GRAY 0x800U ++#define PNG_HAVE_PNG_SIGNATURE 0x1000U ++#define PNG_HAVE_CHUNK_AFTER_IDAT 0x2000U /* Have another chunk after IDAT */ ++ /* 0x4000U (unused) */ ++#define PNG_IS_READ_STRUCT 0x8000U /* Else is a write struct */ ++ ++/* Flags for the transformations the PNG library does on the image data */ ++#define PNG_BGR 0x0001U ++#define PNG_INTERLACE 0x0002U ++#define PNG_PACK 0x0004U ++#define PNG_SHIFT 0x0008U ++#define PNG_SWAP_BYTES 0x0010U ++#define PNG_INVERT_MONO 0x0020U ++#define PNG_QUANTIZE 0x0040U ++#define PNG_COMPOSE 0x0080U /* Was PNG_BACKGROUND */ ++#define PNG_BACKGROUND_EXPAND 0x0100U ++#define PNG_EXPAND_16 0x0200U /* Added to libpng 1.5.2 */ ++#define PNG_16_TO_8 0x0400U /* Becomes 'chop' in 1.5.4 */ ++#define PNG_RGBA 0x0800U ++#define PNG_EXPAND 0x1000U ++#define PNG_GAMMA 0x2000U ++#define PNG_GRAY_TO_RGB 0x4000U ++#define PNG_FILLER 0x8000U ++#define PNG_PACKSWAP 0x10000U ++#define PNG_SWAP_ALPHA 0x20000U ++#define PNG_STRIP_ALPHA 0x40000U ++#define PNG_INVERT_ALPHA 0x80000U ++#define PNG_USER_TRANSFORM 0x100000U ++#define PNG_RGB_TO_GRAY_ERR 0x200000U ++#define PNG_RGB_TO_GRAY_WARN 0x400000U ++#define PNG_RGB_TO_GRAY 0x600000U /* two bits, RGB_TO_GRAY_ERR|WARN */ ++#define PNG_ENCODE_ALPHA 0x800000U /* Added to libpng-1.5.4 */ ++#define PNG_ADD_ALPHA 0x1000000U /* Added to libpng-1.2.7 */ ++#define PNG_EXPAND_tRNS 0x2000000U /* Added to libpng-1.2.9 */ ++#define PNG_SCALE_16_TO_8 0x4000000U /* Added to libpng-1.5.4 */ ++ /* 0x8000000U unused */ ++ /* 0x10000000U unused */ ++ /* 0x20000000U unused */ ++ /* 0x40000000U unused */ ++/* Flags for png_create_struct */ ++#define PNG_STRUCT_PNG 0x0001U ++#define PNG_STRUCT_INFO 0x0002U ++ ++/* Flags for the png_ptr->flags rather than declaring a byte for each one */ ++#define PNG_FLAG_ZLIB_CUSTOM_STRATEGY 0x0001U ++#define PNG_FLAG_ZSTREAM_INITIALIZED 0x0002U /* Added to libpng-1.6.0 */ ++ /* 0x0004U unused */ ++#define PNG_FLAG_ZSTREAM_ENDED 0x0008U /* Added to libpng-1.6.0 */ ++ /* 0x0010U unused */ ++ /* 0x0020U unused */ ++#define PNG_FLAG_ROW_INIT 0x0040U ++#define PNG_FLAG_FILLER_AFTER 0x0080U ++#define PNG_FLAG_CRC_ANCILLARY_USE 0x0100U ++#define PNG_FLAG_CRC_ANCILLARY_NOWARN 0x0200U ++#define PNG_FLAG_CRC_CRITICAL_USE 0x0400U ++#define PNG_FLAG_CRC_CRITICAL_IGNORE 0x0800U ++#define PNG_FLAG_ASSUME_sRGB 0x1000U /* Added to libpng-1.5.4 */ ++#define PNG_FLAG_OPTIMIZE_ALPHA 0x2000U /* Added to libpng-1.5.4 */ ++#define PNG_FLAG_DETECT_UNINITIALIZED 0x4000U /* Added to libpng-1.5.4 */ ++/* #define PNG_FLAG_KEEP_UNKNOWN_CHUNKS 0x8000U */ ++/* #define PNG_FLAG_KEEP_UNSAFE_CHUNKS 0x10000U */ ++#define PNG_FLAG_LIBRARY_MISMATCH 0x20000U ++#define PNG_FLAG_STRIP_ERROR_NUMBERS 0x40000U ++#define PNG_FLAG_STRIP_ERROR_TEXT 0x80000U ++#define PNG_FLAG_BENIGN_ERRORS_WARN 0x100000U /* Added to libpng-1.4.0 */ ++#define PNG_FLAG_APP_WARNINGS_WARN 0x200000U /* Added to libpng-1.6.0 */ ++#define PNG_FLAG_APP_ERRORS_WARN 0x400000U /* Added to libpng-1.6.0 */ ++ /* 0x800000U unused */ ++ /* 0x1000000U unused */ ++ /* 0x2000000U unused */ ++ /* 0x4000000U unused */ ++ /* 0x8000000U unused */ ++ /* 0x10000000U unused */ ++ /* 0x20000000U unused */ ++ /* 0x40000000U unused */ ++ ++#define PNG_FLAG_CRC_ANCILLARY_MASK (PNG_FLAG_CRC_ANCILLARY_USE | \ ++ PNG_FLAG_CRC_ANCILLARY_NOWARN) ++ ++#define PNG_FLAG_CRC_CRITICAL_MASK (PNG_FLAG_CRC_CRITICAL_USE | \ ++ PNG_FLAG_CRC_CRITICAL_IGNORE) ++ ++#define PNG_FLAG_CRC_MASK (PNG_FLAG_CRC_ANCILLARY_MASK | \ ++ PNG_FLAG_CRC_CRITICAL_MASK) ++ ++/* Save typing and make code easier to understand */ ++ ++#define PNG_COLOR_DIST(c1, c2) (abs((int)((c1).red) - (int)((c2).red)) + \ ++ abs((int)((c1).green) - (int)((c2).green)) + \ ++ abs((int)((c1).blue) - (int)((c2).blue))) ++ ++/* Added to libpng-1.6.0: scale a 16-bit value in the range 0..65535 to 0..255 ++ * by dividing by 257 *with rounding*. This macro is exact for the given range. ++ * See the discourse in pngrtran.c png_do_scale_16_to_8. The values in the ++ * macro were established by experiment (modifying the added value). The macro ++ * has a second variant that takes a value already scaled by 255 and divides by ++ * 65535 - this has a maximum error of .502. Over the range 0..65535*65535 it ++ * only gives off-by-one errors and only for 0.5% (1 in 200) of the values. ++ */ ++#define PNG_DIV65535(v24) (((v24) + 32895) >> 16) ++#define PNG_DIV257(v16) PNG_DIV65535((png_uint_32)(v16) * 255) ++ ++/* Added to libpng-1.2.6 JB */ ++#define PNG_ROWBYTES(pixel_bits, width) \ ++ ((pixel_bits) >= 8 ? \ ++ ((size_t)(width) * (((size_t)(pixel_bits)) >> 3)) : \ ++ (( ((size_t)(width) * ((size_t)(pixel_bits))) + 7) >> 3) ) ++ ++/* This returns the number of trailing bits in the last byte of a row, 0 if the ++ * last byte is completely full of pixels. It is, in principle, (pixel_bits x ++ * width) % 8, but that would overflow for large 'width'. The second macro is ++ * the same except that it returns the number of unused bits in the last byte; ++ * (8-TRAILBITS), but 0 when TRAILBITS is 0. ++ * ++ * NOTE: these macros are intended to be self-evidently correct and never ++ * overflow on the assumption that pixel_bits is in the range 0..255. The ++ * arguments are evaluated only once and they can be signed (e.g. as a result of ++ * the integral promotions). The result of the expression always has type ++ * (png_uint_32), however the compiler always knows it is in the range 0..7. ++ */ ++#define PNG_TRAILBITS(pixel_bits, width) \ ++ (((pixel_bits) * ((width) % (png_uint_32)8)) % 8) ++ ++#define PNG_PADBITS(pixel_bits, width) \ ++ ((8 - PNG_TRAILBITS(pixel_bits, width)) % 8) ++ ++/* PNG_OUT_OF_RANGE returns true if value is outside the range ++ * ideal-delta..ideal+delta. Each argument is evaluated twice. ++ * "ideal" and "delta" should be constants, normally simple ++ * integers, "value" a variable. Added to libpng-1.2.6 JB ++ */ ++#define PNG_OUT_OF_RANGE(value, ideal, delta) \ ++ ( (value) < (ideal)-(delta) || (value) > (ideal)+(delta) ) ++ ++/* Conversions between fixed and floating point, only defined if ++ * required (to make sure the code doesn't accidentally use float ++ * when it is supposedly disabled.) ++ */ ++#ifdef PNG_FLOATING_POINT_SUPPORTED ++/* The floating point conversion can't overflow, though it can and ++ * does lose accuracy relative to the original fixed point value. ++ * In practice this doesn't matter because png_fixed_point only ++ * stores numbers with very low precision. The png_ptr and s ++ * arguments are unused by default but are there in case error ++ * checking becomes a requirement. ++ */ ++#define png_float(png_ptr, fixed, s) (.00001 * (fixed)) ++ ++/* The fixed point conversion performs range checking and evaluates ++ * its argument multiple times, so must be used with care. The ++ * range checking uses the PNG specification values for a signed ++ * 32-bit fixed point value except that the values are deliberately ++ * rounded-to-zero to an integral value - 21474 (21474.83 is roughly ++ * (2^31-1) * 100000). 's' is a string that describes the value being ++ * converted. ++ * ++ * NOTE: this macro will raise a png_error if the range check fails, ++ * therefore it is normally only appropriate to use this on values ++ * that come from API calls or other sources where an out of range ++ * error indicates a programming error, not a data error! ++ * ++ * NOTE: by default this is off - the macro is not used - because the ++ * function call saves a lot of code. ++ */ ++#ifdef PNG_FIXED_POINT_MACRO_SUPPORTED ++#define png_fixed(png_ptr, fp, s) ((fp) <= 21474 && (fp) >= -21474 ?\ ++ ((png_fixed_point)(100000 * (fp))) : (png_fixed_error(png_ptr, s),0)) ++#endif ++/* else the corresponding function is defined below, inside the scope of the ++ * cplusplus test. ++ */ ++#endif ++ ++/* Constants for known chunk types. If you need to add a chunk, define the name ++ * here. For historical reasons these constants have the form png_; i.e. ++ * the prefix is lower case. Please use decimal values as the parameters to ++ * match the ISO PNG specification and to avoid relying on the C locale ++ * interpretation of character values. ++ * ++ * Prior to 1.5.6 these constants were strings, as of 1.5.6 png_uint_32 values ++ * are computed and a new macro (PNG_STRING_FROM_CHUNK) added to allow a string ++ * to be generated if required. ++ * ++ * PNG_32b correctly produces a value shifted by up to 24 bits, even on ++ * architectures where (int) is only 16 bits. ++ */ ++#define PNG_32b(b,s) ((png_uint_32)(b) << (s)) ++#define PNG_U32(b1,b2,b3,b4) \ ++ (PNG_32b(b1,24) | PNG_32b(b2,16) | PNG_32b(b3,8) | PNG_32b(b4,0)) ++ ++/* Constants for known chunk types. ++ * ++ * MAINTAINERS: If you need to add a chunk, define the name here. ++ * For historical reasons these constants have the form png_; i.e. ++ * the prefix is lower case. Please use decimal values as the parameters to ++ * match the ISO PNG specification and to avoid relying on the C locale ++ * interpretation of character values. Please keep the list sorted. ++ * ++ * Notice that PNG_U32 is used to define a 32-bit value for the 4 byte chunk ++ * type. In fact the specification does not express chunk types this way, ++ * however using a 32-bit value means that the chunk type can be read from the ++ * stream using exactly the same code as used for a 32-bit unsigned value and ++ * can be examined far more efficiently (using one arithmetic compare). ++ * ++ * Prior to 1.5.6 the chunk type constants were expressed as C strings. The ++ * libpng API still uses strings for 'unknown' chunks and a macro, ++ * PNG_STRING_FROM_CHUNK, allows a string to be generated if required. Notice ++ * that for portable code numeric values must still be used; the string "IHDR" ++ * is not portable and neither is PNG_U32('I', 'H', 'D', 'R'). ++ * ++ * In 1.7.0 the definitions will be made public in png.h to avoid having to ++ * duplicate the same definitions in application code. ++ */ ++#define png_IDAT PNG_U32( 73, 68, 65, 84) ++#define png_IEND PNG_U32( 73, 69, 78, 68) ++#define png_IHDR PNG_U32( 73, 72, 68, 82) ++#define png_PLTE PNG_U32( 80, 76, 84, 69) ++#define png_bKGD PNG_U32( 98, 75, 71, 68) ++#define png_cHRM PNG_U32( 99, 72, 82, 77) ++#define png_eXIf PNG_U32(101, 88, 73, 102) /* registered July 2017 */ ++#define png_fRAc PNG_U32(102, 82, 65, 99) /* registered, not defined */ ++#define png_gAMA PNG_U32(103, 65, 77, 65) ++#define png_gIFg PNG_U32(103, 73, 70, 103) ++#define png_gIFt PNG_U32(103, 73, 70, 116) /* deprecated */ ++#define png_gIFx PNG_U32(103, 73, 70, 120) ++#define png_hIST PNG_U32(104, 73, 83, 84) ++#define png_iCCP PNG_U32(105, 67, 67, 80) ++#define png_iTXt PNG_U32(105, 84, 88, 116) ++#define png_oFFs PNG_U32(111, 70, 70, 115) ++#define png_pCAL PNG_U32(112, 67, 65, 76) ++#define png_pHYs PNG_U32(112, 72, 89, 115) ++#define png_sBIT PNG_U32(115, 66, 73, 84) ++#define png_sCAL PNG_U32(115, 67, 65, 76) ++#define png_sPLT PNG_U32(115, 80, 76, 84) ++#define png_sRGB PNG_U32(115, 82, 71, 66) ++#define png_sTER PNG_U32(115, 84, 69, 82) ++#define png_tEXt PNG_U32(116, 69, 88, 116) ++#define png_tIME PNG_U32(116, 73, 77, 69) ++#define png_tRNS PNG_U32(116, 82, 78, 83) ++#define png_zTXt PNG_U32(122, 84, 88, 116) ++ ++/* The following will work on (signed char*) strings, whereas the get_uint_32 ++ * macro will fail on top-bit-set values because of the sign extension. ++ */ ++#define PNG_CHUNK_FROM_STRING(s)\ ++ PNG_U32(0xff & (s)[0], 0xff & (s)[1], 0xff & (s)[2], 0xff & (s)[3]) ++ ++/* This uses (char), not (png_byte) to avoid warnings on systems where (char) is ++ * signed and the argument is a (char[]) This macro will fail miserably on ++ * systems where (char) is more than 8 bits. ++ */ ++#define PNG_STRING_FROM_CHUNK(s,c)\ ++ (void)(((char*)(s))[0]=(char)(((c)>>24) & 0xff), \ ++ ((char*)(s))[1]=(char)(((c)>>16) & 0xff),\ ++ ((char*)(s))[2]=(char)(((c)>>8) & 0xff), \ ++ ((char*)(s))[3]=(char)((c & 0xff))) ++ ++/* Do the same but terminate with a null character. */ ++#define PNG_CSTRING_FROM_CHUNK(s,c)\ ++ (void)(PNG_STRING_FROM_CHUNK(s,c), ((char*)(s))[4] = 0) ++ ++/* Test on flag values as defined in the spec (section 5.4): */ ++#define PNG_CHUNK_ANCILLARY(c) (1 & ((c) >> 29)) ++#define PNG_CHUNK_CRITICAL(c) (!PNG_CHUNK_ANCILLARY(c)) ++#define PNG_CHUNK_PRIVATE(c) (1 & ((c) >> 21)) ++#define PNG_CHUNK_RESERVED(c) (1 & ((c) >> 13)) ++#define PNG_CHUNK_SAFE_TO_COPY(c) (1 & ((c) >> 5)) ++ ++/* Gamma values (new at libpng-1.5.4): */ ++#define PNG_GAMMA_MAC_OLD 151724 /* Assume '1.8' is really 2.2/1.45! */ ++#define PNG_GAMMA_MAC_INVERSE 65909 ++#define PNG_GAMMA_sRGB_INVERSE 45455 ++ ++/* Almost everything below is C specific; the #defines above can be used in ++ * non-C code (so long as it is C-preprocessed) the rest of this stuff cannot. ++ */ ++#ifndef PNG_VERSION_INFO_ONLY ++ ++#include "pngstruct.h" ++#include "pnginfo.h" ++ ++/* Validate the include paths - the include path used to generate pnglibconf.h ++ * must match that used in the build, or we must be using pnglibconf.h.prebuilt: ++ */ ++#if PNG_ZLIB_VERNUM != 0 && PNG_ZLIB_VERNUM != ZLIB_VERNUM ++# error ZLIB_VERNUM != PNG_ZLIB_VERNUM \ ++ "-I (include path) error: see the notes in pngpriv.h" ++ /* This means that when pnglibconf.h was built the copy of zlib.h that it ++ * used is not the same as the one being used here. Because the build of ++ * libpng makes decisions to use inflateInit2 and inflateReset2 based on the ++ * zlib version number and because this affects handling of certain broken ++ * PNG files the -I directives must match. ++ * ++ * The most likely explanation is that you passed a -I in CFLAGS. This will ++ * not work; all the preprocessor directives and in particular all the -I ++ * directives must be in CPPFLAGS. ++ */ ++#endif ++ ++/* This is used for 16-bit gamma tables -- only the top level pointers are ++ * const; this could be changed: ++ */ ++typedef const png_uint_16p * png_const_uint_16pp; ++ ++/* Added to libpng-1.5.7: sRGB conversion tables */ ++#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\ ++ defined(PNG_SIMPLIFIED_WRITE_SUPPORTED) ++#ifdef PNG_SIMPLIFIED_READ_SUPPORTED ++PNG_INTERNAL_DATA(const png_uint_16, png_sRGB_table, [256]); ++ /* Convert from an sRGB encoded value 0..255 to a 16-bit linear value, ++ * 0..65535. This table gives the closest 16-bit answers (no errors). ++ */ ++#endif ++ ++PNG_INTERNAL_DATA(const png_uint_16, png_sRGB_base, [512]); ++PNG_INTERNAL_DATA(const png_byte, png_sRGB_delta, [512]); ++ ++#define PNG_sRGB_FROM_LINEAR(linear) \ ++ ((png_byte)(0xff & ((png_sRGB_base[(linear)>>15] \ ++ + ((((linear) & 0x7fff)*png_sRGB_delta[(linear)>>15])>>12)) >> 8))) ++ /* Given a value 'linear' in the range 0..255*65535 calculate the 8-bit sRGB ++ * encoded value with maximum error 0.646365. Note that the input is not a ++ * 16-bit value; it has been multiplied by 255! */ ++#endif /* SIMPLIFIED_READ/WRITE */ ++ ++ ++/* Inhibit C++ name-mangling for libpng functions but not for system calls. */ ++#ifdef __cplusplus ++extern "C" { ++#endif /* __cplusplus */ ++ ++/* Internal functions; these are not exported from a DLL however because they ++ * are used within several of the C source files they have to be C extern. ++ * ++ * All of these functions must be declared with PNG_INTERNAL_FUNCTION. ++ */ ++ ++/* Zlib support */ ++#define PNG_UNEXPECTED_ZLIB_RETURN (-7) ++PNG_INTERNAL_FUNCTION(void, png_zstream_error,(png_structrp png_ptr, int ret), ++ PNG_EMPTY); ++ /* Used by the zlib handling functions to ensure that z_stream::msg is always ++ * set before they return. ++ */ ++ ++#ifdef PNG_WRITE_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_free_buffer_list,(png_structrp png_ptr, ++ png_compression_bufferp *list),PNG_EMPTY); ++ /* Free the buffer list used by the compressed write code. */ ++#endif ++ ++#if defined(PNG_FLOATING_POINT_SUPPORTED) && \ ++ !defined(PNG_FIXED_POINT_MACRO_SUPPORTED) && \ ++ (defined(PNG_gAMA_SUPPORTED) || defined(PNG_cHRM_SUPPORTED) || \ ++ defined(PNG_sCAL_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) || \ ++ defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)) || \ ++ (defined(PNG_sCAL_SUPPORTED) && \ ++ defined(PNG_FLOATING_ARITHMETIC_SUPPORTED)) ++PNG_INTERNAL_FUNCTION(png_fixed_point,png_fixed,(png_const_structrp png_ptr, ++ double fp, png_const_charp text),PNG_EMPTY); ++#endif ++ ++/* Check the user version string for compatibility, returns false if the version ++ * numbers aren't compatible. ++ */ ++PNG_INTERNAL_FUNCTION(int,png_user_version_check,(png_structrp png_ptr, ++ png_const_charp user_png_ver),PNG_EMPTY); ++ ++/* Internal base allocator - no messages, NULL on failure to allocate. This ++ * does, however, call the application provided allocator and that could call ++ * png_error (although that would be a bug in the application implementation.) ++ */ ++PNG_INTERNAL_FUNCTION(png_voidp,png_malloc_base,(png_const_structrp png_ptr, ++ png_alloc_size_t size),PNG_ALLOCATED); ++ ++#if defined(PNG_TEXT_SUPPORTED) || defined(PNG_sPLT_SUPPORTED) ||\ ++ defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED) ++/* Internal array allocator, outputs no error or warning messages on failure, ++ * just returns NULL. ++ */ ++PNG_INTERNAL_FUNCTION(png_voidp,png_malloc_array,(png_const_structrp png_ptr, ++ int nelements, size_t element_size),PNG_ALLOCATED); ++ ++/* The same but an existing array is extended by add_elements. This function ++ * also memsets the new elements to 0 and copies the old elements. The old ++ * array is not freed or altered. ++ */ ++PNG_INTERNAL_FUNCTION(png_voidp,png_realloc_array,(png_const_structrp png_ptr, ++ png_const_voidp array, int old_elements, int add_elements, ++ size_t element_size),PNG_ALLOCATED); ++#endif /* text, sPLT or unknown chunks */ ++ ++/* Magic to create a struct when there is no struct to call the user supplied ++ * memory allocators. Because error handling has not been set up the memory ++ * handlers can't safely call png_error, but this is an obscure and undocumented ++ * restriction so libpng has to assume that the 'free' handler, at least, might ++ * call png_error. ++ */ ++PNG_INTERNAL_FUNCTION(png_structp,png_create_png_struct, ++ (png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, ++ png_error_ptr warn_fn, png_voidp mem_ptr, png_malloc_ptr malloc_fn, ++ png_free_ptr free_fn),PNG_ALLOCATED); ++ ++/* Free memory from internal libpng struct */ ++PNG_INTERNAL_FUNCTION(void,png_destroy_png_struct,(png_structrp png_ptr), ++ PNG_EMPTY); ++ ++/* Free an allocated jmp_buf (always succeeds) */ ++PNG_INTERNAL_FUNCTION(void,png_free_jmpbuf,(png_structrp png_ptr),PNG_EMPTY); ++ ++/* Function to allocate memory for zlib. PNGAPI is disallowed. */ ++PNG_INTERNAL_FUNCTION(voidpf,png_zalloc,(voidpf png_ptr, uInt items, uInt size), ++ PNG_ALLOCATED); ++ ++/* Function to free memory for zlib. PNGAPI is disallowed. */ ++PNG_INTERNAL_FUNCTION(void,png_zfree,(voidpf png_ptr, voidpf ptr),PNG_EMPTY); ++ ++/* Next four functions are used internally as callbacks. PNGCBAPI is required ++ * but not PNG_EXPORT. PNGAPI added at libpng version 1.2.3, changed to ++ * PNGCBAPI at 1.5.0 ++ */ ++ ++PNG_INTERNAL_FUNCTION(void PNGCBAPI,png_default_read_data,(png_structp png_ptr, ++ png_bytep data, size_t length),PNG_EMPTY); ++ ++#ifdef PNG_PROGRESSIVE_READ_SUPPORTED ++PNG_INTERNAL_FUNCTION(void PNGCBAPI,png_push_fill_buffer,(png_structp png_ptr, ++ png_bytep buffer, size_t length),PNG_EMPTY); ++#endif ++ ++PNG_INTERNAL_FUNCTION(void PNGCBAPI,png_default_write_data,(png_structp png_ptr, ++ png_bytep data, size_t length),PNG_EMPTY); ++ ++#ifdef PNG_WRITE_FLUSH_SUPPORTED ++# ifdef PNG_STDIO_SUPPORTED ++PNG_INTERNAL_FUNCTION(void PNGCBAPI,png_default_flush,(png_structp png_ptr), ++ PNG_EMPTY); ++# endif ++#endif ++ ++/* Reset the CRC variable */ ++PNG_INTERNAL_FUNCTION(void,png_reset_crc,(png_structrp png_ptr),PNG_EMPTY); ++ ++/* Write the "data" buffer to whatever output you are using */ ++PNG_INTERNAL_FUNCTION(void,png_write_data,(png_structrp png_ptr, ++ png_const_bytep data, size_t length),PNG_EMPTY); ++ ++/* Read and check the PNG file signature */ ++PNG_INTERNAL_FUNCTION(void,png_read_sig,(png_structrp png_ptr, ++ png_inforp info_ptr),PNG_EMPTY); ++ ++/* Read the chunk header (length + type name) */ ++PNG_INTERNAL_FUNCTION(png_uint_32,png_read_chunk_header,(png_structrp png_ptr), ++ PNG_EMPTY); ++ ++/* Read data from whatever input you are using into the "data" buffer */ ++PNG_INTERNAL_FUNCTION(void,png_read_data,(png_structrp png_ptr, png_bytep data, ++ size_t length),PNG_EMPTY); ++ ++/* Read bytes into buf, and update png_ptr->crc */ ++PNG_INTERNAL_FUNCTION(void,png_crc_read,(png_structrp png_ptr, png_bytep buf, ++ png_uint_32 length),PNG_EMPTY); ++ ++/* Read "skip" bytes, read the file crc, and (optionally) verify png_ptr->crc */ ++PNG_INTERNAL_FUNCTION(int,png_crc_finish,(png_structrp png_ptr, ++ png_uint_32 skip),PNG_EMPTY); ++ ++/* Read the CRC from the file and compare it to the libpng calculated CRC */ ++PNG_INTERNAL_FUNCTION(int,png_crc_error,(png_structrp png_ptr),PNG_EMPTY); ++ ++/* Calculate the CRC over a section of data. Note that we are only ++ * passing a maximum of 64K on systems that have this as a memory limit, ++ * since this is the maximum buffer size we can specify. ++ */ ++PNG_INTERNAL_FUNCTION(void,png_calculate_crc,(png_structrp png_ptr, ++ png_const_bytep ptr, size_t length),PNG_EMPTY); ++ ++#ifdef PNG_WRITE_FLUSH_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_flush,(png_structrp png_ptr),PNG_EMPTY); ++#endif ++ ++/* Write various chunks */ ++ ++/* Write the IHDR chunk, and update the png_struct with the necessary ++ * information. ++ */ ++PNG_INTERNAL_FUNCTION(void,png_write_IHDR,(png_structrp png_ptr, ++ png_uint_32 width, png_uint_32 height, int bit_depth, int color_type, ++ int compression_method, int filter_method, int interlace_method),PNG_EMPTY); ++ ++PNG_INTERNAL_FUNCTION(void,png_write_PLTE,(png_structrp png_ptr, ++ png_const_colorp palette, png_uint_32 num_pal),PNG_EMPTY); ++ ++PNG_INTERNAL_FUNCTION(void,png_compress_IDAT,(png_structrp png_ptr, ++ png_const_bytep row_data, png_alloc_size_t row_data_length, int flush), ++ PNG_EMPTY); ++ ++PNG_INTERNAL_FUNCTION(void,png_write_IEND,(png_structrp png_ptr),PNG_EMPTY); ++ ++#ifdef PNG_WRITE_gAMA_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_write_gAMA_fixed,(png_structrp png_ptr, ++ png_fixed_point file_gamma),PNG_EMPTY); ++#endif ++ ++#ifdef PNG_WRITE_sBIT_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_write_sBIT,(png_structrp png_ptr, ++ png_const_color_8p sbit, int color_type),PNG_EMPTY); ++#endif ++ ++#ifdef PNG_WRITE_cHRM_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_write_cHRM_fixed,(png_structrp png_ptr, ++ const png_xy *xy), PNG_EMPTY); ++ /* The xy value must have been previously validated */ ++#endif ++ ++#ifdef PNG_WRITE_sRGB_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_write_sRGB,(png_structrp png_ptr, ++ int intent),PNG_EMPTY); ++#endif ++ ++#ifdef PNG_WRITE_eXIf_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_write_eXIf,(png_structrp png_ptr, ++ png_bytep exif, int num_exif),PNG_EMPTY); ++#endif ++ ++#ifdef PNG_WRITE_iCCP_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_write_iCCP,(png_structrp png_ptr, ++ png_const_charp name, png_const_bytep profile), PNG_EMPTY); ++ /* The profile must have been previously validated for correctness, the ++ * length comes from the first four bytes. Only the base, deflate, ++ * compression is supported. ++ */ ++#endif ++ ++#ifdef PNG_WRITE_sPLT_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_write_sPLT,(png_structrp png_ptr, ++ png_const_sPLT_tp palette),PNG_EMPTY); ++#endif ++ ++#ifdef PNG_WRITE_tRNS_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_write_tRNS,(png_structrp png_ptr, ++ png_const_bytep trans, png_const_color_16p values, int number, ++ int color_type),PNG_EMPTY); ++#endif ++ ++#ifdef PNG_WRITE_bKGD_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_write_bKGD,(png_structrp png_ptr, ++ png_const_color_16p values, int color_type),PNG_EMPTY); ++#endif ++ ++#ifdef PNG_WRITE_hIST_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_write_hIST,(png_structrp png_ptr, ++ png_const_uint_16p hist, int num_hist),PNG_EMPTY); ++#endif ++ ++/* Chunks that have keywords */ ++#ifdef PNG_WRITE_tEXt_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_write_tEXt,(png_structrp png_ptr, ++ png_const_charp key, png_const_charp text, size_t text_len),PNG_EMPTY); ++#endif ++ ++#ifdef PNG_WRITE_zTXt_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_write_zTXt,(png_structrp png_ptr, png_const_charp ++ key, png_const_charp text, int compression),PNG_EMPTY); ++#endif ++ ++#ifdef PNG_WRITE_iTXt_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_write_iTXt,(png_structrp png_ptr, ++ int compression, png_const_charp key, png_const_charp lang, ++ png_const_charp lang_key, png_const_charp text),PNG_EMPTY); ++#endif ++ ++#ifdef PNG_TEXT_SUPPORTED /* Added at version 1.0.14 and 1.2.4 */ ++PNG_INTERNAL_FUNCTION(int,png_set_text_2,(png_const_structrp png_ptr, ++ png_inforp info_ptr, png_const_textp text_ptr, int num_text),PNG_EMPTY); ++#endif ++ ++#ifdef PNG_WRITE_oFFs_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_write_oFFs,(png_structrp png_ptr, ++ png_int_32 x_offset, png_int_32 y_offset, int unit_type),PNG_EMPTY); ++#endif ++ ++#ifdef PNG_WRITE_pCAL_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_write_pCAL,(png_structrp png_ptr, ++ png_charp purpose, png_int_32 X0, png_int_32 X1, int type, int nparams, ++ png_const_charp units, png_charpp params),PNG_EMPTY); ++#endif ++ ++#ifdef PNG_WRITE_pHYs_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_write_pHYs,(png_structrp png_ptr, ++ png_uint_32 x_pixels_per_unit, png_uint_32 y_pixels_per_unit, ++ int unit_type),PNG_EMPTY); ++#endif ++ ++#ifdef PNG_WRITE_tIME_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_write_tIME,(png_structrp png_ptr, ++ png_const_timep mod_time),PNG_EMPTY); ++#endif ++ ++#ifdef PNG_WRITE_sCAL_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_write_sCAL_s,(png_structrp png_ptr, ++ int unit, png_const_charp width, png_const_charp height),PNG_EMPTY); ++#endif ++ ++/* Called when finished processing a row of data */ ++PNG_INTERNAL_FUNCTION(void,png_write_finish_row,(png_structrp png_ptr), ++ PNG_EMPTY); ++ ++/* Internal use only. Called before first row of data */ ++PNG_INTERNAL_FUNCTION(void,png_write_start_row,(png_structrp png_ptr), ++ PNG_EMPTY); ++ ++/* Combine a row of data, dealing with alpha, etc. if requested. 'row' is an ++ * array of png_ptr->width pixels. If the image is not interlaced or this ++ * is the final pass this just does a memcpy, otherwise the "display" flag ++ * is used to determine whether to copy pixels that are not in the current pass. ++ * ++ * Because 'png_do_read_interlace' (below) replicates pixels this allows this ++ * function to achieve the documented 'blocky' appearance during interlaced read ++ * if display is 1 and the 'sparkle' appearance, where existing pixels in 'row' ++ * are not changed if they are not in the current pass, when display is 0. ++ * ++ * 'display' must be 0 or 1, otherwise the memcpy will be done regardless. ++ * ++ * The API always reads from the png_struct row buffer and always assumes that ++ * it is full width (png_do_read_interlace has already been called.) ++ * ++ * This function is only ever used to write to row buffers provided by the ++ * caller of the relevant libpng API and the row must have already been ++ * transformed by the read transformations. ++ * ++ * The PNG_USE_COMPILE_TIME_MASKS option causes generation of pre-computed ++ * bitmasks for use within the code, otherwise runtime generated masks are used. ++ * The default is compile time masks. ++ */ ++#ifndef PNG_USE_COMPILE_TIME_MASKS ++# define PNG_USE_COMPILE_TIME_MASKS 1 ++#endif ++PNG_INTERNAL_FUNCTION(void,png_combine_row,(png_const_structrp png_ptr, ++ png_bytep row, int display),PNG_EMPTY); ++ ++#ifdef PNG_READ_INTERLACING_SUPPORTED ++/* Expand an interlaced row: the 'row_info' describes the pass data that has ++ * been read in and must correspond to the pixels in 'row', the pixels are ++ * expanded (moved apart) in 'row' to match the final layout, when doing this ++ * the pixels are *replicated* to the intervening space. This is essential for ++ * the correct operation of png_combine_row, above. ++ */ ++PNG_INTERNAL_FUNCTION(void,png_do_read_interlace,(png_row_infop row_info, ++ png_bytep row, int pass, png_uint_32 transformations),PNG_EMPTY); ++#endif ++ ++/* GRR TO DO (2.0 or whenever): simplify other internal calling interfaces */ ++ ++#ifdef PNG_WRITE_INTERLACING_SUPPORTED ++/* Grab pixels out of a row for an interlaced pass */ ++PNG_INTERNAL_FUNCTION(void,png_do_write_interlace,(png_row_infop row_info, ++ png_bytep row, int pass),PNG_EMPTY); ++#endif ++ ++/* Unfilter a row: check the filter value before calling this, there is no point ++ * calling it for PNG_FILTER_VALUE_NONE. ++ */ ++PNG_INTERNAL_FUNCTION(void,png_read_filter_row,(png_structrp pp, png_row_infop ++ row_info, png_bytep row, png_const_bytep prev_row, int filter),PNG_EMPTY); ++ ++#if PNG_ARM_NEON_OPT > 0 ++PNG_INTERNAL_FUNCTION(void,png_read_filter_row_up_neon,(png_row_infop row_info, ++ png_bytep row, png_const_bytep prev_row),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_read_filter_row_sub3_neon,(png_row_infop ++ row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_read_filter_row_sub4_neon,(png_row_infop ++ row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_read_filter_row_avg3_neon,(png_row_infop ++ row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_read_filter_row_avg4_neon,(png_row_infop ++ row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_read_filter_row_paeth3_neon,(png_row_infop ++ row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_read_filter_row_paeth4_neon,(png_row_infop ++ row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY); ++#endif ++ ++#if PNG_MIPS_MSA_OPT > 0 ++PNG_INTERNAL_FUNCTION(void,png_read_filter_row_up_msa,(png_row_infop row_info, ++ png_bytep row, png_const_bytep prev_row),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_read_filter_row_sub3_msa,(png_row_infop ++ row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_read_filter_row_sub4_msa,(png_row_infop ++ row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_read_filter_row_avg3_msa,(png_row_infop ++ row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_read_filter_row_avg4_msa,(png_row_infop ++ row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_read_filter_row_paeth3_msa,(png_row_infop ++ row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_read_filter_row_paeth4_msa,(png_row_infop ++ row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY); ++#endif ++ ++#if PNG_POWERPC_VSX_OPT > 0 ++PNG_INTERNAL_FUNCTION(void,png_read_filter_row_up_vsx,(png_row_infop row_info, ++ png_bytep row, png_const_bytep prev_row),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_read_filter_row_sub3_vsx,(png_row_infop ++ row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_read_filter_row_sub4_vsx,(png_row_infop ++ row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_read_filter_row_avg3_vsx,(png_row_infop ++ row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_read_filter_row_avg4_vsx,(png_row_infop ++ row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_read_filter_row_paeth3_vsx,(png_row_infop ++ row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_read_filter_row_paeth4_vsx,(png_row_infop ++ row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY); ++#endif ++ ++#if PNG_INTEL_SSE_IMPLEMENTATION > 0 ++PNG_INTERNAL_FUNCTION(void,png_read_filter_row_sub3_sse2,(png_row_infop ++ row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_read_filter_row_sub4_sse2,(png_row_infop ++ row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_read_filter_row_avg3_sse2,(png_row_infop ++ row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_read_filter_row_avg4_sse2,(png_row_infop ++ row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_read_filter_row_paeth3_sse2,(png_row_infop ++ row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_read_filter_row_paeth4_sse2,(png_row_infop ++ row_info, png_bytep row, png_const_bytep prev_row),PNG_EMPTY); ++#endif ++ ++/* Choose the best filter to use and filter the row data */ ++PNG_INTERNAL_FUNCTION(void,png_write_find_filter,(png_structrp png_ptr, ++ png_row_infop row_info),PNG_EMPTY); ++ ++#ifdef PNG_SEQUENTIAL_READ_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_read_IDAT_data,(png_structrp png_ptr, ++ png_bytep output, png_alloc_size_t avail_out),PNG_EMPTY); ++ /* Read 'avail_out' bytes of data from the IDAT stream. If the output buffer ++ * is NULL the function checks, instead, for the end of the stream. In this ++ * case a benign error will be issued if the stream end is not found or if ++ * extra data has to be consumed. ++ */ ++PNG_INTERNAL_FUNCTION(void,png_read_finish_IDAT,(png_structrp png_ptr), ++ PNG_EMPTY); ++ /* This cleans up when the IDAT LZ stream does not end when the last image ++ * byte is read; there is still some pending input. ++ */ ++ ++PNG_INTERNAL_FUNCTION(void,png_read_finish_row,(png_structrp png_ptr), ++ PNG_EMPTY); ++ /* Finish a row while reading, dealing with interlacing passes, etc. */ ++#endif /* SEQUENTIAL_READ */ ++ ++/* Initialize the row buffers, etc. */ ++PNG_INTERNAL_FUNCTION(void,png_read_start_row,(png_structrp png_ptr),PNG_EMPTY); ++ ++#if ZLIB_VERNUM >= 0x1240 ++PNG_INTERNAL_FUNCTION(int,png_zlib_inflate,(png_structrp png_ptr, int flush), ++ PNG_EMPTY); ++# define PNG_INFLATE(pp, flush) png_zlib_inflate(pp, flush) ++#else /* Zlib < 1.2.4 */ ++# define PNG_INFLATE(pp, flush) inflate(&(pp)->zstream, flush) ++#endif /* Zlib < 1.2.4 */ ++ ++#ifdef PNG_READ_TRANSFORMS_SUPPORTED ++/* Optional call to update the users info structure */ ++PNG_INTERNAL_FUNCTION(void,png_read_transform_info,(png_structrp png_ptr, ++ png_inforp info_ptr),PNG_EMPTY); ++#endif ++ ++/* Shared transform functions, defined in pngtran.c */ ++#if defined(PNG_WRITE_FILLER_SUPPORTED) || \ ++ defined(PNG_READ_STRIP_ALPHA_SUPPORTED) ++PNG_INTERNAL_FUNCTION(void,png_do_strip_channel,(png_row_infop row_info, ++ png_bytep row, int at_start),PNG_EMPTY); ++#endif ++ ++#ifdef PNG_16BIT_SUPPORTED ++#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED) ++PNG_INTERNAL_FUNCTION(void,png_do_swap,(png_row_infop row_info, ++ png_bytep row),PNG_EMPTY); ++#endif ++#endif ++ ++#if defined(PNG_READ_PACKSWAP_SUPPORTED) || \ ++ defined(PNG_WRITE_PACKSWAP_SUPPORTED) ++PNG_INTERNAL_FUNCTION(void,png_do_packswap,(png_row_infop row_info, ++ png_bytep row),PNG_EMPTY); ++#endif ++ ++#if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED) ++PNG_INTERNAL_FUNCTION(void,png_do_invert,(png_row_infop row_info, ++ png_bytep row),PNG_EMPTY); ++#endif ++ ++#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED) ++PNG_INTERNAL_FUNCTION(void,png_do_bgr,(png_row_infop row_info, ++ png_bytep row),PNG_EMPTY); ++#endif ++ ++/* The following decodes the appropriate chunks, and does error correction, ++ * then calls the appropriate callback for the chunk if it is valid. ++ */ ++ ++/* Decode the IHDR chunk */ ++PNG_INTERNAL_FUNCTION(void,png_handle_IHDR,(png_structrp png_ptr, ++ png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_handle_PLTE,(png_structrp png_ptr, ++ png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_handle_IEND,(png_structrp png_ptr, ++ png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); ++ ++#ifdef PNG_READ_bKGD_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_handle_bKGD,(png_structrp png_ptr, ++ png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); ++#endif ++ ++#ifdef PNG_READ_cHRM_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_handle_cHRM,(png_structrp png_ptr, ++ png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); ++#endif ++ ++#ifdef PNG_READ_eXIf_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_handle_eXIf,(png_structrp png_ptr, ++ png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); ++#endif ++ ++#ifdef PNG_READ_gAMA_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_handle_gAMA,(png_structrp png_ptr, ++ png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); ++#endif ++ ++#ifdef PNG_READ_hIST_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_handle_hIST,(png_structrp png_ptr, ++ png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); ++#endif ++ ++#ifdef PNG_READ_iCCP_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_handle_iCCP,(png_structrp png_ptr, ++ png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); ++#endif /* READ_iCCP */ ++ ++#ifdef PNG_READ_iTXt_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_handle_iTXt,(png_structrp png_ptr, ++ png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); ++#endif ++ ++#ifdef PNG_READ_oFFs_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_handle_oFFs,(png_structrp png_ptr, ++ png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); ++#endif ++ ++#ifdef PNG_READ_pCAL_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_handle_pCAL,(png_structrp png_ptr, ++ png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); ++#endif ++ ++#ifdef PNG_READ_pHYs_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_handle_pHYs,(png_structrp png_ptr, ++ png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); ++#endif ++ ++#ifdef PNG_READ_sBIT_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_handle_sBIT,(png_structrp png_ptr, ++ png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); ++#endif ++ ++#ifdef PNG_READ_sCAL_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_handle_sCAL,(png_structrp png_ptr, ++ png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); ++#endif ++ ++#ifdef PNG_READ_sPLT_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_handle_sPLT,(png_structrp png_ptr, ++ png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); ++#endif /* READ_sPLT */ ++ ++#ifdef PNG_READ_sRGB_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_handle_sRGB,(png_structrp png_ptr, ++ png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); ++#endif ++ ++#ifdef PNG_READ_tEXt_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_handle_tEXt,(png_structrp png_ptr, ++ png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); ++#endif ++ ++#ifdef PNG_READ_tIME_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_handle_tIME,(png_structrp png_ptr, ++ png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); ++#endif ++ ++#ifdef PNG_READ_tRNS_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_handle_tRNS,(png_structrp png_ptr, ++ png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); ++#endif ++ ++#ifdef PNG_READ_zTXt_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_handle_zTXt,(png_structrp png_ptr, ++ png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); ++#endif ++ ++PNG_INTERNAL_FUNCTION(void,png_check_chunk_name,(png_const_structrp png_ptr, ++ png_uint_32 chunk_name),PNG_EMPTY); ++ ++PNG_INTERNAL_FUNCTION(void,png_check_chunk_length,(png_const_structrp png_ptr, ++ png_uint_32 chunk_length),PNG_EMPTY); ++ ++PNG_INTERNAL_FUNCTION(void,png_handle_unknown,(png_structrp png_ptr, ++ png_inforp info_ptr, png_uint_32 length, int keep),PNG_EMPTY); ++ /* This is the function that gets called for unknown chunks. The 'keep' ++ * argument is either non-zero for a known chunk that has been set to be ++ * handled as unknown or zero for an unknown chunk. By default the function ++ * just skips the chunk or errors out if it is critical. ++ */ ++ ++#if defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) ||\ ++ defined(PNG_HANDLE_AS_UNKNOWN_SUPPORTED) ++PNG_INTERNAL_FUNCTION(int,png_chunk_unknown_handling, ++ (png_const_structrp png_ptr, png_uint_32 chunk_name),PNG_EMPTY); ++ /* Exactly as the API png_handle_as_unknown() except that the argument is a ++ * 32-bit chunk name, not a string. ++ */ ++#endif /* READ_UNKNOWN_CHUNKS || HANDLE_AS_UNKNOWN */ ++ ++/* Handle the transformations for reading and writing */ ++#ifdef PNG_READ_TRANSFORMS_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_do_read_transformations,(png_structrp png_ptr, ++ png_row_infop row_info),PNG_EMPTY); ++#endif ++#ifdef PNG_WRITE_TRANSFORMS_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_do_write_transformations,(png_structrp png_ptr, ++ png_row_infop row_info),PNG_EMPTY); ++#endif ++ ++#ifdef PNG_READ_TRANSFORMS_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_init_read_transformations,(png_structrp png_ptr), ++ PNG_EMPTY); ++#endif ++ ++#ifdef PNG_PROGRESSIVE_READ_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_push_read_chunk,(png_structrp png_ptr, ++ png_inforp info_ptr),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_push_read_sig,(png_structrp png_ptr, ++ png_inforp info_ptr),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_push_check_crc,(png_structrp png_ptr),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_push_save_buffer,(png_structrp png_ptr), ++ PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_push_restore_buffer,(png_structrp png_ptr, ++ png_bytep buffer, size_t buffer_length),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_push_read_IDAT,(png_structrp png_ptr),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_process_IDAT_data,(png_structrp png_ptr, ++ png_bytep buffer, size_t buffer_length),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_push_process_row,(png_structrp png_ptr), ++ PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_push_handle_unknown,(png_structrp png_ptr, ++ png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_push_have_info,(png_structrp png_ptr, ++ png_inforp info_ptr),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_push_have_end,(png_structrp png_ptr, ++ png_inforp info_ptr),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_push_have_row,(png_structrp png_ptr, ++ png_bytep row),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_push_read_end,(png_structrp png_ptr, ++ png_inforp info_ptr),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_process_some_data,(png_structrp png_ptr, ++ png_inforp info_ptr),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_read_push_finish_row,(png_structrp png_ptr), ++ PNG_EMPTY); ++# ifdef PNG_READ_tEXt_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_push_handle_tEXt,(png_structrp png_ptr, ++ png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_push_read_tEXt,(png_structrp png_ptr, ++ png_inforp info_ptr),PNG_EMPTY); ++# endif ++# ifdef PNG_READ_zTXt_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_push_handle_zTXt,(png_structrp png_ptr, ++ png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_push_read_zTXt,(png_structrp png_ptr, ++ png_inforp info_ptr),PNG_EMPTY); ++# endif ++# ifdef PNG_READ_iTXt_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_push_handle_iTXt,(png_structrp png_ptr, ++ png_inforp info_ptr, png_uint_32 length),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_push_read_iTXt,(png_structrp png_ptr, ++ png_inforp info_ptr),PNG_EMPTY); ++# endif ++ ++#endif /* PROGRESSIVE_READ */ ++ ++/* Added at libpng version 1.6.0 */ ++#ifdef PNG_GAMMA_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_colorspace_set_gamma,(png_const_structrp png_ptr, ++ png_colorspacerp colorspace, png_fixed_point gAMA), PNG_EMPTY); ++ /* Set the colorspace gamma with a value provided by the application or by ++ * the gAMA chunk on read. The value will override anything set by an ICC ++ * profile. ++ */ ++ ++PNG_INTERNAL_FUNCTION(void,png_colorspace_sync_info,(png_const_structrp png_ptr, ++ png_inforp info_ptr), PNG_EMPTY); ++ /* Synchronize the info 'valid' flags with the colorspace */ ++ ++PNG_INTERNAL_FUNCTION(void,png_colorspace_sync,(png_const_structrp png_ptr, ++ png_inforp info_ptr), PNG_EMPTY); ++ /* Copy the png_struct colorspace to the info_struct and call the above to ++ * synchronize the flags. Checks for NULL info_ptr and does nothing. ++ */ ++#endif ++ ++/* Added at libpng version 1.4.0 */ ++#ifdef PNG_COLORSPACE_SUPPORTED ++/* These internal functions are for maintaining the colorspace structure within ++ * a png_info or png_struct (or, indeed, both). ++ */ ++PNG_INTERNAL_FUNCTION(int,png_colorspace_set_chromaticities, ++ (png_const_structrp png_ptr, png_colorspacerp colorspace, const png_xy *xy, ++ int preferred), PNG_EMPTY); ++ ++PNG_INTERNAL_FUNCTION(int,png_colorspace_set_endpoints, ++ (png_const_structrp png_ptr, png_colorspacerp colorspace, const png_XYZ *XYZ, ++ int preferred), PNG_EMPTY); ++ ++#ifdef PNG_sRGB_SUPPORTED ++PNG_INTERNAL_FUNCTION(int,png_colorspace_set_sRGB,(png_const_structrp png_ptr, ++ png_colorspacerp colorspace, int intent), PNG_EMPTY); ++ /* This does set the colorspace gAMA and cHRM values too, but doesn't set the ++ * flags to write them, if it returns false there was a problem and an error ++ * message has already been output (but the colorspace may still need to be ++ * synced to record the invalid flag). ++ */ ++#endif /* sRGB */ ++ ++#ifdef PNG_iCCP_SUPPORTED ++PNG_INTERNAL_FUNCTION(int,png_colorspace_set_ICC,(png_const_structrp png_ptr, ++ png_colorspacerp colorspace, png_const_charp name, ++ png_uint_32 profile_length, png_const_bytep profile, int color_type), ++ PNG_EMPTY); ++ /* The 'name' is used for information only */ ++ ++/* Routines for checking parts of an ICC profile. */ ++#ifdef PNG_READ_iCCP_SUPPORTED ++PNG_INTERNAL_FUNCTION(int,png_icc_check_length,(png_const_structrp png_ptr, ++ png_colorspacerp colorspace, png_const_charp name, ++ png_uint_32 profile_length), PNG_EMPTY); ++#endif /* READ_iCCP */ ++PNG_INTERNAL_FUNCTION(int,png_icc_check_header,(png_const_structrp png_ptr, ++ png_colorspacerp colorspace, png_const_charp name, ++ png_uint_32 profile_length, ++ png_const_bytep profile /* first 132 bytes only */, int color_type), ++ PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(int,png_icc_check_tag_table,(png_const_structrp png_ptr, ++ png_colorspacerp colorspace, png_const_charp name, ++ png_uint_32 profile_length, ++ png_const_bytep profile /* header plus whole tag table */), PNG_EMPTY); ++#ifdef PNG_sRGB_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_icc_set_sRGB,( ++ png_const_structrp png_ptr, png_colorspacerp colorspace, ++ png_const_bytep profile, uLong adler), PNG_EMPTY); ++ /* 'adler' is the Adler32 checksum of the uncompressed profile data. It may ++ * be zero to indicate that it is not available. It is used, if provided, ++ * as a fast check on the profile when checking to see if it is sRGB. ++ */ ++#endif ++#endif /* iCCP */ ++ ++#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_colorspace_set_rgb_coefficients, ++ (png_structrp png_ptr), PNG_EMPTY); ++ /* Set the rgb_to_gray coefficients from the colorspace Y values */ ++#endif /* READ_RGB_TO_GRAY */ ++#endif /* COLORSPACE */ ++ ++/* Added at libpng version 1.4.0 */ ++PNG_INTERNAL_FUNCTION(void,png_check_IHDR,(png_const_structrp png_ptr, ++ png_uint_32 width, png_uint_32 height, int bit_depth, ++ int color_type, int interlace_type, int compression_type, ++ int filter_type),PNG_EMPTY); ++ ++/* Added at libpng version 1.5.10 */ ++#if defined(PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED) || \ ++ defined(PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED) ++PNG_INTERNAL_FUNCTION(void,png_do_check_palette_indexes, ++ (png_structrp png_ptr, png_row_infop row_info),PNG_EMPTY); ++#endif ++ ++#if defined(PNG_FLOATING_POINT_SUPPORTED) && defined(PNG_ERROR_TEXT_SUPPORTED) ++PNG_INTERNAL_FUNCTION(void,png_fixed_error,(png_const_structrp png_ptr, ++ png_const_charp name),PNG_NORETURN); ++#endif ++ ++/* Puts 'string' into 'buffer' at buffer[pos], taking care never to overwrite ++ * the end. Always leaves the buffer nul terminated. Never errors out (and ++ * there is no error code.) ++ */ ++PNG_INTERNAL_FUNCTION(size_t,png_safecat,(png_charp buffer, size_t bufsize, ++ size_t pos, png_const_charp string),PNG_EMPTY); ++ ++/* Various internal functions to handle formatted warning messages, currently ++ * only implemented for warnings. ++ */ ++#if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_TIME_RFC1123_SUPPORTED) ++/* Utility to dump an unsigned value into a buffer, given a start pointer and ++ * and end pointer (which should point just *beyond* the end of the buffer!) ++ * Returns the pointer to the start of the formatted string. This utility only ++ * does unsigned values. ++ */ ++PNG_INTERNAL_FUNCTION(png_charp,png_format_number,(png_const_charp start, ++ png_charp end, int format, png_alloc_size_t number),PNG_EMPTY); ++ ++/* Convenience macro that takes an array: */ ++#define PNG_FORMAT_NUMBER(buffer,format,number) \ ++ png_format_number(buffer, buffer + (sizeof buffer), format, number) ++ ++/* Suggested size for a number buffer (enough for 64 bits and a sign!) */ ++#define PNG_NUMBER_BUFFER_SIZE 24 ++ ++/* These are the integer formats currently supported, the name is formed from ++ * the standard printf(3) format string. ++ */ ++#define PNG_NUMBER_FORMAT_u 1 /* chose unsigned API! */ ++#define PNG_NUMBER_FORMAT_02u 2 ++#define PNG_NUMBER_FORMAT_d 1 /* chose signed API! */ ++#define PNG_NUMBER_FORMAT_02d 2 ++#define PNG_NUMBER_FORMAT_x 3 ++#define PNG_NUMBER_FORMAT_02x 4 ++#define PNG_NUMBER_FORMAT_fixed 5 /* choose the signed API */ ++#endif ++ ++#ifdef PNG_WARNINGS_SUPPORTED ++/* New defines and members adding in libpng-1.5.4 */ ++# define PNG_WARNING_PARAMETER_SIZE 32 ++# define PNG_WARNING_PARAMETER_COUNT 8 /* Maximum 9; see pngerror.c */ ++ ++/* An l-value of this type has to be passed to the APIs below to cache the ++ * values of the parameters to a formatted warning message. ++ */ ++typedef char png_warning_parameters[PNG_WARNING_PARAMETER_COUNT][ ++ PNG_WARNING_PARAMETER_SIZE]; ++ ++PNG_INTERNAL_FUNCTION(void,png_warning_parameter,(png_warning_parameters p, ++ int number, png_const_charp string),PNG_EMPTY); ++ /* Parameters are limited in size to PNG_WARNING_PARAMETER_SIZE characters, ++ * including the trailing '\0'. ++ */ ++PNG_INTERNAL_FUNCTION(void,png_warning_parameter_unsigned, ++ (png_warning_parameters p, int number, int format, png_alloc_size_t value), ++ PNG_EMPTY); ++ /* Use png_alloc_size_t because it is an unsigned type as big as any we ++ * need to output. Use the following for a signed value. ++ */ ++PNG_INTERNAL_FUNCTION(void,png_warning_parameter_signed, ++ (png_warning_parameters p, int number, int format, png_int_32 value), ++ PNG_EMPTY); ++ ++PNG_INTERNAL_FUNCTION(void,png_formatted_warning,(png_const_structrp png_ptr, ++ png_warning_parameters p, png_const_charp message),PNG_EMPTY); ++ /* 'message' follows the X/Open approach of using @1, @2 to insert ++ * parameters previously supplied using the above functions. Errors in ++ * specifying the parameters will simply result in garbage substitutions. ++ */ ++#endif ++ ++#ifdef PNG_BENIGN_ERRORS_SUPPORTED ++/* Application errors (new in 1.6); use these functions (declared below) for ++ * errors in the parameters or order of API function calls on read. The ++ * 'warning' should be used for an error that can be handled completely; the ++ * 'error' for one which can be handled safely but which may lose application ++ * information or settings. ++ * ++ * By default these both result in a png_error call prior to release, while in a ++ * released version the 'warning' is just a warning. However if the application ++ * explicitly disables benign errors (explicitly permitting the code to lose ++ * information) they both turn into warnings. ++ * ++ * If benign errors aren't supported they end up as the corresponding base call ++ * (png_warning or png_error.) ++ */ ++PNG_INTERNAL_FUNCTION(void,png_app_warning,(png_const_structrp png_ptr, ++ png_const_charp message),PNG_EMPTY); ++ /* The application provided invalid parameters to an API function or called ++ * an API function at the wrong time, libpng can completely recover. ++ */ ++ ++PNG_INTERNAL_FUNCTION(void,png_app_error,(png_const_structrp png_ptr, ++ png_const_charp message),PNG_EMPTY); ++ /* As above but libpng will ignore the call, or attempt some other partial ++ * recovery from the error. ++ */ ++#else ++# define png_app_warning(pp,s) png_warning(pp,s) ++# define png_app_error(pp,s) png_error(pp,s) ++#endif ++ ++PNG_INTERNAL_FUNCTION(void,png_chunk_report,(png_const_structrp png_ptr, ++ png_const_charp message, int error),PNG_EMPTY); ++ /* Report a recoverable issue in chunk data. On read this is used to report ++ * a problem found while reading a particular chunk and the ++ * png_chunk_benign_error or png_chunk_warning function is used as ++ * appropriate. On write this is used to report an error that comes from ++ * data set via an application call to a png_set_ API and png_app_error or ++ * png_app_warning is used as appropriate. ++ * ++ * The 'error' parameter must have one of the following values: ++ */ ++#define PNG_CHUNK_WARNING 0 /* never an error */ ++#define PNG_CHUNK_WRITE_ERROR 1 /* an error only on write */ ++#define PNG_CHUNK_ERROR 2 /* always an error */ ++ ++/* ASCII to FP interfaces, currently only implemented if sCAL ++ * support is required. ++ */ ++#if defined(PNG_sCAL_SUPPORTED) ++/* MAX_DIGITS is actually the maximum number of characters in an sCAL ++ * width or height, derived from the precision (number of significant ++ * digits - a build time settable option) and assumptions about the ++ * maximum ridiculous exponent. ++ */ ++#define PNG_sCAL_MAX_DIGITS (PNG_sCAL_PRECISION+1/*.*/+1/*E*/+10/*exponent*/) ++ ++#ifdef PNG_FLOATING_POINT_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_ascii_from_fp,(png_const_structrp png_ptr, ++ png_charp ascii, size_t size, double fp, unsigned int precision), ++ PNG_EMPTY); ++#endif /* FLOATING_POINT */ ++ ++#ifdef PNG_FIXED_POINT_SUPPORTED ++PNG_INTERNAL_FUNCTION(void,png_ascii_from_fixed,(png_const_structrp png_ptr, ++ png_charp ascii, size_t size, png_fixed_point fp),PNG_EMPTY); ++#endif /* FIXED_POINT */ ++#endif /* sCAL */ ++ ++#if defined(PNG_sCAL_SUPPORTED) || defined(PNG_pCAL_SUPPORTED) ++/* An internal API to validate the format of a floating point number. ++ * The result is the index of the next character. If the number is ++ * not valid it will be the index of a character in the supposed number. ++ * ++ * The format of a number is defined in the PNG extensions specification ++ * and this API is strictly conformant to that spec, not anyone elses! ++ * ++ * The format as a regular expression is: ++ * ++ * [+-]?[0-9]+.?([Ee][+-]?[0-9]+)? ++ * ++ * or: ++ * ++ * [+-]?.[0-9]+(.[0-9]+)?([Ee][+-]?[0-9]+)? ++ * ++ * The complexity is that either integer or fraction must be present and the ++ * fraction is permitted to have no digits only if the integer is present. ++ * ++ * NOTE: The dangling E problem. ++ * There is a PNG valid floating point number in the following: ++ * ++ * PNG floating point numbers are not greedy. ++ * ++ * Working this out requires *TWO* character lookahead (because of the ++ * sign), the parser does not do this - it will fail at the 'r' - this ++ * doesn't matter for PNG sCAL chunk values, but it requires more care ++ * if the value were ever to be embedded in something more complex. Use ++ * ANSI-C strtod if you need the lookahead. ++ */ ++/* State table for the parser. */ ++#define PNG_FP_INTEGER 0 /* before or in integer */ ++#define PNG_FP_FRACTION 1 /* before or in fraction */ ++#define PNG_FP_EXPONENT 2 /* before or in exponent */ ++#define PNG_FP_STATE 3 /* mask for the above */ ++#define PNG_FP_SAW_SIGN 4 /* Saw +/- in current state */ ++#define PNG_FP_SAW_DIGIT 8 /* Saw a digit in current state */ ++#define PNG_FP_SAW_DOT 16 /* Saw a dot in current state */ ++#define PNG_FP_SAW_E 32 /* Saw an E (or e) in current state */ ++#define PNG_FP_SAW_ANY 60 /* Saw any of the above 4 */ ++ ++/* These three values don't affect the parser. They are set but not used. ++ */ ++#define PNG_FP_WAS_VALID 64 /* Preceding substring is a valid fp number */ ++#define PNG_FP_NEGATIVE 128 /* A negative number, including "-0" */ ++#define PNG_FP_NONZERO 256 /* A non-zero value */ ++#define PNG_FP_STICKY 448 /* The above three flags */ ++ ++/* This is available for the caller to store in 'state' if required. Do not ++ * call the parser after setting it (the parser sometimes clears it.) ++ */ ++#define PNG_FP_INVALID 512 /* Available for callers as a distinct value */ ++ ++/* Result codes for the parser (boolean - true meants ok, false means ++ * not ok yet.) ++ */ ++#define PNG_FP_MAYBE 0 /* The number may be valid in the future */ ++#define PNG_FP_OK 1 /* The number is valid */ ++ ++/* Tests on the sticky non-zero and negative flags. To pass these checks ++ * the state must also indicate that the whole number is valid - this is ++ * achieved by testing PNG_FP_SAW_DIGIT (see the implementation for why this ++ * is equivalent to PNG_FP_OK above.) ++ */ ++#define PNG_FP_NZ_MASK (PNG_FP_SAW_DIGIT | PNG_FP_NEGATIVE | PNG_FP_NONZERO) ++ /* NZ_MASK: the string is valid and a non-zero negative value */ ++#define PNG_FP_Z_MASK (PNG_FP_SAW_DIGIT | PNG_FP_NONZERO) ++ /* Z MASK: the string is valid and a non-zero value. */ ++ /* PNG_FP_SAW_DIGIT: the string is valid. */ ++#define PNG_FP_IS_ZERO(state) (((state) & PNG_FP_Z_MASK) == PNG_FP_SAW_DIGIT) ++#define PNG_FP_IS_POSITIVE(state) (((state) & PNG_FP_NZ_MASK) == PNG_FP_Z_MASK) ++#define PNG_FP_IS_NEGATIVE(state) (((state) & PNG_FP_NZ_MASK) == PNG_FP_NZ_MASK) ++ ++/* The actual parser. This can be called repeatedly. It updates ++ * the index into the string and the state variable (which must ++ * be initialized to 0). It returns a result code, as above. There ++ * is no point calling the parser any more if it fails to advance to ++ * the end of the string - it is stuck on an invalid character (or ++ * terminated by '\0'). ++ * ++ * Note that the pointer will consume an E or even an E+ and then leave ++ * a 'maybe' state even though a preceding integer.fraction is valid. ++ * The PNG_FP_WAS_VALID flag indicates that a preceding substring was ++ * a valid number. It's possible to recover from this by calling ++ * the parser again (from the start, with state 0) but with a string ++ * that omits the last character (i.e. set the size to the index of ++ * the problem character.) This has not been tested within libpng. ++ */ ++PNG_INTERNAL_FUNCTION(int,png_check_fp_number,(png_const_charp string, ++ size_t size, int *statep, png_size_tp whereami),PNG_EMPTY); ++ ++/* This is the same but it checks a complete string and returns true ++ * only if it just contains a floating point number. As of 1.5.4 this ++ * function also returns the state at the end of parsing the number if ++ * it was valid (otherwise it returns 0.) This can be used for testing ++ * for negative or zero values using the sticky flag. ++ */ ++PNG_INTERNAL_FUNCTION(int,png_check_fp_string,(png_const_charp string, ++ size_t size),PNG_EMPTY); ++#endif /* pCAL || sCAL */ ++ ++#if defined(PNG_GAMMA_SUPPORTED) ||\ ++ defined(PNG_INCH_CONVERSIONS_SUPPORTED) || defined(PNG_READ_pHYs_SUPPORTED) ++/* Added at libpng version 1.5.0 */ ++/* This is a utility to provide a*times/div (rounded) and indicate ++ * if there is an overflow. The result is a boolean - false (0) ++ * for overflow, true (1) if no overflow, in which case *res ++ * holds the result. ++ */ ++PNG_INTERNAL_FUNCTION(int,png_muldiv,(png_fixed_point_p res, png_fixed_point a, ++ png_int_32 multiplied_by, png_int_32 divided_by),PNG_EMPTY); ++#endif ++ ++#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_INCH_CONVERSIONS_SUPPORTED) ++/* Same deal, but issue a warning on overflow and return 0. */ ++PNG_INTERNAL_FUNCTION(png_fixed_point,png_muldiv_warn, ++ (png_const_structrp png_ptr, png_fixed_point a, png_int_32 multiplied_by, ++ png_int_32 divided_by),PNG_EMPTY); ++#endif ++ ++#ifdef PNG_GAMMA_SUPPORTED ++/* Calculate a reciprocal - used for gamma values. This returns ++ * 0 if the argument is 0 in order to maintain an undefined value; ++ * there are no warnings. ++ */ ++PNG_INTERNAL_FUNCTION(png_fixed_point,png_reciprocal,(png_fixed_point a), ++ PNG_EMPTY); ++ ++#ifdef PNG_READ_GAMMA_SUPPORTED ++/* The same but gives a reciprocal of the product of two fixed point ++ * values. Accuracy is suitable for gamma calculations but this is ++ * not exact - use png_muldiv for that. Only required at present on read. ++ */ ++PNG_INTERNAL_FUNCTION(png_fixed_point,png_reciprocal2,(png_fixed_point a, ++ png_fixed_point b),PNG_EMPTY); ++#endif ++ ++/* Return true if the gamma value is significantly different from 1.0 */ ++PNG_INTERNAL_FUNCTION(int,png_gamma_significant,(png_fixed_point gamma_value), ++ PNG_EMPTY); ++#endif ++ ++#ifdef PNG_READ_GAMMA_SUPPORTED ++/* Internal fixed point gamma correction. These APIs are called as ++ * required to convert single values - they don't need to be fast, ++ * they are not used when processing image pixel values. ++ * ++ * While the input is an 'unsigned' value it must actually be the ++ * correct bit value - 0..255 or 0..65535 as required. ++ */ ++PNG_INTERNAL_FUNCTION(png_uint_16,png_gamma_correct,(png_structrp png_ptr, ++ unsigned int value, png_fixed_point gamma_value),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(png_uint_16,png_gamma_16bit_correct,(unsigned int value, ++ png_fixed_point gamma_value),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(png_byte,png_gamma_8bit_correct,(unsigned int value, ++ png_fixed_point gamma_value),PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_destroy_gamma_table,(png_structrp png_ptr), ++ PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(void,png_build_gamma_table,(png_structrp png_ptr, ++ int bit_depth),PNG_EMPTY); ++#endif ++ ++/* SIMPLIFIED READ/WRITE SUPPORT */ ++#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\ ++ defined(PNG_SIMPLIFIED_WRITE_SUPPORTED) ++/* The internal structure that png_image::opaque points to. */ ++typedef struct png_control ++{ ++ png_structp png_ptr; ++ png_infop info_ptr; ++ png_voidp error_buf; /* Always a jmp_buf at present. */ ++ ++ png_const_bytep memory; /* Memory buffer. */ ++ size_t size; /* Size of the memory buffer. */ ++ ++ unsigned int for_write :1; /* Otherwise it is a read structure */ ++ unsigned int owned_file :1; /* We own the file in io_ptr */ ++} png_control; ++ ++/* Return the pointer to the jmp_buf from a png_control: necessary because C ++ * does not reveal the type of the elements of jmp_buf. ++ */ ++#ifdef __cplusplus ++# define png_control_jmp_buf(pc) (((jmp_buf*)((pc)->error_buf))[0]) ++#else ++# define png_control_jmp_buf(pc) ((pc)->error_buf) ++#endif ++ ++/* Utility to safely execute a piece of libpng code catching and logging any ++ * errors that might occur. Returns true on success, false on failure (either ++ * of the function or as a result of a png_error.) ++ */ ++PNG_INTERNAL_CALLBACK(void,png_safe_error,(png_structp png_ptr, ++ png_const_charp error_message),PNG_NORETURN); ++ ++#ifdef PNG_WARNINGS_SUPPORTED ++PNG_INTERNAL_CALLBACK(void,png_safe_warning,(png_structp png_ptr, ++ png_const_charp warning_message),PNG_EMPTY); ++#else ++# define png_safe_warning 0/*dummy argument*/ ++#endif ++ ++PNG_INTERNAL_FUNCTION(int,png_safe_execute,(png_imagep image, ++ int (*function)(png_voidp), png_voidp arg),PNG_EMPTY); ++ ++/* Utility to log an error; this also cleans up the png_image; the function ++ * always returns 0 (false). ++ */ ++PNG_INTERNAL_FUNCTION(int,png_image_error,(png_imagep image, ++ png_const_charp error_message),PNG_EMPTY); ++ ++#ifndef PNG_SIMPLIFIED_READ_SUPPORTED ++/* png_image_free is used by the write code but not exported */ ++PNG_INTERNAL_FUNCTION(void, png_image_free, (png_imagep image), PNG_EMPTY); ++#endif /* !SIMPLIFIED_READ */ ++ ++#endif /* SIMPLIFIED READ/WRITE */ ++ ++/* These are initialization functions for hardware specific PNG filter ++ * optimizations; list these here then select the appropriate one at compile ++ * time using the macro PNG_FILTER_OPTIMIZATIONS. If the macro is not defined ++ * the generic code is used. ++ */ ++#ifdef PNG_FILTER_OPTIMIZATIONS ++PNG_INTERNAL_FUNCTION(void, PNG_FILTER_OPTIMIZATIONS, (png_structp png_ptr, ++ unsigned int bpp), PNG_EMPTY); ++ /* Just declare the optimization that will be used */ ++#else ++ /* List *all* the possible optimizations here - this branch is required if ++ * the builder of libpng passes the definition of PNG_FILTER_OPTIMIZATIONS in ++ * CFLAGS in place of CPPFLAGS *and* uses symbol prefixing. ++ */ ++# if PNG_ARM_NEON_OPT > 0 ++PNG_INTERNAL_FUNCTION(void, png_init_filter_functions_neon, ++ (png_structp png_ptr, unsigned int bpp), PNG_EMPTY); ++#endif ++ ++#if PNG_MIPS_MSA_OPT > 0 ++PNG_INTERNAL_FUNCTION(void, png_init_filter_functions_msa, ++ (png_structp png_ptr, unsigned int bpp), PNG_EMPTY); ++#endif ++ ++# if PNG_INTEL_SSE_IMPLEMENTATION > 0 ++PNG_INTERNAL_FUNCTION(void, png_init_filter_functions_sse2, ++ (png_structp png_ptr, unsigned int bpp), PNG_EMPTY); ++# endif ++#endif ++ ++PNG_INTERNAL_FUNCTION(png_uint_32, png_check_keyword, (png_structrp png_ptr, ++ png_const_charp key, png_bytep new_key), PNG_EMPTY); ++ ++#if (PNG_ARM_NEON_OPT > 0) && (PNG_ARM_NEON_IMPLEMENTATION == 1) ++PNG_INTERNAL_FUNCTION(void, ++ png_riffle_palette_neon, ++ (png_structrp), ++ PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(int, ++ png_do_expand_palette_rgba8_neon, ++ (png_structrp, ++ png_row_infop, ++ png_const_bytep, ++ const png_bytepp, ++ const png_bytepp), ++ PNG_EMPTY); ++PNG_INTERNAL_FUNCTION(int, ++ png_do_expand_palette_rgb8_neon, ++ (png_structrp, ++ png_row_infop, ++ png_const_bytep, ++ const png_bytepp, ++ const png_bytepp), ++ PNG_EMPTY); ++#endif ++ ++/* Maintainer: Put new private prototypes here ^ */ ++ ++#include "pngdebug.h" ++ ++#ifdef __cplusplus ++} ++#endif ++ ++#endif /* PNG_VERSION_INFO_ONLY */ ++#endif /* PNGPRIV_H */ +diff --git a/lib/libpng/pngread.c b/lib/libpng/pngread.c +new file mode 100644 +index 000000000..8fa7d9f16 +--- /dev/null ++++ b/lib/libpng/pngread.c +@@ -0,0 +1,4225 @@ ++ ++/* pngread.c - read a PNG file ++ * ++ * Copyright (c) 2018-2019 Cosmin Truta ++ * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson ++ * Copyright (c) 1996-1997 Andreas Dilger ++ * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc. ++ * ++ * This code is released under the libpng license. ++ * For conditions of distribution and use, see the disclaimer ++ * and license in png.h ++ * ++ * This file contains routines that an application calls directly to ++ * read a PNG file or stream. ++ */ ++ ++#include "pngpriv.h" ++#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) && defined(PNG_STDIO_SUPPORTED) ++# include ++#endif ++ ++#ifdef PNG_READ_SUPPORTED ++ ++/* Create a PNG structure for reading, and allocate any memory needed. */ ++PNG_FUNCTION(png_structp,PNGAPI ++png_create_read_struct,(png_const_charp user_png_ver, png_voidp error_ptr, ++ png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED) ++{ ++#ifndef PNG_USER_MEM_SUPPORTED ++ png_structp png_ptr = png_create_png_struct(user_png_ver, error_ptr, ++ error_fn, warn_fn, NULL, NULL, NULL); ++#else ++ return png_create_read_struct_2(user_png_ver, error_ptr, error_fn, ++ warn_fn, NULL, NULL, NULL); ++} ++ ++/* Alternate create PNG structure for reading, and allocate any memory ++ * needed. ++ */ ++PNG_FUNCTION(png_structp,PNGAPI ++png_create_read_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr, ++ png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr, ++ png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED) ++{ ++ png_structp png_ptr = png_create_png_struct(user_png_ver, error_ptr, ++ error_fn, warn_fn, mem_ptr, malloc_fn, free_fn); ++#endif /* USER_MEM */ ++ ++ if (png_ptr != NULL) ++ { ++ png_ptr->mode = PNG_IS_READ_STRUCT; ++ ++ /* Added in libpng-1.6.0; this can be used to detect a read structure if ++ * required (it will be zero in a write structure.) ++ */ ++# ifdef PNG_SEQUENTIAL_READ_SUPPORTED ++ png_ptr->IDAT_read_size = PNG_IDAT_READ_SIZE; ++# endif ++ ++# ifdef PNG_BENIGN_READ_ERRORS_SUPPORTED ++ png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN; ++ ++ /* In stable builds only warn if an application error can be completely ++ * handled. ++ */ ++# if PNG_RELEASE_BUILD ++ png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN; ++# endif ++# endif ++ ++ /* TODO: delay this, it can be done in png_init_io (if the app doesn't ++ * do it itself) avoiding setting the default function if it is not ++ * required. ++ */ ++ png_set_read_fn(png_ptr, NULL, NULL); ++ } ++ ++ return png_ptr; ++} ++ ++ ++#ifdef PNG_SEQUENTIAL_READ_SUPPORTED ++/* Read the information before the actual image data. This has been ++ * changed in v0.90 to allow reading a file that already has the magic ++ * bytes read from the stream. You can tell libpng how many bytes have ++ * been read from the beginning of the stream (up to the maximum of 8) ++ * via png_set_sig_bytes(), and we will only check the remaining bytes ++ * here. The application can then have access to the signature bytes we ++ * read if it is determined that this isn't a valid PNG file. ++ */ ++void PNGAPI ++png_read_info(png_structrp png_ptr, png_inforp info_ptr) ++{ ++#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED ++ int keep; ++#endif ++ ++ png_debug(1, "in png_read_info"); ++ ++ if (png_ptr == NULL || info_ptr == NULL) ++ return; ++ ++ /* Read and check the PNG file signature. */ ++ png_read_sig(png_ptr, info_ptr); ++ ++ for (;;) ++ { ++ png_uint_32 length = png_read_chunk_header(png_ptr); ++ png_uint_32 chunk_name = png_ptr->chunk_name; ++ ++ /* IDAT logic needs to happen here to simplify getting the two flags ++ * right. ++ */ ++ if (chunk_name == png_IDAT) ++ { ++ if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) ++ png_chunk_error(png_ptr, "Missing IHDR before IDAT"); ++ ++ else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && ++ (png_ptr->mode & PNG_HAVE_PLTE) == 0) ++ png_chunk_error(png_ptr, "Missing PLTE before IDAT"); ++ ++ else if ((png_ptr->mode & PNG_AFTER_IDAT) != 0) ++ png_chunk_benign_error(png_ptr, "Too many IDATs found"); ++ ++ png_ptr->mode |= PNG_HAVE_IDAT; ++ } ++ ++ else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) ++ { ++ png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT; ++ png_ptr->mode |= PNG_AFTER_IDAT; ++ } ++ ++ /* This should be a binary subdivision search or a hash for ++ * matching the chunk name rather than a linear search. ++ */ ++ if (chunk_name == png_IHDR) ++ png_handle_IHDR(png_ptr, info_ptr, length); ++ ++ else if (chunk_name == png_IEND) ++ png_handle_IEND(png_ptr, info_ptr, length); ++ ++#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED ++ else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0) ++ { ++ png_handle_unknown(png_ptr, info_ptr, length, keep); ++ ++ if (chunk_name == png_PLTE) ++ png_ptr->mode |= PNG_HAVE_PLTE; ++ ++ else if (chunk_name == png_IDAT) ++ { ++ png_ptr->idat_size = 0; /* It has been consumed */ ++ break; ++ } ++ } ++#endif ++ else if (chunk_name == png_PLTE) ++ png_handle_PLTE(png_ptr, info_ptr, length); ++ ++ else if (chunk_name == png_IDAT) ++ { ++ png_ptr->idat_size = length; ++ break; ++ } ++ ++#ifdef PNG_READ_bKGD_SUPPORTED ++ else if (chunk_name == png_bKGD) ++ png_handle_bKGD(png_ptr, info_ptr, length); ++#endif ++ ++#ifdef PNG_READ_cHRM_SUPPORTED ++ else if (chunk_name == png_cHRM) ++ png_handle_cHRM(png_ptr, info_ptr, length); ++#endif ++ ++#ifdef PNG_READ_eXIf_SUPPORTED ++ else if (chunk_name == png_eXIf) ++ png_handle_eXIf(png_ptr, info_ptr, length); ++#endif ++ ++#ifdef PNG_READ_gAMA_SUPPORTED ++ else if (chunk_name == png_gAMA) ++ png_handle_gAMA(png_ptr, info_ptr, length); ++#endif ++ ++#ifdef PNG_READ_hIST_SUPPORTED ++ else if (chunk_name == png_hIST) ++ png_handle_hIST(png_ptr, info_ptr, length); ++#endif ++ ++#ifdef PNG_READ_oFFs_SUPPORTED ++ else if (chunk_name == png_oFFs) ++ png_handle_oFFs(png_ptr, info_ptr, length); ++#endif ++ ++#ifdef PNG_READ_pCAL_SUPPORTED ++ else if (chunk_name == png_pCAL) ++ png_handle_pCAL(png_ptr, info_ptr, length); ++#endif ++ ++#ifdef PNG_READ_sCAL_SUPPORTED ++ else if (chunk_name == png_sCAL) ++ png_handle_sCAL(png_ptr, info_ptr, length); ++#endif ++ ++#ifdef PNG_READ_pHYs_SUPPORTED ++ else if (chunk_name == png_pHYs) ++ png_handle_pHYs(png_ptr, info_ptr, length); ++#endif ++ ++#ifdef PNG_READ_sBIT_SUPPORTED ++ else if (chunk_name == png_sBIT) ++ png_handle_sBIT(png_ptr, info_ptr, length); ++#endif ++ ++#ifdef PNG_READ_sRGB_SUPPORTED ++ else if (chunk_name == png_sRGB) ++ png_handle_sRGB(png_ptr, info_ptr, length); ++#endif ++ ++#ifdef PNG_READ_iCCP_SUPPORTED ++ else if (chunk_name == png_iCCP) ++ png_handle_iCCP(png_ptr, info_ptr, length); ++#endif ++ ++#ifdef PNG_READ_sPLT_SUPPORTED ++ else if (chunk_name == png_sPLT) ++ png_handle_sPLT(png_ptr, info_ptr, length); ++#endif ++ ++#ifdef PNG_READ_tEXt_SUPPORTED ++ else if (chunk_name == png_tEXt) ++ png_handle_tEXt(png_ptr, info_ptr, length); ++#endif ++ ++#ifdef PNG_READ_tIME_SUPPORTED ++ else if (chunk_name == png_tIME) ++ png_handle_tIME(png_ptr, info_ptr, length); ++#endif ++ ++#ifdef PNG_READ_tRNS_SUPPORTED ++ else if (chunk_name == png_tRNS) ++ png_handle_tRNS(png_ptr, info_ptr, length); ++#endif ++ ++#ifdef PNG_READ_zTXt_SUPPORTED ++ else if (chunk_name == png_zTXt) ++ png_handle_zTXt(png_ptr, info_ptr, length); ++#endif ++ ++#ifdef PNG_READ_iTXt_SUPPORTED ++ else if (chunk_name == png_iTXt) ++ png_handle_iTXt(png_ptr, info_ptr, length); ++#endif ++ ++ else ++ png_handle_unknown(png_ptr, info_ptr, length, ++ PNG_HANDLE_CHUNK_AS_DEFAULT); ++ } ++} ++#endif /* SEQUENTIAL_READ */ ++ ++/* Optional call to update the users info_ptr structure */ ++void PNGAPI ++png_read_update_info(png_structrp png_ptr, png_inforp info_ptr) ++{ ++ png_debug(1, "in png_read_update_info"); ++ ++ if (png_ptr != NULL) ++ { ++ if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0) ++ { ++ png_read_start_row(png_ptr); ++ ++# ifdef PNG_READ_TRANSFORMS_SUPPORTED ++ png_read_transform_info(png_ptr, info_ptr); ++# else ++ PNG_UNUSED(info_ptr) ++# endif ++ } ++ ++ /* New in 1.6.0 this avoids the bug of doing the initializations twice */ ++ else ++ png_app_error(png_ptr, ++ "png_read_update_info/png_start_read_image: duplicate call"); ++ } ++} ++ ++#ifdef PNG_SEQUENTIAL_READ_SUPPORTED ++/* Initialize palette, background, etc, after transformations ++ * are set, but before any reading takes place. This allows ++ * the user to obtain a gamma-corrected palette, for example. ++ * If the user doesn't call this, we will do it ourselves. ++ */ ++void PNGAPI ++png_start_read_image(png_structrp png_ptr) ++{ ++ png_debug(1, "in png_start_read_image"); ++ ++ if (png_ptr != NULL) ++ { ++ if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0) ++ png_read_start_row(png_ptr); ++ ++ /* New in 1.6.0 this avoids the bug of doing the initializations twice */ ++ else ++ png_app_error(png_ptr, ++ "png_start_read_image/png_read_update_info: duplicate call"); ++ } ++} ++#endif /* SEQUENTIAL_READ */ ++ ++#ifdef PNG_SEQUENTIAL_READ_SUPPORTED ++#ifdef PNG_MNG_FEATURES_SUPPORTED ++/* Undoes intrapixel differencing, ++ * NOTE: this is apparently only supported in the 'sequential' reader. ++ */ ++static void ++png_do_read_intrapixel(png_row_infop row_info, png_bytep row) ++{ ++ png_debug(1, "in png_do_read_intrapixel"); ++ ++ if ( ++ (row_info->color_type & PNG_COLOR_MASK_COLOR) != 0) ++ { ++ int bytes_per_pixel; ++ png_uint_32 row_width = row_info->width; ++ ++ if (row_info->bit_depth == 8) ++ { ++ png_bytep rp; ++ png_uint_32 i; ++ ++ if (row_info->color_type == PNG_COLOR_TYPE_RGB) ++ bytes_per_pixel = 3; ++ ++ else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) ++ bytes_per_pixel = 4; ++ ++ else ++ return; ++ ++ for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) ++ { ++ *(rp) = (png_byte)((256 + *rp + *(rp + 1)) & 0xff); ++ *(rp+2) = (png_byte)((256 + *(rp + 2) + *(rp + 1)) & 0xff); ++ } ++ } ++ else if (row_info->bit_depth == 16) ++ { ++ png_bytep rp; ++ png_uint_32 i; ++ ++ if (row_info->color_type == PNG_COLOR_TYPE_RGB) ++ bytes_per_pixel = 6; ++ ++ else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) ++ bytes_per_pixel = 8; ++ ++ else ++ return; ++ ++ for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) ++ { ++ png_uint_32 s0 = (png_uint_32)(*(rp ) << 8) | *(rp + 1); ++ png_uint_32 s1 = (png_uint_32)(*(rp + 2) << 8) | *(rp + 3); ++ png_uint_32 s2 = (png_uint_32)(*(rp + 4) << 8) | *(rp + 5); ++ png_uint_32 red = (s0 + s1 + 65536) & 0xffff; ++ png_uint_32 blue = (s2 + s1 + 65536) & 0xffff; ++ *(rp ) = (png_byte)((red >> 8) & 0xff); ++ *(rp + 1) = (png_byte)(red & 0xff); ++ *(rp + 4) = (png_byte)((blue >> 8) & 0xff); ++ *(rp + 5) = (png_byte)(blue & 0xff); ++ } ++ } ++ } ++} ++#endif /* MNG_FEATURES */ ++ ++void PNGAPI ++png_read_row(png_structrp png_ptr, png_bytep row, png_bytep dsp_row) ++{ ++ png_row_info row_info; ++ ++ if (png_ptr == NULL) ++ return; ++ ++ png_debug2(1, "in png_read_row (row %lu, pass %d)", ++ (unsigned long)png_ptr->row_number, png_ptr->pass); ++ ++ /* png_read_start_row sets the information (in particular iwidth) for this ++ * interlace pass. ++ */ ++ if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0) ++ png_read_start_row(png_ptr); ++ ++ /* 1.5.6: row_info moved out of png_struct to a local here. */ ++ row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */ ++ row_info.color_type = png_ptr->color_type; ++ row_info.bit_depth = png_ptr->bit_depth; ++ row_info.channels = png_ptr->channels; ++ row_info.pixel_depth = png_ptr->pixel_depth; ++ row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width); ++ ++#ifdef PNG_WARNINGS_SUPPORTED ++ if (png_ptr->row_number == 0 && png_ptr->pass == 0) ++ { ++ /* Check for transforms that have been set but were defined out */ ++#if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED) ++ if ((png_ptr->transformations & PNG_INVERT_MONO) != 0) ++ png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined"); ++#endif ++ ++#if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED) ++ if ((png_ptr->transformations & PNG_FILLER) != 0) ++ png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined"); ++#endif ++ ++#if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \ ++ !defined(PNG_READ_PACKSWAP_SUPPORTED) ++ if ((png_ptr->transformations & PNG_PACKSWAP) != 0) ++ png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined"); ++#endif ++ ++#if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED) ++ if ((png_ptr->transformations & PNG_PACK) != 0) ++ png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined"); ++#endif ++ ++#if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED) ++ if ((png_ptr->transformations & PNG_SHIFT) != 0) ++ png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined"); ++#endif ++ ++#if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED) ++ if ((png_ptr->transformations & PNG_BGR) != 0) ++ png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined"); ++#endif ++ ++#if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED) ++ if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0) ++ png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined"); ++#endif ++ } ++#endif /* WARNINGS */ ++ ++#ifdef PNG_READ_INTERLACING_SUPPORTED ++ /* If interlaced and we do not need a new row, combine row and return. ++ * Notice that the pixels we have from previous rows have been transformed ++ * already; we can only combine like with like (transformed or ++ * untransformed) and, because of the libpng API for interlaced images, this ++ * means we must transform before de-interlacing. ++ */ ++ if (png_ptr->interlaced != 0 && ++ (png_ptr->transformations & PNG_INTERLACE) != 0) ++ { ++ switch (png_ptr->pass) ++ { ++ case 0: ++ if (png_ptr->row_number & 0x07) ++ { ++ if (dsp_row != NULL) ++ png_combine_row(png_ptr, dsp_row, 1/*display*/); ++ png_read_finish_row(png_ptr); ++ return; ++ } ++ break; ++ ++ case 1: ++ if ((png_ptr->row_number & 0x07) || png_ptr->width < 5) ++ { ++ if (dsp_row != NULL) ++ png_combine_row(png_ptr, dsp_row, 1/*display*/); ++ ++ png_read_finish_row(png_ptr); ++ return; ++ } ++ break; ++ ++ case 2: ++ if ((png_ptr->row_number & 0x07) != 4) ++ { ++ if (dsp_row != NULL && (png_ptr->row_number & 4)) ++ png_combine_row(png_ptr, dsp_row, 1/*display*/); ++ ++ png_read_finish_row(png_ptr); ++ return; ++ } ++ break; ++ ++ case 3: ++ if ((png_ptr->row_number & 3) || png_ptr->width < 3) ++ { ++ if (dsp_row != NULL) ++ png_combine_row(png_ptr, dsp_row, 1/*display*/); ++ ++ png_read_finish_row(png_ptr); ++ return; ++ } ++ break; ++ ++ case 4: ++ if ((png_ptr->row_number & 3) != 2) ++ { ++ if (dsp_row != NULL && (png_ptr->row_number & 2)) ++ png_combine_row(png_ptr, dsp_row, 1/*display*/); ++ ++ png_read_finish_row(png_ptr); ++ return; ++ } ++ break; ++ ++ case 5: ++ if ((png_ptr->row_number & 1) || png_ptr->width < 2) ++ { ++ if (dsp_row != NULL) ++ png_combine_row(png_ptr, dsp_row, 1/*display*/); ++ ++ png_read_finish_row(png_ptr); ++ return; ++ } ++ break; ++ ++ default: ++ case 6: ++ if ((png_ptr->row_number & 1) == 0) ++ { ++ png_read_finish_row(png_ptr); ++ return; ++ } ++ break; ++ } ++ } ++#endif ++ ++ if ((png_ptr->mode & PNG_HAVE_IDAT) == 0) ++ png_error(png_ptr, "Invalid attempt to read row data"); ++ ++ /* Fill the row with IDAT data: */ ++ png_ptr->row_buf[0]=255; /* to force error if no data was found */ ++ png_read_IDAT_data(png_ptr, png_ptr->row_buf, row_info.rowbytes + 1); ++ ++ if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE) ++ { ++ if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST) ++ png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1, ++ png_ptr->prev_row + 1, png_ptr->row_buf[0]); ++ else ++ png_error(png_ptr, "bad adaptive filter value"); ++ } ++ ++ /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before ++ * 1.5.6, while the buffer really is this big in current versions of libpng ++ * it may not be in the future, so this was changed just to copy the ++ * interlaced count: ++ */ ++ memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1); ++ ++#ifdef PNG_MNG_FEATURES_SUPPORTED ++ if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 && ++ (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING)) ++ { ++ /* Intrapixel differencing */ ++ png_do_read_intrapixel(&row_info, png_ptr->row_buf + 1); ++ } ++#endif ++ ++#ifdef PNG_READ_TRANSFORMS_SUPPORTED ++ if (png_ptr->transformations) ++ png_do_read_transformations(png_ptr, &row_info); ++#endif ++ ++ /* The transformed pixel depth should match the depth now in row_info. */ ++ if (png_ptr->transformed_pixel_depth == 0) ++ { ++ png_ptr->transformed_pixel_depth = row_info.pixel_depth; ++ if (row_info.pixel_depth > png_ptr->maximum_pixel_depth) ++ png_error(png_ptr, "sequential row overflow"); ++ } ++ ++ else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth) ++ png_error(png_ptr, "internal sequential row size calculation error"); ++ ++#ifdef PNG_READ_INTERLACING_SUPPORTED ++ /* Expand interlaced rows to full size */ ++ if (png_ptr->interlaced != 0 && ++ (png_ptr->transformations & PNG_INTERLACE) != 0) ++ { ++ if (png_ptr->pass < 6) ++ png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass, ++ png_ptr->transformations); ++ ++ if (dsp_row != NULL) ++ png_combine_row(png_ptr, dsp_row, 1/*display*/); ++ ++ if (row != NULL) ++ png_combine_row(png_ptr, row, 0/*row*/); ++ } ++ ++ else ++#endif ++ { ++ if (row != NULL) ++ png_combine_row(png_ptr, row, -1/*ignored*/); ++ ++ if (dsp_row != NULL) ++ png_combine_row(png_ptr, dsp_row, -1/*ignored*/); ++ } ++ png_read_finish_row(png_ptr); ++ ++ if (png_ptr->read_row_fn != NULL) ++ (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass); ++ ++} ++#endif /* SEQUENTIAL_READ */ ++ ++#ifdef PNG_SEQUENTIAL_READ_SUPPORTED ++/* Read one or more rows of image data. If the image is interlaced, ++ * and png_set_interlace_handling() has been called, the rows need to ++ * contain the contents of the rows from the previous pass. If the ++ * image has alpha or transparency, and png_handle_alpha()[*] has been ++ * called, the rows contents must be initialized to the contents of the ++ * screen. ++ * ++ * "row" holds the actual image, and pixels are placed in it ++ * as they arrive. If the image is displayed after each pass, it will ++ * appear to "sparkle" in. "display_row" can be used to display a ++ * "chunky" progressive image, with finer detail added as it becomes ++ * available. If you do not want this "chunky" display, you may pass ++ * NULL for display_row. If you do not want the sparkle display, and ++ * you have not called png_handle_alpha(), you may pass NULL for rows. ++ * If you have called png_handle_alpha(), and the image has either an ++ * alpha channel or a transparency chunk, you must provide a buffer for ++ * rows. In this case, you do not have to provide a display_row buffer ++ * also, but you may. If the image is not interlaced, or if you have ++ * not called png_set_interlace_handling(), the display_row buffer will ++ * be ignored, so pass NULL to it. ++ * ++ * [*] png_handle_alpha() does not exist yet, as of this version of libpng ++ */ ++ ++void PNGAPI ++png_read_rows(png_structrp png_ptr, png_bytepp row, ++ png_bytepp display_row, png_uint_32 num_rows) ++{ ++ png_uint_32 i; ++ png_bytepp rp; ++ png_bytepp dp; ++ ++ png_debug(1, "in png_read_rows"); ++ ++ if (png_ptr == NULL) ++ return; ++ ++ rp = row; ++ dp = display_row; ++ if (rp != NULL && dp != NULL) ++ for (i = 0; i < num_rows; i++) ++ { ++ png_bytep rptr = *rp++; ++ png_bytep dptr = *dp++; ++ ++ png_read_row(png_ptr, rptr, dptr); ++ } ++ ++ else if (rp != NULL) ++ for (i = 0; i < num_rows; i++) ++ { ++ png_bytep rptr = *rp; ++ png_read_row(png_ptr, rptr, NULL); ++ rp++; ++ } ++ ++ else if (dp != NULL) ++ for (i = 0; i < num_rows; i++) ++ { ++ png_bytep dptr = *dp; ++ png_read_row(png_ptr, NULL, dptr); ++ dp++; ++ } ++} ++#endif /* SEQUENTIAL_READ */ ++ ++#ifdef PNG_SEQUENTIAL_READ_SUPPORTED ++/* Read the entire image. If the image has an alpha channel or a tRNS ++ * chunk, and you have called png_handle_alpha()[*], you will need to ++ * initialize the image to the current image that PNG will be overlaying. ++ * We set the num_rows again here, in case it was incorrectly set in ++ * png_read_start_row() by a call to png_read_update_info() or ++ * png_start_read_image() if png_set_interlace_handling() wasn't called ++ * prior to either of these functions like it should have been. You can ++ * only call this function once. If you desire to have an image for ++ * each pass of a interlaced image, use png_read_rows() instead. ++ * ++ * [*] png_handle_alpha() does not exist yet, as of this version of libpng ++ */ ++void PNGAPI ++png_read_image(png_structrp png_ptr, png_bytepp image) ++{ ++ png_uint_32 i, image_height; ++ int pass, j; ++ png_bytepp rp; ++ ++ png_debug(1, "in png_read_image"); ++ ++ if (png_ptr == NULL) ++ return; ++ ++#ifdef PNG_READ_INTERLACING_SUPPORTED ++ if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0) ++ { ++ pass = png_set_interlace_handling(png_ptr); ++ /* And make sure transforms are initialized. */ ++ png_start_read_image(png_ptr); ++ } ++ else ++ { ++ if (png_ptr->interlaced != 0 && ++ (png_ptr->transformations & PNG_INTERLACE) == 0) ++ { ++ /* Caller called png_start_read_image or png_read_update_info without ++ * first turning on the PNG_INTERLACE transform. We can fix this here, ++ * but the caller should do it! ++ */ ++ png_warning(png_ptr, "Interlace handling should be turned on when " ++ "using png_read_image"); ++ /* Make sure this is set correctly */ ++ png_ptr->num_rows = png_ptr->height; ++ } ++ ++ /* Obtain the pass number, which also turns on the PNG_INTERLACE flag in ++ * the above error case. ++ */ ++ pass = png_set_interlace_handling(png_ptr); ++ } ++#else ++ if (png_ptr->interlaced) ++ png_error(png_ptr, ++ "Cannot read interlaced image -- interlace handler disabled"); ++ ++ pass = 1; ++#endif ++ ++ image_height=png_ptr->height; ++ ++ for (j = 0; j < pass; j++) ++ { ++ rp = image; ++ for (i = 0; i < image_height; i++) ++ { ++ png_read_row(png_ptr, *rp, NULL); ++ rp++; ++ } ++ } ++} ++#endif /* SEQUENTIAL_READ */ ++ ++#ifdef PNG_SEQUENTIAL_READ_SUPPORTED ++/* Read the end of the PNG file. Will not read past the end of the ++ * file, will verify the end is accurate, and will read any comments ++ * or time information at the end of the file, if info is not NULL. ++ */ ++void PNGAPI ++png_read_end(png_structrp png_ptr, png_inforp info_ptr) ++{ ++#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED ++ int keep; ++#endif ++ ++ png_debug(1, "in png_read_end"); ++ ++ if (png_ptr == NULL) ++ return; ++ ++ /* If png_read_end is called in the middle of reading the rows there may ++ * still be pending IDAT data and an owned zstream. Deal with this here. ++ */ ++#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED ++ if (png_chunk_unknown_handling(png_ptr, png_IDAT) == 0) ++#endif ++ png_read_finish_IDAT(png_ptr); ++ ++#ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED ++ /* Report invalid palette index; added at libng-1.5.10 */ ++ if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && ++ png_ptr->num_palette_max > png_ptr->num_palette) ++ png_benign_error(png_ptr, "Read palette index exceeding num_palette"); ++#endif ++ ++ do ++ { ++ png_uint_32 length = png_read_chunk_header(png_ptr); ++ png_uint_32 chunk_name = png_ptr->chunk_name; ++ ++ if (chunk_name != png_IDAT) ++ png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT; ++ ++ if (chunk_name == png_IEND) ++ png_handle_IEND(png_ptr, info_ptr, length); ++ ++ else if (chunk_name == png_IHDR) ++ png_handle_IHDR(png_ptr, info_ptr, length); ++ ++ else if (info_ptr == NULL) ++ png_crc_finish(png_ptr, length); ++ ++#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED ++ else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0) ++ { ++ if (chunk_name == png_IDAT) ++ { ++ if ((length > 0 && !(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED)) ++ || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) != 0) ++ png_benign_error(png_ptr, ".Too many IDATs found"); ++ } ++ png_handle_unknown(png_ptr, info_ptr, length, keep); ++ if (chunk_name == png_PLTE) ++ png_ptr->mode |= PNG_HAVE_PLTE; ++ } ++#endif ++ ++ else if (chunk_name == png_IDAT) ++ { ++ /* Zero length IDATs are legal after the last IDAT has been ++ * read, but not after other chunks have been read. 1.6 does not ++ * always read all the deflate data; specifically it cannot be relied ++ * upon to read the Adler32 at the end. If it doesn't ignore IDAT ++ * chunks which are longer than zero as well: ++ */ ++ if ((length > 0 && !(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED)) ++ || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) != 0) ++ png_benign_error(png_ptr, "..Too many IDATs found"); ++ ++ png_crc_finish(png_ptr, length); ++ } ++ else if (chunk_name == png_PLTE) ++ png_handle_PLTE(png_ptr, info_ptr, length); ++ ++#ifdef PNG_READ_bKGD_SUPPORTED ++ else if (chunk_name == png_bKGD) ++ png_handle_bKGD(png_ptr, info_ptr, length); ++#endif ++ ++#ifdef PNG_READ_cHRM_SUPPORTED ++ else if (chunk_name == png_cHRM) ++ png_handle_cHRM(png_ptr, info_ptr, length); ++#endif ++ ++#ifdef PNG_READ_eXIf_SUPPORTED ++ else if (chunk_name == png_eXIf) ++ png_handle_eXIf(png_ptr, info_ptr, length); ++#endif ++ ++#ifdef PNG_READ_gAMA_SUPPORTED ++ else if (chunk_name == png_gAMA) ++ png_handle_gAMA(png_ptr, info_ptr, length); ++#endif ++ ++#ifdef PNG_READ_hIST_SUPPORTED ++ else if (chunk_name == png_hIST) ++ png_handle_hIST(png_ptr, info_ptr, length); ++#endif ++ ++#ifdef PNG_READ_oFFs_SUPPORTED ++ else if (chunk_name == png_oFFs) ++ png_handle_oFFs(png_ptr, info_ptr, length); ++#endif ++ ++#ifdef PNG_READ_pCAL_SUPPORTED ++ else if (chunk_name == png_pCAL) ++ png_handle_pCAL(png_ptr, info_ptr, length); ++#endif ++ ++#ifdef PNG_READ_sCAL_SUPPORTED ++ else if (chunk_name == png_sCAL) ++ png_handle_sCAL(png_ptr, info_ptr, length); ++#endif ++ ++#ifdef PNG_READ_pHYs_SUPPORTED ++ else if (chunk_name == png_pHYs) ++ png_handle_pHYs(png_ptr, info_ptr, length); ++#endif ++ ++#ifdef PNG_READ_sBIT_SUPPORTED ++ else if (chunk_name == png_sBIT) ++ png_handle_sBIT(png_ptr, info_ptr, length); ++#endif ++ ++#ifdef PNG_READ_sRGB_SUPPORTED ++ else if (chunk_name == png_sRGB) ++ png_handle_sRGB(png_ptr, info_ptr, length); ++#endif ++ ++#ifdef PNG_READ_iCCP_SUPPORTED ++ else if (chunk_name == png_iCCP) ++ png_handle_iCCP(png_ptr, info_ptr, length); ++#endif ++ ++#ifdef PNG_READ_sPLT_SUPPORTED ++ else if (chunk_name == png_sPLT) ++ png_handle_sPLT(png_ptr, info_ptr, length); ++#endif ++ ++#ifdef PNG_READ_tEXt_SUPPORTED ++ else if (chunk_name == png_tEXt) ++ png_handle_tEXt(png_ptr, info_ptr, length); ++#endif ++ ++#ifdef PNG_READ_tIME_SUPPORTED ++ else if (chunk_name == png_tIME) ++ png_handle_tIME(png_ptr, info_ptr, length); ++#endif ++ ++#ifdef PNG_READ_tRNS_SUPPORTED ++ else if (chunk_name == png_tRNS) ++ png_handle_tRNS(png_ptr, info_ptr, length); ++#endif ++ ++#ifdef PNG_READ_zTXt_SUPPORTED ++ else if (chunk_name == png_zTXt) ++ png_handle_zTXt(png_ptr, info_ptr, length); ++#endif ++ ++#ifdef PNG_READ_iTXt_SUPPORTED ++ else if (chunk_name == png_iTXt) ++ png_handle_iTXt(png_ptr, info_ptr, length); ++#endif ++ ++ else ++ png_handle_unknown(png_ptr, info_ptr, length, ++ PNG_HANDLE_CHUNK_AS_DEFAULT); ++ } while ((png_ptr->mode & PNG_HAVE_IEND) == 0); ++} ++#endif /* SEQUENTIAL_READ */ ++ ++/* Free all memory used in the read struct */ ++static void ++png_read_destroy(png_structrp png_ptr) ++{ ++ png_debug(1, "in png_read_destroy"); ++ ++#ifdef PNG_READ_GAMMA_SUPPORTED ++ png_destroy_gamma_table(png_ptr); ++#endif ++ ++ png_free(png_ptr, png_ptr->big_row_buf); ++ png_ptr->big_row_buf = NULL; ++ png_free(png_ptr, png_ptr->big_prev_row); ++ png_ptr->big_prev_row = NULL; ++ png_free(png_ptr, png_ptr->read_buffer); ++ png_ptr->read_buffer = NULL; ++ ++#ifdef PNG_READ_QUANTIZE_SUPPORTED ++ png_free(png_ptr, png_ptr->palette_lookup); ++ png_ptr->palette_lookup = NULL; ++ png_free(png_ptr, png_ptr->quantize_index); ++ png_ptr->quantize_index = NULL; ++#endif ++ ++ if ((png_ptr->free_me & PNG_FREE_PLTE) != 0) ++ { ++ png_zfree(png_ptr, png_ptr->palette); ++ png_ptr->palette = NULL; ++ } ++ png_ptr->free_me &= ~PNG_FREE_PLTE; ++ ++#if defined(PNG_tRNS_SUPPORTED) || \ ++ defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) ++ if ((png_ptr->free_me & PNG_FREE_TRNS) != 0) ++ { ++ png_free(png_ptr, png_ptr->trans_alpha); ++ png_ptr->trans_alpha = NULL; ++ } ++ png_ptr->free_me &= ~PNG_FREE_TRNS; ++#endif ++ ++ inflateEnd(&png_ptr->zstream); ++ ++#ifdef PNG_PROGRESSIVE_READ_SUPPORTED ++ png_free(png_ptr, png_ptr->save_buffer); ++ png_ptr->save_buffer = NULL; ++#endif ++ ++#if defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED) && \ ++ defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) ++ png_free(png_ptr, png_ptr->unknown_chunk.data); ++ png_ptr->unknown_chunk.data = NULL; ++#endif ++ ++#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED ++ png_free(png_ptr, png_ptr->chunk_list); ++ png_ptr->chunk_list = NULL; ++#endif ++ ++#if defined(PNG_READ_EXPAND_SUPPORTED) && \ ++ defined(PNG_ARM_NEON_IMPLEMENTATION) ++ png_free(png_ptr, png_ptr->riffled_palette); ++ png_ptr->riffled_palette = NULL; ++#endif ++ ++ /* NOTE: the 'setjmp' buffer may still be allocated and the memory and error ++ * callbacks are still set at this point. They are required to complete the ++ * destruction of the png_struct itself. ++ */ ++} ++ ++/* Free all memory used by the read */ ++void PNGAPI ++png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr, ++ png_infopp end_info_ptr_ptr) ++{ ++ png_structrp png_ptr = NULL; ++ ++ png_debug(1, "in png_destroy_read_struct"); ++ ++ if (png_ptr_ptr != NULL) ++ png_ptr = *png_ptr_ptr; ++ ++ if (png_ptr == NULL) ++ return; ++ ++ /* libpng 1.6.0: use the API to destroy info structs to ensure consistent ++ * behavior. Prior to 1.6.0 libpng did extra 'info' destruction in this API. ++ * The extra was, apparently, unnecessary yet this hides memory leak bugs. ++ */ ++ png_destroy_info_struct(png_ptr, end_info_ptr_ptr); ++ png_destroy_info_struct(png_ptr, info_ptr_ptr); ++ ++ *png_ptr_ptr = NULL; ++ png_read_destroy(png_ptr); ++ png_destroy_png_struct(png_ptr); ++} ++ ++void PNGAPI ++png_set_read_status_fn(png_structrp png_ptr, png_read_status_ptr read_row_fn) ++{ ++ if (png_ptr == NULL) ++ return; ++ ++ png_ptr->read_row_fn = read_row_fn; ++} ++ ++ ++#ifdef PNG_SEQUENTIAL_READ_SUPPORTED ++#ifdef PNG_INFO_IMAGE_SUPPORTED ++void PNGAPI ++png_read_png(png_structrp png_ptr, png_inforp info_ptr, ++ int transforms, voidp params) ++{ ++ if (png_ptr == NULL || info_ptr == NULL) ++ return; ++ ++ /* png_read_info() gives us all of the information from the ++ * PNG file before the first IDAT (image data chunk). ++ */ ++ png_read_info(png_ptr, info_ptr); ++ if (info_ptr->height > PNG_UINT_32_MAX/(sizeof (png_bytep))) ++ png_error(png_ptr, "Image is too high to process with png_read_png()"); ++ ++ /* -------------- image transformations start here ------------------- */ ++ /* libpng 1.6.10: add code to cause a png_app_error if a selected TRANSFORM ++ * is not implemented. This will only happen in de-configured (non-default) ++ * libpng builds. The results can be unexpected - png_read_png may return ++ * short or mal-formed rows because the transform is skipped. ++ */ ++ ++ /* Tell libpng to strip 16-bit/color files down to 8 bits per color. ++ */ ++ if ((transforms & PNG_TRANSFORM_SCALE_16) != 0) ++ /* Added at libpng-1.5.4. "strip_16" produces the same result that it ++ * did in earlier versions, while "scale_16" is now more accurate. ++ */ ++#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED ++ png_set_scale_16(png_ptr); ++#else ++ png_app_error(png_ptr, "PNG_TRANSFORM_SCALE_16 not supported"); ++#endif ++ ++ /* If both SCALE and STRIP are required pngrtran will effectively cancel the ++ * latter by doing SCALE first. This is ok and allows apps not to check for ++ * which is supported to get the right answer. ++ */ ++ if ((transforms & PNG_TRANSFORM_STRIP_16) != 0) ++#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED ++ png_set_strip_16(png_ptr); ++#else ++ png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_16 not supported"); ++#endif ++ ++ /* Strip alpha bytes from the input data without combining with ++ * the background (not recommended). ++ */ ++ if ((transforms & PNG_TRANSFORM_STRIP_ALPHA) != 0) ++#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED ++ png_set_strip_alpha(png_ptr); ++#else ++ png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_ALPHA not supported"); ++#endif ++ ++ /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single ++ * byte into separate bytes (useful for paletted and grayscale images). ++ */ ++ if ((transforms & PNG_TRANSFORM_PACKING) != 0) ++#ifdef PNG_READ_PACK_SUPPORTED ++ png_set_packing(png_ptr); ++#else ++ png_app_error(png_ptr, "PNG_TRANSFORM_PACKING not supported"); ++#endif ++ ++ /* Change the order of packed pixels to least significant bit first ++ * (not useful if you are using png_set_packing). ++ */ ++ if ((transforms & PNG_TRANSFORM_PACKSWAP) != 0) ++#ifdef PNG_READ_PACKSWAP_SUPPORTED ++ png_set_packswap(png_ptr); ++#else ++ png_app_error(png_ptr, "PNG_TRANSFORM_PACKSWAP not supported"); ++#endif ++ ++ /* Expand paletted colors into true RGB triplets ++ * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel ++ * Expand paletted or RGB images with transparency to full alpha ++ * channels so the data will be available as RGBA quartets. ++ */ ++ if ((transforms & PNG_TRANSFORM_EXPAND) != 0) ++#ifdef PNG_READ_EXPAND_SUPPORTED ++ png_set_expand(png_ptr); ++#else ++ png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND not supported"); ++#endif ++ ++ /* We don't handle background color or gamma transformation or quantizing. ++ */ ++ ++ /* Invert monochrome files to have 0 as white and 1 as black ++ */ ++ if ((transforms & PNG_TRANSFORM_INVERT_MONO) != 0) ++#ifdef PNG_READ_INVERT_SUPPORTED ++ png_set_invert_mono(png_ptr); ++#else ++ png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_MONO not supported"); ++#endif ++ ++ /* If you want to shift the pixel values from the range [0,255] or ++ * [0,65535] to the original [0,7] or [0,31], or whatever range the ++ * colors were originally in: ++ */ ++ if ((transforms & PNG_TRANSFORM_SHIFT) != 0) ++#ifdef PNG_READ_SHIFT_SUPPORTED ++ if ((info_ptr->valid & PNG_INFO_sBIT) != 0) ++ png_set_shift(png_ptr, &info_ptr->sig_bit); ++#else ++ png_app_error(png_ptr, "PNG_TRANSFORM_SHIFT not supported"); ++#endif ++ ++ /* Flip the RGB pixels to BGR (or RGBA to BGRA) */ ++ if ((transforms & PNG_TRANSFORM_BGR) != 0) ++#ifdef PNG_READ_BGR_SUPPORTED ++ png_set_bgr(png_ptr); ++#else ++ png_app_error(png_ptr, "PNG_TRANSFORM_BGR not supported"); ++#endif ++ ++ /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */ ++ if ((transforms & PNG_TRANSFORM_SWAP_ALPHA) != 0) ++#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED ++ png_set_swap_alpha(png_ptr); ++#else ++ png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ALPHA not supported"); ++#endif ++ ++ /* Swap bytes of 16-bit files to least significant byte first */ ++ if ((transforms & PNG_TRANSFORM_SWAP_ENDIAN) != 0) ++#ifdef PNG_READ_SWAP_SUPPORTED ++ png_set_swap(png_ptr); ++#else ++ png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported"); ++#endif ++ ++/* Added at libpng-1.2.41 */ ++ /* Invert the alpha channel from opacity to transparency */ ++ if ((transforms & PNG_TRANSFORM_INVERT_ALPHA) != 0) ++#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED ++ png_set_invert_alpha(png_ptr); ++#else ++ png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_ALPHA not supported"); ++#endif ++ ++/* Added at libpng-1.2.41 */ ++ /* Expand grayscale image to RGB */ ++ if ((transforms & PNG_TRANSFORM_GRAY_TO_RGB) != 0) ++#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED ++ png_set_gray_to_rgb(png_ptr); ++#else ++ png_app_error(png_ptr, "PNG_TRANSFORM_GRAY_TO_RGB not supported"); ++#endif ++ ++/* Added at libpng-1.5.4 */ ++ if ((transforms & PNG_TRANSFORM_EXPAND_16) != 0) ++#ifdef PNG_READ_EXPAND_16_SUPPORTED ++ png_set_expand_16(png_ptr); ++#else ++ png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND_16 not supported"); ++#endif ++ ++ /* We don't handle adding filler bytes */ ++ ++ /* We use png_read_image and rely on that for interlace handling, but we also ++ * call png_read_update_info therefore must turn on interlace handling now: ++ */ ++ (void)png_set_interlace_handling(png_ptr); ++ ++ /* Optional call to gamma correct and add the background to the palette ++ * and update info structure. REQUIRED if you are expecting libpng to ++ * update the palette for you (i.e., you selected such a transform above). ++ */ ++ png_read_update_info(png_ptr, info_ptr); ++ ++ /* -------------- image transformations end here ------------------- */ ++ ++ png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0); ++ if (info_ptr->row_pointers == NULL) ++ { ++ png_uint_32 iptr; ++ ++ info_ptr->row_pointers = png_voidcast(png_bytepp, png_malloc(png_ptr, ++ info_ptr->height * (sizeof (png_bytep)))); ++ ++ for (iptr=0; iptrheight; iptr++) ++ info_ptr->row_pointers[iptr] = NULL; ++ ++ info_ptr->free_me |= PNG_FREE_ROWS; ++ ++ for (iptr = 0; iptr < info_ptr->height; iptr++) ++ info_ptr->row_pointers[iptr] = png_voidcast(png_bytep, ++ png_malloc(png_ptr, info_ptr->rowbytes)); ++ } ++ ++ png_read_image(png_ptr, info_ptr->row_pointers); ++ info_ptr->valid |= PNG_INFO_IDAT; ++ ++ /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */ ++ png_read_end(png_ptr, info_ptr); ++ ++ PNG_UNUSED(params) ++} ++#endif /* INFO_IMAGE */ ++#endif /* SEQUENTIAL_READ */ ++ ++#ifdef PNG_SIMPLIFIED_READ_SUPPORTED ++/* SIMPLIFIED READ ++ * ++ * This code currently relies on the sequential reader, though it could easily ++ * be made to work with the progressive one. ++ */ ++/* Arguments to png_image_finish_read: */ ++ ++/* Encoding of PNG data (used by the color-map code) */ ++# define P_NOTSET 0 /* File encoding not yet known */ ++# define P_sRGB 1 /* 8-bit encoded to sRGB gamma */ ++# define P_LINEAR 2 /* 16-bit linear: not encoded, NOT pre-multiplied! */ ++# define P_FILE 3 /* 8-bit encoded to file gamma, not sRGB or linear */ ++# define P_LINEAR8 4 /* 8-bit linear: only from a file value */ ++ ++/* Color-map processing: after libpng has run on the PNG image further ++ * processing may be needed to convert the data to color-map indices. ++ */ ++#define PNG_CMAP_NONE 0 ++#define PNG_CMAP_GA 1 /* Process GA data to a color-map with alpha */ ++#define PNG_CMAP_TRANS 2 /* Process GA data to a background index */ ++#define PNG_CMAP_RGB 3 /* Process RGB data */ ++#define PNG_CMAP_RGB_ALPHA 4 /* Process RGBA data */ ++ ++/* The following document where the background is for each processing case. */ ++#define PNG_CMAP_NONE_BACKGROUND 256 ++#define PNG_CMAP_GA_BACKGROUND 231 ++#define PNG_CMAP_TRANS_BACKGROUND 254 ++#define PNG_CMAP_RGB_BACKGROUND 256 ++#define PNG_CMAP_RGB_ALPHA_BACKGROUND 216 ++ ++typedef struct ++{ ++ /* Arguments: */ ++ png_imagep image; ++ png_voidp buffer; ++ png_int_32 row_stride; ++ png_voidp colormap; ++ png_const_colorp background; ++ /* Local variables: */ ++ png_voidp local_row; ++ png_voidp first_row; ++ ptrdiff_t row_bytes; /* step between rows */ ++ int file_encoding; /* E_ values above */ ++ png_fixed_point gamma_to_linear; /* For P_FILE, reciprocal of gamma */ ++ int colormap_processing; /* PNG_CMAP_ values above */ ++} png_image_read_control; ++ ++/* Do all the *safe* initialization - 'safe' means that png_error won't be ++ * called, so setting up the jmp_buf is not required. This means that anything ++ * called from here must *not* call png_malloc - it has to call png_malloc_warn ++ * instead so that control is returned safely back to this routine. ++ */ ++static int ++png_image_read_init(png_imagep image) ++{ ++ if (image->opaque == NULL) ++ { ++ png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, image, ++ png_safe_error, png_safe_warning); ++ ++ /* And set the rest of the structure to NULL to ensure that the various ++ * fields are consistent. ++ */ ++ memset(image, 0, (sizeof *image)); ++ image->version = PNG_IMAGE_VERSION; ++ ++ if (png_ptr != NULL) ++ { ++ png_infop info_ptr = png_create_info_struct(png_ptr); ++ ++ if (info_ptr != NULL) ++ { ++ png_controlp control = png_voidcast(png_controlp, ++ png_malloc_warn(png_ptr, (sizeof *control))); ++ ++ if (control != NULL) ++ { ++ memset(control, 0, (sizeof *control)); ++ ++ control->png_ptr = png_ptr; ++ control->info_ptr = info_ptr; ++ control->for_write = 0; ++ ++ image->opaque = control; ++ return 1; ++ } ++ ++ /* Error clean up */ ++ png_destroy_info_struct(png_ptr, &info_ptr); ++ } ++ ++ png_destroy_read_struct(&png_ptr, NULL, NULL); ++ } ++ ++ return png_image_error(image, "png_image_read: out of memory"); ++ } ++ ++ return png_image_error(image, "png_image_read: opaque pointer not NULL"); ++} ++ ++/* Utility to find the base format of a PNG file from a png_struct. */ ++static png_uint_32 ++png_image_format(png_structrp png_ptr) ++{ ++ png_uint_32 format = 0; ++ ++ if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0) ++ format |= PNG_FORMAT_FLAG_COLOR; ++ ++ if ((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0) ++ format |= PNG_FORMAT_FLAG_ALPHA; ++ ++ /* Use png_ptr here, not info_ptr, because by examination png_handle_tRNS ++ * sets the png_struct fields; that's all we are interested in here. The ++ * precise interaction with an app call to png_set_tRNS and PNG file reading ++ * is unclear. ++ */ ++ else if (png_ptr->num_trans > 0) ++ format |= PNG_FORMAT_FLAG_ALPHA; ++ ++ if (png_ptr->bit_depth == 16) ++ format |= PNG_FORMAT_FLAG_LINEAR; ++ ++ if ((png_ptr->color_type & PNG_COLOR_MASK_PALETTE) != 0) ++ format |= PNG_FORMAT_FLAG_COLORMAP; ++ ++ return format; ++} ++ ++/* Is the given gamma significantly different from sRGB? The test is the same ++ * one used in pngrtran.c when deciding whether to do gamma correction. The ++ * arithmetic optimizes the division by using the fact that the inverse of the ++ * file sRGB gamma is 2.2 ++ */ ++static int ++png_gamma_not_sRGB(png_fixed_point g) ++{ ++ if (g < PNG_FP_1) ++ { ++ /* An uninitialized gamma is assumed to be sRGB for the simplified API. */ ++ if (g == 0) ++ return 0; ++ ++ return png_gamma_significant((g * 11 + 2)/5 /* i.e. *2.2, rounded */); ++ } ++ ++ return 1; ++} ++ ++/* Do the main body of a 'png_image_begin_read' function; read the PNG file ++ * header and fill in all the information. This is executed in a safe context, ++ * unlike the init routine above. ++ */ ++static int ++png_image_read_header(png_voidp argument) ++{ ++ png_imagep image = png_voidcast(png_imagep, argument); ++ png_structrp png_ptr = image->opaque->png_ptr; ++ png_inforp info_ptr = image->opaque->info_ptr; ++ ++#ifdef PNG_BENIGN_ERRORS_SUPPORTED ++ png_set_benign_errors(png_ptr, 1/*warn*/); ++#endif ++ png_read_info(png_ptr, info_ptr); ++ ++ /* Do this the fast way; just read directly out of png_struct. */ ++ image->width = png_ptr->width; ++ image->height = png_ptr->height; ++ ++ { ++ png_uint_32 format = png_image_format(png_ptr); ++ ++ image->format = format; ++ ++#ifdef PNG_COLORSPACE_SUPPORTED ++ /* Does the colorspace match sRGB? If there is no color endpoint ++ * (colorant) information assume yes, otherwise require the ++ * 'ENDPOINTS_MATCHP_sRGB' colorspace flag to have been set. If the ++ * colorspace has been determined to be invalid ignore it. ++ */ ++ if ((format & PNG_FORMAT_FLAG_COLOR) != 0 && ((png_ptr->colorspace.flags ++ & (PNG_COLORSPACE_HAVE_ENDPOINTS|PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB| ++ PNG_COLORSPACE_INVALID)) == PNG_COLORSPACE_HAVE_ENDPOINTS)) ++ image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB; ++#endif ++ } ++ ++ /* We need the maximum number of entries regardless of the format the ++ * application sets here. ++ */ ++ { ++ png_uint_32 cmap_entries; ++ ++ switch (png_ptr->color_type) ++ { ++ case PNG_COLOR_TYPE_GRAY: ++ cmap_entries = 1U << png_ptr->bit_depth; ++ break; ++ ++ case PNG_COLOR_TYPE_PALETTE: ++ cmap_entries = (png_uint_32)png_ptr->num_palette; ++ break; ++ ++ default: ++ cmap_entries = 256; ++ break; ++ } ++ ++ if (cmap_entries > 256) ++ cmap_entries = 256; ++ ++ image->colormap_entries = cmap_entries; ++ } ++ ++ return 1; ++} ++ ++#ifdef PNG_STDIO_SUPPORTED ++int PNGAPI ++png_image_begin_read_from_stdio(png_imagep image, FILE* file) ++{ ++ if (image != NULL && image->version == PNG_IMAGE_VERSION) ++ { ++ if (file != NULL) ++ { ++ if (png_image_read_init(image) != 0) ++ { ++ /* This is slightly evil, but png_init_io doesn't do anything other ++ * than this and we haven't changed the standard IO functions so ++ * this saves a 'safe' function. ++ */ ++ image->opaque->png_ptr->io_ptr = file; ++ return png_safe_execute(image, png_image_read_header, image); ++ } ++ } ++ ++ else ++ return png_image_error(image, ++ "png_image_begin_read_from_stdio: invalid argument"); ++ } ++ ++ else if (image != NULL) ++ return png_image_error(image, ++ "png_image_begin_read_from_stdio: incorrect PNG_IMAGE_VERSION"); ++ ++ return 0; ++} ++ ++int PNGAPI ++png_image_begin_read_from_file(png_imagep image, const char *file_name) ++{ ++ if (image != NULL && image->version == PNG_IMAGE_VERSION) ++ { ++ if (file_name != NULL) ++ { ++ FILE *fp = fopen(file_name, "rb"); ++ ++ if (fp != NULL) ++ { ++ if (png_image_read_init(image) != 0) ++ { ++ image->opaque->png_ptr->io_ptr = fp; ++ image->opaque->owned_file = 1; ++ return png_safe_execute(image, png_image_read_header, image); ++ } ++ ++ /* Clean up: just the opened file. */ ++ (void)fclose(fp); ++ } ++ ++ else ++ return png_image_error(image, strerror(errno)); ++ } ++ ++ else ++ return png_image_error(image, ++ "png_image_begin_read_from_file: invalid argument"); ++ } ++ ++ else if (image != NULL) ++ return png_image_error(image, ++ "png_image_begin_read_from_file: incorrect PNG_IMAGE_VERSION"); ++ ++ return 0; ++} ++#endif /* STDIO */ ++ ++static void PNGCBAPI ++png_image_memory_read(png_structp png_ptr, png_bytep out, size_t need) ++{ ++ if (png_ptr != NULL) ++ { ++ png_imagep image = png_voidcast(png_imagep, png_ptr->io_ptr); ++ if (image != NULL) ++ { ++ png_controlp cp = image->opaque; ++ if (cp != NULL) ++ { ++ png_const_bytep memory = cp->memory; ++ size_t size = cp->size; ++ ++ if (memory != NULL && size >= need) ++ { ++ memcpy(out, memory, need); ++ cp->memory = memory + need; ++ cp->size = size - need; ++ return; ++ } ++ ++ png_error(png_ptr, "read beyond end of data"); ++ } ++ } ++ ++ png_error(png_ptr, "invalid memory read"); ++ } ++} ++ ++int PNGAPI png_image_begin_read_from_memory(png_imagep image, ++ png_const_voidp memory, size_t size) ++{ ++ if (image != NULL && image->version == PNG_IMAGE_VERSION) ++ { ++ if (memory != NULL && size > 0) ++ { ++ if (png_image_read_init(image) != 0) ++ { ++ /* Now set the IO functions to read from the memory buffer and ++ * store it into io_ptr. Again do this in-place to avoid calling a ++ * libpng function that requires error handling. ++ */ ++ image->opaque->memory = png_voidcast(png_const_bytep, memory); ++ image->opaque->size = size; ++ image->opaque->png_ptr->io_ptr = image; ++ image->opaque->png_ptr->read_data_fn = png_image_memory_read; ++ ++ return png_safe_execute(image, png_image_read_header, image); ++ } ++ } ++ ++ else ++ return png_image_error(image, ++ "png_image_begin_read_from_memory: invalid argument"); ++ } ++ ++ else if (image != NULL) ++ return png_image_error(image, ++ "png_image_begin_read_from_memory: incorrect PNG_IMAGE_VERSION"); ++ ++ return 0; ++} ++ ++/* Utility function to skip chunks that are not used by the simplified image ++ * read functions and an appropriate macro to call it. ++ */ ++#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED ++static void ++png_image_skip_unused_chunks(png_structrp png_ptr) ++{ ++ /* Prepare the reader to ignore all recognized chunks whose data will not ++ * be used, i.e., all chunks recognized by libpng except for those ++ * involved in basic image reading: ++ * ++ * IHDR, PLTE, IDAT, IEND ++ * ++ * Or image data handling: ++ * ++ * tRNS, bKGD, gAMA, cHRM, sRGB, [iCCP] and sBIT. ++ * ++ * This provides a small performance improvement and eliminates any ++ * potential vulnerability to security problems in the unused chunks. ++ * ++ * At present the iCCP chunk data isn't used, so iCCP chunk can be ignored ++ * too. This allows the simplified API to be compiled without iCCP support, ++ * however if the support is there the chunk is still checked to detect ++ * errors (which are unfortunately quite common.) ++ */ ++ { ++ static const png_byte chunks_to_process[] = { ++ 98, 75, 71, 68, '\0', /* bKGD */ ++ 99, 72, 82, 77, '\0', /* cHRM */ ++ 103, 65, 77, 65, '\0', /* gAMA */ ++# ifdef PNG_READ_iCCP_SUPPORTED ++ 105, 67, 67, 80, '\0', /* iCCP */ ++# endif ++ 115, 66, 73, 84, '\0', /* sBIT */ ++ 115, 82, 71, 66, '\0', /* sRGB */ ++ }; ++ ++ /* Ignore unknown chunks and all other chunks except for the ++ * IHDR, PLTE, tRNS, IDAT, and IEND chunks. ++ */ ++ png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_NEVER, ++ NULL, -1); ++ ++ /* But do not ignore image data handling chunks */ ++ png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_AS_DEFAULT, ++ chunks_to_process, (int)/*SAFE*/(sizeof chunks_to_process)/5); ++ } ++} ++ ++# define PNG_SKIP_CHUNKS(p) png_image_skip_unused_chunks(p) ++#else ++# define PNG_SKIP_CHUNKS(p) ((void)0) ++#endif /* HANDLE_AS_UNKNOWN */ ++ ++/* The following macro gives the exact rounded answer for all values in the ++ * range 0..255 (it actually divides by 51.2, but the rounding still generates ++ * the correct numbers 0..5 ++ */ ++#define PNG_DIV51(v8) (((v8) * 5 + 130) >> 8) ++ ++/* Utility functions to make particular color-maps */ ++static void ++set_file_encoding(png_image_read_control *display) ++{ ++ png_fixed_point g = display->image->opaque->png_ptr->colorspace.gamma; ++ if (png_gamma_significant(g) != 0) ++ { ++ if (png_gamma_not_sRGB(g) != 0) ++ { ++ display->file_encoding = P_FILE; ++ display->gamma_to_linear = png_reciprocal(g); ++ } ++ ++ else ++ display->file_encoding = P_sRGB; ++ } ++ ++ else ++ display->file_encoding = P_LINEAR8; ++} ++ ++static unsigned int ++decode_gamma(png_image_read_control *display, png_uint_32 value, int encoding) ++{ ++ if (encoding == P_FILE) /* double check */ ++ encoding = display->file_encoding; ++ ++ if (encoding == P_NOTSET) /* must be the file encoding */ ++ { ++ set_file_encoding(display); ++ encoding = display->file_encoding; ++ } ++ ++ switch (encoding) ++ { ++ case P_FILE: ++ value = png_gamma_16bit_correct(value*257, display->gamma_to_linear); ++ break; ++ ++ case P_sRGB: ++ value = png_sRGB_table[value]; ++ break; ++ ++ case P_LINEAR: ++ break; ++ ++ case P_LINEAR8: ++ value *= 257; ++ break; ++ ++#ifdef __GNUC__ ++ default: ++ png_error(display->image->opaque->png_ptr, ++ "unexpected encoding (internal error)"); ++#endif ++ } ++ ++ return value; ++} ++ ++static png_uint_32 ++png_colormap_compose(png_image_read_control *display, ++ png_uint_32 foreground, int foreground_encoding, png_uint_32 alpha, ++ png_uint_32 background, int encoding) ++{ ++ /* The file value is composed on the background, the background has the given ++ * encoding and so does the result, the file is encoded with P_FILE and the ++ * file and alpha are 8-bit values. The (output) encoding will always be ++ * P_LINEAR or P_sRGB. ++ */ ++ png_uint_32 f = decode_gamma(display, foreground, foreground_encoding); ++ png_uint_32 b = decode_gamma(display, background, encoding); ++ ++ /* The alpha is always an 8-bit value (it comes from the palette), the value ++ * scaled by 255 is what PNG_sRGB_FROM_LINEAR requires. ++ */ ++ f = f * alpha + b * (255-alpha); ++ ++ if (encoding == P_LINEAR) ++ { ++ /* Scale to 65535; divide by 255, approximately (in fact this is extremely ++ * accurate, it divides by 255.00000005937181414556, with no overflow.) ++ */ ++ f *= 257; /* Now scaled by 65535 */ ++ f += f >> 16; ++ f = (f+32768) >> 16; ++ } ++ ++ else /* P_sRGB */ ++ f = PNG_sRGB_FROM_LINEAR(f); ++ ++ return f; ++} ++ ++/* NOTE: P_LINEAR values to this routine must be 16-bit, but P_FILE values must ++ * be 8-bit. ++ */ ++static void ++png_create_colormap_entry(png_image_read_control *display, ++ png_uint_32 ip, png_uint_32 red, png_uint_32 green, png_uint_32 blue, ++ png_uint_32 alpha, int encoding) ++{ ++ png_imagep image = display->image; ++ int output_encoding = (image->format & PNG_FORMAT_FLAG_LINEAR) != 0 ? ++ P_LINEAR : P_sRGB; ++ int convert_to_Y = (image->format & PNG_FORMAT_FLAG_COLOR) == 0 && ++ (red != green || green != blue); ++ ++ if (ip > 255) ++ png_error(image->opaque->png_ptr, "color-map index out of range"); ++ ++ /* Update the cache with whether the file gamma is significantly different ++ * from sRGB. ++ */ ++ if (encoding == P_FILE) ++ { ++ if (display->file_encoding == P_NOTSET) ++ set_file_encoding(display); ++ ++ /* Note that the cached value may be P_FILE too, but if it is then the ++ * gamma_to_linear member has been set. ++ */ ++ encoding = display->file_encoding; ++ } ++ ++ if (encoding == P_FILE) ++ { ++ png_fixed_point g = display->gamma_to_linear; ++ ++ red = png_gamma_16bit_correct(red*257, g); ++ green = png_gamma_16bit_correct(green*257, g); ++ blue = png_gamma_16bit_correct(blue*257, g); ++ ++ if (convert_to_Y != 0 || output_encoding == P_LINEAR) ++ { ++ alpha *= 257; ++ encoding = P_LINEAR; ++ } ++ ++ else ++ { ++ red = PNG_sRGB_FROM_LINEAR(red * 255); ++ green = PNG_sRGB_FROM_LINEAR(green * 255); ++ blue = PNG_sRGB_FROM_LINEAR(blue * 255); ++ encoding = P_sRGB; ++ } ++ } ++ ++ else if (encoding == P_LINEAR8) ++ { ++ /* This encoding occurs quite frequently in test cases because PngSuite ++ * includes a gAMA 1.0 chunk with most images. ++ */ ++ red *= 257; ++ green *= 257; ++ blue *= 257; ++ alpha *= 257; ++ encoding = P_LINEAR; ++ } ++ ++ else if (encoding == P_sRGB && ++ (convert_to_Y != 0 || output_encoding == P_LINEAR)) ++ { ++ /* The values are 8-bit sRGB values, but must be converted to 16-bit ++ * linear. ++ */ ++ red = png_sRGB_table[red]; ++ green = png_sRGB_table[green]; ++ blue = png_sRGB_table[blue]; ++ alpha *= 257; ++ encoding = P_LINEAR; ++ } ++ ++ /* This is set if the color isn't gray but the output is. */ ++ if (encoding == P_LINEAR) ++ { ++ if (convert_to_Y != 0) ++ { ++ /* NOTE: these values are copied from png_do_rgb_to_gray */ ++ png_uint_32 y = (png_uint_32)6968 * red + (png_uint_32)23434 * green + ++ (png_uint_32)2366 * blue; ++ ++ if (output_encoding == P_LINEAR) ++ y = (y + 16384) >> 15; ++ ++ else ++ { ++ /* y is scaled by 32768, we need it scaled by 255: */ ++ y = (y + 128) >> 8; ++ y *= 255; ++ y = PNG_sRGB_FROM_LINEAR((y + 64) >> 7); ++ alpha = PNG_DIV257(alpha); ++ encoding = P_sRGB; ++ } ++ ++ blue = red = green = y; ++ } ++ ++ else if (output_encoding == P_sRGB) ++ { ++ red = PNG_sRGB_FROM_LINEAR(red * 255); ++ green = PNG_sRGB_FROM_LINEAR(green * 255); ++ blue = PNG_sRGB_FROM_LINEAR(blue * 255); ++ alpha = PNG_DIV257(alpha); ++ encoding = P_sRGB; ++ } ++ } ++ ++ if (encoding != output_encoding) ++ png_error(image->opaque->png_ptr, "bad encoding (internal error)"); ++ ++ /* Store the value. */ ++ { ++# ifdef PNG_FORMAT_AFIRST_SUPPORTED ++ int afirst = (image->format & PNG_FORMAT_FLAG_AFIRST) != 0 && ++ (image->format & PNG_FORMAT_FLAG_ALPHA) != 0; ++# else ++# define afirst 0 ++# endif ++# ifdef PNG_FORMAT_BGR_SUPPORTED ++ int bgr = (image->format & PNG_FORMAT_FLAG_BGR) != 0 ? 2 : 0; ++# else ++# define bgr 0 ++# endif ++ ++ if (output_encoding == P_LINEAR) ++ { ++ png_uint_16p entry = png_voidcast(png_uint_16p, display->colormap); ++ ++ entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format); ++ ++ /* The linear 16-bit values must be pre-multiplied by the alpha channel ++ * value, if less than 65535 (this is, effectively, composite on black ++ * if the alpha channel is removed.) ++ */ ++ switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format)) ++ { ++ case 4: ++ entry[afirst ? 0 : 3] = (png_uint_16)alpha; ++ /* FALLTHROUGH */ ++ ++ case 3: ++ if (alpha < 65535) ++ { ++ if (alpha > 0) ++ { ++ blue = (blue * alpha + 32767U)/65535U; ++ green = (green * alpha + 32767U)/65535U; ++ red = (red * alpha + 32767U)/65535U; ++ } ++ ++ else ++ red = green = blue = 0; ++ } ++ entry[afirst + (2 ^ bgr)] = (png_uint_16)blue; ++ entry[afirst + 1] = (png_uint_16)green; ++ entry[afirst + bgr] = (png_uint_16)red; ++ break; ++ ++ case 2: ++ entry[1 ^ afirst] = (png_uint_16)alpha; ++ /* FALLTHROUGH */ ++ ++ case 1: ++ if (alpha < 65535) ++ { ++ if (alpha > 0) ++ green = (green * alpha + 32767U)/65535U; ++ ++ else ++ green = 0; ++ } ++ entry[afirst] = (png_uint_16)green; ++ break; ++ ++ default: ++ break; ++ } ++ } ++ ++ else /* output encoding is P_sRGB */ ++ { ++ png_bytep entry = png_voidcast(png_bytep, display->colormap); ++ ++ entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format); ++ ++ switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format)) ++ { ++ case 4: ++ entry[afirst ? 0 : 3] = (png_byte)alpha; ++ /* FALLTHROUGH */ ++ case 3: ++ entry[afirst + (2 ^ bgr)] = (png_byte)blue; ++ entry[afirst + 1] = (png_byte)green; ++ entry[afirst + bgr] = (png_byte)red; ++ break; ++ ++ case 2: ++ entry[1 ^ afirst] = (png_byte)alpha; ++ /* FALLTHROUGH */ ++ case 1: ++ entry[afirst] = (png_byte)green; ++ break; ++ ++ default: ++ break; ++ } ++ } ++ ++# ifdef afirst ++# undef afirst ++# endif ++# ifdef bgr ++# undef bgr ++# endif ++ } ++} ++ ++static int ++make_gray_file_colormap(png_image_read_control *display) ++{ ++ unsigned int i; ++ ++ for (i=0; i<256; ++i) ++ png_create_colormap_entry(display, i, i, i, i, 255, P_FILE); ++ ++ return (int)i; ++} ++ ++static int ++make_gray_colormap(png_image_read_control *display) ++{ ++ unsigned int i; ++ ++ for (i=0; i<256; ++i) ++ png_create_colormap_entry(display, i, i, i, i, 255, P_sRGB); ++ ++ return (int)i; ++} ++#define PNG_GRAY_COLORMAP_ENTRIES 256 ++ ++static int ++make_ga_colormap(png_image_read_control *display) ++{ ++ unsigned int i, a; ++ ++ /* Alpha is retained, the output will be a color-map with entries ++ * selected by six levels of alpha. One transparent entry, 6 gray ++ * levels for all the intermediate alpha values, leaving 230 entries ++ * for the opaque grays. The color-map entries are the six values ++ * [0..5]*51, the GA processing uses PNG_DIV51(value) to find the ++ * relevant entry. ++ * ++ * if (alpha > 229) // opaque ++ * { ++ * // The 231 entries are selected to make the math below work: ++ * base = 0; ++ * entry = (231 * gray + 128) >> 8; ++ * } ++ * else if (alpha < 26) // transparent ++ * { ++ * base = 231; ++ * entry = 0; ++ * } ++ * else // partially opaque ++ * { ++ * base = 226 + 6 * PNG_DIV51(alpha); ++ * entry = PNG_DIV51(gray); ++ * } ++ */ ++ i = 0; ++ while (i < 231) ++ { ++ unsigned int gray = (i * 256 + 115) / 231; ++ png_create_colormap_entry(display, i++, gray, gray, gray, 255, P_sRGB); ++ } ++ ++ /* 255 is used here for the component values for consistency with the code ++ * that undoes premultiplication in pngwrite.c. ++ */ ++ png_create_colormap_entry(display, i++, 255, 255, 255, 0, P_sRGB); ++ ++ for (a=1; a<5; ++a) ++ { ++ unsigned int g; ++ ++ for (g=0; g<6; ++g) ++ png_create_colormap_entry(display, i++, g*51, g*51, g*51, a*51, ++ P_sRGB); ++ } ++ ++ return (int)i; ++} ++ ++#define PNG_GA_COLORMAP_ENTRIES 256 ++ ++static int ++make_rgb_colormap(png_image_read_control *display) ++{ ++ unsigned int i, r; ++ ++ /* Build a 6x6x6 opaque RGB cube */ ++ for (i=r=0; r<6; ++r) ++ { ++ unsigned int g; ++ ++ for (g=0; g<6; ++g) ++ { ++ unsigned int b; ++ ++ for (b=0; b<6; ++b) ++ png_create_colormap_entry(display, i++, r*51, g*51, b*51, 255, ++ P_sRGB); ++ } ++ } ++ ++ return (int)i; ++} ++ ++#define PNG_RGB_COLORMAP_ENTRIES 216 ++ ++/* Return a palette index to the above palette given three 8-bit sRGB values. */ ++#define PNG_RGB_INDEX(r,g,b) \ ++ ((png_byte)(6 * (6 * PNG_DIV51(r) + PNG_DIV51(g)) + PNG_DIV51(b))) ++ ++static int ++png_image_read_colormap(png_voidp argument) ++{ ++ png_image_read_control *display = ++ png_voidcast(png_image_read_control*, argument); ++ png_imagep image = display->image; ++ ++ png_structrp png_ptr = image->opaque->png_ptr; ++ png_uint_32 output_format = image->format; ++ int output_encoding = (output_format & PNG_FORMAT_FLAG_LINEAR) != 0 ? ++ P_LINEAR : P_sRGB; ++ ++ unsigned int cmap_entries; ++ unsigned int output_processing; /* Output processing option */ ++ unsigned int data_encoding = P_NOTSET; /* Encoding libpng must produce */ ++ ++ /* Background information; the background color and the index of this color ++ * in the color-map if it exists (else 256). ++ */ ++ unsigned int background_index = 256; ++ png_uint_32 back_r, back_g, back_b; ++ ++ /* Flags to accumulate things that need to be done to the input. */ ++ int expand_tRNS = 0; ++ ++ /* Exclude the NYI feature of compositing onto a color-mapped buffer; it is ++ * very difficult to do, the results look awful, and it is difficult to see ++ * what possible use it is because the application can't control the ++ * color-map. ++ */ ++ if (((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0 || ++ png_ptr->num_trans > 0) /* alpha in input */ && ++ ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) /* no alpha in output */) ++ { ++ if (output_encoding == P_LINEAR) /* compose on black */ ++ back_b = back_g = back_r = 0; ++ ++ else if (display->background == NULL /* no way to remove it */) ++ png_error(png_ptr, ++ "background color must be supplied to remove alpha/transparency"); ++ ++ /* Get a copy of the background color (this avoids repeating the checks ++ * below.) The encoding is 8-bit sRGB or 16-bit linear, depending on the ++ * output format. ++ */ ++ else ++ { ++ back_g = display->background->green; ++ if ((output_format & PNG_FORMAT_FLAG_COLOR) != 0) ++ { ++ back_r = display->background->red; ++ back_b = display->background->blue; ++ } ++ else ++ back_b = back_r = back_g; ++ } ++ } ++ ++ else if (output_encoding == P_LINEAR) ++ back_b = back_r = back_g = 65535; ++ ++ else ++ back_b = back_r = back_g = 255; ++ ++ /* Default the input file gamma if required - this is necessary because ++ * libpng assumes that if no gamma information is present the data is in the ++ * output format, but the simplified API deduces the gamma from the input ++ * format. ++ */ ++ if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_GAMMA) == 0) ++ { ++ /* Do this directly, not using the png_colorspace functions, to ensure ++ * that it happens even if the colorspace is invalid (though probably if ++ * it is the setting will be ignored) Note that the same thing can be ++ * achieved at the application interface with png_set_gAMA. ++ */ ++ if (png_ptr->bit_depth == 16 && ++ (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0) ++ png_ptr->colorspace.gamma = PNG_GAMMA_LINEAR; ++ ++ else ++ png_ptr->colorspace.gamma = PNG_GAMMA_sRGB_INVERSE; ++ ++ png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA; ++ } ++ ++ /* Decide what to do based on the PNG color type of the input data. The ++ * utility function png_create_colormap_entry deals with most aspects of the ++ * output transformations; this code works out how to produce bytes of ++ * color-map entries from the original format. ++ */ ++ switch (png_ptr->color_type) ++ { ++ case PNG_COLOR_TYPE_GRAY: ++ if (png_ptr->bit_depth <= 8) ++ { ++ /* There at most 256 colors in the output, regardless of ++ * transparency. ++ */ ++ unsigned int step, i, val, trans = 256/*ignore*/, back_alpha = 0; ++ ++ cmap_entries = 1U << png_ptr->bit_depth; ++ if (cmap_entries > image->colormap_entries) ++ png_error(png_ptr, "gray[8] color-map: too few entries"); ++ ++ step = 255 / (cmap_entries - 1); ++ output_processing = PNG_CMAP_NONE; ++ ++ /* If there is a tRNS chunk then this either selects a transparent ++ * value or, if the output has no alpha, the background color. ++ */ ++ if (png_ptr->num_trans > 0) ++ { ++ trans = png_ptr->trans_color.gray; ++ ++ if ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) ++ back_alpha = output_encoding == P_LINEAR ? 65535 : 255; ++ } ++ ++ /* png_create_colormap_entry just takes an RGBA and writes the ++ * corresponding color-map entry using the format from 'image', ++ * including the required conversion to sRGB or linear as ++ * appropriate. The input values are always either sRGB (if the ++ * gamma correction flag is 0) or 0..255 scaled file encoded values ++ * (if the function must gamma correct them). ++ */ ++ for (i=val=0; ibit_depth < 8) ++ png_set_packing(png_ptr); ++ } ++ ++ else /* bit depth is 16 */ ++ { ++ /* The 16-bit input values can be converted directly to 8-bit gamma ++ * encoded values; however, if a tRNS chunk is present 257 color-map ++ * entries are required. This means that the extra entry requires ++ * special processing; add an alpha channel, sacrifice gray level ++ * 254 and convert transparent (alpha==0) entries to that. ++ * ++ * Use libpng to chop the data to 8 bits. Convert it to sRGB at the ++ * same time to minimize quality loss. If a tRNS chunk is present ++ * this means libpng must handle it too; otherwise it is impossible ++ * to do the exact match on the 16-bit value. ++ * ++ * If the output has no alpha channel *and* the background color is ++ * gray then it is possible to let libpng handle the substitution by ++ * ensuring that the corresponding gray level matches the background ++ * color exactly. ++ */ ++ data_encoding = P_sRGB; ++ ++ if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries) ++ png_error(png_ptr, "gray[16] color-map: too few entries"); ++ ++ cmap_entries = (unsigned int)make_gray_colormap(display); ++ ++ if (png_ptr->num_trans > 0) ++ { ++ unsigned int back_alpha; ++ ++ if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0) ++ back_alpha = 0; ++ ++ else ++ { ++ if (back_r == back_g && back_g == back_b) ++ { ++ /* Background is gray; no special processing will be ++ * required. ++ */ ++ png_color_16 c; ++ png_uint_32 gray = back_g; ++ ++ if (output_encoding == P_LINEAR) ++ { ++ gray = PNG_sRGB_FROM_LINEAR(gray * 255); ++ ++ /* And make sure the corresponding palette entry ++ * matches. ++ */ ++ png_create_colormap_entry(display, gray, back_g, back_g, ++ back_g, 65535, P_LINEAR); ++ } ++ ++ /* The background passed to libpng, however, must be the ++ * sRGB value. ++ */ ++ c.index = 0; /*unused*/ ++ c.gray = c.red = c.green = c.blue = (png_uint_16)gray; ++ ++ /* NOTE: does this work without expanding tRNS to alpha? ++ * It should be the color->gray case below apparently ++ * doesn't. ++ */ ++ png_set_background_fixed(png_ptr, &c, ++ PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, ++ 0/*gamma: not used*/); ++ ++ output_processing = PNG_CMAP_NONE; ++ break; ++ } ++#ifdef __COVERITY__ ++ /* Coverity claims that output_encoding cannot be 2 (P_LINEAR) ++ * here. ++ */ ++ back_alpha = 255; ++#else ++ back_alpha = output_encoding == P_LINEAR ? 65535 : 255; ++#endif ++ } ++ ++ /* output_processing means that the libpng-processed row will be ++ * 8-bit GA and it has to be processing to single byte color-map ++ * values. Entry 254 is replaced by either a completely ++ * transparent entry or by the background color at full ++ * precision (and the background color is not a simple gray ++ * level in this case.) ++ */ ++ expand_tRNS = 1; ++ output_processing = PNG_CMAP_TRANS; ++ background_index = 254; ++ ++ /* And set (overwrite) color-map entry 254 to the actual ++ * background color at full precision. ++ */ ++ png_create_colormap_entry(display, 254, back_r, back_g, back_b, ++ back_alpha, output_encoding); ++ } ++ ++ else ++ output_processing = PNG_CMAP_NONE; ++ } ++ break; ++ ++ case PNG_COLOR_TYPE_GRAY_ALPHA: ++ /* 8-bit or 16-bit PNG with two channels - gray and alpha. A minimum ++ * of 65536 combinations. If, however, the alpha channel is to be ++ * removed there are only 256 possibilities if the background is gray. ++ * (Otherwise there is a subset of the 65536 possibilities defined by ++ * the triangle between black, white and the background color.) ++ * ++ * Reduce 16-bit files to 8-bit and sRGB encode the result. No need to ++ * worry about tRNS matching - tRNS is ignored if there is an alpha ++ * channel. ++ */ ++ data_encoding = P_sRGB; ++ ++ if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0) ++ { ++ if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries) ++ png_error(png_ptr, "gray+alpha color-map: too few entries"); ++ ++ cmap_entries = (unsigned int)make_ga_colormap(display); ++ ++ background_index = PNG_CMAP_GA_BACKGROUND; ++ output_processing = PNG_CMAP_GA; ++ } ++ ++ else /* alpha is removed */ ++ { ++ /* Alpha must be removed as the PNG data is processed when the ++ * background is a color because the G and A channels are ++ * independent and the vector addition (non-parallel vectors) is a ++ * 2-D problem. ++ * ++ * This can be reduced to the same algorithm as above by making a ++ * colormap containing gray levels (for the opaque grays), a ++ * background entry (for a transparent pixel) and a set of four six ++ * level color values, one set for each intermediate alpha value. ++ * See the comments in make_ga_colormap for how this works in the ++ * per-pixel processing. ++ * ++ * If the background is gray, however, we only need a 256 entry gray ++ * level color map. It is sufficient to make the entry generated ++ * for the background color be exactly the color specified. ++ */ ++ if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0 || ++ (back_r == back_g && back_g == back_b)) ++ { ++ /* Background is gray; no special processing will be required. */ ++ png_color_16 c; ++ png_uint_32 gray = back_g; ++ ++ if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries) ++ png_error(png_ptr, "gray-alpha color-map: too few entries"); ++ ++ cmap_entries = (unsigned int)make_gray_colormap(display); ++ ++ if (output_encoding == P_LINEAR) ++ { ++ gray = PNG_sRGB_FROM_LINEAR(gray * 255); ++ ++ /* And make sure the corresponding palette entry matches. */ ++ png_create_colormap_entry(display, gray, back_g, back_g, ++ back_g, 65535, P_LINEAR); ++ } ++ ++ /* The background passed to libpng, however, must be the sRGB ++ * value. ++ */ ++ c.index = 0; /*unused*/ ++ c.gray = c.red = c.green = c.blue = (png_uint_16)gray; ++ ++ png_set_background_fixed(png_ptr, &c, ++ PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, ++ 0/*gamma: not used*/); ++ ++ output_processing = PNG_CMAP_NONE; ++ } ++ ++ else ++ { ++ png_uint_32 i, a; ++ ++ /* This is the same as png_make_ga_colormap, above, except that ++ * the entries are all opaque. ++ */ ++ if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries) ++ png_error(png_ptr, "ga-alpha color-map: too few entries"); ++ ++ i = 0; ++ while (i < 231) ++ { ++ png_uint_32 gray = (i * 256 + 115) / 231; ++ png_create_colormap_entry(display, i++, gray, gray, gray, ++ 255, P_sRGB); ++ } ++ ++ /* NOTE: this preserves the full precision of the application ++ * background color. ++ */ ++ background_index = i; ++ png_create_colormap_entry(display, i++, back_r, back_g, back_b, ++#ifdef __COVERITY__ ++ /* Coverity claims that output_encoding ++ * cannot be 2 (P_LINEAR) here. ++ */ 255U, ++#else ++ output_encoding == P_LINEAR ? 65535U : 255U, ++#endif ++ output_encoding); ++ ++ /* For non-opaque input composite on the sRGB background - this ++ * requires inverting the encoding for each component. The input ++ * is still converted to the sRGB encoding because this is a ++ * reasonable approximate to the logarithmic curve of human ++ * visual sensitivity, at least over the narrow range which PNG ++ * represents. Consequently 'G' is always sRGB encoded, while ++ * 'A' is linear. We need the linear background colors. ++ */ ++ if (output_encoding == P_sRGB) /* else already linear */ ++ { ++ /* This may produce a value not exactly matching the ++ * background, but that's ok because these numbers are only ++ * used when alpha != 0 ++ */ ++ back_r = png_sRGB_table[back_r]; ++ back_g = png_sRGB_table[back_g]; ++ back_b = png_sRGB_table[back_b]; ++ } ++ ++ for (a=1; a<5; ++a) ++ { ++ unsigned int g; ++ ++ /* PNG_sRGB_FROM_LINEAR expects a 16-bit linear value scaled ++ * by an 8-bit alpha value (0..255). ++ */ ++ png_uint_32 alpha = 51 * a; ++ png_uint_32 back_rx = (255-alpha) * back_r; ++ png_uint_32 back_gx = (255-alpha) * back_g; ++ png_uint_32 back_bx = (255-alpha) * back_b; ++ ++ for (g=0; g<6; ++g) ++ { ++ png_uint_32 gray = png_sRGB_table[g*51] * alpha; ++ ++ png_create_colormap_entry(display, i++, ++ PNG_sRGB_FROM_LINEAR(gray + back_rx), ++ PNG_sRGB_FROM_LINEAR(gray + back_gx), ++ PNG_sRGB_FROM_LINEAR(gray + back_bx), 255, P_sRGB); ++ } ++ } ++ ++ cmap_entries = i; ++ output_processing = PNG_CMAP_GA; ++ } ++ } ++ break; ++ ++ case PNG_COLOR_TYPE_RGB: ++ case PNG_COLOR_TYPE_RGB_ALPHA: ++ /* Exclude the case where the output is gray; we can always handle this ++ * with the cases above. ++ */ ++ if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0) ++ { ++ /* The color-map will be grayscale, so we may as well convert the ++ * input RGB values to a simple grayscale and use the grayscale ++ * code above. ++ * ++ * NOTE: calling this apparently damages the recognition of the ++ * transparent color in background color handling; call ++ * png_set_tRNS_to_alpha before png_set_background_fixed. ++ */ ++ png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, -1, ++ -1); ++ data_encoding = P_sRGB; ++ ++ /* The output will now be one or two 8-bit gray or gray+alpha ++ * channels. The more complex case arises when the input has alpha. ++ */ ++ if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || ++ png_ptr->num_trans > 0) && ++ (output_format & PNG_FORMAT_FLAG_ALPHA) != 0) ++ { ++ /* Both input and output have an alpha channel, so no background ++ * processing is required; just map the GA bytes to the right ++ * color-map entry. ++ */ ++ expand_tRNS = 1; ++ ++ if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries) ++ png_error(png_ptr, "rgb[ga] color-map: too few entries"); ++ ++ cmap_entries = (unsigned int)make_ga_colormap(display); ++ background_index = PNG_CMAP_GA_BACKGROUND; ++ output_processing = PNG_CMAP_GA; ++ } ++ ++ else ++ { ++ /* Either the input or the output has no alpha channel, so there ++ * will be no non-opaque pixels in the color-map; it will just be ++ * grayscale. ++ */ ++ if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries) ++ png_error(png_ptr, "rgb[gray] color-map: too few entries"); ++ ++ /* Ideally this code would use libpng to do the gamma correction, ++ * but if an input alpha channel is to be removed we will hit the ++ * libpng bug in gamma+compose+rgb-to-gray (the double gamma ++ * correction bug). Fix this by dropping the gamma correction in ++ * this case and doing it in the palette; this will result in ++ * duplicate palette entries, but that's better than the ++ * alternative of double gamma correction. ++ */ ++ if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || ++ png_ptr->num_trans > 0) && ++ png_gamma_not_sRGB(png_ptr->colorspace.gamma) != 0) ++ { ++ cmap_entries = (unsigned int)make_gray_file_colormap(display); ++ data_encoding = P_FILE; ++ } ++ ++ else ++ cmap_entries = (unsigned int)make_gray_colormap(display); ++ ++ /* But if the input has alpha or transparency it must be removed ++ */ ++ if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || ++ png_ptr->num_trans > 0) ++ { ++ png_color_16 c; ++ png_uint_32 gray = back_g; ++ ++ /* We need to ensure that the application background exists in ++ * the colormap and that completely transparent pixels map to ++ * it. Achieve this simply by ensuring that the entry ++ * selected for the background really is the background color. ++ */ ++ if (data_encoding == P_FILE) /* from the fixup above */ ++ { ++ /* The app supplied a gray which is in output_encoding, we ++ * need to convert it to a value of the input (P_FILE) ++ * encoding then set this palette entry to the required ++ * output encoding. ++ */ ++ if (output_encoding == P_sRGB) ++ gray = png_sRGB_table[gray]; /* now P_LINEAR */ ++ ++ gray = PNG_DIV257(png_gamma_16bit_correct(gray, ++ png_ptr->colorspace.gamma)); /* now P_FILE */ ++ ++ /* And make sure the corresponding palette entry contains ++ * exactly the required sRGB value. ++ */ ++ png_create_colormap_entry(display, gray, back_g, back_g, ++ back_g, 0/*unused*/, output_encoding); ++ } ++ ++ else if (output_encoding == P_LINEAR) ++ { ++ gray = PNG_sRGB_FROM_LINEAR(gray * 255); ++ ++ /* And make sure the corresponding palette entry matches. ++ */ ++ png_create_colormap_entry(display, gray, back_g, back_g, ++ back_g, 0/*unused*/, P_LINEAR); ++ } ++ ++ /* The background passed to libpng, however, must be the ++ * output (normally sRGB) value. ++ */ ++ c.index = 0; /*unused*/ ++ c.gray = c.red = c.green = c.blue = (png_uint_16)gray; ++ ++ /* NOTE: the following is apparently a bug in libpng. Without ++ * it the transparent color recognition in ++ * png_set_background_fixed seems to go wrong. ++ */ ++ expand_tRNS = 1; ++ png_set_background_fixed(png_ptr, &c, ++ PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, ++ 0/*gamma: not used*/); ++ } ++ ++ output_processing = PNG_CMAP_NONE; ++ } ++ } ++ ++ else /* output is color */ ++ { ++ /* We could use png_quantize here so long as there is no transparent ++ * color or alpha; png_quantize ignores alpha. Easier overall just ++ * to do it once and using PNG_DIV51 on the 6x6x6 reduced RGB cube. ++ * Consequently we always want libpng to produce sRGB data. ++ */ ++ data_encoding = P_sRGB; ++ ++ /* Is there any transparency or alpha? */ ++ if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA || ++ png_ptr->num_trans > 0) ++ { ++ /* Is there alpha in the output too? If so all four channels are ++ * processed into a special RGB cube with alpha support. ++ */ ++ if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0) ++ { ++ png_uint_32 r; ++ ++ if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries) ++ png_error(png_ptr, "rgb+alpha color-map: too few entries"); ++ ++ cmap_entries = (unsigned int)make_rgb_colormap(display); ++ ++ /* Add a transparent entry. */ ++ png_create_colormap_entry(display, cmap_entries, 255, 255, ++ 255, 0, P_sRGB); ++ ++ /* This is stored as the background index for the processing ++ * algorithm. ++ */ ++ background_index = cmap_entries++; ++ ++ /* Add 27 r,g,b entries each with alpha 0.5. */ ++ for (r=0; r<256; r = (r << 1) | 0x7f) ++ { ++ png_uint_32 g; ++ ++ for (g=0; g<256; g = (g << 1) | 0x7f) ++ { ++ png_uint_32 b; ++ ++ /* This generates components with the values 0, 127 and ++ * 255 ++ */ ++ for (b=0; b<256; b = (b << 1) | 0x7f) ++ png_create_colormap_entry(display, cmap_entries++, ++ r, g, b, 128, P_sRGB); ++ } ++ } ++ ++ expand_tRNS = 1; ++ output_processing = PNG_CMAP_RGB_ALPHA; ++ } ++ ++ else ++ { ++ /* Alpha/transparency must be removed. The background must ++ * exist in the color map (achieved by setting adding it after ++ * the 666 color-map). If the standard processing code will ++ * pick up this entry automatically that's all that is ++ * required; libpng can be called to do the background ++ * processing. ++ */ ++ unsigned int sample_size = ++ PNG_IMAGE_SAMPLE_SIZE(output_format); ++ png_uint_32 r, g, b; /* sRGB background */ ++ ++ if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries) ++ png_error(png_ptr, "rgb-alpha color-map: too few entries"); ++ ++ cmap_entries = (unsigned int)make_rgb_colormap(display); ++ ++ png_create_colormap_entry(display, cmap_entries, back_r, ++ back_g, back_b, 0/*unused*/, output_encoding); ++ ++ if (output_encoding == P_LINEAR) ++ { ++ r = PNG_sRGB_FROM_LINEAR(back_r * 255); ++ g = PNG_sRGB_FROM_LINEAR(back_g * 255); ++ b = PNG_sRGB_FROM_LINEAR(back_b * 255); ++ } ++ ++ else ++ { ++ r = back_r; ++ g = back_g; ++ b = back_g; ++ } ++ ++ /* Compare the newly-created color-map entry with the one the ++ * PNG_CMAP_RGB algorithm will use. If the two entries don't ++ * match, add the new one and set this as the background ++ * index. ++ */ ++ if (memcmp((png_const_bytep)display->colormap + ++ sample_size * cmap_entries, ++ (png_const_bytep)display->colormap + ++ sample_size * PNG_RGB_INDEX(r,g,b), ++ sample_size) != 0) ++ { ++ /* The background color must be added. */ ++ background_index = cmap_entries++; ++ ++ /* Add 27 r,g,b entries each with created by composing with ++ * the background at alpha 0.5. ++ */ ++ for (r=0; r<256; r = (r << 1) | 0x7f) ++ { ++ for (g=0; g<256; g = (g << 1) | 0x7f) ++ { ++ /* This generates components with the values 0, 127 ++ * and 255 ++ */ ++ for (b=0; b<256; b = (b << 1) | 0x7f) ++ png_create_colormap_entry(display, cmap_entries++, ++ png_colormap_compose(display, r, P_sRGB, 128, ++ back_r, output_encoding), ++ png_colormap_compose(display, g, P_sRGB, 128, ++ back_g, output_encoding), ++ png_colormap_compose(display, b, P_sRGB, 128, ++ back_b, output_encoding), ++ 0/*unused*/, output_encoding); ++ } ++ } ++ ++ expand_tRNS = 1; ++ output_processing = PNG_CMAP_RGB_ALPHA; ++ } ++ ++ else /* background color is in the standard color-map */ ++ { ++ png_color_16 c; ++ ++ c.index = 0; /*unused*/ ++ c.red = (png_uint_16)back_r; ++ c.gray = c.green = (png_uint_16)back_g; ++ c.blue = (png_uint_16)back_b; ++ ++ png_set_background_fixed(png_ptr, &c, ++ PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, ++ 0/*gamma: not used*/); ++ ++ output_processing = PNG_CMAP_RGB; ++ } ++ } ++ } ++ ++ else /* no alpha or transparency in the input */ ++ { ++ /* Alpha in the output is irrelevant, simply map the opaque input ++ * pixels to the 6x6x6 color-map. ++ */ ++ if (PNG_RGB_COLORMAP_ENTRIES > image->colormap_entries) ++ png_error(png_ptr, "rgb color-map: too few entries"); ++ ++ cmap_entries = (unsigned int)make_rgb_colormap(display); ++ output_processing = PNG_CMAP_RGB; ++ } ++ } ++ break; ++ ++ case PNG_COLOR_TYPE_PALETTE: ++ /* It's already got a color-map. It may be necessary to eliminate the ++ * tRNS entries though. ++ */ ++ { ++ unsigned int num_trans = png_ptr->num_trans; ++ png_const_bytep trans = num_trans > 0 ? png_ptr->trans_alpha : NULL; ++ png_const_colorp colormap = png_ptr->palette; ++ int do_background = trans != NULL && ++ (output_format & PNG_FORMAT_FLAG_ALPHA) == 0; ++ unsigned int i; ++ ++ /* Just in case: */ ++ if (trans == NULL) ++ num_trans = 0; ++ ++ output_processing = PNG_CMAP_NONE; ++ data_encoding = P_FILE; /* Don't change from color-map indices */ ++ cmap_entries = (unsigned int)png_ptr->num_palette; ++ if (cmap_entries > 256) ++ cmap_entries = 256; ++ ++ if (cmap_entries > (unsigned int)image->colormap_entries) ++ png_error(png_ptr, "palette color-map: too few entries"); ++ ++ for (i=0; i < cmap_entries; ++i) ++ { ++ if (do_background != 0 && i < num_trans && trans[i] < 255) ++ { ++ if (trans[i] == 0) ++ png_create_colormap_entry(display, i, back_r, back_g, ++ back_b, 0, output_encoding); ++ ++ else ++ { ++ /* Must compose the PNG file color in the color-map entry ++ * on the sRGB color in 'back'. ++ */ ++ png_create_colormap_entry(display, i, ++ png_colormap_compose(display, colormap[i].red, ++ P_FILE, trans[i], back_r, output_encoding), ++ png_colormap_compose(display, colormap[i].green, ++ P_FILE, trans[i], back_g, output_encoding), ++ png_colormap_compose(display, colormap[i].blue, ++ P_FILE, trans[i], back_b, output_encoding), ++ output_encoding == P_LINEAR ? trans[i] * 257U : ++ trans[i], ++ output_encoding); ++ } ++ } ++ ++ else ++ png_create_colormap_entry(display, i, colormap[i].red, ++ colormap[i].green, colormap[i].blue, ++ i < num_trans ? trans[i] : 255U, P_FILE/*8-bit*/); ++ } ++ ++ /* The PNG data may have indices packed in fewer than 8 bits, it ++ * must be expanded if so. ++ */ ++ if (png_ptr->bit_depth < 8) ++ png_set_packing(png_ptr); ++ } ++ break; ++ ++ default: ++ png_error(png_ptr, "invalid PNG color type"); ++ /*NOT REACHED*/ ++ } ++ ++ /* Now deal with the output processing */ ++ if (expand_tRNS != 0 && png_ptr->num_trans > 0 && ++ (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) == 0) ++ png_set_tRNS_to_alpha(png_ptr); ++ ++ switch (data_encoding) ++ { ++ case P_sRGB: ++ /* Change to 8-bit sRGB */ ++ png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, PNG_GAMMA_sRGB); ++ /* FALLTHROUGH */ ++ ++ case P_FILE: ++ if (png_ptr->bit_depth > 8) ++ png_set_scale_16(png_ptr); ++ break; ++ ++#ifdef __GNUC__ ++ default: ++ png_error(png_ptr, "bad data option (internal error)"); ++#endif ++ } ++ ++ if (cmap_entries > 256 || cmap_entries > image->colormap_entries) ++ png_error(png_ptr, "color map overflow (BAD internal error)"); ++ ++ image->colormap_entries = cmap_entries; ++ ++ /* Double check using the recorded background index */ ++ switch (output_processing) ++ { ++ case PNG_CMAP_NONE: ++ if (background_index != PNG_CMAP_NONE_BACKGROUND) ++ goto bad_background; ++ break; ++ ++ case PNG_CMAP_GA: ++ if (background_index != PNG_CMAP_GA_BACKGROUND) ++ goto bad_background; ++ break; ++ ++ case PNG_CMAP_TRANS: ++ if (background_index >= cmap_entries || ++ background_index != PNG_CMAP_TRANS_BACKGROUND) ++ goto bad_background; ++ break; ++ ++ case PNG_CMAP_RGB: ++ if (background_index != PNG_CMAP_RGB_BACKGROUND) ++ goto bad_background; ++ break; ++ ++ case PNG_CMAP_RGB_ALPHA: ++ if (background_index != PNG_CMAP_RGB_ALPHA_BACKGROUND) ++ goto bad_background; ++ break; ++ ++ default: ++ png_error(png_ptr, "bad processing option (internal error)"); ++ ++ bad_background: ++ png_error(png_ptr, "bad background index (internal error)"); ++ } ++ ++ display->colormap_processing = (int)output_processing; ++ ++ return 1/*ok*/; ++} ++ ++/* The final part of the color-map read called from png_image_finish_read. */ ++static int ++png_image_read_and_map(png_voidp argument) ++{ ++ png_image_read_control *display = png_voidcast(png_image_read_control*, ++ argument); ++ png_imagep image = display->image; ++ png_structrp png_ptr = image->opaque->png_ptr; ++ int passes; ++ ++ /* Called when the libpng data must be transformed into the color-mapped ++ * form. There is a local row buffer in display->local and this routine must ++ * do the interlace handling. ++ */ ++ switch (png_ptr->interlaced) ++ { ++ case PNG_INTERLACE_NONE: ++ passes = 1; ++ break; ++ ++ case PNG_INTERLACE_ADAM7: ++ passes = PNG_INTERLACE_ADAM7_PASSES; ++ break; ++ ++ default: ++ png_error(png_ptr, "unknown interlace type"); ++ } ++ ++ { ++ png_uint_32 height = image->height; ++ png_uint_32 width = image->width; ++ int proc = display->colormap_processing; ++ png_bytep first_row = png_voidcast(png_bytep, display->first_row); ++ ptrdiff_t step_row = display->row_bytes; ++ int pass; ++ ++ for (pass = 0; pass < passes; ++pass) ++ { ++ unsigned int startx, stepx, stepy; ++ png_uint_32 y; ++ ++ if (png_ptr->interlaced == PNG_INTERLACE_ADAM7) ++ { ++ /* The row may be empty for a short image: */ ++ if (PNG_PASS_COLS(width, pass) == 0) ++ continue; ++ ++ startx = PNG_PASS_START_COL(pass); ++ stepx = PNG_PASS_COL_OFFSET(pass); ++ y = PNG_PASS_START_ROW(pass); ++ stepy = PNG_PASS_ROW_OFFSET(pass); ++ } ++ ++ else ++ { ++ y = 0; ++ startx = 0; ++ stepx = stepy = 1; ++ } ++ ++ for (; ylocal_row); ++ png_bytep outrow = first_row + y * step_row; ++ png_const_bytep end_row = outrow + width; ++ ++ /* Read read the libpng data into the temporary buffer. */ ++ png_read_row(png_ptr, inrow, NULL); ++ ++ /* Now process the row according to the processing option, note ++ * that the caller verifies that the format of the libpng output ++ * data is as required. ++ */ ++ outrow += startx; ++ switch (proc) ++ { ++ case PNG_CMAP_GA: ++ for (; outrow < end_row; outrow += stepx) ++ { ++ /* The data is always in the PNG order */ ++ unsigned int gray = *inrow++; ++ unsigned int alpha = *inrow++; ++ unsigned int entry; ++ ++ /* NOTE: this code is copied as a comment in ++ * make_ga_colormap above. Please update the ++ * comment if you change this code! ++ */ ++ if (alpha > 229) /* opaque */ ++ { ++ entry = (231 * gray + 128) >> 8; ++ } ++ else if (alpha < 26) /* transparent */ ++ { ++ entry = 231; ++ } ++ else /* partially opaque */ ++ { ++ entry = 226 + 6 * PNG_DIV51(alpha) + PNG_DIV51(gray); ++ } ++ ++ *outrow = (png_byte)entry; ++ } ++ break; ++ ++ case PNG_CMAP_TRANS: ++ for (; outrow < end_row; outrow += stepx) ++ { ++ png_byte gray = *inrow++; ++ png_byte alpha = *inrow++; ++ ++ if (alpha == 0) ++ *outrow = PNG_CMAP_TRANS_BACKGROUND; ++ ++ else if (gray != PNG_CMAP_TRANS_BACKGROUND) ++ *outrow = gray; ++ ++ else ++ *outrow = (png_byte)(PNG_CMAP_TRANS_BACKGROUND+1); ++ } ++ break; ++ ++ case PNG_CMAP_RGB: ++ for (; outrow < end_row; outrow += stepx) ++ { ++ *outrow = PNG_RGB_INDEX(inrow[0], inrow[1], inrow[2]); ++ inrow += 3; ++ } ++ break; ++ ++ case PNG_CMAP_RGB_ALPHA: ++ for (; outrow < end_row; outrow += stepx) ++ { ++ unsigned int alpha = inrow[3]; ++ ++ /* Because the alpha entries only hold alpha==0.5 values ++ * split the processing at alpha==0.25 (64) and 0.75 ++ * (196). ++ */ ++ ++ if (alpha >= 196) ++ *outrow = PNG_RGB_INDEX(inrow[0], inrow[1], ++ inrow[2]); ++ ++ else if (alpha < 64) ++ *outrow = PNG_CMAP_RGB_ALPHA_BACKGROUND; ++ ++ else ++ { ++ /* Likewise there are three entries for each of r, g ++ * and b. We could select the entry by popcount on ++ * the top two bits on those architectures that ++ * support it, this is what the code below does, ++ * crudely. ++ */ ++ unsigned int back_i = PNG_CMAP_RGB_ALPHA_BACKGROUND+1; ++ ++ /* Here are how the values map: ++ * ++ * 0x00 .. 0x3f -> 0 ++ * 0x40 .. 0xbf -> 1 ++ * 0xc0 .. 0xff -> 2 ++ * ++ * So, as above with the explicit alpha checks, the ++ * breakpoints are at 64 and 196. ++ */ ++ if (inrow[0] & 0x80) back_i += 9; /* red */ ++ if (inrow[0] & 0x40) back_i += 9; ++ if (inrow[0] & 0x80) back_i += 3; /* green */ ++ if (inrow[0] & 0x40) back_i += 3; ++ if (inrow[0] & 0x80) back_i += 1; /* blue */ ++ if (inrow[0] & 0x40) back_i += 1; ++ ++ *outrow = (png_byte)back_i; ++ } ++ ++ inrow += 4; ++ } ++ break; ++ ++ default: ++ break; ++ } ++ } ++ } ++ } ++ ++ return 1; ++} ++ ++static int ++png_image_read_colormapped(png_voidp argument) ++{ ++ png_image_read_control *display = png_voidcast(png_image_read_control*, ++ argument); ++ png_imagep image = display->image; ++ png_controlp control = image->opaque; ++ png_structrp png_ptr = control->png_ptr; ++ png_inforp info_ptr = control->info_ptr; ++ ++ int passes = 0; /* As a flag */ ++ ++ PNG_SKIP_CHUNKS(png_ptr); ++ ++ /* Update the 'info' structure and make sure the result is as required; first ++ * make sure to turn on the interlace handling if it will be required ++ * (because it can't be turned on *after* the call to png_read_update_info!) ++ */ ++ if (display->colormap_processing == PNG_CMAP_NONE) ++ passes = png_set_interlace_handling(png_ptr); ++ ++ png_read_update_info(png_ptr, info_ptr); ++ ++ /* The expected output can be deduced from the colormap_processing option. */ ++ switch (display->colormap_processing) ++ { ++ case PNG_CMAP_NONE: ++ /* Output must be one channel and one byte per pixel, the output ++ * encoding can be anything. ++ */ ++ if ((info_ptr->color_type == PNG_COLOR_TYPE_PALETTE || ++ info_ptr->color_type == PNG_COLOR_TYPE_GRAY) && ++ info_ptr->bit_depth == 8) ++ break; ++ ++ goto bad_output; ++ ++ case PNG_CMAP_TRANS: ++ case PNG_CMAP_GA: ++ /* Output must be two channels and the 'G' one must be sRGB, the latter ++ * can be checked with an exact number because it should have been set ++ * to this number above! ++ */ ++ if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA && ++ info_ptr->bit_depth == 8 && ++ png_ptr->screen_gamma == PNG_GAMMA_sRGB && ++ image->colormap_entries == 256) ++ break; ++ ++ goto bad_output; ++ ++ case PNG_CMAP_RGB: ++ /* Output must be 8-bit sRGB encoded RGB */ ++ if (info_ptr->color_type == PNG_COLOR_TYPE_RGB && ++ info_ptr->bit_depth == 8 && ++ png_ptr->screen_gamma == PNG_GAMMA_sRGB && ++ image->colormap_entries == 216) ++ break; ++ ++ goto bad_output; ++ ++ case PNG_CMAP_RGB_ALPHA: ++ /* Output must be 8-bit sRGB encoded RGBA */ ++ if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA && ++ info_ptr->bit_depth == 8 && ++ png_ptr->screen_gamma == PNG_GAMMA_sRGB && ++ image->colormap_entries == 244 /* 216 + 1 + 27 */) ++ break; ++ ++ goto bad_output; ++ ++ default: ++ bad_output: ++ png_error(png_ptr, "bad color-map processing (internal error)"); ++ } ++ ++ /* Now read the rows. Do this here if it is possible to read directly into ++ * the output buffer, otherwise allocate a local row buffer of the maximum ++ * size libpng requires and call the relevant processing routine safely. ++ */ ++ { ++ png_voidp first_row = display->buffer; ++ ptrdiff_t row_bytes = display->row_stride; ++ ++ /* The following expression is designed to work correctly whether it gives ++ * a signed or an unsigned result. ++ */ ++ if (row_bytes < 0) ++ { ++ char *ptr = png_voidcast(char*, first_row); ++ ptr += (image->height-1) * (-row_bytes); ++ first_row = png_voidcast(png_voidp, ptr); ++ } ++ ++ display->first_row = first_row; ++ display->row_bytes = row_bytes; ++ } ++ ++ if (passes == 0) ++ { ++ int result; ++ png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr)); ++ ++ display->local_row = row; ++ result = png_safe_execute(image, png_image_read_and_map, display); ++ display->local_row = NULL; ++ png_free(png_ptr, row); ++ ++ return result; ++ } ++ ++ else ++ { ++ png_alloc_size_t row_bytes = (png_alloc_size_t)display->row_bytes; ++ ++ while (--passes >= 0) ++ { ++ png_uint_32 y = image->height; ++ png_bytep row = png_voidcast(png_bytep, display->first_row); ++ ++ for (; y > 0; --y) ++ { ++ png_read_row(png_ptr, row, NULL); ++ row += row_bytes; ++ } ++ } ++ ++ return 1; ++ } ++} ++ ++/* Just the row reading part of png_image_read. */ ++static int ++png_image_read_composite(png_voidp argument) ++{ ++ png_image_read_control *display = png_voidcast(png_image_read_control*, ++ argument); ++ png_imagep image = display->image; ++ png_structrp png_ptr = image->opaque->png_ptr; ++ int passes; ++ ++ switch (png_ptr->interlaced) ++ { ++ case PNG_INTERLACE_NONE: ++ passes = 1; ++ break; ++ ++ case PNG_INTERLACE_ADAM7: ++ passes = PNG_INTERLACE_ADAM7_PASSES; ++ break; ++ ++ default: ++ png_error(png_ptr, "unknown interlace type"); ++ } ++ ++ { ++ png_uint_32 height = image->height; ++ png_uint_32 width = image->width; ++ ptrdiff_t step_row = display->row_bytes; ++ unsigned int channels = ++ (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ? 3 : 1; ++ int pass; ++ ++ for (pass = 0; pass < passes; ++pass) ++ { ++ unsigned int startx, stepx, stepy; ++ png_uint_32 y; ++ ++ if (png_ptr->interlaced == PNG_INTERLACE_ADAM7) ++ { ++ /* The row may be empty for a short image: */ ++ if (PNG_PASS_COLS(width, pass) == 0) ++ continue; ++ ++ startx = PNG_PASS_START_COL(pass) * channels; ++ stepx = PNG_PASS_COL_OFFSET(pass) * channels; ++ y = PNG_PASS_START_ROW(pass); ++ stepy = PNG_PASS_ROW_OFFSET(pass); ++ } ++ ++ else ++ { ++ y = 0; ++ startx = 0; ++ stepx = channels; ++ stepy = 1; ++ } ++ ++ for (; ylocal_row); ++ png_bytep outrow; ++ png_const_bytep end_row; ++ ++ /* Read the row, which is packed: */ ++ png_read_row(png_ptr, inrow, NULL); ++ ++ outrow = png_voidcast(png_bytep, display->first_row); ++ outrow += y * step_row; ++ end_row = outrow + width * channels; ++ ++ /* Now do the composition on each pixel in this row. */ ++ outrow += startx; ++ for (; outrow < end_row; outrow += stepx) ++ { ++ png_byte alpha = inrow[channels]; ++ ++ if (alpha > 0) /* else no change to the output */ ++ { ++ unsigned int c; ++ ++ for (c=0; cimage; ++ png_structrp png_ptr = image->opaque->png_ptr; ++ png_inforp info_ptr = image->opaque->info_ptr; ++ png_uint_32 height = image->height; ++ png_uint_32 width = image->width; ++ int pass, passes; ++ ++ /* Double check the convoluted logic below. We expect to get here with ++ * libpng doing rgb to gray and gamma correction but background processing ++ * left to the png_image_read_background function. The rows libpng produce ++ * might be 8 or 16-bit but should always have two channels; gray plus alpha. ++ */ ++ if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == 0) ++ png_error(png_ptr, "lost rgb to gray"); ++ ++ if ((png_ptr->transformations & PNG_COMPOSE) != 0) ++ png_error(png_ptr, "unexpected compose"); ++ ++ if (png_get_channels(png_ptr, info_ptr) != 2) ++ png_error(png_ptr, "lost/gained channels"); ++ ++ /* Expect the 8-bit case to always remove the alpha channel */ ++ if ((image->format & PNG_FORMAT_FLAG_LINEAR) == 0 && ++ (image->format & PNG_FORMAT_FLAG_ALPHA) != 0) ++ png_error(png_ptr, "unexpected 8-bit transformation"); ++ ++ switch (png_ptr->interlaced) ++ { ++ case PNG_INTERLACE_NONE: ++ passes = 1; ++ break; ++ ++ case PNG_INTERLACE_ADAM7: ++ passes = PNG_INTERLACE_ADAM7_PASSES; ++ break; ++ ++ default: ++ png_error(png_ptr, "unknown interlace type"); ++ } ++ ++ /* Use direct access to info_ptr here because otherwise the simplified API ++ * would require PNG_EASY_ACCESS_SUPPORTED (just for this.) Note this is ++ * checking the value after libpng expansions, not the original value in the ++ * PNG. ++ */ ++ switch (info_ptr->bit_depth) ++ { ++ case 8: ++ /* 8-bit sRGB gray values with an alpha channel; the alpha channel is ++ * to be removed by composing on a background: either the row if ++ * display->background is NULL or display->background->green if not. ++ * Unlike the code above ALPHA_OPTIMIZED has *not* been done. ++ */ ++ { ++ png_bytep first_row = png_voidcast(png_bytep, display->first_row); ++ ptrdiff_t step_row = display->row_bytes; ++ ++ for (pass = 0; pass < passes; ++pass) ++ { ++ png_bytep row = png_voidcast(png_bytep, display->first_row); ++ unsigned int startx, stepx, stepy; ++ png_uint_32 y; ++ ++ if (png_ptr->interlaced == PNG_INTERLACE_ADAM7) ++ { ++ /* The row may be empty for a short image: */ ++ if (PNG_PASS_COLS(width, pass) == 0) ++ continue; ++ ++ startx = PNG_PASS_START_COL(pass); ++ stepx = PNG_PASS_COL_OFFSET(pass); ++ y = PNG_PASS_START_ROW(pass); ++ stepy = PNG_PASS_ROW_OFFSET(pass); ++ } ++ ++ else ++ { ++ y = 0; ++ startx = 0; ++ stepx = stepy = 1; ++ } ++ ++ if (display->background == NULL) ++ { ++ for (; ylocal_row); ++ png_bytep outrow = first_row + y * step_row; ++ png_const_bytep end_row = outrow + width; ++ ++ /* Read the row, which is packed: */ ++ png_read_row(png_ptr, inrow, NULL); ++ ++ /* Now do the composition on each pixel in this row. */ ++ outrow += startx; ++ for (; outrow < end_row; outrow += stepx) ++ { ++ png_byte alpha = inrow[1]; ++ ++ if (alpha > 0) /* else no change to the output */ ++ { ++ png_uint_32 component = inrow[0]; ++ ++ if (alpha < 255) /* else just use component */ ++ { ++ /* Since PNG_OPTIMIZED_ALPHA was not set it is ++ * necessary to invert the sRGB transfer ++ * function and multiply the alpha out. ++ */ ++ component = png_sRGB_table[component] * alpha; ++ component += png_sRGB_table[outrow[0]] * ++ (255-alpha); ++ component = PNG_sRGB_FROM_LINEAR(component); ++ } ++ ++ outrow[0] = (png_byte)component; ++ } ++ ++ inrow += 2; /* gray and alpha channel */ ++ } ++ } ++ } ++ ++ else /* constant background value */ ++ { ++ png_byte background8 = display->background->green; ++ png_uint_16 background = png_sRGB_table[background8]; ++ ++ for (; ylocal_row); ++ png_bytep outrow = first_row + y * step_row; ++ png_const_bytep end_row = outrow + width; ++ ++ /* Read the row, which is packed: */ ++ png_read_row(png_ptr, inrow, NULL); ++ ++ /* Now do the composition on each pixel in this row. */ ++ outrow += startx; ++ for (; outrow < end_row; outrow += stepx) ++ { ++ png_byte alpha = inrow[1]; ++ ++ if (alpha > 0) /* else use background */ ++ { ++ png_uint_32 component = inrow[0]; ++ ++ if (alpha < 255) /* else just use component */ ++ { ++ component = png_sRGB_table[component] * alpha; ++ component += background * (255-alpha); ++ component = PNG_sRGB_FROM_LINEAR(component); ++ } ++ ++ outrow[0] = (png_byte)component; ++ } ++ ++ else ++ outrow[0] = background8; ++ ++ inrow += 2; /* gray and alpha channel */ ++ } ++ ++ row += display->row_bytes; ++ } ++ } ++ } ++ } ++ break; ++ ++ case 16: ++ /* 16-bit linear with pre-multiplied alpha; the pre-multiplication must ++ * still be done and, maybe, the alpha channel removed. This code also ++ * handles the alpha-first option. ++ */ ++ { ++ png_uint_16p first_row = png_voidcast(png_uint_16p, ++ display->first_row); ++ /* The division by two is safe because the caller passed in a ++ * stride which was multiplied by 2 (below) to get row_bytes. ++ */ ++ ptrdiff_t step_row = display->row_bytes / 2; ++ unsigned int preserve_alpha = (image->format & ++ PNG_FORMAT_FLAG_ALPHA) != 0; ++ unsigned int outchannels = 1U+preserve_alpha; ++ int swap_alpha = 0; ++ ++# ifdef PNG_SIMPLIFIED_READ_AFIRST_SUPPORTED ++ if (preserve_alpha != 0 && ++ (image->format & PNG_FORMAT_FLAG_AFIRST) != 0) ++ swap_alpha = 1; ++# endif ++ ++ for (pass = 0; pass < passes; ++pass) ++ { ++ unsigned int startx, stepx, stepy; ++ png_uint_32 y; ++ ++ /* The 'x' start and step are adjusted to output components here. ++ */ ++ if (png_ptr->interlaced == PNG_INTERLACE_ADAM7) ++ { ++ /* The row may be empty for a short image: */ ++ if (PNG_PASS_COLS(width, pass) == 0) ++ continue; ++ ++ startx = PNG_PASS_START_COL(pass) * outchannels; ++ stepx = PNG_PASS_COL_OFFSET(pass) * outchannels; ++ y = PNG_PASS_START_ROW(pass); ++ stepy = PNG_PASS_ROW_OFFSET(pass); ++ } ++ ++ else ++ { ++ y = 0; ++ startx = 0; ++ stepx = outchannels; ++ stepy = 1; ++ } ++ ++ for (; ylocal_row), NULL); ++ inrow = png_voidcast(png_const_uint_16p, display->local_row); ++ ++ /* Now do the pre-multiplication on each pixel in this row. ++ */ ++ outrow += startx; ++ for (; outrow < end_row; outrow += stepx) ++ { ++ png_uint_32 component = inrow[0]; ++ png_uint_16 alpha = inrow[1]; ++ ++ if (alpha > 0) /* else 0 */ ++ { ++ if (alpha < 65535) /* else just use component */ ++ { ++ component *= alpha; ++ component += 32767; ++ component /= 65535; ++ } ++ } ++ ++ else ++ component = 0; ++ ++ outrow[swap_alpha] = (png_uint_16)component; ++ if (preserve_alpha != 0) ++ outrow[1 ^ swap_alpha] = alpha; ++ ++ inrow += 2; /* components and alpha channel */ ++ } ++ } ++ } ++ } ++ break; ++ ++#ifdef __GNUC__ ++ default: ++ png_error(png_ptr, "unexpected bit depth"); ++#endif ++ } ++ ++ return 1; ++} ++ ++/* The guts of png_image_finish_read as a png_safe_execute callback. */ ++static int ++png_image_read_direct(png_voidp argument) ++{ ++ png_image_read_control *display = png_voidcast(png_image_read_control*, ++ argument); ++ png_imagep image = display->image; ++ png_structrp png_ptr = image->opaque->png_ptr; ++ png_inforp info_ptr = image->opaque->info_ptr; ++ ++ png_uint_32 format = image->format; ++ int linear = (format & PNG_FORMAT_FLAG_LINEAR) != 0; ++ int do_local_compose = 0; ++ int do_local_background = 0; /* to avoid double gamma correction bug */ ++ int passes = 0; ++ ++ /* Add transforms to ensure the correct output format is produced then check ++ * that the required implementation support is there. Always expand; always ++ * need 8 bits minimum, no palette and expanded tRNS. ++ */ ++ png_set_expand(png_ptr); ++ ++ /* Now check the format to see if it was modified. */ ++ { ++ png_uint_32 base_format = png_image_format(png_ptr) & ++ ~PNG_FORMAT_FLAG_COLORMAP /* removed by png_set_expand */; ++ png_uint_32 change = format ^ base_format; ++ png_fixed_point output_gamma; ++ int mode; /* alpha mode */ ++ ++ /* Do this first so that we have a record if rgb to gray is happening. */ ++ if ((change & PNG_FORMAT_FLAG_COLOR) != 0) ++ { ++ /* gray<->color transformation required. */ ++ if ((format & PNG_FORMAT_FLAG_COLOR) != 0) ++ png_set_gray_to_rgb(png_ptr); ++ ++ else ++ { ++ /* libpng can't do both rgb to gray and ++ * background/pre-multiplication if there is also significant gamma ++ * correction, because both operations require linear colors and ++ * the code only supports one transform doing the gamma correction. ++ * Handle this by doing the pre-multiplication or background ++ * operation in this code, if necessary. ++ * ++ * TODO: fix this by rewriting pngrtran.c (!) ++ * ++ * For the moment (given that fixing this in pngrtran.c is an ++ * enormous change) 'do_local_background' is used to indicate that ++ * the problem exists. ++ */ ++ if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0) ++ do_local_background = 1/*maybe*/; ++ ++ png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, ++ PNG_RGB_TO_GRAY_DEFAULT, PNG_RGB_TO_GRAY_DEFAULT); ++ } ++ ++ change &= ~PNG_FORMAT_FLAG_COLOR; ++ } ++ ++ /* Set the gamma appropriately, linear for 16-bit input, sRGB otherwise. ++ */ ++ { ++ png_fixed_point input_gamma_default; ++ ++ if ((base_format & PNG_FORMAT_FLAG_LINEAR) != 0 && ++ (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0) ++ input_gamma_default = PNG_GAMMA_LINEAR; ++ else ++ input_gamma_default = PNG_DEFAULT_sRGB; ++ ++ /* Call png_set_alpha_mode to set the default for the input gamma; the ++ * output gamma is set by a second call below. ++ */ ++ png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, input_gamma_default); ++ } ++ ++ if (linear != 0) ++ { ++ /* If there *is* an alpha channel in the input it must be multiplied ++ * out; use PNG_ALPHA_STANDARD, otherwise just use PNG_ALPHA_PNG. ++ */ ++ if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0) ++ mode = PNG_ALPHA_STANDARD; /* associated alpha */ ++ ++ else ++ mode = PNG_ALPHA_PNG; ++ ++ output_gamma = PNG_GAMMA_LINEAR; ++ } ++ ++ else ++ { ++ mode = PNG_ALPHA_PNG; ++ output_gamma = PNG_DEFAULT_sRGB; ++ } ++ ++ if ((change & PNG_FORMAT_FLAG_ASSOCIATED_ALPHA) != 0) ++ { ++ mode = PNG_ALPHA_OPTIMIZED; ++ change &= ~PNG_FORMAT_FLAG_ASSOCIATED_ALPHA; ++ } ++ ++ /* If 'do_local_background' is set check for the presence of gamma ++ * correction; this is part of the work-round for the libpng bug ++ * described above. ++ * ++ * TODO: fix libpng and remove this. ++ */ ++ if (do_local_background != 0) ++ { ++ png_fixed_point gtest; ++ ++ /* This is 'png_gamma_threshold' from pngrtran.c; the test used for ++ * gamma correction, the screen gamma hasn't been set on png_struct ++ * yet; it's set below. png_struct::gamma, however, is set to the ++ * final value. ++ */ ++ if (png_muldiv(>est, output_gamma, png_ptr->colorspace.gamma, ++ PNG_FP_1) != 0 && png_gamma_significant(gtest) == 0) ++ do_local_background = 0; ++ ++ else if (mode == PNG_ALPHA_STANDARD) ++ { ++ do_local_background = 2/*required*/; ++ mode = PNG_ALPHA_PNG; /* prevent libpng doing it */ ++ } ++ ++ /* else leave as 1 for the checks below */ ++ } ++ ++ /* If the bit-depth changes then handle that here. */ ++ if ((change & PNG_FORMAT_FLAG_LINEAR) != 0) ++ { ++ if (linear != 0 /*16-bit output*/) ++ png_set_expand_16(png_ptr); ++ ++ else /* 8-bit output */ ++ png_set_scale_16(png_ptr); ++ ++ change &= ~PNG_FORMAT_FLAG_LINEAR; ++ } ++ ++ /* Now the background/alpha channel changes. */ ++ if ((change & PNG_FORMAT_FLAG_ALPHA) != 0) ++ { ++ /* Removing an alpha channel requires composition for the 8-bit ++ * formats; for the 16-bit it is already done, above, by the ++ * pre-multiplication and the channel just needs to be stripped. ++ */ ++ if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0) ++ { ++ /* If RGB->gray is happening the alpha channel must be left and the ++ * operation completed locally. ++ * ++ * TODO: fix libpng and remove this. ++ */ ++ if (do_local_background != 0) ++ do_local_background = 2/*required*/; ++ ++ /* 16-bit output: just remove the channel */ ++ else if (linear != 0) /* compose on black (well, pre-multiply) */ ++ png_set_strip_alpha(png_ptr); ++ ++ /* 8-bit output: do an appropriate compose */ ++ else if (display->background != NULL) ++ { ++ png_color_16 c; ++ ++ c.index = 0; /*unused*/ ++ c.red = display->background->red; ++ c.green = display->background->green; ++ c.blue = display->background->blue; ++ c.gray = display->background->green; ++ ++ /* This is always an 8-bit sRGB value, using the 'green' channel ++ * for gray is much better than calculating the luminance here; ++ * we can get off-by-one errors in that calculation relative to ++ * the app expectations and that will show up in transparent ++ * pixels. ++ */ ++ png_set_background_fixed(png_ptr, &c, ++ PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/, ++ 0/*gamma: not used*/); ++ } ++ ++ else /* compose on row: implemented below. */ ++ { ++ do_local_compose = 1; ++ /* This leaves the alpha channel in the output, so it has to be ++ * removed by the code below. Set the encoding to the 'OPTIMIZE' ++ * one so the code only has to hack on the pixels that require ++ * composition. ++ */ ++ mode = PNG_ALPHA_OPTIMIZED; ++ } ++ } ++ ++ else /* output needs an alpha channel */ ++ { ++ /* This is tricky because it happens before the swap operation has ++ * been accomplished; however, the swap does *not* swap the added ++ * alpha channel (weird API), so it must be added in the correct ++ * place. ++ */ ++ png_uint_32 filler; /* opaque filler */ ++ int where; ++ ++ if (linear != 0) ++ filler = 65535; ++ ++ else ++ filler = 255; ++ ++#ifdef PNG_FORMAT_AFIRST_SUPPORTED ++ if ((format & PNG_FORMAT_FLAG_AFIRST) != 0) ++ { ++ where = PNG_FILLER_BEFORE; ++ change &= ~PNG_FORMAT_FLAG_AFIRST; ++ } ++ ++ else ++#endif ++ where = PNG_FILLER_AFTER; ++ ++ png_set_add_alpha(png_ptr, filler, where); ++ } ++ ++ /* This stops the (irrelevant) call to swap_alpha below. */ ++ change &= ~PNG_FORMAT_FLAG_ALPHA; ++ } ++ ++ /* Now set the alpha mode correctly; this is always done, even if there is ++ * no alpha channel in either the input or the output because it correctly ++ * sets the output gamma. ++ */ ++ png_set_alpha_mode_fixed(png_ptr, mode, output_gamma); ++ ++# ifdef PNG_FORMAT_BGR_SUPPORTED ++ if ((change & PNG_FORMAT_FLAG_BGR) != 0) ++ { ++ /* Check only the output format; PNG is never BGR; don't do this if ++ * the output is gray, but fix up the 'format' value in that case. ++ */ ++ if ((format & PNG_FORMAT_FLAG_COLOR) != 0) ++ png_set_bgr(png_ptr); ++ ++ else ++ format &= ~PNG_FORMAT_FLAG_BGR; ++ ++ change &= ~PNG_FORMAT_FLAG_BGR; ++ } ++# endif ++ ++# ifdef PNG_FORMAT_AFIRST_SUPPORTED ++ if ((change & PNG_FORMAT_FLAG_AFIRST) != 0) ++ { ++ /* Only relevant if there is an alpha channel - it's particularly ++ * important to handle this correctly because do_local_compose may ++ * be set above and then libpng will keep the alpha channel for this ++ * code to remove. ++ */ ++ if ((format & PNG_FORMAT_FLAG_ALPHA) != 0) ++ { ++ /* Disable this if doing a local background, ++ * TODO: remove this when local background is no longer required. ++ */ ++ if (do_local_background != 2) ++ png_set_swap_alpha(png_ptr); ++ } ++ ++ else ++ format &= ~PNG_FORMAT_FLAG_AFIRST; ++ ++ change &= ~PNG_FORMAT_FLAG_AFIRST; ++ } ++# endif ++ ++ /* If the *output* is 16-bit then we need to check for a byte-swap on this ++ * architecture. ++ */ ++ if (linear != 0) ++ { ++ png_uint_16 le = 0x0001; ++ ++ if ((*(png_const_bytep) & le) != 0) ++ png_set_swap(png_ptr); ++ } ++ ++ /* If change is not now 0 some transformation is missing - error out. */ ++ if (change != 0) ++ png_error(png_ptr, "png_read_image: unsupported transformation"); ++ } ++ ++ PNG_SKIP_CHUNKS(png_ptr); ++ ++ /* Update the 'info' structure and make sure the result is as required; first ++ * make sure to turn on the interlace handling if it will be required ++ * (because it can't be turned on *after* the call to png_read_update_info!) ++ * ++ * TODO: remove the do_local_background fixup below. ++ */ ++ if (do_local_compose == 0 && do_local_background != 2) ++ passes = png_set_interlace_handling(png_ptr); ++ ++ png_read_update_info(png_ptr, info_ptr); ++ ++ { ++ png_uint_32 info_format = 0; ++ ++ if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0) ++ info_format |= PNG_FORMAT_FLAG_COLOR; ++ ++ if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0) ++ { ++ /* do_local_compose removes this channel below. */ ++ if (do_local_compose == 0) ++ { ++ /* do_local_background does the same if required. */ ++ if (do_local_background != 2 || ++ (format & PNG_FORMAT_FLAG_ALPHA) != 0) ++ info_format |= PNG_FORMAT_FLAG_ALPHA; ++ } ++ } ++ ++ else if (do_local_compose != 0) /* internal error */ ++ png_error(png_ptr, "png_image_read: alpha channel lost"); ++ ++ if ((format & PNG_FORMAT_FLAG_ASSOCIATED_ALPHA) != 0) { ++ info_format |= PNG_FORMAT_FLAG_ASSOCIATED_ALPHA; ++ } ++ ++ if (info_ptr->bit_depth == 16) ++ info_format |= PNG_FORMAT_FLAG_LINEAR; ++ ++#ifdef PNG_FORMAT_BGR_SUPPORTED ++ if ((png_ptr->transformations & PNG_BGR) != 0) ++ info_format |= PNG_FORMAT_FLAG_BGR; ++#endif ++ ++#ifdef PNG_FORMAT_AFIRST_SUPPORTED ++ if (do_local_background == 2) ++ { ++ if ((format & PNG_FORMAT_FLAG_AFIRST) != 0) ++ info_format |= PNG_FORMAT_FLAG_AFIRST; ++ } ++ ++ if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0 || ++ ((png_ptr->transformations & PNG_ADD_ALPHA) != 0 && ++ (png_ptr->flags & PNG_FLAG_FILLER_AFTER) == 0)) ++ { ++ if (do_local_background == 2) ++ png_error(png_ptr, "unexpected alpha swap transformation"); ++ ++ info_format |= PNG_FORMAT_FLAG_AFIRST; ++ } ++# endif ++ ++ /* This is actually an internal error. */ ++ if (info_format != format) ++ png_error(png_ptr, "png_read_image: invalid transformations"); ++ } ++ ++ /* Now read the rows. If do_local_compose is set then it is necessary to use ++ * a local row buffer. The output will be GA, RGBA or BGRA and must be ++ * converted to G, RGB or BGR as appropriate. The 'local_row' member of the ++ * display acts as a flag. ++ */ ++ { ++ png_voidp first_row = display->buffer; ++ ptrdiff_t row_bytes = display->row_stride; ++ ++ if (linear != 0) ++ row_bytes *= 2; ++ ++ /* The following expression is designed to work correctly whether it gives ++ * a signed or an unsigned result. ++ */ ++ if (row_bytes < 0) ++ { ++ char *ptr = png_voidcast(char*, first_row); ++ ptr += (image->height-1) * (-row_bytes); ++ first_row = png_voidcast(png_voidp, ptr); ++ } ++ ++ display->first_row = first_row; ++ display->row_bytes = row_bytes; ++ } ++ ++ if (do_local_compose != 0) ++ { ++ int result; ++ png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr)); ++ ++ display->local_row = row; ++ result = png_safe_execute(image, png_image_read_composite, display); ++ display->local_row = NULL; ++ png_free(png_ptr, row); ++ ++ return result; ++ } ++ ++ else if (do_local_background == 2) ++ { ++ int result; ++ png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr)); ++ ++ display->local_row = row; ++ result = png_safe_execute(image, png_image_read_background, display); ++ display->local_row = NULL; ++ png_free(png_ptr, row); ++ ++ return result; ++ } ++ ++ else ++ { ++ png_alloc_size_t row_bytes = (png_alloc_size_t)display->row_bytes; ++ ++ while (--passes >= 0) ++ { ++ png_uint_32 y = image->height; ++ png_bytep row = png_voidcast(png_bytep, display->first_row); ++ ++ for (; y > 0; --y) ++ { ++ png_read_row(png_ptr, row, NULL); ++ row += row_bytes; ++ } ++ } ++ ++ return 1; ++ } ++} ++ ++int PNGAPI ++png_image_finish_read(png_imagep image, png_const_colorp background, ++ void *buffer, png_int_32 row_stride, void *colormap) ++{ ++ if (image != NULL && image->version == PNG_IMAGE_VERSION) ++ { ++ /* Check for row_stride overflow. This check is not performed on the ++ * original PNG format because it may not occur in the output PNG format ++ * and libpng deals with the issues of reading the original. ++ */ ++ unsigned int channels = PNG_IMAGE_PIXEL_CHANNELS(image->format); ++ ++ /* The following checks just the 'row_stride' calculation to ensure it ++ * fits in a signed 32-bit value. Because channels/components can be ++ * either 1 or 2 bytes in size the length of a row can still overflow 32 ++ * bits; this is just to verify that the 'row_stride' argument can be ++ * represented. ++ */ ++ if (image->width <= 0x7fffffffU/channels) /* no overflow */ ++ { ++ png_uint_32 check; ++ png_uint_32 png_row_stride = image->width * channels; ++ ++ if (row_stride == 0) ++ row_stride = (png_int_32)/*SAFE*/png_row_stride; ++ ++ if (row_stride < 0) ++ check = (png_uint_32)(-row_stride); ++ ++ else ++ check = (png_uint_32)row_stride; ++ ++ /* This verifies 'check', the absolute value of the actual stride ++ * passed in and detects overflow in the application calculation (i.e. ++ * if the app did actually pass in a non-zero 'row_stride'. ++ */ ++ if (image->opaque != NULL && buffer != NULL && check >= png_row_stride) ++ { ++ /* Now check for overflow of the image buffer calculation; this ++ * limits the whole image size to 32 bits for API compatibility with ++ * the current, 32-bit, PNG_IMAGE_BUFFER_SIZE macro. ++ * ++ * The PNG_IMAGE_BUFFER_SIZE macro is: ++ * ++ * (PNG_IMAGE_PIXEL_COMPONENT_SIZE(fmt)*height*(row_stride)) ++ * ++ * And the component size is always 1 or 2, so make sure that the ++ * number of *bytes* that the application is saying are available ++ * does actually fit into a 32-bit number. ++ * ++ * NOTE: this will be changed in 1.7 because PNG_IMAGE_BUFFER_SIZE ++ * will be changed to use png_alloc_size_t; bigger images can be ++ * accommodated on 64-bit systems. ++ */ ++ if (image->height <= ++ 0xffffffffU/PNG_IMAGE_PIXEL_COMPONENT_SIZE(image->format)/check) ++ { ++ if ((image->format & PNG_FORMAT_FLAG_COLORMAP) == 0 || ++ (image->colormap_entries > 0 && colormap != NULL)) ++ { ++ int result; ++ png_image_read_control display; ++ ++ memset(&display, 0, (sizeof display)); ++ display.image = image; ++ display.buffer = buffer; ++ display.row_stride = row_stride; ++ display.colormap = colormap; ++ display.background = background; ++ display.local_row = NULL; ++ ++ /* Choose the correct 'end' routine; for the color-map case ++ * all the setup has already been done. ++ */ ++ if ((image->format & PNG_FORMAT_FLAG_COLORMAP) != 0) ++ result = ++ png_safe_execute(image, ++ png_image_read_colormap, &display) && ++ png_safe_execute(image, ++ png_image_read_colormapped, &display); ++ ++ else ++ result = ++ png_safe_execute(image, ++ png_image_read_direct, &display); ++ ++ png_image_free(image); ++ return result; ++ } ++ ++ else ++ return png_image_error(image, ++ "png_image_finish_read[color-map]: no color-map"); ++ } ++ ++ else ++ return png_image_error(image, ++ "png_image_finish_read: image too large"); ++ } ++ ++ else ++ return png_image_error(image, ++ "png_image_finish_read: invalid argument"); ++ } ++ ++ else ++ return png_image_error(image, ++ "png_image_finish_read: row_stride too large"); ++ } ++ ++ else if (image != NULL) ++ return png_image_error(image, ++ "png_image_finish_read: damaged PNG_IMAGE_VERSION"); ++ ++ return 0; ++} ++ ++#endif /* SIMPLIFIED_READ */ ++#endif /* READ */ +diff --git a/lib/libpng/pngrio.c b/lib/libpng/pngrio.c +new file mode 100644 +index 000000000..794635810 +--- /dev/null ++++ b/lib/libpng/pngrio.c +@@ -0,0 +1,120 @@ ++ ++/* pngrio.c - functions for data input ++ * ++ * Copyright (c) 2018 Cosmin Truta ++ * Copyright (c) 1998-2002,2004,2006-2016,2018 Glenn Randers-Pehrson ++ * Copyright (c) 1996-1997 Andreas Dilger ++ * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc. ++ * ++ * This code is released under the libpng license. ++ * For conditions of distribution and use, see the disclaimer ++ * and license in png.h ++ * ++ * This file provides a location for all input. Users who need ++ * special handling are expected to write a function that has the same ++ * arguments as this and performs a similar function, but that possibly ++ * has a different input method. Note that you shouldn't change this ++ * function, but rather write a replacement function and then make ++ * libpng use it at run time with png_set_read_fn(...). ++ */ ++ ++#include "pngpriv.h" ++ ++#ifdef PNG_READ_SUPPORTED ++ ++/* Read the data from whatever input you are using. The default routine ++ * reads from a file pointer. Note that this routine sometimes gets called ++ * with very small lengths, so you should implement some kind of simple ++ * buffering if you are using unbuffered reads. This should never be asked ++ * to read more than 64K on a 16-bit machine. ++ */ ++void /* PRIVATE */ ++png_read_data(png_structrp png_ptr, png_bytep data, size_t length) ++{ ++ png_debug1(4, "reading %d bytes", (int)length); ++ ++ if (png_ptr->read_data_fn != NULL) ++ (*(png_ptr->read_data_fn))(png_ptr, data, length); ++ ++ else ++ png_error(png_ptr, "Call to NULL read function"); ++} ++ ++#ifdef PNG_STDIO_SUPPORTED ++/* This is the function that does the actual reading of data. If you are ++ * not reading from a standard C stream, you should create a replacement ++ * read_data function and use it at run time with png_set_read_fn(), rather ++ * than changing the library. ++ */ ++void PNGCBAPI ++png_default_read_data(png_structp png_ptr, png_bytep data, size_t length) ++{ ++ size_t check; ++ ++ if (png_ptr == NULL) ++ return; ++ ++ /* fread() returns 0 on error, so it is OK to store this in a size_t ++ * instead of an int, which is what fread() actually returns. ++ */ ++ check = fread(data, 1, length, png_voidcast(png_FILE_p, png_ptr->io_ptr)); ++ ++ if (check != length) ++ png_error(png_ptr, "Read Error"); ++} ++#endif ++ ++/* This function allows the application to supply a new input function ++ * for libpng if standard C streams aren't being used. ++ * ++ * This function takes as its arguments: ++ * ++ * png_ptr - pointer to a png input data structure ++ * ++ * io_ptr - pointer to user supplied structure containing info about ++ * the input functions. May be NULL. ++ * ++ * read_data_fn - pointer to a new input function that takes as its ++ * arguments a pointer to a png_struct, a pointer to ++ * a location where input data can be stored, and a 32-bit ++ * unsigned int that is the number of bytes to be read. ++ * To exit and output any fatal error messages the new write ++ * function should call png_error(png_ptr, "Error msg"). ++ * May be NULL, in which case libpng's default function will ++ * be used. ++ */ ++void PNGAPI ++png_set_read_fn(png_structrp png_ptr, png_voidp io_ptr, ++ png_rw_ptr read_data_fn) ++{ ++ if (png_ptr == NULL) ++ return; ++ ++ png_ptr->io_ptr = io_ptr; ++ ++#ifdef PNG_STDIO_SUPPORTED ++ if (read_data_fn != NULL) ++ png_ptr->read_data_fn = read_data_fn; ++ ++ else ++ png_ptr->read_data_fn = png_default_read_data; ++#else ++ png_ptr->read_data_fn = read_data_fn; ++#endif ++ ++#ifdef PNG_WRITE_SUPPORTED ++ /* It is an error to write to a read device */ ++ if (png_ptr->write_data_fn != NULL) ++ { ++ png_ptr->write_data_fn = NULL; ++ png_warning(png_ptr, ++ "Can't set both read_data_fn and write_data_fn in the" ++ " same structure"); ++ } ++#endif ++ ++#ifdef PNG_WRITE_FLUSH_SUPPORTED ++ png_ptr->output_flush_fn = NULL; ++#endif ++} ++#endif /* READ */ +diff --git a/lib/libpng/pngrtran.c b/lib/libpng/pngrtran.c +new file mode 100644 +index 000000000..9a8fad9f4 +--- /dev/null ++++ b/lib/libpng/pngrtran.c +@@ -0,0 +1,5044 @@ ++ ++/* pngrtran.c - transforms the data in a row for PNG readers ++ * ++ * Copyright (c) 2018-2019 Cosmin Truta ++ * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson ++ * Copyright (c) 1996-1997 Andreas Dilger ++ * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc. ++ * ++ * This code is released under the libpng license. ++ * For conditions of distribution and use, see the disclaimer ++ * and license in png.h ++ * ++ * This file contains functions optionally called by an application ++ * in order to tell libpng how to handle data when reading a PNG. ++ * Transformations that are used in both reading and writing are ++ * in pngtrans.c. ++ */ ++ ++#include "pngpriv.h" ++ ++#ifdef PNG_ARM_NEON_IMPLEMENTATION ++# if PNG_ARM_NEON_IMPLEMENTATION == 1 ++# define PNG_ARM_NEON_INTRINSICS_AVAILABLE ++# if defined(_MSC_VER) && defined(_M_ARM64) ++# include ++# else ++# include ++# endif ++# endif ++#endif ++ ++#ifdef PNG_READ_SUPPORTED ++ ++/* Set the action on getting a CRC error for an ancillary or critical chunk. */ ++void PNGAPI ++png_set_crc_action(png_structrp png_ptr, int crit_action, int ancil_action) ++{ ++ png_debug(1, "in png_set_crc_action"); ++ ++ if (png_ptr == NULL) ++ return; ++ ++ /* Tell libpng how we react to CRC errors in critical chunks */ ++ switch (crit_action) ++ { ++ case PNG_CRC_NO_CHANGE: /* Leave setting as is */ ++ break; ++ ++ case PNG_CRC_WARN_USE: /* Warn/use data */ ++ png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK; ++ png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE; ++ break; ++ ++ case PNG_CRC_QUIET_USE: /* Quiet/use data */ ++ png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK; ++ png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE | ++ PNG_FLAG_CRC_CRITICAL_IGNORE; ++ break; ++ ++ case PNG_CRC_WARN_DISCARD: /* Not a valid action for critical data */ ++ png_warning(png_ptr, ++ "Can't discard critical data on CRC error"); ++ /* FALLTHROUGH */ ++ case PNG_CRC_ERROR_QUIT: /* Error/quit */ ++ ++ case PNG_CRC_DEFAULT: ++ default: ++ png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK; ++ break; ++ } ++ ++ /* Tell libpng how we react to CRC errors in ancillary chunks */ ++ switch (ancil_action) ++ { ++ case PNG_CRC_NO_CHANGE: /* Leave setting as is */ ++ break; ++ ++ case PNG_CRC_WARN_USE: /* Warn/use data */ ++ png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; ++ png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE; ++ break; ++ ++ case PNG_CRC_QUIET_USE: /* Quiet/use data */ ++ png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; ++ png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE | ++ PNG_FLAG_CRC_ANCILLARY_NOWARN; ++ break; ++ ++ case PNG_CRC_ERROR_QUIT: /* Error/quit */ ++ png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; ++ png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_NOWARN; ++ break; ++ ++ case PNG_CRC_WARN_DISCARD: /* Warn/discard data */ ++ ++ case PNG_CRC_DEFAULT: ++ default: ++ png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK; ++ break; ++ } ++} ++ ++#ifdef PNG_READ_TRANSFORMS_SUPPORTED ++/* Is it OK to set a transformation now? Only if png_start_read_image or ++ * png_read_update_info have not been called. It is not necessary for the IHDR ++ * to have been read in all cases; the need_IHDR parameter allows for this ++ * check too. ++ */ ++static int ++png_rtran_ok(png_structrp png_ptr, int need_IHDR) ++{ ++ if (png_ptr != NULL) ++ { ++ if ((png_ptr->flags & PNG_FLAG_ROW_INIT) != 0) ++ png_app_error(png_ptr, ++ "invalid after png_start_read_image or png_read_update_info"); ++ ++ else if (need_IHDR && (png_ptr->mode & PNG_HAVE_IHDR) == 0) ++ png_app_error(png_ptr, "invalid before the PNG header has been read"); ++ ++ else ++ { ++ /* Turn on failure to initialize correctly for all transforms. */ ++ png_ptr->flags |= PNG_FLAG_DETECT_UNINITIALIZED; ++ ++ return 1; /* Ok */ ++ } ++ } ++ ++ return 0; /* no png_error possible! */ ++} ++#endif ++ ++#ifdef PNG_READ_BACKGROUND_SUPPORTED ++/* Handle alpha and tRNS via a background color */ ++void PNGFAPI ++png_set_background_fixed(png_structrp png_ptr, ++ png_const_color_16p background_color, int background_gamma_code, ++ int need_expand, png_fixed_point background_gamma) ++{ ++ png_debug(1, "in png_set_background_fixed"); ++ ++ if (png_rtran_ok(png_ptr, 0) == 0 || background_color == NULL) ++ return; ++ ++ if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN) ++ { ++ png_warning(png_ptr, "Application must supply a known background gamma"); ++ return; ++ } ++ ++ png_ptr->transformations |= PNG_COMPOSE | PNG_STRIP_ALPHA; ++ png_ptr->transformations &= ~PNG_ENCODE_ALPHA; ++ png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; ++ ++ png_ptr->background = *background_color; ++ png_ptr->background_gamma = background_gamma; ++ png_ptr->background_gamma_type = (png_byte)(background_gamma_code); ++ if (need_expand != 0) ++ png_ptr->transformations |= PNG_BACKGROUND_EXPAND; ++ else ++ png_ptr->transformations &= ~PNG_BACKGROUND_EXPAND; ++} ++ ++# ifdef PNG_FLOATING_POINT_SUPPORTED ++void PNGAPI ++png_set_background(png_structrp png_ptr, ++ png_const_color_16p background_color, int background_gamma_code, ++ int need_expand, double background_gamma) ++{ ++ png_set_background_fixed(png_ptr, background_color, background_gamma_code, ++ need_expand, png_fixed(png_ptr, background_gamma, "png_set_background")); ++} ++# endif /* FLOATING_POINT */ ++#endif /* READ_BACKGROUND */ ++ ++/* Scale 16-bit depth files to 8-bit depth. If both of these are set then the ++ * one that pngrtran does first (scale) happens. This is necessary to allow the ++ * TRANSFORM and API behavior to be somewhat consistent, and it's simpler. ++ */ ++#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED ++void PNGAPI ++png_set_scale_16(png_structrp png_ptr) ++{ ++ png_debug(1, "in png_set_scale_16"); ++ ++ if (png_rtran_ok(png_ptr, 0) == 0) ++ return; ++ ++ png_ptr->transformations |= PNG_SCALE_16_TO_8; ++} ++#endif ++ ++#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED ++/* Chop 16-bit depth files to 8-bit depth */ ++void PNGAPI ++png_set_strip_16(png_structrp png_ptr) ++{ ++ png_debug(1, "in png_set_strip_16"); ++ ++ if (png_rtran_ok(png_ptr, 0) == 0) ++ return; ++ ++ png_ptr->transformations |= PNG_16_TO_8; ++} ++#endif ++ ++#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED ++void PNGAPI ++png_set_strip_alpha(png_structrp png_ptr) ++{ ++ png_debug(1, "in png_set_strip_alpha"); ++ ++ if (png_rtran_ok(png_ptr, 0) == 0) ++ return; ++ ++ png_ptr->transformations |= PNG_STRIP_ALPHA; ++} ++#endif ++ ++#if defined(PNG_READ_ALPHA_MODE_SUPPORTED) || defined(PNG_READ_GAMMA_SUPPORTED) ++static png_fixed_point ++translate_gamma_flags(png_structrp png_ptr, png_fixed_point output_gamma, ++ int is_screen) ++{ ++ /* Check for flag values. The main reason for having the old Mac value as a ++ * flag is that it is pretty near impossible to work out what the correct ++ * value is from Apple documentation - a working Mac system is needed to ++ * discover the value! ++ */ ++ if (output_gamma == PNG_DEFAULT_sRGB || ++ output_gamma == PNG_FP_1 / PNG_DEFAULT_sRGB) ++ { ++ /* If there is no sRGB support this just sets the gamma to the standard ++ * sRGB value. (This is a side effect of using this function!) ++ */ ++# ifdef PNG_READ_sRGB_SUPPORTED ++ png_ptr->flags |= PNG_FLAG_ASSUME_sRGB; ++# else ++ PNG_UNUSED(png_ptr) ++# endif ++ if (is_screen != 0) ++ output_gamma = PNG_GAMMA_sRGB; ++ else ++ output_gamma = PNG_GAMMA_sRGB_INVERSE; ++ } ++ ++ else if (output_gamma == PNG_GAMMA_MAC_18 || ++ output_gamma == PNG_FP_1 / PNG_GAMMA_MAC_18) ++ { ++ if (is_screen != 0) ++ output_gamma = PNG_GAMMA_MAC_OLD; ++ else ++ output_gamma = PNG_GAMMA_MAC_INVERSE; ++ } ++ ++ return output_gamma; ++} ++ ++# ifdef PNG_FLOATING_POINT_SUPPORTED ++static png_fixed_point ++convert_gamma_value(png_structrp png_ptr, double output_gamma) ++{ ++ /* The following silently ignores cases where fixed point (times 100,000) ++ * gamma values are passed to the floating point API. This is safe and it ++ * means the fixed point constants work just fine with the floating point ++ * API. The alternative would just lead to undetected errors and spurious ++ * bug reports. Negative values fail inside the _fixed API unless they ++ * correspond to the flag values. ++ */ ++ if (output_gamma > 0 && output_gamma < 128) ++ output_gamma *= PNG_FP_1; ++ ++ /* This preserves -1 and -2 exactly: */ ++ output_gamma = floor(output_gamma + .5); ++ ++ if (output_gamma > PNG_FP_MAX || output_gamma < PNG_FP_MIN) ++ png_fixed_error(png_ptr, "gamma value"); ++ ++ return (png_fixed_point)output_gamma; ++} ++# endif ++#endif /* READ_ALPHA_MODE || READ_GAMMA */ ++ ++#ifdef PNG_READ_ALPHA_MODE_SUPPORTED ++void PNGFAPI ++png_set_alpha_mode_fixed(png_structrp png_ptr, int mode, ++ png_fixed_point output_gamma) ++{ ++ int compose = 0; ++ png_fixed_point file_gamma; ++ ++ png_debug(1, "in png_set_alpha_mode"); ++ ++ if (png_rtran_ok(png_ptr, 0) == 0) ++ return; ++ ++ output_gamma = translate_gamma_flags(png_ptr, output_gamma, 1/*screen*/); ++ ++ /* Validate the value to ensure it is in a reasonable range. The value ++ * is expected to be 1 or greater, but this range test allows for some ++ * viewing correction values. The intent is to weed out users of this API ++ * who use the inverse of the gamma value accidentally! Since some of these ++ * values are reasonable this may have to be changed: ++ * ++ * 1.6.x: changed from 0.07..3 to 0.01..100 (to accommodate the optimal 16-bit ++ * gamma of 36, and its reciprocal.) ++ */ ++ if (output_gamma < 1000 || output_gamma > 10000000) ++ png_error(png_ptr, "output gamma out of expected range"); ++ ++ /* The default file gamma is the inverse of the output gamma; the output ++ * gamma may be changed below so get the file value first: ++ */ ++ file_gamma = png_reciprocal(output_gamma); ++ ++ /* There are really 8 possibilities here, composed of any combination ++ * of: ++ * ++ * premultiply the color channels ++ * do not encode non-opaque pixels ++ * encode the alpha as well as the color channels ++ * ++ * The differences disappear if the input/output ('screen') gamma is 1.0, ++ * because then the encoding is a no-op and there is only the choice of ++ * premultiplying the color channels or not. ++ * ++ * png_set_alpha_mode and png_set_background interact because both use ++ * png_compose to do the work. Calling both is only useful when ++ * png_set_alpha_mode is used to set the default mode - PNG_ALPHA_PNG - along ++ * with a default gamma value. Otherwise PNG_COMPOSE must not be set. ++ */ ++ switch (mode) ++ { ++ case PNG_ALPHA_PNG: /* default: png standard */ ++ /* No compose, but it may be set by png_set_background! */ ++ png_ptr->transformations &= ~PNG_ENCODE_ALPHA; ++ png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; ++ break; ++ ++ case PNG_ALPHA_ASSOCIATED: /* color channels premultiplied */ ++ compose = 1; ++ png_ptr->transformations &= ~PNG_ENCODE_ALPHA; ++ png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; ++ /* The output is linear: */ ++ output_gamma = PNG_FP_1; ++ break; ++ ++ case PNG_ALPHA_OPTIMIZED: /* associated, non-opaque pixels linear */ ++ compose = 1; ++ png_ptr->transformations &= ~PNG_ENCODE_ALPHA; ++ png_ptr->flags |= PNG_FLAG_OPTIMIZE_ALPHA; ++ /* output_gamma records the encoding of opaque pixels! */ ++ break; ++ ++ case PNG_ALPHA_BROKEN: /* associated, non-linear, alpha encoded */ ++ compose = 1; ++ png_ptr->transformations |= PNG_ENCODE_ALPHA; ++ png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; ++ break; ++ ++ default: ++ png_error(png_ptr, "invalid alpha mode"); ++ } ++ ++ /* Only set the default gamma if the file gamma has not been set (this has ++ * the side effect that the gamma in a second call to png_set_alpha_mode will ++ * be ignored.) ++ */ ++ if (png_ptr->colorspace.gamma == 0) ++ { ++ png_ptr->colorspace.gamma = file_gamma; ++ png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA; ++ } ++ ++ /* But always set the output gamma: */ ++ png_ptr->screen_gamma = output_gamma; ++ ++ /* Finally, if pre-multiplying, set the background fields to achieve the ++ * desired result. ++ */ ++ if (compose != 0) ++ { ++ /* And obtain alpha pre-multiplication by composing on black: */ ++ memset(&png_ptr->background, 0, (sizeof png_ptr->background)); ++ png_ptr->background_gamma = png_ptr->colorspace.gamma; /* just in case */ ++ png_ptr->background_gamma_type = PNG_BACKGROUND_GAMMA_FILE; ++ png_ptr->transformations &= ~PNG_BACKGROUND_EXPAND; ++ ++ if ((png_ptr->transformations & PNG_COMPOSE) != 0) ++ png_error(png_ptr, ++ "conflicting calls to set alpha mode and background"); ++ ++ png_ptr->transformations |= PNG_COMPOSE; ++ } ++} ++ ++# ifdef PNG_FLOATING_POINT_SUPPORTED ++void PNGAPI ++png_set_alpha_mode(png_structrp png_ptr, int mode, double output_gamma) ++{ ++ png_set_alpha_mode_fixed(png_ptr, mode, convert_gamma_value(png_ptr, ++ output_gamma)); ++} ++# endif ++#endif ++ ++#ifdef PNG_READ_QUANTIZE_SUPPORTED ++/* Dither file to 8-bit. Supply a palette, the current number ++ * of elements in the palette, the maximum number of elements ++ * allowed, and a histogram if possible. If the current number ++ * of colors is greater than the maximum number, the palette will be ++ * modified to fit in the maximum number. "full_quantize" indicates ++ * whether we need a quantizing cube set up for RGB images, or if we ++ * simply are reducing the number of colors in a paletted image. ++ */ ++ ++typedef struct png_dsort_struct ++{ ++ struct png_dsort_struct * next; ++ png_byte left; ++ png_byte right; ++} png_dsort; ++typedef png_dsort * png_dsortp; ++typedef png_dsort * * png_dsortpp; ++ ++void PNGAPI ++png_set_quantize(png_structrp png_ptr, png_colorp palette, ++ int num_palette, int maximum_colors, png_const_uint_16p histogram, ++ int full_quantize) ++{ ++ png_debug(1, "in png_set_quantize"); ++ ++ if (png_rtran_ok(png_ptr, 0) == 0) ++ return; ++ ++ png_ptr->transformations |= PNG_QUANTIZE; ++ ++ if (full_quantize == 0) ++ { ++ int i; ++ ++ png_ptr->quantize_index = (png_bytep)png_malloc(png_ptr, ++ (png_alloc_size_t)((png_uint_32)num_palette * (sizeof (png_byte)))); ++ for (i = 0; i < num_palette; i++) ++ png_ptr->quantize_index[i] = (png_byte)i; ++ } ++ ++ if (num_palette > maximum_colors) ++ { ++ if (histogram != NULL) ++ { ++ /* This is easy enough, just throw out the least used colors. ++ * Perhaps not the best solution, but good enough. ++ */ ++ ++ int i; ++ ++ /* Initialize an array to sort colors */ ++ png_ptr->quantize_sort = (png_bytep)png_malloc(png_ptr, ++ (png_alloc_size_t)((png_uint_32)num_palette * (sizeof (png_byte)))); ++ ++ /* Initialize the quantize_sort array */ ++ for (i = 0; i < num_palette; i++) ++ png_ptr->quantize_sort[i] = (png_byte)i; ++ ++ /* Find the least used palette entries by starting a ++ * bubble sort, and running it until we have sorted ++ * out enough colors. Note that we don't care about ++ * sorting all the colors, just finding which are ++ * least used. ++ */ ++ ++ for (i = num_palette - 1; i >= maximum_colors; i--) ++ { ++ int done; /* To stop early if the list is pre-sorted */ ++ int j; ++ ++ done = 1; ++ for (j = 0; j < i; j++) ++ { ++ if (histogram[png_ptr->quantize_sort[j]] ++ < histogram[png_ptr->quantize_sort[j + 1]]) ++ { ++ png_byte t; ++ ++ t = png_ptr->quantize_sort[j]; ++ png_ptr->quantize_sort[j] = png_ptr->quantize_sort[j + 1]; ++ png_ptr->quantize_sort[j + 1] = t; ++ done = 0; ++ } ++ } ++ ++ if (done != 0) ++ break; ++ } ++ ++ /* Swap the palette around, and set up a table, if necessary */ ++ if (full_quantize != 0) ++ { ++ int j = num_palette; ++ ++ /* Put all the useful colors within the max, but don't ++ * move the others. ++ */ ++ for (i = 0; i < maximum_colors; i++) ++ { ++ if ((int)png_ptr->quantize_sort[i] >= maximum_colors) ++ { ++ do ++ j--; ++ while ((int)png_ptr->quantize_sort[j] >= maximum_colors); ++ ++ palette[i] = palette[j]; ++ } ++ } ++ } ++ else ++ { ++ int j = num_palette; ++ ++ /* Move all the used colors inside the max limit, and ++ * develop a translation table. ++ */ ++ for (i = 0; i < maximum_colors; i++) ++ { ++ /* Only move the colors we need to */ ++ if ((int)png_ptr->quantize_sort[i] >= maximum_colors) ++ { ++ png_color tmp_color; ++ ++ do ++ j--; ++ while ((int)png_ptr->quantize_sort[j] >= maximum_colors); ++ ++ tmp_color = palette[j]; ++ palette[j] = palette[i]; ++ palette[i] = tmp_color; ++ /* Indicate where the color went */ ++ png_ptr->quantize_index[j] = (png_byte)i; ++ png_ptr->quantize_index[i] = (png_byte)j; ++ } ++ } ++ ++ /* Find closest color for those colors we are not using */ ++ for (i = 0; i < num_palette; i++) ++ { ++ if ((int)png_ptr->quantize_index[i] >= maximum_colors) ++ { ++ int min_d, k, min_k, d_index; ++ ++ /* Find the closest color to one we threw out */ ++ d_index = png_ptr->quantize_index[i]; ++ min_d = PNG_COLOR_DIST(palette[d_index], palette[0]); ++ for (k = 1, min_k = 0; k < maximum_colors; k++) ++ { ++ int d; ++ ++ d = PNG_COLOR_DIST(palette[d_index], palette[k]); ++ ++ if (d < min_d) ++ { ++ min_d = d; ++ min_k = k; ++ } ++ } ++ /* Point to closest color */ ++ png_ptr->quantize_index[i] = (png_byte)min_k; ++ } ++ } ++ } ++ png_free(png_ptr, png_ptr->quantize_sort); ++ png_ptr->quantize_sort = NULL; ++ } ++ else ++ { ++ /* This is much harder to do simply (and quickly). Perhaps ++ * we need to go through a median cut routine, but those ++ * don't always behave themselves with only a few colors ++ * as input. So we will just find the closest two colors, ++ * and throw out one of them (chosen somewhat randomly). ++ * [We don't understand this at all, so if someone wants to ++ * work on improving it, be our guest - AED, GRP] ++ */ ++ int i; ++ int max_d; ++ int num_new_palette; ++ png_dsortp t; ++ png_dsortpp hash; ++ ++ t = NULL; ++ ++ /* Initialize palette index arrays */ ++ png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr, ++ (png_alloc_size_t)((png_uint_32)num_palette * ++ (sizeof (png_byte)))); ++ png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr, ++ (png_alloc_size_t)((png_uint_32)num_palette * ++ (sizeof (png_byte)))); ++ ++ /* Initialize the sort array */ ++ for (i = 0; i < num_palette; i++) ++ { ++ png_ptr->index_to_palette[i] = (png_byte)i; ++ png_ptr->palette_to_index[i] = (png_byte)i; ++ } ++ ++ hash = (png_dsortpp)png_calloc(png_ptr, (png_alloc_size_t)(769 * ++ (sizeof (png_dsortp)))); ++ ++ num_new_palette = num_palette; ++ ++ /* Initial wild guess at how far apart the farthest pixel ++ * pair we will be eliminating will be. Larger ++ * numbers mean more areas will be allocated, Smaller ++ * numbers run the risk of not saving enough data, and ++ * having to do this all over again. ++ * ++ * I have not done extensive checking on this number. ++ */ ++ max_d = 96; ++ ++ while (num_new_palette > maximum_colors) ++ { ++ for (i = 0; i < num_new_palette - 1; i++) ++ { ++ int j; ++ ++ for (j = i + 1; j < num_new_palette; j++) ++ { ++ int d; ++ ++ d = PNG_COLOR_DIST(palette[i], palette[j]); ++ ++ if (d <= max_d) ++ { ++ ++ t = (png_dsortp)png_malloc_warn(png_ptr, ++ (png_alloc_size_t)(sizeof (png_dsort))); ++ ++ if (t == NULL) ++ break; ++ ++ t->next = hash[d]; ++ t->left = (png_byte)i; ++ t->right = (png_byte)j; ++ hash[d] = t; ++ } ++ } ++ if (t == NULL) ++ break; ++ } ++ ++ if (t != NULL) ++ for (i = 0; i <= max_d; i++) ++ { ++ if (hash[i] != NULL) ++ { ++ png_dsortp p; ++ ++ for (p = hash[i]; p; p = p->next) ++ { ++ if ((int)png_ptr->index_to_palette[p->left] ++ < num_new_palette && ++ (int)png_ptr->index_to_palette[p->right] ++ < num_new_palette) ++ { ++ int j, next_j; ++ ++ if (num_new_palette & 0x01) ++ { ++ j = p->left; ++ next_j = p->right; ++ } ++ else ++ { ++ j = p->right; ++ next_j = p->left; ++ } ++ ++ num_new_palette--; ++ palette[png_ptr->index_to_palette[j]] ++ = palette[num_new_palette]; ++ if (full_quantize == 0) ++ { ++ int k; ++ ++ for (k = 0; k < num_palette; k++) ++ { ++ if (png_ptr->quantize_index[k] == ++ png_ptr->index_to_palette[j]) ++ png_ptr->quantize_index[k] = ++ png_ptr->index_to_palette[next_j]; ++ ++ if ((int)png_ptr->quantize_index[k] == ++ num_new_palette) ++ png_ptr->quantize_index[k] = ++ png_ptr->index_to_palette[j]; ++ } ++ } ++ ++ png_ptr->index_to_palette[png_ptr->palette_to_index ++ [num_new_palette]] = png_ptr->index_to_palette[j]; ++ ++ png_ptr->palette_to_index[png_ptr->index_to_palette[j]] ++ = png_ptr->palette_to_index[num_new_palette]; ++ ++ png_ptr->index_to_palette[j] = ++ (png_byte)num_new_palette; ++ ++ png_ptr->palette_to_index[num_new_palette] = ++ (png_byte)j; ++ } ++ if (num_new_palette <= maximum_colors) ++ break; ++ } ++ if (num_new_palette <= maximum_colors) ++ break; ++ } ++ } ++ ++ for (i = 0; i < 769; i++) ++ { ++ if (hash[i] != NULL) ++ { ++ png_dsortp p = hash[i]; ++ while (p) ++ { ++ t = p->next; ++ png_free(png_ptr, p); ++ p = t; ++ } ++ } ++ hash[i] = 0; ++ } ++ max_d += 96; ++ } ++ png_free(png_ptr, hash); ++ png_free(png_ptr, png_ptr->palette_to_index); ++ png_free(png_ptr, png_ptr->index_to_palette); ++ png_ptr->palette_to_index = NULL; ++ png_ptr->index_to_palette = NULL; ++ } ++ num_palette = maximum_colors; ++ } ++ if (png_ptr->palette == NULL) ++ { ++ png_ptr->palette = palette; ++ } ++ png_ptr->num_palette = (png_uint_16)num_palette; ++ ++ if (full_quantize != 0) ++ { ++ int i; ++ png_bytep distance; ++ int total_bits = PNG_QUANTIZE_RED_BITS + PNG_QUANTIZE_GREEN_BITS + ++ PNG_QUANTIZE_BLUE_BITS; ++ int num_red = (1 << PNG_QUANTIZE_RED_BITS); ++ int num_green = (1 << PNG_QUANTIZE_GREEN_BITS); ++ int num_blue = (1 << PNG_QUANTIZE_BLUE_BITS); ++ size_t num_entries = ((size_t)1 << total_bits); ++ ++ png_ptr->palette_lookup = (png_bytep)png_calloc(png_ptr, ++ (png_alloc_size_t)(num_entries * (sizeof (png_byte)))); ++ ++ distance = (png_bytep)png_malloc(png_ptr, (png_alloc_size_t)(num_entries * ++ (sizeof (png_byte)))); ++ ++ memset(distance, 0xff, num_entries * (sizeof (png_byte))); ++ ++ for (i = 0; i < num_palette; i++) ++ { ++ int ir, ig, ib; ++ int r = (palette[i].red >> (8 - PNG_QUANTIZE_RED_BITS)); ++ int g = (palette[i].green >> (8 - PNG_QUANTIZE_GREEN_BITS)); ++ int b = (palette[i].blue >> (8 - PNG_QUANTIZE_BLUE_BITS)); ++ ++ for (ir = 0; ir < num_red; ir++) ++ { ++ /* int dr = abs(ir - r); */ ++ int dr = ((ir > r) ? ir - r : r - ir); ++ int index_r = (ir << (PNG_QUANTIZE_BLUE_BITS + ++ PNG_QUANTIZE_GREEN_BITS)); ++ ++ for (ig = 0; ig < num_green; ig++) ++ { ++ /* int dg = abs(ig - g); */ ++ int dg = ((ig > g) ? ig - g : g - ig); ++ int dt = dr + dg; ++ int dm = ((dr > dg) ? dr : dg); ++ int index_g = index_r | (ig << PNG_QUANTIZE_BLUE_BITS); ++ ++ for (ib = 0; ib < num_blue; ib++) ++ { ++ int d_index = index_g | ib; ++ /* int db = abs(ib - b); */ ++ int db = ((ib > b) ? ib - b : b - ib); ++ int dmax = ((dm > db) ? dm : db); ++ int d = dmax + dt + db; ++ ++ if (d < (int)distance[d_index]) ++ { ++ distance[d_index] = (png_byte)d; ++ png_ptr->palette_lookup[d_index] = (png_byte)i; ++ } ++ } ++ } ++ } ++ } ++ ++ png_free(png_ptr, distance); ++ } ++} ++#endif /* READ_QUANTIZE */ ++ ++#ifdef PNG_READ_GAMMA_SUPPORTED ++void PNGFAPI ++png_set_gamma_fixed(png_structrp png_ptr, png_fixed_point scrn_gamma, ++ png_fixed_point file_gamma) ++{ ++ png_debug(1, "in png_set_gamma_fixed"); ++ ++ if (png_rtran_ok(png_ptr, 0) == 0) ++ return; ++ ++ /* New in libpng-1.5.4 - reserve particular negative values as flags. */ ++ scrn_gamma = translate_gamma_flags(png_ptr, scrn_gamma, 1/*screen*/); ++ file_gamma = translate_gamma_flags(png_ptr, file_gamma, 0/*file*/); ++ ++ /* Checking the gamma values for being >0 was added in 1.5.4 along with the ++ * premultiplied alpha support; this actually hides an undocumented feature ++ * of the previous implementation which allowed gamma processing to be ++ * disabled in background handling. There is no evidence (so far) that this ++ * was being used; however, png_set_background itself accepted and must still ++ * accept '0' for the gamma value it takes, because it isn't always used. ++ * ++ * Since this is an API change (albeit a very minor one that removes an ++ * undocumented API feature) the following checks were only enabled in ++ * libpng-1.6.0. ++ */ ++ if (file_gamma <= 0) ++ png_error(png_ptr, "invalid file gamma in png_set_gamma"); ++ ++ if (scrn_gamma <= 0) ++ png_error(png_ptr, "invalid screen gamma in png_set_gamma"); ++ ++ /* Set the gamma values unconditionally - this overrides the value in the PNG ++ * file if a gAMA chunk was present. png_set_alpha_mode provides a ++ * different, easier, way to default the file gamma. ++ */ ++ png_ptr->colorspace.gamma = file_gamma; ++ png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA; ++ png_ptr->screen_gamma = scrn_gamma; ++} ++ ++# ifdef PNG_FLOATING_POINT_SUPPORTED ++void PNGAPI ++png_set_gamma(png_structrp png_ptr, double scrn_gamma, double file_gamma) ++{ ++ png_set_gamma_fixed(png_ptr, convert_gamma_value(png_ptr, scrn_gamma), ++ convert_gamma_value(png_ptr, file_gamma)); ++} ++# endif /* FLOATING_POINT */ ++#endif /* READ_GAMMA */ ++ ++#ifdef PNG_READ_EXPAND_SUPPORTED ++/* Expand paletted images to RGB, expand grayscale images of ++ * less than 8-bit depth to 8-bit depth, and expand tRNS chunks ++ * to alpha channels. ++ */ ++void PNGAPI ++png_set_expand(png_structrp png_ptr) ++{ ++ png_debug(1, "in png_set_expand"); ++ ++ if (png_rtran_ok(png_ptr, 0) == 0) ++ return; ++ ++ png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); ++} ++ ++/* GRR 19990627: the following three functions currently are identical ++ * to png_set_expand(). However, it is entirely reasonable that someone ++ * might wish to expand an indexed image to RGB but *not* expand a single, ++ * fully transparent palette entry to a full alpha channel--perhaps instead ++ * convert tRNS to the grayscale/RGB format (16-bit RGB value), or replace ++ * the transparent color with a particular RGB value, or drop tRNS entirely. ++ * IOW, a future version of the library may make the transformations flag ++ * a bit more fine-grained, with separate bits for each of these three ++ * functions. ++ * ++ * More to the point, these functions make it obvious what libpng will be ++ * doing, whereas "expand" can (and does) mean any number of things. ++ * ++ * GRP 20060307: In libpng-1.2.9, png_set_gray_1_2_4_to_8() was modified ++ * to expand only the sample depth but not to expand the tRNS to alpha ++ * and its name was changed to png_set_expand_gray_1_2_4_to_8(). ++ */ ++ ++/* Expand paletted images to RGB. */ ++void PNGAPI ++png_set_palette_to_rgb(png_structrp png_ptr) ++{ ++ png_debug(1, "in png_set_palette_to_rgb"); ++ ++ if (png_rtran_ok(png_ptr, 0) == 0) ++ return; ++ ++ png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); ++} ++ ++/* Expand grayscale images of less than 8-bit depth to 8 bits. */ ++void PNGAPI ++png_set_expand_gray_1_2_4_to_8(png_structrp png_ptr) ++{ ++ png_debug(1, "in png_set_expand_gray_1_2_4_to_8"); ++ ++ if (png_rtran_ok(png_ptr, 0) == 0) ++ return; ++ ++ png_ptr->transformations |= PNG_EXPAND; ++} ++ ++/* Expand tRNS chunks to alpha channels. */ ++void PNGAPI ++png_set_tRNS_to_alpha(png_structrp png_ptr) ++{ ++ png_debug(1, "in png_set_tRNS_to_alpha"); ++ ++ if (png_rtran_ok(png_ptr, 0) == 0) ++ return; ++ ++ png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS); ++} ++#endif /* READ_EXPAND */ ++ ++#ifdef PNG_READ_EXPAND_16_SUPPORTED ++/* Expand to 16-bit channels, expand the tRNS chunk too (because otherwise ++ * it may not work correctly.) ++ */ ++void PNGAPI ++png_set_expand_16(png_structrp png_ptr) ++{ ++ png_debug(1, "in png_set_expand_16"); ++ ++ if (png_rtran_ok(png_ptr, 0) == 0) ++ return; ++ ++ png_ptr->transformations |= (PNG_EXPAND_16 | PNG_EXPAND | PNG_EXPAND_tRNS); ++} ++#endif ++ ++#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED ++void PNGAPI ++png_set_gray_to_rgb(png_structrp png_ptr) ++{ ++ png_debug(1, "in png_set_gray_to_rgb"); ++ ++ if (png_rtran_ok(png_ptr, 0) == 0) ++ return; ++ ++ /* Because rgb must be 8 bits or more: */ ++ png_set_expand_gray_1_2_4_to_8(png_ptr); ++ png_ptr->transformations |= PNG_GRAY_TO_RGB; ++} ++#endif ++ ++#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED ++void PNGFAPI ++png_set_rgb_to_gray_fixed(png_structrp png_ptr, int error_action, ++ png_fixed_point red, png_fixed_point green) ++{ ++ png_debug(1, "in png_set_rgb_to_gray"); ++ ++ /* Need the IHDR here because of the check on color_type below. */ ++ /* TODO: fix this */ ++ if (png_rtran_ok(png_ptr, 1) == 0) ++ return; ++ ++ switch (error_action) ++ { ++ case PNG_ERROR_ACTION_NONE: ++ png_ptr->transformations |= PNG_RGB_TO_GRAY; ++ break; ++ ++ case PNG_ERROR_ACTION_WARN: ++ png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN; ++ break; ++ ++ case PNG_ERROR_ACTION_ERROR: ++ png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR; ++ break; ++ ++ default: ++ png_error(png_ptr, "invalid error action to rgb_to_gray"); ++ } ++ ++ if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ++#ifdef PNG_READ_EXPAND_SUPPORTED ++ png_ptr->transformations |= PNG_EXPAND; ++#else ++ { ++ /* Make this an error in 1.6 because otherwise the application may assume ++ * that it just worked and get a memory overwrite. ++ */ ++ png_error(png_ptr, ++ "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED"); ++ ++ /* png_ptr->transformations &= ~PNG_RGB_TO_GRAY; */ ++ } ++#endif ++ { ++ if (red >= 0 && green >= 0 && red + green <= PNG_FP_1) ++ { ++ png_uint_16 red_int, green_int; ++ ++ /* NOTE: this calculation does not round, but this behavior is retained ++ * for consistency; the inaccuracy is very small. The code here always ++ * overwrites the coefficients, regardless of whether they have been ++ * defaulted or set already. ++ */ ++ red_int = (png_uint_16)(((png_uint_32)red*32768)/100000); ++ green_int = (png_uint_16)(((png_uint_32)green*32768)/100000); ++ ++ png_ptr->rgb_to_gray_red_coeff = red_int; ++ png_ptr->rgb_to_gray_green_coeff = green_int; ++ png_ptr->rgb_to_gray_coefficients_set = 1; ++ } ++ ++ else ++ { ++ if (red >= 0 && green >= 0) ++ png_app_warning(png_ptr, ++ "ignoring out of range rgb_to_gray coefficients"); ++ ++ /* Use the defaults, from the cHRM chunk if set, else the historical ++ * values which are close to the sRGB/HDTV/ITU-Rec 709 values. See ++ * png_do_rgb_to_gray for more discussion of the values. In this case ++ * the coefficients are not marked as 'set' and are not overwritten if ++ * something has already provided a default. ++ */ ++ if (png_ptr->rgb_to_gray_red_coeff == 0 && ++ png_ptr->rgb_to_gray_green_coeff == 0) ++ { ++ png_ptr->rgb_to_gray_red_coeff = 6968; ++ png_ptr->rgb_to_gray_green_coeff = 23434; ++ /* png_ptr->rgb_to_gray_blue_coeff = 2366; */ ++ } ++ } ++ } ++} ++ ++#ifdef PNG_FLOATING_POINT_SUPPORTED ++/* Convert a RGB image to a grayscale of the same width. This allows us, ++ * for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image. ++ */ ++ ++void PNGAPI ++png_set_rgb_to_gray(png_structrp png_ptr, int error_action, double red, ++ double green) ++{ ++ png_set_rgb_to_gray_fixed(png_ptr, error_action, ++ png_fixed(png_ptr, red, "rgb to gray red coefficient"), ++ png_fixed(png_ptr, green, "rgb to gray green coefficient")); ++} ++#endif /* FLOATING POINT */ ++ ++#endif /* RGB_TO_GRAY */ ++ ++#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \ ++ defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) ++void PNGAPI ++png_set_read_user_transform_fn(png_structrp png_ptr, png_user_transform_ptr ++ read_user_transform_fn) ++{ ++ png_debug(1, "in png_set_read_user_transform_fn"); ++ ++#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED ++ png_ptr->transformations |= PNG_USER_TRANSFORM; ++ png_ptr->read_user_transform_fn = read_user_transform_fn; ++#endif ++} ++#endif ++ ++#ifdef PNG_READ_TRANSFORMS_SUPPORTED ++#ifdef PNG_READ_GAMMA_SUPPORTED ++/* In the case of gamma transformations only do transformations on images where ++ * the [file] gamma and screen_gamma are not close reciprocals, otherwise it ++ * slows things down slightly, and also needlessly introduces small errors. ++ */ ++static int /* PRIVATE */ ++png_gamma_threshold(png_fixed_point screen_gamma, png_fixed_point file_gamma) ++{ ++ /* PNG_GAMMA_THRESHOLD is the threshold for performing gamma ++ * correction as a difference of the overall transform from 1.0 ++ * ++ * We want to compare the threshold with s*f - 1, if we get ++ * overflow here it is because of wacky gamma values so we ++ * turn on processing anyway. ++ */ ++ png_fixed_point gtest; ++ return !png_muldiv(>est, screen_gamma, file_gamma, PNG_FP_1) || ++ png_gamma_significant(gtest); ++} ++#endif ++ ++/* Initialize everything needed for the read. This includes modifying ++ * the palette. ++ */ ++ ++/* For the moment 'png_init_palette_transformations' and ++ * 'png_init_rgb_transformations' only do some flag canceling optimizations. ++ * The intent is that these two routines should have palette or rgb operations ++ * extracted from 'png_init_read_transformations'. ++ */ ++static void /* PRIVATE */ ++png_init_palette_transformations(png_structrp png_ptr) ++{ ++ /* Called to handle the (input) palette case. In png_do_read_transformations ++ * the first step is to expand the palette if requested, so this code must ++ * take care to only make changes that are invariant with respect to the ++ * palette expansion, or only do them if there is no expansion. ++ * ++ * STRIP_ALPHA has already been handled in the caller (by setting num_trans ++ * to 0.) ++ */ ++ int input_has_alpha = 0; ++ int input_has_transparency = 0; ++ ++ if (png_ptr->num_trans > 0) ++ { ++ int i; ++ ++ /* Ignore if all the entries are opaque (unlikely!) */ ++ for (i=0; inum_trans; ++i) ++ { ++ if (png_ptr->trans_alpha[i] == 255) ++ continue; ++ else if (png_ptr->trans_alpha[i] == 0) ++ input_has_transparency = 1; ++ else ++ { ++ input_has_transparency = 1; ++ input_has_alpha = 1; ++ break; ++ } ++ } ++ } ++ ++ /* If no alpha we can optimize. */ ++ if (input_has_alpha == 0) ++ { ++ /* Any alpha means background and associative alpha processing is ++ * required, however if the alpha is 0 or 1 throughout OPTIMIZE_ALPHA ++ * and ENCODE_ALPHA are irrelevant. ++ */ ++ png_ptr->transformations &= ~PNG_ENCODE_ALPHA; ++ png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; ++ ++ if (input_has_transparency == 0) ++ png_ptr->transformations &= ~(PNG_COMPOSE | PNG_BACKGROUND_EXPAND); ++ } ++ ++#if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED) ++ /* png_set_background handling - deals with the complexity of whether the ++ * background color is in the file format or the screen format in the case ++ * where an 'expand' will happen. ++ */ ++ ++ /* The following code cannot be entered in the alpha pre-multiplication case ++ * because PNG_BACKGROUND_EXPAND is cancelled below. ++ */ ++ if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) != 0 && ++ (png_ptr->transformations & PNG_EXPAND) != 0) ++ { ++ { ++ png_ptr->background.red = ++ png_ptr->palette[png_ptr->background.index].red; ++ png_ptr->background.green = ++ png_ptr->palette[png_ptr->background.index].green; ++ png_ptr->background.blue = ++ png_ptr->palette[png_ptr->background.index].blue; ++ ++#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED ++ if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0) ++ { ++ if ((png_ptr->transformations & PNG_EXPAND_tRNS) == 0) ++ { ++ /* Invert the alpha channel (in tRNS) unless the pixels are ++ * going to be expanded, in which case leave it for later ++ */ ++ int i, istop = png_ptr->num_trans; ++ ++ for (i = 0; i < istop; i++) ++ png_ptr->trans_alpha[i] = ++ (png_byte)(255 - png_ptr->trans_alpha[i]); ++ } ++ } ++#endif /* READ_INVERT_ALPHA */ ++ } ++ } /* background expand and (therefore) no alpha association. */ ++#endif /* READ_EXPAND && READ_BACKGROUND */ ++} ++ ++static void /* PRIVATE */ ++png_init_rgb_transformations(png_structrp png_ptr) ++{ ++ /* Added to libpng-1.5.4: check the color type to determine whether there ++ * is any alpha or transparency in the image and simply cancel the ++ * background and alpha mode stuff if there isn't. ++ */ ++ int input_has_alpha = (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0; ++ int input_has_transparency = png_ptr->num_trans > 0; ++ ++ /* If no alpha we can optimize. */ ++ if (input_has_alpha == 0) ++ { ++ /* Any alpha means background and associative alpha processing is ++ * required, however if the alpha is 0 or 1 throughout OPTIMIZE_ALPHA ++ * and ENCODE_ALPHA are irrelevant. ++ */ ++# ifdef PNG_READ_ALPHA_MODE_SUPPORTED ++ png_ptr->transformations &= ~PNG_ENCODE_ALPHA; ++ png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; ++# endif ++ ++ if (input_has_transparency == 0) ++ png_ptr->transformations &= ~(PNG_COMPOSE | PNG_BACKGROUND_EXPAND); ++ } ++ ++#if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED) ++ /* png_set_background handling - deals with the complexity of whether the ++ * background color is in the file format or the screen format in the case ++ * where an 'expand' will happen. ++ */ ++ ++ /* The following code cannot be entered in the alpha pre-multiplication case ++ * because PNG_BACKGROUND_EXPAND is cancelled below. ++ */ ++ if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) != 0 && ++ (png_ptr->transformations & PNG_EXPAND) != 0 && ++ (png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0) ++ /* i.e., GRAY or GRAY_ALPHA */ ++ { ++ { ++ /* Expand background and tRNS chunks */ ++ int gray = png_ptr->background.gray; ++ int trans_gray = png_ptr->trans_color.gray; ++ ++ switch (png_ptr->bit_depth) ++ { ++ case 1: ++ gray *= 0xff; ++ trans_gray *= 0xff; ++ break; ++ ++ case 2: ++ gray *= 0x55; ++ trans_gray *= 0x55; ++ break; ++ ++ case 4: ++ gray *= 0x11; ++ trans_gray *= 0x11; ++ break; ++ ++ default: ++ ++ case 8: ++ /* FALLTHROUGH */ /* (Already 8 bits) */ ++ ++ case 16: ++ /* Already a full 16 bits */ ++ break; ++ } ++ ++ png_ptr->background.red = png_ptr->background.green = ++ png_ptr->background.blue = (png_uint_16)gray; ++ ++ if ((png_ptr->transformations & PNG_EXPAND_tRNS) == 0) ++ { ++ png_ptr->trans_color.red = png_ptr->trans_color.green = ++ png_ptr->trans_color.blue = (png_uint_16)trans_gray; ++ } ++ } ++ } /* background expand and (therefore) no alpha association. */ ++#endif /* READ_EXPAND && READ_BACKGROUND */ ++} ++ ++void /* PRIVATE */ ++png_init_read_transformations(png_structrp png_ptr) ++{ ++ png_debug(1, "in png_init_read_transformations"); ++ ++ /* This internal function is called from png_read_start_row in pngrutil.c ++ * and it is called before the 'rowbytes' calculation is done, so the code ++ * in here can change or update the transformations flags. ++ * ++ * First do updates that do not depend on the details of the PNG image data ++ * being processed. ++ */ ++ ++#ifdef PNG_READ_GAMMA_SUPPORTED ++ /* Prior to 1.5.4 these tests were performed from png_set_gamma, 1.5.4 adds ++ * png_set_alpha_mode and this is another source for a default file gamma so ++ * the test needs to be performed later - here. In addition prior to 1.5.4 ++ * the tests were repeated for the PALETTE color type here - this is no ++ * longer necessary (and doesn't seem to have been necessary before.) ++ */ ++ { ++ /* The following temporary indicates if overall gamma correction is ++ * required. ++ */ ++ int gamma_correction = 0; ++ ++ if (png_ptr->colorspace.gamma != 0) /* has been set */ ++ { ++ if (png_ptr->screen_gamma != 0) /* screen set too */ ++ gamma_correction = png_gamma_threshold(png_ptr->colorspace.gamma, ++ png_ptr->screen_gamma); ++ ++ else ++ /* Assume the output matches the input; a long time default behavior ++ * of libpng, although the standard has nothing to say about this. ++ */ ++ png_ptr->screen_gamma = png_reciprocal(png_ptr->colorspace.gamma); ++ } ++ ++ else if (png_ptr->screen_gamma != 0) ++ /* The converse - assume the file matches the screen, note that this ++ * perhaps undesirable default can (from 1.5.4) be changed by calling ++ * png_set_alpha_mode (even if the alpha handling mode isn't required ++ * or isn't changed from the default.) ++ */ ++ png_ptr->colorspace.gamma = png_reciprocal(png_ptr->screen_gamma); ++ ++ else /* neither are set */ ++ /* Just in case the following prevents any processing - file and screen ++ * are both assumed to be linear and there is no way to introduce a ++ * third gamma value other than png_set_background with 'UNIQUE', and, ++ * prior to 1.5.4 ++ */ ++ png_ptr->screen_gamma = png_ptr->colorspace.gamma = PNG_FP_1; ++ ++ /* We have a gamma value now. */ ++ png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA; ++ ++ /* Now turn the gamma transformation on or off as appropriate. Notice ++ * that PNG_GAMMA just refers to the file->screen correction. Alpha ++ * composition may independently cause gamma correction because it needs ++ * linear data (e.g. if the file has a gAMA chunk but the screen gamma ++ * hasn't been specified.) In any case this flag may get turned off in ++ * the code immediately below if the transform can be handled outside the ++ * row loop. ++ */ ++ if (gamma_correction != 0) ++ png_ptr->transformations |= PNG_GAMMA; ++ ++ else ++ png_ptr->transformations &= ~PNG_GAMMA; ++ } ++#endif ++ ++ /* Certain transformations have the effect of preventing other ++ * transformations that happen afterward in png_do_read_transformations; ++ * resolve the interdependencies here. From the code of ++ * png_do_read_transformations the order is: ++ * ++ * 1) PNG_EXPAND (including PNG_EXPAND_tRNS) ++ * 2) PNG_STRIP_ALPHA (if no compose) ++ * 3) PNG_RGB_TO_GRAY ++ * 4) PNG_GRAY_TO_RGB iff !PNG_BACKGROUND_IS_GRAY ++ * 5) PNG_COMPOSE ++ * 6) PNG_GAMMA ++ * 7) PNG_STRIP_ALPHA (if compose) ++ * 8) PNG_ENCODE_ALPHA ++ * 9) PNG_SCALE_16_TO_8 ++ * 10) PNG_16_TO_8 ++ * 11) PNG_QUANTIZE (converts to palette) ++ * 12) PNG_EXPAND_16 ++ * 13) PNG_GRAY_TO_RGB iff PNG_BACKGROUND_IS_GRAY ++ * 14) PNG_INVERT_MONO ++ * 15) PNG_INVERT_ALPHA ++ * 16) PNG_SHIFT ++ * 17) PNG_PACK ++ * 18) PNG_BGR ++ * 19) PNG_PACKSWAP ++ * 20) PNG_FILLER (includes PNG_ADD_ALPHA) ++ * 21) PNG_SWAP_ALPHA ++ * 22) PNG_SWAP_BYTES ++ * 23) PNG_USER_TRANSFORM [must be last] ++ */ ++#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED ++ if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 && ++ (png_ptr->transformations & PNG_COMPOSE) == 0) ++ { ++ /* Stripping the alpha channel happens immediately after the 'expand' ++ * transformations, before all other transformation, so it cancels out ++ * the alpha handling. It has the side effect negating the effect of ++ * PNG_EXPAND_tRNS too: ++ */ ++ png_ptr->transformations &= ~(PNG_BACKGROUND_EXPAND | PNG_ENCODE_ALPHA | ++ PNG_EXPAND_tRNS); ++ png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; ++ ++ /* Kill the tRNS chunk itself too. Prior to 1.5.4 this did not happen ++ * so transparency information would remain just so long as it wasn't ++ * expanded. This produces unexpected API changes if the set of things ++ * that do PNG_EXPAND_tRNS changes (perfectly possible given the ++ * documentation - which says ask for what you want, accept what you ++ * get.) This makes the behavior consistent from 1.5.4: ++ */ ++ png_ptr->num_trans = 0; ++ } ++#endif /* STRIP_ALPHA supported, no COMPOSE */ ++ ++#ifdef PNG_READ_ALPHA_MODE_SUPPORTED ++ /* If the screen gamma is about 1.0 then the OPTIMIZE_ALPHA and ENCODE_ALPHA ++ * settings will have no effect. ++ */ ++ if (png_gamma_significant(png_ptr->screen_gamma) == 0) ++ { ++ png_ptr->transformations &= ~PNG_ENCODE_ALPHA; ++ png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA; ++ } ++#endif ++ ++#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED ++ /* Make sure the coefficients for the rgb to gray conversion are set ++ * appropriately. ++ */ ++ if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0) ++ png_colorspace_set_rgb_coefficients(png_ptr); ++#endif ++ ++#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED ++#if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED) ++ /* Detect gray background and attempt to enable optimization for ++ * gray --> RGB case. ++ * ++ * Note: if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or ++ * RGB_ALPHA (in which case need_expand is superfluous anyway), the ++ * background color might actually be gray yet not be flagged as such. ++ * This is not a problem for the current code, which uses ++ * PNG_BACKGROUND_IS_GRAY only to decide when to do the ++ * png_do_gray_to_rgb() transformation. ++ * ++ * TODO: this code needs to be revised to avoid the complexity and ++ * interdependencies. The color type of the background should be recorded in ++ * png_set_background, along with the bit depth, then the code has a record ++ * of exactly what color space the background is currently in. ++ */ ++ if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) != 0) ++ { ++ /* PNG_BACKGROUND_EXPAND: the background is in the file color space, so if ++ * the file was grayscale the background value is gray. ++ */ ++ if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0) ++ png_ptr->mode |= PNG_BACKGROUND_IS_GRAY; ++ } ++ ++ else if ((png_ptr->transformations & PNG_COMPOSE) != 0) ++ { ++ /* PNG_COMPOSE: png_set_background was called with need_expand false, ++ * so the color is in the color space of the output or png_set_alpha_mode ++ * was called and the color is black. Ignore RGB_TO_GRAY because that ++ * happens before GRAY_TO_RGB. ++ */ ++ if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0) ++ { ++ if (png_ptr->background.red == png_ptr->background.green && ++ png_ptr->background.red == png_ptr->background.blue) ++ { ++ png_ptr->mode |= PNG_BACKGROUND_IS_GRAY; ++ png_ptr->background.gray = png_ptr->background.red; ++ } ++ } ++ } ++#endif /* READ_EXPAND && READ_BACKGROUND */ ++#endif /* READ_GRAY_TO_RGB */ ++ ++ /* For indexed PNG data (PNG_COLOR_TYPE_PALETTE) many of the transformations ++ * can be performed directly on the palette, and some (such as rgb to gray) ++ * can be optimized inside the palette. This is particularly true of the ++ * composite (background and alpha) stuff, which can be pretty much all done ++ * in the palette even if the result is expanded to RGB or gray afterward. ++ * ++ * NOTE: this is Not Yet Implemented, the code behaves as in 1.5.1 and ++ * earlier and the palette stuff is actually handled on the first row. This ++ * leads to the reported bug that the palette returned by png_get_PLTE is not ++ * updated. ++ */ ++ if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ++ png_init_palette_transformations(png_ptr); ++ ++ else ++ png_init_rgb_transformations(png_ptr); ++ ++#if defined(PNG_READ_BACKGROUND_SUPPORTED) && \ ++ defined(PNG_READ_EXPAND_16_SUPPORTED) ++ if ((png_ptr->transformations & PNG_EXPAND_16) != 0 && ++ (png_ptr->transformations & PNG_COMPOSE) != 0 && ++ (png_ptr->transformations & PNG_BACKGROUND_EXPAND) == 0 && ++ png_ptr->bit_depth != 16) ++ { ++ /* TODO: fix this. Because the expand_16 operation is after the compose ++ * handling the background color must be 8, not 16, bits deep, but the ++ * application will supply a 16-bit value so reduce it here. ++ * ++ * The PNG_BACKGROUND_EXPAND code above does not expand to 16 bits at ++ * present, so that case is ok (until do_expand_16 is moved.) ++ * ++ * NOTE: this discards the low 16 bits of the user supplied background ++ * color, but until expand_16 works properly there is no choice! ++ */ ++# define CHOP(x) (x)=((png_uint_16)PNG_DIV257(x)) ++ CHOP(png_ptr->background.red); ++ CHOP(png_ptr->background.green); ++ CHOP(png_ptr->background.blue); ++ CHOP(png_ptr->background.gray); ++# undef CHOP ++ } ++#endif /* READ_BACKGROUND && READ_EXPAND_16 */ ++ ++#if defined(PNG_READ_BACKGROUND_SUPPORTED) && \ ++ (defined(PNG_READ_SCALE_16_TO_8_SUPPORTED) || \ ++ defined(PNG_READ_STRIP_16_TO_8_SUPPORTED)) ++ if ((png_ptr->transformations & (PNG_16_TO_8|PNG_SCALE_16_TO_8)) != 0 && ++ (png_ptr->transformations & PNG_COMPOSE) != 0 && ++ (png_ptr->transformations & PNG_BACKGROUND_EXPAND) == 0 && ++ png_ptr->bit_depth == 16) ++ { ++ /* On the other hand, if a 16-bit file is to be reduced to 8-bits per ++ * component this will also happen after PNG_COMPOSE and so the background ++ * color must be pre-expanded here. ++ * ++ * TODO: fix this too. ++ */ ++ png_ptr->background.red = (png_uint_16)(png_ptr->background.red * 257); ++ png_ptr->background.green = ++ (png_uint_16)(png_ptr->background.green * 257); ++ png_ptr->background.blue = (png_uint_16)(png_ptr->background.blue * 257); ++ png_ptr->background.gray = (png_uint_16)(png_ptr->background.gray * 257); ++ } ++#endif ++ ++ /* NOTE: below 'PNG_READ_ALPHA_MODE_SUPPORTED' is presumed to also enable the ++ * background support (see the comments in scripts/pnglibconf.dfa), this ++ * allows pre-multiplication of the alpha channel to be implemented as ++ * compositing on black. This is probably sub-optimal and has been done in ++ * 1.5.4 betas simply to enable external critique and testing (i.e. to ++ * implement the new API quickly, without lots of internal changes.) ++ */ ++ ++#ifdef PNG_READ_GAMMA_SUPPORTED ++# ifdef PNG_READ_BACKGROUND_SUPPORTED ++ /* Includes ALPHA_MODE */ ++ png_ptr->background_1 = png_ptr->background; ++# endif ++ ++ /* This needs to change - in the palette image case a whole set of tables are ++ * built when it would be quicker to just calculate the correct value for ++ * each palette entry directly. Also, the test is too tricky - why check ++ * PNG_RGB_TO_GRAY if PNG_GAMMA is not set? The answer seems to be that ++ * PNG_GAMMA is cancelled even if the gamma is known? The test excludes the ++ * PNG_COMPOSE case, so apparently if there is no *overall* gamma correction ++ * the gamma tables will not be built even if composition is required on a ++ * gamma encoded value. ++ * ++ * In 1.5.4 this is addressed below by an additional check on the individual ++ * file gamma - if it is not 1.0 both RGB_TO_GRAY and COMPOSE need the ++ * tables. ++ */ ++ if ((png_ptr->transformations & PNG_GAMMA) != 0 || ++ ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0 && ++ (png_gamma_significant(png_ptr->colorspace.gamma) != 0 || ++ png_gamma_significant(png_ptr->screen_gamma) != 0)) || ++ ((png_ptr->transformations & PNG_COMPOSE) != 0 && ++ (png_gamma_significant(png_ptr->colorspace.gamma) != 0 || ++ png_gamma_significant(png_ptr->screen_gamma) != 0 ++# ifdef PNG_READ_BACKGROUND_SUPPORTED ++ || (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_UNIQUE && ++ png_gamma_significant(png_ptr->background_gamma) != 0) ++# endif ++ )) || ((png_ptr->transformations & PNG_ENCODE_ALPHA) != 0 && ++ png_gamma_significant(png_ptr->screen_gamma) != 0)) ++ { ++ png_build_gamma_table(png_ptr, png_ptr->bit_depth); ++ ++#ifdef PNG_READ_BACKGROUND_SUPPORTED ++ if ((png_ptr->transformations & PNG_COMPOSE) != 0) ++ { ++ /* Issue a warning about this combination: because RGB_TO_GRAY is ++ * optimized to do the gamma transform if present yet do_background has ++ * to do the same thing if both options are set a ++ * double-gamma-correction happens. This is true in all versions of ++ * libpng to date. ++ */ ++ if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0) ++ png_warning(png_ptr, ++ "libpng does not support gamma+background+rgb_to_gray"); ++ ++ if ((png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) != 0) ++ { ++ /* We don't get to here unless there is a tRNS chunk with non-opaque ++ * entries - see the checking code at the start of this function. ++ */ ++ png_color back, back_1; ++ png_colorp palette = png_ptr->palette; ++ int num_palette = png_ptr->num_palette; ++ int i; ++ if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE) ++ { ++ ++ back.red = png_ptr->gamma_table[png_ptr->background.red]; ++ back.green = png_ptr->gamma_table[png_ptr->background.green]; ++ back.blue = png_ptr->gamma_table[png_ptr->background.blue]; ++ ++ back_1.red = png_ptr->gamma_to_1[png_ptr->background.red]; ++ back_1.green = png_ptr->gamma_to_1[png_ptr->background.green]; ++ back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue]; ++ } ++ else ++ { ++ png_fixed_point g, gs; ++ ++ switch (png_ptr->background_gamma_type) ++ { ++ case PNG_BACKGROUND_GAMMA_SCREEN: ++ g = (png_ptr->screen_gamma); ++ gs = PNG_FP_1; ++ break; ++ ++ case PNG_BACKGROUND_GAMMA_FILE: ++ g = png_reciprocal(png_ptr->colorspace.gamma); ++ gs = png_reciprocal2(png_ptr->colorspace.gamma, ++ png_ptr->screen_gamma); ++ break; ++ ++ case PNG_BACKGROUND_GAMMA_UNIQUE: ++ g = png_reciprocal(png_ptr->background_gamma); ++ gs = png_reciprocal2(png_ptr->background_gamma, ++ png_ptr->screen_gamma); ++ break; ++ default: ++ g = PNG_FP_1; /* back_1 */ ++ gs = PNG_FP_1; /* back */ ++ break; ++ } ++ ++ if (png_gamma_significant(gs) != 0) ++ { ++ back.red = png_gamma_8bit_correct(png_ptr->background.red, ++ gs); ++ back.green = png_gamma_8bit_correct(png_ptr->background.green, ++ gs); ++ back.blue = png_gamma_8bit_correct(png_ptr->background.blue, ++ gs); ++ } ++ ++ else ++ { ++ back.red = (png_byte)png_ptr->background.red; ++ back.green = (png_byte)png_ptr->background.green; ++ back.blue = (png_byte)png_ptr->background.blue; ++ } ++ ++ if (png_gamma_significant(g) != 0) ++ { ++ back_1.red = png_gamma_8bit_correct(png_ptr->background.red, ++ g); ++ back_1.green = png_gamma_8bit_correct( ++ png_ptr->background.green, g); ++ back_1.blue = png_gamma_8bit_correct(png_ptr->background.blue, ++ g); ++ } ++ ++ else ++ { ++ back_1.red = (png_byte)png_ptr->background.red; ++ back_1.green = (png_byte)png_ptr->background.green; ++ back_1.blue = (png_byte)png_ptr->background.blue; ++ } ++ } ++ ++ for (i = 0; i < num_palette; i++) ++ { ++ if (i < (int)png_ptr->num_trans && ++ png_ptr->trans_alpha[i] != 0xff) ++ { ++ if (png_ptr->trans_alpha[i] == 0) ++ { ++ palette[i] = back; ++ } ++ else /* if (png_ptr->trans_alpha[i] != 0xff) */ ++ { ++ png_byte v, w; ++ ++ v = png_ptr->gamma_to_1[palette[i].red]; ++ png_composite(w, v, png_ptr->trans_alpha[i], back_1.red); ++ palette[i].red = png_ptr->gamma_from_1[w]; ++ ++ v = png_ptr->gamma_to_1[palette[i].green]; ++ png_composite(w, v, png_ptr->trans_alpha[i], back_1.green); ++ palette[i].green = png_ptr->gamma_from_1[w]; ++ ++ v = png_ptr->gamma_to_1[palette[i].blue]; ++ png_composite(w, v, png_ptr->trans_alpha[i], back_1.blue); ++ palette[i].blue = png_ptr->gamma_from_1[w]; ++ } ++ } ++ else ++ { ++ palette[i].red = png_ptr->gamma_table[palette[i].red]; ++ palette[i].green = png_ptr->gamma_table[palette[i].green]; ++ palette[i].blue = png_ptr->gamma_table[palette[i].blue]; ++ } ++ } ++ ++ /* Prevent the transformations being done again. ++ * ++ * NOTE: this is highly dubious; it removes the transformations in ++ * place. This seems inconsistent with the general treatment of the ++ * transformations elsewhere. ++ */ ++ png_ptr->transformations &= ~(PNG_COMPOSE | PNG_GAMMA); ++ } /* color_type == PNG_COLOR_TYPE_PALETTE */ ++ ++ /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */ ++ else /* color_type != PNG_COLOR_TYPE_PALETTE */ ++ { ++ int gs_sig, g_sig; ++ png_fixed_point g = PNG_FP_1; /* Correction to linear */ ++ png_fixed_point gs = PNG_FP_1; /* Correction to screen */ ++ ++ switch (png_ptr->background_gamma_type) ++ { ++ case PNG_BACKGROUND_GAMMA_SCREEN: ++ g = png_ptr->screen_gamma; ++ /* gs = PNG_FP_1; */ ++ break; ++ ++ case PNG_BACKGROUND_GAMMA_FILE: ++ g = png_reciprocal(png_ptr->colorspace.gamma); ++ gs = png_reciprocal2(png_ptr->colorspace.gamma, ++ png_ptr->screen_gamma); ++ break; ++ ++ case PNG_BACKGROUND_GAMMA_UNIQUE: ++ g = png_reciprocal(png_ptr->background_gamma); ++ gs = png_reciprocal2(png_ptr->background_gamma, ++ png_ptr->screen_gamma); ++ break; ++ ++ default: ++ png_error(png_ptr, "invalid background gamma type"); ++ } ++ ++ g_sig = png_gamma_significant(g); ++ gs_sig = png_gamma_significant(gs); ++ ++ if (g_sig != 0) ++ png_ptr->background_1.gray = png_gamma_correct(png_ptr, ++ png_ptr->background.gray, g); ++ ++ if (gs_sig != 0) ++ png_ptr->background.gray = png_gamma_correct(png_ptr, ++ png_ptr->background.gray, gs); ++ ++ if ((png_ptr->background.red != png_ptr->background.green) || ++ (png_ptr->background.red != png_ptr->background.blue) || ++ (png_ptr->background.red != png_ptr->background.gray)) ++ { ++ /* RGB or RGBA with color background */ ++ if (g_sig != 0) ++ { ++ png_ptr->background_1.red = png_gamma_correct(png_ptr, ++ png_ptr->background.red, g); ++ ++ png_ptr->background_1.green = png_gamma_correct(png_ptr, ++ png_ptr->background.green, g); ++ ++ png_ptr->background_1.blue = png_gamma_correct(png_ptr, ++ png_ptr->background.blue, g); ++ } ++ ++ if (gs_sig != 0) ++ { ++ png_ptr->background.red = png_gamma_correct(png_ptr, ++ png_ptr->background.red, gs); ++ ++ png_ptr->background.green = png_gamma_correct(png_ptr, ++ png_ptr->background.green, gs); ++ ++ png_ptr->background.blue = png_gamma_correct(png_ptr, ++ png_ptr->background.blue, gs); ++ } ++ } ++ ++ else ++ { ++ /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */ ++ png_ptr->background_1.red = png_ptr->background_1.green ++ = png_ptr->background_1.blue = png_ptr->background_1.gray; ++ ++ png_ptr->background.red = png_ptr->background.green ++ = png_ptr->background.blue = png_ptr->background.gray; ++ } ++ ++ /* The background is now in screen gamma: */ ++ png_ptr->background_gamma_type = PNG_BACKGROUND_GAMMA_SCREEN; ++ } /* color_type != PNG_COLOR_TYPE_PALETTE */ ++ }/* png_ptr->transformations & PNG_BACKGROUND */ ++ ++ else ++ /* Transformation does not include PNG_BACKGROUND */ ++#endif /* READ_BACKGROUND */ ++ if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ++#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED ++ /* RGB_TO_GRAY needs to have non-gamma-corrected values! */ ++ && ((png_ptr->transformations & PNG_EXPAND) == 0 || ++ (png_ptr->transformations & PNG_RGB_TO_GRAY) == 0) ++#endif ++ ) ++ { ++ png_colorp palette = png_ptr->palette; ++ int num_palette = png_ptr->num_palette; ++ int i; ++ ++ /* NOTE: there are other transformations that should probably be in ++ * here too. ++ */ ++ for (i = 0; i < num_palette; i++) ++ { ++ palette[i].red = png_ptr->gamma_table[palette[i].red]; ++ palette[i].green = png_ptr->gamma_table[palette[i].green]; ++ palette[i].blue = png_ptr->gamma_table[palette[i].blue]; ++ } ++ ++ /* Done the gamma correction. */ ++ png_ptr->transformations &= ~PNG_GAMMA; ++ } /* color_type == PALETTE && !PNG_BACKGROUND transformation */ ++ } ++#ifdef PNG_READ_BACKGROUND_SUPPORTED ++ else ++#endif ++#endif /* READ_GAMMA */ ++ ++#ifdef PNG_READ_BACKGROUND_SUPPORTED ++ /* No GAMMA transformation (see the hanging else 4 lines above) */ ++ if ((png_ptr->transformations & PNG_COMPOSE) != 0 && ++ (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)) ++ { ++ int i; ++ int istop = (int)png_ptr->num_trans; ++ png_color back; ++ png_colorp palette = png_ptr->palette; ++ ++ back.red = (png_byte)png_ptr->background.red; ++ back.green = (png_byte)png_ptr->background.green; ++ back.blue = (png_byte)png_ptr->background.blue; ++ ++ for (i = 0; i < istop; i++) ++ { ++ if (png_ptr->trans_alpha[i] == 0) ++ { ++ palette[i] = back; ++ } ++ ++ else if (png_ptr->trans_alpha[i] != 0xff) ++ { ++ /* The png_composite() macro is defined in png.h */ ++ png_composite(palette[i].red, palette[i].red, ++ png_ptr->trans_alpha[i], back.red); ++ ++ png_composite(palette[i].green, palette[i].green, ++ png_ptr->trans_alpha[i], back.green); ++ ++ png_composite(palette[i].blue, palette[i].blue, ++ png_ptr->trans_alpha[i], back.blue); ++ } ++ } ++ ++ png_ptr->transformations &= ~PNG_COMPOSE; ++ } ++#endif /* READ_BACKGROUND */ ++ ++#ifdef PNG_READ_SHIFT_SUPPORTED ++ if ((png_ptr->transformations & PNG_SHIFT) != 0 && ++ (png_ptr->transformations & PNG_EXPAND) == 0 && ++ (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)) ++ { ++ int i; ++ int istop = png_ptr->num_palette; ++ int shift = 8 - png_ptr->sig_bit.red; ++ ++ png_ptr->transformations &= ~PNG_SHIFT; ++ ++ /* significant bits can be in the range 1 to 7 for a meaningful result, if ++ * the number of significant bits is 0 then no shift is done (this is an ++ * error condition which is silently ignored.) ++ */ ++ if (shift > 0 && shift < 8) ++ for (i=0; ipalette[i].red; ++ ++ component >>= shift; ++ png_ptr->palette[i].red = (png_byte)component; ++ } ++ ++ shift = 8 - png_ptr->sig_bit.green; ++ if (shift > 0 && shift < 8) ++ for (i=0; ipalette[i].green; ++ ++ component >>= shift; ++ png_ptr->palette[i].green = (png_byte)component; ++ } ++ ++ shift = 8 - png_ptr->sig_bit.blue; ++ if (shift > 0 && shift < 8) ++ for (i=0; ipalette[i].blue; ++ ++ component >>= shift; ++ png_ptr->palette[i].blue = (png_byte)component; ++ } ++ } ++#endif /* READ_SHIFT */ ++} ++ ++/* Modify the info structure to reflect the transformations. The ++ * info should be updated so a PNG file could be written with it, ++ * assuming the transformations result in valid PNG data. ++ */ ++void /* PRIVATE */ ++png_read_transform_info(png_structrp png_ptr, png_inforp info_ptr) ++{ ++ png_debug(1, "in png_read_transform_info"); ++ ++#ifdef PNG_READ_EXPAND_SUPPORTED ++ if ((png_ptr->transformations & PNG_EXPAND) != 0) ++ { ++ if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ++ { ++ /* This check must match what actually happens in ++ * png_do_expand_palette; if it ever checks the tRNS chunk to see if ++ * it is all opaque we must do the same (at present it does not.) ++ */ ++ if (png_ptr->num_trans > 0) ++ info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA; ++ ++ else ++ info_ptr->color_type = PNG_COLOR_TYPE_RGB; ++ ++ info_ptr->bit_depth = 8; ++ info_ptr->num_trans = 0; ++ ++ if (png_ptr->palette == NULL) ++ png_error (png_ptr, "Palette is NULL in indexed image"); ++ } ++ else ++ { ++ if (png_ptr->num_trans != 0) ++ { ++ if ((png_ptr->transformations & PNG_EXPAND_tRNS) != 0) ++ info_ptr->color_type |= PNG_COLOR_MASK_ALPHA; ++ } ++ if (info_ptr->bit_depth < 8) ++ info_ptr->bit_depth = 8; ++ ++ info_ptr->num_trans = 0; ++ } ++ } ++#endif ++ ++#if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\ ++ defined(PNG_READ_ALPHA_MODE_SUPPORTED) ++ /* The following is almost certainly wrong unless the background value is in ++ * the screen space! ++ */ ++ if ((png_ptr->transformations & PNG_COMPOSE) != 0) ++ info_ptr->background = png_ptr->background; ++#endif ++ ++#ifdef PNG_READ_GAMMA_SUPPORTED ++ /* The following used to be conditional on PNG_GAMMA (prior to 1.5.4), ++ * however it seems that the code in png_init_read_transformations, which has ++ * been called before this from png_read_update_info->png_read_start_row ++ * sometimes does the gamma transform and cancels the flag. ++ * ++ * TODO: this looks wrong; the info_ptr should end up with a gamma equal to ++ * the screen_gamma value. The following probably results in weirdness if ++ * the info_ptr is used by the app after the rows have been read. ++ */ ++ info_ptr->colorspace.gamma = png_ptr->colorspace.gamma; ++#endif ++ ++ if (info_ptr->bit_depth == 16) ++ { ++# ifdef PNG_READ_16BIT_SUPPORTED ++# ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED ++ if ((png_ptr->transformations & PNG_SCALE_16_TO_8) != 0) ++ info_ptr->bit_depth = 8; ++# endif ++ ++# ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED ++ if ((png_ptr->transformations & PNG_16_TO_8) != 0) ++ info_ptr->bit_depth = 8; ++# endif ++ ++# else ++ /* No 16-bit support: force chopping 16-bit input down to 8, in this case ++ * the app program can chose if both APIs are available by setting the ++ * correct scaling to use. ++ */ ++# ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED ++ /* For compatibility with previous versions use the strip method by ++ * default. This code works because if PNG_SCALE_16_TO_8 is already ++ * set the code below will do that in preference to the chop. ++ */ ++ png_ptr->transformations |= PNG_16_TO_8; ++ info_ptr->bit_depth = 8; ++# else ++ ++# ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED ++ png_ptr->transformations |= PNG_SCALE_16_TO_8; ++ info_ptr->bit_depth = 8; ++# else ++ ++ CONFIGURATION ERROR: you must enable at least one 16 to 8 method ++# endif ++# endif ++#endif /* !READ_16BIT */ ++ } ++ ++#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED ++ if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0) ++ info_ptr->color_type = (png_byte)(info_ptr->color_type | ++ PNG_COLOR_MASK_COLOR); ++#endif ++ ++#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED ++ if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0) ++ info_ptr->color_type = (png_byte)(info_ptr->color_type & ++ ~PNG_COLOR_MASK_COLOR); ++#endif ++ ++#ifdef PNG_READ_QUANTIZE_SUPPORTED ++ if ((png_ptr->transformations & PNG_QUANTIZE) != 0) ++ { ++ if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) || ++ (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) && ++ png_ptr->palette_lookup != 0 && info_ptr->bit_depth == 8) ++ { ++ info_ptr->color_type = PNG_COLOR_TYPE_PALETTE; ++ } ++ } ++#endif ++ ++#ifdef PNG_READ_EXPAND_16_SUPPORTED ++ if ((png_ptr->transformations & PNG_EXPAND_16) != 0 && ++ info_ptr->bit_depth == 8 && ++ info_ptr->color_type != PNG_COLOR_TYPE_PALETTE) ++ { ++ info_ptr->bit_depth = 16; ++ } ++#endif ++ ++#ifdef PNG_READ_PACK_SUPPORTED ++ if ((png_ptr->transformations & PNG_PACK) != 0 && ++ (info_ptr->bit_depth < 8)) ++ info_ptr->bit_depth = 8; ++#endif ++ ++ if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ++ info_ptr->channels = 1; ++ ++ else if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0) ++ info_ptr->channels = 3; ++ ++ else ++ info_ptr->channels = 1; ++ ++#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED ++ if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0) ++ { ++ info_ptr->color_type = (png_byte)(info_ptr->color_type & ++ ~PNG_COLOR_MASK_ALPHA); ++ info_ptr->num_trans = 0; ++ } ++#endif ++ ++ if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0) ++ info_ptr->channels++; ++ ++#ifdef PNG_READ_FILLER_SUPPORTED ++ /* STRIP_ALPHA and FILLER allowed: MASK_ALPHA bit stripped above */ ++ if ((png_ptr->transformations & PNG_FILLER) != 0 && ++ (info_ptr->color_type == PNG_COLOR_TYPE_RGB || ++ info_ptr->color_type == PNG_COLOR_TYPE_GRAY)) ++ { ++ info_ptr->channels++; ++ /* If adding a true alpha channel not just filler */ ++ if ((png_ptr->transformations & PNG_ADD_ALPHA) != 0) ++ info_ptr->color_type |= PNG_COLOR_MASK_ALPHA; ++ } ++#endif ++ ++#if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \ ++defined(PNG_READ_USER_TRANSFORM_SUPPORTED) ++ if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0) ++ { ++ if (png_ptr->user_transform_depth != 0) ++ info_ptr->bit_depth = png_ptr->user_transform_depth; ++ ++ if (png_ptr->user_transform_channels != 0) ++ info_ptr->channels = png_ptr->user_transform_channels; ++ } ++#endif ++ ++ info_ptr->pixel_depth = (png_byte)(info_ptr->channels * ++ info_ptr->bit_depth); ++ ++ info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, info_ptr->width); ++ ++ /* Adding in 1.5.4: cache the above value in png_struct so that we can later ++ * check in png_rowbytes that the user buffer won't get overwritten. Note ++ * that the field is not always set - if png_read_update_info isn't called ++ * the application has to either not do any transforms or get the calculation ++ * right itself. ++ */ ++ png_ptr->info_rowbytes = info_ptr->rowbytes; ++ ++#ifndef PNG_READ_EXPAND_SUPPORTED ++ if (png_ptr != NULL) ++ return; ++#endif ++} ++ ++#ifdef PNG_READ_PACK_SUPPORTED ++/* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel, ++ * without changing the actual values. Thus, if you had a row with ++ * a bit depth of 1, you would end up with bytes that only contained ++ * the numbers 0 or 1. If you would rather they contain 0 and 255, use ++ * png_do_shift() after this. ++ */ ++static void ++png_do_unpack(png_row_infop row_info, png_bytep row) ++{ ++ png_debug(1, "in png_do_unpack"); ++ ++ if (row_info->bit_depth < 8) ++ { ++ png_uint_32 i; ++ png_uint_32 row_width=row_info->width; ++ ++ switch (row_info->bit_depth) ++ { ++ case 1: ++ { ++ png_bytep sp = row + (size_t)((row_width - 1) >> 3); ++ png_bytep dp = row + (size_t)row_width - 1; ++ png_uint_32 shift = 7U - ((row_width + 7U) & 0x07); ++ for (i = 0; i < row_width; i++) ++ { ++ *dp = (png_byte)((*sp >> shift) & 0x01); ++ ++ if (shift == 7) ++ { ++ shift = 0; ++ sp--; ++ } ++ ++ else ++ shift++; ++ ++ dp--; ++ } ++ break; ++ } ++ ++ case 2: ++ { ++ ++ png_bytep sp = row + (size_t)((row_width - 1) >> 2); ++ png_bytep dp = row + (size_t)row_width - 1; ++ png_uint_32 shift = ((3U - ((row_width + 3U) & 0x03)) << 1); ++ for (i = 0; i < row_width; i++) ++ { ++ *dp = (png_byte)((*sp >> shift) & 0x03); ++ ++ if (shift == 6) ++ { ++ shift = 0; ++ sp--; ++ } ++ ++ else ++ shift += 2; ++ ++ dp--; ++ } ++ break; ++ } ++ ++ case 4: ++ { ++ png_bytep sp = row + (size_t)((row_width - 1) >> 1); ++ png_bytep dp = row + (size_t)row_width - 1; ++ png_uint_32 shift = ((1U - ((row_width + 1U) & 0x01)) << 2); ++ for (i = 0; i < row_width; i++) ++ { ++ *dp = (png_byte)((*sp >> shift) & 0x0f); ++ ++ if (shift == 4) ++ { ++ shift = 0; ++ sp--; ++ } ++ ++ else ++ shift = 4; ++ ++ dp--; ++ } ++ break; ++ } ++ ++ default: ++ break; ++ } ++ row_info->bit_depth = 8; ++ row_info->pixel_depth = (png_byte)(8 * row_info->channels); ++ row_info->rowbytes = row_width * row_info->channels; ++ } ++} ++#endif ++ ++#ifdef PNG_READ_SHIFT_SUPPORTED ++/* Reverse the effects of png_do_shift. This routine merely shifts the ++ * pixels back to their significant bits values. Thus, if you have ++ * a row of bit depth 8, but only 5 are significant, this will shift ++ * the values back to 0 through 31. ++ */ ++static void ++png_do_unshift(png_row_infop row_info, png_bytep row, ++ png_const_color_8p sig_bits) ++{ ++ int color_type; ++ ++ png_debug(1, "in png_do_unshift"); ++ ++ /* The palette case has already been handled in the _init routine. */ ++ color_type = row_info->color_type; ++ ++ if (color_type != PNG_COLOR_TYPE_PALETTE) ++ { ++ int shift[4]; ++ int channels = 0; ++ int bit_depth = row_info->bit_depth; ++ ++ if ((color_type & PNG_COLOR_MASK_COLOR) != 0) ++ { ++ shift[channels++] = bit_depth - sig_bits->red; ++ shift[channels++] = bit_depth - sig_bits->green; ++ shift[channels++] = bit_depth - sig_bits->blue; ++ } ++ ++ else ++ { ++ shift[channels++] = bit_depth - sig_bits->gray; ++ } ++ ++ if ((color_type & PNG_COLOR_MASK_ALPHA) != 0) ++ { ++ shift[channels++] = bit_depth - sig_bits->alpha; ++ } ++ ++ { ++ int c, have_shift; ++ ++ for (c = have_shift = 0; c < channels; ++c) ++ { ++ /* A shift of more than the bit depth is an error condition but it ++ * gets ignored here. ++ */ ++ if (shift[c] <= 0 || shift[c] >= bit_depth) ++ shift[c] = 0; ++ ++ else ++ have_shift = 1; ++ } ++ ++ if (have_shift == 0) ++ return; ++ } ++ ++ switch (bit_depth) ++ { ++ default: ++ /* Must be 1bpp gray: should not be here! */ ++ /* NOTREACHED */ ++ break; ++ ++ case 2: ++ /* Must be 2bpp gray */ ++ /* assert(channels == 1 && shift[0] == 1) */ ++ { ++ png_bytep bp = row; ++ png_bytep bp_end = bp + row_info->rowbytes; ++ ++ while (bp < bp_end) ++ { ++ int b = (*bp >> 1) & 0x55; ++ *bp++ = (png_byte)b; ++ } ++ break; ++ } ++ ++ case 4: ++ /* Must be 4bpp gray */ ++ /* assert(channels == 1) */ ++ { ++ png_bytep bp = row; ++ png_bytep bp_end = bp + row_info->rowbytes; ++ int gray_shift = shift[0]; ++ int mask = 0xf >> gray_shift; ++ ++ mask |= mask << 4; ++ ++ while (bp < bp_end) ++ { ++ int b = (*bp >> gray_shift) & mask; ++ *bp++ = (png_byte)b; ++ } ++ break; ++ } ++ ++ case 8: ++ /* Single byte components, G, GA, RGB, RGBA */ ++ { ++ png_bytep bp = row; ++ png_bytep bp_end = bp + row_info->rowbytes; ++ int channel = 0; ++ ++ while (bp < bp_end) ++ { ++ int b = *bp >> shift[channel]; ++ if (++channel >= channels) ++ channel = 0; ++ *bp++ = (png_byte)b; ++ } ++ break; ++ } ++ ++#ifdef PNG_READ_16BIT_SUPPORTED ++ case 16: ++ /* Double byte components, G, GA, RGB, RGBA */ ++ { ++ png_bytep bp = row; ++ png_bytep bp_end = bp + row_info->rowbytes; ++ int channel = 0; ++ ++ while (bp < bp_end) ++ { ++ int value = (bp[0] << 8) + bp[1]; ++ ++ value >>= shift[channel]; ++ if (++channel >= channels) ++ channel = 0; ++ *bp++ = (png_byte)(value >> 8); ++ *bp++ = (png_byte)value; ++ } ++ break; ++ } ++#endif ++ } ++ } ++} ++#endif ++ ++#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED ++/* Scale rows of bit depth 16 down to 8 accurately */ ++static void ++png_do_scale_16_to_8(png_row_infop row_info, png_bytep row) ++{ ++ png_debug(1, "in png_do_scale_16_to_8"); ++ ++ if (row_info->bit_depth == 16) ++ { ++ png_bytep sp = row; /* source */ ++ png_bytep dp = row; /* destination */ ++ png_bytep ep = sp + row_info->rowbytes; /* end+1 */ ++ ++ while (sp < ep) ++ { ++ /* The input is an array of 16-bit components, these must be scaled to ++ * 8 bits each. For a 16-bit value V the required value (from the PNG ++ * specification) is: ++ * ++ * (V * 255) / 65535 ++ * ++ * This reduces to round(V / 257), or floor((V + 128.5)/257) ++ * ++ * Represent V as the two byte value vhi.vlo. Make a guess that the ++ * result is the top byte of V, vhi, then the correction to this value ++ * is: ++ * ++ * error = floor(((V-vhi.vhi) + 128.5) / 257) ++ * = floor(((vlo-vhi) + 128.5) / 257) ++ * ++ * This can be approximated using integer arithmetic (and a signed ++ * shift): ++ * ++ * error = (vlo-vhi+128) >> 8; ++ * ++ * The approximate differs from the exact answer only when (vlo-vhi) is ++ * 128; it then gives a correction of +1 when the exact correction is ++ * 0. This gives 128 errors. The exact answer (correct for all 16-bit ++ * input values) is: ++ * ++ * error = (vlo-vhi+128)*65535 >> 24; ++ * ++ * An alternative arithmetic calculation which also gives no errors is: ++ * ++ * (V * 255 + 32895) >> 16 ++ */ ++ ++ png_int_32 tmp = *sp++; /* must be signed! */ ++ tmp += (((int)*sp++ - tmp + 128) * 65535) >> 24; ++ *dp++ = (png_byte)tmp; ++ } ++ ++ row_info->bit_depth = 8; ++ row_info->pixel_depth = (png_byte)(8 * row_info->channels); ++ row_info->rowbytes = row_info->width * row_info->channels; ++ } ++} ++#endif ++ ++#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED ++static void ++/* Simply discard the low byte. This was the default behavior prior ++ * to libpng-1.5.4. ++ */ ++png_do_chop(png_row_infop row_info, png_bytep row) ++{ ++ png_debug(1, "in png_do_chop"); ++ ++ if (row_info->bit_depth == 16) ++ { ++ png_bytep sp = row; /* source */ ++ png_bytep dp = row; /* destination */ ++ png_bytep ep = sp + row_info->rowbytes; /* end+1 */ ++ ++ while (sp < ep) ++ { ++ *dp++ = *sp; ++ sp += 2; /* skip low byte */ ++ } ++ ++ row_info->bit_depth = 8; ++ row_info->pixel_depth = (png_byte)(8 * row_info->channels); ++ row_info->rowbytes = row_info->width * row_info->channels; ++ } ++} ++#endif ++ ++#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED ++static void ++png_do_read_swap_alpha(png_row_infop row_info, png_bytep row) ++{ ++ png_uint_32 row_width = row_info->width; ++ ++ png_debug(1, "in png_do_read_swap_alpha"); ++ ++ if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) ++ { ++ /* This converts from RGBA to ARGB */ ++ if (row_info->bit_depth == 8) ++ { ++ png_bytep sp = row + row_info->rowbytes; ++ png_bytep dp = sp; ++ png_byte save; ++ png_uint_32 i; ++ ++ for (i = 0; i < row_width; i++) ++ { ++ save = *(--sp); ++ *(--dp) = *(--sp); ++ *(--dp) = *(--sp); ++ *(--dp) = *(--sp); ++ *(--dp) = save; ++ } ++ } ++ ++#ifdef PNG_READ_16BIT_SUPPORTED ++ /* This converts from RRGGBBAA to AARRGGBB */ ++ else ++ { ++ png_bytep sp = row + row_info->rowbytes; ++ png_bytep dp = sp; ++ png_byte save[2]; ++ png_uint_32 i; ++ ++ for (i = 0; i < row_width; i++) ++ { ++ save[0] = *(--sp); ++ save[1] = *(--sp); ++ *(--dp) = *(--sp); ++ *(--dp) = *(--sp); ++ *(--dp) = *(--sp); ++ *(--dp) = *(--sp); ++ *(--dp) = *(--sp); ++ *(--dp) = *(--sp); ++ *(--dp) = save[0]; ++ *(--dp) = save[1]; ++ } ++ } ++#endif ++ } ++ ++ else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ++ { ++ /* This converts from GA to AG */ ++ if (row_info->bit_depth == 8) ++ { ++ png_bytep sp = row + row_info->rowbytes; ++ png_bytep dp = sp; ++ png_byte save; ++ png_uint_32 i; ++ ++ for (i = 0; i < row_width; i++) ++ { ++ save = *(--sp); ++ *(--dp) = *(--sp); ++ *(--dp) = save; ++ } ++ } ++ ++#ifdef PNG_READ_16BIT_SUPPORTED ++ /* This converts from GGAA to AAGG */ ++ else ++ { ++ png_bytep sp = row + row_info->rowbytes; ++ png_bytep dp = sp; ++ png_byte save[2]; ++ png_uint_32 i; ++ ++ for (i = 0; i < row_width; i++) ++ { ++ save[0] = *(--sp); ++ save[1] = *(--sp); ++ *(--dp) = *(--sp); ++ *(--dp) = *(--sp); ++ *(--dp) = save[0]; ++ *(--dp) = save[1]; ++ } ++ } ++#endif ++ } ++} ++#endif ++ ++#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED ++static void ++png_do_read_invert_alpha(png_row_infop row_info, png_bytep row) ++{ ++ png_uint_32 row_width; ++ png_debug(1, "in png_do_read_invert_alpha"); ++ ++ row_width = row_info->width; ++ if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) ++ { ++ if (row_info->bit_depth == 8) ++ { ++ /* This inverts the alpha channel in RGBA */ ++ png_bytep sp = row + row_info->rowbytes; ++ png_bytep dp = sp; ++ png_uint_32 i; ++ ++ for (i = 0; i < row_width; i++) ++ { ++ *(--dp) = (png_byte)(255 - *(--sp)); ++ ++/* This does nothing: ++ *(--dp) = *(--sp); ++ *(--dp) = *(--sp); ++ *(--dp) = *(--sp); ++ We can replace it with: ++*/ ++ sp-=3; ++ dp=sp; ++ } ++ } ++ ++#ifdef PNG_READ_16BIT_SUPPORTED ++ /* This inverts the alpha channel in RRGGBBAA */ ++ else ++ { ++ png_bytep sp = row + row_info->rowbytes; ++ png_bytep dp = sp; ++ png_uint_32 i; ++ ++ for (i = 0; i < row_width; i++) ++ { ++ *(--dp) = (png_byte)(255 - *(--sp)); ++ *(--dp) = (png_byte)(255 - *(--sp)); ++ ++/* This does nothing: ++ *(--dp) = *(--sp); ++ *(--dp) = *(--sp); ++ *(--dp) = *(--sp); ++ *(--dp) = *(--sp); ++ *(--dp) = *(--sp); ++ *(--dp) = *(--sp); ++ We can replace it with: ++*/ ++ sp-=6; ++ dp=sp; ++ } ++ } ++#endif ++ } ++ else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ++ { ++ if (row_info->bit_depth == 8) ++ { ++ /* This inverts the alpha channel in GA */ ++ png_bytep sp = row + row_info->rowbytes; ++ png_bytep dp = sp; ++ png_uint_32 i; ++ ++ for (i = 0; i < row_width; i++) ++ { ++ *(--dp) = (png_byte)(255 - *(--sp)); ++ *(--dp) = *(--sp); ++ } ++ } ++ ++#ifdef PNG_READ_16BIT_SUPPORTED ++ else ++ { ++ /* This inverts the alpha channel in GGAA */ ++ png_bytep sp = row + row_info->rowbytes; ++ png_bytep dp = sp; ++ png_uint_32 i; ++ ++ for (i = 0; i < row_width; i++) ++ { ++ *(--dp) = (png_byte)(255 - *(--sp)); ++ *(--dp) = (png_byte)(255 - *(--sp)); ++/* ++ *(--dp) = *(--sp); ++ *(--dp) = *(--sp); ++*/ ++ sp-=2; ++ dp=sp; ++ } ++ } ++#endif ++ } ++} ++#endif ++ ++#ifdef PNG_READ_FILLER_SUPPORTED ++/* Add filler channel if we have RGB color */ ++static void ++png_do_read_filler(png_row_infop row_info, png_bytep row, ++ png_uint_32 filler, png_uint_32 flags) ++{ ++ png_uint_32 i; ++ png_uint_32 row_width = row_info->width; ++ ++#ifdef PNG_READ_16BIT_SUPPORTED ++ png_byte hi_filler = (png_byte)(filler>>8); ++#endif ++ png_byte lo_filler = (png_byte)filler; ++ ++ png_debug(1, "in png_do_read_filler"); ++ ++ if ( ++ row_info->color_type == PNG_COLOR_TYPE_GRAY) ++ { ++ if (row_info->bit_depth == 8) ++ { ++ if ((flags & PNG_FLAG_FILLER_AFTER) != 0) ++ { ++ /* This changes the data from G to GX */ ++ png_bytep sp = row + (size_t)row_width; ++ png_bytep dp = sp + (size_t)row_width; ++ for (i = 1; i < row_width; i++) ++ { ++ *(--dp) = lo_filler; ++ *(--dp) = *(--sp); ++ } ++ *(--dp) = lo_filler; ++ row_info->channels = 2; ++ row_info->pixel_depth = 16; ++ row_info->rowbytes = row_width * 2; ++ } ++ ++ else ++ { ++ /* This changes the data from G to XG */ ++ png_bytep sp = row + (size_t)row_width; ++ png_bytep dp = sp + (size_t)row_width; ++ for (i = 0; i < row_width; i++) ++ { ++ *(--dp) = *(--sp); ++ *(--dp) = lo_filler; ++ } ++ row_info->channels = 2; ++ row_info->pixel_depth = 16; ++ row_info->rowbytes = row_width * 2; ++ } ++ } ++ ++#ifdef PNG_READ_16BIT_SUPPORTED ++ else if (row_info->bit_depth == 16) ++ { ++ if ((flags & PNG_FLAG_FILLER_AFTER) != 0) ++ { ++ /* This changes the data from GG to GGXX */ ++ png_bytep sp = row + (size_t)row_width * 2; ++ png_bytep dp = sp + (size_t)row_width * 2; ++ for (i = 1; i < row_width; i++) ++ { ++ *(--dp) = lo_filler; ++ *(--dp) = hi_filler; ++ *(--dp) = *(--sp); ++ *(--dp) = *(--sp); ++ } ++ *(--dp) = lo_filler; ++ *(--dp) = hi_filler; ++ row_info->channels = 2; ++ row_info->pixel_depth = 32; ++ row_info->rowbytes = row_width * 4; ++ } ++ ++ else ++ { ++ /* This changes the data from GG to XXGG */ ++ png_bytep sp = row + (size_t)row_width * 2; ++ png_bytep dp = sp + (size_t)row_width * 2; ++ for (i = 0; i < row_width; i++) ++ { ++ *(--dp) = *(--sp); ++ *(--dp) = *(--sp); ++ *(--dp) = lo_filler; ++ *(--dp) = hi_filler; ++ } ++ row_info->channels = 2; ++ row_info->pixel_depth = 32; ++ row_info->rowbytes = row_width * 4; ++ } ++ } ++#endif ++ } /* COLOR_TYPE == GRAY */ ++ else if (row_info->color_type == PNG_COLOR_TYPE_RGB) ++ { ++ if (row_info->bit_depth == 8) ++ { ++ if ((flags & PNG_FLAG_FILLER_AFTER) != 0) ++ { ++ /* This changes the data from RGB to RGBX */ ++ png_bytep sp = row + (size_t)row_width * 3; ++ png_bytep dp = sp + (size_t)row_width; ++ for (i = 1; i < row_width; i++) ++ { ++ *(--dp) = lo_filler; ++ *(--dp) = *(--sp); ++ *(--dp) = *(--sp); ++ *(--dp) = *(--sp); ++ } ++ *(--dp) = lo_filler; ++ row_info->channels = 4; ++ row_info->pixel_depth = 32; ++ row_info->rowbytes = row_width * 4; ++ } ++ ++ else ++ { ++ /* This changes the data from RGB to XRGB */ ++ png_bytep sp = row + (size_t)row_width * 3; ++ png_bytep dp = sp + (size_t)row_width; ++ for (i = 0; i < row_width; i++) ++ { ++ *(--dp) = *(--sp); ++ *(--dp) = *(--sp); ++ *(--dp) = *(--sp); ++ *(--dp) = lo_filler; ++ } ++ row_info->channels = 4; ++ row_info->pixel_depth = 32; ++ row_info->rowbytes = row_width * 4; ++ } ++ } ++ ++#ifdef PNG_READ_16BIT_SUPPORTED ++ else if (row_info->bit_depth == 16) ++ { ++ if ((flags & PNG_FLAG_FILLER_AFTER) != 0) ++ { ++ /* This changes the data from RRGGBB to RRGGBBXX */ ++ png_bytep sp = row + (size_t)row_width * 6; ++ png_bytep dp = sp + (size_t)row_width * 2; ++ for (i = 1; i < row_width; i++) ++ { ++ *(--dp) = lo_filler; ++ *(--dp) = hi_filler; ++ *(--dp) = *(--sp); ++ *(--dp) = *(--sp); ++ *(--dp) = *(--sp); ++ *(--dp) = *(--sp); ++ *(--dp) = *(--sp); ++ *(--dp) = *(--sp); ++ } ++ *(--dp) = lo_filler; ++ *(--dp) = hi_filler; ++ row_info->channels = 4; ++ row_info->pixel_depth = 64; ++ row_info->rowbytes = row_width * 8; ++ } ++ ++ else ++ { ++ /* This changes the data from RRGGBB to XXRRGGBB */ ++ png_bytep sp = row + (size_t)row_width * 6; ++ png_bytep dp = sp + (size_t)row_width * 2; ++ for (i = 0; i < row_width; i++) ++ { ++ *(--dp) = *(--sp); ++ *(--dp) = *(--sp); ++ *(--dp) = *(--sp); ++ *(--dp) = *(--sp); ++ *(--dp) = *(--sp); ++ *(--dp) = *(--sp); ++ *(--dp) = lo_filler; ++ *(--dp) = hi_filler; ++ } ++ ++ row_info->channels = 4; ++ row_info->pixel_depth = 64; ++ row_info->rowbytes = row_width * 8; ++ } ++ } ++#endif ++ } /* COLOR_TYPE == RGB */ ++} ++#endif ++ ++#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED ++/* Expand grayscale files to RGB, with or without alpha */ ++static void ++png_do_gray_to_rgb(png_row_infop row_info, png_bytep row) ++{ ++ png_uint_32 i; ++ png_uint_32 row_width = row_info->width; ++ ++ png_debug(1, "in png_do_gray_to_rgb"); ++ ++ if (row_info->bit_depth >= 8 && ++ (row_info->color_type & PNG_COLOR_MASK_COLOR) == 0) ++ { ++ if (row_info->color_type == PNG_COLOR_TYPE_GRAY) ++ { ++ if (row_info->bit_depth == 8) ++ { ++ /* This changes G to RGB */ ++ png_bytep sp = row + (size_t)row_width - 1; ++ png_bytep dp = sp + (size_t)row_width * 2; ++ for (i = 0; i < row_width; i++) ++ { ++ *(dp--) = *sp; ++ *(dp--) = *sp; ++ *(dp--) = *(sp--); ++ } ++ } ++ ++ else ++ { ++ /* This changes GG to RRGGBB */ ++ png_bytep sp = row + (size_t)row_width * 2 - 1; ++ png_bytep dp = sp + (size_t)row_width * 4; ++ for (i = 0; i < row_width; i++) ++ { ++ *(dp--) = *sp; ++ *(dp--) = *(sp - 1); ++ *(dp--) = *sp; ++ *(dp--) = *(sp - 1); ++ *(dp--) = *(sp--); ++ *(dp--) = *(sp--); ++ } ++ } ++ } ++ ++ else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ++ { ++ if (row_info->bit_depth == 8) ++ { ++ /* This changes GA to RGBA */ ++ png_bytep sp = row + (size_t)row_width * 2 - 1; ++ png_bytep dp = sp + (size_t)row_width * 2; ++ for (i = 0; i < row_width; i++) ++ { ++ *(dp--) = *(sp--); ++ *(dp--) = *sp; ++ *(dp--) = *sp; ++ *(dp--) = *(sp--); ++ } ++ } ++ ++ else ++ { ++ /* This changes GGAA to RRGGBBAA */ ++ png_bytep sp = row + (size_t)row_width * 4 - 1; ++ png_bytep dp = sp + (size_t)row_width * 4; ++ for (i = 0; i < row_width; i++) ++ { ++ *(dp--) = *(sp--); ++ *(dp--) = *(sp--); ++ *(dp--) = *sp; ++ *(dp--) = *(sp - 1); ++ *(dp--) = *sp; ++ *(dp--) = *(sp - 1); ++ *(dp--) = *(sp--); ++ *(dp--) = *(sp--); ++ } ++ } ++ } ++ row_info->channels = (png_byte)(row_info->channels + 2); ++ row_info->color_type |= PNG_COLOR_MASK_COLOR; ++ row_info->pixel_depth = (png_byte)(row_info->channels * ++ row_info->bit_depth); ++ row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); ++ } ++} ++#endif ++ ++#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED ++/* Reduce RGB files to grayscale, with or without alpha ++ * using the equation given in Poynton's ColorFAQ of 1998-01-04 at ++ * (THIS LINK IS DEAD June 2008 but ++ * versions dated 1998 through November 2002 have been archived at ++ * https://web.archive.org/web/20000816232553/www.inforamp.net/ ++ * ~poynton/notes/colour_and_gamma/ColorFAQ.txt ) ++ * Charles Poynton poynton at poynton.com ++ * ++ * Y = 0.212671 * R + 0.715160 * G + 0.072169 * B ++ * ++ * which can be expressed with integers as ++ * ++ * Y = (6969 * R + 23434 * G + 2365 * B)/32768 ++ * ++ * Poynton's current link (as of January 2003 through July 2011): ++ * ++ * has changed the numbers slightly: ++ * ++ * Y = 0.2126*R + 0.7152*G + 0.0722*B ++ * ++ * which can be expressed with integers as ++ * ++ * Y = (6966 * R + 23436 * G + 2366 * B)/32768 ++ * ++ * Historically, however, libpng uses numbers derived from the ITU-R Rec 709 ++ * end point chromaticities and the D65 white point. Depending on the ++ * precision used for the D65 white point this produces a variety of different ++ * numbers, however if the four decimal place value used in ITU-R Rec 709 is ++ * used (0.3127,0.3290) the Y calculation would be: ++ * ++ * Y = (6968 * R + 23435 * G + 2366 * B)/32768 ++ * ++ * While this is correct the rounding results in an overflow for white, because ++ * the sum of the rounded coefficients is 32769, not 32768. Consequently ++ * libpng uses, instead, the closest non-overflowing approximation: ++ * ++ * Y = (6968 * R + 23434 * G + 2366 * B)/32768 ++ * ++ * Starting with libpng-1.5.5, if the image being converted has a cHRM chunk ++ * (including an sRGB chunk) then the chromaticities are used to calculate the ++ * coefficients. See the chunk handling in pngrutil.c for more information. ++ * ++ * In all cases the calculation is to be done in a linear colorspace. If no ++ * gamma information is available to correct the encoding of the original RGB ++ * values this results in an implicit assumption that the original PNG RGB ++ * values were linear. ++ * ++ * Other integer coefficients can be used via png_set_rgb_to_gray(). Because ++ * the API takes just red and green coefficients the blue coefficient is ++ * calculated to make the sum 32768. This will result in different rounding ++ * to that used above. ++ */ ++static int ++png_do_rgb_to_gray(png_structrp png_ptr, png_row_infop row_info, png_bytep row) ++{ ++ int rgb_error = 0; ++ ++ png_debug(1, "in png_do_rgb_to_gray"); ++ ++ if ((row_info->color_type & PNG_COLOR_MASK_PALETTE) == 0 && ++ (row_info->color_type & PNG_COLOR_MASK_COLOR) != 0) ++ { ++ png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff; ++ png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff; ++ png_uint_32 bc = 32768 - rc - gc; ++ png_uint_32 row_width = row_info->width; ++ int have_alpha = (row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0; ++ ++ if (row_info->bit_depth == 8) ++ { ++#ifdef PNG_READ_GAMMA_SUPPORTED ++ /* Notice that gamma to/from 1 are not necessarily inverses (if ++ * there is an overall gamma correction). Prior to 1.5.5 this code ++ * checked the linearized values for equality; this doesn't match ++ * the documentation, the original values must be checked. ++ */ ++ if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL) ++ { ++ png_bytep sp = row; ++ png_bytep dp = row; ++ png_uint_32 i; ++ ++ for (i = 0; i < row_width; i++) ++ { ++ png_byte red = *(sp++); ++ png_byte green = *(sp++); ++ png_byte blue = *(sp++); ++ ++ if (red != green || red != blue) ++ { ++ red = png_ptr->gamma_to_1[red]; ++ green = png_ptr->gamma_to_1[green]; ++ blue = png_ptr->gamma_to_1[blue]; ++ ++ rgb_error |= 1; ++ *(dp++) = png_ptr->gamma_from_1[ ++ (rc*red + gc*green + bc*blue + 16384)>>15]; ++ } ++ ++ else ++ { ++ /* If there is no overall correction the table will not be ++ * set. ++ */ ++ if (png_ptr->gamma_table != NULL) ++ red = png_ptr->gamma_table[red]; ++ ++ *(dp++) = red; ++ } ++ ++ if (have_alpha != 0) ++ *(dp++) = *(sp++); ++ } ++ } ++ else ++#endif ++ { ++ png_bytep sp = row; ++ png_bytep dp = row; ++ png_uint_32 i; ++ ++ for (i = 0; i < row_width; i++) ++ { ++ png_byte red = *(sp++); ++ png_byte green = *(sp++); ++ png_byte blue = *(sp++); ++ ++ if (red != green || red != blue) ++ { ++ rgb_error |= 1; ++ /* NOTE: this is the historical approach which simply ++ * truncates the results. ++ */ ++ *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15); ++ } ++ ++ else ++ *(dp++) = red; ++ ++ if (have_alpha != 0) ++ *(dp++) = *(sp++); ++ } ++ } ++ } ++ ++ else /* RGB bit_depth == 16 */ ++ { ++#ifdef PNG_READ_GAMMA_SUPPORTED ++ if (png_ptr->gamma_16_to_1 != NULL && png_ptr->gamma_16_from_1 != NULL) ++ { ++ png_bytep sp = row; ++ png_bytep dp = row; ++ png_uint_32 i; ++ ++ for (i = 0; i < row_width; i++) ++ { ++ png_uint_16 red, green, blue, w; ++ png_byte hi,lo; ++ ++ hi=*(sp)++; lo=*(sp)++; red = (png_uint_16)((hi << 8) | (lo)); ++ hi=*(sp)++; lo=*(sp)++; green = (png_uint_16)((hi << 8) | (lo)); ++ hi=*(sp)++; lo=*(sp)++; blue = (png_uint_16)((hi << 8) | (lo)); ++ ++ if (red == green && red == blue) ++ { ++ if (png_ptr->gamma_16_table != NULL) ++ w = png_ptr->gamma_16_table[(red & 0xff) ++ >> png_ptr->gamma_shift][red >> 8]; ++ ++ else ++ w = red; ++ } ++ ++ else ++ { ++ png_uint_16 red_1 = png_ptr->gamma_16_to_1[(red & 0xff) ++ >> png_ptr->gamma_shift][red>>8]; ++ png_uint_16 green_1 = ++ png_ptr->gamma_16_to_1[(green & 0xff) >> ++ png_ptr->gamma_shift][green>>8]; ++ png_uint_16 blue_1 = png_ptr->gamma_16_to_1[(blue & 0xff) ++ >> png_ptr->gamma_shift][blue>>8]; ++ png_uint_16 gray16 = (png_uint_16)((rc*red_1 + gc*green_1 ++ + bc*blue_1 + 16384)>>15); ++ w = png_ptr->gamma_16_from_1[(gray16 & 0xff) >> ++ png_ptr->gamma_shift][gray16 >> 8]; ++ rgb_error |= 1; ++ } ++ ++ *(dp++) = (png_byte)((w>>8) & 0xff); ++ *(dp++) = (png_byte)(w & 0xff); ++ ++ if (have_alpha != 0) ++ { ++ *(dp++) = *(sp++); ++ *(dp++) = *(sp++); ++ } ++ } ++ } ++ else ++#endif ++ { ++ png_bytep sp = row; ++ png_bytep dp = row; ++ png_uint_32 i; ++ ++ for (i = 0; i < row_width; i++) ++ { ++ png_uint_16 red, green, blue, gray16; ++ png_byte hi,lo; ++ ++ hi=*(sp)++; lo=*(sp)++; red = (png_uint_16)((hi << 8) | (lo)); ++ hi=*(sp)++; lo=*(sp)++; green = (png_uint_16)((hi << 8) | (lo)); ++ hi=*(sp)++; lo=*(sp)++; blue = (png_uint_16)((hi << 8) | (lo)); ++ ++ if (red != green || red != blue) ++ rgb_error |= 1; ++ ++ /* From 1.5.5 in the 16-bit case do the accurate conversion even ++ * in the 'fast' case - this is because this is where the code ++ * ends up when handling linear 16-bit data. ++ */ ++ gray16 = (png_uint_16)((rc*red + gc*green + bc*blue + 16384) >> ++ 15); ++ *(dp++) = (png_byte)((gray16 >> 8) & 0xff); ++ *(dp++) = (png_byte)(gray16 & 0xff); ++ ++ if (have_alpha != 0) ++ { ++ *(dp++) = *(sp++); ++ *(dp++) = *(sp++); ++ } ++ } ++ } ++ } ++ ++ row_info->channels = (png_byte)(row_info->channels - 2); ++ row_info->color_type = (png_byte)(row_info->color_type & ++ ~PNG_COLOR_MASK_COLOR); ++ row_info->pixel_depth = (png_byte)(row_info->channels * ++ row_info->bit_depth); ++ row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); ++ } ++ return rgb_error; ++} ++#endif ++ ++#if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\ ++ defined(PNG_READ_ALPHA_MODE_SUPPORTED) ++/* Replace any alpha or transparency with the supplied background color. ++ * "background" is already in the screen gamma, while "background_1" is ++ * at a gamma of 1.0. Paletted files have already been taken care of. ++ */ ++static void ++png_do_compose(png_row_infop row_info, png_bytep row, png_structrp png_ptr) ++{ ++#ifdef PNG_READ_GAMMA_SUPPORTED ++ png_const_bytep gamma_table = png_ptr->gamma_table; ++ png_const_bytep gamma_from_1 = png_ptr->gamma_from_1; ++ png_const_bytep gamma_to_1 = png_ptr->gamma_to_1; ++ png_const_uint_16pp gamma_16 = png_ptr->gamma_16_table; ++ png_const_uint_16pp gamma_16_from_1 = png_ptr->gamma_16_from_1; ++ png_const_uint_16pp gamma_16_to_1 = png_ptr->gamma_16_to_1; ++ int gamma_shift = png_ptr->gamma_shift; ++ int optimize = (png_ptr->flags & PNG_FLAG_OPTIMIZE_ALPHA) != 0; ++#endif ++ ++ png_bytep sp; ++ png_uint_32 i; ++ png_uint_32 row_width = row_info->width; ++ int shift; ++ ++ png_debug(1, "in png_do_compose"); ++ ++ switch (row_info->color_type) ++ { ++ case PNG_COLOR_TYPE_GRAY: ++ { ++ switch (row_info->bit_depth) ++ { ++ case 1: ++ { ++ sp = row; ++ shift = 7; ++ for (i = 0; i < row_width; i++) ++ { ++ if ((png_uint_16)((*sp >> shift) & 0x01) ++ == png_ptr->trans_color.gray) ++ { ++ unsigned int tmp = *sp & (0x7f7f >> (7 - shift)); ++ tmp |= ++ (unsigned int)(png_ptr->background.gray << shift); ++ *sp = (png_byte)(tmp & 0xff); ++ } ++ ++ if (shift == 0) ++ { ++ shift = 7; ++ sp++; ++ } ++ ++ else ++ shift--; ++ } ++ break; ++ } ++ ++ case 2: ++ { ++#ifdef PNG_READ_GAMMA_SUPPORTED ++ if (gamma_table != NULL) ++ { ++ sp = row; ++ shift = 6; ++ for (i = 0; i < row_width; i++) ++ { ++ if ((png_uint_16)((*sp >> shift) & 0x03) ++ == png_ptr->trans_color.gray) ++ { ++ unsigned int tmp = *sp & (0x3f3f >> (6 - shift)); ++ tmp |= ++ (unsigned int)png_ptr->background.gray << shift; ++ *sp = (png_byte)(tmp & 0xff); ++ } ++ ++ else ++ { ++ unsigned int p = (*sp >> shift) & 0x03; ++ unsigned int g = (gamma_table [p | (p << 2) | ++ (p << 4) | (p << 6)] >> 6) & 0x03; ++ unsigned int tmp = *sp & (0x3f3f >> (6 - shift)); ++ tmp |= (unsigned int)(g << shift); ++ *sp = (png_byte)(tmp & 0xff); ++ } ++ ++ if (shift == 0) ++ { ++ shift = 6; ++ sp++; ++ } ++ ++ else ++ shift -= 2; ++ } ++ } ++ ++ else ++#endif ++ { ++ sp = row; ++ shift = 6; ++ for (i = 0; i < row_width; i++) ++ { ++ if ((png_uint_16)((*sp >> shift) & 0x03) ++ == png_ptr->trans_color.gray) ++ { ++ unsigned int tmp = *sp & (0x3f3f >> (6 - shift)); ++ tmp |= ++ (unsigned int)png_ptr->background.gray << shift; ++ *sp = (png_byte)(tmp & 0xff); ++ } ++ ++ if (shift == 0) ++ { ++ shift = 6; ++ sp++; ++ } ++ ++ else ++ shift -= 2; ++ } ++ } ++ break; ++ } ++ ++ case 4: ++ { ++#ifdef PNG_READ_GAMMA_SUPPORTED ++ if (gamma_table != NULL) ++ { ++ sp = row; ++ shift = 4; ++ for (i = 0; i < row_width; i++) ++ { ++ if ((png_uint_16)((*sp >> shift) & 0x0f) ++ == png_ptr->trans_color.gray) ++ { ++ unsigned int tmp = *sp & (0x0f0f >> (4 - shift)); ++ tmp |= ++ (unsigned int)(png_ptr->background.gray << shift); ++ *sp = (png_byte)(tmp & 0xff); ++ } ++ ++ else ++ { ++ unsigned int p = (*sp >> shift) & 0x0f; ++ unsigned int g = (gamma_table[p | (p << 4)] >> 4) & ++ 0x0f; ++ unsigned int tmp = *sp & (0x0f0f >> (4 - shift)); ++ tmp |= (unsigned int)(g << shift); ++ *sp = (png_byte)(tmp & 0xff); ++ } ++ ++ if (shift == 0) ++ { ++ shift = 4; ++ sp++; ++ } ++ ++ else ++ shift -= 4; ++ } ++ } ++ ++ else ++#endif ++ { ++ sp = row; ++ shift = 4; ++ for (i = 0; i < row_width; i++) ++ { ++ if ((png_uint_16)((*sp >> shift) & 0x0f) ++ == png_ptr->trans_color.gray) ++ { ++ unsigned int tmp = *sp & (0x0f0f >> (4 - shift)); ++ tmp |= ++ (unsigned int)(png_ptr->background.gray << shift); ++ *sp = (png_byte)(tmp & 0xff); ++ } ++ ++ if (shift == 0) ++ { ++ shift = 4; ++ sp++; ++ } ++ ++ else ++ shift -= 4; ++ } ++ } ++ break; ++ } ++ ++ case 8: ++ { ++#ifdef PNG_READ_GAMMA_SUPPORTED ++ if (gamma_table != NULL) ++ { ++ sp = row; ++ for (i = 0; i < row_width; i++, sp++) ++ { ++ if (*sp == png_ptr->trans_color.gray) ++ *sp = (png_byte)png_ptr->background.gray; ++ ++ else ++ *sp = gamma_table[*sp]; ++ } ++ } ++ else ++#endif ++ { ++ sp = row; ++ for (i = 0; i < row_width; i++, sp++) ++ { ++ if (*sp == png_ptr->trans_color.gray) ++ *sp = (png_byte)png_ptr->background.gray; ++ } ++ } ++ break; ++ } ++ ++ case 16: ++ { ++#ifdef PNG_READ_GAMMA_SUPPORTED ++ if (gamma_16 != NULL) ++ { ++ sp = row; ++ for (i = 0; i < row_width; i++, sp += 2) ++ { ++ png_uint_16 v; ++ ++ v = (png_uint_16)(((*sp) << 8) + *(sp + 1)); ++ ++ if (v == png_ptr->trans_color.gray) ++ { ++ /* Background is already in screen gamma */ ++ *sp = (png_byte)((png_ptr->background.gray >> 8) ++ & 0xff); ++ *(sp + 1) = (png_byte)(png_ptr->background.gray ++ & 0xff); ++ } ++ ++ else ++ { ++ v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; ++ *sp = (png_byte)((v >> 8) & 0xff); ++ *(sp + 1) = (png_byte)(v & 0xff); ++ } ++ } ++ } ++ else ++#endif ++ { ++ sp = row; ++ for (i = 0; i < row_width; i++, sp += 2) ++ { ++ png_uint_16 v; ++ ++ v = (png_uint_16)(((*sp) << 8) + *(sp + 1)); ++ ++ if (v == png_ptr->trans_color.gray) ++ { ++ *sp = (png_byte)((png_ptr->background.gray >> 8) ++ & 0xff); ++ *(sp + 1) = (png_byte)(png_ptr->background.gray ++ & 0xff); ++ } ++ } ++ } ++ break; ++ } ++ ++ default: ++ break; ++ } ++ break; ++ } ++ ++ case PNG_COLOR_TYPE_RGB: ++ { ++ if (row_info->bit_depth == 8) ++ { ++#ifdef PNG_READ_GAMMA_SUPPORTED ++ if (gamma_table != NULL) ++ { ++ sp = row; ++ for (i = 0; i < row_width; i++, sp += 3) ++ { ++ if (*sp == png_ptr->trans_color.red && ++ *(sp + 1) == png_ptr->trans_color.green && ++ *(sp + 2) == png_ptr->trans_color.blue) ++ { ++ *sp = (png_byte)png_ptr->background.red; ++ *(sp + 1) = (png_byte)png_ptr->background.green; ++ *(sp + 2) = (png_byte)png_ptr->background.blue; ++ } ++ ++ else ++ { ++ *sp = gamma_table[*sp]; ++ *(sp + 1) = gamma_table[*(sp + 1)]; ++ *(sp + 2) = gamma_table[*(sp + 2)]; ++ } ++ } ++ } ++ else ++#endif ++ { ++ sp = row; ++ for (i = 0; i < row_width; i++, sp += 3) ++ { ++ if (*sp == png_ptr->trans_color.red && ++ *(sp + 1) == png_ptr->trans_color.green && ++ *(sp + 2) == png_ptr->trans_color.blue) ++ { ++ *sp = (png_byte)png_ptr->background.red; ++ *(sp + 1) = (png_byte)png_ptr->background.green; ++ *(sp + 2) = (png_byte)png_ptr->background.blue; ++ } ++ } ++ } ++ } ++ else /* if (row_info->bit_depth == 16) */ ++ { ++#ifdef PNG_READ_GAMMA_SUPPORTED ++ if (gamma_16 != NULL) ++ { ++ sp = row; ++ for (i = 0; i < row_width; i++, sp += 6) ++ { ++ png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1)); ++ ++ png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8) ++ + *(sp + 3)); ++ ++ png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8) ++ + *(sp + 5)); ++ ++ if (r == png_ptr->trans_color.red && ++ g == png_ptr->trans_color.green && ++ b == png_ptr->trans_color.blue) ++ { ++ /* Background is already in screen gamma */ ++ *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff); ++ *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff); ++ *(sp + 2) = (png_byte)((png_ptr->background.green >> 8) ++ & 0xff); ++ *(sp + 3) = (png_byte)(png_ptr->background.green ++ & 0xff); ++ *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) ++ & 0xff); ++ *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff); ++ } ++ ++ else ++ { ++ png_uint_16 v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; ++ *sp = (png_byte)((v >> 8) & 0xff); ++ *(sp + 1) = (png_byte)(v & 0xff); ++ ++ v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)]; ++ *(sp + 2) = (png_byte)((v >> 8) & 0xff); ++ *(sp + 3) = (png_byte)(v & 0xff); ++ ++ v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)]; ++ *(sp + 4) = (png_byte)((v >> 8) & 0xff); ++ *(sp + 5) = (png_byte)(v & 0xff); ++ } ++ } ++ } ++ ++ else ++#endif ++ { ++ sp = row; ++ for (i = 0; i < row_width; i++, sp += 6) ++ { ++ png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1)); ++ ++ png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8) ++ + *(sp + 3)); ++ ++ png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8) ++ + *(sp + 5)); ++ ++ if (r == png_ptr->trans_color.red && ++ g == png_ptr->trans_color.green && ++ b == png_ptr->trans_color.blue) ++ { ++ *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff); ++ *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff); ++ *(sp + 2) = (png_byte)((png_ptr->background.green >> 8) ++ & 0xff); ++ *(sp + 3) = (png_byte)(png_ptr->background.green ++ & 0xff); ++ *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) ++ & 0xff); ++ *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff); ++ } ++ } ++ } ++ } ++ break; ++ } ++ ++ case PNG_COLOR_TYPE_GRAY_ALPHA: ++ { ++ if (row_info->bit_depth == 8) ++ { ++#ifdef PNG_READ_GAMMA_SUPPORTED ++ if (gamma_to_1 != NULL && gamma_from_1 != NULL && ++ gamma_table != NULL) ++ { ++ sp = row; ++ for (i = 0; i < row_width; i++, sp += 2) ++ { ++ png_uint_16 a = *(sp + 1); ++ ++ if (a == 0xff) ++ *sp = gamma_table[*sp]; ++ ++ else if (a == 0) ++ { ++ /* Background is already in screen gamma */ ++ *sp = (png_byte)png_ptr->background.gray; ++ } ++ ++ else ++ { ++ png_byte v, w; ++ ++ v = gamma_to_1[*sp]; ++ png_composite(w, v, a, png_ptr->background_1.gray); ++ if (optimize == 0) ++ w = gamma_from_1[w]; ++ *sp = w; ++ } ++ } ++ } ++ else ++#endif ++ { ++ sp = row; ++ for (i = 0; i < row_width; i++, sp += 2) ++ { ++ png_byte a = *(sp + 1); ++ ++ if (a == 0) ++ *sp = (png_byte)png_ptr->background.gray; ++ ++ else if (a < 0xff) ++ png_composite(*sp, *sp, a, png_ptr->background.gray); ++ } ++ } ++ } ++ else /* if (png_ptr->bit_depth == 16) */ ++ { ++#ifdef PNG_READ_GAMMA_SUPPORTED ++ if (gamma_16 != NULL && gamma_16_from_1 != NULL && ++ gamma_16_to_1 != NULL) ++ { ++ sp = row; ++ for (i = 0; i < row_width; i++, sp += 4) ++ { ++ png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8) ++ + *(sp + 3)); ++ ++ if (a == (png_uint_16)0xffff) ++ { ++ png_uint_16 v; ++ ++ v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; ++ *sp = (png_byte)((v >> 8) & 0xff); ++ *(sp + 1) = (png_byte)(v & 0xff); ++ } ++ ++ else if (a == 0) ++ { ++ /* Background is already in screen gamma */ ++ *sp = (png_byte)((png_ptr->background.gray >> 8) ++ & 0xff); ++ *(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff); ++ } ++ ++ else ++ { ++ png_uint_16 g, v, w; ++ ++ g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp]; ++ png_composite_16(v, g, a, png_ptr->background_1.gray); ++ if (optimize != 0) ++ w = v; ++ else ++ w = gamma_16_from_1[(v & 0xff) >> ++ gamma_shift][v >> 8]; ++ *sp = (png_byte)((w >> 8) & 0xff); ++ *(sp + 1) = (png_byte)(w & 0xff); ++ } ++ } ++ } ++ else ++#endif ++ { ++ sp = row; ++ for (i = 0; i < row_width; i++, sp += 4) ++ { ++ png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8) ++ + *(sp + 3)); ++ ++ if (a == 0) ++ { ++ *sp = (png_byte)((png_ptr->background.gray >> 8) ++ & 0xff); ++ *(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff); ++ } ++ ++ else if (a < 0xffff) ++ { ++ png_uint_16 g, v; ++ ++ g = (png_uint_16)(((*sp) << 8) + *(sp + 1)); ++ png_composite_16(v, g, a, png_ptr->background.gray); ++ *sp = (png_byte)((v >> 8) & 0xff); ++ *(sp + 1) = (png_byte)(v & 0xff); ++ } ++ } ++ } ++ } ++ break; ++ } ++ ++ case PNG_COLOR_TYPE_RGB_ALPHA: ++ { ++ if (row_info->bit_depth == 8) ++ { ++#ifdef PNG_READ_GAMMA_SUPPORTED ++ if (gamma_to_1 != NULL && gamma_from_1 != NULL && ++ gamma_table != NULL) ++ { ++ sp = row; ++ for (i = 0; i < row_width; i++, sp += 4) ++ { ++ png_byte a = *(sp + 3); ++ ++ if (a == 0xff) ++ { ++ *sp = gamma_table[*sp]; ++ *(sp + 1) = gamma_table[*(sp + 1)]; ++ *(sp + 2) = gamma_table[*(sp + 2)]; ++ } ++ ++ else if (a == 0) ++ { ++ /* Background is already in screen gamma */ ++ *sp = (png_byte)png_ptr->background.red; ++ *(sp + 1) = (png_byte)png_ptr->background.green; ++ *(sp + 2) = (png_byte)png_ptr->background.blue; ++ } ++ ++ else ++ { ++ png_byte v, w; ++ ++ v = gamma_to_1[*sp]; ++ png_composite(w, v, a, png_ptr->background_1.red); ++ if (optimize == 0) w = gamma_from_1[w]; ++ *sp = w; ++ ++ v = gamma_to_1[*(sp + 1)]; ++ png_composite(w, v, a, png_ptr->background_1.green); ++ if (optimize == 0) w = gamma_from_1[w]; ++ *(sp + 1) = w; ++ ++ v = gamma_to_1[*(sp + 2)]; ++ png_composite(w, v, a, png_ptr->background_1.blue); ++ if (optimize == 0) w = gamma_from_1[w]; ++ *(sp + 2) = w; ++ } ++ } ++ } ++ else ++#endif ++ { ++ sp = row; ++ for (i = 0; i < row_width; i++, sp += 4) ++ { ++ png_byte a = *(sp + 3); ++ ++ if (a == 0) ++ { ++ *sp = (png_byte)png_ptr->background.red; ++ *(sp + 1) = (png_byte)png_ptr->background.green; ++ *(sp + 2) = (png_byte)png_ptr->background.blue; ++ } ++ ++ else if (a < 0xff) ++ { ++ png_composite(*sp, *sp, a, png_ptr->background.red); ++ ++ png_composite(*(sp + 1), *(sp + 1), a, ++ png_ptr->background.green); ++ ++ png_composite(*(sp + 2), *(sp + 2), a, ++ png_ptr->background.blue); ++ } ++ } ++ } ++ } ++ else /* if (row_info->bit_depth == 16) */ ++ { ++#ifdef PNG_READ_GAMMA_SUPPORTED ++ if (gamma_16 != NULL && gamma_16_from_1 != NULL && ++ gamma_16_to_1 != NULL) ++ { ++ sp = row; ++ for (i = 0; i < row_width; i++, sp += 8) ++ { ++ png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6)) ++ << 8) + (png_uint_16)(*(sp + 7))); ++ ++ if (a == (png_uint_16)0xffff) ++ { ++ png_uint_16 v; ++ ++ v = gamma_16[*(sp + 1) >> gamma_shift][*sp]; ++ *sp = (png_byte)((v >> 8) & 0xff); ++ *(sp + 1) = (png_byte)(v & 0xff); ++ ++ v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)]; ++ *(sp + 2) = (png_byte)((v >> 8) & 0xff); ++ *(sp + 3) = (png_byte)(v & 0xff); ++ ++ v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)]; ++ *(sp + 4) = (png_byte)((v >> 8) & 0xff); ++ *(sp + 5) = (png_byte)(v & 0xff); ++ } ++ ++ else if (a == 0) ++ { ++ /* Background is already in screen gamma */ ++ *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff); ++ *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff); ++ *(sp + 2) = (png_byte)((png_ptr->background.green >> 8) ++ & 0xff); ++ *(sp + 3) = (png_byte)(png_ptr->background.green ++ & 0xff); ++ *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) ++ & 0xff); ++ *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff); ++ } ++ ++ else ++ { ++ png_uint_16 v, w; ++ ++ v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp]; ++ png_composite_16(w, v, a, png_ptr->background_1.red); ++ if (optimize == 0) ++ w = gamma_16_from_1[((w & 0xff) >> gamma_shift)][w >> ++ 8]; ++ *sp = (png_byte)((w >> 8) & 0xff); ++ *(sp + 1) = (png_byte)(w & 0xff); ++ ++ v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)]; ++ png_composite_16(w, v, a, png_ptr->background_1.green); ++ if (optimize == 0) ++ w = gamma_16_from_1[((w & 0xff) >> gamma_shift)][w >> ++ 8]; ++ ++ *(sp + 2) = (png_byte)((w >> 8) & 0xff); ++ *(sp + 3) = (png_byte)(w & 0xff); ++ ++ v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)]; ++ png_composite_16(w, v, a, png_ptr->background_1.blue); ++ if (optimize == 0) ++ w = gamma_16_from_1[((w & 0xff) >> gamma_shift)][w >> ++ 8]; ++ ++ *(sp + 4) = (png_byte)((w >> 8) & 0xff); ++ *(sp + 5) = (png_byte)(w & 0xff); ++ } ++ } ++ } ++ ++ else ++#endif ++ { ++ sp = row; ++ for (i = 0; i < row_width; i++, sp += 8) ++ { ++ png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6)) ++ << 8) + (png_uint_16)(*(sp + 7))); ++ ++ if (a == 0) ++ { ++ *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff); ++ *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff); ++ *(sp + 2) = (png_byte)((png_ptr->background.green >> 8) ++ & 0xff); ++ *(sp + 3) = (png_byte)(png_ptr->background.green ++ & 0xff); ++ *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8) ++ & 0xff); ++ *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff); ++ } ++ ++ else if (a < 0xffff) ++ { ++ png_uint_16 v; ++ ++ png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1)); ++ png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8) ++ + *(sp + 3)); ++ png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8) ++ + *(sp + 5)); ++ ++ png_composite_16(v, r, a, png_ptr->background.red); ++ *sp = (png_byte)((v >> 8) & 0xff); ++ *(sp + 1) = (png_byte)(v & 0xff); ++ ++ png_composite_16(v, g, a, png_ptr->background.green); ++ *(sp + 2) = (png_byte)((v >> 8) & 0xff); ++ *(sp + 3) = (png_byte)(v & 0xff); ++ ++ png_composite_16(v, b, a, png_ptr->background.blue); ++ *(sp + 4) = (png_byte)((v >> 8) & 0xff); ++ *(sp + 5) = (png_byte)(v & 0xff); ++ } ++ } ++ } ++ } ++ break; ++ } ++ ++ default: ++ break; ++ } ++} ++#endif /* READ_BACKGROUND || READ_ALPHA_MODE */ ++ ++#ifdef PNG_READ_GAMMA_SUPPORTED ++/* Gamma correct the image, avoiding the alpha channel. Make sure ++ * you do this after you deal with the transparency issue on grayscale ++ * or RGB images. If your bit depth is 8, use gamma_table, if it ++ * is 16, use gamma_16_table and gamma_shift. Build these with ++ * build_gamma_table(). ++ */ ++static void ++png_do_gamma(png_row_infop row_info, png_bytep row, png_structrp png_ptr) ++{ ++ png_const_bytep gamma_table = png_ptr->gamma_table; ++ png_const_uint_16pp gamma_16_table = png_ptr->gamma_16_table; ++ int gamma_shift = png_ptr->gamma_shift; ++ ++ png_bytep sp; ++ png_uint_32 i; ++ png_uint_32 row_width=row_info->width; ++ ++ png_debug(1, "in png_do_gamma"); ++ ++ if (((row_info->bit_depth <= 8 && gamma_table != NULL) || ++ (row_info->bit_depth == 16 && gamma_16_table != NULL))) ++ { ++ switch (row_info->color_type) ++ { ++ case PNG_COLOR_TYPE_RGB: ++ { ++ if (row_info->bit_depth == 8) ++ { ++ sp = row; ++ for (i = 0; i < row_width; i++) ++ { ++ *sp = gamma_table[*sp]; ++ sp++; ++ *sp = gamma_table[*sp]; ++ sp++; ++ *sp = gamma_table[*sp]; ++ sp++; ++ } ++ } ++ ++ else /* if (row_info->bit_depth == 16) */ ++ { ++ sp = row; ++ for (i = 0; i < row_width; i++) ++ { ++ png_uint_16 v; ++ ++ v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; ++ *sp = (png_byte)((v >> 8) & 0xff); ++ *(sp + 1) = (png_byte)(v & 0xff); ++ sp += 2; ++ ++ v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; ++ *sp = (png_byte)((v >> 8) & 0xff); ++ *(sp + 1) = (png_byte)(v & 0xff); ++ sp += 2; ++ ++ v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; ++ *sp = (png_byte)((v >> 8) & 0xff); ++ *(sp + 1) = (png_byte)(v & 0xff); ++ sp += 2; ++ } ++ } ++ break; ++ } ++ ++ case PNG_COLOR_TYPE_RGB_ALPHA: ++ { ++ if (row_info->bit_depth == 8) ++ { ++ sp = row; ++ for (i = 0; i < row_width; i++) ++ { ++ *sp = gamma_table[*sp]; ++ sp++; ++ ++ *sp = gamma_table[*sp]; ++ sp++; ++ ++ *sp = gamma_table[*sp]; ++ sp++; ++ ++ sp++; ++ } ++ } ++ ++ else /* if (row_info->bit_depth == 16) */ ++ { ++ sp = row; ++ for (i = 0; i < row_width; i++) ++ { ++ png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; ++ *sp = (png_byte)((v >> 8) & 0xff); ++ *(sp + 1) = (png_byte)(v & 0xff); ++ sp += 2; ++ ++ v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; ++ *sp = (png_byte)((v >> 8) & 0xff); ++ *(sp + 1) = (png_byte)(v & 0xff); ++ sp += 2; ++ ++ v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; ++ *sp = (png_byte)((v >> 8) & 0xff); ++ *(sp + 1) = (png_byte)(v & 0xff); ++ sp += 4; ++ } ++ } ++ break; ++ } ++ ++ case PNG_COLOR_TYPE_GRAY_ALPHA: ++ { ++ if (row_info->bit_depth == 8) ++ { ++ sp = row; ++ for (i = 0; i < row_width; i++) ++ { ++ *sp = gamma_table[*sp]; ++ sp += 2; ++ } ++ } ++ ++ else /* if (row_info->bit_depth == 16) */ ++ { ++ sp = row; ++ for (i = 0; i < row_width; i++) ++ { ++ png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; ++ *sp = (png_byte)((v >> 8) & 0xff); ++ *(sp + 1) = (png_byte)(v & 0xff); ++ sp += 4; ++ } ++ } ++ break; ++ } ++ ++ case PNG_COLOR_TYPE_GRAY: ++ { ++ if (row_info->bit_depth == 2) ++ { ++ sp = row; ++ for (i = 0; i < row_width; i += 4) ++ { ++ int a = *sp & 0xc0; ++ int b = *sp & 0x30; ++ int c = *sp & 0x0c; ++ int d = *sp & 0x03; ++ ++ *sp = (png_byte)( ++ ((((int)gamma_table[a|(a>>2)|(a>>4)|(a>>6)]) ) & 0xc0)| ++ ((((int)gamma_table[(b<<2)|b|(b>>2)|(b>>4)])>>2) & 0x30)| ++ ((((int)gamma_table[(c<<4)|(c<<2)|c|(c>>2)])>>4) & 0x0c)| ++ ((((int)gamma_table[(d<<6)|(d<<4)|(d<<2)|d])>>6) )); ++ sp++; ++ } ++ } ++ ++ if (row_info->bit_depth == 4) ++ { ++ sp = row; ++ for (i = 0; i < row_width; i += 2) ++ { ++ int msb = *sp & 0xf0; ++ int lsb = *sp & 0x0f; ++ ++ *sp = (png_byte)((((int)gamma_table[msb | (msb >> 4)]) & 0xf0) ++ | (((int)gamma_table[(lsb << 4) | lsb]) >> 4)); ++ sp++; ++ } ++ } ++ ++ else if (row_info->bit_depth == 8) ++ { ++ sp = row; ++ for (i = 0; i < row_width; i++) ++ { ++ *sp = gamma_table[*sp]; ++ sp++; ++ } ++ } ++ ++ else if (row_info->bit_depth == 16) ++ { ++ sp = row; ++ for (i = 0; i < row_width; i++) ++ { ++ png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp]; ++ *sp = (png_byte)((v >> 8) & 0xff); ++ *(sp + 1) = (png_byte)(v & 0xff); ++ sp += 2; ++ } ++ } ++ break; ++ } ++ ++ default: ++ break; ++ } ++ } ++} ++#endif ++ ++#ifdef PNG_READ_ALPHA_MODE_SUPPORTED ++/* Encode the alpha channel to the output gamma (the input channel is always ++ * linear.) Called only with color types that have an alpha channel. Needs the ++ * from_1 tables. ++ */ ++static void ++png_do_encode_alpha(png_row_infop row_info, png_bytep row, png_structrp png_ptr) ++{ ++ png_uint_32 row_width = row_info->width; ++ ++ png_debug(1, "in png_do_encode_alpha"); ++ ++ if ((row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0) ++ { ++ if (row_info->bit_depth == 8) ++ { ++ png_bytep table = png_ptr->gamma_from_1; ++ ++ if (table != NULL) ++ { ++ int step = (row_info->color_type & PNG_COLOR_MASK_COLOR) ? 4 : 2; ++ ++ /* The alpha channel is the last component: */ ++ row += step - 1; ++ ++ for (; row_width > 0; --row_width, row += step) ++ *row = table[*row]; ++ ++ return; ++ } ++ } ++ ++ else if (row_info->bit_depth == 16) ++ { ++ png_uint_16pp table = png_ptr->gamma_16_from_1; ++ int gamma_shift = png_ptr->gamma_shift; ++ ++ if (table != NULL) ++ { ++ int step = (row_info->color_type & PNG_COLOR_MASK_COLOR) ? 8 : 4; ++ ++ /* The alpha channel is the last component: */ ++ row += step - 2; ++ ++ for (; row_width > 0; --row_width, row += step) ++ { ++ png_uint_16 v; ++ ++ v = table[*(row + 1) >> gamma_shift][*row]; ++ *row = (png_byte)((v >> 8) & 0xff); ++ *(row + 1) = (png_byte)(v & 0xff); ++ } ++ ++ return; ++ } ++ } ++ } ++ ++ /* Only get to here if called with a weird row_info; no harm has been done, ++ * so just issue a warning. ++ */ ++ png_warning(png_ptr, "png_do_encode_alpha: unexpected call"); ++} ++#endif ++ ++#ifdef PNG_READ_EXPAND_SUPPORTED ++/* Expands a palette row to an RGB or RGBA row depending ++ * upon whether you supply trans and num_trans. ++ */ ++static void ++png_do_expand_palette(png_structrp png_ptr, png_row_infop row_info, ++ png_bytep row, png_const_colorp palette, png_const_bytep trans_alpha, ++ int num_trans) ++{ ++ int shift, value; ++ png_bytep sp, dp; ++ png_uint_32 i; ++ png_uint_32 row_width=row_info->width; ++ ++ png_debug(1, "in png_do_expand_palette"); ++ ++ if (row_info->color_type == PNG_COLOR_TYPE_PALETTE) ++ { ++ if (row_info->bit_depth < 8) ++ { ++ switch (row_info->bit_depth) ++ { ++ case 1: ++ { ++ sp = row + (size_t)((row_width - 1) >> 3); ++ dp = row + (size_t)row_width - 1; ++ shift = 7 - (int)((row_width + 7) & 0x07); ++ for (i = 0; i < row_width; i++) ++ { ++ if ((*sp >> shift) & 0x01) ++ *dp = 1; ++ ++ else ++ *dp = 0; ++ ++ if (shift == 7) ++ { ++ shift = 0; ++ sp--; ++ } ++ ++ else ++ shift++; ++ ++ dp--; ++ } ++ break; ++ } ++ ++ case 2: ++ { ++ sp = row + (size_t)((row_width - 1) >> 2); ++ dp = row + (size_t)row_width - 1; ++ shift = (int)((3 - ((row_width + 3) & 0x03)) << 1); ++ for (i = 0; i < row_width; i++) ++ { ++ value = (*sp >> shift) & 0x03; ++ *dp = (png_byte)value; ++ if (shift == 6) ++ { ++ shift = 0; ++ sp--; ++ } ++ ++ else ++ shift += 2; ++ ++ dp--; ++ } ++ break; ++ } ++ ++ case 4: ++ { ++ sp = row + (size_t)((row_width - 1) >> 1); ++ dp = row + (size_t)row_width - 1; ++ shift = (int)((row_width & 0x01) << 2); ++ for (i = 0; i < row_width; i++) ++ { ++ value = (*sp >> shift) & 0x0f; ++ *dp = (png_byte)value; ++ if (shift == 4) ++ { ++ shift = 0; ++ sp--; ++ } ++ ++ else ++ shift += 4; ++ ++ dp--; ++ } ++ break; ++ } ++ ++ default: ++ break; ++ } ++ row_info->bit_depth = 8; ++ row_info->pixel_depth = 8; ++ row_info->rowbytes = row_width; ++ } ++ ++ if (row_info->bit_depth == 8) ++ { ++ { ++ if (num_trans > 0) ++ { ++ sp = row + (size_t)row_width - 1; ++ dp = row + ((size_t)row_width << 2) - 1; ++ ++ i = 0; ++#ifdef PNG_ARM_NEON_INTRINSICS_AVAILABLE ++ if (png_ptr->riffled_palette != NULL) ++ { ++ /* The RGBA optimization works with png_ptr->bit_depth == 8 ++ * but sometimes row_info->bit_depth has been changed to 8. ++ * In these cases, the palette hasn't been riffled. ++ */ ++ i = png_do_expand_palette_rgba8_neon(png_ptr, row_info, row, ++ &sp, &dp); ++ } ++#else ++ PNG_UNUSED(png_ptr) ++#endif ++ ++ for (; i < row_width; i++) ++ { ++ if ((int)(*sp) >= num_trans) ++ *dp-- = 0xff; ++ else ++ *dp-- = trans_alpha[*sp]; ++ *dp-- = palette[*sp].blue; ++ *dp-- = palette[*sp].green; ++ *dp-- = palette[*sp].red; ++ sp--; ++ } ++ row_info->bit_depth = 8; ++ row_info->pixel_depth = 32; ++ row_info->rowbytes = row_width * 4; ++ row_info->color_type = 6; ++ row_info->channels = 4; ++ } ++ ++ else ++ { ++ sp = row + (size_t)row_width - 1; ++ dp = row + (size_t)(row_width * 3) - 1; ++ i = 0; ++#ifdef PNG_ARM_NEON_INTRINSICS_AVAILABLE ++ i = png_do_expand_palette_rgb8_neon(png_ptr, row_info, row, ++ &sp, &dp); ++#else ++ PNG_UNUSED(png_ptr) ++#endif ++ ++ for (; i < row_width; i++) ++ { ++ *dp-- = palette[*sp].blue; ++ *dp-- = palette[*sp].green; ++ *dp-- = palette[*sp].red; ++ sp--; ++ } ++ ++ row_info->bit_depth = 8; ++ row_info->pixel_depth = 24; ++ row_info->rowbytes = row_width * 3; ++ row_info->color_type = 2; ++ row_info->channels = 3; ++ } ++ } ++ } ++ } ++} ++ ++/* If the bit depth < 8, it is expanded to 8. Also, if the already ++ * expanded transparency value is supplied, an alpha channel is built. ++ */ ++static void ++png_do_expand(png_row_infop row_info, png_bytep row, ++ png_const_color_16p trans_color) ++{ ++ int shift, value; ++ png_bytep sp, dp; ++ png_uint_32 i; ++ png_uint_32 row_width=row_info->width; ++ ++ png_debug(1, "in png_do_expand"); ++ ++ if (row_info->color_type == PNG_COLOR_TYPE_GRAY) ++ { ++ unsigned int gray = trans_color != NULL ? trans_color->gray : 0; ++ ++ if (row_info->bit_depth < 8) ++ { ++ switch (row_info->bit_depth) ++ { ++ case 1: ++ { ++ gray = (gray & 0x01) * 0xff; ++ sp = row + (size_t)((row_width - 1) >> 3); ++ dp = row + (size_t)row_width - 1; ++ shift = 7 - (int)((row_width + 7) & 0x07); ++ for (i = 0; i < row_width; i++) ++ { ++ if ((*sp >> shift) & 0x01) ++ *dp = 0xff; ++ ++ else ++ *dp = 0; ++ ++ if (shift == 7) ++ { ++ shift = 0; ++ sp--; ++ } ++ ++ else ++ shift++; ++ ++ dp--; ++ } ++ break; ++ } ++ ++ case 2: ++ { ++ gray = (gray & 0x03) * 0x55; ++ sp = row + (size_t)((row_width - 1) >> 2); ++ dp = row + (size_t)row_width - 1; ++ shift = (int)((3 - ((row_width + 3) & 0x03)) << 1); ++ for (i = 0; i < row_width; i++) ++ { ++ value = (*sp >> shift) & 0x03; ++ *dp = (png_byte)(value | (value << 2) | (value << 4) | ++ (value << 6)); ++ if (shift == 6) ++ { ++ shift = 0; ++ sp--; ++ } ++ ++ else ++ shift += 2; ++ ++ dp--; ++ } ++ break; ++ } ++ ++ case 4: ++ { ++ gray = (gray & 0x0f) * 0x11; ++ sp = row + (size_t)((row_width - 1) >> 1); ++ dp = row + (size_t)row_width - 1; ++ shift = (int)((1 - ((row_width + 1) & 0x01)) << 2); ++ for (i = 0; i < row_width; i++) ++ { ++ value = (*sp >> shift) & 0x0f; ++ *dp = (png_byte)(value | (value << 4)); ++ if (shift == 4) ++ { ++ shift = 0; ++ sp--; ++ } ++ ++ else ++ shift = 4; ++ ++ dp--; ++ } ++ break; ++ } ++ ++ default: ++ break; ++ } ++ ++ row_info->bit_depth = 8; ++ row_info->pixel_depth = 8; ++ row_info->rowbytes = row_width; ++ } ++ ++ if (trans_color != NULL) ++ { ++ if (row_info->bit_depth == 8) ++ { ++ gray = gray & 0xff; ++ sp = row + (size_t)row_width - 1; ++ dp = row + ((size_t)row_width << 1) - 1; ++ ++ for (i = 0; i < row_width; i++) ++ { ++ if ((*sp & 0xffU) == gray) ++ *dp-- = 0; ++ ++ else ++ *dp-- = 0xff; ++ ++ *dp-- = *sp--; ++ } ++ } ++ ++ else if (row_info->bit_depth == 16) ++ { ++ unsigned int gray_high = (gray >> 8) & 0xff; ++ unsigned int gray_low = gray & 0xff; ++ sp = row + row_info->rowbytes - 1; ++ dp = row + (row_info->rowbytes << 1) - 1; ++ for (i = 0; i < row_width; i++) ++ { ++ if ((*(sp - 1) & 0xffU) == gray_high && ++ (*(sp) & 0xffU) == gray_low) ++ { ++ *dp-- = 0; ++ *dp-- = 0; ++ } ++ ++ else ++ { ++ *dp-- = 0xff; ++ *dp-- = 0xff; ++ } ++ ++ *dp-- = *sp--; ++ *dp-- = *sp--; ++ } ++ } ++ ++ row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA; ++ row_info->channels = 2; ++ row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1); ++ row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, ++ row_width); ++ } ++ } ++ else if (row_info->color_type == PNG_COLOR_TYPE_RGB && ++ trans_color != NULL) ++ { ++ if (row_info->bit_depth == 8) ++ { ++ png_byte red = (png_byte)(trans_color->red & 0xff); ++ png_byte green = (png_byte)(trans_color->green & 0xff); ++ png_byte blue = (png_byte)(trans_color->blue & 0xff); ++ sp = row + (size_t)row_info->rowbytes - 1; ++ dp = row + ((size_t)row_width << 2) - 1; ++ for (i = 0; i < row_width; i++) ++ { ++ if (*(sp - 2) == red && *(sp - 1) == green && *(sp) == blue) ++ *dp-- = 0; ++ ++ else ++ *dp-- = 0xff; ++ ++ *dp-- = *sp--; ++ *dp-- = *sp--; ++ *dp-- = *sp--; ++ } ++ } ++ else if (row_info->bit_depth == 16) ++ { ++ png_byte red_high = (png_byte)((trans_color->red >> 8) & 0xff); ++ png_byte green_high = (png_byte)((trans_color->green >> 8) & 0xff); ++ png_byte blue_high = (png_byte)((trans_color->blue >> 8) & 0xff); ++ png_byte red_low = (png_byte)(trans_color->red & 0xff); ++ png_byte green_low = (png_byte)(trans_color->green & 0xff); ++ png_byte blue_low = (png_byte)(trans_color->blue & 0xff); ++ sp = row + row_info->rowbytes - 1; ++ dp = row + ((size_t)row_width << 3) - 1; ++ for (i = 0; i < row_width; i++) ++ { ++ if (*(sp - 5) == red_high && ++ *(sp - 4) == red_low && ++ *(sp - 3) == green_high && ++ *(sp - 2) == green_low && ++ *(sp - 1) == blue_high && ++ *(sp ) == blue_low) ++ { ++ *dp-- = 0; ++ *dp-- = 0; ++ } ++ ++ else ++ { ++ *dp-- = 0xff; ++ *dp-- = 0xff; ++ } ++ ++ *dp-- = *sp--; ++ *dp-- = *sp--; ++ *dp-- = *sp--; ++ *dp-- = *sp--; ++ *dp-- = *sp--; ++ *dp-- = *sp--; ++ } ++ } ++ row_info->color_type = PNG_COLOR_TYPE_RGB_ALPHA; ++ row_info->channels = 4; ++ row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2); ++ row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); ++ } ++} ++#endif ++ ++#ifdef PNG_READ_EXPAND_16_SUPPORTED ++/* If the bit depth is 8 and the color type is not a palette type expand the ++ * whole row to 16 bits. Has no effect otherwise. ++ */ ++static void ++png_do_expand_16(png_row_infop row_info, png_bytep row) ++{ ++ if (row_info->bit_depth == 8 && ++ row_info->color_type != PNG_COLOR_TYPE_PALETTE) ++ { ++ /* The row have a sequence of bytes containing [0..255] and we need ++ * to turn it into another row containing [0..65535], to do this we ++ * calculate: ++ * ++ * (input / 255) * 65535 ++ * ++ * Which happens to be exactly input * 257 and this can be achieved ++ * simply by byte replication in place (copying backwards). ++ */ ++ png_byte *sp = row + row_info->rowbytes; /* source, last byte + 1 */ ++ png_byte *dp = sp + row_info->rowbytes; /* destination, end + 1 */ ++ while (dp > sp) ++ { ++ dp[-2] = dp[-1] = *--sp; dp -= 2; ++ } ++ ++ row_info->rowbytes *= 2; ++ row_info->bit_depth = 16; ++ row_info->pixel_depth = (png_byte)(row_info->channels * 16); ++ } ++} ++#endif ++ ++#ifdef PNG_READ_QUANTIZE_SUPPORTED ++static void ++png_do_quantize(png_row_infop row_info, png_bytep row, ++ png_const_bytep palette_lookup, png_const_bytep quantize_lookup) ++{ ++ png_bytep sp, dp; ++ png_uint_32 i; ++ png_uint_32 row_width=row_info->width; ++ ++ png_debug(1, "in png_do_quantize"); ++ ++ if (row_info->bit_depth == 8) ++ { ++ if (row_info->color_type == PNG_COLOR_TYPE_RGB && palette_lookup) ++ { ++ int r, g, b, p; ++ sp = row; ++ dp = row; ++ for (i = 0; i < row_width; i++) ++ { ++ r = *sp++; ++ g = *sp++; ++ b = *sp++; ++ ++ /* This looks real messy, but the compiler will reduce ++ * it down to a reasonable formula. For example, with ++ * 5 bits per color, we get: ++ * p = (((r >> 3) & 0x1f) << 10) | ++ * (((g >> 3) & 0x1f) << 5) | ++ * ((b >> 3) & 0x1f); ++ */ ++ p = (((r >> (8 - PNG_QUANTIZE_RED_BITS)) & ++ ((1 << PNG_QUANTIZE_RED_BITS) - 1)) << ++ (PNG_QUANTIZE_GREEN_BITS + PNG_QUANTIZE_BLUE_BITS)) | ++ (((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) & ++ ((1 << PNG_QUANTIZE_GREEN_BITS) - 1)) << ++ (PNG_QUANTIZE_BLUE_BITS)) | ++ ((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) & ++ ((1 << PNG_QUANTIZE_BLUE_BITS) - 1)); ++ ++ *dp++ = palette_lookup[p]; ++ } ++ ++ row_info->color_type = PNG_COLOR_TYPE_PALETTE; ++ row_info->channels = 1; ++ row_info->pixel_depth = row_info->bit_depth; ++ row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); ++ } ++ ++ else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA && ++ palette_lookup != NULL) ++ { ++ int r, g, b, p; ++ sp = row; ++ dp = row; ++ for (i = 0; i < row_width; i++) ++ { ++ r = *sp++; ++ g = *sp++; ++ b = *sp++; ++ sp++; ++ ++ p = (((r >> (8 - PNG_QUANTIZE_RED_BITS)) & ++ ((1 << PNG_QUANTIZE_RED_BITS) - 1)) << ++ (PNG_QUANTIZE_GREEN_BITS + PNG_QUANTIZE_BLUE_BITS)) | ++ (((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) & ++ ((1 << PNG_QUANTIZE_GREEN_BITS) - 1)) << ++ (PNG_QUANTIZE_BLUE_BITS)) | ++ ((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) & ++ ((1 << PNG_QUANTIZE_BLUE_BITS) - 1)); ++ ++ *dp++ = palette_lookup[p]; ++ } ++ ++ row_info->color_type = PNG_COLOR_TYPE_PALETTE; ++ row_info->channels = 1; ++ row_info->pixel_depth = row_info->bit_depth; ++ row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width); ++ } ++ ++ else if (row_info->color_type == PNG_COLOR_TYPE_PALETTE && ++ quantize_lookup) ++ { ++ sp = row; ++ ++ for (i = 0; i < row_width; i++, sp++) ++ { ++ *sp = quantize_lookup[*sp]; ++ } ++ } ++ } ++} ++#endif /* READ_QUANTIZE */ ++ ++/* Transform the row. The order of transformations is significant, ++ * and is very touchy. If you add a transformation, take care to ++ * decide how it fits in with the other transformations here. ++ */ ++void /* PRIVATE */ ++png_do_read_transformations(png_structrp png_ptr, png_row_infop row_info) ++{ ++ png_debug(1, "in png_do_read_transformations"); ++ ++ if (png_ptr->row_buf == NULL) ++ { ++ /* Prior to 1.5.4 this output row/pass where the NULL pointer is, but this ++ * error is incredibly rare and incredibly easy to debug without this ++ * information. ++ */ ++ png_error(png_ptr, "NULL row buffer"); ++ } ++ ++ /* The following is debugging; prior to 1.5.4 the code was never compiled in; ++ * in 1.5.4 PNG_FLAG_DETECT_UNINITIALIZED was added and the macro ++ * PNG_WARN_UNINITIALIZED_ROW removed. In 1.6 the new flag is set only for ++ * all transformations, however in practice the ROW_INIT always gets done on ++ * demand, if necessary. ++ */ ++ if ((png_ptr->flags & PNG_FLAG_DETECT_UNINITIALIZED) != 0 && ++ (png_ptr->flags & PNG_FLAG_ROW_INIT) == 0) ++ { ++ /* Application has failed to call either png_read_start_image() or ++ * png_read_update_info() after setting transforms that expand pixels. ++ * This check added to libpng-1.2.19 (but not enabled until 1.5.4). ++ */ ++ png_error(png_ptr, "Uninitialized row"); ++ } ++ ++#ifdef PNG_READ_EXPAND_SUPPORTED ++ if ((png_ptr->transformations & PNG_EXPAND) != 0) ++ { ++ if (row_info->color_type == PNG_COLOR_TYPE_PALETTE) ++ { ++#ifdef PNG_ARM_NEON_INTRINSICS_AVAILABLE ++ if ((png_ptr->num_trans > 0) && (png_ptr->bit_depth == 8)) ++ { ++ if (png_ptr->riffled_palette == NULL) ++ { ++ /* Initialize the accelerated palette expansion. */ ++ png_ptr->riffled_palette = ++ (png_bytep)png_malloc(png_ptr, 256 * 4); ++ png_riffle_palette_neon(png_ptr); ++ } ++ } ++#endif ++ png_do_expand_palette(png_ptr, row_info, png_ptr->row_buf + 1, ++ png_ptr->palette, png_ptr->trans_alpha, png_ptr->num_trans); ++ } ++ ++ else ++ { ++ if (png_ptr->num_trans != 0 && ++ (png_ptr->transformations & PNG_EXPAND_tRNS) != 0) ++ png_do_expand(row_info, png_ptr->row_buf + 1, ++ &(png_ptr->trans_color)); ++ ++ else ++ png_do_expand(row_info, png_ptr->row_buf + 1, NULL); ++ } ++ } ++#endif ++ ++#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED ++ if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 && ++ (png_ptr->transformations & PNG_COMPOSE) == 0 && ++ (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA || ++ row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)) ++ png_do_strip_channel(row_info, png_ptr->row_buf + 1, ++ 0 /* at_start == false, because SWAP_ALPHA happens later */); ++#endif ++ ++#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED ++ if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0) ++ { ++ int rgb_error = ++ png_do_rgb_to_gray(png_ptr, row_info, ++ png_ptr->row_buf + 1); ++ ++ if (rgb_error != 0) ++ { ++ png_ptr->rgb_to_gray_status=1; ++ if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == ++ PNG_RGB_TO_GRAY_WARN) ++ png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel"); ++ ++ if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == ++ PNG_RGB_TO_GRAY_ERR) ++ png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel"); ++ } ++ } ++#endif ++ ++/* From Andreas Dilger e-mail to png-implement, 26 March 1998: ++ * ++ * In most cases, the "simple transparency" should be done prior to doing ++ * gray-to-RGB, or you will have to test 3x as many bytes to check if a ++ * pixel is transparent. You would also need to make sure that the ++ * transparency information is upgraded to RGB. ++ * ++ * To summarize, the current flow is: ++ * - Gray + simple transparency -> compare 1 or 2 gray bytes and composite ++ * with background "in place" if transparent, ++ * convert to RGB if necessary ++ * - Gray + alpha -> composite with gray background and remove alpha bytes, ++ * convert to RGB if necessary ++ * ++ * To support RGB backgrounds for gray images we need: ++ * - Gray + simple transparency -> convert to RGB + simple transparency, ++ * compare 3 or 6 bytes and composite with ++ * background "in place" if transparent ++ * (3x compare/pixel compared to doing ++ * composite with gray bkgrnd) ++ * - Gray + alpha -> convert to RGB + alpha, composite with background and ++ * remove alpha bytes (3x float ++ * operations/pixel compared with composite ++ * on gray background) ++ * ++ * Greg's change will do this. The reason it wasn't done before is for ++ * performance, as this increases the per-pixel operations. If we would check ++ * in advance if the background was gray or RGB, and position the gray-to-RGB ++ * transform appropriately, then it would save a lot of work/time. ++ */ ++ ++#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED ++ /* If gray -> RGB, do so now only if background is non-gray; else do later ++ * for performance reasons ++ */ ++ if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0 && ++ (png_ptr->mode & PNG_BACKGROUND_IS_GRAY) == 0) ++ png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1); ++#endif ++ ++#if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\ ++ defined(PNG_READ_ALPHA_MODE_SUPPORTED) ++ if ((png_ptr->transformations & PNG_COMPOSE) != 0) ++ png_do_compose(row_info, png_ptr->row_buf + 1, png_ptr); ++#endif ++ ++#ifdef PNG_READ_GAMMA_SUPPORTED ++ if ((png_ptr->transformations & PNG_GAMMA) != 0 && ++#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED ++ /* Because RGB_TO_GRAY does the gamma transform. */ ++ (png_ptr->transformations & PNG_RGB_TO_GRAY) == 0 && ++#endif ++#if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\ ++ defined(PNG_READ_ALPHA_MODE_SUPPORTED) ++ /* Because PNG_COMPOSE does the gamma transform if there is something to ++ * do (if there is an alpha channel or transparency.) ++ */ ++ !((png_ptr->transformations & PNG_COMPOSE) != 0 && ++ ((png_ptr->num_trans != 0) || ++ (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)) && ++#endif ++ /* Because png_init_read_transformations transforms the palette, unless ++ * RGB_TO_GRAY will do the transform. ++ */ ++ (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE)) ++ png_do_gamma(row_info, png_ptr->row_buf + 1, png_ptr); ++#endif ++ ++#ifdef PNG_READ_STRIP_ALPHA_SUPPORTED ++ if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 && ++ (png_ptr->transformations & PNG_COMPOSE) != 0 && ++ (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA || ++ row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)) ++ png_do_strip_channel(row_info, png_ptr->row_buf + 1, ++ 0 /* at_start == false, because SWAP_ALPHA happens later */); ++#endif ++ ++#ifdef PNG_READ_ALPHA_MODE_SUPPORTED ++ if ((png_ptr->transformations & PNG_ENCODE_ALPHA) != 0 && ++ (row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0) ++ png_do_encode_alpha(row_info, png_ptr->row_buf + 1, png_ptr); ++#endif ++ ++#ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED ++ if ((png_ptr->transformations & PNG_SCALE_16_TO_8) != 0) ++ png_do_scale_16_to_8(row_info, png_ptr->row_buf + 1); ++#endif ++ ++#ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED ++ /* There is no harm in doing both of these because only one has any effect, ++ * by putting the 'scale' option first if the app asks for scale (either by ++ * calling the API or in a TRANSFORM flag) this is what happens. ++ */ ++ if ((png_ptr->transformations & PNG_16_TO_8) != 0) ++ png_do_chop(row_info, png_ptr->row_buf + 1); ++#endif ++ ++#ifdef PNG_READ_QUANTIZE_SUPPORTED ++ if ((png_ptr->transformations & PNG_QUANTIZE) != 0) ++ { ++ png_do_quantize(row_info, png_ptr->row_buf + 1, ++ png_ptr->palette_lookup, png_ptr->quantize_index); ++ ++ if (row_info->rowbytes == 0) ++ png_error(png_ptr, "png_do_quantize returned rowbytes=0"); ++ } ++#endif /* READ_QUANTIZE */ ++ ++#ifdef PNG_READ_EXPAND_16_SUPPORTED ++ /* Do the expansion now, after all the arithmetic has been done. Notice ++ * that previous transformations can handle the PNG_EXPAND_16 flag if this ++ * is efficient (particularly true in the case of gamma correction, where ++ * better accuracy results faster!) ++ */ ++ if ((png_ptr->transformations & PNG_EXPAND_16) != 0) ++ png_do_expand_16(row_info, png_ptr->row_buf + 1); ++#endif ++ ++#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED ++ /* NOTE: moved here in 1.5.4 (from much later in this list.) */ ++ if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0 && ++ (png_ptr->mode & PNG_BACKGROUND_IS_GRAY) != 0) ++ png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1); ++#endif ++ ++#ifdef PNG_READ_INVERT_SUPPORTED ++ if ((png_ptr->transformations & PNG_INVERT_MONO) != 0) ++ png_do_invert(row_info, png_ptr->row_buf + 1); ++#endif ++ ++#ifdef PNG_READ_INVERT_ALPHA_SUPPORTED ++ if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0) ++ png_do_read_invert_alpha(row_info, png_ptr->row_buf + 1); ++#endif ++ ++#ifdef PNG_READ_SHIFT_SUPPORTED ++ if ((png_ptr->transformations & PNG_SHIFT) != 0) ++ png_do_unshift(row_info, png_ptr->row_buf + 1, ++ &(png_ptr->shift)); ++#endif ++ ++#ifdef PNG_READ_PACK_SUPPORTED ++ if ((png_ptr->transformations & PNG_PACK) != 0) ++ png_do_unpack(row_info, png_ptr->row_buf + 1); ++#endif ++ ++#ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED ++ /* Added at libpng-1.5.10 */ ++ if (row_info->color_type == PNG_COLOR_TYPE_PALETTE && ++ png_ptr->num_palette_max >= 0) ++ png_do_check_palette_indexes(png_ptr, row_info); ++#endif ++ ++#ifdef PNG_READ_BGR_SUPPORTED ++ if ((png_ptr->transformations & PNG_BGR) != 0) ++ png_do_bgr(row_info, png_ptr->row_buf + 1); ++#endif ++ ++#ifdef PNG_READ_PACKSWAP_SUPPORTED ++ if ((png_ptr->transformations & PNG_PACKSWAP) != 0) ++ png_do_packswap(row_info, png_ptr->row_buf + 1); ++#endif ++ ++#ifdef PNG_READ_FILLER_SUPPORTED ++ if ((png_ptr->transformations & PNG_FILLER) != 0) ++ png_do_read_filler(row_info, png_ptr->row_buf + 1, ++ (png_uint_32)png_ptr->filler, png_ptr->flags); ++#endif ++ ++#ifdef PNG_READ_SWAP_ALPHA_SUPPORTED ++ if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0) ++ png_do_read_swap_alpha(row_info, png_ptr->row_buf + 1); ++#endif ++ ++#ifdef PNG_READ_16BIT_SUPPORTED ++#ifdef PNG_READ_SWAP_SUPPORTED ++ if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0) ++ png_do_swap(row_info, png_ptr->row_buf + 1); ++#endif ++#endif ++ ++#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED ++ if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0) ++ { ++ if (png_ptr->read_user_transform_fn != NULL) ++ (*(png_ptr->read_user_transform_fn)) /* User read transform function */ ++ (png_ptr, /* png_ptr */ ++ row_info, /* row_info: */ ++ /* png_uint_32 width; width of row */ ++ /* size_t rowbytes; number of bytes in row */ ++ /* png_byte color_type; color type of pixels */ ++ /* png_byte bit_depth; bit depth of samples */ ++ /* png_byte channels; number of channels (1-4) */ ++ /* png_byte pixel_depth; bits per pixel (depth*channels) */ ++ png_ptr->row_buf + 1); /* start of pixel data for row */ ++#ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED ++ if (png_ptr->user_transform_depth != 0) ++ row_info->bit_depth = png_ptr->user_transform_depth; ++ ++ if (png_ptr->user_transform_channels != 0) ++ row_info->channels = png_ptr->user_transform_channels; ++#endif ++ row_info->pixel_depth = (png_byte)(row_info->bit_depth * ++ row_info->channels); ++ ++ row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_info->width); ++ } ++#endif ++} ++ ++#endif /* READ_TRANSFORMS */ ++#endif /* READ */ +diff --git a/lib/libpng/pngrutil.c b/lib/libpng/pngrutil.c +new file mode 100644 +index 000000000..d5fa08c39 +--- /dev/null ++++ b/lib/libpng/pngrutil.c +@@ -0,0 +1,4681 @@ ++ ++/* pngrutil.c - utilities to read a PNG file ++ * ++ * Copyright (c) 2018 Cosmin Truta ++ * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson ++ * Copyright (c) 1996-1997 Andreas Dilger ++ * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc. ++ * ++ * This code is released under the libpng license. ++ * For conditions of distribution and use, see the disclaimer ++ * and license in png.h ++ * ++ * This file contains routines that are only called from within ++ * libpng itself during the course of reading an image. ++ */ ++ ++#include "pngpriv.h" ++ ++#ifdef PNG_READ_SUPPORTED ++ ++png_uint_32 PNGAPI ++png_get_uint_31(png_const_structrp png_ptr, png_const_bytep buf) ++{ ++ png_uint_32 uval = png_get_uint_32(buf); ++ ++ if (uval > PNG_UINT_31_MAX) ++ png_error(png_ptr, "PNG unsigned integer out of range"); ++ ++ return (uval); ++} ++ ++#if defined(PNG_READ_gAMA_SUPPORTED) || defined(PNG_READ_cHRM_SUPPORTED) ++/* The following is a variation on the above for use with the fixed ++ * point values used for gAMA and cHRM. Instead of png_error it ++ * issues a warning and returns (-1) - an invalid value because both ++ * gAMA and cHRM use *unsigned* integers for fixed point values. ++ */ ++#define PNG_FIXED_ERROR (-1) ++ ++static png_fixed_point /* PRIVATE */ ++png_get_fixed_point(png_structrp png_ptr, png_const_bytep buf) ++{ ++ png_uint_32 uval = png_get_uint_32(buf); ++ ++ if (uval <= PNG_UINT_31_MAX) ++ return (png_fixed_point)uval; /* known to be in range */ ++ ++ /* The caller can turn off the warning by passing NULL. */ ++ if (png_ptr != NULL) ++ png_warning(png_ptr, "PNG fixed point integer out of range"); ++ ++ return PNG_FIXED_ERROR; ++} ++#endif ++ ++#ifdef PNG_READ_INT_FUNCTIONS_SUPPORTED ++/* NOTE: the read macros will obscure these definitions, so that if ++ * PNG_USE_READ_MACROS is set the library will not use them internally, ++ * but the APIs will still be available externally. ++ * ++ * The parentheses around "PNGAPI function_name" in the following three ++ * functions are necessary because they allow the macros to co-exist with ++ * these (unused but exported) functions. ++ */ ++ ++/* Grab an unsigned 32-bit integer from a buffer in big-endian format. */ ++png_uint_32 (PNGAPI ++png_get_uint_32)(png_const_bytep buf) ++{ ++ png_uint_32 uval = ++ ((png_uint_32)(*(buf )) << 24) + ++ ((png_uint_32)(*(buf + 1)) << 16) + ++ ((png_uint_32)(*(buf + 2)) << 8) + ++ ((png_uint_32)(*(buf + 3)) ) ; ++ ++ return uval; ++} ++ ++/* Grab a signed 32-bit integer from a buffer in big-endian format. The ++ * data is stored in the PNG file in two's complement format and there ++ * is no guarantee that a 'png_int_32' is exactly 32 bits, therefore ++ * the following code does a two's complement to native conversion. ++ */ ++png_int_32 (PNGAPI ++png_get_int_32)(png_const_bytep buf) ++{ ++ png_uint_32 uval = png_get_uint_32(buf); ++ if ((uval & 0x80000000) == 0) /* non-negative */ ++ return (png_int_32)uval; ++ ++ uval = (uval ^ 0xffffffff) + 1; /* 2's complement: -x = ~x+1 */ ++ if ((uval & 0x80000000) == 0) /* no overflow */ ++ return -(png_int_32)uval; ++ /* The following has to be safe; this function only gets called on PNG data ++ * and if we get here that data is invalid. 0 is the most safe value and ++ * if not then an attacker would surely just generate a PNG with 0 instead. ++ */ ++ return 0; ++} ++ ++/* Grab an unsigned 16-bit integer from a buffer in big-endian format. */ ++png_uint_16 (PNGAPI ++png_get_uint_16)(png_const_bytep buf) ++{ ++ /* ANSI-C requires an int value to accommodate at least 16 bits so this ++ * works and allows the compiler not to worry about possible narrowing ++ * on 32-bit systems. (Pre-ANSI systems did not make integers smaller ++ * than 16 bits either.) ++ */ ++ unsigned int val = ++ ((unsigned int)(*buf) << 8) + ++ ((unsigned int)(*(buf + 1))); ++ ++ return (png_uint_16)val; ++} ++ ++#endif /* READ_INT_FUNCTIONS */ ++ ++/* Read and check the PNG file signature */ ++void /* PRIVATE */ ++png_read_sig(png_structrp png_ptr, png_inforp info_ptr) ++{ ++ size_t num_checked, num_to_check; ++ ++ /* Exit if the user application does not expect a signature. */ ++ if (png_ptr->sig_bytes >= 8) ++ return; ++ ++ num_checked = png_ptr->sig_bytes; ++ num_to_check = 8 - num_checked; ++ ++#ifdef PNG_IO_STATE_SUPPORTED ++ png_ptr->io_state = PNG_IO_READING | PNG_IO_SIGNATURE; ++#endif ++ ++ /* The signature must be serialized in a single I/O call. */ ++ png_read_data(png_ptr, &(info_ptr->signature[num_checked]), num_to_check); ++ png_ptr->sig_bytes = 8; ++ ++ if (png_sig_cmp(info_ptr->signature, num_checked, num_to_check) != 0) ++ { ++ if (num_checked < 4 && ++ png_sig_cmp(info_ptr->signature, num_checked, num_to_check - 4)) ++ png_error(png_ptr, "Not a PNG file"); ++ else ++ png_error(png_ptr, "PNG file corrupted by ASCII conversion"); ++ } ++ if (num_checked < 3) ++ png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE; ++} ++ ++/* Read the chunk header (length + type name). ++ * Put the type name into png_ptr->chunk_name, and return the length. ++ */ ++png_uint_32 /* PRIVATE */ ++png_read_chunk_header(png_structrp png_ptr) ++{ ++ png_byte buf[8]; ++ png_uint_32 length; ++ ++#ifdef PNG_IO_STATE_SUPPORTED ++ png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_HDR; ++#endif ++ ++ /* Read the length and the chunk name. ++ * This must be performed in a single I/O call. ++ */ ++ png_read_data(png_ptr, buf, 8); ++ length = png_get_uint_31(png_ptr, buf); ++ ++ /* Put the chunk name into png_ptr->chunk_name. */ ++ png_ptr->chunk_name = PNG_CHUNK_FROM_STRING(buf+4); ++ ++ png_debug2(0, "Reading %lx chunk, length = %lu", ++ (unsigned long)png_ptr->chunk_name, (unsigned long)length); ++ ++ /* Reset the crc and run it over the chunk name. */ ++ png_reset_crc(png_ptr); ++ png_calculate_crc(png_ptr, buf + 4, 4); ++ ++ /* Check to see if chunk name is valid. */ ++ png_check_chunk_name(png_ptr, png_ptr->chunk_name); ++ ++ /* Check for too-large chunk length */ ++ png_check_chunk_length(png_ptr, length); ++ ++#ifdef PNG_IO_STATE_SUPPORTED ++ png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_DATA; ++#endif ++ ++ return length; ++} ++ ++/* Read data, and (optionally) run it through the CRC. */ ++void /* PRIVATE */ ++png_crc_read(png_structrp png_ptr, png_bytep buf, png_uint_32 length) ++{ ++ if (png_ptr == NULL) ++ return; ++ ++ png_read_data(png_ptr, buf, length); ++ png_calculate_crc(png_ptr, buf, length); ++} ++ ++/* Optionally skip data and then check the CRC. Depending on whether we ++ * are reading an ancillary or critical chunk, and how the program has set ++ * things up, we may calculate the CRC on the data and print a message. ++ * Returns '1' if there was a CRC error, '0' otherwise. ++ */ ++int /* PRIVATE */ ++png_crc_finish(png_structrp png_ptr, png_uint_32 skip) ++{ ++ /* The size of the local buffer for inflate is a good guess as to a ++ * reasonable size to use for buffering reads from the application. ++ */ ++ while (skip > 0) ++ { ++ png_uint_32 len; ++ png_byte tmpbuf[PNG_INFLATE_BUF_SIZE]; ++ ++ len = (sizeof tmpbuf); ++ if (len > skip) ++ len = skip; ++ skip -= len; ++ ++ png_crc_read(png_ptr, tmpbuf, len); ++ } ++ ++ if (png_crc_error(png_ptr) != 0) ++ { ++ if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) != 0 ? ++ (png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) == 0 : ++ (png_ptr->flags & PNG_FLAG_CRC_CRITICAL_USE) != 0) ++ { ++ png_chunk_warning(png_ptr, "CRC error"); ++ } ++ ++ else ++ png_chunk_error(png_ptr, "CRC error"); ++ ++ return (1); ++ } ++ ++ return (0); ++} ++ ++/* Compare the CRC stored in the PNG file with that calculated by libpng from ++ * the data it has read thus far. ++ */ ++int /* PRIVATE */ ++png_crc_error(png_structrp png_ptr) ++{ ++ png_byte crc_bytes[4]; ++ png_uint_32 crc; ++ int need_crc = 1; ++ ++ if (PNG_CHUNK_ANCILLARY(png_ptr->chunk_name) != 0) ++ { ++ if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_MASK) == ++ (PNG_FLAG_CRC_ANCILLARY_USE | PNG_FLAG_CRC_ANCILLARY_NOWARN)) ++ need_crc = 0; ++ } ++ ++ else /* critical */ ++ { ++ if ((png_ptr->flags & PNG_FLAG_CRC_CRITICAL_IGNORE) != 0) ++ need_crc = 0; ++ } ++ ++#ifdef PNG_IO_STATE_SUPPORTED ++ png_ptr->io_state = PNG_IO_READING | PNG_IO_CHUNK_CRC; ++#endif ++ ++ /* The chunk CRC must be serialized in a single I/O call. */ ++ png_read_data(png_ptr, crc_bytes, 4); ++ ++ if (need_crc != 0) ++ { ++ crc = png_get_uint_32(crc_bytes); ++ return ((int)(crc != png_ptr->crc)); ++ } ++ ++ else ++ return (0); ++} ++ ++#if defined(PNG_READ_iCCP_SUPPORTED) || defined(PNG_READ_iTXt_SUPPORTED) ||\ ++ defined(PNG_READ_pCAL_SUPPORTED) || defined(PNG_READ_sCAL_SUPPORTED) ||\ ++ defined(PNG_READ_sPLT_SUPPORTED) || defined(PNG_READ_tEXt_SUPPORTED) ||\ ++ defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_SEQUENTIAL_READ_SUPPORTED) ++/* Manage the read buffer; this simply reallocates the buffer if it is not small ++ * enough (or if it is not allocated). The routine returns a pointer to the ++ * buffer; if an error occurs and 'warn' is set the routine returns NULL, else ++ * it will call png_error (via png_malloc) on failure. (warn == 2 means ++ * 'silent'). ++ */ ++static png_bytep ++png_read_buffer(png_structrp png_ptr, png_alloc_size_t new_size, int warn) ++{ ++ png_bytep buffer = png_ptr->read_buffer; ++ ++ if (buffer != NULL && new_size > png_ptr->read_buffer_size) ++ { ++ png_ptr->read_buffer = NULL; ++ png_ptr->read_buffer = NULL; ++ png_ptr->read_buffer_size = 0; ++ png_free(png_ptr, buffer); ++ buffer = NULL; ++ } ++ ++ if (buffer == NULL) ++ { ++ buffer = png_voidcast(png_bytep, png_malloc_base(png_ptr, new_size)); ++ ++ if (buffer != NULL) ++ { ++ memset(buffer, 0, new_size); /* just in case */ ++ png_ptr->read_buffer = buffer; ++ png_ptr->read_buffer_size = new_size; ++ } ++ ++ else if (warn < 2) /* else silent */ ++ { ++ if (warn != 0) ++ png_chunk_warning(png_ptr, "insufficient memory to read chunk"); ++ ++ else ++ png_chunk_error(png_ptr, "insufficient memory to read chunk"); ++ } ++ } ++ ++ return buffer; ++} ++#endif /* READ_iCCP|iTXt|pCAL|sCAL|sPLT|tEXt|zTXt|SEQUENTIAL_READ */ ++ ++/* png_inflate_claim: claim the zstream for some nefarious purpose that involves ++ * decompression. Returns Z_OK on success, else a zlib error code. It checks ++ * the owner but, in final release builds, just issues a warning if some other ++ * chunk apparently owns the stream. Prior to release it does a png_error. ++ */ ++static int ++png_inflate_claim(png_structrp png_ptr, png_uint_32 owner) ++{ ++ if (png_ptr->zowner != 0) ++ { ++ char msg[64]; ++ ++ PNG_STRING_FROM_CHUNK(msg, png_ptr->zowner); ++ /* So the message that results is " using zstream"; this is an ++ * internal error, but is very useful for debugging. i18n requirements ++ * are minimal. ++ */ ++ (void)png_safecat(msg, (sizeof msg), 4, " using zstream"); ++#if PNG_RELEASE_BUILD ++ png_chunk_warning(png_ptr, msg); ++ png_ptr->zowner = 0; ++#else ++ png_chunk_error(png_ptr, msg); ++#endif ++ } ++ ++ /* Implementation note: unlike 'png_deflate_claim' this internal function ++ * does not take the size of the data as an argument. Some efficiency could ++ * be gained by using this when it is known *if* the zlib stream itself does ++ * not record the number; however, this is an illusion: the original writer ++ * of the PNG may have selected a lower window size, and we really must ++ * follow that because, for systems with with limited capabilities, we ++ * would otherwise reject the application's attempts to use a smaller window ++ * size (zlib doesn't have an interface to say "this or lower"!). ++ * ++ * inflateReset2 was added to zlib 1.2.4; before this the window could not be ++ * reset, therefore it is necessary to always allocate the maximum window ++ * size with earlier zlibs just in case later compressed chunks need it. ++ */ ++ { ++ int ret; /* zlib return code */ ++#if ZLIB_VERNUM >= 0x1240 ++ int window_bits = 0; ++ ++# if defined(PNG_SET_OPTION_SUPPORTED) && defined(PNG_MAXIMUM_INFLATE_WINDOW) ++ if (((png_ptr->options >> PNG_MAXIMUM_INFLATE_WINDOW) & 3) == ++ PNG_OPTION_ON) ++ { ++ window_bits = 15; ++ png_ptr->zstream_start = 0; /* fixed window size */ ++ } ++ ++ else ++ { ++ png_ptr->zstream_start = 1; ++ } ++# endif ++ ++#endif /* ZLIB_VERNUM >= 0x1240 */ ++ ++ /* Set this for safety, just in case the previous owner left pointers to ++ * memory allocations. ++ */ ++ png_ptr->zstream.next_in = NULL; ++ png_ptr->zstream.avail_in = 0; ++ png_ptr->zstream.next_out = NULL; ++ png_ptr->zstream.avail_out = 0; ++ ++ if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0) ++ { ++#if ZLIB_VERNUM >= 0x1240 ++ ret = inflateReset2(&png_ptr->zstream, window_bits); ++#else ++ ret = inflateReset(&png_ptr->zstream); ++#endif ++ } ++ ++ else ++ { ++#if ZLIB_VERNUM >= 0x1240 ++ ret = inflateInit2(&png_ptr->zstream, window_bits); ++#else ++ ret = inflateInit(&png_ptr->zstream); ++#endif ++ ++ if (ret == Z_OK) ++ png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED; ++ } ++ ++#if ZLIB_VERNUM >= 0x1290 && \ ++ defined(PNG_SET_OPTION_SUPPORTED) && defined(PNG_IGNORE_ADLER32) ++ if (((png_ptr->options >> PNG_IGNORE_ADLER32) & 3) == PNG_OPTION_ON) ++ /* Turn off validation of the ADLER32 checksum in IDAT chunks */ ++ ret = inflateValidate(&png_ptr->zstream, 0); ++#endif ++ ++ if (ret == Z_OK) ++ png_ptr->zowner = owner; ++ ++ else ++ png_zstream_error(png_ptr, ret); ++ ++ return ret; ++ } ++ ++#ifdef window_bits ++# undef window_bits ++#endif ++} ++ ++#if ZLIB_VERNUM >= 0x1240 ++/* Handle the start of the inflate stream if we called inflateInit2(strm,0); ++ * in this case some zlib versions skip validation of the CINFO field and, in ++ * certain circumstances, libpng may end up displaying an invalid image, in ++ * contrast to implementations that call zlib in the normal way (e.g. libpng ++ * 1.5). ++ */ ++int /* PRIVATE */ ++png_zlib_inflate(png_structrp png_ptr, int flush) ++{ ++ if (png_ptr->zstream_start && png_ptr->zstream.avail_in > 0) ++ { ++ if ((*png_ptr->zstream.next_in >> 4) > 7) ++ { ++ png_ptr->zstream.msg = "invalid window size (libpng)"; ++ return Z_DATA_ERROR; ++ } ++ ++ png_ptr->zstream_start = 0; ++ } ++ ++ return inflate(&png_ptr->zstream, flush); ++} ++#endif /* Zlib >= 1.2.4 */ ++ ++#ifdef PNG_READ_COMPRESSED_TEXT_SUPPORTED ++#if defined(PNG_READ_zTXt_SUPPORTED) || defined (PNG_READ_iTXt_SUPPORTED) ++/* png_inflate now returns zlib error codes including Z_OK and Z_STREAM_END to ++ * allow the caller to do multiple calls if required. If the 'finish' flag is ++ * set Z_FINISH will be passed to the final inflate() call and Z_STREAM_END must ++ * be returned or there has been a problem, otherwise Z_SYNC_FLUSH is used and ++ * Z_OK or Z_STREAM_END will be returned on success. ++ * ++ * The input and output sizes are updated to the actual amounts of data consumed ++ * or written, not the amount available (as in a z_stream). The data pointers ++ * are not changed, so the next input is (data+input_size) and the next ++ * available output is (output+output_size). ++ */ ++static int ++png_inflate(png_structrp png_ptr, png_uint_32 owner, int finish, ++ /* INPUT: */ png_const_bytep input, png_uint_32p input_size_ptr, ++ /* OUTPUT: */ png_bytep output, png_alloc_size_t *output_size_ptr) ++{ ++ if (png_ptr->zowner == owner) /* Else not claimed */ ++ { ++ int ret; ++ png_alloc_size_t avail_out = *output_size_ptr; ++ png_uint_32 avail_in = *input_size_ptr; ++ ++ /* zlib can't necessarily handle more than 65535 bytes at once (i.e. it ++ * can't even necessarily handle 65536 bytes) because the type uInt is ++ * "16 bits or more". Consequently it is necessary to chunk the input to ++ * zlib. This code uses ZLIB_IO_MAX, from pngpriv.h, as the maximum (the ++ * maximum value that can be stored in a uInt.) It is possible to set ++ * ZLIB_IO_MAX to a lower value in pngpriv.h and this may sometimes have ++ * a performance advantage, because it reduces the amount of data accessed ++ * at each step and that may give the OS more time to page it in. ++ */ ++ png_ptr->zstream.next_in = PNGZ_INPUT_CAST(input); ++ /* avail_in and avail_out are set below from 'size' */ ++ png_ptr->zstream.avail_in = 0; ++ png_ptr->zstream.avail_out = 0; ++ ++ /* Read directly into the output if it is available (this is set to ++ * a local buffer below if output is NULL). ++ */ ++ if (output != NULL) ++ png_ptr->zstream.next_out = output; ++ ++ do ++ { ++ uInt avail; ++ Byte local_buffer[PNG_INFLATE_BUF_SIZE]; ++ ++ /* zlib INPUT BUFFER */ ++ /* The setting of 'avail_in' used to be outside the loop; by setting it ++ * inside it is possible to chunk the input to zlib and simply rely on ++ * zlib to advance the 'next_in' pointer. This allows arbitrary ++ * amounts of data to be passed through zlib at the unavoidable cost of ++ * requiring a window save (memcpy of up to 32768 output bytes) ++ * every ZLIB_IO_MAX input bytes. ++ */ ++ avail_in += png_ptr->zstream.avail_in; /* not consumed last time */ ++ ++ avail = ZLIB_IO_MAX; ++ ++ if (avail_in < avail) ++ avail = (uInt)avail_in; /* safe: < than ZLIB_IO_MAX */ ++ ++ avail_in -= avail; ++ png_ptr->zstream.avail_in = avail; ++ ++ /* zlib OUTPUT BUFFER */ ++ avail_out += png_ptr->zstream.avail_out; /* not written last time */ ++ ++ avail = ZLIB_IO_MAX; /* maximum zlib can process */ ++ ++ if (output == NULL) ++ { ++ /* Reset the output buffer each time round if output is NULL and ++ * make available the full buffer, up to 'remaining_space' ++ */ ++ png_ptr->zstream.next_out = local_buffer; ++ if ((sizeof local_buffer) < avail) ++ avail = (sizeof local_buffer); ++ } ++ ++ if (avail_out < avail) ++ avail = (uInt)avail_out; /* safe: < ZLIB_IO_MAX */ ++ ++ png_ptr->zstream.avail_out = avail; ++ avail_out -= avail; ++ ++ /* zlib inflate call */ ++ /* In fact 'avail_out' may be 0 at this point, that happens at the end ++ * of the read when the final LZ end code was not passed at the end of ++ * the previous chunk of input data. Tell zlib if we have reached the ++ * end of the output buffer. ++ */ ++ ret = PNG_INFLATE(png_ptr, avail_out > 0 ? Z_NO_FLUSH : ++ (finish ? Z_FINISH : Z_SYNC_FLUSH)); ++ } while (ret == Z_OK); ++ ++ /* For safety kill the local buffer pointer now */ ++ if (output == NULL) ++ png_ptr->zstream.next_out = NULL; ++ ++ /* Claw back the 'size' and 'remaining_space' byte counts. */ ++ avail_in += png_ptr->zstream.avail_in; ++ avail_out += png_ptr->zstream.avail_out; ++ ++ /* Update the input and output sizes; the updated values are the amount ++ * consumed or written, effectively the inverse of what zlib uses. ++ */ ++ if (avail_out > 0) ++ *output_size_ptr -= avail_out; ++ ++ if (avail_in > 0) ++ *input_size_ptr -= avail_in; ++ ++ /* Ensure png_ptr->zstream.msg is set (even in the success case!) */ ++ png_zstream_error(png_ptr, ret); ++ return ret; ++ } ++ ++ else ++ { ++ /* This is a bad internal error. The recovery assigns to the zstream msg ++ * pointer, which is not owned by the caller, but this is safe; it's only ++ * used on errors! ++ */ ++ png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed"); ++ return Z_STREAM_ERROR; ++ } ++} ++ ++/* ++ * Decompress trailing data in a chunk. The assumption is that read_buffer ++ * points at an allocated area holding the contents of a chunk with a ++ * trailing compressed part. What we get back is an allocated area ++ * holding the original prefix part and an uncompressed version of the ++ * trailing part (the malloc area passed in is freed). ++ */ ++static int ++png_decompress_chunk(png_structrp png_ptr, ++ png_uint_32 chunklength, png_uint_32 prefix_size, ++ png_alloc_size_t *newlength /* must be initialized to the maximum! */, ++ int terminate /*add a '\0' to the end of the uncompressed data*/) ++{ ++ /* TODO: implement different limits for different types of chunk. ++ * ++ * The caller supplies *newlength set to the maximum length of the ++ * uncompressed data, but this routine allocates space for the prefix and ++ * maybe a '\0' terminator too. We have to assume that 'prefix_size' is ++ * limited only by the maximum chunk size. ++ */ ++ png_alloc_size_t limit = PNG_SIZE_MAX; ++ ++# ifdef PNG_SET_USER_LIMITS_SUPPORTED ++ if (png_ptr->user_chunk_malloc_max > 0 && ++ png_ptr->user_chunk_malloc_max < limit) ++ limit = png_ptr->user_chunk_malloc_max; ++# elif PNG_USER_CHUNK_MALLOC_MAX > 0 ++ if (PNG_USER_CHUNK_MALLOC_MAX < limit) ++ limit = PNG_USER_CHUNK_MALLOC_MAX; ++# endif ++ ++ if (limit >= prefix_size + (terminate != 0)) ++ { ++ int ret; ++ ++ limit -= prefix_size + (terminate != 0); ++ ++ if (limit < *newlength) ++ *newlength = limit; ++ ++ /* Now try to claim the stream. */ ++ ret = png_inflate_claim(png_ptr, png_ptr->chunk_name); ++ ++ if (ret == Z_OK) ++ { ++ png_uint_32 lzsize = chunklength - prefix_size; ++ ++ ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/, ++ /* input: */ png_ptr->read_buffer + prefix_size, &lzsize, ++ /* output: */ NULL, newlength); ++ ++ if (ret == Z_STREAM_END) ++ { ++ /* Use 'inflateReset' here, not 'inflateReset2' because this ++ * preserves the previously decided window size (otherwise it would ++ * be necessary to store the previous window size.) In practice ++ * this doesn't matter anyway, because png_inflate will call inflate ++ * with Z_FINISH in almost all cases, so the window will not be ++ * maintained. ++ */ ++ if (inflateReset(&png_ptr->zstream) == Z_OK) ++ { ++ /* Because of the limit checks above we know that the new, ++ * expanded, size will fit in a size_t (let alone an ++ * png_alloc_size_t). Use png_malloc_base here to avoid an ++ * extra OOM message. ++ */ ++ png_alloc_size_t new_size = *newlength; ++ png_alloc_size_t buffer_size = prefix_size + new_size + ++ (terminate != 0); ++ png_bytep text = png_voidcast(png_bytep, png_malloc_base(png_ptr, ++ buffer_size)); ++ ++ if (text != NULL) ++ { ++ memset(text, 0, buffer_size); ++ ++ ret = png_inflate(png_ptr, png_ptr->chunk_name, 1/*finish*/, ++ png_ptr->read_buffer + prefix_size, &lzsize, ++ text + prefix_size, newlength); ++ ++ if (ret == Z_STREAM_END) ++ { ++ if (new_size == *newlength) ++ { ++ if (terminate != 0) ++ text[prefix_size + *newlength] = 0; ++ ++ if (prefix_size > 0) ++ memcpy(text, png_ptr->read_buffer, prefix_size); ++ ++ { ++ png_bytep old_ptr = png_ptr->read_buffer; ++ ++ png_ptr->read_buffer = text; ++ png_ptr->read_buffer_size = buffer_size; ++ text = old_ptr; /* freed below */ ++ } ++ } ++ ++ else ++ { ++ /* The size changed on the second read, there can be no ++ * guarantee that anything is correct at this point. ++ * The 'msg' pointer has been set to "unexpected end of ++ * LZ stream", which is fine, but return an error code ++ * that the caller won't accept. ++ */ ++ ret = PNG_UNEXPECTED_ZLIB_RETURN; ++ } ++ } ++ ++ else if (ret == Z_OK) ++ ret = PNG_UNEXPECTED_ZLIB_RETURN; /* for safety */ ++ ++ /* Free the text pointer (this is the old read_buffer on ++ * success) ++ */ ++ png_free(png_ptr, text); ++ ++ /* This really is very benign, but it's still an error because ++ * the extra space may otherwise be used as a Trojan Horse. ++ */ ++ if (ret == Z_STREAM_END && ++ chunklength - prefix_size != lzsize) ++ png_chunk_benign_error(png_ptr, "extra compressed data"); ++ } ++ ++ else ++ { ++ /* Out of memory allocating the buffer */ ++ ret = Z_MEM_ERROR; ++ png_zstream_error(png_ptr, Z_MEM_ERROR); ++ } ++ } ++ ++ else ++ { ++ /* inflateReset failed, store the error message */ ++ png_zstream_error(png_ptr, ret); ++ ret = PNG_UNEXPECTED_ZLIB_RETURN; ++ } ++ } ++ ++ else if (ret == Z_OK) ++ ret = PNG_UNEXPECTED_ZLIB_RETURN; ++ ++ /* Release the claimed stream */ ++ png_ptr->zowner = 0; ++ } ++ ++ else /* the claim failed */ if (ret == Z_STREAM_END) /* impossible! */ ++ ret = PNG_UNEXPECTED_ZLIB_RETURN; ++ ++ return ret; ++ } ++ ++ else ++ { ++ /* Application/configuration limits exceeded */ ++ png_zstream_error(png_ptr, Z_MEM_ERROR); ++ return Z_MEM_ERROR; ++ } ++} ++#endif /* READ_zTXt || READ_iTXt */ ++#endif /* READ_COMPRESSED_TEXT */ ++ ++#ifdef PNG_READ_iCCP_SUPPORTED ++/* Perform a partial read and decompress, producing 'avail_out' bytes and ++ * reading from the current chunk as required. ++ */ ++static int ++png_inflate_read(png_structrp png_ptr, png_bytep read_buffer, uInt read_size, ++ png_uint_32p chunk_bytes, png_bytep next_out, png_alloc_size_t *out_size, ++ int finish) ++{ ++ if (png_ptr->zowner == png_ptr->chunk_name) ++ { ++ int ret; ++ ++ /* next_in and avail_in must have been initialized by the caller. */ ++ png_ptr->zstream.next_out = next_out; ++ png_ptr->zstream.avail_out = 0; /* set in the loop */ ++ ++ do ++ { ++ if (png_ptr->zstream.avail_in == 0) ++ { ++ if (read_size > *chunk_bytes) ++ read_size = (uInt)*chunk_bytes; ++ *chunk_bytes -= read_size; ++ ++ if (read_size > 0) ++ png_crc_read(png_ptr, read_buffer, read_size); ++ ++ png_ptr->zstream.next_in = read_buffer; ++ png_ptr->zstream.avail_in = read_size; ++ } ++ ++ if (png_ptr->zstream.avail_out == 0) ++ { ++ uInt avail = ZLIB_IO_MAX; ++ if (avail > *out_size) ++ avail = (uInt)*out_size; ++ *out_size -= avail; ++ ++ png_ptr->zstream.avail_out = avail; ++ } ++ ++ /* Use Z_SYNC_FLUSH when there is no more chunk data to ensure that all ++ * the available output is produced; this allows reading of truncated ++ * streams. ++ */ ++ ret = PNG_INFLATE(png_ptr, *chunk_bytes > 0 ? ++ Z_NO_FLUSH : (finish ? Z_FINISH : Z_SYNC_FLUSH)); ++ } ++ while (ret == Z_OK && (*out_size > 0 || png_ptr->zstream.avail_out > 0)); ++ ++ *out_size += png_ptr->zstream.avail_out; ++ png_ptr->zstream.avail_out = 0; /* Should not be required, but is safe */ ++ ++ /* Ensure the error message pointer is always set: */ ++ png_zstream_error(png_ptr, ret); ++ return ret; ++ } ++ ++ else ++ { ++ png_ptr->zstream.msg = PNGZ_MSG_CAST("zstream unclaimed"); ++ return Z_STREAM_ERROR; ++ } ++} ++#endif /* READ_iCCP */ ++ ++/* Read and check the IDHR chunk */ ++ ++void /* PRIVATE */ ++png_handle_IHDR(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) ++{ ++ png_byte buf[13]; ++ png_uint_32 width, height; ++ int bit_depth, color_type, compression_type, filter_type; ++ int interlace_type; ++ ++ png_debug(1, "in png_handle_IHDR"); ++ ++ if ((png_ptr->mode & PNG_HAVE_IHDR) != 0) ++ png_chunk_error(png_ptr, "out of place"); ++ ++ /* Check the length */ ++ if (length != 13) ++ png_chunk_error(png_ptr, "invalid"); ++ ++ png_ptr->mode |= PNG_HAVE_IHDR; ++ ++ png_crc_read(png_ptr, buf, 13); ++ png_crc_finish(png_ptr, 0); ++ ++ width = png_get_uint_31(png_ptr, buf); ++ height = png_get_uint_31(png_ptr, buf + 4); ++ bit_depth = buf[8]; ++ color_type = buf[9]; ++ compression_type = buf[10]; ++ filter_type = buf[11]; ++ interlace_type = buf[12]; ++ ++ /* Set internal variables */ ++ png_ptr->width = width; ++ png_ptr->height = height; ++ png_ptr->bit_depth = (png_byte)bit_depth; ++ png_ptr->interlaced = (png_byte)interlace_type; ++ png_ptr->color_type = (png_byte)color_type; ++#ifdef PNG_MNG_FEATURES_SUPPORTED ++ png_ptr->filter_type = (png_byte)filter_type; ++#endif ++ png_ptr->compression_type = (png_byte)compression_type; ++ ++ /* Find number of channels */ ++ switch (png_ptr->color_type) ++ { ++ default: /* invalid, png_set_IHDR calls png_error */ ++ case PNG_COLOR_TYPE_GRAY: ++ case PNG_COLOR_TYPE_PALETTE: ++ png_ptr->channels = 1; ++ break; ++ ++ case PNG_COLOR_TYPE_RGB: ++ png_ptr->channels = 3; ++ break; ++ ++ case PNG_COLOR_TYPE_GRAY_ALPHA: ++ png_ptr->channels = 2; ++ break; ++ ++ case PNG_COLOR_TYPE_RGB_ALPHA: ++ png_ptr->channels = 4; ++ break; ++ } ++ ++ /* Set up other useful info */ ++ png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth * png_ptr->channels); ++ png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->width); ++ png_debug1(3, "bit_depth = %d", png_ptr->bit_depth); ++ png_debug1(3, "channels = %d", png_ptr->channels); ++ png_debug1(3, "rowbytes = %lu", (unsigned long)png_ptr->rowbytes); ++ png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, ++ color_type, interlace_type, compression_type, filter_type); ++} ++ ++/* Read and check the palette */ ++void /* PRIVATE */ ++png_handle_PLTE(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) ++{ ++ png_color palette[PNG_MAX_PALETTE_LENGTH]; ++ int max_palette_length, num, i; ++#ifdef PNG_POINTER_INDEXING_SUPPORTED ++ png_colorp pal_ptr; ++#endif ++ ++ png_debug(1, "in png_handle_PLTE"); ++ ++ if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) ++ png_chunk_error(png_ptr, "missing IHDR"); ++ ++ /* Moved to before the 'after IDAT' check below because otherwise duplicate ++ * PLTE chunks are potentially ignored (the spec says there shall not be more ++ * than one PLTE, the error is not treated as benign, so this check trumps ++ * the requirement that PLTE appears before IDAT.) ++ */ ++ else if ((png_ptr->mode & PNG_HAVE_PLTE) != 0) ++ png_chunk_error(png_ptr, "duplicate"); ++ ++ else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) ++ { ++ /* This is benign because the non-benign error happened before, when an ++ * IDAT was encountered in a color-mapped image with no PLTE. ++ */ ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "out of place"); ++ return; ++ } ++ ++ png_ptr->mode |= PNG_HAVE_PLTE; ++ ++ if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "ignored in grayscale PNG"); ++ return; ++ } ++ ++#ifndef PNG_READ_OPT_PLTE_SUPPORTED ++ if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE) ++ { ++ png_crc_finish(png_ptr, length); ++ return; ++ } ++#endif ++ ++ if (length > 3*PNG_MAX_PALETTE_LENGTH || length % 3) ++ { ++ png_crc_finish(png_ptr, length); ++ ++ if (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE) ++ png_chunk_benign_error(png_ptr, "invalid"); ++ ++ else ++ png_chunk_error(png_ptr, "invalid"); ++ ++ return; ++ } ++ ++ /* The cast is safe because 'length' is less than 3*PNG_MAX_PALETTE_LENGTH */ ++ num = (int)length / 3; ++ ++ /* If the palette has 256 or fewer entries but is too large for the bit ++ * depth, we don't issue an error, to preserve the behavior of previous ++ * libpng versions. We silently truncate the unused extra palette entries ++ * here. ++ */ ++ if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ++ max_palette_length = (1 << png_ptr->bit_depth); ++ else ++ max_palette_length = PNG_MAX_PALETTE_LENGTH; ++ ++ if (num > max_palette_length) ++ num = max_palette_length; ++ ++#ifdef PNG_POINTER_INDEXING_SUPPORTED ++ for (i = 0, pal_ptr = palette; i < num; i++, pal_ptr++) ++ { ++ png_byte buf[3]; ++ ++ png_crc_read(png_ptr, buf, 3); ++ pal_ptr->red = buf[0]; ++ pal_ptr->green = buf[1]; ++ pal_ptr->blue = buf[2]; ++ } ++#else ++ for (i = 0; i < num; i++) ++ { ++ png_byte buf[3]; ++ ++ png_crc_read(png_ptr, buf, 3); ++ /* Don't depend upon png_color being any order */ ++ palette[i].red = buf[0]; ++ palette[i].green = buf[1]; ++ palette[i].blue = buf[2]; ++ } ++#endif ++ ++ /* If we actually need the PLTE chunk (ie for a paletted image), we do ++ * whatever the normal CRC configuration tells us. However, if we ++ * have an RGB image, the PLTE can be considered ancillary, so ++ * we will act as though it is. ++ */ ++#ifndef PNG_READ_OPT_PLTE_SUPPORTED ++ if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ++#endif ++ { ++ png_crc_finish(png_ptr, (png_uint_32) (length - (unsigned int)num * 3)); ++ } ++ ++#ifndef PNG_READ_OPT_PLTE_SUPPORTED ++ else if (png_crc_error(png_ptr) != 0) /* Only if we have a CRC error */ ++ { ++ /* If we don't want to use the data from an ancillary chunk, ++ * we have two options: an error abort, or a warning and we ++ * ignore the data in this chunk (which should be OK, since ++ * it's considered ancillary for a RGB or RGBA image). ++ * ++ * IMPLEMENTATION NOTE: this is only here because png_crc_finish uses the ++ * chunk type to determine whether to check the ancillary or the critical ++ * flags. ++ */ ++ if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_USE) == 0) ++ { ++ if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) != 0) ++ return; ++ ++ else ++ png_chunk_error(png_ptr, "CRC error"); ++ } ++ ++ /* Otherwise, we (optionally) emit a warning and use the chunk. */ ++ else if ((png_ptr->flags & PNG_FLAG_CRC_ANCILLARY_NOWARN) == 0) ++ png_chunk_warning(png_ptr, "CRC error"); ++ } ++#endif ++ ++ /* TODO: png_set_PLTE has the side effect of setting png_ptr->palette to its ++ * own copy of the palette. This has the side effect that when png_start_row ++ * is called (this happens after any call to png_read_update_info) the ++ * info_ptr palette gets changed. This is extremely unexpected and ++ * confusing. ++ * ++ * Fix this by not sharing the palette in this way. ++ */ ++ png_set_PLTE(png_ptr, info_ptr, palette, num); ++ ++ /* The three chunks, bKGD, hIST and tRNS *must* appear after PLTE and before ++ * IDAT. Prior to 1.6.0 this was not checked; instead the code merely ++ * checked the apparent validity of a tRNS chunk inserted before PLTE on a ++ * palette PNG. 1.6.0 attempts to rigorously follow the standard and ++ * therefore does a benign error if the erroneous condition is detected *and* ++ * cancels the tRNS if the benign error returns. The alternative is to ++ * amend the standard since it would be rather hypocritical of the standards ++ * maintainers to ignore it. ++ */ ++#ifdef PNG_READ_tRNS_SUPPORTED ++ if (png_ptr->num_trans > 0 || ++ (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0)) ++ { ++ /* Cancel this because otherwise it would be used if the transforms ++ * require it. Don't cancel the 'valid' flag because this would prevent ++ * detection of duplicate chunks. ++ */ ++ png_ptr->num_trans = 0; ++ ++ if (info_ptr != NULL) ++ info_ptr->num_trans = 0; ++ ++ png_chunk_benign_error(png_ptr, "tRNS must be after"); ++ } ++#endif ++ ++#ifdef PNG_READ_hIST_SUPPORTED ++ if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0) ++ png_chunk_benign_error(png_ptr, "hIST must be after"); ++#endif ++ ++#ifdef PNG_READ_bKGD_SUPPORTED ++ if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0) ++ png_chunk_benign_error(png_ptr, "bKGD must be after"); ++#endif ++} ++ ++void /* PRIVATE */ ++png_handle_IEND(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) ++{ ++ png_debug(1, "in png_handle_IEND"); ++ ++ if ((png_ptr->mode & PNG_HAVE_IHDR) == 0 || ++ (png_ptr->mode & PNG_HAVE_IDAT) == 0) ++ png_chunk_error(png_ptr, "out of place"); ++ ++ png_ptr->mode |= (PNG_AFTER_IDAT | PNG_HAVE_IEND); ++ ++ png_crc_finish(png_ptr, length); ++ ++ if (length != 0) ++ png_chunk_benign_error(png_ptr, "invalid"); ++ ++ PNG_UNUSED(info_ptr) ++} ++ ++#ifdef PNG_READ_gAMA_SUPPORTED ++void /* PRIVATE */ ++png_handle_gAMA(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) ++{ ++ png_fixed_point igamma; ++ png_byte buf[4]; ++ ++ png_debug(1, "in png_handle_gAMA"); ++ ++ if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) ++ png_chunk_error(png_ptr, "missing IHDR"); ++ ++ else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "out of place"); ++ return; ++ } ++ ++ if (length != 4) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "invalid"); ++ return; ++ } ++ ++ png_crc_read(png_ptr, buf, 4); ++ ++ if (png_crc_finish(png_ptr, 0) != 0) ++ return; ++ ++ igamma = png_get_fixed_point(NULL, buf); ++ ++ png_colorspace_set_gamma(png_ptr, &png_ptr->colorspace, igamma); ++ png_colorspace_sync(png_ptr, info_ptr); ++} ++#endif ++ ++#ifdef PNG_READ_sBIT_SUPPORTED ++void /* PRIVATE */ ++png_handle_sBIT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) ++{ ++ unsigned int truelen, i; ++ png_byte sample_depth; ++ png_byte buf[4]; ++ ++ png_debug(1, "in png_handle_sBIT"); ++ ++ if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) ++ png_chunk_error(png_ptr, "missing IHDR"); ++ ++ else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "out of place"); ++ return; ++ } ++ ++ if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sBIT) != 0) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "duplicate"); ++ return; ++ } ++ ++ if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ++ { ++ truelen = 3; ++ sample_depth = 8; ++ } ++ ++ else ++ { ++ truelen = png_ptr->channels; ++ sample_depth = png_ptr->bit_depth; ++ } ++ ++ if (length != truelen || length > 4) ++ { ++ png_chunk_benign_error(png_ptr, "invalid"); ++ png_crc_finish(png_ptr, length); ++ return; ++ } ++ ++ buf[0] = buf[1] = buf[2] = buf[3] = sample_depth; ++ png_crc_read(png_ptr, buf, truelen); ++ ++ if (png_crc_finish(png_ptr, 0) != 0) ++ return; ++ ++ for (i=0; i sample_depth) ++ { ++ png_chunk_benign_error(png_ptr, "invalid"); ++ return; ++ } ++ } ++ ++ if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0) ++ { ++ png_ptr->sig_bit.red = buf[0]; ++ png_ptr->sig_bit.green = buf[1]; ++ png_ptr->sig_bit.blue = buf[2]; ++ png_ptr->sig_bit.alpha = buf[3]; ++ } ++ ++ else ++ { ++ png_ptr->sig_bit.gray = buf[0]; ++ png_ptr->sig_bit.red = buf[0]; ++ png_ptr->sig_bit.green = buf[0]; ++ png_ptr->sig_bit.blue = buf[0]; ++ png_ptr->sig_bit.alpha = buf[1]; ++ } ++ ++ png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit)); ++} ++#endif ++ ++#ifdef PNG_READ_cHRM_SUPPORTED ++void /* PRIVATE */ ++png_handle_cHRM(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) ++{ ++ png_byte buf[32]; ++ png_xy xy; ++ ++ png_debug(1, "in png_handle_cHRM"); ++ ++ if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) ++ png_chunk_error(png_ptr, "missing IHDR"); ++ ++ else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "out of place"); ++ return; ++ } ++ ++ if (length != 32) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "invalid"); ++ return; ++ } ++ ++ png_crc_read(png_ptr, buf, 32); ++ ++ if (png_crc_finish(png_ptr, 0) != 0) ++ return; ++ ++ xy.whitex = png_get_fixed_point(NULL, buf); ++ xy.whitey = png_get_fixed_point(NULL, buf + 4); ++ xy.redx = png_get_fixed_point(NULL, buf + 8); ++ xy.redy = png_get_fixed_point(NULL, buf + 12); ++ xy.greenx = png_get_fixed_point(NULL, buf + 16); ++ xy.greeny = png_get_fixed_point(NULL, buf + 20); ++ xy.bluex = png_get_fixed_point(NULL, buf + 24); ++ xy.bluey = png_get_fixed_point(NULL, buf + 28); ++ ++ if (xy.whitex == PNG_FIXED_ERROR || ++ xy.whitey == PNG_FIXED_ERROR || ++ xy.redx == PNG_FIXED_ERROR || ++ xy.redy == PNG_FIXED_ERROR || ++ xy.greenx == PNG_FIXED_ERROR || ++ xy.greeny == PNG_FIXED_ERROR || ++ xy.bluex == PNG_FIXED_ERROR || ++ xy.bluey == PNG_FIXED_ERROR) ++ { ++ png_chunk_benign_error(png_ptr, "invalid values"); ++ return; ++ } ++ ++ /* If a colorspace error has already been output skip this chunk */ ++ if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0) ++ return; ++ ++ if ((png_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM) != 0) ++ { ++ png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID; ++ png_colorspace_sync(png_ptr, info_ptr); ++ png_chunk_benign_error(png_ptr, "duplicate"); ++ return; ++ } ++ ++ png_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM; ++ (void)png_colorspace_set_chromaticities(png_ptr, &png_ptr->colorspace, &xy, ++ 1/*prefer cHRM values*/); ++ png_colorspace_sync(png_ptr, info_ptr); ++} ++#endif ++ ++#ifdef PNG_READ_sRGB_SUPPORTED ++void /* PRIVATE */ ++png_handle_sRGB(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) ++{ ++ png_byte intent; ++ ++ png_debug(1, "in png_handle_sRGB"); ++ ++ if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) ++ png_chunk_error(png_ptr, "missing IHDR"); ++ ++ else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "out of place"); ++ return; ++ } ++ ++ if (length != 1) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "invalid"); ++ return; ++ } ++ ++ png_crc_read(png_ptr, &intent, 1); ++ ++ if (png_crc_finish(png_ptr, 0) != 0) ++ return; ++ ++ /* If a colorspace error has already been output skip this chunk */ ++ if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0) ++ return; ++ ++ /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect ++ * this. ++ */ ++ if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) != 0) ++ { ++ png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID; ++ png_colorspace_sync(png_ptr, info_ptr); ++ png_chunk_benign_error(png_ptr, "too many profiles"); ++ return; ++ } ++ ++ (void)png_colorspace_set_sRGB(png_ptr, &png_ptr->colorspace, intent); ++ png_colorspace_sync(png_ptr, info_ptr); ++} ++#endif /* READ_sRGB */ ++ ++#ifdef PNG_READ_iCCP_SUPPORTED ++void /* PRIVATE */ ++png_handle_iCCP(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) ++/* Note: this does not properly handle profiles that are > 64K under DOS */ ++{ ++ png_const_charp errmsg = NULL; /* error message output, or no error */ ++ int finished = 0; /* crc checked */ ++ ++ png_debug(1, "in png_handle_iCCP"); ++ ++ if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) ++ png_chunk_error(png_ptr, "missing IHDR"); ++ ++ else if ((png_ptr->mode & (PNG_HAVE_IDAT|PNG_HAVE_PLTE)) != 0) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "out of place"); ++ return; ++ } ++ ++ /* Consistent with all the above colorspace handling an obviously *invalid* ++ * chunk is just ignored, so does not invalidate the color space. An ++ * alternative is to set the 'invalid' flags at the start of this routine ++ * and only clear them in they were not set before and all the tests pass. ++ */ ++ ++ /* The keyword must be at least one character and there is a ++ * terminator (0) byte and the compression method byte, and the ++ * 'zlib' datastream is at least 11 bytes. ++ */ ++ if (length < 14) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "too short"); ++ return; ++ } ++ ++ /* If a colorspace error has already been output skip this chunk */ ++ if ((png_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) != 0) ++ { ++ png_crc_finish(png_ptr, length); ++ return; ++ } ++ ++ /* Only one sRGB or iCCP chunk is allowed, use the HAVE_INTENT flag to detect ++ * this. ++ */ ++ if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_INTENT) == 0) ++ { ++ uInt read_length, keyword_length; ++ char keyword[81]; ++ ++ /* Find the keyword; the keyword plus separator and compression method ++ * bytes can be at most 81 characters long. ++ */ ++ read_length = 81; /* maximum */ ++ if (read_length > length) ++ read_length = (uInt)length; ++ ++ png_crc_read(png_ptr, (png_bytep)keyword, read_length); ++ length -= read_length; ++ ++ /* The minimum 'zlib' stream is assumed to be just the 2 byte header, ++ * 5 bytes minimum 'deflate' stream, and the 4 byte checksum. ++ */ ++ if (length < 11) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "too short"); ++ return; ++ } ++ ++ keyword_length = 0; ++ while (keyword_length < 80 && keyword_length < read_length && ++ keyword[keyword_length] != 0) ++ ++keyword_length; ++ ++ /* TODO: make the keyword checking common */ ++ if (keyword_length >= 1 && keyword_length <= 79) ++ { ++ /* We only understand '0' compression - deflate - so if we get a ++ * different value we can't safely decode the chunk. ++ */ ++ if (keyword_length+1 < read_length && ++ keyword[keyword_length+1] == PNG_COMPRESSION_TYPE_BASE) ++ { ++ read_length -= keyword_length+2; ++ ++ if (png_inflate_claim(png_ptr, png_iCCP) == Z_OK) ++ { ++ Byte profile_header[132]={0}; ++ Byte local_buffer[PNG_INFLATE_BUF_SIZE]; ++ png_alloc_size_t size = (sizeof profile_header); ++ ++ png_ptr->zstream.next_in = (Bytef*)keyword + (keyword_length+2); ++ png_ptr->zstream.avail_in = read_length; ++ (void)png_inflate_read(png_ptr, local_buffer, ++ (sizeof local_buffer), &length, profile_header, &size, ++ 0/*finish: don't, because the output is too small*/); ++ ++ if (size == 0) ++ { ++ /* We have the ICC profile header; do the basic header checks. ++ */ ++ png_uint_32 profile_length = png_get_uint_32(profile_header); ++ ++ if (png_icc_check_length(png_ptr, &png_ptr->colorspace, ++ keyword, profile_length) != 0) ++ { ++ /* The length is apparently ok, so we can check the 132 ++ * byte header. ++ */ ++ if (png_icc_check_header(png_ptr, &png_ptr->colorspace, ++ keyword, profile_length, profile_header, ++ png_ptr->color_type) != 0) ++ { ++ /* Now read the tag table; a variable size buffer is ++ * needed at this point, allocate one for the whole ++ * profile. The header check has already validated ++ * that none of this stuff will overflow. ++ */ ++ png_uint_32 tag_count = ++ png_get_uint_32(profile_header + 128); ++ png_bytep profile = png_read_buffer(png_ptr, ++ profile_length, 2/*silent*/); ++ ++ if (profile != NULL) ++ { ++ memcpy(profile, profile_header, ++ (sizeof profile_header)); ++ ++ size = 12 * tag_count; ++ ++ (void)png_inflate_read(png_ptr, local_buffer, ++ (sizeof local_buffer), &length, ++ profile + (sizeof profile_header), &size, 0); ++ ++ /* Still expect a buffer error because we expect ++ * there to be some tag data! ++ */ ++ if (size == 0) ++ { ++ if (png_icc_check_tag_table(png_ptr, ++ &png_ptr->colorspace, keyword, profile_length, ++ profile) != 0) ++ { ++ /* The profile has been validated for basic ++ * security issues, so read the whole thing in. ++ */ ++ size = profile_length - (sizeof profile_header) ++ - 12 * tag_count; ++ ++ (void)png_inflate_read(png_ptr, local_buffer, ++ (sizeof local_buffer), &length, ++ profile + (sizeof profile_header) + ++ 12 * tag_count, &size, 1/*finish*/); ++ ++ if (length > 0 && !(png_ptr->flags & ++ PNG_FLAG_BENIGN_ERRORS_WARN)) ++ errmsg = "extra compressed data"; ++ ++ /* But otherwise allow extra data: */ ++ else if (size == 0) ++ { ++ if (length > 0) ++ { ++ /* This can be handled completely, so ++ * keep going. ++ */ ++ png_chunk_warning(png_ptr, ++ "extra compressed data"); ++ } ++ ++ png_crc_finish(png_ptr, length); ++ finished = 1; ++ ++# if defined(PNG_sRGB_SUPPORTED) && PNG_sRGB_PROFILE_CHECKS >= 0 ++ /* Check for a match against sRGB */ ++ png_icc_set_sRGB(png_ptr, ++ &png_ptr->colorspace, profile, ++ png_ptr->zstream.adler); ++# endif ++ ++ /* Steal the profile for info_ptr. */ ++ if (info_ptr != NULL) ++ { ++ png_free_data(png_ptr, info_ptr, ++ PNG_FREE_ICCP, 0); ++ ++ info_ptr->iccp_name = png_voidcast(char*, ++ png_malloc_base(png_ptr, ++ keyword_length+1)); ++ if (info_ptr->iccp_name != NULL) ++ { ++ memcpy(info_ptr->iccp_name, keyword, ++ keyword_length+1); ++ info_ptr->iccp_proflen = ++ profile_length; ++ info_ptr->iccp_profile = profile; ++ png_ptr->read_buffer = NULL; /*steal*/ ++ info_ptr->free_me |= PNG_FREE_ICCP; ++ info_ptr->valid |= PNG_INFO_iCCP; ++ } ++ ++ else ++ { ++ png_ptr->colorspace.flags |= ++ PNG_COLORSPACE_INVALID; ++ errmsg = "out of memory"; ++ } ++ } ++ ++ /* else the profile remains in the read ++ * buffer which gets reused for subsequent ++ * chunks. ++ */ ++ ++ if (info_ptr != NULL) ++ png_colorspace_sync(png_ptr, info_ptr); ++ ++ if (errmsg == NULL) ++ { ++ png_ptr->zowner = 0; ++ return; ++ } ++ } ++ if (errmsg == NULL) ++ errmsg = png_ptr->zstream.msg; ++ } ++ /* else png_icc_check_tag_table output an error */ ++ } ++ else /* profile truncated */ ++ errmsg = png_ptr->zstream.msg; ++ } ++ ++ else ++ errmsg = "out of memory"; ++ } ++ ++ /* else png_icc_check_header output an error */ ++ } ++ ++ /* else png_icc_check_length output an error */ ++ } ++ ++ else /* profile truncated */ ++ errmsg = png_ptr->zstream.msg; ++ ++ /* Release the stream */ ++ png_ptr->zowner = 0; ++ } ++ ++ else /* png_inflate_claim failed */ ++ errmsg = png_ptr->zstream.msg; ++ } ++ ++ else ++ errmsg = "bad compression method"; /* or missing */ ++ } ++ ++ else ++ errmsg = "bad keyword"; ++ } ++ ++ else ++ errmsg = "too many profiles"; ++ ++ /* Failure: the reason is in 'errmsg' */ ++ if (finished == 0) ++ png_crc_finish(png_ptr, length); ++ ++ png_ptr->colorspace.flags |= PNG_COLORSPACE_INVALID; ++ png_colorspace_sync(png_ptr, info_ptr); ++ if (errmsg != NULL) /* else already output */ ++ png_chunk_benign_error(png_ptr, errmsg); ++} ++#endif /* READ_iCCP */ ++ ++#ifdef PNG_READ_sPLT_SUPPORTED ++void /* PRIVATE */ ++png_handle_sPLT(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) ++/* Note: this does not properly handle chunks that are > 64K under DOS */ ++{ ++ png_bytep entry_start, buffer; ++ png_sPLT_t new_palette; ++ png_sPLT_entryp pp; ++ png_uint_32 data_length; ++ int entry_size, i; ++ png_uint_32 skip = 0; ++ png_uint_32 dl; ++ size_t max_dl; ++ ++ png_debug(1, "in png_handle_sPLT"); ++ ++#ifdef PNG_USER_LIMITS_SUPPORTED ++ if (png_ptr->user_chunk_cache_max != 0) ++ { ++ if (png_ptr->user_chunk_cache_max == 1) ++ { ++ png_crc_finish(png_ptr, length); ++ return; ++ } ++ ++ if (--png_ptr->user_chunk_cache_max == 1) ++ { ++ png_warning(png_ptr, "No space in chunk cache for sPLT"); ++ png_crc_finish(png_ptr, length); ++ return; ++ } ++ } ++#endif ++ ++ if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) ++ png_chunk_error(png_ptr, "missing IHDR"); ++ ++ else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "out of place"); ++ return; ++ } ++ ++#ifdef PNG_MAX_MALLOC_64K ++ if (length > 65535U) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "too large to fit in memory"); ++ return; ++ } ++#endif ++ ++ buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/); ++ if (buffer == NULL) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "out of memory"); ++ return; ++ } ++ ++ ++ /* WARNING: this may break if size_t is less than 32 bits; it is assumed ++ * that the PNG_MAX_MALLOC_64K test is enabled in this case, but this is a ++ * potential breakage point if the types in pngconf.h aren't exactly right. ++ */ ++ png_crc_read(png_ptr, buffer, length); ++ ++ if (png_crc_finish(png_ptr, skip) != 0) ++ return; ++ ++ buffer[length] = 0; ++ ++ for (entry_start = buffer; *entry_start; entry_start++) ++ /* Empty loop to find end of name */ ; ++ ++ ++entry_start; ++ ++ /* A sample depth should follow the separator, and we should be on it */ ++ if (length < 2U || entry_start > buffer + (length - 2U)) ++ { ++ png_warning(png_ptr, "malformed sPLT chunk"); ++ return; ++ } ++ ++ new_palette.depth = *entry_start++; ++ entry_size = (new_palette.depth == 8 ? 6 : 10); ++ /* This must fit in a png_uint_32 because it is derived from the original ++ * chunk data length. ++ */ ++ data_length = length - (png_uint_32)(entry_start - buffer); ++ ++ /* Integrity-check the data length */ ++ if ((data_length % (unsigned int)entry_size) != 0) ++ { ++ png_warning(png_ptr, "sPLT chunk has bad length"); ++ return; ++ } ++ ++ dl = (png_uint_32)(data_length / (unsigned int)entry_size); ++ max_dl = PNG_SIZE_MAX / (sizeof (png_sPLT_entry)); ++ ++ if (dl > max_dl) ++ { ++ png_warning(png_ptr, "sPLT chunk too long"); ++ return; ++ } ++ ++ new_palette.nentries = (png_int_32)(data_length / (unsigned int)entry_size); ++ ++ new_palette.entries = (png_sPLT_entryp)png_malloc_warn(png_ptr, ++ (png_alloc_size_t) new_palette.nentries * (sizeof (png_sPLT_entry))); ++ ++ if (new_palette.entries == NULL) ++ { ++ png_warning(png_ptr, "sPLT chunk requires too much memory"); ++ return; ++ } ++ ++#ifdef PNG_POINTER_INDEXING_SUPPORTED ++ for (i = 0; i < new_palette.nentries; i++) ++ { ++ pp = new_palette.entries + i; ++ ++ if (new_palette.depth == 8) ++ { ++ pp->red = *entry_start++; ++ pp->green = *entry_start++; ++ pp->blue = *entry_start++; ++ pp->alpha = *entry_start++; ++ } ++ ++ else ++ { ++ pp->red = png_get_uint_16(entry_start); entry_start += 2; ++ pp->green = png_get_uint_16(entry_start); entry_start += 2; ++ pp->blue = png_get_uint_16(entry_start); entry_start += 2; ++ pp->alpha = png_get_uint_16(entry_start); entry_start += 2; ++ } ++ ++ pp->frequency = png_get_uint_16(entry_start); entry_start += 2; ++ } ++#else ++ pp = new_palette.entries; ++ ++ for (i = 0; i < new_palette.nentries; i++) ++ { ++ ++ if (new_palette.depth == 8) ++ { ++ pp[i].red = *entry_start++; ++ pp[i].green = *entry_start++; ++ pp[i].blue = *entry_start++; ++ pp[i].alpha = *entry_start++; ++ } ++ ++ else ++ { ++ pp[i].red = png_get_uint_16(entry_start); entry_start += 2; ++ pp[i].green = png_get_uint_16(entry_start); entry_start += 2; ++ pp[i].blue = png_get_uint_16(entry_start); entry_start += 2; ++ pp[i].alpha = png_get_uint_16(entry_start); entry_start += 2; ++ } ++ ++ pp[i].frequency = png_get_uint_16(entry_start); entry_start += 2; ++ } ++#endif ++ ++ /* Discard all chunk data except the name and stash that */ ++ new_palette.name = (png_charp)buffer; ++ ++ png_set_sPLT(png_ptr, info_ptr, &new_palette, 1); ++ ++ png_free(png_ptr, new_palette.entries); ++} ++#endif /* READ_sPLT */ ++ ++#ifdef PNG_READ_tRNS_SUPPORTED ++void /* PRIVATE */ ++png_handle_tRNS(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) ++{ ++ png_byte readbuf[PNG_MAX_PALETTE_LENGTH]; ++ ++ png_debug(1, "in png_handle_tRNS"); ++ ++ if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) ++ png_chunk_error(png_ptr, "missing IHDR"); ++ ++ else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "out of place"); ++ return; ++ } ++ ++ else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tRNS) != 0) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "duplicate"); ++ return; ++ } ++ ++ if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) ++ { ++ png_byte buf[2]; ++ ++ if (length != 2) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "invalid"); ++ return; ++ } ++ ++ png_crc_read(png_ptr, buf, 2); ++ png_ptr->num_trans = 1; ++ png_ptr->trans_color.gray = png_get_uint_16(buf); ++ } ++ ++ else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB) ++ { ++ png_byte buf[6]; ++ ++ if (length != 6) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "invalid"); ++ return; ++ } ++ ++ png_crc_read(png_ptr, buf, length); ++ png_ptr->num_trans = 1; ++ png_ptr->trans_color.red = png_get_uint_16(buf); ++ png_ptr->trans_color.green = png_get_uint_16(buf + 2); ++ png_ptr->trans_color.blue = png_get_uint_16(buf + 4); ++ } ++ ++ else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ++ { ++ if ((png_ptr->mode & PNG_HAVE_PLTE) == 0) ++ { ++ /* TODO: is this actually an error in the ISO spec? */ ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "out of place"); ++ return; ++ } ++ ++ if (length > (unsigned int) png_ptr->num_palette || ++ length > (unsigned int) PNG_MAX_PALETTE_LENGTH || ++ length == 0) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "invalid"); ++ return; ++ } ++ ++ png_crc_read(png_ptr, readbuf, length); ++ png_ptr->num_trans = (png_uint_16)length; ++ } ++ ++ else ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "invalid with alpha channel"); ++ return; ++ } ++ ++ if (png_crc_finish(png_ptr, 0) != 0) ++ { ++ png_ptr->num_trans = 0; ++ return; ++ } ++ ++ /* TODO: this is a horrible side effect in the palette case because the ++ * png_struct ends up with a pointer to the tRNS buffer owned by the ++ * png_info. Fix this. ++ */ ++ png_set_tRNS(png_ptr, info_ptr, readbuf, png_ptr->num_trans, ++ &(png_ptr->trans_color)); ++} ++#endif ++ ++#ifdef PNG_READ_bKGD_SUPPORTED ++void /* PRIVATE */ ++png_handle_bKGD(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) ++{ ++ unsigned int truelen; ++ png_byte buf[6]; ++ png_color_16 background; ++ ++ png_debug(1, "in png_handle_bKGD"); ++ ++ if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) ++ png_chunk_error(png_ptr, "missing IHDR"); ++ ++ else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0 || ++ (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE && ++ (png_ptr->mode & PNG_HAVE_PLTE) == 0)) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "out of place"); ++ return; ++ } ++ ++ else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_bKGD) != 0) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "duplicate"); ++ return; ++ } ++ ++ if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ++ truelen = 1; ++ ++ else if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0) ++ truelen = 6; ++ ++ else ++ truelen = 2; ++ ++ if (length != truelen) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "invalid"); ++ return; ++ } ++ ++ png_crc_read(png_ptr, buf, truelen); ++ ++ if (png_crc_finish(png_ptr, 0) != 0) ++ return; ++ ++ /* We convert the index value into RGB components so that we can allow ++ * arbitrary RGB values for background when we have transparency, and ++ * so it is easy to determine the RGB values of the background color ++ * from the info_ptr struct. ++ */ ++ if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ++ { ++ background.index = buf[0]; ++ ++ if (info_ptr != NULL && info_ptr->num_palette != 0) ++ { ++ if (buf[0] >= info_ptr->num_palette) ++ { ++ png_chunk_benign_error(png_ptr, "invalid index"); ++ return; ++ } ++ ++ background.red = (png_uint_16)png_ptr->palette[buf[0]].red; ++ background.green = (png_uint_16)png_ptr->palette[buf[0]].green; ++ background.blue = (png_uint_16)png_ptr->palette[buf[0]].blue; ++ } ++ ++ else ++ background.red = background.green = background.blue = 0; ++ ++ background.gray = 0; ++ } ++ ++ else if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0) /* GRAY */ ++ { ++ if (png_ptr->bit_depth <= 8) ++ { ++ if (buf[0] != 0 || buf[1] >= (unsigned int)(1 << png_ptr->bit_depth)) ++ { ++ png_chunk_benign_error(png_ptr, "invalid gray level"); ++ return; ++ } ++ } ++ ++ background.index = 0; ++ background.red = ++ background.green = ++ background.blue = ++ background.gray = png_get_uint_16(buf); ++ } ++ ++ else ++ { ++ if (png_ptr->bit_depth <= 8) ++ { ++ if (buf[0] != 0 || buf[2] != 0 || buf[4] != 0) ++ { ++ png_chunk_benign_error(png_ptr, "invalid color"); ++ return; ++ } ++ } ++ ++ background.index = 0; ++ background.red = png_get_uint_16(buf); ++ background.green = png_get_uint_16(buf + 2); ++ background.blue = png_get_uint_16(buf + 4); ++ background.gray = 0; ++ } ++ ++ png_set_bKGD(png_ptr, info_ptr, &background); ++} ++#endif ++ ++#ifdef PNG_READ_eXIf_SUPPORTED ++void /* PRIVATE */ ++png_handle_eXIf(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) ++{ ++ unsigned int i; ++ ++ png_debug(1, "in png_handle_eXIf"); ++ ++ if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) ++ png_chunk_error(png_ptr, "missing IHDR"); ++ ++ if (length < 2) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "too short"); ++ return; ++ } ++ ++ else if (info_ptr == NULL || (info_ptr->valid & PNG_INFO_eXIf) != 0) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "duplicate"); ++ return; ++ } ++ ++ info_ptr->free_me |= PNG_FREE_EXIF; ++ ++ info_ptr->eXIf_buf = png_voidcast(png_bytep, ++ png_malloc_warn(png_ptr, length)); ++ ++ if (info_ptr->eXIf_buf == NULL) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "out of memory"); ++ return; ++ } ++ ++ for (i = 0; i < length; i++) ++ { ++ png_byte buf[1]; ++ png_crc_read(png_ptr, buf, 1); ++ info_ptr->eXIf_buf[i] = buf[0]; ++ if (i == 1 && buf[0] != 'M' && buf[0] != 'I' ++ && info_ptr->eXIf_buf[0] != buf[0]) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "incorrect byte-order specifier"); ++ png_free(png_ptr, info_ptr->eXIf_buf); ++ info_ptr->eXIf_buf = NULL; ++ return; ++ } ++ } ++ ++ if (png_crc_finish(png_ptr, 0) != 0) ++ return; ++ ++ png_set_eXIf_1(png_ptr, info_ptr, length, info_ptr->eXIf_buf); ++ ++ png_free(png_ptr, info_ptr->eXIf_buf); ++ info_ptr->eXIf_buf = NULL; ++} ++#endif ++ ++#ifdef PNG_READ_hIST_SUPPORTED ++void /* PRIVATE */ ++png_handle_hIST(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) ++{ ++ unsigned int num, i; ++ png_uint_16 readbuf[PNG_MAX_PALETTE_LENGTH]; ++ ++ png_debug(1, "in png_handle_hIST"); ++ ++ if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) ++ png_chunk_error(png_ptr, "missing IHDR"); ++ ++ else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0 || ++ (png_ptr->mode & PNG_HAVE_PLTE) == 0) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "out of place"); ++ return; ++ } ++ ++ else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_hIST) != 0) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "duplicate"); ++ return; ++ } ++ ++ num = length / 2 ; ++ ++ if (num != (unsigned int) png_ptr->num_palette || ++ num > (unsigned int) PNG_MAX_PALETTE_LENGTH) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "invalid"); ++ return; ++ } ++ ++ for (i = 0; i < num; i++) ++ { ++ png_byte buf[2]; ++ ++ png_crc_read(png_ptr, buf, 2); ++ readbuf[i] = png_get_uint_16(buf); ++ } ++ ++ if (png_crc_finish(png_ptr, 0) != 0) ++ return; ++ ++ png_set_hIST(png_ptr, info_ptr, readbuf); ++} ++#endif ++ ++#ifdef PNG_READ_pHYs_SUPPORTED ++void /* PRIVATE */ ++png_handle_pHYs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) ++{ ++ png_byte buf[9]; ++ png_uint_32 res_x, res_y; ++ int unit_type; ++ ++ png_debug(1, "in png_handle_pHYs"); ++ ++ if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) ++ png_chunk_error(png_ptr, "missing IHDR"); ++ ++ else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "out of place"); ++ return; ++ } ++ ++ else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pHYs) != 0) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "duplicate"); ++ return; ++ } ++ ++ if (length != 9) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "invalid"); ++ return; ++ } ++ ++ png_crc_read(png_ptr, buf, 9); ++ ++ if (png_crc_finish(png_ptr, 0) != 0) ++ return; ++ ++ res_x = png_get_uint_32(buf); ++ res_y = png_get_uint_32(buf + 4); ++ unit_type = buf[8]; ++ png_set_pHYs(png_ptr, info_ptr, res_x, res_y, unit_type); ++} ++#endif ++ ++#ifdef PNG_READ_oFFs_SUPPORTED ++void /* PRIVATE */ ++png_handle_oFFs(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) ++{ ++ png_byte buf[9]; ++ png_int_32 offset_x, offset_y; ++ int unit_type; ++ ++ png_debug(1, "in png_handle_oFFs"); ++ ++ if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) ++ png_chunk_error(png_ptr, "missing IHDR"); ++ ++ else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "out of place"); ++ return; ++ } ++ ++ else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_oFFs) != 0) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "duplicate"); ++ return; ++ } ++ ++ if (length != 9) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "invalid"); ++ return; ++ } ++ ++ png_crc_read(png_ptr, buf, 9); ++ ++ if (png_crc_finish(png_ptr, 0) != 0) ++ return; ++ ++ offset_x = png_get_int_32(buf); ++ offset_y = png_get_int_32(buf + 4); ++ unit_type = buf[8]; ++ png_set_oFFs(png_ptr, info_ptr, offset_x, offset_y, unit_type); ++} ++#endif ++ ++#ifdef PNG_READ_pCAL_SUPPORTED ++/* Read the pCAL chunk (described in the PNG Extensions document) */ ++void /* PRIVATE */ ++png_handle_pCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) ++{ ++ png_int_32 X0, X1; ++ png_byte type, nparams; ++ png_bytep buffer, buf, units, endptr; ++ png_charpp params; ++ int i; ++ ++ png_debug(1, "in png_handle_pCAL"); ++ ++ if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) ++ png_chunk_error(png_ptr, "missing IHDR"); ++ ++ else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "out of place"); ++ return; ++ } ++ ++ else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_pCAL) != 0) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "duplicate"); ++ return; ++ } ++ ++ png_debug1(2, "Allocating and reading pCAL chunk data (%u bytes)", ++ length + 1); ++ ++ buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/); ++ ++ if (buffer == NULL) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "out of memory"); ++ return; ++ } ++ ++ png_crc_read(png_ptr, buffer, length); ++ ++ if (png_crc_finish(png_ptr, 0) != 0) ++ return; ++ ++ buffer[length] = 0; /* Null terminate the last string */ ++ ++ png_debug(3, "Finding end of pCAL purpose string"); ++ for (buf = buffer; *buf; buf++) ++ /* Empty loop */ ; ++ ++ endptr = buffer + length; ++ ++ /* We need to have at least 12 bytes after the purpose string ++ * in order to get the parameter information. ++ */ ++ if (endptr - buf <= 12) ++ { ++ png_chunk_benign_error(png_ptr, "invalid"); ++ return; ++ } ++ ++ png_debug(3, "Reading pCAL X0, X1, type, nparams, and units"); ++ X0 = png_get_int_32((png_bytep)buf+1); ++ X1 = png_get_int_32((png_bytep)buf+5); ++ type = buf[9]; ++ nparams = buf[10]; ++ units = buf + 11; ++ ++ png_debug(3, "Checking pCAL equation type and number of parameters"); ++ /* Check that we have the right number of parameters for known ++ * equation types. ++ */ ++ if ((type == PNG_EQUATION_LINEAR && nparams != 2) || ++ (type == PNG_EQUATION_BASE_E && nparams != 3) || ++ (type == PNG_EQUATION_ARBITRARY && nparams != 3) || ++ (type == PNG_EQUATION_HYPERBOLIC && nparams != 4)) ++ { ++ png_chunk_benign_error(png_ptr, "invalid parameter count"); ++ return; ++ } ++ ++ else if (type >= PNG_EQUATION_LAST) ++ { ++ png_chunk_benign_error(png_ptr, "unrecognized equation type"); ++ } ++ ++ for (buf = units; *buf; buf++) ++ /* Empty loop to move past the units string. */ ; ++ ++ png_debug(3, "Allocating pCAL parameters array"); ++ ++ params = png_voidcast(png_charpp, png_malloc_warn(png_ptr, ++ nparams * (sizeof (png_charp)))); ++ ++ if (params == NULL) ++ { ++ png_chunk_benign_error(png_ptr, "out of memory"); ++ return; ++ } ++ ++ /* Get pointers to the start of each parameter string. */ ++ for (i = 0; i < nparams; i++) ++ { ++ buf++; /* Skip the null string terminator from previous parameter. */ ++ ++ png_debug1(3, "Reading pCAL parameter %d", i); ++ ++ for (params[i] = (png_charp)buf; buf <= endptr && *buf != 0; buf++) ++ /* Empty loop to move past each parameter string */ ; ++ ++ /* Make sure we haven't run out of data yet */ ++ if (buf > endptr) ++ { ++ png_free(png_ptr, params); ++ png_chunk_benign_error(png_ptr, "invalid data"); ++ return; ++ } ++ } ++ ++ png_set_pCAL(png_ptr, info_ptr, (png_charp)buffer, X0, X1, type, nparams, ++ (png_charp)units, params); ++ ++ png_free(png_ptr, params); ++} ++#endif ++ ++#ifdef PNG_READ_sCAL_SUPPORTED ++/* Read the sCAL chunk */ ++void /* PRIVATE */ ++png_handle_sCAL(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) ++{ ++ png_bytep buffer; ++ size_t i; ++ int state; ++ ++ png_debug(1, "in png_handle_sCAL"); ++ ++ if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) ++ png_chunk_error(png_ptr, "missing IHDR"); ++ ++ else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "out of place"); ++ return; ++ } ++ ++ else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_sCAL) != 0) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "duplicate"); ++ return; ++ } ++ ++ /* Need unit type, width, \0, height: minimum 4 bytes */ ++ else if (length < 4) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "invalid"); ++ return; ++ } ++ ++ png_debug1(2, "Allocating and reading sCAL chunk data (%u bytes)", ++ length + 1); ++ ++ buffer = png_read_buffer(png_ptr, length+1, 2/*silent*/); ++ ++ if (buffer == NULL) ++ { ++ png_chunk_benign_error(png_ptr, "out of memory"); ++ png_crc_finish(png_ptr, length); ++ return; ++ } ++ ++ png_crc_read(png_ptr, buffer, length); ++ buffer[length] = 0; /* Null terminate the last string */ ++ ++ if (png_crc_finish(png_ptr, 0) != 0) ++ return; ++ ++ /* Validate the unit. */ ++ if (buffer[0] != 1 && buffer[0] != 2) ++ { ++ png_chunk_benign_error(png_ptr, "invalid unit"); ++ return; ++ } ++ ++ /* Validate the ASCII numbers, need two ASCII numbers separated by ++ * a '\0' and they need to fit exactly in the chunk data. ++ */ ++ i = 1; ++ state = 0; ++ ++ if (png_check_fp_number((png_const_charp)buffer, length, &state, &i) == 0 || ++ i >= length || buffer[i++] != 0) ++ png_chunk_benign_error(png_ptr, "bad width format"); ++ ++ else if (PNG_FP_IS_POSITIVE(state) == 0) ++ png_chunk_benign_error(png_ptr, "non-positive width"); ++ ++ else ++ { ++ size_t heighti = i; ++ ++ state = 0; ++ if (png_check_fp_number((png_const_charp)buffer, length, ++ &state, &i) == 0 || i != length) ++ png_chunk_benign_error(png_ptr, "bad height format"); ++ ++ else if (PNG_FP_IS_POSITIVE(state) == 0) ++ png_chunk_benign_error(png_ptr, "non-positive height"); ++ ++ else ++ /* This is the (only) success case. */ ++ png_set_sCAL_s(png_ptr, info_ptr, buffer[0], ++ (png_charp)buffer+1, (png_charp)buffer+heighti); ++ } ++} ++#endif ++ ++#ifdef PNG_READ_tIME_SUPPORTED ++void /* PRIVATE */ ++png_handle_tIME(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) ++{ ++ png_byte buf[7]; ++ png_time mod_time; ++ ++ png_debug(1, "in png_handle_tIME"); ++ ++ if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) ++ png_chunk_error(png_ptr, "missing IHDR"); ++ ++ else if (info_ptr != NULL && (info_ptr->valid & PNG_INFO_tIME) != 0) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "duplicate"); ++ return; ++ } ++ ++ if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) ++ png_ptr->mode |= PNG_AFTER_IDAT; ++ ++ if (length != 7) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "invalid"); ++ return; ++ } ++ ++ png_crc_read(png_ptr, buf, 7); ++ ++ if (png_crc_finish(png_ptr, 0) != 0) ++ return; ++ ++ mod_time.second = buf[6]; ++ mod_time.minute = buf[5]; ++ mod_time.hour = buf[4]; ++ mod_time.day = buf[3]; ++ mod_time.month = buf[2]; ++ mod_time.year = png_get_uint_16(buf); ++ ++ png_set_tIME(png_ptr, info_ptr, &mod_time); ++} ++#endif ++ ++#ifdef PNG_READ_tEXt_SUPPORTED ++/* Note: this does not properly handle chunks that are > 64K under DOS */ ++void /* PRIVATE */ ++png_handle_tEXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) ++{ ++ png_text text_info; ++ png_bytep buffer; ++ png_charp key; ++ png_charp text; ++ png_uint_32 skip = 0; ++ ++ png_debug(1, "in png_handle_tEXt"); ++ ++#ifdef PNG_USER_LIMITS_SUPPORTED ++ if (png_ptr->user_chunk_cache_max != 0) ++ { ++ if (png_ptr->user_chunk_cache_max == 1) ++ { ++ png_crc_finish(png_ptr, length); ++ return; ++ } ++ ++ if (--png_ptr->user_chunk_cache_max == 1) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "no space in chunk cache"); ++ return; ++ } ++ } ++#endif ++ ++ if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) ++ png_chunk_error(png_ptr, "missing IHDR"); ++ ++ if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) ++ png_ptr->mode |= PNG_AFTER_IDAT; ++ ++#ifdef PNG_MAX_MALLOC_64K ++ if (length > 65535U) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "too large to fit in memory"); ++ return; ++ } ++#endif ++ ++ buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/); ++ ++ if (buffer == NULL) ++ { ++ png_chunk_benign_error(png_ptr, "out of memory"); ++ return; ++ } ++ ++ png_crc_read(png_ptr, buffer, length); ++ ++ if (png_crc_finish(png_ptr, skip) != 0) ++ return; ++ ++ key = (png_charp)buffer; ++ key[length] = 0; ++ ++ for (text = key; *text; text++) ++ /* Empty loop to find end of key */ ; ++ ++ if (text != key + length) ++ text++; ++ ++ text_info.compression = PNG_TEXT_COMPRESSION_NONE; ++ text_info.key = key; ++ text_info.lang = NULL; ++ text_info.lang_key = NULL; ++ text_info.itxt_length = 0; ++ text_info.text = text; ++ text_info.text_length = strlen(text); ++ ++ if (png_set_text_2(png_ptr, info_ptr, &text_info, 1) != 0) ++ png_warning(png_ptr, "Insufficient memory to process text chunk"); ++} ++#endif ++ ++#ifdef PNG_READ_zTXt_SUPPORTED ++/* Note: this does not correctly handle chunks that are > 64K under DOS */ ++void /* PRIVATE */ ++png_handle_zTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) ++{ ++ png_const_charp errmsg = NULL; ++ png_bytep buffer; ++ png_uint_32 keyword_length; ++ ++ png_debug(1, "in png_handle_zTXt"); ++ ++#ifdef PNG_USER_LIMITS_SUPPORTED ++ if (png_ptr->user_chunk_cache_max != 0) ++ { ++ if (png_ptr->user_chunk_cache_max == 1) ++ { ++ png_crc_finish(png_ptr, length); ++ return; ++ } ++ ++ if (--png_ptr->user_chunk_cache_max == 1) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "no space in chunk cache"); ++ return; ++ } ++ } ++#endif ++ ++ if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) ++ png_chunk_error(png_ptr, "missing IHDR"); ++ ++ if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) ++ png_ptr->mode |= PNG_AFTER_IDAT; ++ ++ /* Note, "length" is sufficient here; we won't be adding ++ * a null terminator later. ++ */ ++ buffer = png_read_buffer(png_ptr, length, 2/*silent*/); ++ ++ if (buffer == NULL) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "out of memory"); ++ return; ++ } ++ ++ png_crc_read(png_ptr, buffer, length); ++ ++ if (png_crc_finish(png_ptr, 0) != 0) ++ return; ++ ++ /* TODO: also check that the keyword contents match the spec! */ ++ for (keyword_length = 0; ++ keyword_length < length && buffer[keyword_length] != 0; ++ ++keyword_length) ++ /* Empty loop to find end of name */ ; ++ ++ if (keyword_length > 79 || keyword_length < 1) ++ errmsg = "bad keyword"; ++ ++ /* zTXt must have some LZ data after the keyword, although it may expand to ++ * zero bytes; we need a '\0' at the end of the keyword, the compression type ++ * then the LZ data: ++ */ ++ else if (keyword_length + 3 > length) ++ errmsg = "truncated"; ++ ++ else if (buffer[keyword_length+1] != PNG_COMPRESSION_TYPE_BASE) ++ errmsg = "unknown compression type"; ++ ++ else ++ { ++ png_alloc_size_t uncompressed_length = PNG_SIZE_MAX; ++ ++ /* TODO: at present png_decompress_chunk imposes a single application ++ * level memory limit, this should be split to different values for iCCP ++ * and text chunks. ++ */ ++ if (png_decompress_chunk(png_ptr, length, keyword_length+2, ++ &uncompressed_length, 1/*terminate*/) == Z_STREAM_END) ++ { ++ png_text text; ++ ++ if (png_ptr->read_buffer == NULL) ++ errmsg="Read failure in png_handle_zTXt"; ++ else ++ { ++ /* It worked; png_ptr->read_buffer now looks like a tEXt chunk ++ * except for the extra compression type byte and the fact that ++ * it isn't necessarily '\0' terminated. ++ */ ++ buffer = png_ptr->read_buffer; ++ buffer[uncompressed_length+(keyword_length+2)] = 0; ++ ++ text.compression = PNG_TEXT_COMPRESSION_zTXt; ++ text.key = (png_charp)buffer; ++ text.text = (png_charp)(buffer + keyword_length+2); ++ text.text_length = uncompressed_length; ++ text.itxt_length = 0; ++ text.lang = NULL; ++ text.lang_key = NULL; ++ ++ if (png_set_text_2(png_ptr, info_ptr, &text, 1) != 0) ++ errmsg = "insufficient memory"; ++ } ++ } ++ ++ else ++ errmsg = png_ptr->zstream.msg; ++ } ++ ++ if (errmsg != NULL) ++ png_chunk_benign_error(png_ptr, errmsg); ++} ++#endif ++ ++#ifdef PNG_READ_iTXt_SUPPORTED ++/* Note: this does not correctly handle chunks that are > 64K under DOS */ ++void /* PRIVATE */ ++png_handle_iTXt(png_structrp png_ptr, png_inforp info_ptr, png_uint_32 length) ++{ ++ png_const_charp errmsg = NULL; ++ png_bytep buffer; ++ png_uint_32 prefix_length; ++ ++ png_debug(1, "in png_handle_iTXt"); ++ ++#ifdef PNG_USER_LIMITS_SUPPORTED ++ if (png_ptr->user_chunk_cache_max != 0) ++ { ++ if (png_ptr->user_chunk_cache_max == 1) ++ { ++ png_crc_finish(png_ptr, length); ++ return; ++ } ++ ++ if (--png_ptr->user_chunk_cache_max == 1) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "no space in chunk cache"); ++ return; ++ } ++ } ++#endif ++ ++ if ((png_ptr->mode & PNG_HAVE_IHDR) == 0) ++ png_chunk_error(png_ptr, "missing IHDR"); ++ ++ if ((png_ptr->mode & PNG_HAVE_IDAT) != 0) ++ png_ptr->mode |= PNG_AFTER_IDAT; ++ ++ buffer = png_read_buffer(png_ptr, length+1, 1/*warn*/); ++ ++ if (buffer == NULL) ++ { ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "out of memory"); ++ return; ++ } ++ ++ png_crc_read(png_ptr, buffer, length); ++ ++ if (png_crc_finish(png_ptr, 0) != 0) ++ return; ++ ++ /* First the keyword. */ ++ for (prefix_length=0; ++ prefix_length < length && buffer[prefix_length] != 0; ++ ++prefix_length) ++ /* Empty loop */ ; ++ ++ /* Perform a basic check on the keyword length here. */ ++ if (prefix_length > 79 || prefix_length < 1) ++ errmsg = "bad keyword"; ++ ++ /* Expect keyword, compression flag, compression type, language, translated ++ * keyword (both may be empty but are 0 terminated) then the text, which may ++ * be empty. ++ */ ++ else if (prefix_length + 5 > length) ++ errmsg = "truncated"; ++ ++ else if (buffer[prefix_length+1] == 0 || ++ (buffer[prefix_length+1] == 1 && ++ buffer[prefix_length+2] == PNG_COMPRESSION_TYPE_BASE)) ++ { ++ int compressed = buffer[prefix_length+1] != 0; ++ png_uint_32 language_offset, translated_keyword_offset; ++ png_alloc_size_t uncompressed_length = 0; ++ ++ /* Now the language tag */ ++ prefix_length += 3; ++ language_offset = prefix_length; ++ ++ for (; prefix_length < length && buffer[prefix_length] != 0; ++ ++prefix_length) ++ /* Empty loop */ ; ++ ++ /* WARNING: the length may be invalid here, this is checked below. */ ++ translated_keyword_offset = ++prefix_length; ++ ++ for (; prefix_length < length && buffer[prefix_length] != 0; ++ ++prefix_length) ++ /* Empty loop */ ; ++ ++ /* prefix_length should now be at the trailing '\0' of the translated ++ * keyword, but it may already be over the end. None of this arithmetic ++ * can overflow because chunks are at most 2^31 bytes long, but on 16-bit ++ * systems the available allocation may overflow. ++ */ ++ ++prefix_length; ++ ++ if (compressed == 0 && prefix_length <= length) ++ uncompressed_length = length - prefix_length; ++ ++ else if (compressed != 0 && prefix_length < length) ++ { ++ uncompressed_length = PNG_SIZE_MAX; ++ ++ /* TODO: at present png_decompress_chunk imposes a single application ++ * level memory limit, this should be split to different values for ++ * iCCP and text chunks. ++ */ ++ if (png_decompress_chunk(png_ptr, length, prefix_length, ++ &uncompressed_length, 1/*terminate*/) == Z_STREAM_END) ++ buffer = png_ptr->read_buffer; ++ ++ else ++ errmsg = png_ptr->zstream.msg; ++ } ++ ++ else ++ errmsg = "truncated"; ++ ++ if (errmsg == NULL) ++ { ++ png_text text; ++ ++ buffer[uncompressed_length+prefix_length] = 0; ++ ++ if (compressed == 0) ++ text.compression = PNG_ITXT_COMPRESSION_NONE; ++ ++ else ++ text.compression = PNG_ITXT_COMPRESSION_zTXt; ++ ++ text.key = (png_charp)buffer; ++ text.lang = (png_charp)buffer + language_offset; ++ text.lang_key = (png_charp)buffer + translated_keyword_offset; ++ text.text = (png_charp)buffer + prefix_length; ++ text.text_length = 0; ++ text.itxt_length = uncompressed_length; ++ ++ if (png_set_text_2(png_ptr, info_ptr, &text, 1) != 0) ++ errmsg = "insufficient memory"; ++ } ++ } ++ ++ else ++ errmsg = "bad compression info"; ++ ++ if (errmsg != NULL) ++ png_chunk_benign_error(png_ptr, errmsg); ++} ++#endif ++ ++#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED ++/* Utility function for png_handle_unknown; set up png_ptr::unknown_chunk */ ++static int ++png_cache_unknown_chunk(png_structrp png_ptr, png_uint_32 length) ++{ ++ png_alloc_size_t limit = PNG_SIZE_MAX; ++ ++ if (png_ptr->unknown_chunk.data != NULL) ++ { ++ png_free(png_ptr, png_ptr->unknown_chunk.data); ++ png_ptr->unknown_chunk.data = NULL; ++ } ++ ++# ifdef PNG_SET_USER_LIMITS_SUPPORTED ++ if (png_ptr->user_chunk_malloc_max > 0 && ++ png_ptr->user_chunk_malloc_max < limit) ++ limit = png_ptr->user_chunk_malloc_max; ++ ++# elif PNG_USER_CHUNK_MALLOC_MAX > 0 ++ if (PNG_USER_CHUNK_MALLOC_MAX < limit) ++ limit = PNG_USER_CHUNK_MALLOC_MAX; ++# endif ++ ++ if (length <= limit) ++ { ++ PNG_CSTRING_FROM_CHUNK(png_ptr->unknown_chunk.name, png_ptr->chunk_name); ++ /* The following is safe because of the PNG_SIZE_MAX init above */ ++ png_ptr->unknown_chunk.size = (size_t)length/*SAFE*/; ++ /* 'mode' is a flag array, only the bottom four bits matter here */ ++ png_ptr->unknown_chunk.location = (png_byte)png_ptr->mode/*SAFE*/; ++ ++ if (length == 0) ++ png_ptr->unknown_chunk.data = NULL; ++ ++ else ++ { ++ /* Do a 'warn' here - it is handled below. */ ++ png_ptr->unknown_chunk.data = png_voidcast(png_bytep, ++ png_malloc_warn(png_ptr, length)); ++ } ++ } ++ ++ if (png_ptr->unknown_chunk.data == NULL && length > 0) ++ { ++ /* This is benign because we clean up correctly */ ++ png_crc_finish(png_ptr, length); ++ png_chunk_benign_error(png_ptr, "unknown chunk exceeds memory limits"); ++ return 0; ++ } ++ ++ else ++ { ++ if (length > 0) ++ png_crc_read(png_ptr, png_ptr->unknown_chunk.data, length); ++ png_crc_finish(png_ptr, 0); ++ return 1; ++ } ++} ++#endif /* READ_UNKNOWN_CHUNKS */ ++ ++/* Handle an unknown, or known but disabled, chunk */ ++void /* PRIVATE */ ++png_handle_unknown(png_structrp png_ptr, png_inforp info_ptr, ++ png_uint_32 length, int keep) ++{ ++ int handled = 0; /* the chunk was handled */ ++ ++ png_debug(1, "in png_handle_unknown"); ++ ++#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED ++ /* NOTE: this code is based on the code in libpng-1.4.12 except for fixing ++ * the bug which meant that setting a non-default behavior for a specific ++ * chunk would be ignored (the default was always used unless a user ++ * callback was installed). ++ * ++ * 'keep' is the value from the png_chunk_unknown_handling, the setting for ++ * this specific chunk_name, if PNG_HANDLE_AS_UNKNOWN_SUPPORTED, if not it ++ * will always be PNG_HANDLE_CHUNK_AS_DEFAULT and it needs to be set here. ++ * This is just an optimization to avoid multiple calls to the lookup ++ * function. ++ */ ++# ifndef PNG_HANDLE_AS_UNKNOWN_SUPPORTED ++# ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED ++ keep = png_chunk_unknown_handling(png_ptr, png_ptr->chunk_name); ++# endif ++# endif ++ ++ /* One of the following methods will read the chunk or skip it (at least one ++ * of these is always defined because this is the only way to switch on ++ * PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) ++ */ ++# ifdef PNG_READ_USER_CHUNKS_SUPPORTED ++ /* The user callback takes precedence over the chunk keep value, but the ++ * keep value is still required to validate a save of a critical chunk. ++ */ ++ if (png_ptr->read_user_chunk_fn != NULL) ++ { ++ if (png_cache_unknown_chunk(png_ptr, length) != 0) ++ { ++ /* Callback to user unknown chunk handler */ ++ int ret = (*(png_ptr->read_user_chunk_fn))(png_ptr, ++ &png_ptr->unknown_chunk); ++ ++ /* ret is: ++ * negative: An error occurred; png_chunk_error will be called. ++ * zero: The chunk was not handled, the chunk will be discarded ++ * unless png_set_keep_unknown_chunks has been used to set ++ * a 'keep' behavior for this particular chunk, in which ++ * case that will be used. A critical chunk will cause an ++ * error at this point unless it is to be saved. ++ * positive: The chunk was handled, libpng will ignore/discard it. ++ */ ++ if (ret < 0) ++ png_chunk_error(png_ptr, "error in user chunk"); ++ ++ else if (ret == 0) ++ { ++ /* If the keep value is 'default' or 'never' override it, but ++ * still error out on critical chunks unless the keep value is ++ * 'always' While this is weird it is the behavior in 1.4.12. ++ * A possible improvement would be to obey the value set for the ++ * chunk, but this would be an API change that would probably ++ * damage some applications. ++ * ++ * The png_app_warning below catches the case that matters, where ++ * the application has not set specific save or ignore for this ++ * chunk or global save or ignore. ++ */ ++ if (keep < PNG_HANDLE_CHUNK_IF_SAFE) ++ { ++# ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED ++ if (png_ptr->unknown_default < PNG_HANDLE_CHUNK_IF_SAFE) ++ { ++ png_chunk_warning(png_ptr, "Saving unknown chunk:"); ++ png_app_warning(png_ptr, ++ "forcing save of an unhandled chunk;" ++ " please call png_set_keep_unknown_chunks"); ++ /* with keep = PNG_HANDLE_CHUNK_IF_SAFE */ ++ } ++# endif ++ keep = PNG_HANDLE_CHUNK_IF_SAFE; ++ } ++ } ++ ++ else /* chunk was handled */ ++ { ++ handled = 1; ++ /* Critical chunks can be safely discarded at this point. */ ++ keep = PNG_HANDLE_CHUNK_NEVER; ++ } ++ } ++ ++ else ++ keep = PNG_HANDLE_CHUNK_NEVER; /* insufficient memory */ ++ } ++ ++ else ++ /* Use the SAVE_UNKNOWN_CHUNKS code or skip the chunk */ ++# endif /* READ_USER_CHUNKS */ ++ ++# ifdef PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED ++ { ++ /* keep is currently just the per-chunk setting, if there was no ++ * setting change it to the global default now (not that this may ++ * still be AS_DEFAULT) then obtain the cache of the chunk if required, ++ * if not simply skip the chunk. ++ */ ++ if (keep == PNG_HANDLE_CHUNK_AS_DEFAULT) ++ keep = png_ptr->unknown_default; ++ ++ if (keep == PNG_HANDLE_CHUNK_ALWAYS || ++ (keep == PNG_HANDLE_CHUNK_IF_SAFE && ++ PNG_CHUNK_ANCILLARY(png_ptr->chunk_name))) ++ { ++ if (png_cache_unknown_chunk(png_ptr, length) == 0) ++ keep = PNG_HANDLE_CHUNK_NEVER; ++ } ++ ++ else ++ png_crc_finish(png_ptr, length); ++ } ++# else ++# ifndef PNG_READ_USER_CHUNKS_SUPPORTED ++# error no method to support READ_UNKNOWN_CHUNKS ++# endif ++ ++ { ++ /* If here there is no read callback pointer set and no support is ++ * compiled in to just save the unknown chunks, so simply skip this ++ * chunk. If 'keep' is something other than AS_DEFAULT or NEVER then ++ * the app has erroneously asked for unknown chunk saving when there ++ * is no support. ++ */ ++ if (keep > PNG_HANDLE_CHUNK_NEVER) ++ png_app_error(png_ptr, "no unknown chunk support available"); ++ ++ png_crc_finish(png_ptr, length); ++ } ++# endif ++ ++# ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED ++ /* Now store the chunk in the chunk list if appropriate, and if the limits ++ * permit it. ++ */ ++ if (keep == PNG_HANDLE_CHUNK_ALWAYS || ++ (keep == PNG_HANDLE_CHUNK_IF_SAFE && ++ PNG_CHUNK_ANCILLARY(png_ptr->chunk_name))) ++ { ++# ifdef PNG_USER_LIMITS_SUPPORTED ++ switch (png_ptr->user_chunk_cache_max) ++ { ++ case 2: ++ png_ptr->user_chunk_cache_max = 1; ++ png_chunk_benign_error(png_ptr, "no space in chunk cache"); ++ /* FALLTHROUGH */ ++ case 1: ++ /* NOTE: prior to 1.6.0 this case resulted in an unknown critical ++ * chunk being skipped, now there will be a hard error below. ++ */ ++ break; ++ ++ default: /* not at limit */ ++ --(png_ptr->user_chunk_cache_max); ++ /* FALLTHROUGH */ ++ case 0: /* no limit */ ++# endif /* USER_LIMITS */ ++ /* Here when the limit isn't reached or when limits are compiled ++ * out; store the chunk. ++ */ ++ png_set_unknown_chunks(png_ptr, info_ptr, ++ &png_ptr->unknown_chunk, 1); ++ handled = 1; ++# ifdef PNG_USER_LIMITS_SUPPORTED ++ break; ++ } ++# endif ++ } ++# else /* no store support: the chunk must be handled by the user callback */ ++ PNG_UNUSED(info_ptr) ++# endif ++ ++ /* Regardless of the error handling below the cached data (if any) can be ++ * freed now. Notice that the data is not freed if there is a png_error, but ++ * it will be freed by destroy_read_struct. ++ */ ++ if (png_ptr->unknown_chunk.data != NULL) ++ png_free(png_ptr, png_ptr->unknown_chunk.data); ++ png_ptr->unknown_chunk.data = NULL; ++ ++#else /* !PNG_READ_UNKNOWN_CHUNKS_SUPPORTED */ ++ /* There is no support to read an unknown chunk, so just skip it. */ ++ png_crc_finish(png_ptr, length); ++ PNG_UNUSED(info_ptr) ++ PNG_UNUSED(keep) ++#endif /* !READ_UNKNOWN_CHUNKS */ ++ ++ /* Check for unhandled critical chunks */ ++ if (handled == 0 && PNG_CHUNK_CRITICAL(png_ptr->chunk_name)) ++ png_chunk_error(png_ptr, "unhandled critical chunk"); ++} ++ ++/* This function is called to verify that a chunk name is valid. ++ * This function can't have the "critical chunk check" incorporated ++ * into it, since in the future we will need to be able to call user ++ * functions to handle unknown critical chunks after we check that ++ * the chunk name itself is valid. ++ */ ++ ++/* Bit hacking: the test for an invalid byte in the 4 byte chunk name is: ++ * ++ * ((c) < 65 || (c) > 122 || ((c) > 90 && (c) < 97)) ++ */ ++ ++void /* PRIVATE */ ++png_check_chunk_name(png_const_structrp png_ptr, png_uint_32 chunk_name) ++{ ++ int i; ++ png_uint_32 cn=chunk_name; ++ ++ png_debug(1, "in png_check_chunk_name"); ++ ++ for (i=1; i<=4; ++i) ++ { ++ int c = cn & 0xff; ++ ++ if (c < 65 || c > 122 || (c > 90 && c < 97)) ++ png_chunk_error(png_ptr, "invalid chunk type"); ++ ++ cn >>= 8; ++ } ++} ++ ++void /* PRIVATE */ ++png_check_chunk_length(png_const_structrp png_ptr, png_uint_32 length) ++{ ++ png_alloc_size_t limit = PNG_UINT_31_MAX; ++ ++# ifdef PNG_SET_USER_LIMITS_SUPPORTED ++ if (png_ptr->user_chunk_malloc_max > 0 && ++ png_ptr->user_chunk_malloc_max < limit) ++ limit = png_ptr->user_chunk_malloc_max; ++# elif PNG_USER_CHUNK_MALLOC_MAX > 0 ++ if (PNG_USER_CHUNK_MALLOC_MAX < limit) ++ limit = PNG_USER_CHUNK_MALLOC_MAX; ++# endif ++ if (png_ptr->chunk_name == png_IDAT) ++ { ++ png_alloc_size_t idat_limit = PNG_UINT_31_MAX; ++ size_t row_factor = ++ (size_t)png_ptr->width ++ * (size_t)png_ptr->channels ++ * (png_ptr->bit_depth > 8? 2: 1) ++ + 1 ++ + (png_ptr->interlaced? 6: 0); ++ if (png_ptr->height > PNG_UINT_32_MAX/row_factor) ++ idat_limit = PNG_UINT_31_MAX; ++ else ++ idat_limit = png_ptr->height * row_factor; ++ row_factor = row_factor > 32566? 32566 : row_factor; ++ idat_limit += 6 + 5*(idat_limit/row_factor+1); /* zlib+deflate overhead */ ++ idat_limit=idat_limit < PNG_UINT_31_MAX? idat_limit : PNG_UINT_31_MAX; ++ limit = limit < idat_limit? idat_limit : limit; ++ } ++ ++ if (length > limit) ++ { ++ png_debug2(0," length = %lu, limit = %lu", ++ (unsigned long)length,(unsigned long)limit); ++ png_chunk_error(png_ptr, "chunk data is too large"); ++ } ++} ++ ++/* Combines the row recently read in with the existing pixels in the row. This ++ * routine takes care of alpha and transparency if requested. This routine also ++ * handles the two methods of progressive display of interlaced images, ++ * depending on the 'display' value; if 'display' is true then the whole row ++ * (dp) is filled from the start by replicating the available pixels. If ++ * 'display' is false only those pixels present in the pass are filled in. ++ */ ++void /* PRIVATE */ ++png_combine_row(png_const_structrp png_ptr, png_bytep dp, int display) ++{ ++ unsigned int pixel_depth = png_ptr->transformed_pixel_depth; ++ png_const_bytep sp = png_ptr->row_buf + 1; ++ png_alloc_size_t row_width = png_ptr->width; ++ unsigned int pass = png_ptr->pass; ++ png_bytep end_ptr = 0; ++ png_byte end_byte = 0; ++ unsigned int end_mask; ++ ++ png_debug(1, "in png_combine_row"); ++ ++ /* Added in 1.5.6: it should not be possible to enter this routine until at ++ * least one row has been read from the PNG data and transformed. ++ */ ++ if (pixel_depth == 0) ++ png_error(png_ptr, "internal row logic error"); ++ ++ /* Added in 1.5.4: the pixel depth should match the information returned by ++ * any call to png_read_update_info at this point. Do not continue if we got ++ * this wrong. ++ */ ++ if (png_ptr->info_rowbytes != 0 && png_ptr->info_rowbytes != ++ PNG_ROWBYTES(pixel_depth, row_width)) ++ png_error(png_ptr, "internal row size calculation error"); ++ ++ /* Don't expect this to ever happen: */ ++ if (row_width == 0) ++ png_error(png_ptr, "internal row width error"); ++ ++ /* Preserve the last byte in cases where only part of it will be overwritten, ++ * the multiply below may overflow, we don't care because ANSI-C guarantees ++ * we get the low bits. ++ */ ++ end_mask = (pixel_depth * row_width) & 7; ++ if (end_mask != 0) ++ { ++ /* end_ptr == NULL is a flag to say do nothing */ ++ end_ptr = dp + PNG_ROWBYTES(pixel_depth, row_width) - 1; ++ end_byte = *end_ptr; ++# ifdef PNG_READ_PACKSWAP_SUPPORTED ++ if ((png_ptr->transformations & PNG_PACKSWAP) != 0) ++ /* little-endian byte */ ++ end_mask = (unsigned int)(0xff << end_mask); ++ ++ else /* big-endian byte */ ++# endif ++ end_mask = 0xff >> end_mask; ++ /* end_mask is now the bits to *keep* from the destination row */ ++ } ++ ++ /* For non-interlaced images this reduces to a memcpy(). A memcpy() ++ * will also happen if interlacing isn't supported or if the application ++ * does not call png_set_interlace_handling(). In the latter cases the ++ * caller just gets a sequence of the unexpanded rows from each interlace ++ * pass. ++ */ ++#ifdef PNG_READ_INTERLACING_SUPPORTED ++ if (png_ptr->interlaced != 0 && ++ (png_ptr->transformations & PNG_INTERLACE) != 0 && ++ pass < 6 && (display == 0 || ++ /* The following copies everything for 'display' on passes 0, 2 and 4. */ ++ (display == 1 && (pass & 1) != 0))) ++ { ++ /* Narrow images may have no bits in a pass; the caller should handle ++ * this, but this test is cheap: ++ */ ++ if (row_width <= PNG_PASS_START_COL(pass)) ++ return; ++ ++ if (pixel_depth < 8) ++ { ++ /* For pixel depths up to 4 bpp the 8-pixel mask can be expanded to fit ++ * into 32 bits, then a single loop over the bytes using the four byte ++ * values in the 32-bit mask can be used. For the 'display' option the ++ * expanded mask may also not require any masking within a byte. To ++ * make this work the PACKSWAP option must be taken into account - it ++ * simply requires the pixels to be reversed in each byte. ++ * ++ * The 'regular' case requires a mask for each of the first 6 passes, ++ * the 'display' case does a copy for the even passes in the range ++ * 0..6. This has already been handled in the test above. ++ * ++ * The masks are arranged as four bytes with the first byte to use in ++ * the lowest bits (little-endian) regardless of the order (PACKSWAP or ++ * not) of the pixels in each byte. ++ * ++ * NOTE: the whole of this logic depends on the caller of this function ++ * only calling it on rows appropriate to the pass. This function only ++ * understands the 'x' logic; the 'y' logic is handled by the caller. ++ * ++ * The following defines allow generation of compile time constant bit ++ * masks for each pixel depth and each possibility of swapped or not ++ * swapped bytes. Pass 'p' is in the range 0..6; 'x', a pixel index, ++ * is in the range 0..7; and the result is 1 if the pixel is to be ++ * copied in the pass, 0 if not. 'S' is for the sparkle method, 'B' ++ * for the block method. ++ * ++ * With some compilers a compile time expression of the general form: ++ * ++ * (shift >= 32) ? (a >> (shift-32)) : (b >> shift) ++ * ++ * Produces warnings with values of 'shift' in the range 33 to 63 ++ * because the right hand side of the ?: expression is evaluated by ++ * the compiler even though it isn't used. Microsoft Visual C (various ++ * versions) and the Intel C compiler are known to do this. To avoid ++ * this the following macros are used in 1.5.6. This is a temporary ++ * solution to avoid destabilizing the code during the release process. ++ */ ++# if PNG_USE_COMPILE_TIME_MASKS ++# define PNG_LSR(x,s) ((x)>>((s) & 0x1f)) ++# define PNG_LSL(x,s) ((x)<<((s) & 0x1f)) ++# else ++# define PNG_LSR(x,s) ((x)>>(s)) ++# define PNG_LSL(x,s) ((x)<<(s)) ++# endif ++# define S_COPY(p,x) (((p)<4 ? PNG_LSR(0x80088822,(3-(p))*8+(7-(x))) :\ ++ PNG_LSR(0xaa55ff00,(7-(p))*8+(7-(x)))) & 1) ++# define B_COPY(p,x) (((p)<4 ? PNG_LSR(0xff0fff33,(3-(p))*8+(7-(x))) :\ ++ PNG_LSR(0xff55ff00,(7-(p))*8+(7-(x)))) & 1) ++ ++ /* Return a mask for pass 'p' pixel 'x' at depth 'd'. The mask is ++ * little endian - the first pixel is at bit 0 - however the extra ++ * parameter 's' can be set to cause the mask position to be swapped ++ * within each byte, to match the PNG format. This is done by XOR of ++ * the shift with 7, 6 or 4 for bit depths 1, 2 and 4. ++ */ ++# define PIXEL_MASK(p,x,d,s) \ ++ (PNG_LSL(((PNG_LSL(1U,(d)))-1),(((x)*(d))^((s)?8-(d):0)))) ++ ++ /* Hence generate the appropriate 'block' or 'sparkle' pixel copy mask. ++ */ ++# define S_MASKx(p,x,d,s) (S_COPY(p,x)?PIXEL_MASK(p,x,d,s):0) ++# define B_MASKx(p,x,d,s) (B_COPY(p,x)?PIXEL_MASK(p,x,d,s):0) ++ ++ /* Combine 8 of these to get the full mask. For the 1-bpp and 2-bpp ++ * cases the result needs replicating, for the 4-bpp case the above ++ * generates a full 32 bits. ++ */ ++# define MASK_EXPAND(m,d) ((m)*((d)==1?0x01010101:((d)==2?0x00010001:1))) ++ ++# define S_MASK(p,d,s) MASK_EXPAND(S_MASKx(p,0,d,s) + S_MASKx(p,1,d,s) +\ ++ S_MASKx(p,2,d,s) + S_MASKx(p,3,d,s) + S_MASKx(p,4,d,s) +\ ++ S_MASKx(p,5,d,s) + S_MASKx(p,6,d,s) + S_MASKx(p,7,d,s), d) ++ ++# define B_MASK(p,d,s) MASK_EXPAND(B_MASKx(p,0,d,s) + B_MASKx(p,1,d,s) +\ ++ B_MASKx(p,2,d,s) + B_MASKx(p,3,d,s) + B_MASKx(p,4,d,s) +\ ++ B_MASKx(p,5,d,s) + B_MASKx(p,6,d,s) + B_MASKx(p,7,d,s), d) ++ ++#if PNG_USE_COMPILE_TIME_MASKS ++ /* Utility macros to construct all the masks for a depth/swap ++ * combination. The 's' parameter says whether the format is PNG ++ * (big endian bytes) or not. Only the three odd-numbered passes are ++ * required for the display/block algorithm. ++ */ ++# define S_MASKS(d,s) { S_MASK(0,d,s), S_MASK(1,d,s), S_MASK(2,d,s),\ ++ S_MASK(3,d,s), S_MASK(4,d,s), S_MASK(5,d,s) } ++ ++# define B_MASKS(d,s) { B_MASK(1,d,s), B_MASK(3,d,s), B_MASK(5,d,s) } ++ ++# define DEPTH_INDEX(d) ((d)==1?0:((d)==2?1:2)) ++ ++ /* Hence the pre-compiled masks indexed by PACKSWAP (or not), depth and ++ * then pass: ++ */ ++ static const png_uint_32 row_mask[2/*PACKSWAP*/][3/*depth*/][6] = ++ { ++ /* Little-endian byte masks for PACKSWAP */ ++ { S_MASKS(1,0), S_MASKS(2,0), S_MASKS(4,0) }, ++ /* Normal (big-endian byte) masks - PNG format */ ++ { S_MASKS(1,1), S_MASKS(2,1), S_MASKS(4,1) } ++ }; ++ ++ /* display_mask has only three entries for the odd passes, so index by ++ * pass>>1. ++ */ ++ static const png_uint_32 display_mask[2][3][3] = ++ { ++ /* Little-endian byte masks for PACKSWAP */ ++ { B_MASKS(1,0), B_MASKS(2,0), B_MASKS(4,0) }, ++ /* Normal (big-endian byte) masks - PNG format */ ++ { B_MASKS(1,1), B_MASKS(2,1), B_MASKS(4,1) } ++ }; ++ ++# define MASK(pass,depth,display,png)\ ++ ((display)?display_mask[png][DEPTH_INDEX(depth)][pass>>1]:\ ++ row_mask[png][DEPTH_INDEX(depth)][pass]) ++ ++#else /* !PNG_USE_COMPILE_TIME_MASKS */ ++ /* This is the runtime alternative: it seems unlikely that this will ++ * ever be either smaller or faster than the compile time approach. ++ */ ++# define MASK(pass,depth,display,png)\ ++ ((display)?B_MASK(pass,depth,png):S_MASK(pass,depth,png)) ++#endif /* !USE_COMPILE_TIME_MASKS */ ++ ++ /* Use the appropriate mask to copy the required bits. In some cases ++ * the byte mask will be 0 or 0xff; optimize these cases. row_width is ++ * the number of pixels, but the code copies bytes, so it is necessary ++ * to special case the end. ++ */ ++ png_uint_32 pixels_per_byte = 8 / pixel_depth; ++ png_uint_32 mask; ++ ++# ifdef PNG_READ_PACKSWAP_SUPPORTED ++ if ((png_ptr->transformations & PNG_PACKSWAP) != 0) ++ mask = MASK(pass, pixel_depth, display, 0); ++ ++ else ++# endif ++ mask = MASK(pass, pixel_depth, display, 1); ++ ++ for (;;) ++ { ++ png_uint_32 m; ++ ++ /* It doesn't matter in the following if png_uint_32 has more than ++ * 32 bits because the high bits always match those in m<<24; it is, ++ * however, essential to use OR here, not +, because of this. ++ */ ++ m = mask; ++ mask = (m >> 8) | (m << 24); /* rotate right to good compilers */ ++ m &= 0xff; ++ ++ if (m != 0) /* something to copy */ ++ { ++ if (m != 0xff) ++ *dp = (png_byte)((*dp & ~m) | (*sp & m)); ++ else ++ *dp = *sp; ++ } ++ ++ /* NOTE: this may overwrite the last byte with garbage if the image ++ * is not an exact number of bytes wide; libpng has always done ++ * this. ++ */ ++ if (row_width <= pixels_per_byte) ++ break; /* May need to restore part of the last byte */ ++ ++ row_width -= pixels_per_byte; ++ ++dp; ++ ++sp; ++ } ++ } ++ ++ else /* pixel_depth >= 8 */ ++ { ++ unsigned int bytes_to_copy, bytes_to_jump; ++ ++ /* Validate the depth - it must be a multiple of 8 */ ++ if (pixel_depth & 7) ++ png_error(png_ptr, "invalid user transform pixel depth"); ++ ++ pixel_depth >>= 3; /* now in bytes */ ++ row_width *= pixel_depth; ++ ++ /* Regardless of pass number the Adam 7 interlace always results in a ++ * fixed number of pixels to copy then to skip. There may be a ++ * different number of pixels to skip at the start though. ++ */ ++ { ++ unsigned int offset = PNG_PASS_START_COL(pass) * pixel_depth; ++ ++ row_width -= offset; ++ dp += offset; ++ sp += offset; ++ } ++ ++ /* Work out the bytes to copy. */ ++ if (display != 0) ++ { ++ /* When doing the 'block' algorithm the pixel in the pass gets ++ * replicated to adjacent pixels. This is why the even (0,2,4,6) ++ * passes are skipped above - the entire expanded row is copied. ++ */ ++ bytes_to_copy = (1<<((6-pass)>>1)) * pixel_depth; ++ ++ /* But don't allow this number to exceed the actual row width. */ ++ if (bytes_to_copy > row_width) ++ bytes_to_copy = (unsigned int)/*SAFE*/row_width; ++ } ++ ++ else /* normal row; Adam7 only ever gives us one pixel to copy. */ ++ bytes_to_copy = pixel_depth; ++ ++ /* In Adam7 there is a constant offset between where the pixels go. */ ++ bytes_to_jump = PNG_PASS_COL_OFFSET(pass) * pixel_depth; ++ ++ /* And simply copy these bytes. Some optimization is possible here, ++ * depending on the value of 'bytes_to_copy'. Special case the low ++ * byte counts, which we know to be frequent. ++ * ++ * Notice that these cases all 'return' rather than 'break' - this ++ * avoids an unnecessary test on whether to restore the last byte ++ * below. ++ */ ++ switch (bytes_to_copy) ++ { ++ case 1: ++ for (;;) ++ { ++ *dp = *sp; ++ ++ if (row_width <= bytes_to_jump) ++ return; ++ ++ dp += bytes_to_jump; ++ sp += bytes_to_jump; ++ row_width -= bytes_to_jump; ++ } ++ ++ case 2: ++ /* There is a possibility of a partial copy at the end here; this ++ * slows the code down somewhat. ++ */ ++ do ++ { ++ dp[0] = sp[0]; dp[1] = sp[1]; ++ ++ if (row_width <= bytes_to_jump) ++ return; ++ ++ sp += bytes_to_jump; ++ dp += bytes_to_jump; ++ row_width -= bytes_to_jump; ++ } ++ while (row_width > 1); ++ ++ /* And there can only be one byte left at this point: */ ++ *dp = *sp; ++ return; ++ ++ case 3: ++ /* This can only be the RGB case, so each copy is exactly one ++ * pixel and it is not necessary to check for a partial copy. ++ */ ++ for (;;) ++ { ++ dp[0] = sp[0]; dp[1] = sp[1]; dp[2] = sp[2]; ++ ++ if (row_width <= bytes_to_jump) ++ return; ++ ++ sp += bytes_to_jump; ++ dp += bytes_to_jump; ++ row_width -= bytes_to_jump; ++ } ++ ++ default: ++#if PNG_ALIGN_TYPE != PNG_ALIGN_NONE ++ /* Check for double byte alignment and, if possible, use a ++ * 16-bit copy. Don't attempt this for narrow images - ones that ++ * are less than an interlace panel wide. Don't attempt it for ++ * wide bytes_to_copy either - use the memcpy there. ++ */ ++ if (bytes_to_copy < 16 /*else use memcpy*/ && ++ png_isaligned(dp, png_uint_16) && ++ png_isaligned(sp, png_uint_16) && ++ bytes_to_copy % (sizeof (png_uint_16)) == 0 && ++ bytes_to_jump % (sizeof (png_uint_16)) == 0) ++ { ++ /* Everything is aligned for png_uint_16 copies, but try for ++ * png_uint_32 first. ++ */ ++ if (png_isaligned(dp, png_uint_32) && ++ png_isaligned(sp, png_uint_32) && ++ bytes_to_copy % (sizeof (png_uint_32)) == 0 && ++ bytes_to_jump % (sizeof (png_uint_32)) == 0) ++ { ++ png_uint_32p dp32 = png_aligncast(png_uint_32p,dp); ++ png_const_uint_32p sp32 = png_aligncastconst( ++ png_const_uint_32p, sp); ++ size_t skip = (bytes_to_jump-bytes_to_copy) / ++ (sizeof (png_uint_32)); ++ ++ do ++ { ++ size_t c = bytes_to_copy; ++ do ++ { ++ *dp32++ = *sp32++; ++ c -= (sizeof (png_uint_32)); ++ } ++ while (c > 0); ++ ++ if (row_width <= bytes_to_jump) ++ return; ++ ++ dp32 += skip; ++ sp32 += skip; ++ row_width -= bytes_to_jump; ++ } ++ while (bytes_to_copy <= row_width); ++ ++ /* Get to here when the row_width truncates the final copy. ++ * There will be 1-3 bytes left to copy, so don't try the ++ * 16-bit loop below. ++ */ ++ dp = (png_bytep)dp32; ++ sp = (png_const_bytep)sp32; ++ do ++ *dp++ = *sp++; ++ while (--row_width > 0); ++ return; ++ } ++ ++ /* Else do it in 16-bit quantities, but only if the size is ++ * not too large. ++ */ ++ else ++ { ++ png_uint_16p dp16 = png_aligncast(png_uint_16p, dp); ++ png_const_uint_16p sp16 = png_aligncastconst( ++ png_const_uint_16p, sp); ++ size_t skip = (bytes_to_jump-bytes_to_copy) / ++ (sizeof (png_uint_16)); ++ ++ do ++ { ++ size_t c = bytes_to_copy; ++ do ++ { ++ *dp16++ = *sp16++; ++ c -= (sizeof (png_uint_16)); ++ } ++ while (c > 0); ++ ++ if (row_width <= bytes_to_jump) ++ return; ++ ++ dp16 += skip; ++ sp16 += skip; ++ row_width -= bytes_to_jump; ++ } ++ while (bytes_to_copy <= row_width); ++ ++ /* End of row - 1 byte left, bytes_to_copy > row_width: */ ++ dp = (png_bytep)dp16; ++ sp = (png_const_bytep)sp16; ++ do ++ *dp++ = *sp++; ++ while (--row_width > 0); ++ return; ++ } ++ } ++#endif /* ALIGN_TYPE code */ ++ ++ /* The true default - use a memcpy: */ ++ for (;;) ++ { ++ memcpy(dp, sp, bytes_to_copy); ++ ++ if (row_width <= bytes_to_jump) ++ return; ++ ++ sp += bytes_to_jump; ++ dp += bytes_to_jump; ++ row_width -= bytes_to_jump; ++ if (bytes_to_copy > row_width) ++ bytes_to_copy = (unsigned int)/*SAFE*/row_width; ++ } ++ } ++ ++ /* NOT REACHED*/ ++ } /* pixel_depth >= 8 */ ++ ++ /* Here if pixel_depth < 8 to check 'end_ptr' below. */ ++ } ++ else ++#endif /* READ_INTERLACING */ ++ ++ /* If here then the switch above wasn't used so just memcpy the whole row ++ * from the temporary row buffer (notice that this overwrites the end of the ++ * destination row if it is a partial byte.) ++ */ ++ memcpy(dp, sp, PNG_ROWBYTES(pixel_depth, row_width)); ++ ++ /* Restore the overwritten bits from the last byte if necessary. */ ++ if (end_ptr != NULL) ++ *end_ptr = (png_byte)((end_byte & end_mask) | (*end_ptr & ~end_mask)); ++} ++ ++#ifdef PNG_READ_INTERLACING_SUPPORTED ++void /* PRIVATE */ ++png_do_read_interlace(png_row_infop row_info, png_bytep row, int pass, ++ png_uint_32 transformations /* Because these may affect the byte layout */) ++{ ++ /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ ++ /* Offset to next interlace block */ ++ static const unsigned int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; ++ ++ png_debug(1, "in png_do_read_interlace"); ++ if (row != NULL && row_info != NULL) ++ { ++ png_uint_32 final_width; ++ ++ final_width = row_info->width * png_pass_inc[pass]; ++ ++ switch (row_info->pixel_depth) ++ { ++ case 1: ++ { ++ png_bytep sp = row + (size_t)((row_info->width - 1) >> 3); ++ png_bytep dp = row + (size_t)((final_width - 1) >> 3); ++ unsigned int sshift, dshift; ++ unsigned int s_start, s_end; ++ int s_inc; ++ int jstop = (int)png_pass_inc[pass]; ++ png_byte v; ++ png_uint_32 i; ++ int j; ++ ++#ifdef PNG_READ_PACKSWAP_SUPPORTED ++ if ((transformations & PNG_PACKSWAP) != 0) ++ { ++ sshift = ((row_info->width + 7) & 0x07); ++ dshift = ((final_width + 7) & 0x07); ++ s_start = 7; ++ s_end = 0; ++ s_inc = -1; ++ } ++ ++ else ++#endif ++ { ++ sshift = 7 - ((row_info->width + 7) & 0x07); ++ dshift = 7 - ((final_width + 7) & 0x07); ++ s_start = 0; ++ s_end = 7; ++ s_inc = 1; ++ } ++ ++ for (i = 0; i < row_info->width; i++) ++ { ++ v = (png_byte)((*sp >> sshift) & 0x01); ++ for (j = 0; j < jstop; j++) ++ { ++ unsigned int tmp = *dp & (0x7f7f >> (7 - dshift)); ++ tmp |= (unsigned int)(v << dshift); ++ *dp = (png_byte)(tmp & 0xff); ++ ++ if (dshift == s_end) ++ { ++ dshift = s_start; ++ dp--; ++ } ++ ++ else ++ dshift = (unsigned int)((int)dshift + s_inc); ++ } ++ ++ if (sshift == s_end) ++ { ++ sshift = s_start; ++ sp--; ++ } ++ ++ else ++ sshift = (unsigned int)((int)sshift + s_inc); ++ } ++ break; ++ } ++ ++ case 2: ++ { ++ png_bytep sp = row + (png_uint_32)((row_info->width - 1) >> 2); ++ png_bytep dp = row + (png_uint_32)((final_width - 1) >> 2); ++ unsigned int sshift, dshift; ++ unsigned int s_start, s_end; ++ int s_inc; ++ int jstop = (int)png_pass_inc[pass]; ++ png_uint_32 i; ++ ++#ifdef PNG_READ_PACKSWAP_SUPPORTED ++ if ((transformations & PNG_PACKSWAP) != 0) ++ { ++ sshift = (((row_info->width + 3) & 0x03) << 1); ++ dshift = (((final_width + 3) & 0x03) << 1); ++ s_start = 6; ++ s_end = 0; ++ s_inc = -2; ++ } ++ ++ else ++#endif ++ { ++ sshift = ((3 - ((row_info->width + 3) & 0x03)) << 1); ++ dshift = ((3 - ((final_width + 3) & 0x03)) << 1); ++ s_start = 0; ++ s_end = 6; ++ s_inc = 2; ++ } ++ ++ for (i = 0; i < row_info->width; i++) ++ { ++ png_byte v; ++ int j; ++ ++ v = (png_byte)((*sp >> sshift) & 0x03); ++ for (j = 0; j < jstop; j++) ++ { ++ unsigned int tmp = *dp & (0x3f3f >> (6 - dshift)); ++ tmp |= (unsigned int)(v << dshift); ++ *dp = (png_byte)(tmp & 0xff); ++ ++ if (dshift == s_end) ++ { ++ dshift = s_start; ++ dp--; ++ } ++ ++ else ++ dshift = (unsigned int)((int)dshift + s_inc); ++ } ++ ++ if (sshift == s_end) ++ { ++ sshift = s_start; ++ sp--; ++ } ++ ++ else ++ sshift = (unsigned int)((int)sshift + s_inc); ++ } ++ break; ++ } ++ ++ case 4: ++ { ++ png_bytep sp = row + (size_t)((row_info->width - 1) >> 1); ++ png_bytep dp = row + (size_t)((final_width - 1) >> 1); ++ unsigned int sshift, dshift; ++ unsigned int s_start, s_end; ++ int s_inc; ++ png_uint_32 i; ++ int jstop = (int)png_pass_inc[pass]; ++ ++#ifdef PNG_READ_PACKSWAP_SUPPORTED ++ if ((transformations & PNG_PACKSWAP) != 0) ++ { ++ sshift = (((row_info->width + 1) & 0x01) << 2); ++ dshift = (((final_width + 1) & 0x01) << 2); ++ s_start = 4; ++ s_end = 0; ++ s_inc = -4; ++ } ++ ++ else ++#endif ++ { ++ sshift = ((1 - ((row_info->width + 1) & 0x01)) << 2); ++ dshift = ((1 - ((final_width + 1) & 0x01)) << 2); ++ s_start = 0; ++ s_end = 4; ++ s_inc = 4; ++ } ++ ++ for (i = 0; i < row_info->width; i++) ++ { ++ png_byte v = (png_byte)((*sp >> sshift) & 0x0f); ++ int j; ++ ++ for (j = 0; j < jstop; j++) ++ { ++ unsigned int tmp = *dp & (0xf0f >> (4 - dshift)); ++ tmp |= (unsigned int)(v << dshift); ++ *dp = (png_byte)(tmp & 0xff); ++ ++ if (dshift == s_end) ++ { ++ dshift = s_start; ++ dp--; ++ } ++ ++ else ++ dshift = (unsigned int)((int)dshift + s_inc); ++ } ++ ++ if (sshift == s_end) ++ { ++ sshift = s_start; ++ sp--; ++ } ++ ++ else ++ sshift = (unsigned int)((int)sshift + s_inc); ++ } ++ break; ++ } ++ ++ default: ++ { ++ size_t pixel_bytes = (row_info->pixel_depth >> 3); ++ ++ png_bytep sp = row + (size_t)(row_info->width - 1) ++ * pixel_bytes; ++ ++ png_bytep dp = row + (size_t)(final_width - 1) * pixel_bytes; ++ ++ int jstop = (int)png_pass_inc[pass]; ++ png_uint_32 i; ++ ++ for (i = 0; i < row_info->width; i++) ++ { ++ png_byte v[8]; /* SAFE; pixel_depth does not exceed 64 */ ++ int j; ++ ++ memcpy(v, sp, pixel_bytes); ++ ++ for (j = 0; j < jstop; j++) ++ { ++ memcpy(dp, v, pixel_bytes); ++ dp -= pixel_bytes; ++ } ++ ++ sp -= pixel_bytes; ++ } ++ break; ++ } ++ } ++ ++ row_info->width = final_width; ++ row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, final_width); ++ } ++#ifndef PNG_READ_PACKSWAP_SUPPORTED ++ PNG_UNUSED(transformations) /* Silence compiler warning */ ++#endif ++} ++#endif /* READ_INTERLACING */ ++ ++static void ++png_read_filter_row_sub(png_row_infop row_info, png_bytep row, ++ png_const_bytep prev_row) ++{ ++ size_t i; ++ size_t istop = row_info->rowbytes; ++ unsigned int bpp = (row_info->pixel_depth + 7) >> 3; ++ png_bytep rp = row + bpp; ++ ++ PNG_UNUSED(prev_row) ++ ++ for (i = bpp; i < istop; i++) ++ { ++ *rp = (png_byte)(((int)(*rp) + (int)(*(rp-bpp))) & 0xff); ++ rp++; ++ } ++} ++ ++static void ++png_read_filter_row_up(png_row_infop row_info, png_bytep row, ++ png_const_bytep prev_row) ++{ ++ size_t i; ++ size_t istop = row_info->rowbytes; ++ png_bytep rp = row; ++ png_const_bytep pp = prev_row; ++ ++ for (i = 0; i < istop; i++) ++ { ++ *rp = (png_byte)(((int)(*rp) + (int)(*pp++)) & 0xff); ++ rp++; ++ } ++} ++ ++static void ++png_read_filter_row_avg(png_row_infop row_info, png_bytep row, ++ png_const_bytep prev_row) ++{ ++ size_t i; ++ png_bytep rp = row; ++ png_const_bytep pp = prev_row; ++ unsigned int bpp = (row_info->pixel_depth + 7) >> 3; ++ size_t istop = row_info->rowbytes - bpp; ++ ++ for (i = 0; i < bpp; i++) ++ { ++ *rp = (png_byte)(((int)(*rp) + ++ ((int)(*pp++) / 2 )) & 0xff); ++ ++ rp++; ++ } ++ ++ for (i = 0; i < istop; i++) ++ { ++ *rp = (png_byte)(((int)(*rp) + ++ (int)(*pp++ + *(rp-bpp)) / 2 ) & 0xff); ++ ++ rp++; ++ } ++} ++ ++static void ++png_read_filter_row_paeth_1byte_pixel(png_row_infop row_info, png_bytep row, ++ png_const_bytep prev_row) ++{ ++ png_bytep rp_end = row + row_info->rowbytes; ++ int a, c; ++ ++ /* First pixel/byte */ ++ c = *prev_row++; ++ a = *row + c; ++ *row++ = (png_byte)a; ++ ++ /* Remainder */ ++ while (row < rp_end) ++ { ++ int b, pa, pb, pc, p; ++ ++ a &= 0xff; /* From previous iteration or start */ ++ b = *prev_row++; ++ ++ p = b - c; ++ pc = a - c; ++ ++#ifdef PNG_USE_ABS ++ pa = abs(p); ++ pb = abs(pc); ++ pc = abs(p + pc); ++#else ++ pa = p < 0 ? -p : p; ++ pb = pc < 0 ? -pc : pc; ++ pc = (p + pc) < 0 ? -(p + pc) : p + pc; ++#endif ++ ++ /* Find the best predictor, the least of pa, pb, pc favoring the earlier ++ * ones in the case of a tie. ++ */ ++ if (pb < pa) ++ { ++ pa = pb; a = b; ++ } ++ if (pc < pa) a = c; ++ ++ /* Calculate the current pixel in a, and move the previous row pixel to c ++ * for the next time round the loop ++ */ ++ c = b; ++ a += *row; ++ *row++ = (png_byte)a; ++ } ++} ++ ++static void ++png_read_filter_row_paeth_multibyte_pixel(png_row_infop row_info, png_bytep row, ++ png_const_bytep prev_row) ++{ ++ unsigned int bpp = (row_info->pixel_depth + 7) >> 3; ++ png_bytep rp_end = row + bpp; ++ ++ /* Process the first pixel in the row completely (this is the same as 'up' ++ * because there is only one candidate predictor for the first row). ++ */ ++ while (row < rp_end) ++ { ++ int a = *row + *prev_row++; ++ *row++ = (png_byte)a; ++ } ++ ++ /* Remainder */ ++ rp_end = rp_end + (row_info->rowbytes - bpp); ++ ++ while (row < rp_end) ++ { ++ int a, b, c, pa, pb, pc, p; ++ ++ c = *(prev_row - bpp); ++ a = *(row - bpp); ++ b = *prev_row++; ++ ++ p = b - c; ++ pc = a - c; ++ ++#ifdef PNG_USE_ABS ++ pa = abs(p); ++ pb = abs(pc); ++ pc = abs(p + pc); ++#else ++ pa = p < 0 ? -p : p; ++ pb = pc < 0 ? -pc : pc; ++ pc = (p + pc) < 0 ? -(p + pc) : p + pc; ++#endif ++ ++ if (pb < pa) ++ { ++ pa = pb; a = b; ++ } ++ if (pc < pa) a = c; ++ ++ a += *row; ++ *row++ = (png_byte)a; ++ } ++} ++ ++static void ++png_init_filter_functions(png_structrp pp) ++ /* This function is called once for every PNG image (except for PNG images ++ * that only use PNG_FILTER_VALUE_NONE for all rows) to set the ++ * implementations required to reverse the filtering of PNG rows. Reversing ++ * the filter is the first transformation performed on the row data. It is ++ * performed in place, therefore an implementation can be selected based on ++ * the image pixel format. If the implementation depends on image width then ++ * take care to ensure that it works correctly if the image is interlaced - ++ * interlacing causes the actual row width to vary. ++ */ ++{ ++ unsigned int bpp = (pp->pixel_depth + 7) >> 3; ++ ++ pp->read_filter[PNG_FILTER_VALUE_SUB-1] = png_read_filter_row_sub; ++ pp->read_filter[PNG_FILTER_VALUE_UP-1] = png_read_filter_row_up; ++ pp->read_filter[PNG_FILTER_VALUE_AVG-1] = png_read_filter_row_avg; ++ if (bpp == 1) ++ pp->read_filter[PNG_FILTER_VALUE_PAETH-1] = ++ png_read_filter_row_paeth_1byte_pixel; ++ else ++ pp->read_filter[PNG_FILTER_VALUE_PAETH-1] = ++ png_read_filter_row_paeth_multibyte_pixel; ++ ++#ifdef PNG_FILTER_OPTIMIZATIONS ++ /* To use this define PNG_FILTER_OPTIMIZATIONS as the name of a function to ++ * call to install hardware optimizations for the above functions; simply ++ * replace whatever elements of the pp->read_filter[] array with a hardware ++ * specific (or, for that matter, generic) optimization. ++ * ++ * To see an example of this examine what configure.ac does when ++ * --enable-arm-neon is specified on the command line. ++ */ ++ PNG_FILTER_OPTIMIZATIONS(pp, bpp); ++#endif ++} ++ ++void /* PRIVATE */ ++png_read_filter_row(png_structrp pp, png_row_infop row_info, png_bytep row, ++ png_const_bytep prev_row, int filter) ++{ ++ /* OPTIMIZATION: DO NOT MODIFY THIS FUNCTION, instead #define ++ * PNG_FILTER_OPTIMIZATIONS to a function that overrides the generic ++ * implementations. See png_init_filter_functions above. ++ */ ++ if (filter > PNG_FILTER_VALUE_NONE && filter < PNG_FILTER_VALUE_LAST) ++ { ++ if (pp->read_filter[0] == NULL) ++ png_init_filter_functions(pp); ++ ++ pp->read_filter[filter-1](row_info, row, prev_row); ++ } ++} ++ ++#ifdef PNG_SEQUENTIAL_READ_SUPPORTED ++void /* PRIVATE */ ++png_read_IDAT_data(png_structrp png_ptr, png_bytep output, ++ png_alloc_size_t avail_out) ++{ ++ /* Loop reading IDATs and decompressing the result into output[avail_out] */ ++ png_ptr->zstream.next_out = output; ++ png_ptr->zstream.avail_out = 0; /* safety: set below */ ++ ++ if (output == NULL) ++ avail_out = 0; ++ ++ do ++ { ++ int ret; ++ png_byte tmpbuf[PNG_INFLATE_BUF_SIZE]; ++ ++ if (png_ptr->zstream.avail_in == 0) ++ { ++ uInt avail_in; ++ png_bytep buffer; ++ ++ while (png_ptr->idat_size == 0) ++ { ++ png_crc_finish(png_ptr, 0); ++ ++ png_ptr->idat_size = png_read_chunk_header(png_ptr); ++ /* This is an error even in the 'check' case because the code just ++ * consumed a non-IDAT header. ++ */ ++ if (png_ptr->chunk_name != png_IDAT) ++ png_error(png_ptr, "Not enough image data"); ++ } ++ ++ avail_in = png_ptr->IDAT_read_size; ++ ++ if (avail_in > png_ptr->idat_size) ++ avail_in = (uInt)png_ptr->idat_size; ++ ++ /* A PNG with a gradually increasing IDAT size will defeat this attempt ++ * to minimize memory usage by causing lots of re-allocs, but ++ * realistically doing IDAT_read_size re-allocs is not likely to be a ++ * big problem. ++ */ ++ buffer = png_read_buffer(png_ptr, avail_in, 0/*error*/); ++ ++ png_crc_read(png_ptr, buffer, avail_in); ++ png_ptr->idat_size -= avail_in; ++ ++ png_ptr->zstream.next_in = buffer; ++ png_ptr->zstream.avail_in = avail_in; ++ } ++ ++ /* And set up the output side. */ ++ if (output != NULL) /* standard read */ ++ { ++ uInt out = ZLIB_IO_MAX; ++ ++ if (out > avail_out) ++ out = (uInt)avail_out; ++ ++ avail_out -= out; ++ png_ptr->zstream.avail_out = out; ++ } ++ ++ else /* after last row, checking for end */ ++ { ++ png_ptr->zstream.next_out = tmpbuf; ++ png_ptr->zstream.avail_out = (sizeof tmpbuf); ++ } ++ ++ /* Use NO_FLUSH; this gives zlib the maximum opportunity to optimize the ++ * process. If the LZ stream is truncated the sequential reader will ++ * terminally damage the stream, above, by reading the chunk header of the ++ * following chunk (it then exits with png_error). ++ * ++ * TODO: deal more elegantly with truncated IDAT lists. ++ */ ++ ret = PNG_INFLATE(png_ptr, Z_NO_FLUSH); ++ ++ /* Take the unconsumed output back. */ ++ if (output != NULL) ++ avail_out += png_ptr->zstream.avail_out; ++ ++ else /* avail_out counts the extra bytes */ ++ avail_out += (sizeof tmpbuf) - png_ptr->zstream.avail_out; ++ ++ png_ptr->zstream.avail_out = 0; ++ ++ if (ret == Z_STREAM_END) ++ { ++ /* Do this for safety; we won't read any more into this row. */ ++ png_ptr->zstream.next_out = NULL; ++ ++ png_ptr->mode |= PNG_AFTER_IDAT; ++ png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED; ++ ++ if (png_ptr->zstream.avail_in > 0 || png_ptr->idat_size > 0) ++ png_chunk_benign_error(png_ptr, "Extra compressed data"); ++ break; ++ } ++ ++ if (ret != Z_OK) ++ { ++ png_zstream_error(png_ptr, ret); ++ ++ if (output != NULL) ++ png_chunk_error(png_ptr, png_ptr->zstream.msg); ++ ++ else /* checking */ ++ { ++ png_chunk_benign_error(png_ptr, png_ptr->zstream.msg); ++ return; ++ } ++ } ++ } while (avail_out > 0); ++ ++ if (avail_out > 0) ++ { ++ /* The stream ended before the image; this is the same as too few IDATs so ++ * should be handled the same way. ++ */ ++ if (output != NULL) ++ png_error(png_ptr, "Not enough image data"); ++ ++ else /* the deflate stream contained extra data */ ++ png_chunk_benign_error(png_ptr, "Too much image data"); ++ } ++} ++ ++void /* PRIVATE */ ++png_read_finish_IDAT(png_structrp png_ptr) ++{ ++ /* We don't need any more data and the stream should have ended, however the ++ * LZ end code may actually not have been processed. In this case we must ++ * read it otherwise stray unread IDAT data or, more likely, an IDAT chunk ++ * may still remain to be consumed. ++ */ ++ if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0) ++ { ++ /* The NULL causes png_read_IDAT_data to swallow any remaining bytes in ++ * the compressed stream, but the stream may be damaged too, so even after ++ * this call we may need to terminate the zstream ownership. ++ */ ++ png_read_IDAT_data(png_ptr, NULL, 0); ++ png_ptr->zstream.next_out = NULL; /* safety */ ++ ++ /* Now clear everything out for safety; the following may not have been ++ * done. ++ */ ++ if ((png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED) == 0) ++ { ++ png_ptr->mode |= PNG_AFTER_IDAT; ++ png_ptr->flags |= PNG_FLAG_ZSTREAM_ENDED; ++ } ++ } ++ ++ /* If the zstream has not been released do it now *and* terminate the reading ++ * of the final IDAT chunk. ++ */ ++ if (png_ptr->zowner == png_IDAT) ++ { ++ /* Always do this; the pointers otherwise point into the read buffer. */ ++ png_ptr->zstream.next_in = NULL; ++ png_ptr->zstream.avail_in = 0; ++ ++ /* Now we no longer own the zstream. */ ++ png_ptr->zowner = 0; ++ ++ /* The slightly weird semantics of the sequential IDAT reading is that we ++ * are always in or at the end of an IDAT chunk, so we always need to do a ++ * crc_finish here. If idat_size is non-zero we also need to read the ++ * spurious bytes at the end of the chunk now. ++ */ ++ (void)png_crc_finish(png_ptr, png_ptr->idat_size); ++ } ++} ++ ++void /* PRIVATE */ ++png_read_finish_row(png_structrp png_ptr) ++{ ++ /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ ++ ++ /* Start of interlace block */ ++ static const png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; ++ ++ /* Offset to next interlace block */ ++ static const png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; ++ ++ /* Start of interlace block in the y direction */ ++ static const png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; ++ ++ /* Offset to next interlace block in the y direction */ ++ static const png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; ++ ++ png_debug(1, "in png_read_finish_row"); ++ png_ptr->row_number++; ++ if (png_ptr->row_number < png_ptr->num_rows) ++ return; ++ ++ if (png_ptr->interlaced != 0) ++ { ++ png_ptr->row_number = 0; ++ ++ /* TO DO: don't do this if prev_row isn't needed (requires ++ * read-ahead of the next row's filter byte. ++ */ ++ memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1); ++ ++ do ++ { ++ png_ptr->pass++; ++ ++ if (png_ptr->pass >= 7) ++ break; ++ ++ png_ptr->iwidth = (png_ptr->width + ++ png_pass_inc[png_ptr->pass] - 1 - ++ png_pass_start[png_ptr->pass]) / ++ png_pass_inc[png_ptr->pass]; ++ ++ if ((png_ptr->transformations & PNG_INTERLACE) == 0) ++ { ++ png_ptr->num_rows = (png_ptr->height + ++ png_pass_yinc[png_ptr->pass] - 1 - ++ png_pass_ystart[png_ptr->pass]) / ++ png_pass_yinc[png_ptr->pass]; ++ } ++ ++ else /* if (png_ptr->transformations & PNG_INTERLACE) */ ++ break; /* libpng deinterlacing sees every row */ ++ ++ } while (png_ptr->num_rows == 0 || png_ptr->iwidth == 0); ++ ++ if (png_ptr->pass < 7) ++ return; ++ } ++ ++ /* Here after at the end of the last row of the last pass. */ ++ png_read_finish_IDAT(png_ptr); ++} ++#endif /* SEQUENTIAL_READ */ ++ ++void /* PRIVATE */ ++png_read_start_row(png_structrp png_ptr) ++{ ++ /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ ++ ++ /* Start of interlace block */ ++ static const png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; ++ ++ /* Offset to next interlace block */ ++ static const png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; ++ ++ /* Start of interlace block in the y direction */ ++ static const png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; ++ ++ /* Offset to next interlace block in the y direction */ ++ static const png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; ++ ++ unsigned int max_pixel_depth; ++ size_t row_bytes; ++ ++ png_debug(1, "in png_read_start_row"); ++ ++#ifdef PNG_READ_TRANSFORMS_SUPPORTED ++ png_init_read_transformations(png_ptr); ++#endif ++ if (png_ptr->interlaced != 0) ++ { ++ if ((png_ptr->transformations & PNG_INTERLACE) == 0) ++ png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 - ++ png_pass_ystart[0]) / png_pass_yinc[0]; ++ ++ else ++ png_ptr->num_rows = png_ptr->height; ++ ++ png_ptr->iwidth = (png_ptr->width + ++ png_pass_inc[png_ptr->pass] - 1 - ++ png_pass_start[png_ptr->pass]) / ++ png_pass_inc[png_ptr->pass]; ++ } ++ ++ else ++ { ++ png_ptr->num_rows = png_ptr->height; ++ png_ptr->iwidth = png_ptr->width; ++ } ++ ++ max_pixel_depth = (unsigned int)png_ptr->pixel_depth; ++ ++ /* WARNING: * png_read_transform_info (pngrtran.c) performs a simpler set of ++ * calculations to calculate the final pixel depth, then ++ * png_do_read_transforms actually does the transforms. This means that the ++ * code which effectively calculates this value is actually repeated in three ++ * separate places. They must all match. Innocent changes to the order of ++ * transformations can and will break libpng in a way that causes memory ++ * overwrites. ++ * ++ * TODO: fix this. ++ */ ++#ifdef PNG_READ_PACK_SUPPORTED ++ if ((png_ptr->transformations & PNG_PACK) != 0 && png_ptr->bit_depth < 8) ++ max_pixel_depth = 8; ++#endif ++ ++#ifdef PNG_READ_EXPAND_SUPPORTED ++ if ((png_ptr->transformations & PNG_EXPAND) != 0) ++ { ++ if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ++ { ++ if (png_ptr->num_trans != 0) ++ max_pixel_depth = 32; ++ ++ else ++ max_pixel_depth = 24; ++ } ++ ++ else if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) ++ { ++ if (max_pixel_depth < 8) ++ max_pixel_depth = 8; ++ ++ if (png_ptr->num_trans != 0) ++ max_pixel_depth *= 2; ++ } ++ ++ else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB) ++ { ++ if (png_ptr->num_trans != 0) ++ { ++ max_pixel_depth *= 4; ++ max_pixel_depth /= 3; ++ } ++ } ++ } ++#endif ++ ++#ifdef PNG_READ_EXPAND_16_SUPPORTED ++ if ((png_ptr->transformations & PNG_EXPAND_16) != 0) ++ { ++# ifdef PNG_READ_EXPAND_SUPPORTED ++ /* In fact it is an error if it isn't supported, but checking is ++ * the safe way. ++ */ ++ if ((png_ptr->transformations & PNG_EXPAND) != 0) ++ { ++ if (png_ptr->bit_depth < 16) ++ max_pixel_depth *= 2; ++ } ++ else ++# endif ++ png_ptr->transformations &= ~PNG_EXPAND_16; ++ } ++#endif ++ ++#ifdef PNG_READ_FILLER_SUPPORTED ++ if ((png_ptr->transformations & (PNG_FILLER)) != 0) ++ { ++ if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY) ++ { ++ if (max_pixel_depth <= 8) ++ max_pixel_depth = 16; ++ ++ else ++ max_pixel_depth = 32; ++ } ++ ++ else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB || ++ png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ++ { ++ if (max_pixel_depth <= 32) ++ max_pixel_depth = 32; ++ ++ else ++ max_pixel_depth = 64; ++ } ++ } ++#endif ++ ++#ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED ++ if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0) ++ { ++ if ( ++#ifdef PNG_READ_EXPAND_SUPPORTED ++ (png_ptr->num_trans != 0 && ++ (png_ptr->transformations & PNG_EXPAND) != 0) || ++#endif ++#ifdef PNG_READ_FILLER_SUPPORTED ++ (png_ptr->transformations & (PNG_FILLER)) != 0 || ++#endif ++ png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ++ { ++ if (max_pixel_depth <= 16) ++ max_pixel_depth = 32; ++ ++ else ++ max_pixel_depth = 64; ++ } ++ ++ else ++ { ++ if (max_pixel_depth <= 8) ++ { ++ if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA) ++ max_pixel_depth = 32; ++ ++ else ++ max_pixel_depth = 24; ++ } ++ ++ else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA) ++ max_pixel_depth = 64; ++ ++ else ++ max_pixel_depth = 48; ++ } ++ } ++#endif ++ ++#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) && \ ++defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) ++ if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0) ++ { ++ unsigned int user_pixel_depth = png_ptr->user_transform_depth * ++ png_ptr->user_transform_channels; ++ ++ if (user_pixel_depth > max_pixel_depth) ++ max_pixel_depth = user_pixel_depth; ++ } ++#endif ++ ++ /* This value is stored in png_struct and double checked in the row read ++ * code. ++ */ ++ png_ptr->maximum_pixel_depth = (png_byte)max_pixel_depth; ++ png_ptr->transformed_pixel_depth = 0; /* calculated on demand */ ++ ++ /* Align the width on the next larger 8 pixels. Mainly used ++ * for interlacing ++ */ ++ row_bytes = ((png_ptr->width + 7) & ~((png_uint_32)7)); ++ /* Calculate the maximum bytes needed, adding a byte and a pixel ++ * for safety's sake ++ */ ++ row_bytes = PNG_ROWBYTES(max_pixel_depth, row_bytes) + ++ 1 + ((max_pixel_depth + 7) >> 3U); ++ ++#ifdef PNG_MAX_MALLOC_64K ++ if (row_bytes > (png_uint_32)65536L) ++ png_error(png_ptr, "This image requires a row greater than 64KB"); ++#endif ++ ++ if (row_bytes + 48 > png_ptr->old_big_row_buf_size) ++ { ++ png_free(png_ptr, png_ptr->big_row_buf); ++ png_free(png_ptr, png_ptr->big_prev_row); ++ ++ if (png_ptr->interlaced != 0) ++ png_ptr->big_row_buf = (png_bytep)png_calloc(png_ptr, ++ row_bytes + 48); ++ ++ else ++ png_ptr->big_row_buf = (png_bytep)png_malloc(png_ptr, row_bytes + 48); ++ ++ png_ptr->big_prev_row = (png_bytep)png_malloc(png_ptr, row_bytes + 48); ++ ++#ifdef PNG_ALIGNED_MEMORY_SUPPORTED ++ /* Use 16-byte aligned memory for row_buf with at least 16 bytes ++ * of padding before and after row_buf; treat prev_row similarly. ++ * NOTE: the alignment is to the start of the pixels, one beyond the start ++ * of the buffer, because of the filter byte. Prior to libpng 1.5.6 this ++ * was incorrect; the filter byte was aligned, which had the exact ++ * opposite effect of that intended. ++ */ ++ { ++ png_bytep temp = png_ptr->big_row_buf + 32; ++ int extra = (int)((temp - (png_bytep)0) & 0x0f); ++ png_ptr->row_buf = temp - extra - 1/*filter byte*/; ++ ++ temp = png_ptr->big_prev_row + 32; ++ extra = (int)((temp - (png_bytep)0) & 0x0f); ++ png_ptr->prev_row = temp - extra - 1/*filter byte*/; ++ } ++ ++#else ++ /* Use 31 bytes of padding before and 17 bytes after row_buf. */ ++ png_ptr->row_buf = png_ptr->big_row_buf + 31; ++ png_ptr->prev_row = png_ptr->big_prev_row + 31; ++#endif ++ png_ptr->old_big_row_buf_size = row_bytes + 48; ++ } ++ ++#ifdef PNG_MAX_MALLOC_64K ++ if (png_ptr->rowbytes > 65535) ++ png_error(png_ptr, "This image requires a row greater than 64KB"); ++ ++#endif ++ if (png_ptr->rowbytes > (PNG_SIZE_MAX - 1)) ++ png_error(png_ptr, "Row has too many bytes to allocate in memory"); ++ ++ memset(png_ptr->prev_row, 0, png_ptr->rowbytes + 1); ++ ++ png_debug1(3, "width = %u,", png_ptr->width); ++ png_debug1(3, "height = %u,", png_ptr->height); ++ png_debug1(3, "iwidth = %u,", png_ptr->iwidth); ++ png_debug1(3, "num_rows = %u,", png_ptr->num_rows); ++ png_debug1(3, "rowbytes = %lu,", (unsigned long)png_ptr->rowbytes); ++ png_debug1(3, "irowbytes = %lu", ++ (unsigned long)PNG_ROWBYTES(png_ptr->pixel_depth, png_ptr->iwidth) + 1); ++ ++ /* The sequential reader needs a buffer for IDAT, but the progressive reader ++ * does not, so free the read buffer now regardless; the sequential reader ++ * reallocates it on demand. ++ */ ++ if (png_ptr->read_buffer != NULL) ++ { ++ png_bytep buffer = png_ptr->read_buffer; ++ ++ png_ptr->read_buffer_size = 0; ++ png_ptr->read_buffer = NULL; ++ png_free(png_ptr, buffer); ++ } ++ ++ /* Finally claim the zstream for the inflate of the IDAT data, use the bits ++ * value from the stream (note that this will result in a fatal error if the ++ * IDAT stream has a bogus deflate header window_bits value, but this should ++ * not be happening any longer!) ++ */ ++ if (png_inflate_claim(png_ptr, png_IDAT) != Z_OK) ++ png_error(png_ptr, png_ptr->zstream.msg); ++ ++ png_ptr->flags |= PNG_FLAG_ROW_INIT; ++} ++#endif /* READ */ +diff --git a/lib/libpng/pngset.c b/lib/libpng/pngset.c +new file mode 100644 +index 000000000..ec75dbe36 +--- /dev/null ++++ b/lib/libpng/pngset.c +@@ -0,0 +1,1802 @@ ++ ++/* pngset.c - storage of image information into info struct ++ * ++ * Copyright (c) 2018 Cosmin Truta ++ * Copyright (c) 1998-2018 Glenn Randers-Pehrson ++ * Copyright (c) 1996-1997 Andreas Dilger ++ * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc. ++ * ++ * This code is released under the libpng license. ++ * For conditions of distribution and use, see the disclaimer ++ * and license in png.h ++ * ++ * The functions here are used during reads to store data from the file ++ * into the info struct, and during writes to store application data ++ * into the info struct for writing into the file. This abstracts the ++ * info struct and allows us to change the structure in the future. ++ */ ++ ++#include "pngpriv.h" ++ ++#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) ++ ++#ifdef PNG_bKGD_SUPPORTED ++void PNGAPI ++png_set_bKGD(png_const_structrp png_ptr, png_inforp info_ptr, ++ png_const_color_16p background) ++{ ++ png_debug1(1, "in %s storage function", "bKGD"); ++ ++ if (png_ptr == NULL || info_ptr == NULL || background == NULL) ++ return; ++ ++ info_ptr->background = *background; ++ info_ptr->valid |= PNG_INFO_bKGD; ++} ++#endif ++ ++#ifdef PNG_cHRM_SUPPORTED ++void PNGFAPI ++png_set_cHRM_fixed(png_const_structrp png_ptr, png_inforp info_ptr, ++ png_fixed_point white_x, png_fixed_point white_y, png_fixed_point red_x, ++ png_fixed_point red_y, png_fixed_point green_x, png_fixed_point green_y, ++ png_fixed_point blue_x, png_fixed_point blue_y) ++{ ++ png_xy xy; ++ ++ png_debug1(1, "in %s storage function", "cHRM fixed"); ++ ++ if (png_ptr == NULL || info_ptr == NULL) ++ return; ++ ++ xy.redx = red_x; ++ xy.redy = red_y; ++ xy.greenx = green_x; ++ xy.greeny = green_y; ++ xy.bluex = blue_x; ++ xy.bluey = blue_y; ++ xy.whitex = white_x; ++ xy.whitey = white_y; ++ ++ if (png_colorspace_set_chromaticities(png_ptr, &info_ptr->colorspace, &xy, ++ 2/* override with app values*/) != 0) ++ info_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM; ++ ++ png_colorspace_sync_info(png_ptr, info_ptr); ++} ++ ++void PNGFAPI ++png_set_cHRM_XYZ_fixed(png_const_structrp png_ptr, png_inforp info_ptr, ++ png_fixed_point int_red_X, png_fixed_point int_red_Y, ++ png_fixed_point int_red_Z, png_fixed_point int_green_X, ++ png_fixed_point int_green_Y, png_fixed_point int_green_Z, ++ png_fixed_point int_blue_X, png_fixed_point int_blue_Y, ++ png_fixed_point int_blue_Z) ++{ ++ png_XYZ XYZ; ++ ++ png_debug1(1, "in %s storage function", "cHRM XYZ fixed"); ++ ++ if (png_ptr == NULL || info_ptr == NULL) ++ return; ++ ++ XYZ.red_X = int_red_X; ++ XYZ.red_Y = int_red_Y; ++ XYZ.red_Z = int_red_Z; ++ XYZ.green_X = int_green_X; ++ XYZ.green_Y = int_green_Y; ++ XYZ.green_Z = int_green_Z; ++ XYZ.blue_X = int_blue_X; ++ XYZ.blue_Y = int_blue_Y; ++ XYZ.blue_Z = int_blue_Z; ++ ++ if (png_colorspace_set_endpoints(png_ptr, &info_ptr->colorspace, ++ &XYZ, 2) != 0) ++ info_ptr->colorspace.flags |= PNG_COLORSPACE_FROM_cHRM; ++ ++ png_colorspace_sync_info(png_ptr, info_ptr); ++} ++ ++# ifdef PNG_FLOATING_POINT_SUPPORTED ++void PNGAPI ++png_set_cHRM(png_const_structrp png_ptr, png_inforp info_ptr, ++ double white_x, double white_y, double red_x, double red_y, ++ double green_x, double green_y, double blue_x, double blue_y) ++{ ++ png_set_cHRM_fixed(png_ptr, info_ptr, ++ png_fixed(png_ptr, white_x, "cHRM White X"), ++ png_fixed(png_ptr, white_y, "cHRM White Y"), ++ png_fixed(png_ptr, red_x, "cHRM Red X"), ++ png_fixed(png_ptr, red_y, "cHRM Red Y"), ++ png_fixed(png_ptr, green_x, "cHRM Green X"), ++ png_fixed(png_ptr, green_y, "cHRM Green Y"), ++ png_fixed(png_ptr, blue_x, "cHRM Blue X"), ++ png_fixed(png_ptr, blue_y, "cHRM Blue Y")); ++} ++ ++void PNGAPI ++png_set_cHRM_XYZ(png_const_structrp png_ptr, png_inforp info_ptr, double red_X, ++ double red_Y, double red_Z, double green_X, double green_Y, double green_Z, ++ double blue_X, double blue_Y, double blue_Z) ++{ ++ png_set_cHRM_XYZ_fixed(png_ptr, info_ptr, ++ png_fixed(png_ptr, red_X, "cHRM Red X"), ++ png_fixed(png_ptr, red_Y, "cHRM Red Y"), ++ png_fixed(png_ptr, red_Z, "cHRM Red Z"), ++ png_fixed(png_ptr, green_X, "cHRM Green X"), ++ png_fixed(png_ptr, green_Y, "cHRM Green Y"), ++ png_fixed(png_ptr, green_Z, "cHRM Green Z"), ++ png_fixed(png_ptr, blue_X, "cHRM Blue X"), ++ png_fixed(png_ptr, blue_Y, "cHRM Blue Y"), ++ png_fixed(png_ptr, blue_Z, "cHRM Blue Z")); ++} ++# endif /* FLOATING_POINT */ ++ ++#endif /* cHRM */ ++ ++#ifdef PNG_eXIf_SUPPORTED ++void PNGAPI ++png_set_eXIf(png_const_structrp png_ptr, png_inforp info_ptr, ++ png_bytep eXIf_buf) ++{ ++ png_warning(png_ptr, "png_set_eXIf does not work; use png_set_eXIf_1"); ++ PNG_UNUSED(info_ptr) ++ PNG_UNUSED(eXIf_buf) ++} ++ ++void PNGAPI ++png_set_eXIf_1(png_const_structrp png_ptr, png_inforp info_ptr, ++ png_uint_32 num_exif, png_bytep eXIf_buf) ++{ ++ int i; ++ ++ png_debug1(1, "in %s storage function", "eXIf"); ++ ++ if (png_ptr == NULL || info_ptr == NULL) ++ return; ++ ++ if (info_ptr->exif) ++ { ++ png_free(png_ptr, info_ptr->exif); ++ info_ptr->exif = NULL; ++ } ++ ++ info_ptr->num_exif = num_exif; ++ ++ info_ptr->exif = png_voidcast(png_bytep, png_malloc_warn(png_ptr, ++ info_ptr->num_exif)); ++ ++ if (info_ptr->exif == NULL) ++ { ++ png_warning(png_ptr, "Insufficient memory for eXIf chunk data"); ++ return; ++ } ++ ++ info_ptr->free_me |= PNG_FREE_EXIF; ++ ++ for (i = 0; i < (int) info_ptr->num_exif; i++) ++ info_ptr->exif[i] = eXIf_buf[i]; ++ ++ info_ptr->valid |= PNG_INFO_eXIf; ++} ++#endif /* eXIf */ ++ ++#ifdef PNG_gAMA_SUPPORTED ++void PNGFAPI ++png_set_gAMA_fixed(png_const_structrp png_ptr, png_inforp info_ptr, ++ png_fixed_point file_gamma) ++{ ++ png_debug1(1, "in %s storage function", "gAMA"); ++ ++ if (png_ptr == NULL || info_ptr == NULL) ++ return; ++ ++ png_colorspace_set_gamma(png_ptr, &info_ptr->colorspace, file_gamma); ++ png_colorspace_sync_info(png_ptr, info_ptr); ++} ++ ++# ifdef PNG_FLOATING_POINT_SUPPORTED ++void PNGAPI ++png_set_gAMA(png_const_structrp png_ptr, png_inforp info_ptr, double file_gamma) ++{ ++ png_set_gAMA_fixed(png_ptr, info_ptr, png_fixed(png_ptr, file_gamma, ++ "png_set_gAMA")); ++} ++# endif ++#endif ++ ++#ifdef PNG_hIST_SUPPORTED ++void PNGAPI ++png_set_hIST(png_const_structrp png_ptr, png_inforp info_ptr, ++ png_const_uint_16p hist) ++{ ++ int i; ++ ++ png_debug1(1, "in %s storage function", "hIST"); ++ ++ if (png_ptr == NULL || info_ptr == NULL) ++ return; ++ ++ if (info_ptr->num_palette == 0 || info_ptr->num_palette ++ > PNG_MAX_PALETTE_LENGTH) ++ { ++ png_warning(png_ptr, ++ "Invalid palette size, hIST allocation skipped"); ++ ++ return; ++ } ++ ++ png_free_data(png_ptr, info_ptr, PNG_FREE_HIST, 0); ++ ++ /* Changed from info->num_palette to PNG_MAX_PALETTE_LENGTH in ++ * version 1.2.1 ++ */ ++ info_ptr->hist = png_voidcast(png_uint_16p, png_malloc_warn(png_ptr, ++ PNG_MAX_PALETTE_LENGTH * (sizeof (png_uint_16)))); ++ ++ if (info_ptr->hist == NULL) ++ { ++ png_warning(png_ptr, "Insufficient memory for hIST chunk data"); ++ ++ return; ++ } ++ ++ info_ptr->free_me |= PNG_FREE_HIST; ++ ++ for (i = 0; i < info_ptr->num_palette; i++) ++ info_ptr->hist[i] = hist[i]; ++ ++ info_ptr->valid |= PNG_INFO_hIST; ++} ++#endif ++ ++void PNGAPI ++png_set_IHDR(png_const_structrp png_ptr, png_inforp info_ptr, ++ png_uint_32 width, png_uint_32 height, int bit_depth, ++ int color_type, int interlace_type, int compression_type, ++ int filter_type) ++{ ++ png_debug1(1, "in %s storage function", "IHDR"); ++ ++ if (png_ptr == NULL || info_ptr == NULL) ++ return; ++ ++ info_ptr->width = width; ++ info_ptr->height = height; ++ info_ptr->bit_depth = (png_byte)bit_depth; ++ info_ptr->color_type = (png_byte)color_type; ++ info_ptr->compression_type = (png_byte)compression_type; ++ info_ptr->filter_type = (png_byte)filter_type; ++ info_ptr->interlace_type = (png_byte)interlace_type; ++ ++ png_check_IHDR (png_ptr, info_ptr->width, info_ptr->height, ++ info_ptr->bit_depth, info_ptr->color_type, info_ptr->interlace_type, ++ info_ptr->compression_type, info_ptr->filter_type); ++ ++ if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ++ info_ptr->channels = 1; ++ ++ else if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0) ++ info_ptr->channels = 3; ++ ++ else ++ info_ptr->channels = 1; ++ ++ if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0) ++ info_ptr->channels++; ++ ++ info_ptr->pixel_depth = (png_byte)(info_ptr->channels * info_ptr->bit_depth); ++ ++ info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, width); ++} ++ ++#ifdef PNG_oFFs_SUPPORTED ++void PNGAPI ++png_set_oFFs(png_const_structrp png_ptr, png_inforp info_ptr, ++ png_int_32 offset_x, png_int_32 offset_y, int unit_type) ++{ ++ png_debug1(1, "in %s storage function", "oFFs"); ++ ++ if (png_ptr == NULL || info_ptr == NULL) ++ return; ++ ++ info_ptr->x_offset = offset_x; ++ info_ptr->y_offset = offset_y; ++ info_ptr->offset_unit_type = (png_byte)unit_type; ++ info_ptr->valid |= PNG_INFO_oFFs; ++} ++#endif ++ ++#ifdef PNG_pCAL_SUPPORTED ++void PNGAPI ++png_set_pCAL(png_const_structrp png_ptr, png_inforp info_ptr, ++ png_const_charp purpose, png_int_32 X0, png_int_32 X1, int type, ++ int nparams, png_const_charp units, png_charpp params) ++{ ++ size_t length; ++ int i; ++ ++ png_debug1(1, "in %s storage function", "pCAL"); ++ ++ if (png_ptr == NULL || info_ptr == NULL || purpose == NULL || units == NULL ++ || (nparams > 0 && params == NULL)) ++ return; ++ ++ length = strlen(purpose) + 1; ++ png_debug1(3, "allocating purpose for info (%lu bytes)", ++ (unsigned long)length); ++ ++ /* TODO: validate format of calibration name and unit name */ ++ ++ /* Check that the type matches the specification. */ ++ if (type < 0 || type > 3) ++ { ++ png_chunk_report(png_ptr, "Invalid pCAL equation type", ++ PNG_CHUNK_WRITE_ERROR); ++ return; ++ } ++ ++ if (nparams < 0 || nparams > 255) ++ { ++ png_chunk_report(png_ptr, "Invalid pCAL parameter count", ++ PNG_CHUNK_WRITE_ERROR); ++ return; ++ } ++ ++ /* Validate params[nparams] */ ++ for (i=0; ipcal_purpose = png_voidcast(png_charp, ++ png_malloc_warn(png_ptr, length)); ++ ++ if (info_ptr->pcal_purpose == NULL) ++ { ++ png_chunk_report(png_ptr, "Insufficient memory for pCAL purpose", ++ PNG_CHUNK_WRITE_ERROR); ++ return; ++ } ++ ++ memcpy(info_ptr->pcal_purpose, purpose, length); ++ ++ png_debug(3, "storing X0, X1, type, and nparams in info"); ++ info_ptr->pcal_X0 = X0; ++ info_ptr->pcal_X1 = X1; ++ info_ptr->pcal_type = (png_byte)type; ++ info_ptr->pcal_nparams = (png_byte)nparams; ++ ++ length = strlen(units) + 1; ++ png_debug1(3, "allocating units for info (%lu bytes)", ++ (unsigned long)length); ++ ++ info_ptr->pcal_units = png_voidcast(png_charp, ++ png_malloc_warn(png_ptr, length)); ++ ++ if (info_ptr->pcal_units == NULL) ++ { ++ png_warning(png_ptr, "Insufficient memory for pCAL units"); ++ ++ return; ++ } ++ ++ memcpy(info_ptr->pcal_units, units, length); ++ ++ info_ptr->pcal_params = png_voidcast(png_charpp, png_malloc_warn(png_ptr, ++ (size_t)(((unsigned int)nparams + 1) * (sizeof (png_charp))))); ++ ++ if (info_ptr->pcal_params == NULL) ++ { ++ png_warning(png_ptr, "Insufficient memory for pCAL params"); ++ ++ return; ++ } ++ ++ memset(info_ptr->pcal_params, 0, ((unsigned int)nparams + 1) * ++ (sizeof (png_charp))); ++ ++ for (i = 0; i < nparams; i++) ++ { ++ length = strlen(params[i]) + 1; ++ png_debug2(3, "allocating parameter %d for info (%lu bytes)", i, ++ (unsigned long)length); ++ ++ info_ptr->pcal_params[i] = (png_charp)png_malloc_warn(png_ptr, length); ++ ++ if (info_ptr->pcal_params[i] == NULL) ++ { ++ png_warning(png_ptr, "Insufficient memory for pCAL parameter"); ++ ++ return; ++ } ++ ++ memcpy(info_ptr->pcal_params[i], params[i], length); ++ } ++ ++ info_ptr->valid |= PNG_INFO_pCAL; ++ info_ptr->free_me |= PNG_FREE_PCAL; ++} ++#endif ++ ++#ifdef PNG_sCAL_SUPPORTED ++void PNGAPI ++png_set_sCAL_s(png_const_structrp png_ptr, png_inforp info_ptr, ++ int unit, png_const_charp swidth, png_const_charp sheight) ++{ ++ size_t lengthw = 0, lengthh = 0; ++ ++ png_debug1(1, "in %s storage function", "sCAL"); ++ ++ if (png_ptr == NULL || info_ptr == NULL) ++ return; ++ ++ /* Double check the unit (should never get here with an invalid ++ * unit unless this is an API call.) ++ */ ++ if (unit != 1 && unit != 2) ++ png_error(png_ptr, "Invalid sCAL unit"); ++ ++ if (swidth == NULL || (lengthw = strlen(swidth)) == 0 || ++ swidth[0] == 45 /* '-' */ || !png_check_fp_string(swidth, lengthw)) ++ png_error(png_ptr, "Invalid sCAL width"); ++ ++ if (sheight == NULL || (lengthh = strlen(sheight)) == 0 || ++ sheight[0] == 45 /* '-' */ || !png_check_fp_string(sheight, lengthh)) ++ png_error(png_ptr, "Invalid sCAL height"); ++ ++ info_ptr->scal_unit = (png_byte)unit; ++ ++ ++lengthw; ++ ++ png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)lengthw); ++ ++ info_ptr->scal_s_width = png_voidcast(png_charp, ++ png_malloc_warn(png_ptr, lengthw)); ++ ++ if (info_ptr->scal_s_width == NULL) ++ { ++ png_warning(png_ptr, "Memory allocation failed while processing sCAL"); ++ ++ return; ++ } ++ ++ memcpy(info_ptr->scal_s_width, swidth, lengthw); ++ ++ ++lengthh; ++ ++ png_debug1(3, "allocating unit for info (%u bytes)", (unsigned int)lengthh); ++ ++ info_ptr->scal_s_height = png_voidcast(png_charp, ++ png_malloc_warn(png_ptr, lengthh)); ++ ++ if (info_ptr->scal_s_height == NULL) ++ { ++ png_free (png_ptr, info_ptr->scal_s_width); ++ info_ptr->scal_s_width = NULL; ++ ++ png_warning(png_ptr, "Memory allocation failed while processing sCAL"); ++ ++ return; ++ } ++ ++ memcpy(info_ptr->scal_s_height, sheight, lengthh); ++ ++ info_ptr->valid |= PNG_INFO_sCAL; ++ info_ptr->free_me |= PNG_FREE_SCAL; ++} ++ ++# ifdef PNG_FLOATING_POINT_SUPPORTED ++void PNGAPI ++png_set_sCAL(png_const_structrp png_ptr, png_inforp info_ptr, int unit, ++ double width, double height) ++{ ++ png_debug1(1, "in %s storage function", "sCAL"); ++ ++ /* Check the arguments. */ ++ if (width <= 0) ++ png_warning(png_ptr, "Invalid sCAL width ignored"); ++ ++ else if (height <= 0) ++ png_warning(png_ptr, "Invalid sCAL height ignored"); ++ ++ else ++ { ++ /* Convert 'width' and 'height' to ASCII. */ ++ char swidth[PNG_sCAL_MAX_DIGITS+1]; ++ char sheight[PNG_sCAL_MAX_DIGITS+1]; ++ ++ png_ascii_from_fp(png_ptr, swidth, (sizeof swidth), width, ++ PNG_sCAL_PRECISION); ++ png_ascii_from_fp(png_ptr, sheight, (sizeof sheight), height, ++ PNG_sCAL_PRECISION); ++ ++ png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight); ++ } ++} ++# endif ++ ++# ifdef PNG_FIXED_POINT_SUPPORTED ++void PNGAPI ++png_set_sCAL_fixed(png_const_structrp png_ptr, png_inforp info_ptr, int unit, ++ png_fixed_point width, png_fixed_point height) ++{ ++ png_debug1(1, "in %s storage function", "sCAL"); ++ ++ /* Check the arguments. */ ++ if (width <= 0) ++ png_warning(png_ptr, "Invalid sCAL width ignored"); ++ ++ else if (height <= 0) ++ png_warning(png_ptr, "Invalid sCAL height ignored"); ++ ++ else ++ { ++ /* Convert 'width' and 'height' to ASCII. */ ++ char swidth[PNG_sCAL_MAX_DIGITS+1]; ++ char sheight[PNG_sCAL_MAX_DIGITS+1]; ++ ++ png_ascii_from_fixed(png_ptr, swidth, (sizeof swidth), width); ++ png_ascii_from_fixed(png_ptr, sheight, (sizeof sheight), height); ++ ++ png_set_sCAL_s(png_ptr, info_ptr, unit, swidth, sheight); ++ } ++} ++# endif ++#endif ++ ++#ifdef PNG_pHYs_SUPPORTED ++void PNGAPI ++png_set_pHYs(png_const_structrp png_ptr, png_inforp info_ptr, ++ png_uint_32 res_x, png_uint_32 res_y, int unit_type) ++{ ++ png_debug1(1, "in %s storage function", "pHYs"); ++ ++ if (png_ptr == NULL || info_ptr == NULL) ++ return; ++ ++ info_ptr->x_pixels_per_unit = res_x; ++ info_ptr->y_pixels_per_unit = res_y; ++ info_ptr->phys_unit_type = (png_byte)unit_type; ++ info_ptr->valid |= PNG_INFO_pHYs; ++} ++#endif ++ ++void PNGAPI ++png_set_PLTE(png_structrp png_ptr, png_inforp info_ptr, ++ png_const_colorp palette, int num_palette) ++{ ++ ++ png_uint_32 max_palette_length; ++ ++ png_debug1(1, "in %s storage function", "PLTE"); ++ ++ if (png_ptr == NULL || info_ptr == NULL) ++ return; ++ ++ max_palette_length = (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ? ++ (1 << info_ptr->bit_depth) : PNG_MAX_PALETTE_LENGTH; ++ ++ if (num_palette < 0 || num_palette > (int) max_palette_length) ++ { ++ if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ++ png_error(png_ptr, "Invalid palette length"); ++ ++ else ++ { ++ png_warning(png_ptr, "Invalid palette length"); ++ ++ return; ++ } ++ } ++ ++ if ((num_palette > 0 && palette == NULL) || ++ (num_palette == 0 ++# ifdef PNG_MNG_FEATURES_SUPPORTED ++ && (png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) == 0 ++# endif ++ )) ++ { ++ png_error(png_ptr, "Invalid palette"); ++ } ++ ++ /* It may not actually be necessary to set png_ptr->palette here; ++ * we do it for backward compatibility with the way the png_handle_tRNS ++ * function used to do the allocation. ++ * ++ * 1.6.0: the above statement appears to be incorrect; something has to set ++ * the palette inside png_struct on read. ++ */ ++ png_free_data(png_ptr, info_ptr, PNG_FREE_PLTE, 0); ++ ++ /* Changed in libpng-1.2.1 to allocate PNG_MAX_PALETTE_LENGTH instead ++ * of num_palette entries, in case of an invalid PNG file or incorrect ++ * call to png_set_PLTE() with too-large sample values. ++ */ ++ png_ptr->palette = png_voidcast(png_colorp, png_calloc(png_ptr, ++ PNG_MAX_PALETTE_LENGTH * (sizeof (png_color)))); ++ ++ if (num_palette > 0) ++ memcpy(png_ptr->palette, palette, (unsigned int)num_palette * ++ (sizeof (png_color))); ++ info_ptr->palette = png_ptr->palette; ++ info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette; ++ ++ info_ptr->free_me |= PNG_FREE_PLTE; ++ ++ info_ptr->valid |= PNG_INFO_PLTE; ++} ++ ++#ifdef PNG_sBIT_SUPPORTED ++void PNGAPI ++png_set_sBIT(png_const_structrp png_ptr, png_inforp info_ptr, ++ png_const_color_8p sig_bit) ++{ ++ png_debug1(1, "in %s storage function", "sBIT"); ++ ++ if (png_ptr == NULL || info_ptr == NULL || sig_bit == NULL) ++ return; ++ ++ info_ptr->sig_bit = *sig_bit; ++ info_ptr->valid |= PNG_INFO_sBIT; ++} ++#endif ++ ++#ifdef PNG_sRGB_SUPPORTED ++void PNGAPI ++png_set_sRGB(png_const_structrp png_ptr, png_inforp info_ptr, int srgb_intent) ++{ ++ png_debug1(1, "in %s storage function", "sRGB"); ++ ++ if (png_ptr == NULL || info_ptr == NULL) ++ return; ++ ++ (void)png_colorspace_set_sRGB(png_ptr, &info_ptr->colorspace, srgb_intent); ++ png_colorspace_sync_info(png_ptr, info_ptr); ++} ++ ++void PNGAPI ++png_set_sRGB_gAMA_and_cHRM(png_const_structrp png_ptr, png_inforp info_ptr, ++ int srgb_intent) ++{ ++ png_debug1(1, "in %s storage function", "sRGB_gAMA_and_cHRM"); ++ ++ if (png_ptr == NULL || info_ptr == NULL) ++ return; ++ ++ if (png_colorspace_set_sRGB(png_ptr, &info_ptr->colorspace, ++ srgb_intent) != 0) ++ { ++ /* This causes the gAMA and cHRM to be written too */ ++ info_ptr->colorspace.flags |= ++ PNG_COLORSPACE_FROM_gAMA|PNG_COLORSPACE_FROM_cHRM; ++ } ++ ++ png_colorspace_sync_info(png_ptr, info_ptr); ++} ++#endif /* sRGB */ ++ ++ ++#ifdef PNG_iCCP_SUPPORTED ++void PNGAPI ++png_set_iCCP(png_const_structrp png_ptr, png_inforp info_ptr, ++ png_const_charp name, int compression_type, ++ png_const_bytep profile, png_uint_32 proflen) ++{ ++ png_charp new_iccp_name; ++ png_bytep new_iccp_profile; ++ size_t length; ++ ++ png_debug1(1, "in %s storage function", "iCCP"); ++ ++ if (png_ptr == NULL || info_ptr == NULL || name == NULL || profile == NULL) ++ return; ++ ++ if (compression_type != PNG_COMPRESSION_TYPE_BASE) ++ png_app_error(png_ptr, "Invalid iCCP compression method"); ++ ++ /* Set the colorspace first because this validates the profile; do not ++ * override previously set app cHRM or gAMA here (because likely as not the ++ * application knows better than libpng what the correct values are.) Pass ++ * the info_ptr color_type field to png_colorspace_set_ICC because in the ++ * write case it has not yet been stored in png_ptr. ++ */ ++ { ++ int result = png_colorspace_set_ICC(png_ptr, &info_ptr->colorspace, name, ++ proflen, profile, info_ptr->color_type); ++ ++ png_colorspace_sync_info(png_ptr, info_ptr); ++ ++ /* Don't do any of the copying if the profile was bad, or inconsistent. */ ++ if (result == 0) ++ return; ++ ++ /* But do write the gAMA and cHRM chunks from the profile. */ ++ info_ptr->colorspace.flags |= ++ PNG_COLORSPACE_FROM_gAMA|PNG_COLORSPACE_FROM_cHRM; ++ } ++ ++ length = strlen(name)+1; ++ new_iccp_name = png_voidcast(png_charp, png_malloc_warn(png_ptr, length)); ++ ++ if (new_iccp_name == NULL) ++ { ++ png_benign_error(png_ptr, "Insufficient memory to process iCCP chunk"); ++ ++ return; ++ } ++ ++ memcpy(new_iccp_name, name, length); ++ new_iccp_profile = png_voidcast(png_bytep, ++ png_malloc_warn(png_ptr, proflen)); ++ ++ if (new_iccp_profile == NULL) ++ { ++ png_free(png_ptr, new_iccp_name); ++ png_benign_error(png_ptr, ++ "Insufficient memory to process iCCP profile"); ++ ++ return; ++ } ++ ++ memcpy(new_iccp_profile, profile, proflen); ++ ++ png_free_data(png_ptr, info_ptr, PNG_FREE_ICCP, 0); ++ ++ info_ptr->iccp_proflen = proflen; ++ info_ptr->iccp_name = new_iccp_name; ++ info_ptr->iccp_profile = new_iccp_profile; ++ info_ptr->free_me |= PNG_FREE_ICCP; ++ info_ptr->valid |= PNG_INFO_iCCP; ++} ++#endif ++ ++#ifdef PNG_TEXT_SUPPORTED ++void PNGAPI ++png_set_text(png_const_structrp png_ptr, png_inforp info_ptr, ++ png_const_textp text_ptr, int num_text) ++{ ++ int ret; ++ ret = png_set_text_2(png_ptr, info_ptr, text_ptr, num_text); ++ ++ if (ret != 0) ++ png_error(png_ptr, "Insufficient memory to store text"); ++} ++ ++int /* PRIVATE */ ++png_set_text_2(png_const_structrp png_ptr, png_inforp info_ptr, ++ png_const_textp text_ptr, int num_text) ++{ ++ int i; ++ ++ png_debug1(1, "in %lx storage function", png_ptr == NULL ? 0xabadca11U : ++ (unsigned long)png_ptr->chunk_name); ++ ++ if (png_ptr == NULL || info_ptr == NULL || num_text <= 0 || text_ptr == NULL) ++ return(0); ++ ++ /* Make sure we have enough space in the "text" array in info_struct ++ * to hold all of the incoming text_ptr objects. This compare can't overflow ++ * because max_text >= num_text (anyway, subtract of two positive integers ++ * can't overflow in any case.) ++ */ ++ if (num_text > info_ptr->max_text - info_ptr->num_text) ++ { ++ int old_num_text = info_ptr->num_text; ++ int max_text; ++ png_textp new_text = NULL; ++ ++ /* Calculate an appropriate max_text, checking for overflow. */ ++ max_text = old_num_text; ++ if (num_text <= INT_MAX - max_text) ++ { ++ max_text += num_text; ++ ++ /* Round up to a multiple of 8 */ ++ if (max_text < INT_MAX-8) ++ max_text = (max_text + 8) & ~0x7; ++ ++ else ++ max_text = INT_MAX; ++ ++ /* Now allocate a new array and copy the old members in; this does all ++ * the overflow checks. ++ */ ++ new_text = png_voidcast(png_textp,png_realloc_array(png_ptr, ++ info_ptr->text, old_num_text, max_text-old_num_text, ++ sizeof *new_text)); ++ } ++ ++ if (new_text == NULL) ++ { ++ png_chunk_report(png_ptr, "too many text chunks", ++ PNG_CHUNK_WRITE_ERROR); ++ ++ return 1; ++ } ++ ++ png_free(png_ptr, info_ptr->text); ++ ++ info_ptr->text = new_text; ++ info_ptr->free_me |= PNG_FREE_TEXT; ++ info_ptr->max_text = max_text; ++ /* num_text is adjusted below as the entries are copied in */ ++ ++ png_debug1(3, "allocated %d entries for info_ptr->text", max_text); ++ } ++ ++ for (i = 0; i < num_text; i++) ++ { ++ size_t text_length, key_len; ++ size_t lang_len, lang_key_len; ++ png_textp textp = &(info_ptr->text[info_ptr->num_text]); ++ ++ if (text_ptr[i].key == NULL) ++ continue; ++ ++ if (text_ptr[i].compression < PNG_TEXT_COMPRESSION_NONE || ++ text_ptr[i].compression >= PNG_TEXT_COMPRESSION_LAST) ++ { ++ png_chunk_report(png_ptr, "text compression mode is out of range", ++ PNG_CHUNK_WRITE_ERROR); ++ continue; ++ } ++ ++ key_len = strlen(text_ptr[i].key); ++ ++ if (text_ptr[i].compression <= 0) ++ { ++ lang_len = 0; ++ lang_key_len = 0; ++ } ++ ++ else ++# ifdef PNG_iTXt_SUPPORTED ++ { ++ /* Set iTXt data */ ++ ++ if (text_ptr[i].lang != NULL) ++ lang_len = strlen(text_ptr[i].lang); ++ ++ else ++ lang_len = 0; ++ ++ if (text_ptr[i].lang_key != NULL) ++ lang_key_len = strlen(text_ptr[i].lang_key); ++ ++ else ++ lang_key_len = 0; ++ } ++# else /* iTXt */ ++ { ++ png_chunk_report(png_ptr, "iTXt chunk not supported", ++ PNG_CHUNK_WRITE_ERROR); ++ continue; ++ } ++# endif ++ ++ if (text_ptr[i].text == NULL || text_ptr[i].text[0] == '\0') ++ { ++ text_length = 0; ++# ifdef PNG_iTXt_SUPPORTED ++ if (text_ptr[i].compression > 0) ++ textp->compression = PNG_ITXT_COMPRESSION_NONE; ++ ++ else ++# endif ++ textp->compression = PNG_TEXT_COMPRESSION_NONE; ++ } ++ ++ else ++ { ++ text_length = strlen(text_ptr[i].text); ++ textp->compression = text_ptr[i].compression; ++ } ++ ++ textp->key = png_voidcast(png_charp,png_malloc_base(png_ptr, ++ key_len + text_length + lang_len + lang_key_len + 4)); ++ ++ if (textp->key == NULL) ++ { ++ png_chunk_report(png_ptr, "text chunk: out of memory", ++ PNG_CHUNK_WRITE_ERROR); ++ ++ return 1; ++ } ++ ++ png_debug2(2, "Allocated %lu bytes at %p in png_set_text", ++ (unsigned long)(png_uint_32) ++ (key_len + lang_len + lang_key_len + text_length + 4), ++ textp->key); ++ ++ memcpy(textp->key, text_ptr[i].key, key_len); ++ *(textp->key + key_len) = '\0'; ++ ++ if (text_ptr[i].compression > 0) ++ { ++ textp->lang = textp->key + key_len + 1; ++ memcpy(textp->lang, text_ptr[i].lang, lang_len); ++ *(textp->lang + lang_len) = '\0'; ++ textp->lang_key = textp->lang + lang_len + 1; ++ memcpy(textp->lang_key, text_ptr[i].lang_key, lang_key_len); ++ *(textp->lang_key + lang_key_len) = '\0'; ++ textp->text = textp->lang_key + lang_key_len + 1; ++ } ++ ++ else ++ { ++ textp->lang=NULL; ++ textp->lang_key=NULL; ++ textp->text = textp->key + key_len + 1; ++ } ++ ++ if (text_length != 0) ++ memcpy(textp->text, text_ptr[i].text, text_length); ++ ++ *(textp->text + text_length) = '\0'; ++ ++# ifdef PNG_iTXt_SUPPORTED ++ if (textp->compression > 0) ++ { ++ textp->text_length = 0; ++ textp->itxt_length = text_length; ++ } ++ ++ else ++# endif ++ { ++ textp->text_length = text_length; ++ textp->itxt_length = 0; ++ } ++ ++ info_ptr->num_text++; ++ png_debug1(3, "transferred text chunk %d", info_ptr->num_text); ++ } ++ ++ return(0); ++} ++#endif ++ ++#ifdef PNG_tIME_SUPPORTED ++void PNGAPI ++png_set_tIME(png_const_structrp png_ptr, png_inforp info_ptr, ++ png_const_timep mod_time) ++{ ++ png_debug1(1, "in %s storage function", "tIME"); ++ ++ if (png_ptr == NULL || info_ptr == NULL || mod_time == NULL || ++ (png_ptr->mode & PNG_WROTE_tIME) != 0) ++ return; ++ ++ if (mod_time->month == 0 || mod_time->month > 12 || ++ mod_time->day == 0 || mod_time->day > 31 || ++ mod_time->hour > 23 || mod_time->minute > 59 || ++ mod_time->second > 60) ++ { ++ png_warning(png_ptr, "Ignoring invalid time value"); ++ ++ return; ++ } ++ ++ info_ptr->mod_time = *mod_time; ++ info_ptr->valid |= PNG_INFO_tIME; ++} ++#endif ++ ++#ifdef PNG_tRNS_SUPPORTED ++void PNGAPI ++png_set_tRNS(png_structrp png_ptr, png_inforp info_ptr, ++ png_const_bytep trans_alpha, int num_trans, png_const_color_16p trans_color) ++{ ++ png_debug1(1, "in %s storage function", "tRNS"); ++ ++ if (png_ptr == NULL || info_ptr == NULL) ++ ++ return; ++ ++ if (trans_alpha != NULL) ++ { ++ /* It may not actually be necessary to set png_ptr->trans_alpha here; ++ * we do it for backward compatibility with the way the png_handle_tRNS ++ * function used to do the allocation. ++ * ++ * 1.6.0: The above statement is incorrect; png_handle_tRNS effectively ++ * relies on png_set_tRNS storing the information in png_struct ++ * (otherwise it won't be there for the code in pngrtran.c). ++ */ ++ ++ png_free_data(png_ptr, info_ptr, PNG_FREE_TRNS, 0); ++ ++ if (num_trans > 0 && num_trans <= PNG_MAX_PALETTE_LENGTH) ++ { ++ /* Changed from num_trans to PNG_MAX_PALETTE_LENGTH in version 1.2.1 */ ++ info_ptr->trans_alpha = png_voidcast(png_bytep, ++ png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH)); ++ memcpy(info_ptr->trans_alpha, trans_alpha, (size_t)num_trans); ++ } ++ png_ptr->trans_alpha = info_ptr->trans_alpha; ++ } ++ ++ if (trans_color != NULL) ++ { ++#ifdef PNG_WARNINGS_SUPPORTED ++ if (info_ptr->bit_depth < 16) ++ { ++ int sample_max = (1 << info_ptr->bit_depth) - 1; ++ ++ if ((info_ptr->color_type == PNG_COLOR_TYPE_GRAY && ++ trans_color->gray > sample_max) || ++ (info_ptr->color_type == PNG_COLOR_TYPE_RGB && ++ (trans_color->red > sample_max || ++ trans_color->green > sample_max || ++ trans_color->blue > sample_max))) ++ png_warning(png_ptr, ++ "tRNS chunk has out-of-range samples for bit_depth"); ++ } ++#endif ++ ++ info_ptr->trans_color = *trans_color; ++ ++ if (num_trans == 0) ++ num_trans = 1; ++ } ++ ++ info_ptr->num_trans = (png_uint_16)num_trans; ++ ++ if (num_trans != 0) ++ { ++ info_ptr->valid |= PNG_INFO_tRNS; ++ info_ptr->free_me |= PNG_FREE_TRNS; ++ } ++} ++#endif ++ ++#ifdef PNG_sPLT_SUPPORTED ++void PNGAPI ++png_set_sPLT(png_const_structrp png_ptr, ++ png_inforp info_ptr, png_const_sPLT_tp entries, int nentries) ++/* ++ * entries - array of png_sPLT_t structures ++ * to be added to the list of palettes ++ * in the info structure. ++ * ++ * nentries - number of palette structures to be ++ * added. ++ */ ++{ ++ png_sPLT_tp np; ++ ++ if (png_ptr == NULL || info_ptr == NULL || nentries <= 0 || entries == NULL) ++ return; ++ ++ /* Use the internal realloc function, which checks for all the possible ++ * overflows. Notice that the parameters are (int) and (size_t) ++ */ ++ np = png_voidcast(png_sPLT_tp,png_realloc_array(png_ptr, ++ info_ptr->splt_palettes, info_ptr->splt_palettes_num, nentries, ++ sizeof *np)); ++ ++ if (np == NULL) ++ { ++ /* Out of memory or too many chunks */ ++ png_chunk_report(png_ptr, "too many sPLT chunks", PNG_CHUNK_WRITE_ERROR); ++ ++ return; ++ } ++ ++ png_free(png_ptr, info_ptr->splt_palettes); ++ info_ptr->splt_palettes = np; ++ info_ptr->free_me |= PNG_FREE_SPLT; ++ ++ np += info_ptr->splt_palettes_num; ++ ++ do ++ { ++ size_t length; ++ ++ /* Skip invalid input entries */ ++ if (entries->name == NULL || entries->entries == NULL) ++ { ++ /* png_handle_sPLT doesn't do this, so this is an app error */ ++ png_app_error(png_ptr, "png_set_sPLT: invalid sPLT"); ++ /* Just skip the invalid entry */ ++ continue; ++ } ++ ++ np->depth = entries->depth; ++ ++ /* In the event of out-of-memory just return - there's no point keeping ++ * on trying to add sPLT chunks. ++ */ ++ length = strlen(entries->name) + 1; ++ np->name = png_voidcast(png_charp, png_malloc_base(png_ptr, length)); ++ ++ if (np->name == NULL) ++ break; ++ ++ memcpy(np->name, entries->name, length); ++ ++ /* IMPORTANT: we have memory now that won't get freed if something else ++ * goes wrong; this code must free it. png_malloc_array produces no ++ * warnings; use a png_chunk_report (below) if there is an error. ++ */ ++ np->entries = png_voidcast(png_sPLT_entryp, png_malloc_array(png_ptr, ++ entries->nentries, sizeof (png_sPLT_entry))); ++ ++ if (np->entries == NULL) ++ { ++ png_free(png_ptr, np->name); ++ np->name = NULL; ++ break; ++ } ++ ++ np->nentries = entries->nentries; ++ /* This multiply can't overflow because png_malloc_array has already ++ * checked it when doing the allocation. ++ */ ++ memcpy(np->entries, entries->entries, ++ (unsigned int)entries->nentries * sizeof (png_sPLT_entry)); ++ ++ /* Note that 'continue' skips the advance of the out pointer and out ++ * count, so an invalid entry is not added. ++ */ ++ info_ptr->valid |= PNG_INFO_sPLT; ++ ++(info_ptr->splt_palettes_num); ++ ++np; ++ ++entries; ++ } ++ while (--nentries); ++ ++ if (nentries > 0) ++ png_chunk_report(png_ptr, "sPLT out of memory", PNG_CHUNK_WRITE_ERROR); ++} ++#endif /* sPLT */ ++ ++#ifdef PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED ++static png_byte ++check_location(png_const_structrp png_ptr, int location) ++{ ++ location &= (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT); ++ ++ /* New in 1.6.0; copy the location and check it. This is an API ++ * change; previously the app had to use the ++ * png_set_unknown_chunk_location API below for each chunk. ++ */ ++ if (location == 0 && (png_ptr->mode & PNG_IS_READ_STRUCT) == 0) ++ { ++ /* Write struct, so unknown chunks come from the app */ ++ png_app_warning(png_ptr, ++ "png_set_unknown_chunks now expects a valid location"); ++ /* Use the old behavior */ ++ location = (png_byte)(png_ptr->mode & ++ (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT)); ++ } ++ ++ /* This need not be an internal error - if the app calls ++ * png_set_unknown_chunks on a read pointer it must get the location right. ++ */ ++ if (location == 0) ++ png_error(png_ptr, "invalid location in png_set_unknown_chunks"); ++ ++ /* Now reduce the location to the top-most set bit by removing each least ++ * significant bit in turn. ++ */ ++ while (location != (location & -location)) ++ location &= ~(location & -location); ++ ++ /* The cast is safe because 'location' is a bit mask and only the low four ++ * bits are significant. ++ */ ++ return (png_byte)location; ++} ++ ++void PNGAPI ++png_set_unknown_chunks(png_const_structrp png_ptr, ++ png_inforp info_ptr, png_const_unknown_chunkp unknowns, int num_unknowns) ++{ ++ png_unknown_chunkp np; ++ ++ if (png_ptr == NULL || info_ptr == NULL || num_unknowns <= 0 || ++ unknowns == NULL) ++ return; ++ ++ /* Check for the failure cases where support has been disabled at compile ++ * time. This code is hardly ever compiled - it's here because ++ * STORE_UNKNOWN_CHUNKS is set by both read and write code (compiling in this ++ * code) but may be meaningless if the read or write handling of unknown ++ * chunks is not compiled in. ++ */ ++# if !defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) && \ ++ defined(PNG_READ_SUPPORTED) ++ if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0) ++ { ++ png_app_error(png_ptr, "no unknown chunk support on read"); ++ ++ return; ++ } ++# endif ++# if !defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED) && \ ++ defined(PNG_WRITE_SUPPORTED) ++ if ((png_ptr->mode & PNG_IS_READ_STRUCT) == 0) ++ { ++ png_app_error(png_ptr, "no unknown chunk support on write"); ++ ++ return; ++ } ++# endif ++ ++ /* Prior to 1.6.0 this code used png_malloc_warn; however, this meant that ++ * unknown critical chunks could be lost with just a warning resulting in ++ * undefined behavior. Now png_chunk_report is used to provide behavior ++ * appropriate to read or write. ++ */ ++ np = png_voidcast(png_unknown_chunkp, png_realloc_array(png_ptr, ++ info_ptr->unknown_chunks, info_ptr->unknown_chunks_num, num_unknowns, ++ sizeof *np)); ++ ++ if (np == NULL) ++ { ++ png_chunk_report(png_ptr, "too many unknown chunks", ++ PNG_CHUNK_WRITE_ERROR); ++ ++ return; ++ } ++ ++ png_free(png_ptr, info_ptr->unknown_chunks); ++ info_ptr->unknown_chunks = np; /* safe because it is initialized */ ++ info_ptr->free_me |= PNG_FREE_UNKN; ++ ++ np += info_ptr->unknown_chunks_num; ++ ++ /* Increment unknown_chunks_num each time round the loop to protect the ++ * just-allocated chunk data. ++ */ ++ for (; num_unknowns > 0; --num_unknowns, ++unknowns) ++ { ++ memcpy(np->name, unknowns->name, (sizeof np->name)); ++ np->name[(sizeof np->name)-1] = '\0'; ++ np->location = check_location(png_ptr, unknowns->location); ++ ++ if (unknowns->size == 0) ++ { ++ np->data = NULL; ++ np->size = 0; ++ } ++ ++ else ++ { ++ np->data = png_voidcast(png_bytep, ++ png_malloc_base(png_ptr, unknowns->size)); ++ ++ if (np->data == NULL) ++ { ++ png_chunk_report(png_ptr, "unknown chunk: out of memory", ++ PNG_CHUNK_WRITE_ERROR); ++ /* But just skip storing the unknown chunk */ ++ continue; ++ } ++ ++ memcpy(np->data, unknowns->data, unknowns->size); ++ np->size = unknowns->size; ++ } ++ ++ /* These increments are skipped on out-of-memory for the data - the ++ * unknown chunk entry gets overwritten if the png_chunk_report returns. ++ * This is correct in the read case (the chunk is just dropped.) ++ */ ++ ++np; ++ ++(info_ptr->unknown_chunks_num); ++ } ++} ++ ++void PNGAPI ++png_set_unknown_chunk_location(png_const_structrp png_ptr, png_inforp info_ptr, ++ int chunk, int location) ++{ ++ /* This API is pretty pointless in 1.6.0 because the location can be set ++ * before the call to png_set_unknown_chunks. ++ * ++ * TODO: add a png_app_warning in 1.7 ++ */ ++ if (png_ptr != NULL && info_ptr != NULL && chunk >= 0 && ++ chunk < info_ptr->unknown_chunks_num) ++ { ++ if ((location & (PNG_HAVE_IHDR|PNG_HAVE_PLTE|PNG_AFTER_IDAT)) == 0) ++ { ++ png_app_error(png_ptr, "invalid unknown chunk location"); ++ /* Fake out the pre 1.6.0 behavior: */ ++ if (((unsigned int)location & PNG_HAVE_IDAT) != 0) /* undocumented! */ ++ location = PNG_AFTER_IDAT; ++ ++ else ++ location = PNG_HAVE_IHDR; /* also undocumented */ ++ } ++ ++ info_ptr->unknown_chunks[chunk].location = ++ check_location(png_ptr, location); ++ } ++} ++#endif /* STORE_UNKNOWN_CHUNKS */ ++ ++#ifdef PNG_MNG_FEATURES_SUPPORTED ++png_uint_32 PNGAPI ++png_permit_mng_features (png_structrp png_ptr, png_uint_32 mng_features) ++{ ++ png_debug(1, "in png_permit_mng_features"); ++ ++ if (png_ptr == NULL) ++ return 0; ++ ++ png_ptr->mng_features_permitted = mng_features & PNG_ALL_MNG_FEATURES; ++ ++ return png_ptr->mng_features_permitted; ++} ++#endif ++ ++#ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED ++static unsigned int ++add_one_chunk(png_bytep list, unsigned int count, png_const_bytep add, int keep) ++{ ++ unsigned int i; ++ ++ /* Utility function: update the 'keep' state of a chunk if it is already in ++ * the list, otherwise add it to the list. ++ */ ++ for (i=0; i= PNG_HANDLE_CHUNK_LAST) ++ { ++ png_app_error(png_ptr, "png_set_keep_unknown_chunks: invalid keep"); ++ ++ return; ++ } ++ ++ if (num_chunks_in <= 0) ++ { ++ png_ptr->unknown_default = keep; ++ ++ /* '0' means just set the flags, so stop here */ ++ if (num_chunks_in == 0) ++ return; ++ } ++ ++ if (num_chunks_in < 0) ++ { ++ /* Ignore all unknown chunks and all chunks recognized by ++ * libpng except for IHDR, PLTE, tRNS, IDAT, and IEND ++ */ ++ static const png_byte chunks_to_ignore[] = { ++ 98, 75, 71, 68, '\0', /* bKGD */ ++ 99, 72, 82, 77, '\0', /* cHRM */ ++ 101, 88, 73, 102, '\0', /* eXIf */ ++ 103, 65, 77, 65, '\0', /* gAMA */ ++ 104, 73, 83, 84, '\0', /* hIST */ ++ 105, 67, 67, 80, '\0', /* iCCP */ ++ 105, 84, 88, 116, '\0', /* iTXt */ ++ 111, 70, 70, 115, '\0', /* oFFs */ ++ 112, 67, 65, 76, '\0', /* pCAL */ ++ 112, 72, 89, 115, '\0', /* pHYs */ ++ 115, 66, 73, 84, '\0', /* sBIT */ ++ 115, 67, 65, 76, '\0', /* sCAL */ ++ 115, 80, 76, 84, '\0', /* sPLT */ ++ 115, 84, 69, 82, '\0', /* sTER */ ++ 115, 82, 71, 66, '\0', /* sRGB */ ++ 116, 69, 88, 116, '\0', /* tEXt */ ++ 116, 73, 77, 69, '\0', /* tIME */ ++ 122, 84, 88, 116, '\0' /* zTXt */ ++ }; ++ ++ chunk_list = chunks_to_ignore; ++ num_chunks = (unsigned int)/*SAFE*/(sizeof chunks_to_ignore)/5U; ++ } ++ ++ else /* num_chunks_in > 0 */ ++ { ++ if (chunk_list == NULL) ++ { ++ /* Prior to 1.6.0 this was silently ignored, now it is an app_error ++ * which can be switched off. ++ */ ++ png_app_error(png_ptr, "png_set_keep_unknown_chunks: no chunk list"); ++ ++ return; ++ } ++ ++ num_chunks = (unsigned int)num_chunks_in; ++ } ++ ++ old_num_chunks = png_ptr->num_chunk_list; ++ if (png_ptr->chunk_list == NULL) ++ old_num_chunks = 0; ++ ++ /* Since num_chunks is always restricted to UINT_MAX/5 this can't overflow. ++ */ ++ if (num_chunks + old_num_chunks > UINT_MAX/5) ++ { ++ png_app_error(png_ptr, "png_set_keep_unknown_chunks: too many chunks"); ++ ++ return; ++ } ++ ++ /* If these chunks are being reset to the default then no more memory is ++ * required because add_one_chunk above doesn't extend the list if the 'keep' ++ * parameter is the default. ++ */ ++ if (keep != 0) ++ { ++ new_list = png_voidcast(png_bytep, png_malloc(png_ptr, ++ 5 * (num_chunks + old_num_chunks))); ++ ++ if (old_num_chunks > 0) ++ memcpy(new_list, png_ptr->chunk_list, 5*old_num_chunks); ++ } ++ ++ else if (old_num_chunks > 0) ++ new_list = png_ptr->chunk_list; ++ ++ else ++ new_list = NULL; ++ ++ /* Add the new chunks together with each one's handling code. If the chunk ++ * already exists the code is updated, otherwise the chunk is added to the ++ * end. (In libpng 1.6.0 order no longer matters because this code enforces ++ * the earlier convention that the last setting is the one that is used.) ++ */ ++ if (new_list != NULL) ++ { ++ png_const_bytep inlist; ++ png_bytep outlist; ++ unsigned int i; ++ ++ for (i=0; ichunk_list != new_list) ++ png_free(png_ptr, new_list); ++ ++ new_list = NULL; ++ } ++ } ++ ++ else ++ num_chunks = 0; ++ ++ png_ptr->num_chunk_list = num_chunks; ++ ++ if (png_ptr->chunk_list != new_list) ++ { ++ if (png_ptr->chunk_list != NULL) ++ png_free(png_ptr, png_ptr->chunk_list); ++ ++ png_ptr->chunk_list = new_list; ++ } ++} ++#endif ++ ++#ifdef PNG_READ_USER_CHUNKS_SUPPORTED ++void PNGAPI ++png_set_read_user_chunk_fn(png_structrp png_ptr, png_voidp user_chunk_ptr, ++ png_user_chunk_ptr read_user_chunk_fn) ++{ ++ png_debug(1, "in png_set_read_user_chunk_fn"); ++ ++ if (png_ptr == NULL) ++ return; ++ ++ png_ptr->read_user_chunk_fn = read_user_chunk_fn; ++ png_ptr->user_chunk_ptr = user_chunk_ptr; ++} ++#endif ++ ++#ifdef PNG_INFO_IMAGE_SUPPORTED ++void PNGAPI ++png_set_rows(png_const_structrp png_ptr, png_inforp info_ptr, ++ png_bytepp row_pointers) ++{ ++ png_debug1(1, "in %s storage function", "rows"); ++ ++ if (png_ptr == NULL || info_ptr == NULL) ++ return; ++ ++ if (info_ptr->row_pointers != NULL && ++ (info_ptr->row_pointers != row_pointers)) ++ png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0); ++ ++ info_ptr->row_pointers = row_pointers; ++ ++ if (row_pointers != NULL) ++ info_ptr->valid |= PNG_INFO_IDAT; ++} ++#endif ++ ++void PNGAPI ++png_set_compression_buffer_size(png_structrp png_ptr, size_t size) ++{ ++ if (png_ptr == NULL) ++ return; ++ ++ if (size == 0 || size > PNG_UINT_31_MAX) ++ png_error(png_ptr, "invalid compression buffer size"); ++ ++# ifdef PNG_SEQUENTIAL_READ_SUPPORTED ++ if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0) ++ { ++ png_ptr->IDAT_read_size = (png_uint_32)size; /* checked above */ ++ return; ++ } ++# endif ++ ++# ifdef PNG_WRITE_SUPPORTED ++ if ((png_ptr->mode & PNG_IS_READ_STRUCT) == 0) ++ { ++ if (png_ptr->zowner != 0) ++ { ++ png_warning(png_ptr, ++ "Compression buffer size cannot be changed because it is in use"); ++ ++ return; ++ } ++ ++#ifndef __COVERITY__ ++ /* Some compilers complain that this is always false. However, it ++ * can be true when integer overflow happens. ++ */ ++ if (size > ZLIB_IO_MAX) ++ { ++ png_warning(png_ptr, ++ "Compression buffer size limited to system maximum"); ++ size = ZLIB_IO_MAX; /* must fit */ ++ } ++#endif ++ ++ if (size < 6) ++ { ++ /* Deflate will potentially go into an infinite loop on a SYNC_FLUSH ++ * if this is permitted. ++ */ ++ png_warning(png_ptr, ++ "Compression buffer size cannot be reduced below 6"); ++ ++ return; ++ } ++ ++ if (png_ptr->zbuffer_size != size) ++ { ++ png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list); ++ png_ptr->zbuffer_size = (uInt)size; ++ } ++ } ++# endif ++} ++ ++void PNGAPI ++png_set_invalid(png_const_structrp png_ptr, png_inforp info_ptr, int mask) ++{ ++ if (png_ptr != NULL && info_ptr != NULL) ++ info_ptr->valid &= (unsigned int)(~mask); ++} ++ ++ ++#ifdef PNG_SET_USER_LIMITS_SUPPORTED ++/* This function was added to libpng 1.2.6 */ ++void PNGAPI ++png_set_user_limits (png_structrp png_ptr, png_uint_32 user_width_max, ++ png_uint_32 user_height_max) ++{ ++ /* Images with dimensions larger than these limits will be ++ * rejected by png_set_IHDR(). To accept any PNG datastream ++ * regardless of dimensions, set both limits to 0x7fffffff. ++ */ ++ if (png_ptr == NULL) ++ return; ++ ++ png_ptr->user_width_max = user_width_max; ++ png_ptr->user_height_max = user_height_max; ++} ++ ++/* This function was added to libpng 1.4.0 */ ++void PNGAPI ++png_set_chunk_cache_max (png_structrp png_ptr, png_uint_32 user_chunk_cache_max) ++{ ++ if (png_ptr != NULL) ++ png_ptr->user_chunk_cache_max = user_chunk_cache_max; ++} ++ ++/* This function was added to libpng 1.4.1 */ ++void PNGAPI ++png_set_chunk_malloc_max (png_structrp png_ptr, ++ png_alloc_size_t user_chunk_malloc_max) ++{ ++ if (png_ptr != NULL) ++ png_ptr->user_chunk_malloc_max = user_chunk_malloc_max; ++} ++#endif /* ?SET_USER_LIMITS */ ++ ++ ++#ifdef PNG_BENIGN_ERRORS_SUPPORTED ++void PNGAPI ++png_set_benign_errors(png_structrp png_ptr, int allowed) ++{ ++ png_debug(1, "in png_set_benign_errors"); ++ ++ /* If allowed is 1, png_benign_error() is treated as a warning. ++ * ++ * If allowed is 0, png_benign_error() is treated as an error (which ++ * is the default behavior if png_set_benign_errors() is not called). ++ */ ++ ++ if (allowed != 0) ++ png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN | ++ PNG_FLAG_APP_WARNINGS_WARN | PNG_FLAG_APP_ERRORS_WARN; ++ ++ else ++ png_ptr->flags &= ~(PNG_FLAG_BENIGN_ERRORS_WARN | ++ PNG_FLAG_APP_WARNINGS_WARN | PNG_FLAG_APP_ERRORS_WARN); ++} ++#endif /* BENIGN_ERRORS */ ++ ++#ifdef PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED ++ /* Whether to report invalid palette index; added at libng-1.5.10. ++ * It is possible for an indexed (color-type==3) PNG file to contain ++ * pixels with invalid (out-of-range) indexes if the PLTE chunk has ++ * fewer entries than the image's bit-depth would allow. We recover ++ * from this gracefully by filling any incomplete palette with zeros ++ * (opaque black). By default, when this occurs libpng will issue ++ * a benign error. This API can be used to override that behavior. ++ */ ++void PNGAPI ++png_set_check_for_invalid_index(png_structrp png_ptr, int allowed) ++{ ++ png_debug(1, "in png_set_check_for_invalid_index"); ++ ++ if (allowed > 0) ++ png_ptr->num_palette_max = 0; ++ ++ else ++ png_ptr->num_palette_max = -1; ++} ++#endif ++ ++#if defined(PNG_TEXT_SUPPORTED) || defined(PNG_pCAL_SUPPORTED) || \ ++ defined(PNG_iCCP_SUPPORTED) || defined(PNG_sPLT_SUPPORTED) ++/* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification, ++ * and if invalid, correct the keyword rather than discarding the entire ++ * chunk. The PNG 1.0 specification requires keywords 1-79 characters in ++ * length, forbids leading or trailing whitespace, multiple internal spaces, ++ * and the non-break space (0x80) from ISO 8859-1. Returns keyword length. ++ * ++ * The 'new_key' buffer must be 80 characters in size (for the keyword plus a ++ * trailing '\0'). If this routine returns 0 then there was no keyword, or a ++ * valid one could not be generated, and the caller must png_error. ++ */ ++png_uint_32 /* PRIVATE */ ++png_check_keyword(png_structrp png_ptr, png_const_charp key, png_bytep new_key) ++{ ++#ifdef PNG_WARNINGS_SUPPORTED ++ png_const_charp orig_key = key; ++#endif ++ png_uint_32 key_len = 0; ++ int bad_character = 0; ++ int space = 1; ++ ++ png_debug(1, "in png_check_keyword"); ++ ++ if (key == NULL) ++ { ++ *new_key = 0; ++ return 0; ++ } ++ ++ while (*key && key_len < 79) ++ { ++ png_byte ch = (png_byte)*key++; ++ ++ if ((ch > 32 && ch <= 126) || (ch >= 161 /*&& ch <= 255*/)) ++ { ++ *new_key++ = ch; ++key_len; space = 0; ++ } ++ ++ else if (space == 0) ++ { ++ /* A space or an invalid character when one wasn't seen immediately ++ * before; output just a space. ++ */ ++ *new_key++ = 32; ++key_len; space = 1; ++ ++ /* If the character was not a space then it is invalid. */ ++ if (ch != 32) ++ bad_character = ch; ++ } ++ ++ else if (bad_character == 0) ++ bad_character = ch; /* just skip it, record the first error */ ++ } ++ ++ if (key_len > 0 && space != 0) /* trailing space */ ++ { ++ --key_len; --new_key; ++ if (bad_character == 0) ++ bad_character = 32; ++ } ++ ++ /* Terminate the keyword */ ++ *new_key = 0; ++ ++ if (key_len == 0) ++ return 0; ++ ++#ifdef PNG_WARNINGS_SUPPORTED ++ /* Try to only output one warning per keyword: */ ++ if (*key != 0) /* keyword too long */ ++ png_warning(png_ptr, "keyword truncated"); ++ ++ else if (bad_character != 0) ++ { ++ PNG_WARNING_PARAMETERS(p) ++ ++ png_warning_parameter(p, 1, orig_key); ++ png_warning_parameter_signed(p, 2, PNG_NUMBER_FORMAT_02x, bad_character); ++ ++ png_formatted_warning(png_ptr, p, "keyword \"@1\": bad character '0x@2'"); ++ } ++#else /* !WARNINGS */ ++ PNG_UNUSED(png_ptr) ++#endif /* !WARNINGS */ ++ ++ return key_len; ++} ++#endif /* TEXT || pCAL || iCCP || sPLT */ ++#endif /* READ || WRITE */ +diff --git a/lib/libpng/pngstruct.h b/lib/libpng/pngstruct.h +new file mode 100644 +index 000000000..8bdc7ce46 +--- /dev/null ++++ b/lib/libpng/pngstruct.h +@@ -0,0 +1,489 @@ ++ ++/* pngstruct.h - header file for PNG reference library ++ * ++ * Copyright (c) 2018-2019 Cosmin Truta ++ * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson ++ * Copyright (c) 1996-1997 Andreas Dilger ++ * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc. ++ * ++ * This code is released under the libpng license. ++ * For conditions of distribution and use, see the disclaimer ++ * and license in png.h ++ */ ++ ++/* The structure that holds the information to read and write PNG files. ++ * The only people who need to care about what is inside of this are the ++ * people who will be modifying the library for their own special needs. ++ * It should NOT be accessed directly by an application. ++ */ ++ ++#ifndef PNGSTRUCT_H ++#define PNGSTRUCT_H ++/* zlib.h defines the structure z_stream, an instance of which is included ++ * in this structure and is required for decompressing the LZ compressed ++ * data in PNG files. ++ */ ++#ifndef ZLIB_CONST ++ /* We must ensure that zlib uses 'const' in declarations. */ ++# define ZLIB_CONST ++#endif ++#include "zlib.h" ++#ifdef const ++ /* zlib.h sometimes #defines const to nothing, undo this. */ ++# undef const ++#endif ++ ++/* zlib.h has mediocre z_const use before 1.2.6, this stuff is for compatibility ++ * with older builds. ++ */ ++#if ZLIB_VERNUM < 0x1260 ++# define PNGZ_MSG_CAST(s) png_constcast(char*,s) ++# define PNGZ_INPUT_CAST(b) png_constcast(png_bytep,b) ++#else ++# define PNGZ_MSG_CAST(s) (s) ++# define PNGZ_INPUT_CAST(b) (b) ++#endif ++ ++/* zlib.h declares a magic type 'uInt' that limits the amount of data that zlib ++ * can handle at once. This type need be no larger than 16 bits (so maximum of ++ * 65535), this define allows us to discover how big it is, but limited by the ++ * maximum for size_t. The value can be overridden in a library build ++ * (pngusr.h, or set it in CPPFLAGS) and it works to set it to a considerably ++ * lower value (e.g. 255 works). A lower value may help memory usage (slightly) ++ * and may even improve performance on some systems (and degrade it on others.) ++ */ ++#ifndef ZLIB_IO_MAX ++# define ZLIB_IO_MAX ((uInt)-1) ++#endif ++ ++#ifdef PNG_WRITE_SUPPORTED ++/* The type of a compression buffer list used by the write code. */ ++typedef struct png_compression_buffer ++{ ++ struct png_compression_buffer *next; ++ png_byte output[1]; /* actually zbuf_size */ ++} png_compression_buffer, *png_compression_bufferp; ++ ++#define PNG_COMPRESSION_BUFFER_SIZE(pp)\ ++ (offsetof(png_compression_buffer, output) + (pp)->zbuffer_size) ++#endif ++ ++/* Colorspace support; structures used in png_struct, png_info and in internal ++ * functions to hold and communicate information about the color space. ++ * ++ * PNG_COLORSPACE_SUPPORTED is only required if the application will perform ++ * colorspace corrections, otherwise all the colorspace information can be ++ * skipped and the size of libpng can be reduced (significantly) by compiling ++ * out the colorspace support. ++ */ ++#ifdef PNG_COLORSPACE_SUPPORTED ++/* The chromaticities of the red, green and blue colorants and the chromaticity ++ * of the corresponding white point (i.e. of rgb(1.0,1.0,1.0)). ++ */ ++typedef struct png_xy ++{ ++ png_fixed_point redx, redy; ++ png_fixed_point greenx, greeny; ++ png_fixed_point bluex, bluey; ++ png_fixed_point whitex, whitey; ++} png_xy; ++ ++/* The same data as above but encoded as CIE XYZ values. When this data comes ++ * from chromaticities the sum of the Y values is assumed to be 1.0 ++ */ ++typedef struct png_XYZ ++{ ++ png_fixed_point red_X, red_Y, red_Z; ++ png_fixed_point green_X, green_Y, green_Z; ++ png_fixed_point blue_X, blue_Y, blue_Z; ++} png_XYZ; ++#endif /* COLORSPACE */ ++ ++#if defined(PNG_COLORSPACE_SUPPORTED) || defined(PNG_GAMMA_SUPPORTED) ++/* A colorspace is all the above plus, potentially, profile information; ++ * however at present libpng does not use the profile internally so it is only ++ * stored in the png_info struct (if iCCP is supported.) The rendering intent ++ * is retained here and is checked. ++ * ++ * The file gamma encoding information is also stored here and gamma correction ++ * is done by libpng, whereas color correction must currently be done by the ++ * application. ++ */ ++typedef struct png_colorspace ++{ ++#ifdef PNG_GAMMA_SUPPORTED ++ png_fixed_point gamma; /* File gamma */ ++#endif ++ ++#ifdef PNG_COLORSPACE_SUPPORTED ++ png_xy end_points_xy; /* End points as chromaticities */ ++ png_XYZ end_points_XYZ; /* End points as CIE XYZ colorant values */ ++ png_uint_16 rendering_intent; /* Rendering intent of a profile */ ++#endif ++ ++ /* Flags are always defined to simplify the code. */ ++ png_uint_16 flags; /* As defined below */ ++} png_colorspace, * PNG_RESTRICT png_colorspacerp; ++ ++typedef const png_colorspace * PNG_RESTRICT png_const_colorspacerp; ++ ++/* General flags for the 'flags' field */ ++#define PNG_COLORSPACE_HAVE_GAMMA 0x0001 ++#define PNG_COLORSPACE_HAVE_ENDPOINTS 0x0002 ++#define PNG_COLORSPACE_HAVE_INTENT 0x0004 ++#define PNG_COLORSPACE_FROM_gAMA 0x0008 ++#define PNG_COLORSPACE_FROM_cHRM 0x0010 ++#define PNG_COLORSPACE_FROM_sRGB 0x0020 ++#define PNG_COLORSPACE_ENDPOINTS_MATCH_sRGB 0x0040 ++#define PNG_COLORSPACE_MATCHES_sRGB 0x0080 /* exact match on profile */ ++#define PNG_COLORSPACE_INVALID 0x8000 ++#define PNG_COLORSPACE_CANCEL(flags) (0xffff ^ (flags)) ++#endif /* COLORSPACE || GAMMA */ ++ ++struct png_struct_def ++{ ++#ifdef PNG_SETJMP_SUPPORTED ++ jmp_buf jmp_buf_local; /* New name in 1.6.0 for jmp_buf in png_struct */ ++ png_longjmp_ptr longjmp_fn;/* setjmp non-local goto function. */ ++ jmp_buf *jmp_buf_ptr; /* passed to longjmp_fn */ ++ size_t jmp_buf_size; /* size of the above, if allocated */ ++#endif ++ png_error_ptr error_fn; /* function for printing errors and aborting */ ++#ifdef PNG_WARNINGS_SUPPORTED ++ png_error_ptr warning_fn; /* function for printing warnings */ ++#endif ++ png_voidp error_ptr; /* user supplied struct for error functions */ ++ png_rw_ptr write_data_fn; /* function for writing output data */ ++ png_rw_ptr read_data_fn; /* function for reading input data */ ++ png_voidp io_ptr; /* ptr to application struct for I/O functions */ ++ ++#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED ++ png_user_transform_ptr read_user_transform_fn; /* user read transform */ ++#endif ++ ++#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED ++ png_user_transform_ptr write_user_transform_fn; /* user write transform */ ++#endif ++ ++/* These were added in libpng-1.0.2 */ ++#ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED ++#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \ ++ defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) ++ png_voidp user_transform_ptr; /* user supplied struct for user transform */ ++ png_byte user_transform_depth; /* bit depth of user transformed pixels */ ++ png_byte user_transform_channels; /* channels in user transformed pixels */ ++#endif ++#endif ++ ++ png_uint_32 mode; /* tells us where we are in the PNG file */ ++ png_uint_32 flags; /* flags indicating various things to libpng */ ++ png_uint_32 transformations; /* which transformations to perform */ ++ ++ png_uint_32 zowner; /* ID (chunk type) of zstream owner, 0 if none */ ++ z_stream zstream; /* decompression structure */ ++ ++#ifdef PNG_WRITE_SUPPORTED ++ png_compression_bufferp zbuffer_list; /* Created on demand during write */ ++ uInt zbuffer_size; /* size of the actual buffer */ ++ ++ int zlib_level; /* holds zlib compression level */ ++ int zlib_method; /* holds zlib compression method */ ++ int zlib_window_bits; /* holds zlib compression window bits */ ++ int zlib_mem_level; /* holds zlib compression memory level */ ++ int zlib_strategy; /* holds zlib compression strategy */ ++#endif ++/* Added at libpng 1.5.4 */ ++#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED ++ int zlib_text_level; /* holds zlib compression level */ ++ int zlib_text_method; /* holds zlib compression method */ ++ int zlib_text_window_bits; /* holds zlib compression window bits */ ++ int zlib_text_mem_level; /* holds zlib compression memory level */ ++ int zlib_text_strategy; /* holds zlib compression strategy */ ++#endif ++/* End of material added at libpng 1.5.4 */ ++/* Added at libpng 1.6.0 */ ++#ifdef PNG_WRITE_SUPPORTED ++ int zlib_set_level; /* Actual values set into the zstream on write */ ++ int zlib_set_method; ++ int zlib_set_window_bits; ++ int zlib_set_mem_level; ++ int zlib_set_strategy; ++#endif ++ ++ png_uint_32 width; /* width of image in pixels */ ++ png_uint_32 height; /* height of image in pixels */ ++ png_uint_32 num_rows; /* number of rows in current pass */ ++ png_uint_32 usr_width; /* width of row at start of write */ ++ size_t rowbytes; /* size of row in bytes */ ++ png_uint_32 iwidth; /* width of current interlaced row in pixels */ ++ png_uint_32 row_number; /* current row in interlace pass */ ++ png_uint_32 chunk_name; /* PNG_CHUNK() id of current chunk */ ++ png_bytep prev_row; /* buffer to save previous (unfiltered) row. ++ * While reading this is a pointer into ++ * big_prev_row; while writing it is separately ++ * allocated if needed. ++ */ ++ png_bytep row_buf; /* buffer to save current (unfiltered) row. ++ * While reading, this is a pointer into ++ * big_row_buf; while writing it is separately ++ * allocated. ++ */ ++#ifdef PNG_WRITE_FILTER_SUPPORTED ++ png_bytep try_row; /* buffer to save trial row when filtering */ ++ png_bytep tst_row; /* buffer to save best trial row when filtering */ ++#endif ++ size_t info_rowbytes; /* Added in 1.5.4: cache of updated row bytes */ ++ ++ png_uint_32 idat_size; /* current IDAT size for read */ ++ png_uint_32 crc; /* current chunk CRC value */ ++ png_colorp palette; /* palette from the input file */ ++ png_uint_16 num_palette; /* number of color entries in palette */ ++ ++/* Added at libpng-1.5.10 */ ++#ifdef PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED ++ int num_palette_max; /* maximum palette index found in IDAT */ ++#endif ++ ++ png_uint_16 num_trans; /* number of transparency values */ ++ png_byte compression; /* file compression type (always 0) */ ++ png_byte filter; /* file filter type (always 0) */ ++ png_byte interlaced; /* PNG_INTERLACE_NONE, PNG_INTERLACE_ADAM7 */ ++ png_byte pass; /* current interlace pass (0 - 6) */ ++ png_byte do_filter; /* row filter flags (see PNG_FILTER_ in png.h ) */ ++ png_byte color_type; /* color type of file */ ++ png_byte bit_depth; /* bit depth of file */ ++ png_byte usr_bit_depth; /* bit depth of users row: write only */ ++ png_byte pixel_depth; /* number of bits per pixel */ ++ png_byte channels; /* number of channels in file */ ++#ifdef PNG_WRITE_SUPPORTED ++ png_byte usr_channels; /* channels at start of write: write only */ ++#endif ++ png_byte sig_bytes; /* magic bytes read/written from start of file */ ++ png_byte maximum_pixel_depth; ++ /* pixel depth used for the row buffers */ ++ png_byte transformed_pixel_depth; ++ /* pixel depth after read/write transforms */ ++#if ZLIB_VERNUM >= 0x1240 ++ png_byte zstream_start; /* at start of an input zlib stream */ ++#endif /* Zlib >= 1.2.4 */ ++#if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED) ++ png_uint_16 filler; /* filler bytes for pixel expansion */ ++#endif ++ ++#if defined(PNG_bKGD_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) ||\ ++ defined(PNG_READ_ALPHA_MODE_SUPPORTED) ++ png_byte background_gamma_type; ++ png_fixed_point background_gamma; ++ png_color_16 background; /* background color in screen gamma space */ ++#ifdef PNG_READ_GAMMA_SUPPORTED ++ png_color_16 background_1; /* background normalized to gamma 1.0 */ ++#endif ++#endif /* bKGD */ ++ ++#ifdef PNG_WRITE_FLUSH_SUPPORTED ++ png_flush_ptr output_flush_fn; /* Function for flushing output */ ++ png_uint_32 flush_dist; /* how many rows apart to flush, 0 - no flush */ ++ png_uint_32 flush_rows; /* number of rows written since last flush */ ++#endif ++ ++#ifdef PNG_READ_GAMMA_SUPPORTED ++ int gamma_shift; /* number of "insignificant" bits in 16-bit gamma */ ++ png_fixed_point screen_gamma; /* screen gamma value (display_exponent) */ ++ ++ png_bytep gamma_table; /* gamma table for 8-bit depth files */ ++ png_uint_16pp gamma_16_table; /* gamma table for 16-bit depth files */ ++#if defined(PNG_READ_BACKGROUND_SUPPORTED) || \ ++ defined(PNG_READ_ALPHA_MODE_SUPPORTED) || \ ++ defined(PNG_READ_RGB_TO_GRAY_SUPPORTED) ++ png_bytep gamma_from_1; /* converts from 1.0 to screen */ ++ png_bytep gamma_to_1; /* converts from file to 1.0 */ ++ png_uint_16pp gamma_16_from_1; /* converts from 1.0 to screen */ ++ png_uint_16pp gamma_16_to_1; /* converts from file to 1.0 */ ++#endif /* READ_BACKGROUND || READ_ALPHA_MODE || RGB_TO_GRAY */ ++#endif ++ ++#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_sBIT_SUPPORTED) ++ png_color_8 sig_bit; /* significant bits in each available channel */ ++#endif ++ ++#if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED) ++ png_color_8 shift; /* shift for significant bit transformation */ ++#endif ++ ++#if defined(PNG_tRNS_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) \ ++ || defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED) ++ png_bytep trans_alpha; /* alpha values for paletted files */ ++ png_color_16 trans_color; /* transparent color for non-paletted files */ ++#endif ++ ++ png_read_status_ptr read_row_fn; /* called after each row is decoded */ ++ png_write_status_ptr write_row_fn; /* called after each row is encoded */ ++#ifdef PNG_PROGRESSIVE_READ_SUPPORTED ++ png_progressive_info_ptr info_fn; /* called after header data fully read */ ++ png_progressive_row_ptr row_fn; /* called after a prog. row is decoded */ ++ png_progressive_end_ptr end_fn; /* called after image is complete */ ++ png_bytep save_buffer_ptr; /* current location in save_buffer */ ++ png_bytep save_buffer; /* buffer for previously read data */ ++ png_bytep current_buffer_ptr; /* current location in current_buffer */ ++ png_bytep current_buffer; /* buffer for recently used data */ ++ png_uint_32 push_length; /* size of current input chunk */ ++ png_uint_32 skip_length; /* bytes to skip in input data */ ++ size_t save_buffer_size; /* amount of data now in save_buffer */ ++ size_t save_buffer_max; /* total size of save_buffer */ ++ size_t buffer_size; /* total amount of available input data */ ++ size_t current_buffer_size; /* amount of data now in current_buffer */ ++ int process_mode; /* what push library is currently doing */ ++ int cur_palette; /* current push library palette index */ ++ ++#endif /* PROGRESSIVE_READ */ ++ ++#if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__) ++/* For the Borland special 64K segment handler */ ++ png_bytepp offset_table_ptr; ++ png_bytep offset_table; ++ png_uint_16 offset_table_number; ++ png_uint_16 offset_table_count; ++ png_uint_16 offset_table_count_free; ++#endif ++ ++#ifdef PNG_READ_QUANTIZE_SUPPORTED ++ png_bytep palette_lookup; /* lookup table for quantizing */ ++ png_bytep quantize_index; /* index translation for palette files */ ++#endif ++ ++/* Options */ ++#ifdef PNG_SET_OPTION_SUPPORTED ++ png_uint_32 options; /* On/off state (up to 16 options) */ ++#endif ++ ++#if PNG_LIBPNG_VER < 10700 ++/* To do: remove this from libpng-1.7 */ ++#ifdef PNG_TIME_RFC1123_SUPPORTED ++ char time_buffer[29]; /* String to hold RFC 1123 time text */ ++#endif ++#endif ++ ++/* New members added in libpng-1.0.6 */ ++ ++ png_uint_32 free_me; /* flags items libpng is responsible for freeing */ ++ ++#ifdef PNG_USER_CHUNKS_SUPPORTED ++ png_voidp user_chunk_ptr; ++#ifdef PNG_READ_USER_CHUNKS_SUPPORTED ++ png_user_chunk_ptr read_user_chunk_fn; /* user read chunk handler */ ++#endif ++#endif ++ ++#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED ++ int unknown_default; /* As PNG_HANDLE_* */ ++ unsigned int num_chunk_list; /* Number of entries in the list */ ++ png_bytep chunk_list; /* List of png_byte[5]; the textual chunk name ++ * followed by a PNG_HANDLE_* byte */ ++#endif ++ ++/* New members added in libpng-1.0.3 */ ++#ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED ++ png_byte rgb_to_gray_status; ++ /* Added in libpng 1.5.5 to record setting of coefficients: */ ++ png_byte rgb_to_gray_coefficients_set; ++ /* These were changed from png_byte in libpng-1.0.6 */ ++ png_uint_16 rgb_to_gray_red_coeff; ++ png_uint_16 rgb_to_gray_green_coeff; ++ /* deleted in 1.5.5: rgb_to_gray_blue_coeff; */ ++#endif ++ ++/* New member added in libpng-1.6.36 */ ++#if defined(PNG_READ_EXPAND_SUPPORTED) && \ ++ defined(PNG_ARM_NEON_IMPLEMENTATION) ++ png_bytep riffled_palette; /* buffer for accelerated palette expansion */ ++#endif ++ ++/* New member added in libpng-1.0.4 (renamed in 1.0.9) */ ++#if defined(PNG_MNG_FEATURES_SUPPORTED) ++/* Changed from png_byte to png_uint_32 at version 1.2.0 */ ++ png_uint_32 mng_features_permitted; ++#endif ++ ++/* New member added in libpng-1.0.9, ifdef'ed out in 1.0.12, enabled in 1.2.0 */ ++#ifdef PNG_MNG_FEATURES_SUPPORTED ++ png_byte filter_type; ++#endif ++ ++/* New members added in libpng-1.2.0 */ ++ ++/* New members added in libpng-1.0.2 but first enabled by default in 1.2.0 */ ++#ifdef PNG_USER_MEM_SUPPORTED ++ png_voidp mem_ptr; /* user supplied struct for mem functions */ ++ png_malloc_ptr malloc_fn; /* function for allocating memory */ ++ png_free_ptr free_fn; /* function for freeing memory */ ++#endif ++ ++/* New member added in libpng-1.0.13 and 1.2.0 */ ++ png_bytep big_row_buf; /* buffer to save current (unfiltered) row */ ++ ++#ifdef PNG_READ_QUANTIZE_SUPPORTED ++/* The following three members were added at version 1.0.14 and 1.2.4 */ ++ png_bytep quantize_sort; /* working sort array */ ++ png_bytep index_to_palette; /* where the original index currently is ++ in the palette */ ++ png_bytep palette_to_index; /* which original index points to this ++ palette color */ ++#endif ++ ++/* New members added in libpng-1.0.16 and 1.2.6 */ ++ png_byte compression_type; ++ ++#ifdef PNG_USER_LIMITS_SUPPORTED ++ png_uint_32 user_width_max; ++ png_uint_32 user_height_max; ++ ++ /* Added in libpng-1.4.0: Total number of sPLT, text, and unknown ++ * chunks that can be stored (0 means unlimited). ++ */ ++ png_uint_32 user_chunk_cache_max; ++ ++ /* Total memory that a zTXt, sPLT, iTXt, iCCP, or unknown chunk ++ * can occupy when decompressed. 0 means unlimited. ++ */ ++ png_alloc_size_t user_chunk_malloc_max; ++#endif ++ ++/* New member added in libpng-1.0.25 and 1.2.17 */ ++#ifdef PNG_READ_UNKNOWN_CHUNKS_SUPPORTED ++ /* Temporary storage for unknown chunk that the library doesn't recognize, ++ * used while reading the chunk. ++ */ ++ png_unknown_chunk unknown_chunk; ++#endif ++ ++/* New member added in libpng-1.2.26 */ ++ size_t old_big_row_buf_size; ++ ++#ifdef PNG_READ_SUPPORTED ++/* New member added in libpng-1.2.30 */ ++ png_bytep read_buffer; /* buffer for reading chunk data */ ++ png_alloc_size_t read_buffer_size; /* current size of the buffer */ ++#endif ++#ifdef PNG_SEQUENTIAL_READ_SUPPORTED ++ uInt IDAT_read_size; /* limit on read buffer size for IDAT */ ++#endif ++ ++#ifdef PNG_IO_STATE_SUPPORTED ++/* New member added in libpng-1.4.0 */ ++ png_uint_32 io_state; ++#endif ++ ++/* New member added in libpng-1.5.6 */ ++ png_bytep big_prev_row; ++ ++/* New member added in libpng-1.5.7 */ ++ void (*read_filter[PNG_FILTER_VALUE_LAST-1])(png_row_infop row_info, ++ png_bytep row, png_const_bytep prev_row); ++ ++#ifdef PNG_READ_SUPPORTED ++#if defined(PNG_COLORSPACE_SUPPORTED) || defined(PNG_GAMMA_SUPPORTED) ++ png_colorspace colorspace; ++#endif ++#endif ++}; ++#endif /* PNGSTRUCT_H */ +diff --git a/lib/libpng/pngtrans.c b/lib/libpng/pngtrans.c +new file mode 100644 +index 000000000..1100f46eb +--- /dev/null ++++ b/lib/libpng/pngtrans.c +@@ -0,0 +1,864 @@ ++ ++/* pngtrans.c - transforms the data in a row (used by both readers and writers) ++ * ++ * Copyright (c) 2018 Cosmin Truta ++ * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson ++ * Copyright (c) 1996-1997 Andreas Dilger ++ * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc. ++ * ++ * This code is released under the libpng license. ++ * For conditions of distribution and use, see the disclaimer ++ * and license in png.h ++ */ ++ ++#include "pngpriv.h" ++ ++#if defined(PNG_READ_SUPPORTED) || defined(PNG_WRITE_SUPPORTED) ++ ++#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED) ++/* Turn on BGR-to-RGB mapping */ ++void PNGAPI ++png_set_bgr(png_structrp png_ptr) ++{ ++ png_debug(1, "in png_set_bgr"); ++ ++ if (png_ptr == NULL) ++ return; ++ ++ png_ptr->transformations |= PNG_BGR; ++} ++#endif ++ ++#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED) ++/* Turn on 16-bit byte swapping */ ++void PNGAPI ++png_set_swap(png_structrp png_ptr) ++{ ++ png_debug(1, "in png_set_swap"); ++ ++ if (png_ptr == NULL) ++ return; ++ ++ if (png_ptr->bit_depth == 16) ++ png_ptr->transformations |= PNG_SWAP_BYTES; ++} ++#endif ++ ++#if defined(PNG_READ_PACK_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED) ++/* Turn on pixel packing */ ++void PNGAPI ++png_set_packing(png_structrp png_ptr) ++{ ++ png_debug(1, "in png_set_packing"); ++ ++ if (png_ptr == NULL) ++ return; ++ ++ if (png_ptr->bit_depth < 8) ++ { ++ png_ptr->transformations |= PNG_PACK; ++# ifdef PNG_WRITE_SUPPORTED ++ png_ptr->usr_bit_depth = 8; ++# endif ++ } ++} ++#endif ++ ++#if defined(PNG_READ_PACKSWAP_SUPPORTED)||defined(PNG_WRITE_PACKSWAP_SUPPORTED) ++/* Turn on packed pixel swapping */ ++void PNGAPI ++png_set_packswap(png_structrp png_ptr) ++{ ++ png_debug(1, "in png_set_packswap"); ++ ++ if (png_ptr == NULL) ++ return; ++ ++ if (png_ptr->bit_depth < 8) ++ png_ptr->transformations |= PNG_PACKSWAP; ++} ++#endif ++ ++#if defined(PNG_READ_SHIFT_SUPPORTED) || defined(PNG_WRITE_SHIFT_SUPPORTED) ++void PNGAPI ++png_set_shift(png_structrp png_ptr, png_const_color_8p true_bits) ++{ ++ png_debug(1, "in png_set_shift"); ++ ++ if (png_ptr == NULL) ++ return; ++ ++ png_ptr->transformations |= PNG_SHIFT; ++ png_ptr->shift = *true_bits; ++} ++#endif ++ ++#if defined(PNG_READ_INTERLACING_SUPPORTED) || \ ++ defined(PNG_WRITE_INTERLACING_SUPPORTED) ++int PNGAPI ++png_set_interlace_handling(png_structrp png_ptr) ++{ ++ png_debug(1, "in png_set_interlace handling"); ++ ++ if (png_ptr != 0 && png_ptr->interlaced != 0) ++ { ++ png_ptr->transformations |= PNG_INTERLACE; ++ return (7); ++ } ++ ++ return (1); ++} ++#endif ++ ++#if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED) ++/* Add a filler byte on read, or remove a filler or alpha byte on write. ++ * The filler type has changed in v0.95 to allow future 2-byte fillers ++ * for 48-bit input data, as well as to avoid problems with some compilers ++ * that don't like bytes as parameters. ++ */ ++void PNGAPI ++png_set_filler(png_structrp png_ptr, png_uint_32 filler, int filler_loc) ++{ ++ png_debug(1, "in png_set_filler"); ++ ++ if (png_ptr == NULL) ++ return; ++ ++ /* In libpng 1.6 it is possible to determine whether this is a read or write ++ * operation and therefore to do more checking here for a valid call. ++ */ ++ if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0) ++ { ++# ifdef PNG_READ_FILLER_SUPPORTED ++ /* On read png_set_filler is always valid, regardless of the base PNG ++ * format, because other transformations can give a format where the ++ * filler code can execute (basically an 8 or 16-bit component RGB or G ++ * format.) ++ * ++ * NOTE: usr_channels is not used by the read code! (This has led to ++ * confusion in the past.) The filler is only used in the read code. ++ */ ++ png_ptr->filler = (png_uint_16)filler; ++# else ++ png_app_error(png_ptr, "png_set_filler not supported on read"); ++ PNG_UNUSED(filler) /* not used in the write case */ ++ return; ++# endif ++ } ++ ++ else /* write */ ++ { ++# ifdef PNG_WRITE_FILLER_SUPPORTED ++ /* On write the usr_channels parameter must be set correctly at the ++ * start to record the number of channels in the app-supplied data. ++ */ ++ switch (png_ptr->color_type) ++ { ++ case PNG_COLOR_TYPE_RGB: ++ png_ptr->usr_channels = 4; ++ break; ++ ++ case PNG_COLOR_TYPE_GRAY: ++ if (png_ptr->bit_depth >= 8) ++ { ++ png_ptr->usr_channels = 2; ++ break; ++ } ++ ++ else ++ { ++ /* There simply isn't any code in libpng to strip out bits ++ * from bytes when the components are less than a byte in ++ * size! ++ */ ++ png_app_error(png_ptr, ++ "png_set_filler is invalid for" ++ " low bit depth gray output"); ++ return; ++ } ++ ++ default: ++ png_app_error(png_ptr, ++ "png_set_filler: inappropriate color type"); ++ return; ++ } ++# else ++ png_app_error(png_ptr, "png_set_filler not supported on write"); ++ return; ++# endif ++ } ++ ++ /* Here on success - libpng supports the operation, set the transformation ++ * and the flag to say where the filler channel is. ++ */ ++ png_ptr->transformations |= PNG_FILLER; ++ ++ if (filler_loc == PNG_FILLER_AFTER) ++ png_ptr->flags |= PNG_FLAG_FILLER_AFTER; ++ ++ else ++ png_ptr->flags &= ~PNG_FLAG_FILLER_AFTER; ++} ++ ++/* Added to libpng-1.2.7 */ ++void PNGAPI ++png_set_add_alpha(png_structrp png_ptr, png_uint_32 filler, int filler_loc) ++{ ++ png_debug(1, "in png_set_add_alpha"); ++ ++ if (png_ptr == NULL) ++ return; ++ ++ png_set_filler(png_ptr, filler, filler_loc); ++ /* The above may fail to do anything. */ ++ if ((png_ptr->transformations & PNG_FILLER) != 0) ++ png_ptr->transformations |= PNG_ADD_ALPHA; ++} ++ ++#endif ++ ++#if defined(PNG_READ_SWAP_ALPHA_SUPPORTED) || \ ++ defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED) ++void PNGAPI ++png_set_swap_alpha(png_structrp png_ptr) ++{ ++ png_debug(1, "in png_set_swap_alpha"); ++ ++ if (png_ptr == NULL) ++ return; ++ ++ png_ptr->transformations |= PNG_SWAP_ALPHA; ++} ++#endif ++ ++#if defined(PNG_READ_INVERT_ALPHA_SUPPORTED) || \ ++ defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED) ++void PNGAPI ++png_set_invert_alpha(png_structrp png_ptr) ++{ ++ png_debug(1, "in png_set_invert_alpha"); ++ ++ if (png_ptr == NULL) ++ return; ++ ++ png_ptr->transformations |= PNG_INVERT_ALPHA; ++} ++#endif ++ ++#if defined(PNG_READ_INVERT_SUPPORTED) || defined(PNG_WRITE_INVERT_SUPPORTED) ++void PNGAPI ++png_set_invert_mono(png_structrp png_ptr) ++{ ++ png_debug(1, "in png_set_invert_mono"); ++ ++ if (png_ptr == NULL) ++ return; ++ ++ png_ptr->transformations |= PNG_INVERT_MONO; ++} ++ ++/* Invert monochrome grayscale data */ ++void /* PRIVATE */ ++png_do_invert(png_row_infop row_info, png_bytep row) ++{ ++ png_debug(1, "in png_do_invert"); ++ ++ /* This test removed from libpng version 1.0.13 and 1.2.0: ++ * if (row_info->bit_depth == 1 && ++ */ ++ if (row_info->color_type == PNG_COLOR_TYPE_GRAY) ++ { ++ png_bytep rp = row; ++ size_t i; ++ size_t istop = row_info->rowbytes; ++ ++ for (i = 0; i < istop; i++) ++ { ++ *rp = (png_byte)(~(*rp)); ++ rp++; ++ } ++ } ++ ++ else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA && ++ row_info->bit_depth == 8) ++ { ++ png_bytep rp = row; ++ size_t i; ++ size_t istop = row_info->rowbytes; ++ ++ for (i = 0; i < istop; i += 2) ++ { ++ *rp = (png_byte)(~(*rp)); ++ rp += 2; ++ } ++ } ++ ++#ifdef PNG_16BIT_SUPPORTED ++ else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA && ++ row_info->bit_depth == 16) ++ { ++ png_bytep rp = row; ++ size_t i; ++ size_t istop = row_info->rowbytes; ++ ++ for (i = 0; i < istop; i += 4) ++ { ++ *rp = (png_byte)(~(*rp)); ++ *(rp + 1) = (png_byte)(~(*(rp + 1))); ++ rp += 4; ++ } ++ } ++#endif ++} ++#endif ++ ++#ifdef PNG_16BIT_SUPPORTED ++#if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED) ++/* Swaps byte order on 16-bit depth images */ ++void /* PRIVATE */ ++png_do_swap(png_row_infop row_info, png_bytep row) ++{ ++ png_debug(1, "in png_do_swap"); ++ ++ if (row_info->bit_depth == 16) ++ { ++ png_bytep rp = row; ++ png_uint_32 i; ++ png_uint_32 istop= row_info->width * row_info->channels; ++ ++ for (i = 0; i < istop; i++, rp += 2) ++ { ++#ifdef PNG_BUILTIN_BSWAP16_SUPPORTED ++ /* Feature added to libpng-1.6.11 for testing purposes, not ++ * enabled by default. ++ */ ++ *(png_uint_16*)rp = __builtin_bswap16(*(png_uint_16*)rp); ++#else ++ png_byte t = *rp; ++ *rp = *(rp + 1); ++ *(rp + 1) = t; ++#endif ++ } ++ } ++} ++#endif ++#endif ++ ++#if defined(PNG_READ_PACKSWAP_SUPPORTED)||defined(PNG_WRITE_PACKSWAP_SUPPORTED) ++static const png_byte onebppswaptable[256] = { ++ 0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, ++ 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0, ++ 0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, ++ 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8, ++ 0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4, ++ 0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4, ++ 0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC, ++ 0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC, ++ 0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2, ++ 0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2, ++ 0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA, ++ 0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA, ++ 0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0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, 0x6E, 0xAE, 0xEE, 0x3E, 0x7E, 0xBE, 0xFE, ++ 0x03, 0x43, 0x83, 0xC3, 0x13, 0x53, 0x93, 0xD3, ++ 0x23, 0x63, 0xA3, 0xE3, 0x33, 0x73, 0xB3, 0xF3, ++ 0x07, 0x47, 0x87, 0xC7, 0x17, 0x57, 0x97, 0xD7, ++ 0x27, 0x67, 0xA7, 0xE7, 0x37, 0x77, 0xB7, 0xF7, ++ 0x0B, 0x4B, 0x8B, 0xCB, 0x1B, 0x5B, 0x9B, 0xDB, ++ 0x2B, 0x6B, 0xAB, 0xEB, 0x3B, 0x7B, 0xBB, 0xFB, ++ 0x0F, 0x4F, 0x8F, 0xCF, 0x1F, 0x5F, 0x9F, 0xDF, ++ 0x2F, 0x6F, 0xAF, 0xEF, 0x3F, 0x7F, 0xBF, 0xFF ++}; ++ ++static const png_byte fourbppswaptable[256] = { ++ 0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, ++ 0x80, 0x90, 0xA0, 0xB0, 0xC0, 0xD0, 0xE0, 0xF0, ++ 0x01, 0x11, 0x21, 0x31, 0x41, 0x51, 0x61, 0x71, ++ 0x81, 0x91, 0xA1, 0xB1, 0xC1, 0xD1, 0xE1, 0xF1, ++ 0x02, 0x12, 0x22, 0x32, 0x42, 0x52, 0x62, 0x72, ++ 0x82, 0x92, 0xA2, 0xB2, 0xC2, 0xD2, 0xE2, 0xF2, ++ 0x03, 0x13, 0x23, 0x33, 0x43, 0x53, 0x63, 0x73, ++ 0x83, 0x93, 0xA3, 0xB3, 0xC3, 0xD3, 0xE3, 0xF3, ++ 0x04, 0x14, 0x24, 0x34, 0x44, 0x54, 0x64, 0x74, ++ 0x84, 0x94, 0xA4, 0xB4, 0xC4, 0xD4, 0xE4, 0xF4, ++ 0x05, 0x15, 0x25, 0x35, 0x45, 0x55, 0x65, 0x75, ++ 0x85, 0x95, 0xA5, 0xB5, 0xC5, 0xD5, 0xE5, 0xF5, ++ 0x06, 0x16, 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png_bytep row) ++{ ++ png_debug(1, "in png_do_packswap"); ++ ++ if (row_info->bit_depth < 8) ++ { ++ png_bytep rp; ++ png_const_bytep end, table; ++ ++ end = row + row_info->rowbytes; ++ ++ if (row_info->bit_depth == 1) ++ table = onebppswaptable; ++ ++ else if (row_info->bit_depth == 2) ++ table = twobppswaptable; ++ ++ else if (row_info->bit_depth == 4) ++ table = fourbppswaptable; ++ ++ else ++ return; ++ ++ for (rp = row; rp < end; rp++) ++ *rp = table[*rp]; ++ } ++} ++#endif /* PACKSWAP || WRITE_PACKSWAP */ ++ ++#if defined(PNG_WRITE_FILLER_SUPPORTED) || \ ++ defined(PNG_READ_STRIP_ALPHA_SUPPORTED) ++/* Remove a channel - this used to be 'png_do_strip_filler' but it used a ++ * somewhat weird combination of flags to determine what to do. All the calls ++ * to png_do_strip_filler are changed in 1.5.2 to call this instead with the ++ * correct arguments. ++ * ++ * The routine isn't general - the channel must be the channel at the start or ++ * end (not in the middle) of each pixel. ++ */ ++void /* PRIVATE */ ++png_do_strip_channel(png_row_infop row_info, png_bytep row, int at_start) ++{ ++ png_bytep sp = row; /* source pointer */ ++ png_bytep dp = row; /* destination pointer */ ++ png_bytep ep = row + row_info->rowbytes; /* One beyond end of row */ ++ ++ /* At the start sp will point to the first byte to copy and dp to where ++ * it is copied to. ep always points just beyond the end of the row, so ++ * the loop simply copies (channels-1) channels until sp reaches ep. ++ * ++ * at_start: 0 -- convert AG, XG, ARGB, XRGB, AAGG, XXGG, etc. ++ * nonzero -- convert GA, GX, RGBA, RGBX, GGAA, RRGGBBXX, etc. ++ */ ++ ++ /* GA, GX, XG cases */ ++ if (row_info->channels == 2) ++ { ++ if (row_info->bit_depth == 8) ++ { ++ if (at_start != 0) /* Skip initial filler */ ++ ++sp; ++ else /* Skip initial channel and, for sp, the filler */ ++ { ++ sp += 2; ++dp; ++ } ++ ++ /* For a 1 pixel wide image there is nothing to do */ ++ while (sp < ep) ++ { ++ *dp++ = *sp; sp += 2; ++ } ++ ++ row_info->pixel_depth = 8; ++ } ++ ++ else if (row_info->bit_depth == 16) ++ { ++ if (at_start != 0) /* Skip initial filler */ ++ sp += 2; ++ else /* Skip initial channel and, for sp, the filler */ ++ { ++ sp += 4; dp += 2; ++ } ++ ++ while (sp < ep) ++ { ++ *dp++ = *sp++; *dp++ = *sp; sp += 3; ++ } ++ ++ row_info->pixel_depth = 16; ++ } ++ ++ else ++ return; /* bad bit depth */ ++ ++ row_info->channels = 1; ++ ++ /* Finally fix the color type if it records an alpha channel */ ++ if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ++ row_info->color_type = PNG_COLOR_TYPE_GRAY; ++ } ++ ++ /* RGBA, RGBX, XRGB cases */ ++ else if (row_info->channels == 4) ++ { ++ if (row_info->bit_depth == 8) ++ { ++ if (at_start != 0) /* Skip initial filler */ ++ ++sp; ++ else /* Skip initial channels and, for sp, the filler */ ++ { ++ sp += 4; dp += 3; ++ } ++ ++ /* Note that the loop adds 3 to dp and 4 to sp each time. */ ++ while (sp < ep) ++ { ++ *dp++ = *sp++; *dp++ = *sp++; *dp++ = *sp; sp += 2; ++ } ++ ++ row_info->pixel_depth = 24; ++ } ++ ++ else if (row_info->bit_depth == 16) ++ { ++ if (at_start != 0) /* Skip initial filler */ ++ sp += 2; ++ else /* Skip initial channels and, for sp, the filler */ ++ { ++ sp += 8; dp += 6; ++ } ++ ++ while (sp < ep) ++ { ++ /* Copy 6 bytes, skip 2 */ ++ *dp++ = *sp++; *dp++ = *sp++; ++ *dp++ = *sp++; *dp++ = *sp++; ++ *dp++ = *sp++; *dp++ = *sp; sp += 3; ++ } ++ ++ row_info->pixel_depth = 48; ++ } ++ ++ else ++ return; /* bad bit depth */ ++ ++ row_info->channels = 3; ++ ++ /* Finally fix the color type if it records an alpha channel */ ++ if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) ++ row_info->color_type = PNG_COLOR_TYPE_RGB; ++ } ++ ++ else ++ return; /* The filler channel has gone already */ ++ ++ /* Fix the rowbytes value. */ ++ row_info->rowbytes = (size_t)(dp-row); ++} ++#endif ++ ++#if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED) ++/* Swaps red and blue bytes within a pixel */ ++void /* PRIVATE */ ++png_do_bgr(png_row_infop row_info, png_bytep row) ++{ ++ png_debug(1, "in png_do_bgr"); ++ ++ if ((row_info->color_type & PNG_COLOR_MASK_COLOR) != 0) ++ { ++ png_uint_32 row_width = row_info->width; ++ if (row_info->bit_depth == 8) ++ { ++ if (row_info->color_type == PNG_COLOR_TYPE_RGB) ++ { ++ png_bytep rp; ++ png_uint_32 i; ++ ++ for (i = 0, rp = row; i < row_width; i++, rp += 3) ++ { ++ png_byte save = *rp; ++ *rp = *(rp + 2); ++ *(rp + 2) = save; ++ } ++ } ++ ++ else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) ++ { ++ png_bytep rp; ++ png_uint_32 i; ++ ++ for (i = 0, rp = row; i < row_width; i++, rp += 4) ++ { ++ png_byte save = *rp; ++ *rp = *(rp + 2); ++ *(rp + 2) = save; ++ } ++ } ++ } ++ ++#ifdef PNG_16BIT_SUPPORTED ++ else if (row_info->bit_depth == 16) ++ { ++ if (row_info->color_type == PNG_COLOR_TYPE_RGB) ++ { ++ png_bytep rp; ++ png_uint_32 i; ++ ++ for (i = 0, rp = row; i < row_width; i++, rp += 6) ++ { ++ png_byte save = *rp; ++ *rp = *(rp + 4); ++ *(rp + 4) = save; ++ save = *(rp + 1); ++ *(rp + 1) = *(rp + 5); ++ *(rp + 5) = save; ++ } ++ } ++ ++ else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) ++ { ++ png_bytep rp; ++ png_uint_32 i; ++ ++ for (i = 0, rp = row; i < row_width; i++, rp += 8) ++ { ++ png_byte save = *rp; ++ *rp = *(rp + 4); ++ *(rp + 4) = save; ++ save = *(rp + 1); ++ *(rp + 1) = *(rp + 5); ++ *(rp + 5) = save; ++ } ++ } ++ } ++#endif ++ } ++} ++#endif /* READ_BGR || WRITE_BGR */ ++ ++#if defined(PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED) || \ ++ defined(PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED) ++/* Added at libpng-1.5.10 */ ++void /* PRIVATE */ ++png_do_check_palette_indexes(png_structrp png_ptr, png_row_infop row_info) ++{ ++ if (png_ptr->num_palette < (1 << row_info->bit_depth) && ++ png_ptr->num_palette > 0) /* num_palette can be 0 in MNG files */ ++ { ++ /* Calculations moved outside switch in an attempt to stop different ++ * compiler warnings. 'padding' is in *bits* within the last byte, it is ++ * an 'int' because pixel_depth becomes an 'int' in the expression below, ++ * and this calculation is used because it avoids warnings that other ++ * forms produced on either GCC or MSVC. ++ */ ++ int padding = PNG_PADBITS(row_info->pixel_depth, row_info->width); ++ png_bytep rp = png_ptr->row_buf + row_info->rowbytes - 1; ++ ++ switch (row_info->bit_depth) ++ { ++ case 1: ++ { ++ /* in this case, all bytes must be 0 so we don't need ++ * to unpack the pixels except for the rightmost one. ++ */ ++ for (; rp > png_ptr->row_buf; rp--) ++ { ++ if ((*rp >> padding) != 0) ++ png_ptr->num_palette_max = 1; ++ padding = 0; ++ } ++ ++ break; ++ } ++ ++ case 2: ++ { ++ for (; rp > png_ptr->row_buf; rp--) ++ { ++ int i = ((*rp >> padding) & 0x03); ++ ++ if (i > png_ptr->num_palette_max) ++ png_ptr->num_palette_max = i; ++ ++ i = (((*rp >> padding) >> 2) & 0x03); ++ ++ if (i > png_ptr->num_palette_max) ++ png_ptr->num_palette_max = i; ++ ++ i = (((*rp >> padding) >> 4) & 0x03); ++ ++ if (i > png_ptr->num_palette_max) ++ png_ptr->num_palette_max = i; ++ ++ i = (((*rp >> padding) >> 6) & 0x03); ++ ++ if (i > png_ptr->num_palette_max) ++ png_ptr->num_palette_max = i; ++ ++ padding = 0; ++ } ++ ++ break; ++ } ++ ++ case 4: ++ { ++ for (; rp > png_ptr->row_buf; rp--) ++ { ++ int i = ((*rp >> padding) & 0x0f); ++ ++ if (i > png_ptr->num_palette_max) ++ png_ptr->num_palette_max = i; ++ ++ i = (((*rp >> padding) >> 4) & 0x0f); ++ ++ if (i > png_ptr->num_palette_max) ++ png_ptr->num_palette_max = i; ++ ++ padding = 0; ++ } ++ ++ break; ++ } ++ ++ case 8: ++ { ++ for (; rp > png_ptr->row_buf; rp--) ++ { ++ if (*rp > png_ptr->num_palette_max) ++ png_ptr->num_palette_max = (int) *rp; ++ } ++ ++ break; ++ } ++ ++ default: ++ break; ++ } ++ } ++} ++#endif /* CHECK_FOR_INVALID_INDEX */ ++ ++#if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \ ++ defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED) ++#ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED ++void PNGAPI ++png_set_user_transform_info(png_structrp png_ptr, png_voidp ++ user_transform_ptr, int user_transform_depth, int user_transform_channels) ++{ ++ png_debug(1, "in png_set_user_transform_info"); ++ ++ if (png_ptr == NULL) ++ return; ++ ++#ifdef PNG_READ_USER_TRANSFORM_SUPPORTED ++ if ((png_ptr->mode & PNG_IS_READ_STRUCT) != 0 && ++ (png_ptr->flags & PNG_FLAG_ROW_INIT) != 0) ++ { ++ png_app_error(png_ptr, ++ "info change after png_start_read_image or png_read_update_info"); ++ return; ++ } ++#endif ++ ++ png_ptr->user_transform_ptr = user_transform_ptr; ++ png_ptr->user_transform_depth = (png_byte)user_transform_depth; ++ png_ptr->user_transform_channels = (png_byte)user_transform_channels; ++} ++#endif ++ ++/* This function returns a pointer to the user_transform_ptr associated with ++ * the user transform functions. The application should free any memory ++ * associated with this pointer before png_write_destroy and png_read_destroy ++ * are called. ++ */ ++#ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED ++png_voidp PNGAPI ++png_get_user_transform_ptr(png_const_structrp png_ptr) ++{ ++ if (png_ptr == NULL) ++ return (NULL); ++ ++ return png_ptr->user_transform_ptr; ++} ++#endif ++ ++#ifdef PNG_USER_TRANSFORM_INFO_SUPPORTED ++png_uint_32 PNGAPI ++png_get_current_row_number(png_const_structrp png_ptr) ++{ ++ /* See the comments in png.h - this is the sub-image row when reading an ++ * interlaced image. ++ */ ++ if (png_ptr != NULL) ++ return png_ptr->row_number; ++ ++ return PNG_UINT_32_MAX; /* help the app not to fail silently */ ++} ++ ++png_byte PNGAPI ++png_get_current_pass_number(png_const_structrp png_ptr) ++{ ++ if (png_ptr != NULL) ++ return png_ptr->pass; ++ return 8; /* invalid */ ++} ++#endif /* USER_TRANSFORM_INFO */ ++#endif /* READ_USER_TRANSFORM || WRITE_USER_TRANSFORM */ ++#endif /* READ || WRITE */ +diff --git a/lib/libpng/pngwio.c b/lib/libpng/pngwio.c +new file mode 100644 +index 000000000..10e919dd0 +--- /dev/null ++++ b/lib/libpng/pngwio.c +@@ -0,0 +1,168 @@ ++ ++/* pngwio.c - functions for data output ++ * ++ * Copyright (c) 2018 Cosmin Truta ++ * Copyright (c) 1998-2002,2004,2006-2014,2016,2018 Glenn Randers-Pehrson ++ * Copyright (c) 1996-1997 Andreas Dilger ++ * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc. ++ * ++ * This code is released under the libpng license. ++ * For conditions of distribution and use, see the disclaimer ++ * and license in png.h ++ * ++ * This file provides a location for all output. Users who need ++ * special handling are expected to write functions that have the same ++ * arguments as these and perform similar functions, but that possibly ++ * use different output methods. Note that you shouldn't change these ++ * functions, but rather write replacement functions and then change ++ * them at run time with png_set_write_fn(...). ++ */ ++ ++#include "pngpriv.h" ++ ++#ifdef PNG_WRITE_SUPPORTED ++ ++/* Write the data to whatever output you are using. The default routine ++ * writes to a file pointer. Note that this routine sometimes gets called ++ * with very small lengths, so you should implement some kind of simple ++ * buffering if you are using unbuffered writes. This should never be asked ++ * to write more than 64K on a 16-bit machine. ++ */ ++ ++void /* PRIVATE */ ++png_write_data(png_structrp png_ptr, png_const_bytep data, size_t length) ++{ ++ /* NOTE: write_data_fn must not change the buffer! */ ++ if (png_ptr->write_data_fn != NULL ) ++ (*(png_ptr->write_data_fn))(png_ptr, png_constcast(png_bytep,data), ++ length); ++ ++ else ++ png_error(png_ptr, "Call to NULL write function"); ++} ++ ++#ifdef PNG_STDIO_SUPPORTED ++/* This is the function that does the actual writing of data. If you are ++ * not writing to a standard C stream, you should create a replacement ++ * write_data function and use it at run time with png_set_write_fn(), rather ++ * than changing the library. ++ */ ++void PNGCBAPI ++png_default_write_data(png_structp png_ptr, png_bytep data, size_t length) ++{ ++ size_t check; ++ ++ if (png_ptr == NULL) ++ return; ++ ++ check = fwrite(data, 1, length, (png_FILE_p)(png_ptr->io_ptr)); ++ ++ if (check != length) ++ png_error(png_ptr, "Write Error"); ++} ++#endif ++ ++/* This function is called to output any data pending writing (normally ++ * to disk). After png_flush is called, there should be no data pending ++ * writing in any buffers. ++ */ ++#ifdef PNG_WRITE_FLUSH_SUPPORTED ++void /* PRIVATE */ ++png_flush(png_structrp png_ptr) ++{ ++ if (png_ptr->output_flush_fn != NULL) ++ (*(png_ptr->output_flush_fn))(png_ptr); ++} ++ ++# ifdef PNG_STDIO_SUPPORTED ++void PNGCBAPI ++png_default_flush(png_structp png_ptr) ++{ ++ png_FILE_p io_ptr; ++ ++ if (png_ptr == NULL) ++ return; ++ ++ io_ptr = png_voidcast(png_FILE_p, (png_ptr->io_ptr)); ++ fflush(io_ptr); ++} ++# endif ++#endif ++ ++/* This function allows the application to supply new output functions for ++ * libpng if standard C streams aren't being used. ++ * ++ * This function takes as its arguments: ++ * png_ptr - pointer to a png output data structure ++ * io_ptr - pointer to user supplied structure containing info about ++ * the output functions. May be NULL. ++ * write_data_fn - pointer to a new output function that takes as its ++ * arguments a pointer to a png_struct, a pointer to ++ * data to be written, and a 32-bit unsigned int that is ++ * the number of bytes to be written. The new write ++ * function should call png_error(png_ptr, "Error msg") ++ * to exit and output any fatal error messages. May be ++ * NULL, in which case libpng's default function will ++ * be used. ++ * flush_data_fn - pointer to a new flush function that takes as its ++ * arguments a pointer to a png_struct. After a call to ++ * the flush function, there should be no data in any buffers ++ * or pending transmission. If the output method doesn't do ++ * any buffering of output, a function prototype must still be ++ * supplied although it doesn't have to do anything. If ++ * PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile ++ * time, output_flush_fn will be ignored, although it must be ++ * supplied for compatibility. May be NULL, in which case ++ * libpng's default function will be used, if ++ * PNG_WRITE_FLUSH_SUPPORTED is defined. This is not ++ * a good idea if io_ptr does not point to a standard ++ * *FILE structure. ++ */ ++void PNGAPI ++png_set_write_fn(png_structrp png_ptr, png_voidp io_ptr, ++ png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn) ++{ ++ if (png_ptr == NULL) ++ return; ++ ++ png_ptr->io_ptr = io_ptr; ++ ++#ifdef PNG_STDIO_SUPPORTED ++ if (write_data_fn != NULL) ++ png_ptr->write_data_fn = write_data_fn; ++ ++ else ++ png_ptr->write_data_fn = png_default_write_data; ++#else ++ png_ptr->write_data_fn = write_data_fn; ++#endif ++ ++#ifdef PNG_WRITE_FLUSH_SUPPORTED ++# ifdef PNG_STDIO_SUPPORTED ++ ++ if (output_flush_fn != NULL) ++ png_ptr->output_flush_fn = output_flush_fn; ++ ++ else ++ png_ptr->output_flush_fn = png_default_flush; ++ ++# else ++ png_ptr->output_flush_fn = output_flush_fn; ++# endif ++#else ++ PNG_UNUSED(output_flush_fn) ++#endif /* WRITE_FLUSH */ ++ ++#ifdef PNG_READ_SUPPORTED ++ /* It is an error to read while writing a png file */ ++ if (png_ptr->read_data_fn != NULL) ++ { ++ png_ptr->read_data_fn = NULL; ++ ++ png_warning(png_ptr, ++ "Can't set both read_data_fn and write_data_fn in the" ++ " same structure"); ++ } ++#endif ++} ++#endif /* WRITE */ +diff --git a/lib/libpng/pngwrite.c b/lib/libpng/pngwrite.c +new file mode 100644 +index 000000000..59377a4dd +--- /dev/null ++++ b/lib/libpng/pngwrite.c +@@ -0,0 +1,2395 @@ ++ ++/* pngwrite.c - general routines to write a PNG file ++ * ++ * Copyright (c) 2018-2019 Cosmin Truta ++ * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson ++ * Copyright (c) 1996-1997 Andreas Dilger ++ * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc. ++ * ++ * This code is released under the libpng license. ++ * For conditions of distribution and use, see the disclaimer ++ * and license in png.h ++ */ ++ ++#include "pngpriv.h" ++#ifdef PNG_SIMPLIFIED_WRITE_STDIO_SUPPORTED ++# include ++#endif /* SIMPLIFIED_WRITE_STDIO */ ++ ++#ifdef PNG_WRITE_SUPPORTED ++ ++#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED ++/* Write out all the unknown chunks for the current given location */ ++static void ++write_unknown_chunks(png_structrp png_ptr, png_const_inforp info_ptr, ++ unsigned int where) ++{ ++ if (info_ptr->unknown_chunks_num != 0) ++ { ++ png_const_unknown_chunkp up; ++ ++ png_debug(5, "writing extra chunks"); ++ ++ for (up = info_ptr->unknown_chunks; ++ up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num; ++ ++up) ++ if ((up->location & where) != 0) ++ { ++ /* If per-chunk unknown chunk handling is enabled use it, otherwise ++ * just write the chunks the application has set. ++ */ ++#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED ++ int keep = png_handle_as_unknown(png_ptr, up->name); ++ ++ /* NOTE: this code is radically different from the read side in the ++ * matter of handling an ancillary unknown chunk. In the read side ++ * the default behavior is to discard it, in the code below the default ++ * behavior is to write it. Critical chunks are, however, only ++ * written if explicitly listed or if the default is set to write all ++ * unknown chunks. ++ * ++ * The default handling is also slightly weird - it is not possible to ++ * stop the writing of all unsafe-to-copy chunks! ++ * ++ * TODO: REVIEW: this would seem to be a bug. ++ */ ++ if (keep != PNG_HANDLE_CHUNK_NEVER && ++ ((up->name[3] & 0x20) /* safe-to-copy overrides everything */ || ++ keep == PNG_HANDLE_CHUNK_ALWAYS || ++ (keep == PNG_HANDLE_CHUNK_AS_DEFAULT && ++ png_ptr->unknown_default == PNG_HANDLE_CHUNK_ALWAYS))) ++#endif ++ { ++ /* TODO: review, what is wrong with a zero length unknown chunk? */ ++ if (up->size == 0) ++ png_warning(png_ptr, "Writing zero-length unknown chunk"); ++ ++ png_write_chunk(png_ptr, up->name, up->data, up->size); ++ } ++ } ++ } ++} ++#endif /* WRITE_UNKNOWN_CHUNKS */ ++ ++/* Writes all the PNG information. This is the suggested way to use the ++ * library. If you have a new chunk to add, make a function to write it, ++ * and put it in the correct location here. If you want the chunk written ++ * after the image data, put it in png_write_end(). I strongly encourage ++ * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing ++ * the chunk, as that will keep the code from breaking if you want to just ++ * write a plain PNG file. If you have long comments, I suggest writing ++ * them in png_write_end(), and compressing them. ++ */ ++void PNGAPI ++png_write_info_before_PLTE(png_structrp png_ptr, png_const_inforp info_ptr) ++{ ++ png_debug(1, "in png_write_info_before_PLTE"); ++ ++ if (png_ptr == NULL || info_ptr == NULL) ++ return; ++ ++ if ((png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE) == 0) ++ { ++ /* Write PNG signature */ ++ png_write_sig(png_ptr); ++ ++#ifdef PNG_MNG_FEATURES_SUPPORTED ++ if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) != 0 && \ ++ png_ptr->mng_features_permitted != 0) ++ { ++ png_warning(png_ptr, ++ "MNG features are not allowed in a PNG datastream"); ++ png_ptr->mng_features_permitted = 0; ++ } ++#endif ++ ++ /* Write IHDR information. */ ++ png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height, ++ info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type, ++ info_ptr->filter_type, ++#ifdef PNG_WRITE_INTERLACING_SUPPORTED ++ info_ptr->interlace_type ++#else ++ 0 ++#endif ++ ); ++ ++ /* The rest of these check to see if the valid field has the appropriate ++ * flag set, and if it does, writes the chunk. ++ * ++ * 1.6.0: COLORSPACE support controls the writing of these chunks too, and ++ * the chunks will be written if the WRITE routine is there and ++ * information * is available in the COLORSPACE. (See ++ * png_colorspace_sync_info in png.c for where the valid flags get set.) ++ * ++ * Under certain circumstances the colorspace can be invalidated without ++ * syncing the info_struct 'valid' flags; this happens if libpng detects ++ * an error and calls png_error while the color space is being set, yet ++ * the application continues writing the PNG. So check the 'invalid' ++ * flag here too. ++ */ ++#ifdef PNG_GAMMA_SUPPORTED ++# ifdef PNG_WRITE_gAMA_SUPPORTED ++ if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 && ++ (info_ptr->colorspace.flags & PNG_COLORSPACE_FROM_gAMA) != 0 && ++ (info_ptr->valid & PNG_INFO_gAMA) != 0) ++ png_write_gAMA_fixed(png_ptr, info_ptr->colorspace.gamma); ++# endif ++#endif ++ ++#ifdef PNG_COLORSPACE_SUPPORTED ++ /* Write only one of sRGB or an ICC profile. If a profile was supplied ++ * and it matches one of the known sRGB ones issue a warning. ++ */ ++# ifdef PNG_WRITE_iCCP_SUPPORTED ++ if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 && ++ (info_ptr->valid & PNG_INFO_iCCP) != 0) ++ { ++# ifdef PNG_WRITE_sRGB_SUPPORTED ++ if ((info_ptr->valid & PNG_INFO_sRGB) != 0) ++ png_app_warning(png_ptr, ++ "profile matches sRGB but writing iCCP instead"); ++# endif ++ ++ png_write_iCCP(png_ptr, info_ptr->iccp_name, ++ info_ptr->iccp_profile); ++ } ++# ifdef PNG_WRITE_sRGB_SUPPORTED ++ else ++# endif ++# endif ++ ++# ifdef PNG_WRITE_sRGB_SUPPORTED ++ if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 && ++ (info_ptr->valid & PNG_INFO_sRGB) != 0) ++ png_write_sRGB(png_ptr, info_ptr->colorspace.rendering_intent); ++# endif /* WRITE_sRGB */ ++#endif /* COLORSPACE */ ++ ++#ifdef PNG_WRITE_sBIT_SUPPORTED ++ if ((info_ptr->valid & PNG_INFO_sBIT) != 0) ++ png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type); ++#endif ++ ++#ifdef PNG_COLORSPACE_SUPPORTED ++# ifdef PNG_WRITE_cHRM_SUPPORTED ++ if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 && ++ (info_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM) != 0 && ++ (info_ptr->valid & PNG_INFO_cHRM) != 0) ++ png_write_cHRM_fixed(png_ptr, &info_ptr->colorspace.end_points_xy); ++# endif ++#endif ++ ++#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED ++ write_unknown_chunks(png_ptr, info_ptr, PNG_HAVE_IHDR); ++#endif ++ ++ png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE; ++ } ++} ++ ++void PNGAPI ++png_write_info(png_structrp png_ptr, png_const_inforp info_ptr) ++{ ++#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED) ++ int i; ++#endif ++ ++ png_debug(1, "in png_write_info"); ++ ++ if (png_ptr == NULL || info_ptr == NULL) ++ return; ++ ++ png_write_info_before_PLTE(png_ptr, info_ptr); ++ ++ if ((info_ptr->valid & PNG_INFO_PLTE) != 0) ++ png_write_PLTE(png_ptr, info_ptr->palette, ++ (png_uint_32)info_ptr->num_palette); ++ ++ else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ++ png_error(png_ptr, "Valid palette required for paletted images"); ++ ++#ifdef PNG_WRITE_tRNS_SUPPORTED ++ if ((info_ptr->valid & PNG_INFO_tRNS) !=0) ++ { ++#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED ++ /* Invert the alpha channel (in tRNS) */ ++ if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0 && ++ info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ++ { ++ int j, jend; ++ ++ jend = info_ptr->num_trans; ++ if (jend > PNG_MAX_PALETTE_LENGTH) ++ jend = PNG_MAX_PALETTE_LENGTH; ++ ++ for (j = 0; jtrans_alpha[j] = ++ (png_byte)(255 - info_ptr->trans_alpha[j]); ++ } ++#endif ++ png_write_tRNS(png_ptr, info_ptr->trans_alpha, &(info_ptr->trans_color), ++ info_ptr->num_trans, info_ptr->color_type); ++ } ++#endif ++#ifdef PNG_WRITE_bKGD_SUPPORTED ++ if ((info_ptr->valid & PNG_INFO_bKGD) != 0) ++ png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type); ++#endif ++ ++#ifdef PNG_WRITE_eXIf_SUPPORTED ++ if ((info_ptr->valid & PNG_INFO_eXIf) != 0) ++ png_write_eXIf(png_ptr, info_ptr->exif, info_ptr->num_exif); ++#endif ++ ++#ifdef PNG_WRITE_hIST_SUPPORTED ++ if ((info_ptr->valid & PNG_INFO_hIST) != 0) ++ png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette); ++#endif ++ ++#ifdef PNG_WRITE_oFFs_SUPPORTED ++ if ((info_ptr->valid & PNG_INFO_oFFs) != 0) ++ png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset, ++ info_ptr->offset_unit_type); ++#endif ++ ++#ifdef PNG_WRITE_pCAL_SUPPORTED ++ if ((info_ptr->valid & PNG_INFO_pCAL) != 0) ++ png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0, ++ info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams, ++ info_ptr->pcal_units, info_ptr->pcal_params); ++#endif ++ ++#ifdef PNG_WRITE_sCAL_SUPPORTED ++ if ((info_ptr->valid & PNG_INFO_sCAL) != 0) ++ png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit, ++ info_ptr->scal_s_width, info_ptr->scal_s_height); ++#endif /* sCAL */ ++ ++#ifdef PNG_WRITE_pHYs_SUPPORTED ++ if ((info_ptr->valid & PNG_INFO_pHYs) != 0) ++ png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit, ++ info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type); ++#endif /* pHYs */ ++ ++#ifdef PNG_WRITE_tIME_SUPPORTED ++ if ((info_ptr->valid & PNG_INFO_tIME) != 0) ++ { ++ png_write_tIME(png_ptr, &(info_ptr->mod_time)); ++ png_ptr->mode |= PNG_WROTE_tIME; ++ } ++#endif /* tIME */ ++ ++#ifdef PNG_WRITE_sPLT_SUPPORTED ++ if ((info_ptr->valid & PNG_INFO_sPLT) != 0) ++ for (i = 0; i < (int)info_ptr->splt_palettes_num; i++) ++ png_write_sPLT(png_ptr, info_ptr->splt_palettes + i); ++#endif /* sPLT */ ++ ++#ifdef PNG_WRITE_TEXT_SUPPORTED ++ /* Check to see if we need to write text chunks */ ++ for (i = 0; i < info_ptr->num_text; i++) ++ { ++ png_debug2(2, "Writing header text chunk %d, type %d", i, ++ info_ptr->text[i].compression); ++ /* An internationalized chunk? */ ++ if (info_ptr->text[i].compression > 0) ++ { ++#ifdef PNG_WRITE_iTXt_SUPPORTED ++ /* Write international chunk */ ++ png_write_iTXt(png_ptr, ++ info_ptr->text[i].compression, ++ info_ptr->text[i].key, ++ info_ptr->text[i].lang, ++ info_ptr->text[i].lang_key, ++ info_ptr->text[i].text); ++ /* Mark this chunk as written */ ++ if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE) ++ info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR; ++ else ++ info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR; ++#else ++ png_warning(png_ptr, "Unable to write international text"); ++#endif ++ } ++ ++ /* If we want a compressed text chunk */ ++ else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt) ++ { ++#ifdef PNG_WRITE_zTXt_SUPPORTED ++ /* Write compressed chunk */ ++ png_write_zTXt(png_ptr, info_ptr->text[i].key, ++ info_ptr->text[i].text, info_ptr->text[i].compression); ++ /* Mark this chunk as written */ ++ info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR; ++#else ++ png_warning(png_ptr, "Unable to write compressed text"); ++#endif ++ } ++ ++ else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE) ++ { ++#ifdef PNG_WRITE_tEXt_SUPPORTED ++ /* Write uncompressed chunk */ ++ png_write_tEXt(png_ptr, info_ptr->text[i].key, ++ info_ptr->text[i].text, ++ 0); ++ /* Mark this chunk as written */ ++ info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR; ++#else ++ /* Can't get here */ ++ png_warning(png_ptr, "Unable to write uncompressed text"); ++#endif ++ } ++ } ++#endif /* tEXt */ ++ ++#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED ++ write_unknown_chunks(png_ptr, info_ptr, PNG_HAVE_PLTE); ++#endif ++} ++ ++/* Writes the end of the PNG file. If you don't want to write comments or ++ * time information, you can pass NULL for info. If you already wrote these ++ * in png_write_info(), do not write them again here. If you have long ++ * comments, I suggest writing them here, and compressing them. ++ */ ++void PNGAPI ++png_write_end(png_structrp png_ptr, png_inforp info_ptr) ++{ ++ png_debug(1, "in png_write_end"); ++ ++ if (png_ptr == NULL) ++ return; ++ ++ if ((png_ptr->mode & PNG_HAVE_IDAT) == 0) ++ png_error(png_ptr, "No IDATs written into file"); ++ ++#ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED ++ if (png_ptr->num_palette_max > png_ptr->num_palette) ++ png_benign_error(png_ptr, "Wrote palette index exceeding num_palette"); ++#endif ++ ++ /* See if user wants us to write information chunks */ ++ if (info_ptr != NULL) ++ { ++#ifdef PNG_WRITE_TEXT_SUPPORTED ++ int i; /* local index variable */ ++#endif ++#ifdef PNG_WRITE_tIME_SUPPORTED ++ /* Check to see if user has supplied a time chunk */ ++ if ((info_ptr->valid & PNG_INFO_tIME) != 0 && ++ (png_ptr->mode & PNG_WROTE_tIME) == 0) ++ png_write_tIME(png_ptr, &(info_ptr->mod_time)); ++ ++#endif ++#ifdef PNG_WRITE_TEXT_SUPPORTED ++ /* Loop through comment chunks */ ++ for (i = 0; i < info_ptr->num_text; i++) ++ { ++ png_debug2(2, "Writing trailer text chunk %d, type %d", i, ++ info_ptr->text[i].compression); ++ /* An internationalized chunk? */ ++ if (info_ptr->text[i].compression > 0) ++ { ++#ifdef PNG_WRITE_iTXt_SUPPORTED ++ /* Write international chunk */ ++ png_write_iTXt(png_ptr, ++ info_ptr->text[i].compression, ++ info_ptr->text[i].key, ++ info_ptr->text[i].lang, ++ info_ptr->text[i].lang_key, ++ info_ptr->text[i].text); ++ /* Mark this chunk as written */ ++ if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE) ++ info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR; ++ else ++ info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR; ++#else ++ png_warning(png_ptr, "Unable to write international text"); ++#endif ++ } ++ ++ else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt) ++ { ++#ifdef PNG_WRITE_zTXt_SUPPORTED ++ /* Write compressed chunk */ ++ png_write_zTXt(png_ptr, info_ptr->text[i].key, ++ info_ptr->text[i].text, info_ptr->text[i].compression); ++ /* Mark this chunk as written */ ++ info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR; ++#else ++ png_warning(png_ptr, "Unable to write compressed text"); ++#endif ++ } ++ ++ else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE) ++ { ++#ifdef PNG_WRITE_tEXt_SUPPORTED ++ /* Write uncompressed chunk */ ++ png_write_tEXt(png_ptr, info_ptr->text[i].key, ++ info_ptr->text[i].text, 0); ++ /* Mark this chunk as written */ ++ info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR; ++#else ++ png_warning(png_ptr, "Unable to write uncompressed text"); ++#endif ++ } ++ } ++#endif ++ ++#ifdef PNG_WRITE_eXIf_SUPPORTED ++ if ((info_ptr->valid & PNG_INFO_eXIf) != 0) ++ png_write_eXIf(png_ptr, info_ptr->exif, info_ptr->num_exif); ++#endif ++ ++#ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED ++ write_unknown_chunks(png_ptr, info_ptr, PNG_AFTER_IDAT); ++#endif ++ } ++ ++ png_ptr->mode |= PNG_AFTER_IDAT; ++ ++ /* Write end of PNG file */ ++ png_write_IEND(png_ptr); ++ ++ /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03, ++ * and restored again in libpng-1.2.30, may cause some applications that ++ * do not set png_ptr->output_flush_fn to crash. If your application ++ * experiences a problem, please try building libpng with ++ * PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to ++ * png-mng-implement at lists.sf.net . ++ */ ++#ifdef PNG_WRITE_FLUSH_SUPPORTED ++# ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED ++ png_flush(png_ptr); ++# endif ++#endif ++} ++ ++#ifdef PNG_CONVERT_tIME_SUPPORTED ++void PNGAPI ++png_convert_from_struct_tm(png_timep ptime, const struct tm * ttime) ++{ ++ png_debug(1, "in png_convert_from_struct_tm"); ++ ++ ptime->year = (png_uint_16)(1900 + ttime->tm_year); ++ ptime->month = (png_byte)(ttime->tm_mon + 1); ++ ptime->day = (png_byte)ttime->tm_mday; ++ ptime->hour = (png_byte)ttime->tm_hour; ++ ptime->minute = (png_byte)ttime->tm_min; ++ ptime->second = (png_byte)ttime->tm_sec; ++} ++ ++void PNGAPI ++png_convert_from_time_t(png_timep ptime, time_t ttime) ++{ ++ struct tm *tbuf; ++ ++ png_debug(1, "in png_convert_from_time_t"); ++ ++ tbuf = gmtime(&ttime); ++ png_convert_from_struct_tm(ptime, tbuf); ++} ++#endif ++ ++/* Initialize png_ptr structure, and allocate any memory needed */ ++PNG_FUNCTION(png_structp,PNGAPI ++png_create_write_struct,(png_const_charp user_png_ver, png_voidp error_ptr, ++ png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED) ++{ ++#ifndef PNG_USER_MEM_SUPPORTED ++ png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr, ++ error_fn, warn_fn, NULL, NULL, NULL); ++#else ++ return png_create_write_struct_2(user_png_ver, error_ptr, error_fn, ++ warn_fn, NULL, NULL, NULL); ++} ++ ++/* Alternate initialize png_ptr structure, and allocate any memory needed */ ++PNG_FUNCTION(png_structp,PNGAPI ++png_create_write_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr, ++ png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr, ++ png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED) ++{ ++ png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr, ++ error_fn, warn_fn, mem_ptr, malloc_fn, free_fn); ++#endif /* USER_MEM */ ++ if (png_ptr != NULL) ++ { ++ /* Set the zlib control values to defaults; they can be overridden by the ++ * application after the struct has been created. ++ */ ++ png_ptr->zbuffer_size = PNG_ZBUF_SIZE; ++ ++ /* The 'zlib_strategy' setting is irrelevant because png_default_claim in ++ * pngwutil.c defaults it according to whether or not filters will be ++ * used, and ignores this setting. ++ */ ++ png_ptr->zlib_strategy = PNG_Z_DEFAULT_STRATEGY; ++ png_ptr->zlib_level = PNG_Z_DEFAULT_COMPRESSION; ++ png_ptr->zlib_mem_level = 8; ++ png_ptr->zlib_window_bits = 15; ++ png_ptr->zlib_method = 8; ++ ++#ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED ++ png_ptr->zlib_text_strategy = PNG_TEXT_Z_DEFAULT_STRATEGY; ++ png_ptr->zlib_text_level = PNG_TEXT_Z_DEFAULT_COMPRESSION; ++ png_ptr->zlib_text_mem_level = 8; ++ png_ptr->zlib_text_window_bits = 15; ++ png_ptr->zlib_text_method = 8; ++#endif /* WRITE_COMPRESSED_TEXT */ ++ ++ /* This is a highly dubious configuration option; by default it is off, ++ * but it may be appropriate for private builds that are testing ++ * extensions not conformant to the current specification, or of ++ * applications that must not fail to write at all costs! ++ */ ++#ifdef PNG_BENIGN_WRITE_ERRORS_SUPPORTED ++ /* In stable builds only warn if an application error can be completely ++ * handled. ++ */ ++ png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN; ++#endif ++ ++ /* App warnings are warnings in release (or release candidate) builds but ++ * are errors during development. ++ */ ++#if PNG_RELEASE_BUILD ++ png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN; ++#endif ++ ++ /* TODO: delay this, it can be done in png_init_io() (if the app doesn't ++ * do it itself) avoiding setting the default function if it is not ++ * required. ++ */ ++ png_set_write_fn(png_ptr, NULL, NULL, NULL); ++ } ++ ++ return png_ptr; ++} ++ ++ ++/* Write a few rows of image data. If the image is interlaced, ++ * either you will have to write the 7 sub images, or, if you ++ * have called png_set_interlace_handling(), you will have to ++ * "write" the image seven times. ++ */ ++void PNGAPI ++png_write_rows(png_structrp png_ptr, png_bytepp row, ++ png_uint_32 num_rows) ++{ ++ png_uint_32 i; /* row counter */ ++ png_bytepp rp; /* row pointer */ ++ ++ png_debug(1, "in png_write_rows"); ++ ++ if (png_ptr == NULL) ++ return; ++ ++ /* Loop through the rows */ ++ for (i = 0, rp = row; i < num_rows; i++, rp++) ++ { ++ png_write_row(png_ptr, *rp); ++ } ++} ++ ++/* Write the image. You only need to call this function once, even ++ * if you are writing an interlaced image. ++ */ ++void PNGAPI ++png_write_image(png_structrp png_ptr, png_bytepp image) ++{ ++ png_uint_32 i; /* row index */ ++ int pass, num_pass; /* pass variables */ ++ png_bytepp rp; /* points to current row */ ++ ++ if (png_ptr == NULL) ++ return; ++ ++ png_debug(1, "in png_write_image"); ++ ++#ifdef PNG_WRITE_INTERLACING_SUPPORTED ++ /* Initialize interlace handling. If image is not interlaced, ++ * this will set pass to 1 ++ */ ++ num_pass = png_set_interlace_handling(png_ptr); ++#else ++ num_pass = 1; ++#endif ++ /* Loop through passes */ ++ for (pass = 0; pass < num_pass; pass++) ++ { ++ /* Loop through image */ ++ for (i = 0, rp = image; i < png_ptr->height; i++, rp++) ++ { ++ png_write_row(png_ptr, *rp); ++ } ++ } ++} ++ ++#ifdef PNG_MNG_FEATURES_SUPPORTED ++/* Performs intrapixel differencing */ ++static void ++png_do_write_intrapixel(png_row_infop row_info, png_bytep row) ++{ ++ png_debug(1, "in png_do_write_intrapixel"); ++ ++ if ((row_info->color_type & PNG_COLOR_MASK_COLOR) != 0) ++ { ++ int bytes_per_pixel; ++ png_uint_32 row_width = row_info->width; ++ if (row_info->bit_depth == 8) ++ { ++ png_bytep rp; ++ png_uint_32 i; ++ ++ if (row_info->color_type == PNG_COLOR_TYPE_RGB) ++ bytes_per_pixel = 3; ++ ++ else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) ++ bytes_per_pixel = 4; ++ ++ else ++ return; ++ ++ for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) ++ { ++ *(rp) = (png_byte)(*rp - *(rp + 1)); ++ *(rp + 2) = (png_byte)(*(rp + 2) - *(rp + 1)); ++ } ++ } ++ ++#ifdef PNG_WRITE_16BIT_SUPPORTED ++ else if (row_info->bit_depth == 16) ++ { ++ png_bytep rp; ++ png_uint_32 i; ++ ++ if (row_info->color_type == PNG_COLOR_TYPE_RGB) ++ bytes_per_pixel = 6; ++ ++ else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) ++ bytes_per_pixel = 8; ++ ++ else ++ return; ++ ++ for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel) ++ { ++ png_uint_32 s0 = (png_uint_32)(*(rp ) << 8) | *(rp + 1); ++ png_uint_32 s1 = (png_uint_32)(*(rp + 2) << 8) | *(rp + 3); ++ png_uint_32 s2 = (png_uint_32)(*(rp + 4) << 8) | *(rp + 5); ++ png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL); ++ png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL); ++ *(rp ) = (png_byte)(red >> 8); ++ *(rp + 1) = (png_byte)red; ++ *(rp + 4) = (png_byte)(blue >> 8); ++ *(rp + 5) = (png_byte)blue; ++ } ++ } ++#endif /* WRITE_16BIT */ ++ } ++} ++#endif /* MNG_FEATURES */ ++ ++/* Called by user to write a row of image data */ ++void PNGAPI ++png_write_row(png_structrp png_ptr, png_const_bytep row) ++{ ++ /* 1.5.6: moved from png_struct to be a local structure: */ ++ png_row_info row_info; ++ ++ if (png_ptr == NULL) ++ return; ++ ++ png_debug2(1, "in png_write_row (row %u, pass %d)", ++ png_ptr->row_number, png_ptr->pass); ++ ++ /* Initialize transformations and other stuff if first time */ ++ if (png_ptr->row_number == 0 && png_ptr->pass == 0) ++ { ++ /* Make sure we wrote the header info */ ++ if ((png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE) == 0) ++ png_error(png_ptr, ++ "png_write_info was never called before png_write_row"); ++ ++ /* Check for transforms that have been set but were defined out */ ++#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED) ++ if ((png_ptr->transformations & PNG_INVERT_MONO) != 0) ++ png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined"); ++#endif ++ ++#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED) ++ if ((png_ptr->transformations & PNG_FILLER) != 0) ++ png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined"); ++#endif ++#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \ ++ defined(PNG_READ_PACKSWAP_SUPPORTED) ++ if ((png_ptr->transformations & PNG_PACKSWAP) != 0) ++ png_warning(png_ptr, ++ "PNG_WRITE_PACKSWAP_SUPPORTED is not defined"); ++#endif ++ ++#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED) ++ if ((png_ptr->transformations & PNG_PACK) != 0) ++ png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined"); ++#endif ++ ++#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED) ++ if ((png_ptr->transformations & PNG_SHIFT) != 0) ++ png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined"); ++#endif ++ ++#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED) ++ if ((png_ptr->transformations & PNG_BGR) != 0) ++ png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined"); ++#endif ++ ++#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED) ++ if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0) ++ png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined"); ++#endif ++ ++ png_write_start_row(png_ptr); ++ } ++ ++#ifdef PNG_WRITE_INTERLACING_SUPPORTED ++ /* If interlaced and not interested in row, return */ ++ if (png_ptr->interlaced != 0 && ++ (png_ptr->transformations & PNG_INTERLACE) != 0) ++ { ++ switch (png_ptr->pass) ++ { ++ case 0: ++ if ((png_ptr->row_number & 0x07) != 0) ++ { ++ png_write_finish_row(png_ptr); ++ return; ++ } ++ break; ++ ++ case 1: ++ if ((png_ptr->row_number & 0x07) != 0 || png_ptr->width < 5) ++ { ++ png_write_finish_row(png_ptr); ++ return; ++ } ++ break; ++ ++ case 2: ++ if ((png_ptr->row_number & 0x07) != 4) ++ { ++ png_write_finish_row(png_ptr); ++ return; ++ } ++ break; ++ ++ case 3: ++ if ((png_ptr->row_number & 0x03) != 0 || png_ptr->width < 3) ++ { ++ png_write_finish_row(png_ptr); ++ return; ++ } ++ break; ++ ++ case 4: ++ if ((png_ptr->row_number & 0x03) != 2) ++ { ++ png_write_finish_row(png_ptr); ++ return; ++ } ++ break; ++ ++ case 5: ++ if ((png_ptr->row_number & 0x01) != 0 || png_ptr->width < 2) ++ { ++ png_write_finish_row(png_ptr); ++ return; ++ } ++ break; ++ ++ case 6: ++ if ((png_ptr->row_number & 0x01) == 0) ++ { ++ png_write_finish_row(png_ptr); ++ return; ++ } ++ break; ++ ++ default: /* error: ignore it */ ++ break; ++ } ++ } ++#endif ++ ++ /* Set up row info for transformations */ ++ row_info.color_type = png_ptr->color_type; ++ row_info.width = png_ptr->usr_width; ++ row_info.channels = png_ptr->usr_channels; ++ row_info.bit_depth = png_ptr->usr_bit_depth; ++ row_info.pixel_depth = (png_byte)(row_info.bit_depth * row_info.channels); ++ row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width); ++ ++ png_debug1(3, "row_info->color_type = %d", row_info.color_type); ++ png_debug1(3, "row_info->width = %u", row_info.width); ++ png_debug1(3, "row_info->channels = %d", row_info.channels); ++ png_debug1(3, "row_info->bit_depth = %d", row_info.bit_depth); ++ png_debug1(3, "row_info->pixel_depth = %d", row_info.pixel_depth); ++ png_debug1(3, "row_info->rowbytes = %lu", (unsigned long)row_info.rowbytes); ++ ++ /* Copy user's row into buffer, leaving room for filter byte. */ ++ memcpy(png_ptr->row_buf + 1, row, row_info.rowbytes); ++ ++#ifdef PNG_WRITE_INTERLACING_SUPPORTED ++ /* Handle interlacing */ ++ if (png_ptr->interlaced && png_ptr->pass < 6 && ++ (png_ptr->transformations & PNG_INTERLACE) != 0) ++ { ++ png_do_write_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass); ++ /* This should always get caught above, but still ... */ ++ if (row_info.width == 0) ++ { ++ png_write_finish_row(png_ptr); ++ return; ++ } ++ } ++#endif ++ ++#ifdef PNG_WRITE_TRANSFORMS_SUPPORTED ++ /* Handle other transformations */ ++ if (png_ptr->transformations != 0) ++ png_do_write_transformations(png_ptr, &row_info); ++#endif ++ ++ /* At this point the row_info pixel depth must match the 'transformed' depth, ++ * which is also the output depth. ++ */ ++ if (row_info.pixel_depth != png_ptr->pixel_depth || ++ row_info.pixel_depth != png_ptr->transformed_pixel_depth) ++ png_error(png_ptr, "internal write transform logic error"); ++ ++#ifdef PNG_MNG_FEATURES_SUPPORTED ++ /* Write filter_method 64 (intrapixel differencing) only if ++ * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and ++ * 2. Libpng did not write a PNG signature (this filter_method is only ++ * used in PNG datastreams that are embedded in MNG datastreams) and ++ * 3. The application called png_permit_mng_features with a mask that ++ * included PNG_FLAG_MNG_FILTER_64 and ++ * 4. The filter_method is 64 and ++ * 5. The color_type is RGB or RGBA ++ */ ++ if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 && ++ (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING)) ++ { ++ /* Intrapixel differencing */ ++ png_do_write_intrapixel(&row_info, png_ptr->row_buf + 1); ++ } ++#endif ++ ++/* Added at libpng-1.5.10 */ ++#ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED ++ /* Check for out-of-range palette index */ ++ if (row_info.color_type == PNG_COLOR_TYPE_PALETTE && ++ png_ptr->num_palette_max >= 0) ++ png_do_check_palette_indexes(png_ptr, &row_info); ++#endif ++ ++ /* Find a filter if necessary, filter the row and write it out. */ ++ png_write_find_filter(png_ptr, &row_info); ++ ++ if (png_ptr->write_row_fn != NULL) ++ (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass); ++} ++ ++#ifdef PNG_WRITE_FLUSH_SUPPORTED ++/* Set the automatic flush interval or 0 to turn flushing off */ ++void PNGAPI ++png_set_flush(png_structrp png_ptr, int nrows) ++{ ++ png_debug(1, "in png_set_flush"); ++ ++ if (png_ptr == NULL) ++ return; ++ ++ png_ptr->flush_dist = (nrows < 0 ? 0 : (png_uint_32)nrows); ++} ++ ++/* Flush the current output buffers now */ ++void PNGAPI ++png_write_flush(png_structrp png_ptr) ++{ ++ png_debug(1, "in png_write_flush"); ++ ++ if (png_ptr == NULL) ++ return; ++ ++ /* We have already written out all of the data */ ++ if (png_ptr->row_number >= png_ptr->num_rows) ++ return; ++ ++ png_compress_IDAT(png_ptr, NULL, 0, Z_SYNC_FLUSH); ++ png_ptr->flush_rows = 0; ++ png_flush(png_ptr); ++} ++#endif /* WRITE_FLUSH */ ++ ++/* Free any memory used in png_ptr struct without freeing the struct itself. */ ++static void ++png_write_destroy(png_structrp png_ptr) ++{ ++ png_debug(1, "in png_write_destroy"); ++ ++ /* Free any memory zlib uses */ ++ if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0) ++ deflateEnd(&png_ptr->zstream); ++ ++ /* Free our memory. png_free checks NULL for us. */ ++ png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list); ++ png_free(png_ptr, png_ptr->row_buf); ++ png_ptr->row_buf = NULL; ++#ifdef PNG_WRITE_FILTER_SUPPORTED ++ png_free(png_ptr, png_ptr->prev_row); ++ png_free(png_ptr, png_ptr->try_row); ++ png_free(png_ptr, png_ptr->tst_row); ++ png_ptr->prev_row = NULL; ++ png_ptr->try_row = NULL; ++ png_ptr->tst_row = NULL; ++#endif ++ ++#ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED ++ png_free(png_ptr, png_ptr->chunk_list); ++ png_ptr->chunk_list = NULL; ++#endif ++ ++ /* The error handling and memory handling information is left intact at this ++ * point: the jmp_buf may still have to be freed. See png_destroy_png_struct ++ * for how this happens. ++ */ ++} ++ ++/* Free all memory used by the write. ++ * In libpng 1.6.0 this API changed quietly to no longer accept a NULL value for ++ * *png_ptr_ptr. Prior to 1.6.0 it would accept such a value and it would free ++ * the passed in info_structs but it would quietly fail to free any of the data ++ * inside them. In 1.6.0 it quietly does nothing (it has to be quiet because it ++ * has no png_ptr.) ++ */ ++void PNGAPI ++png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr) ++{ ++ png_debug(1, "in png_destroy_write_struct"); ++ ++ if (png_ptr_ptr != NULL) ++ { ++ png_structrp png_ptr = *png_ptr_ptr; ++ ++ if (png_ptr != NULL) /* added in libpng 1.6.0 */ ++ { ++ png_destroy_info_struct(png_ptr, info_ptr_ptr); ++ ++ *png_ptr_ptr = NULL; ++ png_write_destroy(png_ptr); ++ png_destroy_png_struct(png_ptr); ++ } ++ } ++} ++ ++/* Allow the application to select one or more row filters to use. */ ++void PNGAPI ++png_set_filter(png_structrp png_ptr, int method, int filters) ++{ ++ png_debug(1, "in png_set_filter"); ++ ++ if (png_ptr == NULL) ++ return; ++ ++#ifdef PNG_MNG_FEATURES_SUPPORTED ++ if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 && ++ (method == PNG_INTRAPIXEL_DIFFERENCING)) ++ method = PNG_FILTER_TYPE_BASE; ++ ++#endif ++ if (method == PNG_FILTER_TYPE_BASE) ++ { ++ switch (filters & (PNG_ALL_FILTERS | 0x07)) ++ { ++#ifdef PNG_WRITE_FILTER_SUPPORTED ++ case 5: ++ case 6: ++ case 7: png_app_error(png_ptr, "Unknown row filter for method 0"); ++#endif /* WRITE_FILTER */ ++ /* FALLTHROUGH */ ++ case PNG_FILTER_VALUE_NONE: ++ png_ptr->do_filter = PNG_FILTER_NONE; break; ++ ++#ifdef PNG_WRITE_FILTER_SUPPORTED ++ case PNG_FILTER_VALUE_SUB: ++ png_ptr->do_filter = PNG_FILTER_SUB; break; ++ ++ case PNG_FILTER_VALUE_UP: ++ png_ptr->do_filter = PNG_FILTER_UP; break; ++ ++ case PNG_FILTER_VALUE_AVG: ++ png_ptr->do_filter = PNG_FILTER_AVG; break; ++ ++ case PNG_FILTER_VALUE_PAETH: ++ png_ptr->do_filter = PNG_FILTER_PAETH; break; ++ ++ default: ++ png_ptr->do_filter = (png_byte)filters; break; ++#else ++ default: ++ png_app_error(png_ptr, "Unknown row filter for method 0"); ++#endif /* WRITE_FILTER */ ++ } ++ ++#ifdef PNG_WRITE_FILTER_SUPPORTED ++ /* If we have allocated the row_buf, this means we have already started ++ * with the image and we should have allocated all of the filter buffers ++ * that have been selected. If prev_row isn't already allocated, then ++ * it is too late to start using the filters that need it, since we ++ * will be missing the data in the previous row. If an application ++ * wants to start and stop using particular filters during compression, ++ * it should start out with all of the filters, and then remove them ++ * or add them back after the start of compression. ++ * ++ * NOTE: this is a nasty constraint on the code, because it means that the ++ * prev_row buffer must be maintained even if there are currently no ++ * 'prev_row' requiring filters active. ++ */ ++ if (png_ptr->row_buf != NULL) ++ { ++ int num_filters; ++ png_alloc_size_t buf_size; ++ ++ /* Repeat the checks in png_write_start_row; 1 pixel high or wide ++ * images cannot benefit from certain filters. If this isn't done here ++ * the check below will fire on 1 pixel high images. ++ */ ++ if (png_ptr->height == 1) ++ filters &= ~(PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH); ++ ++ if (png_ptr->width == 1) ++ filters &= ~(PNG_FILTER_SUB|PNG_FILTER_AVG|PNG_FILTER_PAETH); ++ ++ if ((filters & (PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH)) != 0 ++ && png_ptr->prev_row == NULL) ++ { ++ /* This is the error case, however it is benign - the previous row ++ * is not available so the filter can't be used. Just warn here. ++ */ ++ png_app_warning(png_ptr, ++ "png_set_filter: UP/AVG/PAETH cannot be added after start"); ++ filters &= ~(PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH); ++ } ++ ++ num_filters = 0; ++ ++ if (filters & PNG_FILTER_SUB) ++ num_filters++; ++ ++ if (filters & PNG_FILTER_UP) ++ num_filters++; ++ ++ if (filters & PNG_FILTER_AVG) ++ num_filters++; ++ ++ if (filters & PNG_FILTER_PAETH) ++ num_filters++; ++ ++ /* Allocate needed row buffers if they have not already been ++ * allocated. ++ */ ++ buf_size = PNG_ROWBYTES(png_ptr->usr_channels * png_ptr->usr_bit_depth, ++ png_ptr->width) + 1; ++ ++ if (png_ptr->try_row == NULL) ++ png_ptr->try_row = png_voidcast(png_bytep, ++ png_malloc(png_ptr, buf_size)); ++ ++ if (num_filters > 1) ++ { ++ if (png_ptr->tst_row == NULL) ++ png_ptr->tst_row = png_voidcast(png_bytep, ++ png_malloc(png_ptr, buf_size)); ++ } ++ } ++ png_ptr->do_filter = (png_byte)filters; ++#endif ++ } ++ else ++ png_error(png_ptr, "Unknown custom filter method"); ++} ++ ++#ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* DEPRECATED */ ++/* Provide floating and fixed point APIs */ ++#ifdef PNG_FLOATING_POINT_SUPPORTED ++void PNGAPI ++png_set_filter_heuristics(png_structrp png_ptr, int heuristic_method, ++ int num_weights, png_const_doublep filter_weights, ++ png_const_doublep filter_costs) ++{ ++ PNG_UNUSED(png_ptr) ++ PNG_UNUSED(heuristic_method) ++ PNG_UNUSED(num_weights) ++ PNG_UNUSED(filter_weights) ++ PNG_UNUSED(filter_costs) ++} ++#endif /* FLOATING_POINT */ ++ ++#ifdef PNG_FIXED_POINT_SUPPORTED ++void PNGAPI ++png_set_filter_heuristics_fixed(png_structrp png_ptr, int heuristic_method, ++ int num_weights, png_const_fixed_point_p filter_weights, ++ png_const_fixed_point_p filter_costs) ++{ ++ PNG_UNUSED(png_ptr) ++ PNG_UNUSED(heuristic_method) ++ PNG_UNUSED(num_weights) ++ PNG_UNUSED(filter_weights) ++ PNG_UNUSED(filter_costs) ++} ++#endif /* FIXED_POINT */ ++#endif /* WRITE_WEIGHTED_FILTER */ ++ ++#ifdef PNG_WRITE_CUSTOMIZE_COMPRESSION_SUPPORTED ++void PNGAPI ++png_set_compression_level(png_structrp png_ptr, int level) ++{ ++ png_debug(1, "in png_set_compression_level"); ++ ++ if (png_ptr == NULL) ++ return; ++ ++ png_ptr->zlib_level = level; ++} ++ ++void PNGAPI ++png_set_compression_mem_level(png_structrp png_ptr, int mem_level) ++{ ++ png_debug(1, "in png_set_compression_mem_level"); ++ ++ if (png_ptr == NULL) ++ return; ++ ++ png_ptr->zlib_mem_level = mem_level; ++} ++ ++void PNGAPI ++png_set_compression_strategy(png_structrp png_ptr, int strategy) ++{ ++ png_debug(1, "in png_set_compression_strategy"); ++ ++ if (png_ptr == NULL) ++ return; ++ ++ /* The flag setting here prevents the libpng dynamic selection of strategy. ++ */ ++ png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY; ++ png_ptr->zlib_strategy = strategy; ++} ++ ++/* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a ++ * smaller value of window_bits if it can do so safely. ++ */ ++void PNGAPI ++png_set_compression_window_bits(png_structrp png_ptr, int window_bits) ++{ ++ if (png_ptr == NULL) ++ return; ++ ++ /* Prior to 1.6.0 this would warn but then set the window_bits value. This ++ * meant that negative window bits values could be selected that would cause ++ * libpng to write a non-standard PNG file with raw deflate or gzip ++ * compressed IDAT or ancillary chunks. Such files can be read and there is ++ * no warning on read, so this seems like a very bad idea. ++ */ ++ if (window_bits > 15) ++ { ++ png_warning(png_ptr, "Only compression windows <= 32k supported by PNG"); ++ window_bits = 15; ++ } ++ ++ else if (window_bits < 8) ++ { ++ png_warning(png_ptr, "Only compression windows >= 256 supported by PNG"); ++ window_bits = 8; ++ } ++ ++ png_ptr->zlib_window_bits = window_bits; ++} ++ ++void PNGAPI ++png_set_compression_method(png_structrp png_ptr, int method) ++{ ++ png_debug(1, "in png_set_compression_method"); ++ ++ if (png_ptr == NULL) ++ return; ++ ++ /* This would produce an invalid PNG file if it worked, but it doesn't and ++ * deflate will fault it, so it is harmless to just warn here. ++ */ ++ if (method != 8) ++ png_warning(png_ptr, "Only compression method 8 is supported by PNG"); ++ ++ png_ptr->zlib_method = method; ++} ++#endif /* WRITE_CUSTOMIZE_COMPRESSION */ ++ ++/* The following were added to libpng-1.5.4 */ ++#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED ++void PNGAPI ++png_set_text_compression_level(png_structrp png_ptr, int level) ++{ ++ png_debug(1, "in png_set_text_compression_level"); ++ ++ if (png_ptr == NULL) ++ return; ++ ++ png_ptr->zlib_text_level = level; ++} ++ ++void PNGAPI ++png_set_text_compression_mem_level(png_structrp png_ptr, int mem_level) ++{ ++ png_debug(1, "in png_set_text_compression_mem_level"); ++ ++ if (png_ptr == NULL) ++ return; ++ ++ png_ptr->zlib_text_mem_level = mem_level; ++} ++ ++void PNGAPI ++png_set_text_compression_strategy(png_structrp png_ptr, int strategy) ++{ ++ png_debug(1, "in png_set_text_compression_strategy"); ++ ++ if (png_ptr == NULL) ++ return; ++ ++ png_ptr->zlib_text_strategy = strategy; ++} ++ ++/* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a ++ * smaller value of window_bits if it can do so safely. ++ */ ++void PNGAPI ++png_set_text_compression_window_bits(png_structrp png_ptr, int window_bits) ++{ ++ if (png_ptr == NULL) ++ return; ++ ++ if (window_bits > 15) ++ { ++ png_warning(png_ptr, "Only compression windows <= 32k supported by PNG"); ++ window_bits = 15; ++ } ++ ++ else if (window_bits < 8) ++ { ++ png_warning(png_ptr, "Only compression windows >= 256 supported by PNG"); ++ window_bits = 8; ++ } ++ ++ png_ptr->zlib_text_window_bits = window_bits; ++} ++ ++void PNGAPI ++png_set_text_compression_method(png_structrp png_ptr, int method) ++{ ++ png_debug(1, "in png_set_text_compression_method"); ++ ++ if (png_ptr == NULL) ++ return; ++ ++ if (method != 8) ++ png_warning(png_ptr, "Only compression method 8 is supported by PNG"); ++ ++ png_ptr->zlib_text_method = method; ++} ++#endif /* WRITE_CUSTOMIZE_ZTXT_COMPRESSION */ ++/* end of API added to libpng-1.5.4 */ ++ ++void PNGAPI ++png_set_write_status_fn(png_structrp png_ptr, png_write_status_ptr write_row_fn) ++{ ++ if (png_ptr == NULL) ++ return; ++ ++ png_ptr->write_row_fn = write_row_fn; ++} ++ ++#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED ++void PNGAPI ++png_set_write_user_transform_fn(png_structrp png_ptr, png_user_transform_ptr ++ write_user_transform_fn) ++{ ++ png_debug(1, "in png_set_write_user_transform_fn"); ++ ++ if (png_ptr == NULL) ++ return; ++ ++ png_ptr->transformations |= PNG_USER_TRANSFORM; ++ png_ptr->write_user_transform_fn = write_user_transform_fn; ++} ++#endif ++ ++ ++#ifdef PNG_INFO_IMAGE_SUPPORTED ++void PNGAPI ++png_write_png(png_structrp png_ptr, png_inforp info_ptr, ++ int transforms, voidp params) ++{ ++ if (png_ptr == NULL || info_ptr == NULL) ++ return; ++ ++ if ((info_ptr->valid & PNG_INFO_IDAT) == 0) ++ { ++ png_app_error(png_ptr, "no rows for png_write_image to write"); ++ return; ++ } ++ ++ /* Write the file header information. */ ++ png_write_info(png_ptr, info_ptr); ++ ++ /* ------ these transformations don't touch the info structure ------- */ ++ ++ /* Invert monochrome pixels */ ++ if ((transforms & PNG_TRANSFORM_INVERT_MONO) != 0) ++#ifdef PNG_WRITE_INVERT_SUPPORTED ++ png_set_invert_mono(png_ptr); ++#else ++ png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_MONO not supported"); ++#endif ++ ++ /* Shift the pixels up to a legal bit depth and fill in ++ * as appropriate to correctly scale the image. ++ */ ++ if ((transforms & PNG_TRANSFORM_SHIFT) != 0) ++#ifdef PNG_WRITE_SHIFT_SUPPORTED ++ if ((info_ptr->valid & PNG_INFO_sBIT) != 0) ++ png_set_shift(png_ptr, &info_ptr->sig_bit); ++#else ++ png_app_error(png_ptr, "PNG_TRANSFORM_SHIFT not supported"); ++#endif ++ ++ /* Pack pixels into bytes */ ++ if ((transforms & PNG_TRANSFORM_PACKING) != 0) ++#ifdef PNG_WRITE_PACK_SUPPORTED ++ png_set_packing(png_ptr); ++#else ++ png_app_error(png_ptr, "PNG_TRANSFORM_PACKING not supported"); ++#endif ++ ++ /* Swap location of alpha bytes from ARGB to RGBA */ ++ if ((transforms & PNG_TRANSFORM_SWAP_ALPHA) != 0) ++#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED ++ png_set_swap_alpha(png_ptr); ++#else ++ png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ALPHA not supported"); ++#endif ++ ++ /* Remove a filler (X) from XRGB/RGBX/AG/GA into to convert it into ++ * RGB, note that the code expects the input color type to be G or RGB; no ++ * alpha channel. ++ */ ++ if ((transforms & (PNG_TRANSFORM_STRIP_FILLER_AFTER| ++ PNG_TRANSFORM_STRIP_FILLER_BEFORE)) != 0) ++ { ++#ifdef PNG_WRITE_FILLER_SUPPORTED ++ if ((transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER) != 0) ++ { ++ if ((transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE) != 0) ++ png_app_error(png_ptr, ++ "PNG_TRANSFORM_STRIP_FILLER: BEFORE+AFTER not supported"); ++ ++ /* Continue if ignored - this is the pre-1.6.10 behavior */ ++ png_set_filler(png_ptr, 0, PNG_FILLER_AFTER); ++ } ++ ++ else if ((transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE) != 0) ++ png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE); ++#else ++ png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_FILLER not supported"); ++#endif ++ } ++ ++ /* Flip BGR pixels to RGB */ ++ if ((transforms & PNG_TRANSFORM_BGR) != 0) ++#ifdef PNG_WRITE_BGR_SUPPORTED ++ png_set_bgr(png_ptr); ++#else ++ png_app_error(png_ptr, "PNG_TRANSFORM_BGR not supported"); ++#endif ++ ++ /* Swap bytes of 16-bit files to most significant byte first */ ++ if ((transforms & PNG_TRANSFORM_SWAP_ENDIAN) != 0) ++#ifdef PNG_WRITE_SWAP_SUPPORTED ++ png_set_swap(png_ptr); ++#else ++ png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported"); ++#endif ++ ++ /* Swap bits of 1-bit, 2-bit, 4-bit packed pixel formats */ ++ if ((transforms & PNG_TRANSFORM_PACKSWAP) != 0) ++#ifdef PNG_WRITE_PACKSWAP_SUPPORTED ++ png_set_packswap(png_ptr); ++#else ++ png_app_error(png_ptr, "PNG_TRANSFORM_PACKSWAP not supported"); ++#endif ++ ++ /* Invert the alpha channel from opacity to transparency */ ++ if ((transforms & PNG_TRANSFORM_INVERT_ALPHA) != 0) ++#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED ++ png_set_invert_alpha(png_ptr); ++#else ++ png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_ALPHA not supported"); ++#endif ++ ++ /* ----------------------- end of transformations ------------------- */ ++ ++ /* Write the bits */ ++ png_write_image(png_ptr, info_ptr->row_pointers); ++ ++ /* It is REQUIRED to call this to finish writing the rest of the file */ ++ png_write_end(png_ptr, info_ptr); ++ ++ PNG_UNUSED(params) ++} ++#endif ++ ++ ++#ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED ++/* Initialize the write structure - general purpose utility. */ ++static int ++png_image_write_init(png_imagep image) ++{ ++ png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, image, ++ png_safe_error, png_safe_warning); ++ ++ if (png_ptr != NULL) ++ { ++ png_infop info_ptr = png_create_info_struct(png_ptr); ++ ++ if (info_ptr != NULL) ++ { ++ png_controlp control = png_voidcast(png_controlp, ++ png_malloc_warn(png_ptr, (sizeof *control))); ++ ++ if (control != NULL) ++ { ++ memset(control, 0, (sizeof *control)); ++ ++ control->png_ptr = png_ptr; ++ control->info_ptr = info_ptr; ++ control->for_write = 1; ++ ++ image->opaque = control; ++ return 1; ++ } ++ ++ /* Error clean up */ ++ png_destroy_info_struct(png_ptr, &info_ptr); ++ } ++ ++ png_destroy_write_struct(&png_ptr, NULL); ++ } ++ ++ return png_image_error(image, "png_image_write_: out of memory"); ++} ++ ++/* Arguments to png_image_write_main: */ ++typedef struct ++{ ++ /* Arguments: */ ++ png_imagep image; ++ png_const_voidp buffer; ++ png_int_32 row_stride; ++ png_const_voidp colormap; ++ int convert_to_8bit; ++ /* Local variables: */ ++ png_const_voidp first_row; ++ ptrdiff_t row_bytes; ++ png_voidp local_row; ++ /* Byte count for memory writing */ ++ png_bytep memory; ++ png_alloc_size_t memory_bytes; /* not used for STDIO */ ++ png_alloc_size_t output_bytes; /* running total */ ++} png_image_write_control; ++ ++/* Write png_uint_16 input to a 16-bit PNG; the png_ptr has already been set to ++ * do any necessary byte swapping. The component order is defined by the ++ * png_image format value. ++ */ ++static int ++png_write_image_16bit(png_voidp argument) ++{ ++ png_image_write_control *display = png_voidcast(png_image_write_control*, ++ argument); ++ png_imagep image = display->image; ++ png_structrp png_ptr = image->opaque->png_ptr; ++ ++ png_const_uint_16p input_row = png_voidcast(png_const_uint_16p, ++ display->first_row); ++ png_uint_16p output_row = png_voidcast(png_uint_16p, display->local_row); ++ png_uint_16p row_end; ++ unsigned int channels = (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ? ++ 3 : 1; ++ int aindex = 0; ++ png_uint_32 y = image->height; ++ ++ if ((image->format & PNG_FORMAT_FLAG_ALPHA) != 0) ++ { ++# ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED ++ if ((image->format & PNG_FORMAT_FLAG_AFIRST) != 0) ++ { ++ aindex = -1; ++ ++input_row; /* To point to the first component */ ++ ++output_row; ++ } ++ else ++ aindex = (int)channels; ++# else ++ aindex = (int)channels; ++# endif ++ } ++ ++ else ++ png_error(png_ptr, "png_write_image: internal call error"); ++ ++ /* Work out the output row end and count over this, note that the increment ++ * above to 'row' means that row_end can actually be beyond the end of the ++ * row; this is correct. ++ */ ++ row_end = output_row + image->width * (channels+1); ++ ++ for (; y > 0; --y) ++ { ++ png_const_uint_16p in_ptr = input_row; ++ png_uint_16p out_ptr = output_row; ++ ++ while (out_ptr < row_end) ++ { ++ png_uint_16 alpha = in_ptr[aindex]; ++ png_uint_32 reciprocal = 0; ++ int c; ++ ++ out_ptr[aindex] = alpha; ++ ++ /* Calculate a reciprocal. The correct calculation is simply ++ * component/alpha*65535 << 15. (I.e. 15 bits of precision); this ++ * allows correct rounding by adding .5 before the shift. 'reciprocal' ++ * is only initialized when required. ++ */ ++ if (alpha > 0 && alpha < 65535) ++ reciprocal = ((0xffff<<15)+(alpha>>1))/alpha; ++ ++ c = (int)channels; ++ do /* always at least one channel */ ++ { ++ png_uint_16 component = *in_ptr++; ++ ++ /* The following gives 65535 for an alpha of 0, which is fine, ++ * otherwise if 0/0 is represented as some other value there is more ++ * likely to be a discontinuity which will probably damage ++ * compression when moving from a fully transparent area to a ++ * nearly transparent one. (The assumption here is that opaque ++ * areas tend not to be 0 intensity.) ++ */ ++ if (component >= alpha) ++ component = 65535; ++ ++ /* component 0 && alpha < 65535) ++ { ++ png_uint_32 calc = component * reciprocal; ++ calc += 16384; /* round to nearest */ ++ component = (png_uint_16)(calc >> 15); ++ } ++ ++ *out_ptr++ = component; ++ } ++ while (--c > 0); ++ ++ /* Skip to next component (skip the intervening alpha channel) */ ++ ++in_ptr; ++ ++out_ptr; ++ } ++ ++ png_write_row(png_ptr, png_voidcast(png_const_bytep, display->local_row)); ++ input_row += (png_uint_16)display->row_bytes/(sizeof (png_uint_16)); ++ } ++ ++ return 1; ++} ++ ++/* Given 16-bit input (1 to 4 channels) write 8-bit output. If an alpha channel ++ * is present it must be removed from the components, the components are then ++ * written in sRGB encoding. No components are added or removed. ++ * ++ * Calculate an alpha reciprocal to reverse pre-multiplication. As above the ++ * calculation can be done to 15 bits of accuracy; however, the output needs to ++ * be scaled in the range 0..255*65535, so include that scaling here. ++ */ ++# define UNP_RECIPROCAL(alpha) ((((0xffff*0xff)<<7)+((alpha)>>1))/(alpha)) ++ ++static png_byte ++png_unpremultiply(png_uint_32 component, png_uint_32 alpha, ++ png_uint_32 reciprocal/*from the above macro*/) ++{ ++ /* The following gives 1.0 for an alpha of 0, which is fine, otherwise if 0/0 ++ * is represented as some other value there is more likely to be a ++ * discontinuity which will probably damage compression when moving from a ++ * fully transparent area to a nearly transparent one. (The assumption here ++ * is that opaque areas tend not to be 0 intensity.) ++ * ++ * There is a rounding problem here; if alpha is less than 128 it will end up ++ * as 0 when scaled to 8 bits. To avoid introducing spurious colors into the ++ * output change for this too. ++ */ ++ if (component >= alpha || alpha < 128) ++ return 255; ++ ++ /* component 0) ++ { ++ /* The test is that alpha/257 (rounded) is less than 255, the first value ++ * that becomes 255 is 65407. ++ * NOTE: this must agree with the PNG_DIV257 macro (which must, therefore, ++ * be exact!) [Could also test reciprocal != 0] ++ */ ++ if (alpha < 65407) ++ { ++ component *= reciprocal; ++ component += 64; /* round to nearest */ ++ component >>= 7; ++ } ++ ++ else ++ component *= 255; ++ ++ /* Convert the component to sRGB. */ ++ return (png_byte)PNG_sRGB_FROM_LINEAR(component); ++ } ++ ++ else ++ return 0; ++} ++ ++static int ++png_write_image_8bit(png_voidp argument) ++{ ++ png_image_write_control *display = png_voidcast(png_image_write_control*, ++ argument); ++ png_imagep image = display->image; ++ png_structrp png_ptr = image->opaque->png_ptr; ++ ++ png_const_uint_16p input_row = png_voidcast(png_const_uint_16p, ++ display->first_row); ++ png_bytep output_row = png_voidcast(png_bytep, display->local_row); ++ png_uint_32 y = image->height; ++ unsigned int channels = (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ? ++ 3 : 1; ++ ++ if ((image->format & PNG_FORMAT_FLAG_ALPHA) != 0) ++ { ++ png_bytep row_end; ++ int aindex; ++ ++# ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED ++ if ((image->format & PNG_FORMAT_FLAG_AFIRST) != 0) ++ { ++ aindex = -1; ++ ++input_row; /* To point to the first component */ ++ ++output_row; ++ } ++ ++ else ++# endif ++ aindex = (int)channels; ++ ++ /* Use row_end in place of a loop counter: */ ++ row_end = output_row + image->width * (channels+1); ++ ++ for (; y > 0; --y) ++ { ++ png_const_uint_16p in_ptr = input_row; ++ png_bytep out_ptr = output_row; ++ ++ while (out_ptr < row_end) ++ { ++ png_uint_16 alpha = in_ptr[aindex]; ++ png_byte alphabyte = (png_byte)PNG_DIV257(alpha); ++ png_uint_32 reciprocal = 0; ++ int c; ++ ++ /* Scale and write the alpha channel. */ ++ out_ptr[aindex] = alphabyte; ++ ++ if (alphabyte > 0 && alphabyte < 255) ++ reciprocal = UNP_RECIPROCAL(alpha); ++ ++ c = (int)channels; ++ do /* always at least one channel */ ++ *out_ptr++ = png_unpremultiply(*in_ptr++, alpha, reciprocal); ++ while (--c > 0); ++ ++ /* Skip to next component (skip the intervening alpha channel) */ ++ ++in_ptr; ++ ++out_ptr; ++ } /* while out_ptr < row_end */ ++ ++ png_write_row(png_ptr, png_voidcast(png_const_bytep, ++ display->local_row)); ++ input_row += (png_uint_16)display->row_bytes/(sizeof (png_uint_16)); ++ } /* while y */ ++ } ++ ++ else ++ { ++ /* No alpha channel, so the row_end really is the end of the row and it ++ * is sufficient to loop over the components one by one. ++ */ ++ png_bytep row_end = output_row + image->width * channels; ++ ++ for (; y > 0; --y) ++ { ++ png_const_uint_16p in_ptr = input_row; ++ png_bytep out_ptr = output_row; ++ ++ while (out_ptr < row_end) ++ { ++ png_uint_32 component = *in_ptr++; ++ ++ component *= 255; ++ *out_ptr++ = (png_byte)PNG_sRGB_FROM_LINEAR(component); ++ } ++ ++ png_write_row(png_ptr, output_row); ++ input_row += (png_uint_16)display->row_bytes/(sizeof (png_uint_16)); ++ } ++ } ++ ++ return 1; ++} ++ ++static void ++png_image_set_PLTE(png_image_write_control *display) ++{ ++ png_imagep image = display->image; ++ const void *cmap = display->colormap; ++ int entries = image->colormap_entries > 256 ? 256 : ++ (int)image->colormap_entries; ++ ++ /* NOTE: the caller must check for cmap != NULL and entries != 0 */ ++ png_uint_32 format = image->format; ++ unsigned int channels = PNG_IMAGE_SAMPLE_CHANNELS(format); ++ ++# if defined(PNG_FORMAT_BGR_SUPPORTED) &&\ ++ defined(PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED) ++ int afirst = (format & PNG_FORMAT_FLAG_AFIRST) != 0 && ++ (format & PNG_FORMAT_FLAG_ALPHA) != 0; ++# else ++# define afirst 0 ++# endif ++ ++# ifdef PNG_FORMAT_BGR_SUPPORTED ++ int bgr = (format & PNG_FORMAT_FLAG_BGR) != 0 ? 2 : 0; ++# else ++# define bgr 0 ++# endif ++ ++ int i, num_trans; ++ png_color palette[256]; ++ png_byte tRNS[256]; ++ ++ memset(tRNS, 255, (sizeof tRNS)); ++ memset(palette, 0, (sizeof palette)); ++ ++ for (i=num_trans=0; i= 3) /* RGB */ ++ { ++ palette[i].blue = (png_byte)PNG_sRGB_FROM_LINEAR(255 * ++ entry[(2 ^ bgr)]); ++ palette[i].green = (png_byte)PNG_sRGB_FROM_LINEAR(255 * ++ entry[1]); ++ palette[i].red = (png_byte)PNG_sRGB_FROM_LINEAR(255 * ++ entry[bgr]); ++ } ++ ++ else /* Gray */ ++ palette[i].blue = palette[i].red = palette[i].green = ++ (png_byte)PNG_sRGB_FROM_LINEAR(255 * *entry); ++ } ++ ++ else /* alpha */ ++ { ++ png_uint_16 alpha = entry[afirst ? 0 : channels-1]; ++ png_byte alphabyte = (png_byte)PNG_DIV257(alpha); ++ png_uint_32 reciprocal = 0; ++ ++ /* Calculate a reciprocal, as in the png_write_image_8bit code above ++ * this is designed to produce a value scaled to 255*65535 when ++ * divided by 128 (i.e. asr 7). ++ */ ++ if (alphabyte > 0 && alphabyte < 255) ++ reciprocal = (((0xffff*0xff)<<7)+(alpha>>1))/alpha; ++ ++ tRNS[i] = alphabyte; ++ if (alphabyte < 255) ++ num_trans = i+1; ++ ++ if (channels >= 3) /* RGB */ ++ { ++ palette[i].blue = png_unpremultiply(entry[afirst + (2 ^ bgr)], ++ alpha, reciprocal); ++ palette[i].green = png_unpremultiply(entry[afirst + 1], alpha, ++ reciprocal); ++ palette[i].red = png_unpremultiply(entry[afirst + bgr], alpha, ++ reciprocal); ++ } ++ ++ else /* gray */ ++ palette[i].blue = palette[i].red = palette[i].green = ++ png_unpremultiply(entry[afirst], alpha, reciprocal); ++ } ++ } ++ ++ else /* Color-map has sRGB values */ ++ { ++ png_const_bytep entry = png_voidcast(png_const_bytep, cmap); ++ ++ entry += (unsigned int)i * channels; ++ ++ switch (channels) ++ { ++ case 4: ++ tRNS[i] = entry[afirst ? 0 : 3]; ++ if (tRNS[i] < 255) ++ num_trans = i+1; ++ /* FALLTHROUGH */ ++ case 3: ++ palette[i].blue = entry[afirst + (2 ^ bgr)]; ++ palette[i].green = entry[afirst + 1]; ++ palette[i].red = entry[afirst + bgr]; ++ break; ++ ++ case 2: ++ tRNS[i] = entry[1 ^ afirst]; ++ if (tRNS[i] < 255) ++ num_trans = i+1; ++ /* FALLTHROUGH */ ++ case 1: ++ palette[i].blue = palette[i].red = palette[i].green = ++ entry[afirst]; ++ break; ++ ++ default: ++ break; ++ } ++ } ++ } ++ ++# ifdef afirst ++# undef afirst ++# endif ++# ifdef bgr ++# undef bgr ++# endif ++ ++ png_set_PLTE(image->opaque->png_ptr, image->opaque->info_ptr, palette, ++ entries); ++ ++ if (num_trans > 0) ++ png_set_tRNS(image->opaque->png_ptr, image->opaque->info_ptr, tRNS, ++ num_trans, NULL); ++ ++ image->colormap_entries = (png_uint_32)entries; ++} ++ ++static int ++png_image_write_main(png_voidp argument) ++{ ++ png_image_write_control *display = png_voidcast(png_image_write_control*, ++ argument); ++ png_imagep image = display->image; ++ png_structrp png_ptr = image->opaque->png_ptr; ++ png_inforp info_ptr = image->opaque->info_ptr; ++ png_uint_32 format = image->format; ++ ++ /* The following four ints are actually booleans */ ++ int colormap = (format & PNG_FORMAT_FLAG_COLORMAP); ++ int linear = !colormap && (format & PNG_FORMAT_FLAG_LINEAR); /* input */ ++ int alpha = !colormap && (format & PNG_FORMAT_FLAG_ALPHA); ++ int write_16bit = linear && (display->convert_to_8bit == 0); ++ ++# ifdef PNG_BENIGN_ERRORS_SUPPORTED ++ /* Make sure we error out on any bad situation */ ++ png_set_benign_errors(png_ptr, 0/*error*/); ++# endif ++ ++ /* Default the 'row_stride' parameter if required, also check the row stride ++ * and total image size to ensure that they are within the system limits. ++ */ ++ { ++ unsigned int channels = PNG_IMAGE_PIXEL_CHANNELS(image->format); ++ ++ if (image->width <= 0x7fffffffU/channels) /* no overflow */ ++ { ++ png_uint_32 check; ++ png_uint_32 png_row_stride = image->width * channels; ++ ++ if (display->row_stride == 0) ++ display->row_stride = (png_int_32)/*SAFE*/png_row_stride; ++ ++ if (display->row_stride < 0) ++ check = (png_uint_32)(-display->row_stride); ++ ++ else ++ check = (png_uint_32)display->row_stride; ++ ++ if (check >= png_row_stride) ++ { ++ /* Now check for overflow of the image buffer calculation; this ++ * limits the whole image size to 32 bits for API compatibility with ++ * the current, 32-bit, PNG_IMAGE_BUFFER_SIZE macro. ++ */ ++ if (image->height > 0xffffffffU/png_row_stride) ++ png_error(image->opaque->png_ptr, "memory image too large"); ++ } ++ ++ else ++ png_error(image->opaque->png_ptr, "supplied row stride too small"); ++ } ++ ++ else ++ png_error(image->opaque->png_ptr, "image row stride too large"); ++ } ++ ++ /* Set the required transforms then write the rows in the correct order. */ ++ if ((format & PNG_FORMAT_FLAG_COLORMAP) != 0) ++ { ++ if (display->colormap != NULL && image->colormap_entries > 0) ++ { ++ png_uint_32 entries = image->colormap_entries; ++ ++ png_set_IHDR(png_ptr, info_ptr, image->width, image->height, ++ entries > 16 ? 8 : (entries > 4 ? 4 : (entries > 2 ? 2 : 1)), ++ PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE, ++ PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); ++ ++ png_image_set_PLTE(display); ++ } ++ ++ else ++ png_error(image->opaque->png_ptr, ++ "no color-map for color-mapped image"); ++ } ++ ++ else ++ png_set_IHDR(png_ptr, info_ptr, image->width, image->height, ++ write_16bit ? 16 : 8, ++ ((format & PNG_FORMAT_FLAG_COLOR) ? PNG_COLOR_MASK_COLOR : 0) + ++ ((format & PNG_FORMAT_FLAG_ALPHA) ? PNG_COLOR_MASK_ALPHA : 0), ++ PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); ++ ++ /* Counter-intuitively the data transformations must be called *after* ++ * png_write_info, not before as in the read code, but the 'set' functions ++ * must still be called before. Just set the color space information, never ++ * write an interlaced image. ++ */ ++ ++ if (write_16bit != 0) ++ { ++ /* The gamma here is 1.0 (linear) and the cHRM chunk matches sRGB. */ ++ png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_LINEAR); ++ ++ if ((image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB) == 0) ++ png_set_cHRM_fixed(png_ptr, info_ptr, ++ /* color x y */ ++ /* white */ 31270, 32900, ++ /* red */ 64000, 33000, ++ /* green */ 30000, 60000, ++ /* blue */ 15000, 6000 ++ ); ++ } ++ ++ else if ((image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB) == 0) ++ png_set_sRGB(png_ptr, info_ptr, PNG_sRGB_INTENT_PERCEPTUAL); ++ ++ /* Else writing an 8-bit file and the *colors* aren't sRGB, but the 8-bit ++ * space must still be gamma encoded. ++ */ ++ else ++ png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_sRGB_INVERSE); ++ ++ /* Write the file header. */ ++ png_write_info(png_ptr, info_ptr); ++ ++ /* Now set up the data transformations (*after* the header is written), ++ * remove the handled transformations from the 'format' flags for checking. ++ * ++ * First check for a little endian system if writing 16-bit files. ++ */ ++ if (write_16bit != 0) ++ { ++ png_uint_16 le = 0x0001; ++ ++ if ((*(png_const_bytep) & le) != 0) ++ png_set_swap(png_ptr); ++ } ++ ++# ifdef PNG_SIMPLIFIED_WRITE_BGR_SUPPORTED ++ if ((format & PNG_FORMAT_FLAG_BGR) != 0) ++ { ++ if (colormap == 0 && (format & PNG_FORMAT_FLAG_COLOR) != 0) ++ png_set_bgr(png_ptr); ++ format &= ~PNG_FORMAT_FLAG_BGR; ++ } ++# endif ++ ++# ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED ++ if ((format & PNG_FORMAT_FLAG_AFIRST) != 0) ++ { ++ if (colormap == 0 && (format & PNG_FORMAT_FLAG_ALPHA) != 0) ++ png_set_swap_alpha(png_ptr); ++ format &= ~PNG_FORMAT_FLAG_AFIRST; ++ } ++# endif ++ ++ /* If there are 16 or fewer color-map entries we wrote a lower bit depth ++ * above, but the application data is still byte packed. ++ */ ++ if (colormap != 0 && image->colormap_entries <= 16) ++ png_set_packing(png_ptr); ++ ++ /* That should have handled all (both) the transforms. */ ++ if ((format & ~(png_uint_32)(PNG_FORMAT_FLAG_COLOR | PNG_FORMAT_FLAG_LINEAR | ++ PNG_FORMAT_FLAG_ALPHA | PNG_FORMAT_FLAG_COLORMAP)) != 0) ++ png_error(png_ptr, "png_write_image: unsupported transformation"); ++ ++ { ++ png_const_bytep row = png_voidcast(png_const_bytep, display->buffer); ++ ptrdiff_t row_bytes = display->row_stride; ++ ++ if (linear != 0) ++ row_bytes *= (sizeof (png_uint_16)); ++ ++ if (row_bytes < 0) ++ row += (image->height-1) * (-row_bytes); ++ ++ display->first_row = row; ++ display->row_bytes = row_bytes; ++ } ++ ++ /* Apply 'fast' options if the flag is set. */ ++ if ((image->flags & PNG_IMAGE_FLAG_FAST) != 0) ++ { ++ png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, PNG_NO_FILTERS); ++ /* NOTE: determined by experiment using pngstest, this reflects some ++ * balance between the time to write the image once and the time to read ++ * it about 50 times. The speed-up in pngstest was about 10-20% of the ++ * total (user) time on a heavily loaded system. ++ */ ++# ifdef PNG_WRITE_CUSTOMIZE_COMPRESSION_SUPPORTED ++ png_set_compression_level(png_ptr, 3); ++# endif ++ } ++ ++ /* Check for the cases that currently require a pre-transform on the row ++ * before it is written. This only applies when the input is 16-bit and ++ * either there is an alpha channel or it is converted to 8-bit. ++ */ ++ if ((linear != 0 && alpha != 0 ) || ++ (colormap == 0 && display->convert_to_8bit != 0)) ++ { ++ png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr, ++ png_get_rowbytes(png_ptr, info_ptr))); ++ int result; ++ ++ display->local_row = row; ++ if (write_16bit != 0) ++ result = png_safe_execute(image, png_write_image_16bit, display); ++ else ++ result = png_safe_execute(image, png_write_image_8bit, display); ++ display->local_row = NULL; ++ ++ png_free(png_ptr, row); ++ ++ /* Skip the 'write_end' on error: */ ++ if (result == 0) ++ return 0; ++ } ++ ++ /* Otherwise this is the case where the input is in a format currently ++ * supported by the rest of the libpng write code; call it directly. ++ */ ++ else ++ { ++ png_const_bytep row = png_voidcast(png_const_bytep, display->first_row); ++ ptrdiff_t row_bytes = display->row_bytes; ++ png_uint_32 y = image->height; ++ ++ for (; y > 0; --y) ++ { ++ png_write_row(png_ptr, row); ++ row += row_bytes; ++ } ++ } ++ ++ png_write_end(png_ptr, info_ptr); ++ return 1; ++} ++ ++ ++static void (PNGCBAPI ++image_memory_write)(png_structp png_ptr, png_bytep/*const*/ data, size_t size) ++{ ++ png_image_write_control *display = png_voidcast(png_image_write_control*, ++ png_ptr->io_ptr/*backdoor: png_get_io_ptr(png_ptr)*/); ++ png_alloc_size_t ob = display->output_bytes; ++ ++ /* Check for overflow; this should never happen: */ ++ if (size <= ((png_alloc_size_t)-1) - ob) ++ { ++ /* I don't think libpng ever does this, but just in case: */ ++ if (size > 0) ++ { ++ if (display->memory_bytes >= ob+size) /* writing */ ++ memcpy(display->memory+ob, data, size); ++ ++ /* Always update the size: */ ++ display->output_bytes = ob+size; ++ } ++ } ++ ++ else ++ png_error(png_ptr, "png_image_write_to_memory: PNG too big"); ++} ++ ++static void (PNGCBAPI ++image_memory_flush)(png_structp png_ptr) ++{ ++ PNG_UNUSED(png_ptr) ++} ++ ++static int ++png_image_write_memory(png_voidp argument) ++{ ++ png_image_write_control *display = png_voidcast(png_image_write_control*, ++ argument); ++ ++ /* The rest of the memory-specific init and write_main in an error protected ++ * environment. This case needs to use callbacks for the write operations ++ * since libpng has no built in support for writing to memory. ++ */ ++ png_set_write_fn(display->image->opaque->png_ptr, display/*io_ptr*/, ++ image_memory_write, image_memory_flush); ++ ++ return png_image_write_main(display); ++} ++ ++int PNGAPI ++png_image_write_to_memory(png_imagep image, void *memory, ++ png_alloc_size_t * PNG_RESTRICT memory_bytes, int convert_to_8bit, ++ const void *buffer, png_int_32 row_stride, const void *colormap) ++{ ++ /* Write the image to the given buffer, or count the bytes if it is NULL */ ++ if (image != NULL && image->version == PNG_IMAGE_VERSION) ++ { ++ if (memory_bytes != NULL && buffer != NULL) ++ { ++ /* This is to give the caller an easier error detection in the NULL ++ * case and guard against uninitialized variable problems: ++ */ ++ if (memory == NULL) ++ *memory_bytes = 0; ++ ++ if (png_image_write_init(image) != 0) ++ { ++ png_image_write_control display; ++ int result; ++ ++ memset(&display, 0, (sizeof display)); ++ display.image = image; ++ display.buffer = buffer; ++ display.row_stride = row_stride; ++ display.colormap = colormap; ++ display.convert_to_8bit = convert_to_8bit; ++ display.memory = png_voidcast(png_bytep, memory); ++ display.memory_bytes = *memory_bytes; ++ display.output_bytes = 0; ++ ++ result = png_safe_execute(image, png_image_write_memory, &display); ++ png_image_free(image); ++ ++ /* write_memory returns true even if we ran out of buffer. */ ++ if (result) ++ { ++ /* On out-of-buffer this function returns '0' but still updates ++ * memory_bytes: ++ */ ++ if (memory != NULL && display.output_bytes > *memory_bytes) ++ result = 0; ++ ++ *memory_bytes = display.output_bytes; ++ } ++ ++ return result; ++ } ++ ++ else ++ return 0; ++ } ++ ++ else ++ return png_image_error(image, ++ "png_image_write_to_memory: invalid argument"); ++ } ++ ++ else if (image != NULL) ++ return png_image_error(image, ++ "png_image_write_to_memory: incorrect PNG_IMAGE_VERSION"); ++ ++ else ++ return 0; ++} ++ ++#ifdef PNG_SIMPLIFIED_WRITE_STDIO_SUPPORTED ++int PNGAPI ++png_image_write_to_stdio(png_imagep image, FILE *file, int convert_to_8bit, ++ const void *buffer, png_int_32 row_stride, const void *colormap) ++{ ++ /* Write the image to the given (FILE*). */ ++ if (image != NULL && image->version == PNG_IMAGE_VERSION) ++ { ++ if (file != NULL && buffer != NULL) ++ { ++ if (png_image_write_init(image) != 0) ++ { ++ png_image_write_control display; ++ int result; ++ ++ /* This is slightly evil, but png_init_io doesn't do anything other ++ * than this and we haven't changed the standard IO functions so ++ * this saves a 'safe' function. ++ */ ++ image->opaque->png_ptr->io_ptr = file; ++ ++ memset(&display, 0, (sizeof display)); ++ display.image = image; ++ display.buffer = buffer; ++ display.row_stride = row_stride; ++ display.colormap = colormap; ++ display.convert_to_8bit = convert_to_8bit; ++ ++ result = png_safe_execute(image, png_image_write_main, &display); ++ png_image_free(image); ++ return result; ++ } ++ ++ else ++ return 0; ++ } ++ ++ else ++ return png_image_error(image, ++ "png_image_write_to_stdio: invalid argument"); ++ } ++ ++ else if (image != NULL) ++ return png_image_error(image, ++ "png_image_write_to_stdio: incorrect PNG_IMAGE_VERSION"); ++ ++ else ++ return 0; ++} ++ ++int PNGAPI ++png_image_write_to_file(png_imagep image, const char *file_name, ++ int convert_to_8bit, const void *buffer, png_int_32 row_stride, ++ const void *colormap) ++{ ++ /* Write the image to the named file. */ ++ if (image != NULL && image->version == PNG_IMAGE_VERSION) ++ { ++ if (file_name != NULL && buffer != NULL) ++ { ++ FILE *fp = fopen(file_name, "wb"); ++ ++ if (fp != NULL) ++ { ++ if (png_image_write_to_stdio(image, fp, convert_to_8bit, buffer, ++ row_stride, colormap) != 0) ++ { ++ int error; /* from fflush/fclose */ ++ ++ /* Make sure the file is flushed correctly. */ ++ if (fflush(fp) == 0 && ferror(fp) == 0) ++ { ++ if (fclose(fp) == 0) ++ return 1; ++ ++ error = errno; /* from fclose */ ++ } ++ ++ else ++ { ++ error = errno; /* from fflush or ferror */ ++ (void)fclose(fp); ++ } ++ ++ (void)remove(file_name); ++ /* The image has already been cleaned up; this is just used to ++ * set the error (because the original write succeeded). ++ */ ++ return png_image_error(image, strerror(error)); ++ } ++ ++ else ++ { ++ /* Clean up: just the opened file. */ ++ (void)fclose(fp); ++ (void)remove(file_name); ++ return 0; ++ } ++ } ++ ++ else ++ return png_image_error(image, strerror(errno)); ++ } ++ ++ else ++ return png_image_error(image, ++ "png_image_write_to_file: invalid argument"); ++ } ++ ++ else if (image != NULL) ++ return png_image_error(image, ++ "png_image_write_to_file: incorrect PNG_IMAGE_VERSION"); ++ ++ else ++ return 0; ++} ++#endif /* SIMPLIFIED_WRITE_STDIO */ ++#endif /* SIMPLIFIED_WRITE */ ++#endif /* WRITE */ +diff --git a/lib/libpng/pngwtran.c b/lib/libpng/pngwtran.c +new file mode 100644 +index 000000000..49a13c1e9 +--- /dev/null ++++ b/lib/libpng/pngwtran.c +@@ -0,0 +1,575 @@ ++ ++/* pngwtran.c - transforms the data in a row for PNG writers ++ * ++ * Copyright (c) 2018 Cosmin Truta ++ * Copyright (c) 1998-2002,2004,2006-2016,2018 Glenn Randers-Pehrson ++ * Copyright (c) 1996-1997 Andreas Dilger ++ * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc. ++ * ++ * This code is released under the libpng license. ++ * For conditions of distribution and use, see the disclaimer ++ * and license in png.h ++ */ ++ ++#include "pngpriv.h" ++ ++#ifdef PNG_WRITE_SUPPORTED ++#ifdef PNG_WRITE_TRANSFORMS_SUPPORTED ++ ++#ifdef PNG_WRITE_PACK_SUPPORTED ++/* Pack pixels into bytes. Pass the true bit depth in bit_depth. The ++ * row_info bit depth should be 8 (one pixel per byte). The channels ++ * should be 1 (this only happens on grayscale and paletted images). ++ */ ++static void ++png_do_pack(png_row_infop row_info, png_bytep row, png_uint_32 bit_depth) ++{ ++ png_debug(1, "in png_do_pack"); ++ ++ if (row_info->bit_depth == 8 && ++ row_info->channels == 1) ++ { ++ switch ((int)bit_depth) ++ { ++ case 1: ++ { ++ png_bytep sp, dp; ++ int mask, v; ++ png_uint_32 i; ++ png_uint_32 row_width = row_info->width; ++ ++ sp = row; ++ dp = row; ++ mask = 0x80; ++ v = 0; ++ ++ for (i = 0; i < row_width; i++) ++ { ++ if (*sp != 0) ++ v |= mask; ++ ++ sp++; ++ ++ if (mask > 1) ++ mask >>= 1; ++ ++ else ++ { ++ mask = 0x80; ++ *dp = (png_byte)v; ++ dp++; ++ v = 0; ++ } ++ } ++ ++ if (mask != 0x80) ++ *dp = (png_byte)v; ++ ++ break; ++ } ++ ++ case 2: ++ { ++ png_bytep sp, dp; ++ unsigned int shift; ++ int v; ++ png_uint_32 i; ++ png_uint_32 row_width = row_info->width; ++ ++ sp = row; ++ dp = row; ++ shift = 6; ++ v = 0; ++ ++ for (i = 0; i < row_width; i++) ++ { ++ png_byte value; ++ ++ value = (png_byte)(*sp & 0x03); ++ v |= (value << shift); ++ ++ if (shift == 0) ++ { ++ shift = 6; ++ *dp = (png_byte)v; ++ dp++; ++ v = 0; ++ } ++ ++ else ++ shift -= 2; ++ ++ sp++; ++ } ++ ++ if (shift != 6) ++ *dp = (png_byte)v; ++ ++ break; ++ } ++ ++ case 4: ++ { ++ png_bytep sp, dp; ++ unsigned int shift; ++ int v; ++ png_uint_32 i; ++ png_uint_32 row_width = row_info->width; ++ ++ sp = row; ++ dp = row; ++ shift = 4; ++ v = 0; ++ ++ for (i = 0; i < row_width; i++) ++ { ++ png_byte value; ++ ++ value = (png_byte)(*sp & 0x0f); ++ v |= (value << shift); ++ ++ if (shift == 0) ++ { ++ shift = 4; ++ *dp = (png_byte)v; ++ dp++; ++ v = 0; ++ } ++ ++ else ++ shift -= 4; ++ ++ sp++; ++ } ++ ++ if (shift != 4) ++ *dp = (png_byte)v; ++ ++ break; ++ } ++ ++ default: ++ break; ++ } ++ ++ row_info->bit_depth = (png_byte)bit_depth; ++ row_info->pixel_depth = (png_byte)(bit_depth * row_info->channels); ++ row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, ++ row_info->width); ++ } ++} ++#endif ++ ++#ifdef PNG_WRITE_SHIFT_SUPPORTED ++/* Shift pixel values to take advantage of whole range. Pass the ++ * true number of bits in bit_depth. The row should be packed ++ * according to row_info->bit_depth. Thus, if you had a row of ++ * bit depth 4, but the pixels only had values from 0 to 7, you ++ * would pass 3 as bit_depth, and this routine would translate the ++ * data to 0 to 15. ++ */ ++static void ++png_do_shift(png_row_infop row_info, png_bytep row, ++ png_const_color_8p bit_depth) ++{ ++ png_debug(1, "in png_do_shift"); ++ ++ if (row_info->color_type != PNG_COLOR_TYPE_PALETTE) ++ { ++ int shift_start[4], shift_dec[4]; ++ unsigned int channels = 0; ++ ++ if ((row_info->color_type & PNG_COLOR_MASK_COLOR) != 0) ++ { ++ shift_start[channels] = row_info->bit_depth - bit_depth->red; ++ shift_dec[channels] = bit_depth->red; ++ channels++; ++ ++ shift_start[channels] = row_info->bit_depth - bit_depth->green; ++ shift_dec[channels] = bit_depth->green; ++ channels++; ++ ++ shift_start[channels] = row_info->bit_depth - bit_depth->blue; ++ shift_dec[channels] = bit_depth->blue; ++ channels++; ++ } ++ ++ else ++ { ++ shift_start[channels] = row_info->bit_depth - bit_depth->gray; ++ shift_dec[channels] = bit_depth->gray; ++ channels++; ++ } ++ ++ if ((row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0) ++ { ++ shift_start[channels] = row_info->bit_depth - bit_depth->alpha; ++ shift_dec[channels] = bit_depth->alpha; ++ channels++; ++ } ++ ++ /* With low row depths, could only be grayscale, so one channel */ ++ if (row_info->bit_depth < 8) ++ { ++ png_bytep bp = row; ++ size_t i; ++ unsigned int mask; ++ size_t row_bytes = row_info->rowbytes; ++ ++ if (bit_depth->gray == 1 && row_info->bit_depth == 2) ++ mask = 0x55; ++ ++ else if (row_info->bit_depth == 4 && bit_depth->gray == 3) ++ mask = 0x11; ++ ++ else ++ mask = 0xff; ++ ++ for (i = 0; i < row_bytes; i++, bp++) ++ { ++ int j; ++ unsigned int v, out; ++ ++ v = *bp; ++ out = 0; ++ ++ for (j = shift_start[0]; j > -shift_dec[0]; j -= shift_dec[0]) ++ { ++ if (j > 0) ++ out |= v << j; ++ ++ else ++ out |= (v >> (-j)) & mask; ++ } ++ ++ *bp = (png_byte)(out & 0xff); ++ } ++ } ++ ++ else if (row_info->bit_depth == 8) ++ { ++ png_bytep bp = row; ++ png_uint_32 i; ++ png_uint_32 istop = channels * row_info->width; ++ ++ for (i = 0; i < istop; i++, bp++) ++ { ++ unsigned int c = i%channels; ++ int j; ++ unsigned int v, out; ++ ++ v = *bp; ++ out = 0; ++ ++ for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c]) ++ { ++ if (j > 0) ++ out |= v << j; ++ ++ else ++ out |= v >> (-j); ++ } ++ ++ *bp = (png_byte)(out & 0xff); ++ } ++ } ++ ++ else ++ { ++ png_bytep bp; ++ png_uint_32 i; ++ png_uint_32 istop = channels * row_info->width; ++ ++ for (bp = row, i = 0; i < istop; i++) ++ { ++ unsigned int c = i%channels; ++ int j; ++ unsigned int value, v; ++ ++ v = png_get_uint_16(bp); ++ value = 0; ++ ++ for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c]) ++ { ++ if (j > 0) ++ value |= v << j; ++ ++ else ++ value |= v >> (-j); ++ } ++ *bp++ = (png_byte)((value >> 8) & 0xff); ++ *bp++ = (png_byte)(value & 0xff); ++ } ++ } ++ } ++} ++#endif ++ ++#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED ++static void ++png_do_write_swap_alpha(png_row_infop row_info, png_bytep row) ++{ ++ png_debug(1, "in png_do_write_swap_alpha"); ++ ++ { ++ if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) ++ { ++ if (row_info->bit_depth == 8) ++ { ++ /* This converts from ARGB to RGBA */ ++ png_bytep sp, dp; ++ png_uint_32 i; ++ png_uint_32 row_width = row_info->width; ++ ++ for (i = 0, sp = dp = row; i < row_width; i++) ++ { ++ png_byte save = *(sp++); ++ *(dp++) = *(sp++); ++ *(dp++) = *(sp++); ++ *(dp++) = *(sp++); ++ *(dp++) = save; ++ } ++ } ++ ++#ifdef PNG_WRITE_16BIT_SUPPORTED ++ else ++ { ++ /* This converts from AARRGGBB to RRGGBBAA */ ++ png_bytep sp, dp; ++ png_uint_32 i; ++ png_uint_32 row_width = row_info->width; ++ ++ for (i = 0, sp = dp = row; i < row_width; i++) ++ { ++ png_byte save[2]; ++ save[0] = *(sp++); ++ save[1] = *(sp++); ++ *(dp++) = *(sp++); ++ *(dp++) = *(sp++); ++ *(dp++) = *(sp++); ++ *(dp++) = *(sp++); ++ *(dp++) = *(sp++); ++ *(dp++) = *(sp++); ++ *(dp++) = save[0]; ++ *(dp++) = save[1]; ++ } ++ } ++#endif /* WRITE_16BIT */ ++ } ++ ++ else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ++ { ++ if (row_info->bit_depth == 8) ++ { ++ /* This converts from AG to GA */ ++ png_bytep sp, dp; ++ png_uint_32 i; ++ png_uint_32 row_width = row_info->width; ++ ++ for (i = 0, sp = dp = row; i < row_width; i++) ++ { ++ png_byte save = *(sp++); ++ *(dp++) = *(sp++); ++ *(dp++) = save; ++ } ++ } ++ ++#ifdef PNG_WRITE_16BIT_SUPPORTED ++ else ++ { ++ /* This converts from AAGG to GGAA */ ++ png_bytep sp, dp; ++ png_uint_32 i; ++ png_uint_32 row_width = row_info->width; ++ ++ for (i = 0, sp = dp = row; i < row_width; i++) ++ { ++ png_byte save[2]; ++ save[0] = *(sp++); ++ save[1] = *(sp++); ++ *(dp++) = *(sp++); ++ *(dp++) = *(sp++); ++ *(dp++) = save[0]; ++ *(dp++) = save[1]; ++ } ++ } ++#endif /* WRITE_16BIT */ ++ } ++ } ++} ++#endif ++ ++#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED ++static void ++png_do_write_invert_alpha(png_row_infop row_info, png_bytep row) ++{ ++ png_debug(1, "in png_do_write_invert_alpha"); ++ ++ { ++ if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA) ++ { ++ if (row_info->bit_depth == 8) ++ { ++ /* This inverts the alpha channel in RGBA */ ++ png_bytep sp, dp; ++ png_uint_32 i; ++ png_uint_32 row_width = row_info->width; ++ ++ for (i = 0, sp = dp = row; i < row_width; i++) ++ { ++ /* Does nothing ++ *(dp++) = *(sp++); ++ *(dp++) = *(sp++); ++ *(dp++) = *(sp++); ++ */ ++ sp+=3; dp = sp; ++ *dp = (png_byte)(255 - *(sp++)); ++ } ++ } ++ ++#ifdef PNG_WRITE_16BIT_SUPPORTED ++ else ++ { ++ /* This inverts the alpha channel in RRGGBBAA */ ++ png_bytep sp, dp; ++ png_uint_32 i; ++ png_uint_32 row_width = row_info->width; ++ ++ for (i = 0, sp = dp = row; i < row_width; i++) ++ { ++ /* Does nothing ++ *(dp++) = *(sp++); ++ *(dp++) = *(sp++); ++ *(dp++) = *(sp++); ++ *(dp++) = *(sp++); ++ *(dp++) = *(sp++); ++ *(dp++) = *(sp++); ++ */ ++ sp+=6; dp = sp; ++ *(dp++) = (png_byte)(255 - *(sp++)); ++ *dp = (png_byte)(255 - *(sp++)); ++ } ++ } ++#endif /* WRITE_16BIT */ ++ } ++ ++ else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ++ { ++ if (row_info->bit_depth == 8) ++ { ++ /* This inverts the alpha channel in GA */ ++ png_bytep sp, dp; ++ png_uint_32 i; ++ png_uint_32 row_width = row_info->width; ++ ++ for (i = 0, sp = dp = row; i < row_width; i++) ++ { ++ *(dp++) = *(sp++); ++ *(dp++) = (png_byte)(255 - *(sp++)); ++ } ++ } ++ ++#ifdef PNG_WRITE_16BIT_SUPPORTED ++ else ++ { ++ /* This inverts the alpha channel in GGAA */ ++ png_bytep sp, dp; ++ png_uint_32 i; ++ png_uint_32 row_width = row_info->width; ++ ++ for (i = 0, sp = dp = row; i < row_width; i++) ++ { ++ /* Does nothing ++ *(dp++) = *(sp++); ++ *(dp++) = *(sp++); ++ */ ++ sp+=2; dp = sp; ++ *(dp++) = (png_byte)(255 - *(sp++)); ++ *dp = (png_byte)(255 - *(sp++)); ++ } ++ } ++#endif /* WRITE_16BIT */ ++ } ++ } ++} ++#endif ++ ++/* Transform the data according to the user's wishes. The order of ++ * transformations is significant. ++ */ ++void /* PRIVATE */ ++png_do_write_transformations(png_structrp png_ptr, png_row_infop row_info) ++{ ++ png_debug(1, "in png_do_write_transformations"); ++ ++ if (png_ptr == NULL) ++ return; ++ ++#ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED ++ if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0) ++ if (png_ptr->write_user_transform_fn != NULL) ++ (*(png_ptr->write_user_transform_fn)) /* User write transform ++ function */ ++ (png_ptr, /* png_ptr */ ++ row_info, /* row_info: */ ++ /* png_uint_32 width; width of row */ ++ /* size_t rowbytes; number of bytes in row */ ++ /* png_byte color_type; color type of pixels */ ++ /* png_byte bit_depth; bit depth of samples */ ++ /* png_byte channels; number of channels (1-4) */ ++ /* png_byte pixel_depth; bits per pixel (depth*channels) */ ++ png_ptr->row_buf + 1); /* start of pixel data for row */ ++#endif ++ ++#ifdef PNG_WRITE_FILLER_SUPPORTED ++ if ((png_ptr->transformations & PNG_FILLER) != 0) ++ png_do_strip_channel(row_info, png_ptr->row_buf + 1, ++ !(png_ptr->flags & PNG_FLAG_FILLER_AFTER)); ++#endif ++ ++#ifdef PNG_WRITE_PACKSWAP_SUPPORTED ++ if ((png_ptr->transformations & PNG_PACKSWAP) != 0) ++ png_do_packswap(row_info, png_ptr->row_buf + 1); ++#endif ++ ++#ifdef PNG_WRITE_PACK_SUPPORTED ++ if ((png_ptr->transformations & PNG_PACK) != 0) ++ png_do_pack(row_info, png_ptr->row_buf + 1, ++ (png_uint_32)png_ptr->bit_depth); ++#endif ++ ++#ifdef PNG_WRITE_SWAP_SUPPORTED ++# ifdef PNG_16BIT_SUPPORTED ++ if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0) ++ png_do_swap(row_info, png_ptr->row_buf + 1); ++# endif ++#endif ++ ++#ifdef PNG_WRITE_SHIFT_SUPPORTED ++ if ((png_ptr->transformations & PNG_SHIFT) != 0) ++ png_do_shift(row_info, png_ptr->row_buf + 1, ++ &(png_ptr->shift)); ++#endif ++ ++#ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED ++ if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0) ++ png_do_write_swap_alpha(row_info, png_ptr->row_buf + 1); ++#endif ++ ++#ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED ++ if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0) ++ png_do_write_invert_alpha(row_info, png_ptr->row_buf + 1); ++#endif ++ ++#ifdef PNG_WRITE_BGR_SUPPORTED ++ if ((png_ptr->transformations & PNG_BGR) != 0) ++ png_do_bgr(row_info, png_ptr->row_buf + 1); ++#endif ++ ++#ifdef PNG_WRITE_INVERT_SUPPORTED ++ if ((png_ptr->transformations & PNG_INVERT_MONO) != 0) ++ png_do_invert(row_info, png_ptr->row_buf + 1); ++#endif ++} ++#endif /* WRITE_TRANSFORMS */ ++#endif /* WRITE */ +diff --git a/lib/libpng/pngwutil.c b/lib/libpng/pngwutil.c +new file mode 100644 +index 000000000..16345e4c0 +--- /dev/null ++++ b/lib/libpng/pngwutil.c +@@ -0,0 +1,2781 @@ ++ ++/* pngwutil.c - utilities to write a PNG file ++ * ++ * Copyright (c) 2018 Cosmin Truta ++ * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson ++ * Copyright (c) 1996-1997 Andreas Dilger ++ * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc. ++ * ++ * This code is released under the libpng license. ++ * For conditions of distribution and use, see the disclaimer ++ * and license in png.h ++ */ ++ ++#include "pngpriv.h" ++ ++#ifdef PNG_WRITE_SUPPORTED ++ ++#ifdef PNG_WRITE_INT_FUNCTIONS_SUPPORTED ++/* Place a 32-bit number into a buffer in PNG byte order. We work ++ * with unsigned numbers for convenience, although one supported ++ * ancillary chunk uses signed (two's complement) numbers. ++ */ ++void PNGAPI ++png_save_uint_32(png_bytep buf, png_uint_32 i) ++{ ++ buf[0] = (png_byte)((i >> 24) & 0xffU); ++ buf[1] = (png_byte)((i >> 16) & 0xffU); ++ buf[2] = (png_byte)((i >> 8) & 0xffU); ++ buf[3] = (png_byte)( i & 0xffU); ++} ++ ++/* Place a 16-bit number into a buffer in PNG byte order. ++ * The parameter is declared unsigned int, not png_uint_16, ++ * just to avoid potential problems on pre-ANSI C compilers. ++ */ ++void PNGAPI ++png_save_uint_16(png_bytep buf, unsigned int i) ++{ ++ buf[0] = (png_byte)((i >> 8) & 0xffU); ++ buf[1] = (png_byte)( i & 0xffU); ++} ++#endif ++ ++/* Simple function to write the signature. If we have already written ++ * the magic bytes of the signature, or more likely, the PNG stream is ++ * being embedded into another stream and doesn't need its own signature, ++ * we should call png_set_sig_bytes() to tell libpng how many of the ++ * bytes have already been written. ++ */ ++void PNGAPI ++png_write_sig(png_structrp png_ptr) ++{ ++ png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10}; ++ ++#ifdef PNG_IO_STATE_SUPPORTED ++ /* Inform the I/O callback that the signature is being written */ ++ png_ptr->io_state = PNG_IO_WRITING | PNG_IO_SIGNATURE; ++#endif ++ ++ /* Write the rest of the 8 byte signature */ ++ png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes], ++ (size_t)(8 - png_ptr->sig_bytes)); ++ ++ if (png_ptr->sig_bytes < 3) ++ png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE; ++} ++ ++/* Write the start of a PNG chunk. The type is the chunk type. ++ * The total_length is the sum of the lengths of all the data you will be ++ * passing in png_write_chunk_data(). ++ */ ++static void ++png_write_chunk_header(png_structrp png_ptr, png_uint_32 chunk_name, ++ png_uint_32 length) ++{ ++ png_byte buf[8]; ++ ++#if defined(PNG_DEBUG) && (PNG_DEBUG > 0) ++ PNG_CSTRING_FROM_CHUNK(buf, chunk_name); ++ png_debug2(0, "Writing %s chunk, length = %lu", buf, (unsigned long)length); ++#endif ++ ++ if (png_ptr == NULL) ++ return; ++ ++#ifdef PNG_IO_STATE_SUPPORTED ++ /* Inform the I/O callback that the chunk header is being written. ++ * PNG_IO_CHUNK_HDR requires a single I/O call. ++ */ ++ png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_HDR; ++#endif ++ ++ /* Write the length and the chunk name */ ++ png_save_uint_32(buf, length); ++ png_save_uint_32(buf + 4, chunk_name); ++ png_write_data(png_ptr, buf, 8); ++ ++ /* Put the chunk name into png_ptr->chunk_name */ ++ png_ptr->chunk_name = chunk_name; ++ ++ /* Reset the crc and run it over the chunk name */ ++ png_reset_crc(png_ptr); ++ ++ png_calculate_crc(png_ptr, buf + 4, 4); ++ ++#ifdef PNG_IO_STATE_SUPPORTED ++ /* Inform the I/O callback that chunk data will (possibly) be written. ++ * PNG_IO_CHUNK_DATA does NOT require a specific number of I/O calls. ++ */ ++ png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_DATA; ++#endif ++} ++ ++void PNGAPI ++png_write_chunk_start(png_structrp png_ptr, png_const_bytep chunk_string, ++ png_uint_32 length) ++{ ++ png_write_chunk_header(png_ptr, PNG_CHUNK_FROM_STRING(chunk_string), length); ++} ++ ++/* Write the data of a PNG chunk started with png_write_chunk_header(). ++ * Note that multiple calls to this function are allowed, and that the ++ * sum of the lengths from these calls *must* add up to the total_length ++ * given to png_write_chunk_header(). ++ */ ++void PNGAPI ++png_write_chunk_data(png_structrp png_ptr, png_const_bytep data, size_t length) ++{ ++ /* Write the data, and run the CRC over it */ ++ if (png_ptr == NULL) ++ return; ++ ++ if (data != NULL && length > 0) ++ { ++ png_write_data(png_ptr, data, length); ++ ++ /* Update the CRC after writing the data, ++ * in case the user I/O routine alters it. ++ */ ++ png_calculate_crc(png_ptr, data, length); ++ } ++} ++ ++/* Finish a chunk started with png_write_chunk_header(). */ ++void PNGAPI ++png_write_chunk_end(png_structrp png_ptr) ++{ ++ png_byte buf[4]; ++ ++ if (png_ptr == NULL) return; ++ ++#ifdef PNG_IO_STATE_SUPPORTED ++ /* Inform the I/O callback that the chunk CRC is being written. ++ * PNG_IO_CHUNK_CRC requires a single I/O function call. ++ */ ++ png_ptr->io_state = PNG_IO_WRITING | PNG_IO_CHUNK_CRC; ++#endif ++ ++ /* Write the crc in a single operation */ ++ png_save_uint_32(buf, png_ptr->crc); ++ ++ png_write_data(png_ptr, buf, 4); ++} ++ ++/* Write a PNG chunk all at once. The type is an array of ASCII characters ++ * representing the chunk name. The array must be at least 4 bytes in ++ * length, and does not need to be null terminated. To be safe, pass the ++ * pre-defined chunk names here, and if you need a new one, define it ++ * where the others are defined. The length is the length of the data. ++ * All the data must be present. If that is not possible, use the ++ * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end() ++ * functions instead. ++ */ ++static void ++png_write_complete_chunk(png_structrp png_ptr, png_uint_32 chunk_name, ++ png_const_bytep data, size_t length) ++{ ++ if (png_ptr == NULL) ++ return; ++ ++ /* On 64-bit architectures 'length' may not fit in a png_uint_32. */ ++ if (length > PNG_UINT_31_MAX) ++ png_error(png_ptr, "length exceeds PNG maximum"); ++ ++ png_write_chunk_header(png_ptr, chunk_name, (png_uint_32)length); ++ png_write_chunk_data(png_ptr, data, length); ++ png_write_chunk_end(png_ptr); ++} ++ ++/* This is the API that calls the internal function above. */ ++void PNGAPI ++png_write_chunk(png_structrp png_ptr, png_const_bytep chunk_string, ++ png_const_bytep data, size_t length) ++{ ++ png_write_complete_chunk(png_ptr, PNG_CHUNK_FROM_STRING(chunk_string), data, ++ length); ++} ++ ++/* This is used below to find the size of an image to pass to png_deflate_claim, ++ * so it only needs to be accurate if the size is less than 16384 bytes (the ++ * point at which a lower LZ window size can be used.) ++ */ ++static png_alloc_size_t ++png_image_size(png_structrp png_ptr) ++{ ++ /* Only return sizes up to the maximum of a png_uint_32; do this by limiting ++ * the width and height used to 15 bits. ++ */ ++ png_uint_32 h = png_ptr->height; ++ ++ if (png_ptr->rowbytes < 32768 && h < 32768) ++ { ++ if (png_ptr->interlaced != 0) ++ { ++ /* Interlacing makes the image larger because of the replication of ++ * both the filter byte and the padding to a byte boundary. ++ */ ++ png_uint_32 w = png_ptr->width; ++ unsigned int pd = png_ptr->pixel_depth; ++ png_alloc_size_t cb_base; ++ int pass; ++ ++ for (cb_base=0, pass=0; pass<=6; ++pass) ++ { ++ png_uint_32 pw = PNG_PASS_COLS(w, pass); ++ ++ if (pw > 0) ++ cb_base += (PNG_ROWBYTES(pd, pw)+1) * PNG_PASS_ROWS(h, pass); ++ } ++ ++ return cb_base; ++ } ++ ++ else ++ return (png_ptr->rowbytes+1) * h; ++ } ++ ++ else ++ return 0xffffffffU; ++} ++ ++#ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED ++ /* This is the code to hack the first two bytes of the deflate stream (the ++ * deflate header) to correct the windowBits value to match the actual data ++ * size. Note that the second argument is the *uncompressed* size but the ++ * first argument is the *compressed* data (and it must be deflate ++ * compressed.) ++ */ ++static void ++optimize_cmf(png_bytep data, png_alloc_size_t data_size) ++{ ++ /* Optimize the CMF field in the zlib stream. The resultant zlib stream is ++ * still compliant to the stream specification. ++ */ ++ if (data_size <= 16384) /* else windowBits must be 15 */ ++ { ++ unsigned int z_cmf = data[0]; /* zlib compression method and flags */ ++ ++ if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70) ++ { ++ unsigned int z_cinfo; ++ unsigned int half_z_window_size; ++ ++ z_cinfo = z_cmf >> 4; ++ half_z_window_size = 1U << (z_cinfo + 7); ++ ++ if (data_size <= half_z_window_size) /* else no change */ ++ { ++ unsigned int tmp; ++ ++ do ++ { ++ half_z_window_size >>= 1; ++ --z_cinfo; ++ } ++ while (z_cinfo > 0 && data_size <= half_z_window_size); ++ ++ z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4); ++ ++ data[0] = (png_byte)z_cmf; ++ tmp = data[1] & 0xe0; ++ tmp += 0x1f - ((z_cmf << 8) + tmp) % 0x1f; ++ data[1] = (png_byte)tmp; ++ } ++ } ++ } ++} ++#endif /* WRITE_OPTIMIZE_CMF */ ++ ++/* Initialize the compressor for the appropriate type of compression. */ ++static int ++png_deflate_claim(png_structrp png_ptr, png_uint_32 owner, ++ png_alloc_size_t data_size) ++{ ++ if (png_ptr->zowner != 0) ++ { ++#if defined(PNG_WARNINGS_SUPPORTED) || defined(PNG_ERROR_TEXT_SUPPORTED) ++ char msg[64]; ++ ++ PNG_STRING_FROM_CHUNK(msg, owner); ++ msg[4] = ':'; ++ msg[5] = ' '; ++ PNG_STRING_FROM_CHUNK(msg+6, png_ptr->zowner); ++ /* So the message that results is " using zstream"; this is an ++ * internal error, but is very useful for debugging. i18n requirements ++ * are minimal. ++ */ ++ (void)png_safecat(msg, (sizeof msg), 10, " using zstream"); ++#endif ++#if PNG_RELEASE_BUILD ++ png_warning(png_ptr, msg); ++ ++ /* Attempt sane error recovery */ ++ if (png_ptr->zowner == png_IDAT) /* don't steal from IDAT */ ++ { ++ png_ptr->zstream.msg = PNGZ_MSG_CAST("in use by IDAT"); ++ return Z_STREAM_ERROR; ++ } ++ ++ png_ptr->zowner = 0; ++#else ++ png_error(png_ptr, msg); ++#endif ++ } ++ ++ { ++ int level = png_ptr->zlib_level; ++ int method = png_ptr->zlib_method; ++ int windowBits = png_ptr->zlib_window_bits; ++ int memLevel = png_ptr->zlib_mem_level; ++ int strategy; /* set below */ ++ int ret; /* zlib return code */ ++ ++ if (owner == png_IDAT) ++ { ++ if ((png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY) != 0) ++ strategy = png_ptr->zlib_strategy; ++ ++ else if (png_ptr->do_filter != PNG_FILTER_NONE) ++ strategy = PNG_Z_DEFAULT_STRATEGY; ++ ++ else ++ strategy = PNG_Z_DEFAULT_NOFILTER_STRATEGY; ++ } ++ ++ else ++ { ++#ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED ++ level = png_ptr->zlib_text_level; ++ method = png_ptr->zlib_text_method; ++ windowBits = png_ptr->zlib_text_window_bits; ++ memLevel = png_ptr->zlib_text_mem_level; ++ strategy = png_ptr->zlib_text_strategy; ++#else ++ /* If customization is not supported the values all come from the ++ * IDAT values except for the strategy, which is fixed to the ++ * default. (This is the pre-1.6.0 behavior too, although it was ++ * implemented in a very different way.) ++ */ ++ strategy = Z_DEFAULT_STRATEGY; ++#endif ++ } ++ ++ /* Adjust 'windowBits' down if larger than 'data_size'; to stop this ++ * happening just pass 32768 as the data_size parameter. Notice that zlib ++ * requires an extra 262 bytes in the window in addition to the data to be ++ * able to see the whole of the data, so if data_size+262 takes us to the ++ * next windowBits size we need to fix up the value later. (Because even ++ * though deflate needs the extra window, inflate does not!) ++ */ ++ if (data_size <= 16384) ++ { ++ /* IMPLEMENTATION NOTE: this 'half_window_size' stuff is only here to ++ * work round a Microsoft Visual C misbehavior which, contrary to C-90, ++ * widens the result of the following shift to 64-bits if (and, ++ * apparently, only if) it is used in a test. ++ */ ++ unsigned int half_window_size = 1U << (windowBits-1); ++ ++ while (data_size + 262 <= half_window_size) ++ { ++ half_window_size >>= 1; ++ --windowBits; ++ } ++ } ++ ++ /* Check against the previous initialized values, if any. */ ++ if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0 && ++ (png_ptr->zlib_set_level != level || ++ png_ptr->zlib_set_method != method || ++ png_ptr->zlib_set_window_bits != windowBits || ++ png_ptr->zlib_set_mem_level != memLevel || ++ png_ptr->zlib_set_strategy != strategy)) ++ { ++ if (deflateEnd(&png_ptr->zstream) != Z_OK) ++ png_warning(png_ptr, "deflateEnd failed (ignored)"); ++ ++ png_ptr->flags &= ~PNG_FLAG_ZSTREAM_INITIALIZED; ++ } ++ ++ /* For safety clear out the input and output pointers (currently zlib ++ * doesn't use them on Init, but it might in the future). ++ */ ++ png_ptr->zstream.next_in = NULL; ++ png_ptr->zstream.avail_in = 0; ++ png_ptr->zstream.next_out = NULL; ++ png_ptr->zstream.avail_out = 0; ++ ++ /* Now initialize if required, setting the new parameters, otherwise just ++ * do a simple reset to the previous parameters. ++ */ ++ if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0) ++ ret = deflateReset(&png_ptr->zstream); ++ ++ else ++ { ++ ret = deflateInit2(&png_ptr->zstream, level, method, windowBits, ++ memLevel, strategy); ++ ++ if (ret == Z_OK) ++ png_ptr->flags |= PNG_FLAG_ZSTREAM_INITIALIZED; ++ } ++ ++ /* The return code is from either deflateReset or deflateInit2; they have ++ * pretty much the same set of error codes. ++ */ ++ if (ret == Z_OK) ++ png_ptr->zowner = owner; ++ ++ else ++ png_zstream_error(png_ptr, ret); ++ ++ return ret; ++ } ++} ++ ++/* Clean up (or trim) a linked list of compression buffers. */ ++void /* PRIVATE */ ++png_free_buffer_list(png_structrp png_ptr, png_compression_bufferp *listp) ++{ ++ png_compression_bufferp list = *listp; ++ ++ if (list != NULL) ++ { ++ *listp = NULL; ++ ++ do ++ { ++ png_compression_bufferp next = list->next; ++ ++ png_free(png_ptr, list); ++ list = next; ++ } ++ while (list != NULL); ++ } ++} ++ ++#ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED ++/* This pair of functions encapsulates the operation of (a) compressing a ++ * text string, and (b) issuing it later as a series of chunk data writes. ++ * The compression_state structure is shared context for these functions ++ * set up by the caller to allow access to the relevant local variables. ++ * ++ * compression_buffer (new in 1.6.0) is just a linked list of zbuffer_size ++ * temporary buffers. From 1.6.0 it is retained in png_struct so that it will ++ * be correctly freed in the event of a write error (previous implementations ++ * just leaked memory.) ++ */ ++typedef struct ++{ ++ png_const_bytep input; /* The uncompressed input data */ ++ png_alloc_size_t input_len; /* Its length */ ++ png_uint_32 output_len; /* Final compressed length */ ++ png_byte output[1024]; /* First block of output */ ++} compression_state; ++ ++static void ++png_text_compress_init(compression_state *comp, png_const_bytep input, ++ png_alloc_size_t input_len) ++{ ++ comp->input = input; ++ comp->input_len = input_len; ++ comp->output_len = 0; ++} ++ ++/* Compress the data in the compression state input */ ++static int ++png_text_compress(png_structrp png_ptr, png_uint_32 chunk_name, ++ compression_state *comp, png_uint_32 prefix_len) ++{ ++ int ret; ++ ++ /* To find the length of the output it is necessary to first compress the ++ * input. The result is buffered rather than using the two-pass algorithm ++ * that is used on the inflate side; deflate is assumed to be slower and a ++ * PNG writer is assumed to have more memory available than a PNG reader. ++ * ++ * IMPLEMENTATION NOTE: the zlib API deflateBound() can be used to find an ++ * upper limit on the output size, but it is always bigger than the input ++ * size so it is likely to be more efficient to use this linked-list ++ * approach. ++ */ ++ ret = png_deflate_claim(png_ptr, chunk_name, comp->input_len); ++ ++ if (ret != Z_OK) ++ return ret; ++ ++ /* Set up the compression buffers, we need a loop here to avoid overflowing a ++ * uInt. Use ZLIB_IO_MAX to limit the input. The output is always limited ++ * by the output buffer size, so there is no need to check that. Since this ++ * is ANSI-C we know that an 'int', hence a uInt, is always at least 16 bits ++ * in size. ++ */ ++ { ++ png_compression_bufferp *end = &png_ptr->zbuffer_list; ++ png_alloc_size_t input_len = comp->input_len; /* may be zero! */ ++ png_uint_32 output_len; ++ ++ /* zlib updates these for us: */ ++ png_ptr->zstream.next_in = PNGZ_INPUT_CAST(comp->input); ++ png_ptr->zstream.avail_in = 0; /* Set below */ ++ png_ptr->zstream.next_out = comp->output; ++ png_ptr->zstream.avail_out = (sizeof comp->output); ++ ++ output_len = png_ptr->zstream.avail_out; ++ ++ do ++ { ++ uInt avail_in = ZLIB_IO_MAX; ++ ++ if (avail_in > input_len) ++ avail_in = (uInt)input_len; ++ ++ input_len -= avail_in; ++ ++ png_ptr->zstream.avail_in = avail_in; ++ ++ if (png_ptr->zstream.avail_out == 0) ++ { ++ png_compression_buffer *next; ++ ++ /* Chunk data is limited to 2^31 bytes in length, so the prefix ++ * length must be counted here. ++ */ ++ if (output_len + prefix_len > PNG_UINT_31_MAX) ++ { ++ ret = Z_MEM_ERROR; ++ break; ++ } ++ ++ /* Need a new (malloc'ed) buffer, but there may be one present ++ * already. ++ */ ++ next = *end; ++ if (next == NULL) ++ { ++ next = png_voidcast(png_compression_bufferp, png_malloc_base ++ (png_ptr, PNG_COMPRESSION_BUFFER_SIZE(png_ptr))); ++ ++ if (next == NULL) ++ { ++ ret = Z_MEM_ERROR; ++ break; ++ } ++ ++ /* Link in this buffer (so that it will be freed later) */ ++ next->next = NULL; ++ *end = next; ++ } ++ ++ png_ptr->zstream.next_out = next->output; ++ png_ptr->zstream.avail_out = png_ptr->zbuffer_size; ++ output_len += png_ptr->zstream.avail_out; ++ ++ /* Move 'end' to the next buffer pointer. */ ++ end = &next->next; ++ } ++ ++ /* Compress the data */ ++ ret = deflate(&png_ptr->zstream, ++ input_len > 0 ? Z_NO_FLUSH : Z_FINISH); ++ ++ /* Claw back input data that was not consumed (because avail_in is ++ * reset above every time round the loop). ++ */ ++ input_len += png_ptr->zstream.avail_in; ++ png_ptr->zstream.avail_in = 0; /* safety */ ++ } ++ while (ret == Z_OK); ++ ++ /* There may be some space left in the last output buffer. This needs to ++ * be subtracted from output_len. ++ */ ++ output_len -= png_ptr->zstream.avail_out; ++ png_ptr->zstream.avail_out = 0; /* safety */ ++ comp->output_len = output_len; ++ ++ /* Now double check the output length, put in a custom message if it is ++ * too long. Otherwise ensure the z_stream::msg pointer is set to ++ * something. ++ */ ++ if (output_len + prefix_len >= PNG_UINT_31_MAX) ++ { ++ png_ptr->zstream.msg = PNGZ_MSG_CAST("compressed data too long"); ++ ret = Z_MEM_ERROR; ++ } ++ ++ else ++ png_zstream_error(png_ptr, ret); ++ ++ /* Reset zlib for another zTXt/iTXt or image data */ ++ png_ptr->zowner = 0; ++ ++ /* The only success case is Z_STREAM_END, input_len must be 0; if not this ++ * is an internal error. ++ */ ++ if (ret == Z_STREAM_END && input_len == 0) ++ { ++#ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED ++ /* Fix up the deflate header, if required */ ++ optimize_cmf(comp->output, comp->input_len); ++#endif ++ /* But Z_OK is returned, not Z_STREAM_END; this allows the claim ++ * function above to return Z_STREAM_END on an error (though it never ++ * does in the current versions of zlib.) ++ */ ++ return Z_OK; ++ } ++ ++ else ++ return ret; ++ } ++} ++ ++/* Ship the compressed text out via chunk writes */ ++static void ++png_write_compressed_data_out(png_structrp png_ptr, compression_state *comp) ++{ ++ png_uint_32 output_len = comp->output_len; ++ png_const_bytep output = comp->output; ++ png_uint_32 avail = (sizeof comp->output); ++ png_compression_buffer *next = png_ptr->zbuffer_list; ++ ++ for (;;) ++ { ++ if (avail > output_len) ++ avail = output_len; ++ ++ png_write_chunk_data(png_ptr, output, avail); ++ ++ output_len -= avail; ++ ++ if (output_len == 0 || next == NULL) ++ break; ++ ++ avail = png_ptr->zbuffer_size; ++ output = next->output; ++ next = next->next; ++ } ++ ++ /* This is an internal error; 'next' must have been NULL! */ ++ if (output_len > 0) ++ png_error(png_ptr, "error writing ancillary chunked compressed data"); ++} ++#endif /* WRITE_COMPRESSED_TEXT */ ++ ++/* Write the IHDR chunk, and update the png_struct with the necessary ++ * information. Note that the rest of this code depends upon this ++ * information being correct. ++ */ ++void /* PRIVATE */ ++png_write_IHDR(png_structrp png_ptr, png_uint_32 width, png_uint_32 height, ++ int bit_depth, int color_type, int compression_type, int filter_type, ++ int interlace_type) ++{ ++ png_byte buf[13]; /* Buffer to store the IHDR info */ ++ int is_invalid_depth; ++ ++ png_debug(1, "in png_write_IHDR"); ++ ++ /* Check that we have valid input data from the application info */ ++ switch (color_type) ++ { ++ case PNG_COLOR_TYPE_GRAY: ++ switch (bit_depth) ++ { ++ case 1: ++ case 2: ++ case 4: ++ case 8: ++#ifdef PNG_WRITE_16BIT_SUPPORTED ++ case 16: ++#endif ++ png_ptr->channels = 1; break; ++ ++ default: ++ png_error(png_ptr, ++ "Invalid bit depth for grayscale image"); ++ } ++ break; ++ ++ case PNG_COLOR_TYPE_RGB: ++ is_invalid_depth = (bit_depth != 8); ++#ifdef PNG_WRITE_16BIT_SUPPORTED ++ is_invalid_depth = (is_invalid_depth && bit_depth != 16); ++#endif ++ if (is_invalid_depth) ++ png_error(png_ptr, "Invalid bit depth for RGB image"); ++ ++ png_ptr->channels = 3; ++ break; ++ ++ case PNG_COLOR_TYPE_PALETTE: ++ switch (bit_depth) ++ { ++ case 1: ++ case 2: ++ case 4: ++ case 8: ++ png_ptr->channels = 1; ++ break; ++ ++ default: ++ png_error(png_ptr, "Invalid bit depth for paletted image"); ++ } ++ break; ++ ++ case PNG_COLOR_TYPE_GRAY_ALPHA: ++ is_invalid_depth = (bit_depth != 8); ++#ifdef PNG_WRITE_16BIT_SUPPORTED ++ is_invalid_depth = (is_invalid_depth && bit_depth != 16); ++#endif ++ if (is_invalid_depth) ++ png_error(png_ptr, "Invalid bit depth for grayscale+alpha image"); ++ ++ png_ptr->channels = 2; ++ break; ++ ++ case PNG_COLOR_TYPE_RGB_ALPHA: ++ is_invalid_depth = (bit_depth != 8); ++#ifdef PNG_WRITE_16BIT_SUPPORTED ++ is_invalid_depth = (is_invalid_depth && bit_depth != 16); ++#endif ++ if (is_invalid_depth) ++ png_error(png_ptr, "Invalid bit depth for RGBA image"); ++ ++ png_ptr->channels = 4; ++ break; ++ ++ default: ++ png_error(png_ptr, "Invalid image color type specified"); ++ } ++ ++ if (compression_type != PNG_COMPRESSION_TYPE_BASE) ++ { ++ png_warning(png_ptr, "Invalid compression type specified"); ++ compression_type = PNG_COMPRESSION_TYPE_BASE; ++ } ++ ++ /* Write filter_method 64 (intrapixel differencing) only if ++ * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and ++ * 2. Libpng did not write a PNG signature (this filter_method is only ++ * used in PNG datastreams that are embedded in MNG datastreams) and ++ * 3. The application called png_permit_mng_features with a mask that ++ * included PNG_FLAG_MNG_FILTER_64 and ++ * 4. The filter_method is 64 and ++ * 5. The color_type is RGB or RGBA ++ */ ++ if ( ++#ifdef PNG_MNG_FEATURES_SUPPORTED ++ !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 && ++ ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) == 0) && ++ (color_type == PNG_COLOR_TYPE_RGB || ++ color_type == PNG_COLOR_TYPE_RGB_ALPHA) && ++ (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) && ++#endif ++ filter_type != PNG_FILTER_TYPE_BASE) ++ { ++ png_warning(png_ptr, "Invalid filter type specified"); ++ filter_type = PNG_FILTER_TYPE_BASE; ++ } ++ ++#ifdef PNG_WRITE_INTERLACING_SUPPORTED ++ if (interlace_type != PNG_INTERLACE_NONE && ++ interlace_type != PNG_INTERLACE_ADAM7) ++ { ++ png_warning(png_ptr, "Invalid interlace type specified"); ++ interlace_type = PNG_INTERLACE_ADAM7; ++ } ++#else ++ interlace_type=PNG_INTERLACE_NONE; ++#endif ++ ++ /* Save the relevant information */ ++ png_ptr->bit_depth = (png_byte)bit_depth; ++ png_ptr->color_type = (png_byte)color_type; ++ png_ptr->interlaced = (png_byte)interlace_type; ++#ifdef PNG_MNG_FEATURES_SUPPORTED ++ png_ptr->filter_type = (png_byte)filter_type; ++#endif ++ png_ptr->compression_type = (png_byte)compression_type; ++ png_ptr->width = width; ++ png_ptr->height = height; ++ ++ png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels); ++ png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width); ++ /* Set the usr info, so any transformations can modify it */ ++ png_ptr->usr_width = png_ptr->width; ++ png_ptr->usr_bit_depth = png_ptr->bit_depth; ++ png_ptr->usr_channels = png_ptr->channels; ++ ++ /* Pack the header information into the buffer */ ++ png_save_uint_32(buf, width); ++ png_save_uint_32(buf + 4, height); ++ buf[8] = (png_byte)bit_depth; ++ buf[9] = (png_byte)color_type; ++ buf[10] = (png_byte)compression_type; ++ buf[11] = (png_byte)filter_type; ++ buf[12] = (png_byte)interlace_type; ++ ++ /* Write the chunk */ ++ png_write_complete_chunk(png_ptr, png_IHDR, buf, 13); ++ ++ if ((png_ptr->do_filter) == PNG_NO_FILTERS) ++ { ++ if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE || ++ png_ptr->bit_depth < 8) ++ png_ptr->do_filter = PNG_FILTER_NONE; ++ ++ else ++ png_ptr->do_filter = PNG_ALL_FILTERS; ++ } ++ ++ png_ptr->mode = PNG_HAVE_IHDR; /* not READY_FOR_ZTXT */ ++} ++ ++/* Write the palette. We are careful not to trust png_color to be in the ++ * correct order for PNG, so people can redefine it to any convenient ++ * structure. ++ */ ++void /* PRIVATE */ ++png_write_PLTE(png_structrp png_ptr, png_const_colorp palette, ++ png_uint_32 num_pal) ++{ ++ png_uint_32 max_palette_length, i; ++ png_const_colorp pal_ptr; ++ png_byte buf[3]; ++ ++ png_debug(1, "in png_write_PLTE"); ++ ++ max_palette_length = (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ? ++ (1 << png_ptr->bit_depth) : PNG_MAX_PALETTE_LENGTH; ++ ++ if (( ++#ifdef PNG_MNG_FEATURES_SUPPORTED ++ (png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) == 0 && ++#endif ++ num_pal == 0) || num_pal > max_palette_length) ++ { ++ if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) ++ { ++ png_error(png_ptr, "Invalid number of colors in palette"); ++ } ++ ++ else ++ { ++ png_warning(png_ptr, "Invalid number of colors in palette"); ++ return; ++ } ++ } ++ ++ if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0) ++ { ++ png_warning(png_ptr, ++ "Ignoring request to write a PLTE chunk in grayscale PNG"); ++ ++ return; ++ } ++ ++ png_ptr->num_palette = (png_uint_16)num_pal; ++ png_debug1(3, "num_palette = %d", png_ptr->num_palette); ++ ++ png_write_chunk_header(png_ptr, png_PLTE, (png_uint_32)(num_pal * 3)); ++#ifdef PNG_POINTER_INDEXING_SUPPORTED ++ ++ for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++) ++ { ++ buf[0] = pal_ptr->red; ++ buf[1] = pal_ptr->green; ++ buf[2] = pal_ptr->blue; ++ png_write_chunk_data(png_ptr, buf, 3); ++ } ++ ++#else ++ /* This is a little slower but some buggy compilers need to do this ++ * instead ++ */ ++ pal_ptr=palette; ++ ++ for (i = 0; i < num_pal; i++) ++ { ++ buf[0] = pal_ptr[i].red; ++ buf[1] = pal_ptr[i].green; ++ buf[2] = pal_ptr[i].blue; ++ png_write_chunk_data(png_ptr, buf, 3); ++ } ++ ++#endif ++ png_write_chunk_end(png_ptr); ++ png_ptr->mode |= PNG_HAVE_PLTE; ++} ++ ++/* This is similar to png_text_compress, above, except that it does not require ++ * all of the data at once and, instead of buffering the compressed result, ++ * writes it as IDAT chunks. Unlike png_text_compress it *can* png_error out ++ * because it calls the write interface. As a result it does its own error ++ * reporting and does not return an error code. In the event of error it will ++ * just call png_error. The input data length may exceed 32-bits. The 'flush' ++ * parameter is exactly the same as that to deflate, with the following ++ * meanings: ++ * ++ * Z_NO_FLUSH: normal incremental output of compressed data ++ * Z_SYNC_FLUSH: do a SYNC_FLUSH, used by png_write_flush ++ * Z_FINISH: this is the end of the input, do a Z_FINISH and clean up ++ * ++ * The routine manages the acquire and release of the png_ptr->zstream by ++ * checking and (at the end) clearing png_ptr->zowner; it does some sanity ++ * checks on the 'mode' flags while doing this. ++ */ ++void /* PRIVATE */ ++png_compress_IDAT(png_structrp png_ptr, png_const_bytep input, ++ png_alloc_size_t input_len, int flush) ++{ ++ if (png_ptr->zowner != png_IDAT) ++ { ++ /* First time. Ensure we have a temporary buffer for compression and ++ * trim the buffer list if it has more than one entry to free memory. ++ * If 'WRITE_COMPRESSED_TEXT' is not set the list will never have been ++ * created at this point, but the check here is quick and safe. ++ */ ++ if (png_ptr->zbuffer_list == NULL) ++ { ++ png_ptr->zbuffer_list = png_voidcast(png_compression_bufferp, ++ png_malloc(png_ptr, PNG_COMPRESSION_BUFFER_SIZE(png_ptr))); ++ png_ptr->zbuffer_list->next = NULL; ++ } ++ ++ else ++ png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list->next); ++ ++ /* It is a terminal error if we can't claim the zstream. */ ++ if (png_deflate_claim(png_ptr, png_IDAT, png_image_size(png_ptr)) != Z_OK) ++ png_error(png_ptr, png_ptr->zstream.msg); ++ ++ /* The output state is maintained in png_ptr->zstream, so it must be ++ * initialized here after the claim. ++ */ ++ png_ptr->zstream.next_out = png_ptr->zbuffer_list->output; ++ png_ptr->zstream.avail_out = png_ptr->zbuffer_size; ++ } ++ ++ /* Now loop reading and writing until all the input is consumed or an error ++ * terminates the operation. The _out values are maintained across calls to ++ * this function, but the input must be reset each time. ++ */ ++ png_ptr->zstream.next_in = PNGZ_INPUT_CAST(input); ++ png_ptr->zstream.avail_in = 0; /* set below */ ++ for (;;) ++ { ++ int ret; ++ ++ /* INPUT: from the row data */ ++ uInt avail = ZLIB_IO_MAX; ++ ++ if (avail > input_len) ++ avail = (uInt)input_len; /* safe because of the check */ ++ ++ png_ptr->zstream.avail_in = avail; ++ input_len -= avail; ++ ++ ret = deflate(&png_ptr->zstream, input_len > 0 ? Z_NO_FLUSH : flush); ++ ++ /* Include as-yet unconsumed input */ ++ input_len += png_ptr->zstream.avail_in; ++ png_ptr->zstream.avail_in = 0; ++ ++ /* OUTPUT: write complete IDAT chunks when avail_out drops to zero. Note ++ * that these two zstream fields are preserved across the calls, therefore ++ * there is no need to set these up on entry to the loop. ++ */ ++ if (png_ptr->zstream.avail_out == 0) ++ { ++ png_bytep data = png_ptr->zbuffer_list->output; ++ uInt size = png_ptr->zbuffer_size; ++ ++ /* Write an IDAT containing the data then reset the buffer. The ++ * first IDAT may need deflate header optimization. ++ */ ++#ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED ++ if ((png_ptr->mode & PNG_HAVE_IDAT) == 0 && ++ png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE) ++ optimize_cmf(data, png_image_size(png_ptr)); ++#endif ++ ++ if (size > 0) ++ png_write_complete_chunk(png_ptr, png_IDAT, data, size); ++ png_ptr->mode |= PNG_HAVE_IDAT; ++ ++ png_ptr->zstream.next_out = data; ++ png_ptr->zstream.avail_out = size; ++ ++ /* For SYNC_FLUSH or FINISH it is essential to keep calling zlib with ++ * the same flush parameter until it has finished output, for NO_FLUSH ++ * it doesn't matter. ++ */ ++ if (ret == Z_OK && flush != Z_NO_FLUSH) ++ continue; ++ } ++ ++ /* The order of these checks doesn't matter much; it just affects which ++ * possible error might be detected if multiple things go wrong at once. ++ */ ++ if (ret == Z_OK) /* most likely return code! */ ++ { ++ /* If all the input has been consumed then just return. If Z_FINISH ++ * was used as the flush parameter something has gone wrong if we get ++ * here. ++ */ ++ if (input_len == 0) ++ { ++ if (flush == Z_FINISH) ++ png_error(png_ptr, "Z_OK on Z_FINISH with output space"); ++ ++ return; ++ } ++ } ++ ++ else if (ret == Z_STREAM_END && flush == Z_FINISH) ++ { ++ /* This is the end of the IDAT data; any pending output must be ++ * flushed. For small PNG files we may still be at the beginning. ++ */ ++ png_bytep data = png_ptr->zbuffer_list->output; ++ uInt size = png_ptr->zbuffer_size - png_ptr->zstream.avail_out; ++ ++#ifdef PNG_WRITE_OPTIMIZE_CMF_SUPPORTED ++ if ((png_ptr->mode & PNG_HAVE_IDAT) == 0 && ++ png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE) ++ optimize_cmf(data, png_image_size(png_ptr)); ++#endif ++ ++ if (size > 0) ++ png_write_complete_chunk(png_ptr, png_IDAT, data, size); ++ png_ptr->zstream.avail_out = 0; ++ png_ptr->zstream.next_out = NULL; ++ png_ptr->mode |= PNG_HAVE_IDAT | PNG_AFTER_IDAT; ++ ++ png_ptr->zowner = 0; /* Release the stream */ ++ return; ++ } ++ ++ else ++ { ++ /* This is an error condition. */ ++ png_zstream_error(png_ptr, ret); ++ png_error(png_ptr, png_ptr->zstream.msg); ++ } ++ } ++} ++ ++/* Write an IEND chunk */ ++void /* PRIVATE */ ++png_write_IEND(png_structrp png_ptr) ++{ ++ png_debug(1, "in png_write_IEND"); ++ ++ png_write_complete_chunk(png_ptr, png_IEND, NULL, 0); ++ png_ptr->mode |= PNG_HAVE_IEND; ++} ++ ++#ifdef PNG_WRITE_gAMA_SUPPORTED ++/* Write a gAMA chunk */ ++void /* PRIVATE */ ++png_write_gAMA_fixed(png_structrp png_ptr, png_fixed_point file_gamma) ++{ ++ png_byte buf[4]; ++ ++ png_debug(1, "in png_write_gAMA"); ++ ++ /* file_gamma is saved in 1/100,000ths */ ++ png_save_uint_32(buf, (png_uint_32)file_gamma); ++ png_write_complete_chunk(png_ptr, png_gAMA, buf, 4); ++} ++#endif ++ ++#ifdef PNG_WRITE_sRGB_SUPPORTED ++/* Write a sRGB chunk */ ++void /* PRIVATE */ ++png_write_sRGB(png_structrp png_ptr, int srgb_intent) ++{ ++ png_byte buf[1]; ++ ++ png_debug(1, "in png_write_sRGB"); ++ ++ if (srgb_intent >= PNG_sRGB_INTENT_LAST) ++ png_warning(png_ptr, ++ "Invalid sRGB rendering intent specified"); ++ ++ buf[0]=(png_byte)srgb_intent; ++ png_write_complete_chunk(png_ptr, png_sRGB, buf, 1); ++} ++#endif ++ ++#ifdef PNG_WRITE_iCCP_SUPPORTED ++/* Write an iCCP chunk */ ++void /* PRIVATE */ ++png_write_iCCP(png_structrp png_ptr, png_const_charp name, ++ png_const_bytep profile) ++{ ++ png_uint_32 name_len; ++ png_uint_32 profile_len; ++ png_byte new_name[81]; /* 1 byte for the compression byte */ ++ compression_state comp; ++ png_uint_32 temp; ++ ++ png_debug(1, "in png_write_iCCP"); ++ ++ /* These are all internal problems: the profile should have been checked ++ * before when it was stored. ++ */ ++ if (profile == NULL) ++ png_error(png_ptr, "No profile for iCCP chunk"); /* internal error */ ++ ++ profile_len = png_get_uint_32(profile); ++ ++ if (profile_len < 132) ++ png_error(png_ptr, "ICC profile too short"); ++ ++ temp = (png_uint_32) (*(profile+8)); ++ if (temp > 3 && (profile_len & 0x03)) ++ png_error(png_ptr, "ICC profile length invalid (not a multiple of 4)"); ++ ++ { ++ png_uint_32 embedded_profile_len = png_get_uint_32(profile); ++ ++ if (profile_len != embedded_profile_len) ++ png_error(png_ptr, "Profile length does not match profile"); ++ } ++ ++ name_len = png_check_keyword(png_ptr, name, new_name); ++ ++ if (name_len == 0) ++ png_error(png_ptr, "iCCP: invalid keyword"); ++ ++ new_name[++name_len] = PNG_COMPRESSION_TYPE_BASE; ++ ++ /* Make sure we include the NULL after the name and the compression type */ ++ ++name_len; ++ ++ png_text_compress_init(&comp, profile, profile_len); ++ ++ /* Allow for keyword terminator and compression byte */ ++ if (png_text_compress(png_ptr, png_iCCP, &comp, name_len) != Z_OK) ++ png_error(png_ptr, png_ptr->zstream.msg); ++ ++ png_write_chunk_header(png_ptr, png_iCCP, name_len + comp.output_len); ++ ++ png_write_chunk_data(png_ptr, new_name, name_len); ++ ++ png_write_compressed_data_out(png_ptr, &comp); ++ ++ png_write_chunk_end(png_ptr); ++} ++#endif ++ ++#ifdef PNG_WRITE_sPLT_SUPPORTED ++/* Write a sPLT chunk */ ++void /* PRIVATE */ ++png_write_sPLT(png_structrp png_ptr, png_const_sPLT_tp spalette) ++{ ++ png_uint_32 name_len; ++ png_byte new_name[80]; ++ png_byte entrybuf[10]; ++ size_t entry_size = (spalette->depth == 8 ? 6 : 10); ++ size_t palette_size = entry_size * (size_t)spalette->nentries; ++ png_sPLT_entryp ep; ++#ifndef PNG_POINTER_INDEXING_SUPPORTED ++ int i; ++#endif ++ ++ png_debug(1, "in png_write_sPLT"); ++ ++ name_len = png_check_keyword(png_ptr, spalette->name, new_name); ++ ++ if (name_len == 0) ++ png_error(png_ptr, "sPLT: invalid keyword"); ++ ++ /* Make sure we include the NULL after the name */ ++ png_write_chunk_header(png_ptr, png_sPLT, ++ (png_uint_32)(name_len + 2 + palette_size)); ++ ++ png_write_chunk_data(png_ptr, (png_bytep)new_name, (size_t)(name_len + 1)); ++ ++ png_write_chunk_data(png_ptr, &spalette->depth, 1); ++ ++ /* Loop through each palette entry, writing appropriately */ ++#ifdef PNG_POINTER_INDEXING_SUPPORTED ++ for (ep = spalette->entries; epentries + spalette->nentries; ep++) ++ { ++ if (spalette->depth == 8) ++ { ++ entrybuf[0] = (png_byte)ep->red; ++ entrybuf[1] = (png_byte)ep->green; ++ entrybuf[2] = (png_byte)ep->blue; ++ entrybuf[3] = (png_byte)ep->alpha; ++ png_save_uint_16(entrybuf + 4, ep->frequency); ++ } ++ ++ else ++ { ++ png_save_uint_16(entrybuf + 0, ep->red); ++ png_save_uint_16(entrybuf + 2, ep->green); ++ png_save_uint_16(entrybuf + 4, ep->blue); ++ png_save_uint_16(entrybuf + 6, ep->alpha); ++ png_save_uint_16(entrybuf + 8, ep->frequency); ++ } ++ ++ png_write_chunk_data(png_ptr, entrybuf, entry_size); ++ } ++#else ++ ep=spalette->entries; ++ for (i = 0; i>spalette->nentries; i++) ++ { ++ if (spalette->depth == 8) ++ { ++ entrybuf[0] = (png_byte)ep[i].red; ++ entrybuf[1] = (png_byte)ep[i].green; ++ entrybuf[2] = (png_byte)ep[i].blue; ++ entrybuf[3] = (png_byte)ep[i].alpha; ++ png_save_uint_16(entrybuf + 4, ep[i].frequency); ++ } ++ ++ else ++ { ++ png_save_uint_16(entrybuf + 0, ep[i].red); ++ png_save_uint_16(entrybuf + 2, ep[i].green); ++ png_save_uint_16(entrybuf + 4, ep[i].blue); ++ png_save_uint_16(entrybuf + 6, ep[i].alpha); ++ png_save_uint_16(entrybuf + 8, ep[i].frequency); ++ } ++ ++ png_write_chunk_data(png_ptr, entrybuf, entry_size); ++ } ++#endif ++ ++ png_write_chunk_end(png_ptr); ++} ++#endif ++ ++#ifdef PNG_WRITE_sBIT_SUPPORTED ++/* Write the sBIT chunk */ ++void /* PRIVATE */ ++png_write_sBIT(png_structrp png_ptr, png_const_color_8p sbit, int color_type) ++{ ++ png_byte buf[4]; ++ size_t size; ++ ++ png_debug(1, "in png_write_sBIT"); ++ ++ /* Make sure we don't depend upon the order of PNG_COLOR_8 */ ++ if ((color_type & PNG_COLOR_MASK_COLOR) != 0) ++ { ++ png_byte maxbits; ++ ++ maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 : ++ png_ptr->usr_bit_depth); ++ ++ if (sbit->red == 0 || sbit->red > maxbits || ++ sbit->green == 0 || sbit->green > maxbits || ++ sbit->blue == 0 || sbit->blue > maxbits) ++ { ++ png_warning(png_ptr, "Invalid sBIT depth specified"); ++ return; ++ } ++ ++ buf[0] = sbit->red; ++ buf[1] = sbit->green; ++ buf[2] = sbit->blue; ++ size = 3; ++ } ++ ++ else ++ { ++ if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth) ++ { ++ png_warning(png_ptr, "Invalid sBIT depth specified"); ++ return; ++ } ++ ++ buf[0] = sbit->gray; ++ size = 1; ++ } ++ ++ if ((color_type & PNG_COLOR_MASK_ALPHA) != 0) ++ { ++ if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth) ++ { ++ png_warning(png_ptr, "Invalid sBIT depth specified"); ++ return; ++ } ++ ++ buf[size++] = sbit->alpha; ++ } ++ ++ png_write_complete_chunk(png_ptr, png_sBIT, buf, size); ++} ++#endif ++ ++#ifdef PNG_WRITE_cHRM_SUPPORTED ++/* Write the cHRM chunk */ ++void /* PRIVATE */ ++png_write_cHRM_fixed(png_structrp png_ptr, const png_xy *xy) ++{ ++ png_byte buf[32]; ++ ++ png_debug(1, "in png_write_cHRM"); ++ ++ /* Each value is saved in 1/100,000ths */ ++ png_save_int_32(buf, xy->whitex); ++ png_save_int_32(buf + 4, xy->whitey); ++ ++ png_save_int_32(buf + 8, xy->redx); ++ png_save_int_32(buf + 12, xy->redy); ++ ++ png_save_int_32(buf + 16, xy->greenx); ++ png_save_int_32(buf + 20, xy->greeny); ++ ++ png_save_int_32(buf + 24, xy->bluex); ++ png_save_int_32(buf + 28, xy->bluey); ++ ++ png_write_complete_chunk(png_ptr, png_cHRM, buf, 32); ++} ++#endif ++ ++#ifdef PNG_WRITE_tRNS_SUPPORTED ++/* Write the tRNS chunk */ ++void /* PRIVATE */ ++png_write_tRNS(png_structrp png_ptr, png_const_bytep trans_alpha, ++ png_const_color_16p tran, int num_trans, int color_type) ++{ ++ png_byte buf[6]; ++ ++ png_debug(1, "in png_write_tRNS"); ++ ++ if (color_type == PNG_COLOR_TYPE_PALETTE) ++ { ++ if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette) ++ { ++ png_app_warning(png_ptr, ++ "Invalid number of transparent colors specified"); ++ return; ++ } ++ ++ /* Write the chunk out as it is */ ++ png_write_complete_chunk(png_ptr, png_tRNS, trans_alpha, ++ (size_t)num_trans); ++ } ++ ++ else if (color_type == PNG_COLOR_TYPE_GRAY) ++ { ++ /* One 16-bit value */ ++ if (tran->gray >= (1 << png_ptr->bit_depth)) ++ { ++ png_app_warning(png_ptr, ++ "Ignoring attempt to write tRNS chunk out-of-range for bit_depth"); ++ ++ return; ++ } ++ ++ png_save_uint_16(buf, tran->gray); ++ png_write_complete_chunk(png_ptr, png_tRNS, buf, 2); ++ } ++ ++ else if (color_type == PNG_COLOR_TYPE_RGB) ++ { ++ /* Three 16-bit values */ ++ png_save_uint_16(buf, tran->red); ++ png_save_uint_16(buf + 2, tran->green); ++ png_save_uint_16(buf + 4, tran->blue); ++#ifdef PNG_WRITE_16BIT_SUPPORTED ++ if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]) != 0) ++#else ++ if ((buf[0] | buf[2] | buf[4]) != 0) ++#endif ++ { ++ png_app_warning(png_ptr, ++ "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8"); ++ return; ++ } ++ ++ png_write_complete_chunk(png_ptr, png_tRNS, buf, 6); ++ } ++ ++ else ++ { ++ png_app_warning(png_ptr, "Can't write tRNS with an alpha channel"); ++ } ++} ++#endif ++ ++#ifdef PNG_WRITE_bKGD_SUPPORTED ++/* Write the background chunk */ ++void /* PRIVATE */ ++png_write_bKGD(png_structrp png_ptr, png_const_color_16p back, int color_type) ++{ ++ png_byte buf[6]; ++ ++ png_debug(1, "in png_write_bKGD"); ++ ++ if (color_type == PNG_COLOR_TYPE_PALETTE) ++ { ++ if ( ++#ifdef PNG_MNG_FEATURES_SUPPORTED ++ (png_ptr->num_palette != 0 || ++ (png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) == 0) && ++#endif ++ back->index >= png_ptr->num_palette) ++ { ++ png_warning(png_ptr, "Invalid background palette index"); ++ return; ++ } ++ ++ buf[0] = back->index; ++ png_write_complete_chunk(png_ptr, png_bKGD, buf, 1); ++ } ++ ++ else if ((color_type & PNG_COLOR_MASK_COLOR) != 0) ++ { ++ png_save_uint_16(buf, back->red); ++ png_save_uint_16(buf + 2, back->green); ++ png_save_uint_16(buf + 4, back->blue); ++#ifdef PNG_WRITE_16BIT_SUPPORTED ++ if (png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]) != 0) ++#else ++ if ((buf[0] | buf[2] | buf[4]) != 0) ++#endif ++ { ++ png_warning(png_ptr, ++ "Ignoring attempt to write 16-bit bKGD chunk " ++ "when bit_depth is 8"); ++ ++ return; ++ } ++ ++ png_write_complete_chunk(png_ptr, png_bKGD, buf, 6); ++ } ++ ++ else ++ { ++ if (back->gray >= (1 << png_ptr->bit_depth)) ++ { ++ png_warning(png_ptr, ++ "Ignoring attempt to write bKGD chunk out-of-range for bit_depth"); ++ ++ return; ++ } ++ ++ png_save_uint_16(buf, back->gray); ++ png_write_complete_chunk(png_ptr, png_bKGD, buf, 2); ++ } ++} ++#endif ++ ++#ifdef PNG_WRITE_eXIf_SUPPORTED ++/* Write the Exif data */ ++void /* PRIVATE */ ++png_write_eXIf(png_structrp png_ptr, png_bytep exif, int num_exif) ++{ ++ int i; ++ png_byte buf[1]; ++ ++ png_debug(1, "in png_write_eXIf"); ++ ++ png_write_chunk_header(png_ptr, png_eXIf, (png_uint_32)(num_exif)); ++ ++ for (i = 0; i < num_exif; i++) ++ { ++ buf[0] = exif[i]; ++ png_write_chunk_data(png_ptr, buf, 1); ++ } ++ ++ png_write_chunk_end(png_ptr); ++} ++#endif ++ ++#ifdef PNG_WRITE_hIST_SUPPORTED ++/* Write the histogram */ ++void /* PRIVATE */ ++png_write_hIST(png_structrp png_ptr, png_const_uint_16p hist, int num_hist) ++{ ++ int i; ++ png_byte buf[3]; ++ ++ png_debug(1, "in png_write_hIST"); ++ ++ if (num_hist > (int)png_ptr->num_palette) ++ { ++ png_debug2(3, "num_hist = %d, num_palette = %d", num_hist, ++ png_ptr->num_palette); ++ ++ png_warning(png_ptr, "Invalid number of histogram entries specified"); ++ return; ++ } ++ ++ png_write_chunk_header(png_ptr, png_hIST, (png_uint_32)(num_hist * 2)); ++ ++ for (i = 0; i < num_hist; i++) ++ { ++ png_save_uint_16(buf, hist[i]); ++ png_write_chunk_data(png_ptr, buf, 2); ++ } ++ ++ png_write_chunk_end(png_ptr); ++} ++#endif ++ ++#ifdef PNG_WRITE_tEXt_SUPPORTED ++/* Write a tEXt chunk */ ++void /* PRIVATE */ ++png_write_tEXt(png_structrp png_ptr, png_const_charp key, png_const_charp text, ++ size_t text_len) ++{ ++ png_uint_32 key_len; ++ png_byte new_key[80]; ++ ++ png_debug(1, "in png_write_tEXt"); ++ ++ key_len = png_check_keyword(png_ptr, key, new_key); ++ ++ if (key_len == 0) ++ png_error(png_ptr, "tEXt: invalid keyword"); ++ ++ if (text == NULL || *text == '\0') ++ text_len = 0; ++ ++ else ++ text_len = strlen(text); ++ ++ if (text_len > PNG_UINT_31_MAX - (key_len+1)) ++ png_error(png_ptr, "tEXt: text too long"); ++ ++ /* Make sure we include the 0 after the key */ ++ png_write_chunk_header(png_ptr, png_tEXt, ++ (png_uint_32)/*checked above*/(key_len + text_len + 1)); ++ /* ++ * We leave it to the application to meet PNG-1.0 requirements on the ++ * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of ++ * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them. ++ * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG. ++ */ ++ png_write_chunk_data(png_ptr, new_key, key_len + 1); ++ ++ if (text_len != 0) ++ png_write_chunk_data(png_ptr, (png_const_bytep)text, text_len); ++ ++ png_write_chunk_end(png_ptr); ++} ++#endif ++ ++#ifdef PNG_WRITE_zTXt_SUPPORTED ++/* Write a compressed text chunk */ ++void /* PRIVATE */ ++png_write_zTXt(png_structrp png_ptr, png_const_charp key, png_const_charp text, ++ int compression) ++{ ++ png_uint_32 key_len; ++ png_byte new_key[81]; ++ compression_state comp; ++ ++ png_debug(1, "in png_write_zTXt"); ++ ++ if (compression == PNG_TEXT_COMPRESSION_NONE) ++ { ++ png_write_tEXt(png_ptr, key, text, 0); ++ return; ++ } ++ ++ if (compression != PNG_TEXT_COMPRESSION_zTXt) ++ png_error(png_ptr, "zTXt: invalid compression type"); ++ ++ key_len = png_check_keyword(png_ptr, key, new_key); ++ ++ if (key_len == 0) ++ png_error(png_ptr, "zTXt: invalid keyword"); ++ ++ /* Add the compression method and 1 for the keyword separator. */ ++ new_key[++key_len] = PNG_COMPRESSION_TYPE_BASE; ++ ++key_len; ++ ++ /* Compute the compressed data; do it now for the length */ ++ png_text_compress_init(&comp, (png_const_bytep)text, ++ text == NULL ? 0 : strlen(text)); ++ ++ if (png_text_compress(png_ptr, png_zTXt, &comp, key_len) != Z_OK) ++ png_error(png_ptr, png_ptr->zstream.msg); ++ ++ /* Write start of chunk */ ++ png_write_chunk_header(png_ptr, png_zTXt, key_len + comp.output_len); ++ ++ /* Write key */ ++ png_write_chunk_data(png_ptr, new_key, key_len); ++ ++ /* Write the compressed data */ ++ png_write_compressed_data_out(png_ptr, &comp); ++ ++ /* Close the chunk */ ++ png_write_chunk_end(png_ptr); ++} ++#endif ++ ++#ifdef PNG_WRITE_iTXt_SUPPORTED ++/* Write an iTXt chunk */ ++void /* PRIVATE */ ++png_write_iTXt(png_structrp png_ptr, int compression, png_const_charp key, ++ png_const_charp lang, png_const_charp lang_key, png_const_charp text) ++{ ++ png_uint_32 key_len, prefix_len; ++ size_t lang_len, lang_key_len; ++ png_byte new_key[82]; ++ compression_state comp; ++ ++ png_debug(1, "in png_write_iTXt"); ++ ++ key_len = png_check_keyword(png_ptr, key, new_key); ++ ++ if (key_len == 0) ++ png_error(png_ptr, "iTXt: invalid keyword"); ++ ++ /* Set the compression flag */ ++ switch (compression) ++ { ++ case PNG_ITXT_COMPRESSION_NONE: ++ case PNG_TEXT_COMPRESSION_NONE: ++ compression = new_key[++key_len] = 0; /* no compression */ ++ break; ++ ++ case PNG_TEXT_COMPRESSION_zTXt: ++ case PNG_ITXT_COMPRESSION_zTXt: ++ compression = new_key[++key_len] = 1; /* compressed */ ++ break; ++ ++ default: ++ png_error(png_ptr, "iTXt: invalid compression"); ++ } ++ ++ new_key[++key_len] = PNG_COMPRESSION_TYPE_BASE; ++ ++key_len; /* for the keywod separator */ ++ ++ /* We leave it to the application to meet PNG-1.0 requirements on the ++ * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of ++ * any non-Latin-1 characters except for NEWLINE. ISO PNG, however, ++ * specifies that the text is UTF-8 and this really doesn't require any ++ * checking. ++ * ++ * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG. ++ * ++ * TODO: validate the language tag correctly (see the spec.) ++ */ ++ if (lang == NULL) lang = ""; /* empty language is valid */ ++ lang_len = strlen(lang)+1; ++ if (lang_key == NULL) lang_key = ""; /* may be empty */ ++ lang_key_len = strlen(lang_key)+1; ++ if (text == NULL) text = ""; /* may be empty */ ++ ++ prefix_len = key_len; ++ if (lang_len > PNG_UINT_31_MAX-prefix_len) ++ prefix_len = PNG_UINT_31_MAX; ++ else ++ prefix_len = (png_uint_32)(prefix_len + lang_len); ++ ++ if (lang_key_len > PNG_UINT_31_MAX-prefix_len) ++ prefix_len = PNG_UINT_31_MAX; ++ else ++ prefix_len = (png_uint_32)(prefix_len + lang_key_len); ++ ++ png_text_compress_init(&comp, (png_const_bytep)text, strlen(text)); ++ ++ if (compression != 0) ++ { ++ if (png_text_compress(png_ptr, png_iTXt, &comp, prefix_len) != Z_OK) ++ png_error(png_ptr, png_ptr->zstream.msg); ++ } ++ ++ else ++ { ++ if (comp.input_len > PNG_UINT_31_MAX-prefix_len) ++ png_error(png_ptr, "iTXt: uncompressed text too long"); ++ ++ /* So the string will fit in a chunk: */ ++ comp.output_len = (png_uint_32)/*SAFE*/comp.input_len; ++ } ++ ++ png_write_chunk_header(png_ptr, png_iTXt, comp.output_len + prefix_len); ++ ++ png_write_chunk_data(png_ptr, new_key, key_len); ++ ++ png_write_chunk_data(png_ptr, (png_const_bytep)lang, lang_len); ++ ++ png_write_chunk_data(png_ptr, (png_const_bytep)lang_key, lang_key_len); ++ ++ if (compression != 0) ++ png_write_compressed_data_out(png_ptr, &comp); ++ ++ else ++ png_write_chunk_data(png_ptr, (png_const_bytep)text, comp.output_len); ++ ++ png_write_chunk_end(png_ptr); ++} ++#endif ++ ++#ifdef PNG_WRITE_oFFs_SUPPORTED ++/* Write the oFFs chunk */ ++void /* PRIVATE */ ++png_write_oFFs(png_structrp png_ptr, png_int_32 x_offset, png_int_32 y_offset, ++ int unit_type) ++{ ++ png_byte buf[9]; ++ ++ png_debug(1, "in png_write_oFFs"); ++ ++ if (unit_type >= PNG_OFFSET_LAST) ++ png_warning(png_ptr, "Unrecognized unit type for oFFs chunk"); ++ ++ png_save_int_32(buf, x_offset); ++ png_save_int_32(buf + 4, y_offset); ++ buf[8] = (png_byte)unit_type; ++ ++ png_write_complete_chunk(png_ptr, png_oFFs, buf, 9); ++} ++#endif ++#ifdef PNG_WRITE_pCAL_SUPPORTED ++/* Write the pCAL chunk (described in the PNG extensions document) */ ++void /* PRIVATE */ ++png_write_pCAL(png_structrp png_ptr, png_charp purpose, png_int_32 X0, ++ png_int_32 X1, int type, int nparams, png_const_charp units, ++ png_charpp params) ++{ ++ png_uint_32 purpose_len; ++ size_t units_len, total_len; ++ png_size_tp params_len; ++ png_byte buf[10]; ++ png_byte new_purpose[80]; ++ int i; ++ ++ png_debug1(1, "in png_write_pCAL (%d parameters)", nparams); ++ ++ if (type >= PNG_EQUATION_LAST) ++ png_error(png_ptr, "Unrecognized equation type for pCAL chunk"); ++ ++ purpose_len = png_check_keyword(png_ptr, purpose, new_purpose); ++ ++ if (purpose_len == 0) ++ png_error(png_ptr, "pCAL: invalid keyword"); ++ ++ ++purpose_len; /* terminator */ ++ ++ png_debug1(3, "pCAL purpose length = %d", (int)purpose_len); ++ units_len = strlen(units) + (nparams == 0 ? 0 : 1); ++ png_debug1(3, "pCAL units length = %d", (int)units_len); ++ total_len = purpose_len + units_len + 10; ++ ++ params_len = (png_size_tp)png_malloc(png_ptr, ++ (png_alloc_size_t)((png_alloc_size_t)nparams * (sizeof (size_t)))); ++ ++ /* Find the length of each parameter, making sure we don't count the ++ * null terminator for the last parameter. ++ */ ++ for (i = 0; i < nparams; i++) ++ { ++ params_len[i] = strlen(params[i]) + (i == nparams - 1 ? 0 : 1); ++ png_debug2(3, "pCAL parameter %d length = %lu", i, ++ (unsigned long)params_len[i]); ++ total_len += params_len[i]; ++ } ++ ++ png_debug1(3, "pCAL total length = %d", (int)total_len); ++ png_write_chunk_header(png_ptr, png_pCAL, (png_uint_32)total_len); ++ png_write_chunk_data(png_ptr, new_purpose, purpose_len); ++ png_save_int_32(buf, X0); ++ png_save_int_32(buf + 4, X1); ++ buf[8] = (png_byte)type; ++ buf[9] = (png_byte)nparams; ++ png_write_chunk_data(png_ptr, buf, 10); ++ png_write_chunk_data(png_ptr, (png_const_bytep)units, (size_t)units_len); ++ ++ for (i = 0; i < nparams; i++) ++ { ++ png_write_chunk_data(png_ptr, (png_const_bytep)params[i], params_len[i]); ++ } ++ ++ png_free(png_ptr, params_len); ++ png_write_chunk_end(png_ptr); ++} ++#endif ++ ++#ifdef PNG_WRITE_sCAL_SUPPORTED ++/* Write the sCAL chunk */ ++void /* PRIVATE */ ++png_write_sCAL_s(png_structrp png_ptr, int unit, png_const_charp width, ++ png_const_charp height) ++{ ++ png_byte buf[64]; ++ size_t wlen, hlen, total_len; ++ ++ png_debug(1, "in png_write_sCAL_s"); ++ ++ wlen = strlen(width); ++ hlen = strlen(height); ++ total_len = wlen + hlen + 2; ++ ++ if (total_len > 64) ++ { ++ png_warning(png_ptr, "Can't write sCAL (buffer too small)"); ++ return; ++ } ++ ++ buf[0] = (png_byte)unit; ++ memcpy(buf + 1, width, wlen + 1); /* Append the '\0' here */ ++ memcpy(buf + wlen + 2, height, hlen); /* Do NOT append the '\0' here */ ++ ++ png_debug1(3, "sCAL total length = %u", (unsigned int)total_len); ++ png_write_complete_chunk(png_ptr, png_sCAL, buf, total_len); ++} ++#endif ++ ++#ifdef PNG_WRITE_pHYs_SUPPORTED ++/* Write the pHYs chunk */ ++void /* PRIVATE */ ++png_write_pHYs(png_structrp png_ptr, png_uint_32 x_pixels_per_unit, ++ png_uint_32 y_pixels_per_unit, ++ int unit_type) ++{ ++ png_byte buf[9]; ++ ++ png_debug(1, "in png_write_pHYs"); ++ ++ if (unit_type >= PNG_RESOLUTION_LAST) ++ png_warning(png_ptr, "Unrecognized unit type for pHYs chunk"); ++ ++ png_save_uint_32(buf, x_pixels_per_unit); ++ png_save_uint_32(buf + 4, y_pixels_per_unit); ++ buf[8] = (png_byte)unit_type; ++ ++ png_write_complete_chunk(png_ptr, png_pHYs, buf, 9); ++} ++#endif ++ ++#ifdef PNG_WRITE_tIME_SUPPORTED ++/* Write the tIME chunk. Use either png_convert_from_struct_tm() ++ * or png_convert_from_time_t(), or fill in the structure yourself. ++ */ ++void /* PRIVATE */ ++png_write_tIME(png_structrp png_ptr, png_const_timep mod_time) ++{ ++ png_byte buf[7]; ++ ++ png_debug(1, "in png_write_tIME"); ++ ++ if (mod_time->month > 12 || mod_time->month < 1 || ++ mod_time->day > 31 || mod_time->day < 1 || ++ mod_time->hour > 23 || mod_time->second > 60) ++ { ++ png_warning(png_ptr, "Invalid time specified for tIME chunk"); ++ return; ++ } ++ ++ png_save_uint_16(buf, mod_time->year); ++ buf[2] = mod_time->month; ++ buf[3] = mod_time->day; ++ buf[4] = mod_time->hour; ++ buf[5] = mod_time->minute; ++ buf[6] = mod_time->second; ++ ++ png_write_complete_chunk(png_ptr, png_tIME, buf, 7); ++} ++#endif ++ ++/* Initializes the row writing capability of libpng */ ++void /* PRIVATE */ ++png_write_start_row(png_structrp png_ptr) ++{ ++#ifdef PNG_WRITE_INTERLACING_SUPPORTED ++ /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ ++ ++ /* Start of interlace block */ ++ static const png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; ++ ++ /* Offset to next interlace block */ ++ static const png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; ++ ++ /* Start of interlace block in the y direction */ ++ static const png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; ++ ++ /* Offset to next interlace block in the y direction */ ++ static const png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; ++#endif ++ ++ png_alloc_size_t buf_size; ++ int usr_pixel_depth; ++ ++#ifdef PNG_WRITE_FILTER_SUPPORTED ++ png_byte filters; ++#endif ++ ++ png_debug(1, "in png_write_start_row"); ++ ++ usr_pixel_depth = png_ptr->usr_channels * png_ptr->usr_bit_depth; ++ buf_size = PNG_ROWBYTES(usr_pixel_depth, png_ptr->width) + 1; ++ ++ /* 1.5.6: added to allow checking in the row write code. */ ++ png_ptr->transformed_pixel_depth = png_ptr->pixel_depth; ++ png_ptr->maximum_pixel_depth = (png_byte)usr_pixel_depth; ++ ++ /* Set up row buffer */ ++ png_ptr->row_buf = png_voidcast(png_bytep, png_malloc(png_ptr, buf_size)); ++ ++ png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE; ++ ++#ifdef PNG_WRITE_FILTER_SUPPORTED ++ filters = png_ptr->do_filter; ++ ++ if (png_ptr->height == 1) ++ filters &= 0xff & ~(PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH); ++ ++ if (png_ptr->width == 1) ++ filters &= 0xff & ~(PNG_FILTER_SUB|PNG_FILTER_AVG|PNG_FILTER_PAETH); ++ ++ if (filters == 0) ++ filters = PNG_FILTER_NONE; ++ ++ png_ptr->do_filter = filters; ++ ++ if (((filters & (PNG_FILTER_SUB | PNG_FILTER_UP | PNG_FILTER_AVG | ++ PNG_FILTER_PAETH)) != 0) && png_ptr->try_row == NULL) ++ { ++ int num_filters = 0; ++ ++ png_ptr->try_row = png_voidcast(png_bytep, png_malloc(png_ptr, buf_size)); ++ ++ if (filters & PNG_FILTER_SUB) ++ num_filters++; ++ ++ if (filters & PNG_FILTER_UP) ++ num_filters++; ++ ++ if (filters & PNG_FILTER_AVG) ++ num_filters++; ++ ++ if (filters & PNG_FILTER_PAETH) ++ num_filters++; ++ ++ if (num_filters > 1) ++ png_ptr->tst_row = png_voidcast(png_bytep, png_malloc(png_ptr, ++ buf_size)); ++ } ++ ++ /* We only need to keep the previous row if we are using one of the following ++ * filters. ++ */ ++ if ((filters & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH)) != 0) ++ png_ptr->prev_row = png_voidcast(png_bytep, ++ png_calloc(png_ptr, buf_size)); ++#endif /* WRITE_FILTER */ ++ ++#ifdef PNG_WRITE_INTERLACING_SUPPORTED ++ /* If interlaced, we need to set up width and height of pass */ ++ if (png_ptr->interlaced != 0) ++ { ++ if ((png_ptr->transformations & PNG_INTERLACE) == 0) ++ { ++ png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 - ++ png_pass_ystart[0]) / png_pass_yinc[0]; ++ ++ png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 - ++ png_pass_start[0]) / png_pass_inc[0]; ++ } ++ ++ else ++ { ++ png_ptr->num_rows = png_ptr->height; ++ png_ptr->usr_width = png_ptr->width; ++ } ++ } ++ ++ else ++#endif ++ { ++ png_ptr->num_rows = png_ptr->height; ++ png_ptr->usr_width = png_ptr->width; ++ } ++} ++ ++/* Internal use only. Called when finished processing a row of data. */ ++void /* PRIVATE */ ++png_write_finish_row(png_structrp png_ptr) ++{ ++#ifdef PNG_WRITE_INTERLACING_SUPPORTED ++ /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ ++ ++ /* Start of interlace block */ ++ static const png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; ++ ++ /* Offset to next interlace block */ ++ static const png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; ++ ++ /* Start of interlace block in the y direction */ ++ static const png_byte png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1}; ++ ++ /* Offset to next interlace block in the y direction */ ++ static const png_byte png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2}; ++#endif ++ ++ png_debug(1, "in png_write_finish_row"); ++ ++ /* Next row */ ++ png_ptr->row_number++; ++ ++ /* See if we are done */ ++ if (png_ptr->row_number < png_ptr->num_rows) ++ return; ++ ++#ifdef PNG_WRITE_INTERLACING_SUPPORTED ++ /* If interlaced, go to next pass */ ++ if (png_ptr->interlaced != 0) ++ { ++ png_ptr->row_number = 0; ++ if ((png_ptr->transformations & PNG_INTERLACE) != 0) ++ { ++ png_ptr->pass++; ++ } ++ ++ else ++ { ++ /* Loop until we find a non-zero width or height pass */ ++ do ++ { ++ png_ptr->pass++; ++ ++ if (png_ptr->pass >= 7) ++ break; ++ ++ png_ptr->usr_width = (png_ptr->width + ++ png_pass_inc[png_ptr->pass] - 1 - ++ png_pass_start[png_ptr->pass]) / ++ png_pass_inc[png_ptr->pass]; ++ ++ png_ptr->num_rows = (png_ptr->height + ++ png_pass_yinc[png_ptr->pass] - 1 - ++ png_pass_ystart[png_ptr->pass]) / ++ png_pass_yinc[png_ptr->pass]; ++ ++ if ((png_ptr->transformations & PNG_INTERLACE) != 0) ++ break; ++ ++ } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0); ++ ++ } ++ ++ /* Reset the row above the image for the next pass */ ++ if (png_ptr->pass < 7) ++ { ++ if (png_ptr->prev_row != NULL) ++ memset(png_ptr->prev_row, 0, ++ PNG_ROWBYTES(png_ptr->usr_channels * ++ png_ptr->usr_bit_depth, png_ptr->width) + 1); ++ ++ return; ++ } ++ } ++#endif ++ ++ /* If we get here, we've just written the last row, so we need ++ to flush the compressor */ ++ png_compress_IDAT(png_ptr, NULL, 0, Z_FINISH); ++} ++ ++#ifdef PNG_WRITE_INTERLACING_SUPPORTED ++/* Pick out the correct pixels for the interlace pass. ++ * The basic idea here is to go through the row with a source ++ * pointer and a destination pointer (sp and dp), and copy the ++ * correct pixels for the pass. As the row gets compacted, ++ * sp will always be >= dp, so we should never overwrite anything. ++ * See the default: case for the easiest code to understand. ++ */ ++void /* PRIVATE */ ++png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass) ++{ ++ /* Arrays to facilitate easy interlacing - use pass (0 - 6) as index */ ++ ++ /* Start of interlace block */ ++ static const png_byte png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0}; ++ ++ /* Offset to next interlace block */ ++ static const png_byte png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1}; ++ ++ png_debug(1, "in png_do_write_interlace"); ++ ++ /* We don't have to do anything on the last pass (6) */ ++ if (pass < 6) ++ { ++ /* Each pixel depth is handled separately */ ++ switch (row_info->pixel_depth) ++ { ++ case 1: ++ { ++ png_bytep sp; ++ png_bytep dp; ++ unsigned int shift; ++ int d; ++ int value; ++ png_uint_32 i; ++ png_uint_32 row_width = row_info->width; ++ ++ dp = row; ++ d = 0; ++ shift = 7; ++ ++ for (i = png_pass_start[pass]; i < row_width; ++ i += png_pass_inc[pass]) ++ { ++ sp = row + (size_t)(i >> 3); ++ value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01; ++ d |= (value << shift); ++ ++ if (shift == 0) ++ { ++ shift = 7; ++ *dp++ = (png_byte)d; ++ d = 0; ++ } ++ ++ else ++ shift--; ++ ++ } ++ if (shift != 7) ++ *dp = (png_byte)d; ++ ++ break; ++ } ++ ++ case 2: ++ { ++ png_bytep sp; ++ png_bytep dp; ++ unsigned int shift; ++ int d; ++ int value; ++ png_uint_32 i; ++ png_uint_32 row_width = row_info->width; ++ ++ dp = row; ++ shift = 6; ++ d = 0; ++ ++ for (i = png_pass_start[pass]; i < row_width; ++ i += png_pass_inc[pass]) ++ { ++ sp = row + (size_t)(i >> 2); ++ value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03; ++ d |= (value << shift); ++ ++ if (shift == 0) ++ { ++ shift = 6; ++ *dp++ = (png_byte)d; ++ d = 0; ++ } ++ ++ else ++ shift -= 2; ++ } ++ if (shift != 6) ++ *dp = (png_byte)d; ++ ++ break; ++ } ++ ++ case 4: ++ { ++ png_bytep sp; ++ png_bytep dp; ++ unsigned int shift; ++ int d; ++ int value; ++ png_uint_32 i; ++ png_uint_32 row_width = row_info->width; ++ ++ dp = row; ++ shift = 4; ++ d = 0; ++ for (i = png_pass_start[pass]; i < row_width; ++ i += png_pass_inc[pass]) ++ { ++ sp = row + (size_t)(i >> 1); ++ value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f; ++ d |= (value << shift); ++ ++ if (shift == 0) ++ { ++ shift = 4; ++ *dp++ = (png_byte)d; ++ d = 0; ++ } ++ ++ else ++ shift -= 4; ++ } ++ if (shift != 4) ++ *dp = (png_byte)d; ++ ++ break; ++ } ++ ++ default: ++ { ++ png_bytep sp; ++ png_bytep dp; ++ png_uint_32 i; ++ png_uint_32 row_width = row_info->width; ++ size_t pixel_bytes; ++ ++ /* Start at the beginning */ ++ dp = row; ++ ++ /* Find out how many bytes each pixel takes up */ ++ pixel_bytes = (row_info->pixel_depth >> 3); ++ ++ /* Loop through the row, only looking at the pixels that matter */ ++ for (i = png_pass_start[pass]; i < row_width; ++ i += png_pass_inc[pass]) ++ { ++ /* Find out where the original pixel is */ ++ sp = row + (size_t)i * pixel_bytes; ++ ++ /* Move the pixel */ ++ if (dp != sp) ++ memcpy(dp, sp, pixel_bytes); ++ ++ /* Next pixel */ ++ dp += pixel_bytes; ++ } ++ break; ++ } ++ } ++ /* Set new row width */ ++ row_info->width = (row_info->width + ++ png_pass_inc[pass] - 1 - ++ png_pass_start[pass]) / ++ png_pass_inc[pass]; ++ ++ row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, ++ row_info->width); ++ } ++} ++#endif ++ ++ ++/* This filters the row, chooses which filter to use, if it has not already ++ * been specified by the application, and then writes the row out with the ++ * chosen filter. ++ */ ++static void /* PRIVATE */ ++png_write_filtered_row(png_structrp png_ptr, png_bytep filtered_row, ++ size_t row_bytes); ++ ++#ifdef PNG_WRITE_FILTER_SUPPORTED ++static size_t /* PRIVATE */ ++png_setup_sub_row(png_structrp png_ptr, png_uint_32 bpp, ++ size_t row_bytes, size_t lmins) ++{ ++ png_bytep rp, dp, lp; ++ size_t i; ++ size_t sum = 0; ++ unsigned int v; ++ ++ png_ptr->try_row[0] = PNG_FILTER_VALUE_SUB; ++ ++ for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1; i < bpp; ++ i++, rp++, dp++) ++ { ++ v = *dp = *rp; ++#ifdef PNG_USE_ABS ++ sum += 128 - abs((int)v - 128); ++#else ++ sum += (v < 128) ? v : 256 - v; ++#endif ++ } ++ ++ for (lp = png_ptr->row_buf + 1; i < row_bytes; ++ i++, rp++, lp++, dp++) ++ { ++ v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff); ++#ifdef PNG_USE_ABS ++ sum += 128 - abs((int)v - 128); ++#else ++ sum += (v < 128) ? v : 256 - v; ++#endif ++ ++ if (sum > lmins) /* We are already worse, don't continue. */ ++ break; ++ } ++ ++ return (sum); ++} ++ ++static void /* PRIVATE */ ++png_setup_sub_row_only(png_structrp png_ptr, png_uint_32 bpp, ++ size_t row_bytes) ++{ ++ png_bytep rp, dp, lp; ++ size_t i; ++ ++ png_ptr->try_row[0] = PNG_FILTER_VALUE_SUB; ++ ++ for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1; i < bpp; ++ i++, rp++, dp++) ++ { ++ *dp = *rp; ++ } ++ ++ for (lp = png_ptr->row_buf + 1; i < row_bytes; ++ i++, rp++, lp++, dp++) ++ { ++ *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff); ++ } ++} ++ ++static size_t /* PRIVATE */ ++png_setup_up_row(png_structrp png_ptr, size_t row_bytes, size_t lmins) ++{ ++ png_bytep rp, dp, pp; ++ size_t i; ++ size_t sum = 0; ++ unsigned int v; ++ ++ png_ptr->try_row[0] = PNG_FILTER_VALUE_UP; ++ ++ for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1, ++ pp = png_ptr->prev_row + 1; i < row_bytes; ++ i++, rp++, pp++, dp++) ++ { ++ v = *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff); ++#ifdef PNG_USE_ABS ++ sum += 128 - abs((int)v - 128); ++#else ++ sum += (v < 128) ? v : 256 - v; ++#endif ++ ++ if (sum > lmins) /* We are already worse, don't continue. */ ++ break; ++ } ++ ++ return (sum); ++} ++static void /* PRIVATE */ ++png_setup_up_row_only(png_structrp png_ptr, size_t row_bytes) ++{ ++ png_bytep rp, dp, pp; ++ size_t i; ++ ++ png_ptr->try_row[0] = PNG_FILTER_VALUE_UP; ++ ++ for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1, ++ pp = png_ptr->prev_row + 1; i < row_bytes; ++ i++, rp++, pp++, dp++) ++ { ++ *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff); ++ } ++} ++ ++static size_t /* PRIVATE */ ++png_setup_avg_row(png_structrp png_ptr, png_uint_32 bpp, ++ size_t row_bytes, size_t lmins) ++{ ++ png_bytep rp, dp, pp, lp; ++ png_uint_32 i; ++ size_t sum = 0; ++ unsigned int v; ++ ++ png_ptr->try_row[0] = PNG_FILTER_VALUE_AVG; ++ ++ for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1, ++ pp = png_ptr->prev_row + 1; i < bpp; i++) ++ { ++ v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff); ++ ++#ifdef PNG_USE_ABS ++ sum += 128 - abs((int)v - 128); ++#else ++ sum += (v < 128) ? v : 256 - v; ++#endif ++ } ++ ++ for (lp = png_ptr->row_buf + 1; i < row_bytes; i++) ++ { ++ v = *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) ++ & 0xff); ++ ++#ifdef PNG_USE_ABS ++ sum += 128 - abs((int)v - 128); ++#else ++ sum += (v < 128) ? v : 256 - v; ++#endif ++ ++ if (sum > lmins) /* We are already worse, don't continue. */ ++ break; ++ } ++ ++ return (sum); ++} ++static void /* PRIVATE */ ++png_setup_avg_row_only(png_structrp png_ptr, png_uint_32 bpp, ++ size_t row_bytes) ++{ ++ png_bytep rp, dp, pp, lp; ++ png_uint_32 i; ++ ++ png_ptr->try_row[0] = PNG_FILTER_VALUE_AVG; ++ ++ for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1, ++ pp = png_ptr->prev_row + 1; i < bpp; i++) ++ { ++ *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff); ++ } ++ ++ for (lp = png_ptr->row_buf + 1; i < row_bytes; i++) ++ { ++ *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) ++ & 0xff); ++ } ++} ++ ++static size_t /* PRIVATE */ ++png_setup_paeth_row(png_structrp png_ptr, png_uint_32 bpp, ++ size_t row_bytes, size_t lmins) ++{ ++ png_bytep rp, dp, pp, cp, lp; ++ size_t i; ++ size_t sum = 0; ++ unsigned int v; ++ ++ png_ptr->try_row[0] = PNG_FILTER_VALUE_PAETH; ++ ++ for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1, ++ pp = png_ptr->prev_row + 1; i < bpp; i++) ++ { ++ v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff); ++ ++#ifdef PNG_USE_ABS ++ sum += 128 - abs((int)v - 128); ++#else ++ sum += (v < 128) ? v : 256 - v; ++#endif ++ } ++ ++ for (lp = png_ptr->row_buf + 1, cp = png_ptr->prev_row + 1; i < row_bytes; ++ i++) ++ { ++ int a, b, c, pa, pb, pc, p; ++ ++ b = *pp++; ++ c = *cp++; ++ a = *lp++; ++ ++ p = b - c; ++ pc = a - c; ++ ++#ifdef PNG_USE_ABS ++ pa = abs(p); ++ pb = abs(pc); ++ pc = abs(p + pc); ++#else ++ pa = p < 0 ? -p : p; ++ pb = pc < 0 ? -pc : pc; ++ pc = (p + pc) < 0 ? -(p + pc) : p + pc; ++#endif ++ ++ p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c; ++ ++ v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff); ++ ++#ifdef PNG_USE_ABS ++ sum += 128 - abs((int)v - 128); ++#else ++ sum += (v < 128) ? v : 256 - v; ++#endif ++ ++ if (sum > lmins) /* We are already worse, don't continue. */ ++ break; ++ } ++ ++ return (sum); ++} ++static void /* PRIVATE */ ++png_setup_paeth_row_only(png_structrp png_ptr, png_uint_32 bpp, ++ size_t row_bytes) ++{ ++ png_bytep rp, dp, pp, cp, lp; ++ size_t i; ++ ++ png_ptr->try_row[0] = PNG_FILTER_VALUE_PAETH; ++ ++ for (i = 0, rp = png_ptr->row_buf + 1, dp = png_ptr->try_row + 1, ++ pp = png_ptr->prev_row + 1; i < bpp; i++) ++ { ++ *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff); ++ } ++ ++ for (lp = png_ptr->row_buf + 1, cp = png_ptr->prev_row + 1; i < row_bytes; ++ i++) ++ { ++ int a, b, c, pa, pb, pc, p; ++ ++ b = *pp++; ++ c = *cp++; ++ a = *lp++; ++ ++ p = b - c; ++ pc = a - c; ++ ++#ifdef PNG_USE_ABS ++ pa = abs(p); ++ pb = abs(pc); ++ pc = abs(p + pc); ++#else ++ pa = p < 0 ? -p : p; ++ pb = pc < 0 ? -pc : pc; ++ pc = (p + pc) < 0 ? -(p + pc) : p + pc; ++#endif ++ ++ p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c; ++ ++ *dp++ = (png_byte)(((int)*rp++ - p) & 0xff); ++ } ++} ++#endif /* WRITE_FILTER */ ++ ++void /* PRIVATE */ ++png_write_find_filter(png_structrp png_ptr, png_row_infop row_info) ++{ ++#ifndef PNG_WRITE_FILTER_SUPPORTED ++ png_write_filtered_row(png_ptr, png_ptr->row_buf, row_info->rowbytes+1); ++#else ++ unsigned int filter_to_do = png_ptr->do_filter; ++ png_bytep row_buf; ++ png_bytep best_row; ++ png_uint_32 bpp; ++ size_t mins; ++ size_t row_bytes = row_info->rowbytes; ++ ++ png_debug(1, "in png_write_find_filter"); ++ ++ /* Find out how many bytes offset each pixel is */ ++ bpp = (row_info->pixel_depth + 7) >> 3; ++ ++ row_buf = png_ptr->row_buf; ++ mins = PNG_SIZE_MAX - 256/* so we can detect potential overflow of the ++ running sum */; ++ ++ /* The prediction method we use is to find which method provides the ++ * smallest value when summing the absolute values of the distances ++ * from zero, using anything >= 128 as negative numbers. This is known ++ * as the "minimum sum of absolute differences" heuristic. Other ++ * heuristics are the "weighted minimum sum of absolute differences" ++ * (experimental and can in theory improve compression), and the "zlib ++ * predictive" method (not implemented yet), which does test compressions ++ * of lines using different filter methods, and then chooses the ++ * (series of) filter(s) that give minimum compressed data size (VERY ++ * computationally expensive). ++ * ++ * GRR 980525: consider also ++ * ++ * (1) minimum sum of absolute differences from running average (i.e., ++ * keep running sum of non-absolute differences & count of bytes) ++ * [track dispersion, too? restart average if dispersion too large?] ++ * ++ * (1b) minimum sum of absolute differences from sliding average, probably ++ * with window size <= deflate window (usually 32K) ++ * ++ * (2) minimum sum of squared differences from zero or running average ++ * (i.e., ~ root-mean-square approach) ++ */ ++ ++ ++ /* We don't need to test the 'no filter' case if this is the only filter ++ * that has been chosen, as it doesn't actually do anything to the data. ++ */ ++ best_row = png_ptr->row_buf; ++ ++ if (PNG_SIZE_MAX/128 <= row_bytes) ++ { ++ /* Overflow can occur in the calculation, just select the lowest set ++ * filter. ++ */ ++ filter_to_do &= 0U-filter_to_do; ++ } ++ else if ((filter_to_do & PNG_FILTER_NONE) != 0 && ++ filter_to_do != PNG_FILTER_NONE) ++ { ++ /* Overflow not possible and multiple filters in the list, including the ++ * 'none' filter. ++ */ ++ png_bytep rp; ++ size_t sum = 0; ++ size_t i; ++ unsigned int v; ++ ++ { ++ for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++) ++ { ++ v = *rp; ++#ifdef PNG_USE_ABS ++ sum += 128 - abs((int)v - 128); ++#else ++ sum += (v < 128) ? v : 256 - v; ++#endif ++ } ++ } ++ ++ mins = sum; ++ } ++ ++ /* Sub filter */ ++ if (filter_to_do == PNG_FILTER_SUB) ++ /* It's the only filter so no testing is needed */ ++ { ++ png_setup_sub_row_only(png_ptr, bpp, row_bytes); ++ best_row = png_ptr->try_row; ++ } ++ ++ else if ((filter_to_do & PNG_FILTER_SUB) != 0) ++ { ++ size_t sum; ++ size_t lmins = mins; ++ ++ sum = png_setup_sub_row(png_ptr, bpp, row_bytes, lmins); ++ ++ if (sum < mins) ++ { ++ mins = sum; ++ best_row = png_ptr->try_row; ++ if (png_ptr->tst_row != NULL) ++ { ++ png_ptr->try_row = png_ptr->tst_row; ++ png_ptr->tst_row = best_row; ++ } ++ } ++ } ++ ++ /* Up filter */ ++ if (filter_to_do == PNG_FILTER_UP) ++ { ++ png_setup_up_row_only(png_ptr, row_bytes); ++ best_row = png_ptr->try_row; ++ } ++ ++ else if ((filter_to_do & PNG_FILTER_UP) != 0) ++ { ++ size_t sum; ++ size_t lmins = mins; ++ ++ sum = png_setup_up_row(png_ptr, row_bytes, lmins); ++ ++ if (sum < mins) ++ { ++ mins = sum; ++ best_row = png_ptr->try_row; ++ if (png_ptr->tst_row != NULL) ++ { ++ png_ptr->try_row = png_ptr->tst_row; ++ png_ptr->tst_row = best_row; ++ } ++ } ++ } ++ ++ /* Avg filter */ ++ if (filter_to_do == PNG_FILTER_AVG) ++ { ++ png_setup_avg_row_only(png_ptr, bpp, row_bytes); ++ best_row = png_ptr->try_row; ++ } ++ ++ else if ((filter_to_do & PNG_FILTER_AVG) != 0) ++ { ++ size_t sum; ++ size_t lmins = mins; ++ ++ sum= png_setup_avg_row(png_ptr, bpp, row_bytes, lmins); ++ ++ if (sum < mins) ++ { ++ mins = sum; ++ best_row = png_ptr->try_row; ++ if (png_ptr->tst_row != NULL) ++ { ++ png_ptr->try_row = png_ptr->tst_row; ++ png_ptr->tst_row = best_row; ++ } ++ } ++ } ++ ++ /* Paeth filter */ ++ if (filter_to_do == PNG_FILTER_PAETH) ++ { ++ png_setup_paeth_row_only(png_ptr, bpp, row_bytes); ++ best_row = png_ptr->try_row; ++ } ++ ++ else if ((filter_to_do & PNG_FILTER_PAETH) != 0) ++ { ++ size_t sum; ++ size_t lmins = mins; ++ ++ sum = png_setup_paeth_row(png_ptr, bpp, row_bytes, lmins); ++ ++ if (sum < mins) ++ { ++ best_row = png_ptr->try_row; ++ if (png_ptr->tst_row != NULL) ++ { ++ png_ptr->try_row = png_ptr->tst_row; ++ png_ptr->tst_row = best_row; ++ } ++ } ++ } ++ ++ /* Do the actual writing of the filtered row data from the chosen filter. */ ++ png_write_filtered_row(png_ptr, best_row, row_info->rowbytes+1); ++ ++#endif /* WRITE_FILTER */ ++} ++ ++ ++/* Do the actual writing of a previously filtered row. */ ++static void ++png_write_filtered_row(png_structrp png_ptr, png_bytep filtered_row, ++ size_t full_row_length/*includes filter byte*/) ++{ ++ png_debug(1, "in png_write_filtered_row"); ++ ++ png_debug1(2, "filter = %d", filtered_row[0]); ++ ++ png_compress_IDAT(png_ptr, filtered_row, full_row_length, Z_NO_FLUSH); ++ ++#ifdef PNG_WRITE_FILTER_SUPPORTED ++ /* Swap the current and previous rows */ ++ if (png_ptr->prev_row != NULL) ++ { ++ png_bytep tptr; ++ ++ tptr = png_ptr->prev_row; ++ png_ptr->prev_row = png_ptr->row_buf; ++ png_ptr->row_buf = tptr; ++ } ++#endif /* WRITE_FILTER */ ++ ++ /* Finish row - updates counters and flushes zlib if last row */ ++ png_write_finish_row(png_ptr); ++ ++#ifdef PNG_WRITE_FLUSH_SUPPORTED ++ png_ptr->flush_rows++; ++ ++ if (png_ptr->flush_dist > 0 && ++ png_ptr->flush_rows >= png_ptr->flush_dist) ++ { ++ png_write_flush(png_ptr); ++ } ++#endif /* WRITE_FLUSH */ ++} ++#endif /* WRITE */ +diff --git a/lib/libpng/sub.mk b/lib/libpng/sub.mk +new file mode 100644 +index 000000000..165ec74e7 +--- /dev/null ++++ b/lib/libpng/sub.mk +@@ -0,0 +1,22 @@ ++global-incdirs-y += include ++ ++# Disable eventual ARM NEON optimization ++cppflags-y += -DPNG_ARM_NEON_OPT=0 ++ ++cflags-y += -Wno-extra ++ ++srcs-y += png.c ++srcs-y += pngerror.c ++srcs-y += pngget.c ++srcs-y += pngmem.c ++srcs-y += pngpread.c ++srcs-y += pngread.c ++srcs-y += pngrio.c ++srcs-y += pngrtran.c ++srcs-y += pngrutil.c ++srcs-y += pngset.c ++srcs-y += pngtrans.c ++srcs-y += pngwio.c ++srcs-y += pngwrite.c ++srcs-y += pngwtran.c ++srcs-y += pngwutil.c +diff --git a/lib/libutee/include/pta_bsec.h b/lib/libutee/include/pta_bsec.h +new file mode 100644 +index 000000000..6935c712b +--- /dev/null ++++ b/lib/libutee/include/pta_bsec.h +@@ -0,0 +1,70 @@ ++/* SPDX-License-Identifier: BSD-2-Clause */ ++/* ++ * Copyright (C) 2021, STMicroelectronics - All Rights Reserved ++ */ ++ ++#ifndef __PTA_BSEC_H ++#define __PTA_BSEC_H ++ ++#define PTA_BSEC_UUID { 0x94cf71ad, 0x80e6, 0x40b5, \ ++ { 0xa7, 0xc6, 0x3d, 0xc5, 0x01, 0xeb, 0x28, 0x03 } } ++ ++/** ++ * Read OTP memory ++ * ++ * [in] value[0].a OTP start offset in byte ++ * [in] value[0].b Access type (0 : shadow, ++ * 1 : fuse, 2 : lock) ++ * [out] memref[1].buffer Output buffer to store read values ++ * [out] memref[1].size Size of OTP to be read ++ * ++ * Return codes: ++ * TEE_SUCCESS - Invoke command success ++ * TEE_ERROR_BAD_PARAMETERS - Incorrect input param ++ */ ++#define PTA_BSEC_READ_MEM 0x0 /* Read OTP */ ++ ++/** ++ * Write OTP memory ++ * ++ * [in] value[0].a OTP start offset in byte ++ * [in] value[0].b Access type (0 : shadow, ++ * 1 : fuse, 2 : lock) ++ * [in] memref[1].buffer Input buffer to read values ++ * [in] memref[1].size Size of OTP to be written ++ * ++ * Return codes: ++ * TEE_SUCCESS - Invoke command success ++ * TEE_ERROR_BAD_PARAMETERS - Incorrect input param ++ */ ++#define PTA_BSEC_WRITE_MEM 0x1 /* Write OTP */ ++ ++/** ++ * Get BSEC state ++ * Return the chip security level by reading the BSEC state ++ * ++ * [out] value[0].a BSEC state ++ * 0 : Secure Open state ++ * 1 : Secure Close state ++ * 3 : Invalid State ++ * Return codes: ++ * TEE_SUCCESS - Invoke command success ++ * TEE_ERROR_BAD_PARAMETERS - Incorrect input param ++ */ ++#define PTA_BSEC_GET_STATE 0x3 /* Get BSEC state */ ++ ++/* Value of PTA_BSEC access type = value[in] b */ ++#define SHADOW_ACCESS 0 ++#define FUSE_ACCESS 1 ++#define LOCK_ACCESS 2 ++ ++/* Bitfield definition for LOCK status */ ++/* warning: bit 31 is reserved in PTA NVMEM for OTP_UPDATE_REQ */ ++#define LOCK_PERM BIT(30) ++#define LOCK_SHADOW_R BIT(29) ++#define LOCK_SHADOW_W BIT(28) ++#define LOCK_SHADOW_P BIT(27) ++#define LOCK_ERROR BIT(26) ++ ++#define LOCK_MASK_SHADOW GENMASK_32(29, 27) ++#endif /* __PTA_BSEC_H */ +diff --git a/lib/libutee/include/pta_scmi_client.h b/lib/libutee/include/pta_scmi_client.h +index 2e98bc779..81f3f1fc7 100644 +--- a/lib/libutee/include/pta_scmi_client.h ++++ b/lib/libutee/include/pta_scmi_client.h +@@ -58,6 +58,17 @@ + */ + #define PTA_SCMI_CMD_GET_CHANNEL_HANDLE 3 + ++/* ++ * PTA_SCMI_CMD_OCALL_THREAD - Allocate a threaded path using OCALL ++ * ++ * [in] value[0].a: channel handle ++ * ++ * Use Ocall support to create a provisioned OP-TEE thread context for ++ * the channel. Successful creation of the thread makes this command to ++ * return with Ocall command PTA_SCMI_OCALL_CMD_THREAD_READY. ++ */ ++#define PTA_SCMI_CMD_OCALL_THREAD 4 ++ + /* + * Capabilities + */ +@@ -65,4 +76,62 @@ + /* Channel supports shared memory using the SMT header protocol */ + #define PTA_SCMI_CAPS_SMT_HEADER BIT32(0) + ++/* ++ * Channel can use command PTA_SCMI_CMD_OCALL_THREAD to provision a ++ * TEE thread for SCMI message passing. ++ */ ++#define PTA_SCMI_CAPS_OCALL_THREAD BIT32(1) ++ ++#define PTA_SCMI_CAPS_VALID_MASK (PTA_SCMI_CAPS_SMT_HEADER | \ ++ PTA_SCMI_CAPS_OCALL_THREAD) ++ ++/* ++ * enum optee_scmi_ocall_cmd ++ * enum optee_scmi_ocall_reply ++ * ++ * These enumerates define the IDs used by REE/TEE to communicate in the ++ * established REE/TEE Ocall thread context. ++ * ++ * At channel setup, we start from the REE: caller requests an Ocall context. ++ * ++ * 0. REE opens a session toward PTA SCMI. REE invokes PTA command ++ * PTA_SCMI_CMD_GET_CHANNEL to get a channel handler. ++ * ++ * 1. REE invokes command PTA_SCMI_CAPS_OCALL_THREAD with an Ocall context. ++ * This is the initial invocation of the Ocall thread context. Any further ++ * error in the thread communication will make Core to return to REE from ++ * this command invocation with an TEE_Result error code. ++ * ++ * 2. Upon support of Ocall the PTA creates an Ocall context and returns to ++ * REE with Ocall command PTA_SCMI_OCALL_CMD_THREAD_READY. ++ * ++ * 3. REE returns to the PTA, from the Ocall, with output param[0].value.a ++ * set to PTA_SCMI_OCALL_PROCESS_SMT_CHANNEL to post an SCMI message. ++ * In such case, OP-TEE processes the message and returns to REE with ++ * Ocall command PTA_SCMI_OCALL_CMD_THREAD_READY. The SCMI response is in ++ * the shared memory buffer. ++ * ++ * 4. Alternatively REE can return from the Ocall with output param[0].value.a ++ * set to PTA_SCMI_OCALL_CLOSE_THREAD. This requests OP-TEE to terminate the ++ * Ocall, release resources and return from initial command invocation at 1. ++ * as if REE closes the SCMI communication. ++ * ++ * At anytime, if an error is reported by Ocall commands or replies, SCMI PTA ++ * release the Ocall thread context and return from initial invocation at 1. ++ * PTA_SCMI_OCALL_ERROR is used in Ocall return to force an error report. ++ * ++ * REE channel initialization completes when returning from step 2. ++ * REE agent posts an SCMI message through step 3. ++ * At channel release, REE driver executes step 4. ++ */ ++ ++enum optee_scmi_ocall_cmd { ++ PTA_SCMI_OCALL_CMD_THREAD_READY = 0, ++}; ++ ++enum optee_scmi_ocall_reply { ++ PTA_SCMI_OCALL_ERROR = 0, ++ PTA_SCMI_OCALL_CLOSE_THREAD = 1, ++ PTA_SCMI_OCALL_PROCESS_SMT_CHANNEL = 2, ++}; + #endif /* SCMI_PTA_SCMI_CLIENT_H */ +diff --git a/lib/libutee/include/pta_tui.h b/lib/libutee/include/pta_tui.h +new file mode 100644 +index 000000000..1efe2900e +--- /dev/null ++++ b/lib/libutee/include/pta_tui.h +@@ -0,0 +1,56 @@ ++/* SPDX-License-Identifier: BSD-2-Clause */ ++/* ++ * Copyright (c) 2016-2021, Linaro Limited ++ */ ++ ++#ifndef __PTA_TUI_H ++#define __PTA_TUI_H ++ ++#define PTA_TUI_UUID { 0x9c199eb0, 0x4d2d, 0x41a5, { \ ++ 0x8c, 0xe9, 0xf2, 0x08, 0x52, 0x15, 0x77, 0xd1 } } ++ ++#define PTA_TUI_SCREEN_24BPP 0 ++ ++/* ++ * [out] value[0].a: width ++ * [out] value[0].b: height ++ * [out] value[1].a: width_dpi ++ * [out] value[1].b: height_dpi ++ * [out] value[2].a: bpp ++ * Returns TEE_Result ++ */ ++#define PTA_TUI_GET_SCREEN_INFO 1 ++ ++/* See TEE_TUIInitSession() */ ++#define PTA_TUI_INIT_SESSION 3 ++ ++/* See TEE_TUICloseSession() */ ++#define PTA_TUI_CLOSE_SESSION 4 ++ ++/* ++ * [in] value[0].a: color ++ */ ++#define PTA_TUI_INIT_SCREEN 5 ++ ++/* ++ * [in] value[0].a: xpos ++ * [in] value[0].b: ypos ++ * [in] value[1].a: width ++ * [in] value[1].b: height ++ * [in] memref[2]: image ++ */ ++#define PTA_TUI_SET_SCREEN_IMAGE 6 ++ ++/* ++ * See TEE_TUIDisplayScreen() ++ * [in] value[0].a clear_input (bool) ++ * [in] value[0].b time_out (milliseconds) ++ * [out] value[1].a is_timedout (bool) ++ * [out] value[2].a is_key (bool) ++ * [out] value[2].b key ++ * [out] value[3].a xpos ++ * [out] value[3].b ypos ++ */ ++#define PTA_TUI_DISPLAY_SCREEN 7 ++ ++#endif /*__PTA_TUI_H*/ +diff --git a/lib/libutee/include/remoteproc_pta.h b/lib/libutee/include/remoteproc_pta.h +new file mode 100644 +index 000000000..cede192eb +--- /dev/null ++++ b/lib/libutee/include/remoteproc_pta.h +@@ -0,0 +1,157 @@ ++/* SPDX-License-Identifier: BSD-2-Clause */ ++/* ++ * Copyright (C) 2020, STMicroelectronics - All Rights Reserved ++ */ ++ ++#ifndef __REMOTEPROC_PTA_H ++#define __REMOTEPROC_PTA_H ++ ++#include ++ ++/* ++ * Interface to the pseudo TA which provides platform implementation ++ * of the remote processor management ++ */ ++ ++#define PTA_REMOTEPROC_UUID { 0x54af4a68, 0x19be, 0x40d7, \ ++ { 0xbb, 0xe6, 0x89, 0x50, 0x35, 0x0a, 0x87, 0x44 } } ++ ++/* Firmware format */ ++#define PTA_REMOTEPROC_PROPRIETARY_FMT BIT32(0) ++#define PTA_REMOTEPROC_ELF_FMT BIT32(1) ++ ++/* Firmware image protection */ ++/* The platorm supports copy of the input firmware image in secure memory */ ++#define PTA_REMOTEPROC_FW_SECURE_COPY BIT32(0) ++/* The platorm supports load of segment with hash protection */ ++#define PTA_REMOTEPROC_FW_WITH_HASH_TABLE BIT32(1) ++/* The platorm is able to change access to secure the firmware input image */ ++#define PTA_REMOTEPROC_FW_MEMORY_PROTECTION BIT32(2) ++ ++/** ++ * struct rproc_pta_key_info - public key information ++ * @algo: Algorithm, defined by public key algorithms TEE_ALG_* ++ * from TEE Internal API specification ++ * @info_size: Byte size of the @info ++ * @info: Append key information data ++ */ ++struct rproc_pta_key_info { ++ uint32_t algo; ++ uint32_t info_size; ++ char info[]; ++}; ++ ++static inline size_t ++ rproc_pta_get_keyinfo_size(struct rproc_pta_key_info *keyinf) ++{ ++ size_t s = 0; ++ ++ if (!keyinf || ADD_OVERFLOW(sizeof(*keyinf), keyinf->info_size, &s)) ++ return 0; ++ ++ return s; ++} ++ ++#define RPROC_PTA_GET_KEYINFO_SIZE(x) rproc_pta_get_keyinfo_size((x)) ++ ++/* ++ * Platform capabilities. ++ * ++ * Get Platform firmware loader service capabilities. ++ * ++ * [in] params[0].value.a: Unique 32bit firmware identifier ++ * [out] params[1].value.a: Firmware format (PTA_REMOTEPROC_*_FMT) ++ * [out] params[2].value.a: Image protection method (PTA_REMOTEPROC_FW_*) ++ */ ++#define PTA_REMOTEPROC_HW_CAPABILITIES 1 ++ ++/* ++ * Firmware loading. ++ * ++ * Optional service to implement only in case of proprietary format. ++ * ++ * [in] params[0].value.a: Unique 32bit firmware identifier ++ * [in] params[1].memref: Loadable firmware image ++ */ ++#define PTA_REMOTEPROC_FIRMWARE_LOAD 2 ++ ++/* ++ * Load a segment with a SHA256 hash. ++ * ++ * This command is used when the platform secure memory is too expensive to ++ * save the whole firmware image in secure memory. Upon segment load, a ++ * successful completion ensures the loaded image complies with the provided ++ * hash. ++ * ++ * [in] params[0].value.a: Unique 32bit firmware identifier ++ * [in] params[1].memref: Section data to load ++ * [in] params[2].value.a: 32bit LSB load device segment address ++ * [in] params[2].value.b: 32bit MSB load device segment address ++ * [in] params[3].memref: Expected hash (SHA256) of the payload ++ */ ++#define PTA_REMOTEPROC_LOAD_SEGMENT_SHA256 3 ++ ++/* ++ * Memory set. ++ * ++ * Fill a remote device memory with requested value. this is use for instance ++ * to clear a memory on the remote firmware load. ++ * ++ * [in] params[0].value.a: Unique 32bit firmware identifier ++ * [in] params[1].value.a: 32bit LSB device memory address ++ * [in] params[1].value.b: 32bit MSB device memory address ++ * [in] params[2].value.a: 32bit LSB device memory size ++ * [in] params[2].value.b: 32bit MSB device memory size ++ * [in] params[3].value.a: Byte value to be set ++ */ ++#define PTA_REMOTEPROC_SET_MEMORY 4 ++ ++/* ++ * Firmware start. ++ * ++ * Start up a successfully remote processor firmware. ++ * ++ * [in] params[0].value.a: Unique 32bit firmware identifier ++ */ ++#define PTA_REMOTEPROC_FIRMWARE_START 5 ++ ++/* ++ * Firmware stop. ++ * ++ * Stop of the remote processor firmware and release/clean resources. ++ * After the command successful completion, remote processor firmware must be ++ * reloaded prior being started again. ++ * ++ * [in] params[0].value.a: Unique 32bit firmware identifier ++ */ ++#define PTA_REMOTEPROC_FIRMWARE_STOP 6 ++ ++/* ++ * Firmware device to physical address conversion. ++ * ++ * Return the physical address corresponding to an address got from the ++ * firmware address layout. ++ * ++ * [in] params[0].value.a: Unique 32bit firmware identifier ++ * [in] params[1].value.a: 32bit LSB Device memory address ++ * [in] params[1].value.b: 32bit MSB Device memory address ++ * [in] params[2].value.a: 32bit LSB Device memory size ++ * [in] params[2].value.b: 32bit MSB Device memory size ++ * [out] params[3].value.a: 32bit LSB converted physical address ++ * [out] params[3].value.b: 32bit MSB converted physical address ++ */ ++#define PTA_REMOTEPROC_FIRMWARE_DA_TO_PA 7 ++ ++/* ++ * Verify the firmware digest against a signature ++ * ++ * Return TEE_SUCCESS if the signature is verified, else an error ++ * ++ * [in] params[0].value.a: Unique 32bit firmware identifier ++ * [in] params[1].memref: Key information (refer to @rproc_pta_key_info) ++ * [in] params[2].memref: Digest of the firmware authenticated data ++ * [in] params[3].memref: Signature of the firmware authenticated data ++ */ ++#define PTA_REMOTEPROC_VERIFY_DIGEST 8 ++ ++#endif /* __REMOTEPROC_PTA_H */ +diff --git a/lib/libutee/include/stm32_pta_calib.h b/lib/libutee/include/stm32_pta_calib.h +new file mode 100644 +index 000000000..359c9a127 +--- /dev/null ++++ b/lib/libutee/include/stm32_pta_calib.h +@@ -0,0 +1,15 @@ ++// SPDX-License-Identifier: BSD-2-Clause ++/* ++ * Copyright (c) 2022, STMicroelectronics ++ */ ++ ++#ifndef __PTA_CALIB_H ++#define __PTA_CALIB_H ++ ++#define PTA_CALIB_UUID { 0xee4e317c, 0x568b, 0x485f, { \ ++ 0xb0, 0xb3, 0xd4, 0x92, 0x42, 0x6c, 0x47, 0x96 } } ++ ++/* See request_calibration() */ ++#define PTA_CALIB_REQUEST 0 ++ ++#endif /*__PTA_CALIB_H*/ +diff --git a/lib/libutee/include/tee_tui_api.h b/lib/libutee/include/tee_tui_api.h +new file mode 100644 +index 000000000..659433a50 +--- /dev/null ++++ b/lib/libutee/include/tee_tui_api.h +@@ -0,0 +1,24 @@ ++/* SPDX-License-Identifier: BSD-2-Clause */ ++/* ++ * Copyright (c) 2016-2021, Linaro Limited ++ */ ++ ++#ifndef __TEE_TUI_API_H ++#define __TEE_TUI_API_H ++ ++#include ++ ++TEE_Result TEE_TUICheckTextFormat(const char *text, uint32_t *width, ++ uint32_t *height, uint32_t *lastIndex); ++TEE_Result TEE_TUIGetScreenInfo(TEE_TUIScreenOrientation screenOrientation, ++ uint32_t nbEntryFields, ++ TEE_TUIScreenInfo *screenInfo); ++TEE_Result TEE_TUIInitSession(void); ++TEE_Result TEE_TUICloseSession(void); ++TEE_Result TEE_TUIDisplayScreen(const TEE_TUIScreenConfiguration *screenConfiguration, ++ bool closeTUISession, ++ TEE_TUIEntryField *entryFields, ++ uint32_t entryFieldCount, ++ TEE_TUIButtonType *selectedButton); ++ ++#endif /*__TEE_TUI_API_H*/ +diff --git a/lib/libutee/include/tee_tui_api_types.h b/lib/libutee/include/tee_tui_api_types.h +new file mode 100644 +index 000000000..78f56e110 +--- /dev/null ++++ b/lib/libutee/include/tee_tui_api_types.h +@@ -0,0 +1,119 @@ ++/* SPDX-License-Identifier: BSD-2-Clause */ ++/* ++ * Copyright (c) 2016-2021, Linaro Limited ++ */ ++ ++#ifndef __TEE_TUI_API_TYPES_H ++#define __TEE_TUI_API_TYPES_H ++ ++#include ++ ++#define TEE_TUI_NUMBER_BUTTON_TYPES 0x00000006 ++ ++typedef enum { ++ TEE_TUI_HIDDEN_MODE = 0, ++ TEE_TUI_CLEAR_MODE, ++ TEE_TUI_TEMPORARY_CLEAR_MODE, ++} TEE_TUIEntryFieldMode; ++ ++typedef enum { ++ TEE_TUI_NUMERICAL = 0, ++ TEE_TUI_ALPHANUMERICAL, ++} TEE_TUIEntryFieldType; ++ ++typedef enum { ++ TEE_TUI_PORTRAIT = 0, ++ TEE_TUI_LANDSCAPE, ++} TEE_TUIScreenOrientation; ++ ++typedef enum { ++ TEE_TUI_CORRECTION = 0, ++ TEE_TUI_OK, ++ TEE_TUI_CANCEL, ++ TEE_TUI_VALIDATE, ++ TEE_TUI_PREVIOUS, ++ TEE_TUI_NEXT, ++} TEE_TUIButtonType; ++ ++typedef enum { ++ TEE_TUI_NO_SOURCE = 0, ++ TEE_TUI_REF_SOURCE, ++ TEE_TUI_OBJECT_SOURCE, ++} TEE_TUIImageSource; ++ ++typedef struct { ++ TEE_TUIImageSource source; ++ ++ __extension__ union { ++ struct { ++ void *image; ++ size_t imageLength; ++ } ref; ++ struct { ++ uint32_t storageID; ++ void *objectID; ++ size_t objectIDLen; ++ } object; ++ }; ++ uint32_t width; ++ uint32_t height; ++} TEE_TUIImage; ++ ++typedef struct { ++ char *text; ++ uint32_t textXOffset; ++ uint32_t textYOffset; ++ uint8_t textColor[3]; ++ TEE_TUIImage image; ++ uint32_t imageXOffset; ++ uint32_t imageYOffset; ++} TEE_TUIScreenLabel; ++ ++typedef struct { ++ char *text; ++ TEE_TUIImage image; ++} TEE_TUIButton; ++ ++typedef struct { ++ TEE_TUIScreenOrientation screenOrientation; ++ TEE_TUIScreenLabel label; ++ TEE_TUIButton * buttons[TEE_TUI_NUMBER_BUTTON_TYPES]; ++ bool requestedButtons[TEE_TUI_NUMBER_BUTTON_TYPES]; ++} TEE_TUIScreenConfiguration; ++ ++typedef struct { ++ char *buttonText; ++ uint32_t buttonWidth; ++ uint32_t buttonHeight; ++ bool buttonTextCustom; ++ bool buttonImageCustom; ++} TEE_TUIScreenButtonInfo; ++ ++typedef struct { ++ uint32_t grayscaleBitsDepth; ++ uint32_t redBitsDepth; ++ uint32_t greenBitsDepth; ++ uint32_t blueBitsDepth; ++ uint32_t widthInch; ++ uint32_t heightInch; ++ uint32_t maxEntryFields; ++ uint32_t entryFieldLabelWidth; ++ uint32_t entryFieldLabelHeight; ++ uint32_t maxEntryFieldLength; ++ uint8_t labelColor[3]; ++ uint32_t labelWidth; ++ uint32_t labelHeight; ++ TEE_TUIScreenButtonInfo buttonInfo[TEE_TUI_NUMBER_BUTTON_TYPES]; ++} TEE_TUIScreenInfo; ++ ++typedef struct { ++ char *label; ++ TEE_TUIEntryFieldMode mode; ++ TEE_TUIEntryFieldType type; ++ uint32_t minExpectedLength; ++ uint32_t maxExpectedLength; ++ char *buffer; ++ size_t bufferLength; ++} TEE_TUIEntryField; ++ ++#endif /*__TEE_TUI_API_TYPES_H*/ +diff --git a/lib/libutee/sub.mk b/lib/libutee/sub.mk +index 266ff49bb..c82eef517 100644 +--- a/lib/libutee/sub.mk ++++ b/lib/libutee/sub.mk +@@ -16,6 +16,11 @@ srcs-y += tee_api_property.c + srcs-y += tee_socket_pta.c + srcs-y += tee_system_pta.c + srcs-y += tee_tcpudp_socket.c ++ ++ifeq ($(CFG_WITH_TUI),y) ++subdirs-y += tui ++endif ++ + endif #ifneq ($(sm),ldelf) + + subdirs-y += arch/$(ARCH) +diff --git a/lib/libutee/tee_api_property.c b/lib/libutee/tee_api_property.c +index 949e15235..7809bec62 100644 +--- a/lib/libutee/tee_api_property.c ++++ b/lib/libutee/tee_api_property.c +@@ -10,6 +10,7 @@ + #include + #include + #include ++#include + #include + #include + #include +@@ -46,6 +47,16 @@ const struct user_ta_property tee_props[] = { + USER_TA_PROP_TYPE_U32, + &(const uint32_t){TEE_ISOCKET_VERSION} + }, ++ { ++ "gpd.tee.tui.languages", ++ USER_TA_PROP_TYPE_STRING, ++ "en" ++ }, ++ { ++ "gpd.tee.tui.orientation", ++ USER_TA_PROP_TYPE_U32, ++ &(const uint32_t){1 << TEE_TUI_LANDSCAPE} ++ }, + }; + + static TEE_Result propset_get(TEE_PropSetHandle h, +diff --git a/lib/libutee/tui/font.c b/lib/libutee/tui/font.c +new file mode 100644 +index 000000000..5c7520608 +--- /dev/null ++++ b/lib/libutee/tui/font.c +@@ -0,0 +1,240 @@ ++// SPDX-License-Identifier: BSD-2-Clause ++/* ++ * Copyright (c) 2016-2021, Linaro Limited ++ */ ++ ++#include ++#include ++#include ++#include ++ ++#include "font.h" ++#include "default_bold.h" ++#include "default_regular.h" ++#include "utf8.h" ++ ++#define UCP_CARRIAGE_RETURN 0x000D ++#define UCP_TUI_BOLD 0xE000 ++#define UCP_TUI_UNDERLINE 0xE001 ++#define UCP_TUI_MOVE_RIGHT 0xE002 ++#define UCP_TUI_MOVE_DOWN 0xE003 ++ ++static const struct font *font_regular = &font_default_regular; ++static const struct font *font_bold = &font_default_bold; ++ ++bool font_set_fonts(const struct font *regular, const struct font *bold) ++{ ++ if (regular->height != bold->height) ++ return false; ++ font_regular = regular; ++ font_bold = bold; ++ return true; ++} ++ ++static const struct font_letter *get_letter(const struct font *font, ++ uint32_t cp) ++{ ++ ssize_t idx = cp - font->first; ++ ++ if (idx < 0 || cp > font->last) ++ return NULL; ++ return font->letters + idx; ++} ++ ++bool font_check_text_format(const char *text, size_t *width, size_t *height, ++ size_t *last_idx) ++{ ++ size_t xp = 0; ++ size_t xmax = 0; ++ size_t ymax = 0; ++ size_t idx = 0; ++ const struct font *font[2] = { font_regular, font_bold }; ++ const struct font_letter *letter; ++ bool ret = false; ++ uint32_t cp; ++ bool bold = false; ++ size_t font_height = font[0]->height; ++ ++ if (!text) ++ goto out; ++ ++ ymax = font_height; ++ while (text[idx]) { ++ cp = utf8_get_code_point(text, &idx); ++ switch (cp) { ++ case UCP_CARRIAGE_RETURN: ++ xp = 0; ++ ymax += font_height; ++ continue; ++ case UCP_TUI_BOLD: ++ bold = !bold; ++ continue; ++ case UCP_TUI_UNDERLINE: ++ continue; ++ case UCP_TUI_MOVE_RIGHT: ++ xp++; ++ if (xp > xmax) ++ xmax = xp; ++ continue; ++ case UCP_TUI_MOVE_DOWN: ++ ymax++; ++ continue; ++ case UTF8_INVALID_CODE: ++ goto out; ++ default: ++ break; ++ } ++ if (cp == UTF8_INVALID_CODE) ++ goto out; ++ letter = get_letter(font[bold], cp); ++ if (!letter) ++ goto out; ++ xp += letter->width; ++ if (xp > xmax) ++ xmax = xp; ++ } ++ ret = true; ++ ++out: ++ if (width) ++ *width = xmax + 1; ++ if (height) ++ *height = ymax + 1; ++ if (last_idx) ++ *last_idx = idx; ++ return ret; ++} ++ ++size_t font_get_max_field_width(size_t num_letters) ++{ ++ return num_letters * font_regular->max_width; ++} ++ ++size_t font_get_max_field_length(size_t width) ++{ ++ return width / font_regular->max_width; ++} ++ ++size_t font_get_text_height(void) ++{ ++ return font_regular->height; ++} ++ ++static bool letter_get_bit(const struct font_letter *letter, size_t x, size_t y) ++{ ++ const uint8_t *bstr = letter->blob; ++ size_t pos = y * ROUNDUP(letter->width, 8) + x; ++ size_t byte_pos = pos / 8; ++ uint8_t bit_mask = 1 << (7 - (pos & 0x7)); ++ ++ assert(byte_pos < letter->blob_size); ++ return !!(bstr[byte_pos] & bit_mask); ++} ++ ++static bool render_letter(struct image *image, size_t xpos, size_t ypos, ++ const struct font_letter *letter, ++ size_t letter_height, uint32_t color) ++{ ++ size_t x; ++ size_t y; ++ bool res = true; ++ ++ for (y = 0; y < letter_height; y++) { ++ for (x = 0; x < letter->width; x++) { ++ if (letter_get_bit(letter, x, y)) { ++ if (!image_set_pixel(image, xpos + x, ypos + y, ++ color)) ++ res = false; ++ } ++ } ++ } ++ return res; ++} ++ ++bool font_render_text(struct image *image, size_t xpos, size_t ypos, ++ const char *text, uint32_t color) ++{ ++ size_t xp = xpos; ++ size_t yp = ypos; ++ size_t idx = 0; ++ const struct font *font[2] = { font_regular, font_bold }; ++ const struct font_letter *letter; ++ bool bold = false; ++ bool underline = false; ++ uint32_t cp; ++ size_t font_height = font[0]->height; ++ ++ if (!text) ++ return false; ++ ++ while (text[idx]) { ++ cp = utf8_get_code_point(text, &idx); ++ switch (cp) { ++ case UCP_CARRIAGE_RETURN: ++ xp = xpos; ++ yp += font_height; ++ continue; ++ case UCP_TUI_BOLD: ++ bold = !bold; ++ continue; ++ case UCP_TUI_UNDERLINE: ++ underline = !underline; ++ continue; ++ case UCP_TUI_MOVE_RIGHT: ++ xp++; ++ continue; ++ case UCP_TUI_MOVE_DOWN: ++ yp++; ++ continue; ++ case UTF8_INVALID_CODE: ++ return false; ++ default: ++ break; ++ } ++ if (cp == UTF8_INVALID_CODE) ++ return false; ++ letter = get_letter(font[bold], cp); ++ if (!letter) ++ return false; ++ if (!render_letter(image, xp, yp, letter, font_height, ++ color)) ++ return false; ++ if (underline) { ++ const struct font_letter *l; ++ size_t over_shoot; ++ ++ l = get_letter(font[bold], '_'); ++ if (!l) ++ return false; ++ if (!render_letter(image, xp, yp, l, font_height, ++ color)) ++ return false; ++ /* ++ * If the letter _ is narrower than the rendered ++ * letter we need to render a second _ to form a ++ * solid line. As the parts of the _ letter ++ * contains some empty space we calculate an ++ * approximate number of pixels to add to ++ * compensate for that. ++ */ ++ over_shoot = (l->width / 10) + 1; ++ if ((l->width - over_shoot) < letter->width) { ++ size_t offs = letter->width - ++ (l->width - over_shoot); ++ ++ /* ++ * Ignore return value as we may try to ++ * write a part of the underscore outside ++ * the image area. It will not risk ++ * confusing the message in this particular ++ * case as the real text is rendered ++ * properly in either way. ++ */ ++ render_letter(image, xp + offs, yp, l, ++ font_height, color); ++ } ++ } ++ xp += letter->width; ++ } ++ return true; ++} +diff --git a/lib/libutee/tui/font.h b/lib/libutee/tui/font.h +new file mode 100644 +index 000000000..1c096f0db +--- /dev/null ++++ b/lib/libutee/tui/font.h +@@ -0,0 +1,40 @@ ++/* SPDX-License-Identifier: BSD-2-Clause */ ++/* ++ * Copyright (c) 2016-2021, Linaro Limited ++ */ ++ ++#ifndef __FONT_H ++#define __FONT_H ++ ++#include ++#include "image.h" ++ ++struct font_letter { ++ const unsigned char *blob; ++ unsigned int blob_size; ++ unsigned int width; ++}; ++ ++struct font { ++ unsigned int first; ++ unsigned int last; ++ const struct font_letter *letters; ++ unsigned int height; ++ unsigned int max_width; ++}; ++ ++bool font_set_fonts(const struct font *regular, const struct font *bold); ++ ++bool font_check_text_format(const char *text, size_t *width, size_t *height, ++ size_t *last_index); ++ ++size_t font_get_max_field_length(size_t width); ++ ++size_t font_get_max_field_width(size_t num_letters); ++ ++size_t font_get_text_height(void); ++ ++bool font_render_text(struct image *image, size_t xpos, size_t ypos, ++ const char *text, uint32_t color); ++ ++#endif /*__FONT_H*/ +diff --git a/lib/libutee/tui/image.c b/lib/libutee/tui/image.c +new file mode 100644 +index 000000000..7aca5beba +--- /dev/null ++++ b/lib/libutee/tui/image.c +@@ -0,0 +1,109 @@ ++// SPDX-License-Identifier: BSD-2-Clause ++/* ++ * Copyright (c) 2016-2021, Linaro Limited ++ */ ++ ++#include ++#include ++#include "image.h" ++ ++static uint32_t color_to_pixel(uint32_t color) ++{ ++#if defined(CFG_STM32_LTDC) ++ return color | 0xFF000000; ++#else ++ /* Convert from ARGB to RGBA and byte swap */ ++ return TEE_U32_BSWAP((color >> 24) | (color << 8)); ++#endif ++} ++ ++struct image *image_alloc(size_t width, size_t height, uint32_t color) ++{ ++ struct image *image = malloc(sizeof(*image)); ++ size_t n; ++ uint32_t *b; ++ ++ if (!image) ++ return NULL; ++ image->blen = sizeof(uint32_t) * height * width; ++ image->buf = malloc(image->blen); ++ if (!image->buf) { ++ free(image); ++ return NULL; ++ } ++ b = image->buf; ++ image->height = height; ++ image->width = width; ++ for (n = 0; n < (height * width); n++) ++ b[n] = color_to_pixel(color); ++ return image; ++} ++ ++void image_free(struct image *image) ++{ ++ if (image) { ++ free(image->buf); ++ free(image); ++ } ++} ++ ++void *image_get_pixel_ptr(struct image *image, size_t x, size_t y) ++{ ++ uint32_t *b = image->buf; ++ size_t pos; ++ ++ if (x >= image->width || y >= image->height) ++ return NULL; ++ ++ pos = y * image->width + x; ++ if (pos >= image->blen) ++ return NULL; ++ ++ return b + pos; ++} ++ ++bool image_set_pixel(struct image *image, size_t x, size_t y, uint32_t color) ++{ ++ uint32_t *p = image_get_pixel_ptr(image, x, y); ++ ++ if (!p) ++ return false; ++ *p = color_to_pixel(color); ++ return true; ++} ++ ++bool image_set_border(struct image *image, size_t size, uint32_t color) ++{ ++ size_t x; ++ size_t y; ++ ++ /* Size * 2 since the border appears on both sides etc */ ++ if (size * 2 > image->width || size * 2 > image->height) ++ return false; ++ ++ /* Top horizonal line */ ++ for (y = 0; y < size; y++) ++ for (x = 0; x < image->width; x++) ++ if (!image_set_pixel(image, x, y, color)) ++ return false; ++ ++ /* Bottom horizonal line */ ++ for (y = image->height - size; y < image->height; y++) ++ for (x = 0; x < image->width; x++) ++ if (!image_set_pixel(image, x, y, color)) ++ return false; ++ ++ /* Left vertical line */ ++ for (y = 0; y < image->height; y++) ++ for (x = 0; x < size; x++) ++ if (!image_set_pixel(image, x, y, color)) ++ return false; ++ ++ /* Right vertical line */ ++ for (y = 0; y < image->height; y++) ++ for (x = image->width - size; x < image->width; x++) ++ if (!image_set_pixel(image, x, y, color)) ++ return false; ++ ++ return true; ++} +diff --git a/lib/libutee/tui/image.h b/lib/libutee/tui/image.h +new file mode 100644 +index 000000000..1227543e9 +--- /dev/null ++++ b/lib/libutee/tui/image.h +@@ -0,0 +1,49 @@ ++/* SPDX-License-Identifier: BSD-2-Clause */ ++/* ++ * Copyright (c) 2016-2021, Linaro Limited ++ */ ++ ++#ifndef __IMAGE_H ++#define __IMAGE_H ++ ++#include ++ ++/* ++ * Selection of values and names from ++ * https://en.wikipedia.org/wiki/X11_color_names ++ * ++ * The top byte is the alpha channel ++ */ ++#define COLOR_RGB_BLACK 0x00000000 ++#define COLOR_RGB_BLUE 0x000000FF ++#define COLOR_RGB_CYAN 0x0000FFFF ++#define COLOR_RGB_DARK_GRAY 0x00A9A9A9 ++#define COLOR_RGB_DARK_GREEN 0x00006a00 ++#define COLOR_RGB_DARK_RED 0x008B0000 ++#define COLOR_RGB_DARK_TURQUOISE 0x008B0000 ++#define COLOR_RGB_GRAY 0x00BEBEBE ++#define COLOR_RGB_GREEN 0x0000FF00 ++#define COLOR_RGB_LIGHT_GOLDENROD 0x00FAFAD2 ++#define COLOR_RGB_LIGHT_GRAY 0x00D3D3D3 ++#define COLOR_RGB_RED 0x00FF0000 ++#define COLOR_RGB_WHITE 0x00FFFFFF ++#define COLOR_RGB_YELLOW 0x00FFFF00 ++ ++struct image { ++ size_t height; ++ size_t width; ++ void *buf; ++ size_t blen; ++}; ++ ++struct image *image_alloc(size_t width, size_t height, uint32_t color); ++void image_free(struct image *image); ++ ++void *image_get_pixel_ptr(struct image *image, size_t x, size_t y); ++bool image_set_pixel(struct image *image, size_t x, size_t y, uint32_t color); ++bool image_set_border(struct image *image, size_t size, uint32_t color); ++ ++bool image_set_png(struct image *image, size_t x, size_t y, const void *data, ++ size_t data_len); ++ ++#endif /*__IMAGE_H*/ +diff --git a/lib/libutee/tui/image_png.c b/lib/libutee/tui/image_png.c +new file mode 100644 +index 000000000..e6b5ac110 +--- /dev/null ++++ b/lib/libutee/tui/image_png.c +@@ -0,0 +1,141 @@ ++// SPDX-License-Identifier: BSD-2-Clause ++/* ++ * Copyright (c) 2016-2021, Linaro Limited ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include "image.h" ++ ++struct image_work { ++ jmp_buf jmpbuf; ++ const uint8_t *data; ++ const uint8_t *end_data; ++}; ++ ++static void error_cb(png_structp png_ptr, png_const_charp msg __maybe_unused) ++{ ++ struct image_work *w; ++ ++ EMSG("%s", msg); ++ ++ w = png_get_error_ptr(png_ptr); ++ if (!w) ++ TEE_Panic(0); ++ longjmp(w->jmpbuf, 1); ++} ++ ++static void warning_cb(png_structp png_ptr __unused, ++ png_const_charp warning_msg __unused) ++{ ++ IMSG("%s", warning_msg); ++} ++ ++static void read_cb(png_structp png_ptr, png_bytep data, png_size_t length) ++{ ++ struct image_work *w = png_get_io_ptr(png_ptr); ++ ++ if (!w) ++ png_error(png_ptr, "read_data: w is NULL"); ++ ++ if ((w->data + length) > w->end_data || (uint8_t *)length > w->end_data) ++ png_error(png_ptr, "reached end of file"); ++ ++ memcpy(data, w->data, length); ++ w->data += length; ++} ++ ++bool image_set_png(struct image *image, size_t x, size_t y, const void *data, ++ size_t data_len) ++{ ++ /* volatile to avoid clobbering when setjmp() returns the second time */ ++ volatile bool rv = false; ++ png_bytep * volatile row_ptrs = NULL; ++ png_structp png_ptr; ++ png_infop info_ptr; ++ png_byte color_type; ++ png_byte bit_depth; ++ size_t n; ++ size_t width; ++ size_t height; ++ struct image_work work = { ++ .data = data, ++ .end_data = (const uint8_t *)data + data_len, ++ }; ++ ++ if (png_sig_cmp(data, 0, data_len)) ++ return false; ++ ++ png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, &work, ++ error_cb, warning_cb); ++ if (!png_ptr) ++ return false; ++ ++ info_ptr = png_create_info_struct(png_ptr); ++ if (!info_ptr) { ++ png_destroy_read_struct(&png_ptr, NULL, NULL); ++ return false; ++ } ++ ++ if (setjmp(work.jmpbuf)) ++ goto out; ++ ++ png_set_read_fn(png_ptr, &work, read_cb); ++ png_read_info(png_ptr, info_ptr); ++ ++ width = png_get_image_width(png_ptr, info_ptr); ++ height = png_get_image_height(png_ptr, info_ptr); ++ color_type = png_get_color_type(png_ptr, info_ptr); ++ bit_depth = png_get_bit_depth(png_ptr, info_ptr); ++ ++ if ((x + width) > image->width || (y + height) > image->height) ++ goto out; ++ ++ /* ++ * Read any color_type into 8bit depth, RGBA format. ++ * See http://www.libpng.org/pub/png/libpng-manual.txt ++ */ ++ ++ if (bit_depth == 16) ++ png_set_strip_16(png_ptr); ++ ++ if (color_type == PNG_COLOR_TYPE_PALETTE) ++ png_set_palette_to_rgb(png_ptr); ++ ++ /* PNG_COLOR_TYPE_GRAY_ALPHA is always 8 or 16bit depth. */ ++ if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) ++ png_set_expand_gray_1_2_4_to_8(png_ptr); ++ ++ if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) ++ png_set_tRNS_to_alpha(png_ptr); ++ ++ /* These color_type don't have an alpha channel then fill it with 0 */ ++ if (color_type == PNG_COLOR_TYPE_RGB || ++ color_type == PNG_COLOR_TYPE_GRAY || ++ color_type == PNG_COLOR_TYPE_PALETTE) ++ png_set_filler(png_ptr, 0, PNG_FILLER_AFTER); ++ ++ if (color_type == PNG_COLOR_TYPE_GRAY || ++ color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ++ png_set_gray_to_rgb(png_ptr); ++ ++ png_read_update_info(png_ptr, info_ptr); ++ ++ row_ptrs = malloc(sizeof(png_bytep) * height); ++ if (!row_ptrs) ++ goto out; ++ for (n = 0; n < height; n++) ++ row_ptrs[n] = image_get_pixel_ptr(image, x, y + n); ++ ++ png_read_image(png_ptr, row_ptrs); ++ rv = true; ++out: ++ free(row_ptrs); ++ png_destroy_read_struct(&png_ptr, &info_ptr, NULL); ++ return rv; ++} +diff --git a/lib/libutee/tui/sub.mk b/lib/libutee/tui/sub.mk +new file mode 100644 +index 000000000..c820037d0 +--- /dev/null ++++ b/lib/libutee/tui/sub.mk +@@ -0,0 +1,38 @@ ++srcs-y += tee_tui.c ++srcs-y += tui_pta.c ++srcs-y += tui_widget.c ++srcs-y += tui_keyboard.c ++srcs-y += tui_entry.c ++srcs-y += utf8.c ++srcs-y += image.c ++srcs-y += image_png.c ++srcs-y += font.c ++ ++# Need to locate font.h from generated sources ++incdirs-y += . ++ ++render_font:=scripts/render_font.py ++ ++gensrcs-y += default_bold ++force-gensrc-default_bold = y ++produce-additional-default_bold = default_bold.h ++produce-default_bold = default_bold.c ++depends-default_bold := $(render_font) \ ++ $(sub-dir)/fonts/amble/Amble-Bold.ttf ++recipe-default_bold := $(PYTHON3) $(render_font) \ ++ --font_file $(sub-dir)/fonts/amble/Amble-Bold.ttf \ ++ --font_size 20 --font_name default_bold \ ++ --out_dir $(sub-dir-out) ++cleanfiles += $(sub-dir-out)/default_bold.c $(sub-dir-out)/default_bold.h ++ ++gensrcs-y += default_regular ++force-gensrc-default_regular = y ++produce-additional-default_regular = default_regular.h ++produce-default_regular = default_regular.c ++depends-default_regular := $(render_font) \ ++ $(sub-dir)/fonts/amble/Amble-Regular.ttf ++recipe-default_regular := $(PYTHON3) $(render_font) \ ++ --font_file $(sub-dir)/fonts/amble/Amble-Regular.ttf \ ++ --font_size 20 --font_name default_regular \ ++ --out_dir $(sub-dir-out) ++cleanfiles += $(sub-dir-out)/default_regular.c $(sub-dir-out)/default_regular.h +diff --git a/lib/libutee/tui/tee_tui.c b/lib/libutee/tui/tee_tui.c +new file mode 100644 +index 000000000..c68c60683 +--- /dev/null ++++ b/lib/libutee/tui/tee_tui.c +@@ -0,0 +1,554 @@ ++// SPDX-License-Identifier: BSD-2-Clause ++/* ++ * Copyright (c) 2016-2021, Linaro Limited ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include "font.h" ++#include "image.h" ++#include "tui_private.h" ++#include ++ ++#define MAX_LABEL_ROWS 4 ++ ++/* update_button_info() has to be called before using this the first time */ ++static TEE_TUIScreenButtonInfo button_info[TEE_TUI_NUMBER_BUTTON_TYPES] = { ++ [TEE_TUI_CORRECTION] = { ++ .buttonText = (char *)"Correction", ++ .buttonTextCustom = false, ++ .buttonImageCustom = true, ++ }, ++ [TEE_TUI_OK] = { ++ .buttonText = (char *)"OK", ++ .buttonTextCustom = false, ++ .buttonImageCustom = true, ++ }, ++ [TEE_TUI_CANCEL] = { ++ .buttonText = (char *)"Cancel", ++ .buttonTextCustom = false, ++ .buttonImageCustom = true, ++ }, ++ [TEE_TUI_VALIDATE] = { ++ .buttonText = (char *)"Validate", ++ .buttonTextCustom = false, ++ .buttonImageCustom = true, ++ }, ++ [TEE_TUI_PREVIOUS] = { ++ .buttonText = (char *)"Previous", ++ .buttonTextCustom = false, ++ .buttonImageCustom = true, ++ }, ++ [TEE_TUI_NEXT] = { ++ .buttonText = (char *)"Next", ++ .buttonTextCustom = false, ++ .buttonImageCustom = true, ++ }, ++}; ++ ++static bool check_result(TEE_Result res, const TEE_Result *results, ++ size_t num_results) ++{ ++ size_t n; ++ ++ for (n = 0; n < num_results; n++) ++ if (res == results[n]) ++ return true; ++ return false; ++} ++ ++#define CHECK_RESULT(r, results) \ ++ do { \ ++ if (!check_result((r), (results), ARRAY_SIZE(results))) \ ++ TEE_Panic(r); \ ++ } while (0) ++ ++TEE_Result TEE_TUICheckTextFormat(const char *text, uint32_t *width, ++ uint32_t *height, uint32_t *lastIndex) ++{ ++ TEE_Result res; ++ size_t w; ++ size_t h; ++ size_t last_index; ++ ++ if (font_check_text_format(text, &w, &h, &last_index)) ++ res = TEE_SUCCESS; ++ else ++ res = TEE_ERROR_NOT_SUPPORTED; ++ *width = w; ++ *height = h; ++ *lastIndex = last_index; ++ return res; ++} ++ ++static TEE_Result update_button_info(void) ++{ ++ size_t n; ++ size_t h; ++ size_t w; ++ ++ for (n = 0; n < TEE_TUI_NUMBER_BUTTON_TYPES; n++) { ++ if (button_info[n].buttonWidth) ++ continue; ++ ++ if (!font_check_text_format(button_info[n].buttonText, &w, &h, ++ NULL)) ++ return TEE_ERROR_GENERIC; ++ button_info[n].buttonWidth = w; ++ button_info[n].buttonHeight = h; ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++TEE_Result TEE_TUIGetScreenInfo(TEE_TUIScreenOrientation screenOrientation, ++ uint32_t nbEntryFields, ++ TEE_TUIScreenInfo *screenInfo) ++{ ++ TEE_Result res; ++ const TEE_Result results[] = { TEE_ERROR_NOT_SUPPORTED, TEE_SUCCESS, }; ++ size_t width; ++ size_t height; ++ size_t width_dpi; ++ size_t height_dpi; ++ uint32_t bpp; ++ uint32_t label_color = COLOR_RGB_BLACK; ++ ++ res = update_button_info(); ++ if (res != TEE_SUCCESS) ++ TEE_Panic(res); ++ res = __tui_pta_get_screen_info(&width, &height, &width_dpi, ++ &height_dpi, &bpp); ++ CHECK_RESULT(res, results); ++ if (res != TEE_SUCCESS) ++ return res; ++ if (screenOrientation != TEE_TUI_LANDSCAPE) ++ res = TEE_ERROR_NOT_SUPPORTED; ++ if (nbEntryFields > MAX_ENTRY_FIELDS) ++ res = TEE_ERROR_NOT_SUPPORTED; ++ screenInfo->grayscaleBitsDepth = 255; ++ screenInfo->redBitsDepth = 255; ++ screenInfo->greenBitsDepth = 255; ++ screenInfo->blueBitsDepth = 255; ++ screenInfo->widthInch = width / width_dpi; ++ screenInfo->heightInch = height / height_dpi; ++ screenInfo->maxEntryFields = MAX_ENTRY_FIELDS; ++ screenInfo->entryFieldLabelWidth = ++ width - button_info[TEE_TUI_CORRECTION].buttonWidth; ++ screenInfo->entryFieldLabelHeight = font_get_text_height(); ++ screenInfo->maxEntryFieldLength = ++ font_get_max_field_length(screenInfo->entryFieldLabelWidth - ++ 4 * BORDER_SIZE); ++ screenInfo->labelColor[0] = label_color; ++ screenInfo->labelColor[1] = label_color >> 8; ++ screenInfo->labelColor[2] = label_color >> 16; ++ screenInfo->labelWidth = width; ++ screenInfo->labelHeight = font_get_text_height() * MAX_LABEL_ROWS; ++ memcpy(screenInfo->buttonInfo, button_info, sizeof(button_info)); ++ return res; ++} ++ ++TEE_Result TEE_TUIInitSession(void) ++{ ++ TEE_Result res; ++ const TEE_Result results[] = { ++ TEE_ERROR_BUSY, TEE_ERROR_OUT_OF_MEMORY, TEE_SUCCESS, ++ }; ++ ++ res = __tui_pta_init_session(); ++ CHECK_RESULT(res, results); ++ return res; ++} ++ ++TEE_Result TEE_TUICloseSession(void) ++{ ++ TEE_Result res; ++ const TEE_Result results[] = { ++ TEE_ERROR_BUSY, TEE_ERROR_BAD_STATE, TEE_SUCCESS, ++ }; ++ ++ res = __tui_pta_close_session(); ++ CHECK_RESULT(res, results); ++ return res; ++} ++ ++static TEE_Result set_label_image(struct image *image, size_t x, size_t y, ++ const TEE_TUIImage *tui_image) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ TEE_ObjectHandle handle = TEE_HANDLE_NULL; ++ void *buf = NULL; ++ TEE_ObjectInfo obj_info; ++ uint32_t count; ++ ++ switch (tui_image->source) { ++ case TEE_TUI_REF_SOURCE: ++ if (!image_set_png(image, x, y, tui_image->ref.image, ++ tui_image->ref.imageLength)) ++ res = TEE_ERROR_BAD_FORMAT; ++ break; ++ case TEE_TUI_OBJECT_SOURCE: ++ res = TEE_OpenPersistentObject(tui_image->object.storageID, ++ tui_image->object.objectID, ++ tui_image->object.objectIDLen, ++ TEE_DATA_FLAG_ACCESS_READ | ++ TEE_DATA_FLAG_SHARE_READ, ++ &handle); ++ if (res != TEE_SUCCESS) ++ break; ++ ++ res = TEE_GetObjectInfo1(handle, &obj_info); ++ if (res != TEE_SUCCESS) ++ break; ++ ++ buf = malloc(obj_info.dataSize); ++ if (!buf) { ++ res = TEE_ERROR_OUT_OF_MEMORY; ++ break; ++ } ++ ++ res = TEE_ReadObjectData(handle, buf, obj_info.dataSize, ++ &count); ++ if (res != TEE_SUCCESS) ++ break; ++ ++ if (count != obj_info.dataSize) { ++ res = TEE_ERROR_BAD_FORMAT; ++ break; ++ } ++ ++ if (!image_set_png(image, x, y, buf, obj_info.dataSize)) ++ res = TEE_ERROR_BAD_FORMAT; ++ break; ++ ++ default: ++ res = TEE_ERROR_BAD_FORMAT; ++ } ++ ++ TEE_CloseObject(handle); ++ free(buf); ++ return res; ++} ++ ++static TEE_Result set_label(const TEE_TUIScreenLabel *label, size_t *ypos) ++{ ++ TEE_Result res; ++ struct image *image = NULL; ++ size_t w; ++ size_t h; ++ size_t xmin; ++ size_t ymin; ++ size_t xmax; ++ size_t ymax; ++ uint32_t color; ++ ++ if (!font_check_text_format(label->text, &w, &h, NULL)) ++ TEE_Panic(TEE_ERROR_NOT_SUPPORTED); ++ ++ if (label->image.source != TEE_TUI_NO_SOURCE) { ++ xmin = MIN(label->imageXOffset, label->textXOffset); ++ ymin = MIN(label->imageYOffset, label->textYOffset); ++ xmax = MAX(label->imageXOffset + label->image.width, ++ label->textXOffset + w); ++ ymax = MAX(label->imageYOffset + label->image.height, ++ label->textYOffset + h); ++ } else { ++ xmin = label->textXOffset; ++ ymin = label->textYOffset; ++ xmax = label->textXOffset + w; ++ ymax = label->textYOffset + h; ++ } ++ ymin += *ypos; ++ ymax += *ypos; ++ *ypos = ymax; ++ ++ image = image_alloc(xmax - xmin, ymax - ymin, ++ COLOR_RGB_LIGHT_GOLDENROD); ++ if (!image) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ if (label->image.source != TEE_TUI_NO_SOURCE) { ++ res = set_label_image(image, label->imageXOffset, ++ label->imageYOffset, &label->image); ++ if (res != TEE_SUCCESS) ++ goto out; ++ } ++ ++ color = (label->textColor[0] << 16) | (label->textColor[1] << 8) | ++ label->textColor[2]; ++ if (!font_render_text(image, label->textYOffset - ymin, ++ label->textXOffset - xmin, label->text, color)) { ++ res = TEE_ERROR_BAD_FORMAT; ++ goto out; ++ } ++ ++ res = __tui_pta_set_screen_image(ymin, xmin, image); ++out: ++ image_free(image); ++ return res; ++} ++ ++static TEE_Result create_buttons(struct tui_widget_head *twh, size_t scr_width, ++ size_t scr_height, ++ TEE_TUIButton * const *buttons, ++ const bool req_buttons[TEE_TUI_NUMBER_BUTTON_TYPES]) ++{ ++ TEE_Result res; ++ size_t n; ++ uint32_t req = 0; ++ enum button_pos bpos[3]; ++ TEE_TUIButtonType btypes[3]; ++ size_t num_buttons; ++ ++ for (n = 0; n < TEE_TUI_NUMBER_BUTTON_TYPES; n++) ++ req |= req_buttons[n] << n; ++ ++ switch (req & ~(1 << TEE_TUI_CORRECTION)) { ++ case (1 << TEE_TUI_OK): ++ bpos[0] = POS_MIDDLE; ++ btypes[0] = TEE_TUI_OK; ++ num_buttons = 1; ++ break; ++ case (1 << TEE_TUI_OK) | (1 << TEE_TUI_PREVIOUS): ++ bpos[0] = POS_MIDDLE; ++ btypes[0] = TEE_TUI_OK; ++ bpos[1] = POS_LEFT; ++ btypes[1] = TEE_TUI_PREVIOUS; ++ num_buttons = 2; ++ break; ++ case (1 << TEE_TUI_CANCEL) | (1 << TEE_TUI_VALIDATE): ++ bpos[0] = POS_LEFT; ++ btypes[0] = TEE_TUI_CANCEL; ++ bpos[1] = POS_RIGHT; ++ btypes[1] = TEE_TUI_VALIDATE; ++ num_buttons = 2; ++ break; ++ case (1 << TEE_TUI_CANCEL) | (1 << TEE_TUI_NEXT): ++ bpos[0] = POS_LEFT; ++ btypes[0] = TEE_TUI_CANCEL; ++ bpos[1] = POS_RIGHT; ++ btypes[1] = TEE_TUI_NEXT; ++ num_buttons = 2; ++ break; ++ case (1 << TEE_TUI_CANCEL) | (1 << TEE_TUI_PREVIOUS) | ++ (1 << TEE_TUI_NEXT): ++ bpos[0] = POS_MIDDLE; ++ btypes[0] = TEE_TUI_CANCEL; ++ bpos[1] = POS_LEFT; ++ btypes[1] = TEE_TUI_PREVIOUS; ++ bpos[2] = POS_RIGHT; ++ btypes[2] = TEE_TUI_NEXT; ++ num_buttons = 3; ++ break; ++ case (1 << TEE_TUI_CANCEL) | (1 << TEE_TUI_VALIDATE) | ++ (1 << TEE_TUI_PREVIOUS): ++ bpos[0] = POS_MIDDLE; ++ btypes[0] = TEE_TUI_CANCEL; ++ bpos[1] = POS_LEFT; ++ btypes[1] = TEE_TUI_PREVIOUS; ++ bpos[2] = POS_RIGHT; ++ btypes[2] = TEE_TUI_VALIDATE; ++ num_buttons = 3; ++ break; ++ default: ++ return TEE_ERROR_NOT_SUPPORTED; ++ } ++ ++ for (n = 0; n < num_buttons; n++) { ++ res = __tui_button_create(twh, btypes[n], ++ button_info + btypes[n], buttons[n], ++ bpos[n], scr_width, scr_height); ++ if (res != TEE_SUCCESS) ++ return res; ++ } ++ return TEE_SUCCESS; ++} ++ ++static void twh_final(struct tui_widget_head *twh) ++{ ++ while (!TAILQ_EMPTY(twh)) { ++ struct tui_widget *tw = TAILQ_FIRST(twh); ++ ++ TAILQ_REMOVE(twh, tw, link); ++ tw->ops->destroy(tw); ++ } ++} ++ ++static bool display_should_exit(struct tui_widget_head *twh, ++ struct tui_state *ts) ++{ ++ if (!ts->clicked_button) ++ return false; ++ ++ if (*ts->clicked_button == TEE_TUI_VALIDATE) ++ return __tui_entry_fields_are_ok(twh); ++ ++ return true; ++} ++ ++static TEE_Result display_screen(struct tui_widget_head *twh, ++ TEE_TUIButtonType *selectedButton) ++{ ++ TEE_Result res; ++ bool is_key; ++ bool is_timedout; ++ uint32_t key; ++ size_t timeout; ++ size_t xpos; ++ size_t ypos; ++ struct tui_widget *tw; ++ struct tui_state ts = { NULL }; ++ ++ /* Find keyboard and save pointer */ ++ ts.keyboard = __tui_keyboard_find(twh); ++ ++ /* Find first input and give focus */ ++ tw = __tui_entry_field_find_first(twh); ++ if (tw) { ++ /* If there's no keyboard something is wrong */ ++ if (!ts.keyboard) ++ return TEE_ERROR_GENERIC; ++ tw->ops->focus(tw, &ts); ++ } ++ ++ /* ++ * If there's no focus yet, select the first widget what can have ++ * focus. ++ */ ++ TAILQ_FOREACH(tw, twh, link) { ++ if (!ts.focus) ++ tw->ops->focus(tw, &ts); ++ } ++ ++ /* If no widget has focus by now something is wrong */ ++ if (!ts.focus) ++ return TEE_ERROR_GENERIC; ++ ++ while (!display_should_exit(twh, &ts)) { ++ TAILQ_FOREACH(tw, twh, link) { ++ res = tw->ops->paint(tw, &ts); ++ if (res != TEE_SUCCESS) ++ return res; ++ } ++ ts.clicked_button = NULL; ++ if (__tui_entry_field_has_temp_clear(ts.input)) ++ timeout = 1000; ++ else ++ timeout = SIZE_MAX; ++ res = __tui_pta_display_screen(true, timeout, &is_timedout, ++ &is_key, &key, &xpos, &ypos); ++ if (res != TEE_SUCCESS) ++ return res; ++ ++ if (is_timedout) { ++ __tui_entry_field_reset_temp_clear(ts.input); ++ continue; ++ } ++ ++ if (!is_key) { ++ tw = __tui_widget_find(twh, xpos, ypos); ++ if (!tw) ++ continue; ++ ++ EMSG("(%zu,%zu) clicked widget at (%zu,%zu)(%zu,%zu)", ++ xpos, ypos, tw->x, tw->y, tw->width, tw->height); ++ tw->ops->click(tw, &ts, xpos, ypos); ++ continue; ++ } ++ ++ switch (key) { ++ case '\t': ++ tw = ts.focus; ++ tw->ops->unfocus(tw, &ts); ++ while (!ts.focus) { ++ tw = TAILQ_NEXT(tw, link); ++ if (!tw) ++ tw = TAILQ_FIRST(twh); ++ tw->ops->focus(tw, &ts); ++ } ++ break; ++ case '\n': ++ case '\r': ++ ts.focus->ops->click(ts.focus, &ts, -1, -1); ++ break; ++ default: ++ ts.focus->ops->input(ts.focus, &ts, key); ++ break; ++ } ++ } ++ if (!ts.clicked_button) ++ return TEE_ERROR_GENERIC; ++ *selectedButton = *ts.clicked_button; ++ return TEE_SUCCESS; ++} ++ ++TEE_Result TEE_TUIDisplayScreen(const TEE_TUIScreenConfiguration *sc, ++ bool closeTUISession, ++ TEE_TUIEntryField *entryFields, ++ uint32_t entryFieldCount, ++ TEE_TUIButtonType *selectedButton) ++{ ++ TEE_Result res; ++ size_t ypos = 0; ++ struct tui_widget_head twh = TAILQ_HEAD_INITIALIZER(twh); ++ size_t scr_height; ++ size_t scr_width; ++ const TEE_Result results[] = { ++ TEE_ERROR_OUT_OF_MEMORY, TEE_ERROR_ITEM_NOT_FOUND, ++ TEE_ERROR_ACCESS_CONFLICT, TEE_ERROR_BAD_FORMAT, ++ TEE_ERROR_BAD_STATE, TEE_ERROR_BUSY, TEE_ERROR_CANCEL, ++ TEE_ERROR_EXTERNAL_CANCEL, TEE_SUCCESS, ++ }; ++ ++ if (sc->screenOrientation != TEE_TUI_LANDSCAPE) ++ TEE_Panic(TEE_ERROR_NOT_SUPPORTED); ++ ++ res = __tui_pta_get_screen_info(&scr_width, &scr_height, ++ NULL, NULL, NULL); ++ CHECK_RESULT(res, results); ++ if (res != TEE_SUCCESS) ++ return res; ++ ++ res = __tui_pta_init_screen(COLOR_RGB_LIGHT_GRAY); ++ CHECK_RESULT(res, results); ++ if (res != TEE_SUCCESS) ++ goto out; ++ ++ res = set_label(&sc->label, &ypos); ++ CHECK_RESULT(res, results); ++ if (res != TEE_SUCCESS) ++ goto out; ++ ++ if (entryFieldCount) { ++ res = __tui_entry_fields_create(&twh, &ypos, scr_width, ++ button_info + ++ TEE_TUI_CORRECTION, ++ entryFields, entryFieldCount); ++ CHECK_RESULT(res, results); ++ if (res != TEE_SUCCESS) ++ goto out; ++ ++ res = __tui_keyboard_create(&twh, &ypos, scr_width); ++ CHECK_RESULT(res, results); ++ if (res != TEE_SUCCESS) ++ goto out; ++ } ++ ++ res = create_buttons(&twh, scr_width, scr_height, sc->buttons, ++ sc->requestedButtons); ++ CHECK_RESULT(res, results); ++ if (res != TEE_SUCCESS) ++ goto out; ++ ++ res = display_screen(&twh, selectedButton); ++ ++out: ++ twh_final(&twh); ++ if (closeTUISession) ++ TEE_TUICloseSession(); ++ return res; ++} +diff --git a/lib/libutee/tui/tui_entry.c b/lib/libutee/tui/tui_entry.c +new file mode 100644 +index 000000000..09a3abb7f +--- /dev/null ++++ b/lib/libutee/tui/tui_entry.c +@@ -0,0 +1,502 @@ ++// SPDX-License-Identifier: BSD-2-Clause ++/* ++ * Copyright (c) 2016-2021, Linaro Limited ++ */ ++ ++#include ++#include ++#include ++#include ++#include "font.h" ++#include "image.h" ++#include "tui_private.h" ++ ++struct tui_correction { ++ struct tui_widget *input; ++ TEE_TUIScreenButtonInfo *bi; ++ struct tui_widget tw; ++}; ++ ++struct tui_input { ++ TEE_TUIEntryField *field; ++ struct tui_keyboard *keyboard; ++ size_t min_len; ++ size_t max_len; ++ bool temp_clear; ++ struct tui_widget tw; ++}; ++ ++struct field_geometry { ++ size_t input_width; ++ size_t input_height; ++ size_t input_x; ++ size_t input_yoffs; ++ size_t input_max_len; ++ size_t corr_width; ++ size_t corr_height; ++ size_t corr_x; ++ size_t corr_yoffs; ++ size_t max_height; ++}; ++ ++static bool __maybe_unused is_tui_correction(struct tui_widget *tw); ++static bool is_tui_input(struct tui_widget *tw); ++ ++static struct tui_correction *to_tui_correction(struct tui_widget *tw) ++{ ++ assert(is_tui_correction(tw)); ++ return container_of(tw, struct tui_correction, tw); ++} ++ ++static struct tui_input *to_tui_input(struct tui_widget *tw) ++{ ++ assert(is_tui_input(tw)); ++ return container_of(tw, struct tui_input, tw); ++} ++ ++static void state_set_input(struct tui_state *ts, struct tui_widget *tw) ++{ ++ if (ts->input != tw) { ++ ts->input = tw; ++ __tui_keyboard_select_layout(ts); ++ } ++} ++ ++static TEE_Result op_correction_paint(struct tui_widget *tw, ++ struct tui_state *ts) ++{ ++ TEE_Result res; ++ struct image *image; ++ struct tui_correction *tc = to_tui_correction(tw); ++ char *text = tc->bi->buttonText; ++ ++ if (!tw->updated) ++ return TEE_SUCCESS; ++ ++ image = __tui_widget_image_alloc(tw, COLOR_RGB_LIGHT_GOLDENROD); ++ if (!image) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ if (!font_render_text(image, 0, 0, text, COLOR_RGB_BLACK)) { ++ res = TEE_ERROR_BAD_FORMAT; ++ goto out; ++ } ++ res = __tui_widget_display(tw, ts, image); ++ if (res == TEE_SUCCESS) ++ tw->updated = false; ++out: ++ image_free(image); ++ return res; ++} ++ ++static void op_correction_focus(struct tui_widget *tw, ++ struct tui_state *ts) ++{ ++ struct tui_correction *tc = to_tui_correction(tw); ++ ++ if (tw != ts->focus) { ++ tw->updated = true; ++ ts->focus = tw; ++ state_set_input(ts, tc->input); ++ } ++} ++ ++static void op_correction_click(struct tui_widget *tw, ++ struct tui_state *ts, ssize_t x __unused, ++ ssize_t y __unused) ++{ ++ if (tw != ts->focus) { ++ ts->focus->ops->unfocus(ts->focus, ts); ++ tw->ops->focus(tw, ts); ++ } ++ ++ ts->input->ops->input(ts->input, ts, 0x08); ++} ++ ++static void op_correction_destroy(struct tui_widget *tw) ++{ ++ free(to_tui_correction(tw)); ++} ++ ++static const struct tui_widget_ops correction_ops = { ++ .paint = op_correction_paint, ++ .focus = op_correction_focus, ++ .unfocus = __tui_widget_op_unfocus, ++ .click = op_correction_click, ++ .input = __tui_widget_op_dummy_input, ++ .destroy = op_correction_destroy, ++}; ++ ++static bool is_tui_correction(struct tui_widget *tw) ++{ ++ return tw->ops == &correction_ops; ++} ++ ++static struct tui_correction *add_entry_correction(struct tui_widget_head *twh, ++ struct field_geometry *fg, ++ size_t ypos, ++ TEE_TUIScreenButtonInfo *bi, ++ struct tui_widget *input) ++{ ++ struct tui_correction *twc = calloc(1, sizeof(*twc)); ++ ++ if (!twc) ++ return NULL; ++ ++ twc->tw.ops = &correction_ops; ++ twc->tw.updated = true; ++ twc->bi = bi; ++ twc->input = input; ++ twc->tw.y = ypos + fg->corr_yoffs; ++ twc->tw.x = fg->corr_x; ++ twc->tw.width = fg->corr_width; ++ twc->tw.height = fg->corr_height; ++ __tui_widget_add(twh, &twc->tw); ++ return twc; ++} ++ ++static TEE_Result op_input_paint(struct tui_widget *tw, struct tui_state *ts) ++{ ++ TEE_Result res; ++ struct tui_input *ti = to_tui_input(tw); ++ char text[MAX_ENTRY_FIELD_LENGTH + 1]; ++ size_t l = ti->field->bufferLength; ++ struct image *image; ++ ++ if (!tw->updated) ++ return TEE_SUCCESS; ++ ++ image = __tui_widget_image_alloc(tw, COLOR_RGB_LIGHT_GOLDENROD); ++ if (!image) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ if (!l) ++ goto display; ++ ++ switch (ti->field->mode) { ++ case TEE_TUI_TEMPORARY_CLEAR_MODE: ++ if (ti->temp_clear) { ++ memset(text, '*', l - 1); ++ text[l - 1] = ti->field->buffer[l - 1]; ++ break; ++ } ++ ++ /*FALLTHROUGH*/ ++ case TEE_TUI_HIDDEN_MODE: ++ memset(text, '*', l); ++ break; ++ case TEE_TUI_CLEAR_MODE: ++ memcpy(text, ti->field->buffer, l); ++ break; ++ default: ++ res = TEE_ERROR_GENERIC; ++ goto out; ++ } ++ text[l] = '\0'; ++ ++ if (!font_render_text(image, 0, 0, text, COLOR_RGB_BLACK)) { ++ res = TEE_ERROR_BAD_FORMAT; ++ goto out; ++ } ++display: ++ res = __tui_widget_display(tw, ts, image); ++ if (res == TEE_SUCCESS) ++ tw->updated = false; ++out: ++ image_free(image); ++ return res; ++} ++ ++static void op_input_focus(struct tui_widget *tw, struct tui_state *ts) ++{ ++ if (tw != ts->focus) { ++ tw->updated = true; ++ ts->focus = tw; ++ state_set_input(ts, tw); ++ } ++} ++ ++static void op_input_click(struct tui_widget *tw, struct tui_state *ts, ++ ssize_t x __unused, ssize_t y __unused) ++{ ++ if (tw != ts->focus) { ++ ts->focus->ops->unfocus(ts->focus, ts); ++ tw->ops->focus(tw, ts); ++ } ++} ++ ++static bool op_input_input(struct tui_widget *tw, ++ struct tui_state *ts __unused, uint32_t key) ++{ ++ struct tui_input *ti = to_tui_input(tw); ++ ++ if ((key == 0x7f || key == 0x08)) { ++ if (ti->field->bufferLength) { ++ ti->field->bufferLength--; ++ ti->field->buffer[ti->field->bufferLength] = '\0'; ++ tw->updated = true; ++ return true; ++ } ++ ++ EMSG("Input buffer empty, ignoring key %#" PRIX32, key); ++ return false; ++ } ++ ++ if (ti->field->bufferLength >= ti->max_len) { ++ EMSG("Input buffer full, ignoring key %#" PRIX32, key); ++ return false; ++ } ++ ++ if ((ti->field->type == TEE_TUI_ALPHANUMERICAL && ++ key >= 0x20 && key <= 0x7d) || ++ (ti->field->type == TEE_TUI_NUMERICAL && ++ key >= '0' && key <= '9')) { ++ ti->field->buffer[ti->field->bufferLength] = key; ++ ti->field->bufferLength++; ++ ti->temp_clear = true; ++ tw->updated = true; ++ return true; ++ } ++ ++ EMSG("Ignoring invalid key %#" PRIX32, key); ++ return false; ++} ++ ++static void op_input_destroy(struct tui_widget *tw) ++{ ++ free(to_tui_input(tw)); ++} ++ ++TEE_TUIEntryFieldType __tui_entry_field_get_input_type(struct tui_widget *tw) ++{ ++ return to_tui_input(tw)->field->type; ++} ++ ++static const struct tui_widget_ops input_ops = { ++ .paint = op_input_paint, ++ .focus = op_input_focus, ++ .unfocus = __tui_widget_op_unfocus, ++ .click = op_input_click, ++ .input = op_input_input, ++ .destroy = op_input_destroy, ++}; ++ ++static bool is_tui_input(struct tui_widget *tw) ++{ ++ return tw->ops == &input_ops; ++} ++ ++static struct tui_input *add_entry_input(struct tui_widget_head *twh, ++ struct field_geometry *fg, size_t ypos, ++ TEE_TUIEntryField *ef) ++{ ++ struct tui_input *twi = calloc(1, sizeof(*twi)); ++ ++ if (!twi) ++ return NULL; ++ ++ twi->tw.ops = &input_ops; ++ twi->tw.updated = true; ++ twi->field = ef; ++ twi->min_len = ef->minExpectedLength; ++ if (ef->maxExpectedLength) ++ twi->max_len = ef->maxExpectedLength; ++ else ++ twi->max_len = fg->input_max_len; ++ twi->tw.y = ypos + fg->input_yoffs; ++ twi->tw.x = fg->input_x; ++ twi->tw.width = fg->input_width; ++ twi->tw.height = fg->input_height; ++ __tui_widget_add(twh, &twi->tw); ++ return twi; ++} ++ ++static TEE_Result get_entry_field_size(TEE_TUIEntryField *efs, size_t num_efs, ++ TEE_TUIScreenButtonInfo *cbi, ++ size_t scr_width, ++ struct field_geometry *fg) ++{ ++ size_t max_label_width = 0; ++ size_t max_len = 0; ++ size_t max_width; ++ size_t n; ++ size_t l; ++ ++ /* ++ * An entry field looks like: ++ * +------------------------------------------------------------+ ++ * | Label text ... | ++ * +------------------------------------+-----------------------+ ++ * | Input field | Correction button | ++ * +------------------------------------+-----------------------+ ++ * Each of the boxes in the pictures can have different heights and ++ * width. The correction button should be on the same level (tw.y) ++ * as the input field, this is accomplished by setting the center ++ * line at the same y level. ++ * ++ * If there's multiple entry fields they should have the same width ++ * to line up nicely, this is done by scanning the required maximum ++ * width of each box in entry fields and pick that larges value for ++ * each. ++ */ ++ ++ fg->corr_width = cbi->buttonWidth; ++ fg->corr_height = cbi->buttonHeight; ++ ++ fg->input_max_len = font_get_max_field_length(scr_width - ++ fg->corr_width - ++ 4 * BORDER_SIZE); ++ for (n = 0; n < num_efs; n++) { ++ if (efs[n].maxExpectedLength > fg->input_max_len) ++ return TEE_ERROR_GENERIC; ++ l = efs[n].maxExpectedLength; ++ if (l > max_len) ++ max_len = l; ++ ++ if (efs[n].label) { ++ if (!font_check_text_format(efs[n].label, &l, ++ NULL, NULL)) ++ return TEE_ERROR_GENERIC; ++ if (l > max_label_width) ++ max_label_width = l; ++ } ++ } ++ ++ fg->input_width = font_get_max_field_width(max_len) + BORDER_SIZE * 2; ++ fg->input_height = font_get_text_height() + BORDER_SIZE * 2; ++ ++ max_width = MAX(fg->input_width + fg->corr_width, max_label_width); ++ fg->max_height = MAX(fg->input_height, fg->corr_height); ++ ++ if (max_width > scr_width) ++ return TEE_ERROR_GENERIC; ++ ++ fg->input_x = (scr_width / 2) - (max_width / 2); ++ fg->corr_x = fg->input_x + fg->input_width; ++ ++ fg->input_yoffs = (fg->max_height / 2) - (fg->input_height / 2); ++ fg->corr_yoffs = (fg->max_height / 2) - (fg->corr_height / 2); ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result set_entry_label(size_t xpos, size_t *ypos, const char *text) ++{ ++ TEE_Result res; ++ struct image *image; ++ size_t w; ++ size_t h; ++ ++ if (!text) ++ return TEE_SUCCESS; ++ ++ if (!font_check_text_format(text, &w, &h, NULL)) ++ return TEE_ERROR_GENERIC; ++ ++ image = image_alloc(w, h, COLOR_RGB_YELLOW); ++ if (!image) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ if (!font_render_text(image, 0, 0, text, COLOR_RGB_BLACK)) { ++ res = TEE_ERROR_GENERIC; ++ goto out; ++ } ++ ++ res = __tui_pta_set_screen_image(xpos, *ypos, image); ++ if (res != TEE_SUCCESS) ++ goto out; ++ ++ (*ypos) += h; ++out: ++ image_free(image); ++ return res; ++} ++ ++TEE_Result __tui_entry_fields_create(struct tui_widget_head *twh, ++ size_t *ypos, size_t scr_width, ++ TEE_TUIScreenButtonInfo *cbi, ++ TEE_TUIEntryField *efs, size_t num_efs) ++{ ++ TEE_Result res; ++ struct field_geometry fg; ++ size_t yp = *ypos; ++ size_t n; ++ ++ for (n = 0; n < num_efs; n++) { ++ if (!efs[n].maxExpectedLength) ++ efs[n].maxExpectedLength = efs[n].bufferLength; ++ efs[n].bufferLength = 0; ++ } ++ ++ res = get_entry_field_size(efs, num_efs, cbi, scr_width, ++ &fg); ++ if (res != TEE_SUCCESS) ++ return res; ++ ++ for (n = 0; n < num_efs; n++) { ++ struct tui_input *twi; ++ ++ res = set_entry_label(fg.input_x, &yp, efs[n].label); ++ if (res != TEE_SUCCESS) ++ return res; ++ ++ twi = add_entry_input(twh, &fg, yp, efs + n); ++ if (!twi) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ if (!add_entry_correction(twh, &fg, yp, cbi, &twi->tw)) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ yp += fg.max_height; ++ } ++ ++ *ypos = yp; ++ ++ return TEE_SUCCESS; ++} ++ ++bool __tui_entry_fields_are_ok(struct tui_widget_head *twh) ++{ ++ struct tui_widget *tw; ++ ++ TAILQ_FOREACH(tw, twh, link) { ++ if (is_tui_input(tw)) { ++ struct tui_input *ti = to_tui_input(tw); ++ ++ if (ti->field->bufferLength < ti->min_len) ++ return false; ++ } ++ } ++ ++ return true; ++} ++ ++bool __tui_entry_field_has_temp_clear(struct tui_widget *tw) ++{ ++ if (tw) { ++ struct tui_input *ti = to_tui_input(tw); ++ ++ return ti->temp_clear; ++ } ++ ++ return false; ++} ++ ++void __tui_entry_field_reset_temp_clear(struct tui_widget *tw) ++{ ++ if (tw) { ++ struct tui_input *ti = to_tui_input(tw); ++ ++ ti->temp_clear = false; ++ tw->updated = true; ++ } ++} ++ ++struct tui_widget *__tui_entry_field_find_first(struct tui_widget_head *twh) ++{ ++ struct tui_widget *tw; ++ ++ TAILQ_FOREACH(tw, twh, link) ++ if (is_tui_input(tw)) ++ return tw; ++ return NULL; ++} +diff --git a/lib/libutee/tui/tui_keyboard.c b/lib/libutee/tui/tui_keyboard.c +new file mode 100644 +index 000000000..563d64f91 +--- /dev/null ++++ b/lib/libutee/tui/tui_keyboard.c +@@ -0,0 +1,517 @@ ++// SPDX-License-Identifier: BSD-2-Clause ++/* ++ * Copyright (c) 2016-2021, Linaro Limited ++ */ ++ ++#include ++#include ++#include ++#include "font.h" ++#include "image.h" ++#include "tui_private.h" ++ ++struct tui_key { ++ char value; ++ const char *text; ++ struct tui_widget tw; ++}; ++ ++struct tui_switchkey { ++ const char *text; ++ size_t layout_idx; ++ struct tui_widget tw; ++}; ++ ++struct tui_keyboard { ++ size_t layout_idx; ++ size_t num_layouts; ++ struct keyboard_head *kbh; ++ struct tui_widget tw; ++}; ++ ++struct keyboard_head { ++ TEE_TUIEntryFieldType type; ++ struct tui_widget_head twh; ++}; ++ ++struct keyboard_layout { ++ TEE_TUIEntryFieldType type; ++ const char *values; ++ size_t columns; ++ size_t rows; ++ const char *switch_str; ++ size_t switch_idx; ++}; ++ ++static const struct keyboard_layout keyboard_layouts[] = { ++ { ++ .type = TEE_TUI_ALPHANUMERICAL, ++ .values = (const char[]) { ++ '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', ++ 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', ++ 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 0, ++ 'z', 'x', 'c', 'v', 'b', 'n', 'm', 0, 0, 0, ++ }, ++ .columns = 10, ++ .rows = 4, ++ .switch_str = "", ++ .switch_idx = 1, ++ }, ++ { ++ .type = TEE_TUI_ALPHANUMERICAL, ++ .values = (const char[]) { ++ '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', ++ 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', ++ 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 0, ++ 'Z', 'X', 'C', 'V', 'B', 'N', 'M', 0, 0, 0, ++ }, ++ .columns = 10, ++ .rows = 4, ++ .switch_str = "", ++ .switch_idx = 2, ++ }, ++ { ++ .type = TEE_TUI_ALPHANUMERICAL, ++ .values = (const char[]) { ++ '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 0, ++ '!', '@', '#', '$', '%', '^', '&', '*', '-', '+', '=', ++ ',', '.', '?', ';', ':', '\'', '"', '`', '/', '\\', '_', ++ '(', ')', '<', '>', '[', ']', '{', '}', '|', 0, 0, ++ }, ++ .columns = 11, ++ .rows = 4, ++ .switch_idx = 0, ++ .switch_str = "", ++ }, ++ { ++ .type = TEE_TUI_NUMERICAL, ++ .values = (const char []) { ++ '1', '2', '3', ++ '4', '5', '6', ++ '7', '8', '9', ++ 0, '0', 0, ++ }, ++ .columns = 3, ++ .rows = 4, ++ }, ++}; ++ ++static void op_dummy_unfocus(struct tui_widget *tw __unused, ++ struct tui_state *ts __unused) ++{ ++ /* Do nothing, this widget can't be in focus */ ++} ++ ++static void op_dummy_focus(struct tui_widget *tw __unused, ++ struct tui_state *ts __unused) ++{ ++ /* Do nothing, this widget can't be in focus */ ++} ++ ++static bool __maybe_unused is_tui_key(struct tui_widget *tw); ++static struct tui_key *to_tui_key(struct tui_widget *tw) ++{ ++ assert(is_tui_key(tw)); ++ return container_of(tw, struct tui_key, tw); ++} ++ ++static bool __maybe_unused is_tui_switchkey(struct tui_widget *tw); ++static struct tui_switchkey *to_tui_switchkey(struct tui_widget *tw) ++{ ++ assert(is_tui_switchkey(tw)); ++ return container_of(tw, struct tui_switchkey, tw); ++} ++ ++static bool is_tui_keyboard(struct tui_widget *tw); ++static struct tui_keyboard *to_tui_keyboard(struct tui_widget *tw) ++{ ++ assert(is_tui_keyboard(tw)); ++ return container_of(tw, struct tui_keyboard, tw); ++} ++ ++static TEE_Result op_key_paint(struct tui_widget *tw, struct tui_state *ts) ++{ ++ TEE_Result res; ++ struct tui_key *k = to_tui_key(tw); ++ struct image *image; ++ char text[2] = { k->value, '\0' }; ++ const char *t; ++ ++ if (k->text) ++ t = k->text; ++ else ++ t = text; ++ ++ if (!tw->updated) ++ return TEE_SUCCESS; ++ ++ image = __tui_widget_image_alloc(tw, COLOR_RGB_LIGHT_GOLDENROD); ++ if (!image) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ if (!font_render_text(image, 0, 0, t, COLOR_RGB_BLACK)) { ++ res = TEE_ERROR_BAD_FORMAT; ++ goto out; ++ } ++ res = __tui_widget_display(tw, ts, image); ++ if (res == TEE_SUCCESS) ++ tw->updated = false; ++out: ++ image_free(image); ++ return res; ++} ++ ++static void op_key_click(struct tui_widget *tw, struct tui_state *ts, ++ ssize_t x __unused, ssize_t y __unused) ++{ ++ struct tui_key *k = to_tui_key(tw); ++ ++ ts->input->ops->input(ts->input, ts, k->value); ++} ++ ++static void op_key_destroy(struct tui_widget *tw) ++{ ++ free(to_tui_key(tw)); ++} ++ ++static const struct tui_widget_ops key_ops = { ++ .paint = op_key_paint, ++ .focus = op_dummy_focus, ++ .unfocus = op_dummy_unfocus, ++ .click = op_key_click, ++ .input = __tui_widget_op_dummy_input, ++ .destroy = op_key_destroy, ++}; ++ ++static bool is_tui_key(struct tui_widget *tw) ++{ ++ return tw->ops == &key_ops; ++} ++ ++static TEE_Result add_key(struct tui_widget_head *twh, char value, ++ const char *text, size_t x, size_t y, ++ size_t w, size_t h) ++{ ++ struct tui_key *tk = calloc(1, sizeof(*tk)); ++ ++ if (!tk) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ tk->tw.ops = &key_ops; ++ tk->tw.updated = true; ++ tk->tw.x = x; ++ tk->tw.y = y; ++ tk->tw.width = w; ++ tk->tw.height = h; ++ tk->value = value; ++ tk->text = text; ++ TAILQ_INSERT_TAIL(twh, &tk->tw, link); ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result op_switchkey_paint(struct tui_widget *tw, ++ struct tui_state *ts) ++{ ++ TEE_Result res; ++ struct tui_switchkey *k = to_tui_switchkey(tw); ++ struct image *image; ++ ++ if (!tw->updated) ++ return TEE_SUCCESS; ++ ++ image = __tui_widget_image_alloc(tw, COLOR_RGB_LIGHT_GOLDENROD); ++ if (!image) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ if (!font_render_text(image, 0, 0, k->text, COLOR_RGB_BLACK)) { ++ res = TEE_ERROR_BAD_FORMAT; ++ goto out; ++ } ++ res = __tui_widget_display(tw, ts, image); ++ if (res == TEE_SUCCESS) ++ tw->updated = false; ++out: ++ image_free(image); ++ return res; ++} ++ ++static void op_switchkey_click(struct tui_widget *tw, struct tui_state *ts, ++ ssize_t x __unused, ssize_t y __unused) ++{ ++ struct tui_switchkey *k = to_tui_switchkey(tw); ++ struct tui_keyboard *kb = to_tui_keyboard(ts->keyboard); ++ ++ kb->layout_idx = k->layout_idx; ++ ts->keyboard->updated = true; ++} ++ ++static void op_switchkey_destroy(struct tui_widget *tw) ++{ ++ free(to_tui_switchkey(tw)); ++} ++ ++static const struct tui_widget_ops switchkey_ops = { ++ .paint = op_switchkey_paint, ++ .focus = op_dummy_focus, ++ .unfocus = op_dummy_unfocus, ++ .click = op_switchkey_click, ++ .input = __tui_widget_op_dummy_input, ++ .destroy = op_switchkey_destroy, ++}; ++ ++static bool is_tui_switchkey(struct tui_widget *tw) ++{ ++ return tw->ops == &switchkey_ops; ++} ++ ++static TEE_Result add_switchkey(struct tui_widget_head *twh, ++ const char *text, size_t layout_idx, ++ size_t x, size_t y) ++{ ++ struct tui_switchkey *k; ++ size_t w; ++ size_t h; ++ ++ if (!font_check_text_format(text, &w, &h, NULL)) ++ return TEE_ERROR_GENERIC; ++ ++ k = calloc(1, sizeof(*k)); ++ if (!k) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ k->tw.ops = &switchkey_ops; ++ k->tw.updated = true; ++ k->tw.x = x; ++ k->tw.y = y; ++ k->tw.width = w; ++ k->tw.height = h; ++ k->text = text; ++ k->layout_idx = layout_idx; ++ TAILQ_INSERT_TAIL(twh, &k->tw, link); ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result op_keyboard_paint(struct tui_widget *tw, struct tui_state *ts) ++{ ++ TEE_Result res; ++ struct tui_keyboard *tkb = to_tui_keyboard(tw); ++ struct tui_widget *tw_key; ++ struct image *image; ++ ++ if (!tw->updated) ++ return TEE_SUCCESS; ++ ++ image = __tui_widget_image_alloc(tw, COLOR_RGB_LIGHT_GRAY); ++ if (!image) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ res = __tui_widget_display(tw, ts, image); ++ image_free(image); ++ if (res != TEE_SUCCESS) ++ return res; ++ ++ TAILQ_FOREACH(tw_key, &tkb->kbh[tkb->layout_idx].twh, link) { ++ tw_key->updated = true; ++ res = tw_key->ops->paint(tw_key, ts); ++ if (res != TEE_SUCCESS) ++ return res; ++ } ++ tw->updated = false; ++ return TEE_SUCCESS; ++} ++ ++static void op_keyboard_click(struct tui_widget *tw, struct tui_state *ts, ++ ssize_t x, ssize_t y) ++{ ++ struct tui_keyboard *tkb = to_tui_keyboard(tw); ++ struct tui_widget *tw_key; ++ ++ tw_key = __tui_widget_find(&tkb->kbh[tkb->layout_idx].twh, x, y); ++ if (tw_key) ++ tw_key->ops->click(tw_key, ts, x, y); ++} ++ ++static void op_keyboard_destroy(struct tui_widget *tw) ++{ ++ struct tui_keyboard *tkb = to_tui_keyboard(tw); ++ struct tui_widget *tw_key; ++ size_t n; ++ ++ for (n = 0; n < tkb->num_layouts; n++) { ++ while (true) { ++ tw_key = TAILQ_FIRST(&tkb->kbh[n].twh); ++ if (!tw_key) ++ break; ++ TAILQ_REMOVE(&tkb->kbh[n].twh, tw_key, link); ++ tw_key->ops->destroy(tw_key); ++ } ++ } ++ free(tkb->kbh); ++ free(tkb); ++} ++ ++static const struct tui_widget_ops keyboard_ops = { ++ .paint = op_keyboard_paint, ++ .focus = op_dummy_focus, ++ .unfocus = op_dummy_unfocus, ++ .click = op_keyboard_click, ++ .input = __tui_widget_op_dummy_input, ++ .destroy = op_keyboard_destroy, ++}; ++ ++static bool is_tui_keyboard(struct tui_widget *tw) ++{ ++ return tw->ops == &keyboard_ops; ++} ++ ++TEE_Result __tui_keyboard_create(struct tui_widget_head *twh, size_t *ypos, ++ size_t scr_width) ++{ ++ TEE_Result res; ++ struct tui_keyboard *tkb; ++ size_t row; ++ size_t col; ++ size_t hk_max = 0; ++ size_t wk_max = 0; ++ size_t hmax = 0; ++ size_t wmax = 0; ++ size_t h; ++ size_t w; ++ size_t n; ++ ++ for (n = 0; n < ARRAY_SIZE(keyboard_layouts); n++) { ++ for (row = 0; row < keyboard_layouts[n].rows; row++) { ++ for (col = 0; col < keyboard_layouts[n].columns; ++ col++) { ++ char text[2] = { ++ keyboard_layouts[n].values[row * ++ keyboard_layouts[n].columns + ++ col], ++ '\0', ++ }; ++ ++ if (!text[0]) ++ continue; ++ if (!font_check_text_format(text, &w, &h, NULL)) ++ return TEE_ERROR_GENERIC; ++ if (w > wk_max) ++ wk_max = w; ++ if (h > hk_max) ++ hk_max = h; ++ } ++ } ++ } ++ ++ for (n = 0; n < ARRAY_SIZE(keyboard_layouts); n++) { ++ size_t num_rows = keyboard_layouts[n].rows; ++ ++ if (keyboard_layouts[n].type == TEE_TUI_ALPHANUMERICAL) ++ num_rows++; ++ hmax = MAX(num_rows * hk_max, hmax); ++ wmax = MAX(keyboard_layouts[n].columns * wk_max, wmax); ++ } ++ ++ tkb = calloc(1, sizeof(*tkb)); ++ if (!tkb) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ tkb->kbh = calloc(ARRAY_SIZE(keyboard_layouts), sizeof(*tkb->kbh)); ++ if (!tkb->kbh) { ++ res = TEE_ERROR_OUT_OF_MEMORY; ++ goto out; ++ } ++ ++ for (n = 0; n < ARRAY_SIZE(keyboard_layouts); n++) ++ TAILQ_INIT(&tkb->kbh[n].twh); ++ ++ tkb->tw.ops = &keyboard_ops; ++ tkb->tw.updated = true; ++ tkb->tw.width = wmax; ++ tkb->tw.height = hmax; ++ tkb->num_layouts = ARRAY_SIZE(keyboard_layouts); ++ ++ if (tkb->tw.width > scr_width) { ++ res = TEE_ERROR_GENERIC; ++ goto out; ++ } ++ tkb->tw.x = (scr_width - tkb->tw.width) / 2; ++ tkb->tw.y = *ypos; ++ ++ for (n = 0; n < ARRAY_SIZE(keyboard_layouts); n++) { ++ size_t xoffs; ++ ++ xoffs = (wmax - (keyboard_layouts[n].columns * wk_max)) / 2; ++ tkb->kbh[n].type = keyboard_layouts[n].type; ++ ++ for (row = 0; row < keyboard_layouts[n].rows; row++) { ++ for (col = 0; col < keyboard_layouts[n].columns; ++ col++) { ++ char value = keyboard_layouts[n].values[row * ++ keyboard_layouts[n].columns + ++ col]; ++ if (!value) ++ continue; ++ res = add_key(&tkb->kbh[n].twh, value, NULL, ++ tkb->tw.x + col * wk_max + xoffs, ++ tkb->tw.y + row * hk_max, ++ wk_max, hk_max); ++ if (res != TEE_SUCCESS) ++ goto out; ++ } ++ } ++ ++ if (keyboard_layouts[n].type == TEE_TUI_ALPHANUMERICAL) { ++ const char *str = ""; ++ size_t switch_idx = keyboard_layouts[n].switch_idx; ++ ++ res = add_switchkey(&tkb->kbh[n].twh, ++ keyboard_layouts[switch_idx].switch_str, ++ switch_idx, tkb->tw.x + xoffs, ++ tkb->tw.y + ++ keyboard_layouts[n].rows * hk_max); ++ if (res != TEE_SUCCESS) ++ goto out; ++ ++ if (!font_check_text_format(str, &w, NULL, NULL)) { ++ res = TEE_ERROR_GENERIC; ++ goto out; ++ } ++ ++ res = add_key(&tkb->kbh[n].twh, ' ', str, ++ tkb->tw.x + 5 * wk_max + xoffs, ++ tkb->tw.y + ++ keyboard_layouts[n].rows * hk_max, ++ w, hk_max); ++ if (res != TEE_SUCCESS) ++ goto out; ++ } ++ } ++ ++ TAILQ_INSERT_TAIL(twh, &tkb->tw, link); ++out: ++ if (res != TEE_SUCCESS) ++ op_keyboard_destroy(&tkb->tw); ++ return res; ++} ++ ++void __tui_keyboard_select_layout(struct tui_state *ts) ++{ ++ struct tui_keyboard *kb = to_tui_keyboard(ts->keyboard); ++ const TEE_TUIEntryFieldType type = ++ __tui_entry_field_get_input_type(ts->input); ++ ++ if (type == kb->kbh[kb->layout_idx].type) ++ return; ++ kb->layout_idx = 0; ++ kb->tw.updated = true; ++ ++ while (type != kb->kbh[kb->layout_idx].type) ++ kb->layout_idx = (kb->layout_idx + 1) % kb->num_layouts; ++} ++ ++struct tui_widget *__tui_keyboard_find(struct tui_widget_head *twh) ++{ ++ struct tui_widget *tw; ++ ++ TAILQ_FOREACH(tw, twh, link) ++ if (is_tui_keyboard(tw)) ++ return tw; ++ return NULL; ++} +diff --git a/lib/libutee/tui/tui_private.h b/lib/libutee/tui/tui_private.h +new file mode 100644 +index 000000000..2b5a0d3b5 +--- /dev/null ++++ b/lib/libutee/tui/tui_private.h +@@ -0,0 +1,107 @@ ++/* SPDX-License-Identifier: BSD-2-Clause */ ++/* ++ * Copyright (c) 2016-2021, Linaro Limited ++ */ ++ ++#ifndef __TUI_PRIVATE_H ++#define __TUI_PRIVATE_H ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include "image.h" ++ ++#define MAX_ENTRY_FIELDS 2 ++#define MAX_ENTRY_FIELD_LENGTH 32 ++#define BORDER_SIZE 2 ++ ++enum button_pos { ++ POS_LEFT, ++ POS_MIDDLE, ++ POS_RIGHT, ++}; ++ ++struct tui_state { ++ TEE_TUIButtonType *clicked_button; ++ struct tui_widget *focus; ++ struct tui_widget *input; ++ struct tui_widget *keyboard; ++}; ++ ++struct tui_widget_ops { ++ TEE_Result (*paint)(struct tui_widget *tw, struct tui_state *ts); ++ void (*focus)(struct tui_widget *tw, struct tui_state *ts); ++ void (*unfocus)(struct tui_widget *tw, struct tui_state *ts); ++ void (*click)(struct tui_widget *tw, struct tui_state *ts, ++ ssize_t x, ssize_t y); ++ bool (*input)(struct tui_widget *tw, struct tui_state *ts, ++ uint32_t key); ++ void (*destroy)(struct tui_widget *tw); ++}; ++ ++struct tui_widget { ++ size_t x; ++ size_t y; ++ size_t width; ++ size_t height; ++ bool updated; ++ const struct tui_widget_ops *ops; ++ TAILQ_ENTRY(tui_widget) link; ++}; ++ ++TAILQ_HEAD(tui_widget_head, tui_widget); ++ ++/* ++ * All functions here are prefixed with __tui since they are global symbols ++ * even if not directly exported to the TA. ++ */ ++ ++TEE_Result __tui_pta_get_screen_info(size_t *width, size_t *height, ++ size_t *width_dpi, size_t *height_dpi, ++ uint32_t *bpp); ++TEE_Result __tui_pta_init_session(void); ++TEE_Result __tui_pta_close_session(void); ++TEE_Result __tui_pta_init_screen(uint32_t color); ++TEE_Result __tui_pta_set_screen_image(size_t xpos, size_t ypos, ++ struct image *image); ++TEE_Result __tui_pta_display_screen(bool clear_input, size_t timeout, ++ bool *is_timedout, bool *is_key, ++ uint32_t *key, size_t *xpos, size_t *ypos); ++ ++void __tui_widget_add(struct tui_widget_head *twh, struct tui_widget *tw); ++struct image *__tui_widget_image_alloc(struct tui_widget *tw, uint32_t color); ++TEE_Result __tui_widget_display(struct tui_widget *tw, struct tui_state *ts, ++ struct image *image); ++struct tui_widget *__tui_widget_find(struct tui_widget_head *twh, size_t xpos, ++ size_t ypos); ++bool __tui_widget_op_dummy_input(struct tui_widget *tw, struct tui_state *ts, ++ uint32_t key); ++void __tui_widget_op_unfocus(struct tui_widget *tw, struct tui_state *ts); ++ ++TEE_Result __tui_button_create(struct tui_widget_head *twh, ++ TEE_TUIButtonType btype, ++ TEE_TUIScreenButtonInfo *binfo, ++ const TEE_TUIButton *custom_button, ++ enum button_pos bpos, size_t scr_width, ++ size_t scr_height); ++ ++TEE_Result __tui_keyboard_create(struct tui_widget_head *twh, size_t *ypos, ++ size_t scr_width); ++void __tui_keyboard_select_layout(struct tui_state *ts); ++struct tui_widget *__tui_keyboard_find(struct tui_widget_head *twh); ++ ++TEE_Result __tui_entry_fields_create(struct tui_widget_head *twh, ++ size_t *ypos, size_t scr_width, ++ TEE_TUIScreenButtonInfo *cbi, ++ TEE_TUIEntryField *efs, size_t num_efs); ++TEE_TUIEntryFieldType __tui_entry_field_get_input_type(struct tui_widget *tw); ++struct tui_widget *__tui_entry_field_find_first(struct tui_widget_head *twh); ++bool __tui_entry_field_has_temp_clear(struct tui_widget *tw); ++void __tui_entry_field_reset_temp_clear(struct tui_widget *tw); ++bool __tui_entry_fields_are_ok(struct tui_widget_head *twh); ++ ++#endif /*__TUI_PRIVATE_H*/ ++ +diff --git a/lib/libutee/tui/tui_pta.c b/lib/libutee/tui/tui_pta.c +new file mode 100644 +index 000000000..aeeb3f953 +--- /dev/null ++++ b/lib/libutee/tui/tui_pta.c +@@ -0,0 +1,137 @@ ++// SPDX-License-Identifier: BSD-2-Clause ++/* ++ * Copyright (c) 2016-2021, Linaro Limited ++ */ ++ ++#include ++#include ++#include ++#include ++#include "image.h" ++#include "tui_private.h" ++ ++static TEE_Result invoke_tui_pta(uint32_t cmd_id, uint32_t param_types, ++ TEE_Param params[TEE_NUM_PARAMS]) ++{ ++ static TEE_TASessionHandle sess = TEE_HANDLE_NULL; ++ static const TEE_UUID core_uuid = PTA_TUI_UUID; ++ ++ if (sess == TEE_HANDLE_NULL) { ++ TEE_Result res = TEE_OpenTASession(&core_uuid, 0, 0, NULL, ++ &sess, NULL); ++ ++ if (res != TEE_SUCCESS) ++ return res; ++ } ++ ++ return TEE_InvokeTACommand(sess, 0, cmd_id, param_types, params, NULL); ++} ++ ++TEE_Result __tui_pta_get_screen_info(size_t *width, size_t *height, ++ size_t *width_dpi, size_t *height_dpi, ++ uint32_t *bpp) ++{ ++ TEE_Result res; ++ uint32_t param_types; ++ TEE_Param params[TEE_NUM_PARAMS]; ++ ++ param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_OUTPUT, ++ TEE_PARAM_TYPE_VALUE_OUTPUT, ++ TEE_PARAM_TYPE_VALUE_OUTPUT, ++ TEE_PARAM_TYPE_NONE); ++ memset(params, 0, sizeof(params)); ++ res = invoke_tui_pta(PTA_TUI_GET_SCREEN_INFO, param_types, params); ++ *width = params[0].value.a; ++ *height = params[0].value.b; ++ if (width_dpi) ++ *width_dpi = params[1].value.a; ++ if (height_dpi) ++ *height_dpi = params[1].value.b; ++ if (bpp) ++ *bpp = params[2].value.a; ++ return res; ++} ++ ++TEE_Result __tui_pta_init_session(void) ++{ ++ uint32_t param_types; ++ TEE_Param params[TEE_NUM_PARAMS]; ++ ++ param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_NONE, ++ TEE_PARAM_TYPE_NONE, ++ TEE_PARAM_TYPE_NONE, ++ TEE_PARAM_TYPE_NONE); ++ memset(params, 0, sizeof(params)); ++ return invoke_tui_pta(PTA_TUI_INIT_SESSION, param_types, params); ++} ++ ++TEE_Result __tui_pta_close_session(void) ++{ ++ uint32_t param_types; ++ TEE_Param params[TEE_NUM_PARAMS]; ++ ++ param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_NONE, ++ TEE_PARAM_TYPE_NONE, ++ TEE_PARAM_TYPE_NONE, ++ TEE_PARAM_TYPE_NONE); ++ memset(params, 0, sizeof(params)); ++ return invoke_tui_pta(PTA_TUI_CLOSE_SESSION, param_types, params); ++} ++ ++TEE_Result __tui_pta_init_screen(uint32_t color) ++{ ++ uint32_t param_types; ++ TEE_Param params[TEE_NUM_PARAMS]; ++ ++ param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_NONE, ++ TEE_PARAM_TYPE_NONE, ++ TEE_PARAM_TYPE_NONE); ++ memset(params, 0, sizeof(params)); ++ params[0].value.a = color; ++ return invoke_tui_pta(PTA_TUI_INIT_SCREEN, param_types, params); ++} ++ ++TEE_Result __tui_pta_set_screen_image(size_t xpos, size_t ypos, ++ struct image *image) ++{ ++ uint32_t param_types; ++ TEE_Param params[TEE_NUM_PARAMS]; ++ ++ param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_MEMREF_INPUT, ++ TEE_PARAM_TYPE_NONE); ++ memset(params, 0, sizeof(params)); ++ params[0].value.a = xpos; ++ params[0].value.b = ypos; ++ params[1].value.a = image->width; ++ params[1].value.b = image->height; ++ params[2].memref.buffer = image->buf; ++ params[2].memref.size = image->blen; ++ return invoke_tui_pta(PTA_TUI_SET_SCREEN_IMAGE, param_types, params); ++} ++ ++TEE_Result __tui_pta_display_screen(bool clear_input, size_t timeout, ++ bool *is_timedout, bool *is_key, ++ uint32_t *key, size_t *xpos, size_t *ypos) ++{ ++ TEE_Result res; ++ uint32_t param_types; ++ TEE_Param params[TEE_NUM_PARAMS]; ++ ++ param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_VALUE_OUTPUT, ++ TEE_PARAM_TYPE_VALUE_OUTPUT, ++ TEE_PARAM_TYPE_VALUE_OUTPUT); ++ memset(params, 0, sizeof(params)); ++ params[0].value.a = clear_input; ++ params[0].value.b = timeout; ++ res = invoke_tui_pta(PTA_TUI_DISPLAY_SCREEN, param_types, params); ++ *is_timedout = params[1].value.a; ++ *is_key = params[2].value.a; ++ *key = params[2].value.b; ++ *xpos = params[3].value.a; ++ *ypos = params[3].value.b; ++ return res; ++} +diff --git a/lib/libutee/tui/tui_widget.c b/lib/libutee/tui/tui_widget.c +new file mode 100644 +index 000000000..12d0a4f6c +--- /dev/null ++++ b/lib/libutee/tui/tui_widget.c +@@ -0,0 +1,199 @@ ++// SPDX-License-Identifier: BSD-2-Clause ++/* ++ * Copyright (c) 2016-2021, Linaro Limited ++ */ ++ ++#include ++#include ++#include ++#include "font.h" ++#include "image.h" ++#include "tui_private.h" ++ ++struct image *__tui_widget_image_alloc(struct tui_widget *tw, uint32_t color) ++{ ++ return image_alloc(tw->width, tw->height, color); ++} ++ ++TEE_Result __tui_widget_display(struct tui_widget *tw, struct tui_state *ts, ++ struct image *image) ++{ ++ if (tw == ts->focus) ++ image_set_border(image, BORDER_SIZE, COLOR_RGB_RED); ++ ++ return __tui_pta_set_screen_image(tw->x, tw->y, image); ++} ++ ++bool __tui_widget_op_dummy_input(struct tui_widget *tw __unused, ++ struct tui_state *ts __unused, ++ uint32_t key __unused) ++{ ++ EMSG("Ignoring key %#" PRIX32, key); ++ return false; ++} ++ ++void __tui_widget_op_unfocus(struct tui_widget *tw, struct tui_state *ts) ++{ ++ if (tw == ts->focus) { ++ tw->updated = true; ++ ts->focus = NULL; ++ } ++} ++ ++void __tui_widget_add(struct tui_widget_head *twh, struct tui_widget *tw) ++{ ++ struct tui_widget *t; ++ ++ TAILQ_FOREACH(t, twh, link) { ++ if (tw->y < t->y || (tw->y == t->y && tw->x < t->y)) { ++ TAILQ_INSERT_BEFORE(t, tw, link); ++ return; ++ } ++ } ++ TAILQ_INSERT_TAIL(twh, tw, link); ++} ++ ++struct tui_widget *__tui_widget_find(struct tui_widget_head *twh, size_t xpos, ++ size_t ypos) ++{ ++ struct tui_widget *tw; ++ ++ TAILQ_FOREACH(tw, twh, link) ++ if (xpos >= tw->x && xpos < (tw->x + tw->width) && ++ ypos >= tw->y && ypos < (tw->y + tw->height)) ++ return tw; ++ return NULL; ++} ++ ++/* ++ * Button implementation. ++ */ ++ ++struct tui_button { ++ TEE_TUIButtonType type; ++ char *text; ++ struct tui_widget tw; ++}; ++ ++static bool __maybe_unused is_tui_button(struct tui_widget *tw); ++ ++static struct tui_button *to_tui_button(struct tui_widget *tw) ++{ ++ assert(is_tui_button(tw)); ++ return container_of(tw, struct tui_button, tw); ++} ++ ++static void op_button_focus(struct tui_widget *tw, struct tui_state *ts) ++{ ++ if (tw != ts->focus) { ++ tw->updated = true; ++ ts->focus = tw; ++ } ++} ++ ++static TEE_Result op_button_paint(struct tui_widget *tw, struct tui_state *ts) ++{ ++ TEE_Result res; ++ struct tui_button *b = to_tui_button(tw); ++ struct image *image; ++ ++ if (!tw->updated) ++ return TEE_SUCCESS; ++ ++ image = __tui_widget_image_alloc(tw, COLOR_RGB_LIGHT_GOLDENROD); ++ if (!image) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ if (!font_render_text(image, 0, 0, b->text, COLOR_RGB_BLACK)) { ++ res = TEE_ERROR_BAD_FORMAT; ++ goto out; ++ } ++ res = __tui_widget_display(tw, ts, image); ++ if (res == TEE_SUCCESS) ++ tw->updated = false; ++out: ++ image_free(image); ++ return res; ++} ++ ++static void op_button_click(struct tui_widget *tw, struct tui_state *ts, ++ ssize_t x __unused, ssize_t y __unused) ++{ ++ struct tui_button *b = to_tui_button(tw); ++ ++ ts->clicked_button = &b->type; ++ if (tw != ts->focus) { ++ ts->focus->ops->unfocus(ts->focus, ts); ++ tw->ops->focus(tw, ts); ++ } ++} ++ ++static void op_button_destroy(struct tui_widget *tw) ++{ ++ free(to_tui_button(tw)); ++} ++ ++static const struct tui_widget_ops button_ops = { ++ .paint = op_button_paint, ++ .focus = op_button_focus, ++ .unfocus = __tui_widget_op_unfocus, ++ .click = op_button_click, ++ .input = __tui_widget_op_dummy_input, ++ .destroy = op_button_destroy, ++}; ++ ++static bool is_tui_button(struct tui_widget *tw) ++{ ++ return tw->ops == &button_ops; ++} ++ ++TEE_Result __tui_button_create(struct tui_widget_head *twh, ++ TEE_TUIButtonType btype, ++ TEE_TUIScreenButtonInfo *binfo, ++ const TEE_TUIButton *custom_button __unused, ++ enum button_pos bpos, ++ size_t scr_width, size_t scr_height) ++{ ++ TEE_Result res; ++ struct tui_button *b = calloc(1, sizeof(*b)); ++ size_t width; ++ size_t height; ++ ++ if (!b) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ b->tw.ops = &button_ops; ++ b->tw.updated = true; ++ b->type = btype; ++ ++ b->text = binfo->buttonText; ++ if (!font_check_text_format(b->text, &width, &height, NULL)) { ++ res = TEE_ERROR_GENERIC; ++ goto out; ++ } ++ ++ b->tw.width = width + BORDER_SIZE * 2; ++ b->tw.height = height + BORDER_SIZE * 2; ++ b->tw.y = scr_height - b->tw.height; ++ switch (bpos) { ++ case POS_LEFT: ++ b->tw.x = 0; ++ break; ++ case POS_MIDDLE: ++ b->tw.x = (scr_width / 2) - (b->tw.width / 2); ++ break; ++ case POS_RIGHT: ++ b->tw.x = scr_width - b->tw.width; ++ break; ++ default: ++ res = TEE_ERROR_GENERIC; ++ goto out; ++ } ++ ++ res = TEE_SUCCESS; ++ __tui_widget_add(twh, &b->tw); ++out: ++ if (res != TEE_SUCCESS) ++ free(b); ++ return res; ++} +diff --git a/lib/libutee/tui/utf8.c b/lib/libutee/tui/utf8.c +new file mode 100644 +index 000000000..2a1c03ac3 +--- /dev/null ++++ b/lib/libutee/tui/utf8.c +@@ -0,0 +1,64 @@ ++// SPDX-License-Identifier: BSD-2-Clause ++/* ++ * Copyright (c) 2016-2021, Linaro Limited ++ */ ++ ++#include "utf8.h" ++ ++uint32_t utf8_get_code_point(const char *utf8_str, size_t *idx) ++{ ++ const uint8_t *p = (const uint8_t *)utf8_str + *idx; ++ uint8_t first = p[0]; ++ uint8_t first_mask; ++ uint8_t first_prefix; ++ size_t n; ++ size_t m; ++ uint32_t cp; ++ ++ if (!first) ++ return 0; ++ ++ if (!(p[0] & 0x80)) { ++ /* 0vvv_vvvv */ ++ (*idx)++; ++ return p[0]; ++ } ++ ++ /* ++ * n = 1: ++ * first_mask = 0xe0, first_prefix = 0xc0 ++ * 110v_vvvv 10vv_vvvv ++ * ++ * n = 2: ++ * first_mask = 0xf0, first_prefix = 0xe0 ++ * 1110_vvvv 10vv_vvvv 10vv_vvvv ++ * ++ * n = 3: ++ * first_mask = 0xf8, first_prefix = 0xf0 ++ * 1111_0vvv 10vv_vvvv 10vv_vvvv 10vv_vvvv ++ * ++ * n = 4: ++ * first_mask = 0xfc, first_prefix = 0xf8 ++ * 1111_10vv 10vv_vvvv 10vv_vvvv 10vv_vvvv 10vv_vvvv ++ * ++ * n = 5: ++ * first_mask = 0xfe, first_prefix = 0xfc ++ * 1111_110v 10vv_vvvv 10vv_vvvv 10vv_vvvv 10vv_vvvv 10vv_vvvv ++ */ ++ first_mask = 0xe0; ++ first_prefix = 0xc0; ++ for (n = 1; n < 6 && (p[n] & 0xc0) == 0x80; n++) { ++ if ((first & first_mask) == first_prefix) { ++ cp = first & ~first_mask; ++ for (m = 1; m <= n; m++) { ++ cp <<= 6; ++ cp |= p[m] & 0x3f; ++ } ++ (*idx) += n + 1; ++ return cp; ++ } ++ first_prefix = first_mask; ++ first_mask = (first_mask >> 1) | 0x80; ++ } ++ return UTF8_INVALID_CODE; ++} +diff --git a/lib/libutee/tui/utf8.h b/lib/libutee/tui/utf8.h +new file mode 100644 +index 000000000..d6878626a +--- /dev/null ++++ b/lib/libutee/tui/utf8.h +@@ -0,0 +1,15 @@ ++/* SPDX-License-Identifier: BSD-2-Clause */ ++/* ++ * Copyright (c) 2016-2021, Linaro Limited ++ */ ++ ++#ifndef __UTF8_H ++#define __UTF8_H ++ ++#include ++ ++#define UTF8_INVALID_CODE 0xffffffff ++ ++uint32_t utf8_get_code_point(const char *utf8_str, size_t *idx); ++ ++#endif /*__UTF8_H*/ +diff --git a/core/lib/zlib/adler32.c b/lib/libzlib/adler32.c +similarity index 100% +rename from core/lib/zlib/adler32.c +rename to lib/libzlib/adler32.c +diff --git a/lib/libzlib/crc32.c b/lib/libzlib/crc32.c +new file mode 100644 +index 000000000..080bd643b +--- /dev/null ++++ b/lib/libzlib/crc32.c +@@ -0,0 +1,443 @@ ++// SPDX-License-Identifier: Zlib ++/* crc32.c -- compute the CRC-32 of a data stream ++ * Copyright (C) 1995-2006, 2010, 2011, 2012, 2016 Mark Adler ++ * For conditions of distribution and use, see copyright notice in zlib.h ++ * ++ * Thanks to Rodney Brown for his contribution of faster ++ * CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing ++ * tables for updating the shift register in one step with three exclusive-ors ++ * instead of four steps with four exclusive-ors. This results in about a ++ * factor of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3. ++ */ ++ ++/* @(#) $Id$ */ ++ ++/* ++ Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore ++ protection on the static variables used to control the first-use generation ++ of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should ++ first call get_crc_table() to initialize the tables before allowing more than ++ one thread to use crc32(). ++ ++ DYNAMIC_CRC_TABLE and MAKECRCH can be #defined to write out crc32.h. ++ */ ++ ++#ifdef MAKECRCH ++# include ++# ifndef DYNAMIC_CRC_TABLE ++# define DYNAMIC_CRC_TABLE ++# endif /* !DYNAMIC_CRC_TABLE */ ++#endif /* MAKECRCH */ ++ ++#include "zutil.h" /* for STDC and FAR definitions */ ++ ++/* Definitions for doing the crc four data bytes at a time. */ ++#if !defined(NOBYFOUR) && defined(Z_U4) ++# define BYFOUR ++#endif ++#ifdef BYFOUR ++ local unsigned long crc32_little OF((unsigned long, ++ const unsigned char FAR *, z_size_t)); ++ local unsigned long crc32_big OF((unsigned long, ++ const unsigned char FAR *, z_size_t)); ++# define TBLS 8 ++#else ++# define TBLS 1 ++#endif /* BYFOUR */ ++ ++/* Local functions for crc concatenation */ ++local unsigned long gf2_matrix_times OF((unsigned long *mat, ++ unsigned long vec)); ++local void gf2_matrix_square OF((unsigned long *square, unsigned long *mat)); ++local uLong crc32_combine_ OF((uLong crc1, uLong crc2, z_off64_t len2)); ++ ++ ++#ifdef DYNAMIC_CRC_TABLE ++ ++local volatile int crc_table_empty = 1; ++local z_crc_t FAR crc_table[TBLS][256]; ++local void make_crc_table OF((void)); ++#ifdef MAKECRCH ++ local void write_table OF((FILE *, const z_crc_t FAR *)); ++#endif /* MAKECRCH */ ++/* ++ Generate tables for a byte-wise 32-bit CRC calculation on the polynomial: ++ x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1. ++ ++ Polynomials over GF(2) are represented in binary, one bit per coefficient, ++ with the lowest powers in the most significant bit. Then adding polynomials ++ is just exclusive-or, and multiplying a polynomial by x is a right shift by ++ one. If we call the above polynomial p, and represent a byte as the ++ polynomial q, also with the lowest power in the most significant bit (so the ++ byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p, ++ where a mod b means the remainder after dividing a by b. ++ ++ This calculation is done using the shift-register method of multiplying and ++ taking the remainder. The register is initialized to zero, and for each ++ incoming bit, x^32 is added mod p to the register if the bit is a one (where ++ x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by ++ x (which is shifting right by one and adding x^32 mod p if the bit shifted ++ out is a one). We start with the highest power (least significant bit) of ++ q and repeat for all eight bits of q. ++ ++ The first table is simply the CRC of all possible eight bit values. This is ++ all the information needed to generate CRCs on data a byte at a time for all ++ combinations of CRC register values and incoming bytes. The remaining tables ++ allow for word-at-a-time CRC calculation for both big-endian and little- ++ endian machines, where a word is four bytes. ++*/ ++local void make_crc_table() ++{ ++ z_crc_t c; ++ int n, k; ++ z_crc_t poly; /* polynomial exclusive-or pattern */ ++ /* terms of polynomial defining this crc (except x^32): */ ++ static volatile int first = 1; /* flag to limit concurrent making */ ++ static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; ++ ++ /* See if another task is already doing this (not thread-safe, but better ++ than nothing -- significantly reduces duration of vulnerability in ++ case the advice about DYNAMIC_CRC_TABLE is ignored) */ ++ if (first) { ++ first = 0; ++ ++ /* make exclusive-or pattern from polynomial (0xedb88320UL) */ ++ poly = 0; ++ for (n = 0; n < (int)(sizeof(p)/sizeof(unsigned char)); n++) ++ poly |= (z_crc_t)1 << (31 - p[n]); ++ ++ /* generate a crc for every 8-bit value */ ++ for (n = 0; n < 256; n++) { ++ c = (z_crc_t)n; ++ for (k = 0; k < 8; k++) ++ c = c & 1 ? poly ^ (c >> 1) : c >> 1; ++ crc_table[0][n] = c; ++ } ++ ++#ifdef BYFOUR ++ /* generate crc for each value followed by one, two, and three zeros, ++ and then the byte reversal of those as well as the first table */ ++ for (n = 0; n < 256; n++) { ++ c = crc_table[0][n]; ++ crc_table[4][n] = ZSWAP32(c); ++ for (k = 1; k < 4; k++) { ++ c = crc_table[0][c & 0xff] ^ (c >> 8); ++ crc_table[k][n] = c; ++ crc_table[k + 4][n] = ZSWAP32(c); ++ } ++ } ++#endif /* BYFOUR */ ++ ++ crc_table_empty = 0; ++ } ++ else { /* not first */ ++ /* wait for the other guy to finish (not efficient, but rare) */ ++ while (crc_table_empty) ++ ; ++ } ++ ++#ifdef MAKECRCH ++ /* write out CRC tables to crc32.h */ ++ { ++ FILE *out; ++ ++ out = fopen("crc32.h", "w"); ++ if (out == NULL) return; ++ fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n"); ++ fprintf(out, " * Generated automatically by crc32.c\n */\n\n"); ++ fprintf(out, "local const z_crc_t FAR "); ++ fprintf(out, "crc_table[TBLS][256] =\n{\n {\n"); ++ write_table(out, crc_table[0]); ++# ifdef BYFOUR ++ fprintf(out, "#ifdef BYFOUR\n"); ++ for (k = 1; k < 8; k++) { ++ fprintf(out, " },\n {\n"); ++ write_table(out, crc_table[k]); ++ } ++ fprintf(out, "#endif\n"); ++# endif /* BYFOUR */ ++ fprintf(out, " }\n};\n"); ++ fclose(out); ++ } ++#endif /* MAKECRCH */ ++} ++ ++#ifdef MAKECRCH ++local void write_table(out, table) ++ FILE *out; ++ const z_crc_t FAR *table; ++{ ++ int n; ++ ++ for (n = 0; n < 256; n++) ++ fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ", ++ (unsigned long)(table[n]), ++ n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", ")); ++} ++#endif /* MAKECRCH */ ++ ++#else /* !DYNAMIC_CRC_TABLE */ ++/* ======================================================================== ++ * Tables of CRC-32s of all single-byte values, made by make_crc_table(). ++ */ ++#include "crc32.h" ++#endif /* DYNAMIC_CRC_TABLE */ ++ ++/* ========================================================================= ++ * This function can be used by asm versions of crc32() ++ */ ++const z_crc_t FAR * ZEXPORT get_crc_table() ++{ ++#ifdef DYNAMIC_CRC_TABLE ++ if (crc_table_empty) ++ make_crc_table(); ++#endif /* DYNAMIC_CRC_TABLE */ ++ return (const z_crc_t FAR *)crc_table; ++} ++ ++/* ========================================================================= */ ++#define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8) ++#define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1 ++ ++/* ========================================================================= */ ++unsigned long ZEXPORT crc32_z(crc, buf, len) ++ unsigned long crc; ++ const unsigned char FAR *buf; ++ z_size_t len; ++{ ++ if (buf == Z_NULL) return 0UL; ++ ++#ifdef DYNAMIC_CRC_TABLE ++ if (crc_table_empty) ++ make_crc_table(); ++#endif /* DYNAMIC_CRC_TABLE */ ++ ++#ifdef BYFOUR ++ if (sizeof(void *) == sizeof(ptrdiff_t)) { ++ z_crc_t endian; ++ ++ endian = 1; ++ if (*((unsigned char *)(&endian))) ++ return crc32_little(crc, buf, len); ++ else ++ return crc32_big(crc, buf, len); ++ } ++#endif /* BYFOUR */ ++ crc = crc ^ 0xffffffffUL; ++ while (len >= 8) { ++ DO8; ++ len -= 8; ++ } ++ if (len) do { ++ DO1; ++ } while (--len); ++ return crc ^ 0xffffffffUL; ++} ++ ++/* ========================================================================= */ ++unsigned long ZEXPORT crc32(crc, buf, len) ++ unsigned long crc; ++ const unsigned char FAR *buf; ++ uInt len; ++{ ++ return crc32_z(crc, buf, len); ++} ++ ++#ifdef BYFOUR ++ ++/* ++ This BYFOUR code accesses the passed unsigned char * buffer with a 32-bit ++ integer pointer type. This violates the strict aliasing rule, where a ++ compiler can assume, for optimization purposes, that two pointers to ++ fundamentally different types won't ever point to the same memory. This can ++ manifest as a problem only if one of the pointers is written to. This code ++ only reads from those pointers. So long as this code remains isolated in ++ this compilation unit, there won't be a problem. For this reason, this code ++ should not be copied and pasted into a compilation unit in which other code ++ writes to the buffer that is passed to these routines. ++ */ ++ ++/* ========================================================================= */ ++#define DOLIT4 c ^= *buf4++; \ ++ c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \ ++ crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24] ++#define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4 ++ ++/* ========================================================================= */ ++local unsigned long crc32_little(crc, buf, len) ++ unsigned long crc; ++ const unsigned char FAR *buf; ++ z_size_t len; ++{ ++ register z_crc_t c; ++ register const z_crc_t FAR *buf4; ++ ++ c = (z_crc_t)crc; ++ c = ~c; ++ while (len && ((ptrdiff_t)buf & 3)) { ++ c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); ++ len--; ++ } ++ ++ buf4 = (const z_crc_t FAR *)(const void FAR *)buf; ++ while (len >= 32) { ++ DOLIT32; ++ len -= 32; ++ } ++ while (len >= 4) { ++ DOLIT4; ++ len -= 4; ++ } ++ buf = (const unsigned char FAR *)buf4; ++ ++ if (len) do { ++ c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); ++ } while (--len); ++ c = ~c; ++ return (unsigned long)c; ++} ++ ++/* ========================================================================= */ ++#define DOBIG4 c ^= *buf4++; \ ++ c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \ ++ crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24] ++#define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4 ++ ++/* ========================================================================= */ ++local unsigned long crc32_big(crc, buf, len) ++ unsigned long crc; ++ const unsigned char FAR *buf; ++ z_size_t len; ++{ ++ register z_crc_t c; ++ register const z_crc_t FAR *buf4; ++ ++ c = ZSWAP32((z_crc_t)crc); ++ c = ~c; ++ while (len && ((ptrdiff_t)buf & 3)) { ++ c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); ++ len--; ++ } ++ ++ buf4 = (const z_crc_t FAR *)(const void FAR *)buf; ++ while (len >= 32) { ++ DOBIG32; ++ len -= 32; ++ } ++ while (len >= 4) { ++ DOBIG4; ++ len -= 4; ++ } ++ buf = (const unsigned char FAR *)buf4; ++ ++ if (len) do { ++ c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); ++ } while (--len); ++ c = ~c; ++ return (unsigned long)(ZSWAP32(c)); ++} ++ ++#endif /* BYFOUR */ ++ ++#define GF2_DIM 32 /* dimension of GF(2) vectors (length of CRC) */ ++ ++/* ========================================================================= */ ++local unsigned long gf2_matrix_times(mat, vec) ++ unsigned long *mat; ++ unsigned long vec; ++{ ++ unsigned long sum; ++ ++ sum = 0; ++ while (vec) { ++ if (vec & 1) ++ sum ^= *mat; ++ vec >>= 1; ++ mat++; ++ } ++ return sum; ++} ++ ++/* ========================================================================= */ ++local void gf2_matrix_square(square, mat) ++ unsigned long *square; ++ unsigned long *mat; ++{ ++ int n; ++ ++ for (n = 0; n < GF2_DIM; n++) ++ square[n] = gf2_matrix_times(mat, mat[n]); ++} ++ ++/* ========================================================================= */ ++local uLong crc32_combine_(crc1, crc2, len2) ++ uLong crc1; ++ uLong crc2; ++ z_off64_t len2; ++{ ++ int n; ++ unsigned long row; ++ unsigned long even[GF2_DIM]; /* even-power-of-two zeros operator */ ++ unsigned long odd[GF2_DIM]; /* odd-power-of-two zeros operator */ ++ ++ /* degenerate case (also disallow negative lengths) */ ++ if (len2 <= 0) ++ return crc1; ++ ++ /* put operator for one zero bit in odd */ ++ odd[0] = 0xedb88320UL; /* CRC-32 polynomial */ ++ row = 1; ++ for (n = 1; n < GF2_DIM; n++) { ++ odd[n] = row; ++ row <<= 1; ++ } ++ ++ /* put operator for two zero bits in even */ ++ gf2_matrix_square(even, odd); ++ ++ /* put operator for four zero bits in odd */ ++ gf2_matrix_square(odd, even); ++ ++ /* apply len2 zeros to crc1 (first square will put the operator for one ++ zero byte, eight zero bits, in even) */ ++ do { ++ /* apply zeros operator for this bit of len2 */ ++ gf2_matrix_square(even, odd); ++ if (len2 & 1) ++ crc1 = gf2_matrix_times(even, crc1); ++ len2 >>= 1; ++ ++ /* if no more bits set, then done */ ++ if (len2 == 0) ++ break; ++ ++ /* another iteration of the loop with odd and even swapped */ ++ gf2_matrix_square(odd, even); ++ if (len2 & 1) ++ crc1 = gf2_matrix_times(odd, crc1); ++ len2 >>= 1; ++ ++ /* if no more bits set, then done */ ++ } while (len2 != 0); ++ ++ /* return combined crc */ ++ crc1 ^= crc2; ++ return crc1; ++} ++ ++/* ========================================================================= */ ++uLong ZEXPORT crc32_combine(crc1, crc2, len2) ++ uLong crc1; ++ uLong crc2; ++ z_off_t len2; ++{ ++ return crc32_combine_(crc1, crc2, len2); ++} ++ ++uLong ZEXPORT crc32_combine64(crc1, crc2, len2) ++ uLong crc1; ++ uLong crc2; ++ z_off64_t len2; ++{ ++ return crc32_combine_(crc1, crc2, len2); ++} +diff --git a/lib/libzlib/crc32.h b/lib/libzlib/crc32.h +new file mode 100644 +index 000000000..1ece563b6 +--- /dev/null ++++ b/lib/libzlib/crc32.h +@@ -0,0 +1,443 @@ ++/* SPDX-License-Identifier: Zlib */ ++/* crc32.h ++ -- tables for rapid CRC calculation ++ * Generated automatically by crc32.c ++ */ ++ ++local const z_crc_t FAR crc_table[TBLS][256] = ++{ ++ { ++ 0x00000000UL, 0x77073096UL, 0xee0e612cUL, 0x990951baUL, 0x076dc419UL, ++ 0x706af48fUL, 0xe963a535UL, 0x9e6495a3UL, 0x0edb8832UL, 0x79dcb8a4UL, ++ 0xe0d5e91eUL, 0x97d2d988UL, 0x09b64c2bUL, 0x7eb17cbdUL, 0xe7b82d07UL, ++ 0x90bf1d91UL, 0x1db71064UL, 0x6ab020f2UL, 0xf3b97148UL, 0x84be41deUL, ++ 0x1adad47dUL, 0x6ddde4ebUL, 0xf4d4b551UL, 0x83d385c7UL, 0x136c9856UL, ++ 0x646ba8c0UL, 0xfd62f97aUL, 0x8a65c9ecUL, 0x14015c4fUL, 0x63066cd9UL, ++ 0xfa0f3d63UL, 0x8d080df5UL, 0x3b6e20c8UL, 0x4c69105eUL, 0xd56041e4UL, ++ 0xa2677172UL, 0x3c03e4d1UL, 0x4b04d447UL, 0xd20d85fdUL, 0xa50ab56bUL, ++ 0x35b5a8faUL, 0x42b2986cUL, 0xdbbbc9d6UL, 0xacbcf940UL, 0x32d86ce3UL, ++ 0x45df5c75UL, 0xdcd60dcfUL, 0xabd13d59UL, 0x26d930acUL, 0x51de003aUL, ++ 0xc8d75180UL, 0xbfd06116UL, 0x21b4f4b5UL, 0x56b3c423UL, 0xcfba9599UL, ++ 0xb8bda50fUL, 0x2802b89eUL, 0x5f058808UL, 0xc60cd9b2UL, 0xb10be924UL, ++ 0x2f6f7c87UL, 0x58684c11UL, 0xc1611dabUL, 0xb6662d3dUL, 0x76dc4190UL, ++ 0x01db7106UL, 0x98d220bcUL, 0xefd5102aUL, 0x71b18589UL, 0x06b6b51fUL, ++ 0x9fbfe4a5UL, 0xe8b8d433UL, 0x7807c9a2UL, 0x0f00f934UL, 0x9609a88eUL, ++ 0xe10e9818UL, 0x7f6a0dbbUL, 0x086d3d2dUL, 0x91646c97UL, 0xe6635c01UL, ++ 0x6b6b51f4UL, 0x1c6c6162UL, 0x856530d8UL, 0xf262004eUL, 0x6c0695edUL, ++ 0x1b01a57bUL, 0x8208f4c1UL, 0xf50fc457UL, 0x65b0d9c6UL, 0x12b7e950UL, ++ 0x8bbeb8eaUL, 0xfcb9887cUL, 0x62dd1ddfUL, 0x15da2d49UL, 0x8cd37cf3UL, ++ 0xfbd44c65UL, 0x4db26158UL, 0x3ab551ceUL, 0xa3bc0074UL, 0xd4bb30e2UL, ++ 0x4adfa541UL, 0x3dd895d7UL, 0xa4d1c46dUL, 0xd3d6f4fbUL, 0x4369e96aUL, ++ 0x346ed9fcUL, 0xad678846UL, 0xda60b8d0UL, 0x44042d73UL, 0x33031de5UL, ++ 0xaa0a4c5fUL, 0xdd0d7cc9UL, 0x5005713cUL, 0x270241aaUL, 0xbe0b1010UL, ++ 0xc90c2086UL, 0x5768b525UL, 0x206f85b3UL, 0xb966d409UL, 0xce61e49fUL, ++ 0x5edef90eUL, 0x29d9c998UL, 0xb0d09822UL, 0xc7d7a8b4UL, 0x59b33d17UL, ++ 0x2eb40d81UL, 0xb7bd5c3bUL, 0xc0ba6cadUL, 0xedb88320UL, 0x9abfb3b6UL, ++ 0x03b6e20cUL, 0x74b1d29aUL, 0xead54739UL, 0x9dd277afUL, 0x04db2615UL, ++ 0x73dc1683UL, 0xe3630b12UL, 0x94643b84UL, 0x0d6d6a3eUL, 0x7a6a5aa8UL, ++ 0xe40ecf0bUL, 0x9309ff9dUL, 0x0a00ae27UL, 0x7d079eb1UL, 0xf00f9344UL, ++ 0x8708a3d2UL, 0x1e01f268UL, 0x6906c2feUL, 0xf762575dUL, 0x806567cbUL, ++ 0x196c3671UL, 0x6e6b06e7UL, 0xfed41b76UL, 0x89d32be0UL, 0x10da7a5aUL, ++ 0x67dd4accUL, 0xf9b9df6fUL, 0x8ebeeff9UL, 0x17b7be43UL, 0x60b08ed5UL, ++ 0xd6d6a3e8UL, 0xa1d1937eUL, 0x38d8c2c4UL, 0x4fdff252UL, 0xd1bb67f1UL, ++ 0xa6bc5767UL, 0x3fb506ddUL, 0x48b2364bUL, 0xd80d2bdaUL, 0xaf0a1b4cUL, ++ 0x36034af6UL, 0x41047a60UL, 0xdf60efc3UL, 0xa867df55UL, 0x316e8eefUL, ++ 0x4669be79UL, 0xcb61b38cUL, 0xbc66831aUL, 0x256fd2a0UL, 0x5268e236UL, ++ 0xcc0c7795UL, 0xbb0b4703UL, 0x220216b9UL, 0x5505262fUL, 0xc5ba3bbeUL, ++ 0xb2bd0b28UL, 0x2bb45a92UL, 0x5cb36a04UL, 0xc2d7ffa7UL, 0xb5d0cf31UL, ++ 0x2cd99e8bUL, 0x5bdeae1dUL, 0x9b64c2b0UL, 0xec63f226UL, 0x756aa39cUL, ++ 0x026d930aUL, 0x9c0906a9UL, 0xeb0e363fUL, 0x72076785UL, 0x05005713UL, ++ 0x95bf4a82UL, 0xe2b87a14UL, 0x7bb12baeUL, 0x0cb61b38UL, 0x92d28e9bUL, ++ 0xe5d5be0dUL, 0x7cdcefb7UL, 0x0bdbdf21UL, 0x86d3d2d4UL, 0xf1d4e242UL, ++ 0x68ddb3f8UL, 0x1fda836eUL, 0x81be16cdUL, 0xf6b9265bUL, 0x6fb077e1UL, ++ 0x18b74777UL, 0x88085ae6UL, 0xff0f6a70UL, 0x66063bcaUL, 0x11010b5cUL, ++ 0x8f659effUL, 0xf862ae69UL, 0x616bffd3UL, 0x166ccf45UL, 0xa00ae278UL, ++ 0xd70dd2eeUL, 0x4e048354UL, 0x3903b3c2UL, 0xa7672661UL, 0xd06016f7UL, ++ 0x4969474dUL, 0x3e6e77dbUL, 0xaed16a4aUL, 0xd9d65adcUL, 0x40df0b66UL, ++ 0x37d83bf0UL, 0xa9bcae53UL, 0xdebb9ec5UL, 0x47b2cf7fUL, 0x30b5ffe9UL, ++ 0xbdbdf21cUL, 0xcabac28aUL, 0x53b39330UL, 0x24b4a3a6UL, 0xbad03605UL, ++ 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL, ++ 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL, ++ 0x2d02ef8dUL ++#ifdef BYFOUR ++ }, ++ { ++ 0x00000000UL, 0x191b3141UL, 0x32366282UL, 0x2b2d53c3UL, 0x646cc504UL, ++ 0x7d77f445UL, 0x565aa786UL, 0x4f4196c7UL, 0xc8d98a08UL, 0xd1c2bb49UL, ++ 0xfaefe88aUL, 0xe3f4d9cbUL, 0xacb54f0cUL, 0xb5ae7e4dUL, 0x9e832d8eUL, ++ 0x87981ccfUL, 0x4ac21251UL, 0x53d92310UL, 0x78f470d3UL, 0x61ef4192UL, ++ 0x2eaed755UL, 0x37b5e614UL, 0x1c98b5d7UL, 0x05838496UL, 0x821b9859UL, ++ 0x9b00a918UL, 0xb02dfadbUL, 0xa936cb9aUL, 0xe6775d5dUL, 0xff6c6c1cUL, ++ 0xd4413fdfUL, 0xcd5a0e9eUL, 0x958424a2UL, 0x8c9f15e3UL, 0xa7b24620UL, ++ 0xbea97761UL, 0xf1e8e1a6UL, 0xe8f3d0e7UL, 0xc3de8324UL, 0xdac5b265UL, ++ 0x5d5daeaaUL, 0x44469febUL, 0x6f6bcc28UL, 0x7670fd69UL, 0x39316baeUL, ++ 0x202a5aefUL, 0x0b07092cUL, 0x121c386dUL, 0xdf4636f3UL, 0xc65d07b2UL, ++ 0xed705471UL, 0xf46b6530UL, 0xbb2af3f7UL, 0xa231c2b6UL, 0x891c9175UL, ++ 0x9007a034UL, 0x179fbcfbUL, 0x0e848dbaUL, 0x25a9de79UL, 0x3cb2ef38UL, ++ 0x73f379ffUL, 0x6ae848beUL, 0x41c51b7dUL, 0x58de2a3cUL, 0xf0794f05UL, ++ 0xe9627e44UL, 0xc24f2d87UL, 0xdb541cc6UL, 0x94158a01UL, 0x8d0ebb40UL, ++ 0xa623e883UL, 0xbf38d9c2UL, 0x38a0c50dUL, 0x21bbf44cUL, 0x0a96a78fUL, ++ 0x138d96ceUL, 0x5ccc0009UL, 0x45d73148UL, 0x6efa628bUL, 0x77e153caUL, ++ 0xbabb5d54UL, 0xa3a06c15UL, 0x888d3fd6UL, 0x91960e97UL, 0xded79850UL, ++ 0xc7cca911UL, 0xece1fad2UL, 0xf5facb93UL, 0x7262d75cUL, 0x6b79e61dUL, ++ 0x4054b5deUL, 0x594f849fUL, 0x160e1258UL, 0x0f152319UL, 0x243870daUL, ++ 0x3d23419bUL, 0x65fd6ba7UL, 0x7ce65ae6UL, 0x57cb0925UL, 0x4ed03864UL, ++ 0x0191aea3UL, 0x188a9fe2UL, 0x33a7cc21UL, 0x2abcfd60UL, 0xad24e1afUL, ++ 0xb43fd0eeUL, 0x9f12832dUL, 0x8609b26cUL, 0xc94824abUL, 0xd05315eaUL, ++ 0xfb7e4629UL, 0xe2657768UL, 0x2f3f79f6UL, 0x362448b7UL, 0x1d091b74UL, ++ 0x04122a35UL, 0x4b53bcf2UL, 0x52488db3UL, 0x7965de70UL, 0x607eef31UL, ++ 0xe7e6f3feUL, 0xfefdc2bfUL, 0xd5d0917cUL, 0xcccba03dUL, 0x838a36faUL, ++ 0x9a9107bbUL, 0xb1bc5478UL, 0xa8a76539UL, 0x3b83984bUL, 0x2298a90aUL, ++ 0x09b5fac9UL, 0x10aecb88UL, 0x5fef5d4fUL, 0x46f46c0eUL, 0x6dd93fcdUL, ++ 0x74c20e8cUL, 0xf35a1243UL, 0xea412302UL, 0xc16c70c1UL, 0xd8774180UL, ++ 0x9736d747UL, 0x8e2de606UL, 0xa500b5c5UL, 0xbc1b8484UL, 0x71418a1aUL, ++ 0x685abb5bUL, 0x4377e898UL, 0x5a6cd9d9UL, 0x152d4f1eUL, 0x0c367e5fUL, ++ 0x271b2d9cUL, 0x3e001cddUL, 0xb9980012UL, 0xa0833153UL, 0x8bae6290UL, ++ 0x92b553d1UL, 0xddf4c516UL, 0xc4eff457UL, 0xefc2a794UL, 0xf6d996d5UL, ++ 0xae07bce9UL, 0xb71c8da8UL, 0x9c31de6bUL, 0x852aef2aUL, 0xca6b79edUL, ++ 0xd37048acUL, 0xf85d1b6fUL, 0xe1462a2eUL, 0x66de36e1UL, 0x7fc507a0UL, ++ 0x54e85463UL, 0x4df36522UL, 0x02b2f3e5UL, 0x1ba9c2a4UL, 0x30849167UL, ++ 0x299fa026UL, 0xe4c5aeb8UL, 0xfdde9ff9UL, 0xd6f3cc3aUL, 0xcfe8fd7bUL, ++ 0x80a96bbcUL, 0x99b25afdUL, 0xb29f093eUL, 0xab84387fUL, 0x2c1c24b0UL, ++ 0x350715f1UL, 0x1e2a4632UL, 0x07317773UL, 0x4870e1b4UL, 0x516bd0f5UL, ++ 0x7a468336UL, 0x635db277UL, 0xcbfad74eUL, 0xd2e1e60fUL, 0xf9ccb5ccUL, ++ 0xe0d7848dUL, 0xaf96124aUL, 0xb68d230bUL, 0x9da070c8UL, 0x84bb4189UL, ++ 0x03235d46UL, 0x1a386c07UL, 0x31153fc4UL, 0x280e0e85UL, 0x674f9842UL, ++ 0x7e54a903UL, 0x5579fac0UL, 0x4c62cb81UL, 0x8138c51fUL, 0x9823f45eUL, ++ 0xb30ea79dUL, 0xaa1596dcUL, 0xe554001bUL, 0xfc4f315aUL, 0xd7626299UL, ++ 0xce7953d8UL, 0x49e14f17UL, 0x50fa7e56UL, 0x7bd72d95UL, 0x62cc1cd4UL, ++ 0x2d8d8a13UL, 0x3496bb52UL, 0x1fbbe891UL, 0x06a0d9d0UL, 0x5e7ef3ecUL, ++ 0x4765c2adUL, 0x6c48916eUL, 0x7553a02fUL, 0x3a1236e8UL, 0x230907a9UL, ++ 0x0824546aUL, 0x113f652bUL, 0x96a779e4UL, 0x8fbc48a5UL, 0xa4911b66UL, ++ 0xbd8a2a27UL, 0xf2cbbce0UL, 0xebd08da1UL, 0xc0fdde62UL, 0xd9e6ef23UL, ++ 0x14bce1bdUL, 0x0da7d0fcUL, 0x268a833fUL, 0x3f91b27eUL, 0x70d024b9UL, ++ 0x69cb15f8UL, 0x42e6463bUL, 0x5bfd777aUL, 0xdc656bb5UL, 0xc57e5af4UL, ++ 0xee530937UL, 0xf7483876UL, 0xb809aeb1UL, 0xa1129ff0UL, 0x8a3fcc33UL, ++ 0x9324fd72UL ++ }, ++ { ++ 0x00000000UL, 0x01c26a37UL, 0x0384d46eUL, 0x0246be59UL, 0x0709a8dcUL, ++ 0x06cbc2ebUL, 0x048d7cb2UL, 0x054f1685UL, 0x0e1351b8UL, 0x0fd13b8fUL, ++ 0x0d9785d6UL, 0x0c55efe1UL, 0x091af964UL, 0x08d89353UL, 0x0a9e2d0aUL, ++ 0x0b5c473dUL, 0x1c26a370UL, 0x1de4c947UL, 0x1fa2771eUL, 0x1e601d29UL, ++ 0x1b2f0bacUL, 0x1aed619bUL, 0x18abdfc2UL, 0x1969b5f5UL, 0x1235f2c8UL, ++ 0x13f798ffUL, 0x11b126a6UL, 0x10734c91UL, 0x153c5a14UL, 0x14fe3023UL, ++ 0x16b88e7aUL, 0x177ae44dUL, 0x384d46e0UL, 0x398f2cd7UL, 0x3bc9928eUL, ++ 0x3a0bf8b9UL, 0x3f44ee3cUL, 0x3e86840bUL, 0x3cc03a52UL, 0x3d025065UL, ++ 0x365e1758UL, 0x379c7d6fUL, 0x35dac336UL, 0x3418a901UL, 0x3157bf84UL, ++ 0x3095d5b3UL, 0x32d36beaUL, 0x331101ddUL, 0x246be590UL, 0x25a98fa7UL, ++ 0x27ef31feUL, 0x262d5bc9UL, 0x23624d4cUL, 0x22a0277bUL, 0x20e69922UL, ++ 0x2124f315UL, 0x2a78b428UL, 0x2bbade1fUL, 0x29fc6046UL, 0x283e0a71UL, ++ 0x2d711cf4UL, 0x2cb376c3UL, 0x2ef5c89aUL, 0x2f37a2adUL, 0x709a8dc0UL, ++ 0x7158e7f7UL, 0x731e59aeUL, 0x72dc3399UL, 0x7793251cUL, 0x76514f2bUL, ++ 0x7417f172UL, 0x75d59b45UL, 0x7e89dc78UL, 0x7f4bb64fUL, 0x7d0d0816UL, ++ 0x7ccf6221UL, 0x798074a4UL, 0x78421e93UL, 0x7a04a0caUL, 0x7bc6cafdUL, ++ 0x6cbc2eb0UL, 0x6d7e4487UL, 0x6f38fadeUL, 0x6efa90e9UL, 0x6bb5866cUL, ++ 0x6a77ec5bUL, 0x68315202UL, 0x69f33835UL, 0x62af7f08UL, 0x636d153fUL, ++ 0x612bab66UL, 0x60e9c151UL, 0x65a6d7d4UL, 0x6464bde3UL, 0x662203baUL, ++ 0x67e0698dUL, 0x48d7cb20UL, 0x4915a117UL, 0x4b531f4eUL, 0x4a917579UL, ++ 0x4fde63fcUL, 0x4e1c09cbUL, 0x4c5ab792UL, 0x4d98dda5UL, 0x46c49a98UL, ++ 0x4706f0afUL, 0x45404ef6UL, 0x448224c1UL, 0x41cd3244UL, 0x400f5873UL, ++ 0x4249e62aUL, 0x438b8c1dUL, 0x54f16850UL, 0x55330267UL, 0x5775bc3eUL, ++ 0x56b7d609UL, 0x53f8c08cUL, 0x523aaabbUL, 0x507c14e2UL, 0x51be7ed5UL, ++ 0x5ae239e8UL, 0x5b2053dfUL, 0x5966ed86UL, 0x58a487b1UL, 0x5deb9134UL, ++ 0x5c29fb03UL, 0x5e6f455aUL, 0x5fad2f6dUL, 0xe1351b80UL, 0xe0f771b7UL, ++ 0xe2b1cfeeUL, 0xe373a5d9UL, 0xe63cb35cUL, 0xe7fed96bUL, 0xe5b86732UL, ++ 0xe47a0d05UL, 0xef264a38UL, 0xeee4200fUL, 0xeca29e56UL, 0xed60f461UL, ++ 0xe82fe2e4UL, 0xe9ed88d3UL, 0xebab368aUL, 0xea695cbdUL, 0xfd13b8f0UL, ++ 0xfcd1d2c7UL, 0xfe976c9eUL, 0xff5506a9UL, 0xfa1a102cUL, 0xfbd87a1bUL, ++ 0xf99ec442UL, 0xf85cae75UL, 0xf300e948UL, 0xf2c2837fUL, 0xf0843d26UL, ++ 0xf1465711UL, 0xf4094194UL, 0xf5cb2ba3UL, 0xf78d95faUL, 0xf64fffcdUL, ++ 0xd9785d60UL, 0xd8ba3757UL, 0xdafc890eUL, 0xdb3ee339UL, 0xde71f5bcUL, ++ 0xdfb39f8bUL, 0xddf521d2UL, 0xdc374be5UL, 0xd76b0cd8UL, 0xd6a966efUL, ++ 0xd4efd8b6UL, 0xd52db281UL, 0xd062a404UL, 0xd1a0ce33UL, 0xd3e6706aUL, ++ 0xd2241a5dUL, 0xc55efe10UL, 0xc49c9427UL, 0xc6da2a7eUL, 0xc7184049UL, ++ 0xc25756ccUL, 0xc3953cfbUL, 0xc1d382a2UL, 0xc011e895UL, 0xcb4dafa8UL, ++ 0xca8fc59fUL, 0xc8c97bc6UL, 0xc90b11f1UL, 0xcc440774UL, 0xcd866d43UL, ++ 0xcfc0d31aUL, 0xce02b92dUL, 0x91af9640UL, 0x906dfc77UL, 0x922b422eUL, ++ 0x93e92819UL, 0x96a63e9cUL, 0x976454abUL, 0x9522eaf2UL, 0x94e080c5UL, ++ 0x9fbcc7f8UL, 0x9e7eadcfUL, 0x9c381396UL, 0x9dfa79a1UL, 0x98b56f24UL, ++ 0x99770513UL, 0x9b31bb4aUL, 0x9af3d17dUL, 0x8d893530UL, 0x8c4b5f07UL, ++ 0x8e0de15eUL, 0x8fcf8b69UL, 0x8a809decUL, 0x8b42f7dbUL, 0x89044982UL, ++ 0x88c623b5UL, 0x839a6488UL, 0x82580ebfUL, 0x801eb0e6UL, 0x81dcdad1UL, ++ 0x8493cc54UL, 0x8551a663UL, 0x8717183aUL, 0x86d5720dUL, 0xa9e2d0a0UL, ++ 0xa820ba97UL, 0xaa6604ceUL, 0xaba46ef9UL, 0xaeeb787cUL, 0xaf29124bUL, ++ 0xad6fac12UL, 0xacadc625UL, 0xa7f18118UL, 0xa633eb2fUL, 0xa4755576UL, ++ 0xa5b73f41UL, 0xa0f829c4UL, 0xa13a43f3UL, 0xa37cfdaaUL, 0xa2be979dUL, ++ 0xb5c473d0UL, 0xb40619e7UL, 0xb640a7beUL, 0xb782cd89UL, 0xb2cddb0cUL, ++ 0xb30fb13bUL, 0xb1490f62UL, 0xb08b6555UL, 0xbbd72268UL, 0xba15485fUL, ++ 0xb853f606UL, 0xb9919c31UL, 0xbcde8ab4UL, 0xbd1ce083UL, 0xbf5a5edaUL, ++ 0xbe9834edUL ++ }, ++ { ++ 0x00000000UL, 0xb8bc6765UL, 0xaa09c88bUL, 0x12b5afeeUL, 0x8f629757UL, ++ 0x37def032UL, 0x256b5fdcUL, 0x9dd738b9UL, 0xc5b428efUL, 0x7d084f8aUL, ++ 0x6fbde064UL, 0xd7018701UL, 0x4ad6bfb8UL, 0xf26ad8ddUL, 0xe0df7733UL, ++ 0x58631056UL, 0x5019579fUL, 0xe8a530faUL, 0xfa109f14UL, 0x42acf871UL, ++ 0xdf7bc0c8UL, 0x67c7a7adUL, 0x75720843UL, 0xcdce6f26UL, 0x95ad7f70UL, ++ 0x2d111815UL, 0x3fa4b7fbUL, 0x8718d09eUL, 0x1acfe827UL, 0xa2738f42UL, ++ 0xb0c620acUL, 0x087a47c9UL, 0xa032af3eUL, 0x188ec85bUL, 0x0a3b67b5UL, ++ 0xb28700d0UL, 0x2f503869UL, 0x97ec5f0cUL, 0x8559f0e2UL, 0x3de59787UL, ++ 0x658687d1UL, 0xdd3ae0b4UL, 0xcf8f4f5aUL, 0x7733283fUL, 0xeae41086UL, ++ 0x525877e3UL, 0x40edd80dUL, 0xf851bf68UL, 0xf02bf8a1UL, 0x48979fc4UL, ++ 0x5a22302aUL, 0xe29e574fUL, 0x7f496ff6UL, 0xc7f50893UL, 0xd540a77dUL, ++ 0x6dfcc018UL, 0x359fd04eUL, 0x8d23b72bUL, 0x9f9618c5UL, 0x272a7fa0UL, ++ 0xbafd4719UL, 0x0241207cUL, 0x10f48f92UL, 0xa848e8f7UL, 0x9b14583dUL, ++ 0x23a83f58UL, 0x311d90b6UL, 0x89a1f7d3UL, 0x1476cf6aUL, 0xaccaa80fUL, ++ 0xbe7f07e1UL, 0x06c36084UL, 0x5ea070d2UL, 0xe61c17b7UL, 0xf4a9b859UL, ++ 0x4c15df3cUL, 0xd1c2e785UL, 0x697e80e0UL, 0x7bcb2f0eUL, 0xc377486bUL, ++ 0xcb0d0fa2UL, 0x73b168c7UL, 0x6104c729UL, 0xd9b8a04cUL, 0x446f98f5UL, ++ 0xfcd3ff90UL, 0xee66507eUL, 0x56da371bUL, 0x0eb9274dUL, 0xb6054028UL, ++ 0xa4b0efc6UL, 0x1c0c88a3UL, 0x81dbb01aUL, 0x3967d77fUL, 0x2bd27891UL, ++ 0x936e1ff4UL, 0x3b26f703UL, 0x839a9066UL, 0x912f3f88UL, 0x299358edUL, ++ 0xb4446054UL, 0x0cf80731UL, 0x1e4da8dfUL, 0xa6f1cfbaUL, 0xfe92dfecUL, ++ 0x462eb889UL, 0x549b1767UL, 0xec277002UL, 0x71f048bbUL, 0xc94c2fdeUL, ++ 0xdbf98030UL, 0x6345e755UL, 0x6b3fa09cUL, 0xd383c7f9UL, 0xc1366817UL, ++ 0x798a0f72UL, 0xe45d37cbUL, 0x5ce150aeUL, 0x4e54ff40UL, 0xf6e89825UL, ++ 0xae8b8873UL, 0x1637ef16UL, 0x048240f8UL, 0xbc3e279dUL, 0x21e91f24UL, ++ 0x99557841UL, 0x8be0d7afUL, 0x335cb0caUL, 0xed59b63bUL, 0x55e5d15eUL, ++ 0x47507eb0UL, 0xffec19d5UL, 0x623b216cUL, 0xda874609UL, 0xc832e9e7UL, ++ 0x708e8e82UL, 0x28ed9ed4UL, 0x9051f9b1UL, 0x82e4565fUL, 0x3a58313aUL, ++ 0xa78f0983UL, 0x1f336ee6UL, 0x0d86c108UL, 0xb53aa66dUL, 0xbd40e1a4UL, ++ 0x05fc86c1UL, 0x1749292fUL, 0xaff54e4aUL, 0x322276f3UL, 0x8a9e1196UL, ++ 0x982bbe78UL, 0x2097d91dUL, 0x78f4c94bUL, 0xc048ae2eUL, 0xd2fd01c0UL, ++ 0x6a4166a5UL, 0xf7965e1cUL, 0x4f2a3979UL, 0x5d9f9697UL, 0xe523f1f2UL, ++ 0x4d6b1905UL, 0xf5d77e60UL, 0xe762d18eUL, 0x5fdeb6ebUL, 0xc2098e52UL, ++ 0x7ab5e937UL, 0x680046d9UL, 0xd0bc21bcUL, 0x88df31eaUL, 0x3063568fUL, ++ 0x22d6f961UL, 0x9a6a9e04UL, 0x07bda6bdUL, 0xbf01c1d8UL, 0xadb46e36UL, ++ 0x15080953UL, 0x1d724e9aUL, 0xa5ce29ffUL, 0xb77b8611UL, 0x0fc7e174UL, ++ 0x9210d9cdUL, 0x2aacbea8UL, 0x38191146UL, 0x80a57623UL, 0xd8c66675UL, ++ 0x607a0110UL, 0x72cfaefeUL, 0xca73c99bUL, 0x57a4f122UL, 0xef189647UL, ++ 0xfdad39a9UL, 0x45115eccUL, 0x764dee06UL, 0xcef18963UL, 0xdc44268dUL, ++ 0x64f841e8UL, 0xf92f7951UL, 0x41931e34UL, 0x5326b1daUL, 0xeb9ad6bfUL, ++ 0xb3f9c6e9UL, 0x0b45a18cUL, 0x19f00e62UL, 0xa14c6907UL, 0x3c9b51beUL, ++ 0x842736dbUL, 0x96929935UL, 0x2e2efe50UL, 0x2654b999UL, 0x9ee8defcUL, ++ 0x8c5d7112UL, 0x34e11677UL, 0xa9362eceUL, 0x118a49abUL, 0x033fe645UL, ++ 0xbb838120UL, 0xe3e09176UL, 0x5b5cf613UL, 0x49e959fdUL, 0xf1553e98UL, ++ 0x6c820621UL, 0xd43e6144UL, 0xc68bceaaUL, 0x7e37a9cfUL, 0xd67f4138UL, ++ 0x6ec3265dUL, 0x7c7689b3UL, 0xc4caeed6UL, 0x591dd66fUL, 0xe1a1b10aUL, ++ 0xf3141ee4UL, 0x4ba87981UL, 0x13cb69d7UL, 0xab770eb2UL, 0xb9c2a15cUL, ++ 0x017ec639UL, 0x9ca9fe80UL, 0x241599e5UL, 0x36a0360bUL, 0x8e1c516eUL, ++ 0x866616a7UL, 0x3eda71c2UL, 0x2c6fde2cUL, 0x94d3b949UL, 0x090481f0UL, ++ 0xb1b8e695UL, 0xa30d497bUL, 0x1bb12e1eUL, 0x43d23e48UL, 0xfb6e592dUL, ++ 0xe9dbf6c3UL, 0x516791a6UL, 0xccb0a91fUL, 0x740cce7aUL, 0x66b96194UL, ++ 0xde0506f1UL ++ }, ++ { ++ 0x00000000UL, 0x96300777UL, 0x2c610eeeUL, 0xba510999UL, 0x19c46d07UL, ++ 0x8ff46a70UL, 0x35a563e9UL, 0xa395649eUL, 0x3288db0eUL, 0xa4b8dc79UL, ++ 0x1ee9d5e0UL, 0x88d9d297UL, 0x2b4cb609UL, 0xbd7cb17eUL, 0x072db8e7UL, ++ 0x911dbf90UL, 0x6410b71dUL, 0xf220b06aUL, 0x4871b9f3UL, 0xde41be84UL, ++ 0x7dd4da1aUL, 0xebe4dd6dUL, 0x51b5d4f4UL, 0xc785d383UL, 0x56986c13UL, ++ 0xc0a86b64UL, 0x7af962fdUL, 0xecc9658aUL, 0x4f5c0114UL, 0xd96c0663UL, ++ 0x633d0ffaUL, 0xf50d088dUL, 0xc8206e3bUL, 0x5e10694cUL, 0xe44160d5UL, ++ 0x727167a2UL, 0xd1e4033cUL, 0x47d4044bUL, 0xfd850dd2UL, 0x6bb50aa5UL, ++ 0xfaa8b535UL, 0x6c98b242UL, 0xd6c9bbdbUL, 0x40f9bcacUL, 0xe36cd832UL, ++ 0x755cdf45UL, 0xcf0dd6dcUL, 0x593dd1abUL, 0xac30d926UL, 0x3a00de51UL, ++ 0x8051d7c8UL, 0x1661d0bfUL, 0xb5f4b421UL, 0x23c4b356UL, 0x9995bacfUL, ++ 0x0fa5bdb8UL, 0x9eb80228UL, 0x0888055fUL, 0xb2d90cc6UL, 0x24e90bb1UL, ++ 0x877c6f2fUL, 0x114c6858UL, 0xab1d61c1UL, 0x3d2d66b6UL, 0x9041dc76UL, ++ 0x0671db01UL, 0xbc20d298UL, 0x2a10d5efUL, 0x8985b171UL, 0x1fb5b606UL, ++ 0xa5e4bf9fUL, 0x33d4b8e8UL, 0xa2c90778UL, 0x34f9000fUL, 0x8ea80996UL, ++ 0x18980ee1UL, 0xbb0d6a7fUL, 0x2d3d6d08UL, 0x976c6491UL, 0x015c63e6UL, ++ 0xf4516b6bUL, 0x62616c1cUL, 0xd8306585UL, 0x4e0062f2UL, 0xed95066cUL, ++ 0x7ba5011bUL, 0xc1f40882UL, 0x57c40ff5UL, 0xc6d9b065UL, 0x50e9b712UL, ++ 0xeab8be8bUL, 0x7c88b9fcUL, 0xdf1ddd62UL, 0x492dda15UL, 0xf37cd38cUL, ++ 0x654cd4fbUL, 0x5861b24dUL, 0xce51b53aUL, 0x7400bca3UL, 0xe230bbd4UL, ++ 0x41a5df4aUL, 0xd795d83dUL, 0x6dc4d1a4UL, 0xfbf4d6d3UL, 0x6ae96943UL, ++ 0xfcd96e34UL, 0x468867adUL, 0xd0b860daUL, 0x732d0444UL, 0xe51d0333UL, ++ 0x5f4c0aaaUL, 0xc97c0dddUL, 0x3c710550UL, 0xaa410227UL, 0x10100bbeUL, ++ 0x86200cc9UL, 0x25b56857UL, 0xb3856f20UL, 0x09d466b9UL, 0x9fe461ceUL, ++ 0x0ef9de5eUL, 0x98c9d929UL, 0x2298d0b0UL, 0xb4a8d7c7UL, 0x173db359UL, ++ 0x810db42eUL, 0x3b5cbdb7UL, 0xad6cbac0UL, 0x2083b8edUL, 0xb6b3bf9aUL, ++ 0x0ce2b603UL, 0x9ad2b174UL, 0x3947d5eaUL, 0xaf77d29dUL, 0x1526db04UL, ++ 0x8316dc73UL, 0x120b63e3UL, 0x843b6494UL, 0x3e6a6d0dUL, 0xa85a6a7aUL, ++ 0x0bcf0ee4UL, 0x9dff0993UL, 0x27ae000aUL, 0xb19e077dUL, 0x44930ff0UL, ++ 0xd2a30887UL, 0x68f2011eUL, 0xfec20669UL, 0x5d5762f7UL, 0xcb676580UL, ++ 0x71366c19UL, 0xe7066b6eUL, 0x761bd4feUL, 0xe02bd389UL, 0x5a7ada10UL, ++ 0xcc4add67UL, 0x6fdfb9f9UL, 0xf9efbe8eUL, 0x43beb717UL, 0xd58eb060UL, ++ 0xe8a3d6d6UL, 0x7e93d1a1UL, 0xc4c2d838UL, 0x52f2df4fUL, 0xf167bbd1UL, ++ 0x6757bca6UL, 0xdd06b53fUL, 0x4b36b248UL, 0xda2b0dd8UL, 0x4c1b0aafUL, ++ 0xf64a0336UL, 0x607a0441UL, 0xc3ef60dfUL, 0x55df67a8UL, 0xef8e6e31UL, ++ 0x79be6946UL, 0x8cb361cbUL, 0x1a8366bcUL, 0xa0d26f25UL, 0x36e26852UL, ++ 0x95770cccUL, 0x03470bbbUL, 0xb9160222UL, 0x2f260555UL, 0xbe3bbac5UL, ++ 0x280bbdb2UL, 0x925ab42bUL, 0x046ab35cUL, 0xa7ffd7c2UL, 0x31cfd0b5UL, ++ 0x8b9ed92cUL, 0x1daede5bUL, 0xb0c2649bUL, 0x26f263ecUL, 0x9ca36a75UL, ++ 0x0a936d02UL, 0xa906099cUL, 0x3f360eebUL, 0x85670772UL, 0x13570005UL, ++ 0x824abf95UL, 0x147ab8e2UL, 0xae2bb17bUL, 0x381bb60cUL, 0x9b8ed292UL, ++ 0x0dbed5e5UL, 0xb7efdc7cUL, 0x21dfdb0bUL, 0xd4d2d386UL, 0x42e2d4f1UL, ++ 0xf8b3dd68UL, 0x6e83da1fUL, 0xcd16be81UL, 0x5b26b9f6UL, 0xe177b06fUL, ++ 0x7747b718UL, 0xe65a0888UL, 0x706a0fffUL, 0xca3b0666UL, 0x5c0b0111UL, ++ 0xff9e658fUL, 0x69ae62f8UL, 0xd3ff6b61UL, 0x45cf6c16UL, 0x78e20aa0UL, ++ 0xeed20dd7UL, 0x5483044eUL, 0xc2b30339UL, 0x612667a7UL, 0xf71660d0UL, ++ 0x4d476949UL, 0xdb776e3eUL, 0x4a6ad1aeUL, 0xdc5ad6d9UL, 0x660bdf40UL, ++ 0xf03bd837UL, 0x53aebca9UL, 0xc59ebbdeUL, 0x7fcfb247UL, 0xe9ffb530UL, ++ 0x1cf2bdbdUL, 0x8ac2bacaUL, 0x3093b353UL, 0xa6a3b424UL, 0x0536d0baUL, ++ 0x9306d7cdUL, 0x2957de54UL, 0xbf67d923UL, 0x2e7a66b3UL, 0xb84a61c4UL, ++ 0x021b685dUL, 0x942b6f2aUL, 0x37be0bb4UL, 0xa18e0cc3UL, 0x1bdf055aUL, ++ 0x8def022dUL ++ }, ++ { ++ 0x00000000UL, 0x41311b19UL, 0x82623632UL, 0xc3532d2bUL, 0x04c56c64UL, ++ 0x45f4777dUL, 0x86a75a56UL, 0xc796414fUL, 0x088ad9c8UL, 0x49bbc2d1UL, ++ 0x8ae8effaUL, 0xcbd9f4e3UL, 0x0c4fb5acUL, 0x4d7eaeb5UL, 0x8e2d839eUL, ++ 0xcf1c9887UL, 0x5112c24aUL, 0x1023d953UL, 0xd370f478UL, 0x9241ef61UL, ++ 0x55d7ae2eUL, 0x14e6b537UL, 0xd7b5981cUL, 0x96848305UL, 0x59981b82UL, ++ 0x18a9009bUL, 0xdbfa2db0UL, 0x9acb36a9UL, 0x5d5d77e6UL, 0x1c6c6cffUL, ++ 0xdf3f41d4UL, 0x9e0e5acdUL, 0xa2248495UL, 0xe3159f8cUL, 0x2046b2a7UL, ++ 0x6177a9beUL, 0xa6e1e8f1UL, 0xe7d0f3e8UL, 0x2483dec3UL, 0x65b2c5daUL, ++ 0xaaae5d5dUL, 0xeb9f4644UL, 0x28cc6b6fUL, 0x69fd7076UL, 0xae6b3139UL, ++ 0xef5a2a20UL, 0x2c09070bUL, 0x6d381c12UL, 0xf33646dfUL, 0xb2075dc6UL, ++ 0x715470edUL, 0x30656bf4UL, 0xf7f32abbUL, 0xb6c231a2UL, 0x75911c89UL, ++ 0x34a00790UL, 0xfbbc9f17UL, 0xba8d840eUL, 0x79dea925UL, 0x38efb23cUL, ++ 0xff79f373UL, 0xbe48e86aUL, 0x7d1bc541UL, 0x3c2ade58UL, 0x054f79f0UL, ++ 0x447e62e9UL, 0x872d4fc2UL, 0xc61c54dbUL, 0x018a1594UL, 0x40bb0e8dUL, ++ 0x83e823a6UL, 0xc2d938bfUL, 0x0dc5a038UL, 0x4cf4bb21UL, 0x8fa7960aUL, ++ 0xce968d13UL, 0x0900cc5cUL, 0x4831d745UL, 0x8b62fa6eUL, 0xca53e177UL, ++ 0x545dbbbaUL, 0x156ca0a3UL, 0xd63f8d88UL, 0x970e9691UL, 0x5098d7deUL, ++ 0x11a9ccc7UL, 0xd2fae1ecUL, 0x93cbfaf5UL, 0x5cd76272UL, 0x1de6796bUL, ++ 0xdeb55440UL, 0x9f844f59UL, 0x58120e16UL, 0x1923150fUL, 0xda703824UL, ++ 0x9b41233dUL, 0xa76bfd65UL, 0xe65ae67cUL, 0x2509cb57UL, 0x6438d04eUL, ++ 0xa3ae9101UL, 0xe29f8a18UL, 0x21cca733UL, 0x60fdbc2aUL, 0xafe124adUL, ++ 0xeed03fb4UL, 0x2d83129fUL, 0x6cb20986UL, 0xab2448c9UL, 0xea1553d0UL, ++ 0x29467efbUL, 0x687765e2UL, 0xf6793f2fUL, 0xb7482436UL, 0x741b091dUL, ++ 0x352a1204UL, 0xf2bc534bUL, 0xb38d4852UL, 0x70de6579UL, 0x31ef7e60UL, ++ 0xfef3e6e7UL, 0xbfc2fdfeUL, 0x7c91d0d5UL, 0x3da0cbccUL, 0xfa368a83UL, ++ 0xbb07919aUL, 0x7854bcb1UL, 0x3965a7a8UL, 0x4b98833bUL, 0x0aa99822UL, ++ 0xc9fab509UL, 0x88cbae10UL, 0x4f5def5fUL, 0x0e6cf446UL, 0xcd3fd96dUL, ++ 0x8c0ec274UL, 0x43125af3UL, 0x022341eaUL, 0xc1706cc1UL, 0x804177d8UL, ++ 0x47d73697UL, 0x06e62d8eUL, 0xc5b500a5UL, 0x84841bbcUL, 0x1a8a4171UL, ++ 0x5bbb5a68UL, 0x98e87743UL, 0xd9d96c5aUL, 0x1e4f2d15UL, 0x5f7e360cUL, ++ 0x9c2d1b27UL, 0xdd1c003eUL, 0x120098b9UL, 0x533183a0UL, 0x9062ae8bUL, ++ 0xd153b592UL, 0x16c5f4ddUL, 0x57f4efc4UL, 0x94a7c2efUL, 0xd596d9f6UL, ++ 0xe9bc07aeUL, 0xa88d1cb7UL, 0x6bde319cUL, 0x2aef2a85UL, 0xed796bcaUL, ++ 0xac4870d3UL, 0x6f1b5df8UL, 0x2e2a46e1UL, 0xe136de66UL, 0xa007c57fUL, ++ 0x6354e854UL, 0x2265f34dUL, 0xe5f3b202UL, 0xa4c2a91bUL, 0x67918430UL, ++ 0x26a09f29UL, 0xb8aec5e4UL, 0xf99fdefdUL, 0x3accf3d6UL, 0x7bfde8cfUL, ++ 0xbc6ba980UL, 0xfd5ab299UL, 0x3e099fb2UL, 0x7f3884abUL, 0xb0241c2cUL, ++ 0xf1150735UL, 0x32462a1eUL, 0x73773107UL, 0xb4e17048UL, 0xf5d06b51UL, ++ 0x3683467aUL, 0x77b25d63UL, 0x4ed7facbUL, 0x0fe6e1d2UL, 0xccb5ccf9UL, ++ 0x8d84d7e0UL, 0x4a1296afUL, 0x0b238db6UL, 0xc870a09dUL, 0x8941bb84UL, ++ 0x465d2303UL, 0x076c381aUL, 0xc43f1531UL, 0x850e0e28UL, 0x42984f67UL, ++ 0x03a9547eUL, 0xc0fa7955UL, 0x81cb624cUL, 0x1fc53881UL, 0x5ef42398UL, ++ 0x9da70eb3UL, 0xdc9615aaUL, 0x1b0054e5UL, 0x5a314ffcUL, 0x996262d7UL, ++ 0xd85379ceUL, 0x174fe149UL, 0x567efa50UL, 0x952dd77bUL, 0xd41ccc62UL, ++ 0x138a8d2dUL, 0x52bb9634UL, 0x91e8bb1fUL, 0xd0d9a006UL, 0xecf37e5eUL, ++ 0xadc26547UL, 0x6e91486cUL, 0x2fa05375UL, 0xe836123aUL, 0xa9070923UL, ++ 0x6a542408UL, 0x2b653f11UL, 0xe479a796UL, 0xa548bc8fUL, 0x661b91a4UL, ++ 0x272a8abdUL, 0xe0bccbf2UL, 0xa18dd0ebUL, 0x62defdc0UL, 0x23efe6d9UL, ++ 0xbde1bc14UL, 0xfcd0a70dUL, 0x3f838a26UL, 0x7eb2913fUL, 0xb924d070UL, ++ 0xf815cb69UL, 0x3b46e642UL, 0x7a77fd5bUL, 0xb56b65dcUL, 0xf45a7ec5UL, ++ 0x370953eeUL, 0x763848f7UL, 0xb1ae09b8UL, 0xf09f12a1UL, 0x33cc3f8aUL, ++ 0x72fd2493UL ++ }, ++ { ++ 0x00000000UL, 0x376ac201UL, 0x6ed48403UL, 0x59be4602UL, 0xdca80907UL, ++ 0xebc2cb06UL, 0xb27c8d04UL, 0x85164f05UL, 0xb851130eUL, 0x8f3bd10fUL, ++ 0xd685970dUL, 0xe1ef550cUL, 0x64f91a09UL, 0x5393d808UL, 0x0a2d9e0aUL, ++ 0x3d475c0bUL, 0x70a3261cUL, 0x47c9e41dUL, 0x1e77a21fUL, 0x291d601eUL, ++ 0xac0b2f1bUL, 0x9b61ed1aUL, 0xc2dfab18UL, 0xf5b56919UL, 0xc8f23512UL, ++ 0xff98f713UL, 0xa626b111UL, 0x914c7310UL, 0x145a3c15UL, 0x2330fe14UL, ++ 0x7a8eb816UL, 0x4de47a17UL, 0xe0464d38UL, 0xd72c8f39UL, 0x8e92c93bUL, ++ 0xb9f80b3aUL, 0x3cee443fUL, 0x0b84863eUL, 0x523ac03cUL, 0x6550023dUL, ++ 0x58175e36UL, 0x6f7d9c37UL, 0x36c3da35UL, 0x01a91834UL, 0x84bf5731UL, ++ 0xb3d59530UL, 0xea6bd332UL, 0xdd011133UL, 0x90e56b24UL, 0xa78fa925UL, ++ 0xfe31ef27UL, 0xc95b2d26UL, 0x4c4d6223UL, 0x7b27a022UL, 0x2299e620UL, ++ 0x15f32421UL, 0x28b4782aUL, 0x1fdeba2bUL, 0x4660fc29UL, 0x710a3e28UL, ++ 0xf41c712dUL, 0xc376b32cUL, 0x9ac8f52eUL, 0xada2372fUL, 0xc08d9a70UL, ++ 0xf7e75871UL, 0xae591e73UL, 0x9933dc72UL, 0x1c259377UL, 0x2b4f5176UL, ++ 0x72f11774UL, 0x459bd575UL, 0x78dc897eUL, 0x4fb64b7fUL, 0x16080d7dUL, ++ 0x2162cf7cUL, 0xa4748079UL, 0x931e4278UL, 0xcaa0047aUL, 0xfdcac67bUL, ++ 0xb02ebc6cUL, 0x87447e6dUL, 0xdefa386fUL, 0xe990fa6eUL, 0x6c86b56bUL, ++ 0x5bec776aUL, 0x02523168UL, 0x3538f369UL, 0x087faf62UL, 0x3f156d63UL, ++ 0x66ab2b61UL, 0x51c1e960UL, 0xd4d7a665UL, 0xe3bd6464UL, 0xba032266UL, ++ 0x8d69e067UL, 0x20cbd748UL, 0x17a11549UL, 0x4e1f534bUL, 0x7975914aUL, ++ 0xfc63de4fUL, 0xcb091c4eUL, 0x92b75a4cUL, 0xa5dd984dUL, 0x989ac446UL, ++ 0xaff00647UL, 0xf64e4045UL, 0xc1248244UL, 0x4432cd41UL, 0x73580f40UL, ++ 0x2ae64942UL, 0x1d8c8b43UL, 0x5068f154UL, 0x67023355UL, 0x3ebc7557UL, ++ 0x09d6b756UL, 0x8cc0f853UL, 0xbbaa3a52UL, 0xe2147c50UL, 0xd57ebe51UL, ++ 0xe839e25aUL, 0xdf53205bUL, 0x86ed6659UL, 0xb187a458UL, 0x3491eb5dUL, ++ 0x03fb295cUL, 0x5a456f5eUL, 0x6d2fad5fUL, 0x801b35e1UL, 0xb771f7e0UL, ++ 0xeecfb1e2UL, 0xd9a573e3UL, 0x5cb33ce6UL, 0x6bd9fee7UL, 0x3267b8e5UL, ++ 0x050d7ae4UL, 0x384a26efUL, 0x0f20e4eeUL, 0x569ea2ecUL, 0x61f460edUL, ++ 0xe4e22fe8UL, 0xd388ede9UL, 0x8a36abebUL, 0xbd5c69eaUL, 0xf0b813fdUL, ++ 0xc7d2d1fcUL, 0x9e6c97feUL, 0xa90655ffUL, 0x2c101afaUL, 0x1b7ad8fbUL, ++ 0x42c49ef9UL, 0x75ae5cf8UL, 0x48e900f3UL, 0x7f83c2f2UL, 0x263d84f0UL, ++ 0x115746f1UL, 0x944109f4UL, 0xa32bcbf5UL, 0xfa958df7UL, 0xcdff4ff6UL, ++ 0x605d78d9UL, 0x5737bad8UL, 0x0e89fcdaUL, 0x39e33edbUL, 0xbcf571deUL, ++ 0x8b9fb3dfUL, 0xd221f5ddUL, 0xe54b37dcUL, 0xd80c6bd7UL, 0xef66a9d6UL, ++ 0xb6d8efd4UL, 0x81b22dd5UL, 0x04a462d0UL, 0x33cea0d1UL, 0x6a70e6d3UL, ++ 0x5d1a24d2UL, 0x10fe5ec5UL, 0x27949cc4UL, 0x7e2adac6UL, 0x494018c7UL, ++ 0xcc5657c2UL, 0xfb3c95c3UL, 0xa282d3c1UL, 0x95e811c0UL, 0xa8af4dcbUL, ++ 0x9fc58fcaUL, 0xc67bc9c8UL, 0xf1110bc9UL, 0x740744ccUL, 0x436d86cdUL, ++ 0x1ad3c0cfUL, 0x2db902ceUL, 0x4096af91UL, 0x77fc6d90UL, 0x2e422b92UL, ++ 0x1928e993UL, 0x9c3ea696UL, 0xab546497UL, 0xf2ea2295UL, 0xc580e094UL, ++ 0xf8c7bc9fUL, 0xcfad7e9eUL, 0x9613389cUL, 0xa179fa9dUL, 0x246fb598UL, ++ 0x13057799UL, 0x4abb319bUL, 0x7dd1f39aUL, 0x3035898dUL, 0x075f4b8cUL, ++ 0x5ee10d8eUL, 0x698bcf8fUL, 0xec9d808aUL, 0xdbf7428bUL, 0x82490489UL, ++ 0xb523c688UL, 0x88649a83UL, 0xbf0e5882UL, 0xe6b01e80UL, 0xd1dadc81UL, ++ 0x54cc9384UL, 0x63a65185UL, 0x3a181787UL, 0x0d72d586UL, 0xa0d0e2a9UL, ++ 0x97ba20a8UL, 0xce0466aaUL, 0xf96ea4abUL, 0x7c78ebaeUL, 0x4b1229afUL, ++ 0x12ac6fadUL, 0x25c6adacUL, 0x1881f1a7UL, 0x2feb33a6UL, 0x765575a4UL, ++ 0x413fb7a5UL, 0xc429f8a0UL, 0xf3433aa1UL, 0xaafd7ca3UL, 0x9d97bea2UL, ++ 0xd073c4b5UL, 0xe71906b4UL, 0xbea740b6UL, 0x89cd82b7UL, 0x0cdbcdb2UL, ++ 0x3bb10fb3UL, 0x620f49b1UL, 0x55658bb0UL, 0x6822d7bbUL, 0x5f4815baUL, ++ 0x06f653b8UL, 0x319c91b9UL, 0xb48adebcUL, 0x83e01cbdUL, 0xda5e5abfUL, ++ 0xed3498beUL ++ }, ++ { ++ 0x00000000UL, 0x6567bcb8UL, 0x8bc809aaUL, 0xeeafb512UL, 0x5797628fUL, ++ 0x32f0de37UL, 0xdc5f6b25UL, 0xb938d79dUL, 0xef28b4c5UL, 0x8a4f087dUL, ++ 0x64e0bd6fUL, 0x018701d7UL, 0xb8bfd64aUL, 0xddd86af2UL, 0x3377dfe0UL, ++ 0x56106358UL, 0x9f571950UL, 0xfa30a5e8UL, 0x149f10faUL, 0x71f8ac42UL, ++ 0xc8c07bdfUL, 0xada7c767UL, 0x43087275UL, 0x266fcecdUL, 0x707fad95UL, ++ 0x1518112dUL, 0xfbb7a43fUL, 0x9ed01887UL, 0x27e8cf1aUL, 0x428f73a2UL, ++ 0xac20c6b0UL, 0xc9477a08UL, 0x3eaf32a0UL, 0x5bc88e18UL, 0xb5673b0aUL, ++ 0xd00087b2UL, 0x6938502fUL, 0x0c5fec97UL, 0xe2f05985UL, 0x8797e53dUL, ++ 0xd1878665UL, 0xb4e03addUL, 0x5a4f8fcfUL, 0x3f283377UL, 0x8610e4eaUL, ++ 0xe3775852UL, 0x0dd8ed40UL, 0x68bf51f8UL, 0xa1f82bf0UL, 0xc49f9748UL, ++ 0x2a30225aUL, 0x4f579ee2UL, 0xf66f497fUL, 0x9308f5c7UL, 0x7da740d5UL, ++ 0x18c0fc6dUL, 0x4ed09f35UL, 0x2bb7238dUL, 0xc518969fUL, 0xa07f2a27UL, ++ 0x1947fdbaUL, 0x7c204102UL, 0x928ff410UL, 0xf7e848a8UL, 0x3d58149bUL, ++ 0x583fa823UL, 0xb6901d31UL, 0xd3f7a189UL, 0x6acf7614UL, 0x0fa8caacUL, ++ 0xe1077fbeUL, 0x8460c306UL, 0xd270a05eUL, 0xb7171ce6UL, 0x59b8a9f4UL, ++ 0x3cdf154cUL, 0x85e7c2d1UL, 0xe0807e69UL, 0x0e2fcb7bUL, 0x6b4877c3UL, ++ 0xa20f0dcbUL, 0xc768b173UL, 0x29c70461UL, 0x4ca0b8d9UL, 0xf5986f44UL, ++ 0x90ffd3fcUL, 0x7e5066eeUL, 0x1b37da56UL, 0x4d27b90eUL, 0x284005b6UL, ++ 0xc6efb0a4UL, 0xa3880c1cUL, 0x1ab0db81UL, 0x7fd76739UL, 0x9178d22bUL, ++ 0xf41f6e93UL, 0x03f7263bUL, 0x66909a83UL, 0x883f2f91UL, 0xed589329UL, ++ 0x546044b4UL, 0x3107f80cUL, 0xdfa84d1eUL, 0xbacff1a6UL, 0xecdf92feUL, ++ 0x89b82e46UL, 0x67179b54UL, 0x027027ecUL, 0xbb48f071UL, 0xde2f4cc9UL, ++ 0x3080f9dbUL, 0x55e74563UL, 0x9ca03f6bUL, 0xf9c783d3UL, 0x176836c1UL, ++ 0x720f8a79UL, 0xcb375de4UL, 0xae50e15cUL, 0x40ff544eUL, 0x2598e8f6UL, ++ 0x73888baeUL, 0x16ef3716UL, 0xf8408204UL, 0x9d273ebcUL, 0x241fe921UL, ++ 0x41785599UL, 0xafd7e08bUL, 0xcab05c33UL, 0x3bb659edUL, 0x5ed1e555UL, ++ 0xb07e5047UL, 0xd519ecffUL, 0x6c213b62UL, 0x094687daUL, 0xe7e932c8UL, ++ 0x828e8e70UL, 0xd49eed28UL, 0xb1f95190UL, 0x5f56e482UL, 0x3a31583aUL, ++ 0x83098fa7UL, 0xe66e331fUL, 0x08c1860dUL, 0x6da63ab5UL, 0xa4e140bdUL, ++ 0xc186fc05UL, 0x2f294917UL, 0x4a4ef5afUL, 0xf3762232UL, 0x96119e8aUL, ++ 0x78be2b98UL, 0x1dd99720UL, 0x4bc9f478UL, 0x2eae48c0UL, 0xc001fdd2UL, ++ 0xa566416aUL, 0x1c5e96f7UL, 0x79392a4fUL, 0x97969f5dUL, 0xf2f123e5UL, ++ 0x05196b4dUL, 0x607ed7f5UL, 0x8ed162e7UL, 0xebb6de5fUL, 0x528e09c2UL, ++ 0x37e9b57aUL, 0xd9460068UL, 0xbc21bcd0UL, 0xea31df88UL, 0x8f566330UL, ++ 0x61f9d622UL, 0x049e6a9aUL, 0xbda6bd07UL, 0xd8c101bfUL, 0x366eb4adUL, ++ 0x53090815UL, 0x9a4e721dUL, 0xff29cea5UL, 0x11867bb7UL, 0x74e1c70fUL, ++ 0xcdd91092UL, 0xa8beac2aUL, 0x46111938UL, 0x2376a580UL, 0x7566c6d8UL, ++ 0x10017a60UL, 0xfeaecf72UL, 0x9bc973caUL, 0x22f1a457UL, 0x479618efUL, ++ 0xa939adfdUL, 0xcc5e1145UL, 0x06ee4d76UL, 0x6389f1ceUL, 0x8d2644dcUL, ++ 0xe841f864UL, 0x51792ff9UL, 0x341e9341UL, 0xdab12653UL, 0xbfd69aebUL, ++ 0xe9c6f9b3UL, 0x8ca1450bUL, 0x620ef019UL, 0x07694ca1UL, 0xbe519b3cUL, ++ 0xdb362784UL, 0x35999296UL, 0x50fe2e2eUL, 0x99b95426UL, 0xfcdee89eUL, ++ 0x12715d8cUL, 0x7716e134UL, 0xce2e36a9UL, 0xab498a11UL, 0x45e63f03UL, ++ 0x208183bbUL, 0x7691e0e3UL, 0x13f65c5bUL, 0xfd59e949UL, 0x983e55f1UL, ++ 0x2106826cUL, 0x44613ed4UL, 0xaace8bc6UL, 0xcfa9377eUL, 0x38417fd6UL, ++ 0x5d26c36eUL, 0xb389767cUL, 0xd6eecac4UL, 0x6fd61d59UL, 0x0ab1a1e1UL, ++ 0xe41e14f3UL, 0x8179a84bUL, 0xd769cb13UL, 0xb20e77abUL, 0x5ca1c2b9UL, ++ 0x39c67e01UL, 0x80fea99cUL, 0xe5991524UL, 0x0b36a036UL, 0x6e511c8eUL, ++ 0xa7166686UL, 0xc271da3eUL, 0x2cde6f2cUL, 0x49b9d394UL, 0xf0810409UL, ++ 0x95e6b8b1UL, 0x7b490da3UL, 0x1e2eb11bUL, 0x483ed243UL, 0x2d596efbUL, ++ 0xc3f6dbe9UL, 0xa6916751UL, 0x1fa9b0ccUL, 0x7ace0c74UL, 0x9461b966UL, ++ 0xf10605deUL ++#endif ++ } ++}; +diff --git a/lib/libzlib/deflate.c b/lib/libzlib/deflate.c +new file mode 100644 +index 000000000..4a8d77336 +--- /dev/null ++++ b/lib/libzlib/deflate.c +@@ -0,0 +1,2165 @@ ++// SPDX-License-Identifier: Zlib ++/* deflate.c -- compress data using the deflation algorithm ++ * Copyright (C) 1995-2017 Jean-loup Gailly and Mark Adler ++ * For conditions of distribution and use, see copyright notice in zlib.h ++ */ ++ ++/* ++ * ALGORITHM ++ * ++ * The "deflation" process depends on being able to identify portions ++ * of the input text which are identical to earlier input (within a ++ * sliding window trailing behind the input currently being processed). ++ * ++ * The most straightforward technique turns out to be the fastest for ++ * most input files: try all possible matches and select the longest. ++ * The key feature of this algorithm is that insertions into the string ++ * dictionary are very simple and thus fast, and deletions are avoided ++ * completely. Insertions are performed at each input character, whereas ++ * string matches are performed only when the previous match ends. So it ++ * is preferable to spend more time in matches to allow very fast string ++ * insertions and avoid deletions. The matching algorithm for small ++ * strings is inspired from that of Rabin & Karp. A brute force approach ++ * is used to find longer strings when a small match has been found. ++ * A similar algorithm is used in comic (by Jan-Mark Wams) and freeze ++ * (by Leonid Broukhis). ++ * A previous version of this file used a more sophisticated algorithm ++ * (by Fiala and Greene) which is guaranteed to run in linear amortized ++ * time, but has a larger average cost, uses more memory and is patented. ++ * However the F&G algorithm may be faster for some highly redundant ++ * files if the parameter max_chain_length (described below) is too large. ++ * ++ * ACKNOWLEDGEMENTS ++ * ++ * The idea of lazy evaluation of matches is due to Jan-Mark Wams, and ++ * I found it in 'freeze' written by Leonid Broukhis. ++ * Thanks to many people for bug reports and testing. ++ * ++ * REFERENCES ++ * ++ * Deutsch, L.P.,"DEFLATE Compressed Data Format Specification". ++ * Available in http://tools.ietf.org/html/rfc1951 ++ * ++ * A description of the Rabin and Karp algorithm is given in the book ++ * "Algorithms" by R. Sedgewick, Addison-Wesley, p252. ++ * ++ * Fiala,E.R., and Greene,D.H. ++ * Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595 ++ * ++ */ ++ ++/* @(#) $Id$ */ ++ ++#include "deflate.h" ++ ++const char deflate_copyright[] = ++ " deflate 1.2.11 Copyright 1995-2017 Jean-loup Gailly and Mark Adler "; ++/* ++ If you use the zlib library in a product, an acknowledgment is welcome ++ in the documentation of your product. If for some reason you cannot ++ include such an acknowledgment, I would appreciate that you keep this ++ copyright string in the executable of your product. ++ */ ++ ++/* =========================================================================== ++ * Function prototypes. ++ */ ++typedef enum { ++ need_more, /* block not completed, need more input or more output */ ++ block_done, /* block flush performed */ ++ finish_started, /* finish started, need only more output at next deflate */ ++ finish_done /* finish done, accept no more input or output */ ++} block_state; ++ ++typedef block_state (*compress_func) OF((deflate_state *s, int flush)); ++/* Compression function. Returns the block state after the call. */ ++ ++local int deflateStateCheck OF((z_streamp strm)); ++local void slide_hash OF((deflate_state *s)); ++local void fill_window OF((deflate_state *s)); ++local block_state deflate_stored OF((deflate_state *s, int flush)); ++local block_state deflate_fast OF((deflate_state *s, int flush)); ++#ifndef FASTEST ++local block_state deflate_slow OF((deflate_state *s, int flush)); ++#endif ++local block_state deflate_rle OF((deflate_state *s, int flush)); ++local block_state deflate_huff OF((deflate_state *s, int flush)); ++local void lm_init OF((deflate_state *s)); ++local void putShortMSB OF((deflate_state *s, uInt b)); ++local void flush_pending OF((z_streamp strm)); ++local unsigned read_buf OF((z_streamp strm, Bytef *buf, unsigned size)); ++#ifdef ASMV ++# pragma message("Assembler code may have bugs -- use at your own risk") ++ void match_init OF((void)); /* asm code initialization */ ++ uInt longest_match OF((deflate_state *s, IPos cur_match)); ++#else ++local uInt longest_match OF((deflate_state *s, IPos cur_match)); ++#endif ++ ++#ifdef ZLIB_DEBUG ++local void check_match OF((deflate_state *s, IPos start, IPos match, ++ int length)); ++#endif ++ ++/* =========================================================================== ++ * Local data ++ */ ++ ++#define NIL 0 ++/* Tail of hash chains */ ++ ++#ifndef TOO_FAR ++# define TOO_FAR 4096 ++#endif ++/* Matches of length 3 are discarded if their distance exceeds TOO_FAR */ ++ ++/* Values for max_lazy_match, good_match and max_chain_length, depending on ++ * the desired pack level (0..9). The values given below have been tuned to ++ * exclude worst case performance for pathological files. Better values may be ++ * found for specific files. ++ */ ++typedef struct config_s { ++ ush good_length; /* reduce lazy search above this match length */ ++ ush max_lazy; /* do not perform lazy search above this match length */ ++ ush nice_length; /* quit search above this match length */ ++ ush max_chain; ++ compress_func func; ++} config; ++ ++#ifdef FASTEST ++local const config configuration_table[2] = { ++/* good lazy nice chain */ ++/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */ ++/* 1 */ {4, 4, 8, 4, deflate_fast}}; /* max speed, no lazy matches */ ++#else ++local const config configuration_table[10] = { ++/* good lazy nice chain */ ++/* 0 */ {0, 0, 0, 0, deflate_stored}, /* store only */ ++/* 1 */ {4, 4, 8, 4, deflate_fast}, /* max speed, no lazy matches */ ++/* 2 */ {4, 5, 16, 8, deflate_fast}, ++/* 3 */ {4, 6, 32, 32, deflate_fast}, ++ ++/* 4 */ {4, 4, 16, 16, deflate_slow}, /* lazy matches */ ++/* 5 */ {8, 16, 32, 32, deflate_slow}, ++/* 6 */ {8, 16, 128, 128, deflate_slow}, ++/* 7 */ {8, 32, 128, 256, deflate_slow}, ++/* 8 */ {32, 128, 258, 1024, deflate_slow}, ++/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* max compression */ ++#endif ++ ++/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4 ++ * For deflate_fast() (levels <= 3) good is ignored and lazy has a different ++ * meaning. ++ */ ++ ++/* rank Z_BLOCK between Z_NO_FLUSH and Z_PARTIAL_FLUSH */ ++#define RANK(f) (((f) * 2) - ((f) > 4 ? 9 : 0)) ++ ++/* =========================================================================== ++ * Update a hash value with the given input byte ++ * IN assertion: all calls to UPDATE_HASH are made with consecutive input ++ * characters, so that a running hash key can be computed from the previous ++ * key instead of complete recalculation each time. ++ */ ++#define UPDATE_HASH(s,h,c) (h = (((h)<hash_shift) ^ (c)) & s->hash_mask) ++ ++ ++/* =========================================================================== ++ * Insert string str in the dictionary and set match_head to the previous head ++ * of the hash chain (the most recent string with same hash key). Return ++ * the previous length of the hash chain. ++ * If this file is compiled with -DFASTEST, the compression level is forced ++ * to 1, and no hash chains are maintained. ++ * IN assertion: all calls to INSERT_STRING are made with consecutive input ++ * characters and the first MIN_MATCH bytes of str are valid (except for ++ * the last MIN_MATCH-1 bytes of the input file). ++ */ ++#ifdef FASTEST ++#define INSERT_STRING(s, str, match_head) \ ++ (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \ ++ match_head = s->head[s->ins_h], \ ++ s->head[s->ins_h] = (Pos)(str)) ++#else ++#define INSERT_STRING(s, str, match_head) \ ++ (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \ ++ match_head = s->prev[(str) & s->w_mask] = s->head[s->ins_h], \ ++ s->head[s->ins_h] = (Pos)(str)) ++#endif ++ ++/* =========================================================================== ++ * Initialize the hash table (avoiding 64K overflow for 16 bit systems). ++ * prev[] will be initialized on the fly. ++ */ ++#define CLEAR_HASH(s) \ ++ s->head[s->hash_size-1] = NIL; \ ++ zmemzero((Bytef *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head)); ++ ++/* =========================================================================== ++ * Slide the hash table when sliding the window down (could be avoided with 32 ++ * bit values at the expense of memory usage). We slide even when level == 0 to ++ * keep the hash table consistent if we switch back to level > 0 later. ++ */ ++local void slide_hash(s) ++ deflate_state *s; ++{ ++ unsigned n, m; ++ Posf *p; ++ uInt wsize = s->w_size; ++ ++ n = s->hash_size; ++ p = &s->head[n]; ++ do { ++ m = *--p; ++ *p = (Pos)(m >= wsize ? m - wsize : NIL); ++ } while (--n); ++ n = wsize; ++#ifndef FASTEST ++ p = &s->prev[n]; ++ do { ++ m = *--p; ++ *p = (Pos)(m >= wsize ? m - wsize : NIL); ++ /* If n is not on any hash chain, prev[n] is garbage but ++ * its value will never be used. ++ */ ++ } while (--n); ++#endif ++} ++ ++/* ========================================================================= */ ++int ZEXPORT deflateInit_(strm, level, version, stream_size) ++ z_streamp strm; ++ int level; ++ const char *version; ++ int stream_size; ++{ ++ return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL, ++ Z_DEFAULT_STRATEGY, version, stream_size); ++ /* To do: ignore strm->next_in if we use it as window */ ++} ++ ++/* ========================================================================= */ ++int ZEXPORT deflateInit2_(strm, level, method, windowBits, memLevel, strategy, ++ version, stream_size) ++ z_streamp strm; ++ int level; ++ int method; ++ int windowBits; ++ int memLevel; ++ int strategy; ++ const char *version; ++ int stream_size; ++{ ++ deflate_state *s; ++ int wrap = 1; ++ static const char my_version[] = ZLIB_VERSION; ++ ++ ushf *overlay; ++ /* We overlay pending_buf and d_buf+l_buf. This works since the average ++ * output size for (length,distance) codes is <= 24 bits. ++ */ ++ ++ if (version == Z_NULL || version[0] != my_version[0] || ++ stream_size != sizeof(z_stream)) { ++ return Z_VERSION_ERROR; ++ } ++ if (strm == Z_NULL) return Z_STREAM_ERROR; ++ ++ strm->msg = Z_NULL; ++ if (strm->zalloc == (alloc_func)0) { ++#ifdef Z_SOLO ++ return Z_STREAM_ERROR; ++#else ++ strm->zalloc = zcalloc; ++ strm->opaque = (voidpf)0; ++#endif ++ } ++ if (strm->zfree == (free_func)0) ++#ifdef Z_SOLO ++ return Z_STREAM_ERROR; ++#else ++ strm->zfree = zcfree; ++#endif ++ ++#ifdef FASTEST ++ if (level != 0) level = 1; ++#else ++ if (level == Z_DEFAULT_COMPRESSION) level = 6; ++#endif ++ ++ if (windowBits < 0) { /* suppress zlib wrapper */ ++ wrap = 0; ++ windowBits = -windowBits; ++ } ++#ifdef GZIP ++ else if (windowBits > 15) { ++ wrap = 2; /* write gzip wrapper instead */ ++ windowBits -= 16; ++ } ++#endif ++ if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED || ++ windowBits < 8 || windowBits > 15 || level < 0 || level > 9 || ++ strategy < 0 || strategy > Z_FIXED || (windowBits == 8 && wrap != 1)) { ++ return Z_STREAM_ERROR; ++ } ++ if (windowBits == 8) windowBits = 9; /* until 256-byte window bug fixed */ ++ s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state)); ++ if (s == Z_NULL) return Z_MEM_ERROR; ++ strm->state = (struct internal_state FAR *)s; ++ s->strm = strm; ++ s->status = INIT_STATE; /* to pass state test in deflateReset() */ ++ ++ s->wrap = wrap; ++ s->gzhead = Z_NULL; ++ s->w_bits = (uInt)windowBits; ++ s->w_size = 1 << s->w_bits; ++ s->w_mask = s->w_size - 1; ++ ++ s->hash_bits = (uInt)memLevel + 7; ++ s->hash_size = 1 << s->hash_bits; ++ s->hash_mask = s->hash_size - 1; ++ s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH); ++ ++ s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte)); ++ s->prev = (Posf *) ZALLOC(strm, s->w_size, sizeof(Pos)); ++ s->head = (Posf *) ZALLOC(strm, s->hash_size, sizeof(Pos)); ++ ++ s->high_water = 0; /* nothing written to s->window yet */ ++ ++ s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */ ++ ++ overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2); ++ s->pending_buf = (uchf *) overlay; ++ s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L); ++ ++ if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL || ++ s->pending_buf == Z_NULL) { ++ s->status = FINISH_STATE; ++ strm->msg = ERR_MSG(Z_MEM_ERROR); ++ deflateEnd (strm); ++ return Z_MEM_ERROR; ++ } ++ s->d_buf = overlay + s->lit_bufsize/sizeof(ush); ++ s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize; ++ ++ s->level = level; ++ s->strategy = strategy; ++ s->method = (Byte)method; ++ ++ return deflateReset(strm); ++} ++ ++/* ========================================================================= ++ * Check for a valid deflate stream state. Return 0 if ok, 1 if not. ++ */ ++local int deflateStateCheck (strm) ++ z_streamp strm; ++{ ++ deflate_state *s; ++ if (strm == Z_NULL || ++ strm->zalloc == (alloc_func)0 || strm->zfree == (free_func)0) ++ return 1; ++ s = strm->state; ++ if (s == Z_NULL || s->strm != strm || (s->status != INIT_STATE && ++#ifdef GZIP ++ s->status != GZIP_STATE && ++#endif ++ s->status != EXTRA_STATE && ++ s->status != NAME_STATE && ++ s->status != COMMENT_STATE && ++ s->status != HCRC_STATE && ++ s->status != BUSY_STATE && ++ s->status != FINISH_STATE)) ++ return 1; ++ return 0; ++} ++ ++/* ========================================================================= */ ++int ZEXPORT deflateSetDictionary (strm, dictionary, dictLength) ++ z_streamp strm; ++ const Bytef *dictionary; ++ uInt dictLength; ++{ ++ deflate_state *s; ++ uInt str, n; ++ int wrap; ++ unsigned avail; ++ z_const unsigned char *next; ++ ++ if (deflateStateCheck(strm) || dictionary == Z_NULL) ++ return Z_STREAM_ERROR; ++ s = strm->state; ++ wrap = s->wrap; ++ if (wrap == 2 || (wrap == 1 && s->status != INIT_STATE) || s->lookahead) ++ return Z_STREAM_ERROR; ++ ++ /* when using zlib wrappers, compute Adler-32 for provided dictionary */ ++ if (wrap == 1) ++ strm->adler = adler32(strm->adler, dictionary, dictLength); ++ s->wrap = 0; /* avoid computing Adler-32 in read_buf */ ++ ++ /* if dictionary would fill window, just replace the history */ ++ if (dictLength >= s->w_size) { ++ if (wrap == 0) { /* already empty otherwise */ ++ CLEAR_HASH(s); ++ s->strstart = 0; ++ s->block_start = 0L; ++ s->insert = 0; ++ } ++ dictionary += dictLength - s->w_size; /* use the tail */ ++ dictLength = s->w_size; ++ } ++ ++ /* insert dictionary into window and hash */ ++ avail = strm->avail_in; ++ next = strm->next_in; ++ strm->avail_in = dictLength; ++ strm->next_in = (z_const Bytef *)dictionary; ++ fill_window(s); ++ while (s->lookahead >= MIN_MATCH) { ++ str = s->strstart; ++ n = s->lookahead - (MIN_MATCH-1); ++ do { ++ UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); ++#ifndef FASTEST ++ s->prev[str & s->w_mask] = s->head[s->ins_h]; ++#endif ++ s->head[s->ins_h] = (Pos)str; ++ str++; ++ } while (--n); ++ s->strstart = str; ++ s->lookahead = MIN_MATCH-1; ++ fill_window(s); ++ } ++ s->strstart += s->lookahead; ++ s->block_start = (long)s->strstart; ++ s->insert = s->lookahead; ++ s->lookahead = 0; ++ s->match_length = s->prev_length = MIN_MATCH-1; ++ s->match_available = 0; ++ strm->next_in = next; ++ strm->avail_in = avail; ++ s->wrap = wrap; ++ return Z_OK; ++} ++ ++/* ========================================================================= */ ++int ZEXPORT deflateGetDictionary (strm, dictionary, dictLength) ++ z_streamp strm; ++ Bytef *dictionary; ++ uInt *dictLength; ++{ ++ deflate_state *s; ++ uInt len; ++ ++ if (deflateStateCheck(strm)) ++ return Z_STREAM_ERROR; ++ s = strm->state; ++ len = s->strstart + s->lookahead; ++ if (len > s->w_size) ++ len = s->w_size; ++ if (dictionary != Z_NULL && len) ++ zmemcpy(dictionary, s->window + s->strstart + s->lookahead - len, len); ++ if (dictLength != Z_NULL) ++ *dictLength = len; ++ return Z_OK; ++} ++ ++/* ========================================================================= */ ++int ZEXPORT deflateResetKeep (strm) ++ z_streamp strm; ++{ ++ deflate_state *s; ++ ++ if (deflateStateCheck(strm)) { ++ return Z_STREAM_ERROR; ++ } ++ ++ strm->total_in = strm->total_out = 0; ++ strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */ ++ strm->data_type = Z_UNKNOWN; ++ ++ s = (deflate_state *)strm->state; ++ s->pending = 0; ++ s->pending_out = s->pending_buf; ++ ++ if (s->wrap < 0) { ++ s->wrap = -s->wrap; /* was made negative by deflate(..., Z_FINISH); */ ++ } ++ s->status = ++#ifdef GZIP ++ s->wrap == 2 ? GZIP_STATE : ++#endif ++ s->wrap ? INIT_STATE : BUSY_STATE; ++ strm->adler = ++#ifdef GZIP ++ s->wrap == 2 ? crc32(0L, Z_NULL, 0) : ++#endif ++ adler32(0L, Z_NULL, 0); ++ s->last_flush = Z_NO_FLUSH; ++ ++ _tr_init(s); ++ ++ return Z_OK; ++} ++ ++/* ========================================================================= */ ++int ZEXPORT deflateReset (strm) ++ z_streamp strm; ++{ ++ int ret; ++ ++ ret = deflateResetKeep(strm); ++ if (ret == Z_OK) ++ lm_init(strm->state); ++ return ret; ++} ++ ++/* ========================================================================= */ ++int ZEXPORT deflateSetHeader (strm, head) ++ z_streamp strm; ++ gz_headerp head; ++{ ++ if (deflateStateCheck(strm) || strm->state->wrap != 2) ++ return Z_STREAM_ERROR; ++ strm->state->gzhead = head; ++ return Z_OK; ++} ++ ++/* ========================================================================= */ ++int ZEXPORT deflatePending (strm, pending, bits) ++ unsigned *pending; ++ int *bits; ++ z_streamp strm; ++{ ++ if (deflateStateCheck(strm)) return Z_STREAM_ERROR; ++ if (pending != Z_NULL) ++ *pending = strm->state->pending; ++ if (bits != Z_NULL) ++ *bits = strm->state->bi_valid; ++ return Z_OK; ++} ++ ++/* ========================================================================= */ ++int ZEXPORT deflatePrime (strm, bits, value) ++ z_streamp strm; ++ int bits; ++ int value; ++{ ++ deflate_state *s; ++ int put; ++ ++ if (deflateStateCheck(strm)) return Z_STREAM_ERROR; ++ s = strm->state; ++ if ((Bytef *)(s->d_buf) < s->pending_out + ((Buf_size + 7) >> 3)) ++ return Z_BUF_ERROR; ++ do { ++ put = Buf_size - s->bi_valid; ++ if (put > bits) ++ put = bits; ++ s->bi_buf |= (ush)((value & ((1 << put) - 1)) << s->bi_valid); ++ s->bi_valid += put; ++ _tr_flush_bits(s); ++ value >>= put; ++ bits -= put; ++ } while (bits); ++ return Z_OK; ++} ++ ++/* ========================================================================= */ ++int ZEXPORT deflateParams(strm, level, strategy) ++ z_streamp strm; ++ int level; ++ int strategy; ++{ ++ deflate_state *s; ++ compress_func func; ++ ++ if (deflateStateCheck(strm)) return Z_STREAM_ERROR; ++ s = strm->state; ++ ++#ifdef FASTEST ++ if (level != 0) level = 1; ++#else ++ if (level == Z_DEFAULT_COMPRESSION) level = 6; ++#endif ++ if (level < 0 || level > 9 || strategy < 0 || strategy > Z_FIXED) { ++ return Z_STREAM_ERROR; ++ } ++ func = configuration_table[s->level].func; ++ ++ if ((strategy != s->strategy || func != configuration_table[level].func) && ++ s->high_water) { ++ /* Flush the last buffer: */ ++ int err = deflate(strm, Z_BLOCK); ++ if (err == Z_STREAM_ERROR) ++ return err; ++ if (strm->avail_out == 0) ++ return Z_BUF_ERROR; ++ } ++ if (s->level != level) { ++ if (s->level == 0 && s->matches != 0) { ++ if (s->matches == 1) { ++ slide_hash(s); ++ } else { ++ CLEAR_HASH(s); ++ } ++ s->matches = 0; ++ } ++ s->level = level; ++ s->max_lazy_match = configuration_table[level].max_lazy; ++ s->good_match = configuration_table[level].good_length; ++ s->nice_match = configuration_table[level].nice_length; ++ s->max_chain_length = configuration_table[level].max_chain; ++ } ++ s->strategy = strategy; ++ return Z_OK; ++} ++ ++/* ========================================================================= */ ++int ZEXPORT deflateTune(strm, good_length, max_lazy, nice_length, max_chain) ++ z_streamp strm; ++ int good_length; ++ int max_lazy; ++ int nice_length; ++ int max_chain; ++{ ++ deflate_state *s; ++ ++ if (deflateStateCheck(strm)) return Z_STREAM_ERROR; ++ s = strm->state; ++ s->good_match = (uInt)good_length; ++ s->max_lazy_match = (uInt)max_lazy; ++ s->nice_match = nice_length; ++ s->max_chain_length = (uInt)max_chain; ++ return Z_OK; ++} ++ ++/* ========================================================================= ++ * For the default windowBits of 15 and memLevel of 8, this function returns ++ * a close to exact, as well as small, upper bound on the compressed size. ++ * They are coded as constants here for a reason--if the #define's are ++ * changed, then this function needs to be changed as well. The return ++ * value for 15 and 8 only works for those exact settings. ++ * ++ * For any setting other than those defaults for windowBits and memLevel, ++ * the value returned is a conservative worst case for the maximum expansion ++ * resulting from using fixed blocks instead of stored blocks, which deflate ++ * can emit on compressed data for some combinations of the parameters. ++ * ++ * This function could be more sophisticated to provide closer upper bounds for ++ * every combination of windowBits and memLevel. But even the conservative ++ * upper bound of about 14% expansion does not seem onerous for output buffer ++ * allocation. ++ */ ++uLong ZEXPORT deflateBound(strm, sourceLen) ++ z_streamp strm; ++ uLong sourceLen; ++{ ++ deflate_state *s; ++ uLong complen, wraplen; ++ ++ /* conservative upper bound for compressed data */ ++ complen = sourceLen + ++ ((sourceLen + 7) >> 3) + ((sourceLen + 63) >> 6) + 5; ++ ++ /* if can't get parameters, return conservative bound plus zlib wrapper */ ++ if (deflateStateCheck(strm)) ++ return complen + 6; ++ ++ /* compute wrapper length */ ++ s = strm->state; ++ switch (s->wrap) { ++ case 0: /* raw deflate */ ++ wraplen = 0; ++ break; ++ case 1: /* zlib wrapper */ ++ wraplen = 6 + (s->strstart ? 4 : 0); ++ break; ++#ifdef GZIP ++ case 2: /* gzip wrapper */ ++ wraplen = 18; ++ if (s->gzhead != Z_NULL) { /* user-supplied gzip header */ ++ Bytef *str; ++ if (s->gzhead->extra != Z_NULL) ++ wraplen += 2 + s->gzhead->extra_len; ++ str = s->gzhead->name; ++ if (str != Z_NULL) ++ do { ++ wraplen++; ++ } while (*str++); ++ str = s->gzhead->comment; ++ if (str != Z_NULL) ++ do { ++ wraplen++; ++ } while (*str++); ++ if (s->gzhead->hcrc) ++ wraplen += 2; ++ } ++ break; ++#endif ++ default: /* for compiler happiness */ ++ wraplen = 6; ++ } ++ ++ /* if not default parameters, return conservative bound */ ++ if (s->w_bits != 15 || s->hash_bits != 8 + 7) ++ return complen + wraplen; ++ ++ /* default settings: return tight bound for that case */ ++ return sourceLen + (sourceLen >> 12) + (sourceLen >> 14) + ++ (sourceLen >> 25) + 13 - 6 + wraplen; ++} ++ ++/* ========================================================================= ++ * Put a short in the pending buffer. The 16-bit value is put in MSB order. ++ * IN assertion: the stream state is correct and there is enough room in ++ * pending_buf. ++ */ ++local void putShortMSB (s, b) ++ deflate_state *s; ++ uInt b; ++{ ++ put_byte(s, (Byte)(b >> 8)); ++ put_byte(s, (Byte)(b & 0xff)); ++} ++ ++/* ========================================================================= ++ * Flush as much pending output as possible. All deflate() output, except for ++ * some deflate_stored() output, goes through this function so some ++ * applications may wish to modify it to avoid allocating a large ++ * strm->next_out buffer and copying into it. (See also read_buf()). ++ */ ++local void flush_pending(strm) ++ z_streamp strm; ++{ ++ unsigned len; ++ deflate_state *s = strm->state; ++ ++ _tr_flush_bits(s); ++ len = s->pending; ++ if (len > strm->avail_out) len = strm->avail_out; ++ if (len == 0) return; ++ ++ zmemcpy(strm->next_out, s->pending_out, len); ++ strm->next_out += len; ++ s->pending_out += len; ++ strm->total_out += len; ++ strm->avail_out -= len; ++ s->pending -= len; ++ if (s->pending == 0) { ++ s->pending_out = s->pending_buf; ++ } ++} ++ ++/* =========================================================================== ++ * Update the header CRC with the bytes s->pending_buf[beg..s->pending - 1]. ++ */ ++#define HCRC_UPDATE(beg) \ ++ do { \ ++ if (s->gzhead->hcrc && s->pending > (beg)) \ ++ strm->adler = crc32(strm->adler, s->pending_buf + (beg), \ ++ s->pending - (beg)); \ ++ } while (0) ++ ++/* ========================================================================= */ ++int ZEXPORT deflate (strm, flush) ++ z_streamp strm; ++ int flush; ++{ ++ int old_flush; /* value of flush param for previous deflate call */ ++ deflate_state *s; ++ ++ if (deflateStateCheck(strm) || flush > Z_BLOCK || flush < 0) { ++ return Z_STREAM_ERROR; ++ } ++ s = strm->state; ++ ++ if (strm->next_out == Z_NULL || ++ (strm->avail_in != 0 && strm->next_in == Z_NULL) || ++ (s->status == FINISH_STATE && flush != Z_FINISH)) { ++ ERR_RETURN(strm, Z_STREAM_ERROR); ++ } ++ if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR); ++ ++ old_flush = s->last_flush; ++ s->last_flush = flush; ++ ++ /* Flush as much pending output as possible */ ++ if (s->pending != 0) { ++ flush_pending(strm); ++ if (strm->avail_out == 0) { ++ /* Since avail_out is 0, deflate will be called again with ++ * more output space, but possibly with both pending and ++ * avail_in equal to zero. There won't be anything to do, ++ * but this is not an error situation so make sure we ++ * return OK instead of BUF_ERROR at next call of deflate: ++ */ ++ s->last_flush = -1; ++ return Z_OK; ++ } ++ ++ /* Make sure there is something to do and avoid duplicate consecutive ++ * flushes. For repeated and useless calls with Z_FINISH, we keep ++ * returning Z_STREAM_END instead of Z_BUF_ERROR. ++ */ ++ } else if (strm->avail_in == 0 && RANK(flush) <= RANK(old_flush) && ++ flush != Z_FINISH) { ++ ERR_RETURN(strm, Z_BUF_ERROR); ++ } ++ ++ /* User must not provide more input after the first FINISH: */ ++ if (s->status == FINISH_STATE && strm->avail_in != 0) { ++ ERR_RETURN(strm, Z_BUF_ERROR); ++ } ++ ++ /* Write the header */ ++ if (s->status == INIT_STATE) { ++ /* zlib header */ ++ uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8; ++ uInt level_flags; ++ ++ if (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2) ++ level_flags = 0; ++ else if (s->level < 6) ++ level_flags = 1; ++ else if (s->level == 6) ++ level_flags = 2; ++ else ++ level_flags = 3; ++ header |= (level_flags << 6); ++ if (s->strstart != 0) header |= PRESET_DICT; ++ header += 31 - (header % 31); ++ ++ putShortMSB(s, header); ++ ++ /* Save the adler32 of the preset dictionary: */ ++ if (s->strstart != 0) { ++ putShortMSB(s, (uInt)(strm->adler >> 16)); ++ putShortMSB(s, (uInt)(strm->adler & 0xffff)); ++ } ++ strm->adler = adler32(0L, Z_NULL, 0); ++ s->status = BUSY_STATE; ++ ++ /* Compression must start with an empty pending buffer */ ++ flush_pending(strm); ++ if (s->pending != 0) { ++ s->last_flush = -1; ++ return Z_OK; ++ } ++ } ++#ifdef GZIP ++ if (s->status == GZIP_STATE) { ++ /* gzip header */ ++ strm->adler = crc32(0L, Z_NULL, 0); ++ put_byte(s, 31); ++ put_byte(s, 139); ++ put_byte(s, 8); ++ if (s->gzhead == Z_NULL) { ++ put_byte(s, 0); ++ put_byte(s, 0); ++ put_byte(s, 0); ++ put_byte(s, 0); ++ put_byte(s, 0); ++ put_byte(s, s->level == 9 ? 2 : ++ (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? ++ 4 : 0)); ++ put_byte(s, OS_CODE); ++ s->status = BUSY_STATE; ++ ++ /* Compression must start with an empty pending buffer */ ++ flush_pending(strm); ++ if (s->pending != 0) { ++ s->last_flush = -1; ++ return Z_OK; ++ } ++ } ++ else { ++ put_byte(s, (s->gzhead->text ? 1 : 0) + ++ (s->gzhead->hcrc ? 2 : 0) + ++ (s->gzhead->extra == Z_NULL ? 0 : 4) + ++ (s->gzhead->name == Z_NULL ? 0 : 8) + ++ (s->gzhead->comment == Z_NULL ? 0 : 16) ++ ); ++ put_byte(s, (Byte)(s->gzhead->time & 0xff)); ++ put_byte(s, (Byte)((s->gzhead->time >> 8) & 0xff)); ++ put_byte(s, (Byte)((s->gzhead->time >> 16) & 0xff)); ++ put_byte(s, (Byte)((s->gzhead->time >> 24) & 0xff)); ++ put_byte(s, s->level == 9 ? 2 : ++ (s->strategy >= Z_HUFFMAN_ONLY || s->level < 2 ? ++ 4 : 0)); ++ put_byte(s, s->gzhead->os & 0xff); ++ if (s->gzhead->extra != Z_NULL) { ++ put_byte(s, s->gzhead->extra_len & 0xff); ++ put_byte(s, (s->gzhead->extra_len >> 8) & 0xff); ++ } ++ if (s->gzhead->hcrc) ++ strm->adler = crc32(strm->adler, s->pending_buf, ++ s->pending); ++ s->gzindex = 0; ++ s->status = EXTRA_STATE; ++ } ++ } ++ if (s->status == EXTRA_STATE) { ++ if (s->gzhead->extra != Z_NULL) { ++ ulg beg = s->pending; /* start of bytes to update crc */ ++ uInt left = (s->gzhead->extra_len & 0xffff) - s->gzindex; ++ while (s->pending + left > s->pending_buf_size) { ++ uInt copy = s->pending_buf_size - s->pending; ++ zmemcpy(s->pending_buf + s->pending, ++ s->gzhead->extra + s->gzindex, copy); ++ s->pending = s->pending_buf_size; ++ HCRC_UPDATE(beg); ++ s->gzindex += copy; ++ flush_pending(strm); ++ if (s->pending != 0) { ++ s->last_flush = -1; ++ return Z_OK; ++ } ++ beg = 0; ++ left -= copy; ++ } ++ zmemcpy(s->pending_buf + s->pending, ++ s->gzhead->extra + s->gzindex, left); ++ s->pending += left; ++ HCRC_UPDATE(beg); ++ s->gzindex = 0; ++ } ++ s->status = NAME_STATE; ++ } ++ if (s->status == NAME_STATE) { ++ if (s->gzhead->name != Z_NULL) { ++ ulg beg = s->pending; /* start of bytes to update crc */ ++ int val; ++ do { ++ if (s->pending == s->pending_buf_size) { ++ HCRC_UPDATE(beg); ++ flush_pending(strm); ++ if (s->pending != 0) { ++ s->last_flush = -1; ++ return Z_OK; ++ } ++ beg = 0; ++ } ++ val = s->gzhead->name[s->gzindex++]; ++ put_byte(s, val); ++ } while (val != 0); ++ HCRC_UPDATE(beg); ++ s->gzindex = 0; ++ } ++ s->status = COMMENT_STATE; ++ } ++ if (s->status == COMMENT_STATE) { ++ if (s->gzhead->comment != Z_NULL) { ++ ulg beg = s->pending; /* start of bytes to update crc */ ++ int val; ++ do { ++ if (s->pending == s->pending_buf_size) { ++ HCRC_UPDATE(beg); ++ flush_pending(strm); ++ if (s->pending != 0) { ++ s->last_flush = -1; ++ return Z_OK; ++ } ++ beg = 0; ++ } ++ val = s->gzhead->comment[s->gzindex++]; ++ put_byte(s, val); ++ } while (val != 0); ++ HCRC_UPDATE(beg); ++ } ++ s->status = HCRC_STATE; ++ } ++ if (s->status == HCRC_STATE) { ++ if (s->gzhead->hcrc) { ++ if (s->pending + 2 > s->pending_buf_size) { ++ flush_pending(strm); ++ if (s->pending != 0) { ++ s->last_flush = -1; ++ return Z_OK; ++ } ++ } ++ put_byte(s, (Byte)(strm->adler & 0xff)); ++ put_byte(s, (Byte)((strm->adler >> 8) & 0xff)); ++ strm->adler = crc32(0L, Z_NULL, 0); ++ } ++ s->status = BUSY_STATE; ++ ++ /* Compression must start with an empty pending buffer */ ++ flush_pending(strm); ++ if (s->pending != 0) { ++ s->last_flush = -1; ++ return Z_OK; ++ } ++ } ++#endif ++ ++ /* Start a new block or continue the current one. ++ */ ++ if (strm->avail_in != 0 || s->lookahead != 0 || ++ (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) { ++ block_state bstate; ++ ++ bstate = s->level == 0 ? deflate_stored(s, flush) : ++ s->strategy == Z_HUFFMAN_ONLY ? deflate_huff(s, flush) : ++ s->strategy == Z_RLE ? deflate_rle(s, flush) : ++ (*(configuration_table[s->level].func))(s, flush); ++ ++ if (bstate == finish_started || bstate == finish_done) { ++ s->status = FINISH_STATE; ++ } ++ if (bstate == need_more || bstate == finish_started) { ++ if (strm->avail_out == 0) { ++ s->last_flush = -1; /* avoid BUF_ERROR next call, see above */ ++ } ++ return Z_OK; ++ /* If flush != Z_NO_FLUSH && avail_out == 0, the next call ++ * of deflate should use the same flush parameter to make sure ++ * that the flush is complete. So we don't have to output an ++ * empty block here, this will be done at next call. This also ++ * ensures that for a very small output buffer, we emit at most ++ * one empty block. ++ */ ++ } ++ if (bstate == block_done) { ++ if (flush == Z_PARTIAL_FLUSH) { ++ _tr_align(s); ++ } else if (flush != Z_BLOCK) { /* FULL_FLUSH or SYNC_FLUSH */ ++ _tr_stored_block(s, (char*)0, 0L, 0); ++ /* For a full flush, this empty block will be recognized ++ * as a special marker by inflate_sync(). ++ */ ++ if (flush == Z_FULL_FLUSH) { ++ CLEAR_HASH(s); /* forget history */ ++ if (s->lookahead == 0) { ++ s->strstart = 0; ++ s->block_start = 0L; ++ s->insert = 0; ++ } ++ } ++ } ++ flush_pending(strm); ++ if (strm->avail_out == 0) { ++ s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */ ++ return Z_OK; ++ } ++ } ++ } ++ ++ if (flush != Z_FINISH) return Z_OK; ++ if (s->wrap <= 0) return Z_STREAM_END; ++ ++ /* Write the trailer */ ++#ifdef GZIP ++ if (s->wrap == 2) { ++ put_byte(s, (Byte)(strm->adler & 0xff)); ++ put_byte(s, (Byte)((strm->adler >> 8) & 0xff)); ++ put_byte(s, (Byte)((strm->adler >> 16) & 0xff)); ++ put_byte(s, (Byte)((strm->adler >> 24) & 0xff)); ++ put_byte(s, (Byte)(strm->total_in & 0xff)); ++ put_byte(s, (Byte)((strm->total_in >> 8) & 0xff)); ++ put_byte(s, (Byte)((strm->total_in >> 16) & 0xff)); ++ put_byte(s, (Byte)((strm->total_in >> 24) & 0xff)); ++ } ++ else ++#endif ++ { ++ putShortMSB(s, (uInt)(strm->adler >> 16)); ++ putShortMSB(s, (uInt)(strm->adler & 0xffff)); ++ } ++ flush_pending(strm); ++ /* If avail_out is zero, the application will call deflate again ++ * to flush the rest. ++ */ ++ if (s->wrap > 0) s->wrap = -s->wrap; /* write the trailer only once! */ ++ return s->pending != 0 ? Z_OK : Z_STREAM_END; ++} ++ ++/* ========================================================================= */ ++int ZEXPORT deflateEnd (strm) ++ z_streamp strm; ++{ ++ int status; ++ ++ if (deflateStateCheck(strm)) return Z_STREAM_ERROR; ++ ++ status = strm->state->status; ++ ++ /* Deallocate in reverse order of allocations: */ ++ TRY_FREE(strm, strm->state->pending_buf); ++ TRY_FREE(strm, strm->state->head); ++ TRY_FREE(strm, strm->state->prev); ++ TRY_FREE(strm, strm->state->window); ++ ++ ZFREE(strm, strm->state); ++ strm->state = Z_NULL; ++ ++ return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK; ++} ++ ++/* ========================================================================= ++ * Copy the source state to the destination state. ++ * To simplify the source, this is not supported for 16-bit MSDOS (which ++ * doesn't have enough memory anyway to duplicate compression states). ++ */ ++int ZEXPORT deflateCopy (dest, source) ++ z_streamp dest; ++ z_streamp source; ++{ ++#ifdef MAXSEG_64K ++ return Z_STREAM_ERROR; ++#else ++ deflate_state *ds; ++ deflate_state *ss; ++ ushf *overlay; ++ ++ ++ if (deflateStateCheck(source) || dest == Z_NULL) { ++ return Z_STREAM_ERROR; ++ } ++ ++ ss = source->state; ++ ++ zmemcpy((voidpf)dest, (voidpf)source, sizeof(z_stream)); ++ ++ ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state)); ++ if (ds == Z_NULL) return Z_MEM_ERROR; ++ dest->state = (struct internal_state FAR *) ds; ++ zmemcpy((voidpf)ds, (voidpf)ss, sizeof(deflate_state)); ++ ds->strm = dest; ++ ++ ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte)); ++ ds->prev = (Posf *) ZALLOC(dest, ds->w_size, sizeof(Pos)); ++ ds->head = (Posf *) ZALLOC(dest, ds->hash_size, sizeof(Pos)); ++ overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2); ++ ds->pending_buf = (uchf *) overlay; ++ ++ if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL || ++ ds->pending_buf == Z_NULL) { ++ deflateEnd (dest); ++ return Z_MEM_ERROR; ++ } ++ /* following zmemcpy do not work for 16-bit MSDOS */ ++ zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte)); ++ zmemcpy((voidpf)ds->prev, (voidpf)ss->prev, ds->w_size * sizeof(Pos)); ++ zmemcpy((voidpf)ds->head, (voidpf)ss->head, ds->hash_size * sizeof(Pos)); ++ zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size); ++ ++ ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf); ++ ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush); ++ ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize; ++ ++ ds->l_desc.dyn_tree = ds->dyn_ltree; ++ ds->d_desc.dyn_tree = ds->dyn_dtree; ++ ds->bl_desc.dyn_tree = ds->bl_tree; ++ ++ return Z_OK; ++#endif /* MAXSEG_64K */ ++} ++ ++/* =========================================================================== ++ * Read a new buffer from the current input stream, update the adler32 ++ * and total number of bytes read. All deflate() input goes through ++ * this function so some applications may wish to modify it to avoid ++ * allocating a large strm->next_in buffer and copying from it. ++ * (See also flush_pending()). ++ */ ++local unsigned read_buf(strm, buf, size) ++ z_streamp strm; ++ Bytef *buf; ++ unsigned size; ++{ ++ unsigned len = strm->avail_in; ++ ++ if (len > size) len = size; ++ if (len == 0) return 0; ++ ++ strm->avail_in -= len; ++ ++ zmemcpy(buf, strm->next_in, len); ++ if (strm->state->wrap == 1) { ++ strm->adler = adler32(strm->adler, buf, len); ++ } ++#ifdef GZIP ++ else if (strm->state->wrap == 2) { ++ strm->adler = crc32(strm->adler, buf, len); ++ } ++#endif ++ strm->next_in += len; ++ strm->total_in += len; ++ ++ return len; ++} ++ ++/* =========================================================================== ++ * Initialize the "longest match" routines for a new zlib stream ++ */ ++local void lm_init (s) ++ deflate_state *s; ++{ ++ s->window_size = (ulg)2L*s->w_size; ++ ++ CLEAR_HASH(s); ++ ++ /* Set the default configuration parameters: ++ */ ++ s->max_lazy_match = configuration_table[s->level].max_lazy; ++ s->good_match = configuration_table[s->level].good_length; ++ s->nice_match = configuration_table[s->level].nice_length; ++ s->max_chain_length = configuration_table[s->level].max_chain; ++ ++ s->strstart = 0; ++ s->block_start = 0L; ++ s->lookahead = 0; ++ s->insert = 0; ++ s->match_length = s->prev_length = MIN_MATCH-1; ++ s->match_available = 0; ++ s->ins_h = 0; ++#ifndef FASTEST ++#ifdef ASMV ++ match_init(); /* initialize the asm code */ ++#endif ++#endif ++} ++ ++#ifndef FASTEST ++/* =========================================================================== ++ * Set match_start to the longest match starting at the given string and ++ * return its length. Matches shorter or equal to prev_length are discarded, ++ * in which case the result is equal to prev_length and match_start is ++ * garbage. ++ * IN assertions: cur_match is the head of the hash chain for the current ++ * string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1 ++ * OUT assertion: the match length is not greater than s->lookahead. ++ */ ++#ifndef ASMV ++/* For 80x86 and 680x0, an optimized version will be provided in match.asm or ++ * match.S. The code will be functionally equivalent. ++ */ ++local uInt longest_match(s, cur_match) ++ deflate_state *s; ++ IPos cur_match; /* current match */ ++{ ++ unsigned chain_length = s->max_chain_length;/* max hash chain length */ ++ register Bytef *scan = s->window + s->strstart; /* current string */ ++ register Bytef *match; /* matched string */ ++ register int len; /* length of current match */ ++ int best_len = (int)s->prev_length; /* best match length so far */ ++ int nice_match = s->nice_match; /* stop if match long enough */ ++ IPos limit = s->strstart > (IPos)MAX_DIST(s) ? ++ s->strstart - (IPos)MAX_DIST(s) : NIL; ++ /* Stop when cur_match becomes <= limit. To simplify the code, ++ * we prevent matches with the string of window index 0. ++ */ ++ Posf *prev = s->prev; ++ uInt wmask = s->w_mask; ++ ++#ifdef UNALIGNED_OK ++ /* Compare two bytes at a time. Note: this is not always beneficial. ++ * Try with and without -DUNALIGNED_OK to check. ++ */ ++ register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1; ++ register ush scan_start = *(ushf*)scan; ++ register ush scan_end = *(ushf*)(scan+best_len-1); ++#else ++ register Bytef *strend = s->window + s->strstart + MAX_MATCH; ++ register Byte scan_end1 = scan[best_len-1]; ++ register Byte scan_end = scan[best_len]; ++#endif ++ ++ /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. ++ * It is easy to get rid of this optimization if necessary. ++ */ ++ Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever"); ++ ++ /* Do not waste too much time if we already have a good match: */ ++ if (s->prev_length >= s->good_match) { ++ chain_length >>= 2; ++ } ++ /* Do not look for matches beyond the end of the input. This is necessary ++ * to make deflate deterministic. ++ */ ++ if ((uInt)nice_match > s->lookahead) nice_match = (int)s->lookahead; ++ ++ Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); ++ ++ do { ++ Assert(cur_match < s->strstart, "no future"); ++ match = s->window + cur_match; ++ ++ /* Skip to next match if the match length cannot increase ++ * or if the match length is less than 2. Note that the checks below ++ * for insufficient lookahead only occur occasionally for performance ++ * reasons. Therefore uninitialized memory will be accessed, and ++ * conditional jumps will be made that depend on those values. ++ * However the length of the match is limited to the lookahead, so ++ * the output of deflate is not affected by the uninitialized values. ++ */ ++#if (defined(UNALIGNED_OK) && MAX_MATCH == 258) ++ /* This code assumes sizeof(unsigned short) == 2. Do not use ++ * UNALIGNED_OK if your compiler uses a different size. ++ */ ++ if (*(ushf*)(match+best_len-1) != scan_end || ++ *(ushf*)match != scan_start) continue; ++ ++ /* It is not necessary to compare scan[2] and match[2] since they are ++ * always equal when the other bytes match, given that the hash keys ++ * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at ++ * strstart+3, +5, ... up to strstart+257. We check for insufficient ++ * lookahead only every 4th comparison; the 128th check will be made ++ * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is ++ * necessary to put more guard bytes at the end of the window, or ++ * to check more often for insufficient lookahead. ++ */ ++ Assert(scan[2] == match[2], "scan[2]?"); ++ scan++, match++; ++ do { ++ } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) && ++ *(ushf*)(scan+=2) == *(ushf*)(match+=2) && ++ *(ushf*)(scan+=2) == *(ushf*)(match+=2) && ++ *(ushf*)(scan+=2) == *(ushf*)(match+=2) && ++ scan < strend); ++ /* The funny "do {}" generates better code on most compilers */ ++ ++ /* Here, scan <= window+strstart+257 */ ++ Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); ++ if (*scan == *match) scan++; ++ ++ len = (MAX_MATCH - 1) - (int)(strend-scan); ++ scan = strend - (MAX_MATCH-1); ++ ++#else /* UNALIGNED_OK */ ++ ++ if (match[best_len] != scan_end || ++ match[best_len-1] != scan_end1 || ++ *match != *scan || ++ *++match != scan[1]) continue; ++ ++ /* The check at best_len-1 can be removed because it will be made ++ * again later. (This heuristic is not always a win.) ++ * It is not necessary to compare scan[2] and match[2] since they ++ * are always equal when the other bytes match, given that ++ * the hash keys are equal and that HASH_BITS >= 8. ++ */ ++ scan += 2, match++; ++ Assert(*scan == *match, "match[2]?"); ++ ++ /* We check for insufficient lookahead only every 8th comparison; ++ * the 256th check will be made at strstart+258. ++ */ ++ do { ++ } while (*++scan == *++match && *++scan == *++match && ++ *++scan == *++match && *++scan == *++match && ++ *++scan == *++match && *++scan == *++match && ++ *++scan == *++match && *++scan == *++match && ++ scan < strend); ++ ++ Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); ++ ++ len = MAX_MATCH - (int)(strend - scan); ++ scan = strend - MAX_MATCH; ++ ++#endif /* UNALIGNED_OK */ ++ ++ if (len > best_len) { ++ s->match_start = cur_match; ++ best_len = len; ++ if (len >= nice_match) break; ++#ifdef UNALIGNED_OK ++ scan_end = *(ushf*)(scan+best_len-1); ++#else ++ scan_end1 = scan[best_len-1]; ++ scan_end = scan[best_len]; ++#endif ++ } ++ } while ((cur_match = prev[cur_match & wmask]) > limit ++ && --chain_length != 0); ++ ++ if ((uInt)best_len <= s->lookahead) return (uInt)best_len; ++ return s->lookahead; ++} ++#endif /* ASMV */ ++ ++#else /* FASTEST */ ++ ++/* --------------------------------------------------------------------------- ++ * Optimized version for FASTEST only ++ */ ++local uInt longest_match(s, cur_match) ++ deflate_state *s; ++ IPos cur_match; /* current match */ ++{ ++ register Bytef *scan = s->window + s->strstart; /* current string */ ++ register Bytef *match; /* matched string */ ++ register int len; /* length of current match */ ++ register Bytef *strend = s->window + s->strstart + MAX_MATCH; ++ ++ /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16. ++ * It is easy to get rid of this optimization if necessary. ++ */ ++ Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever"); ++ ++ Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead"); ++ ++ Assert(cur_match < s->strstart, "no future"); ++ ++ match = s->window + cur_match; ++ ++ /* Return failure if the match length is less than 2: ++ */ ++ if (match[0] != scan[0] || match[1] != scan[1]) return MIN_MATCH-1; ++ ++ /* The check at best_len-1 can be removed because it will be made ++ * again later. (This heuristic is not always a win.) ++ * It is not necessary to compare scan[2] and match[2] since they ++ * are always equal when the other bytes match, given that ++ * the hash keys are equal and that HASH_BITS >= 8. ++ */ ++ scan += 2, match += 2; ++ Assert(*scan == *match, "match[2]?"); ++ ++ /* We check for insufficient lookahead only every 8th comparison; ++ * the 256th check will be made at strstart+258. ++ */ ++ do { ++ } while (*++scan == *++match && *++scan == *++match && ++ *++scan == *++match && *++scan == *++match && ++ *++scan == *++match && *++scan == *++match && ++ *++scan == *++match && *++scan == *++match && ++ scan < strend); ++ ++ Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan"); ++ ++ len = MAX_MATCH - (int)(strend - scan); ++ ++ if (len < MIN_MATCH) return MIN_MATCH - 1; ++ ++ s->match_start = cur_match; ++ return (uInt)len <= s->lookahead ? (uInt)len : s->lookahead; ++} ++ ++#endif /* FASTEST */ ++ ++#ifdef ZLIB_DEBUG ++ ++#define EQUAL 0 ++/* result of memcmp for equal strings */ ++ ++/* =========================================================================== ++ * Check that the match at match_start is indeed a match. ++ */ ++local void check_match(s, start, match, length) ++ deflate_state *s; ++ IPos start, match; ++ int length; ++{ ++ /* check that the match is indeed a match */ ++ if (zmemcmp(s->window + match, ++ s->window + start, length) != EQUAL) { ++ fprintf(stderr, " start %u, match %u, length %d\n", ++ start, match, length); ++ do { ++ fprintf(stderr, "%c%c", s->window[match++], s->window[start++]); ++ } while (--length != 0); ++ z_error("invalid match"); ++ } ++ if (z_verbose > 1) { ++ fprintf(stderr,"\\[%d,%d]", start-match, length); ++ do { putc(s->window[start++], stderr); } while (--length != 0); ++ } ++} ++#else ++# define check_match(s, start, match, length) ++#endif /* ZLIB_DEBUG */ ++ ++/* =========================================================================== ++ * Fill the window when the lookahead becomes insufficient. ++ * Updates strstart and lookahead. ++ * ++ * IN assertion: lookahead < MIN_LOOKAHEAD ++ * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD ++ * At least one byte has been read, or avail_in == 0; reads are ++ * performed for at least two bytes (required for the zip translate_eol ++ * option -- not supported here). ++ */ ++local void fill_window(s) ++ deflate_state *s; ++{ ++ unsigned n; ++ unsigned more; /* Amount of free space at the end of the window. */ ++ uInt wsize = s->w_size; ++ ++ Assert(s->lookahead < MIN_LOOKAHEAD, "already enough lookahead"); ++ ++ do { ++ more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart); ++ ++ /* Deal with !@#$% 64K limit: */ ++ if (sizeof(int) <= 2) { ++ if (more == 0 && s->strstart == 0 && s->lookahead == 0) { ++ more = wsize; ++ ++ } else if (more == (unsigned)(-1)) { ++ /* Very unlikely, but possible on 16 bit machine if ++ * strstart == 0 && lookahead == 1 (input done a byte at time) ++ */ ++ more--; ++ } ++ } ++ ++ /* If the window is almost full and there is insufficient lookahead, ++ * move the upper half to the lower one to make room in the upper half. ++ */ ++ if (s->strstart >= wsize+MAX_DIST(s)) { ++ ++ zmemcpy(s->window, s->window+wsize, (unsigned)wsize - more); ++ s->match_start -= wsize; ++ s->strstart -= wsize; /* we now have strstart >= MAX_DIST */ ++ s->block_start -= (long) wsize; ++ slide_hash(s); ++ more += wsize; ++ } ++ if (s->strm->avail_in == 0) break; ++ ++ /* If there was no sliding: ++ * strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 && ++ * more == window_size - lookahead - strstart ++ * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1) ++ * => more >= window_size - 2*WSIZE + 2 ++ * In the BIG_MEM or MMAP case (not yet supported), ++ * window_size == input_size + MIN_LOOKAHEAD && ++ * strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD. ++ * Otherwise, window_size == 2*WSIZE so more >= 2. ++ * If there was sliding, more >= WSIZE. So in all cases, more >= 2. ++ */ ++ Assert(more >= 2, "more < 2"); ++ ++ n = read_buf(s->strm, s->window + s->strstart + s->lookahead, more); ++ s->lookahead += n; ++ ++ /* Initialize the hash value now that we have some input: */ ++ if (s->lookahead + s->insert >= MIN_MATCH) { ++ uInt str = s->strstart - s->insert; ++ s->ins_h = s->window[str]; ++ UPDATE_HASH(s, s->ins_h, s->window[str + 1]); ++#if MIN_MATCH != 3 ++ Call UPDATE_HASH() MIN_MATCH-3 more times ++#endif ++ while (s->insert) { ++ UPDATE_HASH(s, s->ins_h, s->window[str + MIN_MATCH-1]); ++#ifndef FASTEST ++ s->prev[str & s->w_mask] = s->head[s->ins_h]; ++#endif ++ s->head[s->ins_h] = (Pos)str; ++ str++; ++ s->insert--; ++ if (s->lookahead + s->insert < MIN_MATCH) ++ break; ++ } ++ } ++ /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage, ++ * but this is not important since only literal bytes will be emitted. ++ */ ++ ++ } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0); ++ ++ /* If the WIN_INIT bytes after the end of the current data have never been ++ * written, then zero those bytes in order to avoid memory check reports of ++ * the use of uninitialized (or uninitialised as Julian writes) bytes by ++ * the longest match routines. Update the high water mark for the next ++ * time through here. WIN_INIT is set to MAX_MATCH since the longest match ++ * routines allow scanning to strstart + MAX_MATCH, ignoring lookahead. ++ */ ++ if (s->high_water < s->window_size) { ++ ulg curr = s->strstart + (ulg)(s->lookahead); ++ ulg init; ++ ++ if (s->high_water < curr) { ++ /* Previous high water mark below current data -- zero WIN_INIT ++ * bytes or up to end of window, whichever is less. ++ */ ++ init = s->window_size - curr; ++ if (init > WIN_INIT) ++ init = WIN_INIT; ++ zmemzero(s->window + curr, (unsigned)init); ++ s->high_water = curr + init; ++ } ++ else if (s->high_water < (ulg)curr + WIN_INIT) { ++ /* High water mark at or above current data, but below current data ++ * plus WIN_INIT -- zero out to current data plus WIN_INIT, or up ++ * to end of window, whichever is less. ++ */ ++ init = (ulg)curr + WIN_INIT - s->high_water; ++ if (init > s->window_size - s->high_water) ++ init = s->window_size - s->high_water; ++ zmemzero(s->window + s->high_water, (unsigned)init); ++ s->high_water += init; ++ } ++ } ++ ++ Assert((ulg)s->strstart <= s->window_size - MIN_LOOKAHEAD, ++ "not enough room for search"); ++} ++ ++/* =========================================================================== ++ * Flush the current block, with given end-of-file flag. ++ * IN assertion: strstart is set to the end of the current match. ++ */ ++#define FLUSH_BLOCK_ONLY(s, last) { \ ++ _tr_flush_block(s, (s->block_start >= 0L ? \ ++ (charf *)&s->window[(unsigned)s->block_start] : \ ++ (charf *)Z_NULL), \ ++ (ulg)((long)s->strstart - s->block_start), \ ++ (last)); \ ++ s->block_start = s->strstart; \ ++ flush_pending(s->strm); \ ++ Tracev((stderr,"[FLUSH]")); \ ++} ++ ++/* Same but force premature exit if necessary. */ ++#define FLUSH_BLOCK(s, last) { \ ++ FLUSH_BLOCK_ONLY(s, last); \ ++ if (s->strm->avail_out == 0) return (last) ? finish_started : need_more; \ ++} ++ ++/* Maximum stored block length in deflate format (not including header). */ ++#define MAX_STORED 65535 ++ ++/* Minimum of a and b. */ ++#define MIN(a, b) ((a) > (b) ? (b) : (a)) ++ ++/* =========================================================================== ++ * Copy without compression as much as possible from the input stream, return ++ * the current block state. ++ * ++ * In case deflateParams() is used to later switch to a non-zero compression ++ * level, s->matches (otherwise unused when storing) keeps track of the number ++ * of hash table slides to perform. If s->matches is 1, then one hash table ++ * slide will be done when switching. If s->matches is 2, the maximum value ++ * allowed here, then the hash table will be cleared, since two or more slides ++ * is the same as a clear. ++ * ++ * deflate_stored() is written to minimize the number of times an input byte is ++ * copied. It is most efficient with large input and output buffers, which ++ * maximizes the opportunites to have a single copy from next_in to next_out. ++ */ ++local block_state deflate_stored(s, flush) ++ deflate_state *s; ++ int flush; ++{ ++ /* Smallest worthy block size when not flushing or finishing. By default ++ * this is 32K. This can be as small as 507 bytes for memLevel == 1. For ++ * large input and output buffers, the stored block size will be larger. ++ */ ++ unsigned min_block = MIN(s->pending_buf_size - 5, s->w_size); ++ ++ /* Copy as many min_block or larger stored blocks directly to next_out as ++ * possible. If flushing, copy the remaining available input to next_out as ++ * stored blocks, if there is enough space. ++ */ ++ unsigned len, left, have, last = 0; ++ unsigned used = s->strm->avail_in; ++ do { ++ /* Set len to the maximum size block that we can copy directly with the ++ * available input data and output space. Set left to how much of that ++ * would be copied from what's left in the window. ++ */ ++ len = MAX_STORED; /* maximum deflate stored block length */ ++ have = (s->bi_valid + 42) >> 3; /* number of header bytes */ ++ if (s->strm->avail_out < have) /* need room for header */ ++ break; ++ /* maximum stored block length that will fit in avail_out: */ ++ have = s->strm->avail_out - have; ++ left = s->strstart - s->block_start; /* bytes left in window */ ++ if (len > (ulg)left + s->strm->avail_in) ++ len = left + s->strm->avail_in; /* limit len to the input */ ++ if (len > have) ++ len = have; /* limit len to the output */ ++ ++ /* If the stored block would be less than min_block in length, or if ++ * unable to copy all of the available input when flushing, then try ++ * copying to the window and the pending buffer instead. Also don't ++ * write an empty block when flushing -- deflate() does that. ++ */ ++ if (len < min_block && ((len == 0 && flush != Z_FINISH) || ++ flush == Z_NO_FLUSH || ++ len != left + s->strm->avail_in)) ++ break; ++ ++ /* Make a dummy stored block in pending to get the header bytes, ++ * including any pending bits. This also updates the debugging counts. ++ */ ++ last = flush == Z_FINISH && len == left + s->strm->avail_in ? 1 : 0; ++ _tr_stored_block(s, (char *)0, 0L, last); ++ ++ /* Replace the lengths in the dummy stored block with len. */ ++ s->pending_buf[s->pending - 4] = len; ++ s->pending_buf[s->pending - 3] = len >> 8; ++ s->pending_buf[s->pending - 2] = ~len; ++ s->pending_buf[s->pending - 1] = ~len >> 8; ++ ++ /* Write the stored block header bytes. */ ++ flush_pending(s->strm); ++ ++#ifdef ZLIB_DEBUG ++ /* Update debugging counts for the data about to be copied. */ ++ s->compressed_len += len << 3; ++ s->bits_sent += len << 3; ++#endif ++ ++ /* Copy uncompressed bytes from the window to next_out. */ ++ if (left) { ++ if (left > len) ++ left = len; ++ zmemcpy(s->strm->next_out, s->window + s->block_start, left); ++ s->strm->next_out += left; ++ s->strm->avail_out -= left; ++ s->strm->total_out += left; ++ s->block_start += left; ++ len -= left; ++ } ++ ++ /* Copy uncompressed bytes directly from next_in to next_out, updating ++ * the check value. ++ */ ++ if (len) { ++ read_buf(s->strm, s->strm->next_out, len); ++ s->strm->next_out += len; ++ s->strm->avail_out -= len; ++ s->strm->total_out += len; ++ } ++ } while (last == 0); ++ ++ /* Update the sliding window with the last s->w_size bytes of the copied ++ * data, or append all of the copied data to the existing window if less ++ * than s->w_size bytes were copied. Also update the number of bytes to ++ * insert in the hash tables, in the event that deflateParams() switches to ++ * a non-zero compression level. ++ */ ++ used -= s->strm->avail_in; /* number of input bytes directly copied */ ++ if (used) { ++ /* If any input was used, then no unused input remains in the window, ++ * therefore s->block_start == s->strstart. ++ */ ++ if (used >= s->w_size) { /* supplant the previous history */ ++ s->matches = 2; /* clear hash */ ++ zmemcpy(s->window, s->strm->next_in - s->w_size, s->w_size); ++ s->strstart = s->w_size; ++ } ++ else { ++ if (s->window_size - s->strstart <= used) { ++ /* Slide the window down. */ ++ s->strstart -= s->w_size; ++ zmemcpy(s->window, s->window + s->w_size, s->strstart); ++ if (s->matches < 2) ++ s->matches++; /* add a pending slide_hash() */ ++ } ++ zmemcpy(s->window + s->strstart, s->strm->next_in - used, used); ++ s->strstart += used; ++ } ++ s->block_start = s->strstart; ++ s->insert += MIN(used, s->w_size - s->insert); ++ } ++ if (s->high_water < s->strstart) ++ s->high_water = s->strstart; ++ ++ /* If the last block was written to next_out, then done. */ ++ if (last) ++ return finish_done; ++ ++ /* If flushing and all input has been consumed, then done. */ ++ if (flush != Z_NO_FLUSH && flush != Z_FINISH && ++ s->strm->avail_in == 0 && (long)s->strstart == s->block_start) ++ return block_done; ++ ++ /* Fill the window with any remaining input. */ ++ have = s->window_size - s->strstart - 1; ++ if (s->strm->avail_in > have && s->block_start >= (long)s->w_size) { ++ /* Slide the window down. */ ++ s->block_start -= s->w_size; ++ s->strstart -= s->w_size; ++ zmemcpy(s->window, s->window + s->w_size, s->strstart); ++ if (s->matches < 2) ++ s->matches++; /* add a pending slide_hash() */ ++ have += s->w_size; /* more space now */ ++ } ++ if (have > s->strm->avail_in) ++ have = s->strm->avail_in; ++ if (have) { ++ read_buf(s->strm, s->window + s->strstart, have); ++ s->strstart += have; ++ } ++ if (s->high_water < s->strstart) ++ s->high_water = s->strstart; ++ ++ /* There was not enough avail_out to write a complete worthy or flushed ++ * stored block to next_out. Write a stored block to pending instead, if we ++ * have enough input for a worthy block, or if flushing and there is enough ++ * room for the remaining input as a stored block in the pending buffer. ++ */ ++ have = (s->bi_valid + 42) >> 3; /* number of header bytes */ ++ /* maximum stored block length that will fit in pending: */ ++ have = MIN(s->pending_buf_size - have, MAX_STORED); ++ min_block = MIN(have, s->w_size); ++ left = s->strstart - s->block_start; ++ if (left >= min_block || ++ ((left || flush == Z_FINISH) && flush != Z_NO_FLUSH && ++ s->strm->avail_in == 0 && left <= have)) { ++ len = MIN(left, have); ++ last = flush == Z_FINISH && s->strm->avail_in == 0 && ++ len == left ? 1 : 0; ++ _tr_stored_block(s, (charf *)s->window + s->block_start, len, last); ++ s->block_start += len; ++ flush_pending(s->strm); ++ } ++ ++ /* We've done all we can with the available input and output. */ ++ return last ? finish_started : need_more; ++} ++ ++/* =========================================================================== ++ * Compress as much as possible from the input stream, return the current ++ * block state. ++ * This function does not perform lazy evaluation of matches and inserts ++ * new strings in the dictionary only for unmatched strings or for short ++ * matches. It is used only for the fast compression options. ++ */ ++local block_state deflate_fast(s, flush) ++ deflate_state *s; ++ int flush; ++{ ++ IPos hash_head; /* head of the hash chain */ ++ int bflush; /* set if current block must be flushed */ ++ ++ for (;;) { ++ /* Make sure that we always have enough lookahead, except ++ * at the end of the input file. We need MAX_MATCH bytes ++ * for the next match, plus MIN_MATCH bytes to insert the ++ * string following the next match. ++ */ ++ if (s->lookahead < MIN_LOOKAHEAD) { ++ fill_window(s); ++ if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { ++ return need_more; ++ } ++ if (s->lookahead == 0) break; /* flush the current block */ ++ } ++ ++ /* Insert the string window[strstart .. strstart+2] in the ++ * dictionary, and set hash_head to the head of the hash chain: ++ */ ++ hash_head = NIL; ++ if (s->lookahead >= MIN_MATCH) { ++ INSERT_STRING(s, s->strstart, hash_head); ++ } ++ ++ /* Find the longest match, discarding those <= prev_length. ++ * At this point we have always match_length < MIN_MATCH ++ */ ++ if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) { ++ /* To simplify the code, we prevent matches with the string ++ * of window index 0 (in particular we have to avoid a match ++ * of the string with itself at the start of the input file). ++ */ ++ s->match_length = longest_match (s, hash_head); ++ /* longest_match() sets match_start */ ++ } ++ if (s->match_length >= MIN_MATCH) { ++ check_match(s, s->strstart, s->match_start, s->match_length); ++ ++ _tr_tally_dist(s, s->strstart - s->match_start, ++ s->match_length - MIN_MATCH, bflush); ++ ++ s->lookahead -= s->match_length; ++ ++ /* Insert new strings in the hash table only if the match length ++ * is not too large. This saves time but degrades compression. ++ */ ++#ifndef FASTEST ++ if (s->match_length <= s->max_insert_length && ++ s->lookahead >= MIN_MATCH) { ++ s->match_length--; /* string at strstart already in table */ ++ do { ++ s->strstart++; ++ INSERT_STRING(s, s->strstart, hash_head); ++ /* strstart never exceeds WSIZE-MAX_MATCH, so there are ++ * always MIN_MATCH bytes ahead. ++ */ ++ } while (--s->match_length != 0); ++ s->strstart++; ++ } else ++#endif ++ { ++ s->strstart += s->match_length; ++ s->match_length = 0; ++ s->ins_h = s->window[s->strstart]; ++ UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]); ++#if MIN_MATCH != 3 ++ Call UPDATE_HASH() MIN_MATCH-3 more times ++#endif ++ /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not ++ * matter since it will be recomputed at next deflate call. ++ */ ++ } ++ } else { ++ /* No match, output a literal byte */ ++ Tracevv((stderr,"%c", s->window[s->strstart])); ++ _tr_tally_lit (s, s->window[s->strstart], bflush); ++ s->lookahead--; ++ s->strstart++; ++ } ++ if (bflush) FLUSH_BLOCK(s, 0); ++ } ++ s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1; ++ if (flush == Z_FINISH) { ++ FLUSH_BLOCK(s, 1); ++ return finish_done; ++ } ++ if (s->last_lit) ++ FLUSH_BLOCK(s, 0); ++ return block_done; ++} ++ ++#ifndef FASTEST ++/* =========================================================================== ++ * Same as above, but achieves better compression. We use a lazy ++ * evaluation for matches: a match is finally adopted only if there is ++ * no better match at the next window position. ++ */ ++local block_state deflate_slow(s, flush) ++ deflate_state *s; ++ int flush; ++{ ++ IPos hash_head; /* head of hash chain */ ++ int bflush; /* set if current block must be flushed */ ++ ++ /* Process the input block. */ ++ for (;;) { ++ /* Make sure that we always have enough lookahead, except ++ * at the end of the input file. We need MAX_MATCH bytes ++ * for the next match, plus MIN_MATCH bytes to insert the ++ * string following the next match. ++ */ ++ if (s->lookahead < MIN_LOOKAHEAD) { ++ fill_window(s); ++ if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) { ++ return need_more; ++ } ++ if (s->lookahead == 0) break; /* flush the current block */ ++ } ++ ++ /* Insert the string window[strstart .. strstart+2] in the ++ * dictionary, and set hash_head to the head of the hash chain: ++ */ ++ hash_head = NIL; ++ if (s->lookahead >= MIN_MATCH) { ++ INSERT_STRING(s, s->strstart, hash_head); ++ } ++ ++ /* Find the longest match, discarding those <= prev_length. ++ */ ++ s->prev_length = s->match_length, s->prev_match = s->match_start; ++ s->match_length = MIN_MATCH-1; ++ ++ if (hash_head != NIL && s->prev_length < s->max_lazy_match && ++ s->strstart - hash_head <= MAX_DIST(s)) { ++ /* To simplify the code, we prevent matches with the string ++ * of window index 0 (in particular we have to avoid a match ++ * of the string with itself at the start of the input file). ++ */ ++ s->match_length = longest_match (s, hash_head); ++ /* longest_match() sets match_start */ ++ ++ if (s->match_length <= 5 && (s->strategy == Z_FILTERED ++#if TOO_FAR <= 32767 ++ || (s->match_length == MIN_MATCH && ++ s->strstart - s->match_start > TOO_FAR) ++#endif ++ )) { ++ ++ /* If prev_match is also MIN_MATCH, match_start is garbage ++ * but we will ignore the current match anyway. ++ */ ++ s->match_length = MIN_MATCH-1; ++ } ++ } ++ /* If there was a match at the previous step and the current ++ * match is not better, output the previous match: ++ */ ++ if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) { ++ uInt max_insert = s->strstart + s->lookahead - MIN_MATCH; ++ /* Do not insert strings in hash table beyond this. */ ++ ++ check_match(s, s->strstart-1, s->prev_match, s->prev_length); ++ ++ _tr_tally_dist(s, s->strstart -1 - s->prev_match, ++ s->prev_length - MIN_MATCH, bflush); ++ ++ /* Insert in hash table all strings up to the end of the match. ++ * strstart-1 and strstart are already inserted. If there is not ++ * enough lookahead, the last two strings are not inserted in ++ * the hash table. ++ */ ++ s->lookahead -= s->prev_length-1; ++ s->prev_length -= 2; ++ do { ++ if (++s->strstart <= max_insert) { ++ INSERT_STRING(s, s->strstart, hash_head); ++ } ++ } while (--s->prev_length != 0); ++ s->match_available = 0; ++ s->match_length = MIN_MATCH-1; ++ s->strstart++; ++ ++ if (bflush) FLUSH_BLOCK(s, 0); ++ ++ } else if (s->match_available) { ++ /* If there was no match at the previous position, output a ++ * single literal. If there was a match but the current match ++ * is longer, truncate the previous match to a single literal. ++ */ ++ Tracevv((stderr,"%c", s->window[s->strstart-1])); ++ _tr_tally_lit(s, s->window[s->strstart-1], bflush); ++ if (bflush) { ++ FLUSH_BLOCK_ONLY(s, 0); ++ } ++ s->strstart++; ++ s->lookahead--; ++ if (s->strm->avail_out == 0) return need_more; ++ } else { ++ /* There is no previous match to compare with, wait for ++ * the next step to decide. ++ */ ++ s->match_available = 1; ++ s->strstart++; ++ s->lookahead--; ++ } ++ } ++ Assert (flush != Z_NO_FLUSH, "no flush?"); ++ if (s->match_available) { ++ Tracevv((stderr,"%c", s->window[s->strstart-1])); ++ _tr_tally_lit(s, s->window[s->strstart-1], bflush); ++ s->match_available = 0; ++ } ++ s->insert = s->strstart < MIN_MATCH-1 ? s->strstart : MIN_MATCH-1; ++ if (flush == Z_FINISH) { ++ FLUSH_BLOCK(s, 1); ++ return finish_done; ++ } ++ if (s->last_lit) ++ FLUSH_BLOCK(s, 0); ++ return block_done; ++} ++#endif /* FASTEST */ ++ ++/* =========================================================================== ++ * For Z_RLE, simply look for runs of bytes, generate matches only of distance ++ * one. Do not maintain a hash table. (It will be regenerated if this run of ++ * deflate switches away from Z_RLE.) ++ */ ++local block_state deflate_rle(s, flush) ++ deflate_state *s; ++ int flush; ++{ ++ int bflush; /* set if current block must be flushed */ ++ uInt prev; /* byte at distance one to match */ ++ Bytef *scan, *strend; /* scan goes up to strend for length of run */ ++ ++ for (;;) { ++ /* Make sure that we always have enough lookahead, except ++ * at the end of the input file. We need MAX_MATCH bytes ++ * for the longest run, plus one for the unrolled loop. ++ */ ++ if (s->lookahead <= MAX_MATCH) { ++ fill_window(s); ++ if (s->lookahead <= MAX_MATCH && flush == Z_NO_FLUSH) { ++ return need_more; ++ } ++ if (s->lookahead == 0) break; /* flush the current block */ ++ } ++ ++ /* See how many times the previous byte repeats */ ++ s->match_length = 0; ++ if (s->lookahead >= MIN_MATCH && s->strstart > 0) { ++ scan = s->window + s->strstart - 1; ++ prev = *scan; ++ if (prev == *++scan && prev == *++scan && prev == *++scan) { ++ strend = s->window + s->strstart + MAX_MATCH; ++ do { ++ } while (prev == *++scan && prev == *++scan && ++ prev == *++scan && prev == *++scan && ++ prev == *++scan && prev == *++scan && ++ prev == *++scan && prev == *++scan && ++ scan < strend); ++ s->match_length = MAX_MATCH - (uInt)(strend - scan); ++ if (s->match_length > s->lookahead) ++ s->match_length = s->lookahead; ++ } ++ Assert(scan <= s->window+(uInt)(s->window_size-1), "wild scan"); ++ } ++ ++ /* Emit match if have run of MIN_MATCH or longer, else emit literal */ ++ if (s->match_length >= MIN_MATCH) { ++ check_match(s, s->strstart, s->strstart - 1, s->match_length); ++ ++ _tr_tally_dist(s, 1, s->match_length - MIN_MATCH, bflush); ++ ++ s->lookahead -= s->match_length; ++ s->strstart += s->match_length; ++ s->match_length = 0; ++ } else { ++ /* No match, output a literal byte */ ++ Tracevv((stderr,"%c", s->window[s->strstart])); ++ _tr_tally_lit (s, s->window[s->strstart], bflush); ++ s->lookahead--; ++ s->strstart++; ++ } ++ if (bflush) FLUSH_BLOCK(s, 0); ++ } ++ s->insert = 0; ++ if (flush == Z_FINISH) { ++ FLUSH_BLOCK(s, 1); ++ return finish_done; ++ } ++ if (s->last_lit) ++ FLUSH_BLOCK(s, 0); ++ return block_done; ++} ++ ++/* =========================================================================== ++ * For Z_HUFFMAN_ONLY, do not look for matches. Do not maintain a hash table. ++ * (It will be regenerated if this run of deflate switches away from Huffman.) ++ */ ++local block_state deflate_huff(s, flush) ++ deflate_state *s; ++ int flush; ++{ ++ int bflush; /* set if current block must be flushed */ ++ ++ for (;;) { ++ /* Make sure that we have a literal to write. */ ++ if (s->lookahead == 0) { ++ fill_window(s); ++ if (s->lookahead == 0) { ++ if (flush == Z_NO_FLUSH) ++ return need_more; ++ break; /* flush the current block */ ++ } ++ } ++ ++ /* Output a literal byte */ ++ s->match_length = 0; ++ Tracevv((stderr,"%c", s->window[s->strstart])); ++ _tr_tally_lit (s, s->window[s->strstart], bflush); ++ s->lookahead--; ++ s->strstart++; ++ if (bflush) FLUSH_BLOCK(s, 0); ++ } ++ s->insert = 0; ++ if (flush == Z_FINISH) { ++ FLUSH_BLOCK(s, 1); ++ return finish_done; ++ } ++ if (s->last_lit) ++ FLUSH_BLOCK(s, 0); ++ return block_done; ++} +diff --git a/lib/libzlib/deflate.h b/lib/libzlib/deflate.h +new file mode 100644 +index 000000000..4fde82876 +--- /dev/null ++++ b/lib/libzlib/deflate.h +@@ -0,0 +1,350 @@ ++/* SPDX-License-Identifier: Zlib */ ++/* deflate.h -- internal compression state ++ * Copyright (C) 1995-2016 Jean-loup Gailly ++ * For conditions of distribution and use, see copyright notice in zlib.h ++ */ ++ ++/* WARNING: this file should *not* be used by applications. It is ++ part of the implementation of the compression library and is ++ subject to change. Applications should only use zlib.h. ++ */ ++ ++/* @(#) $Id$ */ ++ ++#ifndef DEFLATE_H ++#define DEFLATE_H ++ ++#include "zutil.h" ++ ++/* define NO_GZIP when compiling if you want to disable gzip header and ++ trailer creation by deflate(). NO_GZIP would be used to avoid linking in ++ the crc code when it is not needed. For shared libraries, gzip encoding ++ should be left enabled. */ ++#ifndef NO_GZIP ++# define GZIP ++#endif ++ ++/* =========================================================================== ++ * Internal compression state. ++ */ ++ ++#define LENGTH_CODES 29 ++/* number of length codes, not counting the special END_BLOCK code */ ++ ++#define LITERALS 256 ++/* number of literal bytes 0..255 */ ++ ++#define L_CODES (LITERALS+1+LENGTH_CODES) ++/* number of Literal or Length codes, including the END_BLOCK code */ ++ ++#define D_CODES 30 ++/* number of distance codes */ ++ ++#define BL_CODES 19 ++/* number of codes used to transfer the bit lengths */ ++ ++#define HEAP_SIZE (2*L_CODES+1) ++/* maximum heap size */ ++ ++#define MAX_BITS 15 ++/* All codes must not exceed MAX_BITS bits */ ++ ++#define Buf_size 16 ++/* size of bit buffer in bi_buf */ ++ ++#define INIT_STATE 42 /* zlib header -> BUSY_STATE */ ++#ifdef GZIP ++# define GZIP_STATE 57 /* gzip header -> BUSY_STATE | EXTRA_STATE */ ++#endif ++#define EXTRA_STATE 69 /* gzip extra block -> NAME_STATE */ ++#define NAME_STATE 73 /* gzip file name -> COMMENT_STATE */ ++#define COMMENT_STATE 91 /* gzip comment -> HCRC_STATE */ ++#define HCRC_STATE 103 /* gzip header CRC -> BUSY_STATE */ ++#define BUSY_STATE 113 /* deflate -> FINISH_STATE */ ++#define FINISH_STATE 666 /* stream complete */ ++/* Stream status */ ++ ++ ++/* Data structure describing a single value and its code string. */ ++typedef struct ct_data_s { ++ union { ++ ush freq; /* frequency count */ ++ ush code; /* bit string */ ++ } fc; ++ union { ++ ush dad; /* father node in Huffman tree */ ++ ush len; /* length of bit string */ ++ } dl; ++} FAR ct_data; ++ ++#define Freq fc.freq ++#define Code fc.code ++#define Dad dl.dad ++#define Len dl.len ++ ++typedef struct static_tree_desc_s static_tree_desc; ++ ++typedef struct tree_desc_s { ++ ct_data *dyn_tree; /* the dynamic tree */ ++ int max_code; /* largest code with non zero frequency */ ++ const static_tree_desc *stat_desc; /* the corresponding static tree */ ++} FAR tree_desc; ++ ++typedef ush Pos; ++typedef Pos FAR Posf; ++typedef unsigned IPos; ++ ++/* A Pos is an index in the character window. We use short instead of int to ++ * save space in the various tables. IPos is used only for parameter passing. ++ */ ++ ++typedef struct internal_state { ++ z_streamp strm; /* pointer back to this zlib stream */ ++ int status; /* as the name implies */ ++ Bytef *pending_buf; /* output still pending */ ++ ulg pending_buf_size; /* size of pending_buf */ ++ Bytef *pending_out; /* next pending byte to output to the stream */ ++ ulg pending; /* nb of bytes in the pending buffer */ ++ int wrap; /* bit 0 true for zlib, bit 1 true for gzip */ ++ gz_headerp gzhead; /* gzip header information to write */ ++ ulg gzindex; /* where in extra, name, or comment */ ++ Byte method; /* can only be DEFLATED */ ++ int last_flush; /* value of flush param for previous deflate call */ ++ ++ /* used by deflate.c: */ ++ ++ uInt w_size; /* LZ77 window size (32K by default) */ ++ uInt w_bits; /* log2(w_size) (8..16) */ ++ uInt w_mask; /* w_size - 1 */ ++ ++ Bytef *window; ++ /* Sliding window. Input bytes are read into the second half of the window, ++ * and move to the first half later to keep a dictionary of at least wSize ++ * bytes. With this organization, matches are limited to a distance of ++ * wSize-MAX_MATCH bytes, but this ensures that IO is always ++ * performed with a length multiple of the block size. Also, it limits ++ * the window size to 64K, which is quite useful on MSDOS. ++ * To do: use the user input buffer as sliding window. ++ */ ++ ++ ulg window_size; ++ /* Actual size of window: 2*wSize, except when the user input buffer ++ * is directly used as sliding window. ++ */ ++ ++ Posf *prev; ++ /* Link to older string with same hash index. To limit the size of this ++ * array to 64K, this link is maintained only for the last 32K strings. ++ * An index in this array is thus a window index modulo 32K. ++ */ ++ ++ Posf *head; /* Heads of the hash chains or NIL. */ ++ ++ uInt ins_h; /* hash index of string to be inserted */ ++ uInt hash_size; /* number of elements in hash table */ ++ uInt hash_bits; /* log2(hash_size) */ ++ uInt hash_mask; /* hash_size-1 */ ++ ++ uInt hash_shift; ++ /* Number of bits by which ins_h must be shifted at each input ++ * step. It must be such that after MIN_MATCH steps, the oldest ++ * byte no longer takes part in the hash key, that is: ++ * hash_shift * MIN_MATCH >= hash_bits ++ */ ++ ++ long block_start; ++ /* Window position at the beginning of the current output block. Gets ++ * negative when the window is moved backwards. ++ */ ++ ++ uInt match_length; /* length of best match */ ++ IPos prev_match; /* previous match */ ++ int match_available; /* set if previous match exists */ ++ uInt strstart; /* start of string to insert */ ++ uInt match_start; /* start of matching string */ ++ uInt lookahead; /* number of valid bytes ahead in window */ ++ ++ uInt prev_length; ++ /* Length of the best match at previous step. Matches not greater than this ++ * are discarded. This is used in the lazy match evaluation. ++ */ ++ ++ uInt max_chain_length; ++ /* To speed up deflation, hash chains are never searched beyond this ++ * length. A higher limit improves compression ratio but degrades the ++ * speed. ++ */ ++ ++ uInt max_lazy_match; ++ /* Attempt to find a better match only when the current match is strictly ++ * smaller than this value. This mechanism is used only for compression ++ * levels >= 4. ++ */ ++# define max_insert_length max_lazy_match ++ /* Insert new strings in the hash table only if the match length is not ++ * greater than this length. This saves time but degrades compression. ++ * max_insert_length is used only for compression levels <= 3. ++ */ ++ ++ int level; /* compression level (1..9) */ ++ int strategy; /* favor or force Huffman coding*/ ++ ++ uInt good_match; ++ /* Use a faster search when the previous match is longer than this */ ++ ++ int nice_match; /* Stop searching when current match exceeds this */ ++ ++ /* used by trees.c: */ ++ /* Didn't use ct_data typedef below to suppress compiler warning */ ++ struct ct_data_s dyn_ltree[HEAP_SIZE]; /* literal and length tree */ ++ struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */ ++ struct ct_data_s bl_tree[2*BL_CODES+1]; /* Huffman tree for bit lengths */ ++ ++ struct tree_desc_s l_desc; /* desc. for literal tree */ ++ struct tree_desc_s d_desc; /* desc. for distance tree */ ++ struct tree_desc_s bl_desc; /* desc. for bit length tree */ ++ ++ ush bl_count[MAX_BITS+1]; ++ /* number of codes at each bit length for an optimal tree */ ++ ++ int heap[2*L_CODES+1]; /* heap used to build the Huffman trees */ ++ int heap_len; /* number of elements in the heap */ ++ int heap_max; /* element of largest frequency */ ++ /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used. ++ * The same heap array is used to build all trees. ++ */ ++ ++ uch depth[2*L_CODES+1]; ++ /* Depth of each subtree used as tie breaker for trees of equal frequency ++ */ ++ ++ uchf *l_buf; /* buffer for literals or lengths */ ++ ++ uInt lit_bufsize; ++ /* Size of match buffer for literals/lengths. There are 4 reasons for ++ * limiting lit_bufsize to 64K: ++ * - frequencies can be kept in 16 bit counters ++ * - if compression is not successful for the first block, all input ++ * data is still in the window so we can still emit a stored block even ++ * when input comes from standard input. (This can also be done for ++ * all blocks if lit_bufsize is not greater than 32K.) ++ * - if compression is not successful for a file smaller than 64K, we can ++ * even emit a stored file instead of a stored block (saving 5 bytes). ++ * This is applicable only for zip (not gzip or zlib). ++ * - creating new Huffman trees less frequently may not provide fast ++ * adaptation to changes in the input data statistics. (Take for ++ * example a binary file with poorly compressible code followed by ++ * a highly compressible string table.) Smaller buffer sizes give ++ * fast adaptation but have of course the overhead of transmitting ++ * trees more frequently. ++ * - I can't count above 4 ++ */ ++ ++ uInt last_lit; /* running index in l_buf */ ++ ++ ushf *d_buf; ++ /* Buffer for distances. To simplify the code, d_buf and l_buf have ++ * the same number of elements. To use different lengths, an extra flag ++ * array would be necessary. ++ */ ++ ++ ulg opt_len; /* bit length of current block with optimal trees */ ++ ulg static_len; /* bit length of current block with static trees */ ++ uInt matches; /* number of string matches in current block */ ++ uInt insert; /* bytes at end of window left to insert */ ++ ++#ifdef ZLIB_DEBUG ++ ulg compressed_len; /* total bit length of compressed file mod 2^32 */ ++ ulg bits_sent; /* bit length of compressed data sent mod 2^32 */ ++#endif ++ ++ ush bi_buf; ++ /* Output buffer. bits are inserted starting at the bottom (least ++ * significant bits). ++ */ ++ int bi_valid; ++ /* Number of valid bits in bi_buf. All bits above the last valid bit ++ * are always zero. ++ */ ++ ++ ulg high_water; ++ /* High water mark offset in window for initialized bytes -- bytes above ++ * this are set to zero in order to avoid memory check warnings when ++ * longest match routines access bytes past the input. This is then ++ * updated to the new high water mark. ++ */ ++ ++} FAR deflate_state; ++ ++/* Output a byte on the stream. ++ * IN assertion: there is enough room in pending_buf. ++ */ ++#define put_byte(s, c) {s->pending_buf[s->pending++] = (Bytef)(c);} ++ ++ ++#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1) ++/* Minimum amount of lookahead, except at the end of the input file. ++ * See deflate.c for comments about the MIN_MATCH+1. ++ */ ++ ++#define MAX_DIST(s) ((s)->w_size-MIN_LOOKAHEAD) ++/* In order to simplify the code, particularly on 16 bit machines, match ++ * distances are limited to MAX_DIST instead of WSIZE. ++ */ ++ ++#define WIN_INIT MAX_MATCH ++/* Number of bytes after end of data in window to initialize in order to avoid ++ memory checker errors from longest match routines */ ++ ++ /* in trees.c */ ++void ZLIB_INTERNAL _tr_init OF((deflate_state *s)); ++int ZLIB_INTERNAL _tr_tally OF((deflate_state *s, unsigned dist, unsigned lc)); ++void ZLIB_INTERNAL _tr_flush_block OF((deflate_state *s, charf *buf, ++ ulg stored_len, int last)); ++void ZLIB_INTERNAL _tr_flush_bits OF((deflate_state *s)); ++void ZLIB_INTERNAL _tr_align OF((deflate_state *s)); ++void ZLIB_INTERNAL _tr_stored_block OF((deflate_state *s, charf *buf, ++ ulg stored_len, int last)); ++ ++#define d_code(dist) \ ++ ((dist) < 256 ? _dist_code[dist] : _dist_code[256+((dist)>>7)]) ++/* Mapping from a distance to a distance code. dist is the distance - 1 and ++ * must not have side effects. _dist_code[256] and _dist_code[257] are never ++ * used. ++ */ ++ ++#ifndef ZLIB_DEBUG ++/* Inline versions of _tr_tally for speed: */ ++ ++#if defined(GEN_TREES_H) || !defined(STDC) ++ extern uch ZLIB_INTERNAL _length_code[]; ++ extern uch ZLIB_INTERNAL _dist_code[]; ++#else ++ extern const uch ZLIB_INTERNAL _length_code[]; ++ extern const uch ZLIB_INTERNAL _dist_code[]; ++#endif ++ ++# define _tr_tally_lit(s, c, flush) \ ++ { uch cc = (c); \ ++ s->d_buf[s->last_lit] = 0; \ ++ s->l_buf[s->last_lit++] = cc; \ ++ s->dyn_ltree[cc].Freq++; \ ++ flush = (s->last_lit == s->lit_bufsize-1); \ ++ } ++# define _tr_tally_dist(s, distance, length, flush) \ ++ { uch len = (uch)(length); \ ++ ush dist = (ush)(distance); \ ++ s->d_buf[s->last_lit] = dist; \ ++ s->l_buf[s->last_lit++] = len; \ ++ dist--; \ ++ s->dyn_ltree[_length_code[len]+LITERALS+1].Freq++; \ ++ s->dyn_dtree[d_code(dist)].Freq++; \ ++ flush = (s->last_lit == s->lit_bufsize-1); \ ++ } ++#else ++# define _tr_tally_lit(s, c, flush) flush = _tr_tally(s, 0, c) ++# define _tr_tally_dist(s, distance, length, flush) \ ++ flush = _tr_tally(s, distance, length) ++#endif ++ ++#endif /* DEFLATE_H */ +diff --git a/core/lib/zlib/gzguts.h b/lib/libzlib/gzguts.h +similarity index 100% +rename from core/lib/zlib/gzguts.h +rename to lib/libzlib/gzguts.h +diff --git a/core/lib/zlib/inffast.c b/lib/libzlib/inffast.c +similarity index 100% +rename from core/lib/zlib/inffast.c +rename to lib/libzlib/inffast.c +diff --git a/core/lib/zlib/inffast.h b/lib/libzlib/inffast.h +similarity index 100% +rename from core/lib/zlib/inffast.h +rename to lib/libzlib/inffast.h +diff --git a/core/lib/zlib/inffixed.h b/lib/libzlib/inffixed.h +similarity index 100% +rename from core/lib/zlib/inffixed.h +rename to lib/libzlib/inffixed.h +diff --git a/core/lib/zlib/inflate.c b/lib/libzlib/inflate.c +similarity index 100% +rename from core/lib/zlib/inflate.c +rename to lib/libzlib/inflate.c +diff --git a/core/lib/zlib/inflate.h b/lib/libzlib/inflate.h +similarity index 100% +rename from core/lib/zlib/inflate.h +rename to lib/libzlib/inflate.h +diff --git a/core/lib/zlib/inftrees.c b/lib/libzlib/inftrees.c +similarity index 100% +rename from core/lib/zlib/inftrees.c +rename to lib/libzlib/inftrees.c +diff --git a/core/lib/zlib/inftrees.h b/lib/libzlib/inftrees.h +similarity index 100% +rename from core/lib/zlib/inftrees.h +rename to lib/libzlib/inftrees.h +diff --git a/core/lib/zlib/sub.mk b/lib/libzlib/sub.mk +similarity index 78% +rename from core/lib/zlib/sub.mk +rename to lib/libzlib/sub.mk +index d4f225dfb..457a5d213 100644 +--- a/core/lib/zlib/sub.mk ++++ b/lib/libzlib/sub.mk +@@ -1,8 +1,11 @@ + global-incdirs-y += . + srcs-y += adler32.c ++srcs-y += crc32.c ++srcs-y += deflate.c + srcs-y += inffast.c + srcs-y += inflate.c + srcs-y += inftrees.c ++srcs-y += trees.c + srcs-y += zutil.c + cflags-remove-y += -Wold-style-definition + cflags-remove-y += -Wswitch-default +diff --git a/lib/libzlib/trees.c b/lib/libzlib/trees.c +new file mode 100644 +index 000000000..258c38f72 +--- /dev/null ++++ b/lib/libzlib/trees.c +@@ -0,0 +1,1204 @@ ++// SPDX-License-Identifier: Zlib ++/* trees.c -- output deflated data using Huffman coding ++ * Copyright (C) 1995-2017 Jean-loup Gailly ++ * detect_data_type() function provided freely by Cosmin Truta, 2006 ++ * For conditions of distribution and use, see copyright notice in zlib.h ++ */ ++ ++/* ++ * ALGORITHM ++ * ++ * The "deflation" process uses several Huffman trees. The more ++ * common source values are represented by shorter bit sequences. ++ * ++ * Each code tree is stored in a compressed form which is itself ++ * a Huffman encoding of the lengths of all the code strings (in ++ * ascending order by source values). The actual code strings are ++ * reconstructed from the lengths in the inflate process, as described ++ * in the deflate specification. ++ * ++ * REFERENCES ++ * ++ * Deutsch, L.P.,"'Deflate' Compressed Data Format Specification". ++ * Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc ++ * ++ * Storer, James A. ++ * Data Compression: Methods and Theory, pp. 49-50. ++ * Computer Science Press, 1988. ISBN 0-7167-8156-5. ++ * ++ * Sedgewick, R. ++ * Algorithms, p290. ++ * Addison-Wesley, 1983. ISBN 0-201-06672-6. ++ */ ++ ++/* @(#) $Id$ */ ++ ++/* #define GEN_TREES_H */ ++ ++#include "deflate.h" ++ ++#ifdef ZLIB_DEBUG ++# include ++#endif ++ ++/* =========================================================================== ++ * Constants ++ */ ++ ++#define MAX_BL_BITS 7 ++/* Bit length codes must not exceed MAX_BL_BITS bits */ ++ ++#define END_BLOCK 256 ++/* end of block literal code */ ++ ++#define REP_3_6 16 ++/* repeat previous bit length 3-6 times (2 bits of repeat count) */ ++ ++#define REPZ_3_10 17 ++/* repeat a zero length 3-10 times (3 bits of repeat count) */ ++ ++#define REPZ_11_138 18 ++/* repeat a zero length 11-138 times (7 bits of repeat count) */ ++ ++local const int extra_lbits[LENGTH_CODES] /* extra bits for each length code */ ++ = {0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0}; ++ ++local const int extra_dbits[D_CODES] /* extra bits for each distance code */ ++ = {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13}; ++ ++local const int extra_blbits[BL_CODES]/* extra bits for each bit length code */ ++ = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7}; ++ ++local const uch bl_order[BL_CODES] ++ = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15}; ++/* The lengths of the bit length codes are sent in order of decreasing ++ * probability, to avoid transmitting the lengths for unused bit length codes. ++ */ ++ ++/* =========================================================================== ++ * Local data. These are initialized only once. ++ */ ++ ++#define DIST_CODE_LEN 512 /* see definition of array dist_code below */ ++ ++#if defined(GEN_TREES_H) || !defined(STDC) ++/* non ANSI compilers may not accept trees.h */ ++ ++local ct_data static_ltree[L_CODES+2]; ++/* The static literal tree. Since the bit lengths are imposed, there is no ++ * need for the L_CODES extra codes used during heap construction. However ++ * The codes 286 and 287 are needed to build a canonical tree (see _tr_init ++ * below). ++ */ ++ ++local ct_data static_dtree[D_CODES]; ++/* The static distance tree. (Actually a trivial tree since all codes use ++ * 5 bits.) ++ */ ++ ++uch _dist_code[DIST_CODE_LEN]; ++/* Distance codes. The first 256 values correspond to the distances ++ * 3 .. 258, the last 256 values correspond to the top 8 bits of ++ * the 15 bit distances. ++ */ ++ ++uch _length_code[MAX_MATCH-MIN_MATCH+1]; ++/* length code for each normalized match length (0 == MIN_MATCH) */ ++ ++local int base_length[LENGTH_CODES]; ++/* First normalized length for each code (0 = MIN_MATCH) */ ++ ++local int base_dist[D_CODES]; ++/* First normalized distance for each code (0 = distance of 1) */ ++ ++#else ++# include "trees.h" ++#endif /* GEN_TREES_H */ ++ ++struct static_tree_desc_s { ++ const ct_data *static_tree; /* static tree or NULL */ ++ const intf *extra_bits; /* extra bits for each code or NULL */ ++ int extra_base; /* base index for extra_bits */ ++ int elems; /* max number of elements in the tree */ ++ int max_length; /* max bit length for the codes */ ++}; ++ ++local const static_tree_desc static_l_desc = ++{static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS}; ++ ++local const static_tree_desc static_d_desc = ++{static_dtree, extra_dbits, 0, D_CODES, MAX_BITS}; ++ ++local const static_tree_desc static_bl_desc = ++{(const ct_data *)0, extra_blbits, 0, BL_CODES, MAX_BL_BITS}; ++ ++/* =========================================================================== ++ * Local (static) routines in this file. ++ */ ++ ++local void tr_static_init OF((void)); ++local void init_block OF((deflate_state *s)); ++local void pqdownheap OF((deflate_state *s, ct_data *tree, int k)); ++local void gen_bitlen OF((deflate_state *s, tree_desc *desc)); ++local void gen_codes OF((ct_data *tree, int max_code, ushf *bl_count)); ++local void build_tree OF((deflate_state *s, tree_desc *desc)); ++local void scan_tree OF((deflate_state *s, ct_data *tree, int max_code)); ++local void send_tree OF((deflate_state *s, ct_data *tree, int max_code)); ++local int build_bl_tree OF((deflate_state *s)); ++local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes, ++ int blcodes)); ++local void compress_block OF((deflate_state *s, const ct_data *ltree, ++ const ct_data *dtree)); ++local int detect_data_type OF((deflate_state *s)); ++local unsigned bi_reverse OF((unsigned value, int length)); ++local void bi_windup OF((deflate_state *s)); ++local void bi_flush OF((deflate_state *s)); ++ ++#ifdef GEN_TREES_H ++local void gen_trees_header OF((void)); ++#endif ++ ++#ifndef ZLIB_DEBUG ++# define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len) ++ /* Send a code of the given tree. c and tree must not have side effects */ ++ ++#else /* !ZLIB_DEBUG */ ++# define send_code(s, c, tree) \ ++ { if (z_verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \ ++ send_bits(s, tree[c].Code, tree[c].Len); } ++#endif ++ ++/* =========================================================================== ++ * Output a short LSB first on the stream. ++ * IN assertion: there is enough room in pendingBuf. ++ */ ++#define put_short(s, w) { \ ++ put_byte(s, (uch)((w) & 0xff)); \ ++ put_byte(s, (uch)((ush)(w) >> 8)); \ ++} ++ ++/* =========================================================================== ++ * Send a value on a given number of bits. ++ * IN assertion: length <= 16 and value fits in length bits. ++ */ ++#ifdef ZLIB_DEBUG ++local void send_bits OF((deflate_state *s, int value, int length)); ++ ++local void send_bits(s, value, length) ++ deflate_state *s; ++ int value; /* value to send */ ++ int length; /* number of bits */ ++{ ++ Tracevv((stderr," l %2d v %4x ", length, value)); ++ Assert(length > 0 && length <= 15, "invalid length"); ++ s->bits_sent += (ulg)length; ++ ++ /* If not enough room in bi_buf, use (valid) bits from bi_buf and ++ * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid)) ++ * unused bits in value. ++ */ ++ if (s->bi_valid > (int)Buf_size - length) { ++ s->bi_buf |= (ush)value << s->bi_valid; ++ put_short(s, s->bi_buf); ++ s->bi_buf = (ush)value >> (Buf_size - s->bi_valid); ++ s->bi_valid += length - Buf_size; ++ } else { ++ s->bi_buf |= (ush)value << s->bi_valid; ++ s->bi_valid += length; ++ } ++} ++#else /* !ZLIB_DEBUG */ ++ ++#define send_bits(s, value, length) \ ++{ int len = length;\ ++ if (s->bi_valid > (int)Buf_size - len) {\ ++ int val = (int)value;\ ++ s->bi_buf |= (ush)val << s->bi_valid;\ ++ put_short(s, s->bi_buf);\ ++ s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\ ++ s->bi_valid += len - Buf_size;\ ++ } else {\ ++ s->bi_buf |= (ush)(value) << s->bi_valid;\ ++ s->bi_valid += len;\ ++ }\ ++} ++#endif /* ZLIB_DEBUG */ ++ ++ ++/* the arguments must not have side effects */ ++ ++/* =========================================================================== ++ * Initialize the various 'constant' tables. ++ */ ++local void tr_static_init() ++{ ++#if defined(GEN_TREES_H) || !defined(STDC) ++ static int static_init_done = 0; ++ int n; /* iterates over tree elements */ ++ int bits; /* bit counter */ ++ int length; /* length value */ ++ int code; /* code value */ ++ int dist; /* distance index */ ++ ush bl_count[MAX_BITS+1]; ++ /* number of codes at each bit length for an optimal tree */ ++ ++ if (static_init_done) return; ++ ++ /* For some embedded targets, global variables are not initialized: */ ++#ifdef NO_INIT_GLOBAL_POINTERS ++ static_l_desc.static_tree = static_ltree; ++ static_l_desc.extra_bits = extra_lbits; ++ static_d_desc.static_tree = static_dtree; ++ static_d_desc.extra_bits = extra_dbits; ++ static_bl_desc.extra_bits = extra_blbits; ++#endif ++ ++ /* Initialize the mapping length (0..255) -> length code (0..28) */ ++ length = 0; ++ for (code = 0; code < LENGTH_CODES-1; code++) { ++ base_length[code] = length; ++ for (n = 0; n < (1< dist code (0..29) */ ++ dist = 0; ++ for (code = 0 ; code < 16; code++) { ++ base_dist[code] = dist; ++ for (n = 0; n < (1<>= 7; /* from now on, all distances are divided by 128 */ ++ for ( ; code < D_CODES; code++) { ++ base_dist[code] = dist << 7; ++ for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) { ++ _dist_code[256 + dist++] = (uch)code; ++ } ++ } ++ Assert (dist == 256, "tr_static_init: 256+dist != 512"); ++ ++ /* Construct the codes of the static literal tree */ ++ for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0; ++ n = 0; ++ while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++; ++ while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++; ++ while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++; ++ while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++; ++ /* Codes 286 and 287 do not exist, but we must include them in the ++ * tree construction to get a canonical Huffman tree (longest code ++ * all ones) ++ */ ++ gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count); ++ ++ /* The static distance tree is trivial: */ ++ for (n = 0; n < D_CODES; n++) { ++ static_dtree[n].Len = 5; ++ static_dtree[n].Code = bi_reverse((unsigned)n, 5); ++ } ++ static_init_done = 1; ++ ++# ifdef GEN_TREES_H ++ gen_trees_header(); ++# endif ++#endif /* defined(GEN_TREES_H) || !defined(STDC) */ ++} ++ ++/* =========================================================================== ++ * Genererate the file trees.h describing the static trees. ++ */ ++#ifdef GEN_TREES_H ++# ifndef ZLIB_DEBUG ++# include ++# endif ++ ++# define SEPARATOR(i, last, width) \ ++ ((i) == (last)? "\n};\n\n" : \ ++ ((i) % (width) == (width)-1 ? ",\n" : ", ")) ++ ++void gen_trees_header() ++{ ++ FILE *header = fopen("trees.h", "w"); ++ int i; ++ ++ Assert (header != NULL, "Can't open trees.h"); ++ fprintf(header, ++ "/* header created automatically with -DGEN_TREES_H */\n\n"); ++ ++ fprintf(header, "local const ct_data static_ltree[L_CODES+2] = {\n"); ++ for (i = 0; i < L_CODES+2; i++) { ++ fprintf(header, "{{%3u},{%3u}}%s", static_ltree[i].Code, ++ static_ltree[i].Len, SEPARATOR(i, L_CODES+1, 5)); ++ } ++ ++ fprintf(header, "local const ct_data static_dtree[D_CODES] = {\n"); ++ for (i = 0; i < D_CODES; i++) { ++ fprintf(header, "{{%2u},{%2u}}%s", static_dtree[i].Code, ++ static_dtree[i].Len, SEPARATOR(i, D_CODES-1, 5)); ++ } ++ ++ fprintf(header, "const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = {\n"); ++ for (i = 0; i < DIST_CODE_LEN; i++) { ++ fprintf(header, "%2u%s", _dist_code[i], ++ SEPARATOR(i, DIST_CODE_LEN-1, 20)); ++ } ++ ++ fprintf(header, ++ "const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= {\n"); ++ for (i = 0; i < MAX_MATCH-MIN_MATCH+1; i++) { ++ fprintf(header, "%2u%s", _length_code[i], ++ SEPARATOR(i, MAX_MATCH-MIN_MATCH, 20)); ++ } ++ ++ fprintf(header, "local const int base_length[LENGTH_CODES] = {\n"); ++ for (i = 0; i < LENGTH_CODES; i++) { ++ fprintf(header, "%1u%s", base_length[i], ++ SEPARATOR(i, LENGTH_CODES-1, 20)); ++ } ++ ++ fprintf(header, "local const int base_dist[D_CODES] = {\n"); ++ for (i = 0; i < D_CODES; i++) { ++ fprintf(header, "%5u%s", base_dist[i], ++ SEPARATOR(i, D_CODES-1, 10)); ++ } ++ ++ fclose(header); ++} ++#endif /* GEN_TREES_H */ ++ ++/* =========================================================================== ++ * Initialize the tree data structures for a new zlib stream. ++ */ ++void ZLIB_INTERNAL _tr_init(s) ++ deflate_state *s; ++{ ++ tr_static_init(); ++ ++ s->l_desc.dyn_tree = s->dyn_ltree; ++ s->l_desc.stat_desc = &static_l_desc; ++ ++ s->d_desc.dyn_tree = s->dyn_dtree; ++ s->d_desc.stat_desc = &static_d_desc; ++ ++ s->bl_desc.dyn_tree = s->bl_tree; ++ s->bl_desc.stat_desc = &static_bl_desc; ++ ++ s->bi_buf = 0; ++ s->bi_valid = 0; ++#ifdef ZLIB_DEBUG ++ s->compressed_len = 0L; ++ s->bits_sent = 0L; ++#endif ++ ++ /* Initialize the first block of the first file: */ ++ init_block(s); ++} ++ ++/* =========================================================================== ++ * Initialize a new block. ++ */ ++local void init_block(s) ++ deflate_state *s; ++{ ++ int n; /* iterates over tree elements */ ++ ++ /* Initialize the trees. */ ++ for (n = 0; n < L_CODES; n++) s->dyn_ltree[n].Freq = 0; ++ for (n = 0; n < D_CODES; n++) s->dyn_dtree[n].Freq = 0; ++ for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0; ++ ++ s->dyn_ltree[END_BLOCK].Freq = 1; ++ s->opt_len = s->static_len = 0L; ++ s->last_lit = s->matches = 0; ++} ++ ++#define SMALLEST 1 ++/* Index within the heap array of least frequent node in the Huffman tree */ ++ ++ ++/* =========================================================================== ++ * Remove the smallest element from the heap and recreate the heap with ++ * one less element. Updates heap and heap_len. ++ */ ++#define pqremove(s, tree, top) \ ++{\ ++ top = s->heap[SMALLEST]; \ ++ s->heap[SMALLEST] = s->heap[s->heap_len--]; \ ++ pqdownheap(s, tree, SMALLEST); \ ++} ++ ++/* =========================================================================== ++ * Compares to subtrees, using the tree depth as tie breaker when ++ * the subtrees have equal frequency. This minimizes the worst case length. ++ */ ++#define smaller(tree, n, m, depth) \ ++ (tree[n].Freq < tree[m].Freq || \ ++ (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m])) ++ ++/* =========================================================================== ++ * Restore the heap property by moving down the tree starting at node k, ++ * exchanging a node with the smallest of its two sons if necessary, stopping ++ * when the heap property is re-established (each father smaller than its ++ * two sons). ++ */ ++local void pqdownheap(s, tree, k) ++ deflate_state *s; ++ ct_data *tree; /* the tree to restore */ ++ int k; /* node to move down */ ++{ ++ int v = s->heap[k]; ++ int j = k << 1; /* left son of k */ ++ while (j <= s->heap_len) { ++ /* Set j to the smallest of the two sons: */ ++ if (j < s->heap_len && ++ smaller(tree, s->heap[j+1], s->heap[j], s->depth)) { ++ j++; ++ } ++ /* Exit if v is smaller than both sons */ ++ if (smaller(tree, v, s->heap[j], s->depth)) break; ++ ++ /* Exchange v with the smallest son */ ++ s->heap[k] = s->heap[j]; k = j; ++ ++ /* And continue down the tree, setting j to the left son of k */ ++ j <<= 1; ++ } ++ s->heap[k] = v; ++} ++ ++/* =========================================================================== ++ * Compute the optimal bit lengths for a tree and update the total bit length ++ * for the current block. ++ * IN assertion: the fields freq and dad are set, heap[heap_max] and ++ * above are the tree nodes sorted by increasing frequency. ++ * OUT assertions: the field len is set to the optimal bit length, the ++ * array bl_count contains the frequencies for each bit length. ++ * The length opt_len is updated; static_len is also updated if stree is ++ * not null. ++ */ ++local void gen_bitlen(s, desc) ++ deflate_state *s; ++ tree_desc *desc; /* the tree descriptor */ ++{ ++ ct_data *tree = desc->dyn_tree; ++ int max_code = desc->max_code; ++ const ct_data *stree = desc->stat_desc->static_tree; ++ const intf *extra = desc->stat_desc->extra_bits; ++ int base = desc->stat_desc->extra_base; ++ int max_length = desc->stat_desc->max_length; ++ int h; /* heap index */ ++ int n, m; /* iterate over the tree elements */ ++ int bits; /* bit length */ ++ int xbits; /* extra bits */ ++ ush f; /* frequency */ ++ int overflow = 0; /* number of elements with bit length too large */ ++ ++ for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0; ++ ++ /* In a first pass, compute the optimal bit lengths (which may ++ * overflow in the case of the bit length tree). ++ */ ++ tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */ ++ ++ for (h = s->heap_max+1; h < HEAP_SIZE; h++) { ++ n = s->heap[h]; ++ bits = tree[tree[n].Dad].Len + 1; ++ if (bits > max_length) bits = max_length, overflow++; ++ tree[n].Len = (ush)bits; ++ /* We overwrite tree[n].Dad which is no longer needed */ ++ ++ if (n > max_code) continue; /* not a leaf node */ ++ ++ s->bl_count[bits]++; ++ xbits = 0; ++ if (n >= base) xbits = extra[n-base]; ++ f = tree[n].Freq; ++ s->opt_len += (ulg)f * (unsigned)(bits + xbits); ++ if (stree) s->static_len += (ulg)f * (unsigned)(stree[n].Len + xbits); ++ } ++ if (overflow == 0) return; ++ ++ Tracev((stderr,"\nbit length overflow\n")); ++ /* This happens for example on obj2 and pic of the Calgary corpus */ ++ ++ /* Find the first bit length which could increase: */ ++ do { ++ bits = max_length-1; ++ while (s->bl_count[bits] == 0) bits--; ++ s->bl_count[bits]--; /* move one leaf down the tree */ ++ s->bl_count[bits+1] += 2; /* move one overflow item as its brother */ ++ s->bl_count[max_length]--; ++ /* The brother of the overflow item also moves one step up, ++ * but this does not affect bl_count[max_length] ++ */ ++ overflow -= 2; ++ } while (overflow > 0); ++ ++ /* Now recompute all bit lengths, scanning in increasing frequency. ++ * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all ++ * lengths instead of fixing only the wrong ones. This idea is taken ++ * from 'ar' written by Haruhiko Okumura.) ++ */ ++ for (bits = max_length; bits != 0; bits--) { ++ n = s->bl_count[bits]; ++ while (n != 0) { ++ m = s->heap[--h]; ++ if (m > max_code) continue; ++ if ((unsigned) tree[m].Len != (unsigned) bits) { ++ Tracev((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits)); ++ s->opt_len += ((ulg)bits - tree[m].Len) * tree[m].Freq; ++ tree[m].Len = (ush)bits; ++ } ++ n--; ++ } ++ } ++} ++ ++/* =========================================================================== ++ * Generate the codes for a given tree and bit counts (which need not be ++ * optimal). ++ * IN assertion: the array bl_count contains the bit length statistics for ++ * the given tree and the field len is set for all tree elements. ++ * OUT assertion: the field code is set for all tree elements of non ++ * zero code length. ++ */ ++local void gen_codes (tree, max_code, bl_count) ++ ct_data *tree; /* the tree to decorate */ ++ int max_code; /* largest code with non zero frequency */ ++ ushf *bl_count; /* number of codes at each bit length */ ++{ ++ ush next_code[MAX_BITS+1]; /* next code value for each bit length */ ++ unsigned code = 0; /* running code value */ ++ int bits; /* bit index */ ++ int n; /* code index */ ++ ++ /* The distribution counts are first used to generate the code values ++ * without bit reversal. ++ */ ++ for (bits = 1; bits <= MAX_BITS; bits++) { ++ code = (code + bl_count[bits-1]) << 1; ++ next_code[bits] = (ush)code; ++ } ++ /* Check that the bit counts in bl_count are consistent. The last code ++ * must be all ones. ++ */ ++ Assert (code + bl_count[MAX_BITS]-1 == (1<dyn_tree; ++ const ct_data *stree = desc->stat_desc->static_tree; ++ int elems = desc->stat_desc->elems; ++ int n, m; /* iterate over heap elements */ ++ int max_code = -1; /* largest code with non zero frequency */ ++ int node; /* new node being created */ ++ ++ /* Construct the initial heap, with least frequent element in ++ * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1]. ++ * heap[0] is not used. ++ */ ++ s->heap_len = 0, s->heap_max = HEAP_SIZE; ++ ++ for (n = 0; n < elems; n++) { ++ if (tree[n].Freq != 0) { ++ s->heap[++(s->heap_len)] = max_code = n; ++ s->depth[n] = 0; ++ } else { ++ tree[n].Len = 0; ++ } ++ } ++ ++ /* The pkzip format requires that at least one distance code exists, ++ * and that at least one bit should be sent even if there is only one ++ * possible code. So to avoid special checks later on we force at least ++ * two codes of non zero frequency. ++ */ ++ while (s->heap_len < 2) { ++ node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0); ++ tree[node].Freq = 1; ++ s->depth[node] = 0; ++ s->opt_len--; if (stree) s->static_len -= stree[node].Len; ++ /* node is 0 or 1 so it does not have extra bits */ ++ } ++ desc->max_code = max_code; ++ ++ /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree, ++ * establish sub-heaps of increasing lengths: ++ */ ++ for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n); ++ ++ /* Construct the Huffman tree by repeatedly combining the least two ++ * frequent nodes. ++ */ ++ node = elems; /* next internal node of the tree */ ++ do { ++ pqremove(s, tree, n); /* n = node of least frequency */ ++ m = s->heap[SMALLEST]; /* m = node of next least frequency */ ++ ++ s->heap[--(s->heap_max)] = n; /* keep the nodes sorted by frequency */ ++ s->heap[--(s->heap_max)] = m; ++ ++ /* Create a new node father of n and m */ ++ tree[node].Freq = tree[n].Freq + tree[m].Freq; ++ s->depth[node] = (uch)((s->depth[n] >= s->depth[m] ? ++ s->depth[n] : s->depth[m]) + 1); ++ tree[n].Dad = tree[m].Dad = (ush)node; ++#ifdef DUMP_BL_TREE ++ if (tree == s->bl_tree) { ++ fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)", ++ node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq); ++ } ++#endif ++ /* and insert the new node in the heap */ ++ s->heap[SMALLEST] = node++; ++ pqdownheap(s, tree, SMALLEST); ++ ++ } while (s->heap_len >= 2); ++ ++ s->heap[--(s->heap_max)] = s->heap[SMALLEST]; ++ ++ /* At this point, the fields freq and dad are set. We can now ++ * generate the bit lengths. ++ */ ++ gen_bitlen(s, (tree_desc *)desc); ++ ++ /* The field len is now set, we can generate the bit codes */ ++ gen_codes ((ct_data *)tree, max_code, s->bl_count); ++} ++ ++/* =========================================================================== ++ * Scan a literal or distance tree to determine the frequencies of the codes ++ * in the bit length tree. ++ */ ++local void scan_tree (s, tree, max_code) ++ deflate_state *s; ++ ct_data *tree; /* the tree to be scanned */ ++ int max_code; /* and its largest code of non zero frequency */ ++{ ++ int n; /* iterates over all tree elements */ ++ int prevlen = -1; /* last emitted length */ ++ int curlen; /* length of current code */ ++ int nextlen = tree[0].Len; /* length of next code */ ++ int count = 0; /* repeat count of the current code */ ++ int max_count = 7; /* max repeat count */ ++ int min_count = 4; /* min repeat count */ ++ ++ if (nextlen == 0) max_count = 138, min_count = 3; ++ tree[max_code+1].Len = (ush)0xffff; /* guard */ ++ ++ for (n = 0; n <= max_code; n++) { ++ curlen = nextlen; nextlen = tree[n+1].Len; ++ if (++count < max_count && curlen == nextlen) { ++ continue; ++ } else if (count < min_count) { ++ s->bl_tree[curlen].Freq += count; ++ } else if (curlen != 0) { ++ if (curlen != prevlen) s->bl_tree[curlen].Freq++; ++ s->bl_tree[REP_3_6].Freq++; ++ } else if (count <= 10) { ++ s->bl_tree[REPZ_3_10].Freq++; ++ } else { ++ s->bl_tree[REPZ_11_138].Freq++; ++ } ++ count = 0; prevlen = curlen; ++ if (nextlen == 0) { ++ max_count = 138, min_count = 3; ++ } else if (curlen == nextlen) { ++ max_count = 6, min_count = 3; ++ } else { ++ max_count = 7, min_count = 4; ++ } ++ } ++} ++ ++/* =========================================================================== ++ * Send a literal or distance tree in compressed form, using the codes in ++ * bl_tree. ++ */ ++local void send_tree (s, tree, max_code) ++ deflate_state *s; ++ ct_data *tree; /* the tree to be scanned */ ++ int max_code; /* and its largest code of non zero frequency */ ++{ ++ int n; /* iterates over all tree elements */ ++ int prevlen = -1; /* last emitted length */ ++ int curlen; /* length of current code */ ++ int nextlen = tree[0].Len; /* length of next code */ ++ int count = 0; /* repeat count of the current code */ ++ int max_count = 7; /* max repeat count */ ++ int min_count = 4; /* min repeat count */ ++ ++ /* tree[max_code+1].Len = -1; */ /* guard already set */ ++ if (nextlen == 0) max_count = 138, min_count = 3; ++ ++ for (n = 0; n <= max_code; n++) { ++ curlen = nextlen; nextlen = tree[n+1].Len; ++ if (++count < max_count && curlen == nextlen) { ++ continue; ++ } else if (count < min_count) { ++ do { send_code(s, curlen, s->bl_tree); } while (--count != 0); ++ ++ } else if (curlen != 0) { ++ if (curlen != prevlen) { ++ send_code(s, curlen, s->bl_tree); count--; ++ } ++ Assert(count >= 3 && count <= 6, " 3_6?"); ++ send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2); ++ ++ } else if (count <= 10) { ++ send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3); ++ ++ } else { ++ send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7); ++ } ++ count = 0; prevlen = curlen; ++ if (nextlen == 0) { ++ max_count = 138, min_count = 3; ++ } else if (curlen == nextlen) { ++ max_count = 6, min_count = 3; ++ } else { ++ max_count = 7, min_count = 4; ++ } ++ } ++} ++ ++/* =========================================================================== ++ * Construct the Huffman tree for the bit lengths and return the index in ++ * bl_order of the last bit length code to send. ++ */ ++local int build_bl_tree(s) ++ deflate_state *s; ++{ ++ int max_blindex; /* index of last bit length code of non zero freq */ ++ ++ /* Determine the bit length frequencies for literal and distance trees */ ++ scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code); ++ scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code); ++ ++ /* Build the bit length tree: */ ++ build_tree(s, (tree_desc *)(&(s->bl_desc))); ++ /* opt_len now includes the length of the tree representations, except ++ * the lengths of the bit lengths codes and the 5+5+4 bits for the counts. ++ */ ++ ++ /* Determine the number of bit length codes to send. The pkzip format ++ * requires that at least 4 bit length codes be sent. (appnote.txt says ++ * 3 but the actual value used is 4.) ++ */ ++ for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) { ++ if (s->bl_tree[bl_order[max_blindex]].Len != 0) break; ++ } ++ /* Update opt_len to include the bit length tree and counts */ ++ s->opt_len += 3*((ulg)max_blindex+1) + 5+5+4; ++ Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld", ++ s->opt_len, s->static_len)); ++ ++ return max_blindex; ++} ++ ++/* =========================================================================== ++ * Send the header for a block using dynamic Huffman trees: the counts, the ++ * lengths of the bit length codes, the literal tree and the distance tree. ++ * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4. ++ */ ++local void send_all_trees(s, lcodes, dcodes, blcodes) ++ deflate_state *s; ++ int lcodes, dcodes, blcodes; /* number of codes for each tree */ ++{ ++ int rank; /* index in bl_order */ ++ ++ Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes"); ++ Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES, ++ "too many codes"); ++ Tracev((stderr, "\nbl counts: ")); ++ send_bits(s, lcodes-257, 5); /* not +255 as stated in appnote.txt */ ++ send_bits(s, dcodes-1, 5); ++ send_bits(s, blcodes-4, 4); /* not -3 as stated in appnote.txt */ ++ for (rank = 0; rank < blcodes; rank++) { ++ Tracev((stderr, "\nbl code %2d ", bl_order[rank])); ++ send_bits(s, s->bl_tree[bl_order[rank]].Len, 3); ++ } ++ Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent)); ++ ++ send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); /* literal tree */ ++ Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent)); ++ ++ send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); /* distance tree */ ++ Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent)); ++} ++ ++/* =========================================================================== ++ * Send a stored block ++ */ ++void ZLIB_INTERNAL _tr_stored_block(s, buf, stored_len, last) ++ deflate_state *s; ++ charf *buf; /* input block */ ++ ulg stored_len; /* length of input block */ ++ int last; /* one if this is the last block for a file */ ++{ ++ send_bits(s, (STORED_BLOCK<<1)+last, 3); /* send block type */ ++ bi_windup(s); /* align on byte boundary */ ++ put_short(s, (ush)stored_len); ++ put_short(s, (ush)~stored_len); ++ zmemcpy(s->pending_buf + s->pending, (Bytef *)buf, stored_len); ++ s->pending += stored_len; ++#ifdef ZLIB_DEBUG ++ s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L; ++ s->compressed_len += (stored_len + 4) << 3; ++ s->bits_sent += 2*16; ++ s->bits_sent += stored_len<<3; ++#endif ++} ++ ++/* =========================================================================== ++ * Flush the bits in the bit buffer to pending output (leaves at most 7 bits) ++ */ ++void ZLIB_INTERNAL _tr_flush_bits(s) ++ deflate_state *s; ++{ ++ bi_flush(s); ++} ++ ++/* =========================================================================== ++ * Send one empty static block to give enough lookahead for inflate. ++ * This takes 10 bits, of which 7 may remain in the bit buffer. ++ */ ++void ZLIB_INTERNAL _tr_align(s) ++ deflate_state *s; ++{ ++ send_bits(s, STATIC_TREES<<1, 3); ++ send_code(s, END_BLOCK, static_ltree); ++#ifdef ZLIB_DEBUG ++ s->compressed_len += 10L; /* 3 for block type, 7 for EOB */ ++#endif ++ bi_flush(s); ++} ++ ++/* =========================================================================== ++ * Determine the best encoding for the current block: dynamic trees, static ++ * trees or store, and write out the encoded block. ++ */ ++void ZLIB_INTERNAL _tr_flush_block(s, buf, stored_len, last) ++ deflate_state *s; ++ charf *buf; /* input block, or NULL if too old */ ++ ulg stored_len; /* length of input block */ ++ int last; /* one if this is the last block for a file */ ++{ ++ ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */ ++ int max_blindex = 0; /* index of last bit length code of non zero freq */ ++ ++ /* Build the Huffman trees unless a stored block is forced */ ++ if (s->level > 0) { ++ ++ /* Check if the file is binary or text */ ++ if (s->strm->data_type == Z_UNKNOWN) ++ s->strm->data_type = detect_data_type(s); ++ ++ /* Construct the literal and distance trees */ ++ build_tree(s, (tree_desc *)(&(s->l_desc))); ++ Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len, ++ s->static_len)); ++ ++ build_tree(s, (tree_desc *)(&(s->d_desc))); ++ Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len, ++ s->static_len)); ++ /* At this point, opt_len and static_len are the total bit lengths of ++ * the compressed block data, excluding the tree representations. ++ */ ++ ++ /* Build the bit length tree for the above two trees, and get the index ++ * in bl_order of the last bit length code to send. ++ */ ++ max_blindex = build_bl_tree(s); ++ ++ /* Determine the best encoding. Compute the block lengths in bytes. */ ++ opt_lenb = (s->opt_len+3+7)>>3; ++ static_lenb = (s->static_len+3+7)>>3; ++ ++ Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ", ++ opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len, ++ s->last_lit)); ++ ++ if (static_lenb <= opt_lenb) opt_lenb = static_lenb; ++ ++ } else { ++ Assert(buf != (char*)0, "lost buf"); ++ opt_lenb = static_lenb = stored_len + 5; /* force a stored block */ ++ } ++ ++#ifdef FORCE_STORED ++ if (buf != (char*)0) { /* force stored block */ ++#else ++ if (stored_len+4 <= opt_lenb && buf != (char*)0) { ++ /* 4: two words for the lengths */ ++#endif ++ /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE. ++ * Otherwise we can't have processed more than WSIZE input bytes since ++ * the last block flush, because compression would have been ++ * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to ++ * transform a block into a stored block. ++ */ ++ _tr_stored_block(s, buf, stored_len, last); ++ ++#ifdef FORCE_STATIC ++ } else if (static_lenb >= 0) { /* force static trees */ ++#else ++ } else if (s->strategy == Z_FIXED || static_lenb == opt_lenb) { ++#endif ++ send_bits(s, (STATIC_TREES<<1)+last, 3); ++ compress_block(s, (const ct_data *)static_ltree, ++ (const ct_data *)static_dtree); ++#ifdef ZLIB_DEBUG ++ s->compressed_len += 3 + s->static_len; ++#endif ++ } else { ++ send_bits(s, (DYN_TREES<<1)+last, 3); ++ send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1, ++ max_blindex+1); ++ compress_block(s, (const ct_data *)s->dyn_ltree, ++ (const ct_data *)s->dyn_dtree); ++#ifdef ZLIB_DEBUG ++ s->compressed_len += 3 + s->opt_len; ++#endif ++ } ++ Assert (s->compressed_len == s->bits_sent, "bad compressed size"); ++ /* The above check is made mod 2^32, for files larger than 512 MB ++ * and uLong implemented on 32 bits. ++ */ ++ init_block(s); ++ ++ if (last) { ++ bi_windup(s); ++#ifdef ZLIB_DEBUG ++ s->compressed_len += 7; /* align on byte boundary */ ++#endif ++ } ++ Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3, ++ s->compressed_len-7*last)); ++} ++ ++/* =========================================================================== ++ * Save the match info and tally the frequency counts. Return true if ++ * the current block must be flushed. ++ */ ++int ZLIB_INTERNAL _tr_tally (s, dist, lc) ++ deflate_state *s; ++ unsigned dist; /* distance of matched string */ ++ unsigned lc; /* match length-MIN_MATCH or unmatched char (if dist==0) */ ++{ ++ s->d_buf[s->last_lit] = (ush)dist; ++ s->l_buf[s->last_lit++] = (uch)lc; ++ if (dist == 0) { ++ /* lc is the unmatched char */ ++ s->dyn_ltree[lc].Freq++; ++ } else { ++ s->matches++; ++ /* Here, lc is the match length - MIN_MATCH */ ++ dist--; /* dist = match distance - 1 */ ++ Assert((ush)dist < (ush)MAX_DIST(s) && ++ (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) && ++ (ush)d_code(dist) < (ush)D_CODES, "_tr_tally: bad match"); ++ ++ s->dyn_ltree[_length_code[lc]+LITERALS+1].Freq++; ++ s->dyn_dtree[d_code(dist)].Freq++; ++ } ++ ++#ifdef TRUNCATE_BLOCK ++ /* Try to guess if it is profitable to stop the current block here */ ++ if ((s->last_lit & 0x1fff) == 0 && s->level > 2) { ++ /* Compute an upper bound for the compressed length */ ++ ulg out_length = (ulg)s->last_lit*8L; ++ ulg in_length = (ulg)((long)s->strstart - s->block_start); ++ int dcode; ++ for (dcode = 0; dcode < D_CODES; dcode++) { ++ out_length += (ulg)s->dyn_dtree[dcode].Freq * ++ (5L+extra_dbits[dcode]); ++ } ++ out_length >>= 3; ++ Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ", ++ s->last_lit, in_length, out_length, ++ 100L - out_length*100L/in_length)); ++ if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1; ++ } ++#endif ++ return (s->last_lit == s->lit_bufsize-1); ++ /* We avoid equality with lit_bufsize because of wraparound at 64K ++ * on 16 bit machines and because stored blocks are restricted to ++ * 64K-1 bytes. ++ */ ++} ++ ++/* =========================================================================== ++ * Send the block data compressed using the given Huffman trees ++ */ ++local void compress_block(s, ltree, dtree) ++ deflate_state *s; ++ const ct_data *ltree; /* literal tree */ ++ const ct_data *dtree; /* distance tree */ ++{ ++ unsigned dist; /* distance of matched string */ ++ int lc; /* match length or unmatched char (if dist == 0) */ ++ unsigned lx = 0; /* running index in l_buf */ ++ unsigned code; /* the code to send */ ++ int extra; /* number of extra bits to send */ ++ ++ if (s->last_lit != 0) do { ++ dist = s->d_buf[lx]; ++ lc = s->l_buf[lx++]; ++ if (dist == 0) { ++ send_code(s, lc, ltree); /* send a literal byte */ ++ Tracecv(isgraph(lc), (stderr," '%c' ", lc)); ++ } else { ++ /* Here, lc is the match length - MIN_MATCH */ ++ code = _length_code[lc]; ++ send_code(s, code+LITERALS+1, ltree); /* send the length code */ ++ extra = extra_lbits[code]; ++ if (extra != 0) { ++ lc -= base_length[code]; ++ send_bits(s, lc, extra); /* send the extra length bits */ ++ } ++ dist--; /* dist is now the match distance - 1 */ ++ code = d_code(dist); ++ Assert (code < D_CODES, "bad d_code"); ++ ++ send_code(s, code, dtree); /* send the distance code */ ++ extra = extra_dbits[code]; ++ if (extra != 0) { ++ dist -= (unsigned)base_dist[code]; ++ send_bits(s, dist, extra); /* send the extra distance bits */ ++ } ++ } /* literal or match pair ? */ ++ ++ /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */ ++ Assert((uInt)(s->pending) < s->lit_bufsize + 2*lx, ++ "pendingBuf overflow"); ++ ++ } while (lx < s->last_lit); ++ ++ send_code(s, END_BLOCK, ltree); ++} ++ ++/* =========================================================================== ++ * Check if the data type is TEXT or BINARY, using the following algorithm: ++ * - TEXT if the two conditions below are satisfied: ++ * a) There are no non-portable control characters belonging to the ++ * "black list" (0..6, 14..25, 28..31). ++ * b) There is at least one printable character belonging to the ++ * "white list" (9 {TAB}, 10 {LF}, 13 {CR}, 32..255). ++ * - BINARY otherwise. ++ * - The following partially-portable control characters form a ++ * "gray list" that is ignored in this detection algorithm: ++ * (7 {BEL}, 8 {BS}, 11 {VT}, 12 {FF}, 26 {SUB}, 27 {ESC}). ++ * IN assertion: the fields Freq of dyn_ltree are set. ++ */ ++local int detect_data_type(s) ++ deflate_state *s; ++{ ++ /* black_mask is the bit mask of black-listed bytes ++ * set bits 0..6, 14..25, and 28..31 ++ * 0xf3ffc07f = binary 11110011111111111100000001111111 ++ */ ++ unsigned long black_mask = 0xf3ffc07fUL; ++ int n; ++ ++ /* Check for non-textual ("black-listed") bytes. */ ++ for (n = 0; n <= 31; n++, black_mask >>= 1) ++ if ((black_mask & 1) && (s->dyn_ltree[n].Freq != 0)) ++ return Z_BINARY; ++ ++ /* Check for textual ("white-listed") bytes. */ ++ if (s->dyn_ltree[9].Freq != 0 || s->dyn_ltree[10].Freq != 0 ++ || s->dyn_ltree[13].Freq != 0) ++ return Z_TEXT; ++ for (n = 32; n < LITERALS; n++) ++ if (s->dyn_ltree[n].Freq != 0) ++ return Z_TEXT; ++ ++ /* There are no "black-listed" or "white-listed" bytes: ++ * this stream either is empty or has tolerated ("gray-listed") bytes only. ++ */ ++ return Z_BINARY; ++} ++ ++/* =========================================================================== ++ * Reverse the first len bits of a code, using straightforward code (a faster ++ * method would use a table) ++ * IN assertion: 1 <= len <= 15 ++ */ ++local unsigned bi_reverse(code, len) ++ unsigned code; /* the value to invert */ ++ int len; /* its bit length */ ++{ ++ register unsigned res = 0; ++ do { ++ res |= code & 1; ++ code >>= 1, res <<= 1; ++ } while (--len > 0); ++ return res >> 1; ++} ++ ++/* =========================================================================== ++ * Flush the bit buffer, keeping at most 7 bits in it. ++ */ ++local void bi_flush(s) ++ deflate_state *s; ++{ ++ if (s->bi_valid == 16) { ++ put_short(s, s->bi_buf); ++ s->bi_buf = 0; ++ s->bi_valid = 0; ++ } else if (s->bi_valid >= 8) { ++ put_byte(s, (Byte)s->bi_buf); ++ s->bi_buf >>= 8; ++ s->bi_valid -= 8; ++ } ++} ++ ++/* =========================================================================== ++ * Flush the bit buffer and align the output on a byte boundary ++ */ ++local void bi_windup(s) ++ deflate_state *s; ++{ ++ if (s->bi_valid > 8) { ++ put_short(s, s->bi_buf); ++ } else if (s->bi_valid > 0) { ++ put_byte(s, (Byte)s->bi_buf); ++ } ++ s->bi_buf = 0; ++ s->bi_valid = 0; ++#ifdef ZLIB_DEBUG ++ s->bits_sent = (s->bits_sent+7) & ~7; ++#endif ++} +diff --git a/lib/libzlib/trees.h b/lib/libzlib/trees.h +new file mode 100644 +index 000000000..445137801 +--- /dev/null ++++ b/lib/libzlib/trees.h +@@ -0,0 +1,129 @@ ++/* SPDX-License-Identifier: Zlib */ ++/* header created automatically with -DGEN_TREES_H */ ++ ++local const ct_data static_ltree[L_CODES+2] = { ++{{ 12},{ 8}}, {{140},{ 8}}, {{ 76},{ 8}}, {{204},{ 8}}, {{ 44},{ 8}}, ++{{172},{ 8}}, {{108},{ 8}}, {{236},{ 8}}, {{ 28},{ 8}}, {{156},{ 8}}, ++{{ 92},{ 8}}, {{220},{ 8}}, {{ 60},{ 8}}, {{188},{ 8}}, {{124},{ 8}}, ++{{252},{ 8}}, {{ 2},{ 8}}, {{130},{ 8}}, {{ 66},{ 8}}, {{194},{ 8}}, ++{{ 34},{ 8}}, {{162},{ 8}}, {{ 98},{ 8}}, {{226},{ 8}}, {{ 18},{ 8}}, ++{{146},{ 8}}, {{ 82},{ 8}}, {{210},{ 8}}, {{ 50},{ 8}}, {{178},{ 8}}, ++{{114},{ 8}}, {{242},{ 8}}, {{ 10},{ 8}}, {{138},{ 8}}, {{ 74},{ 8}}, ++{{202},{ 8}}, {{ 42},{ 8}}, {{170},{ 8}}, {{106},{ 8}}, {{234},{ 8}}, ++{{ 26},{ 8}}, {{154},{ 8}}, {{ 90},{ 8}}, {{218},{ 8}}, {{ 58},{ 8}}, ++{{186},{ 8}}, {{122},{ 8}}, {{250},{ 8}}, {{ 6},{ 8}}, {{134},{ 8}}, ++{{ 70},{ 8}}, {{198},{ 8}}, {{ 38},{ 8}}, {{166},{ 8}}, {{102},{ 8}}, ++{{230},{ 8}}, {{ 22},{ 8}}, {{150},{ 8}}, {{ 86},{ 8}}, {{214},{ 8}}, ++{{ 54},{ 8}}, {{182},{ 8}}, {{118},{ 8}}, {{246},{ 8}}, {{ 14},{ 8}}, ++{{142},{ 8}}, {{ 78},{ 8}}, {{206},{ 8}}, {{ 46},{ 8}}, {{174},{ 8}}, ++{{110},{ 8}}, {{238},{ 8}}, {{ 30},{ 8}}, {{158},{ 8}}, {{ 94},{ 8}}, ++{{222},{ 8}}, {{ 62},{ 8}}, {{190},{ 8}}, {{126},{ 8}}, {{254},{ 8}}, ++{{ 1},{ 8}}, {{129},{ 8}}, {{ 65},{ 8}}, {{193},{ 8}}, {{ 33},{ 8}}, ++{{161},{ 8}}, {{ 97},{ 8}}, {{225},{ 8}}, {{ 17},{ 8}}, {{145},{ 8}}, ++{{ 81},{ 8}}, {{209},{ 8}}, {{ 49},{ 8}}, {{177},{ 8}}, {{113},{ 8}}, ++{{241},{ 8}}, {{ 9},{ 8}}, {{137},{ 8}}, {{ 73},{ 8}}, {{201},{ 8}}, ++{{ 41},{ 8}}, {{169},{ 8}}, {{105},{ 8}}, {{233},{ 8}}, {{ 25},{ 8}}, ++{{153},{ 8}}, {{ 89},{ 8}}, {{217},{ 8}}, {{ 57},{ 8}}, {{185},{ 8}}, ++{{121},{ 8}}, {{249},{ 8}}, {{ 5},{ 8}}, {{133},{ 8}}, {{ 69},{ 8}}, ++{{197},{ 8}}, {{ 37},{ 8}}, {{165},{ 8}}, {{101},{ 8}}, {{229},{ 8}}, ++{{ 21},{ 8}}, {{149},{ 8}}, {{ 85},{ 8}}, {{213},{ 8}}, {{ 53},{ 8}}, ++{{181},{ 8}}, {{117},{ 8}}, {{245},{ 8}}, {{ 13},{ 8}}, {{141},{ 8}}, ++{{ 77},{ 8}}, {{205},{ 8}}, {{ 45},{ 8}}, {{173},{ 8}}, {{109},{ 8}}, ++{{237},{ 8}}, {{ 29},{ 8}}, {{157},{ 8}}, {{ 93},{ 8}}, {{221},{ 8}}, ++{{ 61},{ 8}}, {{189},{ 8}}, {{125},{ 8}}, {{253},{ 8}}, {{ 19},{ 9}}, ++{{275},{ 9}}, {{147},{ 9}}, {{403},{ 9}}, {{ 83},{ 9}}, {{339},{ 9}}, ++{{211},{ 9}}, {{467},{ 9}}, {{ 51},{ 9}}, {{307},{ 9}}, {{179},{ 9}}, ++{{435},{ 9}}, {{115},{ 9}}, {{371},{ 9}}, {{243},{ 9}}, {{499},{ 9}}, ++{{ 11},{ 9}}, {{267},{ 9}}, {{139},{ 9}}, {{395},{ 9}}, {{ 75},{ 9}}, ++{{331},{ 9}}, {{203},{ 9}}, {{459},{ 9}}, {{ 43},{ 9}}, {{299},{ 9}}, ++{{171},{ 9}}, {{427},{ 9}}, {{107},{ 9}}, {{363},{ 9}}, {{235},{ 9}}, ++{{491},{ 9}}, {{ 27},{ 9}}, {{283},{ 9}}, {{155},{ 9}}, {{411},{ 9}}, ++{{ 91},{ 9}}, {{347},{ 9}}, {{219},{ 9}}, {{475},{ 9}}, {{ 59},{ 9}}, ++{{315},{ 9}}, {{187},{ 9}}, {{443},{ 9}}, {{123},{ 9}}, {{379},{ 9}}, ++{{251},{ 9}}, {{507},{ 9}}, {{ 7},{ 9}}, {{263},{ 9}}, {{135},{ 9}}, ++{{391},{ 9}}, {{ 71},{ 9}}, {{327},{ 9}}, {{199},{ 9}}, {{455},{ 9}}, ++{{ 39},{ 9}}, {{295},{ 9}}, {{167},{ 9}}, {{423},{ 9}}, {{103},{ 9}}, ++{{359},{ 9}}, {{231},{ 9}}, {{487},{ 9}}, {{ 23},{ 9}}, {{279},{ 9}}, ++{{151},{ 9}}, {{407},{ 9}}, {{ 87},{ 9}}, {{343},{ 9}}, {{215},{ 9}}, ++{{471},{ 9}}, {{ 55},{ 9}}, {{311},{ 9}}, {{183},{ 9}}, {{439},{ 9}}, ++{{119},{ 9}}, {{375},{ 9}}, {{247},{ 9}}, {{503},{ 9}}, {{ 15},{ 9}}, ++{{271},{ 9}}, {{143},{ 9}}, {{399},{ 9}}, {{ 79},{ 9}}, {{335},{ 9}}, ++{{207},{ 9}}, {{463},{ 9}}, {{ 47},{ 9}}, {{303},{ 9}}, {{175},{ 9}}, ++{{431},{ 9}}, {{111},{ 9}}, {{367},{ 9}}, {{239},{ 9}}, {{495},{ 9}}, ++{{ 31},{ 9}}, {{287},{ 9}}, {{159},{ 9}}, {{415},{ 9}}, {{ 95},{ 9}}, ++{{351},{ 9}}, {{223},{ 9}}, {{479},{ 9}}, {{ 63},{ 9}}, {{319},{ 9}}, ++{{191},{ 9}}, {{447},{ 9}}, {{127},{ 9}}, {{383},{ 9}}, {{255},{ 9}}, ++{{511},{ 9}}, {{ 0},{ 7}}, {{ 64},{ 7}}, {{ 32},{ 7}}, {{ 96},{ 7}}, ++{{ 16},{ 7}}, {{ 80},{ 7}}, {{ 48},{ 7}}, {{112},{ 7}}, {{ 8},{ 7}}, ++{{ 72},{ 7}}, {{ 40},{ 7}}, {{104},{ 7}}, {{ 24},{ 7}}, {{ 88},{ 7}}, ++{{ 56},{ 7}}, {{120},{ 7}}, {{ 4},{ 7}}, {{ 68},{ 7}}, {{ 36},{ 7}}, ++{{100},{ 7}}, {{ 20},{ 7}}, {{ 84},{ 7}}, {{ 52},{ 7}}, {{116},{ 7}}, ++{{ 3},{ 8}}, {{131},{ 8}}, {{ 67},{ 8}}, {{195},{ 8}}, {{ 35},{ 8}}, ++{{163},{ 8}}, {{ 99},{ 8}}, {{227},{ 8}} ++}; ++ ++local const ct_data static_dtree[D_CODES] = { ++{{ 0},{ 5}}, {{16},{ 5}}, {{ 8},{ 5}}, {{24},{ 5}}, {{ 4},{ 5}}, ++{{20},{ 5}}, {{12},{ 5}}, {{28},{ 5}}, {{ 2},{ 5}}, {{18},{ 5}}, ++{{10},{ 5}}, {{26},{ 5}}, {{ 6},{ 5}}, {{22},{ 5}}, {{14},{ 5}}, ++{{30},{ 5}}, {{ 1},{ 5}}, {{17},{ 5}}, {{ 9},{ 5}}, {{25},{ 5}}, ++{{ 5},{ 5}}, {{21},{ 5}}, {{13},{ 5}}, {{29},{ 5}}, {{ 3},{ 5}}, ++{{19},{ 5}}, {{11},{ 5}}, {{27},{ 5}}, {{ 7},{ 5}}, {{23},{ 5}} ++}; ++ ++const uch ZLIB_INTERNAL _dist_code[DIST_CODE_LEN] = { ++ 0, 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, ++ 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, ++10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, ++11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, ++12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, ++13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, ++13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, ++14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, ++14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, ++14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, ++15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, ++15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, ++15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 0, 0, 16, 17, ++18, 18, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 22, ++23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, ++24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, ++26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, ++26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, ++27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, ++27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, ++28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, ++28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, ++28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, ++29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, ++29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, ++29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29 ++}; ++ ++const uch ZLIB_INTERNAL _length_code[MAX_MATCH-MIN_MATCH+1]= { ++ 0, 1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 12, 12, ++13, 13, 13, 13, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16, 16, 16, ++17, 17, 17, 17, 17, 17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 19, 19, 19, 19, ++19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, ++21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, ++22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23, 23, 23, 23, 23, 23, ++23, 23, 23, 23, 23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, ++24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, ++25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, ++25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 26, 26, 26, 26, 26, 26, 26, 26, ++26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, ++26, 26, 26, 26, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, ++27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 28 ++}; ++ ++local const int base_length[LENGTH_CODES] = { ++0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 20, 24, 28, 32, 40, 48, 56, ++64, 80, 96, 112, 128, 160, 192, 224, 0 ++}; ++ ++local const int base_dist[D_CODES] = { ++ 0, 1, 2, 3, 4, 6, 8, 12, 16, 24, ++ 32, 48, 64, 96, 128, 192, 256, 384, 512, 768, ++ 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576 ++}; ++ +diff --git a/core/lib/zlib/zconf.h b/lib/libzlib/zconf.h +similarity index 100% +rename from core/lib/zlib/zconf.h +rename to lib/libzlib/zconf.h +diff --git a/core/lib/zlib/zlib.h b/lib/libzlib/zlib.h +similarity index 100% +rename from core/lib/zlib/zlib.h +rename to lib/libzlib/zlib.h +diff --git a/core/lib/zlib/zutil.c b/lib/libzlib/zutil.c +similarity index 100% +rename from core/lib/zlib/zutil.c +rename to lib/libzlib/zutil.c +diff --git a/core/lib/zlib/zutil.h b/lib/libzlib/zutil.h +similarity index 100% +rename from core/lib/zlib/zutil.h +rename to lib/libzlib/zutil.h +diff --git a/mk/config.mk b/mk/config.mk +index 1b139839b..712829355 100644 +--- a/mk/config.mk ++++ b/mk/config.mk +@@ -130,7 +130,7 @@ CFG_OPTEE_REVISION_MAJOR ?= 3 + CFG_OPTEE_REVISION_MINOR ?= 16 + + # Trusted OS implementation version +-TEE_IMPL_VERSION ?= $(shell git describe --always --dirty=-dev 2>/dev/null || \ ++TEE_IMPL_VERSION ?= $(shell git describe --always --tags --dirty=-dev 2>/dev/null || \ + echo Unknown_$(CFG_OPTEE_REVISION_MAJOR).$(CFG_OPTEE_REVISION_MINOR)) + ifeq ($(CFG_OS_REV_REPORTS_GIT_SHA1),y) + TEE_IMPL_GIT_SHA1 := 0x$(shell git rev-parse --short=8 HEAD 2>/dev/null || echo 0) +@@ -667,17 +667,31 @@ CFG_CORE_TPM_EVENT_LOG ?= n + # Refer to the supported SCMI features embedded upon CFG_SCMI_MSG_* + # CFG_SCMI_MSG_CLOCK embeds SCMI clock protocol support. + # CFG_SCMI_MSG_RESET_DOMAIN embeds SCMI reset domain protocol support. ++# CFG_SCMI_MSG_REGULATOR_CONSUMER uses DT to list regulators exposed thru SCMI + # CFG_SCMI_MSG_SMT embeds SMT based message buffer of communication channel + # CFG_SCMI_MSG_VOLTAGE_DOMAIN embeds SCMI voltage domain protocol support. ++# CFG_SCMI_MSG_PERF_DOMAIN embeds SCMI performance domain management protocol + CFG_SCMI_MSG_DRIVERS ?= n + CFG_SCMI_MSG_CLOCK ?= n + CFG_SCMI_MSG_RESET_DOMAIN ?= n + CFG_SCMI_MSG_SMT ?= n ++CFG_SCMI_MSG_REGULATOR_CONSUMER ?= n + CFG_SCMI_MSG_VOLTAGE_DOMAIN ?= n ++CFG_SCMI_MSG_PERF_DOMAIN ?=n + + # Enable SCMI PTA interface for REE SCMI agents + CFG_SCMI_PTA ?= n + ++# Enable SCMI server (SCP-firmware-scmi library) ++CFG_SCMI_SERVER ?= n ++ifeq ($(CFG_SCMI_SERVER),y) ++$(call force,CFG_SCMI_PTA,y,Mandated by CFG_SCMI_SERVER) ++$(call force,CFG_CORE_OCALL,y,Mandated by CFG_SCMI_SERVER) ++endif ++ ++# Enable Trusted User Interface ++CFG_WITH_TUI ?= n ++ + ifneq ($(CFG_STMM_PATH),) + $(call force,CFG_WITH_STMM_SP,y) + else +@@ -746,6 +760,13 @@ CFG_DRIVERS_RSTCTRL ?= n + # configuration. + CFG_WARN_INSECURE ?= y + ++# CFG_CORE_OCALL=y enables support for OCALLs, allowing core to return ++# from an session or command invocation with an Ocall RPC context. ++CFG_CORE_OCALL ?= n ++ifeq ($(CFG_CORE_SEL1_SPMC)-$(CFG_CORE_OCALL),y-y) ++$(error "SPMC at SEL-1 does not comply with CFG_CORE_OCALL=y") ++endif ++ + # Enables warnings for declarations mixed with statements + CFG_WARN_DECL_AFTER_STATEMENT ?= y + +diff --git a/mk/gcc.mk b/mk/gcc.mk +index adc77a24f..81bfa78ad 100644 +--- a/mk/gcc.mk ++++ b/mk/gcc.mk +@@ -13,11 +13,11 @@ nostdinc$(sm) := -nostdinc -isystem $(shell $(CC$(sm)) \ + -print-file-name=include 2> /dev/null) + + # Get location of libgcc from gcc +-libgcc$(sm) := $(shell $(CC$(sm)) $(CFLAGS$(arch-bits-$(sm))) \ ++libgcc$(sm) := $(shell $(CC$(sm)) $(LIBGCC_LOCATE_CFLAGS) $(CFLAGS$(arch-bits-$(sm))) \ + -print-libgcc-file-name 2> /dev/null) +-libstdc++$(sm) := $(shell $(CXX$(sm)) $(CXXFLAGS$(arch-bits-$(sm))) $(comp-cxxflags$(sm)) \ ++libstdc++$(sm) := $(shell $(CXX$(sm)) $(LIBGCC_LOCATE_CFLAGS) $(CXXFLAGS$(arch-bits-$(sm))) $(comp-cxxflags$(sm)) \ + -print-file-name=libstdc++.a 2> /dev/null) +-libgcc_eh$(sm) := $(shell $(CXX$(sm)) $(CXXFLAGS$(arch-bits-$(sm))) $(comp-cxxflags$(sm)) \ ++libgcc_eh$(sm) := $(shell $(CXX$(sm)) $(LIBGCC_LOCATE_CFLAGS) $(CXXFLAGS$(arch-bits-$(sm))) $(comp-cxxflags$(sm)) \ + -print-file-name=libgcc_eh.a 2> /dev/null) + + # Define these to something to discover accidental use +diff --git a/scripts/checkpatch_inc.sh b/scripts/checkpatch_inc.sh +index 67218f7c3..4be4a9255 100644 +--- a/scripts/checkpatch_inc.sh ++++ b/scripts/checkpatch_inc.sh +@@ -5,8 +5,8 @@ CHECKPATCH_OPT="${CHECKPATCH_OPT:-}" + # checkpatch.pl will ignore the following paths + CHECKPATCH_IGNORE=$(echo \ + core/include/gen-asm-defines.h \ +- core/lib/lib{fdt,tomcrypt} core/lib/zlib \ +- lib/libutils lib/libmbedtls \ ++ core/lib/lib{fdt,tomcrypt} core/libzlib \ ++ lib/libutils lib/libmbedtls lib/libpng \ + lib/libutee/include/elf.h \ + lib/libutee/include/elf_common.h \ + core/arch/arm/include/arm{32,64}.h \ +diff --git a/scripts/render_font.py b/scripts/render_font.py +new file mode 100755 +index 000000000..479b57c37 +--- /dev/null ++++ b/scripts/render_font.py +@@ -0,0 +1,135 @@ ++#!/usr/bin/env python3 ++# SPDX-License-Identifier: BSD-2-Clause ++# ++# Copyright (c) 2016-2021, Linaro Limited ++ ++from PIL import ImageDraw ++from PIL import ImageFont ++from PIL import Image ++import sys ++ ++def get_args(): ++ from argparse import ArgumentParser ++ ++ parser = ArgumentParser() ++ parser.add_argument('--font_file', required=True, ++ help='Name of font file') ++ parser.add_argument('--font_size', type=int, default=40, ++ help='Size of font') ++ parser.add_argument('--font_name', required=True, ++ help='Name of font in program') ++ parser.add_argument('--out_dir', required=True, ++ help='Out directory') ++ parser.add_argument('--verbose', default=False, ++ help='Print informational messages') ++ return parser.parse_args() ++ ++def c_hex(bstr): ++ s = [] ++ for n, x in enumerate(bstr): ++ if n % 8 == 0: ++ if n == 0: ++ s.append("\t") ++ else: ++ s.append("\n\t") ++ else: ++ s.append(" ") ++ ++ s.append("0x{:02x},".format(x)) ++ ++ return "".join(s) ++ ++header_template = """/* ++ * This file is auto generated with ++ * {cmd_line} ++ * do not edit. ++ */ ++""" ++ ++h_file_template = """#ifndef __{FONT_NAME}_H ++#define __{FONT_NAME}_H ++ ++#include "font.h" ++ ++extern const struct font font_{font_name}; ++ ++#endif /*__{FONT_NAME}_H*/ ++""" ++ ++letter_template = """/* {letter} */ ++static const unsigned char letter_{id}[] = {{ ++{c_hex} ++}}; ++ ++""" ++ ++font_letter_template = """\t{{ letter_{id}, sizeof(letter_{id}), {width}}}, ++""" ++ ++font_template = """const struct font font_{name} = {{ ++ .first = 0x{first:02x}, ++ .last = 0x{last:02x}, ++ .letters = letters, ++ .height = {height}, ++ .max_width = {max_width}, ++}}; ++""" ++ ++def main(): ++ args = get_args() ++ ++ font_name = args.font_name ++ out_dir = args.out_dir ++ ++ range_first = 0x20 ++ range_last = 0x7d ++ c_letters = {} ++ ++ letters = range(range_first, range_last + 1) ++ ++ font = ImageFont.truetype(args.font_file, args.font_size) ++ ascent, descent = font.getmetrics() ++ text_height = font.font.height + 2 ++ ++ for x in letters: ++ letter= chr(x) ++ (width, height), (offset_x, offset_y) = font.font.getsize(letter) ++ text_width = width + 2 + offset_x; ++ letter_img = Image.new("1", (text_width, text_height), 0) ++ draw = ImageDraw.Draw(letter_img) ++ draw.fontmode="1" ++ draw.text((0, 0), letter, font=font, fill=1) ++ c_letters[x] = {'img' : letter_img.tobytes(), ++ 'width' : text_width } ++ ++ if args.verbose: ++ print("Writing " + out_dir + "/" + font_name + ".c") ++ ++ with open(out_dir + "/" + font_name + ".c", 'w+') as f: ++ f.write(header_template.format(cmd_line=' '.join(sys.argv))) ++ f.write('#include "font.h"\n\n') ++ for x in letters: ++ f.write(letter_template.format(letter=chr(x), id="{:02x}".format(x), ++ c_hex=c_hex(c_letters[x]['img']))) ++ ++ f.write("static const struct font_letter letters[] = {\n") ++ for x in letters: ++ f.write(font_letter_template.format(id="{:02x}".format(x), ++ width=c_letters[x]['width'])) ++ f.write("};\n\n") ++ ++ f.write(font_template.format(name=args.font_name, ++ first=letters[0], ++ last=letters[-1], ++ height=text_height, ++ max_width=max(l['width'] for l in c_letters.values()))) ++ ++ if args.verbose: ++ print("Writing " + out_dir + "/" + font_name + ".h") ++ ++ with open(out_dir + "/" + font_name + ".h", 'w+') as f: ++ f.write(header_template.format(cmd_line=' '.join(sys.argv))) ++ f.write(h_file_template.format(font_name=font_name, FONT_NAME=font_name.upper())) ++ ++if __name__ == "__main__": ++ main() +diff --git a/scripts/sign_rproc_fw.py b/scripts/sign_rproc_fw.py +new file mode 100755 +index 000000000..4248ed280 +--- /dev/null ++++ b/scripts/sign_rproc_fw.py +@@ -0,0 +1,416 @@ ++#!/usr/bin/env python3 ++# SPDX-License-Identifier: BSD-2-Clause ++# ++# Copyright (C) 2020, STMicroelectronics - All Rights Reserved ++# ++ ++try: ++ from elftools.elf.elffile import ELFFile ++ from elftools.elf.sections import SymbolTableSection ++ from elftools.elf.enums import ENUM_P_TYPE_BASE ++ from elftools.elf.enums import * ++except ImportError: ++ print(""" ++*** ++ERROR: pyelftools python module is not installed or version < 0.25. ++*** ++""") ++ raise ++ ++from Cryptodome.Hash import SHA256 ++from Cryptodome.Signature import pkcs1_15 ++from Cryptodome.PublicKey import RSA ++from Cryptodome.Signature import DSS ++from Cryptodome.PublicKey import ECC ++import os ++import sys ++import struct ++import logging ++import binascii ++ ++ ++logging.basicConfig(stream=sys.stderr, level=logging.INFO) ++ ++ENUM_HASH_TYPE = dict( ++ SHA256=1, ++) ++ ++ENUM_SIGNATURE_TYPE = dict( ++ RSA=1, ++ ECC=2, ++) ++ ++ENUM_BINARY_TYPE = dict( ++ ELF=1, ++) ++ ++ ++def dump_buffer(buf, step=16, name="", logger=logging.info, indent=""): ++ logger("%s%s:" % (indent, name)) ++ for i in range(0, len(buf), step): ++ logger("%s " % (indent) + " ". ++ join(["%02X" % c for c in buf[i:i+step]])) ++ logger("\n") ++ ++ ++class RSA_Signature(object): ++ ++ def __init__(self, key): ++ self._hasher = SHA256.new() ++ self.signer = pkcs1_15.new(key) ++ ++ def hash_compute(self, segment): ++ self._hasher.update(segment) ++ ++ def sign(self): ++ return self.signer.sign(self._hasher) ++ ++ ++class ECC_Signature(object): ++ ++ def __init__(self, key): ++ self._hasher = SHA256.new() ++ self.signer = DSS.new(key, 'fips-186-3') ++ ++ def hash_compute(self, segment): ++ self._hasher.update(segment) ++ ++ def sign(self): ++ return self.signer.sign(self._hasher) ++ ++ ++Signature = { ++ 1: RSA_Signature, ++ 2: ECC_Signature, ++} ++ ++ ++class SegmentHashStruct: ++ pass ++ ++ ++class SegmentHash(object): ++ ''' ++ Hash table based on Elf program segments ++ ''' ++ def __init__(self, img): ++ self._num_segments = img.num_segments() ++ self._pack_fmt = '<%dL' % 8 ++ self.img = img ++ self.hashProgTable = bytes() ++ self._offset = 0 ++ ++ def get_table(self): ++ ''' ++ Create a segment hash table containing for each segment: ++ - the segments header ++ - a hash of the segment ++ ''' ++ h = SHA256.new() ++ seg = SegmentHashStruct() ++ self.size = (h.digest_size + 32) * self._num_segments ++ logging.debug("hash section size %d" % self.size) ++ del h ++ self.buf = bytearray(self.size) ++ self._bufview_ = memoryview(self.buf) ++ ++ for i in range(self._num_segments): ++ h = SHA256.new() ++ segment = self.img.get_segment(i) ++ seg.header = self.img.get_segment(i).header ++ logging.debug("compute hash for segment offset %s" % seg.header) ++ h.update(segment.data()) ++ seg.hash = h.digest() ++ logging.debug("hash computed: %s" % seg.hash) ++ del h ++ struct.pack_into('.sig') ++ ++ parsed = parser.parse_args() ++ ++ # Set defaults for optional arguments. ++ ++ if parsed.outf is None: ++ parsed.outf = str(parsed.inf)+'.sig' ++ ++ return parsed ++ ++ ++def rsa_key(keyf): ++ return RSA.importKey(open(keyf).read()) ++ ++ ++def ecc_key(keyf): ++ return ECC.import_key(open(keyf).read()) ++ ++ ++key_type = { ++ 1: rsa_key, ++ 2: ecc_key, ++} ++ ++ ++def rsa_sig_size(key): ++ return key.size_in_bytes() ++ ++ ++def ecc_sig_size(key): ++ # to be improve... ++ # DSA size is N/4 so 64 for DSA (L,N) = (2048, 256) ++ return 64 ++ ++ ++sig_size_type = { ++ 1: rsa_sig_size, ++ 2: ecc_sig_size, ++} ++ ++ ++def main(): ++ from Cryptodome.Signature import pss ++ from Cryptodome.Hash import SHA256 ++ from Cryptodome.PublicKey import RSA ++ import base64 ++ import logging ++ import struct ++ ++ logging.basicConfig() ++ logger = logging.getLogger(os.path.basename(__file__)) ++ ++ args = get_args(logger) ++ ++ # Initialise the header */ ++ s_header = ImageHeader() ++ ++ get_key = key_type.get(ENUM_SIGNATURE_TYPE[args.key_type], ++ lambda: "Invalid sign type") ++ key = get_key(args.keyf) ++ ++ if not key.has_private(): ++ logger.error('Provided key cannot be used for signing, ' + ++ 'please use offline-signing mode.') ++ sys.exit(1) ++ ++ # Firmware image ++ input_file = open(args.inf, 'rb') ++ img = ELFFile(input_file) ++ ++ # need to reopen the file to get the raw data ++ with open(args.inf, 'rb') as f: ++ bin_img = f.read() ++ img_size = len(bin_img) ++ logging.debug("image size %d" % img_size) ++ s_header.img_length = img_size ++ s_header.img_type = ENUM_BINARY_TYPE['ELF'] ++ ++ # Hash table chunk ++ h = SHA256.new() ++ ++ # Compute the hash table ++ hash_table = SegmentHash(img) ++ hash = hash_table.get_table() ++ ++ s_header.hash_offset = s_header.size ++ s_header.hash_length = hash_table.size ++ s_header.hash_type = ENUM_HASH_TYPE['SHA256'] ++ # Get padding to align on 64 bytes ++ hash_align = s_header.hash_length % 8 ++ ++ # Key information chunk ++ if args.key_infof: ++ with open(args.key_infof, 'rb') as f: ++ key_info = f.read() ++ s_header.key_length = sys.getsizeof(key_info) ++ s_header.key_offset = s_header.hash_offset + s_header.hash_length + \ ++ hash_align ++ # Get padding to align on 64 bytes ++ key_info_align = s_header.key_length % 8 ++ else: ++ key_info_align = 0 ++ ++ # Signature chunk ++ s_header.sign_type = ENUM_SIGNATURE_TYPE[args.key_type] ++ ++ sign_size = sig_size_type.get(ENUM_SIGNATURE_TYPE[args.key_type], ++ lambda: "Invalid sign type")(key) ++ s_header.sign_length = sign_size ++ ++ if args.key_infof: ++ s_header.sign_offset = s_header.key_offset + s_header.key_length + \ ++ key_info_align ++ else: ++ s_header.sign_offset = s_header.hash_offset + s_header.hash_length + \ ++ hash_align ++ ++ s_header.img_offset = s_header.sign_offset + sign_size ++ ++ s_header.length = s_header.size + s_header.hash_length + hash_align + \ ++ s_header.key_length + key_info_align + s_header.sign_length ++ ++ header = s_header.get_packed() ++ ++ # Generate signature ++ signer = Signature.get(ENUM_SIGNATURE_TYPE[args.key_type])(key) ++ ++ signer.hash_compute(header) ++ signer.hash_compute(bytes(hash)) ++ if args.key_infof: ++ signer.hash_compute(key_info) ++ ++ signature = signer.sign() ++ if len(signature) != sign_size: ++ raise Exception(("Actual signature length is not equal to ", ++ "the computed one: {} != {}". ++ format(len(signature), sign_size))) ++ ++ s_header.dump() ++ ++ with open(args.outf, 'wb') as f: ++ f.write(header) ++ f.write(hash) ++ if hash_align: ++ f.write(bytearray(hash_align)) ++ if args.key_infof: ++ if key_info_align: ++ f.write(key_info) ++ f.write(bytearray(key_info_align)) ++ f.write(signature) ++ f.write(bytearray(sign_size - s_header.sign_length)) ++ f.write(bin_img) ++ ++ ++if __name__ == "__main__": ++ main() +diff --git a/ta/mk/build-user-ta.mk b/ta/mk/build-user-ta.mk +index 2d0b61c7f..99690f095 100644 +--- a/ta/mk/build-user-ta.mk ++++ b/ta/mk/build-user-ta.mk +@@ -46,6 +46,9 @@ libnames = utils utee + ifeq ($(CFG_TA_MBEDTLS),y) + libnames += mbedtls + endif ++ifeq ($(CFG_WITH_TUI),y) ++libnames += zlib png ++endif + libdeps = $(addsuffix .a, $(addprefix $(libdirs)/lib, $(libnames))) + + subdirs = $(patsubst %/,%,$(dir $(ta-mk-file))) +diff --git a/ta/mk/ta_dev_kit.mk b/ta/mk/ta_dev_kit.mk +index 1fb0e4188..7a62a191b 100644 +--- a/ta/mk/ta_dev_kit.mk ++++ b/ta/mk/ta_dev_kit.mk +@@ -86,6 +86,13 @@ endif + libnames += dl + libdeps += $(ta-dev-kit-dir$(sm))/lib/libdl.a + ++ifeq ($(CFG_WITH_TUI),y) ++libnames += zlib ++libdeps += $(ta-dev-kit-dir$(sm))/lib/libzlib.a ++libnames += png ++libdeps += $(ta-dev-kit-dir$(sm))/lib/libpng.a ++endif ++ + # libutils provides __getauxval symbol which is needed by libgcc 10.x. We can't + # link libutils after libgcc, because libgcc will replace some symbols provided + # by libutils, which will cause further linking issues. +diff --git a/ta/remoteproc/Makefile b/ta/remoteproc/Makefile +new file mode 100644 +index 000000000..0d1a5010c +--- /dev/null ++++ b/ta/remoteproc/Makefile +@@ -0,0 +1,18 @@ ++# The UUID for the Trusted Application ++BINARY=80a4c275-0a47-4905-8285-1486a9771a08 ++ ++ifdef TA_CROSS_COMPILE ++CROSS_COMPILE ?= $(TA_CROSS_COMPILE) ++endif ++export CROSS_COMPILE ++ ++CFG_TEE_TA_LOG_LEVEL ?= 2 ++CPPFLAGS += -DCFG_TEE_TA_LOG_LEVEL=$(CFG_TEE_TA_LOG_LEVEL) ++ ++-include $(TA_DEV_KIT_DIR)/mk/ta_dev_kit.mk ++ ++ifeq ($(wildcard $(TA_DEV_KIT_DIR)/mk/ta_dev_kit.mk), ) ++clean: ++ @echo 'Note: $$(TA_DEV_KIT_DIR)/mk/ta_dev_kit.mk not found, cannot clean TA' ++ @echo 'Note: TA_DEV_KIT_DIR=$(TA_DEV_KIT_DIR)' ++endif +diff --git a/ta/remoteproc/elf_parser.c b/ta/remoteproc/elf_parser.c +new file mode 100644 +index 000000000..82d2672c3 +--- /dev/null ++++ b/ta/remoteproc/elf_parser.c +@@ -0,0 +1,186 @@ ++ // SPDX-License-Identifier: BSD-2-Clause ++ /* ++ * Copyright (C) 2020-2021, STMicroelectronics - All Rights Reserved ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++ ++static bool va_in_fwm_image_range(void *va, uint8_t *fw, size_t fw_size) ++{ ++ uint8_t *vaddr = va; ++ ++ assert(fw + fw_size >= fw); ++ return vaddr >= fw && vaddr < fw + fw_size; ++} ++ ++/* ++ * e32_parse_ehdr() - Check and parse the ELF header ++ * ++ * fw: Firmware ELF file image ++ * size: Byte size of firmware ELF file image ++ */ ++TEE_Result e32_parse_ehdr(uint8_t *fw, size_t size) ++{ ++ Elf32_Ehdr *ehdr = (Elf32_Ehdr *)(void *)fw; ++ ++ if (!fw || !IS_ALIGNED_WITH_TYPE(fw, uint32_t)) { ++ EMSG("Invalid fw address %p", fw); ++ return TEE_ERROR_BAD_PARAMETERS; ++ } ++ ++ if (size < sizeof(Elf32_Ehdr) || ++ size < (ehdr->e_shoff + sizeof(Elf32_Shdr))) ++ return TEE_ERROR_CORRUPT_OBJECT; ++ ++ if (!IS_ELF(*ehdr) || ++ ehdr->e_ident[EI_VERSION] != EV_CURRENT || ++ ehdr->e_ident[EI_CLASS] != ELFCLASS32 || ++ ehdr->e_phentsize != sizeof(Elf32_Phdr) || ++ ehdr->e_shentsize != sizeof(Elf32_Shdr)) { ++ EMSG("Invalid header"); ++ ++ return TEE_ERROR_BAD_FORMAT; ++ } ++ ++ if (ehdr->e_phnum == 0) { ++ EMSG("No loadable segment found"); ++ return TEE_ERROR_BAD_FORMAT; ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++/* ++ * e32_parser_load_elf_image - simple ELF loader ++ * fw: Firmware ELF file image ++ * fw_size: Firmware ELF file image byte size ++ * load_seg: Callback for loading a firmware image segment into device memory ++ * priv_data: Private data passed to @load_seg callback. ++ */ ++TEE_Result e32_parser_load_elf_image(uint8_t *fw, size_t fw_size, ++ TEE_Result (*load_seg)(uint8_t *src, ++ uint32_t size, ++ uint32_t da, ++ uint32_t mem_size, ++ void *priv), ++ void *priv_data) ++{ ++ Elf32_Ehdr *ehdr = (Elf32_Ehdr *)(void *)fw; ++ Elf32_Phdr *phdr = (void *)((int8_t *)ehdr + ehdr->e_phoff); ++ TEE_Result res = TEE_SUCCESS; ++ unsigned int i = 0; ++ ++ if (!load_seg) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ if (!IS_ALIGNED_WITH_TYPE(phdr, uint32_t) || ++ !va_in_fwm_image_range(phdr, fw, fw_size)) ++ return TEE_ERROR_CORRUPT_OBJECT; ++ ++ for (i = 0; i < ehdr->e_phnum; i++, phdr++) { ++ uint32_t dst = phdr->p_paddr; ++ uint8_t *src = NULL; ++ ++ if (!va_in_fwm_image_range((void *)((vaddr_t)(phdr + 1) - 1), ++ fw, fw_size)) ++ return TEE_ERROR_CORRUPT_OBJECT; ++ ++ if (phdr->p_type != PT_LOAD) ++ continue; ++ ++ src = (uint8_t *)fw + phdr->p_offset; ++ ++ if (!va_in_fwm_image_range(src, fw, fw_size) || ++ !va_in_fwm_image_range(src + phdr->p_filesz, fw, fw_size)) ++ return TEE_ERROR_CORRUPT_OBJECT; ++ ++ res = load_seg(src, phdr->p_filesz, dst, phdr->p_memsz, ++ priv_data); ++ if (res) ++ return res; ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++/* Helper to find resource table in an ELF image */ ++int e32_parser_find_rsc_table(uint8_t *fw, size_t fw_size, ++ Elf32_Addr *rsc_addr, Elf32_Word *rsc_size) ++{ ++ Elf32_Shdr *shdr = NULL; ++ int i = 0; ++ char *name_table = NULL; ++ struct resource_table *table = NULL; ++ Elf32_Ehdr *ehdr = (Elf32_Ehdr *)(void *)fw; ++ uint8_t *elf_data = fw; ++ ++ shdr = (void *)(fw + ehdr->e_shoff); ++ if (!IS_ALIGNED_WITH_TYPE(shdr, uint32_t) || ++ !va_in_fwm_image_range(shdr, fw, fw_size)) ++ return TEE_ERROR_CORRUPT_OBJECT; ++ ++ name_table = (char *)elf_data + shdr[ehdr->e_shstrndx].sh_offset; ++ if (!va_in_fwm_image_range(name_table, fw, fw_size)) ++ return TEE_ERROR_CORRUPT_OBJECT; ++ ++ for (i = 0; i < ehdr->e_shnum; i++, shdr++) { ++ size_t size = shdr->sh_size; ++ size_t offset = shdr->sh_offset; ++ ++ if (!va_in_fwm_image_range(shdr, fw, fw_size)) ++ return TEE_ERROR_CORRUPT_OBJECT; ++ ++ if (strcmp(name_table + shdr->sh_name, ".resource_table")) ++ continue; ++ ++ if (!shdr->sh_size) { ++ IMSG("Ignore empty resource table section"); ++ return TEE_ERROR_NO_DATA; ++ } ++ ++ if (offset + size > fw_size || offset + size < size) { ++ EMSG("Resource table truncated"); ++ return TEE_ERROR_BAD_FORMAT; ++ } ++ ++ if (sizeof(struct resource_table) > size) { ++ EMSG("No header found in resource table"); ++ return TEE_ERROR_BAD_FORMAT; ++ } ++ ++ table = (struct resource_table *)(void *)(elf_data + offset); ++ if (!IS_ALIGNED_WITH_TYPE(table, uint32_t)) ++ return TEE_ERROR_CORRUPT_OBJECT; ++ ++ if (table->ver != 1) { ++ EMSG("Unsupported fw version %"PRId32, table->ver); ++ return TEE_ERROR_BAD_FORMAT; ++ } ++ ++ if (table->reserved[0] || table->reserved[1]) { ++ EMSG("Non zero reserved bytes"); ++ return TEE_ERROR_BAD_FORMAT; ++ } ++ ++ if (table->num * sizeof(*table->offset) + ++ sizeof(struct resource_table) > size) { ++ EMSG("Resource table incomplete"); ++ return TEE_ERROR_BAD_FORMAT; ++ } ++ ++ DMSG("Resource table address %#"PRIx32", size %"PRIu32, ++ shdr->sh_addr, shdr->sh_size); ++ ++ *rsc_addr = shdr->sh_addr; ++ *rsc_size = shdr->sh_size; ++ ++ return TEE_SUCCESS; ++ } ++ ++ return TEE_ERROR_NO_DATA; ++} +diff --git a/ta/remoteproc/include/elf32.h b/ta/remoteproc/include/elf32.h +new file mode 100644 +index 000000000..2806c615d +--- /dev/null ++++ b/ta/remoteproc/include/elf32.h +@@ -0,0 +1,243 @@ ++/* SPDX-License-Identifier: BSD-2-Clause */ ++/*- ++ * Copyright (c) 1996-1998 John D. Polstra. ++ * 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. ++ * ++ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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. ++ * ++ * $FreeBSD$ ++ */ ++ ++#ifndef _SYS_ELF32_H_ ++#define _SYS_ELF32_H_ 1 ++ ++#include ++#include ++ ++/* ++ * ELF definitions common to all 32-bit architectures. ++ */ ++ ++typedef uint32_t Elf32_Addr; ++typedef uint16_t Elf32_Half; ++typedef uint32_t Elf32_Off; ++typedef int32_t Elf32_Sword; ++typedef uint32_t Elf32_Word; ++typedef uint64_t Elf32_Lword; ++ ++typedef Elf32_Word Elf32_Hashelt; ++ ++/* Non-standard class-dependent datatype used for abstraction. */ ++typedef Elf32_Word Elf32_Size; ++typedef Elf32_Sword Elf32_Ssize; ++ ++/* ++ * ELF header. ++ */ ++ ++typedef struct { ++ unsigned char e_ident[EI_NIDENT]; /* File identification. */ ++ Elf32_Half e_type; /* File type. */ ++ Elf32_Half e_machine; /* Machine architecture. */ ++ Elf32_Word e_version; /* ELF format version. */ ++ Elf32_Addr e_entry; /* Entry point. */ ++ Elf32_Off e_phoff; /* Program header file offset. */ ++ Elf32_Off e_shoff; /* Section header file offset. */ ++ Elf32_Word e_flags; /* Architecture-specific flags. */ ++ Elf32_Half e_ehsize; /* Size of ELF header in bytes. */ ++ Elf32_Half e_phentsize; /* Size of program header entry. */ ++ Elf32_Half e_phnum; /* Number of program header entries. */ ++ Elf32_Half e_shentsize; /* Size of section header entry. */ ++ Elf32_Half e_shnum; /* Number of section header entries. */ ++ Elf32_Half e_shstrndx; /* Section name strings section. */ ++} Elf32_Ehdr; ++ ++/* ++ * Section header. ++ */ ++ ++typedef struct { ++ Elf32_Word sh_name; /* Section name (index into the ++ section header string table). */ ++ Elf32_Word sh_type; /* Section type. */ ++ Elf32_Word sh_flags; /* Section flags. */ ++ Elf32_Addr sh_addr; /* Address in memory image. */ ++ Elf32_Off sh_offset; /* Offset in file. */ ++ Elf32_Word sh_size; /* Size in bytes. */ ++ Elf32_Word sh_link; /* Index of a related section. */ ++ Elf32_Word sh_info; /* Depends on section type. */ ++ Elf32_Word sh_addralign; /* Alignment in bytes. */ ++ Elf32_Word sh_entsize; /* Size of each entry in section. */ ++} Elf32_Shdr; ++ ++/* ++ * Program header. ++ */ ++ ++typedef struct { ++ Elf32_Word p_type; /* Entry type. */ ++ Elf32_Off p_offset; /* File offset of contents. */ ++ Elf32_Addr p_vaddr; /* Virtual address in memory image. */ ++ Elf32_Addr p_paddr; /* Physical address (not used). */ ++ Elf32_Word p_filesz; /* Size of contents in file. */ ++ Elf32_Word p_memsz; /* Size of contents in memory. */ ++ Elf32_Word p_flags; /* Access permission flags. */ ++ Elf32_Word p_align; /* Alignment in memory and file. */ ++} Elf32_Phdr; ++ ++/* ++ * Dynamic structure. The ".dynamic" section contains an array of them. ++ */ ++ ++typedef struct { ++ Elf32_Sword d_tag; /* Entry type. */ ++ union { ++ Elf32_Word d_val; /* Integer value. */ ++ Elf32_Addr d_ptr; /* Address value. */ ++ } d_un; ++} Elf32_Dyn; ++ ++/* ++ * Relocation entries. ++ */ ++ ++/* Relocations that don't need an addend field. */ ++typedef struct { ++ Elf32_Addr r_offset; /* Location to be relocated. */ ++ Elf32_Word r_info; /* Relocation type and symbol index. */ ++} Elf32_Rel; ++ ++/* Relocations that need an addend field. */ ++typedef struct { ++ Elf32_Addr r_offset; /* Location to be relocated. */ ++ Elf32_Word r_info; /* Relocation type and symbol index. */ ++ Elf32_Sword r_addend; /* Addend. */ ++} Elf32_Rela; ++ ++/* Macros for accessing the fields of r_info. */ ++#define ELF32_R_SYM(info) ((info) >> 8) ++#define ELF32_R_TYPE(info) ((unsigned char)(info)) ++ ++/* Macro for constructing r_info from field values. */ ++#define ELF32_R_INFO(sym, type) (((sym) << 8) + (unsigned char)(type)) ++ ++/* ++ * Note entry header ++ */ ++typedef Elf_Note Elf32_Nhdr; ++ ++/* ++ * Move entry ++ */ ++typedef struct { ++ Elf32_Lword m_value; /* symbol value */ ++ Elf32_Word m_info; /* size + index */ ++ Elf32_Word m_poffset; /* symbol offset */ ++ Elf32_Half m_repeat; /* repeat count */ ++ Elf32_Half m_stride; /* stride info */ ++} Elf32_Move; ++ ++/* ++ * The macros compose and decompose values for Move.r_info ++ * ++ * sym = ELF32_M_SYM(M.m_info) ++ * size = ELF32_M_SIZE(M.m_info) ++ * M.m_info = ELF32_M_INFO(sym, size) ++ */ ++#define ELF32_M_SYM(info) ((info)>>8) ++#define ELF32_M_SIZE(info) ((unsigned char)(info)) ++#define ELF32_M_INFO(sym, size) (((sym)<<8)+(unsigned char)(size)) ++ ++/* ++ * Hardware/Software capabilities entry ++ */ ++typedef struct { ++ Elf32_Word c_tag; /* how to interpret value */ ++ union { ++ Elf32_Word c_val; ++ Elf32_Addr c_ptr; ++ } c_un; ++} Elf32_Cap; ++ ++/* ++ * Symbol table entries. ++ */ ++ ++typedef struct { ++ Elf32_Word st_name; /* String table index of name. */ ++ Elf32_Addr st_value; /* Symbol value. */ ++ Elf32_Word st_size; /* Size of associated object. */ ++ unsigned char st_info; /* Type and binding information. */ ++ unsigned char st_other; /* Reserved (not used). */ ++ Elf32_Half st_shndx; /* Section index of symbol. */ ++} Elf32_Sym; ++ ++/* Macros for accessing the fields of st_info. */ ++#define ELF32_ST_BIND(info) ((info) >> 4) ++#define ELF32_ST_TYPE(info) ((info) & 0xf) ++ ++/* Macro for constructing st_info from field values. */ ++#define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf)) ++ ++/* Macro for accessing the fields of st_other. */ ++#define ELF32_ST_VISIBILITY(oth) ((oth) & 0x3) ++ ++/* Structures used by Sun & GNU symbol versioning. */ ++typedef struct { ++ Elf32_Half vd_version; ++ Elf32_Half vd_flags; ++ Elf32_Half vd_ndx; ++ Elf32_Half vd_cnt; ++ Elf32_Word vd_hash; ++ Elf32_Word vd_aux; ++ Elf32_Word vd_next; ++} Elf32_Verdef; ++ ++typedef struct { ++ Elf32_Word vda_name; ++ Elf32_Word vda_next; ++} Elf32_Verdaux; ++ ++typedef struct { ++ Elf32_Half vn_version; ++ Elf32_Half vn_cnt; ++ Elf32_Word vn_file; ++ Elf32_Word vn_aux; ++ Elf32_Word vn_next; ++} Elf32_Verneed; ++ ++typedef struct { ++ Elf32_Word vna_hash; ++ Elf32_Half vna_flags; ++ Elf32_Half vna_other; ++ Elf32_Word vna_name; ++ Elf32_Word vna_next; ++} Elf32_Vernaux; ++ ++typedef Elf32_Half Elf32_Versym; ++ ++typedef struct { ++ Elf32_Half si_boundto; /* direct bindings - symbol bound to */ ++ Elf32_Half si_flags; /* per symbol flags */ ++} Elf32_Syminfo; ++ ++#endif /* !_SYS_ELF32_H_ */ +diff --git a/ta/remoteproc/include/elf_common.h b/ta/remoteproc/include/elf_common.h +new file mode 100644 +index 000000000..a1da0ef80 +--- /dev/null ++++ b/ta/remoteproc/include/elf_common.h +@@ -0,0 +1,1014 @@ ++/* SPDX-License-Identifier: BSD-2-Clause */ ++/*- ++ * Copyright (c) 2000, 2001, 2008, 2011, David E. O'Brien ++ * Copyright (c) 1998 John D. Polstra. ++ * 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. ++ * ++ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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. ++ * ++ * $FreeBSD$ ++ */ ++ ++#ifndef _SYS_ELF_COMMON_H_ ++#define _SYS_ELF_COMMON_H_ 1 ++ ++#include ++ ++/* ++ * ELF definitions that are independent of architecture or word size. ++ */ ++ ++#ifndef __ASSEMBLER__ ++/* ++ * Note header. The ".note" section contains an array of notes. Each ++ * begins with this header, aligned to a word boundary. Immediately ++ * following the note header is n_namesz bytes of name, padded to the ++ * next word boundary. Then comes n_descsz bytes of descriptor, again ++ * padded to a word boundary. The values of n_namesz and n_descsz do ++ * not include the padding. ++ */ ++ ++typedef struct { ++ uint32_t n_namesz; /* Length of name. */ ++ uint32_t n_descsz; /* Length of descriptor. */ ++ uint32_t n_type; /* Type of this note. */ ++} Elf_Note; ++ ++/* ++ * The header for GNU-style hash sections. ++ */ ++ ++typedef struct { ++ uint32_t gh_nbuckets; /* Number of hash buckets. */ ++ uint32_t gh_symndx; /* First visible symbol in .dynsym. */ ++ uint32_t gh_maskwords; /* #maskwords used in bloom filter. */ ++ uint32_t gh_shift2; /* Bloom filter shift count. */ ++} Elf_GNU_Hash_Header; ++#endif /*__ASSEMBLER__*/ ++ ++/* Indexes into the e_ident array. Keep synced with ++ http://www.sco.com/developers/gabi/latest/ch4.eheader.html */ ++#define EI_MAG0 0 /* Magic number, byte 0. */ ++#define EI_MAG1 1 /* Magic number, byte 1. */ ++#define EI_MAG2 2 /* Magic number, byte 2. */ ++#define EI_MAG3 3 /* Magic number, byte 3. */ ++#define EI_CLASS 4 /* Class of machine. */ ++#define EI_DATA 5 /* Data format. */ ++#define EI_VERSION 6 /* ELF format version. */ ++#define EI_OSABI 7 /* Operating system / ABI identification */ ++#define EI_ABIVERSION 8 /* ABI version */ ++#define OLD_EI_BRAND 8 /* Start of architecture identification. */ ++#define EI_PAD 9 /* Start of padding (per SVR4 ABI). */ ++#define EI_NIDENT 16 /* Size of e_ident array. */ ++ ++/* Values for the magic number bytes. */ ++#define ELFMAG0 0x7f ++#define ELFMAG1 'E' ++#define ELFMAG2 'L' ++#define ELFMAG3 'F' ++#define ELFMAG "\177ELF" /* magic string */ ++#define SELFMAG 4 /* magic string size */ ++ ++/* Values for e_ident[EI_VERSION] and e_version. */ ++#define EV_NONE 0 ++#define EV_CURRENT 1 ++ ++/* Values for e_ident[EI_CLASS]. */ ++#define ELFCLASSNONE 0 /* Unknown class. */ ++#define ELFCLASS32 1 /* 32-bit architecture. */ ++#define ELFCLASS64 2 /* 64-bit architecture. */ ++ ++/* Values for e_ident[EI_DATA]. */ ++#define ELFDATANONE 0 /* Unknown data format. */ ++#define ELFDATA2LSB 1 /* 2's complement little-endian. */ ++#define ELFDATA2MSB 2 /* 2's complement big-endian. */ ++ ++/* Values for e_ident[EI_OSABI]. */ ++#define ELFOSABI_NONE 0 /* UNIX System V ABI */ ++#define ELFOSABI_HPUX 1 /* HP-UX operating system */ ++#define ELFOSABI_NETBSD 2 /* NetBSD */ ++#define ELFOSABI_LINUX 3 /* GNU/Linux */ ++#define ELFOSABI_HURD 4 /* GNU/Hurd */ ++#define ELFOSABI_86OPEN 5 /* 86Open common IA32 ABI */ ++#define ELFOSABI_SOLARIS 6 /* Solaris */ ++#define ELFOSABI_AIX 7 /* AIX */ ++#define ELFOSABI_IRIX 8 /* IRIX */ ++#define ELFOSABI_FREEBSD 9 /* FreeBSD */ ++#define ELFOSABI_TRU64 10 /* TRU64 UNIX */ ++#define ELFOSABI_MODESTO 11 /* Novell Modesto */ ++#define ELFOSABI_OPENBSD 12 /* OpenBSD */ ++#define ELFOSABI_OPENVMS 13 /* Open VMS */ ++#define ELFOSABI_NSK 14 /* HP Non-Stop Kernel */ ++#define ELFOSABI_AROS 15 /* Amiga Research OS */ ++#define ELFOSABI_ARM 97 /* ARM */ ++#define ELFOSABI_STANDALONE 255 /* Standalone (embedded) application */ ++ ++#define ELFOSABI_SYSV ELFOSABI_NONE /* symbol used in old spec */ ++#define ELFOSABI_MONTEREY ELFOSABI_AIX /* Monterey */ ++ ++/* e_ident */ ++#define IS_ELF(ehdr) ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \ ++ (ehdr).e_ident[EI_MAG1] == ELFMAG1 && \ ++ (ehdr).e_ident[EI_MAG2] == ELFMAG2 && \ ++ (ehdr).e_ident[EI_MAG3] == ELFMAG3) ++ ++/* Values for e_type. */ ++#define ET_NONE 0 /* Unknown type. */ ++#define ET_REL 1 /* Relocatable. */ ++#define ET_EXEC 2 /* Executable. */ ++#define ET_DYN 3 /* Shared object. */ ++#define ET_CORE 4 /* Core file. */ ++#define ET_LOOS 0xfe00 /* First operating system specific. */ ++#define ET_HIOS 0xfeff /* Last operating system-specific. */ ++#define ET_LOPROC 0xff00 /* First processor-specific. */ ++#define ET_HIPROC 0xffff /* Last processor-specific. */ ++ ++/* Values for e_machine. */ ++#define EM_NONE 0 /* Unknown machine. */ ++#define EM_M32 1 /* AT&T WE32100. */ ++#define EM_SPARC 2 /* Sun SPARC. */ ++#define EM_386 3 /* Intel i386. */ ++#define EM_68K 4 /* Motorola 68000. */ ++#define EM_88K 5 /* Motorola 88000. */ ++#define EM_860 7 /* Intel i860. */ ++#define EM_MIPS 8 /* MIPS R3000 Big-Endian only. */ ++#define EM_S370 9 /* IBM System/370. */ ++#define EM_MIPS_RS3_LE 10 /* MIPS R3000 Little-Endian. */ ++#define EM_PARISC 15 /* HP PA-RISC. */ ++#define EM_VPP500 17 /* Fujitsu VPP500. */ ++#define EM_SPARC32PLUS 18 /* SPARC v8plus. */ ++#define EM_960 19 /* Intel 80960. */ ++#define EM_PPC 20 /* PowerPC 32-bit. */ ++#define EM_PPC64 21 /* PowerPC 64-bit. */ ++#define EM_S390 22 /* IBM System/390. */ ++#define EM_V800 36 /* NEC V800. */ ++#define EM_FR20 37 /* Fujitsu FR20. */ ++#define EM_RH32 38 /* TRW RH-32. */ ++#define EM_RCE 39 /* Motorola RCE. */ ++#define EM_ARM 40 /* ARM. */ ++#define EM_SH 42 /* Hitachi SH. */ ++#define EM_SPARCV9 43 /* SPARC v9 64-bit. */ ++#define EM_TRICORE 44 /* Siemens TriCore embedded processor. */ ++#define EM_ARC 45 /* Argonaut RISC Core. */ ++#define EM_H8_300 46 /* Hitachi H8/300. */ ++#define EM_H8_300H 47 /* Hitachi H8/300H. */ ++#define EM_H8S 48 /* Hitachi H8S. */ ++#define EM_H8_500 49 /* Hitachi H8/500. */ ++#define EM_IA_64 50 /* Intel IA-64 Processor. */ ++#define EM_MIPS_X 51 /* Stanford MIPS-X. */ ++#define EM_COLDFIRE 52 /* Motorola ColdFire. */ ++#define EM_68HC12 53 /* Motorola M68HC12. */ ++#define EM_MMA 54 /* Fujitsu MMA. */ ++#define EM_PCP 55 /* Siemens PCP. */ ++#define EM_NCPU 56 /* Sony nCPU. */ ++#define EM_NDR1 57 /* Denso NDR1 microprocessor. */ ++#define EM_STARCORE 58 /* Motorola Star*Core processor. */ ++#define EM_ME16 59 /* Toyota ME16 processor. */ ++#define EM_ST100 60 /* STMicroelectronics ST100 processor. */ ++#define EM_TINYJ 61 /* Advanced Logic Corp. TinyJ processor. */ ++#define EM_X86_64 62 /* Advanced Micro Devices x86-64 */ ++#define EM_AMD64 EM_X86_64 /* Advanced Micro Devices x86-64 (compat) */ ++#define EM_PDSP 63 /* Sony DSP Processor. */ ++#define EM_FX66 66 /* Siemens FX66 microcontroller. */ ++#define EM_ST9PLUS 67 /* STMicroelectronics ST9+ 8/16 ++ microcontroller. */ ++#define EM_ST7 68 /* STmicroelectronics ST7 8-bit ++ microcontroller. */ ++#define EM_68HC16 69 /* Motorola MC68HC16 microcontroller. */ ++#define EM_68HC11 70 /* Motorola MC68HC11 microcontroller. */ ++#define EM_68HC08 71 /* Motorola MC68HC08 microcontroller. */ ++#define EM_68HC05 72 /* Motorola MC68HC05 microcontroller. */ ++#define EM_SVX 73 /* Silicon Graphics SVx. */ ++#define EM_ST19 74 /* STMicroelectronics ST19 8-bit mc. */ ++#define EM_VAX 75 /* Digital VAX. */ ++#define EM_CRIS 76 /* Axis Communications 32-bit embedded ++ processor. */ ++#define EM_JAVELIN 77 /* Infineon Technologies 32-bit embedded ++ processor. */ ++#define EM_FIREPATH 78 /* Element 14 64-bit DSP Processor. */ ++#define EM_ZSP 79 /* LSI Logic 16-bit DSP Processor. */ ++#define EM_MMIX 80 /* Donald Knuth's educational 64-bit proc. */ ++#define EM_HUANY 81 /* Harvard University machine-independent ++ object files. */ ++#define EM_PRISM 82 /* SiTera Prism. */ ++#define EM_AVR 83 /* Atmel AVR 8-bit microcontroller. */ ++#define EM_FR30 84 /* Fujitsu FR30. */ ++#define EM_D10V 85 /* Mitsubishi D10V. */ ++#define EM_D30V 86 /* Mitsubishi D30V. */ ++#define EM_V850 87 /* NEC v850. */ ++#define EM_M32R 88 /* Mitsubishi M32R. */ ++#define EM_MN10300 89 /* Matsushita MN10300. */ ++#define EM_MN10200 90 /* Matsushita MN10200. */ ++#define EM_PJ 91 /* picoJava. */ ++#define EM_OPENRISC 92 /* OpenRISC 32-bit embedded processor. */ ++#define EM_ARC_A5 93 /* ARC Cores Tangent-A5. */ ++#define EM_XTENSA 94 /* Tensilica Xtensa Architecture. */ ++#define EM_VIDEOCORE 95 /* Alphamosaic VideoCore processor. */ ++#define EM_TMM_GPP 96 /* Thompson Multimedia General Purpose ++ Processor. */ ++#define EM_NS32K 97 /* National Semiconductor 32000 series. */ ++#define EM_TPC 98 /* Tenor Network TPC processor. */ ++#define EM_SNP1K 99 /* Trebia SNP 1000 processor. */ ++#define EM_ST200 100 /* STMicroelectronics ST200 microcontroller. */ ++#define EM_IP2K 101 /* Ubicom IP2xxx microcontroller family. */ ++#define EM_MAX 102 /* MAX Processor. */ ++#define EM_CR 103 /* National Semiconductor CompactRISC ++ microprocessor. */ ++#define EM_F2MC16 104 /* Fujitsu F2MC16. */ ++#define EM_MSP430 105 /* Texas Instruments embedded microcontroller ++ msp430. */ ++#define EM_BLACKFIN 106 /* Analog Devices Blackfin (DSP) processor. */ ++#define EM_SE_C33 107 /* S1C33 Family of Seiko Epson processors. */ ++#define EM_SEP 108 /* Sharp embedded microprocessor. */ ++#define EM_ARCA 109 /* Arca RISC Microprocessor. */ ++#define EM_UNICORE 110 /* Microprocessor series from PKU-Unity Ltd. ++ and MPRC of Peking University */ ++#define EM_AARCH64 183 /* AArch64 (64-bit ARM) */ ++ ++/* Non-standard or deprecated. */ ++#define EM_486 6 /* Intel i486. */ ++#define EM_MIPS_RS4_BE 10 /* MIPS R4000 Big-Endian */ ++#define EM_ALPHA_STD 41 /* Digital Alpha (standard value). */ ++#define EM_ALPHA 0x9026 /* Alpha (written in the absence of an ABI) */ ++ ++/* e_flags for EM_ARM */ ++#define EF_ARM_ABI_VERSION 0x05000000 /* ABI version 5 */ ++#define EF_ARM_ABIMASK 0xFF000000 ++#define EF_ARM_BE8 0x00800000 ++#define EF_ARM_ABI_FLOAT_HARD 0x00000400 /* ABI version 5 and later */ ++#define EF_ARM_ABI_FLOAT_SOFT 0x00000200 /* ABI version 5 and later */ ++ ++/* Special section indexes. */ ++#define SHN_UNDEF 0 /* Undefined, missing, irrelevant. */ ++#define SHN_LORESERVE 0xff00 /* First of reserved range. */ ++#define SHN_LOPROC 0xff00 /* First processor-specific. */ ++#define SHN_HIPROC 0xff1f /* Last processor-specific. */ ++#define SHN_LOOS 0xff20 /* First operating system-specific. */ ++#define SHN_HIOS 0xff3f /* Last operating system-specific. */ ++#define SHN_ABS 0xfff1 /* Absolute values. */ ++#define SHN_COMMON 0xfff2 /* Common data. */ ++#define SHN_XINDEX 0xffff /* Escape -- index stored elsewhere. */ ++#define SHN_HIRESERVE 0xffff /* Last of reserved range. */ ++ ++/* sh_type */ ++#define SHT_NULL 0 /* inactive */ ++#define SHT_PROGBITS 1 /* program defined information */ ++#define SHT_SYMTAB 2 /* symbol table section */ ++#define SHT_STRTAB 3 /* string table section */ ++#define SHT_RELA 4 /* relocation section with addends */ ++#define SHT_HASH 5 /* symbol hash table section */ ++#define SHT_DYNAMIC 6 /* dynamic section */ ++#define SHT_NOTE 7 /* note section */ ++#define SHT_NOBITS 8 /* no space section */ ++#define SHT_REL 9 /* relocation section - no addends */ ++#define SHT_SHLIB 10 /* reserved - purpose unknown */ ++#define SHT_DYNSYM 11 /* dynamic symbol table section */ ++#define SHT_INIT_ARRAY 14 /* Initialization function pointers. */ ++#define SHT_FINI_ARRAY 15 /* Termination function pointers. */ ++#define SHT_PREINIT_ARRAY 16 /* Pre-initialization function ptrs. */ ++#define SHT_GROUP 17 /* Section group. */ ++#define SHT_SYMTAB_SHNDX 18 /* Section indexes (see SHN_XINDEX). */ ++#define SHT_LOOS 0x60000000 /* First of OS specific semantics */ ++#define SHT_LOSUNW 0x6ffffff4 ++#define SHT_SUNW_dof 0x6ffffff4 ++#define SHT_SUNW_cap 0x6ffffff5 ++#define SHT_SUNW_SIGNATURE 0x6ffffff6 ++#define SHT_GNU_HASH 0x6ffffff6 ++#define SHT_GNU_LIBLIST 0x6ffffff7 ++#define SHT_SUNW_ANNOTATE 0x6ffffff7 ++#define SHT_SUNW_DEBUGSTR 0x6ffffff8 ++#define SHT_SUNW_DEBUG 0x6ffffff9 ++#define SHT_SUNW_move 0x6ffffffa ++#define SHT_SUNW_COMDAT 0x6ffffffb ++#define SHT_SUNW_syminfo 0x6ffffffc ++#define SHT_SUNW_verdef 0x6ffffffd ++#define SHT_GNU_verdef 0x6ffffffd /* Symbol versions provided */ ++#define SHT_SUNW_verneed 0x6ffffffe ++#define SHT_GNU_verneed 0x6ffffffe /* Symbol versions required */ ++#define SHT_SUNW_versym 0x6fffffff ++#define SHT_GNU_versym 0x6fffffff /* Symbol version table */ ++#define SHT_HISUNW 0x6fffffff ++#define SHT_HIOS 0x6fffffff /* Last of OS specific semantics */ ++#define SHT_LOPROC 0x70000000 /* reserved range for processor */ ++#define SHT_AMD64_UNWIND 0x70000001 /* unwind information */ ++#define SHT_ARM_EXIDX 0x70000001 /* Exception index table. */ ++#define SHT_ARM_PREEMPTMAP 0x70000002 /* BPABI DLL dynamic linking ++ pre-emption map. */ ++#define SHT_ARM_ATTRIBUTES 0x70000003 /* Object file compatibility ++ attributes. */ ++#define SHT_ARM_DEBUGOVERLAY 0x70000004 /* See DBGOVL for details. */ ++#define SHT_ARM_OVERLAYSECTION 0x70000005 /* See DBGOVL for details. */ ++#define SHT_MIPS_REGINFO 0x70000006 ++#define SHT_MIPS_OPTIONS 0x7000000d ++#define SHT_MIPS_DWARF 0x7000001e /* MIPS gcc uses MIPS_DWARF */ ++#define SHT_HIPROC 0x7fffffff /* specific section header types */ ++#define SHT_LOUSER 0x80000000 /* reserved range for application */ ++#define SHT_HIUSER 0xffffffff /* specific indexes */ ++ ++/* Flags for sh_flags. */ ++#define SHF_WRITE 0x1 /* Section contains writable data. */ ++#define SHF_ALLOC 0x2 /* Section occupies memory. */ ++#define SHF_EXECINSTR 0x4 /* Section contains instructions. */ ++#define SHF_MERGE 0x10 /* Section may be merged. */ ++#define SHF_STRINGS 0x20 /* Section contains strings. */ ++#define SHF_INFO_LINK 0x40 /* sh_info holds section index. */ ++#define SHF_LINK_ORDER 0x80 /* Special ordering requirements. */ ++#define SHF_OS_NONCONFORMING 0x100 /* OS-specific processing required. */ ++#define SHF_GROUP 0x200 /* Member of section group. */ ++#define SHF_TLS 0x400 /* Section contains TLS data. */ ++#define SHF_MASKOS 0x0ff00000 /* OS-specific semantics. */ ++#define SHF_MASKPROC 0xf0000000 /* Processor-specific semantics. */ ++ ++/* Values for p_type. */ ++#define PT_NULL 0 /* Unused entry. */ ++#define PT_LOAD 1 /* Loadable segment. */ ++#define PT_DYNAMIC 2 /* Dynamic linking information segment. */ ++#define PT_INTERP 3 /* Pathname of interpreter. */ ++#define PT_NOTE 4 /* Auxiliary information. */ ++#define PT_SHLIB 5 /* Reserved (not used). */ ++#define PT_PHDR 6 /* Location of program header itself. */ ++#define PT_TLS 7 /* Thread local storage segment */ ++#define PT_LOOS 0x60000000 /* First OS-specific. */ ++#define PT_SUNW_UNWIND 0x6464e550 /* amd64 UNWIND program header */ ++#define PT_GNU_EH_FRAME 0x6474e550 ++#define PT_GNU_STACK 0x6474e551 ++#define PT_GNU_RELRO 0x6474e552 ++#define PT_LOSUNW 0x6ffffffa ++#define PT_SUNWBSS 0x6ffffffa /* Sun Specific segment */ ++#define PT_SUNWSTACK 0x6ffffffb /* describes the stack segment */ ++#define PT_SUNWDTRACE 0x6ffffffc /* private */ ++#define PT_SUNWCAP 0x6ffffffd /* hard/soft capabilities segment */ ++#define PT_HISUNW 0x6fffffff ++#define PT_HIOS 0x6fffffff /* Last OS-specific. */ ++#define PT_LOPROC 0x70000000 /* First processor-specific type. */ ++#define PT_ARM_EXIDX 0x70000001 /* .ARM.exidx segment */ ++#define PT_HIPROC 0x7fffffff /* Last processor-specific type. */ ++ ++/* Values for p_flags. */ ++#define PF_X 0x1 /* Executable. */ ++#define PF_W 0x2 /* Writable. */ ++#define PF_R 0x4 /* Readable. */ ++#define PF_MASKOS 0x0ff00000 /* Operating system-specific. */ ++#define PF_MASKPROC 0xf0000000 /* Processor-specific. */ ++ ++/* Extended program header index. */ ++#define PN_XNUM 0xffff ++ ++/* Values for d_tag. */ ++#define DT_NULL 0 /* Terminating entry. */ ++#define DT_NEEDED 1 /* String table offset of a needed shared ++ library. */ ++#define DT_PLTRELSZ 2 /* Total size in bytes of PLT relocations. */ ++#define DT_PLTGOT 3 /* Processor-dependent address. */ ++#define DT_HASH 4 /* Address of symbol hash table. */ ++#define DT_STRTAB 5 /* Address of string table. */ ++#define DT_SYMTAB 6 /* Address of symbol table. */ ++#define DT_RELA 7 /* Address of ElfNN_Rela relocations. */ ++#define DT_RELASZ 8 /* Total size of ElfNN_Rela relocations. */ ++#define DT_RELAENT 9 /* Size of each ElfNN_Rela relocation entry. */ ++#define DT_STRSZ 10 /* Size of string table. */ ++#define DT_SYMENT 11 /* Size of each symbol table entry. */ ++#define DT_INIT 12 /* Address of initialization function. */ ++#define DT_FINI 13 /* Address of finalization function. */ ++#define DT_SONAME 14 /* String table offset of shared object ++ name. */ ++#define DT_RPATH 15 /* String table offset of library path. [sup] */ ++#define DT_SYMBOLIC 16 /* Indicates "symbolic" linking. [sup] */ ++#define DT_REL 17 /* Address of ElfNN_Rel relocations. */ ++#define DT_RELSZ 18 /* Total size of ElfNN_Rel relocations. */ ++#define DT_RELENT 19 /* Size of each ElfNN_Rel relocation. */ ++#define DT_PLTREL 20 /* Type of relocation used for PLT. */ ++#define DT_DEBUG 21 /* Reserved (not used). */ ++#define DT_TEXTREL 22 /* Indicates there may be relocations in ++ non-writable segments. [sup] */ ++#define DT_JMPREL 23 /* Address of PLT relocations. */ ++#define DT_BIND_NOW 24 /* [sup] */ ++#define DT_INIT_ARRAY 25 /* Address of the array of pointers to ++ initialization functions */ ++#define DT_FINI_ARRAY 26 /* Address of the array of pointers to ++ termination functions */ ++#define DT_INIT_ARRAYSZ 27 /* Size in bytes of the array of ++ initialization functions. */ ++#define DT_FINI_ARRAYSZ 28 /* Size in bytes of the array of ++ termination functions. */ ++#define DT_RUNPATH 29 /* String table offset of a null-terminated ++ library search path string. */ ++#define DT_FLAGS 30 /* Object specific flag values. */ ++#define DT_ENCODING 32 /* Values greater than or equal to DT_ENCODING ++ and less than DT_LOOS follow the rules for ++ the interpretation of the d_un union ++ as follows: even == 'd_ptr', odd == 'd_val' ++ or none */ ++#define DT_PREINIT_ARRAY 32 /* Address of the array of pointers to ++ pre-initialization functions. */ ++#define DT_PREINIT_ARRAYSZ 33 /* Size in bytes of the array of ++ pre-initialization functions. */ ++#define DT_MAXPOSTAGS 34 /* number of positive tags */ ++#define DT_LOOS 0x6000000d /* First OS-specific */ ++#define DT_SUNW_AUXILIARY 0x6000000d /* symbol auxiliary name */ ++#define DT_SUNW_RTLDINF 0x6000000e /* ld.so.1 info (private) */ ++#define DT_SUNW_FILTER 0x6000000f /* symbol filter name */ ++#define DT_SUNW_CAP 0x60000010 /* hardware/software */ ++#define DT_HIOS 0x6ffff000 /* Last OS-specific */ ++ ++/* ++ * DT_* entries which fall between DT_VALRNGHI & DT_VALRNGLO use the ++ * Dyn.d_un.d_val field of the Elf*_Dyn structure. ++ */ ++#define DT_VALRNGLO 0x6ffffd00 ++#define DT_CHECKSUM 0x6ffffdf8 /* elf checksum */ ++#define DT_PLTPADSZ 0x6ffffdf9 /* pltpadding size */ ++#define DT_MOVEENT 0x6ffffdfa /* move table entry size */ ++#define DT_MOVESZ 0x6ffffdfb /* move table size */ ++#define DT_FEATURE_1 0x6ffffdfc /* feature holder */ ++#define DT_POSFLAG_1 0x6ffffdfd /* flags for DT_* entries, effecting */ ++ /* the following DT_* entry. */ ++ /* See DF_P1_* definitions */ ++#define DT_SYMINSZ 0x6ffffdfe /* syminfo table size (in bytes) */ ++#define DT_SYMINENT 0x6ffffdff /* syminfo entry size (in bytes) */ ++#define DT_VALRNGHI 0x6ffffdff ++ ++/* ++ * DT_* entries which fall between DT_ADDRRNGHI & DT_ADDRRNGLO use the ++ * Dyn.d_un.d_ptr field of the Elf*_Dyn structure. ++ * ++ * If any adjustment is made to the ELF object after it has been ++ * built, these entries will need to be adjusted. ++ */ ++#define DT_ADDRRNGLO 0x6ffffe00 ++#define DT_GNU_HASH 0x6ffffef5 /* GNU-style hash table */ ++#define DT_CONFIG 0x6ffffefa /* configuration information */ ++#define DT_DEPAUDIT 0x6ffffefb /* dependency auditing */ ++#define DT_AUDIT 0x6ffffefc /* object auditing */ ++#define DT_PLTPAD 0x6ffffefd /* pltpadding (sparcv9) */ ++#define DT_MOVETAB 0x6ffffefe /* move table */ ++#define DT_SYMINFO 0x6ffffeff /* syminfo table */ ++#define DT_ADDRRNGHI 0x6ffffeff ++ ++#define DT_VERSYM 0x6ffffff0 /* Address of versym section. */ ++#define DT_RELACOUNT 0x6ffffff9 /* number of RELATIVE relocations */ ++#define DT_RELCOUNT 0x6ffffffa /* number of RELATIVE relocations */ ++#define DT_FLAGS_1 0x6ffffffb /* state flags - see DF_1_* defs */ ++#define DT_VERDEF 0x6ffffffc /* Address of verdef section. */ ++#define DT_VERDEFNUM 0x6ffffffd /* Number of elems in verdef section */ ++#define DT_VERNEED 0x6ffffffe /* Address of verneed section. */ ++#define DT_VERNEEDNUM 0x6fffffff /* Number of elems in verneed section */ ++ ++#define DT_LOPROC 0x70000000 /* First processor-specific type. */ ++#define DT_DEPRECATED_SPARC_REGISTER 0x7000001 ++#define DT_AUXILIARY 0x7ffffffd /* shared library auxiliary name */ ++#define DT_USED 0x7ffffffe /* ignored - same as needed */ ++#define DT_FILTER 0x7fffffff /* shared library filter name */ ++#define DT_HIPROC 0x7fffffff /* Last processor-specific type. */ ++ ++/* Values for DT_FLAGS */ ++#define DF_ORIGIN 0x0001 /* Indicates that the object being loaded may ++ make reference to the $ORIGIN substitution ++ string */ ++#define DF_SYMBOLIC 0x0002 /* Indicates "symbolic" linking. */ ++#define DF_TEXTREL 0x0004 /* Indicates there may be relocations in ++ non-writable segments. */ ++#define DF_BIND_NOW 0x0008 /* Indicates that the dynamic linker should ++ process all relocations for the object ++ containing this entry before transferring ++ control to the program. */ ++#define DF_STATIC_TLS 0x0010 /* Indicates that the shared object or ++ executable contains code using a static ++ thread-local storage scheme. */ ++ ++/* Values for DT_FLAGS_1 */ ++#define DF_1_BIND_NOW 0x00000001 /* Same as DF_BIND_NOW */ ++#define DF_1_GLOBAL 0x00000002 /* Set the RTLD_GLOBAL for object */ ++#define DF_1_NODELETE 0x00000008 /* Set the RTLD_NODELETE for object */ ++#define DF_1_LOADFLTR 0x00000010 /* Immediate loading of filtees */ ++#define DF_1_NOOPEN 0x00000040 /* Do not allow loading on dlopen() */ ++#define DF_1_ORIGIN 0x00000080 /* Process $ORIGIN */ ++#define DF_1_INTERPOSE 0x00000400 /* Interpose all objects but main */ ++#define DF_1_NODEFLIB 0x00000800 /* Do not search default paths */ ++ ++/* Values for n_type. Used in core files. */ ++#define NT_PRSTATUS 1 /* Process status. */ ++#define NT_FPREGSET 2 /* Floating point registers. */ ++#define NT_PRPSINFO 3 /* Process state info. */ ++#define NT_THRMISC 7 /* Thread miscellaneous info. */ ++#define NT_PROCSTAT_PROC 8 /* Procstat proc data. */ ++#define NT_PROCSTAT_FILES 9 /* Procstat files data. */ ++#define NT_PROCSTAT_VMMAP 10 /* Procstat vmmap data. */ ++#define NT_PROCSTAT_GROUPS 11 /* Procstat groups data. */ ++#define NT_PROCSTAT_UMASK 12 /* Procstat umask data. */ ++#define NT_PROCSTAT_RLIMIT 13 /* Procstat rlimit data. */ ++#define NT_PROCSTAT_OSREL 14 /* Procstat osreldate data. */ ++#define NT_PROCSTAT_PSSTRINGS 15 /* Procstat ps_strings data. */ ++#define NT_PROCSTAT_AUXV 16 /* Procstat auxv data. */ ++ ++/* Symbol Binding - ELFNN_ST_BIND - st_info */ ++#define STB_LOCAL 0 /* Local symbol */ ++#define STB_GLOBAL 1 /* Global symbol */ ++#define STB_WEAK 2 /* like global - lower precedence */ ++#define STB_LOOS 10 /* Reserved range for operating system */ ++#define STB_HIOS 12 /* specific semantics. */ ++#define STB_LOPROC 13 /* reserved range for processor */ ++#define STB_HIPROC 15 /* specific semantics. */ ++ ++/* Symbol type - ELFNN_ST_TYPE - st_info */ ++#define STT_NOTYPE 0 /* Unspecified type. */ ++#define STT_OBJECT 1 /* Data object. */ ++#define STT_FUNC 2 /* Function. */ ++#define STT_SECTION 3 /* Section. */ ++#define STT_FILE 4 /* Source file. */ ++#define STT_COMMON 5 /* Uninitialized common block. */ ++#define STT_TLS 6 /* TLS object. */ ++#define STT_NUM 7 ++#define STT_LOOS 10 /* Reserved range for operating system */ ++#define STT_GNU_IFUNC 10 ++#define STT_HIOS 12 /* specific semantics. */ ++#define STT_LOPROC 13 /* reserved range for processor */ ++#define STT_HIPROC 15 /* specific semantics. */ ++ ++/* Symbol visibility - ELFNN_ST_VISIBILITY - st_other */ ++#define STV_DEFAULT 0x0 /* Default visibility (see binding). */ ++#define STV_INTERNAL 0x1 /* Special meaning in relocatable objects. */ ++#define STV_HIDDEN 0x2 /* Not visible. */ ++#define STV_PROTECTED 0x3 /* Visible but not preemptible. */ ++#define STV_EXPORTED 0x4 ++#define STV_SINGLETON 0x5 ++#define STV_ELIMINATE 0x6 ++ ++/* Special symbol table indexes. */ ++#define STN_UNDEF 0 /* Undefined symbol index. */ ++ ++/* Symbol versioning flags. */ ++#define VER_DEF_CURRENT 1 ++#define VER_DEF_IDX(x) VER_NDX(x) ++ ++#define VER_FLG_BASE 0x01 ++#define VER_FLG_WEAK 0x02 ++ ++#define VER_NEED_CURRENT 1 ++#define VER_NEED_WEAK (1u << 15) ++#define VER_NEED_HIDDEN VER_NDX_HIDDEN ++#define VER_NEED_IDX(x) VER_NDX(x) ++ ++#define VER_NDX_LOCAL 0 ++#define VER_NDX_GLOBAL 1 ++#define VER_NDX_GIVEN 2 ++ ++#define VER_NDX_HIDDEN (1u << 15) ++#define VER_NDX(x) ((x) & ~(1u << 15)) ++ ++#define CA_SUNW_NULL 0 ++#define CA_SUNW_HW_1 1 /* first hardware capabilities entry */ ++#define CA_SUNW_SF_1 2 /* first software capabilities entry */ ++ ++/* ++ * Syminfo flag values ++ */ ++#define SYMINFO_FLG_DIRECT 0x0001 /* symbol ref has direct association */ ++ /* to object containing defn. */ ++#define SYMINFO_FLG_PASSTHRU 0x0002 /* ignored - see SYMINFO_FLG_FILTER */ ++#define SYMINFO_FLG_COPY 0x0004 /* symbol is a copy-reloc */ ++#define SYMINFO_FLG_LAZYLOAD 0x0008 /* object containing defn should be */ ++ /* lazily-loaded */ ++#define SYMINFO_FLG_DIRECTBIND 0x0010 /* ref should be bound directly to */ ++ /* object containing defn. */ ++#define SYMINFO_FLG_NOEXTDIRECT 0x0020 /* don't let an external reference */ ++ /* directly bind to this symbol */ ++#define SYMINFO_FLG_FILTER 0x0002 /* symbol ref is associated to a */ ++#define SYMINFO_FLG_AUXILIARY 0x0040 /* standard or auxiliary filter */ ++ ++/* ++ * Syminfo.si_boundto values. ++ */ ++#define SYMINFO_BT_SELF 0xffff /* symbol bound to self */ ++#define SYMINFO_BT_PARENT 0xfffe /* symbol bound to parent */ ++#define SYMINFO_BT_NONE 0xfffd /* no special symbol binding */ ++#define SYMINFO_BT_EXTERN 0xfffc /* symbol defined as external */ ++#define SYMINFO_BT_LOWRESERVE 0xff00 /* beginning of reserved entries */ ++ ++/* ++ * Syminfo version values. ++ */ ++#define SYMINFO_NONE 0 /* Syminfo version */ ++#define SYMINFO_CURRENT 1 ++#define SYMINFO_NUM 2 ++ ++/* ++ * Relocation types. ++ * ++ * All machine architectures are defined here to allow tools on one to ++ * handle others. ++ */ ++ ++#define R_386_NONE 0 /* No relocation. */ ++#define R_386_32 1 /* Add symbol value. */ ++#define R_386_PC32 2 /* Add PC-relative symbol value. */ ++#define R_386_GOT32 3 /* Add PC-relative GOT offset. */ ++#define R_386_PLT32 4 /* Add PC-relative PLT offset. */ ++#define R_386_COPY 5 /* Copy data from shared object. */ ++#define R_386_GLOB_DAT 6 /* Set GOT entry to data address. */ ++#define R_386_JMP_SLOT 7 /* Set GOT entry to code address. */ ++#define R_386_RELATIVE 8 /* Add load address of shared object. */ ++#define R_386_GOTOFF 9 /* Add GOT-relative symbol address. */ ++#define R_386_GOTPC 10 /* Add PC-relative GOT table address. */ ++#define R_386_TLS_TPOFF 14 /* Negative offset in static TLS block */ ++#define R_386_TLS_IE 15 /* Absolute address of GOT for -ve static TLS */ ++#define R_386_TLS_GOTIE 16 /* GOT entry for negative static TLS block */ ++#define R_386_TLS_LE 17 /* Negative offset relative to static TLS */ ++#define R_386_TLS_GD 18 /* 32 bit offset to GOT (index,off) pair */ ++#define R_386_TLS_LDM 19 /* 32 bit offset to GOT (index,zero) pair */ ++#define R_386_TLS_GD_32 24 /* 32 bit offset to GOT (index,off) pair */ ++#define R_386_TLS_GD_PUSH 25 /* pushl instruction for Sun ABI GD sequence */ ++#define R_386_TLS_GD_CALL 26 /* call instruction for Sun ABI GD sequence */ ++#define R_386_TLS_GD_POP 27 /* popl instruction for Sun ABI GD sequence */ ++#define R_386_TLS_LDM_32 28 /* 32 bit offset to GOT (index,zero) pair */ ++#define R_386_TLS_LDM_PUSH 29 /* pushl instruction for Sun ABI LD sequence */ ++#define R_386_TLS_LDM_CALL 30 /* call instruction for Sun ABI LD sequence */ ++#define R_386_TLS_LDM_POP 31 /* popl instruction for Sun ABI LD sequence */ ++#define R_386_TLS_LDO_32 32 /* 32 bit offset from start of TLS block */ ++#define R_386_TLS_IE_32 33 /* 32 bit offset to GOT static TLS offset entry */ ++#define R_386_TLS_LE_32 34 /* 32 bit offset within static TLS block */ ++#define R_386_TLS_DTPMOD32 35 /* GOT entry containing TLS index */ ++#define R_386_TLS_DTPOFF32 36 /* GOT entry containing TLS offset */ ++#define R_386_TLS_TPOFF32 37 /* GOT entry of -ve static TLS offset */ ++#define R_386_IRELATIVE 42 /* PLT entry resolved indirectly at runtime */ ++ ++#define R_AARCH64_ABS64 257 ++#define R_AARCH64_GLOB_DAT 1025 /* Set GOT entry to data address. */ ++#define R_AARCH64_JUMP_SLOT 1026 /* Set GOT entry to code address. */ ++#define R_AARCH64_RELATIVE 1027 ++ ++#define R_ARM_NONE 0 /* No relocation. */ ++#define R_ARM_PC24 1 ++#define R_ARM_ABS32 2 ++#define R_ARM_REL32 3 ++#define R_ARM_PC13 4 ++#define R_ARM_ABS16 5 ++#define R_ARM_ABS12 6 ++#define R_ARM_THM_ABS5 7 ++#define R_ARM_ABS8 8 ++#define R_ARM_SBREL32 9 ++#define R_ARM_THM_PC22 10 ++#define R_ARM_THM_PC8 11 ++#define R_ARM_AMP_VCALL9 12 ++#define R_ARM_SWI24 13 ++#define R_ARM_THM_SWI8 14 ++#define R_ARM_XPC25 15 ++#define R_ARM_THM_XPC22 16 ++/* TLS relocations */ ++#define R_ARM_TLS_DTPMOD32 17 /* ID of module containing symbol */ ++#define R_ARM_TLS_DTPOFF32 18 /* Offset in TLS block */ ++#define R_ARM_TLS_TPOFF32 19 /* Offset in static TLS block */ ++#define R_ARM_COPY 20 /* Copy data from shared object. */ ++#define R_ARM_GLOB_DAT 21 /* Set GOT entry to data address. */ ++#define R_ARM_JUMP_SLOT 22 /* Set GOT entry to code address. */ ++#define R_ARM_RELATIVE 23 /* Add load address of shared object. */ ++#define R_ARM_GOTOFF 24 /* Add GOT-relative symbol address. */ ++#define R_ARM_GOTPC 25 /* Add PC-relative GOT table address. */ ++#define R_ARM_GOT32 26 /* Add PC-relative GOT offset. */ ++#define R_ARM_PLT32 27 /* Add PC-relative PLT offset. */ ++#define R_ARM_GNU_VTENTRY 100 ++#define R_ARM_GNU_VTINHERIT 101 ++#define R_ARM_RSBREL32 250 ++#define R_ARM_THM_RPC22 251 ++#define R_ARM_RREL32 252 ++#define R_ARM_RABS32 253 ++#define R_ARM_RPC24 254 ++#define R_ARM_RBASE 255 ++ ++/* Name Value Field Calculation */ ++#define R_IA_64_NONE 0 /* None */ ++#define R_IA_64_IMM14 0x21 /* immediate14 S + A */ ++#define R_IA_64_IMM22 0x22 /* immediate22 S + A */ ++#define R_IA_64_IMM64 0x23 /* immediate64 S + A */ ++#define R_IA_64_DIR32MSB 0x24 /* word32 MSB S + A */ ++#define R_IA_64_DIR32LSB 0x25 /* word32 LSB S + A */ ++#define R_IA_64_DIR64MSB 0x26 /* word64 MSB S + A */ ++#define R_IA_64_DIR64LSB 0x27 /* word64 LSB S + A */ ++#define R_IA_64_GPREL22 0x2a /* immediate22 @gprel(S + A) */ ++#define R_IA_64_GPREL64I 0x2b /* immediate64 @gprel(S + A) */ ++#define R_IA_64_GPREL32MSB 0x2c /* word32 MSB @gprel(S + A) */ ++#define R_IA_64_GPREL32LSB 0x2d /* word32 LSB @gprel(S + A) */ ++#define R_IA_64_GPREL64MSB 0x2e /* word64 MSB @gprel(S + A) */ ++#define R_IA_64_GPREL64LSB 0x2f /* word64 LSB @gprel(S + A) */ ++#define R_IA_64_LTOFF22 0x32 /* immediate22 @ltoff(S + A) */ ++#define R_IA_64_LTOFF64I 0x33 /* immediate64 @ltoff(S + A) */ ++#define R_IA_64_PLTOFF22 0x3a /* immediate22 @pltoff(S + A) */ ++#define R_IA_64_PLTOFF64I 0x3b /* immediate64 @pltoff(S + A) */ ++#define R_IA_64_PLTOFF64MSB 0x3e /* word64 MSB @pltoff(S + A) */ ++#define R_IA_64_PLTOFF64LSB 0x3f /* word64 LSB @pltoff(S + A) */ ++#define R_IA_64_FPTR64I 0x43 /* immediate64 @fptr(S + A) */ ++#define R_IA_64_FPTR32MSB 0x44 /* word32 MSB @fptr(S + A) */ ++#define R_IA_64_FPTR32LSB 0x45 /* word32 LSB @fptr(S + A) */ ++#define R_IA_64_FPTR64MSB 0x46 /* word64 MSB @fptr(S + A) */ ++#define R_IA_64_FPTR64LSB 0x47 /* word64 LSB @fptr(S + A) */ ++#define R_IA_64_PCREL60B 0x48 /* immediate60 form1 S + A - P */ ++#define R_IA_64_PCREL21B 0x49 /* immediate21 form1 S + A - P */ ++#define R_IA_64_PCREL21M 0x4a /* immediate21 form2 S + A - P */ ++#define R_IA_64_PCREL21F 0x4b /* immediate21 form3 S + A - P */ ++#define R_IA_64_PCREL32MSB 0x4c /* word32 MSB S + A - P */ ++#define R_IA_64_PCREL32LSB 0x4d /* word32 LSB S + A - P */ ++#define R_IA_64_PCREL64MSB 0x4e /* word64 MSB S + A - P */ ++#define R_IA_64_PCREL64LSB 0x4f /* word64 LSB S + A - P */ ++#define R_IA_64_LTOFF_FPTR22 0x52 /* immediate22 @ltoff(@fptr(S + A)) */ ++#define R_IA_64_LTOFF_FPTR64I 0x53 /* immediate64 @ltoff(@fptr(S + A)) */ ++#define R_IA_64_LTOFF_FPTR32MSB 0x54 /* word32 MSB @ltoff(@fptr(S + A)) */ ++#define R_IA_64_LTOFF_FPTR32LSB 0x55 /* word32 LSB @ltoff(@fptr(S + A)) */ ++#define R_IA_64_LTOFF_FPTR64MSB 0x56 /* word64 MSB @ltoff(@fptr(S + A)) */ ++#define R_IA_64_LTOFF_FPTR64LSB 0x57 /* word64 LSB @ltoff(@fptr(S + A)) */ ++#define R_IA_64_SEGREL32MSB 0x5c /* word32 MSB @segrel(S + A) */ ++#define R_IA_64_SEGREL32LSB 0x5d /* word32 LSB @segrel(S + A) */ ++#define R_IA_64_SEGREL64MSB 0x5e /* word64 MSB @segrel(S + A) */ ++#define R_IA_64_SEGREL64LSB 0x5f /* word64 LSB @segrel(S + A) */ ++#define R_IA_64_SECREL32MSB 0x64 /* word32 MSB @secrel(S + A) */ ++#define R_IA_64_SECREL32LSB 0x65 /* word32 LSB @secrel(S + A) */ ++#define R_IA_64_SECREL64MSB 0x66 /* word64 MSB @secrel(S + A) */ ++#define R_IA_64_SECREL64LSB 0x67 /* word64 LSB @secrel(S + A) */ ++#define R_IA_64_REL32MSB 0x6c /* word32 MSB BD + A */ ++#define R_IA_64_REL32LSB 0x6d /* word32 LSB BD + A */ ++#define R_IA_64_REL64MSB 0x6e /* word64 MSB BD + A */ ++#define R_IA_64_REL64LSB 0x6f /* word64 LSB BD + A */ ++#define R_IA_64_LTV32MSB 0x74 /* word32 MSB S + A */ ++#define R_IA_64_LTV32LSB 0x75 /* word32 LSB S + A */ ++#define R_IA_64_LTV64MSB 0x76 /* word64 MSB S + A */ ++#define R_IA_64_LTV64LSB 0x77 /* word64 LSB S + A */ ++#define R_IA_64_PCREL21BI 0x79 /* immediate21 form1 S + A - P */ ++#define R_IA_64_PCREL22 0x7a /* immediate22 S + A - P */ ++#define R_IA_64_PCREL64I 0x7b /* immediate64 S + A - P */ ++#define R_IA_64_IPLTMSB 0x80 /* function descriptor MSB special */ ++#define R_IA_64_IPLTLSB 0x81 /* function descriptor LSB speciaal */ ++#define R_IA_64_SUB 0x85 /* immediate64 A - S */ ++#define R_IA_64_LTOFF22X 0x86 /* immediate22 special */ ++#define R_IA_64_LDXMOV 0x87 /* immediate22 special */ ++#define R_IA_64_TPREL14 0x91 /* imm14 @tprel(S + A) */ ++#define R_IA_64_TPREL22 0x92 /* imm22 @tprel(S + A) */ ++#define R_IA_64_TPREL64I 0x93 /* imm64 @tprel(S + A) */ ++#define R_IA_64_TPREL64MSB 0x96 /* word64 MSB @tprel(S + A) */ ++#define R_IA_64_TPREL64LSB 0x97 /* word64 LSB @tprel(S + A) */ ++#define R_IA_64_LTOFF_TPREL22 0x9a /* imm22 @ltoff(@tprel(S+A)) */ ++#define R_IA_64_DTPMOD64MSB 0xa6 /* word64 MSB @dtpmod(S + A) */ ++#define R_IA_64_DTPMOD64LSB 0xa7 /* word64 LSB @dtpmod(S + A) */ ++#define R_IA_64_LTOFF_DTPMOD22 0xaa /* imm22 @ltoff(@dtpmod(S+A)) */ ++#define R_IA_64_DTPREL14 0xb1 /* imm14 @dtprel(S + A) */ ++#define R_IA_64_DTPREL22 0xb2 /* imm22 @dtprel(S + A) */ ++#define R_IA_64_DTPREL64I 0xb3 /* imm64 @dtprel(S + A) */ ++#define R_IA_64_DTPREL32MSB 0xb4 /* word32 MSB @dtprel(S + A) */ ++#define R_IA_64_DTPREL32LSB 0xb5 /* word32 LSB @dtprel(S + A) */ ++#define R_IA_64_DTPREL64MSB 0xb6 /* word64 MSB @dtprel(S + A) */ ++#define R_IA_64_DTPREL64LSB 0xb7 /* word64 LSB @dtprel(S + A) */ ++#define R_IA_64_LTOFF_DTPREL22 0xba /* imm22 @ltoff(@dtprel(S+A)) */ ++ ++#define R_MIPS_NONE 0 /* No reloc */ ++#define R_MIPS_16 1 /* Direct 16 bit */ ++#define R_MIPS_32 2 /* Direct 32 bit */ ++#define R_MIPS_REL32 3 /* PC relative 32 bit */ ++#define R_MIPS_26 4 /* Direct 26 bit shifted */ ++#define R_MIPS_HI16 5 /* High 16 bit */ ++#define R_MIPS_LO16 6 /* Low 16 bit */ ++#define R_MIPS_GPREL16 7 /* GP relative 16 bit */ ++#define R_MIPS_LITERAL 8 /* 16 bit literal entry */ ++#define R_MIPS_GOT16 9 /* 16 bit GOT entry */ ++#define R_MIPS_PC16 10 /* PC relative 16 bit */ ++#define R_MIPS_CALL16 11 /* 16 bit GOT entry for function */ ++#define R_MIPS_GPREL32 12 /* GP relative 32 bit */ ++#define R_MIPS_64 18 /* Direct 64 bit */ ++#define R_MIPS_GOTHI16 21 /* GOT HI 16 bit */ ++#define R_MIPS_GOTLO16 22 /* GOT LO 16 bit */ ++#define R_MIPS_CALLHI16 30 /* upper 16 bit GOT entry for function */ ++#define R_MIPS_CALLLO16 31 /* lower 16 bit GOT entry for function */ ++ ++#define R_PPC_NONE 0 /* No relocation. */ ++#define R_PPC_ADDR32 1 ++#define R_PPC_ADDR24 2 ++#define R_PPC_ADDR16 3 ++#define R_PPC_ADDR16_LO 4 ++#define R_PPC_ADDR16_HI 5 ++#define R_PPC_ADDR16_HA 6 ++#define R_PPC_ADDR14 7 ++#define R_PPC_ADDR14_BRTAKEN 8 ++#define R_PPC_ADDR14_BRNTAKEN 9 ++#define R_PPC_REL24 10 ++#define R_PPC_REL14 11 ++#define R_PPC_REL14_BRTAKEN 12 ++#define R_PPC_REL14_BRNTAKEN 13 ++#define R_PPC_GOT16 14 ++#define R_PPC_GOT16_LO 15 ++#define R_PPC_GOT16_HI 16 ++#define R_PPC_GOT16_HA 17 ++#define R_PPC_PLTREL24 18 ++#define R_PPC_COPY 19 ++#define R_PPC_GLOB_DAT 20 ++#define R_PPC_JMP_SLOT 21 ++#define R_PPC_RELATIVE 22 ++#define R_PPC_LOCAL24PC 23 ++#define R_PPC_UADDR32 24 ++#define R_PPC_UADDR16 25 ++#define R_PPC_REL32 26 ++#define R_PPC_PLT32 27 ++#define R_PPC_PLTREL32 28 ++#define R_PPC_PLT16_LO 29 ++#define R_PPC_PLT16_HI 30 ++#define R_PPC_PLT16_HA 31 ++#define R_PPC_SDAREL16 32 ++#define R_PPC_SECTOFF 33 ++#define R_PPC_SECTOFF_LO 34 ++#define R_PPC_SECTOFF_HI 35 ++#define R_PPC_SECTOFF_HA 36 ++ ++/* ++ * 64-bit relocations ++ */ ++#define R_PPC64_ADDR64 38 ++#define R_PPC64_ADDR16_HIGHER 39 ++#define R_PPC64_ADDR16_HIGHERA 40 ++#define R_PPC64_ADDR16_HIGHEST 41 ++#define R_PPC64_ADDR16_HIGHESTA 42 ++#define R_PPC64_UADDR64 43 ++#define R_PPC64_REL64 44 ++#define R_PPC64_PLT64 45 ++#define R_PPC64_PLTREL64 46 ++#define R_PPC64_TOC16 47 ++#define R_PPC64_TOC16_LO 48 ++#define R_PPC64_TOC16_HI 49 ++#define R_PPC64_TOC16_HA 50 ++#define R_PPC64_TOC 51 ++#define R_PPC64_DTPMOD64 68 ++#define R_PPC64_TPREL64 73 ++#define R_PPC64_DTPREL64 78 ++ ++/* ++ * TLS relocations ++ */ ++#define R_PPC_TLS 67 ++#define R_PPC_DTPMOD32 68 ++#define R_PPC_TPREL16 69 ++#define R_PPC_TPREL16_LO 70 ++#define R_PPC_TPREL16_HI 71 ++#define R_PPC_TPREL16_HA 72 ++#define R_PPC_TPREL32 73 ++#define R_PPC_DTPREL16 74 ++#define R_PPC_DTPREL16_LO 75 ++#define R_PPC_DTPREL16_HI 76 ++#define R_PPC_DTPREL16_HA 77 ++#define R_PPC_DTPREL32 78 ++#define R_PPC_GOT_TLSGD16 79 ++#define R_PPC_GOT_TLSGD16_LO 80 ++#define R_PPC_GOT_TLSGD16_HI 81 ++#define R_PPC_GOT_TLSGD16_HA 82 ++#define R_PPC_GOT_TLSLD16 83 ++#define R_PPC_GOT_TLSLD16_LO 84 ++#define R_PPC_GOT_TLSLD16_HI 85 ++#define R_PPC_GOT_TLSLD16_HA 86 ++#define R_PPC_GOT_TPREL16 87 ++#define R_PPC_GOT_TPREL16_LO 88 ++#define R_PPC_GOT_TPREL16_HI 89 ++#define R_PPC_GOT_TPREL16_HA 90 ++ ++/* ++ * The remaining relocs are from the Embedded ELF ABI, and are not in the ++ * SVR4 ELF ABI. ++ */ ++ ++#define R_PPC_EMB_NADDR32 101 ++#define R_PPC_EMB_NADDR16 102 ++#define R_PPC_EMB_NADDR16_LO 103 ++#define R_PPC_EMB_NADDR16_HI 104 ++#define R_PPC_EMB_NADDR16_HA 105 ++#define R_PPC_EMB_SDAI16 106 ++#define R_PPC_EMB_SDA2I16 107 ++#define R_PPC_EMB_SDA2REL 108 ++#define R_PPC_EMB_SDA21 109 ++#define R_PPC_EMB_MRKREF 110 ++#define R_PPC_EMB_RELSEC16 111 ++#define R_PPC_EMB_RELST_LO 112 ++#define R_PPC_EMB_RELST_HI 113 ++#define R_PPC_EMB_RELST_HA 114 ++#define R_PPC_EMB_BIT_FLD 115 ++#define R_PPC_EMB_RELSDA 116 ++ ++#define R_SPARC_NONE 0 ++#define R_SPARC_8 1 ++#define R_SPARC_16 2 ++#define R_SPARC_32 3 ++#define R_SPARC_DISP8 4 ++#define R_SPARC_DISP16 5 ++#define R_SPARC_DISP32 6 ++#define R_SPARC_WDISP30 7 ++#define R_SPARC_WDISP22 8 ++#define R_SPARC_HI22 9 ++#define R_SPARC_22 10 ++#define R_SPARC_13 11 ++#define R_SPARC_LO10 12 ++#define R_SPARC_GOT10 13 ++#define R_SPARC_GOT13 14 ++#define R_SPARC_GOT22 15 ++#define R_SPARC_PC10 16 ++#define R_SPARC_PC22 17 ++#define R_SPARC_WPLT30 18 ++#define R_SPARC_COPY 19 ++#define R_SPARC_GLOB_DAT 20 ++#define R_SPARC_JMP_SLOT 21 ++#define R_SPARC_RELATIVE 22 ++#define R_SPARC_UA32 23 ++#define R_SPARC_PLT32 24 ++#define R_SPARC_HIPLT22 25 ++#define R_SPARC_LOPLT10 26 ++#define R_SPARC_PCPLT32 27 ++#define R_SPARC_PCPLT22 28 ++#define R_SPARC_PCPLT10 29 ++#define R_SPARC_10 30 ++#define R_SPARC_11 31 ++#define R_SPARC_64 32 ++#define R_SPARC_OLO10 33 ++#define R_SPARC_HH22 34 ++#define R_SPARC_HM10 35 ++#define R_SPARC_LM22 36 ++#define R_SPARC_PC_HH22 37 ++#define R_SPARC_PC_HM10 38 ++#define R_SPARC_PC_LM22 39 ++#define R_SPARC_WDISP16 40 ++#define R_SPARC_WDISP19 41 ++#define R_SPARC_GLOB_JMP 42 ++#define R_SPARC_7 43 ++#define R_SPARC_5 44 ++#define R_SPARC_6 45 ++#define R_SPARC_DISP64 46 ++#define R_SPARC_PLT64 47 ++#define R_SPARC_HIX22 48 ++#define R_SPARC_LOX10 49 ++#define R_SPARC_H44 50 ++#define R_SPARC_M44 51 ++#define R_SPARC_L44 52 ++#define R_SPARC_REGISTER 53 ++#define R_SPARC_UA64 54 ++#define R_SPARC_UA16 55 ++#define R_SPARC_TLS_GD_HI22 56 ++#define R_SPARC_TLS_GD_LO10 57 ++#define R_SPARC_TLS_GD_ADD 58 ++#define R_SPARC_TLS_GD_CALL 59 ++#define R_SPARC_TLS_LDM_HI22 60 ++#define R_SPARC_TLS_LDM_LO10 61 ++#define R_SPARC_TLS_LDM_ADD 62 ++#define R_SPARC_TLS_LDM_CALL 63 ++#define R_SPARC_TLS_LDO_HIX22 64 ++#define R_SPARC_TLS_LDO_LOX10 65 ++#define R_SPARC_TLS_LDO_ADD 66 ++#define R_SPARC_TLS_IE_HI22 67 ++#define R_SPARC_TLS_IE_LO10 68 ++#define R_SPARC_TLS_IE_LD 69 ++#define R_SPARC_TLS_IE_LDX 70 ++#define R_SPARC_TLS_IE_ADD 71 ++#define R_SPARC_TLS_LE_HIX22 72 ++#define R_SPARC_TLS_LE_LOX10 73 ++#define R_SPARC_TLS_DTPMOD32 74 ++#define R_SPARC_TLS_DTPMOD64 75 ++#define R_SPARC_TLS_DTPOFF32 76 ++#define R_SPARC_TLS_DTPOFF64 77 ++#define R_SPARC_TLS_TPOFF32 78 ++#define R_SPARC_TLS_TPOFF64 79 ++ ++#define R_X86_64_NONE 0 /* No relocation. */ ++#define R_X86_64_64 1 /* Add 64 bit symbol value. */ ++#define R_X86_64_PC32 2 /* PC-relative 32 bit signed sym value. */ ++#define R_X86_64_GOT32 3 /* PC-relative 32 bit GOT offset. */ ++#define R_X86_64_PLT32 4 /* PC-relative 32 bit PLT offset. */ ++#define R_X86_64_COPY 5 /* Copy data from shared object. */ ++#define R_X86_64_GLOB_DAT 6 /* Set GOT entry to data address. */ ++#define R_X86_64_JMP_SLOT 7 /* Set GOT entry to code address. */ ++#define R_X86_64_RELATIVE 8 /* Add load address of shared object. */ ++#define R_X86_64_GOTPCREL 9 /* Add 32 bit signed pcrel offset to GOT. */ ++#define R_X86_64_32 10 /* Add 32 bit zero extended symbol value */ ++#define R_X86_64_32S 11 /* Add 32 bit sign extended symbol value */ ++#define R_X86_64_16 12 /* Add 16 bit zero extended symbol value */ ++#define R_X86_64_PC16 13 /* Add 16 bit signed extended pc relative symbol value */ ++#define R_X86_64_8 14 /* Add 8 bit zero extended symbol value */ ++#define R_X86_64_PC8 15 /* Add 8 bit signed extended pc relative symbol value */ ++#define R_X86_64_DTPMOD64 16 /* ID of module containing symbol */ ++#define R_X86_64_DTPOFF64 17 /* Offset in TLS block */ ++#define R_X86_64_TPOFF64 18 /* Offset in static TLS block */ ++#define R_X86_64_TLSGD 19 /* PC relative offset to GD GOT entry */ ++#define R_X86_64_TLSLD 20 /* PC relative offset to LD GOT entry */ ++#define R_X86_64_DTPOFF32 21 /* Offset in TLS block */ ++#define R_X86_64_GOTTPOFF 22 /* PC relative offset to IE GOT entry */ ++#define R_X86_64_TPOFF32 23 /* Offset in static TLS block */ ++#define R_X86_64_IRELATIVE 37 ++ ++#endif /* !_SYS_ELF_COMMON_H_ */ +diff --git a/ta/remoteproc/include/elf_parser.h b/ta/remoteproc/include/elf_parser.h +new file mode 100644 +index 000000000..5950db971 +--- /dev/null ++++ b/ta/remoteproc/include/elf_parser.h +@@ -0,0 +1,59 @@ ++/* SPDX-License-Identifier: BSD-2-Clause */ ++/* ++ * Copyright (C) 2020, STMicroelectronics - All Rights Reserved ++ */ ++ ++#ifndef ELF_PARSER ++#define ELF_PARSER ++ ++#include ++#include ++#include ++ ++/** ++ * struct resource_table - firmware resource table header ++ * @ver: version number ++ * @num: number of resource entries ++ * @reserved: reserved (must be zero) ++ * @offset: array of offsets pointing at the various resource entries ++ * ++ * A resource table is essentially a list of system resources required ++ * by the remote processor. It may also include configuration entries. ++ * If needed, the remote processor firmware should contain this table ++ * as a dedicated ".resource_table" ELF section. ++ * ++ * This structure shall be consistent with the Linux kernel structure ++ * definition from include/linux/remoteproc.h. ++ */ ++struct resource_table { ++ uint32_t ver; ++ uint32_t num; ++ uint32_t reserved[2]; ++ uint32_t offset[]; ++} __packed; ++ ++struct fw_elf32 { ++ uintptr_t e_entry; ++ uintptr_t e_phoff; ++ uintptr_t e_shoff; ++ uint32_t e_phnum; ++ uint32_t e_shnum; ++ uint32_t e_phentsize; ++ uint32_t e_shentsize; ++ ++ Elf32_Phdr *phdr; ++ Elf32_Shdr *shdr; ++}; ++ ++TEE_Result e32_parse_ehdr(uint8_t *fw, size_t size); ++TEE_Result e32_parser_load_elf_image(uint8_t *fw, size_t fw_size, ++ TEE_Result (*load_seg)(uint8_t *src, ++ uint32_t size, ++ uint32_t da, ++ uint32_t mem_size, ++ void *priv), ++ void *priv_data); ++int e32_parser_find_rsc_table(uint8_t *fw, size_t fw_size, Elf32_Addr *rsc_addr, ++ Elf32_Word *rsc_size); ++ ++#endif /*ELF_PARSER*/ +diff --git a/ta/remoteproc/include/ta_remoteproc.h b/ta/remoteproc/include/ta_remoteproc.h +new file mode 100644 +index 000000000..a51ba5f70 +--- /dev/null ++++ b/ta/remoteproc/include/ta_remoteproc.h +@@ -0,0 +1,65 @@ ++/* SPDX-License-Identifier: BSD-2-Clause */ ++/* ++ * Copyright (C) 2020, STMicroelectronics - All Rights Reserved ++ */ ++ ++#ifndef TA_RPROC_FW_H ++#define TA_RPROC_FW_H ++ ++/* ++ * This UUID is generated with uuidgen ++ * the ITU-T UUID generator at http://www.itu.int/ITU-T/asn1/uuid.html ++ */ ++#define TA_REMOTEPROC_UUID \ ++ { 0x80a4c275, 0x0a47, 0x4905, \ ++ { 0x82, 0x85, 0x14, 0x86, 0xa9, 0x77, 0x1a, 0x08} } ++ ++/* The function IDs implemented in this TA */ ++ ++/* ++ * Authentication of the firmware and load in the remote processor memory. ++ * ++ * [in] params[0].value.a: unique 32bit identifier of the firmware ++ * [in] params[1].memref: buffer containing the image of the firmware ++ */ ++#define TA_RPROC_FW_CMD_LOAD_FW 1 ++ ++/* ++ * Start the remote processor. ++ * ++ * [in] params[0].value.a: unique 32bit identifier of the firmware ++ */ ++#define TA_RPROC_FW_CMD_START_FW 2 ++ ++/* ++ * Stop the remote processor. ++ * ++ * [in] params[0].value.a: unique 32bit identifier of the firmware ++ */ ++#define TA_RPROC_FW_CMD_STOP_FW 3 ++ ++/* ++ * Return the physical address of the resource table, or 0 if not found ++ * No check is done to verify that the address returned is accessible by the ++ * non secure world. If the resource table is loaded in a protected memory, ++ * then accesses from non-secure world will likely fail. ++ * ++ * [in] params[0].value.a: unique 32bit identifier of the firmware ++ * [out] params[1].value.a: 32bit LSB resource table memory address ++ * [out] params[1].value.b: 32bit MSB resource table memory address ++ * [out] params[2].value.a: 32bit LSB resource table memory size ++ * [out] params[2].value.b: 32bit MSB resource table memory size ++ */ ++#define TA_RPROC_FW_CMD_GET_RSC_TABLE 4 ++ ++/* ++ * Get remote processor firmware core dump. If found, return either ++ * TEE_SUCCESS on successful completion or TEE_ERROR_SHORT_BUFFER if output ++ * buffer is too short to store the core dump. ++ * ++ * [in] params[0].value.a: unique 32bit identifier of the firmware ++ * [out] params[1].memref: Core dump, if found ++ */ ++#define TA_RPROC_FW_CMD_GET_COREDUMP 5 ++ ++#endif /*TA_RPROC_FW_H*/ +diff --git a/ta/remoteproc/include/user_ta_header_defines.h b/ta/remoteproc/include/user_ta_header_defines.h +new file mode 100644 +index 000000000..c2ef1b21b +--- /dev/null ++++ b/ta/remoteproc/include/user_ta_header_defines.h +@@ -0,0 +1,29 @@ ++/* SPDX-License-Identifier: BSD-2-Clause */ ++/* ++ * Copyright (C) 2020, STMicroelectronics - All Rights Reserved ++ */ ++ ++#ifndef USER_TA_HEADER_DEFINES_H ++#define USER_TA_HEADER_DEFINES_H ++ ++#include ++ ++#define TA_UUID TA_REMOTEPROC_UUID ++ ++#define TA_FLAGS (TA_FLAG_DEVICE_ENUM | \ ++ TA_FLAG_SINGLE_INSTANCE | \ ++ TA_FLAG_INSTANCE_KEEP_ALIVE) ++ ++/* Provisioned stack size */ ++#define TA_STACK_SIZE (4 * 1024) ++ ++/* Provisioned heap size for TEE_Malloc() and friends */ ++#define TA_DATA_SIZE (4 * 1024) ++ ++/* The gpd.ta.version property */ ++#define TA_VERSION "1.0" ++ ++/* The gpd.ta.description property */ ++#define TA_DESCRIPTION "remote processor firmware management" ++ ++#endif /* USER_TA_HEADER_DEFINES_H */ +diff --git a/ta/remoteproc/remoteproc_core.c b/ta/remoteproc/remoteproc_core.c +new file mode 100644 +index 000000000..454edf2fb +--- /dev/null ++++ b/ta/remoteproc/remoteproc_core.c +@@ -0,0 +1,781 @@ ++ // SPDX-License-Identifier: BSD-2-Clause ++ /* ++ * Copyright (C) 2020-2021, STMicroelectronics - All Rights Reserved ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++/* Firmware state */ ++enum remoteproc_state { ++ REMOTEPROC_OFF, ++ REMOTEPROC_LOADED, ++ REMOTEPROC_STARTED, ++}; ++ ++#define RPROC_HDR_MAGIC 0x3543A468 /*random value */ ++#define HEADER_VERSION 1 ++ ++/* Supported signature algorithm */ ++enum remoteproc_sign_type { ++ RPROC_RSASSA_PKCS1_v1_5_SHA256 = 1, ++ RPROC_ECDSA_SHA256 = 2, ++}; ++ ++/* ++ * struct remoteproc_segment - program header with hash structure ++ * @phdr: program header ++ * @hash: hash associated to the program segment. ++ */ ++struct remoteproc_segment { ++ Elf32_Phdr phdr; ++ unsigned char hash[TEE_SHA256_HASH_SIZE]; ++}; ++ ++/* ++ * struct remoteproc_fw_hdr - firmware header ++ * @magic: Magic number, must be equal to RPROC_HDR_MAGIC ++ * @version: Version of the header (must be 1) ++ * @hdr_length: Total header byte length including chunks ++ * @sign_length: Signature chunk byte length ++ * @sign_offset: Signature chunk byte offset from header start ++ * @sign_type: Signature type ++ * @phhdr_length: Program header with hashes byte size, possibly 0 ++ * @phhdr_offset: Program header with hashes byte offset, 0 if not used ++ * @phhdr_type: Program header with hash type or 0 if not used ++ * @key_length: Authentication key info byte length, possibly 0 ++ * @key_offset: Authentication key info byte offset, 0 if not used ++ * @img_length: Firmware image chunk byte length ++ * @img_offset: Firmware image chunk byte offset ++ * @img_type: Firmware image type ++ */ ++struct remoteproc_fw_hdr { ++ uint32_t magic; ++ uint32_t version; ++ uint32_t hdr_length; ++ uint32_t sign_length; ++ uint32_t sign_offset; ++ uint32_t sign_type; ++ uint32_t phhdr_length; ++ uint32_t phhdr_offset; ++ uint32_t phhdr_type; ++ uint32_t key_length; ++ uint32_t key_offset; ++ uint32_t img_length; ++ uint32_t img_offset; ++ uint32_t img_type; ++}; ++ ++/* ++ * struct remoteproc_sig_algo - signature algorithm information ++ * @sign_type: Header signature type ++ * @id: Signature algorigthm identifier TEE_ALG_* ++ * @hash_len: Signature hash length ++ */ ++struct remoteproc_sig_algo { ++ enum remoteproc_sign_type sign_type; ++ uint32_t id; ++ size_t hash_len; ++}; ++ ++/* ++ * struct remoteproc_context - firmware context ++ * @fw_id: Unique Id of the firmware ++ * @hdr: Location of a secure copy of the firmware header ++ * @fw_img: Firmware image ++ * @fw_img_size: Byte size of the firmware image ++ * @rsc_pa: Physical address of the firmware resource table ++ * @rsc_size: Byte size of the firmware resource table ++ * @state: Remote-processor state ++ * @hw_fmt: Image format capabilities of the remoteproc PTA ++ * @hw_img_prot: Image protection capabilities of the remoteproc PTA ++ * @link: Linked list element ++ */ ++struct remoteproc_context { ++ uint32_t fw_id; ++ struct remoteproc_fw_hdr *hdr; ++ uint8_t *fw_img; ++ size_t fw_img_size; ++ paddr_t rsc_pa; ++ uint32_t rsc_size; ++ enum remoteproc_state state; ++ uint32_t hw_fmt; ++ uint32_t hw_img_prot; ++ TAILQ_ENTRY(remoteproc_context) link; ++}; ++ ++TAILQ_HEAD(remoteproc_firmware_head, remoteproc_context); ++ ++static struct remoteproc_firmware_head firmware_head = ++ TAILQ_HEAD_INITIALIZER(firmware_head); ++ ++static const struct remoteproc_sig_algo rproc_ta_sign_algo[] = { ++ { ++ .sign_type = RPROC_RSASSA_PKCS1_v1_5_SHA256, ++ .id = TEE_ALG_RSASSA_PKCS1_V1_5_SHA256, ++ .hash_len = TEE_SHA256_HASH_SIZE, ++ }, ++ { ++ .sign_type = RPROC_ECDSA_SHA256, ++ .id = TEE_ALG_ECDSA_P256, ++ .hash_len = TEE_SHA256_HASH_SIZE, ++ }, ++}; ++ ++static size_t session_refcount; ++static TEE_TASessionHandle pta_session; ++ ++static void remoteproc_header_dump(struct remoteproc_fw_hdr __maybe_unused *hdr) ++{ ++ DMSG("magic :\t%#"PRIx32, hdr->magic); ++ DMSG("version :\t%#"PRIx32, hdr->version); ++ DMSG("hdr_length :\t%#"PRIx32, hdr->hdr_length); ++ DMSG("sign_length :\t%#"PRIx32, hdr->sign_length); ++ DMSG("sign_offset :\t%#"PRIx32, hdr->sign_offset); ++ DMSG("sign_type :\t%#"PRIx32, hdr->sign_type); ++ DMSG("phhdr_length :\t%#"PRIx32, hdr->phhdr_length); ++ DMSG("phhdr_offset :\t%#"PRIx32, hdr->phhdr_offset); ++ DMSG("phhdr_type :\t%#"PRIx32, hdr->phhdr_type); ++ DMSG("key_length :\t%#"PRIx32, hdr->key_length); ++ DMSG("key_offset :\t%#"PRIx32, hdr->key_offset); ++ DMSG("img_length :\t%#"PRIx32, hdr->img_length); ++ DMSG("img_offset :\t%#"PRIx32, hdr->img_offset); ++ DMSG("img_type :\t%#"PRIx32, hdr->img_type); ++} ++ ++static struct remoteproc_context *remoteproc_find_firmware(uint32_t fw_id) ++{ ++ struct remoteproc_context *ctx = NULL; ++ ++ TAILQ_FOREACH(ctx, &firmware_head, link) { ++ if (ctx->fw_id == fw_id) ++ return ctx; ++ } ++ ++ return NULL; ++} ++ ++static struct remoteproc_context *remoteproc_add_firmware(uint32_t fw_id) ++{ ++ struct remoteproc_context *ctx = NULL; ++ ++ ctx = TEE_Malloc(sizeof(*ctx), TEE_MALLOC_FILL_ZERO); ++ if (!ctx) ++ return NULL; ++ ++ ctx->fw_id = fw_id; ++ ++ TAILQ_INSERT_TAIL(&firmware_head, ctx, link); ++ ++ return ctx; ++} ++ ++static const struct remoteproc_sig_algo *remoteproc_get_algo(uint32_t sign_type) ++{ ++ unsigned int i = 0; ++ ++ for (i = 0; i < ARRAY_SIZE(rproc_ta_sign_algo); i++) ++ if (sign_type == rproc_ta_sign_algo[i].sign_type) ++ return &rproc_ta_sign_algo[i]; ++ ++ return NULL; ++} ++ ++static TEE_Result remoteproc_pta_verify(struct remoteproc_context *ctx, ++ const struct remoteproc_sig_algo *algo, ++ char *hash, uint32_t hash_len) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ struct remoteproc_fw_hdr *hdr = ctx->hdr; ++ struct rproc_pta_key_info *keyinfo = NULL; ++ uint32_t param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_MEMREF_INPUT, ++ TEE_PARAM_TYPE_MEMREF_INPUT, ++ TEE_PARAM_TYPE_MEMREF_INPUT); ++ TEE_Param params[TEE_NUM_PARAMS] = { }; ++ ++ keyinfo = TEE_Malloc(sizeof(*keyinfo) + hdr->key_length, 0); ++ if (!keyinfo) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ keyinfo->algo = algo->id; ++ keyinfo->info_size = hdr->key_length; ++ memcpy(keyinfo->info, (uint8_t *)hdr + hdr->key_offset, ++ hdr->key_length); ++ ++ params[0].value.a = ctx->fw_id; ++ params[1].memref.buffer = keyinfo; ++ params[1].memref.size = RPROC_PTA_GET_KEYINFO_SIZE(keyinfo); ++ params[2].memref.buffer = hash; ++ params[2].memref.size = hash_len; ++ params[3].memref.buffer = (uint8_t *)hdr + hdr->sign_offset; ++ params[3].memref.size = hdr->sign_length; ++ ++ res = TEE_InvokeTACommand(pta_session, TEE_TIMEOUT_INFINITE, ++ PTA_REMOTEPROC_VERIFY_DIGEST, ++ param_types, params, NULL); ++ if (res != TEE_SUCCESS) ++ EMSG("Failed to verify signature, res = %#"PRIx32, res); ++ ++ TEE_Free(keyinfo); ++ ++ return res; ++} ++ ++static TEE_Result remoteproc_save_fw_header(struct remoteproc_context *ctx, ++ void *fw_orig, ++ uint32_t fw_orig_size) ++{ ++ struct remoteproc_fw_hdr *hdr = fw_orig; ++ ++ remoteproc_header_dump(hdr); ++ ++ if (fw_orig_size <= sizeof(*hdr) || fw_orig_size <= hdr->hdr_length) ++ return TEE_ERROR_CORRUPT_OBJECT; ++ ++ ctx->hdr = TEE_Malloc(hdr->hdr_length, TEE_MALLOC_FILL_ZERO); ++ if (!ctx->hdr) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ memcpy(ctx->hdr, fw_orig, hdr->hdr_length); ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result remoteproc_verify_signature(struct remoteproc_context *ctx) ++{ ++ TEE_OperationHandle op = TEE_HANDLE_NULL; ++ struct remoteproc_fw_hdr *hdr = ctx->hdr; ++ const struct remoteproc_sig_algo *algo = NULL; ++ TEE_Result res = TEE_ERROR_GENERIC; ++ char *hash = NULL; ++ uint32_t hash_len = 0; ++ ++ algo = remoteproc_get_algo(hdr->sign_type); ++ if (!algo) { ++ EMSG("Unsupported signature type %d", hdr->sign_type); ++ return TEE_ERROR_NOT_SUPPORTED; ++ } ++ ++ /* Compute the header hash */ ++ hash_len = algo->hash_len; ++ hash = TEE_Malloc(hash_len, 0); ++ if (!hash) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ res = TEE_AllocateOperation(&op, TEE_ALG_SHA256, TEE_MODE_DIGEST, 0); ++ if (res != TEE_SUCCESS) ++ goto free_hash; ++ ++ res = TEE_DigestDoFinal(op, hdr, hdr->sign_offset, hash, &hash_len); ++ if (res != TEE_SUCCESS) ++ goto out; ++ ++ /* ++ * TODO: ++ * Provide alternative to verify the signature in the TA. This could ++ * be done for instance by getting the key object from secure storage. ++ */ ++ ++ /* By default ask the pta to verify the signature. */ ++ res = remoteproc_pta_verify(ctx, algo, hash, hash_len); ++ ++out: ++ TEE_FreeOperation(op); ++free_hash: ++ TEE_Free(hash); ++ ++ return res; ++} ++ ++static TEE_Result remoteproc_verify_header(struct remoteproc_context *ctx) ++{ ++ struct remoteproc_fw_hdr *hdr = ctx->hdr; ++ uint32_t hdr_size = 0; ++ uint32_t chunk_size = 0; ++ uint32_t alignment = 0; ++ ++ if (hdr->magic != RPROC_HDR_MAGIC) ++ return TEE_ERROR_CORRUPT_OBJECT; ++ ++ if (hdr->version != HEADER_VERSION) ++ return TEE_ERROR_CORRUPT_OBJECT; ++ ++ /* ++ * The offsets are aligned to 64 bits format. The hdr_length takes into ++ * account these alignments while the length of each chunks are the ++ * effective length,excluding the alignment padding bytes. ++ */ ++ alignment = hdr->sign_length % sizeof(uint64_t) + ++ hdr->phhdr_length % sizeof(uint64_t) + ++ hdr->key_length % sizeof(uint64_t); ++ ++ if (ADD_OVERFLOW(sizeof(*hdr), hdr->sign_length, &hdr_size) || ++ ADD_OVERFLOW(hdr_size, hdr->phhdr_length, &hdr_size) || ++ ADD_OVERFLOW(hdr_size, hdr->key_length, &hdr_size) || ++ ADD_OVERFLOW(hdr_size, alignment, &hdr_size) || ++ hdr->hdr_length != hdr_size) ++ return TEE_ERROR_CORRUPT_OBJECT; ++ ++ if (ADD_OVERFLOW(hdr->sign_offset, hdr->sign_length, &chunk_size) || ++ chunk_size > hdr_size || ++ ADD_OVERFLOW(hdr->key_offset, hdr->key_length, &chunk_size) || ++ chunk_size > hdr_size || ++ ADD_OVERFLOW(hdr->phhdr_offset, hdr->phhdr_length, &chunk_size) || ++ chunk_size > hdr_size) ++ return TEE_ERROR_CORRUPT_OBJECT; ++ ++ if (hdr->phhdr_length % sizeof(struct remoteproc_segment)) ++ return TEE_ERROR_CORRUPT_OBJECT; ++ ++ return remoteproc_verify_signature(ctx); ++} ++ ++static TEE_Result get_rproc_pta_capabilities(struct remoteproc_context *ctx) ++{ ++ uint32_t param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_VALUE_OUTPUT, ++ TEE_PARAM_TYPE_VALUE_OUTPUT, ++ TEE_PARAM_TYPE_NONE); ++ TEE_Param params[TEE_NUM_PARAMS] = { }; ++ TEE_Result res = TEE_ERROR_GENERIC; ++ ++ params[0].value.a = ctx->fw_id; ++ ++ res = TEE_InvokeTACommand(pta_session, TEE_TIMEOUT_INFINITE, ++ PTA_REMOTEPROC_HW_CAPABILITIES, ++ param_types, params, NULL); ++ if (res) ++ return res; ++ ++ ctx->hw_fmt = params[1].value.a; ++ ctx->hw_img_prot = params[2].value.a; ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result remoteproc_verify_firmware(struct remoteproc_context *ctx, ++ uint8_t *fw_orig, ++ uint32_t fw_orig_size) ++{ ++ struct remoteproc_fw_hdr *hdr = NULL; ++ TEE_Result res = TEE_ERROR_GENERIC; ++ ++ res = get_rproc_pta_capabilities(ctx); ++ if (res) ++ return res; ++ ++ /* Secure the firmware image depending on strategy */ ++ if (!(ctx->hw_img_prot & PTA_REMOTEPROC_FW_WITH_HASH_TABLE) || ++ ctx->hw_fmt != PTA_REMOTEPROC_ELF_FMT) { ++ /* ++ * Only hash table for ELF format support implemented ++ * in a first step. ++ */ ++ return TEE_ERROR_NOT_IMPLEMENTED; ++ } ++ ++ res = remoteproc_save_fw_header(ctx, fw_orig, fw_orig_size); ++ if (res) ++ return res; ++ ++ res = remoteproc_verify_header(ctx); ++ if (res) ++ goto free_hdr; ++ ++ /* Store location of the loadable binary in non-secure memory */ ++ hdr = ctx->hdr; ++ ctx->fw_img_size = hdr->img_length; ++ ctx->fw_img = fw_orig + hdr->img_offset; ++ ++ DMSG("Firmware image addr: %p size: %zu", ctx->fw_img, ++ ctx->fw_img_size); ++ ++free_hdr: ++ TEE_Free(ctx->hdr); ++ ++ return res; ++} ++ ++static paddr_t remoteproc_da_to_pa(uint32_t da, uint32_t size, void *priv) ++{ ++ struct remoteproc_context *ctx = priv; ++ uint32_t param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_VALUE_OUTPUT); ++ TEE_Param params[TEE_NUM_PARAMS] = { }; ++ TEE_Result res = TEE_ERROR_GENERIC; ++ ++ params[0].value.a = ctx->fw_id; ++ params[1].value.a = da; ++ params[2].value.a = size; ++ ++ res = TEE_InvokeTACommand(pta_session, TEE_TIMEOUT_INFINITE, ++ PTA_REMOTEPROC_FIRMWARE_DA_TO_PA, ++ param_types, params, NULL); ++ if (res != TEE_SUCCESS) { ++ EMSG("Failed to translate device address %#"PRIx32, da); ++ return 0; ++ } ++ ++ return (paddr_t)reg_pair_to_64(params[3].value.b, params[3].value.a); ++} ++ ++static TEE_Result remoteproc_parse_rsc_table(struct remoteproc_context *ctx) ++{ ++ uint32_t da = 0; ++ TEE_Result res = TEE_ERROR_GENERIC; ++ ++ res = e32_parser_find_rsc_table(ctx->fw_img, ctx->fw_img_size, ++ &da, &ctx->rsc_size); ++ if (res == TEE_ERROR_NO_DATA) { ++ /* Firmware without resource table */ ++ ctx->rsc_size = 0; ++ ctx->rsc_pa = 0; ++ return TEE_SUCCESS; ++ } ++ if (res) ++ return res; ++ ++ if (da) { ++ DMSG("Resource table device address %#"PRIx32" size %zu", ++ da, ctx->rsc_size); ++ ++ ctx->rsc_pa = remoteproc_da_to_pa(da, ctx->rsc_size, ctx); ++ if (!ctx->rsc_pa) ++ return TEE_ERROR_ACCESS_DENIED; ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result get_segment_hash(struct remoteproc_context *ctx, uint8_t *src, ++ uint32_t size, uint32_t da, ++ uint32_t mem_size, unsigned char **hash) ++{ ++ struct remoteproc_fw_hdr *hdr = ctx->hdr; ++ struct remoteproc_segment *peh = NULL; ++ unsigned int i = 0; ++ unsigned int nb_entry = hdr->phhdr_length / sizeof(*peh); ++ ++ peh = (void *)((uint8_t *)hdr + hdr->phhdr_offset); ++ ++ for (i = 0; i < nb_entry; peh++, i++) { ++ if (peh->phdr.p_paddr != da) ++ continue; ++ ++ /* ++ * Segment is read from a non secure memory. Crosscheck it using ++ * the hash table to verify that the segment has not been ++ * corrupted. ++ */ ++ if (peh->phdr.p_type != PT_LOAD) ++ return TEE_ERROR_CORRUPT_OBJECT; ++ ++ if (peh->phdr.p_filesz != size || peh->phdr.p_memsz != mem_size) ++ return TEE_ERROR_CORRUPT_OBJECT; ++ ++ if ((Elf32_Off)(src - ctx->fw_img) != peh->phdr.p_offset) ++ return TEE_ERROR_CORRUPT_OBJECT; ++ ++ *hash = peh->hash; ++ ++ return TEE_SUCCESS; ++ } ++ ++ return TEE_ERROR_NO_DATA; ++} ++ ++static TEE_Result remoteproc_load_segment(uint8_t *src, uint32_t size, ++ uint32_t da, uint32_t mem_size, ++ void *priv) ++{ ++ struct remoteproc_context *ctx = priv; ++ uint32_t param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_MEMREF_INPUT, ++ TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_MEMREF_INPUT); ++ TEE_Param params[TEE_NUM_PARAMS] = { }; ++ TEE_Result res = TEE_ERROR_GENERIC; ++ unsigned char *hash = NULL; ++ ++ /* ++ * Invoke platform remoteproc PTA to load the segment in remote ++ * processor memory which is not mapped in the TA space. ++ */ ++ ++ DMSG("Load segment %#"PRIx32" size %"PRIu32" (%"PRIu32")", da, size, ++ mem_size); ++ ++ res = get_segment_hash(ctx, src, size, da, mem_size, &hash); ++ if (res) ++ return res; ++ ++ params[0].value.a = ctx->fw_id; ++ params[1].memref.buffer = src; ++ params[1].memref.size = size; ++ params[2].value.a = da; ++ params[3].memref.buffer = hash; ++ params[3].memref.size = TEE_SHA256_HASH_SIZE; ++ ++ res = TEE_InvokeTACommand(pta_session, TEE_TIMEOUT_INFINITE, ++ PTA_REMOTEPROC_LOAD_SEGMENT_SHA256, ++ param_types, params, NULL); ++ if (res != TEE_SUCCESS) { ++ EMSG("Fails to load segment, res = 0x%x", res); ++ return res; ++ } ++ ++ /* Fill the rest of the memory with 0 */ ++ if (size < mem_size) { ++ param_types = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_VALUE_INPUT); ++ params[1].value.a = da + size; ++ params[2].value.a = mem_size - size; ++ params[3].value.a = 0; ++ ++ res = TEE_InvokeTACommand(pta_session, TEE_TIMEOUT_INFINITE, ++ PTA_REMOTEPROC_SET_MEMORY, ++ param_types, params, NULL); ++ if (res != TEE_SUCCESS) ++ EMSG("Fails to clear segment, res = 0x%x", res); ++ } ++ ++ return res; ++} ++ ++static TEE_Result remoteproc_load_elf(struct remoteproc_context *ctx) ++{ ++ TEE_Result res = TEE_ERROR_GENERIC; ++ ++ res = e32_parse_ehdr(ctx->fw_img, ctx->fw_img_size); ++ if (res) { ++ EMSG("Failed to parse firmware, res = %#"PRIx32, res); ++ return res; ++ } ++ ++ return e32_parser_load_elf_image(ctx->fw_img, ctx->fw_img_size, ++ remoteproc_load_segment, ctx); ++} ++ ++static TEE_Result remoteproc_load_fw(uint32_t pt, ++ TEE_Param params[TEE_NUM_PARAMS]) ++{ ++ const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_MEMREF_INPUT, ++ TEE_PARAM_TYPE_NONE, ++ TEE_PARAM_TYPE_NONE); ++ struct remoteproc_context *ctx = NULL; ++ uint32_t fw_id = params[0].value.a; ++ TEE_Result res = TEE_ERROR_GENERIC; ++ ++ if (pt != exp_pt) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ ctx = remoteproc_find_firmware(fw_id); ++ if (!ctx) ++ ctx = remoteproc_add_firmware(fw_id); ++ if (!ctx) ++ return TEE_ERROR_OUT_OF_MEMORY; ++ ++ if (ctx->state != REMOTEPROC_OFF) ++ return TEE_ERROR_BAD_STATE; ++ ++ if (!params[1].memref.buffer || !params[1].memref.size) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ DMSG("Got base addr: %p size %zu", params[1].memref.buffer, ++ params[1].memref.size); ++ ++ res = remoteproc_verify_firmware(ctx, params[1].memref.buffer, ++ params[1].memref.size); ++ if (res) { ++ EMSG("Can't Authenticate the firmware, res = %#"PRIx32, res); ++ goto out; ++ } ++ ++ res = remoteproc_load_elf(ctx); ++ if (res) ++ goto out; ++ ++ /* Take opportunity to get the resource table address */ ++ res = remoteproc_parse_rsc_table(ctx); ++ if (res == TEE_SUCCESS) ++ ctx->state = REMOTEPROC_LOADED; ++ ++out: ++ /* Clear reference to firmware image from shared memory */ ++ ctx->fw_img = NULL; ++ ctx->fw_img_size = 0; ++ ++ return res; ++} ++ ++static TEE_Result remoteproc_start_fw(uint32_t pt, ++ TEE_Param params[TEE_NUM_PARAMS]) ++{ ++ const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_NONE, ++ TEE_PARAM_TYPE_NONE, ++ TEE_PARAM_TYPE_NONE); ++ struct remoteproc_context *ctx = NULL; ++ TEE_Result res = TEE_ERROR_GENERIC; ++ ++ if (pt != exp_pt) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ ctx = remoteproc_find_firmware(params[0].value.a); ++ if (!ctx) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ switch (ctx->state) { ++ case REMOTEPROC_OFF: ++ res = TEE_ERROR_BAD_STATE; ++ break; ++ case REMOTEPROC_STARTED: ++ res = TEE_SUCCESS; ++ break; ++ case REMOTEPROC_LOADED: ++ res = TEE_InvokeTACommand(pta_session, TEE_TIMEOUT_INFINITE, ++ PTA_REMOTEPROC_FIRMWARE_START, ++ pt, params, NULL); ++ if (res == TEE_SUCCESS) ++ ctx->state = REMOTEPROC_STARTED; ++ break; ++ default: ++ res = TEE_ERROR_BAD_STATE; ++ } ++ ++ return res; ++} ++ ++static TEE_Result remoteproc_stop_fw(uint32_t pt, ++ TEE_Param params[TEE_NUM_PARAMS]) ++{ ++ const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_NONE, ++ TEE_PARAM_TYPE_NONE, ++ TEE_PARAM_TYPE_NONE); ++ struct remoteproc_context *ctx = NULL; ++ TEE_Result res = TEE_ERROR_GENERIC; ++ ++ if (pt != exp_pt) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ ctx = remoteproc_find_firmware(params[0].value.a); ++ if (!ctx) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ switch (ctx->state) { ++ case REMOTEPROC_LOADED: ++ res = TEE_ERROR_BAD_STATE; ++ break; ++ case REMOTEPROC_OFF: ++ res = TEE_SUCCESS; ++ break; ++ case REMOTEPROC_STARTED: ++ res = TEE_InvokeTACommand(pta_session, TEE_TIMEOUT_INFINITE, ++ PTA_REMOTEPROC_FIRMWARE_STOP, ++ pt, params, NULL); ++ if (res == TEE_SUCCESS) ++ ctx->state = REMOTEPROC_OFF; ++ break; ++ default: ++ res = TEE_ERROR_BAD_STATE; ++ } ++ ++ return res; ++} ++ ++static TEE_Result remoteproc_get_rsc_table(uint32_t pt, ++ TEE_Param params[TEE_NUM_PARAMS]) ++{ ++ const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_VALUE_OUTPUT, ++ TEE_PARAM_TYPE_VALUE_OUTPUT, ++ TEE_PARAM_TYPE_NONE); ++ struct remoteproc_context *ctx = NULL; ++ ++ if (pt != exp_pt) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ ctx = remoteproc_find_firmware(params[0].value.a); ++ if (!ctx) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ if (ctx->state == REMOTEPROC_OFF) ++ return TEE_ERROR_BAD_STATE; ++ ++ reg_pair_from_64((uint64_t)ctx->rsc_pa, ++ ¶ms[1].value.b, ¶ms[1].value.a); ++ reg_pair_from_64((uint64_t)ctx->rsc_size, ++ ¶ms[2].value.b, ¶ms[2].value.a); ++ ++ return TEE_SUCCESS; ++} ++ ++TEE_Result TA_CreateEntryPoint(void) ++{ ++ return TEE_SUCCESS; ++} ++ ++void TA_DestroyEntryPoint(void) ++{ ++} ++ ++TEE_Result TA_OpenSessionEntryPoint(uint32_t pt __unused, ++ TEE_Param params[TEE_NUM_PARAMS] __unused, ++ void **sess __unused) ++{ ++ static const TEE_UUID uuid = PTA_REMOTEPROC_UUID; ++ TEE_Result res = TEE_ERROR_GENERIC; ++ ++ if (!session_refcount) { ++ res = TEE_OpenTASession(&uuid, TEE_TIMEOUT_INFINITE, 0, NULL, ++ &pta_session, NULL); ++ if (res) ++ return res; ++ } ++ ++ session_refcount++; ++ ++ return TEE_SUCCESS; ++} ++ ++void TA_CloseSessionEntryPoint(void *sess __unused) ++{ ++ session_refcount--; ++ ++ if (!session_refcount) ++ TEE_CloseTASession(pta_session); ++} ++ ++TEE_Result TA_InvokeCommandEntryPoint(void *sess __unused, uint32_t cmd_id, ++ uint32_t pt, ++ TEE_Param params[TEE_NUM_PARAMS]) ++{ ++ switch (cmd_id) { ++ case TA_RPROC_FW_CMD_LOAD_FW: ++ return remoteproc_load_fw(pt, params); ++ case TA_RPROC_FW_CMD_START_FW: ++ return remoteproc_start_fw(pt, params); ++ case TA_RPROC_FW_CMD_STOP_FW: ++ return remoteproc_stop_fw(pt, params); ++ case TA_RPROC_FW_CMD_GET_RSC_TABLE: ++ return remoteproc_get_rsc_table(pt, params); ++ case TA_RPROC_FW_CMD_GET_COREDUMP: ++ return TEE_ERROR_NOT_IMPLEMENTED; ++ default: ++ return TEE_ERROR_BAD_PARAMETERS; ++ } ++} +diff --git a/ta/remoteproc/sub.mk b/ta/remoteproc/sub.mk +new file mode 100644 +index 000000000..caca5901a +--- /dev/null ++++ b/ta/remoteproc/sub.mk +@@ -0,0 +1,3 @@ ++global-incdirs-y += include ++srcs-y += remoteproc_core.c ++srcs-y += elf_parser.c +diff --git a/ta/remoteproc/user_ta.mk b/ta/remoteproc/user_ta.mk +new file mode 100644 +index 000000000..6db23f705 +--- /dev/null ++++ b/ta/remoteproc/user_ta.mk +@@ -0,0 +1 @@ ++user-ta-uuid := 80a4c275-0a47-4905-8285-1486a9771a08 +\ No newline at end of file +diff --git a/ta/stm32mp_nvmem/Makefile b/ta/stm32mp_nvmem/Makefile +new file mode 100644 +index 000000000..e84fa58b4 +--- /dev/null ++++ b/ta/stm32mp_nvmem/Makefile +@@ -0,0 +1,18 @@ ++# The UUID for the Trusted Application ++BINARY=1a8342cc-81a5-4512-99fe-9e2b3e37d626 ++ ++ifdef TA_CROSS_COMPILE ++CROSS_COMPILE ?= $(TA_CROSS_COMPILE) ++endif ++export CROSS_COMPILE ++ ++CFG_TEE_TA_LOG_LEVEL ?= 2 ++CPPFLAGS += -DCFG_TEE_TA_LOG_LEVEL=$(CFG_TEE_TA_LOG_LEVEL) ++ ++-include $(TA_DEV_KIT_DIR)/mk/ta_dev_kit.mk ++ ++ifeq ($(wildcard $(TA_DEV_KIT_DIR)/mk/ta_dev_kit.mk), ) ++clean: ++ @echo 'Note: $$(TA_DEV_KIT_DIR)/mk/ta_dev_kit.mk not found, cannot clean TA' ++ @echo 'Note: TA_DEV_KIT_DIR=$(TA_DEV_KIT_DIR)' ++endif +diff --git a/ta/stm32mp_nvmem/include/ta_stm32mp_nvmem.h b/ta/stm32mp_nvmem/include/ta_stm32mp_nvmem.h +new file mode 100644 +index 000000000..9a8d7698a +--- /dev/null ++++ b/ta/stm32mp_nvmem/include/ta_stm32mp_nvmem.h +@@ -0,0 +1,37 @@ ++/* SPDX-License-Identifier: BSD-2-Clause */ ++/* ++ * Copyright (c) 2021, STMicroelectronics ++ */ ++ ++#ifndef TA_STM32MP_NVMEM_H ++#define TA_STM32MP_NVMEM_H ++ ++#define TA_STM32MP_NVMEM_UUID { 0x1a8342cc, 0x81a5, 0x4512, \ ++ { 0x99, 0xfe, 0x9e, 0x2b, 0x3e, 0x37, 0xd6, 0x26 } } ++ ++/* TA_STM32MP_NVMEM type = value[in].a */ ++#define STM32MP_NVMEM_OTP 0 ++ ++/* ++ * Read NVMEM memory for STM32CubeProgrammer ++ * ++ * [in] value[0].a: Type (0 for OTP access) ++ * [out] memref[1].buffer Output buffer to return all read values ++ * [out] memref[1].size Size of buffer to be read ++ */ ++#define TA_STM32MP_NVMEM_READ 0x0 ++ ++/* ++ * Write NVMEM memory for STM32CubeProgrammer ++ * ++ * [in] value[0].a Type (0 for OTP access) ++ * [in] memref[1].buffer Input buffer to read values ++ * [in] memref[1].size Size of buffer to be written ++ * ++ * Return codes: ++ * TEE_SUCCESS - Invoke command success ++ * TEE_ERROR_BAD_PARAMETERS - Incorrect input param ++ */ ++#define TA_STM32MP_NVMEM_WRITE 0x1 ++ ++#endif /* TA_STM32MP_NVMEM_H */ +diff --git a/ta/stm32mp_nvmem/sub.mk b/ta/stm32mp_nvmem/sub.mk +new file mode 100644 +index 000000000..bbf1a82e3 +--- /dev/null ++++ b/ta/stm32mp_nvmem/sub.mk +@@ -0,0 +1,3 @@ ++global-incdirs-y += include ++global-incdirs-y += . ++srcs-y += ta_stm32mp_nvmem.c +diff --git a/ta/stm32mp_nvmem/ta_stm32mp_nvmem.c b/ta/stm32mp_nvmem/ta_stm32mp_nvmem.c +new file mode 100644 +index 000000000..1f5be8341 +--- /dev/null ++++ b/ta/stm32mp_nvmem/ta_stm32mp_nvmem.c +@@ -0,0 +1,251 @@ ++// SPDX-License-Identifier: BSD-2-Clause ++/* ++ * Copyright (c) 2021, STMicroelectronics ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#define TA_STM32MP_NVMEM_VERSION 0x02 ++#define OTP_UPDATE_REQ BIT(31) ++#define OTP_MAX_SIZE 96 ++#define OTP_ERROR_DETECTED BIT(0) ++ ++/* OTP Structure ++ * uint32_t version ++ * uint32_t bsec_state ++ * struct OTP [OTP_MAX_SIZE] ++ * uint32_t otp_value ++ * uint32_t otp_lock ++ */ ++#define OTP_EXCHANGE_MAX_SIZE (sizeof(uint32_t) * \ ++ (2 + 2 * OTP_MAX_SIZE)) ++ ++static size_t session_refcount; ++static TEE_TASessionHandle pta_session = TEE_HANDLE_NULL; ++ ++static TEE_Result nvmem_get_state(uint32_t *state) ++{ ++ TEE_Result res = TEE_SUCCESS; ++ TEE_Param params[TEE_NUM_PARAMS]; ++ const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_OUTPUT, ++ TEE_PARAM_TYPE_NONE, ++ TEE_PARAM_TYPE_NONE, ++ TEE_PARAM_TYPE_NONE); ++ ++ assert(state); ++ ++ res = TEE_InvokeTACommand(pta_session, TEE_TIMEOUT_INFINITE, ++ PTA_BSEC_GET_STATE, ++ exp_pt, params, NULL); ++ if (!res) ++ *state = params[0].value.a; ++ ++ return res; ++} ++ ++static TEE_Result nvmem_read(uint32_t pt, TEE_Param params[TEE_NUM_PARAMS]) ++{ ++ const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_MEMREF_OUTPUT, ++ TEE_PARAM_TYPE_NONE, ++ TEE_PARAM_TYPE_NONE); ++ uint32_t *out = (uint32_t *)params[1].memref.buffer; ++ size_t out_size = params[1].memref.size; ++ uint32_t otp_id = 0; ++ TEE_Result res = TEE_SUCCESS; ++ ++ if (pt != exp_pt || !out || !out_size) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ if (params[0].value.a != STM32MP_NVMEM_OTP || ++ out_size < OTP_EXCHANGE_MAX_SIZE) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ /* Version */ ++ *out++ = TA_STM32MP_NVMEM_VERSION; ++ ++ /* Global status */ ++ res = nvmem_get_state(out++); ++ if (res) ++ return res; ++ ++ for (otp_id = 0; otp_id < OTP_MAX_SIZE; otp_id++, out++) { ++ TEE_Param params_pta[TEE_NUM_PARAMS] = { }; ++ ++ /* Read OTP */ ++ params_pta[0].value.a = otp_id * sizeof(uint32_t); ++ params_pta[0].value.b = FUSE_ACCESS; ++ params_pta[1].memref.buffer = out; ++ params_pta[1].memref.size = sizeof(uint32_t); ++ ++ res = TEE_InvokeTACommand(pta_session, TEE_TIMEOUT_INFINITE, ++ PTA_BSEC_READ_MEM, ++ pt, params_pta, NULL); ++ if (res) { ++ *out++ = 0x0; ++ *out = OTP_ERROR_DETECTED; ++ continue; ++ } ++ ++ out++; ++ ++ /* Read OTP state */ ++ params_pta[0].value.a = otp_id * sizeof(uint32_t); ++ params_pta[0].value.b = LOCK_ACCESS; ++ params_pta[1].memref.buffer = out; ++ params_pta[1].memref.size = sizeof(uint32_t); ++ res = TEE_InvokeTACommand(pta_session, TEE_TIMEOUT_INFINITE, ++ PTA_BSEC_READ_MEM, pt, params_pta, ++ NULL); ++ if (res) ++ *out = OTP_ERROR_DETECTED; ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++static TEE_Result nvmem_write(uint32_t pt, TEE_Param params[TEE_NUM_PARAMS]) ++{ ++ const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_VALUE_INPUT, ++ TEE_PARAM_TYPE_MEMREF_INPUT, ++ TEE_PARAM_TYPE_NONE, ++ TEE_PARAM_TYPE_NONE); ++ uint32_t *in = (uint32_t *)params[1].memref.buffer; ++ size_t in_size = params[1].memref.size; ++ uint32_t otp_id = 0; ++ ++ if (pt != exp_pt || !in || !in_size) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ if (params[0].value.a != STM32MP_NVMEM_OTP || ++ in_size < OTP_EXCHANGE_MAX_SIZE) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ /* Check version */ ++ if (*in++ != TA_STM32MP_NVMEM_VERSION) ++ return TEE_ERROR_BAD_PARAMETERS; ++ ++ /* Skip global status */ ++ in++; ++ ++ for (otp_id = 0; otp_id < OTP_MAX_SIZE; otp_id++) { ++ TEE_Param params_pta[TEE_NUM_PARAMS] = { }; ++ uint32_t value = *in++; ++ uint32_t status = *in++; ++ TEE_Result res = TEE_SUCCESS; ++ ++ if (!(status & OTP_UPDATE_REQ)) ++ continue; ++ ++ /* Write OTP */ ++ params_pta[0].value.a = otp_id * sizeof(uint32_t); ++ params_pta[0].value.b = FUSE_ACCESS; ++ params_pta[1].memref.buffer = &value; ++ params_pta[1].memref.size = sizeof(uint32_t); ++ res = TEE_InvokeTACommand(pta_session, TEE_TIMEOUT_INFINITE, ++ PTA_BSEC_WRITE_MEM, ++ pt, params_pta, NULL); ++ if (res) ++ return res; ++ ++ /* Only set the permanent lock, other are not relevant here */ ++ if (status & LOCK_PERM) { ++ uint32_t lock = LOCK_PERM; ++ ++ DMSG("Perm lock detected %i", otp_id); ++ params_pta[0].value.a = otp_id * sizeof(uint32_t); ++ params_pta[0].value.b = LOCK_ACCESS; ++ params_pta[1].memref.buffer = &lock; ++ params_pta[1].memref.size = sizeof(uint32_t); ++ res = TEE_InvokeTACommand(pta_session, ++ TEE_TIMEOUT_INFINITE, ++ PTA_BSEC_WRITE_MEM, ++ pt, params_pta, NULL); ++ if (res) ++ return res; ++ } ++ } ++ ++ return TEE_SUCCESS; ++} ++ ++TEE_Result TA_CreateEntryPoint(void) ++{ ++ return TEE_SUCCESS; ++} ++ ++void TA_DestroyEntryPoint(void) ++{ ++} ++ ++TEE_Result TA_OpenSessionEntryPoint(uint32_t pt __unused, ++ TEE_Param params[TEE_NUM_PARAMS] __unused, ++ void **session __unused) ++{ ++ static const TEE_UUID uuid = PTA_BSEC_UUID; ++ TEE_Result res = TEE_ERROR_GENERIC; ++ TEE_PropSetHandle h = TEE_HANDLE_NULL; ++ TEE_Identity id = { }; ++ ++ res = TEE_AllocatePropertyEnumerator(&h); ++ if (res) ++ goto out; ++ ++ TEE_StartPropertyEnumerator(h, TEE_PROPSET_CURRENT_CLIENT); ++ ++ res = TEE_GetPropertyAsIdentity(h, NULL, &id); ++ if (res) ++ goto out; ++ ++ if (id.login == TEE_LOGIN_REE_KERNEL) { ++ res = TEE_ERROR_ACCESS_DENIED; ++ goto out; ++ } ++ ++ if (!session_refcount) { ++ res = TEE_OpenTASession(&uuid, TEE_TIMEOUT_INFINITE, 0, NULL, ++ &pta_session, NULL); ++ if (res) ++ goto out; ++ } ++ ++ session_refcount++; ++ ++out: ++ if (h) ++ TEE_FreePropertyEnumerator(h); ++ ++ return res; ++} ++ ++void TA_CloseSessionEntryPoint(void *sess __unused) ++{ ++ session_refcount--; ++ ++ if (!session_refcount) ++ TEE_CloseTASession(pta_session); ++} ++ ++TEE_Result TA_InvokeCommandEntryPoint(void *sess __unused, uint32_t cmd, ++ uint32_t pt, ++ TEE_Param params[TEE_NUM_PARAMS]) ++{ ++ switch (cmd) { ++ case TA_STM32MP_NVMEM_READ: ++ return nvmem_read(pt, params); ++ case TA_STM32MP_NVMEM_WRITE: ++ return nvmem_write(pt, params); ++ default: ++ EMSG("Command ID %#"PRIx32" is not supported", cmd); ++ return TEE_ERROR_NOT_SUPPORTED; ++ } ++} +diff --git a/ta/stm32mp_nvmem/user_ta.mk b/ta/stm32mp_nvmem/user_ta.mk +new file mode 100644 +index 000000000..a7896723f +--- /dev/null ++++ b/ta/stm32mp_nvmem/user_ta.mk +@@ -0,0 +1 @@ ++user-ta-uuid := 1a8342cc-81a5-4512-99fe-9e2b3e37d626 +diff --git a/ta/stm32mp_nvmem/user_ta_header_defines.h b/ta/stm32mp_nvmem/user_ta_header_defines.h +new file mode 100644 +index 000000000..1cdbb2f1e +--- /dev/null ++++ b/ta/stm32mp_nvmem/user_ta_header_defines.h +@@ -0,0 +1,19 @@ ++/* SPDX-License-Identifier: BSD-2-Clause */ ++/* ++ * Copyright (c) 2021, STMicroelectronics ++ */ ++ ++#ifndef USER_TA_HEADER_DEFINES_H ++#define USER_TA_HEADER_DEFINES_H ++ ++#include ++ ++#define TA_UUID TA_STM32MP_NVMEM_UUID ++ ++#define TA_FLAGS (TA_FLAG_SINGLE_INSTANCE | \ ++ TA_FLAG_MULTI_SESSION) ++ ++#define TA_STACK_SIZE (4 * 1024) ++#define TA_DATA_SIZE (16 * 1024) ++ ++#endif /* USER_TA_HEADER_DEFINES_H */ +diff --git a/ta/ta.mk b/ta/ta.mk +index 13ebd45a9..f06d18c3b 100644 +--- a/ta/ta.mk ++++ b/ta/ta.mk +@@ -37,6 +37,7 @@ ta-mk-file-export-vars-$(sm) += CFG_TA_BTI + ta-mk-file-export-vars-$(sm) += CFG_CORE_TPM_EVENT_LOG + ta-mk-file-export-add-$(sm) += CFG_TEE_TA_LOG_LEVEL ?= $(CFG_TEE_TA_LOG_LEVEL)_nl_ + ta-mk-file-export-vars-$(sm) += CFG_TA_BGET_TEST ++ta-mk-file-export-vars-$(sm) += CFG_WITH_TUI + + # Expand platform flags here as $(sm) will change if we have several TA + # targets. Platform flags should not change after inclusion of ta/ta.mk. +@@ -102,6 +103,15 @@ libuuid = be807bbd-81e1-4dc4-bd99-3d363f240ece + libl = utee utils + include mk/lib.mk + ++ifeq ($(CFG_WITH_TUI),y) ++libname = zlib ++libdir = lib/libzlib ++include mk/lib.mk ++libname = png ++libdir = lib/libpng ++include mk/lib.mk ++endif ++ + base-prefix := + + incdirs-host := $(filter-out lib/libutils%, $(incdirs$(sm))) +-- +2.25.1 + diff --git a/recipes-security/optee/optee-os/0002-3.12.0-stm32mp-r1.1-rc1.patch b/recipes-security/optee/optee-os/0002-3.12.0-stm32mp-r1.1-rc1.patch deleted file mode 100644 index 27dae5c..0000000 --- a/recipes-security/optee/optee-os/0002-3.12.0-stm32mp-r1.1-rc1.patch +++ /dev/null @@ -1,61 +0,0 @@ -From 4a99b2016fd0a2e207395da314a3c903c2e65c08 Mon Sep 17 00:00:00 2001 -From: Romuald JEANNE -Date: Wed, 2 Jun 2021 16:53:20 +0200 -Subject: [PATCH 2/3] 3.12.0-stm32mp-r1.1-rc1 - ---- - core/arch/arm/plat-stm32mp1/remoteproc_pta.c | 9 +++------ - core/drivers/clk/clk-stm32mp15.c | 2 +- - core/drivers/stm32_bsec.c | 2 +- - 3 files changed, 5 insertions(+), 8 deletions(-) - -diff --git a/core/arch/arm/plat-stm32mp1/remoteproc_pta.c b/core/arch/arm/plat-stm32mp1/remoteproc_pta.c -index 27b0dfa07..8f3360122 100644 ---- a/core/arch/arm/plat-stm32mp1/remoteproc_pta.c -+++ b/core/arch/arm/plat-stm32mp1/remoteproc_pta.c -@@ -492,13 +492,10 @@ static TEE_Result - TEE_Param params[TEE_NUM_PARAMS] __unused, - void **sess_ctx __unused) - { -- struct tee_ta_session *s = tee_ta_get_calling_session(); -+ struct ts_session *s = ts_get_calling_session(); - -- /* Check that we're called from a user TA */ -- if (!s) -- return TEE_ERROR_ACCESS_DENIED; -- -- if (!is_user_ta_ctx(s->ctx)) -+ /* TODO: check that we're called the remove proc TA (check UUID) */ -+ if (!s || !is_user_ta_ctx(s->ctx)) - return TEE_ERROR_ACCESS_DENIED; - - return TEE_SUCCESS; -diff --git a/core/drivers/clk/clk-stm32mp15.c b/core/drivers/clk/clk-stm32mp15.c -index 794fd5e75..6bbab8902 100644 ---- a/core/drivers/clk/clk-stm32mp15.c -+++ b/core/drivers/clk/clk-stm32mp15.c -@@ -962,7 +962,7 @@ static unsigned long __clk_get_parent_rate(enum stm32mp1_parent_id p) - unsigned long clock = 0; - vaddr_t rcc_base = stm32_rcc_base(); - -- switch (p) { -+ switch ((unsigned int)p) { - case _CK_MPU: - /* MPU sub system */ - reg = io_read32(rcc_base + RCC_MPCKSELR); -diff --git a/core/drivers/stm32_bsec.c b/core/drivers/stm32_bsec.c -index 7d389a061..a8eba5f7d 100644 ---- a/core/drivers/stm32_bsec.c -+++ b/core/drivers/stm32_bsec.c -@@ -646,7 +646,7 @@ static void bsec_dt_otp_nsec_access(void *fdt, int bsec_node) - - size = length / sizeof(uint32_t); - -- if (otp_id + size > STM32MP1_OTP_MAX_ID) -+ if (otp_id + size > OTP_MAX_SIZE) - panic("OTP range oversized"); - - for (i = otp_id; i < otp_id + size; i++) --- -2.17.1 - diff --git a/recipes-security/optee/optee-os/0003-3.12.0-stm32mp-r2.patch b/recipes-security/optee/optee-os/0003-3.12.0-stm32mp-r2.patch deleted file mode 100644 index c4f3061..0000000 --- a/recipes-security/optee/optee-os/0003-3.12.0-stm32mp-r2.patch +++ /dev/null @@ -1,2251 +0,0 @@ -From 547f5ef88ac962218f2cb8cb90bc4cab1b99f7f9 Mon Sep 17 00:00:00 2001 -From: Romuald JEANNE -Date: Thu, 28 Oct 2021 15:54:46 +0200 -Subject: [PATCH] 3.12.0-stm32mp-r2 - ---- - core/arch/arm/dts/stm32mp15xx-dkx.dtsi | 2 +- - core/arch/arm/dts/stm32mp15xx-edx.dtsi | 2 +- - core/arch/arm/plat-stm32mp1/conf.mk | 6 + - core/arch/arm/plat-stm32mp1/main.c | 77 ++ - .../arch/arm/plat-stm32mp1/shared_resources.c | 2 +- - core/drivers/clk/clk-stm32mp15.c | 126 ++- - .../crypto/caam/hal/common/hal_cfg_dt.c | 4 +- - core/drivers/gic.c | 29 + - core/drivers/stm32_i2c.c | 33 + - core/drivers/stm32_rtc.c | 2 +- - core/drivers/stm32_tamp.c | 965 ++++++++++++++++++ - core/drivers/sub.mk | 1 + - core/include/drivers/stm32_tamp.h | 378 +++++++ - core/include/kernel/dt.h | 15 +- - core/include/kernel/interrupt.h | 25 + - core/kernel/dt.c | 28 +- - core/kernel/interrupt.c | 30 + - mk/config.mk | 2 +- - 18 files changed, 1632 insertions(+), 95 deletions(-) - create mode 100644 core/drivers/stm32_tamp.c - create mode 100644 core/include/drivers/stm32_tamp.h - -diff --git a/core/arch/arm/dts/stm32mp15xx-dkx.dtsi b/core/arch/arm/dts/stm32mp15xx-dkx.dtsi -index 236cff932..1334b705a 100644 ---- a/core/arch/arm/dts/stm32mp15xx-dkx.dtsi -+++ b/core/arch/arm/dts/stm32mp15xx-dkx.dtsi -@@ -253,7 +253,7 @@ - CLK_CKPER_HSE - CLK_FMC_ACLK - CLK_QSPI_ACLK -- CLK_ETH_DISABLED -+ CLK_ETH_PLL4P - CLK_SDMMC12_PLL4P - CLK_DSI_DSIPLL - CLK_STGEN_HSE -diff --git a/core/arch/arm/dts/stm32mp15xx-edx.dtsi b/core/arch/arm/dts/stm32mp15xx-edx.dtsi -index ef51b00af..d74d37c4e 100644 ---- a/core/arch/arm/dts/stm32mp15xx-edx.dtsi -+++ b/core/arch/arm/dts/stm32mp15xx-edx.dtsi -@@ -255,7 +255,7 @@ - CLK_CKPER_HSE - CLK_FMC_ACLK - CLK_QSPI_ACLK -- CLK_ETH_DISABLED -+ CLK_ETH_PLL4P - CLK_SDMMC12_PLL4P - CLK_DSI_DSIPLL - CLK_STGEN_HSE -diff --git a/core/arch/arm/plat-stm32mp1/conf.mk b/core/arch/arm/plat-stm32mp1/conf.mk -index b1d41c92a..d01a430d9 100644 ---- a/core/arch/arm/plat-stm32mp1/conf.mk -+++ b/core/arch/arm/plat-stm32mp1/conf.mk -@@ -110,7 +110,12 @@ $(call force,CFG_STM32_TIM,n) - $(call force,CFG_STPMIC1,n) - endif - -+# Remoteproc early TA for coprocessor firmware management - CFG_RPROC_PTA ?= n -+ifeq ($(CFG_RPROC_PTA),y) -+CFG_IN_TREE_EARLY_TAS += remoteproc/80a4c275-0a47-4905-8285-1486a9771a08 -+endif -+ - CFG_STM32_BSEC ?= y - CFG_STM32_CLKCALIB ?= y - CFG_STM32_CRYP ?= y -@@ -121,6 +126,7 @@ CFG_STM32_IWDG ?= y - CFG_STM32_RNG ?= y - CFG_STM32_RTC ?= y - CFG_STM32_TIM ?= y -+CFG_STM32_TAMP ?= y - CFG_STM32_UART ?= y - CFG_STM32MP15_CLK ?= y - CFG_STPMIC1 ?= y -diff --git a/core/arch/arm/plat-stm32mp1/main.c b/core/arch/arm/plat-stm32mp1/main.c -index bf2e394fb..13460c6b9 100644 ---- a/core/arch/arm/plat-stm32mp1/main.c -+++ b/core/arch/arm/plat-stm32mp1/main.c -@@ -11,6 +11,8 @@ - #include - #include - #include -+#include -+#include - #include - #include - #include -@@ -666,3 +668,78 @@ bool stm32_rtc_get_read_twice(void) - return apb1_freq < (rtc_freq * 7U); - } - #endif -+ -+#ifdef CFG_STM32_TAMP -+ -+static const char * const itamper_name[] = { -+ [INT_TAMP1] = "RTC power domain", -+ [INT_TAMP2] = "Temperature monitoring", -+ [INT_TAMP3] = "LSE monitoring", -+ [INT_TAMP4] = "HSE monitoring", -+}; -+ -+DECLARE_KEEP_PAGER(itamper_name); -+ -+static int stm32mp1_itamper_action(int id) -+{ -+ const char *tamp_name = NULL; -+ -+ if (id >= 0 && ((size_t)id < ARRAY_SIZE(itamper_name))) -+ tamp_name = itamper_name[id]; -+ -+ MSG("Internal tamper %u (%s) occurs\n", id + 1, tamp_name); -+ -+ return 1; /* Ack TAMPER and reset system */ -+} -+DECLARE_KEEP_PAGER(stm32mp1_itamper_action); -+ -+static int __unused stm32mp1_etamper_action(int id) -+{ -+ MSG("External tamper %u occurs\n", id + 1); -+ -+ return 1; /* Ack TAMPER and reset system */ -+} -+DECLARE_KEEP_PAGER(stm32mp1_etamper_action); -+ -+static TEE_Result stm32_configure_tamp(void) -+{ -+ struct stm32_bkpregs_conf bkpregs_conf = { -+ .nb_zone1_regs = 10, /* 10 registers in zone 1 */ -+ .nb_zone2_regs = 0 /* No register in zone 2 */ -+ /* Zone3 all remaining */ -+ }; -+ -+ /* Enable BKP Register protection */ -+ if (stm32_tamp_set_secure_bkpregs(&bkpregs_conf) != TEE_SUCCESS) -+ panic(); -+ -+ /* -+ * Configure TAMP to accept only secure access, -+ * and use secure Interrupt. -+ */ -+ stm32_tamp_configure_secure_access(TAMP_REGS_IT_SECURE); -+ -+ stm32_tamp_configure_internal(INT_TAMP1, TAMP_ENABLE, -+ stm32mp1_itamper_action); -+ stm32_tamp_configure_internal(INT_TAMP2, TAMP_ENABLE, -+ stm32mp1_itamper_action); -+ stm32_tamp_configure_internal(INT_TAMP3, TAMP_ENABLE, -+ stm32mp1_itamper_action); -+ stm32_tamp_configure_internal(INT_TAMP4, TAMP_ENABLE, -+ stm32mp1_itamper_action); -+ -+ stm32_tamp_configure_active(TAMP_ACTIVE_FILTER_OFF); -+ -+ stm32_tamp_configure_passive(0); -+ -+ if (stm32_tamp_set_config() != TEE_SUCCESS) -+ panic(); -+ -+ /* Enable timestamp for tamper */ -+ stm32_rtc_set_tamper_timestamp(); -+ -+ return TEE_SUCCESS; -+} -+ -+boot_final(stm32_configure_tamp); -+#endif -diff --git a/core/arch/arm/plat-stm32mp1/shared_resources.c b/core/arch/arm/plat-stm32mp1/shared_resources.c -index 7ad3d7b9c..7d2bcacba 100644 ---- a/core/arch/arm/plat-stm32mp1/shared_resources.c -+++ b/core/arch/arm/plat-stm32mp1/shared_resources.c -@@ -159,7 +159,7 @@ static const char *shres2str_state_tbl[4] __maybe_unused = { - [SHRES_SECURE] = "secure", - }; - --static __maybe_unused const char *shres2str_state(enum stm32mp_shres id) -+static __maybe_unused const char *shres2str_state(enum shres_state id) - { - return shres2str_state_tbl[id]; - } -diff --git a/core/drivers/clk/clk-stm32mp15.c b/core/drivers/clk/clk-stm32mp15.c -index 6bbab8902..31d9ab6d6 100644 ---- a/core/drivers/clk/clk-stm32mp15.c -+++ b/core/drivers/clk/clk-stm32mp15.c -@@ -44,27 +44,30 @@ - - /* Identifiers for root oscillators */ - enum stm32mp_osc_id { -- _HSI = 0, -- _HSE, -- _CSI, -- _LSI, -- _LSE, -- _I2S_CKIN, -- _USB_PHY_48, -+ OSC_HSI, -+ OSC_HSE, -+ OSC_CSI, -+ OSC_LSI, -+ OSC_LSE, -+ OSC_I2S_CKIN, -+ OSC_USB_PHY_48, - NB_OSC, - _UNKNOWN_OSC_ID = 0xffU - }; - - /* Identifiers for parent clocks */ - enum stm32mp1_parent_id { --/* -- * Oscillators are valid IDs for parent clock and are already -- * defined in enum stm32mp_osc_id, ending at NB_OSC - 1. -- * This enum defines IDs are the other possible clock parents. -- */ -- _HSI_KER = NB_OSC, -+ _HSI, -+ _HSE, -+ _CSI, -+ _LSI, -+ _LSE, -+ _I2S_CKIN, -+ _USB_PHY_48, -+ _HSI_KER, - _HSE_KER, - _HSE_KER_DIV2, -+ _HSE_RTC, - _CSI_KER, - _PLL1_P, - _PLL1_Q, -@@ -84,6 +87,7 @@ enum stm32mp1_parent_id { - _PCLK3, - _PCLK4, - _PCLK5, -+ _HCLK5, - _HCLK6, - _HCLK2, - _CK_PER, -@@ -130,6 +134,7 @@ static const uint8_t parent_id_clock_id[_PARENT_NB] = { - [_HSI_KER] = CK_HSI, - [_HSE_KER] = CK_HSE, - [_HSE_KER_DIV2] = CK_HSE_DIV2, -+ [_HSE_RTC] = _UNKNOWN_ID, - [_CSI_KER] = CK_CSI, - [_PLL1_P] = PLL1_P, - [_PLL1_Q] = PLL1_Q, -@@ -149,6 +154,7 @@ static const uint8_t parent_id_clock_id[_PARENT_NB] = { - [_PCLK3] = CK_AXI, - [_PCLK4] = CK_AXI, - [_PCLK5] = CK_AXI, -+ [_HCLK5] = CK_AXI, - [_HCLK6] = CK_AXI, - [_HCLK2] = CK_AXI, - [_CK_PER] = CK_PER, -@@ -390,13 +396,13 @@ static const struct stm32mp1_clk_gate stm32mp1_clk_gate[] = { - _CLK_SC2_FIXED(SEC, RCC_MP_APB5ENSETR, BSECEN, BSEC, _PCLK5), - _CLK_SC2_SELEC(SEC, RCC_MP_APB5ENSETR, STGENEN, STGEN_K, _STGEN_SEL), - -- _CLK_SC2_FIXED(SEC, RCC_MP_AHB5ENSETR, GPIOZEN, GPIOZ, _PCLK5), -- _CLK_SC2_FIXED(SEC, RCC_MP_AHB5ENSETR, CRYP1EN, CRYP1, _PCLK5), -- _CLK_SC2_FIXED(SEC, RCC_MP_AHB5ENSETR, HASH1EN, HASH1, _PCLK5), -+ _CLK_SC2_FIXED(SEC, RCC_MP_AHB5ENSETR, GPIOZEN, GPIOZ, _HCLK5), -+ _CLK_SC2_FIXED(SEC, RCC_MP_AHB5ENSETR, CRYP1EN, CRYP1, _HCLK5), -+ _CLK_SC2_FIXED(SEC, RCC_MP_AHB5ENSETR, HASH1EN, HASH1, _HCLK5), - _CLK_SC2_SELEC(SEC, RCC_MP_AHB5ENSETR, RNG1EN, RNG1_K, _RNG1_SEL), -- _CLK_SC2_FIXED(SEC, RCC_MP_AHB5ENSETR, BKPSRAMEN, BKPSRAM, _PCLK5), -+ _CLK_SC2_FIXED(SEC, RCC_MP_AHB5ENSETR, BKPSRAMEN, BKPSRAM, _HCLK5), - -- _CLK_SC2_FIXED(SEC, RCC_MP_TZAHB6ENSETR, MDMA, MDMA, _PCLK5), -+ _CLK_SC2_FIXED(SEC, RCC_MP_TZAHB6ENSETR, MDMA, MDMA, _HCLK6), - - _CLK_SELEC(SEC, RCC_BDCR, RCC_BDCR_RTCCKEN_POS, RTC, _RTC_SEL), - -@@ -492,7 +498,7 @@ static const uint8_t mcuss_parents[] = { - }; - - static const uint8_t rtc_parents[] = { -- _UNKNOWN_ID, _LSE, _LSI, _HSE -+ _UNKNOWN_ID, _LSE, _LSI, _HSE_RTC - }; - - #ifdef STM32MP1_USE_MPU0_RESET -@@ -520,7 +526,7 @@ static const struct stm32mp1_clk_sel stm32mp1_clk_sel[_PARENT_SEL_NB] = { - _CLK_PARENT(_SPI6_SEL, RCC_SPI6CKSELR, 0, 0x7, spi6_parents), - _CLK_PARENT(_USART1_SEL, RCC_UART1CKSELR, 0, 0x7, usart1_parents), - _CLK_PARENT(_RNG1_SEL, RCC_RNG1CKSELR, 0, 0x3, rng1_parents), -- _CLK_PARENT(_RTC_SEL, RCC_BDCR, 0, 0x3, rtc_parents), -+ _CLK_PARENT(_RTC_SEL, RCC_BDCR, 16, 0x3, rtc_parents), - _CLK_PARENT(_MPU_SEL, RCC_MPCKSELR, 0, 0x3, mpu_parents), - _CLK_PARENT(_AXISS_SEL, RCC_ASSCKSELR, 0, 0x3, axiss_parents), - _CLK_PARENT(_MCUSS_SEL, RCC_MSSCKSELR, 0, 0x3, mcuss_parents), -@@ -564,19 +570,19 @@ static const struct stm32mp1_clk_pll stm32mp1_clk_pll[_PLL_NB] = { - _CLK_PLL(_PLL1, PLL_1600, - RCC_RCK12SELR, RCC_PLL1CFGR1, RCC_PLL1CFGR2, - RCC_PLL1FRACR, RCC_PLL1CR, RCC_PLL1CSGR, -- _HSI, _HSE, _UNKNOWN_OSC_ID, _UNKNOWN_OSC_ID), -+ OSC_HSI, OSC_HSE, _UNKNOWN_OSC_ID, _UNKNOWN_OSC_ID), - _CLK_PLL(_PLL2, PLL_1600, - RCC_RCK12SELR, RCC_PLL2CFGR1, RCC_PLL2CFGR2, - RCC_PLL2FRACR, RCC_PLL2CR, RCC_PLL2CSGR, -- _HSI, _HSE, _UNKNOWN_OSC_ID, _UNKNOWN_OSC_ID), -+ OSC_HSI, OSC_HSE, _UNKNOWN_OSC_ID, _UNKNOWN_OSC_ID), - _CLK_PLL(_PLL3, PLL_800, - RCC_RCK3SELR, RCC_PLL3CFGR1, RCC_PLL3CFGR2, - RCC_PLL3FRACR, RCC_PLL3CR, RCC_PLL3CSGR, -- _HSI, _HSE, _CSI, _UNKNOWN_OSC_ID), -+ OSC_HSI, OSC_HSE, OSC_CSI, _UNKNOWN_OSC_ID), - _CLK_PLL(_PLL4, PLL_800, - RCC_RCK4SELR, RCC_PLL4CFGR1, RCC_PLL4CFGR2, - RCC_PLL4FRACR, RCC_PLL4CR, RCC_PLL4CSGR, -- _HSI, _HSE, _CSI, _I2S_CKIN), -+ OSC_HSI, OSC_HSE, OSC_CSI, OSC_I2S_CKIN), - }; - - /* Prescaler table lookups for clock computation */ -@@ -607,6 +613,7 @@ static const char __maybe_unused *const stm32mp1_clk_parent_name[_PARENT_NB] = { - [_HSI_KER] = "HSI_KER", - [_HSE_KER] = "HSE_KER", - [_HSE_KER_DIV2] = "HSE_KER_DIV2", -+ [_HSE_RTC] = "HSE_RTC", - [_CSI_KER] = "CSI_KER", - [_PLL1_P] = "PLL1_P", - [_PLL1_Q] = "PLL1_Q", -@@ -626,8 +633,9 @@ static const char __maybe_unused *const stm32mp1_clk_parent_name[_PARENT_NB] = { - [_PCLK3] = "PCLK3", - [_PCLK4] = "PCLK4", - [_PCLK5] = "PCLK5", -- [_HCLK6] = "KCLK6", - [_HCLK2] = "HCLK2", -+ [_HCLK5] = "HCLK5", -+ [_HCLK6] = "HCLK6", - [_CK_PER] = "CK_PER", - [_CK_MPU] = "CK_MPU", - [_CK_MCU] = "CK_MCU", -@@ -968,10 +976,10 @@ static unsigned long __clk_get_parent_rate(enum stm32mp1_parent_id p) - reg = io_read32(rcc_base + RCC_MPCKSELR); - switch (reg & RCC_SELR_SRC_MASK) { - case RCC_MPCKSELR_HSI: -- clock = osc_frequency(_HSI); -+ clock = osc_frequency(OSC_HSI); - break; - case RCC_MPCKSELR_HSE: -- clock = osc_frequency(_HSE); -+ clock = osc_frequency(OSC_HSE); - break; - case RCC_MPCKSELR_PLL: - clock = stm32mp1_read_pll_freq(_PLL1, _DIV_P); -@@ -991,16 +999,17 @@ static unsigned long __clk_get_parent_rate(enum stm32mp1_parent_id p) - /* AXI sub system */ - case _ACLK: - case _HCLK2: -+ case _HCLK5: - case _HCLK6: - case _PCLK4: - case _PCLK5: - reg = io_read32(rcc_base + RCC_ASSCKSELR); - switch (reg & RCC_SELR_SRC_MASK) { - case RCC_ASSCKSELR_HSI: -- clock = osc_frequency(_HSI); -+ clock = osc_frequency(OSC_HSI); - break; - case RCC_ASSCKSELR_HSE: -- clock = osc_frequency(_HSE); -+ clock = osc_frequency(OSC_HSE); - break; - case RCC_ASSCKSELR_PLL: - clock = stm32mp1_read_pll_freq(_PLL2, _DIV_P); -@@ -1034,13 +1043,13 @@ static unsigned long __clk_get_parent_rate(enum stm32mp1_parent_id p) - reg = io_read32(rcc_base + RCC_MSSCKSELR); - switch (reg & RCC_SELR_SRC_MASK) { - case RCC_MSSCKSELR_HSI: -- clock = osc_frequency(_HSI); -+ clock = osc_frequency(OSC_HSI); - break; - case RCC_MSSCKSELR_HSE: -- clock = osc_frequency(_HSE); -+ clock = osc_frequency(OSC_HSE); - break; - case RCC_MSSCKSELR_CSI: -- clock = osc_frequency(_CSI); -+ clock = osc_frequency(OSC_CSI); - break; - case RCC_MSSCKSELR_PLL: - clock = stm32mp1_read_pll_freq(_PLL3, _DIV_P); -@@ -1075,13 +1084,13 @@ static unsigned long __clk_get_parent_rate(enum stm32mp1_parent_id p) - reg = io_read32(rcc_base + RCC_CPERCKSELR); - switch (reg & RCC_SELR_SRC_MASK) { - case RCC_CPERCKSELR_HSI: -- clock = osc_frequency(_HSI); -+ clock = osc_frequency(OSC_HSI); - break; - case RCC_CPERCKSELR_HSE: -- clock = osc_frequency(_HSE); -+ clock = osc_frequency(OSC_HSE); - break; - case RCC_CPERCKSELR_CSI: -- clock = osc_frequency(_CSI); -+ clock = osc_frequency(OSC_CSI); - break; - default: - break; -@@ -1089,24 +1098,28 @@ static unsigned long __clk_get_parent_rate(enum stm32mp1_parent_id p) - break; - case _HSI: - case _HSI_KER: -- clock = osc_frequency(_HSI); -+ clock = osc_frequency(OSC_HSI); - break; - case _CSI: - case _CSI_KER: -- clock = osc_frequency(_CSI); -+ clock = osc_frequency(OSC_CSI); - break; - case _HSE: - case _HSE_KER: -- clock = osc_frequency(_HSE); -+ clock = osc_frequency(OSC_HSE); - break; - case _HSE_KER_DIV2: -- clock = osc_frequency(_HSE) >> 1; -+ clock = osc_frequency(OSC_HSE) >> 1; -+ break; -+ case _HSE_RTC: -+ clock = osc_frequency(OSC_HSE); -+ clock /= (io_read32(rcc_base + RCC_RTCDIVR) & RCC_DIVR_DIV_MASK) + 1U; - break; - case _LSI: -- clock = osc_frequency(_LSI); -+ clock = osc_frequency(OSC_LSI); - break; - case _LSE: -- clock = osc_frequency(_LSE); -+ clock = osc_frequency(OSC_LSE); - break; - /* PLL */ - case _PLL1_P: -@@ -1147,7 +1160,7 @@ static unsigned long __clk_get_parent_rate(enum stm32mp1_parent_id p) - break; - /* Other */ - case _USB_PHY_48: -- clock = osc_frequency(_USB_PHY_48); -+ clock = osc_frequency(OSC_USB_PHY_48); - break; - default: - break; -@@ -1347,7 +1360,7 @@ static unsigned long clk_stm32_get_rate(unsigned long id) - /* - * Get the parent ID of the target parent clock, or -1 if no parent found. - */ --static int get_parent_id_parent(unsigned int parent_id) -+static int get_parent_id_parent(enum stm32mp1_parent_id parent_id) - { - enum stm32mp1_parent_sel s = _UNKNOWN_SEL; - enum stm32mp1_pll_id pll_id = _PLL_NB; -@@ -1355,6 +1368,8 @@ static int get_parent_id_parent(unsigned int parent_id) - - switch (parent_id) { - case _ACLK: -+ case _HCLK5: -+ case _HCLK6: - case _PCLK4: - case _PCLK5: - s = _AXISS_SEL; -@@ -1382,7 +1397,6 @@ static int get_parent_id_parent(unsigned int parent_id) - case _PCLK1: - case _PCLK2: - case _HCLK2: -- case _HCLK6: - case _CK_PER: - case _CK_MPU: - case _CK_MCU: -@@ -1419,13 +1433,14 @@ static int get_parent_id_parent(unsigned int parent_id) - } - - /* We are only interested in knowing if PLL3 shall be secure or not */ --static void secure_parent_clocks(unsigned long parent_id) -+static void secure_parent_clocks(enum stm32mp1_parent_id parent_id) - { -- int grandparent_id = 0; -+ enum stm32mp1_parent_id grandparent_id = 0; - - switch (parent_id) { - case _ACLK: - case _HCLK2: -+ case _HCLK5: - case _HCLK6: - case _PCLK4: - case _PCLK5: -@@ -1439,6 +1454,7 @@ static void secure_parent_clocks(unsigned long parent_id) - case _HSE: - case _HSE_KER: - case _HSE_KER_DIV2: -+ case _HSE_RTC: - case _LSE: - case _PLL1_P: - case _PLL1_Q: -@@ -1487,13 +1503,13 @@ DECLARE_KEEP_PAGER(stm32mp_clk_ops); - - #ifdef CFG_EMBED_DTB - static const char *stm32mp_osc_node_label[NB_OSC] = { -- [_LSI] = "clk-lsi", -- [_LSE] = "clk-lse", -- [_HSI] = "clk-hsi", -- [_HSE] = "clk-hse", -- [_CSI] = "clk-csi", -- [_I2S_CKIN] = "i2s_ckin", -- [_USB_PHY_48] = "ck_usbo_48m" -+ [OSC_LSI] = "clk-lsi", -+ [OSC_LSE] = "clk-lse", -+ [OSC_HSI] = "clk-hsi", -+ [OSC_HSE] = "clk-hse", -+ [OSC_CSI] = "clk-csi", -+ [OSC_I2S_CKIN] = "i2s_ckin", -+ [OSC_USB_PHY_48] = "ck_usbo_48m" - }; - - static unsigned int clk_freq_prop(void *fdt, int node) -@@ -1520,8 +1536,8 @@ static void get_osc_freq_from_dt(void *fdt) - if (clk_node < 0) - panic(); - -- COMPILE_TIME_ASSERT((int)_HSI == 0); -- for (idx = _HSI; idx < NB_OSC; idx++) { -+ COMPILE_TIME_ASSERT((int)OSC_HSI == 0); -+ for (idx = OSC_HSI; idx < NB_OSC; idx++) { - const char *name = stm32mp_osc_node_label[idx]; - int subnode = 0; - -diff --git a/core/drivers/crypto/caam/hal/common/hal_cfg_dt.c b/core/drivers/crypto/caam/hal/common/hal_cfg_dt.c -index f03a7d54a..0133a0696 100644 ---- a/core/drivers/crypto/caam/hal/common/hal_cfg_dt.c -+++ b/core/drivers/crypto/caam/hal/common/hal_cfg_dt.c -@@ -9,6 +9,7 @@ - #include - #include - #include -+#include - #include - #include - #include -@@ -113,8 +114,7 @@ void caam_hal_cfg_get_jobring_dt(void *fdt, struct caam_jrcfg *jrcfg) - } - - jrcfg->offset = jr_offset; -- /* Add index of the first SPI interrupt */ -- jrcfg->it_num = jr_it_num + 32; -+ jrcfg->it_num = jr_it_num; - } - } - -diff --git a/core/drivers/gic.c b/core/drivers/gic.c -index 3f1a606ed..3efce7ab8 100644 ---- a/core/drivers/gic.c -+++ b/core/drivers/gic.c -@@ -6,12 +6,15 @@ - - #include - #include -+#include - #include - #include - #include -+#include - #include - #include - #include -+#include - #include - #include - #include -@@ -230,6 +233,29 @@ void gic_init_setup(struct gic_data *gd) - #endif - } - -+static int gic_dt_get_irq(const uint32_t *properties, int len) -+{ -+ int it_num = DT_INFO_INVALID_INTERRUPT; -+ -+ if (!properties || len < 2) -+ return DT_INFO_INVALID_INTERRUPT; -+ -+ it_num = fdt32_to_cpu(properties[1]); -+ -+ switch (fdt32_to_cpu(properties[0])) { -+ case 1: -+ it_num += 16; -+ break; -+ case 0: -+ it_num += 32; -+ break; -+ default: -+ it_num = DT_INFO_INVALID_INTERRUPT; -+ } -+ -+ return it_num; -+} -+ - void gic_init_base_addr(struct gic_data *gd, vaddr_t gicc_base __maybe_unused, - vaddr_t gicd_base) - { -@@ -244,6 +270,9 @@ void gic_init_base_addr(struct gic_data *gd, vaddr_t gicc_base __maybe_unused, - void gic_init(struct gic_data *gd, vaddr_t gicc_base, vaddr_t gicd_base) - { - gic_init_base_addr(gd, gicc_base, gicd_base); -+ if (IS_ENABLED(CFG_DT)) -+ gd->chip.dt_get_irq = gic_dt_get_irq; -+ - gic_init_setup(gd); - } - -diff --git a/core/drivers/stm32_i2c.c b/core/drivers/stm32_i2c.c -index 2807cc42a..bc10960dc 100644 ---- a/core/drivers/stm32_i2c.c -+++ b/core/drivers/stm32_i2c.c -@@ -870,6 +870,33 @@ static int wait_isr_event(struct i2c_handle_s *hi2c, uint32_t bit_mask, - return -1; - } - -+static int wait_isr_event_nack(struct i2c_handle_s *hi2c, uint32_t bit_mask, -+ unsigned int awaited_value, uint64_t timeout_ref) -+{ -+ vaddr_t isr = get_base(hi2c) + I2C_ISR; -+ uint32_t val; -+ -+ assert(IS_POWER_OF_TWO(bit_mask) && !(awaited_value & ~1U)); -+ -+ /* May timeout while TEE thread is suspended */ -+ while (!timeout_elapsed(timeout_ref)) { -+ val = io_read32(isr); -+ if (val & I2C_ISR_NACKF) -+ return -1; -+ if (!!(val & bit_mask) == awaited_value) -+ break; -+ } -+ -+ val = io_read32(isr); -+ if (val & I2C_ISR_NACKF) -+ return -1; -+ if (!!(val & bit_mask) == awaited_value) -+ return 0; -+ -+ notif_i2c_timeout(hi2c); -+ return -1; -+} -+ - /* Handle Acknowledge-Failed sequence detection during an I2C Communication */ - static int i2c_ack_failed(struct i2c_handle_s *hi2c, uint64_t timeout_ref) - { -@@ -1329,6 +1356,12 @@ static int i2c_read(struct i2c_handle_s *hi2c, struct i2c_request *request, - I2C_AUTOEND_MODE, I2C_GENERATE_START_READ); - } - -+ /* Start polling for data but return if NACK indicates we should poll later */ -+ if (wait_isr_event_nack(hi2c, I2C_ISR_RXNE, 1, timeout_ref)) { -+ i2c_ack_failed(hi2c, timeout_ref); -+ goto bail; -+ } -+ - do { - if (wait_isr_event(hi2c, I2C_ISR_RXNE, 1, timeout_ref)) - goto bail; -diff --git a/core/drivers/stm32_rtc.c b/core/drivers/stm32_rtc.c -index 79ab627e6..58bf69f2d 100644 ---- a/core/drivers/stm32_rtc.c -+++ b/core/drivers/stm32_rtc.c -@@ -71,7 +71,7 @@ - - #define RTC_ICSR_RSF BIT(5) - --#define RTC_PRER_PREDIV_S_MASK GENMASK_32(15, 0) -+#define RTC_PRER_PREDIV_S_MASK GENMASK_32(14, 0) - - #define RTC_CR_BYPSHAD BIT(5) - #define RTC_CR_BYPSHAD_SHIFT 5 -diff --git a/core/drivers/stm32_tamp.c b/core/drivers/stm32_tamp.c -new file mode 100644 -index 000000000..317ca7801 ---- /dev/null -+++ b/core/drivers/stm32_tamp.c -@@ -0,0 +1,965 @@ -+// SPDX-License-Identifier: BSD-3-Clause -+/* -+ * Copyright (c) 2021, STMicroelectronics -+ */ -+ -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include -+#include /* If psci_reset */ -+#include -+#include -+#include /* If stm32_cores_reset() */ -+ -+#include -+ -+/* STM32 Registers */ -+#define _TAMP_CR1 0x00U -+#define _TAMP_CR2 0x04U -+#define _TAMP_CR3 0x08U -+#define _TAMP_FLTCR 0x0CU -+#define _TAMP_ATCR1 0x10U -+#define _TAMP_ATSEEDR 0x14U -+#define _TAMP_ATOR 0x18U -+#define _TAMP_ATCR2 0x1CU -+#define _TAMP_SECCFGR 0x20U -+#define _TAMP_SMCR 0x20U -+#define _TAMP_PRIVCFGR 0x24U -+#define _TAMP_IER 0x2CU -+#define _TAMP_SR 0x30U -+#define _TAMP_MISR 0x34U -+#define _TAMP_SMISR 0x38U -+#define _TAMP_SCR 0x3CU -+#define _TAMP_COUNTR 0x40U -+#define _TAMP_COUNT2R 0x44U -+#define _TAMP_OR 0x50U -+#define _TAMP_ERCFGR 0X54U -+#define _TAMP_HWCFGR2 0x3ECU -+#define _TAMP_HWCFGR1 0x3F0U -+#define _TAMP_VERR 0x3F4U -+#define _TAMP_IPIDR 0x3F8U -+#define _TAMP_SIDR 0x3FCU -+ -+/* _TAMP_CR1 bit filds */ -+#define _TAMP_CR1_ITAMP(id) BIT((id) + 16U) -+#define _TAMP_CR1_ETAMP(id) BIT((id)) -+ -+/* _TAMP_CR2 bit filds */ -+#define _TAMP_CR2_ETAMPTRG(id) BIT((id) + 24U) -+#define _TAMP_CR2_BKERASE BIT(23) -+#define _TAMP_CR2_BKBLOCK BIT(22) -+#define _TAMP_CR2_ETAMPMSK_MAX_ID 3U -+#define _TAMP_CR2_ETAMPMSK(id) BIT((id) + 16U) -+#define _TAMP_CR2_ETAMPNOER(id) BIT((id)) -+ -+/* _TAMP_CR3 bit filds */ -+#define _TAMP_CR3_ITAMPNOER_ALL GENMASK_32(12, 0) -+#define _TAMP_CR3_ITAMPNOER(id) BIT((id)) -+ -+/* _TAMP_FLTCR bit fields */ -+#define _TAMP_FLTCR_TAMPFREQ GENMASK_32(2, 0) -+#define _TAMP_FLTCR_TAMPFLT GENMASK_32(4, 3) -+#define _TAMP_FLTCR_TAMPPRCH GENMASK_32(6, 5) -+#define _TAMP_FLTCR_TAMPPUDIS BIT(7) -+ -+/* _TAMP_ATCR bit fields */ -+#define _TAMP_ATCR1_ATCKSEL GENMASK_32(18, 16) -+#define _TAMP_ATCR1_ATPER GENMASK_32(26, 24) -+#define _TAMP_ATCR1_COMMON_MASK GENMASK_32(31, 16) -+#define _TAMP_ATCR1_ETAMPAM(id) BIT((id)) -+#define _TAMP_ATCR1_ATOSEL_MASK(i) GENMASK_32(((i) + 1) * 2U + 7U, \ -+ (i) * 2U + 8U) -+#define _TAMP_ATCR1_ATOSEL(i, o) (((o) - 1U) << ((i) * 2U + 8U)) -+ -+/* _TAMP_ATOR bit fields */ -+#define _TAMP_PRNG GENMASK_32(7, 0) -+#define _TAMP_SEEDF BIT(14) -+#define _TAMP_INITS BIT(15) -+ -+/* _TAMP_IER bit fields */ -+#define _TAMP_IER_ITAMP(id) BIT((id) + 16U) -+#define _TAMP_IER_ETAMP(id) BIT((id)) -+ -+/* _TAMP_SR bit fields */ -+#define _TAMP_SR_ETAMPXF_MASK GENMASK_32(7, 0) -+#define _TAMP_SR_ITAMPXF_MASK GENMASK_32(31, 16) -+#define _TAMP_SR_ITAMP(id) BIT((id) + 16U) -+#define _TAMP_SR_ETAMP(id) BIT((id)) -+ -+/* _TAMP_SCR bit fields */ -+#define _TAMP_SCR_ITAMP(id) BIT((id) + 16U) -+#define _TAMP_SCR_ETAMP(id) BIT((id)) -+ -+/* _TAMP_SECCFGR bit fields */ -+#define _TAMP_SECCFGR_BKPRWSEC_MASK GENMASK_32(7, 0) -+#define _TAMP_SECCFGR_BKPRWSEC_SHIFT 0U -+#define _TAMP_SECCFGR_CNT2SEC BIT(14) -+#define _TAMP_SECCFGR_CNT1SEC BIT(15) -+#define _TAMP_SECCFGR_BKPWSEC_MASK GENMASK_32(23, 16) -+#define _TAMP_SECCFGR_BKPWSEC_SHIFT 16U -+#define _TAMP_SECCFGR_BHKLOCK BIT(30) -+#define _TAMP_SECCFGR_TAMPSEC BIT(31) -+ -+/* _TAMP_SMCR bit fields */ -+#define _TAMP_SMCR_BKPRWDPROT_MASK GENMASK_32(7, 0) -+#define _TAMP_SMCR_BKPRWDPROT_SHIFT 0U -+#define _TAMP_SMCR_BKPWDPROT_MASK GENMASK_32(23, 16) -+#define _TAMP_SMCR_BKPWDPROT_SHIFT 16U -+#define _TAMP_SMCR_DPROT BIT(31) -+ -+/* -+ * _TAMP_PRIVCFGR bit fields defined in stm32_tamp.h -+ * See TAMP_.*PRIVILEGED -+ */ -+#define _TAMP_PRIVCFGR_MASK (GENMASK_32(31, 29) | \ -+ GENMASK_32(15, 14)) -+ -+/* _TAMP_ATCR2 bit fields */ -+#define _TAMP_ATCR2_ATOSEL_MASK(i) GENMASK_32(((i) + 1) * 3U + 7U, \ -+ (i) * 3U + 8U) -+#define _TAMP_ATCR2_ATOSEL(i, o) ((o) << ((i) * 3U + 8U)) -+ -+/* _TAMP_OR bit fields */ -+#define _TAMP_OR_IN1RMP_PF10 0U -+#define _TAMP_OR_IN1RMP_PC13 BIT(0) -+#define _TAMP_OR_IN2RMP_PA6 0U -+#define _TAMP_OR_IN2RMP_PI1 BIT(1) -+#define _TAMP_OR_IN3RMP_PC0 0U -+#define _TAMP_OR_IN3RMP_PI2 BIT(2) -+#define _TAMP_OR_IN4RMP_PG8 0U -+#define _TAMP_OR_IN4RMP_PI3 BIT(3) -+ -+#define _TAMP_OR_OUT3RMP_PI8 0U -+#define _TAMP_OR_OUT3RMP_PC13 BIT(0) -+ -+/* _TAMP_ERCFGR bit fields */ -+#define _TAMP_ERCFGR_ERCFG0 BIT(0) -+ -+/* _TAMP_HWCFGR2 bit fields */ -+#define _TAMP_HWCFGR2_TZ GENMASK_32(11, 8) -+#define _TAMP_HWCFGR2_OR GENMASK_32(7, 0) -+ -+/* _TAMP_HWCFGR1 bit fields */ -+#define _TAMP_HWCFGR1_BKPREG GENMASK_32(7, 0) -+#define _TAMP_HWCFGR1_TAMPER GENMASK_32(11, 8) -+#define _TAMP_HWCFGR1_ACTIVE GENMASK_32(15, 12) -+#define _TAMP_HWCFGR1_INTERN GENMASK_32(31, 16) -+#define _TAMP_HWCFGR1_ITAMP_MAX_ID 16U -+#define _TAMP_HWCFGR1_ITAMP(id) BIT((id) + 16U) -+ -+/* _TAMP_VERR bit fields */ -+#define _TAMP_VERR_MINREV GENMASK_32(3, 0) -+#define _TAMP_VERR_MAJREV GENMASK_32(7, 4) -+ -+#define SEED_TIMEOUT_US 1000 -+ -+struct stm32_tamp_int { -+ const uint32_t id; -+ uint32_t mode; -+ int (*func)(int id); -+}; -+ -+struct stm32_tamp_ext { -+ const uint32_t id; -+ uint32_t mode; -+ uint8_t out_pin; -+ int (*func)(int id); -+}; -+ -+struct stm32_tamp_instance { -+ struct stm32_tamp_platdata pdata; -+ uint32_t hwconf1; -+ uint32_t hwconf2; -+ uint32_t secret_list_conf; -+ uint32_t privilege_conf; -+ uint32_t secure_conf; -+ uint32_t passive_conf; -+ uint32_t active_conf; -+}; -+ -+struct stm32_tamp_int int_tamp_mp15[] = { -+ { .id = INT_TAMP1 }, { .id = INT_TAMP2 }, { .id = INT_TAMP3 }, -+ { .id = INT_TAMP4 }, { .id = INT_TAMP5 }, { .id = INT_TAMP8 }, -+}; -+ -+struct stm32_tamp_ext ext_tamp_mp15[] = { -+ { .id = EXT_TAMP1 }, { .id = EXT_TAMP2 }, { .id = EXT_TAMP3 }, -+}; -+ -+/* -+ * Only 1 instance of TAMP is expected per platform -+ * -+ * 0 is the expected initial values for all fields but .id -+ */ -+static struct stm32_tamp_instance stm32_tamp = { }; -+DECLARE_KEEP_PAGER(stm32_tamp); -+ -+struct my_dt_device_match { -+ const char *compatible; -+ void *data; -+}; -+ -+static struct stm32_tamp_compat mp15_compat = { -+ .nb_monotonic_counter = 1, -+ .tags = 0, -+ .int_tamp = int_tamp_mp15, -+ .int_tamp_size = ARRAY_SIZE(int_tamp_mp15), -+ .ext_tamp = ext_tamp_mp15, -+ .ext_tamp_size = ARRAY_SIZE(ext_tamp_mp15), -+}; -+ -+static const struct my_dt_device_match tamp_match_table[] = { -+ { .compatible = "st,stm32-tamp", .data = &mp15_compat}, -+ { 0 } -+}; -+ -+static void stm32_tamp_set_secret_list(struct stm32_tamp_instance *tamp) -+{ -+ vaddr_t base = io_pa_or_va(&tamp->pdata.base); -+ -+ if (tamp->pdata.compat && -+ (tamp->pdata.compat->tags & TAMP_HAS_CONF_SECRET_IPS)) { -+ if (tamp->secret_list_conf & TAMP_CONF_SECRET_BACKUP_SRAM) -+ io_setbits32(base + _TAMP_ERCFGR, _TAMP_ERCFGR_ERCFG0); -+ else -+ io_clrbits32(base + _TAMP_ERCFGR, _TAMP_ERCFGR_ERCFG0); -+ } -+} -+ -+static void stm32_tamp_set_secure(struct stm32_tamp_instance *tamp) -+{ -+ vaddr_t base = io_pa_or_va(&tamp->pdata.base); -+ uint32_t mode = 0; -+ -+ /* Force secure mode: within OP-TEE we only access TAMP from secure */ -+ tamp->secure_conf |= TAMP_REGS_IT_SECURE; -+ mode = tamp->secure_conf; -+ -+ if (tamp->pdata.compat && -+ (tamp->pdata.compat->tags & TAMP_HAS_CONF_SECURE)) { -+ if (mode & TAMP_REGS_IT_SECURE) -+ io_setbits32(base + _TAMP_SECCFGR, -+ _TAMP_SECCFGR_TAMPSEC); -+ else -+ io_clrbits32(base + _TAMP_SECCFGR, -+ _TAMP_SECCFGR_TAMPSEC); -+ -+ if (mode & TAMP_CNT1_SECURE) -+ io_setbits32(base + _TAMP_SECCFGR, -+ _TAMP_SECCFGR_CNT1SEC); -+ else -+ io_clrbits32(base + _TAMP_SECCFGR, -+ _TAMP_SECCFGR_CNT1SEC); -+ -+ if (mode & TAMP_CNT2_SECURE) -+ io_setbits32(base + _TAMP_SECCFGR, -+ _TAMP_SECCFGR_CNT2SEC); -+ else -+ io_clrbits32(base + _TAMP_SECCFGR, -+ _TAMP_SECCFGR_CNT2SEC); -+ } else { -+ /* Note: MP15 doesn't use SECCFG register -+ * and inverts the secure bit -+ */ -+ if (mode & TAMP_REGS_IT_SECURE) -+ io_clrbits32(base + _TAMP_SMCR, _TAMP_SMCR_DPROT); -+ else -+ io_setbits32(base + _TAMP_SMCR, _TAMP_SMCR_DPROT); -+ } -+} -+ -+static void stm32_tamp_set_privilege(struct stm32_tamp_instance *tamp) -+{ -+ vaddr_t base = io_pa_or_va(&tamp->pdata.base); -+ uint32_t mode = tamp->privilege_conf; -+ -+ if (tamp->pdata.compat && -+ (tamp->pdata.compat->tags & TAMP_HAS_CONF_PRIVILEGE)) -+ io_clrsetbits32(base + _TAMP_PRIVCFGR, _TAMP_PRIVCFGR_MASK, -+ mode & _TAMP_PRIVCFGR_MASK); -+} -+ -+static void stm32_tamp_set_pins(vaddr_t base, uint32_t mode) -+{ -+ io_setbits32(base + _TAMP_OR, mode); -+} -+ -+static TEE_Result stm32_tamp_set_seed(vaddr_t base) -+{ -+ /* Need RNG access. */ -+ uint64_t timeout_ref = timeout_init_us(SEED_TIMEOUT_US); -+ uint8_t idx = 0; -+ -+ for (idx = 0; idx < 4U; idx++) { -+ uint32_t rnd = 0; -+ -+ if (crypto_rng_read((uint8_t *)&rnd, sizeof(uint32_t)) != 0) -+ return TEE_ERROR_BAD_STATE; -+ -+ io_write32(base + _TAMP_ATSEEDR, rnd); -+ } -+ -+ while ((io_read32(base + _TAMP_ATOR) & _TAMP_SEEDF) != 0U) { -+ if (timeout_elapsed(timeout_ref)) -+ return TEE_ERROR_BAD_STATE; -+ } -+ -+ return TEE_SUCCESS; -+} -+ -+static bool is_int_tamp_id_valid(uint32_t id) -+{ -+ return (id <= _TAMP_HWCFGR1_ITAMP_MAX_ID) && -+ (stm32_tamp.hwconf1 & _TAMP_HWCFGR1_ITAMP(id)); -+} -+ -+static bool is_ext_tamp_id_valid(uint32_t id) -+{ -+ return (stm32_tamp.pdata.compat) && -+ (id <= stm32_tamp.pdata.compat->ext_tamp_size); -+} -+ -+static TEE_Result stm32_tamp_set_int_config(struct stm32_tamp_compat *tcompat, -+ uint32_t itamp_index, uint32_t *cr1, -+ uint32_t *cr3, uint32_t *ier) -+{ -+ uint32_t id = 0; -+ struct stm32_tamp_int *tamp_int = NULL; -+ -+ if (!tcompat) -+ return TEE_ERROR_BAD_PARAMETERS; -+ -+ tamp_int = &tcompat->int_tamp[itamp_index]; -+ id = tamp_int->id; -+ -+ if (!is_int_tamp_id_valid(id)) -+ return TEE_ERROR_BAD_PARAMETERS; -+ -+ /* If this internal tamper is disabled, reset its configuration. */ -+ if ((tamp_int->mode & TAMP_ENABLE) != TAMP_ENABLE) { -+ *cr1 &= ~_TAMP_CR1_ITAMP(id); -+ *ier &= ~_TAMP_IER_ITAMP(id); -+ if (tcompat->tags & TAMP_HAS_CR3) -+ *cr3 &= ~_TAMP_CR3_ITAMPNOER(id); -+ -+ return TEE_SUCCESS; -+ } -+ -+ *cr1 |= _TAMP_CR1_ITAMP(id); -+ *ier |= _TAMP_IER_ITAMP(id); -+ -+ if (tcompat->tags & TAMP_HAS_CR3) { -+ if ((tamp_int->mode & TAMP_NOERASE) == TAMP_NOERASE) -+ *cr3 |= _TAMP_CR3_ITAMPNOER(id); -+ else -+ *cr3 &= ~_TAMP_CR3_ITAMPNOER(id); -+ } -+ -+ return TEE_SUCCESS; -+} -+ -+static TEE_Result stm32_tamp_set_ext_config(struct stm32_tamp_compat *tcompat, -+ uint32_t etamp_index, -+ uint32_t *cr1, uint32_t *cr2, -+ uint32_t *atcr1, uint32_t *atcr2, -+ uint32_t *ier) -+{ -+ uint32_t id = 0; -+ struct stm32_tamp_ext *tamp_ext = NULL; -+ -+ if (!tcompat) -+ return TEE_ERROR_BAD_PARAMETERS; -+ -+ tamp_ext = &tcompat->ext_tamp[etamp_index]; -+ id = tamp_ext->id; -+ -+ /* Exit if not a valid TAMP_ID */ -+ if (!is_ext_tamp_id_valid(id)) -+ return TEE_ERROR_BAD_PARAMETERS; -+ -+ /* If this external tamper is disabled, reset its configuration. */ -+ if ((tamp_ext->mode & TAMP_ENABLE) != TAMP_ENABLE) { -+ *cr1 &= ~_TAMP_CR1_ETAMP(id); -+ *cr2 &= ~_TAMP_CR2_ETAMPMSK(id); -+ *cr2 &= ~_TAMP_CR2_ETAMPTRG(id); -+ *cr2 &= ~_TAMP_CR2_ETAMPNOER(id); -+ *ier &= ~_TAMP_IER_ETAMP(id); -+ return TEE_SUCCESS; -+ } -+ -+ *cr1 |= _TAMP_CR1_ETAMP(id); -+ -+ if ((tamp_ext->mode & TAMP_TRIG_ON) == TAMP_TRIG_ON) -+ *cr2 |= _TAMP_CR2_ETAMPTRG(id); -+ else -+ *cr2 &= ~_TAMP_CR2_ETAMPTRG(id); -+ -+ if ((tamp_ext->mode & TAMP_ACTIVE) == TAMP_ACTIVE) { -+ *atcr1 |= _TAMP_ATCR1_ETAMPAM(id); -+ /* Configure output pin: -+ * For the case out_pin = 0, we select same output pin than the -+ * input one. -+ */ -+ if (tamp_ext->out_pin == TAMPOUTSEL_SAME_AS_INPUT) -+ tamp_ext->out_pin = id + 1; -+ -+ if (tamp_ext->out_pin <= tcompat->ext_tamp_size) { -+ if (tcompat->tags & TAMP_HAS_ATCR2) -+ *atcr2 = (*atcr2 & -+ ~_TAMP_ATCR2_ATOSEL_MASK(id)) | -+ _TAMP_ATCR2_ATOSEL(id, -+ tamp_ext->out_pin); -+ else -+ *atcr1 = (*atcr1 & -+ ~_TAMP_ATCR1_ATOSEL_MASK(id)) | -+ _TAMP_ATCR1_ATOSEL(id, -+ tamp_ext->out_pin); -+ } -+ } else { -+ *atcr1 &= ~_TAMP_ATCR1_ETAMPAM(id); -+ } -+ -+ if ((tamp_ext->mode & TAMP_NOERASE) == TAMP_NOERASE) -+ *cr2 |= _TAMP_CR2_ETAMPNOER(id); -+ else -+ *cr2 &= ~_TAMP_CR2_ETAMPNOER(id); -+ -+ if (id < _TAMP_CR2_ETAMPMSK_MAX_ID) { -+ /* -+ * Only external TAMP 1, 2 and 3 can be masked -+ */ -+ if ((tamp_ext->mode & TAMP_EVT_MASK) == TAMP_EVT_MASK) { -+ /* -+ * ETAMP(id) event generates a trigger event. This -+ * ETAMP(id) is masked and internally cleared by -+ * hardware. -+ * The secrets are not erased. -+ */ -+ *ier &= ~_TAMP_IER_ETAMP(id); -+ *cr2 |= _TAMP_CR2_ETAMPMSK(id); -+ } else { -+ /* -+ * normal ETAMP interrupt: -+ * ETAMP(id) event generates a trigger event and -+ * TAMP(id) must be cleared by software to allow -+ * next tamper event detection. -+ */ -+ *cr2 &= ~_TAMP_CR2_ETAMPMSK(id); -+ *ier |= _TAMP_IER_ETAMP(id); -+ } -+ } else { -+ /* Other than 1,2,3 external TAMP, we want its interruption */ -+ *ier |= _TAMP_IER_ETAMP(id); -+ } -+ -+ return TEE_SUCCESS; -+} -+ -+TEE_Result stm32_tamp_set_secure_bkpregs(struct stm32_bkpregs_conf *bkr_conf) -+{ -+ uint32_t first_z2 = 0; -+ uint32_t first_z3 = 0; -+ vaddr_t base = io_pa_or_va(&stm32_tamp.pdata.base); -+ -+ if (!bkr_conf) -+ return TEE_ERROR_BAD_PARAMETERS; -+ -+ first_z2 = bkr_conf->nb_zone1_regs; -+ first_z3 = bkr_conf->nb_zone1_regs + bkr_conf->nb_zone2_regs; -+ -+ if ((first_z2 > (stm32_tamp.hwconf1 & _TAMP_HWCFGR1_BKPREG)) || -+ (first_z3 > (stm32_tamp.hwconf1 & _TAMP_HWCFGR1_BKPREG))) -+ return TEE_ERROR_NOT_SUPPORTED; -+ -+ if (stm32_tamp.pdata.compat && -+ (stm32_tamp.pdata.compat->tags & TAMP_HAS_CONF_SECURE)) { -+ io_clrsetbits32(base + _TAMP_SECCFGR, -+ _TAMP_SECCFGR_BKPRWSEC_MASK, -+ (first_z2 << _TAMP_SECCFGR_BKPRWSEC_SHIFT) & -+ _TAMP_SECCFGR_BKPRWSEC_MASK); -+ -+ io_clrsetbits32(base + _TAMP_SECCFGR, -+ _TAMP_SECCFGR_BKPWSEC_MASK, -+ (first_z3 << _TAMP_SECCFGR_BKPWSEC_SHIFT) & -+ _TAMP_SECCFGR_BKPWSEC_MASK); -+ } else { -+ io_clrsetbits32(base + _TAMP_SMCR, -+ _TAMP_SMCR_BKPRWDPROT_MASK, -+ (first_z2 << _TAMP_SMCR_BKPRWDPROT_SHIFT) & -+ _TAMP_SMCR_BKPRWDPROT_MASK); -+ -+ io_clrsetbits32(base + _TAMP_SMCR, -+ _TAMP_SMCR_BKPWDPROT_MASK, -+ (first_z3 << _TAMP_SMCR_BKPWDPROT_SHIFT) & -+ _TAMP_SMCR_BKPWDPROT_MASK); -+ } -+ -+ return TEE_SUCCESS; -+} -+ -+TEE_Result stm32_tamp_set_config(void) -+{ -+ int ret = TEE_SUCCESS; -+ uint32_t i = 0; -+ uint32_t cr1 = 0; -+ uint32_t cr2 = 0; -+ uint32_t cr3 = 0; -+ uint32_t atcr1 = 0; -+ uint32_t atcr2 = 0; -+ uint32_t fltcr = 0; -+ uint32_t ier = 0; -+ -+ vaddr_t base = io_pa_or_va(&stm32_tamp.pdata.base); -+ -+ if (!stm32_tamp.pdata.compat || -+ !stm32_tamp.pdata.compat->int_tamp || -+ !stm32_tamp.pdata.compat->ext_tamp) -+ return TEE_ERROR_BAD_STATE; -+ -+ /* Select extra IP to add in the deleted/blocked IP in case of -+ * tamper event -+ */ -+ stm32_tamp_set_secret_list(&stm32_tamp); -+ -+ /* Select access in secure or unsecure */ -+ stm32_tamp_set_secure(&stm32_tamp); -+ -+ /* Select access in privileged mode or unprivileged mode */ -+ stm32_tamp_set_privilege(&stm32_tamp); -+ -+ /* Set passive filter configuration */ -+ if (stm32_tamp.passive_conf != 0U) -+ fltcr = stm32_tamp.passive_conf; -+ -+ /* Set active mode configuration */ -+ if (stm32_tamp.active_conf != 0U) -+ atcr1 = (atcr1 & ~_TAMP_ATCR1_COMMON_MASK) | -+ (stm32_tamp.active_conf & _TAMP_ATCR1_COMMON_MASK); -+ -+ for (i = 0U; i < stm32_tamp.pdata.compat->int_tamp_size; i++) { -+ ret = stm32_tamp_set_int_config(stm32_tamp.pdata.compat, i, -+ &cr1, &cr3, &ier); -+ if (ret != 0) -+ return ret; -+ } -+ -+ for (i = 0U; i < stm32_tamp.pdata.compat->ext_tamp_size; i++) { -+ ret = stm32_tamp_set_ext_config(stm32_tamp.pdata.compat, i, -+ &cr1, &cr2, &atcr1, &atcr2, -+ &ier); -+ if (ret != 0) -+ return ret; -+ } -+ -+ /* -+ * We apply configuration all in a row: -+ * As for active ext tamper "all the needed tampers must be enabled in -+ * the same write access". -+ */ -+ io_write32(base + _TAMP_FLTCR, fltcr); -+ /* Active filter configuration applied only if not already done. */ -+ if (((io_read32(base + _TAMP_ATOR) & _TAMP_INITS) != _TAMP_INITS)) { -+ io_write32(base + _TAMP_ATCR1, atcr1); -+ if (stm32_tamp.pdata.compat->tags & TAMP_HAS_ATCR2) -+ io_write32(base + _TAMP_ATCR2, atcr2); -+ } -+ -+ io_write32(base + _TAMP_CR1, cr1); -+ io_write32(base + _TAMP_CR2, cr2); -+ if (stm32_tamp.pdata.compat->tags & TAMP_HAS_CR3) -+ io_write32(base + _TAMP_CR3, cr3); -+ -+ /* If active tamper we reinit the seed. */ -+ if (stm32_tamp.active_conf != 0U) { -+ if (stm32_tamp_set_seed(base) != TEE_SUCCESS) { -+ EMSG("Active tamper: SEED not initialized"); -+ return TEE_ERROR_BAD_STATE; -+ } -+ } -+ -+ /* Enable interrupts. */ -+ io_write32(base + _TAMP_IER, ier); -+ -+ return TEE_SUCCESS; -+} -+ -+TEE_Result stm32_tamp_write_mcounter(int cnt_idx) -+{ -+ vaddr_t base = io_pa_or_va(&stm32_tamp.pdata.base); -+ -+ if (cnt_idx < 0 || !stm32_tamp.pdata.compat || -+ cnt_idx > stm32_tamp.pdata.compat->nb_monotonic_counter) -+ return TEE_ERROR_BAD_PARAMETERS; -+ -+ io_write32(base + _TAMP_COUNTR + cnt_idx * sizeof(uint32_t), 1U); -+ -+ return TEE_SUCCESS; -+} -+ -+uint32_t stm32_tamp_read_mcounter(int cnt_idx) -+{ -+ vaddr_t base = io_pa_or_va(&stm32_tamp.pdata.base); -+ -+ if (cnt_idx < 0 || !stm32_tamp.pdata.compat || -+ cnt_idx > stm32_tamp.pdata.compat->nb_monotonic_counter) -+ return 0U; -+ -+ return io_read32(base + _TAMP_COUNTR + cnt_idx * sizeof(uint32_t)); -+} -+ -+void stm32_tamp_configure_secret_list(uint32_t secret_list_conf) -+{ -+ stm32_tamp.secret_list_conf = secret_list_conf; -+} -+ -+void stm32_tamp_configure_privilege_access(uint32_t privilege_conf) -+{ -+ stm32_tamp.privilege_conf = privilege_conf; -+} -+ -+void stm32_tamp_configure_secure_access(uint32_t secure_conf) -+{ -+ stm32_tamp.secure_conf = secure_conf; -+} -+ -+void stm32_tamp_configure_passive(uint32_t passive_conf) -+{ -+ stm32_tamp.passive_conf = passive_conf; -+} -+ -+void stm32_tamp_configure_active(uint32_t active_conf) -+{ -+ stm32_tamp.active_conf = active_conf; -+} -+ -+TEE_Result stm32_tamp_configure_internal(enum stm32_tamp_int_id id, -+ uint32_t mode, -+ int (*callback)(int id)) -+{ -+ uint32_t i = 0; -+ uint32_t itamp_id = id; -+ struct stm32_tamp_int *tamp_int = NULL; -+ -+ if (!stm32_tamp.pdata.compat) -+ return TEE_ERROR_BAD_STATE; -+ -+ /* Find internal Tamp struct*/ -+ for (i = 0U; i < stm32_tamp.pdata.compat->int_tamp_size; i++) { -+ if (stm32_tamp.pdata.compat->int_tamp[i].id == itamp_id) { -+ tamp_int = &stm32_tamp.pdata.compat->int_tamp[i]; -+ break; -+ } -+ } -+ -+ if (!tamp_int) -+ return TEE_ERROR_BAD_PARAMETERS; -+ -+ tamp_int->mode = mode; -+ tamp_int->func = callback; -+ -+ return TEE_SUCCESS; -+} -+ -+TEE_Result stm32_tamp_configure_external(enum stm32_tamp_ext_id id, -+ uint32_t mode, -+ enum stm32_tamp_ext_out_id out_pin, -+ int (*callback)(int id)) -+{ -+ uint32_t i = 0; -+ uint32_t etamp_id = id; -+ struct stm32_tamp_ext *tamp_ext = NULL; -+ -+ if (!stm32_tamp.pdata.compat) -+ return TEE_ERROR_BAD_STATE; -+ -+ /* Find external Tamp struct */ -+ for (i = 0U; i < stm32_tamp.pdata.compat->ext_tamp_size; i++) { -+ if (stm32_tamp.pdata.compat->ext_tamp[i].id == etamp_id) { -+ tamp_ext = &stm32_tamp.pdata.compat->ext_tamp[i]; -+ break; -+ } -+ } -+ -+ if (!tamp_ext) -+ return TEE_ERROR_BAD_PARAMETERS; -+ -+ tamp_ext->mode = mode; -+ tamp_ext->out_pin = out_pin; -+ tamp_ext->func = callback; -+ -+ return TEE_SUCCESS; -+} -+ -+bool stm32_tamp_are_secrets_blocked(void) -+{ -+ if (stm32_tamp.pdata.compat && -+ (stm32_tamp.pdata.compat->tags & TAMP_HAS_SECRET_STATUS)) { -+ vaddr_t base = io_pa_or_va(&stm32_tamp.pdata.base); -+ -+ return (((io_read32(base + _TAMP_CR2) & -+ _TAMP_CR2_BKBLOCK) == _TAMP_CR2_BKBLOCK) || -+ (io_read32(base + _TAMP_SR) != 0)); -+ } else { -+ return false; -+ } -+} -+ -+void stm32_tamp_block_secrets(void) -+{ -+ vaddr_t base = io_pa_or_va(&stm32_tamp.pdata.base); -+ -+ if (stm32_tamp.pdata.compat && -+ (stm32_tamp.pdata.compat->tags & TAMP_HAS_SECRET_STATUS)) -+ io_setbits32(base + _TAMP_CR2, _TAMP_CR2_BKBLOCK); -+} -+ -+void stm32_tamp_unblock_secrets(void) -+{ -+ vaddr_t base = io_pa_or_va(&stm32_tamp.pdata.base); -+ -+ if (stm32_tamp.pdata.compat && -+ (stm32_tamp.pdata.compat->tags & TAMP_HAS_SECRET_STATUS)) -+ io_clrbits32(base + _TAMP_CR2, _TAMP_CR2_BKBLOCK); -+} -+ -+void stm32_tamp_erase_secrets(void) -+{ -+ vaddr_t base = io_pa_or_va(&stm32_tamp.pdata.base); -+ -+ if (stm32_tamp.pdata.compat && -+ (stm32_tamp.pdata.compat->tags & TAMP_HAS_SECRET_STATUS)) -+ io_setbits32(base + _TAMP_CR2, _TAMP_CR2_BKERASE); -+} -+ -+void stm32_tamp_lock_boot_hardware_key(void) -+{ -+ vaddr_t base = io_pa_or_va(&stm32_tamp.pdata.base); -+ -+ if (stm32_tamp.pdata.compat && -+ (stm32_tamp.pdata.compat->tags & TAMP_HAS_LOCK_KEY)) -+ io_setbits32(base + _TAMP_SECCFGR, _TAMP_SECCFGR_BHKLOCK); -+} -+ -+static enum itr_return stm32_tamp_it_handler(struct itr_handler *h) -+{ -+ struct stm32_tamp_instance *tamp = h->data; -+ vaddr_t base = io_pa_or_va(&tamp->pdata.base); -+ uint32_t it = io_read32(base + _TAMP_SR); -+ uint32_t int_it = it & _TAMP_SR_ITAMPXF_MASK; -+ uint32_t ext_it = it & _TAMP_SR_ETAMPXF_MASK; -+ uint8_t i = 0; -+ struct stm32_rtc_time tamp_ts = { }; -+ -+ if (stm32_rtc_is_timestamp_enable()) { -+ stm32_rtc_get_timestamp(&tamp_ts); -+ FMSG("Tamper Event Occurred"); -+ FMSG("Date : %u/%u\n \t Time : %u:%u:%u", -+ tamp_ts.day, tamp_ts.month, tamp_ts.hour, -+ tamp_ts.min, tamp_ts.sec); -+ } -+ -+ while ((int_it != 0U) && (i < stm32_tamp.pdata.compat->int_tamp_size)) { -+ int ret = -1; -+ uint32_t id = tamp->pdata.compat->int_tamp[i].id; -+ -+ if ((it & _TAMP_SR_ITAMP(id)) != 0U) { -+ if (tamp->pdata.compat->int_tamp[i].func) -+ ret = tamp->pdata.compat->int_tamp[i].func(id); -+ -+ if (ret >= 0) { -+ io_setbits32(base + _TAMP_SCR, -+ _TAMP_SR_ITAMP(id)); -+ ext_it &= ~_TAMP_SR_ITAMP(id); -+ -+ if (ret > 0) { -+#ifdef CFG_PM -+ stm32_cores_reset(); -+#else -+ psci_system_reset(); -+#endif -+ } -+ } -+ } -+ i++; -+ } -+ -+ i = 0; -+ /* External tamper interrupt */ -+ while ((ext_it != 0U) && (i < stm32_tamp.pdata.compat->ext_tamp_size)) { -+ int ret = -1; -+ uint32_t id = tamp->pdata.compat->ext_tamp[i].id; -+ -+ if ((ext_it & _TAMP_SR_ETAMP(id)) != 0U) { -+ if (tamp->pdata.compat->ext_tamp[i].func) -+ ret = tamp->pdata.compat->ext_tamp[i].func(id); -+ -+ if (ret >= 0) { -+ io_setbits32(base + _TAMP_SCR, -+ _TAMP_SCR_ETAMP(id)); -+ ext_it &= ~_TAMP_SR_ETAMP(id); -+ -+ if (ret > 0) { -+#ifdef CFG_PM -+ stm32_cores_reset(); -+#else -+ psci_system_reset(); -+#endif -+ } -+ } -+ } -+ i++; -+ } -+ -+ return ITRR_HANDLED; -+} -+ -+#ifdef CFG_DT -+static int get_node_from_multiple_compatible(const void *fdt, -+ struct stm32_tamp_platdata *pdata) -+{ -+ const struct my_dt_device_match *cur = tamp_match_table; -+ -+ while (cur->compatible) { -+ int node = -1; -+ -+ node = fdt_node_offset_by_compatible(fdt, node, -+ cur->compatible); -+ if (node >= 0) { -+ pdata->compat = (struct stm32_tamp_compat *)cur->data; -+ return node; -+ } -+ -+ cur++; -+ } -+ -+ /* Compatible string not found */ -+ return -1; -+} -+ -+static int stm32_tamp_parse_fdt(struct stm32_tamp_platdata *pdata) -+{ -+ int node = -1; -+ struct dt_node_info dt_tamp = { }; -+ void *fdt = NULL; -+ -+ fdt = get_embedded_dt(); -+ if (!fdt) -+ return -1; -+ -+ node = get_node_from_multiple_compatible(fdt, pdata); -+ if (node < 0) -+ return -1; -+ -+ _fdt_fill_device_info(fdt, &dt_tamp, node); -+ -+ if (dt_tamp.status == DT_STATUS_DISABLED || -+ dt_tamp.clock == DT_INFO_INVALID_CLOCK || -+ dt_tamp.reg == DT_INFO_INVALID_REG || -+ dt_tamp.interrupt == DT_INFO_INVALID_INTERRUPT) { -+ return -1; -+ } -+ -+ pdata->it = dt_tamp.interrupt; -+ pdata->base.pa = dt_tamp.reg; -+ pdata->base.va = (vaddr_t)phys_to_virt(dt_tamp.reg, MEM_AREA_IO_SEC); -+ -+ pdata->clock = dt_tamp.clock; -+ -+ pdata->pins_conf = 0; -+ if (stm32_pinctrl_fdt_get_pinctrl(fdt, node, NULL, 0) > 0) { -+ if (pdata->compat == &mp15_compat) { -+ if (fdt_getprop(fdt, node, "st,out3-pc13", NULL)) -+ pdata->pins_conf |= _TAMP_OR_OUT3RMP_PC13; -+ } -+ } -+ -+ if (fdt_getprop(fdt, node, "wakeup-source", NULL)) -+ pdata->is_wakeup_source = true; -+ -+ return 0; -+} -+ -+__weak -+int stm32_tamp_get_platdata(struct stm32_tamp_platdata *pdata __unused) -+{ -+ /* In DT config, the platform data are filled by DT file */ -+ return 0; -+} -+#else /* CFG_DT */ -+static int stm32_tamp_parse_fdt(struct stm32_tamp_platdata *pdata) -+{ -+ /* Do nothing, there is no fdt to parse in this case */ -+ return 0; -+} -+ -+/* This function can be overridden by platform to define pdata of tamp driver */ -+__weak -+int stm32_tamp_get_platdata(struct stm32_tamp_platdata *pdata __unused) -+{ -+ return -1; -+} -+#endif /* CFG_DT */ -+ -+static struct itr_handler stm32_tamp_itr_handler = { -+ .handler = stm32_tamp_it_handler, -+}; -+DECLARE_KEEP_PAGER(stm32_tamp_itr_handler); -+ -+static TEE_Result stm32_tamp_init(void) -+{ -+ int ret = TEE_SUCCESS; -+ vaddr_t base = 0; -+ uint32_t __unused rev = 0; -+ -+ ret = stm32_tamp_get_platdata(&stm32_tamp.pdata); -+ if (ret) -+ return TEE_ERROR_NOT_SUPPORTED; -+ -+ ret = stm32_tamp_parse_fdt(&stm32_tamp.pdata); -+ if (ret) -+ return TEE_ERROR_NOT_SUPPORTED; -+ -+ /* Init Tamp clock */ -+ clk_enable(stm32_tamp.pdata.clock); -+ -+ base = io_pa_or_va(&stm32_tamp.pdata.base); -+ -+ stm32_tamp.hwconf1 = io_read32(base + _TAMP_HWCFGR1); -+ stm32_tamp.hwconf2 = io_read32(base + _TAMP_HWCFGR2); -+ -+#if TRACE_LEVEL >= TRACE_FLOW -+ rev = io_read32(base + _TAMP_VERR); -+ FMSG("STM32 TAMPER V%u.%u", (rev & _TAMP_VERR_MAJREV) >> 4, -+ rev & _TAMP_VERR_MINREV); -+#endif -+ -+ if ((stm32_tamp.hwconf2 & _TAMP_HWCFGR2_TZ) == 0U) { -+ EMSG("Tamper IP doesn't support trustzone"); -+ return TEE_ERROR_NOT_SUPPORTED; -+ } -+ -+ stm32_tamp_set_pins(base, stm32_tamp.pdata.pins_conf); -+ -+ stm32_tamp_itr_handler.it = stm32_tamp.pdata.it; -+ stm32_tamp_itr_handler.data = &stm32_tamp; -+ itr_add(&stm32_tamp_itr_handler); -+ itr_enable(stm32_tamp_itr_handler.it); -+ -+ /* Need a new API */ -+ if (stm32_tamp.pdata.is_wakeup_source) -+ IMSG("TAMP event are not configured as wakeup source"); -+ -+ return TEE_SUCCESS; -+} -+ -+driver_init(stm32_tamp_init); -diff --git a/core/drivers/sub.mk b/core/drivers/sub.mk -index 0d3b81e21..10a641b9d 100644 ---- a/core/drivers/sub.mk -+++ b/core/drivers/sub.mk -@@ -30,6 +30,7 @@ srcs-$(CFG_STM32_IWDG) += stm32_iwdg.c - srcs-$(CFG_STM32_RNG) += stm32_rng.c - srcs-$(CFG_STM32_RTC) += stm32_rtc.c - srcs-$(CFG_STM32_TIM) += stm32_tim.c -+srcs-$(CFG_STM32_TAMP) += stm32_tamp.c - srcs-$(CFG_STM32_UART) += stm32_uart.c - srcs-$(CFG_STPMIC1) += stpmic1.c - srcs-$(CFG_BCM_HWRNG) += bcm_hwrng.c -diff --git a/core/include/drivers/stm32_tamp.h b/core/include/drivers/stm32_tamp.h -new file mode 100644 -index 000000000..556cd136d ---- /dev/null -+++ b/core/include/drivers/stm32_tamp.h -@@ -0,0 +1,378 @@ -+/* SPDX-License-Identifier: BSD-3-Clause */ -+/* -+ * Copyright (c) 2021, STMicroelectronics -+ */ -+ -+#ifndef __STM32_TAMP_H__ -+#define __STM32_TAMP_H__ -+ -+#include -+#include -+#include -+#include -+#include -+ -+/* Internal Tamper */ -+enum stm32_tamp_int_id { -+ INT_TAMP1 = 0, -+ INT_TAMP2, -+ INT_TAMP3, -+ INT_TAMP4, -+ INT_TAMP5, -+ INT_TAMP6, -+ INT_TAMP7, -+ INT_TAMP8, -+ INT_TAMP9, -+ INT_TAMP10, -+ INT_TAMP11, -+ INT_TAMP12, -+ INT_TAMP13, -+ INT_TAMP14, -+ INT_TAMP15, -+ INT_TAMP16 -+}; -+ -+/* External Tamper */ -+enum stm32_tamp_ext_id { -+ EXT_TAMP1 = 0, -+ EXT_TAMP2, -+ EXT_TAMP3, -+ EXT_TAMP4, -+ EXT_TAMP5, -+ EXT_TAMP6, -+ EXT_TAMP7, -+ EXT_TAMP8 -+}; -+ -+/* Out pin to compare for external Tamper */ -+enum stm32_tamp_ext_out_id { -+ TAMPOUTSEL_SAME_AS_INPUT = 0, -+ TAMPOUTSEL1, -+ TAMPOUTSEL2, -+ TAMPOUTSEL3, -+ TAMPOUTSEL4, -+ TAMPOUTSEL5, -+ TAMPOUTSEL6, -+ TAMPOUTSEL7, -+ TAMPOUTSEL8, -+}; -+ -+/* -+ * Define number of backup registers in zone 1 and zone 2 (remaining are in -+ * zone 3) -+ * -+ * backup registers in zone 1 : read/write only in secure mode -+ * zone 2 : write only in secure mode, read in secure -+ * and non-secure mode -+ * zone 3 : read/write in secure and non-secure mode -+ * -+ * Protection zone 1 if nb_zone1_regs == 0 no backup register are in zone 1 -+ * else backup registers from TAMP_BKP0R to TAMP_BKPxR -+ * with x = nb_zone1_regs - 1 are in zone 1. -+ * Protection zone 2 if nb_zone2_regs == 0 no backup register are in zone 2 -+ * else backup registers from -+ * TAMP_BKPyR with y = nb_zone1_regs -+ * to -+ * TAMP_BKPzR with z = (nb_zone1_regs1 + nb_zone2_regs - 1) -+ * are in zone 2. -+ * Protection zone 3 backup registers from TAMP_BKPtR -+ * with t = nb_zone1_regs1 + nb_zone2_regs to last backup -+ * register are in zone 3. -+ */ -+struct stm32_bkpregs_conf { -+ uint32_t nb_zone1_regs; -+ uint32_t nb_zone2_regs; -+}; -+ -+/* Define TAMPER modes */ -+#define TAMP_DISABLE 0x0U -+#define TAMP_ENABLE 0x1U -+#define TAMP_TRIG_OFF 0x0U -+#define TAMP_TRIG_ON 0x2U -+#define TAMP_PASSIVE 0x0U -+#define TAMP_ACTIVE 0x4U -+#define TAMP_ERASE 0x0U -+#define TAMP_NOERASE 0x8U -+#define TAMP_NO_EVT_MASK 0x0U -+#define TAMP_EVT_MASK 0x10U -+ -+/* Define Passive FILTER mode */ -+#define TAMP_FILTER_TAMPPUDIS_OFFSET 7U -+#define TAMP_FILTER_PRECHARGE (0x0U << TAMP_FILTER_TAMPPUDIS_OFFSET) -+#define TAMP_FILTER_PULL_UP_DISABLE (0x1U << TAMP_FILTER_TAMPPUDIS_OFFSET) -+#define TAMP_FILTER_TAMPPRCH_OFFSET 5U -+#define TAMP_FILTER_PRECHARGE_1_CYCLE (0x0U << TAMP_FILTER_TAMPPRCH_OFFSET) -+#define TAMP_FILTER_PRECHARGE_2_CYCLES (0x1U << TAMP_FILTER_TAMPPRCH_OFFSET) -+#define TAMP_FILTER_PRECHARGE_4_CYCLES (0x2U << TAMP_FILTER_TAMPPRCH_OFFSET) -+#define TAMP_FILTER_PRECHARGE_8_CYCLES (0x3U << TAMP_FILTER_TAMPPRCH_OFFSET) -+#define TAMP_FILTER_TAMPFLT_OFFSET 3U -+#define TAMP_FILTER_EDGE (0x0U << TAMP_FILTER_TAMPFLT_OFFSET) -+#define TAMP_FILTER_2_SAMPLE (0x1U << TAMP_FILTER_TAMPFLT_OFFSET) -+#define TAMP_FILTER_4_SAMPLE (0x2U << TAMP_FILTER_TAMPFLT_OFFSET) -+#define TAMP_FILTER_8_SAMPLE (0x3U << TAMP_FILTER_TAMPFLT_OFFSET) -+#define TAMP_FILTER_TAMPFREQ_OFFSET 0U -+#define TAMP_FILTER_SAMPLING_32768 (0x0U << TAMP_FILTER_TAMPFREQ_OFFSET) -+#define TAMP_FILTER_SAMPLING_16384 (0x1U << TAMP_FILTER_TAMPFREQ_OFFSET) -+#define TAMP_FILTER_SAMPLING_8192 (0x2U << TAMP_FILTER_TAMPFREQ_OFFSET) -+#define TAMP_FILTER_SAMPLING_4096 (0x3U << TAMP_FILTER_TAMPFREQ_OFFSET) -+#define TAMP_FILTER_SAMPLING_2048 (0x4U << TAMP_FILTER_TAMPFREQ_OFFSET) -+#define TAMP_FILTER_SAMPLING_1024 (0x5U << TAMP_FILTER_TAMPFREQ_OFFSET) -+#define TAMP_FILTER_SAMPLING_512 (0x6U << TAMP_FILTER_TAMPFREQ_OFFSET) -+#define TAMP_FILTER_SAMPLING_256 (0x7U << TAMP_FILTER_TAMPFREQ_OFFSET) -+ -+/* Define active filter */ -+#define TAMP_ACTIVE_FLTEN_OFFSET 31U -+#define TAMP_ACTIVE_FILTER_OFF (0x0U << TAMP_ACTIVE_FLTEN_OFFSET) -+#define TAMP_ACTIVE_FILTER_ON (0x1U << TAMP_ACTIVE_FLTEN_OFFSET) -+#define TAMP_ACTIVE_ATOSHARE_OFFSET 30U -+#define TAMP_ACTIVE_USE_DEDICATED_OUT (0x0U << \ -+ TAMP_ACTIVE_FILTER_ATOSHARE_OFFSET) -+#define TAMP_ACTIVE_SELECT_OUT (0x1U << \ -+ TAMP_ACTIVE_FILTER_ATOSHARE_OFFSET) -+#define TAMP_ACTIVE_ATPER_OFFSET 24U -+#define TAMP_ACTIVE_ATPER_1_OUTPUT (0x0U << TAMP_ACTIVE_ATPER_OFFSET) -+#define TAMP_ACTIVE_ATPER_2_OUTPUTS (0x1U << TAMP_ACTIVE_ATPER_OFFSET) -+#define TAMP_ACTIVE_ATPER_3_4_OUTPUTS (0x2U << TAMP_ACTIVE_ATPER_OFFSET) -+#define TAMP_ACTIVE_ATPER_5_OUTPUTS (0x3U << TAMP_ACTIVE_ATPER_OFFSET) -+#define TAMP_ACTIVE_ATCKSEL_OFFSET 16U -+#define TAMP_ACTIVE_CKSEL_DIV_0 (0x0U << TAMP_ACTIVE_ATCKSEL_OFFSET) -+#define TAMP_ACTIVE_CKSEL_DIV_2 (0x1U << TAMP_ACTIVE_ATCKSEL_OFFSET) -+#define TAMP_ACTIVE_CKSEL_DIV_4 (0x2U << TAMP_ACTIVE_ATCKSEL_OFFSET) -+#define TAMP_ACTIVE_CKSEL_DIV_8 (0x3U << TAMP_ACTIVE_ATCKSEL_OFFSET) -+#define TAMP_ACTIVE_CKSEL_DIV_16 (0x4U << TAMP_ACTIVE_ATCKSEL_OFFSET) -+#define TAMP_ACTIVE_CKSEL_DIV_32 (0x5U << TAMP_ACTIVE_ATCKSEL_OFFSET) -+#define TAMP_ACTIVE_CKSEL_DIV_64 (0x6U << TAMP_ACTIVE_ATCKSEL_OFFSET) -+#define TAMP_ACTIVE_CKSEL_DIV_128 (0x7U << TAMP_ACTIVE_ATCKSEL_OFFSET) -+ -+/* Define device secret list */ -+#define TAMP_NON_CONFIGURABLE_SECRETS 0x0U -+#define TAMP_CONF_SECRET_BACKUP_SRAM 0x1U -+ -+/* Define secure mode access */ -+/* -+ * Tamper configuration and interrupt can be written when the APB access is -+ * secure or nonsecure. -+ */ -+#define TAMP_REGS_IT_UNSECURE 0U -+/* -+ * Tamper configuration and interrupt can be written only when the APB access -+ * is secure. -+ */ -+#define TAMP_REGS_IT_SECURE BIT(31) -+/* -+ * Tamper monotonic counter 1 can be written when the APB access is secure or -+ * nonsecure. -+ */ -+#define TAMP_CNT1_UNSECURE 0U -+/* -+ * Tamper monotonic counter 3 can be written only when the APB access is -+ * secure. -+ */ -+#define TAMP_CNT1_SECURE BIT(15) -+/* -+ * Tamper monotonic counter 2 can be written when the APB access is secure or -+ * nonsecure. -+ */ -+#define TAMP_CNT2_UNSECURE 0U -+/* -+ * Tamper monotonic counter 3 can be written only when the APB access is -+ * secure. -+ */ -+#define TAMP_CNT2_SECURE BIT(14) -+ -+/* Define privilege mode access */ -+/* -+ * Tamper registers (but backup and cnt) can be written with privileged or -+ * unprivileged access. -+ */ -+#define TAMP_REGS_UNPRIVILEGE 0U -+/* -+ * Tamper registers (but backupand cnt) can be written only with privileged -+ * access. -+ */ -+#define TAMP_REGS_PRIVILEGE BIT(31) -+/* -+ * Backup registers zone 2 can be written with privileged or unprivileged -+ * access. -+ */ -+#define TAMP_BKP2W_UNPRIVILEGE 0U -+/* Backup registers zone 2 can be written only with privileged access. */ -+#define TAMP_BKP2W_PRIVILEGE BIT(30) -+/* -+ * Backup registers zone 1 can be read and written with privileged or -+ * unprivileged access. -+ */ -+#define TAMP_BKP1RW_UNPRIVILEGE 0U -+/* -+ * Backup registers zone 1 can be read and written only with privileged -+ * access. -+ */ -+#define TAMP_BKP1RW_PRIVILEGE BIT(29) -+/* -+ * Monotonic counter 1 can be read and written with privileged or unprivileged -+ * access. -+ */ -+#define TAMP_CNT1_UNPRIVILEGE 0U -+/* Monotonic counter 1 can be read and written only with privileged access. */ -+#define TAMP_CNT1_PRIVILEGE BIT(15) -+/* -+ * Monotonic counter 2 can be read and written with privileged or unprivileged -+ * access. -+ */ -+#define TAMP_CNT2_UNPRIVILEGE 0U -+/* Monotonic counter 2 can be read and written only with privileged access. */ -+#define TAMP_CNT2_PRIVILEGE BIT(14) -+ -+/* -+ * stm32_tamp_write_mcounter : Increase monotonic counter[counter_idx]. -+ */ -+TEE_Result stm32_tamp_write_mcounter(int counter_idx); -+uint32_t stm32_tamp_read_mcounter(int counter_idx); -+ -+bool stm32_tamp_are_secrets_blocked(void); -+void stm32_tamp_block_secrets(void); -+void stm32_tamp_unblock_secrets(void); -+void stm32_tamp_erase_secrets(void); -+void stm32_tamp_lock_boot_hardware_key(void); -+ -+/* -+ * stm32_tamp_configure_secret_list: Configure which extra IPs -+ * are blocked/erased in case of tamper event. -+ * -+ * secret_list_conf is a bit field from TAMP_CONF_SECRET_.* -+ */ -+void stm32_tamp_configure_secret_list(uint32_t secret_list_conf); -+ -+/* -+ * stm32_tamp_configure_secure_access: Configure which registers can be -+ * read/write from unsecure world. -+ * secure_conf is a bit field from TAMP_.*_{UN,}SECURE define -+ */ -+void stm32_tamp_configure_secure_access(uint32_t secure_conf); -+ -+/* -+ * stm32_tamp_configure_privilege_access: Configure which registers can be -+ * read/write from unpriviliged world. -+ * privilege_conf is a bit field from TAMP_.*_{UN,}PRIVILEGE define -+ */ -+void stm32_tamp_configure_privilege_access(uint32_t privilege_conf); -+ -+/* -+ * stm32_tamp_configure_passive: Configure passive mode -+ * passive_conf is a bit field from TAMP_FILTER_* define -+ */ -+void stm32_tamp_configure_passive(uint32_t passive_conf); -+ -+/* -+ * stm32_tamp_configure_ctive: Configure active mode -+ * passive_conf is a bit field from TAMP_ACTIVE_* define -+ */ -+void stm32_tamp_configure_active(uint32_t active_conf); -+ -+/* -+ * stm32_tamp_configure_internal: Configure one internal tamper. -+ * id: internal tamper id -+ * mode: bitmask from TAMPER modes define -+ * callback: function to call when tamper is raised (can be NULL), -+ * called in interrupt context, -+ * if callback returns negative value, blocked secrets stay blocked -+ * (driver doesn't release this specific tamper). -+ * if callback returns 0 this specific tamp is ack (in case of -+ * no-erase tamper, blocked secret are unblocked) -+ * if callback returns positive value, this specific tamp is ack (in -+ * case of no-erase tamper, blocked secret are unblocked) and system -+ * is rebooted). -+ * -+ * return: TEE_BAD_PARAMETERS if 'id' is not a valid internal tamp id, -+ * else TEE_SUCCESS. -+ */ -+TEE_Result stm32_tamp_configure_internal(enum stm32_tamp_int_id id, -+ uint32_t mode, -+ int (*callback)(int id)); -+ -+/* -+ * stm32_tamp_configure_external: Configure one external tamper. -+ * id: external tamper id -+ * mode: bitmask from TAMPER modes define -+ * pin_out; output pin connected to input pin (linekd with selected ext tamp id) -+ * callback: function to call when tamper is raised (can be NULL), -+ * called in interrupt context, -+ * if callback returns negative value, blocked secrets stay blocked -+ * (driver doesn't release this specific tamper). -+ * if callback returns 0 this specific tamp is ack (in case of -+ * no-erase tamper, blocked secret are unblocked) -+ * if callback returns positive value, this specific tamp is ack (in -+ * case of no-erase tamper, blocked secret are unblocked) and system -+ * is rebooted). -+ * -+ * return: TEE_BAD_PARAMETERS if 'id' is not a valid external tamp id, -+ * else TEE_SUCCESS. -+ */ -+TEE_Result stm32_tamp_configure_external(enum stm32_tamp_ext_id id, -+ uint32_t mode, -+ enum stm32_tamp_ext_out_id out_pin, -+ int (*callback)(int id)); -+ -+/* -+ * stm32_tamp_set_secure_bkprwregs : Configure backup registers zone. -+ * registers in zone 1 : read/write only in secure mode -+ * zone 2 : write only in secure mode, read in secure and -+ * non-secure mode -+ * zone 3 : read/write in secure and non-secure mode -+ * -+ * bkpregs_conf : a pointer to struct bkpregs_conf that define the number of -+ * registers in zone 1 and zone 2 (remaining backup registers will be in -+ * zone 3). -+ * -+ * return TEE_ERROR_NOT_SUPPORTED: if zone 1 and/or zone 2 definition are out -+ * of range. -+ * TEE_ERROR_BAD_PARAMETERS: if bkpregs_cond is NULL. -+ * TEE_SUCCESS : if OK. -+ */ -+TEE_Result stm32_tamp_set_secure_bkpregs(struct stm32_bkpregs_conf -+ *bkpregs_conf); -+ -+/* -+ * stm32_tamp_set_config: Apply configuration. -+ * Default one if no previous call to any of : -+ * stm32_tamp_configure_passive() -+ * stm32_tamp_configure_active() -+ * stm32_tamp_configure_internal() -+ * stm32_tamp_configure_external() -+ * stm32_tamp_configure_secret_list() -+ * stm32_tamp_configure_secure_access() -+ * stm32_tamp_configure_privilege_access() -+ * -+ */ -+TEE_Result stm32_tamp_set_config(void); -+ -+/* Compatibility tags */ -+#define TAMP_HAS_CONF_SECURE BIT(0) -+#define TAMP_HAS_CONF_PRIVILEGE BIT(1) -+#define TAMP_HAS_CONF_SECRET_IPS BIT(2) -+#define TAMP_HAS_ATCR2 BIT(3) -+#define TAMP_HAS_CR3 BIT(4) -+#define TAMP_HAS_SECRET_STATUS BIT(5) -+#define TAMP_HAS_LOCK_KEY BIT(6) -+ -+struct stm32_tamp_compat { -+ int nb_monotonic_counter; -+ uint32_t tags; -+ struct stm32_tamp_int *int_tamp; -+ uint32_t int_tamp_size; -+ struct stm32_tamp_ext *ext_tamp; -+ uint32_t ext_tamp_size; -+}; -+ -+struct stm32_tamp_platdata { -+ struct io_pa_va base; -+ uint32_t clock; -+ int it; -+ uint32_t pins_conf; -+ bool is_wakeup_source; -+ struct stm32_tamp_compat *compat; -+}; -+ -+int stm32_tamp_get_platdata(struct stm32_tamp_platdata *pdata); -+ -+#endif /* __STM32_TAMP_H__ */ -diff --git a/core/include/kernel/dt.h b/core/include/kernel/dt.h -index 3044989c7..e9f816488 100644 ---- a/core/include/kernel/dt.h -+++ b/core/include/kernel/dt.h -@@ -31,12 +31,15 @@ - * @reg: Device register physical base address or DT_INFO_INVALID_REG - * @clock: Device identifier (positive value) or DT_INFO_INVALID_CLOCK - * @reset: Device reset identifier (positive value) or DT_INFO_INVALID_CLOCK -+ * @interrupt: Device interrupt identifier (positive value) or -+ * DT_INFO_INVALID_INTERRUPT - */ - struct dt_node_info { - unsigned int status; - paddr_t reg; - int clock; - int reset; -+ int interrupt; - }; - - #if defined(CFG_DT) -@@ -96,18 +99,6 @@ int dt_map_dev(const void *fdt, int offs, vaddr_t *base, size_t *size); - */ - bool dt_have_prop(const void *fdt, int offs, const char *propname); - --/* -- * Get the DT interrupt property of the @node. In the DT an interrupt -- * is defined with at least 2x32 bits detailling the interrupt number and type. -- * -- * @fdt reference to the Device Tree -- * @node is the node offset to read -- * -- * Returns the interrupt number if value >= 0 -- * otherwise DT_INFO_INVALID_INTERRUPT -- */ --int dt_get_irq(void *fdt, int node); -- - /* - * Modify or add "status" property to "disabled" - * -diff --git a/core/include/kernel/interrupt.h b/core/include/kernel/interrupt.h -index 796eded29..4a0b4474b 100644 ---- a/core/include/kernel/interrupt.h -+++ b/core/include/kernel/interrupt.h -@@ -14,6 +14,7 @@ - - struct itr_chip { - const struct itr_ops *ops; -+ int (*dt_get_irq)(const uint32_t *properties, int len); - }; - - struct itr_ops { -@@ -48,6 +49,30 @@ struct itr_handler { - void itr_init(struct itr_chip *data); - void itr_handle(size_t it); - -+#ifdef CFG_DT -+/* -+ * Get the DT interrupt property at @node. In the DT an interrupt -+ * -+ * @fdt reference to the Device Tree -+ * @node is the node offset to read -+ * -+ * Returns the interrupt number if value >= 0 -+ * otherwise DT_INFO_INVALID_INTERRUPT -+ */ -+int dt_get_irq(const void *fdt, int node); -+ -+/* -+ * Get the DT secure-interrupt property at @node. -+ * -+ * @fdt reference to the Device Tree -+ * @node is the node offset to read -+ * -+ * Returns the interrupt number if value >= 0 -+ * otherwise DT_INFO_INVALID_INTERRUPT -+ */ -+int dt_get_irq_secure(const void *fdt, int node); -+#endif -+ - void itr_add(struct itr_handler *handler); - void itr_enable(size_t it); - void itr_disable(size_t it); -diff --git a/core/kernel/dt.c b/core/kernel/dt.c -index fcfa26929..4410bf52b 100644 ---- a/core/kernel/dt.c -+++ b/core/kernel/dt.c -@@ -5,6 +5,7 @@ - - #include - #include -+#include - #include - #include - #include -@@ -51,27 +52,6 @@ bool dt_have_prop(const void *fdt, int offs, const char *propname) - return prop; - } - --int dt_get_irq(void *fdt, int node) --{ -- const uint32_t *int_prop = NULL; -- int len_prop = 0; -- int it_num = DT_INFO_INVALID_INTERRUPT; -- -- /* -- * Interrupt property can be defined with at least 2x32 bits word -- * - Type of interrupt -- * - Interrupt Number -- */ -- int_prop = fdt_getprop(fdt, node, "interrupts", &len_prop); -- -- if (!int_prop || len_prop < 2) -- return it_num; -- -- it_num = fdt32_to_cpu(int_prop[1]); -- -- return it_num; --} -- - int dt_disable_status(void *fdt, int node) - { - const char *prop = NULL; -@@ -288,6 +268,7 @@ void _fdt_fill_device_info(void *fdt, struct dt_node_info *info, int offs) - .reg = DT_INFO_INVALID_REG, - .clock = DT_INFO_INVALID_CLOCK, - .reset = DT_INFO_INVALID_RESET, -+ .interrupt = DT_INFO_INVALID_INTERRUPT, - }; - const fdt32_t *cuint; - -@@ -305,6 +286,11 @@ void _fdt_fill_device_info(void *fdt, struct dt_node_info *info, int offs) - dinfo.reset = (int)fdt32_to_cpu(*cuint); - } - -+ /* We first look for the secure interrupt, if not found the interrupt */ -+ dinfo.interrupt = dt_get_irq_secure(fdt, offs); -+ if (dinfo.interrupt == DT_INFO_INVALID_INTERRUPT) -+ dinfo.interrupt = dt_get_irq(fdt, offs); -+ - dinfo.status = _fdt_get_status(fdt, offs); - - *info = dinfo; -diff --git a/core/kernel/interrupt.c b/core/kernel/interrupt.c -index 376012556..61c299745 100644 ---- a/core/kernel/interrupt.c -+++ b/core/kernel/interrupt.c -@@ -3,8 +3,10 @@ - * Copyright (c) 2016-2019, Linaro Limited - */ - -+#include - #include - #include -+#include - #include - #include - -@@ -25,6 +27,34 @@ void itr_init(struct itr_chip *chip) - itr_chip = chip; - } - -+#ifdef CFG_DT -+static int _dt_get_irq(const void *fdt, int node, const char *prop_name) -+{ -+ const uint32_t *prop = NULL; -+ int len = 0; -+ int it_num = DT_INFO_INVALID_INTERRUPT; -+ -+ if (!itr_chip || !itr_chip->dt_get_irq) -+ return it_num; -+ -+ prop = fdt_getprop(fdt, node, prop_name, &len); -+ if (!prop) -+ return it_num; -+ -+ return itr_chip->dt_get_irq(prop, len); -+} -+ -+int dt_get_irq(const void *fdt, int node) -+{ -+ return _dt_get_irq(fdt, node, "interrupts"); -+} -+ -+int dt_get_irq_secure(const void *fdt, int node) -+{ -+ return _dt_get_irq(fdt, node, "secure-interrupts"); -+} -+#endif -+ - void itr_handle(size_t it) - { - struct itr_handler *h = NULL; -diff --git a/mk/config.mk b/mk/config.mk -index a14c4f832..c92a0edce 100644 ---- a/mk/config.mk -+++ b/mk/config.mk -@@ -111,7 +111,7 @@ CFG_TEE_IMPL_DESCR ?= OPTEE - CFG_OS_REV_REPORTS_GIT_SHA1 ?= y - - # Trusted OS implementation version --TEE_IMPL_VERSION ?= $(shell git describe --always --dirty=-dev 2>/dev/null || echo Unknown) -+TEE_IMPL_VERSION ?= $(shell git describe --always --tags --dirty=-dev 2>/dev/null || echo Unknown) - ifeq ($(CFG_OS_REV_REPORTS_GIT_SHA1),y) - TEE_IMPL_GIT_SHA1 := 0x$(shell git rev-parse --short=8 HEAD 2>/dev/null || echo 0) - else --- -2.17.1 - diff --git a/recipes-security/optee/optee-os/README.HOW_TO.txt b/recipes-security/optee/optee-os/README.HOW_TO.txt index 82fdbff..e694ff1 100644 --- a/recipes-security/optee/optee-os/README.HOW_TO.txt +++ b/recipes-security/optee/optee-os/README.HOW_TO.txt @@ -1,20 +1,22 @@ Compilation of Optee-os (Trusted Execution Environment): 1. Pre-requisite -2. Initialise cross-compilation via SDK +2. Initialize cross-compilation via SDK 3. Prepare optee-os source code -4. Management of optee-os source code +4. Manage optee-os source code 5. Compile optee-os source code 6. Update software on board -1. Pre-requisite: ------------------ +---------------- +1. Pre-requisite +---------------- OpenSTLinux SDK must be installed. If you have never configured you git configuration: $ git config --global user.name "your_name" $ git config --global user.email "your_email@example.com" -2. Initialise cross-compilation via SDK: +--------------------------------------- +2. Initialize cross-compilation via SDK --------------------------------------- Source SDK environment: $ source /environment-setup-cortexa7t2hf-neon-vfpv4-ostl-linux-gnueabi @@ -26,25 +28,39 @@ If you have never configured you git configuration: Warning: the environment are valid only on the shell session where you have sourced the sdk environment. -3. Prepare optee-os source: ------------------------- -If you have the tarball and the list of patch then you must extract the -tarball and apply the patch. - $> tar xfz ##BP##-##PR##.tar.gz -A new directory containing optee standard source code will be created, go into it: - $> cd ##BP## +-------------------------- +3. Prepare optee-os source +-------------------------- +If not already done, extract the sources from Developer Package tarball, for example: +$ tar xfJ en.SOURCES-stm32mp1-*.tar.xz -NB: if there is no git management on source code and you would like to have a git management -on the code see section 4 [Management of optee-os source code] - if there is some patch, please apply it on source code +In the optee-os source directory (sources/*/##BP##-##PR##), +you have one optee-os source tarball, the patches and one Makefile: + - ##BP##-##PR##.tar.xz + - 00*.patch + - Makefile.sdk + +If you would like to have a git management for the source code move to +to section 4 [Management of optee-os source code with GIT]. + +Otherwise, to manage optee-os source code without git, you must extract the +tarball now and apply the patch: + + $> tar xf ##BP##-##PR##.tar.xz + $> cd ##BP## + $> tar xfz ../fonts.tar.gz $> for p in `ls -1 ../*.patch`; do patch -p1 < $p; done -4. Management of optee-os source code: ------------------------------------ +You can now move to section 5 [Compile optee-os source code]. + +--------------------------------------- +4. Manage optee-os source code with GIT +--------------------------------------- If you like to have a better management of change made on optee-os source, you have 3 solutions to use git 4.1 Get STMicroelectronics optee-os source from GitHub +------------------------------------------------------ URL: https://github.com/STMicroelectronics/optee_os.git Branch: ##ARCHIVER_ST_BRANCH## Revision: ##ARCHIVER_ST_REVISION## @@ -53,33 +69,39 @@ you have 3 solutions to use git $ git checkout -b WORKING ##ARCHIVER_ST_REVISION## 4.2 Create Git from tarball +--------------------------- $ cd $ test -d .git || git init . && git add . && git commit -m "optee-os source code" && git gc $ git checkout -b WORKING - $ for p in `ls -1 /*.patch`; do git am $p; done + $ tar xfz ../fonts.tar.gz + $ for p in `ls -1 ../*.patch`; do git am $p; done MANDATORY: You must update sources $ cd $ chmod 755 scripts/bin_to_c.py 4.3 Get Git from community and apply STMicroelectronics patches +--------------------------------------------------------------- +* With the optee-os source code from the OP-TEE git repositories: URL: git://github.com/OP-TEE/optee_os.git Branch: ##ARCHIVER_COMMUNITY_BRANCH## Revision: ##ARCHIVER_COMMUNITY_REVISION## $ git clone git://github.com/OP-TEE/optee_os.git - $ cd + $ cd optee_os $ git checkout -b WORKING ##ARCHIVER_COMMUNITY_REVISION## + $ tar xfz /fonts.tar.gz $ for p in `ls -1 /*.patch`; do git am $p; done MANDATORY: You must update sources $ cd $ chmod 755 scripts/bin_to_c.py -5. Build optee-os source code: --------------------------------- -Since OpenSTLinux has activated FIP by default, so the FIP_artifacts should be specified before launching compilation - - In case of using SOURCES-xxxx.tar.gz of Developer package the FIP_DEPLOYDIR_ROOT should be set as below: +------------------------------- +5. Compile optee-os source code +------------------------------- +Since OpenSTLinux activates FIP by default, FIP_artifacts directory path must be specified before launching compilation + - In case of using SOURCES-xxxx.tar.gz of Developer package the FIP_DEPLOYDIR_ROOT must be set as below: $> export FIP_DEPLOYDIR_ROOT=$PWD/../../FIP_artifacts To compile optee-os source code @@ -87,10 +109,14 @@ To compile optee-os source code or for a specific config : $ make -f $PWD/../Makefile.sdk CFG_EMBED_DTB_SOURCE_FILE=stm32mp157c-ev1 all +By default, the build results for this component are available in $PWD/../deploy directory. +If needed, this deploy directory can be specified by added "DEPLOYDIR=" compilation option to the build command line above. +In case DEPLOYDIR=$FIP_DEPLOYDIR_ROOT/optee it overwrites files directly in FIP artifacts directory. + The generated FIP images are available in $FIP_DEPLOYDIR_ROOT/fip - -6. Update software on board: ----------------------------- +--------------------------- +6. Update software on board +--------------------------- Please use STM32CubeProgrammer and only tick the ssbl-boot and fip partitions (more informations on the wiki website http://wiki.st.com/stm32mpu) diff --git a/recipes-security/optee/optee-os/fonts.tar.gz b/recipes-security/optee/optee-os/fonts.tar.gz new file mode 100644 index 0000000000000000000000000000000000000000..0a564c426623f933d37b52c642ca369490d4b814 GIT binary patch literal 398382 zcmY(pV{m0%7cCsy>ezPY#O|nL+s27HX2-T|2<9<@{UogxQ{d(~r9{;d)ypdK(R4n*D=Nx_>%_{? z;^6`mKlA7$B^4v`q+@5E-?lS7-VO*$DS!Bl__TBYu7B@5Ki6<|YpgSG?^+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