package partition_table_sun; use diagnostics; use strict; use vars qw(@ISA); @ISA = qw(partition_table_raw); use common qw(:common :system :file :functional); use partition_table_raw; use partition_table; use c; #- very bad and rough handling :( my %typeToDos = ( 5 => 0, ); my %typeFromDos = reverse %typeToDos; my ($main_format, $main_fields) = list2kv( a128 => 'info', a14 => 'spare0', a32 => 'infos', a246 => 'spare1', n => 'rspeed', n => 'pcylcount', n => 'sparecyl', a4 => 'spare2', n => 'ilfact', n => 'ncyl', n => 'nacyl', n => 'ntrks', n => 'nsect', a4 => 'spare3', a64 => 'partitions', n => 'magic', n => 'csum', ); $main_format = join '', @$main_format; my ($fields1, $fields2) = ([ qw(type flags) ], [ qw(start_cylinder size) ]); my ($format1, $format2) = ("x C x C", "N N"); my ($size1, $size2) = map { psizeof($_) } ($format1, $format2); my $magic = 0xDABE; my $nb_primary = 8; my $offset = 0; sub adjustStart($$) { my ($hd, $part) = @_; my $end = $part->{start} + $part->{size}; #- since partition must always start on cylinders boundaries on sparc, #- note that if start sector is on the first cylinder, it is adjusted #- to 0 and it is valid, maybe not in fact, problem with Sun disk label. #- IT COULD HURT IF STARTING AT 0, MAYBE THERE ARE SOME FLAGS FOR EXT2 #- TO AVOID WRITING ANYTHING ON THE FIRST 1024 BYTES OF DISK, ELSE PROM #- MAY ERASE EVERYTHING... TO BE CHECKED LATER. #- so now, starting on cylinder 1 is perfect altough it waste some disk space ! $part->{start} = round_down($part->{start}, $hd->cylinder_size()); $part->{start} = $hd->cylinder_size() if $part->{start} == 0; $part->{size} = $end - $part->{start}; $part->{size} = $hd->cylinder_size() if $part->{size} <= 0; } sub adjustEnd($$) { my ($hd, $part) = @_; my $end = $part->{start} + $part->{size}; my $end2 = round_up($end, $hd->cylinder_size()); $end2 = $hd->{geom}{cylinders} * $hd->cylinder_size() if $end2 > $hd->{geom}{cylinders} * $hd->cylinder_size(); $part->{size} = $end2 - $part->{start}; } #- compute crc checksum used for Sun Label partition, expect #- $tmp to be the 512 bytes buffer to be read/written to MBR. sub compute_crc($) { my ($tmp) = @_; my @l2b = unpack "n256", $tmp; my $crc = 0; map { $crc ^= $_ } @l2b; $crc; } 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, $offset) or die "reading of partition in sector $sector failed"; sysread F, $tmp, psizeof($main_format) or die "error while reading partition table in sector $sector"; my %info; @info{@$main_fields} = unpack $main_format, $tmp; #- check magic number $info{magic} == $magic or die "bad magic number"; #- check crc, csum contains the crc so result should be 0. compute_crc($tmp) == 0 or die "bad checksum"; @{$hd->{geom}}{qw(cylinders heads sectors)} = @info{qw(ncyl nsect ntrks)}; my @pt = mapn { my %h; @h{@$fields1} = unpack $format1, $_[0]; @h{@$fields2} = unpack $format2, $_[1]; $h{start} = $sector + $h{start_cylinder} * $hd->cylinder_size(); # $h{type} = $typeToDos{$h{type}} || $h{type}; #- for rewrite it ? $h{size} or $h{$_} = 0 foreach keys %h; \%h; } [ $info{infos} =~ /(.{$size1})/g ], [ $info{partitions} =~ /(.{$size2})/g ]; [ @pt ], \%info; } # write the partition table (and extended ones) # for each entry, it uses fields: start, size, type, active sub write($$$;$) { my ($hd, $sector, $pt, $info) = @_; # my ($csize, $wdsize) = (0, 0); #- 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, $offset) or return 0; } ($info->{infos}, $info->{partitions}) = map { join '', @$_ } list2kv map { $_->{start} % $hd->cylinder_size() == 0 or die "partition not at beginning of cylinder"; # $csize += $_->{size} if $_->{type} != 5; # $wdsize += $_->{size} if $_->{type} == 5; $_->{flags} |= 0x10 if $_->{mntpoint} eq '/'; $_->{flags} |= 0x01 if partition_table::isSwap($_); # local $_->{type} = $typeFromDos{$_->{type}} || $_->{type}; local $_->{start_cylinder} = $_->{start} / $hd->cylinder_size() - $sector; pack($format1, @$_{@$fields1}), pack($format2, @$_{@$fields2}); } @$pt; # $csize == $wdsize or die "partitions are not using whole disk space"; #- compute the checksum by building the buffer to write and call compute_crc. #- set csum to 0 so compute_crc will give the right csum value. $info->{csum} = 0; $info->{csum} = compute_crc(pack($main_format, @$info{@$main_fields})); syswrite F, pack($main_format, @$info{@$main_fields}), psizeof($main_format) or return 0; sync(); 1; } sub info { my ($hd) = @_; #- take care of reduction of the number of cylinders, avoid loop of reduction! unless ($hd->{geom}{totalcylinders} > $hd->{geom}{cylinders}) { $hd->{geom}{totalcylinders} = $hd->{geom}{cylinders}; $hd->{geom}{cylinders} -= 2; #- rebuild some constants according to number of cylinders. $hd->{totalsectors} = $hd->{geom}{heads} * $hd->{geom}{sectors} * $hd->{geom}{cylinders}; } #- build a default suitable partition table, #- checksum will be built when writing on disk. #- note third partition is ALWAYS of type Whole disk. my $info = { info => "DiskDrake partition table", rspeed => 5400, pcylcount => $hd->{geom}{totalcylinders}, sparecyl => 0, ilfact => 1, ncyl => $hd->{geom}{cylinders}, nacyl => $hd->{geom}{totalcylinders} - $hd->{geom}{cylinders}, ntrks => $hd->{geom}{heads}, nsect => $hd->{geom}{sectors}, magic => $magic, }; $info; } sub clear_raw { my ($hd) = @_; my $pt = { raw => [ ({}) x $nb_primary ], info => info($hd) }; #- handle special case for partition 2 which is whole disk. $pt->{raw}[2] = { type => 5, #- the whole disk type. flags => 0, start_cylinder => 0, size => $hd->{geom}{cylinders} * $hd->cylinder_size(), }; $pt; } 1; 3b9722f1b20f9384da28ee51b6c130e19e1a5bb'>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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
package install::steps_gtk; # $Id: steps_gtk.pm 266069 2010-02-09 19:47:35Z pterjan $

use diagnostics;
use strict;
use feature 'state';
use vars qw(@ISA);

@ISA = qw(install::steps_interactive interactive::gtk);

#-######################################################################################
#- misc imports
#-######################################################################################
use install::pkgs;
use install::steps_interactive;
use interactive::gtk;
use xf86misc::main;
use common;
use mygtk2;
use ugtk2 qw(:helpers :wrappers :create);
use devices;
use modules;
use install::gtk;
use install::any;
use mouse;
use install::help::help;
use log;

#-######################################################################################
#- In/Out Steps Functions
#-######################################################################################
sub new($$) {
    my ($type, $o) = @_;

    $ENV{DISPLAY} ||= $o->{display} || ":0";
    my $wanted_DISPLAY = $::testing && -x '/usr/bin/Xnest' ? ':9' : $ENV{DISPLAY};

    if (!$::local_install && 
	($::testing ? $ENV{DISPLAY} ne $wanted_DISPLAY : $ENV{DISPLAY} =~ /^:\d/)) { #- is the display local or distant?
        _setup_and_start_X($o, $wanted_DISPLAY) or return;
    }

    $ENV{DISPLAY} = $wanted_DISPLAY;
    require detect_devices;
    if (detect_devices::is_xbox()) {
        modules::load('xpad');
        run_program::run('xset', 'm', '1/8', '1');
    }
    any::disable_x_screensaver();
    run_program::raw({ detach => 1 }, 'drakx-matchbox-window-manager');
    install::gtk::init_gtk($o);
    install::gtk::init_sizes($o);
    install::gtk::install_theme($o);
    install::gtk::create_steps_window($o);
    _may_configure_framebuffer_640x480($o);

    $ugtk2::grab = 1;

    $o = (bless {}, ref($type) || $type)->SUPER::new($o);
    $o->interactive::gtk::new;
    $o;
}

sub _setup_and_start_X {
    my ($o, $wanted_DISPLAY) = @_;
    my $f = "/tmp/Xconf";

    #- /tmp is mostly tmpfs, but not fully, since it doesn't allow: mount --bind /tmp/.X11-unix /mnt/tmp/.X11-unix
    mkdir '/tmp/.X11-unix';
    run_program::run('mount', '-t', 'tmpfs', 'none', '/tmp/.X11-unix');


    my @servers = qw(Driver:fbdev Driver:vesa); #-)
    if ($::testing) {
        @servers = 'Xnest';
    } elsif (arch() =~ /ia64/) {
        require Xconfig::card;
        my ($card) = Xconfig::card::probe();
        @servers = map { if_($_, "Driver:$_") } $card && $card->{Driver}, 'fbdev';
    } elsif (arch() =~ /i.86/) {
        require Xconfig::card;
        my ($card) = Xconfig::card::probe();
        if ($card && $card->{card_name} eq 'i810') {
            # early i810 do not support VESA:
            log::l("graphical installer not supported on early i810");
            undef @servers;
        }
    }

    foreach (@servers) {
        log::l("Trying with server $_");
        my ($prog, $Driver) = /Driver:(.*)/ ? ('Xorg', $1) : /Xsun|Xnest|^X_move$/ ? $_ : "XF86_$_";
        if (/FB/i) {
            !$o->{vga16} && $o->{allowFB} or next;

            $o->{allowFB} = _launchX($o, $f, $prog, $Driver, $wanted_DISPLAY) #- keep in mind FB is used.
              and return 1;
        } else {
            $o->{vga16} = 1 if /VGA16/;
            _launchX($o, $f, $prog, $Driver, $wanted_DISPLAY) and return 1;
        }
    }
    return undef;
}

sub _launchX {
    my ($o, $f, $server, $Driver, $wanted_DISPLAY) = @_;

    mkdir '/var/log' if !-d '/var/log';

    my @options = $wanted_DISPLAY;
    if ($server eq 'Xnest') {
        push @options, '-ac', '-geometry', $o->{vga} || ($o->{vga16} ? '640x480' : '800x600');
    } else {
        install::gtk::createXconf($f, @{$o->{mouse}}{'Protocol', 'device'}, $o->{mouse}{wacom}[0], $Driver);

        push @options, '-allowMouseOpenFail', '-xf86config', $f if arch() !~ /^sparc/;
        push @options, 'vt7', '-dpi', '75';
        push @options, '-nolisten', 'tcp';

        #- old weird servers: Xsun
        push @options, '-fp', '/usr/share/fonts:unscaled' if $server =~ /Xsun/;
    }

    if (!fork()) {
        c::setsid();
        exec $server, @options or c::_exit(1);
    }

    #- wait for the server to start
    foreach (1..5) {
        sleep 1;
        last if fuzzy_pidofs(qr/\b$server\b/);
        log::l("$server still not running, trying again");
    }
    my $nb;
    my $start_time = time();
    foreach (1..60) {
        log::l("waiting for the server to start ($_ $nb)");
        log::l("Server died"), return 0 if !fuzzy_pidofs(qr/\b$server\b/);
        $nb++ if xf86misc::main::Xtest($wanted_DISPLAY);
        if ($nb > 2) {         #- one succeeded test is not enough :-(
            log::l("AFAIK X server is up");
            return 1;
        }
        time() - $start_time < 60 or last;
        time() - $start_time > 8 and print N("Xorg server is slow to start. Please wait..."), "\n";
        sleep 1;
    }
    log::l("Timeout!!");
    0;
}

#- if we success to start X in 640x480 using driver "vesa", 
#- we configure to use fb on installed system (to ensure splashy works)
#- (useful on 800x480 netbooks)
sub _may_configure_framebuffer_640x480 {
    my ($o) = @_;

    if ($::rootwidth == 640 && !$o->{allowFB}) {
	$o->{vga} = 785;
	$o->{allowFB} = 1;
    }
}

sub enteringStep {
    my ($o, $step) = @_;

    printf "Entering step `%s'\n", common::remove_translate_context($o->{steps}{$step}{text});
    if (my $banner_title = $o->{steps}{$step}{banner_title}) {
        set_default_step_items($banner_title);
    }
    $o->SUPER::enteringStep($step);
    install::gtk::update_steps_position($o);
}
sub leavingStep {
    my ($o, $step) = @_;
    $o->SUPER::leavingStep($step);
}


sub charsetChanged {
    my ($o) = @_;
    Gtk2->set_locale;
    install::gtk::load_font($o);
    install::gtk::create_steps_window($o);
}


sub interactive_help_has_id {
    my ($_o, $id) = @_;
    exists $install::help::help::{$id};
}

sub interactive_help_get_id {
    my ($_o, @l) = @_;
    @l = map { 
	join("\n\n", map { s/\n/ /mg; $_ } split("\n\n", translate($install::help::help::{$_}->())));
    } grep { exists $install::help::help::{$_} } @l;
    join("\n\n\n", @l);
}

#-######################################################################################
#- Steps Functions
#-######################################################################################
sub selectLanguage {
    my ($o) = @_;
    $o->SUPER::selectLanguage;
  
    $o->ask_warn('',
formatAlaTeX(N("Your system is low on resources. You may have some problem installing
%s. If that occurs, you can try a text install instead. For this,
press `F1' when booting on CDROM, then enter `text'.", N("Mageia")))) if availableRamMB() < 70; # 70MB

}

#------------------------------------------------------------------------------
sub selectMouse {
    my ($o, $force) = @_;
    my %old = %{$o->{mouse}};
    $o->SUPER::selectMouse($force) or return;
    my $mouse = $o->{mouse};
    $mouse->{type} eq 'none' ||
      $old{type}   eq $mouse->{type}   && 
      $old{name}   eq $mouse->{name}   &&
      $old{device} eq $mouse->{device} and return;

    while (1) {
	my $x_protocol_changed = mouse::change_mouse_live($mouse, \%old);
	mouse::test_mouse_install($mouse, $x_protocol_changed) and return;

	%old = %$mouse;
	$o->SUPER::selectMouse;
	$mouse = $o->{mouse};
    } 
}

sub setPackages {
    my ($o) = @_;
    my (undef, $old_title) = get_default_step_items();
    set_default_step_items(N("Media Selection") || $old_title);
    install::any::setPackages($o);
    set_default_step_items($old_title);
}

sub reallyChooseDesktop {
    my ($o, $title, $message, $choices, $choice) = @_;

    my $w = ugtk2->new($title);

    my %tips = (
        KDE    => N("Install %s KDE Desktop", N("Mageia")),
        GNOME  => N("Install %s GNOME Desktop", N("Mageia")),
        Custom => N("Custom install"),
    );
    my $prev;
    my @l = map {
	my $val = $_;
	$prev = gtknew('RadioButton', child =>
                          gtknew('Label', text => $val->[1]),
                       tip => $tips{$val->[0]},
		       toggled => sub { $choice = $val if $_[0]->get_active },
                       active => $choice == $val,
		       $prev ? (group => $prev->get_group) : ());
	$prev->signal_connect(key_press_event => sub {
				  my (undef, $event) = @_;
				  if (!$event || ($event->keyval & 0x7f) == 0xd) {
				      Gtk2->main_quit;
				  }
			      });
        my $img = gtksignal_connect(
            gtkadd(Gtk2::EventBox->new, gtknew('Image', file => "desktop-$val->[0]")),
            'button-press-event' => sub {
                my %title = (
                    KDE    => N("KDE Desktop"),
                    GNOME  => N("GNOME Desktop"),
                    Custom => N("Custom Desktop"),
                );

                my $wp = ugtk2->new($title{$val->[0]}, transient => $w->{real_window}, modal => 1);
                gtkadd($wp->{rwindow},
                       gtkpack_(Gtk2::VBox->new,
                                0, gtknew('Title2', label => N("Here's a preview of the '%s' desktop.", $val->[1]),
                                          # workaround infamous 6 years old gnome bug #101968:
                                          width => mygtk2::get_label_width(), 
                                      ),
                                1, gtknew('Image', file => "desktop-$val->[0]-big"),
                                0, Gtk2::HSeparator->new,
                                0, gtkpack(create_hbox('end'),
                                           gtknew('Button', text => N("Close"), clicked => sub { Gtk2->main_quit })
                                       ),
                            ),
                   );
                $wp->{real_window}->set_size_request(-1, -1);
                $wp->{real_window}->grab_focus;
                $wp->{real_window}->show_all;
                $wp->main;
            });
        gtknew('VBox', border_width => 15, spacing => 10, children_tight => [ 
            $img,
            $prev,
        ]);
    } @$choices;

    ugtk2::gtkadd($w->{window},
	   gtknew('VBox', children => [
		    0, gtknew('Title2',
                              # workaround infamous 6 years old gnome bug #101968:
                              width => mygtk2::get_label_width(), label => $message . ' ' .
                                N("Click on images in order to see a bigger preview")),
		    1, gtknew('HButtonBox', children_loose => \@l),
		    0, $w->create_okcancel(N("Next"), undef, '',
                                           [ gtknew('Install_Button', text => N("Help"),
                                                    clicked => sub {
                                                        interactive::gtk::display_help(
                                                            $o,
                                                            { interactive_help_id => 'chooseDesktop' });
                                                    }), undef, 1 ])
		]));
    $w->main;
    
    $choice;
}

sub reallyChooseGroups {
    my ($o, $size_to_display, $individual, $_compssUsers) = @_;

    my $w = ugtk2->new(N("Package Group Selection"));
    my $w_size = gtknew('Label_Left', text => &$size_to_display, padding => [ 0, 0 ]);
    my @entries;

    my $entry = sub {
	my ($e) = @_;

	my $w = gtknew('CheckButton', 
	       text => translate($e->{label}), 
	       tip => translate($e->{descr}),
	       active_ref => \$e->{selected},
	       toggled => sub { 
		   gtkset($w_size, text => &$size_to_display);
	       });
        push @entries, $w;
        $w;
    };
    #- when restarting this step, it might be necessary to reload the compssUsers.pl (bug 11558). kludgy.
    if (!ref $o->{gtk_display_compssUsers}) { install::any::load_rate_files($o) }
    ugtk2::gtkadd($w->{window},
       gtknew('ScrolledWindow', child =>
	   gtknew('VBox', children => [
		    1, $o->{gtk_display_compssUsers}->($entry),
		    1, '',
		    if_($individual,
                        0, gtknew('CheckButton', text => N("Individual package selection"), active_ref => $individual),
                    ),
		    0, $w_size,
		    0, Gtk2::HSeparator->new,
		    0, gtknew('HButtonBox', layout => 'edge', children_tight => [
                        gtknew('Install_Button', text => N("Help"), clicked => sub {
                                   interactive::gtk::display_help($o, { interactive_help_id => 'choosePackageGroups' }) }),
			  gtknew('Button', text => N("Unselect All"), clicked => sub { $_->set_active(0) foreach @entries }),
			  gtknew('Button', text => N("Next"), clicked => sub { Gtk2->main_quit }),
                    ]),
		  ]),
          )
	  );
    $w->main;
    1;
}

sub choosePackagesTree {
    my ($o, $packages) = @_;

    my $available = install::any::getAvailableSpace($o);
    my $availableCorrected = install::pkgs::invCorrectSize($available / sqr(1024)) * sqr(1024);

    my $common;
    $common = {             get_status => sub {
				my $size = install::pkgs::selectedSize($packages);
				N("Total size: %d / %d MB", install::pkgs::correctSize($size / sqr(1024)), $available / sqr(1024));
			    },
			    node_state => sub {
				my $p = install::pkgs::packageByName($packages, $_[0]) or return;
				!install::pkgs::packageMedium($packages, $p)->{ignore} or return;
				$p->arch eq 'src'                       and return;
				$p->flag_base                           and return 'base';
				$p->flag_installed && !$p->flag_upgrade and return 'installed';