Stefan Roese | c110817 | 2020-04-21 09:28:41 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2020 Stefan Roese <sr@denx.de> |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | 4d72caa | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 7 | #include <image.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
Weijie Gao | aba6e77 | 2020-04-21 09:28:45 +0200 | [diff] [blame] | 9 | #include <malloc.h> |
Marek Vasut | 77aed22 | 2023-05-29 14:04:06 +0200 | [diff] [blame] | 10 | #include <asm/sections.h> |
Stefan Roese | c110817 | 2020-04-21 09:28:41 +0200 | [diff] [blame] | 11 | #include <spl.h> |
| 12 | |
Weijie Gao | aba6e77 | 2020-04-21 09:28:45 +0200 | [diff] [blame] | 13 | #include <lzma/LzmaTypes.h> |
| 14 | #include <lzma/LzmaDec.h> |
| 15 | #include <lzma/LzmaTools.h> |
| 16 | |
| 17 | #define LZMA_LEN (1 << 20) |
| 18 | |
Marek Vasut | 77aed22 | 2023-05-29 14:04:06 +0200 | [diff] [blame] | 19 | static void spl_parse_legacy_validate(uintptr_t start, uintptr_t size) |
| 20 | { |
| 21 | uintptr_t spl_start = (uintptr_t)_start; |
Fabio Estevam | 1b8a1be | 2023-06-30 23:30:53 -0300 | [diff] [blame] | 22 | uintptr_t spl_end = (uintptr_t)_image_binary_end; |
Marek Vasut | 77aed22 | 2023-05-29 14:04:06 +0200 | [diff] [blame] | 23 | uintptr_t end = start + size; |
| 24 | |
| 25 | if ((start >= spl_start && start < spl_end) || |
| 26 | (end > spl_start && end <= spl_end) || |
| 27 | (start < spl_start && end >= spl_end) || |
| 28 | (start > end && end > spl_start)) |
| 29 | panic("SPL: Image overlaps SPL\n"); |
| 30 | |
| 31 | if (size > CONFIG_SYS_BOOTM_LEN) |
| 32 | panic("SPL: Image too large\n"); |
| 33 | } |
| 34 | |
Stefan Roese | c110817 | 2020-04-21 09:28:41 +0200 | [diff] [blame] | 35 | int spl_parse_legacy_header(struct spl_image_info *spl_image, |
Simon Glass | f3543e6 | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 36 | const struct legacy_img_hdr *header) |
Stefan Roese | c110817 | 2020-04-21 09:28:41 +0200 | [diff] [blame] | 37 | { |
Simon Glass | f3543e6 | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 38 | u32 header_size = sizeof(struct legacy_img_hdr); |
Stefan Roese | c110817 | 2020-04-21 09:28:41 +0200 | [diff] [blame] | 39 | |
Stefan Roese | c110817 | 2020-04-21 09:28:41 +0200 | [diff] [blame] | 40 | /* check uImage header CRC */ |
Stefan Roese | 5ae526b | 2020-04-21 09:28:42 +0200 | [diff] [blame] | 41 | if (IS_ENABLED(CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK) && |
| 42 | !image_check_hcrc(header)) { |
Stefan Roese | c110817 | 2020-04-21 09:28:41 +0200 | [diff] [blame] | 43 | puts("SPL: Image header CRC check failed!\n"); |
| 44 | return -EINVAL; |
| 45 | } |
Stefan Roese | c110817 | 2020-04-21 09:28:41 +0200 | [diff] [blame] | 46 | |
| 47 | if (spl_image->flags & SPL_COPY_PAYLOAD_ONLY) { |
| 48 | /* |
| 49 | * On some system (e.g. powerpc), the load-address and |
| 50 | * entry-point is located at address 0. We can't load |
| 51 | * to 0-0x40. So skip header in this case. |
| 52 | */ |
| 53 | spl_image->load_addr = image_get_load(header); |
| 54 | spl_image->entry_point = image_get_ep(header); |
| 55 | spl_image->size = image_get_data_size(header); |
| 56 | } else { |
| 57 | spl_image->entry_point = image_get_ep(header); |
| 58 | /* Load including the header */ |
| 59 | spl_image->load_addr = image_get_load(header) - |
| 60 | header_size; |
| 61 | spl_image->size = image_get_data_size(header) + |
| 62 | header_size; |
| 63 | } |
| 64 | |
| 65 | #ifdef CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK |
| 66 | /* store uImage data length and CRC to check later */ |
| 67 | spl_image->dcrc_data = image_get_load(header); |
| 68 | spl_image->dcrc_length = image_get_data_size(header); |
| 69 | spl_image->dcrc = image_get_dcrc(header); |
| 70 | #endif |
| 71 | |
| 72 | spl_image->os = image_get_os(header); |
| 73 | spl_image->name = image_get_name(header); |
| 74 | debug(SPL_TPL_PROMPT |
| 75 | "payload image: %32s load addr: 0x%lx size: %d\n", |
| 76 | spl_image->name, spl_image->load_addr, spl_image->size); |
| 77 | |
Marek Vasut | 77aed22 | 2023-05-29 14:04:06 +0200 | [diff] [blame] | 78 | spl_parse_legacy_validate(spl_image->load_addr, spl_image->size); |
| 79 | spl_parse_legacy_validate(spl_image->entry_point, 0); |
| 80 | |
Stefan Roese | c110817 | 2020-04-21 09:28:41 +0200 | [diff] [blame] | 81 | return 0; |
| 82 | } |
Stefan Roese | 2fc91ed | 2020-04-21 09:28:43 +0200 | [diff] [blame] | 83 | |
Weijie Gao | aba6e77 | 2020-04-21 09:28:45 +0200 | [diff] [blame] | 84 | /* |
| 85 | * This function is added explicitly to avoid code size increase, when |
| 86 | * no compression method is enabled. The compiler will optimize the |
| 87 | * following switch/case statement in spl_load_legacy_img() away due to |
| 88 | * Dead Code Elimination. |
| 89 | */ |
Simon Glass | f3543e6 | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 90 | static inline int spl_image_get_comp(const struct legacy_img_hdr *hdr) |
Weijie Gao | aba6e77 | 2020-04-21 09:28:45 +0200 | [diff] [blame] | 91 | { |
| 92 | if (IS_ENABLED(CONFIG_SPL_LZMA)) |
| 93 | return image_get_comp(hdr); |
| 94 | |
| 95 | return IH_COMP_NONE; |
| 96 | } |
| 97 | |
Stefan Roese | 2fc91ed | 2020-04-21 09:28:43 +0200 | [diff] [blame] | 98 | int spl_load_legacy_img(struct spl_image_info *spl_image, |
Pali Rohár | 2e0429b | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 99 | struct spl_boot_device *bootdev, |
Roger Quadros | 06377c5 | 2022-09-29 13:11:28 +0300 | [diff] [blame] | 100 | struct spl_load_info *load, ulong offset, |
| 101 | struct legacy_img_hdr *hdr) |
Stefan Roese | 2fc91ed | 2020-04-21 09:28:43 +0200 | [diff] [blame] | 102 | { |
Weijie Gao | aba6e77 | 2020-04-21 09:28:45 +0200 | [diff] [blame] | 103 | __maybe_unused SizeT lzma_len; |
| 104 | __maybe_unused void *src; |
Weijie Gao | aba6e77 | 2020-04-21 09:28:45 +0200 | [diff] [blame] | 105 | ulong dataptr; |
Stefan Roese | 2fc91ed | 2020-04-21 09:28:43 +0200 | [diff] [blame] | 106 | int ret; |
| 107 | |
Weijie Gao | fdc03bf | 2022-05-20 11:23:58 +0800 | [diff] [blame] | 108 | /* |
| 109 | * If the payload is compressed, the decompressed data should be |
| 110 | * directly write to its load address. |
| 111 | */ |
Roger Quadros | 06377c5 | 2022-09-29 13:11:28 +0300 | [diff] [blame] | 112 | if (spl_image_get_comp(hdr) != IH_COMP_NONE) |
Weijie Gao | fdc03bf | 2022-05-20 11:23:58 +0800 | [diff] [blame] | 113 | spl_image->flags |= SPL_COPY_PAYLOAD_ONLY; |
| 114 | |
Roger Quadros | 06377c5 | 2022-09-29 13:11:28 +0300 | [diff] [blame] | 115 | ret = spl_parse_image_header(spl_image, bootdev, hdr); |
Stefan Roese | 2fc91ed | 2020-04-21 09:28:43 +0200 | [diff] [blame] | 116 | if (ret) |
| 117 | return ret; |
| 118 | |
| 119 | /* Read image */ |
Roger Quadros | 06377c5 | 2022-09-29 13:11:28 +0300 | [diff] [blame] | 120 | switch (spl_image_get_comp(hdr)) { |
Weijie Gao | aba6e77 | 2020-04-21 09:28:45 +0200 | [diff] [blame] | 121 | case IH_COMP_NONE: |
Roger Quadros | 06377c5 | 2022-09-29 13:11:28 +0300 | [diff] [blame] | 122 | dataptr = offset; |
Weijie Gao | fdc03bf | 2022-05-20 11:23:58 +0800 | [diff] [blame] | 123 | |
| 124 | /* |
| 125 | * Image header will be skipped only if SPL_COPY_PAYLOAD_ONLY |
| 126 | * is set |
| 127 | */ |
| 128 | if (spl_image->flags & SPL_COPY_PAYLOAD_ONLY) |
Dai Okamura | fda2253 | 2022-12-09 20:40:21 +0900 | [diff] [blame] | 129 | dataptr += sizeof(*hdr); |
Weijie Gao | fdc03bf | 2022-05-20 11:23:58 +0800 | [diff] [blame] | 130 | |
Weijie Gao | aba6e77 | 2020-04-21 09:28:45 +0200 | [diff] [blame] | 131 | load->read(load, dataptr, spl_image->size, |
| 132 | (void *)(unsigned long)spl_image->load_addr); |
| 133 | break; |
| 134 | |
| 135 | case IH_COMP_LZMA: |
| 136 | lzma_len = LZMA_LEN; |
| 137 | |
Weijie Gao | fdc03bf | 2022-05-20 11:23:58 +0800 | [diff] [blame] | 138 | /* dataptr points to compressed payload */ |
Dai Okamura | fda2253 | 2022-12-09 20:40:21 +0900 | [diff] [blame] | 139 | dataptr = offset + sizeof(*hdr); |
Weijie Gao | fdc03bf | 2022-05-20 11:23:58 +0800 | [diff] [blame] | 140 | |
Weijie Gao | aba6e77 | 2020-04-21 09:28:45 +0200 | [diff] [blame] | 141 | debug("LZMA: Decompressing %08lx to %08lx\n", |
| 142 | dataptr, spl_image->load_addr); |
| 143 | src = malloc(spl_image->size); |
| 144 | if (!src) { |
| 145 | printf("Unable to allocate %d bytes for LZMA\n", |
| 146 | spl_image->size); |
| 147 | return -ENOMEM; |
| 148 | } |
| 149 | |
| 150 | load->read(load, dataptr, spl_image->size, src); |
| 151 | ret = lzmaBuffToBuffDecompress((void *)spl_image->load_addr, |
| 152 | &lzma_len, src, spl_image->size); |
| 153 | if (ret) { |
| 154 | printf("LZMA decompression error: %d\n", ret); |
| 155 | return ret; |
| 156 | } |
| 157 | |
| 158 | spl_image->size = lzma_len; |
| 159 | break; |
| 160 | |
| 161 | default: |
| 162 | debug("Compression method %s is not supported\n", |
Roger Quadros | 06377c5 | 2022-09-29 13:11:28 +0300 | [diff] [blame] | 163 | genimg_get_comp_short_name(image_get_comp(hdr))); |
Weijie Gao | aba6e77 | 2020-04-21 09:28:45 +0200 | [diff] [blame] | 164 | return -EINVAL; |
| 165 | } |
Stefan Roese | 2fc91ed | 2020-04-21 09:28:43 +0200 | [diff] [blame] | 166 | |
| 167 | return 0; |
| 168 | } |