Rev 1881 | Rev 1884 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1667 | runge | 1 | #!/usr/bin/perl -w |
| 2 | ################################################ |
||
| 3 | # |
||
| 4 | # author: arune @ efnet |
||
| 5 | # |
||
| 6 | ################################################ |
||
| 7 | |||
| 8 | use IO::Socket; |
||
| 9 | use integer; |
||
| 10 | use threads; |
||
| 11 | use Cwd qw(abs_path); |
||
| 12 | use File::Basename; |
||
| 13 | use Getopt::Long; |
||
| 14 | |||
| 1882 | arune | 15 | use IO::Select; |
| 16 | use Fcntl ':seek'; |
||
| 17 | |||
| 1667 | runge | 18 | $host = "localhost"; |
| 19 | $port = 1201; |
||
| 20 | |||
| 21 | # Get hostname and port from command line arguments |
||
| 22 | if ( @ARGV > 0 ) { |
||
| 23 | GetOptions( "host=s" => \$host, # |
||
| 24 | "port=i" => \$port, # |
||
| 25 | ); |
||
| 26 | } |
||
| 27 | |||
| 28 | my $path = dirname(abs_path($0)); |
||
| 29 | |||
| 30 | # Read devices from separate file |
||
| 31 | if (-e $path."/devices.pl") { |
||
| 32 | require $path."/devices.pl"; |
||
| 33 | } else { |
||
| 34 | print "Error: No devices.pl was found in ".$path.". \n\n"; |
||
| 35 | exit 1; |
||
| 36 | } |
||
| 37 | |||
| 38 | $biosfile = "bios.inc"; |
||
| 39 | open (FP, "+<".$biosfile) or die "Error: Cannot open file $biosfile\n"; |
||
| 40 | @file = <FP>; |
||
| 41 | close FP; |
||
| 42 | |||
| 43 | |||
| 44 | # Check bios.inc for the settings that should be updated |
||
| 45 | my $foundidrow = 0; |
||
| 46 | my $foundmcurow = 0; |
||
| 47 | my $foundhfrow = 0; |
||
| 48 | my $foundlfrow = 0; |
||
| 49 | my $foundefrow = 0; |
||
| 50 | my $foundbootrow = 0; |
||
| 51 | foreach $file (@file) { |
||
| 52 | if ($file =~ m/^NODE_HW_ID=/) { |
||
| 53 | print "\nCurrent id-setting: ".$file."\n"; |
||
| 54 | $foundidrow = 1; |
||
| 55 | } |
||
| 56 | if ($file =~ m/^MCU=/) { |
||
| 57 | $foundmcurow = 1; |
||
| 58 | } |
||
| 59 | if ($file =~ m/^HIGHFUSE=/) { |
||
| 60 | $foundhfrow = 1; |
||
| 61 | } |
||
| 62 | if ($file =~ m/^LOWFUSE=/) { |
||
| 63 | $foundlfrow = 1; |
||
| 64 | } |
||
| 65 | if ($file =~ m/^EXTFUSE=/) { |
||
| 66 | $foundefrow = 1; |
||
| 67 | } |
||
| 68 | if ($file =~ m/^BOOT_START=/) { |
||
| 69 | $foundbootrow = 1; |
||
| 70 | } |
||
| 71 | } |
||
| 72 | if ($foundidrow == 0) { |
||
| 73 | print "Error: Did not find NODE_HW_ID= in file $biosfile\n"; |
||
| 74 | exit 1; |
||
| 75 | } |
||
| 76 | if ($foundmcurow == 0) { |
||
| 77 | print "Error: Did not find MCU= in file $biosfile\n"; |
||
| 78 | exit 1; |
||
| 79 | } |
||
| 80 | if ($foundhfrow == 0) { |
||
| 81 | print "Error: Did not find HIGHFUSE= in file $biosfile\n"; |
||
| 82 | exit 1; |
||
| 83 | } |
||
| 84 | if ($foundlfrow == 0) { |
||
| 85 | print "Error: Did not find LOWFUSE= in file $biosfile\n"; |
||
| 86 | exit 1; |
||
| 87 | } |
||
| 88 | if ($foundefrow == 0) { |
||
| 89 | print "Error: Did not find EXTFUSE= in file $biosfile\n"; |
||
| 90 | exit 1; |
||
| 91 | } |
||
| 92 | if ($foundbootrow == 0) { |
||
| 93 | print "Error: Did not find BOOT_START= in file $biosfile\n"; |
||
| 94 | exit 1; |
||
| 95 | } |
||
| 96 | |||
| 97 | my $hwid = ""; |
||
| 98 | my @device = (); |
||
| 99 | |||
| 1882 | arune | 100 | #print "/usr/bin/atomic -s $host -p $port -c \"Node_WaitForInformation\"\n"; |
| 1878 | arune | 101 | #my @lines = qx{/usr/bin/atomic -s $host -p $port -c \"Node_GetInformation 0xB23F54EA\"}; |
| 1882 | arune | 102 | #my @lines = qx{/usr/bin/atomic -s $host -p $port -c \"Node_WaitForInformation\"}; |
| 1667 | runge | 103 | |
| 1882 | arune | 104 | $socket = atomd_initialize($host, $port); |
| 105 | atomd_send_command($socket, "Node_WaitForInformation"); |
||
| 106 | $return = atomd_read_command_response($socket); |
||
| 107 | |||
| 108 | my @lines = split(/\n/, $return); |
||
| 109 | |||
| 1667 | runge | 110 | foreach (@lines) |
| 111 | { |
||
| 112 | my $line = $_; |
||
| 1882 | arune | 113 | print($line."\n"); |
| 1667 | runge | 114 | chomp($line); |
| 115 | |||
| 116 | if (index($line, "Id") != -1) |
||
| 117 | { |
||
| 118 | my $temp = substr($line, index($line, ":") + 1); |
||
| 119 | |||
| 120 | $temp =~ s/^\s+//; |
||
| 121 | $temp =~ s/\s+$//; |
||
| 122 | |||
| 123 | my @hwidChars = split(//, $temp); |
||
| 124 | $hwid = $hwidChars[0]. $hwidChars[1]. $hwidChars[8]. $hwidChars[9]. $hwidChars[6]. $hwidChars[7]. $hwidChars[4]. $hwidChars[5]. $hwidChars[2]. $hwidChars[3]; |
||
| 125 | |||
| 126 | print "Setting HWID in bios.inc to ".$hwid."\n"; |
||
| 127 | } |
||
| 1878 | arune | 128 | elsif (index($line, "Device Type") != -1) |
| 1667 | runge | 129 | { |
| 130 | $devicename = substr($line, index($line, ":") + 1); |
||
| 131 | |||
| 132 | $devicename =~ s/^\s+//; |
||
| 133 | $devicename =~ s/\s+$//; |
||
| 134 | |||
| 135 | @device = $devices[0]; |
||
| 136 | foreach (@devices) |
||
| 137 | { |
||
| 1668 | runge | 138 | if (@{$_}[0] eq $devicename) |
| 1667 | runge | 139 | { |
| 140 | @device = @{$_}; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 1668 | runge | 144 | if ($device[0] eq "unknown") |
| 1667 | runge | 145 | { |
| 146 | print "\nWarning: Unknown device type, will not set MCU settings\n\n"; |
||
| 147 | } |
||
| 148 | else |
||
| 149 | { |
||
| 150 | print "Setting device type to ".$device[0]."\n"; |
||
| 151 | } |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | if ($hwid eq "") |
||
| 156 | { |
||
| 157 | exit(1); |
||
| 158 | } |
||
| 159 | |||
| 160 | # Open bios.inc and replace all settings |
||
| 161 | open (FP, "<".$biosfile) or die "Cannot open file $biosfile\n"; |
||
| 162 | @file = <FP>; |
||
| 163 | close FP; |
||
| 164 | open (FP, ">".$biosfile) or die "Cannot open file $biosfile\n"; |
||
| 165 | foreach $file (@file) { |
||
| 166 | $file =~ s/^NODE_HW_ID=.*/NODE_HW_ID=$hwid/g; |
||
| 1668 | runge | 167 | if ($device[0] ne "unknown") |
| 1667 | runge | 168 | { |
| 169 | $file =~ s/^MCU=.*/MCU=$device[0]/g; |
||
| 170 | $file =~ s/^HIGHFUSE=.*/HIGHFUSE=$device[1]/g; |
||
| 171 | $file =~ s/^LOWFUSE=.*/LOWFUSE=$device[2]/g; |
||
| 172 | $file =~ s/^EXTFUSE=.*/EXTFUSE=$device[3]/g; |
||
| 173 | $file =~ s/^BOOT_START=.*/BOOT_START=$device[4]/g; |
||
| 174 | } |
||
| 175 | print FP $file; |
||
| 176 | } |
||
| 177 | close FP; |
||
| 178 | |||
| 1668 | runge | 179 | exit(0); |
| 1667 | runge | 180 | |
| 181 | |||
| 1882 | arune | 182 | # TODO: Move all atomic-protocol functions to a separate file/lib |
| 183 | |||
| 1881 | arune | 184 | sub atomd_connect |
| 185 | { |
||
| 186 | ($host, $port) = @_; |
||
| 1882 | arune | 187 | $socket = IO::Socket::INET->new( |
| 188 | Proto => "tcp", |
||
| 189 | PeerAddr => $host, |
||
| 190 | PeerPort => $port, |
||
| 191 | Blocking => 1, |
||
| 192 | ) |
||
| 193 | or die "Error: Cannot connect to port $port at $host\n"; |
||
| 194 | |||
| 195 | return $socket; |
||
| 1881 | arune | 196 | } |
| 197 | |||
| 198 | sub atomd_read_packet |
||
| 199 | { |
||
| 200 | ($socket) = @_; |
||
| 1882 | arune | 201 | $command=""; |
| 202 | #while (length($command)<4) |
||
| 203 | #{ |
||
| 204 | #sysread blocks and reads at least 1 byte before returning |
||
| 205 | # $bytes = sysread($socket, $buf, 1); |
||
| 206 | # $command .= $buf; |
||
| 207 | #} |
||
| 208 | read($socket, $command, 4); |
||
| 209 | |||
| 210 | $payload_length=""; |
||
| 211 | #while (length($payload_length)<4) |
||
| 212 | #{ |
||
| 213 | # $bytes = sysread($socket, $buf, 1); |
||
| 214 | # $payload_length .= $buf; |
||
| 215 | #} |
||
| 216 | read($socket, $payload_length, 4); |
||
| 217 | |||
| 218 | $payload_length = int($payload_length); |
||
| 219 | $payload=""; |
||
| 220 | if ($payload_length > 0) |
||
| 221 | { |
||
| 222 | #while (length($payload) < $payload_length) |
||
| 223 | #{ |
||
| 224 | # $bytes = sysread($socket, $buf, 1); |
||
| 225 | # $payload .= $buf; |
||
| 226 | #} |
||
| 227 | read($socket, $payload, $payload_length); |
||
| 228 | } |
||
| 229 | |||
| 230 | $payload_length = sprintf("%04d", $payload_length); |
||
| 231 | |||
| 232 | #print "areadpacket ".$command . $payload_length . $payload."\n"; |
||
| 233 | return $command . $payload_length . $payload; |
||
| 1881 | arune | 234 | } |
| 235 | |||
| 236 | sub atomd_write_packet |
||
| 237 | { |
||
| 238 | ($socket, $command, $payload) = @_; |
||
| 239 | |||
| 1882 | arune | 240 | $payload_length = sprintf("%04d", length($payload)+1); |
| 241 | $packet = $command.$payload_length.$payload.chr(0); |
||
| 242 | |||
| 243 | #print "awritepacket ".$packet."\n"; |
||
| 244 | print $socket $packet; |
||
| 1881 | arune | 245 | } |
| 246 | |||
| 247 | |||
| 248 | sub atomd_data_available |
||
| 249 | { |
||
| 250 | ($socket) = @_; |
||
| 1882 | arune | 251 | #Get the current buffer position |
| 252 | $position = tell($socket); |
||
| 253 | |||
| 254 | #Set the current position to end of buffer |
||
| 255 | seek($socket, -1, SEEK_END); |
||
| 256 | |||
| 257 | #Compare end postion to stored position |
||
| 258 | $has_data = tell($socket) > $position; |
||
| 259 | |||
| 260 | #Restore buffer position |
||
| 261 | seek($socket, $position, SEEK_SET); |
||
| 262 | |||
| 263 | return $has_data; |
||
| 1881 | arune | 264 | } |
| 265 | |||
| 266 | |||
| 267 | sub atomd_initialize |
||
| 268 | { |
||
| 269 | ($host, $port) = @_; |
||
| 1882 | arune | 270 | $socket = atomd_connect($host, $port); |
| 1881 | arune | 271 | |
| 1882 | arune | 272 | #while (!atomd_data_available($socket)) {} |
| 273 | |||
| 274 | #while (atomd_data_available($socket)) |
||
| 275 | #{ |
||
| 276 | # $packet = atomd_read_packet($socket); # Read initial prompt |
||
| 277 | #} |
||
| 278 | |||
| 279 | # Read initial prompt |
||
| 280 | $s = IO::Select->new(); |
||
| 281 | $s->add($socket); |
||
| 282 | $s->can_read(1); |
||
| 283 | |||
| 284 | sysread($socket, $data, 9000); |
||
| 285 | $data=""; |
||
| 286 | #print "ainit".$data."\n"; |
||
| 1881 | arune | 287 | return $socket; |
| 288 | } |
||
| 289 | |||
| 290 | |||
| 291 | sub atomd_send_command |
||
| 292 | { |
||
| 293 | ($socket, $command) = @_; |
||
| 294 | atomd_write_packet($socket, "RESP", $command); |
||
| 295 | } |
||
| 296 | |||
| 297 | |||
| 298 | sub atomd_read_command_response |
||
| 299 | { |
||
| 300 | ($socket) = @_; |
||
| 1882 | arune | 301 | |
| 302 | $response = ""; |
||
| 303 | |||
| 304 | while (1) |
||
| 305 | { |
||
| 306 | $packet = atomd_read_packet($socket); |
||
| 307 | |||
| 308 | if (substr($packet, 0, 4) ne "TEXT") |
||
| 309 | { |
||
| 310 | last; |
||
| 311 | } |
||
| 312 | |||
| 313 | $response .= substr($packet, 8) . "\n"; |
||
| 314 | } |
||
| 315 | |||
| 316 | return $response; |
||
| 1881 | arune | 317 | } |
| 318 |