summaryrefslogtreecommitdiffstats
path: root/perl-install/partition_table_mac.pm
blob: 2fa628acf5976c5df9bffb6ab06d67ae93caece2 (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package partition_table_mac; # $Id$

use diagnostics;
#use strict;   - fixed other PPC code to comply, but program bails on empty partition table - sbenedict
use vars qw(@ISA $freepart_device $bootstrap_part $freepart_start $freepart_size $freepart_part $macos_part);

@ISA = qw(partition_table_raw);

use common;
use partition_table_raw;
use partition_table;
use c;

my %typeToDos = (
  "Apple_partition_map" => 0x401,
  "Apple_Bootstrap"	=> 0x401,
  "Apple_Driver43"	=> 0x401,
  "Apple_Driver_IOKit"	=> 0x401,
  "Apple_Patches"	=> 0x401,
  "Apple_HFS"		=> 0x402,
  "Apple_UNIX_SVR2"	=> 0x83,
  "Apple_Free"		=> 0x0,
);
my %typeFromDos = reverse %typeToDos;


my ($bz_format, $bz_fields) = list2kv(
  n	=> 'bzSig',
  n	=> 'bzBlkSize',
  N	=> 'bzBlkCnt',
  n	=> 'bzDevType',
  n	=> 'bzDevID',
  N	=> 'bzReserved',
  n	=> 'bzDrvrCnt',
);
$bz_format = join '', @$bz_format;


my ($dd_format, $dd_fields) = list2kv(
  N	=> 'ddBlock',
  n	=> 'ddSize',
  n	=> 'ddType',
);
$dd_format = join '', @$dd_format;


my ($p_format, $p_fields) = list2kv(
  n	=> 'pSig',
  n	=> 'pSigPad',
  N	=> 'pMapEntry',
  N	=> 'pPBlockStart',
  N	=> 'pPBlocks',

  a32	=> 'pName',
  a32	=> 'pType',

  N	=> 'pLBlockStart',
  N	=> 'pLBlocks',
  N	=> 'pFlags',
  N	=> 'pBootBlock',
  N	=> 'pBootBytes',

  N	=> 'pAddrs1',
  N	=> 'pAddrs2',
  N	=> 'pAddrs3',
  N	=> 'pAddrs4',
  N	=> 'pChecksum',

  a16	=> 'pProcID',
  a128	=> 'pBootArgs',
  a248	=> 'pReserved',
);
$p_format = join '', @$p_format;

my $magic = 0x4552;
my $pmagic = 0x504D;

sub adjustStart($$) {
    my ($hd, $part) = @_;
    my $end = $part->{start} + $part->{size};
    my $partmap_end = $hd->{primary}{raw}[0]{size};

    if ($part->{start} <= $partmap_end) {
        $part->{start} = $partmap_end + 1;
        $part->{size} = $end - $part->{start};
    }
}

sub adjustEnd($$) {
    my ($hd, $part) = @_;
}

sub read($$) {
    my ($hd, $sector) = @_;
    my $tmp;

    local *F; partition_table_raw::openit($hd, *F) or die "failed to open device";
    c::lseek_sector(fileno(F), $sector, 0) or die "reading of partition in sector $sector failed";

    sysread F, $tmp, psizeof($bz_format) or die "error while reading bz \(Block Zero\) in sector $sector";
    my %info; @info{@$bz_fields} = unpack $bz_format, $tmp;
    my $i;

    foreach $i (0 .. $info{bzDrvrCnt}-1) {
        sysread F, $tmp, psizeof($dd_format) or die "error while reading driver data in sector $sector";
        my %dd; @dd{@$dd_fields} = unpack $dd_format, $tmp;
        push @{$info{ddMap}}, \%dd;
    }

    #- check magic number
    $info{bzSig}  == $magic or die "bad magic number";

    my $numparts;
    c::lseek_sector(fileno(F), $sector, 516) or die "reading of partition in sector $sector failed";
    sysread F, $tmp, 4 or die "error while reading partition info in sector $sector";
    $numparts = unpack "N", $tmp;

    my $partmapsize;
    c::lseek_sector(fileno(F), $sector, 524) or die "reading of partition in sector $sector failed";
    sysread F, $tmp, 4 or die "error while reading partition info in sector $sector";
    $partmapsize = ((unpack "N", $tmp) * $info{bzBlkSize}) / psizeof($p_format);

    c::lseek_sector(fileno(F), $sector, 512) or die "reading of partition in sector $sector failed";

    my @pt;
    for ($i=0;$i<$partmapsize;$i++) {
    	my $part;
        sysread F, $part, psizeof($p_format) or die "error while reading partition info in sector $sector";

        push @pt, map {
            my %h; @h{@$p_fields} = unpack $p_format, $part;
            if ($i < $numparts && $h{pSig} eq $pmagic) {

                $h{size} = ($h{pPBlocks} * $info{bzBlkSize}) / 512;
                $h{start} = ($h{pPBlockStart} * $info{bzBlkSize}) / 512;

                if ($h{pType} =~ /^Apple_UNIX_SVR2/i) {
                    $h{pName} =~ /swap/i ? ($h{type} = 0x82) : ($h{type} = 0x83);
                } elsif ($h{pType} =~ /^Apple_Free/i) {
                	#- need to locate a 1MB partition to setup a bootstrap on
                	if (defined $freepart_start && $freepart_size >= 1) {
                		#- already found a suitable partition
                	} else {
                		$freepart_start = $h{start};
                		$freepart_size = $h{size}/2048;
                		$freepart_device = $hd;
                		$freepart_part = "/dev/" . $hd->{device} . ($i+1);
                		log::l("free apple partition found on drive /dev/$freepart_device->{device}, block $freepart_start, size $freepart_size");
                	}
			$h{type} = 0x0;
			$h{pName} = 'Extra';                    
                } elsif ($h{pType} =~ /^Apple_HFS/i) {
                 	$h{type} = 0x402;
                 	if (defined $macos_part) {		
                 		#- swag at identifying MacOS - 1st HFS partition
                 	} else {	
                 		$macos_part = "/dev/" . $hd->{device} . ($i+1);
                 		log::l("found MacOS at partition $macos_part");
                 	}
                } elsif ($h{pType} =~ /^Apple_Partition_Map/i) {
                 	$h{type} = 0x401;
                 	$h{isMap} = 1;
                } elsif ($h{pType} =~ /^Apple_Bootstrap/i) {
                 	$h{type} = 0x401;
                 	$h{isBoot} = 1;
                 	if (defined $bootstrap_part) {
                 		#found a bootstrap already - use it, but log the find
                 		log::l("found another apple bootstrap at partition /dev/$hd->{device}" . ($i+1));
                 	} else {
                 		$bootstrap_part = "/dev/" . $hd->{device} . ($i+1);
                 		log::l("found apple bootstrap at partition $bootstrap_part");
                 	}
                } else {
                 	$h{type} = 0x401;
                     $h{isDriver} = 1;
                };

                # Let's see if this partition is a driver.
                foreach (@{$info{ddMap}}) {
                    $_->{ddBlock} == $h{pPBlockStart} and $h{isDriver} = 1;
                }

            }
            \%h;
        } [ $part ];
    };

    [ @pt ], \%info;
}

sub write($$$;$) {
    my ($hd, $sector, $pt, $info) = @_;

    #- handle testing for writing partition table on file only!
    local *F;
    if ($::testing) {
	my $file = "/tmp/partition_table_$hd->{device}";
	open F, ">$file" or die "error opening test file $file";
    } else {
	partition_table_raw::openit($hd, *F, 2) or die "error opening device $hd->{device} for writing";
        c::lseek_sector(fileno(F), $sector, 0) or return 0;
    }

    # Find the partition map.
    my @partstowrite;
    my $part = $pt->[0];
    (defined $part->{isMap}) or die "the first partition is not the partition map";
    push @partstowrite, $part;

    # Now go thru the partitions, sort and fill gaps.
    my $last;
    while ($part) {
        $last = $part;
        $part = &partition_table::next($hd, $part);
        $part or last;

        if ($last->{start} + $last->{size} < $part->{start}) {
            #There is a gap between partitions.  Fill it and move on.
            push @partstowrite, {
                type => 0x0,
                start => $last->{start} + $last->{size},
                size => $part->{start} - ($last->{start} + $last->{size}),
            };
        }
        push @partstowrite, $part;
    };

    # now, fill a gap at the end if there is one.
    if ($last->{start} + $last->{size} < $hd->{totalsectors}) {
        push @partstowrite, {
            type => 0x0,
            start => $last->{start} + $last->{size},
            size => $hd->{totalsectors} - ($last->{start} + $last->{size}),
     	};
    }

    # Since we didn't create any new drivers, let's try and match up our driver records with out partitons and see if any are missing.
    $info->{bzDrvrCnt} = 0;
    my @ddstowrite;
    my $dd;
    foreach $dd (@{$info->{ddMap}}) {
        foreach (@partstowrite) {
            if ($dd->{ddBlock} == $_->{pPBlockStart}) {
            	push @ddstowrite, $dd;
            	$info->{bzDrvrCnt}++;
            	last;
            }
        }
    }

    # Now let's write our first block.
    syswrite F, pack($bz_format, @$info{@$bz_fields}), psizeof($bz_format) or return 0;

    # ...and now the driver information.
    foreach (@ddstowrite) {
        syswrite F, pack($dd_format, @$_{@$dd_fields}), psizeof($dd_format) or return 0;
    }
    # zero the rest of the data in the first block.
    foreach ( 1 .. (494 - ((@ddstowrite) * 8))) {
     	syswrite F, "\0", 1 or return 0;
    }
    #c::lseek_sector(fileno(F), $sector, 512) or return 0;
    # Now, we iterate thru the partstowrite and write them.
    foreach (@partstowrite) {
        if (!defined $_->{pSig}) {
            # The values we need to write to disk are not defined.  Let's make them up.
            $_->{pSig} = $pmagic;
            $_->{pSigPad} = 0;
            $_->{pPBlockStart} = ($_->{start} * 512) / $info->{bzBlkSize};
            $_->{pPBlocks} = ($_->{size} * 512) / $info->{bzBlkSize};
            $_->{pLBlockStart} = 0;
            $_->{pLBlocks} = $_->{pPBlocks};
            $_->{pBootBlock} = 0;
            $_->{pBootBytes} = 0;
            $_->{pAddrs1} = 0;
            $_->{pAddrs2} = 0;
            $_->{pAddrs3} = 0;
            $_->{pAddrs4} = 0;
            $_->{pChecksum} = 0;
            $_->{pProcID} = "\0";
            $_->{pBootArgs} = "\0";
            $_->{pReserved} = "\0";

            if ($_->{type} == 0x402) {
                $_->{pType} = "Apple_HFS";
                $_->{pName} = "MacOS";
                $_->{pFlags} = 0x4000037F;
            } elsif ($_->{type} == 0x401 && $_->{start} == 1) {
                $_->{pType} = "Apple_Partition_Map";
                $_->{pName} = "Apple";
                $_->{pFlags} = 0x33;
            } elsif ($_->{type} == 0x401) {
                $_->{pType} = "Apple_Bootstrap";
                $_->{pName} = "bootstrap";
                $_->{pFlags} = 0x33;
		$_->{isBoot} = 1;
		log::l("writing a bootstrap at /dev/$_->{device}");
		$install_steps_interactive::new_bootstrap = 1 if !(defined $partition_table_mac::bootstrap_part);
		$bootstrap_part = "/dev/" . $_->{device};
            } elsif ($_->{type} == 0x82) {
                $_->{pType} = "Apple_UNIX_SVR2";
                $_->{pName} = "swap";
                $_->{pFlags} = 0x33;
            } elsif ($_->{type} == 0x83) {
                $_->{pType} = "Apple_UNIX_SVR2";
                $_->{pName} = "Linux Native";
                $_->{pFlags} = 0x33;
            } elsif ($_->{type} == 0x0) {
                $_->{pType} = "Apple_Free";
                $_->{pName} = "Extra";
                $_->{pFlags} = 0x31;
            }
        };
        $_->{pMapEntry} = @partstowrite;
        syswrite F, pack($p_format, @$_{@$p_fields}), psizeof($p_format) or return 0;
    }

    common::sync();

    1;
}

sub info {
    my ($hd) = @_;

    # - Build the first block of the drive.

    my $info = {
	bzSig => $magic,
	bzBlkSize => 512,
	bzBlkCnt => $hd->{totalsectors},
	bzDevType => 0,
	bzDevID => 0,
	bzReserved => 0,
	bzDrvrCnt => 0,
    };

    $info;
}

sub clear_raw {
    my ($hd) = @_;
    my @oldraw = @{$hd->{primary}{raw}};
    my $pt = { raw => [ ({}) x 63 ], info => info($hd) };

    #- handle special case for partition 1 which is the partition map.
    $pt->{raw}[0] = {
        type => 0x401,
        start => 1,
        size => 63,
        isMap => 1,
    };
#	$pt->{raw}[1] = {
#		type => 0x0,
#		start => 64,
#		size => $hd->{totalsectors} - 64,
#		isMap => 0,
#	};
    push @{$pt->{normal}}, $pt->{raw}[0];
#	push @{$pt->{normal}}, $pt->{raw}[1];

    #- Recover any Apple Drivers, if any.
    my $i = 1;
    foreach (@oldraw) {
        if (defined $_->{isDriver}) {
            $pt->{raw}[$i] = $_;
            push @{$pt->{normal}}, $pt->{raw}[$i];
            $i++;
        }
    };
    @{$pt->{info}{ddMap}} = @{$hd->{primary}{info}{ddMap}};

    $pt;
}

1;
str">"fi-latin1", "fi", 0 ], # there used to be a "fo" layout in XFree86... "fo" => [ N_("_: keyboard\nFaroese"), "is", "is", 0 ], "fr" => [ N_("_: keyboard\nFrench"), "fr-latin1", "fr", 0 ], "ge_ru" => [N_("_: keyboard\nGeorgian (\"Russian\" layout)"), "ge_ru-georgian_academy", "ge_ru",1], "ge_la" => [N_("_: keyboard\nGeorgian (\"Latin\" layout)"), "ge_la-georgian_academy", "ge_la",1], "gr" => [ N_("_: keyboard\nGreek"), "gr-8859_7", "el(extended)", 1 ], "gr_pl" => [ N_("_: keyboard\nGreek (polytonic)"), "gr-8859_7", "el(polytonic)", 1 ], "guj" => [ N_("_: keyboard\nGujarati"), "us", "guj", 1 ], "gur" => [ N_("_: keyboard\nGurmukhi"), "us", "gur", 1 ], "hr" => [ N_("_: keyboard\nCroatian"), "croat", "hr", 0 ], "hu" => [ N_("_: keyboard\nHungarian"), "hu-latin2", "hu", 0 ], "ie" => [ N_("_: keyboard\nIrish"), "uk", "ie", 0 ], "il" => [ N_("_: keyboard\nIsraeli"), "il-8859_8", "il", 1 ], "il_phonetic" => [ N_("_: keyboard\nIsraeli (phonetic)"), "hebrew", "il_phonetic", 1 ], "ir" => [ N_("_: keyboard\nIranian"), "ir-isiri_3342", "ir", 1 ], "is" => [ N_("_: keyboard\nIcelandic"), "is-latin1", "is", 0 ], "it" => [ N_("_: keyboard\nItalian"), "it-latin1", "it", 0 ], "iu" => [ N_("_: keyboard\nInuktitut"), "us", "iu", 1 ], # old XKB layout # Japanese keyboard is dual latin/kana; but telling it here shows a # message to choose the switching key that is misleading, as input methods # are not automatically enabled when typing in kana "jp" => [ N_("_: keyboard\nJapanese 106 keys"), "jp106", "jp", 0 ], "kan" => [ N_("_: keyboard\nKannada"), "us", "kan", 1 ], # There is no XKB korean file yet; but using xmodmap one disables # some functionality; "us" used for XKB until this is fixed "kr" => [ N_("_: keyboard\nKorean"), "us", "us", 1 ], # TODO: console map "ku" => [ N_("_: keyboard\nKurdish (arabic script)"), "us", "ku", 1 ], "ky" => [ N_("_: keyboard\nKyrgyz"), "ky", "ky", 1 ], "la" => [ N_("_: keyboard\nLatin American"), "la-latin1", "la", 0 ], # TODO: console map "lao" => [ N_("_: keyboard\nLaotian"), "us", "lo", 1 ], "lt" => [ N_("_: keyboard\nLithuanian AZERTY (old)"), "lt-latin7", "lt(lt_a)", 0 ], #- TODO: write a console kbd map for lt_new "lt_new" => [ N_("_: keyboard\nLithuanian AZERTY (new)"), "lt-latin7", "lt(lt_std)", 0 ], "lt_b" => [ N_("_: keyboard\nLithuanian \"number row\" QWERTY"), "ltb-latin7", "lt(lt_us)", 1 ], "lt_p" => [ N_("_: keyboard\nLithuanian \"phonetic\" QWERTY"), "ltp-latin7", "lt(phonetic)", 0 ], "lv" => [ N_("_: keyboard\nLatvian"), "lv-latin7", "lv", 0 ], "mal" => [ N_("_: keyboard\nMalayalam"), "us", "ml(mlplusnum)", 1 ], "mk" => [ N_("_: keyboard\nMacedonian"), "mk", "mk", 1 ], "mm" => [ N_("_: keyboard\nMyanmar (Burmese)"), "us", "mm", 1 ], "mng" => [ N_("_: keyboard\nMongolian (cyrillic)"), "us", "mng", 1 ], "mt" => [ N_("_: keyboard\nMaltese (UK)"), "uk", "mt", 0 ], "mt_us" => [ N_("_: keyboard\nMaltese (US)"), "us", "mt_us", 0 ], "nl" => [ N_("_: keyboard\nDutch"), "nl-latin1", "nl", 0 ], "no" => [ N_("_: keyboard\nNorwegian"), "no-latin1", "no", 0 ], "ori" => [ N_("_: keyboard\nOriya"), "us", "ori", 1 ], "pl" => [ N_("_: keyboard\nPolish (qwerty layout)"), "pl", "pl", 0 ], "pl2" => [ N_("_: keyboard\nPolish (qwertz layout)"), "pl-latin2", "pl2", 0 ], # TODO: console map "ps" => [ N_("_: keyboard\nPashto"), "us", "ps", 1 ], "pt" => [ N_("_: keyboard\nPortuguese"), "pt-latin1", "pt", 0 ], # old XKB layout; change "ca_enhanced" -> "ca" once we ship new XKB "qc" => [ N_("_: keyboard\nCanadian (Quebec)"), "qc-latin1", "ca_enhanced", 0 ], #- TODO: write a console kbd map for ro2 "ro2" => [ N_("_: keyboard\nRomanian (qwertz)"), "ro2", "ro", 0 ], "ro" => [ N_("_: keyboard\nRomanian (qwerty)"), "ro", "ro(us_ro)", 0 ], "ru" => [ N_("_: keyboard\nRussian"), "ru4", "ru(winkeys)", 1 ], "ru_yawerty" => [ N_("_: keyboard\nRussian (phonetic)"), "ru-yawerty", "ru(phonetic)", 1 ], "sapmi" => [ N_("_: keyboard\nSaami (norwegian)"), "no-latin1", "sapmi", 0 ], "sapmi_sefi" => [ N_("_: keyboard\nSaami (swedish/finnish)"), "se-latin1", "sapmi(sefi)", 0 ], # TODO: console map "sd" => [ N_("_: keyboard\nSindhi"), "us", "sd", 1 ], "se" => [ N_("_: keyboard\nSwedish"), "se-latin1", "se", 0 ], "si" => [ N_("_: keyboard\nSlovenian"), "slovene", "si", 0 ], # TODO: console map "sin" => [ N_("_: keyboard\nSinhala"), "us", "sin", 1 ], "sk" => [ N_("_: keyboard\nSlovakian (QWERTZ)"), "sk-qwertz", "sk", 0 ], "sk_qwerty" => [ N_("_: keyboard\nSlovakian (QWERTY)"), "sk-qwerty", "sk_qwerty", 0 ], # TODO: console map "sr" => [ N_("_: keyboard\nSerbian (cyrillic)"), "sr", "yu,sr", 1 ], "syr" => [ N_("_: keyboard\nSyriac"), "us", "syr", 1 ], "syr_p" => [ N_("_: keyboard\nSyriac (phonetic)"), "us", "syr_phonetic", 1 ], "tel" => [ N_("_: keyboard\nTelugu"), "us", "tel", 1 ], # no console kbd that I'm aware of "tml" => [ N_("_: keyboard\nTamil (ISCII-layout)"), "us", "tml(INSCRIPT)", 1 ], "tscii" => [ N_("_: keyboard\nTamil (Typewriter-layout)"), "us", "tml(UNI)", 1 ], "th" => [ N_("_: keyboard\nThai (Kedmanee)"), "th", "th", 1 ], "th_tis" => [ N_("_: keyboard\nThai (TIS-820)"), "th", "th_tis", 1 ], # TODO: console map "th_pat" => [ N_("_: keyboard\nThai (Pattachote)"), "us", "th_pat", 1 ], # TODO: console map # NOTE: we define a triple layout here "tifinagh" => [ N_("_: keyboard\nTifinagh (moroccan layout) (+latin/arabic)"), "fr", "fr,tifinagh,ar(azerty)", 1 ], "tifinagh_p" => [ N_("_: keyboard\nTifinagh (phonetic) (+latin/arabic)"), "fr", "fr,tifinagh(phonetic),ar(azerty)", 1 ], # TODO: console map "tj" => [ N_("_: keyboard\nTajik"), "ru4", "tj", 1 ], # TODO: console map "tk" => [ N_("_: keyboard\nTurkmen"), "us", "tk", 0 ], "tr_f" => [ N_("_: keyboard\nTurkish (traditional \"F\" model)"), "trf", "tr(tr_f)", 0 ], "tr_q" => [ N_("_: keyboard\nTurkish (modern \"Q\" model)"), "tr_q-latin5", "tr", 0 ], #-"tw => [ N_("_: keyboard\nChineses bopomofo"), "tw", "tw", 1 ], "ua" => [ N_("_: keyboard\nUkrainian"), "ua", "ua", 1 ], "uk" => [ N_("UK keyboard"), "uk", "gb", 0 ], # TODO: console map "ur" => [ N_("_: keyboard\nUrdu keyboard"), "us", "ur", 1 ], "us" => [ N_("US keyboard"), "us", "en_US", 0 ], "us_intl" => [ N_("US keyboard (international)"), "us-latin1", "us_intl", 0 ], "uz" => [ N_("_: keyboard\nUzbek (cyrillic)"), "uz.uni", "uz", 1 ], # old XKB layout "vn" => [ N_("_: keyboard\nVietnamese \"numeric row\" QWERTY"), "vn-tcvn", "vn(toggle)", 0 ], "yu" => [ N_("_: keyboard\nYugoslavian (latin)"), "sr", "yu", 0 ], ), ); #- list of possible choices for the key combinations to toggle XKB groups #- (eg in X86Config file: XkbOptions "grp:toggle") my %grp_toggles = ( toggle => N_("Right Alt key"), shift_toggle => N_("Both Shift keys simultaneously"), ctrl_shift_toggle => N_("Control and Shift keys simultaneously"), caps_toggle => N_("CapsLock key"), shift_caps_toggle => N_("Shift and CapsLock keys simultaneously"), ctrl_alt_toggle => N_("Ctrl and Alt keys simultaneously"), alt_shift_toggle => N_("Alt and Shift keys simultaneously"), menu_toggle => N_("\"Menu\" key"), lwin_toggle => N_("Left \"Windows\" key"), rwin_toggle => N_("Right \"Windows\" key"), ctrls_toggle => N_("Both Control keys simultaneously"), alts_toggle => N_("Both Alt keys simultaneously"), lshift_toggle => N_("Left Shift key"), rshift_toggle => N_("Right Shift key"), lalt_toggle => N_("Left Alt key"), lctrl_toggle => N_("Left Control key"), rctrl_toggle => N_("Right Control key"), ); #-###################################################################################### #- Functions #-###################################################################################### sub KEYBOARDs() { keys %keyboards } sub KEYBOARD2text { $keyboards{$_[0]} && $keyboards{$_[0]}[0] } sub keyboards() { map { { KEYBOARD => $_ } } keys %keyboards } sub keyboard2one { my ($keyboard, $nb) = @_; ref $keyboard or (is_xbox() ? return undef : internal_error()); my $l = $keyboards{$keyboard->{KEYBOARD}} or return; $l->[$nb]; } sub keyboard2text { keyboard2one($_[0], 0) } sub keyboard2kmap { keyboard2one($_[0], 1) } sub keyboard2xkb { keyboard2one($_[0], 2) } sub xkb_models() { my $models = parse_xkb_rules()->{model}; [ map { $_->[0] } @$models ], { map { @$_ } @$models }; } sub grp_toggles { my ($keyboard) = @_; keyboard2one($keyboard, 3) or return; \%grp_toggles; } sub group_toggle_choose { my ($in, $keyboard) = @_; if (my $grp_toggles = keyboard::grp_toggles($keyboard)) { my $GRP_TOGGLE = $keyboard->{GRP_TOGGLE} || 'caps_toggle'; $GRP_TOGGLE = $in->ask_from_listf('', N("Here you can choose the key or key combination that will allow switching between the different keyboard layouts (eg: latin and non latin)"), sub { translate($grp_toggles->{$_[0]}) }, [ sort keys %$grp_toggles ], $GRP_TOGGLE) or return; $GRP_TOGGLE ne 'rctrl_toggle' and $in->ask_warn(N("Warning"), formatAlaTeX( N("This setting will be activated after the installation. During installation, you will need to use the Right Control key to switch between the different keyboard layouts."))); log::l("GRP_TOGGLE: $GRP_TOGGLE"); $keyboard->{GRP_TOGGLE} = $GRP_TOGGLE; } else { $keyboard->{GRP_TOGGLE} = ''; } 1; } sub loadkeys_files { my ($err) = @_; my $archkbd = arch() =~ /^sparc/ ? "sun" : arch() =~ /i.86/ ? "i386" : arch() =~ /ppc/ ? "mac" : arch(); my $p = "/usr/lib/kbd/keymaps/$archkbd"; my $post = ".kmap.gz"; my %trans = ("cz-latin2" => "cz-lat2"); my %find_file; foreach my $dir (all($p)) { $find_file{$dir} = ''; foreach (all("$p/$dir")) { $find_file{$_} and $err->("file $_ is both in $find_file{$_} and $dir") if $err; $find_file{$_} = "$p/$dir/$_"; } } my (@l, %l); foreach (values %keyboards) { local $_ = $trans{$_->[1]} || $_->[1]; my $l = $find_file{"$_$post"} || $find_file{first(/(..)/) . $post}; if ($l) { push @l, $l; foreach (`zgrep include $l | grep "^include"`) { /include\s+"(.*)"/ or die "bad line $_"; @l{grep { -e $_ } ("$p/$1.inc.gz")} = (); } } else { $err->("invalid loadkeys keytable $_") if $err; } } uniq(@l, keys %l, grep { -e $_ } map { "$p/$_.inc.gz" } qw(compose euro windowkeys linux-keys-bare)); } sub unpack_keyboards { my ($k) = @_; $k or return; [ grep { my $b = $keyboards{$_->[0]}; $b or log::l("bad keyboard $_->[0] in %keyboard::lang2keyboard"); $b; } map { [ split ':' ] } split ' ', $k ]; } sub lang2keyboards { my @li = sort { $b->[1] <=> $a->[1] } map { @$_ } map { my $h = lang::analyse_locale_name($_); #- example: pt_BR and pt my @l = (if_($h->{country}, $h->{main} . '_' . $h->{country}), $h->{main}); my $k = find { $_ } map { $lang2keyboard{$_} } @l; unpack_keyboards($k) || [ [ ($keyboards{$_} ? $_ : "us") => 100 ] ]; } @_; \@li; } sub lang2keyboard { my ($l) = @_; my $kb = lang2keyboards($l)->[0][0]; { KEYBOARD => $keyboards{$kb} ? $kb : 'us' }; #- handle incorrect keyboard mapping to us. } sub default { my ($o_locale) = @_; my $keyboard = from_usb() || lang2keyboard(($o_locale || lang::read())->{lang}); add2hash($keyboard, from_DMI()); $keyboard; } sub from_usb() { return if $::noauto; my ($usb_kbd) = detect_devices::usbKeyboards() or return; my $country_code = detect_devices::usbKeyboard2country_code($usb_kbd) or return; my $keyboard = $usb2keyboard[$country_code]; $keyboard !~ /SKIP/ && { KEYBOARD => $keyboard }; } sub from_DMI() { my $XkbModel = detect_devices::probe_unique_name('XkbModel'); $XkbModel && { XkbModel => $XkbModel }; } sub load { my ($keymap) = @_; return if $::testing; my ($magic, $tables_given, @tables) = common::unpack_with_refs('I' . 'i' . c::MAX_NR_KEYMAPS() . 's' . c::NR_KEYS() . '*', $keymap); $magic != $KMAP_MAGIC and die "failed to read kmap magic"; sysopen(my $F, "/dev/console", 2) or die "failed to open /dev/console: $!"; my $i_tables = 0; each_index { my $table_index = $::i; if (!$_) { #- deallocate table ioctl($F, c::KDSKBENT(), pack("CCS", $table_index, 0, c::K_NOSUCHMAP())) or log::l("removing table $table_index failed: $!"); } else { each_index { ioctl($F, c::KDSKBENT(), pack("CCS", $table_index, $::i, $_)) or log::l("keymap ioctl failed ($table_index $::i $_): $!"); } @{$tables[$i_tables++]}; } } @$tables_given; } sub parse_xkb_rules() { my $cat; my %l; my $lst_file = "$::prefix/usr/X11R6/lib/X11/xkb/rules/xorg.lst"; foreach (cat_($lst_file)) { next if m!^\s*//! || m!^\s*$!; chomp; if (/^!\s*(\S+)$/) { $cat = $1; } elsif (/^\s*(\w\S*)\s+(.*)/) { push @{$l{$cat}}, [ $1, $2 ]; } else { log::l("parse_xkb_rules:$lst_file: bad line $_"); } } \%l; } sub keyboard2full_xkb { my ($keyboard) = @_; my $XkbLayout = keyboard2xkb($keyboard) or return { XkbDisable => '' }; my $XkbModel = $keyboard->{XkbModel} || (arch() =~ /sparc/ ? 'sun' : $XkbLayout eq 'jp' ? 'jp106' : $XkbLayout eq 'br' ? 'abnt2' : 'pc105'); { XkbLayout => $XkbLayout =~ /,/ ? $XkbLayout : join(',', if_($keyboard->{GRP_TOGGLE}, 'us'), $XkbLayout), XkbModel => $XkbModel, XkbOptions => join(',', if_($keyboard->{GRP_TOGGLE}, "grp:$keyboard->{GRP_TOGGLE}", 'grp_led:scroll'), if_($keyboard->{GRP_TOGGLE} ne 'rwin_toggle', 'compose:rwin'), ), }; } sub xmodmap_file { my ($keyboard) = @_; my $KEYBOARD = $keyboard->{KEYBOARD}; my $f = "$ENV{SHARE_PATH}/xmodmap/xmodmap.$KEYBOARD"; if (! -e $f) { eval { require packdrake; my $packer = new packdrake("$ENV{SHARE_PATH}/xmodmap.cz2", quiet => 1); $packer->extract_archive("/tmp", "xmodmap.$KEYBOARD"); }; $f = "/tmp/xmodmap.$KEYBOARD"; } -e $f && $f; } sub setxkbmap { my ($keyboard) = @_; my $xkb = keyboard::keyboard2full_xkb($keyboard) or return; run_program::run('setxkbmap', '-option', '') if $xkb->{XkbOptions}; #- need re-initialised other toggles are cumulated run_program::run('setxkbmap', $xkb->{XkbLayout}, '-model' => $xkb->{XkbModel}, '-option' => $xkb->{XkbOptions} || '', '-compat' => $xkb->{XkbCompat} || ''); } sub setup { my ($keyboard) = @_; return if arch() =~ /^sparc/; #- Xpmac does not map keys quite right if (arch() =~ /ppc/ && !$::testing && $ENV{DISPLAY}) { log::l("Fixing Mac keyboard"); run_program::run('xmodmap', "-e", "keycode 59 = BackSpace"); run_program::run('xmodmap', "-e", "keycode 131 = Shift_R"); run_program::run('xmodmap', "-e", "add shift = Shift_R"); return; } my $kmap = keyboard2kmap($keyboard) or return; log::l("loading keymap $kmap"); if (-e (my $f = "$ENV{SHARE_PATH}/keymaps/$kmap.bkmap")) { load(scalar cat_($f)); } elsif (-e (my $packed_kmaps = "$ENV{SHARE_PATH}/keymaps.cz2")) { my $kid = bg_command->new(sub { eval { require packdrake; my $packer = new packdrake($packed_kmaps, quiet => 1); $packer->extract_archive(undef, "$kmap.bkmap"); }; }); local $/ = undef; eval { my $fd = $kid->{fd}; load(join('', <$fd>)) }; } elsif (-x '/bin/loadkeys') { run_program::run('loadkeys', $kmap); } else { log::l("ERROR: can not load keymap"); } if (-x "/usr/X11R6/bin/setxkbmap") { setxkbmap($keyboard); } else { my $f = xmodmap_file($keyboard); eval { run_program::run('xmodmap', $f) } if $f && !$::testing && $ENV{DISPLAY}; } } sub write { my ($keyboard) = @_; log::l("keyboard::write $keyboard->{KEYBOARD}"); $keyboard = { %$keyboard }; delete $keyboard->{unsafe}; $keyboard->{KEYTABLE} = keyboard2kmap($keyboard); setVarsInSh("$::prefix/etc/sysconfig/keyboard", $keyboard); if (arch() =~ /ppc/) { my $s = "dev.mac_hid.keyboard_sends_linux_keycodes = 1\n"; substInFile { $_ = '' if /^\Qdev.mac_hid.keyboard_sends_linux_keycodes/; $_ .= $s if eof; } "$::prefix/etc/sysctl.conf"; } else { run_program::rooted($::prefix, 'dumpkeys', '>', '/etc/sysconfig/console/default.kmap') or log::l("dumpkeys failed"); } } sub read() { my %keyboard = getVarsFromSh("$::prefix/etc/sysconfig/keyboard") or return; if (!$keyboard{KEYBOARD}) { add2hash(\%keyboard, grep { keyboard2kmap($_) eq $keyboard{KEYTABLE} } keyboards()); } $keyboard{DISABLE_WINDOWS_KEY} = bool2yesno(detect_devices::isLaptop()); keyboard2text(\%keyboard) ? \%keyboard : {}; } sub check() { $^W = 0; my $not_ok = 0; my $warn = sub { print STDERR "$_[0]\n"; }; my $err = sub { &$warn; $not_ok = 1; }; if (my @l = grep { is_empty_array_ref(lang2keyboards($_)) } lang::list_langs()) { $warn->("no keyboard for langs " . join(" ", @l)); } foreach my $lang (lang::list_langs()) { my $l = lang2keyboards($lang); foreach (@$l) { 0 <= $_->[1] && $_->[1] <= 100 or $err->("invalid value $_->[1] in $lang2keyboard{$lang} for $lang in \%lang2keyboard keyboard.pm"); $keyboards{$_->[0]} or $err->("invalid keyboard $_->[0] in $lang2keyboard{$lang} for $lang in \%lang2keyboard keyboard.pm"); } } /SKIP/ || $keyboards{$_} or $err->("invalid keyboard $_ in \@usb2keyboard keyboard.pm") foreach @usb2keyboard; $usb2keyboard[0x21] eq 'us_SKIP' or $err->('@usb2keyboard is badly modified, 0x21 is not us keyboard'); my @xkb_groups = map { if_(/grp:(\S+)/, $1) } cat_('/usr/lib/X11/xkb/rules/xfree86.lst'); $err->("invalid xkb group toggle '$_' in \%grp_toggles") foreach difference2([ keys %grp_toggles ], \@xkb_groups); $warn->("unused xkb group toggle '$_'") foreach grep { !/switch/ } difference2(\@xkb_groups, [ keys %grp_toggles ]); my @xkb_layouts = (#- (map { (split)[0] } grep { /^! layout/ .. /^\s*$/ } cat_('/usr/lib/X11/xkb/rules/xfree86.lst')), all('/usr/lib/X11/xkb/symbols'), (map { (split)[2] } cat_('/usr/lib/X11/xkb/symbols.dir'))); $err->("invalid xkb layout $_") foreach difference2([ map { keyboard2xkb($_) } keyboards() ], \@xkb_layouts); my @kmaps_available = map { if_(m|.*/(.*)\.bkmap|, $1) } `tar tfj share/keymaps.tar.bz2`; my @kmaps_wanted = map { keyboard2kmap($_) } keyboards(); $err->("missing KEYTABLE $_ (either share/keymaps.tar.bz2 need updating or $_ is bad)") foreach difference2(\@kmaps_wanted, \@kmaps_available); $err->("unused KEYTABLE $_ (update share/keymaps.tar.bz2 using share/keymaps_generate)") foreach difference2(\@kmaps_available, \@kmaps_wanted); loadkeys_files($err); exit($not_ok); } 1;