aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/remove-boot-splash
blob: f7e7b63ac356911a89cbaa63ee6ef8582e76cd36 (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
#!/usr/bin/perl

@ARGV == 1 or die "usage: remove-splash <initrd>\n";

my $magic = 'BOOTSPL3';
my $buffer_size = 15;

my ($initrd) = @ARGV;

# FIXME it must have a clear way to do that in perl
if (!system("/bin/zcat $initrd 2> /dev/null | /bin/cpio -t &> /dev/null")) {
    print STDERR "remove-boot-splash: initrd in initramfs format\n";
    chomp(my $tmp_dir = `mktemp -d`);
    chdir $tmp_dir;
    system("/bin/zcat $initrd 2>/dev/null | /bin/cpio -id 2>/dev/null");
    if (-f "$tmp_dir/bootsplash") {
	unlink "$tmp_dir/bootsplash" or die "FATAL: removing of $tmp_dir/bootsplash failed";
	print STDERR "remove-boot-splash: removing bootsplash from initrd\n";
	system("/bin/find . -print | /bin/cpio --quiet -c -o 2> /dev/null | gzip -c > $initrd")
    } else {
	print STDERR "ERROR remove-boot-splash: bootsplash image not found in $initrd\n"
    }
    system("rm -rf $tmp_dir");
    exit
}

open(my $F, "+< $initrd") or die "can't open $initrd: $!\n";

while (1) {
    my $got = read($F, my $buffer, $buffer_size);
    $got > length($magic) or last;

    my $index = index($buffer, $magic);
    if ($index >= 0) {
	my $offset = $index + tell($F) - $got;

	seek($F, $offset + 12, 0) && read($F, my $tmp, 4) or last;
	my $splash_size = unpack("V", $tmp) + 38;

	my $initrd_size = seek($F, 0, 2) && tell($F);
	
	if ($offset + $splash_size == $initrd_size) {
	    truncate($F, $offset);
	    exit 0;
	} else {
	    warn "bootsplash found at offset $offset, but it is not at the end ($offset + $splash_size != $initrd_size)\n";
	}
    }
    seek($F, -length($magic), 1); # handle the case BOOTSPL3 when overlaps between buffers
}

warn "bootsplash not found in $initrd\n";