Skip to content

Commit

Permalink
build: Support build kernelflinger with Slim Bootloader
Browse files Browse the repository at this point in the history
Build kernelflinger as ELF file

Tracked-On: OAM-110589
Signed-off-by: Chen, Gang G <[email protected]>
  • Loading branch information
GangSecurity authored and sysopenci committed Jun 22, 2023
1 parent a1aedee commit bf28706
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 9 deletions.
49 changes: 47 additions & 2 deletions core/definitions.mk
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ IAFW_BUILD_SYSTEM := $(INTEL_PATH_BUILD)/core
BUILD_EFI_STATIC_LIBRARY := $(IAFW_BUILD_SYSTEM)/iafw_static_library.mk
BUILD_IAFW_STATIC_LIBRARY := $(IAFW_BUILD_SYSTEM)/iafw_static_library.mk
BUILD_EFI_EXECUTABLE := $(IAFW_BUILD_SYSTEM)/efi_executable.mk
BUILD_SBL_EXECUTABLE := $(IAFW_BUILD_SYSTEM)/sbl_executable.mk

# Override default definition
CLEAR_VARS := $(IAFW_BUILD_SYSTEM)/clear_vars.mk
Expand All @@ -15,19 +16,36 @@ GENERATE_VERITY_KEY := $(HOST_OUT_EXECUTABLES)/generate_verity_key$(HOST_EXECUTA
OPENSSL := openssl
SBSIGN := sbsign
MKDOSFS := mkdosfs
MKEXT2IMG := $(HOST_OUT_EXECUTABLES)/mkext2img
DUMPEXT2IMG := $(HOST_OUT_EXECUTABLES)/dumpext2img
#MKEXT2IMG := $(HOST_OUT_EXECUTABLES)/mkext2img
#DUMPEXT2IMG := $(HOST_OUT_EXECUTABLES)/dumpext2img
MCOPY := mcopy
SESL := sign-efi-sig-list$(HOST_EXECUTABLE_SUFFIX)
CTESL := cert-to-efi-sig-list$(HOST_EXECUTABLE_SUFFIX)
IASL := $(INTEL_PATH_BUILD)/acpi-tools/linux64/bin/iasl

# Generation
KF4SBL_SYMBOLS_ZIP := $(PRODUCT_OUT)/kf4sbl_symbols.zip
FB4SBL_SYMBOLS_ZIP := $(PRODUCT_OUT)/fb4sbl_symbols.zip

# Extra host tools we need built to use our *_from_target_files
# or sign_target_files_* scripts
INTEL_OTATOOLS := \
$(GENERATE_VERITY_KEY) \
$(AVBTOOL)

ifeq ($(KERNELFLINGER_SUPPORT_NON_EFI_BOOT),true)
# NON UEFI platform
INTEL_OTATOOLS += \
# $(MKEXT2IMG) \
# $(DUMPEXT2IMG) \
$(FASTBOOT) \
$(IASL)
endif

ifeq ($(BOARD_USE_SBL),true)
INTEL_OTATOOLS += abl_toolchain
endif

otatools: $(INTEL_OTATOOLS)

# FIXME: may be unsafe to omit -no-sse
Expand Down Expand Up @@ -73,6 +91,7 @@ GNU_EFI_CRT0 := crt0-efi-$(TARGET_IAFW_ARCH_NAME)
LIBPAYLOAD_CRT0 := crt0-libpayload-$(TARGET_IAFW_ARCH_NAME)

TARGET_EFI_LDS := $(IAFW_BUILD_SYSTEM)/elf_$(TARGET_IAFW_ARCH_NAME)_efi.lds
TARGET_SBL_LDS := $(IAFW_BUILD_SYSTEM)/elf_$(TARGET_IAFW_ARCH_NAME)_sbl.lds
TARGET_IAFW_GLOBAL_OBJCOPY_FLAGS := \
-j .text -j .sdata -j .data \
-j .dynamic -j .dynsym -j .rel \
Expand Down Expand Up @@ -125,6 +144,32 @@ $(hide) $(IAFW_OBJCOPY) $(PRIVATE_OBJCOPY_FLAGS) \
$(hide) $(SBSIGN) --key $1 --cert $2 --output $@ $(@:.efi=.efiunsigned)
endef

define transform-o-to-sbl-executable
@echo "target SBL Executable: $(PRIVATE_MODULE) ($@)"
$(hide) mkdir -p $(dir $@)
$(hide) $(IAFW_LD) $1 \
--defsym=CONFIG_LP_BASE_ADDRESS=$(LIBPAYLOAD_BASE_ADDRESS) \
--defsym=CONFIG_LP_HEAP_SIZE=$(LIBPAYLOAD_HEAP_SIZE) \
--defsym=CONFIG_LP_STACK_SIZE=$(LIBPAYLOAD_STACK_SIZE) \
--whole-archive $(call module-built-files,$(LIBPAYLOAD_CRT0)) --no-whole-archive \
$(PRIVATE_ALL_OBJECTS) --start-group $(PRIVATE_ALL_STATIC_LIBRARIES) --end-group $(IAFW_LIBCLANG) \
-Map $(@:.sbl=.map) -o $(@:.sbl=.sym.elf)
$(hide)$(IAFW_STRIP) --strip-all $(@:.sbl=.sym.elf) -o $(@:.sbl=.elf)

$(hide) cp $(@:.sbl=.elf) $@


python3 $(INTEL_PATH_BUILD)/containertool/GenContainer.py create -t NORMAL -cl ELF1:$@ -k $(INTEL_PATH_BUILD)/testkeys/OS1_TestKey_Priv_RSA2048.pem -o $(PRODUCT_OUT)/sbl_os


$(hide) if [ "$(PRIVATE_MODULE:debug=)" = fb4sbl-user ]; then \
zip -juy $(FB4SBL_SYMBOLS_ZIP) $(@:.sbl=.map) $(@:.sbl=.sym.elf); \
zip -juy $(FB4SBL_SYMBOLS_ZIP) $@; \
elif [ "$(PRIVATE_MODULE:debug=)" = kf4sbl-user ]; then \
zip -juy $(KF4SBL_SYMBOLS_ZIP) $(@:.sbl=.map) $(@:.sbl=.sym.elf); \
fi
endef

# Hook up the prebuilts generation mechanism
include $(INTEL_PATH_COMMON)/external/external.mk

Expand Down
58 changes: 58 additions & 0 deletions core/elf_ia32_sbl.lds
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
OUTPUT_FORMAT(elf32-i386)
OUTPUT_ARCH(i386)

ENTRY(_entry)

SECTIONS
{
. = CONFIG_LP_BASE_ADDRESS;

. = ALIGN(16);
_start = .;

.text : {
*(.text._entry)
*(.text)
*(.text.*)
}

.rodata : {
*(.rodata)
*(.rodata.*)
}

.data : {
*(.data)
*(.data.*)
}

_edata = .;

.bss : {
*(.sbss)
*(.sbss.*)
*(.bss)
*(.bss.*)
*(COMMON)

/* Stack and heap */

. = ALIGN(16);
_heap = .;
. += CONFIG_LP_HEAP_SIZE;
. = ALIGN(16);
_eheap = .;

_estack = .;
. += CONFIG_LP_STACK_SIZE;
. = ALIGN(16);
_stack = .;
}

_end = .;

/DISCARD/ : {
*(.comment)
*(.note*)
}
}
55 changes: 55 additions & 0 deletions core/sbl_executable.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
ifeq ($(strip $(LOCAL_MODULE_CLASS)),)
LOCAL_MODULE_CLASS := SBL
endif

ifeq ($(strip $(LOCAL_MODULE_SUFFIX)),)
LOCAL_MODULE_SUFFIX := .sbl
endif

ifeq ($(strip $(LOCAL_MODULE_PATH)),)
LOCAL_MODULE_PATH := $(PRODUCT_OUT)/sbl
endif

LOCAL_CC := $(IAFW_CC)
LOCAL_CLANG := true
LOCAL_SANITIZE := never
LOCAL_NO_DEFAULT_COMPILER_FLAGS := true
LOCAL_CFLAGS += $(TARGET_IAFW_GLOBAL_CFLAGS)
LOCAL_ASFLAGS += $(TARGET_IAFW_ASFLAGS)
LOCAL_LDFLAGS := $(TARGET_IAFW_GLOBAL_LDFLAGS) -static \
-T $(TARGET_SBL_LDS) $(LOCAL_LDFLAGS)
# If kernel enforce superpages the .text section gets aligned at
# offset 0x200000 which break multiboot compliance.
LOCAL_LDFLAGS += -z max-page-size=0x1000
LOCAL_SBL_LDFALGS := $(LOCAL_LDFLAGS)
LOCAL_OBJCOPY_FLAGS := $(TARGET_IAFW_GLOBAL_OBJCOPY_FLAGS) $(LOCAL_OBJCOPY_FLAGS)

skip_build_from_source :=
ifdef LOCAL_PREBUILT_MODULE_FILE
ifeq (,$(call if-build-from-source,$(LOCAL_MODULE),$(LOCAL_PATH)))
include $(BUILD_SYSTEM)/prebuilt_internal.mk
skip_build_from_source := true
endif
endif

ifndef skip_build_from_source

ifdef LOCAL_IS_HOST_MODULE
$(error This file should not be used to build host binaries. Included by (or near) $(lastword $(filter-out config/%,$(MAKEFILE_LIST))))
endif

WITHOUT_LIBCOMPILER_RT := true
include $(BUILD_SYSTEM)/binary.mk
WITHOUT_LIBCOMPILER_RT :=

LIBPAYLOAD_CRT0_LIB := $(call intermediates-dir-for,STATIC_LIBRARIES,$(LIBPAYLOAD_CRT0))/$(LIBPAYLOAD_CRT0).a
all_objects += $(LIBPAYLOAD_CRT0_LIB)

$(LOCAL_BUILT_MODULE): PRIVATE_OBJCOPY_FLAGS := $(LOCAL_OBJCOPY_FLAGS)

#$(LOCAL_BUILT_MODULE): $(all_objects) $(all_libraries) $(SBLIMAGE) $(SBLSIGN)
$(LOCAL_BUILT_MODULE): $(all_objects) $(all_libraries)
$(call transform-o-to-sbl-executable,$(LOCAL_SBL_LDFALGS))

endif # skip_build_from_source

14 changes: 7 additions & 7 deletions tasks/publish.mk
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ $(PUB_OSAGNOSTIC_TAG): publish_mkdir_dest $(OS_AGNOSTIC_INFO)
$(hide)($(ACP) $(OS_AGNOSTIC_INFO) $@)
endif

# Publish kf4abl symbols files
.PHONY: publish_kf4abl_symbols
# Publish kf4sbl symbols files
.PHONY: publish_kf4sbl_symbols
ifeq ($(TARGET_BUILD_VARIANT:debug=)|$(KERNELFLINGER_SUPPORT_NON_EFI_BOOT),user|true)
publish_kf4abl_symbols: publish_mkdir_dest kf4abl-$(TARGET_BUILD_VARIANT) fb4abl-$(TARGET_BUILD_VARIANT)
$(hide)($(ACP) $(KF4ABL_SYMBOLS_ZIP) $(FB4ABL_SYMBOLS_ZIP) $(publish_dest))
publish_kf4sbl_symbols: publish_mkdir_dest kf4sbl-$(TARGET_BUILD_VARIANT) fb4sbl-$(TARGET_BUILD_VARIANT)
$(hide)($(ACP) $(KF4SBL_SYMBOLS_ZIP) $(FB4SBL_SYMBOLS_ZIP) $(publish_dest))
else
publish_kf4abl_symbols:
@echo "Publish kf4abl symbols: skipped"
publish_kf4sbl_symbols:
@echo "Publish kf4sbl symbols: skipped"
endif

# Publish Firmware symbols
Expand Down Expand Up @@ -286,6 +286,6 @@ publish: aic
$(hide) mkdir -p $(TOP)/pub/$(TARGET_PRODUCT)/$(TARGET_BUILD_VARIANT)
$(hide) cp $(PRODUCT_OUT)/$(TARGET_AIC_FILE_NAME) $(TOP)/pub/$(TARGET_PRODUCT)/$(TARGET_BUILD_VARIANT)
else # ANDROID_AS_GUEST
publish: publish_mkdir_dest $(PUBLISH_GOALS) publish_ifwi publish_gptimage_var publish_firmware_symbols $(PUB_OSAGNOSTIC_TAG) publish_kf4abl_symbols $(PUB_CMCC_ZIP) publish_androidia_image publish_grubinstaller
publish: publish_mkdir_dest $(PUBLISH_GOALS) publish_ifwi publish_gptimage_var publish_firmware_symbols $(PUB_OSAGNOSTIC_TAG) publish_kf4sbl_symbols $(PUB_CMCC_ZIP) publish_androidia_image publish_grubinstaller
@$(ACP) out/dist/* $(publish_dest)
endif # ANDROID_AS_GUEST

0 comments on commit bf28706

Please sign in to comment.