boot_loader_name.rs 1.2 KB

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