xref: /rk3399_ARM-atf/tools/memory/src/memory/image.py (revision d8fdff38b544b79c4f0b757e3b3c82ce9c8a2f9e)
1#
2# Copyright (c) 2023-2025, Arm Limited. All rights reserved.
3#
4# SPDX-License-Identifier: BSD-3-Clause
5#
6
7from abc import ABC, abstractmethod
8from dataclasses import dataclass
9from typing import Dict, Optional
10
11
12@dataclass
13class Region:
14    """Represents a memory region."""
15
16    start: Optional[int] = None
17    """Memory address of the beginning of the region."""
18
19    end: Optional[int] = None
20    """Memory address of the end of the region."""
21
22    length: Optional[int] = None
23    """Current size of the region in bytes."""
24
25    @property
26    def limit(self) -> Optional[int]:
27        """Largest possible end memory address of the region."""
28
29        if self.start is None:
30            return None
31
32        if self.length is None:
33            return None
34
35        return self.start + self.length
36
37    @property
38    def size(self) -> Optional[int]:
39        """Maximum possible size of the region in bytes."""
40
41        if self.end is None:
42            return None
43
44        if self.start is None:
45            return None
46
47        return self.end - self.start
48
49    @property
50    def free(self) -> Optional[int]:
51        """Number of bytes that the region is permitted to further expand."""
52
53        if self.limit is None:
54            return None
55
56        if self.end is None:
57            return None
58
59        return self.limit - self.end
60
61
62class Image(ABC):
63    """An image under analysis."""
64
65    @property
66    @abstractmethod
67    def footprint(self) -> Dict[str, Region]:
68        """Get metrics about the memory regions that this image occupies."""
69
70        pass
71
72    @property
73    @abstractmethod
74    def symbols(self) -> Dict[str, int]:
75        """Get a dictionary of the image's symbols and their corresponding addresses."""
76
77        pass
78