aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / uefi / src / proto / console / gop.rs
| 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | |
| 3 | //! Graphics output protocol. |
| 4 | //! |
| 5 | //! The UEFI GOP is meant to replace existing [VGA][vga] hardware interfaces. |
| 6 | //! |
| 7 | //! The GOP provides access to a hardware frame buffer and allows UEFI apps |
| 8 | //! to draw directly to the graphics output device. |
| 9 | //! |
| 10 | //! The advantage of the GOP over legacy VGA is that it allows multiple GPUs |
| 11 | //! to exist and be used on the system. There is a GOP implementation for every |
| 12 | //! unique GPU in the system which supports UEFI. |
| 13 | //! |
| 14 | //! [vga]: https://en.wikipedia.org/wiki/Video_Graphics_Array |
| 15 | //! |
| 16 | //! # Definitions |
| 17 | //! |
| 18 | //! All graphics operations use a coordinate system where the top-left of the screen |
| 19 | //! is mapped to the point (0, 0), and `y` increases going down. |
| 20 | //! |
| 21 | //! Rectangles are defined by their top-left corner, and their width and height. |
| 22 | //! |
| 23 | //! The stride is understood as the length in bytes of a scan line / row of a buffer. |
| 24 | //! In theory, a buffer with a width of 640 should have (640 * 4) bytes per row, |
| 25 | //! but in practice there might be some extra padding used for efficiency. |
| 26 | //! |
| 27 | //! Frame buffers represent the graphics card's image buffers, backing the displays. |
| 28 | //! |
| 29 | //! Blits (**bl**ock **t**ransfer) can do high-speed memory copy between |
| 30 | //! the frame buffer and itself, or to and from some other buffers. |
| 31 | //! |
| 32 | //! # Blitting |
| 33 | //! |
| 34 | //! On certain hardware, the frame buffer is in a opaque format, |
| 35 | //! or cannot be accessed by the CPU. In those cases, it is not possible |
| 36 | //! to draw directly to the frame buffer. You must draw to another buffer |
| 37 | //! with a known pixel format, and then submit a blit command to copy that buffer |
| 38 | //! into the back buffer. |
| 39 | //! |
| 40 | //! Blitting can also copy a rectangle from the frame buffer to |
| 41 | //! another rectangle in the frame buffer, or move data out of the frame buffer |
| 42 | //! into a CPU-visible buffer. It can also do very fast color fills. |
| 43 | //! |
| 44 | //! The source and destination rectangles must always be of the same size: |
| 45 | //! no stretching / squashing will be done. |
| 46 | //! |
| 47 | //! # Animations |
| 48 | //! |
| 49 | //! UEFI does not mention if double buffering is used, nor how often |
| 50 | //! the frame buffer gets sent to the screen, but it's safe to assume that |
| 51 | //! the graphics card will re-draw the buffer at around the monitor's refresh rate. |
| 52 | //! You will have to implement your own double buffering if you want to |
| 53 | //! avoid tearing with animations. |
| 54 | |
| 55 | use crate::proto::unsafe_protocol; |
| 56 | use crate::util::usize_from_u32; |
| 57 | use crate::{Error, Result, Status, StatusExt, boot}; |
| 58 | use core::fmt::{self, Debug, Formatter}; |
| 59 | use core::marker::PhantomData; |
| 60 | use core::ptr::{self, NonNull}; |
| 61 | use core::slice; |
| 62 | use uefi_raw::protocol::console::{ |
| 63 | EdidDiscoveredProtocol, GraphicsOutputBltOperation, GraphicsOutputBltPixel, |
| 64 | GraphicsOutputModeInformation, GraphicsOutputProtocol, GraphicsOutputProtocolMode, |
| 65 | }; |
| 66 | |
| 67 | pub use uefi_raw::protocol::console::PixelBitmask; |
| 68 | |
| 69 | /// Graphics Output [`Protocol`] (GOP). Provides access to the video hardware's |
| 70 | /// frame buffer. |
| 71 | /// |
| 72 | /// The GOP can be used to set the properties of the framebuffer, and also |
| 73 | /// allows the app to access the in-memory buffer. |
| 74 | /// |
| 75 | /// [`Protocol`]: uefi::proto::Protocol |
| 76 | #[derive(Debug)] |
| 77 | #[repr(transparent)] |
| 78 | #[unsafe_protocol(GraphicsOutputProtocol::GUID)] |
| 79 | pub struct GraphicsOutput(GraphicsOutputProtocol); |
| 80 | |
| 81 | impl GraphicsOutput { |
| 82 | /// Returns information for an available graphics mode that the graphics |
| 83 | /// device and the set of active video output devices supports. |
| 84 | fn query_mode(&self, index: u32) -> Result<Mode> { |
| 85 | let mut info_sz = 0; |
| 86 | let mut info_heap_ptr = ptr::null_mut(); |
| 87 | // query_mode allocates a buffer and stores the heap ptr in the provided |
| 88 | // variable. In this buffer, the queried data can be found. |
| 89 | // SAFETY: The memory is valid. |
| 90 | unsafe { (self.0.query_mode)(&self.0, index, &mut info_sz, &mut info_heap_ptr) } |
| 91 | .to_result()?; |
| 92 | |
| 93 | // A buggy firmware may hand out a buffer shorter than the struct, |
| 94 | // which must not be read past its end. |
| 95 | let info = if info_sz >= size_of::<GraphicsOutputModeInformation>() { |
| 96 | // Transform to owned info on the stack. |
| 97 | // SAFETY: The buffer is at least as large as the struct. |
| 98 | Some(unsafe { *info_heap_ptr }) |
| 99 | } else { |
| 100 | None |
| 101 | }; |
| 102 | |
| 103 | let info_heap_ptr = info_heap_ptr.cast::<u8>(); |
| 104 | |
| 105 | // User has no benefit from propagating this error. If this |
| 106 | // fails, it is an error of the UEFI implementation. |
| 107 | // SAFETY: This pointer was allocated by the matching UEFI allocator. |
| 108 | unsafe { boot::free_pool(NonNull::new(info_heap_ptr).unwrap()) } |
| 109 | .expect("buffer should be deallocatable"); |
| 110 | |
| 111 | let info = info.ok_or(Error::from(Status::BAD_BUFFER_SIZE))?; |
| 112 | |
| 113 | Ok(Mode { |
| 114 | index, |
| 115 | info_sz, |
| 116 | info: ModeInfo(info), |
| 117 | }) |
| 118 | } |
| 119 | |
| 120 | /// Returns a [`ModeIter`]. |
| 121 | #[must_use] |
| 122 | pub const fn modes(&self) -> ModeIter<'_> { |
| 123 | ModeIter { |
| 124 | gop: self, |
| 125 | current: 0, |
| 126 | max: self.mode().max_mode, |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /// Sets the video device into the specified mode, clearing visible portions |
| 131 | /// of the output display to black. |
| 132 | /// |
| 133 | /// This function will invalidate the current framebuffer. |
| 134 | pub fn set_mode(&mut self, mode: &Mode) -> Result { |
| 135 | // SAFETY: The memory is valid. |
| 136 | unsafe { (self.0.set_mode)(&mut self.0, mode.index) }.to_result() |
| 137 | } |
| 138 | |
| 139 | /// Performs a blt (block transfer) operation on the frame buffer. |
| 140 | /// |
| 141 | /// Every operation requires different parameters. |
| 142 | pub fn blt(&mut self, op: BltOp) -> Result { |
| 143 | // Demultiplex the operation type. |
| 144 | // SAFETY: The memory is valid. |
| 145 | unsafe { |
| 146 | match op { |
| 147 | BltOp::VideoFill { |
| 148 | color, |
| 149 | dest: (dest_x, dest_y), |
| 150 | dims: (width, height), |
| 151 | } => { |
| 152 | self.check_framebuffer_region((dest_x, dest_y), (width, height)); |
| 153 | (self.0.blt)( |
| 154 | &mut self.0, |
| 155 | ptr::from_ref(&color) as *mut _, |
| 156 | GraphicsOutputBltOperation::BLT_VIDEO_FILL, |
| 157 | 0, |
| 158 | 0, |
| 159 | dest_x, |
| 160 | dest_y, |
| 161 | width, |
| 162 | height, |
| 163 | 0, |
| 164 | ) |
| 165 | .to_result() |
| 166 | } |
| 167 | BltOp::VideoToBltBuffer { |
| 168 | buffer, |
| 169 | src: (src_x, src_y), |
| 170 | dest: dest_region, |
| 171 | dims: (width, height), |
| 172 | } => { |
| 173 | self.check_framebuffer_region((src_x, src_y), (width, height)); |
| 174 | self.check_blt_buffer_region(dest_region, (width, height), buffer.len()); |
| 175 | match dest_region { |
| 176 | BltRegion::Full => (self.0.blt)( |
| 177 | &mut self.0, |
| 178 | buffer.as_mut_ptr().cast(), |
| 179 | GraphicsOutputBltOperation::BLT_VIDEO_TO_BLT_BUFFER, |
| 180 | src_x, |
| 181 | src_y, |
| 182 | 0, |
| 183 | 0, |
| 184 | width, |
| 185 | height, |
| 186 | 0, |
| 187 | ) |
| 188 | .to_result(), |
| 189 | BltRegion::SubRectangle { |
| 190 | coords: (dest_x, dest_y), |
| 191 | px_stride, |
| 192 | } => (self.0.blt)( |
| 193 | &mut self.0, |
| 194 | buffer.as_mut_ptr().cast(), |
| 195 | GraphicsOutputBltOperation::BLT_VIDEO_TO_BLT_BUFFER, |
| 196 | src_x, |
| 197 | src_y, |
| 198 | dest_x, |
| 199 | dest_y, |
| 200 | width, |
| 201 | height, |
| 202 | px_stride * size_of::<BltPixel>(), |
| 203 | ) |
| 204 | .to_result(), |
| 205 | } |
| 206 | } |
| 207 | BltOp::BufferToVideo { |
| 208 | buffer, |
| 209 | src: src_region, |
| 210 | dest: (dest_x, dest_y), |
| 211 | dims: (width, height), |
| 212 | } => { |
| 213 | self.check_blt_buffer_region(src_region, (width, height), buffer.len()); |
| 214 | self.check_framebuffer_region((dest_x, dest_y), (width, height)); |
| 215 | match src_region { |
| 216 | BltRegion::Full => (self.0.blt)( |
| 217 | &mut self.0, |
| 218 | // SAFETY: The buffer is only used for reading. |
| 219 | buffer.as_ptr().cast::<GraphicsOutputBltPixel>().cast_mut(), |
| 220 | GraphicsOutputBltOperation::BLT_BUFFER_TO_VIDEO, |
| 221 | 0, |
| 222 | 0, |
| 223 | dest_x, |
| 224 | dest_y, |
| 225 | width, |
| 226 | height, |
| 227 | 0, |
| 228 | ) |
| 229 | .to_result(), |
| 230 | BltRegion::SubRectangle { |
| 231 | coords: (src_x, src_y), |
| 232 | px_stride, |
| 233 | } => (self.0.blt)( |
| 234 | &mut self.0, |
| 235 | // SAFETY: The buffer is only used for reading. |
| 236 | buffer.as_ptr().cast::<GraphicsOutputBltPixel>().cast_mut(), |
| 237 | GraphicsOutputBltOperation::BLT_BUFFER_TO_VIDEO, |
| 238 | src_x, |
| 239 | src_y, |
| 240 | dest_x, |
| 241 | dest_y, |
| 242 | width, |
| 243 | height, |
| 244 | px_stride * size_of::<BltPixel>(), |
| 245 | ) |
| 246 | .to_result(), |
| 247 | } |
| 248 | } |
| 249 | BltOp::VideoToVideo { |
| 250 | src: (src_x, src_y), |
| 251 | dest: (dest_x, dest_y), |
| 252 | dims: (width, height), |
| 253 | } => { |
| 254 | self.check_framebuffer_region((src_x, src_y), (width, height)); |
| 255 | self.check_framebuffer_region((dest_x, dest_y), (width, height)); |
| 256 | (self.0.blt)( |
| 257 | &mut self.0, |
| 258 | ptr::null_mut(), |
| 259 | GraphicsOutputBltOperation::BLT_VIDEO_TO_VIDEO, |
| 260 | src_x, |
| 261 | src_y, |
| 262 | dest_x, |
| 263 | dest_y, |
| 264 | width, |
| 265 | height, |
| 266 | 0, |
| 267 | ) |
| 268 | .to_result() |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | /// Memory-safety check for accessing a region of the framebuffer |
| 275 | fn check_framebuffer_region(&self, coords: (usize, usize), dims: (usize, usize)) { |
| 276 | let (width, height) = self.current_mode_info().resolution(); |
| 277 | assert!( |
| 278 | coords.0.saturating_add(dims.0) <= width, |
| 279 | "Horizontal framebuffer coordinate out of bounds" |
| 280 | ); |
| 281 | assert!( |
| 282 | coords.1.saturating_add(dims.1) <= height, |
| 283 | "Vertical framebuffer coordinate out of bounds" |
| 284 | ); |
| 285 | } |
| 286 | |
| 287 | /// Memory-safety check for accessing a region of a user-provided buffer |
| 288 | fn check_blt_buffer_region(&self, region: BltRegion, dims: (usize, usize), buf_length: usize) { |
| 289 | match region { |
| 290 | BltRegion::Full => assert!( |
| 291 | dims.1.saturating_mul(dims.0) <= buf_length, |
| 292 | "BltBuffer access out of bounds" |
| 293 | ), |
| 294 | BltRegion::SubRectangle { |
| 295 | coords: (x, y), |
| 296 | px_stride, |
| 297 | } => { |
| 298 | assert!( |
| 299 | x.saturating_add(dims.0) <= px_stride, |
| 300 | "Horizontal BltBuffer coordinate out of bounds" |
| 301 | ); |
| 302 | assert!( |
| 303 | y.saturating_add(dims.1).saturating_mul(px_stride) <= buf_length, |
| 304 | "Vertical BltBuffer coordinate out of bounds" |
| 305 | ); |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | /// Returns the frame buffer information for the current mode. |
| 311 | #[must_use] |
| 312 | pub const fn current_mode_info(&self) -> ModeInfo { |
| 313 | // SAFETY: The memory is valid. |
| 314 | unsafe { *self.mode().info.cast::<ModeInfo>() } |
| 315 | } |
| 316 | |
| 317 | /// Access the frame buffer directly |
| 318 | pub fn frame_buffer(&mut self) -> FrameBuffer<'_> { |
| 319 | assert!( |
| 320 | self.current_mode_info().pixel_format() != PixelFormat::BltOnly, |
| 321 | "Cannot access the framebuffer in a Blt-only mode" |
| 322 | ); |
| 323 | let base = self.mode().frame_buffer_base as *mut u8; |
| 324 | let size = self.mode().frame_buffer_size; |
| 325 | |
| 326 | FrameBuffer { |
| 327 | base, |
| 328 | size, |
| 329 | _lifetime: PhantomData, |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | const fn mode(&self) -> &GraphicsOutputProtocolMode { |
| 334 | // SAFETY: The memory is valid. |
| 335 | unsafe { &*self.0.mode } |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | /// Represents the format of the pixels in a frame buffer. |
| 340 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 341 | #[repr(u32)] |
| 342 | pub enum PixelFormat { |
| 343 | /// Each pixel is 32-bit long, with 24-bit RGB, and the last byte is reserved. |
| 344 | Rgb = 0, |
| 345 | /// Each pixel is 32-bit long, with 24-bit BGR, and the last byte is reserved. |
| 346 | Bgr, |
| 347 | /// Custom pixel format, check the associated bitmask. |
| 348 | Bitmask, |
| 349 | /// The graphics mode does not support drawing directly to the frame buffer. |
| 350 | /// |
| 351 | /// This means you will have to use the `blt` function which will |
| 352 | /// convert the graphics data to the device's internal pixel format. |
| 353 | BltOnly, |
| 354 | // SAFETY: UEFI also defines a PixelFormatMax variant, and states that all |
| 355 | // valid enum values are guaranteed to be smaller. Since that is the |
| 356 | // case, adding a new enum variant would be a breaking change, so it |
| 357 | // is safe to model this C enum as a Rust enum. |
| 358 | } |
| 359 | |
| 360 | /// Represents a graphics mode compatible with a given graphics device. |
| 361 | #[derive(Copy, Clone, Debug)] |
| 362 | pub struct Mode { |
| 363 | index: u32, |
| 364 | info_sz: usize, |
| 365 | info: ModeInfo, |
| 366 | } |
| 367 | |
| 368 | impl Mode { |
| 369 | /// The size of the info structure in bytes. |
| 370 | /// |
| 371 | /// Newer versions of the spec might add extra information, in a backwards compatible way. |
| 372 | #[must_use] |
| 373 | pub const fn info_size(&self) -> usize { |
| 374 | self.info_sz |
| 375 | } |
| 376 | |
| 377 | /// Returns a reference to the mode info structure. |
| 378 | #[must_use] |
| 379 | pub const fn info(&self) -> &ModeInfo { |
| 380 | &self.info |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | /// Information about a graphics output mode. |
| 385 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 386 | #[repr(transparent)] |
| 387 | pub struct ModeInfo(GraphicsOutputModeInformation); |
| 388 | |
| 389 | impl ModeInfo { |
| 390 | /// Returns the (horizontal, vertical) resolution. |
| 391 | /// |
| 392 | /// On desktop monitors, this usually means (width, height). |
| 393 | #[must_use] |
| 394 | pub const fn resolution(&self) -> (usize, usize) { |
| 395 | ( |
| 396 | usize_from_u32(self.0.horizontal_resolution), |
| 397 | usize_from_u32(self.0.vertical_resolution), |
| 398 | ) |
| 399 | } |
| 400 | |
| 401 | /// Returns the format of the frame buffer. |
| 402 | #[must_use] |
| 403 | pub const fn pixel_format(&self) -> PixelFormat { |
| 404 | match self.0.pixel_format.0 { |
| 405 | 0 => PixelFormat::Rgb, |
| 406 | 1 => PixelFormat::Bgr, |
| 407 | 2 => PixelFormat::Bitmask, |
| 408 | 3 => PixelFormat::BltOnly, |
| 409 | _ => panic!("invalid pixel format"), |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | /// Returns the bitmask of the custom pixel format, if available. |
| 414 | #[must_use] |
| 415 | pub const fn pixel_bitmask(&self) -> Option<PixelBitmask> { |
| 416 | match self.pixel_format() { |
| 417 | PixelFormat::Bitmask => Some(self.0.pixel_information), |
| 418 | _ => None, |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | /// Returns the number of pixels per scanline. |
| 423 | /// |
| 424 | /// Due to performance reasons, the stride might not be equal to the width, |
| 425 | /// instead the stride might be bigger for better alignment. |
| 426 | #[must_use] |
| 427 | pub const fn stride(&self) -> usize { |
| 428 | usize_from_u32(self.0.pixels_per_scan_line) |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | /// Iterator for [`Mode`]s of the [`GraphicsOutput`] protocol. |
| 433 | pub struct ModeIter<'gop> { |
| 434 | gop: &'gop GraphicsOutput, |
| 435 | current: u32, |
| 436 | max: u32, |
| 437 | } |
| 438 | |
| 439 | impl Iterator for ModeIter<'_> { |
| 440 | type Item = Mode; |
| 441 | |
| 442 | fn next(&mut self) -> Option<Self::Item> { |
| 443 | let index = self.current; |
| 444 | if index < self.max { |
| 445 | let m = self.gop.query_mode(index); |
| 446 | self.current += 1; |
| 447 | |
| 448 | m.ok().or_else(|| self.next()) |
| 449 | } else { |
| 450 | None |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | fn size_hint(&self) -> (usize, Option<usize>) { |
| 455 | let size = (self.max - self.current) as usize; |
| 456 | (size, Some(size)) |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | impl Debug for ModeIter<'_> { |
| 461 | fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { |
| 462 | f.debug_struct("ModeIter") |
| 463 | .field("current", &self.current) |
| 464 | .field("max", &self.max) |
| 465 | .finish() |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | impl ExactSizeIterator for ModeIter<'_> {} |
| 470 | |
| 471 | /// Format of pixel data used for blitting. |
| 472 | /// |
| 473 | /// This is a BGR 24-bit format with an 8-bit padding, to keep each pixel 32-bit in size. |
| 474 | #[expect(missing_docs)] |
| 475 | #[derive(Debug, Copy, Clone)] |
| 476 | #[repr(C)] |
| 477 | pub struct BltPixel { |
| 478 | pub blue: u8, |
| 479 | pub green: u8, |
| 480 | pub red: u8, |
| 481 | _reserved: u8, |
| 482 | } |
| 483 | |
| 484 | impl BltPixel { |
| 485 | /// Create a new pixel from RGB values. |
| 486 | #[must_use] |
| 487 | pub const fn new(red: u8, green: u8, blue: u8) -> Self { |
| 488 | Self { |
| 489 | red, |
| 490 | green, |
| 491 | blue, |
| 492 | _reserved: 0, |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | impl From<u32> for BltPixel { |
| 498 | fn from(color: u32) -> Self { |
| 499 | Self { |
| 500 | blue: (color & 0x00_00_FF) as u8, |
| 501 | green: ((color & 0x00_FF_00) >> 8) as u8, |
| 502 | red: ((color & 0xFF_00_00) >> 16) as u8, |
| 503 | _reserved: 0, |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | /// Region of the `BltBuffer` which we are operating on |
| 509 | /// |
| 510 | /// Some `Blt` operations can operate on either the full `BltBuffer` or a |
| 511 | /// sub-rectangle of it, but require the stride to be known in the latter case. |
| 512 | #[derive(Clone, Copy, Debug)] |
| 513 | pub enum BltRegion { |
| 514 | /// Operate on the full `BltBuffer` |
| 515 | Full, |
| 516 | |
| 517 | /// Operate on a sub-rectangle of the `BltBuffer` |
| 518 | SubRectangle { |
| 519 | /// Coordinate of the rectangle in the `BltBuffer` |
| 520 | coords: (usize, usize), |
| 521 | |
| 522 | /// Stride (length of each row of the `BltBuffer`) in **pixels** |
| 523 | px_stride: usize, |
| 524 | }, |
| 525 | } |
| 526 | |
| 527 | /// Blit operation to perform. |
| 528 | #[derive(Debug)] |
| 529 | pub enum BltOp<'buf> { |
| 530 | /// Fills a rectangle of video display with a pixel color. |
| 531 | VideoFill { |
| 532 | /// The color to fill with. |
| 533 | color: BltPixel, |
| 534 | /// The X / Y coordinates of the destination rectangle. |
| 535 | dest: (usize, usize), |
| 536 | /// The width / height of the rectangle. |
| 537 | dims: (usize, usize), |
| 538 | }, |
| 539 | /// Reads data from the video display to the buffer. |
| 540 | VideoToBltBuffer { |
| 541 | /// Buffer into which to copy data. |
| 542 | buffer: &'buf mut [BltPixel], |
| 543 | /// Coordinates of the source rectangle, in the frame buffer. |
| 544 | src: (usize, usize), |
| 545 | /// Location of the destination rectangle in the user-provided buffer |
| 546 | dest: BltRegion, |
| 547 | /// Width / height of the rectangles. |
| 548 | dims: (usize, usize), |
| 549 | }, |
| 550 | /// Write data from the buffer to the video rectangle. |
| 551 | /// Delta must be the stride (count of bytes in a row) of the buffer. |
| 552 | BufferToVideo { |
| 553 | /// Buffer from which to copy data. |
| 554 | buffer: &'buf [BltPixel], |
| 555 | /// Location of the source rectangle in the user-provided buffer. |
| 556 | src: BltRegion, |
| 557 | /// Coordinates of the destination rectangle, in the frame buffer. |
| 558 | dest: (usize, usize), |
| 559 | /// Width / height of the rectangles. |
| 560 | dims: (usize, usize), |
| 561 | }, |
| 562 | /// Copy from the source rectangle in video memory to |
| 563 | /// the destination rectangle, also in video memory. |
| 564 | VideoToVideo { |
| 565 | /// Coordinates of the source rectangle, in the frame buffer. |
| 566 | src: (usize, usize), |
| 567 | /// Coordinates of the destination rectangle, also in the frame buffer. |
| 568 | dest: (usize, usize), |
| 569 | /// Width / height of the rectangles. |
| 570 | dims: (usize, usize), |
| 571 | }, |
| 572 | } |
| 573 | |
| 574 | /// Direct access to a memory-mapped frame buffer |
| 575 | #[derive(Debug)] |
| 576 | pub struct FrameBuffer<'gop> { |
| 577 | base: *mut u8, |
| 578 | size: usize, |
| 579 | _lifetime: PhantomData<&'gop mut u8>, |
| 580 | } |
| 581 | |
| 582 | impl FrameBuffer<'_> { |
| 583 | /// Access the raw framebuffer pointer |
| 584 | /// |
| 585 | /// To use this pointer safely and correctly, you must... |
| 586 | /// - Honor the pixel format and stride specified by the mode info |
| 587 | /// - Keep memory accesses in bound |
| 588 | /// - Use volatile reads and writes |
| 589 | /// - Make sure that the pointer does not outlive the FrameBuffer |
| 590 | /// |
| 591 | /// On some implementations this framebuffer pointer can be used after |
| 592 | /// exiting boot services, but that is not guaranteed by the UEFI Specification. |
| 593 | pub const fn as_mut_ptr(&mut self) -> *mut u8 { |
| 594 | self.base |
| 595 | } |
| 596 | |
| 597 | /// Query the framebuffer size in bytes |
| 598 | #[must_use] |
| 599 | pub const fn size(&self) -> usize { |
| 600 | self.size |
| 601 | } |
| 602 | |
| 603 | /// Modify the i-th byte of the frame buffer |
| 604 | /// |
| 605 | /// # Safety |
| 606 | /// |
| 607 | /// This operation is unsafe because... |
| 608 | /// - You must honor the pixel format and stride specified by the mode info |
| 609 | /// - There is no bound checking on memory accesses in release mode |
| 610 | #[inline] |
| 611 | pub unsafe fn write_byte(&mut self, index: usize, value: u8) { |
| 612 | debug_assert!(index < self.size, "Frame buffer accessed out of bounds"); |
| 613 | // SAFETY: The memory is valid. |
| 614 | unsafe { self.base.add(index).write_volatile(value) } |
| 615 | } |
| 616 | |
| 617 | /// Read the i-th byte of the frame buffer |
| 618 | /// |
| 619 | /// # Safety |
| 620 | /// |
| 621 | /// This operation is unsafe because... |
| 622 | /// - You must honor the pixel format and stride specified by the mode info |
| 623 | /// - There is no bound checking on memory accesses in release mode |
| 624 | #[inline] |
| 625 | #[must_use] |
| 626 | pub unsafe fn read_byte(&self, index: usize) -> u8 { |
| 627 | debug_assert!(index < self.size, "Frame buffer accessed out of bounds"); |
| 628 | // SAFETY: The memory is valid. |
| 629 | unsafe { self.base.add(index).read_volatile() } |
| 630 | } |
| 631 | |
| 632 | /// Write a value in the frame buffer, starting at the i-th byte |
| 633 | /// |
| 634 | /// We only recommend using this method with [u8; N] arrays. Once Rust has |
| 635 | /// const generics, it will be deprecated and replaced with a write_bytes() |
| 636 | /// method that only accepts [u8; N] input. |
| 637 | /// |
| 638 | /// # Safety |
| 639 | /// |
| 640 | /// This operation is unsafe because... |
| 641 | /// - It is your responsibility to make sure that the value type makes sense |
| 642 | /// - You must honor the pixel format and stride specified by the mode info |
| 643 | /// - The frame buffer address plus `index` must be aligned to |
| 644 | /// `align_of::<T>()`, as the value is accessed as a `T` |
| 645 | /// - There is no bound or alignment checking on memory accesses in |
| 646 | /// release mode |
| 647 | #[inline] |
| 648 | pub unsafe fn write_value<T>(&mut self, index: usize, value: T) { |
| 649 | debug_assert!( |
| 650 | index.saturating_add(size_of::<T>()) <= self.size, |
| 651 | "Frame buffer accessed out of bounds" |
| 652 | ); |
| 653 | debug_assert!( |
| 654 | self.base.wrapping_add(index).cast::<T>().is_aligned(), |
| 655 | "Frame buffer accessed at unaligned index" |
| 656 | ); |
| 657 | // SAFETY: The memory is valid. |
| 658 | unsafe { |
| 659 | let ptr = self.base.add(index).cast::<T>(); |
| 660 | ptr.write_volatile(value) |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | /// Read a value from the frame buffer, starting at the i-th byte |
| 665 | /// |
| 666 | /// We only recommend using this method with [u8; N] arrays. Once Rust has |
| 667 | /// const generics, it will be deprecated and replaced with a read_bytes() |
| 668 | /// method that only accepts [u8; N] input. |
| 669 | /// |
| 670 | /// # Safety |
| 671 | /// |
| 672 | /// This operation is unsafe because... |
| 673 | /// - It is your responsibility to make sure that the value type makes sense |
| 674 | /// - You must honor the pixel format and stride specified by the mode info |
| 675 | /// - The frame buffer address plus `index` must be aligned to |
| 676 | /// `align_of::<T>()`, as the value is accessed as a `T` |
| 677 | /// - There is no bound or alignment checking on memory accesses in |
| 678 | /// release mode |
| 679 | #[inline] |
| 680 | #[must_use] |
| 681 | pub unsafe fn read_value<T>(&self, index: usize) -> T { |
| 682 | debug_assert!( |
| 683 | index.saturating_add(size_of::<T>()) <= self.size, |
| 684 | "Frame buffer accessed out of bounds" |
| 685 | ); |
| 686 | debug_assert!( |
| 687 | self.base.wrapping_add(index).cast::<T>().is_aligned(), |
| 688 | "Frame buffer accessed at unaligned index" |
| 689 | ); |
| 690 | // SAFETY: The source layout matches the target view. |
| 691 | unsafe { (self.base.add(index) as *const T).read_volatile() } |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | /// EDID Discovered [`Protocol`]. Exposes the EDID information that was |
| 696 | /// discovered for the device backing a [`GraphicsOutput`] handle. |
| 697 | /// |
| 698 | /// This protocol is installed on the same handle as [`GraphicsOutput`], since |
| 699 | /// both are produced by the same output device driver. |
| 700 | /// |
| 701 | /// [`Protocol`]: uefi::proto::Protocol |
| 702 | /// [`GraphicsOutput`]: crate::proto::console::gop::GraphicsOutput |
| 703 | #[derive(Debug)] |
| 704 | #[repr(transparent)] |
| 705 | #[unsafe_protocol(EdidDiscoveredProtocol::GUID)] |
| 706 | pub struct EdidDiscovered(EdidDiscoveredProtocol); |
| 707 | |
| 708 | impl EdidDiscovered { |
| 709 | /// Get the discovered EDID as raw bytes. |
| 710 | /// |
| 711 | /// Return `None` if no EDID was discovered for this display device. |
| 712 | #[must_use] |
| 713 | pub const fn edid(&self) -> Option<&[u8]> { |
| 714 | if self.0.edid.is_null() { |
| 715 | None |
| 716 | } else { |
| 717 | // SAFETY: |
| 718 | // The memory is valid for `size_of_edid` bytes for the |
| 719 | // lifetime of the protocol, matching the lifetime of `&self`. |
| 720 | unsafe { |
| 721 | Some(slice::from_raw_parts( |
| 722 | self.0.edid, |
| 723 | usize_from_u32(self.0.size_of_edid), |
| 724 | )) |
| 725 | } |
| 726 | } |
| 727 | } |
| 728 | } |