Skip to content

Commit 1129332

Browse files
committed
Use write_volatile in the GOP example
1 parent f525b86 commit 1129332

File tree

2 files changed

+16
-6
lines changed
  • src/proto/console
  • uefi-test-runner/src/proto/console

2 files changed

+16
-6
lines changed

src/proto/console/gop.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ impl GraphicsOutput {
161161
///
162162
/// This function is inherently unsafe since the wrong format
163163
/// could be used by a UEFI app when reading / writting the buffer.
164+
///
165+
/// It is also the callers responsibilty to use volatile memory accesses,
166+
/// otherwise they could be optimized to nothing.
164167
pub unsafe fn frame_buffer(&mut self) -> &mut [u8] {
165168
let data = self.mode.fb_address as *mut u8;
166169
let len = self.mode.fb_size;

uefi-test-runner/src/proto/console/gop.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use core::ptr;
12
use uefi::proto::console::gop::{BltOp, BltPixel, GraphicsOutput, PixelFormat};
23
use uefi::table::boot::BootServices;
34
use uefi_exts::BootServicesExt;
@@ -54,14 +55,20 @@ fn draw_fb(gop: &mut GraphicsOutput) {
5455

5556
type PixelWriter<'a> = &'a Fn(&mut [u8], (u8, u8, u8));
5657
let write_pixel_rgb = |pixel: &mut [u8], (r, g, b)| {
57-
pixel[0] = r;
58-
pixel[1] = g;
59-
pixel[2] = b;
58+
let p = pixel.as_mut_ptr();
59+
unsafe {
60+
ptr::write_volatile(p.offset(0), r);
61+
ptr::write_volatile(p.offset(1), g);
62+
ptr::write_volatile(p.offset(2), b);
63+
}
6064
};
6165
let write_pixel_bgr = |pixel: &mut [u8], (r, g, b)| {
62-
pixel[0] = b;
63-
pixel[1] = g;
64-
pixel[2] = r;
66+
let p = pixel.as_mut_ptr();
67+
unsafe {
68+
ptr::write_volatile(p.offset(0), b);
69+
ptr::write_volatile(p.offset(1), g);
70+
ptr::write_volatile(p.offset(2), r);
71+
}
6572
};
6673
let write_pixel: PixelWriter = match mi.pixel_format() {
6774
PixelFormat::RGB => &write_pixel_rgb,

0 commit comments

Comments
 (0)