aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/perl/dig.pl
blob: a7f7f08a91e5b7e74b18a2525cefdafac00a1b41 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/perl

# Copyright 2012-2022, BlueFANG 

use strict;
use warnings;
use Carp;
use feature 'say';
use File::Find;
use Getopt::Long;
use Pod::Usage;
use Data::Dumper;
use Cwd;

my $man = 0;
my $help = 0;
my $initem = '';
my @hex_addresses = ();
my $print_consts = '';
my $print_structs = '';
my $print_reg_map = '';
my $print_long = '';
my $reg_idx = 8;
my $verbose = '';
my $searchdir = "$ENV{HOME}/projects/bk_ble/sdk/driver/ble";
my $db_file = '';
my $const_name = '';
my $preamble = <<EOF;
#include "sys_config.h"
#include "param_config.h"
#include "param_config.h"
#include "rwapp_config.h"
#include "rwip_config.h"
#include "rwprf_config.h"
#include "kernel_config.h"
#include "rwble_hl_config.h"
#include "rwble_config.h"

#include "rwip.h"
#include "ble_reg_access.h"
#include "reg_blecore.h"
#include "em_map_ble.h"
#include "reg_ble_em_cs.h"
#include "reg_ble_em_rx_buffer.h"
#include "reg_ble_em_rx_desc.h"
#include "reg_ble_em_tx_buffer_cntl.h"
#include "reg_ble_em_tx_desc.h"
#include "reg_ble_em_tx_buffer_data.h"
#include "ecc_p256.h"
#include "llm.h"
#include "llm_util.h"
#include "lld.h"
#include "lld_evt.h"
#include "lld_pdu.h"
#include "lld_util.h"
#include "RomCallFlash.h"
#include "em_buf.h"
#include "ea.h"
#include "common_utils.h"
#include "common_hci.h"
#include "common_llcp.h"
#include "%s";

#define diag_const(name,format)  __asm__("###CT(" format ") " #name " = %%c0" : :\\
        "i" (__builtin_constant_p(name) ? name : 0x0BADDEED) );

void diagnostic_out( void ) {
EOF


my $postamble = <<EOF;
#if defined(%s)
#define f "m"
#else
#define f "c"
#endif
    diag_const(%s,f);
};
 
EOF
    
#Getopt::Long::Configure ("bundling"); # to allow -abc to set a, b, and c

GetOptions ("help|?" => \$help, 
            man => \$man,
            verbose => \$verbose,
            "regname=s" => sub {
                if ($verbose) {
                    print "Pushing name $_[1]\n";
                }
                push @hex_addresses, hex $_[1];
            },
            "db-file=s" => \$db_file,
            "const=s" => \$const_name,
            "dir=s" => \$searchdir,
    ) or pod2usage(2);

pod2usage(-exitval => 0, -verbose => 1) if $help;
pod2usage(-exitval => 0, -verbose => 2) if $man;

sub get_header {
    my $dir = $File::Find::dir;
    my $file = $_;
    
    if ( $file =~ m/.*\.h/ ) {
        print "Header: $file\n";
        extract_consts($file);
    }
}

$SIG{INT} = 'quit_all';
my %const_hash = ();

sub extract_consts($) {

    my $file = shift;
    
    open HFILE, $file or die "Where did $file go?\n";

    while(<HFILE>) {

        my $line = $_;

        while ( $line =~ m/\b([A-Z_][A-Z_0-9]*)\b/g ) {
            my $cname = $1;
            next if exists $const_hash{$cname};
            $const_hash{$cname} = $file;
            open INFILE, ">$ARGV[0]" or die "Cannot open output file $ARGV[0]\n";
            
            printf INFILE $preamble, $file;
            printf INFILE $postamble, $cname, $cname;
            
            close INFILE;

            my $sysout;
            open my $fh, '>>', "output" or die;
            eval {
                my $cur_dir = cwd;
                chdir "$ENV{HOME}/projects/bk_ble";
                close(STDERR);

                my $out;
                open(STDERR, ">>", \$out) or do { print $fh, "failed to open STDERR ($!)\n"; die };

                $sysout = system("make", "-s", "constant_list");

                chdir $cur_dir;
            };

            if ($sysout == 0) {

                my $gen_assembly = "$ENV{HOME}/projects/bk_ble/build/usr/ble_tst/src/diagnostic1.S";
                open DIAGCONST, $gen_assembly
                    or die "Cannot find .S file $gen_assembly\n";

                while (<DIAGCONST>) {

                    if (/^[^#]*###CT\(([^()]*)\)\s*(.*\S)\s*=\s*([0-9]+)$/) {
                        if ($db_file) {
                            print DBFILE "$2=$3\{$file\}{$1}\n";
                        }
                        print "$2=$3\n";
                    }
                }

                if ($cname eq $const_name) {
                    print "Constant $cname added\n";
                }
                
            } elsif ($sysout & 0xFF) {# died from a signal so probably was meant for us instead
                print "Interrupted\n";
                die;
            } else {
                if ($cname eq $const_name) {
                    print "Constant $cname failed to process\n";
                }
            }
        }

    }

    close HFILE;

}

sub quit_all{ # in case one is actually quick enough
    print "Interrupted\n";
    close DBFILE if $db_file;
    die;
}

if ( $db_file ) {
    open DBFILE, ">$db_file" or die "Cannot open the db file $db_file\n";
}

find(\&get_header, $searchdir);
close DBFILE;