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

     1  // Copyright 2020 Google LLC. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package dummy provides a fake device to demo flashing firmware.
    16  package dummy
    17  
    18  import (
    19  	"encoding/json"
    20  	"fmt"
    21  	"os"
    22  	"path/filepath"
    23  
    24  	"github.com/google/trillian-examples/binary_transparency/firmware/api"
    25  	"github.com/google/trillian-examples/binary_transparency/firmware/cmd/flash_tool/devices"
    26  )
    27  
    28  const (
    29  	bundlePath   = "bundle.json"
    30  	firmwarePath = "firmware.bin"
    31  )
    32  
    33  // Device is a fake device using the local filesystem for storage.
    34  type Device struct {
    35  	// bundle holds all the update data except the firmware image.
    36  	bundle api.ProofBundle
    37  
    38  	storage string
    39  }
    40  
    41  var _ devices.Device = Device{}
    42  
    43  // New creates a new dummy device instance using data from flags.
    44  // TODO(al): figure out how/whether to remove the flag from in here.
    45  func New(storage string) (*Device, error) {
    46  	dStat, err := os.Stat(storage)
    47  	if err != nil {
    48  		return nil, fmt.Errorf("unable to stat device storage dir %q: %w", storage, err)
    49  	}
    50  	if !dStat.Mode().IsDir() {
    51  		return nil, fmt.Errorf("device storage %q is not a directory", storage)
    52  	}
    53  
    54  	d := &Device{
    55  		storage: storage,
    56  	}
    57  
    58  	fPath := filepath.Join(storage, bundlePath)
    59  	f, err := os.OpenFile(fPath, os.O_RDONLY, os.ModePerm)
    60  	if err != nil {
    61  		if os.IsNotExist(err) {
    62  			return d, devices.ErrNeedsInit(fmt.Errorf("couldn't read bundle file %q: %w", fPath, err))
    63  		}
    64  		return d, fmt.Errorf("failed to read bundle file %q: %w", fPath, err)
    65  	}
    66  	defer func() {
    67  		_ = f.Close()
    68  	}()
    69  
    70  	err = json.NewDecoder(f).Decode(&d.bundle)
    71  	return d, err
    72  }
    73  
    74  // DeviceCheckpoint returns the latest log checkpoint stored on the device.
    75  func (d Device) DeviceCheckpoint() ([]byte, error) {
    76  	return d.bundle.Checkpoint, nil
    77  }
    78  
    79  // ApplyUpdate applies the firmware update to the dummy device.
    80  // The firmware image is stored in the dummy state directory in the firmware.bin file,
    81  // and the rest of the update bundle is stored in the bundle.json file.
    82  func (d Device) ApplyUpdate(u api.UpdatePackage) error {
    83  	fwFile := filepath.Join(d.storage, firmwarePath)
    84  	bundleFile := filepath.Join(d.storage, bundlePath)
    85  
    86  	if err := os.WriteFile(bundleFile, u.ProofBundle, os.ModePerm); err != nil {
    87  		return fmt.Errorf("failed to write proof bundle to %q: %q", bundleFile, err)
    88  	}
    89  
    90  	fw := u.FirmwareImage
    91  	if err := os.WriteFile(fwFile, fw, os.ModePerm); err != nil {
    92  		return fmt.Errorf("failed to write firmware image to %q: %q", fwFile, err)
    93  	}
    94  
    95  	return nil
    96  }