Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Bryan O'Donoghue | 32ce617 | 2018-03-13 16:50:27 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2017 Linaro |
| 4 | * Bryan O'Donoghue <bryan.odonoghue@linaro.org> |
Bryan O'Donoghue | 32ce617 | 2018-03-13 16:50:27 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <tee/optee.h> |
| 9 | |
Bryan O'Donoghue | 6ffc420 | 2018-03-13 16:50:34 +0000 | [diff] [blame] | 10 | #define optee_hdr_err_msg \ |
| 11 | "OPTEE verification error:" \ |
| 12 | "\n\thdr=%p image=0x%08lx magic=0x%08x tzdram 0x%08lx-0x%08lx " \ |
| 13 | "\n\theader lo=0x%08x hi=0x%08x size=0x%08lx arch=0x%08x" \ |
| 14 | "\n\tuimage params 0x%08lx-0x%08lx\n" |
| 15 | |
Bryan O'Donoghue | 32ce617 | 2018-03-13 16:50:27 +0000 | [diff] [blame] | 16 | int optee_verify_image(struct optee_header *hdr, unsigned long tzdram_start, |
| 17 | unsigned long tzdram_len, unsigned long image_len) |
| 18 | { |
| 19 | unsigned long tzdram_end = tzdram_start + tzdram_len; |
| 20 | uint32_t tee_file_size; |
| 21 | |
| 22 | tee_file_size = hdr->init_size + hdr->paged_size + |
| 23 | sizeof(struct optee_header); |
| 24 | |
| 25 | if (hdr->magic != OPTEE_MAGIC || |
| 26 | hdr->version != OPTEE_VERSION || |
| 27 | hdr->init_load_addr_hi > tzdram_end || |
| 28 | hdr->init_load_addr_lo < tzdram_start || |
| 29 | tee_file_size > tzdram_len || |
| 30 | tee_file_size != image_len || |
| 31 | (hdr->init_load_addr_lo + tee_file_size) > tzdram_end) { |
| 32 | return -EINVAL; |
| 33 | } |
| 34 | |
| 35 | return 0; |
| 36 | } |
Bryan O'Donoghue | c5a6e8b | 2018-03-13 16:50:33 +0000 | [diff] [blame] | 37 | |
| 38 | int optee_verify_bootm_image(unsigned long image_addr, |
| 39 | unsigned long image_load_addr, |
| 40 | unsigned long image_len) |
| 41 | { |
| 42 | struct optee_header *hdr = (struct optee_header *)image_addr; |
| 43 | unsigned long tzdram_start = CONFIG_OPTEE_TZDRAM_BASE; |
| 44 | unsigned long tzdram_len = CONFIG_OPTEE_TZDRAM_SIZE; |
| 45 | |
| 46 | int ret; |
| 47 | |
| 48 | ret = optee_verify_image(hdr, tzdram_start, tzdram_len, image_len); |
| 49 | if (ret) |
Bryan O'Donoghue | 6ffc420 | 2018-03-13 16:50:34 +0000 | [diff] [blame] | 50 | goto error; |
Bryan O'Donoghue | c5a6e8b | 2018-03-13 16:50:33 +0000 | [diff] [blame] | 51 | |
Bryan O'Donoghue | 6ffc420 | 2018-03-13 16:50:34 +0000 | [diff] [blame] | 52 | if (image_load_addr + sizeof(*hdr) != hdr->init_load_addr_lo) { |
Bryan O'Donoghue | c5a6e8b | 2018-03-13 16:50:33 +0000 | [diff] [blame] | 53 | ret = -EINVAL; |
Bryan O'Donoghue | 6ffc420 | 2018-03-13 16:50:34 +0000 | [diff] [blame] | 54 | goto error; |
| 55 | } |
| 56 | |
| 57 | return ret; |
| 58 | error: |
| 59 | printf(optee_hdr_err_msg, hdr, image_addr, hdr->magic, tzdram_start, |
| 60 | tzdram_start + tzdram_len, hdr->init_load_addr_lo, |
| 61 | hdr->init_load_addr_hi, image_len, hdr->arch, image_load_addr, |
| 62 | image_load_addr + image_len); |
Bryan O'Donoghue | c5a6e8b | 2018-03-13 16:50:33 +0000 | [diff] [blame] | 63 | |
| 64 | return ret; |
| 65 | } |