מחיצות 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 הם זיהויי חומרה אופציונליים של עץ המכשיר, שבהם מערך האתחול יכול להשתמש כדי לזהות ביעילות את ה-DTB או ה-DTBO שרוצים לטעון. אם למחולל האתחול נדרש מידע נוסף, צריך להוסיף אותו ל-DTB או ל-DTBO, כדי שמחולל האתחול יוכל לקרוא אותו על ידי ניתוח ה-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 הוא 0x00006800 ו-custom[0] הוא 0x00000abc.
  • id השלישי הוא 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 זהים.

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
...