2 * (C) Copyright 2008 Semihalf
4 * (C) Copyright 2000-2006
7 * See file CREDITS for list of people who contributed to this
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 # include <watchdog.h>
34 unsigned long crc32 (unsigned long, const unsigned char *, unsigned int);
36 int image_check_hcrc (image_header_t *hdr)
39 ulong len = image_get_header_size ();
40 image_header_t header;
42 /* Copy header so we can blank CRC field for re-calculation */
43 memmove (&header, (char *)hdr, image_get_header_size ());
44 image_set_hcrc (&header, 0);
46 hcrc = crc32 (0, (unsigned char *)&header, len);
48 return (hcrc == image_get_hcrc (hdr));
51 int image_check_dcrc (image_header_t *hdr)
53 ulong data = image_get_data (hdr);
54 ulong len = image_get_data_size (hdr);
55 ulong dcrc = crc32 (0, (unsigned char *)data, len);
57 return (dcrc == image_get_dcrc (hdr));
61 int image_check_dcrc_wd (image_header_t *hdr, ulong chunksz)
64 ulong len = image_get_data_size (hdr);
65 ulong data = image_get_data (hdr);
67 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
69 ulong edata = cdata + len;
71 while (cdata < edata) {
72 ulong chunk = edata - cdata;
76 dcrc = crc32 (dcrc, (unsigned char *)cdata, chunk);
82 dcrc = crc32 (0, (unsigned char *)data, len);
85 return (dcrc == image_get_dcrc (hdr));
88 int getenv_verify (void)
90 char *s = getenv ("verify");
91 return (s && (*s == 'n')) ? 0 : 1;
94 void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
96 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
98 size_t tail = (len > chunksz) ? chunksz : len;
100 memmove (to, from, tail);
105 #else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
106 memmove (to, from, len);
107 #endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
109 #endif /* USE_HOSTCC */
112 * image_multi_count - get component (sub-image) count
113 * @hdr: pointer to the header of the multi component image
115 * image_multi_count() returns number of components in a multi
118 * Note: no checking of the image type is done, caller must pass
119 * a valid multi component image.
122 * number of components
124 ulong image_multi_count (image_header_t *hdr)
129 /* get start of the image payload, which in case of multi
130 * component images that points to a table of component sizes */
131 size = (ulong *)image_get_data (hdr);
133 /* count non empty slots */
134 for (i = 0; size[i]; ++i)
141 * image_multi_getimg - get component data address and size
142 * @hdr: pointer to the header of the multi component image
143 * @idx: index of the requested component
144 * @data: pointer to a ulong variable, will hold component data address
145 * @len: pointer to a ulong variable, will hold component size
147 * image_multi_getimg() returns size and data address for the requested
148 * component in a multi component image.
150 * Note: no checking of the image type is done, caller must pass
151 * a valid multi component image.
154 * data address and size of the component, if idx is valid
155 * 0 in data and len, if idx is out of range
157 void image_multi_getimg (image_header_t *hdr, ulong idx,
158 ulong *data, ulong *len)
162 ulong offset, tail, count, img_data;
164 /* get number of component */
165 count = image_multi_count (hdr);
167 /* get start of the image payload, which in case of multi
168 * component images that points to a table of component sizes */
169 size = (ulong *)image_get_data (hdr);
171 /* get address of the proper component data start, which means
172 * skipping sizes table (add 1 for last, null entry) */
173 img_data = image_get_data (hdr) + (count + 1) * sizeof (ulong);
180 /* go over all indices preceding requested component idx */
181 for (i = 0; i < idx; i++) {
182 /* add up i-th component size */
185 /* add up alignment for i-th component */
186 tail += (4 - size[i] % 4);
189 /* calculate idx-th component data address */
190 *data = img_data + offset + tail;