blob: 78a15e8a32f71db6b386a2129d16b381586284cc [file] [log] [blame]
Bryan O'Donoghue32ce6172018-03-13 16:50:27 +00001/*
2 * Copyright (C) 2017 Linaro
3 * Bryan O'Donoghue <bryan.odonoghue@linaro.org>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <tee/optee.h>
10
Bryan O'Donoghue6ffc4202018-03-13 16:50:34 +000011#define optee_hdr_err_msg \
12 "OPTEE verification error:" \
13 "\n\thdr=%p image=0x%08lx magic=0x%08x tzdram 0x%08lx-0x%08lx " \
14 "\n\theader lo=0x%08x hi=0x%08x size=0x%08lx arch=0x%08x" \
15 "\n\tuimage params 0x%08lx-0x%08lx\n"
16
Bryan O'Donoghue32ce6172018-03-13 16:50:27 +000017int optee_verify_image(struct optee_header *hdr, unsigned long tzdram_start,
18 unsigned long tzdram_len, unsigned long image_len)
19{
20 unsigned long tzdram_end = tzdram_start + tzdram_len;
21 uint32_t tee_file_size;
22
23 tee_file_size = hdr->init_size + hdr->paged_size +
24 sizeof(struct optee_header);
25
26 if (hdr->magic != OPTEE_MAGIC ||
27 hdr->version != OPTEE_VERSION ||
28 hdr->init_load_addr_hi > tzdram_end ||
29 hdr->init_load_addr_lo < tzdram_start ||
30 tee_file_size > tzdram_len ||
31 tee_file_size != image_len ||
32 (hdr->init_load_addr_lo + tee_file_size) > tzdram_end) {
33 return -EINVAL;
34 }
35
36 return 0;
37}
Bryan O'Donoghuec5a6e8b2018-03-13 16:50:33 +000038
39int optee_verify_bootm_image(unsigned long image_addr,
40 unsigned long image_load_addr,
41 unsigned long image_len)
42{
43 struct optee_header *hdr = (struct optee_header *)image_addr;
44 unsigned long tzdram_start = CONFIG_OPTEE_TZDRAM_BASE;
45 unsigned long tzdram_len = CONFIG_OPTEE_TZDRAM_SIZE;
46
47 int ret;
48
49 ret = optee_verify_image(hdr, tzdram_start, tzdram_len, image_len);
50 if (ret)
Bryan O'Donoghue6ffc4202018-03-13 16:50:34 +000051 goto error;
Bryan O'Donoghuec5a6e8b2018-03-13 16:50:33 +000052
Bryan O'Donoghue6ffc4202018-03-13 16:50:34 +000053 if (image_load_addr + sizeof(*hdr) != hdr->init_load_addr_lo) {
Bryan O'Donoghuec5a6e8b2018-03-13 16:50:33 +000054 ret = -EINVAL;
Bryan O'Donoghue6ffc4202018-03-13 16:50:34 +000055 goto error;
56 }
57
58 return ret;
59error:
60 printf(optee_hdr_err_msg, hdr, image_addr, hdr->magic, tzdram_start,
61 tzdram_start + tzdram_len, hdr->init_load_addr_lo,
62 hdr->init_load_addr_hi, image_len, hdr->arch, image_load_addr,
63 image_load_addr + image_len);
Bryan O'Donoghuec5a6e8b2018-03-13 16:50:33 +000064
65 return ret;
66}