Simon Glass | 10b84fe | 2015-08-30 16:55:26 -0600 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2015 Google, Inc |
| 3 | * Written by Simon Glass <sjg@chromium.org> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | * |
| 7 | * See README.rockchip for details of the rkspi format |
| 8 | */ |
| 9 | |
| 10 | #include "imagetool.h" |
| 11 | #include <image.h> |
| 12 | #include <rc4.h> |
| 13 | #include "mkimage.h" |
| 14 | #include "rkcommon.h" |
| 15 | |
| 16 | enum { |
Simon Glass | 10b84fe | 2015-08-30 16:55:26 -0600 | [diff] [blame] | 17 | RKSPI_SECT_LEN = RK_BLK_SIZE * 4, |
| 18 | }; |
| 19 | |
Simon Glass | 10b84fe | 2015-08-30 16:55:26 -0600 | [diff] [blame] | 20 | static void rkspi_set_header(void *buf, struct stat *sbuf, int ifd, |
| 21 | struct image_tool_params *params) |
| 22 | { |
| 23 | int sector; |
| 24 | unsigned int size; |
| 25 | int ret; |
| 26 | |
| 27 | size = params->orig_file_size; |
Jeffy Chen | 7bf274b | 2015-11-27 12:07:17 +0800 | [diff] [blame] | 28 | ret = rkcommon_set_header(buf, size, params); |
Simon Glass | 10b84fe | 2015-08-30 16:55:26 -0600 | [diff] [blame] | 29 | debug("size %x\n", size); |
| 30 | if (ret) { |
| 31 | /* TODO(sjg@chromium.org): This method should return an error */ |
Philipp Tomsich | 366aad4 | 2017-04-17 17:48:01 +0200 | [diff] [blame] | 32 | printf("Warning: SPL image is too large (size %#x) and will " |
| 33 | "not boot\n", size); |
Simon Glass | 10b84fe | 2015-08-30 16:55:26 -0600 | [diff] [blame] | 34 | } |
| 35 | |
Simon Glass | 10b84fe | 2015-08-30 16:55:26 -0600 | [diff] [blame] | 36 | /* |
| 37 | * Spread the image out so we only use the first 2KB of each 4KB |
| 38 | * region. This is a feature of the SPI format required by the Rockchip |
| 39 | * boot ROM. Its rationale is unknown. |
| 40 | */ |
| 41 | for (sector = size / RKSPI_SECT_LEN - 1; sector >= 0; sector--) { |
Simon Glass | 25525eb | 2015-12-29 05:22:44 -0700 | [diff] [blame] | 42 | debug("sector %u\n", sector); |
Simon Glass | 10b84fe | 2015-08-30 16:55:26 -0600 | [diff] [blame] | 43 | memmove(buf + sector * RKSPI_SECT_LEN * 2, |
| 44 | buf + sector * RKSPI_SECT_LEN, |
| 45 | RKSPI_SECT_LEN); |
| 46 | memset(buf + sector * RKSPI_SECT_LEN * 2 + RKSPI_SECT_LEN, |
| 47 | '\0', RKSPI_SECT_LEN); |
| 48 | } |
| 49 | } |
| 50 | |
Simon Glass | 10b84fe | 2015-08-30 16:55:26 -0600 | [diff] [blame] | 51 | static int rkspi_check_image_type(uint8_t type) |
| 52 | { |
| 53 | if (type == IH_TYPE_RKSPI) |
| 54 | return EXIT_SUCCESS; |
| 55 | else |
| 56 | return EXIT_FAILURE; |
| 57 | } |
| 58 | |
Philipp Tomsich | 366aad4 | 2017-04-17 17:48:01 +0200 | [diff] [blame] | 59 | /* |
| 60 | * The SPI payload needs to be padded out to make space for odd half-sector |
| 61 | * layout used in flash (i.e. only the first 2K of each 4K sector is used). |
| 62 | */ |
Simon Glass | 10b84fe | 2015-08-30 16:55:26 -0600 | [diff] [blame] | 63 | static int rkspi_vrec_header(struct image_tool_params *params, |
| 64 | struct image_type_params *tparams) |
| 65 | { |
Philipp Tomsich | ad972ac | 2017-05-30 23:32:09 +0200 | [diff] [blame] | 66 | int padding = rkcommon_vrec_header(params, tparams, RK_INIT_SIZE_ALIGN); |
Philipp Tomsich | 366aad4 | 2017-04-17 17:48:01 +0200 | [diff] [blame] | 67 | /* |
| 68 | * The file size has not been adjusted at this point (our caller will |
| 69 | * eventually add the header/padding to the file_size), so we need to |
| 70 | * add up the header_size, file_size and padding ourselves. |
| 71 | */ |
| 72 | int padded_size = tparams->header_size + params->file_size + padding; |
Simon Glass | 10b84fe | 2015-08-30 16:55:26 -0600 | [diff] [blame] | 73 | |
Philipp Tomsich | 366aad4 | 2017-04-17 17:48:01 +0200 | [diff] [blame] | 74 | /* |
| 75 | * We need to store the original file-size (i.e. before padding), as |
| 76 | * imagetool does not set this during its adjustment of file_size. |
| 77 | */ |
| 78 | params->orig_file_size = padded_size; |
Philipp Tomsich | 111bcc4 | 2017-03-15 12:08:43 +0100 | [diff] [blame] | 79 | |
Philipp Tomsich | 366aad4 | 2017-04-17 17:48:01 +0200 | [diff] [blame] | 80 | /* |
| 81 | * Converting to the SPI format (i.e. splitting each 4K page into two |
| 82 | * 2K subpages and then padding these 2K pages up to take a complete |
| 83 | * 4K sector again) will will double the image size. |
| 84 | * |
| 85 | * Thus we return the padded_size as an additional padding requirement |
| 86 | * (be sure to add this to the padding returned from the common code). |
| 87 | */ |
| 88 | return padded_size + padding; |
Simon Glass | 10b84fe | 2015-08-30 16:55:26 -0600 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /* |
| 92 | * rk_spi parameters |
| 93 | */ |
| 94 | U_BOOT_IMAGE_TYPE( |
| 95 | rkspi, |
| 96 | "Rockchip SPI Boot Image support", |
Philipp Tomsich | 111bcc4 | 2017-03-15 12:08:43 +0100 | [diff] [blame] | 97 | 0, |
| 98 | NULL, |
Jeffy Chen | 7bf274b | 2015-11-27 12:07:17 +0800 | [diff] [blame] | 99 | rkcommon_check_params, |
Philipp Tomsich | 2fb371f | 2017-05-30 23:32:08 +0200 | [diff] [blame] | 100 | rkcommon_verify_header, |
| 101 | rkcommon_print_header, |
Simon Glass | 10b84fe | 2015-08-30 16:55:26 -0600 | [diff] [blame] | 102 | rkspi_set_header, |
Philipp Tomsich | 2fb371f | 2017-05-30 23:32:08 +0200 | [diff] [blame] | 103 | NULL, |
Simon Glass | 10b84fe | 2015-08-30 16:55:26 -0600 | [diff] [blame] | 104 | rkspi_check_image_type, |
| 105 | NULL, |
| 106 | rkspi_vrec_header |
| 107 | ); |