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 GRUB_MOD_LICENSE ("GPLv3+");
69 #define printf grub_printf
70 #define malloc grub_malloc
71 #define free grub_free
72 #define strcmp grub_strcmp
73 #define fprintf(stream,template,args...) grub_printf(template, ## args)
74 #define strtol(x,y,z) grub_strtoul(x,y,z)
75 #define atoi(x) grub_strtoul(x,NULL,10)
77 #define memset grub_memset
78 #define outl grub_outl
79 #define outb grub_outb
83 #define NEW(a) ((a *)(malloc(sizeof(a))))
84 #define FREE(a) (free(a))
86 #define VBIOS_START 0xc0000
87 #define VBIOS_SIZE 0x10000
89 #define VBIOS_FILE "/dev/mem"
94 #define MODE_TABLE_OFFSET_845G 617
96 #define RES915_VERSION "0.5.3"
98 #define ATI_SIGNATURE1 "ATI MOBILITY RADEON"
99 #define ATI_SIGNATURE2 "ATI Technologies Inc"
100 #define NVIDIA_SIGNATURE "NVIDIA Corp"
101 #define INTEL_SIGNATURE "Intel Corp"
105 typedef unsigned char * address
;
106 typedef unsigned char byte
;
107 typedef unsigned short word
;
108 typedef unsigned char boolean
;
109 typedef unsigned int cardinal
;
112 CT_UNKWN
, CT_830
, CT_845G
, CT_855GM
, CT_865G
, CT_915G
, CT_915GM
, CT_945G
, CT_945GM
, CT_945GME
,
113 CT_946GZ
, CT_G965
, CT_Q965
, CT_965GM
, CT_G33
, CT_Q33
, CT_Q35
, CT_500GMA
116 const char *const chipset_type_names
[] = {
117 "UNKNOWN", "830", "845G", "855GM", "865G", "915G", "915GM", "945G", "945GM", "945GME",
118 "946GZ", "G965", "Q965", "965GM", "G33", "Q33", "Q35", "500GMA"
122 BT_UNKWN
, BT_1
, BT_2
, BT_3
125 const char *const bios_type_names
[] = {"UNKNOWN", "TYPE 1", "TYPE 2", "TYPE 3"};
127 int freqs
[] = { 60, 75, 85 };
134 } __attribute__((packed
)) vbios_mode
;
144 } __attribute__((packed
)) vbios_resolution_type1
;
162 } __attribute__((packed
)) vbios_modeline_type2
;
169 vbios_modeline_type2 modelines
[];
170 } __attribute__((packed
)) vbios_resolution_type2
;
193 } __attribute__((packed
)) vbios_modeline_type3
;
196 unsigned char unknown
[6];
198 vbios_modeline_type3 modelines
[];
199 } __attribute__((packed
)) vbios_resolution_type3
;
204 chipset_type chipset
;
210 vbios_mode
* mode_table
;
211 cardinal mode_table_size
;
219 static cardinal
get_chipset_id(void) {
220 outl(0x80000000, 0xcf8);
224 static chipset_type
get_chipset(cardinal id
) {
305 static vbios_resolution_type1
* map_type1_resolution(vbios_map
* map
, word res
) {
306 vbios_resolution_type1
* ptr
= ((vbios_resolution_type1
*)(map
->bios_ptr
+ res
));
310 static vbios_resolution_type2
* map_type2_resolution(vbios_map
* map
, word res
) {
311 vbios_resolution_type2
* ptr
= ((vbios_resolution_type2
*)(map
->bios_ptr
+ res
));
315 static vbios_resolution_type3
* map_type3_resolution(vbios_map
* map
, word res
) {
316 vbios_resolution_type3
* ptr
= ((vbios_resolution_type3
*)(map
->bios_ptr
+ res
));
321 static boolean
detect_bios_type(vbios_map
* map
, int entry_size
) {
327 for (i
=0; i
< map
->mode_table_size
; i
++) {
328 if (map
->mode_table
[i
].resolution
<= r1
) {
329 r1
= map
->mode_table
[i
].resolution
;
332 if (map
->mode_table
[i
].resolution
<= r2
) {
333 r2
= map
->mode_table
[i
].resolution
;
337 /*printf("r1 = %d r2 = %d\n", r1, r2);*/
340 return (r2
-r1
-6) % entry_size
== 0;
344 static void close_vbios(vbios_map
* map
);
347 static vbios_map
* open_vbios(chipset_type forced_chipset
) {
348 vbios_map
* map
= NEW(vbios_map
);
349 memset (map
, 0, sizeof(vbios_map
));
355 if (forced_chipset
== CT_UNKWN
) {
356 map
->chipset_id
= get_chipset_id();
358 map
->chipset
= get_chipset(map
->chipset_id
);
360 else if (forced_chipset
!= CT_UNKWN
) {
361 map
->chipset
= forced_chipset
;
364 map
->chipset
= CT_915GM
;
368 * Map the video bios to memory
371 map
->bios_ptr
= (unsigned char *) VBIOS_START
;
375 * check if we have ATI Radeon
378 if (memmem(map
->bios_ptr
, VBIOS_SIZE
, ATI_SIGNATURE1
, strlen(ATI_SIGNATURE1
)) ||
379 memmem(map
->bios_ptr
, VBIOS_SIZE
, ATI_SIGNATURE2
, strlen(ATI_SIGNATURE2
)) ) {
380 fprintf(stderr
, "ATI chipset detected. 915resolution only works with Intel 800/900 series graphic chipsets.\n");
386 * check if we have NVIDIA
389 if (memmem(map
->bios_ptr
, VBIOS_SIZE
, NVIDIA_SIGNATURE
, strlen(NVIDIA_SIGNATURE
))) {
390 fprintf(stderr
, "NVIDIA chipset detected. 915resolution only works with Intel 800/900 series graphic chipsets.\n");
396 * check if we have Intel
399 if (map
->chipset
== CT_UNKWN
&& memmem(map
->bios_ptr
, VBIOS_SIZE
, INTEL_SIGNATURE
, strlen(INTEL_SIGNATURE
))) {
400 fprintf(stderr
, "Intel chipset detected. However, 915resolution was unable to determine the chipset type.\n");
402 fprintf(stderr
, "Chipset Id: %x\n", map
->chipset_id
);
404 fprintf(stderr
, "Please report this problem to stomljen@yahoo.com\n");
415 if (map
->chipset
== CT_UNKWN
) {
416 fprintf(stderr
, "Unknown chipset type and unrecognized bios.\n");
417 fprintf(stderr
, "915resolution only works with Intel 800/900 series graphic chipsets.\n");
419 fprintf(stderr
, "Chipset Id: %x\n", map
->chipset_id
);
425 * Figure out where the mode table is
429 address p
= map
->bios_ptr
+ 16;
430 address limit
= map
->bios_ptr
+ VBIOS_SIZE
- (3 * sizeof(vbios_mode
));
432 while (p
< limit
&& map
->mode_table
== 0) {
433 vbios_mode
* mode_ptr
= (vbios_mode
*) p
;
435 if (((mode_ptr
[0].mode
& 0xf0) == 0x30) && ((mode_ptr
[1].mode
& 0xf0) == 0x30) &&
436 ((mode_ptr
[2].mode
& 0xf0) == 0x30) && ((mode_ptr
[3].mode
& 0xf0) == 0x30)) {
438 map
->mode_table
= mode_ptr
;
444 if (map
->mode_table
== 0) {
445 fprintf(stderr
, "Unable to locate the mode table.\n");
446 fprintf(stderr
, "Please run the program 'dump_bios' as root and\n");
447 fprintf(stderr
, "email the file 'vbios.dmp' to stomljen@yahoo.com.\n");
449 fprintf(stderr
, "Chipset: %s\n", chipset_type_names
[map
->chipset
]);
456 * Determine size of mode table
460 vbios_mode
* mode_ptr
= map
->mode_table
;
462 while (mode_ptr
->mode
!= 0xff) {
463 map
->mode_table_size
++;
469 * Figure out what type of bios we have
470 * order of detection is important
473 if (detect_bios_type(map
, sizeof(vbios_modeline_type3
))) {
476 else if (detect_bios_type(map
, sizeof(vbios_modeline_type2
))) {
479 else if (detect_bios_type(map
, sizeof(vbios_resolution_type1
))) {
483 fprintf(stderr
, "Unable to determine bios type.\n");
484 fprintf(stderr
, "Please run the program 'dump_bios' as root and\n");
485 fprintf(stderr
, "email the file 'vbios.dmp' to stomljen@yahoo.com.\n");
487 fprintf(stderr
, "Chipset: %s\n", chipset_type_names
[map
->chipset
]);
488 fprintf(stderr
, "Mode Table Offset: $C0000 + $%x\n", ((cardinal
)map
->mode_table
) - ((cardinal
)map
->bios_ptr
));
489 fprintf(stderr
, "Mode Table Entries: %u\n", map
->mode_table_size
);
496 static void close_vbios(vbios_map
* map
) {
497 assert(!map
->unlocked
);
502 static void unlock_vbios(vbios_map
* map
) {
504 assert(!map
->unlocked
);
506 map
->unlocked
= TRUE
;
508 switch (map
->chipset
) {
513 outl(0x8000005a, 0xcf8);
514 map
->b1
= inb(0xcfe);
516 outl(0x8000005a, 0xcf8);
534 outl(0x80000090, 0xcf8);
535 map
->b1
= inb(0xcfd);
536 map
->b2
= inb(0xcfe);
538 outl(0x80000090, 0xcf8);
546 cardinal t
= inl(0xcfc);
547 printf("unlock PAM: (0x%08x)\n", t
);
552 static void relock_vbios(vbios_map
* map
) {
554 assert(map
->unlocked
);
555 map
->unlocked
= FALSE
;
557 switch (map
->chipset
) {
562 outl(0x8000005a, 0xcf8);
563 outb(map
->b1
, 0xcfe);
580 outl(0x80000090, 0xcf8);
581 outb(map
->b1
, 0xcfd);
582 outb(map
->b2
, 0xcfe);
588 cardinal t
= inl(0xcfc);
589 printf("relock PAM: (0x%08x)\n", t
);
595 static void list_modes(vbios_map
*map
, cardinal raw
) {
598 for (i
=0; i
< map
->mode_table_size
; i
++) {
602 vbios_resolution_type1
* res
= map_type1_resolution(map
, map
->mode_table
[i
].resolution
);
604 x
= ((((cardinal
) res
->x2
) & 0xf0) << 4) | res
->x1
;
605 y
= ((((cardinal
) res
->y2
) & 0xf0) << 4) | res
->y1
;
607 if (x
!= 0 && y
!= 0) {
608 printf("Mode %02x : %dx%d, %d bits/pixel\n", map
->mode_table
[i
].mode
, x
, y
, map
->mode_table
[i
].bits_per_pixel
);
613 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
);
620 vbios_resolution_type2
* res
= map_type2_resolution(map
, map
->mode_table
[i
].resolution
);
622 x
= res
->modelines
[0].x1
+1;
623 y
= res
->modelines
[0].y1
+1;
625 if (x
!= 0 && y
!= 0) {
626 printf("Mode %02x : %dx%d, %d bits/pixel\n", map
->mode_table
[i
].mode
, x
, y
, map
->mode_table
[i
].bits_per_pixel
);
632 vbios_resolution_type3
* res
= map_type3_resolution(map
, map
->mode_table
[i
].resolution
);
634 x
= res
->modelines
[0].x1
+1;
635 y
= res
->modelines
[0].y1
+1;
637 if (x
!= 0 && y
!= 0) {
638 printf("Mode %02x : %dx%d, %d bits/pixel\n", map
->mode_table
[i
].mode
, x
, y
, map
->mode_table
[i
].bits_per_pixel
);
648 static void gtf_timings(int x
, int y
, int freq
,
649 unsigned long *clock
,
650 word
*hsyncstart
, word
*hsyncend
, word
*hblank
,
651 word
*vsyncstart
, word
*vsyncend
, word
*vblank
)
655 vbl
= y
+ (y
+1)/(20000.0/(11*freq
) - 1) + 1.5;
657 hbl
= 16 * (int)(x
* (30.0 - 300000.0 / vfreq
) /
658 (70.0 + 300000.0 / vfreq
) / 16.0 + 0.5);
663 *hsyncstart
= x
+ hbl
/ 2 - (x
+ hbl
+ 50) / 100 * 8 - 1;
664 *hsyncend
= x
+ hbl
/ 2 - 1;
665 *hblank
= x
+ hbl
- 1;
666 *clock
= (x
+ hbl
) * vfreq
/ 1000;
669 static void set_mode(vbios_map
* map
, cardinal mode
, cardinal x
, cardinal y
, cardinal bp
, cardinal htotal
, cardinal vtotal
) {
673 for (i
=0; i
< map
->mode_table_size
; i
++) {
674 if (map
->mode_table
[i
].mode
== mode
) {
678 vbios_resolution_type1
* res
= map_type1_resolution(map
, map
->mode_table
[i
].resolution
);
681 map
->mode_table
[i
].bits_per_pixel
= bp
;
684 res
->x2
= (htotal
?(((htotal
-x
) >> 8) & 0x0f) : (res
->x2
& 0x0f)) | ((x
>> 4) & 0xf0);
685 res
->x1
= (x
& 0xff);
687 res
->y2
= (vtotal
?(((vtotal
-y
) >> 8) & 0x0f) : (res
->y2
& 0x0f)) | ((y
>> 4) & 0xf0);
688 res
->y1
= (y
& 0xff);
690 res
->x_total
= ((htotal
-x
) & 0xff);
693 res
->y_total
= ((vtotal
-y
) & 0xff);
698 vbios_resolution_type2
* res
= map_type2_resolution(map
, map
->mode_table
[i
].resolution
);
701 res
->ychars
= y
/ 16 - 1;
702 xprev
= res
->modelines
[0].x1
;
703 yprev
= res
->modelines
[0].y1
;
705 for(j
=0; j
< 3; j
++) {
706 vbios_modeline_type2
* modeline
= &res
->modelines
[j
];
708 if (modeline
->x1
== xprev
&& modeline
->y1
== yprev
) {
709 modeline
->x1
= modeline
->x2
= x
-1;
710 modeline
->y1
= modeline
->y2
= y
-1;
712 gtf_timings(x
, y
, freqs
[j
], &modeline
->clock
,
713 &modeline
->hsyncstart
, &modeline
->hsyncend
,
714 &modeline
->hblank
, &modeline
->vsyncstart
,
715 &modeline
->vsyncend
, &modeline
->vblank
);
718 modeline
->htotal
= htotal
;
720 modeline
->htotal
= modeline
->hblank
;
723 modeline
->vtotal
= vtotal
;
725 modeline
->vtotal
= modeline
->vblank
;
732 vbios_resolution_type3
* res
= map_type3_resolution(map
, map
->mode_table
[i
].resolution
);
734 xprev
= res
->modelines
[0].x1
;
735 yprev
= res
->modelines
[0].y1
;
737 for (j
=0; j
< 3; j
++) {
738 vbios_modeline_type3
* modeline
= &res
->modelines
[j
];
740 if (modeline
->x1
== xprev
&& modeline
->y1
== yprev
) {
741 modeline
->x1
= modeline
->x2
= x
-1;
742 modeline
->y1
= modeline
->y2
= y
-1;
744 gtf_timings(x
, y
, freqs
[j
], &modeline
->clock
,
745 &modeline
->hsyncstart
, &modeline
->hsyncend
,
746 &modeline
->hblank
, &modeline
->vsyncstart
,
747 &modeline
->vsyncend
, &modeline
->vblank
);
749 modeline
->htotal
= htotal
;
751 modeline
->htotal
= modeline
->hblank
;
753 modeline
->vtotal
= vtotal
;
755 modeline
->vtotal
= modeline
->vblank
;
757 modeline
->timing_h
= y
-1;
758 modeline
->timing_v
= x
-1;
770 static void display_map_info(vbios_map
* map
) {
771 printf("Chipset: %s\n", chipset_type_names
[map
->chipset
]);
772 printf("BIOS: %s\n", bios_type_names
[map
->bios
]);
774 printf("Mode Table Offset: $C0000 + $%x\n", ((cardinal
)map
->mode_table
) - ((cardinal
)map
->bios_ptr
));
775 printf("Mode Table Entries: %u\n", map
->mode_table_size
);
779 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
) {
782 *list
= *mode
= *x
= *y
= *raw
= *htotal
= *vtotal
= 0;
785 *forced_chipset
= CT_UNKWN
;
788 if ((argc
> index
) && !strcmp(argv
[index
], "-c")) {
795 if (!strcmp(argv
[index
], "845")) {
796 *forced_chipset
= CT_845G
;
798 else if (!strcmp(argv
[index
], "855")) {
799 *forced_chipset
= CT_855GM
;
801 else if (!strcmp(argv
[index
], "865")) {
802 *forced_chipset
= CT_865G
;
804 else if (!strcmp(argv
[index
], "915G")) {
805 *forced_chipset
= CT_915G
;
807 else if (!strcmp(argv
[index
], "915GM")) {
808 *forced_chipset
= CT_915GM
;
810 else if (!strcmp(argv
[index
], "945G")) {
811 *forced_chipset
= CT_945G
;
813 else if (!strcmp(argv
[index
], "945GM")) {
814 *forced_chipset
= CT_945GM
;
816 else if (!strcmp(argv
[index
], "945GME")) {
817 *forced_chipset
= CT_945GME
;
819 else if (!strcmp(argv
[index
], "946GZ")) {
820 *forced_chipset
= CT_946GZ
;
822 else if (!strcmp(argv
[index
], "G965")) {
823 *forced_chipset
= CT_G965
;
825 else if (!strcmp(argv
[index
], "Q965")) {
826 *forced_chipset
= CT_Q965
;
828 else if (!strcmp(argv
[index
], "965GM")) {
829 *forced_chipset
= CT_965GM
;
831 else if (!strcmp(argv
[index
], "G33")) {
832 *forced_chipset
= CT_G33
;
834 else if (!strcmp(argv
[index
], "Q35")) {
835 *forced_chipset
= CT_Q35
;
837 else if (!strcmp(argv
[index
], "Q33")) {
838 *forced_chipset
= CT_Q33
;
840 else if (!strcmp(argv
[index
], "500GMA")) {
841 *forced_chipset
= CT_500GMA
;
844 *forced_chipset
= CT_UNKWN
;
854 if ((argc
> index
) && !strcmp(argv
[index
], "-l")) {
863 if ((argc
> index
) && !strcmp(argv
[index
], "-r")) {
872 if (argc
-index
< 3 || argc
-index
> 6) {
876 *mode
= (cardinal
) strtol(argv
[index
], NULL
, 16);
877 *x
= (cardinal
)atoi(argv
[index
+1]);
878 *y
= (cardinal
)atoi(argv
[index
+2]);
881 if (argc
-index
> 3) {
882 *bp
= (cardinal
)atoi(argv
[index
+3]);
888 if (argc
-index
> 4) {
889 *htotal
= (cardinal
)atoi(argv
[index
+4]);
895 if (argc
-index
> 5) {
896 *vtotal
= (cardinal
)atoi(argv
[index
+5]);
905 static void usage(void) {
906 printf("Usage: 915resolution [-c chipset] [-l] [mode X Y] [bits/pixel] [htotal] [vtotal]\n");
907 printf(" Set the resolution to XxY for a video mode\n");
908 printf(" Bits per pixel are optional. htotal/vtotal settings are additionally optional.\n");
909 printf(" Options:\n");
910 printf(" -c force chipset type (THIS IS USED FOR DEBUG PURPOSES)\n");
911 printf(" -l display the modes found in the video BIOS\n");
912 printf(" -r display the modes found in the video BIOS in raw mode (THIS IS USED FOR DEBUG PURPOSES)\n");
915 static int main_915 (int argc
, char *argv
[])
918 cardinal list
, mode
, x
, y
, bp
, raw
, htotal
, vtotal
;
919 chipset_type forced_chipset
;
921 printf("Intel 800/900 Series VBIOS Hack : version %s\n\n", RES915_VERSION
);
923 if (parse_args(argc
, argv
, &forced_chipset
, &list
, &mode
, &x
, &y
, &bp
, &raw
, &htotal
, &vtotal
) == -1) {
928 map
= open_vbios(forced_chipset
);
929 display_map_info(map
);
934 list_modes(map
, raw
);
937 if (mode
!=0 && x
!=0 && y
!=0) {
940 set_mode(map
, mode
, x
, y
, bp
, htotal
, vtotal
);
944 printf("Patch mode %02x to resolution %dx%d complete\n", mode
, x
, y
);
947 list_modes(map
, raw
);
961 grub_cmd_915resolution (grub_command_t cmd
__attribute__ ((unused
)),
962 int argc
, char *argv
[])
964 return main_915 (argc
, argv
);
967 static grub_command_t cmd
;
969 GRUB_MOD_INIT(915resolution
)
971 cmd
= grub_register_command ("915resolution", grub_cmd_915resolution
,
972 "915resolution", "Intel VBE editor");
975 GRUB_MOD_FINI(915resolution
)
977 grub_unregister_command (cmd
);