blob: a561939b4f046a66e90902fde6977b19442e9f92 [file] [log] [blame]
Stefan Roese21a1df12020-04-21 09:28:41 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2020 Stefan Roese <sr@denx.de>
4 */
5
6#include <common.h>
Simon Glass2dc9c342020-05-10 11:40:01 -06007#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
developeraa1c1d32020-04-21 09:28:45 +02009#include <malloc.h>
Sean Anderson5ff77722023-10-14 16:47:55 -040010#include <mapmem.h>
Marek Vasut5be531b2023-05-29 14:04:06 +020011#include <asm/sections.h>
Stefan Roese21a1df12020-04-21 09:28:41 +020012#include <spl.h>
13
developeraa1c1d32020-04-21 09:28:45 +020014#include <lzma/LzmaTypes.h>
15#include <lzma/LzmaDec.h>
16#include <lzma/LzmaTools.h>
17
18#define LZMA_LEN (1 << 20)
19
Marek Vasut5be531b2023-05-29 14:04:06 +020020static void spl_parse_legacy_validate(uintptr_t start, uintptr_t size)
21{
22 uintptr_t spl_start = (uintptr_t)_start;
Sean Andersonfd4b5302023-10-14 16:47:37 -040023 uintptr_t spl_end = (uintptr_t)&_image_binary_end;
Marek Vasut5be531b2023-05-29 14:04:06 +020024 uintptr_t end = start + size;
25
26 if ((start >= spl_start && start < spl_end) ||
27 (end > spl_start && end <= spl_end) ||
28 (start < spl_start && end >= spl_end) ||
29 (start > end && end > spl_start))
30 panic("SPL: Image overlaps SPL\n");
31
32 if (size > CONFIG_SYS_BOOTM_LEN)
33 panic("SPL: Image too large\n");
34}
35
Stefan Roese21a1df12020-04-21 09:28:41 +020036int spl_parse_legacy_header(struct spl_image_info *spl_image,
Simon Glassbb7d3bb2022-09-06 20:26:52 -060037 const struct legacy_img_hdr *header)
Stefan Roese21a1df12020-04-21 09:28:41 +020038{
Simon Glassbb7d3bb2022-09-06 20:26:52 -060039 u32 header_size = sizeof(struct legacy_img_hdr);
Stefan Roese21a1df12020-04-21 09:28:41 +020040
Stefan Roese21a1df12020-04-21 09:28:41 +020041 /* check uImage header CRC */
Stefan Roesefc962c52020-04-21 09:28:42 +020042 if (IS_ENABLED(CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK) &&
43 !image_check_hcrc(header)) {
Stefan Roese21a1df12020-04-21 09:28:41 +020044 puts("SPL: Image header CRC check failed!\n");
45 return -EINVAL;
46 }
Stefan Roese21a1df12020-04-21 09:28:41 +020047
48 if (spl_image->flags & SPL_COPY_PAYLOAD_ONLY) {
49 /*
50 * On some system (e.g. powerpc), the load-address and
51 * entry-point is located at address 0. We can't load
52 * to 0-0x40. So skip header in this case.
53 */
54 spl_image->load_addr = image_get_load(header);
55 spl_image->entry_point = image_get_ep(header);
56 spl_image->size = image_get_data_size(header);
57 } else {
58 spl_image->entry_point = image_get_ep(header);
59 /* Load including the header */
60 spl_image->load_addr = image_get_load(header) -
61 header_size;
62 spl_image->size = image_get_data_size(header) +
63 header_size;
64 }
65
66#ifdef CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK
67 /* store uImage data length and CRC to check later */
68 spl_image->dcrc_data = image_get_load(header);
69 spl_image->dcrc_length = image_get_data_size(header);
70 spl_image->dcrc = image_get_dcrc(header);
71#endif
72
73 spl_image->os = image_get_os(header);
74 spl_image->name = image_get_name(header);
75 debug(SPL_TPL_PROMPT
76 "payload image: %32s load addr: 0x%lx size: %d\n",
77 spl_image->name, spl_image->load_addr, spl_image->size);
78
Marek Vasut5be531b2023-05-29 14:04:06 +020079 spl_parse_legacy_validate(spl_image->load_addr, spl_image->size);
80 spl_parse_legacy_validate(spl_image->entry_point, 0);
81
Stefan Roese21a1df12020-04-21 09:28:41 +020082 return 0;
83}
Stefan Roese84ae9d82020-04-21 09:28:43 +020084
Sean Andersonce9ffb92023-11-08 11:48:45 -050085int spl_load_legacy_lzma(struct spl_image_info *spl_image,
86 struct spl_load_info *load, ulong offset)
87{
88 SizeT lzma_len = LZMA_LEN;
89 void *src;
90 ulong dataptr, overhead, size;
91 int ret;
92
93 /* dataptr points to compressed payload */
94 dataptr = ALIGN_DOWN(sizeof(struct legacy_img_hdr),
95 spl_get_bl_len(load));
96 overhead = sizeof(struct legacy_img_hdr) - dataptr;
97 size = ALIGN(spl_image->size + overhead, spl_get_bl_len(load));
98 dataptr += offset;
99
100 debug("LZMA: Decompressing %08lx to %08lx\n",
101 dataptr, spl_image->load_addr);
102 src = malloc(size);
103 if (!src) {
104 printf("Unable to allocate %d bytes for LZMA\n",
105 spl_image->size);
106 return -ENOMEM;
107 }
108
109 load->read(load, dataptr, size, src);
110 ret = lzmaBuffToBuffDecompress(map_sysmem(spl_image->load_addr,
111 spl_image->size), &lzma_len,
112 src + overhead, spl_image->size);
113 if (ret) {
114 printf("LZMA decompression error: %d\n", ret);
115 return ret;
116 }
117
118 spl_image->size = lzma_len;
119 return 0;
120}
121
developeraa1c1d32020-04-21 09:28:45 +0200122/*
123 * This function is added explicitly to avoid code size increase, when
124 * no compression method is enabled. The compiler will optimize the
125 * following switch/case statement in spl_load_legacy_img() away due to
126 * Dead Code Elimination.
127 */
Simon Glassbb7d3bb2022-09-06 20:26:52 -0600128static inline int spl_image_get_comp(const struct legacy_img_hdr *hdr)
developeraa1c1d32020-04-21 09:28:45 +0200129{
130 if (IS_ENABLED(CONFIG_SPL_LZMA))
131 return image_get_comp(hdr);
132
133 return IH_COMP_NONE;
134}
135
Stefan Roese84ae9d82020-04-21 09:28:43 +0200136int spl_load_legacy_img(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100137 struct spl_boot_device *bootdev,
Roger Quadros237f0182022-09-29 13:11:28 +0300138 struct spl_load_info *load, ulong offset,
139 struct legacy_img_hdr *hdr)
Stefan Roese84ae9d82020-04-21 09:28:43 +0200140{
developeraa1c1d32020-04-21 09:28:45 +0200141 ulong dataptr;
Stefan Roese84ae9d82020-04-21 09:28:43 +0200142 int ret;
143
developer16e6a2e2022-05-20 11:23:58 +0800144 /*
145 * If the payload is compressed, the decompressed data should be
146 * directly write to its load address.
147 */
Roger Quadros237f0182022-09-29 13:11:28 +0300148 if (spl_image_get_comp(hdr) != IH_COMP_NONE)
developer16e6a2e2022-05-20 11:23:58 +0800149 spl_image->flags |= SPL_COPY_PAYLOAD_ONLY;
150
Roger Quadros237f0182022-09-29 13:11:28 +0300151 ret = spl_parse_image_header(spl_image, bootdev, hdr);
Stefan Roese84ae9d82020-04-21 09:28:43 +0200152 if (ret)
153 return ret;
154
155 /* Read image */
Roger Quadros237f0182022-09-29 13:11:28 +0300156 switch (spl_image_get_comp(hdr)) {
developeraa1c1d32020-04-21 09:28:45 +0200157 case IH_COMP_NONE:
Roger Quadros237f0182022-09-29 13:11:28 +0300158 dataptr = offset;
developer16e6a2e2022-05-20 11:23:58 +0800159
160 /*
161 * Image header will be skipped only if SPL_COPY_PAYLOAD_ONLY
162 * is set
163 */
164 if (spl_image->flags & SPL_COPY_PAYLOAD_ONLY)
Dai Okamura08b72b52022-12-09 20:40:21 +0900165 dataptr += sizeof(*hdr);
developer16e6a2e2022-05-20 11:23:58 +0800166
developeraa1c1d32020-04-21 09:28:45 +0200167 load->read(load, dataptr, spl_image->size,
Sean Anderson5ff77722023-10-14 16:47:55 -0400168 map_sysmem(spl_image->load_addr, spl_image->size));
developeraa1c1d32020-04-21 09:28:45 +0200169 break;
170
Sean Andersonce9ffb92023-11-08 11:48:45 -0500171 case IH_COMP_LZMA:
172 return spl_load_legacy_lzma(spl_image, load, offset);
developer16e6a2e2022-05-20 11:23:58 +0800173
developeraa1c1d32020-04-21 09:28:45 +0200174 default:
175 debug("Compression method %s is not supported\n",
Roger Quadros237f0182022-09-29 13:11:28 +0300176 genimg_get_comp_short_name(image_get_comp(hdr)));
developeraa1c1d32020-04-21 09:28:45 +0200177 return -EINVAL;
178 }
Stefan Roese84ae9d82020-04-21 09:28:43 +0200179
180 return 0;
181}