summaryrefslogtreecommitdiff
path: root/util/mkelfImage/linux-i386/mkelf-linux-i386.c
blob: 47256fd2f44ef1736baa9d4bb960a98e6fd846ed (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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#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 "x86-linux.h"
#include "mkelfImage.h"

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

struct kernel_info;
static void (*parse_kernel_type)(struct kernel_info *info, char *kernel_buf, size_t kernel_size);
static void parse_bzImage_kernel(struct kernel_info *info, char *kernel_buf, size_t kernel_size);
static void parse_elf32_kernel(struct kernel_info *info, char *kernel_buf, size_t kernel_size);
static void parse_elf64_kernel(struct kernel_info *info, char *kernel_buf, size_t kernel_size);

char *vmlinux_x86_64_probe(char *kernel_buf, off_t kernel_size);

char *vmlinux_i386_probe(char *kernel_buf, off_t kernel_size)
{
	Elf32_Ehdr *ehdr;
	Elf32_Phdr *phdr;
	int i;
	int phdrs;
	ehdr = (Elf32_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] != ELFCLASS32) {
		return vmlinux_x86_64_probe(kernel_buf, kernel_size);
//		return "Not a 32bit 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_386) {
		return "Not an i386 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 = (Elf32_Phdr *)(kernel_buf + le32_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";
	}
	parse_kernel_type = parse_elf32_kernel;
	return 0;
}
char *vmlinux_x86_64_probe(char *kernel_buf, off_t kernel_size)
{
        Elf64_Ehdr *ehdr;
        Elf64_Phdr *phdr;
        int i;
	int phdrs = 0;
        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_X86_64) {
                return "Not an x86_64 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";
        }
        parse_kernel_type = parse_elf64_kernel;
        return 0;
}

char *bzImage_i386_probe(char *kernel_buf, off_t kernel_size)
{
	struct x86_linux_header *hdr;
	unsigned long offset;
	int setup_sects;
	hdr = (struct x86_linux_header *)kernel_buf;

	if (le16_to_cpu(hdr->boot_sector_magic) != 0xaa55) {
		return "No bootsector magic";
	}
	if (memcmp(hdr->header_magic, "HdrS", 4) != 0) {
		return "Not a linux kernel";
	}

	if (le16_to_cpu(hdr->protocol_version) < 0x202) {
		return "Kernel protcols version before 2.02 not supported";
	}

	setup_sects = hdr->setup_sects;
	if (setup_sects == 0) {
		setup_sects = 4;
	}
	offset = 512 + (512 *setup_sects);
	if (offset > kernel_size) {
		return "Not enough bytes";
	}
	parse_kernel_type = parse_bzImage_kernel;
	return 0;
}

char *linux_i386_probe(char *kernel_buf, off_t kernel_size)
{
	char *result;
	result = "";
	if (result) result = bzImage_i386_probe(kernel_buf, kernel_size);
	if (result) result = vmlinux_i386_probe(kernel_buf, kernel_size);
	if (result) result = bzImage_i386_probe(kernel_buf, kernel_size);
	return result;
}

#define NR_SECTIONS 16

struct kernel_info
{
	int phdrs;
	void *kernel[NR_SECTIONS];
	size_t filesz[NR_SECTIONS];
	size_t memsz[NR_SECTIONS];
	size_t paddr[NR_SECTIONS];
	size_t vaddr[NR_SECTIONS];
	size_t entry;
	size_t switch_64;
	char *version;
};

static void parse_elf32_kernel(struct kernel_info *info, char *kernel_buf, size_t kernel_size)
{
	Elf32_Ehdr *ehdr;
	Elf32_Phdr *phdr;
	int i;
	int phdrs;
	ehdr = (Elf32_Ehdr *)kernel_buf;
	phdr = (Elf32_Phdr *)(kernel_buf + 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;
		if(phdrs == NR_SECTIONS)
			die("NR_SECTIONS is too small\n");
		info->kernel[phdrs]  = kernel_buf + le32_to_cpu(phdr[i].p_offset);
		info->filesz[phdrs]  = le32_to_cpu(phdr[i].p_filesz);
		info->memsz[phdrs]   = le32_to_cpu(phdr[i].p_memsz);
		info->paddr[phdrs]   = le32_to_cpu(phdr[i].p_paddr) & 0xfffffff;
		info->vaddr[phdrs]   = le32_to_cpu(phdr[i].p_vaddr);
		phdrs++;
	}

	if(!phdrs)
		die("We need at least one phdr\n");

	info->phdrs = phdrs;
        info->entry   = le32_to_cpu(ehdr->e_entry);
	info->switch_64   = 0; //not convert from elf64
	info->version = "unknown";
}

static void parse_elf64_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;
		if(phdrs == NR_SECTIONS)
			die("NR_SECTIONS is too small\n");
		info->kernel[phdrs]  = kernel_buf + le64_to_cpu(phdr[i].p_offset);
		info->filesz[phdrs]  = le64_to_cpu(phdr[i].p_filesz);
		info->memsz[phdrs]   = le64_to_cpu(phdr[i].p_memsz);
		info->paddr[phdrs]   = le64_to_cpu(phdr[i].p_paddr) & 0xfffffff;
		info->vaddr[phdrs]   = le64_to_cpu(phdr[i].p_vaddr);
		phdrs++;
	}

	if(!phdrs)
		die("We need at least one phdr\n");

	info->phdrs = phdrs;
        info->entry   = le64_to_cpu(ehdr->e_entry);
#if  0
	if (info->entry != info->paddr[0]) {
		info->entry = info->paddr[0]; // we still have startup_32 there
		info->switch_64   = 0; //not convert from elf64
	} else
#endif
		info->switch_64   = 1; //convert from elf64

        info->version = "unknown";
}


static void parse_bzImage_kernel(struct kernel_info *info, char *kernel_buf, size_t kernel_size)
{
	struct x86_linux_header *hdr;
	unsigned long offset;
	int setup_sects;
	hdr = (struct x86_linux_header *)kernel_buf;
	setup_sects = hdr->setup_sects;
	if (setup_sects == 0) {
		setup_sects = 4;
	}
	offset = 512 + (512 *setup_sects);

	info->kernel[0]  = kernel_buf + offset;
	info->filesz[0]  = kernel_size - offset;
	info->memsz[0]   = 0x700000;
	info->paddr[0]   = 0x100000;
	info->vaddr[0]   = 0x100000;
	info->phdrs = 1;
	info->entry   = info->paddr[0];
	info->switch_64   = 0; //not convert from elf64, even later bzImage become elf64, it still includes startup_32
	info->version = kernel_buf + 512 + le16_to_cpu(hdr->kver_addr);
}

static void parse_kernel(struct kernel_info *info, char *kernel_buf, size_t kernel_size)
{
	memset(info, 0, sizeof(*info));
	if (parse_kernel_type) {
		parse_kernel_type(info, kernel_buf, kernel_size);
	}
	else {
		die("Unknown kernel format");
	}
}

void linux_i386_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"
		"      --ramdisk-base=<addr>   Set the initrd load address to <addr>\n"
		);
	return;
}


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

#define DEFAULT_RAMDISK_BASE (8*1024*1024)

int linux_i386_mkelf(int argc, char **argv,
	struct memelfheader *ehdr, char *kernel_buf, off_t kernel_size)
{
	const char *ramdisk, *cmdline;
	unsigned long ramdisk_base;
	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 i;

	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 },
		{ "ramdisk-base",    1, 0, OPT_RAMDISK_BASE },
		{ 0 , 0, 0, 0 },
	};
	static const char short_options[] = MKELF_OPT_STR;

	ramdisk_base = DEFAULT_RAMDISK_BASE;
	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_BASE:
		{
			char *end;
			unsigned long base;
			base = strtoul(optarg, &end, 0);
			if ((end == optarg) || (*end != '\0')) {
				error("Invalid ramdisk base\n");
			}
			ramdisk_base = base;
		}
		case OPT_RAMDISK:
			ramdisk = optarg;
			break;
		case OPT_CMDLINE:
			cmdline = optarg;
			break;
		default:
			break;
		}
	}
	ehdr->ei_class  = ELFCLASS32;
	ehdr->ei_data   = ELFDATA2LSB;
	ehdr->e_type    = ET_EXEC;
	ehdr->e_machine = EM_386;

	/* 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 %08x != %08x\n",
			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 */
	index = 4;
	index += (kinfo.phdrs - 1);
	index += ramdisk_size ? 1:0;
	phdr = add_program_headers(ehdr, index);

	/* Fill in the program headers*/
	phdr[0].p_type = PT_NOTE;

	/* Fill in the converter program headers */
	phdr[1].p_paddr  = CONVERTLOC;
	phdr[1].p_vaddr  = CONVERTLOC;
	phdr[1].p_filesz = payload_size;
	phdr[1].p_memsz  = payload_size + params->bss_size;
	phdr[1].p_data   = payload;

	/* Reserve space for the REAL MODE DATA segment AND the GDT segment */
	phdr[2].p_paddr  = REAL_MODE_DATA_LOC;
	phdr[2].p_vaddr  = REAL_MODE_DATA_LOC;
	phdr[2].p_filesz = 0;
	if(!kinfo.switch_64)
		phdr[2].p_memsz = (GDTLOC - REAL_MODE_DATA_LOC) + params->gdt_size;
	else
		phdr[2].p_memsz = (PGTLOC - REAL_MODE_DATA_LOC) + params->pgt_size;
	phdr[2].p_data   = 0;

	if( (phdr[1].p_paddr + phdr[1].p_memsz) > phdr[2].p_paddr) {
		die("Internal error: need to increase REAL_MODE_DATA_LOC !\n");
	}

	index = 3;
	/* Put the second kernel frament if present */
	for(i=0;i<kinfo.phdrs;i++) {
		phdr[index].p_paddr  = kinfo.paddr[i];
		phdr[index].p_vaddr  = kinfo.vaddr[i];
		phdr[index].p_filesz = kinfo.filesz[i];
		phdr[index].p_memsz  = kinfo.memsz[i];
		phdr[index].p_data   = kinfo.kernel[i];
		index++;
	}

	/* Put the ramdisk at ramdisk base.
	 */
	params->initrd_start = params->initrd_size = 0;
	if (ramdisk_size) {
		if( (phdr[index-1].p_paddr + phdr[index-1].p_memsz) > ramdisk_base) {
			die("need to increase increase ramdisk_base !\n");
		}

		phdr[index].p_paddr  = ramdisk_base;
		phdr[index].p_vaddr  = ramdisk_base;
		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++;
	}

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

	/* Setup 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);

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

	return 0;
}