img_parser_mod.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2015-2020, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <limits.h>
  8. #include <stdint.h>
  9. #include <string.h>
  10. #include <common/debug.h>
  11. #include <drivers/auth/auth_common.h>
  12. #include <drivers/auth/img_parser_mod.h>
  13. #include <lib/utils_def.h>
  14. IMPORT_SYM(uintptr_t, __PARSER_LIB_DESCS_START__, PARSER_LIB_DESCS_START);
  15. IMPORT_SYM(uintptr_t, __PARSER_LIB_DESCS_END__, PARSER_LIB_DESCS_END);
  16. static unsigned int parser_lib_indices[IMG_MAX_TYPES];
  17. static img_parser_lib_desc_t *parser_lib_descs;
  18. #define INVALID_IDX UINT_MAX
  19. static void validate_desc(img_parser_lib_desc_t *desc)
  20. {
  21. assert(desc != NULL);
  22. assert(desc->init != NULL);
  23. assert(desc->name != NULL);
  24. assert(desc->check_integrity != NULL);
  25. assert(desc->get_auth_param != NULL);
  26. }
  27. void img_parser_init(void)
  28. {
  29. unsigned int index, mod_num;
  30. /* Initialise internal variables to invalid state */
  31. for (index = 0; index < IMG_MAX_TYPES; index++) {
  32. parser_lib_indices[index] = INVALID_IDX;
  33. }
  34. /* Calculate how many image parsers are registered. At least one parser
  35. * must be present */
  36. mod_num = PARSER_LIB_DESCS_END - PARSER_LIB_DESCS_START;
  37. mod_num /= sizeof(img_parser_lib_desc_t);
  38. assert(mod_num > 0);
  39. parser_lib_descs = (img_parser_lib_desc_t *) PARSER_LIB_DESCS_START;
  40. for (index = 0; index < mod_num; index++) {
  41. /* Check that the image parser library descriptor is valid */
  42. validate_desc(&parser_lib_descs[index]);
  43. /* Initialize image parser */
  44. parser_lib_descs[index].init();
  45. /* Ensure only one parser is registered for each image type */
  46. assert(parser_lib_indices[parser_lib_descs[index].img_type] ==
  47. INVALID_IDX);
  48. /* Keep the index of this hash calculator */
  49. parser_lib_indices[parser_lib_descs[index].img_type] = index;
  50. }
  51. }
  52. int img_parser_check_integrity(img_type_t img_type,
  53. void *img_ptr, unsigned int img_len)
  54. {
  55. unsigned int idx;
  56. assert(img_ptr != NULL);
  57. assert(img_len != 0);
  58. /* No integrity checks on raw images */
  59. if (img_type == IMG_RAW) {
  60. return IMG_PARSER_OK;
  61. }
  62. /* Find the index of the required image parser */
  63. idx = parser_lib_indices[img_type];
  64. assert(idx != INVALID_IDX);
  65. /* Call the function to check the image integrity */
  66. return parser_lib_descs[idx].check_integrity(img_ptr, img_len);
  67. }
  68. /*
  69. * Extract an authentication parameter from an image
  70. *
  71. * Parameters:
  72. * img_type: image type (certificate, raw image, etc)
  73. * type_desc: provides info to obtain the parameter
  74. * img_ptr: pointer to image data
  75. * img_len: image length
  76. * param_ptr: [out] stores a pointer to the parameter
  77. * param_len: [out] stores the length of the parameter
  78. */
  79. int img_parser_get_auth_param(img_type_t img_type,
  80. const auth_param_type_desc_t *type_desc,
  81. void *img_ptr, unsigned int img_len,
  82. void **param_ptr, unsigned int *param_len)
  83. {
  84. unsigned int idx;
  85. assert(type_desc != NULL);
  86. assert(img_ptr != NULL);
  87. assert(img_len != 0);
  88. assert(param_ptr != NULL);
  89. assert(param_len != NULL);
  90. /* In a raw image we can only get the data itself */
  91. if (img_type == IMG_RAW) {
  92. assert(type_desc->type == AUTH_PARAM_RAW_DATA);
  93. *param_ptr = img_ptr;
  94. *param_len = img_len;
  95. return IMG_PARSER_OK;
  96. }
  97. /* Find the index of the required image parser library */
  98. idx = parser_lib_indices[img_type];
  99. assert(idx != INVALID_IDX);
  100. /* Call the function to obtain the parameter */
  101. return parser_lib_descs[idx].get_auth_param(type_desc, img_ptr, img_len,
  102. param_ptr, param_len);
  103. }