xref: /openbmc/u-boot/tools/default_image.c (revision 5794619e)
1 /*
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 
34 static image_header_t header;
35 
36 static int image_check_image_types (uint8_t type)
37 {
38 	if ((type > IH_TYPE_INVALID) && (type < IH_TYPE_FLATDT))
39 		return EXIT_SUCCESS;
40 	else
41 		return EXIT_FAILURE;
42 }
43 
44 static int image_check_params (struct mkimage_params *params)
45 {
46 	return	((params->dflag && (params->fflag || params->lflag)) ||
47 		(params->fflag && (params->dflag || params->lflag)) ||
48 		(params->lflag && (params->dflag || params->fflag)));
49 }
50 
51 static int image_verify_header (unsigned char *ptr, int image_size,
52 			struct mkimage_params *params)
53 {
54 	uint32_t len;
55 	const unsigned char *data;
56 	uint32_t checksum;
57 	image_header_t header;
58 	image_header_t *hdr = &header;
59 
60 	/*
61 	 * create copy of header so that we can blank out the
62 	 * checksum field for checking - this can't be done
63 	 * on the PROT_READ mapped data.
64 	 */
65 	memcpy (hdr, ptr, sizeof(image_header_t));
66 
67 	if (be32_to_cpu(hdr->ih_magic) != IH_MAGIC) {
68 		fprintf (stderr,
69 			"%s: Bad Magic Number: \"%s\" is no valid image\n",
70 			params->cmdname, params->imagefile);
71 		return -FDT_ERR_BADMAGIC;
72 	}
73 
74 	data = (const unsigned char *)hdr;
75 	len  = sizeof(image_header_t);
76 
77 	checksum = be32_to_cpu(hdr->ih_hcrc);
78 	hdr->ih_hcrc = cpu_to_be32(0);	/* clear for re-calculation */
79 
80 	if (crc32 (0, data, len) != checksum) {
81 		fprintf (stderr,
82 			"%s: ERROR: \"%s\" has bad header checksum!\n",
83 			params->cmdname, params->imagefile);
84 		return -FDT_ERR_BADSTATE;
85 	}
86 
87 	data = (const unsigned char *)ptr + sizeof(image_header_t);
88 	len  = image_size - sizeof(image_header_t) ;
89 
90 	checksum = be32_to_cpu(hdr->ih_dcrc);
91 	if (crc32 (0, data, len) != checksum) {
92 		fprintf (stderr,
93 			"%s: ERROR: \"%s\" has corrupted data!\n",
94 			params->cmdname, params->imagefile);
95 		return -FDT_ERR_BADSTRUCTURE;
96 	}
97 	return 0;
98 }
99 
100 static void image_set_header (void *ptr, struct stat *sbuf, int ifd,
101 				struct mkimage_params *params)
102 {
103 	uint32_t checksum;
104 
105 	image_header_t * hdr = (image_header_t *)ptr;
106 
107 	checksum = crc32 (0,
108 			(const unsigned char *)(ptr +
109 				sizeof(image_header_t)),
110 			sbuf->st_size - sizeof(image_header_t));
111 
112 	/* Build new header */
113 	image_set_magic (hdr, IH_MAGIC);
114 	image_set_time (hdr, sbuf->st_mtime);
115 	image_set_size (hdr, sbuf->st_size - sizeof(image_header_t));
116 	image_set_load (hdr, params->addr);
117 	image_set_ep (hdr, params->ep);
118 	image_set_dcrc (hdr, checksum);
119 	image_set_os (hdr, params->os);
120 	image_set_arch (hdr, params->arch);
121 	image_set_type (hdr, params->type);
122 	image_set_comp (hdr, params->comp);
123 
124 	image_set_name (hdr, params->imagename);
125 
126 	checksum = crc32 (0, (const unsigned char *)hdr,
127 				sizeof(image_header_t));
128 
129 	image_set_hcrc (hdr, checksum);
130 }
131 
132 /*
133  * Default image type parameters definition
134  */
135 static struct image_type_params defimage_params = {
136 	.name = "Default Image support",
137 	.header_size = sizeof(image_header_t),
138 	.hdr = (void*)&header,
139 	.check_image_type = image_check_image_types,
140 	.verify_header = image_verify_header,
141 	.print_header = image_print_contents,
142 	.set_header = image_set_header,
143 	.check_params = image_check_params,
144 };
145 
146 void init_default_image_type (void)
147 {
148 	mkimage_register (&defimage_params);
149 }
150