-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
edtlib: add hash
attribute to nodes
#83748
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,6 +72,8 @@ | |
from dataclasses import dataclass | ||
from typing import (Any, Callable, Iterable, NoReturn, | ||
Optional, TYPE_CHECKING, Union) | ||
import base64 | ||
import hashlib | ||
import logging | ||
import os | ||
import re | ||
|
@@ -90,6 +92,14 @@ | |
from devicetree.grutils import Graph | ||
from devicetree._private import _slice_helper | ||
|
||
def _compute_hash(path: str) -> str: | ||
# Calculates the hash associated with the node's full path. | ||
hasher = hashlib.sha256() | ||
hasher.update(path.encode()) | ||
hash = base64.b64encode(hasher.digest()).decode() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why encode it with base64? Maybe use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My first version was with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Understood, return base64.b64encode(hasher.digest(), altchars="__").decode().rstrip("=") |
||
# replace invalid chars and drop padding | ||
return hash.translate(hash.maketrans("+/", "__", "=")) | ||
|
||
# | ||
# Public classes | ||
# | ||
|
@@ -912,6 +922,11 @@ class Node: | |
The ordinal is defined for all Nodes, and is unique among nodes in its | ||
EDT 'nodes' list. | ||
hash: | ||
A hashed value of the devicetree path of the node. This is defined for | ||
all Nodes, and is checked for uniqueness among nodes in its EDT 'nodes' | ||
list. | ||
required_by: | ||
A list with the nodes that directly depend on the node | ||
|
@@ -1027,6 +1042,7 @@ def __init__( | |
self.interrupts: list[ControllerAndData] = [] | ||
self.pinctrls: list[PinCtrl] = [] | ||
self.bus_node = self._bus_node(support_fixed_partitions_on_any_bus) | ||
self.hash: str = _compute_hash(dt_node.path) | ||
|
||
self._init_binding() | ||
self._init_regs() | ||
|
@@ -2270,10 +2286,18 @@ def _init_nodes(self) -> None: | |
# Creates a list of edtlib.Node objects from the dtlib.Node objects, in | ||
# self.nodes | ||
|
||
hash2node: dict[str, Node] = {} | ||
|
||
for dt_node in self._dt.node_iter(): | ||
# Warning: We depend on parent Nodes being created before their | ||
# children. This is guaranteed by node_iter(). | ||
node = Node(dt_node, self, self._fixed_partitions_no_bus) | ||
|
||
if node.hash in hash2node: | ||
_err(f"hash collision between '{node.path}' and " | ||
f"'{hash2node[node.hash].path}'") | ||
hash2node[node.hash] = node | ||
|
||
self.nodes.append(node) | ||
self._node2enode[dt_node] = node | ||
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a test for macro |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this is closely related to the node hierarchy, so HASH should not be used as a secondary naming convention. So, we should clearly label it as the function for obtaining the node's hash.