示例#1
0
static void
get_findroot_cap(const char *osroot)
{
	FILE		*fp;
	char		path[PATH_MAX];
	char		buf[BAM_MAXLINE];
	struct stat	sb;
	int		dboot;
	int		error;
	int		ret;
	const char	*fcn = "get_findroot_cap()";

	assert(is_grub(osroot));

	(void) snprintf(path, sizeof (path), "%s/%s",
	    osroot, "boot/grub/capability");

	if (stat(path, &sb) == -1) {
		bam_is_findroot = BAM_FINDROOT_ABSENT;
		BAM_DPRINTF((D_FINDROOT_ABSENT, fcn));
		return;
	}

	fp = fopen(path, "r");
	error = errno;
	INJECT_ERROR1("GET_CAP_FINDROOT_FOPEN", fp = NULL);
	if (fp == NULL) {
		bam_error(OPEN_FAIL, path, strerror(error));
		return;
	}

	dboot = 0;
	while (s_fgets(buf, sizeof (buf), fp) != NULL) {
		if (strcmp(buf, "findroot") == 0) {
			BAM_DPRINTF((D_FINDROOT_PRESENT, fcn));
			bam_is_findroot = BAM_FINDROOT_PRESENT;
		}
		if (strcmp(buf, "dboot") == 0) {
			BAM_DPRINTF((D_DBOOT_PRESENT, fcn));
			dboot = 1;
		}
	}

	assert(dboot);

	if (bam_is_findroot == BAM_FINDROOT_UNKNOWN) {
		bam_is_findroot = BAM_FINDROOT_ABSENT;
		BAM_DPRINTF((D_FINDROOT_ABSENT, fcn));
	}
out:
	ret = fclose(fp);
	error = errno;
	INJECT_ERROR1("GET_CAP_FINDROOT_FCLOSE", ret = 1);
	if (ret != 0) {
		bam_error(CLOSE_FAIL, path, strerror(error));
	}
}
示例#2
0
static char *
skip_wspace(char *ptr)
{
	const char		*fcn = "skip_wspace()";

	INJECT_ERROR1("SKIP_WSPACE", ptr = NULL);
	if (ptr == NULL) {
		BAM_DPRINTF((D_SKIP_WSPACE_PTR_NULL, fcn));
		return (NULL);
	}

	BAM_DPRINTF((D_SKIP_WSPACE_ENTRY_PTR, fcn, ptr));
	for (; *ptr != '\0'; ptr++) {
		if ((*ptr != ' ') && (*ptr != '\t') &&
		    (*ptr != '\n'))
			break;
	}

	ptr = (*ptr == '\0' ? NULL : ptr);

	BAM_DPRINTF((D_SKIP_WSPACE_EXIT_PTR, fcn, ptr ? ptr : "NULL"));

	return (ptr);
}
示例#3
0
static char *
skip_wspace(char *ptr)
{
	const char		*fcn = "skip_wspace()";

	INJECT_ERROR1("SKIP_WSPACE", ptr = NULL);
	if (ptr == NULL) {
		BAM_DPRINTF(("%s: NULL ptr\n", fcn));
		return (NULL);
	}

	BAM_DPRINTF(("%s: ptr on entry: %s\n", fcn, ptr));
	for (; *ptr != '\0'; ptr++) {
		if ((*ptr != ' ') && (*ptr != '\t') &&
		    (*ptr != '\n'))
			break;
	}

	ptr = (*ptr == '\0' ? NULL : ptr);

	BAM_DPRINTF(("%s: ptr on exit: %s\n", fcn, ptr ? ptr : "NULL"));

	return (ptr);
}
示例#4
0
error_t
get_boot_cap(const char *osroot)
{
	char		fname[PATH_MAX];
	char		*image;
	uchar_t		*ident;
	int		fd;
	int		m;
	multiboot_header_t *mbh;
	struct stat	sb;
	int		error;
	const char	*fcn = "get_boot_cap()";

	if (is_sparc()) {
		/* there is no non dboot sparc new-boot */
		bam_direct = BAM_DIRECT_DBOOT;
		BAM_DPRINTF((D_IS_SPARC_DBOOT, fcn));
		return (BAM_SUCCESS);
	}

	if (!is_grub(osroot)) {
		bam_error(NOT_GRUB_ROOT, osroot);
		return (BAM_ERROR);
	}

	(void) snprintf(fname, PATH_MAX, "%s/%s", osroot,
	    "platform/i86pc/kernel/unix");
	fd = open(fname, O_RDONLY);
	error = errno;
	INJECT_ERROR1("GET_CAP_UNIX_OPEN", fd = -1);
	if (fd < 0) {
		bam_error(OPEN_FAIL, fname, strerror(error));
		return (BAM_ERROR);
	}

	/*
	 * Verify that this is a sane unix at least 8192 bytes in length
	 */
	if (fstat(fd, &sb) == -1 || sb.st_size < 8192) {
		(void) close(fd);
		bam_error(INVALID_BINARY, fname);
		return (BAM_ERROR);
	}

	/*
	 * mmap the first 8K
	 */
	image = mmap(NULL, 8192, PROT_READ, MAP_SHARED, fd, 0);
	error = errno;
	INJECT_ERROR1("GET_CAP_MMAP", image = MAP_FAILED);
	if (image == MAP_FAILED) {
		bam_error(MMAP_FAIL, fname, strerror(error));
		return (BAM_ERROR);
	}

	ident = (uchar_t *)image;
	if (ident[EI_MAG0] != ELFMAG0 || ident[EI_MAG1] != ELFMAG1 ||
	    ident[EI_MAG2] != ELFMAG2 || ident[EI_MAG3] != ELFMAG3) {
		bam_error(NOT_ELF_FILE, fname);
		return (BAM_ERROR);
	}
	if (ident[EI_CLASS] != ELFCLASS32) {
		bam_error(WRONG_ELF_CLASS, fname, ident[EI_CLASS]);
		return (BAM_ERROR);
	}

	/*
	 * The GRUB multiboot header must be 32-bit aligned and completely
	 * contained in the 1st 8K of the file.  If the unix binary has
	 * a multiboot header, then it is a 'dboot' kernel.  Otherwise,
	 * this kernel must be booted via multiboot -- we call this a
	 * 'multiboot' kernel.
	 */
	bam_direct = BAM_DIRECT_MULTIBOOT;
	for (m = 0; m < 8192 - sizeof (multiboot_header_t); m += 4) {
		mbh = (void *)(image + m);
		if (mbh->magic == MB_HEADER_MAGIC) {
			BAM_DPRINTF((D_IS_DBOOT, fcn));
			bam_direct = BAM_DIRECT_DBOOT;
			break;
		}
	}
	(void) munmap(image, 8192);
	(void) close(fd);

	INJECT_ERROR1("GET_CAP_MULTIBOOT", bam_direct = BAM_DIRECT_MULTIBOOT);
	if (bam_direct == BAM_DIRECT_DBOOT) {
		if (bam_is_hv == BAM_HV_PRESENT) {
			BAM_DPRINTF((D_IS_XVM, fcn));
		} else {
			BAM_DPRINTF((D_IS_NOT_XVM, fcn));
		}
	} else {
		BAM_DPRINTF((D_IS_MULTIBOOT, fcn));
	}

	/* Not a fatal error if this fails */
	get_findroot_cap(osroot);

	BAM_DPRINTF((D_RETURN_SUCCESS, fcn));
	return (BAM_SUCCESS);
}