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,24 +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 ]
412- . try_into ( )
413- . expect ( "header len checked" ) ;
414- 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" ) ?;
415420
416- if tbl_sig == sig_bytes {
421+ if header . signature == sig_bytes {
417422 // Found the table
418- 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 ) ) ;
419424 }
420425
421- if tbl_len == 0 {
426+ if header . length == 0 {
422427 // Invalid table length, stop searching
423- bail ! ( "Found table with zero length at offset {offset}" ) ;
428+ bail ! ( "found table with zero length at offset {offset}" ) ;
424429 }
425430 // Move to the next table
426- offset += tbl_len ;
431+ offset += header . length as usize ;
427432 }
428433
429- bail ! ( "Table not found: {signature}" ) ;
434+ bail ! ( "table not found: {signature}" ) ;
430435}
0 commit comments