summaryrefslogtreecommitdiff
path: root/usr/src/lib/libgrubmgmt/common/libgrub_bargs.c
blob: b6c4e5f9a1cd4ddb8bc54fbd3a0f69d22234908f (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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * This file contains functions for constructing boot arguments
 * from GRUB menu for Fast Reboot.
 */
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <strings.h>
#include <unistd.h>
#include <fcntl.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/elf.h>

#include "libgrub_impl.h"

#if defined(__sparc)
#define	CUR_ELFDATA	ELFDATA2MSB
#elif defined(__i386)
#define	CUR_ELFDATA	ELFDATA2LSB
#endif /* __i386 */

/*
 * Open the kernel file.
 * Return zero on sucess or error code otherwise.
 * On success the kernel file descriptor is returned in fdp.
 */
static int
get_kernel_fd(const char *path, int *fdp)
{
	const char	*bname;
	int		fd = -1, class, format;
	char		ident[EI_NIDENT];

	/* kernel basename must be unix */
	if ((bname = strrchr(path, '/')) == NULL)
		bname = path;
	else
		bname++;

	if (strcmp(bname, "unix") != 0) {
		if (strcmp(bname, "xen.gz") == 0)
			return (EG_XVMNOTSUP);
		return (EG_NOTUNIX);
	}

	if ((fd = open64(path, O_RDONLY)) >= 0 &&
	    (pread64(fd, ident, sizeof (ident), 0) == sizeof (ident))) {

		class = ident[EI_CLASS];
		format = ident[EI_DATA];

		if ((class == ELFCLASS32 || class == ELFCLASS64) &&
		    (memcmp(&ident[EI_MAG0], ELFMAG, 4) == 0) &&
		    format == CUR_ELFDATA) {
			*fdp = fd;
			return (0);
		}
	}

	if (fd >= 0)
		(void) close(fd);
	return (EG_OPENKERNFILE);
}

/*
 * Construct boot arguments for Fast Reboot from the ge_barg field of
 * a GRUB menu entry.
 * Return 0 on success, errno on failure.
 */
static int
barg2bootargs(const grub_barg_t *barg, grub_boot_args_t *fbarg)
{
	int	rc = 0;
	char	path[BOOTARGS_MAX];
	char	rpath[BOOTARGS_MAX];
	const grub_fsdesc_t	*fsd;

	assert(fbarg);
	bzero(fbarg, sizeof (*fbarg));
	fbarg->gba_kernel_fd = -1;

	if (!IS_BARG_VALID(barg))
		return (EINVAL);
	if ((fsd = grub_get_rootfsd(&barg->gb_root)) == NULL)
		return (EG_UNKNOWNFS);

	bcopy(fsd, &fbarg->gba_fsd, sizeof (fbarg->gba_fsd));
	bcopy(barg->gb_kernel, fbarg->gba_kernel, sizeof (fbarg->gba_kernel));
	bcopy(barg->gb_module, fbarg->gba_module, sizeof (fbarg->gba_module));

	if (fbarg->gba_fsd.gfs_mountp[0] == 0 &&
	    (rc = grub_fsd_mount_tmp(&fbarg->gba_fsd,
	    barg->gb_root.gr_fstyp)) != 0)
		return (rc);

	if (snprintf(path, sizeof (path), "%s%s", fbarg->gba_fsd.gfs_mountp,
	    fbarg->gba_kernel) >= sizeof (path)) {
		rc = E2BIG;
		goto err_out;
	}
	(void) strtok(path, " \t");
	(void) clean_path(path);

	/*
	 * GRUB requires absolute path, no symlinks, so do we
	 */
	if ((rc = resolvepath(path, rpath, sizeof (rpath))) == -1)
		rc = errno;
	else {
		rpath[rc] = 0;
		if (strcmp(rpath, path) != 0)
			rc = EG_NOTABSPATH;
		else
			rc = get_kernel_fd(rpath, &fbarg->gba_kernel_fd);
	}

	/* construct bootargs command-line */
	if (rc == 0 && snprintf(fbarg->gba_bootargs,
	    sizeof (fbarg->gba_bootargs), "%s %s", fbarg->gba_fsd.gfs_mountp,
	    fbarg->gba_kernel) >= sizeof (fbarg->gba_bootargs))
		rc = E2BIG;

err_out:
	if (rc != 0)
		grub_cleanup_boot_args(fbarg);

	return (rc);
}

/*
 * Construct boot arguments for Fast Reboot from grub_menu_t.
 * Return 0 on success, errno on failure.
 */
static int
grub_entry_get_boot_args(grub_entry_t *ent, grub_boot_args_t *fbarg)
{
	int rc = EG_INVALIDENT;

	if (IS_ENTRY_VALID(ent) && (rc = grub_entry_construct_barg(ent)) == 0)
		return (barg2bootargs(&ent->ge_barg, fbarg));
	else
		return (rc);
}

/*
 * Construct boot arguments for Fast Reboot from grub_menu_t and the
 * entry number.
 * Return 0 on success, errno on failure.
 */
static int
grub_menu_get_boot_args(const grub_menu_t *mp, int num,
    grub_boot_args_t *fbarg)
{
	grub_entry_t *ent;

	assert(mp);
	assert(fbarg);

	if ((ent = grub_menu_get_entry(mp, num)) == NULL)
		return (EG_NOENTRY);

	return (grub_entry_get_boot_args(ent, fbarg));
}

/*
 * Construct boot arguments from the specified GRUB menu entry.
 * Caller must allocate space for fbarg, and call grub_cleanup_boot_args()
 * when it's done with fbarg to clean up.
 *
 * Return 0 on success, errno on failure.
 */
int
grub_get_boot_args(grub_boot_args_t *fbarg, const char *menupath, int num)
{
	int rc;
	grub_menu_t *mp;

	assert(fbarg);
	if ((rc = grub_menu_init(menupath, &mp)) == 0) {
		rc = grub_menu_get_boot_args(mp, num, fbarg);
		grub_menu_fini(mp);
	}
	return (rc);
}

/*
 * Clean up when done with fbarg: close file handle, unmount file
 * systems.  Must be safe to call even if not all the fields are
 * set up.
 */
void
grub_cleanup_boot_args(grub_boot_args_t *fbarg)
{
	if (fbarg == NULL)
		return;

	(void) close(fbarg->gba_kernel_fd);
	grub_fsd_umount_tmp(&fbarg->gba_fsd);
}