summaryrefslogtreecommitdiffstats
path: root/debian/grub-extras/915resolution/915resolution.c
blob: 0091a669573910f93a60bb2e0ba5f79caa947311 (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
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
/* 915resolution - Utility to change vbemodes on the intel
 * integrated video chipset */

/*
 * Based on Nathan Coulson's http://nathancoulson.com/proj/eee/grub-1.96-915resolution-0.5.2-3.patch
 * Oct 10, 2008, Released as 915
 * Oct 10, 2008, Updated to include support for 945GM thanks to Scot Doyle
 */

/* Copied from 915 resolution created by steve tomjenovic
 * 915 resolution was in the public domain.
 * 
 * All I have done, was make the above program run within 
 * the grub2 environment.  
 *
 * Some of the checks are still commented, as I did not find
 * easy replacement for memmem.
 *
 * Slightly edited by Nathan Coulson (conathan@gmail.com)
 */

/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2003,2007  Free Software Foundation, Inc.
 *  Copyright (C) 2003  NIIBE Yutaka <gniibe@m17n.org>
 *
 *  GRUB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */


/* 915 resolution by steve tomljenovic
 *
 * This was tested only on Sony VGN-FS550.  Use at your own risk
 *
 * This code is based on the techniques used in :
 *
 *   - 855patch.  Many thanks to Christian Zietz (czietz gmx net)
 *     for demonstrating how to shadow the VBIOS into system RAM
 *     and then modify it.
 *
 *   - 1280patch by Andrew Tipton (andrewtipton null li).
 *
 *   - 855resolution by Alain Poirier
 *
 * This source code is into the public domain.
 */

#include <grub/types.h>
#include <grub/misc.h>
#include <grub/mm.h>
#include <grub/err.h>
#include <grub/dl.h>
#include <grub/normal.h>
#include <grub/i386/io.h>

GRUB_MOD_LICENSE ("GPLv3+");

#define printf					grub_printf
#define malloc					grub_malloc
#define free					grub_free
#define strcmp					grub_strcmp
#define fprintf(stream, ...)			grub_printf(__VA_ARGS__)
#define strtol(x,y,z)				grub_strtoul(x,y,z)
#define atoi(x)					grub_strtoul(x,NULL,10)
#define assert(x)				
#define memset					grub_memset
#define outl		grub_outl
#define outb		grub_outb
#define inl		grub_inl
#define inb		grub_inb

#define NEW(a) ((a *)(malloc(sizeof(a))))
#define FREE(a) (free(a))

#define VBIOS_START         0xc0000
#define VBIOS_SIZE          0x10000

#define VBIOS_FILE    "/dev/mem"

#define FALSE 0
#define TRUE 1

#define MODE_TABLE_OFFSET_845G 617

#define RES915_VERSION "0.5.3"

#define ATI_SIGNATURE1 "ATI MOBILITY RADEON"
#define ATI_SIGNATURE2 "ATI Technologies Inc"
#define NVIDIA_SIGNATURE "NVIDIA Corp"
#define INTEL_SIGNATURE "Intel Corp"

#define DEBUG 0

typedef unsigned char * address;
typedef unsigned char byte;
typedef unsigned short word;
typedef unsigned char boolean;
typedef unsigned int cardinal;

typedef enum {
    CT_UNKWN, CT_830, CT_845G, CT_855GM, CT_865G, CT_915G, CT_915GM, CT_945G, CT_945GM, CT_945GME,
    CT_946GZ, CT_G965, CT_Q965, CT_965GM, CT_G33, CT_Q33, CT_Q35, CT_500GMA,
    CT_GM45,  CT_GMA3150, CT_HD3000
} chipset_type;

const char *const chipset_type_names[] = {
    "UNKNOWN", "830",  "845G", "855GM", "865G", "915G", "915GM", "945G", "945GM", "945GME",
    "946GZ",   "G965", "Q965", "965GM", "G33", "Q33", "Q35", "500GMA",
    "GM45",    "GMA3150", "HD3000"
};

typedef enum {
    BT_UNKWN, BT_1, BT_2, BT_3
} bios_type;

const char *const bios_type_names[] = {"UNKNOWN", "TYPE 1", "TYPE 2", "TYPE 3"};

int freqs[] = { 60, 75, 85 };

typedef struct {
    byte mode;
    byte bits_per_pixel;
    word resolution;
    byte unknown;
} __attribute__((packed)) vbios_mode;

typedef struct {
    byte unknow1[2];
    byte x1;
    byte x_total;
    byte x2;
    byte y1;
    byte y_total;
    byte y2;
} __attribute__((packed)) vbios_resolution_type1;

typedef struct {
    unsigned long clock;

    word x1;
    word htotal;
    word x2;
    word hblank;
    word hsyncstart;
    word hsyncend;

    word y1;
    word vtotal;
    word y2;
    word vblank;
    word vsyncstart;
    word vsyncend;
} __attribute__((packed)) vbios_modeline_type2;

typedef struct {
    byte xchars;
    byte ychars;
    byte unknown[4];

    vbios_modeline_type2 modelines[];
} __attribute__((packed)) vbios_resolution_type2;

typedef struct {
    unsigned long clock;

    word x1;
    word htotal;
    word x2;
    word hblank;
    word hsyncstart;
    word hsyncend;

    word y1;
    word vtotal;
    word y2;
    word vblank;
    word vsyncstart;
    word vsyncend;

    word timing_h;
    word timing_v;

    byte unknown[6];
} __attribute__((packed)) vbios_modeline_type3;

typedef struct {
    unsigned char unknown[6];

    vbios_modeline_type3 modelines[];
} __attribute__((packed)) vbios_resolution_type3;


typedef struct {
    cardinal chipset_id;
    chipset_type chipset;
    bios_type bios;
    
    int bios_fd;
    address bios_ptr;

    vbios_mode * mode_table;
    cardinal mode_table_size;

    byte b1, b2;

    boolean unlocked;
} vbios_map;


static cardinal get_chipset_id(void) {
    outl(0x80000000, 0xcf8);
    return inl(0xcfc);
}

static chipset_type get_chipset(cardinal id) {
    chipset_type type;
    
    switch (id) {
    case 0x35758086:
        type = CT_830;
        break;

    case 0x25608086:
        type = CT_845G;
        break;
        
    case 0x35808086:
        type = CT_855GM;
        break;
        
    case 0x25708086:
        type = CT_865G;
        break;

    case 0x25808086:
	type = CT_915G;
	break;

    case 0x25908086:
        type = CT_915GM;
        break;

    case 0x27708086:
        type = CT_945G;
        break;

    case 0x27a08086:
        type = CT_945GM;
        break;

    case 0x27ac8086:
        type = CT_945GME;
        break;

    case 0x29708086:
        type = CT_946GZ;
        break;

    case 0x29a08086:
	type = CT_G965;
	break;

    case 0x29908086:
        type = CT_Q965;
        break;

    case 0x2a008086:
        type = CT_965GM;
        break;

    case 0x29c08086:
        type = CT_G33;
        break;

    case 0x29b08086:
        type = CT_Q35;
        break;

    case 0x29d08086:
        type = CT_Q33;
        break;

    case 0x81008086:
      type = CT_500GMA;
      break;

    case 0xa0008086:
        type = CT_GMA3150;
        break;

    case 0xa0108086:
        type = CT_GMA3150;
        break;

    case 0x01048086:
        type = CT_HD3000;
        break;

    case 0x2a408086:
        type = CT_GM45;
        break;

    case 0x2a018086:
        type = CT_965GM;
        break;

    case 0x2a028086:
        type = CT_965GM;
        break;

    default:
        type = CT_UNKWN;
        break;
    }

    return type;
}


static vbios_resolution_type1 * map_type1_resolution(vbios_map * map, word res) {
    vbios_resolution_type1 * ptr = ((vbios_resolution_type1*)(map->bios_ptr + res)); 
    return ptr;
}

static vbios_resolution_type2 * map_type2_resolution(vbios_map * map, word res) {
    vbios_resolution_type2 * ptr = ((vbios_resolution_type2*)(map->bios_ptr + res)); 
    return ptr;
}

static vbios_resolution_type3 * map_type3_resolution(vbios_map * map, word res) {
    vbios_resolution_type3 * ptr = ((vbios_resolution_type3*)(map->bios_ptr + res)); 
    return ptr;
}


static boolean detect_bios_type(vbios_map * map, int entry_size) {
    unsigned i;
    short int r1, r2;
    
    r1 = r2 = 32000;

    for (i=0; i < map->mode_table_size; i++) {
        if (map->mode_table[i].resolution <= r1) {
            r1 = map->mode_table[i].resolution;
    	}
        else {
            if (map->mode_table[i].resolution <= r2) {
            	r2 = map->mode_table[i].resolution;
            }
    	}

        /*printf("r1 = %d  r2 = %d\n", r1, r2);*/
    }

    return (r2-r1-6) % entry_size == 0;
}


static void close_vbios(vbios_map * map);


static vbios_map * open_vbios(chipset_type forced_chipset) {
    vbios_map * map = NEW(vbios_map);
    memset (map, 0, sizeof(vbios_map));

    /*
     * Determine chipset
     */

    if (forced_chipset == CT_UNKWN) {
        map->chipset_id = get_chipset_id();

        map->chipset = get_chipset(map->chipset_id);
    }
    else if (forced_chipset != CT_UNKWN) {
        map->chipset = forced_chipset;
    }
    else {
        map->chipset = CT_915GM;
    }
    
    /*
     *  Map the video bios to memory
     */

    map->bios_ptr = (unsigned char *) VBIOS_START;

#if 0
    /*
     * check if we have ATI Radeon
     */
    
    if (memmem(map->bios_ptr, VBIOS_SIZE, ATI_SIGNATURE1, strlen(ATI_SIGNATURE1)) ||
        memmem(map->bios_ptr, VBIOS_SIZE, ATI_SIGNATURE2, strlen(ATI_SIGNATURE2)) ) {
        fprintf(stderr, "ATI chipset detected.  915resolution only works with Intel 800/900 series graphic chipsets.\n");
        close(map->bios_fd);
        exit(2);
    }

    /*
     * check if we have NVIDIA
     */
    
    if (memmem(map->bios_ptr, VBIOS_SIZE, NVIDIA_SIGNATURE, strlen(NVIDIA_SIGNATURE))) {
        fprintf(stderr, "NVIDIA chipset detected.  915resolution only works with Intel 800/900 series graphic chipsets.\n");
        close(map->bios_fd);
        exit(2);
    }

    /*
     * check if we have Intel
     */
    
    if (map->chipset == CT_UNKWN && memmem(map->bios_ptr, VBIOS_SIZE, INTEL_SIGNATURE, strlen(INTEL_SIGNATURE))) {
        fprintf(stderr, "Intel chipset detected.  However, 915resolution was unable to determine the chipset type.\n");

        fprintf(stderr, "Chipset Id: %x\n", map->chipset_id);

        fprintf(stderr, "Please report this problem to stomljen@yahoo.com\n");
        
        close_vbios(map);
        exit(2);
    }
#endif

    /*
     * check for others
     */

    if (map->chipset == CT_UNKWN) {
        fprintf(stderr, "Unknown chipset type and unrecognized bios.\n");
        fprintf(stderr, "915resolution only works with Intel 800/900 series graphic chipsets.\n");

        fprintf(stderr, "Chipset Id: %x\n", map->chipset_id);
        close_vbios(map);
        return 0;
    }

    /*
     * Figure out where the mode table is 
     */
    
    {
        address p = map->bios_ptr + 16;
        address limit = map->bios_ptr + VBIOS_SIZE - (3 * sizeof(vbios_mode));
        
        while (p < limit && map->mode_table == 0) {
            vbios_mode * mode_ptr = (vbios_mode *) p;
            
            if (((mode_ptr[0].mode & 0xf0) == 0x30) && ((mode_ptr[1].mode & 0xf0) == 0x30) &&
                ((mode_ptr[2].mode & 0xf0) == 0x30) && ((mode_ptr[3].mode & 0xf0) == 0x30)) {

                map->mode_table = mode_ptr;
            }
            
            p++;
        }

        if (map->mode_table == 0) {
            fprintf(stderr, "Unable to locate the mode table.\n");
            fprintf(stderr, "Please run the program 'dump_bios' as root and\n");
            fprintf(stderr, "email the file 'vbios.dmp' to stomljen@yahoo.com.\n");
            
            fprintf(stderr, "Chipset: %s\n", chipset_type_names[map->chipset]);
            close_vbios(map);
            return 0;
        }
    }

    /*
     * Determine size of mode table
     */
    
    {
        vbios_mode * mode_ptr = map->mode_table;
        
        while (mode_ptr->mode != 0xff) {
            map->mode_table_size++;
            mode_ptr++;
        }
    }

    /*
     * Figure out what type of bios we have
     *  order of detection is important
     */

    if (detect_bios_type(map, sizeof(vbios_modeline_type3))) {
        map->bios = BT_3;
    }
    else if (detect_bios_type(map, sizeof(vbios_modeline_type2))) {
        map->bios = BT_2;
    }
    else if (detect_bios_type(map, sizeof(vbios_resolution_type1))) {
        map->bios = BT_1;
    }
    else {
        fprintf(stderr, "Unable to determine bios type.\n");
        fprintf(stderr, "Please run the program 'dump_bios' as root and\n");
        fprintf(stderr, "email the file 'vbios.dmp' to stomljen@yahoo.com.\n");

        fprintf(stderr, "Chipset: %s\n", chipset_type_names[map->chipset]);
        fprintf(stderr, "Mode Table Offset: $C0000 + $%x\n", ((cardinal)map->mode_table) - ((cardinal)map->bios_ptr));
        fprintf(stderr, "Mode Table Entries: %u\n", map->mode_table_size);
        return 0;
    }

    return map;
}

static void close_vbios(vbios_map * map) {
    assert(!map->unlocked);

    FREE(map);
}

static void unlock_vbios(vbios_map * map) {

    assert(!map->unlocked);
        
    map->unlocked = TRUE;
    
    switch (map->chipset) {
    case CT_UNKWN:
        break;
    case CT_830:
    case CT_855GM:
        outl(0x8000005a, 0xcf8);
        map->b1 = inb(0xcfe);
        
        outl(0x8000005a, 0xcf8);
        outb(0x33, 0xcfe);
        break;
    case CT_845G:
    case CT_865G:
    case CT_915G:
    case CT_915GM:
    case CT_945G:
    case CT_945GM:
    case CT_945GME:
    case CT_946GZ:
    case CT_G965:
    case CT_Q965:
    case CT_965GM:
    case CT_G33:
    case CT_Q35:
    case CT_Q33:
    case CT_500GMA:
    case CT_GM45:
    case CT_GMA3150:
    case CT_HD3000:
        outl(0x80000090, 0xcf8);
        map->b1 = inb(0xcfd);
        map->b2 = inb(0xcfe);
        
        outl(0x80000090, 0xcf8);
        outb(0x33, 0xcfd);
        outb(0x33, 0xcfe);
        break;
    }

#if DEBUG
    {
        cardinal t = inl(0xcfc);
        printf("unlock PAM: (0x%08x)\n", t);
    }
#endif
}

static void relock_vbios(vbios_map * map) {

    assert(map->unlocked);
    map->unlocked = FALSE;
    
    switch (map->chipset) {
    case CT_UNKWN:
        break;
    case CT_830:
    case CT_855GM:
        outl(0x8000005a, 0xcf8);
        outb(map->b1, 0xcfe);
        break;
    case CT_845G:
    case CT_865G:
    case CT_915G:
    case CT_915GM:
    case CT_945G:
    case CT_945GM:
    case CT_945GME:
    case CT_946GZ:
    case CT_G965:
    case CT_Q965:
    case CT_965GM:
    case CT_G33:
    case CT_Q35:
    case CT_Q33:
    case CT_500GMA:
    case CT_GM45:
    case CT_GMA3150:
    case CT_HD3000:
        outl(0x80000090, 0xcf8);
        outb(map->b1, 0xcfd);
        outb(map->b2, 0xcfe);
        break;
    }

#if DEBUG
    {
        cardinal t = inl(0xcfc);
        printf("relock PAM: (0x%08x)\n", t);
    }
#endif
}


static void list_modes(vbios_map *map, cardinal raw) {
    cardinal i, x, y;

    for (i=0; i < map->mode_table_size; i++) {
        switch(map->bios) {
        case BT_1:
            {
                vbios_resolution_type1 * res = map_type1_resolution(map, map->mode_table[i].resolution);
                
                x = ((((cardinal) res->x2) & 0xf0) << 4) | res->x1;
                y = ((((cardinal) res->y2) & 0xf0) << 4) | res->y1;
                
                if (x != 0 && y != 0) {
                    printf("Mode %02x : %dx%d, %d bits/pixel\n", map->mode_table[i].mode, x, y, map->mode_table[i].bits_per_pixel);
                }

		if (raw)
		{
                    printf("Mode %02x (raw) :\n\t%02x %02x\n\t%02x\n\t%02x\n\t%02x\n\t%02x\n\t%02x\n\t%02x\n", map->mode_table[i].mode, res->unknow1[0],res->unknow1[1], res->x1,res->x_total,res->x2,res->y1,res->y_total,res->y2);
		}

            }
            break;
        case BT_2:
            {
                vbios_resolution_type2 * res = map_type2_resolution(map, map->mode_table[i].resolution);
                
                x = res->modelines[0].x1+1;
                y = res->modelines[0].y1+1;

                if (x != 0 && y != 0) {
                    printf("Mode %02x : %dx%d, %d bits/pixel\n", map->mode_table[i].mode, x, y, map->mode_table[i].bits_per_pixel);
                }
            }
            break;
        case BT_3:
            {
                vbios_resolution_type3 * res = map_type3_resolution(map, map->mode_table[i].resolution);
                
                x = res->modelines[0].x1+1;
                y = res->modelines[0].y1+1;
                
                if (x != 0 && y != 0) {
                    printf("Mode %02x : %dx%d, %d bits/pixel\n", map->mode_table[i].mode, x, y, map->mode_table[i].bits_per_pixel);
                }
            }
            break;
        case BT_UNKWN:
            break;
        }
    }
}

static void gtf_timings(int x, int y, int freq,
        unsigned long *clock,
        word *hsyncstart, word *hsyncend, word *hblank,
        word *vsyncstart, word *vsyncend, word *vblank)
{
    int hbl, vbl, vfreq;

    /*
      (y+1)/(20000.0/(11*freq) - 1) + 0.5
      = ((y+1) * (11*freq))/(20000.0 - (11*freq)) + 0.5
      = ((y+1) * (11*freq) + 10000.0 - (11*freq) / 2)/(20000.0 - (11*freq))
      = ((y+1) * (11*freq) + 10000.0 - 5 * freq - (freq + 1) / 2)/(20000.0 - (11*freq))
*/
    vbl = y + 1 +
      grub_divmod64 (((y+1) * (11*(long long)freq) + 10000 - 5 * (long long)freq
		      - (freq + 1) / 2),
		     (20000 - (11*(long long)freq)), 0);
    vfreq = vbl * freq;
    /*
      (x * (30.0 - 300000.0 / vfreq) /
      (70.0 + 300000.0 / vfreq) / 16.0 + 0.5)
      = ((x * (30.0 - 300000.0 / vfreq) /
          (70.0 + 300000.0 / vfreq) + 8) / 16)
      = ((x * (30.0 * vfreq - 300000.0) /
          (70.0 * vfreq + 300000.0) + 8) / 16)
      = (((x * (30 * vfreq - 300000)) /
          (70 * vfreq + 300000) + 8) / 16)
    */
    hbl = 16 * (int)((grub_divmod64((x * (30 * (long long)vfreq - 300000)),
				    (70 * (long long)vfreq + 300000), 0)
		      + 8) / 16);

    *vsyncstart = y;
    *vsyncend = y + 3;
    *vblank = vbl - 1;
    *hsyncstart = x + hbl / 2 - (x + hbl + 50) / 100 * 8 - 1;
    *hsyncend = x + hbl / 2 - 1;
    *hblank = x + hbl - 1;
    *clock = (x + hbl) * vfreq / 1000;
}

static void set_mode(vbios_map * map, cardinal mode, cardinal x, cardinal y, cardinal bp, cardinal htotal, cardinal vtotal) {
    int xprev, yprev;
    cardinal i, j;

    for (i=0; i < map->mode_table_size; i++) {
        if (map->mode_table[i].mode == mode) {
            switch(map->bios) {
            case BT_1:
                {
                    vbios_resolution_type1 * res = map_type1_resolution(map, map->mode_table[i].resolution);
                    
                    if (bp) {
                        map->mode_table[i].bits_per_pixel = bp;
                    }
                    
                    res->x2 = (htotal?(((htotal-x) >> 8) & 0x0f) : (res->x2 & 0x0f)) | ((x >> 4) & 0xf0);
                    res->x1 = (x & 0xff);
                    
                    res->y2 = (vtotal?(((vtotal-y) >> 8) & 0x0f) : (res->y2 & 0x0f)) | ((y >> 4) & 0xf0);
                    res->y1 = (y & 0xff);
		    if (htotal)
			res->x_total = ((htotal-x) & 0xff);

		    if (vtotal)
			res->y_total = ((vtotal-y) & 0xff);
                }
                break;
            case BT_2:
                {
                    vbios_resolution_type2 * res = map_type2_resolution(map, map->mode_table[i].resolution);

                    res->xchars = x / 8;
                    res->ychars = y / 16 - 1;
                    xprev = res->modelines[0].x1;
                    yprev = res->modelines[0].y1;

                    for(j=0; j < 3; j++) {
                        vbios_modeline_type2 * modeline = &res->modelines[j];
                        
                        if (modeline->x1 == xprev && modeline->y1 == yprev) {
                            unsigned long clock;
                            word hsyncstart, hsyncend, hblank;
                            word vsyncstart, vsyncend, vblank;

                            modeline->x1 = modeline->x2 = x-1;
                            modeline->y1 = modeline->y2 = y-1;

                            gtf_timings(x, y, freqs[j], &clock,
                                    &hsyncstart, &hsyncend, &hblank,
                                    &vsyncstart, &vsyncend, &vblank);
                            modeline->clock = clock;
                            modeline->hsyncstart = hsyncstart;
                            modeline->hsyncend = hsyncend;
                            modeline->hblank = hblank;
                            modeline->vsyncstart = vsyncstart;
                            modeline->vsyncend = vsyncend;
                            modeline->vblank = vblank;

                            if (htotal)
                                modeline->htotal = htotal;
                            else
                                modeline->htotal = modeline->hblank;

                            if (vtotal)
                                modeline->vtotal = vtotal;
                            else
                                modeline->vtotal = modeline->vblank;
                        }
                    }
                }
                break;
            case BT_3:
                {
                    vbios_resolution_type3 * res = map_type3_resolution(map, map->mode_table[i].resolution);
                    
                    xprev = res->modelines[0].x1;
                    yprev = res->modelines[0].y1;

                    for (j=0; j < 3; j++) {
                        vbios_modeline_type3 * modeline = &res->modelines[j];
                        
                        if (modeline->x1 == xprev && modeline->y1 == yprev) {
                            unsigned long clock;
                            word hsyncstart, hsyncend, hblank;
                            word vsyncstart, vsyncend, vblank;

                            modeline->x1 = modeline->x2 = x-1;
                            modeline->y1 = modeline->y2 = y-1;
                            
                            gtf_timings(x, y, freqs[j], &clock,
                                    &hsyncstart, &hsyncend, &hblank,
                                    &vsyncstart, &vsyncend, &vblank);
                            modeline->clock = clock;
                            modeline->hsyncstart = hsyncstart;
                            modeline->hsyncend = hsyncend;
                            modeline->hblank = hblank;
                            modeline->vsyncstart = vsyncstart;
                            modeline->vsyncend = vsyncend;
                            modeline->vblank = vblank;

                            if (htotal)
                                modeline->htotal = htotal;
                            else
                                modeline->htotal = modeline->hblank;
                            if (vtotal)
                                modeline->vtotal = vtotal;
                            else
                                modeline->vtotal = modeline->vblank;

                            modeline->timing_h   = y-1;
                            modeline->timing_v   = x-1;
                        }
                    }
                }
                break;
            case BT_UNKWN:
                break;
            }
        }
    }
}   

static void display_map_info(vbios_map * map) {
    printf("Chipset: %s\n", chipset_type_names[map->chipset]);
    printf("BIOS: %s\n", bios_type_names[map->bios]);

    printf("Mode Table Offset: $C0000 + $%x\n", ((cardinal)map->mode_table) - ((cardinal)map->bios_ptr));
    printf("Mode Table Entries: %u\n", map->mode_table_size);
}


static int parse_args(cardinal argc, char *argv[], chipset_type *forced_chipset,
		      cardinal *list, cardinal *mode, cardinal *x, cardinal *y,
		      cardinal *bp, cardinal *raw, cardinal *htotal,
		      cardinal *vtotal, cardinal *quiet) {
    cardinal index = 0;

    *list = *mode = *x = *y = *raw = *htotal = *vtotal = 0;
    *bp = 0;
    *quiet = 0;

    *forced_chipset = CT_UNKWN;

    if ((argc > index) && !strcmp(argv[index], "-q")) {
        *quiet = 1;
        index++;

        if(argc<=index) {
            return 0;
        }
    }    

    if ((argc > index) && !strcmp(argv[index], "-c")) {
        index++;

        if(argc<=index) {
            return 0;
        }
        
        if (!strcmp(argv[index], "845")) {
            *forced_chipset = CT_845G;
        }
        else if (!strcmp(argv[index], "855")) {
            *forced_chipset = CT_855GM;
        }
        else if (!strcmp(argv[index], "865")) {
            *forced_chipset = CT_865G;
        }
        else if (!strcmp(argv[index], "915G")) {
            *forced_chipset = CT_915G;
        }
        else if (!strcmp(argv[index], "915GM")) {
            *forced_chipset = CT_915GM;
        }
        else if (!strcmp(argv[index], "945G")) {
            *forced_chipset = CT_945G;
        }
        else if (!strcmp(argv[index], "945GM")) {
            *forced_chipset = CT_945GM;
        }
        else if (!strcmp(argv[index], "945GME")) {
            *forced_chipset = CT_945GME;
        }
        else if (!strcmp(argv[index], "946GZ")) {
            *forced_chipset = CT_946GZ;
        }
        else if (!strcmp(argv[index], "G965")) {
            *forced_chipset = CT_G965;
        }
        else if (!strcmp(argv[index], "Q965")) {
            *forced_chipset = CT_Q965;
        }
        else if (!strcmp(argv[index], "965GM")) {
            *forced_chipset = CT_965GM;
        }
        else if (!strcmp(argv[index], "G33")) {
            *forced_chipset = CT_G33;
        }
        else if (!strcmp(argv[index], "Q35")) {
            *forced_chipset = CT_Q35;
        }
        else if (!strcmp(argv[index], "Q33")) {
            *forced_chipset = CT_Q33;
        }
	else if (!strcmp(argv[index], "500GMA")) {
	    *forced_chipset = CT_500GMA;
	}
        else if (!strcmp(argv[index], "GM45")) {
            *forced_chipset = CT_GM45;
        }
        else if (!strcmp(argv[index], "GMA3150")) {
            *forced_chipset = CT_GMA3150;
        }
        else if (!strcmp(argv[index], "HD3000")) {
            *forced_chipset = CT_HD3000;
        }
        else {
            *forced_chipset = CT_UNKWN;
        }
        
        index++;
        
        if (argc<=index) {
            return 0;
        }
    }

    if ((argc > index) && !strcmp(argv[index], "-l")) {
        *list = 1;
        index++;

        if(argc<=index) {
            return 0;
        }
    }
    
    if ((argc > index) && !strcmp(argv[index], "-r")) {
        *raw = 1;
        index++;

        if(argc<=index) {
            return 0;
        }
    }
    
    if (argc-index < 3 || argc-index > 6) {
        return -1;
    }

    *mode = (cardinal) strtol(argv[index], NULL, 16);
    *x = (cardinal)atoi(argv[index+1]);
    *y = (cardinal)atoi(argv[index+2]);


    if (argc-index > 3) {
        *bp = (cardinal)atoi(argv[index+3]);
    }
    else {
        *bp = 0;
    }
    
    if (argc-index > 4) {
        *htotal = (cardinal)atoi(argv[index+4]);
    }
    else {
        *htotal = 0;
    }

    if (argc-index > 5) {
        *vtotal = (cardinal)atoi(argv[index+5]);
    }
    else {
        *vtotal = 0;
    }
    
    return 0;
}

static void usage(void) {
    printf("Usage: 915resolution [-q] [-c chipset] [-l] [mode X Y] [bits/pixel] [htotal] [vtotal]\n");
    printf("  Set the resolution to XxY for a video mode\n");
    printf("  Bits per pixel are optional.  htotal/vtotal settings are additionally optional.\n");
    printf("  Options:\n");
    printf("    -q don't show any normal messages\n");
    printf("    -c force chipset type (THIS IS USED FOR DEBUG PURPOSES)\n");
    printf("    -l display the modes found in the video BIOS\n");
    printf("    -r display the modes found in the video BIOS in raw mode (THIS IS USED FOR DEBUG PURPOSES)\n");
}

static int main_915 (int argc, char *argv[])
{
    vbios_map * map;
    cardinal list, mode, x, y, bp, raw, htotal, vtotal;
    cardinal quiet;
    chipset_type forced_chipset;
    
    if (parse_args(argc, argv, &forced_chipset, &list, &mode,
		   &x, &y, &bp, &raw, &htotal, &vtotal, &quiet) == -1) {
        printf("Intel 800/900 Series VBIOS Hack : version %s\n\n",
	       RES915_VERSION);
        usage();
        return 2;
    }

    if (!quiet)
      printf("Intel 800/900 Series VBIOS Hack : version %s\n\n",
	     RES915_VERSION);

    map = open_vbios(forced_chipset);
    if (!quiet)
      {
	display_map_info(map);
	printf("\n");
      }

    if (list) {
        list_modes(map, raw);
    }

    if (mode!=0 && x!=0 && y!=0) {
          unlock_vbios(map);

        set_mode(map, mode, x, y, bp, htotal, vtotal);

          relock_vbios(map);
        
        printf("Patch mode %02x to resolution %dx%d complete\n", mode, x, y);
        
        if (list) {
            list_modes(map, raw);
        }
    }

    close_vbios(map);
    
    return 0;
}





static grub_err_t
grub_cmd_915resolution (grub_command_t cmd __attribute__ ((unused)), 
			int argc, char *argv[])
{
  return main_915 (argc, argv);
}

static grub_command_t cmd;

GRUB_MOD_INIT(915resolution)
{
  cmd = grub_register_command ("915resolution", grub_cmd_915resolution,
			       "915resolution", "Intel VBE editor");
}

GRUB_MOD_FINI(915resolution)
{
  grub_unregister_command (cmd);
}