├── FlexLexer.h ├── Keccak-compact-settings.h ├── Keccak-compact-versal.cpp ├── Keccak-compact-versal.h ├── Keccak-compact.cpp ├── Keccak-compact.h ├── LICENSE ├── Makefile ├── README.md ├── attributes.h ├── authentication-versal.cpp ├── authentication-versal.h ├── authentication-zynq.cpp ├── authentication-zynq.h ├── authentication-zynqmp.cpp ├── authentication-zynqmp.h ├── authentication.cpp ├── authentication.h ├── authkeys-versal.cpp ├── authkeys-versal.h ├── authkeys.cpp ├── authkeys.h ├── baseclass.h ├── bif.l ├── bif.tab.cpp ├── bif.tab.hpp ├── bif.y ├── bif.yy.cpp ├── bifoptions.cpp ├── bifoptions.h ├── bifscanner.h ├── binary-versal.cpp ├── binary-versal.h ├── binary.cpp ├── binary.h ├── binfile.cpp ├── bitutils-versal.cpp ├── bitutils-versal.h ├── bitutils.cpp ├── bitutils.h ├── bootgenenum.h ├── bootgenexception.h ├── bootheader-versal.cpp ├── bootheader-versal.h ├── bootheader-zynq.cpp ├── bootheader-zynq.h ├── bootheader-zynqmp.cpp ├── bootheader-zynqmp.h ├── bootheader.cpp ├── bootheader.h ├── bootimage-versal.cpp ├── bootimage-versal.h ├── bootimage-zynq.cpp ├── bootimage-zynq.h ├── bootimage-zynqmp.cpp ├── bootimage-zynqmp.h ├── bootimage.cpp ├── bootimage.h ├── cJSON.c ├── cJSON.h ├── cdo-alloc.c ├── cdo-alloc.h ├── cdo-binary.c ├── cdo-binary.h ├── cdo-command.c ├── cdo-command.h ├── cdo-driver ├── README.md ├── cdo_driver.c └── cdo_driver.h ├── cdo-load.c ├── cdo-load.h ├── cdo-metadata.c ├── cdo-metadata.h ├── cdo-npi.c ├── cdo-npi.h ├── cdo-overlay.c ├── cdo-overlay.h ├── cdo-raw.c ├── cdo-raw.h ├── cdo-source.c ├── cdo-source.h ├── checksum-versal.cpp ├── checksum-versal.h ├── checksum.cpp ├── checksum.h ├── cmdoptions.l ├── cmdoptions.tab.cpp ├── cmdoptions.tab.hpp ├── cmdoptions.y ├── cmdoptions.yy.cpp ├── cmdoptionsscanner.h ├── elf.h ├── elftools.cpp ├── elftools.h ├── elftypes.h ├── encryption-versal.cpp ├── encryption-versal.h ├── encryption-versalnet.cpp ├── encryption-zynq.cpp ├── encryption-zynq.h ├── encryption-zynqmp.cpp ├── encryption-zynqmp.h ├── encryption.cpp ├── encryption.h ├── encryptutils.cpp ├── encryptutils.h ├── fileutils.h ├── hash.cpp ├── hash.h ├── help-versal.h ├── help.h ├── imageheadertable-versal.cpp ├── imageheadertable-versal.h ├── imageheadertable-zynq.cpp ├── imageheadertable-zynq.h ├── imageheadertable-zynqmp.cpp ├── imageheadertable-zynqmp.h ├── imageheadertable.cpp ├── imageheadertable.h ├── kdf.cpp ├── kdf.h ├── link.h ├── location.hh ├── logger.cpp ├── logger.h ├── main.cpp ├── mcsfile.cpp ├── obfskutil.h ├── options.cpp ├── options.h ├── outputfile-versal.cpp ├── outputfile.cpp ├── outputfile.h ├── parsing.cpp ├── parsing.h ├── partition-versal.cpp ├── partition.cpp ├── partitionheadertable-versal.cpp ├── partitionheadertable-versal.h ├── partitionheadertable-zynq.cpp ├── partitionheadertable-zynq.h ├── partitionheadertable-zynqmp.cpp ├── partitionheadertable-zynqmp.h ├── partitionheadertable.cpp ├── partitionheadertable.h ├── position.hh ├── postprocess.cpp ├── readimage-versal.cpp ├── readimage-versal.h ├── readimage-zynq.cpp ├── readimage-zynq.h ├── readimage-zynqmp.cpp ├── readimage-zynqmp.h ├── readimage.cpp ├── readimage.h ├── regdefs.h ├── reginit.cpp ├── reginit.l ├── reginit.tab.cpp ├── reginit.tab.hpp ├── reginit.y ├── reginit.yy.cpp ├── reginitscanner.h ├── stack.hh ├── stringutils.h ├── systemutils.h ├── verifyimage-versal.cpp ├── verifyimage-zynqmp.cpp ├── version.h ├── win_include └── stdint.h └── xil-bignum.h /Keccak-compact-settings.h: -------------------------------------------------------------------------------- 1 | #define cKeccakB 1600 2 | #define cKeccakR 832 3 | #define cKeccakFixedOutputLengthInBytes 48 4 | -------------------------------------------------------------------------------- /Keccak-compact-versal.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xilinx/bootgen/4f023ac2ca2ea452740194045fcecf1671f5b2c0/Keccak-compact-versal.cpp -------------------------------------------------------------------------------- /Keccak-compact-versal.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xilinx/bootgen/4f023ac2ca2ea452740194045fcecf1671f5b2c0/Keccak-compact-versal.h -------------------------------------------------------------------------------- /Keccak-compact.h: -------------------------------------------------------------------------------- 1 | /* 2 | The Keccak sponge function, designed by Guido Bertoni, Joan Daemen, 3 | Michaël Peeters and Gilles Van Assche. For more information, feedback or 4 | questions, please refer to our website: http://keccak.noekeon.org/ 5 | 6 | Implementation by Ronny Van Keer, 7 | hereby denoted as "the implementer". 8 | 9 | To the extent possible under law, the implementer has waived all copyright 10 | and related or neighboring rights to the source code in this file. 11 | http://creativecommons.org/publicdomain/zero/1.0/ 12 | */ 13 | 14 | #ifndef _Keccak_compact_h_ 15 | #define _Keccak_compact_h_ 16 | 17 | #include "Keccak-compact-settings.h" 18 | 19 | int crypto_hash_NIST_SHA3(unsigned char *out, const unsigned char *in, unsigned long long inlen); 20 | int crypto_hash( unsigned char *out, const unsigned char *in, unsigned long long inlen ); 21 | 22 | /* 23 | ** API with message queue (inspired from NIST's API) 24 | */ 25 | 26 | typedef unsigned char BitSequence; 27 | typedef unsigned long long DataLength; 28 | typedef enum { SUCCESS = 0, FAIL = 1, BAD_HASHLEN = 2 } HashReturn; 29 | 30 | #if defined(__GNUC__) 31 | #define ALIGN __attribute__ ((aligned(32))) 32 | #elif defined(_MSC_VER) 33 | #define ALIGN __declspec(align(32)) 34 | #else 35 | #define ALIGN 36 | #endif 37 | 38 | ALIGN typedef struct hashStateStruct 39 | { 40 | ALIGN unsigned char state[cKeccakB / 8]; 41 | int bitsInQueue; 42 | } hashState; 43 | 44 | 45 | HashReturn Init(hashState *state); 46 | 47 | HashReturn Update(hashState *state, const BitSequence *data, DataLength databitlen); 48 | 49 | HashReturn Final(hashState *state, BitSequence *hashval, unsigned int hashbytelen); 50 | 51 | #endif 52 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | ############################################################################## 2 | # 3 | # Copyright 2015-2022 Xilinx, Inc. 4 | # Copyright 2022-2023 Advanced Micro Devices, Inc. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # http://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | ############################################################################## 19 | # 20 | # Building Bootgen for x86 Host: 21 | # 'make "LIBS= -ldl 22 | # -lpthread" "INCLUDE_USER=-I"' 23 | # 24 | # Building Bootgen for ARM v8: 25 | # 'make "CROSS_COMPILE=aarch64-linux-gnu-" "LIBS= \ 26 | # -ldl -lpthread" 27 | # "INCLUDE_USER=-I"' 28 | # 29 | ############################################################################## 30 | 31 | ifneq ($(CROSS_COMPILE), ) 32 | CXX = $(CROSS_COMPILE)g++ 33 | CC = $(CROSS_COMPILE)gcc 34 | else ifneq ($(CROSS_COMPILER), ) 35 | CXX = $(CROSS_COMPILER) 36 | CC = $(subst g++,gcc,$(CROSS_COMPILER)) 37 | else 38 | CXX = g++ 39 | CC = gcc 40 | endif 41 | 42 | OBJ = o 43 | CXXFLAGS ?= -std=c++0x -O -Wall -Wno-reorder -Wno-deprecated-declarations 44 | CFLAGS ?= -O -Wall 45 | 46 | GCCVERSIONGTEQ9 := $(shell expr `${CXX} -dumpversion | cut -f1 -d.` \>= 9) 47 | ifeq "$(GCCVERSIONGTEQ9)" "1" 48 | CXXFLAGS += -Wno-aligned-new -Wno-misleading-indentation -Wno-class-memaccess 49 | endif 50 | 51 | EXEC = bootgen 52 | UNAME := $(shell uname) 53 | 54 | ifeq ($(UNAME), Linux) 55 | INCLUDE_SYS = -I. 56 | LIBS = -lssl -lcrypto 57 | RTLIBS = 58 | OPTIONS_USER = 59 | endif 60 | 61 | INCLUDE = $(INCLUDE_USER) $(INCLUDE_SYS) 62 | 63 | OPTIONS = $(OPTIONS_USER) 64 | 65 | all: $(EXEC) $(RTLIBS) 66 | 67 | OBJECTS = $(addsuffix .o, $(basename $(wildcard *.cpp))) 68 | OBJECTS += $(addsuffix .o, $(basename $(wildcard *.c))) 69 | 70 | %.${OBJ} : %.cpp 71 | ${CXX} -c ${CXXFLAGS} $(OPTIONS) ${INCLUDE} $< 72 | 73 | %.${OBJ} : %.c 74 | ${CC} -c ${CFLAGS} $(OPTIONS) ${INCLUDE} $< 75 | 76 | ${EXEC}: $(OBJECTS) 77 | echo Building executable file: $@... 78 | ${CXX} $(CXXFLAGS) $(LDFLAGS) $(OPTIONS_USER) -o $@ $(OBJECTS) $(LIBS) 79 | 80 | execs: ${EXEC} 81 | 82 | clean: 83 | echo 84 | rm -rf ${EXEC} 85 | rm -f $(OBJECTS) 86 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xilinx/bootgen/4f023ac2ca2ea452740194045fcecf1671f5b2c0/README.md -------------------------------------------------------------------------------- /attributes.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #ifndef _ATTRIBUTES_LIST_H_ 19 | #define _ATTRIBUTES_LIST_H_ 20 | 21 | #include 22 | #include 23 | #include 24 | 25 | std::pair > type 26 | ( 27 | "type", 28 | { 29 | "bootloader", 30 | "cdo", 31 | "pmcdata", 32 | "bootimage", 33 | "raw", 34 | "cfi", 35 | "cfi-gsc", 36 | "slr-boot", 37 | "slr-config" 38 | } 39 | ); 40 | 41 | std::pair > core 42 | ( 43 | "core", 44 | { 45 | "psm", 46 | "a72-0", 47 | "a72-1", 48 | "r5-0", 49 | "r5-1", 50 | "r5-lockstep", 51 | "aie" 52 | } 53 | ); 54 | 55 | std::pair > encryption 56 | ( 57 | "encryption", 58 | { 59 | "none", 60 | "aes" 61 | } 62 | ); 63 | 64 | std::pair > authentication 65 | ( 66 | "authentication", 67 | { 68 | "none", 69 | "rsa", 70 | "ecdsa-p384" // This is similar to "ecdsa" option we currently support. As there is a request for another ecdsa algo, we had to rename this. 71 | "ecdsa-p521" 72 | } 73 | ); 74 | 75 | std::pair > keysrc 76 | ( 77 | "keysrc", 78 | { 79 | /* Options for bootloader/pmcdata/metaheader and other partitions */ 80 | "efuse_red_key", 81 | "efuse_blk_key", 82 | "bbram_red_key", 83 | "bbram_blk_key", 84 | "bh_blk_key", 85 | /* Options for other partitions - these are invalid for bootloader/pmcdata/metaheader*/ 86 | "user_key0", 87 | "user_key1", 88 | "user_key2", 89 | "user_key3", 90 | "user_key4", 91 | "user_key5", 92 | "user_key6", 93 | "user_key7", 94 | "efuse_user_key0", 95 | "efuse_user_blk_key0", 96 | "efuse_user_key1", 97 | "efuse_user_blk_key1", 98 | "kup_key" 99 | } 100 | ); 101 | 102 | std::pair > checksum 103 | ( 104 | "checksum", 105 | { 106 | "none", 107 | "sha3" 108 | } 109 | ); 110 | 111 | 112 | /* List of all attributes */ 113 | std::vector>> bootgen_options 114 | { 115 | type, 116 | core, 117 | encryption, 118 | authentication, 119 | keysrc, 120 | checksum 121 | }; 122 | 123 | #endif -------------------------------------------------------------------------------- /authentication-zynq.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #pragma once 19 | #ifndef _AUTHENTICATION_ZYNQ_H_ 20 | #define _AUTHENTICATION_ZYNQ_H_ 21 | 22 | /* 23 | ------------------------------------------------------------------------------- 24 | *********************************************** H E A D E R F I L E S *** 25 | ------------------------------------------------------------------------------- 26 | */ 27 | #include "authentication.h" 28 | /* 29 | ------------------------------------------------------------------------------- 30 | ********************************************* P R E P R O C E S S O R S *** 31 | ------------------------------------------------------------------------------- 32 | */ 33 | #define AUTH_HDR_ZYNQ 0x00000101 34 | 35 | typedef struct 36 | { 37 | uint32_t acHeader; // 0x000 38 | uint32_t acSize; // 0x004 39 | uint8_t acUdf[UDF_DATA_SIZE]; // 0x008 40 | ACKey2048 acPpk; // 0x040 41 | ACKey2048 acSpk; // 0x280 42 | ACSignature2048 acSpkSignature; // 0x4C0 43 | ACSignature2048 acPartitionSignature; // 0x5C0 44 | } AuthCertificate2048Structure; 45 | 46 | 47 | /* 48 | ------------------------------------------------------------------------------- 49 | ********************************************************* C L A S S E S *** 50 | ------------------------------------------------------------------------------- 51 | */ 52 | 53 | /******************************************************************************/ 54 | class ZynqAuthenticationContext : public AuthenticationContext 55 | { 56 | public: 57 | ZynqAuthenticationContext(); 58 | ZynqAuthenticationContext(const AuthenticationContext* refAuthContext); 59 | ZynqAuthenticationContext(const AuthCertificate2048Structure* existingCert); 60 | ~ZynqAuthenticationContext(); 61 | 62 | uint32_t GetCertificateSize(void) { return certSize; } 63 | Section* CreateCertificate(BootImage& bi, Binary& cache, Section* dataSection); 64 | void Link(BootImage& bi, std::list sections, AuthenticationCertificate* cert); 65 | void GeneratePPKHash(const std::string& filename); 66 | void SetKeyLength(Authentication::Type type); 67 | void GenerateSPKHash(uint8_t* sha256_hash_padded); 68 | void CreatePadding(uint8_t* signature, const uint8_t* hash); 69 | void CopyPartitionSignature(BootImage& bi, std::list sections, uint8_t* signatureBlock, Section* acSection); 70 | void RearrangeEndianess(uint8_t *array, uint32_t size) {}; 71 | 72 | private: 73 | uint32_t certSize; 74 | void CopySPKSignature(ACSignature2048* ptr); 75 | }; 76 | #endif 77 | -------------------------------------------------------------------------------- /authentication-zynqmp.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #pragma once 19 | #ifndef _AUTHENTICATION_ZYNQMP_H_ 20 | #define _AUTHENTICATION_ZYNQMP_H_ 21 | 22 | 23 | /* 24 | ------------------------------------------------------------------------------- 25 | *********************************************** H E A D E R F I L E S *** 26 | ------------------------------------------------------------------------------- 27 | */ 28 | #include "authentication.h" 29 | 30 | /* 31 | ------------------------------------------------------------------------------- 32 | ********************************************* P R E P R O C E S S O R S *** 33 | ------------------------------------------------------------------------------- 34 | */ 35 | #define AUTH_HDR_ZYNQMP 0x115 36 | #define AC_HDR_SHA_2_3_BIT_SHIFT 2 37 | #define AC_HDR_PPK_SELECT_BIT_SHIFT 16 38 | #define AC_HDR_SPK_SELECT_BIT_SHIFT 18 39 | 40 | /* 41 | ------------------------------------------------------------------------------- 42 | *************************************************** S T R U C T U R E S *** 43 | ------------------------------------------------------------------------------- 44 | */ 45 | 46 | 47 | /* RSA 4096 Authentication Certificate */ 48 | typedef struct 49 | { 50 | uint32_t acHeader; // 0x000 51 | uint32_t spkId; // 0x004 52 | uint8_t acUdf[UDF_DATA_SIZE]; // 0x008 53 | ACKey4096 acPpk; // 0x040 54 | ACKey4096 acSpk; // 0x480 55 | ACSignature4096 acSpkSignature; // 0x8C0 56 | ACSignature4096 acBhSignature; // 0xAC0 57 | ACSignature4096 acPartitionSignature; // 0xCC0 58 | } AuthCertificate4096Structure; 59 | 60 | 61 | class ZynqMpAuthenticationContext : public AuthenticationContext 62 | { 63 | public: 64 | ZynqMpAuthenticationContext(); 65 | ZynqMpAuthenticationContext(const AuthenticationContext* refAuthContext); 66 | ZynqMpAuthenticationContext(const AuthCertificate4096Structure* existingCert); 67 | ~ZynqMpAuthenticationContext(); 68 | 69 | uint32_t GetCertificateSize(void) { return certSize; } 70 | Section* CreateCertificate(BootImage& bi, Binary& cache, Section* dataSection); 71 | void Link(BootImage& bi, std::list sections, AuthenticationCertificate* cert); 72 | void GeneratePPKHash(const std::string& filename); 73 | void GenerateSPKHash(uint8_t* sha256_hash_padded); 74 | void GenerateBHHash(BootImage& bi, uint8_t* sha_hash_padded); 75 | void CreatePadding(uint8_t* signature, const uint8_t* hash); 76 | void CopyPartitionSignature(BootImage& bi, std::list sections, uint8_t* signatureBlock, Section* acSection); 77 | void RearrangeEndianess(uint8_t *array, uint32_t size); 78 | void AddAuthCertSizeToTotalFSBLSize(PartitionHeader* header); 79 | 80 | void SetKeyLength(Authentication::Type type); 81 | 82 | private: 83 | uint32_t certSize; 84 | void CopySPKSignature(ACSignature4096* ptr); 85 | std::string GetCertificateName(std::string name); 86 | void CopyBHSignature(BootImage& bi, ACSignature4096* ptr); 87 | }; 88 | #endif 89 | -------------------------------------------------------------------------------- /authkeys-versal.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #ifndef _AUTH_KEYS_VERSAL_H_ 19 | #define _AUTH_KEYS_VERSAL_H_ 20 | 21 | /* 22 | ------------------------------------------------------------------------------- 23 | ************************************************************ HEADER FILES ***** 24 | ------------------------------------------------------------------------------- 25 | */ 26 | #include 27 | #include 28 | #include "logger.h" 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include "authkeys.h" 35 | 36 | 37 | 38 | /* 39 | ------------------------------------------------------------------------------- 40 | *********************************************************** PREPROCESSORS ***** 41 | ------------------------------------------------------------------------------- 42 | */ 43 | 44 | //AC key related defines 45 | #define RSA_4096_N 0x0 46 | #define RSA_4096_N_EXT 0x200 47 | #define RSA_4096_E 0x400 48 | 49 | #define VERSAL_ACKEY_STRUCT_SIZE 1028 50 | 51 | #define RSA_4096_N_SIZE 512 52 | #define RSA_4096_N_EXT_SIZE 512 53 | #define RSA_4096_E_SIZE 4 54 | /* 55 | ------------------------------------------------------------------------------- 56 | ************************************************************** STRUCTURES ***** 57 | ------------------------------------------------------------------------------- 58 | */ 59 | 60 | typedef struct 61 | { 62 | uint8_t N[RSA_4096_KEY_LENGTH]; // Modulus 63 | uint8_t N_extension[RSA_4096_KEY_LENGTH]; // Montgomery Modulus extension 64 | uint8_t E[4]; // Exponent 65 | } ACKey4096Sha3Padding; 66 | 67 | typedef struct 68 | { 69 | uint8_t x[EC_P384_KEY_LENGTH]; // x co-ordinate 70 | uint8_t y[EC_P384_KEY_LENGTH]; // y co-ordinate 71 | uint8_t pad[932]; // Padding 72 | } ACKeyECDSA; 73 | 74 | typedef struct 75 | { 76 | uint8_t x[EC_P521_KEY_LENGTH2]; // x co-ordinate 77 | uint8_t y[EC_P521_KEY_LENGTH2]; // y co-ordinate 78 | uint8_t pad[896]; // Padding 79 | } ACKeyECDSAP521; 80 | 81 | typedef struct 82 | { 83 | uint8_t Signature[SIGN_LENGTH_VERSAL]; 84 | } ACSignatureECDSA; 85 | 86 | /* 87 | ------------------------------------------------------------------------------- 88 | ***************************************************************** CLASSES ***** 89 | ------------------------------------------------------------------------------- 90 | */ 91 | class VersalKey : public Key 92 | { 93 | public: 94 | VersalKey(const std::string& name0); 95 | VersalKey(const Key& otherKey); 96 | ~VersalKey(); 97 | 98 | void Parse(const std::string& filename, bool isSecret0); 99 | uint8_t ParseECDSAOpenSSLKey(const std::string& filename); 100 | EC_KEY *eckey; 101 | uint8_t *x; // x co-ordinate (384 bits) 102 | uint8_t *y; // y co-ordinate (384 bits) 103 | }; 104 | 105 | /******************************************************************************/ 106 | class Key4096Sha3Padding : public VersalKey 107 | { 108 | public: 109 | Key4096Sha3Padding(const std::string& name0) : VersalKey(name0) { } 110 | Key4096Sha3Padding(const Key4096Sha3Padding& otherKey) : VersalKey(otherKey) { } 111 | 112 | void Export(void* dst); 113 | void Import(const void* acKey, const std::string& name0); 114 | }; 115 | 116 | 117 | /******************************************************************************/ 118 | class KeyECDSA : public VersalKey 119 | { 120 | public: 121 | KeyECDSA(const std::string& name0) : VersalKey(name0) { } 122 | KeyECDSA(const KeyECDSA& otherKey) : VersalKey(otherKey) { } 123 | 124 | void Export(void* dst); 125 | void Import(const void* acKey, const std::string& name0); 126 | }; 127 | 128 | /******************************************************************************/ 129 | class KeyECDSAp521 : public VersalKey 130 | { 131 | public: 132 | KeyECDSAp521(const std::string& name0) : VersalKey(name0) { } 133 | KeyECDSAp521(const KeyECDSAp521& otherKey) : VersalKey(otherKey) { } 134 | 135 | void Export(void* dst); 136 | void Import(const void* acKey, const std::string& name0); 137 | }; 138 | #endif 139 | -------------------------------------------------------------------------------- /baseclass.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #pragma once 19 | 20 | #ifndef _BASECLASS_H_ 21 | #define _BASECLASS_H_ 22 | 23 | /* 24 | ------------------------------------------------------------------------------- 25 | ********************************************************* C L A S S E S *** 26 | ------------------------------------------------------------------------------- 27 | */ 28 | /******************************************************************************/ 29 | class BaseThing 30 | { 31 | public: 32 | BaseThing() : section(NULL) { } 33 | 34 | uint32_t ComputeWordChecksum( void* firstWordPtr, size_t length) 35 | { 36 | uint32_t checksum = 0; 37 | size_t numChecksumedWords = length / sizeof(uint32_t); 38 | for(size_t i=0;i< numChecksumedWords; i++) 39 | { 40 | checksum += ((uint32_t*)firstWordPtr)[i]; 41 | } 42 | /* Invert the Checksum value */ 43 | checksum ^= 0xFFFFFFFF; 44 | return checksum; 45 | } 46 | 47 | Section* section; 48 | }; 49 | 50 | 51 | /******************************************************************************/ 52 | template 53 | class Override 54 | { 55 | public: 56 | Override() : overriden(false), value(0) { } 57 | 58 | Override& operator=(T value0) 59 | { 60 | overriden = true; 61 | value = value0; 62 | return *this; 63 | } 64 | 65 | Override& operator=(Override& el) 66 | { 67 | overriden = el.overriden; 68 | value = el.value; 69 | return *this; 70 | } 71 | 72 | void Set(T value0) 73 | { 74 | overriden = true; 75 | value = value0; 76 | } 77 | 78 | T Value() const { return value; } 79 | 80 | T ValueOrDefault(T defValue) const { return overriden?value:defValue;} 81 | 82 | bool IsSet() const { return overriden;} 83 | 84 | private: 85 | bool overriden; 86 | T value; 87 | }; 88 | #endif 89 | -------------------------------------------------------------------------------- /bifscanner.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #pragma once 19 | 20 | #ifndef _BIFSCANNER_H_ 21 | #define _BIFSCANNER_H_ 22 | 23 | /* 24 | ------------------------------------------------------------------------------- 25 | *********************************************** H E A D E R F I L E S *** 26 | ------------------------------------------------------------------------------- 27 | */ 28 | // Only include FlexLexer.h if it hasn't been already included 29 | #if ! defined(yyFlexLexerOnce) 30 | #undef yyFlexLexer 31 | #define yyFlexLexer bifFlexLexer 32 | #include "FlexLexer.h" 33 | #endif 34 | 35 | // Override the interface for yylex since we namespaced it 36 | #undef YY_DECL 37 | #define YY_DECL int BIF::FlexScanner::yylex() 38 | 39 | // Include Bison for types / tokens 40 | #include "bif.tab.hpp" 41 | 42 | 43 | /* 44 | ------------------------------------------------------------------------------- 45 | ********************************************************* C L A S S E S *** 46 | ------------------------------------------------------------------------------- 47 | */ 48 | /******************************************************************************/ 49 | namespace BIF 50 | { 51 | class FlexScanner : public yyFlexLexer 52 | { 53 | public: 54 | // save the pointer to yylval so we can change it, and invoke scanner 55 | int yylex(BIF::BisonParser::semantic_type * lval, BIF::BisonParser::location_type* loc) { 56 | yylval = lval; 57 | yylloc = loc; 58 | return yylex(); 59 | } 60 | void comment(); 61 | std::string filename; 62 | 63 | private: 64 | // Scanning function created by Flex; make this private to force usage 65 | // of the overloaded method so we can get a pointer to Bison's yylval 66 | int yylex(); 67 | 68 | // point to yylval (provided by Bison in overloaded yylex) 69 | BIF::BisonParser::semantic_type * yylval; 70 | BIF::BisonParser::location_type * yylloc; 71 | }; 72 | } 73 | #endif 74 | -------------------------------------------------------------------------------- /binary-versal.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #pragma once 19 | 20 | #ifndef _VERSAL_BINARY_H_ 21 | #define _VERSAL_BINARY_H_ 22 | 23 | /* 24 | ------------------------------------------------------------------------------- 25 | *********************************************** H E A D E R F I L E S *** 26 | ------------------------------------------------------------------------------- 27 | */ 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include "binary.h" 34 | 35 | class Section; 36 | class Options; 37 | class Binary; 38 | 39 | /* 40 | ------------------------------------------------------------------------------- 41 | ********************************************* P R E P R O C E S S O R S *** 42 | ------------------------------------------------------------------------------- 43 | */ 44 | #define WORD_SIZE_IN_BYTES 4 45 | #define VERSAL_AC_SIZE 0xE60 46 | 47 | typedef std::list SectionList; 48 | 49 | /* 50 | ------------------------------------------------------------------------------- 51 | ********************************************************* C L A S S E S *** 52 | ------------------------------------------------------------------------------- 53 | */ 54 | /******************************************************************************/ 55 | class VersalBinary : public Binary 56 | { 57 | public: 58 | VersalBinary() {}; 59 | ~VersalBinary(); 60 | 61 | void StackAndAlign(Options& options); 62 | }; 63 | #endif 64 | -------------------------------------------------------------------------------- /binary.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #pragma once 19 | 20 | #ifndef _BINARY_H_ 21 | #define _BINARY_H_ 22 | 23 | /* 24 | ------------------------------------------------------------------------------- 25 | *********************************************** H E A D E R F I L E S *** 26 | ------------------------------------------------------------------------------- 27 | */ 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | 34 | class Section; 35 | class Options; 36 | 37 | /* 38 | ------------------------------------------------------------------------------- 39 | ********************************************* P R E P R O C E S S O R S *** 40 | ------------------------------------------------------------------------------- 41 | */ 42 | #define WORD_SIZE_IN_BYTES 4 43 | 44 | typedef std::list SectionList; 45 | 46 | 47 | /* 48 | ------------------------------------------------------------------------------- 49 | ********************************************************* C L A S S E S *** 50 | ------------------------------------------------------------------------------- 51 | */ 52 | /******************************************************************************/ 53 | class Binary 54 | { 55 | public: 56 | Binary() : TotalSize(0) { } 57 | virtual ~Binary(); 58 | 59 | typedef uint64_t Address_t; 60 | typedef uint64_t Length_t; 61 | 62 | virtual void StackAndAlign(Options& options); 63 | //void Dump(std::ostream& s); 64 | 65 | static bool CheckAddress(uint64_t addr) 66 | { 67 | return addr < 0x100000000ULL; 68 | } 69 | SectionList Sections; 70 | Length_t TotalSize; 71 | }; 72 | 73 | /******************************************************************************/ 74 | class Section 75 | { 76 | public: 77 | Section(const std::string& name, Binary::Length_t length); 78 | ~Section(); 79 | 80 | std::string Name; 81 | Binary::Address_t Address; 82 | Binary::Length_t Length; 83 | uint32_t Alignment; 84 | Binary::Length_t Reserve; 85 | uint8_t* Data; 86 | uint64_t firstChunkSize; 87 | bool continuation; 88 | bool isPartitionData; 89 | bool isCertificate; 90 | bool isFirstElfSection; 91 | bool isBitStream; 92 | bool isBootloader; 93 | bool isPmcdata; 94 | uint32_t partitionNum; 95 | int index; 96 | 97 | Binary::Address_t WordAddress() 98 | { 99 | return Address / sizeof(uint32_t); 100 | } 101 | void PadToWordAlignment(uint32_t headPad, uint32_t tailPad); 102 | void IncreaseLengthAndPadTo(Binary::Length_t length, uint8_t padding); 103 | void Summarize(std::ostream& out); 104 | void IncreaseLengthAndPadSHA3(Binary::Length_t newLength); 105 | }; 106 | 107 | #endif 108 | -------------------------------------------------------------------------------- /bitutils-versal.cpp: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | /* 19 | ------------------------------------------------------------------------------- 20 | *********************************************** H E A D E R F I L E S *** 21 | ------------------------------------------------------------------------------- 22 | */ 23 | #include 24 | #include 25 | #include 26 | #include "bootgenexception.h" 27 | #include "bitutils-versal.h" 28 | #include 29 | #include 30 | #include 31 | #include "bootimage.h" 32 | 33 | 34 | /* 35 | ------------------------------------------------------------------------------- 36 | ***************************************************** F U N C T I O N S *** 37 | ------------------------------------------------------------------------------- 38 | */ 39 | /******************************************************************************/ 40 | VersalBitFile::VersalBitFile(std::istream& stream0) 41 | : BitFile(stream0), 42 | temp(NULL), 43 | encryptType(Encryption::None) 44 | { 45 | } 46 | 47 | /******************************************************************************/ 48 | VersalBitFile::~VersalBitFile() 49 | { 50 | if (temp) 51 | { 52 | delete temp; 53 | } 54 | } 55 | 56 | /******************************************************************************/ 57 | void VersalBitFile::Copy(OutputStream* os) 58 | { 59 | os->swap_cfi_bytes = true; 60 | os->Alloc(header.dataByteLength + 32); 61 | if (header.dataByteLength % 4) 62 | { 63 | LOG_ERROR("Bitstream length %d - is not word aligned", header.dataByteLength); 64 | } 65 | uint32_t bytes = header.dataByteLength / sizeof(uint8_t); 66 | uint8_t cfiBytes[16] = { 0 }; 67 | uint32_t numCfiLines = bytes / 16; 68 | for (uint32_t i = 0; i < numCfiLines; i++) 69 | { 70 | is.ReadCfiLine(cfiBytes); 71 | os->WriteCfiLine(cfiBytes); 72 | } 73 | } 74 | 75 | /******************************************************************************/ 76 | void VersalBitFile::CopyNpi(OutputStream* os) 77 | { 78 | os->Alloc(header.dataByteLength + 32); 79 | if (header.dataByteLength % 4) 80 | { 81 | LOG_ERROR("NPI length %d - is not word aligned", header.dataByteLength); 82 | } 83 | uint32_t bytes = header.dataByteLength / sizeof(uint8_t); 84 | uint8_t npiBytes[4] = { 0 }; 85 | uint32_t numnpiLines = bytes / 4; 86 | bool change_endianness = false; 87 | 88 | /* Read ID value (XNLX/CDO) from CDO Header to identify endianness */ 89 | /* 8 characters (npi line) + one new line character = 9 characters */ 90 | //is.Skip(9); 91 | is.ReadNpiLine(npiBytes); 92 | is.ReadNpiLine(npiBytes); 93 | uint32_t npi_word = ReadBigEndian32(npiBytes); 94 | if ((npi_word != 0x584c4e58) && (npi_word != 0x584e4c58) && (npi_word != 0x004f4443) && (npi_word != 0x43444f00)) 95 | { 96 | LOG_ERROR("Invalid CDO format - incorrect identification word (XLNX/CDO) - 0x%x", npi_word); 97 | } 98 | if ((npi_word == 0x584c4e58) || (npi_word == 0x004f4443)) 99 | { 100 | /* Convert BE CDOs to LE */ 101 | change_endianness = true; 102 | LOG_WARNING("CDO is in Big Endian format. Big Endian CDOs are deprecated. Please use little endian CDOs."); 103 | } 104 | /* HACK - Need correction */ 105 | /* Few CDO files have 0x prepended to each word 106 | So setting the stream to start and reading the banner again to reach the actual CDO word */ 107 | is.Start(); 108 | ReadRbtHeader(); 109 | 110 | for (uint32_t i = 0; i < numnpiLines; i++) 111 | { 112 | is.ReadNpiLine(npiBytes); 113 | if (change_endianness) 114 | { 115 | npi_word = ReadBigEndian32(npiBytes); 116 | } 117 | else 118 | { 119 | npi_word = ReadLittleEndian32(npiBytes); 120 | } 121 | os->WriteLong(npi_word); 122 | } 123 | } 124 | 125 | /******************************************************************************/ 126 | OutputStream* VersalBitFile::GetOutputStreamType(void) 127 | { 128 | return new OutputStream_LE(); 129 | } 130 | 131 | /******************************************************************************/ 132 | bool VersalBitFile::GetBitStripFlag(void) 133 | { 134 | return false; 135 | } 136 | 137 | /******************************************************************************/ 138 | bool VersalBitFile::GetBitPadFlag(bool) 139 | { 140 | return false; 141 | } 142 | 143 | /******************************************************************************/ 144 | bool VersalBitFile::GetPreserveHeaderFlag(void) 145 | { 146 | return false; 147 | } 148 | 149 | /******************************************************************************/ 150 | void VersalBitFile::ComparePartsDataBase(const std::string&) 151 | { 152 | /* TO-DO */ 153 | return; 154 | } 155 | 156 | -------------------------------------------------------------------------------- /bitutils-versal.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #pragma once 19 | 20 | #ifndef _BITUTILS_VERSAL_H_ 21 | #define _BITUTILS_VERSAL_H_ 22 | /* 23 | ------------------------------------------------------------------------------- 24 | *********************************************** H E A D E R F I L E S *** 25 | ------------------------------------------------------------------------------- 26 | */ 27 | #include "bitutils.h" 28 | 29 | /* Forward Class Reference */ 30 | class OutputStream_LE; 31 | 32 | 33 | /* 34 | ------------------------------------------------------------------------------- 35 | ********************************************************* C L A S S E S *** 36 | ------------------------------------------------------------------------------- 37 | */ 38 | /****************************************************************************** 39 | VersalBitFile 40 | ******************************************************************************/ 41 | class VersalBitFile : public BitFile 42 | { 43 | public: 44 | VersalBitFile(std::istream& stream); 45 | ~VersalBitFile(); 46 | 47 | OutputStream* GetOutputStreamType(void); 48 | bool GetBitStripFlag(void); 49 | bool GetBitPadFlag(bool); 50 | bool GetPreserveHeaderFlag(void); 51 | void DummyRead(void) { }; 52 | void ComparePartsDataBase(const std::string&); 53 | void Copy(OutputStream* os); 54 | void CopyNpi(OutputStream* os); 55 | 56 | protected: 57 | Encryption::Type encryptType; 58 | OutputStream_BE* temp; 59 | }; 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /bootgenexception.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #ifndef _BOOTGENEXCEPTION_H_ 19 | #define _BOOTGENEXCEPTION_H_ 20 | 21 | 22 | /* 23 | ------------------------------------------------------------------------------- 24 | *********************************************** H E A D E R F I L E S *** 25 | ------------------------------------------------------------------------------- 26 | */ 27 | #include 28 | #include 29 | #include "stringutils.h" 30 | 31 | 32 | /* 33 | ------------------------------------------------------------------------------- 34 | ********************************************* P R E P R O C E S S O R S *** 35 | ------------------------------------------------------------------------------- 36 | */ 37 | #define BootGenException(a) BootGenExceptionClass(__FILE__,__LINE__,a) 38 | #define BootGenException1(a,b) BootGenExceptionClass(__FILE__,__LINE__,a,b) 39 | #define BootGenException2(a,b,c) BootGenExceptionClass(__FILE__,__LINE__,a,b,c) 40 | 41 | 42 | /* 43 | ------------------------------------------------------------------------------- 44 | ********************************************************* C L A S S E S *** 45 | ------------------------------------------------------------------------------- 46 | */ 47 | /******************************************************************************/ 48 | class BootGenExceptionClass : public std::exception 49 | { 50 | public: 51 | BootGenExceptionClass(const char* filename0, int line0, const std::string& msg0 ) 52 | : msg(msg0) 53 | , filename(filename0) 54 | , line(line0) 55 | { 56 | } 57 | 58 | BootGenExceptionClass(const char* filename0, int line0, const char* format, ...) 59 | : filename(filename0) 60 | , line(line0) 61 | { 62 | va_list ap; 63 | va_start(ap, format); 64 | msg = StringUtils::vFormat(format,ap); 65 | va_end(ap); 66 | } 67 | 68 | ~BootGenExceptionClass() throw() 69 | { 70 | } 71 | 72 | const char* what() const throw() 73 | { 74 | return msg.c_str(); 75 | } 76 | 77 | private: 78 | std::string msg; 79 | std::string filename; 80 | int line; 81 | }; 82 | #endif 83 | -------------------------------------------------------------------------------- /bootheader-zynq.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #pragma once 19 | 20 | #ifndef _BOOTHEADER_ZYNQ_H_ 21 | #define _BOOTHEADER_ZYNQ_H_ 22 | 23 | /* 24 | ------------------------------------------------------------------------------- 25 | *********************************************** H E A D E R F I L E S *** 26 | ------------------------------------------------------------------------------- 27 | */ 28 | #include "bootheader.h" 29 | #include "imageheadertable-zynq.h" 30 | /* 31 | ------------------------------------------------------------------------------- 32 | ********************************************* P R E P R O C E S S O R S *** 33 | ------------------------------------------------------------------------------- 34 | */ 35 | #define UDF_BH_SIZE_ZYNQ 19 36 | 37 | /* 38 | ------------------------------------------------------------------------------- 39 | *************************************************** S T R U C T U R E S *** 40 | ------------------------------------------------------------------------------- 41 | */ 42 | typedef struct 43 | { 44 | uint32_t bootVectors [MAX_BOOT_VECTORS]; // (0x00) 45 | uint32_t widthDetectionWord; // (0x20) 46 | uint32_t identificationWord; // (0x24) 47 | uint32_t encryptionKeySource; // (0x28) 48 | uint32_t headerVersion; // (0x2C) 49 | uint32_t sourceOffset; // (0x30) 50 | uint32_t fsblLength; // (0x34) 51 | uint32_t fsblLoadAddress; // (0x38) 52 | uint32_t fsblExecAddress; // (0x3C) 53 | uint32_t totalFsblLength; // (0x40) 54 | uint32_t qspiConfigWord; // (0x44) 55 | uint32_t headerChecksum; // (0x48) 56 | uint32_t udf [UDF_BH_SIZE_ZYNQ]; // (0x4C) 57 | uint32_t imageHeaderByteOffset; // (0x98) 58 | uint32_t partitionHeaderByteOffset; // (0x9C) 59 | } ZynqBootHeaderStructure; 60 | 61 | 62 | /* 63 | ------------------------------------------------------------------------------- 64 | ********************************************************* C L A S S E S *** 65 | ------------------------------------------------------------------------------- 66 | */ 67 | 68 | /******************************************************************************/ 69 | class ZynqBootHeader : public BootHeader 70 | { 71 | public: 72 | ZynqBootHeader(); 73 | ZynqBootHeader(std::ifstream& src); 74 | 75 | ~ZynqBootHeader(); 76 | 77 | void Build(BootImage& bi, Binary& cache); 78 | void Link(BootImage& bi); 79 | void SetBHForSinglePartitionImage(); 80 | void SetBHForXIP(BootImage & bi); 81 | void BuildRegInitTable (Options& options); 82 | uint32_t CalcHeaderChecksum(void* addr, size_t len); 83 | 84 | void SetBootVectors(BootImage &bi); 85 | void SetWidthDetectionWord(void); 86 | void SetIdentificationWord(void); 87 | void SetSourceOffset(uint32_t offset); 88 | void SetHeaderChecksum(void); 89 | void SetFsblLength(uint32_t size); 90 | void SetTotalFsblLength(uint32_t size); 91 | void SetFsblExecAddress(uint32_t address); 92 | void SetUdf(std::string udfFile); 93 | void SetImageHeaderByteOffset(uint32_t address); 94 | void SetPartitionHeaderByteOffset(uint32_t address); 95 | void SetEncryptionKeySource(KeySource::Type keyType); 96 | void SetHeaderVersion(void); 97 | void SetFsblLoadAddress(uint32_t address); 98 | void SetQspiConfigWord(void); 99 | 100 | uint32_t GetBootHeaderSize(void); 101 | uint32_t GetWidthDetectionWord(void); 102 | uint32_t GetIdentificationWord(void); 103 | uint32_t GetSourceOffset(void); 104 | uint32_t GetHeaderChecksum(void); 105 | uint32_t GetFsblLength(void); 106 | uint32_t GetTotalFsblLength(void); 107 | uint32_t GetFsblExecAddress(void); 108 | uint32_t GetImageHeaderByteOffset(void); 109 | uint32_t GetPartitionHeaderByteOffset(void); 110 | uint32_t GetHeaderVersion(void); 111 | uint32_t GetFsblLoadAddress(void); 112 | uint32_t GetQspiConfigWord(void); 113 | uint32_t GetXipBaseAddr(void); 114 | uint32_t* GetChecksumStartPtr(void); 115 | KeySource::Type GetEncryptionKeySource(void); 116 | 117 | private: 118 | ZynqBootHeaderStructure *bHTable; 119 | }; 120 | 121 | #endif 122 | 123 | -------------------------------------------------------------------------------- /bootheader.cpp: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | /* 19 | ------------------------------------------------------------------------------- 20 | *********************************************** H E A D E R F I L E S *** 21 | ------------------------------------------------------------------------------- 22 | */ 23 | #include "bootimage.h" 24 | #include 25 | #include "options.h" 26 | #include "fileutils.h" 27 | 28 | /* 29 | ------------------------------------------------------------------------------- 30 | ***************************************************** F U N C T I O N S *** 31 | ------------------------------------------------------------------------------- 32 | */ 33 | 34 | /******************************************************************************/ 35 | BootHeader::BootHeader() 36 | : prebuilt(false) 37 | , bhSize(0) 38 | , fullBhSize(0) 39 | , allHdrSize(0) 40 | , slaveBootSplitMode(false) 41 | { 42 | } 43 | 44 | /******************************************************************************/ 45 | void BootHeader::BootloaderAddressAndSizeCheck(PartitionHeader & hdr) 46 | { 47 | if (!Binary::CheckAddress(hdr.partition->section->Address)) 48 | { 49 | LOG_ERROR("Bootgen does not support addresses larger than 32 bits. Bootloader is placed at %llX", hdr.partition->section->Address); 50 | } 51 | if (!Binary::CheckAddress(hdr.GetPartitionSize())) 52 | { 53 | LOG_ERROR("Bootgen does not support addresses larger than 32 bits. Bootloader size is %llX", hdr.GetPartitionSize()); 54 | } 55 | if (!Binary::CheckAddress(hdr.GetLoadAddress())) 56 | { 57 | LOG_ERROR("Bootgen does not support addresses larger than 32 bits. Bootloader load address is %llX", hdr.GetLoadAddress()); 58 | } 59 | if (!Binary::CheckAddress(hdr.GetExecAddress())) 60 | { 61 | LOG_ERROR("Bootgen does not support addresses larger than 32 bits. Bootloader exec address is %llX", hdr.GetExecAddress()); 62 | } 63 | if (!Binary::CheckAddress(hdr.GetTransferSize())) 64 | { 65 | LOG_ERROR("Bootgen does not support addresses larger than 32 bits. Bootloader transfer size is %llX", hdr.GetTransferSize()); 66 | } 67 | } 68 | 69 | /******************************************************************************/ 70 | void BootHeader::Copy(BootHeader* otherHeader) 71 | { 72 | if (prebuilt) 73 | { 74 | LOG_ERROR("Cannot copy bootheader, since it has already been imported"); 75 | } 76 | 77 | /* The BH Section Length can vary due to PUF Helper Data */ 78 | if (section->Length < otherHeader->section->Length) 79 | { 80 | uint32_t newBhSize = otherHeader->section->Length; 81 | uint8_t* newDataPtr = new uint8_t[newBhSize]; 82 | 83 | delete[] section->Data; 84 | section->Data = newDataPtr; 85 | section->Length = newBhSize; 86 | } 87 | 88 | memcpy(section->Data, otherHeader->section->Data,section->Length); 89 | prebuilt = true; 90 | } 91 | 92 | /******************************************************************************/ 93 | void BootHeader::LoadUdfData(const std::string& udfFilename, uint8_t* udfData, uint32_t size) 94 | { 95 | FILE* fPtr; 96 | int datum, ret; 97 | uint32_t nbytes = 0; 98 | 99 | std::string filename(udfFilename); 100 | std::string baseFile = StringUtils::BaseName(filename); 101 | 102 | fPtr = fopen(filename.c_str(),"r"); 103 | 104 | if (fPtr) 105 | { 106 | while ((ret = fscanf(fPtr, "%2X", &datum)) != EOF) 107 | { 108 | if (ret == 1) 109 | { 110 | if (datum < 0 || datum > 255) 111 | { 112 | LOG_ERROR("Bad hex conversion in %s", baseFile.c_str()); 113 | } 114 | if (nbytes == size*4) 115 | { 116 | LOG_ERROR("More than %d bytes in the Boot Header UDF Data file - %s is not allowed.", size*4, baseFile.c_str()); 117 | } 118 | udfData[nbytes++] = datum; 119 | } 120 | else 121 | { 122 | LOG_ERROR("Error reading hex characters from file - %s ", baseFile.c_str()); 123 | } 124 | } 125 | } 126 | else 127 | { 128 | LOG_ERROR("Failure to open boot header udf file - %s ", baseFile.c_str()); 129 | } 130 | fclose(fPtr); 131 | } 132 | 133 | /******************************************************************************/ 134 | bool BootHeader::GetPrebuiltFlag(void) 135 | { 136 | return prebuilt; 137 | } 138 | -------------------------------------------------------------------------------- /bootimage-versal.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #pragma once 19 | 20 | #ifndef _BOOTIMAGE_VERSAL_H_ 21 | #define _BOOTIMAGE_VERSAL_H_ 22 | 23 | /* 24 | ------------------------------------------------------------------------------- 25 | *********************************************** H E A D E R F I L E S *** 26 | ------------------------------------------------------------------------------- 27 | */ 28 | #include "bootimage.h" 29 | #include "bootheader-versal.h" 30 | #include "encryption-versal.h" 31 | #include "binary-versal.h" 32 | 33 | #define ENCR_BLOCK_IN_BYTES 16 //= 128 bits 34 | 35 | /* 36 | ------------------------------------------------------------------------------- 37 | ********************************************************* C L A S S E S *** 38 | ------------------------------------------------------------------------------- 39 | */ 40 | /******************************************************************************/ 41 | class VersalBootImage : public BootImage 42 | { 43 | public: 44 | VersalBootImage(Options& options, uint8_t index); 45 | ~VersalBootImage(); 46 | 47 | void BuildAndLink(Binary* cache); 48 | void Add(BifOptions* bifoptions); 49 | void ReplaceImages(void); 50 | void ReplaceImagesNoSubSys(void); 51 | void AppendImagesInSubsystems(void); 52 | void OutputOptionalEfuseHash(); 53 | void OutputPartitionFiles(Options& options, Binary& cache) {}; 54 | void GenerateAuthenticationKeys(void) {}; 55 | 56 | void ConfigureEncryptionContext(ImageHeader * image, Encryption::Type encryptType); 57 | void ConfigureAuthenticationContext(ImageHeader * image, Authentication::Type authType, PartitionBifOptions * partitionbifoptions); 58 | void ConfigureChecksumContext(ImageHeader * image, Checksum::Type checksumType); 59 | void ConfigureProcessingStages(ImageHeader* image, PartitionBifOptions* partitionbifoptions); 60 | 61 | void CheckForIhtAttributes(std::string base_file); 62 | void ParseBootImage(PartitionBifOptions * it); 63 | void ValidateSecureAttributes(ImageHeader * image, BifOptions* bifoptions, PartitionBifOptions* partitionBifOptions); 64 | ImageHeader* ParsePartitionDataToImage(BifOptions* bifoptions, PartitionBifOptions* it); 65 | void OutputOptionalSecureDebugImage(); 66 | void ConfigureEncryptionBlocks(ImageHeader * image, PartitionBifOptions* partitionBifOptions); 67 | void SetPmcdataFile(const std::string & filename); 68 | uint64_t GetSecureChunkSize(bool isBootloader); 69 | uint32_t current_image_block; 70 | }; 71 | #endif 72 | -------------------------------------------------------------------------------- /bootimage-zynq.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #pragma once 19 | 20 | #ifndef _BOOTIMAGE_ZYNQ_H_ 21 | #define _BOOTIMAGE_ZYNQ_H_ 22 | 23 | 24 | /* 25 | ------------------------------------------------------------------------------- 26 | *********************************************** H E A D E R F I L E S *** 27 | ------------------------------------------------------------------------------- 28 | */ 29 | #include "bootimage.h" 30 | #include "bootheader-zynq.h" 31 | #include "imageheadertable-zynq.h" 32 | 33 | 34 | /* 35 | ------------------------------------------------------------------------------- 36 | ********************************************************* C L A S S E S *** 37 | ------------------------------------------------------------------------------- 38 | */ 39 | /******************************************************************************/ 40 | class ZynqBootImage : public BootImage 41 | { 42 | public: 43 | ZynqBootImage(Options& options, uint8_t index); 44 | ~ZynqBootImage() 45 | { 46 | if (importedBh != NULL) 47 | { 48 | delete importedBh; 49 | } 50 | }; 51 | 52 | void ConfigureEncryptionContext(ImageHeader * image, Encryption::Type encryptType); 53 | void ConfigureAuthenticationContext(ImageHeader * image, Authentication::Type authType, PartitionBifOptions * partitionbifoptions); 54 | void ConfigureChecksumContext(ImageHeader * image, Checksum::Type checksumType); 55 | void ConfigureProcessingStages(ImageHeader * image, PartitionBifOptions * partitionbifoptions); 56 | 57 | void DetermineEncryptionDefaults(); 58 | void ParseBootImage(PartitionBifOptions * it); 59 | void ValidateSecureAttributes(ImageHeader * image, BifOptions* bifoptions, PartitionBifOptions* partitionBifOptions); 60 | void ParsePartitionDataToImage(BifOptions* bifoptions, PartitionBifOptions* it); 61 | 62 | void Add(BifOptions* bifoptions); 63 | protected: 64 | ZynqBootHeader* importedBh; 65 | }; 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /bootimage-zynqmp.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #pragma once 19 | 20 | #ifndef _BOOTIMAGE_ZYNQMP_H_ 21 | #define _BOOTIMAGE_ZYNQMP_H_ 22 | 23 | 24 | /* 25 | ------------------------------------------------------------------------------- 26 | *********************************************** H E A D E R F I L E S *** 27 | ------------------------------------------------------------------------------- 28 | */ 29 | #include "bootimage.h" 30 | #include "bootheader-zynqmp.h" 31 | #include "imageheadertable-zynqmp.h" 32 | 33 | 34 | /* 35 | ------------------------------------------------------------------------------- 36 | ********************************************************* C L A S S E S *** 37 | ------------------------------------------------------------------------------- 38 | */ 39 | /******************************************************************************/ 40 | class ZynqMpBootImage : public BootImage 41 | { 42 | public: 43 | ZynqMpBootImage(Options& options, uint8_t index); 44 | ~ZynqMpBootImage() 45 | { 46 | if (importedBh != NULL) 47 | { 48 | delete importedBh; 49 | } 50 | } 51 | 52 | void ConfigureEncryptionContext(ImageHeader * image, Encryption::Type encryptType); 53 | void ConfigureAuthenticationContext(ImageHeader * image, Authentication::Type authType, PartitionBifOptions * partitionbifoptions); 54 | void ConfigureChecksumContext(ImageHeader * image, Checksum::Type checksumType); 55 | void ConfigureProcessingStages(ImageHeader * image, PartitionBifOptions * partitionbifoptions); 56 | 57 | void ParseBootImage(PartitionBifOptions * it); 58 | void ValidateSecureAttributes(ImageHeader * image,BifOptions* bifoptions, PartitionBifOptions* partitionBifOptions); 59 | void ParsePartitionDataToImage(BifOptions* bifoptions, PartitionBifOptions* it); 60 | 61 | void Add(BifOptions* bifoptions); 62 | 63 | protected: 64 | ZynqMpBootHeader* importedBh; 65 | }; 66 | #endif 67 | -------------------------------------------------------------------------------- /cdo-alloc.c: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2019-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include "cdo-alloc.h" 24 | 25 | void * myalloc(size_t len) { 26 | void * p = malloc(len); 27 | if (p == NULL) { 28 | fprintf(stderr, "end of memory\n"); 29 | exit(1); 30 | } 31 | return p; 32 | } 33 | 34 | void * myalloc_zero(size_t len) { 35 | void * p = myalloc(len); 36 | memset(p, 0, len); 37 | return p; 38 | } 39 | 40 | void * myrealloc(void * p, size_t len) { 41 | p = realloc(p, len); 42 | if (p == NULL) { 43 | fprintf(stderr, "end of memory\n"); 44 | exit(1); 45 | } 46 | return p; 47 | } 48 | 49 | void myfree(void * p) { 50 | free(p); 51 | } 52 | -------------------------------------------------------------------------------- /cdo-alloc.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2019-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #ifndef D_cdo_alloc 19 | #define D_cdo_alloc 20 | 21 | void * myalloc(size_t len); 22 | void * myalloc_zero(size_t len); 23 | void * myrealloc(void * p, size_t len); 24 | void myfree(void * p); 25 | 26 | #endif /* D_cdo_alloc */ 27 | -------------------------------------------------------------------------------- /cdo-binary.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2019-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #ifndef D_cdo_binary 19 | #define D_cdo_binary 20 | 21 | #include "cdo-command.h" 22 | 23 | typedef struct 24 | { 25 | uint8_t cmd_id; 26 | uint8_t module_id; 27 | uint16_t reserved1; 28 | uint8_t min_arg_cnt; 29 | uint8_t max_arg_cnt; 30 | uint16_t reserved2; 31 | } XPlmi_CmdInfo; 32 | 33 | void cdobinary_set_verbose(uint32_t verbose); 34 | void cdobinary_set_auto_align(uint32_t enable); 35 | void cdobinary_set_add_offset(uint32_t enable); 36 | CdoSequence * decode_cdo_binary(const void * data, size_t len); 37 | void cdoseq_write_header_raw(FILE * f, CdoSequence ** seq); 38 | void * cdoseq_to_binary(CdoSequence * seq, size_t * sizep, uint32_t be); 39 | int check_cdo_commands(void* data, uint32_t l, uint32_t * xplm_data, uint32_t xplm_length); 40 | int is_cdo_data(void * data, size_t size); 41 | uint32_t* get_slr_sync_point_offsets(void); 42 | uint32_t* get_slr_start_marker_offsets(void); 43 | uint32_t* get_slr_end_marker_offsets(void); 44 | void search_for_sync_points(void); 45 | size_t get_num_of_sync_points(void); 46 | size_t get_num_start_markers(void); 47 | size_t get_num_end_markers(void); 48 | #endif /* D_cdo_binary */ 49 | -------------------------------------------------------------------------------- /cdo-driver/README.md: -------------------------------------------------------------------------------- 1 | cdo-driver 2 | 3 | # Overview 4 | This is a library enabling the generation of CDO files which can be read by bootgen. 5 | It can be combined with the aie-rt libraries to enable device configuration. 6 | -------------------------------------------------------------------------------- /cdo-driver/cdo_driver.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2019-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #ifndef _CDO_DRIVER_H_ 19 | #define _CDO_DRIVER_H_ 20 | 21 | #include 22 | #include 23 | 24 | enum byte_ordering{Little_Endian, Big_Endian}; 25 | 26 | enum CDO_COMMAND { 27 | CDO_CMD_DMA_WRITE = 0x105U, 28 | CDO_CMD_MASK_POLL64 = 0x106U, 29 | CDO_CMD_MASK_WRITE64 = 0x107U, 30 | CDO_CMD_WRITE64 = 0x108U, 31 | CDO_CMD_NO_OPERATION = 0x111U 32 | }; 33 | 34 | typedef struct cdoHeader { 35 | 36 | /* Format: 37 | * 0x0 - No. of remaining words in header, 38 | * 0x4 - Identification Word, 39 | * 0x8 - Version, 40 | * 0xC - CDO Length, 41 | * 0x10- Checksum */ 42 | 43 | const uint32_t NumWords; 44 | const uint32_t IdentWord; 45 | const uint32_t Version; 46 | 47 | uint32_t CDOLength; // Length of CDO object in words excluding header. 48 | uint32_t CheckSum; // one's complement of sum of all fields in header i.e. ~(NumWords+IdentWord+Version+Length) 49 | 50 | } cdoHeader; 51 | 52 | void startCDOFileStream(const char* cdoFileName); 53 | void endCurrentCDOFileStream(); 54 | void FileHeader(); 55 | void EnAXIdebug(); 56 | void setEndianness(bool endianness); 57 | void SectionHeader(); 58 | void configureHeader(); 59 | unsigned int getPadBytesForDmaWrCmdAlignment(uint32_t DmaCmdLength); 60 | void insertNoOpCommand(unsigned int numPadBytes); 61 | void insertDmaWriteCmdHdr(uint32_t DmaCmdLength); 62 | void disableDmaCmdAlignment(); 63 | 64 | 65 | #endif /* _CDO_DRIVER_H_ */ 66 | -------------------------------------------------------------------------------- /cdo-load.c: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2019-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include "cdo-binary.h" 25 | #include "cdo-source.h" 26 | #include "cdo-raw.h" 27 | #include "cdo-load.h" 28 | 29 | #define MAX_LINE_LENGTH 100 30 | static char slr_id_binary; 31 | 32 | char slr_id_from_binary(char ch) 33 | { 34 | if (slr_id_binary != 0) 35 | { 36 | ch = slr_id_binary; 37 | slr_id_binary = 0; 38 | } 39 | return ch; 40 | } 41 | 42 | void * file_to_buf(const char * path, size_t * sizep) { 43 | FILE * f = fopen(path, "rb"); 44 | void * buf = NULL; 45 | size_t capacity = 0; 46 | size_t size = 0; 47 | *sizep = 0; 48 | if (f == NULL) { 49 | fprintf(stderr, "cannot open file: %s\n", path); 50 | return NULL; 51 | } 52 | 53 | for (;;) { 54 | size_t len; 55 | if (size == capacity) { 56 | capacity = capacity ? capacity * 2 : 1; 57 | buf = realloc(buf, capacity); 58 | if (buf == NULL) { 59 | fprintf(stderr, "end of memory while reading file: %s\n", path); 60 | goto error; 61 | } 62 | } 63 | len = fread((char *)buf + size, 1, capacity - size, f); 64 | if (len == 0) break; 65 | size += len; 66 | } 67 | 68 | if (ferror(f)) { 69 | fprintf(stderr, "cannot read file: %s\n", path); 70 | goto error; 71 | } 72 | 73 | fclose(f); 74 | *sizep = size; 75 | return buf; 76 | 77 | error: 78 | if (buf) free(buf); 79 | fclose(f); 80 | return NULL; 81 | } 82 | 83 | CdoSequence * cdoseq_load_cdo(const char * path) { 84 | CdoSequence * seq = NULL; 85 | CdoRawInfo * raw = NULL; 86 | size_t size; 87 | void * data = file_to_buf(path, &size); 88 | if (data == NULL) goto done; 89 | raw = cdoraw_decode(data, size); 90 | if (raw != NULL) { 91 | free(data); 92 | data = raw->data; 93 | size = raw->size; 94 | } 95 | if (is_cdo_data(data, size)) { 96 | seq = decode_cdo_binary(data, size); 97 | if (seq == NULL) { 98 | fprintf(stderr, "cannot decode binary cdo file: %s\n", path); 99 | goto done; 100 | } 101 | if (raw != NULL) { 102 | cdometa_add_markers(&raw->meta, seq); 103 | } 104 | } else { 105 | FILE * infile = fopen(path, "r"); 106 | if (infile == NULL) { 107 | fprintf(stderr, "cannot open file: %s\n", path); 108 | goto done; 109 | } 110 | seq = cdoseq_from_source(infile); 111 | fclose(infile); 112 | if (seq == NULL) { 113 | fprintf(stderr, "cannot parse source cdo file: %s\n", path); 114 | goto done; 115 | } 116 | } 117 | done: 118 | if (raw != NULL) { 119 | cdoraw_delete(raw); 120 | } else { 121 | free(data); 122 | } 123 | return seq; 124 | } 125 | 126 | void cdoseq_extract_writes(CdoSequence * seq) { 127 | LINK * l = seq->cmds.next; 128 | while (l != &seq->cmds) { 129 | CdoCommand * cmd = all2cmds(l); 130 | l = l->next; 131 | switch (cmd->type) { 132 | case (CdoCmdWrite): 133 | case (CdoCmdMaskWrite): 134 | /* Ignore writes that configure frequency */ 135 | if (cmd->dstaddr == 0xf1260050 || cmd->dstaddr == 0xf1260054 || 136 | cmd->dstaddr == 0xf1260104) { 137 | cdocmd_free(cmd); 138 | } 139 | break; 140 | default: 141 | cdocmd_free(cmd); 142 | break; 143 | } 144 | } 145 | } 146 | 147 | /******************************************************************************/ 148 | CdoSequence* cdoseq_extract_cdo_till_ssit_sync_slaves(CdoSequence * seq, uint32_t sync_index) 149 | { 150 | CdoSequence* temp_seq = seq; 151 | 152 | LINK * l = temp_seq->cmds.next; 153 | uint32_t num_sync_slave_cmds = 0; 154 | uint8_t stop = 0; 155 | uint8_t start = 0; 156 | if (num_sync_slave_cmds == sync_index - 1) 157 | { 158 | start = 1; 159 | } 160 | while (l != &temp_seq->cmds) 161 | { 162 | CdoCommand * cmd = all2cmds(l); 163 | l = l->next; 164 | if ((cmd->type == CdoCmdSsitSyncSlaves) && (stop == 0)) 165 | { 166 | num_sync_slave_cmds++; 167 | if ((num_sync_slave_cmds == sync_index - 1) && (start == 0)) 168 | { 169 | start = 1; 170 | cdocmd_free(cmd); 171 | continue; 172 | } 173 | if (num_sync_slave_cmds == sync_index) 174 | { 175 | stop = 1; 176 | continue; 177 | } 178 | } 179 | if ((start == 0) || (stop == 1)) 180 | { 181 | cdocmd_free(cmd); 182 | } 183 | } 184 | return temp_seq; 185 | } 186 | -------------------------------------------------------------------------------- /cdo-load.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2019-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #ifndef D_cdo_load 19 | #define D_cdo_load 20 | 21 | #include 22 | #include "cdo-command.h" 23 | 24 | void * file_to_buf(const char * path, size_t * sizep); 25 | CdoSequence * cdoseq_load_cdo(const char * path); 26 | 27 | char slr_id_from_binary(char ch); 28 | void cdoseq_extract_writes(CdoSequence * seq); 29 | CdoSequence* cdoseq_extract_cdo_till_ssit_sync_slaves(CdoSequence * seq, uint32_t sync_index); 30 | #endif /* D_cdo_load */ 31 | -------------------------------------------------------------------------------- /cdo-metadata.c: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include "cdo-alloc.h" 23 | #include "cdo-metadata.h" 24 | 25 | static void copy_marker_string(char * dst, size_t dstmax, CdoCommand * cmd) { 26 | size_t len = strlen(cmd->buf); 27 | if (len >= dstmax) len = dstmax - 1; 28 | memcpy(dst, cmd->buf, len); 29 | dst[len] = '\0'; 30 | } 31 | 32 | void cdometa_extract(CdoMetadata * meta, CdoSequence * seq) { 33 | LINK * l = seq->cmds.next; 34 | 35 | while (l != &seq->cmds) { 36 | CdoCommand * cmd = all2cmds(l); 37 | l = l->next; 38 | if (cmd->type == CdoCmdMarker) { 39 | switch (cmd->value) { 40 | case 1: 41 | if (meta->creator[0] != '\0') break; 42 | copy_marker_string(meta->creator, sizeof meta->creator, cmd); 43 | break; 44 | case 2: 45 | if (meta->design[0] != '\0') break; 46 | copy_marker_string(meta->design, sizeof meta->design, cmd); 47 | break; 48 | case 3: 49 | if (meta->arch[0] != '\0') break; 50 | copy_marker_string(meta->arch, sizeof meta->arch, cmd); 51 | break; 52 | case 4: 53 | if (meta->part[0] != '\0') break; 54 | copy_marker_string(meta->part, sizeof meta->part, cmd); 55 | break; 56 | case 5: 57 | if (meta->slr[0] != '\0') break; 58 | copy_marker_string(meta->slr, sizeof meta->slr, cmd); 59 | break; 60 | case 6: 61 | if (meta->date[0] != '\0') break; 62 | copy_marker_string(meta->date, sizeof meta->date, cmd); 63 | break; 64 | default: 65 | break; 66 | } 67 | } 68 | } 69 | } 70 | 71 | void cdometa_add_markers(CdoMetadata * meta, CdoSequence * seq) { 72 | CdoSequence * newseq = cdocmd_create_sequence(); 73 | CdoMetadata curmeta; 74 | 75 | memset(&curmeta, 0, sizeof curmeta); 76 | cdometa_extract(&curmeta, seq); 77 | if (meta->creator[0] != '\0' && curmeta.creator[0] == '\0') { 78 | cdocmd_add_marker(newseq, 1, meta->creator); 79 | } 80 | if (meta->design[0] != '\0' && curmeta.design[0] == '\0') { 81 | cdocmd_add_marker(newseq, 2, meta->design); 82 | } 83 | if (meta->arch[0] != '\0' && curmeta.arch[0] == '\0') { 84 | cdocmd_add_marker(newseq, 3, meta->arch); 85 | } 86 | if (meta->part[0] != '\0' && curmeta.part[0] == '\0') { 87 | cdocmd_add_marker(newseq, 4, meta->part); 88 | } 89 | if (meta->slr[0] != '\0' && curmeta.slr[0] == '\0') { 90 | cdocmd_add_marker(newseq, 5, meta->slr); 91 | } 92 | if (meta->date[0] != '\0' && curmeta.date[0] == '\0') { 93 | cdocmd_add_marker(newseq, 6, meta->date); 94 | } 95 | 96 | cdocmd_concat_seq(newseq, seq); 97 | cdocmd_concat_seq(seq, newseq); 98 | cdocmd_delete_sequence(newseq); 99 | } 100 | -------------------------------------------------------------------------------- /cdo-metadata.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #ifndef D_cdo_metadata 19 | #define D_cdo_metadata 20 | 21 | #include "cdo-command.h" 22 | 23 | typedef struct CdoMetadata CdoMetadata; 24 | 25 | struct CdoMetadata { 26 | char creator[256]; 27 | char design[128]; 28 | char arch[128]; 29 | char part[64]; 30 | char slr[32]; 31 | char date[32]; 32 | }; 33 | 34 | void cdometa_extract(CdoMetadata * meta, CdoSequence * seq); 35 | void cdometa_add_markers(CdoMetadata * meta, CdoSequence * seq); 36 | 37 | #endif /* D_cdo_metadata */ 38 | -------------------------------------------------------------------------------- /cdo-npi.c: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2019-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include "cdo-binary.h" 26 | #include "cdo-source.h" 27 | #include "cdo-npi.h" 28 | 29 | uint32_t cdocmd_post_process_cdo_seq(const void * data, size_t size, CdoSequence ** seqp) { 30 | CdoSequence * seq = decode_cdo_binary(data, size); 31 | *seqp = NULL; 32 | if (seq == NULL) return 1; 33 | *seqp = seq; 34 | return 0; 35 | } 36 | 37 | uint32_t cdocmd_post_process_cdo(const void * data, size_t size, void ** new_data, size_t * new_size) { 38 | CdoSequence * seq; 39 | *new_data = NULL; 40 | *new_size = 0; 41 | cdobinary_set_auto_align(1); 42 | if (cdocmd_post_process_cdo_seq(data, size, &seq)) return 1; 43 | if (seq == NULL) return 0; 44 | *new_data = cdoseq_to_binary(seq, new_size, 0); 45 | cdocmd_delete_sequence(seq); 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /cdo-npi.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2019-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #ifndef D_cdo_npi 19 | #define D_cdo_npi 20 | 21 | #include "cdo-command.h" 22 | 23 | enum { 24 | houseclean_bypass_lpd = 1, 25 | houseclean_bypass_fpd = 2, 26 | houseclean_bypass_npd = 4, 27 | houseclean_bypass_plpd = 8, 28 | houseclean_bypass_cpm = 16, 29 | }; 30 | 31 | int is_platform_spp(void); 32 | int cdocmd_post_process_mode(const char * s, uint32_t verbose); 33 | void set_cfu_stream_keyhole_size(uint64_t size); 34 | void set_platform_spp(void); 35 | void set_spp_ddr_enable(uint32_t enable); 36 | void cdocmd_post_process_start(void); 37 | uint32_t cdocmd_post_process_end_seq(CdoSequence ** seqp); 38 | void cdocmd_post_process_end(void ** new_data, size_t * new_size); 39 | uint32_t cdocmd_post_process_cdo_seq(const void * data, size_t size, CdoSequence ** seqp); 40 | uint32_t cdocmd_post_process_cfi_seq(const void * data, size_t size, uint32_t is_gsc, CdoSequence ** seqp); 41 | uint32_t cdocmd_post_process_cdo(const void * data, size_t size, void ** new_data, size_t * new_size); 42 | uint32_t cdocmd_post_process_cfi(const void * data, size_t size, uint32_t is_gsc, void ** new_data, size_t * new_size); 43 | void cdocmd_rewrite_address_filter(CdoSequence * seq, const char * address_filter_file); 44 | 45 | #endif /* D_cdo_npi */ 46 | -------------------------------------------------------------------------------- /cdo-overlay.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2019-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #ifndef D_cdo_overlay 19 | #define D_cdo_overlay 20 | 21 | #include "cdo-command.h" 22 | 23 | typedef struct CdoOverlayInfo CdoOverlayInfo; 24 | typedef struct CdoOverlayEntry CdoOverlayEntry; 25 | 26 | struct CdoOverlayEntry { 27 | CdoCommand * start; 28 | CdoCommand * end; 29 | unsigned int level; 30 | }; 31 | 32 | struct CdoOverlayInfo { 33 | CdoSequence * seq; 34 | LINK actions; 35 | }; 36 | 37 | CdoOverlayInfo * cdooverlay_open(CdoSequence * seq); 38 | void cdooverlay_close(CdoOverlayInfo * info); 39 | int cdooverlay_apply(CdoSequence * seq, CdoOverlayInfo * ovl); 40 | 41 | #endif /* D_cdo_overlay */ 42 | -------------------------------------------------------------------------------- /cdo-raw.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2019-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #ifndef D_cdo_raw 19 | #define D_cdo_raw 20 | 21 | #include "cdo-metadata.h" 22 | 23 | typedef struct CdoRawInfo CdoRawInfo; 24 | 25 | typedef enum { 26 | CdoRawUnknown, 27 | CdoRawCdo, 28 | CdoRawCfi 29 | } CdoRawType; 30 | 31 | struct CdoRawInfo { 32 | CdoRawType type; 33 | CdoMetadata meta; 34 | uint32_t data_free_disable:1; 35 | size_t size; 36 | uint32_t *data; 37 | }; 38 | 39 | CdoRawInfo * cdoraw_create(CdoRawType type); 40 | void cdoraw_delete(CdoRawInfo * raw); 41 | CdoRawInfo * cdoraw_decode(void * data, size_t size); 42 | void cdoraw_encode(CdoRawInfo * raw, FILE * f); 43 | void cdoraw_encode_seq(FILE * f, CdoSequence * seq, uint32_t be); 44 | CdoRawInfo * decode_raw(void * data, size_t size); 45 | void encode_raw(FILE * f, void * data, size_t size); 46 | 47 | #endif /* D_cdo_raw */ 48 | -------------------------------------------------------------------------------- /cdo-source.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2019-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #ifndef D_cdo_source 19 | #define D_cdo_source 20 | 21 | #include 22 | #include "cdo-command.h" 23 | 24 | CdoSequence * cdoseq_from_source(FILE * f); 25 | void cdoseq_to_source(FILE * f, CdoSequence * seq); 26 | char slr_id_from_source(char ch); 27 | #endif /* D_cdo_source */ 28 | -------------------------------------------------------------------------------- /checksum-versal.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #pragma once 19 | 20 | /* 21 | ------------------------------------------------------------------------------- 22 | *********************************************** H E A D E R F I L E S *** 23 | ------------------------------------------------------------------------------- 24 | */ 25 | #include "bootgenenum.h" 26 | #include "binary.h" 27 | #include "bootimage.h" 28 | #include "checksum.h" 29 | 30 | class BootImage; 31 | class Section; 32 | 33 | 34 | /* 35 | ------------------------------------------------------------------------------- 36 | ********************************************* P R E P R O C E S S O R S *** 37 | ------------------------------------------------------------------------------- 38 | */ 39 | #define MD5_LENGTH_BYTES 16 40 | #define SHA2_LENGTH_BYTES 32 41 | #define SHA3_LENGTH_BYTES 48 42 | 43 | 44 | /* 45 | ------------------------------------------------------------------------------- 46 | ********************************************************* C L A S S E S *** 47 | ------------------------------------------------------------------------------- 48 | */ 49 | /******************************************************************************/ 50 | class VersalSHA3ChecksumContext : public ChecksumContext 51 | { 52 | public: 53 | Checksum::Type Type() 54 | { 55 | return Checksum::SHA3; 56 | } 57 | 58 | uint8_t Size() 59 | { 60 | return SHA3_LENGTH_BYTES; 61 | } 62 | 63 | Section* Build(Binary& cache, const uint8_t* data, const Binary::Length_t length); 64 | Section* Build(std::string name); 65 | 66 | void Link(bool bootloader, const uint8_t* data, const Binary::Length_t length, Section* section); 67 | }; 68 | 69 | /******************************************************************************/ 70 | class VersalChecksumTable : public ChecksumTable 71 | { 72 | public: 73 | void Build(BootImage& bi, Binary& cache); 74 | void Link(BootImage& bi); 75 | }; 76 | -------------------------------------------------------------------------------- /checksum.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #pragma once 19 | 20 | #ifndef _CHECKSUM_H_ 21 | #define _CHECKSUM_H_ 22 | 23 | /* 24 | ------------------------------------------------------------------------------- 25 | *********************************************** H E A D E R F I L E S *** 26 | ------------------------------------------------------------------------------- 27 | */ 28 | #include "bootgenenum.h" 29 | #include "binary.h" 30 | 31 | /* Forward Class References */ 32 | class BootImage; 33 | class Section; 34 | 35 | 36 | /* 37 | ------------------------------------------------------------------------------- 38 | ********************************************* P R E P R O C E S S O R S *** 39 | ------------------------------------------------------------------------------- 40 | */ 41 | #define MD5_LENGTH_BYTES 16 42 | #define SHA2_LENGTH_BYTES 32 43 | #define SHA3_LENGTH_BYTES 48 44 | 45 | 46 | /* 47 | ------------------------------------------------------------------------------- 48 | ********************************************************* C L A S S E S *** 49 | ------------------------------------------------------------------------------- 50 | */ 51 | /******************************************************************************/ 52 | class ChecksumTable 53 | { 54 | public: 55 | ChecksumTable() {} 56 | virtual ~ChecksumTable() {} 57 | 58 | virtual void Build(BootImage& bi, Binary& cache); 59 | virtual void Link(BootImage& bi); 60 | }; 61 | 62 | /******************************************************************************/ 63 | class ChecksumContext 64 | { 65 | public: 66 | ChecksumContext() {} 67 | virtual ~ChecksumContext() {} 68 | 69 | virtual Checksum::Type Type() = 0; 70 | 71 | virtual uint8_t Size() 72 | { 73 | return 0; 74 | } 75 | 76 | virtual Section* Build(Binary& cache, const uint8_t* data, const Binary::Length_t length) 77 | { 78 | return NULL; 79 | } 80 | 81 | virtual Section* Build(std::string name) 82 | { 83 | return NULL; 84 | } 85 | 86 | virtual void Link(bool bootloader, const uint8_t* data, const Binary::Length_t length, Section* section) 87 | { 88 | 89 | } 90 | }; 91 | 92 | /******************************************************************************/ 93 | class NoneChecksumContext : public ChecksumContext 94 | { 95 | public: 96 | virtual Checksum::Type Type() 97 | { 98 | return Checksum::None; 99 | } 100 | }; 101 | 102 | /******************************************************************************/ 103 | class MD5ChecksumContext : public ChecksumContext 104 | { 105 | public: 106 | MD5ChecksumContext() {} 107 | ~MD5ChecksumContext() {} 108 | 109 | Checksum::Type Type() 110 | { 111 | return Checksum::MD5; 112 | } 113 | 114 | uint8_t Size() 115 | { 116 | return MD5_LENGTH_BYTES; 117 | } 118 | 119 | Section* Build(Binary& cache, const uint8_t* data, const Binary::Length_t length); 120 | void Link(bool bootloader, const uint8_t* data, const Binary::Length_t length, Section* section); 121 | }; 122 | 123 | /******************************************************************************/ 124 | class SHA2ChecksumContext : public ChecksumContext 125 | { 126 | public: 127 | SHA2ChecksumContext() {} 128 | ~SHA2ChecksumContext() {} 129 | 130 | Checksum::Type Type() 131 | { 132 | return Checksum::SHA2; 133 | } 134 | 135 | uint8_t Size() 136 | { 137 | return SHA2_LENGTH_BYTES; 138 | } 139 | 140 | Section* Build(Binary& cache, const uint8_t* data, const Binary::Length_t length); 141 | void Link(bool bootloader, const uint8_t* data, const Binary::Length_t length, Section* section); 142 | }; 143 | 144 | /******************************************************************************/ 145 | class SHA3ChecksumContext : public ChecksumContext 146 | { 147 | public: 148 | SHA3ChecksumContext() {} 149 | ~SHA3ChecksumContext() {} 150 | 151 | Checksum::Type Type() 152 | { 153 | return Checksum::SHA3; 154 | } 155 | 156 | uint8_t Size() 157 | { 158 | return SHA3_LENGTH_BYTES; 159 | } 160 | 161 | virtual Section* Build(Binary& cache, const uint8_t* data, const Binary::Length_t length) ; 162 | virtual Section* Build(std::string name) { return NULL; } 163 | virtual void Link(bool bootloader, const uint8_t* data, const Binary::Length_t length, Section* section); 164 | }; 165 | #endif 166 | -------------------------------------------------------------------------------- /cmdoptionsscanner.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #pragma once 19 | 20 | #ifndef _CMDOPTIONSSCANNER_H_ 21 | #define _CMDOPTIONSSCANNER_H_ 22 | 23 | /* 24 | ------------------------------------------------------------------------------- 25 | *********************************************** H E A D E R F I L E S *** 26 | ------------------------------------------------------------------------------- 27 | */ 28 | // Only include FlexLexer.h if it hasn't been already included 29 | #if ! defined(yyFlexLexerOnce) 30 | 31 | #undef yyFlexLexer 32 | #define yyFlexLexer reginitFlexLexer 33 | #include "FlexLexer.h" 34 | #endif 35 | 36 | // Override the interface for yylex since we namespaced it 37 | #undef YY_DECL 38 | #define YY_DECL int CO::FlexScanner::yylex() 39 | 40 | // Include Bison for types / tokens 41 | #include "cmdoptions.tab.hpp" 42 | #include "options.h" 43 | #include "parsing.h" 44 | 45 | 46 | /* 47 | ------------------------------------------------------------------------------- 48 | ********************************************************* C L A S S E S *** 49 | ------------------------------------------------------------------------------- 50 | */ 51 | 52 | /******************************************************************************/ 53 | namespace CO 54 | { 55 | class FlexScanner : public yyFlexLexer 56 | { 57 | public: 58 | // save the pointer to yylval so we can change it, and invoke scanner 59 | int yylex(CO::BisonParser::semantic_type * lval, CO::BisonParser::location_type* loc) 60 | { 61 | yylval = lval; 62 | yylloc = loc; 63 | return yylex(); 64 | } 65 | 66 | void comment(); 67 | std::string commandline; 68 | void ReportError(const CO::BisonParser::location_type &loc, const std::string &msg) 69 | { 70 | std::cerr << "ERROR: " << msg << "\n"; 71 | Parsing::Error2(loc,commandline); 72 | } 73 | 74 | private: 75 | // Scanning function created by Flex; make this private to force usage 76 | // of the overloaded method so we can get a pointer to Bison's yylval 77 | int yylex(); 78 | 79 | // point to yylval (provided by Bison in overloaded yylex) 80 | CO::BisonParser::semantic_type * yylval; 81 | CO::BisonParser::location_type * yylloc; 82 | }; 83 | } 84 | #endif 85 | -------------------------------------------------------------------------------- /elftypes.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #pragma once 19 | 20 | #ifndef _ELFTYPES_H_ 21 | #define _ELFTYPES_H_ 22 | 23 | /* 24 | ------------------------------------------------------------------------------- 25 | *********************************************** H E A D E R F I L E S *** 26 | ------------------------------------------------------------------------------- 27 | */ 28 | #include 29 | 30 | 31 | /* ELF 32-bit */ 32 | typedef uint32_t Elf32_Addr; 33 | typedef uint16_t Elf32_Half; 34 | typedef uint32_t Elf32_Off; 35 | typedef int32_t Elf32_Sword; 36 | typedef uint32_t Elf32_Xword; 37 | typedef uint32_t Elf32_Word; 38 | 39 | /* ELF 64-bit */ 40 | typedef uint64_t Elf64_Addr; 41 | typedef uint64_t Elf64_Off; 42 | typedef int64_t Elf64_Sxword; 43 | typedef uint64_t Elf64_Xword; 44 | typedef int32_t Elf32_Sword; 45 | typedef uint32_t Elf64_Word; 46 | typedef uint16_t Elf64_Half; 47 | #endif 48 | -------------------------------------------------------------------------------- /encryption-versal.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #pragma once 19 | #ifndef _ENCRYPTION_VERSAL_CONTEXT_ 20 | #define _ENCRYPTION_VERSAL_CONTEXT_ 21 | 22 | /* 23 | ------------------------------------------------------------------------------- 24 | *********************************************** H E A D E R F I L E S *** 25 | ------------------------------------------------------------------------------- 26 | */ 27 | #include 28 | #include "bootgenenum.h" 29 | #include "options.h" 30 | #include 31 | #include 32 | 33 | /* Forward Class References */ 34 | class PartitionHeader; 35 | class Options; 36 | class DataMover; 37 | 38 | /* 39 | ------------------------------------------------------------------------------- 40 | ********************************************************* C L A S S E S *** 41 | ------------------------------------------------------------------------------- 42 | */ 43 | /******************************************************************************/ 44 | class VersalEncryptionContext : public EncryptionContext 45 | { 46 | public: 47 | VersalEncryptionContext(); 48 | VersalEncryptionContext(const EncryptionContext * other); 49 | ~VersalEncryptionContext(); 50 | 51 | Encryption::Type Type() 52 | { 53 | return Encryption::AES; 54 | } 55 | 56 | void Process(BootImage& bi, PartitionHeader* partition); 57 | void Process(BootImage& bi); 58 | 59 | //VersalNet defines 60 | uint32_t ConfigureEncryptionBlocksforPmcData(BootImage& bi, PartitionHeader * partHdr); 61 | uint32_t ConfigureEncryptionBlocksforPartition(BootImage & bi, PartitionHeader * partHdr); 62 | void ChunkifyAndProcess(BootImage & bi, PartitionHeader * partHdr); 63 | 64 | void SetAesSeedString(const std::string & key); 65 | void SetAesSeed(const uint8_t * key); 66 | const uint32_t* GetAesSeed(void); 67 | 68 | void GenerateAesKey(void); 69 | 70 | void WriteEncryptionKeyFile(const std::string& baseFileName, bool useOptionalKey, uint32_t blocks); 71 | void ReadEncryptionKeyFile(const std::string& keyFileName); 72 | 73 | void SetAesKey(const uint8_t* binarykey); 74 | const uint32_t* GetAesKey(void); 75 | 76 | void SetIv(const uint8_t* iv); 77 | void SetIvString(const std::string& IV); 78 | void GenerateIv(void); 79 | const uint32_t* GetIv(void); 80 | 81 | void PackNextEncryptionKey(uint8_t* aesKeyNext, int aeskeyPtr); 82 | void PackNextIv(uint8_t * aesIvNext, int aesIvPtr); 83 | void GetNextKey(uint8_t* keyNext, int ptr); 84 | void GetNextIv(uint8_t* keyNext, int ptr); 85 | 86 | void GenerateRemainingKeys(Options& options); 87 | 88 | private: 89 | bool isBootloader; 90 | bool isPmcData; 91 | bool aesSeedexits; 92 | 93 | void ChunkifyAndEncrypt(Options& options, const uint8_t *inBuf, uint32_t inLen, uint8_t *aad, uint32_t aad_len, uint8_t* outBuf, uint32_t& outLen); 94 | void GetEncryptionKeys(Options& options, uint8_t* aesKey, uint8_t* aesOptKey, uint8_t* aesIV); 95 | void WarnforDPACMImpactonBootTime(bool dpacmKeyRollingEnable, bool dpacmMaskEnable); 96 | 97 | void AesGcm256Encrypt(unsigned char* gcm_pt, int pt_len, unsigned char* gcm_key, unsigned char* gcm_iv, unsigned char* gcm_aad, int aad_len, 98 | unsigned char* gcm_ct, int& ct_len, unsigned char* gcm_tag); 99 | void AesGcm256Decrypt(unsigned char* gcm_pt, int& pt_len, unsigned char* gcm_key, unsigned char* gcm_iv, unsigned char* gcm_aad, int aad_len, 100 | unsigned char* gcm_ct, int ct_len, unsigned char* gcm_tag); 101 | 102 | protected: 103 | AesGcmEncryptionContext* encryptionAlgorithm; 104 | }; 105 | #endif 106 | -------------------------------------------------------------------------------- /encryption-zynq.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #ifndef _ENCRYPTION_ZYNQ_CONTEXT_ 19 | #define _ENCRYPTION_ZYNQ_CONTEXT_ 20 | 21 | 22 | /* 23 | ------------------------------------------------------------------------------- 24 | *********************************************** H E A D E R F I L E S *** 25 | ------------------------------------------------------------------------------- 26 | */ 27 | #include "encryption.h" 28 | #include "encryptutils.h" 29 | 30 | /* 31 | ------------------------------------------------------------------------------- 32 | ********************************************************* C L A S S E S *** 33 | ------------------------------------------------------------------------------- 34 | */ 35 | /******************************************************************************/ 36 | class ZynqEncryptionContext : public EncryptionContext 37 | { 38 | public: 39 | ZynqEncryptionContext(); 40 | ZynqEncryptionContext(const EncryptionContext* other); 41 | ZynqEncryptionContext(uint32_t wordsPerAesBlock, uint32_t words_per_AES_key0); 42 | 43 | ~ZynqEncryptionContext(); 44 | 45 | Encryption::Type Type() 46 | { 47 | return Encryption::AES; 48 | } 49 | 50 | void ReadEncryptionKeyFile(const std::string& keyFileName); 51 | void GenerateEncryptionKeyFile(const std::string& baseFileName, Options& options); 52 | void WriteEncryptionKeyFile(const std::string& baseFileName, bool useOptionalKey = false, uint32_t blocks = 0); 53 | void Process(BootImage& bi, PartitionHeader* partition); 54 | 55 | void GenerateTempKeys(void); 56 | void EncryptKeys(const uint8_t* inputData, uint32_t inputDataByteLength, uint8_t* encryptedData); 57 | 58 | void ClearKeySchedule(void); 59 | void InitKeySchedule(void); 60 | void AES_Encrypt(const uint8_t* inputData, uint32_t inputDataByteLength, uint8_t* encryptedData); 61 | void ComputeHMACDigest(const uint8_t* data, const uint32_t dataByteLength, bool initializeNewDigest, uint8_t* digest); 62 | 63 | void GenerateAesKey(void); 64 | void SetAesKey(const uint8_t* binarykey); 65 | const uint8_t* GetAesKey(void); 66 | 67 | void GenerateCbc(void); 68 | void setCbcString(const std::string& ASCIIinitialVector); 69 | void SetCbc(const uint8_t * binaryinitialVector); 70 | const uint8_t* GetCbc(void); 71 | 72 | void GenerateHmacKey(void); 73 | void SetHmacKeyString(const std::string& asciiHMACKey); 74 | void SetHmacKey(const uint8_t * binaryHMACKey); 75 | const uint8_t* GetHmacKey(void); 76 | 77 | DataMover* dataMover; 78 | bool maskEfuseFlag; 79 | 80 | private: 81 | void AddRoundKey(uint32_t* int_array, uint32_t num_round); 82 | void ByteSubTransformation(uint32_t* int_array); 83 | void ShiftRowTransformation(uint32_t* int_array); 84 | void MixColumnTransformation(uint32_t* int_array); 85 | 86 | uint32_t CvtChars2Int(uint8_t value1, uint8_t value2, uint8_t value3, uint8_t value4); 87 | uint32_t Transform(uint32_t orig_col, uint32_t col_num, bool rotate_rcon); 88 | 89 | bool first_AES_block; 90 | uint32_t d_NumRounds; 91 | uint32_t wordsPerAesKey; 92 | uint32_t wordsPerAesBlock; 93 | uint32_t* working_AES_block; 94 | uint32_t* keySchedule; 95 | uint32_t* aesKey; 96 | uint8_t ivCbc[16]; 97 | uint32_t* hmacKey; 98 | }; 99 | 100 | /******************************************************************************/ 101 | class DataStreamEncryption : public ZynqEncryptionContext 102 | { 103 | public: 104 | DataStreamEncryption(uint32_t wordsPerAesBlock, uint32_t words_per_AES_key0); 105 | ~DataStreamEncryption(void); 106 | 107 | void AESDataStreamEncrypt(KeySource::Type keySource, bool isFPGAData, const uint8_t* inputData, 108 | uint32_t inputDataByteLength, uint8_t*& encryptedDataStream, 109 | uint32_t& encryptedBufferByteLength, uint32_t* presBitHdr, uint32_t presBitHdrSize); 110 | 111 | private: 112 | void SetDataStreamHeader(KeySource::Type keySource, uint32_t encryptedStreamByteLength, uint32_t* presBitHdr, uint32_t presBitHdrSize); 113 | void InitializeInputDataStream(const uint8_t* inputData, uint32_t inputDataByteLength, bool isFPGAData); 114 | void InitializeHMAC_ioPadData(uint8_t padByte, uint8_t * padBuffer); 115 | void InitializePad1Data(uint32_t pad1DataByteLength); 116 | void InitializePad2Data(uint32_t pad2DataByteLength); 117 | void EncryptDataStream(uint8_t* encryptedOutputData); 118 | 119 | uint8_t* stream_header; 120 | uint8_t ipad_data[BYTES_PER_IPAD]; 121 | uint8_t* input_data_Buffer; 122 | uint32_t input_data_ByteLength; 123 | uint8_t pad1_data[BYTES_PER_PAD1]; 124 | uint8_t gap_2048[BYTES_PER_2048_GAP]; 125 | uint8_t opad_data[BYTES_PER_OPAD]; 126 | uint8_t first_digest_gap[BYTES_PER_FIRST_DIGEST]; 127 | uint8_t pad2_data[BYTES_PER_PAD2]; 128 | uint8_t HMAC_digest[BYTES_PER_HMAC_DIGEST]; 129 | 130 | uint8_t calculated_first_digest[BYTES_PER_FIRST_DIGEST + BYTES_PER_PAD2]; 131 | }; 132 | #endif 133 | -------------------------------------------------------------------------------- /encryption-zynqmp.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #ifndef _ENCRYPTION_ZYNQMP_CONTEXT_ 19 | #define _ENCRYPTION_ZYNQMP_CONTEXT_ 20 | 21 | 22 | /* 23 | ------------------------------------------------------------------------------- 24 | *********************************************** H E A D E R F I L E S *** 25 | ------------------------------------------------------------------------------- 26 | */ 27 | #include "encryption.h" 28 | 29 | 30 | /* 31 | ------------------------------------------------------------------------------- 32 | ********************************************************* C L A S S E S *** 33 | ------------------------------------------------------------------------------- 34 | */ 35 | 36 | /******************************************************************************/ 37 | class ZynqMpEncryptionContext : public EncryptionContext 38 | { 39 | public: 40 | ZynqMpEncryptionContext(); 41 | ZynqMpEncryptionContext(const EncryptionContext * other); 42 | ~ZynqMpEncryptionContext(); 43 | 44 | Encryption::Type Type() 45 | { 46 | return Encryption::AES; 47 | } 48 | 49 | void ReadEncryptionKeyFile(const std::string& keyFileName); 50 | void WriteEncryptionKeyFile(const std::string& baseFileName, bool useOptionalKey, uint32_t blocks); 51 | void Process(BootImage& bi, PartitionHeader* partition); 52 | void GenerateAesKey(void); 53 | 54 | void SetAesSeedString(const std::string & key); 55 | void SetAesSeed(const uint8_t * key); 56 | const uint32_t* GetAesSeed(void); 57 | 58 | void SetAesKey(const uint8_t* binarykey); 59 | const uint32_t* GetAesKey(void); 60 | 61 | void SetIv(const uint8_t* iv); 62 | void SetIvString(const std::string& IV); 63 | void GenerateIv(void); 64 | const uint32_t* GetIv(void); 65 | 66 | void SetAesOptKey(const uint8_t* binarykey); 67 | void SetAesOptKeyString(const std::string& key); 68 | void GenerateAesOptKey(void); 69 | const uint32_t* GetAesOptKey(void); 70 | 71 | void PackNextEncryptionKey(uint8_t* aesKeyNext, int aeskeyPtr); 72 | void PackNextIv(uint8_t * aesIvNext, int aesIvPtr); 73 | void GetNextKey(uint8_t* keyNext, int ptr); 74 | void GetNextIv(uint8_t* keyNext, int ptr); 75 | 76 | void GetEncryptionKeys(Options& options, uint8_t* aesKey, uint8_t* aesOptKey, uint8_t* aesIV); 77 | void GenerateRemainingKeys(Options& options, std::string aesFilename); 78 | 79 | protected: 80 | bool isBootloader; 81 | bool aesSeedexits; 82 | int partNum; 83 | 84 | void ChunkifyAndEncrypt(Options& options, const uint8_t *inBuf, uint32_t inLen, uint8_t* outBuf, uint32_t& outLen); 85 | void GenerateGreyKey(); 86 | void ReadBhIv(uint8_t * bhIv); 87 | AesGcmEncryptionContext* encryptionAlgorithm; 88 | }; 89 | #endif 90 | -------------------------------------------------------------------------------- /encryptutils.cpp: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | /* 19 | ------------------------------------------------------------------------------- 20 | *********************************************** H E A D E R F I L E S *** 21 | ------------------------------------------------------------------------------- 22 | */ 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | #include "encryptutils.h" 37 | #include "fileutils.h" 38 | #include "stringutils.h" 39 | #include "systemutils.h" 40 | 41 | 42 | static const uint8_t reverse8BitValueTable[256] = 43 | { 44 | 0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0, // 0x00 45 | 0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, 0x18, 0x98, 0x58, 0xD8, 0x38, 0xB8, 0x78, 0xF8, // 0x10 46 | 0x04, 0x84, 0x44, 0xC4, 0x24, 0xA4, 0x64, 0xE4, 0x14, 0x94, 0x54, 0xD4, 0x34, 0xB4, 0x74, 0xF4, // 0x20 47 | 0x0C, 0x8C, 0x4C, 0xCC, 0x2C, 0xAC, 0x6C, 0xEC, 0x1C, 0x9C, 0x5C, 0xDC, 0x3C, 0xBC, 0x7C, 0xFC, // 0x30 48 | 0x02, 0x82, 0x42, 0xC2, 0x22, 0xA2, 0x62, 0xE2, 0x12, 0x92, 0x52, 0xD2, 0x32, 0xB2, 0x72, 0xF2, // 0x40 49 | 0x0A, 0x8A, 0x4A, 0xCA, 0x2A, 0xAA, 0x6A, 0xEA, 0x1A, 0x9A, 0x5A, 0xDA, 0x3A, 0xBA, 0x7A, 0xFA, // 0x50 50 | 0x06, 0x86, 0x46, 0xC6, 0x26, 0xA6, 0x66, 0xE6, 0x16, 0x96, 0x56, 0xD6, 0x36, 0xB6, 0x76, 0xF6, // 0x60 51 | 0x0E, 0x8E, 0x4E, 0xCE, 0x2E, 0xAE, 0x6E, 0xEE, 0x1E, 0x9E, 0x5E, 0xDE, 0x3E, 0xBE, 0x7E, 0xFE, // 0x70 52 | 0x01, 0x81, 0x41, 0xC1, 0x21, 0xA1, 0x61, 0xE1, 0x11, 0x91, 0x51, 0xD1, 0x31, 0xB1, 0x71, 0xF1, // 0x80 53 | 0x09, 0x89, 0x49, 0xC9, 0x29, 0xA9, 0x69, 0xE9, 0x19, 0x99, 0x59, 0xD9, 0x39, 0xB9, 0x79, 0xF9, // 0x90 54 | 0x05, 0x85, 0x45, 0xC5, 0x25, 0xA5, 0x65, 0xE5, 0x15, 0x95, 0x55, 0xD5, 0x35, 0xB5, 0x75, 0xF5, // 0xA0 55 | 0x0D, 0x8D, 0x4D, 0xCD, 0x2D, 0xAD, 0x6D, 0xED, 0x1D, 0x9D, 0x5D, 0xDD, 0x3D, 0xBD, 0x7D, 0xFD, // 0xB0 56 | 0x03, 0x83, 0x43, 0xC3, 0x23, 0xA3, 0x63, 0xE3, 0x13, 0x93, 0x53, 0xD3, 0x33, 0xB3, 0x73, 0xF3, // 0xC0 57 | 0x0B, 0x8B, 0x4B, 0xCB, 0x2B, 0xAB, 0x6B, 0xEB, 0x1B, 0x9B, 0x5B, 0xDB, 0x3B, 0xBB, 0x7B, 0xFB, // 0xD0 58 | 0x07, 0x87, 0x47, 0xC7, 0x27, 0xA7, 0x67, 0xE7, 0x17, 0x97, 0x57, 0xD7, 0x37, 0xB7, 0x77, 0xF7, // 0xE0 59 | 0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF, 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF // 0xF0 60 | }; 61 | 62 | 63 | /* 64 | ------------------------------------------------------------------------------- 65 | ***************************************************** F U N C T I O N S *** 66 | ------------------------------------------------------------------------------- 67 | */ 68 | 69 | /******************************************************************************/ 70 | void memcpy_be(uint8_t* dst, const uint32_t* src, size_t size) 71 | { 72 | uint32_t value = 0; 73 | uint8_t *addr; 74 | for(uint32_t i=0; i 0) 90 | { 91 | valueBitLength -= BITS_IN_BYTE; 92 | reversedValue |= (reverse8BitValueTable[value & 0xFF] << valueBitLength); 93 | value >>= BITS_IN_BYTE; 94 | } 95 | return reversedValue; 96 | } 97 | 98 | /******************************************************************************/ 99 | void reverse_copy(uint8_t* dst, uint64_t value, size_t size) 100 | { 101 | int8_t ix = 1; 102 | do 103 | { 104 | dst[size - ix] = value & 0xFF; 105 | value = value >> (ix * 8); 106 | ix++; 107 | } while (value > 0); 108 | } 109 | -------------------------------------------------------------------------------- /fileutils.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #pragma once 19 | 20 | #ifndef _FILEUTILS_H_ 21 | #define _FILEUTILS_H_ 22 | 23 | /* 24 | ------------------------------------------------------------------------------- 25 | *********************************************** H E A D E R F I L E S *** 26 | ------------------------------------------------------------------------------- 27 | */ 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | /* 34 | ------------------------------------------------------------------------------- 35 | ********************************************************* C L A S S E S *** 36 | ------------------------------------------------------------------------------- 37 | */ 38 | /******************************************************************************/ 39 | class ByteFile 40 | { 41 | public: 42 | ByteFile(const std::string& name) 43 | { 44 | std::ifstream fl(name.c_str(), std::ios::binary); 45 | if (!fl) 46 | { 47 | LOG_ERROR("Cannot read file - %s", name.c_str()); 48 | } 49 | 50 | fl.seekg(0, std::ios::end); 51 | if(!fl) 52 | { 53 | LOG_ERROR("Cannot seek to end of file - %s", name.c_str()); 54 | } 55 | if(fl.bad() || fl.fail()) 56 | { 57 | LOG_ERROR("Cannot seek to end of file - %s", name.c_str()); 58 | } 59 | if(! fl.good()) 60 | { 61 | LOG_ERROR("Cannot seek to end of file - %s", name.c_str()); 62 | } 63 | len = fl.tellg(); 64 | bytes = new uint8_t[len]; 65 | fl.seekg(0, std::ios::beg); 66 | fl.read((char*)bytes, len); 67 | fl.close(); 68 | } 69 | 70 | ~ByteFile() 71 | { 72 | if (bytes) 73 | { 74 | delete [] bytes; 75 | } 76 | bytes = 0; 77 | } 78 | 79 | uint8_t* bytes; 80 | size_t len; 81 | }; 82 | 83 | /******************************************************************************/ 84 | class FileImport 85 | { 86 | public: 87 | bool LoadHexData(const std::string& fileName, uint8_t* dataArray, int count) 88 | { 89 | FILE* f; 90 | int datum, ret, nbytes; 91 | nbytes = 0; 92 | memset(dataArray, 0, count); 93 | f = fopen(fileName.c_str(), "r"); 94 | 95 | if ((ret = fscanf(f, "%2X", &datum)) == EOF) 96 | { 97 | fclose(f); 98 | LOG_ERROR("File is empty - %s", fileName.c_str()); 99 | } 100 | fclose(f); 101 | f = fopen(fileName.c_str(), "r"); 102 | 103 | if(f) 104 | { 105 | while ((ret = fscanf(f, "%2X", &datum)) != EOF) 106 | { 107 | if (ret == 1) 108 | { 109 | if (datum < 0 || datum > 255) 110 | { 111 | fclose(f); 112 | LOG_ERROR("Bad hex character %x in file - %s", datum, fileName.c_str()); 113 | } 114 | if (nbytes == count) 115 | { 116 | fclose(f); 117 | return false; 118 | } 119 | dataArray[nbytes++] = datum; 120 | } 121 | else 122 | { 123 | fclose(f); 124 | LOG_ERROR("Unable to read hex characters from file - %s", fileName.c_str()); 125 | } 126 | } 127 | if (nbytes < count) 128 | { 129 | fclose(f); 130 | return false; 131 | } 132 | } 133 | else 134 | { 135 | LOG_ERROR("Unable to open file - %s", fileName.c_str()); 136 | } 137 | fclose(f); 138 | return true; 139 | } 140 | }; 141 | 142 | class FileUtils 143 | { 144 | public: 145 | static uint32_t GetFileSize(std::string filename) 146 | { 147 | FILE *p_file = NULL; 148 | uint32_t size = 0; 149 | p_file = fopen(filename.c_str(), "rb"); 150 | if (p_file) 151 | { 152 | fseek(p_file, 0, SEEK_END); 153 | size = ftell(p_file); 154 | } 155 | else 156 | { 157 | LOG_ERROR("Unable to open file - %s", filename.c_str()); 158 | } 159 | fclose(p_file); 160 | return size; 161 | } 162 | }; 163 | #endif 164 | -------------------------------------------------------------------------------- /hash.cpp: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | /* 19 | ------------------------------------------------------------------------------- 20 | *********************************************** H E A D E R F I L E S *** 21 | ------------------------------------------------------------------------------- 22 | */ 23 | #include "hash.h" 24 | #include "Keccak-compact-versal.h" 25 | 26 | /* 27 | ------------------------------------------------------------------------------- 28 | ***************************************************** F U N C T I O N S *** 29 | ------------------------------------------------------------------------------- 30 | */ 31 | /******************************************************************************/ 32 | uint8_t HashSha2::GetHashLength(void) 33 | { 34 | return SHA2_LENGTH_BYTES; 35 | } 36 | 37 | /******************************************************************************/ 38 | uint8_t HashSha2::UpdateHash(const void* data, size_t length) 39 | { 40 | uint8_t errorCode = SHA256_Update(&ctx, data, length); 41 | 42 | /* Send the inverted error code, to be consistent with the SHA3 algo */ 43 | return (!errorCode); 44 | } 45 | 46 | /******************************************************************************/ 47 | uint8_t HashSha2::FinalHash(uint8_t* hashout) 48 | { 49 | uint8_t errorCode = SHA256_Final(hashout, &ctx); 50 | 51 | /* Send the inverted error code, to be consistent with the SHA3 algo */ 52 | return (!errorCode); 53 | } 54 | 55 | /******************************************************************************/ 56 | void HashSha2::CalculateHash(bool flag, const uint8_t *data, size_t length, uint8_t* out) 57 | { 58 | SHA256(data, length, out); 59 | } 60 | 61 | /******************************************************************************/ 62 | std::string HashSha2::GetHashFileExtension(void) 63 | { 64 | return ".sha256"; 65 | } 66 | 67 | /******************************************************************************/ 68 | uint8_t HashSha3::GetHashLength(void) 69 | { 70 | return SHA3_LENGTH_BYTES; 71 | } 72 | 73 | /******************************************************************************/ 74 | uint8_t HashSha3::UpdateHash(const void* data, size_t length) 75 | { 76 | size_t length_in_bits = length * 8; 77 | 78 | return Update(&ctx, (BitSequence*)data, length_in_bits); 79 | } 80 | 81 | /******************************************************************************/ 82 | uint8_t HashSha3::FinalHash(uint8_t* hashout) 83 | { 84 | return Final(&ctx, hashout, SHA3_LENGTH_BYTES); 85 | } 86 | 87 | /******************************************************************************/ 88 | void HashSha3::CalculateHash(bool nist, const uint8_t *data, size_t length, uint8_t* out) 89 | { 90 | if (nist) 91 | { 92 | crypto_hash_NIST_SHA3(out, data, length); 93 | } 94 | else 95 | { 96 | crypto_hash(out, data, length); 97 | } 98 | } 99 | 100 | /******************************************************************************/ 101 | void HashSha3::CalculateVersalHash(bool nist, const uint8_t *data, size_t length, uint8_t* out) 102 | { 103 | Versalcrypto_hash(out, data, length, nist); 104 | } 105 | 106 | /******************************************************************************/ 107 | std::string HashSha3::GetHashFileExtension(void) 108 | { 109 | return ".sha384"; 110 | } 111 | -------------------------------------------------------------------------------- /hash.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #ifndef _HASH_H_ 19 | #define _HASH_H_ 20 | 21 | /* 22 | ------------------------------------------------------------------------------- 23 | *********************************************** H E A D E R F I L E S *** 24 | ------------------------------------------------------------------------------- 25 | */ 26 | #include 27 | #include 28 | #include 29 | 30 | #include "Keccak-compact.h" 31 | #include "bootgenenum.h" 32 | 33 | 34 | /* 35 | ------------------------------------------------------------------------------- 36 | ********************************************* P R E P R O C E S S O R S *** 37 | ------------------------------------------------------------------------------- 38 | */ 39 | #define SHA2_LENGTH_BYTES 32 40 | #define SHA3_LENGTH_BYTES 48 41 | 42 | 43 | /* 44 | ------------------------------------------------------------------------------- 45 | ********************************************************* C L A S S E S *** 46 | ------------------------------------------------------------------------------- 47 | */ 48 | /******************************************************************************/ 49 | class Hash 50 | { 51 | public: 52 | Hash() 53 | : hashLength(0) 54 | { }; 55 | 56 | virtual ~Hash() {} 57 | 58 | virtual void InitHash(void) = 0; 59 | virtual uint8_t GetHashLength(void) = 0; 60 | virtual uint8_t UpdateHash(const void*, size_t) = 0; 61 | virtual uint8_t FinalHash(uint8_t*) = 0; 62 | virtual void CalculateHash(bool flag, const uint8_t*, size_t, uint8_t*) = 0; 63 | virtual void CalculateVersalHash(bool flag, const uint8_t*, size_t, uint8_t*) {} 64 | virtual std::string GetHashFileExtension(void) = 0; 65 | 66 | protected: 67 | uint8_t hashLength; 68 | }; 69 | 70 | /******************************************************************************/ 71 | class HashSha2 : public Hash 72 | { 73 | public: 74 | HashSha2() 75 | { 76 | ctx.md_len = 0; 77 | ctx.num = 0; 78 | ctx.Nl = 0; 79 | ctx.Nh = 0; 80 | } 81 | 82 | ~HashSha2() {} 83 | 84 | uint8_t GetHashLength(void); 85 | void InitHash(void) { SHA256_Init(&ctx); } 86 | uint8_t UpdateHash(const void*, size_t); 87 | uint8_t FinalHash(uint8_t*); 88 | void CalculateHash(bool flag, const uint8_t*, size_t, uint8_t*); 89 | std::string GetHashFileExtension(void); 90 | 91 | private: 92 | SHA256_CTX ctx; 93 | }; 94 | 95 | /******************************************************************************/ 96 | class HashSha3 : public Hash 97 | { 98 | public: 99 | HashSha3() 100 | { 101 | ctx.bitsInQueue = 0; 102 | } 103 | 104 | ~HashSha3() {} 105 | 106 | uint8_t GetHashLength(void); 107 | void InitHash(void) { Init(&ctx); } 108 | uint8_t UpdateHash(const void*, size_t); 109 | uint8_t FinalHash(uint8_t*); 110 | void CalculateHash(bool flag, const uint8_t*, size_t, uint8_t*); 111 | void CalculateVersalHash(bool flag, const uint8_t*, size_t, uint8_t*); 112 | std::string GetHashFileExtension(void); 113 | 114 | private: 115 | hashState ctx; 116 | }; 117 | 118 | #endif 119 | -------------------------------------------------------------------------------- /imageheadertable-versal.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Xilinx/bootgen/4f023ac2ca2ea452740194045fcecf1671f5b2c0/imageheadertable-versal.cpp -------------------------------------------------------------------------------- /imageheadertable-zynq.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #pragma once 19 | 20 | #ifndef _IMAGEHEADERTABLE_ZYNQ_H_ 21 | #define _IMAGEHEADERTABLE_ZYNQ_H_ 22 | 23 | /* 24 | ------------------------------------------------------------------------------- 25 | *********************************************** H E A D E R F I L E S *** 26 | ------------------------------------------------------------------------------- 27 | */ 28 | #include "imageheadertable.h" 29 | #include "partitionheadertable-zynq.h" 30 | #include "bootimage.h" 31 | #include "fileutils.h" 32 | #include "elftools.h" 33 | #include "bitutils.h" 34 | 35 | 36 | /* 37 | ------------------------------------------------------------------------------- 38 | ********************************************* P R E P R O C E S S O R S *** 39 | ------------------------------------------------------------------------------- 40 | */ 41 | #define MAX_IHT_RESERVED_ZYNQ 11 42 | #define MAX_NUM_PARTITIONS_ZYNQ 14 43 | 44 | /* 32MB Linear QSPI range */ 45 | #define LQSPI_BASE_ADDR_ZYNQ (0xFC000000) 46 | #define LQSPI_SIZE_ZYNQ (0x2000000) 47 | #define LQSPI_RANGE_MASK_ZYNQ (0x1FFFFFF) 48 | 49 | /* 32MB + 32MB SMC (NOR0 + NOR1) ranges */ 50 | #define NOR_BASE_ADDR_ZYNQ (0xE2000000) 51 | #define NOR_SIZE_ZYNQ (0x4000000) 52 | #define NOR_RANGE_MASK_ZYNQ (0x3FFFFFF) 53 | 54 | /* 55 | ------------------------------------------------------------------------------- 56 | *************************************************** S T R U C T U R E S *** 57 | ------------------------------------------------------------------------------- 58 | */ 59 | typedef struct 60 | { 61 | uint32_t version; // 0x00 62 | uint32_t partitionTotalCount; // 0x04 63 | uint32_t firstPartitionHeaderWordOffset; // 0x08 64 | uint32_t firstImageHeaderWordOffset; // 0x0C 65 | uint32_t headerAuthCertificateWordOffset; // 0x10 66 | uint32_t reserved[MAX_IHT_RESERVED_ZYNQ]; // 0x14 67 | } ZynqImageHeaderTableStructure; 68 | 69 | typedef struct 70 | { 71 | uint32_t nextImageHeaderWordOffset; // 0x00 72 | uint32_t partitionHeaderWordOffset; // 0x04 73 | uint32_t dataSectionCount; // 0x08 74 | uint32_t imageNameLength; // 0x0C 75 | char imageName[1]; // 0x10 76 | } ZynqImageHeaderStructure; 77 | 78 | 79 | /* 80 | ------------------------------------------------------------------------------- 81 | ********************************************************* C L A S S E S *** 82 | ------------------------------------------------------------------------------- 83 | */ 84 | /******************************************************************************/ 85 | class ZynqImageHeaderTable : public ImageHeaderTable 86 | { 87 | public: 88 | ZynqImageHeaderTable(); 89 | ZynqImageHeaderTable(std::ifstream& ifs); 90 | ~ZynqImageHeaderTable(); 91 | 92 | void ValidateSecurityCombinations(Authentication::Type, Encryption::Type, Checksum::Type) {}; 93 | void RealignSectionDataPtr(void); 94 | uint8_t GetMaxNumOfPartitions(void) { return MAX_NUM_PARTITIONS_ZYNQ; } 95 | 96 | void SetImageHeaderTableVersion(uint32_t version); 97 | void SetPartitionCount(uint32_t count); 98 | void SetFirstPartitionHeaderOffset(uint32_t offset); 99 | void SetFirstImageHeaderOffset(uint32_t offset); 100 | void SetHeaderAuthCertificateOffset(uint32_t offset); 101 | void SetReservedFields(void); 102 | void SetChecksum(void) {} 103 | void SetBootDevice(BootDevice::Type type) {} 104 | 105 | uint32_t GetImageHeaderTableVersion(void); 106 | uint32_t GetPartitionCount(void); 107 | uint32_t GetFirstPartitionHeaderOffset(void); 108 | uint32_t GetFirstImageHeaderOffset(void); 109 | uint32_t GetHeaderAuthCertificateOffset(void); 110 | 111 | private: 112 | ZynqImageHeaderTableStructure *iHTable; 113 | }; 114 | 115 | /******************************************************************************/ 116 | class ZynqImageHeader : public ImageHeader 117 | { 118 | public: 119 | ZynqImageHeader(std::string& filename); 120 | ZynqImageHeader(std::ifstream& f); 121 | ~ZynqImageHeader(); 122 | 123 | void Build(BootImage& bi, Binary& cache); 124 | void Link(BootImage &bi, PartitionHeader* partitionHeader, ImageHeader* nextImageheader); 125 | void ValidateEncryptionAttributes(BootImage & bi); 126 | void ImportFpgaDataFile(BootImage & bi); 127 | void ImportBit(BootImage & bi); 128 | void ImportBin(BootImage & bi); 129 | void CreateElfPartitions(BootImage& bi, ElfFormat* elf, uint8_t proc_state); 130 | uint64_t GetExecAddrForXip(uint64_t execAddr); 131 | 132 | void SetNextImageHeaderOffset(uint32_t addr); 133 | void SetPartitionHeaderOffset(uint32_t addr); 134 | void SetDataSectionCount(uint32_t cnt); 135 | void SetImageNameLength(uint32_t len); 136 | void SetImageName(void); 137 | 138 | Binary::Address_t GetNextImageHeaderOffset(); 139 | uint32_t GetPartitionHeaderOffset(void); 140 | uint32_t GetDataSectionCount(void); 141 | uint32_t GetImageNameLength(void); 142 | 143 | private: 144 | ZynqImageHeaderStructure *imageHeader; 145 | }; 146 | #endif 147 | -------------------------------------------------------------------------------- /kdf.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | ******************************************************************************/ 16 | 17 | /***************************************************************************** 18 | * 19 | * This source contains the implementation of the interface functions for secure 20 | * library. 21 | * 22 | * MODIFICATION HISTORY: 23 | * 24 | * Ver Who Date Changes 25 | * ----- --- -------- ------------------------------------------------------- 26 | * 1.0 Ramya 08/27/2021 Initial release 27 | * 28 | ******************************************************************************/ 29 | 30 | #pragma once 31 | 32 | #ifndef _KDF_H_ 33 | #define _KDF_H_ 34 | 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | 41 | #define VERSION "v1.0" 42 | 43 | #define BYTES_PER_AES_SEED 32 44 | #define KDF_COUNTER_BYTES 4 45 | 46 | /* Error Codes */ 47 | #define KDF_INPUT_FID_UNAVAILABLE 0x00000001 48 | #define KDF_INPUT_SEED_UNAVAILABLE 0x00000002 49 | 50 | #define KDF_CAVP_TESTFILE_UNAVAILABLE 0x00010001 51 | #define KDF_CAVP_TESTFILE_IMPROPER 0x00010002 52 | #define KDF_CAVP_KI_LENGTH_IMPROPER 0x00010003 53 | #define KDF_CAVP_HEXKEY_IMPROPER 0x00010004 54 | #define KDF_CAVP_VERIFY_KO_FAILED 0x00010005 55 | #define KDF_CAVP_FID_LENGTH_IMPROPER 0x00010006 56 | 57 | /********************************************************************************/ 58 | class Kdf 59 | { 60 | public: 61 | Kdf() 62 | : key_out_length(0) 63 | , key_seed(NULL) 64 | , fixed_input_data_byte_length(0) 65 | , fixed_input_data(NULL) 66 | , verify_ko(NULL) 67 | , key_out(NULL) 68 | , kdf_log_file("") 69 | { }; 70 | 71 | ~Kdf() 72 | { 73 | if (key_seed != NULL) 74 | { 75 | delete[] key_seed; 76 | } 77 | if (fixed_input_data != NULL) 78 | { 79 | delete[] fixed_input_data; 80 | } 81 | if (verify_ko != NULL) 82 | { 83 | delete[] verify_ko; 84 | } 85 | if(key_out != NULL) 86 | { 87 | delete[] key_out; 88 | } 89 | }; 90 | 91 | uint32_t CounterModeKDF(uint32_t* k_in, uint32_t* fid, uint32_t fid_byte_length, uint32_t* ko_buf, uint32_t ko_bytes, bool reset_cntr = true); 92 | uint32_t CAVPonCounterModeKDF(std::string filename); 93 | void ResetKdfCounter(); 94 | void SetKdfLogFilename(std::string filename); 95 | 96 | std::string GetKdfLogFilename(void); 97 | std::string GetVersion(void); 98 | 99 | private: 100 | uint8_t* key_out; 101 | uint8_t* key_seed; 102 | uint32_t key_out_length; 103 | uint8_t* verify_ko; 104 | uint8_t* fixed_input_data; 105 | uint32_t fixed_input_data_byte_length; 106 | uint32_t version; 107 | uint8_t kdf_counter[KDF_COUNTER_BYTES]; 108 | std::string kdf_log_file; 109 | 110 | void KDF(uint32_t ko_bytes, bool reset_cntr = true); 111 | uint32_t ParseKDFTestVectorFile(std::string filename); 112 | bool IsKdfCounterMax(); 113 | }; 114 | 115 | #endif 116 | -------------------------------------------------------------------------------- /link.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | /******************************************************************************* 3 | * Copyright (c) 2007-2018 Wind River Systems, Inc. and others. 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * The Eclipse Public License is available at 8 | * http://www.eclipse.org/legal/epl-v10.html 9 | * and the Eclipse Distribution License is available at 10 | * http://www.eclipse.org/org/documents/edl-v10.php. 11 | * You may elect to redistribute this code under either of these licenses. 12 | * 13 | * Contributors: 14 | * Wind River Systems - initial API and implementation 15 | *******************************************************************************/ 16 | 17 | /* 18 | * Double linked list support. 19 | */ 20 | 21 | #ifndef D_link 22 | #define D_link 23 | 24 | #include 25 | 26 | typedef struct LINK LINK; 27 | 28 | struct LINK { 29 | LINK * next; 30 | LINK * prev; 31 | }; 32 | 33 | 34 | #define TCF_LIST_INIT(list) {&(list), &(list)} 35 | 36 | #define list_init(list) { \ 37 | (list)->next = (list)->prev = (list); \ 38 | } 39 | 40 | #define list_is_empty(list) ((list)->next == (list) || (list)->next == NULL) 41 | 42 | #define list_remove(item) { \ 43 | LINK * _l_ = item; \ 44 | _l_->prev->next = _l_->next; \ 45 | _l_->next->prev = _l_->prev; \ 46 | _l_->next = _l_->prev = _l_; \ 47 | } 48 | 49 | #define list_add_first(item, list) { \ 50 | (item)->next = (list)->next; (item)->prev = (list); \ 51 | (list)->next->prev = (item); (list)->next = (item); \ 52 | } 53 | 54 | #define list_add_last(item, list) { \ 55 | (item)->next = (list); (item)->prev = (list)->prev; \ 56 | (list)->prev->next = (item); (list)->prev = (item); \ 57 | } 58 | 59 | #define list_concat(item, list) { \ 60 | if (!list_is_empty(list)) { \ 61 | (item)->prev->next = (list)->next; \ 62 | (list)->next->prev = (item)->prev; \ 63 | (item)->prev = (list)->prev; \ 64 | (list)->prev->next = (item); \ 65 | } \ 66 | } 67 | 68 | #define list_foreach(trav, list) \ 69 | for (trav = (list)->next; trav != list; trav = trav->next) 70 | 71 | #define list_item_type(item, type, link) \ 72 | ((type *)((char *)(item) - offsetof(type, link))) 73 | 74 | #endif /* D_link */ 75 | -------------------------------------------------------------------------------- /location.hh: -------------------------------------------------------------------------------- 1 | /* A Bison parser, made by GNU Bison 2.7. */ 2 | 3 | /* Locations for Bison parsers in C++ 4 | 5 | Copyright (C) 2002-2007, 2009-2012 Free Software Foundation, Inc. 6 | 7 | This program is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This program is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this program. If not, see . */ 19 | 20 | /* As a special exception, you may create a larger work that contains 21 | part or all of the Bison parser skeleton and distribute that work 22 | under terms of your choice, so long as that work isn't itself a 23 | parser generator using the skeleton or a modified version thereof 24 | as a parser skeleton. Alternatively, if you modify or redistribute 25 | the parser skeleton itself, you may (at your option) remove this 26 | special exception, which will cause the skeleton and the resulting 27 | Bison output files to be licensed under the GNU General Public 28 | License without this special exception. 29 | 30 | This special exception was added by the Free Software Foundation in 31 | version 2.2 of Bison. */ 32 | 33 | /** 34 | ** \file ../bisonflex/location.hh 35 | ** Define the BIF::location class. 36 | */ 37 | 38 | #ifndef YY_YY_BISONFLEX_LOCATION_HH_INCLUDED 39 | # define YY_YY_BISONFLEX_LOCATION_HH_INCLUDED 40 | 41 | # include "position.hh" 42 | 43 | /* Line 166 of location.cc */ 44 | #line 24 "../s/bif.y" 45 | namespace BIF { 46 | /* Line 166 of location.cc */ 47 | #line 48 "../bisonflex/location.hh" 48 | 49 | /// Abstract a location. 50 | class location 51 | { 52 | public: 53 | 54 | /// Construct a location from \a b to \a e. 55 | location (const position& b, const position& e) 56 | : begin (b) 57 | , end (e) 58 | { 59 | } 60 | 61 | /// Construct a 0-width location in \a p. 62 | explicit location (const position& p = position ()) 63 | : begin (p) 64 | , end (p) 65 | { 66 | } 67 | 68 | /// Construct a 0-width location in \a f, \a l, \a c. 69 | explicit location (std::string* f, 70 | unsigned int l = 1u, 71 | unsigned int c = 1u) 72 | : begin (f, l, c) 73 | , end (f, l, c) 74 | { 75 | } 76 | 77 | 78 | /// Initialization. 79 | void initialize (std::string* f = YY_NULL, 80 | unsigned int l = 1u, 81 | unsigned int c = 1u) 82 | { 83 | begin.initialize (f, l, c); 84 | end = begin; 85 | } 86 | 87 | /** \name Line and Column related manipulators 88 | ** \{ */ 89 | public: 90 | /// Reset initial location to final location. 91 | void step () 92 | { 93 | begin = end; 94 | } 95 | 96 | /// Extend the current location to the COUNT next columns. 97 | void columns (unsigned int count = 1) 98 | { 99 | end += count; 100 | } 101 | 102 | /// Extend the current location to the COUNT next lines. 103 | void lines (unsigned int count = 1) 104 | { 105 | end.lines (count); 106 | } 107 | /** \} */ 108 | 109 | 110 | public: 111 | /// Beginning of the located region. 112 | position begin; 113 | /// End of the located region. 114 | position end; 115 | }; 116 | 117 | /// Join two location objects to create a location. 118 | inline const location operator+ (const location& begin, const location& end) 119 | { 120 | location res = begin; 121 | res.end = end.end; 122 | return res; 123 | } 124 | 125 | /// Add two location objects. 126 | inline const location operator+ (const location& begin, unsigned int width) 127 | { 128 | location res = begin; 129 | res.columns (width); 130 | return res; 131 | } 132 | 133 | /// Add and assign a location. 134 | inline location& operator+= (location& res, unsigned int width) 135 | { 136 | res.columns (width); 137 | return res; 138 | } 139 | 140 | /// Compare two location objects. 141 | inline bool 142 | operator== (const location& loc1, const location& loc2) 143 | { 144 | return loc1.begin == loc2.begin && loc1.end == loc2.end; 145 | } 146 | 147 | /// Compare two location objects. 148 | inline bool 149 | operator!= (const location& loc1, const location& loc2) 150 | { 151 | return !(loc1 == loc2); 152 | } 153 | 154 | /** \brief Intercept output stream redirection. 155 | ** \param ostr the destination output stream 156 | ** \param loc a reference to the location to redirect 157 | ** 158 | ** Avoid duplicate information. 159 | */ 160 | template 161 | inline std::basic_ostream& 162 | operator<< (std::basic_ostream& ostr, const location& loc) 163 | { 164 | position last = loc.end - 1; 165 | ostr << loc.begin; 166 | if (last.filename 167 | && (!loc.begin.filename 168 | || *loc.begin.filename != *last.filename)) 169 | ostr << '-' << last; 170 | else if (loc.begin.line != last.line) 171 | ostr << '-' << last.line << '.' << last.column; 172 | else if (loc.begin.column != last.column) 173 | ostr << '-' << last.column; 174 | return ostr; 175 | } 176 | 177 | /* Line 296 of location.cc */ 178 | #line 24 "../s/bif.y" 179 | } // BIF 180 | /* Line 296 of location.cc */ 181 | #line 182 "../bisonflex/location.hh" 182 | 183 | #endif /* !YY_YY_BISONFLEX_LOCATION_HH_INCLUDED */ 184 | -------------------------------------------------------------------------------- /logger.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #ifndef _LOGGER_H_ 19 | #define _LOGGER_H_ 20 | 21 | 22 | /* 23 | ------------------------------------------------------------------------------- 24 | *********************************************** H E A D E R F I L E S *** 25 | ------------------------------------------------------------------------------- 26 | */ 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #include "stringutils.h" 33 | #include "bootgenenum.h" 34 | #include "bootgenexception.h" 35 | #include "binary.h" 36 | 37 | 38 | /* 39 | ------------------------------------------------------------------------------- 40 | ********************************************* P R E P R O C E S S O R S *** 41 | ------------------------------------------------------------------------------- 42 | */ 43 | #define DEBUG_STAMP __FILE__,__LINE__ 44 | #define LOG_LEVEL(a) Logger::Instance()->SetLogLevel(a) 45 | #define LOG_ERROR Logger::Instance()->Error 46 | #define LOG_WARNING Logger::Instance()->Warning 47 | #define LOG_INFO Logger::Instance()->Info 48 | #define LOG_DEBUG Logger::Instance()->Debug 49 | #define LOG_TRACE Logger::Instance()->Trace 50 | #define LOG_DUMP_IMAGE Logger::Instance()->DumpBinaryImage 51 | #define LOG_DUMP_BYTES Logger::Instance()->DumpBytes 52 | #define LOG_OUT Logger::Instance()->Out 53 | #define LOG_MSG Logger::Instance()->Message 54 | #define GET_LOG_LEVEL Logger::Instance()->GetLogLevel 55 | 56 | struct LogLevel 57 | { 58 | typedef enum 59 | { 60 | ERROR, 61 | WARNING, 62 | INFO, 63 | DEBUG, 64 | TRACE, 65 | } Type; 66 | }; 67 | 68 | 69 | /* 70 | ------------------------------------------------------------------------------- 71 | ********************************************************* C L A S S E S *** 72 | ------------------------------------------------------------------------------- 73 | */ 74 | /******************************************************************************/ 75 | class Logger 76 | { 77 | public: 78 | static Logger* Instance(void); 79 | void Error(const char* format, ...); 80 | void Warning(const char* format, ...); 81 | void Info(const char* format, ...); 82 | void Debug(std::string filename, int line, const char* format, ...); 83 | void Trace(const char* format, ...); 84 | void DumpBinaryImage(Binary &cache); 85 | void DumpBytes(uint8_t* ptr, int count); 86 | void Out(std::string stringout); 87 | void Message(const char* format, ...); 88 | void SetLogLevel(LogLevel::Type level); 89 | LogLevel::Type GetLogLevel(void); 90 | 91 | private: 92 | Logger() 93 | { 94 | log_level = LogLevel::WARNING; 95 | } 96 | ~Logger() { }; 97 | std::string msg; 98 | std::ofstream outFile; 99 | LogLevel::Type log_level; 100 | static Logger* logger; 101 | }; 102 | 103 | #endif 104 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | /* 19 | ------------------------------------------------------------------------------- 20 | *********************************************** H E A D E R F I L E S *** 21 | ------------------------------------------------------------------------------- 22 | */ 23 | #include 24 | #include 25 | #include 26 | #include 27 | 28 | #include "bootimage.h" 29 | #include "bootgenexception.h" 30 | #include "options.h" 31 | #include "bifoptions.h" 32 | #include "stringutils.h" 33 | #include "version.h" 34 | 35 | 36 | #include "encryption-zynqmp.h" 37 | 38 | #ifdef _WIN32 39 | #include "openssl/ms/applink.c" 40 | #endif 41 | static const char* time_stamp = __TIME__; 42 | static const char* date_stamp = __DATE__; 43 | 44 | 45 | /* 46 | ------------------------------------------------------------------------------- 47 | ***************************************************** F U N C T I O N S *** 48 | ------------------------------------------------------------------------------- 49 | */ 50 | /******************************************************************************/ 51 | static void DisplayBanner() 52 | { 53 | char version[64], *year; 54 | strncpy(version, RDI_VERSION, sizeof version); 55 | version[sizeof version - 1] = '\0'; 56 | year = strtok(version, "."); 57 | LOG_MSG("\n"); 58 | LOG_MSG("****** %s v%s", PROGRAMNAME, RDI_VERSION); 59 | LOG_MSG(" **** Build date : %s-%s", date_stamp, time_stamp); 60 | LOG_MSG(" ** Copyright 1986-2022 Xilinx, Inc. All Rights Reserved."); 61 | LOG_MSG(" ** Copyright 2022-%s Advanced Micro Devices, Inc. All Rights Reserved.\n", year ? year : "2023"); 62 | } 63 | 64 | /******************************************************************************/ 65 | class BootGenApp 66 | { 67 | public: 68 | void Run(int argc, const char* argv[]) 69 | { 70 | Options options; 71 | options.ParseArgs(argc, argv); 72 | options.ProcessVerifyKDF(); 73 | options.ProcessReadImage(); 74 | std::string bifFile = options.GetBifFilename(); 75 | LOG_TRACE("BIF File: %s", bifFile.c_str()); 76 | 77 | if (bifFile.length() > 0) 78 | { 79 | BIF_File bif(bifFile); 80 | bif.Process(options); 81 | LOG_MSG("\n[INFO] : Bootimage generated successfully\n"); 82 | } 83 | } 84 | }; 85 | 86 | /******************************************************************************/ 87 | int main(int argc, const char* argv[]) 88 | { 89 | try 90 | { 91 | BootGenApp app; 92 | DisplayBanner(); 93 | app.Run(argc,argv); 94 | return 0; 95 | } 96 | catch(std::exception& ex) 97 | { 98 | std::cerr << "[ERROR] : " << ex.what() << std::endl; 99 | return 1; 100 | } 101 | catch(const char* msg) 102 | { 103 | std::cerr << "FATAL: Internal Assertion: " << msg << std::endl; 104 | return 2; 105 | } 106 | catch(...) 107 | { 108 | std::cerr << "FATAL: Unknown Exception caught."<< std::endl; 109 | return 3; 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /obfskutil.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2020 Xilinx, Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | ******************************************************************************/ 16 | 17 | #ifndef isl_iostreams_util2_hpp 18 | #define isl_iostreams_util2_hpp 19 | #if defined(_MSC_VER) && (_MSC_VER >= 1020) 20 | # pragma once 21 | #endif 22 | 23 | #ifdef _MSC_VER 24 | # define ISL_HAS_DECLSPEC 25 | # define ISL_MSVC 26 | #endif 27 | 28 | #define yellow (unsigned) 29 | #define lemon const char 30 | #define green (signed) 31 | #define plum (char) 32 | #define brown (int) 33 | #define pink (const) 34 | #define black struct 35 | #define red const unsigned char 36 | #define white (const unsigned char *) 37 | #define coral a kind of red 38 | #define orchid const unsigned char 39 | #define orange black 40 | #define salmon a kind of orange 41 | #define purple black 42 | #define crimson const char 43 | #define tomato (const char *) 44 | 45 | 46 | #ifdef ISL_HAS_DECLSPEC 47 | # ifdef ISL_IOSTREAMS_SOURCE 48 | # define ISL_IOSTREAMS_DECL __declspec(dllexport) 49 | # else 50 | # define ISL_IOSTREAMS_DECL __declspec(dllimport) 51 | # endif 52 | # else 53 | # define ISL_IOSTREAMS_DECL __attribute__((visibility("default"))) 54 | # define ISL_IOSTREAMS_LOCAL __attribute__ ((visibility("hidden"))) 55 | #endif 56 | purple i0{red *i1; orchid *i2;i0(orchid *i1,red *i2,lemon *i3,crimson *i4):i1(i1),i2(i2),i3(i3),i4(i4){}crimson *i3; lemon *i4;}; 57 | #ifndef ISL_IOSTREAMS_DECL 58 | # define ISL_IOSTREAMS_DECL 59 | #endif 60 | 61 | #ifndef ISL_IOSTREAMS_LOCAL 62 | # define ISL_IOSTREAMS_LOCAL 63 | #endif 64 | 65 | #define test i0 66 | #define test1 i1 67 | #define test2 i2 68 | #define test3 i3 69 | #define test4 i4 70 | #define obfs test 71 | ISL_IOSTREAMS_DECL 72 | void obfsk(void *); 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /parsing.cpp: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | /* 19 | ------------------------------------------------------------------------------- 20 | *********************************************** H E A D E R F I L E S *** 21 | ------------------------------------------------------------------------------- 22 | */ 23 | #include 24 | #include "parsing.h" 25 | 26 | 27 | /* 28 | ------------------------------------------------------------------------------- 29 | ***************************************************** F U N C T I O N S *** 30 | ------------------------------------------------------------------------------- 31 | */ 32 | /******************************************************************************/ 33 | void Parsing::Error(const BIF::BisonParser::location_type &loc, const std::string &msg) 34 | { 35 | std::cerr << "\nERROR:BootGen - " << msg << "\n"; 36 | std::cerr << " Line #" << loc.begin.line << ", \"" << *loc.begin.filename << "\".\n"; 37 | 38 | std::string linedata; 39 | { 40 | std::ifstream echo(loc.begin.filename->c_str()); 41 | for(unsigned int line=0;line 10) 55 | { 56 | std::cerr << "... "; 57 | first = loc.begin.column - 10; 58 | } 59 | else 60 | { 61 | first = 1; 62 | } 63 | int len = loc.end.column - loc.begin.column; 64 | if (len > 70) 65 | { 66 | len = 70; 67 | } 68 | std::cerr << linedata.substr(first-1,70) << "\n"; 69 | unsigned int i; 70 | if (first != 1) 71 | { 72 | std::cerr << " "; 73 | } 74 | for(i=first;i 30 | 31 | /* 32 | ------------------------------------------------------------------------------- 33 | ********************************************************* C L A S S E S *** 34 | ------------------------------------------------------------------------------- 35 | */ 36 | /******************************************************************************/ 37 | class Parsing 38 | { 39 | public: 40 | static void Error(const BIF::BisonParser::location_type &loc, const std::string &msg); 41 | static void Error2(const BIF::BisonParser::location_type &loc,const std::string& linedata); 42 | }; 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /partitionheadertable-zynq.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #pragma once 19 | 20 | #ifndef _PARTITIONHEADERTABLE_ZYNQ_H_ 21 | #define _PARTITIONHEADERTABLE_ZYNQ_H_ 22 | 23 | 24 | /* 25 | ------------------------------------------------------------------------------- 26 | *********************************************** H E A D E R F I L E S *** 27 | ------------------------------------------------------------------------------- 28 | */ 29 | #include "partitionheadertable.h" 30 | #include "imageheadertable-zynq.h" 31 | #include "checksum.h" 32 | 33 | /* 34 | ------------------------------------------------------------------------------- 35 | ********************************************* P R E P R O C E S S O R S *** 36 | ------------------------------------------------------------------------------- 37 | */ 38 | #define PHT_RESERVED 4 39 | 40 | #define PH_TAIL_ALIGN_SHIFT 0 41 | #define PH_TAIL_ALIGN_MASK 0x3 42 | #define PH_HEAD_ALIGN_SHIFT 2 43 | #define PH_HEAD_ALIGN_MASK 0x3 44 | #define PH_DEST_DEVICE_SHIFT 4 45 | #define PH_DEST_DEVICE_MASK 0xF 46 | #define PH_CHECKSUM_SHIFT 12 47 | #define PH_CHECKSUM_MASK 0x7 48 | #define PH_AC_FLAG_SHIFT 15 49 | #define PH_AC_FLAG_MASK 0x1 50 | #define PH_OWNER_SHIFT 16 51 | #define PH_OWNER_MASK 0x3 52 | 53 | 54 | /* 55 | ------------------------------------------------------------------------------- 56 | *************************************************** S T R U C T U R E S *** 57 | ------------------------------------------------------------------------------- 58 | */ 59 | typedef struct 60 | { 61 | uint32_t encryptedPartitionLength; // 0x00 62 | uint32_t unencryptedPartitionLength; // 0x04 63 | uint32_t totalPartitionLength; // 0x08 64 | uint32_t destinationLoadAddress; // 0x0C 65 | uint32_t destinationExecAddress; // 0x10 66 | uint32_t partitionWordOffset; // 0x14 67 | uint32_t partitionAttributes; // 0x18 68 | uint32_t dataSectionCount; // 0x1C 69 | uint32_t checksumWordOffset; // 0x20 70 | uint32_t imageHeaderWordOffset; // 0x24 71 | uint32_t authCertificateOffset; // 0x28 72 | uint32_t reserved[PHT_RESERVED]; // 0x2C 73 | uint32_t pHChecksum; // 0x3C 74 | } ZynqPartitionHeaderTableStructure; 75 | 76 | 77 | /* 78 | ------------------------------------------------------------------------------- 79 | ********************************************************* C L A S S E S *** 80 | ------------------------------------------------------------------------------- 81 | */ 82 | class ZynqPartitionHeader : public PartitionHeader 83 | { 84 | public: 85 | ZynqPartitionHeader(ImageHeader* imageheader, int index0); 86 | ~ZynqPartitionHeader(); 87 | 88 | void ReadHeader(std::ifstream& ifs); 89 | void ReadData(std::ifstream& ifs); 90 | 91 | void SetEncryptedPartitionLength(uint32_t len); 92 | void SetUnencryptedPartitionLength(uint32_t len); 93 | void SetTotalPartitionLength(uint32_t len); 94 | void SetLoadAddress(uint64_t addr, bool arch); 95 | void SetExecAddress(uint64_t addr); 96 | void SetPartitionWordOffset(uint32_t addr); 97 | void SetPartitionAttributes(void); 98 | void SetSectionCount(uint32_t cnt); 99 | void SetChecksumOffset(void); 100 | void SetImageHeaderOffset(uint32_t offset); 101 | void SetAuthCertificateOffset(void); 102 | void SetChecksum(void); 103 | void SetReserved(void); 104 | void SetBitLoadAddress(bool load, uint32_t val); 105 | void RealignSectionDataPtr(void); 106 | bool IsBootloader(void) { return isBootloader; } 107 | 108 | uint8_t GetDestinationDevice(void); 109 | uint8_t GetHeadAlignment(void); 110 | uint8_t GetTailAlignment(void); 111 | uint8_t GetAuthCertFlag(void); 112 | uint8_t GetChecksumType(void); 113 | uint8_t GetOwnerType(void); 114 | uint32_t GetPartitionHeaderSize(void); 115 | uint32_t GetChecksum(void); 116 | uint32_t GetExecAddress(void); 117 | uint32_t GetLoadAddress(void); 118 | uint32_t GetEncryptedPartitionLength(void); 119 | uint32_t GetUnencryptedPartitionLength(void); 120 | uint32_t GetTotalPartitionLength(void); 121 | uint32_t GetAuthCertificateOffset(void); 122 | uint32_t GetPartitionWordOffset(void); 123 | 124 | bool IsFirstPartitionInImage() 125 | { 126 | return index == 0; 127 | } 128 | 129 | private: 130 | ZynqPartitionHeaderTableStructure* pHTable; 131 | }; 132 | #endif 133 | -------------------------------------------------------------------------------- /position.hh: -------------------------------------------------------------------------------- 1 | /* A Bison parser, made by GNU Bison 2.7. */ 2 | 3 | /* Positions for Bison parsers in C++ 4 | 5 | Copyright (C) 2002-2007, 2009-2012 Free Software Foundation, Inc. 6 | 7 | This program is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This program is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this program. If not, see . */ 19 | 20 | /* As a special exception, you may create a larger work that contains 21 | part or all of the Bison parser skeleton and distribute that work 22 | under terms of your choice, so long as that work isn't itself a 23 | parser generator using the skeleton or a modified version thereof 24 | as a parser skeleton. Alternatively, if you modify or redistribute 25 | the parser skeleton itself, you may (at your option) remove this 26 | special exception, which will cause the skeleton and the resulting 27 | Bison output files to be licensed under the GNU General Public 28 | License without this special exception. 29 | 30 | This special exception was added by the Free Software Foundation in 31 | version 2.2 of Bison. */ 32 | 33 | /** 34 | ** \file ../bisonflex/position.hh 35 | ** Define the BIF::position class. 36 | */ 37 | 38 | #ifndef YY_YY_BISONFLEX_POSITION_HH_INCLUDED 39 | # define YY_YY_BISONFLEX_POSITION_HH_INCLUDED 40 | 41 | # include // std::max 42 | # include 43 | # include 44 | 45 | # ifndef YY_NULL 46 | # if defined __cplusplus && 201103L <= __cplusplus 47 | # define YY_NULL nullptr 48 | # else 49 | # define YY_NULL 0 50 | # endif 51 | # endif 52 | 53 | /* Line 36 of location.cc */ 54 | #line 24 "../s/bif.y" 55 | namespace BIF { 56 | /* Line 36 of location.cc */ 57 | #line 58 "../bisonflex/position.hh" 58 | /// Abstract a position. 59 | class position 60 | { 61 | public: 62 | 63 | /// Construct a position. 64 | explicit position (std::string* f = YY_NULL, 65 | unsigned int l = 1u, 66 | unsigned int c = 1u) 67 | : filename (f) 68 | , line (l) 69 | , column (c) 70 | { 71 | } 72 | 73 | 74 | /// Initialization. 75 | void initialize (std::string* fn = YY_NULL, 76 | unsigned int l = 1u, 77 | unsigned int c = 1u) 78 | { 79 | filename = fn; 80 | line = l; 81 | column = c; 82 | } 83 | 84 | /** \name Line and Column related manipulators 85 | ** \{ */ 86 | /// (line related) Advance to the COUNT next lines. 87 | void lines (int count = 1) 88 | { 89 | column = 1u; 90 | line += count; 91 | } 92 | 93 | /// (column related) Advance to the COUNT next columns. 94 | void columns (int count = 1) 95 | { 96 | column = std::max (1u, column + count); 97 | } 98 | /** \} */ 99 | 100 | /// File name to which this position refers. 101 | std::string* filename; 102 | /// Current line number. 103 | unsigned int line; 104 | /// Current column number. 105 | unsigned int column; 106 | }; 107 | 108 | /// Add and assign a position. 109 | inline position& 110 | operator+= (position& res, const int width) 111 | { 112 | res.columns (width); 113 | return res; 114 | } 115 | 116 | /// Add two position objects. 117 | inline const position 118 | operator+ (const position& begin, const int width) 119 | { 120 | position res = begin; 121 | return res += width; 122 | } 123 | 124 | /// Add and assign a position. 125 | inline position& 126 | operator-= (position& res, const int width) 127 | { 128 | return res += -width; 129 | } 130 | 131 | /// Add two position objects. 132 | inline const position 133 | operator- (const position& begin, const int width) 134 | { 135 | return begin + -width; 136 | } 137 | 138 | /// Compare two position objects. 139 | inline bool 140 | operator== (const position& pos1, const position& pos2) 141 | { 142 | return (pos1.line == pos2.line 143 | && pos1.column == pos2.column 144 | && (pos1.filename == pos2.filename 145 | || (pos1.filename && pos2.filename 146 | && *pos1.filename == *pos2.filename))); 147 | } 148 | 149 | /// Compare two position objects. 150 | inline bool 151 | operator!= (const position& pos1, const position& pos2) 152 | { 153 | return !(pos1 == pos2); 154 | } 155 | 156 | /** \brief Intercept output stream redirection. 157 | ** \param ostr the destination output stream 158 | ** \param pos a reference to the position to redirect 159 | */ 160 | template 161 | inline std::basic_ostream& 162 | operator<< (std::basic_ostream& ostr, const position& pos) 163 | { 164 | if (pos.filename) 165 | ostr << *pos.filename << ':'; 166 | return ostr << pos.line << '.' << pos.column; 167 | } 168 | 169 | /* Line 148 of location.cc */ 170 | #line 24 "../s/bif.y" 171 | } // BIF 172 | /* Line 148 of location.cc */ 173 | #line 174 "../bisonflex/position.hh" 174 | #endif /* !YY_YY_BISONFLEX_POSITION_HH_INCLUDED */ 175 | -------------------------------------------------------------------------------- /postprocess.cpp: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #include "imageheadertable-versal.h" 19 | #include "partitionheadertable-versal.h" 20 | #include "bootimage-versal.h" 21 | #include "bootimage.h" 22 | extern "C" { 23 | #include "cdo-npi.h" 24 | }; 25 | 26 | bool VersalImageHeader::PostProcessCdo(const uint8_t* cdo_data, Binary::Length_t cdo_size) { 27 | void * new_data = NULL; 28 | size_t new_size = 0; 29 | if (cdocmd_post_process_cdo(cdo_data, cdo_size, &new_data, &new_size)) { 30 | LOG_ERROR("CDO post process error"); 31 | return false; 32 | } 33 | if (new_data == NULL) return false; 34 | SetPartitionType(PartitionType::CONFIG_DATA_OBJ); 35 | PartitionHeader* hdr = new VersalPartitionHeader(this, 0); 36 | hdr->firstValidIndex = true; 37 | hdr->execAddress = 0; 38 | hdr->loadAddress = 0xFFFFFFFFFFFFFFFF; 39 | hdr->partitionSize = hdr->transferSize = new_size; 40 | hdr->partition = new VersalPartition(hdr, (uint8_t*)new_data, new_size); 41 | partitionHeaderList.push_back(hdr); 42 | return true; 43 | } 44 | -------------------------------------------------------------------------------- /readimage-versal.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | #pragma once 18 | 19 | #ifndef _READIMAGE_VERSAL_H_ 20 | #define _READIMAGE_VERSAL_H_ 21 | 22 | /* 23 | ------------------------------------------------------------------------------- 24 | *********************************************** H E A D E R F I L E S *** 25 | ------------------------------------------------------------------------------- 26 | */ 27 | #include 28 | #include 29 | #include 30 | #include "readimage.h" 31 | 32 | #include "bootheader-versal.h" 33 | #include "imageheadertable-versal.h" 34 | #include "partitionheadertable-versal.h" 35 | #include "authentication-versal.h" 36 | 37 | 38 | /* Forward Class References */ 39 | class BootGenOptions; 40 | class BootHeader; 41 | class ImageHeaderTable; 42 | class BootImage; 43 | class Partition; 44 | class ImageHeader; 45 | class ByteFile; 46 | 47 | 48 | /* 49 | ------------------------------------------------------------------------------- 50 | ********************************************************* C L A S S E S *** 51 | ------------------------------------------------------------------------------- 52 | */ 53 | /******************************************************************************/ 54 | class VersalReadImage :public ReadImage 55 | { 56 | public: 57 | VersalReadImage(std::string filename) : ReadImage(filename) 58 | { 59 | binFilename = filename; 60 | bH = NULL; 61 | iHT = NULL; 62 | iHs.clear(); 63 | pHTs.clear(); 64 | partitionBuffers.clear(); 65 | aCs.clear(); 66 | authenticationVerified = true; 67 | versalNetSeries = false; 68 | } 69 | ~VersalReadImage(); 70 | 71 | void ReadBinaryFile(DumpOption::Type dump=DumpOption::NONE, std::string path=""); 72 | void ReadHeaderTableDetails(); 73 | void ReadPartitions(); 74 | void DisplayImageDetails(ReadImageOption::Type type, DumpOption::Type dump, std::string path=""); 75 | void DisplayHeaderTableDetails(ReadImageOption::Type type); 76 | void DisplayBootHeader(void); 77 | void DisplayImageHeaderTable(void); 78 | void DisplayImageHeaders(void); 79 | void DisplayPartitionHeaderTable(void); 80 | void DisplayAuthenicationCertificates(void); 81 | void DisplayACFields(uint8_t* rsa_ac, Authentication::Type auth_type); 82 | void DisplayBhAttributes(uint32_t value); 83 | void DisplayIhtAttributes(uint32_t value); 84 | void DisplayIhAttributes(uint32_t value); 85 | void DisplayPhtAttributes(uint32_t value); 86 | void DisplaySmapVectors(void); 87 | void VerifyAuthentication(bool); 88 | void VerifyHeaderTableSignature(); 89 | void VerifySPKSignature(uint8_t* aC); 90 | void VerifyPartitionSignature(void); 91 | bool VerifySignature(bool nist, uint8_t * data, size_t dataLength, ACKey4096Sha3Padding * acKey, uint8_t* signature); 92 | bool VerifyECDSASignature(bool nist, uint8_t * data, size_t dataLength, ACKeyECDSA *eckey, uint8_t* signature); 93 | bool VerifyECDSAP521Signature(bool nist, uint8_t * data, size_t dataLength, ACKeyECDSAP521 *eckey, uint8_t* signature); 94 | void Separator(void); 95 | void DumpPartitions(uint8_t* buffer, uint32_t length, std::string name, uint32_t id = 0, uint32_t index = 0); 96 | void DisplayImageInfo(); 97 | uint32_t GetPdiId(void); 98 | uint32_t GetParentId(void); 99 | uint8_t GetCreatorId(void); 100 | std::string GetPartitionType(uint32_t); 101 | std::string GetPartitionCore(uint32_t); 102 | 103 | protected: 104 | std::string binFilename; 105 | VersalBootHeaderStructure* bH; 106 | VersalImageHeaderTableStructure* iHT; 107 | VersalImageHeaderStructure *iH; 108 | VersalPartitionHeaderTableStructure *pHT; 109 | std::list iHs; 110 | std::list pHTs; 111 | std::list aCs; 112 | std::list partitionBuffers; 113 | bool authenticationVerified; 114 | }; 115 | 116 | #endif 117 | -------------------------------------------------------------------------------- /readimage-zynq.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #pragma once 19 | 20 | #ifndef _READIMAGE_ZYNQ_H_ 21 | #define _READIMAGE_ZYNQ_H_ 22 | 23 | 24 | /* 25 | ------------------------------------------------------------------------------- 26 | *********************************************** H E A D E R F I L E S *** 27 | ------------------------------------------------------------------------------- 28 | */ 29 | 30 | #include "readimage.h" 31 | #include "bootheader-zynq.h" 32 | #include "imageheadertable-zynq.h" 33 | #include "partitionheadertable-zynq.h" 34 | #include "authentication-zynq.h" 35 | 36 | /* 37 | ------------------------------------------------------------------------------- 38 | ********************************************************* C L A S S E S *** 39 | ------------------------------------------------------------------------------- 40 | */ 41 | /******************************************************************************/ 42 | class ZynqReadImage : public ReadImage 43 | { 44 | public: 45 | ZynqReadImage(std::string filename) : ReadImage(filename) 46 | { 47 | bH = NULL; 48 | iHT = NULL; 49 | iH = NULL; 50 | pHT = NULL; 51 | iHs.clear(); 52 | pHTs.clear(); 53 | } 54 | ~ZynqReadImage(); 55 | 56 | void ReadBinaryFile(DumpOption::Type dump = DumpOption::NONE, std::string path = ""); 57 | void ReadHeaderTableDetails(); 58 | void ReadPartitions(); 59 | void DisplayBootHeader(void); 60 | void DisplayImageHeaderTable(void); 61 | void DisplayImageHeaders(void); 62 | void DisplayPartitionHeaderTable(void); 63 | void DisplayAuthenicationCertificates(void); 64 | void DisplayACFields(uint8_t* rsa_ac, Authentication::Type auth_type); 65 | void DisplayBhAttributes(uint32_t value); 66 | void DisplayPhtAttributes(uint32_t value); 67 | void DisplayBootVectors(void); 68 | void DumpPartitions(uint8_t* buffer, uint32_t length, std::string name); 69 | 70 | protected: 71 | ZynqBootHeaderStructure* bH; 72 | ZynqImageHeaderTableStructure* iHT; 73 | ZynqImageHeaderStructure* iH; 74 | ZynqPartitionHeaderTableStructure* pHT; 75 | std::list iHs; 76 | std::list pHTs; 77 | }; 78 | #endif 79 | -------------------------------------------------------------------------------- /readimage-zynqmp.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #pragma once 19 | 20 | #ifndef _READIMAGE_ZYNQMP_H_ 21 | #define _READIMAGE_ZYNQMP_H_ 22 | 23 | 24 | /* 25 | ------------------------------------------------------------------------------- 26 | *********************************************** H E A D E R F I L E S *** 27 | ------------------------------------------------------------------------------- 28 | */ 29 | #include 30 | #include 31 | #include 32 | 33 | #include "readimage.h" 34 | #include "bootheader-zynqmp.h" 35 | #include "imageheadertable-zynqmp.h" 36 | #include "partitionheadertable-zynqmp.h" 37 | #include "authentication-zynqmp.h" 38 | 39 | /* 40 | ------------------------------------------------------------------------------- 41 | ********************************************************* C L A S S E S *** 42 | ------------------------------------------------------------------------------- 43 | */ 44 | /******************************************************************************/ 45 | class ZynqMpReadImage : public ReadImage 46 | { 47 | public: 48 | ZynqMpReadImage(std::string filename) : ReadImage(filename) 49 | { 50 | bH = NULL; 51 | iHT = NULL; 52 | iH = NULL; 53 | pHT = NULL; 54 | iHs.clear(); 55 | pHTs.clear(); 56 | authenticationVerified = true; 57 | plAcCount = 0; 58 | } 59 | ~ZynqMpReadImage(); 60 | 61 | void ReadBinaryFile(DumpOption::Type dump = DumpOption::NONE, std::string path = ""); 62 | void ReadHeaderTableDetails(); 63 | void ReadPartitions(); 64 | void DisplayBootHeader(void); 65 | void DisplayImageHeaderTable(void); 66 | void DisplayImageHeaders(void); 67 | void DisplayPartitionHeaderTable(void); 68 | void DisplayAuthenicationCertificates(void); 69 | void DisplayACFields(uint8_t* rsa_ac, Authentication::Type auth_type); 70 | void DisplayBhAttributes(uint32_t value); 71 | void DisplayPhtAttributes(uint32_t value); 72 | void DisplayBootVectors(void); 73 | void DumpPartitions(uint8_t* buffer, uint32_t length, std::string name); 74 | 75 | void VerifyAuthentication(bool); 76 | void VerifyHeaderTableSignature(); 77 | void VerifySPKSignature(AuthCertificate4096Structure * auth_cert); 78 | void VerifyPartitionSignature(void); 79 | bool VerifySignature(bool nist, uint8_t * data, size_t dataLength, ACKey4096 * acKey, uint8_t * signature); 80 | 81 | protected: 82 | ZynqMpBootHeaderStructure* bH; 83 | ZynqMpImageHeaderTableStructure* iHT; 84 | ZynqMpImageHeaderStructure* iH; 85 | ZynqMpPartitionHeaderTableStructure* pHT; 86 | std::list iHs; 87 | std::list pHTs; 88 | bool authenticationVerified; 89 | int plAcCount; 90 | }; 91 | #endif 92 | -------------------------------------------------------------------------------- /readimage.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #pragma once 19 | 20 | #ifndef _READIMAGE_H_ 21 | #define _READIMAGE_H_ 22 | 23 | 24 | /* 25 | ------------------------------------------------------------------------------- 26 | *********************************************** H E A D E R F I L E S *** 27 | ------------------------------------------------------------------------------- 28 | */ 29 | #include 30 | #include 31 | #include 32 | 33 | #include "authentication.h" 34 | #include "bootgenenum.h" 35 | #include "encryption.h" 36 | #include "binary.h" 37 | #include "baseclass.h" 38 | #include "bootgenexception.h" 39 | #include "logger.h" 40 | #include "authkeys.h" 41 | 42 | /* Forward Class References */ 43 | class BootGenOptions; 44 | class BootHeader; 45 | class ImageHeaderTable; 46 | class BootImage; 47 | class Partition; 48 | class ImageHeader; 49 | class ByteFile; 50 | 51 | #define SEPARATOR "--------------------------------------------------------------------------------" 52 | /* 53 | ------------------------------------------------------------------------------- 54 | ***************************************************** F U N C T I O N S *** 55 | ------------------------------------------------------------------------------- 56 | */ 57 | 58 | /******************************************************************************/ 59 | class ReadImage 60 | { 61 | public: 62 | ReadImage(std::string filename) 63 | { 64 | binFilename = filename; 65 | iHNames.clear(); 66 | pHTNames.clear(); 67 | aCs.clear(); 68 | readType = ReadImageOption::NONE; 69 | dumpType = DumpOption::NONE; 70 | } 71 | virtual ~ReadImage() { } 72 | 73 | virtual void ReadBinaryFile(DumpOption::Type dump = DumpOption::NONE, std::string path = "") = 0; 74 | virtual void ReadHeaderTableDetails() = 0; 75 | virtual void DumpPartitions() {}; 76 | virtual void ReadPartitions(void) = 0; 77 | virtual void DisplayImageDetails(ReadImageOption::Type type, DumpOption::Type dump, std::string path=""); 78 | virtual void DisplayBootHeader(void) = 0; 79 | virtual void DisplayImageHeaderTable(void) = 0; 80 | virtual void DisplayImageHeaders(void) = 0; 81 | virtual void DisplayPartitionHeaderTable(void) = 0; 82 | virtual void DisplayAuthenicationCertificates(void) = 0; 83 | virtual void DisplayACFields(uint8_t* rsa_ac, Authentication::Type auth_type) = 0; 84 | void DisplayValue(std::string name1, uint32_t value1); 85 | void DisplayValue(std::string name1, uint32_t value1, std::string name2, uint32_t value2); 86 | void DisplayValue(std::string name1, uint32_t value1, std::string name2, std::string value2); 87 | void DisplayLongValues(std::string name1, uint8_t* value, uint32_t num_bytes); 88 | void DisplayAscii(std::string name1, std::string value1); 89 | void DisplayIV(std::string name, uint32_t* ptr); 90 | void DisplayKey(std::string name, uint32_t* ptr); 91 | void DisplayAttributes(std::string name1, std::string value1, std::string name2, std::string value2); 92 | virtual void DisplayBootVectors(void) {}; 93 | 94 | virtual void VerifyAuthentication(bool) {}; 95 | void Separator(); 96 | bool versalNetSeries; 97 | protected: 98 | std::string binFilename; 99 | std::list iHNames; 100 | std::list pHTNames; 101 | std::list aCNames; 102 | std::list aCs; 103 | RSA* rsa; 104 | std::string dumpPath; 105 | DumpOption::Type dumpType; 106 | ReadImageOption::Type readType; 107 | }; 108 | #endif 109 | -------------------------------------------------------------------------------- /reginit.cpp: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | /* 19 | ------------------------------------------------------------------------------- 20 | *********************************************** H E A D E R F I L E S *** 21 | ------------------------------------------------------------------------------- 22 | */ 23 | #include 24 | 25 | #include "bootimage.h" 26 | #include "reginitscanner.h" 27 | #include "options.h" 28 | #include "bifoptions.h" 29 | #include "regdefs.h" 30 | 31 | 32 | /* 33 | ------------------------------------------------------------------------------- 34 | ***************************************************** F U N C T I O N S *** 35 | ------------------------------------------------------------------------------- 36 | */ 37 | /*****************************************************************************/ 38 | void RegisterTable::Build(Options& options, RegisterInitTable* regtab0) 39 | { 40 | regtab = regtab0; 41 | filename = options.bifOptions->GetRegInitFileName(); 42 | 43 | if (filename != "") 44 | { 45 | LOG_INFO("Started RE parsing : %s", filename.c_str()); 46 | 47 | RE::FlexScanner scanner; 48 | RE::BisonParser parser(scanner,options,*this); 49 | scanner.filename = filename; 50 | std::ifstream s(filename.c_str()); 51 | if (!s) 52 | { 53 | LOG_ERROR("Cannot read file - %s", filename.c_str()); 54 | } 55 | scanner.switch_streams(&s); 56 | parser.parse(); 57 | fileParseEnd = true; 58 | LOG_INFO("Done RE parsing : %s. Added %d regiter pairs", filename.c_str(), count); 59 | } 60 | 61 | if(invalidAddr.size() != 0) 62 | { 63 | for (size_t itr = 0; itr < invalidAddr.size(); itr++) 64 | { 65 | LOG_MSG("\t 0x%x", invalidAddr[itr]); 66 | } 67 | LOG_ERROR("Given ini file has the above invalid Addresses : %s", filename.c_str()); 68 | } 69 | 70 | /* Fill the remainder of the area with NOPs. */ 71 | while(count < MAX_REGISTER_INITS) 72 | { 73 | Add(options, INVALID_REGISTER_ADDRESS, 0); 74 | } 75 | } 76 | 77 | /******************************************************************************/ 78 | void RegisterTable::Add(Options& options, uint32_t address, uint32_t value) 79 | { 80 | if (count >= MAX_REGISTER_INITS) 81 | { 82 | LOG_ERROR("Too many register init pairs in %s", filename.c_str()); 83 | } 84 | 85 | if (fileParseEnd != true) 86 | { 87 | bool isvalidAddress = false; 88 | for (int j = 0; j < MAX_REG_GROUPS; j++) 89 | { 90 | if ((address <= (VersalAddressRanges[j].baseaddr + VersalAddressRanges[j].size)) && 91 | (address >= (VersalAddressRanges[j].baseaddr))) 92 | { 93 | isvalidAddress = true; 94 | break; 95 | } 96 | } 97 | 98 | if (!isvalidAddress) 99 | { 100 | invalidAddr.push_back(address); 101 | } 102 | } 103 | 104 | if (address != 0xFFFFFFFF) 105 | { 106 | LOG_INFO("\t address [0x%8x], value = 0x%x", address, value); 107 | } 108 | 109 | regtab->registerInitialization[count ].address = address; 110 | regtab->registerInitialization[count ].value = value; 111 | count++; 112 | } 113 | -------------------------------------------------------------------------------- /reginit.l: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | /* This is the lexer/flexer input file, that tokenizes the register initialization file */ 18 | /* Run the regedit.y file first through bison to generate the regidit.tab.hpp file, which is included herein */ 19 | /* Then run this file through flex to generate the reginit.yy.c source code */ 20 | 21 | 22 | %{ 23 | #include 24 | #include "reginitscanner.h" 25 | #include "reginit.tab.hpp" 26 | typedef RE::BisonParser::token tok; 27 | #include 28 | %} 29 | 30 | %option nodefault yyclass="FlexScanner" noyywrap c++ noyywrap nounput batch debug 31 | 32 | /* 33 | \.bitset\. return tok::BITSET; 34 | \.bitclear\. return tok::BITCLEAR; 35 | \.boot\. return tok::BOOT; 36 | \.user\. return tok::USER; 37 | */ 38 | 39 | 40 | %{ 41 | # define YY_USER_ACTION yylloc->columns (yyleng); 42 | %} 43 | 44 | %% 45 | 46 | %{ 47 | yylloc->step (); 48 | %} 49 | 50 | "/*" { comment(); } 51 | \/\/.*?\n /* ignore */; 52 | 53 | 54 | [0-9]+ yylval->number=atoi(yytext); return tok::DECVALUE; 55 | 0[xX][0-9a-fA-F]+ yylval->number=strtoul(yytext+2,NULL,16); return tok::HEXVALUE; 56 | 57 | \.set\. return tok::SET; 58 | \.or\. return tok::OR; 59 | \.xor\. return tok::XOR; 60 | \.and\. return tok::AND; 61 | 62 | \+ return tok::PLUS; 63 | \- return tok::MINUS; 64 | \<\< return tok::LSHIFT; 65 | \>\> return tok::RSHIFT; 66 | \* return tok::MULT; 67 | \/ return tok::DIVIDE; 68 | \% return tok::MODULO; 69 | \~ return tok::NEGATION; 70 | \& return tok::AND; 71 | \| return tok::OR; 72 | \^ return tok::XOR; 73 | \( return tok::LPAREN; 74 | \) return tok::RPAREN; 75 | \= return tok::EQUAL; 76 | ; return tok::SEMICOLON; 77 | 78 | 79 | [ \t]+ yylloc->step (); 80 | [\n]+ yylloc->lines (yyleng); yylloc->step (); 81 | . std::cerr << "WARNING: bad character '" << yytext << " at " << *yylloc << std::endl; 82 | %% 83 | 84 | 85 | void RE::FlexScanner::comment() 86 | { 87 | char c, c1; 88 | loop: 89 | while ((c = yyinput()) != '*' && c != 0) { 90 | // putchar(c); 91 | } 92 | 93 | if ((c1 = yyinput()) != '/' && c != 0) 94 | { 95 | unput(c1); 96 | goto loop; 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /reginit.y: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | /* register init grammar 18 | * Use bison to process this file into reginit.tab.cpp 19 | */ 20 | 21 | %require "2.4.1" 22 | %skeleton "lalr1.cc" 23 | %defines 24 | %define namespace "RE" 25 | %define parser_class_name "BisonParser" 26 | %parse-param { RE::FlexScanner& scanner } 27 | %parse-param { Options& options} 28 | %parse-param { RegisterTable& regtab} 29 | %lex-param { RE::FlexScanner& scanner } 30 | %locations 31 | %initial-action 32 | { 33 | // Initialize the initial location. 34 | @$.begin.filename = @$.end.filename = &scanner.filename; 35 | }; 36 | 37 | %define api.location.type "BIF::location" 38 | 39 | %code requires { 40 | namespace RE { 41 | class BisonScanner; 42 | class FlexScanner; 43 | class Parser; 44 | } 45 | #include "bootimage.h" 46 | #include "location.hh" 47 | } 48 | 49 | %code { 50 | static int yylex(RE::BisonParser::semantic_type * yylval, RE::BisonParser::location_type* loc, RE::FlexScanner &scanner); 51 | } 52 | 53 | %{ 54 | #include 55 | #include "reginitscanner.h" 56 | #include "options.h" 57 | #include "parsing.h" 58 | %} 59 | 60 | 61 | %start top 62 | 63 | %union 64 | { 65 | unsigned long number; 66 | } 67 | 68 | %token SET OR XOR AND MULT DIVIDE MODULO PLUS MINUS LSHIFT RSHIFT 69 | %left SET OR XOR AND MULT DIVIDE MODULO PLUS MINUS LSHIFT RSHIFT 70 | 71 | %token NEGATION 72 | %right NEGATION 73 | 74 | %token LPAREN RPAREN EQUAL SEMICOLON 75 | %right LPAREN 76 | %left RPAREN 77 | %left EQUAL 78 | 79 | 80 | 81 | %token DECVALUE HEXVALUE 82 | %type expression number multiplicative_expression unary_expression additive_expression shift_expression 83 | %type and_expression xor_expression 84 | 85 | 86 | %% 87 | top : statements ; 88 | 89 | statements : /* empty */ | statements statement ; 90 | 91 | statement : SET expression EQUAL expression SEMICOLON { regtab.Add(options,$2,$4); } ; 92 | 93 | number : HEXVALUE | 94 | DECVALUE | 95 | LPAREN expression RPAREN { $$ = $2; }; 96 | 97 | unary_expression 98 | : number 99 | | PLUS unary_expression {$$ = $2; *options.debugstr << $$ << " + " << $2 << std::endl;} 100 | | NEGATION unary_expression {$$ = ~$2; *options.debugstr << $$ << " ~ " << $2 << std::endl;} 101 | ; 102 | 103 | multiplicative_expression 104 | : unary_expression 105 | | multiplicative_expression MULT unary_expression {$$ = $1 * $3; *options.debugstr << $$ << " = " << $1 << " + " << $3 << std::endl;} 106 | | multiplicative_expression DIVIDE unary_expression {$$ = $1 / $3; *options.debugstr << $$ << " = " << $1 << " / " << $3 << std::endl;} 107 | | multiplicative_expression MODULO unary_expression {$$ = $1 % $3; *options.debugstr << $$ << " = " << $1 << " % " << $3 << std::endl;} 108 | ; 109 | 110 | additive_expression 111 | : multiplicative_expression 112 | | additive_expression PLUS multiplicative_expression {$$ = $1 + $3;*options.debugstr << $$ << " = " << $1 << " + " << $3 << std::endl;} 113 | | additive_expression MINUS multiplicative_expression {$$ = $1 - $3;*options.debugstr << $$ << " = " << $1 << " - " << $3 << std::endl;} 114 | ; 115 | 116 | shift_expression 117 | : additive_expression 118 | | shift_expression LSHIFT additive_expression {$$ = $1 << $3;*options.debugstr << $$ << " = " << $1 << " << " << $3 << std::endl;} 119 | | shift_expression RSHIFT additive_expression {$$ = $1 >> $3;*options.debugstr << $$ << " = " << $1 << " >> " << $3 << std::endl;} 120 | ; 121 | 122 | and_expression 123 | : shift_expression 124 | | and_expression AND shift_expression {$$ = $1 & $3;*options.debugstr << $$ << " = " << $1 << " & " << $3 << std::endl;} 125 | ; 126 | 127 | xor_expression 128 | : and_expression 129 | | xor_expression XOR and_expression {$$ = $1 ^ $3;*options.debugstr << $$ << " = " << $1 << " ^ " << $3 << std::endl;} 130 | ; 131 | 132 | expression 133 | : xor_expression 134 | | expression OR xor_expression {$$ = $1 | $3;*options.debugstr << $$ << " = " << $1 << " | " << $3 << std::endl;} 135 | ; 136 | 137 | 138 | %% 139 | 140 | 141 | void RE::BisonParser::error(const RE::BisonParser::location_type &loc, const std::string &msg) { 142 | Parsing::Error(loc,msg); 143 | } 144 | 145 | static int yylex(RE::BisonParser::semantic_type * yylval, RE::BisonParser::location_type* loc, RE::FlexScanner &scanner) { 146 | return scanner.yylex(yylval, loc); 147 | } 148 | -------------------------------------------------------------------------------- /reginitscanner.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #pragma once 19 | 20 | #ifndef _REGINITSCANNER_H_ 21 | #define _REGINITSCANNER_H_ 22 | 23 | /* 24 | ------------------------------------------------------------------------------- 25 | *********************************************** H E A D E R F I L E S *** 26 | ------------------------------------------------------------------------------- 27 | */ 28 | // Only include FlexLexer.h if it hasn't been already included 29 | #if ! defined(yyFlexLexerOnce) 30 | 31 | #undef yyFlexLexer 32 | #define yyFlexLexer reginitFlexLexer 33 | #include "FlexLexer.h" 34 | #endif 35 | 36 | // Override the interface for yylex since we namespaced it 37 | #undef YY_DECL 38 | #define YY_DECL int RE::FlexScanner::yylex() 39 | 40 | // Include Bison for types / tokens 41 | #include "reginit.tab.hpp" 42 | 43 | 44 | /* 45 | ------------------------------------------------------------------------------- 46 | ********************************************************* C L A S S E S *** 47 | ------------------------------------------------------------------------------- 48 | */ 49 | /******************************************************************************/ 50 | namespace RE 51 | { 52 | class FlexScanner : public yyFlexLexer 53 | { 54 | public: 55 | // save the pointer to yylval so we can change it, and invoke scanner 56 | int yylex(RE::BisonParser::semantic_type * lval, RE::BisonParser::location_type* loc) { 57 | yylval = lval; 58 | yylloc = loc; 59 | return yylex(); 60 | } 61 | void comment(); 62 | std::string filename; 63 | 64 | private: 65 | // Scanning function created by Flex; make this private to force usage 66 | // of the overloaded method so we can get a pointer to Bison's yylval 67 | int yylex(); 68 | // point to yylval (provided by Bison in overloaded yylex) 69 | RE::BisonParser::semantic_type * yylval; 70 | RE::BisonParser::location_type * yylloc; 71 | }; 72 | } 73 | #endif 74 | -------------------------------------------------------------------------------- /stack.hh: -------------------------------------------------------------------------------- 1 | /* A Bison parser, made by GNU Bison 2.7. */ 2 | 3 | /* Stack handling for Bison parsers in C++ 4 | 5 | Copyright (C) 2002-2012 Free Software Foundation, Inc. 6 | 7 | This program is free software: you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation, either version 3 of the License, or 10 | (at your option) any later version. 11 | 12 | This program is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License 18 | along with this program. If not, see . */ 19 | 20 | /* As a special exception, you may create a larger work that contains 21 | part or all of the Bison parser skeleton and distribute that work 22 | under terms of your choice, so long as that work isn't itself a 23 | parser generator using the skeleton or a modified version thereof 24 | as a parser skeleton. Alternatively, if you modify or redistribute 25 | the parser skeleton itself, you may (at your option) remove this 26 | special exception, which will cause the skeleton and the resulting 27 | Bison output files to be licensed under the GNU General Public 28 | License without this special exception. 29 | 30 | This special exception was added by the Free Software Foundation in 31 | version 2.2 of Bison. */ 32 | 33 | /** 34 | ** \file ../bisonflex/stack.hh 35 | ** Define the BIF::stack class. 36 | */ 37 | 38 | #ifndef YY_YY_BISONFLEX_STACK_HH_INCLUDED 39 | # define YY_YY_BISONFLEX_STACK_HH_INCLUDED 40 | 41 | # include 42 | 43 | /* Line 34 of stack.hh */ 44 | #line 24 "../s/bif.y" 45 | namespace BIF { 46 | /* Line 34 of stack.hh */ 47 | #line 48 "../bisonflex/stack.hh" 48 | template > 49 | class stack 50 | { 51 | public: 52 | // Hide our reversed order. 53 | typedef typename S::reverse_iterator iterator; 54 | typedef typename S::const_reverse_iterator const_iterator; 55 | 56 | stack () : seq_ () 57 | { 58 | } 59 | 60 | stack (unsigned int n) : seq_ (n) 61 | { 62 | } 63 | 64 | inline 65 | T& 66 | operator [] (unsigned int i) 67 | { 68 | return seq_[i]; 69 | } 70 | 71 | inline 72 | const T& 73 | operator [] (unsigned int i) const 74 | { 75 | return seq_[i]; 76 | } 77 | 78 | inline 79 | void 80 | push (const T& t) 81 | { 82 | seq_.push_front (t); 83 | } 84 | 85 | inline 86 | void 87 | pop (unsigned int n = 1) 88 | { 89 | for (; n; --n) 90 | seq_.pop_front (); 91 | } 92 | 93 | inline 94 | unsigned int 95 | height () const 96 | { 97 | return seq_.size (); 98 | } 99 | 100 | inline const_iterator begin () const { return seq_.rbegin (); } 101 | inline const_iterator end () const { return seq_.rend (); } 102 | 103 | private: 104 | S seq_; 105 | }; 106 | 107 | /// Present a slice of the top of a stack. 108 | template > 109 | class slice 110 | { 111 | public: 112 | slice (const S& stack, unsigned int range) 113 | : stack_ (stack) 114 | , range_ (range) 115 | { 116 | } 117 | 118 | inline 119 | const T& 120 | operator [] (unsigned int i) const 121 | { 122 | return stack_[range_ - i]; 123 | } 124 | 125 | private: 126 | const S& stack_; 127 | unsigned int range_; 128 | }; 129 | /* Line 116 of stack.hh */ 130 | #line 24 "../s/bif.y" 131 | } // BIF 132 | /* Line 116 of stack.hh */ 133 | #line 134 "../bisonflex/stack.hh" 134 | 135 | #endif /* !YY_YY_BISONFLEX_STACK_HH_INCLUDED */ 136 | -------------------------------------------------------------------------------- /stringutils.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Copyright 2015-2022 Xilinx, Inc. 3 | * Copyright 2022-2023 Advanced Micro Devices, Inc. 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | ******************************************************************************/ 17 | 18 | #pragma once 19 | 20 | #ifndef _STRINGUTILS_H_ 21 | #define _STRINGUTILS_H_ 22 | 23 | /* 24 | ------------------------------------------------------------------------------- 25 | *********************************************** H E A D E R F I L E S *** 26 | ------------------------------------------------------------------------------- 27 | */ 28 | #include 29 | #include 30 | #include // for std::transform 31 | #include 32 | #include 33 | #include 34 | 35 | 36 | /* 37 | ------------------------------------------------------------------------------- 38 | ********************************************************* C L A S S E S *** 39 | ------------------------------------------------------------------------------- 40 | */ 41 | /******************************************************************************/ 42 | class StringUtils 43 | { 44 | public: 45 | static bool EndsWith(const std::string& str, const std::string& suffix) 46 | { 47 | size_t i = str.rfind(suffix); 48 | return (i != std::string::npos) && (i == (str.length() - suffix.length())); 49 | } 50 | 51 | static std::string ChangeExtension(const std::string& source, const std::string& extension) 52 | { 53 | if (extension[0] != '.') 54 | { 55 | throw std::exception(); 56 | } 57 | std::string s = source; 58 | size_t i = s.rfind("."); 59 | size_t len = s.length() - i; 60 | s.replace(i,len,extension); 61 | return s; 62 | } 63 | 64 | static std::string RemoveExtension(const std::string& source) 65 | { 66 | std::string s = source; 67 | return s.substr(0, s.find_last_of(".")); 68 | } 69 | 70 | static std::string GetExtension(const std::string& source) 71 | { 72 | std::string s = source; 73 | size_t i = s.rfind("."); 74 | if (i == std::string::npos) return ""; 75 | std::string x = s.substr(i); 76 | return x; 77 | } 78 | 79 | static void ToLower(std::string& str) 80 | { 81 | std::transform(str.begin(), str.end(), str.begin(), ::tolower); 82 | } 83 | 84 | static std::string BaseName(const std::string& fullpath) 85 | { 86 | size_t i1 = fullpath.rfind("/"); 87 | size_t i2 = fullpath.rfind("\\"); 88 | std::string x; 89 | if (i1 != std::string::npos) 90 | { 91 | x = fullpath.substr(i1+1); 92 | } 93 | else if (i2 != std::string::npos) 94 | { 95 | x = fullpath.substr(i2+1); 96 | } 97 | else 98 | { 99 | x = fullpath; 100 | } 101 | return x; 102 | } 103 | 104 | static std::string Format(const std::string fmt, ...) 105 | { 106 | std::string str; 107 | va_list ap; 108 | va_start(ap, fmt); 109 | str = vFormat(fmt,ap); 110 | va_end(ap); 111 | return str; 112 | } 113 | 114 | static std::string vFormat(const std::string fmt, va_list ap) 115 | { 116 | int size = 300; 117 | std::string str; 118 | while (1) 119 | { 120 | str.resize(size); 121 | int n = vsnprintf((char *)str.c_str(), size,fmt.c_str(), ap); 122 | if (n > -1 && n < size) 123 | { 124 | str.resize(n); 125 | return str; 126 | } 127 | if (n > -1) 128 | size = n + 1; 129 | else 130 | size *= 2; 131 | } 132 | return str; 133 | } 134 | 135 | static std::pair Split_jsonString(const std::string& str) 136 | { 137 | auto first = str.find_last_of('{'); 138 | if (first == std::string::npos) return{}; 139 | auto mid = str.find_first_of(':', first); 140 | if (mid == std::string::npos) return{}; 141 | auto last = str.find_first_of('}', mid); 142 | if (last == std::string::npos) return{}; 143 | return{ str.substr(first + 1, mid - first - 1), str.substr(mid + 1, last - mid - 1) }; 144 | } 145 | 146 | static std::string Trim_quotes(const std::string& str) { 147 | auto first = str.find_first_of('"'); 148 | if (first == std::string::npos) return{}; 149 | auto last = str.find_first_of('"', first + 1); 150 | if (last == std::string::npos) return{}; 151 | return str.substr(first + 1, last - first - 1); 152 | } 153 | 154 | static std::string FolderPath(const std::string& fullpath) 155 | { 156 | size_t i1 = fullpath.rfind("/"); 157 | size_t i2 = fullpath.rfind("\\"); 158 | std::string x; 159 | if (i1 != std::string::npos) 160 | { 161 | x = fullpath.substr(0, i1); 162 | } 163 | else if (i2 != std::string::npos) 164 | { 165 | x = fullpath.substr(0, i2); 166 | } 167 | else 168 | { 169 | x = "./"; 170 | } 171 | return x; 172 | } 173 | }; 174 | #endif 175 | -------------------------------------------------------------------------------- /xil-bignum.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. 3 | * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved 4 | * 5 | * Licensed under the OpenSSL license (the "License"). You may not use 6 | * this file except in compliance with the License. You can obtain a copy 7 | * in the file LICENSE in the source distribution or at 8 | * https://www.openssl.org/source/license.html 9 | */ 10 | 11 | #ifndef _XIL_BIGNUM_H_ 12 | #define _XIL_BIGNUM_H_ 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | struct bignum_st { 19 | BN_ULONG *d; /* Pointer to an array of 'BN_BITS2' bit 20 | * chunks. */ 21 | int top; /* Index of last used d +1. */ 22 | /* The next are internal book keeping for bn_expand. */ 23 | int dmax; /* Size of the d array. */ 24 | int neg; /* one if the number is negative */ 25 | int flags; 26 | }; 27 | 28 | /* Used for montgomery multiplication */ 29 | struct bn_mont_ctx_st { 30 | int ri; /* number of bits in R */ 31 | BIGNUM RR; /* used to convert to montgomery form */ 32 | BIGNUM N; /* The modulus */ 33 | BIGNUM Ni; /* R*(1/R mod N) - N*Ni = 1 (Ni is only 34 | * stored for bignum algorithm) */ 35 | BN_ULONG n0[2]; /* least significant word(s) of Ni; (type 36 | * changed with 0.9.9, was "BN_ULONG n0;" 37 | * before) */ 38 | int flags; 39 | }; 40 | 41 | #endif --------------------------------------------------------------------------------