FlattenedDeviceTree.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/ByteBuffer.h>
  7. #include <AK/Error.h>
  8. #include <AK/IterationDecision.h>
  9. #include <AK/MemoryStream.h>
  10. #include <AK/StringView.h>
  11. #include <LibDeviceTree/FlattenedDeviceTree.h>
  12. namespace DeviceTree {
  13. static ErrorOr<StringView> read_string_view(ReadonlyBytes bytes, StringView error_string)
  14. {
  15. auto len = strnlen(reinterpret_cast<char const*>(bytes.data()), bytes.size());
  16. if (len == bytes.size()) {
  17. return Error::from_string_view_or_print_error_and_return_errno(error_string, EINVAL);
  18. }
  19. return StringView { bytes.slice(0, len) };
  20. }
  21. ErrorOr<void> walk_device_tree(FlattenedDeviceTreeHeader const& header, ReadonlyBytes raw_device_tree, DeviceTreeCallbacks callbacks)
  22. {
  23. ReadonlyBytes struct_bytes { raw_device_tree.data() + header.off_dt_struct, header.size_dt_struct };
  24. FixedMemoryStream stream(struct_bytes);
  25. char const* begin_strings_block = reinterpret_cast<char const*>(raw_device_tree.data() + header.off_dt_strings);
  26. FlattenedDeviceTreeTokenType prev_token = EndNode;
  27. StringView current_node_name;
  28. while (!stream.is_eof()) {
  29. auto current_token = TRY(stream.read_value<BigEndian<u32>>());
  30. switch (current_token) {
  31. case BeginNode: {
  32. current_node_name = TRY(read_string_view(struct_bytes.slice(stream.offset()), "Non-null terminated name for FDT_BEGIN_NODE token!"sv));
  33. size_t const consume_len = round_up_to_power_of_two(current_node_name.length() + 1, 4);
  34. TRY(stream.discard(consume_len));
  35. if (callbacks.on_node_begin) {
  36. if (IterationDecision::Break == TRY(callbacks.on_node_begin(current_node_name)))
  37. return {};
  38. }
  39. break;
  40. }
  41. case EndNode:
  42. if (callbacks.on_node_end) {
  43. if (IterationDecision::Break == TRY(callbacks.on_node_end(current_node_name)))
  44. return {};
  45. }
  46. break;
  47. case Property: {
  48. if (prev_token == EndNode) {
  49. return Error::from_string_view_or_print_error_and_return_errno("Invalid node sequence, FDT_PROP after FDT_END_NODE"sv, EINVAL);
  50. }
  51. auto len = TRY(stream.read_value<BigEndian<u32>>());
  52. auto nameoff = TRY(stream.read_value<BigEndian<u32>>());
  53. if (nameoff >= header.size_dt_strings) {
  54. return Error::from_string_view_or_print_error_and_return_errno("Invalid name offset in FDT_PROP"sv, EINVAL);
  55. }
  56. size_t const prop_name_max_len = header.size_dt_strings - nameoff;
  57. size_t const prop_name_len = strnlen(begin_strings_block + nameoff, prop_name_max_len);
  58. if (prop_name_len == prop_name_max_len) {
  59. return Error::from_string_view_or_print_error_and_return_errno("Non-null terminated name for FDT_PROP token!"sv, EINVAL);
  60. }
  61. StringView prop_name(begin_strings_block + nameoff, prop_name_len);
  62. if (len >= stream.remaining()) {
  63. return Error::from_string_view_or_print_error_and_return_errno("Property value length too large"sv, EINVAL);
  64. }
  65. ReadonlyBytes prop_value;
  66. if (len != 0) {
  67. prop_value = { struct_bytes.slice(stream.offset()).data(), len };
  68. size_t const consume_len = round_up_to_power_of_two(static_cast<u32>(len), 4);
  69. TRY(stream.discard(consume_len));
  70. }
  71. if (callbacks.on_property) {
  72. if (IterationDecision::Break == TRY(callbacks.on_property(prop_name, prop_value)))
  73. return {};
  74. }
  75. break;
  76. }
  77. case NoOp:
  78. if (callbacks.on_noop) {
  79. if (IterationDecision::Break == TRY(callbacks.on_noop()))
  80. return {};
  81. }
  82. break;
  83. case End: {
  84. if (prev_token == BeginNode || prev_token == Property) {
  85. return Error::from_string_view_or_print_error_and_return_errno("Invalid node sequence, FDT_END after BEGIN_NODE or PROP"sv, EINVAL);
  86. }
  87. if (!stream.is_eof()) {
  88. return Error::from_string_view_or_print_error_and_return_errno("Expected EOF at FTD_END but more data remains"sv, EINVAL);
  89. }
  90. if (callbacks.on_end) {
  91. return callbacks.on_end();
  92. }
  93. return {};
  94. }
  95. default:
  96. return Error::from_string_view_or_print_error_and_return_errno("Invalid token"sv, EINVAL);
  97. }
  98. prev_token = static_cast<FlattenedDeviceTreeTokenType>(static_cast<u32>(current_token));
  99. }
  100. return Error::from_string_view_or_print_error_and_return_errno("Unexpected end of stream"sv, EINVAL);
  101. }
  102. static ErrorOr<ReadonlyBytes> slow_get_property_raw(StringView name, FlattenedDeviceTreeHeader const& header, ReadonlyBytes raw_device_tree)
  103. {
  104. // Name is a path like /path/to/node/property
  105. Vector<StringView, 16> path;
  106. TRY(name.for_each_split_view('/', SplitBehavior::Nothing, [&path](StringView view) -> ErrorOr<void> {
  107. if (path.size() == path.capacity()) {
  108. return Error::from_errno(ENAMETOOLONG);
  109. }
  110. // This can never fail as all entries go into the inline buffer, enforced by the check above.
  111. path.unchecked_append(view);
  112. return {};
  113. }));
  114. bool check_property_name = path.size() == 1; // Properties on root node should be checked immediately
  115. ssize_t current_path_idx = -1; // Start "before" the root FDT_BEGIN_NODE tag
  116. ReadonlyBytes found_property_value;
  117. DeviceTreeCallbacks callbacks = {
  118. .on_node_begin = [&](StringView token_name) -> ErrorOr<IterationDecision> {
  119. if (current_path_idx < 0) {
  120. ++current_path_idx; // Root node
  121. return IterationDecision::Continue;
  122. }
  123. if (token_name == path[current_path_idx]) {
  124. ++current_path_idx;
  125. if (current_path_idx == static_cast<ssize_t>(path.size() - 1)) {
  126. check_property_name = true;
  127. }
  128. }
  129. return IterationDecision::Continue;
  130. },
  131. .on_node_end = [&](StringView) -> ErrorOr<IterationDecision> {
  132. if (check_property_name) {
  133. // Not found, but we were looking for the property
  134. return Error::from_errno(EINVAL);
  135. }
  136. return IterationDecision::Continue;
  137. },
  138. .on_property = [&](StringView property_name, ReadonlyBytes property_value) -> ErrorOr<IterationDecision> {
  139. if (check_property_name && property_name == path[current_path_idx]) {
  140. found_property_value = property_value;
  141. return IterationDecision::Break;
  142. }
  143. return IterationDecision::Continue;
  144. },
  145. .on_noop = nullptr,
  146. .on_end = [&]() -> ErrorOr<void> {
  147. return Error::from_string_view_or_print_error_and_return_errno("Property not found"sv, EINVAL);
  148. }
  149. };
  150. TRY(walk_device_tree(header, raw_device_tree, move(callbacks)));
  151. return found_property_value;
  152. }
  153. template<typename T>
  154. ErrorOr<T> slow_get_property(StringView name, FlattenedDeviceTreeHeader const& header, ReadonlyBytes raw_device_tree)
  155. {
  156. [[maybe_unused]] auto bytes = TRY(slow_get_property_raw(name, header, raw_device_tree));
  157. if constexpr (IsVoid<T>) {
  158. return {};
  159. } else if constexpr (IsArithmetic<T>) {
  160. if (bytes.size() != sizeof(T)) {
  161. return Error::from_errno(EINVAL);
  162. }
  163. return *bit_cast<T*>(bytes.data());
  164. } else {
  165. static_assert(IsSame<T, StringView>);
  166. return T(bytes);
  167. }
  168. }
  169. template ErrorOr<void> slow_get_property(StringView name, FlattenedDeviceTreeHeader const& header, ReadonlyBytes raw_device_tree);
  170. template ErrorOr<u32> slow_get_property(StringView name, FlattenedDeviceTreeHeader const& header, ReadonlyBytes raw_device_tree);
  171. template ErrorOr<u64> slow_get_property(StringView name, FlattenedDeviceTreeHeader const& header, ReadonlyBytes raw_device_tree);
  172. template ErrorOr<StringView> slow_get_property(StringView name, FlattenedDeviceTreeHeader const& header, ReadonlyBytes raw_device_tree);
  173. } // namespace DeviceTree