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> |
Weijie Gao | aba6e77 | 2020-04-21 09:28:45 +0200 | [diff] [blame] | 8 | #include <malloc.h> |
Stefan Roese | c110817 | 2020-04-21 09:28:41 +0200 | [diff] [blame] | 9 | #include <spl.h> |
| 10 | |
Weijie Gao | aba6e77 | 2020-04-21 09:28:45 +0200 | [diff] [blame] | 11 | #include <lzma/LzmaTypes.h> |
| 12 | #include <lzma/LzmaDec.h> |
| 13 | #include <lzma/LzmaTools.h> |
| 14 | |
| 15 | #define LZMA_LEN (1 << 20) |
| 16 | |
Stefan Roese | c110817 | 2020-04-21 09:28:41 +0200 | [diff] [blame] | 17 | int spl_parse_legacy_header(struct spl_image_info *spl_image, |
| 18 | const struct image_header *header) |
| 19 | { |
| 20 | u32 header_size = sizeof(struct image_header); |
| 21 | |
Stefan Roese | c110817 | 2020-04-21 09:28:41 +0200 | [diff] [blame] | 22 | /* check uImage header CRC */ |
Stefan Roese | 5ae526b | 2020-04-21 09:28:42 +0200 | [diff] [blame] | 23 | if (IS_ENABLED(CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK) && |
| 24 | !image_check_hcrc(header)) { |
Stefan Roese | c110817 | 2020-04-21 09:28:41 +0200 | [diff] [blame] | 25 | puts("SPL: Image header CRC check failed!\n"); |
| 26 | return -EINVAL; |
| 27 | } |
Stefan Roese | c110817 | 2020-04-21 09:28:41 +0200 | [diff] [blame] | 28 | |
| 29 | if (spl_image->flags & SPL_COPY_PAYLOAD_ONLY) { |
| 30 | /* |
| 31 | * On some system (e.g. powerpc), the load-address and |
| 32 | * entry-point is located at address 0. We can't load |
| 33 | * to 0-0x40. So skip header in this case. |
| 34 | */ |
| 35 | spl_image->load_addr = image_get_load(header); |
| 36 | spl_image->entry_point = image_get_ep(header); |
| 37 | spl_image->size = image_get_data_size(header); |
| 38 | } else { |
| 39 | spl_image->entry_point = image_get_ep(header); |
| 40 | /* Load including the header */ |
| 41 | spl_image->load_addr = image_get_load(header) - |
| 42 | header_size; |
| 43 | spl_image->size = image_get_data_size(header) + |
| 44 | header_size; |
| 45 | } |
| 46 | |
| 47 | #ifdef CONFIG_SPL_LEGACY_IMAGE_CRC_CHECK |
| 48 | /* store uImage data length and CRC to check later */ |
| 49 | spl_image->dcrc_data = image_get_load(header); |
| 50 | spl_image->dcrc_length = image_get_data_size(header); |
| 51 | spl_image->dcrc = image_get_dcrc(header); |
| 52 | #endif |
| 53 | |
| 54 | spl_image->os = image_get_os(header); |
| 55 | spl_image->name = image_get_name(header); |
| 56 | debug(SPL_TPL_PROMPT |
| 57 | "payload image: %32s load addr: 0x%lx size: %d\n", |
| 58 | spl_image->name, spl_image->load_addr, spl_image->size); |
| 59 | |
| 60 | return 0; |
| 61 | } |
Stefan Roese | 2fc91ed | 2020-04-21 09:28:43 +0200 | [diff] [blame] | 62 | |
Weijie Gao | aba6e77 | 2020-04-21 09:28:45 +0200 | [diff] [blame] | 63 | /* |
| 64 | * This function is added explicitly to avoid code size increase, when |
| 65 | * no compression method is enabled. The compiler will optimize the |
| 66 | * following switch/case statement in spl_load_legacy_img() away due to |
| 67 | * Dead Code Elimination. |
| 68 | */ |
| 69 | static inline int spl_image_get_comp(const struct image_header *hdr) |
| 70 | { |
| 71 | if (IS_ENABLED(CONFIG_SPL_LZMA)) |
| 72 | return image_get_comp(hdr); |
| 73 | |
| 74 | return IH_COMP_NONE; |
| 75 | } |
| 76 | |
Stefan Roese | 2fc91ed | 2020-04-21 09:28:43 +0200 | [diff] [blame] | 77 | int spl_load_legacy_img(struct spl_image_info *spl_image, |
| 78 | struct spl_load_info *load, ulong header) |
| 79 | { |
Weijie Gao | aba6e77 | 2020-04-21 09:28:45 +0200 | [diff] [blame] | 80 | __maybe_unused SizeT lzma_len; |
| 81 | __maybe_unused void *src; |
Stefan Roese | 2fc91ed | 2020-04-21 09:28:43 +0200 | [diff] [blame] | 82 | struct image_header hdr; |
Weijie Gao | aba6e77 | 2020-04-21 09:28:45 +0200 | [diff] [blame] | 83 | ulong dataptr; |
Stefan Roese | 2fc91ed | 2020-04-21 09:28:43 +0200 | [diff] [blame] | 84 | int ret; |
| 85 | |
| 86 | /* Read header into local struct */ |
| 87 | load->read(load, header, sizeof(hdr), &hdr); |
| 88 | |
| 89 | ret = spl_parse_image_header(spl_image, &hdr); |
| 90 | if (ret) |
| 91 | return ret; |
| 92 | |
Weijie Gao | aba6e77 | 2020-04-21 09:28:45 +0200 | [diff] [blame] | 93 | dataptr = header + sizeof(hdr); |
| 94 | |
Stefan Roese | 2fc91ed | 2020-04-21 09:28:43 +0200 | [diff] [blame] | 95 | /* Read image */ |
Weijie Gao | aba6e77 | 2020-04-21 09:28:45 +0200 | [diff] [blame] | 96 | switch (spl_image_get_comp(&hdr)) { |
| 97 | case IH_COMP_NONE: |
| 98 | load->read(load, dataptr, spl_image->size, |
| 99 | (void *)(unsigned long)spl_image->load_addr); |
| 100 | break; |
| 101 | |
| 102 | case IH_COMP_LZMA: |
| 103 | lzma_len = LZMA_LEN; |
| 104 | |
| 105 | debug("LZMA: Decompressing %08lx to %08lx\n", |
| 106 | dataptr, spl_image->load_addr); |
| 107 | src = malloc(spl_image->size); |
| 108 | if (!src) { |
| 109 | printf("Unable to allocate %d bytes for LZMA\n", |
| 110 | spl_image->size); |
| 111 | return -ENOMEM; |
| 112 | } |
| 113 | |
| 114 | load->read(load, dataptr, spl_image->size, src); |
| 115 | ret = lzmaBuffToBuffDecompress((void *)spl_image->load_addr, |
| 116 | &lzma_len, src, spl_image->size); |
| 117 | if (ret) { |
| 118 | printf("LZMA decompression error: %d\n", ret); |
| 119 | return ret; |
| 120 | } |
| 121 | |
| 122 | spl_image->size = lzma_len; |
| 123 | break; |
| 124 | |
| 125 | default: |
| 126 | debug("Compression method %s is not supported\n", |
| 127 | genimg_get_comp_short_name(image_get_comp(&hdr))); |
| 128 | return -EINVAL; |
| 129 | } |
Stefan Roese | 2fc91ed | 2020-04-21 09:28:43 +0200 | [diff] [blame] | 130 | |
| 131 | return 0; |
| 132 | } |