Загрузка данных


###############################################################################

sub do_substitutions

{
    my ($cfgRef) = @_;
    my @dict;
    my $sub = 0;

    # Loop over device file and search and replace the config
    for my $i (0 .. @$cfgRef-1)
    {
        my $line = ${$cfgRef}[$i];
        my($first, $rest) = split(/ /, $line, 2);

		if ($line =~ /^MIX\b/)
		{
			next;
		}

        # Save/replace dictionary items
        if ($first =~ /:$/)
        {
            # Strip off filename and line
            $line =~ s/ filename=.+$//g;            

            unshift @dict, $line;
        }
        else
        {
            # Do replacements
            foreach (@dict)
            {
                my($first, $rest) = split(/ /, $_, 2);

                # Strip : off end
                $first =~ s/:$//g;          

                # Going to replace?
                if (${$cfgRef}[$i] =~ /\b$first\b/)
                {
                    $sub++;
                }

                # Empty replace
                if (!(defined $rest && length $rest > 0))
                {
                    $rest = '';
                }

                # Replace
                s/\b$first\b/$rest/g for ${$cfgRef}[$i];    
            }
        }
    }

    return $sub;
}

###############################################################################

sub sanity_check
{
    my ($cfgRef) = @_;

	my @smClocks;

	# Get list of LM and agents
    my @list = grep(/^LM\d*\b/ || /^SCMI_AGENT\d*\b/ || /DEV_SM_CLK_\S*\b/,
    	@$cfgRef);

    # Loop over the list
    my $lmCnt = -1;
    my $agentCnt = -1;
    foreach my $l (@list)
    {
		# Handle LM
		if ($l =~ /^LM(\d*)\b/)
		{
            my $lmIdx = $1;
            my $lmHandle = 'LM' . $lmIdx;

            # Check LM order
            $lmCnt++;
            if ($lmIdx != $lmCnt)
            {
			    print STDERR 'error: invalid LM numerical order' . "\n";
			    exit;
            }

	        if ((my $parm = &param($l, 'name')) ne '!')
	        {
	            $parm =~ s/\"//g;

				# Check SM LM
				if ($lmHandle eq 'LM0')
				{
					if ($parm ne 'SM')
					{
					    print STDERR 'error: invalid LM0 name (must be SM)'
					        . "\n";
					    exit;
					}
				}
				if ($parm eq 'SM')
				{
					if ($lmHandle ne 'LM0')
					{
					    print STDERR 'error: invalid SM LM (must be LM0)'
					        . "\n";
					    exit;
					}
				}
	        }
	        if ((my $parm = &param($l, 'did')) ne '!')
	        {
				# Check SM DID
				if ($lmHandle eq 'LM0')
				{
					if ($parm ne '2')
					{
					    print STDERR 'error: invalid SM/LM0 DID (must be 2)'
					        . "\n";
					    exit;
					}
				}
	        }
	        if ((my $parm = &param($l, 'rpc')) ne '!')
	        {
	            my $rpcType = $parm;

				# Check SM info
				if ($lmHandle eq 'LM0')
				{
					if ($parm ne 'none')
					{
					    print STDERR 'error: invalid SM/LM0 RPC type (must be none)'
					        . "\n";
					    exit;
					}
				}
            }
	    }

        # Handle agent
        if ($l =~ /^SCMI_AGENT(\d*)\b/)
        {
            my $agentIdx = $1;

            # Check agent order
            $agentCnt++;
            if ($agentIdx != $agentCnt)
            {
			    print STDERR 'error: invalid agent numerical order' . "\n";
			    exit;
            }

            # Check LM defined before agent
            if ($lmCnt == -1)
            {
			    print STDERR 'error: agent before lm' . "\n";
			    exit;
            }
        }

		# Handle clock
		if ($l =~ /DEV_SM_CLK_\S*\b/)
		{
			# Check if no clock access
			if ($l =~ /clk=none/)
			{
				next;
			}

	        my @words = split(/ /, $l);
			
		    foreach my $w (@words)
		    {
				if ($w =~ /DEV_SM_(CLK_\S*)\b/)
				{
					my $clkStr = $1;

					if ($lmCnt == 0)
					{
						push @smClocks, $clkStr;
					}
					else
					{
						if (grep { $clkStr eq $_} @smClocks)
						{
							print STDERR 'warning: LM' . $lmCnt
								. ': has access to ' . $clkStr . "\n";
						}
					}
				}
			}
		}
    }
}