Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 10b84fe | 2015-08-30 16:55:26 -0600 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2015 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
| 5 | * |
Simon Glass | 10b84fe | 2015-08-30 16:55:26 -0600 | [diff] [blame] | 6 | * See README.rockchip for details of the rkspi format |
| 7 | */ |
| 8 | |
| 9 | #include "imagetool.h" |
| 10 | #include <image.h> |
| 11 | #include <rc4.h> |
| 12 | #include "mkimage.h" |
| 13 | #include "rkcommon.h" |
| 14 | |
| 15 | enum { |
Simon Glass | 10b84fe | 2015-08-30 16:55:26 -0600 | [diff] [blame] | 16 | RKSPI_SECT_LEN = RK_BLK_SIZE * 4, |
| 17 | }; |
| 18 | |
Simon Glass | 10b84fe | 2015-08-30 16:55:26 -0600 | [diff] [blame] | 19 | static void rkspi_set_header(void *buf, struct stat *sbuf, int ifd, |
| 20 | struct image_tool_params *params) |
| 21 | { |
| 22 | int sector; |
| 23 | unsigned int size; |
Simon Glass | 10b84fe | 2015-08-30 16:55:26 -0600 | [diff] [blame] | 24 | |
| 25 | size = params->orig_file_size; |
Jeffy Chen | eea6cd8 | 2019-12-27 11:24:41 +0800 | [diff] [blame] | 26 | |
| 27 | rkcommon_set_header(buf, sbuf, ifd, params); |
Simon Glass | 10b84fe | 2015-08-30 16:55:26 -0600 | [diff] [blame] | 28 | |
Simon Glass | 10b84fe | 2015-08-30 16:55:26 -0600 | [diff] [blame] | 29 | /* |
| 30 | * Spread the image out so we only use the first 2KB of each 4KB |
| 31 | * region. This is a feature of the SPI format required by the Rockchip |
| 32 | * boot ROM. Its rationale is unknown. |
| 33 | */ |
Jeffy Chen | eea6cd8 | 2019-12-27 11:24:41 +0800 | [diff] [blame] | 34 | if (params->vflag) |
| 35 | fprintf(stderr, "Spreading spi image from %u to %u\n", |
| 36 | size, params->file_size); |
| 37 | |
Simon Glass | 10b84fe | 2015-08-30 16:55:26 -0600 | [diff] [blame] | 38 | for (sector = size / RKSPI_SECT_LEN - 1; sector >= 0; sector--) { |
Simon Glass | 25525eb | 2015-12-29 05:22:44 -0700 | [diff] [blame] | 39 | debug("sector %u\n", sector); |
Simon Glass | 10b84fe | 2015-08-30 16:55:26 -0600 | [diff] [blame] | 40 | memmove(buf + sector * RKSPI_SECT_LEN * 2, |
| 41 | buf + sector * RKSPI_SECT_LEN, |
| 42 | RKSPI_SECT_LEN); |
| 43 | memset(buf + sector * RKSPI_SECT_LEN * 2 + RKSPI_SECT_LEN, |
| 44 | '\0', RKSPI_SECT_LEN); |
| 45 | } |
| 46 | } |
| 47 | |
Simon Glass | 10b84fe | 2015-08-30 16:55:26 -0600 | [diff] [blame] | 48 | static int rkspi_check_image_type(uint8_t type) |
| 49 | { |
| 50 | if (type == IH_TYPE_RKSPI) |
| 51 | return EXIT_SUCCESS; |
| 52 | else |
| 53 | return EXIT_FAILURE; |
| 54 | } |
| 55 | |
Philipp Tomsich | 366aad4 | 2017-04-17 17:48:01 +0200 | [diff] [blame] | 56 | /* |
Jeffy Chen | eea6cd8 | 2019-12-27 11:24:41 +0800 | [diff] [blame] | 57 | * The SPI payload needs to make space for odd half-sector layout used in flash |
| 58 | * (i.e. only the first 2K of each 4K sector is used). |
Philipp Tomsich | 366aad4 | 2017-04-17 17:48:01 +0200 | [diff] [blame] | 59 | */ |
Simon Glass | 10b84fe | 2015-08-30 16:55:26 -0600 | [diff] [blame] | 60 | static int rkspi_vrec_header(struct image_tool_params *params, |
| 61 | struct image_type_params *tparams) |
| 62 | { |
Jeffy Chen | eea6cd8 | 2019-12-27 11:24:41 +0800 | [diff] [blame] | 63 | rkcommon_vrec_header(params, tparams); |
Philipp Tomsich | 111bcc4 | 2017-03-15 12:08:43 +0100 | [diff] [blame] | 64 | |
Philipp Tomsich | 366aad4 | 2017-04-17 17:48:01 +0200 | [diff] [blame] | 65 | /* |
| 66 | * Converting to the SPI format (i.e. splitting each 4K page into two |
| 67 | * 2K subpages and then padding these 2K pages up to take a complete |
Jeffy Chen | eea6cd8 | 2019-12-27 11:24:41 +0800 | [diff] [blame] | 68 | * 4K sector again) which will double the image size. |
Philipp Tomsich | 366aad4 | 2017-04-17 17:48:01 +0200 | [diff] [blame] | 69 | */ |
Jeffy Chen | eea6cd8 | 2019-12-27 11:24:41 +0800 | [diff] [blame] | 70 | params->file_size = ROUND(params->file_size, RKSPI_SECT_LEN) << 1; |
| 71 | |
| 72 | /* Ignoring pad len, since we are using our own copy_image() */ |
| 73 | return 0; |
Simon Glass | 10b84fe | 2015-08-30 16:55:26 -0600 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /* |
| 77 | * rk_spi parameters |
| 78 | */ |
| 79 | U_BOOT_IMAGE_TYPE( |
| 80 | rkspi, |
| 81 | "Rockchip SPI Boot Image support", |
Philipp Tomsich | 111bcc4 | 2017-03-15 12:08:43 +0100 | [diff] [blame] | 82 | 0, |
| 83 | NULL, |
Jeffy Chen | 7bf274b | 2015-11-27 12:07:17 +0800 | [diff] [blame] | 84 | rkcommon_check_params, |
Philipp Tomsich | 2fb371f | 2017-05-30 23:32:08 +0200 | [diff] [blame] | 85 | rkcommon_verify_header, |
| 86 | rkcommon_print_header, |
Simon Glass | 10b84fe | 2015-08-30 16:55:26 -0600 | [diff] [blame] | 87 | rkspi_set_header, |
Philipp Tomsich | 2fb371f | 2017-05-30 23:32:08 +0200 | [diff] [blame] | 88 | NULL, |
Simon Glass | 10b84fe | 2015-08-30 16:55:26 -0600 | [diff] [blame] | 89 | rkspi_check_image_type, |
| 90 | NULL, |
| 91 | rkspi_vrec_header |
| 92 | ); |