mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: "Uwe Kleine-König" <u.kleine-koenig@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH v2] scripts: Provide script that helps using cpp defines in dcd tables
Date: Mon, 24 Jul 2017 10:59:04 +0200	[thread overview]
Message-ID: <20170724085904.1349-1-u.kleine-koenig@pengutronix.de> (raw)

This script was used to help create commit 8f426992c562 ("ARM: imx: use
register defines in imxcfg files instead of plain numbers").

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
---
Changes since v1:

 - explain perl Pod in the file instead of the commit log
 - minor whitespace cleanup

 scripts/regsubst.pl | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 129 insertions(+)
 create mode 100755 scripts/regsubst.pl

diff --git a/scripts/regsubst.pl b/scripts/regsubst.pl
new file mode 100755
index 000000000000..9621a58c47c6
--- /dev/null
+++ b/scripts/regsubst.pl
@@ -0,0 +1,129 @@
+#!/usr/bin/perl
+#
+# This script is documented in Perl Pod, you can pretty-print it using
+#
+# 	$ perldoc scripts/regsubst.pl.
+#
+# The real code starts after "=cut" below.
+
+=head1 NAME
+
+regsubst.pl - helper script to make use of defines
+
+=head1 SYNOPSIS
+
+B<regsubst.pl> [B<-I> I<includepath>] I<filename>
+
+=head1 DESCRIPTION
+
+B<regsubst.pl> parses the given file recursively for #include directives and
+then substitutes raw hex values in I<filename> by definitions found and prints
+the result to stdout.
+
+This is targeted to make i.MX DCD tables more readable but for sure can be used
+elsewhere too.
+
+=head1 BUGS
+
+B<regsubst.pl> is dumb and so might replace values to aggressively. So better
+double check the result.
+
+=head1 OPTIONS
+
+=over 4
+
+=item B<-I> I<includepath>
+
+Add I<includepath> to the list of search paths for include files.
+
+=back
+
+=head1 EXAMPLE
+
+First you have to add the right #include directives to your file:
+
+ $ cat flash-header-myboard.imxcfg
+ soc imx6
+ loadaddr 0x20000000
+ dcdofs 0x400
+ 
+ #include <mach/imx6-ddr-regs.h>
+ #include <mach/imx6dl-ddr-regs.h>
+ 
+ wm 32 0x020e0774 0x000C0000
+ wm 32 0x020e0754 0x00000000
+ ...
+
+Then you can process the file with B<regsubst.pl>:
+
+ $ scripts/regsubst.pl -I arch/arm/mach-imx/include flash-header-myboard.imxcfg
+ soc imx6
+ loadaddr 0x20000000
+ dcdofs 0x400
+ 
+ #include <mach/imx6-ddr-regs.h>
+ #include <mach/imx6dl-ddr-regs.h>
+ 
+ wm 32 MX6_IOM_GRP_DDR_TYPE 0x000C0000
+ wm 32 MX6_IOM_GRP_DDRPKE 0x00000000
+ ...
+
+If the result looks ok, you can replace the file:
+
+ $ scripts/regsubst.pl -I arch/arm/mach-imx/include flash-header-myboard.imxcfg > u
+ $ mov u flash-header-myboard.imxcfg
+
+=cut
+
+use strict;
+use warnings;
+use Getopt::Long qw(GetOptions);
+
+my @includepaths;
+
+GetOptions('I=s' => \@includepaths);
+
+my %regnamemap;
+
+sub scan {
+	my @incfiles;
+
+	push @incfiles, @_;
+
+	foreach my $i (@incfiles) {
+		foreach my $incpath (@includepaths) {
+			if (-e "$incpath/$i") {
+				open(my $fd, "<", "$incpath/$i") || die "Failed to open include file $incpath/$i";
+
+				while (<$fd>) {
+					if (/^\s*#\s*include\s+["<](.*)[">]/) {
+						push @incfiles, $1;
+					};
+
+					if (/^\s*#\s*define\s+([A-Z_0-9]*)\s+(0x[0-9a-f]+)/) {
+						my $regname = $1;
+						my $regaddrre = $2 =~ s/^0x0*/0x0\*/r;
+						$regnamemap{$regaddrre} = $regname;
+					};
+				};
+
+				close($fd);
+
+				last;
+			}
+		}
+	}
+}
+
+while (<>) {
+	my $line = $_;
+
+	if (/#include ["<](.*)[">]/) {
+		scan $1;
+	}
+
+	foreach my $regaddr (keys %regnamemap) {
+		$line =~ s/$regaddr/$regnamemap{$regaddr}/ei;
+	}
+	print $line;
+};
-- 
2.11.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

                 reply	other threads:[~2017-07-24  8:59 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170724085904.1349-1-u.kleine-koenig@pengutronix.de \
    --to=u.kleine-koenig@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox