048b866ce65e29a3e74615339866e98878e5dcb4
[oweals/u-boot.git] / common / image.c
1 /*
2  * (C) Copyright 2008 Semihalf
3  *
4  * (C) Copyright 2000-2006
5  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6  *
7  * See file CREDITS for list of people who contributed to this
8  * project.
9  *
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.
14  *
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.
19  *
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,
23  * MA 02111-1307 USA
24  */
25 #ifndef USE_HOSTCC
26 # include <common.h>
27 # include <watchdog.h>
28 #else
29 # include "mkimage.h"
30 #endif
31
32 #include <image.h>
33
34 unsigned long crc32 (unsigned long, const unsigned char *, unsigned int);
35
36 int image_check_hcrc (image_header_t *hdr)
37 {
38         ulong hcrc;
39         ulong len = image_get_header_size ();
40         image_header_t header;
41
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);
45
46         hcrc = crc32 (0, (unsigned char *)&header, len);
47
48         return (hcrc == image_get_hcrc (hdr));
49 }
50
51 int image_check_dcrc (image_header_t *hdr)
52 {
53         ulong data = image_get_data (hdr);
54         ulong len = image_get_data_size (hdr);
55         ulong dcrc = crc32 (0, (unsigned char *)data, len);
56
57         return (dcrc == image_get_dcrc (hdr));
58 }
59
60 #ifndef USE_HOSTCC
61 int image_check_dcrc_wd (image_header_t *hdr, ulong chunksz)
62 {
63         ulong dcrc = 0;
64         ulong len = image_get_data_size (hdr);
65         ulong data = image_get_data (hdr);
66
67 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
68         ulong cdata = data;
69         ulong edata = cdata + len;
70
71         while (cdata < edata) {
72                 ulong chunk = edata - cdata;
73
74                 if (chunk > chunksz)
75                         chunk = chunksz;
76                 dcrc = crc32 (dcrc, (unsigned char *)cdata, chunk);
77                 cdata += chunk;
78
79                 WATCHDOG_RESET ();
80         }
81 #else
82         dcrc = crc32 (0, (unsigned char *)data, len);
83 #endif
84
85         return (dcrc == image_get_dcrc (hdr));
86 }
87
88 int getenv_verify (void)
89 {
90         char *s = getenv ("verify");
91         return (s && (*s == 'n')) ? 0 : 1;
92 }
93
94 void memmove_wd (void *to, void *from, size_t len, ulong chunksz)
95 {
96 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
97         while (len > 0) {
98                 size_t tail = (len > chunksz) ? chunksz : len;
99                 WATCHDOG_RESET ();
100                 memmove (to, from, tail);
101                 to += tail;
102                 from += tail;
103                 len -= tail;
104         }
105 #else   /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
106         memmove (to, from, len);
107 #endif  /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
108 }
109 #endif /* USE_HOSTCC */