mbedtls_common.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (c) 2015-2023, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <stddef.h>
  8. /* mbed TLS headers */
  9. #include <mbedtls/memory_buffer_alloc.h>
  10. #include <mbedtls/platform.h>
  11. #include <mbedtls/version.h>
  12. #include <common/debug.h>
  13. #include <drivers/auth/mbedtls/mbedtls_common.h>
  14. #include <plat/common/platform.h>
  15. static void cleanup(void)
  16. {
  17. ERROR("EXIT from BL2\n");
  18. panic();
  19. }
  20. /*
  21. * mbed TLS initialization function
  22. */
  23. void mbedtls_init(void)
  24. {
  25. static int ready;
  26. void *heap_addr;
  27. size_t heap_size = 0;
  28. int err;
  29. if (!ready) {
  30. if (atexit(cleanup))
  31. panic();
  32. err = plat_get_mbedtls_heap(&heap_addr, &heap_size);
  33. /* Ensure heap setup is proper */
  34. if (err < 0) {
  35. ERROR("Mbed TLS failed to get a heap\n");
  36. panic();
  37. }
  38. assert(heap_size >= TF_MBEDTLS_HEAP_SIZE);
  39. /* Initialize the mbed TLS heap */
  40. mbedtls_memory_buffer_alloc_init(heap_addr, heap_size);
  41. #ifdef MBEDTLS_PLATFORM_SNPRINTF_ALT
  42. mbedtls_platform_set_snprintf(snprintf);
  43. #endif
  44. ready = 1;
  45. }
  46. }
  47. /*
  48. * The following helper function simply returns the default allocated heap.
  49. * It can be used by platforms for their plat_get_mbedtls_heap() implementation.
  50. */
  51. int get_mbedtls_heap_helper(void **heap_addr, size_t *heap_size)
  52. {
  53. static unsigned char heap[TF_MBEDTLS_HEAP_SIZE];
  54. assert(heap_addr != NULL);
  55. assert(heap_size != NULL);
  56. *heap_addr = heap;
  57. *heap_size = sizeof(heap);
  58. return 0;
  59. }