]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
3decb14a JR |
2 | /* |
3 | * (C) Copyright 2010 | |
4 | * Linaro LTD, www.linaro.org | |
5 | * Author: John Rigby <[email protected]> | |
6 | * Based on TI's signGP.c | |
7 | * | |
8 | * (C) Copyright 2009 | |
9 | * Stefano Babic, DENX Software Engineering, [email protected]. | |
10 | * | |
11 | * (C) Copyright 2008 | |
12 | * Marvell Semiconductor <www.marvell.com> | |
13 | * Written-by: Prafulla Wadaskar <[email protected]> | |
3decb14a JR |
14 | */ |
15 | ||
f86ed6a8 | 16 | #include "imagetool.h" |
bf411ea9 | 17 | #include <compiler.h> |
3decb14a | 18 | #include <image.h> |
bf411ea9 | 19 | #include "gpheader.h" |
3decb14a JR |
20 | #include "omapimage.h" |
21 | ||
c8e1ca3e PT |
22 | #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) |
23 | ||
3decb14a JR |
24 | /* Header size is CH header rounded up to 512 bytes plus GP header */ |
25 | #define OMAP_CH_HDR_SIZE 512 | |
bf411ea9 | 26 | #define OMAP_FILE_HDR_SIZE (OMAP_CH_HDR_SIZE + GPIMAGE_HDR_SIZE) |
3decb14a | 27 | |
79b9ebb7 TR |
28 | static int do_swap32 = 0; |
29 | ||
3decb14a JR |
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; | |
bf411ea9 | 36 | return EXIT_FAILURE; |
3decb14a JR |
37 | } |
38 | ||
39 | static int omapimage_verify_header(unsigned char *ptr, int image_size, | |
f86ed6a8 | 40 | struct image_tool_params *params) |
3decb14a JR |
41 | { |
42 | struct ch_toc *toc = (struct ch_toc *)ptr; | |
43 | struct gp_header *gph = (struct gp_header *)(ptr+OMAP_CH_HDR_SIZE); | |
bf411ea9 | 44 | uint32_t offset, size; |
3decb14a JR |
45 | |
46 | while (toc->section_offset != 0xffffffff | |
47 | && toc->section_size != 0xffffffff) { | |
79b9ebb7 | 48 | if (do_swap32) { |
bf411ea9 KM |
49 | offset = cpu_to_be32(toc->section_offset); |
50 | size = cpu_to_be32(toc->section_size); | |
79b9ebb7 TR |
51 | } else { |
52 | offset = toc->section_offset; | |
53 | size = toc->section_size; | |
54 | } | |
3decb14a JR |
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 | } | |
79b9ebb7 | 62 | |
bf411ea9 | 63 | return gph_verify_header(gph, do_swap32); |
3decb14a JR |
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); | |
bf411ea9 | 93 | uint32_t offset, size; |
3decb14a JR |
94 | |
95 | while (toc->section_offset != 0xffffffff | |
96 | && toc->section_size != 0xffffffff) { | |
79b9ebb7 | 97 | if (do_swap32) { |
bf411ea9 KM |
98 | offset = cpu_to_be32(toc->section_offset); |
99 | size = cpu_to_be32(toc->section_size); | |
79b9ebb7 TR |
100 | } else { |
101 | offset = toc->section_offset; | |
102 | size = toc->section_size; | |
103 | } | |
3decb14a JR |
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 | ||
bf411ea9 | 118 | gph_print_header(gph, do_swap32); |
3decb14a JR |
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, | |
f86ed6a8 | 127 | struct image_tool_params *params) |
3decb14a JR |
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 | ||
3a0e70f1 | 147 | gph_set_header(gph, sbuf->st_size - OMAP_CH_HDR_SIZE, |
bf411ea9 | 148 | params->addr, 0); |
79b9ebb7 TR |
149 | |
150 | if (strncmp(params->imagename, "byteswap", 8) == 0) { | |
151 | do_swap32 = 1; | |
152 | int swapped = 0; | |
153 | uint32_t *data = (uint32_t *)ptr; | |
c8e1ca3e PT |
154 | const off_t size_in_words = |
155 | DIV_ROUND_UP(sbuf->st_size, sizeof(uint32_t)); | |
79b9ebb7 | 156 | |
c8e1ca3e | 157 | while (swapped < size_in_words) { |
bf411ea9 | 158 | *data = cpu_to_be32(*data); |
79b9ebb7 TR |
159 | swapped++; |
160 | data++; | |
161 | } | |
162 | } | |
3decb14a JR |
163 | } |
164 | ||
3decb14a JR |
165 | /* |
166 | * omapimage parameters | |
167 | */ | |
a93648d1 GMF |
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 | ); |