1 /* 2 * Rockchip trust image generator 3 * 4 * (C) Copyright 2008-2015 Fuzhou Rockchip Electronics Co., Ltd 5 * Peter, Software Engineering, <superpeter.cai@gmail.com>. 6 * 7 * SPDX-License-Identifier: GPL-2.0+ 8 */ 9 #ifndef TRUST_MERGER_H 10 #define TRUST_MERGER_H 11 12 #include <stdint.h> 13 #include <stdio.h> 14 #include <stdlib.h> 15 #include <string.h> 16 #include <memory.h> 17 #include <stdbool.h> 18 19 20 #define VERSION "v1.0 (2015-06-15)" 21 #define DO_ALIGN(a, b) (((a) > 0) ? ((((a) - 1) / (b) + 1) * (b)) : (a)) 22 23 24 /* config file */ 25 #define SEC_VERSION "[VERSION]" 26 #define SEC_BL30 "[BL30_OPTION]" 27 #define SEC_BL31 "[BL31_OPTION]" 28 #define SEC_BL32 "[BL32_OPTION]" 29 #define SEC_BL33 "[BL33_OPTION]" 30 #define SEC_OUT "[OUTPUT]" 31 32 #define OPT_MAJOR "MAJOR" 33 #define OPT_MINOR "MINOR" 34 #define OPT_SEC "SEC" 35 #define OPT_PATH "PATH" 36 #define OPT_ADDR "ADDR" 37 #define OPT_OUT_PATH "PATH" 38 39 /* options */ 40 #define OPT_VERBOSE "--verbose" 41 #define OPT_HELP "--help" 42 #define OPT_VERSION "--version" 43 #define OPT_MERGE "--pack" 44 #define OPT_UNPACK "--unpack" 45 #define OPT_SUBFIX "--subfix" 46 #define OPT_REPLACE "--replace" 47 #define OPT_PREPATH "--prepath" 48 #define OPT_RSA "--rsa" 49 #define OPT_SHA "--sha" 50 #define OPT_SIZE "--size" 51 #define OPT_IGNORE_BL32 "--ignore-bl32" 52 53 #define DEF_MAJOR 0 54 #define DEF_MINOR 0 55 #define DEF_BL30_PATH "bl30.bin" 56 #define DEF_BL31_PATH "bl31.bin" 57 #define DEF_BL32_PATH "bl32.bin" 58 #define DEF_BL33_PATH "bl33.bin" 59 60 #define DEF_OUT_PATH "trust.img" 61 62 #define DEF_CONFIG_FILE "RKTRUST.ini" 63 64 65 #define MAX_LINE_LEN 256 66 #define SCANF_EAT(in) fscanf(in, "%*[ \r\n\t/]") 67 68 #define ENTRY_ALIGN (2048) 69 70 enum { 71 BL30_SEC = 0, 72 BL31_SEC, 73 BL32_SEC, 74 BL33_SEC, 75 BL_MAX_SEC 76 }; 77 78 79 80 typedef struct { 81 bool sec; 82 uint32_t id; 83 char path[MAX_LINE_LEN]; 84 uint32_t addr; 85 uint32_t offset; 86 uint32_t size; 87 uint32_t align_size; 88 } bl_entry_t; 89 90 typedef struct { 91 uint16_t major; 92 uint16_t minor; 93 bl_entry_t bl3x[BL_MAX_SEC]; 94 char outPath[MAX_LINE_LEN]; 95 } OPT_T; 96 97 98 #define TRUST_HEAD_TAG "BL3X" 99 #define SIGNATURE_SIZE 256 100 #define TRUST_HEADER_SIZE 2048 101 102 typedef struct { 103 uint32_t tag; 104 uint32_t version; 105 uint32_t flags; 106 uint32_t size; 107 uint32_t reserved[4]; 108 uint32_t RSA_N[64]; 109 uint32_t RSA_E[64]; 110 uint32_t RSA_C[64]; 111 } TRUST_HEADER, *PTRUST_HEADER; 112 113 114 typedef struct { 115 uint32_t HashData[8]; 116 uint32_t LoadAddr; 117 uint32_t reserved[3]; 118 } COMPONENT_DATA, *PCOMPONENT_DATA; 119 120 121 typedef struct { 122 uint32_t ComponentID; 123 uint32_t StorageAddr; 124 uint32_t ImageSize; 125 uint32_t reserved; 126 } TRUST_COMPONENT, *PTRUST_COMPONENT; 127 128 #define EI_NIDENT 16 129 #define ELF_MAGIC 0x464c457f 130 131 typedef struct { 132 uint8_t e_ident[EI_NIDENT]; 133 uint16_t e_type; 134 uint16_t e_machine; 135 uint32_t e_version; 136 uint32_t e_entry; /* Entry point */ 137 uint32_t e_phoff; 138 uint32_t e_shoff; 139 uint32_t e_flags; 140 uint16_t e_ehsize; 141 uint16_t e_phentsize; 142 uint16_t e_phnum; 143 uint16_t e_shentsize; 144 uint16_t e_shnum; 145 uint16_t e_shstrndx; 146 } Elf32_Ehdr; 147 148 typedef struct { 149 uint8_t e_ident[EI_NIDENT]; /* ELF "magic number" */ 150 uint16_t e_type; 151 uint16_t e_machine; 152 uint32_t e_version; 153 uint64_t e_entry; /* Entry point virtual address */ 154 uint64_t e_phoff; /* Program header table file offset */ 155 uint64_t e_shoff; /* Section header table file offset */ 156 uint32_t e_flags; 157 uint16_t e_ehsize; 158 uint16_t e_phentsize; 159 uint16_t e_phnum; 160 uint16_t e_shentsize; 161 uint16_t e_shnum; 162 uint16_t e_shstrndx; 163 } Elf64_Ehdr; 164 165 typedef struct { 166 uint32_t p_type; 167 uint32_t p_offset; 168 uint32_t p_vaddr; 169 uint32_t p_paddr; 170 uint32_t p_filesz; 171 uint32_t p_memsz; 172 uint32_t p_flags; 173 uint32_t p_align; 174 } Elf32_Phdr; 175 176 typedef struct { 177 uint32_t p_type; 178 uint32_t p_flags; 179 uint64_t p_offset; /* Segment file offset */ 180 uint64_t p_vaddr; /* Segment virtual address */ 181 uint64_t p_paddr; /* Segment physical address */ 182 uint64_t p_filesz; /* Segment size in file */ 183 uint64_t p_memsz; /* Segment size in memory */ 184 uint64_t p_align; /* Segment alignment, file & memory */ 185 } Elf64_Phdr; 186 187 #endif /* TRUST_MERGER_H */ 188