1 /* 915resolution - Utility to change vbemodes on the intel
2 * integrated video chipset */
5 * Based on Nathan Coulson's http://nathancoulson.com/proj/eee/grub-1.96-915resolution-0.5.2-3.patch
6 * Oct 10, 2008, Released as 915
7 * Oct 10, 2008, Updated to include support for 945GM thanks to Scot Doyle
10 /* Copied from 915 resolution created by steve tomjenovic
11 * 915 resolution was in the public domain.
13 * All I have done, was make the above program run within
14 * the grub2 environment.
16 * Some of the checks are still commented, as I did not find
17 * easy replacement for memmem.
19 * Slightly edited by Nathan Coulson (conathan@gmail.com)
23 * GRUB -- GRand Unified Bootloader
24 * Copyright (C) 2003,2007 Free Software Foundation, Inc.
25 * Copyright (C) 2003 NIIBE Yutaka <gniibe@m17n.org>
27 * GRUB is free software: you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published by
29 * the Free Software Foundation, either version 3 of the License, or
30 * (at your option) any later version.
32 * GRUB is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License
38 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
42 /* 915 resolution by steve tomljenovic
44 * This was tested only on Sony VGN-FS550. Use at your own risk
46 * This code is based on the techniques used in :
48 * - 855patch. Many thanks to Christian Zietz (czietz gmx net)
49 * for demonstrating how to shadow the VBIOS into system RAM
52 * - 1280patch by Andrew Tipton (andrewtipton null li).
54 * - 855resolution by Alain Poirier
56 * This source code is into the public domain.
59 #include <grub/types.h>
60 #include <grub/misc.h>
64 #include <grub/normal.h>
65 #include <grub/i386/io.h>
67 #define printf grub_printf
68 #define malloc grub_malloc
69 #define free grub_free
70 #define strcmp grub_strcmp
71 #define fprintf(stream,template,args...) grub_printf(template, ## args)
72 #define strtol(x,y,z) grub_strtoul(x,y,z)
73 #define atoi(x) grub_strtoul(x,NULL,10)
75 #define memset grub_memset
76 #define outl grub_outl
77 #define outb grub_outb
81 #define NEW(a) ((a *)(malloc(sizeof(a))))
82 #define FREE(a) (free(a))
84 #define VBIOS_START 0xc0000
85 #define VBIOS_SIZE 0x10000
87 #define VBIOS_FILE "/dev/mem"
92 #define MODE_TABLE_OFFSET_845G 617
94 #define RES915_VERSION "0.5.3"
96 #define ATI_SIGNATURE1 "ATI MOBILITY RADEON"
97 #define ATI_SIGNATURE2 "ATI Technologies Inc"
98 #define NVIDIA_SIGNATURE "NVIDIA Corp"
99 #define INTEL_SIGNATURE "Intel Corp"
103 typedef unsigned char * address
;
104 typedef unsigned char byte
;
105 typedef unsigned short word
;
106 typedef unsigned char boolean
;
107 typedef unsigned int cardinal
;
110 CT_UNKWN
, CT_830
, CT_845G
, CT_855GM
, CT_865G
, CT_915G
, CT_915GM
, CT_945G
, CT_945GM
, CT_945GME
,
111 CT_946GZ
, CT_G965
, CT_Q965
, CT_965GM
, CT_G33
, CT_Q33
, CT_Q35
, CT_500GMA
114 char * chipset_type_names
[] = {
115 "UNKNOWN", "830", "845G", "855GM", "865G", "915G", "915GM", "945G", "945GM", "945GME",
116 "946GZ", "G965", "Q965", "965GM", "G33", "Q33", "Q35", "500GMA"
120 BT_UNKWN
, BT_1
, BT_2
, BT_3
123 char * bios_type_names
[] = {"UNKNOWN", "TYPE 1", "TYPE 2", "TYPE 3"};
125 int freqs
[] = { 60, 75, 85 };
132 } __attribute__((packed
)) vbios_mode
;
142 } __attribute__((packed
)) vbios_resolution_type1
;
160 } __attribute__((packed
)) vbios_modeline_type2
;
167 vbios_modeline_type2 modelines
[];
168 } __attribute__((packed
)) vbios_resolution_type2
;
191 } __attribute__((packed
)) vbios_modeline_type3
;
194 unsigned char unknown
[6];
196 vbios_modeline_type3 modelines
[];
197 } __attribute__((packed
)) vbios_resolution_type3
;
202 chipset_type chipset
;
208 vbios_mode
* mode_table
;
209 cardinal mode_table_size
;
217 static cardinal
get_chipset_id(void) {
218 outl(0x80000000, 0xcf8);
222 static chipset_type
get_chipset(cardinal id
) {
303 static vbios_resolution_type1
* map_type1_resolution(vbios_map
* map
, word res
) {
304 vbios_resolution_type1
* ptr
= ((vbios_resolution_type1
*)(map
->bios_ptr
+ res
));
308 static vbios_resolution_type2
* map_type2_resolution(vbios_map
* map
, word res
) {
309 vbios_resolution_type2
* ptr
= ((vbios_resolution_type2
*)(map
->bios_ptr
+ res
));
313 static vbios_resolution_type3
* map_type3_resolution(vbios_map
* map
, word res
) {
314 vbios_resolution_type3
* ptr
= ((vbios_resolution_type3
*)(map
->bios_ptr
+ res
));
319 static boolean
detect_bios_type(vbios_map
* map
, int entry_size
) {
325 for (i
=0; i
< map
->mode_table_size
; i
++) {
326 if (map
->mode_table
[i
].resolution
<= r1
) {
327 r1
= map
->mode_table
[i
].resolution
;
330 if (map
->mode_table
[i
].resolution
<= r2
) {
331 r2
= map
->mode_table
[i
].resolution
;
335 /*printf("r1 = %d r2 = %d\n", r1, r2);*/
338 return (r2
-r1
-6) % entry_size
== 0;
342 static void close_vbios(vbios_map
* map
);
345 static vbios_map
* open_vbios(chipset_type forced_chipset
) {
346 vbios_map
* map
= NEW(vbios_map
);
347 memset (map
, 0, sizeof(vbios_map
));
353 if (forced_chipset
== CT_UNKWN
) {
354 map
->chipset_id
= get_chipset_id();
356 map
->chipset
= get_chipset(map
->chipset_id
);
358 else if (forced_chipset
!= CT_UNKWN
) {
359 map
->chipset
= forced_chipset
;
362 map
->chipset
= CT_915GM
;
366 * Map the video bios to memory
369 map
->bios_ptr
= (unsigned char *) VBIOS_START
;
373 * check if we have ATI Radeon
376 if (memmem(map
->bios_ptr
, VBIOS_SIZE
, ATI_SIGNATURE1
, strlen(ATI_SIGNATURE1
)) ||
377 memmem(map
->bios_ptr
, VBIOS_SIZE
, ATI_SIGNATURE2
, strlen(ATI_SIGNATURE2
)) ) {
378 fprintf(stderr
, "ATI chipset detected. 915resolution only works with Intel 800/900 series graphic chipsets.\n");
384 * check if we have NVIDIA
387 if (memmem(map
->bios_ptr
, VBIOS_SIZE
, NVIDIA_SIGNATURE
, strlen(NVIDIA_SIGNATURE
))) {
388 fprintf(stderr
, "NVIDIA chipset detected. 915resolution only works with Intel 800/900 series graphic chipsets.\n");
394 * check if we have Intel
397 if (map
->chipset
== CT_UNKWN
&& memmem(map
->bios_ptr
, VBIOS_SIZE
, INTEL_SIGNATURE
, strlen(INTEL_SIGNATURE
))) {
398 fprintf(stderr
, "Intel chipset detected. However, 915resolution was unable to determine the chipset type.\n");
400 fprintf(stderr
, "Chipset Id: %x\n", map
->chipset_id
);
402 fprintf(stderr
, "Please report this problem to stomljen@yahoo.com\n");
413 if (map
->chipset
== CT_UNKWN
) {
414 fprintf(stderr
, "Unknown chipset type and unrecognized bios.\n");
415 fprintf(stderr
, "915resolution only works with Intel 800/900 series graphic chipsets.\n");
417 fprintf(stderr
, "Chipset Id: %x\n", map
->chipset_id
);
423 * Figure out where the mode table is
427 address p
= map
->bios_ptr
+ 16;
428 address limit
= map
->bios_ptr
+ VBIOS_SIZE
- (3 * sizeof(vbios_mode
));
430 while (p
< limit
&& map
->mode_table
== 0) {
431 vbios_mode
* mode_ptr
= (vbios_mode
*) p
;
433 if (((mode_ptr
[0].mode
& 0xf0) == 0x30) && ((mode_ptr
[1].mode
& 0xf0) == 0x30) &&
434 ((mode_ptr
[2].mode
& 0xf0) == 0x30) && ((mode_ptr
[3].mode
& 0xf0) == 0x30)) {
436 map
->mode_table
= mode_ptr
;
442 if (map
->mode_table
== 0) {
443 fprintf(stderr
, "Unable to locate the mode table.\n");
444 fprintf(stderr
, "Please run the program 'dump_bios' as root and\n");
445 fprintf(stderr
, "email the file 'vbios.dmp' to stomljen@yahoo.com.\n");
447 fprintf(stderr
, "Chipset: %s\n", chipset_type_names
[map
->chipset
]);
454 * Determine size of mode table
458 vbios_mode
* mode_ptr
= map
->mode_table
;
460 while (mode_ptr
->mode
!= 0xff) {
461 map
->mode_table_size
++;
467 * Figure out what type of bios we have
468 * order of detection is important
471 if (detect_bios_type(map
, sizeof(vbios_modeline_type3
))) {
474 else if (detect_bios_type(map
, sizeof(vbios_modeline_type2
))) {
477 else if (detect_bios_type(map
, sizeof(vbios_resolution_type1
))) {
481 fprintf(stderr
, "Unable to determine bios type.\n");
482 fprintf(stderr
, "Please run the program 'dump_bios' as root and\n");
483 fprintf(stderr
, "email the file 'vbios.dmp' to stomljen@yahoo.com.\n");
485 fprintf(stderr
, "Chipset: %s\n", chipset_type_names
[map
->chipset
]);
486 fprintf(stderr
, "Mode Table Offset: $C0000 + $%x\n", ((cardinal
)map
->mode_table
) - ((cardinal
)map
->bios_ptr
));
487 fprintf(stderr
, "Mode Table Entries: %u\n", map
->mode_table_size
);
494 static void close_vbios(vbios_map
* map
) {
495 assert(!map
->unlocked
);
500 static void unlock_vbios(vbios_map
* map
) {
502 assert(!map
->unlocked
);
504 map
->unlocked
= TRUE
;
506 switch (map
->chipset
) {
511 outl(0x8000005a, 0xcf8);
512 map
->b1
= inb(0xcfe);
514 outl(0x8000005a, 0xcf8);
532 outl(0x80000090, 0xcf8);
533 map
->b1
= inb(0xcfd);
534 map
->b2
= inb(0xcfe);
536 outl(0x80000090, 0xcf8);
544 cardinal t
= inl(0xcfc);
545 printf("unlock PAM: (0x%08x)\n", t
);
550 static void relock_vbios(vbios_map
* map
) {
552 assert(map
->unlocked
);
553 map
->unlocked
= FALSE
;
555 switch (map
->chipset
) {
560 outl(0x8000005a, 0xcf8);
561 outb(map
->b1
, 0xcfe);
578 outl(0x80000090, 0xcf8);
579 outb(map
->b1
, 0xcfd);
580 outb(map
->b2
, 0xcfe);
586 cardinal t
= inl(0xcfc);
587 printf("relock PAM: (0x%08x)\n", t
);
593 static void list_modes(vbios_map
*map
, cardinal raw
) {
596 for (i
=0; i
< map
->mode_table_size
; i
++) {
600 vbios_resolution_type1
* res
= map_type1_resolution(map
, map
->mode_table
[i
].resolution
);
602 x
= ((((cardinal
) res
->x2
) & 0xf0) << 4) | res
->x1
;
603 y
= ((((cardinal
) res
->y2
) & 0xf0) << 4) | res
->y1
;
605 if (x
!= 0 && y
!= 0) {
606 printf("Mode %02x : %dx%d, %d bits/pixel\n", map
->mode_table
[i
].mode
, x
, y
, map
->mode_table
[i
].bits_per_pixel
);
611 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
);
618 vbios_resolution_type2
* res
= map_type2_resolution(map
, map
->mode_table
[i
].resolution
);
620 x
= res
->modelines
[0].x1
+1;
621 y
= res
->modelines
[0].y1
+1;
623 if (x
!= 0 && y
!= 0) {
624 printf("Mode %02x : %dx%d, %d bits/pixel\n", map
->mode_table
[i
].mode
, x
, y
, map
->mode_table
[i
].bits_per_pixel
);
630 vbios_resolution_type3
* res
= map_type3_resolution(map
, map
->mode_table
[i
].resolution
);
632 x
= res
->modelines
[0].x1
+1;
633 y
= res
->modelines
[0].y1
+1;
635 if (x
!= 0 && y
!= 0) {
636 printf("Mode %02x : %dx%d, %d bits/pixel\n", map
->mode_table
[i
].mode
, x
, y
, map
->mode_table
[i
].bits_per_pixel
);
646 static void gtf_timings(int x
, int y
, int freq
,
647 unsigned long *clock
,
648 word
*hsyncstart
, word
*hsyncend
, word
*hblank
,
649 word
*vsyncstart
, word
*vsyncend
, word
*vblank
)
653 vbl
= y
+ (y
+1)/(20000.0/(11*freq
) - 1) + 1.5;
655 hbl
= 16 * (int)(x
* (30.0 - 300000.0 / vfreq
) /
656 (70.0 + 300000.0 / vfreq
) / 16.0 + 0.5);
661 *hsyncstart
= x
+ hbl
/ 2 - (x
+ hbl
+ 50) / 100 * 8 - 1;
662 *hsyncend
= x
+ hbl
/ 2 - 1;
663 *hblank
= x
+ hbl
- 1;
664 *clock
= (x
+ hbl
) * vfreq
/ 1000;
667 static void set_mode(vbios_map
* map
, cardinal mode
, cardinal x
, cardinal y
, cardinal bp
, cardinal htotal
, cardinal vtotal
) {
671 for (i
=0; i
< map
->mode_table_size
; i
++) {
672 if (map
->mode_table
[i
].mode
== mode
) {
676 vbios_resolution_type1
* res
= map_type1_resolution(map
, map
->mode_table
[i
].resolution
);
679 map
->mode_table
[i
].bits_per_pixel
= bp
;
682 res
->x2
= (htotal
?(((htotal
-x
) >> 8) & 0x0f) : (res
->x2
& 0x0f)) | ((x
>> 4) & 0xf0);
683 res
->x1
= (x
& 0xff);
685 res
->y2
= (vtotal
?(((vtotal
-y
) >> 8) & 0x0f) : (res
->y2
& 0x0f)) | ((y
>> 4) & 0xf0);
686 res
->y1
= (y
& 0xff);
688 res
->x_total
= ((htotal
-x
) & 0xff);
691 res
->y_total
= ((vtotal
-y
) & 0xff);
696 vbios_resolution_type2
* res
= map_type2_resolution(map
, map
->mode_table
[i
].resolution
);
699 res
->ychars
= y
/ 16 - 1;
700 xprev
= res
->modelines
[0].x1
;
701 yprev
= res
->modelines
[0].y1
;
703 for(j
=0; j
< 3; j
++) {
704 vbios_modeline_type2
* modeline
= &res
->modelines
[j
];
706 if (modeline
->x1
== xprev
&& modeline
->y1
== yprev
) {
707 modeline
->x1
= modeline
->x2
= x
-1;
708 modeline
->y1
= modeline
->y2
= y
-1;
710 gtf_timings(x
, y
, freqs
[j
], &modeline
->clock
,
711 &modeline
->hsyncstart
, &modeline
->hsyncend
,
712 &modeline
->hblank
, &modeline
->vsyncstart
,
713 &modeline
->vsyncend
, &modeline
->vblank
);
716 modeline
->htotal
= htotal
;
718 modeline
->htotal
= modeline
->hblank
;
721 modeline
->vtotal
= vtotal
;
723 modeline
->vtotal
= modeline
->vblank
;
730 vbios_resolution_type3
* res
= map_type3_resolution(map
, map
->mode_table
[i
].resolution
);
732 xprev
= res
->modelines
[0].x1
;
733 yprev
= res
->modelines
[0].y1
;
735 for (j
=0; j
< 3; j
++) {
736 vbios_modeline_type3
* modeline
= &res
->modelines
[j
];
738 if (modeline
->x1
== xprev
&& modeline
->y1
== yprev
) {
739 modeline
->x1
= modeline
->x2
= x
-1;
740 modeline
->y1
= modeline
->y2
= y
-1;
742 gtf_timings(x
, y
, freqs
[j
], &modeline
->clock
,
743 &modeline
->hsyncstart
, &modeline
->hsyncend
,
744 &modeline
->hblank
, &modeline
->vsyncstart
,
745 &modeline
->vsyncend
, &modeline
->vblank
);
747 modeline
->htotal
= htotal
;
749 modeline
->htotal
= modeline
->hblank
;
751 modeline
->vtotal
= vtotal
;
753 modeline
->vtotal
= modeline
->vblank
;
755 modeline
->timing_h
= y
-1;
756 modeline
->timing_v
= x
-1;
768 static void display_map_info(vbios_map
* map
) {
769 printf("Chipset: %s\n", chipset_type_names
[map
->chipset
]);
770 printf("BIOS: %s\n", bios_type_names
[map
->bios
]);
772 printf("Mode Table Offset: $C0000 + $%x\n", ((cardinal
)map
->mode_table
) - ((cardinal
)map
->bios_ptr
));
773 printf("Mode Table Entries: %u\n", map
->mode_table_size
);
777 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
) {
780 *list
= *mode
= *x
= *y
= *raw
= *htotal
= *vtotal
= 0;
783 *forced_chipset
= CT_UNKWN
;
786 if ((argc
> index
) && !strcmp(argv
[index
], "-c")) {
793 if (!strcmp(argv
[index
], "845")) {
794 *forced_chipset
= CT_845G
;
796 else if (!strcmp(argv
[index
], "855")) {
797 *forced_chipset
= CT_855GM
;
799 else if (!strcmp(argv
[index
], "865")) {
800 *forced_chipset
= CT_865G
;
802 else if (!strcmp(argv
[index
], "915G")) {
803 *forced_chipset
= CT_915G
;
805 else if (!strcmp(argv
[index
], "915GM")) {
806 *forced_chipset
= CT_915GM
;
808 else if (!strcmp(argv
[index
], "945G")) {
809 *forced_chipset
= CT_945G
;
811 else if (!strcmp(argv
[index
], "945GM")) {
812 *forced_chipset
= CT_945GM
;
814 else if (!strcmp(argv
[index
], "945GME")) {
815 *forced_chipset
= CT_945GME
;
817 else if (!strcmp(argv
[index
], "946GZ")) {
818 *forced_chipset
= CT_946GZ
;
820 else if (!strcmp(argv
[index
], "G965")) {
821 *forced_chipset
= CT_G965
;
823 else if (!strcmp(argv
[index
], "Q965")) {
824 *forced_chipset
= CT_Q965
;
826 else if (!strcmp(argv
[index
], "965GM")) {
827 *forced_chipset
= CT_965GM
;
829 else if (!strcmp(argv
[index
], "G33")) {
830 *forced_chipset
= CT_G33
;
832 else if (!strcmp(argv
[index
], "Q35")) {
833 *forced_chipset
= CT_Q35
;
835 else if (!strcmp(argv
[index
], "Q33")) {
836 *forced_chipset
= CT_Q33
;
838 else if (!strcmp(argv
[index
], "500GMA")) {
839 *forced_chipset
= CT_500GMA
;
842 *forced_chipset
= CT_UNKWN
;
852 if ((argc
> index
) && !strcmp(argv
[index
], "-l")) {
861 if ((argc
> index
) && !strcmp(argv
[index
], "-r")) {
870 if (argc
-index
< 3 || argc
-index
> 6) {
874 *mode
= (cardinal
) strtol(argv
[index
], NULL
, 16);
875 *x
= (cardinal
)atoi(argv
[index
+1]);
876 *y
= (cardinal
)atoi(argv
[index
+2]);
879 if (argc
-index
> 3) {
880 *bp
= (cardinal
)atoi(argv
[index
+3]);
886 if (argc
-index
> 4) {
887 *htotal
= (cardinal
)atoi(argv
[index
+4]);
893 if (argc
-index
> 5) {
894 *vtotal
= (cardinal
)atoi(argv
[index
+5]);
903 static void usage(void) {
904 printf("Usage: 915resolution [-c chipset] [-l] [mode X Y] [bits/pixel] [htotal] [vtotal]\n");
905 printf(" Set the resolution to XxY for a video mode\n");
906 printf(" Bits per pixel are optional. htotal/vtotal settings are additionally optional.\n");
907 printf(" Options:\n");
908 printf(" -c force chipset type (THIS IS USED FOR DEBUG PURPOSES)\n");
909 printf(" -l display the modes found in the video BIOS\n");
910 printf(" -r display the modes found in the video BIOS in raw mode (THIS IS USED FOR DEBUG PURPOSES)\n");
913 static int main (int argc
, char *argv
[])
916 cardinal list
, mode
, x
, y
, bp
, raw
, htotal
, vtotal
;
917 chipset_type forced_chipset
;
919 printf("Intel 800/900 Series VBIOS Hack : version %s\n\n", RES915_VERSION
);
921 if (parse_args(argc
, argv
, &forced_chipset
, &list
, &mode
, &x
, &y
, &bp
, &raw
, &htotal
, &vtotal
) == -1) {
926 map
= open_vbios(forced_chipset
);
927 display_map_info(map
);
932 list_modes(map
, raw
);
935 if (mode
!=0 && x
!=0 && y
!=0) {
938 set_mode(map
, mode
, x
, y
, bp
, htotal
, vtotal
);
942 printf("Patch mode %02x to resolution %dx%d complete\n", mode
, x
, y
);
945 list_modes(map
, raw
);
959 grub_cmd_915resolution (grub_command_t cmd
__attribute__ ((unused
)),
960 int argc
, char *argv
[])
962 return main (argc
, argv
);
965 static grub_command_t cmd
;
967 GRUB_MOD_INIT(915resolution
)
969 cmd
= grub_register_command ("915resolution", grub_cmd_915resolution
,
970 "915resolution", "Intel VBE editor");
973 GRUB_MOD_FINI(915resolution
)
975 grub_unregister_command (cmd
);