summaryrefslogtreecommitdiffstats
path: root/tools/mkimage/patches/030-allow-to-use-different-magic.patch
blob: febeb9295fa552ab873c36a149d896d9d9bef388 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
From f3d2056b81b7a92d28402c22736534d84fe23cfe Mon Sep 17 00:00:00 2001
From: Gabor Juhos <juhosg@openwrt.org>
Date: Mon, 5 Dec 2011 14:52:22 +0000
Subject: [PATCH] tools/mkimage: allow to use different magic in uImages

This patch makes it possible to set a custom image magic.

--- a/tools/dumpimage.c
+++ b/tools/dumpimage.c
@@ -7,12 +7,15 @@
 
 #include "dumpimage.h"
 #include <image.h>
+#include <stdint.h>
 #include <version.h>
 
 static void usage(void);
 
 /* parameters initialized by core will be used by the image type code */
-static struct image_tool_params params;
+static struct image_tool_params params = {
+	.magic = IH_MAGIC,
+};
 
 /*
  * dumpimage_extract_subimage -
@@ -67,16 +70,26 @@ int main(int argc, char **argv)
 	int ifd = -1;
 	struct stat sbuf;
 	char *ptr;
+	unsigned long long magic;
 	int retval = EXIT_SUCCESS;
 	struct image_type_params *tparams = NULL;
 
 	params.cmdname = *argv;
 
-	while ((opt = getopt(argc, argv, "hlo:T:p:V")) != -1) {
+	while ((opt = getopt(argc, argv, "hlM:o:T:p:V")) != -1) {
 		switch (opt) {
 		case 'l':
 			params.lflag = 1;
 			break;
+		case 'M':
+			magic = strtoull(optarg, &ptr, 16);
+			if (ptr == optarg || *ptr || magic > UINT32_MAX) {
+				fprintf(stderr, "%s: invalid magic %s\n",
+					params.cmdname, optarg);
+				exit(EXIT_FAILURE);
+			}
+			params.magic = magic;
+			break;
 		case 'o':
 			params.outfile = optarg;
 			params.iflag = 1;
@@ -201,13 +214,15 @@ int main(int argc, char **argv)
 
 static void usage(void)
 {
-	fprintf(stderr, "Usage: %s [-T type] -l image\n"
+	fprintf(stderr, "Usage: %s [-T type] [-M magic] -l image\n"
 		"          -l ==> list image header information\n"
-		"          -T ==> parse image file as 'type'\n",
+		"          -T ==> parse image file as 'type'\n"
+		"          -M ==> set expected image magic to 'magic'\n",
 		params.cmdname);
 	fprintf(stderr,
-		"       %s [-T type] [-p position] [-o outfile] image\n"
+		"       %s [-T type] [-M magic] [-p position] [-o outfile] image\n"
 		"          -T ==> declare image type as 'type'\n"
+		"          -M ==> set expected image magic to 'magic'\n"
 		"          -p ==> 'position' (starting at 0) of the component to extract from image\n"
 		"          -o ==> extract component to file 'outfile'\n",
 		params.cmdname);
--- a/tools/mkimage.c
+++ b/tools/mkimage.c
@@ -13,6 +13,7 @@
 #include <fit_common.h>
 #include <getopt.h>
 #include <image.h>
+#include <stdint.h>
 #include <version.h>
 #ifdef __linux__
 #include <sys/ioctl.h>
@@ -26,6 +27,7 @@ static struct image_tool_params params =
 	.arch = IH_ARCH_PPC,
 	.type = IH_TYPE_KERNEL,
 	.comp = IH_COMP_GZIP,
+	.magic = IH_MAGIC,
 	.dtc = MKIMAGE_DEFAULT_DTC_OPTIONS,
 	.imagename = "",
 	.imagename2 = "",
@@ -89,11 +91,12 @@ static void usage(const char *msg)
 			 "          -q ==> quiet\n",
 		params.cmdname);
 	fprintf(stderr,
-		"       %s [-x] -A arch -O os -T type -C comp -a addr -e ep -n name -d data_file[:data_file...] image\n"
+		"       %s [-x] -A arch -O os -T type -C comp -M magic -a addr -e ep -n name -d data_file[:data_file...] image\n"
 		"          -A ==> set architecture to 'arch'\n"
 		"          -O ==> set operating system to 'os'\n"
 		"          -T ==> set image type to 'type'\n"
 		"          -C ==> set compression type 'comp'\n"
+		"          -M ==> set image magic to 'magic'\n"
 		"          -a ==> set load address to 'addr' (hex)\n"
 		"          -e ==> set entry point to 'ep' (hex)\n"
 		"          -n ==> set image name to 'name'\n"
@@ -164,7 +167,7 @@ static int add_content(int type, const c
 }
 
 static const char optstring[] =
-	"a:A:b:B:c:C:d:D:e:Ef:Fg:G:i:k:K:ln:N:o:O:p:qrR:stT:vVxy:Y:z:Z:";
+	"a:A:b:B:c:C:d:D:e:Ef:Fg:G:i:k:K:lM:n:N:o:O:p:qrR:stT:vVxy:Y:z:Z:";
 
 static const struct option longopts[] = {
 	{ "load-address", required_argument, NULL, 'a' },
@@ -210,6 +213,7 @@ static const struct option longopts[] =
 static void process_args(int argc, char **argv)
 {
 	char *ptr;
+	unsigned long long magic;
 	int type = IH_TYPE_INVALID;
 	char *datafile = NULL;
 	int opt;
@@ -312,6 +316,15 @@ static void process_args(int argc, char
 		case 'l':
 			params.lflag = 1;
 			break;
+		case 'M':
+			magic = strtoull(optarg, &ptr, 16);
+			if (ptr == optarg || *ptr || magic > UINT32_MAX) {
+				fprintf(stderr, "%s: invalid magic %s\n",
+					params.cmdname, optarg);
+				exit(EXIT_FAILURE);
+			}
+			params.magic = magic;
+			break;
 		case 'n':
 			params.imagename = optarg;
 			break;
--- a/tools/default_image.c
+++ b/tools/default_image.c
@@ -67,7 +67,7 @@ static int image_verify_header(unsigned
 	 */
 	memcpy(hdr, ptr, sizeof(struct legacy_img_hdr));
 
-	if (be32_to_cpu(hdr->ih_magic) != IH_MAGIC) {
+	if (be32_to_cpu(hdr->ih_magic) != params->magic) {
 		debug("%s: Bad Magic Number: \"%s\" is no valid image\n",
 		      params->cmdname, params->imagefile);
 		return -FDT_ERR_BADMAGIC;
@@ -146,7 +146,7 @@ static void image_set_header(void *ptr,
 	}
 
 	/* Build new header */
-	image_set_magic(hdr, IH_MAGIC);
+	image_set_magic(hdr, params->magic);
 	image_set_time(hdr, time);
 	image_set_size(hdr, imagesize);
 	image_set_load(hdr, addr);
--- a/tools/imagetool.h
+++ b/tools/imagetool.h
@@ -67,6 +67,7 @@ struct image_tool_params {
 	int arch;
 	int type;
 	int comp;
+	unsigned int magic;
 	char *dtc;
 	unsigned int addr;
 	unsigned int ep;