wind_follower
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Parsing dtb === ###### tags: `Linux Kernel Study` `dts` `dtb` `device tree` `init` u-boot會透過通用暫存器傳遞dtb的起始位址 在kernel啟動初始階段將dtb的起始位址保存到__atags_pointer, 因此__atags_pointer指向dtb在記憶體中的起始位址。 然後跳轉到 start_kernel() (arch/arm/kernel/head-common.S) (arch/arm/kernel/head.S) ## struct machine_desc 用來描述platform架構的硬體資訊, 以vexpress-a9來說, 該結構體定義在: arch/arm/include/asm/mach/arch.h kernel初始化階段根據dtb選擇machine_desc來基於對不同的platform進行初始化 ## Flow **start_kernel**() (init/main.c)   +--**setup_arch**() (arch/arm/kernel/setup.c)     +-- mdesc = **setup_machine_fdt**(__atags_pointer) (arch/arm/kernel/devtree.c)     |  +-- **of_flat_dt_match_machine**() (/drivers/of/fdt.c)     |  |  +-- **arch_get_next_mach**()     |  |  |  called by callback get_next_compat()     |  |  |      |  |  +-- **of_flat_dt_match**()     |  +-- **early_init_dt_scan_nodes**() (/drivers/of/fdt.c)     |     +-- **of_scan_flat_dt**(early_init_dt_scan_chosen, boot_command_line)     |     |  +-- **early_init_dt_scan_chosen**()     |     +-- **of_scan_flat_dt**(early_init_dt_scan_root, NULL);     |     |  +--**early_init_dt_scan_root**()     |     +-- **of_scan_flat_dt**(early_init_dt_scan_memory, NULL);     |     |  +-- **early_init_dt_scan_memory**()     +-- **unflatten_device_tree**() ## setup_arch ```c= void __init setup_arch(char **cmdline_p) { const struct machine_desc *mdesc; ...... /* 傳入dtb的起始位址__atags_pointer獲取platform的machine_desc結構體*/ mdesc = setup_machine_fdt(__atags_pointer); /* 不是dtb格式的話用atags獲得machine_desc*/ if (!mdesc) mdesc = setup_machine_tags(__atags_pointer, __machine_arch_type); /* 以上兩者都沒有的話, 報err*/ if (!mdesc) { early_print("\nError: invalid dtb and unrecognized/unsupported machine ID\n"); early_print(" r1=0x%08x, r2=0x%08x\n", __machine_arch_type, __atags_pointer); if (__atags_pointer) early_print(" r2[]=%*ph\n", 16, phys_to_virt(__atags_pointer)); dump_machine_table(); } /* 保存machine_desc與其name*/ machine_desc = mdesc; machine_name = mdesc->name; dump_stack_set_arch_desc("%s", mdesc->name); ...... /* 創建device node tree*/ unflatten_device_tree(); ...... } ``` ## setup_machine_fdt ```c= /** * setup_machine_fdt - Machine setup when an dtb was passed to the kernel * @dt_phys: physical address of dt blob * * If a dtb was passed to the kernel in r2, then use it to choose the * correct machine_desc and to setup the system. */ const struct machine_desc * __init setup_machine_fdt(unsigned int dt_phys) { const struct machine_desc *mdesc, *mdesc_best = NULL; ...... if (!dt_phys || !early_init_dt_verify(phys_to_virt(dt_phys))) return NULL; /* * iterate all machine_desc尋找匹配的machine_desc, * callback arch_get_next_mach用來獲取下一個mdesc */ mdesc = of_flat_dt_match_machine(mdesc_best, arch_get_next_mach); if (!mdesc) { ...... dump_machine_table(); /* does not return */ } /* We really don't want to do this, but sometimes firmware provides buggy data */ if (mdesc->dt_fixup) mdesc->dt_fixup(); /** 獲取dtb的一些資訊*/ early_init_dt_scan_nodes(); /* Change machine number to match the mdesc we're using */ __machine_arch_type = mdesc->nr; return mdesc; } ``` ## of_flat_dt_match_machine ```c= /** * of_flat_dt_match_machine - Iterate match tables to find matching machine. * * @default_match: A machine specific ptr to return in case of no match. * @get_next_compat: callback function to return next compatible match table. * * Iterate through machine match tables to find the best match for the machine * compatible string in the FDT. */ const void * __init of_flat_dt_match_machine(const void *default_match, const void * (*get_next_compat)(const char * const**)) { const void *data = NULL; const void *best_data = default_match; const char *const *compat; unsigned long dt_root; unsigned int best_score = ~1, score = 0; dt_root = of_get_flat_dt_root(); /* iterate all machine_desc, 找到最合適的 struct machine_desc*/ while ((data = get_next_compat(&compat))) { score = of_flat_dt_match(dt_root, compat); if (score > 0 && score < best_score) { best_data = data; best_score = score; } } if (!best_data) { ...... } pr_info("Machine model: %s\n", of_flat_dt_get_machine_name()); return best_data; } ``` ## early_init_dt_scan_nodes 分別獲得dts中 /chosen (u-boot傳送的boot args), {size,address}-cells info, 以及 memory node的資訊 ```c= void __init early_init_dt_scan_nodes(void) { int rc = 0; /* Retrieve various information from the /chosen node */ rc = of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line); if (!rc) pr_warn("No chosen node found, continuing without\n"); /* Initialize {size,address}-cells info */ of_scan_flat_dt(early_init_dt_scan_root, NULL); /* Setup memory, calling early_init_dt_add_memory_arch */ of_scan_flat_dt(early_init_dt_scan_memory, NULL); } ``` #### {size,address}-cells 在 dts中 address-cells代表用多少個cell表示base address, size-cells表示該空間有多大, 如 /arch/arm/boot/dts/vexpress-v2p-ca9.dts address-cells與size-cells皆為1, 因此可以看到在memory node reg的部分表示addr與size各使用一個cell, 0x60000000 為 memery的base address 0x40000000 為 memory的大小, 為 1 GB ```c= /dts-v1/; #include "vexpress-v2m.dtsi" / { model = "V2P-CA9"; ...... compatible = "arm,vexpress,v2p-ca9", "arm,vexpress"; ...... #address-cells = <1>; #size-cells = <1>; chosen { }; ...... memory@60000000 { device_type = "memory"; reg = <0x60000000 0x40000000>; }; ``` ## of_scan_flat_dt 在early_init_dt_scan_nodes呼叫三次of_scan_flat_dt,分別傳入callback early_init_dt_scan_chosen, early_init_dt_scan_root, early_init_dt_scan_memory 以分別獲取dtb的/chosen, {size,address}-cells, memory資訊 of_scan_flat_dt會iterate dtb的節點, 並呼叫callback it, callback return 1則停止iterate, 回傳0,代表不是這個節點, 繼續iterate ```c= /** * of_scan_flat_dt - scan flattened tree blob and call callback on each. * @it: callback function * @data: context data pointer * * This function is used to scan the flattened device-tree, it is * used to extract the memory information at boot before we can * unflatten the tree */ int __init of_scan_flat_dt(int (*it)(unsigned long node, const char *uname, int depth, void *data), void *data) { const void *blob = initial_boot_params; /*blob指向dtb起始?*/ const char *pathp; int offset, rc = 0, depth = -1; if (!blob) return 0; for (offset = fdt_next_node(blob, -1, &depth); /* 從根node開始iterate, 呼叫callback*/ offset >= 0 && depth >= 0 && !rc; /** rc=0繼續for迴圈*/ offset = fdt_next_node(blob, offset, &depth)) { /*繼續下一個節點*/ pathp = fdt_get_name(blob, offset, NULL); /*獲取節點名稱*/ rc = it(offset, pathp, depth, data); /* 呼叫 callback, return 1則停止for迴圈*/ } return rc; } ``` ## unflatten_device_tree 將 dtb 轉換為樹狀結構,每個節點保存在struct device_node中 #### struct device_node 該結構用來保存 device_tree節點 ```c= struct device_node { const char *name; phandle phandle; const char *full_name; struct fwnode_handle fwnode; struct property *properties; struct property *deadprops; /* removed properties */ struct device_node *parent; struct device_node *child; struct device_node *sibling; #if defined(CONFIG_OF_KOBJ) struct kobject kobj; #endif unsigned long _flags; void *data; #if defined(CONFIG_SPARC) unsigned int unique_id; struct of_irq_controller *irq_trans; #endif }; ``` device_node內嵌的property結構描述節點的屬性 ```c= struct property { char *name; int length; void *value; struct property *next; #if defined(CONFIG_OF_DYNAMIC) || defined(CONFIG_SPARC) unsigned long _flags; #endif #if defined(CONFIG_OF_PROMTREE) unsigned int unique_id; #endif #if defined(CONFIG_OF_KOBJ) struct bin_attribute attr; #endif }; ``` ```c= /dts-v1/; #include "vexpress-v2m.dtsi" / { /* root節點 (struct device_node)*/ /* 這些是struct property*/ model = "V2P-CA9"; ...... compatible = "arm,vexpress,v2p-ca9", "arm,vexpress"; ...... #address-cells = <1>; #size-cells = <1>; chosen { }; ...... memory@60000000 { /* 節點node :struct device_info*/ /* 這些是struct property*/ device_type = "memory"; reg = <0x60000000 0x40000000>; }; ``` ![](https://i.imgur.com/HWSDelR.png)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully