blob: e9d072975bb8b330d5868699e73f11f18376ca95 [file] [log] [blame]
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +05301/*
2 * (C) Copyright 2008 Semihalf
3 *
4 * (C) Copyright 2000-2004
5 * DENX Software Engineering
6 * Wolfgang Denk, wd@denx.de
7 *
8 * Updated-by: Prafulla Wadaskar <prafulla@marvell.com>
9 * default_image specific code abstracted from mkimage.c
10 * some functions added to address abstraction
11 *
12 * All rights reserved.
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 * MA 02111-1307 USA
28 */
29
30#include "mkimage.h"
31#include <image.h>
32#include <u-boot/crc.h>
33
34static image_header_t header;
35
Stephen Warren6904b6e2011-10-18 11:11:49 +000036static int image_check_image_types(uint8_t type)
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053037{
Stephen Warren8c87eba2011-11-10 13:17:53 -070038 if (((type > IH_TYPE_INVALID) && (type < IH_TYPE_FLATDT)) ||
39 (type == IH_TYPE_KERNEL_NOLOAD))
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053040 return EXIT_SUCCESS;
41 else
42 return EXIT_FAILURE;
43}
44
Stephen Warren6904b6e2011-10-18 11:11:49 +000045static int image_check_params(struct mkimage_params *params)
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053046{
47 return ((params->dflag && (params->fflag || params->lflag)) ||
48 (params->fflag && (params->dflag || params->lflag)) ||
49 (params->lflag && (params->dflag || params->fflag)));
50}
51
Stephen Warren6904b6e2011-10-18 11:11:49 +000052static int image_verify_header(unsigned char *ptr, int image_size,
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053053 struct mkimage_params *params)
54{
55 uint32_t len;
56 const unsigned char *data;
57 uint32_t checksum;
58 image_header_t header;
59 image_header_t *hdr = &header;
60
61 /*
62 * create copy of header so that we can blank out the
63 * checksum field for checking - this can't be done
64 * on the PROT_READ mapped data.
65 */
Stephen Warren6904b6e2011-10-18 11:11:49 +000066 memcpy(hdr, ptr, sizeof(image_header_t));
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053067
68 if (be32_to_cpu(hdr->ih_magic) != IH_MAGIC) {
Stephen Warren6904b6e2011-10-18 11:11:49 +000069 fprintf(stderr,
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053070 "%s: Bad Magic Number: \"%s\" is no valid image\n",
71 params->cmdname, params->imagefile);
72 return -FDT_ERR_BADMAGIC;
73 }
74
75 data = (const unsigned char *)hdr;
76 len = sizeof(image_header_t);
77
78 checksum = be32_to_cpu(hdr->ih_hcrc);
79 hdr->ih_hcrc = cpu_to_be32(0); /* clear for re-calculation */
80
Stephen Warren6904b6e2011-10-18 11:11:49 +000081 if (crc32(0, data, len) != checksum) {
82 fprintf(stderr,
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053083 "%s: ERROR: \"%s\" has bad header checksum!\n",
84 params->cmdname, params->imagefile);
85 return -FDT_ERR_BADSTATE;
86 }
87
88 data = (const unsigned char *)ptr + sizeof(image_header_t);
89 len = image_size - sizeof(image_header_t) ;
90
91 checksum = be32_to_cpu(hdr->ih_dcrc);
Stephen Warren6904b6e2011-10-18 11:11:49 +000092 if (crc32(0, data, len) != checksum) {
93 fprintf(stderr,
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +053094 "%s: ERROR: \"%s\" has corrupted data!\n",
95 params->cmdname, params->imagefile);
96 return -FDT_ERR_BADSTRUCTURE;
97 }
98 return 0;
99}
100
Stephen Warren6904b6e2011-10-18 11:11:49 +0000101static void image_set_header(void *ptr, struct stat *sbuf, int ifd,
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530102 struct mkimage_params *params)
103{
104 uint32_t checksum;
105
106 image_header_t * hdr = (image_header_t *)ptr;
107
Stephen Warren6904b6e2011-10-18 11:11:49 +0000108 checksum = crc32(0,
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530109 (const unsigned char *)(ptr +
110 sizeof(image_header_t)),
111 sbuf->st_size - sizeof(image_header_t));
112
113 /* Build new header */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000114 image_set_magic(hdr, IH_MAGIC);
115 image_set_time(hdr, sbuf->st_mtime);
116 image_set_size(hdr, sbuf->st_size - sizeof(image_header_t));
117 image_set_load(hdr, params->addr);
118 image_set_ep(hdr, params->ep);
119 image_set_dcrc(hdr, checksum);
120 image_set_os(hdr, params->os);
121 image_set_arch(hdr, params->arch);
122 image_set_type(hdr, params->type);
123 image_set_comp(hdr, params->comp);
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530124
Stephen Warren6904b6e2011-10-18 11:11:49 +0000125 image_set_name(hdr, params->imagename);
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530126
Stephen Warren6904b6e2011-10-18 11:11:49 +0000127 checksum = crc32(0, (const unsigned char *)hdr,
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530128 sizeof(image_header_t));
129
Stephen Warren6904b6e2011-10-18 11:11:49 +0000130 image_set_hcrc(hdr, checksum);
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530131}
132
133/*
134 * Default image type parameters definition
135 */
136static struct image_type_params defimage_params = {
137 .name = "Default Image support",
138 .header_size = sizeof(image_header_t),
139 .hdr = (void*)&header,
140 .check_image_type = image_check_image_types,
141 .verify_header = image_verify_header,
142 .print_header = image_print_contents,
143 .set_header = image_set_header,
144 .check_params = image_check_params,
145};
146
Stephen Warren6904b6e2011-10-18 11:11:49 +0000147void init_default_image_type(void)
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530148{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000149 mkimage_register(&defimage_params);
Prafulla Wadaskarfabf3cf2009-08-19 17:36:46 +0530150}