xref: /onnv-gate/usr/src/lib/libgrubmgmt/common/libgrub_bargs.c (revision 9160:1517e6edbc6f)
1*9160SSherry.Moore@Sun.COM /*
2*9160SSherry.Moore@Sun.COM  * CDDL HEADER START
3*9160SSherry.Moore@Sun.COM  *
4*9160SSherry.Moore@Sun.COM  * The contents of this file are subject to the terms of the
5*9160SSherry.Moore@Sun.COM  * Common Development and Distribution License (the "License").
6*9160SSherry.Moore@Sun.COM  * You may not use this file except in compliance with the License.
7*9160SSherry.Moore@Sun.COM  *
8*9160SSherry.Moore@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*9160SSherry.Moore@Sun.COM  * or http://www.opensolaris.org/os/licensing.
10*9160SSherry.Moore@Sun.COM  * See the License for the specific language governing permissions
11*9160SSherry.Moore@Sun.COM  * and limitations under the License.
12*9160SSherry.Moore@Sun.COM  *
13*9160SSherry.Moore@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
14*9160SSherry.Moore@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*9160SSherry.Moore@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
16*9160SSherry.Moore@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
17*9160SSherry.Moore@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
18*9160SSherry.Moore@Sun.COM  *
19*9160SSherry.Moore@Sun.COM  * CDDL HEADER END
20*9160SSherry.Moore@Sun.COM  */
21*9160SSherry.Moore@Sun.COM /*
22*9160SSherry.Moore@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23*9160SSherry.Moore@Sun.COM  * Use is subject to license terms.
24*9160SSherry.Moore@Sun.COM  */
25*9160SSherry.Moore@Sun.COM 
26*9160SSherry.Moore@Sun.COM /*
27*9160SSherry.Moore@Sun.COM  * This file contains functions for constructing boot arguments
28*9160SSherry.Moore@Sun.COM  * from GRUB menu for Fast Reboot.
29*9160SSherry.Moore@Sun.COM  */
30*9160SSherry.Moore@Sun.COM #include <stdio.h>
31*9160SSherry.Moore@Sun.COM #include <stdlib.h>
32*9160SSherry.Moore@Sun.COM #include <errno.h>
33*9160SSherry.Moore@Sun.COM #include <strings.h>
34*9160SSherry.Moore@Sun.COM #include <unistd.h>
35*9160SSherry.Moore@Sun.COM #include <fcntl.h>
36*9160SSherry.Moore@Sun.COM #include <assert.h>
37*9160SSherry.Moore@Sun.COM #include <sys/types.h>
38*9160SSherry.Moore@Sun.COM #include <sys/elf.h>
39*9160SSherry.Moore@Sun.COM 
40*9160SSherry.Moore@Sun.COM #include "libgrub_impl.h"
41*9160SSherry.Moore@Sun.COM 
42*9160SSherry.Moore@Sun.COM #if defined(__sparc)
43*9160SSherry.Moore@Sun.COM #define	CUR_ELFDATA	ELFDATA2MSB
44*9160SSherry.Moore@Sun.COM #elif defined(__i386)
45*9160SSherry.Moore@Sun.COM #define	CUR_ELFDATA	ELFDATA2LSB
46*9160SSherry.Moore@Sun.COM #endif /* __i386 */
47*9160SSherry.Moore@Sun.COM 
48*9160SSherry.Moore@Sun.COM /*
49*9160SSherry.Moore@Sun.COM  * Open the kernel file.
50*9160SSherry.Moore@Sun.COM  * Return zero on sucess or error code otherwise.
51*9160SSherry.Moore@Sun.COM  * On success the kernel file descriptor is returned in fdp.
52*9160SSherry.Moore@Sun.COM  */
53*9160SSherry.Moore@Sun.COM static int
get_kernel_fd(const char * path,int * fdp)54*9160SSherry.Moore@Sun.COM get_kernel_fd(const char *path, int *fdp)
55*9160SSherry.Moore@Sun.COM {
56*9160SSherry.Moore@Sun.COM 	const char	*bname;
57*9160SSherry.Moore@Sun.COM 	int		fd = -1, class, format;
58*9160SSherry.Moore@Sun.COM 	char		ident[EI_NIDENT];
59*9160SSherry.Moore@Sun.COM 
60*9160SSherry.Moore@Sun.COM 	/* kernel basename must be unix */
61*9160SSherry.Moore@Sun.COM 	if ((bname = strrchr(path, '/')) == NULL)
62*9160SSherry.Moore@Sun.COM 		bname = path;
63*9160SSherry.Moore@Sun.COM 	else
64*9160SSherry.Moore@Sun.COM 		bname++;
65*9160SSherry.Moore@Sun.COM 
66*9160SSherry.Moore@Sun.COM 	if (strcmp(bname, "unix") != 0) {
67*9160SSherry.Moore@Sun.COM 		if (strcmp(bname, "xen.gz") == 0)
68*9160SSherry.Moore@Sun.COM 			return (EG_XVMNOTSUP);
69*9160SSherry.Moore@Sun.COM 		return (EG_NOTUNIX);
70*9160SSherry.Moore@Sun.COM 	}
71*9160SSherry.Moore@Sun.COM 
72*9160SSherry.Moore@Sun.COM 	if ((fd = open64(path, O_RDONLY)) >= 0 &&
73*9160SSherry.Moore@Sun.COM 	    (pread64(fd, ident, sizeof (ident), 0) == sizeof (ident))) {
74*9160SSherry.Moore@Sun.COM 
75*9160SSherry.Moore@Sun.COM 		class = ident[EI_CLASS];
76*9160SSherry.Moore@Sun.COM 		format = ident[EI_DATA];
77*9160SSherry.Moore@Sun.COM 
78*9160SSherry.Moore@Sun.COM 		if ((class == ELFCLASS32 || class == ELFCLASS64) &&
79*9160SSherry.Moore@Sun.COM 		    (memcmp(&ident[EI_MAG0], ELFMAG, 4) == 0) &&
80*9160SSherry.Moore@Sun.COM 		    format == CUR_ELFDATA) {
81*9160SSherry.Moore@Sun.COM 			*fdp = fd;
82*9160SSherry.Moore@Sun.COM 			return (0);
83*9160SSherry.Moore@Sun.COM 		}
84*9160SSherry.Moore@Sun.COM 	}
85*9160SSherry.Moore@Sun.COM 
86*9160SSherry.Moore@Sun.COM 	if (fd >= 0)
87*9160SSherry.Moore@Sun.COM 		(void) close(fd);
88*9160SSherry.Moore@Sun.COM 	return (EG_OPENKERNFILE);
89*9160SSherry.Moore@Sun.COM }
90*9160SSherry.Moore@Sun.COM 
91*9160SSherry.Moore@Sun.COM /*
92*9160SSherry.Moore@Sun.COM  * Construct boot arguments for Fast Reboot from the ge_barg field of
93*9160SSherry.Moore@Sun.COM  * a GRUB menu entry.
94*9160SSherry.Moore@Sun.COM  * Return 0 on success, errno on failure.
95*9160SSherry.Moore@Sun.COM  */
96*9160SSherry.Moore@Sun.COM static int
barg2bootargs(const grub_barg_t * barg,grub_boot_args_t * fbarg)97*9160SSherry.Moore@Sun.COM barg2bootargs(const grub_barg_t *barg, grub_boot_args_t *fbarg)
98*9160SSherry.Moore@Sun.COM {
99*9160SSherry.Moore@Sun.COM 	int	rc = 0;
100*9160SSherry.Moore@Sun.COM 	char	path[BOOTARGS_MAX];
101*9160SSherry.Moore@Sun.COM 	char	rpath[BOOTARGS_MAX];
102*9160SSherry.Moore@Sun.COM 	const grub_fsdesc_t	*fsd;
103*9160SSherry.Moore@Sun.COM 
104*9160SSherry.Moore@Sun.COM 	assert(fbarg);
105*9160SSherry.Moore@Sun.COM 	bzero(fbarg, sizeof (*fbarg));
106*9160SSherry.Moore@Sun.COM 	fbarg->gba_kernel_fd = -1;
107*9160SSherry.Moore@Sun.COM 
108*9160SSherry.Moore@Sun.COM 	if (!IS_BARG_VALID(barg))
109*9160SSherry.Moore@Sun.COM 		return (EINVAL);
110*9160SSherry.Moore@Sun.COM 	if ((fsd = grub_get_rootfsd(&barg->gb_root)) == NULL)
111*9160SSherry.Moore@Sun.COM 		return (EG_UNKNOWNFS);
112*9160SSherry.Moore@Sun.COM 
113*9160SSherry.Moore@Sun.COM 	bcopy(fsd, &fbarg->gba_fsd, sizeof (fbarg->gba_fsd));
114*9160SSherry.Moore@Sun.COM 	bcopy(barg->gb_kernel, fbarg->gba_kernel, sizeof (fbarg->gba_kernel));
115*9160SSherry.Moore@Sun.COM 	bcopy(barg->gb_module, fbarg->gba_module, sizeof (fbarg->gba_module));
116*9160SSherry.Moore@Sun.COM 
117*9160SSherry.Moore@Sun.COM 	if (fbarg->gba_fsd.gfs_mountp[0] == 0 &&
118*9160SSherry.Moore@Sun.COM 	    (rc = grub_fsd_mount_tmp(&fbarg->gba_fsd,
119*9160SSherry.Moore@Sun.COM 	    barg->gb_root.gr_fstyp)) != 0)
120*9160SSherry.Moore@Sun.COM 		return (rc);
121*9160SSherry.Moore@Sun.COM 
122*9160SSherry.Moore@Sun.COM 	if (snprintf(path, sizeof (path), "%s%s", fbarg->gba_fsd.gfs_mountp,
123*9160SSherry.Moore@Sun.COM 	    fbarg->gba_kernel) >= sizeof (path)) {
124*9160SSherry.Moore@Sun.COM 		rc = E2BIG;
125*9160SSherry.Moore@Sun.COM 		goto err_out;
126*9160SSherry.Moore@Sun.COM 	}
127*9160SSherry.Moore@Sun.COM 	(void) strtok(path, " \t");
128*9160SSherry.Moore@Sun.COM 	(void) clean_path(path);
129*9160SSherry.Moore@Sun.COM 
130*9160SSherry.Moore@Sun.COM 	/*
131*9160SSherry.Moore@Sun.COM 	 * GRUB requires absolute path, no symlinks, so do we
132*9160SSherry.Moore@Sun.COM 	 */
133*9160SSherry.Moore@Sun.COM 	if ((rc = resolvepath(path, rpath, sizeof (rpath))) == -1)
134*9160SSherry.Moore@Sun.COM 		rc = errno;
135*9160SSherry.Moore@Sun.COM 	else {
136*9160SSherry.Moore@Sun.COM 		rpath[rc] = 0;
137*9160SSherry.Moore@Sun.COM 		if (strcmp(rpath, path) != 0)
138*9160SSherry.Moore@Sun.COM 			rc = EG_NOTABSPATH;
139*9160SSherry.Moore@Sun.COM 		else
140*9160SSherry.Moore@Sun.COM 			rc = get_kernel_fd(rpath, &fbarg->gba_kernel_fd);
141*9160SSherry.Moore@Sun.COM 	}
142*9160SSherry.Moore@Sun.COM 
143*9160SSherry.Moore@Sun.COM 	/* construct bootargs command-line */
144*9160SSherry.Moore@Sun.COM 	if (rc == 0 && snprintf(fbarg->gba_bootargs,
145*9160SSherry.Moore@Sun.COM 	    sizeof (fbarg->gba_bootargs), "%s %s", fbarg->gba_fsd.gfs_mountp,
146*9160SSherry.Moore@Sun.COM 	    fbarg->gba_kernel) >= sizeof (fbarg->gba_bootargs))
147*9160SSherry.Moore@Sun.COM 		rc = E2BIG;
148*9160SSherry.Moore@Sun.COM 
149*9160SSherry.Moore@Sun.COM err_out:
150*9160SSherry.Moore@Sun.COM 	if (rc != 0)
151*9160SSherry.Moore@Sun.COM 		grub_cleanup_boot_args(fbarg);
152*9160SSherry.Moore@Sun.COM 
153*9160SSherry.Moore@Sun.COM 	return (rc);
154*9160SSherry.Moore@Sun.COM }
155*9160SSherry.Moore@Sun.COM 
156*9160SSherry.Moore@Sun.COM /*
157*9160SSherry.Moore@Sun.COM  * Construct boot arguments for Fast Reboot from grub_menu_t.
158*9160SSherry.Moore@Sun.COM  * Return 0 on success, errno on failure.
159*9160SSherry.Moore@Sun.COM  */
160*9160SSherry.Moore@Sun.COM static int
grub_entry_get_boot_args(grub_entry_t * ent,grub_boot_args_t * fbarg)161*9160SSherry.Moore@Sun.COM grub_entry_get_boot_args(grub_entry_t *ent, grub_boot_args_t *fbarg)
162*9160SSherry.Moore@Sun.COM {
163*9160SSherry.Moore@Sun.COM 	int rc = EG_INVALIDENT;
164*9160SSherry.Moore@Sun.COM 
165*9160SSherry.Moore@Sun.COM 	if (IS_ENTRY_VALID(ent) && (rc = grub_entry_construct_barg(ent)) == 0)
166*9160SSherry.Moore@Sun.COM 		return (barg2bootargs(&ent->ge_barg, fbarg));
167*9160SSherry.Moore@Sun.COM 	else
168*9160SSherry.Moore@Sun.COM 		return (rc);
169*9160SSherry.Moore@Sun.COM }
170*9160SSherry.Moore@Sun.COM 
171*9160SSherry.Moore@Sun.COM /*
172*9160SSherry.Moore@Sun.COM  * Construct boot arguments for Fast Reboot from grub_menu_t and the
173*9160SSherry.Moore@Sun.COM  * entry number.
174*9160SSherry.Moore@Sun.COM  * Return 0 on success, errno on failure.
175*9160SSherry.Moore@Sun.COM  */
176*9160SSherry.Moore@Sun.COM static int
grub_menu_get_boot_args(const grub_menu_t * mp,int num,grub_boot_args_t * fbarg)177*9160SSherry.Moore@Sun.COM grub_menu_get_boot_args(const grub_menu_t *mp, int num,
178*9160SSherry.Moore@Sun.COM     grub_boot_args_t *fbarg)
179*9160SSherry.Moore@Sun.COM {
180*9160SSherry.Moore@Sun.COM 	grub_entry_t *ent;
181*9160SSherry.Moore@Sun.COM 
182*9160SSherry.Moore@Sun.COM 	assert(mp);
183*9160SSherry.Moore@Sun.COM 	assert(fbarg);
184*9160SSherry.Moore@Sun.COM 
185*9160SSherry.Moore@Sun.COM 	if ((ent = grub_menu_get_entry(mp, num)) == NULL)
186*9160SSherry.Moore@Sun.COM 		return (EG_NOENTRY);
187*9160SSherry.Moore@Sun.COM 
188*9160SSherry.Moore@Sun.COM 	return (grub_entry_get_boot_args(ent, fbarg));
189*9160SSherry.Moore@Sun.COM }
190*9160SSherry.Moore@Sun.COM 
191*9160SSherry.Moore@Sun.COM /*
192*9160SSherry.Moore@Sun.COM  * Construct boot arguments from the specified GRUB menu entry.
193*9160SSherry.Moore@Sun.COM  * Caller must allocate space for fbarg, and call grub_cleanup_boot_args()
194*9160SSherry.Moore@Sun.COM  * when it's done with fbarg to clean up.
195*9160SSherry.Moore@Sun.COM  *
196*9160SSherry.Moore@Sun.COM  * Return 0 on success, errno on failure.
197*9160SSherry.Moore@Sun.COM  */
198*9160SSherry.Moore@Sun.COM int
grub_get_boot_args(grub_boot_args_t * fbarg,const char * menupath,int num)199*9160SSherry.Moore@Sun.COM grub_get_boot_args(grub_boot_args_t *fbarg, const char *menupath, int num)
200*9160SSherry.Moore@Sun.COM {
201*9160SSherry.Moore@Sun.COM 	int rc;
202*9160SSherry.Moore@Sun.COM 	grub_menu_t *mp;
203*9160SSherry.Moore@Sun.COM 
204*9160SSherry.Moore@Sun.COM 	assert(fbarg);
205*9160SSherry.Moore@Sun.COM 	if ((rc = grub_menu_init(menupath, &mp)) == 0) {
206*9160SSherry.Moore@Sun.COM 		rc = grub_menu_get_boot_args(mp, num, fbarg);
207*9160SSherry.Moore@Sun.COM 		grub_menu_fini(mp);
208*9160SSherry.Moore@Sun.COM 	}
209*9160SSherry.Moore@Sun.COM 	return (rc);
210*9160SSherry.Moore@Sun.COM }
211*9160SSherry.Moore@Sun.COM 
212*9160SSherry.Moore@Sun.COM /*
213*9160SSherry.Moore@Sun.COM  * Clean up when done with fbarg: close file handle, unmount file
214*9160SSherry.Moore@Sun.COM  * systems.  Must be safe to call even if not all the fields are
215*9160SSherry.Moore@Sun.COM  * set up.
216*9160SSherry.Moore@Sun.COM  */
217*9160SSherry.Moore@Sun.COM void
grub_cleanup_boot_args(grub_boot_args_t * fbarg)218*9160SSherry.Moore@Sun.COM grub_cleanup_boot_args(grub_boot_args_t *fbarg)
219*9160SSherry.Moore@Sun.COM {
220*9160SSherry.Moore@Sun.COM 	if (fbarg == NULL)
221*9160SSherry.Moore@Sun.COM 		return;
222*9160SSherry.Moore@Sun.COM 
223*9160SSherry.Moore@Sun.COM 	(void) close(fbarg->gba_kernel_fd);
224*9160SSherry.Moore@Sun.COM 	grub_fsd_umount_tmp(&fbarg->gba_fsd);
225*9160SSherry.Moore@Sun.COM }
226