xref: /rk3399_ARM-atf/drivers/auth/img_parser_mod.c (revision 51faada71a219a8b94cd8d8e423f0f22e9da4d8f)
1 /*
2  * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of ARM nor the names of its contributors may be used
15  * to endorse or promote products derived from this software without specific
16  * prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <assert.h>
32 #include <auth_common.h>
33 #include <debug.h>
34 #include <errno.h>
35 #include <img_parser_mod.h>
36 #include <limits.h>
37 #include <stdint.h>
38 #include <string.h>
39 
40 extern uintptr_t __PARSER_LIB_DESCS_START__;
41 extern uintptr_t __PARSER_LIB_DESCS_END__;
42 #define PARSER_LIB_DESCS_START	((uintptr_t) (&__PARSER_LIB_DESCS_START__))
43 #define PARSER_LIB_DESCS_END	((uintptr_t) (&__PARSER_LIB_DESCS_END__))
44 static unsigned int parser_lib_indices[IMG_MAX_TYPES];
45 static img_parser_lib_desc_t *parser_lib_descs;
46 
47 #define INVALID_IDX		UINT_MAX
48 
49 static void validate_desc(img_parser_lib_desc_t *desc)
50 {
51 	assert(desc != NULL);
52 	assert(desc->init != NULL);
53 	assert(desc->name != NULL);
54 	assert(desc->check_integrity != NULL);
55 	assert(desc->get_auth_param != NULL);
56 }
57 
58 void img_parser_init(void)
59 {
60 	unsigned int index, mod_num;
61 
62 	/* Initialise internal variables to invalid state */
63 	for (index = 0; index < IMG_MAX_TYPES; index++) {
64 		parser_lib_indices[index] = INVALID_IDX;
65 	}
66 
67 	/* Calculate how many image parsers are registered. At least one parser
68 	 * must be present */
69 	mod_num = PARSER_LIB_DESCS_END - PARSER_LIB_DESCS_START;
70 	mod_num /= sizeof(img_parser_lib_desc_t);
71 	assert(mod_num > 0);
72 
73 	parser_lib_descs = (img_parser_lib_desc_t *) PARSER_LIB_DESCS_START;
74 	for (index = 0; index < mod_num; index++) {
75 
76 		/* Check that the image parser library descriptor is valid */
77 		validate_desc(&parser_lib_descs[index]);
78 
79 		/* Initialize image parser */
80 		parser_lib_descs[index].init();
81 
82 		/* Ensure only one parser is registered for each image type */
83 		assert(parser_lib_indices[parser_lib_descs[index].img_type] ==
84 				INVALID_IDX);
85 
86 		/* Keep the index of this hash calculator */
87 		parser_lib_indices[parser_lib_descs[index].img_type] = index;
88 	}
89 }
90 
91 int img_parser_check_integrity(img_type_t img_type,
92 			       void *img_ptr, unsigned int img_len)
93 {
94 	unsigned int idx;
95 
96 	assert(img_ptr != NULL);
97 	assert(img_len != 0);
98 
99 	/* No integrity checks on raw images */
100 	if (img_type == IMG_RAW) {
101 		return IMG_PARSER_OK;
102 	}
103 
104 	/* Find the index of the required image parser */
105 	idx = parser_lib_indices[img_type];
106 	assert(idx != INVALID_IDX);
107 
108 	/* Call the function to check the image integrity */
109 	return parser_lib_descs[idx].check_integrity(img_ptr, img_len);
110 }
111 
112 /*
113  * Extract an authentication parameter from an image
114  *
115  * Parameters:
116  *   img_type: image type (certificate, raw image, etc)
117  *   type_desc: provides info to obtain the parameter
118  *   img_ptr: pointer to image data
119  *   img_len: image length
120  *   param_ptr: [out] stores a pointer to the parameter
121  *   param_len: [out] stores the length of the parameter
122  */
123 int img_parser_get_auth_param(img_type_t img_type,
124 			      const auth_param_type_desc_t *type_desc,
125 			      void *img_ptr, unsigned int img_len,
126 			      void **param_ptr, unsigned int *param_len)
127 {
128 	unsigned int idx;
129 
130 	assert(type_desc != NULL);
131 	assert(img_ptr != NULL);
132 	assert(img_len != 0);
133 	assert(param_ptr != NULL);
134 	assert(param_len != NULL);
135 
136 	/* In a raw image we can only get the data itself */
137 	if (img_type == IMG_RAW) {
138 		assert(type_desc->type == AUTH_PARAM_RAW_DATA);
139 		*param_ptr = img_ptr;
140 		*param_len = img_len;
141 		return IMG_PARSER_OK;
142 	}
143 
144 	/* Find the index of the required image parser library */
145 	idx = parser_lib_indices[img_type];
146 	assert(idx != INVALID_IDX);
147 
148 	/* Call the function to obtain the parameter */
149 	return parser_lib_descs[idx].get_auth_param(type_desc, img_ptr, img_len,
150 			param_ptr, param_len);
151 }
152