2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2009 Jared D. McNeill <jmcneill@invisible.ca>
6 * Copyright 2020 Toomas Soome
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 #include <sys/param.h>
35 #include <machine/psl.h>
36 #include <machine/cpufunc.h>
38 #include <bootstrap.h>
41 #include <dev/vt/hw/vga/vt_vga_reg.h>
46 * VESA BIOS Extensions routines
49 static struct vbeinfoblock *vbe;
50 static struct modeinfoblock *vbe_mode;
52 static uint16_t *vbe_mode_list;
53 static size_t vbe_mode_list_size;
54 struct vesa_edid_info *edid_info = NULL;
56 /* The default VGA color palette format is 6 bits per primary color. */
57 int palette_format = 6;
59 #define VESA_MODE_BASE 0x100
62 * palette array for 8-bit indexed colors. In this case, cmap does store
63 * index and pe8 does store actual RGB. This is needed because we may
64 * not be able to read palette data from hardware.
66 struct paletteentry *pe8 = NULL;
68 static struct named_resolution {
103 vbe_resolution_compare(struct named_resolution *res, const char *cmp)
106 if (strcasecmp(res->name, cmp) == 0)
108 if (res->alias != NULL && strcasecmp(res->alias, cmp) == 0)
114 vbe_get_max_resolution(int *width, int *height)
116 struct named_resolution *res;
118 char *height_start, *width_start;
121 *width = *height = 0;
122 maxres = getenv("vbe_max_resolution");
123 /* No max_resolution set? Bail out; choose highest resolution */
126 /* See if it matches one of our known resolutions */
127 for (idx = 0; idx < nitems(resolutions); ++idx) {
128 res = &resolutions[idx];
129 if (vbe_resolution_compare(res, maxres)) {
131 *height = res->height;
135 /* Not a known resolution, try to parse it; make a copy we can modify */
136 maxres = strdup(maxres);
139 height_start = strchr(maxres, 'x');
140 if (height_start == NULL) {
144 width_start = maxres;
146 /* Errors from this will effectively mean "no max" */
147 *width = (int)strtol(width_start, NULL, 0);
148 *height = (int)strtol(height_start, NULL, 0);
153 vga_get_reg(int reg, int index)
155 return (inb(reg + index));
159 vga_get_atr(int reg, int i)
163 (void) inb(reg + VGA_GEN_INPUT_STAT_1);
164 outb(reg + VGA_AC_WRITE, i);
165 ret = inb(reg + VGA_AC_READ);
167 (void) inb(reg + VGA_GEN_INPUT_STAT_1);
173 vga_set_atr(int reg, int i, int v)
175 (void) inb(reg + VGA_GEN_INPUT_STAT_1);
176 outb(reg + VGA_AC_WRITE, i);
177 outb(reg + VGA_AC_WRITE, v);
179 (void) inb(reg + VGA_GEN_INPUT_STAT_1);
183 vga_set_indexed(int reg, int indexreg, int datareg, uint8_t index, uint8_t val)
185 outb(reg + indexreg, index);
186 outb(reg + datareg, val);
190 vga_get_indexed(int reg, int indexreg, int datareg, uint8_t index)
192 outb(reg + indexreg, index);
193 return (inb(reg + datareg));
197 vga_get_crtc(int reg, int i)
199 return (vga_get_indexed(reg, VGA_CRTC_ADDRESS, VGA_CRTC_DATA, i));
203 vga_set_crtc(int reg, int i, int v)
205 vga_set_indexed(reg, VGA_CRTC_ADDRESS, VGA_CRTC_DATA, i, v);
209 vga_get_seq(int reg, int i)
211 return (vga_get_indexed(reg, VGA_SEQ_ADDRESS, VGA_SEQ_DATA, i));
215 vga_set_seq(int reg, int i, int v)
217 vga_set_indexed(reg, VGA_SEQ_ADDRESS, VGA_SEQ_DATA, i, v);
221 vga_get_grc(int reg, int i)
223 return (vga_get_indexed(reg, VGA_GC_ADDRESS, VGA_GC_DATA, i));
227 vga_set_grc(int reg, int i, int v)
229 vga_set_indexed(reg, VGA_GC_ADDRESS, VGA_GC_DATA, i, v);
233 * Return true when this controller is VGA compatible.
241 return ((vbe->Capabilities & VBE_CAP_NONVGA) == 0);
244 /* Actually assuming mode 3. */
246 bios_set_text_mode(int mode)
250 if (vbe->Capabilities & VBE_CAP_DAC8) {
254 * The mode change should reset the palette format to
255 * 6 bits, but apparently some systems do fail with 8-bit
256 * palette, so we switch to 6-bit here.
259 (void) biosvbe_palette_format(&m);
264 v86.eax = mode; /* set VGA text mode */
266 atr = vga_get_atr(VGA_REG_BASE, VGA_AC_MODE_CONTROL);
267 atr &= ~VGA_AC_MC_BI;
268 atr &= ~VGA_AC_MC_ELG;
269 vga_set_atr(VGA_REG_BASE, VGA_AC_MODE_CONTROL, atr);
271 gfx_state.tg_mode = mode;
272 gfx_state.tg_fb_type = FB_TEXT;
273 gfx_state.tg_fb.fb_height = TEXT_ROWS;
274 gfx_state.tg_fb.fb_width = TEXT_COLS;
276 gfx_state.tg_fb.fb_mask_red = (1 << palette_format) - 1 << 16;
277 gfx_state.tg_fb.fb_mask_green = (1 << palette_format) - 1 << 8;
278 gfx_state.tg_fb.fb_mask_blue = (1 << palette_format) - 1 << 0;
279 gfx_state.tg_ctype = CT_INDEXED;
280 env_setenv("screen.textmode", EV_VOLATILE | EV_NOHOOK, "1", NULL, NULL);
283 /* Function 00h - Return VBE Controller Information */
285 biosvbe_info(struct vbeinfoblock *vbep)
287 struct vbeinfoblock *rvbe;
293 rvbe = bio_alloc(sizeof(*rvbe));
297 /* Now check if we have vesa. */
298 memset(rvbe, 0, sizeof (*vbe));
299 memcpy(rvbe->VbeSignature, "VBE2", 4);
304 v86.es = VTOPSEG(rvbe);
305 v86.edi = VTOPOFF(rvbe);
307 ret = v86.eax & 0xffff;
309 if (ret != VBE_SUCCESS)
312 if (memcmp(rvbe->VbeSignature, "VESA", 4) != 0) {
316 bcopy(rvbe, vbep, sizeof(*vbep));
318 bio_free(rvbe, sizeof(*rvbe));
322 /* Function 01h - Return VBE Mode Information */
324 biosvbe_get_mode_info(int mode, struct modeinfoblock *mi)
326 struct modeinfoblock *rmi;
329 rmi = bio_alloc(sizeof(*rmi));
337 v86.es = VTOPSEG(rmi);
338 v86.edi = VTOPOFF(rmi);
341 ret = v86.eax & 0xffff;
342 if (ret != VBE_SUCCESS)
344 bcopy(rmi, mi, sizeof(*rmi));
346 bio_free(rmi, sizeof(*rmi));
350 /* Function 02h - Set VBE Mode */
352 biosvbe_set_mode(int mode, struct crtciinfoblock *ci)
356 if (vbe->Capabilities & VBE_CAP_DAC8) {
360 * The mode change should reset the palette format to
361 * 6 bits, but apparently some systems do fail with 8-bit
362 * palette, so we switch to 6-bit here.
365 if (biosvbe_palette_format(&m) == VBE_SUCCESS)
371 v86.ebx = mode | 0x4000; /* set linear FB bit */
372 v86.es = VTOPSEG(ci);
373 v86.edi = VTOPOFF(ci);
375 rv = v86.eax & 0xffff;
376 if (vbe->Capabilities & VBE_CAP_DAC8) {
379 /* Switch to 8-bits per primary color. */
381 if (biosvbe_palette_format(&m) == VBE_SUCCESS)
384 env_setenv("screen.textmode", EV_VOLATILE | EV_NOHOOK, "0", NULL, NULL);
388 /* Function 03h - Get VBE Mode */
390 biosvbe_get_mode(int *mode)
396 *mode = v86.ebx & 0x3fff; /* Bits 0-13 */
397 return (v86.eax & 0xffff);
400 /* Function 08h - Set/Get DAC Palette Format */
402 biosvbe_palette_format(int *format)
409 *format = (v86.ebx >> 8) & 0xff;
410 return (v86.eax & 0xffff);
413 /* Function 09h - Set/Get Palette Data */
415 biosvbe_palette_data(int mode, int reg, struct paletteentry *pe)
423 v86.es = VTOPSEG(pe);
424 v86.edi = VTOPOFF(pe);
426 return (v86.eax & 0xffff);
430 * Function 15h BL=00h - Report VBE/DDC Capabilities
432 * int biosvbe_ddc_caps(void)
433 * return: VBE/DDC capabilities
436 biosvbe_ddc_caps(void)
440 v86.eax = 0x4f15; /* display identification extensions */
441 v86.ebx = 0; /* report DDC capabilities */
442 v86.ecx = 0; /* controller unit number (00h = primary) */
446 if (VBE_ERROR(v86.eax & 0xffff))
448 return (v86.ebx & 0xffff);
451 /* Function 11h BL=01h - Flat Panel status */
453 biosvbe_ddc_read_flat_panel_info(void *buf)
457 v86.eax = 0x4f11; /* Flat Panel Interface extensions */
458 v86.ebx = 1; /* Return Flat Panel Information */
459 v86.es = VTOPSEG(buf);
460 v86.edi = VTOPOFF(buf);
462 return (v86.eax & 0xffff);
465 /* Function 15h BL=01h - Read EDID */
467 biosvbe_ddc_read_edid(int blockno, void *buf)
471 v86.eax = 0x4f15; /* display identification extensions */
472 v86.ebx = 1; /* read EDID */
473 v86.ecx = 0; /* controller unit number (00h = primary) */
475 v86.es = VTOPSEG(buf);
476 v86.edi = VTOPOFF(buf);
478 return (v86.eax & 0xffff);
482 vbe_mode_is_supported(struct modeinfoblock *mi)
484 if ((mi->ModeAttributes & 0x01) == 0)
485 return (0); /* mode not supported by hardware */
486 if ((mi->ModeAttributes & 0x08) == 0)
487 return (0); /* linear fb not available */
488 if ((mi->ModeAttributes & 0x10) == 0)
489 return (0); /* text mode */
490 if (mi->NumberOfPlanes != 1)
491 return (0); /* planar mode not supported */
492 if (mi->MemoryModel != 0x04 /* Packed pixel */ &&
493 mi->MemoryModel != 0x06 /* Direct Color */)
494 return (0); /* unsupported pixel format */
503 printf("VBE not available\n");
510 mode_set(struct env_var *ev, int flags __unused, const void *value)
514 if (strcmp(ev->ev_name, "screen.textmode") == 0) {
521 v = strtoul(value, &end, 0);
522 if (errno != 0 || *(char *)value == '\0' || *end != '\0' ||
525 env_setenv("screen.textmode", EV_VOLATILE | EV_NOHOOK,
529 bios_text_font(true);
530 bios_set_text_mode(VGA_TEXT_MODE);
531 (void) cons_update_mode(false);
534 } else if (strcmp(ev->ev_name, "vbe_max_resolution") == 0) {
535 env_setenv("vbe_max_resolution", EV_VOLATILE | EV_NOHOOK,
541 mode = vbe_default_mode();
542 if (gfx_state.tg_mode != mode) {
544 bios_text_font(false);
546 cons_update_mode(true);
552 vbe_farptr(uint32_t farptr)
554 return (PTOV((((farptr & 0xffff0000) >> 12) + (farptr & 0xffff))));
562 /* First set FB for text mode. */
563 gfx_state.tg_fb_type = FB_TEXT;
564 gfx_state.tg_fb.fb_height = TEXT_ROWS;
565 gfx_state.tg_fb.fb_width = TEXT_COLS;
566 gfx_state.tg_ctype = CT_INDEXED;
567 gfx_state.tg_mode = 3;
569 env_setenv("screen.textmode", EV_VOLATILE, "1", mode_set,
571 env_setenv("vbe_max_resolution", EV_VOLATILE, NULL, mode_set,
575 vbe = malloc(sizeof(*vbe));
580 if (vbe_mode == NULL) {
581 vbe_mode = malloc(sizeof(*vbe_mode));
582 if (vbe_mode == NULL) {
588 if (biosvbe_info(vbe) != VBE_SUCCESS) {
597 * Copy mode list. We must do this because some systems do
598 * corrupt the provided list (vbox 6.1 is one example).
600 p = ml = vbe_farptr(vbe->VideoModePtr);
601 while(*p++ != 0xFFFF)
604 vbe_mode_list_size = (uintptr_t)p - (uintptr_t)ml;
607 * Since vbe_init() is used only once at very start of the loader,
608 * we assume malloc will not fail there, but in case it does,
609 * we point vbe_mode_list to memory pointed by VideoModePtr.
611 vbe_mode_list = malloc(vbe_mode_list_size);
612 if (vbe_mode_list == NULL)
615 bcopy(ml, vbe_mode_list, vbe_mode_list_size);
617 /* reset VideoModePtr, to make sure, we only do use vbe_mode_list. */
618 vbe->VideoModePtr = 0;
620 /* vbe_set_mode() will set up the rest. */
626 return (gfx_state.tg_fb_type == FB_VBE);
630 vbe_set_palette(const struct paletteentry *entry, size_t slot)
632 struct paletteentry pe;
635 if (!vbe_check() || (vbe->Capabilities & VBE_CAP_DAC8) == 0)
638 if (gfx_state.tg_ctype != CT_INDEXED) {
642 pe.Blue = entry->Blue;
643 pe.Green = entry->Green;
645 pe.Reserved = entry->Reserved;
647 if (vbe->Capabilities & VBE_CAP_SNOW)
652 ret = biosvbe_palette_data(mode, slot, &pe);
654 return (ret == VBE_SUCCESS ? 0 : 1);
660 return (gfx_state.tg_mode);
664 vbe_set_mode(int modenum)
666 struct modeinfoblock mi;
672 ret = biosvbe_get_mode_info(modenum, &mi);
673 if (VBE_ERROR(ret)) {
674 printf("mode 0x%x invalid\n", modenum);
678 if (!vbe_mode_is_supported(&mi)) {
679 printf("mode 0x%x not supported\n", modenum);
683 /* calculate bytes per pixel */
684 switch (mi.BitsPerPixel) {
692 printf("BitsPerPixel %d is not supported\n", mi.BitsPerPixel);
696 ret = biosvbe_set_mode(modenum, NULL);
697 if (VBE_ERROR(ret)) {
698 printf("mode 0x%x could not be set\n", modenum);
702 gfx_state.tg_mode = modenum;
703 gfx_state.tg_fb_type = FB_VBE;
704 /* make sure we have current MI in vbestate */
705 memcpy(vbe_mode, &mi, sizeof (*vbe_mode));
707 gfx_state.tg_fb.fb_addr = (uint64_t)mi.PhysBasePtr & 0xffffffff;
708 gfx_state.tg_fb.fb_height = mi.YResolution;
709 gfx_state.tg_fb.fb_width = mi.XResolution;
710 gfx_state.tg_fb.fb_bpp = mi.BitsPerPixel;
712 /* Bytes per pixel */
713 bpp = roundup2(mi.BitsPerPixel, NBBY) / NBBY;
715 /* vbe_mode_is_supported() excludes the rest */
716 switch (mi.MemoryModel) {
718 gfx_state.tg_ctype = CT_INDEXED;
721 gfx_state.tg_ctype = CT_RGB;
725 #define COLOR_MASK(size, pos) (((1 << size) - 1) << pos)
726 if (gfx_state.tg_ctype == CT_INDEXED) {
727 gfx_state.tg_fb.fb_mask_red = COLOR_MASK(palette_format, 16);
728 gfx_state.tg_fb.fb_mask_green = COLOR_MASK(palette_format, 8);
729 gfx_state.tg_fb.fb_mask_blue = COLOR_MASK(palette_format, 0);
730 } else if (vbe->VbeVersion >= 0x300) {
731 gfx_state.tg_fb.fb_mask_red =
732 COLOR_MASK(mi.LinRedMaskSize, mi.LinRedFieldPosition);
733 gfx_state.tg_fb.fb_mask_green =
734 COLOR_MASK(mi.LinGreenMaskSize, mi.LinGreenFieldPosition);
735 gfx_state.tg_fb.fb_mask_blue =
736 COLOR_MASK(mi.LinBlueMaskSize, mi.LinBlueFieldPosition);
738 gfx_state.tg_fb.fb_mask_red =
739 COLOR_MASK(mi.RedMaskSize, mi.RedFieldPosition);
740 gfx_state.tg_fb.fb_mask_green =
741 COLOR_MASK(mi.GreenMaskSize, mi.GreenFieldPosition);
742 gfx_state.tg_fb.fb_mask_blue =
743 COLOR_MASK(mi.BlueMaskSize, mi.BlueFieldPosition);
745 gfx_state.tg_fb.fb_mask_reserved = ~(gfx_state.tg_fb.fb_mask_red |
746 gfx_state.tg_fb.fb_mask_green |
747 gfx_state.tg_fb.fb_mask_blue);
749 if (vbe->VbeVersion >= 0x300)
750 gfx_state.tg_fb.fb_stride = mi.LinBytesPerScanLine / bpp;
752 gfx_state.tg_fb.fb_stride = mi.BytesPerScanLine / bpp;
754 gfx_state.tg_fb.fb_size = mi.YResolution * gfx_state.tg_fb.fb_stride *
761 * Verify existance of mode number or find mode by
762 * dimensions. If depth is not given, walk values 32, 24, 16, 8.
765 vbe_find_mode_xydm(int x, int y, int depth, int m)
767 struct modeinfoblock mi;
770 int idx, nentries, i;
772 memset(vbe, 0, sizeof (*vbe));
773 if (biosvbe_info(vbe) != VBE_SUCCESS)
778 else if (depth == -1)
783 nentries = vbe_mode_list_size / sizeof(*vbe_mode_list);
785 for (idx = 0; idx < nentries; idx++) {
786 mode = vbe_mode_list[idx];
790 if (biosvbe_get_mode_info(mode, &mi) != VBE_SUCCESS) {
794 /* we only care about linear modes here */
795 if (vbe_mode_is_supported(&mi) == 0)
805 if (mi.XResolution == x &&
806 mi.YResolution == y &&
807 mi.BitsPerPixel == i)
820 vbe_find_mode(char *str)
824 if (!gfx_parse_mode_str(str, &x, &y, &depth))
827 return (vbe_find_mode_xydm(x, y, depth, -1));
831 vbe_dump_mode(int modenum, struct modeinfoblock *mi)
833 printf("0x%x=%dx%dx%d", modenum,
834 mi->XResolution, mi->YResolution, mi->BitsPerPixel);
838 vbe_get_edid(edid_res_list_t *res)
840 struct vesa_edid_info *edidp;
841 const uint8_t magic[] = EDID_MAGIC;
845 if (edid_info != NULL)
846 return (gfx_get_edid_resolution(edid_info, res));
848 ddc_caps = biosvbe_ddc_caps();
853 edidp = bio_alloc(sizeof(*edidp));
856 memset(edidp, 0, sizeof(*edidp));
858 if (VBE_ERROR(biosvbe_ddc_read_edid(0, edidp)))
861 if (memcmp(edidp, magic, sizeof(magic)) != 0)
864 /* Unknown EDID version. */
865 if (edidp->header.version != 1)
868 ret = gfx_get_edid_resolution(edidp, res);
869 edid_info = malloc(sizeof(*edid_info));
870 if (edid_info != NULL)
871 memcpy(edid_info, edidp, sizeof (*edid_info));
873 bio_free(edidp, sizeof(*edidp));
878 vbe_get_flatpanel(uint32_t *pwidth, uint32_t *pheight)
880 struct vesa_flat_panel_info *fp_info;
883 fp_info = bio_alloc(sizeof (*fp_info));
886 memset(fp_info, 0, sizeof (*fp_info));
888 if (VBE_ERROR(biosvbe_ddc_read_flat_panel_info(fp_info)))
891 *pwidth = fp_info->HSize;
892 *pheight = fp_info->VSize;
896 bio_free(fp_info, sizeof (*fp_info));
901 vbe_print_memory(unsigned vmem)
906 if (vmem >= 10240000) {
909 } else if (vmem >= 10000) {
913 printf("Total memory: %u%cB\n", vmem, unit);
917 vbe_print_vbe_info(struct vbeinfoblock *vbep)
919 char *oemstring = "";
920 char *oemvendor = "", *oemproductname = "", *oemproductrev = "";
922 if (vbep->OemStringPtr != 0)
923 oemstring = vbe_farptr(vbep->OemStringPtr);
925 if (vbep->OemVendorNamePtr != 0)
926 oemvendor = vbe_farptr(vbep->OemVendorNamePtr);
928 if (vbep->OemProductNamePtr != 0)
929 oemproductname = vbe_farptr(vbep->OemProductNamePtr);
931 if (vbep->OemProductRevPtr != 0)
932 oemproductrev = vbe_farptr(vbep->OemProductRevPtr);
934 printf("VESA VBE Version %d.%d\n%s\n", vbep->VbeVersion >> 8,
935 vbep->VbeVersion & 0xF, oemstring);
937 if (vbep->OemSoftwareRev != 0) {
938 printf("OEM Version %d.%d, %s (%s, %s)\n",
939 vbep->OemSoftwareRev >> 8, vbep->OemSoftwareRev & 0xF,
940 oemvendor, oemproductname, oemproductrev);
942 vbe_print_memory(vbep->TotalMemory << 16);
943 printf("Number of Image Pages: %d\n", vbe_mode->LinNumberOfImagePages);
946 /* List available modes, filter by depth. If depth is -1, list all. */
948 vbe_modelist(int depth)
950 struct modeinfoblock mi;
952 int nmodes, idx, nentries;
954 uint32_t width, height;
957 struct resolution *rp;
962 ddc_caps = biosvbe_ddc_caps();
971 edid = vbe_get_edid(&res);
974 while ((rp = TAILQ_FIRST(&res)) != NULL) {
975 printf(" %dx%d", rp->width, rp->height);
976 TAILQ_REMOVE(&res, rp, next);
981 printf(": no EDID information\n");
985 if (vbe_get_flatpanel(&width, &height))
986 printf(": Panel %dx%d\n", width, height);
989 memset(vbe, 0, sizeof (*vbe));
990 memcpy(vbe->VbeSignature, "VBE2", 4);
991 if (biosvbe_info(vbe) != VBE_SUCCESS)
993 if (memcmp(vbe->VbeSignature, "VESA", 4) != 0)
996 vbe_print_vbe_info(vbe);
999 nentries = vbe_mode_list_size / sizeof(*vbe_mode_list);
1000 for (idx = 0; idx < nentries; idx++) {
1001 mode = vbe_mode_list[idx];
1005 if (biosvbe_get_mode_info(mode, &mi) != VBE_SUCCESS)
1008 /* we only care about linear modes here */
1009 if (vbe_mode_is_supported(&mi) == 0)
1012 /* apply requested filter */
1013 if (depth != -1 && mi.BitsPerPixel != depth)
1016 if (nmodes % 4 == 0)
1021 vbe_dump_mode(mode, &mi);
1027 printf("none found");
1032 vbe_print_mode(bool verbose __unused)
1034 int nc, mode, i, rc;
1038 memset(vbe, 0, sizeof (*vbe));
1039 if (biosvbe_info(vbe) != VBE_SUCCESS)
1042 vbe_print_vbe_info(vbe);
1044 if (biosvbe_get_mode(&mode) != VBE_SUCCESS) {
1045 printf("Error getting current VBE mode\n");
1049 if (biosvbe_get_mode_info(mode, vbe_mode) != VBE_SUCCESS ||
1050 vbe_mode_is_supported(vbe_mode) == 0) {
1051 printf("VBE mode (0x%x) is not framebuffer mode\n", mode);
1055 printf("\nCurrent VBE mode: ");
1056 vbe_dump_mode(mode, vbe_mode);
1059 printf("%ux%ux%u, stride=%u\n",
1060 gfx_state.tg_fb.fb_width,
1061 gfx_state.tg_fb.fb_height,
1062 gfx_state.tg_fb.fb_bpp,
1063 gfx_state.tg_fb.fb_stride *
1064 (roundup2(gfx_state.tg_fb.fb_bpp, NBBY) / NBBY));
1065 printf(" frame buffer: address=%jx, size=%jx\n",
1066 (uintmax_t)gfx_state.tg_fb.fb_addr,
1067 (uintmax_t)gfx_state.tg_fb.fb_size);
1069 if (vbe_mode->MemoryModel == 0x6) {
1070 printf(" color mask: R=%08x, G=%08x, B=%08x\n",
1071 gfx_state.tg_fb.fb_mask_red,
1072 gfx_state.tg_fb.fb_mask_green,
1073 gfx_state.tg_fb.fb_mask_blue);
1075 for (i = 0; i < nc; i++) {
1076 printf("%d: R=%02x, G=%02x, B=%02x %08x", i,
1077 (cmap[i] & gfx_state.tg_fb.fb_mask_red) >>
1078 ffs(gfx_state.tg_fb.fb_mask_red) - 1,
1079 (cmap[i] & gfx_state.tg_fb.fb_mask_green) >>
1080 ffs(gfx_state.tg_fb.fb_mask_green) - 1,
1081 (cmap[i] & gfx_state.tg_fb.fb_mask_blue) >>
1082 ffs(gfx_state.tg_fb.fb_mask_blue) - 1, cmap[i]);
1083 if (pager_output("\n") != 0)
1090 mode = 1; /* get DAC palette width */
1091 rc = biosvbe_palette_format(&mode);
1092 if (rc != VBE_SUCCESS)
1095 printf(" palette format: %x bits per primary\n", mode);
1100 for (i = 0; i < nc; i++) {
1101 printf("%d: R=%02x, G=%02x, B=%02x", i,
1102 pe8[i].Red, pe8[i].Green, pe8[i].Blue);
1103 if (pager_output("\n") != 0)
1110 * Try EDID preferred mode, if EDID or the suggested mode is not available,
1111 * then try flat panel information.
1112 * Fall back to VBE_DEFAULT_MODE.
1115 vbe_default_mode(void)
1117 edid_res_list_t res;
1118 struct resolution *rp;
1120 uint32_t width, height;
1123 vbe_get_max_resolution(&width, &height);
1124 if (width != 0 && height != 0)
1125 modenum = vbe_find_mode_xydm(width, height, -1, -1);
1128 if (vbe_get_edid(&res)) {
1129 while ((rp = TAILQ_FIRST(&res)) != NULL) {
1131 modenum = vbe_find_mode_xydm(
1132 rp->width, rp->height, -1, -1);
1134 TAILQ_REMOVE(&res, rp, next);
1140 vbe_get_flatpanel(&width, &height)) {
1141 modenum = vbe_find_mode_xydm(width, height, -1, -1);
1144 /* Still no mode? Fall back to default. */
1146 modenum = vbe_find_mode(VBE_DEFAULT_MODE);
1150 COMMAND_SET(vbe, "vbe", "vesa framebuffer mode management", command_vesa);
1153 command_vesa(int argc, char *argv[])
1156 int modenum = -1, n;
1164 if (strcmp(argv[1], "list") == 0) {
1166 if (argc != 2 && argc != 3)
1172 n = strtoul(arg, &cp, 0);
1173 if (errno != 0 || *arg == '\0' || cp[0] != '\0') {
1174 snprintf(command_errbuf,
1175 sizeof (command_errbuf),
1176 "depth should be an integer");
1184 if (strcmp(argv[1], "get") == 0) {
1185 bool verbose = false;
1188 if (argc > 3 || strcmp(argv[2], "-v") != 0)
1192 vbe_print_mode(verbose);
1196 if (strcmp(argv[1], "off") == 0) {
1200 if (gfx_state.tg_mode == VGA_TEXT_MODE)
1204 bios_text_font(true);
1205 bios_set_text_mode(VGA_TEXT_MODE);
1206 cons_update_mode(false);
1210 if (strcmp(argv[1], "on") == 0) {
1214 modenum = vbe_default_mode();
1216 snprintf(command_errbuf, sizeof (command_errbuf),
1217 "%s: no suitable VBE mode number found", argv[0]);
1220 } else if (strcmp(argv[1], "set") == 0) {
1224 if (strncmp(argv[2], "0x", 2) == 0) {
1227 n = strtoul(arg, &cp, 0);
1228 if (errno != 0 || *arg == '\0' || cp[0] != '\0') {
1229 snprintf(command_errbuf,
1230 sizeof (command_errbuf),
1231 "mode should be an integer");
1234 modenum = vbe_find_mode_xydm(0, 0, 0, n);
1235 } else if (strchr(argv[2], 'x') != NULL) {
1236 modenum = vbe_find_mode(argv[2]);
1243 snprintf(command_errbuf, sizeof (command_errbuf),
1244 "%s: mode %s not supported by firmware\n",
1249 if (modenum >= VESA_MODE_BASE) {
1250 if (gfx_state.tg_mode != modenum) {
1252 bios_text_font(false);
1253 vbe_set_mode(modenum);
1254 cons_update_mode(true);
1258 snprintf(command_errbuf, sizeof (command_errbuf),
1259 "%s: mode %s is not framebuffer mode\n", argv[0], argv[2]);
1264 snprintf(command_errbuf, sizeof (command_errbuf),
1265 "usage: %s on | off | get | list [depth] | "
1266 "set <display or VBE mode number>", argv[0]);