github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/pkg/boot/netboot/simple/simple.go (about)

     1  // Copyright 2017-2021 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package simple
     6  
     7  import (
     8  	"context"
     9  	"fmt"
    10  	"io"
    11  	l "log"
    12  	"math"
    13  	"net/url"
    14  
    15  	"github.com/mvdan/u-root-coreutils/pkg/boot"
    16  	"github.com/mvdan/u-root-coreutils/pkg/boot/fit"
    17  	"github.com/mvdan/u-root-coreutils/pkg/curl"
    18  )
    19  
    20  // FetchAndProbe fetches the file at the specified URL and checks if it is an
    21  // Image file type rather than a config such as ipxe.
    22  // TODO: detect nonFIT multiboot and bzImage Linux kernel files
    23  func FetchAndProbe(ctx context.Context, u *url.URL, s curl.Schemes) ([]boot.OSImage, error) {
    24  	file, err := s.Fetch(ctx, u)
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  	var images []boot.OSImage
    29  
    30  	fimgs, err := fit.ParseConfig(io.NewSectionReader(file, 0, math.MaxInt64))
    31  	if err == nil {
    32  		for i := range fimgs {
    33  			images = append(images, &fimgs[i])
    34  		}
    35  	} else {
    36  		l.Printf("Parsing boot file as FIT image failed: %v", err)
    37  	}
    38  
    39  	if len(images) == 0 {
    40  		return nil, fmt.Errorf("exhausted all supported simple file types")
    41  	}
    42  	return images, nil
    43  }