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