Script for recovering CIS

Pavel Roskin proski at gnu.org
Thu Nov 6 20:46:08 GMT 2003


Hello!

dump_cis doesn't have an option to dump the CIS in the binary format.
I've written a script that takes the output of "dump_cis -v" and converts
it a a binary suitable for replacing CIS using "cis" keyword in
/etc/pcmcia/config.

Unlike pack_cis, which loses important information, such as lan_node_id
(i.e. the MAC address for network cards), cis2bin doesn't lose anything.

cis2bin understands the "Socket" keyword and creates separate output files
for different sockets.  cis2bin adds the end tuple if it's missing from
the dump_cis output.

cis2bin allows to add or remove tuples without having to adjust the
offsets.  It just warns about incorrect offsets and ignores them.

This script is just a quick hack, but I want to post it anyway because it
may be useful for somebody.  In the long term, I believe we need
following:

1) CIS exported as is (even if it's inconsistent) via procfs or sysfs
2) dump_cis that can parse CIS from files without root permissions
3) pack_cis that doesn't lose information

Sorry, my lex and yacc skills are not so good, but I think pack_cis could
be rewritten in Perl or another high-level language to be more extensible
and easy to hack.

-- 
Regards,
Pavel Roskin
-------------- next part --------------
#! /usr/bin/perl -w

# Convert output of "dump_cis -v" to a binary.

use strict;

my $output_open = 0;
my $tuple = 0;

if ($#ARGV != 1) {
	print STDERR "Usage: cis2bin infile outfile\n" .
		     "Socket number is appended to outfile if found\n";
	exit 1;
}

my $infile = $ARGV[0];
my $outfile = $ARGV[1];

open (INFILE, "< $infile") || die "Cannot open $infile: $!";

while (<INFILE>) {
	if ( m{^Socket\s+([0-9]+)} ) {
		close (OUTFILE) if $output_open;
		open (OUTFILE, "> ${outfile}-$1") ||
			die "Cannot open ${outfile}-$1: $!";
		$output_open = 1;
	}
	if ( m{^\s*offset (0x[0-9a-f]+), tuple (0x[0-9a-f]+), link (0x[0-9a-f]+)} ) {
		my $offset = hex($1);
		$tuple = hex($2);
		my $link = hex($3);
		if (!$output_open) {
			# No socket number - use exact filename
			open (OUTFILE, "> ${outfile}") ||
				die "Cannot open ${outfile}: $!";
			$output_open = 1;
		}
		printf OUTFILE "%c%c", $tuple, $link;

		# Sanity check
		my $real_offset = tell OUTFILE;
		if ($offset != $real_offset) {
			printf STDERR "WARNING: Offset should be 0x%02x," .
				" not 0x%02x\n", $real_offset, $offset;
		}

		while ($link > 0) {
			$_ = <INFILE>;
			my @bytes = split;
			$link = $link - $#bytes - 1;
			foreach (@bytes) {
				printf OUTFILE "%c", hex($_);
			}
		}
	}
}

if ($output_open) {
	# Write end tuple if it wasn't the last tuple
	if ($tuple != 0xff) {
		printf OUTFILE "%c%c", 0xff, 0x00;
	}
	close (OUTFILE);
}

close (INFILE);


More information about the linux-pcmcia mailing list