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