Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
John Rigby | 3decb14 | 2011-07-21 09:10:30 -0400 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2010 |
| 4 | * Linaro LTD, www.linaro.org |
| 5 | * Author: John Rigby <john.rigby@linaro.org> |
| 6 | * Based on TI's signGP.c |
| 7 | * |
| 8 | * (C) Copyright 2009 |
| 9 | * Stefano Babic, DENX Software Engineering, sbabic@denx.de. |
| 10 | * |
| 11 | * (C) Copyright 2008 |
| 12 | * Marvell Semiconductor <www.marvell.com> |
| 13 | * Written-by: Prafulla Wadaskar <prafulla@marvell.com> |
John Rigby | 3decb14 | 2011-07-21 09:10:30 -0400 | [diff] [blame] | 14 | */ |
| 15 | |
Guilherme Maciel Ferreira | f86ed6a | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 16 | #include "imagetool.h" |
Karicheri, Muralidharan | bf411ea | 2014-04-04 13:16:48 -0400 | [diff] [blame] | 17 | #include <compiler.h> |
John Rigby | 3decb14 | 2011-07-21 09:10:30 -0400 | [diff] [blame] | 18 | #include <image.h> |
Karicheri, Muralidharan | bf411ea | 2014-04-04 13:16:48 -0400 | [diff] [blame] | 19 | #include "gpheader.h" |
John Rigby | 3decb14 | 2011-07-21 09:10:30 -0400 | [diff] [blame] | 20 | #include "omapimage.h" |
| 21 | |
Philipp Tomsich | c8e1ca3 | 2017-12-04 17:04:02 +0100 | [diff] [blame] | 22 | #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) |
| 23 | |
John Rigby | 3decb14 | 2011-07-21 09:10:30 -0400 | [diff] [blame] | 24 | /* Header size is CH header rounded up to 512 bytes plus GP header */ |
| 25 | #define OMAP_CH_HDR_SIZE 512 |
Karicheri, Muralidharan | bf411ea | 2014-04-04 13:16:48 -0400 | [diff] [blame] | 26 | #define OMAP_FILE_HDR_SIZE (OMAP_CH_HDR_SIZE + GPIMAGE_HDR_SIZE) |
John Rigby | 3decb14 | 2011-07-21 09:10:30 -0400 | [diff] [blame] | 27 | |
Tom Rini | 79b9ebb | 2012-10-16 13:06:06 +0000 | [diff] [blame] | 28 | static int do_swap32 = 0; |
| 29 | |
John Rigby | 3decb14 | 2011-07-21 09:10:30 -0400 | [diff] [blame] | 30 | static uint8_t omapimage_header[OMAP_FILE_HDR_SIZE]; |
| 31 | |
| 32 | static int omapimage_check_image_types(uint8_t type) |
| 33 | { |
| 34 | if (type == IH_TYPE_OMAPIMAGE) |
| 35 | return EXIT_SUCCESS; |
Karicheri, Muralidharan | bf411ea | 2014-04-04 13:16:48 -0400 | [diff] [blame] | 36 | return EXIT_FAILURE; |
John Rigby | 3decb14 | 2011-07-21 09:10:30 -0400 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | static int omapimage_verify_header(unsigned char *ptr, int image_size, |
Guilherme Maciel Ferreira | f86ed6a | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 40 | struct image_tool_params *params) |
John Rigby | 3decb14 | 2011-07-21 09:10:30 -0400 | [diff] [blame] | 41 | { |
| 42 | struct ch_toc *toc = (struct ch_toc *)ptr; |
| 43 | struct gp_header *gph = (struct gp_header *)(ptr+OMAP_CH_HDR_SIZE); |
Karicheri, Muralidharan | bf411ea | 2014-04-04 13:16:48 -0400 | [diff] [blame] | 44 | uint32_t offset, size; |
John Rigby | 3decb14 | 2011-07-21 09:10:30 -0400 | [diff] [blame] | 45 | |
| 46 | while (toc->section_offset != 0xffffffff |
| 47 | && toc->section_size != 0xffffffff) { |
Tom Rini | 79b9ebb | 2012-10-16 13:06:06 +0000 | [diff] [blame] | 48 | if (do_swap32) { |
Karicheri, Muralidharan | bf411ea | 2014-04-04 13:16:48 -0400 | [diff] [blame] | 49 | offset = cpu_to_be32(toc->section_offset); |
| 50 | size = cpu_to_be32(toc->section_size); |
Tom Rini | 79b9ebb | 2012-10-16 13:06:06 +0000 | [diff] [blame] | 51 | } else { |
| 52 | offset = toc->section_offset; |
| 53 | size = toc->section_size; |
| 54 | } |
John Rigby | 3decb14 | 2011-07-21 09:10:30 -0400 | [diff] [blame] | 55 | if (!offset || !size) |
| 56 | return -1; |
| 57 | if (offset >= OMAP_CH_HDR_SIZE || |
| 58 | offset+size >= OMAP_CH_HDR_SIZE) |
| 59 | return -1; |
| 60 | toc++; |
| 61 | } |
Tom Rini | 79b9ebb | 2012-10-16 13:06:06 +0000 | [diff] [blame] | 62 | |
Karicheri, Muralidharan | bf411ea | 2014-04-04 13:16:48 -0400 | [diff] [blame] | 63 | return gph_verify_header(gph, do_swap32); |
John Rigby | 3decb14 | 2011-07-21 09:10:30 -0400 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static void omapimage_print_section(struct ch_settings *chs) |
| 67 | { |
| 68 | const char *section_name; |
| 69 | |
| 70 | if (chs->section_key) |
| 71 | section_name = "CHSETTINGS"; |
| 72 | else |
| 73 | section_name = "UNKNOWNKEY"; |
| 74 | |
| 75 | printf("%s (%x) " |
| 76 | "valid:%x " |
| 77 | "version:%x " |
| 78 | "reserved:%x " |
| 79 | "flags:%x\n", |
| 80 | section_name, |
| 81 | chs->section_key, |
| 82 | chs->valid, |
| 83 | chs->version, |
| 84 | chs->reserved, |
| 85 | chs->flags); |
| 86 | } |
| 87 | |
| 88 | static void omapimage_print_header(const void *ptr) |
| 89 | { |
| 90 | const struct ch_toc *toc = (struct ch_toc *)ptr; |
| 91 | const struct gp_header *gph = |
| 92 | (struct gp_header *)(ptr+OMAP_CH_HDR_SIZE); |
Karicheri, Muralidharan | bf411ea | 2014-04-04 13:16:48 -0400 | [diff] [blame] | 93 | uint32_t offset, size; |
John Rigby | 3decb14 | 2011-07-21 09:10:30 -0400 | [diff] [blame] | 94 | |
| 95 | while (toc->section_offset != 0xffffffff |
| 96 | && toc->section_size != 0xffffffff) { |
Tom Rini | 79b9ebb | 2012-10-16 13:06:06 +0000 | [diff] [blame] | 97 | if (do_swap32) { |
Karicheri, Muralidharan | bf411ea | 2014-04-04 13:16:48 -0400 | [diff] [blame] | 98 | offset = cpu_to_be32(toc->section_offset); |
| 99 | size = cpu_to_be32(toc->section_size); |
Tom Rini | 79b9ebb | 2012-10-16 13:06:06 +0000 | [diff] [blame] | 100 | } else { |
| 101 | offset = toc->section_offset; |
| 102 | size = toc->section_size; |
| 103 | } |
John Rigby | 3decb14 | 2011-07-21 09:10:30 -0400 | [diff] [blame] | 104 | |
| 105 | if (offset >= OMAP_CH_HDR_SIZE || |
| 106 | offset+size >= OMAP_CH_HDR_SIZE) |
| 107 | exit(EXIT_FAILURE); |
| 108 | |
| 109 | printf("Section %s offset %x length %x\n", |
| 110 | toc->section_name, |
| 111 | toc->section_offset, |
| 112 | toc->section_size); |
| 113 | |
| 114 | omapimage_print_section((struct ch_settings *)(ptr+offset)); |
| 115 | toc++; |
| 116 | } |
| 117 | |
Karicheri, Muralidharan | bf411ea | 2014-04-04 13:16:48 -0400 | [diff] [blame] | 118 | gph_print_header(gph, do_swap32); |
John Rigby | 3decb14 | 2011-07-21 09:10:30 -0400 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static int toc_offset(void *hdr, void *member) |
| 122 | { |
| 123 | return member - hdr; |
| 124 | } |
| 125 | |
| 126 | static void omapimage_set_header(void *ptr, struct stat *sbuf, int ifd, |
Guilherme Maciel Ferreira | f86ed6a | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 127 | struct image_tool_params *params) |
John Rigby | 3decb14 | 2011-07-21 09:10:30 -0400 | [diff] [blame] | 128 | { |
| 129 | struct ch_toc *toc = (struct ch_toc *)ptr; |
| 130 | struct ch_settings *chs = (struct ch_settings *) |
| 131 | (ptr + 2 * sizeof(*toc)); |
| 132 | struct gp_header *gph = (struct gp_header *)(ptr + OMAP_CH_HDR_SIZE); |
| 133 | |
| 134 | toc->section_offset = toc_offset(ptr, chs); |
| 135 | toc->section_size = sizeof(struct ch_settings); |
| 136 | strcpy((char *)toc->section_name, "CHSETTINGS"); |
| 137 | |
| 138 | chs->section_key = KEY_CHSETTINGS; |
| 139 | chs->valid = 0; |
| 140 | chs->version = 1; |
| 141 | chs->reserved = 0; |
| 142 | chs->flags = 0; |
| 143 | |
| 144 | toc++; |
| 145 | memset(toc, 0xff, sizeof(*toc)); |
| 146 | |
Lokesh Vutla | 3a0e70f | 2017-12-28 20:40:00 +0530 | [diff] [blame] | 147 | gph_set_header(gph, sbuf->st_size - OMAP_CH_HDR_SIZE, |
Karicheri, Muralidharan | bf411ea | 2014-04-04 13:16:48 -0400 | [diff] [blame] | 148 | params->addr, 0); |
Tom Rini | 79b9ebb | 2012-10-16 13:06:06 +0000 | [diff] [blame] | 149 | |
| 150 | if (strncmp(params->imagename, "byteswap", 8) == 0) { |
| 151 | do_swap32 = 1; |
| 152 | int swapped = 0; |
| 153 | uint32_t *data = (uint32_t *)ptr; |
Philipp Tomsich | c8e1ca3 | 2017-12-04 17:04:02 +0100 | [diff] [blame] | 154 | const off_t size_in_words = |
| 155 | DIV_ROUND_UP(sbuf->st_size, sizeof(uint32_t)); |
Tom Rini | 79b9ebb | 2012-10-16 13:06:06 +0000 | [diff] [blame] | 156 | |
Philipp Tomsich | c8e1ca3 | 2017-12-04 17:04:02 +0100 | [diff] [blame] | 157 | while (swapped < size_in_words) { |
Karicheri, Muralidharan | bf411ea | 2014-04-04 13:16:48 -0400 | [diff] [blame] | 158 | *data = cpu_to_be32(*data); |
Tom Rini | 79b9ebb | 2012-10-16 13:06:06 +0000 | [diff] [blame] | 159 | swapped++; |
| 160 | data++; |
| 161 | } |
| 162 | } |
John Rigby | 3decb14 | 2011-07-21 09:10:30 -0400 | [diff] [blame] | 163 | } |
| 164 | |
John Rigby | 3decb14 | 2011-07-21 09:10:30 -0400 | [diff] [blame] | 165 | /* |
| 166 | * omapimage parameters |
| 167 | */ |
Guilherme Maciel Ferreira | a93648d | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 168 | U_BOOT_IMAGE_TYPE( |
| 169 | omapimage, |
| 170 | "TI OMAP CH/GP Boot Image support", |
| 171 | OMAP_FILE_HDR_SIZE, |
| 172 | (void *)&omapimage_header, |
| 173 | gpimage_check_params, |
| 174 | omapimage_verify_header, |
| 175 | omapimage_print_header, |
| 176 | omapimage_set_header, |
| 177 | NULL, |
| 178 | omapimage_check_image_types, |
| 179 | NULL, |
| 180 | NULL |
| 181 | ); |