mirror of
https://github.com/openharmony/tee_tee_os_framework.git
synced 2026-07-20 23:46:14 -04:00
no message
Match-id-75051cd762e311e3b67da88c0002898bcf67c006
This commit is contained in:
+7
-20
@@ -103,17 +103,16 @@ tees: setup_links libs $(drivers) $(frameworks) $(service)
|
||||
$(drivers): setup_links libs link_libs
|
||||
$(frameworks): setup_links libs link_libs
|
||||
$(service): setup_links libs link_libs
|
||||
package: hmfilemgr
|
||||
package: $(STAGE_DIR)/bootfs.img
|
||||
cp -a $(PLAT_AUTOCONF_FILE) $(TOPDIR)/../tee_os_kernel/include/generated/.
|
||||
cp -a $(PLAT_CFG_DIR)/plat_cfg.c $(TOPDIR)/../tee_os_kernel/build/tools/generate_img
|
||||
cp -a $(PLAT_CFG_DIR)/plat_cfg.h $(TOPDIR)/../tee_os_kernel/build/tools/generate_img
|
||||
@mkdir $(FILEMGR_BOOT_ELF)
|
||||
cp -a $(STAGE_DIR)/bootfs.img $(FILEMGR_BOOT_ELF)/.
|
||||
|
||||
PHONY += $(STAGE_DIR)/bootfs.img
|
||||
PHONY += link_libs link_arm_libs link_aarch64_libs
|
||||
link_libs: link_arm_libs link_aarch64_libs
|
||||
mkdir -p $(OUTPUTDIR)/elfloader
|
||||
mkdir -p $(OUTPUTDIR)/kernel
|
||||
cp -a $(OUTPUTDIR)/$(TEE_ARCH)/libs/elfloader.o $(OUTPUTDIR)/elfloader/.
|
||||
cp -a $(OUTPUTDIR)/$(TEE_ARCH)/libs/kernel.elf $(OUTPUTDIR)/kernel/.
|
||||
cp -a $(OUTPUTDIR)/$(TEE_ARCH)/libs/libuart.a $(OUTPUTDIR)/kernel/.
|
||||
cp -a $(OUTPUTDIR)/$(TEE_ARCH)/libs/libhardware.a $(OUTPUTDIR)/kernel/.
|
||||
cp -a $(OUTPUTDIR)/$(TEE_ARCH)/libs/libklibc.a $(OUTPUTDIR)/kernel/.
|
||||
|
||||
link_arm_libs: libs
|
||||
@echo "[link] libs=$(libs)"
|
||||
@@ -158,18 +157,6 @@ install_libs:
|
||||
if [ -e $$l ] && [ ! -L $$l ]; then cp -rf $$l $(LIBS_INSTALL_DIR); fi; \
|
||||
done
|
||||
|
||||
# packaging the image
|
||||
PHONY += hmfilemgr
|
||||
hmfilemgr: $(OUTPUTDIR)/$(TEE_ARCH)/apps/hmfilemgr
|
||||
$(STAGE_DIR)/bootfs.img:
|
||||
$(OUTPUTDIR)/$(TEE_ARCH)/apps/hmfilemgr: $(STAGE_DIR)/bootfs.img
|
||||
@echo "Building hmfilemgr"
|
||||
$(MAKE) -C $(BUILD_TOOLS)/hmfilemgr ARCH=$(TEE_ARCH) -j
|
||||
|
||||
PHONY += teehm.img trustedcore.img
|
||||
package: $(STAGE_DIR)/trustedcore.img
|
||||
@echo "!!generate trustedcore.img success"
|
||||
|
||||
PHONY += release
|
||||
release:
|
||||
@echo "!!compile release"
|
||||
|
||||
@@ -18,7 +18,6 @@ override APP_DIR := $(OUTPUTDIR)/$(ARCH)/apps
|
||||
override DRV_DIR := $(OUTPUTDIR)/$(ARCH)/drivers
|
||||
override HDR_L_DIR := $(OUTPUTDIR)/headers
|
||||
override KERNEL_OUTDIR := $(OUTPUTDIR)/kernel
|
||||
override ELFLOADER_OUTDIR := $(OUTPUTDIR)/elfloader
|
||||
|
||||
ifneq ($V,)
|
||||
VER :=
|
||||
@@ -53,12 +52,6 @@ STAGE_DIR := $(OUTPUTDIR)/stage
|
||||
|
||||
# selection of platform
|
||||
|
||||
ifeq ($(strip $(QUICK_BOOT_CHK)), true)
|
||||
WITH_TEEOS_ENCRYPT := false
|
||||
else
|
||||
WITH_TEEOS_ENCRYPT := true
|
||||
endif
|
||||
|
||||
ifeq ($(TARGET_BUILD_VARIANT),eng)
|
||||
WITH_ENG_VERSION = true
|
||||
else
|
||||
|
||||
@@ -98,55 +98,6 @@ $(service):
|
||||
$(if $(findstring true, $(CONFIG_SUPPORT_64BIT)), ,$(VER) LDFLAGS= $(MAKE) -C $(SERVICES_PATH)/$@ ARCH=arm TARG=_a32 USE_GNU_CXX=y -f $(PREBUILD_HEADER)/.config -f Makefile all)
|
||||
$(if $(findstring false, $(CONFIG_SUPPORT_64BIT)), ,$(VER) LDFLAGS= $(MAKE) -C $(SERVICES_PATH)/$@ ARCH=aarch64 -f $(PREBUILD_HEADER)/.config -f Makefile all)
|
||||
|
||||
COMPARE_IMAGE := 0
|
||||
WITH_LOG_ENCODE := false
|
||||
|
||||
# Add boot-apps here
|
||||
# NOTE: boot-apps will package to kernel.elf do not need to change
|
||||
boot-apps := $(OUTPUTDIR)/$(TEE_ARCH)/apps/hmfilemgr
|
||||
boot-apps += $(PREBUILD_LIBS)/$(TEE_ARCH)/hmsysmgr
|
||||
|
||||
HM_APPS_TOOLS := $(BUILD_TOOLS)/generate_img
|
||||
HM_APPS_LIBCPIO := $(TOPDIR)/../tee_os_kernel/libcore_shared/syslib/libcpio
|
||||
|
||||
.PHONY : cpio-strip
|
||||
cpio-strip :
|
||||
@echo "[$@] building..."
|
||||
$(Q)$(MAKE) $(MAKE_SILENT) -C $(HM_APPS_TOOLS)/$@ -f $(HM_APPS_TOOLS)/$@/Makefile \
|
||||
SOURCE_DIR=$(HM_APPS_TOOLS)/$@ -Wall -Wextra \
|
||||
LIBCPIO_BASE=$(HM_APPS_LIBCPIO) \
|
||||
HM_APPS_DIR=$(TOPDIR)
|
||||
@echo "[$@] done"
|
||||
|
||||
DDK_FLAG:=false
|
||||
$(STAGE_DIR)/teehm.img.elf: $(ELFLOADER_OUTDIR)/elfloader.o hmfilemgr cpio-strip
|
||||
@echo "[GEN_IMAGE] $@"
|
||||
$(VER) $(BUILD_TOOLS)/generate_img/smart-strip.sh $(boot-apps)
|
||||
$(VER) DDK_FLAG=$(DDK_FLAG) CONFIG_NO_PLATCFG_EMBEDDED=$(CONFIG_NO_PLATCFG_EMBEDDED) PREBUILD_DIR=$(PREBUILD_DIR) ELFLOADER_DIR=$(ELFLOADER_OUTDIR) OUTPUTDIR=$(OUTPUTDIR)\
|
||||
KERNEL_OUTDIR=$(KERNEL_OUTDIR) BUILD_TOOLS=$(BUILD_TOOLS) $(BUILD_TOOLS)/generate_img/gen_boot_image.sh $(KERNEL_OUTDIR)/kernel.elf $(boot-apps) $@ 2>&1 \
|
||||
| while read line; do echo " [GEN_IMAGE] $$line"; done; \
|
||||
exit ${PIPESTATUS[0]}
|
||||
|
||||
$(STAGE_DIR)/teehm.img: $(STAGE_DIR)/teehm.img.elf
|
||||
@echo "[OBJCOPY $@]"
|
||||
$(VER) $(OBJCOPY) -O binary $< $@
|
||||
ifeq ($(CONFIG_QEMU_PLATFORM),y)
|
||||
cp $(STAGE_DIR)/teehm.img $(STAGE_DIR)/bl32.bin
|
||||
endif
|
||||
|
||||
$(STAGE_DIR)/trustedcore.img: $(STAGE_DIR)/teehm.img
|
||||
@echo "[Installing $@]"
|
||||
$(VER) IMAGE_ROOT=$(STAGE_DIR) $(BUILD_TOOLS)/pack_img/packimg.sh \
|
||||
oh \
|
||||
$(COMPARE_IMAGE) \
|
||||
oh \
|
||||
$(WITH_TEEOS_ENCRYPT) \
|
||||
$(WITH_LOG_ENCODE)
|
||||
ifneq ($(CODE_CHECKER),y)
|
||||
$(VER) $(TOPDIR)/../tee_os_kernel/libcore_shared/syslib/libc/clean_libc.sh $(TOPDIR)/../tee_os_kernel
|
||||
$(VER) $(TEELIB)/libopenssl/clean_openssl.sh $(TOPDIR)
|
||||
endif
|
||||
|
||||
ifneq ($(VERSION_DDK),y)
|
||||
$(VER) rm -rf $(BUILD_TOOLS)/generate_img/cpio-strip/cpio-strip
|
||||
endif
|
||||
@@ -163,15 +114,5 @@ GENERAL_OPTIONS := -Wdate-time -Wfloat-equal -Wshadow -fsigned-char -fno-strict-
|
||||
-pipe -fno-common
|
||||
uniq = $(if $1,$(firstword $1) $(call uniq,$(filter-out $(firstword $1),$1)))
|
||||
|
||||
SDK_CPPFLAGS := $(flags) $(c-flags) -I$(PREBUILD_DIR)/headers -I$(PREBUILD_DIR)/headers/ddk/legacy -I$(PREBUILD_DIR)/headers/sys/hmapi -I$(PREBUILD_DIR)/headers/sys/hmapi/kernel -I$(PREBUILD_DIR)/headers/sys/legacy -I$(PREBUILD_DIR)/headers/ddk/hmapi
|
||||
SDK_CPPFLAGS := $(filter-out --target=$(TARGET_ARCH), $(SDK_CPPFLAGS))
|
||||
SDK_CPPFLAGS += --target=$(HM_TARGET_ARCH)
|
||||
SDK_CPPFLAGS := $(call uniq, $(SDK_CPPFLAGS) $(GENERAL_OPTIONS))
|
||||
SDK_CPPFLAGS := $(filter-out -fsanitize=cfi, $(SDK_CPPFLAGS))
|
||||
SDK_CPPFLAGS := $(filter-out -flto, $(SDK_CPPFLAGS))
|
||||
SDK_CPPFLAGS += -include$(PREBUILD_DIR)/headers/autoconf.h
|
||||
SDK_CPPFLAGS += -include$(PREBUILD_DIR)/headers/platautoconf.h
|
||||
export SDK_CPPFLAGS
|
||||
|
||||
# bootfs image
|
||||
include $(BUILD_PACK)/bootfs.mk
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
# Copyright (C) 2022 Huawei Technologies Co., Ltd.
|
||||
# Licensed under the Mulan PSL v2.
|
||||
# You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
# You may obtain a copy of Mulan PSL v2 at:
|
||||
# http://license.coscl.org.cn/MulanPSL2
|
||||
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
|
||||
# PURPOSE.
|
||||
# See the Mulan PSL v2 for more details.
|
||||
|
||||
override CC := gcc
|
||||
GENERAL_OPTIONS := -Wall -Werror -Wextra -Wfloat-equal -Wshadow -Wtrampolines -Wformat=2\
|
||||
-fno-common -fno-strict-aliasing -fsigned-char -pipe -fstack-protector-all
|
||||
LINK_OPTIONS := -fPIC -Wl,-z,relro -Wl,-z,noexecstack -Wl,-z,now -pie -Wl,-Bsymbolic -rdynamic -Wl,--no-undefined
|
||||
|
||||
GCC_VERSION := $(shell expr `gcc -dumpversion | cut -f1 -d.` \>= 5)
|
||||
|
||||
ifeq ($(GCC_VERSION), 1)
|
||||
GENERAL_OPTIONS += -Wdate-time
|
||||
endif
|
||||
|
||||
#ifeq ($(CONFIG_HW_SECUREC_MIN_MEM),y)
|
||||
GENERAL_OPTIONS += -DSECUREC_WARP_OUTPUT=1 -DSECUREC_WITH_PERFORMANCE_ADDONS=0
|
||||
#endif
|
||||
c_files := $(TEE_SECUREC_DIR)/src/memcpy_s.c
|
||||
c_files += $(TEE_SECUREC_DIR)/src/memset_s.c
|
||||
c_files += $(TEE_SECUREC_DIR)/src/snprintf_s.c
|
||||
c_files += $(TEE_SECUREC_DIR)/src/vsnprintf_s.c
|
||||
c_files += $(TEE_SECUREC_DIR)/src/secureprintoutput_a.c
|
||||
|
||||
c_files_include += -I$(TEE_SECUREC_DIR)/include/
|
||||
|
||||
cpio-strip : $(SOURCE_DIR)/cpio_strip.c $(LIBCPIO_BASE)/src/cpio.c $(LIBCPIO_BASE)/include/cpio/cpio.h
|
||||
@echo " [CC] $@"
|
||||
$(Q)$(CC) $(CPPFLAGS) $(GENERAL_OPTIONS) $(LINK_OPTIONS) -I$(LIBCPIO_BASE)/include $(c_files_include) $(c_files) $(filter %.c, $^) -o $@
|
||||
|
||||
@@ -1,203 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2022 Huawei Technologies Co., Ltd.
|
||||
* Licensed under the Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
|
||||
* PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
#define _XOPEN_SOURCE 700
|
||||
#define ARG_COUNTER 2
|
||||
|
||||
/*
|
||||
* We deliberately use HM's CPIO library rather than libarchive or similar so
|
||||
* we have the same interpretation of CPIO files as HM. This isn't strictly
|
||||
* essential, but it's nice for testing the robustness of this library.
|
||||
*/
|
||||
#include <cpio/cpio.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <securec.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
/*
|
||||
* Synthesise an i-node number, inode number 0-10 may reserved by
|
||||
* some filesystem.
|
||||
*/
|
||||
#define INODE_RESERVED 11
|
||||
|
||||
static void clear_file_name_stuff(char *name)
|
||||
{
|
||||
if (name == NULL)
|
||||
return;
|
||||
|
||||
unsigned int sz = strlen(name);
|
||||
if (sz == 0)
|
||||
return;
|
||||
|
||||
name += (--sz);
|
||||
while (sz) {
|
||||
if (*name == '#') {
|
||||
*name = '\0';
|
||||
name--;
|
||||
sz--;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int clean_cpio_header(struct cpio_header *header, const char *c_ino, int c_ino_len)
|
||||
{
|
||||
errno_t ret_s = memcpy_s(header->c_ino, sizeof(header->c_ino), c_ino, c_ino_len);
|
||||
if (ret_s != EOK) {
|
||||
perror("ino memory copy failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Set the file owned by 'root' */
|
||||
ret_s = memset_s(header->c_uid, sizeof(header->c_uid), 0, sizeof(header->c_uid));
|
||||
if (ret_s != EOK) {
|
||||
perror("uid memory set failed\n");
|
||||
return -1;
|
||||
}
|
||||
ret_s = memset_s(header->c_gid, sizeof(header->c_gid), 0, sizeof(header->c_gid));
|
||||
if (ret_s != EOK) {
|
||||
perror("gid memory set failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Clean the modified time */
|
||||
ret_s = memset_s(header->c_mtime, sizeof(header->c_mtime), 0, sizeof(header->c_mtime));
|
||||
if (ret_s != EOK) {
|
||||
perror("mtime memory set failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Clean the dev num */
|
||||
ret_s = memset_s(header->c_devmajor, sizeof(header->c_devmajor), 0, sizeof(header->c_devmajor));
|
||||
if (ret_s != EOK) {
|
||||
perror("devmajor memory set failed\n");
|
||||
return -1;
|
||||
}
|
||||
ret_s = memset_s(header->c_devminor, sizeof(header->c_devminor), 0, sizeof(header->c_devminor));
|
||||
if (ret_s != EOK) {
|
||||
perror("devminor memory set failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int strip_cpio_entry(void *archive)
|
||||
{
|
||||
struct cpio_header *next = NULL;
|
||||
struct cpio_header *header = archive;
|
||||
const char *filename = NULL;
|
||||
unsigned idx = 0;
|
||||
|
||||
while (header) {
|
||||
char c_ino[sizeof(header->c_ino) + 1] = { 0 };
|
||||
int ret;
|
||||
|
||||
/* Get the header location */
|
||||
ret = cpio_parse_entry(header, &filename, NULL, NULL, &next);
|
||||
if (ret < 0) {
|
||||
errno = -EINVAL;
|
||||
perror("failed to locate entry");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Reach EOF */
|
||||
if (ret == 1)
|
||||
return 0;
|
||||
|
||||
/* Set the file inode number */
|
||||
ret = snprintf_s(c_ino, sizeof(c_ino), sizeof(c_ino) - 1, "%08x", INODE_RESERVED + idx);
|
||||
if (ret < 0) {
|
||||
perror("the file inode number set failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = clean_cpio_header(header, c_ino, sizeof(header->c_ino));
|
||||
if (ret < 0) {
|
||||
perror("failed to clean cpio header\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* clear filename suffix "##..." */
|
||||
clear_file_name_stuff((char *)filename);
|
||||
|
||||
header = next;
|
||||
idx++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
void *archive = NULL;
|
||||
int fd = -1;
|
||||
struct stat stat;
|
||||
int ret;
|
||||
|
||||
if (argc != ARG_COUNTER) {
|
||||
fprintf(stderr,
|
||||
"Usage: %s file\n"
|
||||
" Strip meta data from a CPIO file\n",
|
||||
argv[0]);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fd = open(argv[1], O_RDWR);
|
||||
if (fd < 0) {
|
||||
perror("failed to open archive");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = fstat(fd, &stat);
|
||||
if (ret) {
|
||||
perror("failed to get stat of archive");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
/* Mmap the file */
|
||||
archive = mmap(NULL, stat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
if (archive == MAP_FAILED) {
|
||||
perror("failed to mmap archive");
|
||||
archive = NULL;
|
||||
goto fail;
|
||||
}
|
||||
|
||||
/* Strip each file entry */
|
||||
ret = strip_cpio_entry(archive);
|
||||
if (ret)
|
||||
goto fail2;
|
||||
|
||||
if (munmap(archive, stat.st_size) != EOK)
|
||||
perror("munmap failed\n");
|
||||
|
||||
close(fd);
|
||||
return 0;
|
||||
|
||||
fail2:
|
||||
if (munmap(archive, stat.st_size) != EOK)
|
||||
perror("strip failed and munmap failed\n");
|
||||
fail:
|
||||
close(fd);
|
||||
return -1;
|
||||
}
|
||||
@@ -1,107 +0,0 @@
|
||||
#!/bin/bash
|
||||
# function belong to gen_boot_image.
|
||||
# Copyright (C) 2022 Huawei Technologies Co., Ltd.
|
||||
# Licensed under the Mulan PSL v2.
|
||||
# You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
# You may obtain a copy of Mulan PSL v2 at:
|
||||
# http://license.coscl.org.cn/MulanPSL2
|
||||
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
|
||||
# PURPOSE.
|
||||
# See the Mulan PSL v2 for more details.
|
||||
set -e
|
||||
function pad8_name()
|
||||
{
|
||||
name="$1"
|
||||
padnum=$(expr \( 8 - \( \( length "${name}" \) + 1 \) % 8 \) % 8)
|
||||
if [ "${padnum}" == 0 ];
|
||||
then
|
||||
echo "$name"
|
||||
return 0
|
||||
fi
|
||||
padstr=$(printf "%-${padnum}s" '#')
|
||||
padstr=${padstr// /'#'}
|
||||
echo "${name}""${padstr}"
|
||||
return 0;
|
||||
}
|
||||
|
||||
fail() {
|
||||
echo "(failed)" > /dev/stderr
|
||||
[ -n "${TEMP_DIR}" ] && rm -rf "${TEMP_DIR}"
|
||||
exit 1
|
||||
}
|
||||
|
||||
do_others() {
|
||||
# Strip CPIO metadata if possible.
|
||||
if [ "${DDK_FLAG}" != "true" ]; then
|
||||
CPIO_STRIP="$BUILD_TOOLS"/generate_img/cpio-strip/cpio-strip
|
||||
else
|
||||
CPIO_STRIP="$TOPDIR"/prebuild/tee-kernel-release/tools/cpio-strip
|
||||
fi
|
||||
"$CPIO_STRIP" "${TEMP_DIR}"/archive.cpio
|
||||
UNZIP_SIZE=$(ls -l "${TEMP_DIR}"/archive.cpio | awk '{print $5}')
|
||||
if [ "${CONFIG_NO_ZIP_IMAGE}" == "true" ]; then
|
||||
cp "${TEMP_DIR}"/archive.cpio "${TEMP_DIR}"/archive.cpio.gz
|
||||
else
|
||||
gzip -n -c "${TEMP_DIR}"/archive.cpio > "${TEMP_DIR}"/archive.cpio.gz
|
||||
echo "before zipped size is ${UNZIP_SIZE}"
|
||||
fi
|
||||
|
||||
if [ "$?" -ne 0 ]; then
|
||||
fail
|
||||
fi
|
||||
|
||||
popd &>/dev/null
|
||||
|
||||
pushd "${TEMP_DIR}" >/dev/null
|
||||
cp "${TEMP_DIR}"/archive.cpio.gz "${TEMP_DIR}"/archive.data
|
||||
|
||||
if [ "$HM_ARCH" == "aarch64" ] ; then
|
||||
emul="aarch64elf"
|
||||
else
|
||||
emul="armelf"
|
||||
fi
|
||||
${LD} -T "${SCRIPT_DIR}/archive.bin.lds" \
|
||||
--oformat "${FORMAT}" -r -b binary -m ${emul} archive.data \
|
||||
-o "${TEMP_DIR}/archive.o" || fail
|
||||
popd >/dev/null
|
||||
|
||||
LINKER_DIR="$OUTPUTDIR"/arm/package
|
||||
mkdir -p "$LINKER_DIR"
|
||||
ARCHIVE_FLAGS="-DUNZIP_SIZE=${UNZIP_SIZE}"
|
||||
${CC} ${SDK_CPPFLAGS} ${ARCHIVE_FLAGS} -P -E \
|
||||
-o "${LINKER_DIR}/linker.lds_pp" \
|
||||
-x c "${SCRIPT_DIR}/linker.lds"
|
||||
|
||||
if [ "${DDK_FLAG}" != "true" ]; then
|
||||
CC_PLATFORM_ES=""
|
||||
if [ "${chip_type}" == "es" ]; then
|
||||
CC_PLATFORM_ES="_es"
|
||||
fi
|
||||
if [ "${CONFIG_NO_PLATCFG_EMBEDDED}" != "true" ]; then
|
||||
${CC} ${SDK_CPPFLAGS} -I"${PLAT_CFG_DIR}"/ \
|
||||
-I"${PLAT_COMMON_DIR}"/include/ \
|
||||
-I${TOPDIR}/prebuild/tee-kernel-release/headers/kernel/include/arch/arm/uapi/ \
|
||||
-I${TOPDIR}/prebuild/tee-kernel-local-release/headers/kernel/include/arch/arm/uapi/ \
|
||||
-I${OUTPUTDIR}/prebuild/tee-kernel-release/headers/kernel/include/arch/arm/uapi/ \
|
||||
-I${OUTPUTDIR}/prebuild/tee-kernel-local-release/headers/kernel/include/arch/arm/uapi/ \
|
||||
-Wall -Wextra -Werror -Wformat=2 -c \
|
||||
-o "${LINKER_DIR}/plat_cfg.o" \
|
||||
""${PLAT_CFG_DIR}"/plat_cfg.c"
|
||||
LINK_PLAT_CFG="${LINKER_DIR}/plat_cfg.o"
|
||||
fi
|
||||
fi
|
||||
|
||||
${LD} -T "${LINKER_DIR}/linker.lds_pp" \
|
||||
-z relro -z now -z notext -pie --apply-dynamic-relocs\
|
||||
--oformat "${FORMAT}" \
|
||||
"${ELFLOADER_DIR}/elfloader.o" "${TEMP_DIR}/archive.o" \
|
||||
"${LINK_PLAT_CFG}" \
|
||||
"${KERNEL_OUTDIR}"/libklibc.a "${KERNEL_OUTDIR}"/libhardware.a "${KERNEL_OUTDIR}"/libuart.a \
|
||||
-o "${OUTPUT_FILE}" \
|
||||
|| fail
|
||||
|
||||
[ -n "${TEMP_DIR}" ] && rm -rf "${TEMP_DIR}"
|
||||
|
||||
}
|
||||
|
||||
@@ -1,95 +0,0 @@
|
||||
#!/bin/bash
|
||||
# Print all commands if V=3; maximum verbosity.
|
||||
# Copyright (C) 2022 Huawei Technologies Co., Ltd.
|
||||
# Licensed under the Mulan PSL v2.
|
||||
# You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
# You may obtain a copy of Mulan PSL v2 at:
|
||||
# http://license.coscl.org.cn/MulanPSL2
|
||||
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
|
||||
# PURPOSE.
|
||||
# See the Mulan PSL v2 for more details.
|
||||
set -e
|
||||
source ${BUILD_TOOLS}/generate_img/gen_boot_function.sh
|
||||
|
||||
V=3
|
||||
|
||||
if [ 0"${V}" -ge 3 ]; then
|
||||
set -x
|
||||
fi
|
||||
|
||||
if [ "$#" -lt 3 ]; then
|
||||
echo "Usage: $0 <kernel elf> <user elf> <output file>"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
KERNEL_IMAGE="$1" ; shift 1
|
||||
sep=""
|
||||
while [ "$#" -ne 1 ]; do
|
||||
USER_IMAGES="$USER_IMAGES$sep$1" ; shift 1
|
||||
sep=" "
|
||||
done
|
||||
|
||||
OUTPUT_FILE="$1" ; shift 1
|
||||
if [ "$HM_ARCH" == "aarch64" ] ; then
|
||||
ENTRY_ADDR=0x48000000 ; FORMAT=elf64-littleaarch64
|
||||
else
|
||||
ENTRY_ADDR=0x48000000 ; FORMAT=elf32-littlearm
|
||||
fi
|
||||
|
||||
if [ ! -e "${KERNEL_IMAGE}" ]; then
|
||||
echo "File '${KERNEL_IMAGE}' does not exist."; exit 1
|
||||
fi
|
||||
|
||||
for USER_IMAGE in $USER_IMAGES; do
|
||||
if [ ! -e "${USER_IMAGE}" ]; then
|
||||
echo "File '${USER_IMAGE}' does not exist."; exit 1
|
||||
fi
|
||||
done
|
||||
|
||||
# Get the script's location.
|
||||
SCRIPT_PATH=$(readlink -f ${BASH_SOURCE[0]})
|
||||
SCRIPT_DIR=$(dirname "${SCRIPT_PATH}")/lds
|
||||
|
||||
# Create working directory.
|
||||
# Warning: mktemp functions differently on Linux and OSX.
|
||||
TEMP_DIR=$(mktemp -d -t HMXXXX)
|
||||
|
||||
if [ "${PLAT}" != "virt" ]; then
|
||||
CPIO_EXE=$(which cpio)
|
||||
else
|
||||
CPIO_EXE=cpio
|
||||
fi
|
||||
|
||||
# Generate an archive of the userspace and kernel images.
|
||||
pad8_kernel_img_name=$(pad8_name "kernel.elf")
|
||||
mkdir -p "${TEMP_DIR}/cpio"
|
||||
cp -f "${KERNEL_IMAGE}" "${TEMP_DIR}"/cpio/"${pad8_kernel_img_name}"
|
||||
|
||||
pad8_user_images=""
|
||||
sep=""
|
||||
for USER_IMAGE in ${USER_IMAGES}; do
|
||||
tmp_name=$(pad8_name $(basename "${USER_IMAGE}"))
|
||||
cp -f "${USER_IMAGE}" "${TEMP_DIR}"/cpio/"${tmp_name}"
|
||||
pad8_user_images="${pad8_user_images}""${sep}""${tmp_name}"
|
||||
sep=" "
|
||||
done
|
||||
|
||||
${STRIP} -p --strip-debug "${TEMP_DIR}"/cpio/*
|
||||
|
||||
pushd "${TEMP_DIR}/cpio" &>/dev/null
|
||||
(
|
||||
printf "${pad8_kernel_img_name}\n"
|
||||
for USER_IMAGE in $pad8_user_images; do
|
||||
printf "$(basename ${USER_IMAGE})\n"
|
||||
printf "$(basename ${USER_IMAGE})\n" 1>&2
|
||||
done
|
||||
) | "${CPIO_EXE}" --quiet -o -H newc > "${TEMP_DIR}"/archive.cpio
|
||||
|
||||
if [ $? -ne 0 ]; then
|
||||
fail
|
||||
fi
|
||||
|
||||
do_others
|
||||
|
||||
exit 0
|
||||
@@ -1,11 +0,0 @@
|
||||
/*
|
||||
* Packing everything under archive_ar
|
||||
*/
|
||||
|
||||
SECTIONS
|
||||
{
|
||||
.archive_ar :
|
||||
{
|
||||
*(.data)
|
||||
}
|
||||
}
|
||||
@@ -1,98 +0,0 @@
|
||||
#include <autoconf.h>
|
||||
#include <mmu.h>
|
||||
|
||||
IMAGE_BASE_ADDR = (CONFIG_KERNEL_IMG_BASE + BOOT_OFFSET);
|
||||
|
||||
PHDRS
|
||||
{
|
||||
interp PT_INTERP FLAGS (4);
|
||||
text PT_LOAD FLAGS (5);
|
||||
rodata PT_LOAD FLAGS (4);
|
||||
data PT_LOAD FLAGS (6);
|
||||
dynamic PT_DYNAMIC FLAGS (6);
|
||||
stack PT_GNU_STACK FLAGS (6);
|
||||
relro 0x6474e552 FLAGS (4);
|
||||
}
|
||||
|
||||
SECTIONS
|
||||
{
|
||||
. = IMAGE_BASE_ADDR;
|
||||
_image_base_addr = .;
|
||||
.text :
|
||||
{
|
||||
*(.text.efi.header)
|
||||
*(.text.start)
|
||||
*(.text.*)
|
||||
*(.text)
|
||||
} : text
|
||||
|
||||
.rela.dyn :
|
||||
{
|
||||
*(.rela.init)
|
||||
*(.rela.text .rela.text.*)
|
||||
*(.rela.rodata .rela.rodata.*)
|
||||
*(.rela.data .rela.data.*)
|
||||
*(.rela.got)
|
||||
*(.rela.bss .rela.bss.*)
|
||||
}
|
||||
|
||||
.rodata :
|
||||
{
|
||||
*(.rodata .rodata.*)
|
||||
}
|
||||
|
||||
. = DATA_SEGMENT_ALIGN (4K, 4K);
|
||||
.dynamic :
|
||||
{
|
||||
*(.dynamic)
|
||||
} : data : dynamic
|
||||
|
||||
.got :
|
||||
{
|
||||
*(.got.plt)
|
||||
*(.igot.plt)
|
||||
*(.got)
|
||||
*(.igot)
|
||||
} : data
|
||||
|
||||
. = DATA_SEGMENT_RELRO_END (0, .);
|
||||
|
||||
.bss :
|
||||
{
|
||||
. = ALIGN(8);
|
||||
__data_end = .;
|
||||
*(.bss .bss.*)
|
||||
} : data
|
||||
__bss_end = .;
|
||||
. = ALIGN(8);
|
||||
|
||||
.stack :
|
||||
{
|
||||
_abortstack_bottom = .;
|
||||
. = . + 4K;
|
||||
. = ALIGN(16K);
|
||||
_abortstack_top = .;
|
||||
} : data
|
||||
|
||||
.data :
|
||||
{
|
||||
*(.data .data.*)
|
||||
. = ALIGN(16);
|
||||
_archive_start = .;
|
||||
*(.archive_ar)
|
||||
_archive_end = .;
|
||||
} : data
|
||||
|
||||
_end = .;
|
||||
|
||||
. = DATA_SEGMENT_END (.);
|
||||
|
||||
__init_size = __data_end - IMAGE_BASE_ADDR;
|
||||
__init_mem_usage = _end - IMAGE_BASE_ADDR;
|
||||
__unzip_size = UNZIP_SIZE;
|
||||
/DISCARD/ :
|
||||
{
|
||||
*(.note.gnu.build-id)
|
||||
*(.comment)
|
||||
}
|
||||
}
|
||||
@@ -1,96 +0,0 @@
|
||||
# Copyright (C) 2022 Huawei Technologies Co., Ltd.
|
||||
# Licensed under the Mulan PSL v2.
|
||||
# You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
# You may obtain a copy of Mulan PSL v2 at:
|
||||
# http://license.coscl.org.cn/MulanPSL2
|
||||
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
|
||||
# PURPOSE.
|
||||
# See the Mulan PSL v2 for more details.
|
||||
#
|
||||
# @TAG(HUAWEI)
|
||||
#
|
||||
|
||||
# Targets
|
||||
TARGET := hmfilemgr
|
||||
|
||||
# used by common/var.mk
|
||||
MODULE_FOLDER := $(shell basename $(CURDIR))
|
||||
include $(BUILD_CONFIG)/var.mk
|
||||
include $(BUILD_CONFIG)/toolchain.mk
|
||||
|
||||
BOOTIMG := $(STAGE_DIR)/bootfs.img
|
||||
PREBUILD_FILEMGR_OBJ := $(PREBUILD_APPS)/$(TARGET)/src
|
||||
|
||||
hm_filemgr_objects := $(BUILD_DIR)/src/get_ramfs_data.o
|
||||
|
||||
INSTALL_FILE := $(APP_DIR)/$(TARGET)
|
||||
# default compile target
|
||||
default: $(INSTALL_FILE)
|
||||
|
||||
# build hmfilemgr
|
||||
$(INSTALL_FILE): $(BUILD_DIR)/$(TARGET)
|
||||
@echo "[ INSTALL APP ] $@"
|
||||
@test -d $(dir $@) || mkdir -p $(dir $@)
|
||||
@cp -rf $< $@
|
||||
ifeq ($(CONFIG_ARCH_AARCH64),y)
|
||||
LIBS := hongmeng ramfs debug syscalls pathmgr \
|
||||
ac security hmlog ac_policy ipc
|
||||
|
||||
LIBS += c vfs mmgr
|
||||
TARGET_ARCH := $(TARGET_ARCH_64)
|
||||
else
|
||||
LIBS := hongmeng_a32 ramfs_a32 debug_a32 syscalls_a32 pathmgr_a32 \
|
||||
ac_a32 security_a32 hmlog_a32 ac_policy_a32 ipc_a32
|
||||
LIBS += c_a32 vfs_a32 mmgr_a32
|
||||
TARGET_ARCH := $(TARGET_ARCH_32)
|
||||
endif
|
||||
# the main part of hmfilemgr
|
||||
LIBS += hmfilemgr
|
||||
ENTRY_POINT=_hm_start
|
||||
LINK_LIBS=$(LIBS:%=-l%)
|
||||
ifeq ($(CONFIG_ARCH_AARCH64),y)
|
||||
LINK_LIBS += -lhwsecurec
|
||||
else
|
||||
LINK_LIBS += -lhwsecurec_a32
|
||||
endif
|
||||
hmfilemgr_dep_libs = $(addprefix $(LIB_DIR)/,$(LLIBS:%=lib%.a))
|
||||
|
||||
hmsysmgr_lib :=
|
||||
|
||||
hmfilemgr_c-flags += --target=$(TARGET_ARCH)
|
||||
hmfilemgr_c-flags += -Wall -Wextra -std=gnu11 -Oz -fno-builtin -fPIC -Wformat=2
|
||||
hmfilemgr_c-flags += -Wdate-time -Werror -Wfloat-equal -Wshadow -fno-common -fno-strict-aliasing -pipe
|
||||
hmfilemgr_c-flags += -fstack-protector-strong -mtune=cortex-a53 -march=armv8-a -ffunction-sections -fdata-sections -fno-omit-frame-pointer
|
||||
hmfilemgr_c-flags += -include$(PREBUILD_HEADER)/autoconf.h -include$(PREBUILD_HEADER)/platautoconf.h
|
||||
|
||||
ifeq ($(CONFIG_ARCH_AARCH64),y)
|
||||
filemgr_LDFLAGS += -u __vsyscall_ptr -T $(PREBUILD_TOOLS)/common/boot-app-unxom-$(TEE_ARCH).lds
|
||||
else
|
||||
filemgr_LDFLAGS += -u __vsyscall_ptr -T $(PREBUILD_TOOLS)/common/boot-app-$(TEE_ARCH).lds
|
||||
endif
|
||||
|
||||
filemgr_LDFLAGS += -L$(LIB_DIR)
|
||||
ifeq ($(CONFIG_ARCH_AARCH64),y)
|
||||
filemgr_LDFLAGS += -L$(TOPDIR)/../tee_os_kernel/build/arm/$(PLAT)/libhwsecurec/
|
||||
else
|
||||
filemgr_LDFLAGS += -L$(TOPDIR)/../tee_os_kernel/build/arm/$(PLAT)/libhwsecurec_a32/
|
||||
endif
|
||||
filemgr_LDFLAGS += --gc-sections -L$(PREBUILD_ARCH_PLAT_LIBS) --start-group $(LINK_LIBS) $(LIBCOMPILER_RT_BUILTINS) --end-group
|
||||
filemgr_LDFLAGS += -u _hm_start -e _hm_start -z max-page-size=0x1000 -nostdlib --build-id=none -z noexecstack -z relro -z now
|
||||
|
||||
filemgr_LDFLAGS += -s
|
||||
ifeq ($(CONFIG_SYSSERV_ASLR), y)
|
||||
filemgr_LDFLAGS += -pie
|
||||
endif
|
||||
|
||||
$(BUILD_DIR)/$(TARGET): $(hm_filemgr_objects) $(hmfilemgr_dep_libs)
|
||||
@echo "[ LD ] $@"
|
||||
$(VER) $(LD) $(hm_filemgr_objects) $(hmsysmgr_lib) $(filemgr_LDFLAGS) -o $@
|
||||
$(VER) $(OBJCOPY) $@
|
||||
$(STRIP) --strip-debug $@
|
||||
|
||||
$(BUILD_DIR)/src/get_ramfs_data.o: $(CURDIR)/src/get_ramfs_data.c $(BOOTIMG)
|
||||
@test -d $(dir $@) || mkdir -p $(dir $@)
|
||||
$(VER)$(CC) $(hmfilemgr_c-flags) -DBOOTFS_IMG="$(filter %.img, $^)" \
|
||||
-c -o $@ $(filter %.c,$^)
|
||||
@@ -1,44 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2022 Huawei Technologies Co., Ltd.
|
||||
* Licensed under the Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
|
||||
* PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
#include "get_ramfs_data.h"
|
||||
|
||||
#define __str_append(str) #str
|
||||
#define str_append(str) __str_append(str)
|
||||
|
||||
void *get_ramfs_data(size_t *ramfs_size)
|
||||
{
|
||||
if (!ramfs_size)
|
||||
return NULL;
|
||||
#ifdef CONFIG_ARCH_AARCH64
|
||||
__asm__ __volatile__(" .pushsection .rodata.get_ramfs_data, \"a\", @progbits\n"
|
||||
" .balign 4096\n"
|
||||
" .global g_ramfs_data\n"
|
||||
"g_ramfs_data:\n"
|
||||
" .incbin \"" str_append(BOOTFS_IMG) "\"\n"
|
||||
" .global g_ramfs_size\n"
|
||||
"g_ramfs_size:\n"
|
||||
" .word . - g_ramfs_data\n"
|
||||
" .popsection\n");
|
||||
#else
|
||||
__asm__ __volatile__(" .pushsection .rodata.get_ramfs_data, \"a\"\n"
|
||||
" .balign 4096\n"
|
||||
" .global g_ramfs_data\n"
|
||||
"g_ramfs_data:\n"
|
||||
" .incbin \"" str_append(BOOTFS_IMG) "\"\n"
|
||||
" .global g_ramfs_size\n"
|
||||
"g_ramfs_size:\n"
|
||||
" .word . - g_ramfs_data\n"
|
||||
" .popsection\n");
|
||||
#endif
|
||||
*ramfs_size = g_ramfs_size;
|
||||
return g_ramfs_data;
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2022 Huawei Technologies Co., Ltd.
|
||||
* Licensed under the Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
|
||||
* PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
#ifndef _GET_RAMFS_DATA_H_
|
||||
#define _GET_RAMFS_DATA_H_
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
void *get_ramfs_data(size_t *ramfs_size);
|
||||
extern char g_ramfs_data[];
|
||||
extern size_t g_ramfs_size;
|
||||
#endif /* _GET_RAMFS_DATA_H_ */
|
||||
@@ -1,37 +0,0 @@
|
||||
/*
|
||||
* Copyright Huawei Technologies Co., Ltd. 2010-2019. All rights reserved.
|
||||
*/
|
||||
OUTPUT_FORMAT("elf64-littleaarch64")
|
||||
OUTPUT_FORMAT(aarch64)
|
||||
ENTRY(_hm_start)
|
||||
PHDRS
|
||||
{
|
||||
phdr PT_PHDR PHDRS AT (0x400000) FLAGS (4);
|
||||
rodata PT_LOAD FILEHDR PHDRS FLAGS (4);
|
||||
text PT_LOAD FLAGS (1);
|
||||
data PT_LOAD FLAGS (6);
|
||||
}
|
||||
SECTIONS
|
||||
{
|
||||
. = 0x400000 + SIZEOF_HEADERS;
|
||||
.rodata : { *(.rodata .rodata.*) } : rodata
|
||||
. = ALIGN(0x1000);
|
||||
.text : { *(.text .text.*) } : text
|
||||
.init_array :
|
||||
{
|
||||
PROVIDE_HIDDEN (__init_array_start = .);
|
||||
KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*)))
|
||||
KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors))
|
||||
PROVIDE_HIDDEN (__init_array_end = .);
|
||||
}
|
||||
.fini_array :
|
||||
{
|
||||
PROVIDE_HIDDEN (__fini_array_start = .);
|
||||
KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*)))
|
||||
KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors))
|
||||
PROVIDE_HIDDEN (__fini_array_end = .);
|
||||
}
|
||||
. = ALIGN(0x1000);
|
||||
.data : { *(.data .data.*) } : data
|
||||
.bss : { *(.bss .bss.* ) } : data
|
||||
}
|
||||
@@ -1,401 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2022 Huawei Technologies Co., Ltd.
|
||||
* Licensed under the Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
|
||||
* PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define EI_NIDENT 16
|
||||
#define SHT_SYMTAB 2
|
||||
#define SHT_STRTAB 3
|
||||
|
||||
#define MAX_ALLOC_SIZE 0xa000000
|
||||
#define MAX_EH_SHNUM 0xfff0
|
||||
|
||||
/* refer to elf manpage */
|
||||
#define EI_MAG0 0
|
||||
#define ELFMAG0 0x7f
|
||||
#define EI_MAG1 1
|
||||
#define ELFMAG1 'E'
|
||||
#define EI_MAG2 2
|
||||
#define ELFMAG2 'L'
|
||||
#define EI_MAG3 3
|
||||
#define ELFMAG3 'F'
|
||||
|
||||
#define EI_CLASS 4
|
||||
#define ELFCLASS32 1
|
||||
#define ELFCLASS64 2
|
||||
|
||||
struct elf_shdr {
|
||||
uint32_t sh_name; /* Section name (string tbl index) */
|
||||
uint32_t sh_type; /* Section type */
|
||||
#ifdef aarch64
|
||||
uint64_t sh_flags; /* Section flags */
|
||||
uint64_t sh_addr; /* Section virtual addr at execution */
|
||||
uint64_t sh_offset; /* Section file offset */
|
||||
uint64_t sh_size; /* Section size in bytes */
|
||||
#else
|
||||
uint32_t sh_flags;
|
||||
uint32_t sh_addr;
|
||||
uint32_t sh_offset;
|
||||
uint32_t sh_size;
|
||||
#endif
|
||||
uint32_t sh_link; /* Link to another section */
|
||||
uint32_t sh_info; /* Additional section information */
|
||||
#ifdef aarch64
|
||||
uint64_t sh_addralign; /* Section alignment */
|
||||
uint64_t sh_entsize; /* Entry size if section holds table */
|
||||
#else
|
||||
uint32_t sh_addralign; /* Section alignment */
|
||||
uint32_t sh_entsize; /* Entry size if section holds table */
|
||||
#endif
|
||||
};
|
||||
|
||||
struct elf_ehdr {
|
||||
uint8_t eh_ident[EI_NIDENT]; /* Magic number and other info */
|
||||
uint16_t eh_type; /* Object file type */
|
||||
uint16_t eh_machine; /* Architecture */
|
||||
uint32_t eh_version; /* Object file version */
|
||||
#ifdef aarch64
|
||||
uint64_t eh_entry; /* Entry point virtual address */
|
||||
uint64_t eh_phoff; /* Program header table file offset */
|
||||
uint64_t eh_shoff; /* Section header table file offset */
|
||||
#else
|
||||
uint32_t eh_entry; /* Entry point32_t virtual address */
|
||||
uint32_t eh_phoff; /* Program header table file offset */
|
||||
uint32_t eh_shoff; /* Section header table file offset */
|
||||
#endif
|
||||
uint32_t eh_flags; /* Processor-specific flags */
|
||||
uint16_t eh_ehsize; /* ELF header size in bytes */
|
||||
uint16_t eh_phentsize; /* Program header table entry size */
|
||||
uint16_t eh_phnum; /* Program header table entry count */
|
||||
uint16_t eh_shentsize; /* Section header table entry size */
|
||||
uint16_t eh_shnum; /* Section header table entry count */
|
||||
uint16_t eh_shstrndx; /* Section header string table index */
|
||||
};
|
||||
|
||||
static struct elf_ehdr *g_elf_head = NULL;
|
||||
static struct elf_shdr *g_sec_head = NULL;
|
||||
static FILE *g_infile = NULL;
|
||||
static FILE *g_outfile = NULL;
|
||||
|
||||
static void out_clean(void)
|
||||
{
|
||||
/* free all file and alloced mem */
|
||||
if (g_elf_head != NULL) {
|
||||
free(g_elf_head);
|
||||
g_elf_head = NULL;
|
||||
}
|
||||
|
||||
if (g_sec_head != NULL) {
|
||||
free(g_sec_head);
|
||||
g_sec_head = NULL;
|
||||
}
|
||||
|
||||
if (g_infile != NULL) {
|
||||
fclose(g_infile);
|
||||
g_infile = NULL;
|
||||
}
|
||||
|
||||
if (g_outfile != NULL) {
|
||||
fclose(g_outfile);
|
||||
g_outfile = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
#define OPEN_FILE_ERROR_RET (-2)
|
||||
#define ELF_HEAD_ERROR_RET (-3)
|
||||
#define ELF_TAB_ERROR_RET (-4)
|
||||
|
||||
#define ELF_TYPE_INDEX 1
|
||||
#define IN_FILE_INDEX 2
|
||||
#define OUT_FILE_INDEX 3
|
||||
#define PARAMS_NUM 4
|
||||
|
||||
static int32_t check_input_param(int32_t argc, char *argv[])
|
||||
{
|
||||
if ((argc != PARAMS_NUM) || (argv == NULL)) {
|
||||
printf("Usage: \"elf_extract type infile outfile\"!!!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* check the input type */
|
||||
char *type = argv[ELF_TYPE_INDEX];
|
||||
if (strncmp(type, "rtosck", (strlen("rtosck") + 1))) {
|
||||
printf("Error:wrong type input\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* open the infile rtosck elf */
|
||||
g_infile = fopen(argv[IN_FILE_INDEX], "rb");
|
||||
if (g_infile == NULL) {
|
||||
printf("Error:can't open file\n");
|
||||
return OPEN_FILE_ERROR_RET;
|
||||
}
|
||||
|
||||
/* open the outfile rtosck image */
|
||||
g_outfile = fopen(argv[OUT_FILE_INDEX], "ab");
|
||||
if (g_outfile == NULL) {
|
||||
printf("Error:can't open file\n");
|
||||
return OPEN_FILE_ERROR_RET;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int32_t check_head_magic(void)
|
||||
{
|
||||
if ((g_elf_head->eh_ident[EI_MAG0] != ELFMAG0) || (g_elf_head->eh_ident[EI_MAG1] != ELFMAG1) ||
|
||||
(g_elf_head->eh_ident[EI_MAG2] != ELFMAG2) || (g_elf_head->eh_ident[EI_MAG3] != ELFMAG3)) {
|
||||
printf("invalid elf magic\n");
|
||||
return ELF_HEAD_ERROR_RET;
|
||||
}
|
||||
#ifdef aarch64
|
||||
if (g_elf_head->eh_ident[EI_CLASS] != ELFCLASS64) {
|
||||
printf("Error:elf class is not ELFCLASS64!\n");
|
||||
return ELF_HEAD_ERROR_RET;
|
||||
}
|
||||
#else
|
||||
if (g_elf_head->eh_ident[EI_CLASS] != ELFCLASS32) {
|
||||
printf("Error:elf class is not ELFCLASS32!\n");
|
||||
return ELF_HEAD_ERROR_RET;
|
||||
}
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int32_t check_elf_head(void)
|
||||
{
|
||||
int32_t ret;
|
||||
/* parse the elf header to get the elf detail */
|
||||
g_elf_head = malloc(sizeof(*g_elf_head));
|
||||
if (g_elf_head == NULL) {
|
||||
printf("Error:elf head read error!\n");
|
||||
return ELF_HEAD_ERROR_RET;
|
||||
}
|
||||
|
||||
/* read count is 1, so should return 1 if it is succ */
|
||||
ret = (int32_t)fread(g_elf_head, sizeof(*g_elf_head), 1, g_infile);
|
||||
if (ret != 1) {
|
||||
printf("Error:elf head read error!\n");
|
||||
return ELF_HEAD_ERROR_RET;
|
||||
}
|
||||
|
||||
ret = check_head_magic();
|
||||
if (ret != 0)
|
||||
return ret;
|
||||
|
||||
if (g_elf_head->eh_shnum == 0) {
|
||||
printf("Error:elf head shnum is invalid!\n");
|
||||
return ELF_HEAD_ERROR_RET;
|
||||
}
|
||||
|
||||
if (g_elf_head->eh_ehsize != sizeof(*g_elf_head) || g_elf_head->eh_shentsize != sizeof(*g_sec_head)) {
|
||||
printf("Error:elf head is invalid\n");
|
||||
return ELF_HEAD_ERROR_RET;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int32_t read_elf_section_head(void)
|
||||
{
|
||||
int32_t ret;
|
||||
|
||||
ret = check_elf_head();
|
||||
if (ret != 0)
|
||||
return ret;
|
||||
|
||||
if (g_elf_head->eh_shnum > MAX_EH_SHNUM) {
|
||||
printf("Error:large shnum\n");
|
||||
return ELF_HEAD_ERROR_RET;
|
||||
}
|
||||
|
||||
if (fseek(g_infile, g_elf_head->eh_shoff, SEEK_SET) != 0) {
|
||||
printf("Error:elf head read error!\n");
|
||||
return ELF_HEAD_ERROR_RET;
|
||||
}
|
||||
|
||||
/* parse the section header to get the elf's section detail */
|
||||
if (g_elf_head->eh_shnum == 0) {
|
||||
printf("Error:elf head eh_shnum is zero!\n");
|
||||
return ELF_HEAD_ERROR_RET;
|
||||
}
|
||||
g_sec_head = malloc(sizeof(*g_sec_head) * g_elf_head->eh_shnum);
|
||||
if (g_sec_head == NULL) {
|
||||
printf("Error:elf head read error!\n");
|
||||
return ELF_HEAD_ERROR_RET;
|
||||
}
|
||||
|
||||
/* read count is eh_shnum, so it should return eh_shnum when it is succ */
|
||||
ret = (int32_t)fread(g_sec_head, sizeof(*g_sec_head), g_elf_head->eh_shnum, g_infile);
|
||||
if (ret != g_elf_head->eh_shnum) {
|
||||
printf("Error:section head read error!\n");
|
||||
return ELF_HEAD_ERROR_RET;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int32_t read_sym_tab(const struct elf_shdr *sec_entry)
|
||||
{
|
||||
int32_t ret;
|
||||
uint32_t symtab_offset = (uint32_t)sec_entry->sh_offset;
|
||||
uint32_t sym_tab_size = (uint32_t)sec_entry->sh_size;
|
||||
|
||||
if (sym_tab_size > MAX_ALLOC_SIZE || sym_tab_size == 0) {
|
||||
printf("symtab size is invalid\n");
|
||||
return ELF_TAB_ERROR_RET;
|
||||
}
|
||||
|
||||
if (fseek(g_infile, symtab_offset, SEEK_SET) != 0) {
|
||||
printf("symtab fseek failed\n");
|
||||
return ELF_TAB_ERROR_RET;
|
||||
}
|
||||
if (sec_entry->sh_size == 0) {
|
||||
printf("ERROR:sec_entry sh_size is zero\n");
|
||||
return ELF_TAB_ERROR_RET;
|
||||
}
|
||||
char *sym_tab = malloc(sec_entry->sh_size);
|
||||
if (sym_tab == NULL) {
|
||||
printf("ERROR:malloc failed for sym_tab\n");
|
||||
return ELF_TAB_ERROR_RET;
|
||||
}
|
||||
|
||||
/* read count is 1, so should return 1 if it is succ */
|
||||
if (fread(sym_tab, sec_entry->sh_size, 1, g_infile) != 1) {
|
||||
printf("symtab fread failed\n");
|
||||
ret = ELF_TAB_ERROR_RET;
|
||||
goto free_out;
|
||||
}
|
||||
|
||||
/* write into out image file */
|
||||
if (fseek(g_outfile, 0, SEEK_END) != 0) {
|
||||
printf("symtab fseek outfile failed\n");
|
||||
ret = ELF_TAB_ERROR_RET;
|
||||
goto free_out;
|
||||
}
|
||||
|
||||
/* write count is 1, so should return 1 if it is succ */
|
||||
if (fwrite(sym_tab, sym_tab_size, 1, g_outfile) != 1) {
|
||||
printf("symtab fwrite failedn");
|
||||
ret = ELF_TAB_ERROR_RET;
|
||||
goto free_out;
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
|
||||
free_out:
|
||||
free(sym_tab);
|
||||
sym_tab = NULL;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int32_t read_str_tab(const struct elf_shdr *sec_entry)
|
||||
{
|
||||
int32_t ret;
|
||||
/* read the rtosck strtab out */
|
||||
uint32_t strtab_offset = (uint32_t)sec_entry->sh_offset;
|
||||
|
||||
if (sec_entry->sh_size > MAX_ALLOC_SIZE || sec_entry->sh_size == 0) {
|
||||
printf("strtab size is invalid\n");
|
||||
return ELF_TAB_ERROR_RET;
|
||||
}
|
||||
|
||||
if (fseek(g_infile, strtab_offset, SEEK_SET) != 0) {
|
||||
printf("str fseek failed\n");
|
||||
return ELF_TAB_ERROR_RET;
|
||||
}
|
||||
|
||||
char *str_tab = malloc(sec_entry->sh_size);
|
||||
if (str_tab == NULL) {
|
||||
printf("ERROR:malloc failed for str_tab\n");
|
||||
return ELF_TAB_ERROR_RET;
|
||||
}
|
||||
|
||||
/* read count is 1, should return 1 if it is succ */
|
||||
if (fread(str_tab, sec_entry->sh_size, 1, g_infile) != 1) {
|
||||
printf("str fread failed\n");
|
||||
ret = ELF_TAB_ERROR_RET;
|
||||
goto free_out;
|
||||
}
|
||||
|
||||
/* write into out image file */
|
||||
if (fseek(g_outfile, 0, SEEK_END) != 0) {
|
||||
printf("str fseek failed\n");
|
||||
ret = ELF_TAB_ERROR_RET;
|
||||
goto free_out;
|
||||
}
|
||||
|
||||
/* write count is 1, should return 1 if it is succ */
|
||||
if (fwrite(str_tab, sec_entry->sh_size, 1, g_outfile) != 1) {
|
||||
printf("str fwrite failed\n");
|
||||
ret = ELF_TAB_ERROR_RET;
|
||||
goto free_out;
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
|
||||
free_out:
|
||||
free(str_tab);
|
||||
str_tab = NULL;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int32_t main(int32_t argc, char *argv[])
|
||||
{
|
||||
int32_t ret;
|
||||
int32_t i;
|
||||
int32_t j = 0;
|
||||
|
||||
ret = check_input_param(argc, argv);
|
||||
if (ret != 0)
|
||||
goto out;
|
||||
|
||||
ret = read_elf_section_head();
|
||||
if (ret != 0)
|
||||
goto out;
|
||||
|
||||
struct elf_shdr *sec_entry = g_sec_head;
|
||||
/* traverse all section and get the symtab and strtab of elf */
|
||||
for (i = 0; i < g_elf_head->eh_shnum; i++) {
|
||||
if (sec_entry->sh_size == 0) {
|
||||
sec_entry++;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* read the symtab out */
|
||||
if (sec_entry->sh_type == SHT_SYMTAB) {
|
||||
ret = read_sym_tab(sec_entry);
|
||||
if (ret != 0) {
|
||||
printf("Error: read sym failed\n");
|
||||
goto out;
|
||||
}
|
||||
} else if (sec_entry->sh_type == SHT_STRTAB) {
|
||||
if (j != 0)
|
||||
continue;
|
||||
ret = read_str_tab(sec_entry);
|
||||
if (ret != 0) {
|
||||
printf("Error: read str failed\n");
|
||||
goto out;
|
||||
}
|
||||
j++;
|
||||
}
|
||||
sec_entry++;
|
||||
}
|
||||
|
||||
/* ret should be success now */
|
||||
ret = 0;
|
||||
out:
|
||||
out_clean();
|
||||
return ret;
|
||||
}
|
||||
@@ -1,415 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
#trustedcore package header generator
|
||||
# Copyright (C) 2022 Huawei Technologies Co., Ltd.
|
||||
# Licensed under the Mulan PSL v2.
|
||||
# You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
# You may obtain a copy of Mulan PSL v2 at:
|
||||
# http://license.coscl.org.cn/MulanPSL2
|
||||
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
|
||||
# PURPOSE.
|
||||
# See the Mulan PSL v2 for more details.
|
||||
"""Description:package image for phone
|
||||
"""
|
||||
from __future__ import print_function
|
||||
import struct
|
||||
import os
|
||||
import sys
|
||||
import binascii
|
||||
import shutil
|
||||
import subprocess
|
||||
|
||||
header_size = 0x400
|
||||
kernel_load_addr = 0
|
||||
image_load_addr = 0
|
||||
global_img_symtab_offset = 0
|
||||
global_img_symtab_size = 0
|
||||
global_img_strtab_size = 0
|
||||
global_img_bss_size = 0
|
||||
rtosck_img_symtab_offset = 0
|
||||
rtosck_img_symtab_size = 0
|
||||
rtosck_img_strtab_size = 0
|
||||
rtosck_img_bss_size = 0
|
||||
|
||||
compare_image = 0
|
||||
|
||||
img_paths = 0
|
||||
task_names = 1
|
||||
heap_sizes = 3
|
||||
task_uuids = 2
|
||||
|
||||
elf_class = 0
|
||||
tee_dict = {'TEE_GOT_START': 0,
|
||||
'TEE_GOT_END': 0,
|
||||
'TEE_RELPLT_START': 0,
|
||||
'TEE_RELPLT_END': 0,
|
||||
'TEE_RELDYN_START': 0,
|
||||
'TEE_RELDYN_END': 0,
|
||||
'TEE_RELADYN_START': 0,
|
||||
'TEE_RELADYN_END': 0,
|
||||
'TEE_DYNAMIC_START': 0,
|
||||
'TEE_DYNAMIC_END': 0,
|
||||
'TEE_DYNSYM_START': 0,
|
||||
'TEE_DYNSYM_END': 0,
|
||||
'TEE_RELAPLT_START': 0,
|
||||
'TEE_RELAPLT_END': 0,
|
||||
'GT_DATA_START': 0,
|
||||
'GT_DATA_END': 0
|
||||
}
|
||||
|
||||
EI_NIDENT = 16
|
||||
ELFINFO_CLASS_64 = '\x02'
|
||||
ELFINFO_MAG0 = '\x7f'
|
||||
ELFINFO_MAG3 = 'F'
|
||||
ELFINFO_MAG2_INDEX = 2
|
||||
ELFINFO_MAG0_INDEX = 0
|
||||
ELFINFO_CLASS_32 = '\x01'
|
||||
ELFINFO_MAG1 = 'E'
|
||||
ELFINFO_MAG1_INDEX = 1
|
||||
ELFINFO_CLASS_INDEX = 4
|
||||
ELFINFO_MAG2 = 'L'
|
||||
ELFINFO_MAG3_INDEX = 3
|
||||
|
||||
|
||||
def pack_header_item(input_file, img_addr, img_size, img_file):
|
||||
"""package header item for image header"""
|
||||
task_size = img_file[heap_sizes]
|
||||
task_name = img_file[task_names]
|
||||
task_uuid = img_file[task_uuids]
|
||||
out_strs = struct.pack('<3I20s16s',
|
||||
img_addr,
|
||||
img_size,
|
||||
task_size,
|
||||
task_name.encode("utf-8"),
|
||||
task_uuid.encode("utf-8"));
|
||||
input_file.write(out_strs)
|
||||
|
||||
|
||||
def get_file_size(name):
|
||||
"""get file size for params"""
|
||||
size = os.path.getsize(name)
|
||||
mod = size % 4
|
||||
if mod:
|
||||
size += 4 - mod
|
||||
return size
|
||||
|
||||
|
||||
#filelist:
|
||||
#0: rtosck.img
|
||||
#1: globaltask.img
|
||||
#2-N: ta.sec
|
||||
#N+1: encRndFile
|
||||
def generate_header(files_list, header_filename):
|
||||
"""generate image header"""
|
||||
with open(header_filename, 'wb') as file_names:
|
||||
|
||||
t_filelist = files_list[1:]
|
||||
task_num = len(t_filelist)
|
||||
filesize_list = [get_file_size(i[img_paths]) for i in files_list]
|
||||
img_offset_list = []
|
||||
kernel_file = files_list[0]
|
||||
kernel_size = filesize_list[0]
|
||||
|
||||
filesize_total = sum(filesize_list[1:])
|
||||
|
||||
offset = header_size
|
||||
img_offset = offset
|
||||
for index in enumerate(t_filelist):
|
||||
img_offset_list.append(img_offset)
|
||||
img_size = filesize_list[index + 1]
|
||||
img_offset += img_size
|
||||
|
||||
task_offset = header_size
|
||||
kernel_offset = header_size + filesize_total;
|
||||
print("kernel_size=0x{0:x}, load addr=0x{1:x}".format(
|
||||
kernel_size, kernel_load_addr))
|
||||
output_strs = struct.pack('<9I',
|
||||
header_size,
|
||||
kernel_load_addr,
|
||||
kernel_size,
|
||||
task_num,
|
||||
filesize_total,
|
||||
kernel_got_size,
|
||||
image_load_addr,
|
||||
task_offset,
|
||||
kernel_offset)
|
||||
file_names.write(output_strs)
|
||||
|
||||
for index, imgfile in enumerate(t_filelist):
|
||||
img_offset = img_offset_list[index]
|
||||
img_size = filesize_list[index + 1]
|
||||
print("[{0}]:Name={1} addr=0x{2:x} size=0x{3:x}".format(index,
|
||||
imgfile[task_names], img_offset, img_size))
|
||||
pack_header_item(file_names, img_offset, img_size, imgfile)
|
||||
|
||||
output_strs = struct.pack('<4I',
|
||||
global_img_symtab_offset,
|
||||
global_img_symtab_size,
|
||||
global_img_strtab_size,
|
||||
global_img_bss_size)
|
||||
file_names.write(output_strs)
|
||||
|
||||
output_strs = struct.pack('<4I',
|
||||
rtosck_img_symtab_offset,
|
||||
rtosck_img_symtab_size,
|
||||
rtosck_img_strtab_size,
|
||||
rtosck_img_bss_size)
|
||||
file_names.write(output_strs)
|
||||
|
||||
global tee_dict
|
||||
global elf_class
|
||||
got_start = tee_dict.get('TEE_GOT_START')
|
||||
got_size = tee_dict.get('TEE_GOT_END') - tee_dict.get('TEE_GOT_START')
|
||||
relplt_start = tee_dict.get('TEE_RELPLT_START')
|
||||
relplt_size = tee_dict.get('TEE_RELPLT_END') - \
|
||||
tee_dict.get('TEE_RELPLT_START')
|
||||
reldyn_start = tee_dict.get('TEE_RELDYN_START')
|
||||
reldyn_size = tee_dict.get('TEE_RELDYN_END') - \
|
||||
tee_dict.get('TEE_RELDYN_START')
|
||||
reladyn_start = tee_dict.get('TEE_RELADYN_START')
|
||||
reladyn_size = tee_dict.get('TEE_RELADYN_END') - \
|
||||
tee_dict.get('TEE_RELADYN_START')
|
||||
dynamic_start = tee_dict.get('TEE_DYNAMIC_START')
|
||||
dynamic_size = tee_dict.get('TEE_DYNAMIC_END') - \
|
||||
tee_dict.get('TEE_DYNAMIC_START')
|
||||
dynsym_start = tee_dict.get('TEE_DYNSYM_START')
|
||||
dynsym_size = tee_dict.get('TEE_DYNSYM_END') - \
|
||||
tee_dict.get('TEE_DYNSYM_START')
|
||||
relaplt_start = tee_dict.get('TEE_RELAPLT_START')
|
||||
relaplt_size = tee_dict.get('TEE_RELAPLT_END') - \
|
||||
tee_dict.get('TEE_RELAPLT_START')
|
||||
gtdata_start = tee_dict.get('GT_DATA_START')
|
||||
gtdata_size = tee_dict.get('GT_DATA_END') - \
|
||||
tee_dict.get('GT_DATA_START')
|
||||
|
||||
elf_put = struct.pack('<17I',
|
||||
elf_class,
|
||||
got_start,
|
||||
got_size,
|
||||
relplt_start,
|
||||
relplt_size,
|
||||
reldyn_start,
|
||||
reldyn_size,
|
||||
reladyn_start,
|
||||
reladyn_size,
|
||||
dynamic_start,
|
||||
dynamic_size,
|
||||
dynsym_start,
|
||||
dynsym_size,
|
||||
relaplt_start,
|
||||
relaplt_size,
|
||||
gtdata_start,
|
||||
gtdata_size)
|
||||
file_names.write(elf_put)
|
||||
|
||||
#write image
|
||||
for index, imgfile in enumerate(t_filelist):
|
||||
offset = img_offset_list[index]
|
||||
file_names.seek(offset)
|
||||
print("place img_[{0}] at 0x{1:x}, imgfile:{2}".format(
|
||||
index, offset, imgfile[img_paths]))
|
||||
with open(imgfile[img_paths], 'rb') as inf:
|
||||
file_names.write(inf.read())
|
||||
|
||||
file_names.seek(kernel_offset)
|
||||
with open(kernel_file[img_paths], 'rb') as inf:
|
||||
file_names.write(inf.read())
|
||||
|
||||
|
||||
#align image file according 64 bytes
|
||||
def align_image(img_name):
|
||||
"""image size align for 64 bytes"""
|
||||
img_size = os.path.getsize(img_name)
|
||||
print("The size of image file is {0}".format(img_size))
|
||||
|
||||
mod = img_size % 64
|
||||
if mod:
|
||||
img_size += 64 - mod
|
||||
|
||||
with open(img_name, 'ab+') as img:
|
||||
img.seek(0, 2)
|
||||
img.truncate(img_size)
|
||||
|
||||
img.close()
|
||||
img_size = os.path.getsize(img_name)
|
||||
print("After align, the size of image file is {0}".format(img_size))
|
||||
|
||||
|
||||
def encryt_image(img_name):
|
||||
"""encrypto image"""
|
||||
if len(sys.argv) > 8:
|
||||
kecc1pub_file = sys.argv[8]
|
||||
else:
|
||||
print("kecc1pub_file not exist:do not do encryption.")
|
||||
return
|
||||
if kecc1pub_file.startswith('kecc1pub_'):
|
||||
print("kecc1pub_file exist:go on do encryption.")
|
||||
else:
|
||||
print("kecc1pub_file not exist:do not do encryption.")
|
||||
return
|
||||
|
||||
pwd = sys.path[0]
|
||||
path_one = pwd + '/../prebuild/tee-kernel-release/tools/img_encry_tool/'
|
||||
path_two = pwd + \
|
||||
'/../prebuild/tee-kernel-local-release/tools/img_encry_tool/'
|
||||
if os.path.exists(path_one):
|
||||
wraptool_path = path_one
|
||||
elif os.path.exists(path_two):
|
||||
wraptool_path = path_two
|
||||
else:
|
||||
print("img_encry_tool path not exists")
|
||||
return
|
||||
#=== create temp dir for image encryption
|
||||
tmp_encimg_file_path = wraptool_path + 'tmp_enc/'
|
||||
if os.path.exists(tmp_encimg_file_path):
|
||||
shutil.rmtree(tmp_encimg_file_path)
|
||||
os.mkdir(tmp_encimg_file_path)
|
||||
|
||||
#=== step 1: generate Krnd/IV, used to encrypimage
|
||||
print("step 1: generate Krnd/IV")
|
||||
krnd_file = tmp_encimg_file_path + 'krnd.rnd'
|
||||
|
||||
subprocess.run(["openssl", "rand", "-out", krnd_file, \
|
||||
"32"], shell=False, check=True)
|
||||
rnd_file_size = os.path.getsize(krnd_file)
|
||||
|
||||
with open(krnd_file, 'rb') as rndfp:
|
||||
random = rndfp.read(rnd_file_size)
|
||||
rndfp.close()
|
||||
random_str = struct.unpack("32s", random)
|
||||
|
||||
input_key_str = ''
|
||||
for rand_chr in random_str:
|
||||
input_key_str = input_key_str + rand_chr.decode("utf8", "ignore")
|
||||
aes_key = binascii.b2a_hex(input_key_str.encode())
|
||||
|
||||
krnd_iv_file = tmp_encimg_file_path + 'krndIV.iv'
|
||||
subprocess.run(["openssl", "rand", "-out", \
|
||||
krnd_iv_file, "16"], shell=False, check=True)
|
||||
iv_file_size = os.path.getsize(krnd_iv_file)
|
||||
with open(krnd_iv_file, 'rb') as ivfps:
|
||||
iv_fps = ivfps.read(iv_file_size)
|
||||
ivfps.close()
|
||||
iv_str = struct.unpack("16s", iv_fps)
|
||||
|
||||
input_key_str = ''
|
||||
for iv_chr in iv_str:
|
||||
input_key_str = input_key_str + iv_chr.decode("utf8", "ignore")
|
||||
aes_rnd_iv = binascii.b2a_hex(input_key_str.encode())
|
||||
|
||||
#generate enc image
|
||||
enc_img_file = tmp_encimg_file_path + 'trustedcore_enc.img'
|
||||
subprocess.run(["openssl", "enc", "-aes-256-cbc", "-in", img_name, \
|
||||
"-out", enc_img_file, "-K", aes_key, "-iv", aes_rnd_iv], \
|
||||
shell=False, check=True)
|
||||
#encry img must be 64 bytes align
|
||||
align_image(enc_img_file)
|
||||
|
||||
print("step 2: generate En_Krnd/IV")
|
||||
with open(krnd_file, 'ab') as rndfp:
|
||||
with open(krnd_iv_file, 'rb') as ivfps:
|
||||
rndfp.write(ivfps.read())
|
||||
rndfp.close()
|
||||
# use wrap tool to generate enc_img header:trustedcore_enc_header.bin
|
||||
wraptool_file = wraptool_path + 'wraptool.c'
|
||||
wraptool_outfile = wraptool_path + 'wraptool'
|
||||
fastboot_pubkey_file = wraptool_path + kecc1pub_file
|
||||
enc_img_head_file = tmp_encimg_file_path + 'trustedcore_enc_header.bin'
|
||||
|
||||
subprocess.run(["cd", wraptool_path], shell=False, check=True)
|
||||
resp = subprocess.run([wraptool_outfile, krnd_file, fastboot_pubkey_file, \
|
||||
enc_img_head_file], shell=False, check=True)
|
||||
if resp.returncode == 0:
|
||||
print("suceed to do key wrap")
|
||||
else:
|
||||
raise RuntimeError("Failed to do key wrap process.")
|
||||
subprocess.run(["cd", "-"], shell=False, check=True)
|
||||
|
||||
subprocess.run(["cp", img_name, tmp_encimg_file_path, \
|
||||
"/"], shell=False, check=True)
|
||||
|
||||
#=== step 3:pack the ecrypt image
|
||||
#then add the len and trustedcore_enc.img
|
||||
print("step 3: pack the encryption image")
|
||||
enc_img_final_file = img_name
|
||||
no_encry_img = img_name + '.no_encrypt'
|
||||
subprocess.run(["cp", enc_img_final_file, no_encry_img], \
|
||||
shell=False, check=True)
|
||||
subprocess.run(["rm", enc_img_final_file], shell=False, check=True)
|
||||
resp = subprocess.run(["cp", enc_img_head_file, enc_img_final_file], \
|
||||
shell=False, check=True)
|
||||
if resp == 0:
|
||||
print("suceed to cp trustedcore_enc_header.bin")
|
||||
else:
|
||||
raise RuntimeError("Failed to cp trustedcore_enc_header.bin.")
|
||||
|
||||
#add encry img total size
|
||||
img_size = os.path.getsize(enc_img_file)
|
||||
print("The size of image file is {0}".format(img_size))
|
||||
write_text = struct.pack('<1I',
|
||||
img_size)
|
||||
with open(enc_img_final_file, 'ab') as enc_img_fp:
|
||||
enc_img_fp.write(write_text)
|
||||
enc_img_fp.close()
|
||||
|
||||
#enc img header total 256 bytes
|
||||
img_head_size = os.path.getsize(enc_img_final_file)
|
||||
mod = img_head_size % 256
|
||||
if mod:
|
||||
img_head_size += 256 - mod
|
||||
with open(enc_img_final_file, 'ab+') as img:
|
||||
img.seek(0, 2)
|
||||
img.truncate(img_head_size)
|
||||
img.close()
|
||||
|
||||
#add the encryt image append the header
|
||||
with open(enc_img_final_file, 'ab') as enc_img_fp:
|
||||
with open(enc_img_file, 'rb') as ivfps:
|
||||
enc_img_fp.write(ivfps.read())
|
||||
enc_img_fp.close()
|
||||
|
||||
#remove the temp dir
|
||||
if os.path.exists(tmp_encimg_file_path):
|
||||
shutil.rmtree(tmp_encimg_file_path)
|
||||
print("End: succeed to pack the encryption image")
|
||||
#=== end
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
input_file_list = []
|
||||
internal_task_list = []
|
||||
image_file_path = sys.argv[1]
|
||||
input_kernel_file = sys.argv[2],
|
||||
kernel_load_addr = int(sys.argv[3], 16)
|
||||
kernel_got_size = int(sys.argv[4], 16)
|
||||
input_file_list.append(input_kernel_file)
|
||||
|
||||
rtosck_sym_head_args = sys.argv[5]
|
||||
rtosck_sym_item = rtosck_sym_head_args.split(',');
|
||||
rtosck_img_symtab_offset = int(rtosck_sym_item[0])
|
||||
rtosck_img_symtab_size = int(rtosck_sym_item[1], 16)
|
||||
rtosck_img_strtab_size = int(rtosck_sym_item[2]) - rtosck_img_symtab_size;
|
||||
|
||||
compare_image = int(sys.argv[6], 10)
|
||||
|
||||
image_load_addr = int(sys.argv[7], 16)
|
||||
|
||||
pwd_path = sys.path[0]
|
||||
tmp_internal_task_path = pwd_path + '/tmp/'
|
||||
print("cwd={0}, tmp_internal_task_path={1}".format(
|
||||
pwd_path, tmp_internal_task_path))
|
||||
if os.path.exists(tmp_internal_task_path):
|
||||
shutil.rmtree(tmp_internal_task_path)
|
||||
os.mkdir(tmp_internal_task_path)
|
||||
|
||||
#encrypt verify elf
|
||||
for index_elf, taskitem in enumerate(input_file_list):
|
||||
if index_elf in (0, 1):
|
||||
internal_task_list.append(taskitem)
|
||||
continue
|
||||
|
||||
generate_header(internal_task_list, image_file_path)
|
||||
shutil.rmtree(tmp_internal_task_path)
|
||||
align_image(image_file_path)
|
||||
encryt_image(image_file_path)
|
||||
@@ -1,107 +0,0 @@
|
||||
#!/bin/bash
|
||||
# Copyright (C) 2022 Huawei Technologies Co., Ltd.
|
||||
# Licensed under the Mulan PSL v2.
|
||||
# You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
# You may obtain a copy of Mulan PSL v2 at:
|
||||
# http://license.coscl.org.cn/MulanPSL2
|
||||
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
|
||||
# PURPOSE.
|
||||
# See the Mulan PSL v2 for more details.
|
||||
set -e
|
||||
COMPARE_IMAGE="$2"
|
||||
|
||||
KERNEL_TEXT_BASE=0; echo "kernel text base=${KERNEL_TEXT_BASE}"
|
||||
|
||||
PLATFORM_CHOOSE="$1"; echo "platform is ${PLATFORM_CHOOSE}"
|
||||
|
||||
CHIP_CHOOSE="$3"; echo "chip is ${CHIP_CHOOSE}"
|
||||
|
||||
TEEOS_IMG_ENCRYPT="$4"; echo "TEE IMG encrypt is ${TEEOS_IMG_ENCRYPT}"
|
||||
|
||||
if [ "${TEEOS_IMG_ENCRYPT}" ]; then
|
||||
echo "TEE IMG encrypt is ${TEEOS_IMG_ENCRYPT}"
|
||||
else
|
||||
echo "Error:there is no input TEEOS_IMG_ENCRYPT Feature"; exit -1
|
||||
fi
|
||||
|
||||
TEEOS_LOG_ENCODE="$5"; echo "TEEOS_LOG_ENCODE is ${TEEOS_LOG_ENCODE}"
|
||||
|
||||
IMAGE_LOAD_ADDR=0; echo "IMAGE_LOAD_ADDR is ${IMAGE_LOAD_ADDR}"
|
||||
|
||||
CURDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"; echo "CURDIR: ${CURDIR}"
|
||||
|
||||
if [[ "${IMAGE_ROOT}" == '' ]]; then
|
||||
echo "IMAGE_ROOT must specify"; exit -1;
|
||||
fi
|
||||
echo "IMAGE_ROOT: ${IMAGE_ROOT}"
|
||||
|
||||
DST_PATH="${IMAGE_ROOT}"/trustedcore.img
|
||||
KERNEL_NAME=teehm.img
|
||||
KERNEL_PATH="${IMAGE_ROOT}"/"${KERNEL_NAME}"
|
||||
|
||||
IMG_PATH="${IMAGE_ROOT}"
|
||||
KERNEL_ELF="${IMG_PATH}"/"${KERNEL_NAME}".elf
|
||||
ELF_EXTRACT_SRC="${CURDIR}"/elf_extract.c
|
||||
ELF_EXTRACT_BIN="${CURDIR}"/elf_extract
|
||||
|
||||
if [ -f "${ELF_EXTRACT_BIN}" ];then
|
||||
echo "Info: found elf_extract tool"
|
||||
rm "${ELF_EXTRACT_BIN}"
|
||||
fi
|
||||
|
||||
set +o errexit; GCC_VERSION=$(expr `gcc -dumpversion | cut -f1 -d.` \>= 5); set -o errexit
|
||||
if [ "${GCC_VERSION}" -eq 1 ];then
|
||||
DATA_TIME=-Wdate-time
|
||||
fi
|
||||
|
||||
GENERAL_OPTIONS="-Wall -Werror -Wformat=2 -fPIC -fstack-protector-all -Wextra -Wfloat-equal \
|
||||
-Wshadow -fsigned-char -fno-strict-aliasing -fno-common -pipe -Wtrampolines"
|
||||
LINK_OPTIONS="-pie -Wl,-z,relro -Wl,-z,noexecstack -Wl,-z,now -Wl,-Bsymbolic -Wl,--no-undefined -rdynamic"
|
||||
if [ -f "${ELF_EXTRACT_SRC}" ];then
|
||||
gcc "${ELF_EXTRACT_SRC}" -o "${ELF_EXTRACT_BIN}" ${GENERAL_OPTIONS} ${DATA_TIME} -D${HM_ARCH} ${LINK_OPTIONS}
|
||||
if [ "$?" -ne 0 ];then
|
||||
echo "Error: build elf_extract tool fail"; exit -1
|
||||
fi
|
||||
else
|
||||
echo "Error: Can not find elf_extract tool"; exit -1
|
||||
fi
|
||||
|
||||
#Add new internal task here:
|
||||
TASK_LIST=""
|
||||
TASK_HIVCODEC_LIST=""
|
||||
|
||||
KERNEL_SYMTAB_OFFSET=$(ls -l "${KERNEL_PATH}"| awk '{print $5}')
|
||||
|
||||
"${ELF_EXTRACT_BIN}" "rtosck" "${KERNEL_ELF}" "${KERNEL_PATH}"
|
||||
|
||||
if [ "$?" -ne 0 ];then
|
||||
echo "Error: elf64_extract rtosck failed."; exit -1
|
||||
fi
|
||||
|
||||
KERNEL_TOTAL_SIZE=$(ls -l "${KERNEL_PATH}"| awk '{print $5}')
|
||||
KERNEL_SYMSTR_TOTAL_SIZE=$(expr "${KERNEL_TOTAL_SIZE}" - "${KERNEL_SYMTAB_OFFSET}")
|
||||
|
||||
KERNEL_SYMTAB_SIZE=$(readelf -S -W "${KERNEL_ELF}" 2>/dev/null | awk '{if ($2==".symtab") {print $6} else {if ($3==".symtab") {print $7}}}')
|
||||
KERNEL_SYM_HEAD="${KERNEL_SYMTAB_OFFSET},${KERNEL_SYMTAB_SIZE},${KERNEL_SYMSTR_TOTAL_SIZE}"
|
||||
|
||||
GOT_SIZE=0
|
||||
"${CURDIR}"/header.py "${DST_PATH}" "${KERNEL_PATH}" "${KERNEL_TEXT_BASE}" "${GOT_SIZE}" "${KERNEL_SYM_HEAD}" "${COMPARE_IMAGE}" "${IMAGE_LOAD_ADDR}" "${KECC1PUB_PLAT}"
|
||||
RET="$?"
|
||||
if [ 0 -ne "${RET}" ];then
|
||||
echo "failed to do header.py ret is ${RET}"
|
||||
if [ -f "${DST_PATH}" ];then
|
||||
rm "${DST_PATH}"
|
||||
fi
|
||||
else
|
||||
echo "succeed to execute head.py"
|
||||
fi
|
||||
|
||||
if [ x${VERSION_DDK} != "xy" ];then
|
||||
if [ -f "${ELF_EXTRACT_BIN}" ];then
|
||||
echo "Info: found elf64_extract tool"; rm "${ELF_EXTRACT_BIN}"
|
||||
fi
|
||||
fi
|
||||
|
||||
RET="$?"
|
||||
exit "${RET}"
|
||||
Reference in New Issue
Block a user