77
88use anyhow:: { bail, Context , Result } ;
99use log:: debug;
10+ use scale:: Decode ;
1011
1112use crate :: Machine ;
1213
@@ -392,6 +393,13 @@ fn qemu_loader_append(data: &mut Vec<u8>, cmd: LoaderCmd) {
392393 }
393394}
394395
396+ /// ACPI table header (first 8 bytes of every ACPI table)
397+ #[ derive( Debug , Decode ) ]
398+ struct AcpiTableHeader {
399+ signature : [ u8 ; 4 ] ,
400+ length : u32 ,
401+ }
402+
395403/// Searches for an ACPI table with the given signature and returns its offset,
396404/// checksum offset, and length.
397405fn find_acpi_table ( tables : & [ u8 ] , signature : & str ) -> Result < ( u32 , u32 , u32 ) > {
@@ -407,22 +415,21 @@ fn find_acpi_table(tables: &[u8], signature: &str) -> Result<(u32, u32, u32)> {
407415 bail ! ( "Table not found: {signature}" ) ;
408416 }
409417
410- let tbl_sig = & tables[ offset..offset + 4 ] ;
411- let tbl_len_bytes: [ u8 ; 4 ] = tables[ offset + 4 ..offset + 8 ] . try_into ( ) . unwrap ( ) ;
412- let tbl_len = u32:: from_le_bytes ( tbl_len_bytes) as usize ;
418+ let header = AcpiTableHeader :: decode ( & mut & tables[ offset..] )
419+ . context ( "failed to decode ACPI table header" ) ?;
413420
414- if tbl_sig == sig_bytes {
421+ if header . signature == sig_bytes {
415422 // Found the table
416- return Ok ( ( offset as u32 , ( offset + 9 ) as u32 , tbl_len as u32 ) ) ;
423+ return Ok ( ( offset as u32 , ( offset + 9 ) as u32 , header . length ) ) ;
417424 }
418425
419- if tbl_len == 0 {
426+ if header . length == 0 {
420427 // Invalid table length, stop searching
421- bail ! ( "Found table with zero length at offset {offset}" ) ;
428+ bail ! ( "found table with zero length at offset {offset}" ) ;
422429 }
423430 // Move to the next table
424- offset += tbl_len ;
431+ offset += header . length as usize ;
425432 }
426433
427- bail ! ( "Table not found: {signature}" ) ;
434+ bail ! ( "table not found: {signature}" ) ;
428435}
0 commit comments