github.com/google/trillian-examples@v0.0.0-20240520080811-0d40d35cef0e/binary_transparency/firmware/devices/usbarmory/bootloader/dtb.go (about)

     1  // https://github.com/usbarmory/armory-boot
     2  //
     3  // Copyright (c) F-Secure Corporation
     4  // https://foundry.f-secure.com
     5  //
     6  // Use of this source code is governed by the license
     7  // that can be found in the LICENSE file.
     8  
     9  //go:build armory
    10  // +build armory
    11  
    12  package main
    13  
    14  import (
    15  	"bytes"
    16  
    17  	"github.com/u-root/u-root/pkg/dt"
    18  )
    19  
    20  func fixupDeviceTree(dtb []byte, cmdline string) (dtbFixed []byte, err error) {
    21  	fdt, err := dt.ReadFDT(bytes.NewReader(dtb))
    22  
    23  	if err != nil {
    24  		return
    25  	}
    26  
    27  	for _, node := range fdt.RootNode.Children {
    28  		if node.Name == "chosen" {
    29  			bootargs := dt.Property{
    30  				Name:  "bootargs",
    31  				Value: []byte(cmdline + "\x00"),
    32  			}
    33  
    34  			node.Properties = append(node.Properties, bootargs)
    35  		}
    36  
    37  		// temporary fixup until newer dtbs with correct device_type
    38  		// information are released
    39  		if node.Name == "memory" {
    40  			device_type := dt.Property{
    41  				Name:  "device_type",
    42  				Value: []byte("memory" + "\x00"),
    43  			}
    44  
    45  			node.Properties = append(node.Properties, device_type)
    46  		}
    47  	}
    48  
    49  	dtbBuf := new(bytes.Buffer)
    50  	_, err = fdt.Write(dtbBuf)
    51  
    52  	return dtbBuf.Bytes(), err
    53  }