พาร์ติชัน DTB และ DTBO

หาก BLOB แผนผังอุปกรณ์ (DTB) หรือ BLOB แผนผังอุปกรณ์สำหรับการวางซ้อน (DTBO) อยู่ในพาร์ติชันที่ไม่ซ้ำกัน เช่น พาร์ติชัน dtb และ dtbo ให้ใช้โครงสร้างตารางต่อไปนี้ และรูปแบบส่วนหัว

รูปที่ 1 ตัวอย่างเลย์เอาต์พาร์ติชัน DTB และ DTBO

โครงสร้างข้อมูล

dt_table_header มีค่าเท่านั้นสำหรับ พาร์ติชัน dtb/dtbo คุณไม่สามารถต่อท้ายรูปแบบนี้ หลังสิ้นสุด image.gz หากคุณมี DTB หรือ DTBO รายการเดียว คุณต้อง ยังคงใช้รูปแบบนี้ (และ dt_entry_count ใน dt_table_header เท่ากับ 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_size dt_entry_count และ dt_entries_offset ตัวอย่าง

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

id, rev, custom ใน dt_table_entry คือหมายเลขระบุฮาร์ดแวร์ของอุปกรณ์ (ไม่บังคับ) โครงสร้างที่ Bootloader สามารถใช้เพื่อระบุ DTB หรือ DTBO ที่จะโหลดอย่างมีประสิทธิภาพ หาก Bootloader ต้องการข้อมูลเพิ่มเติมใส่ไว้ใน DTB หรือ DTBO โดย Bootloader สามารถอ่านได้โดยการแยกวิเคราะห์ DTB หรือ DTBO (ดูโค้ดตัวอย่างด้านล่าง)

โค้ดตัวอย่าง

โค้ดตัวอย่างต่อไปนี้จะตรวจสอบการระบุฮาร์ดแวร์ใน Bootloader

  • ฟังก์ชัน check_dtbo() จะตรวจสอบการระบุฮาร์ดแวร์ โดยจะตรวจสอบข้อมูลในโครงสร้าง dt_table_entry (id, rev ฯลฯ) หากข้อมูลนี้ไม่เพียงพอ ระบบจะโหลด dtb ลงในหน่วยความจำและตรวจสอบค่าใน dtb
  • ค่าของ my_hw_information และ soc_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 รูป (ที่มา ที่ system/libufdt ใน AOSP) mkdtimg รองรับ หลายคำสั่ง รวมทั้ง create, cfg_create และ dump

สร้าง

ใช้คำสั่ง create เพื่อสร้าง รูปภาพ dtb/dtbo ภาพ:

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

ftbX_filename สร้าง dt_table_entry ใน รูปภาพ entryX_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/:id mkdtimg อ่านค่า จากเส้นทางในไฟล์ DTB หรือ DTBO และกําหนดค่า (32 บิต) ให้กับแบบสัมพัทธ์ ใน dt_table_entry อีกวิธีหนึ่งคือ global_option เป็นตัวเลือกเริ่มต้นสำหรับรายการทั้งหมด ค่าเริ่มต้น ค่าของ page_size ใน dt_table_header คือ 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) id ปัจจุบันเท่ากับ 0x00010000 และcustom[0] เท่ากับ 0x00000abc
  • idที่ 2 คือ 0x00006800 และ custom[0] คือ 0x00000abc
  • idที่ 3 คือ 0x00006801 และ custom[0] มีค่า 0x00000123
  • ส่วนรายการอื่นๆ ทั้งหมดจะใช้ค่าเริ่มต้น (0)

cfg_create

คำสั่ง cfg_create สร้างอิมเมจที่มีไฟล์การกำหนดค่าใน โดยใช้รูปแบบต่อไปนี้

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

ตัวเลือก global_option และ entryX_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_offset และ dt_size เดียวกัน นี่คือ มีประโยชน์เมื่อใช้ฮาร์ดแวร์อื่นที่มี DT เหมือนกัน

ดัมพ์

สำหรับรูปภาพ 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
...