Merge tag 'v2022.04-rc5' into next
[platform/kernel/u-boot.git] / common / spl / spl_legacy.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2020 Stefan Roese <sr@denx.de>
4  */
5
6 #include <common.h>
7 #include <image.h>
8 #include <log.h>
9 #include <malloc.h>
10 #include <spl.h>
11
12 #include <lzma/LzmaTypes.h>
13 #include <lzma/LzmaDec.h>
14 #include <lzma/LzmaTools.h>
15
16 #define LZMA_LEN        (1 << 20)
17
18 int 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
23         /* check uImage header CRC */
24         if (IS_ENABLED(CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK) &&
25             !image_check_hcrc(header)) {
26                 puts("SPL: Image header CRC check failed!\n");
27                 return -EINVAL;
28         }
29
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 }
63
64 /*
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  */
70 static 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
78 int spl_load_legacy_img(struct spl_image_info *spl_image,
79                         struct spl_boot_device *bootdev,
80                         struct spl_load_info *load, ulong header)
81 {
82         __maybe_unused SizeT lzma_len;
83         __maybe_unused void *src;
84         struct image_header hdr;
85         ulong dataptr;
86         int ret;
87
88         /* Read header into local struct */
89         load->read(load, header, sizeof(hdr), &hdr);
90
91         ret = spl_parse_image_header(spl_image, bootdev, &hdr);
92         if (ret)
93                 return ret;
94
95         dataptr = header + sizeof(hdr);
96
97         /* Read image */
98         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         }
132
133         return 0;
134 }