blob: 51011e41f0e44ee10f4ab6c4d9a74bd8524b1b34 [file] [log] [blame]
Pankaj Gupta0476cb32020-12-09 14:02:40 +05301/*
Jiafei Pan08b12652022-03-29 15:01:09 +08002 * Copyright 2018-2022 NXP
Pankaj Gupta0476cb32020-12-09 14:02:40 +05303 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#include <assert.h>
9
10#include <common/bl_common.h>
11#include <common/desc_image_load.h>
12#include <lib/xlat_tables/xlat_tables_v2.h>
13
14#include "load_img.h"
15
16/******************************************************************************
17 * This function can be used to load DDR PHY/FUSE Images
18 *
19 * @param [in] image_id Image ID to be loaded
20 *
21 * @param [in,out] image_base Location at which the image should be loaded
22 * In case image is prepended by a CSF header,
23 * image_base is pointer to actual image after
24 * the header
25 *
26 * @param [in,out] image_size User should pass the maximum size of the image
27 * possible.(Buffer size starting from image_base)
28 * Actual size of the image loaded is returned
29 * back.
30 *****************************************************************************/
31int load_img(unsigned int image_id, uintptr_t *image_base,
32 uint32_t *image_size)
33{
34 int err = 0;
35
36 image_desc_t img_info = {
37 .image_id = image_id,
38 SET_STATIC_PARAM_HEAD(image_info, PARAM_IMAGE_BINARY,
39 VERSION_2, image_info_t, 0),
40#ifdef CSF_HEADER_PREPENDED
41 .image_info.image_base = *image_base - CSF_HDR_SZ,
42 .image_info.image_max_size = *image_size + CSF_HDR_SZ,
43#else
44 .image_info.image_base = *image_base,
45 .image_info.image_max_size = *image_size,
46#endif
47 };
48
49 /* Create MMU entry for the CSF header */
50#if PLAT_XLAT_TABLES_DYNAMIC
51#ifdef CSF_HEADER_PREPENDED
Jiafei Pan08b12652022-03-29 15:01:09 +080052 err = mmap_add_dynamic_region(img_info.image_info.image_base,
Pankaj Gupta0476cb32020-12-09 14:02:40 +053053 img_info.image_info.image_base,
54 CSF_HDR_SZ,
55 MT_MEMORY | MT_RW | MT_SECURE);
Jiafei Pan08b12652022-03-29 15:01:09 +080056 if (err != 0) {
57 ERROR("Failed to add dynamic memory region.\n");
58 return err;
59 }
Pankaj Gupta0476cb32020-12-09 14:02:40 +053060#endif
61#endif
62
63 VERBOSE("BL2: Loading IMG %d\n", image_id);
64 err = load_auth_image(image_id, &img_info.image_info);
65 if (err != 0) {
66 VERBOSE("Failed to load IMG %d\n", image_id);
67 return err;
68 }
69
70#ifdef CSF_HEADER_PREPENDED
71 *image_base = img_info.image_info.image_base + CSF_HDR_SZ;
72 *image_size = img_info.image_info.image_size - CSF_HDR_SZ;
73#if PLAT_XLAT_TABLES_DYNAMIC
74 mmap_remove_dynamic_region(img_info.image_info.image_base,
75 CSF_HDR_SZ);
76#endif
77#else
78 *image_base = img_info.image_info.image_base;
79 *image_size = img_info.image_info.image_size;
80#endif
81
82 return err;
83}