#!/usr/bin/env perl # # srecimage.pl - script to convert a binary image into srec # Copyright (c) 2015 - Jo-Philipp Wich # # This script is in the public domain. use strict; my ($input, $output, $offset) = @ARGV; if (!defined($input) || !-f $input || !defined($output) || !defined($offset) || $offset !~ /^(0x)?[a-fA-F0-9]+$/) { die "Usage: $0 \n"; } sub srec { my ($type, $addr, $data, $len) = @_; my @addrtypes = qw(%04X %04X %06X %08X %08X %04X %06X %08X %06X %04X); my $addrstr = sprintf $addrtypes[$type], $addr; $len = length($data) if ($len <= 0); $len += 1 + (length($addrstr) / 2); my $sum = $len; foreach my $byte (unpack('C*', pack('H*', $addrstr)), unpack('C*', $data)) { $sum += $byte; } return sprintf "S%d%02X%s%s%02X\r\n", $type, $len, $addrstr, uc(unpack('H*', $data)), ~($sum & 0xFF) & 0xFF; } open(IN, '<:raw', $input) || die "Unable to open $input: $!\n"; open(OUT, '>:raw', $output) || die "Unable to open $output: $!\n"; my ($basename) = $output =~ m!([^/]+)$!; print OUT srec(0, 0, $basename, 0); my $off = hex($offset); my $len; while (defined($len = read(IN, my $buf, 16)) && $len > 0) { print OUT srec(3, $off, $buf, $len); $off += $len; } print OUT srec(7, hex($offset), "", 0); close OUT; close IN; ue='openwrt-18.06'>openwrt-18.06 upstream openwrtJames
aboutsummaryrefslogtreecommitdiffstats
blob: 6f10170b3b683889e6f3c7d266e1185a5489650c (plain)
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
#!/usr/bin/env perl

use strict;
use warnings;
use Config;

if (@ARGV < 2) {
	die "Usage: $0 <prefix> <command...>\n";
}

sub gettime {
	my ($sec, $usec);

	eval {
		require Time::HiRes;
		($sec, $usec) = Time::HiRes::gettimeofday();
	};

	unless (defined($sec) && defined($usec)) {
		my $tv_t = ($Config{'longsize'} == 8) ? 'qq' : 'll';
		my $tv = pack $tv_t, 0, 0;

		eval {
			require 'syscall.ph';
			syscall(SYS_gettimeofday(), $tv, 0);
		};

		($sec, $usec) = unpack $tv_t, $tv;
	}

	return ($sec, $usec);
}

my ($prefix, @cmd) = @ARGV;
my ($sec, $usec) = gettime();
my $pid = fork();

if (!defined($pid)) {
	die "$0: Failure to fork(): $!\n";
}
elsif ($pid == 0) {
	exec(@cmd);
	die "$0: Failure to exec(): $!\n";
}
else {
	$SIG{'INT'} = 'IGNORE';
	$SIG{'QUIT'} = 'IGNORE';

	if (waitpid($pid, 0) == -1) {
		die "$0: Failure to waitpid(): $!\n";
	}

	my $exitcode = $? >> 8;
	my ($sec2, $usec2) = gettime();
	my (undef, undef, $cuser, $csystem) = times();

	printf STDOUT "%s#%.2f#%.2f#%.2f\n",
		$prefix, $cuser, $csystem,
		($sec2 - $sec) + ($usec2 - $usec) / 1000000;

	$SIG{'INT'} = 'DEFAULT';
	$SIG{'QUIT'} = 'DEFAULT';

	exit $exitcode;
}