aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/rtlil.cc
Commit message (Expand)AuthorAgeFilesLines
* Bugfix in hierarchy handling of blackbox module portsClifford Wolf2018-01-051-1/+3
* Add RTLIL::Const::is_fully_ones()Clifford Wolf2017-12-141-0/+11
* Add SigSpec::is_fully_ones()Clifford Wolf2017-12-141-0/+15
* Add src arguments to all cell creator helper functionsClifford Wolf2017-09-091-56/+91
* Update more stuff to use get_src_attribute() and set_src_attribute()Clifford Wolf2017-09-011-1/+1
* Merge remote-tracking branch 'upstream/master'Jason Lowdermilk2017-08-301-0/+16
|\
| * Add {get,set}_src_attribute() methods on RTLIL::AttrObjectClifford Wolf2017-08-301-0/+16
* | fix indent levelJason Lowdermilk2017-08-301-2/+2
* | Add support for source line tracking through synthesis phaseJason Lowdermilk2017-08-291-3/+4
|/
* Add Const methods is_fully_zero(), is_fully_def(), and is_fully_undef()Clifford Wolf2017-08-181-0/+33
* Add $_ANDNOT_ and $_ORNOT_ gatesClifford Wolf2017-05-171-26/+30
* Add $live and $fair cell types, add support for s_eventually keywordClifford Wolf2017-02-251-1/+17
* Add $cover cell type and SVA cover() supportClifford Wolf2017-02-041-1/+9
* Fix RTLIL::Memory::start_offset initializationClifford Wolf2017-01-251-0/+1
* Bugfix in RTLIL::SigSpec::remove2()Clifford Wolf2016-12-311-3/+4
* Remember global declarations and defines accross read_verilog callsClifford Wolf2016-11-151-0/+2
* Added $anyseq cell typeClifford Wolf2016-10-141-1/+10
* Added $ff and $_FF_ cell typesClifford Wolf2016-10-121-0/+25
* Improvements in assertpmuxClifford Wolf2016-09-071-0/+16
* Removed $aconst cell typeClifford Wolf2016-08-301-1/+1
* Removed $predict againClifford Wolf2016-08-281-9/+1
* Fixed handling of transparent bram rd ports on ROMsClifford Wolf2016-08-271-0/+1
* Added $anyconst and $aconstClifford Wolf2016-07-271-0/+6
* Added $initstate cell type and vlog functionClifford Wolf2016-07-211-0/+6
* After reading the SV spec, using non-standard predict() instead of expect()Clifford Wolf2016-07-211-2/+2
* Added basic support for $expect cellsClifford Wolf2016-07-131-8/+17
* A few modifications after pull request commentsRuben Undheim2016-06-181-1/+1
* Added support for SystemVerilog packages with localparam definitionsRuben Undheim2016-06-181-0/+2
* Improved support for $sop cellsClifford Wolf2016-06-171-1/+1
* Added $sop cell type and "abc -sop"Clifford Wolf2016-06-171-0/+9
* Added addBufGate module methodClifford Wolf2016-02-021-0/+1
* rtlil: Improve performance of SigSpec::extract(SigSpec, SigSpec*)Rick Altherr2016-01-311-2/+31
* rtlil: speed up SigSpec::sort_and_unify()Rick Altherr2016-01-311-1/+11
* rtlil: improve performance of SigSpec::replace(SigSpec, SigSpec, SigSpec*)Rick Altherr2016-01-311-6/+14
* rtlil: improve performance of SigSpec::remove2(SigSpec, SigSpec*)Rick Altherr2016-01-311-2/+29
* rtlil: rewrite remove2() to avoid copyingRick Altherr2016-01-301-45/+18
* rtlil: duplicate remove2() for std::set<>Rick Altherr2016-01-291-0/+39
* Import more std:: stuff into Yosys namespaceClifford Wolf2015-10-251-14/+14
* renamed SigSpec::to_single_sigbit() to SigSpec::as_bit(), added is_bit()Clifford Wolf2015-10-241-12/+11
* Fixed driver conflict handling (various cmds)Clifford Wolf2015-10-241-3/+12
* Fixed handling of driver-driver conflicts in wreduceClifford Wolf2015-10-241-0/+4
* Added read-enable to memory modelClifford Wolf2015-09-251-0/+2
* Cosmetic fix in Module::addLut()Clifford Wolf2015-09-181-4/+4
* Added $tribuf and $_TBUF_ cell typesClifford Wolf2015-08-161-0/+20
* Fixed handling of [a-fxz?] in decimal constantsClifford Wolf2015-08-111-0/+4
* Added WORDS parameter to $meminitClifford Wolf2015-07-311-1/+1
* Fixed trailing whitespacesClifford Wolf2015-07-021-5/+5
* Added design->rename(module, new_name)Clifford Wolf2015-06-301-0/+7
* Added "rename -top new_name"Clifford Wolf2015-06-171-0/+15
* Fixed cstr_buf for std::string with small string optimizationClifford Wolf2015-06-111-1/+1
0 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
/* Copyright (c) 2008, XenSource Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of XenSource Inc. nor the names of its contributors
 *       may be used to endorse or promote products derived from this software
 *       without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <time.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <libgen.h>
#include <inttypes.h>
#include <sys/stat.h>

#include "libvhd.h"
#include "vhd-util.h"

// allow the VHD timestamp to be at most this many seconds into the future to 
// account for time skew with NFS servers
#define TIMESTAMP_MAX_SLACK 1800

static int
vhd_util_check_zeros(void *buf, size_t size)
{
	int i;
	char *p;

	p = buf;
	for (i = 0; i < size; i++)
		if (p[i])
			return i;

	return 0;
}

static int
vhd_util_check_footer_opened(vhd_footer_t *footer)
{
	int i, n;
	uint32_t *buf;

	buf = (uint32_t *)footer;
	n = sizeof(*footer) / sizeof(uint32_t);

	for (i = 0; i < n; i++)
		if (buf[i] != 0xc7c7c7c7)
			return 0;

	return 1;
}

static char *
vhd_util_check_validate_footer(vhd_footer_t *footer)
{
	int size;
	uint32_t checksum, now;

	size = sizeof(footer->cookie);
	if (memcmp(footer->cookie, HD_COOKIE, size))
		return "invalid cookie";

	checksum = vhd_checksum_footer(footer);
	if (checksum != footer->checksum) {
		if (footer->hidden &&
		    !strncmp(footer->crtr_app, "tap", 3) &&
		    (footer->crtr_ver == VHD_VERSION(0, 1) ||
		     footer->crtr_ver == VHD_VERSION(1, 1))) {
			char tmp = footer->hidden;
			footer->hidden = 0;
			checksum = vhd_checksum_footer(footer);
			footer->hidden = tmp;

			if (checksum == footer->checksum)
				goto ok;
		}

		return "invalid checksum";
	}

ok:
	if (!(footer->features & HD_RESERVED))
		return "invalid 'reserved' feature";

	if (footer->features & ~(HD_TEMPORARY | HD_RESERVED))
		return "invalid extra features";

	if (footer->ff_version != HD_FF_VERSION)
		return "invalid file format version";

	if (footer->type != HD_TYPE_DYNAMIC &&
	    footer->type != HD_TYPE_DIFF    &&
	    footer->data_offset != ~(0ULL))
		return "invalid data offset";

	now = vhd_time(time(NULL));
	if (footer->timestamp > now + TIMESTAMP_MAX_SLACK)
		return "creation time in future";

	if (!strncmp(footer->crtr_app, "tap", 3) &&
	    footer->crtr_ver > VHD_CURRENT_VERSION)
		return "unsupported tap creator version";

	if (vhd_chs(footer->curr_size) < footer->geometry)
		return "geometry too large";

	if (footer->type != HD_TYPE_FIXED   &&
	    footer->type != HD_TYPE_DYNAMIC &&
	    footer->type != HD_TYPE_DIFF)
		return "invalid type";

	if (footer->saved && footer->saved != 1)
		return "invalid 'saved' state";

	if (footer->hidden && footer->hidden != 1)
		return "invalid 'hidden' state";

	if (vhd_util_check_zeros(footer->reserved,
				 sizeof(footer->reserved)))
		return "invalid 'reserved' bits";

	return NULL;
}

static char *
vhd_util_check_validate_header(int fd, vhd_header_t *header)
{
	off_t eof;
	int i, cnt, size;
	uint32_t checksum;

	size = sizeof(header->cookie);
	if (memcmp(header->cookie, DD_COOKIE, size))
		return "invalid cookie";

	checksum = vhd_checksum_header(header);
	if (checksum != header->checksum)
		return "invalid checksum";

	if (header->hdr_ver != 0x00010000)
		return "invalid header version";

	if (header->data_offset != ~(0ULL))
		return "invalid data offset";

	eof = lseek(fd, 0, SEEK_END);
	if (eof == (off_t)-1)
		return "error finding eof";

	if (header->table_offset <= 0  ||
	    header->table_offset % 512 ||
	    (header->table_offset +
	     (header->max_bat_size * sizeof(uint32_t)) >
	     eof - sizeof(vhd_footer_t)))
		return "invalid table offset";

	for (cnt = 0, i = 0; i < sizeof(header->block_size) * 8; i++)
		if ((header->block_size >> i) & 1)
			cnt++;

	if (cnt != 1)
		return "invalid block size";

	if (header->res1)
		return "invalid reserved bits";

	if (vhd_util_check_zeros(header->res2, sizeof(header->res2)))
		return "invalid reserved bits";

	return NULL;
}

static char *
vhd_util_check_validate_differencing_header(vhd_context_t *vhd)
{
	vhd_header_t *header;

	header = &vhd->header;

	if (vhd->footer.type == HD_TYPE_DIFF) {
		char *parent;
		uint32_t now;

		now = vhd_time(time(NULL));
		if (header->prt_ts > now + TIMESTAMP_MAX_SLACK)
			return "parent creation time in future";

		if (vhd_header_decode_parent(vhd, header, &parent))
			return "invalid parent name";

		free(parent);
	} else {
		if (vhd_util_check_zeros(header->prt_name,
					 sizeof(header->prt_name)))
			return "invalid non-null parent name";

		if (vhd_util_check_zeros(header->loc, sizeof(header->loc)))
			return "invalid non-null parent locators";

		if (!blk_uuid_is_nil(&header->prt_uuid))
			return "invalid non-null parent uuid";

		if (header->prt_ts)
			return "invalid non-zero parent timestamp";
	}

	return NULL;
}

static char *
vhd_util_check_validate_batmap(vhd_context_t *vhd, vhd_batmap_t *batmap)
{
	int size;
	off_t eof;
	uint32_t checksum;

	size = sizeof(batmap->header.cookie);
	if (memcmp(batmap->header.cookie, VHD_BATMAP_COOKIE, size))
		return "invalid cookie";

	if (batmap->header.batmap_version > VHD_BATMAP_CURRENT_VERSION)
		return "unsupported batmap version";

	checksum = vhd_checksum_batmap(batmap);
	if (checksum != batmap->header.checksum)
		return "invalid checksum";

	if (!batmap->header.batmap_size)
		return "invalid size zero";

	eof = lseek(vhd->fd, 0, SEEK_END);
	if (eof == (off_t)-1)
		return "error finding eof";

	if (!batmap->header.batmap_offset ||
	    batmap->header.batmap_offset % 512)
		return "invalid batmap offset";

	if ((batmap->header.batmap_offset +
	     vhd_sectors_to_bytes(batmap->header.batmap_size)) >
	    eof - sizeof(vhd_footer_t))
		return "invalid batmap size";

	return NULL;
}

static char *
vhd_util_check_validate_parent_locator(vhd_context_t *vhd,
				       vhd_parent_locator_t *loc)
{
	off_t eof;

	if (vhd_validate_platform_code(loc->code))
		return "invalid platform code";

	if (loc->code == PLAT_CODE_NONE) {
		if (vhd_util_check_zeros(loc, sizeof(*loc)))
			return "non-zero locator";

		return NULL;
	}

	if (!loc->data_offset)
		return "invalid data offset";

	if (!loc->data_space)
		return "invalid data space";

	if (!loc->data_len)
		return "invalid data length";

	eof = lseek(vhd->fd, 0, SEEK_END);
	if (eof == (off_t)-1)
		return "error finding eof";

	if (loc->data_offset + vhd_parent_locator_size(loc) >
	    eof - sizeof(vhd_footer_t))
		return "invalid size";

	if (loc->res)
		return "invalid reserved bits";

	return NULL;
}

static const char *
vhd_util_check_validate_parent(vhd_context_t *vhd, const char *ppath)
{
	const char *msg;
	vhd_context_t parent;
	uint32_t status;

	msg = NULL;

	if (vhd_parent_raw(vhd))
		return msg;

	if (vhd_open(&parent, ppath,
				VHD_OPEN_RDONLY | VHD_OPEN_IGNORE_DISABLED))
		return "error opening parent";

	if (blk_uuid_compare(&vhd->header.prt_uuid, &parent.footer.uuid)) {
		msg = "invalid parent uuid";
		goto out;
	}

out:
	vhd_close(&parent);
	return msg;
}

static int
vhd_util_check_footer(int fd, vhd_footer_t *footer, int ignore)
{
	size_t size;
	int err, opened;
	char *msg, *buf;
	off_t eof, off;
	vhd_footer_t primary, backup;

	memset(&primary, 0, sizeof(primary));
	memset(&backup, 0, sizeof(backup));

	err = posix_memalign((void **)&buf, VHD_SECTOR_SIZE, sizeof(primary));
	if (err) {
		printf("error allocating buffer: %d\n", err);
		return -err;
	}

	memset(buf, 0, sizeof(primary));

	eof = lseek(fd, 0, SEEK_END);
	if (eof == (off_t)-1) {
		err = -errno;
		printf("error calculating end of file: %d\n", err);
		goto out;
	}

	size = ((eof % 512) ? 511 : 512);
	eof  = lseek(fd, eof - size, SEEK_SET);
	if (eof == (off_t)-1) {
		err = -errno;
		printf("error calculating end of file: %d\n", err);
		goto out;
	}

	err = read(fd, buf, 512);
	if (err != size) {
		err = (errno ? -errno : -EIO);
		printf("error reading primary footer: %d\n", err);
		goto out;
	}

	memcpy(&primary, buf, sizeof(primary));
	opened = vhd_util_check_footer_opened(&primary);
	vhd_footer_in(&primary);

	msg = vhd_util_check_validate_footer(&primary);
	if (msg) {
		if (opened && ignore)
			goto check_backup;

		err = -EINVAL;
		printf("primary footer invalid: %s\n", msg);
		goto out;
	}

	if (primary.type == HD_TYPE_FIXED) {
		err = 0;
		goto out;
	}

check_backup:
	off = lseek(fd, 0, SEEK_SET);
	if (off == (off_t)-1) {
		err = -errno;
		printf("error seeking to backup footer: %d\n", err);
		goto out;
	}

	size = 512;
	memset(buf, 0, sizeof(primary));

	err = read(fd, buf, size);
	if (err != size) {
		err = (errno ? -errno : -EIO);
		printf("error reading backup footer: %d\n", err);
		goto out;
	}

	memcpy(&backup, buf, sizeof(backup));
	vhd_footer_in(&backup);

	msg = vhd_util_check_validate_footer(&backup);
	if (msg) {
		err = -EINVAL;
		printf("backup footer invalid: %s\n", msg);
		goto out;
	}

	if (memcmp(&primary, &backup, sizeof(primary))) {
		if (opened && ignore) {
			memcpy(&primary, &backup, sizeof(primary));
			goto ok;
		}

		if (backup.hidden &&
		    !strncmp(backup.crtr_app, "tap", 3) &&
		    (backup.crtr_ver == VHD_VERSION(0, 1) ||
		     backup.crtr_ver == VHD_VERSION(1, 1))) {
			char cmp, tmp = backup.hidden;
			backup.hidden = 0;
			cmp = memcmp(&primary, &backup, sizeof(primary));
			backup.hidden = tmp;
			if (!cmp)
				goto ok;
		}

		err = -EINVAL;
		printf("primary and backup footers do not match\n");
		goto out;
	}

ok:
	err = 0;
	memcpy(footer, &primary, sizeof(primary));

out:
	free(buf);
	return err;
}

static int
vhd_util_check_header(int fd, vhd_footer_t *footer)
{
	int err;
	off_t off;
	char *msg, *buf;
	vhd_header_t header;

	err = posix_memalign((void **)&buf, VHD_SECTOR_SIZE, sizeof(header));
	if (err) {
		printf("error allocating header: %d\n", err);
		return err;
	}

	off = footer->data_offset;
	off = lseek(fd, off, SEEK_SET);
	if (off == (off_t)-1) {
		err = -errno;
		printf("error seeking to header: %d\n", err);
		goto out;
	}

	err = read(fd, buf, sizeof(header));
	if (err != sizeof(header)) {
		err = (errno ? -errno : -EIO);
		printf("error reading header: %d\n", err);
		goto out;
	}

	memcpy(&header, buf, sizeof(header));
	vhd_header_in(&header);

	msg = vhd_util_check_validate_header(fd, &header);
	if (msg) {
		err = -EINVAL;
		printf("header is invalid: %s\n", msg);
		goto out;
	}

	err = 0;

out:
	free(buf);
	return err;
}

static int
vhd_util_check_differencing_header(vhd_context_t *vhd)
{
	char *msg;

	msg = vhd_util_check_validate_differencing_header(vhd);
	if (msg) {
		printf("differencing header is invalid: %s\n", msg);
		return -EINVAL;
	}

	return 0;
}

static int
vhd_util_check_bat(vhd_context_t *vhd)
{
	off_t eof, eoh;
	int i, j, err, block_size;

	err = vhd_seek(vhd, 0, SEEK_END);
	if (err) {
		printf("error calculating eof: %d\n", err);
		return err;
	}

	eof = vhd_position(vhd);
	if (eof == (off_t)-1) {
		printf("error calculating eof: %d\n", -errno);
		return -errno;
	}

	/* adjust eof for vhds with short footers */
	if (eof % 512) {
		if (eof % 512 != 511) {
			printf("invalid file size: 0x%"PRIx64"\n", eof);
			return -EINVAL;
		}

		eof++;
	}

	err = vhd_get_bat(vhd);
	if (err) {
		printf("error reading bat: %d\n", err);
		return err;
	}

	err = vhd_end_of_headers(vhd, &eoh);
	if (err) {
		printf("error calculating end of metadata: %d\n", err);
		return err;
	}

	eof  -= sizeof(vhd_footer_t);
	eof >>= VHD_SECTOR_SHIFT;
	eoh >>= VHD_SECTOR_SHIFT;
	block_size = vhd->spb + vhd->bm_secs;

	for (i = 0; i < vhd->header.max_bat_size; i++) {
		uint32_t off = vhd->bat.bat[i];
		if (off == DD_BLK_UNUSED)
			continue;

		if (off < eoh) {
			printf("block %d (offset 0x%x) clobbers headers\n",
			       i, off);
			return -EINVAL;
		}

		if (off + block_size > eof) {
			printf("block %d (offset 0x%x) clobbers footer\n",
			       i, off);
			return -EINVAL;
		}

		for (j = 0; j < vhd->header.max_bat_size; j++) {
			uint32_t joff = vhd->bat.bat[j];

			if (i == j)
				continue;

			if (joff == DD_BLK_UNUSED)
				continue;

			if (off == joff)
				err = -EINVAL;

			if (off > joff && off < joff + block_size)
				err = -EINVAL;

			if (off + block_size > joff &&
			    off + block_size < joff + block_size)
				err = -EINVAL;

			if (err) {
				printf("block %d (offset 0x%x) clobbers "
				       "block %d (offset 0x%x)\n",
				       i, off, j, joff);
				return err;
			}
		}
	}

	return 0;
}

static int
vhd_util_check_batmap(vhd_context_t *vhd)
{
	char *msg;
	int i, err;

	err = vhd_get_bat(vhd);
	if (err) {
		printf("error reading bat: %d\n", err);
		return err;
	}

	err = vhd_get_batmap(vhd);
	if (err) {
		printf("error reading batmap: %d\n", err);
		return err;
	}

	msg = vhd_util_check_validate_batmap(vhd, &vhd->batmap);
	if (msg) {
		printf("batmap is invalid: %s\n", msg);
		return -EINVAL;
	}

	for (i = 0; i < vhd->header.max_bat_size; i++) {
		if (!vhd_batmap_test(vhd, &vhd->batmap, i))
			continue;

		if (vhd->bat.bat[i] == DD_BLK_UNUSED) {
			printf("batmap shows unallocated block %d full\n", i);
			return -EINVAL;
		}
	}

	return 0;
}

static int
vhd_util_check_parent_locators(vhd_context_t *vhd)
{
	int i, n, err;
	vhd_parent_locator_t *loc;
	char *file, *ppath, *location, *pname;
	const char *msg;
	int mac, macx, w2ku, w2ru, wi2r, wi2k, found;

	mac      = 0;
	macx     = 0;
	w2ku     = 0;
	w2ru     = 0;
	wi2r     = 0;
	wi2k     = 0;
	found    = 0;
	pname    = NULL;
	ppath    = NULL;
	location = NULL;

	err = vhd_header_decode_parent(vhd, &vhd->header, &pname);
	if (err) {
		printf("error decoding parent name: %d\n", err);
		return err;
	}

	n = sizeof(vhd->header.loc) / sizeof(vhd->header.loc[0]);
	for (i = 0; i < n; i++) {
		ppath    = NULL;
		location = NULL;
		loc = vhd->header.loc + i;

		msg = vhd_util_check_validate_parent_locator(vhd, loc);
		if (msg) {
			err = -EINVAL;
			printf("invalid parent locator %d: %s\n", i, msg);
			goto out;
		}

		if (loc->code == PLAT_CODE_NONE)
			continue;

		switch (loc->code) {
		case PLAT_CODE_MACX:
			if (macx++)
				goto dup;
			break;

		case PLAT_CODE_MAC:
			if (mac++)
				goto dup;
			break;

		case PLAT_CODE_W2KU:
			if (w2ku++)
				goto dup;
			break;

		case PLAT_CODE_W2RU:
			if (w2ru++)
				goto dup;
			break;

		case PLAT_CODE_WI2R:
			if (wi2r++)
				goto dup;
			break;

		case PLAT_CODE_WI2K:
			if (wi2k++)
				goto dup;
			break;

		default:
			err = -EINVAL;
			printf("invalid  platform code for locator %d\n", i);
			goto out;
		}

		if (loc->code != PLAT_CODE_MACX &&
		    loc->code != PLAT_CODE_W2RU &&
		    loc->code != PLAT_CODE_W2KU)
			continue;

		err = vhd_parent_locator_read(vhd, loc, &ppath);
		if (err) {
			printf("error reading parent locator %d: %d\n", i, err);
			goto out;
		}

		file = basename(ppath);
		if (strcmp(pname, file)) {
			err = -EINVAL;
			printf("parent locator %d name (%s) does not match "
			       "header name (%s)\n", i, file, pname);
			goto out;
		}

		err = vhd_find_parent(vhd, ppath, &location);
		if (err) {
			printf("error resolving %s: %d\n", ppath, err);
			goto out;
		}

		err = access(location, R_OK);
		if (err && loc->code == PLAT_CODE_MACX) {
			err = -errno;
			printf("parent locator %d points to missing file %s "
				"(resolved to %s)\n", i, ppath, location);
			goto out;
		}

		msg = vhd_util_check_validate_parent(vhd, location);
		if (msg) {
			err = -EINVAL;
			printf("invalid parent %s: %s\n", location, msg);
			goto out;
		}

		found++;
		free(ppath);
		free(location);
		ppath = NULL;
		location = NULL;

		continue;

	dup:
		printf("duplicate platform code in locator %d: 0x%x\n",
		       i, loc->code);
		err = -EINVAL;
		goto out;
	}

	if (!found) {
		err = -EINVAL;