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