DTB 和 DTBO 分區

如果裝置樹 Blob (DTB) 或重疊裝置樹 Blob (DTBO) 位於專屬分區 (例如 dtbdtbo 分區),請使用下列表格結構和標頭格式:

圖 1. DTB 和 DTBO 分區版面配置範例。

資料結構

dt_table_header 適用於 dtb/dtbo 分割區;您無法在 image.gz 結尾後附加此格式。如果您只有一個 DTB 或 DTBO,仍必須使用這個格式 (dt_table_header 中的 dt_entry_count 為 1)。

#define DT_TABLE_MAGIC 0xd7b7ab1e

struct dt_table_header {
  uint32_t magic;             // DT_TABLE_MAGIC
  uint32_t total_size;        // includes dt_table_header + all dt_table_entry
                              // and all dtb/dtbo
  uint32_t header_size;       // sizeof(dt_table_header)

  uint32_t dt_entry_size;     // sizeof(dt_table_entry)
  uint32_t dt_entry_count;    // number of dt_table_entry
  uint32_t dt_entries_offset; // offset to the first dt_table_entry
                              // from head of dt_table_header

  uint32_t page_size;         // flash page size we assume
  uint32_t version;       // DTBO image version, the current version is 0.
                          // The version is incremented when the
                          // dt_table_header struct is updated.
};

struct dt_table_entry {
  uint32_t dt_size;
  uint32_t dt_offset;         // offset from head of dt_table_header

  uint32_t id;                // optional, must be zero if unused
  uint32_t rev;               // optional, must be zero if unused
  uint32_t custom[4];         // optional, must be zero if unused
};

如要讀取所有 dt_table_entry,請使用 dt_entry_sizedt_entry_countdt_entries_offset。例子:

my_read(entries_buf,
        header_addr + header->dt_entries_offset,
        header->dt_entry_size * header->dt_entry_count);

dt_table_entry 中的 idrevcustom 是裝置樹狀結構體的選用硬體 ID,可讓引導程式有效地識別要載入的 DTB 或 DTBO。如果引導程式需要其他資訊,請將這些資訊放入 DTB 或 DTBO,讓引導程式透過剖析 DTB 或 DTBO 來讀取這些資訊 (請參閱下方的範例程式碼)。

程式碼範例

以下程式碼範例會檢查引導程式中的硬體 ID。

  • check_dtbo() 函式會檢查硬體 ID。它會先檢查結構體 dt_table_entry 中的資料 (idrev 等)。如果這項資料不足,就會將 dtb 資料載入記憶體,並檢查 dtb 中的值。
  • my_hw_informationsoc_id 屬性的值會在根節點中剖析 (範例在 my_dtbo_1.dts 中)。
    [my_dtbo_1.dts]
    /dts-v1/;
    /plugin/;
    
    / {
      /* As DTS design, these properties only for loader, won't overlay */
      compatible = "board_manufacturer,board_model";
    
      /* These properties are examples */
      board_id = <0x00010000>;
      board_rev = <0x00010001>;
      another_hw_information = "some_data";
      soc_id = <0x68000000>;
      ...
    };
    
    &device@0 {
      value = <0x1>;
      status = "okay";
    };
    
    
    [my_bootloader.c]
    int check_dtbo(const dt_table_entry *entry, uint32_t header_addr) {
      ...
      if (entry->id != ... || entry->rev != ...) {
        ...
      }
      ...
      void * fdt_buf = my_load_dtb(header_addr + entry->dt_offset, entry->dt_size);
      int root_node_off = fdt_path_offset(fdt_buf, "/");
      ...
      const char *my_hw_information =
        (const char *)fdt_getprop(fdt_buf, root_node_off, "my_hw_information", NULL);
      if (my_hw_information != NULL && strcmp(my_hw_information, ...) != 0) {
        ...
      }
      const fdt32_t *soc_id = fdt_getprop(fdt_buf, root_node_off, "soc_id", NULL);
      if (soc_id != NULL && *soc_id != ...) {
        ...
      }
      ...
    }

mkdtimg

mkdtimg 是用來建立 dtb/dtbo 映像檔的工具 (AOSP 中 system/libufdt原始碼)。mkdtimg 支援多個指令,包括 createcfg_createdump

create

使用 create 指令建立 dtb/dtbo 映像檔:

mkdtimg create <image_filename> (<global-option>...) \
    <ftb1_filename> (<entry1_option>...) \
    <ftb2_filename> (<entry2_option>...) \
    ...

ftbX_filename 會在圖片中產生 dt_table_entryentryX_option 是指派給 dt_table_entry 的值。這些值可以是下列任一項目:

--id=<number|path>
--rev=<number|path>
--custom0=<number|path>
--custom1=<number|path>
--custom2=<number|path>
--custom3=<number|path>

數值可以是 32 位元數字 (例如 68000),或十六進位數字 (例如 0x6800)。或者,您也可以使用下列格式指定路徑:

<full_node_path>:<property_name>

例如:/board/:idmkdtimg 會從 DTB 或 DTBO 檔案的路徑讀取值,並將值 (32 位元) 指派給 dt_table_entry 中的相對屬性。或者,您也可以將 global_option 設為所有項目的預設選項。dt_table_headerpage_size 的預設值為 2048;請使用 global_option --page_size=<number> 指派其他值。

例子:

[board1.dts]
/dts-v1/;
/plugin/;

/ {
  compatible = "board_manufacturer,board_model";
  board_id = <0x00010000>;
  board_rev = <0x00010001>;
  another_hw_information = "some_data";
  ...
};

&device@0 {
  value = <0x1>;
  status = "okay";
};


mkdtimg create dtbo.img --id=/:board_id --custom0=0xabc \
  board1.dtbo \
  board2.dtbo --id=0x6800 \
  board3.dtbo --id=0x6801 --custom0=0x123
  • 第一個 dt_table_entry (board1.dtbo) id0x00010000,而 custom[0]0x00000abc
  • 第二個 id0x00006800custom[0] 則是 0x00000abc
  • 第三個 id0x00006801,而 custom[0] 則為 0x00000123
  • 其他所有值都會使用預設值 (0)。

cfg_create

cfg_create 指令會使用以下格式的設定檔建立映像檔:

# global options
  <global_option>
  ...
# entries
<ftb1_filename>     # comment
  <entry1_option>   # comment
  ...
<ftb2_filename>
  <entry2_option>
  ...
...

選項 global_optionentryX_option 必須以一或多個空格字元開頭 (這些選項與 create 選項相同,但沒有 -- 前置字元)。系統會忽略空白行或開頭為 # 的行。

例子:

[dtboimg.cfg]
# global options
  id=/:board_id
  rev=/:board_rev
  custom0=0xabc

board1.dtbo

board2.dtbo
  id=0x6800       # override the value of id in global options

board2.dtbo
  id=0x6801       # override the value of id in global options
  custom0=0x123   # override the value of custom0 in global options


mkdtimg cfg_create dtbo.img dtboimg.cfg

mkdtimg 不會處理 .dtb/.dtbo 檔案的對齊方式,而是會將這些檔案附加至圖片。使用 dtc.dts 編譯為 .dtb/.dtbo 時,必須新增選項 -a。舉例來說,新增 -a 4 選項會增加邊框間距,讓 .dtb/.dtbo 的大小對齊 4 個位元組。

多個 DT 資料表項目可以共用一個 .dtb/.dtbo。如果您為不同的項目使用相同的檔案名稱,則圖片中只會儲存一個內容,且 dt_offsetdt_size 也相同。當您使用不同硬體搭配相同的 DT 時,這項功能就會相當實用。

dump

針對 dtb/dtbo 映像檔,請使用 dump 指令列印映像檔中的資訊。例子:

mkdtimg dump dtbo.img
dt_table_header:
               magic = d7b7ab1e
          total_size = 1300
         header_size = 32
       dt_entry_size = 32
      dt_entry_count = 3
   dt_entries_offset = 32
           page_size = 2048
             version = 0
dt_table_entry[0]:
             dt_size = 380
           dt_offset = 128
                  id = 00010000
                 rev = 00010001
           custom[0] = 00000abc
           custom[1] = 00000000
           custom[2] = 00000000
           custom[3] = 00000000
           (FDT)size = 380
     (FDT)compatible = board_manufacturer,board_model
...