boot_loader_name.rs 1000 B

123456789101112131415161718192021222324252627282930313233
  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,str,slice};
  25. unsafe {
  26. let strlen = self.size as usize - mem::size_of::<BootLoaderNameTag>();
  27. str::from_utf8_unchecked(
  28. slice::from_raw_parts((&self.string) as *const u8, strlen))
  29. }
  30. }
  31. }