summaryrefslogtreecommitdiff
path: root/arch/um/kernel/load_file.c
blob: 5cecd0e291fb5c4c6c4e27743052ae8dbd4a5409 (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
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
 */
#include <linux/memblock.h>
#include <os.h>

#include "um_arch.h"

static int __init __uml_load_file(const char *filename, void *buf, int size)
{
	int fd, n;

	fd = os_open_file(filename, of_read(OPENFLAGS()), 0);
	if (fd < 0) {
		printk(KERN_ERR "Opening '%s' failed - err = %d\n", filename,
		       -fd);
		return -1;
	}
	n = os_read_file(fd, buf, size);
	if (n != size) {
		printk(KERN_ERR "Read of %d bytes from '%s' failed, "
		       "err = %d\n", size,
		       filename, -n);
		return -1;
	}

	os_close_file(fd);
	return 0;
}

void *uml_load_file(const char *filename, unsigned long long *size)
{
	void *area;
	int err;

	*size = 0;

	if (!filename)
		return NULL;

	err = os_file_size(filename, size);
	if (err)
		return NULL;

	if (*size == 0) {
		printk(KERN_ERR "\"%s\" is empty\n", filename);
		return NULL;
	}

	area = memblock_alloc(*size, SMP_CACHE_BYTES);
	if (!area)
		panic("%s: Failed to allocate %llu bytes\n", __func__, *size);

	if (__uml_load_file(filename, area, *size)) {
		memblock_free(area, *size);
		return NULL;
	}

	return area;
}