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