blob: 82d032680616626c81d905a0fc7651f9802b8f00 [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>
Simon Glassf7ae49f2020-05-10 11:40:05 -06008#include <log.h>
Weijie Gaoaba6e772020-04-21 09:28:45 +02009#include <malloc.h>
Stefan Roesec1108172020-04-21 09:28:41 +020010#include <spl.h>
11
Weijie Gaoaba6e772020-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 Roesec1108172020-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 Roesec1108172020-04-21 09:28:41 +020023 /* check uImage header CRC */
Stefan Roese5ae526b2020-04-21 09:28:42 +020024 if (IS_ENABLED(CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK) &&
25 !image_check_hcrc(header)) {
Stefan Roesec1108172020-04-21 09:28:41 +020026 puts("SPL: Image header CRC check failed!\n");
27 return -EINVAL;
28 }
Stefan Roesec1108172020-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 Roese2fc91ed2020-04-21 09:28:43 +020063
Weijie Gaoaba6e772020-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 Roese2fc91ed2020-04-21 09:28:43 +020078int spl_load_legacy_img(struct spl_image_info *spl_image,
79 struct spl_load_info *load, ulong header)
80{
Weijie Gaoaba6e772020-04-21 09:28:45 +020081 __maybe_unused SizeT lzma_len;
82 __maybe_unused void *src;
Stefan Roese2fc91ed2020-04-21 09:28:43 +020083 struct image_header hdr;
Weijie Gaoaba6e772020-04-21 09:28:45 +020084 ulong dataptr;
Stefan Roese2fc91ed2020-04-21 09:28:43 +020085 int ret;
86
87 /* Read header into local struct */
88 load->read(load, header, sizeof(hdr), &hdr);
89
90 ret = spl_parse_image_header(spl_image, &hdr);
91 if (ret)
92 return ret;
93
Weijie Gaoaba6e772020-04-21 09:28:45 +020094 dataptr = header + sizeof(hdr);
95
Stefan Roese2fc91ed2020-04-21 09:28:43 +020096 /* Read image */
Weijie Gaoaba6e772020-04-21 09:28:45 +020097 switch (spl_image_get_comp(&hdr)) {
98 case IH_COMP_NONE:
99 load->read(load, dataptr, spl_image->size,
100 (void *)(unsigned long)spl_image->load_addr);
101 break;
102
103 case IH_COMP_LZMA:
104 lzma_len = LZMA_LEN;
105
106 debug("LZMA: Decompressing %08lx to %08lx\n",
107 dataptr, spl_image->load_addr);
108 src = malloc(spl_image->size);
109 if (!src) {
110 printf("Unable to allocate %d bytes for LZMA\n",
111 spl_image->size);
112 return -ENOMEM;
113 }
114
115 load->read(load, dataptr, spl_image->size, src);
116 ret = lzmaBuffToBuffDecompress((void *)spl_image->load_addr,
117 &lzma_len, src, spl_image->size);
118 if (ret) {
119 printf("LZMA decompression error: %d\n", ret);
120 return ret;
121 }
122
123 spl_image->size = lzma_len;
124 break;
125
126 default:
127 debug("Compression method %s is not supported\n",
128 genimg_get_comp_short_name(image_get_comp(&hdr)));
129 return -EINVAL;
130 }
Stefan Roese2fc91ed2020-04-21 09:28:43 +0200131
132 return 0;
133}