示例#1
0
static EFI_STATUS
load(const char *filepath, dev_info_t *devinfo, void **bufp, size_t *bufsize)
{
	spa_t *spa;
	struct zfsmount zfsmount;
	dnode_phys_t dn;
	struct stat st;
	int err;
	void *buf;
	EFI_STATUS status;

	spa = devinfo->devdata;

	DPRINTF("load: '%s' spa: '%s', devpath: %s\n", filepath, spa->spa_name,
	    devpath_str(devinfo->devpath));

	if ((err = zfs_spa_init(spa)) != 0) {
		DPRINTF("Failed to load pool '%s' (%d)\n", spa->spa_name, err);
		return (EFI_NOT_FOUND);
	}

	if ((err = zfs_mount(spa, 0, &zfsmount)) != 0) {
		DPRINTF("Failed to mount pool '%s' (%d)\n", spa->spa_name, err);
		return (EFI_NOT_FOUND);
	}

	if ((err = zfs_lookup(&zfsmount, filepath, &dn)) != 0) {
		if (err == ENOENT) {
			DPRINTF("Failed to find '%s' on pool '%s' (%d)\n",
			    filepath, spa->spa_name, err);
			return (EFI_NOT_FOUND);
		}
		printf("Failed to lookup '%s' on pool '%s' (%d)\n", filepath,
		    spa->spa_name, err);
		return (EFI_INVALID_PARAMETER);
	}

	if ((err = zfs_dnode_stat(spa, &dn, &st)) != 0) {
		printf("Failed to stat '%s' on pool '%s' (%d)\n", filepath,
		    spa->spa_name, err);
		return (EFI_INVALID_PARAMETER);
	}

	if ((status = bs->AllocatePool(EfiLoaderData, (UINTN)st.st_size, &buf))
	    != EFI_SUCCESS) {
		printf("Failed to allocate load buffer %zu for pool '%s' for '%s' "
		    "(%lu)\n", st.st_size, spa->spa_name, filepath, EFI_ERROR_CODE(status));
		return (EFI_INVALID_PARAMETER);
	}

	if ((err = dnode_read(spa, &dn, 0, buf, st.st_size)) != 0) {
		printf("Failed to read node from %s (%d)\n", spa->spa_name,
		    err);
		(void)bs->FreePool(buf);
		return (EFI_INVALID_PARAMETER);
	}

	*bufsize = st.st_size;
	*bufp = buf;

	return (EFI_SUCCESS);
}
示例#2
0
int
main(int argc, char** argv)
{
	char buf[512], hash[33];
	MD5_CTX ctx;
	struct stat sb;
	struct zfsmount zfsmnt;
	dnode_phys_t dn;
#if 0
	uint64_t rootobj;
#endif
	spa_t *spa;
	off_t off;
	ssize_t n;
	int i, failures, *fd;

	zfs_init();
	if (argc == 1) {
		static char *av[] = {
			"zfsboottest",
			"/dev/gpt/system0",
			"/dev/gpt/system1",
			"-",
			"/boot/zfsloader",
			"/boot/support.4th",
			"/boot/kernel/kernel",
			NULL,
		};
		argc = sizeof(av) / sizeof(av[0]) - 1;
		argv = av;
	}
	for (i = 1; i < argc; i++) {
		if (strcmp(argv[i], "-") == 0)
			break;
	}
	fd = malloc(sizeof(fd[0]) * (i - 1));
	if (fd == NULL)
		errx(1, "Unable to allocate memory.");
	for (i = 1; i < argc; i++) {
		if (strcmp(argv[i], "-") == 0)
			break;
		fd[i - 1] = open(argv[i], O_RDONLY);
		if (fd[i - 1] == -1) {
			warn("open(%s) failed", argv[i]);
			continue;
		}
		if (vdev_probe(vdev_read, &fd[i - 1], NULL) != 0) {
			warnx("vdev_probe(%s) failed", argv[i]);
			close(fd[i - 1]);
		}
	}
	spa_all_status();

	spa = STAILQ_FIRST(&zfs_pools);
	if (spa == NULL) {
		fprintf(stderr, "no pools\n");
		exit(1);
	}

	if (zfs_spa_init(spa)) {
		fprintf(stderr, "can't init pool\n");
		exit(1);
	}

#if 0
	if (zfs_get_root(spa, &rootobj)) {
		fprintf(stderr, "can't get root\n");
		exit(1);
	}

	if (zfs_mount(spa, rootobj, &zfsmnt)) {
#else
	if (zfs_mount(spa, 0, &zfsmnt)) {
		fprintf(stderr, "can't mount\n");
		exit(1);
	}
#endif

	printf("\n");
	for (++i, failures = 0; i < argc; i++) {
		if (zfs_lookup(&zfsmnt, argv[i], &dn)) {
			fprintf(stderr, "%s: can't lookup\n", argv[i]);
			failures++;
			continue;
		}

		if (zfs_dnode_stat(spa, &dn, &sb)) {
			fprintf(stderr, "%s: can't stat\n", argv[i]);
			failures++;
			continue;
		}

		off = 0;
		MD5Init(&ctx);
		do {
			n = sb.st_size - off;
			n = n > sizeof(buf) ? sizeof(buf) : n;
			n = zfs_read(spa, &dn, buf, n, off);
			if (n < 0) {
				fprintf(stderr, "%s: zfs_read failed\n",
				    argv[i]);
				failures++;
				break;
			}
			MD5Update(&ctx, buf, n);
			off += n;
		} while (off < sb.st_size);
		if (off < sb.st_size)
			continue;
		MD5End(&ctx, hash);
		printf("%s %s\n", hash, argv[i]);
	}

	return (failures == 0 ? 0 : 1);
}