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