intel-device-plugins-for-ku.../pkg/fpga/bitstream/bitstream.go
Mikko Ylinen f208a74f85 fpga/bitstream: use consts as much as possible
Signed-off-by: Mikko Ylinen <mikko.ylinen@intel.com>
2023-11-04 09:04:28 +02:00

56 lines
1.6 KiB
Go

// Copyright 2019 Intel Corporation. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bitstream
import (
"os"
"path/filepath"
"github.com/pkg/errors"
)
// GetFPGABitstream scans bitstream storage and returns first found bitstream by region and afu id.
func GetFPGABitstream(bitstreamDir, region, afu string) (File, error) {
bitstreamPath := ""
for _, ext := range []string{fileExtensionGBS, fileExtensionAOCX} {
bitstreamPath = filepath.Join(bitstreamDir, region, afu+ext)
_, err := os.Stat(bitstreamPath)
if os.IsNotExist(err) {
continue
}
if err != nil {
return nil, errors.Errorf("%s: stat error: %v", bitstreamPath, err)
}
return Open(bitstreamPath)
}
return nil, errors.Errorf("%s/%s: bitstream not found", region, afu)
}
// Open bitstream file, detecting type based on the filename extension.
func Open(fname string) (File, error) {
switch filepath.Ext(fname) {
case fileExtensionGBS:
return OpenGBS(fname)
case fileExtensionAOCX:
return OpenAOCX(fname)
}
return nil, errors.Errorf("unsupported file format %s", fname)
}