1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764
#![no_std]
#![cfg_attr(feature = "unstable", feature(error_in_core))]
#![deny(missing_debug_implementations)]
// --- BEGIN STYLE CHECKS ---
// These checks are optional in CI for PRs, as discussed in
// https://github.com/rust-osdev/multiboot2/pull/92
#![deny(clippy::all)]
#![deny(rustdoc::all)]
#![allow(rustdoc::private_doc_tests)]
// --- END STYLE CHECKS ---
//! Library that assists parsing the Multiboot2 Information Structure (MBI) from
//! Multiboot2-compliant bootloaders, like GRUB. It supports all tags from the specification
//! including full support for the sections of ELF-64. This library is `no_std` and can be
//! used in a Multiboot2-kernel.
//!
//! The GNU Multiboot(2) specification aims to provide a standardised
//! method of sharing commonly used information about the host machine at
//! boot time and give the payload, i.e. a kernel, a well defined machine
//! state.
//!
//! ## Example
//!
//! ```rust
//! use multiboot2::{BootInformation, BootInformationHeader};
//!
//! fn kernel_entry(mb_magic: u32, mbi_ptr: u32) {
//! if mb_magic == multiboot2::MAGIC {
//! let boot_info = unsafe { BootInformation::load(mbi_ptr as *const BootInformationHeader).unwrap() };
//! let _cmd = boot_info.command_line_tag();
//! } else { /* Panic or use multiboot1 flow. */ }
//! }
//! ```
//!
//! ## MSRV
//! The MSRV is 1.68.0 stable.
#[cfg(feature = "builder")]
extern crate alloc;
// this crate can use std in tests only
#[cfg_attr(test, macro_use)]
#[cfg(test)]
extern crate std;
use core::fmt;
use core::mem::size_of;
use derive_more::Display;
// Must be public so that custom tags can be DSTs.
pub use ptr_meta::Pointee;
use crate::framebuffer::UnknownFramebufferType;
pub use boot_loader_name::BootLoaderNameTag;
#[cfg(feature = "builder")]
use builder::traits::StructAsBytes;
pub use command_line::CommandLineTag;
pub use efi::{EFIImageHandle32Tag, EFIImageHandle64Tag, EFISdt32Tag, EFISdt64Tag};
pub use elf_sections::{
ElfSection, ElfSectionFlags, ElfSectionIter, ElfSectionType, ElfSectionsTag,
};
pub use framebuffer::{FramebufferColor, FramebufferField, FramebufferTag, FramebufferType};
pub use image_load_addr::ImageLoadPhysAddrTag;
pub use memory_map::{
BasicMemoryInfoTag, EFIBootServicesNotExitedTag, EFIMemoryAreaType, EFIMemoryDesc,
EFIMemoryMapTag, MemoryArea, MemoryAreaType, MemoryMapTag,
};
pub use module::{ModuleIter, ModuleTag};
pub use rsdp::{RsdpV1Tag, RsdpV2Tag};
pub use smbios::SmbiosTag;
use tag_type::TagIter;
pub use tag_type::{EndTag, Tag, TagType, TagTypeId};
pub use vbe_info::{
VBECapabilities, VBEControlInfo, VBEDirectColorAttributes, VBEField, VBEInfoTag,
VBEMemoryModel, VBEModeAttributes, VBEModeInfo, VBEWindowAttributes,
};
#[macro_use]
extern crate bitflags;
mod boot_loader_name;
mod command_line;
mod efi;
mod elf_sections;
mod framebuffer;
mod image_load_addr;
mod memory_map;
mod module;
mod rsdp;
mod smbios;
mod tag_type;
mod vbe_info;
#[cfg(feature = "builder")]
pub mod builder;
/// Magic number that a multiboot2-compliant boot loader will store in `eax` register
/// right before handoff to the payload (the kernel). This value can be used to check,
/// that the kernel was indeed booted via multiboot2.
///
/// Caution: You might need some assembly code (e.g. GAS or NASM) first, which
/// moves `eax` to another register, like `edi`. Otherwise it probably happens,
/// that the Rust compiler output changes `eax` before you can access it.
pub const MAGIC: u32 = 0x36d76289;
/// # Safety
/// Deprecated. Please use BootInformation::load() instead.
#[deprecated = "Please use BootInformation::load() instead."]
pub unsafe fn load<'a>(address: usize) -> Result<BootInformation<'a>, MbiLoadError> {
let ptr = address as *const BootInformationHeader;
BootInformation::load(ptr)
}
/// # Safety
/// Deprecated. Please use BootInformation::load() instead.
#[deprecated = "Please use BootInformation::load() instead."]
pub unsafe fn load_with_offset<'a>(
address: usize,
offset: usize,
) -> Result<BootInformation<'a>, MbiLoadError> {
let ptr = address as *const u8;
let ptr = ptr.add(offset);
BootInformation::load(ptr.cast())
}
/// Error type that describes errors while loading/parsing a multiboot2 information structure
/// from a given address.
#[derive(Display, Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum MbiLoadError {
/// The address is invalid. Make sure that the address is 8-byte aligned,
/// according to the spec.
#[display(fmt = "The address is invalid")]
IllegalAddress,
/// The total size of the multiboot2 information structure must be a multiple of 8.
/// (Not in spec, but it is implicitly the case, because the begin of MBI
/// and all tags are 8-byte aligned and the end tag is exactly 8 byte long).
#[display(fmt = "The size of the MBI is unexpected")]
IllegalTotalSize(u32),
/// End tag missing. Each multiboot2 header requires to have an end tag.
#[display(fmt = "There is no end tag")]
NoEndTag,
}
#[cfg(feature = "unstable")]
impl core::error::Error for MbiLoadError {}
/// The basic header of a boot information.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
pub struct BootInformationHeader {
// size is multiple of 8
total_size: u32,
_reserved: u32,
// Followed by the boot information tags.
}
impl BootInformationHeader {
#[cfg(feature = "builder")]
fn new(total_size: u32) -> Self {
Self {
total_size,
_reserved: 0,
}
}
}
#[cfg(feature = "builder")]
impl StructAsBytes for BootInformationHeader {
fn byte_size(&self) -> usize {
core::mem::size_of::<Self>()
}
}
/// This type holds the whole data of the MBI. This helps to better satisfy miri
/// when it checks for memory issues.
#[derive(ptr_meta::Pointee)]
#[repr(C)]
struct BootInformationInner {
header: BootInformationHeader,
tags: [u8],
}
impl BootInformationInner {
/// Checks if the MBI has a valid end tag by checking the end of the mbi's
/// bytes.
fn has_valid_end_tag(&self) -> bool {
let end_tag_prototype = EndTag::default();
let self_ptr = unsafe { self.tags.as_ptr().sub(size_of::<BootInformationHeader>()) };
let end_tag_ptr = unsafe {
self_ptr
.add(self.header.total_size as usize)
.sub(size_of::<EndTag>())
};
let end_tag = unsafe { &*(end_tag_ptr as *const EndTag) };
end_tag.typ == end_tag_prototype.typ && end_tag.size == end_tag_prototype.size
}
}
/// A Multiboot 2 Boot Information (MBI) accessor.
#[repr(transparent)]
pub struct BootInformation<'a>(&'a BootInformationInner);
impl<'a> BootInformation<'a> {
/// Loads the [`BootInformation`] from a pointer. The pointer must be valid
/// and aligned to an 8-byte boundary, as defined by the spec.
///
/// ## Example
///
/// ```rust
/// use multiboot2::{BootInformation, BootInformationHeader};
///
/// fn kernel_entry(mb_magic: u32, mbi_ptr: u32) {
/// if mb_magic == multiboot2::MAGIC {
/// let boot_info = unsafe { BootInformation::load(mbi_ptr as *const BootInformationHeader).unwrap() };
/// let _cmd = boot_info.command_line_tag();
/// } else { /* Panic or use multiboot1 flow. */ }
/// }
/// ```
///
/// ## Safety
/// * `ptr` must be valid for reading. Otherwise this function might cause
/// invalid machine state or crash your binary (kernel). This can be the
/// case in environments with standard environment (segfault), but also in
/// boot environments, such as UEFI.
/// * The memory at `ptr` must not be modified after calling `load` or the
/// program may observe unsynchronized mutation.
pub unsafe fn load(ptr: *const BootInformationHeader) -> Result<Self, MbiLoadError> {
// null or not aligned
if ptr.is_null() || ptr.align_offset(8) != 0 {
return Err(MbiLoadError::IllegalAddress);
}
// mbi: reference to basic header
let mbi = &*ptr;
// Check if total size is a multiple of 8.
// See MbiLoadError::IllegalTotalSize for comments
if mbi.total_size & 0b111 != 0 {
return Err(MbiLoadError::IllegalTotalSize(mbi.total_size));
}
let slice_size = mbi.total_size as usize - size_of::<BootInformationHeader>();
// mbi: reference to full mbi
let mbi = ptr_meta::from_raw_parts::<BootInformationInner>(ptr.cast(), slice_size);
let mbi = &*mbi;
if !mbi.has_valid_end_tag() {
return Err(MbiLoadError::NoEndTag);
}
Ok(Self(mbi))
}
/// Get the start address of the boot info.
pub fn start_address(&self) -> usize {
self.as_ptr() as usize
}
/// Get the start address of the boot info as pointer.
pub fn as_ptr(&self) -> *const () {
core::ptr::addr_of!(*self.0).cast()
}
/// Get the end address of the boot info.
///
/// This is the same as doing:
///
/// ```rust,no_run
/// # let boot_info = unsafe { multiboot2::load(0xdeadbeef).unwrap() };
/// let end_addr = boot_info.start_address() + boot_info.total_size();
/// ```
pub fn end_address(&self) -> usize {
self.start_address() + self.total_size()
}
/// Get the total size of the boot info struct.
pub fn total_size(&self) -> usize {
self.0.header.total_size as usize
}
/// Search for the basic memory info tag.
pub fn basic_memory_info_tag(&self) -> Option<&BasicMemoryInfoTag> {
self.get_tag::<BasicMemoryInfoTag, _>(TagType::BasicMeminfo)
}
/// Returns an [`ElfSectionIter`] iterator over the ELF Sections, if the
/// [`ElfSectionsTag`] is present.
///
/// # Examples
///
/// ```rust,no_run
/// # let boot_info = unsafe { multiboot2::load(0xdeadbeef).unwrap() };
/// if let Some(sections) = boot_info.elf_sections() {
/// let mut total = 0;
/// for section in sections {
/// println!("Section: {:?}", section);
/// total += 1;
/// }
/// }
/// ```
pub fn elf_sections(&self) -> Option<ElfSectionIter> {
let tag = self.get_tag::<ElfSectionsTag, _>(TagType::ElfSections);
tag.map(|t| {
assert!((t.entry_size * t.shndx) <= t.size);
t.sections()
})
}
/// Search for the Memory map tag.
pub fn memory_map_tag(&self) -> Option<&MemoryMapTag> {
self.get_tag::<MemoryMapTag, _>(TagType::Mmap)
}
/// Get an iterator of all module tags.
pub fn module_tags(&self) -> ModuleIter {
module::module_iter(self.tags())
}
/// Search for the BootLoader name tag.
pub fn boot_loader_name_tag(&self) -> Option<&BootLoaderNameTag> {
self.get_tag::<BootLoaderNameTag, _>(TagType::BootLoaderName)
}
/// Search for the Command line tag.
pub fn command_line_tag(&self) -> Option<&CommandLineTag> {
self.get_tag::<CommandLineTag, _>(TagType::Cmdline)
}
/// Search for the VBE framebuffer tag. The result is `Some(Err(e))`, if the
/// framebuffer type is unknown, while the framebuffer tag is present.
pub fn framebuffer_tag(&self) -> Option<Result<&FramebufferTag, UnknownFramebufferType>> {
self.get_tag::<FramebufferTag, _>(TagType::Framebuffer)
.map(|tag| match tag.buffer_type() {
Ok(_) => Ok(tag),
Err(e) => Err(e),
})
}
/// Search for the EFI 32-bit SDT tag.
pub fn efi_sdt_32_tag(&self) -> Option<&EFISdt32Tag> {
self.get_tag::<EFISdt32Tag, _>(TagType::Efi32)
}
/// Search for the EFI 64-bit SDT tag.
pub fn efi_sdt_64_tag(&self) -> Option<&EFISdt64Tag> {
self.get_tag::<EFISdt64Tag, _>(TagType::Efi64)
}
/// Search for the (ACPI 1.0) RSDP tag.
pub fn rsdp_v1_tag(&self) -> Option<&RsdpV1Tag> {
self.get_tag::<RsdpV1Tag, _>(TagType::AcpiV1)
}
/// Search for the (ACPI 2.0 or later) RSDP tag.
pub fn rsdp_v2_tag(&self) -> Option<&RsdpV2Tag> {
self.get_tag::<RsdpV2Tag, _>(TagType::AcpiV2)
}
/// Search for the EFI Memory map tag, if the boot services were exited.
/// Otherwise, if the [`TagType::EfiBs`] tag is present, this returns `None`
/// as it is strictly recommended to get the memory map from the `uefi`
/// services.
pub fn efi_memory_map_tag(&self) -> Option<&EFIMemoryMapTag> {
// If the EFIBootServicesNotExited is present, then we should not use
// the memory map, as it could still be in use.
match self.get_tag::<Tag, _>(TagType::EfiBs) {
Some(_tag) => None,
None => self.get_tag::<EFIMemoryMapTag, _>(TagType::EfiMmap),
}
}
/// Search for the EFI 32-bit image handle pointer tag.
pub fn efi_32_ih_tag(&self) -> Option<&EFIImageHandle32Tag> {
self.get_tag::<EFIImageHandle32Tag, _>(TagType::Efi32Ih)
}
/// Search for the EFI 64-bit image handle pointer tag.
pub fn efi_64_ih_tag(&self) -> Option<&EFIImageHandle64Tag> {
self.get_tag::<EFIImageHandle64Tag, _>(TagType::Efi64Ih)
}
/// Search for the EFI boot services not exited tag.
pub fn efi_bs_not_exited_tag(&self) -> Option<&EFIBootServicesNotExitedTag> {
self.get_tag::<EFIBootServicesNotExitedTag, _>(TagType::EfiBs)
}
/// Search for the Image Load Base Physical Address tag.
pub fn load_base_addr_tag(&self) -> Option<&ImageLoadPhysAddrTag> {
self.get_tag::<ImageLoadPhysAddrTag, _>(TagType::LoadBaseAddr)
}
/// Search for the VBE information tag.
pub fn vbe_info_tag(&self) -> Option<&VBEInfoTag> {
self.get_tag::<VBEInfoTag, _>(TagType::Vbe)
}
/// Search for the SMBIOS tag.
pub fn smbios_tag(&self) -> Option<&SmbiosTag> {
self.get_tag::<SmbiosTag, _>(TagType::Smbios)
}
/// Public getter to find any Multiboot tag by its type, including
/// specified and custom ones.
///
/// The parameter can be of type `u32`, [`TagType`], or [`TagTypeId`].
///
/// # Specified or Custom Tags
/// The Multiboot2 specification specifies a list of tags, see [`TagType`].
/// However, it doesn't forbid to use custom tags. Because of this, there
/// exists the [`TagType`] abstraction. It is recommended to use this
/// getter only for custom tags. For specified tags, use getters, such as
/// [`Self::efi_64_ih_tag`].
///
/// ## Use Custom Tags
/// The following example shows how you may use this interface to parse
/// custom tags from the MBI. If they are dynamically sized (DST), a few more
/// special handling is required. This is reflected by code-comments.
///
/// ```no_run
/// use std::str::Utf8Error;
/// use multiboot2::{Tag, TagTrait, TagTypeId};
///
/// #[repr(C)]
/// #[derive(multiboot2::Pointee)] // Only needed for DSTs.
/// struct CustomTag {
/// // new type from the lib: has repr(u32)
/// tag: TagTypeId,
/// size: u32,
/// // begin of inline string
/// name: [u8],
/// }
///
/// // This implementation is only necessary for tags that are DSTs.
/// impl TagTrait for CustomTag {
/// fn dst_size(base_tag: &Tag) -> usize {
/// // The size of the sized portion of the custom tag.
/// let tag_base_size = 8; // id + size is 8 byte in size
/// assert!(base_tag.size >= 8);
/// base_tag.size as usize - tag_base_size
/// }
/// }
///
/// impl CustomTag {
/// fn name(&self) -> Result<&str, Utf8Error> {
/// Tag::get_dst_str_slice(&self.name)
/// }
/// }
///
/// let mbi = unsafe { multiboot2::load(0xdeadbeef).unwrap() };
///
/// let tag = mbi
/// .get_tag::<CustomTag, _>(0x1337)
/// .unwrap();
/// assert_eq!(tag.name(), Ok("name"));
/// ```
pub fn get_tag<TagT: TagTrait + ?Sized + 'a, TagType: Into<TagTypeId>>(
&'a self,
typ: TagType,
) -> Option<&'a TagT> {
let typ = typ.into();
self.tags()
.find(|tag| tag.typ == typ)
.map(|tag| tag.cast_tag::<TagT>())
}
/// Returns an iterator over all tags.
fn tags(&self) -> TagIter {
TagIter::new(&self.0.tags)
}
}
// SAFETY: BootInformation contains a const ptr to memory that is never mutated.
// Sending this pointer to other threads is sound.
unsafe impl Send for BootInformation<'_> {}
impl fmt::Debug for BootInformation<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
/// Limit how many Elf-Sections should be debug-formatted.
/// Can be thousands of sections for a Rust binary => this is useless output.
/// If the user really wants this, they should debug-format the field directly.
const ELF_SECTIONS_LIMIT: usize = 17;
let mut debug = f.debug_struct("Multiboot2 Boot Information");
debug
.field("start_address", &(self.start_address() as *const u64))
.field("end_address", &(self.end_address() as *const u64))
.field("total_size", &(self.total_size() as *const u64))
.field(
"boot_loader_name_tag",
&self
.boot_loader_name_tag()
.and_then(|x| x.name().ok())
.unwrap_or("<unknown>"),
)
.field(
"command_line",
&self
.command_line_tag()
.and_then(|x| x.cmdline().ok())
.unwrap_or(""),
)
.field("memory_areas", &self.memory_map_tag())
// so far, I didn't found a nice way to connect the iterator with ".field()" because
// the iterator isn't Debug
.field("module_tags", &self.module_tags());
// usually this is REALLY big (thousands of tags) => skip it here
let elf_sections_tag_entries_count = self.elf_sections().map(|x| x.count()).unwrap_or(0);
if elf_sections_tag_entries_count > ELF_SECTIONS_LIMIT {
debug.field("elf_sections_tags (count)", &elf_sections_tag_entries_count);
} else {
debug.field(
"elf_sections_tags",
&self.elf_sections().unwrap_or_default(),
);
}
debug
.field("efi_32_ih", &self.efi_32_ih_tag())
.field("efi_64_ih", &self.efi_64_ih_tag())
.field("efi_sdt_32_tag", &self.efi_sdt_32_tag())
.field("efi_sdt_64_tag", &self.efi_sdt_64_tag())
.field("efi_memory_map_tag", &self.efi_memory_map_tag())
.finish()
}
}
/// A trait to abstract over all sized and unsized tags (DSTs). For sized tags,
/// this trait does not much. For DSTs, a `TagTrait::dst_size` implementation
/// must me provided, which returns the right size hint for the dynamically
/// sized portion of the struct.
///
/// The [`TagTrait::from_base_tag`] method has a default implementation for all
/// tags that are `Sized`.
///
/// # Trivia
/// This crate uses the [`Pointee`]-abstraction of the [`ptr_meta`] crate to
/// create fat pointers.
pub trait TagTrait: Pointee {
/// Returns the amount of items in the dynamically sized portion of the
/// DST. Note that this is not the amount of bytes. So if the dynamically
/// sized portion is 16 bytes in size and each element is 4 bytes big, then
/// this function must return 4.
fn dst_size(base_tag: &Tag) -> Self::Metadata;
/// Creates a reference to a (dynamically sized) tag type in a safe way.
/// DST tags need to implement a proper [`Self::dst_size`] implementation.
///
/// # Safety
/// Callers must be sure that the "size" field of the provided [`Tag`] is
/// sane and the underlying memory valid. The implementation of this trait
/// **must have** a correct [`Self::dst_size`] implementation.
unsafe fn from_base_tag<'a>(tag: &Tag) -> &'a Self {
let ptr = core::ptr::addr_of!(*tag);
let ptr = ptr_meta::from_raw_parts(ptr.cast(), Self::dst_size(tag));
&*ptr
}
}
// All sized tags automatically have a Pointee implementation where
// Pointee::Metadata is (). Hence, the TagTrait is implemented automatically for
// all tags that are sized.
impl<T: Pointee<Metadata = ()>> TagTrait for T {
#[allow(clippy::unused_unit)]
fn dst_size(_: &Tag) -> Self::Metadata {
()
}
}
/* TODO doesn't work, missing support in Rust (so far):
https://github.com/rust-lang/rust/issues/20400
fn dst_size(base_tag: &Tag) -> usize {
// The size of the sized portion of the module tag.
let tag_base_size = 16;
assert!(base_tag.size >= 8);
base_tag.size as usize - tag_base_size
}
}
*/
#[cfg(test)]
mod tests {
use super::*;
use core::str::Utf8Error;
#[test]
fn no_tags() {
#[repr(C, align(8))]
struct Bytes([u8; 16]);
let bytes: Bytes = Bytes([
16, 0, 0, 0, // total_size
0, 0, 0, 0, // reserved
0, 0, 0, 0, // end tag type
8, 0, 0, 0, // end tag size
]);
let ptr = bytes.0.as_ptr();
let addr = ptr as usize;
let bi = unsafe { BootInformation::load(ptr.cast()) };
let bi = bi.unwrap();
assert_eq!(addr, bi.start_address());
assert_eq!(addr + bytes.0.len(), bi.end_address());
assert_eq!(bytes.0.len(), bi.total_size());
assert!(bi.elf_sections().is_none());
assert!(bi.memory_map_tag().is_none());
assert!(bi.module_tags().next().is_none());
assert!(bi.boot_loader_name_tag().is_none());
assert!(bi.command_line_tag().is_none());
}
#[test]
#[should_panic]
fn invalid_total_size() {
#[repr(C, align(8))]
struct Bytes([u8; 15]);
let bytes: Bytes = Bytes([
15, 0, 0, 0, // total_size
0, 0, 0, 0, // reserved
0, 0, 0, 0, // end tag type
8, 0, 0, // end tag size
]);
let ptr = bytes.0.as_ptr();
let addr = ptr as usize;
let bi = unsafe { BootInformation::load(ptr.cast()) };
let bi = bi.unwrap();
assert_eq!(addr, bi.start_address());
assert_eq!(addr + bytes.0.len(), bi.end_address());
assert_eq!(bytes.0.len(), bi.total_size());
assert!(bi.elf_sections().is_none());
assert!(bi.memory_map_tag().is_none());
assert!(bi.module_tags().next().is_none());
assert!(bi.boot_loader_name_tag().is_none());
assert!(bi.command_line_tag().is_none());
}
#[test]
#[should_panic]
fn invalid_end_tag() {
#[repr(C, align(8))]
struct Bytes([u8; 16]);
let bytes: Bytes = Bytes([
16, 0, 0, 0, // total_size
0, 0, 0, 0, // reserved
0, 0, 0, 0, // end tag type
9, 0, 0, 0, // end tag size
]);
let ptr = bytes.0.as_ptr();
let addr = ptr as usize;
let bi = unsafe { BootInformation::load(ptr.cast()) };
let bi = bi.unwrap();
assert_eq!(addr, bi.start_address());
assert_eq!(addr + bytes.0.len(), bi.end_address());
assert_eq!(bytes.0.len(), bi.total_size());
assert!(bi.elf_sections().is_none());
assert!(bi.memory_map_tag().is_none());
assert!(bi.module_tags().next().is_none());
assert!(bi.boot_loader_name_tag().is_none());
assert!(bi.command_line_tag().is_none());
}
#[test]
#[cfg_attr(miri, ignore)]
fn name_tag() {
#[repr(C, align(8))]
struct Bytes([u8; 32]);
let bytes: Bytes = Bytes([
32, 0, 0, 0, // total_size
0, 0, 0, 0, // reserved
2, 0, 0, 0, // boot loader name tag type
13, 0, 0, 0, // boot loader name tag size
110, 97, 109, 101, // boot loader name 'name'
0, 0, 0, 0, // boot loader name null + padding
0, 0, 0, 0, // end tag type
8, 0, 0, 0, // end tag size
]);
let ptr = bytes.0.as_ptr();
let addr = ptr as usize;
let bi = unsafe { BootInformation::load(ptr.cast()) };
let bi = bi.unwrap();
assert_eq!(addr, bi.start_address());
assert_eq!(addr + bytes.0.len(), bi.end_address());
assert_eq!(bytes.0.len(), bi.total_size());
assert!(bi.elf_sections().is_none());
assert!(bi.memory_map_tag().is_none());
assert!(bi.module_tags().next().is_none());
assert_eq!(
"name",
bi.boot_loader_name_tag()
.expect("tag must be present")
.name()
.expect("must be valid utf8")
);
assert!(bi.command_line_tag().is_none());
}
#[test]
#[cfg_attr(miri, ignore)]
fn framebuffer_tag_rgb() {
// direct RGB mode test:
// taken from GRUB2 running in QEMU at
// 1280x720 with 32bpp in BGRA format.
#[repr(C, align(8))]
struct Bytes([u8; 56]);
let bytes: Bytes = Bytes([
56, 0, 0, 0, // total size
0, 0, 0, 0, // reserved
8, 0, 0, 0, // framebuffer tag type
40, 0, 0, 0, // framebuffer tag size
0, 0, 0, 253, // framebuffer low dword of address
0, 0, 0, 0, // framebuffer high dword of address
0, 20, 0, 0, // framebuffer pitch
0, 5, 0, 0, // framebuffer width
208, 2, 0, 0, // framebuffer height
32, 1, 0, 0, // framebuffer bpp, type, reserved word
16, 8, 8, 8, // framebuffer red pos/size, green pos/size
0, 8, 0, 0, // framebuffer blue pos/size, padding word
0, 0, 0, 0, // end tag type
8, 0, 0, 0, // end tag size
]);
let ptr = bytes.0.as_ptr();
let addr = ptr as usize;
let bi = unsafe { BootInformation::load(ptr.cast()) };
let bi = bi.unwrap();
assert_eq!(addr, bi.start_address());
assert_eq!(addr + bytes.0.len(), bi.end_address());
assert_eq!(bytes.0.len(), bi.total_size());
use framebuffer::{FramebufferField, FramebufferType};
assert!(bi.framebuffer_tag().is_some());
let fbi = bi
.framebuffer_tag()
.expect("Framebuffer info should be available")
.expect("Framebuffer info type should be valid");
assert_eq!(fbi.address(), 4244635648);
assert_eq!(fbi.pitch(), 5120);
assert_eq!(fbi.width(), 1280);
assert_eq!(fbi.height(), 720);
assert_eq!(fbi.bpp(), 32);
assert_eq!(
fbi.buffer_type().unwrap(),
FramebufferType::RGB {
red: FramebufferField {
position: 16,
size: 8,
},
green: FramebufferField {
position: 8,
size: 8,
},
blue: FramebufferField {
position: 0,
size: 8,
},
}
);
}
#[test]
#[cfg_attr(miri, ignore)]
fn framebuffer_tag_indexed() {
// indexed mode test:
// this is synthetic, as I can't get QEMU
// to run in indexed color mode.
#[repr(C, align(8))]
struct Bytes([u8; 64]);
let bytes: Bytes = Bytes([
64, 0, 0, 0, // total size
0, 0, 0, 0, // reserved
8, 0, 0, 0, // framebuffer tag type
48, 0, 0, 0, // framebuffer tag size
0, 0, 0, 253, // framebuffer low dword of address
0, 0, 0, 0, // framebuffer high dword of address
0, 20, 0, 0, // framebuffer pitch
0, 5, 0, 0, // framebuffer width
208, 2, 0, 0, // framebuffer height
32, 0, 0, 0, // framebuffer bpp, type, reserved word
4, 0, 0, 0, // framebuffer palette length
255, 0, 0, 0, // framebuffer palette
255, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, // end tag type
8, 0, 0, 0, // end tag size
]);
let ptr = bytes.0.as_ptr();
let addr = ptr as usize;
let bi = unsafe { BootInformation::load(ptr.cast()) };
let bi = bi.unwrap();
assert_eq!(addr, bi.start_address());
assert_eq!(addr + bytes.0.len(), bi.end_address());
assert_eq!(bytes.0.len(), bi.total_size());
use framebuffer::{FramebufferColor, FramebufferType};
assert!(bi.framebuffer_tag().is_some());
let fbi = bi
.framebuffer_tag()
.expect("Framebuffer info should be available")
.expect("Framebuffer info type should be valid");
assert_eq!(fbi.address(), 4244635648);
assert_eq!(fbi.pitch(), 5120);
assert_eq!(fbi.width(), 1280);
assert_eq!(fbi.height(), 720);
assert_eq!(fbi.bpp(), 32);
match fbi.buffer_type().unwrap() {
FramebufferType::Indexed { palette } => assert_eq!(
palette,
[
FramebufferColor {
red: 255,
green: 0,
blue: 0,
},
FramebufferColor {
red: 0,
green: 255,
blue: 0,
},
FramebufferColor {
red: 0,
green: 0,
blue: 255,
},
FramebufferColor {
red: 0,
green: 0,
blue: 0,
}
]
),
_ => panic!("Expected indexed framebuffer type."),
}
}
#[test]
#[cfg_attr(miri, ignore)]
fn vbe_info_tag() {
//Taken from GRUB2 running in QEMU.
#[repr(C, align(8))]
struct Bytes([u8; 800]);
let bytes = Bytes([
32, 3, 0, 0, // Total size.
0, 0, 0, 0, // Reserved
7, 0, 0, 0, // Tag type.
16, 3, 0, 0, // Tag size.
122, 65, 255, 255, // VBE mode, protected mode interface segment,
0, 96, 79, 0, // protected mode interface offset, and length.
86, 69, 83, 65, // "VESA" signature.
0, 3, 220, 87, // VBE version, lower half of OEM string ptr,
0, 192, 1, 0, // upper half of OEM string ptr, lower half of capabilities
0, 0, 34, 128, // upper half of capabilities, lower half of vide mode ptr,
0, 96, 0, 1, // upper half of video mode ptr, number of 64kb memory blocks
0, 0, 240, 87, // OEM software revision, lower half of OEM vendor string ptr,
0, 192, 3,
88, // upper half of OEM vendor string ptr, lower half of OEM product string ptr,
0, 192, 23,
88, // upper half of OEM product string ptr, lower half of OEM revision string ptr,
0, 192, 0, 1, // upper half of OEM revision string ptr.
1, 1, 2, 1, // Reserved data....
3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 13, 1, 14, 1, 15, 1, 16, 1, 17, 1, 18, 1, 19, 1, 20, 1,
21, 1, 22, 1, 23, 1, 24, 1, 25, 1, 26, 1, 27, 1, 28, 1, 29, 1, 30, 1, 31, 1, 64, 1, 65,
1, 66, 1, 67, 1, 68, 1, 69, 1, 70, 1, 71, 1, 72, 1, 73, 1, 74, 1, 75, 1, 76, 1, 117, 1,
118, 1, 119, 1, 120, 1, 121, 1, 122, 1, 123, 1, 124, 1, 125, 1, 126, 1, 127, 1, 128, 1,
129, 1, 130, 1, 131, 1, 132, 1, 133, 1, 134, 1, 135, 1, 136, 1, 137, 1, 138, 1, 139, 1,
140, 1, 141, 1, 142, 1, 143, 1, 144, 1, 145, 1, 146, 1, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0,
5, 0, 6, 0, 7, 0, 13, 0, 14, 0, 15, 0, 16, 0, 17, 0, 18, 0, 19, 0, 106, 0, 255, 255, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // Until Here
187, 0, 7, 0, // Mode attributes, window A and B attributes
64, 0, 64, 0, // Window granularity and size.
0, 160, 0, 0, // Window A and B segments.
186, 84, 0, 192, // Window relocation function pointer.
0, 20, 0, 5, // Pitch, X resolution.
32, 3, 8, 16, // Y resolution, X char size, Y char size.
1, 32, 1, 6, // Number of planes, BPP, number of banks, memory model.
0, 3, 1, 8, // Bank size, number of images, reserved, red mask size.
16, 8, 8,
8, // Red mask position, green mask size, green mask position, blue mask size,
0, 8, 24,
2, // blue mask position, reserved mask size, reserved mask position, color attributes.
0, 0, 0, 253, // Frame buffer base address.
0, 0, 0, 0, // Off screen memory offset.
0, 0, 0, 20, // Off screen memory size, reserved data...
0, 0, 8, 16, 8, 8, 8, 0, 8, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, // Until here.
0, 0, 0, 0, // End tag type.
8, 0, 0, 0, // End tag size.
]);
let ptr = bytes.0.as_ptr();
let addr = ptr as usize;
let bi = unsafe { BootInformation::load(ptr.cast()) };
let bi = bi.unwrap();
assert_eq!(addr, bi.start_address());
assert_eq!(addr + bytes.0.len(), bi.end_address());
assert_eq!(bytes.0.len(), bi.total_size());
assert!(bi.vbe_info_tag().is_some());
let vbe = bi.vbe_info_tag().unwrap();
use vbe_info::*;
assert_eq!({ vbe.mode }, 16762);
assert_eq!({ vbe.interface_segment }, 65535);
assert_eq!({ vbe.interface_offset }, 24576);
assert_eq!({ vbe.interface_length }, 79);
assert_eq!({ vbe.control_info.signature }, [86, 69, 83, 65]);
assert_eq!({ vbe.control_info.version }, 768);
assert_eq!({ vbe.control_info.oem_string_ptr }, 3221247964);
assert_eq!(
{ vbe.control_info.capabilities },
VBECapabilities::SWITCHABLE_DAC
);
assert_eq!({ vbe.control_info.mode_list_ptr }, 1610645538);
assert_eq!({ vbe.control_info.total_memory }, 256);
assert_eq!({ vbe.control_info.oem_software_revision }, 0);
assert_eq!({ vbe.control_info.oem_vendor_name_ptr }, 3221247984);
assert_eq!({ vbe.control_info.oem_product_name_ptr }, 3221248003);
assert_eq!({ vbe.control_info.oem_product_revision_ptr }, 3221248023);
assert!({ vbe.mode_info.mode_attributes }.contains(
VBEModeAttributes::SUPPORTED
| VBEModeAttributes::COLOR
| VBEModeAttributes::GRAPHICS
| VBEModeAttributes::NOT_VGA_COMPATIBLE
| VBEModeAttributes::LINEAR_FRAMEBUFFER
));
assert!(vbe.mode_info.window_a_attributes.contains(
VBEWindowAttributes::RELOCATABLE
| VBEWindowAttributes::READABLE
| VBEWindowAttributes::WRITEABLE
));
assert_eq!({ vbe.mode_info.window_granularity }, 64);
assert_eq!({ vbe.mode_info.window_size }, 64);
assert_eq!({ vbe.mode_info.window_a_segment }, 40960);
assert_eq!({ vbe.mode_info.window_function_ptr }, 3221247162);
assert_eq!({ vbe.mode_info.pitch }, 5120);
assert_eq!({ vbe.mode_info.resolution }, (1280, 800));
assert_eq!(vbe.mode_info.character_size, (8, 16));
assert_eq!(vbe.mode_info.number_of_planes, 1);
assert_eq!(vbe.mode_info.bpp, 32);
assert_eq!(vbe.mode_info.number_of_banks, 1);
assert_eq!(vbe.mode_info.memory_model, VBEMemoryModel::DirectColor);
assert_eq!(vbe.mode_info.bank_size, 0);
assert_eq!(vbe.mode_info.number_of_image_pages, 3);
assert_eq!(
vbe.mode_info.red_field,
VBEField {
position: 16,
size: 8,
}
);
assert_eq!(
vbe.mode_info.green_field,
VBEField {
position: 8,
size: 8,
}
);
assert_eq!(
vbe.mode_info.blue_field,
VBEField {
position: 0,
size: 8,
}
);
assert_eq!(
vbe.mode_info.reserved_field,
VBEField {
position: 24,
size: 8,
}
);
assert_eq!(
vbe.mode_info.direct_color_attributes,
VBEDirectColorAttributes::RESERVED_USABLE
);
assert_eq!({ vbe.mode_info.framebuffer_base_ptr }, 4244635648);
assert_eq!({ vbe.mode_info.offscreen_memory_offset }, 0);
assert_eq!({ vbe.mode_info.offscreen_memory_size }, 0);
}
#[test]
/// Compile time test for [`VBEInfoTag`].
fn vbe_info_tag_size() {
unsafe {
// 16 for the start + 512 from `VBEControlInfo` + 256 from `VBEModeInfo`.
core::mem::transmute::<[u8; 784], VBEInfoTag>([0u8; 784]);
}
}
/// Tests to parse a MBI that was statically extracted from a test run with
/// GRUB as bootloader.
#[test]
#[cfg_attr(miri, ignore)]
fn grub2() {
#[repr(C, align(8))]
struct Bytes([u8; 960]);
let mut bytes: Bytes = Bytes([
192, 3, 0, 0, // total_size
0, 0, 0, 0, // reserved
1, 0, 0, 0, // boot command tag type
9, 0, 0, 0, // boot command tag size
0, 0, 0, 0, // boot command null + padding
0, 0, 0, 0, // boot command padding
2, 0, 0, 0, // boot loader name tag type
26, 0, 0, 0, // boot loader name tag size
71, 82, 85, 66, // boot loader name
32, 50, 46, 48, // boot loader name
50, 126, 98, 101, // boot loader name
116, 97, 51, 45, // boot loader name
53, 0, 0, 0, // boot loader name null + padding
0, 0, 0, 0, // boot loader name padding
10, 0, 0, 0, // APM tag type
28, 0, 0, 0, // APM tag size
2, 1, 0, 240, // APM version, cseg
207, 212, 0, 0, // APM offset
0, 240, 0, 240, // APM cseg_16, dseg
3, 0, 240, 255, // APM flags, cseg_len
240, 255, 240, 255, // APM cseg_16_len, dseg_len
0, 0, 0, 0, // APM padding
6, 0, 0, 0, // memory map tag type
160, 0, 0, 0, // memory map tag size
24, 0, 0, 0, // memory map entry_size
0, 0, 0, 0, // memory map entry_version
0, 0, 0, 0, // memory map entry 0 base_addr
0, 0, 0, 0, // memory map entry 0 base_addr
0, 252, 9, 0, // memory map entry 0 length
0, 0, 0, 0, // memory map entry 0 length
1, 0, 0, 0, // memory map entry 0 type
0, 0, 0, 0, // memory map entry 0 reserved
0, 252, 9, 0, // memory map entry 1 base_addr
0, 0, 0, 0, // memory map entry 1 base_addr
0, 4, 0, 0, // memory map entry 1 length
0, 0, 0, 0, // memory map entry 1 length
2, 0, 0, 0, // memory map entry 1 type
0, 0, 0, 0, // memory map entry 1 reserved
0, 0, 15, 0, // memory map entry 2 base_addr
0, 0, 0, 0, // memory map entry 2 base_addr
0, 0, 1, 0, // memory map entry 2 length
0, 0, 0, 0, // memory map entry 2 length
2, 0, 0, 0, // memory map entry 2 type
0, 0, 0, 0, // memory map entry 2 reserved
0, 0, 16, 0, // memory map entry 3 base_addr
0, 0, 0, 0, // memory map entry 3 base_addr
0, 0, 238, 7, // memory map entry 3 length
0, 0, 0, 0, // memory map entry 3 length
1, 0, 0, 0, // memory map entry 3 type
0, 0, 0, 0, // memory map entry 3 reserved
0, 0, 254, 7, // memory map entry 4 base_addr
0, 0, 0, 0, // memory map entry 4 base_addr
0, 0, 2, 0, // memory map entry 4 length
0, 0, 0, 0, // memory map entry 4 length
2, 0, 0, 0, // memory map entry 4 type
0, 0, 0, 0, // memory map entry 4 reserved
0, 0, 252, 255, // memory map entry 5 base_addr
0, 0, 0, 0, // memory map entry 5 base_addr
0, 0, 4, 0, // memory map entry 5 length
0, 0, 0, 0, // memory map entry 5 length
2, 0, 0, 0, // memory map entry 5 type
0, 0, 0, 0, // memory map entry 5 reserved
9, 0, 0, 0, // elf symbols tag type
84, 2, 0, 0, // elf symbols tag size
9, 0, 0, 0, // elf symbols num
64, 0, 0, 0, // elf symbols entsize
8, 0, 0, 0, // elf symbols shndx
0, 0, 0, 0, // elf symbols entry 0 name
0, 0, 0, 0, // elf symbols entry 0 type
0, 0, 0, 0, // elf symbols entry 0 flags
0, 0, 0, 0, // elf symbols entry 0 flags
0, 0, 0, 0, // elf symbols entry 0 addr
0, 0, 0, 0, // elf symbols entry 0 addr
0, 0, 0, 0, // elf symbols entry 0 offset
0, 0, 0, 0, // elf symbols entry 0 offset
0, 0, 0, 0, // elf symbols entry 0 size
0, 0, 0, 0, // elf symbols entry 0 size
0, 0, 0, 0, // elf symbols entry 0 link
0, 0, 0, 0, // elf symbols entry 0 info
0, 0, 0, 0, // elf symbols entry 0 addralign
0, 0, 0, 0, // elf symbols entry 0 addralign
0, 0, 0, 0, // elf symbols entry 0 entsize
0, 0, 0, 0, // elf symbols entry 0 entsize
27, 0, 0, 0, // elf symbols entry 1 name
1, 0, 0, 0, // elf symbols entry 1 type
2, 0, 0, 0, // elf symbols entry 1 flags
0, 0, 0, 0, // elf symbols entry 1 flags
0, 0, 16, 0, // elf symbols entry 1 addr
0, 128, 255, 255, // elf symbols entry 1 addr
0, 16, 0, 0, // elf symbols entry 1 offset
0, 0, 0, 0, // elf symbols entry 1 offset
0, 48, 0, 0, // elf symbols entry 1 size
0, 0, 0, 0, // elf symbols entry 1 size
0, 0, 0, 0, // elf symbols entry 1 link
0, 0, 0, 0, // elf symbols entry 1 info
16, 0, 0, 0, // elf symbols entry 1 addralign
0, 0, 0, 0, // elf symbols entry 1 addralign
0, 0, 0, 0, // elf symbols entry 1 entsize
0, 0, 0, 0, // elf symbols entry 1 entsize
35, 0, 0, 0, // elf symbols entry 2 name
1, 0, 0, 0, // elf symbols entry 2 type
6, 0, 0, 0, // elf symbols entry 2 flags
0, 0, 0, 0, // elf symbols entry 2 flags
0, 48, 16, 0, // elf symbols entry 2 addr
0, 128, 255, 255, // elf symbols entry 2 addr
0, 64, 0, 0, // elf symbols entry 2 offset
0, 0, 0, 0, // elf symbols entry 2 offset
0, 144, 0, 0, // elf symbols entry 2 size
0, 0, 0, 0, // elf symbols entry 2 size
0, 0, 0, 0, // elf symbols entry 2 link
0, 0, 0, 0, // elf symbols entry 2 info
16, 0, 0, 0, // elf symbols entry 2 addralign
0, 0, 0, 0, // elf symbols entry 2 addralign
0, 0, 0, 0, // elf symbols entry 2 entsize
0, 0, 0, 0, // elf symbols entry 2 entsize
41, 0, 0, 0, // elf symbols entry 3 name
1, 0, 0, 0, // elf symbols entry 3 type
3, 0, 0, 0, // elf symbols entry 3 flags
0, 0, 0, 0, // elf symbols entry 3 flags
0, 192, 16, 0, // elf symbols entry 3 addr
0, 128, 255, 255, // elf symbols entry 3 addr
0, 208, 0, 0, // elf symbols entry 3 offset
0, 0, 0, 0, // elf symbols entry 3 offset
0, 32, 0, 0, // elf symbols entry 3 size
0, 0, 0, 0, // elf symbols entry 3 size
0, 0, 0, 0, // elf symbols entry 3 link
0, 0, 0, 0, // elf symbols entry 3 info
8, 0, 0, 0, // elf symbols entry 3 addralign
0, 0, 0, 0, // elf symbols entry 3 addralign
0, 0, 0, 0, // elf symbols entry 3 entsize
0, 0, 0, 0, // elf symbols entry 3 entsize
47, 0, 0, 0, // elf symbols entry 4 name
8, 0, 0, 0, // elf symbols entry 4 type
3, 0, 0, 0, // elf symbols entry 4 flags
0, 0, 0, 0, // elf symbols entry 4 flags
0, 224, 16, 0, // elf symbols entry 4 addr
0, 128, 255, 255, // elf symbols entry 4 addr
0, 240, 0, 0, // elf symbols entry 4 offset
0, 0, 0, 0, // elf symbols entry 4 offset
0, 80, 0, 0, // elf symbols entry 4 size
0, 0, 0, 0, // elf symbols entry 4 size
0, 0, 0, 0, // elf symbols entry 4 link
0, 0, 0, 0, // elf symbols entry 4 info
0, 16, 0, 0, // elf symbols entry 4 addralign
0, 0, 0, 0, // elf symbols entry 4 addralign
0, 0, 0, 0, // elf symbols entry 4 entsize
0, 0, 0, 0, // elf symbols entry 4 entsize
52, 0, 0, 0, // elf symbols entry 5 name
1, 0, 0, 0, // elf symbols entry 5 type
3, 0, 0, 0, // elf symbols entry 5 flags
0, 0, 0, 0, // elf symbols entry 5 flags
0, 48, 17, 0, // elf symbols entry 5 addr
0, 128, 255, 255, // elf symbols entry 5 addr
0, 240, 0, 0, // elf symbols entry 5 offset
0, 0, 0, 0, // elf symbols entry 5 offset
0, 0, 0, 0, // elf symbols entry 5 size
0, 0, 0, 0, // elf symbols entry 5 size
0, 0, 0, 0, // elf symbols entry 5 link
0, 0, 0, 0, // elf symbols entry 5 info
1, 0, 0, 0, // elf symbols entry 5 addralign
0, 0, 0, 0, // elf symbols entry 5 addralign
0, 0, 0, 0, // elf symbols entry 5 entsize
0, 0, 0, 0, // elf symbols entry 5 entsize
1, 0, 0, 0, // elf symbols entry 6 name
2, 0, 0, 0, // elf symbols entry 6 type
0, 0, 0, 0, // elf symbols entry 6 flags
0, 0, 0, 0, // elf symbols entry 6 flags
0, 48, 17, 0, // elf symbols entry 6 addr
0, 0, 0, 0, // elf symbols entry 6 addr
0, 240, 0, 0, // elf symbols entry 6 offset
0, 0, 0, 0, // elf symbols entry 6 offset
224, 43, 0, 0, // elf symbols entry 6 size
0, 0, 0, 0, // elf symbols entry 6 size
7, 0, 0, 0, // elf symbols entry 6 link
102, 1, 0, 0, // elf symbols entry 6 info
8, 0, 0, 0, // elf symbols entry 6 addralign
0, 0, 0, 0, // elf symbols entry 6 addralign
24, 0, 0, 0, // elf symbols entry 6 entsize
0, 0, 0, 0, // elf symbols entry 6 entsize
9, 0, 0, 0, // elf symbols entry 7 name
3, 0, 0, 0, // elf symbols entry 7 type
0, 0, 0, 0, // elf symbols entry 7 flags
0, 0, 0, 0, // elf symbols entry 7 flags
224, 91, 17, 0, // elf symbols entry 7 addr
0, 0, 0, 0, // elf symbols entry 7 addr
224, 27, 1, 0, // elf symbols entry 7 offset
0, 0, 0, 0, // elf symbols entry 7 offset
145, 55, 0, 0, // elf symbols entry 7 size
0, 0, 0, 0, // elf symbols entry 7 size
0, 0, 0, 0, // elf symbols entry 7 link
0, 0, 0, 0, // elf symbols entry 7 info
1, 0, 0, 0, // elf symbols entry 7 addralign
0, 0, 0, 0, // elf symbols entry 7 addralign
0, 0, 0, 0, // elf symbols entry 7 entsize
0, 0, 0, 0, // elf symbols entry 7 entsize
17, 0, 0, 0, // elf symbols entry 8 name
3, 0, 0, 0, // elf symbols entry 8 type
0, 0, 0, 0, // elf symbols entry 8 flags
0, 0, 0, 0, // elf symbols entry 8 flags
113, 147, 17, 0, // elf symbols entry 8 addr
0, 0, 0, 0, // elf symbols entry 8 addr
113, 83, 1, 0, // elf symbols entry 8 offset
0, 0, 0, 0, // elf symbols entry 8 offset
65, 0, 0, 0, // elf symbols entry 8 size
0, 0, 0, 0, // elf symbols entry 8 size
0, 0, 0, 0, // elf symbols entry 8 link
0, 0, 0, 0, // elf symbols entry 8 info
1, 0, 0, 0, // elf symbols entry 8 addralign
0, 0, 0, 0, // elf symbols entry 8 addralign
0, 0, 0, 0, // elf symbols entry 8 entsize
0, 0, 0, 0, // elf symbols entry 8 entsize
0, 0, 0, 0, // elf symbols padding
4, 0, 0, 0, // basic memory tag type
16, 0, 0, 0, // basic memory tag size
127, 2, 0, 0, // basic memory mem_lower
128, 251, 1, 0, // basic memory mem_upper
5, 0, 0, 0, // BIOS boot device tag type
20, 0, 0, 0, // BIOS boot device tag size
224, 0, 0, 0, // BIOS boot device biosdev
255, 255, 255, 255, // BIOS boot device partition
255, 255, 255, 255, // BIOS boot device subpartition
0, 0, 0, 0, // BIOS boot device padding
8, 0, 0, 0, // framebuffer info tag type
32, 0, 0, 0, // framebuffer info tag size
0, 128, 11, 0, // framebuffer info framebuffer_addr
0, 0, 0, 0, // framebuffer info framebuffer_addr
160, 0, 0, 0, // framebuffer info framebuffer_pitch
80, 0, 0, 0, // framebuffer info framebuffer_width
25, 0, 0, 0, // framebuffer info framebuffer_height
16, 2, 0, 0, // framebuffer info framebuffer_[bpp,type], reserved, color_info
14, 0, 0, 0, // ACPI old tag type
28, 0, 0, 0, // ACPI old tag size
82, 83, 68, 32, // ACPI old
80, 84, 82, 32, // ACPI old
89, 66, 79, 67, // ACPI old
72, 83, 32, 0, // ACPI old
220, 24, 254, 7, // ACPI old
0, 0, 0, 0, // ACPI old padding
0, 0, 0, 0, // end tag type
8, 0, 0, 0, // end tag size
]);
#[repr(C, align(8))]
struct StringBytes([u8; 65]);
let string_bytes: StringBytes = StringBytes([
0, 46, 115, 121, 109, 116, 97, 98, 0, 46, 115, 116, 114, 116, 97, 98, 0, 46, 115, 104,
115, 116, 114, 116, 97, 98, 0, 46, 114, 111, 100, 97, 116, 97, 0, 46, 116, 101, 120,
116, 0, 46, 100, 97, 116, 97, 0, 46, 98, 115, 115, 0, 46, 100, 97, 116, 97, 46, 114,
101, 108, 46, 114, 111, 0,
]);
let string_addr = string_bytes.0.as_ptr() as u64;
for i in 0..8 {
bytes.0[796 + i] = (string_addr >> (i * 8)) as u8;
}
let ptr = bytes.0.as_ptr();
let addr = ptr as usize;
let bi = unsafe { BootInformation::load(ptr.cast()) };
let bi = bi.unwrap();
test_grub2_boot_info(&bi, addr, string_addr, &bytes.0, &string_bytes.0);
let bi = unsafe { load_with_offset(addr, 0) };
let bi = bi.unwrap();
test_grub2_boot_info(&bi, addr, string_addr, &bytes.0, &string_bytes.0);
let offset = 8usize;
let bi = unsafe { load_with_offset(addr - offset, offset) };
let bi = bi.unwrap();
test_grub2_boot_info(&bi, addr, string_addr, &bytes.0, &string_bytes.0);
// Check that the MBI's debug output can be printed without SEGFAULT.
// If this works, it is a good indicator than transitively a lot of
// stuff works.
println!("{bi:#?}");
}
/// Helper for [`grub2`].
fn test_grub2_boot_info(
bi: &BootInformation,
addr: usize,
string_addr: u64,
bytes: &[u8],
string_bytes: &[u8],
) {
assert_eq!(addr, bi.start_address());
assert_eq!(addr + bytes.len(), bi.end_address());
assert_eq!(bytes.len(), bi.total_size());
let mut es = bi.elf_sections().unwrap();
let s1 = es.next().expect("Should have one more section");
assert_eq!(".rodata", s1.name().expect("Should be valid utf-8"));
assert_eq!(0xFFFF_8000_0010_0000, s1.start_address());
assert_eq!(0xFFFF_8000_0010_3000, s1.end_address());
assert_eq!(0x0000_0000_0000_3000, s1.size());
assert_eq!(ElfSectionFlags::ALLOCATED, s1.flags());
assert_eq!(ElfSectionType::ProgramSection, s1.section_type());
let s2 = es.next().expect("Should have one more section");
assert_eq!(".text", s2.name().expect("Should be valid utf-8"));
assert_eq!(0xFFFF_8000_0010_3000, s2.start_address());
assert_eq!(0xFFFF_8000_0010_C000, s2.end_address());
assert_eq!(0x0000_0000_0000_9000, s2.size());
assert_eq!(
ElfSectionFlags::EXECUTABLE | ElfSectionFlags::ALLOCATED,
s2.flags()
);
assert_eq!(ElfSectionType::ProgramSection, s2.section_type());
let s3 = es.next().expect("Should have one more section");
assert_eq!(".data", s3.name().expect("Should be valid utf-8"));
assert_eq!(0xFFFF_8000_0010_C000, s3.start_address());
assert_eq!(0xFFFF_8000_0010_E000, s3.end_address());
assert_eq!(0x0000_0000_0000_2000, s3.size());
assert_eq!(
ElfSectionFlags::ALLOCATED | ElfSectionFlags::WRITABLE,
s3.flags()
);
assert_eq!(ElfSectionType::ProgramSection, s3.section_type());
let s4 = es.next().expect("Should have one more section");
assert_eq!(".bss", s4.name().expect("Should be valid utf-8"));
assert_eq!(0xFFFF_8000_0010_E000, s4.start_address());
assert_eq!(0xFFFF_8000_0011_3000, s4.end_address());
assert_eq!(0x0000_0000_0000_5000, s4.size());
assert_eq!(
ElfSectionFlags::ALLOCATED | ElfSectionFlags::WRITABLE,
s4.flags()
);
assert_eq!(ElfSectionType::Uninitialized, s4.section_type());
let s5 = es.next().expect("Should have one more section");
assert_eq!(".data.rel.ro", s5.name().expect("Should be valid utf-8"));
assert_eq!(0xFFFF_8000_0011_3000, s5.start_address());
assert_eq!(0xFFFF_8000_0011_3000, s5.end_address());
assert_eq!(0x0000_0000_0000_0000, s5.size());
assert_eq!(
ElfSectionFlags::ALLOCATED | ElfSectionFlags::WRITABLE,
s5.flags()
);
assert_eq!(ElfSectionType::ProgramSection, s5.section_type());
let s6 = es.next().expect("Should have one more section");
assert_eq!(".symtab", s6.name().expect("Should be valid utf-8"));
assert_eq!(0x0000_0000_0011_3000, s6.start_address());
assert_eq!(0x0000_0000_0011_5BE0, s6.end_address());
assert_eq!(0x0000_0000_0000_2BE0, s6.size());
assert_eq!(ElfSectionFlags::empty(), s6.flags());
assert_eq!(ElfSectionType::LinkerSymbolTable, s6.section_type());
let s7 = es.next().expect("Should have one more section");
assert_eq!(".strtab", s7.name().expect("Should be valid utf-8"));
assert_eq!(0x0000_0000_0011_5BE0, s7.start_address());
assert_eq!(0x0000_0000_0011_9371, s7.end_address());
assert_eq!(0x0000_0000_0000_3791, s7.size());
assert_eq!(ElfSectionFlags::empty(), s7.flags());
assert_eq!(ElfSectionType::StringTable, s7.section_type());
let s8 = es.next().expect("Should have one more section");
assert_eq!(".shstrtab", s8.name().expect("Should be valid utf-8"));
assert_eq!(string_addr, s8.start_address());
assert_eq!(string_addr + string_bytes.len() as u64, s8.end_address());
assert_eq!(string_bytes.len() as u64, s8.size());
assert_eq!(ElfSectionFlags::empty(), s8.flags());
assert_eq!(ElfSectionType::StringTable, s8.section_type());
assert!(es.next().is_none());
let mut mm = bi
.memory_map_tag()
.unwrap()
.memory_areas()
.iter()
.filter(|area| area.typ() == MemoryAreaType::Available);
let mm1 = mm.next().unwrap();
assert_eq!(0x00000000, mm1.start_address());
assert_eq!(0x009_FC00, mm1.end_address());
assert_eq!(0x009_FC00, mm1.size());
assert_eq!(MemoryAreaType::Available, mm1.typ());
let mm2 = mm.next().unwrap();
assert_eq!(0x010_0000, mm2.start_address());
assert_eq!(0x7FE_0000, mm2.end_address());
assert_eq!(0x7EE_0000, mm2.size());
assert_eq!(MemoryAreaType::Available, mm2.typ());
assert!(mm.next().is_none());
// Test the RSDP tag
let rsdp_old = bi.rsdp_v1_tag().unwrap();
assert_eq!("RSD PTR ", rsdp_old.signature().unwrap());
assert!(rsdp_old.checksum_is_valid());
assert_eq!("BOCHS ", rsdp_old.oem_id().unwrap());
assert_eq!(0, rsdp_old.revision());
assert_eq!(0x7FE18DC, rsdp_old.rsdt_address());
assert!(bi.module_tags().next().is_none());
assert_eq!(
"GRUB 2.02~beta3-5",
bi.boot_loader_name_tag()
.expect("tag must be present")
.name()
.expect("must be valid utf-8")
);
assert_eq!(
"",
bi.command_line_tag()
.expect("tag must present")
.cmdline()
.expect("must be valid utf-8")
);
// Test the Framebuffer tag
let fbi = bi
.framebuffer_tag()
.expect("Framebuffer info should be available")
.expect("Framebuffer info type should be valid");
assert_eq!(fbi.address(), 753664);
assert_eq!(fbi.pitch(), 160);
assert_eq!(fbi.width(), 80);
assert_eq!(fbi.height(), 25);
assert_eq!(fbi.bpp(), 16);
assert_eq!(fbi.buffer_type(), Ok(FramebufferType::Text));
}
#[test]
#[cfg_attr(miri, ignore)]
fn elf_sections() {
#[repr(C, align(8))]
struct Bytes([u8; 168]);
let mut bytes: Bytes = Bytes([
168, 0, 0, 0, // total_size
0, 0, 0, 0, // reserved
9, 0, 0, 0, // elf symbols tag type
20, 2, 0, 0, // elf symbols tag size
2, 0, 0, 0, // elf symbols num
64, 0, 0, 0, // elf symbols entsize
1, 0, 0, 0, // elf symbols shndx
0, 0, 0, 0, // elf symbols entry 0 name
0, 0, 0, 0, // elf symbols entry 0 type
0, 0, 0, 0, // elf symbols entry 0 flags
0, 0, 0, 0, // elf symbols entry 0 flags
0, 0, 0, 0, // elf symbols entry 0 addr
0, 0, 0, 0, // elf symbols entry 0 addr
0, 0, 0, 0, // elf symbols entry 0 offset
0, 0, 0, 0, // elf symbols entry 0 offset
0, 0, 0, 0, // elf symbols entry 0 size
0, 0, 0, 0, // elf symbols entry 0 size
0, 0, 0, 0, // elf symbols entry 0 link
0, 0, 0, 0, // elf symbols entry 0 info
0, 0, 0, 0, // elf symbols entry 0 addralign
0, 0, 0, 0, // elf symbols entry 0 addralign
0, 0, 0, 0, // elf symbols entry 0 entsize
0, 0, 0, 0, // elf symbols entry 0 entsize
1, 0, 0, 0, // elf symbols entry 1 name
3, 0, 0, 0, // elf symbols entry 1 type
0, 0, 0, 0, // elf symbols entry 1 flags
0, 0, 0, 0, // elf symbols entry 1 flags
255, 255, 255, 255, // elf symbols entry 1 addr
255, 255, 255, 255, // elf symbols entry 1 addr
113, 83, 1, 0, // elf symbols entry 1 offset
0, 0, 0, 0, // elf symbols entry 1 offset
11, 0, 0, 0, // elf symbols entry 1 size
0, 0, 0, 0, // elf symbols entry 1 size
0, 0, 0, 0, // elf symbols entry 1 link
0, 0, 0, 0, // elf symbols entry 1 info
1, 0, 0, 0, // elf symbols entry 1 addralign
0, 0, 0, 0, // elf symbols entry 1 addralign
0, 0, 0, 0, // elf symbols entry 1 entsize
0, 0, 0, 0, // elf symbols entry 1 entsize
0, 0, 0, 0, // elf symbols padding
0, 0, 0, 0, // end tag type
8, 0, 0, 0, // end tag size
]);
#[repr(C, align(8))]
struct StringBytes([u8; 11]);
let string_bytes: StringBytes =
StringBytes([0, 46, 115, 104, 115, 116, 114, 116, 97, 98, 0]);
let string_addr = string_bytes.0.as_ptr() as u64;
for i in 0..8 {
let offset = 108;
assert_eq!(255, bytes.0[offset + i]);
bytes.0[offset + i] = (string_addr >> (i * 8)) as u8;
}
let ptr = bytes.0.as_ptr();
let addr = ptr as usize;
let bi = unsafe { BootInformation::load(ptr.cast()) };
let bi = bi.unwrap();
assert_eq!(addr, bi.start_address());
assert_eq!(addr + bytes.0.len(), bi.end_address());
assert_eq!(bytes.0.len(), bi.total_size());
let mut es = bi.elf_sections().unwrap();
let s1 = es.next().expect("Should have one more section");
assert_eq!(".shstrtab", s1.name().expect("Should be valid utf-8"));
assert_eq!(string_addr, s1.start_address());
assert_eq!(string_addr + string_bytes.0.len() as u64, s1.end_address());
assert_eq!(string_bytes.0.len() as u64, s1.size());
assert_eq!(ElfSectionFlags::empty(), s1.flags());
assert_eq!(ElfSectionType::StringTable, s1.section_type());
assert!(es.next().is_none());
}
#[test]
#[cfg_attr(miri, ignore)]
fn efi_memory_map() {
#[repr(C, align(8))]
struct Bytes([u8; 72]);
// test that the EFI memory map is detected.
let bytes: Bytes = Bytes([
72, 0, 0, 0, // size
0, 0, 0, 0, // reserved
17, 0, 0, 0, // EFI memory map type
56, 0, 0, 0, // EFI memory map size
48, 0, 0, 0, // EFI descriptor size
1, 0, 0, 0, // EFI descriptor version, don't think this matters.
7, 0, 0, 0, // Type: EfiConventionalMemory
0, 0, 0, 0, // Padding
0, 0, 16, 0, // Physical Address: should be 0x100000
0, 0, 0, 0, // Extension of physical address.
0, 0, 16, 0, // Virtual Address: should be 0x100000
0, 0, 0, 0, // Extension of virtual address.
4, 0, 0, 0, // 4 KiB Pages: 16 KiB
0, 0, 0, 0, // Extension of pages
0, 0, 0, 0, // Attributes of this memory range.
0, 0, 0, 0, // Extension of attributes
0, 0, 0, 0, // end tag type.
8, 0, 0, 0, // end tag size.
]);
let ptr = bytes.0.as_ptr();
let addr = ptr as usize;
let bi = unsafe { BootInformation::load(ptr.cast()) };
let bi = bi.unwrap();
assert_eq!(addr, bi.start_address());
assert_eq!(addr + bytes.0.len(), bi.end_address());
assert_eq!(bytes.0.len(), bi.total_size());
let efi_memory_map = bi.efi_memory_map_tag().unwrap();
let mut efi_mmap_iter = efi_memory_map.memory_areas();
let desc = efi_mmap_iter.next().unwrap();
assert_eq!(desc.phys_start, 0x100000);
assert_eq!(desc.page_count, 4);
assert_eq!(desc.ty, EFIMemoryAreaType::CONVENTIONAL);
// test that the EFI memory map is not detected if the boot services
// are not exited.
struct Bytes2([u8; 80]);
let bytes2: Bytes2 = Bytes2([
80, 0, 0, 0, // size
0, 0, 0, 0, // reserved
17, 0, 0, 0, // EFI memory map type
56, 0, 0, 0, // EFI memory map size
48, 0, 0, 0, // EFI descriptor size
1, 0, 0, 0, // EFI descriptor version, don't think this matters.
7, 0, 0, 0, // Type: EfiConventionalMemory
0, 0, 0, 0, // Padding
0, 0, 16, 0, // Physical Address: should be 0x100000
0, 0, 0, 0, // Extension of physical address.
0, 0, 16, 0, // Virtual Address: should be 0x100000
0, 0, 0, 0, // Extension of virtual address.
4, 0, 0, 0, // 4 KiB Pages: 16 KiB
0, 0, 0, 0, // Extension of pages
0, 0, 0, 0, // Attributes of this memory range.
0, 0, 0, 0, // Extension of attributes
18, 0, 0, 0, // Tag ExitBootServices not terminated.
8, 0, 0, 0, // Tag ExitBootServices size.
0, 0, 0, 0, // end tag type.
8, 0, 0, 0, // end tag size.
]);
let bi = unsafe { BootInformation::load(bytes2.0.as_ptr().cast()) };
let bi = bi.unwrap();
let efi_mmap = bi.efi_memory_map_tag();
assert!(efi_mmap.is_none());
}
#[test]
#[cfg(feature = "unstable")]
/// This test succeeds if it compiles.
fn mbi_load_error_implements_error() {
fn consumer<E: core::error::Error>(_e: E) {}
consumer(MbiLoadError::IllegalAddress)
}
/// Example for a custom tag.
#[test]
#[cfg_attr(miri, ignore)]
fn get_custom_tag_from_mbi() {
const CUSTOM_TAG_ID: u32 = 0x1337;
#[repr(C, align(8))]
struct CustomTag {
tag: TagTypeId,
size: u32,
foo: u32,
}
#[repr(C, align(8))]
struct AlignedBytes([u8; 32]);
// Raw bytes of a MBI that only contains the custom tag.
let bytes: AlignedBytes = AlignedBytes([
32,
0,
0,
0, // end: total size
0,
0,
0,
0, // end: padding; end of multiboot2 boot information begin
CUSTOM_TAG_ID.to_le_bytes()[0],
CUSTOM_TAG_ID.to_le_bytes()[1],
CUSTOM_TAG_ID.to_le_bytes()[2],
CUSTOM_TAG_ID.to_le_bytes()[3], // end: my custom tag id
12,
0,
0,
0, // end: tag size
42,
0,
0,
0,
0,
0,
0,
0, // 8 byte padding
0,
0,
0,
0, // end: end tag type
8,
0,
0,
0, // end: end tag size
]);
let ptr = bytes.0.as_ptr();
let addr = ptr as usize;
let bi = unsafe { BootInformation::load(ptr.cast()) };
let bi = bi.unwrap();
assert_eq!(addr, bi.start_address());
assert_eq!(addr + bytes.0.len(), bi.end_address());
assert_eq!(bytes.0.len(), bi.total_size());
let tag = bi.get_tag::<CustomTag, _>(CUSTOM_TAG_ID).unwrap();
assert_eq!(tag.foo, 42);
}
/// Example for a custom DST tag.
#[test]
#[cfg_attr(miri, ignore)]
fn get_custom_dst_tag_from_mbi() {
const CUSTOM_TAG_ID: u32 = 0x1337;
#[repr(C)]
#[derive(crate::Pointee)]
struct CustomTag {
tag: TagTypeId,
size: u32,
name: [u8],
}
impl CustomTag {
fn name(&self) -> Result<&str, Utf8Error> {
Tag::get_dst_str_slice(&self.name)
}
}
impl TagTrait for CustomTag {
fn dst_size(base_tag: &Tag) -> usize {
// The size of the sized portion of the command line tag.
let tag_base_size = 8;
assert!(base_tag.size >= 8);
base_tag.size as usize - tag_base_size
}
}
#[repr(C, align(8))]
struct AlignedBytes([u8; 32]);
// Raw bytes of a MBI that only contains the custom tag.
let bytes: AlignedBytes = AlignedBytes([
32,
0,
0,
0, // end: total size
0,
0,
0,
0, // end: padding; end of multiboot2 boot information begin
CUSTOM_TAG_ID.to_le_bytes()[0],
CUSTOM_TAG_ID.to_le_bytes()[1],
CUSTOM_TAG_ID.to_le_bytes()[2],
CUSTOM_TAG_ID.to_le_bytes()[3], // end: my custom tag id
14,
0,
0,
0, // end: tag size
b'h',
b'e',
b'l',
b'l',
b'o',
b'\0',
0,
0, // 2 byte padding
0,
0,
0,
0, // end: end tag type
8,
0,
0,
0, // end: end tag size
]);
let ptr = bytes.0.as_ptr();
let addr = ptr as usize;
let bi = unsafe { BootInformation::load(ptr.cast()) };
let bi = bi.unwrap();
assert_eq!(addr, bi.start_address());
assert_eq!(addr + bytes.0.len(), bi.end_address());
assert_eq!(bytes.0.len(), bi.total_size());
let tag = bi.get_tag::<CustomTag, _>(CUSTOM_TAG_ID).unwrap();
assert_eq!(tag.name(), Ok("hello"));
}
/// Tests that `get_tag` can consume multiple types that implement `Into<TagTypeId>`
#[test]
#[cfg_attr(miri, ignore)]
fn get_tag_into_variants() {
#[repr(C, align(8))]
struct Bytes([u8; 32]);
let bytes: Bytes = Bytes([
32,
0,
0,
0, // total_size
0,
0,
0,
0, // reserved
TagType::Cmdline.val().to_le_bytes()[0],
TagType::Cmdline.val().to_le_bytes()[1],
TagType::Cmdline.val().to_le_bytes()[2],
TagType::Cmdline.val().to_le_bytes()[3],
13,
0,
0,
0, // tag size
110,
97,
109,
101, // ASCII string 'name'
0,
0,
0,
0, // null byte + padding
0,
0,
0,
0, // end tag type
8,
0,
0,
0, // end tag size
]);
let ptr = bytes.0.as_ptr();
let bi = unsafe { BootInformation::load(ptr.cast()) };
let bi = bi.unwrap();
let _tag = bi.get_tag::<CommandLineTag, _>(TagType::Cmdline).unwrap();
let _tag = bi.get_tag::<CommandLineTag, _>(1).unwrap();
let _tag = bi.get_tag::<CommandLineTag, _>(TagTypeId::new(1)).unwrap();
}
}