blob: 535695d829a41dfd83b8d0423f94759db0fa8e8d [file] [log] [blame]
Juan Castillo8e55d932015-04-02 09:48:16 +01001/*
Zelalem87675d42020-02-03 14:56:42 -06002 * Copyright (c) 2015-2020, ARM Limited and Contributors. All rights reserved.
Juan Castillo8e55d932015-04-02 09:48:16 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Juan Castillo8e55d932015-04-02 09:48:16 +01005 */
6
7#include <assert.h>
Juan Castillo8e55d932015-04-02 09:48:16 +01008#include <limits.h>
9#include <stdint.h>
10#include <string.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000011
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>
Juan Castillo8e55d932015-04-02 09:48:16 +010016
Joel Hutton5cc3bc82018-03-21 11:40:57 +000017IMPORT_SYM(uintptr_t, __PARSER_LIB_DESCS_START__, PARSER_LIB_DESCS_START);
18IMPORT_SYM(uintptr_t, __PARSER_LIB_DESCS_END__, PARSER_LIB_DESCS_END);
Juan Castillo8e55d932015-04-02 09:48:16 +010019static unsigned int parser_lib_indices[IMG_MAX_TYPES];
20static img_parser_lib_desc_t *parser_lib_descs;
21
22#define INVALID_IDX UINT_MAX
23
24static 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
33void 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
66int 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 */
98int 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}