optee.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2017 Linaro
  4. * Bryan O'Donoghue <bryan.odonoghue@linaro.org>
  5. */
  6. #include <common.h>
  7. #include <tee/optee.h>
  8. #define optee_hdr_err_msg \
  9. "OPTEE verification error:" \
  10. "\n\thdr=%p image=0x%08lx magic=0x%08x tzdram 0x%08lx-0x%08lx " \
  11. "\n\theader lo=0x%08x hi=0x%08x size=0x%08lx arch=0x%08x" \
  12. "\n\tuimage params 0x%08lx-0x%08lx\n"
  13. int optee_verify_image(struct optee_header *hdr, unsigned long tzdram_start,
  14. unsigned long tzdram_len, unsigned long image_len)
  15. {
  16. unsigned long tzdram_end = tzdram_start + tzdram_len;
  17. uint32_t tee_file_size;
  18. tee_file_size = hdr->init_size + hdr->paged_size +
  19. sizeof(struct optee_header);
  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. return 0;
  30. }
  31. int optee_verify_bootm_image(unsigned long image_addr,
  32. unsigned long image_load_addr,
  33. unsigned long image_len)
  34. {
  35. struct optee_header *hdr = (struct optee_header *)image_addr;
  36. unsigned long tzdram_start = CONFIG_OPTEE_TZDRAM_BASE;
  37. unsigned long tzdram_len = CONFIG_OPTEE_TZDRAM_SIZE;
  38. int ret;
  39. ret = optee_verify_image(hdr, tzdram_start, tzdram_len, image_len);
  40. if (ret)
  41. goto error;
  42. if (image_load_addr + sizeof(*hdr) != hdr->init_load_addr_lo) {
  43. ret = -EINVAL;
  44. goto error;
  45. }
  46. return ret;
  47. error:
  48. printf(optee_hdr_err_msg, hdr, image_addr, hdr->magic, tzdram_start,
  49. tzdram_start + tzdram_len, hdr->init_load_addr_lo,
  50. hdr->init_load_addr_hi, image_len, hdr->arch, image_load_addr,
  51. image_load_addr + image_len);
  52. return ret;
  53. }