summaryrefslogtreecommitdiff
path: root/usr.sbin/pkg_add/OpenBSD/FwUpdate.pm
blob: 6b8a9d682450ea267ef71625d708db2ec346124e (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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#! /usr/bin/perl

# ex:ts=8 sw=4:
# $OpenBSD: FwUpdate.pm,v 1.3 2015/01/03 17:32:43 espie Exp $
#
# Copyright (c) 2014 Marc Espie <espie@openbsd.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
use strict;
use warnings;
use OpenBSD::PkgAdd;
use OpenBSD::PackageRepository;

package OpenBSD::FwUpdate::State;
our @ISA = qw(OpenBSD::PkgAdd::State);

sub find_path
{
	my $state = shift;
	open my $cmd, '-|', OpenBSD::Paths->sysctl, '-n', 'kern.version';
	my $line = <$cmd>;
	close($cmd);
	if ($line =~ m/^OpenBSD (\d\.\d)(\S*)\s/) {
		my ($version, $tag) = ($1, $2);
		if ($tag ne '-stable') {
			$version = 'snapshots';
		}
		$state->{path} = "http://firmware.openbsd.org/firmware/$version/";
	} else {
		$state->fatal("Couldn't find/parse OS version");
	}
}

sub handle_options
{
	my $state = shift;
	$state->OpenBSD::State::handle_options('adnp:', 
	    '[-adnv] [-D keyword] [-p path] [driver...]');
	$state->{not} = $state->opt('n');
	if ($state->opt('p')) {
		$state->{path} = $state->opt('p');
	} else {
		$state->find_path;
	}
	$main::not = $state->{not};
	$state->{localbase} = OpenBSD::Paths->localbase;
	$state->{destdir} = '';
	$state->{wantntogo} = 0;
	$state->{subst}->add('repair', 1);
	if ($state->opt('a') && @ARGV != 0) {
		$state->usage;
	}
	if ($state->opt('d') && @ARGV == 0) {
		$state->usage("Driver specification required for delete opration");
	}
	$state->{fw_repository} = 
	    OpenBSD::PackageRepository->new($state->{path});
	if ($state->verbose && !$state->opt('d')) {
		$state->say("PKG_PATH=#1", $state->{path});
	}
}

sub finish_init
{
	my $state = shift;
	delete $state->{signer_list}; # XXX uncache value
	$state->{subst}->add('FW_UPDATE', 1);
}

package OpenBSD::FwUpdate::Update;
our @ISA = qw(OpenBSD::Update);

package OpenBSD::FwUpdate;
our @ISA = qw(OpenBSD::PkgAdd);

OpenBSD::Auto::cache(updater,
    sub {
	    require OpenBSD::Update;
	    return OpenBSD::FwUpdate::Update->new;
    });

my %possible_drivers =  map {($_, 1)}
    (qw(acx athn bwi ipw iwi iwn malo otus pgt radeondrm rsu uath
	upgt urtwn uvideo wpi));


sub parse_dmesg
{
	my ($self, $f, $search, $found) = @_;

	while (<$f>) {
		chomp;
		for my $driver (keys %$search) {
			next unless m/^\Q$driver\E\d+\s+at\s/;
			delete $search->{$driver};
			$found->{$driver} = 1;
		}
	}
}

sub find_machine_drivers
{
	my ($self, $state, $h) = @_;
	my %search = %possible_drivers;
	if (open(my $f, '<', '/var/run/dmesg.boot')) {
		$self->parse_dmesg($f, \%search, $h);
		close($f);
	} else {
		$state->errsay("Can't open dmesg.boot: #1", $!);
	}
	if (open(my $cmd, '-|', 'dmesg')) {
		$self->parse_dmesg($cmd, \%search, $h);
		close($cmd);
	} else {
		$state->errsay("Can't run dmesg: #1", $!);
	}
}

sub find_installed_drivers
{
	my ($self, $state, $h) = @_;
	my $inst = $state->repo->installed;
	for my $driver (keys %possible_drivers) {	
		my $search = OpenBSD::Search::Stem->new("$driver-firmware");
		my $l = $inst->match_locations($search);
		if (@$l > 0) {
			$h->{$driver} = OpenBSD::Handle->from_location($l->[0]);
		}
	}
}


sub new_state
{
	my ($self, $cmd) = @_;
	return OpenBSD::FwUpdate::State->new($cmd);
}

sub find_handle
{
	my ($self, $state, $done, $inst, $driver) = @_;
	my $pkgname = "$driver-firmware";
	my $set;
	if ($done->{$driver}) {
		$set = $state->updateset->add_older($done->{$driver});
	} else {
		$set = $state->updateset->add_hints($pkgname);
	}
	$set->add_repositories($state->{fw_repository});
	return $set;
}

sub mark_set_for_deletion
{
	my ($self, $set) = @_;
	# XXX to be simplified. Basically, we pre-do the work of the updater...
	for my $h ($set->older) {
		$h->{update_found} = 1;
	}
	$set->{updates}++;
}

# no way we can find a new quirks on the firmware site
sub do_quirks
{
	my ($self, $state) = @_;
	$self->SUPER::do_quirks($state);
	$state->finish_init;
}

sub process_parameters
{
	my ($self, $state) = @_;

	my $todo = {};
	my $done = {};
	$self->find_machine_drivers($state, $todo);
	$self->find_installed_drivers($state, $done);
	my $inst = $state->repo->installed;

	if (@ARGV == 0) {
		for my $driver (keys %$todo) {
			push(@{$state->{setlist}}, 
			    $self->find_handle($state, $done, $inst, $driver));
		}
	} else {
		for my $driver (@ARGV) {
			my $set = $self->find_handle($state, $done, $inst, 
			    $driver);
			if ($state->opt('d')) {
				if (!$done->{$driver}) {
					$state->errsay("Can't delete uninstalled driver: #1", $driver);
					next;
				}
				$self->mark_set_for_deletion($set);
			} 
			push(@{$state->{setlist}}, $set);
		}
	}
}

1;