The frame buffer now operates correctly in the cases where the frame
buffer pitch is not equal to (fb_pixels_width * bytes_per_pixel).

Reported by: Alperen Erkan <[email protected]>
---
 i386/i386at/fb_disp.c | 23 ++++++++++++++++++-----
 i386/i386at/fb_disp.h |  4 +++-
 i386/i386at/immc.c    |  9 ++++++---
 3 files changed, 27 insertions(+), 9 deletions(-)

diff --git a/i386/i386at/fb_disp.c b/i386/i386at/fb_disp.c
index 315bcce2..0b00e5a5 100644
--- a/i386/i386at/fb_disp.c
+++ b/i386/i386at/fb_disp.c
@@ -120,6 +120,7 @@ static uint8_t vgafont14[] = {
 
 static void    *fb_addr;
 static uint32_t fb_pixels_w, fb_pixels_h;
+static uint32_t fb_pitch;
 static uint32_t fb_chars_w, fb_chars_h;
 static uint8_t *fb_chars_buf;
 static uint8_t  fb_bpp; /* bytes per pixel (not bits) */
@@ -137,7 +138,9 @@ static const uint32_t fb_cursor_h = 2;
 #define fb_colbyte_black 0x00
 
 boolean_t
-fb_disp_init (void *addr, uint32_t width, uint32_t height, uint8_t bpp)
+fb_disp_init (void *addr,
+             uint32_t pitch, uint32_t width, uint32_t height,
+             uint8_t bpp)
 {
   if (bpp != 24 && bpp != 32)
     return FALSE;
@@ -148,6 +151,10 @@ fb_disp_init (void *addr, uint32_t width, uint32_t height, 
uint8_t bpp)
 
   fb_addr = addr;
 
+  if (pitch < (fb_pixels_w * fb_bpp))
+    return FALSE;
+
+  fb_pitch = pitch;
   fb_chars_w = fb_disp_max_cols ();
   fb_chars_h = fb_disp_max_rows ();
 
@@ -186,7 +193,7 @@ inline
 static uint8_t *
 fb_xyb2addr (short x, short y)
 {
-  return (fb_addr + (fb_bpp * (x + (y * fb_pixels_w))));
+  return (fb_addr + (fb_bpp * x) + (y * fb_pitch));
 }
 
 uint32_t
@@ -237,7 +244,7 @@ fb_paint_cursor (fb_disp_pos_t pos, uint8_t colour)
          addr[2] = colour;
          addr += fb_bpp;
        }
-      addr += (fb_bpp * (fb_pixels_w - fb_font_w));
+      addr += (fb_pitch - (fb_font_w * fb_bpp));
     }
 }
 
@@ -269,7 +276,7 @@ fb_drawchar (fb_disp_pos_t x, fb_disp_pos_t y, char ch)
          to[2] = colour;
          to += fb_bpp;
        }
-      to += (fb_bpp * (fb_pixels_w - fb_font_w));
+      to += (fb_pitch - (fb_font_w * fb_bpp));
       from++;
     }
 }
@@ -382,5 +389,11 @@ fb_disp_cls (void)
 {
   assert (fb_addr != NULL);
 
-  memset (fb_addr, fb_colbyte_black, fb_pixels_w * fb_pixels_h * fb_bpp);
+  void *addr = fb_addr;
+
+  for (uint32_t i = 0; i < fb_pixels_h; i++)
+    {
+      memset (addr, fb_colbyte_black, fb_pixels_w * fb_bpp);
+      addr += fb_pitch;
+    }
 }
diff --git a/i386/i386at/fb_disp.h b/i386/i386at/fb_disp.h
index dd154eb6..e86bcfa6 100644
--- a/i386/i386at/fb_disp.h
+++ b/i386/i386at/fb_disp.h
@@ -51,7 +51,9 @@ typedef       short fb_disp_pos_t;
 /* Initialise the display with frame buffer's virtual address and
    pixel dimensions. bpp must be 24 or 32. */
 boolean_t
-fb_disp_init (void *v_addr, uint32_t width, uint32_t height, uint8_t bpp);
+fb_disp_init (void *v_addr,
+             uint32_t pitch, uint32_t width, uint32_t height,
+             uint8_t bpp);
 
 /* The maximum number of character columns and rows supported by the
    frame buffer pixel dimensions. */
diff --git a/i386/i386at/immc.c b/i386/i386at/immc.c
index c1dfe606..6e4d9661 100644
--- a/i386/i386at/immc.c
+++ b/i386/i386at/immc.c
@@ -171,11 +171,12 @@ immc_l1_tables[IMMC_FB_NUM_PAGETABLES][NPTES] 
__aligned(PAGE_SIZE);
 
 static void
 immc_enable_fb(const uint64_t paddr,
+              const uint32_t pitch,
               const uint32_t width,
               const uint32_t height,
               const uint8_t  bpp)
 {
-  uint64_t fb_size = (width * height * (bpp / 8));
+  uint64_t fb_size = ((uint64_t)pitch * (uint64_t)height);
 
   const uint64_t paddr_end = round_page (paddr + fb_size);
   uint64_t paddr_map = trunc_page (paddr);
@@ -211,7 +212,7 @@ immc_enable_fb(const uint64_t paddr,
   immc_l3_table[0] = (pa_to_pte (_kvtophys (&immc_l2_table))
                      | INTEL_PTE_VALID | INTEL_PTE_WRITE);
 
-  uint32_t mapped_height = ((paddr_map - paddr) / width / (bpp / 8));
+  uint32_t mapped_height = ((paddr_map - paddr) / pitch);
 
   if (mapped_height > height)
     mapped_height = height;
@@ -224,7 +225,7 @@ immc_enable_fb(const uint64_t paddr,
        (pa_to_pte (_kvtophys (&immc_l3_table))
         | INTEL_PTE_VALID | INTEL_PTE_WRITE);
 
-      if (fb_disp_init (IMMC_FB_VADDR, width, mapped_height, bpp))
+      if (fb_disp_init (IMMC_FB_VADDR, pitch, width, mapped_height, bpp))
        {
          immc_rows = fb_disp_max_rows();
          immc_cols = fb_disp_max_cols();
@@ -261,6 +262,7 @@ immc_enable(const void *mbi, const uint32_t magic)
          && mb1->fb_info.framebuffer_type == MULTIBOOT_FRAMEBUFFER_TYPE_RGB)
        {
          immc_enable_fb(mb1->fb_info.framebuffer_addr,
+                        mb1->fb_info.framebuffer_pitch,
                         mb1->fb_info.framebuffer_width,
                         mb1->fb_info.framebuffer_height,
                         mb1->fb_info.framebuffer_bpp);
@@ -288,6 +290,7 @@ immc_enable(const void *mbi, const uint32_t magic)
                  == MULTIBOOT2_FRAMEBUFFER_TYPE_RGB)
                {
                  immc_enable_fb(fb_tag->common.framebuffer_addr,
+                                fb_tag->common.framebuffer_pitch,
                                 fb_tag->common.framebuffer_width,
                                 fb_tag->common.framebuffer_height,
                                 fb_tag->common.framebuffer_bpp);
-- 
2.47.3


Reply via email to