Bryan O'Donoghue | 4b0de7c | 2018-03-13 16:50:27 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 11 | int optee_verify_image(struct optee_header *hdr, unsigned long tzdram_start, |
| 12 | unsigned long tzdram_len, unsigned long image_len) |
| 13 | { |
| 14 | unsigned long tzdram_end = tzdram_start + tzdram_len; |
| 15 | uint32_t tee_file_size; |
| 16 | |
| 17 | tee_file_size = hdr->init_size + hdr->paged_size + |
| 18 | sizeof(struct optee_header); |
| 19 | |
| 20 | if (hdr->magic != OPTEE_MAGIC || |
| 21 | hdr->version != OPTEE_VERSION || |
| 22 | hdr->init_load_addr_hi > tzdram_end || |
| 23 | hdr->init_load_addr_lo < tzdram_start || |
| 24 | tee_file_size > tzdram_len || |
| 25 | tee_file_size != image_len || |
| 26 | (hdr->init_load_addr_lo + tee_file_size) > tzdram_end) { |
| 27 | return -EINVAL; |
| 28 | } |
| 29 | |
| 30 | return 0; |
| 31 | } |
Bryan O'Donoghue | 4915f9b | 2018-03-13 16:50:33 +0000 | [diff] [blame^] | 32 | |
| 33 | int optee_verify_bootm_image(unsigned long image_addr, |
| 34 | unsigned long image_load_addr, |
| 35 | unsigned long image_len) |
| 36 | { |
| 37 | struct optee_header *hdr = (struct optee_header *)image_addr; |
| 38 | unsigned long tzdram_start = CONFIG_OPTEE_TZDRAM_BASE; |
| 39 | unsigned long tzdram_len = CONFIG_OPTEE_TZDRAM_SIZE; |
| 40 | |
| 41 | int ret; |
| 42 | |
| 43 | ret = optee_verify_image(hdr, tzdram_start, tzdram_len, image_len); |
| 44 | if (ret) |
| 45 | return ret; |
| 46 | |
| 47 | if (image_load_addr + sizeof(*hdr) != hdr->init_load_addr_lo) |
| 48 | ret = -EINVAL; |
| 49 | |
| 50 | return ret; |
| 51 | } |