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 */ 110 111 /** 112 * image_multi_count - get component (sub-image) count 113 * @hdr: pointer to the header of the multi component image 114 * 115 * image_multi_count() returns number of components in a multi 116 * component image. 117 * 118 * Note: no checking of the image type is done, caller must pass 119 * a valid multi component image. 120 * 121 * returns: 122 * number of components 123 */ 124 ulong image_multi_count (image_header_t *hdr) 125 { 126 ulong i, count = 0; 127 ulong *size; 128 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); 132 133 /* count non empty slots */ 134 for (i = 0; size[i]; ++i) 135 count++; 136 137 return count; 138 } 139 140 /** 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 146 * 147 * image_multi_getimg() returns size and data address for the requested 148 * component in a multi component image. 149 * 150 * Note: no checking of the image type is done, caller must pass 151 * a valid multi component image. 152 * 153 * returns: 154 * data address and size of the component, if idx is valid 155 * 0 in data and len, if idx is out of range 156 */ 157 void image_multi_getimg (image_header_t *hdr, ulong idx, 158 ulong *data, ulong *len) 159 { 160 int i; 161 ulong *size; 162 ulong offset, tail, count, img_data; 163 164 /* get number of component */ 165 count = image_multi_count (hdr); 166 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); 170 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); 174 175 if (idx < count) { 176 *len = size[idx]; 177 offset = 0; 178 tail = 0; 179 180 /* go over all indices preceding requested component idx */ 181 for (i = 0; i < idx; i++) { 182 /* add up i-th component size */ 183 offset += size[i]; 184 185 /* add up alignment for i-th component */ 186 tail += (4 - size[i] % 4); 187 } 188 189 /* calculate idx-th component data address */ 190 *data = img_data + offset + tail; 191 } else { 192 *len = 0; 193 *data = 0; 194 } 195 } 196 197 #ifndef USE_HOSTCC 198 const char* image_get_os_name (uint8_t os) 199 { 200 const char *name; 201 202 switch (os) { 203 case IH_OS_INVALID: name = "Invalid OS"; break; 204 case IH_OS_NETBSD: name = "NetBSD"; break; 205 case IH_OS_LINUX: name = "Linux"; break; 206 case IH_OS_VXWORKS: name = "VxWorks"; break; 207 case IH_OS_QNX: name = "QNX"; break; 208 case IH_OS_U_BOOT: name = "U-Boot"; break; 209 case IH_OS_RTEMS: name = "RTEMS"; break; 210 #ifdef CONFIG_ARTOS 211 case IH_OS_ARTOS: name = "ARTOS"; break; 212 #endif 213 #ifdef CONFIG_LYNXKDI 214 case IH_OS_LYNXOS: name = "LynxOS"; break; 215 #endif 216 default: name = "Unknown OS"; break; 217 } 218 219 return name; 220 } 221 222 const char* image_get_arch_name (uint8_t arch) 223 { 224 const char *name; 225 226 switch (arch) { 227 case IH_ARCH_INVALID: name = "Invalid Architecture"; break; 228 case IH_ARCH_ALPHA: name = "Alpha"; break; 229 case IH_ARCH_ARM: name = "ARM"; break; 230 case IH_ARCH_AVR32: name = "AVR32"; break; 231 case IH_ARCH_BLACKFIN: name = "Blackfin"; break; 232 case IH_ARCH_I386: name = "Intel x86"; break; 233 case IH_ARCH_IA64: name = "IA64"; break; 234 case IH_ARCH_M68K: name = "M68K"; break; 235 case IH_ARCH_MICROBLAZE:name = "Microblaze"; break; 236 case IH_ARCH_MIPS64: name = "MIPS 64 Bit"; break; 237 case IH_ARCH_MIPS: name = "MIPS"; break; 238 case IH_ARCH_NIOS2: name = "Nios-II"; break; 239 case IH_ARCH_NIOS: name = "Nios"; break; 240 case IH_ARCH_PPC: name = "PowerPC"; break; 241 case IH_ARCH_S390: name = "IBM S390"; break; 242 case IH_ARCH_SH: name = "SuperH"; break; 243 case IH_ARCH_SPARC64: name = "SPARC 64 Bit"; break; 244 case IH_ARCH_SPARC: name = "SPARC"; break; 245 default: name = "Unknown Architecture"; break; 246 } 247 248 return name; 249 } 250 251 const char* image_get_type_name (uint8_t type) 252 { 253 const char *name; 254 255 switch (type) { 256 case IH_TYPE_INVALID: name = "Invalid Image"; break; 257 case IH_TYPE_STANDALONE:name = "Standalone Program"; break; 258 case IH_TYPE_KERNEL: name = "Kernel Image"; break; 259 case IH_TYPE_RAMDISK: name = "RAMDisk Image"; break; 260 case IH_TYPE_MULTI: name = "Multi-File Image"; break; 261 case IH_TYPE_FIRMWARE: name = "Firmware"; break; 262 case IH_TYPE_SCRIPT: name = "Script"; break; 263 case IH_TYPE_FLATDT: name = "Flat Device Tree"; break; 264 default: name = "Unknown Image"; break; 265 } 266 267 return name; 268 } 269 270 const char* image_get_comp_name (uint8_t comp) 271 { 272 const char *name; 273 274 switch (comp) { 275 case IH_COMP_NONE: name = "uncompressed"; break; 276 case IH_COMP_GZIP: name = "gzip compressed"; break; 277 case IH_COMP_BZIP2: name = "bzip2 compressed"; break; 278 default: name = "unknown compression"; break; 279 } 280 281 return name; 282 } 283 #endif 284