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