blob: 29d3ec70734dedefb2253ee39d1ef8732eed3d31 [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>
developeraa1c1d32020-04-21 09:28:45 +02007#include <malloc.h>
Stefan Roese21a1df12020-04-21 09:28:41 +02008#include <spl.h>
9
developeraa1c1d32020-04-21 09:28:45 +020010#include <lzma/LzmaTypes.h>
11#include <lzma/LzmaDec.h>
12#include <lzma/LzmaTools.h>
13
14#define LZMA_LEN (1 << 20)
15
Stefan Roese21a1df12020-04-21 09:28:41 +020016int spl_parse_legacy_header(struct spl_image_info *spl_image,
17 const struct image_header *header)
18{
19 u32 header_size = sizeof(struct image_header);
20
Stefan Roese21a1df12020-04-21 09:28:41 +020021 /* check uImage header CRC */
Stefan Roesefc962c52020-04-21 09:28:42 +020022 if (IS_ENABLED(CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK) &&
23 !image_check_hcrc(header)) {
Stefan Roese21a1df12020-04-21 09:28:41 +020024 puts("SPL: Image header CRC check failed!\n");
25 return -EINVAL;
26 }
Stefan Roese21a1df12020-04-21 09:28:41 +020027
28 if (spl_image->flags & SPL_COPY_PAYLOAD_ONLY) {
29 /*
30 * On some system (e.g. powerpc), the load-address and
31 * entry-point is located at address 0. We can't load
32 * to 0-0x40. So skip header in this case.
33 */
34 spl_image->load_addr = image_get_load(header);
35 spl_image->entry_point = image_get_ep(header);
36 spl_image->size = image_get_data_size(header);
37 } else {
38 spl_image->entry_point = image_get_ep(header);
39 /* Load including the header */
40 spl_image->load_addr = image_get_load(header) -
41 header_size;
42 spl_image->size = image_get_data_size(header) +
43 header_size;
44 }
45
46#ifdef CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK
47 /* store uImage data length and CRC to check later */
48 spl_image->dcrc_data = image_get_load(header);
49 spl_image->dcrc_length = image_get_data_size(header);
50 spl_image->dcrc = image_get_dcrc(header);
51#endif
52
53 spl_image->os = image_get_os(header);
54 spl_image->name = image_get_name(header);
55 debug(SPL_TPL_PROMPT
56 "payload image: %32s load addr: 0x%lx size: %d\n",
57 spl_image->name, spl_image->load_addr, spl_image->size);
58
59 return 0;
60}
Stefan Roese84ae9d82020-04-21 09:28:43 +020061
developeraa1c1d32020-04-21 09:28:45 +020062/*
63 * This function is added explicitly to avoid code size increase, when
64 * no compression method is enabled. The compiler will optimize the
65 * following switch/case statement in spl_load_legacy_img() away due to
66 * Dead Code Elimination.
67 */
68static inline int spl_image_get_comp(const struct image_header *hdr)
69{
70 if (IS_ENABLED(CONFIG_SPL_LZMA))
71 return image_get_comp(hdr);
72
73 return IH_COMP_NONE;
74}
75
Stefan Roese84ae9d82020-04-21 09:28:43 +020076int spl_load_legacy_img(struct spl_image_info *spl_image,
77 struct spl_load_info *load, ulong header)
78{
developeraa1c1d32020-04-21 09:28:45 +020079 __maybe_unused SizeT lzma_len;
80 __maybe_unused void *src;
Stefan Roese84ae9d82020-04-21 09:28:43 +020081 struct image_header hdr;
developeraa1c1d32020-04-21 09:28:45 +020082 ulong dataptr;
Stefan Roese84ae9d82020-04-21 09:28:43 +020083 int ret;
84
85 /* Read header into local struct */
86 load->read(load, header, sizeof(hdr), &hdr);
87
88 ret = spl_parse_image_header(spl_image, &hdr);
89 if (ret)
90 return ret;
91
developeraa1c1d32020-04-21 09:28:45 +020092 dataptr = header + sizeof(hdr);
93
Stefan Roese84ae9d82020-04-21 09:28:43 +020094 /* Read image */
developeraa1c1d32020-04-21 09:28:45 +020095 switch (spl_image_get_comp(&hdr)) {
96 case IH_COMP_NONE:
97 load->read(load, dataptr, spl_image->size,
98 (void *)(unsigned long)spl_image->load_addr);
99 break;
100
101 case IH_COMP_LZMA:
102 lzma_len = LZMA_LEN;
103
104 debug("LZMA: Decompressing %08lx to %08lx\n",
105 dataptr, spl_image->load_addr);
106 src = malloc(spl_image->size);
107 if (!src) {
108 printf("Unable to allocate %d bytes for LZMA\n",
109 spl_image->size);
110 return -ENOMEM;
111 }
112
113 load->read(load, dataptr, spl_image->size, src);
114 ret = lzmaBuffToBuffDecompress((void *)spl_image->load_addr,
115 &lzma_len, src, spl_image->size);
116 if (ret) {
117 printf("LZMA decompression error: %d\n", ret);
118 return ret;
119 }
120
121 spl_image->size = lzma_len;
122 break;
123
124 default:
125 debug("Compression method %s is not supported\n",
126 genimg_get_comp_short_name(image_get_comp(&hdr)));
127 return -EINVAL;
128 }
Stefan Roese84ae9d82020-04-21 09:28:43 +0200129
130 return 0;
131}