Linux dts list python tool

Windows Notepad - Save as - Encoding (ANSI)
Windows Notepad - Edit - Replace... - replaces ? with blank

@ gv.py
out_file = ''
temp_file = ''
dt_dir_path = ''
nr_plats = 1
is_oem = False
is_ext = False
dts_name = ''

arg0_proc_name = ''
arg1_plat_name = ''

@ dts_list.py
################################################
# Show dts tree for msm platform
# 2015-2016, all rights reserved.
################################################

import sys,os
import shutil
import gv

def print_usage(toast):
    if toast != '':
        print('\n' + toast)
    print("\nUsage: python " + gv.arg0_proc_name + " [OPTION...]\n")
    print("    If you want to print all the DT of one platform, you should")
    print("    assign the directory as the first argument, otherwise it ")
    print("    will use the current work directory.")
    print("    -o    list all the .dtsi files which belong to a .dts to")
    print("        a file")
    print("    -e    list all the .dtsi files which belong to a .dts to")
    print("        a file and copy the assciated .dtsi files to")
    print("        ." + os.path.sep + 'EXT\n')
    print("For example:")
    print("    Linux:")
    print("    python " + gv.arg0_proc_name + " msm8909")
    print("    python " + gv.arg0_proc_name + " ./ msm8909")
    print("    python " + gv.arg0_proc_name + " -o msm8909-1gb-qrd-skuc.dts")
    print("    python " + gv.arg0_proc_name + " -e msm8909-1gb-qrd-skuc.dts\n")
    print("    vi KERNEL-msm8909.list")
    print("    vi OEM-msm8909-1gb-qrd-skuc.dts.list")
    print("    vi ./EXT/OEM-msm8909-1gb-qrd-skuc.dts.list\n")

    print("    <------------------------------------------------>\n")
    print("    Windows:")
    print("    python " + gv.arg0_proc_name + " msm8909")
    print("    python " + gv.arg0_proc_name + " E:\F\case\dts_test msm8909")
    print("    python " + gv.arg0_proc_name + " -o E:\F\case\dts_test\msm8909-1gb-qrd-skuc.dts")
    print("    python " + gv.arg0_proc_name + " -e E:\F\case\dts_test\msm8909-1gb-qrd-skuc.dts\n")
    print("    Open E:\F\case\dts_test\KERNEL-msm8909.list with text editor")
    print("    Open E:\F\case\dts_test\OEM-msm8909-1gb-qrd-skuc.dts.list ")
    print("        with text editor")
    print("    Open E:\F\case\dts_test\EXT\OEM-msm8909-1gb-qrd-skuc.dts.list ")
    print("        with text editor\n")

def write_file(f_name, line):
    h = open(f_name, 'a+')
    h.writelines(line)
    h.close()

def contains_substring(src, sub):
    if sub in src:
        return True
    elif src.startswith(sub):
        return True
    else:
        return False

def get_dt_list(dir_path, temp_file):
    for dts_file in os.listdir(dir_path):
        if contains_substring(dts_file, gv.arg1_plat_name) > 0:
            write_file(temp_file, dir_path + os.path.sep + dts_file + '\n')

def list_one_tree(dt_file_path, h, out_file):
    fp = open(dt_file_path, 'r')
    for line in fp:
        p = line.lstrip(' ')
        if p.startswith('/*') or p.startswith('//'):
            continue

        if line.find('include') > 0 and line.find(".dts") > 0:
            if line.find("/include/") > 0:
                idx = line.find('/include/')
                idx += 9
                p = line[idx:len(line)]
            elif line.find("#include") > 0:
                idx = line.find('#include')
                idx += 8
                p = line[idx:len(line)]
            else:
                idx = line.find('include')
                idx += 7
                p = line[idx:len(line)]

            p = p.lstrip('/').lstrip(' ').lstrip('\"').rstrip('\"')
            p = p.rstrip('\r').rstrip('\n').rstrip('\"')
            write_file(out_file, h + p + '\n')

            # Get the next
            p = p.rstrip('\r').rstrip('\n').lstrip('\"').rstrip('\"')
            p = gv.dt_dir_path + os.path.sep + p
            if os.path.exists(p):
                list_one_tree(p, h + '-', out_file)
    fp.close()

def walk_dt(dt_file_path, out_file, is_oem):
    p = dt_file_path

    idx = dt_file_path.find(os.path.sep)
    if idx > 0:
        idx += 1
        p = dt_file_path[idx:len(dt_file_path)]

    if p != '':
        write_file(out_file,
                "/**************(" + str(gv.nr_plats) +
        ") platform*****************/\n")
        gv.nr_plats += 1

        p = p.rstrip('\r').rstrip('\n')
        idx = p.rfind(os.path.sep)
        if idx > 0:
            idx += 1
            _dts = p[idx:]
        else:
            _dts = p
        write_file(out_file, _dts + '\n')

        dt_file_path = dt_file_path.rstrip('\r').rstrip('\n')
        list_one_tree(dt_file_path, "-", out_file)
        write_file(out_file, "\n")

def _show_dt(dir_path):
    h = open(gv.temp_file, 'r')
    for line in h:
        line = line.rstrip('\r').rstrip('\n')
        if line.endswith('.dts') == True:
            walk_dt(line, gv.out_file, False)
    h.close();

def create_file(file):
    if os.path.exists(file):
        os.remove(file)

    h = open(file, 'w')
    h.write('Author: George Tso\n')
    h.write('If you have any good advice, you can contact me directly\n\n')
    h.close()

def remove_file(file):
    if os.path.exists(file):
        os.remove(file)

def show_dt():
    create_file(gv.temp_file)

    if len(gv.dt_dir_path) > 1:
        get_dt_list(gv.dt_dir_path, gv.temp_file)
        _show_dt(gv.dt_dir_path)

    remove_file(gv.temp_file)

def remove_dir(dir_path):
    if os.path.isdir(dir_path):
        for file in os.listdir(dir_path):
            if (os.path.isdir(dir_path + os.path.sep + file)):
                remove_dir(dir_path + os.path.sep + file)
            else:
                os.remove(dir_path + os.path.sep + file)
        os.removedirs(dir_path)

def extract_one_DT_to_EXT(file_path):
    if not os.path.exists(file_path):
        return

    is_found = False
    src_dir = gv.dt_dir_path
    dst_dir = gv.dt_dir_path

    h = open(file_path, 'r')
    for line in h:
        if line.find('*') > 0:
            is_found = True
            continue
        if (is_found == False):
            continue
        line = line.lstrip('-').lstrip('/').lstrip('\\')
        line = line.rstrip('\r').rstrip('\n').rstrip('\"')
        src_path = src_dir + os.path.sep + line
        dst_path = dst_dir + os.path.sep + 'EXT' + os.path.sep + line
        if os.path.exists(src_path) and not os.path.isdir(src_path):
            idx = dst_path.rfind(os.path.sep)
            tmp_dst_dir = dst_path[0:idx]
            if (os.path.exists(tmp_dst_dir) == False):
                os.makedirs(tmp_dst_dir)
            shutil.copy(src_path, dst_path)

def parse_args():
    if len(sys.argv) == 2:
        gv.dt_dir_path = os.getcwd()
        gv.arg1_plat_name = sys.argv[1]
    elif len(sys.argv) == 3:
        if not os.path.isdir(sys.argv[1]) and cmp(sys.argv[1], '-o') and cmp(sys.argv[1], '-e'):
            print_usage()
            sys.exit(0)
        elif not cmp(sys.argv[1], '-o'):
            gv.is_oem = True
            idx = sys.argv[2].rfind(os.path.sep)
            if idx > 0:
                idx += 1
                gv.dt_dir_path = sys.argv[2][0:idx]
                gv.dts_name = sys.argv[2][idx:]
            else:
                gv.dt_dir_path = os.getcwd()
                gv.dts_name = sys.argv[2]
        elif not cmp(sys.argv[1], '-e'):
            gv.is_ext = True
            idx = sys.argv[2].rfind(os.path.sep)
            if idx > 0:
                idx += 1
                gv.dt_dir_path = sys.argv[2][0:idx]
                gv.dts_name = sys.argv[2][idx:]
            else:
                gv.dt_dir_path = os.getcwd()
                gv.dts_name = sys.argv[2]
        else:
            gv.dt_dir_path = sys.argv[1]
            gv.arg1_plat_name = sys.argv[2]

    gv.dt_dir_path = gv.dt_dir_path.rstrip('\r').rstrip('\n')
    gv.temp_file = gv.dt_dir_path + os.path.sep + '.tmp.log'
    if gv.is_oem == True:
        gv.out_file = gv.dt_dir_path + os.path.sep + 'OEM-' + gv.dts_name + '.list'
    elif gv.is_ext == True:
        remove_dir(gv.dt_dir_path + os.path.sep + 'EXT')
        os.makedirs(gv.dt_dir_path + os.path.sep + 'EXT')
        gv.out_file = gv.dt_dir_path + os.path.sep + 'EXT' + os.path.sep + 'OEM-' + gv.dts_name + '.list'
    else:
        gv.out_file = gv.dt_dir_path + os.path.sep + 'KERNEL-' + gv.arg1_plat_name + '.list'

    print('\nDT directory: ' + gv.dt_dir_path)
    if gv.is_ext:
        print('\nEXT directory: ' + gv.dt_dir_path + os.path.sep + 'EXT' + os.path.sep)
    print('\nout file: ' + gv.out_file + '\n')
    create_file(gv.out_file)

def main():
    gv.arg0_proc_name = sys.argv[0]
    if sys.argv[0].rfind(os.path.sep) > 0 :
        index = sys.argv[0].rfind(os.path.sep)
        gv.arg0_proc_name = sys.argv[0][index+1:]

    if len(sys.argv) < 2:
        print_usage('')
        sys.exit(0)

    parse_args()

    if gv.is_ext == True:
        walk_dt(gv.dt_dir_path + os.path.sep + gv.dts_name,
        gv.out_file, True)
        extract_one_DT_to_EXT(gv.out_file)
    elif gv.is_oem == True:
            walk_dt(gv.dt_dir_path + os.path.sep + gv.dts_name,
        gv.out_file, True)
    else:
        if gv.arg1_plat_name.find('.dts') > 0 or gv.arg1_plat_name.find('dtsi') > 0:
            print_usage('Invalid arguments')
            sys.exit(0)
        show_dt()

if __name__ == '__main__':
    main()

  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 《Linux DTS详解.pdf》是一本关于Linux设备树(Device Tree Source,简称DTS)的详细讲解和介绍的电子书。 Linux设备树是一种用于描述硬件设备的数据结构,它提供了一种通用的方式来描述和配置嵌入式系统中的硬件设备,包括处理器、外设、中断控制器等。使用设备树可以使Linux内核可以在不同的硬件平台上运行,而不需要修改内核代码。 该电子书首先介绍了设备树的基本概念和原理,包括设备树的语法、节点和属性的描述方式等。然后详细讲解了设备树的编译和加载流程,包括如何生成设备树二进制文件、如何将设备树绑定到内核中以及设备树的动态更新等。 接着,电子书对设备树的各种用途进行了详细的讲解。其中包括如何描述处理器、内存和中断控制器等基本设备,如何描述各种外设,以及如何描述多处理器系统中的设备亲和性等。此外,还介绍了如何使用设备树来配置Linux内核和设备驱动,以及如何在设备树中定义和使用设备树覆盖(Device Tree Overlay)。 最后,电子书还介绍了一些实际的案例和示例,帮助读者更好地理解和应用设备树。通过阅读该电子书,读者可以全面了解设备树在嵌入式系统中的作用和用法,并能够灵活地应用设备树来描述和配置自己的硬件设备。 《Linux DTS详解.pdf》是一本非常有价值的电子书,适合对Linux设备树感兴趣的开发人员和嵌入式系统工程师阅读和学习。 ### 回答2: 《Linux DTS详解》是一本专门讲解Linux设备树文件(Device Tree Source)的书籍。Linux设备树是一种描述硬件结构和配置信息的数据结构,它将硬件设备的信息以一种与硬件无关的方式呈现给Linux内核,使得内核可以根据设备树进行设备的自动识别和配置。 该书详细介绍了设备树的基本概念、结构和语法规则。通过对设备树的解析,读者可以了解如何正确编写设备树文件,以描述自己的硬件设备和系统配置。书中还介绍了如何使用设备树文件来建立硬件与软件之间的对应关系,以及如何利用设备树进行设备的驱动和管理。 此外,书中还介绍了设备树文件在Linux内核中的加载和解析过程,以及如何在启动阶段启用设备树,并将设备树信息传递给内核。读者可以了解设备树在启动过程中的作用和工作原理,并学会如何调试和修改设备树配置。 《Linux DTS详解》还涵盖了设备树在各个架构、子系统和驱动中的应用实例。读者可以了解如何编写适用于不同硬件平台和设备的设备树文件,并掌握设备树的高级用法和技术。 总之,《Linux DTS详解》是一本深入讲解Linux设备树的实用指南,适合Linux系统开发工程师、嵌入式系统工程师和硬件工程师阅读,帮助他们了解和应用设备树技术,提高系统的稳定性和兼容性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值