blob: 2ec715442389abfdb68968cf7b092d56f40af2be [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,
19 const struct image_header *header)
20{
21 u32 header_size = sizeof(struct image_header);
22
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 */
70static inline int spl_image_get_comp(const struct image_header *hdr)
71{
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,
Stefan Roese84ae9d82020-04-21 09:28:43 +020080 struct spl_load_info *load, ulong header)
81{
developeraa1c1d32020-04-21 09:28:45 +020082 __maybe_unused SizeT lzma_len;
83 __maybe_unused void *src;
Stefan Roese84ae9d82020-04-21 09:28:43 +020084 struct image_header hdr;
developeraa1c1d32020-04-21 09:28:45 +020085 ulong dataptr;
Stefan Roese84ae9d82020-04-21 09:28:43 +020086 int ret;
87
88 /* Read header into local struct */
89 load->read(load, header, sizeof(hdr), &hdr);
90
Pali Rohárdda8f882022-01-14 14:31:38 +010091 ret = spl_parse_image_header(spl_image, bootdev, &hdr);
Stefan Roese84ae9d82020-04-21 09:28:43 +020092 if (ret)
93 return ret;
94
developeraa1c1d32020-04-21 09:28:45 +020095 dataptr = header + sizeof(hdr);
96
Stefan Roese84ae9d82020-04-21 09:28:43 +020097 /* Read image */
developeraa1c1d32020-04-21 09:28:45 +020098 switch (spl_image_get_comp(&hdr)) {
99 case IH_COMP_NONE:
100 load->read(load, dataptr, spl_image->size,
101 (void *)(unsigned long)spl_image->load_addr);
102 break;
103
104 case IH_COMP_LZMA:
105 lzma_len = LZMA_LEN;
106
107 debug("LZMA: Decompressing %08lx to %08lx\n",
108 dataptr, spl_image->load_addr);
109 src = malloc(spl_image->size);
110 if (!src) {
111 printf("Unable to allocate %d bytes for LZMA\n",
112 spl_image->size);
113 return -ENOMEM;
114 }
115
116 load->read(load, dataptr, spl_image->size, src);
117 ret = lzmaBuffToBuffDecompress((void *)spl_image->load_addr,
118 &lzma_len, src, spl_image->size);
119 if (ret) {
120 printf("LZMA decompression error: %d\n", ret);
121 return ret;
122 }
123
124 spl_image->size = lzma_len;
125 break;
126
127 default:
128 debug("Compression method %s is not supported\n",
129 genimg_get_comp_short_name(image_get_comp(&hdr)));
130 return -EINVAL;
131 }
Stefan Roese84ae9d82020-04-21 09:28:43 +0200132
133 return 0;
134}