753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore/*
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * CDDL HEADER START
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore *
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * The contents of this file are subject to the terms of the
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * Common Development and Distribution License (the "License").
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * You may not use this file except in compliance with the License.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore *
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * or http://www.opensolaris.org/os/licensing.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * See the License for the specific language governing permissions
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * and limitations under the License.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore *
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * When distributing Covered Code, include this CDDL HEADER in each
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * If applicable, add the following below this CDDL HEADER, with the
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * fields enclosed by brackets "[]" replaced with your own identifying
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * information: Portions Copyright [yyyy] [name of copyright owner]
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore *
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * CDDL HEADER END
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore */
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore/*
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * Use is subject to license terms.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore */
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore/*
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * This file contains functions for constructing boot arguments
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * from GRUB menu for Fast Reboot.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore */
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore#include <stdio.h>
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore#include <stdlib.h>
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore#include <errno.h>
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore#include <strings.h>
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore#include <unistd.h>
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore#include <fcntl.h>
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore#include <assert.h>
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore#include <sys/types.h>
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore#include <sys/elf.h>
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore#include "libgrub_impl.h"
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore#if defined(__sparc)
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore#define CUR_ELFDATA ELFDATA2MSB
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore#elif defined(__i386)
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore#define CUR_ELFDATA ELFDATA2LSB
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore#endif /* __i386 */
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore/*
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * Open the kernel file.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * Return zero on sucess or error code otherwise.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * On success the kernel file descriptor is returned in fdp.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore */
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moorestatic int
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Mooreget_kernel_fd(const char *path, int *fdp)
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore{
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore const char *bname;
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore int fd = -1, class, format;
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore char ident[EI_NIDENT];
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore /* kernel basename must be unix */
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore if ((bname = strrchr(path, '/')) == NULL)
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore bname = path;
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore else
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore bname++;
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore if (strcmp(bname, "unix") != 0) {
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore if (strcmp(bname, "xen.gz") == 0)
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore return (EG_XVMNOTSUP);
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore return (EG_NOTUNIX);
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore }
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore if ((fd = open64(path, O_RDONLY)) >= 0 &&
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore (pread64(fd, ident, sizeof (ident), 0) == sizeof (ident))) {
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore class = ident[EI_CLASS];
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore format = ident[EI_DATA];
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore if ((class == ELFCLASS32 || class == ELFCLASS64) &&
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore (memcmp(&ident[EI_MAG0], ELFMAG, 4) == 0) &&
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore format == CUR_ELFDATA) {
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore *fdp = fd;
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore return (0);
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore }
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore }
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore if (fd >= 0)
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore (void) close(fd);
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore return (EG_OPENKERNFILE);
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore}
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore/*
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * Construct boot arguments for Fast Reboot from the ge_barg field of
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * a GRUB menu entry.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * Return 0 on success, errno on failure.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore */
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moorestatic int
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moorebarg2bootargs(const grub_barg_t *barg, grub_boot_args_t *fbarg)
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore{
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore int rc = 0;
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore char path[BOOTARGS_MAX];
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore char rpath[BOOTARGS_MAX];
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore const grub_fsdesc_t *fsd;
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore assert(fbarg);
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore bzero(fbarg, sizeof (*fbarg));
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore fbarg->gba_kernel_fd = -1;
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore if (!IS_BARG_VALID(barg))
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore return (EINVAL);
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore if ((fsd = grub_get_rootfsd(&barg->gb_root)) == NULL)
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore return (EG_UNKNOWNFS);
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore bcopy(fsd, &fbarg->gba_fsd, sizeof (fbarg->gba_fsd));
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore bcopy(barg->gb_kernel, fbarg->gba_kernel, sizeof (fbarg->gba_kernel));
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore bcopy(barg->gb_module, fbarg->gba_module, sizeof (fbarg->gba_module));
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore if (fbarg->gba_fsd.gfs_mountp[0] == 0 &&
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore (rc = grub_fsd_mount_tmp(&fbarg->gba_fsd,
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore barg->gb_root.gr_fstyp)) != 0)
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore return (rc);
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore if (snprintf(path, sizeof (path), "%s%s", fbarg->gba_fsd.gfs_mountp,
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore fbarg->gba_kernel) >= sizeof (path)) {
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore rc = E2BIG;
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore goto err_out;
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore }
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore (void) strtok(path, " \t");
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore (void) clean_path(path);
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore /*
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * GRUB requires absolute path, no symlinks, so do we
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore */
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore if ((rc = resolvepath(path, rpath, sizeof (rpath))) == -1)
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore rc = errno;
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore else {
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore rpath[rc] = 0;
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore if (strcmp(rpath, path) != 0)
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore rc = EG_NOTABSPATH;
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore else
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore rc = get_kernel_fd(rpath, &fbarg->gba_kernel_fd);
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore }
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore /* construct bootargs command-line */
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore if (rc == 0 && snprintf(fbarg->gba_bootargs,
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore sizeof (fbarg->gba_bootargs), "%s %s", fbarg->gba_fsd.gfs_mountp,
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore fbarg->gba_kernel) >= sizeof (fbarg->gba_bootargs))
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore rc = E2BIG;
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Mooreerr_out:
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore if (rc != 0)
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore grub_cleanup_boot_args(fbarg);
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore return (rc);
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore}
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore/*
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * Construct boot arguments for Fast Reboot from grub_menu_t.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * Return 0 on success, errno on failure.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore */
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moorestatic int
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Mooregrub_entry_get_boot_args(grub_entry_t *ent, grub_boot_args_t *fbarg)
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore{
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore int rc = EG_INVALIDENT;
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore if (IS_ENTRY_VALID(ent) && (rc = grub_entry_construct_barg(ent)) == 0)
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore return (barg2bootargs(&ent->ge_barg, fbarg));
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore else
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore return (rc);
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore}
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore/*
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * Construct boot arguments for Fast Reboot from grub_menu_t and the
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * entry number.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * Return 0 on success, errno on failure.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore */
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moorestatic int
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Mooregrub_menu_get_boot_args(const grub_menu_t *mp, int num,
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore grub_boot_args_t *fbarg)
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore{
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore grub_entry_t *ent;
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore assert(mp);
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore assert(fbarg);
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore if ((ent = grub_menu_get_entry(mp, num)) == NULL)
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore return (EG_NOENTRY);
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore return (grub_entry_get_boot_args(ent, fbarg));
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore}
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore/*
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * Construct boot arguments from the specified GRUB menu entry.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * Caller must allocate space for fbarg, and call grub_cleanup_boot_args()
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * when it's done with fbarg to clean up.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore *
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * Return 0 on success, errno on failure.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore */
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Mooreint
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Mooregrub_get_boot_args(grub_boot_args_t *fbarg, const char *menupath, int num)
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore{
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore int rc;
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore grub_menu_t *mp;
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore assert(fbarg);
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore if ((rc = grub_menu_init(menupath, &mp)) == 0) {
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore rc = grub_menu_get_boot_args(mp, num, fbarg);
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore grub_menu_fini(mp);
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore }
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore return (rc);
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore}
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore/*
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * Clean up when done with fbarg: close file handle, unmount file
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * systems. Must be safe to call even if not all the fields are
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore * set up.
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore */
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moorevoid
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Mooregrub_cleanup_boot_args(grub_boot_args_t *fbarg)
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore{
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore if (fbarg == NULL)
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore return;
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore (void) close(fbarg->gba_kernel_fd);
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore grub_fsd_umount_tmp(&fbarg->gba_fsd);
753a6d457b330b1b29b2d3eefcd0831116ce950dSherry Moore}