diff options
author | Andrew Kaster <akaster@serenityos.org> | 2023-02-17 10:25:55 -0700 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2023-02-19 13:49:07 +0100 |
commit | 895f54f4873198c057b5189c29aba4009d5e1031 (patch) | |
tree | a5d114c2903210f17bbfec6ba8d8d3928d9ff750 /Userland/Libraries/LibDeviceTree/Validation.cpp | |
parent | 01f32a22b4734c3934904b78072c55f2b134ade0 (diff) | |
download | serenity-895f54f4873198c057b5189c29aba4009d5e1031.zip |
LibDeviceTree: Refactor dump() to return ErrorOr, and use ReadonlyBytes
ReadonlyBytes is much nicer to use than a u8 const* + size_t.
Diffstat (limited to 'Userland/Libraries/LibDeviceTree/Validation.cpp')
-rw-r--r-- | Userland/Libraries/LibDeviceTree/Validation.cpp | 41 |
1 files changed, 21 insertions, 20 deletions
diff --git a/Userland/Libraries/LibDeviceTree/Validation.cpp b/Userland/Libraries/LibDeviceTree/Validation.cpp index 7404231b36..a30312a3ea 100644 --- a/Userland/Libraries/LibDeviceTree/Validation.cpp +++ b/Userland/Libraries/LibDeviceTree/Validation.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021, Andrew Kaster <akaster@serenityos.org> + * Copyright (c) 2021-2023, Andrew Kaster <akaster@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -7,11 +7,12 @@ #include <AK/DeprecatedString.h> #include <AK/Endian.h> #include <AK/Format.h> +#include <AK/Try.h> #include <LibDeviceTree/Validation.h> namespace DeviceTree { -bool validate_flattened_device_tree(FlattenedDeviceTreeHeader const& header, u8 const* blob_start, size_t blob_size, Verbose verbose) +bool validate_flattened_device_tree(FlattenedDeviceTreeHeader const& header, ReadonlyBytes raw_device_tree, Verbose verbose) { if (header.magic != 0xD00DFEEDU) { if (verbose == Verbose::Yes) @@ -31,27 +32,27 @@ bool validate_flattened_device_tree(FlattenedDeviceTreeHeader const& header, u8 return false; } - if (header.totalsize != blob_size) { + if (header.totalsize != raw_device_tree.size()) { if (verbose == Verbose::Yes) - warnln("FDT Header total size mismatch: {}, expected {}!", header.totalsize, blob_size); + warnln("FDT Header total size mismatch: {}, expected {}!", header.totalsize, raw_device_tree.size()); return false; } - if (header.off_dt_struct > blob_size) { + if (header.off_dt_struct > raw_device_tree.size()) { if (verbose == Verbose::Yes) - warnln("FDT Header reports larger StructureBlock offset than possible: {} but total size is {}!", header.off_dt_struct, blob_size); + warnln("FDT Header reports larger StructureBlock offset than possible: {} but total size is {}!", header.off_dt_struct, raw_device_tree.size()); return false; } - if (header.off_dt_strings > blob_size) { + if (header.off_dt_strings > raw_device_tree.size()) { if (verbose == Verbose::Yes) - warnln("FDT Header reports larger StringsBlock offset than possible: {} but total size is {}!", header.off_dt_strings, blob_size); + warnln("FDT Header reports larger StringsBlock offset than possible: {} but total size is {}!", header.off_dt_strings, raw_device_tree.size()); return false; } - if (header.off_mem_rsvmap > blob_size) { + if (header.off_mem_rsvmap > raw_device_tree.size()) { if (verbose == Verbose::Yes) - warnln("FDT Header reports larger MemoryReservationBlock offset than possible: {} but total size is {}!", header.off_mem_rsvmap, blob_size); + warnln("FDT Header reports larger MemoryReservationBlock offset than possible: {} but total size is {}!", header.off_mem_rsvmap, raw_device_tree.size()); return false; } @@ -80,7 +81,7 @@ bool validate_flattened_device_tree(FlattenedDeviceTreeHeader const& header, u8 return false; } - auto* mem_reserve_block = reinterpret_cast<FlattenedDeviceTreeReserveEntry const*>(&blob_start[header.off_mem_rsvmap]); + auto* mem_reserve_block = reinterpret_cast<FlattenedDeviceTreeReserveEntry const*>(&raw_device_tree[header.off_mem_rsvmap]); u64 next_block_offset = header.off_mem_rsvmap + sizeof(FlattenedDeviceTreeReserveEntry); while ((next_block_offset < header.off_dt_struct) && (*mem_reserve_block != FlattenedDeviceTreeReserveEntry {})) { ++mem_reserve_block; @@ -95,23 +96,23 @@ bool validate_flattened_device_tree(FlattenedDeviceTreeHeader const& header, u8 // check for overlap. Overflow not possible b/c the fields are u32 u64 structure_block_size = header.off_dt_struct + header.size_dt_struct; - if ((structure_block_size > header.off_dt_strings) || (structure_block_size > blob_size)) { + if ((structure_block_size > header.off_dt_strings) || (structure_block_size > raw_device_tree.size())) { if (verbose == Verbose::Yes) - warnln("FDT Header reports invalid StructureBlock block size: {} is too large given StringsBlock offset {} and total size {}", structure_block_size, header.off_dt_strings, blob_size); + warnln("FDT Header reports invalid StructureBlock block size: {} is too large given StringsBlock offset {} and total size {}", structure_block_size, header.off_dt_strings, raw_device_tree.size()); return false; } u64 strings_block_size = header.off_dt_strings + header.size_dt_strings; - if (strings_block_size > blob_size) { + if (strings_block_size > raw_device_tree.size()) { if (verbose == Verbose::Yes) - warnln("FDT Header reports invalid StringsBlock size: {} is too large given total size {}", strings_block_size, blob_size); + warnln("FDT Header reports invalid StringsBlock size: {} is too large given total size {}", strings_block_size, raw_device_tree.size()); return false; } return true; } -bool dump(FlattenedDeviceTreeHeader const& header, u8 const* blob_start, size_t blob_size) +ErrorOr<void> dump(FlattenedDeviceTreeHeader const& header, ReadonlyBytes raw_device_tree) { outln("/dts-v1/;"); outln("// magic: 0x{:08x}", header.magic); @@ -125,11 +126,11 @@ bool dump(FlattenedDeviceTreeHeader const& header, u8 const* blob_start, size_t outln("// size_dt_strings: 0x{:08x}", header.size_dt_strings); outln("// size_dt_struct: 0x{:08x}", header.size_dt_struct); - if (!validate_flattened_device_tree(header, blob_start, blob_size, Verbose::Yes)) - return false; + if (!validate_flattened_device_tree(header, raw_device_tree, Verbose::Yes)) + return Error::from_errno(EINVAL); // Now that we know the device tree is valid, print out the rest of the information - auto* mem_reserve_block = reinterpret_cast<FlattenedDeviceTreeReserveEntry const*>(&blob_start[header.off_mem_rsvmap]); + auto const* mem_reserve_block = reinterpret_cast<FlattenedDeviceTreeReserveEntry const*>(&raw_device_tree[header.off_mem_rsvmap]); u64 next_block_offset = header.off_mem_rsvmap + sizeof(FlattenedDeviceTreeReserveEntry); while ((next_block_offset < header.off_dt_struct) && (*mem_reserve_block != FlattenedDeviceTreeReserveEntry {})) { outln("/memreserve/ 0x{:08x} 0x{:08x};", mem_reserve_block->address, mem_reserve_block->size); @@ -137,7 +138,7 @@ bool dump(FlattenedDeviceTreeHeader const& header, u8 const* blob_start, size_t next_block_offset += sizeof(FlattenedDeviceTreeReserveEntry); } - return true; + return {}; } } // namespace DeviceTree |