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> |
Stefan Roese | 21a1df1 | 2020-04-21 09:28:41 +0200 | [diff] [blame] | 10 | #include <spl.h> |
| 11 | |
developer | aa1c1d3 | 2020-04-21 09:28:45 +0200 | [diff] [blame] | 12 | #include <lzma/LzmaTypes.h> |
| 13 | #include <lzma/LzmaDec.h> |
| 14 | #include <lzma/LzmaTools.h> |
| 15 | |
| 16 | #define LZMA_LEN (1 << 20) |
| 17 | |
Stefan Roese | 21a1df1 | 2020-04-21 09:28:41 +0200 | [diff] [blame] | 18 | int spl_parse_legacy_header(struct spl_image_info *spl_image, |
Simon Glass | bb7d3bb | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 19 | const struct legacy_img_hdr *header) |
Stefan Roese | 21a1df1 | 2020-04-21 09:28:41 +0200 | [diff] [blame] | 20 | { |
Simon Glass | bb7d3bb | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 21 | u32 header_size = sizeof(struct legacy_img_hdr); |
Stefan Roese | 21a1df1 | 2020-04-21 09:28:41 +0200 | [diff] [blame] | 22 | |
Stefan Roese | 21a1df1 | 2020-04-21 09:28:41 +0200 | [diff] [blame] | 23 | /* check uImage header CRC */ |
Stefan Roese | fc962c5 | 2020-04-21 09:28:42 +0200 | [diff] [blame] | 24 | if (IS_ENABLED(CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK) && |
| 25 | !image_check_hcrc(header)) { |
Stefan Roese | 21a1df1 | 2020-04-21 09:28:41 +0200 | [diff] [blame] | 26 | puts("SPL: Image header CRC check failed!\n"); |
| 27 | return -EINVAL; |
| 28 | } |
Stefan Roese | 21a1df1 | 2020-04-21 09:28:41 +0200 | [diff] [blame] | 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 | } |
Stefan Roese | 84ae9d8 | 2020-04-21 09:28:43 +0200 | [diff] [blame] | 63 | |
developer | aa1c1d3 | 2020-04-21 09:28:45 +0200 | [diff] [blame] | 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 | */ |
Simon Glass | bb7d3bb | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 70 | static inline int spl_image_get_comp(const struct legacy_img_hdr *hdr) |
developer | aa1c1d3 | 2020-04-21 09:28:45 +0200 | [diff] [blame] | 71 | { |
| 72 | if (IS_ENABLED(CONFIG_SPL_LZMA)) |
| 73 | return image_get_comp(hdr); |
| 74 | |
| 75 | return IH_COMP_NONE; |
| 76 | } |
| 77 | |
Stefan Roese | 84ae9d8 | 2020-04-21 09:28:43 +0200 | [diff] [blame] | 78 | int spl_load_legacy_img(struct spl_image_info *spl_image, |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 79 | struct spl_boot_device *bootdev, |
Roger Quadros | 237f018 | 2022-09-29 13:11:28 +0300 | [diff] [blame] | 80 | struct spl_load_info *load, ulong offset, |
| 81 | struct legacy_img_hdr *hdr) |
Stefan Roese | 84ae9d8 | 2020-04-21 09:28:43 +0200 | [diff] [blame] | 82 | { |
developer | aa1c1d3 | 2020-04-21 09:28:45 +0200 | [diff] [blame] | 83 | __maybe_unused SizeT lzma_len; |
| 84 | __maybe_unused void *src; |
developer | aa1c1d3 | 2020-04-21 09:28:45 +0200 | [diff] [blame] | 85 | ulong dataptr; |
Stefan Roese | 84ae9d8 | 2020-04-21 09:28:43 +0200 | [diff] [blame] | 86 | int ret; |
| 87 | |
developer | 16e6a2e | 2022-05-20 11:23:58 +0800 | [diff] [blame] | 88 | /* |
| 89 | * If the payload is compressed, the decompressed data should be |
| 90 | * directly write to its load address. |
| 91 | */ |
Roger Quadros | 237f018 | 2022-09-29 13:11:28 +0300 | [diff] [blame] | 92 | if (spl_image_get_comp(hdr) != IH_COMP_NONE) |
developer | 16e6a2e | 2022-05-20 11:23:58 +0800 | [diff] [blame] | 93 | spl_image->flags |= SPL_COPY_PAYLOAD_ONLY; |
| 94 | |
Roger Quadros | 237f018 | 2022-09-29 13:11:28 +0300 | [diff] [blame] | 95 | ret = spl_parse_image_header(spl_image, bootdev, hdr); |
Stefan Roese | 84ae9d8 | 2020-04-21 09:28:43 +0200 | [diff] [blame] | 96 | if (ret) |
| 97 | return ret; |
| 98 | |
| 99 | /* Read image */ |
Roger Quadros | 237f018 | 2022-09-29 13:11:28 +0300 | [diff] [blame] | 100 | switch (spl_image_get_comp(hdr)) { |
developer | aa1c1d3 | 2020-04-21 09:28:45 +0200 | [diff] [blame] | 101 | case IH_COMP_NONE: |
Roger Quadros | 237f018 | 2022-09-29 13:11:28 +0300 | [diff] [blame] | 102 | dataptr = offset; |
developer | 16e6a2e | 2022-05-20 11:23:58 +0800 | [diff] [blame] | 103 | |
| 104 | /* |
| 105 | * Image header will be skipped only if SPL_COPY_PAYLOAD_ONLY |
| 106 | * is set |
| 107 | */ |
| 108 | if (spl_image->flags & SPL_COPY_PAYLOAD_ONLY) |
Dai Okamura | 08b72b5 | 2022-12-09 20:40:21 +0900 | [diff] [blame^] | 109 | dataptr += sizeof(*hdr); |
developer | 16e6a2e | 2022-05-20 11:23:58 +0800 | [diff] [blame] | 110 | |
developer | aa1c1d3 | 2020-04-21 09:28:45 +0200 | [diff] [blame] | 111 | load->read(load, dataptr, spl_image->size, |
| 112 | (void *)(unsigned long)spl_image->load_addr); |
| 113 | break; |
| 114 | |
| 115 | case IH_COMP_LZMA: |
| 116 | lzma_len = LZMA_LEN; |
| 117 | |
developer | 16e6a2e | 2022-05-20 11:23:58 +0800 | [diff] [blame] | 118 | /* dataptr points to compressed payload */ |
Dai Okamura | 08b72b5 | 2022-12-09 20:40:21 +0900 | [diff] [blame^] | 119 | dataptr = offset + sizeof(*hdr); |
developer | 16e6a2e | 2022-05-20 11:23:58 +0800 | [diff] [blame] | 120 | |
developer | aa1c1d3 | 2020-04-21 09:28:45 +0200 | [diff] [blame] | 121 | debug("LZMA: Decompressing %08lx to %08lx\n", |
| 122 | dataptr, spl_image->load_addr); |
| 123 | src = malloc(spl_image->size); |
| 124 | if (!src) { |
| 125 | printf("Unable to allocate %d bytes for LZMA\n", |
| 126 | spl_image->size); |
| 127 | return -ENOMEM; |
| 128 | } |
| 129 | |
| 130 | load->read(load, dataptr, spl_image->size, src); |
| 131 | ret = lzmaBuffToBuffDecompress((void *)spl_image->load_addr, |
| 132 | &lzma_len, src, spl_image->size); |
| 133 | if (ret) { |
| 134 | printf("LZMA decompression error: %d\n", ret); |
| 135 | return ret; |
| 136 | } |
| 137 | |
| 138 | spl_image->size = lzma_len; |
| 139 | break; |
| 140 | |
| 141 | default: |
| 142 | debug("Compression method %s is not supported\n", |
Roger Quadros | 237f018 | 2022-09-29 13:11:28 +0300 | [diff] [blame] | 143 | genimg_get_comp_short_name(image_get_comp(hdr))); |
developer | aa1c1d3 | 2020-04-21 09:28:45 +0200 | [diff] [blame] | 144 | return -EINVAL; |
| 145 | } |
Stefan Roese | 84ae9d8 | 2020-04-21 09:28:43 +0200 | [diff] [blame] | 146 | |
| 147 | return 0; |
| 148 | } |