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