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