summaryrefslogtreecommitdiff
path: root/kexec/arch/loongarch/kexec-elf-loongarch.c
blob: 2bf128f1e362f95b81bd0bdabc3b71cbc6b37757 (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
/*
 * kexec-elf-loongarch.c - kexec Elf loader for loongarch
 *
 * Copyright (C) 2022 Loongson Technology Corporation Limited.
 *   Youling Tang <tangyouling@loongson.cn>
 *
 * This source code is licensed under the GNU General Public License,
 * Version 2.  See the file COPYING for more details.
*/

#define _GNU_SOURCE

#include <limits.h>
#include <errno.h>
#include <elf.h>

#include "kexec.h"
#include "kexec-elf.h"
#include "kexec-syscall.h"
#include "crashdump-loongarch.h"
#include "kexec-loongarch.h"
#include "arch/options.h"

off_t initrd_base, initrd_size;

int elf_loongarch_probe(const char *kernel_buf, off_t kernel_size)
{
	struct mem_ehdr ehdr;
	int result;

	result = build_elf_exec_info(kernel_buf, kernel_size, &ehdr, 0);
	if (result < 0) {
		dbgprintf("%s: Not an ELF executable.\n", __func__);
		goto out;
	}

	/* Verify the architecuture specific bits. */
	if (ehdr.e_machine != EM_LOONGARCH) {
		dbgprintf("%s: Not an LoongArch ELF executable.\n", __func__);
		result = -1;
		goto out;
	}

	result = 0;
out:
	free_elf_info(&ehdr);
	return result;
}

int elf_loongarch_load(int argc, char **argv, const char *kernel_buf,
	off_t kernel_size, struct kexec_info *info)
{
	const struct loongarch_image_header *header = NULL;
	unsigned long kernel_segment;
	struct mem_ehdr ehdr;
	int result;

	result = build_elf_exec_info(kernel_buf, kernel_size, &ehdr, 0);

	if (result < 0) {
		dbgprintf("%s: build_elf_exec_info failed\n", __func__);
		goto exit;
	}

	kernel_segment = loongarch_locate_kernel_segment(info);

	if (kernel_segment == ULONG_MAX) {
		dbgprintf("%s: Kernel segment is not allocated\n", __func__);
		result = EFAILED;
		goto exit;
	}

	dbgprintf("%s: kernel_segment: %016lx\n", __func__, kernel_segment);
	dbgprintf("%s: image_size:     %016lx\n", __func__,
		kernel_size);
	dbgprintf("%s: text_offset:    %016lx\n", __func__,
		loongarch_mem.text_offset);
	dbgprintf("%s: phys_offset:    %016lx\n", __func__,
		loongarch_mem.phys_offset);
	dbgprintf("%s: PE format:      %s\n", __func__,
		(loongarch_header_check_pe_sig(header) ? "yes" : "no"));

	/* create and initialize elf core header segment */
	if (info->kexec_flags & KEXEC_ON_CRASH) {
		result = load_crashdump_segments(info);
		if (result) {
			dbgprintf("%s: Creating eflcorehdr failed.\n",
								__func__);
			goto exit;
		}
	}

	info->entry = (void *)virt_to_phys(ehdr.e_entry);

	result = elf_exec_load(&ehdr, info);

	if (result) {
		dbgprintf("%s: elf_exec_load failed\n", __func__);
		goto exit;
	}

	/* load additional data */
	result = loongarch_load_other_segments(info, kernel_segment + kernel_size);

exit:
	free_elf_info(&ehdr);
	if (result)
		fprintf(stderr, "kexec: Bad elf image file, load failed.\n");
	return result;
}

void elf_loongarch_usage(void)
{
	printf(
"     An LoongArch ELF image, little endian.\n"
"     Typically vmlinux or a stripped version of vmlinux.\n\n");
}