| 1 |
|
| 2 |
|
| 3 |
#include <commonlib/endian.h> |
| 4 |
#include <string.h> |
| 5 |
|
| 6 |
#include "cbfs.h" |
| 7 |
#include "cbfs_sections.h" |
| 8 |
#include "elfparsing.h" |
| 9 |
|
| 10 |
#define MBN_V7 7 |
| 11 |
|
| 12 |
struct mbnv7_hashtbl_seg { |
| 13 |
uint32_t reserved; |
| 14 |
uint32_t version; |
| 15 |
uint32_t common_metadata_size; |
| 16 |
uint32_t qti_metadata_size; |
| 17 |
uint32_t oem_metadata_size; |
| 18 |
uint32_t hash_table_size; |
| 19 |
uint32_t qti_signature_size; |
| 20 |
uint32_t qti_certificate_chain_size; |
| 21 |
uint32_t oem_signature_size; |
| 22 |
uint32_t oem_certificate_chain_size; |
| 23 |
}; |
| 24 |
|
| 25 |
enum hash_table_algo { |
| 26 |
QUALCOMM_HASH_TABLE_ALGO_SHA384 = 3, |
| 27 |
}; |
| 28 |
|
| 29 |
struct common_metadata { |
| 30 |
uint32_t major; |
| 31 |
uint32_t minor; |
| 32 |
uint32_t software_id; |
| 33 |
uint32_t secondary_software_id; |
| 34 |
uint32_t hash_table_algorithm; |
| 35 |
uint32_t measurement_register_target; |
| 36 |
}; |
| 37 |
|
| 38 |
static void *qualcomm_find_hash_mbnv7(struct buffer *elf, size_t bb_offset, |
| 39 |
struct vb2_hash *real_hash) |
| 40 |
{ |
| 41 |
void *hashtable = NULL, *bb_hash = NULL; |
| 42 |
struct common_metadata *cmn = NULL; |
| 43 |
struct mbnv7_hashtbl_seg *mbn; |
| 44 |
struct parsed_elf pelf; |
| 45 |
int bb_segment = -1; |
| 46 |
Elf64_Phdr *ph; |
| 47 |
|
| 48 |
while (true) { |
| 49 |
size_t tmp; |
| 50 |
|
| 51 |
if (!buffer_check_magic(elf, ELFMAG, 4) || |
| 52 |
parse_elf(elf, &pelf, ELF_PARSE_PHDR)) |
| 53 |
return NULL; |
| 54 |
|
| 55 |
ph = &pelf.phdr[pelf.ehdr.e_phnum - 1]; |
| 56 |
|
| 57 |
tmp = ph->p_offset + ph->p_filesz; |
| 58 |
|
| 59 |
if (buffer_offset(elf) < bb_offset && |
| 60 |
bb_offset < (buffer_offset(elf) + tmp)) |
| 61 |
break; |
| 62 |
|
| 63 |
parsed_elf_destroy(&pelf); |
| 64 |
|
| 65 |
buffer_seek(elf, tmp); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
for (int i = 0; i < pelf.ehdr.e_phnum; i++) { |
| 71 |
ph = &pelf.phdr[i]; |
| 72 |
mbn = buffer_get(elf) + ph->p_offset; |
| 73 |
|
| 74 |
if (((ph->p_flags & PF_QC_SG_MASK) == PF_QC_SG_HASH) && |
| 75 |
(mbn->version == MBN_V7)) { |
| 76 |
cmn = buffer_get(elf) + ph->p_offset + |
| 77 |
sizeof(*mbn); |
| 78 |
|
| 79 |
hashtable = buffer_get(elf) + ph->p_offset + |
| 80 |
sizeof(*mbn) + mbn->common_metadata_size + |
| 81 |
mbn->qti_metadata_size + |
| 82 |
mbn->oem_metadata_size; |
| 83 |
} else if (ph->p_type == PT_LOAD && ph->p_flags == (PF_X | PF_W | PF_R)) { |
| 84 |
bb_segment = i; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
if (!hashtable || !cmn) |
| 89 |
goto destroy_elf; |
| 90 |
|
| 91 |
|
| 92 |
if (cmn->hash_table_algorithm != QUALCOMM_HASH_TABLE_ALGO_SHA384) |
| 93 |
goto destroy_elf; |
| 94 |
|
| 95 |
bb_hash = hashtable + bb_segment * VB2_SHA384_DIGEST_SIZE; |
| 96 |
|
| 97 |
|
| 98 |
if (vb2_hash_calculate(false, buffer_get(elf) + pelf.phdr[bb_segment].p_offset, |
| 99 |
pelf.phdr[bb_segment].p_filesz, VB2_HASH_SHA384, real_hash)) { |
| 100 |
ERROR("fixups: vboot digest error\n"); |
| 101 |
goto destroy_elf; |
| 102 |
} |
| 103 |
|
| 104 |
destroy_elf: |
| 105 |
parsed_elf_destroy(&pelf); |
| 106 |
return bb_hash; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
static void *qualcomm_find_hash(struct buffer *in, size_t bb_offset, struct vb2_hash *real_hash) |
| 115 |
{ |
| 116 |
Elf64_Phdr *ph; |
| 117 |
struct buffer elf; |
| 118 |
buffer_clone(&elf, in); |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
size_t search_end_size = MIN(0, buffer_size(in) - 32 * KiB); |
| 123 |
|
| 124 |
|
| 125 |
while (buffer_size(&elf) > search_end_size && |
| 126 |
!buffer_check_magic(&elf, "EFI PART", 8)) |
| 127 |
buffer_seek(&elf, 512); |
| 128 |
|
| 129 |
|
| 130 |
while (buffer_size(&elf) > search_end_size && |
| 131 |
!buffer_check_magic(&elf, ELFMAG, 4)) |
| 132 |
buffer_seek(&elf, 512); |
| 133 |
|
| 134 |
if (buffer_size(&elf) <= search_end_size) |
| 135 |
return NULL; |
| 136 |
|
| 137 |
struct parsed_elf pelf; |
| 138 |
if (parse_elf(&elf, &pelf, ELF_PARSE_PHDR)) |
| 139 |
return NULL; |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
ph = &pelf.phdr[pelf.ehdr.e_phnum - 1]; |
| 146 |
if (bb_offset > (ph->p_offset + ph->p_filesz)) { |
| 147 |
parsed_elf_destroy(&pelf); |
| 148 |
return qualcomm_find_hash_mbnv7(&elf, bb_offset, real_hash); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
void *bb_hash = NULL; |
| 154 |
void *hashtable = NULL; |
| 155 |
int i; |
| 156 |
int bb_segment = -1; |
| 157 |
for (i = 0; i < pelf.ehdr.e_phnum; i++) { |
| 158 |
ph = &pelf.phdr[i]; |
| 159 |
if ((ph->p_flags & PF_QC_SG_MASK) == PF_QC_SG_HASH) { |
| 160 |
if ((int)ph->p_filesz != |
| 161 |
(pelf.ehdr.e_phnum + 1) * VB2_SHA384_DIGEST_SIZE) { |
| 162 |
ERROR("fixups: Qualcomm hash segment has wrong size!\n"); |
| 163 |
goto destroy_elf; |
| 164 |
} |
| 165 |
hashtable = buffer_get(&elf) + ph->p_offset; |
| 166 |
} else if (bb_segment < 0 && ph->p_offset + ph->p_filesz < buffer_size(&elf) && |
| 167 |
buffer_offset(&elf) + ph->p_offset <= bb_offset && |
| 168 |
buffer_offset(&elf) + ph->p_offset + ph->p_filesz > bb_offset) { |
| 169 |
bb_segment = i; |
| 170 |
} |
| 171 |
} |
| 172 |
if (!hashtable) |
| 173 |
goto destroy_elf; |
| 174 |
if (bb_segment < 0) { |
| 175 |
ERROR("fixups: Cannot find bootblock code in Qualcomm MBN!\n"); |
| 176 |
goto destroy_elf; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
if (vb2_hash_calculate(false, buffer_get(&elf) + pelf.phdr[bb_segment].p_offset, |
| 181 |
pelf.phdr[bb_segment].p_filesz, VB2_HASH_SHA384, real_hash)) { |
| 182 |
ERROR("fixups: vboot digest error\n"); |
| 183 |
goto destroy_elf; |
| 184 |
} |
| 185 |
bb_hash = hashtable + (bb_segment + 1) * VB2_SHA384_DIGEST_SIZE; |
| 186 |
|
| 187 |
destroy_elf: |
| 188 |
parsed_elf_destroy(&pelf); |
| 189 |
return bb_hash; |
| 190 |
} |
| 191 |
|
| 192 |
static bool qualcomm_probe(struct buffer *buffer, size_t offset) |
| 193 |
{ |
| 194 |
struct vb2_hash real_hash; |
| 195 |
void *table_hash = qualcomm_find_hash(buffer, offset, &real_hash); |
| 196 |
if (!table_hash) |
| 197 |
return false; |
| 198 |
|
| 199 |
if (memcmp(real_hash.raw, table_hash, VB2_SHA384_DIGEST_SIZE)) { |
| 200 |
ERROR("fixups: Identified Qualcomm MBN, but existing hash doesn't match!\n"); |
| 201 |
return false; |
| 202 |
} |
| 203 |
|
| 204 |
return true; |
| 205 |
} |
| 206 |
|
| 207 |
static int qualcomm_fixup(struct buffer *buffer, size_t offset) |
| 208 |
{ |
| 209 |
struct vb2_hash real_hash; |
| 210 |
void *table_hash = qualcomm_find_hash(buffer, offset, &real_hash); |
| 211 |
if (!table_hash) { |
| 212 |
ERROR("fixups: Cannot find Qualcomm MBN headers anymore!\n"); |
| 213 |
return -1; |
| 214 |
} |
| 215 |
|
| 216 |
memcpy(table_hash, real_hash.raw, VB2_SHA384_DIGEST_SIZE); |
| 217 |
INFO("fixups: Updated Qualcomm MBN header bootblock hash.\n"); |
| 218 |
return 0; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
#define MEDIATEK_BOOTBLOCK_HEADER_SIZE 2048 |
| 231 |
#define MEDIATEK_BOOTBLOCK_GFH_SIZE 176 |
| 232 |
static void *mediatek_find_hash(struct buffer *bootblock, struct vb2_hash *real_hash) |
| 233 |
{ |
| 234 |
struct buffer buffer; |
| 235 |
size_t data_size; |
| 236 |
const char emmc_magic[] = "EMMC_BOOT"; |
| 237 |
const char sf_magic[] = "SF_BOOT"; |
| 238 |
const char brlyt_magic[] = "BRLYT"; |
| 239 |
const size_t brlyt_offset = 512; |
| 240 |
|
| 241 |
buffer_clone(&buffer, bootblock); |
| 242 |
|
| 243 |
|
| 244 |
if (buffer_size(&buffer) < |
| 245 |
MEDIATEK_BOOTBLOCK_HEADER_SIZE + MEDIATEK_BOOTBLOCK_GFH_SIZE) |
| 246 |
return NULL; |
| 247 |
|
| 248 |
|
| 249 |
if (!buffer_check_magic(&buffer, emmc_magic, strlen(emmc_magic)) && |
| 250 |
!buffer_check_magic(&buffer, sf_magic, strlen(sf_magic))) |
| 251 |
return NULL; |
| 252 |
|
| 253 |
|
| 254 |
buffer_seek(&buffer, brlyt_offset); |
| 255 |
if (!buffer_check_magic(&buffer, brlyt_magic, strlen(brlyt_magic))) |
| 256 |
return NULL; |
| 257 |
|
| 258 |
buffer_seek(&buffer, MEDIATEK_BOOTBLOCK_HEADER_SIZE - brlyt_offset); |
| 259 |
data_size = read_le32(buffer_get(&buffer) + 32); |
| 260 |
if (data_size <= MEDIATEK_BOOTBLOCK_GFH_SIZE + VB2_SHA256_DIGEST_SIZE) { |
| 261 |
ERROR("fixups: MediaTek: data size too small: %zu\n", data_size); |
| 262 |
return NULL; |
| 263 |
} |
| 264 |
data_size -= MEDIATEK_BOOTBLOCK_GFH_SIZE + VB2_SHA256_DIGEST_SIZE; |
| 265 |
|
| 266 |
if (buffer_size(&buffer) < |
| 267 |
MEDIATEK_BOOTBLOCK_GFH_SIZE + data_size + VB2_SHA256_DIGEST_SIZE) { |
| 268 |
ERROR("fixups: MediaTek: not enough data: %zu\n", buffer_size(&buffer)); |
| 269 |
return NULL; |
| 270 |
} |
| 271 |
|
| 272 |
if (vb2_hash_calculate(false, buffer_get(&buffer), |
| 273 |
MEDIATEK_BOOTBLOCK_GFH_SIZE + data_size, |
| 274 |
VB2_HASH_SHA256, real_hash)) { |
| 275 |
ERROR("fixups: MediaTek: vboot digest error\n"); |
| 276 |
return NULL; |
| 277 |
} |
| 278 |
|
| 279 |
buffer_seek(&buffer, MEDIATEK_BOOTBLOCK_GFH_SIZE + data_size); |
| 280 |
return buffer_get(&buffer); |
| 281 |
} |
| 282 |
|
| 283 |
static bool mediatek_probe(struct buffer *buffer) |
| 284 |
{ |
| 285 |
struct vb2_hash real_hash; |
| 286 |
void *hash = mediatek_find_hash(buffer, &real_hash); |
| 287 |
if (!hash) |
| 288 |
return false; |
| 289 |
|
| 290 |
if (memcmp(real_hash.raw, hash, VB2_SHA256_DIGEST_SIZE)) { |
| 291 |
ERROR("fixups: Found MediaTek bootblock, but existing hash doesn't match!\n"); |
| 292 |
return false; |
| 293 |
} |
| 294 |
|
| 295 |
return true; |
| 296 |
} |
| 297 |
|
| 298 |
static int mediatek_fixup(struct buffer *buffer, unused size_t offset) |
| 299 |
{ |
| 300 |
struct vb2_hash real_hash; |
| 301 |
void *hash = mediatek_find_hash(buffer, &real_hash); |
| 302 |
if (!hash) { |
| 303 |
ERROR("fixups: Cannot find MediaTek header anymore!\n"); |
| 304 |
return -1; |
| 305 |
} |
| 306 |
|
| 307 |
memcpy(hash, real_hash.raw, VB2_SHA256_DIGEST_SIZE); |
| 308 |
INFO("fixups: Updated MediaTek bootblock hash.\n"); |
| 309 |
return 0; |
| 310 |
} |
| 311 |
|
| 312 |
platform_fixup_func platform_fixups_probe(struct buffer *buffer, size_t offset, |
| 313 |
const char *region_name) |
| 314 |
{ |
| 315 |
if (!strcmp(region_name, SECTION_NAME_BOOTBLOCK)) { |
| 316 |
if (qualcomm_probe(buffer, offset)) |
| 317 |
return qualcomm_fixup; |
| 318 |
else if (mediatek_probe(buffer)) |
| 319 |
return mediatek_fixup; |
| 320 |
} else if (!strcmp(region_name, SECTION_NAME_PRIMARY_CBFS)) { |
| 321 |
|
| 322 |
} else { |
| 323 |
ERROR("%s called for unexpected FMAP region %s!\n", __func__, region_name); |
| 324 |
} |
| 325 |
|
| 326 |
return NULL; |
| 327 |
} |