boot_loader_name.rs 984 B

12345678910111213141516171819202122232425262728293031
  1. /// This Tag contains the name of the bootloader that is booting the kernel.
  2. ///
  3. /// The name is a normal C-style UTF-8 zero-terminated string that can be
  4. /// obtained via the `name` method.
  5. #[derive(Clone, Copy, Debug)]
  6. #[repr(C, packed)] // only repr(C) would add unwanted padding before first_section
  7. pub struct BootLoaderNameTag {
  8. typ: u32,
  9. size: u32,
  10. string: u8,
  11. }
  12. impl BootLoaderNameTag {
  13. /// Read the name of the bootloader that is booting the kernel.
  14. ///
  15. /// # Examples
  16. ///
  17. /// ```ignore
  18. /// if let Some(tag) = boot_info.boot_loader_name_tag() {
  19. /// let name = tag.name();
  20. /// assert_eq!("GRUB 2.02~beta3-5", name);
  21. /// }
  22. /// ```
  23. pub fn name(&self) -> &str {
  24. use core::{mem, slice, str};
  25. unsafe {
  26. let strlen = self.size as usize - mem::size_of::<BootLoaderNameTag>();
  27. str::from_utf8_unchecked(slice::from_raw_parts((&self.string) as *const u8, strlen))
  28. }
  29. }
  30. }