Blame view

arch/powerpc/boot/mktree.c 3.5 KB
0cfcccb46   Josh Boyer   [POWERPC] Add mkt...
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
  /*
   * Makes a tree bootable image for IBM Evaluation boards.
   * Basically, just take a zImage, skip the ELF header, and stuff
   * a 32 byte header on the front.
   *
   * We use htonl, which is a network macro, to make sure we're doing
   * The Right Thing on an LE machine.  It's non-obvious, but it should
   * work on anything BSD'ish.
   */
  
  #include <fcntl.h>
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
  #include <sys/stat.h>
  #include <unistd.h>
  #include <netinet/in.h>
  #ifdef __sun__
  #include <inttypes.h>
  #else
  #include <stdint.h>
  #endif
  
  /* This gets tacked on the front of the image.  There are also a few
   * bytes allocated after the _start label used by the boot rom (see
   * head.S for details).
   */
  typedef struct boot_block {
  	uint32_t bb_magic;		/* 0x0052504F */
  	uint32_t bb_dest;		/* Target address of the image */
  	uint32_t bb_num_512blocks;	/* Size, rounded-up, in 512 byte blks */
  	uint32_t bb_debug_flag;	/* Run debugger or image after load */
  	uint32_t bb_entry_point;	/* The image address to start */
  	uint32_t bb_checksum;	/* 32 bit checksum including header */
  	uint32_t reserved[2];
  } boot_block_t;
  
  #define IMGBLK	512
3c15a6888   Stephen Rothwell   powerpc: use cons...
39
  unsigned int	tmpbuf[IMGBLK / sizeof(unsigned int)];
0cfcccb46   Josh Boyer   [POWERPC] Add mkt...
40
41
42
43
44
  
  int main(int argc, char *argv[])
  {
  	int	in_fd, out_fd;
  	int	nblks, i;
93f1cc609   Timur Tabi   powerpc: Fix mktr...
45
  	unsigned int	cksum, *cp;
0cfcccb46   Josh Boyer   [POWERPC] Add mkt...
46
47
  	struct	stat	st;
  	boot_block_t	bt;
f6dfc8055   David Gibson   [POWERPC] Support...
48
49
50
  	if (argc < 5) {
  		fprintf(stderr, "usage: %s <zImage-file> <boot-image> <load address> <entry point>
  ",argv[0]);
0cfcccb46   Josh Boyer   [POWERPC] Add mkt...
51
52
53
54
55
56
57
58
59
60
61
62
63
  		exit(1);
  	}
  
  	if (stat(argv[1], &st) < 0) {
  		perror("stat");
  		exit(2);
  	}
  
  	nblks = (st.st_size + IMGBLK) / IMGBLK;
  
  	bt.bb_magic = htonl(0x0052504F);
  
  	/* If we have the optional entry point parameter, use it */
f6dfc8055   David Gibson   [POWERPC] Support...
64
65
  	bt.bb_dest = htonl(strtoul(argv[3], NULL, 0));
  	bt.bb_entry_point = htonl(strtoul(argv[4], NULL, 0));
0cfcccb46   Josh Boyer   [POWERPC] Add mkt...
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
  
  	/* We know these from the linker command.
  	 * ...and then move it up into memory a little more so the
  	 * relocation can happen.
  	 */
  	bt.bb_num_512blocks = htonl(nblks);
  	bt.bb_debug_flag = 0;
  
  	bt.bb_checksum = 0;
  
  	/* To be neat and tidy :-).
  	*/
  	bt.reserved[0] = 0;
  	bt.reserved[1] = 0;
  
  	if ((in_fd = open(argv[1], O_RDONLY)) < 0) {
  		perror("zImage open");
  		exit(3);
  	}
  
  	if ((out_fd = open(argv[2], (O_RDWR | O_CREAT | O_TRUNC), 0666)) < 0) {
  		perror("bootfile open");
  		exit(3);
  	}
  
  	cksum = 0;
  	cp = (void *)&bt;
93f1cc609   Timur Tabi   powerpc: Fix mktr...
93
  	for (i = 0; i < sizeof(bt) / sizeof(unsigned int); i++)
0cfcccb46   Josh Boyer   [POWERPC] Add mkt...
94
95
96
97
  		cksum += *cp++;
  
  	/* Assume zImage is an ELF file, and skip the 64K header.
  	*/
3c15a6888   Stephen Rothwell   powerpc: use cons...
98
  	if (read(in_fd, tmpbuf, sizeof(tmpbuf)) != sizeof(tmpbuf)) {
0cfcccb46   Josh Boyer   [POWERPC] Add mkt...
99
100
101
102
103
  		fprintf(stderr, "%s is too small to be an ELF image
  ",
  				argv[1]);
  		exit(4);
  	}
3c15a6888   Stephen Rothwell   powerpc: use cons...
104
  	if (tmpbuf[0] != htonl(0x7f454c46)) {
0cfcccb46   Josh Boyer   [POWERPC] Add mkt...
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
  		fprintf(stderr, "%s is not an ELF image
  ", argv[1]);
  		exit(4);
  	}
  
  	if (lseek(in_fd, (64 * 1024), SEEK_SET) < 0) {
  		fprintf(stderr, "%s failed to seek in ELF image
  ", argv[1]);
  		exit(4);
  	}
  
  	nblks -= (64 * 1024) / IMGBLK;
  
  	/* And away we go......
  	*/
  	if (write(out_fd, &bt, sizeof(bt)) != sizeof(bt)) {
  		perror("boot-image write");
  		exit(5);
  	}
  
  	while (nblks-- > 0) {
3c15a6888   Stephen Rothwell   powerpc: use cons...
126
  		if (read(in_fd, tmpbuf, sizeof(tmpbuf)) < 0) {
0cfcccb46   Josh Boyer   [POWERPC] Add mkt...
127
128
129
  			perror("zImage read");
  			exit(5);
  		}
3c15a6888   Stephen Rothwell   powerpc: use cons...
130
  		cp = tmpbuf;
93f1cc609   Timur Tabi   powerpc: Fix mktr...
131
  		for (i = 0; i < sizeof(tmpbuf) / sizeof(unsigned int); i++)
0cfcccb46   Josh Boyer   [POWERPC] Add mkt...
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
  			cksum += *cp++;
  		if (write(out_fd, tmpbuf, sizeof(tmpbuf)) != sizeof(tmpbuf)) {
  			perror("boot-image write");
  			exit(5);
  		}
  	}
  
  	/* rewrite the header with the computed checksum.
  	*/
  	bt.bb_checksum = htonl(cksum);
  	if (lseek(out_fd, 0, SEEK_SET) < 0) {
  		perror("rewrite seek");
  		exit(1);
  	}
  	if (write(out_fd, &bt, sizeof(bt)) != sizeof(bt)) {
  		perror("boot-image rewrite");
  		exit(1);
  	}
  
  	exit(0);
  }