• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2#coding=utf-8
3
4#
5# Copyright (c) 2022 Huawei Device Co., Ltd.
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19import string
20import json
21import sys
22import os
23import struct
24
25# find out/rk3568/packages/phone/system/ -type f -print | file -f - | grep ELF | cut -d":" -f1 | wc -l
26
27
28class ELFWalker():
29    def __init__(self, product_out_path="./demo/archinfo/assets/rk3568/3.2.7.5"):
30        self._files = []
31        self._links = {}
32        self._walked = False
33        self._product_out_path = product_out_path
34
35    def get_product_images_path(self):
36        return os.path.join(self._product_out_path, "packages/phone/")
37
38    def get_product_out_path(self):
39        return self._product_out_path
40
41    def get_link_file_map(self):
42        if not self._walked:
43            self.__walk_path("packages/phone/system")
44            self.__walk_path("packages/phone/vendor")
45        return self._links
46
47    def get_elf_files(self):
48        if not self._walked:
49            self.__walk_path("packages/phone/system")
50            self.__walk_path("packages/phone/vendor")
51        return self._files
52
53    def __walk_path(self, subdir):
54        for root, subdirs, files in os.walk(os.path.join(self._product_out_path, subdir)):
55            for _filename in files:
56                _asset_file = os.path.join(root, _filename)
57                if os.path.islink(_asset_file):
58                    if _asset_file.find(".so") > 0:
59                        target = os.readlink(_asset_file)
60                        self._links[_asset_file] = target
61                    continue
62                if not os.path.isfile(_asset_file):
63                    continue
64                with open(_asset_file, "rb") as f:
65                    data = f.read(4)
66                    try:
67                        magic = struct.unpack("Bccc", data)
68                        if magic[0] == 0x7F and magic[1] == b'E' and magic[2] == b'L' and magic[3] == b'F':
69                            self._files.append(_asset_file)
70                    except:
71                        pass
72
73        self._walked = True
74
75if __name__ == '__main__':
76    elfFiles = ELFWalker()
77    for f in elfFiles.get_elf_files():
78        print(f)
79    for src, target in elfFiles.get_link_file_map().items():
80        print('{} -> {}'.format(str, target))
81