blob: 4c7f44687e89370f710e2690c3bb2a294aae84a6 [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>
Stefan Roese21a1df12020-04-21 09:28:41 +020010#include <spl.h>
11
developeraa1c1d32020-04-21 09:28:45 +020012#include <lzma/LzmaTypes.h>
13#include <lzma/LzmaDec.h>
14#include <lzma/LzmaTools.h>
15
16#define LZMA_LEN (1 << 20)
17
Stefan Roese21a1df12020-04-21 09:28:41 +020018int spl_parse_legacy_header(struct spl_image_info *spl_image,
Simon Glassbb7d3bb2022-09-06 20:26:52 -060019 const struct legacy_img_hdr *header)
Stefan Roese21a1df12020-04-21 09:28:41 +020020{
Simon Glassbb7d3bb2022-09-06 20:26:52 -060021 u32 header_size = sizeof(struct legacy_img_hdr);
Stefan Roese21a1df12020-04-21 09:28:41 +020022
Stefan Roese21a1df12020-04-21 09:28:41 +020023 /* check uImage header CRC */
Stefan Roesefc962c52020-04-21 09:28:42 +020024 if (IS_ENABLED(CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK) &&
25 !image_check_hcrc(header)) {
Stefan Roese21a1df12020-04-21 09:28:41 +020026 puts("SPL: Image header CRC check failed!\n");
27 return -EINVAL;
28 }
Stefan Roese21a1df12020-04-21 09:28:41 +020029
30 if (spl_image->flags & SPL_COPY_PAYLOAD_ONLY) {
31 /*
32 * On some system (e.g. powerpc), the load-address and
33 * entry-point is located at address 0. We can't load
34 * to 0-0x40. So skip header in this case.
35 */
36 spl_image->load_addr = image_get_load(header);
37 spl_image->entry_point = image_get_ep(header);
38 spl_image->size = image_get_data_size(header);
39 } else {
40 spl_image->entry_point = image_get_ep(header);
41 /* Load including the header */
42 spl_image->load_addr = image_get_load(header) -
43 header_size;
44 spl_image->size = image_get_data_size(header) +
45 header_size;
46 }
47
48#ifdef CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK
49 /* store uImage data length and CRC to check later */
50 spl_image->dcrc_data = image_get_load(header);
51 spl_image->dcrc_length = image_get_data_size(header);
52 spl_image->dcrc = image_get_dcrc(header);
53#endif
54
55 spl_image->os = image_get_os(header);
56 spl_image->name = image_get_name(header);
57 debug(SPL_TPL_PROMPT
58 "payload image: %32s load addr: 0x%lx size: %d\n",
59 spl_image->name, spl_image->load_addr, spl_image->size);
60
61 return 0;
62}
Stefan Roese84ae9d82020-04-21 09:28:43 +020063
developeraa1c1d32020-04-21 09:28:45 +020064/*
65 * This function is added explicitly to avoid code size increase, when
66 * no compression method is enabled. The compiler will optimize the
67 * following switch/case statement in spl_load_legacy_img() away due to
68 * Dead Code Elimination.
69 */
Simon Glassbb7d3bb2022-09-06 20:26:52 -060070static inline int spl_image_get_comp(const struct legacy_img_hdr *hdr)
developeraa1c1d32020-04-21 09:28:45 +020071{
72 if (IS_ENABLED(CONFIG_SPL_LZMA))
73 return image_get_comp(hdr);
74
75 return IH_COMP_NONE;
76}
77
Stefan Roese84ae9d82020-04-21 09:28:43 +020078int spl_load_legacy_img(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +010079 struct spl_boot_device *bootdev,
Roger Quadros237f0182022-09-29 13:11:28 +030080 struct spl_load_info *load, ulong offset,
81 struct legacy_img_hdr *hdr)
Stefan Roese84ae9d82020-04-21 09:28:43 +020082{
developeraa1c1d32020-04-21 09:28:45 +020083 __maybe_unused SizeT lzma_len;
84 __maybe_unused void *src;
developeraa1c1d32020-04-21 09:28:45 +020085 ulong dataptr;
Stefan Roese84ae9d82020-04-21 09:28:43 +020086 int ret;
87
developer16e6a2e2022-05-20 11:23:58 +080088 /*
89 * If the payload is compressed, the decompressed data should be
90 * directly write to its load address.
91 */
Roger Quadros237f0182022-09-29 13:11:28 +030092 if (spl_image_get_comp(hdr) != IH_COMP_NONE)
developer16e6a2e2022-05-20 11:23:58 +080093 spl_image->flags |= SPL_COPY_PAYLOAD_ONLY;
94
Roger Quadros237f0182022-09-29 13:11:28 +030095 ret = spl_parse_image_header(spl_image, bootdev, hdr);
Stefan Roese84ae9d82020-04-21 09:28:43 +020096 if (ret)
97 return ret;
98
99 /* Read image */
Roger Quadros237f0182022-09-29 13:11:28 +0300100 switch (spl_image_get_comp(hdr)) {
developeraa1c1d32020-04-21 09:28:45 +0200101 case IH_COMP_NONE:
Roger Quadros237f0182022-09-29 13:11:28 +0300102 dataptr = offset;
developer16e6a2e2022-05-20 11:23:58 +0800103
104 /*
105 * Image header will be skipped only if SPL_COPY_PAYLOAD_ONLY
106 * is set
107 */
108 if (spl_image->flags & SPL_COPY_PAYLOAD_ONLY)
109 dataptr += sizeof(hdr);
110
developeraa1c1d32020-04-21 09:28:45 +0200111 load->read(load, dataptr, spl_image->size,
112 (void *)(unsigned long)spl_image->load_addr);
113 break;
114
115 case IH_COMP_LZMA:
116 lzma_len = LZMA_LEN;
117
developer16e6a2e2022-05-20 11:23:58 +0800118 /* dataptr points to compressed payload */
Roger Quadros237f0182022-09-29 13:11:28 +0300119 dataptr = offset + sizeof(hdr);
developer16e6a2e2022-05-20 11:23:58 +0800120
developeraa1c1d32020-04-21 09:28:45 +0200121 debug("LZMA: Decompressing %08lx to %08lx\n",
122 dataptr, spl_image->load_addr);
123 src = malloc(spl_image->size);
124 if (!src) {
125 printf("Unable to allocate %d bytes for LZMA\n",
126 spl_image->size);
127 return -ENOMEM;
128 }
129
130 load->read(load, dataptr, spl_image->size, src);
131 ret = lzmaBuffToBuffDecompress((void *)spl_image->load_addr,
132 &lzma_len, src, spl_image->size);
133 if (ret) {
134 printf("LZMA decompression error: %d\n", ret);
135 return ret;
136 }
137
138 spl_image->size = lzma_len;
139 break;
140
141 default:
142 debug("Compression method %s is not supported\n",
Roger Quadros237f0182022-09-29 13:11:28 +0300143 genimg_get_comp_short_name(image_get_comp(hdr)));
developeraa1c1d32020-04-21 09:28:45 +0200144 return -EINVAL;
145 }
Stefan Roese84ae9d82020-04-21 09:28:43 +0200146
147 return 0;
148}