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 LoadSize; 118 uint32_t reserved[2]; 119 } COMPONENT_DATA, *PCOMPONENT_DATA; 120 121 122 typedef struct { 123 uint32_t ComponentID; 124 uint32_t StorageAddr; 125 uint32_t ImageSize; 126 uint32_t reserved; 127 } TRUST_COMPONENT, *PTRUST_COMPONENT; 128 129 #define EI_NIDENT 16 130 #define ELF_MAGIC 0x464c457f 131 132 typedef struct { 133 uint8_t e_ident[EI_NIDENT]; 134 uint16_t e_type; 135 uint16_t e_machine; 136 uint32_t e_version; 137 uint32_t e_entry; /* Entry point */ 138 uint32_t e_phoff; 139 uint32_t e_shoff; 140 uint32_t e_flags; 141 uint16_t e_ehsize; 142 uint16_t e_phentsize; 143 uint16_t e_phnum; 144 uint16_t e_shentsize; 145 uint16_t e_shnum; 146 uint16_t e_shstrndx; 147 } Elf32_Ehdr; 148 149 typedef struct { 150 uint8_t e_ident[EI_NIDENT]; /* ELF "magic number" */ 151 uint16_t e_type; 152 uint16_t e_machine; 153 uint32_t e_version; 154 uint64_t e_entry; /* Entry point virtual address */ 155 uint64_t e_phoff; /* Program header table file offset */ 156 uint64_t e_shoff; /* Section header table file offset */ 157 uint32_t e_flags; 158 uint16_t e_ehsize; 159 uint16_t e_phentsize; 160 uint16_t e_phnum; 161 uint16_t e_shentsize; 162 uint16_t e_shnum; 163 uint16_t e_shstrndx; 164 } Elf64_Ehdr; 165 166 typedef struct { 167 uint32_t p_type; 168 uint32_t p_offset; 169 uint32_t p_vaddr; 170 uint32_t p_paddr; 171 uint32_t p_filesz; 172 uint32_t p_memsz; 173 uint32_t p_flags; 174 uint32_t p_align; 175 } Elf32_Phdr; 176 177 typedef struct { 178 uint32_t p_type; 179 uint32_t p_flags; 180 uint64_t p_offset; /* Segment file offset */ 181 uint64_t p_vaddr; /* Segment virtual address */ 182 uint64_t p_paddr; /* Segment physical address */ 183 uint64_t p_filesz; /* Segment size in file */ 184 uint64_t p_memsz; /* Segment size in memory */ 185 uint64_t p_align; /* Segment alignment, file & memory */ 186 } Elf64_Phdr; 187 188 #endif /* TRUST_MERGER_H */ 189