summaryrefslogtreecommitdiff
path: root/util/mkelfImage/linux-ia64/mkelf-linux-ia64.c
blob: cfd79869aebfd97096a9c76beec5765ca3599ced (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#define _GNU_SOURCE
#include <getopt.h>
#include "elf.h"
#include "elf_boot.h"
#include "convert.h"
#include "mkelfImage.h"

static unsigned char payload[] = {
#include "convert.bin.c"
};

char *linux_ia64_probe(char *kernel_buf, off_t kernel_size)
{
	Elf64_Ehdr *ehdr;
	Elf64_Phdr *phdr;
	int i;
	int phdrs;
	ehdr = (Elf64_Ehdr *)kernel_buf;
	if (
		(ehdr->e_ident[EI_MAG0] != ELFMAG0) ||
		(ehdr->e_ident[EI_MAG1] != ELFMAG1) ||
		(ehdr->e_ident[EI_MAG2] != ELFMAG2) ||
		(ehdr->e_ident[EI_MAG3] != ELFMAG3)) {
		return "No ELF signature found on kernel\n";
	}
	if (ehdr->e_ident[EI_CLASS] != ELFCLASS64) {
		return "Not a 64bit ELF kernel\n";
	}
	if (ehdr->e_ident[EI_DATA]  != ELFDATA2LSB) {
		return "Not a little endian ELF kernel\n";
	}
	if (le16_to_cpu(ehdr->e_type) != ET_EXEC) {
		return "Not an executable kernel\n";
	}
	if (le16_to_cpu(ehdr->e_machine) != EM_IA_64) {
		return "Not an ia64 kernel\n";
	}
	if (	(ehdr->e_ident[EI_VERSION] != EV_CURRENT) ||
		(le32_to_cpu(ehdr->e_version) != EV_CURRENT)) {
		return "Kernel not using ELF version 1.\n";
	}
	if (le16_to_cpu(ehdr->e_phentsize) != sizeof(*phdr)) {
		return "Kernel uses bad program header size.\n";
	}
	phdr = (Elf64_Phdr *)(kernel_buf + le64_to_cpu(ehdr->e_phoff));
	phdrs = 0;
	for(i = 0; i < le16_to_cpu(ehdr->e_phnum); i++) {
		if (le32_to_cpu(phdr[i].p_type) != PT_LOAD)
			continue;
		phdrs++;
	}
	if (phdrs == 0) {
		return "No PT_LOAD segments!\n";
	}
	return 0;
}

struct kernel_info
{
	int phdrs;
	char *kernel_buf;
	Elf64_Ehdr *ehdr;
	Elf64_Phdr *phdr;
	uint64_t entry;
	char *version;
};

static void parse_kernel(struct kernel_info *info,
	char *kernel_buf, size_t kernel_size)
{
	Elf64_Ehdr *ehdr;
	Elf64_Phdr *phdr;
	int i;
	int phdrs;
	ehdr = (Elf64_Ehdr *)kernel_buf;
	phdr = (Elf64_Phdr *)(kernel_buf + le64_to_cpu(ehdr->e_phoff));
	phdrs = 0;
	for(i = 0; i < le16_to_cpu(ehdr->e_phnum); i++) {
		if (le32_to_cpu(phdr[i].p_type) != PT_LOAD)
			continue;
		phdrs++;
	}
	if (phdrs == 0) {
		die("No PT_LOAD segments!\n");
	}
	info->kernel_buf = kernel_buf;
	info->ehdr    = ehdr;
	info->phdrs   = phdrs;
	info->phdr    = phdr;
	info->entry   = le64_to_cpu(ehdr->e_entry);
	info->version = "unknown";
}

static int populate_kernel_phdrs(struct kernel_info *info, struct memelfphdr *phdr)
{
	uint64_t paddr;
	int i;
	paddr = 0;
	for(i = 0; i < info->phdrs; i++) {
		Elf64_Phdr *hdr;
		int j;
		hdr = 0;
		for(j = 0; j < le16_to_cpu(info->ehdr->e_phnum); j++) {
			if (le16_to_cpu(info->phdr[j].p_type != PT_LOAD)) {
				continue;
			}
			if (paddr > le64_to_cpu(info->phdr[j].p_paddr)) {
				continue;
			}
			if (hdr && 
				le64_to_cpu(hdr->p_paddr) < 
				le64_to_cpu(info->phdr[j].p_paddr)) {
				continue;
			}
			hdr = info->phdr + j;
		}
		if (!hdr) {
			die("Expected %d phdrs found %d!", info->phdrs, i);
		}
		phdr[i].p_paddr  = le64_to_cpu(hdr->p_paddr);
		phdr[i].p_vaddr  = le64_to_cpu(hdr->p_vaddr);
		phdr[i].p_filesz = le64_to_cpu(hdr->p_filesz);
		phdr[i].p_memsz  = le64_to_cpu(hdr->p_memsz);
		phdr[i].p_data   = info->kernel_buf + le64_to_cpu(hdr->p_offset);
		paddr = phdr[i].p_paddr + phdr[i].p_memsz;
	}
	return i;
}
	

void linux_ia64_usage(void)
{
	printf(
		"      --command-line=<string> Set the command line to <string>\n"
		"      --append=<string>       Set the command line to <string>\n"
		"      --initrd=<filename>     Set the initrd to <filename>\n"
		"      --ramdisk=<filename>    Set the initrd to <filename>\n"
		);
	return;
}

#define OPT_CMDLINE        OPT_MAX+0
#define OPT_RAMDISK        OPT_MAX+1

int linux_ia64_mkelf(int argc, char **argv,
	struct memelfheader *ehdr, char *kernel_buf, off_t kernel_size)
{
	const char *ramdisk, *cmdline;
	char *payload_buf, *ramdisk_buf;
	off_t payload_size, ramdisk_size;
	struct memelfphdr *phdr;
	struct memelfnote *note;
	struct kernel_info kinfo;
	struct image_parameters *params;
	int index;

	int opt;
	static const struct option options[] = {
		MKELF_OPTIONS
		{ "command-line",    1, 0, OPT_CMDLINE },
		{ "append",          1, 0, OPT_CMDLINE },
		{ "initrd",          1, 0, OPT_RAMDISK },
		{ "ramdisk",         1, 0, OPT_RAMDISK },
		{ 0 , 0, 0, 0 },
	};
	static const char short_options[] = "HV";

	ramdisk = 0;
	cmdline="";

	while((opt = getopt_long(argc, argv, short_options, options, 0)) != -1) {
		switch(opt) {
		case '?':
			error("Unknown option %s\n", argv[optind]);
			break;
		case OPT_RAMDISK:
			ramdisk = optarg;
			break;
		case OPT_CMDLINE:
			cmdline = optarg;
			break;
		default:
			break;
		}
	}
	ehdr->ei_class  = ELFCLASS64;
	ehdr->ei_data   = ELFDATA2LSB;
	ehdr->e_type    = ET_EXEC;
	ehdr->e_machine = EM_IA_64;
	
	/* locate the payload buffer */
	payload_buf = payload;
	payload_size = sizeof(payload);

	/* slurp the input files */
	ramdisk_buf = slurp_file(ramdisk, &ramdisk_size);

	/* parse the kernel */
	parse_kernel(&kinfo, kernel_buf, kernel_size);

	/* Find the parameters */
	params = (void *)(payload_buf + (payload_size - sizeof(*params)));

	/* A sanity check against bad versions of binutils */
	if (params->convert_magic != CONVERT_MAGIC) {
		die("Internal error convert_magic %16llx != %16llx\n",
			(unsigned long long)(params->convert_magic), CONVERT_MAGIC);
	}

	/* Copy the command line */
	strncpy(params->cmdline, cmdline, sizeof(params->cmdline));
	params->cmdline[sizeof(params->cmdline)-1]= '\0';

	/* Add a program header for the note section */
	phdr = add_program_headers(ehdr, 2 + kinfo.phdrs + (ramdisk_size?1:0));

	/* Fill in the program headers*/
	phdr[0].p_type = PT_NOTE;
	
	/* Fill in the kernel program headers */
	index = 1 + populate_kernel_phdrs(&kinfo, phdr + 1); 
	
	/* Fill in the converter program header */
	phdr[index].p_paddr  = roundup(phdr[index -1].p_paddr + phdr[index -1].p_memsz, 16);
	phdr[index].p_vaddr  = phdr[index].p_paddr;
	phdr[index].p_filesz = payload_size;
	phdr[index].p_memsz  = payload_size;
	phdr[index].p_data   = payload_buf;
	index++;

	/* Set the start location */
	params->entry = kinfo.entry;
	ehdr->e_entry = phdr[index -1].p_paddr;


	/* Fill in the ramdisk program header */
	params->initrd_start = params->initrd_size = 0;
	if (ramdisk_size) {
		phdr[index].p_paddr  = roundup(phdr[index -1].p_paddr + phdr[index -1].p_memsz, 16);
		phdr[index].p_vaddr  = phdr[index].p_paddr;
		phdr[index].p_filesz = ramdisk_size;
		phdr[index].p_memsz  = ramdisk_size;
		phdr[index].p_data   = ramdisk_buf;
		params->initrd_start = phdr[index].p_paddr;
		params->initrd_size  = phdr[index].p_filesz;
		index++;
	}

	/* Compute the elf notes */
	note = add_notes(ehdr, 3);
	note[0].n_type = EIN_PROGRAM_NAME;
	note[0].n_name = "ELFBoot";
	note[0].n_desc = "Linux";
	note[0].n_descsz = strlen(note[0].n_desc)+1;

	note[1].n_type = EIN_PROGRAM_VERSION;
	note[1].n_name = "ELFBoot";
	note[1].n_desc = kinfo.version;
	note[1].n_descsz = strlen(note[1].n_desc)+1;

	note[2].n_type = EIN_PROGRAM_CHECKSUM;
	note[2].n_name = "ELFBoot";
	note[2].n_desc = 0;
	note[2].n_descsz = 2;

	return 0;
}